]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/breakpoint.c
Target FP: Add conversion routines to target-float.{c,h}
[thirdparty/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 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
648 {
649 rbreak_start_breakpoint_count = breakpoint_count;
650 }
651
652 /* Called at the end of an "rbreak" command to record the last
653 breakpoint made. */
654
655 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
656 {
657 prev_breakpoint_count = rbreak_start_breakpoint_count;
658 }
659
660 /* Used in run_command to zero the hit count when a new run starts. */
661
662 void
663 clear_breakpoint_hit_counts (void)
664 {
665 struct breakpoint *b;
666
667 ALL_BREAKPOINTS (b)
668 b->hit_count = 0;
669 }
670
671 \f
672 /* Return the breakpoint with the specified number, or NULL
673 if the number does not refer to an existing breakpoint. */
674
675 struct breakpoint *
676 get_breakpoint (int num)
677 {
678 struct breakpoint *b;
679
680 ALL_BREAKPOINTS (b)
681 if (b->number == num)
682 return b;
683
684 return NULL;
685 }
686
687 \f
688
689 /* Mark locations as "conditions have changed" in case the target supports
690 evaluating conditions on its side. */
691
692 static void
693 mark_breakpoint_modified (struct breakpoint *b)
694 {
695 struct bp_location *loc;
696
697 /* This is only meaningful if the target is
698 evaluating conditions and if the user has
699 opted for condition evaluation on the target's
700 side. */
701 if (gdb_evaluates_breakpoint_condition_p ()
702 || !target_supports_evaluation_of_breakpoint_conditions ())
703 return;
704
705 if (!is_breakpoint (b))
706 return;
707
708 for (loc = b->loc; loc; loc = loc->next)
709 loc->condition_changed = condition_modified;
710 }
711
712 /* Mark location as "conditions have changed" in case the target supports
713 evaluating conditions on its side. */
714
715 static void
716 mark_breakpoint_location_modified (struct bp_location *loc)
717 {
718 /* This is only meaningful if the target is
719 evaluating conditions and if the user has
720 opted for condition evaluation on the target's
721 side. */
722 if (gdb_evaluates_breakpoint_condition_p ()
723 || !target_supports_evaluation_of_breakpoint_conditions ())
724
725 return;
726
727 if (!is_breakpoint (loc->owner))
728 return;
729
730 loc->condition_changed = condition_modified;
731 }
732
733 /* Sets the condition-evaluation mode using the static global
734 condition_evaluation_mode. */
735
736 static void
737 set_condition_evaluation_mode (char *args, int from_tty,
738 struct cmd_list_element *c)
739 {
740 const char *old_mode, *new_mode;
741
742 if ((condition_evaluation_mode_1 == condition_evaluation_target)
743 && !target_supports_evaluation_of_breakpoint_conditions ())
744 {
745 condition_evaluation_mode_1 = condition_evaluation_mode;
746 warning (_("Target does not support breakpoint condition evaluation.\n"
747 "Using host evaluation mode instead."));
748 return;
749 }
750
751 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
752 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
753
754 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
755 settings was "auto". */
756 condition_evaluation_mode = condition_evaluation_mode_1;
757
758 /* Only update the mode if the user picked a different one. */
759 if (new_mode != old_mode)
760 {
761 struct bp_location *loc, **loc_tmp;
762 /* If the user switched to a different evaluation mode, we
763 need to synch the changes with the target as follows:
764
765 "host" -> "target": Send all (valid) conditions to the target.
766 "target" -> "host": Remove all the conditions from the target.
767 */
768
769 if (new_mode == condition_evaluation_target)
770 {
771 /* Mark everything modified and synch conditions with the
772 target. */
773 ALL_BP_LOCATIONS (loc, loc_tmp)
774 mark_breakpoint_location_modified (loc);
775 }
776 else
777 {
778 /* Manually mark non-duplicate locations to synch conditions
779 with the target. We do this to remove all the conditions the
780 target knows about. */
781 ALL_BP_LOCATIONS (loc, loc_tmp)
782 if (is_breakpoint (loc->owner) && loc->inserted)
783 loc->needs_update = 1;
784 }
785
786 /* Do the update. */
787 update_global_location_list (UGLL_MAY_INSERT);
788 }
789
790 return;
791 }
792
793 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
794 what "auto" is translating to. */
795
796 static void
797 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
798 struct cmd_list_element *c, const char *value)
799 {
800 if (condition_evaluation_mode == condition_evaluation_auto)
801 fprintf_filtered (file,
802 _("Breakpoint condition evaluation "
803 "mode is %s (currently %s).\n"),
804 value,
805 breakpoint_condition_evaluation_mode ());
806 else
807 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
808 value);
809 }
810
811 /* A comparison function for bp_location AP and BP that is used by
812 bsearch. This comparison function only cares about addresses, unlike
813 the more general bp_locations_compare function. */
814
815 static int
816 bp_locations_compare_addrs (const void *ap, const void *bp)
817 {
818 const struct bp_location *a = *(const struct bp_location **) ap;
819 const struct bp_location *b = *(const struct bp_location **) bp;
820
821 if (a->address == b->address)
822 return 0;
823 else
824 return ((a->address > b->address) - (a->address < b->address));
825 }
826
827 /* Helper function to skip all bp_locations with addresses
828 less than ADDRESS. It returns the first bp_location that
829 is greater than or equal to ADDRESS. If none is found, just
830 return NULL. */
831
832 static struct bp_location **
833 get_first_locp_gte_addr (CORE_ADDR address)
834 {
835 struct bp_location dummy_loc;
836 struct bp_location *dummy_locp = &dummy_loc;
837 struct bp_location **locp_found = NULL;
838
839 /* Initialize the dummy location's address field. */
840 dummy_loc.address = address;
841
842 /* Find a close match to the first location at ADDRESS. */
843 locp_found = ((struct bp_location **)
844 bsearch (&dummy_locp, bp_locations, bp_locations_count,
845 sizeof (struct bp_location **),
846 bp_locations_compare_addrs));
847
848 /* Nothing was found, nothing left to do. */
849 if (locp_found == NULL)
850 return NULL;
851
852 /* We may have found a location that is at ADDRESS but is not the first in the
853 location's list. Go backwards (if possible) and locate the first one. */
854 while ((locp_found - 1) >= bp_locations
855 && (*(locp_found - 1))->address == address)
856 locp_found--;
857
858 return locp_found;
859 }
860
861 void
862 set_breakpoint_condition (struct breakpoint *b, const char *exp,
863 int from_tty)
864 {
865 xfree (b->cond_string);
866 b->cond_string = NULL;
867
868 if (is_watchpoint (b))
869 {
870 struct watchpoint *w = (struct watchpoint *) b;
871
872 w->cond_exp.reset ();
873 }
874 else
875 {
876 struct bp_location *loc;
877
878 for (loc = b->loc; loc; loc = loc->next)
879 {
880 loc->cond.reset ();
881
882 /* No need to free the condition agent expression
883 bytecode (if we have one). We will handle this
884 when we go through update_global_location_list. */
885 }
886 }
887
888 if (*exp == 0)
889 {
890 if (from_tty)
891 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
892 }
893 else
894 {
895 const char *arg = exp;
896
897 /* I don't know if it matters whether this is the string the user
898 typed in or the decompiled expression. */
899 b->cond_string = xstrdup (arg);
900 b->condition_not_parsed = 0;
901
902 if (is_watchpoint (b))
903 {
904 struct watchpoint *w = (struct watchpoint *) b;
905
906 innermost_block = NULL;
907 arg = exp;
908 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
909 if (*arg)
910 error (_("Junk at end of expression"));
911 w->cond_exp_valid_block = innermost_block;
912 }
913 else
914 {
915 struct bp_location *loc;
916
917 for (loc = b->loc; loc; loc = loc->next)
918 {
919 arg = exp;
920 loc->cond =
921 parse_exp_1 (&arg, loc->address,
922 block_for_pc (loc->address), 0);
923 if (*arg)
924 error (_("Junk at end of expression"));
925 }
926 }
927 }
928 mark_breakpoint_modified (b);
929
930 observer_notify_breakpoint_modified (b);
931 }
932
933 /* Completion for the "condition" command. */
934
935 static void
936 condition_completer (struct cmd_list_element *cmd,
937 completion_tracker &tracker,
938 const char *text, const char *word)
939 {
940 const char *space;
941
942 text = skip_spaces (text);
943 space = skip_to_space (text);
944 if (*space == '\0')
945 {
946 int len;
947 struct breakpoint *b;
948 VEC (char_ptr) *result = NULL;
949
950 if (text[0] == '$')
951 {
952 /* We don't support completion of history indices. */
953 if (!isdigit (text[1]))
954 complete_internalvar (tracker, &text[1]);
955 return;
956 }
957
958 /* We're completing the breakpoint number. */
959 len = strlen (text);
960
961 ALL_BREAKPOINTS (b)
962 {
963 char number[50];
964
965 xsnprintf (number, sizeof (number), "%d", b->number);
966
967 if (strncmp (number, text, len) == 0)
968 {
969 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
970 tracker.add_completion (std::move (copy));
971 }
972 }
973
974 return;
975 }
976
977 /* We're completing the expression part. */
978 text = skip_spaces (space);
979 expression_completer (cmd, tracker, text, word);
980 }
981
982 /* condition N EXP -- set break condition of breakpoint N to EXP. */
983
984 static void
985 condition_command (char *arg, int from_tty)
986 {
987 struct breakpoint *b;
988 char *p;
989 int bnum;
990
991 if (arg == 0)
992 error_no_arg (_("breakpoint number"));
993
994 p = arg;
995 bnum = get_number (&p);
996 if (bnum == 0)
997 error (_("Bad breakpoint argument: '%s'"), arg);
998
999 ALL_BREAKPOINTS (b)
1000 if (b->number == bnum)
1001 {
1002 /* Check if this breakpoint has a "stop" method implemented in an
1003 extension language. This method and conditions entered into GDB
1004 from the CLI are mutually exclusive. */
1005 const struct extension_language_defn *extlang
1006 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1007
1008 if (extlang != NULL)
1009 {
1010 error (_("Only one stop condition allowed. There is currently"
1011 " a %s stop condition defined for this breakpoint."),
1012 ext_lang_capitalized_name (extlang));
1013 }
1014 set_breakpoint_condition (b, p, from_tty);
1015
1016 if (is_breakpoint (b))
1017 update_global_location_list (UGLL_MAY_INSERT);
1018
1019 return;
1020 }
1021
1022 error (_("No breakpoint number %d."), bnum);
1023 }
1024
1025 /* Check that COMMAND do not contain commands that are suitable
1026 only for tracepoints and not suitable for ordinary breakpoints.
1027 Throw if any such commands is found. */
1028
1029 static void
1030 check_no_tracepoint_commands (struct command_line *commands)
1031 {
1032 struct command_line *c;
1033
1034 for (c = commands; c; c = c->next)
1035 {
1036 int i;
1037
1038 if (c->control_type == while_stepping_control)
1039 error (_("The 'while-stepping' command can "
1040 "only be used for tracepoints"));
1041
1042 for (i = 0; i < c->body_count; ++i)
1043 check_no_tracepoint_commands ((c->body_list)[i]);
1044
1045 /* Not that command parsing removes leading whitespace and comment
1046 lines and also empty lines. So, we only need to check for
1047 command directly. */
1048 if (strstr (c->line, "collect ") == c->line)
1049 error (_("The 'collect' command can only be used for tracepoints"));
1050
1051 if (strstr (c->line, "teval ") == c->line)
1052 error (_("The 'teval' command can only be used for tracepoints"));
1053 }
1054 }
1055
1056 struct longjmp_breakpoint : public breakpoint
1057 {
1058 ~longjmp_breakpoint () override;
1059 };
1060
1061 /* Encapsulate tests for different types of tracepoints. */
1062
1063 static bool
1064 is_tracepoint_type (bptype type)
1065 {
1066 return (type == bp_tracepoint
1067 || type == bp_fast_tracepoint
1068 || type == bp_static_tracepoint);
1069 }
1070
1071 static bool
1072 is_longjmp_type (bptype type)
1073 {
1074 return type == bp_longjmp || type == bp_exception;
1075 }
1076
1077 int
1078 is_tracepoint (const struct breakpoint *b)
1079 {
1080 return is_tracepoint_type (b->type);
1081 }
1082
1083 /* Factory function to create an appropriate instance of breakpoint given
1084 TYPE. */
1085
1086 static std::unique_ptr<breakpoint>
1087 new_breakpoint_from_type (bptype type)
1088 {
1089 breakpoint *b;
1090
1091 if (is_tracepoint_type (type))
1092 b = new tracepoint ();
1093 else if (is_longjmp_type (type))
1094 b = new longjmp_breakpoint ();
1095 else
1096 b = new breakpoint ();
1097
1098 return std::unique_ptr<breakpoint> (b);
1099 }
1100
1101 /* A helper function that validates that COMMANDS are valid for a
1102 breakpoint. This function will throw an exception if a problem is
1103 found. */
1104
1105 static void
1106 validate_commands_for_breakpoint (struct breakpoint *b,
1107 struct command_line *commands)
1108 {
1109 if (is_tracepoint (b))
1110 {
1111 struct tracepoint *t = (struct tracepoint *) b;
1112 struct command_line *c;
1113 struct command_line *while_stepping = 0;
1114
1115 /* Reset the while-stepping step count. The previous commands
1116 might have included a while-stepping action, while the new
1117 ones might not. */
1118 t->step_count = 0;
1119
1120 /* We need to verify that each top-level element of commands is
1121 valid for tracepoints, that there's at most one
1122 while-stepping element, and that the while-stepping's body
1123 has valid tracing commands excluding nested while-stepping.
1124 We also need to validate the tracepoint action line in the
1125 context of the tracepoint --- validate_actionline actually
1126 has side effects, like setting the tracepoint's
1127 while-stepping STEP_COUNT, in addition to checking if the
1128 collect/teval actions parse and make sense in the
1129 tracepoint's context. */
1130 for (c = commands; c; c = c->next)
1131 {
1132 if (c->control_type == while_stepping_control)
1133 {
1134 if (b->type == bp_fast_tracepoint)
1135 error (_("The 'while-stepping' command "
1136 "cannot be used for fast tracepoint"));
1137 else if (b->type == bp_static_tracepoint)
1138 error (_("The 'while-stepping' command "
1139 "cannot be used for static tracepoint"));
1140
1141 if (while_stepping)
1142 error (_("The 'while-stepping' command "
1143 "can be used only once"));
1144 else
1145 while_stepping = c;
1146 }
1147
1148 validate_actionline (c->line, b);
1149 }
1150 if (while_stepping)
1151 {
1152 struct command_line *c2;
1153
1154 gdb_assert (while_stepping->body_count == 1);
1155 c2 = while_stepping->body_list[0];
1156 for (; c2; c2 = c2->next)
1157 {
1158 if (c2->control_type == while_stepping_control)
1159 error (_("The 'while-stepping' command cannot be nested"));
1160 }
1161 }
1162 }
1163 else
1164 {
1165 check_no_tracepoint_commands (commands);
1166 }
1167 }
1168
1169 /* Return a vector of all the static tracepoints set at ADDR. The
1170 caller is responsible for releasing the vector. */
1171
1172 VEC(breakpoint_p) *
1173 static_tracepoints_here (CORE_ADDR addr)
1174 {
1175 struct breakpoint *b;
1176 VEC(breakpoint_p) *found = 0;
1177 struct bp_location *loc;
1178
1179 ALL_BREAKPOINTS (b)
1180 if (b->type == bp_static_tracepoint)
1181 {
1182 for (loc = b->loc; loc; loc = loc->next)
1183 if (loc->address == addr)
1184 VEC_safe_push(breakpoint_p, found, b);
1185 }
1186
1187 return found;
1188 }
1189
1190 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1191 validate that only allowed commands are included. */
1192
1193 void
1194 breakpoint_set_commands (struct breakpoint *b,
1195 command_line_up &&commands)
1196 {
1197 validate_commands_for_breakpoint (b, commands.get ());
1198
1199 b->commands = std::move (commands);
1200 observer_notify_breakpoint_modified (b);
1201 }
1202
1203 /* Set the internal `silent' flag on the breakpoint. Note that this
1204 is not the same as the "silent" that may appear in the breakpoint's
1205 commands. */
1206
1207 void
1208 breakpoint_set_silent (struct breakpoint *b, int silent)
1209 {
1210 int old_silent = b->silent;
1211
1212 b->silent = silent;
1213 if (old_silent != silent)
1214 observer_notify_breakpoint_modified (b);
1215 }
1216
1217 /* Set the thread for this breakpoint. If THREAD is -1, make the
1218 breakpoint work for any thread. */
1219
1220 void
1221 breakpoint_set_thread (struct breakpoint *b, int thread)
1222 {
1223 int old_thread = b->thread;
1224
1225 b->thread = thread;
1226 if (old_thread != thread)
1227 observer_notify_breakpoint_modified (b);
1228 }
1229
1230 /* Set the task for this breakpoint. If TASK is 0, make the
1231 breakpoint work for any task. */
1232
1233 void
1234 breakpoint_set_task (struct breakpoint *b, int task)
1235 {
1236 int old_task = b->task;
1237
1238 b->task = task;
1239 if (old_task != task)
1240 observer_notify_breakpoint_modified (b);
1241 }
1242
1243 void
1244 check_tracepoint_command (char *line, void *closure)
1245 {
1246 struct breakpoint *b = (struct breakpoint *) closure;
1247
1248 validate_actionline (line, b);
1249 }
1250
1251 static void
1252 commands_command_1 (const char *arg, int from_tty,
1253 struct command_line *control)
1254 {
1255 counted_command_line cmd;
1256
1257 std::string new_arg;
1258
1259 if (arg == NULL || !*arg)
1260 {
1261 if (breakpoint_count - prev_breakpoint_count > 1)
1262 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1263 breakpoint_count);
1264 else if (breakpoint_count > 0)
1265 new_arg = string_printf ("%d", breakpoint_count);
1266 arg = new_arg.c_str ();
1267 }
1268
1269 map_breakpoint_numbers
1270 (arg, [&] (breakpoint *b)
1271 {
1272 if (cmd == NULL)
1273 {
1274 if (control != NULL)
1275 cmd = copy_command_lines (control->body_list[0]);
1276 else
1277 {
1278 std::string str
1279 = string_printf (_("Type commands for breakpoint(s) "
1280 "%s, one per line."),
1281 arg);
1282
1283 cmd = read_command_lines (&str[0],
1284 from_tty, 1,
1285 (is_tracepoint (b)
1286 ? check_tracepoint_command : 0),
1287 b);
1288 }
1289 }
1290
1291 /* If a breakpoint was on the list more than once, we don't need to
1292 do anything. */
1293 if (b->commands != cmd)
1294 {
1295 validate_commands_for_breakpoint (b, cmd.get ());
1296 b->commands = cmd;
1297 observer_notify_breakpoint_modified (b);
1298 }
1299 });
1300
1301 if (cmd == NULL)
1302 error (_("No breakpoints specified."));
1303 }
1304
1305 static void
1306 commands_command (char *arg, int from_tty)
1307 {
1308 commands_command_1 (arg, from_tty, NULL);
1309 }
1310
1311 /* Like commands_command, but instead of reading the commands from
1312 input stream, takes them from an already parsed command structure.
1313
1314 This is used by cli-script.c to DTRT with breakpoint commands
1315 that are part of if and while bodies. */
1316 enum command_control_type
1317 commands_from_control_command (const char *arg, struct command_line *cmd)
1318 {
1319 commands_command_1 (arg, 0, cmd);
1320 return simple_control;
1321 }
1322
1323 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1324
1325 static int
1326 bp_location_has_shadow (struct bp_location *bl)
1327 {
1328 if (bl->loc_type != bp_loc_software_breakpoint)
1329 return 0;
1330 if (!bl->inserted)
1331 return 0;
1332 if (bl->target_info.shadow_len == 0)
1333 /* BL isn't valid, or doesn't shadow memory. */
1334 return 0;
1335 return 1;
1336 }
1337
1338 /* Update BUF, which is LEN bytes read from the target address
1339 MEMADDR, by replacing a memory breakpoint with its shadowed
1340 contents.
1341
1342 If READBUF is not NULL, this buffer must not overlap with the of
1343 the breakpoint location's shadow_contents buffer. Otherwise, a
1344 failed assertion internal error will be raised. */
1345
1346 static void
1347 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1348 const gdb_byte *writebuf_org,
1349 ULONGEST memaddr, LONGEST len,
1350 struct bp_target_info *target_info,
1351 struct gdbarch *gdbarch)
1352 {
1353 /* Now do full processing of the found relevant range of elements. */
1354 CORE_ADDR bp_addr = 0;
1355 int bp_size = 0;
1356 int bptoffset = 0;
1357
1358 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1359 current_program_space->aspace, 0))
1360 {
1361 /* The breakpoint is inserted in a different address space. */
1362 return;
1363 }
1364
1365 /* Addresses and length of the part of the breakpoint that
1366 we need to copy. */
1367 bp_addr = target_info->placed_address;
1368 bp_size = target_info->shadow_len;
1369
1370 if (bp_addr + bp_size <= memaddr)
1371 {
1372 /* The breakpoint is entirely before the chunk of memory we are
1373 reading. */
1374 return;
1375 }
1376
1377 if (bp_addr >= memaddr + len)
1378 {
1379 /* The breakpoint is entirely after the chunk of memory we are
1380 reading. */
1381 return;
1382 }
1383
1384 /* Offset within shadow_contents. */
1385 if (bp_addr < memaddr)
1386 {
1387 /* Only copy the second part of the breakpoint. */
1388 bp_size -= memaddr - bp_addr;
1389 bptoffset = memaddr - bp_addr;
1390 bp_addr = memaddr;
1391 }
1392
1393 if (bp_addr + bp_size > memaddr + len)
1394 {
1395 /* Only copy the first part of the breakpoint. */
1396 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1397 }
1398
1399 if (readbuf != NULL)
1400 {
1401 /* Verify that the readbuf buffer does not overlap with the
1402 shadow_contents buffer. */
1403 gdb_assert (target_info->shadow_contents >= readbuf + len
1404 || readbuf >= (target_info->shadow_contents
1405 + target_info->shadow_len));
1406
1407 /* Update the read buffer with this inserted breakpoint's
1408 shadow. */
1409 memcpy (readbuf + bp_addr - memaddr,
1410 target_info->shadow_contents + bptoffset, bp_size);
1411 }
1412 else
1413 {
1414 const unsigned char *bp;
1415 CORE_ADDR addr = target_info->reqstd_address;
1416 int placed_size;
1417
1418 /* Update the shadow with what we want to write to memory. */
1419 memcpy (target_info->shadow_contents + bptoffset,
1420 writebuf_org + bp_addr - memaddr, bp_size);
1421
1422 /* Determine appropriate breakpoint contents and size for this
1423 address. */
1424 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1425
1426 /* Update the final write buffer with this inserted
1427 breakpoint's INSN. */
1428 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1429 }
1430 }
1431
1432 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1433 by replacing any memory breakpoints with their shadowed contents.
1434
1435 If READBUF is not NULL, this buffer must not overlap with any of
1436 the breakpoint location's shadow_contents buffers. Otherwise,
1437 a failed assertion internal error will be raised.
1438
1439 The range of shadowed area by each bp_location is:
1440 bl->address - bp_locations_placed_address_before_address_max
1441 up to bl->address + bp_locations_shadow_len_after_address_max
1442 The range we were requested to resolve shadows for is:
1443 memaddr ... memaddr + len
1444 Thus the safe cutoff boundaries for performance optimization are
1445 memaddr + len <= (bl->address
1446 - bp_locations_placed_address_before_address_max)
1447 and:
1448 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1449
1450 void
1451 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1452 const gdb_byte *writebuf_org,
1453 ULONGEST memaddr, LONGEST len)
1454 {
1455 /* Left boundary, right boundary and median element of our binary
1456 search. */
1457 unsigned bc_l, bc_r, bc;
1458
1459 /* Find BC_L which is a leftmost element which may affect BUF
1460 content. It is safe to report lower value but a failure to
1461 report higher one. */
1462
1463 bc_l = 0;
1464 bc_r = bp_locations_count;
1465 while (bc_l + 1 < bc_r)
1466 {
1467 struct bp_location *bl;
1468
1469 bc = (bc_l + bc_r) / 2;
1470 bl = bp_locations[bc];
1471
1472 /* Check first BL->ADDRESS will not overflow due to the added
1473 constant. Then advance the left boundary only if we are sure
1474 the BC element can in no way affect the BUF content (MEMADDR
1475 to MEMADDR + LEN range).
1476
1477 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1478 offset so that we cannot miss a breakpoint with its shadow
1479 range tail still reaching MEMADDR. */
1480
1481 if ((bl->address + bp_locations_shadow_len_after_address_max
1482 >= bl->address)
1483 && (bl->address + bp_locations_shadow_len_after_address_max
1484 <= memaddr))
1485 bc_l = bc;
1486 else
1487 bc_r = bc;
1488 }
1489
1490 /* Due to the binary search above, we need to make sure we pick the
1491 first location that's at BC_L's address. E.g., if there are
1492 multiple locations at the same address, BC_L may end up pointing
1493 at a duplicate location, and miss the "master"/"inserted"
1494 location. Say, given locations L1, L2 and L3 at addresses A and
1495 B:
1496
1497 L1@A, L2@A, L3@B, ...
1498
1499 BC_L could end up pointing at location L2, while the "master"
1500 location could be L1. Since the `loc->inserted' flag is only set
1501 on "master" locations, we'd forget to restore the shadow of L1
1502 and L2. */
1503 while (bc_l > 0
1504 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1505 bc_l--;
1506
1507 /* Now do full processing of the found relevant range of elements. */
1508
1509 for (bc = bc_l; bc < bp_locations_count; bc++)
1510 {
1511 struct bp_location *bl = bp_locations[bc];
1512
1513 /* bp_location array has BL->OWNER always non-NULL. */
1514 if (bl->owner->type == bp_none)
1515 warning (_("reading through apparently deleted breakpoint #%d?"),
1516 bl->owner->number);
1517
1518 /* Performance optimization: any further element can no longer affect BUF
1519 content. */
1520
1521 if (bl->address >= bp_locations_placed_address_before_address_max
1522 && memaddr + len <= (bl->address
1523 - bp_locations_placed_address_before_address_max))
1524 break;
1525
1526 if (!bp_location_has_shadow (bl))
1527 continue;
1528
1529 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1530 memaddr, len, &bl->target_info, bl->gdbarch);
1531 }
1532 }
1533
1534 \f
1535
1536 /* Return true if BPT is either a software breakpoint or a hardware
1537 breakpoint. */
1538
1539 int
1540 is_breakpoint (const struct breakpoint *bpt)
1541 {
1542 return (bpt->type == bp_breakpoint
1543 || bpt->type == bp_hardware_breakpoint
1544 || bpt->type == bp_dprintf);
1545 }
1546
1547 /* Return true if BPT is of any hardware watchpoint kind. */
1548
1549 static int
1550 is_hardware_watchpoint (const struct breakpoint *bpt)
1551 {
1552 return (bpt->type == bp_hardware_watchpoint
1553 || bpt->type == bp_read_watchpoint
1554 || bpt->type == bp_access_watchpoint);
1555 }
1556
1557 /* Return true if BPT is of any watchpoint kind, hardware or
1558 software. */
1559
1560 int
1561 is_watchpoint (const struct breakpoint *bpt)
1562 {
1563 return (is_hardware_watchpoint (bpt)
1564 || bpt->type == bp_watchpoint);
1565 }
1566
1567 /* Returns true if the current thread and its running state are safe
1568 to evaluate or update watchpoint B. Watchpoints on local
1569 expressions need to be evaluated in the context of the thread that
1570 was current when the watchpoint was created, and, that thread needs
1571 to be stopped to be able to select the correct frame context.
1572 Watchpoints on global expressions can be evaluated on any thread,
1573 and in any state. It is presently left to the target allowing
1574 memory accesses when threads are running. */
1575
1576 static int
1577 watchpoint_in_thread_scope (struct watchpoint *b)
1578 {
1579 return (b->pspace == current_program_space
1580 && (ptid_equal (b->watchpoint_thread, null_ptid)
1581 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1582 && !is_executing (inferior_ptid))));
1583 }
1584
1585 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1586 associated bp_watchpoint_scope breakpoint. */
1587
1588 static void
1589 watchpoint_del_at_next_stop (struct watchpoint *w)
1590 {
1591 if (w->related_breakpoint != w)
1592 {
1593 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1594 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1595 w->related_breakpoint->disposition = disp_del_at_next_stop;
1596 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1597 w->related_breakpoint = w;
1598 }
1599 w->disposition = disp_del_at_next_stop;
1600 }
1601
1602 /* Extract a bitfield value from value VAL using the bit parameters contained in
1603 watchpoint W. */
1604
1605 static struct value *
1606 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1607 {
1608 struct value *bit_val;
1609
1610 if (val == NULL)
1611 return NULL;
1612
1613 bit_val = allocate_value (value_type (val));
1614
1615 unpack_value_bitfield (bit_val,
1616 w->val_bitpos,
1617 w->val_bitsize,
1618 value_contents_for_printing (val),
1619 value_offset (val),
1620 val);
1621
1622 return bit_val;
1623 }
1624
1625 /* Allocate a dummy location and add it to B, which must be a software
1626 watchpoint. This is required because even if a software watchpoint
1627 is not watching any memory, bpstat_stop_status requires a location
1628 to be able to report stops. */
1629
1630 static void
1631 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1632 struct program_space *pspace)
1633 {
1634 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1635
1636 b->loc = allocate_bp_location (b);
1637 b->loc->pspace = pspace;
1638 b->loc->address = -1;
1639 b->loc->length = -1;
1640 }
1641
1642 /* Returns true if B is a software watchpoint that is not watching any
1643 memory (e.g., "watch $pc"). */
1644
1645 static int
1646 is_no_memory_software_watchpoint (struct breakpoint *b)
1647 {
1648 return (b->type == bp_watchpoint
1649 && b->loc != NULL
1650 && b->loc->next == NULL
1651 && b->loc->address == -1
1652 && b->loc->length == -1);
1653 }
1654
1655 /* Assuming that B is a watchpoint:
1656 - Reparse watchpoint expression, if REPARSE is non-zero
1657 - Evaluate expression and store the result in B->val
1658 - Evaluate the condition if there is one, and store the result
1659 in b->loc->cond.
1660 - Update the list of values that must be watched in B->loc.
1661
1662 If the watchpoint disposition is disp_del_at_next_stop, then do
1663 nothing. If this is local watchpoint that is out of scope, delete
1664 it.
1665
1666 Even with `set breakpoint always-inserted on' the watchpoints are
1667 removed + inserted on each stop here. Normal breakpoints must
1668 never be removed because they might be missed by a running thread
1669 when debugging in non-stop mode. On the other hand, hardware
1670 watchpoints (is_hardware_watchpoint; processed here) are specific
1671 to each LWP since they are stored in each LWP's hardware debug
1672 registers. Therefore, such LWP must be stopped first in order to
1673 be able to modify its hardware watchpoints.
1674
1675 Hardware watchpoints must be reset exactly once after being
1676 presented to the user. It cannot be done sooner, because it would
1677 reset the data used to present the watchpoint hit to the user. And
1678 it must not be done later because it could display the same single
1679 watchpoint hit during multiple GDB stops. Note that the latter is
1680 relevant only to the hardware watchpoint types bp_read_watchpoint
1681 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1682 not user-visible - its hit is suppressed if the memory content has
1683 not changed.
1684
1685 The following constraints influence the location where we can reset
1686 hardware watchpoints:
1687
1688 * target_stopped_by_watchpoint and target_stopped_data_address are
1689 called several times when GDB stops.
1690
1691 [linux]
1692 * Multiple hardware watchpoints can be hit at the same time,
1693 causing GDB to stop. GDB only presents one hardware watchpoint
1694 hit at a time as the reason for stopping, and all the other hits
1695 are presented later, one after the other, each time the user
1696 requests the execution to be resumed. Execution is not resumed
1697 for the threads still having pending hit event stored in
1698 LWP_INFO->STATUS. While the watchpoint is already removed from
1699 the inferior on the first stop the thread hit event is kept being
1700 reported from its cached value by linux_nat_stopped_data_address
1701 until the real thread resume happens after the watchpoint gets
1702 presented and thus its LWP_INFO->STATUS gets reset.
1703
1704 Therefore the hardware watchpoint hit can get safely reset on the
1705 watchpoint removal from inferior. */
1706
1707 static void
1708 update_watchpoint (struct watchpoint *b, int reparse)
1709 {
1710 int within_current_scope;
1711 struct frame_id saved_frame_id;
1712 int frame_saved;
1713
1714 /* If this is a local watchpoint, we only want to check if the
1715 watchpoint frame is in scope if the current thread is the thread
1716 that was used to create the watchpoint. */
1717 if (!watchpoint_in_thread_scope (b))
1718 return;
1719
1720 if (b->disposition == disp_del_at_next_stop)
1721 return;
1722
1723 frame_saved = 0;
1724
1725 /* Determine if the watchpoint is within scope. */
1726 if (b->exp_valid_block == NULL)
1727 within_current_scope = 1;
1728 else
1729 {
1730 struct frame_info *fi = get_current_frame ();
1731 struct gdbarch *frame_arch = get_frame_arch (fi);
1732 CORE_ADDR frame_pc = get_frame_pc (fi);
1733
1734 /* If we're at a point where the stack has been destroyed
1735 (e.g. in a function epilogue), unwinding may not work
1736 properly. Do not attempt to recreate locations at this
1737 point. See similar comments in watchpoint_check. */
1738 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1739 return;
1740
1741 /* Save the current frame's ID so we can restore it after
1742 evaluating the watchpoint expression on its own frame. */
1743 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1744 took a frame parameter, so that we didn't have to change the
1745 selected frame. */
1746 frame_saved = 1;
1747 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1748
1749 fi = frame_find_by_id (b->watchpoint_frame);
1750 within_current_scope = (fi != NULL);
1751 if (within_current_scope)
1752 select_frame (fi);
1753 }
1754
1755 /* We don't free locations. They are stored in the bp_location array
1756 and update_global_location_list will eventually delete them and
1757 remove breakpoints if needed. */
1758 b->loc = NULL;
1759
1760 if (within_current_scope && reparse)
1761 {
1762 const char *s;
1763
1764 b->exp.reset ();
1765 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1766 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1767 /* If the meaning of expression itself changed, the old value is
1768 no longer relevant. We don't want to report a watchpoint hit
1769 to the user when the old value and the new value may actually
1770 be completely different objects. */
1771 value_free (b->val);
1772 b->val = NULL;
1773 b->val_valid = 0;
1774
1775 /* Note that unlike with breakpoints, the watchpoint's condition
1776 expression is stored in the breakpoint object, not in the
1777 locations (re)created below. */
1778 if (b->cond_string != NULL)
1779 {
1780 b->cond_exp.reset ();
1781
1782 s = b->cond_string;
1783 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1784 }
1785 }
1786
1787 /* If we failed to parse the expression, for example because
1788 it refers to a global variable in a not-yet-loaded shared library,
1789 don't try to insert watchpoint. We don't automatically delete
1790 such watchpoint, though, since failure to parse expression
1791 is different from out-of-scope watchpoint. */
1792 if (!target_has_execution)
1793 {
1794 /* Without execution, memory can't change. No use to try and
1795 set watchpoint locations. The watchpoint will be reset when
1796 the target gains execution, through breakpoint_re_set. */
1797 if (!can_use_hw_watchpoints)
1798 {
1799 if (b->ops->works_in_software_mode (b))
1800 b->type = bp_watchpoint;
1801 else
1802 error (_("Can't set read/access watchpoint when "
1803 "hardware watchpoints are disabled."));
1804 }
1805 }
1806 else if (within_current_scope && b->exp)
1807 {
1808 int pc = 0;
1809 struct value *val_chain, *v, *result, *next;
1810 struct program_space *frame_pspace;
1811
1812 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1813
1814 /* Avoid setting b->val if it's already set. The meaning of
1815 b->val is 'the last value' user saw, and we should update
1816 it only if we reported that last value to user. As it
1817 happens, the code that reports it updates b->val directly.
1818 We don't keep track of the memory value for masked
1819 watchpoints. */
1820 if (!b->val_valid && !is_masked_watchpoint (b))
1821 {
1822 if (b->val_bitsize != 0)
1823 {
1824 v = extract_bitfield_from_watchpoint_value (b, v);
1825 if (v != NULL)
1826 release_value (v);
1827 }
1828 b->val = v;
1829 b->val_valid = 1;
1830 }
1831
1832 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1833
1834 /* Look at each value on the value chain. */
1835 for (v = val_chain; v; v = value_next (v))
1836 {
1837 /* If it's a memory location, and GDB actually needed
1838 its contents to evaluate the expression, then we
1839 must watch it. If the first value returned is
1840 still lazy, that means an error occurred reading it;
1841 watch it anyway in case it becomes readable. */
1842 if (VALUE_LVAL (v) == lval_memory
1843 && (v == val_chain || ! value_lazy (v)))
1844 {
1845 struct type *vtype = check_typedef (value_type (v));
1846
1847 /* We only watch structs and arrays if user asked
1848 for it explicitly, never if they just happen to
1849 appear in the middle of some value chain. */
1850 if (v == result
1851 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1852 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1853 {
1854 CORE_ADDR addr;
1855 enum target_hw_bp_type type;
1856 struct bp_location *loc, **tmp;
1857 int bitpos = 0, bitsize = 0;
1858
1859 if (value_bitsize (v) != 0)
1860 {
1861 /* Extract the bit parameters out from the bitfield
1862 sub-expression. */
1863 bitpos = value_bitpos (v);
1864 bitsize = value_bitsize (v);
1865 }
1866 else if (v == result && b->val_bitsize != 0)
1867 {
1868 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1869 lvalue whose bit parameters are saved in the fields
1870 VAL_BITPOS and VAL_BITSIZE. */
1871 bitpos = b->val_bitpos;
1872 bitsize = b->val_bitsize;
1873 }
1874
1875 addr = value_address (v);
1876 if (bitsize != 0)
1877 {
1878 /* Skip the bytes that don't contain the bitfield. */
1879 addr += bitpos / 8;
1880 }
1881
1882 type = hw_write;
1883 if (b->type == bp_read_watchpoint)
1884 type = hw_read;
1885 else if (b->type == bp_access_watchpoint)
1886 type = hw_access;
1887
1888 loc = allocate_bp_location (b);
1889 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1890 ;
1891 *tmp = loc;
1892 loc->gdbarch = get_type_arch (value_type (v));
1893
1894 loc->pspace = frame_pspace;
1895 loc->address = addr;
1896
1897 if (bitsize != 0)
1898 {
1899 /* Just cover the bytes that make up the bitfield. */
1900 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1901 }
1902 else
1903 loc->length = TYPE_LENGTH (value_type (v));
1904
1905 loc->watchpoint_type = type;
1906 }
1907 }
1908 }
1909
1910 /* Change the type of breakpoint between hardware assisted or
1911 an ordinary watchpoint depending on the hardware support
1912 and free hardware slots. REPARSE is set when the inferior
1913 is started. */
1914 if (reparse)
1915 {
1916 int reg_cnt;
1917 enum bp_loc_type loc_type;
1918 struct bp_location *bl;
1919
1920 reg_cnt = can_use_hardware_watchpoint (val_chain);
1921
1922 if (reg_cnt)
1923 {
1924 int i, target_resources_ok, other_type_used;
1925 enum bptype type;
1926
1927 /* Use an exact watchpoint when there's only one memory region to be
1928 watched, and only one debug register is needed to watch it. */
1929 b->exact = target_exact_watchpoints && reg_cnt == 1;
1930
1931 /* We need to determine how many resources are already
1932 used for all other hardware watchpoints plus this one
1933 to see if we still have enough resources to also fit
1934 this watchpoint in as well. */
1935
1936 /* If this is a software watchpoint, we try to turn it
1937 to a hardware one -- count resources as if B was of
1938 hardware watchpoint type. */
1939 type = b->type;
1940 if (type == bp_watchpoint)
1941 type = bp_hardware_watchpoint;
1942
1943 /* This watchpoint may or may not have been placed on
1944 the list yet at this point (it won't be in the list
1945 if we're trying to create it for the first time,
1946 through watch_command), so always account for it
1947 manually. */
1948
1949 /* Count resources used by all watchpoints except B. */
1950 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1951
1952 /* Add in the resources needed for B. */
1953 i += hw_watchpoint_use_count (b);
1954
1955 target_resources_ok
1956 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1957 if (target_resources_ok <= 0)
1958 {
1959 int sw_mode = b->ops->works_in_software_mode (b);
1960
1961 if (target_resources_ok == 0 && !sw_mode)
1962 error (_("Target does not support this type of "
1963 "hardware watchpoint."));
1964 else if (target_resources_ok < 0 && !sw_mode)
1965 error (_("There are not enough available hardware "
1966 "resources for this watchpoint."));
1967
1968 /* Downgrade to software watchpoint. */
1969 b->type = bp_watchpoint;
1970 }
1971 else
1972 {
1973 /* If this was a software watchpoint, we've just
1974 found we have enough resources to turn it to a
1975 hardware watchpoint. Otherwise, this is a
1976 nop. */
1977 b->type = type;
1978 }
1979 }
1980 else if (!b->ops->works_in_software_mode (b))
1981 {
1982 if (!can_use_hw_watchpoints)
1983 error (_("Can't set read/access watchpoint when "
1984 "hardware watchpoints are disabled."));
1985 else
1986 error (_("Expression cannot be implemented with "
1987 "read/access watchpoint."));
1988 }
1989 else
1990 b->type = bp_watchpoint;
1991
1992 loc_type = (b->type == bp_watchpoint? bp_loc_other
1993 : bp_loc_hardware_watchpoint);
1994 for (bl = b->loc; bl; bl = bl->next)
1995 bl->loc_type = loc_type;
1996 }
1997
1998 for (v = val_chain; v; v = next)
1999 {
2000 next = value_next (v);
2001 if (v != b->val)
2002 value_free (v);
2003 }
2004
2005 /* If a software watchpoint is not watching any memory, then the
2006 above left it without any location set up. But,
2007 bpstat_stop_status requires a location to be able to report
2008 stops, so make sure there's at least a dummy one. */
2009 if (b->type == bp_watchpoint && b->loc == NULL)
2010 software_watchpoint_add_no_memory_location (b, frame_pspace);
2011 }
2012 else if (!within_current_scope)
2013 {
2014 printf_filtered (_("\
2015 Watchpoint %d deleted because the program has left the block\n\
2016 in which its expression is valid.\n"),
2017 b->number);
2018 watchpoint_del_at_next_stop (b);
2019 }
2020
2021 /* Restore the selected frame. */
2022 if (frame_saved)
2023 select_frame (frame_find_by_id (saved_frame_id));
2024 }
2025
2026
2027 /* Returns 1 iff breakpoint location should be
2028 inserted in the inferior. We don't differentiate the type of BL's owner
2029 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2030 breakpoint_ops is not defined, because in insert_bp_location,
2031 tracepoint's insert_location will not be called. */
2032 static int
2033 should_be_inserted (struct bp_location *bl)
2034 {
2035 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2036 return 0;
2037
2038 if (bl->owner->disposition == disp_del_at_next_stop)
2039 return 0;
2040
2041 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2042 return 0;
2043
2044 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2045 return 0;
2046
2047 /* This is set for example, when we're attached to the parent of a
2048 vfork, and have detached from the child. The child is running
2049 free, and we expect it to do an exec or exit, at which point the
2050 OS makes the parent schedulable again (and the target reports
2051 that the vfork is done). Until the child is done with the shared
2052 memory region, do not insert breakpoints in the parent, otherwise
2053 the child could still trip on the parent's breakpoints. Since
2054 the parent is blocked anyway, it won't miss any breakpoint. */
2055 if (bl->pspace->breakpoints_not_allowed)
2056 return 0;
2057
2058 /* Don't insert a breakpoint if we're trying to step past its
2059 location, except if the breakpoint is a single-step breakpoint,
2060 and the breakpoint's thread is the thread which is stepping past
2061 a breakpoint. */
2062 if ((bl->loc_type == bp_loc_software_breakpoint
2063 || bl->loc_type == bp_loc_hardware_breakpoint)
2064 && stepping_past_instruction_at (bl->pspace->aspace,
2065 bl->address)
2066 /* The single-step breakpoint may be inserted at the location
2067 we're trying to step if the instruction branches to itself.
2068 However, the instruction won't be executed at all and it may
2069 break the semantics of the instruction, for example, the
2070 instruction is a conditional branch or updates some flags.
2071 We can't fix it unless GDB is able to emulate the instruction
2072 or switch to displaced stepping. */
2073 && !(bl->owner->type == bp_single_step
2074 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2075 {
2076 if (debug_infrun)
2077 {
2078 fprintf_unfiltered (gdb_stdlog,
2079 "infrun: skipping breakpoint: "
2080 "stepping past insn at: %s\n",
2081 paddress (bl->gdbarch, bl->address));
2082 }
2083 return 0;
2084 }
2085
2086 /* Don't insert watchpoints if we're trying to step past the
2087 instruction that triggered one. */
2088 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2089 && stepping_past_nonsteppable_watchpoint ())
2090 {
2091 if (debug_infrun)
2092 {
2093 fprintf_unfiltered (gdb_stdlog,
2094 "infrun: stepping past non-steppable watchpoint. "
2095 "skipping watchpoint at %s:%d\n",
2096 paddress (bl->gdbarch, bl->address),
2097 bl->length);
2098 }
2099 return 0;
2100 }
2101
2102 return 1;
2103 }
2104
2105 /* Same as should_be_inserted but does the check assuming
2106 that the location is not duplicated. */
2107
2108 static int
2109 unduplicated_should_be_inserted (struct bp_location *bl)
2110 {
2111 int result;
2112 const int save_duplicate = bl->duplicate;
2113
2114 bl->duplicate = 0;
2115 result = should_be_inserted (bl);
2116 bl->duplicate = save_duplicate;
2117 return result;
2118 }
2119
2120 /* Parses a conditional described by an expression COND into an
2121 agent expression bytecode suitable for evaluation
2122 by the bytecode interpreter. Return NULL if there was
2123 any error during parsing. */
2124
2125 static agent_expr_up
2126 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2127 {
2128 if (cond == NULL)
2129 return NULL;
2130
2131 agent_expr_up aexpr;
2132
2133 /* We don't want to stop processing, so catch any errors
2134 that may show up. */
2135 TRY
2136 {
2137 aexpr = gen_eval_for_expr (scope, cond);
2138 }
2139
2140 CATCH (ex, RETURN_MASK_ERROR)
2141 {
2142 /* If we got here, it means the condition could not be parsed to a valid
2143 bytecode expression and thus can't be evaluated on the target's side.
2144 It's no use iterating through the conditions. */
2145 }
2146 END_CATCH
2147
2148 /* We have a valid agent expression. */
2149 return aexpr;
2150 }
2151
2152 /* Based on location BL, create a list of breakpoint conditions to be
2153 passed on to the target. If we have duplicated locations with different
2154 conditions, we will add such conditions to the list. The idea is that the
2155 target will evaluate the list of conditions and will only notify GDB when
2156 one of them is true. */
2157
2158 static void
2159 build_target_condition_list (struct bp_location *bl)
2160 {
2161 struct bp_location **locp = NULL, **loc2p;
2162 int null_condition_or_parse_error = 0;
2163 int modified = bl->needs_update;
2164 struct bp_location *loc;
2165
2166 /* Release conditions left over from a previous insert. */
2167 bl->target_info.conditions.clear ();
2168
2169 /* This is only meaningful if the target is
2170 evaluating conditions and if the user has
2171 opted for condition evaluation on the target's
2172 side. */
2173 if (gdb_evaluates_breakpoint_condition_p ()
2174 || !target_supports_evaluation_of_breakpoint_conditions ())
2175 return;
2176
2177 /* Do a first pass to check for locations with no assigned
2178 conditions or conditions that fail to parse to a valid agent expression
2179 bytecode. If any of these happen, then it's no use to send conditions
2180 to the target since this location will always trigger and generate a
2181 response back to GDB. */
2182 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2183 {
2184 loc = (*loc2p);
2185 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2186 {
2187 if (modified)
2188 {
2189 /* Re-parse the conditions since something changed. In that
2190 case we already freed the condition bytecodes (see
2191 force_breakpoint_reinsertion). We just
2192 need to parse the condition to bytecodes again. */
2193 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2194 loc->cond.get ());
2195 }
2196
2197 /* If we have a NULL bytecode expression, it means something
2198 went wrong or we have a null condition expression. */
2199 if (!loc->cond_bytecode)
2200 {
2201 null_condition_or_parse_error = 1;
2202 break;
2203 }
2204 }
2205 }
2206
2207 /* If any of these happened, it means we will have to evaluate the conditions
2208 for the location's address on gdb's side. It is no use keeping bytecodes
2209 for all the other duplicate locations, thus we free all of them here.
2210
2211 This is so we have a finer control over which locations' conditions are
2212 being evaluated by GDB or the remote stub. */
2213 if (null_condition_or_parse_error)
2214 {
2215 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2216 {
2217 loc = (*loc2p);
2218 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2219 {
2220 /* Only go as far as the first NULL bytecode is
2221 located. */
2222 if (!loc->cond_bytecode)
2223 return;
2224
2225 loc->cond_bytecode.reset ();
2226 }
2227 }
2228 }
2229
2230 /* No NULL conditions or failed bytecode generation. Build a condition list
2231 for this location's address. */
2232 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2233 {
2234 loc = (*loc2p);
2235 if (loc->cond
2236 && is_breakpoint (loc->owner)
2237 && loc->pspace->num == bl->pspace->num
2238 && loc->owner->enable_state == bp_enabled
2239 && loc->enabled)
2240 {
2241 /* Add the condition to the vector. This will be used later
2242 to send the conditions to the target. */
2243 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2244 }
2245 }
2246
2247 return;
2248 }
2249
2250 /* Parses a command described by string CMD into an agent expression
2251 bytecode suitable for evaluation by the bytecode interpreter.
2252 Return NULL if there was any error during parsing. */
2253
2254 static agent_expr_up
2255 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2256 {
2257 struct cleanup *old_cleanups = 0;
2258 struct expression **argvec;
2259 const char *cmdrest;
2260 const char *format_start, *format_end;
2261 struct format_piece *fpieces;
2262 int nargs;
2263 struct gdbarch *gdbarch = get_current_arch ();
2264
2265 if (cmd == NULL)
2266 return NULL;
2267
2268 cmdrest = cmd;
2269
2270 if (*cmdrest == ',')
2271 ++cmdrest;
2272 cmdrest = skip_spaces (cmdrest);
2273
2274 if (*cmdrest++ != '"')
2275 error (_("No format string following the location"));
2276
2277 format_start = cmdrest;
2278
2279 fpieces = parse_format_string (&cmdrest);
2280
2281 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2282
2283 format_end = cmdrest;
2284
2285 if (*cmdrest++ != '"')
2286 error (_("Bad format string, non-terminated '\"'."));
2287
2288 cmdrest = skip_spaces (cmdrest);
2289
2290 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2291 error (_("Invalid argument syntax"));
2292
2293 if (*cmdrest == ',')
2294 cmdrest++;
2295 cmdrest = skip_spaces (cmdrest);
2296
2297 /* For each argument, make an expression. */
2298
2299 argvec = (struct expression **) alloca (strlen (cmd)
2300 * sizeof (struct expression *));
2301
2302 nargs = 0;
2303 while (*cmdrest != '\0')
2304 {
2305 const char *cmd1;
2306
2307 cmd1 = cmdrest;
2308 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2309 argvec[nargs++] = expr.release ();
2310 cmdrest = cmd1;
2311 if (*cmdrest == ',')
2312 ++cmdrest;
2313 }
2314
2315 agent_expr_up aexpr;
2316
2317 /* We don't want to stop processing, so catch any errors
2318 that may show up. */
2319 TRY
2320 {
2321 aexpr = gen_printf (scope, gdbarch, 0, 0,
2322 format_start, format_end - format_start,
2323 fpieces, nargs, argvec);
2324 }
2325 CATCH (ex, RETURN_MASK_ERROR)
2326 {
2327 /* If we got here, it means the command could not be parsed to a valid
2328 bytecode expression and thus can't be evaluated on the target's side.
2329 It's no use iterating through the other commands. */
2330 }
2331 END_CATCH
2332
2333 do_cleanups (old_cleanups);
2334
2335 /* We have a valid agent expression, return it. */
2336 return aexpr;
2337 }
2338
2339 /* Based on location BL, create a list of breakpoint commands to be
2340 passed on to the target. If we have duplicated locations with
2341 different commands, we will add any such to the list. */
2342
2343 static void
2344 build_target_command_list (struct bp_location *bl)
2345 {
2346 struct bp_location **locp = NULL, **loc2p;
2347 int null_command_or_parse_error = 0;
2348 int modified = bl->needs_update;
2349 struct bp_location *loc;
2350
2351 /* Clear commands left over from a previous insert. */
2352 bl->target_info.tcommands.clear ();
2353
2354 if (!target_can_run_breakpoint_commands ())
2355 return;
2356
2357 /* For now, limit to agent-style dprintf breakpoints. */
2358 if (dprintf_style != dprintf_style_agent)
2359 return;
2360
2361 /* For now, if we have any duplicate location that isn't a dprintf,
2362 don't install the target-side commands, as that would make the
2363 breakpoint not be reported to the core, and we'd lose
2364 control. */
2365 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2366 {
2367 loc = (*loc2p);
2368 if (is_breakpoint (loc->owner)
2369 && loc->pspace->num == bl->pspace->num
2370 && loc->owner->type != bp_dprintf)
2371 return;
2372 }
2373
2374 /* Do a first pass to check for locations with no assigned
2375 conditions or conditions that fail to parse to a valid agent expression
2376 bytecode. If any of these happen, then it's no use to send conditions
2377 to the target since this location will always trigger and generate a
2378 response back to GDB. */
2379 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2380 {
2381 loc = (*loc2p);
2382 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2383 {
2384 if (modified)
2385 {
2386 /* Re-parse the commands since something changed. In that
2387 case we already freed the command bytecodes (see
2388 force_breakpoint_reinsertion). We just
2389 need to parse the command to bytecodes again. */
2390 loc->cmd_bytecode
2391 = parse_cmd_to_aexpr (bl->address,
2392 loc->owner->extra_string);
2393 }
2394
2395 /* If we have a NULL bytecode expression, it means something
2396 went wrong or we have a null command expression. */
2397 if (!loc->cmd_bytecode)
2398 {
2399 null_command_or_parse_error = 1;
2400 break;
2401 }
2402 }
2403 }
2404
2405 /* If anything failed, then we're not doing target-side commands,
2406 and so clean up. */
2407 if (null_command_or_parse_error)
2408 {
2409 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2410 {
2411 loc = (*loc2p);
2412 if (is_breakpoint (loc->owner)
2413 && loc->pspace->num == bl->pspace->num)
2414 {
2415 /* Only go as far as the first NULL bytecode is
2416 located. */
2417 if (loc->cmd_bytecode == NULL)
2418 return;
2419
2420 loc->cmd_bytecode.reset ();
2421 }
2422 }
2423 }
2424
2425 /* No NULL commands or failed bytecode generation. Build a command list
2426 for this location's address. */
2427 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2428 {
2429 loc = (*loc2p);
2430 if (loc->owner->extra_string
2431 && is_breakpoint (loc->owner)
2432 && loc->pspace->num == bl->pspace->num
2433 && loc->owner->enable_state == bp_enabled
2434 && loc->enabled)
2435 {
2436 /* Add the command to the vector. This will be used later
2437 to send the commands to the target. */
2438 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2439 }
2440 }
2441
2442 bl->target_info.persist = 0;
2443 /* Maybe flag this location as persistent. */
2444 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2445 bl->target_info.persist = 1;
2446 }
2447
2448 /* Return the kind of breakpoint on address *ADDR. Get the kind
2449 of breakpoint according to ADDR except single-step breakpoint.
2450 Get the kind of single-step breakpoint according to the current
2451 registers state. */
2452
2453 static int
2454 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2455 {
2456 if (bl->owner->type == bp_single_step)
2457 {
2458 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2459 struct regcache *regcache;
2460
2461 regcache = get_thread_regcache (thr->ptid);
2462
2463 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2464 regcache, addr);
2465 }
2466 else
2467 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2468 }
2469
2470 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2471 location. Any error messages are printed to TMP_ERROR_STREAM; and
2472 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2473 Returns 0 for success, 1 if the bp_location type is not supported or
2474 -1 for failure.
2475
2476 NOTE drow/2003-09-09: This routine could be broken down to an
2477 object-style method for each breakpoint or catchpoint type. */
2478 static int
2479 insert_bp_location (struct bp_location *bl,
2480 struct ui_file *tmp_error_stream,
2481 int *disabled_breaks,
2482 int *hw_breakpoint_error,
2483 int *hw_bp_error_explained_already)
2484 {
2485 enum errors bp_err = GDB_NO_ERROR;
2486 const char *bp_err_message = NULL;
2487
2488 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2489 return 0;
2490
2491 /* Note we don't initialize bl->target_info, as that wipes out
2492 the breakpoint location's shadow_contents if the breakpoint
2493 is still inserted at that location. This in turn breaks
2494 target_read_memory which depends on these buffers when
2495 a memory read is requested at the breakpoint location:
2496 Once the target_info has been wiped, we fail to see that
2497 we have a breakpoint inserted at that address and thus
2498 read the breakpoint instead of returning the data saved in
2499 the breakpoint location's shadow contents. */
2500 bl->target_info.reqstd_address = bl->address;
2501 bl->target_info.placed_address_space = bl->pspace->aspace;
2502 bl->target_info.length = bl->length;
2503
2504 /* When working with target-side conditions, we must pass all the conditions
2505 for the same breakpoint address down to the target since GDB will not
2506 insert those locations. With a list of breakpoint conditions, the target
2507 can decide when to stop and notify GDB. */
2508
2509 if (is_breakpoint (bl->owner))
2510 {
2511 build_target_condition_list (bl);
2512 build_target_command_list (bl);
2513 /* Reset the modification marker. */
2514 bl->needs_update = 0;
2515 }
2516
2517 if (bl->loc_type == bp_loc_software_breakpoint
2518 || bl->loc_type == bp_loc_hardware_breakpoint)
2519 {
2520 if (bl->owner->type != bp_hardware_breakpoint)
2521 {
2522 /* If the explicitly specified breakpoint type
2523 is not hardware breakpoint, check the memory map to see
2524 if the breakpoint address is in read only memory or not.
2525
2526 Two important cases are:
2527 - location type is not hardware breakpoint, memory
2528 is readonly. We change the type of the location to
2529 hardware breakpoint.
2530 - location type is hardware breakpoint, memory is
2531 read-write. This means we've previously made the
2532 location hardware one, but then the memory map changed,
2533 so we undo.
2534
2535 When breakpoints are removed, remove_breakpoints will use
2536 location types we've just set here, the only possible
2537 problem is that memory map has changed during running
2538 program, but it's not going to work anyway with current
2539 gdb. */
2540 struct mem_region *mr
2541 = lookup_mem_region (bl->target_info.reqstd_address);
2542
2543 if (mr)
2544 {
2545 if (automatic_hardware_breakpoints)
2546 {
2547 enum bp_loc_type new_type;
2548
2549 if (mr->attrib.mode != MEM_RW)
2550 new_type = bp_loc_hardware_breakpoint;
2551 else
2552 new_type = bp_loc_software_breakpoint;
2553
2554 if (new_type != bl->loc_type)
2555 {
2556 static int said = 0;
2557
2558 bl->loc_type = new_type;
2559 if (!said)
2560 {
2561 fprintf_filtered (gdb_stdout,
2562 _("Note: automatically using "
2563 "hardware breakpoints for "
2564 "read-only addresses.\n"));
2565 said = 1;
2566 }
2567 }
2568 }
2569 else if (bl->loc_type == bp_loc_software_breakpoint
2570 && mr->attrib.mode != MEM_RW)
2571 {
2572 fprintf_unfiltered (tmp_error_stream,
2573 _("Cannot insert breakpoint %d.\n"
2574 "Cannot set software breakpoint "
2575 "at read-only address %s\n"),
2576 bl->owner->number,
2577 paddress (bl->gdbarch, bl->address));
2578 return 1;
2579 }
2580 }
2581 }
2582
2583 /* First check to see if we have to handle an overlay. */
2584 if (overlay_debugging == ovly_off
2585 || bl->section == NULL
2586 || !(section_is_overlay (bl->section)))
2587 {
2588 /* No overlay handling: just set the breakpoint. */
2589 TRY
2590 {
2591 int val;
2592
2593 val = bl->owner->ops->insert_location (bl);
2594 if (val)
2595 bp_err = GENERIC_ERROR;
2596 }
2597 CATCH (e, RETURN_MASK_ALL)
2598 {
2599 bp_err = e.error;
2600 bp_err_message = e.message;
2601 }
2602 END_CATCH
2603 }
2604 else
2605 {
2606 /* This breakpoint is in an overlay section.
2607 Shall we set a breakpoint at the LMA? */
2608 if (!overlay_events_enabled)
2609 {
2610 /* Yes -- overlay event support is not active,
2611 so we must try to set a breakpoint at the LMA.
2612 This will not work for a hardware breakpoint. */
2613 if (bl->loc_type == bp_loc_hardware_breakpoint)
2614 warning (_("hardware breakpoint %d not supported in overlay!"),
2615 bl->owner->number);
2616 else
2617 {
2618 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2619 bl->section);
2620 /* Set a software (trap) breakpoint at the LMA. */
2621 bl->overlay_target_info = bl->target_info;
2622 bl->overlay_target_info.reqstd_address = addr;
2623
2624 /* No overlay handling: just set the breakpoint. */
2625 TRY
2626 {
2627 int val;
2628
2629 bl->overlay_target_info.kind
2630 = breakpoint_kind (bl, &addr);
2631 bl->overlay_target_info.placed_address = addr;
2632 val = target_insert_breakpoint (bl->gdbarch,
2633 &bl->overlay_target_info);
2634 if (val)
2635 bp_err = GENERIC_ERROR;
2636 }
2637 CATCH (e, RETURN_MASK_ALL)
2638 {
2639 bp_err = e.error;
2640 bp_err_message = e.message;
2641 }
2642 END_CATCH
2643
2644 if (bp_err != GDB_NO_ERROR)
2645 fprintf_unfiltered (tmp_error_stream,
2646 "Overlay breakpoint %d "
2647 "failed: in ROM?\n",
2648 bl->owner->number);
2649 }
2650 }
2651 /* Shall we set a breakpoint at the VMA? */
2652 if (section_is_mapped (bl->section))
2653 {
2654 /* Yes. This overlay section is mapped into memory. */
2655 TRY
2656 {
2657 int val;
2658
2659 val = bl->owner->ops->insert_location (bl);
2660 if (val)
2661 bp_err = GENERIC_ERROR;
2662 }
2663 CATCH (e, RETURN_MASK_ALL)
2664 {
2665 bp_err = e.error;
2666 bp_err_message = e.message;
2667 }
2668 END_CATCH
2669 }
2670 else
2671 {
2672 /* No. This breakpoint will not be inserted.
2673 No error, but do not mark the bp as 'inserted'. */
2674 return 0;
2675 }
2676 }
2677
2678 if (bp_err != GDB_NO_ERROR)
2679 {
2680 /* Can't set the breakpoint. */
2681
2682 /* In some cases, we might not be able to insert a
2683 breakpoint in a shared library that has already been
2684 removed, but we have not yet processed the shlib unload
2685 event. Unfortunately, some targets that implement
2686 breakpoint insertion themselves can't tell why the
2687 breakpoint insertion failed (e.g., the remote target
2688 doesn't define error codes), so we must treat generic
2689 errors as memory errors. */
2690 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2691 && bl->loc_type == bp_loc_software_breakpoint
2692 && (solib_name_from_address (bl->pspace, bl->address)
2693 || shared_objfile_contains_address_p (bl->pspace,
2694 bl->address)))
2695 {
2696 /* See also: disable_breakpoints_in_shlibs. */
2697 bl->shlib_disabled = 1;
2698 observer_notify_breakpoint_modified (bl->owner);
2699 if (!*disabled_breaks)
2700 {
2701 fprintf_unfiltered (tmp_error_stream,
2702 "Cannot insert breakpoint %d.\n",
2703 bl->owner->number);
2704 fprintf_unfiltered (tmp_error_stream,
2705 "Temporarily disabling shared "
2706 "library breakpoints:\n");
2707 }
2708 *disabled_breaks = 1;
2709 fprintf_unfiltered (tmp_error_stream,
2710 "breakpoint #%d\n", bl->owner->number);
2711 return 0;
2712 }
2713 else
2714 {
2715 if (bl->loc_type == bp_loc_hardware_breakpoint)
2716 {
2717 *hw_breakpoint_error = 1;
2718 *hw_bp_error_explained_already = bp_err_message != NULL;
2719 fprintf_unfiltered (tmp_error_stream,
2720 "Cannot insert hardware breakpoint %d%s",
2721 bl->owner->number, bp_err_message ? ":" : ".\n");
2722 if (bp_err_message != NULL)
2723 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2724 }
2725 else
2726 {
2727 if (bp_err_message == NULL)
2728 {
2729 std::string message
2730 = memory_error_message (TARGET_XFER_E_IO,
2731 bl->gdbarch, bl->address);
2732
2733 fprintf_unfiltered (tmp_error_stream,
2734 "Cannot insert breakpoint %d.\n"
2735 "%s\n",
2736 bl->owner->number, message.c_str ());
2737 }
2738 else
2739 {
2740 fprintf_unfiltered (tmp_error_stream,
2741 "Cannot insert breakpoint %d: %s\n",
2742 bl->owner->number,
2743 bp_err_message);
2744 }
2745 }
2746 return 1;
2747
2748 }
2749 }
2750 else
2751 bl->inserted = 1;
2752
2753 return 0;
2754 }
2755
2756 else if (bl->loc_type == bp_loc_hardware_watchpoint
2757 /* NOTE drow/2003-09-08: This state only exists for removing
2758 watchpoints. It's not clear that it's necessary... */
2759 && bl->owner->disposition != disp_del_at_next_stop)
2760 {
2761 int val;
2762
2763 gdb_assert (bl->owner->ops != NULL
2764 && bl->owner->ops->insert_location != NULL);
2765
2766 val = bl->owner->ops->insert_location (bl);
2767
2768 /* If trying to set a read-watchpoint, and it turns out it's not
2769 supported, try emulating one with an access watchpoint. */
2770 if (val == 1 && bl->watchpoint_type == hw_read)
2771 {
2772 struct bp_location *loc, **loc_temp;
2773
2774 /* But don't try to insert it, if there's already another
2775 hw_access location that would be considered a duplicate
2776 of this one. */
2777 ALL_BP_LOCATIONS (loc, loc_temp)
2778 if (loc != bl
2779 && loc->watchpoint_type == hw_access
2780 && watchpoint_locations_match (bl, loc))
2781 {
2782 bl->duplicate = 1;
2783 bl->inserted = 1;
2784 bl->target_info = loc->target_info;
2785 bl->watchpoint_type = hw_access;
2786 val = 0;
2787 break;
2788 }
2789
2790 if (val == 1)
2791 {
2792 bl->watchpoint_type = hw_access;
2793 val = bl->owner->ops->insert_location (bl);
2794
2795 if (val)
2796 /* Back to the original value. */
2797 bl->watchpoint_type = hw_read;
2798 }
2799 }
2800
2801 bl->inserted = (val == 0);
2802 }
2803
2804 else if (bl->owner->type == bp_catchpoint)
2805 {
2806 int val;
2807
2808 gdb_assert (bl->owner->ops != NULL
2809 && bl->owner->ops->insert_location != NULL);
2810
2811 val = bl->owner->ops->insert_location (bl);
2812 if (val)
2813 {
2814 bl->owner->enable_state = bp_disabled;
2815
2816 if (val == 1)
2817 warning (_("\
2818 Error inserting catchpoint %d: Your system does not support this type\n\
2819 of catchpoint."), bl->owner->number);
2820 else
2821 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2822 }
2823
2824 bl->inserted = (val == 0);
2825
2826 /* We've already printed an error message if there was a problem
2827 inserting this catchpoint, and we've disabled the catchpoint,
2828 so just return success. */
2829 return 0;
2830 }
2831
2832 return 0;
2833 }
2834
2835 /* This function is called when program space PSPACE is about to be
2836 deleted. It takes care of updating breakpoints to not reference
2837 PSPACE anymore. */
2838
2839 void
2840 breakpoint_program_space_exit (struct program_space *pspace)
2841 {
2842 struct breakpoint *b, *b_temp;
2843 struct bp_location *loc, **loc_temp;
2844
2845 /* Remove any breakpoint that was set through this program space. */
2846 ALL_BREAKPOINTS_SAFE (b, b_temp)
2847 {
2848 if (b->pspace == pspace)
2849 delete_breakpoint (b);
2850 }
2851
2852 /* Breakpoints set through other program spaces could have locations
2853 bound to PSPACE as well. Remove those. */
2854 ALL_BP_LOCATIONS (loc, loc_temp)
2855 {
2856 struct bp_location *tmp;
2857
2858 if (loc->pspace == pspace)
2859 {
2860 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2861 if (loc->owner->loc == loc)
2862 loc->owner->loc = loc->next;
2863 else
2864 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2865 if (tmp->next == loc)
2866 {
2867 tmp->next = loc->next;
2868 break;
2869 }
2870 }
2871 }
2872
2873 /* Now update the global location list to permanently delete the
2874 removed locations above. */
2875 update_global_location_list (UGLL_DONT_INSERT);
2876 }
2877
2878 /* Make sure all breakpoints are inserted in inferior.
2879 Throws exception on any error.
2880 A breakpoint that is already inserted won't be inserted
2881 again, so calling this function twice is safe. */
2882 void
2883 insert_breakpoints (void)
2884 {
2885 struct breakpoint *bpt;
2886
2887 ALL_BREAKPOINTS (bpt)
2888 if (is_hardware_watchpoint (bpt))
2889 {
2890 struct watchpoint *w = (struct watchpoint *) bpt;
2891
2892 update_watchpoint (w, 0 /* don't reparse. */);
2893 }
2894
2895 /* Updating watchpoints creates new locations, so update the global
2896 location list. Explicitly tell ugll to insert locations and
2897 ignore breakpoints_always_inserted_mode. */
2898 update_global_location_list (UGLL_INSERT);
2899 }
2900
2901 /* Invoke CALLBACK for each of bp_location. */
2902
2903 void
2904 iterate_over_bp_locations (walk_bp_location_callback callback)
2905 {
2906 struct bp_location *loc, **loc_tmp;
2907
2908 ALL_BP_LOCATIONS (loc, loc_tmp)
2909 {
2910 callback (loc, NULL);
2911 }
2912 }
2913
2914 /* This is used when we need to synch breakpoint conditions between GDB and the
2915 target. It is the case with deleting and disabling of breakpoints when using
2916 always-inserted mode. */
2917
2918 static void
2919 update_inserted_breakpoint_locations (void)
2920 {
2921 struct bp_location *bl, **blp_tmp;
2922 int error_flag = 0;
2923 int val = 0;
2924 int disabled_breaks = 0;
2925 int hw_breakpoint_error = 0;
2926 int hw_bp_details_reported = 0;
2927
2928 string_file tmp_error_stream;
2929
2930 /* Explicitly mark the warning -- this will only be printed if
2931 there was an error. */
2932 tmp_error_stream.puts ("Warning:\n");
2933
2934 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2935
2936 ALL_BP_LOCATIONS (bl, blp_tmp)
2937 {
2938 /* We only want to update software breakpoints and hardware
2939 breakpoints. */
2940 if (!is_breakpoint (bl->owner))
2941 continue;
2942
2943 /* We only want to update locations that are already inserted
2944 and need updating. This is to avoid unwanted insertion during
2945 deletion of breakpoints. */
2946 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2947 continue;
2948
2949 switch_to_program_space_and_thread (bl->pspace);
2950
2951 /* For targets that support global breakpoints, there's no need
2952 to select an inferior to insert breakpoint to. In fact, even
2953 if we aren't attached to any process yet, we should still
2954 insert breakpoints. */
2955 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2956 && ptid_equal (inferior_ptid, null_ptid))
2957 continue;
2958
2959 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2960 &hw_breakpoint_error, &hw_bp_details_reported);
2961 if (val)
2962 error_flag = val;
2963 }
2964
2965 if (error_flag)
2966 {
2967 target_terminal::ours_for_output ();
2968 error_stream (tmp_error_stream);
2969 }
2970 }
2971
2972 /* Used when starting or continuing the program. */
2973
2974 static void
2975 insert_breakpoint_locations (void)
2976 {
2977 struct breakpoint *bpt;
2978 struct bp_location *bl, **blp_tmp;
2979 int error_flag = 0;
2980 int val = 0;
2981 int disabled_breaks = 0;
2982 int hw_breakpoint_error = 0;
2983 int hw_bp_error_explained_already = 0;
2984
2985 string_file tmp_error_stream;
2986
2987 /* Explicitly mark the warning -- this will only be printed if
2988 there was an error. */
2989 tmp_error_stream.puts ("Warning:\n");
2990
2991 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2992
2993 ALL_BP_LOCATIONS (bl, blp_tmp)
2994 {
2995 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2996 continue;
2997
2998 /* There is no point inserting thread-specific breakpoints if
2999 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3000 has BL->OWNER always non-NULL. */
3001 if (bl->owner->thread != -1
3002 && !valid_global_thread_id (bl->owner->thread))
3003 continue;
3004
3005 switch_to_program_space_and_thread (bl->pspace);
3006
3007 /* For targets that support global breakpoints, there's no need
3008 to select an inferior to insert breakpoint to. In fact, even
3009 if we aren't attached to any process yet, we should still
3010 insert breakpoints. */
3011 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3012 && ptid_equal (inferior_ptid, null_ptid))
3013 continue;
3014
3015 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3016 &hw_breakpoint_error, &hw_bp_error_explained_already);
3017 if (val)
3018 error_flag = val;
3019 }
3020
3021 /* If we failed to insert all locations of a watchpoint, remove
3022 them, as half-inserted watchpoint is of limited use. */
3023 ALL_BREAKPOINTS (bpt)
3024 {
3025 int some_failed = 0;
3026 struct bp_location *loc;
3027
3028 if (!is_hardware_watchpoint (bpt))
3029 continue;
3030
3031 if (!breakpoint_enabled (bpt))
3032 continue;
3033
3034 if (bpt->disposition == disp_del_at_next_stop)
3035 continue;
3036
3037 for (loc = bpt->loc; loc; loc = loc->next)
3038 if (!loc->inserted && should_be_inserted (loc))
3039 {
3040 some_failed = 1;
3041 break;
3042 }
3043 if (some_failed)
3044 {
3045 for (loc = bpt->loc; loc; loc = loc->next)
3046 if (loc->inserted)
3047 remove_breakpoint (loc);
3048
3049 hw_breakpoint_error = 1;
3050 tmp_error_stream.printf ("Could not insert "
3051 "hardware watchpoint %d.\n",
3052 bpt->number);
3053 error_flag = -1;
3054 }
3055 }
3056
3057 if (error_flag)
3058 {
3059 /* If a hardware breakpoint or watchpoint was inserted, add a
3060 message about possibly exhausted resources. */
3061 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3062 {
3063 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3064 You may have requested too many hardware breakpoints/watchpoints.\n");
3065 }
3066 target_terminal::ours_for_output ();
3067 error_stream (tmp_error_stream);
3068 }
3069 }
3070
3071 /* Used when the program stops.
3072 Returns zero if successful, or non-zero if there was a problem
3073 removing a breakpoint location. */
3074
3075 int
3076 remove_breakpoints (void)
3077 {
3078 struct bp_location *bl, **blp_tmp;
3079 int val = 0;
3080
3081 ALL_BP_LOCATIONS (bl, blp_tmp)
3082 {
3083 if (bl->inserted && !is_tracepoint (bl->owner))
3084 val |= remove_breakpoint (bl);
3085 }
3086 return val;
3087 }
3088
3089 /* When a thread exits, remove breakpoints that are related to
3090 that thread. */
3091
3092 static void
3093 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3094 {
3095 struct breakpoint *b, *b_tmp;
3096
3097 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3098 {
3099 if (b->thread == tp->global_num && user_breakpoint_p (b))
3100 {
3101 b->disposition = disp_del_at_next_stop;
3102
3103 printf_filtered (_("\
3104 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3105 b->number, print_thread_id (tp));
3106
3107 /* Hide it from the user. */
3108 b->number = 0;
3109 }
3110 }
3111 }
3112
3113 /* Remove breakpoints of process PID. */
3114
3115 int
3116 remove_breakpoints_pid (int pid)
3117 {
3118 struct bp_location *bl, **blp_tmp;
3119 int val;
3120 struct inferior *inf = find_inferior_pid (pid);
3121
3122 ALL_BP_LOCATIONS (bl, blp_tmp)
3123 {
3124 if (bl->pspace != inf->pspace)
3125 continue;
3126
3127 if (bl->inserted && !bl->target_info.persist)
3128 {
3129 val = remove_breakpoint (bl);
3130 if (val != 0)
3131 return val;
3132 }
3133 }
3134 return 0;
3135 }
3136
3137 static int internal_breakpoint_number = -1;
3138
3139 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3140 If INTERNAL is non-zero, the breakpoint number will be populated
3141 from internal_breakpoint_number and that variable decremented.
3142 Otherwise the breakpoint number will be populated from
3143 breakpoint_count and that value incremented. Internal breakpoints
3144 do not set the internal var bpnum. */
3145 static void
3146 set_breakpoint_number (int internal, struct breakpoint *b)
3147 {
3148 if (internal)
3149 b->number = internal_breakpoint_number--;
3150 else
3151 {
3152 set_breakpoint_count (breakpoint_count + 1);
3153 b->number = breakpoint_count;
3154 }
3155 }
3156
3157 static struct breakpoint *
3158 create_internal_breakpoint (struct gdbarch *gdbarch,
3159 CORE_ADDR address, enum bptype type,
3160 const struct breakpoint_ops *ops)
3161 {
3162 symtab_and_line sal;
3163 sal.pc = address;
3164 sal.section = find_pc_overlay (sal.pc);
3165 sal.pspace = current_program_space;
3166
3167 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3168 b->number = internal_breakpoint_number--;
3169 b->disposition = disp_donttouch;
3170
3171 return b;
3172 }
3173
3174 static const char *const longjmp_names[] =
3175 {
3176 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3177 };
3178 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3179
3180 /* Per-objfile data private to breakpoint.c. */
3181 struct breakpoint_objfile_data
3182 {
3183 /* Minimal symbol for "_ovly_debug_event" (if any). */
3184 struct bound_minimal_symbol overlay_msym {};
3185
3186 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3187 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3188
3189 /* True if we have looked for longjmp probes. */
3190 int longjmp_searched = 0;
3191
3192 /* SystemTap probe points for longjmp (if any). These are non-owning
3193 references. */
3194 std::vector<probe *> longjmp_probes;
3195
3196 /* Minimal symbol for "std::terminate()" (if any). */
3197 struct bound_minimal_symbol terminate_msym {};
3198
3199 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3200 struct bound_minimal_symbol exception_msym {};
3201
3202 /* True if we have looked for exception probes. */
3203 int exception_searched = 0;
3204
3205 /* SystemTap probe points for unwinding (if any). These are non-owning
3206 references. */
3207 std::vector<probe *> exception_probes;
3208 };
3209
3210 static const struct objfile_data *breakpoint_objfile_key;
3211
3212 /* Minimal symbol not found sentinel. */
3213 static struct minimal_symbol msym_not_found;
3214
3215 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3216
3217 static int
3218 msym_not_found_p (const struct minimal_symbol *msym)
3219 {
3220 return msym == &msym_not_found;
3221 }
3222
3223 /* Return per-objfile data needed by breakpoint.c.
3224 Allocate the data if necessary. */
3225
3226 static struct breakpoint_objfile_data *
3227 get_breakpoint_objfile_data (struct objfile *objfile)
3228 {
3229 struct breakpoint_objfile_data *bp_objfile_data;
3230
3231 bp_objfile_data = ((struct breakpoint_objfile_data *)
3232 objfile_data (objfile, breakpoint_objfile_key));
3233 if (bp_objfile_data == NULL)
3234 {
3235 bp_objfile_data = 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 delete bp_objfile_data;
3248 }
3249
3250 static void
3251 create_overlay_event_breakpoint (void)
3252 {
3253 struct objfile *objfile;
3254 const char *const func_name = "_ovly_debug_event";
3255
3256 ALL_OBJFILES (objfile)
3257 {
3258 struct breakpoint *b;
3259 struct breakpoint_objfile_data *bp_objfile_data;
3260 CORE_ADDR addr;
3261 struct explicit_location explicit_loc;
3262
3263 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3264
3265 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3266 continue;
3267
3268 if (bp_objfile_data->overlay_msym.minsym == NULL)
3269 {
3270 struct bound_minimal_symbol m;
3271
3272 m = lookup_minimal_symbol_text (func_name, objfile);
3273 if (m.minsym == NULL)
3274 {
3275 /* Avoid future lookups in this objfile. */
3276 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3277 continue;
3278 }
3279 bp_objfile_data->overlay_msym = m;
3280 }
3281
3282 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3283 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3284 bp_overlay_event,
3285 &internal_breakpoint_ops);
3286 initialize_explicit_location (&explicit_loc);
3287 explicit_loc.function_name = ASTRDUP (func_name);
3288 b->location = new_explicit_location (&explicit_loc);
3289
3290 if (overlay_debugging == ovly_auto)
3291 {
3292 b->enable_state = bp_enabled;
3293 overlay_events_enabled = 1;
3294 }
3295 else
3296 {
3297 b->enable_state = bp_disabled;
3298 overlay_events_enabled = 0;
3299 }
3300 }
3301 }
3302
3303 static void
3304 create_longjmp_master_breakpoint (void)
3305 {
3306 struct program_space *pspace;
3307
3308 scoped_restore_current_program_space restore_pspace;
3309
3310 ALL_PSPACES (pspace)
3311 {
3312 struct objfile *objfile;
3313
3314 set_current_program_space (pspace);
3315
3316 ALL_OBJFILES (objfile)
3317 {
3318 int i;
3319 struct gdbarch *gdbarch;
3320 struct breakpoint_objfile_data *bp_objfile_data;
3321
3322 gdbarch = get_objfile_arch (objfile);
3323
3324 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3325
3326 if (!bp_objfile_data->longjmp_searched)
3327 {
3328 std::vector<probe *> ret
3329 = find_probes_in_objfile (objfile, "libc", "longjmp");
3330
3331 if (!ret.empty ())
3332 {
3333 /* We are only interested in checking one element. */
3334 probe *p = ret[0];
3335
3336 if (!can_evaluate_probe_arguments (p))
3337 {
3338 /* We cannot use the probe interface here, because it does
3339 not know how to evaluate arguments. */
3340 ret.clear ();
3341 }
3342 }
3343 bp_objfile_data->longjmp_probes = ret;
3344 bp_objfile_data->longjmp_searched = 1;
3345 }
3346
3347 if (!bp_objfile_data->longjmp_probes.empty ())
3348 {
3349 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3350
3351 for (probe *p : bp_objfile_data->longjmp_probes)
3352 {
3353 struct breakpoint *b;
3354
3355 b = create_internal_breakpoint (gdbarch,
3356 get_probe_address (p, objfile),
3357 bp_longjmp_master,
3358 &internal_breakpoint_ops);
3359 b->location = new_probe_location ("-probe-stap libc:longjmp");
3360 b->enable_state = bp_disabled;
3361 }
3362
3363 continue;
3364 }
3365
3366 if (!gdbarch_get_longjmp_target_p (gdbarch))
3367 continue;
3368
3369 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3370 {
3371 struct breakpoint *b;
3372 const char *func_name;
3373 CORE_ADDR addr;
3374 struct explicit_location explicit_loc;
3375
3376 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3377 continue;
3378
3379 func_name = longjmp_names[i];
3380 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3381 {
3382 struct bound_minimal_symbol m;
3383
3384 m = lookup_minimal_symbol_text (func_name, objfile);
3385 if (m.minsym == NULL)
3386 {
3387 /* Prevent future lookups in this objfile. */
3388 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3389 continue;
3390 }
3391 bp_objfile_data->longjmp_msym[i] = m;
3392 }
3393
3394 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3395 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3396 &internal_breakpoint_ops);
3397 initialize_explicit_location (&explicit_loc);
3398 explicit_loc.function_name = ASTRDUP (func_name);
3399 b->location = new_explicit_location (&explicit_loc);
3400 b->enable_state = bp_disabled;
3401 }
3402 }
3403 }
3404 }
3405
3406 /* Create a master std::terminate breakpoint. */
3407 static void
3408 create_std_terminate_master_breakpoint (void)
3409 {
3410 struct program_space *pspace;
3411 const char *const func_name = "std::terminate()";
3412
3413 scoped_restore_current_program_space restore_pspace;
3414
3415 ALL_PSPACES (pspace)
3416 {
3417 struct objfile *objfile;
3418 CORE_ADDR addr;
3419
3420 set_current_program_space (pspace);
3421
3422 ALL_OBJFILES (objfile)
3423 {
3424 struct breakpoint *b;
3425 struct breakpoint_objfile_data *bp_objfile_data;
3426 struct explicit_location explicit_loc;
3427
3428 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3429
3430 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3431 continue;
3432
3433 if (bp_objfile_data->terminate_msym.minsym == NULL)
3434 {
3435 struct bound_minimal_symbol m;
3436
3437 m = lookup_minimal_symbol (func_name, NULL, objfile);
3438 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3439 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3440 {
3441 /* Prevent future lookups in this objfile. */
3442 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3443 continue;
3444 }
3445 bp_objfile_data->terminate_msym = m;
3446 }
3447
3448 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3449 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3450 bp_std_terminate_master,
3451 &internal_breakpoint_ops);
3452 initialize_explicit_location (&explicit_loc);
3453 explicit_loc.function_name = ASTRDUP (func_name);
3454 b->location = new_explicit_location (&explicit_loc);
3455 b->enable_state = bp_disabled;
3456 }
3457 }
3458 }
3459
3460 /* Install a master breakpoint on the unwinder's debug hook. */
3461
3462 static void
3463 create_exception_master_breakpoint (void)
3464 {
3465 struct objfile *objfile;
3466 const char *const func_name = "_Unwind_DebugHook";
3467
3468 ALL_OBJFILES (objfile)
3469 {
3470 struct breakpoint *b;
3471 struct gdbarch *gdbarch;
3472 struct breakpoint_objfile_data *bp_objfile_data;
3473 CORE_ADDR addr;
3474 struct explicit_location explicit_loc;
3475
3476 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3477
3478 /* We prefer the SystemTap probe point if it exists. */
3479 if (!bp_objfile_data->exception_searched)
3480 {
3481 std::vector<probe *> ret
3482 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3483
3484 if (!ret.empty ())
3485 {
3486 /* We are only interested in checking one element. */
3487 probe *p = ret[0];
3488
3489 if (!can_evaluate_probe_arguments (p))
3490 {
3491 /* We cannot use the probe interface here, because it does
3492 not know how to evaluate arguments. */
3493 ret.clear ();
3494 }
3495 }
3496 bp_objfile_data->exception_probes = ret;
3497 bp_objfile_data->exception_searched = 1;
3498 }
3499
3500 if (!bp_objfile_data->exception_probes.empty ())
3501 {
3502 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3503
3504 for (probe *p : bp_objfile_data->exception_probes)
3505 {
3506 struct breakpoint *b;
3507
3508 b = create_internal_breakpoint (gdbarch,
3509 get_probe_address (p, objfile),
3510 bp_exception_master,
3511 &internal_breakpoint_ops);
3512 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3513 b->enable_state = bp_disabled;
3514 }
3515
3516 continue;
3517 }
3518
3519 /* Otherwise, try the hook function. */
3520
3521 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3522 continue;
3523
3524 gdbarch = get_objfile_arch (objfile);
3525
3526 if (bp_objfile_data->exception_msym.minsym == NULL)
3527 {
3528 struct bound_minimal_symbol debug_hook;
3529
3530 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3531 if (debug_hook.minsym == NULL)
3532 {
3533 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3534 continue;
3535 }
3536
3537 bp_objfile_data->exception_msym = debug_hook;
3538 }
3539
3540 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3541 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3542 &current_target);
3543 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3544 &internal_breakpoint_ops);
3545 initialize_explicit_location (&explicit_loc);
3546 explicit_loc.function_name = ASTRDUP (func_name);
3547 b->location = new_explicit_location (&explicit_loc);
3548 b->enable_state = bp_disabled;
3549 }
3550 }
3551
3552 /* Does B have a location spec? */
3553
3554 static int
3555 breakpoint_event_location_empty_p (const struct breakpoint *b)
3556 {
3557 return b->location != NULL && event_location_empty_p (b->location.get ());
3558 }
3559
3560 void
3561 update_breakpoints_after_exec (void)
3562 {
3563 struct breakpoint *b, *b_tmp;
3564 struct bp_location *bploc, **bplocp_tmp;
3565
3566 /* We're about to delete breakpoints from GDB's lists. If the
3567 INSERTED flag is true, GDB will try to lift the breakpoints by
3568 writing the breakpoints' "shadow contents" back into memory. The
3569 "shadow contents" are NOT valid after an exec, so GDB should not
3570 do that. Instead, the target is responsible from marking
3571 breakpoints out as soon as it detects an exec. We don't do that
3572 here instead, because there may be other attempts to delete
3573 breakpoints after detecting an exec and before reaching here. */
3574 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3575 if (bploc->pspace == current_program_space)
3576 gdb_assert (!bploc->inserted);
3577
3578 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3579 {
3580 if (b->pspace != current_program_space)
3581 continue;
3582
3583 /* Solib breakpoints must be explicitly reset after an exec(). */
3584 if (b->type == bp_shlib_event)
3585 {
3586 delete_breakpoint (b);
3587 continue;
3588 }
3589
3590 /* JIT breakpoints must be explicitly reset after an exec(). */
3591 if (b->type == bp_jit_event)
3592 {
3593 delete_breakpoint (b);
3594 continue;
3595 }
3596
3597 /* Thread event breakpoints must be set anew after an exec(),
3598 as must overlay event and longjmp master breakpoints. */
3599 if (b->type == bp_thread_event || b->type == bp_overlay_event
3600 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3601 || b->type == bp_exception_master)
3602 {
3603 delete_breakpoint (b);
3604 continue;
3605 }
3606
3607 /* Step-resume breakpoints are meaningless after an exec(). */
3608 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3609 {
3610 delete_breakpoint (b);
3611 continue;
3612 }
3613
3614 /* Just like single-step breakpoints. */
3615 if (b->type == bp_single_step)
3616 {
3617 delete_breakpoint (b);
3618 continue;
3619 }
3620
3621 /* Longjmp and longjmp-resume breakpoints are also meaningless
3622 after an exec. */
3623 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3624 || b->type == bp_longjmp_call_dummy
3625 || b->type == bp_exception || b->type == bp_exception_resume)
3626 {
3627 delete_breakpoint (b);
3628 continue;
3629 }
3630
3631 if (b->type == bp_catchpoint)
3632 {
3633 /* For now, none of the bp_catchpoint breakpoints need to
3634 do anything at this point. In the future, if some of
3635 the catchpoints need to something, we will need to add
3636 a new method, and call this method from here. */
3637 continue;
3638 }
3639
3640 /* bp_finish is a special case. The only way we ought to be able
3641 to see one of these when an exec() has happened, is if the user
3642 caught a vfork, and then said "finish". Ordinarily a finish just
3643 carries them to the call-site of the current callee, by setting
3644 a temporary bp there and resuming. But in this case, the finish
3645 will carry them entirely through the vfork & exec.
3646
3647 We don't want to allow a bp_finish to remain inserted now. But
3648 we can't safely delete it, 'cause finish_command has a handle to
3649 the bp on a bpstat, and will later want to delete it. There's a
3650 chance (and I've seen it happen) that if we delete the bp_finish
3651 here, that its storage will get reused by the time finish_command
3652 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3653 We really must allow finish_command to delete a bp_finish.
3654
3655 In the absence of a general solution for the "how do we know
3656 it's safe to delete something others may have handles to?"
3657 problem, what we'll do here is just uninsert the bp_finish, and
3658 let finish_command delete it.
3659
3660 (We know the bp_finish is "doomed" in the sense that it's
3661 momentary, and will be deleted as soon as finish_command sees
3662 the inferior stopped. So it doesn't matter that the bp's
3663 address is probably bogus in the new a.out, unlike e.g., the
3664 solib breakpoints.) */
3665
3666 if (b->type == bp_finish)
3667 {
3668 continue;
3669 }
3670
3671 /* Without a symbolic address, we have little hope of the
3672 pre-exec() address meaning the same thing in the post-exec()
3673 a.out. */
3674 if (breakpoint_event_location_empty_p (b))
3675 {
3676 delete_breakpoint (b);
3677 continue;
3678 }
3679 }
3680 }
3681
3682 int
3683 detach_breakpoints (ptid_t ptid)
3684 {
3685 struct bp_location *bl, **blp_tmp;
3686 int val = 0;
3687 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3688 struct inferior *inf = current_inferior ();
3689
3690 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3691 error (_("Cannot detach breakpoints of inferior_ptid"));
3692
3693 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3694 inferior_ptid = ptid;
3695 ALL_BP_LOCATIONS (bl, blp_tmp)
3696 {
3697 if (bl->pspace != inf->pspace)
3698 continue;
3699
3700 /* This function must physically remove breakpoints locations
3701 from the specified ptid, without modifying the breakpoint
3702 package's state. Locations of type bp_loc_other are only
3703 maintained at GDB side. So, there is no need to remove
3704 these bp_loc_other locations. Moreover, removing these
3705 would modify the breakpoint package's state. */
3706 if (bl->loc_type == bp_loc_other)
3707 continue;
3708
3709 if (bl->inserted)
3710 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3711 }
3712
3713 return val;
3714 }
3715
3716 /* Remove the breakpoint location BL from the current address space.
3717 Note that this is used to detach breakpoints from a child fork.
3718 When we get here, the child isn't in the inferior list, and neither
3719 do we have objects to represent its address space --- we should
3720 *not* look at bl->pspace->aspace here. */
3721
3722 static int
3723 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3724 {
3725 int val;
3726
3727 /* BL is never in moribund_locations by our callers. */
3728 gdb_assert (bl->owner != NULL);
3729
3730 /* The type of none suggests that owner is actually deleted.
3731 This should not ever happen. */
3732 gdb_assert (bl->owner->type != bp_none);
3733
3734 if (bl->loc_type == bp_loc_software_breakpoint
3735 || bl->loc_type == bp_loc_hardware_breakpoint)
3736 {
3737 /* "Normal" instruction breakpoint: either the standard
3738 trap-instruction bp (bp_breakpoint), or a
3739 bp_hardware_breakpoint. */
3740
3741 /* First check to see if we have to handle an overlay. */
3742 if (overlay_debugging == ovly_off
3743 || bl->section == NULL
3744 || !(section_is_overlay (bl->section)))
3745 {
3746 /* No overlay handling: just remove the breakpoint. */
3747
3748 /* If we're trying to uninsert a memory breakpoint that we
3749 know is set in a dynamic object that is marked
3750 shlib_disabled, then either the dynamic object was
3751 removed with "remove-symbol-file" or with
3752 "nosharedlibrary". In the former case, we don't know
3753 whether another dynamic object might have loaded over the
3754 breakpoint's address -- the user might well let us know
3755 about it next with add-symbol-file (the whole point of
3756 add-symbol-file is letting the user manually maintain a
3757 list of dynamically loaded objects). If we have the
3758 breakpoint's shadow memory, that is, this is a software
3759 breakpoint managed by GDB, check whether the breakpoint
3760 is still inserted in memory, to avoid overwriting wrong
3761 code with stale saved shadow contents. Note that HW
3762 breakpoints don't have shadow memory, as they're
3763 implemented using a mechanism that is not dependent on
3764 being able to modify the target's memory, and as such
3765 they should always be removed. */
3766 if (bl->shlib_disabled
3767 && bl->target_info.shadow_len != 0
3768 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3769 val = 0;
3770 else
3771 val = bl->owner->ops->remove_location (bl, reason);
3772 }
3773 else
3774 {
3775 /* This breakpoint is in an overlay section.
3776 Did we set a breakpoint at the LMA? */
3777 if (!overlay_events_enabled)
3778 {
3779 /* Yes -- overlay event support is not active, so we
3780 should have set a breakpoint at the LMA. Remove it.
3781 */
3782 /* Ignore any failures: if the LMA is in ROM, we will
3783 have already warned when we failed to insert it. */
3784 if (bl->loc_type == bp_loc_hardware_breakpoint)
3785 target_remove_hw_breakpoint (bl->gdbarch,
3786 &bl->overlay_target_info);
3787 else
3788 target_remove_breakpoint (bl->gdbarch,
3789 &bl->overlay_target_info,
3790 reason);
3791 }
3792 /* Did we set a breakpoint at the VMA?
3793 If so, we will have marked the breakpoint 'inserted'. */
3794 if (bl->inserted)
3795 {
3796 /* Yes -- remove it. Previously we did not bother to
3797 remove the breakpoint if the section had been
3798 unmapped, but let's not rely on that being safe. We
3799 don't know what the overlay manager might do. */
3800
3801 /* However, we should remove *software* breakpoints only
3802 if the section is still mapped, or else we overwrite
3803 wrong code with the saved shadow contents. */
3804 if (bl->loc_type == bp_loc_hardware_breakpoint
3805 || section_is_mapped (bl->section))
3806 val = bl->owner->ops->remove_location (bl, reason);
3807 else
3808 val = 0;
3809 }
3810 else
3811 {
3812 /* No -- not inserted, so no need to remove. No error. */
3813 val = 0;
3814 }
3815 }
3816
3817 /* In some cases, we might not be able to remove a breakpoint in
3818 a shared library that has already been removed, but we have
3819 not yet processed the shlib unload event. Similarly for an
3820 unloaded add-symbol-file object - the user might not yet have
3821 had the chance to remove-symbol-file it. shlib_disabled will
3822 be set if the library/object has already been removed, but
3823 the breakpoint hasn't been uninserted yet, e.g., after
3824 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3825 always-inserted mode. */
3826 if (val
3827 && (bl->loc_type == bp_loc_software_breakpoint
3828 && (bl->shlib_disabled
3829 || solib_name_from_address (bl->pspace, bl->address)
3830 || shared_objfile_contains_address_p (bl->pspace,
3831 bl->address))))
3832 val = 0;
3833
3834 if (val)
3835 return val;
3836 bl->inserted = (reason == DETACH_BREAKPOINT);
3837 }
3838 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3839 {
3840 gdb_assert (bl->owner->ops != NULL
3841 && bl->owner->ops->remove_location != NULL);
3842
3843 bl->inserted = (reason == DETACH_BREAKPOINT);
3844 bl->owner->ops->remove_location (bl, reason);
3845
3846 /* Failure to remove any of the hardware watchpoints comes here. */
3847 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3848 warning (_("Could not remove hardware watchpoint %d."),
3849 bl->owner->number);
3850 }
3851 else if (bl->owner->type == bp_catchpoint
3852 && breakpoint_enabled (bl->owner)
3853 && !bl->duplicate)
3854 {
3855 gdb_assert (bl->owner->ops != NULL
3856 && bl->owner->ops->remove_location != NULL);
3857
3858 val = bl->owner->ops->remove_location (bl, reason);
3859 if (val)
3860 return val;
3861
3862 bl->inserted = (reason == DETACH_BREAKPOINT);
3863 }
3864
3865 return 0;
3866 }
3867
3868 static int
3869 remove_breakpoint (struct bp_location *bl)
3870 {
3871 /* BL is never in moribund_locations by our callers. */
3872 gdb_assert (bl->owner != NULL);
3873
3874 /* The type of none suggests that owner is actually deleted.
3875 This should not ever happen. */
3876 gdb_assert (bl->owner->type != bp_none);
3877
3878 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3879
3880 switch_to_program_space_and_thread (bl->pspace);
3881
3882 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3883 }
3884
3885 /* Clear the "inserted" flag in all breakpoints. */
3886
3887 void
3888 mark_breakpoints_out (void)
3889 {
3890 struct bp_location *bl, **blp_tmp;
3891
3892 ALL_BP_LOCATIONS (bl, blp_tmp)
3893 if (bl->pspace == current_program_space)
3894 bl->inserted = 0;
3895 }
3896
3897 /* Clear the "inserted" flag in all breakpoints and delete any
3898 breakpoints which should go away between runs of the program.
3899
3900 Plus other such housekeeping that has to be done for breakpoints
3901 between runs.
3902
3903 Note: this function gets called at the end of a run (by
3904 generic_mourn_inferior) and when a run begins (by
3905 init_wait_for_inferior). */
3906
3907
3908
3909 void
3910 breakpoint_init_inferior (enum inf_context context)
3911 {
3912 struct breakpoint *b, *b_tmp;
3913 struct bp_location *bl;
3914 int ix;
3915 struct program_space *pspace = current_program_space;
3916
3917 /* If breakpoint locations are shared across processes, then there's
3918 nothing to do. */
3919 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3920 return;
3921
3922 mark_breakpoints_out ();
3923
3924 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3925 {
3926 if (b->loc && b->loc->pspace != pspace)
3927 continue;
3928
3929 switch (b->type)
3930 {
3931 case bp_call_dummy:
3932 case bp_longjmp_call_dummy:
3933
3934 /* If the call dummy breakpoint is at the entry point it will
3935 cause problems when the inferior is rerun, so we better get
3936 rid of it. */
3937
3938 case bp_watchpoint_scope:
3939
3940 /* Also get rid of scope breakpoints. */
3941
3942 case bp_shlib_event:
3943
3944 /* Also remove solib event breakpoints. Their addresses may
3945 have changed since the last time we ran the program.
3946 Actually we may now be debugging against different target;
3947 and so the solib backend that installed this breakpoint may
3948 not be used in by the target. E.g.,
3949
3950 (gdb) file prog-linux
3951 (gdb) run # native linux target
3952 ...
3953 (gdb) kill
3954 (gdb) file prog-win.exe
3955 (gdb) tar rem :9999 # remote Windows gdbserver.
3956 */
3957
3958 case bp_step_resume:
3959
3960 /* Also remove step-resume breakpoints. */
3961
3962 case bp_single_step:
3963
3964 /* Also remove single-step breakpoints. */
3965
3966 delete_breakpoint (b);
3967 break;
3968
3969 case bp_watchpoint:
3970 case bp_hardware_watchpoint:
3971 case bp_read_watchpoint:
3972 case bp_access_watchpoint:
3973 {
3974 struct watchpoint *w = (struct watchpoint *) b;
3975
3976 /* Likewise for watchpoints on local expressions. */
3977 if (w->exp_valid_block != NULL)
3978 delete_breakpoint (b);
3979 else
3980 {
3981 /* Get rid of existing locations, which are no longer
3982 valid. New ones will be created in
3983 update_watchpoint, when the inferior is restarted.
3984 The next update_global_location_list call will
3985 garbage collect them. */
3986 b->loc = NULL;
3987
3988 if (context == inf_starting)
3989 {
3990 /* Reset val field to force reread of starting value in
3991 insert_breakpoints. */
3992 if (w->val)
3993 value_free (w->val);
3994 w->val = NULL;
3995 w->val_valid = 0;
3996 }
3997 }
3998 }
3999 break;
4000 default:
4001 break;
4002 }
4003 }
4004
4005 /* Get rid of the moribund locations. */
4006 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4007 decref_bp_location (&bl);
4008 VEC_free (bp_location_p, moribund_locations);
4009 }
4010
4011 /* These functions concern about actual breakpoints inserted in the
4012 target --- to e.g. check if we need to do decr_pc adjustment or if
4013 we need to hop over the bkpt --- so we check for address space
4014 match, not program space. */
4015
4016 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4017 exists at PC. It returns ordinary_breakpoint_here if it's an
4018 ordinary breakpoint, or permanent_breakpoint_here if it's a
4019 permanent breakpoint.
4020 - When continuing from a location with an ordinary breakpoint, we
4021 actually single step once before calling insert_breakpoints.
4022 - When continuing from a location with a permanent breakpoint, we
4023 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4024 the target, to advance the PC past the breakpoint. */
4025
4026 enum breakpoint_here
4027 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4028 {
4029 struct bp_location *bl, **blp_tmp;
4030 int any_breakpoint_here = 0;
4031
4032 ALL_BP_LOCATIONS (bl, blp_tmp)
4033 {
4034 if (bl->loc_type != bp_loc_software_breakpoint
4035 && bl->loc_type != bp_loc_hardware_breakpoint)
4036 continue;
4037
4038 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4039 if ((breakpoint_enabled (bl->owner)
4040 || bl->permanent)
4041 && breakpoint_location_address_match (bl, aspace, pc))
4042 {
4043 if (overlay_debugging
4044 && section_is_overlay (bl->section)
4045 && !section_is_mapped (bl->section))
4046 continue; /* unmapped overlay -- can't be a match */
4047 else if (bl->permanent)
4048 return permanent_breakpoint_here;
4049 else
4050 any_breakpoint_here = 1;
4051 }
4052 }
4053
4054 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4055 }
4056
4057 /* See breakpoint.h. */
4058
4059 int
4060 breakpoint_in_range_p (const address_space *aspace,
4061 CORE_ADDR addr, ULONGEST len)
4062 {
4063 struct bp_location *bl, **blp_tmp;
4064
4065 ALL_BP_LOCATIONS (bl, blp_tmp)
4066 {
4067 if (bl->loc_type != bp_loc_software_breakpoint
4068 && bl->loc_type != bp_loc_hardware_breakpoint)
4069 continue;
4070
4071 if ((breakpoint_enabled (bl->owner)
4072 || bl->permanent)
4073 && breakpoint_location_address_range_overlap (bl, aspace,
4074 addr, len))
4075 {
4076 if (overlay_debugging
4077 && section_is_overlay (bl->section)
4078 && !section_is_mapped (bl->section))
4079 {
4080 /* Unmapped overlay -- can't be a match. */
4081 continue;
4082 }
4083
4084 return 1;
4085 }
4086 }
4087
4088 return 0;
4089 }
4090
4091 /* Return true if there's a moribund breakpoint at PC. */
4092
4093 int
4094 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4095 {
4096 struct bp_location *loc;
4097 int ix;
4098
4099 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4100 if (breakpoint_location_address_match (loc, aspace, pc))
4101 return 1;
4102
4103 return 0;
4104 }
4105
4106 /* Returns non-zero iff BL is inserted at PC, in address space
4107 ASPACE. */
4108
4109 static int
4110 bp_location_inserted_here_p (struct bp_location *bl,
4111 const address_space *aspace, CORE_ADDR pc)
4112 {
4113 if (bl->inserted
4114 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4115 aspace, pc))
4116 {
4117 if (overlay_debugging
4118 && section_is_overlay (bl->section)
4119 && !section_is_mapped (bl->section))
4120 return 0; /* unmapped overlay -- can't be a match */
4121 else
4122 return 1;
4123 }
4124 return 0;
4125 }
4126
4127 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4128
4129 int
4130 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4131 {
4132 struct bp_location **blp, **blp_tmp = NULL;
4133
4134 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4135 {
4136 struct bp_location *bl = *blp;
4137
4138 if (bl->loc_type != bp_loc_software_breakpoint
4139 && bl->loc_type != bp_loc_hardware_breakpoint)
4140 continue;
4141
4142 if (bp_location_inserted_here_p (bl, aspace, pc))
4143 return 1;
4144 }
4145 return 0;
4146 }
4147
4148 /* This function returns non-zero iff there is a software breakpoint
4149 inserted at PC. */
4150
4151 int
4152 software_breakpoint_inserted_here_p (const address_space *aspace,
4153 CORE_ADDR pc)
4154 {
4155 struct bp_location **blp, **blp_tmp = NULL;
4156
4157 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4158 {
4159 struct bp_location *bl = *blp;
4160
4161 if (bl->loc_type != bp_loc_software_breakpoint)
4162 continue;
4163
4164 if (bp_location_inserted_here_p (bl, aspace, pc))
4165 return 1;
4166 }
4167
4168 return 0;
4169 }
4170
4171 /* See breakpoint.h. */
4172
4173 int
4174 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4175 CORE_ADDR pc)
4176 {
4177 struct bp_location **blp, **blp_tmp = NULL;
4178
4179 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4180 {
4181 struct bp_location *bl = *blp;
4182
4183 if (bl->loc_type != bp_loc_hardware_breakpoint)
4184 continue;
4185
4186 if (bp_location_inserted_here_p (bl, aspace, pc))
4187 return 1;
4188 }
4189
4190 return 0;
4191 }
4192
4193 int
4194 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4195 CORE_ADDR addr, ULONGEST len)
4196 {
4197 struct breakpoint *bpt;
4198
4199 ALL_BREAKPOINTS (bpt)
4200 {
4201 struct bp_location *loc;
4202
4203 if (bpt->type != bp_hardware_watchpoint
4204 && bpt->type != bp_access_watchpoint)
4205 continue;
4206
4207 if (!breakpoint_enabled (bpt))
4208 continue;
4209
4210 for (loc = bpt->loc; loc; loc = loc->next)
4211 if (loc->pspace->aspace == aspace && loc->inserted)
4212 {
4213 CORE_ADDR l, h;
4214
4215 /* Check for intersection. */
4216 l = std::max<CORE_ADDR> (loc->address, addr);
4217 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4218 if (l < h)
4219 return 1;
4220 }
4221 }
4222 return 0;
4223 }
4224 \f
4225
4226 /* bpstat stuff. External routines' interfaces are documented
4227 in breakpoint.h. */
4228
4229 int
4230 is_catchpoint (struct breakpoint *ep)
4231 {
4232 return (ep->type == bp_catchpoint);
4233 }
4234
4235 /* Frees any storage that is part of a bpstat. Does not walk the
4236 'next' chain. */
4237
4238 bpstats::~bpstats ()
4239 {
4240 if (old_val != NULL)
4241 value_free (old_val);
4242 if (bp_location_at != NULL)
4243 decref_bp_location (&bp_location_at);
4244 }
4245
4246 /* Clear a bpstat so that it says we are not at any breakpoint.
4247 Also free any storage that is part of a bpstat. */
4248
4249 void
4250 bpstat_clear (bpstat *bsp)
4251 {
4252 bpstat p;
4253 bpstat q;
4254
4255 if (bsp == 0)
4256 return;
4257 p = *bsp;
4258 while (p != NULL)
4259 {
4260 q = p->next;
4261 delete p;
4262 p = q;
4263 }
4264 *bsp = NULL;
4265 }
4266
4267 bpstats::bpstats (const bpstats &other)
4268 : next (NULL),
4269 bp_location_at (other.bp_location_at),
4270 breakpoint_at (other.breakpoint_at),
4271 commands (other.commands),
4272 old_val (other.old_val),
4273 print (other.print),
4274 stop (other.stop),
4275 print_it (other.print_it)
4276 {
4277 if (old_val != NULL)
4278 {
4279 old_val = value_copy (old_val);
4280 release_value (old_val);
4281 }
4282 incref_bp_location (bp_location_at);
4283 }
4284
4285 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4286 is part of the bpstat is copied as well. */
4287
4288 bpstat
4289 bpstat_copy (bpstat bs)
4290 {
4291 bpstat p = NULL;
4292 bpstat tmp;
4293 bpstat retval = NULL;
4294
4295 if (bs == NULL)
4296 return bs;
4297
4298 for (; bs != NULL; bs = bs->next)
4299 {
4300 tmp = new bpstats (*bs);
4301
4302 if (p == NULL)
4303 /* This is the first thing in the chain. */
4304 retval = tmp;
4305 else
4306 p->next = tmp;
4307 p = tmp;
4308 }
4309 p->next = NULL;
4310 return retval;
4311 }
4312
4313 /* Find the bpstat associated with this breakpoint. */
4314
4315 bpstat
4316 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4317 {
4318 if (bsp == NULL)
4319 return NULL;
4320
4321 for (; bsp != NULL; bsp = bsp->next)
4322 {
4323 if (bsp->breakpoint_at == breakpoint)
4324 return bsp;
4325 }
4326 return NULL;
4327 }
4328
4329 /* See breakpoint.h. */
4330
4331 int
4332 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4333 {
4334 for (; bsp != NULL; bsp = bsp->next)
4335 {
4336 if (bsp->breakpoint_at == NULL)
4337 {
4338 /* A moribund location can never explain a signal other than
4339 GDB_SIGNAL_TRAP. */
4340 if (sig == GDB_SIGNAL_TRAP)
4341 return 1;
4342 }
4343 else
4344 {
4345 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4346 sig))
4347 return 1;
4348 }
4349 }
4350
4351 return 0;
4352 }
4353
4354 /* Put in *NUM the breakpoint number of the first breakpoint we are
4355 stopped at. *BSP upon return is a bpstat which points to the
4356 remaining breakpoints stopped at (but which is not guaranteed to be
4357 good for anything but further calls to bpstat_num).
4358
4359 Return 0 if passed a bpstat which does not indicate any breakpoints.
4360 Return -1 if stopped at a breakpoint that has been deleted since
4361 we set it.
4362 Return 1 otherwise. */
4363
4364 int
4365 bpstat_num (bpstat *bsp, int *num)
4366 {
4367 struct breakpoint *b;
4368
4369 if ((*bsp) == NULL)
4370 return 0; /* No more breakpoint values */
4371
4372 /* We assume we'll never have several bpstats that correspond to a
4373 single breakpoint -- otherwise, this function might return the
4374 same number more than once and this will look ugly. */
4375 b = (*bsp)->breakpoint_at;
4376 *bsp = (*bsp)->next;
4377 if (b == NULL)
4378 return -1; /* breakpoint that's been deleted since */
4379
4380 *num = b->number; /* We have its number */
4381 return 1;
4382 }
4383
4384 /* See breakpoint.h. */
4385
4386 void
4387 bpstat_clear_actions (void)
4388 {
4389 struct thread_info *tp;
4390 bpstat bs;
4391
4392 if (ptid_equal (inferior_ptid, null_ptid))
4393 return;
4394
4395 tp = find_thread_ptid (inferior_ptid);
4396 if (tp == NULL)
4397 return;
4398
4399 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4400 {
4401 bs->commands = NULL;
4402
4403 if (bs->old_val != NULL)
4404 {
4405 value_free (bs->old_val);
4406 bs->old_val = NULL;
4407 }
4408 }
4409 }
4410
4411 /* Called when a command is about to proceed the inferior. */
4412
4413 static void
4414 breakpoint_about_to_proceed (void)
4415 {
4416 if (!ptid_equal (inferior_ptid, null_ptid))
4417 {
4418 struct thread_info *tp = inferior_thread ();
4419
4420 /* Allow inferior function calls in breakpoint commands to not
4421 interrupt the command list. When the call finishes
4422 successfully, the inferior will be standing at the same
4423 breakpoint as if nothing happened. */
4424 if (tp->control.in_infcall)
4425 return;
4426 }
4427
4428 breakpoint_proceeded = 1;
4429 }
4430
4431 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4432 or its equivalent. */
4433
4434 static int
4435 command_line_is_silent (struct command_line *cmd)
4436 {
4437 return cmd && (strcmp ("silent", cmd->line) == 0);
4438 }
4439
4440 /* Execute all the commands associated with all the breakpoints at
4441 this location. Any of these commands could cause the process to
4442 proceed beyond this point, etc. We look out for such changes by
4443 checking the global "breakpoint_proceeded" after each command.
4444
4445 Returns true if a breakpoint command resumed the inferior. In that
4446 case, it is the caller's responsibility to recall it again with the
4447 bpstat of the current thread. */
4448
4449 static int
4450 bpstat_do_actions_1 (bpstat *bsp)
4451 {
4452 bpstat bs;
4453 int again = 0;
4454
4455 /* Avoid endless recursion if a `source' command is contained
4456 in bs->commands. */
4457 if (executing_breakpoint_commands)
4458 return 0;
4459
4460 scoped_restore save_executing
4461 = make_scoped_restore (&executing_breakpoint_commands, 1);
4462
4463 scoped_restore preventer = prevent_dont_repeat ();
4464
4465 /* This pointer will iterate over the list of bpstat's. */
4466 bs = *bsp;
4467
4468 breakpoint_proceeded = 0;
4469 for (; bs != NULL; bs = bs->next)
4470 {
4471 struct command_line *cmd = NULL;
4472
4473 /* Take ownership of the BSP's command tree, if it has one.
4474
4475 The command tree could legitimately contain commands like
4476 'step' and 'next', which call clear_proceed_status, which
4477 frees stop_bpstat's command tree. To make sure this doesn't
4478 free the tree we're executing out from under us, we need to
4479 take ownership of the tree ourselves. Since a given bpstat's
4480 commands are only executed once, we don't need to copy it; we
4481 can clear the pointer in the bpstat, and make sure we free
4482 the tree when we're done. */
4483 counted_command_line ccmd = bs->commands;
4484 bs->commands = NULL;
4485 if (ccmd != NULL)
4486 cmd = ccmd.get ();
4487 if (command_line_is_silent (cmd))
4488 {
4489 /* The action has been already done by bpstat_stop_status. */
4490 cmd = cmd->next;
4491 }
4492
4493 while (cmd != NULL)
4494 {
4495 execute_control_command (cmd);
4496
4497 if (breakpoint_proceeded)
4498 break;
4499 else
4500 cmd = cmd->next;
4501 }
4502
4503 if (breakpoint_proceeded)
4504 {
4505 if (current_ui->async)
4506 /* If we are in async mode, then the target might be still
4507 running, not stopped at any breakpoint, so nothing for
4508 us to do here -- just return to the event loop. */
4509 ;
4510 else
4511 /* In sync mode, when execute_control_command returns
4512 we're already standing on the next breakpoint.
4513 Breakpoint commands for that stop were not run, since
4514 execute_command does not run breakpoint commands --
4515 only command_line_handler does, but that one is not
4516 involved in execution of breakpoint commands. So, we
4517 can now execute breakpoint commands. It should be
4518 noted that making execute_command do bpstat actions is
4519 not an option -- in this case we'll have recursive
4520 invocation of bpstat for each breakpoint with a
4521 command, and can easily blow up GDB stack. Instead, we
4522 return true, which will trigger the caller to recall us
4523 with the new stop_bpstat. */
4524 again = 1;
4525 break;
4526 }
4527 }
4528 return again;
4529 }
4530
4531 void
4532 bpstat_do_actions (void)
4533 {
4534 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4535
4536 /* Do any commands attached to breakpoint we are stopped at. */
4537 while (!ptid_equal (inferior_ptid, null_ptid)
4538 && target_has_execution
4539 && !is_exited (inferior_ptid)
4540 && !is_executing (inferior_ptid))
4541 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4542 and only return when it is stopped at the next breakpoint, we
4543 keep doing breakpoint actions until it returns false to
4544 indicate the inferior was not resumed. */
4545 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4546 break;
4547
4548 discard_cleanups (cleanup_if_error);
4549 }
4550
4551 /* Print out the (old or new) value associated with a watchpoint. */
4552
4553 static void
4554 watchpoint_value_print (struct value *val, struct ui_file *stream)
4555 {
4556 if (val == NULL)
4557 fprintf_unfiltered (stream, _("<unreadable>"));
4558 else
4559 {
4560 struct value_print_options opts;
4561 get_user_print_options (&opts);
4562 value_print (val, stream, &opts);
4563 }
4564 }
4565
4566 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4567 debugging multiple threads. */
4568
4569 void
4570 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4571 {
4572 if (uiout->is_mi_like_p ())
4573 return;
4574
4575 uiout->text ("\n");
4576
4577 if (show_thread_that_caused_stop ())
4578 {
4579 const char *name;
4580 struct thread_info *thr = inferior_thread ();
4581
4582 uiout->text ("Thread ");
4583 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4584
4585 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4586 if (name != NULL)
4587 {
4588 uiout->text (" \"");
4589 uiout->field_fmt ("name", "%s", name);
4590 uiout->text ("\"");
4591 }
4592
4593 uiout->text (" hit ");
4594 }
4595 }
4596
4597 /* Generic routine for printing messages indicating why we
4598 stopped. The behavior of this function depends on the value
4599 'print_it' in the bpstat structure. Under some circumstances we
4600 may decide not to print anything here and delegate the task to
4601 normal_stop(). */
4602
4603 static enum print_stop_action
4604 print_bp_stop_message (bpstat bs)
4605 {
4606 switch (bs->print_it)
4607 {
4608 case print_it_noop:
4609 /* Nothing should be printed for this bpstat entry. */
4610 return PRINT_UNKNOWN;
4611 break;
4612
4613 case print_it_done:
4614 /* We still want to print the frame, but we already printed the
4615 relevant messages. */
4616 return PRINT_SRC_AND_LOC;
4617 break;
4618
4619 case print_it_normal:
4620 {
4621 struct breakpoint *b = bs->breakpoint_at;
4622
4623 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4624 which has since been deleted. */
4625 if (b == NULL)
4626 return PRINT_UNKNOWN;
4627
4628 /* Normal case. Call the breakpoint's print_it method. */
4629 return b->ops->print_it (bs);
4630 }
4631 break;
4632
4633 default:
4634 internal_error (__FILE__, __LINE__,
4635 _("print_bp_stop_message: unrecognized enum value"));
4636 break;
4637 }
4638 }
4639
4640 /* A helper function that prints a shared library stopped event. */
4641
4642 static void
4643 print_solib_event (int is_catchpoint)
4644 {
4645 int any_deleted
4646 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4647 int any_added
4648 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4649
4650 if (!is_catchpoint)
4651 {
4652 if (any_added || any_deleted)
4653 current_uiout->text (_("Stopped due to shared library event:\n"));
4654 else
4655 current_uiout->text (_("Stopped due to shared library event (no "
4656 "libraries added or removed)\n"));
4657 }
4658
4659 if (current_uiout->is_mi_like_p ())
4660 current_uiout->field_string ("reason",
4661 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4662
4663 if (any_deleted)
4664 {
4665 char *name;
4666 int ix;
4667
4668 current_uiout->text (_(" Inferior unloaded "));
4669 ui_out_emit_list list_emitter (current_uiout, "removed");
4670 for (ix = 0;
4671 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4672 ix, name);
4673 ++ix)
4674 {
4675 if (ix > 0)
4676 current_uiout->text (" ");
4677 current_uiout->field_string ("library", name);
4678 current_uiout->text ("\n");
4679 }
4680 }
4681
4682 if (any_added)
4683 {
4684 struct so_list *iter;
4685 int ix;
4686
4687 current_uiout->text (_(" Inferior loaded "));
4688 ui_out_emit_list list_emitter (current_uiout, "added");
4689 for (ix = 0;
4690 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4691 ix, iter);
4692 ++ix)
4693 {
4694 if (ix > 0)
4695 current_uiout->text (" ");
4696 current_uiout->field_string ("library", iter->so_name);
4697 current_uiout->text ("\n");
4698 }
4699 }
4700 }
4701
4702 /* Print a message indicating what happened. This is called from
4703 normal_stop(). The input to this routine is the head of the bpstat
4704 list - a list of the eventpoints that caused this stop. KIND is
4705 the target_waitkind for the stopping event. This
4706 routine calls the generic print routine for printing a message
4707 about reasons for stopping. This will print (for example) the
4708 "Breakpoint n," part of the output. The return value of this
4709 routine is one of:
4710
4711 PRINT_UNKNOWN: Means we printed nothing.
4712 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4713 code to print the location. An example is
4714 "Breakpoint 1, " which should be followed by
4715 the location.
4716 PRINT_SRC_ONLY: Means we printed something, but there is no need
4717 to also print the location part of the message.
4718 An example is the catch/throw messages, which
4719 don't require a location appended to the end.
4720 PRINT_NOTHING: We have done some printing and we don't need any
4721 further info to be printed. */
4722
4723 enum print_stop_action
4724 bpstat_print (bpstat bs, int kind)
4725 {
4726 enum print_stop_action val;
4727
4728 /* Maybe another breakpoint in the chain caused us to stop.
4729 (Currently all watchpoints go on the bpstat whether hit or not.
4730 That probably could (should) be changed, provided care is taken
4731 with respect to bpstat_explains_signal). */
4732 for (; bs; bs = bs->next)
4733 {
4734 val = print_bp_stop_message (bs);
4735 if (val == PRINT_SRC_ONLY
4736 || val == PRINT_SRC_AND_LOC
4737 || val == PRINT_NOTHING)
4738 return val;
4739 }
4740
4741 /* If we had hit a shared library event breakpoint,
4742 print_bp_stop_message would print out this message. If we hit an
4743 OS-level shared library event, do the same thing. */
4744 if (kind == TARGET_WAITKIND_LOADED)
4745 {
4746 print_solib_event (0);
4747 return PRINT_NOTHING;
4748 }
4749
4750 /* We reached the end of the chain, or we got a null BS to start
4751 with and nothing was printed. */
4752 return PRINT_UNKNOWN;
4753 }
4754
4755 /* Evaluate the boolean expression EXP and return the result. */
4756
4757 static bool
4758 breakpoint_cond_eval (expression *exp)
4759 {
4760 struct value *mark = value_mark ();
4761 bool res = value_true (evaluate_expression (exp));
4762
4763 value_free_to_mark (mark);
4764 return res;
4765 }
4766
4767 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4768
4769 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4770 : next (NULL),
4771 bp_location_at (bl),
4772 breakpoint_at (bl->owner),
4773 commands (NULL),
4774 old_val (NULL),
4775 print (0),
4776 stop (0),
4777 print_it (print_it_normal)
4778 {
4779 incref_bp_location (bl);
4780 **bs_link_pointer = this;
4781 *bs_link_pointer = &next;
4782 }
4783
4784 bpstats::bpstats ()
4785 : next (NULL),
4786 bp_location_at (NULL),
4787 breakpoint_at (NULL),
4788 commands (NULL),
4789 old_val (NULL),
4790 print (0),
4791 stop (0),
4792 print_it (print_it_normal)
4793 {
4794 }
4795 \f
4796 /* The target has stopped with waitstatus WS. Check if any hardware
4797 watchpoints have triggered, according to the target. */
4798
4799 int
4800 watchpoints_triggered (struct target_waitstatus *ws)
4801 {
4802 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4803 CORE_ADDR addr;
4804 struct breakpoint *b;
4805
4806 if (!stopped_by_watchpoint)
4807 {
4808 /* We were not stopped by a watchpoint. Mark all watchpoints
4809 as not triggered. */
4810 ALL_BREAKPOINTS (b)
4811 if (is_hardware_watchpoint (b))
4812 {
4813 struct watchpoint *w = (struct watchpoint *) b;
4814
4815 w->watchpoint_triggered = watch_triggered_no;
4816 }
4817
4818 return 0;
4819 }
4820
4821 if (!target_stopped_data_address (&current_target, &addr))
4822 {
4823 /* We were stopped by a watchpoint, but we don't know where.
4824 Mark all watchpoints as unknown. */
4825 ALL_BREAKPOINTS (b)
4826 if (is_hardware_watchpoint (b))
4827 {
4828 struct watchpoint *w = (struct watchpoint *) b;
4829
4830 w->watchpoint_triggered = watch_triggered_unknown;
4831 }
4832
4833 return 1;
4834 }
4835
4836 /* The target could report the data address. Mark watchpoints
4837 affected by this data address as triggered, and all others as not
4838 triggered. */
4839
4840 ALL_BREAKPOINTS (b)
4841 if (is_hardware_watchpoint (b))
4842 {
4843 struct watchpoint *w = (struct watchpoint *) b;
4844 struct bp_location *loc;
4845
4846 w->watchpoint_triggered = watch_triggered_no;
4847 for (loc = b->loc; loc; loc = loc->next)
4848 {
4849 if (is_masked_watchpoint (b))
4850 {
4851 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4852 CORE_ADDR start = loc->address & w->hw_wp_mask;
4853
4854 if (newaddr == start)
4855 {
4856 w->watchpoint_triggered = watch_triggered_yes;
4857 break;
4858 }
4859 }
4860 /* Exact match not required. Within range is sufficient. */
4861 else if (target_watchpoint_addr_within_range (&current_target,
4862 addr, loc->address,
4863 loc->length))
4864 {
4865 w->watchpoint_triggered = watch_triggered_yes;
4866 break;
4867 }
4868 }
4869 }
4870
4871 return 1;
4872 }
4873
4874 /* Possible return values for watchpoint_check. */
4875 enum wp_check_result
4876 {
4877 /* The watchpoint has been deleted. */
4878 WP_DELETED = 1,
4879
4880 /* The value has changed. */
4881 WP_VALUE_CHANGED = 2,
4882
4883 /* The value has not changed. */
4884 WP_VALUE_NOT_CHANGED = 3,
4885
4886 /* Ignore this watchpoint, no matter if the value changed or not. */
4887 WP_IGNORE = 4,
4888 };
4889
4890 #define BP_TEMPFLAG 1
4891 #define BP_HARDWAREFLAG 2
4892
4893 /* Evaluate watchpoint condition expression and check if its value
4894 changed. */
4895
4896 static wp_check_result
4897 watchpoint_check (bpstat bs)
4898 {
4899 struct watchpoint *b;
4900 struct frame_info *fr;
4901 int within_current_scope;
4902
4903 /* BS is built from an existing struct breakpoint. */
4904 gdb_assert (bs->breakpoint_at != NULL);
4905 b = (struct watchpoint *) bs->breakpoint_at;
4906
4907 /* If this is a local watchpoint, we only want to check if the
4908 watchpoint frame is in scope if the current thread is the thread
4909 that was used to create the watchpoint. */
4910 if (!watchpoint_in_thread_scope (b))
4911 return WP_IGNORE;
4912
4913 if (b->exp_valid_block == NULL)
4914 within_current_scope = 1;
4915 else
4916 {
4917 struct frame_info *frame = get_current_frame ();
4918 struct gdbarch *frame_arch = get_frame_arch (frame);
4919 CORE_ADDR frame_pc = get_frame_pc (frame);
4920
4921 /* stack_frame_destroyed_p() returns a non-zero value if we're
4922 still in the function but the stack frame has already been
4923 invalidated. Since we can't rely on the values of local
4924 variables after the stack has been destroyed, we are treating
4925 the watchpoint in that state as `not changed' without further
4926 checking. Don't mark watchpoints as changed if the current
4927 frame is in an epilogue - even if they are in some other
4928 frame, our view of the stack is likely to be wrong and
4929 frame_find_by_id could error out. */
4930 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4931 return WP_IGNORE;
4932
4933 fr = frame_find_by_id (b->watchpoint_frame);
4934 within_current_scope = (fr != NULL);
4935
4936 /* If we've gotten confused in the unwinder, we might have
4937 returned a frame that can't describe this variable. */
4938 if (within_current_scope)
4939 {
4940 struct symbol *function;
4941
4942 function = get_frame_function (fr);
4943 if (function == NULL
4944 || !contained_in (b->exp_valid_block,
4945 SYMBOL_BLOCK_VALUE (function)))
4946 within_current_scope = 0;
4947 }
4948
4949 if (within_current_scope)
4950 /* If we end up stopping, the current frame will get selected
4951 in normal_stop. So this call to select_frame won't affect
4952 the user. */
4953 select_frame (fr);
4954 }
4955
4956 if (within_current_scope)
4957 {
4958 /* We use value_{,free_to_}mark because it could be a *long*
4959 time before we return to the command level and call
4960 free_all_values. We can't call free_all_values because we
4961 might be in the middle of evaluating a function call. */
4962
4963 int pc = 0;
4964 struct value *mark;
4965 struct value *new_val;
4966
4967 if (is_masked_watchpoint (b))
4968 /* Since we don't know the exact trigger address (from
4969 stopped_data_address), just tell the user we've triggered
4970 a mask watchpoint. */
4971 return WP_VALUE_CHANGED;
4972
4973 mark = value_mark ();
4974 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4975
4976 if (b->val_bitsize != 0)
4977 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4978
4979 /* We use value_equal_contents instead of value_equal because
4980 the latter coerces an array to a pointer, thus comparing just
4981 the address of the array instead of its contents. This is
4982 not what we want. */
4983 if ((b->val != NULL) != (new_val != NULL)
4984 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4985 {
4986 if (new_val != NULL)
4987 {
4988 release_value (new_val);
4989 value_free_to_mark (mark);
4990 }
4991 bs->old_val = b->val;
4992 b->val = new_val;
4993 b->val_valid = 1;
4994 return WP_VALUE_CHANGED;
4995 }
4996 else
4997 {
4998 /* Nothing changed. */
4999 value_free_to_mark (mark);
5000 return WP_VALUE_NOT_CHANGED;
5001 }
5002 }
5003 else
5004 {
5005 /* This seems like the only logical thing to do because
5006 if we temporarily ignored the watchpoint, then when
5007 we reenter the block in which it is valid it contains
5008 garbage (in the case of a function, it may have two
5009 garbage values, one before and one after the prologue).
5010 So we can't even detect the first assignment to it and
5011 watch after that (since the garbage may or may not equal
5012 the first value assigned). */
5013 /* We print all the stop information in
5014 breakpoint_ops->print_it, but in this case, by the time we
5015 call breakpoint_ops->print_it this bp will be deleted
5016 already. So we have no choice but print the information
5017 here. */
5018
5019 SWITCH_THRU_ALL_UIS ()
5020 {
5021 struct ui_out *uiout = current_uiout;
5022
5023 if (uiout->is_mi_like_p ())
5024 uiout->field_string
5025 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5026 uiout->text ("\nWatchpoint ");
5027 uiout->field_int ("wpnum", b->number);
5028 uiout->text (" deleted because the program has left the block in\n"
5029 "which its expression is valid.\n");
5030 }
5031
5032 /* Make sure the watchpoint's commands aren't executed. */
5033 b->commands = NULL;
5034 watchpoint_del_at_next_stop (b);
5035
5036 return WP_DELETED;
5037 }
5038 }
5039
5040 /* Return true if it looks like target has stopped due to hitting
5041 breakpoint location BL. This function does not check if we should
5042 stop, only if BL explains the stop. */
5043
5044 static int
5045 bpstat_check_location (const struct bp_location *bl,
5046 const address_space *aspace, CORE_ADDR bp_addr,
5047 const struct target_waitstatus *ws)
5048 {
5049 struct breakpoint *b = bl->owner;
5050
5051 /* BL is from an existing breakpoint. */
5052 gdb_assert (b != NULL);
5053
5054 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5055 }
5056
5057 /* Determine if the watched values have actually changed, and we
5058 should stop. If not, set BS->stop to 0. */
5059
5060 static void
5061 bpstat_check_watchpoint (bpstat bs)
5062 {
5063 const struct bp_location *bl;
5064 struct watchpoint *b;
5065
5066 /* BS is built for existing struct breakpoint. */
5067 bl = bs->bp_location_at;
5068 gdb_assert (bl != NULL);
5069 b = (struct watchpoint *) bs->breakpoint_at;
5070 gdb_assert (b != NULL);
5071
5072 {
5073 int must_check_value = 0;
5074
5075 if (b->type == bp_watchpoint)
5076 /* For a software watchpoint, we must always check the
5077 watched value. */
5078 must_check_value = 1;
5079 else if (b->watchpoint_triggered == watch_triggered_yes)
5080 /* We have a hardware watchpoint (read, write, or access)
5081 and the target earlier reported an address watched by
5082 this watchpoint. */
5083 must_check_value = 1;
5084 else if (b->watchpoint_triggered == watch_triggered_unknown
5085 && b->type == bp_hardware_watchpoint)
5086 /* We were stopped by a hardware watchpoint, but the target could
5087 not report the data address. We must check the watchpoint's
5088 value. Access and read watchpoints are out of luck; without
5089 a data address, we can't figure it out. */
5090 must_check_value = 1;
5091
5092 if (must_check_value)
5093 {
5094 wp_check_result e;
5095
5096 TRY
5097 {
5098 e = watchpoint_check (bs);
5099 }
5100 CATCH (ex, RETURN_MASK_ALL)
5101 {
5102 exception_fprintf (gdb_stderr, ex,
5103 "Error evaluating expression "
5104 "for watchpoint %d\n",
5105 b->number);
5106
5107 SWITCH_THRU_ALL_UIS ()
5108 {
5109 printf_filtered (_("Watchpoint %d deleted.\n"),
5110 b->number);
5111 }
5112 watchpoint_del_at_next_stop (b);
5113 e = WP_DELETED;
5114 }
5115 END_CATCH
5116
5117 switch (e)
5118 {
5119 case WP_DELETED:
5120 /* We've already printed what needs to be printed. */
5121 bs->print_it = print_it_done;
5122 /* Stop. */
5123 break;
5124 case WP_IGNORE:
5125 bs->print_it = print_it_noop;
5126 bs->stop = 0;
5127 break;
5128 case WP_VALUE_CHANGED:
5129 if (b->type == bp_read_watchpoint)
5130 {
5131 /* There are two cases to consider here:
5132
5133 1. We're watching the triggered memory for reads.
5134 In that case, trust the target, and always report
5135 the watchpoint hit to the user. Even though
5136 reads don't cause value changes, the value may
5137 have changed since the last time it was read, and
5138 since we're not trapping writes, we will not see
5139 those, and as such we should ignore our notion of
5140 old value.
5141
5142 2. We're watching the triggered memory for both
5143 reads and writes. There are two ways this may
5144 happen:
5145
5146 2.1. This is a target that can't break on data
5147 reads only, but can break on accesses (reads or
5148 writes), such as e.g., x86. We detect this case
5149 at the time we try to insert read watchpoints.
5150
5151 2.2. Otherwise, the target supports read
5152 watchpoints, but, the user set an access or write
5153 watchpoint watching the same memory as this read
5154 watchpoint.
5155
5156 If we're watching memory writes as well as reads,
5157 ignore watchpoint hits when we find that the
5158 value hasn't changed, as reads don't cause
5159 changes. This still gives false positives when
5160 the program writes the same value to memory as
5161 what there was already in memory (we will confuse
5162 it for a read), but it's much better than
5163 nothing. */
5164
5165 int other_write_watchpoint = 0;
5166
5167 if (bl->watchpoint_type == hw_read)
5168 {
5169 struct breakpoint *other_b;
5170
5171 ALL_BREAKPOINTS (other_b)
5172 if (other_b->type == bp_hardware_watchpoint
5173 || other_b->type == bp_access_watchpoint)
5174 {
5175 struct watchpoint *other_w =
5176 (struct watchpoint *) other_b;
5177
5178 if (other_w->watchpoint_triggered
5179 == watch_triggered_yes)
5180 {
5181 other_write_watchpoint = 1;
5182 break;
5183 }
5184 }
5185 }
5186
5187 if (other_write_watchpoint
5188 || bl->watchpoint_type == hw_access)
5189 {
5190 /* We're watching the same memory for writes,
5191 and the value changed since the last time we
5192 updated it, so this trap must be for a write.
5193 Ignore it. */
5194 bs->print_it = print_it_noop;
5195 bs->stop = 0;
5196 }
5197 }
5198 break;
5199 case WP_VALUE_NOT_CHANGED:
5200 if (b->type == bp_hardware_watchpoint
5201 || b->type == bp_watchpoint)
5202 {
5203 /* Don't stop: write watchpoints shouldn't fire if
5204 the value hasn't changed. */
5205 bs->print_it = print_it_noop;
5206 bs->stop = 0;
5207 }
5208 /* Stop. */
5209 break;
5210 default:
5211 /* Can't happen. */
5212 break;
5213 }
5214 }
5215 else /* must_check_value == 0 */
5216 {
5217 /* This is a case where some watchpoint(s) triggered, but
5218 not at the address of this watchpoint, or else no
5219 watchpoint triggered after all. So don't print
5220 anything for this watchpoint. */
5221 bs->print_it = print_it_noop;
5222 bs->stop = 0;
5223 }
5224 }
5225 }
5226
5227 /* For breakpoints that are currently marked as telling gdb to stop,
5228 check conditions (condition proper, frame, thread and ignore count)
5229 of breakpoint referred to by BS. If we should not stop for this
5230 breakpoint, set BS->stop to 0. */
5231
5232 static void
5233 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5234 {
5235 const struct bp_location *bl;
5236 struct breakpoint *b;
5237 /* Assume stop. */
5238 bool condition_result = true;
5239 struct expression *cond;
5240
5241 gdb_assert (bs->stop);
5242
5243 /* BS is built for existing struct breakpoint. */
5244 bl = bs->bp_location_at;
5245 gdb_assert (bl != NULL);
5246 b = bs->breakpoint_at;
5247 gdb_assert (b != NULL);
5248
5249 /* Even if the target evaluated the condition on its end and notified GDB, we
5250 need to do so again since GDB does not know if we stopped due to a
5251 breakpoint or a single step breakpoint. */
5252
5253 if (frame_id_p (b->frame_id)
5254 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5255 {
5256 bs->stop = 0;
5257 return;
5258 }
5259
5260 /* If this is a thread/task-specific breakpoint, don't waste cpu
5261 evaluating the condition if this isn't the specified
5262 thread/task. */
5263 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5264 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5265
5266 {
5267 bs->stop = 0;
5268 return;
5269 }
5270
5271 /* Evaluate extension language breakpoints that have a "stop" method
5272 implemented. */
5273 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5274
5275 if (is_watchpoint (b))
5276 {
5277 struct watchpoint *w = (struct watchpoint *) b;
5278
5279 cond = w->cond_exp.get ();
5280 }
5281 else
5282 cond = bl->cond.get ();
5283
5284 if (cond && b->disposition != disp_del_at_next_stop)
5285 {
5286 int within_current_scope = 1;
5287 struct watchpoint * w;
5288
5289 /* We use value_mark and value_free_to_mark because it could
5290 be a long time before we return to the command level and
5291 call free_all_values. We can't call free_all_values
5292 because we might be in the middle of evaluating a
5293 function call. */
5294 struct value *mark = value_mark ();
5295
5296 if (is_watchpoint (b))
5297 w = (struct watchpoint *) b;
5298 else
5299 w = NULL;
5300
5301 /* Need to select the frame, with all that implies so that
5302 the conditions will have the right context. Because we
5303 use the frame, we will not see an inlined function's
5304 variables when we arrive at a breakpoint at the start
5305 of the inlined function; the current frame will be the
5306 call site. */
5307 if (w == NULL || w->cond_exp_valid_block == NULL)
5308 select_frame (get_current_frame ());
5309 else
5310 {
5311 struct frame_info *frame;
5312
5313 /* For local watchpoint expressions, which particular
5314 instance of a local is being watched matters, so we
5315 keep track of the frame to evaluate the expression
5316 in. To evaluate the condition however, it doesn't
5317 really matter which instantiation of the function
5318 where the condition makes sense triggers the
5319 watchpoint. This allows an expression like "watch
5320 global if q > 10" set in `func', catch writes to
5321 global on all threads that call `func', or catch
5322 writes on all recursive calls of `func' by a single
5323 thread. We simply always evaluate the condition in
5324 the innermost frame that's executing where it makes
5325 sense to evaluate the condition. It seems
5326 intuitive. */
5327 frame = block_innermost_frame (w->cond_exp_valid_block);
5328 if (frame != NULL)
5329 select_frame (frame);
5330 else
5331 within_current_scope = 0;
5332 }
5333 if (within_current_scope)
5334 {
5335 TRY
5336 {
5337 condition_result = breakpoint_cond_eval (cond);
5338 }
5339 CATCH (ex, RETURN_MASK_ALL)
5340 {
5341 exception_fprintf (gdb_stderr, ex,
5342 "Error in testing breakpoint condition:\n");
5343 }
5344 END_CATCH
5345 }
5346 else
5347 {
5348 warning (_("Watchpoint condition cannot be tested "
5349 "in the current scope"));
5350 /* If we failed to set the right context for this
5351 watchpoint, unconditionally report it. */
5352 }
5353 /* FIXME-someday, should give breakpoint #. */
5354 value_free_to_mark (mark);
5355 }
5356
5357 if (cond && !condition_result)
5358 {
5359 bs->stop = 0;
5360 }
5361 else if (b->ignore_count > 0)
5362 {
5363 b->ignore_count--;
5364 bs->stop = 0;
5365 /* Increase the hit count even though we don't stop. */
5366 ++(b->hit_count);
5367 observer_notify_breakpoint_modified (b);
5368 }
5369 }
5370
5371 /* Returns true if we need to track moribund locations of LOC's type
5372 on the current target. */
5373
5374 static int
5375 need_moribund_for_location_type (struct bp_location *loc)
5376 {
5377 return ((loc->loc_type == bp_loc_software_breakpoint
5378 && !target_supports_stopped_by_sw_breakpoint ())
5379 || (loc->loc_type == bp_loc_hardware_breakpoint
5380 && !target_supports_stopped_by_hw_breakpoint ()));
5381 }
5382
5383
5384 /* Get a bpstat associated with having just stopped at address
5385 BP_ADDR in thread PTID.
5386
5387 Determine whether we stopped at a breakpoint, etc, or whether we
5388 don't understand this stop. Result is a chain of bpstat's such
5389 that:
5390
5391 if we don't understand the stop, the result is a null pointer.
5392
5393 if we understand why we stopped, the result is not null.
5394
5395 Each element of the chain refers to a particular breakpoint or
5396 watchpoint at which we have stopped. (We may have stopped for
5397 several reasons concurrently.)
5398
5399 Each element of the chain has valid next, breakpoint_at,
5400 commands, FIXME??? fields. */
5401
5402 bpstat
5403 bpstat_stop_status (const address_space *aspace,
5404 CORE_ADDR bp_addr, ptid_t ptid,
5405 const struct target_waitstatus *ws)
5406 {
5407 struct breakpoint *b = NULL;
5408 struct bp_location *bl;
5409 struct bp_location *loc;
5410 /* First item of allocated bpstat's. */
5411 bpstat bs_head = NULL, *bs_link = &bs_head;
5412 /* Pointer to the last thing in the chain currently. */
5413 bpstat bs;
5414 int ix;
5415 int need_remove_insert;
5416 int removed_any;
5417
5418 /* First, build the bpstat chain with locations that explain a
5419 target stop, while being careful to not set the target running,
5420 as that may invalidate locations (in particular watchpoint
5421 locations are recreated). Resuming will happen here with
5422 breakpoint conditions or watchpoint expressions that include
5423 inferior function calls. */
5424
5425 ALL_BREAKPOINTS (b)
5426 {
5427 if (!breakpoint_enabled (b))
5428 continue;
5429
5430 for (bl = b->loc; bl != NULL; bl = bl->next)
5431 {
5432 /* For hardware watchpoints, we look only at the first
5433 location. The watchpoint_check function will work on the
5434 entire expression, not the individual locations. For
5435 read watchpoints, the watchpoints_triggered function has
5436 checked all locations already. */
5437 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5438 break;
5439
5440 if (!bl->enabled || bl->shlib_disabled)
5441 continue;
5442
5443 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5444 continue;
5445
5446 /* Come here if it's a watchpoint, or if the break address
5447 matches. */
5448
5449 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5450 explain stop. */
5451
5452 /* Assume we stop. Should we find a watchpoint that is not
5453 actually triggered, or if the condition of the breakpoint
5454 evaluates as false, we'll reset 'stop' to 0. */
5455 bs->stop = 1;
5456 bs->print = 1;
5457
5458 /* If this is a scope breakpoint, mark the associated
5459 watchpoint as triggered so that we will handle the
5460 out-of-scope event. We'll get to the watchpoint next
5461 iteration. */
5462 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5463 {
5464 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5465
5466 w->watchpoint_triggered = watch_triggered_yes;
5467 }
5468 }
5469 }
5470
5471 /* Check if a moribund breakpoint explains the stop. */
5472 if (!target_supports_stopped_by_sw_breakpoint ()
5473 || !target_supports_stopped_by_hw_breakpoint ())
5474 {
5475 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5476 {
5477 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5478 && need_moribund_for_location_type (loc))
5479 {
5480 bs = new bpstats (loc, &bs_link);
5481 /* For hits of moribund locations, we should just proceed. */
5482 bs->stop = 0;
5483 bs->print = 0;
5484 bs->print_it = print_it_noop;
5485 }
5486 }
5487 }
5488
5489 /* A bit of special processing for shlib breakpoints. We need to
5490 process solib loading here, so that the lists of loaded and
5491 unloaded libraries are correct before we handle "catch load" and
5492 "catch unload". */
5493 for (bs = bs_head; bs != NULL; bs = bs->next)
5494 {
5495 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5496 {
5497 handle_solib_event ();
5498 break;
5499 }
5500 }
5501
5502 /* Now go through the locations that caused the target to stop, and
5503 check whether we're interested in reporting this stop to higher
5504 layers, or whether we should resume the target transparently. */
5505
5506 removed_any = 0;
5507
5508 for (bs = bs_head; bs != NULL; bs = bs->next)
5509 {
5510 if (!bs->stop)
5511 continue;
5512
5513 b = bs->breakpoint_at;
5514 b->ops->check_status (bs);
5515 if (bs->stop)
5516 {
5517 bpstat_check_breakpoint_conditions (bs, ptid);
5518
5519 if (bs->stop)
5520 {
5521 ++(b->hit_count);
5522 observer_notify_breakpoint_modified (b);
5523
5524 /* We will stop here. */
5525 if (b->disposition == disp_disable)
5526 {
5527 --(b->enable_count);
5528 if (b->enable_count <= 0)
5529 b->enable_state = bp_disabled;
5530 removed_any = 1;
5531 }
5532 if (b->silent)
5533 bs->print = 0;
5534 bs->commands = b->commands;
5535 if (command_line_is_silent (bs->commands
5536 ? bs->commands.get () : NULL))
5537 bs->print = 0;
5538
5539 b->ops->after_condition_true (bs);
5540 }
5541
5542 }
5543
5544 /* Print nothing for this entry if we don't stop or don't
5545 print. */
5546 if (!bs->stop || !bs->print)
5547 bs->print_it = print_it_noop;
5548 }
5549
5550 /* If we aren't stopping, the value of some hardware watchpoint may
5551 not have changed, but the intermediate memory locations we are
5552 watching may have. Don't bother if we're stopping; this will get
5553 done later. */
5554 need_remove_insert = 0;
5555 if (! bpstat_causes_stop (bs_head))
5556 for (bs = bs_head; bs != NULL; bs = bs->next)
5557 if (!bs->stop
5558 && bs->breakpoint_at
5559 && is_hardware_watchpoint (bs->breakpoint_at))
5560 {
5561 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5562
5563 update_watchpoint (w, 0 /* don't reparse. */);
5564 need_remove_insert = 1;
5565 }
5566
5567 if (need_remove_insert)
5568 update_global_location_list (UGLL_MAY_INSERT);
5569 else if (removed_any)
5570 update_global_location_list (UGLL_DONT_INSERT);
5571
5572 return bs_head;
5573 }
5574
5575 static void
5576 handle_jit_event (void)
5577 {
5578 struct frame_info *frame;
5579 struct gdbarch *gdbarch;
5580
5581 if (debug_infrun)
5582 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5583
5584 /* Switch terminal for any messages produced by
5585 breakpoint_re_set. */
5586 target_terminal::ours_for_output ();
5587
5588 frame = get_current_frame ();
5589 gdbarch = get_frame_arch (frame);
5590
5591 jit_event_handler (gdbarch);
5592
5593 target_terminal::inferior ();
5594 }
5595
5596 /* Prepare WHAT final decision for infrun. */
5597
5598 /* Decide what infrun needs to do with this bpstat. */
5599
5600 struct bpstat_what
5601 bpstat_what (bpstat bs_head)
5602 {
5603 struct bpstat_what retval;
5604 bpstat bs;
5605
5606 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5607 retval.call_dummy = STOP_NONE;
5608 retval.is_longjmp = 0;
5609
5610 for (bs = bs_head; bs != NULL; bs = bs->next)
5611 {
5612 /* Extract this BS's action. After processing each BS, we check
5613 if its action overrides all we've seem so far. */
5614 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5615 enum bptype bptype;
5616
5617 if (bs->breakpoint_at == NULL)
5618 {
5619 /* I suspect this can happen if it was a momentary
5620 breakpoint which has since been deleted. */
5621 bptype = bp_none;
5622 }
5623 else
5624 bptype = bs->breakpoint_at->type;
5625
5626 switch (bptype)
5627 {
5628 case bp_none:
5629 break;
5630 case bp_breakpoint:
5631 case bp_hardware_breakpoint:
5632 case bp_single_step:
5633 case bp_until:
5634 case bp_finish:
5635 case bp_shlib_event:
5636 if (bs->stop)
5637 {
5638 if (bs->print)
5639 this_action = BPSTAT_WHAT_STOP_NOISY;
5640 else
5641 this_action = BPSTAT_WHAT_STOP_SILENT;
5642 }
5643 else
5644 this_action = BPSTAT_WHAT_SINGLE;
5645 break;
5646 case bp_watchpoint:
5647 case bp_hardware_watchpoint:
5648 case bp_read_watchpoint:
5649 case bp_access_watchpoint:
5650 if (bs->stop)
5651 {
5652 if (bs->print)
5653 this_action = BPSTAT_WHAT_STOP_NOISY;
5654 else
5655 this_action = BPSTAT_WHAT_STOP_SILENT;
5656 }
5657 else
5658 {
5659 /* There was a watchpoint, but we're not stopping.
5660 This requires no further action. */
5661 }
5662 break;
5663 case bp_longjmp:
5664 case bp_longjmp_call_dummy:
5665 case bp_exception:
5666 if (bs->stop)
5667 {
5668 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5669 retval.is_longjmp = bptype != bp_exception;
5670 }
5671 else
5672 this_action = BPSTAT_WHAT_SINGLE;
5673 break;
5674 case bp_longjmp_resume:
5675 case bp_exception_resume:
5676 if (bs->stop)
5677 {
5678 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5679 retval.is_longjmp = bptype == bp_longjmp_resume;
5680 }
5681 else
5682 this_action = BPSTAT_WHAT_SINGLE;
5683 break;
5684 case bp_step_resume:
5685 if (bs->stop)
5686 this_action = BPSTAT_WHAT_STEP_RESUME;
5687 else
5688 {
5689 /* It is for the wrong frame. */
5690 this_action = BPSTAT_WHAT_SINGLE;
5691 }
5692 break;
5693 case bp_hp_step_resume:
5694 if (bs->stop)
5695 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5696 else
5697 {
5698 /* It is for the wrong frame. */
5699 this_action = BPSTAT_WHAT_SINGLE;
5700 }
5701 break;
5702 case bp_watchpoint_scope:
5703 case bp_thread_event:
5704 case bp_overlay_event:
5705 case bp_longjmp_master:
5706 case bp_std_terminate_master:
5707 case bp_exception_master:
5708 this_action = BPSTAT_WHAT_SINGLE;
5709 break;
5710 case bp_catchpoint:
5711 if (bs->stop)
5712 {
5713 if (bs->print)
5714 this_action = BPSTAT_WHAT_STOP_NOISY;
5715 else
5716 this_action = BPSTAT_WHAT_STOP_SILENT;
5717 }
5718 else
5719 {
5720 /* There was a catchpoint, but we're not stopping.
5721 This requires no further action. */
5722 }
5723 break;
5724 case bp_jit_event:
5725 this_action = BPSTAT_WHAT_SINGLE;
5726 break;
5727 case bp_call_dummy:
5728 /* Make sure the action is stop (silent or noisy),
5729 so infrun.c pops the dummy frame. */
5730 retval.call_dummy = STOP_STACK_DUMMY;
5731 this_action = BPSTAT_WHAT_STOP_SILENT;
5732 break;
5733 case bp_std_terminate:
5734 /* Make sure the action is stop (silent or noisy),
5735 so infrun.c pops the dummy frame. */
5736 retval.call_dummy = STOP_STD_TERMINATE;
5737 this_action = BPSTAT_WHAT_STOP_SILENT;
5738 break;
5739 case bp_tracepoint:
5740 case bp_fast_tracepoint:
5741 case bp_static_tracepoint:
5742 /* Tracepoint hits should not be reported back to GDB, and
5743 if one got through somehow, it should have been filtered
5744 out already. */
5745 internal_error (__FILE__, __LINE__,
5746 _("bpstat_what: tracepoint encountered"));
5747 break;
5748 case bp_gnu_ifunc_resolver:
5749 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5750 this_action = BPSTAT_WHAT_SINGLE;
5751 break;
5752 case bp_gnu_ifunc_resolver_return:
5753 /* The breakpoint will be removed, execution will restart from the
5754 PC of the former breakpoint. */
5755 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5756 break;
5757
5758 case bp_dprintf:
5759 if (bs->stop)
5760 this_action = BPSTAT_WHAT_STOP_SILENT;
5761 else
5762 this_action = BPSTAT_WHAT_SINGLE;
5763 break;
5764
5765 default:
5766 internal_error (__FILE__, __LINE__,
5767 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5768 }
5769
5770 retval.main_action = std::max (retval.main_action, this_action);
5771 }
5772
5773 return retval;
5774 }
5775
5776 void
5777 bpstat_run_callbacks (bpstat bs_head)
5778 {
5779 bpstat bs;
5780
5781 for (bs = bs_head; bs != NULL; bs = bs->next)
5782 {
5783 struct breakpoint *b = bs->breakpoint_at;
5784
5785 if (b == NULL)
5786 continue;
5787 switch (b->type)
5788 {
5789 case bp_jit_event:
5790 handle_jit_event ();
5791 break;
5792 case bp_gnu_ifunc_resolver:
5793 gnu_ifunc_resolver_stop (b);
5794 break;
5795 case bp_gnu_ifunc_resolver_return:
5796 gnu_ifunc_resolver_return_stop (b);
5797 break;
5798 }
5799 }
5800 }
5801
5802 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5803 without hardware support). This isn't related to a specific bpstat,
5804 just to things like whether watchpoints are set. */
5805
5806 int
5807 bpstat_should_step (void)
5808 {
5809 struct breakpoint *b;
5810
5811 ALL_BREAKPOINTS (b)
5812 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5813 return 1;
5814 return 0;
5815 }
5816
5817 int
5818 bpstat_causes_stop (bpstat bs)
5819 {
5820 for (; bs != NULL; bs = bs->next)
5821 if (bs->stop)
5822 return 1;
5823
5824 return 0;
5825 }
5826
5827 \f
5828
5829 /* Compute a string of spaces suitable to indent the next line
5830 so it starts at the position corresponding to the table column
5831 named COL_NAME in the currently active table of UIOUT. */
5832
5833 static char *
5834 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5835 {
5836 static char wrap_indent[80];
5837 int i, total_width, width, align;
5838 const char *text;
5839
5840 total_width = 0;
5841 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5842 {
5843 if (strcmp (text, col_name) == 0)
5844 {
5845 gdb_assert (total_width < sizeof wrap_indent);
5846 memset (wrap_indent, ' ', total_width);
5847 wrap_indent[total_width] = 0;
5848
5849 return wrap_indent;
5850 }
5851
5852 total_width += width + 1;
5853 }
5854
5855 return NULL;
5856 }
5857
5858 /* Determine if the locations of this breakpoint will have their conditions
5859 evaluated by the target, host or a mix of both. Returns the following:
5860
5861 "host": Host evals condition.
5862 "host or target": Host or Target evals condition.
5863 "target": Target evals condition.
5864 */
5865
5866 static const char *
5867 bp_condition_evaluator (struct breakpoint *b)
5868 {
5869 struct bp_location *bl;
5870 char host_evals = 0;
5871 char target_evals = 0;
5872
5873 if (!b)
5874 return NULL;
5875
5876 if (!is_breakpoint (b))
5877 return NULL;
5878
5879 if (gdb_evaluates_breakpoint_condition_p ()
5880 || !target_supports_evaluation_of_breakpoint_conditions ())
5881 return condition_evaluation_host;
5882
5883 for (bl = b->loc; bl; bl = bl->next)
5884 {
5885 if (bl->cond_bytecode)
5886 target_evals++;
5887 else
5888 host_evals++;
5889 }
5890
5891 if (host_evals && target_evals)
5892 return condition_evaluation_both;
5893 else if (target_evals)
5894 return condition_evaluation_target;
5895 else
5896 return condition_evaluation_host;
5897 }
5898
5899 /* Determine the breakpoint location's condition evaluator. This is
5900 similar to bp_condition_evaluator, but for locations. */
5901
5902 static const char *
5903 bp_location_condition_evaluator (struct bp_location *bl)
5904 {
5905 if (bl && !is_breakpoint (bl->owner))
5906 return NULL;
5907
5908 if (gdb_evaluates_breakpoint_condition_p ()
5909 || !target_supports_evaluation_of_breakpoint_conditions ())
5910 return condition_evaluation_host;
5911
5912 if (bl && bl->cond_bytecode)
5913 return condition_evaluation_target;
5914 else
5915 return condition_evaluation_host;
5916 }
5917
5918 /* Print the LOC location out of the list of B->LOC locations. */
5919
5920 static void
5921 print_breakpoint_location (struct breakpoint *b,
5922 struct bp_location *loc)
5923 {
5924 struct ui_out *uiout = current_uiout;
5925
5926 scoped_restore_current_program_space restore_pspace;
5927
5928 if (loc != NULL && loc->shlib_disabled)
5929 loc = NULL;
5930
5931 if (loc != NULL)
5932 set_current_program_space (loc->pspace);
5933
5934 if (b->display_canonical)
5935 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5936 else if (loc && loc->symtab)
5937 {
5938 const struct symbol *sym = loc->symbol;
5939
5940 if (sym == NULL)
5941 sym = find_pc_sect_function (loc->address, loc->section);
5942
5943 if (sym)
5944 {
5945 uiout->text ("in ");
5946 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5947 uiout->text (" ");
5948 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5949 uiout->text ("at ");
5950 }
5951 uiout->field_string ("file",
5952 symtab_to_filename_for_display (loc->symtab));
5953 uiout->text (":");
5954
5955 if (uiout->is_mi_like_p ())
5956 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5957
5958 uiout->field_int ("line", loc->line_number);
5959 }
5960 else if (loc)
5961 {
5962 string_file stb;
5963
5964 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5965 demangle, "");
5966 uiout->field_stream ("at", stb);
5967 }
5968 else
5969 {
5970 uiout->field_string ("pending",
5971 event_location_to_string (b->location.get ()));
5972 /* If extra_string is available, it could be holding a condition
5973 or dprintf arguments. In either case, make sure it is printed,
5974 too, but only for non-MI streams. */
5975 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5976 {
5977 if (b->type == bp_dprintf)
5978 uiout->text (",");
5979 else
5980 uiout->text (" ");
5981 uiout->text (b->extra_string);
5982 }
5983 }
5984
5985 if (loc && is_breakpoint (b)
5986 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5987 && bp_condition_evaluator (b) == condition_evaluation_both)
5988 {
5989 uiout->text (" (");
5990 uiout->field_string ("evaluated-by",
5991 bp_location_condition_evaluator (loc));
5992 uiout->text (")");
5993 }
5994 }
5995
5996 static const char *
5997 bptype_string (enum bptype type)
5998 {
5999 struct ep_type_description
6000 {
6001 enum bptype type;
6002 const char *description;
6003 };
6004 static struct ep_type_description bptypes[] =
6005 {
6006 {bp_none, "?deleted?"},
6007 {bp_breakpoint, "breakpoint"},
6008 {bp_hardware_breakpoint, "hw breakpoint"},
6009 {bp_single_step, "sw single-step"},
6010 {bp_until, "until"},
6011 {bp_finish, "finish"},
6012 {bp_watchpoint, "watchpoint"},
6013 {bp_hardware_watchpoint, "hw watchpoint"},
6014 {bp_read_watchpoint, "read watchpoint"},
6015 {bp_access_watchpoint, "acc watchpoint"},
6016 {bp_longjmp, "longjmp"},
6017 {bp_longjmp_resume, "longjmp resume"},
6018 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6019 {bp_exception, "exception"},
6020 {bp_exception_resume, "exception resume"},
6021 {bp_step_resume, "step resume"},
6022 {bp_hp_step_resume, "high-priority step resume"},
6023 {bp_watchpoint_scope, "watchpoint scope"},
6024 {bp_call_dummy, "call dummy"},
6025 {bp_std_terminate, "std::terminate"},
6026 {bp_shlib_event, "shlib events"},
6027 {bp_thread_event, "thread events"},
6028 {bp_overlay_event, "overlay events"},
6029 {bp_longjmp_master, "longjmp master"},
6030 {bp_std_terminate_master, "std::terminate master"},
6031 {bp_exception_master, "exception master"},
6032 {bp_catchpoint, "catchpoint"},
6033 {bp_tracepoint, "tracepoint"},
6034 {bp_fast_tracepoint, "fast tracepoint"},
6035 {bp_static_tracepoint, "static tracepoint"},
6036 {bp_dprintf, "dprintf"},
6037 {bp_jit_event, "jit events"},
6038 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6039 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6040 };
6041
6042 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6043 || ((int) type != bptypes[(int) type].type))
6044 internal_error (__FILE__, __LINE__,
6045 _("bptypes table does not describe type #%d."),
6046 (int) type);
6047
6048 return bptypes[(int) type].description;
6049 }
6050
6051 /* For MI, output a field named 'thread-groups' with a list as the value.
6052 For CLI, prefix the list with the string 'inf'. */
6053
6054 static void
6055 output_thread_groups (struct ui_out *uiout,
6056 const char *field_name,
6057 VEC(int) *inf_num,
6058 int mi_only)
6059 {
6060 int is_mi = uiout->is_mi_like_p ();
6061 int inf;
6062 int i;
6063
6064 /* For backward compatibility, don't display inferiors in CLI unless
6065 there are several. Always display them for MI. */
6066 if (!is_mi && mi_only)
6067 return;
6068
6069 ui_out_emit_list list_emitter (uiout, field_name);
6070
6071 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6072 {
6073 if (is_mi)
6074 {
6075 char mi_group[10];
6076
6077 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6078 uiout->field_string (NULL, mi_group);
6079 }
6080 else
6081 {
6082 if (i == 0)
6083 uiout->text (" inf ");
6084 else
6085 uiout->text (", ");
6086
6087 uiout->text (plongest (inf));
6088 }
6089 }
6090 }
6091
6092 /* Print B to gdb_stdout. */
6093
6094 static void
6095 print_one_breakpoint_location (struct breakpoint *b,
6096 struct bp_location *loc,
6097 int loc_number,
6098 struct bp_location **last_loc,
6099 int allflag)
6100 {
6101 struct command_line *l;
6102 static char bpenables[] = "nynny";
6103
6104 struct ui_out *uiout = current_uiout;
6105 int header_of_multiple = 0;
6106 int part_of_multiple = (loc != NULL);
6107 struct value_print_options opts;
6108
6109 get_user_print_options (&opts);
6110
6111 gdb_assert (!loc || loc_number != 0);
6112 /* See comment in print_one_breakpoint concerning treatment of
6113 breakpoints with single disabled location. */
6114 if (loc == NULL
6115 && (b->loc != NULL
6116 && (b->loc->next != NULL || !b->loc->enabled)))
6117 header_of_multiple = 1;
6118 if (loc == NULL)
6119 loc = b->loc;
6120
6121 annotate_record ();
6122
6123 /* 1 */
6124 annotate_field (0);
6125 if (part_of_multiple)
6126 {
6127 char *formatted;
6128 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6129 uiout->field_string ("number", formatted);
6130 xfree (formatted);
6131 }
6132 else
6133 {
6134 uiout->field_int ("number", b->number);
6135 }
6136
6137 /* 2 */
6138 annotate_field (1);
6139 if (part_of_multiple)
6140 uiout->field_skip ("type");
6141 else
6142 uiout->field_string ("type", bptype_string (b->type));
6143
6144 /* 3 */
6145 annotate_field (2);
6146 if (part_of_multiple)
6147 uiout->field_skip ("disp");
6148 else
6149 uiout->field_string ("disp", bpdisp_text (b->disposition));
6150
6151
6152 /* 4 */
6153 annotate_field (3);
6154 if (part_of_multiple)
6155 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6156 else
6157 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6158 uiout->spaces (2);
6159
6160
6161 /* 5 and 6 */
6162 if (b->ops != NULL && b->ops->print_one != NULL)
6163 {
6164 /* Although the print_one can possibly print all locations,
6165 calling it here is not likely to get any nice result. So,
6166 make sure there's just one location. */
6167 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6168 b->ops->print_one (b, last_loc);
6169 }
6170 else
6171 switch (b->type)
6172 {
6173 case bp_none:
6174 internal_error (__FILE__, __LINE__,
6175 _("print_one_breakpoint: bp_none encountered\n"));
6176 break;
6177
6178 case bp_watchpoint:
6179 case bp_hardware_watchpoint:
6180 case bp_read_watchpoint:
6181 case bp_access_watchpoint:
6182 {
6183 struct watchpoint *w = (struct watchpoint *) b;
6184
6185 /* Field 4, the address, is omitted (which makes the columns
6186 not line up too nicely with the headers, but the effect
6187 is relatively readable). */
6188 if (opts.addressprint)
6189 uiout->field_skip ("addr");
6190 annotate_field (5);
6191 uiout->field_string ("what", w->exp_string);
6192 }
6193 break;
6194
6195 case bp_breakpoint:
6196 case bp_hardware_breakpoint:
6197 case bp_single_step:
6198 case bp_until:
6199 case bp_finish:
6200 case bp_longjmp:
6201 case bp_longjmp_resume:
6202 case bp_longjmp_call_dummy:
6203 case bp_exception:
6204 case bp_exception_resume:
6205 case bp_step_resume:
6206 case bp_hp_step_resume:
6207 case bp_watchpoint_scope:
6208 case bp_call_dummy:
6209 case bp_std_terminate:
6210 case bp_shlib_event:
6211 case bp_thread_event:
6212 case bp_overlay_event:
6213 case bp_longjmp_master:
6214 case bp_std_terminate_master:
6215 case bp_exception_master:
6216 case bp_tracepoint:
6217 case bp_fast_tracepoint:
6218 case bp_static_tracepoint:
6219 case bp_dprintf:
6220 case bp_jit_event:
6221 case bp_gnu_ifunc_resolver:
6222 case bp_gnu_ifunc_resolver_return:
6223 if (opts.addressprint)
6224 {
6225 annotate_field (4);
6226 if (header_of_multiple)
6227 uiout->field_string ("addr", "<MULTIPLE>");
6228 else if (b->loc == NULL || loc->shlib_disabled)
6229 uiout->field_string ("addr", "<PENDING>");
6230 else
6231 uiout->field_core_addr ("addr",
6232 loc->gdbarch, loc->address);
6233 }
6234 annotate_field (5);
6235 if (!header_of_multiple)
6236 print_breakpoint_location (b, loc);
6237 if (b->loc)
6238 *last_loc = b->loc;
6239 break;
6240 }
6241
6242
6243 if (loc != NULL && !header_of_multiple)
6244 {
6245 struct inferior *inf;
6246 VEC(int) *inf_num = NULL;
6247 int mi_only = 1;
6248
6249 ALL_INFERIORS (inf)
6250 {
6251 if (inf->pspace == loc->pspace)
6252 VEC_safe_push (int, inf_num, inf->num);
6253 }
6254
6255 /* For backward compatibility, don't display inferiors in CLI unless
6256 there are several. Always display for MI. */
6257 if (allflag
6258 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6259 && (number_of_program_spaces () > 1
6260 || number_of_inferiors () > 1)
6261 /* LOC is for existing B, it cannot be in
6262 moribund_locations and thus having NULL OWNER. */
6263 && loc->owner->type != bp_catchpoint))
6264 mi_only = 0;
6265 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6266 VEC_free (int, inf_num);
6267 }
6268
6269 if (!part_of_multiple)
6270 {
6271 if (b->thread != -1)
6272 {
6273 /* FIXME: This seems to be redundant and lost here; see the
6274 "stop only in" line a little further down. */
6275 uiout->text (" thread ");
6276 uiout->field_int ("thread", b->thread);
6277 }
6278 else if (b->task != 0)
6279 {
6280 uiout->text (" task ");
6281 uiout->field_int ("task", b->task);
6282 }
6283 }
6284
6285 uiout->text ("\n");
6286
6287 if (!part_of_multiple)
6288 b->ops->print_one_detail (b, uiout);
6289
6290 if (part_of_multiple && frame_id_p (b->frame_id))
6291 {
6292 annotate_field (6);
6293 uiout->text ("\tstop only in stack frame at ");
6294 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6295 the frame ID. */
6296 uiout->field_core_addr ("frame",
6297 b->gdbarch, b->frame_id.stack_addr);
6298 uiout->text ("\n");
6299 }
6300
6301 if (!part_of_multiple && b->cond_string)
6302 {
6303 annotate_field (7);
6304 if (is_tracepoint (b))
6305 uiout->text ("\ttrace only if ");
6306 else
6307 uiout->text ("\tstop only if ");
6308 uiout->field_string ("cond", b->cond_string);
6309
6310 /* Print whether the target is doing the breakpoint's condition
6311 evaluation. If GDB is doing the evaluation, don't print anything. */
6312 if (is_breakpoint (b)
6313 && breakpoint_condition_evaluation_mode ()
6314 == condition_evaluation_target)
6315 {
6316 uiout->text (" (");
6317 uiout->field_string ("evaluated-by",
6318 bp_condition_evaluator (b));
6319 uiout->text (" evals)");
6320 }
6321 uiout->text ("\n");
6322 }
6323
6324 if (!part_of_multiple && b->thread != -1)
6325 {
6326 /* FIXME should make an annotation for this. */
6327 uiout->text ("\tstop only in thread ");
6328 if (uiout->is_mi_like_p ())
6329 uiout->field_int ("thread", b->thread);
6330 else
6331 {
6332 struct thread_info *thr = find_thread_global_id (b->thread);
6333
6334 uiout->field_string ("thread", print_thread_id (thr));
6335 }
6336 uiout->text ("\n");
6337 }
6338
6339 if (!part_of_multiple)
6340 {
6341 if (b->hit_count)
6342 {
6343 /* FIXME should make an annotation for this. */
6344 if (is_catchpoint (b))
6345 uiout->text ("\tcatchpoint");
6346 else if (is_tracepoint (b))
6347 uiout->text ("\ttracepoint");
6348 else
6349 uiout->text ("\tbreakpoint");
6350 uiout->text (" already hit ");
6351 uiout->field_int ("times", b->hit_count);
6352 if (b->hit_count == 1)
6353 uiout->text (" time\n");
6354 else
6355 uiout->text (" times\n");
6356 }
6357 else
6358 {
6359 /* Output the count also if it is zero, but only if this is mi. */
6360 if (uiout->is_mi_like_p ())
6361 uiout->field_int ("times", b->hit_count);
6362 }
6363 }
6364
6365 if (!part_of_multiple && b->ignore_count)
6366 {
6367 annotate_field (8);
6368 uiout->text ("\tignore next ");
6369 uiout->field_int ("ignore", b->ignore_count);
6370 uiout->text (" hits\n");
6371 }
6372
6373 /* Note that an enable count of 1 corresponds to "enable once"
6374 behavior, which is reported by the combination of enablement and
6375 disposition, so we don't need to mention it here. */
6376 if (!part_of_multiple && b->enable_count > 1)
6377 {
6378 annotate_field (8);
6379 uiout->text ("\tdisable after ");
6380 /* Tweak the wording to clarify that ignore and enable counts
6381 are distinct, and have additive effect. */
6382 if (b->ignore_count)
6383 uiout->text ("additional ");
6384 else
6385 uiout->text ("next ");
6386 uiout->field_int ("enable", b->enable_count);
6387 uiout->text (" hits\n");
6388 }
6389
6390 if (!part_of_multiple && is_tracepoint (b))
6391 {
6392 struct tracepoint *tp = (struct tracepoint *) b;
6393
6394 if (tp->traceframe_usage)
6395 {
6396 uiout->text ("\ttrace buffer usage ");
6397 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6398 uiout->text (" bytes\n");
6399 }
6400 }
6401
6402 l = b->commands ? b->commands.get () : NULL;
6403 if (!part_of_multiple && l)
6404 {
6405 annotate_field (9);
6406 ui_out_emit_tuple tuple_emitter (uiout, "script");
6407 print_command_lines (uiout, l, 4);
6408 }
6409
6410 if (is_tracepoint (b))
6411 {
6412 struct tracepoint *t = (struct tracepoint *) b;
6413
6414 if (!part_of_multiple && t->pass_count)
6415 {
6416 annotate_field (10);
6417 uiout->text ("\tpass count ");
6418 uiout->field_int ("pass", t->pass_count);
6419 uiout->text (" \n");
6420 }
6421
6422 /* Don't display it when tracepoint or tracepoint location is
6423 pending. */
6424 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6425 {
6426 annotate_field (11);
6427
6428 if (uiout->is_mi_like_p ())
6429 uiout->field_string ("installed",
6430 loc->inserted ? "y" : "n");
6431 else
6432 {
6433 if (loc->inserted)
6434 uiout->text ("\t");
6435 else
6436 uiout->text ("\tnot ");
6437 uiout->text ("installed on target\n");
6438 }
6439 }
6440 }
6441
6442 if (uiout->is_mi_like_p () && !part_of_multiple)
6443 {
6444 if (is_watchpoint (b))
6445 {
6446 struct watchpoint *w = (struct watchpoint *) b;
6447
6448 uiout->field_string ("original-location", w->exp_string);
6449 }
6450 else if (b->location != NULL
6451 && event_location_to_string (b->location.get ()) != NULL)
6452 uiout->field_string ("original-location",
6453 event_location_to_string (b->location.get ()));
6454 }
6455 }
6456
6457 static void
6458 print_one_breakpoint (struct breakpoint *b,
6459 struct bp_location **last_loc,
6460 int allflag)
6461 {
6462 struct ui_out *uiout = current_uiout;
6463
6464 {
6465 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6466
6467 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6468 }
6469
6470 /* If this breakpoint has custom print function,
6471 it's already printed. Otherwise, print individual
6472 locations, if any. */
6473 if (b->ops == NULL || b->ops->print_one == NULL)
6474 {
6475 /* If breakpoint has a single location that is disabled, we
6476 print it as if it had several locations, since otherwise it's
6477 hard to represent "breakpoint enabled, location disabled"
6478 situation.
6479
6480 Note that while hardware watchpoints have several locations
6481 internally, that's not a property exposed to user. */
6482 if (b->loc
6483 && !is_hardware_watchpoint (b)
6484 && (b->loc->next || !b->loc->enabled))
6485 {
6486 struct bp_location *loc;
6487 int n = 1;
6488
6489 for (loc = b->loc; loc; loc = loc->next, ++n)
6490 {
6491 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6492 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6493 }
6494 }
6495 }
6496 }
6497
6498 static int
6499 breakpoint_address_bits (struct breakpoint *b)
6500 {
6501 int print_address_bits = 0;
6502 struct bp_location *loc;
6503
6504 /* Software watchpoints that aren't watching memory don't have an
6505 address to print. */
6506 if (is_no_memory_software_watchpoint (b))
6507 return 0;
6508
6509 for (loc = b->loc; loc; loc = loc->next)
6510 {
6511 int addr_bit;
6512
6513 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6514 if (addr_bit > print_address_bits)
6515 print_address_bits = addr_bit;
6516 }
6517
6518 return print_address_bits;
6519 }
6520
6521 /* See breakpoint.h. */
6522
6523 void
6524 print_breakpoint (breakpoint *b)
6525 {
6526 struct bp_location *dummy_loc = NULL;
6527 print_one_breakpoint (b, &dummy_loc, 0);
6528 }
6529
6530 /* Return true if this breakpoint was set by the user, false if it is
6531 internal or momentary. */
6532
6533 int
6534 user_breakpoint_p (struct breakpoint *b)
6535 {
6536 return b->number > 0;
6537 }
6538
6539 /* See breakpoint.h. */
6540
6541 int
6542 pending_breakpoint_p (struct breakpoint *b)
6543 {
6544 return b->loc == NULL;
6545 }
6546
6547 /* Print information on user settable breakpoint (watchpoint, etc)
6548 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6549 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6550 FILTER is non-NULL, call it on each breakpoint and only include the
6551 ones for which it returns non-zero. Return the total number of
6552 breakpoints listed. */
6553
6554 static int
6555 breakpoint_1 (const char *args, int allflag,
6556 int (*filter) (const struct breakpoint *))
6557 {
6558 struct breakpoint *b;
6559 struct bp_location *last_loc = NULL;
6560 int nr_printable_breakpoints;
6561 struct value_print_options opts;
6562 int print_address_bits = 0;
6563 int print_type_col_width = 14;
6564 struct ui_out *uiout = current_uiout;
6565
6566 get_user_print_options (&opts);
6567
6568 /* Compute the number of rows in the table, as well as the size
6569 required for address fields. */
6570 nr_printable_breakpoints = 0;
6571 ALL_BREAKPOINTS (b)
6572 {
6573 /* If we have a filter, only list the breakpoints it accepts. */
6574 if (filter && !filter (b))
6575 continue;
6576
6577 /* If we have an "args" string, it is a list of breakpoints to
6578 accept. Skip the others. */
6579 if (args != NULL && *args != '\0')
6580 {
6581 if (allflag && parse_and_eval_long (args) != b->number)
6582 continue;
6583 if (!allflag && !number_is_in_list (args, b->number))
6584 continue;
6585 }
6586
6587 if (allflag || user_breakpoint_p (b))
6588 {
6589 int addr_bit, type_len;
6590
6591 addr_bit = breakpoint_address_bits (b);
6592 if (addr_bit > print_address_bits)
6593 print_address_bits = addr_bit;
6594
6595 type_len = strlen (bptype_string (b->type));
6596 if (type_len > print_type_col_width)
6597 print_type_col_width = type_len;
6598
6599 nr_printable_breakpoints++;
6600 }
6601 }
6602
6603 {
6604 ui_out_emit_table table_emitter (uiout,
6605 opts.addressprint ? 6 : 5,
6606 nr_printable_breakpoints,
6607 "BreakpointTable");
6608
6609 if (nr_printable_breakpoints > 0)
6610 annotate_breakpoints_headers ();
6611 if (nr_printable_breakpoints > 0)
6612 annotate_field (0);
6613 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6614 if (nr_printable_breakpoints > 0)
6615 annotate_field (1);
6616 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6617 if (nr_printable_breakpoints > 0)
6618 annotate_field (2);
6619 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6620 if (nr_printable_breakpoints > 0)
6621 annotate_field (3);
6622 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6623 if (opts.addressprint)
6624 {
6625 if (nr_printable_breakpoints > 0)
6626 annotate_field (4);
6627 if (print_address_bits <= 32)
6628 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6629 else
6630 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6631 }
6632 if (nr_printable_breakpoints > 0)
6633 annotate_field (5);
6634 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6635 uiout->table_body ();
6636 if (nr_printable_breakpoints > 0)
6637 annotate_breakpoints_table ();
6638
6639 ALL_BREAKPOINTS (b)
6640 {
6641 QUIT;
6642 /* If we have a filter, only list the breakpoints it accepts. */
6643 if (filter && !filter (b))
6644 continue;
6645
6646 /* If we have an "args" string, it is a list of breakpoints to
6647 accept. Skip the others. */
6648
6649 if (args != NULL && *args != '\0')
6650 {
6651 if (allflag) /* maintenance info breakpoint */
6652 {
6653 if (parse_and_eval_long (args) != b->number)
6654 continue;
6655 }
6656 else /* all others */
6657 {
6658 if (!number_is_in_list (args, b->number))
6659 continue;
6660 }
6661 }
6662 /* We only print out user settable breakpoints unless the
6663 allflag is set. */
6664 if (allflag || user_breakpoint_p (b))
6665 print_one_breakpoint (b, &last_loc, allflag);
6666 }
6667 }
6668
6669 if (nr_printable_breakpoints == 0)
6670 {
6671 /* If there's a filter, let the caller decide how to report
6672 empty list. */
6673 if (!filter)
6674 {
6675 if (args == NULL || *args == '\0')
6676 uiout->message ("No breakpoints or watchpoints.\n");
6677 else
6678 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6679 args);
6680 }
6681 }
6682 else
6683 {
6684 if (last_loc && !server_command)
6685 set_next_address (last_loc->gdbarch, last_loc->address);
6686 }
6687
6688 /* FIXME? Should this be moved up so that it is only called when
6689 there have been breakpoints? */
6690 annotate_breakpoints_table_end ();
6691
6692 return nr_printable_breakpoints;
6693 }
6694
6695 /* Display the value of default-collect in a way that is generally
6696 compatible with the breakpoint list. */
6697
6698 static void
6699 default_collect_info (void)
6700 {
6701 struct ui_out *uiout = current_uiout;
6702
6703 /* If it has no value (which is frequently the case), say nothing; a
6704 message like "No default-collect." gets in user's face when it's
6705 not wanted. */
6706 if (!*default_collect)
6707 return;
6708
6709 /* The following phrase lines up nicely with per-tracepoint collect
6710 actions. */
6711 uiout->text ("default collect ");
6712 uiout->field_string ("default-collect", default_collect);
6713 uiout->text (" \n");
6714 }
6715
6716 static void
6717 info_breakpoints_command (char *args, int from_tty)
6718 {
6719 breakpoint_1 (args, 0, NULL);
6720
6721 default_collect_info ();
6722 }
6723
6724 static void
6725 info_watchpoints_command (char *args, int from_tty)
6726 {
6727 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6728 struct ui_out *uiout = current_uiout;
6729
6730 if (num_printed == 0)
6731 {
6732 if (args == NULL || *args == '\0')
6733 uiout->message ("No watchpoints.\n");
6734 else
6735 uiout->message ("No watchpoint matching '%s'.\n", args);
6736 }
6737 }
6738
6739 static void
6740 maintenance_info_breakpoints (const char *args, int from_tty)
6741 {
6742 breakpoint_1 (args, 1, NULL);
6743
6744 default_collect_info ();
6745 }
6746
6747 static int
6748 breakpoint_has_pc (struct breakpoint *b,
6749 struct program_space *pspace,
6750 CORE_ADDR pc, struct obj_section *section)
6751 {
6752 struct bp_location *bl = b->loc;
6753
6754 for (; bl; bl = bl->next)
6755 {
6756 if (bl->pspace == pspace
6757 && bl->address == pc
6758 && (!overlay_debugging || bl->section == section))
6759 return 1;
6760 }
6761 return 0;
6762 }
6763
6764 /* Print a message describing any user-breakpoints set at PC. This
6765 concerns with logical breakpoints, so we match program spaces, not
6766 address spaces. */
6767
6768 static void
6769 describe_other_breakpoints (struct gdbarch *gdbarch,
6770 struct program_space *pspace, CORE_ADDR pc,
6771 struct obj_section *section, int thread)
6772 {
6773 int others = 0;
6774 struct breakpoint *b;
6775
6776 ALL_BREAKPOINTS (b)
6777 others += (user_breakpoint_p (b)
6778 && breakpoint_has_pc (b, pspace, pc, section));
6779 if (others > 0)
6780 {
6781 if (others == 1)
6782 printf_filtered (_("Note: breakpoint "));
6783 else /* if (others == ???) */
6784 printf_filtered (_("Note: breakpoints "));
6785 ALL_BREAKPOINTS (b)
6786 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6787 {
6788 others--;
6789 printf_filtered ("%d", b->number);
6790 if (b->thread == -1 && thread != -1)
6791 printf_filtered (" (all threads)");
6792 else if (b->thread != -1)
6793 printf_filtered (" (thread %d)", b->thread);
6794 printf_filtered ("%s%s ",
6795 ((b->enable_state == bp_disabled
6796 || b->enable_state == bp_call_disabled)
6797 ? " (disabled)"
6798 : ""),
6799 (others > 1) ? ","
6800 : ((others == 1) ? " and" : ""));
6801 }
6802 printf_filtered (_("also set at pc "));
6803 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6804 printf_filtered (".\n");
6805 }
6806 }
6807 \f
6808
6809 /* Return true iff it is meaningful to use the address member of
6810 BPT locations. For some breakpoint types, the locations' address members
6811 are irrelevant and it makes no sense to attempt to compare them to other
6812 addresses (or use them for any other purpose either).
6813
6814 More specifically, each of the following breakpoint types will
6815 always have a zero valued location address and we don't want to mark
6816 breakpoints of any of these types to be a duplicate of an actual
6817 breakpoint location at address zero:
6818
6819 bp_watchpoint
6820 bp_catchpoint
6821
6822 */
6823
6824 static int
6825 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6826 {
6827 enum bptype type = bpt->type;
6828
6829 return (type != bp_watchpoint && type != bp_catchpoint);
6830 }
6831
6832 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6833 true if LOC1 and LOC2 represent the same watchpoint location. */
6834
6835 static int
6836 watchpoint_locations_match (struct bp_location *loc1,
6837 struct bp_location *loc2)
6838 {
6839 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6840 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6841
6842 /* Both of them must exist. */
6843 gdb_assert (w1 != NULL);
6844 gdb_assert (w2 != NULL);
6845
6846 /* If the target can evaluate the condition expression in hardware,
6847 then we we need to insert both watchpoints even if they are at
6848 the same place. Otherwise the watchpoint will only trigger when
6849 the condition of whichever watchpoint was inserted evaluates to
6850 true, not giving a chance for GDB to check the condition of the
6851 other watchpoint. */
6852 if ((w1->cond_exp
6853 && target_can_accel_watchpoint_condition (loc1->address,
6854 loc1->length,
6855 loc1->watchpoint_type,
6856 w1->cond_exp.get ()))
6857 || (w2->cond_exp
6858 && target_can_accel_watchpoint_condition (loc2->address,
6859 loc2->length,
6860 loc2->watchpoint_type,
6861 w2->cond_exp.get ())))
6862 return 0;
6863
6864 /* Note that this checks the owner's type, not the location's. In
6865 case the target does not support read watchpoints, but does
6866 support access watchpoints, we'll have bp_read_watchpoint
6867 watchpoints with hw_access locations. Those should be considered
6868 duplicates of hw_read locations. The hw_read locations will
6869 become hw_access locations later. */
6870 return (loc1->owner->type == loc2->owner->type
6871 && loc1->pspace->aspace == loc2->pspace->aspace
6872 && loc1->address == loc2->address
6873 && loc1->length == loc2->length);
6874 }
6875
6876 /* See breakpoint.h. */
6877
6878 int
6879 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6880 const address_space *aspace2, CORE_ADDR addr2)
6881 {
6882 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6883 || aspace1 == aspace2)
6884 && addr1 == addr2);
6885 }
6886
6887 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6888 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6889 matches ASPACE2. On targets that have global breakpoints, the address
6890 space doesn't really matter. */
6891
6892 static int
6893 breakpoint_address_match_range (const address_space *aspace1,
6894 CORE_ADDR addr1,
6895 int len1, const address_space *aspace2,
6896 CORE_ADDR addr2)
6897 {
6898 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6899 || aspace1 == aspace2)
6900 && addr2 >= addr1 && addr2 < addr1 + len1);
6901 }
6902
6903 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6904 a ranged breakpoint. In most targets, a match happens only if ASPACE
6905 matches the breakpoint's address space. On targets that have global
6906 breakpoints, the address space doesn't really matter. */
6907
6908 static int
6909 breakpoint_location_address_match (struct bp_location *bl,
6910 const address_space *aspace,
6911 CORE_ADDR addr)
6912 {
6913 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6914 aspace, addr)
6915 || (bl->length
6916 && breakpoint_address_match_range (bl->pspace->aspace,
6917 bl->address, bl->length,
6918 aspace, addr)));
6919 }
6920
6921 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6922 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6923 match happens only if ASPACE matches the breakpoint's address
6924 space. On targets that have global breakpoints, the address space
6925 doesn't really matter. */
6926
6927 static int
6928 breakpoint_location_address_range_overlap (struct bp_location *bl,
6929 const address_space *aspace,
6930 CORE_ADDR addr, int len)
6931 {
6932 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6933 || bl->pspace->aspace == aspace)
6934 {
6935 int bl_len = bl->length != 0 ? bl->length : 1;
6936
6937 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6938 return 1;
6939 }
6940 return 0;
6941 }
6942
6943 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6944 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6945 true, otherwise returns false. */
6946
6947 static int
6948 tracepoint_locations_match (struct bp_location *loc1,
6949 struct bp_location *loc2)
6950 {
6951 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6952 /* Since tracepoint locations are never duplicated with others', tracepoint
6953 locations at the same address of different tracepoints are regarded as
6954 different locations. */
6955 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6956 else
6957 return 0;
6958 }
6959
6960 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6961 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6962 represent the same location. */
6963
6964 static int
6965 breakpoint_locations_match (struct bp_location *loc1,
6966 struct bp_location *loc2)
6967 {
6968 int hw_point1, hw_point2;
6969
6970 /* Both of them must not be in moribund_locations. */
6971 gdb_assert (loc1->owner != NULL);
6972 gdb_assert (loc2->owner != NULL);
6973
6974 hw_point1 = is_hardware_watchpoint (loc1->owner);
6975 hw_point2 = is_hardware_watchpoint (loc2->owner);
6976
6977 if (hw_point1 != hw_point2)
6978 return 0;
6979 else if (hw_point1)
6980 return watchpoint_locations_match (loc1, loc2);
6981 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6982 return tracepoint_locations_match (loc1, loc2);
6983 else
6984 /* We compare bp_location.length in order to cover ranged breakpoints. */
6985 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6986 loc2->pspace->aspace, loc2->address)
6987 && loc1->length == loc2->length);
6988 }
6989
6990 static void
6991 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6992 int bnum, int have_bnum)
6993 {
6994 /* The longest string possibly returned by hex_string_custom
6995 is 50 chars. These must be at least that big for safety. */
6996 char astr1[64];
6997 char astr2[64];
6998
6999 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7000 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7001 if (have_bnum)
7002 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7003 bnum, astr1, astr2);
7004 else
7005 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7006 }
7007
7008 /* Adjust a breakpoint's address to account for architectural
7009 constraints on breakpoint placement. Return the adjusted address.
7010 Note: Very few targets require this kind of adjustment. For most
7011 targets, this function is simply the identity function. */
7012
7013 static CORE_ADDR
7014 adjust_breakpoint_address (struct gdbarch *gdbarch,
7015 CORE_ADDR bpaddr, enum bptype bptype)
7016 {
7017 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7018 {
7019 /* Very few targets need any kind of breakpoint adjustment. */
7020 return bpaddr;
7021 }
7022 else if (bptype == bp_watchpoint
7023 || bptype == bp_hardware_watchpoint
7024 || bptype == bp_read_watchpoint
7025 || bptype == bp_access_watchpoint
7026 || bptype == bp_catchpoint)
7027 {
7028 /* Watchpoints and the various bp_catch_* eventpoints should not
7029 have their addresses modified. */
7030 return bpaddr;
7031 }
7032 else if (bptype == bp_single_step)
7033 {
7034 /* Single-step breakpoints should not have their addresses
7035 modified. If there's any architectural constrain that
7036 applies to this address, then it should have already been
7037 taken into account when the breakpoint was created in the
7038 first place. If we didn't do this, stepping through e.g.,
7039 Thumb-2 IT blocks would break. */
7040 return bpaddr;
7041 }
7042 else
7043 {
7044 CORE_ADDR adjusted_bpaddr;
7045
7046 /* Some targets have architectural constraints on the placement
7047 of breakpoint instructions. Obtain the adjusted address. */
7048 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7049
7050 /* An adjusted breakpoint address can significantly alter
7051 a user's expectations. Print a warning if an adjustment
7052 is required. */
7053 if (adjusted_bpaddr != bpaddr)
7054 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7055
7056 return adjusted_bpaddr;
7057 }
7058 }
7059
7060 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7061 {
7062 bp_location *loc = this;
7063
7064 gdb_assert (ops != NULL);
7065
7066 loc->ops = ops;
7067 loc->owner = owner;
7068 loc->cond_bytecode = NULL;
7069 loc->shlib_disabled = 0;
7070 loc->enabled = 1;
7071
7072 switch (owner->type)
7073 {
7074 case bp_breakpoint:
7075 case bp_single_step:
7076 case bp_until:
7077 case bp_finish:
7078 case bp_longjmp:
7079 case bp_longjmp_resume:
7080 case bp_longjmp_call_dummy:
7081 case bp_exception:
7082 case bp_exception_resume:
7083 case bp_step_resume:
7084 case bp_hp_step_resume:
7085 case bp_watchpoint_scope:
7086 case bp_call_dummy:
7087 case bp_std_terminate:
7088 case bp_shlib_event:
7089 case bp_thread_event:
7090 case bp_overlay_event:
7091 case bp_jit_event:
7092 case bp_longjmp_master:
7093 case bp_std_terminate_master:
7094 case bp_exception_master:
7095 case bp_gnu_ifunc_resolver:
7096 case bp_gnu_ifunc_resolver_return:
7097 case bp_dprintf:
7098 loc->loc_type = bp_loc_software_breakpoint;
7099 mark_breakpoint_location_modified (loc);
7100 break;
7101 case bp_hardware_breakpoint:
7102 loc->loc_type = bp_loc_hardware_breakpoint;
7103 mark_breakpoint_location_modified (loc);
7104 break;
7105 case bp_hardware_watchpoint:
7106 case bp_read_watchpoint:
7107 case bp_access_watchpoint:
7108 loc->loc_type = bp_loc_hardware_watchpoint;
7109 break;
7110 case bp_watchpoint:
7111 case bp_catchpoint:
7112 case bp_tracepoint:
7113 case bp_fast_tracepoint:
7114 case bp_static_tracepoint:
7115 loc->loc_type = bp_loc_other;
7116 break;
7117 default:
7118 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7119 }
7120
7121 loc->refc = 1;
7122 }
7123
7124 /* Allocate a struct bp_location. */
7125
7126 static struct bp_location *
7127 allocate_bp_location (struct breakpoint *bpt)
7128 {
7129 return bpt->ops->allocate_location (bpt);
7130 }
7131
7132 static void
7133 free_bp_location (struct bp_location *loc)
7134 {
7135 loc->ops->dtor (loc);
7136 delete loc;
7137 }
7138
7139 /* Increment reference count. */
7140
7141 static void
7142 incref_bp_location (struct bp_location *bl)
7143 {
7144 ++bl->refc;
7145 }
7146
7147 /* Decrement reference count. If the reference count reaches 0,
7148 destroy the bp_location. Sets *BLP to NULL. */
7149
7150 static void
7151 decref_bp_location (struct bp_location **blp)
7152 {
7153 gdb_assert ((*blp)->refc > 0);
7154
7155 if (--(*blp)->refc == 0)
7156 free_bp_location (*blp);
7157 *blp = NULL;
7158 }
7159
7160 /* Add breakpoint B at the end of the global breakpoint chain. */
7161
7162 static breakpoint *
7163 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7164 {
7165 struct breakpoint *b1;
7166 struct breakpoint *result = b.get ();
7167
7168 /* Add this breakpoint to the end of the chain so that a list of
7169 breakpoints will come out in order of increasing numbers. */
7170
7171 b1 = breakpoint_chain;
7172 if (b1 == 0)
7173 breakpoint_chain = b.release ();
7174 else
7175 {
7176 while (b1->next)
7177 b1 = b1->next;
7178 b1->next = b.release ();
7179 }
7180
7181 return result;
7182 }
7183
7184 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7185
7186 static void
7187 init_raw_breakpoint_without_location (struct breakpoint *b,
7188 struct gdbarch *gdbarch,
7189 enum bptype bptype,
7190 const struct breakpoint_ops *ops)
7191 {
7192 gdb_assert (ops != NULL);
7193
7194 b->ops = ops;
7195 b->type = bptype;
7196 b->gdbarch = gdbarch;
7197 b->language = current_language->la_language;
7198 b->input_radix = input_radix;
7199 b->related_breakpoint = b;
7200 }
7201
7202 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7203 that has type BPTYPE and has no locations as yet. */
7204
7205 static struct breakpoint *
7206 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7207 enum bptype bptype,
7208 const struct breakpoint_ops *ops)
7209 {
7210 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7211
7212 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7213 return add_to_breakpoint_chain (std::move (b));
7214 }
7215
7216 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7217 resolutions should be made as the user specified the location explicitly
7218 enough. */
7219
7220 static void
7221 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7222 {
7223 gdb_assert (loc->owner != NULL);
7224
7225 if (loc->owner->type == bp_breakpoint
7226 || loc->owner->type == bp_hardware_breakpoint
7227 || is_tracepoint (loc->owner))
7228 {
7229 int is_gnu_ifunc;
7230 const char *function_name;
7231 CORE_ADDR func_addr;
7232
7233 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7234 &func_addr, NULL, &is_gnu_ifunc);
7235
7236 if (is_gnu_ifunc && !explicit_loc)
7237 {
7238 struct breakpoint *b = loc->owner;
7239
7240 gdb_assert (loc->pspace == current_program_space);
7241 if (gnu_ifunc_resolve_name (function_name,
7242 &loc->requested_address))
7243 {
7244 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7245 loc->address = adjust_breakpoint_address (loc->gdbarch,
7246 loc->requested_address,
7247 b->type);
7248 }
7249 else if (b->type == bp_breakpoint && b->loc == loc
7250 && loc->next == NULL && b->related_breakpoint == b)
7251 {
7252 /* Create only the whole new breakpoint of this type but do not
7253 mess more complicated breakpoints with multiple locations. */
7254 b->type = bp_gnu_ifunc_resolver;
7255 /* Remember the resolver's address for use by the return
7256 breakpoint. */
7257 loc->related_address = func_addr;
7258 }
7259 }
7260
7261 if (function_name)
7262 loc->function_name = xstrdup (function_name);
7263 }
7264 }
7265
7266 /* Attempt to determine architecture of location identified by SAL. */
7267 struct gdbarch *
7268 get_sal_arch (struct symtab_and_line sal)
7269 {
7270 if (sal.section)
7271 return get_objfile_arch (sal.section->objfile);
7272 if (sal.symtab)
7273 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7274
7275 return NULL;
7276 }
7277
7278 /* Low level routine for partially initializing a breakpoint of type
7279 BPTYPE. The newly created breakpoint's address, section, source
7280 file name, and line number are provided by SAL.
7281
7282 It is expected that the caller will complete the initialization of
7283 the newly created breakpoint struct as well as output any status
7284 information regarding the creation of a new breakpoint. */
7285
7286 static void
7287 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7288 struct symtab_and_line sal, enum bptype bptype,
7289 const struct breakpoint_ops *ops)
7290 {
7291 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7292
7293 add_location_to_breakpoint (b, &sal);
7294
7295 if (bptype != bp_catchpoint)
7296 gdb_assert (sal.pspace != NULL);
7297
7298 /* Store the program space that was used to set the breakpoint,
7299 except for ordinary breakpoints, which are independent of the
7300 program space. */
7301 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7302 b->pspace = sal.pspace;
7303 }
7304
7305 /* set_raw_breakpoint is a low level routine for allocating and
7306 partially initializing a breakpoint of type BPTYPE. The newly
7307 created breakpoint's address, section, source file name, and line
7308 number are provided by SAL. The newly created and partially
7309 initialized breakpoint is added to the breakpoint chain and
7310 is also returned as the value of this function.
7311
7312 It is expected that the caller will complete the initialization of
7313 the newly created breakpoint struct as well as output any status
7314 information regarding the creation of a new breakpoint. In
7315 particular, set_raw_breakpoint does NOT set the breakpoint
7316 number! Care should be taken to not allow an error to occur
7317 prior to completing the initialization of the breakpoint. If this
7318 should happen, a bogus breakpoint will be left on the chain. */
7319
7320 struct breakpoint *
7321 set_raw_breakpoint (struct gdbarch *gdbarch,
7322 struct symtab_and_line sal, enum bptype bptype,
7323 const struct breakpoint_ops *ops)
7324 {
7325 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7326
7327 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7328 return add_to_breakpoint_chain (std::move (b));
7329 }
7330
7331 /* Call this routine when stepping and nexting to enable a breakpoint
7332 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7333 initiated the operation. */
7334
7335 void
7336 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7337 {
7338 struct breakpoint *b, *b_tmp;
7339 int thread = tp->global_num;
7340
7341 /* To avoid having to rescan all objfile symbols at every step,
7342 we maintain a list of continually-inserted but always disabled
7343 longjmp "master" breakpoints. Here, we simply create momentary
7344 clones of those and enable them for the requested thread. */
7345 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7346 if (b->pspace == current_program_space
7347 && (b->type == bp_longjmp_master
7348 || b->type == bp_exception_master))
7349 {
7350 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7351 struct breakpoint *clone;
7352
7353 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7354 after their removal. */
7355 clone = momentary_breakpoint_from_master (b, type,
7356 &momentary_breakpoint_ops, 1);
7357 clone->thread = thread;
7358 }
7359
7360 tp->initiating_frame = frame;
7361 }
7362
7363 /* Delete all longjmp breakpoints from THREAD. */
7364 void
7365 delete_longjmp_breakpoint (int thread)
7366 {
7367 struct breakpoint *b, *b_tmp;
7368
7369 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7370 if (b->type == bp_longjmp || b->type == bp_exception)
7371 {
7372 if (b->thread == thread)
7373 delete_breakpoint (b);
7374 }
7375 }
7376
7377 void
7378 delete_longjmp_breakpoint_at_next_stop (int thread)
7379 {
7380 struct breakpoint *b, *b_tmp;
7381
7382 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7383 if (b->type == bp_longjmp || b->type == bp_exception)
7384 {
7385 if (b->thread == thread)
7386 b->disposition = disp_del_at_next_stop;
7387 }
7388 }
7389
7390 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7391 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7392 pointer to any of them. Return NULL if this system cannot place longjmp
7393 breakpoints. */
7394
7395 struct breakpoint *
7396 set_longjmp_breakpoint_for_call_dummy (void)
7397 {
7398 struct breakpoint *b, *retval = NULL;
7399
7400 ALL_BREAKPOINTS (b)
7401 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7402 {
7403 struct breakpoint *new_b;
7404
7405 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7406 &momentary_breakpoint_ops,
7407 1);
7408 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7409
7410 /* Link NEW_B into the chain of RETVAL breakpoints. */
7411
7412 gdb_assert (new_b->related_breakpoint == new_b);
7413 if (retval == NULL)
7414 retval = new_b;
7415 new_b->related_breakpoint = retval;
7416 while (retval->related_breakpoint != new_b->related_breakpoint)
7417 retval = retval->related_breakpoint;
7418 retval->related_breakpoint = new_b;
7419 }
7420
7421 return retval;
7422 }
7423
7424 /* Verify all existing dummy frames and their associated breakpoints for
7425 TP. Remove those which can no longer be found in the current frame
7426 stack.
7427
7428 You should call this function only at places where it is safe to currently
7429 unwind the whole stack. Failed stack unwind would discard live dummy
7430 frames. */
7431
7432 void
7433 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7434 {
7435 struct breakpoint *b, *b_tmp;
7436
7437 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7438 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7439 {
7440 struct breakpoint *dummy_b = b->related_breakpoint;
7441
7442 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7443 dummy_b = dummy_b->related_breakpoint;
7444 if (dummy_b->type != bp_call_dummy
7445 || frame_find_by_id (dummy_b->frame_id) != NULL)
7446 continue;
7447
7448 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7449
7450 while (b->related_breakpoint != b)
7451 {
7452 if (b_tmp == b->related_breakpoint)
7453 b_tmp = b->related_breakpoint->next;
7454 delete_breakpoint (b->related_breakpoint);
7455 }
7456 delete_breakpoint (b);
7457 }
7458 }
7459
7460 void
7461 enable_overlay_breakpoints (void)
7462 {
7463 struct breakpoint *b;
7464
7465 ALL_BREAKPOINTS (b)
7466 if (b->type == bp_overlay_event)
7467 {
7468 b->enable_state = bp_enabled;
7469 update_global_location_list (UGLL_MAY_INSERT);
7470 overlay_events_enabled = 1;
7471 }
7472 }
7473
7474 void
7475 disable_overlay_breakpoints (void)
7476 {
7477 struct breakpoint *b;
7478
7479 ALL_BREAKPOINTS (b)
7480 if (b->type == bp_overlay_event)
7481 {
7482 b->enable_state = bp_disabled;
7483 update_global_location_list (UGLL_DONT_INSERT);
7484 overlay_events_enabled = 0;
7485 }
7486 }
7487
7488 /* Set an active std::terminate breakpoint for each std::terminate
7489 master breakpoint. */
7490 void
7491 set_std_terminate_breakpoint (void)
7492 {
7493 struct breakpoint *b, *b_tmp;
7494
7495 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7496 if (b->pspace == current_program_space
7497 && b->type == bp_std_terminate_master)
7498 {
7499 momentary_breakpoint_from_master (b, bp_std_terminate,
7500 &momentary_breakpoint_ops, 1);
7501 }
7502 }
7503
7504 /* Delete all the std::terminate breakpoints. */
7505 void
7506 delete_std_terminate_breakpoint (void)
7507 {
7508 struct breakpoint *b, *b_tmp;
7509
7510 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7511 if (b->type == bp_std_terminate)
7512 delete_breakpoint (b);
7513 }
7514
7515 struct breakpoint *
7516 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7517 {
7518 struct breakpoint *b;
7519
7520 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7521 &internal_breakpoint_ops);
7522
7523 b->enable_state = bp_enabled;
7524 /* location has to be used or breakpoint_re_set will delete me. */
7525 b->location = new_address_location (b->loc->address, NULL, 0);
7526
7527 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7528
7529 return b;
7530 }
7531
7532 struct lang_and_radix
7533 {
7534 enum language lang;
7535 int radix;
7536 };
7537
7538 /* Create a breakpoint for JIT code registration and unregistration. */
7539
7540 struct breakpoint *
7541 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7542 {
7543 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7544 &internal_breakpoint_ops);
7545 }
7546
7547 /* Remove JIT code registration and unregistration breakpoint(s). */
7548
7549 void
7550 remove_jit_event_breakpoints (void)
7551 {
7552 struct breakpoint *b, *b_tmp;
7553
7554 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7555 if (b->type == bp_jit_event
7556 && b->loc->pspace == current_program_space)
7557 delete_breakpoint (b);
7558 }
7559
7560 void
7561 remove_solib_event_breakpoints (void)
7562 {
7563 struct breakpoint *b, *b_tmp;
7564
7565 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7566 if (b->type == bp_shlib_event
7567 && b->loc->pspace == current_program_space)
7568 delete_breakpoint (b);
7569 }
7570
7571 /* See breakpoint.h. */
7572
7573 void
7574 remove_solib_event_breakpoints_at_next_stop (void)
7575 {
7576 struct breakpoint *b, *b_tmp;
7577
7578 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7579 if (b->type == bp_shlib_event
7580 && b->loc->pspace == current_program_space)
7581 b->disposition = disp_del_at_next_stop;
7582 }
7583
7584 /* Helper for create_solib_event_breakpoint /
7585 create_and_insert_solib_event_breakpoint. Allows specifying which
7586 INSERT_MODE to pass through to update_global_location_list. */
7587
7588 static struct breakpoint *
7589 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7590 enum ugll_insert_mode insert_mode)
7591 {
7592 struct breakpoint *b;
7593
7594 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7595 &internal_breakpoint_ops);
7596 update_global_location_list_nothrow (insert_mode);
7597 return b;
7598 }
7599
7600 struct breakpoint *
7601 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7602 {
7603 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7604 }
7605
7606 /* See breakpoint.h. */
7607
7608 struct breakpoint *
7609 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7610 {
7611 struct breakpoint *b;
7612
7613 /* Explicitly tell update_global_location_list to insert
7614 locations. */
7615 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7616 if (!b->loc->inserted)
7617 {
7618 delete_breakpoint (b);
7619 return NULL;
7620 }
7621 return b;
7622 }
7623
7624 /* Disable any breakpoints that are on code in shared libraries. Only
7625 apply to enabled breakpoints, disabled ones can just stay disabled. */
7626
7627 void
7628 disable_breakpoints_in_shlibs (void)
7629 {
7630 struct bp_location *loc, **locp_tmp;
7631
7632 ALL_BP_LOCATIONS (loc, locp_tmp)
7633 {
7634 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7635 struct breakpoint *b = loc->owner;
7636
7637 /* We apply the check to all breakpoints, including disabled for
7638 those with loc->duplicate set. This is so that when breakpoint
7639 becomes enabled, or the duplicate is removed, gdb will try to
7640 insert all breakpoints. If we don't set shlib_disabled here,
7641 we'll try to insert those breakpoints and fail. */
7642 if (((b->type == bp_breakpoint)
7643 || (b->type == bp_jit_event)
7644 || (b->type == bp_hardware_breakpoint)
7645 || (is_tracepoint (b)))
7646 && loc->pspace == current_program_space
7647 && !loc->shlib_disabled
7648 && solib_name_from_address (loc->pspace, loc->address)
7649 )
7650 {
7651 loc->shlib_disabled = 1;
7652 }
7653 }
7654 }
7655
7656 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7657 notification of unloaded_shlib. Only apply to enabled breakpoints,
7658 disabled ones can just stay disabled. */
7659
7660 static void
7661 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7662 {
7663 struct bp_location *loc, **locp_tmp;
7664 int disabled_shlib_breaks = 0;
7665
7666 ALL_BP_LOCATIONS (loc, locp_tmp)
7667 {
7668 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7669 struct breakpoint *b = loc->owner;
7670
7671 if (solib->pspace == loc->pspace
7672 && !loc->shlib_disabled
7673 && (((b->type == bp_breakpoint
7674 || b->type == bp_jit_event
7675 || b->type == bp_hardware_breakpoint)
7676 && (loc->loc_type == bp_loc_hardware_breakpoint
7677 || loc->loc_type == bp_loc_software_breakpoint))
7678 || is_tracepoint (b))
7679 && solib_contains_address_p (solib, loc->address))
7680 {
7681 loc->shlib_disabled = 1;
7682 /* At this point, we cannot rely on remove_breakpoint
7683 succeeding so we must mark the breakpoint as not inserted
7684 to prevent future errors occurring in remove_breakpoints. */
7685 loc->inserted = 0;
7686
7687 /* This may cause duplicate notifications for the same breakpoint. */
7688 observer_notify_breakpoint_modified (b);
7689
7690 if (!disabled_shlib_breaks)
7691 {
7692 target_terminal::ours_for_output ();
7693 warning (_("Temporarily disabling breakpoints "
7694 "for unloaded shared library \"%s\""),
7695 solib->so_name);
7696 }
7697 disabled_shlib_breaks = 1;
7698 }
7699 }
7700 }
7701
7702 /* Disable any breakpoints and tracepoints in OBJFILE upon
7703 notification of free_objfile. Only apply to enabled breakpoints,
7704 disabled ones can just stay disabled. */
7705
7706 static void
7707 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7708 {
7709 struct breakpoint *b;
7710
7711 if (objfile == NULL)
7712 return;
7713
7714 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7715 managed by the user with add-symbol-file/remove-symbol-file.
7716 Similarly to how breakpoints in shared libraries are handled in
7717 response to "nosharedlibrary", mark breakpoints in such modules
7718 shlib_disabled so they end up uninserted on the next global
7719 location list update. Shared libraries not loaded by the user
7720 aren't handled here -- they're already handled in
7721 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7722 solib_unloaded observer. We skip objfiles that are not
7723 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7724 main objfile). */
7725 if ((objfile->flags & OBJF_SHARED) == 0
7726 || (objfile->flags & OBJF_USERLOADED) == 0)
7727 return;
7728
7729 ALL_BREAKPOINTS (b)
7730 {
7731 struct bp_location *loc;
7732 int bp_modified = 0;
7733
7734 if (!is_breakpoint (b) && !is_tracepoint (b))
7735 continue;
7736
7737 for (loc = b->loc; loc != NULL; loc = loc->next)
7738 {
7739 CORE_ADDR loc_addr = loc->address;
7740
7741 if (loc->loc_type != bp_loc_hardware_breakpoint
7742 && loc->loc_type != bp_loc_software_breakpoint)
7743 continue;
7744
7745 if (loc->shlib_disabled != 0)
7746 continue;
7747
7748 if (objfile->pspace != loc->pspace)
7749 continue;
7750
7751 if (loc->loc_type != bp_loc_hardware_breakpoint
7752 && loc->loc_type != bp_loc_software_breakpoint)
7753 continue;
7754
7755 if (is_addr_in_objfile (loc_addr, objfile))
7756 {
7757 loc->shlib_disabled = 1;
7758 /* At this point, we don't know whether the object was
7759 unmapped from the inferior or not, so leave the
7760 inserted flag alone. We'll handle failure to
7761 uninsert quietly, in case the object was indeed
7762 unmapped. */
7763
7764 mark_breakpoint_location_modified (loc);
7765
7766 bp_modified = 1;
7767 }
7768 }
7769
7770 if (bp_modified)
7771 observer_notify_breakpoint_modified (b);
7772 }
7773 }
7774
7775 /* FORK & VFORK catchpoints. */
7776
7777 /* An instance of this type is used to represent a fork or vfork
7778 catchpoint. A breakpoint is really of this type iff its ops pointer points
7779 to CATCH_FORK_BREAKPOINT_OPS. */
7780
7781 struct fork_catchpoint : public breakpoint
7782 {
7783 /* Process id of a child process whose forking triggered this
7784 catchpoint. This field is only valid immediately after this
7785 catchpoint has triggered. */
7786 ptid_t forked_inferior_pid;
7787 };
7788
7789 /* Implement the "insert" breakpoint_ops method for fork
7790 catchpoints. */
7791
7792 static int
7793 insert_catch_fork (struct bp_location *bl)
7794 {
7795 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7796 }
7797
7798 /* Implement the "remove" breakpoint_ops method for fork
7799 catchpoints. */
7800
7801 static int
7802 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7803 {
7804 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7805 }
7806
7807 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7808 catchpoints. */
7809
7810 static int
7811 breakpoint_hit_catch_fork (const struct bp_location *bl,
7812 const address_space *aspace, CORE_ADDR bp_addr,
7813 const struct target_waitstatus *ws)
7814 {
7815 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7816
7817 if (ws->kind != TARGET_WAITKIND_FORKED)
7818 return 0;
7819
7820 c->forked_inferior_pid = ws->value.related_pid;
7821 return 1;
7822 }
7823
7824 /* Implement the "print_it" breakpoint_ops method for fork
7825 catchpoints. */
7826
7827 static enum print_stop_action
7828 print_it_catch_fork (bpstat bs)
7829 {
7830 struct ui_out *uiout = current_uiout;
7831 struct breakpoint *b = bs->breakpoint_at;
7832 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7833
7834 annotate_catchpoint (b->number);
7835 maybe_print_thread_hit_breakpoint (uiout);
7836 if (b->disposition == disp_del)
7837 uiout->text ("Temporary catchpoint ");
7838 else
7839 uiout->text ("Catchpoint ");
7840 if (uiout->is_mi_like_p ())
7841 {
7842 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7843 uiout->field_string ("disp", bpdisp_text (b->disposition));
7844 }
7845 uiout->field_int ("bkptno", b->number);
7846 uiout->text (" (forked process ");
7847 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7848 uiout->text ("), ");
7849 return PRINT_SRC_AND_LOC;
7850 }
7851
7852 /* Implement the "print_one" breakpoint_ops method for fork
7853 catchpoints. */
7854
7855 static void
7856 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7857 {
7858 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7859 struct value_print_options opts;
7860 struct ui_out *uiout = current_uiout;
7861
7862 get_user_print_options (&opts);
7863
7864 /* Field 4, the address, is omitted (which makes the columns not
7865 line up too nicely with the headers, but the effect is relatively
7866 readable). */
7867 if (opts.addressprint)
7868 uiout->field_skip ("addr");
7869 annotate_field (5);
7870 uiout->text ("fork");
7871 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7872 {
7873 uiout->text (", process ");
7874 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7875 uiout->spaces (1);
7876 }
7877
7878 if (uiout->is_mi_like_p ())
7879 uiout->field_string ("catch-type", "fork");
7880 }
7881
7882 /* Implement the "print_mention" breakpoint_ops method for fork
7883 catchpoints. */
7884
7885 static void
7886 print_mention_catch_fork (struct breakpoint *b)
7887 {
7888 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7889 }
7890
7891 /* Implement the "print_recreate" breakpoint_ops method for fork
7892 catchpoints. */
7893
7894 static void
7895 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7896 {
7897 fprintf_unfiltered (fp, "catch fork");
7898 print_recreate_thread (b, fp);
7899 }
7900
7901 /* The breakpoint_ops structure to be used in fork catchpoints. */
7902
7903 static struct breakpoint_ops catch_fork_breakpoint_ops;
7904
7905 /* Implement the "insert" breakpoint_ops method for vfork
7906 catchpoints. */
7907
7908 static int
7909 insert_catch_vfork (struct bp_location *bl)
7910 {
7911 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7912 }
7913
7914 /* Implement the "remove" breakpoint_ops method for vfork
7915 catchpoints. */
7916
7917 static int
7918 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7919 {
7920 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7921 }
7922
7923 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7924 catchpoints. */
7925
7926 static int
7927 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7928 const address_space *aspace, CORE_ADDR bp_addr,
7929 const struct target_waitstatus *ws)
7930 {
7931 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7932
7933 if (ws->kind != TARGET_WAITKIND_VFORKED)
7934 return 0;
7935
7936 c->forked_inferior_pid = ws->value.related_pid;
7937 return 1;
7938 }
7939
7940 /* Implement the "print_it" breakpoint_ops method for vfork
7941 catchpoints. */
7942
7943 static enum print_stop_action
7944 print_it_catch_vfork (bpstat bs)
7945 {
7946 struct ui_out *uiout = current_uiout;
7947 struct breakpoint *b = bs->breakpoint_at;
7948 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7949
7950 annotate_catchpoint (b->number);
7951 maybe_print_thread_hit_breakpoint (uiout);
7952 if (b->disposition == disp_del)
7953 uiout->text ("Temporary catchpoint ");
7954 else
7955 uiout->text ("Catchpoint ");
7956 if (uiout->is_mi_like_p ())
7957 {
7958 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7959 uiout->field_string ("disp", bpdisp_text (b->disposition));
7960 }
7961 uiout->field_int ("bkptno", b->number);
7962 uiout->text (" (vforked process ");
7963 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7964 uiout->text ("), ");
7965 return PRINT_SRC_AND_LOC;
7966 }
7967
7968 /* Implement the "print_one" breakpoint_ops method for vfork
7969 catchpoints. */
7970
7971 static void
7972 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7973 {
7974 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7975 struct value_print_options opts;
7976 struct ui_out *uiout = current_uiout;
7977
7978 get_user_print_options (&opts);
7979 /* Field 4, the address, is omitted (which makes the columns not
7980 line up too nicely with the headers, but the effect is relatively
7981 readable). */
7982 if (opts.addressprint)
7983 uiout->field_skip ("addr");
7984 annotate_field (5);
7985 uiout->text ("vfork");
7986 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7987 {
7988 uiout->text (", process ");
7989 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7990 uiout->spaces (1);
7991 }
7992
7993 if (uiout->is_mi_like_p ())
7994 uiout->field_string ("catch-type", "vfork");
7995 }
7996
7997 /* Implement the "print_mention" breakpoint_ops method for vfork
7998 catchpoints. */
7999
8000 static void
8001 print_mention_catch_vfork (struct breakpoint *b)
8002 {
8003 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8004 }
8005
8006 /* Implement the "print_recreate" breakpoint_ops method for vfork
8007 catchpoints. */
8008
8009 static void
8010 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8011 {
8012 fprintf_unfiltered (fp, "catch vfork");
8013 print_recreate_thread (b, fp);
8014 }
8015
8016 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8017
8018 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8019
8020 /* An instance of this type is used to represent an solib catchpoint.
8021 A breakpoint is really of this type iff its ops pointer points to
8022 CATCH_SOLIB_BREAKPOINT_OPS. */
8023
8024 struct solib_catchpoint : public breakpoint
8025 {
8026 ~solib_catchpoint () override;
8027
8028 /* True for "catch load", false for "catch unload". */
8029 unsigned char is_load;
8030
8031 /* Regular expression to match, if any. COMPILED is only valid when
8032 REGEX is non-NULL. */
8033 char *regex;
8034 std::unique_ptr<compiled_regex> compiled;
8035 };
8036
8037 solib_catchpoint::~solib_catchpoint ()
8038 {
8039 xfree (this->regex);
8040 }
8041
8042 static int
8043 insert_catch_solib (struct bp_location *ignore)
8044 {
8045 return 0;
8046 }
8047
8048 static int
8049 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8050 {
8051 return 0;
8052 }
8053
8054 static int
8055 breakpoint_hit_catch_solib (const struct bp_location *bl,
8056 const address_space *aspace,
8057 CORE_ADDR bp_addr,
8058 const struct target_waitstatus *ws)
8059 {
8060 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8061 struct breakpoint *other;
8062
8063 if (ws->kind == TARGET_WAITKIND_LOADED)
8064 return 1;
8065
8066 ALL_BREAKPOINTS (other)
8067 {
8068 struct bp_location *other_bl;
8069
8070 if (other == bl->owner)
8071 continue;
8072
8073 if (other->type != bp_shlib_event)
8074 continue;
8075
8076 if (self->pspace != NULL && other->pspace != self->pspace)
8077 continue;
8078
8079 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8080 {
8081 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8082 return 1;
8083 }
8084 }
8085
8086 return 0;
8087 }
8088
8089 static void
8090 check_status_catch_solib (struct bpstats *bs)
8091 {
8092 struct solib_catchpoint *self
8093 = (struct solib_catchpoint *) bs->breakpoint_at;
8094 int ix;
8095
8096 if (self->is_load)
8097 {
8098 struct so_list *iter;
8099
8100 for (ix = 0;
8101 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8102 ix, iter);
8103 ++ix)
8104 {
8105 if (!self->regex
8106 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8107 return;
8108 }
8109 }
8110 else
8111 {
8112 char *iter;
8113
8114 for (ix = 0;
8115 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8116 ix, iter);
8117 ++ix)
8118 {
8119 if (!self->regex
8120 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8121 return;
8122 }
8123 }
8124
8125 bs->stop = 0;
8126 bs->print_it = print_it_noop;
8127 }
8128
8129 static enum print_stop_action
8130 print_it_catch_solib (bpstat bs)
8131 {
8132 struct breakpoint *b = bs->breakpoint_at;
8133 struct ui_out *uiout = current_uiout;
8134
8135 annotate_catchpoint (b->number);
8136 maybe_print_thread_hit_breakpoint (uiout);
8137 if (b->disposition == disp_del)
8138 uiout->text ("Temporary catchpoint ");
8139 else
8140 uiout->text ("Catchpoint ");
8141 uiout->field_int ("bkptno", b->number);
8142 uiout->text ("\n");
8143 if (uiout->is_mi_like_p ())
8144 uiout->field_string ("disp", bpdisp_text (b->disposition));
8145 print_solib_event (1);
8146 return PRINT_SRC_AND_LOC;
8147 }
8148
8149 static void
8150 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8151 {
8152 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8153 struct value_print_options opts;
8154 struct ui_out *uiout = current_uiout;
8155 char *msg;
8156
8157 get_user_print_options (&opts);
8158 /* Field 4, the address, is omitted (which makes the columns not
8159 line up too nicely with the headers, but the effect is relatively
8160 readable). */
8161 if (opts.addressprint)
8162 {
8163 annotate_field (4);
8164 uiout->field_skip ("addr");
8165 }
8166
8167 annotate_field (5);
8168 if (self->is_load)
8169 {
8170 if (self->regex)
8171 msg = xstrprintf (_("load of library matching %s"), self->regex);
8172 else
8173 msg = xstrdup (_("load of library"));
8174 }
8175 else
8176 {
8177 if (self->regex)
8178 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8179 else
8180 msg = xstrdup (_("unload of library"));
8181 }
8182 uiout->field_string ("what", msg);
8183 xfree (msg);
8184
8185 if (uiout->is_mi_like_p ())
8186 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8187 }
8188
8189 static void
8190 print_mention_catch_solib (struct breakpoint *b)
8191 {
8192 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8193
8194 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8195 self->is_load ? "load" : "unload");
8196 }
8197
8198 static void
8199 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8200 {
8201 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8202
8203 fprintf_unfiltered (fp, "%s %s",
8204 b->disposition == disp_del ? "tcatch" : "catch",
8205 self->is_load ? "load" : "unload");
8206 if (self->regex)
8207 fprintf_unfiltered (fp, " %s", self->regex);
8208 fprintf_unfiltered (fp, "\n");
8209 }
8210
8211 static struct breakpoint_ops catch_solib_breakpoint_ops;
8212
8213 /* Shared helper function (MI and CLI) for creating and installing
8214 a shared object event catchpoint. If IS_LOAD is non-zero then
8215 the events to be caught are load events, otherwise they are
8216 unload events. If IS_TEMP is non-zero the catchpoint is a
8217 temporary one. If ENABLED is non-zero the catchpoint is
8218 created in an enabled state. */
8219
8220 void
8221 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8222 {
8223 struct gdbarch *gdbarch = get_current_arch ();
8224
8225 if (!arg)
8226 arg = "";
8227 arg = skip_spaces (arg);
8228
8229 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8230
8231 if (*arg != '\0')
8232 {
8233 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8234 _("Invalid regexp")));
8235 c->regex = xstrdup (arg);
8236 }
8237
8238 c->is_load = is_load;
8239 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8240 &catch_solib_breakpoint_ops);
8241
8242 c->enable_state = enabled ? bp_enabled : bp_disabled;
8243
8244 install_breakpoint (0, std::move (c), 1);
8245 }
8246
8247 /* A helper function that does all the work for "catch load" and
8248 "catch unload". */
8249
8250 static void
8251 catch_load_or_unload (char *arg, int from_tty, int is_load,
8252 struct cmd_list_element *command)
8253 {
8254 int tempflag;
8255 const int enabled = 1;
8256
8257 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8258
8259 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8260 }
8261
8262 static void
8263 catch_load_command_1 (char *arg, int from_tty,
8264 struct cmd_list_element *command)
8265 {
8266 catch_load_or_unload (arg, from_tty, 1, command);
8267 }
8268
8269 static void
8270 catch_unload_command_1 (char *arg, int from_tty,
8271 struct cmd_list_element *command)
8272 {
8273 catch_load_or_unload (arg, from_tty, 0, command);
8274 }
8275
8276 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8277 is non-zero, then make the breakpoint temporary. If COND_STRING is
8278 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8279 the breakpoint_ops structure associated to the catchpoint. */
8280
8281 void
8282 init_catchpoint (struct breakpoint *b,
8283 struct gdbarch *gdbarch, int tempflag,
8284 const char *cond_string,
8285 const struct breakpoint_ops *ops)
8286 {
8287 symtab_and_line sal;
8288 sal.pspace = current_program_space;
8289
8290 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8291
8292 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8293 b->disposition = tempflag ? disp_del : disp_donttouch;
8294 }
8295
8296 void
8297 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8298 {
8299 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8300 set_breakpoint_number (internal, b);
8301 if (is_tracepoint (b))
8302 set_tracepoint_count (breakpoint_count);
8303 if (!internal)
8304 mention (b);
8305 observer_notify_breakpoint_created (b);
8306
8307 if (update_gll)
8308 update_global_location_list (UGLL_MAY_INSERT);
8309 }
8310
8311 static void
8312 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8313 int tempflag, const char *cond_string,
8314 const struct breakpoint_ops *ops)
8315 {
8316 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8317
8318 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8319
8320 c->forked_inferior_pid = null_ptid;
8321
8322 install_breakpoint (0, std::move (c), 1);
8323 }
8324
8325 /* Exec catchpoints. */
8326
8327 /* An instance of this type is used to represent an exec catchpoint.
8328 A breakpoint is really of this type iff its ops pointer points to
8329 CATCH_EXEC_BREAKPOINT_OPS. */
8330
8331 struct exec_catchpoint : public breakpoint
8332 {
8333 ~exec_catchpoint () override;
8334
8335 /* Filename of a program whose exec triggered this catchpoint.
8336 This field is only valid immediately after this catchpoint has
8337 triggered. */
8338 char *exec_pathname;
8339 };
8340
8341 /* Exec catchpoint destructor. */
8342
8343 exec_catchpoint::~exec_catchpoint ()
8344 {
8345 xfree (this->exec_pathname);
8346 }
8347
8348 static int
8349 insert_catch_exec (struct bp_location *bl)
8350 {
8351 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8352 }
8353
8354 static int
8355 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8356 {
8357 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8358 }
8359
8360 static int
8361 breakpoint_hit_catch_exec (const struct bp_location *bl,
8362 const address_space *aspace, CORE_ADDR bp_addr,
8363 const struct target_waitstatus *ws)
8364 {
8365 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8366
8367 if (ws->kind != TARGET_WAITKIND_EXECD)
8368 return 0;
8369
8370 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8371 return 1;
8372 }
8373
8374 static enum print_stop_action
8375 print_it_catch_exec (bpstat bs)
8376 {
8377 struct ui_out *uiout = current_uiout;
8378 struct breakpoint *b = bs->breakpoint_at;
8379 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8380
8381 annotate_catchpoint (b->number);
8382 maybe_print_thread_hit_breakpoint (uiout);
8383 if (b->disposition == disp_del)
8384 uiout->text ("Temporary catchpoint ");
8385 else
8386 uiout->text ("Catchpoint ");
8387 if (uiout->is_mi_like_p ())
8388 {
8389 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8390 uiout->field_string ("disp", bpdisp_text (b->disposition));
8391 }
8392 uiout->field_int ("bkptno", b->number);
8393 uiout->text (" (exec'd ");
8394 uiout->field_string ("new-exec", c->exec_pathname);
8395 uiout->text ("), ");
8396
8397 return PRINT_SRC_AND_LOC;
8398 }
8399
8400 static void
8401 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8402 {
8403 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8404 struct value_print_options opts;
8405 struct ui_out *uiout = current_uiout;
8406
8407 get_user_print_options (&opts);
8408
8409 /* Field 4, the address, is omitted (which makes the columns
8410 not line up too nicely with the headers, but the effect
8411 is relatively readable). */
8412 if (opts.addressprint)
8413 uiout->field_skip ("addr");
8414 annotate_field (5);
8415 uiout->text ("exec");
8416 if (c->exec_pathname != NULL)
8417 {
8418 uiout->text (", program \"");
8419 uiout->field_string ("what", c->exec_pathname);
8420 uiout->text ("\" ");
8421 }
8422
8423 if (uiout->is_mi_like_p ())
8424 uiout->field_string ("catch-type", "exec");
8425 }
8426
8427 static void
8428 print_mention_catch_exec (struct breakpoint *b)
8429 {
8430 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8431 }
8432
8433 /* Implement the "print_recreate" breakpoint_ops method for exec
8434 catchpoints. */
8435
8436 static void
8437 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8438 {
8439 fprintf_unfiltered (fp, "catch exec");
8440 print_recreate_thread (b, fp);
8441 }
8442
8443 static struct breakpoint_ops catch_exec_breakpoint_ops;
8444
8445 static int
8446 hw_breakpoint_used_count (void)
8447 {
8448 int i = 0;
8449 struct breakpoint *b;
8450 struct bp_location *bl;
8451
8452 ALL_BREAKPOINTS (b)
8453 {
8454 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8455 for (bl = b->loc; bl; bl = bl->next)
8456 {
8457 /* Special types of hardware breakpoints may use more than
8458 one register. */
8459 i += b->ops->resources_needed (bl);
8460 }
8461 }
8462
8463 return i;
8464 }
8465
8466 /* Returns the resources B would use if it were a hardware
8467 watchpoint. */
8468
8469 static int
8470 hw_watchpoint_use_count (struct breakpoint *b)
8471 {
8472 int i = 0;
8473 struct bp_location *bl;
8474
8475 if (!breakpoint_enabled (b))
8476 return 0;
8477
8478 for (bl = b->loc; bl; bl = bl->next)
8479 {
8480 /* Special types of hardware watchpoints may use more than
8481 one register. */
8482 i += b->ops->resources_needed (bl);
8483 }
8484
8485 return i;
8486 }
8487
8488 /* Returns the sum the used resources of all hardware watchpoints of
8489 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8490 the sum of the used resources of all hardware watchpoints of other
8491 types _not_ TYPE. */
8492
8493 static int
8494 hw_watchpoint_used_count_others (struct breakpoint *except,
8495 enum bptype type, int *other_type_used)
8496 {
8497 int i = 0;
8498 struct breakpoint *b;
8499
8500 *other_type_used = 0;
8501 ALL_BREAKPOINTS (b)
8502 {
8503 if (b == except)
8504 continue;
8505 if (!breakpoint_enabled (b))
8506 continue;
8507
8508 if (b->type == type)
8509 i += hw_watchpoint_use_count (b);
8510 else if (is_hardware_watchpoint (b))
8511 *other_type_used = 1;
8512 }
8513
8514 return i;
8515 }
8516
8517 void
8518 disable_watchpoints_before_interactive_call_start (void)
8519 {
8520 struct breakpoint *b;
8521
8522 ALL_BREAKPOINTS (b)
8523 {
8524 if (is_watchpoint (b) && breakpoint_enabled (b))
8525 {
8526 b->enable_state = bp_call_disabled;
8527 update_global_location_list (UGLL_DONT_INSERT);
8528 }
8529 }
8530 }
8531
8532 void
8533 enable_watchpoints_after_interactive_call_stop (void)
8534 {
8535 struct breakpoint *b;
8536
8537 ALL_BREAKPOINTS (b)
8538 {
8539 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8540 {
8541 b->enable_state = bp_enabled;
8542 update_global_location_list (UGLL_MAY_INSERT);
8543 }
8544 }
8545 }
8546
8547 void
8548 disable_breakpoints_before_startup (void)
8549 {
8550 current_program_space->executing_startup = 1;
8551 update_global_location_list (UGLL_DONT_INSERT);
8552 }
8553
8554 void
8555 enable_breakpoints_after_startup (void)
8556 {
8557 current_program_space->executing_startup = 0;
8558 breakpoint_re_set ();
8559 }
8560
8561 /* Create a new single-step breakpoint for thread THREAD, with no
8562 locations. */
8563
8564 static struct breakpoint *
8565 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8566 {
8567 std::unique_ptr<breakpoint> b (new breakpoint ());
8568
8569 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8570 &momentary_breakpoint_ops);
8571
8572 b->disposition = disp_donttouch;
8573 b->frame_id = null_frame_id;
8574
8575 b->thread = thread;
8576 gdb_assert (b->thread != 0);
8577
8578 return add_to_breakpoint_chain (std::move (b));
8579 }
8580
8581 /* Set a momentary breakpoint of type TYPE at address specified by
8582 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8583 frame. */
8584
8585 breakpoint_up
8586 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8587 struct frame_id frame_id, enum bptype type)
8588 {
8589 struct breakpoint *b;
8590
8591 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8592 tail-called one. */
8593 gdb_assert (!frame_id_artificial_p (frame_id));
8594
8595 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8596 b->enable_state = bp_enabled;
8597 b->disposition = disp_donttouch;
8598 b->frame_id = frame_id;
8599
8600 /* If we're debugging a multi-threaded program, then we want
8601 momentary breakpoints to be active in only a single thread of
8602 control. */
8603 if (in_thread_list (inferior_ptid))
8604 b->thread = ptid_to_global_thread_id (inferior_ptid);
8605
8606 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8607
8608 return breakpoint_up (b);
8609 }
8610
8611 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8612 The new breakpoint will have type TYPE, use OPS as its
8613 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8614
8615 static struct breakpoint *
8616 momentary_breakpoint_from_master (struct breakpoint *orig,
8617 enum bptype type,
8618 const struct breakpoint_ops *ops,
8619 int loc_enabled)
8620 {
8621 struct breakpoint *copy;
8622
8623 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8624 copy->loc = allocate_bp_location (copy);
8625 set_breakpoint_location_function (copy->loc, 1);
8626
8627 copy->loc->gdbarch = orig->loc->gdbarch;
8628 copy->loc->requested_address = orig->loc->requested_address;
8629 copy->loc->address = orig->loc->address;
8630 copy->loc->section = orig->loc->section;
8631 copy->loc->pspace = orig->loc->pspace;
8632 copy->loc->probe = orig->loc->probe;
8633 copy->loc->line_number = orig->loc->line_number;
8634 copy->loc->symtab = orig->loc->symtab;
8635 copy->loc->enabled = loc_enabled;
8636 copy->frame_id = orig->frame_id;
8637 copy->thread = orig->thread;
8638 copy->pspace = orig->pspace;
8639
8640 copy->enable_state = bp_enabled;
8641 copy->disposition = disp_donttouch;
8642 copy->number = internal_breakpoint_number--;
8643
8644 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8645 return copy;
8646 }
8647
8648 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8649 ORIG is NULL. */
8650
8651 struct breakpoint *
8652 clone_momentary_breakpoint (struct breakpoint *orig)
8653 {
8654 /* If there's nothing to clone, then return nothing. */
8655 if (orig == NULL)
8656 return NULL;
8657
8658 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8659 }
8660
8661 breakpoint_up
8662 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8663 enum bptype type)
8664 {
8665 struct symtab_and_line sal;
8666
8667 sal = find_pc_line (pc, 0);
8668 sal.pc = pc;
8669 sal.section = find_pc_overlay (pc);
8670 sal.explicit_pc = 1;
8671
8672 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8673 }
8674 \f
8675
8676 /* Tell the user we have just set a breakpoint B. */
8677
8678 static void
8679 mention (struct breakpoint *b)
8680 {
8681 b->ops->print_mention (b);
8682 if (current_uiout->is_mi_like_p ())
8683 return;
8684 printf_filtered ("\n");
8685 }
8686 \f
8687
8688 static int bp_loc_is_permanent (struct bp_location *loc);
8689
8690 static struct bp_location *
8691 add_location_to_breakpoint (struct breakpoint *b,
8692 const struct symtab_and_line *sal)
8693 {
8694 struct bp_location *loc, **tmp;
8695 CORE_ADDR adjusted_address;
8696 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8697
8698 if (loc_gdbarch == NULL)
8699 loc_gdbarch = b->gdbarch;
8700
8701 /* Adjust the breakpoint's address prior to allocating a location.
8702 Once we call allocate_bp_location(), that mostly uninitialized
8703 location will be placed on the location chain. Adjustment of the
8704 breakpoint may cause target_read_memory() to be called and we do
8705 not want its scan of the location chain to find a breakpoint and
8706 location that's only been partially initialized. */
8707 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8708 sal->pc, b->type);
8709
8710 /* Sort the locations by their ADDRESS. */
8711 loc = allocate_bp_location (b);
8712 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8713 tmp = &((*tmp)->next))
8714 ;
8715 loc->next = *tmp;
8716 *tmp = loc;
8717
8718 loc->requested_address = sal->pc;
8719 loc->address = adjusted_address;
8720 loc->pspace = sal->pspace;
8721 loc->probe.probe = sal->probe;
8722 loc->probe.objfile = sal->objfile;
8723 gdb_assert (loc->pspace != NULL);
8724 loc->section = sal->section;
8725 loc->gdbarch = loc_gdbarch;
8726 loc->line_number = sal->line;
8727 loc->symtab = sal->symtab;
8728 loc->symbol = sal->symbol;
8729
8730 set_breakpoint_location_function (loc,
8731 sal->explicit_pc || sal->explicit_line);
8732
8733 /* While by definition, permanent breakpoints are already present in the
8734 code, we don't mark the location as inserted. Normally one would expect
8735 that GDB could rely on that breakpoint instruction to stop the program,
8736 thus removing the need to insert its own breakpoint, except that executing
8737 the breakpoint instruction can kill the target instead of reporting a
8738 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8739 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8740 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8741 breakpoint be inserted normally results in QEMU knowing about the GDB
8742 breakpoint, and thus trap before the breakpoint instruction is executed.
8743 (If GDB later needs to continue execution past the permanent breakpoint,
8744 it manually increments the PC, thus avoiding executing the breakpoint
8745 instruction.) */
8746 if (bp_loc_is_permanent (loc))
8747 loc->permanent = 1;
8748
8749 return loc;
8750 }
8751 \f
8752
8753 /* See breakpoint.h. */
8754
8755 int
8756 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8757 {
8758 int len;
8759 CORE_ADDR addr;
8760 const gdb_byte *bpoint;
8761 gdb_byte *target_mem;
8762
8763 addr = address;
8764 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8765
8766 /* Software breakpoints unsupported? */
8767 if (bpoint == NULL)
8768 return 0;
8769
8770 target_mem = (gdb_byte *) alloca (len);
8771
8772 /* Enable the automatic memory restoration from breakpoints while
8773 we read the memory. Otherwise we could say about our temporary
8774 breakpoints they are permanent. */
8775 scoped_restore restore_memory
8776 = make_scoped_restore_show_memory_breakpoints (0);
8777
8778 if (target_read_memory (address, target_mem, len) == 0
8779 && memcmp (target_mem, bpoint, len) == 0)
8780 return 1;
8781
8782 return 0;
8783 }
8784
8785 /* Return 1 if LOC is pointing to a permanent breakpoint,
8786 return 0 otherwise. */
8787
8788 static int
8789 bp_loc_is_permanent (struct bp_location *loc)
8790 {
8791 gdb_assert (loc != NULL);
8792
8793 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8794 attempt to read from the addresses the locations of these breakpoint types
8795 point to. program_breakpoint_here_p, below, will attempt to read
8796 memory. */
8797 if (!breakpoint_address_is_meaningful (loc->owner))
8798 return 0;
8799
8800 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8801 switch_to_program_space_and_thread (loc->pspace);
8802 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8803 }
8804
8805 /* Build a command list for the dprintf corresponding to the current
8806 settings of the dprintf style options. */
8807
8808 static void
8809 update_dprintf_command_list (struct breakpoint *b)
8810 {
8811 char *dprintf_args = b->extra_string;
8812 char *printf_line = NULL;
8813
8814 if (!dprintf_args)
8815 return;
8816
8817 dprintf_args = skip_spaces (dprintf_args);
8818
8819 /* Allow a comma, as it may have terminated a location, but don't
8820 insist on it. */
8821 if (*dprintf_args == ',')
8822 ++dprintf_args;
8823 dprintf_args = skip_spaces (dprintf_args);
8824
8825 if (*dprintf_args != '"')
8826 error (_("Bad format string, missing '\"'."));
8827
8828 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8829 printf_line = xstrprintf ("printf %s", dprintf_args);
8830 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8831 {
8832 if (!dprintf_function)
8833 error (_("No function supplied for dprintf call"));
8834
8835 if (dprintf_channel && strlen (dprintf_channel) > 0)
8836 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8837 dprintf_function,
8838 dprintf_channel,
8839 dprintf_args);
8840 else
8841 printf_line = xstrprintf ("call (void) %s (%s)",
8842 dprintf_function,
8843 dprintf_args);
8844 }
8845 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8846 {
8847 if (target_can_run_breakpoint_commands ())
8848 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8849 else
8850 {
8851 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8852 printf_line = xstrprintf ("printf %s", dprintf_args);
8853 }
8854 }
8855 else
8856 internal_error (__FILE__, __LINE__,
8857 _("Invalid dprintf style."));
8858
8859 gdb_assert (printf_line != NULL);
8860 /* Manufacture a printf sequence. */
8861 {
8862 struct command_line *printf_cmd_line = XNEW (struct command_line);
8863
8864 printf_cmd_line->control_type = simple_control;
8865 printf_cmd_line->body_count = 0;
8866 printf_cmd_line->body_list = NULL;
8867 printf_cmd_line->next = NULL;
8868 printf_cmd_line->line = printf_line;
8869
8870 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
8871 }
8872 }
8873
8874 /* Update all dprintf commands, making their command lists reflect
8875 current style settings. */
8876
8877 static void
8878 update_dprintf_commands (char *args, int from_tty,
8879 struct cmd_list_element *c)
8880 {
8881 struct breakpoint *b;
8882
8883 ALL_BREAKPOINTS (b)
8884 {
8885 if (b->type == bp_dprintf)
8886 update_dprintf_command_list (b);
8887 }
8888 }
8889
8890 /* Create a breakpoint with SAL as location. Use LOCATION
8891 as a description of the location, and COND_STRING
8892 as condition expression. If LOCATION is NULL then create an
8893 "address location" from the address in the SAL. */
8894
8895 static void
8896 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8897 gdb::array_view<const symtab_and_line> sals,
8898 event_location_up &&location,
8899 gdb::unique_xmalloc_ptr<char> filter,
8900 gdb::unique_xmalloc_ptr<char> cond_string,
8901 gdb::unique_xmalloc_ptr<char> extra_string,
8902 enum bptype type, enum bpdisp disposition,
8903 int thread, int task, int ignore_count,
8904 const struct breakpoint_ops *ops, int from_tty,
8905 int enabled, int internal, unsigned flags,
8906 int display_canonical)
8907 {
8908 int i;
8909
8910 if (type == bp_hardware_breakpoint)
8911 {
8912 int target_resources_ok;
8913
8914 i = hw_breakpoint_used_count ();
8915 target_resources_ok =
8916 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8917 i + 1, 0);
8918 if (target_resources_ok == 0)
8919 error (_("No hardware breakpoint support in the target."));
8920 else if (target_resources_ok < 0)
8921 error (_("Hardware breakpoints used exceeds limit."));
8922 }
8923
8924 gdb_assert (!sals.empty ());
8925
8926 for (const auto &sal : sals)
8927 {
8928 struct bp_location *loc;
8929
8930 if (from_tty)
8931 {
8932 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8933 if (!loc_gdbarch)
8934 loc_gdbarch = gdbarch;
8935
8936 describe_other_breakpoints (loc_gdbarch,
8937 sal.pspace, sal.pc, sal.section, thread);
8938 }
8939
8940 if (&sal == &sals[0])
8941 {
8942 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8943 b->thread = thread;
8944 b->task = task;
8945
8946 b->cond_string = cond_string.release ();
8947 b->extra_string = extra_string.release ();
8948 b->ignore_count = ignore_count;
8949 b->enable_state = enabled ? bp_enabled : bp_disabled;
8950 b->disposition = disposition;
8951
8952 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8953 b->loc->inserted = 1;
8954
8955 if (type == bp_static_tracepoint)
8956 {
8957 struct tracepoint *t = (struct tracepoint *) b;
8958 struct static_tracepoint_marker marker;
8959
8960 if (strace_marker_p (b))
8961 {
8962 /* We already know the marker exists, otherwise, we
8963 wouldn't see a sal for it. */
8964 const char *p
8965 = &event_location_to_string (b->location.get ())[3];
8966 const char *endp;
8967 char *marker_str;
8968
8969 p = skip_spaces (p);
8970
8971 endp = skip_to_space (p);
8972
8973 marker_str = savestring (p, endp - p);
8974 t->static_trace_marker_id = marker_str;
8975
8976 printf_filtered (_("Probed static tracepoint "
8977 "marker \"%s\"\n"),
8978 t->static_trace_marker_id);
8979 }
8980 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8981 {
8982 t->static_trace_marker_id = xstrdup (marker.str_id);
8983 release_static_tracepoint_marker (&marker);
8984
8985 printf_filtered (_("Probed static tracepoint "
8986 "marker \"%s\"\n"),
8987 t->static_trace_marker_id);
8988 }
8989 else
8990 warning (_("Couldn't determine the static "
8991 "tracepoint marker to probe"));
8992 }
8993
8994 loc = b->loc;
8995 }
8996 else
8997 {
8998 loc = add_location_to_breakpoint (b, &sal);
8999 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9000 loc->inserted = 1;
9001 }
9002
9003 if (b->cond_string)
9004 {
9005 const char *arg = b->cond_string;
9006
9007 loc->cond = parse_exp_1 (&arg, loc->address,
9008 block_for_pc (loc->address), 0);
9009 if (*arg)
9010 error (_("Garbage '%s' follows condition"), arg);
9011 }
9012
9013 /* Dynamic printf requires and uses additional arguments on the
9014 command line, otherwise it's an error. */
9015 if (type == bp_dprintf)
9016 {
9017 if (b->extra_string)
9018 update_dprintf_command_list (b);
9019 else
9020 error (_("Format string required"));
9021 }
9022 else if (b->extra_string)
9023 error (_("Garbage '%s' at end of command"), b->extra_string);
9024 }
9025
9026 b->display_canonical = display_canonical;
9027 if (location != NULL)
9028 b->location = std::move (location);
9029 else
9030 b->location = new_address_location (b->loc->address, NULL, 0);
9031 b->filter = filter.release ();
9032 }
9033
9034 static void
9035 create_breakpoint_sal (struct gdbarch *gdbarch,
9036 gdb::array_view<const symtab_and_line> sals,
9037 event_location_up &&location,
9038 gdb::unique_xmalloc_ptr<char> filter,
9039 gdb::unique_xmalloc_ptr<char> cond_string,
9040 gdb::unique_xmalloc_ptr<char> extra_string,
9041 enum bptype type, enum bpdisp disposition,
9042 int thread, int task, int ignore_count,
9043 const struct breakpoint_ops *ops, int from_tty,
9044 int enabled, int internal, unsigned flags,
9045 int display_canonical)
9046 {
9047 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9048
9049 init_breakpoint_sal (b.get (), gdbarch,
9050 sals, std::move (location),
9051 std::move (filter),
9052 std::move (cond_string),
9053 std::move (extra_string),
9054 type, disposition,
9055 thread, task, ignore_count,
9056 ops, from_tty,
9057 enabled, internal, flags,
9058 display_canonical);
9059
9060 install_breakpoint (internal, std::move (b), 0);
9061 }
9062
9063 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9064 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9065 value. COND_STRING, if not NULL, specified the condition to be
9066 used for all breakpoints. Essentially the only case where
9067 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9068 function. In that case, it's still not possible to specify
9069 separate conditions for different overloaded functions, so
9070 we take just a single condition string.
9071
9072 NOTE: If the function succeeds, the caller is expected to cleanup
9073 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9074 array contents). If the function fails (error() is called), the
9075 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9076 COND and SALS arrays and each of those arrays contents. */
9077
9078 static void
9079 create_breakpoints_sal (struct gdbarch *gdbarch,
9080 struct linespec_result *canonical,
9081 gdb::unique_xmalloc_ptr<char> cond_string,
9082 gdb::unique_xmalloc_ptr<char> extra_string,
9083 enum bptype type, enum bpdisp disposition,
9084 int thread, int task, int ignore_count,
9085 const struct breakpoint_ops *ops, int from_tty,
9086 int enabled, int internal, unsigned flags)
9087 {
9088 if (canonical->pre_expanded)
9089 gdb_assert (canonical->lsals.size () == 1);
9090
9091 for (const auto &lsal : canonical->lsals)
9092 {
9093 /* Note that 'location' can be NULL in the case of a plain
9094 'break', without arguments. */
9095 event_location_up location
9096 = (canonical->location != NULL
9097 ? copy_event_location (canonical->location.get ()) : NULL);
9098 gdb::unique_xmalloc_ptr<char> filter_string
9099 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9100
9101 create_breakpoint_sal (gdbarch, lsal.sals,
9102 std::move (location),
9103 std::move (filter_string),
9104 std::move (cond_string),
9105 std::move (extra_string),
9106 type, disposition,
9107 thread, task, ignore_count, ops,
9108 from_tty, enabled, internal, flags,
9109 canonical->special_display);
9110 }
9111 }
9112
9113 /* Parse LOCATION which is assumed to be a SAL specification possibly
9114 followed by conditionals. On return, SALS contains an array of SAL
9115 addresses found. LOCATION points to the end of the SAL (for
9116 linespec locations).
9117
9118 The array and the line spec strings are allocated on the heap, it is
9119 the caller's responsibility to free them. */
9120
9121 static void
9122 parse_breakpoint_sals (const struct event_location *location,
9123 struct linespec_result *canonical)
9124 {
9125 struct symtab_and_line cursal;
9126
9127 if (event_location_type (location) == LINESPEC_LOCATION)
9128 {
9129 const char *address = get_linespec_location (location);
9130
9131 if (address == NULL)
9132 {
9133 /* The last displayed codepoint, if it's valid, is our default
9134 breakpoint address. */
9135 if (last_displayed_sal_is_valid ())
9136 {
9137 /* Set sal's pspace, pc, symtab, and line to the values
9138 corresponding to the last call to print_frame_info.
9139 Be sure to reinitialize LINE with NOTCURRENT == 0
9140 as the breakpoint line number is inappropriate otherwise.
9141 find_pc_line would adjust PC, re-set it back. */
9142 symtab_and_line sal = get_last_displayed_sal ();
9143 CORE_ADDR pc = sal.pc;
9144
9145 sal = find_pc_line (pc, 0);
9146
9147 /* "break" without arguments is equivalent to "break *PC"
9148 where PC is the last displayed codepoint's address. So
9149 make sure to set sal.explicit_pc to prevent GDB from
9150 trying to expand the list of sals to include all other
9151 instances with the same symtab and line. */
9152 sal.pc = pc;
9153 sal.explicit_pc = 1;
9154
9155 struct linespec_sals lsal;
9156 lsal.sals = {sal};
9157 lsal.canonical = NULL;
9158
9159 canonical->lsals.push_back (std::move (lsal));
9160 return;
9161 }
9162 else
9163 error (_("No default breakpoint address now."));
9164 }
9165 }
9166
9167 /* Force almost all breakpoints to be in terms of the
9168 current_source_symtab (which is decode_line_1's default).
9169 This should produce the results we want almost all of the
9170 time while leaving default_breakpoint_* alone.
9171
9172 ObjC: However, don't match an Objective-C method name which
9173 may have a '+' or '-' succeeded by a '['. */
9174 cursal = get_current_source_symtab_and_line ();
9175 if (last_displayed_sal_is_valid ())
9176 {
9177 const char *address = NULL;
9178
9179 if (event_location_type (location) == LINESPEC_LOCATION)
9180 address = get_linespec_location (location);
9181
9182 if (!cursal.symtab
9183 || (address != NULL
9184 && strchr ("+-", address[0]) != NULL
9185 && address[1] != '['))
9186 {
9187 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9188 get_last_displayed_symtab (),
9189 get_last_displayed_line (),
9190 canonical, NULL, NULL);
9191 return;
9192 }
9193 }
9194
9195 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9196 cursal.symtab, cursal.line, canonical, NULL, NULL);
9197 }
9198
9199
9200 /* Convert each SAL into a real PC. Verify that the PC can be
9201 inserted as a breakpoint. If it can't throw an error. */
9202
9203 static void
9204 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9205 {
9206 for (auto &sal : sals)
9207 resolve_sal_pc (&sal);
9208 }
9209
9210 /* Fast tracepoints may have restrictions on valid locations. For
9211 instance, a fast tracepoint using a jump instead of a trap will
9212 likely have to overwrite more bytes than a trap would, and so can
9213 only be placed where the instruction is longer than the jump, or a
9214 multi-instruction sequence does not have a jump into the middle of
9215 it, etc. */
9216
9217 static void
9218 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9219 gdb::array_view<const symtab_and_line> sals)
9220 {
9221 int rslt;
9222 char *msg;
9223 struct cleanup *old_chain;
9224
9225 for (const auto &sal : sals)
9226 {
9227 struct gdbarch *sarch;
9228
9229 sarch = get_sal_arch (sal);
9230 /* We fall back to GDBARCH if there is no architecture
9231 associated with SAL. */
9232 if (sarch == NULL)
9233 sarch = gdbarch;
9234 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9235 old_chain = make_cleanup (xfree, msg);
9236
9237 if (!rslt)
9238 error (_("May not have a fast tracepoint at %s%s"),
9239 paddress (sarch, sal.pc), (msg ? msg : ""));
9240
9241 do_cleanups (old_chain);
9242 }
9243 }
9244
9245 /* Given TOK, a string specification of condition and thread, as
9246 accepted by the 'break' command, extract the condition
9247 string and thread number and set *COND_STRING and *THREAD.
9248 PC identifies the context at which the condition should be parsed.
9249 If no condition is found, *COND_STRING is set to NULL.
9250 If no thread is found, *THREAD is set to -1. */
9251
9252 static void
9253 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9254 char **cond_string, int *thread, int *task,
9255 char **rest)
9256 {
9257 *cond_string = NULL;
9258 *thread = -1;
9259 *task = 0;
9260 *rest = NULL;
9261
9262 while (tok && *tok)
9263 {
9264 const char *end_tok;
9265 int toklen;
9266 const char *cond_start = NULL;
9267 const char *cond_end = NULL;
9268
9269 tok = skip_spaces (tok);
9270
9271 if ((*tok == '"' || *tok == ',') && rest)
9272 {
9273 *rest = savestring (tok, strlen (tok));
9274 return;
9275 }
9276
9277 end_tok = skip_to_space (tok);
9278
9279 toklen = end_tok - tok;
9280
9281 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9282 {
9283 tok = cond_start = end_tok + 1;
9284 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9285 cond_end = tok;
9286 *cond_string = savestring (cond_start, cond_end - cond_start);
9287 }
9288 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9289 {
9290 const char *tmptok;
9291 struct thread_info *thr;
9292
9293 tok = end_tok + 1;
9294 thr = parse_thread_id (tok, &tmptok);
9295 if (tok == tmptok)
9296 error (_("Junk after thread keyword."));
9297 *thread = thr->global_num;
9298 tok = tmptok;
9299 }
9300 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9301 {
9302 char *tmptok;
9303
9304 tok = end_tok + 1;
9305 *task = strtol (tok, &tmptok, 0);
9306 if (tok == tmptok)
9307 error (_("Junk after task keyword."));
9308 if (!valid_task_id (*task))
9309 error (_("Unknown task %d."), *task);
9310 tok = tmptok;
9311 }
9312 else if (rest)
9313 {
9314 *rest = savestring (tok, strlen (tok));
9315 return;
9316 }
9317 else
9318 error (_("Junk at end of arguments."));
9319 }
9320 }
9321
9322 /* Decode a static tracepoint marker spec. */
9323
9324 static std::vector<symtab_and_line>
9325 decode_static_tracepoint_spec (const char **arg_p)
9326 {
9327 VEC(static_tracepoint_marker_p) *markers = NULL;
9328 const char *p = &(*arg_p)[3];
9329 const char *endp;
9330 int i;
9331
9332 p = skip_spaces (p);
9333
9334 endp = skip_to_space (p);
9335
9336 std::string marker_str (p, endp - p);
9337
9338 markers = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9339 if (VEC_empty(static_tracepoint_marker_p, markers))
9340 error (_("No known static tracepoint marker named %s"),
9341 marker_str.c_str ());
9342
9343 std::vector<symtab_and_line> sals;
9344 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9345
9346 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9347 {
9348 struct static_tracepoint_marker *marker;
9349
9350 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9351
9352 symtab_and_line sal = find_pc_line (marker->address, 0);
9353 sal.pc = marker->address;
9354 sals.push_back (sal);
9355
9356 release_static_tracepoint_marker (marker);
9357 }
9358
9359 *arg_p = endp;
9360 return sals;
9361 }
9362
9363 /* See breakpoint.h. */
9364
9365 int
9366 create_breakpoint (struct gdbarch *gdbarch,
9367 const struct event_location *location,
9368 const char *cond_string,
9369 int thread, const char *extra_string,
9370 int parse_extra,
9371 int tempflag, enum bptype type_wanted,
9372 int ignore_count,
9373 enum auto_boolean pending_break_support,
9374 const struct breakpoint_ops *ops,
9375 int from_tty, int enabled, int internal,
9376 unsigned flags)
9377 {
9378 struct linespec_result canonical;
9379 struct cleanup *bkpt_chain = NULL;
9380 int pending = 0;
9381 int task = 0;
9382 int prev_bkpt_count = breakpoint_count;
9383
9384 gdb_assert (ops != NULL);
9385
9386 /* If extra_string isn't useful, set it to NULL. */
9387 if (extra_string != NULL && *extra_string == '\0')
9388 extra_string = NULL;
9389
9390 TRY
9391 {
9392 ops->create_sals_from_location (location, &canonical, type_wanted);
9393 }
9394 CATCH (e, RETURN_MASK_ERROR)
9395 {
9396 /* If caller is interested in rc value from parse, set
9397 value. */
9398 if (e.error == NOT_FOUND_ERROR)
9399 {
9400 /* If pending breakpoint support is turned off, throw
9401 error. */
9402
9403 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9404 throw_exception (e);
9405
9406 exception_print (gdb_stderr, e);
9407
9408 /* If pending breakpoint support is auto query and the user
9409 selects no, then simply return the error code. */
9410 if (pending_break_support == AUTO_BOOLEAN_AUTO
9411 && !nquery (_("Make %s pending on future shared library load? "),
9412 bptype_string (type_wanted)))
9413 return 0;
9414
9415 /* At this point, either the user was queried about setting
9416 a pending breakpoint and selected yes, or pending
9417 breakpoint behavior is on and thus a pending breakpoint
9418 is defaulted on behalf of the user. */
9419 pending = 1;
9420 }
9421 else
9422 throw_exception (e);
9423 }
9424 END_CATCH
9425
9426 if (!pending && canonical.lsals.empty ())
9427 return 0;
9428
9429 /* ----------------------------- SNIP -----------------------------
9430 Anything added to the cleanup chain beyond this point is assumed
9431 to be part of a breakpoint. If the breakpoint create succeeds
9432 then the memory is not reclaimed. */
9433 bkpt_chain = make_cleanup (null_cleanup, 0);
9434
9435 /* Resolve all line numbers to PC's and verify that the addresses
9436 are ok for the target. */
9437 if (!pending)
9438 {
9439 for (auto &lsal : canonical.lsals)
9440 breakpoint_sals_to_pc (lsal.sals);
9441 }
9442
9443 /* Fast tracepoints may have additional restrictions on location. */
9444 if (!pending && type_wanted == bp_fast_tracepoint)
9445 {
9446 for (const auto &lsal : canonical.lsals)
9447 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9448 }
9449
9450 /* Verify that condition can be parsed, before setting any
9451 breakpoints. Allocate a separate condition expression for each
9452 breakpoint. */
9453 if (!pending)
9454 {
9455 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9456 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9457
9458 if (parse_extra)
9459 {
9460 char *rest;
9461 char *cond;
9462
9463 const linespec_sals &lsal = canonical.lsals[0];
9464
9465 /* Here we only parse 'arg' to separate condition
9466 from thread number, so parsing in context of first
9467 sal is OK. When setting the breakpoint we'll
9468 re-parse it in context of each sal. */
9469
9470 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9471 &cond, &thread, &task, &rest);
9472 cond_string_copy.reset (cond);
9473 extra_string_copy.reset (rest);
9474 }
9475 else
9476 {
9477 if (type_wanted != bp_dprintf
9478 && extra_string != NULL && *extra_string != '\0')
9479 error (_("Garbage '%s' at end of location"), extra_string);
9480
9481 /* Create a private copy of condition string. */
9482 if (cond_string)
9483 cond_string_copy.reset (xstrdup (cond_string));
9484 /* Create a private copy of any extra string. */
9485 if (extra_string)
9486 extra_string_copy.reset (xstrdup (extra_string));
9487 }
9488
9489 ops->create_breakpoints_sal (gdbarch, &canonical,
9490 std::move (cond_string_copy),
9491 std::move (extra_string_copy),
9492 type_wanted,
9493 tempflag ? disp_del : disp_donttouch,
9494 thread, task, ignore_count, ops,
9495 from_tty, enabled, internal, flags);
9496 }
9497 else
9498 {
9499 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9500
9501 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9502 b->location = copy_event_location (location);
9503
9504 if (parse_extra)
9505 b->cond_string = NULL;
9506 else
9507 {
9508 /* Create a private copy of condition string. */
9509 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9510 b->thread = thread;
9511 }
9512
9513 /* Create a private copy of any extra string. */
9514 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9515 b->ignore_count = ignore_count;
9516 b->disposition = tempflag ? disp_del : disp_donttouch;
9517 b->condition_not_parsed = 1;
9518 b->enable_state = enabled ? bp_enabled : bp_disabled;
9519 if ((type_wanted != bp_breakpoint
9520 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9521 b->pspace = current_program_space;
9522
9523 install_breakpoint (internal, std::move (b), 0);
9524 }
9525
9526 if (canonical.lsals.size () > 1)
9527 {
9528 warning (_("Multiple breakpoints were set.\nUse the "
9529 "\"delete\" command to delete unwanted breakpoints."));
9530 prev_breakpoint_count = prev_bkpt_count;
9531 }
9532
9533 /* That's it. Discard the cleanups for data inserted into the
9534 breakpoint. */
9535 discard_cleanups (bkpt_chain);
9536
9537 /* error call may happen here - have BKPT_CHAIN already discarded. */
9538 update_global_location_list (UGLL_MAY_INSERT);
9539
9540 return 1;
9541 }
9542
9543 /* Set a breakpoint.
9544 ARG is a string describing breakpoint address,
9545 condition, and thread.
9546 FLAG specifies if a breakpoint is hardware on,
9547 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9548 and BP_TEMPFLAG. */
9549
9550 static void
9551 break_command_1 (const char *arg, int flag, int from_tty)
9552 {
9553 int tempflag = flag & BP_TEMPFLAG;
9554 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9555 ? bp_hardware_breakpoint
9556 : bp_breakpoint);
9557 struct breakpoint_ops *ops;
9558
9559 event_location_up location = string_to_event_location (&arg, current_language);
9560
9561 /* Matching breakpoints on probes. */
9562 if (location != NULL
9563 && event_location_type (location.get ()) == PROBE_LOCATION)
9564 ops = &bkpt_probe_breakpoint_ops;
9565 else
9566 ops = &bkpt_breakpoint_ops;
9567
9568 create_breakpoint (get_current_arch (),
9569 location.get (),
9570 NULL, 0, arg, 1 /* parse arg */,
9571 tempflag, type_wanted,
9572 0 /* Ignore count */,
9573 pending_break_support,
9574 ops,
9575 from_tty,
9576 1 /* enabled */,
9577 0 /* internal */,
9578 0);
9579 }
9580
9581 /* Helper function for break_command_1 and disassemble_command. */
9582
9583 void
9584 resolve_sal_pc (struct symtab_and_line *sal)
9585 {
9586 CORE_ADDR pc;
9587
9588 if (sal->pc == 0 && sal->symtab != NULL)
9589 {
9590 if (!find_line_pc (sal->symtab, sal->line, &pc))
9591 error (_("No line %d in file \"%s\"."),
9592 sal->line, symtab_to_filename_for_display (sal->symtab));
9593 sal->pc = pc;
9594
9595 /* If this SAL corresponds to a breakpoint inserted using a line
9596 number, then skip the function prologue if necessary. */
9597 if (sal->explicit_line)
9598 skip_prologue_sal (sal);
9599 }
9600
9601 if (sal->section == 0 && sal->symtab != NULL)
9602 {
9603 const struct blockvector *bv;
9604 const struct block *b;
9605 struct symbol *sym;
9606
9607 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9608 SYMTAB_COMPUNIT (sal->symtab));
9609 if (bv != NULL)
9610 {
9611 sym = block_linkage_function (b);
9612 if (sym != NULL)
9613 {
9614 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9615 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9616 sym);
9617 }
9618 else
9619 {
9620 /* It really is worthwhile to have the section, so we'll
9621 just have to look harder. This case can be executed
9622 if we have line numbers but no functions (as can
9623 happen in assembly source). */
9624
9625 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9626 switch_to_program_space_and_thread (sal->pspace);
9627
9628 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9629 if (msym.minsym)
9630 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9631 }
9632 }
9633 }
9634 }
9635
9636 void
9637 break_command (char *arg, int from_tty)
9638 {
9639 break_command_1 (arg, 0, from_tty);
9640 }
9641
9642 void
9643 tbreak_command (char *arg, int from_tty)
9644 {
9645 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9646 }
9647
9648 static void
9649 hbreak_command (char *arg, int from_tty)
9650 {
9651 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9652 }
9653
9654 static void
9655 thbreak_command (char *arg, int from_tty)
9656 {
9657 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9658 }
9659
9660 static void
9661 stop_command (char *arg, int from_tty)
9662 {
9663 printf_filtered (_("Specify the type of breakpoint to set.\n\
9664 Usage: stop in <function | address>\n\
9665 stop at <line>\n"));
9666 }
9667
9668 static void
9669 stopin_command (const char *arg, int from_tty)
9670 {
9671 int badInput = 0;
9672
9673 if (arg == (char *) NULL)
9674 badInput = 1;
9675 else if (*arg != '*')
9676 {
9677 const char *argptr = arg;
9678 int hasColon = 0;
9679
9680 /* Look for a ':'. If this is a line number specification, then
9681 say it is bad, otherwise, it should be an address or
9682 function/method name. */
9683 while (*argptr && !hasColon)
9684 {
9685 hasColon = (*argptr == ':');
9686 argptr++;
9687 }
9688
9689 if (hasColon)
9690 badInput = (*argptr != ':'); /* Not a class::method */
9691 else
9692 badInput = isdigit (*arg); /* a simple line number */
9693 }
9694
9695 if (badInput)
9696 printf_filtered (_("Usage: stop in <function | address>\n"));
9697 else
9698 break_command_1 (arg, 0, from_tty);
9699 }
9700
9701 static void
9702 stopat_command (const char *arg, int from_tty)
9703 {
9704 int badInput = 0;
9705
9706 if (arg == (char *) NULL || *arg == '*') /* no line number */
9707 badInput = 1;
9708 else
9709 {
9710 const char *argptr = arg;
9711 int hasColon = 0;
9712
9713 /* Look for a ':'. If there is a '::' then get out, otherwise
9714 it is probably a line number. */
9715 while (*argptr && !hasColon)
9716 {
9717 hasColon = (*argptr == ':');
9718 argptr++;
9719 }
9720
9721 if (hasColon)
9722 badInput = (*argptr == ':'); /* we have class::method */
9723 else
9724 badInput = !isdigit (*arg); /* not a line number */
9725 }
9726
9727 if (badInput)
9728 printf_filtered (_("Usage: stop at <line>\n"));
9729 else
9730 break_command_1 (arg, 0, from_tty);
9731 }
9732
9733 /* The dynamic printf command is mostly like a regular breakpoint, but
9734 with a prewired command list consisting of a single output command,
9735 built from extra arguments supplied on the dprintf command
9736 line. */
9737
9738 static void
9739 dprintf_command (char *arg_in, int from_tty)
9740 {
9741 const char *arg = arg_in;
9742 event_location_up location = string_to_event_location (&arg, current_language);
9743
9744 /* If non-NULL, ARG should have been advanced past the location;
9745 the next character must be ','. */
9746 if (arg != NULL)
9747 {
9748 if (arg[0] != ',' || arg[1] == '\0')
9749 error (_("Format string required"));
9750 else
9751 {
9752 /* Skip the comma. */
9753 ++arg;
9754 }
9755 }
9756
9757 create_breakpoint (get_current_arch (),
9758 location.get (),
9759 NULL, 0, arg, 1 /* parse arg */,
9760 0, bp_dprintf,
9761 0 /* Ignore count */,
9762 pending_break_support,
9763 &dprintf_breakpoint_ops,
9764 from_tty,
9765 1 /* enabled */,
9766 0 /* internal */,
9767 0);
9768 }
9769
9770 static void
9771 agent_printf_command (char *arg, int from_tty)
9772 {
9773 error (_("May only run agent-printf on the target"));
9774 }
9775
9776 /* Implement the "breakpoint_hit" breakpoint_ops method for
9777 ranged breakpoints. */
9778
9779 static int
9780 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9781 const address_space *aspace,
9782 CORE_ADDR bp_addr,
9783 const struct target_waitstatus *ws)
9784 {
9785 if (ws->kind != TARGET_WAITKIND_STOPPED
9786 || ws->value.sig != GDB_SIGNAL_TRAP)
9787 return 0;
9788
9789 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9790 bl->length, aspace, bp_addr);
9791 }
9792
9793 /* Implement the "resources_needed" breakpoint_ops method for
9794 ranged breakpoints. */
9795
9796 static int
9797 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9798 {
9799 return target_ranged_break_num_registers ();
9800 }
9801
9802 /* Implement the "print_it" breakpoint_ops method for
9803 ranged breakpoints. */
9804
9805 static enum print_stop_action
9806 print_it_ranged_breakpoint (bpstat bs)
9807 {
9808 struct breakpoint *b = bs->breakpoint_at;
9809 struct bp_location *bl = b->loc;
9810 struct ui_out *uiout = current_uiout;
9811
9812 gdb_assert (b->type == bp_hardware_breakpoint);
9813
9814 /* Ranged breakpoints have only one location. */
9815 gdb_assert (bl && bl->next == NULL);
9816
9817 annotate_breakpoint (b->number);
9818
9819 maybe_print_thread_hit_breakpoint (uiout);
9820
9821 if (b->disposition == disp_del)
9822 uiout->text ("Temporary ranged breakpoint ");
9823 else
9824 uiout->text ("Ranged breakpoint ");
9825 if (uiout->is_mi_like_p ())
9826 {
9827 uiout->field_string ("reason",
9828 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9829 uiout->field_string ("disp", bpdisp_text (b->disposition));
9830 }
9831 uiout->field_int ("bkptno", b->number);
9832 uiout->text (", ");
9833
9834 return PRINT_SRC_AND_LOC;
9835 }
9836
9837 /* Implement the "print_one" breakpoint_ops method for
9838 ranged breakpoints. */
9839
9840 static void
9841 print_one_ranged_breakpoint (struct breakpoint *b,
9842 struct bp_location **last_loc)
9843 {
9844 struct bp_location *bl = b->loc;
9845 struct value_print_options opts;
9846 struct ui_out *uiout = current_uiout;
9847
9848 /* Ranged breakpoints have only one location. */
9849 gdb_assert (bl && bl->next == NULL);
9850
9851 get_user_print_options (&opts);
9852
9853 if (opts.addressprint)
9854 /* We don't print the address range here, it will be printed later
9855 by print_one_detail_ranged_breakpoint. */
9856 uiout->field_skip ("addr");
9857 annotate_field (5);
9858 print_breakpoint_location (b, bl);
9859 *last_loc = bl;
9860 }
9861
9862 /* Implement the "print_one_detail" breakpoint_ops method for
9863 ranged breakpoints. */
9864
9865 static void
9866 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9867 struct ui_out *uiout)
9868 {
9869 CORE_ADDR address_start, address_end;
9870 struct bp_location *bl = b->loc;
9871 string_file stb;
9872
9873 gdb_assert (bl);
9874
9875 address_start = bl->address;
9876 address_end = address_start + bl->length - 1;
9877
9878 uiout->text ("\taddress range: ");
9879 stb.printf ("[%s, %s]",
9880 print_core_address (bl->gdbarch, address_start),
9881 print_core_address (bl->gdbarch, address_end));
9882 uiout->field_stream ("addr", stb);
9883 uiout->text ("\n");
9884 }
9885
9886 /* Implement the "print_mention" breakpoint_ops method for
9887 ranged breakpoints. */
9888
9889 static void
9890 print_mention_ranged_breakpoint (struct breakpoint *b)
9891 {
9892 struct bp_location *bl = b->loc;
9893 struct ui_out *uiout = current_uiout;
9894
9895 gdb_assert (bl);
9896 gdb_assert (b->type == bp_hardware_breakpoint);
9897
9898 if (uiout->is_mi_like_p ())
9899 return;
9900
9901 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9902 b->number, paddress (bl->gdbarch, bl->address),
9903 paddress (bl->gdbarch, bl->address + bl->length - 1));
9904 }
9905
9906 /* Implement the "print_recreate" breakpoint_ops method for
9907 ranged breakpoints. */
9908
9909 static void
9910 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9911 {
9912 fprintf_unfiltered (fp, "break-range %s, %s",
9913 event_location_to_string (b->location.get ()),
9914 event_location_to_string (b->location_range_end.get ()));
9915 print_recreate_thread (b, fp);
9916 }
9917
9918 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9919
9920 static struct breakpoint_ops ranged_breakpoint_ops;
9921
9922 /* Find the address where the end of the breakpoint range should be
9923 placed, given the SAL of the end of the range. This is so that if
9924 the user provides a line number, the end of the range is set to the
9925 last instruction of the given line. */
9926
9927 static CORE_ADDR
9928 find_breakpoint_range_end (struct symtab_and_line sal)
9929 {
9930 CORE_ADDR end;
9931
9932 /* If the user provided a PC value, use it. Otherwise,
9933 find the address of the end of the given location. */
9934 if (sal.explicit_pc)
9935 end = sal.pc;
9936 else
9937 {
9938 int ret;
9939 CORE_ADDR start;
9940
9941 ret = find_line_pc_range (sal, &start, &end);
9942 if (!ret)
9943 error (_("Could not find location of the end of the range."));
9944
9945 /* find_line_pc_range returns the start of the next line. */
9946 end--;
9947 }
9948
9949 return end;
9950 }
9951
9952 /* Implement the "break-range" CLI command. */
9953
9954 static void
9955 break_range_command (char *arg_in, int from_tty)
9956 {
9957 const char *arg = arg_in;
9958 const char *arg_start;
9959 struct linespec_result canonical_start, canonical_end;
9960 int bp_count, can_use_bp, length;
9961 CORE_ADDR end;
9962 struct breakpoint *b;
9963
9964 /* We don't support software ranged breakpoints. */
9965 if (target_ranged_break_num_registers () < 0)
9966 error (_("This target does not support hardware ranged breakpoints."));
9967
9968 bp_count = hw_breakpoint_used_count ();
9969 bp_count += target_ranged_break_num_registers ();
9970 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9971 bp_count, 0);
9972 if (can_use_bp < 0)
9973 error (_("Hardware breakpoints used exceeds limit."));
9974
9975 arg = skip_spaces (arg);
9976 if (arg == NULL || arg[0] == '\0')
9977 error(_("No address range specified."));
9978
9979 arg_start = arg;
9980 event_location_up start_location = string_to_event_location (&arg,
9981 current_language);
9982 parse_breakpoint_sals (start_location.get (), &canonical_start);
9983
9984 if (arg[0] != ',')
9985 error (_("Too few arguments."));
9986 else if (canonical_start.lsals.empty ())
9987 error (_("Could not find location of the beginning of the range."));
9988
9989 const linespec_sals &lsal_start = canonical_start.lsals[0];
9990
9991 if (canonical_start.lsals.size () > 1
9992 || lsal_start.sals.size () != 1)
9993 error (_("Cannot create a ranged breakpoint with multiple locations."));
9994
9995 const symtab_and_line &sal_start = lsal_start.sals[0];
9996 std::string addr_string_start (arg_start, arg - arg_start);
9997
9998 arg++; /* Skip the comma. */
9999 arg = skip_spaces (arg);
10000
10001 /* Parse the end location. */
10002
10003 arg_start = arg;
10004
10005 /* We call decode_line_full directly here instead of using
10006 parse_breakpoint_sals because we need to specify the start location's
10007 symtab and line as the default symtab and line for the end of the
10008 range. This makes it possible to have ranges like "foo.c:27, +14",
10009 where +14 means 14 lines from the start location. */
10010 event_location_up end_location = string_to_event_location (&arg,
10011 current_language);
10012 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10013 sal_start.symtab, sal_start.line,
10014 &canonical_end, NULL, NULL);
10015
10016 if (canonical_end.lsals.empty ())
10017 error (_("Could not find location of the end of the range."));
10018
10019 const linespec_sals &lsal_end = canonical_end.lsals[0];
10020 if (canonical_end.lsals.size () > 1
10021 || lsal_end.sals.size () != 1)
10022 error (_("Cannot create a ranged breakpoint with multiple locations."));
10023
10024 const symtab_and_line &sal_end = lsal_end.sals[0];
10025
10026 end = find_breakpoint_range_end (sal_end);
10027 if (sal_start.pc > end)
10028 error (_("Invalid address range, end precedes start."));
10029
10030 length = end - sal_start.pc + 1;
10031 if (length < 0)
10032 /* Length overflowed. */
10033 error (_("Address range too large."));
10034 else if (length == 1)
10035 {
10036 /* This range is simple enough to be handled by
10037 the `hbreak' command. */
10038 hbreak_command (&addr_string_start[0], 1);
10039
10040 return;
10041 }
10042
10043 /* Now set up the breakpoint. */
10044 b = set_raw_breakpoint (get_current_arch (), sal_start,
10045 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10046 set_breakpoint_count (breakpoint_count + 1);
10047 b->number = breakpoint_count;
10048 b->disposition = disp_donttouch;
10049 b->location = std::move (start_location);
10050 b->location_range_end = std::move (end_location);
10051 b->loc->length = length;
10052
10053 mention (b);
10054 observer_notify_breakpoint_created (b);
10055 update_global_location_list (UGLL_MAY_INSERT);
10056 }
10057
10058 /* Return non-zero if EXP is verified as constant. Returned zero
10059 means EXP is variable. Also the constant detection may fail for
10060 some constant expressions and in such case still falsely return
10061 zero. */
10062
10063 static int
10064 watchpoint_exp_is_const (const struct expression *exp)
10065 {
10066 int i = exp->nelts;
10067
10068 while (i > 0)
10069 {
10070 int oplenp, argsp;
10071
10072 /* We are only interested in the descriptor of each element. */
10073 operator_length (exp, i, &oplenp, &argsp);
10074 i -= oplenp;
10075
10076 switch (exp->elts[i].opcode)
10077 {
10078 case BINOP_ADD:
10079 case BINOP_SUB:
10080 case BINOP_MUL:
10081 case BINOP_DIV:
10082 case BINOP_REM:
10083 case BINOP_MOD:
10084 case BINOP_LSH:
10085 case BINOP_RSH:
10086 case BINOP_LOGICAL_AND:
10087 case BINOP_LOGICAL_OR:
10088 case BINOP_BITWISE_AND:
10089 case BINOP_BITWISE_IOR:
10090 case BINOP_BITWISE_XOR:
10091 case BINOP_EQUAL:
10092 case BINOP_NOTEQUAL:
10093 case BINOP_LESS:
10094 case BINOP_GTR:
10095 case BINOP_LEQ:
10096 case BINOP_GEQ:
10097 case BINOP_REPEAT:
10098 case BINOP_COMMA:
10099 case BINOP_EXP:
10100 case BINOP_MIN:
10101 case BINOP_MAX:
10102 case BINOP_INTDIV:
10103 case BINOP_CONCAT:
10104 case TERNOP_COND:
10105 case TERNOP_SLICE:
10106
10107 case OP_LONG:
10108 case OP_FLOAT:
10109 case OP_LAST:
10110 case OP_COMPLEX:
10111 case OP_STRING:
10112 case OP_ARRAY:
10113 case OP_TYPE:
10114 case OP_TYPEOF:
10115 case OP_DECLTYPE:
10116 case OP_TYPEID:
10117 case OP_NAME:
10118 case OP_OBJC_NSSTRING:
10119
10120 case UNOP_NEG:
10121 case UNOP_LOGICAL_NOT:
10122 case UNOP_COMPLEMENT:
10123 case UNOP_ADDR:
10124 case UNOP_HIGH:
10125 case UNOP_CAST:
10126
10127 case UNOP_CAST_TYPE:
10128 case UNOP_REINTERPRET_CAST:
10129 case UNOP_DYNAMIC_CAST:
10130 /* Unary, binary and ternary operators: We have to check
10131 their operands. If they are constant, then so is the
10132 result of that operation. For instance, if A and B are
10133 determined to be constants, then so is "A + B".
10134
10135 UNOP_IND is one exception to the rule above, because the
10136 value of *ADDR is not necessarily a constant, even when
10137 ADDR is. */
10138 break;
10139
10140 case OP_VAR_VALUE:
10141 /* Check whether the associated symbol is a constant.
10142
10143 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10144 possible that a buggy compiler could mark a variable as
10145 constant even when it is not, and TYPE_CONST would return
10146 true in this case, while SYMBOL_CLASS wouldn't.
10147
10148 We also have to check for function symbols because they
10149 are always constant. */
10150 {
10151 struct symbol *s = exp->elts[i + 2].symbol;
10152
10153 if (SYMBOL_CLASS (s) != LOC_BLOCK
10154 && SYMBOL_CLASS (s) != LOC_CONST
10155 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10156 return 0;
10157 break;
10158 }
10159
10160 /* The default action is to return 0 because we are using
10161 the optimistic approach here: If we don't know something,
10162 then it is not a constant. */
10163 default:
10164 return 0;
10165 }
10166 }
10167
10168 return 1;
10169 }
10170
10171 /* Watchpoint destructor. */
10172
10173 watchpoint::~watchpoint ()
10174 {
10175 xfree (this->exp_string);
10176 xfree (this->exp_string_reparse);
10177 value_free (this->val);
10178 }
10179
10180 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10181
10182 static void
10183 re_set_watchpoint (struct breakpoint *b)
10184 {
10185 struct watchpoint *w = (struct watchpoint *) b;
10186
10187 /* Watchpoint can be either on expression using entirely global
10188 variables, or it can be on local variables.
10189
10190 Watchpoints of the first kind are never auto-deleted, and even
10191 persist across program restarts. Since they can use variables
10192 from shared libraries, we need to reparse expression as libraries
10193 are loaded and unloaded.
10194
10195 Watchpoints on local variables can also change meaning as result
10196 of solib event. For example, if a watchpoint uses both a local
10197 and a global variables in expression, it's a local watchpoint,
10198 but unloading of a shared library will make the expression
10199 invalid. This is not a very common use case, but we still
10200 re-evaluate expression, to avoid surprises to the user.
10201
10202 Note that for local watchpoints, we re-evaluate it only if
10203 watchpoints frame id is still valid. If it's not, it means the
10204 watchpoint is out of scope and will be deleted soon. In fact,
10205 I'm not sure we'll ever be called in this case.
10206
10207 If a local watchpoint's frame id is still valid, then
10208 w->exp_valid_block is likewise valid, and we can safely use it.
10209
10210 Don't do anything about disabled watchpoints, since they will be
10211 reevaluated again when enabled. */
10212 update_watchpoint (w, 1 /* reparse */);
10213 }
10214
10215 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10216
10217 static int
10218 insert_watchpoint (struct bp_location *bl)
10219 {
10220 struct watchpoint *w = (struct watchpoint *) bl->owner;
10221 int length = w->exact ? 1 : bl->length;
10222
10223 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10224 w->cond_exp.get ());
10225 }
10226
10227 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10228
10229 static int
10230 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10231 {
10232 struct watchpoint *w = (struct watchpoint *) bl->owner;
10233 int length = w->exact ? 1 : bl->length;
10234
10235 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10236 w->cond_exp.get ());
10237 }
10238
10239 static int
10240 breakpoint_hit_watchpoint (const struct bp_location *bl,
10241 const address_space *aspace, CORE_ADDR bp_addr,
10242 const struct target_waitstatus *ws)
10243 {
10244 struct breakpoint *b = bl->owner;
10245 struct watchpoint *w = (struct watchpoint *) b;
10246
10247 /* Continuable hardware watchpoints are treated as non-existent if the
10248 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10249 some data address). Otherwise gdb won't stop on a break instruction
10250 in the code (not from a breakpoint) when a hardware watchpoint has
10251 been defined. Also skip watchpoints which we know did not trigger
10252 (did not match the data address). */
10253 if (is_hardware_watchpoint (b)
10254 && w->watchpoint_triggered == watch_triggered_no)
10255 return 0;
10256
10257 return 1;
10258 }
10259
10260 static void
10261 check_status_watchpoint (bpstat bs)
10262 {
10263 gdb_assert (is_watchpoint (bs->breakpoint_at));
10264
10265 bpstat_check_watchpoint (bs);
10266 }
10267
10268 /* Implement the "resources_needed" breakpoint_ops method for
10269 hardware watchpoints. */
10270
10271 static int
10272 resources_needed_watchpoint (const struct bp_location *bl)
10273 {
10274 struct watchpoint *w = (struct watchpoint *) bl->owner;
10275 int length = w->exact? 1 : bl->length;
10276
10277 return target_region_ok_for_hw_watchpoint (bl->address, length);
10278 }
10279
10280 /* Implement the "works_in_software_mode" breakpoint_ops method for
10281 hardware watchpoints. */
10282
10283 static int
10284 works_in_software_mode_watchpoint (const struct breakpoint *b)
10285 {
10286 /* Read and access watchpoints only work with hardware support. */
10287 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10288 }
10289
10290 static enum print_stop_action
10291 print_it_watchpoint (bpstat bs)
10292 {
10293 struct breakpoint *b;
10294 enum print_stop_action result;
10295 struct watchpoint *w;
10296 struct ui_out *uiout = current_uiout;
10297
10298 gdb_assert (bs->bp_location_at != NULL);
10299
10300 b = bs->breakpoint_at;
10301 w = (struct watchpoint *) b;
10302
10303 annotate_watchpoint (b->number);
10304 maybe_print_thread_hit_breakpoint (uiout);
10305
10306 string_file stb;
10307
10308 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10309 switch (b->type)
10310 {
10311 case bp_watchpoint:
10312 case bp_hardware_watchpoint:
10313 if (uiout->is_mi_like_p ())
10314 uiout->field_string
10315 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10316 mention (b);
10317 tuple_emitter.emplace (uiout, "value");
10318 uiout->text ("\nOld value = ");
10319 watchpoint_value_print (bs->old_val, &stb);
10320 uiout->field_stream ("old", stb);
10321 uiout->text ("\nNew value = ");
10322 watchpoint_value_print (w->val, &stb);
10323 uiout->field_stream ("new", stb);
10324 uiout->text ("\n");
10325 /* More than one watchpoint may have been triggered. */
10326 result = PRINT_UNKNOWN;
10327 break;
10328
10329 case bp_read_watchpoint:
10330 if (uiout->is_mi_like_p ())
10331 uiout->field_string
10332 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10333 mention (b);
10334 tuple_emitter.emplace (uiout, "value");
10335 uiout->text ("\nValue = ");
10336 watchpoint_value_print (w->val, &stb);
10337 uiout->field_stream ("value", stb);
10338 uiout->text ("\n");
10339 result = PRINT_UNKNOWN;
10340 break;
10341
10342 case bp_access_watchpoint:
10343 if (bs->old_val != NULL)
10344 {
10345 if (uiout->is_mi_like_p ())
10346 uiout->field_string
10347 ("reason",
10348 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10349 mention (b);
10350 tuple_emitter.emplace (uiout, "value");
10351 uiout->text ("\nOld value = ");
10352 watchpoint_value_print (bs->old_val, &stb);
10353 uiout->field_stream ("old", stb);
10354 uiout->text ("\nNew value = ");
10355 }
10356 else
10357 {
10358 mention (b);
10359 if (uiout->is_mi_like_p ())
10360 uiout->field_string
10361 ("reason",
10362 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10363 tuple_emitter.emplace (uiout, "value");
10364 uiout->text ("\nValue = ");
10365 }
10366 watchpoint_value_print (w->val, &stb);
10367 uiout->field_stream ("new", stb);
10368 uiout->text ("\n");
10369 result = PRINT_UNKNOWN;
10370 break;
10371 default:
10372 result = PRINT_UNKNOWN;
10373 }
10374
10375 return result;
10376 }
10377
10378 /* Implement the "print_mention" breakpoint_ops method for hardware
10379 watchpoints. */
10380
10381 static void
10382 print_mention_watchpoint (struct breakpoint *b)
10383 {
10384 struct watchpoint *w = (struct watchpoint *) b;
10385 struct ui_out *uiout = current_uiout;
10386 const char *tuple_name;
10387
10388 switch (b->type)
10389 {
10390 case bp_watchpoint:
10391 uiout->text ("Watchpoint ");
10392 tuple_name = "wpt";
10393 break;
10394 case bp_hardware_watchpoint:
10395 uiout->text ("Hardware watchpoint ");
10396 tuple_name = "wpt";
10397 break;
10398 case bp_read_watchpoint:
10399 uiout->text ("Hardware read watchpoint ");
10400 tuple_name = "hw-rwpt";
10401 break;
10402 case bp_access_watchpoint:
10403 uiout->text ("Hardware access (read/write) watchpoint ");
10404 tuple_name = "hw-awpt";
10405 break;
10406 default:
10407 internal_error (__FILE__, __LINE__,
10408 _("Invalid hardware watchpoint type."));
10409 }
10410
10411 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10412 uiout->field_int ("number", b->number);
10413 uiout->text (": ");
10414 uiout->field_string ("exp", w->exp_string);
10415 }
10416
10417 /* Implement the "print_recreate" breakpoint_ops method for
10418 watchpoints. */
10419
10420 static void
10421 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10422 {
10423 struct watchpoint *w = (struct watchpoint *) b;
10424
10425 switch (b->type)
10426 {
10427 case bp_watchpoint:
10428 case bp_hardware_watchpoint:
10429 fprintf_unfiltered (fp, "watch");
10430 break;
10431 case bp_read_watchpoint:
10432 fprintf_unfiltered (fp, "rwatch");
10433 break;
10434 case bp_access_watchpoint:
10435 fprintf_unfiltered (fp, "awatch");
10436 break;
10437 default:
10438 internal_error (__FILE__, __LINE__,
10439 _("Invalid watchpoint type."));
10440 }
10441
10442 fprintf_unfiltered (fp, " %s", w->exp_string);
10443 print_recreate_thread (b, fp);
10444 }
10445
10446 /* Implement the "explains_signal" breakpoint_ops method for
10447 watchpoints. */
10448
10449 static int
10450 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10451 {
10452 /* A software watchpoint cannot cause a signal other than
10453 GDB_SIGNAL_TRAP. */
10454 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10455 return 0;
10456
10457 return 1;
10458 }
10459
10460 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10461
10462 static struct breakpoint_ops watchpoint_breakpoint_ops;
10463
10464 /* Implement the "insert" breakpoint_ops method for
10465 masked hardware watchpoints. */
10466
10467 static int
10468 insert_masked_watchpoint (struct bp_location *bl)
10469 {
10470 struct watchpoint *w = (struct watchpoint *) bl->owner;
10471
10472 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10473 bl->watchpoint_type);
10474 }
10475
10476 /* Implement the "remove" breakpoint_ops method for
10477 masked hardware watchpoints. */
10478
10479 static int
10480 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10481 {
10482 struct watchpoint *w = (struct watchpoint *) bl->owner;
10483
10484 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10485 bl->watchpoint_type);
10486 }
10487
10488 /* Implement the "resources_needed" breakpoint_ops method for
10489 masked hardware watchpoints. */
10490
10491 static int
10492 resources_needed_masked_watchpoint (const struct bp_location *bl)
10493 {
10494 struct watchpoint *w = (struct watchpoint *) bl->owner;
10495
10496 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10497 }
10498
10499 /* Implement the "works_in_software_mode" breakpoint_ops method for
10500 masked hardware watchpoints. */
10501
10502 static int
10503 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10504 {
10505 return 0;
10506 }
10507
10508 /* Implement the "print_it" breakpoint_ops method for
10509 masked hardware watchpoints. */
10510
10511 static enum print_stop_action
10512 print_it_masked_watchpoint (bpstat bs)
10513 {
10514 struct breakpoint *b = bs->breakpoint_at;
10515 struct ui_out *uiout = current_uiout;
10516
10517 /* Masked watchpoints have only one location. */
10518 gdb_assert (b->loc && b->loc->next == NULL);
10519
10520 annotate_watchpoint (b->number);
10521 maybe_print_thread_hit_breakpoint (uiout);
10522
10523 switch (b->type)
10524 {
10525 case bp_hardware_watchpoint:
10526 if (uiout->is_mi_like_p ())
10527 uiout->field_string
10528 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10529 break;
10530
10531 case bp_read_watchpoint:
10532 if (uiout->is_mi_like_p ())
10533 uiout->field_string
10534 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10535 break;
10536
10537 case bp_access_watchpoint:
10538 if (uiout->is_mi_like_p ())
10539 uiout->field_string
10540 ("reason",
10541 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10542 break;
10543 default:
10544 internal_error (__FILE__, __LINE__,
10545 _("Invalid hardware watchpoint type."));
10546 }
10547
10548 mention (b);
10549 uiout->text (_("\n\
10550 Check the underlying instruction at PC for the memory\n\
10551 address and value which triggered this watchpoint.\n"));
10552 uiout->text ("\n");
10553
10554 /* More than one watchpoint may have been triggered. */
10555 return PRINT_UNKNOWN;
10556 }
10557
10558 /* Implement the "print_one_detail" breakpoint_ops method for
10559 masked hardware watchpoints. */
10560
10561 static void
10562 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10563 struct ui_out *uiout)
10564 {
10565 struct watchpoint *w = (struct watchpoint *) b;
10566
10567 /* Masked watchpoints have only one location. */
10568 gdb_assert (b->loc && b->loc->next == NULL);
10569
10570 uiout->text ("\tmask ");
10571 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10572 uiout->text ("\n");
10573 }
10574
10575 /* Implement the "print_mention" breakpoint_ops method for
10576 masked hardware watchpoints. */
10577
10578 static void
10579 print_mention_masked_watchpoint (struct breakpoint *b)
10580 {
10581 struct watchpoint *w = (struct watchpoint *) b;
10582 struct ui_out *uiout = current_uiout;
10583 const char *tuple_name;
10584
10585 switch (b->type)
10586 {
10587 case bp_hardware_watchpoint:
10588 uiout->text ("Masked hardware watchpoint ");
10589 tuple_name = "wpt";
10590 break;
10591 case bp_read_watchpoint:
10592 uiout->text ("Masked hardware read watchpoint ");
10593 tuple_name = "hw-rwpt";
10594 break;
10595 case bp_access_watchpoint:
10596 uiout->text ("Masked hardware access (read/write) watchpoint ");
10597 tuple_name = "hw-awpt";
10598 break;
10599 default:
10600 internal_error (__FILE__, __LINE__,
10601 _("Invalid hardware watchpoint type."));
10602 }
10603
10604 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10605 uiout->field_int ("number", b->number);
10606 uiout->text (": ");
10607 uiout->field_string ("exp", w->exp_string);
10608 }
10609
10610 /* Implement the "print_recreate" breakpoint_ops method for
10611 masked hardware watchpoints. */
10612
10613 static void
10614 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10615 {
10616 struct watchpoint *w = (struct watchpoint *) b;
10617 char tmp[40];
10618
10619 switch (b->type)
10620 {
10621 case bp_hardware_watchpoint:
10622 fprintf_unfiltered (fp, "watch");
10623 break;
10624 case bp_read_watchpoint:
10625 fprintf_unfiltered (fp, "rwatch");
10626 break;
10627 case bp_access_watchpoint:
10628 fprintf_unfiltered (fp, "awatch");
10629 break;
10630 default:
10631 internal_error (__FILE__, __LINE__,
10632 _("Invalid hardware watchpoint type."));
10633 }
10634
10635 sprintf_vma (tmp, w->hw_wp_mask);
10636 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10637 print_recreate_thread (b, fp);
10638 }
10639
10640 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10641
10642 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10643
10644 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10645
10646 static int
10647 is_masked_watchpoint (const struct breakpoint *b)
10648 {
10649 return b->ops == &masked_watchpoint_breakpoint_ops;
10650 }
10651
10652 /* accessflag: hw_write: watch write,
10653 hw_read: watch read,
10654 hw_access: watch access (read or write) */
10655 static void
10656 watch_command_1 (const char *arg, int accessflag, int from_tty,
10657 int just_location, int internal)
10658 {
10659 struct breakpoint *scope_breakpoint = NULL;
10660 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10661 struct value *val, *mark, *result;
10662 int saved_bitpos = 0, saved_bitsize = 0;
10663 const char *exp_start = NULL;
10664 const char *exp_end = NULL;
10665 const char *tok, *end_tok;
10666 int toklen = -1;
10667 const char *cond_start = NULL;
10668 const char *cond_end = NULL;
10669 enum bptype bp_type;
10670 int thread = -1;
10671 int pc = 0;
10672 /* Flag to indicate whether we are going to use masks for
10673 the hardware watchpoint. */
10674 int use_mask = 0;
10675 CORE_ADDR mask = 0;
10676
10677 /* Make sure that we actually have parameters to parse. */
10678 if (arg != NULL && arg[0] != '\0')
10679 {
10680 const char *value_start;
10681
10682 exp_end = arg + strlen (arg);
10683
10684 /* Look for "parameter value" pairs at the end
10685 of the arguments string. */
10686 for (tok = exp_end - 1; tok > arg; tok--)
10687 {
10688 /* Skip whitespace at the end of the argument list. */
10689 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10690 tok--;
10691
10692 /* Find the beginning of the last token.
10693 This is the value of the parameter. */
10694 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10695 tok--;
10696 value_start = tok + 1;
10697
10698 /* Skip whitespace. */
10699 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10700 tok--;
10701
10702 end_tok = tok;
10703
10704 /* Find the beginning of the second to last token.
10705 This is the parameter itself. */
10706 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10707 tok--;
10708 tok++;
10709 toklen = end_tok - tok + 1;
10710
10711 if (toklen == 6 && startswith (tok, "thread"))
10712 {
10713 struct thread_info *thr;
10714 /* At this point we've found a "thread" token, which means
10715 the user is trying to set a watchpoint that triggers
10716 only in a specific thread. */
10717 const char *endp;
10718
10719 if (thread != -1)
10720 error(_("You can specify only one thread."));
10721
10722 /* Extract the thread ID from the next token. */
10723 thr = parse_thread_id (value_start, &endp);
10724
10725 /* Check if the user provided a valid thread ID. */
10726 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10727 invalid_thread_id_error (value_start);
10728
10729 thread = thr->global_num;
10730 }
10731 else if (toklen == 4 && startswith (tok, "mask"))
10732 {
10733 /* We've found a "mask" token, which means the user wants to
10734 create a hardware watchpoint that is going to have the mask
10735 facility. */
10736 struct value *mask_value, *mark;
10737
10738 if (use_mask)
10739 error(_("You can specify only one mask."));
10740
10741 use_mask = just_location = 1;
10742
10743 mark = value_mark ();
10744 mask_value = parse_to_comma_and_eval (&value_start);
10745 mask = value_as_address (mask_value);
10746 value_free_to_mark (mark);
10747 }
10748 else
10749 /* We didn't recognize what we found. We should stop here. */
10750 break;
10751
10752 /* Truncate the string and get rid of the "parameter value" pair before
10753 the arguments string is parsed by the parse_exp_1 function. */
10754 exp_end = tok;
10755 }
10756 }
10757 else
10758 exp_end = arg;
10759
10760 /* Parse the rest of the arguments. From here on out, everything
10761 is in terms of a newly allocated string instead of the original
10762 ARG. */
10763 innermost_block = NULL;
10764 std::string expression (arg, exp_end - arg);
10765 exp_start = arg = expression.c_str ();
10766 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
10767 exp_end = arg;
10768 /* Remove trailing whitespace from the expression before saving it.
10769 This makes the eventual display of the expression string a bit
10770 prettier. */
10771 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10772 --exp_end;
10773
10774 /* Checking if the expression is not constant. */
10775 if (watchpoint_exp_is_const (exp.get ()))
10776 {
10777 int len;
10778
10779 len = exp_end - exp_start;
10780 while (len > 0 && isspace (exp_start[len - 1]))
10781 len--;
10782 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10783 }
10784
10785 exp_valid_block = innermost_block;
10786 mark = value_mark ();
10787 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
10788
10789 if (val != NULL && just_location)
10790 {
10791 saved_bitpos = value_bitpos (val);
10792 saved_bitsize = value_bitsize (val);
10793 }
10794
10795 if (just_location)
10796 {
10797 int ret;
10798
10799 exp_valid_block = NULL;
10800 val = value_addr (result);
10801 release_value (val);
10802 value_free_to_mark (mark);
10803
10804 if (use_mask)
10805 {
10806 ret = target_masked_watch_num_registers (value_as_address (val),
10807 mask);
10808 if (ret == -1)
10809 error (_("This target does not support masked watchpoints."));
10810 else if (ret == -2)
10811 error (_("Invalid mask or memory region."));
10812 }
10813 }
10814 else if (val != NULL)
10815 release_value (val);
10816
10817 tok = skip_spaces (arg);
10818 end_tok = skip_to_space (tok);
10819
10820 toklen = end_tok - tok;
10821 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10822 {
10823 innermost_block = NULL;
10824 tok = cond_start = end_tok + 1;
10825 parse_exp_1 (&tok, 0, 0, 0);
10826
10827 /* The watchpoint expression may not be local, but the condition
10828 may still be. E.g.: `watch global if local > 0'. */
10829 cond_exp_valid_block = innermost_block;
10830
10831 cond_end = tok;
10832 }
10833 if (*tok)
10834 error (_("Junk at end of command."));
10835
10836 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10837
10838 /* Save this because create_internal_breakpoint below invalidates
10839 'wp_frame'. */
10840 frame_id watchpoint_frame = get_frame_id (wp_frame);
10841
10842 /* If the expression is "local", then set up a "watchpoint scope"
10843 breakpoint at the point where we've left the scope of the watchpoint
10844 expression. Create the scope breakpoint before the watchpoint, so
10845 that we will encounter it first in bpstat_stop_status. */
10846 if (exp_valid_block != NULL && wp_frame != NULL)
10847 {
10848 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10849
10850 if (frame_id_p (caller_frame_id))
10851 {
10852 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10853 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10854
10855 scope_breakpoint
10856 = create_internal_breakpoint (caller_arch, caller_pc,
10857 bp_watchpoint_scope,
10858 &momentary_breakpoint_ops);
10859
10860 /* create_internal_breakpoint could invalidate WP_FRAME. */
10861 wp_frame = NULL;
10862
10863 scope_breakpoint->enable_state = bp_enabled;
10864
10865 /* Automatically delete the breakpoint when it hits. */
10866 scope_breakpoint->disposition = disp_del;
10867
10868 /* Only break in the proper frame (help with recursion). */
10869 scope_breakpoint->frame_id = caller_frame_id;
10870
10871 /* Set the address at which we will stop. */
10872 scope_breakpoint->loc->gdbarch = caller_arch;
10873 scope_breakpoint->loc->requested_address = caller_pc;
10874 scope_breakpoint->loc->address
10875 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10876 scope_breakpoint->loc->requested_address,
10877 scope_breakpoint->type);
10878 }
10879 }
10880
10881 /* Now set up the breakpoint. We create all watchpoints as hardware
10882 watchpoints here even if hardware watchpoints are turned off, a call
10883 to update_watchpoint later in this function will cause the type to
10884 drop back to bp_watchpoint (software watchpoint) if required. */
10885
10886 if (accessflag == hw_read)
10887 bp_type = bp_read_watchpoint;
10888 else if (accessflag == hw_access)
10889 bp_type = bp_access_watchpoint;
10890 else
10891 bp_type = bp_hardware_watchpoint;
10892
10893 std::unique_ptr<watchpoint> w (new watchpoint ());
10894
10895 if (use_mask)
10896 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10897 &masked_watchpoint_breakpoint_ops);
10898 else
10899 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10900 &watchpoint_breakpoint_ops);
10901 w->thread = thread;
10902 w->disposition = disp_donttouch;
10903 w->pspace = current_program_space;
10904 w->exp = std::move (exp);
10905 w->exp_valid_block = exp_valid_block;
10906 w->cond_exp_valid_block = cond_exp_valid_block;
10907 if (just_location)
10908 {
10909 struct type *t = value_type (val);
10910 CORE_ADDR addr = value_as_address (val);
10911
10912 w->exp_string_reparse
10913 = current_language->la_watch_location_expression (t, addr).release ();
10914
10915 w->exp_string = xstrprintf ("-location %.*s",
10916 (int) (exp_end - exp_start), exp_start);
10917 }
10918 else
10919 w->exp_string = savestring (exp_start, exp_end - exp_start);
10920
10921 if (use_mask)
10922 {
10923 w->hw_wp_mask = mask;
10924 }
10925 else
10926 {
10927 w->val = val;
10928 w->val_bitpos = saved_bitpos;
10929 w->val_bitsize = saved_bitsize;
10930 w->val_valid = 1;
10931 }
10932
10933 if (cond_start)
10934 w->cond_string = savestring (cond_start, cond_end - cond_start);
10935 else
10936 w->cond_string = 0;
10937
10938 if (frame_id_p (watchpoint_frame))
10939 {
10940 w->watchpoint_frame = watchpoint_frame;
10941 w->watchpoint_thread = inferior_ptid;
10942 }
10943 else
10944 {
10945 w->watchpoint_frame = null_frame_id;
10946 w->watchpoint_thread = null_ptid;
10947 }
10948
10949 if (scope_breakpoint != NULL)
10950 {
10951 /* The scope breakpoint is related to the watchpoint. We will
10952 need to act on them together. */
10953 w->related_breakpoint = scope_breakpoint;
10954 scope_breakpoint->related_breakpoint = w.get ();
10955 }
10956
10957 if (!just_location)
10958 value_free_to_mark (mark);
10959
10960 /* Finally update the new watchpoint. This creates the locations
10961 that should be inserted. */
10962 update_watchpoint (w.get (), 1);
10963
10964 install_breakpoint (internal, std::move (w), 1);
10965 }
10966
10967 /* Return count of debug registers needed to watch the given expression.
10968 If the watchpoint cannot be handled in hardware return zero. */
10969
10970 static int
10971 can_use_hardware_watchpoint (struct value *v)
10972 {
10973 int found_memory_cnt = 0;
10974 struct value *head = v;
10975
10976 /* Did the user specifically forbid us to use hardware watchpoints? */
10977 if (!can_use_hw_watchpoints)
10978 return 0;
10979
10980 /* Make sure that the value of the expression depends only upon
10981 memory contents, and values computed from them within GDB. If we
10982 find any register references or function calls, we can't use a
10983 hardware watchpoint.
10984
10985 The idea here is that evaluating an expression generates a series
10986 of values, one holding the value of every subexpression. (The
10987 expression a*b+c has five subexpressions: a, b, a*b, c, and
10988 a*b+c.) GDB's values hold almost enough information to establish
10989 the criteria given above --- they identify memory lvalues,
10990 register lvalues, computed values, etcetera. So we can evaluate
10991 the expression, and then scan the chain of values that leaves
10992 behind to decide whether we can detect any possible change to the
10993 expression's final value using only hardware watchpoints.
10994
10995 However, I don't think that the values returned by inferior
10996 function calls are special in any way. So this function may not
10997 notice that an expression involving an inferior function call
10998 can't be watched with hardware watchpoints. FIXME. */
10999 for (; v; v = value_next (v))
11000 {
11001 if (VALUE_LVAL (v) == lval_memory)
11002 {
11003 if (v != head && value_lazy (v))
11004 /* A lazy memory lvalue in the chain is one that GDB never
11005 needed to fetch; we either just used its address (e.g.,
11006 `a' in `a.b') or we never needed it at all (e.g., `a'
11007 in `a,b'). This doesn't apply to HEAD; if that is
11008 lazy then it was not readable, but watch it anyway. */
11009 ;
11010 else
11011 {
11012 /* Ahh, memory we actually used! Check if we can cover
11013 it with hardware watchpoints. */
11014 struct type *vtype = check_typedef (value_type (v));
11015
11016 /* We only watch structs and arrays if user asked for it
11017 explicitly, never if they just happen to appear in a
11018 middle of some value chain. */
11019 if (v == head
11020 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11021 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11022 {
11023 CORE_ADDR vaddr = value_address (v);
11024 int len;
11025 int num_regs;
11026
11027 len = (target_exact_watchpoints
11028 && is_scalar_type_recursive (vtype))?
11029 1 : TYPE_LENGTH (value_type (v));
11030
11031 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11032 if (!num_regs)
11033 return 0;
11034 else
11035 found_memory_cnt += num_regs;
11036 }
11037 }
11038 }
11039 else if (VALUE_LVAL (v) != not_lval
11040 && deprecated_value_modifiable (v) == 0)
11041 return 0; /* These are values from the history (e.g., $1). */
11042 else if (VALUE_LVAL (v) == lval_register)
11043 return 0; /* Cannot watch a register with a HW watchpoint. */
11044 }
11045
11046 /* The expression itself looks suitable for using a hardware
11047 watchpoint, but give the target machine a chance to reject it. */
11048 return found_memory_cnt;
11049 }
11050
11051 void
11052 watch_command_wrapper (const char *arg, int from_tty, int internal)
11053 {
11054 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11055 }
11056
11057 /* A helper function that looks for the "-location" argument and then
11058 calls watch_command_1. */
11059
11060 static void
11061 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11062 {
11063 int just_location = 0;
11064
11065 if (arg
11066 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11067 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11068 {
11069 arg = skip_spaces (arg);
11070 just_location = 1;
11071 }
11072
11073 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11074 }
11075
11076 static void
11077 watch_command (char *arg, int from_tty)
11078 {
11079 watch_maybe_just_location (arg, hw_write, from_tty);
11080 }
11081
11082 void
11083 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
11084 {
11085 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11086 }
11087
11088 static void
11089 rwatch_command (char *arg, int from_tty)
11090 {
11091 watch_maybe_just_location (arg, hw_read, from_tty);
11092 }
11093
11094 void
11095 awatch_command_wrapper (const char *arg, int from_tty, int internal)
11096 {
11097 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11098 }
11099
11100 static void
11101 awatch_command (char *arg, int from_tty)
11102 {
11103 watch_maybe_just_location (arg, hw_access, from_tty);
11104 }
11105 \f
11106
11107 /* Data for the FSM that manages the until(location)/advance commands
11108 in infcmd.c. Here because it uses the mechanisms of
11109 breakpoints. */
11110
11111 struct until_break_fsm
11112 {
11113 /* The base class. */
11114 struct thread_fsm thread_fsm;
11115
11116 /* The thread that as current when the command was executed. */
11117 int thread;
11118
11119 /* The breakpoint set at the destination location. */
11120 struct breakpoint *location_breakpoint;
11121
11122 /* Breakpoint set at the return address in the caller frame. May be
11123 NULL. */
11124 struct breakpoint *caller_breakpoint;
11125 };
11126
11127 static void until_break_fsm_clean_up (struct thread_fsm *self,
11128 struct thread_info *thread);
11129 static int until_break_fsm_should_stop (struct thread_fsm *self,
11130 struct thread_info *thread);
11131 static enum async_reply_reason
11132 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11133
11134 /* until_break_fsm's vtable. */
11135
11136 static struct thread_fsm_ops until_break_fsm_ops =
11137 {
11138 NULL, /* dtor */
11139 until_break_fsm_clean_up,
11140 until_break_fsm_should_stop,
11141 NULL, /* return_value */
11142 until_break_fsm_async_reply_reason,
11143 };
11144
11145 /* Allocate a new until_break_command_fsm. */
11146
11147 static struct until_break_fsm *
11148 new_until_break_fsm (struct interp *cmd_interp, int thread,
11149 breakpoint_up &&location_breakpoint,
11150 breakpoint_up &&caller_breakpoint)
11151 {
11152 struct until_break_fsm *sm;
11153
11154 sm = XCNEW (struct until_break_fsm);
11155 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11156
11157 sm->thread = thread;
11158 sm->location_breakpoint = location_breakpoint.release ();
11159 sm->caller_breakpoint = caller_breakpoint.release ();
11160
11161 return sm;
11162 }
11163
11164 /* Implementation of the 'should_stop' FSM method for the
11165 until(location)/advance commands. */
11166
11167 static int
11168 until_break_fsm_should_stop (struct thread_fsm *self,
11169 struct thread_info *tp)
11170 {
11171 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11172
11173 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11174 sm->location_breakpoint) != NULL
11175 || (sm->caller_breakpoint != NULL
11176 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11177 sm->caller_breakpoint) != NULL))
11178 thread_fsm_set_finished (self);
11179
11180 return 1;
11181 }
11182
11183 /* Implementation of the 'clean_up' FSM method for the
11184 until(location)/advance commands. */
11185
11186 static void
11187 until_break_fsm_clean_up (struct thread_fsm *self,
11188 struct thread_info *thread)
11189 {
11190 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11191
11192 /* Clean up our temporary breakpoints. */
11193 if (sm->location_breakpoint != NULL)
11194 {
11195 delete_breakpoint (sm->location_breakpoint);
11196 sm->location_breakpoint = NULL;
11197 }
11198 if (sm->caller_breakpoint != NULL)
11199 {
11200 delete_breakpoint (sm->caller_breakpoint);
11201 sm->caller_breakpoint = NULL;
11202 }
11203 delete_longjmp_breakpoint (sm->thread);
11204 }
11205
11206 /* Implementation of the 'async_reply_reason' FSM method for the
11207 until(location)/advance commands. */
11208
11209 static enum async_reply_reason
11210 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11211 {
11212 return EXEC_ASYNC_LOCATION_REACHED;
11213 }
11214
11215 void
11216 until_break_command (const char *arg, int from_tty, int anywhere)
11217 {
11218 struct frame_info *frame;
11219 struct gdbarch *frame_gdbarch;
11220 struct frame_id stack_frame_id;
11221 struct frame_id caller_frame_id;
11222 struct cleanup *old_chain;
11223 int thread;
11224 struct thread_info *tp;
11225 struct until_break_fsm *sm;
11226
11227 clear_proceed_status (0);
11228
11229 /* Set a breakpoint where the user wants it and at return from
11230 this function. */
11231
11232 event_location_up location = string_to_event_location (&arg, current_language);
11233
11234 std::vector<symtab_and_line> sals
11235 = (last_displayed_sal_is_valid ()
11236 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11237 get_last_displayed_symtab (),
11238 get_last_displayed_line ())
11239 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11240 NULL, (struct symtab *) NULL, 0));
11241
11242 if (sals.size () != 1)
11243 error (_("Couldn't get information on specified line."));
11244
11245 symtab_and_line &sal = sals[0];
11246
11247 if (*arg)
11248 error (_("Junk at end of arguments."));
11249
11250 resolve_sal_pc (&sal);
11251
11252 tp = inferior_thread ();
11253 thread = tp->global_num;
11254
11255 old_chain = make_cleanup (null_cleanup, NULL);
11256
11257 /* Note linespec handling above invalidates the frame chain.
11258 Installing a breakpoint also invalidates the frame chain (as it
11259 may need to switch threads), so do any frame handling before
11260 that. */
11261
11262 frame = get_selected_frame (NULL);
11263 frame_gdbarch = get_frame_arch (frame);
11264 stack_frame_id = get_stack_frame_id (frame);
11265 caller_frame_id = frame_unwind_caller_id (frame);
11266
11267 /* Keep within the current frame, or in frames called by the current
11268 one. */
11269
11270 breakpoint_up caller_breakpoint;
11271 if (frame_id_p (caller_frame_id))
11272 {
11273 struct symtab_and_line sal2;
11274 struct gdbarch *caller_gdbarch;
11275
11276 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11277 sal2.pc = frame_unwind_caller_pc (frame);
11278 caller_gdbarch = frame_unwind_caller_arch (frame);
11279 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11280 sal2,
11281 caller_frame_id,
11282 bp_until);
11283
11284 set_longjmp_breakpoint (tp, caller_frame_id);
11285 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11286 }
11287
11288 /* set_momentary_breakpoint could invalidate FRAME. */
11289 frame = NULL;
11290
11291 breakpoint_up location_breakpoint;
11292 if (anywhere)
11293 /* If the user told us to continue until a specified location,
11294 we don't specify a frame at which we need to stop. */
11295 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11296 null_frame_id, bp_until);
11297 else
11298 /* Otherwise, specify the selected frame, because we want to stop
11299 only at the very same frame. */
11300 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11301 stack_frame_id, bp_until);
11302
11303 sm = new_until_break_fsm (command_interp (), tp->global_num,
11304 std::move (location_breakpoint),
11305 std::move (caller_breakpoint));
11306 tp->thread_fsm = &sm->thread_fsm;
11307
11308 discard_cleanups (old_chain);
11309
11310 proceed (-1, GDB_SIGNAL_DEFAULT);
11311 }
11312
11313 /* This function attempts to parse an optional "if <cond>" clause
11314 from the arg string. If one is not found, it returns NULL.
11315
11316 Else, it returns a pointer to the condition string. (It does not
11317 attempt to evaluate the string against a particular block.) And,
11318 it updates arg to point to the first character following the parsed
11319 if clause in the arg string. */
11320
11321 const char *
11322 ep_parse_optional_if_clause (const char **arg)
11323 {
11324 const char *cond_string;
11325
11326 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11327 return NULL;
11328
11329 /* Skip the "if" keyword. */
11330 (*arg) += 2;
11331
11332 /* Skip any extra leading whitespace, and record the start of the
11333 condition string. */
11334 *arg = skip_spaces (*arg);
11335 cond_string = *arg;
11336
11337 /* Assume that the condition occupies the remainder of the arg
11338 string. */
11339 (*arg) += strlen (cond_string);
11340
11341 return cond_string;
11342 }
11343
11344 /* Commands to deal with catching events, such as signals, exceptions,
11345 process start/exit, etc. */
11346
11347 typedef enum
11348 {
11349 catch_fork_temporary, catch_vfork_temporary,
11350 catch_fork_permanent, catch_vfork_permanent
11351 }
11352 catch_fork_kind;
11353
11354 static void
11355 catch_fork_command_1 (char *arg_entry, int from_tty,
11356 struct cmd_list_element *command)
11357 {
11358 const char *arg = arg_entry;
11359 struct gdbarch *gdbarch = get_current_arch ();
11360 const char *cond_string = NULL;
11361 catch_fork_kind fork_kind;
11362 int tempflag;
11363
11364 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11365 tempflag = (fork_kind == catch_fork_temporary
11366 || fork_kind == catch_vfork_temporary);
11367
11368 if (!arg)
11369 arg = "";
11370 arg = skip_spaces (arg);
11371
11372 /* The allowed syntax is:
11373 catch [v]fork
11374 catch [v]fork if <cond>
11375
11376 First, check if there's an if clause. */
11377 cond_string = ep_parse_optional_if_clause (&arg);
11378
11379 if ((*arg != '\0') && !isspace (*arg))
11380 error (_("Junk at end of arguments."));
11381
11382 /* If this target supports it, create a fork or vfork catchpoint
11383 and enable reporting of such events. */
11384 switch (fork_kind)
11385 {
11386 case catch_fork_temporary:
11387 case catch_fork_permanent:
11388 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11389 &catch_fork_breakpoint_ops);
11390 break;
11391 case catch_vfork_temporary:
11392 case catch_vfork_permanent:
11393 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11394 &catch_vfork_breakpoint_ops);
11395 break;
11396 default:
11397 error (_("unsupported or unknown fork kind; cannot catch it"));
11398 break;
11399 }
11400 }
11401
11402 static void
11403 catch_exec_command_1 (char *arg_entry, int from_tty,
11404 struct cmd_list_element *command)
11405 {
11406 const char *arg = arg_entry;
11407 struct gdbarch *gdbarch = get_current_arch ();
11408 int tempflag;
11409 const char *cond_string = NULL;
11410
11411 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11412
11413 if (!arg)
11414 arg = "";
11415 arg = skip_spaces (arg);
11416
11417 /* The allowed syntax is:
11418 catch exec
11419 catch exec if <cond>
11420
11421 First, check if there's an if clause. */
11422 cond_string = ep_parse_optional_if_clause (&arg);
11423
11424 if ((*arg != '\0') && !isspace (*arg))
11425 error (_("Junk at end of arguments."));
11426
11427 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11428 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11429 &catch_exec_breakpoint_ops);
11430 c->exec_pathname = NULL;
11431
11432 install_breakpoint (0, std::move (c), 1);
11433 }
11434
11435 void
11436 init_ada_exception_breakpoint (struct breakpoint *b,
11437 struct gdbarch *gdbarch,
11438 struct symtab_and_line sal,
11439 const char *addr_string,
11440 const struct breakpoint_ops *ops,
11441 int tempflag,
11442 int enabled,
11443 int from_tty)
11444 {
11445 if (from_tty)
11446 {
11447 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11448 if (!loc_gdbarch)
11449 loc_gdbarch = gdbarch;
11450
11451 describe_other_breakpoints (loc_gdbarch,
11452 sal.pspace, sal.pc, sal.section, -1);
11453 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11454 version for exception catchpoints, because two catchpoints
11455 used for different exception names will use the same address.
11456 In this case, a "breakpoint ... also set at..." warning is
11457 unproductive. Besides, the warning phrasing is also a bit
11458 inappropriate, we should use the word catchpoint, and tell
11459 the user what type of catchpoint it is. The above is good
11460 enough for now, though. */
11461 }
11462
11463 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11464
11465 b->enable_state = enabled ? bp_enabled : bp_disabled;
11466 b->disposition = tempflag ? disp_del : disp_donttouch;
11467 b->location = string_to_event_location (&addr_string,
11468 language_def (language_ada));
11469 b->language = language_ada;
11470 }
11471
11472 static void
11473 catch_command (const char *arg, int from_tty)
11474 {
11475 error (_("Catch requires an event name."));
11476 }
11477 \f
11478
11479 static void
11480 tcatch_command (const char *arg, int from_tty)
11481 {
11482 error (_("Catch requires an event name."));
11483 }
11484
11485 /* Compare two breakpoints and return a strcmp-like result. */
11486
11487 static int
11488 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11489 {
11490 uintptr_t ua = (uintptr_t) a;
11491 uintptr_t ub = (uintptr_t) b;
11492
11493 if (a->number < b->number)
11494 return -1;
11495 else if (a->number > b->number)
11496 return 1;
11497
11498 /* Now sort by address, in case we see, e..g, two breakpoints with
11499 the number 0. */
11500 if (ua < ub)
11501 return -1;
11502 return ua > ub ? 1 : 0;
11503 }
11504
11505 /* Delete breakpoints by address or line. */
11506
11507 static void
11508 clear_command (char *arg, int from_tty)
11509 {
11510 struct breakpoint *b;
11511 int default_match;
11512 int i;
11513
11514 std::vector<symtab_and_line> decoded_sals;
11515 symtab_and_line last_sal;
11516 gdb::array_view<symtab_and_line> sals;
11517 if (arg)
11518 {
11519 decoded_sals
11520 = decode_line_with_current_source (arg,
11521 (DECODE_LINE_FUNFIRSTLINE
11522 | DECODE_LINE_LIST_MODE));
11523 default_match = 0;
11524 sals = decoded_sals;
11525 }
11526 else
11527 {
11528 /* Set sal's line, symtab, pc, and pspace to the values
11529 corresponding to the last call to print_frame_info. If the
11530 codepoint is not valid, this will set all the fields to 0. */
11531 last_sal = get_last_displayed_sal ();
11532 if (last_sal.symtab == 0)
11533 error (_("No source file specified."));
11534
11535 default_match = 1;
11536 sals = last_sal;
11537 }
11538
11539 /* We don't call resolve_sal_pc here. That's not as bad as it
11540 seems, because all existing breakpoints typically have both
11541 file/line and pc set. So, if clear is given file/line, we can
11542 match this to existing breakpoint without obtaining pc at all.
11543
11544 We only support clearing given the address explicitly
11545 present in breakpoint table. Say, we've set breakpoint
11546 at file:line. There were several PC values for that file:line,
11547 due to optimization, all in one block.
11548
11549 We've picked one PC value. If "clear" is issued with another
11550 PC corresponding to the same file:line, the breakpoint won't
11551 be cleared. We probably can still clear the breakpoint, but
11552 since the other PC value is never presented to user, user
11553 can only find it by guessing, and it does not seem important
11554 to support that. */
11555
11556 /* For each line spec given, delete bps which correspond to it. Do
11557 it in two passes, solely to preserve the current behavior that
11558 from_tty is forced true if we delete more than one
11559 breakpoint. */
11560
11561 std::vector<struct breakpoint *> found;
11562 for (const auto &sal : sals)
11563 {
11564 const char *sal_fullname;
11565
11566 /* If exact pc given, clear bpts at that pc.
11567 If line given (pc == 0), clear all bpts on specified line.
11568 If defaulting, clear all bpts on default line
11569 or at default pc.
11570
11571 defaulting sal.pc != 0 tests to do
11572
11573 0 1 pc
11574 1 1 pc _and_ line
11575 0 0 line
11576 1 0 <can't happen> */
11577
11578 sal_fullname = (sal.symtab == NULL
11579 ? NULL : symtab_to_fullname (sal.symtab));
11580
11581 /* Find all matching breakpoints and add them to 'found'. */
11582 ALL_BREAKPOINTS (b)
11583 {
11584 int match = 0;
11585 /* Are we going to delete b? */
11586 if (b->type != bp_none && !is_watchpoint (b))
11587 {
11588 struct bp_location *loc = b->loc;
11589 for (; loc; loc = loc->next)
11590 {
11591 /* If the user specified file:line, don't allow a PC
11592 match. This matches historical gdb behavior. */
11593 int pc_match = (!sal.explicit_line
11594 && sal.pc
11595 && (loc->pspace == sal.pspace)
11596 && (loc->address == sal.pc)
11597 && (!section_is_overlay (loc->section)
11598 || loc->section == sal.section));
11599 int line_match = 0;
11600
11601 if ((default_match || sal.explicit_line)
11602 && loc->symtab != NULL
11603 && sal_fullname != NULL
11604 && sal.pspace == loc->pspace
11605 && loc->line_number == sal.line
11606 && filename_cmp (symtab_to_fullname (loc->symtab),
11607 sal_fullname) == 0)
11608 line_match = 1;
11609
11610 if (pc_match || line_match)
11611 {
11612 match = 1;
11613 break;
11614 }
11615 }
11616 }
11617
11618 if (match)
11619 found.push_back (b);
11620 }
11621 }
11622
11623 /* Now go thru the 'found' chain and delete them. */
11624 if (found.empty ())
11625 {
11626 if (arg)
11627 error (_("No breakpoint at %s."), arg);
11628 else
11629 error (_("No breakpoint at this line."));
11630 }
11631
11632 /* Remove duplicates from the vec. */
11633 std::sort (found.begin (), found.end (),
11634 [] (const breakpoint *a, const breakpoint *b)
11635 {
11636 return compare_breakpoints (a, b) < 0;
11637 });
11638 found.erase (std::unique (found.begin (), found.end (),
11639 [] (const breakpoint *a, const breakpoint *b)
11640 {
11641 return compare_breakpoints (a, b) == 0;
11642 }),
11643 found.end ());
11644
11645 if (found.size () > 1)
11646 from_tty = 1; /* Always report if deleted more than one. */
11647 if (from_tty)
11648 {
11649 if (found.size () == 1)
11650 printf_unfiltered (_("Deleted breakpoint "));
11651 else
11652 printf_unfiltered (_("Deleted breakpoints "));
11653 }
11654
11655 for (breakpoint *iter : found)
11656 {
11657 if (from_tty)
11658 printf_unfiltered ("%d ", iter->number);
11659 delete_breakpoint (iter);
11660 }
11661 if (from_tty)
11662 putchar_unfiltered ('\n');
11663 }
11664 \f
11665 /* Delete breakpoint in BS if they are `delete' breakpoints and
11666 all breakpoints that are marked for deletion, whether hit or not.
11667 This is called after any breakpoint is hit, or after errors. */
11668
11669 void
11670 breakpoint_auto_delete (bpstat bs)
11671 {
11672 struct breakpoint *b, *b_tmp;
11673
11674 for (; bs; bs = bs->next)
11675 if (bs->breakpoint_at
11676 && bs->breakpoint_at->disposition == disp_del
11677 && bs->stop)
11678 delete_breakpoint (bs->breakpoint_at);
11679
11680 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11681 {
11682 if (b->disposition == disp_del_at_next_stop)
11683 delete_breakpoint (b);
11684 }
11685 }
11686
11687 /* A comparison function for bp_location AP and BP being interfaced to
11688 qsort. Sort elements primarily by their ADDRESS (no matter what
11689 does breakpoint_address_is_meaningful say for its OWNER),
11690 secondarily by ordering first permanent elements and
11691 terciarily just ensuring the array is sorted stable way despite
11692 qsort being an unstable algorithm. */
11693
11694 static int
11695 bp_locations_compare (const void *ap, const void *bp)
11696 {
11697 const struct bp_location *a = *(const struct bp_location **) ap;
11698 const struct bp_location *b = *(const struct bp_location **) bp;
11699
11700 if (a->address != b->address)
11701 return (a->address > b->address) - (a->address < b->address);
11702
11703 /* Sort locations at the same address by their pspace number, keeping
11704 locations of the same inferior (in a multi-inferior environment)
11705 grouped. */
11706
11707 if (a->pspace->num != b->pspace->num)
11708 return ((a->pspace->num > b->pspace->num)
11709 - (a->pspace->num < b->pspace->num));
11710
11711 /* Sort permanent breakpoints first. */
11712 if (a->permanent != b->permanent)
11713 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11714
11715 /* Make the internal GDB representation stable across GDB runs
11716 where A and B memory inside GDB can differ. Breakpoint locations of
11717 the same type at the same address can be sorted in arbitrary order. */
11718
11719 if (a->owner->number != b->owner->number)
11720 return ((a->owner->number > b->owner->number)
11721 - (a->owner->number < b->owner->number));
11722
11723 return (a > b) - (a < b);
11724 }
11725
11726 /* Set bp_locations_placed_address_before_address_max and
11727 bp_locations_shadow_len_after_address_max according to the current
11728 content of the bp_locations array. */
11729
11730 static void
11731 bp_locations_target_extensions_update (void)
11732 {
11733 struct bp_location *bl, **blp_tmp;
11734
11735 bp_locations_placed_address_before_address_max = 0;
11736 bp_locations_shadow_len_after_address_max = 0;
11737
11738 ALL_BP_LOCATIONS (bl, blp_tmp)
11739 {
11740 CORE_ADDR start, end, addr;
11741
11742 if (!bp_location_has_shadow (bl))
11743 continue;
11744
11745 start = bl->target_info.placed_address;
11746 end = start + bl->target_info.shadow_len;
11747
11748 gdb_assert (bl->address >= start);
11749 addr = bl->address - start;
11750 if (addr > bp_locations_placed_address_before_address_max)
11751 bp_locations_placed_address_before_address_max = addr;
11752
11753 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11754
11755 gdb_assert (bl->address < end);
11756 addr = end - bl->address;
11757 if (addr > bp_locations_shadow_len_after_address_max)
11758 bp_locations_shadow_len_after_address_max = addr;
11759 }
11760 }
11761
11762 /* Download tracepoint locations if they haven't been. */
11763
11764 static void
11765 download_tracepoint_locations (void)
11766 {
11767 struct breakpoint *b;
11768 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11769
11770 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11771
11772 ALL_TRACEPOINTS (b)
11773 {
11774 struct bp_location *bl;
11775 struct tracepoint *t;
11776 int bp_location_downloaded = 0;
11777
11778 if ((b->type == bp_fast_tracepoint
11779 ? !may_insert_fast_tracepoints
11780 : !may_insert_tracepoints))
11781 continue;
11782
11783 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11784 {
11785 if (target_can_download_tracepoint ())
11786 can_download_tracepoint = TRIBOOL_TRUE;
11787 else
11788 can_download_tracepoint = TRIBOOL_FALSE;
11789 }
11790
11791 if (can_download_tracepoint == TRIBOOL_FALSE)
11792 break;
11793
11794 for (bl = b->loc; bl; bl = bl->next)
11795 {
11796 /* In tracepoint, locations are _never_ duplicated, so
11797 should_be_inserted is equivalent to
11798 unduplicated_should_be_inserted. */
11799 if (!should_be_inserted (bl) || bl->inserted)
11800 continue;
11801
11802 switch_to_program_space_and_thread (bl->pspace);
11803
11804 target_download_tracepoint (bl);
11805
11806 bl->inserted = 1;
11807 bp_location_downloaded = 1;
11808 }
11809 t = (struct tracepoint *) b;
11810 t->number_on_target = b->number;
11811 if (bp_location_downloaded)
11812 observer_notify_breakpoint_modified (b);
11813 }
11814 }
11815
11816 /* Swap the insertion/duplication state between two locations. */
11817
11818 static void
11819 swap_insertion (struct bp_location *left, struct bp_location *right)
11820 {
11821 const int left_inserted = left->inserted;
11822 const int left_duplicate = left->duplicate;
11823 const int left_needs_update = left->needs_update;
11824 const struct bp_target_info left_target_info = left->target_info;
11825
11826 /* Locations of tracepoints can never be duplicated. */
11827 if (is_tracepoint (left->owner))
11828 gdb_assert (!left->duplicate);
11829 if (is_tracepoint (right->owner))
11830 gdb_assert (!right->duplicate);
11831
11832 left->inserted = right->inserted;
11833 left->duplicate = right->duplicate;
11834 left->needs_update = right->needs_update;
11835 left->target_info = right->target_info;
11836 right->inserted = left_inserted;
11837 right->duplicate = left_duplicate;
11838 right->needs_update = left_needs_update;
11839 right->target_info = left_target_info;
11840 }
11841
11842 /* Force the re-insertion of the locations at ADDRESS. This is called
11843 once a new/deleted/modified duplicate location is found and we are evaluating
11844 conditions on the target's side. Such conditions need to be updated on
11845 the target. */
11846
11847 static void
11848 force_breakpoint_reinsertion (struct bp_location *bl)
11849 {
11850 struct bp_location **locp = NULL, **loc2p;
11851 struct bp_location *loc;
11852 CORE_ADDR address = 0;
11853 int pspace_num;
11854
11855 address = bl->address;
11856 pspace_num = bl->pspace->num;
11857
11858 /* This is only meaningful if the target is
11859 evaluating conditions and if the user has
11860 opted for condition evaluation on the target's
11861 side. */
11862 if (gdb_evaluates_breakpoint_condition_p ()
11863 || !target_supports_evaluation_of_breakpoint_conditions ())
11864 return;
11865
11866 /* Flag all breakpoint locations with this address and
11867 the same program space as the location
11868 as "its condition has changed". We need to
11869 update the conditions on the target's side. */
11870 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11871 {
11872 loc = *loc2p;
11873
11874 if (!is_breakpoint (loc->owner)
11875 || pspace_num != loc->pspace->num)
11876 continue;
11877
11878 /* Flag the location appropriately. We use a different state to
11879 let everyone know that we already updated the set of locations
11880 with addr bl->address and program space bl->pspace. This is so
11881 we don't have to keep calling these functions just to mark locations
11882 that have already been marked. */
11883 loc->condition_changed = condition_updated;
11884
11885 /* Free the agent expression bytecode as well. We will compute
11886 it later on. */
11887 loc->cond_bytecode.reset ();
11888 }
11889 }
11890 /* Called whether new breakpoints are created, or existing breakpoints
11891 deleted, to update the global location list and recompute which
11892 locations are duplicate of which.
11893
11894 The INSERT_MODE flag determines whether locations may not, may, or
11895 shall be inserted now. See 'enum ugll_insert_mode' for more
11896 info. */
11897
11898 static void
11899 update_global_location_list (enum ugll_insert_mode insert_mode)
11900 {
11901 struct breakpoint *b;
11902 struct bp_location **locp, *loc;
11903 /* Last breakpoint location address that was marked for update. */
11904 CORE_ADDR last_addr = 0;
11905 /* Last breakpoint location program space that was marked for update. */
11906 int last_pspace_num = -1;
11907
11908 /* Used in the duplicates detection below. When iterating over all
11909 bp_locations, points to the first bp_location of a given address.
11910 Breakpoints and watchpoints of different types are never
11911 duplicates of each other. Keep one pointer for each type of
11912 breakpoint/watchpoint, so we only need to loop over all locations
11913 once. */
11914 struct bp_location *bp_loc_first; /* breakpoint */
11915 struct bp_location *wp_loc_first; /* hardware watchpoint */
11916 struct bp_location *awp_loc_first; /* access watchpoint */
11917 struct bp_location *rwp_loc_first; /* read watchpoint */
11918
11919 /* Saved former bp_locations array which we compare against the newly
11920 built bp_locations from the current state of ALL_BREAKPOINTS. */
11921 struct bp_location **old_locp;
11922 unsigned old_locations_count;
11923 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11924
11925 old_locations_count = bp_locations_count;
11926 bp_locations = NULL;
11927 bp_locations_count = 0;
11928
11929 ALL_BREAKPOINTS (b)
11930 for (loc = b->loc; loc; loc = loc->next)
11931 bp_locations_count++;
11932
11933 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11934 locp = bp_locations;
11935 ALL_BREAKPOINTS (b)
11936 for (loc = b->loc; loc; loc = loc->next)
11937 *locp++ = loc;
11938 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11939 bp_locations_compare);
11940
11941 bp_locations_target_extensions_update ();
11942
11943 /* Identify bp_location instances that are no longer present in the
11944 new list, and therefore should be freed. Note that it's not
11945 necessary that those locations should be removed from inferior --
11946 if there's another location at the same address (previously
11947 marked as duplicate), we don't need to remove/insert the
11948 location.
11949
11950 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11951 and former bp_location array state respectively. */
11952
11953 locp = bp_locations;
11954 for (old_locp = old_locations.get ();
11955 old_locp < old_locations.get () + old_locations_count;
11956 old_locp++)
11957 {
11958 struct bp_location *old_loc = *old_locp;
11959 struct bp_location **loc2p;
11960
11961 /* Tells if 'old_loc' is found among the new locations. If
11962 not, we have to free it. */
11963 int found_object = 0;
11964 /* Tells if the location should remain inserted in the target. */
11965 int keep_in_target = 0;
11966 int removed = 0;
11967
11968 /* Skip LOCP entries which will definitely never be needed.
11969 Stop either at or being the one matching OLD_LOC. */
11970 while (locp < bp_locations + bp_locations_count
11971 && (*locp)->address < old_loc->address)
11972 locp++;
11973
11974 for (loc2p = locp;
11975 (loc2p < bp_locations + bp_locations_count
11976 && (*loc2p)->address == old_loc->address);
11977 loc2p++)
11978 {
11979 /* Check if this is a new/duplicated location or a duplicated
11980 location that had its condition modified. If so, we want to send
11981 its condition to the target if evaluation of conditions is taking
11982 place there. */
11983 if ((*loc2p)->condition_changed == condition_modified
11984 && (last_addr != old_loc->address
11985 || last_pspace_num != old_loc->pspace->num))
11986 {
11987 force_breakpoint_reinsertion (*loc2p);
11988 last_pspace_num = old_loc->pspace->num;
11989 }
11990
11991 if (*loc2p == old_loc)
11992 found_object = 1;
11993 }
11994
11995 /* We have already handled this address, update it so that we don't
11996 have to go through updates again. */
11997 last_addr = old_loc->address;
11998
11999 /* Target-side condition evaluation: Handle deleted locations. */
12000 if (!found_object)
12001 force_breakpoint_reinsertion (old_loc);
12002
12003 /* If this location is no longer present, and inserted, look if
12004 there's maybe a new location at the same address. If so,
12005 mark that one inserted, and don't remove this one. This is
12006 needed so that we don't have a time window where a breakpoint
12007 at certain location is not inserted. */
12008
12009 if (old_loc->inserted)
12010 {
12011 /* If the location is inserted now, we might have to remove
12012 it. */
12013
12014 if (found_object && should_be_inserted (old_loc))
12015 {
12016 /* The location is still present in the location list,
12017 and still should be inserted. Don't do anything. */
12018 keep_in_target = 1;
12019 }
12020 else
12021 {
12022 /* This location still exists, but it won't be kept in the
12023 target since it may have been disabled. We proceed to
12024 remove its target-side condition. */
12025
12026 /* The location is either no longer present, or got
12027 disabled. See if there's another location at the
12028 same address, in which case we don't need to remove
12029 this one from the target. */
12030
12031 /* OLD_LOC comes from existing struct breakpoint. */
12032 if (breakpoint_address_is_meaningful (old_loc->owner))
12033 {
12034 for (loc2p = locp;
12035 (loc2p < bp_locations + bp_locations_count
12036 && (*loc2p)->address == old_loc->address);
12037 loc2p++)
12038 {
12039 struct bp_location *loc2 = *loc2p;
12040
12041 if (breakpoint_locations_match (loc2, old_loc))
12042 {
12043 /* Read watchpoint locations are switched to
12044 access watchpoints, if the former are not
12045 supported, but the latter are. */
12046 if (is_hardware_watchpoint (old_loc->owner))
12047 {
12048 gdb_assert (is_hardware_watchpoint (loc2->owner));
12049 loc2->watchpoint_type = old_loc->watchpoint_type;
12050 }
12051
12052 /* loc2 is a duplicated location. We need to check
12053 if it should be inserted in case it will be
12054 unduplicated. */
12055 if (loc2 != old_loc
12056 && unduplicated_should_be_inserted (loc2))
12057 {
12058 swap_insertion (old_loc, loc2);
12059 keep_in_target = 1;
12060 break;
12061 }
12062 }
12063 }
12064 }
12065 }
12066
12067 if (!keep_in_target)
12068 {
12069 if (remove_breakpoint (old_loc))
12070 {
12071 /* This is just about all we can do. We could keep
12072 this location on the global list, and try to
12073 remove it next time, but there's no particular
12074 reason why we will succeed next time.
12075
12076 Note that at this point, old_loc->owner is still
12077 valid, as delete_breakpoint frees the breakpoint
12078 only after calling us. */
12079 printf_filtered (_("warning: Error removing "
12080 "breakpoint %d\n"),
12081 old_loc->owner->number);
12082 }
12083 removed = 1;
12084 }
12085 }
12086
12087 if (!found_object)
12088 {
12089 if (removed && target_is_non_stop_p ()
12090 && need_moribund_for_location_type (old_loc))
12091 {
12092 /* This location was removed from the target. In
12093 non-stop mode, a race condition is possible where
12094 we've removed a breakpoint, but stop events for that
12095 breakpoint are already queued and will arrive later.
12096 We apply an heuristic to be able to distinguish such
12097 SIGTRAPs from other random SIGTRAPs: we keep this
12098 breakpoint location for a bit, and will retire it
12099 after we see some number of events. The theory here
12100 is that reporting of events should, "on the average",
12101 be fair, so after a while we'll see events from all
12102 threads that have anything of interest, and no longer
12103 need to keep this breakpoint location around. We
12104 don't hold locations forever so to reduce chances of
12105 mistaking a non-breakpoint SIGTRAP for a breakpoint
12106 SIGTRAP.
12107
12108 The heuristic failing can be disastrous on
12109 decr_pc_after_break targets.
12110
12111 On decr_pc_after_break targets, like e.g., x86-linux,
12112 if we fail to recognize a late breakpoint SIGTRAP,
12113 because events_till_retirement has reached 0 too
12114 soon, we'll fail to do the PC adjustment, and report
12115 a random SIGTRAP to the user. When the user resumes
12116 the inferior, it will most likely immediately crash
12117 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12118 corrupted, because of being resumed e.g., in the
12119 middle of a multi-byte instruction, or skipped a
12120 one-byte instruction. This was actually seen happen
12121 on native x86-linux, and should be less rare on
12122 targets that do not support new thread events, like
12123 remote, due to the heuristic depending on
12124 thread_count.
12125
12126 Mistaking a random SIGTRAP for a breakpoint trap
12127 causes similar symptoms (PC adjustment applied when
12128 it shouldn't), but then again, playing with SIGTRAPs
12129 behind the debugger's back is asking for trouble.
12130
12131 Since hardware watchpoint traps are always
12132 distinguishable from other traps, so we don't need to
12133 apply keep hardware watchpoint moribund locations
12134 around. We simply always ignore hardware watchpoint
12135 traps we can no longer explain. */
12136
12137 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12138 old_loc->owner = NULL;
12139
12140 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12141 }
12142 else
12143 {
12144 old_loc->owner = NULL;
12145 decref_bp_location (&old_loc);
12146 }
12147 }
12148 }
12149
12150 /* Rescan breakpoints at the same address and section, marking the
12151 first one as "first" and any others as "duplicates". This is so
12152 that the bpt instruction is only inserted once. If we have a
12153 permanent breakpoint at the same place as BPT, make that one the
12154 official one, and the rest as duplicates. Permanent breakpoints
12155 are sorted first for the same address.
12156
12157 Do the same for hardware watchpoints, but also considering the
12158 watchpoint's type (regular/access/read) and length. */
12159
12160 bp_loc_first = NULL;
12161 wp_loc_first = NULL;
12162 awp_loc_first = NULL;
12163 rwp_loc_first = NULL;
12164 ALL_BP_LOCATIONS (loc, locp)
12165 {
12166 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12167 non-NULL. */
12168 struct bp_location **loc_first_p;
12169 b = loc->owner;
12170
12171 if (!unduplicated_should_be_inserted (loc)
12172 || !breakpoint_address_is_meaningful (b)
12173 /* Don't detect duplicate for tracepoint locations because they are
12174 never duplicated. See the comments in field `duplicate' of
12175 `struct bp_location'. */
12176 || is_tracepoint (b))
12177 {
12178 /* Clear the condition modification flag. */
12179 loc->condition_changed = condition_unchanged;
12180 continue;
12181 }
12182
12183 if (b->type == bp_hardware_watchpoint)
12184 loc_first_p = &wp_loc_first;
12185 else if (b->type == bp_read_watchpoint)
12186 loc_first_p = &rwp_loc_first;
12187 else if (b->type == bp_access_watchpoint)
12188 loc_first_p = &awp_loc_first;
12189 else
12190 loc_first_p = &bp_loc_first;
12191
12192 if (*loc_first_p == NULL
12193 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12194 || !breakpoint_locations_match (loc, *loc_first_p))
12195 {
12196 *loc_first_p = loc;
12197 loc->duplicate = 0;
12198
12199 if (is_breakpoint (loc->owner) && loc->condition_changed)
12200 {
12201 loc->needs_update = 1;
12202 /* Clear the condition modification flag. */
12203 loc->condition_changed = condition_unchanged;
12204 }
12205 continue;
12206 }
12207
12208
12209 /* This and the above ensure the invariant that the first location
12210 is not duplicated, and is the inserted one.
12211 All following are marked as duplicated, and are not inserted. */
12212 if (loc->inserted)
12213 swap_insertion (loc, *loc_first_p);
12214 loc->duplicate = 1;
12215
12216 /* Clear the condition modification flag. */
12217 loc->condition_changed = condition_unchanged;
12218 }
12219
12220 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12221 {
12222 if (insert_mode != UGLL_DONT_INSERT)
12223 insert_breakpoint_locations ();
12224 else
12225 {
12226 /* Even though the caller told us to not insert new
12227 locations, we may still need to update conditions on the
12228 target's side of breakpoints that were already inserted
12229 if the target is evaluating breakpoint conditions. We
12230 only update conditions for locations that are marked
12231 "needs_update". */
12232 update_inserted_breakpoint_locations ();
12233 }
12234 }
12235
12236 if (insert_mode != UGLL_DONT_INSERT)
12237 download_tracepoint_locations ();
12238 }
12239
12240 void
12241 breakpoint_retire_moribund (void)
12242 {
12243 struct bp_location *loc;
12244 int ix;
12245
12246 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12247 if (--(loc->events_till_retirement) == 0)
12248 {
12249 decref_bp_location (&loc);
12250 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12251 --ix;
12252 }
12253 }
12254
12255 static void
12256 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12257 {
12258
12259 TRY
12260 {
12261 update_global_location_list (insert_mode);
12262 }
12263 CATCH (e, RETURN_MASK_ERROR)
12264 {
12265 }
12266 END_CATCH
12267 }
12268
12269 /* Clear BKP from a BPS. */
12270
12271 static void
12272 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12273 {
12274 bpstat bs;
12275
12276 for (bs = bps; bs; bs = bs->next)
12277 if (bs->breakpoint_at == bpt)
12278 {
12279 bs->breakpoint_at = NULL;
12280 bs->old_val = NULL;
12281 /* bs->commands will be freed later. */
12282 }
12283 }
12284
12285 /* Callback for iterate_over_threads. */
12286 static int
12287 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12288 {
12289 struct breakpoint *bpt = (struct breakpoint *) data;
12290
12291 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12292 return 0;
12293 }
12294
12295 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12296 callbacks. */
12297
12298 static void
12299 say_where (struct breakpoint *b)
12300 {
12301 struct value_print_options opts;
12302
12303 get_user_print_options (&opts);
12304
12305 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12306 single string. */
12307 if (b->loc == NULL)
12308 {
12309 /* For pending locations, the output differs slightly based
12310 on b->extra_string. If this is non-NULL, it contains either
12311 a condition or dprintf arguments. */
12312 if (b->extra_string == NULL)
12313 {
12314 printf_filtered (_(" (%s) pending."),
12315 event_location_to_string (b->location.get ()));
12316 }
12317 else if (b->type == bp_dprintf)
12318 {
12319 printf_filtered (_(" (%s,%s) pending."),
12320 event_location_to_string (b->location.get ()),
12321 b->extra_string);
12322 }
12323 else
12324 {
12325 printf_filtered (_(" (%s %s) pending."),
12326 event_location_to_string (b->location.get ()),
12327 b->extra_string);
12328 }
12329 }
12330 else
12331 {
12332 if (opts.addressprint || b->loc->symtab == NULL)
12333 {
12334 printf_filtered (" at ");
12335 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12336 gdb_stdout);
12337 }
12338 if (b->loc->symtab != NULL)
12339 {
12340 /* If there is a single location, we can print the location
12341 more nicely. */
12342 if (b->loc->next == NULL)
12343 printf_filtered (": file %s, line %d.",
12344 symtab_to_filename_for_display (b->loc->symtab),
12345 b->loc->line_number);
12346 else
12347 /* This is not ideal, but each location may have a
12348 different file name, and this at least reflects the
12349 real situation somewhat. */
12350 printf_filtered (": %s.",
12351 event_location_to_string (b->location.get ()));
12352 }
12353
12354 if (b->loc->next)
12355 {
12356 struct bp_location *loc = b->loc;
12357 int n = 0;
12358 for (; loc; loc = loc->next)
12359 ++n;
12360 printf_filtered (" (%d locations)", n);
12361 }
12362 }
12363 }
12364
12365 /* Default bp_location_ops methods. */
12366
12367 static void
12368 bp_location_dtor (struct bp_location *self)
12369 {
12370 xfree (self->function_name);
12371 }
12372
12373 static const struct bp_location_ops bp_location_ops =
12374 {
12375 bp_location_dtor
12376 };
12377
12378 /* Destructor for the breakpoint base class. */
12379
12380 breakpoint::~breakpoint ()
12381 {
12382 xfree (this->cond_string);
12383 xfree (this->extra_string);
12384 xfree (this->filter);
12385 }
12386
12387 static struct bp_location *
12388 base_breakpoint_allocate_location (struct breakpoint *self)
12389 {
12390 return new bp_location (&bp_location_ops, self);
12391 }
12392
12393 static void
12394 base_breakpoint_re_set (struct breakpoint *b)
12395 {
12396 /* Nothing to re-set. */
12397 }
12398
12399 #define internal_error_pure_virtual_called() \
12400 gdb_assert_not_reached ("pure virtual function called")
12401
12402 static int
12403 base_breakpoint_insert_location (struct bp_location *bl)
12404 {
12405 internal_error_pure_virtual_called ();
12406 }
12407
12408 static int
12409 base_breakpoint_remove_location (struct bp_location *bl,
12410 enum remove_bp_reason reason)
12411 {
12412 internal_error_pure_virtual_called ();
12413 }
12414
12415 static int
12416 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12417 const address_space *aspace,
12418 CORE_ADDR bp_addr,
12419 const struct target_waitstatus *ws)
12420 {
12421 internal_error_pure_virtual_called ();
12422 }
12423
12424 static void
12425 base_breakpoint_check_status (bpstat bs)
12426 {
12427 /* Always stop. */
12428 }
12429
12430 /* A "works_in_software_mode" breakpoint_ops method that just internal
12431 errors. */
12432
12433 static int
12434 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12435 {
12436 internal_error_pure_virtual_called ();
12437 }
12438
12439 /* A "resources_needed" breakpoint_ops method that just internal
12440 errors. */
12441
12442 static int
12443 base_breakpoint_resources_needed (const struct bp_location *bl)
12444 {
12445 internal_error_pure_virtual_called ();
12446 }
12447
12448 static enum print_stop_action
12449 base_breakpoint_print_it (bpstat bs)
12450 {
12451 internal_error_pure_virtual_called ();
12452 }
12453
12454 static void
12455 base_breakpoint_print_one_detail (const struct breakpoint *self,
12456 struct ui_out *uiout)
12457 {
12458 /* nothing */
12459 }
12460
12461 static void
12462 base_breakpoint_print_mention (struct breakpoint *b)
12463 {
12464 internal_error_pure_virtual_called ();
12465 }
12466
12467 static void
12468 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12469 {
12470 internal_error_pure_virtual_called ();
12471 }
12472
12473 static void
12474 base_breakpoint_create_sals_from_location
12475 (const struct event_location *location,
12476 struct linespec_result *canonical,
12477 enum bptype type_wanted)
12478 {
12479 internal_error_pure_virtual_called ();
12480 }
12481
12482 static void
12483 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12484 struct linespec_result *c,
12485 gdb::unique_xmalloc_ptr<char> cond_string,
12486 gdb::unique_xmalloc_ptr<char> extra_string,
12487 enum bptype type_wanted,
12488 enum bpdisp disposition,
12489 int thread,
12490 int task, int ignore_count,
12491 const struct breakpoint_ops *o,
12492 int from_tty, int enabled,
12493 int internal, unsigned flags)
12494 {
12495 internal_error_pure_virtual_called ();
12496 }
12497
12498 static std::vector<symtab_and_line>
12499 base_breakpoint_decode_location (struct breakpoint *b,
12500 const struct event_location *location,
12501 struct program_space *search_pspace)
12502 {
12503 internal_error_pure_virtual_called ();
12504 }
12505
12506 /* The default 'explains_signal' method. */
12507
12508 static int
12509 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12510 {
12511 return 1;
12512 }
12513
12514 /* The default "after_condition_true" method. */
12515
12516 static void
12517 base_breakpoint_after_condition_true (struct bpstats *bs)
12518 {
12519 /* Nothing to do. */
12520 }
12521
12522 struct breakpoint_ops base_breakpoint_ops =
12523 {
12524 base_breakpoint_allocate_location,
12525 base_breakpoint_re_set,
12526 base_breakpoint_insert_location,
12527 base_breakpoint_remove_location,
12528 base_breakpoint_breakpoint_hit,
12529 base_breakpoint_check_status,
12530 base_breakpoint_resources_needed,
12531 base_breakpoint_works_in_software_mode,
12532 base_breakpoint_print_it,
12533 NULL,
12534 base_breakpoint_print_one_detail,
12535 base_breakpoint_print_mention,
12536 base_breakpoint_print_recreate,
12537 base_breakpoint_create_sals_from_location,
12538 base_breakpoint_create_breakpoints_sal,
12539 base_breakpoint_decode_location,
12540 base_breakpoint_explains_signal,
12541 base_breakpoint_after_condition_true,
12542 };
12543
12544 /* Default breakpoint_ops methods. */
12545
12546 static void
12547 bkpt_re_set (struct breakpoint *b)
12548 {
12549 /* FIXME: is this still reachable? */
12550 if (breakpoint_event_location_empty_p (b))
12551 {
12552 /* Anything without a location can't be re-set. */
12553 delete_breakpoint (b);
12554 return;
12555 }
12556
12557 breakpoint_re_set_default (b);
12558 }
12559
12560 static int
12561 bkpt_insert_location (struct bp_location *bl)
12562 {
12563 CORE_ADDR addr = bl->target_info.reqstd_address;
12564
12565 bl->target_info.kind = breakpoint_kind (bl, &addr);
12566 bl->target_info.placed_address = addr;
12567
12568 if (bl->loc_type == bp_loc_hardware_breakpoint)
12569 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12570 else
12571 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12572 }
12573
12574 static int
12575 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12576 {
12577 if (bl->loc_type == bp_loc_hardware_breakpoint)
12578 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12579 else
12580 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12581 }
12582
12583 static int
12584 bkpt_breakpoint_hit (const struct bp_location *bl,
12585 const address_space *aspace, CORE_ADDR bp_addr,
12586 const struct target_waitstatus *ws)
12587 {
12588 if (ws->kind != TARGET_WAITKIND_STOPPED
12589 || ws->value.sig != GDB_SIGNAL_TRAP)
12590 return 0;
12591
12592 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12593 aspace, bp_addr))
12594 return 0;
12595
12596 if (overlay_debugging /* unmapped overlay section */
12597 && section_is_overlay (bl->section)
12598 && !section_is_mapped (bl->section))
12599 return 0;
12600
12601 return 1;
12602 }
12603
12604 static int
12605 dprintf_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 (dprintf_style == dprintf_style_agent
12610 && target_can_run_breakpoint_commands ())
12611 {
12612 /* An agent-style dprintf never causes a stop. If we see a trap
12613 for this address it must be for a breakpoint that happens to
12614 be set at the same address. */
12615 return 0;
12616 }
12617
12618 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12619 }
12620
12621 static int
12622 bkpt_resources_needed (const struct bp_location *bl)
12623 {
12624 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12625
12626 return 1;
12627 }
12628
12629 static enum print_stop_action
12630 bkpt_print_it (bpstat bs)
12631 {
12632 struct breakpoint *b;
12633 const struct bp_location *bl;
12634 int bp_temp;
12635 struct ui_out *uiout = current_uiout;
12636
12637 gdb_assert (bs->bp_location_at != NULL);
12638
12639 bl = bs->bp_location_at;
12640 b = bs->breakpoint_at;
12641
12642 bp_temp = b->disposition == disp_del;
12643 if (bl->address != bl->requested_address)
12644 breakpoint_adjustment_warning (bl->requested_address,
12645 bl->address,
12646 b->number, 1);
12647 annotate_breakpoint (b->number);
12648 maybe_print_thread_hit_breakpoint (uiout);
12649
12650 if (bp_temp)
12651 uiout->text ("Temporary breakpoint ");
12652 else
12653 uiout->text ("Breakpoint ");
12654 if (uiout->is_mi_like_p ())
12655 {
12656 uiout->field_string ("reason",
12657 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12658 uiout->field_string ("disp", bpdisp_text (b->disposition));
12659 }
12660 uiout->field_int ("bkptno", b->number);
12661 uiout->text (", ");
12662
12663 return PRINT_SRC_AND_LOC;
12664 }
12665
12666 static void
12667 bkpt_print_mention (struct breakpoint *b)
12668 {
12669 if (current_uiout->is_mi_like_p ())
12670 return;
12671
12672 switch (b->type)
12673 {
12674 case bp_breakpoint:
12675 case bp_gnu_ifunc_resolver:
12676 if (b->disposition == disp_del)
12677 printf_filtered (_("Temporary breakpoint"));
12678 else
12679 printf_filtered (_("Breakpoint"));
12680 printf_filtered (_(" %d"), b->number);
12681 if (b->type == bp_gnu_ifunc_resolver)
12682 printf_filtered (_(" at gnu-indirect-function resolver"));
12683 break;
12684 case bp_hardware_breakpoint:
12685 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12686 break;
12687 case bp_dprintf:
12688 printf_filtered (_("Dprintf %d"), b->number);
12689 break;
12690 }
12691
12692 say_where (b);
12693 }
12694
12695 static void
12696 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12697 {
12698 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12699 fprintf_unfiltered (fp, "tbreak");
12700 else if (tp->type == bp_breakpoint)
12701 fprintf_unfiltered (fp, "break");
12702 else if (tp->type == bp_hardware_breakpoint
12703 && tp->disposition == disp_del)
12704 fprintf_unfiltered (fp, "thbreak");
12705 else if (tp->type == bp_hardware_breakpoint)
12706 fprintf_unfiltered (fp, "hbreak");
12707 else
12708 internal_error (__FILE__, __LINE__,
12709 _("unhandled breakpoint type %d"), (int) tp->type);
12710
12711 fprintf_unfiltered (fp, " %s",
12712 event_location_to_string (tp->location.get ()));
12713
12714 /* Print out extra_string if this breakpoint is pending. It might
12715 contain, for example, conditions that were set by the user. */
12716 if (tp->loc == NULL && tp->extra_string != NULL)
12717 fprintf_unfiltered (fp, " %s", tp->extra_string);
12718
12719 print_recreate_thread (tp, fp);
12720 }
12721
12722 static void
12723 bkpt_create_sals_from_location (const struct event_location *location,
12724 struct linespec_result *canonical,
12725 enum bptype type_wanted)
12726 {
12727 create_sals_from_location_default (location, canonical, type_wanted);
12728 }
12729
12730 static void
12731 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12732 struct linespec_result *canonical,
12733 gdb::unique_xmalloc_ptr<char> cond_string,
12734 gdb::unique_xmalloc_ptr<char> extra_string,
12735 enum bptype type_wanted,
12736 enum bpdisp disposition,
12737 int thread,
12738 int task, int ignore_count,
12739 const struct breakpoint_ops *ops,
12740 int from_tty, int enabled,
12741 int internal, unsigned flags)
12742 {
12743 create_breakpoints_sal_default (gdbarch, canonical,
12744 std::move (cond_string),
12745 std::move (extra_string),
12746 type_wanted,
12747 disposition, thread, task,
12748 ignore_count, ops, from_tty,
12749 enabled, internal, flags);
12750 }
12751
12752 static std::vector<symtab_and_line>
12753 bkpt_decode_location (struct breakpoint *b,
12754 const struct event_location *location,
12755 struct program_space *search_pspace)
12756 {
12757 return decode_location_default (b, location, search_pspace);
12758 }
12759
12760 /* Virtual table for internal breakpoints. */
12761
12762 static void
12763 internal_bkpt_re_set (struct breakpoint *b)
12764 {
12765 switch (b->type)
12766 {
12767 /* Delete overlay event and longjmp master breakpoints; they
12768 will be reset later by breakpoint_re_set. */
12769 case bp_overlay_event:
12770 case bp_longjmp_master:
12771 case bp_std_terminate_master:
12772 case bp_exception_master:
12773 delete_breakpoint (b);
12774 break;
12775
12776 /* This breakpoint is special, it's set up when the inferior
12777 starts and we really don't want to touch it. */
12778 case bp_shlib_event:
12779
12780 /* Like bp_shlib_event, this breakpoint type is special. Once
12781 it is set up, we do not want to touch it. */
12782 case bp_thread_event:
12783 break;
12784 }
12785 }
12786
12787 static void
12788 internal_bkpt_check_status (bpstat bs)
12789 {
12790 if (bs->breakpoint_at->type == bp_shlib_event)
12791 {
12792 /* If requested, stop when the dynamic linker notifies GDB of
12793 events. This allows the user to get control and place
12794 breakpoints in initializer routines for dynamically loaded
12795 objects (among other things). */
12796 bs->stop = stop_on_solib_events;
12797 bs->print = stop_on_solib_events;
12798 }
12799 else
12800 bs->stop = 0;
12801 }
12802
12803 static enum print_stop_action
12804 internal_bkpt_print_it (bpstat bs)
12805 {
12806 struct breakpoint *b;
12807
12808 b = bs->breakpoint_at;
12809
12810 switch (b->type)
12811 {
12812 case bp_shlib_event:
12813 /* Did we stop because the user set the stop_on_solib_events
12814 variable? (If so, we report this as a generic, "Stopped due
12815 to shlib event" message.) */
12816 print_solib_event (0);
12817 break;
12818
12819 case bp_thread_event:
12820 /* Not sure how we will get here.
12821 GDB should not stop for these breakpoints. */
12822 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12823 break;
12824
12825 case bp_overlay_event:
12826 /* By analogy with the thread event, GDB should not stop for these. */
12827 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12828 break;
12829
12830 case bp_longjmp_master:
12831 /* These should never be enabled. */
12832 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12833 break;
12834
12835 case bp_std_terminate_master:
12836 /* These should never be enabled. */
12837 printf_filtered (_("std::terminate Master Breakpoint: "
12838 "gdb should not stop!\n"));
12839 break;
12840
12841 case bp_exception_master:
12842 /* These should never be enabled. */
12843 printf_filtered (_("Exception Master Breakpoint: "
12844 "gdb should not stop!\n"));
12845 break;
12846 }
12847
12848 return PRINT_NOTHING;
12849 }
12850
12851 static void
12852 internal_bkpt_print_mention (struct breakpoint *b)
12853 {
12854 /* Nothing to mention. These breakpoints are internal. */
12855 }
12856
12857 /* Virtual table for momentary breakpoints */
12858
12859 static void
12860 momentary_bkpt_re_set (struct breakpoint *b)
12861 {
12862 /* Keep temporary breakpoints, which can be encountered when we step
12863 over a dlopen call and solib_add is resetting the breakpoints.
12864 Otherwise these should have been blown away via the cleanup chain
12865 or by breakpoint_init_inferior when we rerun the executable. */
12866 }
12867
12868 static void
12869 momentary_bkpt_check_status (bpstat bs)
12870 {
12871 /* Nothing. The point of these breakpoints is causing a stop. */
12872 }
12873
12874 static enum print_stop_action
12875 momentary_bkpt_print_it (bpstat bs)
12876 {
12877 return PRINT_UNKNOWN;
12878 }
12879
12880 static void
12881 momentary_bkpt_print_mention (struct breakpoint *b)
12882 {
12883 /* Nothing to mention. These breakpoints are internal. */
12884 }
12885
12886 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12887
12888 It gets cleared already on the removal of the first one of such placed
12889 breakpoints. This is OK as they get all removed altogether. */
12890
12891 longjmp_breakpoint::~longjmp_breakpoint ()
12892 {
12893 thread_info *tp = find_thread_global_id (this->thread);
12894
12895 if (tp != NULL)
12896 tp->initiating_frame = null_frame_id;
12897 }
12898
12899 /* Specific methods for probe breakpoints. */
12900
12901 static int
12902 bkpt_probe_insert_location (struct bp_location *bl)
12903 {
12904 int v = bkpt_insert_location (bl);
12905
12906 if (v == 0)
12907 {
12908 /* The insertion was successful, now let's set the probe's semaphore
12909 if needed. */
12910 if (bl->probe.probe->pops->set_semaphore != NULL)
12911 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12912 bl->probe.objfile,
12913 bl->gdbarch);
12914 }
12915
12916 return v;
12917 }
12918
12919 static int
12920 bkpt_probe_remove_location (struct bp_location *bl,
12921 enum remove_bp_reason reason)
12922 {
12923 /* Let's clear the semaphore before removing the location. */
12924 if (bl->probe.probe->pops->clear_semaphore != NULL)
12925 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12926 bl->probe.objfile,
12927 bl->gdbarch);
12928
12929 return bkpt_remove_location (bl, reason);
12930 }
12931
12932 static void
12933 bkpt_probe_create_sals_from_location (const struct event_location *location,
12934 struct linespec_result *canonical,
12935 enum bptype type_wanted)
12936 {
12937 struct linespec_sals lsal;
12938
12939 lsal.sals = parse_probes (location, NULL, canonical);
12940 lsal.canonical
12941 = xstrdup (event_location_to_string (canonical->location.get ()));
12942 canonical->lsals.push_back (std::move (lsal));
12943 }
12944
12945 static std::vector<symtab_and_line>
12946 bkpt_probe_decode_location (struct breakpoint *b,
12947 const struct event_location *location,
12948 struct program_space *search_pspace)
12949 {
12950 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12951 if (sals.empty ())
12952 error (_("probe not found"));
12953 return sals;
12954 }
12955
12956 /* The breakpoint_ops structure to be used in tracepoints. */
12957
12958 static void
12959 tracepoint_re_set (struct breakpoint *b)
12960 {
12961 breakpoint_re_set_default (b);
12962 }
12963
12964 static int
12965 tracepoint_breakpoint_hit (const struct bp_location *bl,
12966 const address_space *aspace, CORE_ADDR bp_addr,
12967 const struct target_waitstatus *ws)
12968 {
12969 /* By definition, the inferior does not report stops at
12970 tracepoints. */
12971 return 0;
12972 }
12973
12974 static void
12975 tracepoint_print_one_detail (const struct breakpoint *self,
12976 struct ui_out *uiout)
12977 {
12978 struct tracepoint *tp = (struct tracepoint *) self;
12979 if (tp->static_trace_marker_id)
12980 {
12981 gdb_assert (self->type == bp_static_tracepoint);
12982
12983 uiout->text ("\tmarker id is ");
12984 uiout->field_string ("static-tracepoint-marker-string-id",
12985 tp->static_trace_marker_id);
12986 uiout->text ("\n");
12987 }
12988 }
12989
12990 static void
12991 tracepoint_print_mention (struct breakpoint *b)
12992 {
12993 if (current_uiout->is_mi_like_p ())
12994 return;
12995
12996 switch (b->type)
12997 {
12998 case bp_tracepoint:
12999 printf_filtered (_("Tracepoint"));
13000 printf_filtered (_(" %d"), b->number);
13001 break;
13002 case bp_fast_tracepoint:
13003 printf_filtered (_("Fast tracepoint"));
13004 printf_filtered (_(" %d"), b->number);
13005 break;
13006 case bp_static_tracepoint:
13007 printf_filtered (_("Static tracepoint"));
13008 printf_filtered (_(" %d"), b->number);
13009 break;
13010 default:
13011 internal_error (__FILE__, __LINE__,
13012 _("unhandled tracepoint type %d"), (int) b->type);
13013 }
13014
13015 say_where (b);
13016 }
13017
13018 static void
13019 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13020 {
13021 struct tracepoint *tp = (struct tracepoint *) self;
13022
13023 if (self->type == bp_fast_tracepoint)
13024 fprintf_unfiltered (fp, "ftrace");
13025 else if (self->type == bp_static_tracepoint)
13026 fprintf_unfiltered (fp, "strace");
13027 else if (self->type == bp_tracepoint)
13028 fprintf_unfiltered (fp, "trace");
13029 else
13030 internal_error (__FILE__, __LINE__,
13031 _("unhandled tracepoint type %d"), (int) self->type);
13032
13033 fprintf_unfiltered (fp, " %s",
13034 event_location_to_string (self->location.get ()));
13035 print_recreate_thread (self, fp);
13036
13037 if (tp->pass_count)
13038 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13039 }
13040
13041 static void
13042 tracepoint_create_sals_from_location (const struct event_location *location,
13043 struct linespec_result *canonical,
13044 enum bptype type_wanted)
13045 {
13046 create_sals_from_location_default (location, canonical, type_wanted);
13047 }
13048
13049 static void
13050 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13051 struct linespec_result *canonical,
13052 gdb::unique_xmalloc_ptr<char> cond_string,
13053 gdb::unique_xmalloc_ptr<char> extra_string,
13054 enum bptype type_wanted,
13055 enum bpdisp disposition,
13056 int thread,
13057 int task, int ignore_count,
13058 const struct breakpoint_ops *ops,
13059 int from_tty, int enabled,
13060 int internal, unsigned flags)
13061 {
13062 create_breakpoints_sal_default (gdbarch, canonical,
13063 std::move (cond_string),
13064 std::move (extra_string),
13065 type_wanted,
13066 disposition, thread, task,
13067 ignore_count, ops, from_tty,
13068 enabled, internal, flags);
13069 }
13070
13071 static std::vector<symtab_and_line>
13072 tracepoint_decode_location (struct breakpoint *b,
13073 const struct event_location *location,
13074 struct program_space *search_pspace)
13075 {
13076 return decode_location_default (b, location, search_pspace);
13077 }
13078
13079 struct breakpoint_ops tracepoint_breakpoint_ops;
13080
13081 /* The breakpoint_ops structure to be use on tracepoints placed in a
13082 static probe. */
13083
13084 static void
13085 tracepoint_probe_create_sals_from_location
13086 (const struct event_location *location,
13087 struct linespec_result *canonical,
13088 enum bptype type_wanted)
13089 {
13090 /* We use the same method for breakpoint on probes. */
13091 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13092 }
13093
13094 static std::vector<symtab_and_line>
13095 tracepoint_probe_decode_location (struct breakpoint *b,
13096 const struct event_location *location,
13097 struct program_space *search_pspace)
13098 {
13099 /* We use the same method for breakpoint on probes. */
13100 return bkpt_probe_decode_location (b, location, search_pspace);
13101 }
13102
13103 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13104
13105 /* Dprintf breakpoint_ops methods. */
13106
13107 static void
13108 dprintf_re_set (struct breakpoint *b)
13109 {
13110 breakpoint_re_set_default (b);
13111
13112 /* extra_string should never be non-NULL for dprintf. */
13113 gdb_assert (b->extra_string != NULL);
13114
13115 /* 1 - connect to target 1, that can run breakpoint commands.
13116 2 - create a dprintf, which resolves fine.
13117 3 - disconnect from target 1
13118 4 - connect to target 2, that can NOT run breakpoint commands.
13119
13120 After steps #3/#4, you'll want the dprintf command list to
13121 be updated, because target 1 and 2 may well return different
13122 answers for target_can_run_breakpoint_commands().
13123 Given absence of finer grained resetting, we get to do
13124 it all the time. */
13125 if (b->extra_string != NULL)
13126 update_dprintf_command_list (b);
13127 }
13128
13129 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13130
13131 static void
13132 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13133 {
13134 fprintf_unfiltered (fp, "dprintf %s,%s",
13135 event_location_to_string (tp->location.get ()),
13136 tp->extra_string);
13137 print_recreate_thread (tp, fp);
13138 }
13139
13140 /* Implement the "after_condition_true" breakpoint_ops method for
13141 dprintf.
13142
13143 dprintf's are implemented with regular commands in their command
13144 list, but we run the commands here instead of before presenting the
13145 stop to the user, as dprintf's don't actually cause a stop. This
13146 also makes it so that the commands of multiple dprintfs at the same
13147 address are all handled. */
13148
13149 static void
13150 dprintf_after_condition_true (struct bpstats *bs)
13151 {
13152 struct bpstats tmp_bs;
13153 struct bpstats *tmp_bs_p = &tmp_bs;
13154
13155 /* dprintf's never cause a stop. This wasn't set in the
13156 check_status hook instead because that would make the dprintf's
13157 condition not be evaluated. */
13158 bs->stop = 0;
13159
13160 /* Run the command list here. Take ownership of it instead of
13161 copying. We never want these commands to run later in
13162 bpstat_do_actions, if a breakpoint that causes a stop happens to
13163 be set at same address as this dprintf, or even if running the
13164 commands here throws. */
13165 tmp_bs.commands = bs->commands;
13166 bs->commands = NULL;
13167
13168 bpstat_do_actions_1 (&tmp_bs_p);
13169
13170 /* 'tmp_bs.commands' will usually be NULL by now, but
13171 bpstat_do_actions_1 may return early without processing the whole
13172 list. */
13173 }
13174
13175 /* The breakpoint_ops structure to be used on static tracepoints with
13176 markers (`-m'). */
13177
13178 static void
13179 strace_marker_create_sals_from_location (const struct event_location *location,
13180 struct linespec_result *canonical,
13181 enum bptype type_wanted)
13182 {
13183 struct linespec_sals lsal;
13184 const char *arg_start, *arg;
13185
13186 arg = arg_start = get_linespec_location (location);
13187 lsal.sals = decode_static_tracepoint_spec (&arg);
13188
13189 std::string str (arg_start, arg - arg_start);
13190 const char *ptr = str.c_str ();
13191 canonical->location = new_linespec_location (&ptr);
13192
13193 lsal.canonical
13194 = xstrdup (event_location_to_string (canonical->location.get ()));
13195 canonical->lsals.push_back (std::move (lsal));
13196 }
13197
13198 static void
13199 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13200 struct linespec_result *canonical,
13201 gdb::unique_xmalloc_ptr<char> cond_string,
13202 gdb::unique_xmalloc_ptr<char> extra_string,
13203 enum bptype type_wanted,
13204 enum bpdisp disposition,
13205 int thread,
13206 int task, int ignore_count,
13207 const struct breakpoint_ops *ops,
13208 int from_tty, int enabled,
13209 int internal, unsigned flags)
13210 {
13211 const linespec_sals &lsal = canonical->lsals[0];
13212
13213 /* If the user is creating a static tracepoint by marker id
13214 (strace -m MARKER_ID), then store the sals index, so that
13215 breakpoint_re_set can try to match up which of the newly
13216 found markers corresponds to this one, and, don't try to
13217 expand multiple locations for each sal, given than SALS
13218 already should contain all sals for MARKER_ID. */
13219
13220 for (size_t i = 0; i < lsal.sals.size (); i++)
13221 {
13222 event_location_up location
13223 = copy_event_location (canonical->location.get ());
13224
13225 std::unique_ptr<tracepoint> tp (new tracepoint ());
13226 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13227 std::move (location), NULL,
13228 std::move (cond_string),
13229 std::move (extra_string),
13230 type_wanted, disposition,
13231 thread, task, ignore_count, ops,
13232 from_tty, enabled, internal, flags,
13233 canonical->special_display);
13234 /* Given that its possible to have multiple markers with
13235 the same string id, if the user is creating a static
13236 tracepoint by marker id ("strace -m MARKER_ID"), then
13237 store the sals index, so that breakpoint_re_set can
13238 try to match up which of the newly found markers
13239 corresponds to this one */
13240 tp->static_trace_marker_id_idx = i;
13241
13242 install_breakpoint (internal, std::move (tp), 0);
13243 }
13244 }
13245
13246 static std::vector<symtab_and_line>
13247 strace_marker_decode_location (struct breakpoint *b,
13248 const struct event_location *location,
13249 struct program_space *search_pspace)
13250 {
13251 struct tracepoint *tp = (struct tracepoint *) b;
13252 const char *s = get_linespec_location (location);
13253
13254 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13255 if (sals.size () > tp->static_trace_marker_id_idx)
13256 {
13257 sals[0] = sals[tp->static_trace_marker_id_idx];
13258 sals.resize (1);
13259 return sals;
13260 }
13261 else
13262 error (_("marker %s not found"), tp->static_trace_marker_id);
13263 }
13264
13265 static struct breakpoint_ops strace_marker_breakpoint_ops;
13266
13267 static int
13268 strace_marker_p (struct breakpoint *b)
13269 {
13270 return b->ops == &strace_marker_breakpoint_ops;
13271 }
13272
13273 /* Delete a breakpoint and clean up all traces of it in the data
13274 structures. */
13275
13276 void
13277 delete_breakpoint (struct breakpoint *bpt)
13278 {
13279 struct breakpoint *b;
13280
13281 gdb_assert (bpt != NULL);
13282
13283 /* Has this bp already been deleted? This can happen because
13284 multiple lists can hold pointers to bp's. bpstat lists are
13285 especial culprits.
13286
13287 One example of this happening is a watchpoint's scope bp. When
13288 the scope bp triggers, we notice that the watchpoint is out of
13289 scope, and delete it. We also delete its scope bp. But the
13290 scope bp is marked "auto-deleting", and is already on a bpstat.
13291 That bpstat is then checked for auto-deleting bp's, which are
13292 deleted.
13293
13294 A real solution to this problem might involve reference counts in
13295 bp's, and/or giving them pointers back to their referencing
13296 bpstat's, and teaching delete_breakpoint to only free a bp's
13297 storage when no more references were extent. A cheaper bandaid
13298 was chosen. */
13299 if (bpt->type == bp_none)
13300 return;
13301
13302 /* At least avoid this stale reference until the reference counting
13303 of breakpoints gets resolved. */
13304 if (bpt->related_breakpoint != bpt)
13305 {
13306 struct breakpoint *related;
13307 struct watchpoint *w;
13308
13309 if (bpt->type == bp_watchpoint_scope)
13310 w = (struct watchpoint *) bpt->related_breakpoint;
13311 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13312 w = (struct watchpoint *) bpt;
13313 else
13314 w = NULL;
13315 if (w != NULL)
13316 watchpoint_del_at_next_stop (w);
13317
13318 /* Unlink bpt from the bpt->related_breakpoint ring. */
13319 for (related = bpt; related->related_breakpoint != bpt;
13320 related = related->related_breakpoint);
13321 related->related_breakpoint = bpt->related_breakpoint;
13322 bpt->related_breakpoint = bpt;
13323 }
13324
13325 /* watch_command_1 creates a watchpoint but only sets its number if
13326 update_watchpoint succeeds in creating its bp_locations. If there's
13327 a problem in that process, we'll be asked to delete the half-created
13328 watchpoint. In that case, don't announce the deletion. */
13329 if (bpt->number)
13330 observer_notify_breakpoint_deleted (bpt);
13331
13332 if (breakpoint_chain == bpt)
13333 breakpoint_chain = bpt->next;
13334
13335 ALL_BREAKPOINTS (b)
13336 if (b->next == bpt)
13337 {
13338 b->next = bpt->next;
13339 break;
13340 }
13341
13342 /* Be sure no bpstat's are pointing at the breakpoint after it's
13343 been freed. */
13344 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13345 in all threads for now. Note that we cannot just remove bpstats
13346 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13347 commands are associated with the bpstat; if we remove it here,
13348 then the later call to bpstat_do_actions (&stop_bpstat); in
13349 event-top.c won't do anything, and temporary breakpoints with
13350 commands won't work. */
13351
13352 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13353
13354 /* Now that breakpoint is removed from breakpoint list, update the
13355 global location list. This will remove locations that used to
13356 belong to this breakpoint. Do this before freeing the breakpoint
13357 itself, since remove_breakpoint looks at location's owner. It
13358 might be better design to have location completely
13359 self-contained, but it's not the case now. */
13360 update_global_location_list (UGLL_DONT_INSERT);
13361
13362 /* On the chance that someone will soon try again to delete this
13363 same bp, we mark it as deleted before freeing its storage. */
13364 bpt->type = bp_none;
13365 delete bpt;
13366 }
13367
13368 /* Iterator function to call a user-provided callback function once
13369 for each of B and its related breakpoints. */
13370
13371 static void
13372 iterate_over_related_breakpoints (struct breakpoint *b,
13373 gdb::function_view<void (breakpoint *)> function)
13374 {
13375 struct breakpoint *related;
13376
13377 related = b;
13378 do
13379 {
13380 struct breakpoint *next;
13381
13382 /* FUNCTION may delete RELATED. */
13383 next = related->related_breakpoint;
13384
13385 if (next == related)
13386 {
13387 /* RELATED is the last ring entry. */
13388 function (related);
13389
13390 /* FUNCTION may have deleted it, so we'd never reach back to
13391 B. There's nothing left to do anyway, so just break
13392 out. */
13393 break;
13394 }
13395 else
13396 function (related);
13397
13398 related = next;
13399 }
13400 while (related != b);
13401 }
13402
13403 static void
13404 delete_command (const char *arg, int from_tty)
13405 {
13406 struct breakpoint *b, *b_tmp;
13407
13408 dont_repeat ();
13409
13410 if (arg == 0)
13411 {
13412 int breaks_to_delete = 0;
13413
13414 /* Delete all breakpoints if no argument. Do not delete
13415 internal breakpoints, these have to be deleted with an
13416 explicit breakpoint number argument. */
13417 ALL_BREAKPOINTS (b)
13418 if (user_breakpoint_p (b))
13419 {
13420 breaks_to_delete = 1;
13421 break;
13422 }
13423
13424 /* Ask user only if there are some breakpoints to delete. */
13425 if (!from_tty
13426 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13427 {
13428 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13429 if (user_breakpoint_p (b))
13430 delete_breakpoint (b);
13431 }
13432 }
13433 else
13434 map_breakpoint_numbers
13435 (arg, [&] (breakpoint *b)
13436 {
13437 iterate_over_related_breakpoints (b, delete_breakpoint);
13438 });
13439 }
13440
13441 /* Return true if all locations of B bound to PSPACE are pending. If
13442 PSPACE is NULL, all locations of all program spaces are
13443 considered. */
13444
13445 static int
13446 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13447 {
13448 struct bp_location *loc;
13449
13450 for (loc = b->loc; loc != NULL; loc = loc->next)
13451 if ((pspace == NULL
13452 || loc->pspace == pspace)
13453 && !loc->shlib_disabled
13454 && !loc->pspace->executing_startup)
13455 return 0;
13456 return 1;
13457 }
13458
13459 /* Subroutine of update_breakpoint_locations to simplify it.
13460 Return non-zero if multiple fns in list LOC have the same name.
13461 Null names are ignored. */
13462
13463 static int
13464 ambiguous_names_p (struct bp_location *loc)
13465 {
13466 struct bp_location *l;
13467 htab_t htab = htab_create_alloc (13, htab_hash_string,
13468 (int (*) (const void *,
13469 const void *)) streq,
13470 NULL, xcalloc, xfree);
13471
13472 for (l = loc; l != NULL; l = l->next)
13473 {
13474 const char **slot;
13475 const char *name = l->function_name;
13476
13477 /* Allow for some names to be NULL, ignore them. */
13478 if (name == NULL)
13479 continue;
13480
13481 slot = (const char **) htab_find_slot (htab, (const void *) name,
13482 INSERT);
13483 /* NOTE: We can assume slot != NULL here because xcalloc never
13484 returns NULL. */
13485 if (*slot != NULL)
13486 {
13487 htab_delete (htab);
13488 return 1;
13489 }
13490 *slot = name;
13491 }
13492
13493 htab_delete (htab);
13494 return 0;
13495 }
13496
13497 /* When symbols change, it probably means the sources changed as well,
13498 and it might mean the static tracepoint markers are no longer at
13499 the same address or line numbers they used to be at last we
13500 checked. Losing your static tracepoints whenever you rebuild is
13501 undesirable. This function tries to resync/rematch gdb static
13502 tracepoints with the markers on the target, for static tracepoints
13503 that have not been set by marker id. Static tracepoint that have
13504 been set by marker id are reset by marker id in breakpoint_re_set.
13505 The heuristic is:
13506
13507 1) For a tracepoint set at a specific address, look for a marker at
13508 the old PC. If one is found there, assume to be the same marker.
13509 If the name / string id of the marker found is different from the
13510 previous known name, assume that means the user renamed the marker
13511 in the sources, and output a warning.
13512
13513 2) For a tracepoint set at a given line number, look for a marker
13514 at the new address of the old line number. If one is found there,
13515 assume to be the same marker. If the name / string id of the
13516 marker found is different from the previous known name, assume that
13517 means the user renamed the marker in the sources, and output a
13518 warning.
13519
13520 3) If a marker is no longer found at the same address or line, it
13521 may mean the marker no longer exists. But it may also just mean
13522 the code changed a bit. Maybe the user added a few lines of code
13523 that made the marker move up or down (in line number terms). Ask
13524 the target for info about the marker with the string id as we knew
13525 it. If found, update line number and address in the matching
13526 static tracepoint. This will get confused if there's more than one
13527 marker with the same ID (possible in UST, although unadvised
13528 precisely because it confuses tools). */
13529
13530 static struct symtab_and_line
13531 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13532 {
13533 struct tracepoint *tp = (struct tracepoint *) b;
13534 struct static_tracepoint_marker marker;
13535 CORE_ADDR pc;
13536
13537 pc = sal.pc;
13538 if (sal.line)
13539 find_line_pc (sal.symtab, sal.line, &pc);
13540
13541 if (target_static_tracepoint_marker_at (pc, &marker))
13542 {
13543 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13544 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13545 b->number,
13546 tp->static_trace_marker_id, marker.str_id);
13547
13548 xfree (tp->static_trace_marker_id);
13549 tp->static_trace_marker_id = xstrdup (marker.str_id);
13550 release_static_tracepoint_marker (&marker);
13551
13552 return sal;
13553 }
13554
13555 /* Old marker wasn't found on target at lineno. Try looking it up
13556 by string ID. */
13557 if (!sal.explicit_pc
13558 && sal.line != 0
13559 && sal.symtab != NULL
13560 && tp->static_trace_marker_id != NULL)
13561 {
13562 VEC(static_tracepoint_marker_p) *markers;
13563
13564 markers
13565 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13566
13567 if (!VEC_empty(static_tracepoint_marker_p, markers))
13568 {
13569 struct symbol *sym;
13570 struct static_tracepoint_marker *tpmarker;
13571 struct ui_out *uiout = current_uiout;
13572 struct explicit_location explicit_loc;
13573
13574 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13575
13576 xfree (tp->static_trace_marker_id);
13577 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13578
13579 warning (_("marker for static tracepoint %d (%s) not "
13580 "found at previous line number"),
13581 b->number, tp->static_trace_marker_id);
13582
13583 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13584 sym = find_pc_sect_function (tpmarker->address, NULL);
13585 uiout->text ("Now in ");
13586 if (sym)
13587 {
13588 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13589 uiout->text (" at ");
13590 }
13591 uiout->field_string ("file",
13592 symtab_to_filename_for_display (sal2.symtab));
13593 uiout->text (":");
13594
13595 if (uiout->is_mi_like_p ())
13596 {
13597 const char *fullname = symtab_to_fullname (sal2.symtab);
13598
13599 uiout->field_string ("fullname", fullname);
13600 }
13601
13602 uiout->field_int ("line", sal2.line);
13603 uiout->text ("\n");
13604
13605 b->loc->line_number = sal2.line;
13606 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13607
13608 b->location.reset (NULL);
13609 initialize_explicit_location (&explicit_loc);
13610 explicit_loc.source_filename
13611 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13612 explicit_loc.line_offset.offset = b->loc->line_number;
13613 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13614 b->location = new_explicit_location (&explicit_loc);
13615
13616 /* Might be nice to check if function changed, and warn if
13617 so. */
13618
13619 release_static_tracepoint_marker (tpmarker);
13620 }
13621 }
13622 return sal;
13623 }
13624
13625 /* Returns 1 iff locations A and B are sufficiently same that
13626 we don't need to report breakpoint as changed. */
13627
13628 static int
13629 locations_are_equal (struct bp_location *a, struct bp_location *b)
13630 {
13631 while (a && b)
13632 {
13633 if (a->address != b->address)
13634 return 0;
13635
13636 if (a->shlib_disabled != b->shlib_disabled)
13637 return 0;
13638
13639 if (a->enabled != b->enabled)
13640 return 0;
13641
13642 a = a->next;
13643 b = b->next;
13644 }
13645
13646 if ((a == NULL) != (b == NULL))
13647 return 0;
13648
13649 return 1;
13650 }
13651
13652 /* Split all locations of B that are bound to PSPACE out of B's
13653 location list to a separate list and return that list's head. If
13654 PSPACE is NULL, hoist out all locations of B. */
13655
13656 static struct bp_location *
13657 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13658 {
13659 struct bp_location head;
13660 struct bp_location *i = b->loc;
13661 struct bp_location **i_link = &b->loc;
13662 struct bp_location *hoisted = &head;
13663
13664 if (pspace == NULL)
13665 {
13666 i = b->loc;
13667 b->loc = NULL;
13668 return i;
13669 }
13670
13671 head.next = NULL;
13672
13673 while (i != NULL)
13674 {
13675 if (i->pspace == pspace)
13676 {
13677 *i_link = i->next;
13678 i->next = NULL;
13679 hoisted->next = i;
13680 hoisted = i;
13681 }
13682 else
13683 i_link = &i->next;
13684 i = *i_link;
13685 }
13686
13687 return head.next;
13688 }
13689
13690 /* Create new breakpoint locations for B (a hardware or software
13691 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13692 zero, then B is a ranged breakpoint. Only recreates locations for
13693 FILTER_PSPACE. Locations of other program spaces are left
13694 untouched. */
13695
13696 void
13697 update_breakpoint_locations (struct breakpoint *b,
13698 struct program_space *filter_pspace,
13699 gdb::array_view<const symtab_and_line> sals,
13700 gdb::array_view<const symtab_and_line> sals_end)
13701 {
13702 int i;
13703 struct bp_location *existing_locations;
13704
13705 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13706 {
13707 /* Ranged breakpoints have only one start location and one end
13708 location. */
13709 b->enable_state = bp_disabled;
13710 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13711 "multiple locations found\n"),
13712 b->number);
13713 return;
13714 }
13715
13716 /* If there's no new locations, and all existing locations are
13717 pending, don't do anything. This optimizes the common case where
13718 all locations are in the same shared library, that was unloaded.
13719 We'd like to retain the location, so that when the library is
13720 loaded again, we don't loose the enabled/disabled status of the
13721 individual locations. */
13722 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13723 return;
13724
13725 existing_locations = hoist_existing_locations (b, filter_pspace);
13726
13727 for (const auto &sal : sals)
13728 {
13729 struct bp_location *new_loc;
13730
13731 switch_to_program_space_and_thread (sal.pspace);
13732
13733 new_loc = add_location_to_breakpoint (b, &sal);
13734
13735 /* Reparse conditions, they might contain references to the
13736 old symtab. */
13737 if (b->cond_string != NULL)
13738 {
13739 const char *s;
13740
13741 s = b->cond_string;
13742 TRY
13743 {
13744 new_loc->cond = parse_exp_1 (&s, sal.pc,
13745 block_for_pc (sal.pc),
13746 0);
13747 }
13748 CATCH (e, RETURN_MASK_ERROR)
13749 {
13750 warning (_("failed to reevaluate condition "
13751 "for breakpoint %d: %s"),
13752 b->number, e.message);
13753 new_loc->enabled = 0;
13754 }
13755 END_CATCH
13756 }
13757
13758 if (!sals_end.empty ())
13759 {
13760 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13761
13762 new_loc->length = end - sals[0].pc + 1;
13763 }
13764 }
13765
13766 /* If possible, carry over 'disable' status from existing
13767 breakpoints. */
13768 {
13769 struct bp_location *e = existing_locations;
13770 /* If there are multiple breakpoints with the same function name,
13771 e.g. for inline functions, comparing function names won't work.
13772 Instead compare pc addresses; this is just a heuristic as things
13773 may have moved, but in practice it gives the correct answer
13774 often enough until a better solution is found. */
13775 int have_ambiguous_names = ambiguous_names_p (b->loc);
13776
13777 for (; e; e = e->next)
13778 {
13779 if (!e->enabled && e->function_name)
13780 {
13781 struct bp_location *l = b->loc;
13782 if (have_ambiguous_names)
13783 {
13784 for (; l; l = l->next)
13785 if (breakpoint_locations_match (e, l))
13786 {
13787 l->enabled = 0;
13788 break;
13789 }
13790 }
13791 else
13792 {
13793 for (; l; l = l->next)
13794 if (l->function_name
13795 && strcmp (e->function_name, l->function_name) == 0)
13796 {
13797 l->enabled = 0;
13798 break;
13799 }
13800 }
13801 }
13802 }
13803 }
13804
13805 if (!locations_are_equal (existing_locations, b->loc))
13806 observer_notify_breakpoint_modified (b);
13807 }
13808
13809 /* Find the SaL locations corresponding to the given LOCATION.
13810 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13811
13812 static std::vector<symtab_and_line>
13813 location_to_sals (struct breakpoint *b, struct event_location *location,
13814 struct program_space *search_pspace, int *found)
13815 {
13816 struct gdb_exception exception = exception_none;
13817
13818 gdb_assert (b->ops != NULL);
13819
13820 std::vector<symtab_and_line> sals;
13821
13822 TRY
13823 {
13824 sals = b->ops->decode_location (b, location, search_pspace);
13825 }
13826 CATCH (e, RETURN_MASK_ERROR)
13827 {
13828 int not_found_and_ok = 0;
13829
13830 exception = e;
13831
13832 /* For pending breakpoints, it's expected that parsing will
13833 fail until the right shared library is loaded. User has
13834 already told to create pending breakpoints and don't need
13835 extra messages. If breakpoint is in bp_shlib_disabled
13836 state, then user already saw the message about that
13837 breakpoint being disabled, and don't want to see more
13838 errors. */
13839 if (e.error == NOT_FOUND_ERROR
13840 && (b->condition_not_parsed
13841 || (b->loc != NULL
13842 && search_pspace != NULL
13843 && b->loc->pspace != search_pspace)
13844 || (b->loc && b->loc->shlib_disabled)
13845 || (b->loc && b->loc->pspace->executing_startup)
13846 || b->enable_state == bp_disabled))
13847 not_found_and_ok = 1;
13848
13849 if (!not_found_and_ok)
13850 {
13851 /* We surely don't want to warn about the same breakpoint
13852 10 times. One solution, implemented here, is disable
13853 the breakpoint on error. Another solution would be to
13854 have separate 'warning emitted' flag. Since this
13855 happens only when a binary has changed, I don't know
13856 which approach is better. */
13857 b->enable_state = bp_disabled;
13858 throw_exception (e);
13859 }
13860 }
13861 END_CATCH
13862
13863 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13864 {
13865 for (auto &sal : sals)
13866 resolve_sal_pc (&sal);
13867 if (b->condition_not_parsed && b->extra_string != NULL)
13868 {
13869 char *cond_string, *extra_string;
13870 int thread, task;
13871
13872 find_condition_and_thread (b->extra_string, sals[0].pc,
13873 &cond_string, &thread, &task,
13874 &extra_string);
13875 gdb_assert (b->cond_string == NULL);
13876 if (cond_string)
13877 b->cond_string = cond_string;
13878 b->thread = thread;
13879 b->task = task;
13880 if (extra_string)
13881 {
13882 xfree (b->extra_string);
13883 b->extra_string = extra_string;
13884 }
13885 b->condition_not_parsed = 0;
13886 }
13887
13888 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13889 sals[0] = update_static_tracepoint (b, sals[0]);
13890
13891 *found = 1;
13892 }
13893 else
13894 *found = 0;
13895
13896 return sals;
13897 }
13898
13899 /* The default re_set method, for typical hardware or software
13900 breakpoints. Reevaluate the breakpoint and recreate its
13901 locations. */
13902
13903 static void
13904 breakpoint_re_set_default (struct breakpoint *b)
13905 {
13906 struct program_space *filter_pspace = current_program_space;
13907 std::vector<symtab_and_line> expanded, expanded_end;
13908
13909 int found;
13910 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13911 filter_pspace, &found);
13912 if (found)
13913 expanded = std::move (sals);
13914
13915 if (b->location_range_end != NULL)
13916 {
13917 std::vector<symtab_and_line> sals_end
13918 = location_to_sals (b, b->location_range_end.get (),
13919 filter_pspace, &found);
13920 if (found)
13921 expanded_end = std::move (sals_end);
13922 }
13923
13924 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13925 }
13926
13927 /* Default method for creating SALs from an address string. It basically
13928 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13929
13930 static void
13931 create_sals_from_location_default (const struct event_location *location,
13932 struct linespec_result *canonical,
13933 enum bptype type_wanted)
13934 {
13935 parse_breakpoint_sals (location, canonical);
13936 }
13937
13938 /* Call create_breakpoints_sal for the given arguments. This is the default
13939 function for the `create_breakpoints_sal' method of
13940 breakpoint_ops. */
13941
13942 static void
13943 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13944 struct linespec_result *canonical,
13945 gdb::unique_xmalloc_ptr<char> cond_string,
13946 gdb::unique_xmalloc_ptr<char> extra_string,
13947 enum bptype type_wanted,
13948 enum bpdisp disposition,
13949 int thread,
13950 int task, int ignore_count,
13951 const struct breakpoint_ops *ops,
13952 int from_tty, int enabled,
13953 int internal, unsigned flags)
13954 {
13955 create_breakpoints_sal (gdbarch, canonical,
13956 std::move (cond_string),
13957 std::move (extra_string),
13958 type_wanted, disposition,
13959 thread, task, ignore_count, ops, from_tty,
13960 enabled, internal, flags);
13961 }
13962
13963 /* Decode the line represented by S by calling decode_line_full. This is the
13964 default function for the `decode_location' method of breakpoint_ops. */
13965
13966 static std::vector<symtab_and_line>
13967 decode_location_default (struct breakpoint *b,
13968 const struct event_location *location,
13969 struct program_space *search_pspace)
13970 {
13971 struct linespec_result canonical;
13972
13973 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13974 (struct symtab *) NULL, 0,
13975 &canonical, multiple_symbols_all,
13976 b->filter);
13977
13978 /* We should get 0 or 1 resulting SALs. */
13979 gdb_assert (canonical.lsals.size () < 2);
13980
13981 if (!canonical.lsals.empty ())
13982 {
13983 const linespec_sals &lsal = canonical.lsals[0];
13984 return std::move (lsal.sals);
13985 }
13986 return {};
13987 }
13988
13989 /* Reset a breakpoint. */
13990
13991 static void
13992 breakpoint_re_set_one (breakpoint *b)
13993 {
13994 input_radix = b->input_radix;
13995 set_language (b->language);
13996
13997 b->ops->re_set (b);
13998 }
13999
14000 /* Re-set breakpoint locations for the current program space.
14001 Locations bound to other program spaces are left untouched. */
14002
14003 void
14004 breakpoint_re_set (void)
14005 {
14006 struct breakpoint *b, *b_tmp;
14007
14008 {
14009 scoped_restore_current_language save_language;
14010 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
14011 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14012
14013 /* Note: we must not try to insert locations until after all
14014 breakpoints have been re-set. Otherwise, e.g., when re-setting
14015 breakpoint 1, we'd insert the locations of breakpoint 2, which
14016 hadn't been re-set yet, and thus may have stale locations. */
14017
14018 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14019 {
14020 TRY
14021 {
14022 breakpoint_re_set_one (b);
14023 }
14024 CATCH (ex, RETURN_MASK_ALL)
14025 {
14026 exception_fprintf (gdb_stderr, ex,
14027 "Error in re-setting breakpoint %d: ",
14028 b->number);
14029 }
14030 END_CATCH
14031 }
14032
14033 jit_breakpoint_re_set ();
14034 }
14035
14036 create_overlay_event_breakpoint ();
14037 create_longjmp_master_breakpoint ();
14038 create_std_terminate_master_breakpoint ();
14039 create_exception_master_breakpoint ();
14040
14041 /* Now we can insert. */
14042 update_global_location_list (UGLL_MAY_INSERT);
14043 }
14044 \f
14045 /* Reset the thread number of this breakpoint:
14046
14047 - If the breakpoint is for all threads, leave it as-is.
14048 - Else, reset it to the current thread for inferior_ptid. */
14049 void
14050 breakpoint_re_set_thread (struct breakpoint *b)
14051 {
14052 if (b->thread != -1)
14053 {
14054 if (in_thread_list (inferior_ptid))
14055 b->thread = ptid_to_global_thread_id (inferior_ptid);
14056
14057 /* We're being called after following a fork. The new fork is
14058 selected as current, and unless this was a vfork will have a
14059 different program space from the original thread. Reset that
14060 as well. */
14061 b->loc->pspace = current_program_space;
14062 }
14063 }
14064
14065 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14066 If from_tty is nonzero, it prints a message to that effect,
14067 which ends with a period (no newline). */
14068
14069 void
14070 set_ignore_count (int bptnum, int count, int from_tty)
14071 {
14072 struct breakpoint *b;
14073
14074 if (count < 0)
14075 count = 0;
14076
14077 ALL_BREAKPOINTS (b)
14078 if (b->number == bptnum)
14079 {
14080 if (is_tracepoint (b))
14081 {
14082 if (from_tty && count != 0)
14083 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14084 bptnum);
14085 return;
14086 }
14087
14088 b->ignore_count = count;
14089 if (from_tty)
14090 {
14091 if (count == 0)
14092 printf_filtered (_("Will stop next time "
14093 "breakpoint %d is reached."),
14094 bptnum);
14095 else if (count == 1)
14096 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14097 bptnum);
14098 else
14099 printf_filtered (_("Will ignore next %d "
14100 "crossings of breakpoint %d."),
14101 count, bptnum);
14102 }
14103 observer_notify_breakpoint_modified (b);
14104 return;
14105 }
14106
14107 error (_("No breakpoint number %d."), bptnum);
14108 }
14109
14110 /* Command to set ignore-count of breakpoint N to COUNT. */
14111
14112 static void
14113 ignore_command (char *args, int from_tty)
14114 {
14115 char *p = args;
14116 int num;
14117
14118 if (p == 0)
14119 error_no_arg (_("a breakpoint number"));
14120
14121 num = get_number (&p);
14122 if (num == 0)
14123 error (_("bad breakpoint number: '%s'"), args);
14124 if (*p == 0)
14125 error (_("Second argument (specified ignore-count) is missing."));
14126
14127 set_ignore_count (num,
14128 longest_to_int (value_as_long (parse_and_eval (p))),
14129 from_tty);
14130 if (from_tty)
14131 printf_filtered ("\n");
14132 }
14133 \f
14134 /* Call FUNCTION on each of the breakpoints
14135 whose numbers are given in ARGS. */
14136
14137 static void
14138 map_breakpoint_numbers (const char *args,
14139 gdb::function_view<void (breakpoint *)> function)
14140 {
14141 int num;
14142 struct breakpoint *b, *tmp;
14143
14144 if (args == 0 || *args == '\0')
14145 error_no_arg (_("one or more breakpoint numbers"));
14146
14147 number_or_range_parser parser (args);
14148
14149 while (!parser.finished ())
14150 {
14151 const char *p = parser.cur_tok ();
14152 bool match = false;
14153
14154 num = parser.get_number ();
14155 if (num == 0)
14156 {
14157 warning (_("bad breakpoint number at or near '%s'"), p);
14158 }
14159 else
14160 {
14161 ALL_BREAKPOINTS_SAFE (b, tmp)
14162 if (b->number == num)
14163 {
14164 match = true;
14165 function (b);
14166 break;
14167 }
14168 if (!match)
14169 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14170 }
14171 }
14172 }
14173
14174 static struct bp_location *
14175 find_location_by_number (const char *number)
14176 {
14177 const char *p1;
14178 int bp_num;
14179 int loc_num;
14180 struct breakpoint *b;
14181 struct bp_location *loc;
14182
14183 p1 = number;
14184 bp_num = get_number_trailer (&p1, '.');
14185 if (bp_num == 0 || p1[0] != '.')
14186 error (_("Bad breakpoint number '%s'"), number);
14187
14188 ALL_BREAKPOINTS (b)
14189 if (b->number == bp_num)
14190 {
14191 break;
14192 }
14193
14194 if (!b || b->number != bp_num)
14195 error (_("Bad breakpoint number '%s'"), number);
14196
14197 /* Skip the dot. */
14198 ++p1;
14199 const char *save = p1;
14200 loc_num = get_number (&p1);
14201 if (loc_num == 0)
14202 error (_("Bad breakpoint location number '%s'"), number);
14203
14204 --loc_num;
14205 loc = b->loc;
14206 for (;loc_num && loc; --loc_num, loc = loc->next)
14207 ;
14208 if (!loc)
14209 error (_("Bad breakpoint location number '%s'"), save);
14210
14211 return loc;
14212 }
14213
14214
14215 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14216 If from_tty is nonzero, it prints a message to that effect,
14217 which ends with a period (no newline). */
14218
14219 void
14220 disable_breakpoint (struct breakpoint *bpt)
14221 {
14222 /* Never disable a watchpoint scope breakpoint; we want to
14223 hit them when we leave scope so we can delete both the
14224 watchpoint and its scope breakpoint at that time. */
14225 if (bpt->type == bp_watchpoint_scope)
14226 return;
14227
14228 bpt->enable_state = bp_disabled;
14229
14230 /* Mark breakpoint locations modified. */
14231 mark_breakpoint_modified (bpt);
14232
14233 if (target_supports_enable_disable_tracepoint ()
14234 && current_trace_status ()->running && is_tracepoint (bpt))
14235 {
14236 struct bp_location *location;
14237
14238 for (location = bpt->loc; location; location = location->next)
14239 target_disable_tracepoint (location);
14240 }
14241
14242 update_global_location_list (UGLL_DONT_INSERT);
14243
14244 observer_notify_breakpoint_modified (bpt);
14245 }
14246
14247 static void
14248 disable_command (const char *args, int from_tty)
14249 {
14250 if (args == 0)
14251 {
14252 struct breakpoint *bpt;
14253
14254 ALL_BREAKPOINTS (bpt)
14255 if (user_breakpoint_p (bpt))
14256 disable_breakpoint (bpt);
14257 }
14258 else
14259 {
14260 std::string num = extract_arg (&args);
14261
14262 while (!num.empty ())
14263 {
14264 if (num.find ('.') != std::string::npos)
14265 {
14266 struct bp_location *loc = find_location_by_number (num.c_str ());
14267
14268 if (loc)
14269 {
14270 if (loc->enabled)
14271 {
14272 loc->enabled = 0;
14273 mark_breakpoint_location_modified (loc);
14274 }
14275 if (target_supports_enable_disable_tracepoint ()
14276 && current_trace_status ()->running && loc->owner
14277 && is_tracepoint (loc->owner))
14278 target_disable_tracepoint (loc);
14279 }
14280 update_global_location_list (UGLL_DONT_INSERT);
14281 }
14282 else
14283 map_breakpoint_numbers
14284 (num.c_str (), [&] (breakpoint *b)
14285 {
14286 iterate_over_related_breakpoints (b, disable_breakpoint);
14287 });
14288 num = extract_arg (&args);
14289 }
14290 }
14291 }
14292
14293 static void
14294 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14295 int count)
14296 {
14297 int target_resources_ok;
14298
14299 if (bpt->type == bp_hardware_breakpoint)
14300 {
14301 int i;
14302 i = hw_breakpoint_used_count ();
14303 target_resources_ok =
14304 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14305 i + 1, 0);
14306 if (target_resources_ok == 0)
14307 error (_("No hardware breakpoint support in the target."));
14308 else if (target_resources_ok < 0)
14309 error (_("Hardware breakpoints used exceeds limit."));
14310 }
14311
14312 if (is_watchpoint (bpt))
14313 {
14314 /* Initialize it just to avoid a GCC false warning. */
14315 enum enable_state orig_enable_state = bp_disabled;
14316
14317 TRY
14318 {
14319 struct watchpoint *w = (struct watchpoint *) bpt;
14320
14321 orig_enable_state = bpt->enable_state;
14322 bpt->enable_state = bp_enabled;
14323 update_watchpoint (w, 1 /* reparse */);
14324 }
14325 CATCH (e, RETURN_MASK_ALL)
14326 {
14327 bpt->enable_state = orig_enable_state;
14328 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14329 bpt->number);
14330 return;
14331 }
14332 END_CATCH
14333 }
14334
14335 bpt->enable_state = bp_enabled;
14336
14337 /* Mark breakpoint locations modified. */
14338 mark_breakpoint_modified (bpt);
14339
14340 if (target_supports_enable_disable_tracepoint ()
14341 && current_trace_status ()->running && is_tracepoint (bpt))
14342 {
14343 struct bp_location *location;
14344
14345 for (location = bpt->loc; location; location = location->next)
14346 target_enable_tracepoint (location);
14347 }
14348
14349 bpt->disposition = disposition;
14350 bpt->enable_count = count;
14351 update_global_location_list (UGLL_MAY_INSERT);
14352
14353 observer_notify_breakpoint_modified (bpt);
14354 }
14355
14356
14357 void
14358 enable_breakpoint (struct breakpoint *bpt)
14359 {
14360 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14361 }
14362
14363 /* The enable command enables the specified breakpoints (or all defined
14364 breakpoints) so they once again become (or continue to be) effective
14365 in stopping the inferior. */
14366
14367 static void
14368 enable_command (const char *args, int from_tty)
14369 {
14370 if (args == 0)
14371 {
14372 struct breakpoint *bpt;
14373
14374 ALL_BREAKPOINTS (bpt)
14375 if (user_breakpoint_p (bpt))
14376 enable_breakpoint (bpt);
14377 }
14378 else
14379 {
14380 std::string num = extract_arg (&args);
14381
14382 while (!num.empty ())
14383 {
14384 if (num.find ('.') != std::string::npos)
14385 {
14386 struct bp_location *loc = find_location_by_number (num.c_str ());
14387
14388 if (loc)
14389 {
14390 if (!loc->enabled)
14391 {
14392 loc->enabled = 1;
14393 mark_breakpoint_location_modified (loc);
14394 }
14395 if (target_supports_enable_disable_tracepoint ()
14396 && current_trace_status ()->running && loc->owner
14397 && is_tracepoint (loc->owner))
14398 target_enable_tracepoint (loc);
14399 }
14400 update_global_location_list (UGLL_MAY_INSERT);
14401 }
14402 else
14403 map_breakpoint_numbers
14404 (num.c_str (), [&] (breakpoint *b)
14405 {
14406 iterate_over_related_breakpoints (b, enable_breakpoint);
14407 });
14408 num = extract_arg (&args);
14409 }
14410 }
14411 }
14412
14413 static void
14414 enable_once_command (const char *args, int from_tty)
14415 {
14416 map_breakpoint_numbers
14417 (args, [&] (breakpoint *b)
14418 {
14419 iterate_over_related_breakpoints
14420 (b, [&] (breakpoint *bpt)
14421 {
14422 enable_breakpoint_disp (bpt, disp_disable, 1);
14423 });
14424 });
14425 }
14426
14427 static void
14428 enable_count_command (const char *args, int from_tty)
14429 {
14430 int count;
14431
14432 if (args == NULL)
14433 error_no_arg (_("hit count"));
14434
14435 count = get_number (&args);
14436
14437 map_breakpoint_numbers
14438 (args, [&] (breakpoint *b)
14439 {
14440 iterate_over_related_breakpoints
14441 (b, [&] (breakpoint *bpt)
14442 {
14443 enable_breakpoint_disp (bpt, disp_disable, count);
14444 });
14445 });
14446 }
14447
14448 static void
14449 enable_delete_command (const char *args, int from_tty)
14450 {
14451 map_breakpoint_numbers
14452 (args, [&] (breakpoint *b)
14453 {
14454 iterate_over_related_breakpoints
14455 (b, [&] (breakpoint *bpt)
14456 {
14457 enable_breakpoint_disp (bpt, disp_del, 1);
14458 });
14459 });
14460 }
14461 \f
14462 static void
14463 set_breakpoint_cmd (const char *args, int from_tty)
14464 {
14465 }
14466
14467 static void
14468 show_breakpoint_cmd (const char *args, int from_tty)
14469 {
14470 }
14471
14472 /* Invalidate last known value of any hardware watchpoint if
14473 the memory which that value represents has been written to by
14474 GDB itself. */
14475
14476 static void
14477 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14478 CORE_ADDR addr, ssize_t len,
14479 const bfd_byte *data)
14480 {
14481 struct breakpoint *bp;
14482
14483 ALL_BREAKPOINTS (bp)
14484 if (bp->enable_state == bp_enabled
14485 && bp->type == bp_hardware_watchpoint)
14486 {
14487 struct watchpoint *wp = (struct watchpoint *) bp;
14488
14489 if (wp->val_valid && wp->val)
14490 {
14491 struct bp_location *loc;
14492
14493 for (loc = bp->loc; loc != NULL; loc = loc->next)
14494 if (loc->loc_type == bp_loc_hardware_watchpoint
14495 && loc->address + loc->length > addr
14496 && addr + len > loc->address)
14497 {
14498 value_free (wp->val);
14499 wp->val = NULL;
14500 wp->val_valid = 0;
14501 }
14502 }
14503 }
14504 }
14505
14506 /* Create and insert a breakpoint for software single step. */
14507
14508 void
14509 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14510 const address_space *aspace,
14511 CORE_ADDR next_pc)
14512 {
14513 struct thread_info *tp = inferior_thread ();
14514 struct symtab_and_line sal;
14515 CORE_ADDR pc = next_pc;
14516
14517 if (tp->control.single_step_breakpoints == NULL)
14518 {
14519 tp->control.single_step_breakpoints
14520 = new_single_step_breakpoint (tp->global_num, gdbarch);
14521 }
14522
14523 sal = find_pc_line (pc, 0);
14524 sal.pc = pc;
14525 sal.section = find_pc_overlay (pc);
14526 sal.explicit_pc = 1;
14527 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14528
14529 update_global_location_list (UGLL_INSERT);
14530 }
14531
14532 /* Insert single step breakpoints according to the current state. */
14533
14534 int
14535 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14536 {
14537 struct regcache *regcache = get_current_regcache ();
14538 std::vector<CORE_ADDR> next_pcs;
14539
14540 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14541
14542 if (!next_pcs.empty ())
14543 {
14544 struct frame_info *frame = get_current_frame ();
14545 const address_space *aspace = get_frame_address_space (frame);
14546
14547 for (CORE_ADDR pc : next_pcs)
14548 insert_single_step_breakpoint (gdbarch, aspace, pc);
14549
14550 return 1;
14551 }
14552 else
14553 return 0;
14554 }
14555
14556 /* See breakpoint.h. */
14557
14558 int
14559 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14560 const address_space *aspace,
14561 CORE_ADDR pc)
14562 {
14563 struct bp_location *loc;
14564
14565 for (loc = bp->loc; loc != NULL; loc = loc->next)
14566 if (loc->inserted
14567 && breakpoint_location_address_match (loc, aspace, pc))
14568 return 1;
14569
14570 return 0;
14571 }
14572
14573 /* Check whether a software single-step breakpoint is inserted at
14574 PC. */
14575
14576 int
14577 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14578 CORE_ADDR pc)
14579 {
14580 struct breakpoint *bpt;
14581
14582 ALL_BREAKPOINTS (bpt)
14583 {
14584 if (bpt->type == bp_single_step
14585 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14586 return 1;
14587 }
14588 return 0;
14589 }
14590
14591 /* Tracepoint-specific operations. */
14592
14593 /* Set tracepoint count to NUM. */
14594 static void
14595 set_tracepoint_count (int num)
14596 {
14597 tracepoint_count = num;
14598 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14599 }
14600
14601 static void
14602 trace_command (char *arg_in, int from_tty)
14603 {
14604 const char *arg = arg_in;
14605 struct breakpoint_ops *ops;
14606
14607 event_location_up location = string_to_event_location (&arg,
14608 current_language);
14609 if (location != NULL
14610 && event_location_type (location.get ()) == PROBE_LOCATION)
14611 ops = &tracepoint_probe_breakpoint_ops;
14612 else
14613 ops = &tracepoint_breakpoint_ops;
14614
14615 create_breakpoint (get_current_arch (),
14616 location.get (),
14617 NULL, 0, arg, 1 /* parse arg */,
14618 0 /* tempflag */,
14619 bp_tracepoint /* type_wanted */,
14620 0 /* Ignore count */,
14621 pending_break_support,
14622 ops,
14623 from_tty,
14624 1 /* enabled */,
14625 0 /* internal */, 0);
14626 }
14627
14628 static void
14629 ftrace_command (char *arg_in, int from_tty)
14630 {
14631 const char *arg = arg_in;
14632 event_location_up location = string_to_event_location (&arg,
14633 current_language);
14634 create_breakpoint (get_current_arch (),
14635 location.get (),
14636 NULL, 0, arg, 1 /* parse arg */,
14637 0 /* tempflag */,
14638 bp_fast_tracepoint /* type_wanted */,
14639 0 /* Ignore count */,
14640 pending_break_support,
14641 &tracepoint_breakpoint_ops,
14642 from_tty,
14643 1 /* enabled */,
14644 0 /* internal */, 0);
14645 }
14646
14647 /* strace command implementation. Creates a static tracepoint. */
14648
14649 static void
14650 strace_command (char *arg_in, int from_tty)
14651 {
14652 const char *arg = arg_in;
14653 struct breakpoint_ops *ops;
14654 event_location_up location;
14655
14656 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14657 or with a normal static tracepoint. */
14658 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14659 {
14660 ops = &strace_marker_breakpoint_ops;
14661 location = new_linespec_location (&arg);
14662 }
14663 else
14664 {
14665 ops = &tracepoint_breakpoint_ops;
14666 location = string_to_event_location (&arg, current_language);
14667 }
14668
14669 create_breakpoint (get_current_arch (),
14670 location.get (),
14671 NULL, 0, arg, 1 /* parse arg */,
14672 0 /* tempflag */,
14673 bp_static_tracepoint /* type_wanted */,
14674 0 /* Ignore count */,
14675 pending_break_support,
14676 ops,
14677 from_tty,
14678 1 /* enabled */,
14679 0 /* internal */, 0);
14680 }
14681
14682 /* Set up a fake reader function that gets command lines from a linked
14683 list that was acquired during tracepoint uploading. */
14684
14685 static struct uploaded_tp *this_utp;
14686 static int next_cmd;
14687
14688 static char *
14689 read_uploaded_action (void)
14690 {
14691 char *rslt;
14692
14693 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14694
14695 next_cmd++;
14696
14697 return rslt;
14698 }
14699
14700 /* Given information about a tracepoint as recorded on a target (which
14701 can be either a live system or a trace file), attempt to create an
14702 equivalent GDB tracepoint. This is not a reliable process, since
14703 the target does not necessarily have all the information used when
14704 the tracepoint was originally defined. */
14705
14706 struct tracepoint *
14707 create_tracepoint_from_upload (struct uploaded_tp *utp)
14708 {
14709 const char *addr_str;
14710 char small_buf[100];
14711 struct tracepoint *tp;
14712
14713 if (utp->at_string)
14714 addr_str = utp->at_string;
14715 else
14716 {
14717 /* In the absence of a source location, fall back to raw
14718 address. Since there is no way to confirm that the address
14719 means the same thing as when the trace was started, warn the
14720 user. */
14721 warning (_("Uploaded tracepoint %d has no "
14722 "source location, using raw address"),
14723 utp->number);
14724 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14725 addr_str = small_buf;
14726 }
14727
14728 /* There's not much we can do with a sequence of bytecodes. */
14729 if (utp->cond && !utp->cond_string)
14730 warning (_("Uploaded tracepoint %d condition "
14731 "has no source form, ignoring it"),
14732 utp->number);
14733
14734 event_location_up location = string_to_event_location (&addr_str,
14735 current_language);
14736 if (!create_breakpoint (get_current_arch (),
14737 location.get (),
14738 utp->cond_string, -1, addr_str,
14739 0 /* parse cond/thread */,
14740 0 /* tempflag */,
14741 utp->type /* type_wanted */,
14742 0 /* Ignore count */,
14743 pending_break_support,
14744 &tracepoint_breakpoint_ops,
14745 0 /* from_tty */,
14746 utp->enabled /* enabled */,
14747 0 /* internal */,
14748 CREATE_BREAKPOINT_FLAGS_INSERTED))
14749 return NULL;
14750
14751 /* Get the tracepoint we just created. */
14752 tp = get_tracepoint (tracepoint_count);
14753 gdb_assert (tp != NULL);
14754
14755 if (utp->pass > 0)
14756 {
14757 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14758 tp->number);
14759
14760 trace_pass_command (small_buf, 0);
14761 }
14762
14763 /* If we have uploaded versions of the original commands, set up a
14764 special-purpose "reader" function and call the usual command line
14765 reader, then pass the result to the breakpoint command-setting
14766 function. */
14767 if (!VEC_empty (char_ptr, utp->cmd_strings))
14768 {
14769 command_line_up cmd_list;
14770
14771 this_utp = utp;
14772 next_cmd = 0;
14773
14774 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14775
14776 breakpoint_set_commands (tp, std::move (cmd_list));
14777 }
14778 else if (!VEC_empty (char_ptr, utp->actions)
14779 || !VEC_empty (char_ptr, utp->step_actions))
14780 warning (_("Uploaded tracepoint %d actions "
14781 "have no source form, ignoring them"),
14782 utp->number);
14783
14784 /* Copy any status information that might be available. */
14785 tp->hit_count = utp->hit_count;
14786 tp->traceframe_usage = utp->traceframe_usage;
14787
14788 return tp;
14789 }
14790
14791 /* Print information on tracepoint number TPNUM_EXP, or all if
14792 omitted. */
14793
14794 static void
14795 info_tracepoints_command (char *args, int from_tty)
14796 {
14797 struct ui_out *uiout = current_uiout;
14798 int num_printed;
14799
14800 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14801
14802 if (num_printed == 0)
14803 {
14804 if (args == NULL || *args == '\0')
14805 uiout->message ("No tracepoints.\n");
14806 else
14807 uiout->message ("No tracepoint matching '%s'.\n", args);
14808 }
14809
14810 default_collect_info ();
14811 }
14812
14813 /* The 'enable trace' command enables tracepoints.
14814 Not supported by all targets. */
14815 static void
14816 enable_trace_command (char *args, int from_tty)
14817 {
14818 enable_command (args, from_tty);
14819 }
14820
14821 /* The 'disable trace' command disables tracepoints.
14822 Not supported by all targets. */
14823 static void
14824 disable_trace_command (char *args, int from_tty)
14825 {
14826 disable_command (args, from_tty);
14827 }
14828
14829 /* Remove a tracepoint (or all if no argument). */
14830 static void
14831 delete_trace_command (const char *arg, int from_tty)
14832 {
14833 struct breakpoint *b, *b_tmp;
14834
14835 dont_repeat ();
14836
14837 if (arg == 0)
14838 {
14839 int breaks_to_delete = 0;
14840
14841 /* Delete all breakpoints if no argument.
14842 Do not delete internal or call-dummy breakpoints, these
14843 have to be deleted with an explicit breakpoint number
14844 argument. */
14845 ALL_TRACEPOINTS (b)
14846 if (is_tracepoint (b) && user_breakpoint_p (b))
14847 {
14848 breaks_to_delete = 1;
14849 break;
14850 }
14851
14852 /* Ask user only if there are some breakpoints to delete. */
14853 if (!from_tty
14854 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14855 {
14856 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14857 if (is_tracepoint (b) && user_breakpoint_p (b))
14858 delete_breakpoint (b);
14859 }
14860 }
14861 else
14862 map_breakpoint_numbers
14863 (arg, [&] (breakpoint *b)
14864 {
14865 iterate_over_related_breakpoints (b, delete_breakpoint);
14866 });
14867 }
14868
14869 /* Helper function for trace_pass_command. */
14870
14871 static void
14872 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14873 {
14874 tp->pass_count = count;
14875 observer_notify_breakpoint_modified (tp);
14876 if (from_tty)
14877 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14878 tp->number, count);
14879 }
14880
14881 /* Set passcount for tracepoint.
14882
14883 First command argument is passcount, second is tracepoint number.
14884 If tracepoint number omitted, apply to most recently defined.
14885 Also accepts special argument "all". */
14886
14887 static void
14888 trace_pass_command (char *args, int from_tty)
14889 {
14890 struct tracepoint *t1;
14891 unsigned int count;
14892
14893 if (args == 0 || *args == 0)
14894 error (_("passcount command requires an "
14895 "argument (count + optional TP num)"));
14896
14897 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
14898
14899 args = skip_spaces (args);
14900 if (*args && strncasecmp (args, "all", 3) == 0)
14901 {
14902 struct breakpoint *b;
14903
14904 args += 3; /* Skip special argument "all". */
14905 if (*args)
14906 error (_("Junk at end of arguments."));
14907
14908 ALL_TRACEPOINTS (b)
14909 {
14910 t1 = (struct tracepoint *) b;
14911 trace_pass_set_count (t1, count, from_tty);
14912 }
14913 }
14914 else if (*args == '\0')
14915 {
14916 t1 = get_tracepoint_by_number (&args, NULL);
14917 if (t1)
14918 trace_pass_set_count (t1, count, from_tty);
14919 }
14920 else
14921 {
14922 number_or_range_parser parser (args);
14923 while (!parser.finished ())
14924 {
14925 t1 = get_tracepoint_by_number (&args, &parser);
14926 if (t1)
14927 trace_pass_set_count (t1, count, from_tty);
14928 }
14929 }
14930 }
14931
14932 struct tracepoint *
14933 get_tracepoint (int num)
14934 {
14935 struct breakpoint *t;
14936
14937 ALL_TRACEPOINTS (t)
14938 if (t->number == num)
14939 return (struct tracepoint *) t;
14940
14941 return NULL;
14942 }
14943
14944 /* Find the tracepoint with the given target-side number (which may be
14945 different from the tracepoint number after disconnecting and
14946 reconnecting). */
14947
14948 struct tracepoint *
14949 get_tracepoint_by_number_on_target (int num)
14950 {
14951 struct breakpoint *b;
14952
14953 ALL_TRACEPOINTS (b)
14954 {
14955 struct tracepoint *t = (struct tracepoint *) b;
14956
14957 if (t->number_on_target == num)
14958 return t;
14959 }
14960
14961 return NULL;
14962 }
14963
14964 /* Utility: parse a tracepoint number and look it up in the list.
14965 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14966 If the argument is missing, the most recent tracepoint
14967 (tracepoint_count) is returned. */
14968
14969 struct tracepoint *
14970 get_tracepoint_by_number (char **arg,
14971 number_or_range_parser *parser)
14972 {
14973 struct breakpoint *t;
14974 int tpnum;
14975 char *instring = arg == NULL ? NULL : *arg;
14976
14977 if (parser != NULL)
14978 {
14979 gdb_assert (!parser->finished ());
14980 tpnum = parser->get_number ();
14981 }
14982 else if (arg == NULL || *arg == NULL || ! **arg)
14983 tpnum = tracepoint_count;
14984 else
14985 tpnum = get_number (arg);
14986
14987 if (tpnum <= 0)
14988 {
14989 if (instring && *instring)
14990 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14991 instring);
14992 else
14993 printf_filtered (_("No previous tracepoint\n"));
14994 return NULL;
14995 }
14996
14997 ALL_TRACEPOINTS (t)
14998 if (t->number == tpnum)
14999 {
15000 return (struct tracepoint *) t;
15001 }
15002
15003 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15004 return NULL;
15005 }
15006
15007 void
15008 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15009 {
15010 if (b->thread != -1)
15011 fprintf_unfiltered (fp, " thread %d", b->thread);
15012
15013 if (b->task != 0)
15014 fprintf_unfiltered (fp, " task %d", b->task);
15015
15016 fprintf_unfiltered (fp, "\n");
15017 }
15018
15019 /* Save information on user settable breakpoints (watchpoints, etc) to
15020 a new script file named FILENAME. If FILTER is non-NULL, call it
15021 on each breakpoint and only include the ones for which it returns
15022 non-zero. */
15023
15024 static void
15025 save_breakpoints (const char *filename, int from_tty,
15026 int (*filter) (const struct breakpoint *))
15027 {
15028 struct breakpoint *tp;
15029 int any = 0;
15030 int extra_trace_bits = 0;
15031
15032 if (filename == 0 || *filename == 0)
15033 error (_("Argument required (file name in which to save)"));
15034
15035 /* See if we have anything to save. */
15036 ALL_BREAKPOINTS (tp)
15037 {
15038 /* Skip internal and momentary breakpoints. */
15039 if (!user_breakpoint_p (tp))
15040 continue;
15041
15042 /* If we have a filter, only save the breakpoints it accepts. */
15043 if (filter && !filter (tp))
15044 continue;
15045
15046 any = 1;
15047
15048 if (is_tracepoint (tp))
15049 {
15050 extra_trace_bits = 1;
15051
15052 /* We can stop searching. */
15053 break;
15054 }
15055 }
15056
15057 if (!any)
15058 {
15059 warning (_("Nothing to save."));
15060 return;
15061 }
15062
15063 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15064
15065 stdio_file fp;
15066
15067 if (!fp.open (expanded_filename.get (), "w"))
15068 error (_("Unable to open file '%s' for saving (%s)"),
15069 expanded_filename.get (), safe_strerror (errno));
15070
15071 if (extra_trace_bits)
15072 save_trace_state_variables (&fp);
15073
15074 ALL_BREAKPOINTS (tp)
15075 {
15076 /* Skip internal and momentary breakpoints. */
15077 if (!user_breakpoint_p (tp))
15078 continue;
15079
15080 /* If we have a filter, only save the breakpoints it accepts. */
15081 if (filter && !filter (tp))
15082 continue;
15083
15084 tp->ops->print_recreate (tp, &fp);
15085
15086 /* Note, we can't rely on tp->number for anything, as we can't
15087 assume the recreated breakpoint numbers will match. Use $bpnum
15088 instead. */
15089
15090 if (tp->cond_string)
15091 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15092
15093 if (tp->ignore_count)
15094 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15095
15096 if (tp->type != bp_dprintf && tp->commands)
15097 {
15098 fp.puts (" commands\n");
15099
15100 current_uiout->redirect (&fp);
15101 TRY
15102 {
15103 print_command_lines (current_uiout, tp->commands.get (), 2);
15104 }
15105 CATCH (ex, RETURN_MASK_ALL)
15106 {
15107 current_uiout->redirect (NULL);
15108 throw_exception (ex);
15109 }
15110 END_CATCH
15111
15112 current_uiout->redirect (NULL);
15113 fp.puts (" end\n");
15114 }
15115
15116 if (tp->enable_state == bp_disabled)
15117 fp.puts ("disable $bpnum\n");
15118
15119 /* If this is a multi-location breakpoint, check if the locations
15120 should be individually disabled. Watchpoint locations are
15121 special, and not user visible. */
15122 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15123 {
15124 struct bp_location *loc;
15125 int n = 1;
15126
15127 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15128 if (!loc->enabled)
15129 fp.printf ("disable $bpnum.%d\n", n);
15130 }
15131 }
15132
15133 if (extra_trace_bits && *default_collect)
15134 fp.printf ("set default-collect %s\n", default_collect);
15135
15136 if (from_tty)
15137 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15138 }
15139
15140 /* The `save breakpoints' command. */
15141
15142 static void
15143 save_breakpoints_command (const char *args, int from_tty)
15144 {
15145 save_breakpoints (args, from_tty, NULL);
15146 }
15147
15148 /* The `save tracepoints' command. */
15149
15150 static void
15151 save_tracepoints_command (const char *args, int from_tty)
15152 {
15153 save_breakpoints (args, from_tty, is_tracepoint);
15154 }
15155
15156 /* Create a vector of all tracepoints. */
15157
15158 VEC(breakpoint_p) *
15159 all_tracepoints (void)
15160 {
15161 VEC(breakpoint_p) *tp_vec = 0;
15162 struct breakpoint *tp;
15163
15164 ALL_TRACEPOINTS (tp)
15165 {
15166 VEC_safe_push (breakpoint_p, tp_vec, tp);
15167 }
15168
15169 return tp_vec;
15170 }
15171
15172 \f
15173 /* This help string is used to consolidate all the help string for specifying
15174 locations used by several commands. */
15175
15176 #define LOCATION_HELP_STRING \
15177 "Linespecs are colon-separated lists of location parameters, such as\n\
15178 source filename, function name, label name, and line number.\n\
15179 Example: To specify the start of a label named \"the_top\" in the\n\
15180 function \"fact\" in the file \"factorial.c\", use\n\
15181 \"factorial.c:fact:the_top\".\n\
15182 \n\
15183 Address locations begin with \"*\" and specify an exact address in the\n\
15184 program. Example: To specify the fourth byte past the start function\n\
15185 \"main\", use \"*main + 4\".\n\
15186 \n\
15187 Explicit locations are similar to linespecs but use an option/argument\n\
15188 syntax to specify location parameters.\n\
15189 Example: To specify the start of the label named \"the_top\" in the\n\
15190 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15191 -function fact -label the_top\".\n"
15192
15193 /* This help string is used for the break, hbreak, tbreak and thbreak
15194 commands. It is defined as a macro to prevent duplication.
15195 COMMAND should be a string constant containing the name of the
15196 command. */
15197
15198 #define BREAK_ARGS_HELP(command) \
15199 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15200 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15201 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15202 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15203 `-probe-dtrace' (for a DTrace probe).\n\
15204 LOCATION may be a linespec, address, or explicit location as described\n\
15205 below.\n\
15206 \n\
15207 With no LOCATION, uses current execution address of the selected\n\
15208 stack frame. This is useful for breaking on return to a stack frame.\n\
15209 \n\
15210 THREADNUM is the number from \"info threads\".\n\
15211 CONDITION is a boolean expression.\n\
15212 \n" LOCATION_HELP_STRING "\n\
15213 Multiple breakpoints at one place are permitted, and useful if their\n\
15214 conditions are different.\n\
15215 \n\
15216 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15217
15218 /* List of subcommands for "catch". */
15219 static struct cmd_list_element *catch_cmdlist;
15220
15221 /* List of subcommands for "tcatch". */
15222 static struct cmd_list_element *tcatch_cmdlist;
15223
15224 void
15225 add_catch_command (const char *name, const char *docstring,
15226 cmd_sfunc_ftype *sfunc,
15227 completer_ftype *completer,
15228 void *user_data_catch,
15229 void *user_data_tcatch)
15230 {
15231 struct cmd_list_element *command;
15232
15233 command = add_cmd (name, class_breakpoint, docstring,
15234 &catch_cmdlist);
15235 set_cmd_sfunc (command, sfunc);
15236 set_cmd_context (command, user_data_catch);
15237 set_cmd_completer (command, completer);
15238
15239 command = add_cmd (name, class_breakpoint, docstring,
15240 &tcatch_cmdlist);
15241 set_cmd_sfunc (command, sfunc);
15242 set_cmd_context (command, user_data_tcatch);
15243 set_cmd_completer (command, completer);
15244 }
15245
15246 static void
15247 save_command (const char *arg, int from_tty)
15248 {
15249 printf_unfiltered (_("\"save\" must be followed by "
15250 "the name of a save subcommand.\n"));
15251 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15252 }
15253
15254 struct breakpoint *
15255 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15256 void *data)
15257 {
15258 struct breakpoint *b, *b_tmp;
15259
15260 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15261 {
15262 if ((*callback) (b, data))
15263 return b;
15264 }
15265
15266 return NULL;
15267 }
15268
15269 /* Zero if any of the breakpoint's locations could be a location where
15270 functions have been inlined, nonzero otherwise. */
15271
15272 static int
15273 is_non_inline_function (struct breakpoint *b)
15274 {
15275 /* The shared library event breakpoint is set on the address of a
15276 non-inline function. */
15277 if (b->type == bp_shlib_event)
15278 return 1;
15279
15280 return 0;
15281 }
15282
15283 /* Nonzero if the specified PC cannot be a location where functions
15284 have been inlined. */
15285
15286 int
15287 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15288 const struct target_waitstatus *ws)
15289 {
15290 struct breakpoint *b;
15291 struct bp_location *bl;
15292
15293 ALL_BREAKPOINTS (b)
15294 {
15295 if (!is_non_inline_function (b))
15296 continue;
15297
15298 for (bl = b->loc; bl != NULL; bl = bl->next)
15299 {
15300 if (!bl->shlib_disabled
15301 && bpstat_check_location (bl, aspace, pc, ws))
15302 return 1;
15303 }
15304 }
15305
15306 return 0;
15307 }
15308
15309 /* Remove any references to OBJFILE which is going to be freed. */
15310
15311 void
15312 breakpoint_free_objfile (struct objfile *objfile)
15313 {
15314 struct bp_location **locp, *loc;
15315
15316 ALL_BP_LOCATIONS (loc, locp)
15317 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15318 loc->symtab = NULL;
15319 }
15320
15321 void
15322 initialize_breakpoint_ops (void)
15323 {
15324 static int initialized = 0;
15325
15326 struct breakpoint_ops *ops;
15327
15328 if (initialized)
15329 return;
15330 initialized = 1;
15331
15332 /* The breakpoint_ops structure to be inherit by all kinds of
15333 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15334 internal and momentary breakpoints, etc.). */
15335 ops = &bkpt_base_breakpoint_ops;
15336 *ops = base_breakpoint_ops;
15337 ops->re_set = bkpt_re_set;
15338 ops->insert_location = bkpt_insert_location;
15339 ops->remove_location = bkpt_remove_location;
15340 ops->breakpoint_hit = bkpt_breakpoint_hit;
15341 ops->create_sals_from_location = bkpt_create_sals_from_location;
15342 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15343 ops->decode_location = bkpt_decode_location;
15344
15345 /* The breakpoint_ops structure to be used in regular breakpoints. */
15346 ops = &bkpt_breakpoint_ops;
15347 *ops = bkpt_base_breakpoint_ops;
15348 ops->re_set = bkpt_re_set;
15349 ops->resources_needed = bkpt_resources_needed;
15350 ops->print_it = bkpt_print_it;
15351 ops->print_mention = bkpt_print_mention;
15352 ops->print_recreate = bkpt_print_recreate;
15353
15354 /* Ranged breakpoints. */
15355 ops = &ranged_breakpoint_ops;
15356 *ops = bkpt_breakpoint_ops;
15357 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15358 ops->resources_needed = resources_needed_ranged_breakpoint;
15359 ops->print_it = print_it_ranged_breakpoint;
15360 ops->print_one = print_one_ranged_breakpoint;
15361 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15362 ops->print_mention = print_mention_ranged_breakpoint;
15363 ops->print_recreate = print_recreate_ranged_breakpoint;
15364
15365 /* Internal breakpoints. */
15366 ops = &internal_breakpoint_ops;
15367 *ops = bkpt_base_breakpoint_ops;
15368 ops->re_set = internal_bkpt_re_set;
15369 ops->check_status = internal_bkpt_check_status;
15370 ops->print_it = internal_bkpt_print_it;
15371 ops->print_mention = internal_bkpt_print_mention;
15372
15373 /* Momentary breakpoints. */
15374 ops = &momentary_breakpoint_ops;
15375 *ops = bkpt_base_breakpoint_ops;
15376 ops->re_set = momentary_bkpt_re_set;
15377 ops->check_status = momentary_bkpt_check_status;
15378 ops->print_it = momentary_bkpt_print_it;
15379 ops->print_mention = momentary_bkpt_print_mention;
15380
15381 /* Probe breakpoints. */
15382 ops = &bkpt_probe_breakpoint_ops;
15383 *ops = bkpt_breakpoint_ops;
15384 ops->insert_location = bkpt_probe_insert_location;
15385 ops->remove_location = bkpt_probe_remove_location;
15386 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15387 ops->decode_location = bkpt_probe_decode_location;
15388
15389 /* Watchpoints. */
15390 ops = &watchpoint_breakpoint_ops;
15391 *ops = base_breakpoint_ops;
15392 ops->re_set = re_set_watchpoint;
15393 ops->insert_location = insert_watchpoint;
15394 ops->remove_location = remove_watchpoint;
15395 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15396 ops->check_status = check_status_watchpoint;
15397 ops->resources_needed = resources_needed_watchpoint;
15398 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15399 ops->print_it = print_it_watchpoint;
15400 ops->print_mention = print_mention_watchpoint;
15401 ops->print_recreate = print_recreate_watchpoint;
15402 ops->explains_signal = explains_signal_watchpoint;
15403
15404 /* Masked watchpoints. */
15405 ops = &masked_watchpoint_breakpoint_ops;
15406 *ops = watchpoint_breakpoint_ops;
15407 ops->insert_location = insert_masked_watchpoint;
15408 ops->remove_location = remove_masked_watchpoint;
15409 ops->resources_needed = resources_needed_masked_watchpoint;
15410 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15411 ops->print_it = print_it_masked_watchpoint;
15412 ops->print_one_detail = print_one_detail_masked_watchpoint;
15413 ops->print_mention = print_mention_masked_watchpoint;
15414 ops->print_recreate = print_recreate_masked_watchpoint;
15415
15416 /* Tracepoints. */
15417 ops = &tracepoint_breakpoint_ops;
15418 *ops = base_breakpoint_ops;
15419 ops->re_set = tracepoint_re_set;
15420 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15421 ops->print_one_detail = tracepoint_print_one_detail;
15422 ops->print_mention = tracepoint_print_mention;
15423 ops->print_recreate = tracepoint_print_recreate;
15424 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15425 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15426 ops->decode_location = tracepoint_decode_location;
15427
15428 /* Probe tracepoints. */
15429 ops = &tracepoint_probe_breakpoint_ops;
15430 *ops = tracepoint_breakpoint_ops;
15431 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15432 ops->decode_location = tracepoint_probe_decode_location;
15433
15434 /* Static tracepoints with marker (`-m'). */
15435 ops = &strace_marker_breakpoint_ops;
15436 *ops = tracepoint_breakpoint_ops;
15437 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15438 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15439 ops->decode_location = strace_marker_decode_location;
15440
15441 /* Fork catchpoints. */
15442 ops = &catch_fork_breakpoint_ops;
15443 *ops = base_breakpoint_ops;
15444 ops->insert_location = insert_catch_fork;
15445 ops->remove_location = remove_catch_fork;
15446 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15447 ops->print_it = print_it_catch_fork;
15448 ops->print_one = print_one_catch_fork;
15449 ops->print_mention = print_mention_catch_fork;
15450 ops->print_recreate = print_recreate_catch_fork;
15451
15452 /* Vfork catchpoints. */
15453 ops = &catch_vfork_breakpoint_ops;
15454 *ops = base_breakpoint_ops;
15455 ops->insert_location = insert_catch_vfork;
15456 ops->remove_location = remove_catch_vfork;
15457 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15458 ops->print_it = print_it_catch_vfork;
15459 ops->print_one = print_one_catch_vfork;
15460 ops->print_mention = print_mention_catch_vfork;
15461 ops->print_recreate = print_recreate_catch_vfork;
15462
15463 /* Exec catchpoints. */
15464 ops = &catch_exec_breakpoint_ops;
15465 *ops = base_breakpoint_ops;
15466 ops->insert_location = insert_catch_exec;
15467 ops->remove_location = remove_catch_exec;
15468 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15469 ops->print_it = print_it_catch_exec;
15470 ops->print_one = print_one_catch_exec;
15471 ops->print_mention = print_mention_catch_exec;
15472 ops->print_recreate = print_recreate_catch_exec;
15473
15474 /* Solib-related catchpoints. */
15475 ops = &catch_solib_breakpoint_ops;
15476 *ops = base_breakpoint_ops;
15477 ops->insert_location = insert_catch_solib;
15478 ops->remove_location = remove_catch_solib;
15479 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15480 ops->check_status = check_status_catch_solib;
15481 ops->print_it = print_it_catch_solib;
15482 ops->print_one = print_one_catch_solib;
15483 ops->print_mention = print_mention_catch_solib;
15484 ops->print_recreate = print_recreate_catch_solib;
15485
15486 ops = &dprintf_breakpoint_ops;
15487 *ops = bkpt_base_breakpoint_ops;
15488 ops->re_set = dprintf_re_set;
15489 ops->resources_needed = bkpt_resources_needed;
15490 ops->print_it = bkpt_print_it;
15491 ops->print_mention = bkpt_print_mention;
15492 ops->print_recreate = dprintf_print_recreate;
15493 ops->after_condition_true = dprintf_after_condition_true;
15494 ops->breakpoint_hit = dprintf_breakpoint_hit;
15495 }
15496
15497 /* Chain containing all defined "enable breakpoint" subcommands. */
15498
15499 static struct cmd_list_element *enablebreaklist = NULL;
15500
15501 void
15502 _initialize_breakpoint (void)
15503 {
15504 struct cmd_list_element *c;
15505
15506 initialize_breakpoint_ops ();
15507
15508 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15509 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15510 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15511
15512 breakpoint_objfile_key
15513 = register_objfile_data_with_cleanup (NULL, free_breakpoint_objfile_data);
15514
15515 breakpoint_chain = 0;
15516 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15517 before a breakpoint is set. */
15518 breakpoint_count = 0;
15519
15520 tracepoint_count = 0;
15521
15522 add_com ("ignore", class_breakpoint, ignore_command, _("\
15523 Set ignore-count of breakpoint number N to COUNT.\n\
15524 Usage is `ignore N COUNT'."));
15525
15526 add_com ("commands", class_breakpoint, commands_command, _("\
15527 Set commands to be executed when the given breakpoints are hit.\n\
15528 Give a space-separated breakpoint list as argument after \"commands\".\n\
15529 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15530 (e.g. `5-7').\n\
15531 With no argument, the targeted breakpoint is the last one set.\n\
15532 The commands themselves follow starting on the next line.\n\
15533 Type a line containing \"end\" to indicate the end of them.\n\
15534 Give \"silent\" as the first line to make the breakpoint silent;\n\
15535 then no output is printed when it is hit, except what the commands print."));
15536
15537 c = add_com ("condition", class_breakpoint, condition_command, _("\
15538 Specify breakpoint number N to break only if COND is true.\n\
15539 Usage is `condition N COND', where N is an integer and COND is an\n\
15540 expression to be evaluated whenever breakpoint N is reached."));
15541 set_cmd_completer (c, condition_completer);
15542
15543 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15544 Set a temporary breakpoint.\n\
15545 Like \"break\" except the breakpoint is only temporary,\n\
15546 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15547 by using \"enable delete\" on the breakpoint number.\n\
15548 \n"
15549 BREAK_ARGS_HELP ("tbreak")));
15550 set_cmd_completer (c, location_completer);
15551
15552 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15553 Set a hardware assisted breakpoint.\n\
15554 Like \"break\" except the breakpoint requires hardware support,\n\
15555 some target hardware may not have this support.\n\
15556 \n"
15557 BREAK_ARGS_HELP ("hbreak")));
15558 set_cmd_completer (c, location_completer);
15559
15560 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15561 Set a temporary hardware assisted breakpoint.\n\
15562 Like \"hbreak\" except the breakpoint is only temporary,\n\
15563 so it will be deleted when hit.\n\
15564 \n"
15565 BREAK_ARGS_HELP ("thbreak")));
15566 set_cmd_completer (c, location_completer);
15567
15568 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15569 Enable some breakpoints.\n\
15570 Give breakpoint numbers (separated by spaces) as arguments.\n\
15571 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15572 This is used to cancel the effect of the \"disable\" command.\n\
15573 With a subcommand you can enable temporarily."),
15574 &enablelist, "enable ", 1, &cmdlist);
15575
15576 add_com_alias ("en", "enable", class_breakpoint, 1);
15577
15578 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15579 Enable some breakpoints.\n\
15580 Give breakpoint numbers (separated by spaces) as arguments.\n\
15581 This is used to cancel the effect of the \"disable\" command.\n\
15582 May be abbreviated to simply \"enable\".\n"),
15583 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15584
15585 add_cmd ("once", no_class, enable_once_command, _("\
15586 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15587 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15588 &enablebreaklist);
15589
15590 add_cmd ("delete", no_class, enable_delete_command, _("\
15591 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15592 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15593 &enablebreaklist);
15594
15595 add_cmd ("count", no_class, enable_count_command, _("\
15596 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15597 If a breakpoint is hit while enabled in this fashion,\n\
15598 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15599 &enablebreaklist);
15600
15601 add_cmd ("delete", no_class, enable_delete_command, _("\
15602 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15603 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15604 &enablelist);
15605
15606 add_cmd ("once", no_class, enable_once_command, _("\
15607 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15608 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15609 &enablelist);
15610
15611 add_cmd ("count", no_class, enable_count_command, _("\
15612 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15613 If a breakpoint is hit while enabled in this fashion,\n\
15614 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15615 &enablelist);
15616
15617 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15618 Disable some breakpoints.\n\
15619 Arguments are breakpoint numbers with spaces in between.\n\
15620 To disable all breakpoints, give no argument.\n\
15621 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15622 &disablelist, "disable ", 1, &cmdlist);
15623 add_com_alias ("dis", "disable", class_breakpoint, 1);
15624 add_com_alias ("disa", "disable", class_breakpoint, 1);
15625
15626 add_cmd ("breakpoints", class_alias, disable_command, _("\
15627 Disable some breakpoints.\n\
15628 Arguments are breakpoint numbers with spaces in between.\n\
15629 To disable all breakpoints, give no argument.\n\
15630 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15631 This command may be abbreviated \"disable\"."),
15632 &disablelist);
15633
15634 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15635 Delete some breakpoints or auto-display expressions.\n\
15636 Arguments are breakpoint numbers with spaces in between.\n\
15637 To delete all breakpoints, give no argument.\n\
15638 \n\
15639 Also a prefix command for deletion of other GDB objects.\n\
15640 The \"unset\" command is also an alias for \"delete\"."),
15641 &deletelist, "delete ", 1, &cmdlist);
15642 add_com_alias ("d", "delete", class_breakpoint, 1);
15643 add_com_alias ("del", "delete", class_breakpoint, 1);
15644
15645 add_cmd ("breakpoints", class_alias, delete_command, _("\
15646 Delete some breakpoints or auto-display expressions.\n\
15647 Arguments are breakpoint numbers with spaces in between.\n\
15648 To delete all breakpoints, give no argument.\n\
15649 This command may be abbreviated \"delete\"."),
15650 &deletelist);
15651
15652 add_com ("clear", class_breakpoint, clear_command, _("\
15653 Clear breakpoint at specified location.\n\
15654 Argument may be a linespec, explicit, or address location as described below.\n\
15655 \n\
15656 With no argument, clears all breakpoints in the line that the selected frame\n\
15657 is executing in.\n"
15658 "\n" LOCATION_HELP_STRING "\n\
15659 See also the \"delete\" command which clears breakpoints by number."));
15660 add_com_alias ("cl", "clear", class_breakpoint, 1);
15661
15662 c = add_com ("break", class_breakpoint, break_command, _("\
15663 Set breakpoint at specified location.\n"
15664 BREAK_ARGS_HELP ("break")));
15665 set_cmd_completer (c, location_completer);
15666
15667 add_com_alias ("b", "break", class_run, 1);
15668 add_com_alias ("br", "break", class_run, 1);
15669 add_com_alias ("bre", "break", class_run, 1);
15670 add_com_alias ("brea", "break", class_run, 1);
15671
15672 if (dbx_commands)
15673 {
15674 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15675 Break in function/address or break at a line in the current file."),
15676 &stoplist, "stop ", 1, &cmdlist);
15677 add_cmd ("in", class_breakpoint, stopin_command,
15678 _("Break in function or address."), &stoplist);
15679 add_cmd ("at", class_breakpoint, stopat_command,
15680 _("Break at a line in the current file."), &stoplist);
15681 add_com ("status", class_info, info_breakpoints_command, _("\
15682 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15683 The \"Type\" column indicates one of:\n\
15684 \tbreakpoint - normal breakpoint\n\
15685 \twatchpoint - watchpoint\n\
15686 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15687 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15688 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15689 address and file/line number respectively.\n\
15690 \n\
15691 Convenience variable \"$_\" and default examine address for \"x\"\n\
15692 are set to the address of the last breakpoint listed unless the command\n\
15693 is prefixed with \"server \".\n\n\
15694 Convenience variable \"$bpnum\" contains the number of the last\n\
15695 breakpoint set."));
15696 }
15697
15698 add_info ("breakpoints", info_breakpoints_command, _("\
15699 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15700 The \"Type\" column indicates one of:\n\
15701 \tbreakpoint - normal breakpoint\n\
15702 \twatchpoint - watchpoint\n\
15703 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15704 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15705 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15706 address and file/line number respectively.\n\
15707 \n\
15708 Convenience variable \"$_\" and default examine address for \"x\"\n\
15709 are set to the address of the last breakpoint listed unless the command\n\
15710 is prefixed with \"server \".\n\n\
15711 Convenience variable \"$bpnum\" contains the number of the last\n\
15712 breakpoint set."));
15713
15714 add_info_alias ("b", "breakpoints", 1);
15715
15716 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15717 Status of all breakpoints, or breakpoint number NUMBER.\n\
15718 The \"Type\" column indicates one of:\n\
15719 \tbreakpoint - normal breakpoint\n\
15720 \twatchpoint - watchpoint\n\
15721 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15722 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15723 \tuntil - internal breakpoint used by the \"until\" command\n\
15724 \tfinish - internal breakpoint used by the \"finish\" command\n\
15725 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15726 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15727 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15728 address and file/line number respectively.\n\
15729 \n\
15730 Convenience variable \"$_\" and default examine address for \"x\"\n\
15731 are set to the address of the last breakpoint listed unless the command\n\
15732 is prefixed with \"server \".\n\n\
15733 Convenience variable \"$bpnum\" contains the number of the last\n\
15734 breakpoint set."),
15735 &maintenanceinfolist);
15736
15737 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15738 Set catchpoints to catch events."),
15739 &catch_cmdlist, "catch ",
15740 0/*allow-unknown*/, &cmdlist);
15741
15742 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15743 Set temporary catchpoints to catch events."),
15744 &tcatch_cmdlist, "tcatch ",
15745 0/*allow-unknown*/, &cmdlist);
15746
15747 add_catch_command ("fork", _("Catch calls to fork."),
15748 catch_fork_command_1,
15749 NULL,
15750 (void *) (uintptr_t) catch_fork_permanent,
15751 (void *) (uintptr_t) catch_fork_temporary);
15752 add_catch_command ("vfork", _("Catch calls to vfork."),
15753 catch_fork_command_1,
15754 NULL,
15755 (void *) (uintptr_t) catch_vfork_permanent,
15756 (void *) (uintptr_t) catch_vfork_temporary);
15757 add_catch_command ("exec", _("Catch calls to exec."),
15758 catch_exec_command_1,
15759 NULL,
15760 CATCH_PERMANENT,
15761 CATCH_TEMPORARY);
15762 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15763 Usage: catch load [REGEX]\n\
15764 If REGEX is given, only stop for libraries matching the regular expression."),
15765 catch_load_command_1,
15766 NULL,
15767 CATCH_PERMANENT,
15768 CATCH_TEMPORARY);
15769 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15770 Usage: catch unload [REGEX]\n\
15771 If REGEX is given, only stop for libraries matching the regular expression."),
15772 catch_unload_command_1,
15773 NULL,
15774 CATCH_PERMANENT,
15775 CATCH_TEMPORARY);
15776
15777 c = add_com ("watch", class_breakpoint, watch_command, _("\
15778 Set a watchpoint for an expression.\n\
15779 Usage: watch [-l|-location] EXPRESSION\n\
15780 A watchpoint stops execution of your program whenever the value of\n\
15781 an expression changes.\n\
15782 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15783 the memory to which it refers."));
15784 set_cmd_completer (c, expression_completer);
15785
15786 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15787 Set a read watchpoint for an expression.\n\
15788 Usage: rwatch [-l|-location] EXPRESSION\n\
15789 A watchpoint stops execution of your program whenever the value of\n\
15790 an expression is read.\n\
15791 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15792 the memory to which it refers."));
15793 set_cmd_completer (c, expression_completer);
15794
15795 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15796 Set a watchpoint for an expression.\n\
15797 Usage: awatch [-l|-location] EXPRESSION\n\
15798 A watchpoint stops execution of your program whenever the value of\n\
15799 an expression is either read or written.\n\
15800 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15801 the memory to which it refers."));
15802 set_cmd_completer (c, expression_completer);
15803
15804 add_info ("watchpoints", info_watchpoints_command, _("\
15805 Status of specified watchpoints (all watchpoints if no argument)."));
15806
15807 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15808 respond to changes - contrary to the description. */
15809 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15810 &can_use_hw_watchpoints, _("\
15811 Set debugger's willingness to use watchpoint hardware."), _("\
15812 Show debugger's willingness to use watchpoint hardware."), _("\
15813 If zero, gdb will not use hardware for new watchpoints, even if\n\
15814 such is available. (However, any hardware watchpoints that were\n\
15815 created before setting this to nonzero, will continue to use watchpoint\n\
15816 hardware.)"),
15817 NULL,
15818 show_can_use_hw_watchpoints,
15819 &setlist, &showlist);
15820
15821 can_use_hw_watchpoints = 1;
15822
15823 /* Tracepoint manipulation commands. */
15824
15825 c = add_com ("trace", class_breakpoint, trace_command, _("\
15826 Set a tracepoint at specified location.\n\
15827 \n"
15828 BREAK_ARGS_HELP ("trace") "\n\
15829 Do \"help tracepoints\" for info on other tracepoint commands."));
15830 set_cmd_completer (c, location_completer);
15831
15832 add_com_alias ("tp", "trace", class_alias, 0);
15833 add_com_alias ("tr", "trace", class_alias, 1);
15834 add_com_alias ("tra", "trace", class_alias, 1);
15835 add_com_alias ("trac", "trace", class_alias, 1);
15836
15837 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15838 Set a fast tracepoint at specified location.\n\
15839 \n"
15840 BREAK_ARGS_HELP ("ftrace") "\n\
15841 Do \"help tracepoints\" for info on other tracepoint commands."));
15842 set_cmd_completer (c, location_completer);
15843
15844 c = add_com ("strace", class_breakpoint, strace_command, _("\
15845 Set a static tracepoint at location or marker.\n\
15846 \n\
15847 strace [LOCATION] [if CONDITION]\n\
15848 LOCATION may be a linespec, explicit, or address location (described below) \n\
15849 or -m MARKER_ID.\n\n\
15850 If a marker id is specified, probe the marker with that name. With\n\
15851 no LOCATION, uses current execution address of the selected stack frame.\n\
15852 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15853 This collects arbitrary user data passed in the probe point call to the\n\
15854 tracing library. You can inspect it when analyzing the trace buffer,\n\
15855 by printing the $_sdata variable like any other convenience variable.\n\
15856 \n\
15857 CONDITION is a boolean expression.\n\
15858 \n" LOCATION_HELP_STRING "\n\
15859 Multiple tracepoints at one place are permitted, and useful if their\n\
15860 conditions are different.\n\
15861 \n\
15862 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15863 Do \"help tracepoints\" for info on other tracepoint commands."));
15864 set_cmd_completer (c, location_completer);
15865
15866 add_info ("tracepoints", info_tracepoints_command, _("\
15867 Status of specified tracepoints (all tracepoints if no argument).\n\
15868 Convenience variable \"$tpnum\" contains the number of the\n\
15869 last tracepoint set."));
15870
15871 add_info_alias ("tp", "tracepoints", 1);
15872
15873 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15874 Delete specified tracepoints.\n\
15875 Arguments are tracepoint numbers, separated by spaces.\n\
15876 No argument means delete all tracepoints."),
15877 &deletelist);
15878 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15879
15880 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15881 Disable specified tracepoints.\n\
15882 Arguments are tracepoint numbers, separated by spaces.\n\
15883 No argument means disable all tracepoints."),
15884 &disablelist);
15885 deprecate_cmd (c, "disable");
15886
15887 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15888 Enable specified tracepoints.\n\
15889 Arguments are tracepoint numbers, separated by spaces.\n\
15890 No argument means enable all tracepoints."),
15891 &enablelist);
15892 deprecate_cmd (c, "enable");
15893
15894 add_com ("passcount", class_trace, trace_pass_command, _("\
15895 Set the passcount for a tracepoint.\n\
15896 The trace will end when the tracepoint has been passed 'count' times.\n\
15897 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15898 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15899
15900 add_prefix_cmd ("save", class_breakpoint, save_command,
15901 _("Save breakpoint definitions as a script."),
15902 &save_cmdlist, "save ",
15903 0/*allow-unknown*/, &cmdlist);
15904
15905 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15906 Save current breakpoint definitions as a script.\n\
15907 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15908 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15909 session to restore them."),
15910 &save_cmdlist);
15911 set_cmd_completer (c, filename_completer);
15912
15913 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15914 Save current tracepoint definitions as a script.\n\
15915 Use the 'source' command in another debug session to restore them."),
15916 &save_cmdlist);
15917 set_cmd_completer (c, filename_completer);
15918
15919 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15920 deprecate_cmd (c, "save tracepoints");
15921
15922 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15923 Breakpoint specific settings\n\
15924 Configure various breakpoint-specific variables such as\n\
15925 pending breakpoint behavior"),
15926 &breakpoint_set_cmdlist, "set breakpoint ",
15927 0/*allow-unknown*/, &setlist);
15928 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15929 Breakpoint specific settings\n\
15930 Configure various breakpoint-specific variables such as\n\
15931 pending breakpoint behavior"),
15932 &breakpoint_show_cmdlist, "show breakpoint ",
15933 0/*allow-unknown*/, &showlist);
15934
15935 add_setshow_auto_boolean_cmd ("pending", no_class,
15936 &pending_break_support, _("\
15937 Set debugger's behavior regarding pending breakpoints."), _("\
15938 Show debugger's behavior regarding pending breakpoints."), _("\
15939 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15940 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15941 an error. If auto, an unrecognized breakpoint location results in a\n\
15942 user-query to see if a pending breakpoint should be created."),
15943 NULL,
15944 show_pending_break_support,
15945 &breakpoint_set_cmdlist,
15946 &breakpoint_show_cmdlist);
15947
15948 pending_break_support = AUTO_BOOLEAN_AUTO;
15949
15950 add_setshow_boolean_cmd ("auto-hw", no_class,
15951 &automatic_hardware_breakpoints, _("\
15952 Set automatic usage of hardware breakpoints."), _("\
15953 Show automatic usage of hardware breakpoints."), _("\
15954 If set, the debugger will automatically use hardware breakpoints for\n\
15955 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15956 a warning will be emitted for such breakpoints."),
15957 NULL,
15958 show_automatic_hardware_breakpoints,
15959 &breakpoint_set_cmdlist,
15960 &breakpoint_show_cmdlist);
15961
15962 add_setshow_boolean_cmd ("always-inserted", class_support,
15963 &always_inserted_mode, _("\
15964 Set mode for inserting breakpoints."), _("\
15965 Show mode for inserting breakpoints."), _("\
15966 When this mode is on, breakpoints are inserted immediately as soon as\n\
15967 they're created, kept inserted even when execution stops, and removed\n\
15968 only when the user deletes them. When this mode is off (the default),\n\
15969 breakpoints are inserted only when execution continues, and removed\n\
15970 when execution stops."),
15971 NULL,
15972 &show_always_inserted_mode,
15973 &breakpoint_set_cmdlist,
15974 &breakpoint_show_cmdlist);
15975
15976 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15977 condition_evaluation_enums,
15978 &condition_evaluation_mode_1, _("\
15979 Set mode of breakpoint condition evaluation."), _("\
15980 Show mode of breakpoint condition evaluation."), _("\
15981 When this is set to \"host\", breakpoint conditions will be\n\
15982 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15983 breakpoint conditions will be downloaded to the target (if the target\n\
15984 supports such feature) and conditions will be evaluated on the target's side.\n\
15985 If this is set to \"auto\" (default), this will be automatically set to\n\
15986 \"target\" if it supports condition evaluation, otherwise it will\n\
15987 be set to \"gdb\""),
15988 &set_condition_evaluation_mode,
15989 &show_condition_evaluation_mode,
15990 &breakpoint_set_cmdlist,
15991 &breakpoint_show_cmdlist);
15992
15993 add_com ("break-range", class_breakpoint, break_range_command, _("\
15994 Set a breakpoint for an address range.\n\
15995 break-range START-LOCATION, END-LOCATION\n\
15996 where START-LOCATION and END-LOCATION can be one of the following:\n\
15997 LINENUM, for that line in the current file,\n\
15998 FILE:LINENUM, for that line in that file,\n\
15999 +OFFSET, for that number of lines after the current line\n\
16000 or the start of the range\n\
16001 FUNCTION, for the first line in that function,\n\
16002 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16003 *ADDRESS, for the instruction at that address.\n\
16004 \n\
16005 The breakpoint will stop execution of the inferior whenever it executes\n\
16006 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16007 range (including START-LOCATION and END-LOCATION)."));
16008
16009 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16010 Set a dynamic printf at specified location.\n\
16011 dprintf location,format string,arg1,arg2,...\n\
16012 location may be a linespec, explicit, or address location.\n"
16013 "\n" LOCATION_HELP_STRING));
16014 set_cmd_completer (c, location_completer);
16015
16016 add_setshow_enum_cmd ("dprintf-style", class_support,
16017 dprintf_style_enums, &dprintf_style, _("\
16018 Set the style of usage for dynamic printf."), _("\
16019 Show the style of usage for dynamic printf."), _("\
16020 This setting chooses how GDB will do a dynamic printf.\n\
16021 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16022 console, as with the \"printf\" command.\n\
16023 If the value is \"call\", the print is done by calling a function in your\n\
16024 program; by default printf(), but you can choose a different function or\n\
16025 output stream by setting dprintf-function and dprintf-channel."),
16026 update_dprintf_commands, NULL,
16027 &setlist, &showlist);
16028
16029 dprintf_function = xstrdup ("printf");
16030 add_setshow_string_cmd ("dprintf-function", class_support,
16031 &dprintf_function, _("\
16032 Set the function to use for dynamic printf"), _("\
16033 Show the function to use for dynamic printf"), NULL,
16034 update_dprintf_commands, NULL,
16035 &setlist, &showlist);
16036
16037 dprintf_channel = xstrdup ("");
16038 add_setshow_string_cmd ("dprintf-channel", class_support,
16039 &dprintf_channel, _("\
16040 Set the channel to use for dynamic printf"), _("\
16041 Show the channel to use for dynamic printf"), NULL,
16042 update_dprintf_commands, NULL,
16043 &setlist, &showlist);
16044
16045 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16046 &disconnected_dprintf, _("\
16047 Set whether dprintf continues after GDB disconnects."), _("\
16048 Show whether dprintf continues after GDB disconnects."), _("\
16049 Use this to let dprintf commands continue to hit and produce output\n\
16050 even if GDB disconnects or detaches from the target."),
16051 NULL,
16052 NULL,
16053 &setlist, &showlist);
16054
16055 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16056 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16057 (target agent only) This is useful for formatted output in user-defined commands."));
16058
16059 automatic_hardware_breakpoints = 1;
16060
16061 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16062 observer_attach_thread_exit (remove_threaded_breakpoints);
16063 }