Remove unnecessary function prototypes.
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70 #include "thread-fsm.h"
71 #include "tid-parse.h"
72
73 /* readline include files */
74 #include "readline/readline.h"
75 #include "readline/history.h"
76
77 /* readline defines this. */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "extension.h"
82 #include <algorithm>
83 #include "progspace-and-thread.h"
84 #include "common/array-view.h"
85
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 enable_delete_command (char *, int);
97
98 static void enable_once_command (char *, int);
99
100 static void enable_count_command (char *, int);
101
102 static void disable_command (char *, int);
103
104 static void enable_command (char *, int);
105
106 static void map_breakpoint_numbers (const char *,
107 void (*) (struct breakpoint *,
108 void *),
109 void *);
110
111 static void ignore_command (char *, int);
112
113 static int breakpoint_re_set_one (void *);
114
115 static void breakpoint_re_set_default (struct breakpoint *);
116
117 static void
118 create_sals_from_location_default (const struct event_location *location,
119 struct linespec_result *canonical,
120 enum bptype type_wanted);
121
122 static void create_breakpoints_sal_default (struct gdbarch *,
123 struct linespec_result *,
124 gdb::unique_xmalloc_ptr<char>,
125 gdb::unique_xmalloc_ptr<char>,
126 enum bptype,
127 enum bpdisp, int, int,
128 int,
129 const struct breakpoint_ops *,
130 int, int, int, unsigned);
131
132 static std::vector<symtab_and_line> decode_location_default
133 (struct breakpoint *b, const struct event_location *location,
134 struct program_space *search_pspace);
135
136 static void clear_command (char *, int);
137
138 static void catch_command (char *, int);
139
140 static int can_use_hardware_watchpoint (struct value *);
141
142 static void break_command_1 (char *, int, int);
143
144 static void mention (struct breakpoint *);
145
146 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
147 enum bptype,
148 const struct breakpoint_ops *);
149 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
150 const struct symtab_and_line *);
151
152 /* This function is used in gdbtk sources and thus can not be made
153 static. */
154 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
155 struct symtab_and_line,
156 enum bptype,
157 const struct breakpoint_ops *);
158
159 static struct breakpoint *
160 momentary_breakpoint_from_master (struct breakpoint *orig,
161 enum bptype type,
162 const struct breakpoint_ops *ops,
163 int loc_enabled);
164
165 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
166
167 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
168 CORE_ADDR bpaddr,
169 enum bptype bptype);
170
171 static void describe_other_breakpoints (struct gdbarch *,
172 struct program_space *, CORE_ADDR,
173 struct obj_section *, int);
174
175 static int watchpoint_locations_match (struct bp_location *loc1,
176 struct bp_location *loc2);
177
178 static int breakpoint_location_address_match (struct bp_location *bl,
179 struct address_space *aspace,
180 CORE_ADDR addr);
181
182 static int breakpoint_location_address_range_overlap (struct bp_location *,
183 struct address_space *,
184 CORE_ADDR, int);
185
186 static void info_breakpoints_command (char *, int);
187
188 static void info_watchpoints_command (char *, int);
189
190 static int breakpoint_1 (char *, int,
191 int (*) (const struct breakpoint *));
192
193 static int breakpoint_cond_eval (void *);
194
195 static void cleanup_executing_breakpoints (void *);
196
197 static void commands_command (char *, int);
198
199 static void condition_command (char *, int);
200
201 static int remove_breakpoint (struct bp_location *);
202 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
203
204 static enum print_stop_action print_bp_stop_message (bpstat bs);
205
206 static int watchpoint_check (void *);
207
208 static void maintenance_info_breakpoints (char *, int);
209
210 static int hw_breakpoint_used_count (void);
211
212 static int hw_watchpoint_use_count (struct breakpoint *);
213
214 static int hw_watchpoint_used_count_others (struct breakpoint *except,
215 enum bptype type,
216 int *other_type_used);
217
218 static void hbreak_command (char *, int);
219
220 static void thbreak_command (char *, int);
221
222 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
223 int count);
224
225 static void stop_command (char *arg, int from_tty);
226
227 static void stopin_command (char *arg, int from_tty);
228
229 static void stopat_command (char *arg, int from_tty);
230
231 static void tcatch_command (char *arg, int from_tty);
232
233 static void free_bp_location (struct bp_location *loc);
234 static void incref_bp_location (struct bp_location *loc);
235 static void decref_bp_location (struct bp_location **loc);
236
237 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
238
239 /* update_global_location_list's modes of operation wrt to whether to
240 insert locations now. */
241 enum ugll_insert_mode
242 {
243 /* Don't insert any breakpoint locations into the inferior, only
244 remove already-inserted locations that no longer should be
245 inserted. Functions that delete a breakpoint or breakpoints
246 should specify this mode, so that deleting a breakpoint doesn't
247 have the side effect of inserting the locations of other
248 breakpoints that are marked not-inserted, but should_be_inserted
249 returns true on them.
250
251 This behavior is useful is situations close to tear-down -- e.g.,
252 after an exec, while the target still has execution, but
253 breakpoint shadows of the previous executable image should *NOT*
254 be restored to the new image; or before detaching, where the
255 target still has execution and wants to delete breakpoints from
256 GDB's lists, and all breakpoints had already been removed from
257 the inferior. */
258 UGLL_DONT_INSERT,
259
260 /* May insert breakpoints iff breakpoints_should_be_inserted_now
261 claims breakpoints should be inserted now. */
262 UGLL_MAY_INSERT,
263
264 /* Insert locations now, irrespective of
265 breakpoints_should_be_inserted_now. E.g., say all threads are
266 stopped right now, and the user did "continue". We need to
267 insert breakpoints _before_ resuming the target, but
268 UGLL_MAY_INSERT wouldn't insert them, because
269 breakpoints_should_be_inserted_now returns false at that point,
270 as no thread is running yet. */
271 UGLL_INSERT
272 };
273
274 static void update_global_location_list (enum ugll_insert_mode);
275
276 static void update_global_location_list_nothrow (enum ugll_insert_mode);
277
278 static int is_hardware_watchpoint (const struct breakpoint *bpt);
279
280 static void insert_breakpoint_locations (void);
281
282 static void info_tracepoints_command (char *, int);
283
284 static void delete_trace_command (char *, int);
285
286 static void enable_trace_command (char *, int);
287
288 static void disable_trace_command (char *, int);
289
290 static void trace_pass_command (char *, int);
291
292 static void set_tracepoint_count (int num);
293
294 static int is_masked_watchpoint (const struct breakpoint *b);
295
296 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
297
298 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
299 otherwise. */
300
301 static int strace_marker_p (struct breakpoint *b);
302
303 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
304 that are implemented on top of software or hardware breakpoints
305 (user breakpoints, internal and momentary breakpoints, etc.). */
306 static struct breakpoint_ops bkpt_base_breakpoint_ops;
307
308 /* Internal breakpoints class type. */
309 static struct breakpoint_ops internal_breakpoint_ops;
310
311 /* Momentary breakpoints class type. */
312 static struct breakpoint_ops momentary_breakpoint_ops;
313
314 /* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316 struct breakpoint_ops bkpt_breakpoint_ops;
317
318 /* Breakpoints set on probes. */
319 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
321 /* Dynamic printf class type. */
322 struct breakpoint_ops dprintf_breakpoint_ops;
323
324 /* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331 static const char dprintf_style_gdb[] = "gdb";
332 static const char dprintf_style_call[] = "call";
333 static const char dprintf_style_agent[] = "agent";
334 static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339 };
340 static const char *dprintf_style = dprintf_style_gdb;
341
342 /* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347 static char *dprintf_function;
348
349 /* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357 static char *dprintf_channel;
358
359 /* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361 static int disconnected_dprintf = 1;
362
363 /* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365 struct counted_command_line
366 {
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372 };
373
374 struct command_line *
375 breakpoint_commands (struct breakpoint *b)
376 {
377 return b->commands ? b->commands->commands : NULL;
378 }
379
380 /* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383 static int breakpoint_proceeded;
384
385 const char *
386 bpdisp_text (enum bpdisp disp)
387 {
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
392
393 return bpdisps[(int) disp];
394 }
395
396 /* Prototypes for exported functions. */
397 /* If FALSE, gdb will not use hardware support for watchpoints, even
398 if such is available. */
399 static int can_use_hw_watchpoints;
400
401 static void
402 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405 {
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
409 value);
410 }
411
412 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
414 for unrecognized breakpoint locations.
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416 static enum auto_boolean pending_break_support;
417 static void
418 show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421 {
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
425 value);
426 }
427
428 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
429 set with "break" but falling in read-only memory.
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432 static int automatic_hardware_breakpoints;
433 static void
434 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437 {
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
440 value);
441 }
442
443 /* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450 static int always_inserted_mode = 0;
451
452 static void
453 show_always_inserted_mode (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c, const char *value)
455 {
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
458 }
459
460 /* See breakpoint.h. */
461
462 int
463 breakpoints_should_be_inserted_now (void)
464 {
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
474 struct thread_info *tp;
475
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
483 if (threads_are_executing ())
484 return 1;
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
492 }
493 return 0;
494 }
495
496 static const char condition_evaluation_both[] = "host or target";
497
498 /* Modes for breakpoint condition evaluation. */
499 static const char condition_evaluation_auto[] = "auto";
500 static const char condition_evaluation_host[] = "host";
501 static const char condition_evaluation_target[] = "target";
502 static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507 };
508
509 /* Global that holds the current mode for breakpoint condition evaluation. */
510 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512 /* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514 static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516 /* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521 static const char *
522 translate_condition_evaluation_mode (const char *mode)
523 {
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533 }
534
535 /* Discovers what condition_evaluation_auto translates to. */
536
537 static const char *
538 breakpoint_condition_evaluation_mode (void)
539 {
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541 }
542
543 /* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546 static int
547 gdb_evaluates_breakpoint_condition_p (void)
548 {
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552 }
553
554 /* Are we executing breakpoint commands? */
555 static int executing_breakpoint_commands;
556
557 /* Are overlay event breakpoints enabled? */
558 static int overlay_events_enabled;
559
560 /* See description in breakpoint.h. */
561 int target_exact_watchpoints = 0;
562
563 /* Walk the following statement or block through all breakpoints.
564 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
565 current breakpoint. */
566
567 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
568
569 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
570 for (B = breakpoint_chain; \
571 B ? (TMP=B->next, 1): 0; \
572 B = TMP)
573
574 /* Similar iterator for the low-level breakpoints. SAFE variant is
575 not provided so update_global_location_list must not be called
576 while executing the block of ALL_BP_LOCATIONS. */
577
578 #define ALL_BP_LOCATIONS(B,BP_TMP) \
579 for (BP_TMP = bp_locations; \
580 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
581 BP_TMP++)
582
583 /* Iterates through locations with address ADDRESS for the currently selected
584 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
585 to where the loop should start from.
586 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
587 appropriate location to start with. */
588
589 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
590 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
591 BP_LOCP_TMP = BP_LOCP_START; \
592 BP_LOCP_START \
593 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
594 && (*BP_LOCP_TMP)->address == ADDRESS); \
595 BP_LOCP_TMP++)
596
597 /* Iterator for tracepoints only. */
598
599 #define ALL_TRACEPOINTS(B) \
600 for (B = breakpoint_chain; B; B = B->next) \
601 if (is_tracepoint (B))
602
603 /* Chains of all breakpoints defined. */
604
605 struct breakpoint *breakpoint_chain;
606
607 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
608
609 static struct bp_location **bp_locations;
610
611 /* Number of elements of BP_LOCATIONS. */
612
613 static unsigned bp_locations_count;
614
615 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
616 ADDRESS for the current elements of BP_LOCATIONS which get a valid
617 result from bp_location_has_shadow. You can use it for roughly
618 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
619 an address you need to read. */
620
621 static CORE_ADDR bp_locations_placed_address_before_address_max;
622
623 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
624 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
625 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
626 You can use it for roughly limiting the subrange of BP_LOCATIONS to
627 scan for shadow bytes for an address you need to read. */
628
629 static CORE_ADDR bp_locations_shadow_len_after_address_max;
630
631 /* The locations that no longer correspond to any breakpoint, unlinked
632 from the bp_locations array, but for which a hit may still be
633 reported by a target. */
634 VEC(bp_location_p) *moribund_locations = NULL;
635
636 /* Number of last breakpoint made. */
637
638 static int breakpoint_count;
639
640 /* The value of `breakpoint_count' before the last command that
641 created breakpoints. If the last (break-like) command created more
642 than one breakpoint, then the difference between BREAKPOINT_COUNT
643 and PREV_BREAKPOINT_COUNT is more than one. */
644 static int prev_breakpoint_count;
645
646 /* Number of last tracepoint made. */
647
648 static int tracepoint_count;
649
650 static struct cmd_list_element *breakpoint_set_cmdlist;
651 static struct cmd_list_element *breakpoint_show_cmdlist;
652 struct cmd_list_element *save_cmdlist;
653
654 /* See declaration at breakpoint.h. */
655
656 struct breakpoint *
657 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
658 void *user_data)
659 {
660 struct breakpoint *b = NULL;
661
662 ALL_BREAKPOINTS (b)
663 {
664 if (func (b, user_data) != 0)
665 break;
666 }
667
668 return b;
669 }
670
671 /* Return whether a breakpoint is an active enabled breakpoint. */
672 static int
673 breakpoint_enabled (struct breakpoint *b)
674 {
675 return (b->enable_state == bp_enabled);
676 }
677
678 /* Set breakpoint count to NUM. */
679
680 static void
681 set_breakpoint_count (int num)
682 {
683 prev_breakpoint_count = breakpoint_count;
684 breakpoint_count = num;
685 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
686 }
687
688 /* Used by `start_rbreak_breakpoints' below, to record the current
689 breakpoint count before "rbreak" creates any breakpoint. */
690 static int rbreak_start_breakpoint_count;
691
692 /* Called at the start an "rbreak" command to record the first
693 breakpoint made. */
694
695 void
696 start_rbreak_breakpoints (void)
697 {
698 rbreak_start_breakpoint_count = breakpoint_count;
699 }
700
701 /* Called at the end of an "rbreak" command to record the last
702 breakpoint made. */
703
704 void
705 end_rbreak_breakpoints (void)
706 {
707 prev_breakpoint_count = rbreak_start_breakpoint_count;
708 }
709
710 /* Used in run_command to zero the hit count when a new run starts. */
711
712 void
713 clear_breakpoint_hit_counts (void)
714 {
715 struct breakpoint *b;
716
717 ALL_BREAKPOINTS (b)
718 b->hit_count = 0;
719 }
720
721 /* Allocate a new counted_command_line with reference count of 1.
722 The new structure owns COMMANDS. */
723
724 static struct counted_command_line *
725 alloc_counted_command_line (struct command_line *commands)
726 {
727 struct counted_command_line *result = XNEW (struct counted_command_line);
728
729 result->refc = 1;
730 result->commands = commands;
731
732 return result;
733 }
734
735 /* Increment reference count. This does nothing if CMD is NULL. */
736
737 static void
738 incref_counted_command_line (struct counted_command_line *cmd)
739 {
740 if (cmd)
741 ++cmd->refc;
742 }
743
744 /* Decrement reference count. If the reference count reaches 0,
745 destroy the counted_command_line. Sets *CMDP to NULL. This does
746 nothing if *CMDP is NULL. */
747
748 static void
749 decref_counted_command_line (struct counted_command_line **cmdp)
750 {
751 if (*cmdp)
752 {
753 if (--(*cmdp)->refc == 0)
754 {
755 free_command_lines (&(*cmdp)->commands);
756 xfree (*cmdp);
757 }
758 *cmdp = NULL;
759 }
760 }
761
762 /* A cleanup function that calls decref_counted_command_line. */
763
764 static void
765 do_cleanup_counted_command_line (void *arg)
766 {
767 decref_counted_command_line ((struct counted_command_line **) arg);
768 }
769
770 /* Create a cleanup that calls decref_counted_command_line on the
771 argument. */
772
773 static struct cleanup *
774 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
775 {
776 return make_cleanup (do_cleanup_counted_command_line, cmdp);
777 }
778
779 \f
780 /* Return the breakpoint with the specified number, or NULL
781 if the number does not refer to an existing breakpoint. */
782
783 struct breakpoint *
784 get_breakpoint (int num)
785 {
786 struct breakpoint *b;
787
788 ALL_BREAKPOINTS (b)
789 if (b->number == num)
790 return b;
791
792 return NULL;
793 }
794
795 \f
796
797 /* Mark locations as "conditions have changed" in case the target supports
798 evaluating conditions on its side. */
799
800 static void
801 mark_breakpoint_modified (struct breakpoint *b)
802 {
803 struct bp_location *loc;
804
805 /* This is only meaningful if the target is
806 evaluating conditions and if the user has
807 opted for condition evaluation on the target's
808 side. */
809 if (gdb_evaluates_breakpoint_condition_p ()
810 || !target_supports_evaluation_of_breakpoint_conditions ())
811 return;
812
813 if (!is_breakpoint (b))
814 return;
815
816 for (loc = b->loc; loc; loc = loc->next)
817 loc->condition_changed = condition_modified;
818 }
819
820 /* Mark location as "conditions have changed" in case the target supports
821 evaluating conditions on its side. */
822
823 static void
824 mark_breakpoint_location_modified (struct bp_location *loc)
825 {
826 /* This is only meaningful if the target is
827 evaluating conditions and if the user has
828 opted for condition evaluation on the target's
829 side. */
830 if (gdb_evaluates_breakpoint_condition_p ()
831 || !target_supports_evaluation_of_breakpoint_conditions ())
832
833 return;
834
835 if (!is_breakpoint (loc->owner))
836 return;
837
838 loc->condition_changed = condition_modified;
839 }
840
841 /* Sets the condition-evaluation mode using the static global
842 condition_evaluation_mode. */
843
844 static void
845 set_condition_evaluation_mode (char *args, int from_tty,
846 struct cmd_list_element *c)
847 {
848 const char *old_mode, *new_mode;
849
850 if ((condition_evaluation_mode_1 == condition_evaluation_target)
851 && !target_supports_evaluation_of_breakpoint_conditions ())
852 {
853 condition_evaluation_mode_1 = condition_evaluation_mode;
854 warning (_("Target does not support breakpoint condition evaluation.\n"
855 "Using host evaluation mode instead."));
856 return;
857 }
858
859 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
860 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
861
862 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
863 settings was "auto". */
864 condition_evaluation_mode = condition_evaluation_mode_1;
865
866 /* Only update the mode if the user picked a different one. */
867 if (new_mode != old_mode)
868 {
869 struct bp_location *loc, **loc_tmp;
870 /* If the user switched to a different evaluation mode, we
871 need to synch the changes with the target as follows:
872
873 "host" -> "target": Send all (valid) conditions to the target.
874 "target" -> "host": Remove all the conditions from the target.
875 */
876
877 if (new_mode == condition_evaluation_target)
878 {
879 /* Mark everything modified and synch conditions with the
880 target. */
881 ALL_BP_LOCATIONS (loc, loc_tmp)
882 mark_breakpoint_location_modified (loc);
883 }
884 else
885 {
886 /* Manually mark non-duplicate locations to synch conditions
887 with the target. We do this to remove all the conditions the
888 target knows about. */
889 ALL_BP_LOCATIONS (loc, loc_tmp)
890 if (is_breakpoint (loc->owner) && loc->inserted)
891 loc->needs_update = 1;
892 }
893
894 /* Do the update. */
895 update_global_location_list (UGLL_MAY_INSERT);
896 }
897
898 return;
899 }
900
901 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
902 what "auto" is translating to. */
903
904 static void
905 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
906 struct cmd_list_element *c, const char *value)
907 {
908 if (condition_evaluation_mode == condition_evaluation_auto)
909 fprintf_filtered (file,
910 _("Breakpoint condition evaluation "
911 "mode is %s (currently %s).\n"),
912 value,
913 breakpoint_condition_evaluation_mode ());
914 else
915 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
916 value);
917 }
918
919 /* A comparison function for bp_location AP and BP that is used by
920 bsearch. This comparison function only cares about addresses, unlike
921 the more general bp_locations_compare function. */
922
923 static int
924 bp_locations_compare_addrs (const void *ap, const void *bp)
925 {
926 const struct bp_location *a = *(const struct bp_location **) ap;
927 const struct bp_location *b = *(const struct bp_location **) bp;
928
929 if (a->address == b->address)
930 return 0;
931 else
932 return ((a->address > b->address) - (a->address < b->address));
933 }
934
935 /* Helper function to skip all bp_locations with addresses
936 less than ADDRESS. It returns the first bp_location that
937 is greater than or equal to ADDRESS. If none is found, just
938 return NULL. */
939
940 static struct bp_location **
941 get_first_locp_gte_addr (CORE_ADDR address)
942 {
943 struct bp_location dummy_loc;
944 struct bp_location *dummy_locp = &dummy_loc;
945 struct bp_location **locp_found = NULL;
946
947 /* Initialize the dummy location's address field. */
948 dummy_loc.address = address;
949
950 /* Find a close match to the first location at ADDRESS. */
951 locp_found = ((struct bp_location **)
952 bsearch (&dummy_locp, bp_locations, bp_locations_count,
953 sizeof (struct bp_location **),
954 bp_locations_compare_addrs));
955
956 /* Nothing was found, nothing left to do. */
957 if (locp_found == NULL)
958 return NULL;
959
960 /* We may have found a location that is at ADDRESS but is not the first in the
961 location's list. Go backwards (if possible) and locate the first one. */
962 while ((locp_found - 1) >= bp_locations
963 && (*(locp_found - 1))->address == address)
964 locp_found--;
965
966 return locp_found;
967 }
968
969 void
970 set_breakpoint_condition (struct breakpoint *b, const char *exp,
971 int from_tty)
972 {
973 xfree (b->cond_string);
974 b->cond_string = NULL;
975
976 if (is_watchpoint (b))
977 {
978 struct watchpoint *w = (struct watchpoint *) b;
979
980 w->cond_exp.reset ();
981 }
982 else
983 {
984 struct bp_location *loc;
985
986 for (loc = b->loc; loc; loc = loc->next)
987 {
988 loc->cond.reset ();
989
990 /* No need to free the condition agent expression
991 bytecode (if we have one). We will handle this
992 when we go through update_global_location_list. */
993 }
994 }
995
996 if (*exp == 0)
997 {
998 if (from_tty)
999 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1000 }
1001 else
1002 {
1003 const char *arg = exp;
1004
1005 /* I don't know if it matters whether this is the string the user
1006 typed in or the decompiled expression. */
1007 b->cond_string = xstrdup (arg);
1008 b->condition_not_parsed = 0;
1009
1010 if (is_watchpoint (b))
1011 {
1012 struct watchpoint *w = (struct watchpoint *) b;
1013
1014 innermost_block = NULL;
1015 arg = exp;
1016 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1017 if (*arg)
1018 error (_("Junk at end of expression"));
1019 w->cond_exp_valid_block = innermost_block;
1020 }
1021 else
1022 {
1023 struct bp_location *loc;
1024
1025 for (loc = b->loc; loc; loc = loc->next)
1026 {
1027 arg = exp;
1028 loc->cond =
1029 parse_exp_1 (&arg, loc->address,
1030 block_for_pc (loc->address), 0);
1031 if (*arg)
1032 error (_("Junk at end of expression"));
1033 }
1034 }
1035 }
1036 mark_breakpoint_modified (b);
1037
1038 observer_notify_breakpoint_modified (b);
1039 }
1040
1041 /* Completion for the "condition" command. */
1042
1043 static void
1044 condition_completer (struct cmd_list_element *cmd,
1045 completion_tracker &tracker,
1046 const char *text, const char *word)
1047 {
1048 const char *space;
1049
1050 text = skip_spaces_const (text);
1051 space = skip_to_space_const (text);
1052 if (*space == '\0')
1053 {
1054 int len;
1055 struct breakpoint *b;
1056 VEC (char_ptr) *result = NULL;
1057
1058 if (text[0] == '$')
1059 {
1060 /* We don't support completion of history indices. */
1061 if (!isdigit (text[1]))
1062 complete_internalvar (tracker, &text[1]);
1063 return;
1064 }
1065
1066 /* We're completing the breakpoint number. */
1067 len = strlen (text);
1068
1069 ALL_BREAKPOINTS (b)
1070 {
1071 char number[50];
1072
1073 xsnprintf (number, sizeof (number), "%d", b->number);
1074
1075 if (strncmp (number, text, len) == 0)
1076 {
1077 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
1078 tracker.add_completion (std::move (copy));
1079 }
1080 }
1081
1082 return;
1083 }
1084
1085 /* We're completing the expression part. */
1086 text = skip_spaces_const (space);
1087 expression_completer (cmd, tracker, text, word);
1088 }
1089
1090 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1091
1092 static void
1093 condition_command (char *arg, int from_tty)
1094 {
1095 struct breakpoint *b;
1096 char *p;
1097 int bnum;
1098
1099 if (arg == 0)
1100 error_no_arg (_("breakpoint number"));
1101
1102 p = arg;
1103 bnum = get_number (&p);
1104 if (bnum == 0)
1105 error (_("Bad breakpoint argument: '%s'"), arg);
1106
1107 ALL_BREAKPOINTS (b)
1108 if (b->number == bnum)
1109 {
1110 /* Check if this breakpoint has a "stop" method implemented in an
1111 extension language. This method and conditions entered into GDB
1112 from the CLI are mutually exclusive. */
1113 const struct extension_language_defn *extlang
1114 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1115
1116 if (extlang != NULL)
1117 {
1118 error (_("Only one stop condition allowed. There is currently"
1119 " a %s stop condition defined for this breakpoint."),
1120 ext_lang_capitalized_name (extlang));
1121 }
1122 set_breakpoint_condition (b, p, from_tty);
1123
1124 if (is_breakpoint (b))
1125 update_global_location_list (UGLL_MAY_INSERT);
1126
1127 return;
1128 }
1129
1130 error (_("No breakpoint number %d."), bnum);
1131 }
1132
1133 /* Check that COMMAND do not contain commands that are suitable
1134 only for tracepoints and not suitable for ordinary breakpoints.
1135 Throw if any such commands is found. */
1136
1137 static void
1138 check_no_tracepoint_commands (struct command_line *commands)
1139 {
1140 struct command_line *c;
1141
1142 for (c = commands; c; c = c->next)
1143 {
1144 int i;
1145
1146 if (c->control_type == while_stepping_control)
1147 error (_("The 'while-stepping' command can "
1148 "only be used for tracepoints"));
1149
1150 for (i = 0; i < c->body_count; ++i)
1151 check_no_tracepoint_commands ((c->body_list)[i]);
1152
1153 /* Not that command parsing removes leading whitespace and comment
1154 lines and also empty lines. So, we only need to check for
1155 command directly. */
1156 if (strstr (c->line, "collect ") == c->line)
1157 error (_("The 'collect' command can only be used for tracepoints"));
1158
1159 if (strstr (c->line, "teval ") == c->line)
1160 error (_("The 'teval' command can only be used for tracepoints"));
1161 }
1162 }
1163
1164 struct longjmp_breakpoint : public breakpoint
1165 {
1166 ~longjmp_breakpoint () override;
1167 };
1168
1169 /* Encapsulate tests for different types of tracepoints. */
1170
1171 static bool
1172 is_tracepoint_type (bptype type)
1173 {
1174 return (type == bp_tracepoint
1175 || type == bp_fast_tracepoint
1176 || type == bp_static_tracepoint);
1177 }
1178
1179 static bool
1180 is_longjmp_type (bptype type)
1181 {
1182 return type == bp_longjmp || type == bp_exception;
1183 }
1184
1185 int
1186 is_tracepoint (const struct breakpoint *b)
1187 {
1188 return is_tracepoint_type (b->type);
1189 }
1190
1191 /* Factory function to create an appropriate instance of breakpoint given
1192 TYPE. */
1193
1194 static std::unique_ptr<breakpoint>
1195 new_breakpoint_from_type (bptype type)
1196 {
1197 breakpoint *b;
1198
1199 if (is_tracepoint_type (type))
1200 b = new tracepoint ();
1201 else if (is_longjmp_type (type))
1202 b = new longjmp_breakpoint ();
1203 else
1204 b = new breakpoint ();
1205
1206 return std::unique_ptr<breakpoint> (b);
1207 }
1208
1209 /* A helper function that validates that COMMANDS are valid for a
1210 breakpoint. This function will throw an exception if a problem is
1211 found. */
1212
1213 static void
1214 validate_commands_for_breakpoint (struct breakpoint *b,
1215 struct command_line *commands)
1216 {
1217 if (is_tracepoint (b))
1218 {
1219 struct tracepoint *t = (struct tracepoint *) b;
1220 struct command_line *c;
1221 struct command_line *while_stepping = 0;
1222
1223 /* Reset the while-stepping step count. The previous commands
1224 might have included a while-stepping action, while the new
1225 ones might not. */
1226 t->step_count = 0;
1227
1228 /* We need to verify that each top-level element of commands is
1229 valid for tracepoints, that there's at most one
1230 while-stepping element, and that the while-stepping's body
1231 has valid tracing commands excluding nested while-stepping.
1232 We also need to validate the tracepoint action line in the
1233 context of the tracepoint --- validate_actionline actually
1234 has side effects, like setting the tracepoint's
1235 while-stepping STEP_COUNT, in addition to checking if the
1236 collect/teval actions parse and make sense in the
1237 tracepoint's context. */
1238 for (c = commands; c; c = c->next)
1239 {
1240 if (c->control_type == while_stepping_control)
1241 {
1242 if (b->type == bp_fast_tracepoint)
1243 error (_("The 'while-stepping' command "
1244 "cannot be used for fast tracepoint"));
1245 else if (b->type == bp_static_tracepoint)
1246 error (_("The 'while-stepping' command "
1247 "cannot be used for static tracepoint"));
1248
1249 if (while_stepping)
1250 error (_("The 'while-stepping' command "
1251 "can be used only once"));
1252 else
1253 while_stepping = c;
1254 }
1255
1256 validate_actionline (c->line, b);
1257 }
1258 if (while_stepping)
1259 {
1260 struct command_line *c2;
1261
1262 gdb_assert (while_stepping->body_count == 1);
1263 c2 = while_stepping->body_list[0];
1264 for (; c2; c2 = c2->next)
1265 {
1266 if (c2->control_type == while_stepping_control)
1267 error (_("The 'while-stepping' command cannot be nested"));
1268 }
1269 }
1270 }
1271 else
1272 {
1273 check_no_tracepoint_commands (commands);
1274 }
1275 }
1276
1277 /* Return a vector of all the static tracepoints set at ADDR. The
1278 caller is responsible for releasing the vector. */
1279
1280 VEC(breakpoint_p) *
1281 static_tracepoints_here (CORE_ADDR addr)
1282 {
1283 struct breakpoint *b;
1284 VEC(breakpoint_p) *found = 0;
1285 struct bp_location *loc;
1286
1287 ALL_BREAKPOINTS (b)
1288 if (b->type == bp_static_tracepoint)
1289 {
1290 for (loc = b->loc; loc; loc = loc->next)
1291 if (loc->address == addr)
1292 VEC_safe_push(breakpoint_p, found, b);
1293 }
1294
1295 return found;
1296 }
1297
1298 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1299 validate that only allowed commands are included. */
1300
1301 void
1302 breakpoint_set_commands (struct breakpoint *b,
1303 command_line_up &&commands)
1304 {
1305 validate_commands_for_breakpoint (b, commands.get ());
1306
1307 decref_counted_command_line (&b->commands);
1308 b->commands = alloc_counted_command_line (commands.release ());
1309 observer_notify_breakpoint_modified (b);
1310 }
1311
1312 /* Set the internal `silent' flag on the breakpoint. Note that this
1313 is not the same as the "silent" that may appear in the breakpoint's
1314 commands. */
1315
1316 void
1317 breakpoint_set_silent (struct breakpoint *b, int silent)
1318 {
1319 int old_silent = b->silent;
1320
1321 b->silent = silent;
1322 if (old_silent != silent)
1323 observer_notify_breakpoint_modified (b);
1324 }
1325
1326 /* Set the thread for this breakpoint. If THREAD is -1, make the
1327 breakpoint work for any thread. */
1328
1329 void
1330 breakpoint_set_thread (struct breakpoint *b, int thread)
1331 {
1332 int old_thread = b->thread;
1333
1334 b->thread = thread;
1335 if (old_thread != thread)
1336 observer_notify_breakpoint_modified (b);
1337 }
1338
1339 /* Set the task for this breakpoint. If TASK is 0, make the
1340 breakpoint work for any task. */
1341
1342 void
1343 breakpoint_set_task (struct breakpoint *b, int task)
1344 {
1345 int old_task = b->task;
1346
1347 b->task = task;
1348 if (old_task != task)
1349 observer_notify_breakpoint_modified (b);
1350 }
1351
1352 void
1353 check_tracepoint_command (char *line, void *closure)
1354 {
1355 struct breakpoint *b = (struct breakpoint *) closure;
1356
1357 validate_actionline (line, b);
1358 }
1359
1360 /* A structure used to pass information through
1361 map_breakpoint_numbers. */
1362
1363 struct commands_info
1364 {
1365 /* True if the command was typed at a tty. */
1366 int from_tty;
1367
1368 /* The breakpoint range spec. */
1369 const char *arg;
1370
1371 /* Non-NULL if the body of the commands are being read from this
1372 already-parsed command. */
1373 struct command_line *control;
1374
1375 /* The command lines read from the user, or NULL if they have not
1376 yet been read. */
1377 struct counted_command_line *cmd;
1378 };
1379
1380 /* A callback for map_breakpoint_numbers that sets the commands for
1381 commands_command. */
1382
1383 static void
1384 do_map_commands_command (struct breakpoint *b, void *data)
1385 {
1386 struct commands_info *info = (struct commands_info *) data;
1387
1388 if (info->cmd == NULL)
1389 {
1390 command_line_up l;
1391
1392 if (info->control != NULL)
1393 l = copy_command_lines (info->control->body_list[0]);
1394 else
1395 {
1396 struct cleanup *old_chain;
1397 char *str;
1398
1399 str = xstrprintf (_("Type commands for breakpoint(s) "
1400 "%s, one per line."),
1401 info->arg);
1402
1403 old_chain = make_cleanup (xfree, str);
1404
1405 l = read_command_lines (str,
1406 info->from_tty, 1,
1407 (is_tracepoint (b)
1408 ? check_tracepoint_command : 0),
1409 b);
1410
1411 do_cleanups (old_chain);
1412 }
1413
1414 info->cmd = alloc_counted_command_line (l.release ());
1415 }
1416
1417 /* If a breakpoint was on the list more than once, we don't need to
1418 do anything. */
1419 if (b->commands != info->cmd)
1420 {
1421 validate_commands_for_breakpoint (b, info->cmd->commands);
1422 incref_counted_command_line (info->cmd);
1423 decref_counted_command_line (&b->commands);
1424 b->commands = info->cmd;
1425 observer_notify_breakpoint_modified (b);
1426 }
1427 }
1428
1429 static void
1430 commands_command_1 (const char *arg, int from_tty,
1431 struct command_line *control)
1432 {
1433 struct cleanup *cleanups;
1434 struct commands_info info;
1435
1436 info.from_tty = from_tty;
1437 info.control = control;
1438 info.cmd = NULL;
1439 /* If we read command lines from the user, then `info' will hold an
1440 extra reference to the commands that we must clean up. */
1441 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1442
1443 std::string new_arg;
1444
1445 if (arg == NULL || !*arg)
1446 {
1447 if (breakpoint_count - prev_breakpoint_count > 1)
1448 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1449 breakpoint_count);
1450 else if (breakpoint_count > 0)
1451 new_arg = string_printf ("%d", breakpoint_count);
1452 }
1453 else
1454 new_arg = arg;
1455
1456 info.arg = new_arg.c_str ();
1457
1458 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1459
1460 if (info.cmd == NULL)
1461 error (_("No breakpoints specified."));
1462
1463 do_cleanups (cleanups);
1464 }
1465
1466 static void
1467 commands_command (char *arg, int from_tty)
1468 {
1469 commands_command_1 (arg, from_tty, NULL);
1470 }
1471
1472 /* Like commands_command, but instead of reading the commands from
1473 input stream, takes them from an already parsed command structure.
1474
1475 This is used by cli-script.c to DTRT with breakpoint commands
1476 that are part of if and while bodies. */
1477 enum command_control_type
1478 commands_from_control_command (const char *arg, struct command_line *cmd)
1479 {
1480 commands_command_1 (arg, 0, cmd);
1481 return simple_control;
1482 }
1483
1484 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1485
1486 static int
1487 bp_location_has_shadow (struct bp_location *bl)
1488 {
1489 if (bl->loc_type != bp_loc_software_breakpoint)
1490 return 0;
1491 if (!bl->inserted)
1492 return 0;
1493 if (bl->target_info.shadow_len == 0)
1494 /* BL isn't valid, or doesn't shadow memory. */
1495 return 0;
1496 return 1;
1497 }
1498
1499 /* Update BUF, which is LEN bytes read from the target address
1500 MEMADDR, by replacing a memory breakpoint with its shadowed
1501 contents.
1502
1503 If READBUF is not NULL, this buffer must not overlap with the of
1504 the breakpoint location's shadow_contents buffer. Otherwise, a
1505 failed assertion internal error will be raised. */
1506
1507 static void
1508 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1509 const gdb_byte *writebuf_org,
1510 ULONGEST memaddr, LONGEST len,
1511 struct bp_target_info *target_info,
1512 struct gdbarch *gdbarch)
1513 {
1514 /* Now do full processing of the found relevant range of elements. */
1515 CORE_ADDR bp_addr = 0;
1516 int bp_size = 0;
1517 int bptoffset = 0;
1518
1519 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1520 current_program_space->aspace, 0))
1521 {
1522 /* The breakpoint is inserted in a different address space. */
1523 return;
1524 }
1525
1526 /* Addresses and length of the part of the breakpoint that
1527 we need to copy. */
1528 bp_addr = target_info->placed_address;
1529 bp_size = target_info->shadow_len;
1530
1531 if (bp_addr + bp_size <= memaddr)
1532 {
1533 /* The breakpoint is entirely before the chunk of memory we are
1534 reading. */
1535 return;
1536 }
1537
1538 if (bp_addr >= memaddr + len)
1539 {
1540 /* The breakpoint is entirely after the chunk of memory we are
1541 reading. */
1542 return;
1543 }
1544
1545 /* Offset within shadow_contents. */
1546 if (bp_addr < memaddr)
1547 {
1548 /* Only copy the second part of the breakpoint. */
1549 bp_size -= memaddr - bp_addr;
1550 bptoffset = memaddr - bp_addr;
1551 bp_addr = memaddr;
1552 }
1553
1554 if (bp_addr + bp_size > memaddr + len)
1555 {
1556 /* Only copy the first part of the breakpoint. */
1557 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1558 }
1559
1560 if (readbuf != NULL)
1561 {
1562 /* Verify that the readbuf buffer does not overlap with the
1563 shadow_contents buffer. */
1564 gdb_assert (target_info->shadow_contents >= readbuf + len
1565 || readbuf >= (target_info->shadow_contents
1566 + target_info->shadow_len));
1567
1568 /* Update the read buffer with this inserted breakpoint's
1569 shadow. */
1570 memcpy (readbuf + bp_addr - memaddr,
1571 target_info->shadow_contents + bptoffset, bp_size);
1572 }
1573 else
1574 {
1575 const unsigned char *bp;
1576 CORE_ADDR addr = target_info->reqstd_address;
1577 int placed_size;
1578
1579 /* Update the shadow with what we want to write to memory. */
1580 memcpy (target_info->shadow_contents + bptoffset,
1581 writebuf_org + bp_addr - memaddr, bp_size);
1582
1583 /* Determine appropriate breakpoint contents and size for this
1584 address. */
1585 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1586
1587 /* Update the final write buffer with this inserted
1588 breakpoint's INSN. */
1589 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1590 }
1591 }
1592
1593 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1594 by replacing any memory breakpoints with their shadowed contents.
1595
1596 If READBUF is not NULL, this buffer must not overlap with any of
1597 the breakpoint location's shadow_contents buffers. Otherwise,
1598 a failed assertion internal error will be raised.
1599
1600 The range of shadowed area by each bp_location is:
1601 bl->address - bp_locations_placed_address_before_address_max
1602 up to bl->address + bp_locations_shadow_len_after_address_max
1603 The range we were requested to resolve shadows for is:
1604 memaddr ... memaddr + len
1605 Thus the safe cutoff boundaries for performance optimization are
1606 memaddr + len <= (bl->address
1607 - bp_locations_placed_address_before_address_max)
1608 and:
1609 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1610
1611 void
1612 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1613 const gdb_byte *writebuf_org,
1614 ULONGEST memaddr, LONGEST len)
1615 {
1616 /* Left boundary, right boundary and median element of our binary
1617 search. */
1618 unsigned bc_l, bc_r, bc;
1619
1620 /* Find BC_L which is a leftmost element which may affect BUF
1621 content. It is safe to report lower value but a failure to
1622 report higher one. */
1623
1624 bc_l = 0;
1625 bc_r = bp_locations_count;
1626 while (bc_l + 1 < bc_r)
1627 {
1628 struct bp_location *bl;
1629
1630 bc = (bc_l + bc_r) / 2;
1631 bl = bp_locations[bc];
1632
1633 /* Check first BL->ADDRESS will not overflow due to the added
1634 constant. Then advance the left boundary only if we are sure
1635 the BC element can in no way affect the BUF content (MEMADDR
1636 to MEMADDR + LEN range).
1637
1638 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1639 offset so that we cannot miss a breakpoint with its shadow
1640 range tail still reaching MEMADDR. */
1641
1642 if ((bl->address + bp_locations_shadow_len_after_address_max
1643 >= bl->address)
1644 && (bl->address + bp_locations_shadow_len_after_address_max
1645 <= memaddr))
1646 bc_l = bc;
1647 else
1648 bc_r = bc;
1649 }
1650
1651 /* Due to the binary search above, we need to make sure we pick the
1652 first location that's at BC_L's address. E.g., if there are
1653 multiple locations at the same address, BC_L may end up pointing
1654 at a duplicate location, and miss the "master"/"inserted"
1655 location. Say, given locations L1, L2 and L3 at addresses A and
1656 B:
1657
1658 L1@A, L2@A, L3@B, ...
1659
1660 BC_L could end up pointing at location L2, while the "master"
1661 location could be L1. Since the `loc->inserted' flag is only set
1662 on "master" locations, we'd forget to restore the shadow of L1
1663 and L2. */
1664 while (bc_l > 0
1665 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1666 bc_l--;
1667
1668 /* Now do full processing of the found relevant range of elements. */
1669
1670 for (bc = bc_l; bc < bp_locations_count; bc++)
1671 {
1672 struct bp_location *bl = bp_locations[bc];
1673
1674 /* bp_location array has BL->OWNER always non-NULL. */
1675 if (bl->owner->type == bp_none)
1676 warning (_("reading through apparently deleted breakpoint #%d?"),
1677 bl->owner->number);
1678
1679 /* Performance optimization: any further element can no longer affect BUF
1680 content. */
1681
1682 if (bl->address >= bp_locations_placed_address_before_address_max
1683 && memaddr + len <= (bl->address
1684 - bp_locations_placed_address_before_address_max))
1685 break;
1686
1687 if (!bp_location_has_shadow (bl))
1688 continue;
1689
1690 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1691 memaddr, len, &bl->target_info, bl->gdbarch);
1692 }
1693 }
1694
1695 \f
1696
1697 /* Return true if BPT is either a software breakpoint or a hardware
1698 breakpoint. */
1699
1700 int
1701 is_breakpoint (const struct breakpoint *bpt)
1702 {
1703 return (bpt->type == bp_breakpoint
1704 || bpt->type == bp_hardware_breakpoint
1705 || bpt->type == bp_dprintf);
1706 }
1707
1708 /* Return true if BPT is of any hardware watchpoint kind. */
1709
1710 static int
1711 is_hardware_watchpoint (const struct breakpoint *bpt)
1712 {
1713 return (bpt->type == bp_hardware_watchpoint
1714 || bpt->type == bp_read_watchpoint
1715 || bpt->type == bp_access_watchpoint);
1716 }
1717
1718 /* Return true if BPT is of any watchpoint kind, hardware or
1719 software. */
1720
1721 int
1722 is_watchpoint (const struct breakpoint *bpt)
1723 {
1724 return (is_hardware_watchpoint (bpt)
1725 || bpt->type == bp_watchpoint);
1726 }
1727
1728 /* Returns true if the current thread and its running state are safe
1729 to evaluate or update watchpoint B. Watchpoints on local
1730 expressions need to be evaluated in the context of the thread that
1731 was current when the watchpoint was created, and, that thread needs
1732 to be stopped to be able to select the correct frame context.
1733 Watchpoints on global expressions can be evaluated on any thread,
1734 and in any state. It is presently left to the target allowing
1735 memory accesses when threads are running. */
1736
1737 static int
1738 watchpoint_in_thread_scope (struct watchpoint *b)
1739 {
1740 return (b->pspace == current_program_space
1741 && (ptid_equal (b->watchpoint_thread, null_ptid)
1742 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1743 && !is_executing (inferior_ptid))));
1744 }
1745
1746 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1747 associated bp_watchpoint_scope breakpoint. */
1748
1749 static void
1750 watchpoint_del_at_next_stop (struct watchpoint *w)
1751 {
1752 if (w->related_breakpoint != w)
1753 {
1754 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1755 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1756 w->related_breakpoint->disposition = disp_del_at_next_stop;
1757 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1758 w->related_breakpoint = w;
1759 }
1760 w->disposition = disp_del_at_next_stop;
1761 }
1762
1763 /* Extract a bitfield value from value VAL using the bit parameters contained in
1764 watchpoint W. */
1765
1766 static struct value *
1767 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1768 {
1769 struct value *bit_val;
1770
1771 if (val == NULL)
1772 return NULL;
1773
1774 bit_val = allocate_value (value_type (val));
1775
1776 unpack_value_bitfield (bit_val,
1777 w->val_bitpos,
1778 w->val_bitsize,
1779 value_contents_for_printing (val),
1780 value_offset (val),
1781 val);
1782
1783 return bit_val;
1784 }
1785
1786 /* Allocate a dummy location and add it to B, which must be a software
1787 watchpoint. This is required because even if a software watchpoint
1788 is not watching any memory, bpstat_stop_status requires a location
1789 to be able to report stops. */
1790
1791 static void
1792 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1793 struct program_space *pspace)
1794 {
1795 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1796
1797 b->loc = allocate_bp_location (b);
1798 b->loc->pspace = pspace;
1799 b->loc->address = -1;
1800 b->loc->length = -1;
1801 }
1802
1803 /* Returns true if B is a software watchpoint that is not watching any
1804 memory (e.g., "watch $pc"). */
1805
1806 static int
1807 is_no_memory_software_watchpoint (struct breakpoint *b)
1808 {
1809 return (b->type == bp_watchpoint
1810 && b->loc != NULL
1811 && b->loc->next == NULL
1812 && b->loc->address == -1
1813 && b->loc->length == -1);
1814 }
1815
1816 /* Assuming that B is a watchpoint:
1817 - Reparse watchpoint expression, if REPARSE is non-zero
1818 - Evaluate expression and store the result in B->val
1819 - Evaluate the condition if there is one, and store the result
1820 in b->loc->cond.
1821 - Update the list of values that must be watched in B->loc.
1822
1823 If the watchpoint disposition is disp_del_at_next_stop, then do
1824 nothing. If this is local watchpoint that is out of scope, delete
1825 it.
1826
1827 Even with `set breakpoint always-inserted on' the watchpoints are
1828 removed + inserted on each stop here. Normal breakpoints must
1829 never be removed because they might be missed by a running thread
1830 when debugging in non-stop mode. On the other hand, hardware
1831 watchpoints (is_hardware_watchpoint; processed here) are specific
1832 to each LWP since they are stored in each LWP's hardware debug
1833 registers. Therefore, such LWP must be stopped first in order to
1834 be able to modify its hardware watchpoints.
1835
1836 Hardware watchpoints must be reset exactly once after being
1837 presented to the user. It cannot be done sooner, because it would
1838 reset the data used to present the watchpoint hit to the user. And
1839 it must not be done later because it could display the same single
1840 watchpoint hit during multiple GDB stops. Note that the latter is
1841 relevant only to the hardware watchpoint types bp_read_watchpoint
1842 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1843 not user-visible - its hit is suppressed if the memory content has
1844 not changed.
1845
1846 The following constraints influence the location where we can reset
1847 hardware watchpoints:
1848
1849 * target_stopped_by_watchpoint and target_stopped_data_address are
1850 called several times when GDB stops.
1851
1852 [linux]
1853 * Multiple hardware watchpoints can be hit at the same time,
1854 causing GDB to stop. GDB only presents one hardware watchpoint
1855 hit at a time as the reason for stopping, and all the other hits
1856 are presented later, one after the other, each time the user
1857 requests the execution to be resumed. Execution is not resumed
1858 for the threads still having pending hit event stored in
1859 LWP_INFO->STATUS. While the watchpoint is already removed from
1860 the inferior on the first stop the thread hit event is kept being
1861 reported from its cached value by linux_nat_stopped_data_address
1862 until the real thread resume happens after the watchpoint gets
1863 presented and thus its LWP_INFO->STATUS gets reset.
1864
1865 Therefore the hardware watchpoint hit can get safely reset on the
1866 watchpoint removal from inferior. */
1867
1868 static void
1869 update_watchpoint (struct watchpoint *b, int reparse)
1870 {
1871 int within_current_scope;
1872 struct frame_id saved_frame_id;
1873 int frame_saved;
1874
1875 /* If this is a local watchpoint, we only want to check if the
1876 watchpoint frame is in scope if the current thread is the thread
1877 that was used to create the watchpoint. */
1878 if (!watchpoint_in_thread_scope (b))
1879 return;
1880
1881 if (b->disposition == disp_del_at_next_stop)
1882 return;
1883
1884 frame_saved = 0;
1885
1886 /* Determine if the watchpoint is within scope. */
1887 if (b->exp_valid_block == NULL)
1888 within_current_scope = 1;
1889 else
1890 {
1891 struct frame_info *fi = get_current_frame ();
1892 struct gdbarch *frame_arch = get_frame_arch (fi);
1893 CORE_ADDR frame_pc = get_frame_pc (fi);
1894
1895 /* If we're at a point where the stack has been destroyed
1896 (e.g. in a function epilogue), unwinding may not work
1897 properly. Do not attempt to recreate locations at this
1898 point. See similar comments in watchpoint_check. */
1899 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1900 return;
1901
1902 /* Save the current frame's ID so we can restore it after
1903 evaluating the watchpoint expression on its own frame. */
1904 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1905 took a frame parameter, so that we didn't have to change the
1906 selected frame. */
1907 frame_saved = 1;
1908 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1909
1910 fi = frame_find_by_id (b->watchpoint_frame);
1911 within_current_scope = (fi != NULL);
1912 if (within_current_scope)
1913 select_frame (fi);
1914 }
1915
1916 /* We don't free locations. They are stored in the bp_location array
1917 and update_global_location_list will eventually delete them and
1918 remove breakpoints if needed. */
1919 b->loc = NULL;
1920
1921 if (within_current_scope && reparse)
1922 {
1923 const char *s;
1924
1925 b->exp.reset ();
1926 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1927 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1928 /* If the meaning of expression itself changed, the old value is
1929 no longer relevant. We don't want to report a watchpoint hit
1930 to the user when the old value and the new value may actually
1931 be completely different objects. */
1932 value_free (b->val);
1933 b->val = NULL;
1934 b->val_valid = 0;
1935
1936 /* Note that unlike with breakpoints, the watchpoint's condition
1937 expression is stored in the breakpoint object, not in the
1938 locations (re)created below. */
1939 if (b->cond_string != NULL)
1940 {
1941 b->cond_exp.reset ();
1942
1943 s = b->cond_string;
1944 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1945 }
1946 }
1947
1948 /* If we failed to parse the expression, for example because
1949 it refers to a global variable in a not-yet-loaded shared library,
1950 don't try to insert watchpoint. We don't automatically delete
1951 such watchpoint, though, since failure to parse expression
1952 is different from out-of-scope watchpoint. */
1953 if (!target_has_execution)
1954 {
1955 /* Without execution, memory can't change. No use to try and
1956 set watchpoint locations. The watchpoint will be reset when
1957 the target gains execution, through breakpoint_re_set. */
1958 if (!can_use_hw_watchpoints)
1959 {
1960 if (b->ops->works_in_software_mode (b))
1961 b->type = bp_watchpoint;
1962 else
1963 error (_("Can't set read/access watchpoint when "
1964 "hardware watchpoints are disabled."));
1965 }
1966 }
1967 else if (within_current_scope && b->exp)
1968 {
1969 int pc = 0;
1970 struct value *val_chain, *v, *result, *next;
1971 struct program_space *frame_pspace;
1972
1973 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1974
1975 /* Avoid setting b->val if it's already set. The meaning of
1976 b->val is 'the last value' user saw, and we should update
1977 it only if we reported that last value to user. As it
1978 happens, the code that reports it updates b->val directly.
1979 We don't keep track of the memory value for masked
1980 watchpoints. */
1981 if (!b->val_valid && !is_masked_watchpoint (b))
1982 {
1983 if (b->val_bitsize != 0)
1984 {
1985 v = extract_bitfield_from_watchpoint_value (b, v);
1986 if (v != NULL)
1987 release_value (v);
1988 }
1989 b->val = v;
1990 b->val_valid = 1;
1991 }
1992
1993 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1994
1995 /* Look at each value on the value chain. */
1996 for (v = val_chain; v; v = value_next (v))
1997 {
1998 /* If it's a memory location, and GDB actually needed
1999 its contents to evaluate the expression, then we
2000 must watch it. If the first value returned is
2001 still lazy, that means an error occurred reading it;
2002 watch it anyway in case it becomes readable. */
2003 if (VALUE_LVAL (v) == lval_memory
2004 && (v == val_chain || ! value_lazy (v)))
2005 {
2006 struct type *vtype = check_typedef (value_type (v));
2007
2008 /* We only watch structs and arrays if user asked
2009 for it explicitly, never if they just happen to
2010 appear in the middle of some value chain. */
2011 if (v == result
2012 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2013 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2014 {
2015 CORE_ADDR addr;
2016 enum target_hw_bp_type type;
2017 struct bp_location *loc, **tmp;
2018 int bitpos = 0, bitsize = 0;
2019
2020 if (value_bitsize (v) != 0)
2021 {
2022 /* Extract the bit parameters out from the bitfield
2023 sub-expression. */
2024 bitpos = value_bitpos (v);
2025 bitsize = value_bitsize (v);
2026 }
2027 else if (v == result && b->val_bitsize != 0)
2028 {
2029 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2030 lvalue whose bit parameters are saved in the fields
2031 VAL_BITPOS and VAL_BITSIZE. */
2032 bitpos = b->val_bitpos;
2033 bitsize = b->val_bitsize;
2034 }
2035
2036 addr = value_address (v);
2037 if (bitsize != 0)
2038 {
2039 /* Skip the bytes that don't contain the bitfield. */
2040 addr += bitpos / 8;
2041 }
2042
2043 type = hw_write;
2044 if (b->type == bp_read_watchpoint)
2045 type = hw_read;
2046 else if (b->type == bp_access_watchpoint)
2047 type = hw_access;
2048
2049 loc = allocate_bp_location (b);
2050 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2051 ;
2052 *tmp = loc;
2053 loc->gdbarch = get_type_arch (value_type (v));
2054
2055 loc->pspace = frame_pspace;
2056 loc->address = addr;
2057
2058 if (bitsize != 0)
2059 {
2060 /* Just cover the bytes that make up the bitfield. */
2061 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2062 }
2063 else
2064 loc->length = TYPE_LENGTH (value_type (v));
2065
2066 loc->watchpoint_type = type;
2067 }
2068 }
2069 }
2070
2071 /* Change the type of breakpoint between hardware assisted or
2072 an ordinary watchpoint depending on the hardware support
2073 and free hardware slots. REPARSE is set when the inferior
2074 is started. */
2075 if (reparse)
2076 {
2077 int reg_cnt;
2078 enum bp_loc_type loc_type;
2079 struct bp_location *bl;
2080
2081 reg_cnt = can_use_hardware_watchpoint (val_chain);
2082
2083 if (reg_cnt)
2084 {
2085 int i, target_resources_ok, other_type_used;
2086 enum bptype type;
2087
2088 /* Use an exact watchpoint when there's only one memory region to be
2089 watched, and only one debug register is needed to watch it. */
2090 b->exact = target_exact_watchpoints && reg_cnt == 1;
2091
2092 /* We need to determine how many resources are already
2093 used for all other hardware watchpoints plus this one
2094 to see if we still have enough resources to also fit
2095 this watchpoint in as well. */
2096
2097 /* If this is a software watchpoint, we try to turn it
2098 to a hardware one -- count resources as if B was of
2099 hardware watchpoint type. */
2100 type = b->type;
2101 if (type == bp_watchpoint)
2102 type = bp_hardware_watchpoint;
2103
2104 /* This watchpoint may or may not have been placed on
2105 the list yet at this point (it won't be in the list
2106 if we're trying to create it for the first time,
2107 through watch_command), so always account for it
2108 manually. */
2109
2110 /* Count resources used by all watchpoints except B. */
2111 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2112
2113 /* Add in the resources needed for B. */
2114 i += hw_watchpoint_use_count (b);
2115
2116 target_resources_ok
2117 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2118 if (target_resources_ok <= 0)
2119 {
2120 int sw_mode = b->ops->works_in_software_mode (b);
2121
2122 if (target_resources_ok == 0 && !sw_mode)
2123 error (_("Target does not support this type of "
2124 "hardware watchpoint."));
2125 else if (target_resources_ok < 0 && !sw_mode)
2126 error (_("There are not enough available hardware "
2127 "resources for this watchpoint."));
2128
2129 /* Downgrade to software watchpoint. */
2130 b->type = bp_watchpoint;
2131 }
2132 else
2133 {
2134 /* If this was a software watchpoint, we've just
2135 found we have enough resources to turn it to a
2136 hardware watchpoint. Otherwise, this is a
2137 nop. */
2138 b->type = type;
2139 }
2140 }
2141 else if (!b->ops->works_in_software_mode (b))
2142 {
2143 if (!can_use_hw_watchpoints)
2144 error (_("Can't set read/access watchpoint when "
2145 "hardware watchpoints are disabled."));
2146 else
2147 error (_("Expression cannot be implemented with "
2148 "read/access watchpoint."));
2149 }
2150 else
2151 b->type = bp_watchpoint;
2152
2153 loc_type = (b->type == bp_watchpoint? bp_loc_other
2154 : bp_loc_hardware_watchpoint);
2155 for (bl = b->loc; bl; bl = bl->next)
2156 bl->loc_type = loc_type;
2157 }
2158
2159 for (v = val_chain; v; v = next)
2160 {
2161 next = value_next (v);
2162 if (v != b->val)
2163 value_free (v);
2164 }
2165
2166 /* If a software watchpoint is not watching any memory, then the
2167 above left it without any location set up. But,
2168 bpstat_stop_status requires a location to be able to report
2169 stops, so make sure there's at least a dummy one. */
2170 if (b->type == bp_watchpoint && b->loc == NULL)
2171 software_watchpoint_add_no_memory_location (b, frame_pspace);
2172 }
2173 else if (!within_current_scope)
2174 {
2175 printf_filtered (_("\
2176 Watchpoint %d deleted because the program has left the block\n\
2177 in which its expression is valid.\n"),
2178 b->number);
2179 watchpoint_del_at_next_stop (b);
2180 }
2181
2182 /* Restore the selected frame. */
2183 if (frame_saved)
2184 select_frame (frame_find_by_id (saved_frame_id));
2185 }
2186
2187
2188 /* Returns 1 iff breakpoint location should be
2189 inserted in the inferior. We don't differentiate the type of BL's owner
2190 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2191 breakpoint_ops is not defined, because in insert_bp_location,
2192 tracepoint's insert_location will not be called. */
2193 static int
2194 should_be_inserted (struct bp_location *bl)
2195 {
2196 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2197 return 0;
2198
2199 if (bl->owner->disposition == disp_del_at_next_stop)
2200 return 0;
2201
2202 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2203 return 0;
2204
2205 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2206 return 0;
2207
2208 /* This is set for example, when we're attached to the parent of a
2209 vfork, and have detached from the child. The child is running
2210 free, and we expect it to do an exec or exit, at which point the
2211 OS makes the parent schedulable again (and the target reports
2212 that the vfork is done). Until the child is done with the shared
2213 memory region, do not insert breakpoints in the parent, otherwise
2214 the child could still trip on the parent's breakpoints. Since
2215 the parent is blocked anyway, it won't miss any breakpoint. */
2216 if (bl->pspace->breakpoints_not_allowed)
2217 return 0;
2218
2219 /* Don't insert a breakpoint if we're trying to step past its
2220 location, except if the breakpoint is a single-step breakpoint,
2221 and the breakpoint's thread is the thread which is stepping past
2222 a breakpoint. */
2223 if ((bl->loc_type == bp_loc_software_breakpoint
2224 || bl->loc_type == bp_loc_hardware_breakpoint)
2225 && stepping_past_instruction_at (bl->pspace->aspace,
2226 bl->address)
2227 /* The single-step breakpoint may be inserted at the location
2228 we're trying to step if the instruction branches to itself.
2229 However, the instruction won't be executed at all and it may
2230 break the semantics of the instruction, for example, the
2231 instruction is a conditional branch or updates some flags.
2232 We can't fix it unless GDB is able to emulate the instruction
2233 or switch to displaced stepping. */
2234 && !(bl->owner->type == bp_single_step
2235 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2236 {
2237 if (debug_infrun)
2238 {
2239 fprintf_unfiltered (gdb_stdlog,
2240 "infrun: skipping breakpoint: "
2241 "stepping past insn at: %s\n",
2242 paddress (bl->gdbarch, bl->address));
2243 }
2244 return 0;
2245 }
2246
2247 /* Don't insert watchpoints if we're trying to step past the
2248 instruction that triggered one. */
2249 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2250 && stepping_past_nonsteppable_watchpoint ())
2251 {
2252 if (debug_infrun)
2253 {
2254 fprintf_unfiltered (gdb_stdlog,
2255 "infrun: stepping past non-steppable watchpoint. "
2256 "skipping watchpoint at %s:%d\n",
2257 paddress (bl->gdbarch, bl->address),
2258 bl->length);
2259 }
2260 return 0;
2261 }
2262
2263 return 1;
2264 }
2265
2266 /* Same as should_be_inserted but does the check assuming
2267 that the location is not duplicated. */
2268
2269 static int
2270 unduplicated_should_be_inserted (struct bp_location *bl)
2271 {
2272 int result;
2273 const int save_duplicate = bl->duplicate;
2274
2275 bl->duplicate = 0;
2276 result = should_be_inserted (bl);
2277 bl->duplicate = save_duplicate;
2278 return result;
2279 }
2280
2281 /* Parses a conditional described by an expression COND into an
2282 agent expression bytecode suitable for evaluation
2283 by the bytecode interpreter. Return NULL if there was
2284 any error during parsing. */
2285
2286 static agent_expr_up
2287 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2288 {
2289 if (cond == NULL)
2290 return NULL;
2291
2292 agent_expr_up aexpr;
2293
2294 /* We don't want to stop processing, so catch any errors
2295 that may show up. */
2296 TRY
2297 {
2298 aexpr = gen_eval_for_expr (scope, cond);
2299 }
2300
2301 CATCH (ex, RETURN_MASK_ERROR)
2302 {
2303 /* If we got here, it means the condition could not be parsed to a valid
2304 bytecode expression and thus can't be evaluated on the target's side.
2305 It's no use iterating through the conditions. */
2306 }
2307 END_CATCH
2308
2309 /* We have a valid agent expression. */
2310 return aexpr;
2311 }
2312
2313 /* Based on location BL, create a list of breakpoint conditions to be
2314 passed on to the target. If we have duplicated locations with different
2315 conditions, we will add such conditions to the list. The idea is that the
2316 target will evaluate the list of conditions and will only notify GDB when
2317 one of them is true. */
2318
2319 static void
2320 build_target_condition_list (struct bp_location *bl)
2321 {
2322 struct bp_location **locp = NULL, **loc2p;
2323 int null_condition_or_parse_error = 0;
2324 int modified = bl->needs_update;
2325 struct bp_location *loc;
2326
2327 /* Release conditions left over from a previous insert. */
2328 bl->target_info.conditions.clear ();
2329
2330 /* This is only meaningful if the target is
2331 evaluating conditions and if the user has
2332 opted for condition evaluation on the target's
2333 side. */
2334 if (gdb_evaluates_breakpoint_condition_p ()
2335 || !target_supports_evaluation_of_breakpoint_conditions ())
2336 return;
2337
2338 /* Do a first pass to check for locations with no assigned
2339 conditions or conditions that fail to parse to a valid agent expression
2340 bytecode. If any of these happen, then it's no use to send conditions
2341 to the target since this location will always trigger and generate a
2342 response back to GDB. */
2343 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2344 {
2345 loc = (*loc2p);
2346 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2347 {
2348 if (modified)
2349 {
2350 /* Re-parse the conditions since something changed. In that
2351 case we already freed the condition bytecodes (see
2352 force_breakpoint_reinsertion). We just
2353 need to parse the condition to bytecodes again. */
2354 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2355 loc->cond.get ());
2356 }
2357
2358 /* If we have a NULL bytecode expression, it means something
2359 went wrong or we have a null condition expression. */
2360 if (!loc->cond_bytecode)
2361 {
2362 null_condition_or_parse_error = 1;
2363 break;
2364 }
2365 }
2366 }
2367
2368 /* If any of these happened, it means we will have to evaluate the conditions
2369 for the location's address on gdb's side. It is no use keeping bytecodes
2370 for all the other duplicate locations, thus we free all of them here.
2371
2372 This is so we have a finer control over which locations' conditions are
2373 being evaluated by GDB or the remote stub. */
2374 if (null_condition_or_parse_error)
2375 {
2376 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2377 {
2378 loc = (*loc2p);
2379 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2380 {
2381 /* Only go as far as the first NULL bytecode is
2382 located. */
2383 if (!loc->cond_bytecode)
2384 return;
2385
2386 loc->cond_bytecode.reset ();
2387 }
2388 }
2389 }
2390
2391 /* No NULL conditions or failed bytecode generation. Build a condition list
2392 for this location's address. */
2393 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2394 {
2395 loc = (*loc2p);
2396 if (loc->cond
2397 && is_breakpoint (loc->owner)
2398 && loc->pspace->num == bl->pspace->num
2399 && loc->owner->enable_state == bp_enabled
2400 && loc->enabled)
2401 {
2402 /* Add the condition to the vector. This will be used later
2403 to send the conditions to the target. */
2404 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2405 }
2406 }
2407
2408 return;
2409 }
2410
2411 /* Parses a command described by string CMD into an agent expression
2412 bytecode suitable for evaluation by the bytecode interpreter.
2413 Return NULL if there was any error during parsing. */
2414
2415 static agent_expr_up
2416 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2417 {
2418 struct cleanup *old_cleanups = 0;
2419 struct expression **argvec;
2420 const char *cmdrest;
2421 const char *format_start, *format_end;
2422 struct format_piece *fpieces;
2423 int nargs;
2424 struct gdbarch *gdbarch = get_current_arch ();
2425
2426 if (cmd == NULL)
2427 return NULL;
2428
2429 cmdrest = cmd;
2430
2431 if (*cmdrest == ',')
2432 ++cmdrest;
2433 cmdrest = skip_spaces_const (cmdrest);
2434
2435 if (*cmdrest++ != '"')
2436 error (_("No format string following the location"));
2437
2438 format_start = cmdrest;
2439
2440 fpieces = parse_format_string (&cmdrest);
2441
2442 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2443
2444 format_end = cmdrest;
2445
2446 if (*cmdrest++ != '"')
2447 error (_("Bad format string, non-terminated '\"'."));
2448
2449 cmdrest = skip_spaces_const (cmdrest);
2450
2451 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2452 error (_("Invalid argument syntax"));
2453
2454 if (*cmdrest == ',')
2455 cmdrest++;
2456 cmdrest = skip_spaces_const (cmdrest);
2457
2458 /* For each argument, make an expression. */
2459
2460 argvec = (struct expression **) alloca (strlen (cmd)
2461 * sizeof (struct expression *));
2462
2463 nargs = 0;
2464 while (*cmdrest != '\0')
2465 {
2466 const char *cmd1;
2467
2468 cmd1 = cmdrest;
2469 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2470 argvec[nargs++] = expr.release ();
2471 cmdrest = cmd1;
2472 if (*cmdrest == ',')
2473 ++cmdrest;
2474 }
2475
2476 agent_expr_up aexpr;
2477
2478 /* We don't want to stop processing, so catch any errors
2479 that may show up. */
2480 TRY
2481 {
2482 aexpr = gen_printf (scope, gdbarch, 0, 0,
2483 format_start, format_end - format_start,
2484 fpieces, nargs, argvec);
2485 }
2486 CATCH (ex, RETURN_MASK_ERROR)
2487 {
2488 /* If we got here, it means the command could not be parsed to a valid
2489 bytecode expression and thus can't be evaluated on the target's side.
2490 It's no use iterating through the other commands. */
2491 }
2492 END_CATCH
2493
2494 do_cleanups (old_cleanups);
2495
2496 /* We have a valid agent expression, return it. */
2497 return aexpr;
2498 }
2499
2500 /* Based on location BL, create a list of breakpoint commands to be
2501 passed on to the target. If we have duplicated locations with
2502 different commands, we will add any such to the list. */
2503
2504 static void
2505 build_target_command_list (struct bp_location *bl)
2506 {
2507 struct bp_location **locp = NULL, **loc2p;
2508 int null_command_or_parse_error = 0;
2509 int modified = bl->needs_update;
2510 struct bp_location *loc;
2511
2512 /* Clear commands left over from a previous insert. */
2513 bl->target_info.tcommands.clear ();
2514
2515 if (!target_can_run_breakpoint_commands ())
2516 return;
2517
2518 /* For now, limit to agent-style dprintf breakpoints. */
2519 if (dprintf_style != dprintf_style_agent)
2520 return;
2521
2522 /* For now, if we have any duplicate location that isn't a dprintf,
2523 don't install the target-side commands, as that would make the
2524 breakpoint not be reported to the core, and we'd lose
2525 control. */
2526 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2527 {
2528 loc = (*loc2p);
2529 if (is_breakpoint (loc->owner)
2530 && loc->pspace->num == bl->pspace->num
2531 && loc->owner->type != bp_dprintf)
2532 return;
2533 }
2534
2535 /* Do a first pass to check for locations with no assigned
2536 conditions or conditions that fail to parse to a valid agent expression
2537 bytecode. If any of these happen, then it's no use to send conditions
2538 to the target since this location will always trigger and generate a
2539 response back to GDB. */
2540 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2541 {
2542 loc = (*loc2p);
2543 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2544 {
2545 if (modified)
2546 {
2547 /* Re-parse the commands since something changed. In that
2548 case we already freed the command bytecodes (see
2549 force_breakpoint_reinsertion). We just
2550 need to parse the command to bytecodes again. */
2551 loc->cmd_bytecode
2552 = parse_cmd_to_aexpr (bl->address,
2553 loc->owner->extra_string);
2554 }
2555
2556 /* If we have a NULL bytecode expression, it means something
2557 went wrong or we have a null command expression. */
2558 if (!loc->cmd_bytecode)
2559 {
2560 null_command_or_parse_error = 1;
2561 break;
2562 }
2563 }
2564 }
2565
2566 /* If anything failed, then we're not doing target-side commands,
2567 and so clean up. */
2568 if (null_command_or_parse_error)
2569 {
2570 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2571 {
2572 loc = (*loc2p);
2573 if (is_breakpoint (loc->owner)
2574 && loc->pspace->num == bl->pspace->num)
2575 {
2576 /* Only go as far as the first NULL bytecode is
2577 located. */
2578 if (loc->cmd_bytecode == NULL)
2579 return;
2580
2581 loc->cmd_bytecode.reset ();
2582 }
2583 }
2584 }
2585
2586 /* No NULL commands or failed bytecode generation. Build a command list
2587 for this location's address. */
2588 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2589 {
2590 loc = (*loc2p);
2591 if (loc->owner->extra_string
2592 && is_breakpoint (loc->owner)
2593 && loc->pspace->num == bl->pspace->num
2594 && loc->owner->enable_state == bp_enabled
2595 && loc->enabled)
2596 {
2597 /* Add the command to the vector. This will be used later
2598 to send the commands to the target. */
2599 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2600 }
2601 }
2602
2603 bl->target_info.persist = 0;
2604 /* Maybe flag this location as persistent. */
2605 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2606 bl->target_info.persist = 1;
2607 }
2608
2609 /* Return the kind of breakpoint on address *ADDR. Get the kind
2610 of breakpoint according to ADDR except single-step breakpoint.
2611 Get the kind of single-step breakpoint according to the current
2612 registers state. */
2613
2614 static int
2615 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2616 {
2617 if (bl->owner->type == bp_single_step)
2618 {
2619 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2620 struct regcache *regcache;
2621
2622 regcache = get_thread_regcache (thr->ptid);
2623
2624 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2625 regcache, addr);
2626 }
2627 else
2628 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2629 }
2630
2631 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2632 location. Any error messages are printed to TMP_ERROR_STREAM; and
2633 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2634 Returns 0 for success, 1 if the bp_location type is not supported or
2635 -1 for failure.
2636
2637 NOTE drow/2003-09-09: This routine could be broken down to an
2638 object-style method for each breakpoint or catchpoint type. */
2639 static int
2640 insert_bp_location (struct bp_location *bl,
2641 struct ui_file *tmp_error_stream,
2642 int *disabled_breaks,
2643 int *hw_breakpoint_error,
2644 int *hw_bp_error_explained_already)
2645 {
2646 enum errors bp_err = GDB_NO_ERROR;
2647 const char *bp_err_message = NULL;
2648
2649 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2650 return 0;
2651
2652 /* Note we don't initialize bl->target_info, as that wipes out
2653 the breakpoint location's shadow_contents if the breakpoint
2654 is still inserted at that location. This in turn breaks
2655 target_read_memory which depends on these buffers when
2656 a memory read is requested at the breakpoint location:
2657 Once the target_info has been wiped, we fail to see that
2658 we have a breakpoint inserted at that address and thus
2659 read the breakpoint instead of returning the data saved in
2660 the breakpoint location's shadow contents. */
2661 bl->target_info.reqstd_address = bl->address;
2662 bl->target_info.placed_address_space = bl->pspace->aspace;
2663 bl->target_info.length = bl->length;
2664
2665 /* When working with target-side conditions, we must pass all the conditions
2666 for the same breakpoint address down to the target since GDB will not
2667 insert those locations. With a list of breakpoint conditions, the target
2668 can decide when to stop and notify GDB. */
2669
2670 if (is_breakpoint (bl->owner))
2671 {
2672 build_target_condition_list (bl);
2673 build_target_command_list (bl);
2674 /* Reset the modification marker. */
2675 bl->needs_update = 0;
2676 }
2677
2678 if (bl->loc_type == bp_loc_software_breakpoint
2679 || bl->loc_type == bp_loc_hardware_breakpoint)
2680 {
2681 if (bl->owner->type != bp_hardware_breakpoint)
2682 {
2683 /* If the explicitly specified breakpoint type
2684 is not hardware breakpoint, check the memory map to see
2685 if the breakpoint address is in read only memory or not.
2686
2687 Two important cases are:
2688 - location type is not hardware breakpoint, memory
2689 is readonly. We change the type of the location to
2690 hardware breakpoint.
2691 - location type is hardware breakpoint, memory is
2692 read-write. This means we've previously made the
2693 location hardware one, but then the memory map changed,
2694 so we undo.
2695
2696 When breakpoints are removed, remove_breakpoints will use
2697 location types we've just set here, the only possible
2698 problem is that memory map has changed during running
2699 program, but it's not going to work anyway with current
2700 gdb. */
2701 struct mem_region *mr
2702 = lookup_mem_region (bl->target_info.reqstd_address);
2703
2704 if (mr)
2705 {
2706 if (automatic_hardware_breakpoints)
2707 {
2708 enum bp_loc_type new_type;
2709
2710 if (mr->attrib.mode != MEM_RW)
2711 new_type = bp_loc_hardware_breakpoint;
2712 else
2713 new_type = bp_loc_software_breakpoint;
2714
2715 if (new_type != bl->loc_type)
2716 {
2717 static int said = 0;
2718
2719 bl->loc_type = new_type;
2720 if (!said)
2721 {
2722 fprintf_filtered (gdb_stdout,
2723 _("Note: automatically using "
2724 "hardware breakpoints for "
2725 "read-only addresses.\n"));
2726 said = 1;
2727 }
2728 }
2729 }
2730 else if (bl->loc_type == bp_loc_software_breakpoint
2731 && mr->attrib.mode != MEM_RW)
2732 {
2733 fprintf_unfiltered (tmp_error_stream,
2734 _("Cannot insert breakpoint %d.\n"
2735 "Cannot set software breakpoint "
2736 "at read-only address %s\n"),
2737 bl->owner->number,
2738 paddress (bl->gdbarch, bl->address));
2739 return 1;
2740 }
2741 }
2742 }
2743
2744 /* First check to see if we have to handle an overlay. */
2745 if (overlay_debugging == ovly_off
2746 || bl->section == NULL
2747 || !(section_is_overlay (bl->section)))
2748 {
2749 /* No overlay handling: just set the breakpoint. */
2750 TRY
2751 {
2752 int val;
2753
2754 val = bl->owner->ops->insert_location (bl);
2755 if (val)
2756 bp_err = GENERIC_ERROR;
2757 }
2758 CATCH (e, RETURN_MASK_ALL)
2759 {
2760 bp_err = e.error;
2761 bp_err_message = e.message;
2762 }
2763 END_CATCH
2764 }
2765 else
2766 {
2767 /* This breakpoint is in an overlay section.
2768 Shall we set a breakpoint at the LMA? */
2769 if (!overlay_events_enabled)
2770 {
2771 /* Yes -- overlay event support is not active,
2772 so we must try to set a breakpoint at the LMA.
2773 This will not work for a hardware breakpoint. */
2774 if (bl->loc_type == bp_loc_hardware_breakpoint)
2775 warning (_("hardware breakpoint %d not supported in overlay!"),
2776 bl->owner->number);
2777 else
2778 {
2779 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2780 bl->section);
2781 /* Set a software (trap) breakpoint at the LMA. */
2782 bl->overlay_target_info = bl->target_info;
2783 bl->overlay_target_info.reqstd_address = addr;
2784
2785 /* No overlay handling: just set the breakpoint. */
2786 TRY
2787 {
2788 int val;
2789
2790 bl->overlay_target_info.kind
2791 = breakpoint_kind (bl, &addr);
2792 bl->overlay_target_info.placed_address = addr;
2793 val = target_insert_breakpoint (bl->gdbarch,
2794 &bl->overlay_target_info);
2795 if (val)
2796 bp_err = GENERIC_ERROR;
2797 }
2798 CATCH (e, RETURN_MASK_ALL)
2799 {
2800 bp_err = e.error;
2801 bp_err_message = e.message;
2802 }
2803 END_CATCH
2804
2805 if (bp_err != GDB_NO_ERROR)
2806 fprintf_unfiltered (tmp_error_stream,
2807 "Overlay breakpoint %d "
2808 "failed: in ROM?\n",
2809 bl->owner->number);
2810 }
2811 }
2812 /* Shall we set a breakpoint at the VMA? */
2813 if (section_is_mapped (bl->section))
2814 {
2815 /* Yes. This overlay section is mapped into memory. */
2816 TRY
2817 {
2818 int val;
2819
2820 val = bl->owner->ops->insert_location (bl);
2821 if (val)
2822 bp_err = GENERIC_ERROR;
2823 }
2824 CATCH (e, RETURN_MASK_ALL)
2825 {
2826 bp_err = e.error;
2827 bp_err_message = e.message;
2828 }
2829 END_CATCH
2830 }
2831 else
2832 {
2833 /* No. This breakpoint will not be inserted.
2834 No error, but do not mark the bp as 'inserted'. */
2835 return 0;
2836 }
2837 }
2838
2839 if (bp_err != GDB_NO_ERROR)
2840 {
2841 /* Can't set the breakpoint. */
2842
2843 /* In some cases, we might not be able to insert a
2844 breakpoint in a shared library that has already been
2845 removed, but we have not yet processed the shlib unload
2846 event. Unfortunately, some targets that implement
2847 breakpoint insertion themselves can't tell why the
2848 breakpoint insertion failed (e.g., the remote target
2849 doesn't define error codes), so we must treat generic
2850 errors as memory errors. */
2851 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2852 && bl->loc_type == bp_loc_software_breakpoint
2853 && (solib_name_from_address (bl->pspace, bl->address)
2854 || shared_objfile_contains_address_p (bl->pspace,
2855 bl->address)))
2856 {
2857 /* See also: disable_breakpoints_in_shlibs. */
2858 bl->shlib_disabled = 1;
2859 observer_notify_breakpoint_modified (bl->owner);
2860 if (!*disabled_breaks)
2861 {
2862 fprintf_unfiltered (tmp_error_stream,
2863 "Cannot insert breakpoint %d.\n",
2864 bl->owner->number);
2865 fprintf_unfiltered (tmp_error_stream,
2866 "Temporarily disabling shared "
2867 "library breakpoints:\n");
2868 }
2869 *disabled_breaks = 1;
2870 fprintf_unfiltered (tmp_error_stream,
2871 "breakpoint #%d\n", bl->owner->number);
2872 return 0;
2873 }
2874 else
2875 {
2876 if (bl->loc_type == bp_loc_hardware_breakpoint)
2877 {
2878 *hw_breakpoint_error = 1;
2879 *hw_bp_error_explained_already = bp_err_message != NULL;
2880 fprintf_unfiltered (tmp_error_stream,
2881 "Cannot insert hardware breakpoint %d%s",
2882 bl->owner->number, bp_err_message ? ":" : ".\n");
2883 if (bp_err_message != NULL)
2884 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2885 }
2886 else
2887 {
2888 if (bp_err_message == NULL)
2889 {
2890 std::string message
2891 = memory_error_message (TARGET_XFER_E_IO,
2892 bl->gdbarch, bl->address);
2893
2894 fprintf_unfiltered (tmp_error_stream,
2895 "Cannot insert breakpoint %d.\n"
2896 "%s\n",
2897 bl->owner->number, message.c_str ());
2898 }
2899 else
2900 {
2901 fprintf_unfiltered (tmp_error_stream,
2902 "Cannot insert breakpoint %d: %s\n",
2903 bl->owner->number,
2904 bp_err_message);
2905 }
2906 }
2907 return 1;
2908
2909 }
2910 }
2911 else
2912 bl->inserted = 1;
2913
2914 return 0;
2915 }
2916
2917 else if (bl->loc_type == bp_loc_hardware_watchpoint
2918 /* NOTE drow/2003-09-08: This state only exists for removing
2919 watchpoints. It's not clear that it's necessary... */
2920 && bl->owner->disposition != disp_del_at_next_stop)
2921 {
2922 int val;
2923
2924 gdb_assert (bl->owner->ops != NULL
2925 && bl->owner->ops->insert_location != NULL);
2926
2927 val = bl->owner->ops->insert_location (bl);
2928
2929 /* If trying to set a read-watchpoint, and it turns out it's not
2930 supported, try emulating one with an access watchpoint. */
2931 if (val == 1 && bl->watchpoint_type == hw_read)
2932 {
2933 struct bp_location *loc, **loc_temp;
2934
2935 /* But don't try to insert it, if there's already another
2936 hw_access location that would be considered a duplicate
2937 of this one. */
2938 ALL_BP_LOCATIONS (loc, loc_temp)
2939 if (loc != bl
2940 && loc->watchpoint_type == hw_access
2941 && watchpoint_locations_match (bl, loc))
2942 {
2943 bl->duplicate = 1;
2944 bl->inserted = 1;
2945 bl->target_info = loc->target_info;
2946 bl->watchpoint_type = hw_access;
2947 val = 0;
2948 break;
2949 }
2950
2951 if (val == 1)
2952 {
2953 bl->watchpoint_type = hw_access;
2954 val = bl->owner->ops->insert_location (bl);
2955
2956 if (val)
2957 /* Back to the original value. */
2958 bl->watchpoint_type = hw_read;
2959 }
2960 }
2961
2962 bl->inserted = (val == 0);
2963 }
2964
2965 else if (bl->owner->type == bp_catchpoint)
2966 {
2967 int val;
2968
2969 gdb_assert (bl->owner->ops != NULL
2970 && bl->owner->ops->insert_location != NULL);
2971
2972 val = bl->owner->ops->insert_location (bl);
2973 if (val)
2974 {
2975 bl->owner->enable_state = bp_disabled;
2976
2977 if (val == 1)
2978 warning (_("\
2979 Error inserting catchpoint %d: Your system does not support this type\n\
2980 of catchpoint."), bl->owner->number);
2981 else
2982 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2983 }
2984
2985 bl->inserted = (val == 0);
2986
2987 /* We've already printed an error message if there was a problem
2988 inserting this catchpoint, and we've disabled the catchpoint,
2989 so just return success. */
2990 return 0;
2991 }
2992
2993 return 0;
2994 }
2995
2996 /* This function is called when program space PSPACE is about to be
2997 deleted. It takes care of updating breakpoints to not reference
2998 PSPACE anymore. */
2999
3000 void
3001 breakpoint_program_space_exit (struct program_space *pspace)
3002 {
3003 struct breakpoint *b, *b_temp;
3004 struct bp_location *loc, **loc_temp;
3005
3006 /* Remove any breakpoint that was set through this program space. */
3007 ALL_BREAKPOINTS_SAFE (b, b_temp)
3008 {
3009 if (b->pspace == pspace)
3010 delete_breakpoint (b);
3011 }
3012
3013 /* Breakpoints set through other program spaces could have locations
3014 bound to PSPACE as well. Remove those. */
3015 ALL_BP_LOCATIONS (loc, loc_temp)
3016 {
3017 struct bp_location *tmp;
3018
3019 if (loc->pspace == pspace)
3020 {
3021 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3022 if (loc->owner->loc == loc)
3023 loc->owner->loc = loc->next;
3024 else
3025 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3026 if (tmp->next == loc)
3027 {
3028 tmp->next = loc->next;
3029 break;
3030 }
3031 }
3032 }
3033
3034 /* Now update the global location list to permanently delete the
3035 removed locations above. */
3036 update_global_location_list (UGLL_DONT_INSERT);
3037 }
3038
3039 /* Make sure all breakpoints are inserted in inferior.
3040 Throws exception on any error.
3041 A breakpoint that is already inserted won't be inserted
3042 again, so calling this function twice is safe. */
3043 void
3044 insert_breakpoints (void)
3045 {
3046 struct breakpoint *bpt;
3047
3048 ALL_BREAKPOINTS (bpt)
3049 if (is_hardware_watchpoint (bpt))
3050 {
3051 struct watchpoint *w = (struct watchpoint *) bpt;
3052
3053 update_watchpoint (w, 0 /* don't reparse. */);
3054 }
3055
3056 /* Updating watchpoints creates new locations, so update the global
3057 location list. Explicitly tell ugll to insert locations and
3058 ignore breakpoints_always_inserted_mode. */
3059 update_global_location_list (UGLL_INSERT);
3060 }
3061
3062 /* Invoke CALLBACK for each of bp_location. */
3063
3064 void
3065 iterate_over_bp_locations (walk_bp_location_callback callback)
3066 {
3067 struct bp_location *loc, **loc_tmp;
3068
3069 ALL_BP_LOCATIONS (loc, loc_tmp)
3070 {
3071 callback (loc, NULL);
3072 }
3073 }
3074
3075 /* This is used when we need to synch breakpoint conditions between GDB and the
3076 target. It is the case with deleting and disabling of breakpoints when using
3077 always-inserted mode. */
3078
3079 static void
3080 update_inserted_breakpoint_locations (void)
3081 {
3082 struct bp_location *bl, **blp_tmp;
3083 int error_flag = 0;
3084 int val = 0;
3085 int disabled_breaks = 0;
3086 int hw_breakpoint_error = 0;
3087 int hw_bp_details_reported = 0;
3088
3089 string_file tmp_error_stream;
3090
3091 /* Explicitly mark the warning -- this will only be printed if
3092 there was an error. */
3093 tmp_error_stream.puts ("Warning:\n");
3094
3095 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3096
3097 ALL_BP_LOCATIONS (bl, blp_tmp)
3098 {
3099 /* We only want to update software breakpoints and hardware
3100 breakpoints. */
3101 if (!is_breakpoint (bl->owner))
3102 continue;
3103
3104 /* We only want to update locations that are already inserted
3105 and need updating. This is to avoid unwanted insertion during
3106 deletion of breakpoints. */
3107 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3108 continue;
3109
3110 switch_to_program_space_and_thread (bl->pspace);
3111
3112 /* For targets that support global breakpoints, there's no need
3113 to select an inferior to insert breakpoint to. In fact, even
3114 if we aren't attached to any process yet, we should still
3115 insert breakpoints. */
3116 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3117 && ptid_equal (inferior_ptid, null_ptid))
3118 continue;
3119
3120 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3121 &hw_breakpoint_error, &hw_bp_details_reported);
3122 if (val)
3123 error_flag = val;
3124 }
3125
3126 if (error_flag)
3127 {
3128 target_terminal_ours_for_output ();
3129 error_stream (tmp_error_stream);
3130 }
3131 }
3132
3133 /* Used when starting or continuing the program. */
3134
3135 static void
3136 insert_breakpoint_locations (void)
3137 {
3138 struct breakpoint *bpt;
3139 struct bp_location *bl, **blp_tmp;
3140 int error_flag = 0;
3141 int val = 0;
3142 int disabled_breaks = 0;
3143 int hw_breakpoint_error = 0;
3144 int hw_bp_error_explained_already = 0;
3145
3146 string_file tmp_error_stream;
3147
3148 /* Explicitly mark the warning -- this will only be printed if
3149 there was an error. */
3150 tmp_error_stream.puts ("Warning:\n");
3151
3152 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3153
3154 ALL_BP_LOCATIONS (bl, blp_tmp)
3155 {
3156 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3157 continue;
3158
3159 /* There is no point inserting thread-specific breakpoints if
3160 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3161 has BL->OWNER always non-NULL. */
3162 if (bl->owner->thread != -1
3163 && !valid_global_thread_id (bl->owner->thread))
3164 continue;
3165
3166 switch_to_program_space_and_thread (bl->pspace);
3167
3168 /* For targets that support global breakpoints, there's no need
3169 to select an inferior to insert breakpoint to. In fact, even
3170 if we aren't attached to any process yet, we should still
3171 insert breakpoints. */
3172 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3173 && ptid_equal (inferior_ptid, null_ptid))
3174 continue;
3175
3176 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3177 &hw_breakpoint_error, &hw_bp_error_explained_already);
3178 if (val)
3179 error_flag = val;
3180 }
3181
3182 /* If we failed to insert all locations of a watchpoint, remove
3183 them, as half-inserted watchpoint is of limited use. */
3184 ALL_BREAKPOINTS (bpt)
3185 {
3186 int some_failed = 0;
3187 struct bp_location *loc;
3188
3189 if (!is_hardware_watchpoint (bpt))
3190 continue;
3191
3192 if (!breakpoint_enabled (bpt))
3193 continue;
3194
3195 if (bpt->disposition == disp_del_at_next_stop)
3196 continue;
3197
3198 for (loc = bpt->loc; loc; loc = loc->next)
3199 if (!loc->inserted && should_be_inserted (loc))
3200 {
3201 some_failed = 1;
3202 break;
3203 }
3204 if (some_failed)
3205 {
3206 for (loc = bpt->loc; loc; loc = loc->next)
3207 if (loc->inserted)
3208 remove_breakpoint (loc);
3209
3210 hw_breakpoint_error = 1;
3211 tmp_error_stream.printf ("Could not insert "
3212 "hardware watchpoint %d.\n",
3213 bpt->number);
3214 error_flag = -1;
3215 }
3216 }
3217
3218 if (error_flag)
3219 {
3220 /* If a hardware breakpoint or watchpoint was inserted, add a
3221 message about possibly exhausted resources. */
3222 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3223 {
3224 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3225 You may have requested too many hardware breakpoints/watchpoints.\n");
3226 }
3227 target_terminal_ours_for_output ();
3228 error_stream (tmp_error_stream);
3229 }
3230 }
3231
3232 /* Used when the program stops.
3233 Returns zero if successful, or non-zero if there was a problem
3234 removing a breakpoint location. */
3235
3236 int
3237 remove_breakpoints (void)
3238 {
3239 struct bp_location *bl, **blp_tmp;
3240 int val = 0;
3241
3242 ALL_BP_LOCATIONS (bl, blp_tmp)
3243 {
3244 if (bl->inserted && !is_tracepoint (bl->owner))
3245 val |= remove_breakpoint (bl);
3246 }
3247 return val;
3248 }
3249
3250 /* When a thread exits, remove breakpoints that are related to
3251 that thread. */
3252
3253 static void
3254 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3255 {
3256 struct breakpoint *b, *b_tmp;
3257
3258 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3259 {
3260 if (b->thread == tp->global_num && user_breakpoint_p (b))
3261 {
3262 b->disposition = disp_del_at_next_stop;
3263
3264 printf_filtered (_("\
3265 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3266 b->number, print_thread_id (tp));
3267
3268 /* Hide it from the user. */
3269 b->number = 0;
3270 }
3271 }
3272 }
3273
3274 /* Remove breakpoints of process PID. */
3275
3276 int
3277 remove_breakpoints_pid (int pid)
3278 {
3279 struct bp_location *bl, **blp_tmp;
3280 int val;
3281 struct inferior *inf = find_inferior_pid (pid);
3282
3283 ALL_BP_LOCATIONS (bl, blp_tmp)
3284 {
3285 if (bl->pspace != inf->pspace)
3286 continue;
3287
3288 if (bl->inserted && !bl->target_info.persist)
3289 {
3290 val = remove_breakpoint (bl);
3291 if (val != 0)
3292 return val;
3293 }
3294 }
3295 return 0;
3296 }
3297
3298 int
3299 reattach_breakpoints (int pid)
3300 {
3301 struct bp_location *bl, **blp_tmp;
3302 int val;
3303 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3304 struct inferior *inf;
3305 struct thread_info *tp;
3306
3307 tp = any_live_thread_of_process (pid);
3308 if (tp == NULL)
3309 return 1;
3310
3311 inf = find_inferior_pid (pid);
3312
3313 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3314 inferior_ptid = tp->ptid;
3315
3316 string_file tmp_error_stream;
3317
3318 ALL_BP_LOCATIONS (bl, blp_tmp)
3319 {
3320 if (bl->pspace != inf->pspace)
3321 continue;
3322
3323 if (bl->inserted)
3324 {
3325 bl->inserted = 0;
3326 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3327 if (val != 0)
3328 return val;
3329 }
3330 }
3331 return 0;
3332 }
3333
3334 static int internal_breakpoint_number = -1;
3335
3336 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3337 If INTERNAL is non-zero, the breakpoint number will be populated
3338 from internal_breakpoint_number and that variable decremented.
3339 Otherwise the breakpoint number will be populated from
3340 breakpoint_count and that value incremented. Internal breakpoints
3341 do not set the internal var bpnum. */
3342 static void
3343 set_breakpoint_number (int internal, struct breakpoint *b)
3344 {
3345 if (internal)
3346 b->number = internal_breakpoint_number--;
3347 else
3348 {
3349 set_breakpoint_count (breakpoint_count + 1);
3350 b->number = breakpoint_count;
3351 }
3352 }
3353
3354 static struct breakpoint *
3355 create_internal_breakpoint (struct gdbarch *gdbarch,
3356 CORE_ADDR address, enum bptype type,
3357 const struct breakpoint_ops *ops)
3358 {
3359 symtab_and_line sal;
3360 sal.pc = address;
3361 sal.section = find_pc_overlay (sal.pc);
3362 sal.pspace = current_program_space;
3363
3364 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3365 b->number = internal_breakpoint_number--;
3366 b->disposition = disp_donttouch;
3367
3368 return b;
3369 }
3370
3371 static const char *const longjmp_names[] =
3372 {
3373 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3374 };
3375 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3376
3377 /* Per-objfile data private to breakpoint.c. */
3378 struct breakpoint_objfile_data
3379 {
3380 /* Minimal symbol for "_ovly_debug_event" (if any). */
3381 struct bound_minimal_symbol overlay_msym;
3382
3383 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3384 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3385
3386 /* True if we have looked for longjmp probes. */
3387 int longjmp_searched;
3388
3389 /* SystemTap probe points for longjmp (if any). */
3390 VEC (probe_p) *longjmp_probes;
3391
3392 /* Minimal symbol for "std::terminate()" (if any). */
3393 struct bound_minimal_symbol terminate_msym;
3394
3395 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3396 struct bound_minimal_symbol exception_msym;
3397
3398 /* True if we have looked for exception probes. */
3399 int exception_searched;
3400
3401 /* SystemTap probe points for unwinding (if any). */
3402 VEC (probe_p) *exception_probes;
3403 };
3404
3405 static const struct objfile_data *breakpoint_objfile_key;
3406
3407 /* Minimal symbol not found sentinel. */
3408 static struct minimal_symbol msym_not_found;
3409
3410 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3411
3412 static int
3413 msym_not_found_p (const struct minimal_symbol *msym)
3414 {
3415 return msym == &msym_not_found;
3416 }
3417
3418 /* Return per-objfile data needed by breakpoint.c.
3419 Allocate the data if necessary. */
3420
3421 static struct breakpoint_objfile_data *
3422 get_breakpoint_objfile_data (struct objfile *objfile)
3423 {
3424 struct breakpoint_objfile_data *bp_objfile_data;
3425
3426 bp_objfile_data = ((struct breakpoint_objfile_data *)
3427 objfile_data (objfile, breakpoint_objfile_key));
3428 if (bp_objfile_data == NULL)
3429 {
3430 bp_objfile_data =
3431 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3432
3433 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3434 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3435 }
3436 return bp_objfile_data;
3437 }
3438
3439 static void
3440 free_breakpoint_probes (struct objfile *obj, void *data)
3441 {
3442 struct breakpoint_objfile_data *bp_objfile_data
3443 = (struct breakpoint_objfile_data *) data;
3444
3445 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3446 VEC_free (probe_p, bp_objfile_data->exception_probes);
3447 }
3448
3449 static void
3450 create_overlay_event_breakpoint (void)
3451 {
3452 struct objfile *objfile;
3453 const char *const func_name = "_ovly_debug_event";
3454
3455 ALL_OBJFILES (objfile)
3456 {
3457 struct breakpoint *b;
3458 struct breakpoint_objfile_data *bp_objfile_data;
3459 CORE_ADDR addr;
3460 struct explicit_location explicit_loc;
3461
3462 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3463
3464 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3465 continue;
3466
3467 if (bp_objfile_data->overlay_msym.minsym == NULL)
3468 {
3469 struct bound_minimal_symbol m;
3470
3471 m = lookup_minimal_symbol_text (func_name, objfile);
3472 if (m.minsym == NULL)
3473 {
3474 /* Avoid future lookups in this objfile. */
3475 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3476 continue;
3477 }
3478 bp_objfile_data->overlay_msym = m;
3479 }
3480
3481 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3482 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3483 bp_overlay_event,
3484 &internal_breakpoint_ops);
3485 initialize_explicit_location (&explicit_loc);
3486 explicit_loc.function_name = ASTRDUP (func_name);
3487 b->location = new_explicit_location (&explicit_loc);
3488
3489 if (overlay_debugging == ovly_auto)
3490 {
3491 b->enable_state = bp_enabled;
3492 overlay_events_enabled = 1;
3493 }
3494 else
3495 {
3496 b->enable_state = bp_disabled;
3497 overlay_events_enabled = 0;
3498 }
3499 }
3500 }
3501
3502 static void
3503 create_longjmp_master_breakpoint (void)
3504 {
3505 struct program_space *pspace;
3506
3507 scoped_restore_current_program_space restore_pspace;
3508
3509 ALL_PSPACES (pspace)
3510 {
3511 struct objfile *objfile;
3512
3513 set_current_program_space (pspace);
3514
3515 ALL_OBJFILES (objfile)
3516 {
3517 int i;
3518 struct gdbarch *gdbarch;
3519 struct breakpoint_objfile_data *bp_objfile_data;
3520
3521 gdbarch = get_objfile_arch (objfile);
3522
3523 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3524
3525 if (!bp_objfile_data->longjmp_searched)
3526 {
3527 VEC (probe_p) *ret;
3528
3529 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3530 if (ret != NULL)
3531 {
3532 /* We are only interested in checking one element. */
3533 struct probe *p = VEC_index (probe_p, ret, 0);
3534
3535 if (!can_evaluate_probe_arguments (p))
3536 {
3537 /* We cannot use the probe interface here, because it does
3538 not know how to evaluate arguments. */
3539 VEC_free (probe_p, ret);
3540 ret = NULL;
3541 }
3542 }
3543 bp_objfile_data->longjmp_probes = ret;
3544 bp_objfile_data->longjmp_searched = 1;
3545 }
3546
3547 if (bp_objfile_data->longjmp_probes != NULL)
3548 {
3549 int i;
3550 struct probe *probe;
3551 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3552
3553 for (i = 0;
3554 VEC_iterate (probe_p,
3555 bp_objfile_data->longjmp_probes,
3556 i, probe);
3557 ++i)
3558 {
3559 struct breakpoint *b;
3560
3561 b = create_internal_breakpoint (gdbarch,
3562 get_probe_address (probe,
3563 objfile),
3564 bp_longjmp_master,
3565 &internal_breakpoint_ops);
3566 b->location = new_probe_location ("-probe-stap libc:longjmp");
3567 b->enable_state = bp_disabled;
3568 }
3569
3570 continue;
3571 }
3572
3573 if (!gdbarch_get_longjmp_target_p (gdbarch))
3574 continue;
3575
3576 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3577 {
3578 struct breakpoint *b;
3579 const char *func_name;
3580 CORE_ADDR addr;
3581 struct explicit_location explicit_loc;
3582
3583 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3584 continue;
3585
3586 func_name = longjmp_names[i];
3587 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3588 {
3589 struct bound_minimal_symbol m;
3590
3591 m = lookup_minimal_symbol_text (func_name, objfile);
3592 if (m.minsym == NULL)
3593 {
3594 /* Prevent future lookups in this objfile. */
3595 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3596 continue;
3597 }
3598 bp_objfile_data->longjmp_msym[i] = m;
3599 }
3600
3601 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3602 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3603 &internal_breakpoint_ops);
3604 initialize_explicit_location (&explicit_loc);
3605 explicit_loc.function_name = ASTRDUP (func_name);
3606 b->location = new_explicit_location (&explicit_loc);
3607 b->enable_state = bp_disabled;
3608 }
3609 }
3610 }
3611 }
3612
3613 /* Create a master std::terminate breakpoint. */
3614 static void
3615 create_std_terminate_master_breakpoint (void)
3616 {
3617 struct program_space *pspace;
3618 const char *const func_name = "std::terminate()";
3619
3620 scoped_restore_current_program_space restore_pspace;
3621
3622 ALL_PSPACES (pspace)
3623 {
3624 struct objfile *objfile;
3625 CORE_ADDR addr;
3626
3627 set_current_program_space (pspace);
3628
3629 ALL_OBJFILES (objfile)
3630 {
3631 struct breakpoint *b;
3632 struct breakpoint_objfile_data *bp_objfile_data;
3633 struct explicit_location explicit_loc;
3634
3635 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3636
3637 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3638 continue;
3639
3640 if (bp_objfile_data->terminate_msym.minsym == NULL)
3641 {
3642 struct bound_minimal_symbol m;
3643
3644 m = lookup_minimal_symbol (func_name, NULL, objfile);
3645 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3646 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3647 {
3648 /* Prevent future lookups in this objfile. */
3649 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3650 continue;
3651 }
3652 bp_objfile_data->terminate_msym = m;
3653 }
3654
3655 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3656 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3657 bp_std_terminate_master,
3658 &internal_breakpoint_ops);
3659 initialize_explicit_location (&explicit_loc);
3660 explicit_loc.function_name = ASTRDUP (func_name);
3661 b->location = new_explicit_location (&explicit_loc);
3662 b->enable_state = bp_disabled;
3663 }
3664 }
3665 }
3666
3667 /* Install a master breakpoint on the unwinder's debug hook. */
3668
3669 static void
3670 create_exception_master_breakpoint (void)
3671 {
3672 struct objfile *objfile;
3673 const char *const func_name = "_Unwind_DebugHook";
3674
3675 ALL_OBJFILES (objfile)
3676 {
3677 struct breakpoint *b;
3678 struct gdbarch *gdbarch;
3679 struct breakpoint_objfile_data *bp_objfile_data;
3680 CORE_ADDR addr;
3681 struct explicit_location explicit_loc;
3682
3683 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3684
3685 /* We prefer the SystemTap probe point if it exists. */
3686 if (!bp_objfile_data->exception_searched)
3687 {
3688 VEC (probe_p) *ret;
3689
3690 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3691
3692 if (ret != NULL)
3693 {
3694 /* We are only interested in checking one element. */
3695 struct probe *p = VEC_index (probe_p, ret, 0);
3696
3697 if (!can_evaluate_probe_arguments (p))
3698 {
3699 /* We cannot use the probe interface here, because it does
3700 not know how to evaluate arguments. */
3701 VEC_free (probe_p, ret);
3702 ret = NULL;
3703 }
3704 }
3705 bp_objfile_data->exception_probes = ret;
3706 bp_objfile_data->exception_searched = 1;
3707 }
3708
3709 if (bp_objfile_data->exception_probes != NULL)
3710 {
3711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3712 int i;
3713 struct probe *probe;
3714
3715 for (i = 0;
3716 VEC_iterate (probe_p,
3717 bp_objfile_data->exception_probes,
3718 i, probe);
3719 ++i)
3720 {
3721 struct breakpoint *b;
3722
3723 b = create_internal_breakpoint (gdbarch,
3724 get_probe_address (probe,
3725 objfile),
3726 bp_exception_master,
3727 &internal_breakpoint_ops);
3728 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3729 b->enable_state = bp_disabled;
3730 }
3731
3732 continue;
3733 }
3734
3735 /* Otherwise, try the hook function. */
3736
3737 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3738 continue;
3739
3740 gdbarch = get_objfile_arch (objfile);
3741
3742 if (bp_objfile_data->exception_msym.minsym == NULL)
3743 {
3744 struct bound_minimal_symbol debug_hook;
3745
3746 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3747 if (debug_hook.minsym == NULL)
3748 {
3749 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3750 continue;
3751 }
3752
3753 bp_objfile_data->exception_msym = debug_hook;
3754 }
3755
3756 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3757 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3758 &current_target);
3759 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3760 &internal_breakpoint_ops);
3761 initialize_explicit_location (&explicit_loc);
3762 explicit_loc.function_name = ASTRDUP (func_name);
3763 b->location = new_explicit_location (&explicit_loc);
3764 b->enable_state = bp_disabled;
3765 }
3766 }
3767
3768 /* Does B have a location spec? */
3769
3770 static int
3771 breakpoint_event_location_empty_p (const struct breakpoint *b)
3772 {
3773 return b->location != NULL && event_location_empty_p (b->location.get ());
3774 }
3775
3776 void
3777 update_breakpoints_after_exec (void)
3778 {
3779 struct breakpoint *b, *b_tmp;
3780 struct bp_location *bploc, **bplocp_tmp;
3781
3782 /* We're about to delete breakpoints from GDB's lists. If the
3783 INSERTED flag is true, GDB will try to lift the breakpoints by
3784 writing the breakpoints' "shadow contents" back into memory. The
3785 "shadow contents" are NOT valid after an exec, so GDB should not
3786 do that. Instead, the target is responsible from marking
3787 breakpoints out as soon as it detects an exec. We don't do that
3788 here instead, because there may be other attempts to delete
3789 breakpoints after detecting an exec and before reaching here. */
3790 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3791 if (bploc->pspace == current_program_space)
3792 gdb_assert (!bploc->inserted);
3793
3794 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3795 {
3796 if (b->pspace != current_program_space)
3797 continue;
3798
3799 /* Solib breakpoints must be explicitly reset after an exec(). */
3800 if (b->type == bp_shlib_event)
3801 {
3802 delete_breakpoint (b);
3803 continue;
3804 }
3805
3806 /* JIT breakpoints must be explicitly reset after an exec(). */
3807 if (b->type == bp_jit_event)
3808 {
3809 delete_breakpoint (b);
3810 continue;
3811 }
3812
3813 /* Thread event breakpoints must be set anew after an exec(),
3814 as must overlay event and longjmp master breakpoints. */
3815 if (b->type == bp_thread_event || b->type == bp_overlay_event
3816 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3817 || b->type == bp_exception_master)
3818 {
3819 delete_breakpoint (b);
3820 continue;
3821 }
3822
3823 /* Step-resume breakpoints are meaningless after an exec(). */
3824 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3825 {
3826 delete_breakpoint (b);
3827 continue;
3828 }
3829
3830 /* Just like single-step breakpoints. */
3831 if (b->type == bp_single_step)
3832 {
3833 delete_breakpoint (b);
3834 continue;
3835 }
3836
3837 /* Longjmp and longjmp-resume breakpoints are also meaningless
3838 after an exec. */
3839 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3840 || b->type == bp_longjmp_call_dummy
3841 || b->type == bp_exception || b->type == bp_exception_resume)
3842 {
3843 delete_breakpoint (b);
3844 continue;
3845 }
3846
3847 if (b->type == bp_catchpoint)
3848 {
3849 /* For now, none of the bp_catchpoint breakpoints need to
3850 do anything at this point. In the future, if some of
3851 the catchpoints need to something, we will need to add
3852 a new method, and call this method from here. */
3853 continue;
3854 }
3855
3856 /* bp_finish is a special case. The only way we ought to be able
3857 to see one of these when an exec() has happened, is if the user
3858 caught a vfork, and then said "finish". Ordinarily a finish just
3859 carries them to the call-site of the current callee, by setting
3860 a temporary bp there and resuming. But in this case, the finish
3861 will carry them entirely through the vfork & exec.
3862
3863 We don't want to allow a bp_finish to remain inserted now. But
3864 we can't safely delete it, 'cause finish_command has a handle to
3865 the bp on a bpstat, and will later want to delete it. There's a
3866 chance (and I've seen it happen) that if we delete the bp_finish
3867 here, that its storage will get reused by the time finish_command
3868 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3869 We really must allow finish_command to delete a bp_finish.
3870
3871 In the absence of a general solution for the "how do we know
3872 it's safe to delete something others may have handles to?"
3873 problem, what we'll do here is just uninsert the bp_finish, and
3874 let finish_command delete it.
3875
3876 (We know the bp_finish is "doomed" in the sense that it's
3877 momentary, and will be deleted as soon as finish_command sees
3878 the inferior stopped. So it doesn't matter that the bp's
3879 address is probably bogus in the new a.out, unlike e.g., the
3880 solib breakpoints.) */
3881
3882 if (b->type == bp_finish)
3883 {
3884 continue;
3885 }
3886
3887 /* Without a symbolic address, we have little hope of the
3888 pre-exec() address meaning the same thing in the post-exec()
3889 a.out. */
3890 if (breakpoint_event_location_empty_p (b))
3891 {
3892 delete_breakpoint (b);
3893 continue;
3894 }
3895 }
3896 }
3897
3898 int
3899 detach_breakpoints (ptid_t ptid)
3900 {
3901 struct bp_location *bl, **blp_tmp;
3902 int val = 0;
3903 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3904 struct inferior *inf = current_inferior ();
3905
3906 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3907 error (_("Cannot detach breakpoints of inferior_ptid"));
3908
3909 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3910 inferior_ptid = ptid;
3911 ALL_BP_LOCATIONS (bl, blp_tmp)
3912 {
3913 if (bl->pspace != inf->pspace)
3914 continue;
3915
3916 /* This function must physically remove breakpoints locations
3917 from the specified ptid, without modifying the breakpoint
3918 package's state. Locations of type bp_loc_other are only
3919 maintained at GDB side. So, there is no need to remove
3920 these bp_loc_other locations. Moreover, removing these
3921 would modify the breakpoint package's state. */
3922 if (bl->loc_type == bp_loc_other)
3923 continue;
3924
3925 if (bl->inserted)
3926 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3927 }
3928
3929 return val;
3930 }
3931
3932 /* Remove the breakpoint location BL from the current address space.
3933 Note that this is used to detach breakpoints from a child fork.
3934 When we get here, the child isn't in the inferior list, and neither
3935 do we have objects to represent its address space --- we should
3936 *not* look at bl->pspace->aspace here. */
3937
3938 static int
3939 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3940 {
3941 int val;
3942
3943 /* BL is never in moribund_locations by our callers. */
3944 gdb_assert (bl->owner != NULL);
3945
3946 /* The type of none suggests that owner is actually deleted.
3947 This should not ever happen. */
3948 gdb_assert (bl->owner->type != bp_none);
3949
3950 if (bl->loc_type == bp_loc_software_breakpoint
3951 || bl->loc_type == bp_loc_hardware_breakpoint)
3952 {
3953 /* "Normal" instruction breakpoint: either the standard
3954 trap-instruction bp (bp_breakpoint), or a
3955 bp_hardware_breakpoint. */
3956
3957 /* First check to see if we have to handle an overlay. */
3958 if (overlay_debugging == ovly_off
3959 || bl->section == NULL
3960 || !(section_is_overlay (bl->section)))
3961 {
3962 /* No overlay handling: just remove the breakpoint. */
3963
3964 /* If we're trying to uninsert a memory breakpoint that we
3965 know is set in a dynamic object that is marked
3966 shlib_disabled, then either the dynamic object was
3967 removed with "remove-symbol-file" or with
3968 "nosharedlibrary". In the former case, we don't know
3969 whether another dynamic object might have loaded over the
3970 breakpoint's address -- the user might well let us know
3971 about it next with add-symbol-file (the whole point of
3972 add-symbol-file is letting the user manually maintain a
3973 list of dynamically loaded objects). If we have the
3974 breakpoint's shadow memory, that is, this is a software
3975 breakpoint managed by GDB, check whether the breakpoint
3976 is still inserted in memory, to avoid overwriting wrong
3977 code with stale saved shadow contents. Note that HW
3978 breakpoints don't have shadow memory, as they're
3979 implemented using a mechanism that is not dependent on
3980 being able to modify the target's memory, and as such
3981 they should always be removed. */
3982 if (bl->shlib_disabled
3983 && bl->target_info.shadow_len != 0
3984 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3985 val = 0;
3986 else
3987 val = bl->owner->ops->remove_location (bl, reason);
3988 }
3989 else
3990 {
3991 /* This breakpoint is in an overlay section.
3992 Did we set a breakpoint at the LMA? */
3993 if (!overlay_events_enabled)
3994 {
3995 /* Yes -- overlay event support is not active, so we
3996 should have set a breakpoint at the LMA. Remove it.
3997 */
3998 /* Ignore any failures: if the LMA is in ROM, we will
3999 have already warned when we failed to insert it. */
4000 if (bl->loc_type == bp_loc_hardware_breakpoint)
4001 target_remove_hw_breakpoint (bl->gdbarch,
4002 &bl->overlay_target_info);
4003 else
4004 target_remove_breakpoint (bl->gdbarch,
4005 &bl->overlay_target_info,
4006 reason);
4007 }
4008 /* Did we set a breakpoint at the VMA?
4009 If so, we will have marked the breakpoint 'inserted'. */
4010 if (bl->inserted)
4011 {
4012 /* Yes -- remove it. Previously we did not bother to
4013 remove the breakpoint if the section had been
4014 unmapped, but let's not rely on that being safe. We
4015 don't know what the overlay manager might do. */
4016
4017 /* However, we should remove *software* breakpoints only
4018 if the section is still mapped, or else we overwrite
4019 wrong code with the saved shadow contents. */
4020 if (bl->loc_type == bp_loc_hardware_breakpoint
4021 || section_is_mapped (bl->section))
4022 val = bl->owner->ops->remove_location (bl, reason);
4023 else
4024 val = 0;
4025 }
4026 else
4027 {
4028 /* No -- not inserted, so no need to remove. No error. */
4029 val = 0;
4030 }
4031 }
4032
4033 /* In some cases, we might not be able to remove a breakpoint in
4034 a shared library that has already been removed, but we have
4035 not yet processed the shlib unload event. Similarly for an
4036 unloaded add-symbol-file object - the user might not yet have
4037 had the chance to remove-symbol-file it. shlib_disabled will
4038 be set if the library/object has already been removed, but
4039 the breakpoint hasn't been uninserted yet, e.g., after
4040 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4041 always-inserted mode. */
4042 if (val
4043 && (bl->loc_type == bp_loc_software_breakpoint
4044 && (bl->shlib_disabled
4045 || solib_name_from_address (bl->pspace, bl->address)
4046 || shared_objfile_contains_address_p (bl->pspace,
4047 bl->address))))
4048 val = 0;
4049
4050 if (val)
4051 return val;
4052 bl->inserted = (reason == DETACH_BREAKPOINT);
4053 }
4054 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4055 {
4056 gdb_assert (bl->owner->ops != NULL
4057 && bl->owner->ops->remove_location != NULL);
4058
4059 bl->inserted = (reason == DETACH_BREAKPOINT);
4060 bl->owner->ops->remove_location (bl, reason);
4061
4062 /* Failure to remove any of the hardware watchpoints comes here. */
4063 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4064 warning (_("Could not remove hardware watchpoint %d."),
4065 bl->owner->number);
4066 }
4067 else if (bl->owner->type == bp_catchpoint
4068 && breakpoint_enabled (bl->owner)
4069 && !bl->duplicate)
4070 {
4071 gdb_assert (bl->owner->ops != NULL
4072 && bl->owner->ops->remove_location != NULL);
4073
4074 val = bl->owner->ops->remove_location (bl, reason);
4075 if (val)
4076 return val;
4077
4078 bl->inserted = (reason == DETACH_BREAKPOINT);
4079 }
4080
4081 return 0;
4082 }
4083
4084 static int
4085 remove_breakpoint (struct bp_location *bl)
4086 {
4087 /* BL is never in moribund_locations by our callers. */
4088 gdb_assert (bl->owner != NULL);
4089
4090 /* The type of none suggests that owner is actually deleted.
4091 This should not ever happen. */
4092 gdb_assert (bl->owner->type != bp_none);
4093
4094 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4095
4096 switch_to_program_space_and_thread (bl->pspace);
4097
4098 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4099 }
4100
4101 /* Clear the "inserted" flag in all breakpoints. */
4102
4103 void
4104 mark_breakpoints_out (void)
4105 {
4106 struct bp_location *bl, **blp_tmp;
4107
4108 ALL_BP_LOCATIONS (bl, blp_tmp)
4109 if (bl->pspace == current_program_space)
4110 bl->inserted = 0;
4111 }
4112
4113 /* Clear the "inserted" flag in all breakpoints and delete any
4114 breakpoints which should go away between runs of the program.
4115
4116 Plus other such housekeeping that has to be done for breakpoints
4117 between runs.
4118
4119 Note: this function gets called at the end of a run (by
4120 generic_mourn_inferior) and when a run begins (by
4121 init_wait_for_inferior). */
4122
4123
4124
4125 void
4126 breakpoint_init_inferior (enum inf_context context)
4127 {
4128 struct breakpoint *b, *b_tmp;
4129 struct bp_location *bl;
4130 int ix;
4131 struct program_space *pspace = current_program_space;
4132
4133 /* If breakpoint locations are shared across processes, then there's
4134 nothing to do. */
4135 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4136 return;
4137
4138 mark_breakpoints_out ();
4139
4140 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4141 {
4142 if (b->loc && b->loc->pspace != pspace)
4143 continue;
4144
4145 switch (b->type)
4146 {
4147 case bp_call_dummy:
4148 case bp_longjmp_call_dummy:
4149
4150 /* If the call dummy breakpoint is at the entry point it will
4151 cause problems when the inferior is rerun, so we better get
4152 rid of it. */
4153
4154 case bp_watchpoint_scope:
4155
4156 /* Also get rid of scope breakpoints. */
4157
4158 case bp_shlib_event:
4159
4160 /* Also remove solib event breakpoints. Their addresses may
4161 have changed since the last time we ran the program.
4162 Actually we may now be debugging against different target;
4163 and so the solib backend that installed this breakpoint may
4164 not be used in by the target. E.g.,
4165
4166 (gdb) file prog-linux
4167 (gdb) run # native linux target
4168 ...
4169 (gdb) kill
4170 (gdb) file prog-win.exe
4171 (gdb) tar rem :9999 # remote Windows gdbserver.
4172 */
4173
4174 case bp_step_resume:
4175
4176 /* Also remove step-resume breakpoints. */
4177
4178 case bp_single_step:
4179
4180 /* Also remove single-step breakpoints. */
4181
4182 delete_breakpoint (b);
4183 break;
4184
4185 case bp_watchpoint:
4186 case bp_hardware_watchpoint:
4187 case bp_read_watchpoint:
4188 case bp_access_watchpoint:
4189 {
4190 struct watchpoint *w = (struct watchpoint *) b;
4191
4192 /* Likewise for watchpoints on local expressions. */
4193 if (w->exp_valid_block != NULL)
4194 delete_breakpoint (b);
4195 else
4196 {
4197 /* Get rid of existing locations, which are no longer
4198 valid. New ones will be created in
4199 update_watchpoint, when the inferior is restarted.
4200 The next update_global_location_list call will
4201 garbage collect them. */
4202 b->loc = NULL;
4203
4204 if (context == inf_starting)
4205 {
4206 /* Reset val field to force reread of starting value in
4207 insert_breakpoints. */
4208 if (w->val)
4209 value_free (w->val);
4210 w->val = NULL;
4211 w->val_valid = 0;
4212 }
4213 }
4214 }
4215 break;
4216 default:
4217 break;
4218 }
4219 }
4220
4221 /* Get rid of the moribund locations. */
4222 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4223 decref_bp_location (&bl);
4224 VEC_free (bp_location_p, moribund_locations);
4225 }
4226
4227 /* These functions concern about actual breakpoints inserted in the
4228 target --- to e.g. check if we need to do decr_pc adjustment or if
4229 we need to hop over the bkpt --- so we check for address space
4230 match, not program space. */
4231
4232 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4233 exists at PC. It returns ordinary_breakpoint_here if it's an
4234 ordinary breakpoint, or permanent_breakpoint_here if it's a
4235 permanent breakpoint.
4236 - When continuing from a location with an ordinary breakpoint, we
4237 actually single step once before calling insert_breakpoints.
4238 - When continuing from a location with a permanent breakpoint, we
4239 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4240 the target, to advance the PC past the breakpoint. */
4241
4242 enum breakpoint_here
4243 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4244 {
4245 struct bp_location *bl, **blp_tmp;
4246 int any_breakpoint_here = 0;
4247
4248 ALL_BP_LOCATIONS (bl, blp_tmp)
4249 {
4250 if (bl->loc_type != bp_loc_software_breakpoint
4251 && bl->loc_type != bp_loc_hardware_breakpoint)
4252 continue;
4253
4254 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4255 if ((breakpoint_enabled (bl->owner)
4256 || bl->permanent)
4257 && breakpoint_location_address_match (bl, aspace, pc))
4258 {
4259 if (overlay_debugging
4260 && section_is_overlay (bl->section)
4261 && !section_is_mapped (bl->section))
4262 continue; /* unmapped overlay -- can't be a match */
4263 else if (bl->permanent)
4264 return permanent_breakpoint_here;
4265 else
4266 any_breakpoint_here = 1;
4267 }
4268 }
4269
4270 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4271 }
4272
4273 /* See breakpoint.h. */
4274
4275 int
4276 breakpoint_in_range_p (struct address_space *aspace,
4277 CORE_ADDR addr, ULONGEST len)
4278 {
4279 struct bp_location *bl, **blp_tmp;
4280
4281 ALL_BP_LOCATIONS (bl, blp_tmp)
4282 {
4283 if (bl->loc_type != bp_loc_software_breakpoint
4284 && bl->loc_type != bp_loc_hardware_breakpoint)
4285 continue;
4286
4287 if ((breakpoint_enabled (bl->owner)
4288 || bl->permanent)
4289 && breakpoint_location_address_range_overlap (bl, aspace,
4290 addr, len))
4291 {
4292 if (overlay_debugging
4293 && section_is_overlay (bl->section)
4294 && !section_is_mapped (bl->section))
4295 {
4296 /* Unmapped overlay -- can't be a match. */
4297 continue;
4298 }
4299
4300 return 1;
4301 }
4302 }
4303
4304 return 0;
4305 }
4306
4307 /* Return true if there's a moribund breakpoint at PC. */
4308
4309 int
4310 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4311 {
4312 struct bp_location *loc;
4313 int ix;
4314
4315 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4316 if (breakpoint_location_address_match (loc, aspace, pc))
4317 return 1;
4318
4319 return 0;
4320 }
4321
4322 /* Returns non-zero iff BL is inserted at PC, in address space
4323 ASPACE. */
4324
4325 static int
4326 bp_location_inserted_here_p (struct bp_location *bl,
4327 struct address_space *aspace, CORE_ADDR pc)
4328 {
4329 if (bl->inserted
4330 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4331 aspace, pc))
4332 {
4333 if (overlay_debugging
4334 && section_is_overlay (bl->section)
4335 && !section_is_mapped (bl->section))
4336 return 0; /* unmapped overlay -- can't be a match */
4337 else
4338 return 1;
4339 }
4340 return 0;
4341 }
4342
4343 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4344
4345 int
4346 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4347 {
4348 struct bp_location **blp, **blp_tmp = NULL;
4349
4350 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4351 {
4352 struct bp_location *bl = *blp;
4353
4354 if (bl->loc_type != bp_loc_software_breakpoint
4355 && bl->loc_type != bp_loc_hardware_breakpoint)
4356 continue;
4357
4358 if (bp_location_inserted_here_p (bl, aspace, pc))
4359 return 1;
4360 }
4361 return 0;
4362 }
4363
4364 /* This function returns non-zero iff there is a software breakpoint
4365 inserted at PC. */
4366
4367 int
4368 software_breakpoint_inserted_here_p (struct address_space *aspace,
4369 CORE_ADDR pc)
4370 {
4371 struct bp_location **blp, **blp_tmp = NULL;
4372
4373 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4374 {
4375 struct bp_location *bl = *blp;
4376
4377 if (bl->loc_type != bp_loc_software_breakpoint)
4378 continue;
4379
4380 if (bp_location_inserted_here_p (bl, aspace, pc))
4381 return 1;
4382 }
4383
4384 return 0;
4385 }
4386
4387 /* See breakpoint.h. */
4388
4389 int
4390 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4391 CORE_ADDR pc)
4392 {
4393 struct bp_location **blp, **blp_tmp = NULL;
4394
4395 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4396 {
4397 struct bp_location *bl = *blp;
4398
4399 if (bl->loc_type != bp_loc_hardware_breakpoint)
4400 continue;
4401
4402 if (bp_location_inserted_here_p (bl, aspace, pc))
4403 return 1;
4404 }
4405
4406 return 0;
4407 }
4408
4409 int
4410 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4411 CORE_ADDR addr, ULONGEST len)
4412 {
4413 struct breakpoint *bpt;
4414
4415 ALL_BREAKPOINTS (bpt)
4416 {
4417 struct bp_location *loc;
4418
4419 if (bpt->type != bp_hardware_watchpoint
4420 && bpt->type != bp_access_watchpoint)
4421 continue;
4422
4423 if (!breakpoint_enabled (bpt))
4424 continue;
4425
4426 for (loc = bpt->loc; loc; loc = loc->next)
4427 if (loc->pspace->aspace == aspace && loc->inserted)
4428 {
4429 CORE_ADDR l, h;
4430
4431 /* Check for intersection. */
4432 l = std::max<CORE_ADDR> (loc->address, addr);
4433 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4434 if (l < h)
4435 return 1;
4436 }
4437 }
4438 return 0;
4439 }
4440 \f
4441
4442 /* bpstat stuff. External routines' interfaces are documented
4443 in breakpoint.h. */
4444
4445 int
4446 is_catchpoint (struct breakpoint *ep)
4447 {
4448 return (ep->type == bp_catchpoint);
4449 }
4450
4451 /* Frees any storage that is part of a bpstat. Does not walk the
4452 'next' chain. */
4453
4454 static void
4455 bpstat_free (bpstat bs)
4456 {
4457 if (bs->old_val != NULL)
4458 value_free (bs->old_val);
4459 decref_counted_command_line (&bs->commands);
4460 decref_bp_location (&bs->bp_location_at);
4461 xfree (bs);
4462 }
4463
4464 /* Clear a bpstat so that it says we are not at any breakpoint.
4465 Also free any storage that is part of a bpstat. */
4466
4467 void
4468 bpstat_clear (bpstat *bsp)
4469 {
4470 bpstat p;
4471 bpstat q;
4472
4473 if (bsp == 0)
4474 return;
4475 p = *bsp;
4476 while (p != NULL)
4477 {
4478 q = p->next;
4479 bpstat_free (p);
4480 p = q;
4481 }
4482 *bsp = NULL;
4483 }
4484
4485 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4486 is part of the bpstat is copied as well. */
4487
4488 bpstat
4489 bpstat_copy (bpstat bs)
4490 {
4491 bpstat p = NULL;
4492 bpstat tmp;
4493 bpstat retval = NULL;
4494
4495 if (bs == NULL)
4496 return bs;
4497
4498 for (; bs != NULL; bs = bs->next)
4499 {
4500 tmp = (bpstat) xmalloc (sizeof (*tmp));
4501 memcpy (tmp, bs, sizeof (*tmp));
4502 incref_counted_command_line (tmp->commands);
4503 incref_bp_location (tmp->bp_location_at);
4504 if (bs->old_val != NULL)
4505 {
4506 tmp->old_val = value_copy (bs->old_val);
4507 release_value (tmp->old_val);
4508 }
4509
4510 if (p == NULL)
4511 /* This is the first thing in the chain. */
4512 retval = tmp;
4513 else
4514 p->next = tmp;
4515 p = tmp;
4516 }
4517 p->next = NULL;
4518 return retval;
4519 }
4520
4521 /* Find the bpstat associated with this breakpoint. */
4522
4523 bpstat
4524 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4525 {
4526 if (bsp == NULL)
4527 return NULL;
4528
4529 for (; bsp != NULL; bsp = bsp->next)
4530 {
4531 if (bsp->breakpoint_at == breakpoint)
4532 return bsp;
4533 }
4534 return NULL;
4535 }
4536
4537 /* See breakpoint.h. */
4538
4539 int
4540 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4541 {
4542 for (; bsp != NULL; bsp = bsp->next)
4543 {
4544 if (bsp->breakpoint_at == NULL)
4545 {
4546 /* A moribund location can never explain a signal other than
4547 GDB_SIGNAL_TRAP. */
4548 if (sig == GDB_SIGNAL_TRAP)
4549 return 1;
4550 }
4551 else
4552 {
4553 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4554 sig))
4555 return 1;
4556 }
4557 }
4558
4559 return 0;
4560 }
4561
4562 /* Put in *NUM the breakpoint number of the first breakpoint we are
4563 stopped at. *BSP upon return is a bpstat which points to the
4564 remaining breakpoints stopped at (but which is not guaranteed to be
4565 good for anything but further calls to bpstat_num).
4566
4567 Return 0 if passed a bpstat which does not indicate any breakpoints.
4568 Return -1 if stopped at a breakpoint that has been deleted since
4569 we set it.
4570 Return 1 otherwise. */
4571
4572 int
4573 bpstat_num (bpstat *bsp, int *num)
4574 {
4575 struct breakpoint *b;
4576
4577 if ((*bsp) == NULL)
4578 return 0; /* No more breakpoint values */
4579
4580 /* We assume we'll never have several bpstats that correspond to a
4581 single breakpoint -- otherwise, this function might return the
4582 same number more than once and this will look ugly. */
4583 b = (*bsp)->breakpoint_at;
4584 *bsp = (*bsp)->next;
4585 if (b == NULL)
4586 return -1; /* breakpoint that's been deleted since */
4587
4588 *num = b->number; /* We have its number */
4589 return 1;
4590 }
4591
4592 /* See breakpoint.h. */
4593
4594 void
4595 bpstat_clear_actions (void)
4596 {
4597 struct thread_info *tp;
4598 bpstat bs;
4599
4600 if (ptid_equal (inferior_ptid, null_ptid))
4601 return;
4602
4603 tp = find_thread_ptid (inferior_ptid);
4604 if (tp == NULL)
4605 return;
4606
4607 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4608 {
4609 decref_counted_command_line (&bs->commands);
4610
4611 if (bs->old_val != NULL)
4612 {
4613 value_free (bs->old_val);
4614 bs->old_val = NULL;
4615 }
4616 }
4617 }
4618
4619 /* Called when a command is about to proceed the inferior. */
4620
4621 static void
4622 breakpoint_about_to_proceed (void)
4623 {
4624 if (!ptid_equal (inferior_ptid, null_ptid))
4625 {
4626 struct thread_info *tp = inferior_thread ();
4627
4628 /* Allow inferior function calls in breakpoint commands to not
4629 interrupt the command list. When the call finishes
4630 successfully, the inferior will be standing at the same
4631 breakpoint as if nothing happened. */
4632 if (tp->control.in_infcall)
4633 return;
4634 }
4635
4636 breakpoint_proceeded = 1;
4637 }
4638
4639 /* Stub for cleaning up our state if we error-out of a breakpoint
4640 command. */
4641 static void
4642 cleanup_executing_breakpoints (void *ignore)
4643 {
4644 executing_breakpoint_commands = 0;
4645 }
4646
4647 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4648 or its equivalent. */
4649
4650 static int
4651 command_line_is_silent (struct command_line *cmd)
4652 {
4653 return cmd && (strcmp ("silent", cmd->line) == 0);
4654 }
4655
4656 /* Execute all the commands associated with all the breakpoints at
4657 this location. Any of these commands could cause the process to
4658 proceed beyond this point, etc. We look out for such changes by
4659 checking the global "breakpoint_proceeded" after each command.
4660
4661 Returns true if a breakpoint command resumed the inferior. In that
4662 case, it is the caller's responsibility to recall it again with the
4663 bpstat of the current thread. */
4664
4665 static int
4666 bpstat_do_actions_1 (bpstat *bsp)
4667 {
4668 bpstat bs;
4669 struct cleanup *old_chain;
4670 int again = 0;
4671
4672 /* Avoid endless recursion if a `source' command is contained
4673 in bs->commands. */
4674 if (executing_breakpoint_commands)
4675 return 0;
4676
4677 executing_breakpoint_commands = 1;
4678 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4679
4680 scoped_restore preventer = prevent_dont_repeat ();
4681
4682 /* This pointer will iterate over the list of bpstat's. */
4683 bs = *bsp;
4684
4685 breakpoint_proceeded = 0;
4686 for (; bs != NULL; bs = bs->next)
4687 {
4688 struct counted_command_line *ccmd;
4689 struct command_line *cmd;
4690 struct cleanup *this_cmd_tree_chain;
4691
4692 /* Take ownership of the BSP's command tree, if it has one.
4693
4694 The command tree could legitimately contain commands like
4695 'step' and 'next', which call clear_proceed_status, which
4696 frees stop_bpstat's command tree. To make sure this doesn't
4697 free the tree we're executing out from under us, we need to
4698 take ownership of the tree ourselves. Since a given bpstat's
4699 commands are only executed once, we don't need to copy it; we
4700 can clear the pointer in the bpstat, and make sure we free
4701 the tree when we're done. */
4702 ccmd = bs->commands;
4703 bs->commands = NULL;
4704 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4705 cmd = ccmd ? ccmd->commands : NULL;
4706 if (command_line_is_silent (cmd))
4707 {
4708 /* The action has been already done by bpstat_stop_status. */
4709 cmd = cmd->next;
4710 }
4711
4712 while (cmd != NULL)
4713 {
4714 execute_control_command (cmd);
4715
4716 if (breakpoint_proceeded)
4717 break;
4718 else
4719 cmd = cmd->next;
4720 }
4721
4722 /* We can free this command tree now. */
4723 do_cleanups (this_cmd_tree_chain);
4724
4725 if (breakpoint_proceeded)
4726 {
4727 if (current_ui->async)
4728 /* If we are in async mode, then the target might be still
4729 running, not stopped at any breakpoint, so nothing for
4730 us to do here -- just return to the event loop. */
4731 ;
4732 else
4733 /* In sync mode, when execute_control_command returns
4734 we're already standing on the next breakpoint.
4735 Breakpoint commands for that stop were not run, since
4736 execute_command does not run breakpoint commands --
4737 only command_line_handler does, but that one is not
4738 involved in execution of breakpoint commands. So, we
4739 can now execute breakpoint commands. It should be
4740 noted that making execute_command do bpstat actions is
4741 not an option -- in this case we'll have recursive
4742 invocation of bpstat for each breakpoint with a
4743 command, and can easily blow up GDB stack. Instead, we
4744 return true, which will trigger the caller to recall us
4745 with the new stop_bpstat. */
4746 again = 1;
4747 break;
4748 }
4749 }
4750 do_cleanups (old_chain);
4751 return again;
4752 }
4753
4754 void
4755 bpstat_do_actions (void)
4756 {
4757 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4758
4759 /* Do any commands attached to breakpoint we are stopped at. */
4760 while (!ptid_equal (inferior_ptid, null_ptid)
4761 && target_has_execution
4762 && !is_exited (inferior_ptid)
4763 && !is_executing (inferior_ptid))
4764 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4765 and only return when it is stopped at the next breakpoint, we
4766 keep doing breakpoint actions until it returns false to
4767 indicate the inferior was not resumed. */
4768 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4769 break;
4770
4771 discard_cleanups (cleanup_if_error);
4772 }
4773
4774 /* Print out the (old or new) value associated with a watchpoint. */
4775
4776 static void
4777 watchpoint_value_print (struct value *val, struct ui_file *stream)
4778 {
4779 if (val == NULL)
4780 fprintf_unfiltered (stream, _("<unreadable>"));
4781 else
4782 {
4783 struct value_print_options opts;
4784 get_user_print_options (&opts);
4785 value_print (val, stream, &opts);
4786 }
4787 }
4788
4789 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4790 debugging multiple threads. */
4791
4792 void
4793 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4794 {
4795 if (uiout->is_mi_like_p ())
4796 return;
4797
4798 uiout->text ("\n");
4799
4800 if (show_thread_that_caused_stop ())
4801 {
4802 const char *name;
4803 struct thread_info *thr = inferior_thread ();
4804
4805 uiout->text ("Thread ");
4806 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4807
4808 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4809 if (name != NULL)
4810 {
4811 uiout->text (" \"");
4812 uiout->field_fmt ("name", "%s", name);
4813 uiout->text ("\"");
4814 }
4815
4816 uiout->text (" hit ");
4817 }
4818 }
4819
4820 /* Generic routine for printing messages indicating why we
4821 stopped. The behavior of this function depends on the value
4822 'print_it' in the bpstat structure. Under some circumstances we
4823 may decide not to print anything here and delegate the task to
4824 normal_stop(). */
4825
4826 static enum print_stop_action
4827 print_bp_stop_message (bpstat bs)
4828 {
4829 switch (bs->print_it)
4830 {
4831 case print_it_noop:
4832 /* Nothing should be printed for this bpstat entry. */
4833 return PRINT_UNKNOWN;
4834 break;
4835
4836 case print_it_done:
4837 /* We still want to print the frame, but we already printed the
4838 relevant messages. */
4839 return PRINT_SRC_AND_LOC;
4840 break;
4841
4842 case print_it_normal:
4843 {
4844 struct breakpoint *b = bs->breakpoint_at;
4845
4846 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4847 which has since been deleted. */
4848 if (b == NULL)
4849 return PRINT_UNKNOWN;
4850
4851 /* Normal case. Call the breakpoint's print_it method. */
4852 return b->ops->print_it (bs);
4853 }
4854 break;
4855
4856 default:
4857 internal_error (__FILE__, __LINE__,
4858 _("print_bp_stop_message: unrecognized enum value"));
4859 break;
4860 }
4861 }
4862
4863 /* A helper function that prints a shared library stopped event. */
4864
4865 static void
4866 print_solib_event (int is_catchpoint)
4867 {
4868 int any_deleted
4869 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4870 int any_added
4871 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4872
4873 if (!is_catchpoint)
4874 {
4875 if (any_added || any_deleted)
4876 current_uiout->text (_("Stopped due to shared library event:\n"));
4877 else
4878 current_uiout->text (_("Stopped due to shared library event (no "
4879 "libraries added or removed)\n"));
4880 }
4881
4882 if (current_uiout->is_mi_like_p ())
4883 current_uiout->field_string ("reason",
4884 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4885
4886 if (any_deleted)
4887 {
4888 char *name;
4889 int ix;
4890
4891 current_uiout->text (_(" Inferior unloaded "));
4892 ui_out_emit_list list_emitter (current_uiout, "removed");
4893 for (ix = 0;
4894 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4895 ix, name);
4896 ++ix)
4897 {
4898 if (ix > 0)
4899 current_uiout->text (" ");
4900 current_uiout->field_string ("library", name);
4901 current_uiout->text ("\n");
4902 }
4903 }
4904
4905 if (any_added)
4906 {
4907 struct so_list *iter;
4908 int ix;
4909
4910 current_uiout->text (_(" Inferior loaded "));
4911 ui_out_emit_list list_emitter (current_uiout, "added");
4912 for (ix = 0;
4913 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4914 ix, iter);
4915 ++ix)
4916 {
4917 if (ix > 0)
4918 current_uiout->text (" ");
4919 current_uiout->field_string ("library", iter->so_name);
4920 current_uiout->text ("\n");
4921 }
4922 }
4923 }
4924
4925 /* Print a message indicating what happened. This is called from
4926 normal_stop(). The input to this routine is the head of the bpstat
4927 list - a list of the eventpoints that caused this stop. KIND is
4928 the target_waitkind for the stopping event. This
4929 routine calls the generic print routine for printing a message
4930 about reasons for stopping. This will print (for example) the
4931 "Breakpoint n," part of the output. The return value of this
4932 routine is one of:
4933
4934 PRINT_UNKNOWN: Means we printed nothing.
4935 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4936 code to print the location. An example is
4937 "Breakpoint 1, " which should be followed by
4938 the location.
4939 PRINT_SRC_ONLY: Means we printed something, but there is no need
4940 to also print the location part of the message.
4941 An example is the catch/throw messages, which
4942 don't require a location appended to the end.
4943 PRINT_NOTHING: We have done some printing and we don't need any
4944 further info to be printed. */
4945
4946 enum print_stop_action
4947 bpstat_print (bpstat bs, int kind)
4948 {
4949 enum print_stop_action val;
4950
4951 /* Maybe another breakpoint in the chain caused us to stop.
4952 (Currently all watchpoints go on the bpstat whether hit or not.
4953 That probably could (should) be changed, provided care is taken
4954 with respect to bpstat_explains_signal). */
4955 for (; bs; bs = bs->next)
4956 {
4957 val = print_bp_stop_message (bs);
4958 if (val == PRINT_SRC_ONLY
4959 || val == PRINT_SRC_AND_LOC
4960 || val == PRINT_NOTHING)
4961 return val;
4962 }
4963
4964 /* If we had hit a shared library event breakpoint,
4965 print_bp_stop_message would print out this message. If we hit an
4966 OS-level shared library event, do the same thing. */
4967 if (kind == TARGET_WAITKIND_LOADED)
4968 {
4969 print_solib_event (0);
4970 return PRINT_NOTHING;
4971 }
4972
4973 /* We reached the end of the chain, or we got a null BS to start
4974 with and nothing was printed. */
4975 return PRINT_UNKNOWN;
4976 }
4977
4978 /* Evaluate the expression EXP and return 1 if value is zero.
4979 This returns the inverse of the condition because it is called
4980 from catch_errors which returns 0 if an exception happened, and if an
4981 exception happens we want execution to stop.
4982 The argument is a "struct expression *" that has been cast to a
4983 "void *" to make it pass through catch_errors. */
4984
4985 static int
4986 breakpoint_cond_eval (void *exp)
4987 {
4988 struct value *mark = value_mark ();
4989 int i = !value_true (evaluate_expression ((struct expression *) exp));
4990
4991 value_free_to_mark (mark);
4992 return i;
4993 }
4994
4995 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4996
4997 static bpstat
4998 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4999 {
5000 bpstat bs;
5001
5002 bs = (bpstat) xmalloc (sizeof (*bs));
5003 bs->next = NULL;
5004 **bs_link_pointer = bs;
5005 *bs_link_pointer = &bs->next;
5006 bs->breakpoint_at = bl->owner;
5007 bs->bp_location_at = bl;
5008 incref_bp_location (bl);
5009 /* If the condition is false, etc., don't do the commands. */
5010 bs->commands = NULL;
5011 bs->old_val = NULL;
5012 bs->print_it = print_it_normal;
5013 return bs;
5014 }
5015 \f
5016 /* The target has stopped with waitstatus WS. Check if any hardware
5017 watchpoints have triggered, according to the target. */
5018
5019 int
5020 watchpoints_triggered (struct target_waitstatus *ws)
5021 {
5022 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5023 CORE_ADDR addr;
5024 struct breakpoint *b;
5025
5026 if (!stopped_by_watchpoint)
5027 {
5028 /* We were not stopped by a watchpoint. Mark all watchpoints
5029 as not triggered. */
5030 ALL_BREAKPOINTS (b)
5031 if (is_hardware_watchpoint (b))
5032 {
5033 struct watchpoint *w = (struct watchpoint *) b;
5034
5035 w->watchpoint_triggered = watch_triggered_no;
5036 }
5037
5038 return 0;
5039 }
5040
5041 if (!target_stopped_data_address (&current_target, &addr))
5042 {
5043 /* We were stopped by a watchpoint, but we don't know where.
5044 Mark all watchpoints as unknown. */
5045 ALL_BREAKPOINTS (b)
5046 if (is_hardware_watchpoint (b))
5047 {
5048 struct watchpoint *w = (struct watchpoint *) b;
5049
5050 w->watchpoint_triggered = watch_triggered_unknown;
5051 }
5052
5053 return 1;
5054 }
5055
5056 /* The target could report the data address. Mark watchpoints
5057 affected by this data address as triggered, and all others as not
5058 triggered. */
5059
5060 ALL_BREAKPOINTS (b)
5061 if (is_hardware_watchpoint (b))
5062 {
5063 struct watchpoint *w = (struct watchpoint *) b;
5064 struct bp_location *loc;
5065
5066 w->watchpoint_triggered = watch_triggered_no;
5067 for (loc = b->loc; loc; loc = loc->next)
5068 {
5069 if (is_masked_watchpoint (b))
5070 {
5071 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5072 CORE_ADDR start = loc->address & w->hw_wp_mask;
5073
5074 if (newaddr == start)
5075 {
5076 w->watchpoint_triggered = watch_triggered_yes;
5077 break;
5078 }
5079 }
5080 /* Exact match not required. Within range is sufficient. */
5081 else if (target_watchpoint_addr_within_range (&current_target,
5082 addr, loc->address,
5083 loc->length))
5084 {
5085 w->watchpoint_triggered = watch_triggered_yes;
5086 break;
5087 }
5088 }
5089 }
5090
5091 return 1;
5092 }
5093
5094 /* Possible return values for watchpoint_check (this can't be an enum
5095 because of check_errors). */
5096 /* The watchpoint has been deleted. */
5097 #define WP_DELETED 1
5098 /* The value has changed. */
5099 #define WP_VALUE_CHANGED 2
5100 /* The value has not changed. */
5101 #define WP_VALUE_NOT_CHANGED 3
5102 /* Ignore this watchpoint, no matter if the value changed or not. */
5103 #define WP_IGNORE 4
5104
5105 #define BP_TEMPFLAG 1
5106 #define BP_HARDWAREFLAG 2
5107
5108 /* Evaluate watchpoint condition expression and check if its value
5109 changed.
5110
5111 P should be a pointer to struct bpstat, but is defined as a void *
5112 in order for this function to be usable with catch_errors. */
5113
5114 static int
5115 watchpoint_check (void *p)
5116 {
5117 bpstat bs = (bpstat) p;
5118 struct watchpoint *b;
5119 struct frame_info *fr;
5120 int within_current_scope;
5121
5122 /* BS is built from an existing struct breakpoint. */
5123 gdb_assert (bs->breakpoint_at != NULL);
5124 b = (struct watchpoint *) bs->breakpoint_at;
5125
5126 /* If this is a local watchpoint, we only want to check if the
5127 watchpoint frame is in scope if the current thread is the thread
5128 that was used to create the watchpoint. */
5129 if (!watchpoint_in_thread_scope (b))
5130 return WP_IGNORE;
5131
5132 if (b->exp_valid_block == NULL)
5133 within_current_scope = 1;
5134 else
5135 {
5136 struct frame_info *frame = get_current_frame ();
5137 struct gdbarch *frame_arch = get_frame_arch (frame);
5138 CORE_ADDR frame_pc = get_frame_pc (frame);
5139
5140 /* stack_frame_destroyed_p() returns a non-zero value if we're
5141 still in the function but the stack frame has already been
5142 invalidated. Since we can't rely on the values of local
5143 variables after the stack has been destroyed, we are treating
5144 the watchpoint in that state as `not changed' without further
5145 checking. Don't mark watchpoints as changed if the current
5146 frame is in an epilogue - even if they are in some other
5147 frame, our view of the stack is likely to be wrong and
5148 frame_find_by_id could error out. */
5149 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5150 return WP_IGNORE;
5151
5152 fr = frame_find_by_id (b->watchpoint_frame);
5153 within_current_scope = (fr != NULL);
5154
5155 /* If we've gotten confused in the unwinder, we might have
5156 returned a frame that can't describe this variable. */
5157 if (within_current_scope)
5158 {
5159 struct symbol *function;
5160
5161 function = get_frame_function (fr);
5162 if (function == NULL
5163 || !contained_in (b->exp_valid_block,
5164 SYMBOL_BLOCK_VALUE (function)))
5165 within_current_scope = 0;
5166 }
5167
5168 if (within_current_scope)
5169 /* If we end up stopping, the current frame will get selected
5170 in normal_stop. So this call to select_frame won't affect
5171 the user. */
5172 select_frame (fr);
5173 }
5174
5175 if (within_current_scope)
5176 {
5177 /* We use value_{,free_to_}mark because it could be a *long*
5178 time before we return to the command level and call
5179 free_all_values. We can't call free_all_values because we
5180 might be in the middle of evaluating a function call. */
5181
5182 int pc = 0;
5183 struct value *mark;
5184 struct value *new_val;
5185
5186 if (is_masked_watchpoint (b))
5187 /* Since we don't know the exact trigger address (from
5188 stopped_data_address), just tell the user we've triggered
5189 a mask watchpoint. */
5190 return WP_VALUE_CHANGED;
5191
5192 mark = value_mark ();
5193 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5194
5195 if (b->val_bitsize != 0)
5196 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5197
5198 /* We use value_equal_contents instead of value_equal because
5199 the latter coerces an array to a pointer, thus comparing just
5200 the address of the array instead of its contents. This is
5201 not what we want. */
5202 if ((b->val != NULL) != (new_val != NULL)
5203 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5204 {
5205 if (new_val != NULL)
5206 {
5207 release_value (new_val);
5208 value_free_to_mark (mark);
5209 }
5210 bs->old_val = b->val;
5211 b->val = new_val;
5212 b->val_valid = 1;
5213 return WP_VALUE_CHANGED;
5214 }
5215 else
5216 {
5217 /* Nothing changed. */
5218 value_free_to_mark (mark);
5219 return WP_VALUE_NOT_CHANGED;
5220 }
5221 }
5222 else
5223 {
5224 /* This seems like the only logical thing to do because
5225 if we temporarily ignored the watchpoint, then when
5226 we reenter the block in which it is valid it contains
5227 garbage (in the case of a function, it may have two
5228 garbage values, one before and one after the prologue).
5229 So we can't even detect the first assignment to it and
5230 watch after that (since the garbage may or may not equal
5231 the first value assigned). */
5232 /* We print all the stop information in
5233 breakpoint_ops->print_it, but in this case, by the time we
5234 call breakpoint_ops->print_it this bp will be deleted
5235 already. So we have no choice but print the information
5236 here. */
5237
5238 SWITCH_THRU_ALL_UIS ()
5239 {
5240 struct ui_out *uiout = current_uiout;
5241
5242 if (uiout->is_mi_like_p ())
5243 uiout->field_string
5244 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5245 uiout->text ("\nWatchpoint ");
5246 uiout->field_int ("wpnum", b->number);
5247 uiout->text (" deleted because the program has left the block in\n"
5248 "which its expression is valid.\n");
5249 }
5250
5251 /* Make sure the watchpoint's commands aren't executed. */
5252 decref_counted_command_line (&b->commands);
5253 watchpoint_del_at_next_stop (b);
5254
5255 return WP_DELETED;
5256 }
5257 }
5258
5259 /* Return true if it looks like target has stopped due to hitting
5260 breakpoint location BL. This function does not check if we should
5261 stop, only if BL explains the stop. */
5262
5263 static int
5264 bpstat_check_location (const struct bp_location *bl,
5265 struct address_space *aspace, CORE_ADDR bp_addr,
5266 const struct target_waitstatus *ws)
5267 {
5268 struct breakpoint *b = bl->owner;
5269
5270 /* BL is from an existing breakpoint. */
5271 gdb_assert (b != NULL);
5272
5273 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5274 }
5275
5276 /* Determine if the watched values have actually changed, and we
5277 should stop. If not, set BS->stop to 0. */
5278
5279 static void
5280 bpstat_check_watchpoint (bpstat bs)
5281 {
5282 const struct bp_location *bl;
5283 struct watchpoint *b;
5284
5285 /* BS is built for existing struct breakpoint. */
5286 bl = bs->bp_location_at;
5287 gdb_assert (bl != NULL);
5288 b = (struct watchpoint *) bs->breakpoint_at;
5289 gdb_assert (b != NULL);
5290
5291 {
5292 int must_check_value = 0;
5293
5294 if (b->type == bp_watchpoint)
5295 /* For a software watchpoint, we must always check the
5296 watched value. */
5297 must_check_value = 1;
5298 else if (b->watchpoint_triggered == watch_triggered_yes)
5299 /* We have a hardware watchpoint (read, write, or access)
5300 and the target earlier reported an address watched by
5301 this watchpoint. */
5302 must_check_value = 1;
5303 else if (b->watchpoint_triggered == watch_triggered_unknown
5304 && b->type == bp_hardware_watchpoint)
5305 /* We were stopped by a hardware watchpoint, but the target could
5306 not report the data address. We must check the watchpoint's
5307 value. Access and read watchpoints are out of luck; without
5308 a data address, we can't figure it out. */
5309 must_check_value = 1;
5310
5311 if (must_check_value)
5312 {
5313 char *message
5314 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5315 b->number);
5316 struct cleanup *cleanups = make_cleanup (xfree, message);
5317 int e = catch_errors (watchpoint_check, bs, message,
5318 RETURN_MASK_ALL);
5319 do_cleanups (cleanups);
5320 switch (e)
5321 {
5322 case WP_DELETED:
5323 /* We've already printed what needs to be printed. */
5324 bs->print_it = print_it_done;
5325 /* Stop. */
5326 break;
5327 case WP_IGNORE:
5328 bs->print_it = print_it_noop;
5329 bs->stop = 0;
5330 break;
5331 case WP_VALUE_CHANGED:
5332 if (b->type == bp_read_watchpoint)
5333 {
5334 /* There are two cases to consider here:
5335
5336 1. We're watching the triggered memory for reads.
5337 In that case, trust the target, and always report
5338 the watchpoint hit to the user. Even though
5339 reads don't cause value changes, the value may
5340 have changed since the last time it was read, and
5341 since we're not trapping writes, we will not see
5342 those, and as such we should ignore our notion of
5343 old value.
5344
5345 2. We're watching the triggered memory for both
5346 reads and writes. There are two ways this may
5347 happen:
5348
5349 2.1. This is a target that can't break on data
5350 reads only, but can break on accesses (reads or
5351 writes), such as e.g., x86. We detect this case
5352 at the time we try to insert read watchpoints.
5353
5354 2.2. Otherwise, the target supports read
5355 watchpoints, but, the user set an access or write
5356 watchpoint watching the same memory as this read
5357 watchpoint.
5358
5359 If we're watching memory writes as well as reads,
5360 ignore watchpoint hits when we find that the
5361 value hasn't changed, as reads don't cause
5362 changes. This still gives false positives when
5363 the program writes the same value to memory as
5364 what there was already in memory (we will confuse
5365 it for a read), but it's much better than
5366 nothing. */
5367
5368 int other_write_watchpoint = 0;
5369
5370 if (bl->watchpoint_type == hw_read)
5371 {
5372 struct breakpoint *other_b;
5373
5374 ALL_BREAKPOINTS (other_b)
5375 if (other_b->type == bp_hardware_watchpoint
5376 || other_b->type == bp_access_watchpoint)
5377 {
5378 struct watchpoint *other_w =
5379 (struct watchpoint *) other_b;
5380
5381 if (other_w->watchpoint_triggered
5382 == watch_triggered_yes)
5383 {
5384 other_write_watchpoint = 1;
5385 break;
5386 }
5387 }
5388 }
5389
5390 if (other_write_watchpoint
5391 || bl->watchpoint_type == hw_access)
5392 {
5393 /* We're watching the same memory for writes,
5394 and the value changed since the last time we
5395 updated it, so this trap must be for a write.
5396 Ignore it. */
5397 bs->print_it = print_it_noop;
5398 bs->stop = 0;
5399 }
5400 }
5401 break;
5402 case WP_VALUE_NOT_CHANGED:
5403 if (b->type == bp_hardware_watchpoint
5404 || b->type == bp_watchpoint)
5405 {
5406 /* Don't stop: write watchpoints shouldn't fire if
5407 the value hasn't changed. */
5408 bs->print_it = print_it_noop;
5409 bs->stop = 0;
5410 }
5411 /* Stop. */
5412 break;
5413 default:
5414 /* Can't happen. */
5415 case 0:
5416 /* Error from catch_errors. */
5417 {
5418 SWITCH_THRU_ALL_UIS ()
5419 {
5420 printf_filtered (_("Watchpoint %d deleted.\n"),
5421 b->number);
5422 }
5423 watchpoint_del_at_next_stop (b);
5424 /* We've already printed what needs to be printed. */
5425 bs->print_it = print_it_done;
5426 }
5427 break;
5428 }
5429 }
5430 else /* must_check_value == 0 */
5431 {
5432 /* This is a case where some watchpoint(s) triggered, but
5433 not at the address of this watchpoint, or else no
5434 watchpoint triggered after all. So don't print
5435 anything for this watchpoint. */
5436 bs->print_it = print_it_noop;
5437 bs->stop = 0;
5438 }
5439 }
5440 }
5441
5442 /* For breakpoints that are currently marked as telling gdb to stop,
5443 check conditions (condition proper, frame, thread and ignore count)
5444 of breakpoint referred to by BS. If we should not stop for this
5445 breakpoint, set BS->stop to 0. */
5446
5447 static void
5448 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5449 {
5450 const struct bp_location *bl;
5451 struct breakpoint *b;
5452 int value_is_zero = 0;
5453 struct expression *cond;
5454
5455 gdb_assert (bs->stop);
5456
5457 /* BS is built for existing struct breakpoint. */
5458 bl = bs->bp_location_at;
5459 gdb_assert (bl != NULL);
5460 b = bs->breakpoint_at;
5461 gdb_assert (b != NULL);
5462
5463 /* Even if the target evaluated the condition on its end and notified GDB, we
5464 need to do so again since GDB does not know if we stopped due to a
5465 breakpoint or a single step breakpoint. */
5466
5467 if (frame_id_p (b->frame_id)
5468 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5469 {
5470 bs->stop = 0;
5471 return;
5472 }
5473
5474 /* If this is a thread/task-specific breakpoint, don't waste cpu
5475 evaluating the condition if this isn't the specified
5476 thread/task. */
5477 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5478 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5479
5480 {
5481 bs->stop = 0;
5482 return;
5483 }
5484
5485 /* Evaluate extension language breakpoints that have a "stop" method
5486 implemented. */
5487 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5488
5489 if (is_watchpoint (b))
5490 {
5491 struct watchpoint *w = (struct watchpoint *) b;
5492
5493 cond = w->cond_exp.get ();
5494 }
5495 else
5496 cond = bl->cond.get ();
5497
5498 if (cond && b->disposition != disp_del_at_next_stop)
5499 {
5500 int within_current_scope = 1;
5501 struct watchpoint * w;
5502
5503 /* We use value_mark and value_free_to_mark because it could
5504 be a long time before we return to the command level and
5505 call free_all_values. We can't call free_all_values
5506 because we might be in the middle of evaluating a
5507 function call. */
5508 struct value *mark = value_mark ();
5509
5510 if (is_watchpoint (b))
5511 w = (struct watchpoint *) b;
5512 else
5513 w = NULL;
5514
5515 /* Need to select the frame, with all that implies so that
5516 the conditions will have the right context. Because we
5517 use the frame, we will not see an inlined function's
5518 variables when we arrive at a breakpoint at the start
5519 of the inlined function; the current frame will be the
5520 call site. */
5521 if (w == NULL || w->cond_exp_valid_block == NULL)
5522 select_frame (get_current_frame ());
5523 else
5524 {
5525 struct frame_info *frame;
5526
5527 /* For local watchpoint expressions, which particular
5528 instance of a local is being watched matters, so we
5529 keep track of the frame to evaluate the expression
5530 in. To evaluate the condition however, it doesn't
5531 really matter which instantiation of the function
5532 where the condition makes sense triggers the
5533 watchpoint. This allows an expression like "watch
5534 global if q > 10" set in `func', catch writes to
5535 global on all threads that call `func', or catch
5536 writes on all recursive calls of `func' by a single
5537 thread. We simply always evaluate the condition in
5538 the innermost frame that's executing where it makes
5539 sense to evaluate the condition. It seems
5540 intuitive. */
5541 frame = block_innermost_frame (w->cond_exp_valid_block);
5542 if (frame != NULL)
5543 select_frame (frame);
5544 else
5545 within_current_scope = 0;
5546 }
5547 if (within_current_scope)
5548 value_is_zero
5549 = catch_errors (breakpoint_cond_eval, cond,
5550 "Error in testing breakpoint condition:\n",
5551 RETURN_MASK_ALL);
5552 else
5553 {
5554 warning (_("Watchpoint condition cannot be tested "
5555 "in the current scope"));
5556 /* If we failed to set the right context for this
5557 watchpoint, unconditionally report it. */
5558 value_is_zero = 0;
5559 }
5560 /* FIXME-someday, should give breakpoint #. */
5561 value_free_to_mark (mark);
5562 }
5563
5564 if (cond && value_is_zero)
5565 {
5566 bs->stop = 0;
5567 }
5568 else if (b->ignore_count > 0)
5569 {
5570 b->ignore_count--;
5571 bs->stop = 0;
5572 /* Increase the hit count even though we don't stop. */
5573 ++(b->hit_count);
5574 observer_notify_breakpoint_modified (b);
5575 }
5576 }
5577
5578 /* Returns true if we need to track moribund locations of LOC's type
5579 on the current target. */
5580
5581 static int
5582 need_moribund_for_location_type (struct bp_location *loc)
5583 {
5584 return ((loc->loc_type == bp_loc_software_breakpoint
5585 && !target_supports_stopped_by_sw_breakpoint ())
5586 || (loc->loc_type == bp_loc_hardware_breakpoint
5587 && !target_supports_stopped_by_hw_breakpoint ()));
5588 }
5589
5590
5591 /* Get a bpstat associated with having just stopped at address
5592 BP_ADDR in thread PTID.
5593
5594 Determine whether we stopped at a breakpoint, etc, or whether we
5595 don't understand this stop. Result is a chain of bpstat's such
5596 that:
5597
5598 if we don't understand the stop, the result is a null pointer.
5599
5600 if we understand why we stopped, the result is not null.
5601
5602 Each element of the chain refers to a particular breakpoint or
5603 watchpoint at which we have stopped. (We may have stopped for
5604 several reasons concurrently.)
5605
5606 Each element of the chain has valid next, breakpoint_at,
5607 commands, FIXME??? fields. */
5608
5609 bpstat
5610 bpstat_stop_status (struct address_space *aspace,
5611 CORE_ADDR bp_addr, ptid_t ptid,
5612 const struct target_waitstatus *ws)
5613 {
5614 struct breakpoint *b = NULL;
5615 struct bp_location *bl;
5616 struct bp_location *loc;
5617 /* First item of allocated bpstat's. */
5618 bpstat bs_head = NULL, *bs_link = &bs_head;
5619 /* Pointer to the last thing in the chain currently. */
5620 bpstat bs;
5621 int ix;
5622 int need_remove_insert;
5623 int removed_any;
5624
5625 /* First, build the bpstat chain with locations that explain a
5626 target stop, while being careful to not set the target running,
5627 as that may invalidate locations (in particular watchpoint
5628 locations are recreated). Resuming will happen here with
5629 breakpoint conditions or watchpoint expressions that include
5630 inferior function calls. */
5631
5632 ALL_BREAKPOINTS (b)
5633 {
5634 if (!breakpoint_enabled (b))
5635 continue;
5636
5637 for (bl = b->loc; bl != NULL; bl = bl->next)
5638 {
5639 /* For hardware watchpoints, we look only at the first
5640 location. The watchpoint_check function will work on the
5641 entire expression, not the individual locations. For
5642 read watchpoints, the watchpoints_triggered function has
5643 checked all locations already. */
5644 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5645 break;
5646
5647 if (!bl->enabled || bl->shlib_disabled)
5648 continue;
5649
5650 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5651 continue;
5652
5653 /* Come here if it's a watchpoint, or if the break address
5654 matches. */
5655
5656 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5657 explain stop. */
5658
5659 /* Assume we stop. Should we find a watchpoint that is not
5660 actually triggered, or if the condition of the breakpoint
5661 evaluates as false, we'll reset 'stop' to 0. */
5662 bs->stop = 1;
5663 bs->print = 1;
5664
5665 /* If this is a scope breakpoint, mark the associated
5666 watchpoint as triggered so that we will handle the
5667 out-of-scope event. We'll get to the watchpoint next
5668 iteration. */
5669 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5670 {
5671 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5672
5673 w->watchpoint_triggered = watch_triggered_yes;
5674 }
5675 }
5676 }
5677
5678 /* Check if a moribund breakpoint explains the stop. */
5679 if (!target_supports_stopped_by_sw_breakpoint ()
5680 || !target_supports_stopped_by_hw_breakpoint ())
5681 {
5682 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5683 {
5684 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5685 && need_moribund_for_location_type (loc))
5686 {
5687 bs = bpstat_alloc (loc, &bs_link);
5688 /* For hits of moribund locations, we should just proceed. */
5689 bs->stop = 0;
5690 bs->print = 0;
5691 bs->print_it = print_it_noop;
5692 }
5693 }
5694 }
5695
5696 /* A bit of special processing for shlib breakpoints. We need to
5697 process solib loading here, so that the lists of loaded and
5698 unloaded libraries are correct before we handle "catch load" and
5699 "catch unload". */
5700 for (bs = bs_head; bs != NULL; bs = bs->next)
5701 {
5702 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5703 {
5704 handle_solib_event ();
5705 break;
5706 }
5707 }
5708
5709 /* Now go through the locations that caused the target to stop, and
5710 check whether we're interested in reporting this stop to higher
5711 layers, or whether we should resume the target transparently. */
5712
5713 removed_any = 0;
5714
5715 for (bs = bs_head; bs != NULL; bs = bs->next)
5716 {
5717 if (!bs->stop)
5718 continue;
5719
5720 b = bs->breakpoint_at;
5721 b->ops->check_status (bs);
5722 if (bs->stop)
5723 {
5724 bpstat_check_breakpoint_conditions (bs, ptid);
5725
5726 if (bs->stop)
5727 {
5728 ++(b->hit_count);
5729 observer_notify_breakpoint_modified (b);
5730
5731 /* We will stop here. */
5732 if (b->disposition == disp_disable)
5733 {
5734 --(b->enable_count);
5735 if (b->enable_count <= 0)
5736 b->enable_state = bp_disabled;
5737 removed_any = 1;
5738 }
5739 if (b->silent)
5740 bs->print = 0;
5741 bs->commands = b->commands;
5742 incref_counted_command_line (bs->commands);
5743 if (command_line_is_silent (bs->commands
5744 ? bs->commands->commands : NULL))
5745 bs->print = 0;
5746
5747 b->ops->after_condition_true (bs);
5748 }
5749
5750 }
5751
5752 /* Print nothing for this entry if we don't stop or don't
5753 print. */
5754 if (!bs->stop || !bs->print)
5755 bs->print_it = print_it_noop;
5756 }
5757
5758 /* If we aren't stopping, the value of some hardware watchpoint may
5759 not have changed, but the intermediate memory locations we are
5760 watching may have. Don't bother if we're stopping; this will get
5761 done later. */
5762 need_remove_insert = 0;
5763 if (! bpstat_causes_stop (bs_head))
5764 for (bs = bs_head; bs != NULL; bs = bs->next)
5765 if (!bs->stop
5766 && bs->breakpoint_at
5767 && is_hardware_watchpoint (bs->breakpoint_at))
5768 {
5769 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5770
5771 update_watchpoint (w, 0 /* don't reparse. */);
5772 need_remove_insert = 1;
5773 }
5774
5775 if (need_remove_insert)
5776 update_global_location_list (UGLL_MAY_INSERT);
5777 else if (removed_any)
5778 update_global_location_list (UGLL_DONT_INSERT);
5779
5780 return bs_head;
5781 }
5782
5783 static void
5784 handle_jit_event (void)
5785 {
5786 struct frame_info *frame;
5787 struct gdbarch *gdbarch;
5788
5789 if (debug_infrun)
5790 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5791
5792 /* Switch terminal for any messages produced by
5793 breakpoint_re_set. */
5794 target_terminal_ours_for_output ();
5795
5796 frame = get_current_frame ();
5797 gdbarch = get_frame_arch (frame);
5798
5799 jit_event_handler (gdbarch);
5800
5801 target_terminal_inferior ();
5802 }
5803
5804 /* Prepare WHAT final decision for infrun. */
5805
5806 /* Decide what infrun needs to do with this bpstat. */
5807
5808 struct bpstat_what
5809 bpstat_what (bpstat bs_head)
5810 {
5811 struct bpstat_what retval;
5812 bpstat bs;
5813
5814 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5815 retval.call_dummy = STOP_NONE;
5816 retval.is_longjmp = 0;
5817
5818 for (bs = bs_head; bs != NULL; bs = bs->next)
5819 {
5820 /* Extract this BS's action. After processing each BS, we check
5821 if its action overrides all we've seem so far. */
5822 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5823 enum bptype bptype;
5824
5825 if (bs->breakpoint_at == NULL)
5826 {
5827 /* I suspect this can happen if it was a momentary
5828 breakpoint which has since been deleted. */
5829 bptype = bp_none;
5830 }
5831 else
5832 bptype = bs->breakpoint_at->type;
5833
5834 switch (bptype)
5835 {
5836 case bp_none:
5837 break;
5838 case bp_breakpoint:
5839 case bp_hardware_breakpoint:
5840 case bp_single_step:
5841 case bp_until:
5842 case bp_finish:
5843 case bp_shlib_event:
5844 if (bs->stop)
5845 {
5846 if (bs->print)
5847 this_action = BPSTAT_WHAT_STOP_NOISY;
5848 else
5849 this_action = BPSTAT_WHAT_STOP_SILENT;
5850 }
5851 else
5852 this_action = BPSTAT_WHAT_SINGLE;
5853 break;
5854 case bp_watchpoint:
5855 case bp_hardware_watchpoint:
5856 case bp_read_watchpoint:
5857 case bp_access_watchpoint:
5858 if (bs->stop)
5859 {
5860 if (bs->print)
5861 this_action = BPSTAT_WHAT_STOP_NOISY;
5862 else
5863 this_action = BPSTAT_WHAT_STOP_SILENT;
5864 }
5865 else
5866 {
5867 /* There was a watchpoint, but we're not stopping.
5868 This requires no further action. */
5869 }
5870 break;
5871 case bp_longjmp:
5872 case bp_longjmp_call_dummy:
5873 case bp_exception:
5874 if (bs->stop)
5875 {
5876 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5877 retval.is_longjmp = bptype != bp_exception;
5878 }
5879 else
5880 this_action = BPSTAT_WHAT_SINGLE;
5881 break;
5882 case bp_longjmp_resume:
5883 case bp_exception_resume:
5884 if (bs->stop)
5885 {
5886 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5887 retval.is_longjmp = bptype == bp_longjmp_resume;
5888 }
5889 else
5890 this_action = BPSTAT_WHAT_SINGLE;
5891 break;
5892 case bp_step_resume:
5893 if (bs->stop)
5894 this_action = BPSTAT_WHAT_STEP_RESUME;
5895 else
5896 {
5897 /* It is for the wrong frame. */
5898 this_action = BPSTAT_WHAT_SINGLE;
5899 }
5900 break;
5901 case bp_hp_step_resume:
5902 if (bs->stop)
5903 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5904 else
5905 {
5906 /* It is for the wrong frame. */
5907 this_action = BPSTAT_WHAT_SINGLE;
5908 }
5909 break;
5910 case bp_watchpoint_scope:
5911 case bp_thread_event:
5912 case bp_overlay_event:
5913 case bp_longjmp_master:
5914 case bp_std_terminate_master:
5915 case bp_exception_master:
5916 this_action = BPSTAT_WHAT_SINGLE;
5917 break;
5918 case bp_catchpoint:
5919 if (bs->stop)
5920 {
5921 if (bs->print)
5922 this_action = BPSTAT_WHAT_STOP_NOISY;
5923 else
5924 this_action = BPSTAT_WHAT_STOP_SILENT;
5925 }
5926 else
5927 {
5928 /* There was a catchpoint, but we're not stopping.
5929 This requires no further action. */
5930 }
5931 break;
5932 case bp_jit_event:
5933 this_action = BPSTAT_WHAT_SINGLE;
5934 break;
5935 case bp_call_dummy:
5936 /* Make sure the action is stop (silent or noisy),
5937 so infrun.c pops the dummy frame. */
5938 retval.call_dummy = STOP_STACK_DUMMY;
5939 this_action = BPSTAT_WHAT_STOP_SILENT;
5940 break;
5941 case bp_std_terminate:
5942 /* Make sure the action is stop (silent or noisy),
5943 so infrun.c pops the dummy frame. */
5944 retval.call_dummy = STOP_STD_TERMINATE;
5945 this_action = BPSTAT_WHAT_STOP_SILENT;
5946 break;
5947 case bp_tracepoint:
5948 case bp_fast_tracepoint:
5949 case bp_static_tracepoint:
5950 /* Tracepoint hits should not be reported back to GDB, and
5951 if one got through somehow, it should have been filtered
5952 out already. */
5953 internal_error (__FILE__, __LINE__,
5954 _("bpstat_what: tracepoint encountered"));
5955 break;
5956 case bp_gnu_ifunc_resolver:
5957 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5958 this_action = BPSTAT_WHAT_SINGLE;
5959 break;
5960 case bp_gnu_ifunc_resolver_return:
5961 /* The breakpoint will be removed, execution will restart from the
5962 PC of the former breakpoint. */
5963 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5964 break;
5965
5966 case bp_dprintf:
5967 if (bs->stop)
5968 this_action = BPSTAT_WHAT_STOP_SILENT;
5969 else
5970 this_action = BPSTAT_WHAT_SINGLE;
5971 break;
5972
5973 default:
5974 internal_error (__FILE__, __LINE__,
5975 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5976 }
5977
5978 retval.main_action = std::max (retval.main_action, this_action);
5979 }
5980
5981 return retval;
5982 }
5983
5984 void
5985 bpstat_run_callbacks (bpstat bs_head)
5986 {
5987 bpstat bs;
5988
5989 for (bs = bs_head; bs != NULL; bs = bs->next)
5990 {
5991 struct breakpoint *b = bs->breakpoint_at;
5992
5993 if (b == NULL)
5994 continue;
5995 switch (b->type)
5996 {
5997 case bp_jit_event:
5998 handle_jit_event ();
5999 break;
6000 case bp_gnu_ifunc_resolver:
6001 gnu_ifunc_resolver_stop (b);
6002 break;
6003 case bp_gnu_ifunc_resolver_return:
6004 gnu_ifunc_resolver_return_stop (b);
6005 break;
6006 }
6007 }
6008 }
6009
6010 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6011 without hardware support). This isn't related to a specific bpstat,
6012 just to things like whether watchpoints are set. */
6013
6014 int
6015 bpstat_should_step (void)
6016 {
6017 struct breakpoint *b;
6018
6019 ALL_BREAKPOINTS (b)
6020 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6021 return 1;
6022 return 0;
6023 }
6024
6025 int
6026 bpstat_causes_stop (bpstat bs)
6027 {
6028 for (; bs != NULL; bs = bs->next)
6029 if (bs->stop)
6030 return 1;
6031
6032 return 0;
6033 }
6034
6035 \f
6036
6037 /* Compute a string of spaces suitable to indent the next line
6038 so it starts at the position corresponding to the table column
6039 named COL_NAME in the currently active table of UIOUT. */
6040
6041 static char *
6042 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6043 {
6044 static char wrap_indent[80];
6045 int i, total_width, width, align;
6046 const char *text;
6047
6048 total_width = 0;
6049 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6050 {
6051 if (strcmp (text, col_name) == 0)
6052 {
6053 gdb_assert (total_width < sizeof wrap_indent);
6054 memset (wrap_indent, ' ', total_width);
6055 wrap_indent[total_width] = 0;
6056
6057 return wrap_indent;
6058 }
6059
6060 total_width += width + 1;
6061 }
6062
6063 return NULL;
6064 }
6065
6066 /* Determine if the locations of this breakpoint will have their conditions
6067 evaluated by the target, host or a mix of both. Returns the following:
6068
6069 "host": Host evals condition.
6070 "host or target": Host or Target evals condition.
6071 "target": Target evals condition.
6072 */
6073
6074 static const char *
6075 bp_condition_evaluator (struct breakpoint *b)
6076 {
6077 struct bp_location *bl;
6078 char host_evals = 0;
6079 char target_evals = 0;
6080
6081 if (!b)
6082 return NULL;
6083
6084 if (!is_breakpoint (b))
6085 return NULL;
6086
6087 if (gdb_evaluates_breakpoint_condition_p ()
6088 || !target_supports_evaluation_of_breakpoint_conditions ())
6089 return condition_evaluation_host;
6090
6091 for (bl = b->loc; bl; bl = bl->next)
6092 {
6093 if (bl->cond_bytecode)
6094 target_evals++;
6095 else
6096 host_evals++;
6097 }
6098
6099 if (host_evals && target_evals)
6100 return condition_evaluation_both;
6101 else if (target_evals)
6102 return condition_evaluation_target;
6103 else
6104 return condition_evaluation_host;
6105 }
6106
6107 /* Determine the breakpoint location's condition evaluator. This is
6108 similar to bp_condition_evaluator, but for locations. */
6109
6110 static const char *
6111 bp_location_condition_evaluator (struct bp_location *bl)
6112 {
6113 if (bl && !is_breakpoint (bl->owner))
6114 return NULL;
6115
6116 if (gdb_evaluates_breakpoint_condition_p ()
6117 || !target_supports_evaluation_of_breakpoint_conditions ())
6118 return condition_evaluation_host;
6119
6120 if (bl && bl->cond_bytecode)
6121 return condition_evaluation_target;
6122 else
6123 return condition_evaluation_host;
6124 }
6125
6126 /* Print the LOC location out of the list of B->LOC locations. */
6127
6128 static void
6129 print_breakpoint_location (struct breakpoint *b,
6130 struct bp_location *loc)
6131 {
6132 struct ui_out *uiout = current_uiout;
6133
6134 scoped_restore_current_program_space restore_pspace;
6135
6136 if (loc != NULL && loc->shlib_disabled)
6137 loc = NULL;
6138
6139 if (loc != NULL)
6140 set_current_program_space (loc->pspace);
6141
6142 if (b->display_canonical)
6143 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6144 else if (loc && loc->symtab)
6145 {
6146 struct symbol *sym
6147 = find_pc_sect_function (loc->address, loc->section);
6148 if (sym)
6149 {
6150 uiout->text ("in ");
6151 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6152 uiout->text (" ");
6153 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6154 uiout->text ("at ");
6155 }
6156 uiout->field_string ("file",
6157 symtab_to_filename_for_display (loc->symtab));
6158 uiout->text (":");
6159
6160 if (uiout->is_mi_like_p ())
6161 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6162
6163 uiout->field_int ("line", loc->line_number);
6164 }
6165 else if (loc)
6166 {
6167 string_file stb;
6168
6169 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6170 demangle, "");
6171 uiout->field_stream ("at", stb);
6172 }
6173 else
6174 {
6175 uiout->field_string ("pending",
6176 event_location_to_string (b->location.get ()));
6177 /* If extra_string is available, it could be holding a condition
6178 or dprintf arguments. In either case, make sure it is printed,
6179 too, but only for non-MI streams. */
6180 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6181 {
6182 if (b->type == bp_dprintf)
6183 uiout->text (",");
6184 else
6185 uiout->text (" ");
6186 uiout->text (b->extra_string);
6187 }
6188 }
6189
6190 if (loc && is_breakpoint (b)
6191 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6192 && bp_condition_evaluator (b) == condition_evaluation_both)
6193 {
6194 uiout->text (" (");
6195 uiout->field_string ("evaluated-by",
6196 bp_location_condition_evaluator (loc));
6197 uiout->text (")");
6198 }
6199 }
6200
6201 static const char *
6202 bptype_string (enum bptype type)
6203 {
6204 struct ep_type_description
6205 {
6206 enum bptype type;
6207 const char *description;
6208 };
6209 static struct ep_type_description bptypes[] =
6210 {
6211 {bp_none, "?deleted?"},
6212 {bp_breakpoint, "breakpoint"},
6213 {bp_hardware_breakpoint, "hw breakpoint"},
6214 {bp_single_step, "sw single-step"},
6215 {bp_until, "until"},
6216 {bp_finish, "finish"},
6217 {bp_watchpoint, "watchpoint"},
6218 {bp_hardware_watchpoint, "hw watchpoint"},
6219 {bp_read_watchpoint, "read watchpoint"},
6220 {bp_access_watchpoint, "acc watchpoint"},
6221 {bp_longjmp, "longjmp"},
6222 {bp_longjmp_resume, "longjmp resume"},
6223 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6224 {bp_exception, "exception"},
6225 {bp_exception_resume, "exception resume"},
6226 {bp_step_resume, "step resume"},
6227 {bp_hp_step_resume, "high-priority step resume"},
6228 {bp_watchpoint_scope, "watchpoint scope"},
6229 {bp_call_dummy, "call dummy"},
6230 {bp_std_terminate, "std::terminate"},
6231 {bp_shlib_event, "shlib events"},
6232 {bp_thread_event, "thread events"},
6233 {bp_overlay_event, "overlay events"},
6234 {bp_longjmp_master, "longjmp master"},
6235 {bp_std_terminate_master, "std::terminate master"},
6236 {bp_exception_master, "exception master"},
6237 {bp_catchpoint, "catchpoint"},
6238 {bp_tracepoint, "tracepoint"},
6239 {bp_fast_tracepoint, "fast tracepoint"},
6240 {bp_static_tracepoint, "static tracepoint"},
6241 {bp_dprintf, "dprintf"},
6242 {bp_jit_event, "jit events"},
6243 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6244 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6245 };
6246
6247 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6248 || ((int) type != bptypes[(int) type].type))
6249 internal_error (__FILE__, __LINE__,
6250 _("bptypes table does not describe type #%d."),
6251 (int) type);
6252
6253 return bptypes[(int) type].description;
6254 }
6255
6256 /* For MI, output a field named 'thread-groups' with a list as the value.
6257 For CLI, prefix the list with the string 'inf'. */
6258
6259 static void
6260 output_thread_groups (struct ui_out *uiout,
6261 const char *field_name,
6262 VEC(int) *inf_num,
6263 int mi_only)
6264 {
6265 int is_mi = uiout->is_mi_like_p ();
6266 int inf;
6267 int i;
6268
6269 /* For backward compatibility, don't display inferiors in CLI unless
6270 there are several. Always display them for MI. */
6271 if (!is_mi && mi_only)
6272 return;
6273
6274 ui_out_emit_list list_emitter (uiout, field_name);
6275
6276 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6277 {
6278 if (is_mi)
6279 {
6280 char mi_group[10];
6281
6282 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6283 uiout->field_string (NULL, mi_group);
6284 }
6285 else
6286 {
6287 if (i == 0)
6288 uiout->text (" inf ");
6289 else
6290 uiout->text (", ");
6291
6292 uiout->text (plongest (inf));
6293 }
6294 }
6295 }
6296
6297 /* Print B to gdb_stdout. */
6298
6299 static void
6300 print_one_breakpoint_location (struct breakpoint *b,
6301 struct bp_location *loc,
6302 int loc_number,
6303 struct bp_location **last_loc,
6304 int allflag)
6305 {
6306 struct command_line *l;
6307 static char bpenables[] = "nynny";
6308
6309 struct ui_out *uiout = current_uiout;
6310 int header_of_multiple = 0;
6311 int part_of_multiple = (loc != NULL);
6312 struct value_print_options opts;
6313
6314 get_user_print_options (&opts);
6315
6316 gdb_assert (!loc || loc_number != 0);
6317 /* See comment in print_one_breakpoint concerning treatment of
6318 breakpoints with single disabled location. */
6319 if (loc == NULL
6320 && (b->loc != NULL
6321 && (b->loc->next != NULL || !b->loc->enabled)))
6322 header_of_multiple = 1;
6323 if (loc == NULL)
6324 loc = b->loc;
6325
6326 annotate_record ();
6327
6328 /* 1 */
6329 annotate_field (0);
6330 if (part_of_multiple)
6331 {
6332 char *formatted;
6333 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6334 uiout->field_string ("number", formatted);
6335 xfree (formatted);
6336 }
6337 else
6338 {
6339 uiout->field_int ("number", b->number);
6340 }
6341
6342 /* 2 */
6343 annotate_field (1);
6344 if (part_of_multiple)
6345 uiout->field_skip ("type");
6346 else
6347 uiout->field_string ("type", bptype_string (b->type));
6348
6349 /* 3 */
6350 annotate_field (2);
6351 if (part_of_multiple)
6352 uiout->field_skip ("disp");
6353 else
6354 uiout->field_string ("disp", bpdisp_text (b->disposition));
6355
6356
6357 /* 4 */
6358 annotate_field (3);
6359 if (part_of_multiple)
6360 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6361 else
6362 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6363 uiout->spaces (2);
6364
6365
6366 /* 5 and 6 */
6367 if (b->ops != NULL && b->ops->print_one != NULL)
6368 {
6369 /* Although the print_one can possibly print all locations,
6370 calling it here is not likely to get any nice result. So,
6371 make sure there's just one location. */
6372 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6373 b->ops->print_one (b, last_loc);
6374 }
6375 else
6376 switch (b->type)
6377 {
6378 case bp_none:
6379 internal_error (__FILE__, __LINE__,
6380 _("print_one_breakpoint: bp_none encountered\n"));
6381 break;
6382
6383 case bp_watchpoint:
6384 case bp_hardware_watchpoint:
6385 case bp_read_watchpoint:
6386 case bp_access_watchpoint:
6387 {
6388 struct watchpoint *w = (struct watchpoint *) b;
6389
6390 /* Field 4, the address, is omitted (which makes the columns
6391 not line up too nicely with the headers, but the effect
6392 is relatively readable). */
6393 if (opts.addressprint)
6394 uiout->field_skip ("addr");
6395 annotate_field (5);
6396 uiout->field_string ("what", w->exp_string);
6397 }
6398 break;
6399
6400 case bp_breakpoint:
6401 case bp_hardware_breakpoint:
6402 case bp_single_step:
6403 case bp_until:
6404 case bp_finish:
6405 case bp_longjmp:
6406 case bp_longjmp_resume:
6407 case bp_longjmp_call_dummy:
6408 case bp_exception:
6409 case bp_exception_resume:
6410 case bp_step_resume:
6411 case bp_hp_step_resume:
6412 case bp_watchpoint_scope:
6413 case bp_call_dummy:
6414 case bp_std_terminate:
6415 case bp_shlib_event:
6416 case bp_thread_event:
6417 case bp_overlay_event:
6418 case bp_longjmp_master:
6419 case bp_std_terminate_master:
6420 case bp_exception_master:
6421 case bp_tracepoint:
6422 case bp_fast_tracepoint:
6423 case bp_static_tracepoint:
6424 case bp_dprintf:
6425 case bp_jit_event:
6426 case bp_gnu_ifunc_resolver:
6427 case bp_gnu_ifunc_resolver_return:
6428 if (opts.addressprint)
6429 {
6430 annotate_field (4);
6431 if (header_of_multiple)
6432 uiout->field_string ("addr", "<MULTIPLE>");
6433 else if (b->loc == NULL || loc->shlib_disabled)
6434 uiout->field_string ("addr", "<PENDING>");
6435 else
6436 uiout->field_core_addr ("addr",
6437 loc->gdbarch, loc->address);
6438 }
6439 annotate_field (5);
6440 if (!header_of_multiple)
6441 print_breakpoint_location (b, loc);
6442 if (b->loc)
6443 *last_loc = b->loc;
6444 break;
6445 }
6446
6447
6448 if (loc != NULL && !header_of_multiple)
6449 {
6450 struct inferior *inf;
6451 VEC(int) *inf_num = NULL;
6452 int mi_only = 1;
6453
6454 ALL_INFERIORS (inf)
6455 {
6456 if (inf->pspace == loc->pspace)
6457 VEC_safe_push (int, inf_num, inf->num);
6458 }
6459
6460 /* For backward compatibility, don't display inferiors in CLI unless
6461 there are several. Always display for MI. */
6462 if (allflag
6463 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6464 && (number_of_program_spaces () > 1
6465 || number_of_inferiors () > 1)
6466 /* LOC is for existing B, it cannot be in
6467 moribund_locations and thus having NULL OWNER. */
6468 && loc->owner->type != bp_catchpoint))
6469 mi_only = 0;
6470 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6471 VEC_free (int, inf_num);
6472 }
6473
6474 if (!part_of_multiple)
6475 {
6476 if (b->thread != -1)
6477 {
6478 /* FIXME: This seems to be redundant and lost here; see the
6479 "stop only in" line a little further down. */
6480 uiout->text (" thread ");
6481 uiout->field_int ("thread", b->thread);
6482 }
6483 else if (b->task != 0)
6484 {
6485 uiout->text (" task ");
6486 uiout->field_int ("task", b->task);
6487 }
6488 }
6489
6490 uiout->text ("\n");
6491
6492 if (!part_of_multiple)
6493 b->ops->print_one_detail (b, uiout);
6494
6495 if (part_of_multiple && frame_id_p (b->frame_id))
6496 {
6497 annotate_field (6);
6498 uiout->text ("\tstop only in stack frame at ");
6499 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6500 the frame ID. */
6501 uiout->field_core_addr ("frame",
6502 b->gdbarch, b->frame_id.stack_addr);
6503 uiout->text ("\n");
6504 }
6505
6506 if (!part_of_multiple && b->cond_string)
6507 {
6508 annotate_field (7);
6509 if (is_tracepoint (b))
6510 uiout->text ("\ttrace only if ");
6511 else
6512 uiout->text ("\tstop only if ");
6513 uiout->field_string ("cond", b->cond_string);
6514
6515 /* Print whether the target is doing the breakpoint's condition
6516 evaluation. If GDB is doing the evaluation, don't print anything. */
6517 if (is_breakpoint (b)
6518 && breakpoint_condition_evaluation_mode ()
6519 == condition_evaluation_target)
6520 {
6521 uiout->text (" (");
6522 uiout->field_string ("evaluated-by",
6523 bp_condition_evaluator (b));
6524 uiout->text (" evals)");
6525 }
6526 uiout->text ("\n");
6527 }
6528
6529 if (!part_of_multiple && b->thread != -1)
6530 {
6531 /* FIXME should make an annotation for this. */
6532 uiout->text ("\tstop only in thread ");
6533 if (uiout->is_mi_like_p ())
6534 uiout->field_int ("thread", b->thread);
6535 else
6536 {
6537 struct thread_info *thr = find_thread_global_id (b->thread);
6538
6539 uiout->field_string ("thread", print_thread_id (thr));
6540 }
6541 uiout->text ("\n");
6542 }
6543
6544 if (!part_of_multiple)
6545 {
6546 if (b->hit_count)
6547 {
6548 /* FIXME should make an annotation for this. */
6549 if (is_catchpoint (b))
6550 uiout->text ("\tcatchpoint");
6551 else if (is_tracepoint (b))
6552 uiout->text ("\ttracepoint");
6553 else
6554 uiout->text ("\tbreakpoint");
6555 uiout->text (" already hit ");
6556 uiout->field_int ("times", b->hit_count);
6557 if (b->hit_count == 1)
6558 uiout->text (" time\n");
6559 else
6560 uiout->text (" times\n");
6561 }
6562 else
6563 {
6564 /* Output the count also if it is zero, but only if this is mi. */
6565 if (uiout->is_mi_like_p ())
6566 uiout->field_int ("times", b->hit_count);
6567 }
6568 }
6569
6570 if (!part_of_multiple && b->ignore_count)
6571 {
6572 annotate_field (8);
6573 uiout->text ("\tignore next ");
6574 uiout->field_int ("ignore", b->ignore_count);
6575 uiout->text (" hits\n");
6576 }
6577
6578 /* Note that an enable count of 1 corresponds to "enable once"
6579 behavior, which is reported by the combination of enablement and
6580 disposition, so we don't need to mention it here. */
6581 if (!part_of_multiple && b->enable_count > 1)
6582 {
6583 annotate_field (8);
6584 uiout->text ("\tdisable after ");
6585 /* Tweak the wording to clarify that ignore and enable counts
6586 are distinct, and have additive effect. */
6587 if (b->ignore_count)
6588 uiout->text ("additional ");
6589 else
6590 uiout->text ("next ");
6591 uiout->field_int ("enable", b->enable_count);
6592 uiout->text (" hits\n");
6593 }
6594
6595 if (!part_of_multiple && is_tracepoint (b))
6596 {
6597 struct tracepoint *tp = (struct tracepoint *) b;
6598
6599 if (tp->traceframe_usage)
6600 {
6601 uiout->text ("\ttrace buffer usage ");
6602 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6603 uiout->text (" bytes\n");
6604 }
6605 }
6606
6607 l = b->commands ? b->commands->commands : NULL;
6608 if (!part_of_multiple && l)
6609 {
6610 annotate_field (9);
6611 ui_out_emit_tuple tuple_emitter (uiout, "script");
6612 print_command_lines (uiout, l, 4);
6613 }
6614
6615 if (is_tracepoint (b))
6616 {
6617 struct tracepoint *t = (struct tracepoint *) b;
6618
6619 if (!part_of_multiple && t->pass_count)
6620 {
6621 annotate_field (10);
6622 uiout->text ("\tpass count ");
6623 uiout->field_int ("pass", t->pass_count);
6624 uiout->text (" \n");
6625 }
6626
6627 /* Don't display it when tracepoint or tracepoint location is
6628 pending. */
6629 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6630 {
6631 annotate_field (11);
6632
6633 if (uiout->is_mi_like_p ())
6634 uiout->field_string ("installed",
6635 loc->inserted ? "y" : "n");
6636 else
6637 {
6638 if (loc->inserted)
6639 uiout->text ("\t");
6640 else
6641 uiout->text ("\tnot ");
6642 uiout->text ("installed on target\n");
6643 }
6644 }
6645 }
6646
6647 if (uiout->is_mi_like_p () && !part_of_multiple)
6648 {
6649 if (is_watchpoint (b))
6650 {
6651 struct watchpoint *w = (struct watchpoint *) b;
6652
6653 uiout->field_string ("original-location", w->exp_string);
6654 }
6655 else if (b->location != NULL
6656 && event_location_to_string (b->location.get ()) != NULL)
6657 uiout->field_string ("original-location",
6658 event_location_to_string (b->location.get ()));
6659 }
6660 }
6661
6662 static void
6663 print_one_breakpoint (struct breakpoint *b,
6664 struct bp_location **last_loc,
6665 int allflag)
6666 {
6667 struct ui_out *uiout = current_uiout;
6668
6669 {
6670 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6671
6672 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6673 }
6674
6675 /* If this breakpoint has custom print function,
6676 it's already printed. Otherwise, print individual
6677 locations, if any. */
6678 if (b->ops == NULL || b->ops->print_one == NULL)
6679 {
6680 /* If breakpoint has a single location that is disabled, we
6681 print it as if it had several locations, since otherwise it's
6682 hard to represent "breakpoint enabled, location disabled"
6683 situation.
6684
6685 Note that while hardware watchpoints have several locations
6686 internally, that's not a property exposed to user. */
6687 if (b->loc
6688 && !is_hardware_watchpoint (b)
6689 && (b->loc->next || !b->loc->enabled))
6690 {
6691 struct bp_location *loc;
6692 int n = 1;
6693
6694 for (loc = b->loc; loc; loc = loc->next, ++n)
6695 {
6696 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6697 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6698 }
6699 }
6700 }
6701 }
6702
6703 static int
6704 breakpoint_address_bits (struct breakpoint *b)
6705 {
6706 int print_address_bits = 0;
6707 struct bp_location *loc;
6708
6709 /* Software watchpoints that aren't watching memory don't have an
6710 address to print. */
6711 if (is_no_memory_software_watchpoint (b))
6712 return 0;
6713
6714 for (loc = b->loc; loc; loc = loc->next)
6715 {
6716 int addr_bit;
6717
6718 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6719 if (addr_bit > print_address_bits)
6720 print_address_bits = addr_bit;
6721 }
6722
6723 return print_address_bits;
6724 }
6725
6726 struct captured_breakpoint_query_args
6727 {
6728 int bnum;
6729 };
6730
6731 static int
6732 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6733 {
6734 struct captured_breakpoint_query_args *args
6735 = (struct captured_breakpoint_query_args *) data;
6736 struct breakpoint *b;
6737 struct bp_location *dummy_loc = NULL;
6738
6739 ALL_BREAKPOINTS (b)
6740 {
6741 if (args->bnum == b->number)
6742 {
6743 print_one_breakpoint (b, &dummy_loc, 0);
6744 return GDB_RC_OK;
6745 }
6746 }
6747 return GDB_RC_NONE;
6748 }
6749
6750 enum gdb_rc
6751 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6752 char **error_message)
6753 {
6754 struct captured_breakpoint_query_args args;
6755
6756 args.bnum = bnum;
6757 /* For the moment we don't trust print_one_breakpoint() to not throw
6758 an error. */
6759 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6760 error_message, RETURN_MASK_ALL) < 0)
6761 return GDB_RC_FAIL;
6762 else
6763 return GDB_RC_OK;
6764 }
6765
6766 /* Return true if this breakpoint was set by the user, false if it is
6767 internal or momentary. */
6768
6769 int
6770 user_breakpoint_p (struct breakpoint *b)
6771 {
6772 return b->number > 0;
6773 }
6774
6775 /* See breakpoint.h. */
6776
6777 int
6778 pending_breakpoint_p (struct breakpoint *b)
6779 {
6780 return b->loc == NULL;
6781 }
6782
6783 /* Print information on user settable breakpoint (watchpoint, etc)
6784 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6785 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6786 FILTER is non-NULL, call it on each breakpoint and only include the
6787 ones for which it returns non-zero. Return the total number of
6788 breakpoints listed. */
6789
6790 static int
6791 breakpoint_1 (char *args, int allflag,
6792 int (*filter) (const struct breakpoint *))
6793 {
6794 struct breakpoint *b;
6795 struct bp_location *last_loc = NULL;
6796 int nr_printable_breakpoints;
6797 struct value_print_options opts;
6798 int print_address_bits = 0;
6799 int print_type_col_width = 14;
6800 struct ui_out *uiout = current_uiout;
6801
6802 get_user_print_options (&opts);
6803
6804 /* Compute the number of rows in the table, as well as the size
6805 required for address fields. */
6806 nr_printable_breakpoints = 0;
6807 ALL_BREAKPOINTS (b)
6808 {
6809 /* If we have a filter, only list the breakpoints it accepts. */
6810 if (filter && !filter (b))
6811 continue;
6812
6813 /* If we have an "args" string, it is a list of breakpoints to
6814 accept. Skip the others. */
6815 if (args != NULL && *args != '\0')
6816 {
6817 if (allflag && parse_and_eval_long (args) != b->number)
6818 continue;
6819 if (!allflag && !number_is_in_list (args, b->number))
6820 continue;
6821 }
6822
6823 if (allflag || user_breakpoint_p (b))
6824 {
6825 int addr_bit, type_len;
6826
6827 addr_bit = breakpoint_address_bits (b);
6828 if (addr_bit > print_address_bits)
6829 print_address_bits = addr_bit;
6830
6831 type_len = strlen (bptype_string (b->type));
6832 if (type_len > print_type_col_width)
6833 print_type_col_width = type_len;
6834
6835 nr_printable_breakpoints++;
6836 }
6837 }
6838
6839 {
6840 ui_out_emit_table table_emitter (uiout,
6841 opts.addressprint ? 6 : 5,
6842 nr_printable_breakpoints,
6843 "BreakpointTable");
6844
6845 if (nr_printable_breakpoints > 0)
6846 annotate_breakpoints_headers ();
6847 if (nr_printable_breakpoints > 0)
6848 annotate_field (0);
6849 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6850 if (nr_printable_breakpoints > 0)
6851 annotate_field (1);
6852 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6853 if (nr_printable_breakpoints > 0)
6854 annotate_field (2);
6855 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6856 if (nr_printable_breakpoints > 0)
6857 annotate_field (3);
6858 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6859 if (opts.addressprint)
6860 {
6861 if (nr_printable_breakpoints > 0)
6862 annotate_field (4);
6863 if (print_address_bits <= 32)
6864 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6865 else
6866 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6867 }
6868 if (nr_printable_breakpoints > 0)
6869 annotate_field (5);
6870 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6871 uiout->table_body ();
6872 if (nr_printable_breakpoints > 0)
6873 annotate_breakpoints_table ();
6874
6875 ALL_BREAKPOINTS (b)
6876 {
6877 QUIT;
6878 /* If we have a filter, only list the breakpoints it accepts. */
6879 if (filter && !filter (b))
6880 continue;
6881
6882 /* If we have an "args" string, it is a list of breakpoints to
6883 accept. Skip the others. */
6884
6885 if (args != NULL && *args != '\0')
6886 {
6887 if (allflag) /* maintenance info breakpoint */
6888 {
6889 if (parse_and_eval_long (args) != b->number)
6890 continue;
6891 }
6892 else /* all others */
6893 {
6894 if (!number_is_in_list (args, b->number))
6895 continue;
6896 }
6897 }
6898 /* We only print out user settable breakpoints unless the
6899 allflag is set. */
6900 if (allflag || user_breakpoint_p (b))
6901 print_one_breakpoint (b, &last_loc, allflag);
6902 }
6903 }
6904
6905 if (nr_printable_breakpoints == 0)
6906 {
6907 /* If there's a filter, let the caller decide how to report
6908 empty list. */
6909 if (!filter)
6910 {
6911 if (args == NULL || *args == '\0')
6912 uiout->message ("No breakpoints or watchpoints.\n");
6913 else
6914 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6915 args);
6916 }
6917 }
6918 else
6919 {
6920 if (last_loc && !server_command)
6921 set_next_address (last_loc->gdbarch, last_loc->address);
6922 }
6923
6924 /* FIXME? Should this be moved up so that it is only called when
6925 there have been breakpoints? */
6926 annotate_breakpoints_table_end ();
6927
6928 return nr_printable_breakpoints;
6929 }
6930
6931 /* Display the value of default-collect in a way that is generally
6932 compatible with the breakpoint list. */
6933
6934 static void
6935 default_collect_info (void)
6936 {
6937 struct ui_out *uiout = current_uiout;
6938
6939 /* If it has no value (which is frequently the case), say nothing; a
6940 message like "No default-collect." gets in user's face when it's
6941 not wanted. */
6942 if (!*default_collect)
6943 return;
6944
6945 /* The following phrase lines up nicely with per-tracepoint collect
6946 actions. */
6947 uiout->text ("default collect ");
6948 uiout->field_string ("default-collect", default_collect);
6949 uiout->text (" \n");
6950 }
6951
6952 static void
6953 info_breakpoints_command (char *args, int from_tty)
6954 {
6955 breakpoint_1 (args, 0, NULL);
6956
6957 default_collect_info ();
6958 }
6959
6960 static void
6961 info_watchpoints_command (char *args, int from_tty)
6962 {
6963 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6964 struct ui_out *uiout = current_uiout;
6965
6966 if (num_printed == 0)
6967 {
6968 if (args == NULL || *args == '\0')
6969 uiout->message ("No watchpoints.\n");
6970 else
6971 uiout->message ("No watchpoint matching '%s'.\n", args);
6972 }
6973 }
6974
6975 static void
6976 maintenance_info_breakpoints (char *args, int from_tty)
6977 {
6978 breakpoint_1 (args, 1, NULL);
6979
6980 default_collect_info ();
6981 }
6982
6983 static int
6984 breakpoint_has_pc (struct breakpoint *b,
6985 struct program_space *pspace,
6986 CORE_ADDR pc, struct obj_section *section)
6987 {
6988 struct bp_location *bl = b->loc;
6989
6990 for (; bl; bl = bl->next)
6991 {
6992 if (bl->pspace == pspace
6993 && bl->address == pc
6994 && (!overlay_debugging || bl->section == section))
6995 return 1;
6996 }
6997 return 0;
6998 }
6999
7000 /* Print a message describing any user-breakpoints set at PC. This
7001 concerns with logical breakpoints, so we match program spaces, not
7002 address spaces. */
7003
7004 static void
7005 describe_other_breakpoints (struct gdbarch *gdbarch,
7006 struct program_space *pspace, CORE_ADDR pc,
7007 struct obj_section *section, int thread)
7008 {
7009 int others = 0;
7010 struct breakpoint *b;
7011
7012 ALL_BREAKPOINTS (b)
7013 others += (user_breakpoint_p (b)
7014 && breakpoint_has_pc (b, pspace, pc, section));
7015 if (others > 0)
7016 {
7017 if (others == 1)
7018 printf_filtered (_("Note: breakpoint "));
7019 else /* if (others == ???) */
7020 printf_filtered (_("Note: breakpoints "));
7021 ALL_BREAKPOINTS (b)
7022 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7023 {
7024 others--;
7025 printf_filtered ("%d", b->number);
7026 if (b->thread == -1 && thread != -1)
7027 printf_filtered (" (all threads)");
7028 else if (b->thread != -1)
7029 printf_filtered (" (thread %d)", b->thread);
7030 printf_filtered ("%s%s ",
7031 ((b->enable_state == bp_disabled
7032 || b->enable_state == bp_call_disabled)
7033 ? " (disabled)"
7034 : ""),
7035 (others > 1) ? ","
7036 : ((others == 1) ? " and" : ""));
7037 }
7038 printf_filtered (_("also set at pc "));
7039 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7040 printf_filtered (".\n");
7041 }
7042 }
7043 \f
7044
7045 /* Return true iff it is meaningful to use the address member of
7046 BPT locations. For some breakpoint types, the locations' address members
7047 are irrelevant and it makes no sense to attempt to compare them to other
7048 addresses (or use them for any other purpose either).
7049
7050 More specifically, each of the following breakpoint types will
7051 always have a zero valued location address and we don't want to mark
7052 breakpoints of any of these types to be a duplicate of an actual
7053 breakpoint location at address zero:
7054
7055 bp_watchpoint
7056 bp_catchpoint
7057
7058 */
7059
7060 static int
7061 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7062 {
7063 enum bptype type = bpt->type;
7064
7065 return (type != bp_watchpoint && type != bp_catchpoint);
7066 }
7067
7068 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7069 true if LOC1 and LOC2 represent the same watchpoint location. */
7070
7071 static int
7072 watchpoint_locations_match (struct bp_location *loc1,
7073 struct bp_location *loc2)
7074 {
7075 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7076 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7077
7078 /* Both of them must exist. */
7079 gdb_assert (w1 != NULL);
7080 gdb_assert (w2 != NULL);
7081
7082 /* If the target can evaluate the condition expression in hardware,
7083 then we we need to insert both watchpoints even if they are at
7084 the same place. Otherwise the watchpoint will only trigger when
7085 the condition of whichever watchpoint was inserted evaluates to
7086 true, not giving a chance for GDB to check the condition of the
7087 other watchpoint. */
7088 if ((w1->cond_exp
7089 && target_can_accel_watchpoint_condition (loc1->address,
7090 loc1->length,
7091 loc1->watchpoint_type,
7092 w1->cond_exp.get ()))
7093 || (w2->cond_exp
7094 && target_can_accel_watchpoint_condition (loc2->address,
7095 loc2->length,
7096 loc2->watchpoint_type,
7097 w2->cond_exp.get ())))
7098 return 0;
7099
7100 /* Note that this checks the owner's type, not the location's. In
7101 case the target does not support read watchpoints, but does
7102 support access watchpoints, we'll have bp_read_watchpoint
7103 watchpoints with hw_access locations. Those should be considered
7104 duplicates of hw_read locations. The hw_read locations will
7105 become hw_access locations later. */
7106 return (loc1->owner->type == loc2->owner->type
7107 && loc1->pspace->aspace == loc2->pspace->aspace
7108 && loc1->address == loc2->address
7109 && loc1->length == loc2->length);
7110 }
7111
7112 /* See breakpoint.h. */
7113
7114 int
7115 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7116 struct address_space *aspace2, CORE_ADDR addr2)
7117 {
7118 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7119 || aspace1 == aspace2)
7120 && addr1 == addr2);
7121 }
7122
7123 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7124 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7125 matches ASPACE2. On targets that have global breakpoints, the address
7126 space doesn't really matter. */
7127
7128 static int
7129 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7130 int len1, struct address_space *aspace2,
7131 CORE_ADDR addr2)
7132 {
7133 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7134 || aspace1 == aspace2)
7135 && addr2 >= addr1 && addr2 < addr1 + len1);
7136 }
7137
7138 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7139 a ranged breakpoint. In most targets, a match happens only if ASPACE
7140 matches the breakpoint's address space. On targets that have global
7141 breakpoints, the address space doesn't really matter. */
7142
7143 static int
7144 breakpoint_location_address_match (struct bp_location *bl,
7145 struct address_space *aspace,
7146 CORE_ADDR addr)
7147 {
7148 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7149 aspace, addr)
7150 || (bl->length
7151 && breakpoint_address_match_range (bl->pspace->aspace,
7152 bl->address, bl->length,
7153 aspace, addr)));
7154 }
7155
7156 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7157 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7158 match happens only if ASPACE matches the breakpoint's address
7159 space. On targets that have global breakpoints, the address space
7160 doesn't really matter. */
7161
7162 static int
7163 breakpoint_location_address_range_overlap (struct bp_location *bl,
7164 struct address_space *aspace,
7165 CORE_ADDR addr, int len)
7166 {
7167 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7168 || bl->pspace->aspace == aspace)
7169 {
7170 int bl_len = bl->length != 0 ? bl->length : 1;
7171
7172 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7173 return 1;
7174 }
7175 return 0;
7176 }
7177
7178 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7179 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7180 true, otherwise returns false. */
7181
7182 static int
7183 tracepoint_locations_match (struct bp_location *loc1,
7184 struct bp_location *loc2)
7185 {
7186 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7187 /* Since tracepoint locations are never duplicated with others', tracepoint
7188 locations at the same address of different tracepoints are regarded as
7189 different locations. */
7190 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7191 else
7192 return 0;
7193 }
7194
7195 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7196 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7197 represent the same location. */
7198
7199 static int
7200 breakpoint_locations_match (struct bp_location *loc1,
7201 struct bp_location *loc2)
7202 {
7203 int hw_point1, hw_point2;
7204
7205 /* Both of them must not be in moribund_locations. */
7206 gdb_assert (loc1->owner != NULL);
7207 gdb_assert (loc2->owner != NULL);
7208
7209 hw_point1 = is_hardware_watchpoint (loc1->owner);
7210 hw_point2 = is_hardware_watchpoint (loc2->owner);
7211
7212 if (hw_point1 != hw_point2)
7213 return 0;
7214 else if (hw_point1)
7215 return watchpoint_locations_match (loc1, loc2);
7216 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7217 return tracepoint_locations_match (loc1, loc2);
7218 else
7219 /* We compare bp_location.length in order to cover ranged breakpoints. */
7220 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7221 loc2->pspace->aspace, loc2->address)
7222 && loc1->length == loc2->length);
7223 }
7224
7225 static void
7226 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7227 int bnum, int have_bnum)
7228 {
7229 /* The longest string possibly returned by hex_string_custom
7230 is 50 chars. These must be at least that big for safety. */
7231 char astr1[64];
7232 char astr2[64];
7233
7234 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7235 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7236 if (have_bnum)
7237 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7238 bnum, astr1, astr2);
7239 else
7240 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7241 }
7242
7243 /* Adjust a breakpoint's address to account for architectural
7244 constraints on breakpoint placement. Return the adjusted address.
7245 Note: Very few targets require this kind of adjustment. For most
7246 targets, this function is simply the identity function. */
7247
7248 static CORE_ADDR
7249 adjust_breakpoint_address (struct gdbarch *gdbarch,
7250 CORE_ADDR bpaddr, enum bptype bptype)
7251 {
7252 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7253 {
7254 /* Very few targets need any kind of breakpoint adjustment. */
7255 return bpaddr;
7256 }
7257 else if (bptype == bp_watchpoint
7258 || bptype == bp_hardware_watchpoint
7259 || bptype == bp_read_watchpoint
7260 || bptype == bp_access_watchpoint
7261 || bptype == bp_catchpoint)
7262 {
7263 /* Watchpoints and the various bp_catch_* eventpoints should not
7264 have their addresses modified. */
7265 return bpaddr;
7266 }
7267 else if (bptype == bp_single_step)
7268 {
7269 /* Single-step breakpoints should not have their addresses
7270 modified. If there's any architectural constrain that
7271 applies to this address, then it should have already been
7272 taken into account when the breakpoint was created in the
7273 first place. If we didn't do this, stepping through e.g.,
7274 Thumb-2 IT blocks would break. */
7275 return bpaddr;
7276 }
7277 else
7278 {
7279 CORE_ADDR adjusted_bpaddr;
7280
7281 /* Some targets have architectural constraints on the placement
7282 of breakpoint instructions. Obtain the adjusted address. */
7283 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7284
7285 /* An adjusted breakpoint address can significantly alter
7286 a user's expectations. Print a warning if an adjustment
7287 is required. */
7288 if (adjusted_bpaddr != bpaddr)
7289 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7290
7291 return adjusted_bpaddr;
7292 }
7293 }
7294
7295 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7296 {
7297 bp_location *loc = this;
7298
7299 gdb_assert (ops != NULL);
7300
7301 loc->ops = ops;
7302 loc->owner = owner;
7303 loc->cond_bytecode = NULL;
7304 loc->shlib_disabled = 0;
7305 loc->enabled = 1;
7306
7307 switch (owner->type)
7308 {
7309 case bp_breakpoint:
7310 case bp_single_step:
7311 case bp_until:
7312 case bp_finish:
7313 case bp_longjmp:
7314 case bp_longjmp_resume:
7315 case bp_longjmp_call_dummy:
7316 case bp_exception:
7317 case bp_exception_resume:
7318 case bp_step_resume:
7319 case bp_hp_step_resume:
7320 case bp_watchpoint_scope:
7321 case bp_call_dummy:
7322 case bp_std_terminate:
7323 case bp_shlib_event:
7324 case bp_thread_event:
7325 case bp_overlay_event:
7326 case bp_jit_event:
7327 case bp_longjmp_master:
7328 case bp_std_terminate_master:
7329 case bp_exception_master:
7330 case bp_gnu_ifunc_resolver:
7331 case bp_gnu_ifunc_resolver_return:
7332 case bp_dprintf:
7333 loc->loc_type = bp_loc_software_breakpoint;
7334 mark_breakpoint_location_modified (loc);
7335 break;
7336 case bp_hardware_breakpoint:
7337 loc->loc_type = bp_loc_hardware_breakpoint;
7338 mark_breakpoint_location_modified (loc);
7339 break;
7340 case bp_hardware_watchpoint:
7341 case bp_read_watchpoint:
7342 case bp_access_watchpoint:
7343 loc->loc_type = bp_loc_hardware_watchpoint;
7344 break;
7345 case bp_watchpoint:
7346 case bp_catchpoint:
7347 case bp_tracepoint:
7348 case bp_fast_tracepoint:
7349 case bp_static_tracepoint:
7350 loc->loc_type = bp_loc_other;
7351 break;
7352 default:
7353 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7354 }
7355
7356 loc->refc = 1;
7357 }
7358
7359 /* Allocate a struct bp_location. */
7360
7361 static struct bp_location *
7362 allocate_bp_location (struct breakpoint *bpt)
7363 {
7364 return bpt->ops->allocate_location (bpt);
7365 }
7366
7367 static void
7368 free_bp_location (struct bp_location *loc)
7369 {
7370 loc->ops->dtor (loc);
7371 delete loc;
7372 }
7373
7374 /* Increment reference count. */
7375
7376 static void
7377 incref_bp_location (struct bp_location *bl)
7378 {
7379 ++bl->refc;
7380 }
7381
7382 /* Decrement reference count. If the reference count reaches 0,
7383 destroy the bp_location. Sets *BLP to NULL. */
7384
7385 static void
7386 decref_bp_location (struct bp_location **blp)
7387 {
7388 gdb_assert ((*blp)->refc > 0);
7389
7390 if (--(*blp)->refc == 0)
7391 free_bp_location (*blp);
7392 *blp = NULL;
7393 }
7394
7395 /* Add breakpoint B at the end of the global breakpoint chain. */
7396
7397 static breakpoint *
7398 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7399 {
7400 struct breakpoint *b1;
7401 struct breakpoint *result = b.get ();
7402
7403 /* Add this breakpoint to the end of the chain so that a list of
7404 breakpoints will come out in order of increasing numbers. */
7405
7406 b1 = breakpoint_chain;
7407 if (b1 == 0)
7408 breakpoint_chain = b.release ();
7409 else
7410 {
7411 while (b1->next)
7412 b1 = b1->next;
7413 b1->next = b.release ();
7414 }
7415
7416 return result;
7417 }
7418
7419 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7420
7421 static void
7422 init_raw_breakpoint_without_location (struct breakpoint *b,
7423 struct gdbarch *gdbarch,
7424 enum bptype bptype,
7425 const struct breakpoint_ops *ops)
7426 {
7427 gdb_assert (ops != NULL);
7428
7429 b->ops = ops;
7430 b->type = bptype;
7431 b->gdbarch = gdbarch;
7432 b->language = current_language->la_language;
7433 b->input_radix = input_radix;
7434 b->related_breakpoint = b;
7435 }
7436
7437 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7438 that has type BPTYPE and has no locations as yet. */
7439
7440 static struct breakpoint *
7441 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7442 enum bptype bptype,
7443 const struct breakpoint_ops *ops)
7444 {
7445 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7446
7447 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7448 return add_to_breakpoint_chain (std::move (b));
7449 }
7450
7451 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7452 resolutions should be made as the user specified the location explicitly
7453 enough. */
7454
7455 static void
7456 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7457 {
7458 gdb_assert (loc->owner != NULL);
7459
7460 if (loc->owner->type == bp_breakpoint
7461 || loc->owner->type == bp_hardware_breakpoint
7462 || is_tracepoint (loc->owner))
7463 {
7464 int is_gnu_ifunc;
7465 const char *function_name;
7466 CORE_ADDR func_addr;
7467
7468 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7469 &func_addr, NULL, &is_gnu_ifunc);
7470
7471 if (is_gnu_ifunc && !explicit_loc)
7472 {
7473 struct breakpoint *b = loc->owner;
7474
7475 gdb_assert (loc->pspace == current_program_space);
7476 if (gnu_ifunc_resolve_name (function_name,
7477 &loc->requested_address))
7478 {
7479 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7480 loc->address = adjust_breakpoint_address (loc->gdbarch,
7481 loc->requested_address,
7482 b->type);
7483 }
7484 else if (b->type == bp_breakpoint && b->loc == loc
7485 && loc->next == NULL && b->related_breakpoint == b)
7486 {
7487 /* Create only the whole new breakpoint of this type but do not
7488 mess more complicated breakpoints with multiple locations. */
7489 b->type = bp_gnu_ifunc_resolver;
7490 /* Remember the resolver's address for use by the return
7491 breakpoint. */
7492 loc->related_address = func_addr;
7493 }
7494 }
7495
7496 if (function_name)
7497 loc->function_name = xstrdup (function_name);
7498 }
7499 }
7500
7501 /* Attempt to determine architecture of location identified by SAL. */
7502 struct gdbarch *
7503 get_sal_arch (struct symtab_and_line sal)
7504 {
7505 if (sal.section)
7506 return get_objfile_arch (sal.section->objfile);
7507 if (sal.symtab)
7508 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7509
7510 return NULL;
7511 }
7512
7513 /* Low level routine for partially initializing a breakpoint of type
7514 BPTYPE. The newly created breakpoint's address, section, source
7515 file name, and line number are provided by SAL.
7516
7517 It is expected that the caller will complete the initialization of
7518 the newly created breakpoint struct as well as output any status
7519 information regarding the creation of a new breakpoint. */
7520
7521 static void
7522 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7523 struct symtab_and_line sal, enum bptype bptype,
7524 const struct breakpoint_ops *ops)
7525 {
7526 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7527
7528 add_location_to_breakpoint (b, &sal);
7529
7530 if (bptype != bp_catchpoint)
7531 gdb_assert (sal.pspace != NULL);
7532
7533 /* Store the program space that was used to set the breakpoint,
7534 except for ordinary breakpoints, which are independent of the
7535 program space. */
7536 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7537 b->pspace = sal.pspace;
7538 }
7539
7540 /* set_raw_breakpoint is a low level routine for allocating and
7541 partially initializing a breakpoint of type BPTYPE. The newly
7542 created breakpoint's address, section, source file name, and line
7543 number are provided by SAL. The newly created and partially
7544 initialized breakpoint is added to the breakpoint chain and
7545 is also returned as the value of this function.
7546
7547 It is expected that the caller will complete the initialization of
7548 the newly created breakpoint struct as well as output any status
7549 information regarding the creation of a new breakpoint. In
7550 particular, set_raw_breakpoint does NOT set the breakpoint
7551 number! Care should be taken to not allow an error to occur
7552 prior to completing the initialization of the breakpoint. If this
7553 should happen, a bogus breakpoint will be left on the chain. */
7554
7555 struct breakpoint *
7556 set_raw_breakpoint (struct gdbarch *gdbarch,
7557 struct symtab_and_line sal, enum bptype bptype,
7558 const struct breakpoint_ops *ops)
7559 {
7560 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7561
7562 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7563 return add_to_breakpoint_chain (std::move (b));
7564 }
7565
7566 /* Call this routine when stepping and nexting to enable a breakpoint
7567 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7568 initiated the operation. */
7569
7570 void
7571 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7572 {
7573 struct breakpoint *b, *b_tmp;
7574 int thread = tp->global_num;
7575
7576 /* To avoid having to rescan all objfile symbols at every step,
7577 we maintain a list of continually-inserted but always disabled
7578 longjmp "master" breakpoints. Here, we simply create momentary
7579 clones of those and enable them for the requested thread. */
7580 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7581 if (b->pspace == current_program_space
7582 && (b->type == bp_longjmp_master
7583 || b->type == bp_exception_master))
7584 {
7585 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7586 struct breakpoint *clone;
7587
7588 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7589 after their removal. */
7590 clone = momentary_breakpoint_from_master (b, type,
7591 &momentary_breakpoint_ops, 1);
7592 clone->thread = thread;
7593 }
7594
7595 tp->initiating_frame = frame;
7596 }
7597
7598 /* Delete all longjmp breakpoints from THREAD. */
7599 void
7600 delete_longjmp_breakpoint (int thread)
7601 {
7602 struct breakpoint *b, *b_tmp;
7603
7604 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7605 if (b->type == bp_longjmp || b->type == bp_exception)
7606 {
7607 if (b->thread == thread)
7608 delete_breakpoint (b);
7609 }
7610 }
7611
7612 void
7613 delete_longjmp_breakpoint_at_next_stop (int thread)
7614 {
7615 struct breakpoint *b, *b_tmp;
7616
7617 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7618 if (b->type == bp_longjmp || b->type == bp_exception)
7619 {
7620 if (b->thread == thread)
7621 b->disposition = disp_del_at_next_stop;
7622 }
7623 }
7624
7625 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7626 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7627 pointer to any of them. Return NULL if this system cannot place longjmp
7628 breakpoints. */
7629
7630 struct breakpoint *
7631 set_longjmp_breakpoint_for_call_dummy (void)
7632 {
7633 struct breakpoint *b, *retval = NULL;
7634
7635 ALL_BREAKPOINTS (b)
7636 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7637 {
7638 struct breakpoint *new_b;
7639
7640 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7641 &momentary_breakpoint_ops,
7642 1);
7643 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7644
7645 /* Link NEW_B into the chain of RETVAL breakpoints. */
7646
7647 gdb_assert (new_b->related_breakpoint == new_b);
7648 if (retval == NULL)
7649 retval = new_b;
7650 new_b->related_breakpoint = retval;
7651 while (retval->related_breakpoint != new_b->related_breakpoint)
7652 retval = retval->related_breakpoint;
7653 retval->related_breakpoint = new_b;
7654 }
7655
7656 return retval;
7657 }
7658
7659 /* Verify all existing dummy frames and their associated breakpoints for
7660 TP. Remove those which can no longer be found in the current frame
7661 stack.
7662
7663 You should call this function only at places where it is safe to currently
7664 unwind the whole stack. Failed stack unwind would discard live dummy
7665 frames. */
7666
7667 void
7668 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7669 {
7670 struct breakpoint *b, *b_tmp;
7671
7672 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7673 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7674 {
7675 struct breakpoint *dummy_b = b->related_breakpoint;
7676
7677 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7678 dummy_b = dummy_b->related_breakpoint;
7679 if (dummy_b->type != bp_call_dummy
7680 || frame_find_by_id (dummy_b->frame_id) != NULL)
7681 continue;
7682
7683 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7684
7685 while (b->related_breakpoint != b)
7686 {
7687 if (b_tmp == b->related_breakpoint)
7688 b_tmp = b->related_breakpoint->next;
7689 delete_breakpoint (b->related_breakpoint);
7690 }
7691 delete_breakpoint (b);
7692 }
7693 }
7694
7695 void
7696 enable_overlay_breakpoints (void)
7697 {
7698 struct breakpoint *b;
7699
7700 ALL_BREAKPOINTS (b)
7701 if (b->type == bp_overlay_event)
7702 {
7703 b->enable_state = bp_enabled;
7704 update_global_location_list (UGLL_MAY_INSERT);
7705 overlay_events_enabled = 1;
7706 }
7707 }
7708
7709 void
7710 disable_overlay_breakpoints (void)
7711 {
7712 struct breakpoint *b;
7713
7714 ALL_BREAKPOINTS (b)
7715 if (b->type == bp_overlay_event)
7716 {
7717 b->enable_state = bp_disabled;
7718 update_global_location_list (UGLL_DONT_INSERT);
7719 overlay_events_enabled = 0;
7720 }
7721 }
7722
7723 /* Set an active std::terminate breakpoint for each std::terminate
7724 master breakpoint. */
7725 void
7726 set_std_terminate_breakpoint (void)
7727 {
7728 struct breakpoint *b, *b_tmp;
7729
7730 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7731 if (b->pspace == current_program_space
7732 && b->type == bp_std_terminate_master)
7733 {
7734 momentary_breakpoint_from_master (b, bp_std_terminate,
7735 &momentary_breakpoint_ops, 1);
7736 }
7737 }
7738
7739 /* Delete all the std::terminate breakpoints. */
7740 void
7741 delete_std_terminate_breakpoint (void)
7742 {
7743 struct breakpoint *b, *b_tmp;
7744
7745 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7746 if (b->type == bp_std_terminate)
7747 delete_breakpoint (b);
7748 }
7749
7750 struct breakpoint *
7751 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7752 {
7753 struct breakpoint *b;
7754
7755 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7756 &internal_breakpoint_ops);
7757
7758 b->enable_state = bp_enabled;
7759 /* location has to be used or breakpoint_re_set will delete me. */
7760 b->location = new_address_location (b->loc->address, NULL, 0);
7761
7762 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7763
7764 return b;
7765 }
7766
7767 struct lang_and_radix
7768 {
7769 enum language lang;
7770 int radix;
7771 };
7772
7773 /* Create a breakpoint for JIT code registration and unregistration. */
7774
7775 struct breakpoint *
7776 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7777 {
7778 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7779 &internal_breakpoint_ops);
7780 }
7781
7782 /* Remove JIT code registration and unregistration breakpoint(s). */
7783
7784 void
7785 remove_jit_event_breakpoints (void)
7786 {
7787 struct breakpoint *b, *b_tmp;
7788
7789 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7790 if (b->type == bp_jit_event
7791 && b->loc->pspace == current_program_space)
7792 delete_breakpoint (b);
7793 }
7794
7795 void
7796 remove_solib_event_breakpoints (void)
7797 {
7798 struct breakpoint *b, *b_tmp;
7799
7800 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7801 if (b->type == bp_shlib_event
7802 && b->loc->pspace == current_program_space)
7803 delete_breakpoint (b);
7804 }
7805
7806 /* See breakpoint.h. */
7807
7808 void
7809 remove_solib_event_breakpoints_at_next_stop (void)
7810 {
7811 struct breakpoint *b, *b_tmp;
7812
7813 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7814 if (b->type == bp_shlib_event
7815 && b->loc->pspace == current_program_space)
7816 b->disposition = disp_del_at_next_stop;
7817 }
7818
7819 /* Helper for create_solib_event_breakpoint /
7820 create_and_insert_solib_event_breakpoint. Allows specifying which
7821 INSERT_MODE to pass through to update_global_location_list. */
7822
7823 static struct breakpoint *
7824 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7825 enum ugll_insert_mode insert_mode)
7826 {
7827 struct breakpoint *b;
7828
7829 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7830 &internal_breakpoint_ops);
7831 update_global_location_list_nothrow (insert_mode);
7832 return b;
7833 }
7834
7835 struct breakpoint *
7836 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7837 {
7838 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7839 }
7840
7841 /* See breakpoint.h. */
7842
7843 struct breakpoint *
7844 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7845 {
7846 struct breakpoint *b;
7847
7848 /* Explicitly tell update_global_location_list to insert
7849 locations. */
7850 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7851 if (!b->loc->inserted)
7852 {
7853 delete_breakpoint (b);
7854 return NULL;
7855 }
7856 return b;
7857 }
7858
7859 /* Disable any breakpoints that are on code in shared libraries. Only
7860 apply to enabled breakpoints, disabled ones can just stay disabled. */
7861
7862 void
7863 disable_breakpoints_in_shlibs (void)
7864 {
7865 struct bp_location *loc, **locp_tmp;
7866
7867 ALL_BP_LOCATIONS (loc, locp_tmp)
7868 {
7869 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7870 struct breakpoint *b = loc->owner;
7871
7872 /* We apply the check to all breakpoints, including disabled for
7873 those with loc->duplicate set. This is so that when breakpoint
7874 becomes enabled, or the duplicate is removed, gdb will try to
7875 insert all breakpoints. If we don't set shlib_disabled here,
7876 we'll try to insert those breakpoints and fail. */
7877 if (((b->type == bp_breakpoint)
7878 || (b->type == bp_jit_event)
7879 || (b->type == bp_hardware_breakpoint)
7880 || (is_tracepoint (b)))
7881 && loc->pspace == current_program_space
7882 && !loc->shlib_disabled
7883 && solib_name_from_address (loc->pspace, loc->address)
7884 )
7885 {
7886 loc->shlib_disabled = 1;
7887 }
7888 }
7889 }
7890
7891 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7892 notification of unloaded_shlib. Only apply to enabled breakpoints,
7893 disabled ones can just stay disabled. */
7894
7895 static void
7896 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7897 {
7898 struct bp_location *loc, **locp_tmp;
7899 int disabled_shlib_breaks = 0;
7900
7901 ALL_BP_LOCATIONS (loc, locp_tmp)
7902 {
7903 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7904 struct breakpoint *b = loc->owner;
7905
7906 if (solib->pspace == loc->pspace
7907 && !loc->shlib_disabled
7908 && (((b->type == bp_breakpoint
7909 || b->type == bp_jit_event
7910 || b->type == bp_hardware_breakpoint)
7911 && (loc->loc_type == bp_loc_hardware_breakpoint
7912 || loc->loc_type == bp_loc_software_breakpoint))
7913 || is_tracepoint (b))
7914 && solib_contains_address_p (solib, loc->address))
7915 {
7916 loc->shlib_disabled = 1;
7917 /* At this point, we cannot rely on remove_breakpoint
7918 succeeding so we must mark the breakpoint as not inserted
7919 to prevent future errors occurring in remove_breakpoints. */
7920 loc->inserted = 0;
7921
7922 /* This may cause duplicate notifications for the same breakpoint. */
7923 observer_notify_breakpoint_modified (b);
7924
7925 if (!disabled_shlib_breaks)
7926 {
7927 target_terminal_ours_for_output ();
7928 warning (_("Temporarily disabling breakpoints "
7929 "for unloaded shared library \"%s\""),
7930 solib->so_name);
7931 }
7932 disabled_shlib_breaks = 1;
7933 }
7934 }
7935 }
7936
7937 /* Disable any breakpoints and tracepoints in OBJFILE upon
7938 notification of free_objfile. Only apply to enabled breakpoints,
7939 disabled ones can just stay disabled. */
7940
7941 static void
7942 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7943 {
7944 struct breakpoint *b;
7945
7946 if (objfile == NULL)
7947 return;
7948
7949 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7950 managed by the user with add-symbol-file/remove-symbol-file.
7951 Similarly to how breakpoints in shared libraries are handled in
7952 response to "nosharedlibrary", mark breakpoints in such modules
7953 shlib_disabled so they end up uninserted on the next global
7954 location list update. Shared libraries not loaded by the user
7955 aren't handled here -- they're already handled in
7956 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7957 solib_unloaded observer. We skip objfiles that are not
7958 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7959 main objfile). */
7960 if ((objfile->flags & OBJF_SHARED) == 0
7961 || (objfile->flags & OBJF_USERLOADED) == 0)
7962 return;
7963
7964 ALL_BREAKPOINTS (b)
7965 {
7966 struct bp_location *loc;
7967 int bp_modified = 0;
7968
7969 if (!is_breakpoint (b) && !is_tracepoint (b))
7970 continue;
7971
7972 for (loc = b->loc; loc != NULL; loc = loc->next)
7973 {
7974 CORE_ADDR loc_addr = loc->address;
7975
7976 if (loc->loc_type != bp_loc_hardware_breakpoint
7977 && loc->loc_type != bp_loc_software_breakpoint)
7978 continue;
7979
7980 if (loc->shlib_disabled != 0)
7981 continue;
7982
7983 if (objfile->pspace != loc->pspace)
7984 continue;
7985
7986 if (loc->loc_type != bp_loc_hardware_breakpoint
7987 && loc->loc_type != bp_loc_software_breakpoint)
7988 continue;
7989
7990 if (is_addr_in_objfile (loc_addr, objfile))
7991 {
7992 loc->shlib_disabled = 1;
7993 /* At this point, we don't know whether the object was
7994 unmapped from the inferior or not, so leave the
7995 inserted flag alone. We'll handle failure to
7996 uninsert quietly, in case the object was indeed
7997 unmapped. */
7998
7999 mark_breakpoint_location_modified (loc);
8000
8001 bp_modified = 1;
8002 }
8003 }
8004
8005 if (bp_modified)
8006 observer_notify_breakpoint_modified (b);
8007 }
8008 }
8009
8010 /* FORK & VFORK catchpoints. */
8011
8012 /* An instance of this type is used to represent a fork or vfork
8013 catchpoint. A breakpoint is really of this type iff its ops pointer points
8014 to CATCH_FORK_BREAKPOINT_OPS. */
8015
8016 struct fork_catchpoint : public breakpoint
8017 {
8018 /* Process id of a child process whose forking triggered this
8019 catchpoint. This field is only valid immediately after this
8020 catchpoint has triggered. */
8021 ptid_t forked_inferior_pid;
8022 };
8023
8024 /* Implement the "insert" breakpoint_ops method for fork
8025 catchpoints. */
8026
8027 static int
8028 insert_catch_fork (struct bp_location *bl)
8029 {
8030 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8031 }
8032
8033 /* Implement the "remove" breakpoint_ops method for fork
8034 catchpoints. */
8035
8036 static int
8037 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8038 {
8039 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8040 }
8041
8042 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8043 catchpoints. */
8044
8045 static int
8046 breakpoint_hit_catch_fork (const struct bp_location *bl,
8047 struct address_space *aspace, CORE_ADDR bp_addr,
8048 const struct target_waitstatus *ws)
8049 {
8050 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8051
8052 if (ws->kind != TARGET_WAITKIND_FORKED)
8053 return 0;
8054
8055 c->forked_inferior_pid = ws->value.related_pid;
8056 return 1;
8057 }
8058
8059 /* Implement the "print_it" breakpoint_ops method for fork
8060 catchpoints. */
8061
8062 static enum print_stop_action
8063 print_it_catch_fork (bpstat bs)
8064 {
8065 struct ui_out *uiout = current_uiout;
8066 struct breakpoint *b = bs->breakpoint_at;
8067 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8068
8069 annotate_catchpoint (b->number);
8070 maybe_print_thread_hit_breakpoint (uiout);
8071 if (b->disposition == disp_del)
8072 uiout->text ("Temporary catchpoint ");
8073 else
8074 uiout->text ("Catchpoint ");
8075 if (uiout->is_mi_like_p ())
8076 {
8077 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8078 uiout->field_string ("disp", bpdisp_text (b->disposition));
8079 }
8080 uiout->field_int ("bkptno", b->number);
8081 uiout->text (" (forked process ");
8082 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8083 uiout->text ("), ");
8084 return PRINT_SRC_AND_LOC;
8085 }
8086
8087 /* Implement the "print_one" breakpoint_ops method for fork
8088 catchpoints. */
8089
8090 static void
8091 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8092 {
8093 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8094 struct value_print_options opts;
8095 struct ui_out *uiout = current_uiout;
8096
8097 get_user_print_options (&opts);
8098
8099 /* Field 4, the address, is omitted (which makes the columns not
8100 line up too nicely with the headers, but the effect is relatively
8101 readable). */
8102 if (opts.addressprint)
8103 uiout->field_skip ("addr");
8104 annotate_field (5);
8105 uiout->text ("fork");
8106 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8107 {
8108 uiout->text (", process ");
8109 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8110 uiout->spaces (1);
8111 }
8112
8113 if (uiout->is_mi_like_p ())
8114 uiout->field_string ("catch-type", "fork");
8115 }
8116
8117 /* Implement the "print_mention" breakpoint_ops method for fork
8118 catchpoints. */
8119
8120 static void
8121 print_mention_catch_fork (struct breakpoint *b)
8122 {
8123 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8124 }
8125
8126 /* Implement the "print_recreate" breakpoint_ops method for fork
8127 catchpoints. */
8128
8129 static void
8130 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8131 {
8132 fprintf_unfiltered (fp, "catch fork");
8133 print_recreate_thread (b, fp);
8134 }
8135
8136 /* The breakpoint_ops structure to be used in fork catchpoints. */
8137
8138 static struct breakpoint_ops catch_fork_breakpoint_ops;
8139
8140 /* Implement the "insert" breakpoint_ops method for vfork
8141 catchpoints. */
8142
8143 static int
8144 insert_catch_vfork (struct bp_location *bl)
8145 {
8146 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8147 }
8148
8149 /* Implement the "remove" breakpoint_ops method for vfork
8150 catchpoints. */
8151
8152 static int
8153 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8154 {
8155 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8156 }
8157
8158 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8159 catchpoints. */
8160
8161 static int
8162 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8163 struct address_space *aspace, CORE_ADDR bp_addr,
8164 const struct target_waitstatus *ws)
8165 {
8166 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8167
8168 if (ws->kind != TARGET_WAITKIND_VFORKED)
8169 return 0;
8170
8171 c->forked_inferior_pid = ws->value.related_pid;
8172 return 1;
8173 }
8174
8175 /* Implement the "print_it" breakpoint_ops method for vfork
8176 catchpoints. */
8177
8178 static enum print_stop_action
8179 print_it_catch_vfork (bpstat bs)
8180 {
8181 struct ui_out *uiout = current_uiout;
8182 struct breakpoint *b = bs->breakpoint_at;
8183 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8184
8185 annotate_catchpoint (b->number);
8186 maybe_print_thread_hit_breakpoint (uiout);
8187 if (b->disposition == disp_del)
8188 uiout->text ("Temporary catchpoint ");
8189 else
8190 uiout->text ("Catchpoint ");
8191 if (uiout->is_mi_like_p ())
8192 {
8193 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8194 uiout->field_string ("disp", bpdisp_text (b->disposition));
8195 }
8196 uiout->field_int ("bkptno", b->number);
8197 uiout->text (" (vforked process ");
8198 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8199 uiout->text ("), ");
8200 return PRINT_SRC_AND_LOC;
8201 }
8202
8203 /* Implement the "print_one" breakpoint_ops method for vfork
8204 catchpoints. */
8205
8206 static void
8207 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8208 {
8209 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8210 struct value_print_options opts;
8211 struct ui_out *uiout = current_uiout;
8212
8213 get_user_print_options (&opts);
8214 /* Field 4, the address, is omitted (which makes the columns not
8215 line up too nicely with the headers, but the effect is relatively
8216 readable). */
8217 if (opts.addressprint)
8218 uiout->field_skip ("addr");
8219 annotate_field (5);
8220 uiout->text ("vfork");
8221 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8222 {
8223 uiout->text (", process ");
8224 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8225 uiout->spaces (1);
8226 }
8227
8228 if (uiout->is_mi_like_p ())
8229 uiout->field_string ("catch-type", "vfork");
8230 }
8231
8232 /* Implement the "print_mention" breakpoint_ops method for vfork
8233 catchpoints. */
8234
8235 static void
8236 print_mention_catch_vfork (struct breakpoint *b)
8237 {
8238 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8239 }
8240
8241 /* Implement the "print_recreate" breakpoint_ops method for vfork
8242 catchpoints. */
8243
8244 static void
8245 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8246 {
8247 fprintf_unfiltered (fp, "catch vfork");
8248 print_recreate_thread (b, fp);
8249 }
8250
8251 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8252
8253 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8254
8255 /* An instance of this type is used to represent an solib catchpoint.
8256 A breakpoint is really of this type iff its ops pointer points to
8257 CATCH_SOLIB_BREAKPOINT_OPS. */
8258
8259 struct solib_catchpoint : public breakpoint
8260 {
8261 ~solib_catchpoint () override;
8262
8263 /* True for "catch load", false for "catch unload". */
8264 unsigned char is_load;
8265
8266 /* Regular expression to match, if any. COMPILED is only valid when
8267 REGEX is non-NULL. */
8268 char *regex;
8269 std::unique_ptr<compiled_regex> compiled;
8270 };
8271
8272 solib_catchpoint::~solib_catchpoint ()
8273 {
8274 xfree (this->regex);
8275 }
8276
8277 static int
8278 insert_catch_solib (struct bp_location *ignore)
8279 {
8280 return 0;
8281 }
8282
8283 static int
8284 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8285 {
8286 return 0;
8287 }
8288
8289 static int
8290 breakpoint_hit_catch_solib (const struct bp_location *bl,
8291 struct address_space *aspace,
8292 CORE_ADDR bp_addr,
8293 const struct target_waitstatus *ws)
8294 {
8295 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8296 struct breakpoint *other;
8297
8298 if (ws->kind == TARGET_WAITKIND_LOADED)
8299 return 1;
8300
8301 ALL_BREAKPOINTS (other)
8302 {
8303 struct bp_location *other_bl;
8304
8305 if (other == bl->owner)
8306 continue;
8307
8308 if (other->type != bp_shlib_event)
8309 continue;
8310
8311 if (self->pspace != NULL && other->pspace != self->pspace)
8312 continue;
8313
8314 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8315 {
8316 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8317 return 1;
8318 }
8319 }
8320
8321 return 0;
8322 }
8323
8324 static void
8325 check_status_catch_solib (struct bpstats *bs)
8326 {
8327 struct solib_catchpoint *self
8328 = (struct solib_catchpoint *) bs->breakpoint_at;
8329 int ix;
8330
8331 if (self->is_load)
8332 {
8333 struct so_list *iter;
8334
8335 for (ix = 0;
8336 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8337 ix, iter);
8338 ++ix)
8339 {
8340 if (!self->regex
8341 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8342 return;
8343 }
8344 }
8345 else
8346 {
8347 char *iter;
8348
8349 for (ix = 0;
8350 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8351 ix, iter);
8352 ++ix)
8353 {
8354 if (!self->regex
8355 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8356 return;
8357 }
8358 }
8359
8360 bs->stop = 0;
8361 bs->print_it = print_it_noop;
8362 }
8363
8364 static enum print_stop_action
8365 print_it_catch_solib (bpstat bs)
8366 {
8367 struct breakpoint *b = bs->breakpoint_at;
8368 struct ui_out *uiout = current_uiout;
8369
8370 annotate_catchpoint (b->number);
8371 maybe_print_thread_hit_breakpoint (uiout);
8372 if (b->disposition == disp_del)
8373 uiout->text ("Temporary catchpoint ");
8374 else
8375 uiout->text ("Catchpoint ");
8376 uiout->field_int ("bkptno", b->number);
8377 uiout->text ("\n");
8378 if (uiout->is_mi_like_p ())
8379 uiout->field_string ("disp", bpdisp_text (b->disposition));
8380 print_solib_event (1);
8381 return PRINT_SRC_AND_LOC;
8382 }
8383
8384 static void
8385 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8386 {
8387 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8388 struct value_print_options opts;
8389 struct ui_out *uiout = current_uiout;
8390 char *msg;
8391
8392 get_user_print_options (&opts);
8393 /* Field 4, the address, is omitted (which makes the columns not
8394 line up too nicely with the headers, but the effect is relatively
8395 readable). */
8396 if (opts.addressprint)
8397 {
8398 annotate_field (4);
8399 uiout->field_skip ("addr");
8400 }
8401
8402 annotate_field (5);
8403 if (self->is_load)
8404 {
8405 if (self->regex)
8406 msg = xstrprintf (_("load of library matching %s"), self->regex);
8407 else
8408 msg = xstrdup (_("load of library"));
8409 }
8410 else
8411 {
8412 if (self->regex)
8413 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8414 else
8415 msg = xstrdup (_("unload of library"));
8416 }
8417 uiout->field_string ("what", msg);
8418 xfree (msg);
8419
8420 if (uiout->is_mi_like_p ())
8421 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8422 }
8423
8424 static void
8425 print_mention_catch_solib (struct breakpoint *b)
8426 {
8427 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8428
8429 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8430 self->is_load ? "load" : "unload");
8431 }
8432
8433 static void
8434 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8435 {
8436 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8437
8438 fprintf_unfiltered (fp, "%s %s",
8439 b->disposition == disp_del ? "tcatch" : "catch",
8440 self->is_load ? "load" : "unload");
8441 if (self->regex)
8442 fprintf_unfiltered (fp, " %s", self->regex);
8443 fprintf_unfiltered (fp, "\n");
8444 }
8445
8446 static struct breakpoint_ops catch_solib_breakpoint_ops;
8447
8448 /* Shared helper function (MI and CLI) for creating and installing
8449 a shared object event catchpoint. If IS_LOAD is non-zero then
8450 the events to be caught are load events, otherwise they are
8451 unload events. If IS_TEMP is non-zero the catchpoint is a
8452 temporary one. If ENABLED is non-zero the catchpoint is
8453 created in an enabled state. */
8454
8455 void
8456 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8457 {
8458 struct gdbarch *gdbarch = get_current_arch ();
8459
8460 if (!arg)
8461 arg = "";
8462 arg = skip_spaces_const (arg);
8463
8464 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8465
8466 if (*arg != '\0')
8467 {
8468 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8469 _("Invalid regexp")));
8470 c->regex = xstrdup (arg);
8471 }
8472
8473 c->is_load = is_load;
8474 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8475 &catch_solib_breakpoint_ops);
8476
8477 c->enable_state = enabled ? bp_enabled : bp_disabled;
8478
8479 install_breakpoint (0, std::move (c), 1);
8480 }
8481
8482 /* A helper function that does all the work for "catch load" and
8483 "catch unload". */
8484
8485 static void
8486 catch_load_or_unload (char *arg, int from_tty, int is_load,
8487 struct cmd_list_element *command)
8488 {
8489 int tempflag;
8490 const int enabled = 1;
8491
8492 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8493
8494 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8495 }
8496
8497 static void
8498 catch_load_command_1 (char *arg, int from_tty,
8499 struct cmd_list_element *command)
8500 {
8501 catch_load_or_unload (arg, from_tty, 1, command);
8502 }
8503
8504 static void
8505 catch_unload_command_1 (char *arg, int from_tty,
8506 struct cmd_list_element *command)
8507 {
8508 catch_load_or_unload (arg, from_tty, 0, command);
8509 }
8510
8511 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8512 is non-zero, then make the breakpoint temporary. If COND_STRING is
8513 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8514 the breakpoint_ops structure associated to the catchpoint. */
8515
8516 void
8517 init_catchpoint (struct breakpoint *b,
8518 struct gdbarch *gdbarch, int tempflag,
8519 const char *cond_string,
8520 const struct breakpoint_ops *ops)
8521 {
8522 symtab_and_line sal;
8523 sal.pspace = current_program_space;
8524
8525 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8526
8527 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8528 b->disposition = tempflag ? disp_del : disp_donttouch;
8529 }
8530
8531 void
8532 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8533 {
8534 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8535 set_breakpoint_number (internal, b);
8536 if (is_tracepoint (b))
8537 set_tracepoint_count (breakpoint_count);
8538 if (!internal)
8539 mention (b);
8540 observer_notify_breakpoint_created (b);
8541
8542 if (update_gll)
8543 update_global_location_list (UGLL_MAY_INSERT);
8544 }
8545
8546 static void
8547 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8548 int tempflag, const char *cond_string,
8549 const struct breakpoint_ops *ops)
8550 {
8551 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8552
8553 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8554
8555 c->forked_inferior_pid = null_ptid;
8556
8557 install_breakpoint (0, std::move (c), 1);
8558 }
8559
8560 /* Exec catchpoints. */
8561
8562 /* An instance of this type is used to represent an exec catchpoint.
8563 A breakpoint is really of this type iff its ops pointer points to
8564 CATCH_EXEC_BREAKPOINT_OPS. */
8565
8566 struct exec_catchpoint : public breakpoint
8567 {
8568 ~exec_catchpoint () override;
8569
8570 /* Filename of a program whose exec triggered this catchpoint.
8571 This field is only valid immediately after this catchpoint has
8572 triggered. */
8573 char *exec_pathname;
8574 };
8575
8576 /* Exec catchpoint destructor. */
8577
8578 exec_catchpoint::~exec_catchpoint ()
8579 {
8580 xfree (this->exec_pathname);
8581 }
8582
8583 static int
8584 insert_catch_exec (struct bp_location *bl)
8585 {
8586 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8587 }
8588
8589 static int
8590 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8591 {
8592 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8593 }
8594
8595 static int
8596 breakpoint_hit_catch_exec (const struct bp_location *bl,
8597 struct address_space *aspace, CORE_ADDR bp_addr,
8598 const struct target_waitstatus *ws)
8599 {
8600 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8601
8602 if (ws->kind != TARGET_WAITKIND_EXECD)
8603 return 0;
8604
8605 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8606 return 1;
8607 }
8608
8609 static enum print_stop_action
8610 print_it_catch_exec (bpstat bs)
8611 {
8612 struct ui_out *uiout = current_uiout;
8613 struct breakpoint *b = bs->breakpoint_at;
8614 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8615
8616 annotate_catchpoint (b->number);
8617 maybe_print_thread_hit_breakpoint (uiout);
8618 if (b->disposition == disp_del)
8619 uiout->text ("Temporary catchpoint ");
8620 else
8621 uiout->text ("Catchpoint ");
8622 if (uiout->is_mi_like_p ())
8623 {
8624 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8625 uiout->field_string ("disp", bpdisp_text (b->disposition));
8626 }
8627 uiout->field_int ("bkptno", b->number);
8628 uiout->text (" (exec'd ");
8629 uiout->field_string ("new-exec", c->exec_pathname);
8630 uiout->text ("), ");
8631
8632 return PRINT_SRC_AND_LOC;
8633 }
8634
8635 static void
8636 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8637 {
8638 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8639 struct value_print_options opts;
8640 struct ui_out *uiout = current_uiout;
8641
8642 get_user_print_options (&opts);
8643
8644 /* Field 4, the address, is omitted (which makes the columns
8645 not line up too nicely with the headers, but the effect
8646 is relatively readable). */
8647 if (opts.addressprint)
8648 uiout->field_skip ("addr");
8649 annotate_field (5);
8650 uiout->text ("exec");
8651 if (c->exec_pathname != NULL)
8652 {
8653 uiout->text (", program \"");
8654 uiout->field_string ("what", c->exec_pathname);
8655 uiout->text ("\" ");
8656 }
8657
8658 if (uiout->is_mi_like_p ())
8659 uiout->field_string ("catch-type", "exec");
8660 }
8661
8662 static void
8663 print_mention_catch_exec (struct breakpoint *b)
8664 {
8665 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8666 }
8667
8668 /* Implement the "print_recreate" breakpoint_ops method for exec
8669 catchpoints. */
8670
8671 static void
8672 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8673 {
8674 fprintf_unfiltered (fp, "catch exec");
8675 print_recreate_thread (b, fp);
8676 }
8677
8678 static struct breakpoint_ops catch_exec_breakpoint_ops;
8679
8680 static int
8681 hw_breakpoint_used_count (void)
8682 {
8683 int i = 0;
8684 struct breakpoint *b;
8685 struct bp_location *bl;
8686
8687 ALL_BREAKPOINTS (b)
8688 {
8689 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8690 for (bl = b->loc; bl; bl = bl->next)
8691 {
8692 /* Special types of hardware breakpoints may use more than
8693 one register. */
8694 i += b->ops->resources_needed (bl);
8695 }
8696 }
8697
8698 return i;
8699 }
8700
8701 /* Returns the resources B would use if it were a hardware
8702 watchpoint. */
8703
8704 static int
8705 hw_watchpoint_use_count (struct breakpoint *b)
8706 {
8707 int i = 0;
8708 struct bp_location *bl;
8709
8710 if (!breakpoint_enabled (b))
8711 return 0;
8712
8713 for (bl = b->loc; bl; bl = bl->next)
8714 {
8715 /* Special types of hardware watchpoints may use more than
8716 one register. */
8717 i += b->ops->resources_needed (bl);
8718 }
8719
8720 return i;
8721 }
8722
8723 /* Returns the sum the used resources of all hardware watchpoints of
8724 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8725 the sum of the used resources of all hardware watchpoints of other
8726 types _not_ TYPE. */
8727
8728 static int
8729 hw_watchpoint_used_count_others (struct breakpoint *except,
8730 enum bptype type, int *other_type_used)
8731 {
8732 int i = 0;
8733 struct breakpoint *b;
8734
8735 *other_type_used = 0;
8736 ALL_BREAKPOINTS (b)
8737 {
8738 if (b == except)
8739 continue;
8740 if (!breakpoint_enabled (b))
8741 continue;
8742
8743 if (b->type == type)
8744 i += hw_watchpoint_use_count (b);
8745 else if (is_hardware_watchpoint (b))
8746 *other_type_used = 1;
8747 }
8748
8749 return i;
8750 }
8751
8752 void
8753 disable_watchpoints_before_interactive_call_start (void)
8754 {
8755 struct breakpoint *b;
8756
8757 ALL_BREAKPOINTS (b)
8758 {
8759 if (is_watchpoint (b) && breakpoint_enabled (b))
8760 {
8761 b->enable_state = bp_call_disabled;
8762 update_global_location_list (UGLL_DONT_INSERT);
8763 }
8764 }
8765 }
8766
8767 void
8768 enable_watchpoints_after_interactive_call_stop (void)
8769 {
8770 struct breakpoint *b;
8771
8772 ALL_BREAKPOINTS (b)
8773 {
8774 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8775 {
8776 b->enable_state = bp_enabled;
8777 update_global_location_list (UGLL_MAY_INSERT);
8778 }
8779 }
8780 }
8781
8782 void
8783 disable_breakpoints_before_startup (void)
8784 {
8785 current_program_space->executing_startup = 1;
8786 update_global_location_list (UGLL_DONT_INSERT);
8787 }
8788
8789 void
8790 enable_breakpoints_after_startup (void)
8791 {
8792 current_program_space->executing_startup = 0;
8793 breakpoint_re_set ();
8794 }
8795
8796 /* Create a new single-step breakpoint for thread THREAD, with no
8797 locations. */
8798
8799 static struct breakpoint *
8800 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8801 {
8802 std::unique_ptr<breakpoint> b (new breakpoint ());
8803
8804 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8805 &momentary_breakpoint_ops);
8806
8807 b->disposition = disp_donttouch;
8808 b->frame_id = null_frame_id;
8809
8810 b->thread = thread;
8811 gdb_assert (b->thread != 0);
8812
8813 return add_to_breakpoint_chain (std::move (b));
8814 }
8815
8816 /* Set a momentary breakpoint of type TYPE at address specified by
8817 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8818 frame. */
8819
8820 struct breakpoint *
8821 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8822 struct frame_id frame_id, enum bptype type)
8823 {
8824 struct breakpoint *b;
8825
8826 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8827 tail-called one. */
8828 gdb_assert (!frame_id_artificial_p (frame_id));
8829
8830 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8831 b->enable_state = bp_enabled;
8832 b->disposition = disp_donttouch;
8833 b->frame_id = frame_id;
8834
8835 /* If we're debugging a multi-threaded program, then we want
8836 momentary breakpoints to be active in only a single thread of
8837 control. */
8838 if (in_thread_list (inferior_ptid))
8839 b->thread = ptid_to_global_thread_id (inferior_ptid);
8840
8841 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8842
8843 return b;
8844 }
8845
8846 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8847 The new breakpoint will have type TYPE, use OPS as its
8848 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8849
8850 static struct breakpoint *
8851 momentary_breakpoint_from_master (struct breakpoint *orig,
8852 enum bptype type,
8853 const struct breakpoint_ops *ops,
8854 int loc_enabled)
8855 {
8856 struct breakpoint *copy;
8857
8858 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8859 copy->loc = allocate_bp_location (copy);
8860 set_breakpoint_location_function (copy->loc, 1);
8861
8862 copy->loc->gdbarch = orig->loc->gdbarch;
8863 copy->loc->requested_address = orig->loc->requested_address;
8864 copy->loc->address = orig->loc->address;
8865 copy->loc->section = orig->loc->section;
8866 copy->loc->pspace = orig->loc->pspace;
8867 copy->loc->probe = orig->loc->probe;
8868 copy->loc->line_number = orig->loc->line_number;
8869 copy->loc->symtab = orig->loc->symtab;
8870 copy->loc->enabled = loc_enabled;
8871 copy->frame_id = orig->frame_id;
8872 copy->thread = orig->thread;
8873 copy->pspace = orig->pspace;
8874
8875 copy->enable_state = bp_enabled;
8876 copy->disposition = disp_donttouch;
8877 copy->number = internal_breakpoint_number--;
8878
8879 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8880 return copy;
8881 }
8882
8883 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8884 ORIG is NULL. */
8885
8886 struct breakpoint *
8887 clone_momentary_breakpoint (struct breakpoint *orig)
8888 {
8889 /* If there's nothing to clone, then return nothing. */
8890 if (orig == NULL)
8891 return NULL;
8892
8893 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8894 }
8895
8896 struct breakpoint *
8897 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8898 enum bptype type)
8899 {
8900 struct symtab_and_line sal;
8901
8902 sal = find_pc_line (pc, 0);
8903 sal.pc = pc;
8904 sal.section = find_pc_overlay (pc);
8905 sal.explicit_pc = 1;
8906
8907 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8908 }
8909 \f
8910
8911 /* Tell the user we have just set a breakpoint B. */
8912
8913 static void
8914 mention (struct breakpoint *b)
8915 {
8916 b->ops->print_mention (b);
8917 if (current_uiout->is_mi_like_p ())
8918 return;
8919 printf_filtered ("\n");
8920 }
8921 \f
8922
8923 static int bp_loc_is_permanent (struct bp_location *loc);
8924
8925 static struct bp_location *
8926 add_location_to_breakpoint (struct breakpoint *b,
8927 const struct symtab_and_line *sal)
8928 {
8929 struct bp_location *loc, **tmp;
8930 CORE_ADDR adjusted_address;
8931 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8932
8933 if (loc_gdbarch == NULL)
8934 loc_gdbarch = b->gdbarch;
8935
8936 /* Adjust the breakpoint's address prior to allocating a location.
8937 Once we call allocate_bp_location(), that mostly uninitialized
8938 location will be placed on the location chain. Adjustment of the
8939 breakpoint may cause target_read_memory() to be called and we do
8940 not want its scan of the location chain to find a breakpoint and
8941 location that's only been partially initialized. */
8942 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8943 sal->pc, b->type);
8944
8945 /* Sort the locations by their ADDRESS. */
8946 loc = allocate_bp_location (b);
8947 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8948 tmp = &((*tmp)->next))
8949 ;
8950 loc->next = *tmp;
8951 *tmp = loc;
8952
8953 loc->requested_address = sal->pc;
8954 loc->address = adjusted_address;
8955 loc->pspace = sal->pspace;
8956 loc->probe.probe = sal->probe;
8957 loc->probe.objfile = sal->objfile;
8958 gdb_assert (loc->pspace != NULL);
8959 loc->section = sal->section;
8960 loc->gdbarch = loc_gdbarch;
8961 loc->line_number = sal->line;
8962 loc->symtab = sal->symtab;
8963
8964 set_breakpoint_location_function (loc,
8965 sal->explicit_pc || sal->explicit_line);
8966
8967 /* While by definition, permanent breakpoints are already present in the
8968 code, we don't mark the location as inserted. Normally one would expect
8969 that GDB could rely on that breakpoint instruction to stop the program,
8970 thus removing the need to insert its own breakpoint, except that executing
8971 the breakpoint instruction can kill the target instead of reporting a
8972 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8973 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8974 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8975 breakpoint be inserted normally results in QEMU knowing about the GDB
8976 breakpoint, and thus trap before the breakpoint instruction is executed.
8977 (If GDB later needs to continue execution past the permanent breakpoint,
8978 it manually increments the PC, thus avoiding executing the breakpoint
8979 instruction.) */
8980 if (bp_loc_is_permanent (loc))
8981 loc->permanent = 1;
8982
8983 return loc;
8984 }
8985 \f
8986
8987 /* See breakpoint.h. */
8988
8989 int
8990 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8991 {
8992 int len;
8993 CORE_ADDR addr;
8994 const gdb_byte *bpoint;
8995 gdb_byte *target_mem;
8996 struct cleanup *cleanup;
8997 int retval = 0;
8998
8999 addr = address;
9000 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9001
9002 /* Software breakpoints unsupported? */
9003 if (bpoint == NULL)
9004 return 0;
9005
9006 target_mem = (gdb_byte *) alloca (len);
9007
9008 /* Enable the automatic memory restoration from breakpoints while
9009 we read the memory. Otherwise we could say about our temporary
9010 breakpoints they are permanent. */
9011 cleanup = make_show_memory_breakpoints_cleanup (0);
9012
9013 if (target_read_memory (address, target_mem, len) == 0
9014 && memcmp (target_mem, bpoint, len) == 0)
9015 retval = 1;
9016
9017 do_cleanups (cleanup);
9018
9019 return retval;
9020 }
9021
9022 /* Return 1 if LOC is pointing to a permanent breakpoint,
9023 return 0 otherwise. */
9024
9025 static int
9026 bp_loc_is_permanent (struct bp_location *loc)
9027 {
9028 gdb_assert (loc != NULL);
9029
9030 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9031 attempt to read from the addresses the locations of these breakpoint types
9032 point to. program_breakpoint_here_p, below, will attempt to read
9033 memory. */
9034 if (!breakpoint_address_is_meaningful (loc->owner))
9035 return 0;
9036
9037 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9038 switch_to_program_space_and_thread (loc->pspace);
9039 return program_breakpoint_here_p (loc->gdbarch, loc->address);
9040 }
9041
9042 /* Build a command list for the dprintf corresponding to the current
9043 settings of the dprintf style options. */
9044
9045 static void
9046 update_dprintf_command_list (struct breakpoint *b)
9047 {
9048 char *dprintf_args = b->extra_string;
9049 char *printf_line = NULL;
9050
9051 if (!dprintf_args)
9052 return;
9053
9054 dprintf_args = skip_spaces (dprintf_args);
9055
9056 /* Allow a comma, as it may have terminated a location, but don't
9057 insist on it. */
9058 if (*dprintf_args == ',')
9059 ++dprintf_args;
9060 dprintf_args = skip_spaces (dprintf_args);
9061
9062 if (*dprintf_args != '"')
9063 error (_("Bad format string, missing '\"'."));
9064
9065 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9066 printf_line = xstrprintf ("printf %s", dprintf_args);
9067 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9068 {
9069 if (!dprintf_function)
9070 error (_("No function supplied for dprintf call"));
9071
9072 if (dprintf_channel && strlen (dprintf_channel) > 0)
9073 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9074 dprintf_function,
9075 dprintf_channel,
9076 dprintf_args);
9077 else
9078 printf_line = xstrprintf ("call (void) %s (%s)",
9079 dprintf_function,
9080 dprintf_args);
9081 }
9082 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9083 {
9084 if (target_can_run_breakpoint_commands ())
9085 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9086 else
9087 {
9088 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9089 printf_line = xstrprintf ("printf %s", dprintf_args);
9090 }
9091 }
9092 else
9093 internal_error (__FILE__, __LINE__,
9094 _("Invalid dprintf style."));
9095
9096 gdb_assert (printf_line != NULL);
9097 /* Manufacture a printf sequence. */
9098 {
9099 struct command_line *printf_cmd_line = XNEW (struct command_line);
9100
9101 printf_cmd_line->control_type = simple_control;
9102 printf_cmd_line->body_count = 0;
9103 printf_cmd_line->body_list = NULL;
9104 printf_cmd_line->next = NULL;
9105 printf_cmd_line->line = printf_line;
9106
9107 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9108 }
9109 }
9110
9111 /* Update all dprintf commands, making their command lists reflect
9112 current style settings. */
9113
9114 static void
9115 update_dprintf_commands (char *args, int from_tty,
9116 struct cmd_list_element *c)
9117 {
9118 struct breakpoint *b;
9119
9120 ALL_BREAKPOINTS (b)
9121 {
9122 if (b->type == bp_dprintf)
9123 update_dprintf_command_list (b);
9124 }
9125 }
9126
9127 /* Create a breakpoint with SAL as location. Use LOCATION
9128 as a description of the location, and COND_STRING
9129 as condition expression. If LOCATION is NULL then create an
9130 "address location" from the address in the SAL. */
9131
9132 static void
9133 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9134 gdb::array_view<const symtab_and_line> sals,
9135 event_location_up &&location,
9136 gdb::unique_xmalloc_ptr<char> filter,
9137 gdb::unique_xmalloc_ptr<char> cond_string,
9138 gdb::unique_xmalloc_ptr<char> extra_string,
9139 enum bptype type, enum bpdisp disposition,
9140 int thread, int task, int ignore_count,
9141 const struct breakpoint_ops *ops, int from_tty,
9142 int enabled, int internal, unsigned flags,
9143 int display_canonical)
9144 {
9145 int i;
9146
9147 if (type == bp_hardware_breakpoint)
9148 {
9149 int target_resources_ok;
9150
9151 i = hw_breakpoint_used_count ();
9152 target_resources_ok =
9153 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9154 i + 1, 0);
9155 if (target_resources_ok == 0)
9156 error (_("No hardware breakpoint support in the target."));
9157 else if (target_resources_ok < 0)
9158 error (_("Hardware breakpoints used exceeds limit."));
9159 }
9160
9161 gdb_assert (!sals.empty ());
9162
9163 for (const auto &sal : sals)
9164 {
9165 struct bp_location *loc;
9166
9167 if (from_tty)
9168 {
9169 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9170 if (!loc_gdbarch)
9171 loc_gdbarch = gdbarch;
9172
9173 describe_other_breakpoints (loc_gdbarch,
9174 sal.pspace, sal.pc, sal.section, thread);
9175 }
9176
9177 if (&sal == &sals[0])
9178 {
9179 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9180 b->thread = thread;
9181 b->task = task;
9182
9183 b->cond_string = cond_string.release ();
9184 b->extra_string = extra_string.release ();
9185 b->ignore_count = ignore_count;
9186 b->enable_state = enabled ? bp_enabled : bp_disabled;
9187 b->disposition = disposition;
9188
9189 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9190 b->loc->inserted = 1;
9191
9192 if (type == bp_static_tracepoint)
9193 {
9194 struct tracepoint *t = (struct tracepoint *) b;
9195 struct static_tracepoint_marker marker;
9196
9197 if (strace_marker_p (b))
9198 {
9199 /* We already know the marker exists, otherwise, we
9200 wouldn't see a sal for it. */
9201 const char *p
9202 = &event_location_to_string (b->location.get ())[3];
9203 const char *endp;
9204 char *marker_str;
9205
9206 p = skip_spaces_const (p);
9207
9208 endp = skip_to_space_const (p);
9209
9210 marker_str = savestring (p, endp - p);
9211 t->static_trace_marker_id = marker_str;
9212
9213 printf_filtered (_("Probed static tracepoint "
9214 "marker \"%s\"\n"),
9215 t->static_trace_marker_id);
9216 }
9217 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9218 {
9219 t->static_trace_marker_id = xstrdup (marker.str_id);
9220 release_static_tracepoint_marker (&marker);
9221
9222 printf_filtered (_("Probed static tracepoint "
9223 "marker \"%s\"\n"),
9224 t->static_trace_marker_id);
9225 }
9226 else
9227 warning (_("Couldn't determine the static "
9228 "tracepoint marker to probe"));
9229 }
9230
9231 loc = b->loc;
9232 }
9233 else
9234 {
9235 loc = add_location_to_breakpoint (b, &sal);
9236 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9237 loc->inserted = 1;
9238 }
9239
9240 if (b->cond_string)
9241 {
9242 const char *arg = b->cond_string;
9243
9244 loc->cond = parse_exp_1 (&arg, loc->address,
9245 block_for_pc (loc->address), 0);
9246 if (*arg)
9247 error (_("Garbage '%s' follows condition"), arg);
9248 }
9249
9250 /* Dynamic printf requires and uses additional arguments on the
9251 command line, otherwise it's an error. */
9252 if (type == bp_dprintf)
9253 {
9254 if (b->extra_string)
9255 update_dprintf_command_list (b);
9256 else
9257 error (_("Format string required"));
9258 }
9259 else if (b->extra_string)
9260 error (_("Garbage '%s' at end of command"), b->extra_string);
9261 }
9262
9263 b->display_canonical = display_canonical;
9264 if (location != NULL)
9265 b->location = std::move (location);
9266 else
9267 b->location = new_address_location (b->loc->address, NULL, 0);
9268 b->filter = filter.release ();
9269 }
9270
9271 static void
9272 create_breakpoint_sal (struct gdbarch *gdbarch,
9273 gdb::array_view<const symtab_and_line> sals,
9274 event_location_up &&location,
9275 gdb::unique_xmalloc_ptr<char> filter,
9276 gdb::unique_xmalloc_ptr<char> cond_string,
9277 gdb::unique_xmalloc_ptr<char> extra_string,
9278 enum bptype type, enum bpdisp disposition,
9279 int thread, int task, int ignore_count,
9280 const struct breakpoint_ops *ops, int from_tty,
9281 int enabled, int internal, unsigned flags,
9282 int display_canonical)
9283 {
9284 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9285
9286 init_breakpoint_sal (b.get (), gdbarch,
9287 sals, std::move (location),
9288 std::move (filter),
9289 std::move (cond_string),
9290 std::move (extra_string),
9291 type, disposition,
9292 thread, task, ignore_count,
9293 ops, from_tty,
9294 enabled, internal, flags,
9295 display_canonical);
9296
9297 install_breakpoint (internal, std::move (b), 0);
9298 }
9299
9300 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9301 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9302 value. COND_STRING, if not NULL, specified the condition to be
9303 used for all breakpoints. Essentially the only case where
9304 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9305 function. In that case, it's still not possible to specify
9306 separate conditions for different overloaded functions, so
9307 we take just a single condition string.
9308
9309 NOTE: If the function succeeds, the caller is expected to cleanup
9310 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9311 array contents). If the function fails (error() is called), the
9312 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9313 COND and SALS arrays and each of those arrays contents. */
9314
9315 static void
9316 create_breakpoints_sal (struct gdbarch *gdbarch,
9317 struct linespec_result *canonical,
9318 gdb::unique_xmalloc_ptr<char> cond_string,
9319 gdb::unique_xmalloc_ptr<char> extra_string,
9320 enum bptype type, enum bpdisp disposition,
9321 int thread, int task, int ignore_count,
9322 const struct breakpoint_ops *ops, int from_tty,
9323 int enabled, int internal, unsigned flags)
9324 {
9325 if (canonical->pre_expanded)
9326 gdb_assert (canonical->lsals.size () == 1);
9327
9328 for (const auto &lsal : canonical->lsals)
9329 {
9330 /* Note that 'location' can be NULL in the case of a plain
9331 'break', without arguments. */
9332 event_location_up location
9333 = (canonical->location != NULL
9334 ? copy_event_location (canonical->location.get ()) : NULL);
9335 gdb::unique_xmalloc_ptr<char> filter_string
9336 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9337
9338 create_breakpoint_sal (gdbarch, lsal.sals,
9339 std::move (location),
9340 std::move (filter_string),
9341 std::move (cond_string),
9342 std::move (extra_string),
9343 type, disposition,
9344 thread, task, ignore_count, ops,
9345 from_tty, enabled, internal, flags,
9346 canonical->special_display);
9347 }
9348 }
9349
9350 /* Parse LOCATION which is assumed to be a SAL specification possibly
9351 followed by conditionals. On return, SALS contains an array of SAL
9352 addresses found. LOCATION points to the end of the SAL (for
9353 linespec locations).
9354
9355 The array and the line spec strings are allocated on the heap, it is
9356 the caller's responsibility to free them. */
9357
9358 static void
9359 parse_breakpoint_sals (const struct event_location *location,
9360 struct linespec_result *canonical)
9361 {
9362 struct symtab_and_line cursal;
9363
9364 if (event_location_type (location) == LINESPEC_LOCATION)
9365 {
9366 const char *address = get_linespec_location (location);
9367
9368 if (address == NULL)
9369 {
9370 /* The last displayed codepoint, if it's valid, is our default
9371 breakpoint address. */
9372 if (last_displayed_sal_is_valid ())
9373 {
9374 /* Set sal's pspace, pc, symtab, and line to the values
9375 corresponding to the last call to print_frame_info.
9376 Be sure to reinitialize LINE with NOTCURRENT == 0
9377 as the breakpoint line number is inappropriate otherwise.
9378 find_pc_line would adjust PC, re-set it back. */
9379 symtab_and_line sal = get_last_displayed_sal ();
9380 CORE_ADDR pc = sal.pc;
9381
9382 sal = find_pc_line (pc, 0);
9383
9384 /* "break" without arguments is equivalent to "break *PC"
9385 where PC is the last displayed codepoint's address. So
9386 make sure to set sal.explicit_pc to prevent GDB from
9387 trying to expand the list of sals to include all other
9388 instances with the same symtab and line. */
9389 sal.pc = pc;
9390 sal.explicit_pc = 1;
9391
9392 struct linespec_sals lsal;
9393 lsal.sals = {sal};
9394 lsal.canonical = NULL;
9395
9396 canonical->lsals.push_back (std::move (lsal));
9397 return;
9398 }
9399 else
9400 error (_("No default breakpoint address now."));
9401 }
9402 }
9403
9404 /* Force almost all breakpoints to be in terms of the
9405 current_source_symtab (which is decode_line_1's default).
9406 This should produce the results we want almost all of the
9407 time while leaving default_breakpoint_* alone.
9408
9409 ObjC: However, don't match an Objective-C method name which
9410 may have a '+' or '-' succeeded by a '['. */
9411 cursal = get_current_source_symtab_and_line ();
9412 if (last_displayed_sal_is_valid ())
9413 {
9414 const char *address = NULL;
9415
9416 if (event_location_type (location) == LINESPEC_LOCATION)
9417 address = get_linespec_location (location);
9418
9419 if (!cursal.symtab
9420 || (address != NULL
9421 && strchr ("+-", address[0]) != NULL
9422 && address[1] != '['))
9423 {
9424 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9425 get_last_displayed_symtab (),
9426 get_last_displayed_line (),
9427 canonical, NULL, NULL);
9428 return;
9429 }
9430 }
9431
9432 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9433 cursal.symtab, cursal.line, canonical, NULL, NULL);
9434 }
9435
9436
9437 /* Convert each SAL into a real PC. Verify that the PC can be
9438 inserted as a breakpoint. If it can't throw an error. */
9439
9440 static void
9441 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9442 {
9443 for (auto &sal : sals)
9444 resolve_sal_pc (&sal);
9445 }
9446
9447 /* Fast tracepoints may have restrictions on valid locations. For
9448 instance, a fast tracepoint using a jump instead of a trap will
9449 likely have to overwrite more bytes than a trap would, and so can
9450 only be placed where the instruction is longer than the jump, or a
9451 multi-instruction sequence does not have a jump into the middle of
9452 it, etc. */
9453
9454 static void
9455 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9456 gdb::array_view<const symtab_and_line> sals)
9457 {
9458 int rslt;
9459 char *msg;
9460 struct cleanup *old_chain;
9461
9462 for (const auto &sal : sals)
9463 {
9464 struct gdbarch *sarch;
9465
9466 sarch = get_sal_arch (sal);
9467 /* We fall back to GDBARCH if there is no architecture
9468 associated with SAL. */
9469 if (sarch == NULL)
9470 sarch = gdbarch;
9471 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9472 old_chain = make_cleanup (xfree, msg);
9473
9474 if (!rslt)
9475 error (_("May not have a fast tracepoint at %s%s"),
9476 paddress (sarch, sal.pc), (msg ? msg : ""));
9477
9478 do_cleanups (old_chain);
9479 }
9480 }
9481
9482 /* Given TOK, a string specification of condition and thread, as
9483 accepted by the 'break' command, extract the condition
9484 string and thread number and set *COND_STRING and *THREAD.
9485 PC identifies the context at which the condition should be parsed.
9486 If no condition is found, *COND_STRING is set to NULL.
9487 If no thread is found, *THREAD is set to -1. */
9488
9489 static void
9490 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9491 char **cond_string, int *thread, int *task,
9492 char **rest)
9493 {
9494 *cond_string = NULL;
9495 *thread = -1;
9496 *task = 0;
9497 *rest = NULL;
9498
9499 while (tok && *tok)
9500 {
9501 const char *end_tok;
9502 int toklen;
9503 const char *cond_start = NULL;
9504 const char *cond_end = NULL;
9505
9506 tok = skip_spaces_const (tok);
9507
9508 if ((*tok == '"' || *tok == ',') && rest)
9509 {
9510 *rest = savestring (tok, strlen (tok));
9511 return;
9512 }
9513
9514 end_tok = skip_to_space_const (tok);
9515
9516 toklen = end_tok - tok;
9517
9518 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9519 {
9520 tok = cond_start = end_tok + 1;
9521 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9522 cond_end = tok;
9523 *cond_string = savestring (cond_start, cond_end - cond_start);
9524 }
9525 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9526 {
9527 const char *tmptok;
9528 struct thread_info *thr;
9529
9530 tok = end_tok + 1;
9531 thr = parse_thread_id (tok, &tmptok);
9532 if (tok == tmptok)
9533 error (_("Junk after thread keyword."));
9534 *thread = thr->global_num;
9535 tok = tmptok;
9536 }
9537 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9538 {
9539 char *tmptok;
9540
9541 tok = end_tok + 1;
9542 *task = strtol (tok, &tmptok, 0);
9543 if (tok == tmptok)
9544 error (_("Junk after task keyword."));
9545 if (!valid_task_id (*task))
9546 error (_("Unknown task %d."), *task);
9547 tok = tmptok;
9548 }
9549 else if (rest)
9550 {
9551 *rest = savestring (tok, strlen (tok));
9552 return;
9553 }
9554 else
9555 error (_("Junk at end of arguments."));
9556 }
9557 }
9558
9559 /* Decode a static tracepoint marker spec. */
9560
9561 static std::vector<symtab_and_line>
9562 decode_static_tracepoint_spec (const char **arg_p)
9563 {
9564 VEC(static_tracepoint_marker_p) *markers = NULL;
9565 struct cleanup *old_chain;
9566 const char *p = &(*arg_p)[3];
9567 const char *endp;
9568 char *marker_str;
9569 int i;
9570
9571 p = skip_spaces_const (p);
9572
9573 endp = skip_to_space_const (p);
9574
9575 marker_str = savestring (p, endp - p);
9576 old_chain = make_cleanup (xfree, marker_str);
9577
9578 markers = target_static_tracepoint_markers_by_strid (marker_str);
9579 if (VEC_empty(static_tracepoint_marker_p, markers))
9580 error (_("No known static tracepoint marker named %s"), marker_str);
9581
9582 std::vector<symtab_and_line> sals;
9583 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9584
9585 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9586 {
9587 struct static_tracepoint_marker *marker;
9588
9589 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9590
9591 symtab_and_line sal = find_pc_line (marker->address, 0);
9592 sal.pc = marker->address;
9593 sals.push_back (sal);
9594
9595 release_static_tracepoint_marker (marker);
9596 }
9597
9598 do_cleanups (old_chain);
9599
9600 *arg_p = endp;
9601 return sals;
9602 }
9603
9604 /* See breakpoint.h. */
9605
9606 int
9607 create_breakpoint (struct gdbarch *gdbarch,
9608 const struct event_location *location,
9609 const char *cond_string,
9610 int thread, const char *extra_string,
9611 int parse_extra,
9612 int tempflag, enum bptype type_wanted,
9613 int ignore_count,
9614 enum auto_boolean pending_break_support,
9615 const struct breakpoint_ops *ops,
9616 int from_tty, int enabled, int internal,
9617 unsigned flags)
9618 {
9619 struct linespec_result canonical;
9620 struct cleanup *bkpt_chain = NULL;
9621 int pending = 0;
9622 int task = 0;
9623 int prev_bkpt_count = breakpoint_count;
9624
9625 gdb_assert (ops != NULL);
9626
9627 /* If extra_string isn't useful, set it to NULL. */
9628 if (extra_string != NULL && *extra_string == '\0')
9629 extra_string = NULL;
9630
9631 TRY
9632 {
9633 ops->create_sals_from_location (location, &canonical, type_wanted);
9634 }
9635 CATCH (e, RETURN_MASK_ERROR)
9636 {
9637 /* If caller is interested in rc value from parse, set
9638 value. */
9639 if (e.error == NOT_FOUND_ERROR)
9640 {
9641 /* If pending breakpoint support is turned off, throw
9642 error. */
9643
9644 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9645 throw_exception (e);
9646
9647 exception_print (gdb_stderr, e);
9648
9649 /* If pending breakpoint support is auto query and the user
9650 selects no, then simply return the error code. */
9651 if (pending_break_support == AUTO_BOOLEAN_AUTO
9652 && !nquery (_("Make %s pending on future shared library load? "),
9653 bptype_string (type_wanted)))
9654 return 0;
9655
9656 /* At this point, either the user was queried about setting
9657 a pending breakpoint and selected yes, or pending
9658 breakpoint behavior is on and thus a pending breakpoint
9659 is defaulted on behalf of the user. */
9660 pending = 1;
9661 }
9662 else
9663 throw_exception (e);
9664 }
9665 END_CATCH
9666
9667 if (!pending && canonical.lsals.empty ())
9668 return 0;
9669
9670 /* ----------------------------- SNIP -----------------------------
9671 Anything added to the cleanup chain beyond this point is assumed
9672 to be part of a breakpoint. If the breakpoint create succeeds
9673 then the memory is not reclaimed. */
9674 bkpt_chain = make_cleanup (null_cleanup, 0);
9675
9676 /* Resolve all line numbers to PC's and verify that the addresses
9677 are ok for the target. */
9678 if (!pending)
9679 {
9680 for (auto &lsal : canonical.lsals)
9681 breakpoint_sals_to_pc (lsal.sals);
9682 }
9683
9684 /* Fast tracepoints may have additional restrictions on location. */
9685 if (!pending && type_wanted == bp_fast_tracepoint)
9686 {
9687 for (const auto &lsal : canonical.lsals)
9688 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9689 }
9690
9691 /* Verify that condition can be parsed, before setting any
9692 breakpoints. Allocate a separate condition expression for each
9693 breakpoint. */
9694 if (!pending)
9695 {
9696 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9697 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9698
9699 if (parse_extra)
9700 {
9701 char *rest;
9702 char *cond;
9703
9704 const linespec_sals &lsal = canonical.lsals[0];
9705
9706 /* Here we only parse 'arg' to separate condition
9707 from thread number, so parsing in context of first
9708 sal is OK. When setting the breakpoint we'll
9709 re-parse it in context of each sal. */
9710
9711 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9712 &cond, &thread, &task, &rest);
9713 cond_string_copy.reset (cond);
9714 extra_string_copy.reset (rest);
9715 }
9716 else
9717 {
9718 if (type_wanted != bp_dprintf
9719 && extra_string != NULL && *extra_string != '\0')
9720 error (_("Garbage '%s' at end of location"), extra_string);
9721
9722 /* Create a private copy of condition string. */
9723 if (cond_string)
9724 cond_string_copy.reset (xstrdup (cond_string));
9725 /* Create a private copy of any extra string. */
9726 if (extra_string)
9727 extra_string_copy.reset (xstrdup (extra_string));
9728 }
9729
9730 ops->create_breakpoints_sal (gdbarch, &canonical,
9731 std::move (cond_string_copy),
9732 std::move (extra_string_copy),
9733 type_wanted,
9734 tempflag ? disp_del : disp_donttouch,
9735 thread, task, ignore_count, ops,
9736 from_tty, enabled, internal, flags);
9737 }
9738 else
9739 {
9740 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9741
9742 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9743 b->location = copy_event_location (location);
9744
9745 if (parse_extra)
9746 b->cond_string = NULL;
9747 else
9748 {
9749 /* Create a private copy of condition string. */
9750 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9751 b->thread = thread;
9752 }
9753
9754 /* Create a private copy of any extra string. */
9755 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9756 b->ignore_count = ignore_count;
9757 b->disposition = tempflag ? disp_del : disp_donttouch;
9758 b->condition_not_parsed = 1;
9759 b->enable_state = enabled ? bp_enabled : bp_disabled;
9760 if ((type_wanted != bp_breakpoint
9761 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9762 b->pspace = current_program_space;
9763
9764 install_breakpoint (internal, std::move (b), 0);
9765 }
9766
9767 if (canonical.lsals.size () > 1)
9768 {
9769 warning (_("Multiple breakpoints were set.\nUse the "
9770 "\"delete\" command to delete unwanted breakpoints."));
9771 prev_breakpoint_count = prev_bkpt_count;
9772 }
9773
9774 /* That's it. Discard the cleanups for data inserted into the
9775 breakpoint. */
9776 discard_cleanups (bkpt_chain);
9777
9778 /* error call may happen here - have BKPT_CHAIN already discarded. */
9779 update_global_location_list (UGLL_MAY_INSERT);
9780
9781 return 1;
9782 }
9783
9784 /* Set a breakpoint.
9785 ARG is a string describing breakpoint address,
9786 condition, and thread.
9787 FLAG specifies if a breakpoint is hardware on,
9788 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9789 and BP_TEMPFLAG. */
9790
9791 static void
9792 break_command_1 (char *arg, int flag, int from_tty)
9793 {
9794 int tempflag = flag & BP_TEMPFLAG;
9795 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9796 ? bp_hardware_breakpoint
9797 : bp_breakpoint);
9798 struct breakpoint_ops *ops;
9799
9800 event_location_up location = string_to_event_location (&arg, current_language);
9801
9802 /* Matching breakpoints on probes. */
9803 if (location != NULL
9804 && event_location_type (location.get ()) == PROBE_LOCATION)
9805 ops = &bkpt_probe_breakpoint_ops;
9806 else
9807 ops = &bkpt_breakpoint_ops;
9808
9809 create_breakpoint (get_current_arch (),
9810 location.get (),
9811 NULL, 0, arg, 1 /* parse arg */,
9812 tempflag, type_wanted,
9813 0 /* Ignore count */,
9814 pending_break_support,
9815 ops,
9816 from_tty,
9817 1 /* enabled */,
9818 0 /* internal */,
9819 0);
9820 }
9821
9822 /* Helper function for break_command_1 and disassemble_command. */
9823
9824 void
9825 resolve_sal_pc (struct symtab_and_line *sal)
9826 {
9827 CORE_ADDR pc;
9828
9829 if (sal->pc == 0 && sal->symtab != NULL)
9830 {
9831 if (!find_line_pc (sal->symtab, sal->line, &pc))
9832 error (_("No line %d in file \"%s\"."),
9833 sal->line, symtab_to_filename_for_display (sal->symtab));
9834 sal->pc = pc;
9835
9836 /* If this SAL corresponds to a breakpoint inserted using a line
9837 number, then skip the function prologue if necessary. */
9838 if (sal->explicit_line)
9839 skip_prologue_sal (sal);
9840 }
9841
9842 if (sal->section == 0 && sal->symtab != NULL)
9843 {
9844 const struct blockvector *bv;
9845 const struct block *b;
9846 struct symbol *sym;
9847
9848 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9849 SYMTAB_COMPUNIT (sal->symtab));
9850 if (bv != NULL)
9851 {
9852 sym = block_linkage_function (b);
9853 if (sym != NULL)
9854 {
9855 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9856 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9857 sym);
9858 }
9859 else
9860 {
9861 /* It really is worthwhile to have the section, so we'll
9862 just have to look harder. This case can be executed
9863 if we have line numbers but no functions (as can
9864 happen in assembly source). */
9865
9866 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9867 switch_to_program_space_and_thread (sal->pspace);
9868
9869 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9870 if (msym.minsym)
9871 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9872 }
9873 }
9874 }
9875 }
9876
9877 void
9878 break_command (char *arg, int from_tty)
9879 {
9880 break_command_1 (arg, 0, from_tty);
9881 }
9882
9883 void
9884 tbreak_command (char *arg, int from_tty)
9885 {
9886 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9887 }
9888
9889 static void
9890 hbreak_command (char *arg, int from_tty)
9891 {
9892 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9893 }
9894
9895 static void
9896 thbreak_command (char *arg, int from_tty)
9897 {
9898 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9899 }
9900
9901 static void
9902 stop_command (char *arg, int from_tty)
9903 {
9904 printf_filtered (_("Specify the type of breakpoint to set.\n\
9905 Usage: stop in <function | address>\n\
9906 stop at <line>\n"));
9907 }
9908
9909 static void
9910 stopin_command (char *arg, int from_tty)
9911 {
9912 int badInput = 0;
9913
9914 if (arg == (char *) NULL)
9915 badInput = 1;
9916 else if (*arg != '*')
9917 {
9918 char *argptr = arg;
9919 int hasColon = 0;
9920
9921 /* Look for a ':'. If this is a line number specification, then
9922 say it is bad, otherwise, it should be an address or
9923 function/method name. */
9924 while (*argptr && !hasColon)
9925 {
9926 hasColon = (*argptr == ':');
9927 argptr++;
9928 }
9929
9930 if (hasColon)
9931 badInput = (*argptr != ':'); /* Not a class::method */
9932 else
9933 badInput = isdigit (*arg); /* a simple line number */
9934 }
9935
9936 if (badInput)
9937 printf_filtered (_("Usage: stop in <function | address>\n"));
9938 else
9939 break_command_1 (arg, 0, from_tty);
9940 }
9941
9942 static void
9943 stopat_command (char *arg, int from_tty)
9944 {
9945 int badInput = 0;
9946
9947 if (arg == (char *) NULL || *arg == '*') /* no line number */
9948 badInput = 1;
9949 else
9950 {
9951 char *argptr = arg;
9952 int hasColon = 0;
9953
9954 /* Look for a ':'. If there is a '::' then get out, otherwise
9955 it is probably a line number. */
9956 while (*argptr && !hasColon)
9957 {
9958 hasColon = (*argptr == ':');
9959 argptr++;
9960 }
9961
9962 if (hasColon)
9963 badInput = (*argptr == ':'); /* we have class::method */
9964 else
9965 badInput = !isdigit (*arg); /* not a line number */
9966 }
9967
9968 if (badInput)
9969 printf_filtered (_("Usage: stop at <line>\n"));
9970 else
9971 break_command_1 (arg, 0, from_tty);
9972 }
9973
9974 /* The dynamic printf command is mostly like a regular breakpoint, but
9975 with a prewired command list consisting of a single output command,
9976 built from extra arguments supplied on the dprintf command
9977 line. */
9978
9979 static void
9980 dprintf_command (char *arg, int from_tty)
9981 {
9982 event_location_up location = string_to_event_location (&arg, current_language);
9983
9984 /* If non-NULL, ARG should have been advanced past the location;
9985 the next character must be ','. */
9986 if (arg != NULL)
9987 {
9988 if (arg[0] != ',' || arg[1] == '\0')
9989 error (_("Format string required"));
9990 else
9991 {
9992 /* Skip the comma. */
9993 ++arg;
9994 }
9995 }
9996
9997 create_breakpoint (get_current_arch (),
9998 location.get (),
9999 NULL, 0, arg, 1 /* parse arg */,
10000 0, bp_dprintf,
10001 0 /* Ignore count */,
10002 pending_break_support,
10003 &dprintf_breakpoint_ops,
10004 from_tty,
10005 1 /* enabled */,
10006 0 /* internal */,
10007 0);
10008 }
10009
10010 static void
10011 agent_printf_command (char *arg, int from_tty)
10012 {
10013 error (_("May only run agent-printf on the target"));
10014 }
10015
10016 /* Implement the "breakpoint_hit" breakpoint_ops method for
10017 ranged breakpoints. */
10018
10019 static int
10020 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10021 struct address_space *aspace,
10022 CORE_ADDR bp_addr,
10023 const struct target_waitstatus *ws)
10024 {
10025 if (ws->kind != TARGET_WAITKIND_STOPPED
10026 || ws->value.sig != GDB_SIGNAL_TRAP)
10027 return 0;
10028
10029 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10030 bl->length, aspace, bp_addr);
10031 }
10032
10033 /* Implement the "resources_needed" breakpoint_ops method for
10034 ranged breakpoints. */
10035
10036 static int
10037 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10038 {
10039 return target_ranged_break_num_registers ();
10040 }
10041
10042 /* Implement the "print_it" breakpoint_ops method for
10043 ranged breakpoints. */
10044
10045 static enum print_stop_action
10046 print_it_ranged_breakpoint (bpstat bs)
10047 {
10048 struct breakpoint *b = bs->breakpoint_at;
10049 struct bp_location *bl = b->loc;
10050 struct ui_out *uiout = current_uiout;
10051
10052 gdb_assert (b->type == bp_hardware_breakpoint);
10053
10054 /* Ranged breakpoints have only one location. */
10055 gdb_assert (bl && bl->next == NULL);
10056
10057 annotate_breakpoint (b->number);
10058
10059 maybe_print_thread_hit_breakpoint (uiout);
10060
10061 if (b->disposition == disp_del)
10062 uiout->text ("Temporary ranged breakpoint ");
10063 else
10064 uiout->text ("Ranged breakpoint ");
10065 if (uiout->is_mi_like_p ())
10066 {
10067 uiout->field_string ("reason",
10068 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10069 uiout->field_string ("disp", bpdisp_text (b->disposition));
10070 }
10071 uiout->field_int ("bkptno", b->number);
10072 uiout->text (", ");
10073
10074 return PRINT_SRC_AND_LOC;
10075 }
10076
10077 /* Implement the "print_one" breakpoint_ops method for
10078 ranged breakpoints. */
10079
10080 static void
10081 print_one_ranged_breakpoint (struct breakpoint *b,
10082 struct bp_location **last_loc)
10083 {
10084 struct bp_location *bl = b->loc;
10085 struct value_print_options opts;
10086 struct ui_out *uiout = current_uiout;
10087
10088 /* Ranged breakpoints have only one location. */
10089 gdb_assert (bl && bl->next == NULL);
10090
10091 get_user_print_options (&opts);
10092
10093 if (opts.addressprint)
10094 /* We don't print the address range here, it will be printed later
10095 by print_one_detail_ranged_breakpoint. */
10096 uiout->field_skip ("addr");
10097 annotate_field (5);
10098 print_breakpoint_location (b, bl);
10099 *last_loc = bl;
10100 }
10101
10102 /* Implement the "print_one_detail" breakpoint_ops method for
10103 ranged breakpoints. */
10104
10105 static void
10106 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10107 struct ui_out *uiout)
10108 {
10109 CORE_ADDR address_start, address_end;
10110 struct bp_location *bl = b->loc;
10111 string_file stb;
10112
10113 gdb_assert (bl);
10114
10115 address_start = bl->address;
10116 address_end = address_start + bl->length - 1;
10117
10118 uiout->text ("\taddress range: ");
10119 stb.printf ("[%s, %s]",
10120 print_core_address (bl->gdbarch, address_start),
10121 print_core_address (bl->gdbarch, address_end));
10122 uiout->field_stream ("addr", stb);
10123 uiout->text ("\n");
10124 }
10125
10126 /* Implement the "print_mention" breakpoint_ops method for
10127 ranged breakpoints. */
10128
10129 static void
10130 print_mention_ranged_breakpoint (struct breakpoint *b)
10131 {
10132 struct bp_location *bl = b->loc;
10133 struct ui_out *uiout = current_uiout;
10134
10135 gdb_assert (bl);
10136 gdb_assert (b->type == bp_hardware_breakpoint);
10137
10138 if (uiout->is_mi_like_p ())
10139 return;
10140
10141 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10142 b->number, paddress (bl->gdbarch, bl->address),
10143 paddress (bl->gdbarch, bl->address + bl->length - 1));
10144 }
10145
10146 /* Implement the "print_recreate" breakpoint_ops method for
10147 ranged breakpoints. */
10148
10149 static void
10150 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10151 {
10152 fprintf_unfiltered (fp, "break-range %s, %s",
10153 event_location_to_string (b->location.get ()),
10154 event_location_to_string (b->location_range_end.get ()));
10155 print_recreate_thread (b, fp);
10156 }
10157
10158 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10159
10160 static struct breakpoint_ops ranged_breakpoint_ops;
10161
10162 /* Find the address where the end of the breakpoint range should be
10163 placed, given the SAL of the end of the range. This is so that if
10164 the user provides a line number, the end of the range is set to the
10165 last instruction of the given line. */
10166
10167 static CORE_ADDR
10168 find_breakpoint_range_end (struct symtab_and_line sal)
10169 {
10170 CORE_ADDR end;
10171
10172 /* If the user provided a PC value, use it. Otherwise,
10173 find the address of the end of the given location. */
10174 if (sal.explicit_pc)
10175 end = sal.pc;
10176 else
10177 {
10178 int ret;
10179 CORE_ADDR start;
10180
10181 ret = find_line_pc_range (sal, &start, &end);
10182 if (!ret)
10183 error (_("Could not find location of the end of the range."));
10184
10185 /* find_line_pc_range returns the start of the next line. */
10186 end--;
10187 }
10188
10189 return end;
10190 }
10191
10192 /* Implement the "break-range" CLI command. */
10193
10194 static void
10195 break_range_command (char *arg, int from_tty)
10196 {
10197 char *arg_start, *addr_string_start;
10198 struct linespec_result canonical_start, canonical_end;
10199 int bp_count, can_use_bp, length;
10200 CORE_ADDR end;
10201 struct breakpoint *b;
10202 struct cleanup *cleanup_bkpt;
10203
10204 /* We don't support software ranged breakpoints. */
10205 if (target_ranged_break_num_registers () < 0)
10206 error (_("This target does not support hardware ranged breakpoints."));
10207
10208 bp_count = hw_breakpoint_used_count ();
10209 bp_count += target_ranged_break_num_registers ();
10210 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10211 bp_count, 0);
10212 if (can_use_bp < 0)
10213 error (_("Hardware breakpoints used exceeds limit."));
10214
10215 arg = skip_spaces (arg);
10216 if (arg == NULL || arg[0] == '\0')
10217 error(_("No address range specified."));
10218
10219 arg_start = arg;
10220 event_location_up start_location = string_to_event_location (&arg,
10221 current_language);
10222 parse_breakpoint_sals (start_location.get (), &canonical_start);
10223
10224 if (arg[0] != ',')
10225 error (_("Too few arguments."));
10226 else if (canonical_start.lsals.empty ())
10227 error (_("Could not find location of the beginning of the range."));
10228
10229 const linespec_sals &lsal_start = canonical_start.lsals[0];
10230
10231 if (canonical_start.lsals.size () > 1
10232 || lsal_start.sals.size () != 1)
10233 error (_("Cannot create a ranged breakpoint with multiple locations."));
10234
10235 const symtab_and_line &sal_start = lsal_start.sals[0];
10236 addr_string_start = savestring (arg_start, arg - arg_start);
10237 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10238
10239 arg++; /* Skip the comma. */
10240 arg = skip_spaces (arg);
10241
10242 /* Parse the end location. */
10243
10244 arg_start = arg;
10245
10246 /* We call decode_line_full directly here instead of using
10247 parse_breakpoint_sals because we need to specify the start location's
10248 symtab and line as the default symtab and line for the end of the
10249 range. This makes it possible to have ranges like "foo.c:27, +14",
10250 where +14 means 14 lines from the start location. */
10251 event_location_up end_location = string_to_event_location (&arg,
10252 current_language);
10253 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10254 sal_start.symtab, sal_start.line,
10255 &canonical_end, NULL, NULL);
10256
10257 if (canonical_end.lsals.empty ())
10258 error (_("Could not find location of the end of the range."));
10259
10260 const linespec_sals &lsal_end = canonical_end.lsals[0];
10261 if (canonical_end.lsals.size () > 1
10262 || lsal_end.sals.size () != 1)
10263 error (_("Cannot create a ranged breakpoint with multiple locations."));
10264
10265 const symtab_and_line &sal_end = lsal_end.sals[0];
10266
10267 end = find_breakpoint_range_end (sal_end);
10268 if (sal_start.pc > end)
10269 error (_("Invalid address range, end precedes start."));
10270
10271 length = end - sal_start.pc + 1;
10272 if (length < 0)
10273 /* Length overflowed. */
10274 error (_("Address range too large."));
10275 else if (length == 1)
10276 {
10277 /* This range is simple enough to be handled by
10278 the `hbreak' command. */
10279 hbreak_command (addr_string_start, 1);
10280
10281 do_cleanups (cleanup_bkpt);
10282
10283 return;
10284 }
10285
10286 /* Now set up the breakpoint. */
10287 b = set_raw_breakpoint (get_current_arch (), sal_start,
10288 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10289 set_breakpoint_count (breakpoint_count + 1);
10290 b->number = breakpoint_count;
10291 b->disposition = disp_donttouch;
10292 b->location = std::move (start_location);
10293 b->location_range_end = std::move (end_location);
10294 b->loc->length = length;
10295
10296 do_cleanups (cleanup_bkpt);
10297
10298 mention (b);
10299 observer_notify_breakpoint_created (b);
10300 update_global_location_list (UGLL_MAY_INSERT);
10301 }
10302
10303 /* Return non-zero if EXP is verified as constant. Returned zero
10304 means EXP is variable. Also the constant detection may fail for
10305 some constant expressions and in such case still falsely return
10306 zero. */
10307
10308 static int
10309 watchpoint_exp_is_const (const struct expression *exp)
10310 {
10311 int i = exp->nelts;
10312
10313 while (i > 0)
10314 {
10315 int oplenp, argsp;
10316
10317 /* We are only interested in the descriptor of each element. */
10318 operator_length (exp, i, &oplenp, &argsp);
10319 i -= oplenp;
10320
10321 switch (exp->elts[i].opcode)
10322 {
10323 case BINOP_ADD:
10324 case BINOP_SUB:
10325 case BINOP_MUL:
10326 case BINOP_DIV:
10327 case BINOP_REM:
10328 case BINOP_MOD:
10329 case BINOP_LSH:
10330 case BINOP_RSH:
10331 case BINOP_LOGICAL_AND:
10332 case BINOP_LOGICAL_OR:
10333 case BINOP_BITWISE_AND:
10334 case BINOP_BITWISE_IOR:
10335 case BINOP_BITWISE_XOR:
10336 case BINOP_EQUAL:
10337 case BINOP_NOTEQUAL:
10338 case BINOP_LESS:
10339 case BINOP_GTR:
10340 case BINOP_LEQ:
10341 case BINOP_GEQ:
10342 case BINOP_REPEAT:
10343 case BINOP_COMMA:
10344 case BINOP_EXP:
10345 case BINOP_MIN:
10346 case BINOP_MAX:
10347 case BINOP_INTDIV:
10348 case BINOP_CONCAT:
10349 case TERNOP_COND:
10350 case TERNOP_SLICE:
10351
10352 case OP_LONG:
10353 case OP_DOUBLE:
10354 case OP_DECFLOAT:
10355 case OP_LAST:
10356 case OP_COMPLEX:
10357 case OP_STRING:
10358 case OP_ARRAY:
10359 case OP_TYPE:
10360 case OP_TYPEOF:
10361 case OP_DECLTYPE:
10362 case OP_TYPEID:
10363 case OP_NAME:
10364 case OP_OBJC_NSSTRING:
10365
10366 case UNOP_NEG:
10367 case UNOP_LOGICAL_NOT:
10368 case UNOP_COMPLEMENT:
10369 case UNOP_ADDR:
10370 case UNOP_HIGH:
10371 case UNOP_CAST:
10372
10373 case UNOP_CAST_TYPE:
10374 case UNOP_REINTERPRET_CAST:
10375 case UNOP_DYNAMIC_CAST:
10376 /* Unary, binary and ternary operators: We have to check
10377 their operands. If they are constant, then so is the
10378 result of that operation. For instance, if A and B are
10379 determined to be constants, then so is "A + B".
10380
10381 UNOP_IND is one exception to the rule above, because the
10382 value of *ADDR is not necessarily a constant, even when
10383 ADDR is. */
10384 break;
10385
10386 case OP_VAR_VALUE:
10387 /* Check whether the associated symbol is a constant.
10388
10389 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10390 possible that a buggy compiler could mark a variable as
10391 constant even when it is not, and TYPE_CONST would return
10392 true in this case, while SYMBOL_CLASS wouldn't.
10393
10394 We also have to check for function symbols because they
10395 are always constant. */
10396 {
10397 struct symbol *s = exp->elts[i + 2].symbol;
10398
10399 if (SYMBOL_CLASS (s) != LOC_BLOCK
10400 && SYMBOL_CLASS (s) != LOC_CONST
10401 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10402 return 0;
10403 break;
10404 }
10405
10406 /* The default action is to return 0 because we are using
10407 the optimistic approach here: If we don't know something,
10408 then it is not a constant. */
10409 default:
10410 return 0;
10411 }
10412 }
10413
10414 return 1;
10415 }
10416
10417 /* Watchpoint destructor. */
10418
10419 watchpoint::~watchpoint ()
10420 {
10421 xfree (this->exp_string);
10422 xfree (this->exp_string_reparse);
10423 value_free (this->val);
10424 }
10425
10426 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10427
10428 static void
10429 re_set_watchpoint (struct breakpoint *b)
10430 {
10431 struct watchpoint *w = (struct watchpoint *) b;
10432
10433 /* Watchpoint can be either on expression using entirely global
10434 variables, or it can be on local variables.
10435
10436 Watchpoints of the first kind are never auto-deleted, and even
10437 persist across program restarts. Since they can use variables
10438 from shared libraries, we need to reparse expression as libraries
10439 are loaded and unloaded.
10440
10441 Watchpoints on local variables can also change meaning as result
10442 of solib event. For example, if a watchpoint uses both a local
10443 and a global variables in expression, it's a local watchpoint,
10444 but unloading of a shared library will make the expression
10445 invalid. This is not a very common use case, but we still
10446 re-evaluate expression, to avoid surprises to the user.
10447
10448 Note that for local watchpoints, we re-evaluate it only if
10449 watchpoints frame id is still valid. If it's not, it means the
10450 watchpoint is out of scope and will be deleted soon. In fact,
10451 I'm not sure we'll ever be called in this case.
10452
10453 If a local watchpoint's frame id is still valid, then
10454 w->exp_valid_block is likewise valid, and we can safely use it.
10455
10456 Don't do anything about disabled watchpoints, since they will be
10457 reevaluated again when enabled. */
10458 update_watchpoint (w, 1 /* reparse */);
10459 }
10460
10461 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10462
10463 static int
10464 insert_watchpoint (struct bp_location *bl)
10465 {
10466 struct watchpoint *w = (struct watchpoint *) bl->owner;
10467 int length = w->exact ? 1 : bl->length;
10468
10469 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10470 w->cond_exp.get ());
10471 }
10472
10473 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10474
10475 static int
10476 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10477 {
10478 struct watchpoint *w = (struct watchpoint *) bl->owner;
10479 int length = w->exact ? 1 : bl->length;
10480
10481 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10482 w->cond_exp.get ());
10483 }
10484
10485 static int
10486 breakpoint_hit_watchpoint (const struct bp_location *bl,
10487 struct address_space *aspace, CORE_ADDR bp_addr,
10488 const struct target_waitstatus *ws)
10489 {
10490 struct breakpoint *b = bl->owner;
10491 struct watchpoint *w = (struct watchpoint *) b;
10492
10493 /* Continuable hardware watchpoints are treated as non-existent if the
10494 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10495 some data address). Otherwise gdb won't stop on a break instruction
10496 in the code (not from a breakpoint) when a hardware watchpoint has
10497 been defined. Also skip watchpoints which we know did not trigger
10498 (did not match the data address). */
10499 if (is_hardware_watchpoint (b)
10500 && w->watchpoint_triggered == watch_triggered_no)
10501 return 0;
10502
10503 return 1;
10504 }
10505
10506 static void
10507 check_status_watchpoint (bpstat bs)
10508 {
10509 gdb_assert (is_watchpoint (bs->breakpoint_at));
10510
10511 bpstat_check_watchpoint (bs);
10512 }
10513
10514 /* Implement the "resources_needed" breakpoint_ops method for
10515 hardware watchpoints. */
10516
10517 static int
10518 resources_needed_watchpoint (const struct bp_location *bl)
10519 {
10520 struct watchpoint *w = (struct watchpoint *) bl->owner;
10521 int length = w->exact? 1 : bl->length;
10522
10523 return target_region_ok_for_hw_watchpoint (bl->address, length);
10524 }
10525
10526 /* Implement the "works_in_software_mode" breakpoint_ops method for
10527 hardware watchpoints. */
10528
10529 static int
10530 works_in_software_mode_watchpoint (const struct breakpoint *b)
10531 {
10532 /* Read and access watchpoints only work with hardware support. */
10533 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10534 }
10535
10536 static enum print_stop_action
10537 print_it_watchpoint (bpstat bs)
10538 {
10539 struct cleanup *old_chain;
10540 struct breakpoint *b;
10541 enum print_stop_action result;
10542 struct watchpoint *w;
10543 struct ui_out *uiout = current_uiout;
10544
10545 gdb_assert (bs->bp_location_at != NULL);
10546
10547 b = bs->breakpoint_at;
10548 w = (struct watchpoint *) b;
10549
10550 old_chain = make_cleanup (null_cleanup, NULL);
10551
10552 annotate_watchpoint (b->number);
10553 maybe_print_thread_hit_breakpoint (uiout);
10554
10555 string_file stb;
10556
10557 switch (b->type)
10558 {
10559 case bp_watchpoint:
10560 case bp_hardware_watchpoint:
10561 if (uiout->is_mi_like_p ())
10562 uiout->field_string
10563 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10564 mention (b);
10565 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10566 uiout->text ("\nOld value = ");
10567 watchpoint_value_print (bs->old_val, &stb);
10568 uiout->field_stream ("old", stb);
10569 uiout->text ("\nNew value = ");
10570 watchpoint_value_print (w->val, &stb);
10571 uiout->field_stream ("new", stb);
10572 uiout->text ("\n");
10573 /* More than one watchpoint may have been triggered. */
10574 result = PRINT_UNKNOWN;
10575 break;
10576
10577 case bp_read_watchpoint:
10578 if (uiout->is_mi_like_p ())
10579 uiout->field_string
10580 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10581 mention (b);
10582 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10583 uiout->text ("\nValue = ");
10584 watchpoint_value_print (w->val, &stb);
10585 uiout->field_stream ("value", stb);
10586 uiout->text ("\n");
10587 result = PRINT_UNKNOWN;
10588 break;
10589
10590 case bp_access_watchpoint:
10591 if (bs->old_val != NULL)
10592 {
10593 if (uiout->is_mi_like_p ())
10594 uiout->field_string
10595 ("reason",
10596 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10597 mention (b);
10598 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10599 uiout->text ("\nOld value = ");
10600 watchpoint_value_print (bs->old_val, &stb);
10601 uiout->field_stream ("old", stb);
10602 uiout->text ("\nNew value = ");
10603 }
10604 else
10605 {
10606 mention (b);
10607 if (uiout->is_mi_like_p ())
10608 uiout->field_string
10609 ("reason",
10610 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10611 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10612 uiout->text ("\nValue = ");
10613 }
10614 watchpoint_value_print (w->val, &stb);
10615 uiout->field_stream ("new", stb);
10616 uiout->text ("\n");
10617 result = PRINT_UNKNOWN;
10618 break;
10619 default:
10620 result = PRINT_UNKNOWN;
10621 }
10622
10623 do_cleanups (old_chain);
10624 return result;
10625 }
10626
10627 /* Implement the "print_mention" breakpoint_ops method for hardware
10628 watchpoints. */
10629
10630 static void
10631 print_mention_watchpoint (struct breakpoint *b)
10632 {
10633 struct watchpoint *w = (struct watchpoint *) b;
10634 struct ui_out *uiout = current_uiout;
10635 const char *tuple_name;
10636
10637 switch (b->type)
10638 {
10639 case bp_watchpoint:
10640 uiout->text ("Watchpoint ");
10641 tuple_name = "wpt";
10642 break;
10643 case bp_hardware_watchpoint:
10644 uiout->text ("Hardware watchpoint ");
10645 tuple_name = "wpt";
10646 break;
10647 case bp_read_watchpoint:
10648 uiout->text ("Hardware read watchpoint ");
10649 tuple_name = "hw-rwpt";
10650 break;
10651 case bp_access_watchpoint:
10652 uiout->text ("Hardware access (read/write) watchpoint ");
10653 tuple_name = "hw-awpt";
10654 break;
10655 default:
10656 internal_error (__FILE__, __LINE__,
10657 _("Invalid hardware watchpoint type."));
10658 }
10659
10660 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10661 uiout->field_int ("number", b->number);
10662 uiout->text (": ");
10663 uiout->field_string ("exp", w->exp_string);
10664 }
10665
10666 /* Implement the "print_recreate" breakpoint_ops method for
10667 watchpoints. */
10668
10669 static void
10670 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10671 {
10672 struct watchpoint *w = (struct watchpoint *) b;
10673
10674 switch (b->type)
10675 {
10676 case bp_watchpoint:
10677 case bp_hardware_watchpoint:
10678 fprintf_unfiltered (fp, "watch");
10679 break;
10680 case bp_read_watchpoint:
10681 fprintf_unfiltered (fp, "rwatch");
10682 break;
10683 case bp_access_watchpoint:
10684 fprintf_unfiltered (fp, "awatch");
10685 break;
10686 default:
10687 internal_error (__FILE__, __LINE__,
10688 _("Invalid watchpoint type."));
10689 }
10690
10691 fprintf_unfiltered (fp, " %s", w->exp_string);
10692 print_recreate_thread (b, fp);
10693 }
10694
10695 /* Implement the "explains_signal" breakpoint_ops method for
10696 watchpoints. */
10697
10698 static int
10699 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10700 {
10701 /* A software watchpoint cannot cause a signal other than
10702 GDB_SIGNAL_TRAP. */
10703 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10704 return 0;
10705
10706 return 1;
10707 }
10708
10709 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10710
10711 static struct breakpoint_ops watchpoint_breakpoint_ops;
10712
10713 /* Implement the "insert" breakpoint_ops method for
10714 masked hardware watchpoints. */
10715
10716 static int
10717 insert_masked_watchpoint (struct bp_location *bl)
10718 {
10719 struct watchpoint *w = (struct watchpoint *) bl->owner;
10720
10721 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10722 bl->watchpoint_type);
10723 }
10724
10725 /* Implement the "remove" breakpoint_ops method for
10726 masked hardware watchpoints. */
10727
10728 static int
10729 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10730 {
10731 struct watchpoint *w = (struct watchpoint *) bl->owner;
10732
10733 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10734 bl->watchpoint_type);
10735 }
10736
10737 /* Implement the "resources_needed" breakpoint_ops method for
10738 masked hardware watchpoints. */
10739
10740 static int
10741 resources_needed_masked_watchpoint (const struct bp_location *bl)
10742 {
10743 struct watchpoint *w = (struct watchpoint *) bl->owner;
10744
10745 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10746 }
10747
10748 /* Implement the "works_in_software_mode" breakpoint_ops method for
10749 masked hardware watchpoints. */
10750
10751 static int
10752 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10753 {
10754 return 0;
10755 }
10756
10757 /* Implement the "print_it" breakpoint_ops method for
10758 masked hardware watchpoints. */
10759
10760 static enum print_stop_action
10761 print_it_masked_watchpoint (bpstat bs)
10762 {
10763 struct breakpoint *b = bs->breakpoint_at;
10764 struct ui_out *uiout = current_uiout;
10765
10766 /* Masked watchpoints have only one location. */
10767 gdb_assert (b->loc && b->loc->next == NULL);
10768
10769 annotate_watchpoint (b->number);
10770 maybe_print_thread_hit_breakpoint (uiout);
10771
10772 switch (b->type)
10773 {
10774 case bp_hardware_watchpoint:
10775 if (uiout->is_mi_like_p ())
10776 uiout->field_string
10777 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10778 break;
10779
10780 case bp_read_watchpoint:
10781 if (uiout->is_mi_like_p ())
10782 uiout->field_string
10783 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10784 break;
10785
10786 case bp_access_watchpoint:
10787 if (uiout->is_mi_like_p ())
10788 uiout->field_string
10789 ("reason",
10790 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10791 break;
10792 default:
10793 internal_error (__FILE__, __LINE__,
10794 _("Invalid hardware watchpoint type."));
10795 }
10796
10797 mention (b);
10798 uiout->text (_("\n\
10799 Check the underlying instruction at PC for the memory\n\
10800 address and value which triggered this watchpoint.\n"));
10801 uiout->text ("\n");
10802
10803 /* More than one watchpoint may have been triggered. */
10804 return PRINT_UNKNOWN;
10805 }
10806
10807 /* Implement the "print_one_detail" breakpoint_ops method for
10808 masked hardware watchpoints. */
10809
10810 static void
10811 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10812 struct ui_out *uiout)
10813 {
10814 struct watchpoint *w = (struct watchpoint *) b;
10815
10816 /* Masked watchpoints have only one location. */
10817 gdb_assert (b->loc && b->loc->next == NULL);
10818
10819 uiout->text ("\tmask ");
10820 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10821 uiout->text ("\n");
10822 }
10823
10824 /* Implement the "print_mention" breakpoint_ops method for
10825 masked hardware watchpoints. */
10826
10827 static void
10828 print_mention_masked_watchpoint (struct breakpoint *b)
10829 {
10830 struct watchpoint *w = (struct watchpoint *) b;
10831 struct ui_out *uiout = current_uiout;
10832 const char *tuple_name;
10833
10834 switch (b->type)
10835 {
10836 case bp_hardware_watchpoint:
10837 uiout->text ("Masked hardware watchpoint ");
10838 tuple_name = "wpt";
10839 break;
10840 case bp_read_watchpoint:
10841 uiout->text ("Masked hardware read watchpoint ");
10842 tuple_name = "hw-rwpt";
10843 break;
10844 case bp_access_watchpoint:
10845 uiout->text ("Masked hardware access (read/write) watchpoint ");
10846 tuple_name = "hw-awpt";
10847 break;
10848 default:
10849 internal_error (__FILE__, __LINE__,
10850 _("Invalid hardware watchpoint type."));
10851 }
10852
10853 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10854 uiout->field_int ("number", b->number);
10855 uiout->text (": ");
10856 uiout->field_string ("exp", w->exp_string);
10857 }
10858
10859 /* Implement the "print_recreate" breakpoint_ops method for
10860 masked hardware watchpoints. */
10861
10862 static void
10863 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10864 {
10865 struct watchpoint *w = (struct watchpoint *) b;
10866 char tmp[40];
10867
10868 switch (b->type)
10869 {
10870 case bp_hardware_watchpoint:
10871 fprintf_unfiltered (fp, "watch");
10872 break;
10873 case bp_read_watchpoint:
10874 fprintf_unfiltered (fp, "rwatch");
10875 break;
10876 case bp_access_watchpoint:
10877 fprintf_unfiltered (fp, "awatch");
10878 break;
10879 default:
10880 internal_error (__FILE__, __LINE__,
10881 _("Invalid hardware watchpoint type."));
10882 }
10883
10884 sprintf_vma (tmp, w->hw_wp_mask);
10885 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10886 print_recreate_thread (b, fp);
10887 }
10888
10889 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10890
10891 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10892
10893 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10894
10895 static int
10896 is_masked_watchpoint (const struct breakpoint *b)
10897 {
10898 return b->ops == &masked_watchpoint_breakpoint_ops;
10899 }
10900
10901 /* accessflag: hw_write: watch write,
10902 hw_read: watch read,
10903 hw_access: watch access (read or write) */
10904 static void
10905 watch_command_1 (const char *arg, int accessflag, int from_tty,
10906 int just_location, int internal)
10907 {
10908 struct breakpoint *scope_breakpoint = NULL;
10909 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10910 struct value *val, *mark, *result;
10911 int saved_bitpos = 0, saved_bitsize = 0;
10912 const char *exp_start = NULL;
10913 const char *exp_end = NULL;
10914 const char *tok, *end_tok;
10915 int toklen = -1;
10916 const char *cond_start = NULL;
10917 const char *cond_end = NULL;
10918 enum bptype bp_type;
10919 int thread = -1;
10920 int pc = 0;
10921 /* Flag to indicate whether we are going to use masks for
10922 the hardware watchpoint. */
10923 int use_mask = 0;
10924 CORE_ADDR mask = 0;
10925 char *expression;
10926 struct cleanup *back_to;
10927
10928 /* Make sure that we actually have parameters to parse. */
10929 if (arg != NULL && arg[0] != '\0')
10930 {
10931 const char *value_start;
10932
10933 exp_end = arg + strlen (arg);
10934
10935 /* Look for "parameter value" pairs at the end
10936 of the arguments string. */
10937 for (tok = exp_end - 1; tok > arg; tok--)
10938 {
10939 /* Skip whitespace at the end of the argument list. */
10940 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10941 tok--;
10942
10943 /* Find the beginning of the last token.
10944 This is the value of the parameter. */
10945 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10946 tok--;
10947 value_start = tok + 1;
10948
10949 /* Skip whitespace. */
10950 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10951 tok--;
10952
10953 end_tok = tok;
10954
10955 /* Find the beginning of the second to last token.
10956 This is the parameter itself. */
10957 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10958 tok--;
10959 tok++;
10960 toklen = end_tok - tok + 1;
10961
10962 if (toklen == 6 && startswith (tok, "thread"))
10963 {
10964 struct thread_info *thr;
10965 /* At this point we've found a "thread" token, which means
10966 the user is trying to set a watchpoint that triggers
10967 only in a specific thread. */
10968 const char *endp;
10969
10970 if (thread != -1)
10971 error(_("You can specify only one thread."));
10972
10973 /* Extract the thread ID from the next token. */
10974 thr = parse_thread_id (value_start, &endp);
10975
10976 /* Check if the user provided a valid thread ID. */
10977 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10978 invalid_thread_id_error (value_start);
10979
10980 thread = thr->global_num;
10981 }
10982 else if (toklen == 4 && startswith (tok, "mask"))
10983 {
10984 /* We've found a "mask" token, which means the user wants to
10985 create a hardware watchpoint that is going to have the mask
10986 facility. */
10987 struct value *mask_value, *mark;
10988
10989 if (use_mask)
10990 error(_("You can specify only one mask."));
10991
10992 use_mask = just_location = 1;
10993
10994 mark = value_mark ();
10995 mask_value = parse_to_comma_and_eval (&value_start);
10996 mask = value_as_address (mask_value);
10997 value_free_to_mark (mark);
10998 }
10999 else
11000 /* We didn't recognize what we found. We should stop here. */
11001 break;
11002
11003 /* Truncate the string and get rid of the "parameter value" pair before
11004 the arguments string is parsed by the parse_exp_1 function. */
11005 exp_end = tok;
11006 }
11007 }
11008 else
11009 exp_end = arg;
11010
11011 /* Parse the rest of the arguments. From here on out, everything
11012 is in terms of a newly allocated string instead of the original
11013 ARG. */
11014 innermost_block = NULL;
11015 expression = savestring (arg, exp_end - arg);
11016 back_to = make_cleanup (xfree, expression);
11017 exp_start = arg = expression;
11018 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11019 exp_end = arg;
11020 /* Remove trailing whitespace from the expression before saving it.
11021 This makes the eventual display of the expression string a bit
11022 prettier. */
11023 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11024 --exp_end;
11025
11026 /* Checking if the expression is not constant. */
11027 if (watchpoint_exp_is_const (exp.get ()))
11028 {
11029 int len;
11030
11031 len = exp_end - exp_start;
11032 while (len > 0 && isspace (exp_start[len - 1]))
11033 len--;
11034 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11035 }
11036
11037 exp_valid_block = innermost_block;
11038 mark = value_mark ();
11039 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11040
11041 if (val != NULL && just_location)
11042 {
11043 saved_bitpos = value_bitpos (val);
11044 saved_bitsize = value_bitsize (val);
11045 }
11046
11047 if (just_location)
11048 {
11049 int ret;
11050
11051 exp_valid_block = NULL;
11052 val = value_addr (result);
11053 release_value (val);
11054 value_free_to_mark (mark);
11055
11056 if (use_mask)
11057 {
11058 ret = target_masked_watch_num_registers (value_as_address (val),
11059 mask);
11060 if (ret == -1)
11061 error (_("This target does not support masked watchpoints."));
11062 else if (ret == -2)
11063 error (_("Invalid mask or memory region."));
11064 }
11065 }
11066 else if (val != NULL)
11067 release_value (val);
11068
11069 tok = skip_spaces_const (arg);
11070 end_tok = skip_to_space_const (tok);
11071
11072 toklen = end_tok - tok;
11073 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11074 {
11075 innermost_block = NULL;
11076 tok = cond_start = end_tok + 1;
11077 parse_exp_1 (&tok, 0, 0, 0);
11078
11079 /* The watchpoint expression may not be local, but the condition
11080 may still be. E.g.: `watch global if local > 0'. */
11081 cond_exp_valid_block = innermost_block;
11082
11083 cond_end = tok;
11084 }
11085 if (*tok)
11086 error (_("Junk at end of command."));
11087
11088 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11089
11090 /* Save this because create_internal_breakpoint below invalidates
11091 'wp_frame'. */
11092 frame_id watchpoint_frame = get_frame_id (wp_frame);
11093
11094 /* If the expression is "local", then set up a "watchpoint scope"
11095 breakpoint at the point where we've left the scope of the watchpoint
11096 expression. Create the scope breakpoint before the watchpoint, so
11097 that we will encounter it first in bpstat_stop_status. */
11098 if (exp_valid_block != NULL && wp_frame != NULL)
11099 {
11100 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11101
11102 if (frame_id_p (caller_frame_id))
11103 {
11104 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11105 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11106
11107 scope_breakpoint
11108 = create_internal_breakpoint (caller_arch, caller_pc,
11109 bp_watchpoint_scope,
11110 &momentary_breakpoint_ops);
11111
11112 /* create_internal_breakpoint could invalidate WP_FRAME. */
11113 wp_frame = NULL;
11114
11115 scope_breakpoint->enable_state = bp_enabled;
11116
11117 /* Automatically delete the breakpoint when it hits. */
11118 scope_breakpoint->disposition = disp_del;
11119
11120 /* Only break in the proper frame (help with recursion). */
11121 scope_breakpoint->frame_id = caller_frame_id;
11122
11123 /* Set the address at which we will stop. */
11124 scope_breakpoint->loc->gdbarch = caller_arch;
11125 scope_breakpoint->loc->requested_address = caller_pc;
11126 scope_breakpoint->loc->address
11127 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11128 scope_breakpoint->loc->requested_address,
11129 scope_breakpoint->type);
11130 }
11131 }
11132
11133 /* Now set up the breakpoint. We create all watchpoints as hardware
11134 watchpoints here even if hardware watchpoints are turned off, a call
11135 to update_watchpoint later in this function will cause the type to
11136 drop back to bp_watchpoint (software watchpoint) if required. */
11137
11138 if (accessflag == hw_read)
11139 bp_type = bp_read_watchpoint;
11140 else if (accessflag == hw_access)
11141 bp_type = bp_access_watchpoint;
11142 else
11143 bp_type = bp_hardware_watchpoint;
11144
11145 std::unique_ptr<watchpoint> w (new watchpoint ());
11146
11147 if (use_mask)
11148 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11149 &masked_watchpoint_breakpoint_ops);
11150 else
11151 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11152 &watchpoint_breakpoint_ops);
11153 w->thread = thread;
11154 w->disposition = disp_donttouch;
11155 w->pspace = current_program_space;
11156 w->exp = std::move (exp);
11157 w->exp_valid_block = exp_valid_block;
11158 w->cond_exp_valid_block = cond_exp_valid_block;
11159 if (just_location)
11160 {
11161 struct type *t = value_type (val);
11162 CORE_ADDR addr = value_as_address (val);
11163
11164 w->exp_string_reparse
11165 = current_language->la_watch_location_expression (t, addr).release ();
11166
11167 w->exp_string = xstrprintf ("-location %.*s",
11168 (int) (exp_end - exp_start), exp_start);
11169 }
11170 else
11171 w->exp_string = savestring (exp_start, exp_end - exp_start);
11172
11173 if (use_mask)
11174 {
11175 w->hw_wp_mask = mask;
11176 }
11177 else
11178 {
11179 w->val = val;
11180 w->val_bitpos = saved_bitpos;
11181 w->val_bitsize = saved_bitsize;
11182 w->val_valid = 1;
11183 }
11184
11185 if (cond_start)
11186 w->cond_string = savestring (cond_start, cond_end - cond_start);
11187 else
11188 w->cond_string = 0;
11189
11190 if (frame_id_p (watchpoint_frame))
11191 {
11192 w->watchpoint_frame = watchpoint_frame;
11193 w->watchpoint_thread = inferior_ptid;
11194 }
11195 else
11196 {
11197 w->watchpoint_frame = null_frame_id;
11198 w->watchpoint_thread = null_ptid;
11199 }
11200
11201 if (scope_breakpoint != NULL)
11202 {
11203 /* The scope breakpoint is related to the watchpoint. We will
11204 need to act on them together. */
11205 w->related_breakpoint = scope_breakpoint;
11206 scope_breakpoint->related_breakpoint = w.get ();
11207 }
11208
11209 if (!just_location)
11210 value_free_to_mark (mark);
11211
11212 /* Finally update the new watchpoint. This creates the locations
11213 that should be inserted. */
11214 update_watchpoint (w.get (), 1);
11215
11216 install_breakpoint (internal, std::move (w), 1);
11217 do_cleanups (back_to);
11218 }
11219
11220 /* Return count of debug registers needed to watch the given expression.
11221 If the watchpoint cannot be handled in hardware return zero. */
11222
11223 static int
11224 can_use_hardware_watchpoint (struct value *v)
11225 {
11226 int found_memory_cnt = 0;
11227 struct value *head = v;
11228
11229 /* Did the user specifically forbid us to use hardware watchpoints? */
11230 if (!can_use_hw_watchpoints)
11231 return 0;
11232
11233 /* Make sure that the value of the expression depends only upon
11234 memory contents, and values computed from them within GDB. If we
11235 find any register references or function calls, we can't use a
11236 hardware watchpoint.
11237
11238 The idea here is that evaluating an expression generates a series
11239 of values, one holding the value of every subexpression. (The
11240 expression a*b+c has five subexpressions: a, b, a*b, c, and
11241 a*b+c.) GDB's values hold almost enough information to establish
11242 the criteria given above --- they identify memory lvalues,
11243 register lvalues, computed values, etcetera. So we can evaluate
11244 the expression, and then scan the chain of values that leaves
11245 behind to decide whether we can detect any possible change to the
11246 expression's final value using only hardware watchpoints.
11247
11248 However, I don't think that the values returned by inferior
11249 function calls are special in any way. So this function may not
11250 notice that an expression involving an inferior function call
11251 can't be watched with hardware watchpoints. FIXME. */
11252 for (; v; v = value_next (v))
11253 {
11254 if (VALUE_LVAL (v) == lval_memory)
11255 {
11256 if (v != head && value_lazy (v))
11257 /* A lazy memory lvalue in the chain is one that GDB never
11258 needed to fetch; we either just used its address (e.g.,
11259 `a' in `a.b') or we never needed it at all (e.g., `a'
11260 in `a,b'). This doesn't apply to HEAD; if that is
11261 lazy then it was not readable, but watch it anyway. */
11262 ;
11263 else
11264 {
11265 /* Ahh, memory we actually used! Check if we can cover
11266 it with hardware watchpoints. */
11267 struct type *vtype = check_typedef (value_type (v));
11268
11269 /* We only watch structs and arrays if user asked for it
11270 explicitly, never if they just happen to appear in a
11271 middle of some value chain. */
11272 if (v == head
11273 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11274 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11275 {
11276 CORE_ADDR vaddr = value_address (v);
11277 int len;
11278 int num_regs;
11279
11280 len = (target_exact_watchpoints
11281 && is_scalar_type_recursive (vtype))?
11282 1 : TYPE_LENGTH (value_type (v));
11283
11284 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11285 if (!num_regs)
11286 return 0;
11287 else
11288 found_memory_cnt += num_regs;
11289 }
11290 }
11291 }
11292 else if (VALUE_LVAL (v) != not_lval
11293 && deprecated_value_modifiable (v) == 0)
11294 return 0; /* These are values from the history (e.g., $1). */
11295 else if (VALUE_LVAL (v) == lval_register)
11296 return 0; /* Cannot watch a register with a HW watchpoint. */
11297 }
11298
11299 /* The expression itself looks suitable for using a hardware
11300 watchpoint, but give the target machine a chance to reject it. */
11301 return found_memory_cnt;
11302 }
11303
11304 void
11305 watch_command_wrapper (char *arg, int from_tty, int internal)
11306 {
11307 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11308 }
11309
11310 /* A helper function that looks for the "-location" argument and then
11311 calls watch_command_1. */
11312
11313 static void
11314 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11315 {
11316 int just_location = 0;
11317
11318 if (arg
11319 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11320 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11321 {
11322 arg = skip_spaces (arg);
11323 just_location = 1;
11324 }
11325
11326 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11327 }
11328
11329 static void
11330 watch_command (char *arg, int from_tty)
11331 {
11332 watch_maybe_just_location (arg, hw_write, from_tty);
11333 }
11334
11335 void
11336 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11337 {
11338 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11339 }
11340
11341 static void
11342 rwatch_command (char *arg, int from_tty)
11343 {
11344 watch_maybe_just_location (arg, hw_read, from_tty);
11345 }
11346
11347 void
11348 awatch_command_wrapper (char *arg, int from_tty, int internal)
11349 {
11350 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11351 }
11352
11353 static void
11354 awatch_command (char *arg, int from_tty)
11355 {
11356 watch_maybe_just_location (arg, hw_access, from_tty);
11357 }
11358 \f
11359
11360 /* Data for the FSM that manages the until(location)/advance commands
11361 in infcmd.c. Here because it uses the mechanisms of
11362 breakpoints. */
11363
11364 struct until_break_fsm
11365 {
11366 /* The base class. */
11367 struct thread_fsm thread_fsm;
11368
11369 /* The thread that as current when the command was executed. */
11370 int thread;
11371
11372 /* The breakpoint set at the destination location. */
11373 struct breakpoint *location_breakpoint;
11374
11375 /* Breakpoint set at the return address in the caller frame. May be
11376 NULL. */
11377 struct breakpoint *caller_breakpoint;
11378 };
11379
11380 static void until_break_fsm_clean_up (struct thread_fsm *self,
11381 struct thread_info *thread);
11382 static int until_break_fsm_should_stop (struct thread_fsm *self,
11383 struct thread_info *thread);
11384 static enum async_reply_reason
11385 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11386
11387 /* until_break_fsm's vtable. */
11388
11389 static struct thread_fsm_ops until_break_fsm_ops =
11390 {
11391 NULL, /* dtor */
11392 until_break_fsm_clean_up,
11393 until_break_fsm_should_stop,
11394 NULL, /* return_value */
11395 until_break_fsm_async_reply_reason,
11396 };
11397
11398 /* Allocate a new until_break_command_fsm. */
11399
11400 static struct until_break_fsm *
11401 new_until_break_fsm (struct interp *cmd_interp, int thread,
11402 struct breakpoint *location_breakpoint,
11403 struct breakpoint *caller_breakpoint)
11404 {
11405 struct until_break_fsm *sm;
11406
11407 sm = XCNEW (struct until_break_fsm);
11408 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11409
11410 sm->thread = thread;
11411 sm->location_breakpoint = location_breakpoint;
11412 sm->caller_breakpoint = caller_breakpoint;
11413
11414 return sm;
11415 }
11416
11417 /* Implementation of the 'should_stop' FSM method for the
11418 until(location)/advance commands. */
11419
11420 static int
11421 until_break_fsm_should_stop (struct thread_fsm *self,
11422 struct thread_info *tp)
11423 {
11424 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11425
11426 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11427 sm->location_breakpoint) != NULL
11428 || (sm->caller_breakpoint != NULL
11429 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11430 sm->caller_breakpoint) != NULL))
11431 thread_fsm_set_finished (self);
11432
11433 return 1;
11434 }
11435
11436 /* Implementation of the 'clean_up' FSM method for the
11437 until(location)/advance commands. */
11438
11439 static void
11440 until_break_fsm_clean_up (struct thread_fsm *self,
11441 struct thread_info *thread)
11442 {
11443 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11444
11445 /* Clean up our temporary breakpoints. */
11446 if (sm->location_breakpoint != NULL)
11447 {
11448 delete_breakpoint (sm->location_breakpoint);
11449 sm->location_breakpoint = NULL;
11450 }
11451 if (sm->caller_breakpoint != NULL)
11452 {
11453 delete_breakpoint (sm->caller_breakpoint);
11454 sm->caller_breakpoint = NULL;
11455 }
11456 delete_longjmp_breakpoint (sm->thread);
11457 }
11458
11459 /* Implementation of the 'async_reply_reason' FSM method for the
11460 until(location)/advance commands. */
11461
11462 static enum async_reply_reason
11463 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11464 {
11465 return EXEC_ASYNC_LOCATION_REACHED;
11466 }
11467
11468 void
11469 until_break_command (char *arg, int from_tty, int anywhere)
11470 {
11471 struct frame_info *frame;
11472 struct gdbarch *frame_gdbarch;
11473 struct frame_id stack_frame_id;
11474 struct frame_id caller_frame_id;
11475 struct breakpoint *location_breakpoint;
11476 struct breakpoint *caller_breakpoint = NULL;
11477 struct cleanup *old_chain;
11478 int thread;
11479 struct thread_info *tp;
11480 struct until_break_fsm *sm;
11481
11482 clear_proceed_status (0);
11483
11484 /* Set a breakpoint where the user wants it and at return from
11485 this function. */
11486
11487 event_location_up location = string_to_event_location (&arg, current_language);
11488
11489 std::vector<symtab_and_line> sals
11490 = (last_displayed_sal_is_valid ()
11491 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11492 get_last_displayed_symtab (),
11493 get_last_displayed_line ())
11494 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11495 NULL, (struct symtab *) NULL, 0));
11496
11497 if (sals.size () != 1)
11498 error (_("Couldn't get information on specified line."));
11499
11500 symtab_and_line &sal = sals[0];
11501
11502 if (*arg)
11503 error (_("Junk at end of arguments."));
11504
11505 resolve_sal_pc (&sal);
11506
11507 tp = inferior_thread ();
11508 thread = tp->global_num;
11509
11510 old_chain = make_cleanup (null_cleanup, NULL);
11511
11512 /* Note linespec handling above invalidates the frame chain.
11513 Installing a breakpoint also invalidates the frame chain (as it
11514 may need to switch threads), so do any frame handling before
11515 that. */
11516
11517 frame = get_selected_frame (NULL);
11518 frame_gdbarch = get_frame_arch (frame);
11519 stack_frame_id = get_stack_frame_id (frame);
11520 caller_frame_id = frame_unwind_caller_id (frame);
11521
11522 /* Keep within the current frame, or in frames called by the current
11523 one. */
11524
11525 if (frame_id_p (caller_frame_id))
11526 {
11527 struct symtab_and_line sal2;
11528 struct gdbarch *caller_gdbarch;
11529
11530 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11531 sal2.pc = frame_unwind_caller_pc (frame);
11532 caller_gdbarch = frame_unwind_caller_arch (frame);
11533 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11534 sal2,
11535 caller_frame_id,
11536 bp_until);
11537 make_cleanup_delete_breakpoint (caller_breakpoint);
11538
11539 set_longjmp_breakpoint (tp, caller_frame_id);
11540 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11541 }
11542
11543 /* set_momentary_breakpoint could invalidate FRAME. */
11544 frame = NULL;
11545
11546 if (anywhere)
11547 /* If the user told us to continue until a specified location,
11548 we don't specify a frame at which we need to stop. */
11549 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11550 null_frame_id, bp_until);
11551 else
11552 /* Otherwise, specify the selected frame, because we want to stop
11553 only at the very same frame. */
11554 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11555 stack_frame_id, bp_until);
11556 make_cleanup_delete_breakpoint (location_breakpoint);
11557
11558 sm = new_until_break_fsm (command_interp (), tp->global_num,
11559 location_breakpoint, caller_breakpoint);
11560 tp->thread_fsm = &sm->thread_fsm;
11561
11562 discard_cleanups (old_chain);
11563
11564 proceed (-1, GDB_SIGNAL_DEFAULT);
11565 }
11566
11567 /* This function attempts to parse an optional "if <cond>" clause
11568 from the arg string. If one is not found, it returns NULL.
11569
11570 Else, it returns a pointer to the condition string. (It does not
11571 attempt to evaluate the string against a particular block.) And,
11572 it updates arg to point to the first character following the parsed
11573 if clause in the arg string. */
11574
11575 const char *
11576 ep_parse_optional_if_clause (const char **arg)
11577 {
11578 const char *cond_string;
11579
11580 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11581 return NULL;
11582
11583 /* Skip the "if" keyword. */
11584 (*arg) += 2;
11585
11586 /* Skip any extra leading whitespace, and record the start of the
11587 condition string. */
11588 *arg = skip_spaces_const (*arg);
11589 cond_string = *arg;
11590
11591 /* Assume that the condition occupies the remainder of the arg
11592 string. */
11593 (*arg) += strlen (cond_string);
11594
11595 return cond_string;
11596 }
11597
11598 /* Commands to deal with catching events, such as signals, exceptions,
11599 process start/exit, etc. */
11600
11601 typedef enum
11602 {
11603 catch_fork_temporary, catch_vfork_temporary,
11604 catch_fork_permanent, catch_vfork_permanent
11605 }
11606 catch_fork_kind;
11607
11608 static void
11609 catch_fork_command_1 (char *arg_entry, int from_tty,
11610 struct cmd_list_element *command)
11611 {
11612 const char *arg = arg_entry;
11613 struct gdbarch *gdbarch = get_current_arch ();
11614 const char *cond_string = NULL;
11615 catch_fork_kind fork_kind;
11616 int tempflag;
11617
11618 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11619 tempflag = (fork_kind == catch_fork_temporary
11620 || fork_kind == catch_vfork_temporary);
11621
11622 if (!arg)
11623 arg = "";
11624 arg = skip_spaces_const (arg);
11625
11626 /* The allowed syntax is:
11627 catch [v]fork
11628 catch [v]fork if <cond>
11629
11630 First, check if there's an if clause. */
11631 cond_string = ep_parse_optional_if_clause (&arg);
11632
11633 if ((*arg != '\0') && !isspace (*arg))
11634 error (_("Junk at end of arguments."));
11635
11636 /* If this target supports it, create a fork or vfork catchpoint
11637 and enable reporting of such events. */
11638 switch (fork_kind)
11639 {
11640 case catch_fork_temporary:
11641 case catch_fork_permanent:
11642 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11643 &catch_fork_breakpoint_ops);
11644 break;
11645 case catch_vfork_temporary:
11646 case catch_vfork_permanent:
11647 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11648 &catch_vfork_breakpoint_ops);
11649 break;
11650 default:
11651 error (_("unsupported or unknown fork kind; cannot catch it"));
11652 break;
11653 }
11654 }
11655
11656 static void
11657 catch_exec_command_1 (char *arg_entry, int from_tty,
11658 struct cmd_list_element *command)
11659 {
11660 const char *arg = arg_entry;
11661 struct gdbarch *gdbarch = get_current_arch ();
11662 int tempflag;
11663 const char *cond_string = NULL;
11664
11665 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11666
11667 if (!arg)
11668 arg = "";
11669 arg = skip_spaces_const (arg);
11670
11671 /* The allowed syntax is:
11672 catch exec
11673 catch exec if <cond>
11674
11675 First, check if there's an if clause. */
11676 cond_string = ep_parse_optional_if_clause (&arg);
11677
11678 if ((*arg != '\0') && !isspace (*arg))
11679 error (_("Junk at end of arguments."));
11680
11681 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11682 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11683 &catch_exec_breakpoint_ops);
11684 c->exec_pathname = NULL;
11685
11686 install_breakpoint (0, std::move (c), 1);
11687 }
11688
11689 void
11690 init_ada_exception_breakpoint (struct breakpoint *b,
11691 struct gdbarch *gdbarch,
11692 struct symtab_and_line sal,
11693 char *addr_string,
11694 const struct breakpoint_ops *ops,
11695 int tempflag,
11696 int enabled,
11697 int from_tty)
11698 {
11699 if (from_tty)
11700 {
11701 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11702 if (!loc_gdbarch)
11703 loc_gdbarch = gdbarch;
11704
11705 describe_other_breakpoints (loc_gdbarch,
11706 sal.pspace, sal.pc, sal.section, -1);
11707 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11708 version for exception catchpoints, because two catchpoints
11709 used for different exception names will use the same address.
11710 In this case, a "breakpoint ... also set at..." warning is
11711 unproductive. Besides, the warning phrasing is also a bit
11712 inappropriate, we should use the word catchpoint, and tell
11713 the user what type of catchpoint it is. The above is good
11714 enough for now, though. */
11715 }
11716
11717 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11718
11719 b->enable_state = enabled ? bp_enabled : bp_disabled;
11720 b->disposition = tempflag ? disp_del : disp_donttouch;
11721 b->location = string_to_event_location (&addr_string,
11722 language_def (language_ada));
11723 b->language = language_ada;
11724 }
11725
11726 static void
11727 catch_command (char *arg, int from_tty)
11728 {
11729 error (_("Catch requires an event name."));
11730 }
11731 \f
11732
11733 static void
11734 tcatch_command (char *arg, int from_tty)
11735 {
11736 error (_("Catch requires an event name."));
11737 }
11738
11739 /* A qsort comparison function that sorts breakpoints in order. */
11740
11741 static int
11742 compare_breakpoints (const void *a, const void *b)
11743 {
11744 const breakpoint_p *ba = (const breakpoint_p *) a;
11745 uintptr_t ua = (uintptr_t) *ba;
11746 const breakpoint_p *bb = (const breakpoint_p *) b;
11747 uintptr_t ub = (uintptr_t) *bb;
11748
11749 if ((*ba)->number < (*bb)->number)
11750 return -1;
11751 else if ((*ba)->number > (*bb)->number)
11752 return 1;
11753
11754 /* Now sort by address, in case we see, e..g, two breakpoints with
11755 the number 0. */
11756 if (ua < ub)
11757 return -1;
11758 return ua > ub ? 1 : 0;
11759 }
11760
11761 /* Delete breakpoints by address or line. */
11762
11763 static void
11764 clear_command (char *arg, int from_tty)
11765 {
11766 struct breakpoint *b, *prev;
11767 VEC(breakpoint_p) *found = 0;
11768 int ix;
11769 int default_match;
11770 int i;
11771 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11772
11773 std::vector<symtab_and_line> decoded_sals;
11774 symtab_and_line last_sal;
11775 gdb::array_view<symtab_and_line> sals;
11776 if (arg)
11777 {
11778 decoded_sals
11779 = decode_line_with_current_source (arg,
11780 (DECODE_LINE_FUNFIRSTLINE
11781 | DECODE_LINE_LIST_MODE));
11782 default_match = 0;
11783 sals = decoded_sals;
11784 }
11785 else
11786 {
11787 /* Set sal's line, symtab, pc, and pspace to the values
11788 corresponding to the last call to print_frame_info. If the
11789 codepoint is not valid, this will set all the fields to 0. */
11790 last_sal = get_last_displayed_sal ();
11791 if (last_sal.symtab == 0)
11792 error (_("No source file specified."));
11793
11794 default_match = 1;
11795 sals = last_sal;
11796 }
11797
11798 /* We don't call resolve_sal_pc here. That's not as bad as it
11799 seems, because all existing breakpoints typically have both
11800 file/line and pc set. So, if clear is given file/line, we can
11801 match this to existing breakpoint without obtaining pc at all.
11802
11803 We only support clearing given the address explicitly
11804 present in breakpoint table. Say, we've set breakpoint
11805 at file:line. There were several PC values for that file:line,
11806 due to optimization, all in one block.
11807
11808 We've picked one PC value. If "clear" is issued with another
11809 PC corresponding to the same file:line, the breakpoint won't
11810 be cleared. We probably can still clear the breakpoint, but
11811 since the other PC value is never presented to user, user
11812 can only find it by guessing, and it does not seem important
11813 to support that. */
11814
11815 /* For each line spec given, delete bps which correspond to it. Do
11816 it in two passes, solely to preserve the current behavior that
11817 from_tty is forced true if we delete more than one
11818 breakpoint. */
11819
11820 found = NULL;
11821 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11822 for (const auto &sal : sals)
11823 {
11824 const char *sal_fullname;
11825
11826 /* If exact pc given, clear bpts at that pc.
11827 If line given (pc == 0), clear all bpts on specified line.
11828 If defaulting, clear all bpts on default line
11829 or at default pc.
11830
11831 defaulting sal.pc != 0 tests to do
11832
11833 0 1 pc
11834 1 1 pc _and_ line
11835 0 0 line
11836 1 0 <can't happen> */
11837
11838 sal_fullname = (sal.symtab == NULL
11839 ? NULL : symtab_to_fullname (sal.symtab));
11840
11841 /* Find all matching breakpoints and add them to 'found'. */
11842 ALL_BREAKPOINTS (b)
11843 {
11844 int match = 0;
11845 /* Are we going to delete b? */
11846 if (b->type != bp_none && !is_watchpoint (b))
11847 {
11848 struct bp_location *loc = b->loc;
11849 for (; loc; loc = loc->next)
11850 {
11851 /* If the user specified file:line, don't allow a PC
11852 match. This matches historical gdb behavior. */
11853 int pc_match = (!sal.explicit_line
11854 && sal.pc
11855 && (loc->pspace == sal.pspace)
11856 && (loc->address == sal.pc)
11857 && (!section_is_overlay (loc->section)
11858 || loc->section == sal.section));
11859 int line_match = 0;
11860
11861 if ((default_match || sal.explicit_line)
11862 && loc->symtab != NULL
11863 && sal_fullname != NULL
11864 && sal.pspace == loc->pspace
11865 && loc->line_number == sal.line
11866 && filename_cmp (symtab_to_fullname (loc->symtab),
11867 sal_fullname) == 0)
11868 line_match = 1;
11869
11870 if (pc_match || line_match)
11871 {
11872 match = 1;
11873 break;
11874 }
11875 }
11876 }
11877
11878 if (match)
11879 VEC_safe_push(breakpoint_p, found, b);
11880 }
11881 }
11882
11883 /* Now go thru the 'found' chain and delete them. */
11884 if (VEC_empty(breakpoint_p, found))
11885 {
11886 if (arg)
11887 error (_("No breakpoint at %s."), arg);
11888 else
11889 error (_("No breakpoint at this line."));
11890 }
11891
11892 /* Remove duplicates from the vec. */
11893 qsort (VEC_address (breakpoint_p, found),
11894 VEC_length (breakpoint_p, found),
11895 sizeof (breakpoint_p),
11896 compare_breakpoints);
11897 prev = VEC_index (breakpoint_p, found, 0);
11898 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11899 {
11900 if (b == prev)
11901 {
11902 VEC_ordered_remove (breakpoint_p, found, ix);
11903 --ix;
11904 }
11905 }
11906
11907 if (VEC_length(breakpoint_p, found) > 1)
11908 from_tty = 1; /* Always report if deleted more than one. */
11909 if (from_tty)
11910 {
11911 if (VEC_length(breakpoint_p, found) == 1)
11912 printf_unfiltered (_("Deleted breakpoint "));
11913 else
11914 printf_unfiltered (_("Deleted breakpoints "));
11915 }
11916
11917 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11918 {
11919 if (from_tty)
11920 printf_unfiltered ("%d ", b->number);
11921 delete_breakpoint (b);
11922 }
11923 if (from_tty)
11924 putchar_unfiltered ('\n');
11925
11926 do_cleanups (cleanups);
11927 }
11928 \f
11929 /* Delete breakpoint in BS if they are `delete' breakpoints and
11930 all breakpoints that are marked for deletion, whether hit or not.
11931 This is called after any breakpoint is hit, or after errors. */
11932
11933 void
11934 breakpoint_auto_delete (bpstat bs)
11935 {
11936 struct breakpoint *b, *b_tmp;
11937
11938 for (; bs; bs = bs->next)
11939 if (bs->breakpoint_at
11940 && bs->breakpoint_at->disposition == disp_del
11941 && bs->stop)
11942 delete_breakpoint (bs->breakpoint_at);
11943
11944 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11945 {
11946 if (b->disposition == disp_del_at_next_stop)
11947 delete_breakpoint (b);
11948 }
11949 }
11950
11951 /* A comparison function for bp_location AP and BP being interfaced to
11952 qsort. Sort elements primarily by their ADDRESS (no matter what
11953 does breakpoint_address_is_meaningful say for its OWNER),
11954 secondarily by ordering first permanent elements and
11955 terciarily just ensuring the array is sorted stable way despite
11956 qsort being an unstable algorithm. */
11957
11958 static int
11959 bp_locations_compare (const void *ap, const void *bp)
11960 {
11961 const struct bp_location *a = *(const struct bp_location **) ap;
11962 const struct bp_location *b = *(const struct bp_location **) bp;
11963
11964 if (a->address != b->address)
11965 return (a->address > b->address) - (a->address < b->address);
11966
11967 /* Sort locations at the same address by their pspace number, keeping
11968 locations of the same inferior (in a multi-inferior environment)
11969 grouped. */
11970
11971 if (a->pspace->num != b->pspace->num)
11972 return ((a->pspace->num > b->pspace->num)
11973 - (a->pspace->num < b->pspace->num));
11974
11975 /* Sort permanent breakpoints first. */
11976 if (a->permanent != b->permanent)
11977 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11978
11979 /* Make the internal GDB representation stable across GDB runs
11980 where A and B memory inside GDB can differ. Breakpoint locations of
11981 the same type at the same address can be sorted in arbitrary order. */
11982
11983 if (a->owner->number != b->owner->number)
11984 return ((a->owner->number > b->owner->number)
11985 - (a->owner->number < b->owner->number));
11986
11987 return (a > b) - (a < b);
11988 }
11989
11990 /* Set bp_locations_placed_address_before_address_max and
11991 bp_locations_shadow_len_after_address_max according to the current
11992 content of the bp_locations array. */
11993
11994 static void
11995 bp_locations_target_extensions_update (void)
11996 {
11997 struct bp_location *bl, **blp_tmp;
11998
11999 bp_locations_placed_address_before_address_max = 0;
12000 bp_locations_shadow_len_after_address_max = 0;
12001
12002 ALL_BP_LOCATIONS (bl, blp_tmp)
12003 {
12004 CORE_ADDR start, end, addr;
12005
12006 if (!bp_location_has_shadow (bl))
12007 continue;
12008
12009 start = bl->target_info.placed_address;
12010 end = start + bl->target_info.shadow_len;
12011
12012 gdb_assert (bl->address >= start);
12013 addr = bl->address - start;
12014 if (addr > bp_locations_placed_address_before_address_max)
12015 bp_locations_placed_address_before_address_max = addr;
12016
12017 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12018
12019 gdb_assert (bl->address < end);
12020 addr = end - bl->address;
12021 if (addr > bp_locations_shadow_len_after_address_max)
12022 bp_locations_shadow_len_after_address_max = addr;
12023 }
12024 }
12025
12026 /* Download tracepoint locations if they haven't been. */
12027
12028 static void
12029 download_tracepoint_locations (void)
12030 {
12031 struct breakpoint *b;
12032 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12033
12034 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12035
12036 ALL_TRACEPOINTS (b)
12037 {
12038 struct bp_location *bl;
12039 struct tracepoint *t;
12040 int bp_location_downloaded = 0;
12041
12042 if ((b->type == bp_fast_tracepoint
12043 ? !may_insert_fast_tracepoints
12044 : !may_insert_tracepoints))
12045 continue;
12046
12047 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12048 {
12049 if (target_can_download_tracepoint ())
12050 can_download_tracepoint = TRIBOOL_TRUE;
12051 else
12052 can_download_tracepoint = TRIBOOL_FALSE;
12053 }
12054
12055 if (can_download_tracepoint == TRIBOOL_FALSE)
12056 break;
12057
12058 for (bl = b->loc; bl; bl = bl->next)
12059 {
12060 /* In tracepoint, locations are _never_ duplicated, so
12061 should_be_inserted is equivalent to
12062 unduplicated_should_be_inserted. */
12063 if (!should_be_inserted (bl) || bl->inserted)
12064 continue;
12065
12066 switch_to_program_space_and_thread (bl->pspace);
12067
12068 target_download_tracepoint (bl);
12069
12070 bl->inserted = 1;
12071 bp_location_downloaded = 1;
12072 }
12073 t = (struct tracepoint *) b;
12074 t->number_on_target = b->number;
12075 if (bp_location_downloaded)
12076 observer_notify_breakpoint_modified (b);
12077 }
12078 }
12079
12080 /* Swap the insertion/duplication state between two locations. */
12081
12082 static void
12083 swap_insertion (struct bp_location *left, struct bp_location *right)
12084 {
12085 const int left_inserted = left->inserted;
12086 const int left_duplicate = left->duplicate;
12087 const int left_needs_update = left->needs_update;
12088 const struct bp_target_info left_target_info = left->target_info;
12089
12090 /* Locations of tracepoints can never be duplicated. */
12091 if (is_tracepoint (left->owner))
12092 gdb_assert (!left->duplicate);
12093 if (is_tracepoint (right->owner))
12094 gdb_assert (!right->duplicate);
12095
12096 left->inserted = right->inserted;
12097 left->duplicate = right->duplicate;
12098 left->needs_update = right->needs_update;
12099 left->target_info = right->target_info;
12100 right->inserted = left_inserted;
12101 right->duplicate = left_duplicate;
12102 right->needs_update = left_needs_update;
12103 right->target_info = left_target_info;
12104 }
12105
12106 /* Force the re-insertion of the locations at ADDRESS. This is called
12107 once a new/deleted/modified duplicate location is found and we are evaluating
12108 conditions on the target's side. Such conditions need to be updated on
12109 the target. */
12110
12111 static void
12112 force_breakpoint_reinsertion (struct bp_location *bl)
12113 {
12114 struct bp_location **locp = NULL, **loc2p;
12115 struct bp_location *loc;
12116 CORE_ADDR address = 0;
12117 int pspace_num;
12118
12119 address = bl->address;
12120 pspace_num = bl->pspace->num;
12121
12122 /* This is only meaningful if the target is
12123 evaluating conditions and if the user has
12124 opted for condition evaluation on the target's
12125 side. */
12126 if (gdb_evaluates_breakpoint_condition_p ()
12127 || !target_supports_evaluation_of_breakpoint_conditions ())
12128 return;
12129
12130 /* Flag all breakpoint locations with this address and
12131 the same program space as the location
12132 as "its condition has changed". We need to
12133 update the conditions on the target's side. */
12134 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12135 {
12136 loc = *loc2p;
12137
12138 if (!is_breakpoint (loc->owner)
12139 || pspace_num != loc->pspace->num)
12140 continue;
12141
12142 /* Flag the location appropriately. We use a different state to
12143 let everyone know that we already updated the set of locations
12144 with addr bl->address and program space bl->pspace. This is so
12145 we don't have to keep calling these functions just to mark locations
12146 that have already been marked. */
12147 loc->condition_changed = condition_updated;
12148
12149 /* Free the agent expression bytecode as well. We will compute
12150 it later on. */
12151 loc->cond_bytecode.reset ();
12152 }
12153 }
12154 /* Called whether new breakpoints are created, or existing breakpoints
12155 deleted, to update the global location list and recompute which
12156 locations are duplicate of which.
12157
12158 The INSERT_MODE flag determines whether locations may not, may, or
12159 shall be inserted now. See 'enum ugll_insert_mode' for more
12160 info. */
12161
12162 static void
12163 update_global_location_list (enum ugll_insert_mode insert_mode)
12164 {
12165 struct breakpoint *b;
12166 struct bp_location **locp, *loc;
12167 struct cleanup *cleanups;
12168 /* Last breakpoint location address that was marked for update. */
12169 CORE_ADDR last_addr = 0;
12170 /* Last breakpoint location program space that was marked for update. */
12171 int last_pspace_num = -1;
12172
12173 /* Used in the duplicates detection below. When iterating over all
12174 bp_locations, points to the first bp_location of a given address.
12175 Breakpoints and watchpoints of different types are never
12176 duplicates of each other. Keep one pointer for each type of
12177 breakpoint/watchpoint, so we only need to loop over all locations
12178 once. */
12179 struct bp_location *bp_loc_first; /* breakpoint */
12180 struct bp_location *wp_loc_first; /* hardware watchpoint */
12181 struct bp_location *awp_loc_first; /* access watchpoint */
12182 struct bp_location *rwp_loc_first; /* read watchpoint */
12183
12184 /* Saved former bp_locations array which we compare against the newly
12185 built bp_locations from the current state of ALL_BREAKPOINTS. */
12186 struct bp_location **old_locations, **old_locp;
12187 unsigned old_locations_count;
12188
12189 old_locations = bp_locations;
12190 old_locations_count = bp_locations_count;
12191 bp_locations = NULL;
12192 bp_locations_count = 0;
12193 cleanups = make_cleanup (xfree, old_locations);
12194
12195 ALL_BREAKPOINTS (b)
12196 for (loc = b->loc; loc; loc = loc->next)
12197 bp_locations_count++;
12198
12199 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12200 locp = bp_locations;
12201 ALL_BREAKPOINTS (b)
12202 for (loc = b->loc; loc; loc = loc->next)
12203 *locp++ = loc;
12204 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12205 bp_locations_compare);
12206
12207 bp_locations_target_extensions_update ();
12208
12209 /* Identify bp_location instances that are no longer present in the
12210 new list, and therefore should be freed. Note that it's not
12211 necessary that those locations should be removed from inferior --
12212 if there's another location at the same address (previously
12213 marked as duplicate), we don't need to remove/insert the
12214 location.
12215
12216 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12217 and former bp_location array state respectively. */
12218
12219 locp = bp_locations;
12220 for (old_locp = old_locations;
12221 old_locp < old_locations + old_locations_count;
12222 old_locp++)
12223 {
12224 struct bp_location *old_loc = *old_locp;
12225 struct bp_location **loc2p;
12226
12227 /* Tells if 'old_loc' is found among the new locations. If
12228 not, we have to free it. */
12229 int found_object = 0;
12230 /* Tells if the location should remain inserted in the target. */
12231 int keep_in_target = 0;
12232 int removed = 0;
12233
12234 /* Skip LOCP entries which will definitely never be needed.
12235 Stop either at or being the one matching OLD_LOC. */
12236 while (locp < bp_locations + bp_locations_count
12237 && (*locp)->address < old_loc->address)
12238 locp++;
12239
12240 for (loc2p = locp;
12241 (loc2p < bp_locations + bp_locations_count
12242 && (*loc2p)->address == old_loc->address);
12243 loc2p++)
12244 {
12245 /* Check if this is a new/duplicated location or a duplicated
12246 location that had its condition modified. If so, we want to send
12247 its condition to the target if evaluation of conditions is taking
12248 place there. */
12249 if ((*loc2p)->condition_changed == condition_modified
12250 && (last_addr != old_loc->address
12251 || last_pspace_num != old_loc->pspace->num))
12252 {
12253 force_breakpoint_reinsertion (*loc2p);
12254 last_pspace_num = old_loc->pspace->num;
12255 }
12256
12257 if (*loc2p == old_loc)
12258 found_object = 1;
12259 }
12260
12261 /* We have already handled this address, update it so that we don't
12262 have to go through updates again. */
12263 last_addr = old_loc->address;
12264
12265 /* Target-side condition evaluation: Handle deleted locations. */
12266 if (!found_object)
12267 force_breakpoint_reinsertion (old_loc);
12268
12269 /* If this location is no longer present, and inserted, look if
12270 there's maybe a new location at the same address. If so,
12271 mark that one inserted, and don't remove this one. This is
12272 needed so that we don't have a time window where a breakpoint
12273 at certain location is not inserted. */
12274
12275 if (old_loc->inserted)
12276 {
12277 /* If the location is inserted now, we might have to remove
12278 it. */
12279
12280 if (found_object && should_be_inserted (old_loc))
12281 {
12282 /* The location is still present in the location list,
12283 and still should be inserted. Don't do anything. */
12284 keep_in_target = 1;
12285 }
12286 else
12287 {
12288 /* This location still exists, but it won't be kept in the
12289 target since it may have been disabled. We proceed to
12290 remove its target-side condition. */
12291
12292 /* The location is either no longer present, or got
12293 disabled. See if there's another location at the
12294 same address, in which case we don't need to remove
12295 this one from the target. */
12296
12297 /* OLD_LOC comes from existing struct breakpoint. */
12298 if (breakpoint_address_is_meaningful (old_loc->owner))
12299 {
12300 for (loc2p = locp;
12301 (loc2p < bp_locations + bp_locations_count
12302 && (*loc2p)->address == old_loc->address);
12303 loc2p++)
12304 {
12305 struct bp_location *loc2 = *loc2p;
12306
12307 if (breakpoint_locations_match (loc2, old_loc))
12308 {
12309 /* Read watchpoint locations are switched to
12310 access watchpoints, if the former are not
12311 supported, but the latter are. */
12312 if (is_hardware_watchpoint (old_loc->owner))
12313 {
12314 gdb_assert (is_hardware_watchpoint (loc2->owner));
12315 loc2->watchpoint_type = old_loc->watchpoint_type;
12316 }
12317
12318 /* loc2 is a duplicated location. We need to check
12319 if it should be inserted in case it will be
12320 unduplicated. */
12321 if (loc2 != old_loc
12322 && unduplicated_should_be_inserted (loc2))
12323 {
12324 swap_insertion (old_loc, loc2);
12325 keep_in_target = 1;
12326 break;
12327 }
12328 }
12329 }
12330 }
12331 }
12332
12333 if (!keep_in_target)
12334 {
12335 if (remove_breakpoint (old_loc))
12336 {
12337 /* This is just about all we can do. We could keep
12338 this location on the global list, and try to
12339 remove it next time, but there's no particular
12340 reason why we will succeed next time.
12341
12342 Note that at this point, old_loc->owner is still
12343 valid, as delete_breakpoint frees the breakpoint
12344 only after calling us. */
12345 printf_filtered (_("warning: Error removing "
12346 "breakpoint %d\n"),
12347 old_loc->owner->number);
12348 }
12349 removed = 1;
12350 }
12351 }
12352
12353 if (!found_object)
12354 {
12355 if (removed && target_is_non_stop_p ()
12356 && need_moribund_for_location_type (old_loc))
12357 {
12358 /* This location was removed from the target. In
12359 non-stop mode, a race condition is possible where
12360 we've removed a breakpoint, but stop events for that
12361 breakpoint are already queued and will arrive later.
12362 We apply an heuristic to be able to distinguish such
12363 SIGTRAPs from other random SIGTRAPs: we keep this
12364 breakpoint location for a bit, and will retire it
12365 after we see some number of events. The theory here
12366 is that reporting of events should, "on the average",
12367 be fair, so after a while we'll see events from all
12368 threads that have anything of interest, and no longer
12369 need to keep this breakpoint location around. We
12370 don't hold locations forever so to reduce chances of
12371 mistaking a non-breakpoint SIGTRAP for a breakpoint
12372 SIGTRAP.
12373
12374 The heuristic failing can be disastrous on
12375 decr_pc_after_break targets.
12376
12377 On decr_pc_after_break targets, like e.g., x86-linux,
12378 if we fail to recognize a late breakpoint SIGTRAP,
12379 because events_till_retirement has reached 0 too
12380 soon, we'll fail to do the PC adjustment, and report
12381 a random SIGTRAP to the user. When the user resumes
12382 the inferior, it will most likely immediately crash
12383 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12384 corrupted, because of being resumed e.g., in the
12385 middle of a multi-byte instruction, or skipped a
12386 one-byte instruction. This was actually seen happen
12387 on native x86-linux, and should be less rare on
12388 targets that do not support new thread events, like
12389 remote, due to the heuristic depending on
12390 thread_count.
12391
12392 Mistaking a random SIGTRAP for a breakpoint trap
12393 causes similar symptoms (PC adjustment applied when
12394 it shouldn't), but then again, playing with SIGTRAPs
12395 behind the debugger's back is asking for trouble.
12396
12397 Since hardware watchpoint traps are always
12398 distinguishable from other traps, so we don't need to
12399 apply keep hardware watchpoint moribund locations
12400 around. We simply always ignore hardware watchpoint
12401 traps we can no longer explain. */
12402
12403 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12404 old_loc->owner = NULL;
12405
12406 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12407 }
12408 else
12409 {
12410 old_loc->owner = NULL;
12411 decref_bp_location (&old_loc);
12412 }
12413 }
12414 }
12415
12416 /* Rescan breakpoints at the same address and section, marking the
12417 first one as "first" and any others as "duplicates". This is so
12418 that the bpt instruction is only inserted once. If we have a
12419 permanent breakpoint at the same place as BPT, make that one the
12420 official one, and the rest as duplicates. Permanent breakpoints
12421 are sorted first for the same address.
12422
12423 Do the same for hardware watchpoints, but also considering the
12424 watchpoint's type (regular/access/read) and length. */
12425
12426 bp_loc_first = NULL;
12427 wp_loc_first = NULL;
12428 awp_loc_first = NULL;
12429 rwp_loc_first = NULL;
12430 ALL_BP_LOCATIONS (loc, locp)
12431 {
12432 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12433 non-NULL. */
12434 struct bp_location **loc_first_p;
12435 b = loc->owner;
12436
12437 if (!unduplicated_should_be_inserted (loc)
12438 || !breakpoint_address_is_meaningful (b)
12439 /* Don't detect duplicate for tracepoint locations because they are
12440 never duplicated. See the comments in field `duplicate' of
12441 `struct bp_location'. */
12442 || is_tracepoint (b))
12443 {
12444 /* Clear the condition modification flag. */
12445 loc->condition_changed = condition_unchanged;
12446 continue;
12447 }
12448
12449 if (b->type == bp_hardware_watchpoint)
12450 loc_first_p = &wp_loc_first;
12451 else if (b->type == bp_read_watchpoint)
12452 loc_first_p = &rwp_loc_first;
12453 else if (b->type == bp_access_watchpoint)
12454 loc_first_p = &awp_loc_first;
12455 else
12456 loc_first_p = &bp_loc_first;
12457
12458 if (*loc_first_p == NULL
12459 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12460 || !breakpoint_locations_match (loc, *loc_first_p))
12461 {
12462 *loc_first_p = loc;
12463 loc->duplicate = 0;
12464
12465 if (is_breakpoint (loc->owner) && loc->condition_changed)
12466 {
12467 loc->needs_update = 1;
12468 /* Clear the condition modification flag. */
12469 loc->condition_changed = condition_unchanged;
12470 }
12471 continue;
12472 }
12473
12474
12475 /* This and the above ensure the invariant that the first location
12476 is not duplicated, and is the inserted one.
12477 All following are marked as duplicated, and are not inserted. */
12478 if (loc->inserted)
12479 swap_insertion (loc, *loc_first_p);
12480 loc->duplicate = 1;
12481
12482 /* Clear the condition modification flag. */
12483 loc->condition_changed = condition_unchanged;
12484 }
12485
12486 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12487 {
12488 if (insert_mode != UGLL_DONT_INSERT)
12489 insert_breakpoint_locations ();
12490 else
12491 {
12492 /* Even though the caller told us to not insert new
12493 locations, we may still need to update conditions on the
12494 target's side of breakpoints that were already inserted
12495 if the target is evaluating breakpoint conditions. We
12496 only update conditions for locations that are marked
12497 "needs_update". */
12498 update_inserted_breakpoint_locations ();
12499 }
12500 }
12501
12502 if (insert_mode != UGLL_DONT_INSERT)
12503 download_tracepoint_locations ();
12504
12505 do_cleanups (cleanups);
12506 }
12507
12508 void
12509 breakpoint_retire_moribund (void)
12510 {
12511 struct bp_location *loc;
12512 int ix;
12513
12514 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12515 if (--(loc->events_till_retirement) == 0)
12516 {
12517 decref_bp_location (&loc);
12518 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12519 --ix;
12520 }
12521 }
12522
12523 static void
12524 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12525 {
12526
12527 TRY
12528 {
12529 update_global_location_list (insert_mode);
12530 }
12531 CATCH (e, RETURN_MASK_ERROR)
12532 {
12533 }
12534 END_CATCH
12535 }
12536
12537 /* Clear BKP from a BPS. */
12538
12539 static void
12540 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12541 {
12542 bpstat bs;
12543
12544 for (bs = bps; bs; bs = bs->next)
12545 if (bs->breakpoint_at == bpt)
12546 {
12547 bs->breakpoint_at = NULL;
12548 bs->old_val = NULL;
12549 /* bs->commands will be freed later. */
12550 }
12551 }
12552
12553 /* Callback for iterate_over_threads. */
12554 static int
12555 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12556 {
12557 struct breakpoint *bpt = (struct breakpoint *) data;
12558
12559 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12560 return 0;
12561 }
12562
12563 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12564 callbacks. */
12565
12566 static void
12567 say_where (struct breakpoint *b)
12568 {
12569 struct value_print_options opts;
12570
12571 get_user_print_options (&opts);
12572
12573 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12574 single string. */
12575 if (b->loc == NULL)
12576 {
12577 /* For pending locations, the output differs slightly based
12578 on b->extra_string. If this is non-NULL, it contains either
12579 a condition or dprintf arguments. */
12580 if (b->extra_string == NULL)
12581 {
12582 printf_filtered (_(" (%s) pending."),
12583 event_location_to_string (b->location.get ()));
12584 }
12585 else if (b->type == bp_dprintf)
12586 {
12587 printf_filtered (_(" (%s,%s) pending."),
12588 event_location_to_string (b->location.get ()),
12589 b->extra_string);
12590 }
12591 else
12592 {
12593 printf_filtered (_(" (%s %s) pending."),
12594 event_location_to_string (b->location.get ()),
12595 b->extra_string);
12596 }
12597 }
12598 else
12599 {
12600 if (opts.addressprint || b->loc->symtab == NULL)
12601 {
12602 printf_filtered (" at ");
12603 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12604 gdb_stdout);
12605 }
12606 if (b->loc->symtab != NULL)
12607 {
12608 /* If there is a single location, we can print the location
12609 more nicely. */
12610 if (b->loc->next == NULL)
12611 printf_filtered (": file %s, line %d.",
12612 symtab_to_filename_for_display (b->loc->symtab),
12613 b->loc->line_number);
12614 else
12615 /* This is not ideal, but each location may have a
12616 different file name, and this at least reflects the
12617 real situation somewhat. */
12618 printf_filtered (": %s.",
12619 event_location_to_string (b->location.get ()));
12620 }
12621
12622 if (b->loc->next)
12623 {
12624 struct bp_location *loc = b->loc;
12625 int n = 0;
12626 for (; loc; loc = loc->next)
12627 ++n;
12628 printf_filtered (" (%d locations)", n);
12629 }
12630 }
12631 }
12632
12633 /* Default bp_location_ops methods. */
12634
12635 static void
12636 bp_location_dtor (struct bp_location *self)
12637 {
12638 xfree (self->function_name);
12639 }
12640
12641 static const struct bp_location_ops bp_location_ops =
12642 {
12643 bp_location_dtor
12644 };
12645
12646 /* Destructor for the breakpoint base class. */
12647
12648 breakpoint::~breakpoint ()
12649 {
12650 decref_counted_command_line (&this->commands);
12651 xfree (this->cond_string);
12652 xfree (this->extra_string);
12653 xfree (this->filter);
12654 }
12655
12656 static struct bp_location *
12657 base_breakpoint_allocate_location (struct breakpoint *self)
12658 {
12659 return new bp_location (&bp_location_ops, self);
12660 }
12661
12662 static void
12663 base_breakpoint_re_set (struct breakpoint *b)
12664 {
12665 /* Nothing to re-set. */
12666 }
12667
12668 #define internal_error_pure_virtual_called() \
12669 gdb_assert_not_reached ("pure virtual function called")
12670
12671 static int
12672 base_breakpoint_insert_location (struct bp_location *bl)
12673 {
12674 internal_error_pure_virtual_called ();
12675 }
12676
12677 static int
12678 base_breakpoint_remove_location (struct bp_location *bl,
12679 enum remove_bp_reason reason)
12680 {
12681 internal_error_pure_virtual_called ();
12682 }
12683
12684 static int
12685 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12686 struct address_space *aspace,
12687 CORE_ADDR bp_addr,
12688 const struct target_waitstatus *ws)
12689 {
12690 internal_error_pure_virtual_called ();
12691 }
12692
12693 static void
12694 base_breakpoint_check_status (bpstat bs)
12695 {
12696 /* Always stop. */
12697 }
12698
12699 /* A "works_in_software_mode" breakpoint_ops method that just internal
12700 errors. */
12701
12702 static int
12703 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12704 {
12705 internal_error_pure_virtual_called ();
12706 }
12707
12708 /* A "resources_needed" breakpoint_ops method that just internal
12709 errors. */
12710
12711 static int
12712 base_breakpoint_resources_needed (const struct bp_location *bl)
12713 {
12714 internal_error_pure_virtual_called ();
12715 }
12716
12717 static enum print_stop_action
12718 base_breakpoint_print_it (bpstat bs)
12719 {
12720 internal_error_pure_virtual_called ();
12721 }
12722
12723 static void
12724 base_breakpoint_print_one_detail (const struct breakpoint *self,
12725 struct ui_out *uiout)
12726 {
12727 /* nothing */
12728 }
12729
12730 static void
12731 base_breakpoint_print_mention (struct breakpoint *b)
12732 {
12733 internal_error_pure_virtual_called ();
12734 }
12735
12736 static void
12737 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12738 {
12739 internal_error_pure_virtual_called ();
12740 }
12741
12742 static void
12743 base_breakpoint_create_sals_from_location
12744 (const struct event_location *location,
12745 struct linespec_result *canonical,
12746 enum bptype type_wanted)
12747 {
12748 internal_error_pure_virtual_called ();
12749 }
12750
12751 static void
12752 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12753 struct linespec_result *c,
12754 gdb::unique_xmalloc_ptr<char> cond_string,
12755 gdb::unique_xmalloc_ptr<char> extra_string,
12756 enum bptype type_wanted,
12757 enum bpdisp disposition,
12758 int thread,
12759 int task, int ignore_count,
12760 const struct breakpoint_ops *o,
12761 int from_tty, int enabled,
12762 int internal, unsigned flags)
12763 {
12764 internal_error_pure_virtual_called ();
12765 }
12766
12767 static std::vector<symtab_and_line>
12768 base_breakpoint_decode_location (struct breakpoint *b,
12769 const struct event_location *location,
12770 struct program_space *search_pspace)
12771 {
12772 internal_error_pure_virtual_called ();
12773 }
12774
12775 /* The default 'explains_signal' method. */
12776
12777 static int
12778 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12779 {
12780 return 1;
12781 }
12782
12783 /* The default "after_condition_true" method. */
12784
12785 static void
12786 base_breakpoint_after_condition_true (struct bpstats *bs)
12787 {
12788 /* Nothing to do. */
12789 }
12790
12791 struct breakpoint_ops base_breakpoint_ops =
12792 {
12793 base_breakpoint_allocate_location,
12794 base_breakpoint_re_set,
12795 base_breakpoint_insert_location,
12796 base_breakpoint_remove_location,
12797 base_breakpoint_breakpoint_hit,
12798 base_breakpoint_check_status,
12799 base_breakpoint_resources_needed,
12800 base_breakpoint_works_in_software_mode,
12801 base_breakpoint_print_it,
12802 NULL,
12803 base_breakpoint_print_one_detail,
12804 base_breakpoint_print_mention,
12805 base_breakpoint_print_recreate,
12806 base_breakpoint_create_sals_from_location,
12807 base_breakpoint_create_breakpoints_sal,
12808 base_breakpoint_decode_location,
12809 base_breakpoint_explains_signal,
12810 base_breakpoint_after_condition_true,
12811 };
12812
12813 /* Default breakpoint_ops methods. */
12814
12815 static void
12816 bkpt_re_set (struct breakpoint *b)
12817 {
12818 /* FIXME: is this still reachable? */
12819 if (breakpoint_event_location_empty_p (b))
12820 {
12821 /* Anything without a location can't be re-set. */
12822 delete_breakpoint (b);
12823 return;
12824 }
12825
12826 breakpoint_re_set_default (b);
12827 }
12828
12829 static int
12830 bkpt_insert_location (struct bp_location *bl)
12831 {
12832 CORE_ADDR addr = bl->target_info.reqstd_address;
12833
12834 bl->target_info.kind = breakpoint_kind (bl, &addr);
12835 bl->target_info.placed_address = addr;
12836
12837 if (bl->loc_type == bp_loc_hardware_breakpoint)
12838 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12839 else
12840 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12841 }
12842
12843 static int
12844 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12845 {
12846 if (bl->loc_type == bp_loc_hardware_breakpoint)
12847 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12848 else
12849 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12850 }
12851
12852 static int
12853 bkpt_breakpoint_hit (const struct bp_location *bl,
12854 struct address_space *aspace, CORE_ADDR bp_addr,
12855 const struct target_waitstatus *ws)
12856 {
12857 if (ws->kind != TARGET_WAITKIND_STOPPED
12858 || ws->value.sig != GDB_SIGNAL_TRAP)
12859 return 0;
12860
12861 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12862 aspace, bp_addr))
12863 return 0;
12864
12865 if (overlay_debugging /* unmapped overlay section */
12866 && section_is_overlay (bl->section)
12867 && !section_is_mapped (bl->section))
12868 return 0;
12869
12870 return 1;
12871 }
12872
12873 static int
12874 dprintf_breakpoint_hit (const struct bp_location *bl,
12875 struct address_space *aspace, CORE_ADDR bp_addr,
12876 const struct target_waitstatus *ws)
12877 {
12878 if (dprintf_style == dprintf_style_agent
12879 && target_can_run_breakpoint_commands ())
12880 {
12881 /* An agent-style dprintf never causes a stop. If we see a trap
12882 for this address it must be for a breakpoint that happens to
12883 be set at the same address. */
12884 return 0;
12885 }
12886
12887 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12888 }
12889
12890 static int
12891 bkpt_resources_needed (const struct bp_location *bl)
12892 {
12893 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12894
12895 return 1;
12896 }
12897
12898 static enum print_stop_action
12899 bkpt_print_it (bpstat bs)
12900 {
12901 struct breakpoint *b;
12902 const struct bp_location *bl;
12903 int bp_temp;
12904 struct ui_out *uiout = current_uiout;
12905
12906 gdb_assert (bs->bp_location_at != NULL);
12907
12908 bl = bs->bp_location_at;
12909 b = bs->breakpoint_at;
12910
12911 bp_temp = b->disposition == disp_del;
12912 if (bl->address != bl->requested_address)
12913 breakpoint_adjustment_warning (bl->requested_address,
12914 bl->address,
12915 b->number, 1);
12916 annotate_breakpoint (b->number);
12917 maybe_print_thread_hit_breakpoint (uiout);
12918
12919 if (bp_temp)
12920 uiout->text ("Temporary breakpoint ");
12921 else
12922 uiout->text ("Breakpoint ");
12923 if (uiout->is_mi_like_p ())
12924 {
12925 uiout->field_string ("reason",
12926 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12927 uiout->field_string ("disp", bpdisp_text (b->disposition));
12928 }
12929 uiout->field_int ("bkptno", b->number);
12930 uiout->text (", ");
12931
12932 return PRINT_SRC_AND_LOC;
12933 }
12934
12935 static void
12936 bkpt_print_mention (struct breakpoint *b)
12937 {
12938 if (current_uiout->is_mi_like_p ())
12939 return;
12940
12941 switch (b->type)
12942 {
12943 case bp_breakpoint:
12944 case bp_gnu_ifunc_resolver:
12945 if (b->disposition == disp_del)
12946 printf_filtered (_("Temporary breakpoint"));
12947 else
12948 printf_filtered (_("Breakpoint"));
12949 printf_filtered (_(" %d"), b->number);
12950 if (b->type == bp_gnu_ifunc_resolver)
12951 printf_filtered (_(" at gnu-indirect-function resolver"));
12952 break;
12953 case bp_hardware_breakpoint:
12954 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12955 break;
12956 case bp_dprintf:
12957 printf_filtered (_("Dprintf %d"), b->number);
12958 break;
12959 }
12960
12961 say_where (b);
12962 }
12963
12964 static void
12965 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12966 {
12967 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12968 fprintf_unfiltered (fp, "tbreak");
12969 else if (tp->type == bp_breakpoint)
12970 fprintf_unfiltered (fp, "break");
12971 else if (tp->type == bp_hardware_breakpoint
12972 && tp->disposition == disp_del)
12973 fprintf_unfiltered (fp, "thbreak");
12974 else if (tp->type == bp_hardware_breakpoint)
12975 fprintf_unfiltered (fp, "hbreak");
12976 else
12977 internal_error (__FILE__, __LINE__,
12978 _("unhandled breakpoint type %d"), (int) tp->type);
12979
12980 fprintf_unfiltered (fp, " %s",
12981 event_location_to_string (tp->location.get ()));
12982
12983 /* Print out extra_string if this breakpoint is pending. It might
12984 contain, for example, conditions that were set by the user. */
12985 if (tp->loc == NULL && tp->extra_string != NULL)
12986 fprintf_unfiltered (fp, " %s", tp->extra_string);
12987
12988 print_recreate_thread (tp, fp);
12989 }
12990
12991 static void
12992 bkpt_create_sals_from_location (const struct event_location *location,
12993 struct linespec_result *canonical,
12994 enum bptype type_wanted)
12995 {
12996 create_sals_from_location_default (location, canonical, type_wanted);
12997 }
12998
12999 static void
13000 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13001 struct linespec_result *canonical,
13002 gdb::unique_xmalloc_ptr<char> cond_string,
13003 gdb::unique_xmalloc_ptr<char> extra_string,
13004 enum bptype type_wanted,
13005 enum bpdisp disposition,
13006 int thread,
13007 int task, int ignore_count,
13008 const struct breakpoint_ops *ops,
13009 int from_tty, int enabled,
13010 int internal, unsigned flags)
13011 {
13012 create_breakpoints_sal_default (gdbarch, canonical,
13013 std::move (cond_string),
13014 std::move (extra_string),
13015 type_wanted,
13016 disposition, thread, task,
13017 ignore_count, ops, from_tty,
13018 enabled, internal, flags);
13019 }
13020
13021 static std::vector<symtab_and_line>
13022 bkpt_decode_location (struct breakpoint *b,
13023 const struct event_location *location,
13024 struct program_space *search_pspace)
13025 {
13026 return decode_location_default (b, location, search_pspace);
13027 }
13028
13029 /* Virtual table for internal breakpoints. */
13030
13031 static void
13032 internal_bkpt_re_set (struct breakpoint *b)
13033 {
13034 switch (b->type)
13035 {
13036 /* Delete overlay event and longjmp master breakpoints; they
13037 will be reset later by breakpoint_re_set. */
13038 case bp_overlay_event:
13039 case bp_longjmp_master:
13040 case bp_std_terminate_master:
13041 case bp_exception_master:
13042 delete_breakpoint (b);
13043 break;
13044
13045 /* This breakpoint is special, it's set up when the inferior
13046 starts and we really don't want to touch it. */
13047 case bp_shlib_event:
13048
13049 /* Like bp_shlib_event, this breakpoint type is special. Once
13050 it is set up, we do not want to touch it. */
13051 case bp_thread_event:
13052 break;
13053 }
13054 }
13055
13056 static void
13057 internal_bkpt_check_status (bpstat bs)
13058 {
13059 if (bs->breakpoint_at->type == bp_shlib_event)
13060 {
13061 /* If requested, stop when the dynamic linker notifies GDB of
13062 events. This allows the user to get control and place
13063 breakpoints in initializer routines for dynamically loaded
13064 objects (among other things). */
13065 bs->stop = stop_on_solib_events;
13066 bs->print = stop_on_solib_events;
13067 }
13068 else
13069 bs->stop = 0;
13070 }
13071
13072 static enum print_stop_action
13073 internal_bkpt_print_it (bpstat bs)
13074 {
13075 struct breakpoint *b;
13076
13077 b = bs->breakpoint_at;
13078
13079 switch (b->type)
13080 {
13081 case bp_shlib_event:
13082 /* Did we stop because the user set the stop_on_solib_events
13083 variable? (If so, we report this as a generic, "Stopped due
13084 to shlib event" message.) */
13085 print_solib_event (0);
13086 break;
13087
13088 case bp_thread_event:
13089 /* Not sure how we will get here.
13090 GDB should not stop for these breakpoints. */
13091 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13092 break;
13093
13094 case bp_overlay_event:
13095 /* By analogy with the thread event, GDB should not stop for these. */
13096 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13097 break;
13098
13099 case bp_longjmp_master:
13100 /* These should never be enabled. */
13101 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13102 break;
13103
13104 case bp_std_terminate_master:
13105 /* These should never be enabled. */
13106 printf_filtered (_("std::terminate Master Breakpoint: "
13107 "gdb should not stop!\n"));
13108 break;
13109
13110 case bp_exception_master:
13111 /* These should never be enabled. */
13112 printf_filtered (_("Exception Master Breakpoint: "
13113 "gdb should not stop!\n"));
13114 break;
13115 }
13116
13117 return PRINT_NOTHING;
13118 }
13119
13120 static void
13121 internal_bkpt_print_mention (struct breakpoint *b)
13122 {
13123 /* Nothing to mention. These breakpoints are internal. */
13124 }
13125
13126 /* Virtual table for momentary breakpoints */
13127
13128 static void
13129 momentary_bkpt_re_set (struct breakpoint *b)
13130 {
13131 /* Keep temporary breakpoints, which can be encountered when we step
13132 over a dlopen call and solib_add is resetting the breakpoints.
13133 Otherwise these should have been blown away via the cleanup chain
13134 or by breakpoint_init_inferior when we rerun the executable. */
13135 }
13136
13137 static void
13138 momentary_bkpt_check_status (bpstat bs)
13139 {
13140 /* Nothing. The point of these breakpoints is causing a stop. */
13141 }
13142
13143 static enum print_stop_action
13144 momentary_bkpt_print_it (bpstat bs)
13145 {
13146 return PRINT_UNKNOWN;
13147 }
13148
13149 static void
13150 momentary_bkpt_print_mention (struct breakpoint *b)
13151 {
13152 /* Nothing to mention. These breakpoints are internal. */
13153 }
13154
13155 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13156
13157 It gets cleared already on the removal of the first one of such placed
13158 breakpoints. This is OK as they get all removed altogether. */
13159
13160 longjmp_breakpoint::~longjmp_breakpoint ()
13161 {
13162 thread_info *tp = find_thread_global_id (this->thread);
13163
13164 if (tp != NULL)
13165 tp->initiating_frame = null_frame_id;
13166 }
13167
13168 /* Specific methods for probe breakpoints. */
13169
13170 static int
13171 bkpt_probe_insert_location (struct bp_location *bl)
13172 {
13173 int v = bkpt_insert_location (bl);
13174
13175 if (v == 0)
13176 {
13177 /* The insertion was successful, now let's set the probe's semaphore
13178 if needed. */
13179 if (bl->probe.probe->pops->set_semaphore != NULL)
13180 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13181 bl->probe.objfile,
13182 bl->gdbarch);
13183 }
13184
13185 return v;
13186 }
13187
13188 static int
13189 bkpt_probe_remove_location (struct bp_location *bl,
13190 enum remove_bp_reason reason)
13191 {
13192 /* Let's clear the semaphore before removing the location. */
13193 if (bl->probe.probe->pops->clear_semaphore != NULL)
13194 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13195 bl->probe.objfile,
13196 bl->gdbarch);
13197
13198 return bkpt_remove_location (bl, reason);
13199 }
13200
13201 static void
13202 bkpt_probe_create_sals_from_location (const struct event_location *location,
13203 struct linespec_result *canonical,
13204 enum bptype type_wanted)
13205 {
13206 struct linespec_sals lsal;
13207
13208 lsal.sals = parse_probes (location, NULL, canonical);
13209 lsal.canonical
13210 = xstrdup (event_location_to_string (canonical->location.get ()));
13211 canonical->lsals.push_back (std::move (lsal));
13212 }
13213
13214 static std::vector<symtab_and_line>
13215 bkpt_probe_decode_location (struct breakpoint *b,
13216 const struct event_location *location,
13217 struct program_space *search_pspace)
13218 {
13219 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13220 if (sals.empty ())
13221 error (_("probe not found"));
13222 return sals;
13223 }
13224
13225 /* The breakpoint_ops structure to be used in tracepoints. */
13226
13227 static void
13228 tracepoint_re_set (struct breakpoint *b)
13229 {
13230 breakpoint_re_set_default (b);
13231 }
13232
13233 static int
13234 tracepoint_breakpoint_hit (const struct bp_location *bl,
13235 struct address_space *aspace, CORE_ADDR bp_addr,
13236 const struct target_waitstatus *ws)
13237 {
13238 /* By definition, the inferior does not report stops at
13239 tracepoints. */
13240 return 0;
13241 }
13242
13243 static void
13244 tracepoint_print_one_detail (const struct breakpoint *self,
13245 struct ui_out *uiout)
13246 {
13247 struct tracepoint *tp = (struct tracepoint *) self;
13248 if (tp->static_trace_marker_id)
13249 {
13250 gdb_assert (self->type == bp_static_tracepoint);
13251
13252 uiout->text ("\tmarker id is ");
13253 uiout->field_string ("static-tracepoint-marker-string-id",
13254 tp->static_trace_marker_id);
13255 uiout->text ("\n");
13256 }
13257 }
13258
13259 static void
13260 tracepoint_print_mention (struct breakpoint *b)
13261 {
13262 if (current_uiout->is_mi_like_p ())
13263 return;
13264
13265 switch (b->type)
13266 {
13267 case bp_tracepoint:
13268 printf_filtered (_("Tracepoint"));
13269 printf_filtered (_(" %d"), b->number);
13270 break;
13271 case bp_fast_tracepoint:
13272 printf_filtered (_("Fast tracepoint"));
13273 printf_filtered (_(" %d"), b->number);
13274 break;
13275 case bp_static_tracepoint:
13276 printf_filtered (_("Static tracepoint"));
13277 printf_filtered (_(" %d"), b->number);
13278 break;
13279 default:
13280 internal_error (__FILE__, __LINE__,
13281 _("unhandled tracepoint type %d"), (int) b->type);
13282 }
13283
13284 say_where (b);
13285 }
13286
13287 static void
13288 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13289 {
13290 struct tracepoint *tp = (struct tracepoint *) self;
13291
13292 if (self->type == bp_fast_tracepoint)
13293 fprintf_unfiltered (fp, "ftrace");
13294 else if (self->type == bp_static_tracepoint)
13295 fprintf_unfiltered (fp, "strace");
13296 else if (self->type == bp_tracepoint)
13297 fprintf_unfiltered (fp, "trace");
13298 else
13299 internal_error (__FILE__, __LINE__,
13300 _("unhandled tracepoint type %d"), (int) self->type);
13301
13302 fprintf_unfiltered (fp, " %s",
13303 event_location_to_string (self->location.get ()));
13304 print_recreate_thread (self, fp);
13305
13306 if (tp->pass_count)
13307 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13308 }
13309
13310 static void
13311 tracepoint_create_sals_from_location (const struct event_location *location,
13312 struct linespec_result *canonical,
13313 enum bptype type_wanted)
13314 {
13315 create_sals_from_location_default (location, canonical, type_wanted);
13316 }
13317
13318 static void
13319 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13320 struct linespec_result *canonical,
13321 gdb::unique_xmalloc_ptr<char> cond_string,
13322 gdb::unique_xmalloc_ptr<char> extra_string,
13323 enum bptype type_wanted,
13324 enum bpdisp disposition,
13325 int thread,
13326 int task, int ignore_count,
13327 const struct breakpoint_ops *ops,
13328 int from_tty, int enabled,
13329 int internal, unsigned flags)
13330 {
13331 create_breakpoints_sal_default (gdbarch, canonical,
13332 std::move (cond_string),
13333 std::move (extra_string),
13334 type_wanted,
13335 disposition, thread, task,
13336 ignore_count, ops, from_tty,
13337 enabled, internal, flags);
13338 }
13339
13340 static std::vector<symtab_and_line>
13341 tracepoint_decode_location (struct breakpoint *b,
13342 const struct event_location *location,
13343 struct program_space *search_pspace)
13344 {
13345 return decode_location_default (b, location, search_pspace);
13346 }
13347
13348 struct breakpoint_ops tracepoint_breakpoint_ops;
13349
13350 /* The breakpoint_ops structure to be use on tracepoints placed in a
13351 static probe. */
13352
13353 static void
13354 tracepoint_probe_create_sals_from_location
13355 (const struct event_location *location,
13356 struct linespec_result *canonical,
13357 enum bptype type_wanted)
13358 {
13359 /* We use the same method for breakpoint on probes. */
13360 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13361 }
13362
13363 static std::vector<symtab_and_line>
13364 tracepoint_probe_decode_location (struct breakpoint *b,
13365 const struct event_location *location,
13366 struct program_space *search_pspace)
13367 {
13368 /* We use the same method for breakpoint on probes. */
13369 return bkpt_probe_decode_location (b, location, search_pspace);
13370 }
13371
13372 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13373
13374 /* Dprintf breakpoint_ops methods. */
13375
13376 static void
13377 dprintf_re_set (struct breakpoint *b)
13378 {
13379 breakpoint_re_set_default (b);
13380
13381 /* extra_string should never be non-NULL for dprintf. */
13382 gdb_assert (b->extra_string != NULL);
13383
13384 /* 1 - connect to target 1, that can run breakpoint commands.
13385 2 - create a dprintf, which resolves fine.
13386 3 - disconnect from target 1
13387 4 - connect to target 2, that can NOT run breakpoint commands.
13388
13389 After steps #3/#4, you'll want the dprintf command list to
13390 be updated, because target 1 and 2 may well return different
13391 answers for target_can_run_breakpoint_commands().
13392 Given absence of finer grained resetting, we get to do
13393 it all the time. */
13394 if (b->extra_string != NULL)
13395 update_dprintf_command_list (b);
13396 }
13397
13398 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13399
13400 static void
13401 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13402 {
13403 fprintf_unfiltered (fp, "dprintf %s,%s",
13404 event_location_to_string (tp->location.get ()),
13405 tp->extra_string);
13406 print_recreate_thread (tp, fp);
13407 }
13408
13409 /* Implement the "after_condition_true" breakpoint_ops method for
13410 dprintf.
13411
13412 dprintf's are implemented with regular commands in their command
13413 list, but we run the commands here instead of before presenting the
13414 stop to the user, as dprintf's don't actually cause a stop. This
13415 also makes it so that the commands of multiple dprintfs at the same
13416 address are all handled. */
13417
13418 static void
13419 dprintf_after_condition_true (struct bpstats *bs)
13420 {
13421 struct cleanup *old_chain;
13422 struct bpstats tmp_bs = { NULL };
13423 struct bpstats *tmp_bs_p = &tmp_bs;
13424
13425 /* dprintf's never cause a stop. This wasn't set in the
13426 check_status hook instead because that would make the dprintf's
13427 condition not be evaluated. */
13428 bs->stop = 0;
13429
13430 /* Run the command list here. Take ownership of it instead of
13431 copying. We never want these commands to run later in
13432 bpstat_do_actions, if a breakpoint that causes a stop happens to
13433 be set at same address as this dprintf, or even if running the
13434 commands here throws. */
13435 tmp_bs.commands = bs->commands;
13436 bs->commands = NULL;
13437 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13438
13439 bpstat_do_actions_1 (&tmp_bs_p);
13440
13441 /* 'tmp_bs.commands' will usually be NULL by now, but
13442 bpstat_do_actions_1 may return early without processing the whole
13443 list. */
13444 do_cleanups (old_chain);
13445 }
13446
13447 /* The breakpoint_ops structure to be used on static tracepoints with
13448 markers (`-m'). */
13449
13450 static void
13451 strace_marker_create_sals_from_location (const struct event_location *location,
13452 struct linespec_result *canonical,
13453 enum bptype type_wanted)
13454 {
13455 struct linespec_sals lsal;
13456 const char *arg_start, *arg;
13457 char *str;
13458 struct cleanup *cleanup;
13459
13460 arg = arg_start = get_linespec_location (location);
13461 lsal.sals = decode_static_tracepoint_spec (&arg);
13462
13463 str = savestring (arg_start, arg - arg_start);
13464 cleanup = make_cleanup (xfree, str);
13465 canonical->location = new_linespec_location (&str);
13466 do_cleanups (cleanup);
13467
13468 lsal.canonical
13469 = xstrdup (event_location_to_string (canonical->location.get ()));
13470 canonical->lsals.push_back (std::move (lsal));
13471 }
13472
13473 static void
13474 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13475 struct linespec_result *canonical,
13476 gdb::unique_xmalloc_ptr<char> cond_string,
13477 gdb::unique_xmalloc_ptr<char> extra_string,
13478 enum bptype type_wanted,
13479 enum bpdisp disposition,
13480 int thread,
13481 int task, int ignore_count,
13482 const struct breakpoint_ops *ops,
13483 int from_tty, int enabled,
13484 int internal, unsigned flags)
13485 {
13486 const linespec_sals &lsal = canonical->lsals[0];
13487
13488 /* If the user is creating a static tracepoint by marker id
13489 (strace -m MARKER_ID), then store the sals index, so that
13490 breakpoint_re_set can try to match up which of the newly
13491 found markers corresponds to this one, and, don't try to
13492 expand multiple locations for each sal, given than SALS
13493 already should contain all sals for MARKER_ID. */
13494
13495 for (size_t i = 0; i < lsal.sals.size (); i++)
13496 {
13497 event_location_up location
13498 = copy_event_location (canonical->location.get ());
13499
13500 std::unique_ptr<tracepoint> tp (new tracepoint ());
13501 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13502 std::move (location), NULL,
13503 std::move (cond_string),
13504 std::move (extra_string),
13505 type_wanted, disposition,
13506 thread, task, ignore_count, ops,
13507 from_tty, enabled, internal, flags,
13508 canonical->special_display);
13509 /* Given that its possible to have multiple markers with
13510 the same string id, if the user is creating a static
13511 tracepoint by marker id ("strace -m MARKER_ID"), then
13512 store the sals index, so that breakpoint_re_set can
13513 try to match up which of the newly found markers
13514 corresponds to this one */
13515 tp->static_trace_marker_id_idx = i;
13516
13517 install_breakpoint (internal, std::move (tp), 0);
13518 }
13519 }
13520
13521 static std::vector<symtab_and_line>
13522 strace_marker_decode_location (struct breakpoint *b,
13523 const struct event_location *location,
13524 struct program_space *search_pspace)
13525 {
13526 struct tracepoint *tp = (struct tracepoint *) b;
13527 const char *s = get_linespec_location (location);
13528
13529 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13530 if (sals.size () > tp->static_trace_marker_id_idx)
13531 {
13532 sals[0] = sals[tp->static_trace_marker_id_idx];
13533 sals.resize (1);
13534 return sals;
13535 }
13536 else
13537 error (_("marker %s not found"), tp->static_trace_marker_id);
13538 }
13539
13540 static struct breakpoint_ops strace_marker_breakpoint_ops;
13541
13542 static int
13543 strace_marker_p (struct breakpoint *b)
13544 {
13545 return b->ops == &strace_marker_breakpoint_ops;
13546 }
13547
13548 /* Delete a breakpoint and clean up all traces of it in the data
13549 structures. */
13550
13551 void
13552 delete_breakpoint (struct breakpoint *bpt)
13553 {
13554 struct breakpoint *b;
13555
13556 gdb_assert (bpt != NULL);
13557
13558 /* Has this bp already been deleted? This can happen because
13559 multiple lists can hold pointers to bp's. bpstat lists are
13560 especial culprits.
13561
13562 One example of this happening is a watchpoint's scope bp. When
13563 the scope bp triggers, we notice that the watchpoint is out of
13564 scope, and delete it. We also delete its scope bp. But the
13565 scope bp is marked "auto-deleting", and is already on a bpstat.
13566 That bpstat is then checked for auto-deleting bp's, which are
13567 deleted.
13568
13569 A real solution to this problem might involve reference counts in
13570 bp's, and/or giving them pointers back to their referencing
13571 bpstat's, and teaching delete_breakpoint to only free a bp's
13572 storage when no more references were extent. A cheaper bandaid
13573 was chosen. */
13574 if (bpt->type == bp_none)
13575 return;
13576
13577 /* At least avoid this stale reference until the reference counting
13578 of breakpoints gets resolved. */
13579 if (bpt->related_breakpoint != bpt)
13580 {
13581 struct breakpoint *related;
13582 struct watchpoint *w;
13583
13584 if (bpt->type == bp_watchpoint_scope)
13585 w = (struct watchpoint *) bpt->related_breakpoint;
13586 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13587 w = (struct watchpoint *) bpt;
13588 else
13589 w = NULL;
13590 if (w != NULL)
13591 watchpoint_del_at_next_stop (w);
13592
13593 /* Unlink bpt from the bpt->related_breakpoint ring. */
13594 for (related = bpt; related->related_breakpoint != bpt;
13595 related = related->related_breakpoint);
13596 related->related_breakpoint = bpt->related_breakpoint;
13597 bpt->related_breakpoint = bpt;
13598 }
13599
13600 /* watch_command_1 creates a watchpoint but only sets its number if
13601 update_watchpoint succeeds in creating its bp_locations. If there's
13602 a problem in that process, we'll be asked to delete the half-created
13603 watchpoint. In that case, don't announce the deletion. */
13604 if (bpt->number)
13605 observer_notify_breakpoint_deleted (bpt);
13606
13607 if (breakpoint_chain == bpt)
13608 breakpoint_chain = bpt->next;
13609
13610 ALL_BREAKPOINTS (b)
13611 if (b->next == bpt)
13612 {
13613 b->next = bpt->next;
13614 break;
13615 }
13616
13617 /* Be sure no bpstat's are pointing at the breakpoint after it's
13618 been freed. */
13619 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13620 in all threads for now. Note that we cannot just remove bpstats
13621 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13622 commands are associated with the bpstat; if we remove it here,
13623 then the later call to bpstat_do_actions (&stop_bpstat); in
13624 event-top.c won't do anything, and temporary breakpoints with
13625 commands won't work. */
13626
13627 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13628
13629 /* Now that breakpoint is removed from breakpoint list, update the
13630 global location list. This will remove locations that used to
13631 belong to this breakpoint. Do this before freeing the breakpoint
13632 itself, since remove_breakpoint looks at location's owner. It
13633 might be better design to have location completely
13634 self-contained, but it's not the case now. */
13635 update_global_location_list (UGLL_DONT_INSERT);
13636
13637 /* On the chance that someone will soon try again to delete this
13638 same bp, we mark it as deleted before freeing its storage. */
13639 bpt->type = bp_none;
13640 delete bpt;
13641 }
13642
13643 static void
13644 do_delete_breakpoint_cleanup (void *b)
13645 {
13646 delete_breakpoint ((struct breakpoint *) b);
13647 }
13648
13649 struct cleanup *
13650 make_cleanup_delete_breakpoint (struct breakpoint *b)
13651 {
13652 return make_cleanup (do_delete_breakpoint_cleanup, b);
13653 }
13654
13655 /* Iterator function to call a user-provided callback function once
13656 for each of B and its related breakpoints. */
13657
13658 static void
13659 iterate_over_related_breakpoints (struct breakpoint *b,
13660 void (*function) (struct breakpoint *,
13661 void *),
13662 void *data)
13663 {
13664 struct breakpoint *related;
13665
13666 related = b;
13667 do
13668 {
13669 struct breakpoint *next;
13670
13671 /* FUNCTION may delete RELATED. */
13672 next = related->related_breakpoint;
13673
13674 if (next == related)
13675 {
13676 /* RELATED is the last ring entry. */
13677 function (related, data);
13678
13679 /* FUNCTION may have deleted it, so we'd never reach back to
13680 B. There's nothing left to do anyway, so just break
13681 out. */
13682 break;
13683 }
13684 else
13685 function (related, data);
13686
13687 related = next;
13688 }
13689 while (related != b);
13690 }
13691
13692 static void
13693 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13694 {
13695 delete_breakpoint (b);
13696 }
13697
13698 /* A callback for map_breakpoint_numbers that calls
13699 delete_breakpoint. */
13700
13701 static void
13702 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13703 {
13704 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13705 }
13706
13707 void
13708 delete_command (char *arg, int from_tty)
13709 {
13710 struct breakpoint *b, *b_tmp;
13711
13712 dont_repeat ();
13713
13714 if (arg == 0)
13715 {
13716 int breaks_to_delete = 0;
13717
13718 /* Delete all breakpoints if no argument. Do not delete
13719 internal breakpoints, these have to be deleted with an
13720 explicit breakpoint number argument. */
13721 ALL_BREAKPOINTS (b)
13722 if (user_breakpoint_p (b))
13723 {
13724 breaks_to_delete = 1;
13725 break;
13726 }
13727
13728 /* Ask user only if there are some breakpoints to delete. */
13729 if (!from_tty
13730 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13731 {
13732 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13733 if (user_breakpoint_p (b))
13734 delete_breakpoint (b);
13735 }
13736 }
13737 else
13738 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13739 }
13740
13741 /* Return true if all locations of B bound to PSPACE are pending. If
13742 PSPACE is NULL, all locations of all program spaces are
13743 considered. */
13744
13745 static int
13746 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13747 {
13748 struct bp_location *loc;
13749
13750 for (loc = b->loc; loc != NULL; loc = loc->next)
13751 if ((pspace == NULL
13752 || loc->pspace == pspace)
13753 && !loc->shlib_disabled
13754 && !loc->pspace->executing_startup)
13755 return 0;
13756 return 1;
13757 }
13758
13759 /* Subroutine of update_breakpoint_locations to simplify it.
13760 Return non-zero if multiple fns in list LOC have the same name.
13761 Null names are ignored. */
13762
13763 static int
13764 ambiguous_names_p (struct bp_location *loc)
13765 {
13766 struct bp_location *l;
13767 htab_t htab = htab_create_alloc (13, htab_hash_string,
13768 (int (*) (const void *,
13769 const void *)) streq,
13770 NULL, xcalloc, xfree);
13771
13772 for (l = loc; l != NULL; l = l->next)
13773 {
13774 const char **slot;
13775 const char *name = l->function_name;
13776
13777 /* Allow for some names to be NULL, ignore them. */
13778 if (name == NULL)
13779 continue;
13780
13781 slot = (const char **) htab_find_slot (htab, (const void *) name,
13782 INSERT);
13783 /* NOTE: We can assume slot != NULL here because xcalloc never
13784 returns NULL. */
13785 if (*slot != NULL)
13786 {
13787 htab_delete (htab);
13788 return 1;
13789 }
13790 *slot = name;
13791 }
13792
13793 htab_delete (htab);
13794 return 0;
13795 }
13796
13797 /* When symbols change, it probably means the sources changed as well,
13798 and it might mean the static tracepoint markers are no longer at
13799 the same address or line numbers they used to be at last we
13800 checked. Losing your static tracepoints whenever you rebuild is
13801 undesirable. This function tries to resync/rematch gdb static
13802 tracepoints with the markers on the target, for static tracepoints
13803 that have not been set by marker id. Static tracepoint that have
13804 been set by marker id are reset by marker id in breakpoint_re_set.
13805 The heuristic is:
13806
13807 1) For a tracepoint set at a specific address, look for a marker at
13808 the old PC. If one is found there, assume to be the same marker.
13809 If the name / string id of the marker found is different from the
13810 previous known name, assume that means the user renamed the marker
13811 in the sources, and output a warning.
13812
13813 2) For a tracepoint set at a given line number, look for a marker
13814 at the new address of the old line number. If one is found there,
13815 assume to be the same marker. If the name / string id of the
13816 marker found is different from the previous known name, assume that
13817 means the user renamed the marker in the sources, and output a
13818 warning.
13819
13820 3) If a marker is no longer found at the same address or line, it
13821 may mean the marker no longer exists. But it may also just mean
13822 the code changed a bit. Maybe the user added a few lines of code
13823 that made the marker move up or down (in line number terms). Ask
13824 the target for info about the marker with the string id as we knew
13825 it. If found, update line number and address in the matching
13826 static tracepoint. This will get confused if there's more than one
13827 marker with the same ID (possible in UST, although unadvised
13828 precisely because it confuses tools). */
13829
13830 static struct symtab_and_line
13831 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13832 {
13833 struct tracepoint *tp = (struct tracepoint *) b;
13834 struct static_tracepoint_marker marker;
13835 CORE_ADDR pc;
13836
13837 pc = sal.pc;
13838 if (sal.line)
13839 find_line_pc (sal.symtab, sal.line, &pc);
13840
13841 if (target_static_tracepoint_marker_at (pc, &marker))
13842 {
13843 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13844 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13845 b->number,
13846 tp->static_trace_marker_id, marker.str_id);
13847
13848 xfree (tp->static_trace_marker_id);
13849 tp->static_trace_marker_id = xstrdup (marker.str_id);
13850 release_static_tracepoint_marker (&marker);
13851
13852 return sal;
13853 }
13854
13855 /* Old marker wasn't found on target at lineno. Try looking it up
13856 by string ID. */
13857 if (!sal.explicit_pc
13858 && sal.line != 0
13859 && sal.symtab != NULL
13860 && tp->static_trace_marker_id != NULL)
13861 {
13862 VEC(static_tracepoint_marker_p) *markers;
13863
13864 markers
13865 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13866
13867 if (!VEC_empty(static_tracepoint_marker_p, markers))
13868 {
13869 struct symbol *sym;
13870 struct static_tracepoint_marker *tpmarker;
13871 struct ui_out *uiout = current_uiout;
13872 struct explicit_location explicit_loc;
13873
13874 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13875
13876 xfree (tp->static_trace_marker_id);
13877 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13878
13879 warning (_("marker for static tracepoint %d (%s) not "
13880 "found at previous line number"),
13881 b->number, tp->static_trace_marker_id);
13882
13883 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13884 sym = find_pc_sect_function (tpmarker->address, NULL);
13885 uiout->text ("Now in ");
13886 if (sym)
13887 {
13888 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13889 uiout->text (" at ");
13890 }
13891 uiout->field_string ("file",
13892 symtab_to_filename_for_display (sal2.symtab));
13893 uiout->text (":");
13894
13895 if (uiout->is_mi_like_p ())
13896 {
13897 const char *fullname = symtab_to_fullname (sal2.symtab);
13898
13899 uiout->field_string ("fullname", fullname);
13900 }
13901
13902 uiout->field_int ("line", sal2.line);
13903 uiout->text ("\n");
13904
13905 b->loc->line_number = sal2.line;
13906 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13907
13908 b->location.reset (NULL);
13909 initialize_explicit_location (&explicit_loc);
13910 explicit_loc.source_filename
13911 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13912 explicit_loc.line_offset.offset = b->loc->line_number;
13913 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13914 b->location = new_explicit_location (&explicit_loc);
13915
13916 /* Might be nice to check if function changed, and warn if
13917 so. */
13918
13919 release_static_tracepoint_marker (tpmarker);
13920 }
13921 }
13922 return sal;
13923 }
13924
13925 /* Returns 1 iff locations A and B are sufficiently same that
13926 we don't need to report breakpoint as changed. */
13927
13928 static int
13929 locations_are_equal (struct bp_location *a, struct bp_location *b)
13930 {
13931 while (a && b)
13932 {
13933 if (a->address != b->address)
13934 return 0;
13935
13936 if (a->shlib_disabled != b->shlib_disabled)
13937 return 0;
13938
13939 if (a->enabled != b->enabled)
13940 return 0;
13941
13942 a = a->next;
13943 b = b->next;
13944 }
13945
13946 if ((a == NULL) != (b == NULL))
13947 return 0;
13948
13949 return 1;
13950 }
13951
13952 /* Split all locations of B that are bound to PSPACE out of B's
13953 location list to a separate list and return that list's head. If
13954 PSPACE is NULL, hoist out all locations of B. */
13955
13956 static struct bp_location *
13957 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13958 {
13959 struct bp_location head;
13960 struct bp_location *i = b->loc;
13961 struct bp_location **i_link = &b->loc;
13962 struct bp_location *hoisted = &head;
13963
13964 if (pspace == NULL)
13965 {
13966 i = b->loc;
13967 b->loc = NULL;
13968 return i;
13969 }
13970
13971 head.next = NULL;
13972
13973 while (i != NULL)
13974 {
13975 if (i->pspace == pspace)
13976 {
13977 *i_link = i->next;
13978 i->next = NULL;
13979 hoisted->next = i;
13980 hoisted = i;
13981 }
13982 else
13983 i_link = &i->next;
13984 i = *i_link;
13985 }
13986
13987 return head.next;
13988 }
13989
13990 /* Create new breakpoint locations for B (a hardware or software
13991 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13992 zero, then B is a ranged breakpoint. Only recreates locations for
13993 FILTER_PSPACE. Locations of other program spaces are left
13994 untouched. */
13995
13996 void
13997 update_breakpoint_locations (struct breakpoint *b,
13998 struct program_space *filter_pspace,
13999 gdb::array_view<const symtab_and_line> sals,
14000 gdb::array_view<const symtab_and_line> sals_end)
14001 {
14002 int i;
14003 struct bp_location *existing_locations;
14004
14005 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
14006 {
14007 /* Ranged breakpoints have only one start location and one end
14008 location. */
14009 b->enable_state = bp_disabled;
14010 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14011 "multiple locations found\n"),
14012 b->number);
14013 return;
14014 }
14015
14016 /* If there's no new locations, and all existing locations are
14017 pending, don't do anything. This optimizes the common case where
14018 all locations are in the same shared library, that was unloaded.
14019 We'd like to retain the location, so that when the library is
14020 loaded again, we don't loose the enabled/disabled status of the
14021 individual locations. */
14022 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
14023 return;
14024
14025 existing_locations = hoist_existing_locations (b, filter_pspace);
14026
14027 for (const auto &sal : sals)
14028 {
14029 struct bp_location *new_loc;
14030
14031 switch_to_program_space_and_thread (sal.pspace);
14032
14033 new_loc = add_location_to_breakpoint (b, &sal);
14034
14035 /* Reparse conditions, they might contain references to the
14036 old symtab. */
14037 if (b->cond_string != NULL)
14038 {
14039 const char *s;
14040
14041 s = b->cond_string;
14042 TRY
14043 {
14044 new_loc->cond = parse_exp_1 (&s, sal.pc,
14045 block_for_pc (sal.pc),
14046 0);
14047 }
14048 CATCH (e, RETURN_MASK_ERROR)
14049 {
14050 warning (_("failed to reevaluate condition "
14051 "for breakpoint %d: %s"),
14052 b->number, e.message);
14053 new_loc->enabled = 0;
14054 }
14055 END_CATCH
14056 }
14057
14058 if (!sals_end.empty ())
14059 {
14060 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
14061
14062 new_loc->length = end - sals[0].pc + 1;
14063 }
14064 }
14065
14066 /* If possible, carry over 'disable' status from existing
14067 breakpoints. */
14068 {
14069 struct bp_location *e = existing_locations;
14070 /* If there are multiple breakpoints with the same function name,
14071 e.g. for inline functions, comparing function names won't work.
14072 Instead compare pc addresses; this is just a heuristic as things
14073 may have moved, but in practice it gives the correct answer
14074 often enough until a better solution is found. */
14075 int have_ambiguous_names = ambiguous_names_p (b->loc);
14076
14077 for (; e; e = e->next)
14078 {
14079 if (!e->enabled && e->function_name)
14080 {
14081 struct bp_location *l = b->loc;
14082 if (have_ambiguous_names)
14083 {
14084 for (; l; l = l->next)
14085 if (breakpoint_locations_match (e, l))
14086 {
14087 l->enabled = 0;
14088 break;
14089 }
14090 }
14091 else
14092 {
14093 for (; l; l = l->next)
14094 if (l->function_name
14095 && strcmp (e->function_name, l->function_name) == 0)
14096 {
14097 l->enabled = 0;
14098 break;
14099 }
14100 }
14101 }
14102 }
14103 }
14104
14105 if (!locations_are_equal (existing_locations, b->loc))
14106 observer_notify_breakpoint_modified (b);
14107 }
14108
14109 /* Find the SaL locations corresponding to the given LOCATION.
14110 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14111
14112 static std::vector<symtab_and_line>
14113 location_to_sals (struct breakpoint *b, struct event_location *location,
14114 struct program_space *search_pspace, int *found)
14115 {
14116 struct gdb_exception exception = exception_none;
14117
14118 gdb_assert (b->ops != NULL);
14119
14120 std::vector<symtab_and_line> sals;
14121
14122 TRY
14123 {
14124 sals = b->ops->decode_location (b, location, search_pspace);
14125 }
14126 CATCH (e, RETURN_MASK_ERROR)
14127 {
14128 int not_found_and_ok = 0;
14129
14130 exception = e;
14131
14132 /* For pending breakpoints, it's expected that parsing will
14133 fail until the right shared library is loaded. User has
14134 already told to create pending breakpoints and don't need
14135 extra messages. If breakpoint is in bp_shlib_disabled
14136 state, then user already saw the message about that
14137 breakpoint being disabled, and don't want to see more
14138 errors. */
14139 if (e.error == NOT_FOUND_ERROR
14140 && (b->condition_not_parsed
14141 || (b->loc != NULL
14142 && search_pspace != NULL
14143 && b->loc->pspace != search_pspace)
14144 || (b->loc && b->loc->shlib_disabled)
14145 || (b->loc && b->loc->pspace->executing_startup)
14146 || b->enable_state == bp_disabled))
14147 not_found_and_ok = 1;
14148
14149 if (!not_found_and_ok)
14150 {
14151 /* We surely don't want to warn about the same breakpoint
14152 10 times. One solution, implemented here, is disable
14153 the breakpoint on error. Another solution would be to
14154 have separate 'warning emitted' flag. Since this
14155 happens only when a binary has changed, I don't know
14156 which approach is better. */
14157 b->enable_state = bp_disabled;
14158 throw_exception (e);
14159 }
14160 }
14161 END_CATCH
14162
14163 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14164 {
14165 for (auto &sal : sals)
14166 resolve_sal_pc (&sal);
14167 if (b->condition_not_parsed && b->extra_string != NULL)
14168 {
14169 char *cond_string, *extra_string;
14170 int thread, task;
14171
14172 find_condition_and_thread (b->extra_string, sals[0].pc,
14173 &cond_string, &thread, &task,
14174 &extra_string);
14175 gdb_assert (b->cond_string == NULL);
14176 if (cond_string)
14177 b->cond_string = cond_string;
14178 b->thread = thread;
14179 b->task = task;
14180 if (extra_string)
14181 {
14182 xfree (b->extra_string);
14183 b->extra_string = extra_string;
14184 }
14185 b->condition_not_parsed = 0;
14186 }
14187
14188 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14189 sals[0] = update_static_tracepoint (b, sals[0]);
14190
14191 *found = 1;
14192 }
14193 else
14194 *found = 0;
14195
14196 return sals;
14197 }
14198
14199 /* The default re_set method, for typical hardware or software
14200 breakpoints. Reevaluate the breakpoint and recreate its
14201 locations. */
14202
14203 static void
14204 breakpoint_re_set_default (struct breakpoint *b)
14205 {
14206 struct program_space *filter_pspace = current_program_space;
14207 std::vector<symtab_and_line> expanded, expanded_end;
14208
14209 int found;
14210 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
14211 filter_pspace, &found);
14212 if (found)
14213 expanded = std::move (sals);
14214
14215 if (b->location_range_end != NULL)
14216 {
14217 std::vector<symtab_and_line> sals_end
14218 = location_to_sals (b, b->location_range_end.get (),
14219 filter_pspace, &found);
14220 if (found)
14221 expanded_end = std::move (sals_end);
14222 }
14223
14224 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14225 }
14226
14227 /* Default method for creating SALs from an address string. It basically
14228 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14229
14230 static void
14231 create_sals_from_location_default (const struct event_location *location,
14232 struct linespec_result *canonical,
14233 enum bptype type_wanted)
14234 {
14235 parse_breakpoint_sals (location, canonical);
14236 }
14237
14238 /* Call create_breakpoints_sal for the given arguments. This is the default
14239 function for the `create_breakpoints_sal' method of
14240 breakpoint_ops. */
14241
14242 static void
14243 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14244 struct linespec_result *canonical,
14245 gdb::unique_xmalloc_ptr<char> cond_string,
14246 gdb::unique_xmalloc_ptr<char> extra_string,
14247 enum bptype type_wanted,
14248 enum bpdisp disposition,
14249 int thread,
14250 int task, int ignore_count,
14251 const struct breakpoint_ops *ops,
14252 int from_tty, int enabled,
14253 int internal, unsigned flags)
14254 {
14255 create_breakpoints_sal (gdbarch, canonical,
14256 std::move (cond_string),
14257 std::move (extra_string),
14258 type_wanted, disposition,
14259 thread, task, ignore_count, ops, from_tty,
14260 enabled, internal, flags);
14261 }
14262
14263 /* Decode the line represented by S by calling decode_line_full. This is the
14264 default function for the `decode_location' method of breakpoint_ops. */
14265
14266 static std::vector<symtab_and_line>
14267 decode_location_default (struct breakpoint *b,
14268 const struct event_location *location,
14269 struct program_space *search_pspace)
14270 {
14271 struct linespec_result canonical;
14272
14273 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14274 (struct symtab *) NULL, 0,
14275 &canonical, multiple_symbols_all,
14276 b->filter);
14277
14278 /* We should get 0 or 1 resulting SALs. */
14279 gdb_assert (canonical.lsals.size () < 2);
14280
14281 if (!canonical.lsals.empty ())
14282 {
14283 const linespec_sals &lsal = canonical.lsals[0];
14284 return std::move (lsal.sals);
14285 }
14286 return {};
14287 }
14288
14289 /* Prepare the global context for a re-set of breakpoint B. */
14290
14291 static struct cleanup *
14292 prepare_re_set_context (struct breakpoint *b)
14293 {
14294 input_radix = b->input_radix;
14295 set_language (b->language);
14296
14297 return make_cleanup (null_cleanup, NULL);
14298 }
14299
14300 /* Reset a breakpoint given it's struct breakpoint * BINT.
14301 The value we return ends up being the return value from catch_errors.
14302 Unused in this case. */
14303
14304 static int
14305 breakpoint_re_set_one (void *bint)
14306 {
14307 /* Get past catch_errs. */
14308 struct breakpoint *b = (struct breakpoint *) bint;
14309 struct cleanup *cleanups;
14310
14311 cleanups = prepare_re_set_context (b);
14312 b->ops->re_set (b);
14313 do_cleanups (cleanups);
14314 return 0;
14315 }
14316
14317 /* Re-set breakpoint locations for the current program space.
14318 Locations bound to other program spaces are left untouched. */
14319
14320 void
14321 breakpoint_re_set (void)
14322 {
14323 struct breakpoint *b, *b_tmp;
14324 enum language save_language;
14325 int save_input_radix;
14326
14327 save_language = current_language->la_language;
14328 save_input_radix = input_radix;
14329
14330 {
14331 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14332
14333 /* Note: we must not try to insert locations until after all
14334 breakpoints have been re-set. Otherwise, e.g., when re-setting
14335 breakpoint 1, we'd insert the locations of breakpoint 2, which
14336 hadn't been re-set yet, and thus may have stale locations. */
14337
14338 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14339 {
14340 /* Format possible error msg. */
14341 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14342 b->number);
14343 struct cleanup *cleanups = make_cleanup (xfree, message);
14344 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14345 do_cleanups (cleanups);
14346 }
14347 set_language (save_language);
14348 input_radix = save_input_radix;
14349
14350 jit_breakpoint_re_set ();
14351 }
14352
14353 create_overlay_event_breakpoint ();
14354 create_longjmp_master_breakpoint ();
14355 create_std_terminate_master_breakpoint ();
14356 create_exception_master_breakpoint ();
14357
14358 /* Now we can insert. */
14359 update_global_location_list (UGLL_MAY_INSERT);
14360 }
14361 \f
14362 /* Reset the thread number of this breakpoint:
14363
14364 - If the breakpoint is for all threads, leave it as-is.
14365 - Else, reset it to the current thread for inferior_ptid. */
14366 void
14367 breakpoint_re_set_thread (struct breakpoint *b)
14368 {
14369 if (b->thread != -1)
14370 {
14371 if (in_thread_list (inferior_ptid))
14372 b->thread = ptid_to_global_thread_id (inferior_ptid);
14373
14374 /* We're being called after following a fork. The new fork is
14375 selected as current, and unless this was a vfork will have a
14376 different program space from the original thread. Reset that
14377 as well. */
14378 b->loc->pspace = current_program_space;
14379 }
14380 }
14381
14382 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14383 If from_tty is nonzero, it prints a message to that effect,
14384 which ends with a period (no newline). */
14385
14386 void
14387 set_ignore_count (int bptnum, int count, int from_tty)
14388 {
14389 struct breakpoint *b;
14390
14391 if (count < 0)
14392 count = 0;
14393
14394 ALL_BREAKPOINTS (b)
14395 if (b->number == bptnum)
14396 {
14397 if (is_tracepoint (b))
14398 {
14399 if (from_tty && count != 0)
14400 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14401 bptnum);
14402 return;
14403 }
14404
14405 b->ignore_count = count;
14406 if (from_tty)
14407 {
14408 if (count == 0)
14409 printf_filtered (_("Will stop next time "
14410 "breakpoint %d is reached."),
14411 bptnum);
14412 else if (count == 1)
14413 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14414 bptnum);
14415 else
14416 printf_filtered (_("Will ignore next %d "
14417 "crossings of breakpoint %d."),
14418 count, bptnum);
14419 }
14420 observer_notify_breakpoint_modified (b);
14421 return;
14422 }
14423
14424 error (_("No breakpoint number %d."), bptnum);
14425 }
14426
14427 /* Command to set ignore-count of breakpoint N to COUNT. */
14428
14429 static void
14430 ignore_command (char *args, int from_tty)
14431 {
14432 char *p = args;
14433 int num;
14434
14435 if (p == 0)
14436 error_no_arg (_("a breakpoint number"));
14437
14438 num = get_number (&p);
14439 if (num == 0)
14440 error (_("bad breakpoint number: '%s'"), args);
14441 if (*p == 0)
14442 error (_("Second argument (specified ignore-count) is missing."));
14443
14444 set_ignore_count (num,
14445 longest_to_int (value_as_long (parse_and_eval (p))),
14446 from_tty);
14447 if (from_tty)
14448 printf_filtered ("\n");
14449 }
14450 \f
14451 /* Call FUNCTION on each of the breakpoints
14452 whose numbers are given in ARGS. */
14453
14454 static void
14455 map_breakpoint_numbers (const char *args,
14456 void (*function) (struct breakpoint *,
14457 void *),
14458 void *data)
14459 {
14460 int num;
14461 struct breakpoint *b, *tmp;
14462
14463 if (args == 0 || *args == '\0')
14464 error_no_arg (_("one or more breakpoint numbers"));
14465
14466 number_or_range_parser parser (args);
14467
14468 while (!parser.finished ())
14469 {
14470 const char *p = parser.cur_tok ();
14471 bool match = false;
14472
14473 num = parser.get_number ();
14474 if (num == 0)
14475 {
14476 warning (_("bad breakpoint number at or near '%s'"), p);
14477 }
14478 else
14479 {
14480 ALL_BREAKPOINTS_SAFE (b, tmp)
14481 if (b->number == num)
14482 {
14483 match = true;
14484 function (b, data);
14485 break;
14486 }
14487 if (!match)
14488 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14489 }
14490 }
14491 }
14492
14493 static struct bp_location *
14494 find_location_by_number (char *number)
14495 {
14496 char *dot = strchr (number, '.');
14497 char *p1;
14498 int bp_num;
14499 int loc_num;
14500 struct breakpoint *b;
14501 struct bp_location *loc;
14502
14503 *dot = '\0';
14504
14505 p1 = number;
14506 bp_num = get_number (&p1);
14507 if (bp_num == 0)
14508 error (_("Bad breakpoint number '%s'"), number);
14509
14510 ALL_BREAKPOINTS (b)
14511 if (b->number == bp_num)
14512 {
14513 break;
14514 }
14515
14516 if (!b || b->number != bp_num)
14517 error (_("Bad breakpoint number '%s'"), number);
14518
14519 p1 = dot+1;
14520 loc_num = get_number (&p1);
14521 if (loc_num == 0)
14522 error (_("Bad breakpoint location number '%s'"), number);
14523
14524 --loc_num;
14525 loc = b->loc;
14526 for (;loc_num && loc; --loc_num, loc = loc->next)
14527 ;
14528 if (!loc)
14529 error (_("Bad breakpoint location number '%s'"), dot+1);
14530
14531 return loc;
14532 }
14533
14534
14535 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14536 If from_tty is nonzero, it prints a message to that effect,
14537 which ends with a period (no newline). */
14538
14539 void
14540 disable_breakpoint (struct breakpoint *bpt)
14541 {
14542 /* Never disable a watchpoint scope breakpoint; we want to
14543 hit them when we leave scope so we can delete both the
14544 watchpoint and its scope breakpoint at that time. */
14545 if (bpt->type == bp_watchpoint_scope)
14546 return;
14547
14548 bpt->enable_state = bp_disabled;
14549
14550 /* Mark breakpoint locations modified. */
14551 mark_breakpoint_modified (bpt);
14552
14553 if (target_supports_enable_disable_tracepoint ()
14554 && current_trace_status ()->running && is_tracepoint (bpt))
14555 {
14556 struct bp_location *location;
14557
14558 for (location = bpt->loc; location; location = location->next)
14559 target_disable_tracepoint (location);
14560 }
14561
14562 update_global_location_list (UGLL_DONT_INSERT);
14563
14564 observer_notify_breakpoint_modified (bpt);
14565 }
14566
14567 /* A callback for iterate_over_related_breakpoints. */
14568
14569 static void
14570 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14571 {
14572 disable_breakpoint (b);
14573 }
14574
14575 /* A callback for map_breakpoint_numbers that calls
14576 disable_breakpoint. */
14577
14578 static void
14579 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14580 {
14581 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14582 }
14583
14584 static void
14585 disable_command (char *args, int from_tty)
14586 {
14587 if (args == 0)
14588 {
14589 struct breakpoint *bpt;
14590
14591 ALL_BREAKPOINTS (bpt)
14592 if (user_breakpoint_p (bpt))
14593 disable_breakpoint (bpt);
14594 }
14595 else
14596 {
14597 char *num = extract_arg (&args);
14598
14599 while (num)
14600 {
14601 if (strchr (num, '.'))
14602 {
14603 struct bp_location *loc = find_location_by_number (num);
14604
14605 if (loc)
14606 {
14607 if (loc->enabled)
14608 {
14609 loc->enabled = 0;
14610 mark_breakpoint_location_modified (loc);
14611 }
14612 if (target_supports_enable_disable_tracepoint ()
14613 && current_trace_status ()->running && loc->owner
14614 && is_tracepoint (loc->owner))
14615 target_disable_tracepoint (loc);
14616 }
14617 update_global_location_list (UGLL_DONT_INSERT);
14618 }
14619 else
14620 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14621 num = extract_arg (&args);
14622 }
14623 }
14624 }
14625
14626 static void
14627 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14628 int count)
14629 {
14630 int target_resources_ok;
14631
14632 if (bpt->type == bp_hardware_breakpoint)
14633 {
14634 int i;
14635 i = hw_breakpoint_used_count ();
14636 target_resources_ok =
14637 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14638 i + 1, 0);
14639 if (target_resources_ok == 0)
14640 error (_("No hardware breakpoint support in the target."));
14641 else if (target_resources_ok < 0)
14642 error (_("Hardware breakpoints used exceeds limit."));
14643 }
14644
14645 if (is_watchpoint (bpt))
14646 {
14647 /* Initialize it just to avoid a GCC false warning. */
14648 enum enable_state orig_enable_state = bp_disabled;
14649
14650 TRY
14651 {
14652 struct watchpoint *w = (struct watchpoint *) bpt;
14653
14654 orig_enable_state = bpt->enable_state;
14655 bpt->enable_state = bp_enabled;
14656 update_watchpoint (w, 1 /* reparse */);
14657 }
14658 CATCH (e, RETURN_MASK_ALL)
14659 {
14660 bpt->enable_state = orig_enable_state;
14661 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14662 bpt->number);
14663 return;
14664 }
14665 END_CATCH
14666 }
14667
14668 bpt->enable_state = bp_enabled;
14669
14670 /* Mark breakpoint locations modified. */
14671 mark_breakpoint_modified (bpt);
14672
14673 if (target_supports_enable_disable_tracepoint ()
14674 && current_trace_status ()->running && is_tracepoint (bpt))
14675 {
14676 struct bp_location *location;
14677
14678 for (location = bpt->loc; location; location = location->next)
14679 target_enable_tracepoint (location);
14680 }
14681
14682 bpt->disposition = disposition;
14683 bpt->enable_count = count;
14684 update_global_location_list (UGLL_MAY_INSERT);
14685
14686 observer_notify_breakpoint_modified (bpt);
14687 }
14688
14689
14690 void
14691 enable_breakpoint (struct breakpoint *bpt)
14692 {
14693 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14694 }
14695
14696 static void
14697 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14698 {
14699 enable_breakpoint (bpt);
14700 }
14701
14702 /* A callback for map_breakpoint_numbers that calls
14703 enable_breakpoint. */
14704
14705 static void
14706 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14707 {
14708 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14709 }
14710
14711 /* The enable command enables the specified breakpoints (or all defined
14712 breakpoints) so they once again become (or continue to be) effective
14713 in stopping the inferior. */
14714
14715 static void
14716 enable_command (char *args, int from_tty)
14717 {
14718 if (args == 0)
14719 {
14720 struct breakpoint *bpt;
14721
14722 ALL_BREAKPOINTS (bpt)
14723 if (user_breakpoint_p (bpt))
14724 enable_breakpoint (bpt);
14725 }
14726 else
14727 {
14728 char *num = extract_arg (&args);
14729
14730 while (num)
14731 {
14732 if (strchr (num, '.'))
14733 {
14734 struct bp_location *loc = find_location_by_number (num);
14735
14736 if (loc)
14737 {
14738 if (!loc->enabled)
14739 {
14740 loc->enabled = 1;
14741 mark_breakpoint_location_modified (loc);
14742 }
14743 if (target_supports_enable_disable_tracepoint ()
14744 && current_trace_status ()->running && loc->owner
14745 && is_tracepoint (loc->owner))
14746 target_enable_tracepoint (loc);
14747 }
14748 update_global_location_list (UGLL_MAY_INSERT);
14749 }
14750 else
14751 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14752 num = extract_arg (&args);
14753 }
14754 }
14755 }
14756
14757 /* This struct packages up disposition data for application to multiple
14758 breakpoints. */
14759
14760 struct disp_data
14761 {
14762 enum bpdisp disp;
14763 int count;
14764 };
14765
14766 static void
14767 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14768 {
14769 struct disp_data disp_data = *(struct disp_data *) arg;
14770
14771 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14772 }
14773
14774 static void
14775 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14776 {
14777 struct disp_data disp = { disp_disable, 1 };
14778
14779 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14780 }
14781
14782 static void
14783 enable_once_command (char *args, int from_tty)
14784 {
14785 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14786 }
14787
14788 static void
14789 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14790 {
14791 struct disp_data disp = { disp_disable, *(int *) countptr };
14792
14793 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14794 }
14795
14796 static void
14797 enable_count_command (char *args, int from_tty)
14798 {
14799 int count;
14800
14801 if (args == NULL)
14802 error_no_arg (_("hit count"));
14803
14804 count = get_number (&args);
14805
14806 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14807 }
14808
14809 static void
14810 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14811 {
14812 struct disp_data disp = { disp_del, 1 };
14813
14814 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14815 }
14816
14817 static void
14818 enable_delete_command (char *args, int from_tty)
14819 {
14820 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14821 }
14822 \f
14823 static void
14824 set_breakpoint_cmd (char *args, int from_tty)
14825 {
14826 }
14827
14828 static void
14829 show_breakpoint_cmd (char *args, int from_tty)
14830 {
14831 }
14832
14833 /* Invalidate last known value of any hardware watchpoint if
14834 the memory which that value represents has been written to by
14835 GDB itself. */
14836
14837 static void
14838 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14839 CORE_ADDR addr, ssize_t len,
14840 const bfd_byte *data)
14841 {
14842 struct breakpoint *bp;
14843
14844 ALL_BREAKPOINTS (bp)
14845 if (bp->enable_state == bp_enabled
14846 && bp->type == bp_hardware_watchpoint)
14847 {
14848 struct watchpoint *wp = (struct watchpoint *) bp;
14849
14850 if (wp->val_valid && wp->val)
14851 {
14852 struct bp_location *loc;
14853
14854 for (loc = bp->loc; loc != NULL; loc = loc->next)
14855 if (loc->loc_type == bp_loc_hardware_watchpoint
14856 && loc->address + loc->length > addr
14857 && addr + len > loc->address)
14858 {
14859 value_free (wp->val);
14860 wp->val = NULL;
14861 wp->val_valid = 0;
14862 }
14863 }
14864 }
14865 }
14866
14867 /* Create and insert a breakpoint for software single step. */
14868
14869 void
14870 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14871 struct address_space *aspace,
14872 CORE_ADDR next_pc)
14873 {
14874 struct thread_info *tp = inferior_thread ();
14875 struct symtab_and_line sal;
14876 CORE_ADDR pc = next_pc;
14877
14878 if (tp->control.single_step_breakpoints == NULL)
14879 {
14880 tp->control.single_step_breakpoints
14881 = new_single_step_breakpoint (tp->global_num, gdbarch);
14882 }
14883
14884 sal = find_pc_line (pc, 0);
14885 sal.pc = pc;
14886 sal.section = find_pc_overlay (pc);
14887 sal.explicit_pc = 1;
14888 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14889
14890 update_global_location_list (UGLL_INSERT);
14891 }
14892
14893 /* Insert single step breakpoints according to the current state. */
14894
14895 int
14896 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14897 {
14898 struct regcache *regcache = get_current_regcache ();
14899 std::vector<CORE_ADDR> next_pcs;
14900
14901 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14902
14903 if (!next_pcs.empty ())
14904 {
14905 struct frame_info *frame = get_current_frame ();
14906 struct address_space *aspace = get_frame_address_space (frame);
14907
14908 for (CORE_ADDR pc : next_pcs)
14909 insert_single_step_breakpoint (gdbarch, aspace, pc);
14910
14911 return 1;
14912 }
14913 else
14914 return 0;
14915 }
14916
14917 /* See breakpoint.h. */
14918
14919 int
14920 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14921 struct address_space *aspace,
14922 CORE_ADDR pc)
14923 {
14924 struct bp_location *loc;
14925
14926 for (loc = bp->loc; loc != NULL; loc = loc->next)
14927 if (loc->inserted
14928 && breakpoint_location_address_match (loc, aspace, pc))
14929 return 1;
14930
14931 return 0;
14932 }
14933
14934 /* Check whether a software single-step breakpoint is inserted at
14935 PC. */
14936
14937 int
14938 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14939 CORE_ADDR pc)
14940 {
14941 struct breakpoint *bpt;
14942
14943 ALL_BREAKPOINTS (bpt)
14944 {
14945 if (bpt->type == bp_single_step
14946 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14947 return 1;
14948 }
14949 return 0;
14950 }
14951
14952 /* Tracepoint-specific operations. */
14953
14954 /* Set tracepoint count to NUM. */
14955 static void
14956 set_tracepoint_count (int num)
14957 {
14958 tracepoint_count = num;
14959 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14960 }
14961
14962 static void
14963 trace_command (char *arg, int from_tty)
14964 {
14965 struct breakpoint_ops *ops;
14966
14967 event_location_up location = string_to_event_location (&arg,
14968 current_language);
14969 if (location != NULL
14970 && event_location_type (location.get ()) == PROBE_LOCATION)
14971 ops = &tracepoint_probe_breakpoint_ops;
14972 else
14973 ops = &tracepoint_breakpoint_ops;
14974
14975 create_breakpoint (get_current_arch (),
14976 location.get (),
14977 NULL, 0, arg, 1 /* parse arg */,
14978 0 /* tempflag */,
14979 bp_tracepoint /* type_wanted */,
14980 0 /* Ignore count */,
14981 pending_break_support,
14982 ops,
14983 from_tty,
14984 1 /* enabled */,
14985 0 /* internal */, 0);
14986 }
14987
14988 static void
14989 ftrace_command (char *arg, int from_tty)
14990 {
14991 event_location_up location = string_to_event_location (&arg,
14992 current_language);
14993 create_breakpoint (get_current_arch (),
14994 location.get (),
14995 NULL, 0, arg, 1 /* parse arg */,
14996 0 /* tempflag */,
14997 bp_fast_tracepoint /* type_wanted */,
14998 0 /* Ignore count */,
14999 pending_break_support,
15000 &tracepoint_breakpoint_ops,
15001 from_tty,
15002 1 /* enabled */,
15003 0 /* internal */, 0);
15004 }
15005
15006 /* strace command implementation. Creates a static tracepoint. */
15007
15008 static void
15009 strace_command (char *arg, int from_tty)
15010 {
15011 struct breakpoint_ops *ops;
15012 event_location_up location;
15013 struct cleanup *back_to;
15014
15015 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15016 or with a normal static tracepoint. */
15017 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15018 {
15019 ops = &strace_marker_breakpoint_ops;
15020 location = new_linespec_location (&arg);
15021 }
15022 else
15023 {
15024 ops = &tracepoint_breakpoint_ops;
15025 location = string_to_event_location (&arg, current_language);
15026 }
15027
15028 create_breakpoint (get_current_arch (),
15029 location.get (),
15030 NULL, 0, arg, 1 /* parse arg */,
15031 0 /* tempflag */,
15032 bp_static_tracepoint /* type_wanted */,
15033 0 /* Ignore count */,
15034 pending_break_support,
15035 ops,
15036 from_tty,
15037 1 /* enabled */,
15038 0 /* internal */, 0);
15039 }
15040
15041 /* Set up a fake reader function that gets command lines from a linked
15042 list that was acquired during tracepoint uploading. */
15043
15044 static struct uploaded_tp *this_utp;
15045 static int next_cmd;
15046
15047 static char *
15048 read_uploaded_action (void)
15049 {
15050 char *rslt;
15051
15052 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15053
15054 next_cmd++;
15055
15056 return rslt;
15057 }
15058
15059 /* Given information about a tracepoint as recorded on a target (which
15060 can be either a live system or a trace file), attempt to create an
15061 equivalent GDB tracepoint. This is not a reliable process, since
15062 the target does not necessarily have all the information used when
15063 the tracepoint was originally defined. */
15064
15065 struct tracepoint *
15066 create_tracepoint_from_upload (struct uploaded_tp *utp)
15067 {
15068 char *addr_str, small_buf[100];
15069 struct tracepoint *tp;
15070
15071 if (utp->at_string)
15072 addr_str = utp->at_string;
15073 else
15074 {
15075 /* In the absence of a source location, fall back to raw
15076 address. Since there is no way to confirm that the address
15077 means the same thing as when the trace was started, warn the
15078 user. */
15079 warning (_("Uploaded tracepoint %d has no "
15080 "source location, using raw address"),
15081 utp->number);
15082 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15083 addr_str = small_buf;
15084 }
15085
15086 /* There's not much we can do with a sequence of bytecodes. */
15087 if (utp->cond && !utp->cond_string)
15088 warning (_("Uploaded tracepoint %d condition "
15089 "has no source form, ignoring it"),
15090 utp->number);
15091
15092 event_location_up location = string_to_event_location (&addr_str,
15093 current_language);
15094 if (!create_breakpoint (get_current_arch (),
15095 location.get (),
15096 utp->cond_string, -1, addr_str,
15097 0 /* parse cond/thread */,
15098 0 /* tempflag */,
15099 utp->type /* type_wanted */,
15100 0 /* Ignore count */,
15101 pending_break_support,
15102 &tracepoint_breakpoint_ops,
15103 0 /* from_tty */,
15104 utp->enabled /* enabled */,
15105 0 /* internal */,
15106 CREATE_BREAKPOINT_FLAGS_INSERTED))
15107 return NULL;
15108
15109 /* Get the tracepoint we just created. */
15110 tp = get_tracepoint (tracepoint_count);
15111 gdb_assert (tp != NULL);
15112
15113 if (utp->pass > 0)
15114 {
15115 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15116 tp->number);
15117
15118 trace_pass_command (small_buf, 0);
15119 }
15120
15121 /* If we have uploaded versions of the original commands, set up a
15122 special-purpose "reader" function and call the usual command line
15123 reader, then pass the result to the breakpoint command-setting
15124 function. */
15125 if (!VEC_empty (char_ptr, utp->cmd_strings))
15126 {
15127 command_line_up cmd_list;
15128
15129 this_utp = utp;
15130 next_cmd = 0;
15131
15132 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15133
15134 breakpoint_set_commands (tp, std::move (cmd_list));
15135 }
15136 else if (!VEC_empty (char_ptr, utp->actions)
15137 || !VEC_empty (char_ptr, utp->step_actions))
15138 warning (_("Uploaded tracepoint %d actions "
15139 "have no source form, ignoring them"),
15140 utp->number);
15141
15142 /* Copy any status information that might be available. */
15143 tp->hit_count = utp->hit_count;
15144 tp->traceframe_usage = utp->traceframe_usage;
15145
15146 return tp;
15147 }
15148
15149 /* Print information on tracepoint number TPNUM_EXP, or all if
15150 omitted. */
15151
15152 static void
15153 info_tracepoints_command (char *args, int from_tty)
15154 {
15155 struct ui_out *uiout = current_uiout;
15156 int num_printed;
15157
15158 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15159
15160 if (num_printed == 0)
15161 {
15162 if (args == NULL || *args == '\0')
15163 uiout->message ("No tracepoints.\n");
15164 else
15165 uiout->message ("No tracepoint matching '%s'.\n", args);
15166 }
15167
15168 default_collect_info ();
15169 }
15170
15171 /* The 'enable trace' command enables tracepoints.
15172 Not supported by all targets. */
15173 static void
15174 enable_trace_command (char *args, int from_tty)
15175 {
15176 enable_command (args, from_tty);
15177 }
15178
15179 /* The 'disable trace' command disables tracepoints.
15180 Not supported by all targets. */
15181 static void
15182 disable_trace_command (char *args, int from_tty)
15183 {
15184 disable_command (args, from_tty);
15185 }
15186
15187 /* Remove a tracepoint (or all if no argument). */
15188 static void
15189 delete_trace_command (char *arg, int from_tty)
15190 {
15191 struct breakpoint *b, *b_tmp;
15192
15193 dont_repeat ();
15194
15195 if (arg == 0)
15196 {
15197 int breaks_to_delete = 0;
15198
15199 /* Delete all breakpoints if no argument.
15200 Do not delete internal or call-dummy breakpoints, these
15201 have to be deleted with an explicit breakpoint number
15202 argument. */
15203 ALL_TRACEPOINTS (b)
15204 if (is_tracepoint (b) && user_breakpoint_p (b))
15205 {
15206 breaks_to_delete = 1;
15207 break;
15208 }
15209
15210 /* Ask user only if there are some breakpoints to delete. */
15211 if (!from_tty
15212 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15213 {
15214 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15215 if (is_tracepoint (b) && user_breakpoint_p (b))
15216 delete_breakpoint (b);
15217 }
15218 }
15219 else
15220 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15221 }
15222
15223 /* Helper function for trace_pass_command. */
15224
15225 static void
15226 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15227 {
15228 tp->pass_count = count;
15229 observer_notify_breakpoint_modified (tp);
15230 if (from_tty)
15231 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15232 tp->number, count);
15233 }
15234
15235 /* Set passcount for tracepoint.
15236
15237 First command argument is passcount, second is tracepoint number.
15238 If tracepoint number omitted, apply to most recently defined.
15239 Also accepts special argument "all". */
15240
15241 static void
15242 trace_pass_command (char *args, int from_tty)
15243 {
15244 struct tracepoint *t1;
15245 unsigned int count;
15246
15247 if (args == 0 || *args == 0)
15248 error (_("passcount command requires an "
15249 "argument (count + optional TP num)"));
15250
15251 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15252
15253 args = skip_spaces (args);
15254 if (*args && strncasecmp (args, "all", 3) == 0)
15255 {
15256 struct breakpoint *b;
15257
15258 args += 3; /* Skip special argument "all". */
15259 if (*args)
15260 error (_("Junk at end of arguments."));
15261
15262 ALL_TRACEPOINTS (b)
15263 {
15264 t1 = (struct tracepoint *) b;
15265 trace_pass_set_count (t1, count, from_tty);
15266 }
15267 }
15268 else if (*args == '\0')
15269 {
15270 t1 = get_tracepoint_by_number (&args, NULL);
15271 if (t1)
15272 trace_pass_set_count (t1, count, from_tty);
15273 }
15274 else
15275 {
15276 number_or_range_parser parser (args);
15277 while (!parser.finished ())
15278 {
15279 t1 = get_tracepoint_by_number (&args, &parser);
15280 if (t1)
15281 trace_pass_set_count (t1, count, from_tty);
15282 }
15283 }
15284 }
15285
15286 struct tracepoint *
15287 get_tracepoint (int num)
15288 {
15289 struct breakpoint *t;
15290
15291 ALL_TRACEPOINTS (t)
15292 if (t->number == num)
15293 return (struct tracepoint *) t;
15294
15295 return NULL;
15296 }
15297
15298 /* Find the tracepoint with the given target-side number (which may be
15299 different from the tracepoint number after disconnecting and
15300 reconnecting). */
15301
15302 struct tracepoint *
15303 get_tracepoint_by_number_on_target (int num)
15304 {
15305 struct breakpoint *b;
15306
15307 ALL_TRACEPOINTS (b)
15308 {
15309 struct tracepoint *t = (struct tracepoint *) b;
15310
15311 if (t->number_on_target == num)
15312 return t;
15313 }
15314
15315 return NULL;
15316 }
15317
15318 /* Utility: parse a tracepoint number and look it up in the list.
15319 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15320 If the argument is missing, the most recent tracepoint
15321 (tracepoint_count) is returned. */
15322
15323 struct tracepoint *
15324 get_tracepoint_by_number (char **arg,
15325 number_or_range_parser *parser)
15326 {
15327 struct breakpoint *t;
15328 int tpnum;
15329 char *instring = arg == NULL ? NULL : *arg;
15330
15331 if (parser != NULL)
15332 {
15333 gdb_assert (!parser->finished ());
15334 tpnum = parser->get_number ();
15335 }
15336 else if (arg == NULL || *arg == NULL || ! **arg)
15337 tpnum = tracepoint_count;
15338 else
15339 tpnum = get_number (arg);
15340
15341 if (tpnum <= 0)
15342 {
15343 if (instring && *instring)
15344 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15345 instring);
15346 else
15347 printf_filtered (_("No previous tracepoint\n"));
15348 return NULL;
15349 }
15350
15351 ALL_TRACEPOINTS (t)
15352 if (t->number == tpnum)
15353 {
15354 return (struct tracepoint *) t;
15355 }
15356
15357 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15358 return NULL;
15359 }
15360
15361 void
15362 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15363 {
15364 if (b->thread != -1)
15365 fprintf_unfiltered (fp, " thread %d", b->thread);
15366
15367 if (b->task != 0)
15368 fprintf_unfiltered (fp, " task %d", b->task);
15369
15370 fprintf_unfiltered (fp, "\n");
15371 }
15372
15373 /* Save information on user settable breakpoints (watchpoints, etc) to
15374 a new script file named FILENAME. If FILTER is non-NULL, call it
15375 on each breakpoint and only include the ones for which it returns
15376 non-zero. */
15377
15378 static void
15379 save_breakpoints (char *filename, int from_tty,
15380 int (*filter) (const struct breakpoint *))
15381 {
15382 struct breakpoint *tp;
15383 int any = 0;
15384 int extra_trace_bits = 0;
15385
15386 if (filename == 0 || *filename == 0)
15387 error (_("Argument required (file name in which to save)"));
15388
15389 /* See if we have anything to save. */
15390 ALL_BREAKPOINTS (tp)
15391 {
15392 /* Skip internal and momentary breakpoints. */
15393 if (!user_breakpoint_p (tp))
15394 continue;
15395
15396 /* If we have a filter, only save the breakpoints it accepts. */
15397 if (filter && !filter (tp))
15398 continue;
15399
15400 any = 1;
15401
15402 if (is_tracepoint (tp))
15403 {
15404 extra_trace_bits = 1;
15405
15406 /* We can stop searching. */
15407 break;
15408 }
15409 }
15410
15411 if (!any)
15412 {
15413 warning (_("Nothing to save."));
15414 return;
15415 }
15416
15417 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15418
15419 stdio_file fp;
15420
15421 if (!fp.open (expanded_filename.get (), "w"))
15422 error (_("Unable to open file '%s' for saving (%s)"),
15423 expanded_filename.get (), safe_strerror (errno));
15424
15425 if (extra_trace_bits)
15426 save_trace_state_variables (&fp);
15427
15428 ALL_BREAKPOINTS (tp)
15429 {
15430 /* Skip internal and momentary breakpoints. */
15431 if (!user_breakpoint_p (tp))
15432 continue;
15433
15434 /* If we have a filter, only save the breakpoints it accepts. */
15435 if (filter && !filter (tp))
15436 continue;
15437
15438 tp->ops->print_recreate (tp, &fp);
15439
15440 /* Note, we can't rely on tp->number for anything, as we can't
15441 assume the recreated breakpoint numbers will match. Use $bpnum
15442 instead. */
15443
15444 if (tp->cond_string)
15445 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15446
15447 if (tp->ignore_count)
15448 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15449
15450 if (tp->type != bp_dprintf && tp->commands)
15451 {
15452 fp.puts (" commands\n");
15453
15454 current_uiout->redirect (&fp);
15455 TRY
15456 {
15457 print_command_lines (current_uiout, tp->commands->commands, 2);
15458 }
15459 CATCH (ex, RETURN_MASK_ALL)
15460 {
15461 current_uiout->redirect (NULL);
15462 throw_exception (ex);
15463 }
15464 END_CATCH
15465
15466 current_uiout->redirect (NULL);
15467 fp.puts (" end\n");
15468 }
15469
15470 if (tp->enable_state == bp_disabled)
15471 fp.puts ("disable $bpnum\n");
15472
15473 /* If this is a multi-location breakpoint, check if the locations
15474 should be individually disabled. Watchpoint locations are
15475 special, and not user visible. */
15476 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15477 {
15478 struct bp_location *loc;
15479 int n = 1;
15480
15481 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15482 if (!loc->enabled)
15483 fp.printf ("disable $bpnum.%d\n", n);
15484 }
15485 }
15486
15487 if (extra_trace_bits && *default_collect)
15488 fp.printf ("set default-collect %s\n", default_collect);
15489
15490 if (from_tty)
15491 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15492 }
15493
15494 /* The `save breakpoints' command. */
15495
15496 static void
15497 save_breakpoints_command (char *args, int from_tty)
15498 {
15499 save_breakpoints (args, from_tty, NULL);
15500 }
15501
15502 /* The `save tracepoints' command. */
15503
15504 static void
15505 save_tracepoints_command (char *args, int from_tty)
15506 {
15507 save_breakpoints (args, from_tty, is_tracepoint);
15508 }
15509
15510 /* Create a vector of all tracepoints. */
15511
15512 VEC(breakpoint_p) *
15513 all_tracepoints (void)
15514 {
15515 VEC(breakpoint_p) *tp_vec = 0;
15516 struct breakpoint *tp;
15517
15518 ALL_TRACEPOINTS (tp)
15519 {
15520 VEC_safe_push (breakpoint_p, tp_vec, tp);
15521 }
15522
15523 return tp_vec;
15524 }
15525
15526 \f
15527 /* This help string is used to consolidate all the help string for specifying
15528 locations used by several commands. */
15529
15530 #define LOCATION_HELP_STRING \
15531 "Linespecs are colon-separated lists of location parameters, such as\n\
15532 source filename, function name, label name, and line number.\n\
15533 Example: To specify the start of a label named \"the_top\" in the\n\
15534 function \"fact\" in the file \"factorial.c\", use\n\
15535 \"factorial.c:fact:the_top\".\n\
15536 \n\
15537 Address locations begin with \"*\" and specify an exact address in the\n\
15538 program. Example: To specify the fourth byte past the start function\n\
15539 \"main\", use \"*main + 4\".\n\
15540 \n\
15541 Explicit locations are similar to linespecs but use an option/argument\n\
15542 syntax to specify location parameters.\n\
15543 Example: To specify the start of the label named \"the_top\" in the\n\
15544 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15545 -function fact -label the_top\".\n"
15546
15547 /* This help string is used for the break, hbreak, tbreak and thbreak
15548 commands. It is defined as a macro to prevent duplication.
15549 COMMAND should be a string constant containing the name of the
15550 command. */
15551
15552 #define BREAK_ARGS_HELP(command) \
15553 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15554 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15555 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15556 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15557 `-probe-dtrace' (for a DTrace probe).\n\
15558 LOCATION may be a linespec, address, or explicit location as described\n\
15559 below.\n\
15560 \n\
15561 With no LOCATION, uses current execution address of the selected\n\
15562 stack frame. This is useful for breaking on return to a stack frame.\n\
15563 \n\
15564 THREADNUM is the number from \"info threads\".\n\
15565 CONDITION is a boolean expression.\n\
15566 \n" LOCATION_HELP_STRING "\n\
15567 Multiple breakpoints at one place are permitted, and useful if their\n\
15568 conditions are different.\n\
15569 \n\
15570 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15571
15572 /* List of subcommands for "catch". */
15573 static struct cmd_list_element *catch_cmdlist;
15574
15575 /* List of subcommands for "tcatch". */
15576 static struct cmd_list_element *tcatch_cmdlist;
15577
15578 void
15579 add_catch_command (const char *name, const char *docstring,
15580 cmd_sfunc_ftype *sfunc,
15581 completer_ftype *completer,
15582 void *user_data_catch,
15583 void *user_data_tcatch)
15584 {
15585 struct cmd_list_element *command;
15586
15587 command = add_cmd (name, class_breakpoint, NULL, docstring,
15588 &catch_cmdlist);
15589 set_cmd_sfunc (command, sfunc);
15590 set_cmd_context (command, user_data_catch);
15591 set_cmd_completer (command, completer);
15592
15593 command = add_cmd (name, class_breakpoint, NULL, docstring,
15594 &tcatch_cmdlist);
15595 set_cmd_sfunc (command, sfunc);
15596 set_cmd_context (command, user_data_tcatch);
15597 set_cmd_completer (command, completer);
15598 }
15599
15600 static void
15601 save_command (char *arg, int from_tty)
15602 {
15603 printf_unfiltered (_("\"save\" must be followed by "
15604 "the name of a save subcommand.\n"));
15605 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15606 }
15607
15608 struct breakpoint *
15609 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15610 void *data)
15611 {
15612 struct breakpoint *b, *b_tmp;
15613
15614 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15615 {
15616 if ((*callback) (b, data))
15617 return b;
15618 }
15619
15620 return NULL;
15621 }
15622
15623 /* Zero if any of the breakpoint's locations could be a location where
15624 functions have been inlined, nonzero otherwise. */
15625
15626 static int
15627 is_non_inline_function (struct breakpoint *b)
15628 {
15629 /* The shared library event breakpoint is set on the address of a
15630 non-inline function. */
15631 if (b->type == bp_shlib_event)
15632 return 1;
15633
15634 return 0;
15635 }
15636
15637 /* Nonzero if the specified PC cannot be a location where functions
15638 have been inlined. */
15639
15640 int
15641 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15642 const struct target_waitstatus *ws)
15643 {
15644 struct breakpoint *b;
15645 struct bp_location *bl;
15646
15647 ALL_BREAKPOINTS (b)
15648 {
15649 if (!is_non_inline_function (b))
15650 continue;
15651
15652 for (bl = b->loc; bl != NULL; bl = bl->next)
15653 {
15654 if (!bl->shlib_disabled
15655 && bpstat_check_location (bl, aspace, pc, ws))
15656 return 1;
15657 }
15658 }
15659
15660 return 0;
15661 }
15662
15663 /* Remove any references to OBJFILE which is going to be freed. */
15664
15665 void
15666 breakpoint_free_objfile (struct objfile *objfile)
15667 {
15668 struct bp_location **locp, *loc;
15669
15670 ALL_BP_LOCATIONS (loc, locp)
15671 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15672 loc->symtab = NULL;
15673 }
15674
15675 void
15676 initialize_breakpoint_ops (void)
15677 {
15678 static int initialized = 0;
15679
15680 struct breakpoint_ops *ops;
15681
15682 if (initialized)
15683 return;
15684 initialized = 1;
15685
15686 /* The breakpoint_ops structure to be inherit by all kinds of
15687 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15688 internal and momentary breakpoints, etc.). */
15689 ops = &bkpt_base_breakpoint_ops;
15690 *ops = base_breakpoint_ops;
15691 ops->re_set = bkpt_re_set;
15692 ops->insert_location = bkpt_insert_location;
15693 ops->remove_location = bkpt_remove_location;
15694 ops->breakpoint_hit = bkpt_breakpoint_hit;
15695 ops->create_sals_from_location = bkpt_create_sals_from_location;
15696 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15697 ops->decode_location = bkpt_decode_location;
15698
15699 /* The breakpoint_ops structure to be used in regular breakpoints. */
15700 ops = &bkpt_breakpoint_ops;
15701 *ops = bkpt_base_breakpoint_ops;
15702 ops->re_set = bkpt_re_set;
15703 ops->resources_needed = bkpt_resources_needed;
15704 ops->print_it = bkpt_print_it;
15705 ops->print_mention = bkpt_print_mention;
15706 ops->print_recreate = bkpt_print_recreate;
15707
15708 /* Ranged breakpoints. */
15709 ops = &ranged_breakpoint_ops;
15710 *ops = bkpt_breakpoint_ops;
15711 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15712 ops->resources_needed = resources_needed_ranged_breakpoint;
15713 ops->print_it = print_it_ranged_breakpoint;
15714 ops->print_one = print_one_ranged_breakpoint;
15715 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15716 ops->print_mention = print_mention_ranged_breakpoint;
15717 ops->print_recreate = print_recreate_ranged_breakpoint;
15718
15719 /* Internal breakpoints. */
15720 ops = &internal_breakpoint_ops;
15721 *ops = bkpt_base_breakpoint_ops;
15722 ops->re_set = internal_bkpt_re_set;
15723 ops->check_status = internal_bkpt_check_status;
15724 ops->print_it = internal_bkpt_print_it;
15725 ops->print_mention = internal_bkpt_print_mention;
15726
15727 /* Momentary breakpoints. */
15728 ops = &momentary_breakpoint_ops;
15729 *ops = bkpt_base_breakpoint_ops;
15730 ops->re_set = momentary_bkpt_re_set;
15731 ops->check_status = momentary_bkpt_check_status;
15732 ops->print_it = momentary_bkpt_print_it;
15733 ops->print_mention = momentary_bkpt_print_mention;
15734
15735 /* Probe breakpoints. */
15736 ops = &bkpt_probe_breakpoint_ops;
15737 *ops = bkpt_breakpoint_ops;
15738 ops->insert_location = bkpt_probe_insert_location;
15739 ops->remove_location = bkpt_probe_remove_location;
15740 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15741 ops->decode_location = bkpt_probe_decode_location;
15742
15743 /* Watchpoints. */
15744 ops = &watchpoint_breakpoint_ops;
15745 *ops = base_breakpoint_ops;
15746 ops->re_set = re_set_watchpoint;
15747 ops->insert_location = insert_watchpoint;
15748 ops->remove_location = remove_watchpoint;
15749 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15750 ops->check_status = check_status_watchpoint;
15751 ops->resources_needed = resources_needed_watchpoint;
15752 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15753 ops->print_it = print_it_watchpoint;
15754 ops->print_mention = print_mention_watchpoint;
15755 ops->print_recreate = print_recreate_watchpoint;
15756 ops->explains_signal = explains_signal_watchpoint;
15757
15758 /* Masked watchpoints. */
15759 ops = &masked_watchpoint_breakpoint_ops;
15760 *ops = watchpoint_breakpoint_ops;
15761 ops->insert_location = insert_masked_watchpoint;
15762 ops->remove_location = remove_masked_watchpoint;
15763 ops->resources_needed = resources_needed_masked_watchpoint;
15764 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15765 ops->print_it = print_it_masked_watchpoint;
15766 ops->print_one_detail = print_one_detail_masked_watchpoint;
15767 ops->print_mention = print_mention_masked_watchpoint;
15768 ops->print_recreate = print_recreate_masked_watchpoint;
15769
15770 /* Tracepoints. */
15771 ops = &tracepoint_breakpoint_ops;
15772 *ops = base_breakpoint_ops;
15773 ops->re_set = tracepoint_re_set;
15774 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15775 ops->print_one_detail = tracepoint_print_one_detail;
15776 ops->print_mention = tracepoint_print_mention;
15777 ops->print_recreate = tracepoint_print_recreate;
15778 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15779 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15780 ops->decode_location = tracepoint_decode_location;
15781
15782 /* Probe tracepoints. */
15783 ops = &tracepoint_probe_breakpoint_ops;
15784 *ops = tracepoint_breakpoint_ops;
15785 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15786 ops->decode_location = tracepoint_probe_decode_location;
15787
15788 /* Static tracepoints with marker (`-m'). */
15789 ops = &strace_marker_breakpoint_ops;
15790 *ops = tracepoint_breakpoint_ops;
15791 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15792 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15793 ops->decode_location = strace_marker_decode_location;
15794
15795 /* Fork catchpoints. */
15796 ops = &catch_fork_breakpoint_ops;
15797 *ops = base_breakpoint_ops;
15798 ops->insert_location = insert_catch_fork;
15799 ops->remove_location = remove_catch_fork;
15800 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15801 ops->print_it = print_it_catch_fork;
15802 ops->print_one = print_one_catch_fork;
15803 ops->print_mention = print_mention_catch_fork;
15804 ops->print_recreate = print_recreate_catch_fork;
15805
15806 /* Vfork catchpoints. */
15807 ops = &catch_vfork_breakpoint_ops;
15808 *ops = base_breakpoint_ops;
15809 ops->insert_location = insert_catch_vfork;
15810 ops->remove_location = remove_catch_vfork;
15811 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15812 ops->print_it = print_it_catch_vfork;
15813 ops->print_one = print_one_catch_vfork;
15814 ops->print_mention = print_mention_catch_vfork;
15815 ops->print_recreate = print_recreate_catch_vfork;
15816
15817 /* Exec catchpoints. */
15818 ops = &catch_exec_breakpoint_ops;
15819 *ops = base_breakpoint_ops;
15820 ops->insert_location = insert_catch_exec;
15821 ops->remove_location = remove_catch_exec;
15822 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15823 ops->print_it = print_it_catch_exec;
15824 ops->print_one = print_one_catch_exec;
15825 ops->print_mention = print_mention_catch_exec;
15826 ops->print_recreate = print_recreate_catch_exec;
15827
15828 /* Solib-related catchpoints. */
15829 ops = &catch_solib_breakpoint_ops;
15830 *ops = base_breakpoint_ops;
15831 ops->insert_location = insert_catch_solib;
15832 ops->remove_location = remove_catch_solib;
15833 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15834 ops->check_status = check_status_catch_solib;
15835 ops->print_it = print_it_catch_solib;
15836 ops->print_one = print_one_catch_solib;
15837 ops->print_mention = print_mention_catch_solib;
15838 ops->print_recreate = print_recreate_catch_solib;
15839
15840 ops = &dprintf_breakpoint_ops;
15841 *ops = bkpt_base_breakpoint_ops;
15842 ops->re_set = dprintf_re_set;
15843 ops->resources_needed = bkpt_resources_needed;
15844 ops->print_it = bkpt_print_it;
15845 ops->print_mention = bkpt_print_mention;
15846 ops->print_recreate = dprintf_print_recreate;
15847 ops->after_condition_true = dprintf_after_condition_true;
15848 ops->breakpoint_hit = dprintf_breakpoint_hit;
15849 }
15850
15851 /* Chain containing all defined "enable breakpoint" subcommands. */
15852
15853 static struct cmd_list_element *enablebreaklist = NULL;
15854
15855 void
15856 _initialize_breakpoint (void)
15857 {
15858 struct cmd_list_element *c;
15859
15860 initialize_breakpoint_ops ();
15861
15862 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15863 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15864 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15865
15866 breakpoint_objfile_key
15867 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15868
15869 breakpoint_chain = 0;
15870 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15871 before a breakpoint is set. */
15872 breakpoint_count = 0;
15873
15874 tracepoint_count = 0;
15875
15876 add_com ("ignore", class_breakpoint, ignore_command, _("\
15877 Set ignore-count of breakpoint number N to COUNT.\n\
15878 Usage is `ignore N COUNT'."));
15879
15880 add_com ("commands", class_breakpoint, commands_command, _("\
15881 Set commands to be executed when the given breakpoints are hit.\n\
15882 Give a space-separated breakpoint list as argument after \"commands\".\n\
15883 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15884 (e.g. `5-7').\n\
15885 With no argument, the targeted breakpoint is the last one set.\n\
15886 The commands themselves follow starting on the next line.\n\
15887 Type a line containing \"end\" to indicate the end of them.\n\
15888 Give \"silent\" as the first line to make the breakpoint silent;\n\
15889 then no output is printed when it is hit, except what the commands print."));
15890
15891 c = add_com ("condition", class_breakpoint, condition_command, _("\
15892 Specify breakpoint number N to break only if COND is true.\n\
15893 Usage is `condition N COND', where N is an integer and COND is an\n\
15894 expression to be evaluated whenever breakpoint N is reached."));
15895 set_cmd_completer (c, condition_completer);
15896
15897 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15898 Set a temporary breakpoint.\n\
15899 Like \"break\" except the breakpoint is only temporary,\n\
15900 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15901 by using \"enable delete\" on the breakpoint number.\n\
15902 \n"
15903 BREAK_ARGS_HELP ("tbreak")));
15904 set_cmd_completer (c, location_completer);
15905
15906 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15907 Set a hardware assisted breakpoint.\n\
15908 Like \"break\" except the breakpoint requires hardware support,\n\
15909 some target hardware may not have this support.\n\
15910 \n"
15911 BREAK_ARGS_HELP ("hbreak")));
15912 set_cmd_completer (c, location_completer);
15913
15914 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15915 Set a temporary hardware assisted breakpoint.\n\
15916 Like \"hbreak\" except the breakpoint is only temporary,\n\
15917 so it will be deleted when hit.\n\
15918 \n"
15919 BREAK_ARGS_HELP ("thbreak")));
15920 set_cmd_completer (c, location_completer);
15921
15922 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15923 Enable some breakpoints.\n\
15924 Give breakpoint numbers (separated by spaces) as arguments.\n\
15925 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15926 This is used to cancel the effect of the \"disable\" command.\n\
15927 With a subcommand you can enable temporarily."),
15928 &enablelist, "enable ", 1, &cmdlist);
15929
15930 add_com_alias ("en", "enable", class_breakpoint, 1);
15931
15932 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15933 Enable some breakpoints.\n\
15934 Give breakpoint numbers (separated by spaces) as arguments.\n\
15935 This is used to cancel the effect of the \"disable\" command.\n\
15936 May be abbreviated to simply \"enable\".\n"),
15937 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15938
15939 add_cmd ("once", no_class, enable_once_command, _("\
15940 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15941 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15942 &enablebreaklist);
15943
15944 add_cmd ("delete", no_class, enable_delete_command, _("\
15945 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15946 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15947 &enablebreaklist);
15948
15949 add_cmd ("count", no_class, enable_count_command, _("\
15950 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15951 If a breakpoint is hit while enabled in this fashion,\n\
15952 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15953 &enablebreaklist);
15954
15955 add_cmd ("delete", no_class, enable_delete_command, _("\
15956 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15957 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15958 &enablelist);
15959
15960 add_cmd ("once", no_class, enable_once_command, _("\
15961 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15962 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15963 &enablelist);
15964
15965 add_cmd ("count", no_class, enable_count_command, _("\
15966 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15967 If a breakpoint is hit while enabled in this fashion,\n\
15968 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15969 &enablelist);
15970
15971 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15972 Disable some breakpoints.\n\
15973 Arguments are breakpoint numbers with spaces in between.\n\
15974 To disable all breakpoints, give no argument.\n\
15975 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15976 &disablelist, "disable ", 1, &cmdlist);
15977 add_com_alias ("dis", "disable", class_breakpoint, 1);
15978 add_com_alias ("disa", "disable", class_breakpoint, 1);
15979
15980 add_cmd ("breakpoints", class_alias, disable_command, _("\
15981 Disable some breakpoints.\n\
15982 Arguments are breakpoint numbers with spaces in between.\n\
15983 To disable all breakpoints, give no argument.\n\
15984 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15985 This command may be abbreviated \"disable\"."),
15986 &disablelist);
15987
15988 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15989 Delete some breakpoints or auto-display expressions.\n\
15990 Arguments are breakpoint numbers with spaces in between.\n\
15991 To delete all breakpoints, give no argument.\n\
15992 \n\
15993 Also a prefix command for deletion of other GDB objects.\n\
15994 The \"unset\" command is also an alias for \"delete\"."),
15995 &deletelist, "delete ", 1, &cmdlist);
15996 add_com_alias ("d", "delete", class_breakpoint, 1);
15997 add_com_alias ("del", "delete", class_breakpoint, 1);
15998
15999 add_cmd ("breakpoints", class_alias, delete_command, _("\
16000 Delete some breakpoints or auto-display expressions.\n\
16001 Arguments are breakpoint numbers with spaces in between.\n\
16002 To delete all breakpoints, give no argument.\n\
16003 This command may be abbreviated \"delete\"."),
16004 &deletelist);
16005
16006 add_com ("clear", class_breakpoint, clear_command, _("\
16007 Clear breakpoint at specified location.\n\
16008 Argument may be a linespec, explicit, or address location as described below.\n\
16009 \n\
16010 With no argument, clears all breakpoints in the line that the selected frame\n\
16011 is executing in.\n"
16012 "\n" LOCATION_HELP_STRING "\n\
16013 See also the \"delete\" command which clears breakpoints by number."));
16014 add_com_alias ("cl", "clear", class_breakpoint, 1);
16015
16016 c = add_com ("break", class_breakpoint, break_command, _("\
16017 Set breakpoint at specified location.\n"
16018 BREAK_ARGS_HELP ("break")));
16019 set_cmd_completer (c, location_completer);
16020
16021 add_com_alias ("b", "break", class_run, 1);
16022 add_com_alias ("br", "break", class_run, 1);
16023 add_com_alias ("bre", "break", class_run, 1);
16024 add_com_alias ("brea", "break", class_run, 1);
16025
16026 if (dbx_commands)
16027 {
16028 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16029 Break in function/address or break at a line in the current file."),
16030 &stoplist, "stop ", 1, &cmdlist);
16031 add_cmd ("in", class_breakpoint, stopin_command,
16032 _("Break in function or address."), &stoplist);
16033 add_cmd ("at", class_breakpoint, stopat_command,
16034 _("Break at a line in the current file."), &stoplist);
16035 add_com ("status", class_info, info_breakpoints_command, _("\
16036 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16037 The \"Type\" column indicates one of:\n\
16038 \tbreakpoint - normal breakpoint\n\
16039 \twatchpoint - watchpoint\n\
16040 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16041 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16042 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16043 address and file/line number respectively.\n\
16044 \n\
16045 Convenience variable \"$_\" and default examine address for \"x\"\n\
16046 are set to the address of the last breakpoint listed unless the command\n\
16047 is prefixed with \"server \".\n\n\
16048 Convenience variable \"$bpnum\" contains the number of the last\n\
16049 breakpoint set."));
16050 }
16051
16052 add_info ("breakpoints", info_breakpoints_command, _("\
16053 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16054 The \"Type\" column indicates one of:\n\
16055 \tbreakpoint - normal breakpoint\n\
16056 \twatchpoint - watchpoint\n\
16057 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16058 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16059 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16060 address and file/line number respectively.\n\
16061 \n\
16062 Convenience variable \"$_\" and default examine address for \"x\"\n\
16063 are set to the address of the last breakpoint listed unless the command\n\
16064 is prefixed with \"server \".\n\n\
16065 Convenience variable \"$bpnum\" contains the number of the last\n\
16066 breakpoint set."));
16067
16068 add_info_alias ("b", "breakpoints", 1);
16069
16070 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16071 Status of all breakpoints, or breakpoint number NUMBER.\n\
16072 The \"Type\" column indicates one of:\n\
16073 \tbreakpoint - normal breakpoint\n\
16074 \twatchpoint - watchpoint\n\
16075 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16076 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16077 \tuntil - internal breakpoint used by the \"until\" command\n\
16078 \tfinish - internal breakpoint used by the \"finish\" command\n\
16079 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16080 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16081 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16082 address and file/line number respectively.\n\
16083 \n\
16084 Convenience variable \"$_\" and default examine address for \"x\"\n\
16085 are set to the address of the last breakpoint listed unless the command\n\
16086 is prefixed with \"server \".\n\n\
16087 Convenience variable \"$bpnum\" contains the number of the last\n\
16088 breakpoint set."),
16089 &maintenanceinfolist);
16090
16091 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16092 Set catchpoints to catch events."),
16093 &catch_cmdlist, "catch ",
16094 0/*allow-unknown*/, &cmdlist);
16095
16096 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16097 Set temporary catchpoints to catch events."),
16098 &tcatch_cmdlist, "tcatch ",
16099 0/*allow-unknown*/, &cmdlist);
16100
16101 add_catch_command ("fork", _("Catch calls to fork."),
16102 catch_fork_command_1,
16103 NULL,
16104 (void *) (uintptr_t) catch_fork_permanent,
16105 (void *) (uintptr_t) catch_fork_temporary);
16106 add_catch_command ("vfork", _("Catch calls to vfork."),
16107 catch_fork_command_1,
16108 NULL,
16109 (void *) (uintptr_t) catch_vfork_permanent,
16110 (void *) (uintptr_t) catch_vfork_temporary);
16111 add_catch_command ("exec", _("Catch calls to exec."),
16112 catch_exec_command_1,
16113 NULL,
16114 CATCH_PERMANENT,
16115 CATCH_TEMPORARY);
16116 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16117 Usage: catch load [REGEX]\n\
16118 If REGEX is given, only stop for libraries matching the regular expression."),
16119 catch_load_command_1,
16120 NULL,
16121 CATCH_PERMANENT,
16122 CATCH_TEMPORARY);
16123 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16124 Usage: catch unload [REGEX]\n\
16125 If REGEX is given, only stop for libraries matching the regular expression."),
16126 catch_unload_command_1,
16127 NULL,
16128 CATCH_PERMANENT,
16129 CATCH_TEMPORARY);
16130
16131 c = add_com ("watch", class_breakpoint, watch_command, _("\
16132 Set a watchpoint for an expression.\n\
16133 Usage: watch [-l|-location] EXPRESSION\n\
16134 A watchpoint stops execution of your program whenever the value of\n\
16135 an expression changes.\n\
16136 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16137 the memory to which it refers."));
16138 set_cmd_completer (c, expression_completer);
16139
16140 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16141 Set a read watchpoint for an expression.\n\
16142 Usage: rwatch [-l|-location] EXPRESSION\n\
16143 A watchpoint stops execution of your program whenever the value of\n\
16144 an expression is read.\n\
16145 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16146 the memory to which it refers."));
16147 set_cmd_completer (c, expression_completer);
16148
16149 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16150 Set a watchpoint for an expression.\n\
16151 Usage: awatch [-l|-location] EXPRESSION\n\
16152 A watchpoint stops execution of your program whenever the value of\n\
16153 an expression is either read or written.\n\
16154 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16155 the memory to which it refers."));
16156 set_cmd_completer (c, expression_completer);
16157
16158 add_info ("watchpoints", info_watchpoints_command, _("\
16159 Status of specified watchpoints (all watchpoints if no argument)."));
16160
16161 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16162 respond to changes - contrary to the description. */
16163 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16164 &can_use_hw_watchpoints, _("\
16165 Set debugger's willingness to use watchpoint hardware."), _("\
16166 Show debugger's willingness to use watchpoint hardware."), _("\
16167 If zero, gdb will not use hardware for new watchpoints, even if\n\
16168 such is available. (However, any hardware watchpoints that were\n\
16169 created before setting this to nonzero, will continue to use watchpoint\n\
16170 hardware.)"),
16171 NULL,
16172 show_can_use_hw_watchpoints,
16173 &setlist, &showlist);
16174
16175 can_use_hw_watchpoints = 1;
16176
16177 /* Tracepoint manipulation commands. */
16178
16179 c = add_com ("trace", class_breakpoint, trace_command, _("\
16180 Set a tracepoint at specified location.\n\
16181 \n"
16182 BREAK_ARGS_HELP ("trace") "\n\
16183 Do \"help tracepoints\" for info on other tracepoint commands."));
16184 set_cmd_completer (c, location_completer);
16185
16186 add_com_alias ("tp", "trace", class_alias, 0);
16187 add_com_alias ("tr", "trace", class_alias, 1);
16188 add_com_alias ("tra", "trace", class_alias, 1);
16189 add_com_alias ("trac", "trace", class_alias, 1);
16190
16191 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16192 Set a fast tracepoint at specified location.\n\
16193 \n"
16194 BREAK_ARGS_HELP ("ftrace") "\n\
16195 Do \"help tracepoints\" for info on other tracepoint commands."));
16196 set_cmd_completer (c, location_completer);
16197
16198 c = add_com ("strace", class_breakpoint, strace_command, _("\
16199 Set a static tracepoint at location or marker.\n\
16200 \n\
16201 strace [LOCATION] [if CONDITION]\n\
16202 LOCATION may be a linespec, explicit, or address location (described below) \n\
16203 or -m MARKER_ID.\n\n\
16204 If a marker id is specified, probe the marker with that name. With\n\
16205 no LOCATION, uses current execution address of the selected stack frame.\n\
16206 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16207 This collects arbitrary user data passed in the probe point call to the\n\
16208 tracing library. You can inspect it when analyzing the trace buffer,\n\
16209 by printing the $_sdata variable like any other convenience variable.\n\
16210 \n\
16211 CONDITION is a boolean expression.\n\
16212 \n" LOCATION_HELP_STRING "\n\
16213 Multiple tracepoints at one place are permitted, and useful if their\n\
16214 conditions are different.\n\
16215 \n\
16216 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16217 Do \"help tracepoints\" for info on other tracepoint commands."));
16218 set_cmd_completer (c, location_completer);
16219
16220 add_info ("tracepoints", info_tracepoints_command, _("\
16221 Status of specified tracepoints (all tracepoints if no argument).\n\
16222 Convenience variable \"$tpnum\" contains the number of the\n\
16223 last tracepoint set."));
16224
16225 add_info_alias ("tp", "tracepoints", 1);
16226
16227 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16228 Delete specified tracepoints.\n\
16229 Arguments are tracepoint numbers, separated by spaces.\n\
16230 No argument means delete all tracepoints."),
16231 &deletelist);
16232 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16233
16234 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16235 Disable specified tracepoints.\n\
16236 Arguments are tracepoint numbers, separated by spaces.\n\
16237 No argument means disable all tracepoints."),
16238 &disablelist);
16239 deprecate_cmd (c, "disable");
16240
16241 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16242 Enable specified tracepoints.\n\
16243 Arguments are tracepoint numbers, separated by spaces.\n\
16244 No argument means enable all tracepoints."),
16245 &enablelist);
16246 deprecate_cmd (c, "enable");
16247
16248 add_com ("passcount", class_trace, trace_pass_command, _("\
16249 Set the passcount for a tracepoint.\n\
16250 The trace will end when the tracepoint has been passed 'count' times.\n\
16251 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16252 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16253
16254 add_prefix_cmd ("save", class_breakpoint, save_command,
16255 _("Save breakpoint definitions as a script."),
16256 &save_cmdlist, "save ",
16257 0/*allow-unknown*/, &cmdlist);
16258
16259 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16260 Save current breakpoint definitions as a script.\n\
16261 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16262 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16263 session to restore them."),
16264 &save_cmdlist);
16265 set_cmd_completer (c, filename_completer);
16266
16267 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16268 Save current tracepoint definitions as a script.\n\
16269 Use the 'source' command in another debug session to restore them."),
16270 &save_cmdlist);
16271 set_cmd_completer (c, filename_completer);
16272
16273 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16274 deprecate_cmd (c, "save tracepoints");
16275
16276 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16277 Breakpoint specific settings\n\
16278 Configure various breakpoint-specific variables such as\n\
16279 pending breakpoint behavior"),
16280 &breakpoint_set_cmdlist, "set breakpoint ",
16281 0/*allow-unknown*/, &setlist);
16282 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16283 Breakpoint specific settings\n\
16284 Configure various breakpoint-specific variables such as\n\
16285 pending breakpoint behavior"),
16286 &breakpoint_show_cmdlist, "show breakpoint ",
16287 0/*allow-unknown*/, &showlist);
16288
16289 add_setshow_auto_boolean_cmd ("pending", no_class,
16290 &pending_break_support, _("\
16291 Set debugger's behavior regarding pending breakpoints."), _("\
16292 Show debugger's behavior regarding pending breakpoints."), _("\
16293 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16294 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16295 an error. If auto, an unrecognized breakpoint location results in a\n\
16296 user-query to see if a pending breakpoint should be created."),
16297 NULL,
16298 show_pending_break_support,
16299 &breakpoint_set_cmdlist,
16300 &breakpoint_show_cmdlist);
16301
16302 pending_break_support = AUTO_BOOLEAN_AUTO;
16303
16304 add_setshow_boolean_cmd ("auto-hw", no_class,
16305 &automatic_hardware_breakpoints, _("\
16306 Set automatic usage of hardware breakpoints."), _("\
16307 Show automatic usage of hardware breakpoints."), _("\
16308 If set, the debugger will automatically use hardware breakpoints for\n\
16309 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16310 a warning will be emitted for such breakpoints."),
16311 NULL,
16312 show_automatic_hardware_breakpoints,
16313 &breakpoint_set_cmdlist,
16314 &breakpoint_show_cmdlist);
16315
16316 add_setshow_boolean_cmd ("always-inserted", class_support,
16317 &always_inserted_mode, _("\
16318 Set mode for inserting breakpoints."), _("\
16319 Show mode for inserting breakpoints."), _("\
16320 When this mode is on, breakpoints are inserted immediately as soon as\n\
16321 they're created, kept inserted even when execution stops, and removed\n\
16322 only when the user deletes them. When this mode is off (the default),\n\
16323 breakpoints are inserted only when execution continues, and removed\n\
16324 when execution stops."),
16325 NULL,
16326 &show_always_inserted_mode,
16327 &breakpoint_set_cmdlist,
16328 &breakpoint_show_cmdlist);
16329
16330 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16331 condition_evaluation_enums,
16332 &condition_evaluation_mode_1, _("\
16333 Set mode of breakpoint condition evaluation."), _("\
16334 Show mode of breakpoint condition evaluation."), _("\
16335 When this is set to \"host\", breakpoint conditions will be\n\
16336 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16337 breakpoint conditions will be downloaded to the target (if the target\n\
16338 supports such feature) and conditions will be evaluated on the target's side.\n\
16339 If this is set to \"auto\" (default), this will be automatically set to\n\
16340 \"target\" if it supports condition evaluation, otherwise it will\n\
16341 be set to \"gdb\""),
16342 &set_condition_evaluation_mode,
16343 &show_condition_evaluation_mode,
16344 &breakpoint_set_cmdlist,
16345 &breakpoint_show_cmdlist);
16346
16347 add_com ("break-range", class_breakpoint, break_range_command, _("\
16348 Set a breakpoint for an address range.\n\
16349 break-range START-LOCATION, END-LOCATION\n\
16350 where START-LOCATION and END-LOCATION can be one of the following:\n\
16351 LINENUM, for that line in the current file,\n\
16352 FILE:LINENUM, for that line in that file,\n\
16353 +OFFSET, for that number of lines after the current line\n\
16354 or the start of the range\n\
16355 FUNCTION, for the first line in that function,\n\
16356 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16357 *ADDRESS, for the instruction at that address.\n\
16358 \n\
16359 The breakpoint will stop execution of the inferior whenever it executes\n\
16360 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16361 range (including START-LOCATION and END-LOCATION)."));
16362
16363 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16364 Set a dynamic printf at specified location.\n\
16365 dprintf location,format string,arg1,arg2,...\n\
16366 location may be a linespec, explicit, or address location.\n"
16367 "\n" LOCATION_HELP_STRING));
16368 set_cmd_completer (c, location_completer);
16369
16370 add_setshow_enum_cmd ("dprintf-style", class_support,
16371 dprintf_style_enums, &dprintf_style, _("\
16372 Set the style of usage for dynamic printf."), _("\
16373 Show the style of usage for dynamic printf."), _("\
16374 This setting chooses how GDB will do a dynamic printf.\n\
16375 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16376 console, as with the \"printf\" command.\n\
16377 If the value is \"call\", the print is done by calling a function in your\n\
16378 program; by default printf(), but you can choose a different function or\n\
16379 output stream by setting dprintf-function and dprintf-channel."),
16380 update_dprintf_commands, NULL,
16381 &setlist, &showlist);
16382
16383 dprintf_function = xstrdup ("printf");
16384 add_setshow_string_cmd ("dprintf-function", class_support,
16385 &dprintf_function, _("\
16386 Set the function to use for dynamic printf"), _("\
16387 Show the function to use for dynamic printf"), NULL,
16388 update_dprintf_commands, NULL,
16389 &setlist, &showlist);
16390
16391 dprintf_channel = xstrdup ("");
16392 add_setshow_string_cmd ("dprintf-channel", class_support,
16393 &dprintf_channel, _("\
16394 Set the channel to use for dynamic printf"), _("\
16395 Show the channel to use for dynamic printf"), NULL,
16396 update_dprintf_commands, NULL,
16397 &setlist, &showlist);
16398
16399 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16400 &disconnected_dprintf, _("\
16401 Set whether dprintf continues after GDB disconnects."), _("\
16402 Show whether dprintf continues after GDB disconnects."), _("\
16403 Use this to let dprintf commands continue to hit and produce output\n\
16404 even if GDB disconnects or detaches from the target."),
16405 NULL,
16406 NULL,
16407 &setlist, &showlist);
16408
16409 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16410 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16411 (target agent only) This is useful for formatted output in user-defined commands."));
16412
16413 automatic_hardware_breakpoints = 1;
16414
16415 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16416 observer_attach_thread_exit (remove_threaded_breakpoints);
16417 }
This page took 0.537522 seconds and 5 git commands to generate.