gdb:
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2012 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 "gdbthread.h"
36 #include "target.h"
37 #include "language.h"
38 #include "gdb_string.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 "gdb_assert.h"
51 #include "block.h"
52 #include "solib.h"
53 #include "solist.h"
54 #include "observer.h"
55 #include "exceptions.h"
56 #include "memattr.h"
57 #include "ada-lang.h"
58 #include "top.h"
59 #include "valprint.h"
60 #include "jit.h"
61 #include "xml-syscall.h"
62 #include "parser-defs.h"
63 #include "cli/cli-utils.h"
64 #include "continuations.h"
65 #include "stack.h"
66 #include "skip.h"
67 #include "gdb_regex.h"
68 #include "ax-gdb.h"
69
70 /* readline include files */
71 #include "readline/readline.h"
72 #include "readline/history.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "python/python.h"
79
80 /* Prototypes for local functions. */
81
82 static void enable_delete_command (char *, int);
83
84 static void enable_once_command (char *, int);
85
86 static void enable_count_command (char *, int);
87
88 static void disable_command (char *, int);
89
90 static void enable_command (char *, int);
91
92 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
93 void *),
94 void *);
95
96 static void ignore_command (char *, int);
97
98 static int breakpoint_re_set_one (void *);
99
100 static void breakpoint_re_set_default (struct breakpoint *);
101
102 static void create_sals_from_address_default (char **,
103 struct linespec_result *,
104 enum bptype, char *,
105 char **);
106
107 static void create_breakpoints_sal_default (struct gdbarch *,
108 struct linespec_result *,
109 struct linespec_sals *,
110 char *, enum bptype,
111 enum bpdisp, int, int,
112 int,
113 const struct breakpoint_ops *,
114 int, int, int, unsigned);
115
116 static void decode_linespec_default (struct breakpoint *, char **,
117 struct symtabs_and_lines *);
118
119 static void clear_command (char *, int);
120
121 static void catch_command (char *, int);
122
123 static int can_use_hardware_watchpoint (struct value *);
124
125 static void break_command_1 (char *, int, int);
126
127 static void mention (struct breakpoint *);
128
129 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
130 enum bptype,
131 const struct breakpoint_ops *);
132 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
133 const struct symtab_and_line *);
134
135 /* This function is used in gdbtk sources and thus can not be made
136 static. */
137 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
138 struct symtab_and_line,
139 enum bptype,
140 const struct breakpoint_ops *);
141
142 static struct breakpoint *
143 momentary_breakpoint_from_master (struct breakpoint *orig,
144 enum bptype type,
145 const struct breakpoint_ops *ops);
146
147 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
148
149 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
150 CORE_ADDR bpaddr,
151 enum bptype bptype);
152
153 static void describe_other_breakpoints (struct gdbarch *,
154 struct program_space *, CORE_ADDR,
155 struct obj_section *, int);
156
157 static int breakpoint_address_match (struct address_space *aspace1,
158 CORE_ADDR addr1,
159 struct address_space *aspace2,
160 CORE_ADDR addr2);
161
162 static int watchpoint_locations_match (struct bp_location *loc1,
163 struct bp_location *loc2);
164
165 static int breakpoint_location_address_match (struct bp_location *bl,
166 struct address_space *aspace,
167 CORE_ADDR addr);
168
169 static void breakpoints_info (char *, int);
170
171 static void watchpoints_info (char *, int);
172
173 static int breakpoint_1 (char *, int,
174 int (*) (const struct breakpoint *));
175
176 static int breakpoint_cond_eval (void *);
177
178 static void cleanup_executing_breakpoints (void *);
179
180 static void commands_command (char *, int);
181
182 static void condition_command (char *, int);
183
184 typedef enum
185 {
186 mark_inserted,
187 mark_uninserted
188 }
189 insertion_state_t;
190
191 static int remove_breakpoint (struct bp_location *, insertion_state_t);
192 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
193
194 static enum print_stop_action print_bp_stop_message (bpstat bs);
195
196 static int watchpoint_check (void *);
197
198 static void maintenance_info_breakpoints (char *, int);
199
200 static int hw_breakpoint_used_count (void);
201
202 static int hw_watchpoint_use_count (struct breakpoint *);
203
204 static int hw_watchpoint_used_count_others (struct breakpoint *except,
205 enum bptype type,
206 int *other_type_used);
207
208 static void hbreak_command (char *, int);
209
210 static void thbreak_command (char *, int);
211
212 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
213 int count);
214
215 static void stop_command (char *arg, int from_tty);
216
217 static void stopin_command (char *arg, int from_tty);
218
219 static void stopat_command (char *arg, int from_tty);
220
221 static char *ep_parse_optional_if_clause (char **arg);
222
223 static void catch_exception_command_1 (enum exception_event_kind ex_event,
224 char *arg, int tempflag, int from_tty);
225
226 static void tcatch_command (char *arg, int from_tty);
227
228 static void detach_single_step_breakpoints (void);
229
230 static int single_step_breakpoint_inserted_here_p (struct address_space *,
231 CORE_ADDR pc);
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 static void update_global_location_list (int);
240
241 static void update_global_location_list_nothrow (int);
242
243 static int is_hardware_watchpoint (const struct breakpoint *bpt);
244
245 static void insert_breakpoint_locations (void);
246
247 static int syscall_catchpoint_p (struct breakpoint *b);
248
249 static void tracepoints_info (char *, int);
250
251 static void delete_trace_command (char *, int);
252
253 static void enable_trace_command (char *, int);
254
255 static void disable_trace_command (char *, int);
256
257 static void trace_pass_command (char *, int);
258
259 static int is_masked_watchpoint (const struct breakpoint *b);
260
261 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
262
263 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
264 otherwise. */
265
266 static int strace_marker_p (struct breakpoint *b);
267
268 static void init_catchpoint (struct breakpoint *b,
269 struct gdbarch *gdbarch, int tempflag,
270 char *cond_string,
271 const struct breakpoint_ops *ops);
272
273 /* The abstract base class all breakpoint_ops structures inherit
274 from. */
275 static struct breakpoint_ops base_breakpoint_ops;
276
277 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
278 that are implemented on top of software or hardware breakpoints
279 (user breakpoints, internal and momentary breakpoints, etc.). */
280 static struct breakpoint_ops bkpt_base_breakpoint_ops;
281
282 /* Internal breakpoints class type. */
283 static struct breakpoint_ops internal_breakpoint_ops;
284
285 /* Momentary breakpoints class type. */
286 static struct breakpoint_ops momentary_breakpoint_ops;
287
288 /* The breakpoint_ops structure to be used in regular user created
289 breakpoints. */
290 struct breakpoint_ops bkpt_breakpoint_ops;
291
292 /* A reference-counted struct command_line. This lets multiple
293 breakpoints share a single command list. */
294 struct counted_command_line
295 {
296 /* The reference count. */
297 int refc;
298
299 /* The command list. */
300 struct command_line *commands;
301 };
302
303 struct command_line *
304 breakpoint_commands (struct breakpoint *b)
305 {
306 return b->commands ? b->commands->commands : NULL;
307 }
308
309 /* Flag indicating that a command has proceeded the inferior past the
310 current breakpoint. */
311
312 static int breakpoint_proceeded;
313
314 const char *
315 bpdisp_text (enum bpdisp disp)
316 {
317 /* NOTE: the following values are a part of MI protocol and
318 represent values of 'disp' field returned when inferior stops at
319 a breakpoint. */
320 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
321
322 return bpdisps[(int) disp];
323 }
324
325 /* Prototypes for exported functions. */
326 /* If FALSE, gdb will not use hardware support for watchpoints, even
327 if such is available. */
328 static int can_use_hw_watchpoints;
329
330 static void
331 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
332 struct cmd_list_element *c,
333 const char *value)
334 {
335 fprintf_filtered (file,
336 _("Debugger's willingness to use "
337 "watchpoint hardware is %s.\n"),
338 value);
339 }
340
341 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
342 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
343 for unrecognized breakpoint locations.
344 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
345 static enum auto_boolean pending_break_support;
346 static void
347 show_pending_break_support (struct ui_file *file, int from_tty,
348 struct cmd_list_element *c,
349 const char *value)
350 {
351 fprintf_filtered (file,
352 _("Debugger's behavior regarding "
353 "pending breakpoints is %s.\n"),
354 value);
355 }
356
357 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
358 set with "break" but falling in read-only memory.
359 If 0, gdb will warn about such breakpoints, but won't automatically
360 use hardware breakpoints. */
361 static int automatic_hardware_breakpoints;
362 static void
363 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
364 struct cmd_list_element *c,
365 const char *value)
366 {
367 fprintf_filtered (file,
368 _("Automatic usage of hardware breakpoints is %s.\n"),
369 value);
370 }
371
372 /* If on, gdb will keep breakpoints inserted even as inferior is
373 stopped, and immediately insert any new breakpoints. If off, gdb
374 will insert breakpoints into inferior only when resuming it, and
375 will remove breakpoints upon stop. If auto, GDB will behave as ON
376 if in non-stop mode, and as OFF if all-stop mode.*/
377
378 static const char always_inserted_auto[] = "auto";
379 static const char always_inserted_on[] = "on";
380 static const char always_inserted_off[] = "off";
381 static const char *const always_inserted_enums[] = {
382 always_inserted_auto,
383 always_inserted_off,
384 always_inserted_on,
385 NULL
386 };
387 static const char *always_inserted_mode = always_inserted_auto;
388 static void
389 show_always_inserted_mode (struct ui_file *file, int from_tty,
390 struct cmd_list_element *c, const char *value)
391 {
392 if (always_inserted_mode == always_inserted_auto)
393 fprintf_filtered (file,
394 _("Always inserted breakpoint "
395 "mode is %s (currently %s).\n"),
396 value,
397 breakpoints_always_inserted_mode () ? "on" : "off");
398 else
399 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
400 value);
401 }
402
403 int
404 breakpoints_always_inserted_mode (void)
405 {
406 return (always_inserted_mode == always_inserted_on
407 || (always_inserted_mode == always_inserted_auto && non_stop));
408 }
409
410 static const char condition_evaluation_both[] = "host or target";
411
412 /* Modes for breakpoint condition evaluation. */
413 static const char condition_evaluation_auto[] = "auto";
414 static const char condition_evaluation_host[] = "host";
415 static const char condition_evaluation_target[] = "target";
416 static const char *const condition_evaluation_enums[] = {
417 condition_evaluation_auto,
418 condition_evaluation_host,
419 condition_evaluation_target,
420 NULL
421 };
422
423 /* Global that holds the current mode for breakpoint condition evaluation. */
424 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
425
426 /* Global that we use to display information to the user (gets its value from
427 condition_evaluation_mode_1. */
428 static const char *condition_evaluation_mode = condition_evaluation_auto;
429
430 /* Translate a condition evaluation mode MODE into either "host"
431 or "target". This is used mostly to translate from "auto" to the
432 real setting that is being used. It returns the translated
433 evaluation mode. */
434
435 static const char *
436 translate_condition_evaluation_mode (const char *mode)
437 {
438 if (mode == condition_evaluation_auto)
439 {
440 if (target_supports_evaluation_of_breakpoint_conditions ())
441 return condition_evaluation_target;
442 else
443 return condition_evaluation_host;
444 }
445 else
446 return mode;
447 }
448
449 /* Discovers what condition_evaluation_auto translates to. */
450
451 static const char *
452 breakpoint_condition_evaluation_mode (void)
453 {
454 return translate_condition_evaluation_mode (condition_evaluation_mode);
455 }
456
457 /* Return true if GDB should evaluate breakpoint conditions or false
458 otherwise. */
459
460 static int
461 gdb_evaluates_breakpoint_condition_p (void)
462 {
463 const char *mode = breakpoint_condition_evaluation_mode ();
464
465 return (mode == condition_evaluation_host);
466 }
467
468 void _initialize_breakpoint (void);
469
470 /* Are we executing breakpoint commands? */
471 static int executing_breakpoint_commands;
472
473 /* Are overlay event breakpoints enabled? */
474 static int overlay_events_enabled;
475
476 /* See description in breakpoint.h. */
477 int target_exact_watchpoints = 0;
478
479 /* Walk the following statement or block through all breakpoints.
480 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
481 current breakpoint. */
482
483 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
484
485 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
486 for (B = breakpoint_chain; \
487 B ? (TMP=B->next, 1): 0; \
488 B = TMP)
489
490 /* Similar iterator for the low-level breakpoints. SAFE variant is
491 not provided so update_global_location_list must not be called
492 while executing the block of ALL_BP_LOCATIONS. */
493
494 #define ALL_BP_LOCATIONS(B,BP_TMP) \
495 for (BP_TMP = bp_location; \
496 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
497 BP_TMP++)
498
499 /* Iterates through locations with address ADDRESS for the currently selected
500 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
501 to where the loop should start from.
502 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
503 appropriate location to start with. */
504
505 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
506 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
507 BP_LOCP_TMP = BP_LOCP_START; \
508 BP_LOCP_START \
509 && (BP_LOCP_TMP < bp_location + bp_location_count \
510 && (*BP_LOCP_TMP)->address == ADDRESS); \
511 BP_LOCP_TMP++)
512
513 /* Iterator for tracepoints only. */
514
515 #define ALL_TRACEPOINTS(B) \
516 for (B = breakpoint_chain; B; B = B->next) \
517 if (is_tracepoint (B))
518
519 /* Chains of all breakpoints defined. */
520
521 struct breakpoint *breakpoint_chain;
522
523 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
524
525 static struct bp_location **bp_location;
526
527 /* Number of elements of BP_LOCATION. */
528
529 static unsigned bp_location_count;
530
531 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
532 ADDRESS for the current elements of BP_LOCATION which get a valid
533 result from bp_location_has_shadow. You can use it for roughly
534 limiting the subrange of BP_LOCATION to scan for shadow bytes for
535 an address you need to read. */
536
537 static CORE_ADDR bp_location_placed_address_before_address_max;
538
539 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
540 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
541 BP_LOCATION which get a valid result from bp_location_has_shadow.
542 You can use it for roughly limiting the subrange of BP_LOCATION to
543 scan for shadow bytes for an address you need to read. */
544
545 static CORE_ADDR bp_location_shadow_len_after_address_max;
546
547 /* The locations that no longer correspond to any breakpoint, unlinked
548 from bp_location array, but for which a hit may still be reported
549 by a target. */
550 VEC(bp_location_p) *moribund_locations = NULL;
551
552 /* Number of last breakpoint made. */
553
554 static int breakpoint_count;
555
556 /* The value of `breakpoint_count' before the last command that
557 created breakpoints. If the last (break-like) command created more
558 than one breakpoint, then the difference between BREAKPOINT_COUNT
559 and PREV_BREAKPOINT_COUNT is more than one. */
560 static int prev_breakpoint_count;
561
562 /* Number of last tracepoint made. */
563
564 static int tracepoint_count;
565
566 static struct cmd_list_element *breakpoint_set_cmdlist;
567 static struct cmd_list_element *breakpoint_show_cmdlist;
568 struct cmd_list_element *save_cmdlist;
569
570 /* Return whether a breakpoint is an active enabled breakpoint. */
571 static int
572 breakpoint_enabled (struct breakpoint *b)
573 {
574 return (b->enable_state == bp_enabled);
575 }
576
577 /* Set breakpoint count to NUM. */
578
579 static void
580 set_breakpoint_count (int num)
581 {
582 prev_breakpoint_count = breakpoint_count;
583 breakpoint_count = num;
584 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
585 }
586
587 /* Used by `start_rbreak_breakpoints' below, to record the current
588 breakpoint count before "rbreak" creates any breakpoint. */
589 static int rbreak_start_breakpoint_count;
590
591 /* Called at the start an "rbreak" command to record the first
592 breakpoint made. */
593
594 void
595 start_rbreak_breakpoints (void)
596 {
597 rbreak_start_breakpoint_count = breakpoint_count;
598 }
599
600 /* Called at the end of an "rbreak" command to record the last
601 breakpoint made. */
602
603 void
604 end_rbreak_breakpoints (void)
605 {
606 prev_breakpoint_count = rbreak_start_breakpoint_count;
607 }
608
609 /* Used in run_command to zero the hit count when a new run starts. */
610
611 void
612 clear_breakpoint_hit_counts (void)
613 {
614 struct breakpoint *b;
615
616 ALL_BREAKPOINTS (b)
617 b->hit_count = 0;
618 }
619
620 /* Allocate a new counted_command_line with reference count of 1.
621 The new structure owns COMMANDS. */
622
623 static struct counted_command_line *
624 alloc_counted_command_line (struct command_line *commands)
625 {
626 struct counted_command_line *result
627 = xmalloc (sizeof (struct counted_command_line));
628
629 result->refc = 1;
630 result->commands = commands;
631 return result;
632 }
633
634 /* Increment reference count. This does nothing if CMD is NULL. */
635
636 static void
637 incref_counted_command_line (struct counted_command_line *cmd)
638 {
639 if (cmd)
640 ++cmd->refc;
641 }
642
643 /* Decrement reference count. If the reference count reaches 0,
644 destroy the counted_command_line. Sets *CMDP to NULL. This does
645 nothing if *CMDP is NULL. */
646
647 static void
648 decref_counted_command_line (struct counted_command_line **cmdp)
649 {
650 if (*cmdp)
651 {
652 if (--(*cmdp)->refc == 0)
653 {
654 free_command_lines (&(*cmdp)->commands);
655 xfree (*cmdp);
656 }
657 *cmdp = NULL;
658 }
659 }
660
661 /* A cleanup function that calls decref_counted_command_line. */
662
663 static void
664 do_cleanup_counted_command_line (void *arg)
665 {
666 decref_counted_command_line (arg);
667 }
668
669 /* Create a cleanup that calls decref_counted_command_line on the
670 argument. */
671
672 static struct cleanup *
673 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
674 {
675 return make_cleanup (do_cleanup_counted_command_line, cmdp);
676 }
677
678 \f
679 /* Return the breakpoint with the specified number, or NULL
680 if the number does not refer to an existing breakpoint. */
681
682 struct breakpoint *
683 get_breakpoint (int num)
684 {
685 struct breakpoint *b;
686
687 ALL_BREAKPOINTS (b)
688 if (b->number == num)
689 return b;
690
691 return NULL;
692 }
693
694 \f
695
696 /* Mark locations as "conditions have changed" in case the target supports
697 evaluating conditions on its side. */
698
699 static void
700 mark_breakpoint_modified (struct breakpoint *b)
701 {
702 struct bp_location *loc;
703
704 /* This is only meaningful if the target is
705 evaluating conditions and if the user has
706 opted for condition evaluation on the target's
707 side. */
708 if (gdb_evaluates_breakpoint_condition_p ()
709 || !target_supports_evaluation_of_breakpoint_conditions ())
710 return;
711
712 if (!is_breakpoint (b))
713 return;
714
715 for (loc = b->loc; loc; loc = loc->next)
716 loc->condition_changed = condition_modified;
717 }
718
719 /* Mark location as "conditions have changed" in case the target supports
720 evaluating conditions on its side. */
721
722 static void
723 mark_breakpoint_location_modified (struct bp_location *loc)
724 {
725 /* This is only meaningful if the target is
726 evaluating conditions and if the user has
727 opted for condition evaluation on the target's
728 side. */
729 if (gdb_evaluates_breakpoint_condition_p ()
730 || !target_supports_evaluation_of_breakpoint_conditions ())
731
732 return;
733
734 if (!is_breakpoint (loc->owner))
735 return;
736
737 loc->condition_changed = condition_modified;
738 }
739
740 /* Sets the condition-evaluation mode using the static global
741 condition_evaluation_mode. */
742
743 static void
744 set_condition_evaluation_mode (char *args, int from_tty,
745 struct cmd_list_element *c)
746 {
747 struct breakpoint *b;
748 const char *old_mode, *new_mode;
749
750 if ((condition_evaluation_mode_1 == condition_evaluation_target)
751 && !target_supports_evaluation_of_breakpoint_conditions ())
752 {
753 condition_evaluation_mode_1 = condition_evaluation_mode;
754 warning (_("Target does not support breakpoint condition evaluation.\n"
755 "Using host evaluation mode instead."));
756 return;
757 }
758
759 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
760 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
761
762 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
763 settings was "auto". */
764 condition_evaluation_mode = condition_evaluation_mode_1;
765
766 /* Only update the mode if the user picked a different one. */
767 if (new_mode != old_mode)
768 {
769 struct bp_location *loc, **loc_tmp;
770 /* If the user switched to a different evaluation mode, we
771 need to synch the changes with the target as follows:
772
773 "host" -> "target": Send all (valid) conditions to the target.
774 "target" -> "host": Remove all the conditions from the target.
775 */
776
777 if (new_mode == condition_evaluation_target)
778 {
779 /* Mark everything modified and synch conditions with the
780 target. */
781 ALL_BP_LOCATIONS (loc, loc_tmp)
782 mark_breakpoint_location_modified (loc);
783 }
784 else
785 {
786 /* Manually mark non-duplicate locations to synch conditions
787 with the target. We do this to remove all the conditions the
788 target knows about. */
789 ALL_BP_LOCATIONS (loc, loc_tmp)
790 if (is_breakpoint (loc->owner) && loc->inserted)
791 loc->needs_update = 1;
792 }
793
794 /* Do the update. */
795 update_global_location_list (1);
796 }
797
798 return;
799 }
800
801 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
802 what "auto" is translating to. */
803
804 static void
805 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
806 struct cmd_list_element *c, const char *value)
807 {
808 if (condition_evaluation_mode == condition_evaluation_auto)
809 fprintf_filtered (file,
810 _("Breakpoint condition evaluation "
811 "mode is %s (currently %s).\n"),
812 value,
813 breakpoint_condition_evaluation_mode ());
814 else
815 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
816 value);
817 }
818
819 /* A comparison function for bp_location AP and BP that is used by
820 bsearch. This comparison function only cares about addresses, unlike
821 the more general bp_location_compare function. */
822
823 static int
824 bp_location_compare_addrs (const void *ap, const void *bp)
825 {
826 struct bp_location *a = *(void **) ap;
827 struct bp_location *b = *(void **) bp;
828
829 if (a->address == b->address)
830 return 0;
831 else
832 return ((a->address > b->address) - (a->address < b->address));
833 }
834
835 /* Helper function to skip all bp_locations with addresses
836 less than ADDRESS. It returns the first bp_location that
837 is greater than or equal to ADDRESS. If none is found, just
838 return NULL. */
839
840 static struct bp_location **
841 get_first_locp_gte_addr (CORE_ADDR address)
842 {
843 struct bp_location dummy_loc;
844 struct bp_location *dummy_locp = &dummy_loc;
845 struct bp_location **locp_found = NULL;
846
847 /* Initialize the dummy location's address field. */
848 memset (&dummy_loc, 0, sizeof (struct bp_location));
849 dummy_loc.address = address;
850
851 /* Find a close match to the first location at ADDRESS. */
852 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
853 sizeof (struct bp_location **),
854 bp_location_compare_addrs);
855
856 /* Nothing was found, nothing left to do. */
857 if (locp_found == NULL)
858 return NULL;
859
860 /* We may have found a location that is at ADDRESS but is not the first in the
861 location's list. Go backwards (if possible) and locate the first one. */
862 while ((locp_found - 1) >= bp_location
863 && (*(locp_found - 1))->address == address)
864 locp_found--;
865
866 return locp_found;
867 }
868
869 void
870 set_breakpoint_condition (struct breakpoint *b, char *exp,
871 int from_tty)
872 {
873 xfree (b->cond_string);
874 b->cond_string = NULL;
875
876 if (is_watchpoint (b))
877 {
878 struct watchpoint *w = (struct watchpoint *) b;
879
880 xfree (w->cond_exp);
881 w->cond_exp = NULL;
882 }
883 else
884 {
885 struct bp_location *loc;
886
887 for (loc = b->loc; loc; loc = loc->next)
888 {
889 xfree (loc->cond);
890 loc->cond = NULL;
891
892 /* No need to free the condition agent expression
893 bytecode (if we have one). We will handle this
894 when we go through update_global_location_list. */
895 }
896 }
897
898 if (*exp == 0)
899 {
900 if (from_tty)
901 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
902 }
903 else
904 {
905 char *arg = exp;
906
907 /* I don't know if it matters whether this is the string the user
908 typed in or the decompiled expression. */
909 b->cond_string = xstrdup (arg);
910 b->condition_not_parsed = 0;
911
912 if (is_watchpoint (b))
913 {
914 struct watchpoint *w = (struct watchpoint *) b;
915
916 innermost_block = NULL;
917 arg = exp;
918 w->cond_exp = parse_exp_1 (&arg, 0, 0);
919 if (*arg)
920 error (_("Junk at end of expression"));
921 w->cond_exp_valid_block = innermost_block;
922 }
923 else
924 {
925 struct bp_location *loc;
926
927 for (loc = b->loc; loc; loc = loc->next)
928 {
929 arg = exp;
930 loc->cond =
931 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
932 if (*arg)
933 error (_("Junk at end of expression"));
934 }
935 }
936 }
937 mark_breakpoint_modified (b);
938
939 breakpoints_changed ();
940 observer_notify_breakpoint_modified (b);
941 }
942
943 /* condition N EXP -- set break condition of breakpoint N to EXP. */
944
945 static void
946 condition_command (char *arg, int from_tty)
947 {
948 struct breakpoint *b;
949 char *p;
950 int bnum;
951
952 if (arg == 0)
953 error_no_arg (_("breakpoint number"));
954
955 p = arg;
956 bnum = get_number (&p);
957 if (bnum == 0)
958 error (_("Bad breakpoint argument: '%s'"), arg);
959
960 ALL_BREAKPOINTS (b)
961 if (b->number == bnum)
962 {
963 /* Check if this breakpoint has a Python object assigned to
964 it, and if it has a definition of the "stop"
965 method. This method and conditions entered into GDB from
966 the CLI are mutually exclusive. */
967 if (b->py_bp_object
968 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
969 error (_("Cannot set a condition where a Python 'stop' "
970 "method has been defined in the breakpoint."));
971 set_breakpoint_condition (b, p, from_tty);
972
973 if (is_breakpoint (b))
974 update_global_location_list (1);
975
976 return;
977 }
978
979 error (_("No breakpoint number %d."), bnum);
980 }
981
982 /* Check that COMMAND do not contain commands that are suitable
983 only for tracepoints and not suitable for ordinary breakpoints.
984 Throw if any such commands is found. */
985
986 static void
987 check_no_tracepoint_commands (struct command_line *commands)
988 {
989 struct command_line *c;
990
991 for (c = commands; c; c = c->next)
992 {
993 int i;
994
995 if (c->control_type == while_stepping_control)
996 error (_("The 'while-stepping' command can "
997 "only be used for tracepoints"));
998
999 for (i = 0; i < c->body_count; ++i)
1000 check_no_tracepoint_commands ((c->body_list)[i]);
1001
1002 /* Not that command parsing removes leading whitespace and comment
1003 lines and also empty lines. So, we only need to check for
1004 command directly. */
1005 if (strstr (c->line, "collect ") == c->line)
1006 error (_("The 'collect' command can only be used for tracepoints"));
1007
1008 if (strstr (c->line, "teval ") == c->line)
1009 error (_("The 'teval' command can only be used for tracepoints"));
1010 }
1011 }
1012
1013 /* Encapsulate tests for different types of tracepoints. */
1014
1015 static int
1016 is_tracepoint_type (enum bptype type)
1017 {
1018 return (type == bp_tracepoint
1019 || type == bp_fast_tracepoint
1020 || type == bp_static_tracepoint);
1021 }
1022
1023 int
1024 is_tracepoint (const struct breakpoint *b)
1025 {
1026 return is_tracepoint_type (b->type);
1027 }
1028
1029 /* A helper function that validates that COMMANDS are valid for a
1030 breakpoint. This function will throw an exception if a problem is
1031 found. */
1032
1033 static void
1034 validate_commands_for_breakpoint (struct breakpoint *b,
1035 struct command_line *commands)
1036 {
1037 if (is_tracepoint (b))
1038 {
1039 /* We need to verify that each top-level element of commands is
1040 valid for tracepoints, that there's at most one
1041 while-stepping element, and that while-stepping's body has
1042 valid tracing commands excluding nested while-stepping. */
1043 struct command_line *c;
1044 struct command_line *while_stepping = 0;
1045 for (c = commands; c; c = c->next)
1046 {
1047 if (c->control_type == while_stepping_control)
1048 {
1049 if (b->type == bp_fast_tracepoint)
1050 error (_("The 'while-stepping' command "
1051 "cannot be used for fast tracepoint"));
1052 else if (b->type == bp_static_tracepoint)
1053 error (_("The 'while-stepping' command "
1054 "cannot be used for static tracepoint"));
1055
1056 if (while_stepping)
1057 error (_("The 'while-stepping' command "
1058 "can be used only once"));
1059 else
1060 while_stepping = c;
1061 }
1062 }
1063 if (while_stepping)
1064 {
1065 struct command_line *c2;
1066
1067 gdb_assert (while_stepping->body_count == 1);
1068 c2 = while_stepping->body_list[0];
1069 for (; c2; c2 = c2->next)
1070 {
1071 if (c2->control_type == while_stepping_control)
1072 error (_("The 'while-stepping' command cannot be nested"));
1073 }
1074 }
1075 }
1076 else
1077 {
1078 check_no_tracepoint_commands (commands);
1079 }
1080 }
1081
1082 /* Return a vector of all the static tracepoints set at ADDR. The
1083 caller is responsible for releasing the vector. */
1084
1085 VEC(breakpoint_p) *
1086 static_tracepoints_here (CORE_ADDR addr)
1087 {
1088 struct breakpoint *b;
1089 VEC(breakpoint_p) *found = 0;
1090 struct bp_location *loc;
1091
1092 ALL_BREAKPOINTS (b)
1093 if (b->type == bp_static_tracepoint)
1094 {
1095 for (loc = b->loc; loc; loc = loc->next)
1096 if (loc->address == addr)
1097 VEC_safe_push(breakpoint_p, found, b);
1098 }
1099
1100 return found;
1101 }
1102
1103 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1104 validate that only allowed commands are included. */
1105
1106 void
1107 breakpoint_set_commands (struct breakpoint *b,
1108 struct command_line *commands)
1109 {
1110 validate_commands_for_breakpoint (b, commands);
1111
1112 decref_counted_command_line (&b->commands);
1113 b->commands = alloc_counted_command_line (commands);
1114 breakpoints_changed ();
1115 observer_notify_breakpoint_modified (b);
1116 }
1117
1118 /* Set the internal `silent' flag on the breakpoint. Note that this
1119 is not the same as the "silent" that may appear in the breakpoint's
1120 commands. */
1121
1122 void
1123 breakpoint_set_silent (struct breakpoint *b, int silent)
1124 {
1125 int old_silent = b->silent;
1126
1127 b->silent = silent;
1128 if (old_silent != silent)
1129 observer_notify_breakpoint_modified (b);
1130 }
1131
1132 /* Set the thread for this breakpoint. If THREAD is -1, make the
1133 breakpoint work for any thread. */
1134
1135 void
1136 breakpoint_set_thread (struct breakpoint *b, int thread)
1137 {
1138 int old_thread = b->thread;
1139
1140 b->thread = thread;
1141 if (old_thread != thread)
1142 observer_notify_breakpoint_modified (b);
1143 }
1144
1145 /* Set the task for this breakpoint. If TASK is 0, make the
1146 breakpoint work for any task. */
1147
1148 void
1149 breakpoint_set_task (struct breakpoint *b, int task)
1150 {
1151 int old_task = b->task;
1152
1153 b->task = task;
1154 if (old_task != task)
1155 observer_notify_breakpoint_modified (b);
1156 }
1157
1158 void
1159 check_tracepoint_command (char *line, void *closure)
1160 {
1161 struct breakpoint *b = closure;
1162
1163 validate_actionline (&line, b);
1164 }
1165
1166 /* A structure used to pass information through
1167 map_breakpoint_numbers. */
1168
1169 struct commands_info
1170 {
1171 /* True if the command was typed at a tty. */
1172 int from_tty;
1173
1174 /* The breakpoint range spec. */
1175 char *arg;
1176
1177 /* Non-NULL if the body of the commands are being read from this
1178 already-parsed command. */
1179 struct command_line *control;
1180
1181 /* The command lines read from the user, or NULL if they have not
1182 yet been read. */
1183 struct counted_command_line *cmd;
1184 };
1185
1186 /* A callback for map_breakpoint_numbers that sets the commands for
1187 commands_command. */
1188
1189 static void
1190 do_map_commands_command (struct breakpoint *b, void *data)
1191 {
1192 struct commands_info *info = data;
1193
1194 if (info->cmd == NULL)
1195 {
1196 struct command_line *l;
1197
1198 if (info->control != NULL)
1199 l = copy_command_lines (info->control->body_list[0]);
1200 else
1201 {
1202 struct cleanup *old_chain;
1203 char *str;
1204
1205 str = xstrprintf (_("Type commands for breakpoint(s) "
1206 "%s, one per line."),
1207 info->arg);
1208
1209 old_chain = make_cleanup (xfree, str);
1210
1211 l = read_command_lines (str,
1212 info->from_tty, 1,
1213 (is_tracepoint (b)
1214 ? check_tracepoint_command : 0),
1215 b);
1216
1217 do_cleanups (old_chain);
1218 }
1219
1220 info->cmd = alloc_counted_command_line (l);
1221 }
1222
1223 /* If a breakpoint was on the list more than once, we don't need to
1224 do anything. */
1225 if (b->commands != info->cmd)
1226 {
1227 validate_commands_for_breakpoint (b, info->cmd->commands);
1228 incref_counted_command_line (info->cmd);
1229 decref_counted_command_line (&b->commands);
1230 b->commands = info->cmd;
1231 breakpoints_changed ();
1232 observer_notify_breakpoint_modified (b);
1233 }
1234 }
1235
1236 static void
1237 commands_command_1 (char *arg, int from_tty,
1238 struct command_line *control)
1239 {
1240 struct cleanup *cleanups;
1241 struct commands_info info;
1242
1243 info.from_tty = from_tty;
1244 info.control = control;
1245 info.cmd = NULL;
1246 /* If we read command lines from the user, then `info' will hold an
1247 extra reference to the commands that we must clean up. */
1248 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1249
1250 if (arg == NULL || !*arg)
1251 {
1252 if (breakpoint_count - prev_breakpoint_count > 1)
1253 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1254 breakpoint_count);
1255 else if (breakpoint_count > 0)
1256 arg = xstrprintf ("%d", breakpoint_count);
1257 else
1258 {
1259 /* So that we don't try to free the incoming non-NULL
1260 argument in the cleanup below. Mapping breakpoint
1261 numbers will fail in this case. */
1262 arg = NULL;
1263 }
1264 }
1265 else
1266 /* The command loop has some static state, so we need to preserve
1267 our argument. */
1268 arg = xstrdup (arg);
1269
1270 if (arg != NULL)
1271 make_cleanup (xfree, arg);
1272
1273 info.arg = arg;
1274
1275 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1276
1277 if (info.cmd == NULL)
1278 error (_("No breakpoints specified."));
1279
1280 do_cleanups (cleanups);
1281 }
1282
1283 static void
1284 commands_command (char *arg, int from_tty)
1285 {
1286 commands_command_1 (arg, from_tty, NULL);
1287 }
1288
1289 /* Like commands_command, but instead of reading the commands from
1290 input stream, takes them from an already parsed command structure.
1291
1292 This is used by cli-script.c to DTRT with breakpoint commands
1293 that are part of if and while bodies. */
1294 enum command_control_type
1295 commands_from_control_command (char *arg, struct command_line *cmd)
1296 {
1297 commands_command_1 (arg, 0, cmd);
1298 return simple_control;
1299 }
1300
1301 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1302
1303 static int
1304 bp_location_has_shadow (struct bp_location *bl)
1305 {
1306 if (bl->loc_type != bp_loc_software_breakpoint)
1307 return 0;
1308 if (!bl->inserted)
1309 return 0;
1310 if (bl->target_info.shadow_len == 0)
1311 /* BL isn't valid, or doesn't shadow memory. */
1312 return 0;
1313 return 1;
1314 }
1315
1316 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1317 by replacing any memory breakpoints with their shadowed contents.
1318
1319 If READBUF is not NULL, this buffer must not overlap with any of
1320 the breakpoint location's shadow_contents buffers. Otherwise,
1321 a failed assertion internal error will be raised.
1322
1323 The range of shadowed area by each bp_location is:
1324 bl->address - bp_location_placed_address_before_address_max
1325 up to bl->address + bp_location_shadow_len_after_address_max
1326 The range we were requested to resolve shadows for is:
1327 memaddr ... memaddr + len
1328 Thus the safe cutoff boundaries for performance optimization are
1329 memaddr + len <= (bl->address
1330 - bp_location_placed_address_before_address_max)
1331 and:
1332 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1333
1334 void
1335 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1336 const gdb_byte *writebuf_org,
1337 ULONGEST memaddr, LONGEST len)
1338 {
1339 /* Left boundary, right boundary and median element of our binary
1340 search. */
1341 unsigned bc_l, bc_r, bc;
1342
1343 /* Find BC_L which is a leftmost element which may affect BUF
1344 content. It is safe to report lower value but a failure to
1345 report higher one. */
1346
1347 bc_l = 0;
1348 bc_r = bp_location_count;
1349 while (bc_l + 1 < bc_r)
1350 {
1351 struct bp_location *bl;
1352
1353 bc = (bc_l + bc_r) / 2;
1354 bl = bp_location[bc];
1355
1356 /* Check first BL->ADDRESS will not overflow due to the added
1357 constant. Then advance the left boundary only if we are sure
1358 the BC element can in no way affect the BUF content (MEMADDR
1359 to MEMADDR + LEN range).
1360
1361 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1362 offset so that we cannot miss a breakpoint with its shadow
1363 range tail still reaching MEMADDR. */
1364
1365 if ((bl->address + bp_location_shadow_len_after_address_max
1366 >= bl->address)
1367 && (bl->address + bp_location_shadow_len_after_address_max
1368 <= memaddr))
1369 bc_l = bc;
1370 else
1371 bc_r = bc;
1372 }
1373
1374 /* Due to the binary search above, we need to make sure we pick the
1375 first location that's at BC_L's address. E.g., if there are
1376 multiple locations at the same address, BC_L may end up pointing
1377 at a duplicate location, and miss the "master"/"inserted"
1378 location. Say, given locations L1, L2 and L3 at addresses A and
1379 B:
1380
1381 L1@A, L2@A, L3@B, ...
1382
1383 BC_L could end up pointing at location L2, while the "master"
1384 location could be L1. Since the `loc->inserted' flag is only set
1385 on "master" locations, we'd forget to restore the shadow of L1
1386 and L2. */
1387 while (bc_l > 0
1388 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1389 bc_l--;
1390
1391 /* Now do full processing of the found relevant range of elements. */
1392
1393 for (bc = bc_l; bc < bp_location_count; bc++)
1394 {
1395 struct bp_location *bl = bp_location[bc];
1396 CORE_ADDR bp_addr = 0;
1397 int bp_size = 0;
1398 int bptoffset = 0;
1399
1400 /* bp_location array has BL->OWNER always non-NULL. */
1401 if (bl->owner->type == bp_none)
1402 warning (_("reading through apparently deleted breakpoint #%d?"),
1403 bl->owner->number);
1404
1405 /* Performance optimization: any further element can no longer affect BUF
1406 content. */
1407
1408 if (bl->address >= bp_location_placed_address_before_address_max
1409 && memaddr + len <= (bl->address
1410 - bp_location_placed_address_before_address_max))
1411 break;
1412
1413 if (!bp_location_has_shadow (bl))
1414 continue;
1415 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1416 current_program_space->aspace, 0))
1417 continue;
1418
1419 /* Addresses and length of the part of the breakpoint that
1420 we need to copy. */
1421 bp_addr = bl->target_info.placed_address;
1422 bp_size = bl->target_info.shadow_len;
1423
1424 if (bp_addr + bp_size <= memaddr)
1425 /* The breakpoint is entirely before the chunk of memory we
1426 are reading. */
1427 continue;
1428
1429 if (bp_addr >= memaddr + len)
1430 /* The breakpoint is entirely after the chunk of memory we are
1431 reading. */
1432 continue;
1433
1434 /* Offset within shadow_contents. */
1435 if (bp_addr < memaddr)
1436 {
1437 /* Only copy the second part of the breakpoint. */
1438 bp_size -= memaddr - bp_addr;
1439 bptoffset = memaddr - bp_addr;
1440 bp_addr = memaddr;
1441 }
1442
1443 if (bp_addr + bp_size > memaddr + len)
1444 {
1445 /* Only copy the first part of the breakpoint. */
1446 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1447 }
1448
1449 if (readbuf != NULL)
1450 {
1451 /* Verify that the readbuf buffer does not overlap with
1452 the shadow_contents buffer. */
1453 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1454 || readbuf >= (bl->target_info.shadow_contents
1455 + bl->target_info.shadow_len));
1456
1457 /* Update the read buffer with this inserted breakpoint's
1458 shadow. */
1459 memcpy (readbuf + bp_addr - memaddr,
1460 bl->target_info.shadow_contents + bptoffset, bp_size);
1461 }
1462 else
1463 {
1464 struct gdbarch *gdbarch = bl->gdbarch;
1465 const unsigned char *bp;
1466 CORE_ADDR placed_address = bl->target_info.placed_address;
1467 unsigned placed_size = bl->target_info.placed_size;
1468
1469 /* Update the shadow with what we want to write to memory. */
1470 memcpy (bl->target_info.shadow_contents + bptoffset,
1471 writebuf_org + bp_addr - memaddr, bp_size);
1472
1473 /* Determine appropriate breakpoint contents and size for this
1474 address. */
1475 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1476
1477 /* Update the final write buffer with this inserted
1478 breakpoint's INSN. */
1479 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1480 }
1481 }
1482 }
1483 \f
1484
1485 /* Return true if BPT is either a software breakpoint or a hardware
1486 breakpoint. */
1487
1488 int
1489 is_breakpoint (const struct breakpoint *bpt)
1490 {
1491 return (bpt->type == bp_breakpoint
1492 || bpt->type == bp_hardware_breakpoint);
1493 }
1494
1495 /* Return true if BPT is of any hardware watchpoint kind. */
1496
1497 static int
1498 is_hardware_watchpoint (const struct breakpoint *bpt)
1499 {
1500 return (bpt->type == bp_hardware_watchpoint
1501 || bpt->type == bp_read_watchpoint
1502 || bpt->type == bp_access_watchpoint);
1503 }
1504
1505 /* Return true if BPT is of any watchpoint kind, hardware or
1506 software. */
1507
1508 int
1509 is_watchpoint (const struct breakpoint *bpt)
1510 {
1511 return (is_hardware_watchpoint (bpt)
1512 || bpt->type == bp_watchpoint);
1513 }
1514
1515 /* Returns true if the current thread and its running state are safe
1516 to evaluate or update watchpoint B. Watchpoints on local
1517 expressions need to be evaluated in the context of the thread that
1518 was current when the watchpoint was created, and, that thread needs
1519 to be stopped to be able to select the correct frame context.
1520 Watchpoints on global expressions can be evaluated on any thread,
1521 and in any state. It is presently left to the target allowing
1522 memory accesses when threads are running. */
1523
1524 static int
1525 watchpoint_in_thread_scope (struct watchpoint *b)
1526 {
1527 return (b->base.pspace == current_program_space
1528 && (ptid_equal (b->watchpoint_thread, null_ptid)
1529 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1530 && !is_executing (inferior_ptid))));
1531 }
1532
1533 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1534 associated bp_watchpoint_scope breakpoint. */
1535
1536 static void
1537 watchpoint_del_at_next_stop (struct watchpoint *w)
1538 {
1539 struct breakpoint *b = &w->base;
1540
1541 if (b->related_breakpoint != b)
1542 {
1543 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1544 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1545 b->related_breakpoint->disposition = disp_del_at_next_stop;
1546 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1547 b->related_breakpoint = b;
1548 }
1549 b->disposition = disp_del_at_next_stop;
1550 }
1551
1552 /* Assuming that B is a watchpoint:
1553 - Reparse watchpoint expression, if REPARSE is non-zero
1554 - Evaluate expression and store the result in B->val
1555 - Evaluate the condition if there is one, and store the result
1556 in b->loc->cond.
1557 - Update the list of values that must be watched in B->loc.
1558
1559 If the watchpoint disposition is disp_del_at_next_stop, then do
1560 nothing. If this is local watchpoint that is out of scope, delete
1561 it.
1562
1563 Even with `set breakpoint always-inserted on' the watchpoints are
1564 removed + inserted on each stop here. Normal breakpoints must
1565 never be removed because they might be missed by a running thread
1566 when debugging in non-stop mode. On the other hand, hardware
1567 watchpoints (is_hardware_watchpoint; processed here) are specific
1568 to each LWP since they are stored in each LWP's hardware debug
1569 registers. Therefore, such LWP must be stopped first in order to
1570 be able to modify its hardware watchpoints.
1571
1572 Hardware watchpoints must be reset exactly once after being
1573 presented to the user. It cannot be done sooner, because it would
1574 reset the data used to present the watchpoint hit to the user. And
1575 it must not be done later because it could display the same single
1576 watchpoint hit during multiple GDB stops. Note that the latter is
1577 relevant only to the hardware watchpoint types bp_read_watchpoint
1578 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1579 not user-visible - its hit is suppressed if the memory content has
1580 not changed.
1581
1582 The following constraints influence the location where we can reset
1583 hardware watchpoints:
1584
1585 * target_stopped_by_watchpoint and target_stopped_data_address are
1586 called several times when GDB stops.
1587
1588 [linux]
1589 * Multiple hardware watchpoints can be hit at the same time,
1590 causing GDB to stop. GDB only presents one hardware watchpoint
1591 hit at a time as the reason for stopping, and all the other hits
1592 are presented later, one after the other, each time the user
1593 requests the execution to be resumed. Execution is not resumed
1594 for the threads still having pending hit event stored in
1595 LWP_INFO->STATUS. While the watchpoint is already removed from
1596 the inferior on the first stop the thread hit event is kept being
1597 reported from its cached value by linux_nat_stopped_data_address
1598 until the real thread resume happens after the watchpoint gets
1599 presented and thus its LWP_INFO->STATUS gets reset.
1600
1601 Therefore the hardware watchpoint hit can get safely reset on the
1602 watchpoint removal from inferior. */
1603
1604 static void
1605 update_watchpoint (struct watchpoint *b, int reparse)
1606 {
1607 int within_current_scope;
1608 struct frame_id saved_frame_id;
1609 int frame_saved;
1610
1611 /* If this is a local watchpoint, we only want to check if the
1612 watchpoint frame is in scope if the current thread is the thread
1613 that was used to create the watchpoint. */
1614 if (!watchpoint_in_thread_scope (b))
1615 return;
1616
1617 if (b->base.disposition == disp_del_at_next_stop)
1618 return;
1619
1620 frame_saved = 0;
1621
1622 /* Determine if the watchpoint is within scope. */
1623 if (b->exp_valid_block == NULL)
1624 within_current_scope = 1;
1625 else
1626 {
1627 struct frame_info *fi = get_current_frame ();
1628 struct gdbarch *frame_arch = get_frame_arch (fi);
1629 CORE_ADDR frame_pc = get_frame_pc (fi);
1630
1631 /* If we're in a function epilogue, unwinding may not work
1632 properly, so do not attempt to recreate locations at this
1633 point. See similar comments in watchpoint_check. */
1634 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1635 return;
1636
1637 /* Save the current frame's ID so we can restore it after
1638 evaluating the watchpoint expression on its own frame. */
1639 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1640 took a frame parameter, so that we didn't have to change the
1641 selected frame. */
1642 frame_saved = 1;
1643 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1644
1645 fi = frame_find_by_id (b->watchpoint_frame);
1646 within_current_scope = (fi != NULL);
1647 if (within_current_scope)
1648 select_frame (fi);
1649 }
1650
1651 /* We don't free locations. They are stored in the bp_location array
1652 and update_global_location_list will eventually delete them and
1653 remove breakpoints if needed. */
1654 b->base.loc = NULL;
1655
1656 if (within_current_scope && reparse)
1657 {
1658 char *s;
1659
1660 if (b->exp)
1661 {
1662 xfree (b->exp);
1663 b->exp = NULL;
1664 }
1665 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1666 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1667 /* If the meaning of expression itself changed, the old value is
1668 no longer relevant. We don't want to report a watchpoint hit
1669 to the user when the old value and the new value may actually
1670 be completely different objects. */
1671 value_free (b->val);
1672 b->val = NULL;
1673 b->val_valid = 0;
1674
1675 /* Note that unlike with breakpoints, the watchpoint's condition
1676 expression is stored in the breakpoint object, not in the
1677 locations (re)created below. */
1678 if (b->base.cond_string != NULL)
1679 {
1680 if (b->cond_exp != NULL)
1681 {
1682 xfree (b->cond_exp);
1683 b->cond_exp = NULL;
1684 }
1685
1686 s = b->base.cond_string;
1687 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1688 }
1689 }
1690
1691 /* If we failed to parse the expression, for example because
1692 it refers to a global variable in a not-yet-loaded shared library,
1693 don't try to insert watchpoint. We don't automatically delete
1694 such watchpoint, though, since failure to parse expression
1695 is different from out-of-scope watchpoint. */
1696 if ( !target_has_execution)
1697 {
1698 /* Without execution, memory can't change. No use to try and
1699 set watchpoint locations. The watchpoint will be reset when
1700 the target gains execution, through breakpoint_re_set. */
1701 }
1702 else if (within_current_scope && b->exp)
1703 {
1704 int pc = 0;
1705 struct value *val_chain, *v, *result, *next;
1706 struct program_space *frame_pspace;
1707
1708 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1709
1710 /* Avoid setting b->val if it's already set. The meaning of
1711 b->val is 'the last value' user saw, and we should update
1712 it only if we reported that last value to user. As it
1713 happens, the code that reports it updates b->val directly.
1714 We don't keep track of the memory value for masked
1715 watchpoints. */
1716 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1717 {
1718 b->val = v;
1719 b->val_valid = 1;
1720 }
1721
1722 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1723
1724 /* Look at each value on the value chain. */
1725 for (v = val_chain; v; v = value_next (v))
1726 {
1727 /* If it's a memory location, and GDB actually needed
1728 its contents to evaluate the expression, then we
1729 must watch it. If the first value returned is
1730 still lazy, that means an error occurred reading it;
1731 watch it anyway in case it becomes readable. */
1732 if (VALUE_LVAL (v) == lval_memory
1733 && (v == val_chain || ! value_lazy (v)))
1734 {
1735 struct type *vtype = check_typedef (value_type (v));
1736
1737 /* We only watch structs and arrays if user asked
1738 for it explicitly, never if they just happen to
1739 appear in the middle of some value chain. */
1740 if (v == result
1741 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1742 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1743 {
1744 CORE_ADDR addr;
1745 int len, type;
1746 struct bp_location *loc, **tmp;
1747
1748 addr = value_address (v);
1749 len = TYPE_LENGTH (value_type (v));
1750 type = hw_write;
1751 if (b->base.type == bp_read_watchpoint)
1752 type = hw_read;
1753 else if (b->base.type == bp_access_watchpoint)
1754 type = hw_access;
1755
1756 loc = allocate_bp_location (&b->base);
1757 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1758 ;
1759 *tmp = loc;
1760 loc->gdbarch = get_type_arch (value_type (v));
1761
1762 loc->pspace = frame_pspace;
1763 loc->address = addr;
1764 loc->length = len;
1765 loc->watchpoint_type = type;
1766 }
1767 }
1768 }
1769
1770 /* Change the type of breakpoint between hardware assisted or
1771 an ordinary watchpoint depending on the hardware support
1772 and free hardware slots. REPARSE is set when the inferior
1773 is started. */
1774 if (reparse)
1775 {
1776 int reg_cnt;
1777 enum bp_loc_type loc_type;
1778 struct bp_location *bl;
1779
1780 reg_cnt = can_use_hardware_watchpoint (val_chain);
1781
1782 if (reg_cnt)
1783 {
1784 int i, target_resources_ok, other_type_used;
1785 enum bptype type;
1786
1787 /* Use an exact watchpoint when there's only one memory region to be
1788 watched, and only one debug register is needed to watch it. */
1789 b->exact = target_exact_watchpoints && reg_cnt == 1;
1790
1791 /* We need to determine how many resources are already
1792 used for all other hardware watchpoints plus this one
1793 to see if we still have enough resources to also fit
1794 this watchpoint in as well. */
1795
1796 /* If this is a software watchpoint, we try to turn it
1797 to a hardware one -- count resources as if B was of
1798 hardware watchpoint type. */
1799 type = b->base.type;
1800 if (type == bp_watchpoint)
1801 type = bp_hardware_watchpoint;
1802
1803 /* This watchpoint may or may not have been placed on
1804 the list yet at this point (it won't be in the list
1805 if we're trying to create it for the first time,
1806 through watch_command), so always account for it
1807 manually. */
1808
1809 /* Count resources used by all watchpoints except B. */
1810 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1811
1812 /* Add in the resources needed for B. */
1813 i += hw_watchpoint_use_count (&b->base);
1814
1815 target_resources_ok
1816 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1817 if (target_resources_ok <= 0)
1818 {
1819 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1820
1821 if (target_resources_ok == 0 && !sw_mode)
1822 error (_("Target does not support this type of "
1823 "hardware watchpoint."));
1824 else if (target_resources_ok < 0 && !sw_mode)
1825 error (_("There are not enough available hardware "
1826 "resources for this watchpoint."));
1827
1828 /* Downgrade to software watchpoint. */
1829 b->base.type = bp_watchpoint;
1830 }
1831 else
1832 {
1833 /* If this was a software watchpoint, we've just
1834 found we have enough resources to turn it to a
1835 hardware watchpoint. Otherwise, this is a
1836 nop. */
1837 b->base.type = type;
1838 }
1839 }
1840 else if (!b->base.ops->works_in_software_mode (&b->base))
1841 error (_("Expression cannot be implemented with "
1842 "read/access watchpoint."));
1843 else
1844 b->base.type = bp_watchpoint;
1845
1846 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1847 : bp_loc_hardware_watchpoint);
1848 for (bl = b->base.loc; bl; bl = bl->next)
1849 bl->loc_type = loc_type;
1850 }
1851
1852 for (v = val_chain; v; v = next)
1853 {
1854 next = value_next (v);
1855 if (v != b->val)
1856 value_free (v);
1857 }
1858
1859 /* If a software watchpoint is not watching any memory, then the
1860 above left it without any location set up. But,
1861 bpstat_stop_status requires a location to be able to report
1862 stops, so make sure there's at least a dummy one. */
1863 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1864 {
1865 struct breakpoint *base = &b->base;
1866 base->loc = allocate_bp_location (base);
1867 base->loc->pspace = frame_pspace;
1868 base->loc->address = -1;
1869 base->loc->length = -1;
1870 base->loc->watchpoint_type = -1;
1871 }
1872 }
1873 else if (!within_current_scope)
1874 {
1875 printf_filtered (_("\
1876 Watchpoint %d deleted because the program has left the block\n\
1877 in which its expression is valid.\n"),
1878 b->base.number);
1879 watchpoint_del_at_next_stop (b);
1880 }
1881
1882 /* Restore the selected frame. */
1883 if (frame_saved)
1884 select_frame (frame_find_by_id (saved_frame_id));
1885 }
1886
1887
1888 /* Returns 1 iff breakpoint location should be
1889 inserted in the inferior. We don't differentiate the type of BL's owner
1890 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1891 breakpoint_ops is not defined, because in insert_bp_location,
1892 tracepoint's insert_location will not be called. */
1893 static int
1894 should_be_inserted (struct bp_location *bl)
1895 {
1896 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1897 return 0;
1898
1899 if (bl->owner->disposition == disp_del_at_next_stop)
1900 return 0;
1901
1902 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1903 return 0;
1904
1905 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1906 return 0;
1907
1908 /* This is set for example, when we're attached to the parent of a
1909 vfork, and have detached from the child. The child is running
1910 free, and we expect it to do an exec or exit, at which point the
1911 OS makes the parent schedulable again (and the target reports
1912 that the vfork is done). Until the child is done with the shared
1913 memory region, do not insert breakpoints in the parent, otherwise
1914 the child could still trip on the parent's breakpoints. Since
1915 the parent is blocked anyway, it won't miss any breakpoint. */
1916 if (bl->pspace->breakpoints_not_allowed)
1917 return 0;
1918
1919 return 1;
1920 }
1921
1922 /* Same as should_be_inserted but does the check assuming
1923 that the location is not duplicated. */
1924
1925 static int
1926 unduplicated_should_be_inserted (struct bp_location *bl)
1927 {
1928 int result;
1929 const int save_duplicate = bl->duplicate;
1930
1931 bl->duplicate = 0;
1932 result = should_be_inserted (bl);
1933 bl->duplicate = save_duplicate;
1934 return result;
1935 }
1936
1937 /* Parses a conditional described by an expression COND into an
1938 agent expression bytecode suitable for evaluation
1939 by the bytecode interpreter. Return NULL if there was
1940 any error during parsing. */
1941
1942 static struct agent_expr *
1943 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
1944 {
1945 struct agent_expr *aexpr = NULL;
1946 struct cleanup *old_chain = NULL;
1947 volatile struct gdb_exception ex;
1948
1949 if (!cond)
1950 return NULL;
1951
1952 /* We don't want to stop processing, so catch any errors
1953 that may show up. */
1954 TRY_CATCH (ex, RETURN_MASK_ERROR)
1955 {
1956 aexpr = gen_eval_for_expr (scope, cond);
1957 }
1958
1959 if (ex.reason < 0)
1960 {
1961 /* If we got here, it means the condition could not be parsed to a valid
1962 bytecode expression and thus can't be evaluated on the target's side.
1963 It's no use iterating through the conditions. */
1964 return NULL;
1965 }
1966
1967 /* We have a valid agent expression. */
1968 return aexpr;
1969 }
1970
1971 /* Based on location BL, create a list of breakpoint conditions to be
1972 passed on to the target. If we have duplicated locations with different
1973 conditions, we will add such conditions to the list. The idea is that the
1974 target will evaluate the list of conditions and will only notify GDB when
1975 one of them is true. */
1976
1977 static void
1978 build_target_condition_list (struct bp_location *bl)
1979 {
1980 struct bp_location **locp = NULL, **loc2p;
1981 int null_condition_or_parse_error = 0;
1982 int modified = bl->needs_update;
1983 struct bp_location *loc;
1984
1985 /* This is only meaningful if the target is
1986 evaluating conditions and if the user has
1987 opted for condition evaluation on the target's
1988 side. */
1989 if (gdb_evaluates_breakpoint_condition_p ()
1990 || !target_supports_evaluation_of_breakpoint_conditions ())
1991 return;
1992
1993 /* Do a first pass to check for locations with no assigned
1994 conditions or conditions that fail to parse to a valid agent expression
1995 bytecode. If any of these happen, then it's no use to send conditions
1996 to the target since this location will always trigger and generate a
1997 response back to GDB. */
1998 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
1999 {
2000 loc = (*loc2p);
2001 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2002 {
2003 if (modified)
2004 {
2005 struct agent_expr *aexpr;
2006
2007 /* Re-parse the conditions since something changed. In that
2008 case we already freed the condition bytecodes (see
2009 force_breakpoint_reinsertion). We just
2010 need to parse the condition to bytecodes again. */
2011 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2012 loc->cond_bytecode = aexpr;
2013
2014 /* Check if we managed to parse the conditional expression
2015 correctly. If not, we will not send this condition
2016 to the target. */
2017 if (aexpr)
2018 continue;
2019 }
2020
2021 /* If we have a NULL bytecode expression, it means something
2022 went wrong or we have a null condition expression. */
2023 if (!loc->cond_bytecode)
2024 {
2025 null_condition_or_parse_error = 1;
2026 break;
2027 }
2028 }
2029 }
2030
2031 /* If any of these happened, it means we will have to evaluate the conditions
2032 for the location's address on gdb's side. It is no use keeping bytecodes
2033 for all the other duplicate locations, thus we free all of them here.
2034
2035 This is so we have a finer control over which locations' conditions are
2036 being evaluated by GDB or the remote stub. */
2037 if (null_condition_or_parse_error)
2038 {
2039 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2040 {
2041 loc = (*loc2p);
2042 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2043 {
2044 /* Only go as far as the first NULL bytecode is
2045 located. */
2046 if (!loc->cond_bytecode)
2047 return;
2048
2049 free_agent_expr (loc->cond_bytecode);
2050 loc->cond_bytecode = NULL;
2051 }
2052 }
2053 }
2054
2055 /* No NULL conditions or failed bytecode generation. Build a condition list
2056 for this location's address. */
2057 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2058 {
2059 loc = (*loc2p);
2060 if (loc->cond
2061 && is_breakpoint (loc->owner)
2062 && loc->pspace->num == bl->pspace->num
2063 && loc->owner->enable_state == bp_enabled
2064 && loc->enabled)
2065 /* Add the condition to the vector. This will be used later to send the
2066 conditions to the target. */
2067 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2068 loc->cond_bytecode);
2069 }
2070
2071 return;
2072 }
2073
2074 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2075 location. Any error messages are printed to TMP_ERROR_STREAM; and
2076 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2077 Returns 0 for success, 1 if the bp_location type is not supported or
2078 -1 for failure.
2079
2080 NOTE drow/2003-09-09: This routine could be broken down to an
2081 object-style method for each breakpoint or catchpoint type. */
2082 static int
2083 insert_bp_location (struct bp_location *bl,
2084 struct ui_file *tmp_error_stream,
2085 int *disabled_breaks,
2086 int *hw_breakpoint_error)
2087 {
2088 int val = 0;
2089
2090 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2091 return 0;
2092
2093 /* Note we don't initialize bl->target_info, as that wipes out
2094 the breakpoint location's shadow_contents if the breakpoint
2095 is still inserted at that location. This in turn breaks
2096 target_read_memory which depends on these buffers when
2097 a memory read is requested at the breakpoint location:
2098 Once the target_info has been wiped, we fail to see that
2099 we have a breakpoint inserted at that address and thus
2100 read the breakpoint instead of returning the data saved in
2101 the breakpoint location's shadow contents. */
2102 bl->target_info.placed_address = bl->address;
2103 bl->target_info.placed_address_space = bl->pspace->aspace;
2104 bl->target_info.length = bl->length;
2105
2106 /* When working with target-side conditions, we must pass all the conditions
2107 for the same breakpoint address down to the target since GDB will not
2108 insert those locations. With a list of breakpoint conditions, the target
2109 can decide when to stop and notify GDB. */
2110
2111 if (is_breakpoint (bl->owner))
2112 {
2113 build_target_condition_list (bl);
2114 /* Reset the condition modification marker. */
2115 bl->needs_update = 0;
2116 }
2117
2118 if (bl->loc_type == bp_loc_software_breakpoint
2119 || bl->loc_type == bp_loc_hardware_breakpoint)
2120 {
2121 if (bl->owner->type != bp_hardware_breakpoint)
2122 {
2123 /* If the explicitly specified breakpoint type
2124 is not hardware breakpoint, check the memory map to see
2125 if the breakpoint address is in read only memory or not.
2126
2127 Two important cases are:
2128 - location type is not hardware breakpoint, memory
2129 is readonly. We change the type of the location to
2130 hardware breakpoint.
2131 - location type is hardware breakpoint, memory is
2132 read-write. This means we've previously made the
2133 location hardware one, but then the memory map changed,
2134 so we undo.
2135
2136 When breakpoints are removed, remove_breakpoints will use
2137 location types we've just set here, the only possible
2138 problem is that memory map has changed during running
2139 program, but it's not going to work anyway with current
2140 gdb. */
2141 struct mem_region *mr
2142 = lookup_mem_region (bl->target_info.placed_address);
2143
2144 if (mr)
2145 {
2146 if (automatic_hardware_breakpoints)
2147 {
2148 enum bp_loc_type new_type;
2149
2150 if (mr->attrib.mode != MEM_RW)
2151 new_type = bp_loc_hardware_breakpoint;
2152 else
2153 new_type = bp_loc_software_breakpoint;
2154
2155 if (new_type != bl->loc_type)
2156 {
2157 static int said = 0;
2158
2159 bl->loc_type = new_type;
2160 if (!said)
2161 {
2162 fprintf_filtered (gdb_stdout,
2163 _("Note: automatically using "
2164 "hardware breakpoints for "
2165 "read-only addresses.\n"));
2166 said = 1;
2167 }
2168 }
2169 }
2170 else if (bl->loc_type == bp_loc_software_breakpoint
2171 && mr->attrib.mode != MEM_RW)
2172 warning (_("cannot set software breakpoint "
2173 "at readonly address %s"),
2174 paddress (bl->gdbarch, bl->address));
2175 }
2176 }
2177
2178 /* First check to see if we have to handle an overlay. */
2179 if (overlay_debugging == ovly_off
2180 || bl->section == NULL
2181 || !(section_is_overlay (bl->section)))
2182 {
2183 /* No overlay handling: just set the breakpoint. */
2184
2185 val = bl->owner->ops->insert_location (bl);
2186 }
2187 else
2188 {
2189 /* This breakpoint is in an overlay section.
2190 Shall we set a breakpoint at the LMA? */
2191 if (!overlay_events_enabled)
2192 {
2193 /* Yes -- overlay event support is not active,
2194 so we must try to set a breakpoint at the LMA.
2195 This will not work for a hardware breakpoint. */
2196 if (bl->loc_type == bp_loc_hardware_breakpoint)
2197 warning (_("hardware breakpoint %d not supported in overlay!"),
2198 bl->owner->number);
2199 else
2200 {
2201 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2202 bl->section);
2203 /* Set a software (trap) breakpoint at the LMA. */
2204 bl->overlay_target_info = bl->target_info;
2205 bl->overlay_target_info.placed_address = addr;
2206 val = target_insert_breakpoint (bl->gdbarch,
2207 &bl->overlay_target_info);
2208 if (val != 0)
2209 fprintf_unfiltered (tmp_error_stream,
2210 "Overlay breakpoint %d "
2211 "failed: in ROM?\n",
2212 bl->owner->number);
2213 }
2214 }
2215 /* Shall we set a breakpoint at the VMA? */
2216 if (section_is_mapped (bl->section))
2217 {
2218 /* Yes. This overlay section is mapped into memory. */
2219 val = bl->owner->ops->insert_location (bl);
2220 }
2221 else
2222 {
2223 /* No. This breakpoint will not be inserted.
2224 No error, but do not mark the bp as 'inserted'. */
2225 return 0;
2226 }
2227 }
2228
2229 if (val)
2230 {
2231 /* Can't set the breakpoint. */
2232 if (solib_name_from_address (bl->pspace, bl->address))
2233 {
2234 /* See also: disable_breakpoints_in_shlibs. */
2235 val = 0;
2236 bl->shlib_disabled = 1;
2237 observer_notify_breakpoint_modified (bl->owner);
2238 if (!*disabled_breaks)
2239 {
2240 fprintf_unfiltered (tmp_error_stream,
2241 "Cannot insert breakpoint %d.\n",
2242 bl->owner->number);
2243 fprintf_unfiltered (tmp_error_stream,
2244 "Temporarily disabling shared "
2245 "library breakpoints:\n");
2246 }
2247 *disabled_breaks = 1;
2248 fprintf_unfiltered (tmp_error_stream,
2249 "breakpoint #%d\n", bl->owner->number);
2250 }
2251 else
2252 {
2253 if (bl->loc_type == bp_loc_hardware_breakpoint)
2254 {
2255 *hw_breakpoint_error = 1;
2256 fprintf_unfiltered (tmp_error_stream,
2257 "Cannot insert hardware "
2258 "breakpoint %d.\n",
2259 bl->owner->number);
2260 }
2261 else
2262 {
2263 fprintf_unfiltered (tmp_error_stream,
2264 "Cannot insert breakpoint %d.\n",
2265 bl->owner->number);
2266 fprintf_filtered (tmp_error_stream,
2267 "Error accessing memory address ");
2268 fputs_filtered (paddress (bl->gdbarch, bl->address),
2269 tmp_error_stream);
2270 fprintf_filtered (tmp_error_stream, ": %s.\n",
2271 safe_strerror (val));
2272 }
2273
2274 }
2275 }
2276 else
2277 bl->inserted = 1;
2278
2279 return val;
2280 }
2281
2282 else if (bl->loc_type == bp_loc_hardware_watchpoint
2283 /* NOTE drow/2003-09-08: This state only exists for removing
2284 watchpoints. It's not clear that it's necessary... */
2285 && bl->owner->disposition != disp_del_at_next_stop)
2286 {
2287 gdb_assert (bl->owner->ops != NULL
2288 && bl->owner->ops->insert_location != NULL);
2289
2290 val = bl->owner->ops->insert_location (bl);
2291
2292 /* If trying to set a read-watchpoint, and it turns out it's not
2293 supported, try emulating one with an access watchpoint. */
2294 if (val == 1 && bl->watchpoint_type == hw_read)
2295 {
2296 struct bp_location *loc, **loc_temp;
2297
2298 /* But don't try to insert it, if there's already another
2299 hw_access location that would be considered a duplicate
2300 of this one. */
2301 ALL_BP_LOCATIONS (loc, loc_temp)
2302 if (loc != bl
2303 && loc->watchpoint_type == hw_access
2304 && watchpoint_locations_match (bl, loc))
2305 {
2306 bl->duplicate = 1;
2307 bl->inserted = 1;
2308 bl->target_info = loc->target_info;
2309 bl->watchpoint_type = hw_access;
2310 val = 0;
2311 break;
2312 }
2313
2314 if (val == 1)
2315 {
2316 bl->watchpoint_type = hw_access;
2317 val = bl->owner->ops->insert_location (bl);
2318
2319 if (val)
2320 /* Back to the original value. */
2321 bl->watchpoint_type = hw_read;
2322 }
2323 }
2324
2325 bl->inserted = (val == 0);
2326 }
2327
2328 else if (bl->owner->type == bp_catchpoint)
2329 {
2330 gdb_assert (bl->owner->ops != NULL
2331 && bl->owner->ops->insert_location != NULL);
2332
2333 val = bl->owner->ops->insert_location (bl);
2334 if (val)
2335 {
2336 bl->owner->enable_state = bp_disabled;
2337
2338 if (val == 1)
2339 warning (_("\
2340 Error inserting catchpoint %d: Your system does not support this type\n\
2341 of catchpoint."), bl->owner->number);
2342 else
2343 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2344 }
2345
2346 bl->inserted = (val == 0);
2347
2348 /* We've already printed an error message if there was a problem
2349 inserting this catchpoint, and we've disabled the catchpoint,
2350 so just return success. */
2351 return 0;
2352 }
2353
2354 return 0;
2355 }
2356
2357 /* This function is called when program space PSPACE is about to be
2358 deleted. It takes care of updating breakpoints to not reference
2359 PSPACE anymore. */
2360
2361 void
2362 breakpoint_program_space_exit (struct program_space *pspace)
2363 {
2364 struct breakpoint *b, *b_temp;
2365 struct bp_location *loc, **loc_temp;
2366
2367 /* Remove any breakpoint that was set through this program space. */
2368 ALL_BREAKPOINTS_SAFE (b, b_temp)
2369 {
2370 if (b->pspace == pspace)
2371 delete_breakpoint (b);
2372 }
2373
2374 /* Breakpoints set through other program spaces could have locations
2375 bound to PSPACE as well. Remove those. */
2376 ALL_BP_LOCATIONS (loc, loc_temp)
2377 {
2378 struct bp_location *tmp;
2379
2380 if (loc->pspace == pspace)
2381 {
2382 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2383 if (loc->owner->loc == loc)
2384 loc->owner->loc = loc->next;
2385 else
2386 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2387 if (tmp->next == loc)
2388 {
2389 tmp->next = loc->next;
2390 break;
2391 }
2392 }
2393 }
2394
2395 /* Now update the global location list to permanently delete the
2396 removed locations above. */
2397 update_global_location_list (0);
2398 }
2399
2400 /* Make sure all breakpoints are inserted in inferior.
2401 Throws exception on any error.
2402 A breakpoint that is already inserted won't be inserted
2403 again, so calling this function twice is safe. */
2404 void
2405 insert_breakpoints (void)
2406 {
2407 struct breakpoint *bpt;
2408
2409 ALL_BREAKPOINTS (bpt)
2410 if (is_hardware_watchpoint (bpt))
2411 {
2412 struct watchpoint *w = (struct watchpoint *) bpt;
2413
2414 update_watchpoint (w, 0 /* don't reparse. */);
2415 }
2416
2417 update_global_location_list (1);
2418
2419 /* update_global_location_list does not insert breakpoints when
2420 always_inserted_mode is not enabled. Explicitly insert them
2421 now. */
2422 if (!breakpoints_always_inserted_mode ())
2423 insert_breakpoint_locations ();
2424 }
2425
2426 /* Invoke CALLBACK for each of bp_location. */
2427
2428 void
2429 iterate_over_bp_locations (walk_bp_location_callback callback)
2430 {
2431 struct bp_location *loc, **loc_tmp;
2432
2433 ALL_BP_LOCATIONS (loc, loc_tmp)
2434 {
2435 callback (loc, NULL);
2436 }
2437 }
2438
2439 /* This is used when we need to synch breakpoint conditions between GDB and the
2440 target. It is the case with deleting and disabling of breakpoints when using
2441 always-inserted mode. */
2442
2443 static void
2444 update_inserted_breakpoint_locations (void)
2445 {
2446 struct bp_location *bl, **blp_tmp;
2447 int error_flag = 0;
2448 int val = 0;
2449 int disabled_breaks = 0;
2450 int hw_breakpoint_error = 0;
2451
2452 struct ui_file *tmp_error_stream = mem_fileopen ();
2453 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2454
2455 /* Explicitly mark the warning -- this will only be printed if
2456 there was an error. */
2457 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2458
2459 save_current_space_and_thread ();
2460
2461 ALL_BP_LOCATIONS (bl, blp_tmp)
2462 {
2463 /* We only want to update software breakpoints and hardware
2464 breakpoints. */
2465 if (!is_breakpoint (bl->owner))
2466 continue;
2467
2468 /* We only want to update locations that are already inserted
2469 and need updating. This is to avoid unwanted insertion during
2470 deletion of breakpoints. */
2471 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2472 continue;
2473
2474 switch_to_program_space_and_thread (bl->pspace);
2475
2476 /* For targets that support global breakpoints, there's no need
2477 to select an inferior to insert breakpoint to. In fact, even
2478 if we aren't attached to any process yet, we should still
2479 insert breakpoints. */
2480 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2481 && ptid_equal (inferior_ptid, null_ptid))
2482 continue;
2483
2484 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2485 &hw_breakpoint_error);
2486 if (val)
2487 error_flag = val;
2488 }
2489
2490 if (error_flag)
2491 {
2492 target_terminal_ours_for_output ();
2493 error_stream (tmp_error_stream);
2494 }
2495
2496 do_cleanups (cleanups);
2497 }
2498
2499 /* Used when starting or continuing the program. */
2500
2501 static void
2502 insert_breakpoint_locations (void)
2503 {
2504 struct breakpoint *bpt;
2505 struct bp_location *bl, **blp_tmp;
2506 int error_flag = 0;
2507 int val = 0;
2508 int disabled_breaks = 0;
2509 int hw_breakpoint_error = 0;
2510
2511 struct ui_file *tmp_error_stream = mem_fileopen ();
2512 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2513
2514 /* Explicitly mark the warning -- this will only be printed if
2515 there was an error. */
2516 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2517
2518 save_current_space_and_thread ();
2519
2520 ALL_BP_LOCATIONS (bl, blp_tmp)
2521 {
2522 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2523 continue;
2524
2525 /* There is no point inserting thread-specific breakpoints if
2526 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2527 has BL->OWNER always non-NULL. */
2528 if (bl->owner->thread != -1
2529 && !valid_thread_id (bl->owner->thread))
2530 continue;
2531
2532 switch_to_program_space_and_thread (bl->pspace);
2533
2534 /* For targets that support global breakpoints, there's no need
2535 to select an inferior to insert breakpoint to. In fact, even
2536 if we aren't attached to any process yet, we should still
2537 insert breakpoints. */
2538 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2539 && ptid_equal (inferior_ptid, null_ptid))
2540 continue;
2541
2542 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2543 &hw_breakpoint_error);
2544 if (val)
2545 error_flag = val;
2546 }
2547
2548 /* If we failed to insert all locations of a watchpoint, remove
2549 them, as half-inserted watchpoint is of limited use. */
2550 ALL_BREAKPOINTS (bpt)
2551 {
2552 int some_failed = 0;
2553 struct bp_location *loc;
2554
2555 if (!is_hardware_watchpoint (bpt))
2556 continue;
2557
2558 if (!breakpoint_enabled (bpt))
2559 continue;
2560
2561 if (bpt->disposition == disp_del_at_next_stop)
2562 continue;
2563
2564 for (loc = bpt->loc; loc; loc = loc->next)
2565 if (!loc->inserted && should_be_inserted (loc))
2566 {
2567 some_failed = 1;
2568 break;
2569 }
2570 if (some_failed)
2571 {
2572 for (loc = bpt->loc; loc; loc = loc->next)
2573 if (loc->inserted)
2574 remove_breakpoint (loc, mark_uninserted);
2575
2576 hw_breakpoint_error = 1;
2577 fprintf_unfiltered (tmp_error_stream,
2578 "Could not insert hardware watchpoint %d.\n",
2579 bpt->number);
2580 error_flag = -1;
2581 }
2582 }
2583
2584 if (error_flag)
2585 {
2586 /* If a hardware breakpoint or watchpoint was inserted, add a
2587 message about possibly exhausted resources. */
2588 if (hw_breakpoint_error)
2589 {
2590 fprintf_unfiltered (tmp_error_stream,
2591 "Could not insert hardware breakpoints:\n\
2592 You may have requested too many hardware breakpoints/watchpoints.\n");
2593 }
2594 target_terminal_ours_for_output ();
2595 error_stream (tmp_error_stream);
2596 }
2597
2598 do_cleanups (cleanups);
2599 }
2600
2601 /* Used when the program stops.
2602 Returns zero if successful, or non-zero if there was a problem
2603 removing a breakpoint location. */
2604
2605 int
2606 remove_breakpoints (void)
2607 {
2608 struct bp_location *bl, **blp_tmp;
2609 int val = 0;
2610
2611 ALL_BP_LOCATIONS (bl, blp_tmp)
2612 {
2613 if (bl->inserted && !is_tracepoint (bl->owner))
2614 val |= remove_breakpoint (bl, mark_uninserted);
2615 }
2616 return val;
2617 }
2618
2619 /* Remove breakpoints of process PID. */
2620
2621 int
2622 remove_breakpoints_pid (int pid)
2623 {
2624 struct bp_location *bl, **blp_tmp;
2625 int val;
2626 struct inferior *inf = find_inferior_pid (pid);
2627
2628 ALL_BP_LOCATIONS (bl, blp_tmp)
2629 {
2630 if (bl->pspace != inf->pspace)
2631 continue;
2632
2633 if (bl->inserted)
2634 {
2635 val = remove_breakpoint (bl, mark_uninserted);
2636 if (val != 0)
2637 return val;
2638 }
2639 }
2640 return 0;
2641 }
2642
2643 int
2644 reattach_breakpoints (int pid)
2645 {
2646 struct cleanup *old_chain;
2647 struct bp_location *bl, **blp_tmp;
2648 int val;
2649 struct ui_file *tmp_error_stream;
2650 int dummy1 = 0, dummy2 = 0;
2651 struct inferior *inf;
2652 struct thread_info *tp;
2653
2654 tp = any_live_thread_of_process (pid);
2655 if (tp == NULL)
2656 return 1;
2657
2658 inf = find_inferior_pid (pid);
2659 old_chain = save_inferior_ptid ();
2660
2661 inferior_ptid = tp->ptid;
2662
2663 tmp_error_stream = mem_fileopen ();
2664 make_cleanup_ui_file_delete (tmp_error_stream);
2665
2666 ALL_BP_LOCATIONS (bl, blp_tmp)
2667 {
2668 if (bl->pspace != inf->pspace)
2669 continue;
2670
2671 if (bl->inserted)
2672 {
2673 bl->inserted = 0;
2674 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2675 if (val != 0)
2676 {
2677 do_cleanups (old_chain);
2678 return val;
2679 }
2680 }
2681 }
2682 do_cleanups (old_chain);
2683 return 0;
2684 }
2685
2686 static int internal_breakpoint_number = -1;
2687
2688 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2689 If INTERNAL is non-zero, the breakpoint number will be populated
2690 from internal_breakpoint_number and that variable decremented.
2691 Otherwise the breakpoint number will be populated from
2692 breakpoint_count and that value incremented. Internal breakpoints
2693 do not set the internal var bpnum. */
2694 static void
2695 set_breakpoint_number (int internal, struct breakpoint *b)
2696 {
2697 if (internal)
2698 b->number = internal_breakpoint_number--;
2699 else
2700 {
2701 set_breakpoint_count (breakpoint_count + 1);
2702 b->number = breakpoint_count;
2703 }
2704 }
2705
2706 static struct breakpoint *
2707 create_internal_breakpoint (struct gdbarch *gdbarch,
2708 CORE_ADDR address, enum bptype type,
2709 const struct breakpoint_ops *ops)
2710 {
2711 struct symtab_and_line sal;
2712 struct breakpoint *b;
2713
2714 init_sal (&sal); /* Initialize to zeroes. */
2715
2716 sal.pc = address;
2717 sal.section = find_pc_overlay (sal.pc);
2718 sal.pspace = current_program_space;
2719
2720 b = set_raw_breakpoint (gdbarch, sal, type, ops);
2721 b->number = internal_breakpoint_number--;
2722 b->disposition = disp_donttouch;
2723
2724 return b;
2725 }
2726
2727 static const char *const longjmp_names[] =
2728 {
2729 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2730 };
2731 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2732
2733 /* Per-objfile data private to breakpoint.c. */
2734 struct breakpoint_objfile_data
2735 {
2736 /* Minimal symbol for "_ovly_debug_event" (if any). */
2737 struct minimal_symbol *overlay_msym;
2738
2739 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2740 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2741
2742 /* Minimal symbol for "std::terminate()" (if any). */
2743 struct minimal_symbol *terminate_msym;
2744
2745 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2746 struct minimal_symbol *exception_msym;
2747 };
2748
2749 static const struct objfile_data *breakpoint_objfile_key;
2750
2751 /* Minimal symbol not found sentinel. */
2752 static struct minimal_symbol msym_not_found;
2753
2754 /* Returns TRUE if MSYM point to the "not found" sentinel. */
2755
2756 static int
2757 msym_not_found_p (const struct minimal_symbol *msym)
2758 {
2759 return msym == &msym_not_found;
2760 }
2761
2762 /* Return per-objfile data needed by breakpoint.c.
2763 Allocate the data if necessary. */
2764
2765 static struct breakpoint_objfile_data *
2766 get_breakpoint_objfile_data (struct objfile *objfile)
2767 {
2768 struct breakpoint_objfile_data *bp_objfile_data;
2769
2770 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2771 if (bp_objfile_data == NULL)
2772 {
2773 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2774 sizeof (*bp_objfile_data));
2775
2776 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2777 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2778 }
2779 return bp_objfile_data;
2780 }
2781
2782 static void
2783 create_overlay_event_breakpoint (void)
2784 {
2785 struct objfile *objfile;
2786 const char *const func_name = "_ovly_debug_event";
2787
2788 ALL_OBJFILES (objfile)
2789 {
2790 struct breakpoint *b;
2791 struct breakpoint_objfile_data *bp_objfile_data;
2792 CORE_ADDR addr;
2793
2794 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2795
2796 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2797 continue;
2798
2799 if (bp_objfile_data->overlay_msym == NULL)
2800 {
2801 struct minimal_symbol *m;
2802
2803 m = lookup_minimal_symbol_text (func_name, objfile);
2804 if (m == NULL)
2805 {
2806 /* Avoid future lookups in this objfile. */
2807 bp_objfile_data->overlay_msym = &msym_not_found;
2808 continue;
2809 }
2810 bp_objfile_data->overlay_msym = m;
2811 }
2812
2813 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2814 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2815 bp_overlay_event,
2816 &internal_breakpoint_ops);
2817 b->addr_string = xstrdup (func_name);
2818
2819 if (overlay_debugging == ovly_auto)
2820 {
2821 b->enable_state = bp_enabled;
2822 overlay_events_enabled = 1;
2823 }
2824 else
2825 {
2826 b->enable_state = bp_disabled;
2827 overlay_events_enabled = 0;
2828 }
2829 }
2830 update_global_location_list (1);
2831 }
2832
2833 static void
2834 create_longjmp_master_breakpoint (void)
2835 {
2836 struct program_space *pspace;
2837 struct cleanup *old_chain;
2838
2839 old_chain = save_current_program_space ();
2840
2841 ALL_PSPACES (pspace)
2842 {
2843 struct objfile *objfile;
2844
2845 set_current_program_space (pspace);
2846
2847 ALL_OBJFILES (objfile)
2848 {
2849 int i;
2850 struct gdbarch *gdbarch;
2851 struct breakpoint_objfile_data *bp_objfile_data;
2852
2853 gdbarch = get_objfile_arch (objfile);
2854 if (!gdbarch_get_longjmp_target_p (gdbarch))
2855 continue;
2856
2857 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2858
2859 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2860 {
2861 struct breakpoint *b;
2862 const char *func_name;
2863 CORE_ADDR addr;
2864
2865 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2866 continue;
2867
2868 func_name = longjmp_names[i];
2869 if (bp_objfile_data->longjmp_msym[i] == NULL)
2870 {
2871 struct minimal_symbol *m;
2872
2873 m = lookup_minimal_symbol_text (func_name, objfile);
2874 if (m == NULL)
2875 {
2876 /* Prevent future lookups in this objfile. */
2877 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2878 continue;
2879 }
2880 bp_objfile_data->longjmp_msym[i] = m;
2881 }
2882
2883 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2884 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2885 &internal_breakpoint_ops);
2886 b->addr_string = xstrdup (func_name);
2887 b->enable_state = bp_disabled;
2888 }
2889 }
2890 }
2891 update_global_location_list (1);
2892
2893 do_cleanups (old_chain);
2894 }
2895
2896 /* Create a master std::terminate breakpoint. */
2897 static void
2898 create_std_terminate_master_breakpoint (void)
2899 {
2900 struct program_space *pspace;
2901 struct cleanup *old_chain;
2902 const char *const func_name = "std::terminate()";
2903
2904 old_chain = save_current_program_space ();
2905
2906 ALL_PSPACES (pspace)
2907 {
2908 struct objfile *objfile;
2909 CORE_ADDR addr;
2910
2911 set_current_program_space (pspace);
2912
2913 ALL_OBJFILES (objfile)
2914 {
2915 struct breakpoint *b;
2916 struct breakpoint_objfile_data *bp_objfile_data;
2917
2918 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2919
2920 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2921 continue;
2922
2923 if (bp_objfile_data->terminate_msym == NULL)
2924 {
2925 struct minimal_symbol *m;
2926
2927 m = lookup_minimal_symbol (func_name, NULL, objfile);
2928 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2929 && MSYMBOL_TYPE (m) != mst_file_text))
2930 {
2931 /* Prevent future lookups in this objfile. */
2932 bp_objfile_data->terminate_msym = &msym_not_found;
2933 continue;
2934 }
2935 bp_objfile_data->terminate_msym = m;
2936 }
2937
2938 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2939 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2940 bp_std_terminate_master,
2941 &internal_breakpoint_ops);
2942 b->addr_string = xstrdup (func_name);
2943 b->enable_state = bp_disabled;
2944 }
2945 }
2946
2947 update_global_location_list (1);
2948
2949 do_cleanups (old_chain);
2950 }
2951
2952 /* Install a master breakpoint on the unwinder's debug hook. */
2953
2954 static void
2955 create_exception_master_breakpoint (void)
2956 {
2957 struct objfile *objfile;
2958 const char *const func_name = "_Unwind_DebugHook";
2959
2960 ALL_OBJFILES (objfile)
2961 {
2962 struct breakpoint *b;
2963 struct gdbarch *gdbarch;
2964 struct breakpoint_objfile_data *bp_objfile_data;
2965 CORE_ADDR addr;
2966
2967 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2968
2969 if (msym_not_found_p (bp_objfile_data->exception_msym))
2970 continue;
2971
2972 gdbarch = get_objfile_arch (objfile);
2973
2974 if (bp_objfile_data->exception_msym == NULL)
2975 {
2976 struct minimal_symbol *debug_hook;
2977
2978 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2979 if (debug_hook == NULL)
2980 {
2981 bp_objfile_data->exception_msym = &msym_not_found;
2982 continue;
2983 }
2984
2985 bp_objfile_data->exception_msym = debug_hook;
2986 }
2987
2988 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2989 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2990 &current_target);
2991 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2992 &internal_breakpoint_ops);
2993 b->addr_string = xstrdup (func_name);
2994 b->enable_state = bp_disabled;
2995 }
2996
2997 update_global_location_list (1);
2998 }
2999
3000 void
3001 update_breakpoints_after_exec (void)
3002 {
3003 struct breakpoint *b, *b_tmp;
3004 struct bp_location *bploc, **bplocp_tmp;
3005
3006 /* We're about to delete breakpoints from GDB's lists. If the
3007 INSERTED flag is true, GDB will try to lift the breakpoints by
3008 writing the breakpoints' "shadow contents" back into memory. The
3009 "shadow contents" are NOT valid after an exec, so GDB should not
3010 do that. Instead, the target is responsible from marking
3011 breakpoints out as soon as it detects an exec. We don't do that
3012 here instead, because there may be other attempts to delete
3013 breakpoints after detecting an exec and before reaching here. */
3014 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3015 if (bploc->pspace == current_program_space)
3016 gdb_assert (!bploc->inserted);
3017
3018 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3019 {
3020 if (b->pspace != current_program_space)
3021 continue;
3022
3023 /* Solib breakpoints must be explicitly reset after an exec(). */
3024 if (b->type == bp_shlib_event)
3025 {
3026 delete_breakpoint (b);
3027 continue;
3028 }
3029
3030 /* JIT breakpoints must be explicitly reset after an exec(). */
3031 if (b->type == bp_jit_event)
3032 {
3033 delete_breakpoint (b);
3034 continue;
3035 }
3036
3037 /* Thread event breakpoints must be set anew after an exec(),
3038 as must overlay event and longjmp master breakpoints. */
3039 if (b->type == bp_thread_event || b->type == bp_overlay_event
3040 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3041 || b->type == bp_exception_master)
3042 {
3043 delete_breakpoint (b);
3044 continue;
3045 }
3046
3047 /* Step-resume breakpoints are meaningless after an exec(). */
3048 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3049 {
3050 delete_breakpoint (b);
3051 continue;
3052 }
3053
3054 /* Longjmp and longjmp-resume breakpoints are also meaningless
3055 after an exec. */
3056 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3057 || b->type == bp_exception || b->type == bp_exception_resume)
3058 {
3059 delete_breakpoint (b);
3060 continue;
3061 }
3062
3063 if (b->type == bp_catchpoint)
3064 {
3065 /* For now, none of the bp_catchpoint breakpoints need to
3066 do anything at this point. In the future, if some of
3067 the catchpoints need to something, we will need to add
3068 a new method, and call this method from here. */
3069 continue;
3070 }
3071
3072 /* bp_finish is a special case. The only way we ought to be able
3073 to see one of these when an exec() has happened, is if the user
3074 caught a vfork, and then said "finish". Ordinarily a finish just
3075 carries them to the call-site of the current callee, by setting
3076 a temporary bp there and resuming. But in this case, the finish
3077 will carry them entirely through the vfork & exec.
3078
3079 We don't want to allow a bp_finish to remain inserted now. But
3080 we can't safely delete it, 'cause finish_command has a handle to
3081 the bp on a bpstat, and will later want to delete it. There's a
3082 chance (and I've seen it happen) that if we delete the bp_finish
3083 here, that its storage will get reused by the time finish_command
3084 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3085 We really must allow finish_command to delete a bp_finish.
3086
3087 In the absence of a general solution for the "how do we know
3088 it's safe to delete something others may have handles to?"
3089 problem, what we'll do here is just uninsert the bp_finish, and
3090 let finish_command delete it.
3091
3092 (We know the bp_finish is "doomed" in the sense that it's
3093 momentary, and will be deleted as soon as finish_command sees
3094 the inferior stopped. So it doesn't matter that the bp's
3095 address is probably bogus in the new a.out, unlike e.g., the
3096 solib breakpoints.) */
3097
3098 if (b->type == bp_finish)
3099 {
3100 continue;
3101 }
3102
3103 /* Without a symbolic address, we have little hope of the
3104 pre-exec() address meaning the same thing in the post-exec()
3105 a.out. */
3106 if (b->addr_string == NULL)
3107 {
3108 delete_breakpoint (b);
3109 continue;
3110 }
3111 }
3112 /* FIXME what about longjmp breakpoints? Re-create them here? */
3113 create_overlay_event_breakpoint ();
3114 create_longjmp_master_breakpoint ();
3115 create_std_terminate_master_breakpoint ();
3116 create_exception_master_breakpoint ();
3117 }
3118
3119 int
3120 detach_breakpoints (int pid)
3121 {
3122 struct bp_location *bl, **blp_tmp;
3123 int val = 0;
3124 struct cleanup *old_chain = save_inferior_ptid ();
3125 struct inferior *inf = current_inferior ();
3126
3127 if (pid == PIDGET (inferior_ptid))
3128 error (_("Cannot detach breakpoints of inferior_ptid"));
3129
3130 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3131 inferior_ptid = pid_to_ptid (pid);
3132 ALL_BP_LOCATIONS (bl, blp_tmp)
3133 {
3134 if (bl->pspace != inf->pspace)
3135 continue;
3136
3137 if (bl->inserted)
3138 val |= remove_breakpoint_1 (bl, mark_inserted);
3139 }
3140
3141 /* Detach single-step breakpoints as well. */
3142 detach_single_step_breakpoints ();
3143
3144 do_cleanups (old_chain);
3145 return val;
3146 }
3147
3148 /* Remove the breakpoint location BL from the current address space.
3149 Note that this is used to detach breakpoints from a child fork.
3150 When we get here, the child isn't in the inferior list, and neither
3151 do we have objects to represent its address space --- we should
3152 *not* look at bl->pspace->aspace here. */
3153
3154 static int
3155 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3156 {
3157 int val;
3158
3159 /* BL is never in moribund_locations by our callers. */
3160 gdb_assert (bl->owner != NULL);
3161
3162 if (bl->owner->enable_state == bp_permanent)
3163 /* Permanent breakpoints cannot be inserted or removed. */
3164 return 0;
3165
3166 /* The type of none suggests that owner is actually deleted.
3167 This should not ever happen. */
3168 gdb_assert (bl->owner->type != bp_none);
3169
3170 if (bl->loc_type == bp_loc_software_breakpoint
3171 || bl->loc_type == bp_loc_hardware_breakpoint)
3172 {
3173 /* "Normal" instruction breakpoint: either the standard
3174 trap-instruction bp (bp_breakpoint), or a
3175 bp_hardware_breakpoint. */
3176
3177 /* First check to see if we have to handle an overlay. */
3178 if (overlay_debugging == ovly_off
3179 || bl->section == NULL
3180 || !(section_is_overlay (bl->section)))
3181 {
3182 /* No overlay handling: just remove the breakpoint. */
3183 val = bl->owner->ops->remove_location (bl);
3184 }
3185 else
3186 {
3187 /* This breakpoint is in an overlay section.
3188 Did we set a breakpoint at the LMA? */
3189 if (!overlay_events_enabled)
3190 {
3191 /* Yes -- overlay event support is not active, so we
3192 should have set a breakpoint at the LMA. Remove it.
3193 */
3194 /* Ignore any failures: if the LMA is in ROM, we will
3195 have already warned when we failed to insert it. */
3196 if (bl->loc_type == bp_loc_hardware_breakpoint)
3197 target_remove_hw_breakpoint (bl->gdbarch,
3198 &bl->overlay_target_info);
3199 else
3200 target_remove_breakpoint (bl->gdbarch,
3201 &bl->overlay_target_info);
3202 }
3203 /* Did we set a breakpoint at the VMA?
3204 If so, we will have marked the breakpoint 'inserted'. */
3205 if (bl->inserted)
3206 {
3207 /* Yes -- remove it. Previously we did not bother to
3208 remove the breakpoint if the section had been
3209 unmapped, but let's not rely on that being safe. We
3210 don't know what the overlay manager might do. */
3211
3212 /* However, we should remove *software* breakpoints only
3213 if the section is still mapped, or else we overwrite
3214 wrong code with the saved shadow contents. */
3215 if (bl->loc_type == bp_loc_hardware_breakpoint
3216 || section_is_mapped (bl->section))
3217 val = bl->owner->ops->remove_location (bl);
3218 else
3219 val = 0;
3220 }
3221 else
3222 {
3223 /* No -- not inserted, so no need to remove. No error. */
3224 val = 0;
3225 }
3226 }
3227
3228 /* In some cases, we might not be able to remove a breakpoint
3229 in a shared library that has already been removed, but we
3230 have not yet processed the shlib unload event. */
3231 if (val && solib_name_from_address (bl->pspace, bl->address))
3232 val = 0;
3233
3234 if (val)
3235 return val;
3236 bl->inserted = (is == mark_inserted);
3237 }
3238 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3239 {
3240 gdb_assert (bl->owner->ops != NULL
3241 && bl->owner->ops->remove_location != NULL);
3242
3243 bl->inserted = (is == mark_inserted);
3244 bl->owner->ops->remove_location (bl);
3245
3246 /* Failure to remove any of the hardware watchpoints comes here. */
3247 if ((is == mark_uninserted) && (bl->inserted))
3248 warning (_("Could not remove hardware watchpoint %d."),
3249 bl->owner->number);
3250 }
3251 else if (bl->owner->type == bp_catchpoint
3252 && breakpoint_enabled (bl->owner)
3253 && !bl->duplicate)
3254 {
3255 gdb_assert (bl->owner->ops != NULL
3256 && bl->owner->ops->remove_location != NULL);
3257
3258 val = bl->owner->ops->remove_location (bl);
3259 if (val)
3260 return val;
3261
3262 bl->inserted = (is == mark_inserted);
3263 }
3264
3265 return 0;
3266 }
3267
3268 static int
3269 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3270 {
3271 int ret;
3272 struct cleanup *old_chain;
3273
3274 /* BL is never in moribund_locations by our callers. */
3275 gdb_assert (bl->owner != NULL);
3276
3277 if (bl->owner->enable_state == bp_permanent)
3278 /* Permanent breakpoints cannot be inserted or removed. */
3279 return 0;
3280
3281 /* The type of none suggests that owner is actually deleted.
3282 This should not ever happen. */
3283 gdb_assert (bl->owner->type != bp_none);
3284
3285 old_chain = save_current_space_and_thread ();
3286
3287 switch_to_program_space_and_thread (bl->pspace);
3288
3289 ret = remove_breakpoint_1 (bl, is);
3290
3291 do_cleanups (old_chain);
3292 return ret;
3293 }
3294
3295 /* Clear the "inserted" flag in all breakpoints. */
3296
3297 void
3298 mark_breakpoints_out (void)
3299 {
3300 struct bp_location *bl, **blp_tmp;
3301
3302 ALL_BP_LOCATIONS (bl, blp_tmp)
3303 if (bl->pspace == current_program_space)
3304 bl->inserted = 0;
3305 }
3306
3307 /* Clear the "inserted" flag in all breakpoints and delete any
3308 breakpoints which should go away between runs of the program.
3309
3310 Plus other such housekeeping that has to be done for breakpoints
3311 between runs.
3312
3313 Note: this function gets called at the end of a run (by
3314 generic_mourn_inferior) and when a run begins (by
3315 init_wait_for_inferior). */
3316
3317
3318
3319 void
3320 breakpoint_init_inferior (enum inf_context context)
3321 {
3322 struct breakpoint *b, *b_tmp;
3323 struct bp_location *bl, **blp_tmp;
3324 int ix;
3325 struct program_space *pspace = current_program_space;
3326
3327 /* If breakpoint locations are shared across processes, then there's
3328 nothing to do. */
3329 if (gdbarch_has_global_breakpoints (target_gdbarch))
3330 return;
3331
3332 ALL_BP_LOCATIONS (bl, blp_tmp)
3333 {
3334 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3335 if (bl->pspace == pspace
3336 && bl->owner->enable_state != bp_permanent)
3337 bl->inserted = 0;
3338 }
3339
3340 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3341 {
3342 if (b->loc && b->loc->pspace != pspace)
3343 continue;
3344
3345 switch (b->type)
3346 {
3347 case bp_call_dummy:
3348
3349 /* If the call dummy breakpoint is at the entry point it will
3350 cause problems when the inferior is rerun, so we better get
3351 rid of it. */
3352
3353 case bp_watchpoint_scope:
3354
3355 /* Also get rid of scope breakpoints. */
3356
3357 case bp_shlib_event:
3358
3359 /* Also remove solib event breakpoints. Their addresses may
3360 have changed since the last time we ran the program.
3361 Actually we may now be debugging against different target;
3362 and so the solib backend that installed this breakpoint may
3363 not be used in by the target. E.g.,
3364
3365 (gdb) file prog-linux
3366 (gdb) run # native linux target
3367 ...
3368 (gdb) kill
3369 (gdb) file prog-win.exe
3370 (gdb) tar rem :9999 # remote Windows gdbserver.
3371 */
3372
3373 case bp_step_resume:
3374
3375 /* Also remove step-resume breakpoints. */
3376
3377 delete_breakpoint (b);
3378 break;
3379
3380 case bp_watchpoint:
3381 case bp_hardware_watchpoint:
3382 case bp_read_watchpoint:
3383 case bp_access_watchpoint:
3384 {
3385 struct watchpoint *w = (struct watchpoint *) b;
3386
3387 /* Likewise for watchpoints on local expressions. */
3388 if (w->exp_valid_block != NULL)
3389 delete_breakpoint (b);
3390 else if (context == inf_starting)
3391 {
3392 /* Reset val field to force reread of starting value in
3393 insert_breakpoints. */
3394 if (w->val)
3395 value_free (w->val);
3396 w->val = NULL;
3397 w->val_valid = 0;
3398 }
3399 }
3400 break;
3401 default:
3402 break;
3403 }
3404 }
3405
3406 /* Get rid of the moribund locations. */
3407 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3408 decref_bp_location (&bl);
3409 VEC_free (bp_location_p, moribund_locations);
3410 }
3411
3412 /* These functions concern about actual breakpoints inserted in the
3413 target --- to e.g. check if we need to do decr_pc adjustment or if
3414 we need to hop over the bkpt --- so we check for address space
3415 match, not program space. */
3416
3417 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3418 exists at PC. It returns ordinary_breakpoint_here if it's an
3419 ordinary breakpoint, or permanent_breakpoint_here if it's a
3420 permanent breakpoint.
3421 - When continuing from a location with an ordinary breakpoint, we
3422 actually single step once before calling insert_breakpoints.
3423 - When continuing from a location with a permanent breakpoint, we
3424 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3425 the target, to advance the PC past the breakpoint. */
3426
3427 enum breakpoint_here
3428 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3429 {
3430 struct bp_location *bl, **blp_tmp;
3431 int any_breakpoint_here = 0;
3432
3433 ALL_BP_LOCATIONS (bl, blp_tmp)
3434 {
3435 if (bl->loc_type != bp_loc_software_breakpoint
3436 && bl->loc_type != bp_loc_hardware_breakpoint)
3437 continue;
3438
3439 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3440 if ((breakpoint_enabled (bl->owner)
3441 || bl->owner->enable_state == bp_permanent)
3442 && breakpoint_location_address_match (bl, aspace, pc))
3443 {
3444 if (overlay_debugging
3445 && section_is_overlay (bl->section)
3446 && !section_is_mapped (bl->section))
3447 continue; /* unmapped overlay -- can't be a match */
3448 else if (bl->owner->enable_state == bp_permanent)
3449 return permanent_breakpoint_here;
3450 else
3451 any_breakpoint_here = 1;
3452 }
3453 }
3454
3455 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3456 }
3457
3458 /* Return true if there's a moribund breakpoint at PC. */
3459
3460 int
3461 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3462 {
3463 struct bp_location *loc;
3464 int ix;
3465
3466 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3467 if (breakpoint_location_address_match (loc, aspace, pc))
3468 return 1;
3469
3470 return 0;
3471 }
3472
3473 /* Returns non-zero if there's a breakpoint inserted at PC, which is
3474 inserted using regular breakpoint_chain / bp_location array
3475 mechanism. This does not check for single-step breakpoints, which
3476 are inserted and removed using direct target manipulation. */
3477
3478 int
3479 regular_breakpoint_inserted_here_p (struct address_space *aspace,
3480 CORE_ADDR pc)
3481 {
3482 struct bp_location *bl, **blp_tmp;
3483
3484 ALL_BP_LOCATIONS (bl, blp_tmp)
3485 {
3486 if (bl->loc_type != bp_loc_software_breakpoint
3487 && bl->loc_type != bp_loc_hardware_breakpoint)
3488 continue;
3489
3490 if (bl->inserted
3491 && breakpoint_location_address_match (bl, aspace, pc))
3492 {
3493 if (overlay_debugging
3494 && section_is_overlay (bl->section)
3495 && !section_is_mapped (bl->section))
3496 continue; /* unmapped overlay -- can't be a match */
3497 else
3498 return 1;
3499 }
3500 }
3501 return 0;
3502 }
3503
3504 /* Returns non-zero iff there's either regular breakpoint
3505 or a single step breakpoint inserted at PC. */
3506
3507 int
3508 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
3509 {
3510 if (regular_breakpoint_inserted_here_p (aspace, pc))
3511 return 1;
3512
3513 if (single_step_breakpoint_inserted_here_p (aspace, pc))
3514 return 1;
3515
3516 return 0;
3517 }
3518
3519 /* This function returns non-zero iff there is a software breakpoint
3520 inserted at PC. */
3521
3522 int
3523 software_breakpoint_inserted_here_p (struct address_space *aspace,
3524 CORE_ADDR pc)
3525 {
3526 struct bp_location *bl, **blp_tmp;
3527
3528 ALL_BP_LOCATIONS (bl, blp_tmp)
3529 {
3530 if (bl->loc_type != bp_loc_software_breakpoint)
3531 continue;
3532
3533 if (bl->inserted
3534 && breakpoint_address_match (bl->pspace->aspace, bl->address,
3535 aspace, pc))
3536 {
3537 if (overlay_debugging
3538 && section_is_overlay (bl->section)
3539 && !section_is_mapped (bl->section))
3540 continue; /* unmapped overlay -- can't be a match */
3541 else
3542 return 1;
3543 }
3544 }
3545
3546 /* Also check for software single-step breakpoints. */
3547 if (single_step_breakpoint_inserted_here_p (aspace, pc))
3548 return 1;
3549
3550 return 0;
3551 }
3552
3553 int
3554 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3555 CORE_ADDR addr, ULONGEST len)
3556 {
3557 struct breakpoint *bpt;
3558
3559 ALL_BREAKPOINTS (bpt)
3560 {
3561 struct bp_location *loc;
3562
3563 if (bpt->type != bp_hardware_watchpoint
3564 && bpt->type != bp_access_watchpoint)
3565 continue;
3566
3567 if (!breakpoint_enabled (bpt))
3568 continue;
3569
3570 for (loc = bpt->loc; loc; loc = loc->next)
3571 if (loc->pspace->aspace == aspace && loc->inserted)
3572 {
3573 CORE_ADDR l, h;
3574
3575 /* Check for intersection. */
3576 l = max (loc->address, addr);
3577 h = min (loc->address + loc->length, addr + len);
3578 if (l < h)
3579 return 1;
3580 }
3581 }
3582 return 0;
3583 }
3584
3585 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3586 PC is valid for process/thread PTID. */
3587
3588 int
3589 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3590 ptid_t ptid)
3591 {
3592 struct bp_location *bl, **blp_tmp;
3593 /* The thread and task IDs associated to PTID, computed lazily. */
3594 int thread = -1;
3595 int task = 0;
3596
3597 ALL_BP_LOCATIONS (bl, blp_tmp)
3598 {
3599 if (bl->loc_type != bp_loc_software_breakpoint
3600 && bl->loc_type != bp_loc_hardware_breakpoint)
3601 continue;
3602
3603 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3604 if (!breakpoint_enabled (bl->owner)
3605 && bl->owner->enable_state != bp_permanent)
3606 continue;
3607
3608 if (!breakpoint_location_address_match (bl, aspace, pc))
3609 continue;
3610
3611 if (bl->owner->thread != -1)
3612 {
3613 /* This is a thread-specific breakpoint. Check that ptid
3614 matches that thread. If thread hasn't been computed yet,
3615 it is now time to do so. */
3616 if (thread == -1)
3617 thread = pid_to_thread_id (ptid);
3618 if (bl->owner->thread != thread)
3619 continue;
3620 }
3621
3622 if (bl->owner->task != 0)
3623 {
3624 /* This is a task-specific breakpoint. Check that ptid
3625 matches that task. If task hasn't been computed yet,
3626 it is now time to do so. */
3627 if (task == 0)
3628 task = ada_get_task_number (ptid);
3629 if (bl->owner->task != task)
3630 continue;
3631 }
3632
3633 if (overlay_debugging
3634 && section_is_overlay (bl->section)
3635 && !section_is_mapped (bl->section))
3636 continue; /* unmapped overlay -- can't be a match */
3637
3638 return 1;
3639 }
3640
3641 return 0;
3642 }
3643 \f
3644
3645 /* bpstat stuff. External routines' interfaces are documented
3646 in breakpoint.h. */
3647
3648 int
3649 is_catchpoint (struct breakpoint *ep)
3650 {
3651 return (ep->type == bp_catchpoint);
3652 }
3653
3654 /* Frees any storage that is part of a bpstat. Does not walk the
3655 'next' chain. */
3656
3657 static void
3658 bpstat_free (bpstat bs)
3659 {
3660 if (bs->old_val != NULL)
3661 value_free (bs->old_val);
3662 decref_counted_command_line (&bs->commands);
3663 decref_bp_location (&bs->bp_location_at);
3664 xfree (bs);
3665 }
3666
3667 /* Clear a bpstat so that it says we are not at any breakpoint.
3668 Also free any storage that is part of a bpstat. */
3669
3670 void
3671 bpstat_clear (bpstat *bsp)
3672 {
3673 bpstat p;
3674 bpstat q;
3675
3676 if (bsp == 0)
3677 return;
3678 p = *bsp;
3679 while (p != NULL)
3680 {
3681 q = p->next;
3682 bpstat_free (p);
3683 p = q;
3684 }
3685 *bsp = NULL;
3686 }
3687
3688 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3689 is part of the bpstat is copied as well. */
3690
3691 bpstat
3692 bpstat_copy (bpstat bs)
3693 {
3694 bpstat p = NULL;
3695 bpstat tmp;
3696 bpstat retval = NULL;
3697
3698 if (bs == NULL)
3699 return bs;
3700
3701 for (; bs != NULL; bs = bs->next)
3702 {
3703 tmp = (bpstat) xmalloc (sizeof (*tmp));
3704 memcpy (tmp, bs, sizeof (*tmp));
3705 incref_counted_command_line (tmp->commands);
3706 incref_bp_location (tmp->bp_location_at);
3707 if (bs->old_val != NULL)
3708 {
3709 tmp->old_val = value_copy (bs->old_val);
3710 release_value (tmp->old_val);
3711 }
3712
3713 if (p == NULL)
3714 /* This is the first thing in the chain. */
3715 retval = tmp;
3716 else
3717 p->next = tmp;
3718 p = tmp;
3719 }
3720 p->next = NULL;
3721 return retval;
3722 }
3723
3724 /* Find the bpstat associated with this breakpoint. */
3725
3726 bpstat
3727 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3728 {
3729 if (bsp == NULL)
3730 return NULL;
3731
3732 for (; bsp != NULL; bsp = bsp->next)
3733 {
3734 if (bsp->breakpoint_at == breakpoint)
3735 return bsp;
3736 }
3737 return NULL;
3738 }
3739
3740 /* Put in *NUM the breakpoint number of the first breakpoint we are
3741 stopped at. *BSP upon return is a bpstat which points to the
3742 remaining breakpoints stopped at (but which is not guaranteed to be
3743 good for anything but further calls to bpstat_num).
3744
3745 Return 0 if passed a bpstat which does not indicate any breakpoints.
3746 Return -1 if stopped at a breakpoint that has been deleted since
3747 we set it.
3748 Return 1 otherwise. */
3749
3750 int
3751 bpstat_num (bpstat *bsp, int *num)
3752 {
3753 struct breakpoint *b;
3754
3755 if ((*bsp) == NULL)
3756 return 0; /* No more breakpoint values */
3757
3758 /* We assume we'll never have several bpstats that correspond to a
3759 single breakpoint -- otherwise, this function might return the
3760 same number more than once and this will look ugly. */
3761 b = (*bsp)->breakpoint_at;
3762 *bsp = (*bsp)->next;
3763 if (b == NULL)
3764 return -1; /* breakpoint that's been deleted since */
3765
3766 *num = b->number; /* We have its number */
3767 return 1;
3768 }
3769
3770 /* See breakpoint.h. */
3771
3772 void
3773 bpstat_clear_actions (void)
3774 {
3775 struct thread_info *tp;
3776 bpstat bs;
3777
3778 if (ptid_equal (inferior_ptid, null_ptid))
3779 return;
3780
3781 tp = find_thread_ptid (inferior_ptid);
3782 if (tp == NULL)
3783 return;
3784
3785 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
3786 {
3787 decref_counted_command_line (&bs->commands);
3788
3789 if (bs->old_val != NULL)
3790 {
3791 value_free (bs->old_val);
3792 bs->old_val = NULL;
3793 }
3794 }
3795 }
3796
3797 /* Called when a command is about to proceed the inferior. */
3798
3799 static void
3800 breakpoint_about_to_proceed (void)
3801 {
3802 if (!ptid_equal (inferior_ptid, null_ptid))
3803 {
3804 struct thread_info *tp = inferior_thread ();
3805
3806 /* Allow inferior function calls in breakpoint commands to not
3807 interrupt the command list. When the call finishes
3808 successfully, the inferior will be standing at the same
3809 breakpoint as if nothing happened. */
3810 if (tp->control.in_infcall)
3811 return;
3812 }
3813
3814 breakpoint_proceeded = 1;
3815 }
3816
3817 /* Stub for cleaning up our state if we error-out of a breakpoint
3818 command. */
3819 static void
3820 cleanup_executing_breakpoints (void *ignore)
3821 {
3822 executing_breakpoint_commands = 0;
3823 }
3824
3825 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
3826 or its equivalent. */
3827
3828 static int
3829 command_line_is_silent (struct command_line *cmd)
3830 {
3831 return cmd && (strcmp ("silent", cmd->line) == 0
3832 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3833 }
3834
3835 /* Execute all the commands associated with all the breakpoints at
3836 this location. Any of these commands could cause the process to
3837 proceed beyond this point, etc. We look out for such changes by
3838 checking the global "breakpoint_proceeded" after each command.
3839
3840 Returns true if a breakpoint command resumed the inferior. In that
3841 case, it is the caller's responsibility to recall it again with the
3842 bpstat of the current thread. */
3843
3844 static int
3845 bpstat_do_actions_1 (bpstat *bsp)
3846 {
3847 bpstat bs;
3848 struct cleanup *old_chain;
3849 int again = 0;
3850
3851 /* Avoid endless recursion if a `source' command is contained
3852 in bs->commands. */
3853 if (executing_breakpoint_commands)
3854 return 0;
3855
3856 executing_breakpoint_commands = 1;
3857 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3858
3859 prevent_dont_repeat ();
3860
3861 /* This pointer will iterate over the list of bpstat's. */
3862 bs = *bsp;
3863
3864 breakpoint_proceeded = 0;
3865 for (; bs != NULL; bs = bs->next)
3866 {
3867 struct counted_command_line *ccmd;
3868 struct command_line *cmd;
3869 struct cleanup *this_cmd_tree_chain;
3870
3871 /* Take ownership of the BSP's command tree, if it has one.
3872
3873 The command tree could legitimately contain commands like
3874 'step' and 'next', which call clear_proceed_status, which
3875 frees stop_bpstat's command tree. To make sure this doesn't
3876 free the tree we're executing out from under us, we need to
3877 take ownership of the tree ourselves. Since a given bpstat's
3878 commands are only executed once, we don't need to copy it; we
3879 can clear the pointer in the bpstat, and make sure we free
3880 the tree when we're done. */
3881 ccmd = bs->commands;
3882 bs->commands = NULL;
3883 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3884 cmd = ccmd ? ccmd->commands : NULL;
3885 if (command_line_is_silent (cmd))
3886 {
3887 /* The action has been already done by bpstat_stop_status. */
3888 cmd = cmd->next;
3889 }
3890
3891 while (cmd != NULL)
3892 {
3893 execute_control_command (cmd);
3894
3895 if (breakpoint_proceeded)
3896 break;
3897 else
3898 cmd = cmd->next;
3899 }
3900
3901 /* We can free this command tree now. */
3902 do_cleanups (this_cmd_tree_chain);
3903
3904 if (breakpoint_proceeded)
3905 {
3906 if (target_can_async_p ())
3907 /* If we are in async mode, then the target might be still
3908 running, not stopped at any breakpoint, so nothing for
3909 us to do here -- just return to the event loop. */
3910 ;
3911 else
3912 /* In sync mode, when execute_control_command returns
3913 we're already standing on the next breakpoint.
3914 Breakpoint commands for that stop were not run, since
3915 execute_command does not run breakpoint commands --
3916 only command_line_handler does, but that one is not
3917 involved in execution of breakpoint commands. So, we
3918 can now execute breakpoint commands. It should be
3919 noted that making execute_command do bpstat actions is
3920 not an option -- in this case we'll have recursive
3921 invocation of bpstat for each breakpoint with a
3922 command, and can easily blow up GDB stack. Instead, we
3923 return true, which will trigger the caller to recall us
3924 with the new stop_bpstat. */
3925 again = 1;
3926 break;
3927 }
3928 }
3929 do_cleanups (old_chain);
3930 return again;
3931 }
3932
3933 void
3934 bpstat_do_actions (void)
3935 {
3936 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3937
3938 /* Do any commands attached to breakpoint we are stopped at. */
3939 while (!ptid_equal (inferior_ptid, null_ptid)
3940 && target_has_execution
3941 && !is_exited (inferior_ptid)
3942 && !is_executing (inferior_ptid))
3943 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3944 and only return when it is stopped at the next breakpoint, we
3945 keep doing breakpoint actions until it returns false to
3946 indicate the inferior was not resumed. */
3947 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
3948 break;
3949
3950 discard_cleanups (cleanup_if_error);
3951 }
3952
3953 /* Print out the (old or new) value associated with a watchpoint. */
3954
3955 static void
3956 watchpoint_value_print (struct value *val, struct ui_file *stream)
3957 {
3958 if (val == NULL)
3959 fprintf_unfiltered (stream, _("<unreadable>"));
3960 else
3961 {
3962 struct value_print_options opts;
3963 get_user_print_options (&opts);
3964 value_print (val, stream, &opts);
3965 }
3966 }
3967
3968 /* Generic routine for printing messages indicating why we
3969 stopped. The behavior of this function depends on the value
3970 'print_it' in the bpstat structure. Under some circumstances we
3971 may decide not to print anything here and delegate the task to
3972 normal_stop(). */
3973
3974 static enum print_stop_action
3975 print_bp_stop_message (bpstat bs)
3976 {
3977 switch (bs->print_it)
3978 {
3979 case print_it_noop:
3980 /* Nothing should be printed for this bpstat entry. */
3981 return PRINT_UNKNOWN;
3982 break;
3983
3984 case print_it_done:
3985 /* We still want to print the frame, but we already printed the
3986 relevant messages. */
3987 return PRINT_SRC_AND_LOC;
3988 break;
3989
3990 case print_it_normal:
3991 {
3992 struct breakpoint *b = bs->breakpoint_at;
3993
3994 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3995 which has since been deleted. */
3996 if (b == NULL)
3997 return PRINT_UNKNOWN;
3998
3999 /* Normal case. Call the breakpoint's print_it method. */
4000 return b->ops->print_it (bs);
4001 }
4002 break;
4003
4004 default:
4005 internal_error (__FILE__, __LINE__,
4006 _("print_bp_stop_message: unrecognized enum value"));
4007 break;
4008 }
4009 }
4010
4011 /* A helper function that prints a shared library stopped event. */
4012
4013 static void
4014 print_solib_event (int is_catchpoint)
4015 {
4016 int any_deleted
4017 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4018 int any_added
4019 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4020
4021 if (!is_catchpoint)
4022 {
4023 if (any_added || any_deleted)
4024 ui_out_text (current_uiout,
4025 _("Stopped due to shared library event:\n"));
4026 else
4027 ui_out_text (current_uiout,
4028 _("Stopped due to shared library event (no "
4029 "libraries added or removed)\n"));
4030 }
4031
4032 if (ui_out_is_mi_like_p (current_uiout))
4033 ui_out_field_string (current_uiout, "reason",
4034 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4035
4036 if (any_deleted)
4037 {
4038 struct cleanup *cleanup;
4039 char *name;
4040 int ix;
4041
4042 ui_out_text (current_uiout, _(" Inferior unloaded "));
4043 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4044 "removed");
4045 for (ix = 0;
4046 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4047 ix, name);
4048 ++ix)
4049 {
4050 if (ix > 0)
4051 ui_out_text (current_uiout, " ");
4052 ui_out_field_string (current_uiout, "library", name);
4053 ui_out_text (current_uiout, "\n");
4054 }
4055
4056 do_cleanups (cleanup);
4057 }
4058
4059 if (any_added)
4060 {
4061 struct so_list *iter;
4062 int ix;
4063 struct cleanup *cleanup;
4064
4065 ui_out_text (current_uiout, _(" Inferior loaded "));
4066 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4067 "added");
4068 for (ix = 0;
4069 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4070 ix, iter);
4071 ++ix)
4072 {
4073 if (ix > 0)
4074 ui_out_text (current_uiout, " ");
4075 ui_out_field_string (current_uiout, "library", iter->so_name);
4076 ui_out_text (current_uiout, "\n");
4077 }
4078
4079 do_cleanups (cleanup);
4080 }
4081 }
4082
4083 /* Print a message indicating what happened. This is called from
4084 normal_stop(). The input to this routine is the head of the bpstat
4085 list - a list of the eventpoints that caused this stop. KIND is
4086 the target_waitkind for the stopping event. This
4087 routine calls the generic print routine for printing a message
4088 about reasons for stopping. This will print (for example) the
4089 "Breakpoint n," part of the output. The return value of this
4090 routine is one of:
4091
4092 PRINT_UNKNOWN: Means we printed nothing.
4093 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4094 code to print the location. An example is
4095 "Breakpoint 1, " which should be followed by
4096 the location.
4097 PRINT_SRC_ONLY: Means we printed something, but there is no need
4098 to also print the location part of the message.
4099 An example is the catch/throw messages, which
4100 don't require a location appended to the end.
4101 PRINT_NOTHING: We have done some printing and we don't need any
4102 further info to be printed. */
4103
4104 enum print_stop_action
4105 bpstat_print (bpstat bs, int kind)
4106 {
4107 int val;
4108
4109 /* Maybe another breakpoint in the chain caused us to stop.
4110 (Currently all watchpoints go on the bpstat whether hit or not.
4111 That probably could (should) be changed, provided care is taken
4112 with respect to bpstat_explains_signal). */
4113 for (; bs; bs = bs->next)
4114 {
4115 val = print_bp_stop_message (bs);
4116 if (val == PRINT_SRC_ONLY
4117 || val == PRINT_SRC_AND_LOC
4118 || val == PRINT_NOTHING)
4119 return val;
4120 }
4121
4122 /* If we had hit a shared library event breakpoint,
4123 print_bp_stop_message would print out this message. If we hit an
4124 OS-level shared library event, do the same thing. */
4125 if (kind == TARGET_WAITKIND_LOADED)
4126 {
4127 print_solib_event (0);
4128 return PRINT_NOTHING;
4129 }
4130
4131 /* We reached the end of the chain, or we got a null BS to start
4132 with and nothing was printed. */
4133 return PRINT_UNKNOWN;
4134 }
4135
4136 /* Evaluate the expression EXP and return 1 if value is zero. This is
4137 used inside a catch_errors to evaluate the breakpoint condition.
4138 The argument is a "struct expression *" that has been cast to a
4139 "char *" to make it pass through catch_errors. */
4140
4141 static int
4142 breakpoint_cond_eval (void *exp)
4143 {
4144 struct value *mark = value_mark ();
4145 int i = !value_true (evaluate_expression ((struct expression *) exp));
4146
4147 value_free_to_mark (mark);
4148 return i;
4149 }
4150
4151 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4152
4153 static bpstat
4154 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4155 {
4156 bpstat bs;
4157
4158 bs = (bpstat) xmalloc (sizeof (*bs));
4159 bs->next = NULL;
4160 **bs_link_pointer = bs;
4161 *bs_link_pointer = &bs->next;
4162 bs->breakpoint_at = bl->owner;
4163 bs->bp_location_at = bl;
4164 incref_bp_location (bl);
4165 /* If the condition is false, etc., don't do the commands. */
4166 bs->commands = NULL;
4167 bs->old_val = NULL;
4168 bs->print_it = print_it_normal;
4169 return bs;
4170 }
4171 \f
4172 /* The target has stopped with waitstatus WS. Check if any hardware
4173 watchpoints have triggered, according to the target. */
4174
4175 int
4176 watchpoints_triggered (struct target_waitstatus *ws)
4177 {
4178 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4179 CORE_ADDR addr;
4180 struct breakpoint *b;
4181
4182 if (!stopped_by_watchpoint)
4183 {
4184 /* We were not stopped by a watchpoint. Mark all watchpoints
4185 as not triggered. */
4186 ALL_BREAKPOINTS (b)
4187 if (is_hardware_watchpoint (b))
4188 {
4189 struct watchpoint *w = (struct watchpoint *) b;
4190
4191 w->watchpoint_triggered = watch_triggered_no;
4192 }
4193
4194 return 0;
4195 }
4196
4197 if (!target_stopped_data_address (&current_target, &addr))
4198 {
4199 /* We were stopped by a watchpoint, but we don't know where.
4200 Mark all watchpoints as unknown. */
4201 ALL_BREAKPOINTS (b)
4202 if (is_hardware_watchpoint (b))
4203 {
4204 struct watchpoint *w = (struct watchpoint *) b;
4205
4206 w->watchpoint_triggered = watch_triggered_unknown;
4207 }
4208
4209 return stopped_by_watchpoint;
4210 }
4211
4212 /* The target could report the data address. Mark watchpoints
4213 affected by this data address as triggered, and all others as not
4214 triggered. */
4215
4216 ALL_BREAKPOINTS (b)
4217 if (is_hardware_watchpoint (b))
4218 {
4219 struct watchpoint *w = (struct watchpoint *) b;
4220 struct bp_location *loc;
4221
4222 w->watchpoint_triggered = watch_triggered_no;
4223 for (loc = b->loc; loc; loc = loc->next)
4224 {
4225 if (is_masked_watchpoint (b))
4226 {
4227 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4228 CORE_ADDR start = loc->address & w->hw_wp_mask;
4229
4230 if (newaddr == start)
4231 {
4232 w->watchpoint_triggered = watch_triggered_yes;
4233 break;
4234 }
4235 }
4236 /* Exact match not required. Within range is sufficient. */
4237 else if (target_watchpoint_addr_within_range (&current_target,
4238 addr, loc->address,
4239 loc->length))
4240 {
4241 w->watchpoint_triggered = watch_triggered_yes;
4242 break;
4243 }
4244 }
4245 }
4246
4247 return 1;
4248 }
4249
4250 /* Possible return values for watchpoint_check (this can't be an enum
4251 because of check_errors). */
4252 /* The watchpoint has been deleted. */
4253 #define WP_DELETED 1
4254 /* The value has changed. */
4255 #define WP_VALUE_CHANGED 2
4256 /* The value has not changed. */
4257 #define WP_VALUE_NOT_CHANGED 3
4258 /* Ignore this watchpoint, no matter if the value changed or not. */
4259 #define WP_IGNORE 4
4260
4261 #define BP_TEMPFLAG 1
4262 #define BP_HARDWAREFLAG 2
4263
4264 /* Evaluate watchpoint condition expression and check if its value
4265 changed.
4266
4267 P should be a pointer to struct bpstat, but is defined as a void *
4268 in order for this function to be usable with catch_errors. */
4269
4270 static int
4271 watchpoint_check (void *p)
4272 {
4273 bpstat bs = (bpstat) p;
4274 struct watchpoint *b;
4275 struct frame_info *fr;
4276 int within_current_scope;
4277
4278 /* BS is built from an existing struct breakpoint. */
4279 gdb_assert (bs->breakpoint_at != NULL);
4280 b = (struct watchpoint *) bs->breakpoint_at;
4281
4282 /* If this is a local watchpoint, we only want to check if the
4283 watchpoint frame is in scope if the current thread is the thread
4284 that was used to create the watchpoint. */
4285 if (!watchpoint_in_thread_scope (b))
4286 return WP_IGNORE;
4287
4288 if (b->exp_valid_block == NULL)
4289 within_current_scope = 1;
4290 else
4291 {
4292 struct frame_info *frame = get_current_frame ();
4293 struct gdbarch *frame_arch = get_frame_arch (frame);
4294 CORE_ADDR frame_pc = get_frame_pc (frame);
4295
4296 /* in_function_epilogue_p() returns a non-zero value if we're
4297 still in the function but the stack frame has already been
4298 invalidated. Since we can't rely on the values of local
4299 variables after the stack has been destroyed, we are treating
4300 the watchpoint in that state as `not changed' without further
4301 checking. Don't mark watchpoints as changed if the current
4302 frame is in an epilogue - even if they are in some other
4303 frame, our view of the stack is likely to be wrong and
4304 frame_find_by_id could error out. */
4305 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4306 return WP_IGNORE;
4307
4308 fr = frame_find_by_id (b->watchpoint_frame);
4309 within_current_scope = (fr != NULL);
4310
4311 /* If we've gotten confused in the unwinder, we might have
4312 returned a frame that can't describe this variable. */
4313 if (within_current_scope)
4314 {
4315 struct symbol *function;
4316
4317 function = get_frame_function (fr);
4318 if (function == NULL
4319 || !contained_in (b->exp_valid_block,
4320 SYMBOL_BLOCK_VALUE (function)))
4321 within_current_scope = 0;
4322 }
4323
4324 if (within_current_scope)
4325 /* If we end up stopping, the current frame will get selected
4326 in normal_stop. So this call to select_frame won't affect
4327 the user. */
4328 select_frame (fr);
4329 }
4330
4331 if (within_current_scope)
4332 {
4333 /* We use value_{,free_to_}mark because it could be a *long*
4334 time before we return to the command level and call
4335 free_all_values. We can't call free_all_values because we
4336 might be in the middle of evaluating a function call. */
4337
4338 int pc = 0;
4339 struct value *mark;
4340 struct value *new_val;
4341
4342 if (is_masked_watchpoint (&b->base))
4343 /* Since we don't know the exact trigger address (from
4344 stopped_data_address), just tell the user we've triggered
4345 a mask watchpoint. */
4346 return WP_VALUE_CHANGED;
4347
4348 mark = value_mark ();
4349 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
4350
4351 /* We use value_equal_contents instead of value_equal because
4352 the latter coerces an array to a pointer, thus comparing just
4353 the address of the array instead of its contents. This is
4354 not what we want. */
4355 if ((b->val != NULL) != (new_val != NULL)
4356 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4357 {
4358 if (new_val != NULL)
4359 {
4360 release_value (new_val);
4361 value_free_to_mark (mark);
4362 }
4363 bs->old_val = b->val;
4364 b->val = new_val;
4365 b->val_valid = 1;
4366 return WP_VALUE_CHANGED;
4367 }
4368 else
4369 {
4370 /* Nothing changed. */
4371 value_free_to_mark (mark);
4372 return WP_VALUE_NOT_CHANGED;
4373 }
4374 }
4375 else
4376 {
4377 struct ui_out *uiout = current_uiout;
4378
4379 /* This seems like the only logical thing to do because
4380 if we temporarily ignored the watchpoint, then when
4381 we reenter the block in which it is valid it contains
4382 garbage (in the case of a function, it may have two
4383 garbage values, one before and one after the prologue).
4384 So we can't even detect the first assignment to it and
4385 watch after that (since the garbage may or may not equal
4386 the first value assigned). */
4387 /* We print all the stop information in
4388 breakpoint_ops->print_it, but in this case, by the time we
4389 call breakpoint_ops->print_it this bp will be deleted
4390 already. So we have no choice but print the information
4391 here. */
4392 if (ui_out_is_mi_like_p (uiout))
4393 ui_out_field_string
4394 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4395 ui_out_text (uiout, "\nWatchpoint ");
4396 ui_out_field_int (uiout, "wpnum", b->base.number);
4397 ui_out_text (uiout,
4398 " deleted because the program has left the block in\n\
4399 which its expression is valid.\n");
4400
4401 /* Make sure the watchpoint's commands aren't executed. */
4402 decref_counted_command_line (&b->base.commands);
4403 watchpoint_del_at_next_stop (b);
4404
4405 return WP_DELETED;
4406 }
4407 }
4408
4409 /* Return true if it looks like target has stopped due to hitting
4410 breakpoint location BL. This function does not check if we should
4411 stop, only if BL explains the stop. */
4412
4413 static int
4414 bpstat_check_location (const struct bp_location *bl,
4415 struct address_space *aspace, CORE_ADDR bp_addr,
4416 const struct target_waitstatus *ws)
4417 {
4418 struct breakpoint *b = bl->owner;
4419
4420 /* BL is from an existing breakpoint. */
4421 gdb_assert (b != NULL);
4422
4423 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4424 }
4425
4426 /* Determine if the watched values have actually changed, and we
4427 should stop. If not, set BS->stop to 0. */
4428
4429 static void
4430 bpstat_check_watchpoint (bpstat bs)
4431 {
4432 const struct bp_location *bl;
4433 struct watchpoint *b;
4434
4435 /* BS is built for existing struct breakpoint. */
4436 bl = bs->bp_location_at;
4437 gdb_assert (bl != NULL);
4438 b = (struct watchpoint *) bs->breakpoint_at;
4439 gdb_assert (b != NULL);
4440
4441 {
4442 int must_check_value = 0;
4443
4444 if (b->base.type == bp_watchpoint)
4445 /* For a software watchpoint, we must always check the
4446 watched value. */
4447 must_check_value = 1;
4448 else if (b->watchpoint_triggered == watch_triggered_yes)
4449 /* We have a hardware watchpoint (read, write, or access)
4450 and the target earlier reported an address watched by
4451 this watchpoint. */
4452 must_check_value = 1;
4453 else if (b->watchpoint_triggered == watch_triggered_unknown
4454 && b->base.type == bp_hardware_watchpoint)
4455 /* We were stopped by a hardware watchpoint, but the target could
4456 not report the data address. We must check the watchpoint's
4457 value. Access and read watchpoints are out of luck; without
4458 a data address, we can't figure it out. */
4459 must_check_value = 1;
4460
4461 if (must_check_value)
4462 {
4463 char *message
4464 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4465 b->base.number);
4466 struct cleanup *cleanups = make_cleanup (xfree, message);
4467 int e = catch_errors (watchpoint_check, bs, message,
4468 RETURN_MASK_ALL);
4469 do_cleanups (cleanups);
4470 switch (e)
4471 {
4472 case WP_DELETED:
4473 /* We've already printed what needs to be printed. */
4474 bs->print_it = print_it_done;
4475 /* Stop. */
4476 break;
4477 case WP_IGNORE:
4478 bs->print_it = print_it_noop;
4479 bs->stop = 0;
4480 break;
4481 case WP_VALUE_CHANGED:
4482 if (b->base.type == bp_read_watchpoint)
4483 {
4484 /* There are two cases to consider here:
4485
4486 1. We're watching the triggered memory for reads.
4487 In that case, trust the target, and always report
4488 the watchpoint hit to the user. Even though
4489 reads don't cause value changes, the value may
4490 have changed since the last time it was read, and
4491 since we're not trapping writes, we will not see
4492 those, and as such we should ignore our notion of
4493 old value.
4494
4495 2. We're watching the triggered memory for both
4496 reads and writes. There are two ways this may
4497 happen:
4498
4499 2.1. This is a target that can't break on data
4500 reads only, but can break on accesses (reads or
4501 writes), such as e.g., x86. We detect this case
4502 at the time we try to insert read watchpoints.
4503
4504 2.2. Otherwise, the target supports read
4505 watchpoints, but, the user set an access or write
4506 watchpoint watching the same memory as this read
4507 watchpoint.
4508
4509 If we're watching memory writes as well as reads,
4510 ignore watchpoint hits when we find that the
4511 value hasn't changed, as reads don't cause
4512 changes. This still gives false positives when
4513 the program writes the same value to memory as
4514 what there was already in memory (we will confuse
4515 it for a read), but it's much better than
4516 nothing. */
4517
4518 int other_write_watchpoint = 0;
4519
4520 if (bl->watchpoint_type == hw_read)
4521 {
4522 struct breakpoint *other_b;
4523
4524 ALL_BREAKPOINTS (other_b)
4525 if (other_b->type == bp_hardware_watchpoint
4526 || other_b->type == bp_access_watchpoint)
4527 {
4528 struct watchpoint *other_w =
4529 (struct watchpoint *) other_b;
4530
4531 if (other_w->watchpoint_triggered
4532 == watch_triggered_yes)
4533 {
4534 other_write_watchpoint = 1;
4535 break;
4536 }
4537 }
4538 }
4539
4540 if (other_write_watchpoint
4541 || bl->watchpoint_type == hw_access)
4542 {
4543 /* We're watching the same memory for writes,
4544 and the value changed since the last time we
4545 updated it, so this trap must be for a write.
4546 Ignore it. */
4547 bs->print_it = print_it_noop;
4548 bs->stop = 0;
4549 }
4550 }
4551 break;
4552 case WP_VALUE_NOT_CHANGED:
4553 if (b->base.type == bp_hardware_watchpoint
4554 || b->base.type == bp_watchpoint)
4555 {
4556 /* Don't stop: write watchpoints shouldn't fire if
4557 the value hasn't changed. */
4558 bs->print_it = print_it_noop;
4559 bs->stop = 0;
4560 }
4561 /* Stop. */
4562 break;
4563 default:
4564 /* Can't happen. */
4565 case 0:
4566 /* Error from catch_errors. */
4567 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
4568 watchpoint_del_at_next_stop (b);
4569 /* We've already printed what needs to be printed. */
4570 bs->print_it = print_it_done;
4571 break;
4572 }
4573 }
4574 else /* must_check_value == 0 */
4575 {
4576 /* This is a case where some watchpoint(s) triggered, but
4577 not at the address of this watchpoint, or else no
4578 watchpoint triggered after all. So don't print
4579 anything for this watchpoint. */
4580 bs->print_it = print_it_noop;
4581 bs->stop = 0;
4582 }
4583 }
4584 }
4585
4586
4587 /* Check conditions (condition proper, frame, thread and ignore count)
4588 of breakpoint referred to by BS. If we should not stop for this
4589 breakpoint, set BS->stop to 0. */
4590
4591 static void
4592 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4593 {
4594 int thread_id = pid_to_thread_id (ptid);
4595 const struct bp_location *bl;
4596 struct breakpoint *b;
4597
4598 /* BS is built for existing struct breakpoint. */
4599 bl = bs->bp_location_at;
4600 gdb_assert (bl != NULL);
4601 b = bs->breakpoint_at;
4602 gdb_assert (b != NULL);
4603
4604 /* Even if the target evaluated the condition on its end and notified GDB, we
4605 need to do so again since GDB does not know if we stopped due to a
4606 breakpoint or a single step breakpoint. */
4607
4608 if (frame_id_p (b->frame_id)
4609 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
4610 bs->stop = 0;
4611 else if (bs->stop)
4612 {
4613 int value_is_zero = 0;
4614 struct expression *cond;
4615
4616 /* Evaluate Python breakpoints that have a "stop"
4617 method implemented. */
4618 if (b->py_bp_object)
4619 bs->stop = gdbpy_should_stop (b->py_bp_object);
4620
4621 if (is_watchpoint (b))
4622 {
4623 struct watchpoint *w = (struct watchpoint *) b;
4624
4625 cond = w->cond_exp;
4626 }
4627 else
4628 cond = bl->cond;
4629
4630 if (cond && b->disposition != disp_del_at_next_stop)
4631 {
4632 int within_current_scope = 1;
4633 struct watchpoint * w;
4634
4635 /* We use value_mark and value_free_to_mark because it could
4636 be a long time before we return to the command level and
4637 call free_all_values. We can't call free_all_values
4638 because we might be in the middle of evaluating a
4639 function call. */
4640 struct value *mark = value_mark ();
4641
4642 if (is_watchpoint (b))
4643 w = (struct watchpoint *) b;
4644 else
4645 w = NULL;
4646
4647 /* Need to select the frame, with all that implies so that
4648 the conditions will have the right context. Because we
4649 use the frame, we will not see an inlined function's
4650 variables when we arrive at a breakpoint at the start
4651 of the inlined function; the current frame will be the
4652 call site. */
4653 if (w == NULL || w->cond_exp_valid_block == NULL)
4654 select_frame (get_current_frame ());
4655 else
4656 {
4657 struct frame_info *frame;
4658
4659 /* For local watchpoint expressions, which particular
4660 instance of a local is being watched matters, so we
4661 keep track of the frame to evaluate the expression
4662 in. To evaluate the condition however, it doesn't
4663 really matter which instantiation of the function
4664 where the condition makes sense triggers the
4665 watchpoint. This allows an expression like "watch
4666 global if q > 10" set in `func', catch writes to
4667 global on all threads that call `func', or catch
4668 writes on all recursive calls of `func' by a single
4669 thread. We simply always evaluate the condition in
4670 the innermost frame that's executing where it makes
4671 sense to evaluate the condition. It seems
4672 intuitive. */
4673 frame = block_innermost_frame (w->cond_exp_valid_block);
4674 if (frame != NULL)
4675 select_frame (frame);
4676 else
4677 within_current_scope = 0;
4678 }
4679 if (within_current_scope)
4680 value_is_zero
4681 = catch_errors (breakpoint_cond_eval, cond,
4682 "Error in testing breakpoint condition:\n",
4683 RETURN_MASK_ALL);
4684 else
4685 {
4686 warning (_("Watchpoint condition cannot be tested "
4687 "in the current scope"));
4688 /* If we failed to set the right context for this
4689 watchpoint, unconditionally report it. */
4690 value_is_zero = 0;
4691 }
4692 /* FIXME-someday, should give breakpoint #. */
4693 value_free_to_mark (mark);
4694 }
4695
4696 if (cond && value_is_zero)
4697 {
4698 bs->stop = 0;
4699 }
4700 else if (b->thread != -1 && b->thread != thread_id)
4701 {
4702 bs->stop = 0;
4703 }
4704 else if (b->ignore_count > 0)
4705 {
4706 b->ignore_count--;
4707 annotate_ignore_count_change ();
4708 bs->stop = 0;
4709 /* Increase the hit count even though we don't stop. */
4710 ++(b->hit_count);
4711 observer_notify_breakpoint_modified (b);
4712 }
4713 }
4714 }
4715
4716
4717 /* Get a bpstat associated with having just stopped at address
4718 BP_ADDR in thread PTID.
4719
4720 Determine whether we stopped at a breakpoint, etc, or whether we
4721 don't understand this stop. Result is a chain of bpstat's such
4722 that:
4723
4724 if we don't understand the stop, the result is a null pointer.
4725
4726 if we understand why we stopped, the result is not null.
4727
4728 Each element of the chain refers to a particular breakpoint or
4729 watchpoint at which we have stopped. (We may have stopped for
4730 several reasons concurrently.)
4731
4732 Each element of the chain has valid next, breakpoint_at,
4733 commands, FIXME??? fields. */
4734
4735 bpstat
4736 bpstat_stop_status (struct address_space *aspace,
4737 CORE_ADDR bp_addr, ptid_t ptid,
4738 const struct target_waitstatus *ws)
4739 {
4740 struct breakpoint *b = NULL;
4741 struct bp_location *bl;
4742 struct bp_location *loc;
4743 /* First item of allocated bpstat's. */
4744 bpstat bs_head = NULL, *bs_link = &bs_head;
4745 /* Pointer to the last thing in the chain currently. */
4746 bpstat bs;
4747 int ix;
4748 int need_remove_insert;
4749 int removed_any;
4750
4751 /* First, build the bpstat chain with locations that explain a
4752 target stop, while being careful to not set the target running,
4753 as that may invalidate locations (in particular watchpoint
4754 locations are recreated). Resuming will happen here with
4755 breakpoint conditions or watchpoint expressions that include
4756 inferior function calls. */
4757
4758 ALL_BREAKPOINTS (b)
4759 {
4760 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4761 continue;
4762
4763 for (bl = b->loc; bl != NULL; bl = bl->next)
4764 {
4765 /* For hardware watchpoints, we look only at the first
4766 location. The watchpoint_check function will work on the
4767 entire expression, not the individual locations. For
4768 read watchpoints, the watchpoints_triggered function has
4769 checked all locations already. */
4770 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4771 break;
4772
4773 if (bl->shlib_disabled)
4774 continue;
4775
4776 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
4777 continue;
4778
4779 /* Come here if it's a watchpoint, or if the break address
4780 matches. */
4781
4782 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4783 explain stop. */
4784
4785 /* Assume we stop. Should we find a watchpoint that is not
4786 actually triggered, or if the condition of the breakpoint
4787 evaluates as false, we'll reset 'stop' to 0. */
4788 bs->stop = 1;
4789 bs->print = 1;
4790
4791 /* If this is a scope breakpoint, mark the associated
4792 watchpoint as triggered so that we will handle the
4793 out-of-scope event. We'll get to the watchpoint next
4794 iteration. */
4795 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
4796 {
4797 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4798
4799 w->watchpoint_triggered = watch_triggered_yes;
4800 }
4801 }
4802 }
4803
4804 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4805 {
4806 if (breakpoint_location_address_match (loc, aspace, bp_addr))
4807 {
4808 bs = bpstat_alloc (loc, &bs_link);
4809 /* For hits of moribund locations, we should just proceed. */
4810 bs->stop = 0;
4811 bs->print = 0;
4812 bs->print_it = print_it_noop;
4813 }
4814 }
4815
4816 /* A bit of special processing for shlib breakpoints. We need to
4817 process solib loading here, so that the lists of loaded and
4818 unloaded libraries are correct before we handle "catch load" and
4819 "catch unload". */
4820 for (bs = bs_head; bs != NULL; bs = bs->next)
4821 {
4822 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
4823 {
4824 handle_solib_event ();
4825 break;
4826 }
4827 }
4828
4829 /* Now go through the locations that caused the target to stop, and
4830 check whether we're interested in reporting this stop to higher
4831 layers, or whether we should resume the target transparently. */
4832
4833 removed_any = 0;
4834
4835 for (bs = bs_head; bs != NULL; bs = bs->next)
4836 {
4837 if (!bs->stop)
4838 continue;
4839
4840 b = bs->breakpoint_at;
4841 b->ops->check_status (bs);
4842 if (bs->stop)
4843 {
4844 bpstat_check_breakpoint_conditions (bs, ptid);
4845
4846 if (bs->stop)
4847 {
4848 ++(b->hit_count);
4849 observer_notify_breakpoint_modified (b);
4850
4851 /* We will stop here. */
4852 if (b->disposition == disp_disable)
4853 {
4854 --(b->enable_count);
4855 if (b->enable_count <= 0
4856 && b->enable_state != bp_permanent)
4857 b->enable_state = bp_disabled;
4858 removed_any = 1;
4859 }
4860 if (b->silent)
4861 bs->print = 0;
4862 bs->commands = b->commands;
4863 incref_counted_command_line (bs->commands);
4864 if (command_line_is_silent (bs->commands
4865 ? bs->commands->commands : NULL))
4866 bs->print = 0;
4867 }
4868
4869 }
4870
4871 /* Print nothing for this entry if we don't stop or don't
4872 print. */
4873 if (!bs->stop || !bs->print)
4874 bs->print_it = print_it_noop;
4875 }
4876
4877 /* If we aren't stopping, the value of some hardware watchpoint may
4878 not have changed, but the intermediate memory locations we are
4879 watching may have. Don't bother if we're stopping; this will get
4880 done later. */
4881 need_remove_insert = 0;
4882 if (! bpstat_causes_stop (bs_head))
4883 for (bs = bs_head; bs != NULL; bs = bs->next)
4884 if (!bs->stop
4885 && bs->breakpoint_at
4886 && is_hardware_watchpoint (bs->breakpoint_at))
4887 {
4888 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4889
4890 update_watchpoint (w, 0 /* don't reparse. */);
4891 need_remove_insert = 1;
4892 }
4893
4894 if (need_remove_insert)
4895 update_global_location_list (1);
4896 else if (removed_any)
4897 update_global_location_list (0);
4898
4899 return bs_head;
4900 }
4901
4902 static void
4903 handle_jit_event (void)
4904 {
4905 struct frame_info *frame;
4906 struct gdbarch *gdbarch;
4907
4908 /* Switch terminal for any messages produced by
4909 breakpoint_re_set. */
4910 target_terminal_ours_for_output ();
4911
4912 frame = get_current_frame ();
4913 gdbarch = get_frame_arch (frame);
4914
4915 jit_event_handler (gdbarch);
4916
4917 target_terminal_inferior ();
4918 }
4919
4920 /* Handle an solib event by calling solib_add. */
4921
4922 void
4923 handle_solib_event (void)
4924 {
4925 clear_program_space_solib_cache (current_inferior ()->pspace);
4926
4927 /* Check for any newly added shared libraries if we're supposed to
4928 be adding them automatically. Switch terminal for any messages
4929 produced by breakpoint_re_set. */
4930 target_terminal_ours_for_output ();
4931 #ifdef SOLIB_ADD
4932 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4933 #else
4934 solib_add (NULL, 0, &current_target, auto_solib_add);
4935 #endif
4936 target_terminal_inferior ();
4937 }
4938
4939 /* Prepare WHAT final decision for infrun. */
4940
4941 /* Decide what infrun needs to do with this bpstat. */
4942
4943 struct bpstat_what
4944 bpstat_what (bpstat bs_head)
4945 {
4946 struct bpstat_what retval;
4947 int jit_event = 0;
4948 bpstat bs;
4949
4950 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
4951 retval.call_dummy = STOP_NONE;
4952 retval.is_longjmp = 0;
4953
4954 for (bs = bs_head; bs != NULL; bs = bs->next)
4955 {
4956 /* Extract this BS's action. After processing each BS, we check
4957 if its action overrides all we've seem so far. */
4958 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4959 enum bptype bptype;
4960
4961 if (bs->breakpoint_at == NULL)
4962 {
4963 /* I suspect this can happen if it was a momentary
4964 breakpoint which has since been deleted. */
4965 bptype = bp_none;
4966 }
4967 else
4968 bptype = bs->breakpoint_at->type;
4969
4970 switch (bptype)
4971 {
4972 case bp_none:
4973 break;
4974 case bp_breakpoint:
4975 case bp_hardware_breakpoint:
4976 case bp_until:
4977 case bp_finish:
4978 case bp_shlib_event:
4979 if (bs->stop)
4980 {
4981 if (bs->print)
4982 this_action = BPSTAT_WHAT_STOP_NOISY;
4983 else
4984 this_action = BPSTAT_WHAT_STOP_SILENT;
4985 }
4986 else
4987 this_action = BPSTAT_WHAT_SINGLE;
4988 break;
4989 case bp_watchpoint:
4990 case bp_hardware_watchpoint:
4991 case bp_read_watchpoint:
4992 case bp_access_watchpoint:
4993 if (bs->stop)
4994 {
4995 if (bs->print)
4996 this_action = BPSTAT_WHAT_STOP_NOISY;
4997 else
4998 this_action = BPSTAT_WHAT_STOP_SILENT;
4999 }
5000 else
5001 {
5002 /* There was a watchpoint, but we're not stopping.
5003 This requires no further action. */
5004 }
5005 break;
5006 case bp_longjmp:
5007 case bp_exception:
5008 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5009 retval.is_longjmp = bptype == bp_longjmp;
5010 break;
5011 case bp_longjmp_resume:
5012 case bp_exception_resume:
5013 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5014 retval.is_longjmp = bptype == bp_longjmp_resume;
5015 break;
5016 case bp_step_resume:
5017 if (bs->stop)
5018 this_action = BPSTAT_WHAT_STEP_RESUME;
5019 else
5020 {
5021 /* It is for the wrong frame. */
5022 this_action = BPSTAT_WHAT_SINGLE;
5023 }
5024 break;
5025 case bp_hp_step_resume:
5026 if (bs->stop)
5027 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5028 else
5029 {
5030 /* It is for the wrong frame. */
5031 this_action = BPSTAT_WHAT_SINGLE;
5032 }
5033 break;
5034 case bp_watchpoint_scope:
5035 case bp_thread_event:
5036 case bp_overlay_event:
5037 case bp_longjmp_master:
5038 case bp_std_terminate_master:
5039 case bp_exception_master:
5040 this_action = BPSTAT_WHAT_SINGLE;
5041 break;
5042 case bp_catchpoint:
5043 if (bs->stop)
5044 {
5045 if (bs->print)
5046 this_action = BPSTAT_WHAT_STOP_NOISY;
5047 else
5048 this_action = BPSTAT_WHAT_STOP_SILENT;
5049 }
5050 else
5051 {
5052 /* There was a catchpoint, but we're not stopping.
5053 This requires no further action. */
5054 }
5055 break;
5056 case bp_jit_event:
5057 jit_event = 1;
5058 this_action = BPSTAT_WHAT_SINGLE;
5059 break;
5060 case bp_call_dummy:
5061 /* Make sure the action is stop (silent or noisy),
5062 so infrun.c pops the dummy frame. */
5063 retval.call_dummy = STOP_STACK_DUMMY;
5064 this_action = BPSTAT_WHAT_STOP_SILENT;
5065 break;
5066 case bp_std_terminate:
5067 /* Make sure the action is stop (silent or noisy),
5068 so infrun.c pops the dummy frame. */
5069 retval.call_dummy = STOP_STD_TERMINATE;
5070 this_action = BPSTAT_WHAT_STOP_SILENT;
5071 break;
5072 case bp_tracepoint:
5073 case bp_fast_tracepoint:
5074 case bp_static_tracepoint:
5075 /* Tracepoint hits should not be reported back to GDB, and
5076 if one got through somehow, it should have been filtered
5077 out already. */
5078 internal_error (__FILE__, __LINE__,
5079 _("bpstat_what: tracepoint encountered"));
5080 break;
5081 case bp_gnu_ifunc_resolver:
5082 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5083 this_action = BPSTAT_WHAT_SINGLE;
5084 break;
5085 case bp_gnu_ifunc_resolver_return:
5086 /* The breakpoint will be removed, execution will restart from the
5087 PC of the former breakpoint. */
5088 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5089 break;
5090 default:
5091 internal_error (__FILE__, __LINE__,
5092 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5093 }
5094
5095 retval.main_action = max (retval.main_action, this_action);
5096 }
5097
5098 /* These operations may affect the bs->breakpoint_at state so they are
5099 delayed after MAIN_ACTION is decided above. */
5100
5101 if (jit_event)
5102 {
5103 if (debug_infrun)
5104 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5105
5106 handle_jit_event ();
5107 }
5108
5109 for (bs = bs_head; bs != NULL; bs = bs->next)
5110 {
5111 struct breakpoint *b = bs->breakpoint_at;
5112
5113 if (b == NULL)
5114 continue;
5115 switch (b->type)
5116 {
5117 case bp_gnu_ifunc_resolver:
5118 gnu_ifunc_resolver_stop (b);
5119 break;
5120 case bp_gnu_ifunc_resolver_return:
5121 gnu_ifunc_resolver_return_stop (b);
5122 break;
5123 }
5124 }
5125
5126 return retval;
5127 }
5128
5129 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5130 without hardware support). This isn't related to a specific bpstat,
5131 just to things like whether watchpoints are set. */
5132
5133 int
5134 bpstat_should_step (void)
5135 {
5136 struct breakpoint *b;
5137
5138 ALL_BREAKPOINTS (b)
5139 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5140 return 1;
5141 return 0;
5142 }
5143
5144 int
5145 bpstat_causes_stop (bpstat bs)
5146 {
5147 for (; bs != NULL; bs = bs->next)
5148 if (bs->stop)
5149 return 1;
5150
5151 return 0;
5152 }
5153
5154 \f
5155
5156 /* Compute a string of spaces suitable to indent the next line
5157 so it starts at the position corresponding to the table column
5158 named COL_NAME in the currently active table of UIOUT. */
5159
5160 static char *
5161 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5162 {
5163 static char wrap_indent[80];
5164 int i, total_width, width, align;
5165 char *text;
5166
5167 total_width = 0;
5168 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5169 {
5170 if (strcmp (text, col_name) == 0)
5171 {
5172 gdb_assert (total_width < sizeof wrap_indent);
5173 memset (wrap_indent, ' ', total_width);
5174 wrap_indent[total_width] = 0;
5175
5176 return wrap_indent;
5177 }
5178
5179 total_width += width + 1;
5180 }
5181
5182 return NULL;
5183 }
5184
5185 /* Determine if the locations of this breakpoint will have their conditions
5186 evaluated by the target, host or a mix of both. Returns the following:
5187
5188 "host": Host evals condition.
5189 "host or target": Host or Target evals condition.
5190 "target": Target evals condition.
5191 */
5192
5193 static const char *
5194 bp_condition_evaluator (struct breakpoint *b)
5195 {
5196 struct bp_location *bl;
5197 char host_evals = 0;
5198 char target_evals = 0;
5199
5200 if (!b)
5201 return NULL;
5202
5203 if (!is_breakpoint (b))
5204 return NULL;
5205
5206 if (gdb_evaluates_breakpoint_condition_p ()
5207 || !target_supports_evaluation_of_breakpoint_conditions ())
5208 return condition_evaluation_host;
5209
5210 for (bl = b->loc; bl; bl = bl->next)
5211 {
5212 if (bl->cond_bytecode)
5213 target_evals++;
5214 else
5215 host_evals++;
5216 }
5217
5218 if (host_evals && target_evals)
5219 return condition_evaluation_both;
5220 else if (target_evals)
5221 return condition_evaluation_target;
5222 else
5223 return condition_evaluation_host;
5224 }
5225
5226 /* Determine the breakpoint location's condition evaluator. This is
5227 similar to bp_condition_evaluator, but for locations. */
5228
5229 static const char *
5230 bp_location_condition_evaluator (struct bp_location *bl)
5231 {
5232 if (bl && !is_breakpoint (bl->owner))
5233 return NULL;
5234
5235 if (gdb_evaluates_breakpoint_condition_p ()
5236 || !target_supports_evaluation_of_breakpoint_conditions ())
5237 return condition_evaluation_host;
5238
5239 if (bl && bl->cond_bytecode)
5240 return condition_evaluation_target;
5241 else
5242 return condition_evaluation_host;
5243 }
5244
5245 /* Print the LOC location out of the list of B->LOC locations. */
5246
5247 static void
5248 print_breakpoint_location (struct breakpoint *b,
5249 struct bp_location *loc)
5250 {
5251 struct ui_out *uiout = current_uiout;
5252 struct cleanup *old_chain = save_current_program_space ();
5253
5254 if (loc != NULL && loc->shlib_disabled)
5255 loc = NULL;
5256
5257 if (loc != NULL)
5258 set_current_program_space (loc->pspace);
5259
5260 if (b->display_canonical)
5261 ui_out_field_string (uiout, "what", b->addr_string);
5262 else if (loc && loc->source_file)
5263 {
5264 struct symbol *sym
5265 = find_pc_sect_function (loc->address, loc->section);
5266 if (sym)
5267 {
5268 ui_out_text (uiout, "in ");
5269 ui_out_field_string (uiout, "func",
5270 SYMBOL_PRINT_NAME (sym));
5271 ui_out_text (uiout, " ");
5272 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5273 ui_out_text (uiout, "at ");
5274 }
5275 ui_out_field_string (uiout, "file", loc->source_file);
5276 ui_out_text (uiout, ":");
5277
5278 if (ui_out_is_mi_like_p (uiout))
5279 {
5280 struct symtab_and_line sal = find_pc_line (loc->address, 0);
5281 char *fullname = symtab_to_fullname (sal.symtab);
5282
5283 if (fullname)
5284 ui_out_field_string (uiout, "fullname", fullname);
5285 }
5286
5287 ui_out_field_int (uiout, "line", loc->line_number);
5288 }
5289 else if (loc)
5290 {
5291 struct ui_file *stb = mem_fileopen ();
5292 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5293
5294 print_address_symbolic (loc->gdbarch, loc->address, stb,
5295 demangle, "");
5296 ui_out_field_stream (uiout, "at", stb);
5297
5298 do_cleanups (stb_chain);
5299 }
5300 else
5301 ui_out_field_string (uiout, "pending", b->addr_string);
5302
5303 if (loc && is_breakpoint (b)
5304 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5305 && bp_condition_evaluator (b) == condition_evaluation_both)
5306 {
5307 ui_out_text (uiout, " (");
5308 ui_out_field_string (uiout, "evaluated-by",
5309 bp_location_condition_evaluator (loc));
5310 ui_out_text (uiout, ")");
5311 }
5312
5313 do_cleanups (old_chain);
5314 }
5315
5316 static const char *
5317 bptype_string (enum bptype type)
5318 {
5319 struct ep_type_description
5320 {
5321 enum bptype type;
5322 char *description;
5323 };
5324 static struct ep_type_description bptypes[] =
5325 {
5326 {bp_none, "?deleted?"},
5327 {bp_breakpoint, "breakpoint"},
5328 {bp_hardware_breakpoint, "hw breakpoint"},
5329 {bp_until, "until"},
5330 {bp_finish, "finish"},
5331 {bp_watchpoint, "watchpoint"},
5332 {bp_hardware_watchpoint, "hw watchpoint"},
5333 {bp_read_watchpoint, "read watchpoint"},
5334 {bp_access_watchpoint, "acc watchpoint"},
5335 {bp_longjmp, "longjmp"},
5336 {bp_longjmp_resume, "longjmp resume"},
5337 {bp_exception, "exception"},
5338 {bp_exception_resume, "exception resume"},
5339 {bp_step_resume, "step resume"},
5340 {bp_hp_step_resume, "high-priority step resume"},
5341 {bp_watchpoint_scope, "watchpoint scope"},
5342 {bp_call_dummy, "call dummy"},
5343 {bp_std_terminate, "std::terminate"},
5344 {bp_shlib_event, "shlib events"},
5345 {bp_thread_event, "thread events"},
5346 {bp_overlay_event, "overlay events"},
5347 {bp_longjmp_master, "longjmp master"},
5348 {bp_std_terminate_master, "std::terminate master"},
5349 {bp_exception_master, "exception master"},
5350 {bp_catchpoint, "catchpoint"},
5351 {bp_tracepoint, "tracepoint"},
5352 {bp_fast_tracepoint, "fast tracepoint"},
5353 {bp_static_tracepoint, "static tracepoint"},
5354 {bp_jit_event, "jit events"},
5355 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5356 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5357 };
5358
5359 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5360 || ((int) type != bptypes[(int) type].type))
5361 internal_error (__FILE__, __LINE__,
5362 _("bptypes table does not describe type #%d."),
5363 (int) type);
5364
5365 return bptypes[(int) type].description;
5366 }
5367
5368 /* Print B to gdb_stdout. */
5369
5370 static void
5371 print_one_breakpoint_location (struct breakpoint *b,
5372 struct bp_location *loc,
5373 int loc_number,
5374 struct bp_location **last_loc,
5375 int allflag)
5376 {
5377 struct command_line *l;
5378 static char bpenables[] = "nynny";
5379
5380 struct ui_out *uiout = current_uiout;
5381 int header_of_multiple = 0;
5382 int part_of_multiple = (loc != NULL);
5383 struct value_print_options opts;
5384
5385 get_user_print_options (&opts);
5386
5387 gdb_assert (!loc || loc_number != 0);
5388 /* See comment in print_one_breakpoint concerning treatment of
5389 breakpoints with single disabled location. */
5390 if (loc == NULL
5391 && (b->loc != NULL
5392 && (b->loc->next != NULL || !b->loc->enabled)))
5393 header_of_multiple = 1;
5394 if (loc == NULL)
5395 loc = b->loc;
5396
5397 annotate_record ();
5398
5399 /* 1 */
5400 annotate_field (0);
5401 if (part_of_multiple)
5402 {
5403 char *formatted;
5404 formatted = xstrprintf ("%d.%d", b->number, loc_number);
5405 ui_out_field_string (uiout, "number", formatted);
5406 xfree (formatted);
5407 }
5408 else
5409 {
5410 ui_out_field_int (uiout, "number", b->number);
5411 }
5412
5413 /* 2 */
5414 annotate_field (1);
5415 if (part_of_multiple)
5416 ui_out_field_skip (uiout, "type");
5417 else
5418 ui_out_field_string (uiout, "type", bptype_string (b->type));
5419
5420 /* 3 */
5421 annotate_field (2);
5422 if (part_of_multiple)
5423 ui_out_field_skip (uiout, "disp");
5424 else
5425 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
5426
5427
5428 /* 4 */
5429 annotate_field (3);
5430 if (part_of_multiple)
5431 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
5432 else
5433 ui_out_field_fmt (uiout, "enabled", "%c",
5434 bpenables[(int) b->enable_state]);
5435 ui_out_spaces (uiout, 2);
5436
5437
5438 /* 5 and 6 */
5439 if (b->ops != NULL && b->ops->print_one != NULL)
5440 {
5441 /* Although the print_one can possibly print all locations,
5442 calling it here is not likely to get any nice result. So,
5443 make sure there's just one location. */
5444 gdb_assert (b->loc == NULL || b->loc->next == NULL);
5445 b->ops->print_one (b, last_loc);
5446 }
5447 else
5448 switch (b->type)
5449 {
5450 case bp_none:
5451 internal_error (__FILE__, __LINE__,
5452 _("print_one_breakpoint: bp_none encountered\n"));
5453 break;
5454
5455 case bp_watchpoint:
5456 case bp_hardware_watchpoint:
5457 case bp_read_watchpoint:
5458 case bp_access_watchpoint:
5459 {
5460 struct watchpoint *w = (struct watchpoint *) b;
5461
5462 /* Field 4, the address, is omitted (which makes the columns
5463 not line up too nicely with the headers, but the effect
5464 is relatively readable). */
5465 if (opts.addressprint)
5466 ui_out_field_skip (uiout, "addr");
5467 annotate_field (5);
5468 ui_out_field_string (uiout, "what", w->exp_string);
5469 }
5470 break;
5471
5472 case bp_breakpoint:
5473 case bp_hardware_breakpoint:
5474 case bp_until:
5475 case bp_finish:
5476 case bp_longjmp:
5477 case bp_longjmp_resume:
5478 case bp_exception:
5479 case bp_exception_resume:
5480 case bp_step_resume:
5481 case bp_hp_step_resume:
5482 case bp_watchpoint_scope:
5483 case bp_call_dummy:
5484 case bp_std_terminate:
5485 case bp_shlib_event:
5486 case bp_thread_event:
5487 case bp_overlay_event:
5488 case bp_longjmp_master:
5489 case bp_std_terminate_master:
5490 case bp_exception_master:
5491 case bp_tracepoint:
5492 case bp_fast_tracepoint:
5493 case bp_static_tracepoint:
5494 case bp_jit_event:
5495 case bp_gnu_ifunc_resolver:
5496 case bp_gnu_ifunc_resolver_return:
5497 if (opts.addressprint)
5498 {
5499 annotate_field (4);
5500 if (header_of_multiple)
5501 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
5502 else if (b->loc == NULL || loc->shlib_disabled)
5503 ui_out_field_string (uiout, "addr", "<PENDING>");
5504 else
5505 ui_out_field_core_addr (uiout, "addr",
5506 loc->gdbarch, loc->address);
5507 }
5508 annotate_field (5);
5509 if (!header_of_multiple)
5510 print_breakpoint_location (b, loc);
5511 if (b->loc)
5512 *last_loc = b->loc;
5513 break;
5514 }
5515
5516
5517 /* For backward compatibility, don't display inferiors unless there
5518 are several. */
5519 if (loc != NULL
5520 && !header_of_multiple
5521 && (allflag
5522 || (!gdbarch_has_global_breakpoints (target_gdbarch)
5523 && (number_of_program_spaces () > 1
5524 || number_of_inferiors () > 1)
5525 /* LOC is for existing B, it cannot be in
5526 moribund_locations and thus having NULL OWNER. */
5527 && loc->owner->type != bp_catchpoint)))
5528 {
5529 struct inferior *inf;
5530 int first = 1;
5531
5532 for (inf = inferior_list; inf != NULL; inf = inf->next)
5533 {
5534 if (inf->pspace == loc->pspace)
5535 {
5536 if (first)
5537 {
5538 first = 0;
5539 ui_out_text (uiout, " inf ");
5540 }
5541 else
5542 ui_out_text (uiout, ", ");
5543 ui_out_text (uiout, plongest (inf->num));
5544 }
5545 }
5546 }
5547
5548 if (!part_of_multiple)
5549 {
5550 if (b->thread != -1)
5551 {
5552 /* FIXME: This seems to be redundant and lost here; see the
5553 "stop only in" line a little further down. */
5554 ui_out_text (uiout, " thread ");
5555 ui_out_field_int (uiout, "thread", b->thread);
5556 }
5557 else if (b->task != 0)
5558 {
5559 ui_out_text (uiout, " task ");
5560 ui_out_field_int (uiout, "task", b->task);
5561 }
5562 }
5563
5564 ui_out_text (uiout, "\n");
5565
5566 if (!part_of_multiple)
5567 b->ops->print_one_detail (b, uiout);
5568
5569 if (part_of_multiple && frame_id_p (b->frame_id))
5570 {
5571 annotate_field (6);
5572 ui_out_text (uiout, "\tstop only in stack frame at ");
5573 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
5574 the frame ID. */
5575 ui_out_field_core_addr (uiout, "frame",
5576 b->gdbarch, b->frame_id.stack_addr);
5577 ui_out_text (uiout, "\n");
5578 }
5579
5580 if (!part_of_multiple && b->cond_string)
5581 {
5582 annotate_field (7);
5583 if (is_tracepoint (b))
5584 ui_out_text (uiout, "\ttrace only if ");
5585 else
5586 ui_out_text (uiout, "\tstop only if ");
5587 ui_out_field_string (uiout, "cond", b->cond_string);
5588
5589 /* Print whether the target is doing the breakpoint's condition
5590 evaluation. If GDB is doing the evaluation, don't print anything. */
5591 if (is_breakpoint (b)
5592 && breakpoint_condition_evaluation_mode ()
5593 == condition_evaluation_target)
5594 {
5595 ui_out_text (uiout, " (");
5596 ui_out_field_string (uiout, "evaluated-by",
5597 bp_condition_evaluator (b));
5598 ui_out_text (uiout, " evals)");
5599 }
5600 ui_out_text (uiout, "\n");
5601 }
5602
5603 if (!part_of_multiple && b->thread != -1)
5604 {
5605 /* FIXME should make an annotation for this. */
5606 ui_out_text (uiout, "\tstop only in thread ");
5607 ui_out_field_int (uiout, "thread", b->thread);
5608 ui_out_text (uiout, "\n");
5609 }
5610
5611 if (!part_of_multiple && b->hit_count)
5612 {
5613 /* FIXME should make an annotation for this. */
5614 if (is_catchpoint (b))
5615 ui_out_text (uiout, "\tcatchpoint");
5616 else if (is_tracepoint (b))
5617 ui_out_text (uiout, "\ttracepoint");
5618 else
5619 ui_out_text (uiout, "\tbreakpoint");
5620 ui_out_text (uiout, " already hit ");
5621 ui_out_field_int (uiout, "times", b->hit_count);
5622 if (b->hit_count == 1)
5623 ui_out_text (uiout, " time\n");
5624 else
5625 ui_out_text (uiout, " times\n");
5626 }
5627
5628 /* Output the count also if it is zero, but only if this is mi.
5629 FIXME: Should have a better test for this. */
5630 if (ui_out_is_mi_like_p (uiout))
5631 if (!part_of_multiple && b->hit_count == 0)
5632 ui_out_field_int (uiout, "times", b->hit_count);
5633
5634 if (!part_of_multiple && b->ignore_count)
5635 {
5636 annotate_field (8);
5637 ui_out_text (uiout, "\tignore next ");
5638 ui_out_field_int (uiout, "ignore", b->ignore_count);
5639 ui_out_text (uiout, " hits\n");
5640 }
5641
5642 /* Note that an enable count of 1 corresponds to "enable once"
5643 behavior, which is reported by the combination of enablement and
5644 disposition, so we don't need to mention it here. */
5645 if (!part_of_multiple && b->enable_count > 1)
5646 {
5647 annotate_field (8);
5648 ui_out_text (uiout, "\tdisable after ");
5649 /* Tweak the wording to clarify that ignore and enable counts
5650 are distinct, and have additive effect. */
5651 if (b->ignore_count)
5652 ui_out_text (uiout, "additional ");
5653 else
5654 ui_out_text (uiout, "next ");
5655 ui_out_field_int (uiout, "enable", b->enable_count);
5656 ui_out_text (uiout, " hits\n");
5657 }
5658
5659 if (!part_of_multiple && is_tracepoint (b))
5660 {
5661 struct tracepoint *tp = (struct tracepoint *) b;
5662
5663 if (tp->traceframe_usage)
5664 {
5665 ui_out_text (uiout, "\ttrace buffer usage ");
5666 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5667 ui_out_text (uiout, " bytes\n");
5668 }
5669 }
5670
5671 l = b->commands ? b->commands->commands : NULL;
5672 if (!part_of_multiple && l)
5673 {
5674 struct cleanup *script_chain;
5675
5676 annotate_field (9);
5677 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
5678 print_command_lines (uiout, l, 4);
5679 do_cleanups (script_chain);
5680 }
5681
5682 if (is_tracepoint (b))
5683 {
5684 struct tracepoint *t = (struct tracepoint *) b;
5685
5686 if (!part_of_multiple && t->pass_count)
5687 {
5688 annotate_field (10);
5689 ui_out_text (uiout, "\tpass count ");
5690 ui_out_field_int (uiout, "pass", t->pass_count);
5691 ui_out_text (uiout, " \n");
5692 }
5693 }
5694
5695 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5696 {
5697 if (is_watchpoint (b))
5698 {
5699 struct watchpoint *w = (struct watchpoint *) b;
5700
5701 ui_out_field_string (uiout, "original-location", w->exp_string);
5702 }
5703 else if (b->addr_string)
5704 ui_out_field_string (uiout, "original-location", b->addr_string);
5705 }
5706 }
5707
5708 static void
5709 print_one_breakpoint (struct breakpoint *b,
5710 struct bp_location **last_loc,
5711 int allflag)
5712 {
5713 struct cleanup *bkpt_chain;
5714 struct ui_out *uiout = current_uiout;
5715
5716 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5717
5718 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
5719 do_cleanups (bkpt_chain);
5720
5721 /* If this breakpoint has custom print function,
5722 it's already printed. Otherwise, print individual
5723 locations, if any. */
5724 if (b->ops == NULL || b->ops->print_one == NULL)
5725 {
5726 /* If breakpoint has a single location that is disabled, we
5727 print it as if it had several locations, since otherwise it's
5728 hard to represent "breakpoint enabled, location disabled"
5729 situation.
5730
5731 Note that while hardware watchpoints have several locations
5732 internally, that's not a property exposed to user. */
5733 if (b->loc
5734 && !is_hardware_watchpoint (b)
5735 && (b->loc->next || !b->loc->enabled))
5736 {
5737 struct bp_location *loc;
5738 int n = 1;
5739
5740 for (loc = b->loc; loc; loc = loc->next, ++n)
5741 {
5742 struct cleanup *inner2 =
5743 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5744 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5745 do_cleanups (inner2);
5746 }
5747 }
5748 }
5749 }
5750
5751 static int
5752 breakpoint_address_bits (struct breakpoint *b)
5753 {
5754 int print_address_bits = 0;
5755 struct bp_location *loc;
5756
5757 for (loc = b->loc; loc; loc = loc->next)
5758 {
5759 int addr_bit;
5760
5761 /* Software watchpoints that aren't watching memory don't have
5762 an address to print. */
5763 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5764 continue;
5765
5766 addr_bit = gdbarch_addr_bit (loc->gdbarch);
5767 if (addr_bit > print_address_bits)
5768 print_address_bits = addr_bit;
5769 }
5770
5771 return print_address_bits;
5772 }
5773
5774 struct captured_breakpoint_query_args
5775 {
5776 int bnum;
5777 };
5778
5779 static int
5780 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5781 {
5782 struct captured_breakpoint_query_args *args = data;
5783 struct breakpoint *b;
5784 struct bp_location *dummy_loc = NULL;
5785
5786 ALL_BREAKPOINTS (b)
5787 {
5788 if (args->bnum == b->number)
5789 {
5790 print_one_breakpoint (b, &dummy_loc, 0);
5791 return GDB_RC_OK;
5792 }
5793 }
5794 return GDB_RC_NONE;
5795 }
5796
5797 enum gdb_rc
5798 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5799 char **error_message)
5800 {
5801 struct captured_breakpoint_query_args args;
5802
5803 args.bnum = bnum;
5804 /* For the moment we don't trust print_one_breakpoint() to not throw
5805 an error. */
5806 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5807 error_message, RETURN_MASK_ALL) < 0)
5808 return GDB_RC_FAIL;
5809 else
5810 return GDB_RC_OK;
5811 }
5812
5813 /* Return true if this breakpoint was set by the user, false if it is
5814 internal or momentary. */
5815
5816 int
5817 user_breakpoint_p (struct breakpoint *b)
5818 {
5819 return b->number > 0;
5820 }
5821
5822 /* Print information on user settable breakpoint (watchpoint, etc)
5823 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5824 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5825 FILTER is non-NULL, call it on each breakpoint and only include the
5826 ones for which it returns non-zero. Return the total number of
5827 breakpoints listed. */
5828
5829 static int
5830 breakpoint_1 (char *args, int allflag,
5831 int (*filter) (const struct breakpoint *))
5832 {
5833 struct breakpoint *b;
5834 struct bp_location *last_loc = NULL;
5835 int nr_printable_breakpoints;
5836 struct cleanup *bkpttbl_chain;
5837 struct value_print_options opts;
5838 int print_address_bits = 0;
5839 int print_type_col_width = 14;
5840 struct ui_out *uiout = current_uiout;
5841
5842 get_user_print_options (&opts);
5843
5844 /* Compute the number of rows in the table, as well as the size
5845 required for address fields. */
5846 nr_printable_breakpoints = 0;
5847 ALL_BREAKPOINTS (b)
5848 {
5849 /* If we have a filter, only list the breakpoints it accepts. */
5850 if (filter && !filter (b))
5851 continue;
5852
5853 /* If we have an "args" string, it is a list of breakpoints to
5854 accept. Skip the others. */
5855 if (args != NULL && *args != '\0')
5856 {
5857 if (allflag && parse_and_eval_long (args) != b->number)
5858 continue;
5859 if (!allflag && !number_is_in_list (args, b->number))
5860 continue;
5861 }
5862
5863 if (allflag || user_breakpoint_p (b))
5864 {
5865 int addr_bit, type_len;
5866
5867 addr_bit = breakpoint_address_bits (b);
5868 if (addr_bit > print_address_bits)
5869 print_address_bits = addr_bit;
5870
5871 type_len = strlen (bptype_string (b->type));
5872 if (type_len > print_type_col_width)
5873 print_type_col_width = type_len;
5874
5875 nr_printable_breakpoints++;
5876 }
5877 }
5878
5879 if (opts.addressprint)
5880 bkpttbl_chain
5881 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5882 nr_printable_breakpoints,
5883 "BreakpointTable");
5884 else
5885 bkpttbl_chain
5886 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5887 nr_printable_breakpoints,
5888 "BreakpointTable");
5889
5890 if (nr_printable_breakpoints > 0)
5891 annotate_breakpoints_headers ();
5892 if (nr_printable_breakpoints > 0)
5893 annotate_field (0);
5894 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
5895 if (nr_printable_breakpoints > 0)
5896 annotate_field (1);
5897 ui_out_table_header (uiout, print_type_col_width, ui_left,
5898 "type", "Type"); /* 2 */
5899 if (nr_printable_breakpoints > 0)
5900 annotate_field (2);
5901 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
5902 if (nr_printable_breakpoints > 0)
5903 annotate_field (3);
5904 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
5905 if (opts.addressprint)
5906 {
5907 if (nr_printable_breakpoints > 0)
5908 annotate_field (4);
5909 if (print_address_bits <= 32)
5910 ui_out_table_header (uiout, 10, ui_left,
5911 "addr", "Address"); /* 5 */
5912 else
5913 ui_out_table_header (uiout, 18, ui_left,
5914 "addr", "Address"); /* 5 */
5915 }
5916 if (nr_printable_breakpoints > 0)
5917 annotate_field (5);
5918 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5919 ui_out_table_body (uiout);
5920 if (nr_printable_breakpoints > 0)
5921 annotate_breakpoints_table ();
5922
5923 ALL_BREAKPOINTS (b)
5924 {
5925 QUIT;
5926 /* If we have a filter, only list the breakpoints it accepts. */
5927 if (filter && !filter (b))
5928 continue;
5929
5930 /* If we have an "args" string, it is a list of breakpoints to
5931 accept. Skip the others. */
5932
5933 if (args != NULL && *args != '\0')
5934 {
5935 if (allflag) /* maintenance info breakpoint */
5936 {
5937 if (parse_and_eval_long (args) != b->number)
5938 continue;
5939 }
5940 else /* all others */
5941 {
5942 if (!number_is_in_list (args, b->number))
5943 continue;
5944 }
5945 }
5946 /* We only print out user settable breakpoints unless the
5947 allflag is set. */
5948 if (allflag || user_breakpoint_p (b))
5949 print_one_breakpoint (b, &last_loc, allflag);
5950 }
5951
5952 do_cleanups (bkpttbl_chain);
5953
5954 if (nr_printable_breakpoints == 0)
5955 {
5956 /* If there's a filter, let the caller decide how to report
5957 empty list. */
5958 if (!filter)
5959 {
5960 if (args == NULL || *args == '\0')
5961 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5962 else
5963 ui_out_message (uiout, 0,
5964 "No breakpoint or watchpoint matching '%s'.\n",
5965 args);
5966 }
5967 }
5968 else
5969 {
5970 if (last_loc && !server_command)
5971 set_next_address (last_loc->gdbarch, last_loc->address);
5972 }
5973
5974 /* FIXME? Should this be moved up so that it is only called when
5975 there have been breakpoints? */
5976 annotate_breakpoints_table_end ();
5977
5978 return nr_printable_breakpoints;
5979 }
5980
5981 /* Display the value of default-collect in a way that is generally
5982 compatible with the breakpoint list. */
5983
5984 static void
5985 default_collect_info (void)
5986 {
5987 struct ui_out *uiout = current_uiout;
5988
5989 /* If it has no value (which is frequently the case), say nothing; a
5990 message like "No default-collect." gets in user's face when it's
5991 not wanted. */
5992 if (!*default_collect)
5993 return;
5994
5995 /* The following phrase lines up nicely with per-tracepoint collect
5996 actions. */
5997 ui_out_text (uiout, "default collect ");
5998 ui_out_field_string (uiout, "default-collect", default_collect);
5999 ui_out_text (uiout, " \n");
6000 }
6001
6002 static void
6003 breakpoints_info (char *args, int from_tty)
6004 {
6005 breakpoint_1 (args, 0, NULL);
6006
6007 default_collect_info ();
6008 }
6009
6010 static void
6011 watchpoints_info (char *args, int from_tty)
6012 {
6013 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6014 struct ui_out *uiout = current_uiout;
6015
6016 if (num_printed == 0)
6017 {
6018 if (args == NULL || *args == '\0')
6019 ui_out_message (uiout, 0, "No watchpoints.\n");
6020 else
6021 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6022 }
6023 }
6024
6025 static void
6026 maintenance_info_breakpoints (char *args, int from_tty)
6027 {
6028 breakpoint_1 (args, 1, NULL);
6029
6030 default_collect_info ();
6031 }
6032
6033 static int
6034 breakpoint_has_pc (struct breakpoint *b,
6035 struct program_space *pspace,
6036 CORE_ADDR pc, struct obj_section *section)
6037 {
6038 struct bp_location *bl = b->loc;
6039
6040 for (; bl; bl = bl->next)
6041 {
6042 if (bl->pspace == pspace
6043 && bl->address == pc
6044 && (!overlay_debugging || bl->section == section))
6045 return 1;
6046 }
6047 return 0;
6048 }
6049
6050 /* Print a message describing any user-breakpoints set at PC. This
6051 concerns with logical breakpoints, so we match program spaces, not
6052 address spaces. */
6053
6054 static void
6055 describe_other_breakpoints (struct gdbarch *gdbarch,
6056 struct program_space *pspace, CORE_ADDR pc,
6057 struct obj_section *section, int thread)
6058 {
6059 int others = 0;
6060 struct breakpoint *b;
6061
6062 ALL_BREAKPOINTS (b)
6063 others += (user_breakpoint_p (b)
6064 && breakpoint_has_pc (b, pspace, pc, section));
6065 if (others > 0)
6066 {
6067 if (others == 1)
6068 printf_filtered (_("Note: breakpoint "));
6069 else /* if (others == ???) */
6070 printf_filtered (_("Note: breakpoints "));
6071 ALL_BREAKPOINTS (b)
6072 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6073 {
6074 others--;
6075 printf_filtered ("%d", b->number);
6076 if (b->thread == -1 && thread != -1)
6077 printf_filtered (" (all threads)");
6078 else if (b->thread != -1)
6079 printf_filtered (" (thread %d)", b->thread);
6080 printf_filtered ("%s%s ",
6081 ((b->enable_state == bp_disabled
6082 || b->enable_state == bp_call_disabled)
6083 ? " (disabled)"
6084 : b->enable_state == bp_permanent
6085 ? " (permanent)"
6086 : ""),
6087 (others > 1) ? ","
6088 : ((others == 1) ? " and" : ""));
6089 }
6090 printf_filtered (_("also set at pc "));
6091 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6092 printf_filtered (".\n");
6093 }
6094 }
6095 \f
6096
6097 /* Return true iff it is meaningful to use the address member of
6098 BPT. For some breakpoint types, the address member is irrelevant
6099 and it makes no sense to attempt to compare it to other addresses
6100 (or use it for any other purpose either).
6101
6102 More specifically, each of the following breakpoint types will
6103 always have a zero valued address and we don't want to mark
6104 breakpoints of any of these types to be a duplicate of an actual
6105 breakpoint at address zero:
6106
6107 bp_watchpoint
6108 bp_catchpoint
6109
6110 */
6111
6112 static int
6113 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6114 {
6115 enum bptype type = bpt->type;
6116
6117 return (type != bp_watchpoint && type != bp_catchpoint);
6118 }
6119
6120 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6121 true if LOC1 and LOC2 represent the same watchpoint location. */
6122
6123 static int
6124 watchpoint_locations_match (struct bp_location *loc1,
6125 struct bp_location *loc2)
6126 {
6127 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6128 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6129
6130 /* Both of them must exist. */
6131 gdb_assert (w1 != NULL);
6132 gdb_assert (w2 != NULL);
6133
6134 /* If the target can evaluate the condition expression in hardware,
6135 then we we need to insert both watchpoints even if they are at
6136 the same place. Otherwise the watchpoint will only trigger when
6137 the condition of whichever watchpoint was inserted evaluates to
6138 true, not giving a chance for GDB to check the condition of the
6139 other watchpoint. */
6140 if ((w1->cond_exp
6141 && target_can_accel_watchpoint_condition (loc1->address,
6142 loc1->length,
6143 loc1->watchpoint_type,
6144 w1->cond_exp))
6145 || (w2->cond_exp
6146 && target_can_accel_watchpoint_condition (loc2->address,
6147 loc2->length,
6148 loc2->watchpoint_type,
6149 w2->cond_exp)))
6150 return 0;
6151
6152 /* Note that this checks the owner's type, not the location's. In
6153 case the target does not support read watchpoints, but does
6154 support access watchpoints, we'll have bp_read_watchpoint
6155 watchpoints with hw_access locations. Those should be considered
6156 duplicates of hw_read locations. The hw_read locations will
6157 become hw_access locations later. */
6158 return (loc1->owner->type == loc2->owner->type
6159 && loc1->pspace->aspace == loc2->pspace->aspace
6160 && loc1->address == loc2->address
6161 && loc1->length == loc2->length);
6162 }
6163
6164 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6165 same breakpoint location. In most targets, this can only be true
6166 if ASPACE1 matches ASPACE2. On targets that have global
6167 breakpoints, the address space doesn't really matter. */
6168
6169 static int
6170 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6171 struct address_space *aspace2, CORE_ADDR addr2)
6172 {
6173 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6174 || aspace1 == aspace2)
6175 && addr1 == addr2);
6176 }
6177
6178 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6179 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6180 matches ASPACE2. On targets that have global breakpoints, the address
6181 space doesn't really matter. */
6182
6183 static int
6184 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6185 int len1, struct address_space *aspace2,
6186 CORE_ADDR addr2)
6187 {
6188 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6189 || aspace1 == aspace2)
6190 && addr2 >= addr1 && addr2 < addr1 + len1);
6191 }
6192
6193 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6194 a ranged breakpoint. In most targets, a match happens only if ASPACE
6195 matches the breakpoint's address space. On targets that have global
6196 breakpoints, the address space doesn't really matter. */
6197
6198 static int
6199 breakpoint_location_address_match (struct bp_location *bl,
6200 struct address_space *aspace,
6201 CORE_ADDR addr)
6202 {
6203 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6204 aspace, addr)
6205 || (bl->length
6206 && breakpoint_address_match_range (bl->pspace->aspace,
6207 bl->address, bl->length,
6208 aspace, addr)));
6209 }
6210
6211 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6212 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6213 true, otherwise returns false. */
6214
6215 static int
6216 tracepoint_locations_match (struct bp_location *loc1,
6217 struct bp_location *loc2)
6218 {
6219 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6220 /* Since tracepoint locations are never duplicated with others', tracepoint
6221 locations at the same address of different tracepoints are regarded as
6222 different locations. */
6223 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6224 else
6225 return 0;
6226 }
6227
6228 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6229 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6230 represent the same location. */
6231
6232 static int
6233 breakpoint_locations_match (struct bp_location *loc1,
6234 struct bp_location *loc2)
6235 {
6236 int hw_point1, hw_point2;
6237
6238 /* Both of them must not be in moribund_locations. */
6239 gdb_assert (loc1->owner != NULL);
6240 gdb_assert (loc2->owner != NULL);
6241
6242 hw_point1 = is_hardware_watchpoint (loc1->owner);
6243 hw_point2 = is_hardware_watchpoint (loc2->owner);
6244
6245 if (hw_point1 != hw_point2)
6246 return 0;
6247 else if (hw_point1)
6248 return watchpoint_locations_match (loc1, loc2);
6249 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6250 return tracepoint_locations_match (loc1, loc2);
6251 else
6252 /* We compare bp_location.length in order to cover ranged breakpoints. */
6253 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6254 loc2->pspace->aspace, loc2->address)
6255 && loc1->length == loc2->length);
6256 }
6257
6258 static void
6259 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6260 int bnum, int have_bnum)
6261 {
6262 /* The longest string possibly returned by hex_string_custom
6263 is 50 chars. These must be at least that big for safety. */
6264 char astr1[64];
6265 char astr2[64];
6266
6267 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6268 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6269 if (have_bnum)
6270 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6271 bnum, astr1, astr2);
6272 else
6273 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6274 }
6275
6276 /* Adjust a breakpoint's address to account for architectural
6277 constraints on breakpoint placement. Return the adjusted address.
6278 Note: Very few targets require this kind of adjustment. For most
6279 targets, this function is simply the identity function. */
6280
6281 static CORE_ADDR
6282 adjust_breakpoint_address (struct gdbarch *gdbarch,
6283 CORE_ADDR bpaddr, enum bptype bptype)
6284 {
6285 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6286 {
6287 /* Very few targets need any kind of breakpoint adjustment. */
6288 return bpaddr;
6289 }
6290 else if (bptype == bp_watchpoint
6291 || bptype == bp_hardware_watchpoint
6292 || bptype == bp_read_watchpoint
6293 || bptype == bp_access_watchpoint
6294 || bptype == bp_catchpoint)
6295 {
6296 /* Watchpoints and the various bp_catch_* eventpoints should not
6297 have their addresses modified. */
6298 return bpaddr;
6299 }
6300 else
6301 {
6302 CORE_ADDR adjusted_bpaddr;
6303
6304 /* Some targets have architectural constraints on the placement
6305 of breakpoint instructions. Obtain the adjusted address. */
6306 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6307
6308 /* An adjusted breakpoint address can significantly alter
6309 a user's expectations. Print a warning if an adjustment
6310 is required. */
6311 if (adjusted_bpaddr != bpaddr)
6312 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6313
6314 return adjusted_bpaddr;
6315 }
6316 }
6317
6318 void
6319 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6320 struct breakpoint *owner)
6321 {
6322 memset (loc, 0, sizeof (*loc));
6323
6324 gdb_assert (ops != NULL);
6325
6326 loc->ops = ops;
6327 loc->owner = owner;
6328 loc->cond = NULL;
6329 loc->cond_bytecode = NULL;
6330 loc->shlib_disabled = 0;
6331 loc->enabled = 1;
6332
6333 switch (owner->type)
6334 {
6335 case bp_breakpoint:
6336 case bp_until:
6337 case bp_finish:
6338 case bp_longjmp:
6339 case bp_longjmp_resume:
6340 case bp_exception:
6341 case bp_exception_resume:
6342 case bp_step_resume:
6343 case bp_hp_step_resume:
6344 case bp_watchpoint_scope:
6345 case bp_call_dummy:
6346 case bp_std_terminate:
6347 case bp_shlib_event:
6348 case bp_thread_event:
6349 case bp_overlay_event:
6350 case bp_jit_event:
6351 case bp_longjmp_master:
6352 case bp_std_terminate_master:
6353 case bp_exception_master:
6354 case bp_gnu_ifunc_resolver:
6355 case bp_gnu_ifunc_resolver_return:
6356 loc->loc_type = bp_loc_software_breakpoint;
6357 mark_breakpoint_location_modified (loc);
6358 break;
6359 case bp_hardware_breakpoint:
6360 loc->loc_type = bp_loc_hardware_breakpoint;
6361 mark_breakpoint_location_modified (loc);
6362 break;
6363 case bp_hardware_watchpoint:
6364 case bp_read_watchpoint:
6365 case bp_access_watchpoint:
6366 loc->loc_type = bp_loc_hardware_watchpoint;
6367 break;
6368 case bp_watchpoint:
6369 case bp_catchpoint:
6370 case bp_tracepoint:
6371 case bp_fast_tracepoint:
6372 case bp_static_tracepoint:
6373 loc->loc_type = bp_loc_other;
6374 break;
6375 default:
6376 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6377 }
6378
6379 loc->refc = 1;
6380 }
6381
6382 /* Allocate a struct bp_location. */
6383
6384 static struct bp_location *
6385 allocate_bp_location (struct breakpoint *bpt)
6386 {
6387 return bpt->ops->allocate_location (bpt);
6388 }
6389
6390 static void
6391 free_bp_location (struct bp_location *loc)
6392 {
6393 loc->ops->dtor (loc);
6394 xfree (loc);
6395 }
6396
6397 /* Increment reference count. */
6398
6399 static void
6400 incref_bp_location (struct bp_location *bl)
6401 {
6402 ++bl->refc;
6403 }
6404
6405 /* Decrement reference count. If the reference count reaches 0,
6406 destroy the bp_location. Sets *BLP to NULL. */
6407
6408 static void
6409 decref_bp_location (struct bp_location **blp)
6410 {
6411 gdb_assert ((*blp)->refc > 0);
6412
6413 if (--(*blp)->refc == 0)
6414 free_bp_location (*blp);
6415 *blp = NULL;
6416 }
6417
6418 /* Add breakpoint B at the end of the global breakpoint chain. */
6419
6420 static void
6421 add_to_breakpoint_chain (struct breakpoint *b)
6422 {
6423 struct breakpoint *b1;
6424
6425 /* Add this breakpoint to the end of the chain so that a list of
6426 breakpoints will come out in order of increasing numbers. */
6427
6428 b1 = breakpoint_chain;
6429 if (b1 == 0)
6430 breakpoint_chain = b;
6431 else
6432 {
6433 while (b1->next)
6434 b1 = b1->next;
6435 b1->next = b;
6436 }
6437 }
6438
6439 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
6440
6441 static void
6442 init_raw_breakpoint_without_location (struct breakpoint *b,
6443 struct gdbarch *gdbarch,
6444 enum bptype bptype,
6445 const struct breakpoint_ops *ops)
6446 {
6447 memset (b, 0, sizeof (*b));
6448
6449 gdb_assert (ops != NULL);
6450
6451 b->ops = ops;
6452 b->type = bptype;
6453 b->gdbarch = gdbarch;
6454 b->language = current_language->la_language;
6455 b->input_radix = input_radix;
6456 b->thread = -1;
6457 b->enable_state = bp_enabled;
6458 b->next = 0;
6459 b->silent = 0;
6460 b->ignore_count = 0;
6461 b->commands = NULL;
6462 b->frame_id = null_frame_id;
6463 b->condition_not_parsed = 0;
6464 b->py_bp_object = NULL;
6465 b->related_breakpoint = b;
6466 }
6467
6468 /* Helper to set_raw_breakpoint below. Creates a breakpoint
6469 that has type BPTYPE and has no locations as yet. */
6470
6471 static struct breakpoint *
6472 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
6473 enum bptype bptype,
6474 const struct breakpoint_ops *ops)
6475 {
6476 struct breakpoint *b = XNEW (struct breakpoint);
6477
6478 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6479 add_to_breakpoint_chain (b);
6480 return b;
6481 }
6482
6483 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
6484 resolutions should be made as the user specified the location explicitly
6485 enough. */
6486
6487 static void
6488 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
6489 {
6490 gdb_assert (loc->owner != NULL);
6491
6492 if (loc->owner->type == bp_breakpoint
6493 || loc->owner->type == bp_hardware_breakpoint
6494 || is_tracepoint (loc->owner))
6495 {
6496 int is_gnu_ifunc;
6497 const char *function_name;
6498
6499 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6500 NULL, NULL, &is_gnu_ifunc);
6501
6502 if (is_gnu_ifunc && !explicit_loc)
6503 {
6504 struct breakpoint *b = loc->owner;
6505
6506 gdb_assert (loc->pspace == current_program_space);
6507 if (gnu_ifunc_resolve_name (function_name,
6508 &loc->requested_address))
6509 {
6510 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
6511 loc->address = adjust_breakpoint_address (loc->gdbarch,
6512 loc->requested_address,
6513 b->type);
6514 }
6515 else if (b->type == bp_breakpoint && b->loc == loc
6516 && loc->next == NULL && b->related_breakpoint == b)
6517 {
6518 /* Create only the whole new breakpoint of this type but do not
6519 mess more complicated breakpoints with multiple locations. */
6520 b->type = bp_gnu_ifunc_resolver;
6521 }
6522 }
6523
6524 if (function_name)
6525 loc->function_name = xstrdup (function_name);
6526 }
6527 }
6528
6529 /* Attempt to determine architecture of location identified by SAL. */
6530 struct gdbarch *
6531 get_sal_arch (struct symtab_and_line sal)
6532 {
6533 if (sal.section)
6534 return get_objfile_arch (sal.section->objfile);
6535 if (sal.symtab)
6536 return get_objfile_arch (sal.symtab->objfile);
6537
6538 return NULL;
6539 }
6540
6541 /* Low level routine for partially initializing a breakpoint of type
6542 BPTYPE. The newly created breakpoint's address, section, source
6543 file name, and line number are provided by SAL.
6544
6545 It is expected that the caller will complete the initialization of
6546 the newly created breakpoint struct as well as output any status
6547 information regarding the creation of a new breakpoint. */
6548
6549 static void
6550 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
6551 struct symtab_and_line sal, enum bptype bptype,
6552 const struct breakpoint_ops *ops)
6553 {
6554 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6555
6556 add_location_to_breakpoint (b, &sal);
6557
6558 if (bptype != bp_catchpoint)
6559 gdb_assert (sal.pspace != NULL);
6560
6561 /* Store the program space that was used to set the breakpoint,
6562 except for ordinary breakpoints, which are independent of the
6563 program space. */
6564 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6565 b->pspace = sal.pspace;
6566
6567 breakpoints_changed ();
6568 }
6569
6570 /* set_raw_breakpoint is a low level routine for allocating and
6571 partially initializing a breakpoint of type BPTYPE. The newly
6572 created breakpoint's address, section, source file name, and line
6573 number are provided by SAL. The newly created and partially
6574 initialized breakpoint is added to the breakpoint chain and
6575 is also returned as the value of this function.
6576
6577 It is expected that the caller will complete the initialization of
6578 the newly created breakpoint struct as well as output any status
6579 information regarding the creation of a new breakpoint. In
6580 particular, set_raw_breakpoint does NOT set the breakpoint
6581 number! Care should be taken to not allow an error to occur
6582 prior to completing the initialization of the breakpoint. If this
6583 should happen, a bogus breakpoint will be left on the chain. */
6584
6585 struct breakpoint *
6586 set_raw_breakpoint (struct gdbarch *gdbarch,
6587 struct symtab_and_line sal, enum bptype bptype,
6588 const struct breakpoint_ops *ops)
6589 {
6590 struct breakpoint *b = XNEW (struct breakpoint);
6591
6592 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
6593 add_to_breakpoint_chain (b);
6594 return b;
6595 }
6596
6597
6598 /* Note that the breakpoint object B describes a permanent breakpoint
6599 instruction, hard-wired into the inferior's code. */
6600 void
6601 make_breakpoint_permanent (struct breakpoint *b)
6602 {
6603 struct bp_location *bl;
6604
6605 b->enable_state = bp_permanent;
6606
6607 /* By definition, permanent breakpoints are already present in the
6608 code. Mark all locations as inserted. For now,
6609 make_breakpoint_permanent is called in just one place, so it's
6610 hard to say if it's reasonable to have permanent breakpoint with
6611 multiple locations or not, but it's easy to implement. */
6612 for (bl = b->loc; bl; bl = bl->next)
6613 bl->inserted = 1;
6614 }
6615
6616 /* Call this routine when stepping and nexting to enable a breakpoint
6617 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6618 initiated the operation. */
6619
6620 void
6621 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
6622 {
6623 struct breakpoint *b, *b_tmp;
6624 int thread = tp->num;
6625
6626 /* To avoid having to rescan all objfile symbols at every step,
6627 we maintain a list of continually-inserted but always disabled
6628 longjmp "master" breakpoints. Here, we simply create momentary
6629 clones of those and enable them for the requested thread. */
6630 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6631 if (b->pspace == current_program_space
6632 && (b->type == bp_longjmp_master
6633 || b->type == bp_exception_master))
6634 {
6635 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6636 struct breakpoint *clone;
6637
6638 clone = momentary_breakpoint_from_master (b, type,
6639 &momentary_breakpoint_ops);
6640 clone->thread = thread;
6641 }
6642
6643 tp->initiating_frame = frame;
6644 }
6645
6646 /* Delete all longjmp breakpoints from THREAD. */
6647 void
6648 delete_longjmp_breakpoint (int thread)
6649 {
6650 struct breakpoint *b, *b_tmp;
6651
6652 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6653 if (b->type == bp_longjmp || b->type == bp_exception)
6654 {
6655 if (b->thread == thread)
6656 delete_breakpoint (b);
6657 }
6658 }
6659
6660 void
6661 delete_longjmp_breakpoint_at_next_stop (int thread)
6662 {
6663 struct breakpoint *b, *b_tmp;
6664
6665 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6666 if (b->type == bp_longjmp || b->type == bp_exception)
6667 {
6668 if (b->thread == thread)
6669 b->disposition = disp_del_at_next_stop;
6670 }
6671 }
6672
6673 void
6674 enable_overlay_breakpoints (void)
6675 {
6676 struct breakpoint *b;
6677
6678 ALL_BREAKPOINTS (b)
6679 if (b->type == bp_overlay_event)
6680 {
6681 b->enable_state = bp_enabled;
6682 update_global_location_list (1);
6683 overlay_events_enabled = 1;
6684 }
6685 }
6686
6687 void
6688 disable_overlay_breakpoints (void)
6689 {
6690 struct breakpoint *b;
6691
6692 ALL_BREAKPOINTS (b)
6693 if (b->type == bp_overlay_event)
6694 {
6695 b->enable_state = bp_disabled;
6696 update_global_location_list (0);
6697 overlay_events_enabled = 0;
6698 }
6699 }
6700
6701 /* Set an active std::terminate breakpoint for each std::terminate
6702 master breakpoint. */
6703 void
6704 set_std_terminate_breakpoint (void)
6705 {
6706 struct breakpoint *b, *b_tmp;
6707
6708 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6709 if (b->pspace == current_program_space
6710 && b->type == bp_std_terminate_master)
6711 {
6712 momentary_breakpoint_from_master (b, bp_std_terminate,
6713 &momentary_breakpoint_ops);
6714 }
6715 }
6716
6717 /* Delete all the std::terminate breakpoints. */
6718 void
6719 delete_std_terminate_breakpoint (void)
6720 {
6721 struct breakpoint *b, *b_tmp;
6722
6723 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6724 if (b->type == bp_std_terminate)
6725 delete_breakpoint (b);
6726 }
6727
6728 struct breakpoint *
6729 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6730 {
6731 struct breakpoint *b;
6732
6733 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6734 &internal_breakpoint_ops);
6735
6736 b->enable_state = bp_enabled;
6737 /* addr_string has to be used or breakpoint_re_set will delete me. */
6738 b->addr_string
6739 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
6740
6741 update_global_location_list_nothrow (1);
6742
6743 return b;
6744 }
6745
6746 void
6747 remove_thread_event_breakpoints (void)
6748 {
6749 struct breakpoint *b, *b_tmp;
6750
6751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6752 if (b->type == bp_thread_event
6753 && b->loc->pspace == current_program_space)
6754 delete_breakpoint (b);
6755 }
6756
6757 struct lang_and_radix
6758 {
6759 enum language lang;
6760 int radix;
6761 };
6762
6763 /* Create a breakpoint for JIT code registration and unregistration. */
6764
6765 struct breakpoint *
6766 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6767 {
6768 struct breakpoint *b;
6769
6770 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6771 &internal_breakpoint_ops);
6772 update_global_location_list_nothrow (1);
6773 return b;
6774 }
6775
6776 /* Remove JIT code registration and unregistration breakpoint(s). */
6777
6778 void
6779 remove_jit_event_breakpoints (void)
6780 {
6781 struct breakpoint *b, *b_tmp;
6782
6783 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6784 if (b->type == bp_jit_event
6785 && b->loc->pspace == current_program_space)
6786 delete_breakpoint (b);
6787 }
6788
6789 void
6790 remove_solib_event_breakpoints (void)
6791 {
6792 struct breakpoint *b, *b_tmp;
6793
6794 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6795 if (b->type == bp_shlib_event
6796 && b->loc->pspace == current_program_space)
6797 delete_breakpoint (b);
6798 }
6799
6800 struct breakpoint *
6801 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6802 {
6803 struct breakpoint *b;
6804
6805 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6806 &internal_breakpoint_ops);
6807 update_global_location_list_nothrow (1);
6808 return b;
6809 }
6810
6811 /* Disable any breakpoints that are on code in shared libraries. Only
6812 apply to enabled breakpoints, disabled ones can just stay disabled. */
6813
6814 void
6815 disable_breakpoints_in_shlibs (void)
6816 {
6817 struct bp_location *loc, **locp_tmp;
6818
6819 ALL_BP_LOCATIONS (loc, locp_tmp)
6820 {
6821 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6822 struct breakpoint *b = loc->owner;
6823
6824 /* We apply the check to all breakpoints, including disabled for
6825 those with loc->duplicate set. This is so that when breakpoint
6826 becomes enabled, or the duplicate is removed, gdb will try to
6827 insert all breakpoints. If we don't set shlib_disabled here,
6828 we'll try to insert those breakpoints and fail. */
6829 if (((b->type == bp_breakpoint)
6830 || (b->type == bp_jit_event)
6831 || (b->type == bp_hardware_breakpoint)
6832 || (is_tracepoint (b)))
6833 && loc->pspace == current_program_space
6834 && !loc->shlib_disabled
6835 #ifdef PC_SOLIB
6836 && PC_SOLIB (loc->address)
6837 #else
6838 && solib_name_from_address (loc->pspace, loc->address)
6839 #endif
6840 )
6841 {
6842 loc->shlib_disabled = 1;
6843 }
6844 }
6845 }
6846
6847 /* Disable any breakpoints and tracepoints that are in an unloaded shared
6848 library. Only apply to enabled breakpoints, disabled ones can just stay
6849 disabled. */
6850
6851 static void
6852 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6853 {
6854 struct bp_location *loc, **locp_tmp;
6855 int disabled_shlib_breaks = 0;
6856
6857 /* SunOS a.out shared libraries are always mapped, so do not
6858 disable breakpoints; they will only be reported as unloaded
6859 through clear_solib when GDB discards its shared library
6860 list. See clear_solib for more information. */
6861 if (exec_bfd != NULL
6862 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6863 return;
6864
6865 ALL_BP_LOCATIONS (loc, locp_tmp)
6866 {
6867 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6868 struct breakpoint *b = loc->owner;
6869
6870 if (solib->pspace == loc->pspace
6871 && !loc->shlib_disabled
6872 && (((b->type == bp_breakpoint
6873 || b->type == bp_jit_event
6874 || b->type == bp_hardware_breakpoint)
6875 && (loc->loc_type == bp_loc_hardware_breakpoint
6876 || loc->loc_type == bp_loc_software_breakpoint))
6877 || is_tracepoint (b))
6878 && solib_contains_address_p (solib, loc->address))
6879 {
6880 loc->shlib_disabled = 1;
6881 /* At this point, we cannot rely on remove_breakpoint
6882 succeeding so we must mark the breakpoint as not inserted
6883 to prevent future errors occurring in remove_breakpoints. */
6884 loc->inserted = 0;
6885
6886 /* This may cause duplicate notifications for the same breakpoint. */
6887 observer_notify_breakpoint_modified (b);
6888
6889 if (!disabled_shlib_breaks)
6890 {
6891 target_terminal_ours_for_output ();
6892 warning (_("Temporarily disabling breakpoints "
6893 "for unloaded shared library \"%s\""),
6894 solib->so_name);
6895 }
6896 disabled_shlib_breaks = 1;
6897 }
6898 }
6899 }
6900
6901 /* FORK & VFORK catchpoints. */
6902
6903 /* An instance of this type is used to represent a fork or vfork
6904 catchpoint. It includes a "struct breakpoint" as a kind of base
6905 class; users downcast to "struct breakpoint *" when needed. A
6906 breakpoint is really of this type iff its ops pointer points to
6907 CATCH_FORK_BREAKPOINT_OPS. */
6908
6909 struct fork_catchpoint
6910 {
6911 /* The base class. */
6912 struct breakpoint base;
6913
6914 /* Process id of a child process whose forking triggered this
6915 catchpoint. This field is only valid immediately after this
6916 catchpoint has triggered. */
6917 ptid_t forked_inferior_pid;
6918 };
6919
6920 /* Implement the "insert" breakpoint_ops method for fork
6921 catchpoints. */
6922
6923 static int
6924 insert_catch_fork (struct bp_location *bl)
6925 {
6926 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
6927 }
6928
6929 /* Implement the "remove" breakpoint_ops method for fork
6930 catchpoints. */
6931
6932 static int
6933 remove_catch_fork (struct bp_location *bl)
6934 {
6935 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6936 }
6937
6938 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
6939 catchpoints. */
6940
6941 static int
6942 breakpoint_hit_catch_fork (const struct bp_location *bl,
6943 struct address_space *aspace, CORE_ADDR bp_addr,
6944 const struct target_waitstatus *ws)
6945 {
6946 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6947
6948 if (ws->kind != TARGET_WAITKIND_FORKED)
6949 return 0;
6950
6951 c->forked_inferior_pid = ws->value.related_pid;
6952 return 1;
6953 }
6954
6955 /* Implement the "print_it" breakpoint_ops method for fork
6956 catchpoints. */
6957
6958 static enum print_stop_action
6959 print_it_catch_fork (bpstat bs)
6960 {
6961 struct ui_out *uiout = current_uiout;
6962 struct breakpoint *b = bs->breakpoint_at;
6963 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
6964
6965 annotate_catchpoint (b->number);
6966 if (b->disposition == disp_del)
6967 ui_out_text (uiout, "\nTemporary catchpoint ");
6968 else
6969 ui_out_text (uiout, "\nCatchpoint ");
6970 if (ui_out_is_mi_like_p (uiout))
6971 {
6972 ui_out_field_string (uiout, "reason",
6973 async_reason_lookup (EXEC_ASYNC_FORK));
6974 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6975 }
6976 ui_out_field_int (uiout, "bkptno", b->number);
6977 ui_out_text (uiout, " (forked process ");
6978 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6979 ui_out_text (uiout, "), ");
6980 return PRINT_SRC_AND_LOC;
6981 }
6982
6983 /* Implement the "print_one" breakpoint_ops method for fork
6984 catchpoints. */
6985
6986 static void
6987 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
6988 {
6989 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6990 struct value_print_options opts;
6991 struct ui_out *uiout = current_uiout;
6992
6993 get_user_print_options (&opts);
6994
6995 /* Field 4, the address, is omitted (which makes the columns not
6996 line up too nicely with the headers, but the effect is relatively
6997 readable). */
6998 if (opts.addressprint)
6999 ui_out_field_skip (uiout, "addr");
7000 annotate_field (5);
7001 ui_out_text (uiout, "fork");
7002 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7003 {
7004 ui_out_text (uiout, ", process ");
7005 ui_out_field_int (uiout, "what",
7006 ptid_get_pid (c->forked_inferior_pid));
7007 ui_out_spaces (uiout, 1);
7008 }
7009 }
7010
7011 /* Implement the "print_mention" breakpoint_ops method for fork
7012 catchpoints. */
7013
7014 static void
7015 print_mention_catch_fork (struct breakpoint *b)
7016 {
7017 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7018 }
7019
7020 /* Implement the "print_recreate" breakpoint_ops method for fork
7021 catchpoints. */
7022
7023 static void
7024 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7025 {
7026 fprintf_unfiltered (fp, "catch fork");
7027 print_recreate_thread (b, fp);
7028 }
7029
7030 /* The breakpoint_ops structure to be used in fork catchpoints. */
7031
7032 static struct breakpoint_ops catch_fork_breakpoint_ops;
7033
7034 /* Implement the "insert" breakpoint_ops method for vfork
7035 catchpoints. */
7036
7037 static int
7038 insert_catch_vfork (struct bp_location *bl)
7039 {
7040 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
7041 }
7042
7043 /* Implement the "remove" breakpoint_ops method for vfork
7044 catchpoints. */
7045
7046 static int
7047 remove_catch_vfork (struct bp_location *bl)
7048 {
7049 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7050 }
7051
7052 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7053 catchpoints. */
7054
7055 static int
7056 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7057 struct address_space *aspace, CORE_ADDR bp_addr,
7058 const struct target_waitstatus *ws)
7059 {
7060 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7061
7062 if (ws->kind != TARGET_WAITKIND_VFORKED)
7063 return 0;
7064
7065 c->forked_inferior_pid = ws->value.related_pid;
7066 return 1;
7067 }
7068
7069 /* Implement the "print_it" breakpoint_ops method for vfork
7070 catchpoints. */
7071
7072 static enum print_stop_action
7073 print_it_catch_vfork (bpstat bs)
7074 {
7075 struct ui_out *uiout = current_uiout;
7076 struct breakpoint *b = bs->breakpoint_at;
7077 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7078
7079 annotate_catchpoint (b->number);
7080 if (b->disposition == disp_del)
7081 ui_out_text (uiout, "\nTemporary catchpoint ");
7082 else
7083 ui_out_text (uiout, "\nCatchpoint ");
7084 if (ui_out_is_mi_like_p (uiout))
7085 {
7086 ui_out_field_string (uiout, "reason",
7087 async_reason_lookup (EXEC_ASYNC_VFORK));
7088 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7089 }
7090 ui_out_field_int (uiout, "bkptno", b->number);
7091 ui_out_text (uiout, " (vforked process ");
7092 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7093 ui_out_text (uiout, "), ");
7094 return PRINT_SRC_AND_LOC;
7095 }
7096
7097 /* Implement the "print_one" breakpoint_ops method for vfork
7098 catchpoints. */
7099
7100 static void
7101 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7102 {
7103 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7104 struct value_print_options opts;
7105 struct ui_out *uiout = current_uiout;
7106
7107 get_user_print_options (&opts);
7108 /* Field 4, the address, is omitted (which makes the columns not
7109 line up too nicely with the headers, but the effect is relatively
7110 readable). */
7111 if (opts.addressprint)
7112 ui_out_field_skip (uiout, "addr");
7113 annotate_field (5);
7114 ui_out_text (uiout, "vfork");
7115 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7116 {
7117 ui_out_text (uiout, ", process ");
7118 ui_out_field_int (uiout, "what",
7119 ptid_get_pid (c->forked_inferior_pid));
7120 ui_out_spaces (uiout, 1);
7121 }
7122 }
7123
7124 /* Implement the "print_mention" breakpoint_ops method for vfork
7125 catchpoints. */
7126
7127 static void
7128 print_mention_catch_vfork (struct breakpoint *b)
7129 {
7130 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7131 }
7132
7133 /* Implement the "print_recreate" breakpoint_ops method for vfork
7134 catchpoints. */
7135
7136 static void
7137 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7138 {
7139 fprintf_unfiltered (fp, "catch vfork");
7140 print_recreate_thread (b, fp);
7141 }
7142
7143 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7144
7145 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7146
7147 /* An instance of this type is used to represent an solib catchpoint.
7148 It includes a "struct breakpoint" as a kind of base class; users
7149 downcast to "struct breakpoint *" when needed. A breakpoint is
7150 really of this type iff its ops pointer points to
7151 CATCH_SOLIB_BREAKPOINT_OPS. */
7152
7153 struct solib_catchpoint
7154 {
7155 /* The base class. */
7156 struct breakpoint base;
7157
7158 /* True for "catch load", false for "catch unload". */
7159 unsigned char is_load;
7160
7161 /* Regular expression to match, if any. COMPILED is only valid when
7162 REGEX is non-NULL. */
7163 char *regex;
7164 regex_t compiled;
7165 };
7166
7167 static void
7168 dtor_catch_solib (struct breakpoint *b)
7169 {
7170 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7171
7172 if (self->regex)
7173 regfree (&self->compiled);
7174 xfree (self->regex);
7175
7176 base_breakpoint_ops.dtor (b);
7177 }
7178
7179 static int
7180 insert_catch_solib (struct bp_location *ignore)
7181 {
7182 return 0;
7183 }
7184
7185 static int
7186 remove_catch_solib (struct bp_location *ignore)
7187 {
7188 return 0;
7189 }
7190
7191 static int
7192 breakpoint_hit_catch_solib (const struct bp_location *bl,
7193 struct address_space *aspace,
7194 CORE_ADDR bp_addr,
7195 const struct target_waitstatus *ws)
7196 {
7197 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7198 struct breakpoint *other;
7199
7200 if (ws->kind == TARGET_WAITKIND_LOADED)
7201 return 1;
7202
7203 ALL_BREAKPOINTS (other)
7204 {
7205 struct bp_location *other_bl;
7206
7207 if (other == bl->owner)
7208 continue;
7209
7210 if (other->type != bp_shlib_event)
7211 continue;
7212
7213 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7214 continue;
7215
7216 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7217 {
7218 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7219 return 1;
7220 }
7221 }
7222
7223 return 0;
7224 }
7225
7226 static void
7227 check_status_catch_solib (struct bpstats *bs)
7228 {
7229 struct solib_catchpoint *self
7230 = (struct solib_catchpoint *) bs->breakpoint_at;
7231 int ix;
7232
7233 if (self->is_load)
7234 {
7235 struct so_list *iter;
7236
7237 for (ix = 0;
7238 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7239 ix, iter);
7240 ++ix)
7241 {
7242 if (!self->regex
7243 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7244 return;
7245 }
7246 }
7247 else
7248 {
7249 char *iter;
7250
7251 for (ix = 0;
7252 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7253 ix, iter);
7254 ++ix)
7255 {
7256 if (!self->regex
7257 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7258 return;
7259 }
7260 }
7261
7262 bs->stop = 0;
7263 bs->print_it = print_it_noop;
7264 }
7265
7266 static enum print_stop_action
7267 print_it_catch_solib (bpstat bs)
7268 {
7269 struct breakpoint *b = bs->breakpoint_at;
7270 struct ui_out *uiout = current_uiout;
7271
7272 annotate_catchpoint (b->number);
7273 if (b->disposition == disp_del)
7274 ui_out_text (uiout, "\nTemporary catchpoint ");
7275 else
7276 ui_out_text (uiout, "\nCatchpoint ");
7277 ui_out_field_int (uiout, "bkptno", b->number);
7278 ui_out_text (uiout, "\n");
7279 if (ui_out_is_mi_like_p (uiout))
7280 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7281 print_solib_event (1);
7282 return PRINT_SRC_AND_LOC;
7283 }
7284
7285 static void
7286 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7287 {
7288 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7289 struct value_print_options opts;
7290 struct ui_out *uiout = current_uiout;
7291 char *msg;
7292
7293 get_user_print_options (&opts);
7294 /* Field 4, the address, is omitted (which makes the columns not
7295 line up too nicely with the headers, but the effect is relatively
7296 readable). */
7297 if (opts.addressprint)
7298 {
7299 annotate_field (4);
7300 ui_out_field_skip (uiout, "addr");
7301 }
7302
7303 annotate_field (5);
7304 if (self->is_load)
7305 {
7306 if (self->regex)
7307 msg = xstrprintf (_("load of library matching %s"), self->regex);
7308 else
7309 msg = xstrdup (_("load of library"));
7310 }
7311 else
7312 {
7313 if (self->regex)
7314 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7315 else
7316 msg = xstrdup (_("unload of library"));
7317 }
7318 ui_out_field_string (uiout, "what", msg);
7319 xfree (msg);
7320 }
7321
7322 static void
7323 print_mention_catch_solib (struct breakpoint *b)
7324 {
7325 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7326
7327 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7328 self->is_load ? "load" : "unload");
7329 }
7330
7331 static void
7332 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7333 {
7334 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7335
7336 fprintf_unfiltered (fp, "%s %s",
7337 b->disposition == disp_del ? "tcatch" : "catch",
7338 self->is_load ? "load" : "unload");
7339 if (self->regex)
7340 fprintf_unfiltered (fp, " %s", self->regex);
7341 fprintf_unfiltered (fp, "\n");
7342 }
7343
7344 static struct breakpoint_ops catch_solib_breakpoint_ops;
7345
7346 /* A helper function that does all the work for "catch load" and
7347 "catch unload". */
7348
7349 static void
7350 catch_load_or_unload (char *arg, int from_tty, int is_load,
7351 struct cmd_list_element *command)
7352 {
7353 struct solib_catchpoint *c;
7354 struct gdbarch *gdbarch = get_current_arch ();
7355 int tempflag;
7356 regex_t compiled;
7357 struct cleanup *cleanup;
7358
7359 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7360
7361 if (!arg)
7362 arg = "";
7363 arg = skip_spaces (arg);
7364
7365 c = XCNEW (struct solib_catchpoint);
7366 cleanup = make_cleanup (xfree, c);
7367
7368 if (*arg != '\0')
7369 {
7370 int errcode;
7371
7372 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7373 if (errcode != 0)
7374 {
7375 char *err = get_regcomp_error (errcode, &c->compiled);
7376
7377 make_cleanup (xfree, err);
7378 error (_("Invalid regexp (%s): %s"), err, arg);
7379 }
7380 c->regex = xstrdup (arg);
7381 }
7382
7383 c->is_load = is_load;
7384 init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7385 &catch_solib_breakpoint_ops);
7386
7387 discard_cleanups (cleanup);
7388 install_breakpoint (0, &c->base, 1);
7389 }
7390
7391 static void
7392 catch_load_command_1 (char *arg, int from_tty,
7393 struct cmd_list_element *command)
7394 {
7395 catch_load_or_unload (arg, from_tty, 1, command);
7396 }
7397
7398 static void
7399 catch_unload_command_1 (char *arg, int from_tty,
7400 struct cmd_list_element *command)
7401 {
7402 catch_load_or_unload (arg, from_tty, 0, command);
7403 }
7404
7405 DEF_VEC_I(int);
7406
7407 /* An instance of this type is used to represent a syscall catchpoint.
7408 It includes a "struct breakpoint" as a kind of base class; users
7409 downcast to "struct breakpoint *" when needed. A breakpoint is
7410 really of this type iff its ops pointer points to
7411 CATCH_SYSCALL_BREAKPOINT_OPS. */
7412
7413 struct syscall_catchpoint
7414 {
7415 /* The base class. */
7416 struct breakpoint base;
7417
7418 /* Syscall numbers used for the 'catch syscall' feature. If no
7419 syscall has been specified for filtering, its value is NULL.
7420 Otherwise, it holds a list of all syscalls to be caught. The
7421 list elements are allocated with xmalloc. */
7422 VEC(int) *syscalls_to_be_caught;
7423 };
7424
7425 /* Implement the "dtor" breakpoint_ops method for syscall
7426 catchpoints. */
7427
7428 static void
7429 dtor_catch_syscall (struct breakpoint *b)
7430 {
7431 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7432
7433 VEC_free (int, c->syscalls_to_be_caught);
7434
7435 base_breakpoint_ops.dtor (b);
7436 }
7437
7438 static const struct inferior_data *catch_syscall_inferior_data = NULL;
7439
7440 struct catch_syscall_inferior_data
7441 {
7442 /* We keep a count of the number of times the user has requested a
7443 particular syscall to be tracked, and pass this information to the
7444 target. This lets capable targets implement filtering directly. */
7445
7446 /* Number of times that "any" syscall is requested. */
7447 int any_syscall_count;
7448
7449 /* Count of each system call. */
7450 VEC(int) *syscalls_counts;
7451
7452 /* This counts all syscall catch requests, so we can readily determine
7453 if any catching is necessary. */
7454 int total_syscalls_count;
7455 };
7456
7457 static struct catch_syscall_inferior_data*
7458 get_catch_syscall_inferior_data (struct inferior *inf)
7459 {
7460 struct catch_syscall_inferior_data *inf_data;
7461
7462 inf_data = inferior_data (inf, catch_syscall_inferior_data);
7463 if (inf_data == NULL)
7464 {
7465 inf_data = XZALLOC (struct catch_syscall_inferior_data);
7466 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7467 }
7468
7469 return inf_data;
7470 }
7471
7472 static void
7473 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7474 {
7475 xfree (arg);
7476 }
7477
7478
7479 /* Implement the "insert" breakpoint_ops method for syscall
7480 catchpoints. */
7481
7482 static int
7483 insert_catch_syscall (struct bp_location *bl)
7484 {
7485 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
7486 struct inferior *inf = current_inferior ();
7487 struct catch_syscall_inferior_data *inf_data
7488 = get_catch_syscall_inferior_data (inf);
7489
7490 ++inf_data->total_syscalls_count;
7491 if (!c->syscalls_to_be_caught)
7492 ++inf_data->any_syscall_count;
7493 else
7494 {
7495 int i, iter;
7496
7497 for (i = 0;
7498 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7499 i++)
7500 {
7501 int elem;
7502
7503 if (iter >= VEC_length (int, inf_data->syscalls_counts))
7504 {
7505 int old_size = VEC_length (int, inf_data->syscalls_counts);
7506 uintptr_t vec_addr_offset
7507 = old_size * ((uintptr_t) sizeof (int));
7508 uintptr_t vec_addr;
7509 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7510 vec_addr = ((uintptr_t) VEC_address (int,
7511 inf_data->syscalls_counts)
7512 + vec_addr_offset);
7513 memset ((void *) vec_addr, 0,
7514 (iter + 1 - old_size) * sizeof (int));
7515 }
7516 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7517 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
7518 }
7519 }
7520
7521 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
7522 inf_data->total_syscalls_count != 0,
7523 inf_data->any_syscall_count,
7524 VEC_length (int,
7525 inf_data->syscalls_counts),
7526 VEC_address (int,
7527 inf_data->syscalls_counts));
7528 }
7529
7530 /* Implement the "remove" breakpoint_ops method for syscall
7531 catchpoints. */
7532
7533 static int
7534 remove_catch_syscall (struct bp_location *bl)
7535 {
7536 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
7537 struct inferior *inf = current_inferior ();
7538 struct catch_syscall_inferior_data *inf_data
7539 = get_catch_syscall_inferior_data (inf);
7540
7541 --inf_data->total_syscalls_count;
7542 if (!c->syscalls_to_be_caught)
7543 --inf_data->any_syscall_count;
7544 else
7545 {
7546 int i, iter;
7547
7548 for (i = 0;
7549 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7550 i++)
7551 {
7552 int elem;
7553 if (iter >= VEC_length (int, inf_data->syscalls_counts))
7554 /* Shouldn't happen. */
7555 continue;
7556 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7557 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
7558 }
7559 }
7560
7561 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
7562 inf_data->total_syscalls_count != 0,
7563 inf_data->any_syscall_count,
7564 VEC_length (int,
7565 inf_data->syscalls_counts),
7566 VEC_address (int,
7567 inf_data->syscalls_counts));
7568 }
7569
7570 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7571 catchpoints. */
7572
7573 static int
7574 breakpoint_hit_catch_syscall (const struct bp_location *bl,
7575 struct address_space *aspace, CORE_ADDR bp_addr,
7576 const struct target_waitstatus *ws)
7577 {
7578 /* We must check if we are catching specific syscalls in this
7579 breakpoint. If we are, then we must guarantee that the called
7580 syscall is the same syscall we are catching. */
7581 int syscall_number = 0;
7582 const struct syscall_catchpoint *c
7583 = (const struct syscall_catchpoint *) bl->owner;
7584
7585 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7586 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
7587 return 0;
7588
7589 syscall_number = ws->value.syscall_number;
7590
7591 /* Now, checking if the syscall is the same. */
7592 if (c->syscalls_to_be_caught)
7593 {
7594 int i, iter;
7595
7596 for (i = 0;
7597 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7598 i++)
7599 if (syscall_number == iter)
7600 break;
7601 /* Not the same. */
7602 if (!iter)
7603 return 0;
7604 }
7605
7606 return 1;
7607 }
7608
7609 /* Implement the "print_it" breakpoint_ops method for syscall
7610 catchpoints. */
7611
7612 static enum print_stop_action
7613 print_it_catch_syscall (bpstat bs)
7614 {
7615 struct ui_out *uiout = current_uiout;
7616 struct breakpoint *b = bs->breakpoint_at;
7617 /* These are needed because we want to know in which state a
7618 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7619 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7620 must print "called syscall" or "returned from syscall". */
7621 ptid_t ptid;
7622 struct target_waitstatus last;
7623 struct syscall s;
7624 char *syscall_id;
7625
7626 get_last_target_status (&ptid, &last);
7627
7628 get_syscall_by_number (last.value.syscall_number, &s);
7629
7630 annotate_catchpoint (b->number);
7631
7632 if (b->disposition == disp_del)
7633 ui_out_text (uiout, "\nTemporary catchpoint ");
7634 else
7635 ui_out_text (uiout, "\nCatchpoint ");
7636 if (ui_out_is_mi_like_p (uiout))
7637 {
7638 ui_out_field_string (uiout, "reason",
7639 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7640 ? EXEC_ASYNC_SYSCALL_ENTRY
7641 : EXEC_ASYNC_SYSCALL_RETURN));
7642 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7643 }
7644 ui_out_field_int (uiout, "bkptno", b->number);
7645
7646 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
7647 ui_out_text (uiout, " (call to syscall ");
7648 else
7649 ui_out_text (uiout, " (returned from syscall ");
7650
7651 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7652 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7653 if (s.name != NULL)
7654 ui_out_field_string (uiout, "syscall-name", s.name);
7655
7656 ui_out_text (uiout, "), ");
7657
7658 return PRINT_SRC_AND_LOC;
7659 }
7660
7661 /* Implement the "print_one" breakpoint_ops method for syscall
7662 catchpoints. */
7663
7664 static void
7665 print_one_catch_syscall (struct breakpoint *b,
7666 struct bp_location **last_loc)
7667 {
7668 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7669 struct value_print_options opts;
7670 struct ui_out *uiout = current_uiout;
7671
7672 get_user_print_options (&opts);
7673 /* Field 4, the address, is omitted (which makes the columns not
7674 line up too nicely with the headers, but the effect is relatively
7675 readable). */
7676 if (opts.addressprint)
7677 ui_out_field_skip (uiout, "addr");
7678 annotate_field (5);
7679
7680 if (c->syscalls_to_be_caught
7681 && VEC_length (int, c->syscalls_to_be_caught) > 1)
7682 ui_out_text (uiout, "syscalls \"");
7683 else
7684 ui_out_text (uiout, "syscall \"");
7685
7686 if (c->syscalls_to_be_caught)
7687 {
7688 int i, iter;
7689 char *text = xstrprintf ("%s", "");
7690
7691 for (i = 0;
7692 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7693 i++)
7694 {
7695 char *x = text;
7696 struct syscall s;
7697 get_syscall_by_number (iter, &s);
7698
7699 if (s.name != NULL)
7700 text = xstrprintf ("%s%s, ", text, s.name);
7701 else
7702 text = xstrprintf ("%s%d, ", text, iter);
7703
7704 /* We have to xfree the last 'text' (now stored at 'x')
7705 because xstrprintf dynamically allocates new space for it
7706 on every call. */
7707 xfree (x);
7708 }
7709 /* Remove the last comma. */
7710 text[strlen (text) - 2] = '\0';
7711 ui_out_field_string (uiout, "what", text);
7712 }
7713 else
7714 ui_out_field_string (uiout, "what", "<any syscall>");
7715 ui_out_text (uiout, "\" ");
7716 }
7717
7718 /* Implement the "print_mention" breakpoint_ops method for syscall
7719 catchpoints. */
7720
7721 static void
7722 print_mention_catch_syscall (struct breakpoint *b)
7723 {
7724 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7725
7726 if (c->syscalls_to_be_caught)
7727 {
7728 int i, iter;
7729
7730 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
7731 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7732 else
7733 printf_filtered (_("Catchpoint %d (syscall"), b->number);
7734
7735 for (i = 0;
7736 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7737 i++)
7738 {
7739 struct syscall s;
7740 get_syscall_by_number (iter, &s);
7741
7742 if (s.name)
7743 printf_filtered (" '%s' [%d]", s.name, s.number);
7744 else
7745 printf_filtered (" %d", s.number);
7746 }
7747 printf_filtered (")");
7748 }
7749 else
7750 printf_filtered (_("Catchpoint %d (any syscall)"),
7751 b->number);
7752 }
7753
7754 /* Implement the "print_recreate" breakpoint_ops method for syscall
7755 catchpoints. */
7756
7757 static void
7758 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
7759 {
7760 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7761
7762 fprintf_unfiltered (fp, "catch syscall");
7763
7764 if (c->syscalls_to_be_caught)
7765 {
7766 int i, iter;
7767
7768 for (i = 0;
7769 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7770 i++)
7771 {
7772 struct syscall s;
7773
7774 get_syscall_by_number (iter, &s);
7775 if (s.name)
7776 fprintf_unfiltered (fp, " %s", s.name);
7777 else
7778 fprintf_unfiltered (fp, " %d", s.number);
7779 }
7780 }
7781 print_recreate_thread (b, fp);
7782 }
7783
7784 /* The breakpoint_ops structure to be used in syscall catchpoints. */
7785
7786 static struct breakpoint_ops catch_syscall_breakpoint_ops;
7787
7788 /* Returns non-zero if 'b' is a syscall catchpoint. */
7789
7790 static int
7791 syscall_catchpoint_p (struct breakpoint *b)
7792 {
7793 return (b->ops == &catch_syscall_breakpoint_ops);
7794 }
7795
7796 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
7797 is non-zero, then make the breakpoint temporary. If COND_STRING is
7798 not NULL, then store it in the breakpoint. OPS, if not NULL, is
7799 the breakpoint_ops structure associated to the catchpoint. */
7800
7801 static void
7802 init_catchpoint (struct breakpoint *b,
7803 struct gdbarch *gdbarch, int tempflag,
7804 char *cond_string,
7805 const struct breakpoint_ops *ops)
7806 {
7807 struct symtab_and_line sal;
7808
7809 init_sal (&sal);
7810 sal.pspace = current_program_space;
7811
7812 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
7813
7814 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
7815 b->disposition = tempflag ? disp_del : disp_donttouch;
7816 }
7817
7818 void
7819 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
7820 {
7821 add_to_breakpoint_chain (b);
7822 set_breakpoint_number (internal, b);
7823 if (!internal)
7824 mention (b);
7825 observer_notify_breakpoint_created (b);
7826
7827 if (update_gll)
7828 update_global_location_list (1);
7829 }
7830
7831 static void
7832 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
7833 int tempflag, char *cond_string,
7834 const struct breakpoint_ops *ops)
7835 {
7836 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
7837
7838 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
7839
7840 c->forked_inferior_pid = null_ptid;
7841
7842 install_breakpoint (0, &c->base, 1);
7843 }
7844
7845 /* Exec catchpoints. */
7846
7847 /* An instance of this type is used to represent an exec catchpoint.
7848 It includes a "struct breakpoint" as a kind of base class; users
7849 downcast to "struct breakpoint *" when needed. A breakpoint is
7850 really of this type iff its ops pointer points to
7851 CATCH_EXEC_BREAKPOINT_OPS. */
7852
7853 struct exec_catchpoint
7854 {
7855 /* The base class. */
7856 struct breakpoint base;
7857
7858 /* Filename of a program whose exec triggered this catchpoint.
7859 This field is only valid immediately after this catchpoint has
7860 triggered. */
7861 char *exec_pathname;
7862 };
7863
7864 /* Implement the "dtor" breakpoint_ops method for exec
7865 catchpoints. */
7866
7867 static void
7868 dtor_catch_exec (struct breakpoint *b)
7869 {
7870 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
7871
7872 xfree (c->exec_pathname);
7873
7874 base_breakpoint_ops.dtor (b);
7875 }
7876
7877 static int
7878 insert_catch_exec (struct bp_location *bl)
7879 {
7880 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
7881 }
7882
7883 static int
7884 remove_catch_exec (struct bp_location *bl)
7885 {
7886 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
7887 }
7888
7889 static int
7890 breakpoint_hit_catch_exec (const struct bp_location *bl,
7891 struct address_space *aspace, CORE_ADDR bp_addr,
7892 const struct target_waitstatus *ws)
7893 {
7894 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
7895
7896 if (ws->kind != TARGET_WAITKIND_EXECD)
7897 return 0;
7898
7899 c->exec_pathname = xstrdup (ws->value.execd_pathname);
7900 return 1;
7901 }
7902
7903 static enum print_stop_action
7904 print_it_catch_exec (bpstat bs)
7905 {
7906 struct ui_out *uiout = current_uiout;
7907 struct breakpoint *b = bs->breakpoint_at;
7908 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
7909
7910 annotate_catchpoint (b->number);
7911 if (b->disposition == disp_del)
7912 ui_out_text (uiout, "\nTemporary catchpoint ");
7913 else
7914 ui_out_text (uiout, "\nCatchpoint ");
7915 if (ui_out_is_mi_like_p (uiout))
7916 {
7917 ui_out_field_string (uiout, "reason",
7918 async_reason_lookup (EXEC_ASYNC_EXEC));
7919 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7920 }
7921 ui_out_field_int (uiout, "bkptno", b->number);
7922 ui_out_text (uiout, " (exec'd ");
7923 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
7924 ui_out_text (uiout, "), ");
7925
7926 return PRINT_SRC_AND_LOC;
7927 }
7928
7929 static void
7930 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
7931 {
7932 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
7933 struct value_print_options opts;
7934 struct ui_out *uiout = current_uiout;
7935
7936 get_user_print_options (&opts);
7937
7938 /* Field 4, the address, is omitted (which makes the columns
7939 not line up too nicely with the headers, but the effect
7940 is relatively readable). */
7941 if (opts.addressprint)
7942 ui_out_field_skip (uiout, "addr");
7943 annotate_field (5);
7944 ui_out_text (uiout, "exec");
7945 if (c->exec_pathname != NULL)
7946 {
7947 ui_out_text (uiout, ", program \"");
7948 ui_out_field_string (uiout, "what", c->exec_pathname);
7949 ui_out_text (uiout, "\" ");
7950 }
7951 }
7952
7953 static void
7954 print_mention_catch_exec (struct breakpoint *b)
7955 {
7956 printf_filtered (_("Catchpoint %d (exec)"), b->number);
7957 }
7958
7959 /* Implement the "print_recreate" breakpoint_ops method for exec
7960 catchpoints. */
7961
7962 static void
7963 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
7964 {
7965 fprintf_unfiltered (fp, "catch exec");
7966 print_recreate_thread (b, fp);
7967 }
7968
7969 static struct breakpoint_ops catch_exec_breakpoint_ops;
7970
7971 static void
7972 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
7973 const struct breakpoint_ops *ops)
7974 {
7975 struct syscall_catchpoint *c;
7976 struct gdbarch *gdbarch = get_current_arch ();
7977
7978 c = XNEW (struct syscall_catchpoint);
7979 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
7980 c->syscalls_to_be_caught = filter;
7981
7982 install_breakpoint (0, &c->base, 1);
7983 }
7984
7985 static int
7986 hw_breakpoint_used_count (void)
7987 {
7988 int i = 0;
7989 struct breakpoint *b;
7990 struct bp_location *bl;
7991
7992 ALL_BREAKPOINTS (b)
7993 {
7994 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7995 for (bl = b->loc; bl; bl = bl->next)
7996 {
7997 /* Special types of hardware breakpoints may use more than
7998 one register. */
7999 i += b->ops->resources_needed (bl);
8000 }
8001 }
8002
8003 return i;
8004 }
8005
8006 /* Returns the resources B would use if it were a hardware
8007 watchpoint. */
8008
8009 static int
8010 hw_watchpoint_use_count (struct breakpoint *b)
8011 {
8012 int i = 0;
8013 struct bp_location *bl;
8014
8015 if (!breakpoint_enabled (b))
8016 return 0;
8017
8018 for (bl = b->loc; bl; bl = bl->next)
8019 {
8020 /* Special types of hardware watchpoints may use more than
8021 one register. */
8022 i += b->ops->resources_needed (bl);
8023 }
8024
8025 return i;
8026 }
8027
8028 /* Returns the sum the used resources of all hardware watchpoints of
8029 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8030 the sum of the used resources of all hardware watchpoints of other
8031 types _not_ TYPE. */
8032
8033 static int
8034 hw_watchpoint_used_count_others (struct breakpoint *except,
8035 enum bptype type, int *other_type_used)
8036 {
8037 int i = 0;
8038 struct breakpoint *b;
8039
8040 *other_type_used = 0;
8041 ALL_BREAKPOINTS (b)
8042 {
8043 if (b == except)
8044 continue;
8045 if (!breakpoint_enabled (b))
8046 continue;
8047
8048 if (b->type == type)
8049 i += hw_watchpoint_use_count (b);
8050 else if (is_hardware_watchpoint (b))
8051 *other_type_used = 1;
8052 }
8053
8054 return i;
8055 }
8056
8057 void
8058 disable_watchpoints_before_interactive_call_start (void)
8059 {
8060 struct breakpoint *b;
8061
8062 ALL_BREAKPOINTS (b)
8063 {
8064 if (is_watchpoint (b) && breakpoint_enabled (b))
8065 {
8066 b->enable_state = bp_call_disabled;
8067 update_global_location_list (0);
8068 }
8069 }
8070 }
8071
8072 void
8073 enable_watchpoints_after_interactive_call_stop (void)
8074 {
8075 struct breakpoint *b;
8076
8077 ALL_BREAKPOINTS (b)
8078 {
8079 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8080 {
8081 b->enable_state = bp_enabled;
8082 update_global_location_list (1);
8083 }
8084 }
8085 }
8086
8087 void
8088 disable_breakpoints_before_startup (void)
8089 {
8090 current_program_space->executing_startup = 1;
8091 update_global_location_list (0);
8092 }
8093
8094 void
8095 enable_breakpoints_after_startup (void)
8096 {
8097 current_program_space->executing_startup = 0;
8098 breakpoint_re_set ();
8099 }
8100
8101
8102 /* Set a breakpoint that will evaporate an end of command
8103 at address specified by SAL.
8104 Restrict it to frame FRAME if FRAME is nonzero. */
8105
8106 struct breakpoint *
8107 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8108 struct frame_id frame_id, enum bptype type)
8109 {
8110 struct breakpoint *b;
8111
8112 /* If FRAME_ID is valid, it should be a real frame, not an inlined
8113 one. */
8114 gdb_assert (!frame_id_inlined_p (frame_id));
8115
8116 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8117 b->enable_state = bp_enabled;
8118 b->disposition = disp_donttouch;
8119 b->frame_id = frame_id;
8120
8121 /* If we're debugging a multi-threaded program, then we want
8122 momentary breakpoints to be active in only a single thread of
8123 control. */
8124 if (in_thread_list (inferior_ptid))
8125 b->thread = pid_to_thread_id (inferior_ptid);
8126
8127 update_global_location_list_nothrow (1);
8128
8129 return b;
8130 }
8131
8132 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8133 The new breakpoint will have type TYPE, and use OPS as it
8134 breakpoint_ops. */
8135
8136 static struct breakpoint *
8137 momentary_breakpoint_from_master (struct breakpoint *orig,
8138 enum bptype type,
8139 const struct breakpoint_ops *ops)
8140 {
8141 struct breakpoint *copy;
8142
8143 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8144 copy->loc = allocate_bp_location (copy);
8145 set_breakpoint_location_function (copy->loc, 1);
8146
8147 copy->loc->gdbarch = orig->loc->gdbarch;
8148 copy->loc->requested_address = orig->loc->requested_address;
8149 copy->loc->address = orig->loc->address;
8150 copy->loc->section = orig->loc->section;
8151 copy->loc->pspace = orig->loc->pspace;
8152
8153 if (orig->loc->source_file != NULL)
8154 copy->loc->source_file = xstrdup (orig->loc->source_file);
8155
8156 copy->loc->line_number = orig->loc->line_number;
8157 copy->frame_id = orig->frame_id;
8158 copy->thread = orig->thread;
8159 copy->pspace = orig->pspace;
8160
8161 copy->enable_state = bp_enabled;
8162 copy->disposition = disp_donttouch;
8163 copy->number = internal_breakpoint_number--;
8164
8165 update_global_location_list_nothrow (0);
8166 return copy;
8167 }
8168
8169 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8170 ORIG is NULL. */
8171
8172 struct breakpoint *
8173 clone_momentary_breakpoint (struct breakpoint *orig)
8174 {
8175 /* If there's nothing to clone, then return nothing. */
8176 if (orig == NULL)
8177 return NULL;
8178
8179 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8180 }
8181
8182 struct breakpoint *
8183 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8184 enum bptype type)
8185 {
8186 struct symtab_and_line sal;
8187
8188 sal = find_pc_line (pc, 0);
8189 sal.pc = pc;
8190 sal.section = find_pc_overlay (pc);
8191 sal.explicit_pc = 1;
8192
8193 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8194 }
8195 \f
8196
8197 /* Tell the user we have just set a breakpoint B. */
8198
8199 static void
8200 mention (struct breakpoint *b)
8201 {
8202 b->ops->print_mention (b);
8203 if (ui_out_is_mi_like_p (current_uiout))
8204 return;
8205 printf_filtered ("\n");
8206 }
8207 \f
8208
8209 static struct bp_location *
8210 add_location_to_breakpoint (struct breakpoint *b,
8211 const struct symtab_and_line *sal)
8212 {
8213 struct bp_location *loc, **tmp;
8214 CORE_ADDR adjusted_address;
8215 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8216
8217 if (loc_gdbarch == NULL)
8218 loc_gdbarch = b->gdbarch;
8219
8220 /* Adjust the breakpoint's address prior to allocating a location.
8221 Once we call allocate_bp_location(), that mostly uninitialized
8222 location will be placed on the location chain. Adjustment of the
8223 breakpoint may cause target_read_memory() to be called and we do
8224 not want its scan of the location chain to find a breakpoint and
8225 location that's only been partially initialized. */
8226 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8227 sal->pc, b->type);
8228
8229 loc = allocate_bp_location (b);
8230 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8231 ;
8232 *tmp = loc;
8233
8234 loc->requested_address = sal->pc;
8235 loc->address = adjusted_address;
8236 loc->pspace = sal->pspace;
8237 gdb_assert (loc->pspace != NULL);
8238 loc->section = sal->section;
8239 loc->gdbarch = loc_gdbarch;
8240
8241 if (sal->symtab != NULL)
8242 loc->source_file = xstrdup (sal->symtab->filename);
8243 loc->line_number = sal->line;
8244
8245 set_breakpoint_location_function (loc,
8246 sal->explicit_pc || sal->explicit_line);
8247 return loc;
8248 }
8249 \f
8250
8251 /* Return 1 if LOC is pointing to a permanent breakpoint,
8252 return 0 otherwise. */
8253
8254 static int
8255 bp_loc_is_permanent (struct bp_location *loc)
8256 {
8257 int len;
8258 CORE_ADDR addr;
8259 const gdb_byte *bpoint;
8260 gdb_byte *target_mem;
8261 struct cleanup *cleanup;
8262 int retval = 0;
8263
8264 gdb_assert (loc != NULL);
8265
8266 addr = loc->address;
8267 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
8268
8269 /* Software breakpoints unsupported? */
8270 if (bpoint == NULL)
8271 return 0;
8272
8273 target_mem = alloca (len);
8274
8275 /* Enable the automatic memory restoration from breakpoints while
8276 we read the memory. Otherwise we could say about our temporary
8277 breakpoints they are permanent. */
8278 cleanup = save_current_space_and_thread ();
8279
8280 switch_to_program_space_and_thread (loc->pspace);
8281 make_show_memory_breakpoints_cleanup (0);
8282
8283 if (target_read_memory (loc->address, target_mem, len) == 0
8284 && memcmp (target_mem, bpoint, len) == 0)
8285 retval = 1;
8286
8287 do_cleanups (cleanup);
8288
8289 return retval;
8290 }
8291
8292
8293
8294 /* Create a breakpoint with SAL as location. Use ADDR_STRING
8295 as textual description of the location, and COND_STRING
8296 as condition expression. */
8297
8298 static void
8299 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8300 struct symtabs_and_lines sals, char *addr_string,
8301 char *filter, char *cond_string,
8302 enum bptype type, enum bpdisp disposition,
8303 int thread, int task, int ignore_count,
8304 const struct breakpoint_ops *ops, int from_tty,
8305 int enabled, int internal, unsigned flags,
8306 int display_canonical)
8307 {
8308 int i;
8309
8310 if (type == bp_hardware_breakpoint)
8311 {
8312 int target_resources_ok;
8313
8314 i = hw_breakpoint_used_count ();
8315 target_resources_ok =
8316 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8317 i + 1, 0);
8318 if (target_resources_ok == 0)
8319 error (_("No hardware breakpoint support in the target."));
8320 else if (target_resources_ok < 0)
8321 error (_("Hardware breakpoints used exceeds limit."));
8322 }
8323
8324 gdb_assert (sals.nelts > 0);
8325
8326 for (i = 0; i < sals.nelts; ++i)
8327 {
8328 struct symtab_and_line sal = sals.sals[i];
8329 struct bp_location *loc;
8330
8331 if (from_tty)
8332 {
8333 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8334 if (!loc_gdbarch)
8335 loc_gdbarch = gdbarch;
8336
8337 describe_other_breakpoints (loc_gdbarch,
8338 sal.pspace, sal.pc, sal.section, thread);
8339 }
8340
8341 if (i == 0)
8342 {
8343 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8344 b->thread = thread;
8345 b->task = task;
8346
8347 b->cond_string = cond_string;
8348 b->ignore_count = ignore_count;
8349 b->enable_state = enabled ? bp_enabled : bp_disabled;
8350 b->disposition = disposition;
8351
8352 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8353 b->loc->inserted = 1;
8354
8355 if (type == bp_static_tracepoint)
8356 {
8357 struct tracepoint *t = (struct tracepoint *) b;
8358 struct static_tracepoint_marker marker;
8359
8360 if (strace_marker_p (b))
8361 {
8362 /* We already know the marker exists, otherwise, we
8363 wouldn't see a sal for it. */
8364 char *p = &addr_string[3];
8365 char *endp;
8366 char *marker_str;
8367
8368 p = skip_spaces (p);
8369
8370 endp = skip_to_space (p);
8371
8372 marker_str = savestring (p, endp - p);
8373 t->static_trace_marker_id = marker_str;
8374
8375 printf_filtered (_("Probed static tracepoint "
8376 "marker \"%s\"\n"),
8377 t->static_trace_marker_id);
8378 }
8379 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8380 {
8381 t->static_trace_marker_id = xstrdup (marker.str_id);
8382 release_static_tracepoint_marker (&marker);
8383
8384 printf_filtered (_("Probed static tracepoint "
8385 "marker \"%s\"\n"),
8386 t->static_trace_marker_id);
8387 }
8388 else
8389 warning (_("Couldn't determine the static "
8390 "tracepoint marker to probe"));
8391 }
8392
8393 loc = b->loc;
8394 }
8395 else
8396 {
8397 loc = add_location_to_breakpoint (b, &sal);
8398 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8399 loc->inserted = 1;
8400 }
8401
8402 if (bp_loc_is_permanent (loc))
8403 make_breakpoint_permanent (b);
8404
8405 if (b->cond_string)
8406 {
8407 char *arg = b->cond_string;
8408 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
8409 if (*arg)
8410 error (_("Garbage %s follows condition"), arg);
8411 }
8412 }
8413
8414 b->display_canonical = display_canonical;
8415 if (addr_string)
8416 b->addr_string = addr_string;
8417 else
8418 /* addr_string has to be used or breakpoint_re_set will delete
8419 me. */
8420 b->addr_string
8421 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
8422 b->filter = filter;
8423 }
8424
8425 static void
8426 create_breakpoint_sal (struct gdbarch *gdbarch,
8427 struct symtabs_and_lines sals, char *addr_string,
8428 char *filter, char *cond_string,
8429 enum bptype type, enum bpdisp disposition,
8430 int thread, int task, int ignore_count,
8431 const struct breakpoint_ops *ops, int from_tty,
8432 int enabled, int internal, unsigned flags,
8433 int display_canonical)
8434 {
8435 struct breakpoint *b;
8436 struct cleanup *old_chain;
8437
8438 if (is_tracepoint_type (type))
8439 {
8440 struct tracepoint *t;
8441
8442 t = XCNEW (struct tracepoint);
8443 b = &t->base;
8444 }
8445 else
8446 b = XNEW (struct breakpoint);
8447
8448 old_chain = make_cleanup (xfree, b);
8449
8450 init_breakpoint_sal (b, gdbarch,
8451 sals, addr_string,
8452 filter, cond_string,
8453 type, disposition,
8454 thread, task, ignore_count,
8455 ops, from_tty,
8456 enabled, internal, flags,
8457 display_canonical);
8458 discard_cleanups (old_chain);
8459
8460 install_breakpoint (internal, b, 0);
8461 }
8462
8463 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8464 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8465 value. COND_STRING, if not NULL, specified the condition to be
8466 used for all breakpoints. Essentially the only case where
8467 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8468 function. In that case, it's still not possible to specify
8469 separate conditions for different overloaded functions, so
8470 we take just a single condition string.
8471
8472 NOTE: If the function succeeds, the caller is expected to cleanup
8473 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8474 array contents). If the function fails (error() is called), the
8475 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8476 COND and SALS arrays and each of those arrays contents. */
8477
8478 static void
8479 create_breakpoints_sal (struct gdbarch *gdbarch,
8480 struct linespec_result *canonical,
8481 char *cond_string,
8482 enum bptype type, enum bpdisp disposition,
8483 int thread, int task, int ignore_count,
8484 const struct breakpoint_ops *ops, int from_tty,
8485 int enabled, int internal, unsigned flags)
8486 {
8487 int i;
8488 struct linespec_sals *lsal;
8489
8490 if (canonical->pre_expanded)
8491 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8492
8493 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
8494 {
8495 /* Note that 'addr_string' can be NULL in the case of a plain
8496 'break', without arguments. */
8497 char *addr_string = (canonical->addr_string
8498 ? xstrdup (canonical->addr_string)
8499 : NULL);
8500 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8501 struct cleanup *inner = make_cleanup (xfree, addr_string);
8502
8503 make_cleanup (xfree, filter_string);
8504 create_breakpoint_sal (gdbarch, lsal->sals,
8505 addr_string,
8506 filter_string,
8507 cond_string, type, disposition,
8508 thread, task, ignore_count, ops,
8509 from_tty, enabled, internal, flags,
8510 canonical->special_display);
8511 discard_cleanups (inner);
8512 }
8513 }
8514
8515 /* Parse ADDRESS which is assumed to be a SAL specification possibly
8516 followed by conditionals. On return, SALS contains an array of SAL
8517 addresses found. ADDR_STRING contains a vector of (canonical)
8518 address strings. ADDRESS points to the end of the SAL.
8519
8520 The array and the line spec strings are allocated on the heap, it is
8521 the caller's responsibility to free them. */
8522
8523 static void
8524 parse_breakpoint_sals (char **address,
8525 struct linespec_result *canonical)
8526 {
8527 char *addr_start = *address;
8528
8529 /* If no arg given, or if first arg is 'if ', use the default
8530 breakpoint. */
8531 if ((*address) == NULL
8532 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
8533 {
8534 /* The last displayed codepoint, if it's valid, is our default breakpoint
8535 address. */
8536 if (last_displayed_sal_is_valid ())
8537 {
8538 struct linespec_sals lsal;
8539 struct symtab_and_line sal;
8540
8541 init_sal (&sal); /* Initialize to zeroes. */
8542 lsal.sals.sals = (struct symtab_and_line *)
8543 xmalloc (sizeof (struct symtab_and_line));
8544
8545 /* Set sal's pspace, pc, symtab, and line to the values
8546 corresponding to the last call to print_frame_info. */
8547 get_last_displayed_sal (&sal);
8548 sal.section = find_pc_overlay (sal.pc);
8549
8550 /* "break" without arguments is equivalent to "break *PC"
8551 where PC is the last displayed codepoint's address. So
8552 make sure to set sal.explicit_pc to prevent GDB from
8553 trying to expand the list of sals to include all other
8554 instances with the same symtab and line. */
8555 sal.explicit_pc = 1;
8556
8557 lsal.sals.sals[0] = sal;
8558 lsal.sals.nelts = 1;
8559 lsal.canonical = NULL;
8560
8561 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
8562 }
8563 else
8564 error (_("No default breakpoint address now."));
8565 }
8566 else
8567 {
8568 /* Force almost all breakpoints to be in terms of the
8569 current_source_symtab (which is decode_line_1's default).
8570 This should produce the results we want almost all of the
8571 time while leaving default_breakpoint_* alone. */
8572 if (last_displayed_sal_is_valid ())
8573 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8574 get_last_displayed_symtab (),
8575 get_last_displayed_line (),
8576 canonical, NULL, NULL);
8577 else
8578 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8579 (struct symtab *) NULL, 0,
8580 canonical, NULL, NULL);
8581 }
8582 }
8583
8584
8585 /* Convert each SAL into a real PC. Verify that the PC can be
8586 inserted as a breakpoint. If it can't throw an error. */
8587
8588 static void
8589 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
8590 {
8591 int i;
8592
8593 for (i = 0; i < sals->nelts; i++)
8594 resolve_sal_pc (&sals->sals[i]);
8595 }
8596
8597 /* Fast tracepoints may have restrictions on valid locations. For
8598 instance, a fast tracepoint using a jump instead of a trap will
8599 likely have to overwrite more bytes than a trap would, and so can
8600 only be placed where the instruction is longer than the jump, or a
8601 multi-instruction sequence does not have a jump into the middle of
8602 it, etc. */
8603
8604 static void
8605 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8606 struct symtabs_and_lines *sals)
8607 {
8608 int i, rslt;
8609 struct symtab_and_line *sal;
8610 char *msg;
8611 struct cleanup *old_chain;
8612
8613 for (i = 0; i < sals->nelts; i++)
8614 {
8615 struct gdbarch *sarch;
8616
8617 sal = &sals->sals[i];
8618
8619 sarch = get_sal_arch (*sal);
8620 /* We fall back to GDBARCH if there is no architecture
8621 associated with SAL. */
8622 if (sarch == NULL)
8623 sarch = gdbarch;
8624 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
8625 NULL, &msg);
8626 old_chain = make_cleanup (xfree, msg);
8627
8628 if (!rslt)
8629 error (_("May not have a fast tracepoint at 0x%s%s"),
8630 paddress (sarch, sal->pc), (msg ? msg : ""));
8631
8632 do_cleanups (old_chain);
8633 }
8634 }
8635
8636 /* Given TOK, a string specification of condition and thread, as
8637 accepted by the 'break' command, extract the condition
8638 string and thread number and set *COND_STRING and *THREAD.
8639 PC identifies the context at which the condition should be parsed.
8640 If no condition is found, *COND_STRING is set to NULL.
8641 If no thread is found, *THREAD is set to -1. */
8642 static void
8643 find_condition_and_thread (char *tok, CORE_ADDR pc,
8644 char **cond_string, int *thread, int *task)
8645 {
8646 *cond_string = NULL;
8647 *thread = -1;
8648 while (tok && *tok)
8649 {
8650 char *end_tok;
8651 int toklen;
8652 char *cond_start = NULL;
8653 char *cond_end = NULL;
8654
8655 tok = skip_spaces (tok);
8656
8657 end_tok = skip_to_space (tok);
8658
8659 toklen = end_tok - tok;
8660
8661 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8662 {
8663 struct expression *expr;
8664
8665 tok = cond_start = end_tok + 1;
8666 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
8667 xfree (expr);
8668 cond_end = tok;
8669 *cond_string = savestring (cond_start,
8670 cond_end - cond_start);
8671 }
8672 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8673 {
8674 char *tmptok;
8675
8676 tok = end_tok + 1;
8677 tmptok = tok;
8678 *thread = strtol (tok, &tok, 0);
8679 if (tok == tmptok)
8680 error (_("Junk after thread keyword."));
8681 if (!valid_thread_id (*thread))
8682 error (_("Unknown thread %d."), *thread);
8683 }
8684 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8685 {
8686 char *tmptok;
8687
8688 tok = end_tok + 1;
8689 tmptok = tok;
8690 *task = strtol (tok, &tok, 0);
8691 if (tok == tmptok)
8692 error (_("Junk after task keyword."));
8693 if (!valid_task_id (*task))
8694 error (_("Unknown task %d."), *task);
8695 }
8696 else
8697 error (_("Junk at end of arguments."));
8698 }
8699 }
8700
8701 /* Decode a static tracepoint marker spec. */
8702
8703 static struct symtabs_and_lines
8704 decode_static_tracepoint_spec (char **arg_p)
8705 {
8706 VEC(static_tracepoint_marker_p) *markers = NULL;
8707 struct symtabs_and_lines sals;
8708 struct symtab_and_line sal;
8709 struct symbol *sym;
8710 struct cleanup *old_chain;
8711 char *p = &(*arg_p)[3];
8712 char *endp;
8713 char *marker_str;
8714 int i;
8715
8716 p = skip_spaces (p);
8717
8718 endp = skip_to_space (p);
8719
8720 marker_str = savestring (p, endp - p);
8721 old_chain = make_cleanup (xfree, marker_str);
8722
8723 markers = target_static_tracepoint_markers_by_strid (marker_str);
8724 if (VEC_empty(static_tracepoint_marker_p, markers))
8725 error (_("No known static tracepoint marker named %s"), marker_str);
8726
8727 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8728 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
8729
8730 for (i = 0; i < sals.nelts; i++)
8731 {
8732 struct static_tracepoint_marker *marker;
8733
8734 marker = VEC_index (static_tracepoint_marker_p, markers, i);
8735
8736 init_sal (&sals.sals[i]);
8737
8738 sals.sals[i] = find_pc_line (marker->address, 0);
8739 sals.sals[i].pc = marker->address;
8740
8741 release_static_tracepoint_marker (marker);
8742 }
8743
8744 do_cleanups (old_chain);
8745
8746 *arg_p = endp;
8747 return sals;
8748 }
8749
8750 /* Set a breakpoint. This function is shared between CLI and MI
8751 functions for setting a breakpoint. This function has two major
8752 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
8753 parameter. If non-zero, the function will parse arg, extracting
8754 breakpoint location, address and thread. Otherwise, ARG is just
8755 the location of breakpoint, with condition and thread specified by
8756 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
8757 the breakpoint number will be allocated from the internal
8758 breakpoint count. Returns true if any breakpoint was created;
8759 false otherwise. */
8760
8761 int
8762 create_breakpoint (struct gdbarch *gdbarch,
8763 char *arg, char *cond_string, int thread,
8764 int parse_condition_and_thread,
8765 int tempflag, enum bptype type_wanted,
8766 int ignore_count,
8767 enum auto_boolean pending_break_support,
8768 const struct breakpoint_ops *ops,
8769 int from_tty, int enabled, int internal,
8770 unsigned flags)
8771 {
8772 volatile struct gdb_exception e;
8773 char *copy_arg = NULL;
8774 char *addr_start = arg;
8775 struct linespec_result canonical;
8776 struct cleanup *old_chain;
8777 struct cleanup *bkpt_chain = NULL;
8778 int i;
8779 int pending = 0;
8780 int task = 0;
8781 int prev_bkpt_count = breakpoint_count;
8782
8783 gdb_assert (ops != NULL);
8784
8785 init_linespec_result (&canonical);
8786
8787 TRY_CATCH (e, RETURN_MASK_ALL)
8788 {
8789 ops->create_sals_from_address (&arg, &canonical, type_wanted,
8790 addr_start, &copy_arg);
8791 }
8792
8793 /* If caller is interested in rc value from parse, set value. */
8794 switch (e.reason)
8795 {
8796 case GDB_NO_ERROR:
8797 if (VEC_empty (linespec_sals, canonical.sals))
8798 return 0;
8799 break;
8800 case RETURN_ERROR:
8801 switch (e.error)
8802 {
8803 case NOT_FOUND_ERROR:
8804
8805 /* If pending breakpoint support is turned off, throw
8806 error. */
8807
8808 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8809 throw_exception (e);
8810
8811 exception_print (gdb_stderr, e);
8812
8813 /* If pending breakpoint support is auto query and the user
8814 selects no, then simply return the error code. */
8815 if (pending_break_support == AUTO_BOOLEAN_AUTO
8816 && !nquery (_("Make %s pending on future shared library load? "),
8817 bptype_string (type_wanted)))
8818 return 0;
8819
8820 /* At this point, either the user was queried about setting
8821 a pending breakpoint and selected yes, or pending
8822 breakpoint behavior is on and thus a pending breakpoint
8823 is defaulted on behalf of the user. */
8824 {
8825 struct linespec_sals lsal;
8826
8827 copy_arg = xstrdup (addr_start);
8828 lsal.canonical = xstrdup (copy_arg);
8829 lsal.sals.nelts = 1;
8830 lsal.sals.sals = XNEW (struct symtab_and_line);
8831 init_sal (&lsal.sals.sals[0]);
8832 pending = 1;
8833 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
8834 }
8835 break;
8836 default:
8837 throw_exception (e);
8838 }
8839 break;
8840 default:
8841 throw_exception (e);
8842 }
8843
8844 /* Create a chain of things that always need to be cleaned up. */
8845 old_chain = make_cleanup_destroy_linespec_result (&canonical);
8846
8847 /* ----------------------------- SNIP -----------------------------
8848 Anything added to the cleanup chain beyond this point is assumed
8849 to be part of a breakpoint. If the breakpoint create succeeds
8850 then the memory is not reclaimed. */
8851 bkpt_chain = make_cleanup (null_cleanup, 0);
8852
8853 /* Resolve all line numbers to PC's and verify that the addresses
8854 are ok for the target. */
8855 if (!pending)
8856 {
8857 int ix;
8858 struct linespec_sals *iter;
8859
8860 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
8861 breakpoint_sals_to_pc (&iter->sals);
8862 }
8863
8864 /* Fast tracepoints may have additional restrictions on location. */
8865 if (!pending && type_wanted == bp_fast_tracepoint)
8866 {
8867 int ix;
8868 struct linespec_sals *iter;
8869
8870 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
8871 check_fast_tracepoint_sals (gdbarch, &iter->sals);
8872 }
8873
8874 /* Verify that condition can be parsed, before setting any
8875 breakpoints. Allocate a separate condition expression for each
8876 breakpoint. */
8877 if (!pending)
8878 {
8879 struct linespec_sals *lsal;
8880
8881 lsal = VEC_index (linespec_sals, canonical.sals, 0);
8882
8883 if (parse_condition_and_thread)
8884 {
8885 /* Here we only parse 'arg' to separate condition
8886 from thread number, so parsing in context of first
8887 sal is OK. When setting the breakpoint we'll
8888 re-parse it in context of each sal. */
8889 cond_string = NULL;
8890 thread = -1;
8891 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
8892 &thread, &task);
8893 if (cond_string)
8894 make_cleanup (xfree, cond_string);
8895 }
8896 else
8897 {
8898 /* Create a private copy of condition string. */
8899 if (cond_string)
8900 {
8901 cond_string = xstrdup (cond_string);
8902 make_cleanup (xfree, cond_string);
8903 }
8904 }
8905
8906 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
8907 cond_string, type_wanted,
8908 tempflag ? disp_del : disp_donttouch,
8909 thread, task, ignore_count, ops,
8910 from_tty, enabled, internal, flags);
8911 }
8912 else
8913 {
8914 struct breakpoint *b;
8915
8916 make_cleanup (xfree, copy_arg);
8917
8918 if (is_tracepoint_type (type_wanted))
8919 {
8920 struct tracepoint *t;
8921
8922 t = XCNEW (struct tracepoint);
8923 b = &t->base;
8924 }
8925 else
8926 b = XNEW (struct breakpoint);
8927
8928 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
8929
8930 b->addr_string = copy_arg;
8931 b->cond_string = NULL;
8932 b->ignore_count = ignore_count;
8933 b->disposition = tempflag ? disp_del : disp_donttouch;
8934 b->condition_not_parsed = 1;
8935 b->enable_state = enabled ? bp_enabled : bp_disabled;
8936 if ((type_wanted != bp_breakpoint
8937 && type_wanted != bp_hardware_breakpoint) || thread != -1)
8938 b->pspace = current_program_space;
8939
8940 install_breakpoint (internal, b, 0);
8941 }
8942
8943 if (VEC_length (linespec_sals, canonical.sals) > 1)
8944 {
8945 warning (_("Multiple breakpoints were set.\nUse the "
8946 "\"delete\" command to delete unwanted breakpoints."));
8947 prev_breakpoint_count = prev_bkpt_count;
8948 }
8949
8950 /* That's it. Discard the cleanups for data inserted into the
8951 breakpoint. */
8952 discard_cleanups (bkpt_chain);
8953 /* But cleanup everything else. */
8954 do_cleanups (old_chain);
8955
8956 /* error call may happen here - have BKPT_CHAIN already discarded. */
8957 update_global_location_list (1);
8958
8959 return 1;
8960 }
8961
8962 /* Set a breakpoint.
8963 ARG is a string describing breakpoint address,
8964 condition, and thread.
8965 FLAG specifies if a breakpoint is hardware on,
8966 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8967 and BP_TEMPFLAG. */
8968
8969 static void
8970 break_command_1 (char *arg, int flag, int from_tty)
8971 {
8972 int tempflag = flag & BP_TEMPFLAG;
8973 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8974 ? bp_hardware_breakpoint
8975 : bp_breakpoint);
8976
8977 create_breakpoint (get_current_arch (),
8978 arg,
8979 NULL, 0, 1 /* parse arg */,
8980 tempflag, type_wanted,
8981 0 /* Ignore count */,
8982 pending_break_support,
8983 &bkpt_breakpoint_ops,
8984 from_tty,
8985 1 /* enabled */,
8986 0 /* internal */,
8987 0);
8988 }
8989
8990 /* Helper function for break_command_1 and disassemble_command. */
8991
8992 void
8993 resolve_sal_pc (struct symtab_and_line *sal)
8994 {
8995 CORE_ADDR pc;
8996
8997 if (sal->pc == 0 && sal->symtab != NULL)
8998 {
8999 if (!find_line_pc (sal->symtab, sal->line, &pc))
9000 error (_("No line %d in file \"%s\"."),
9001 sal->line, sal->symtab->filename);
9002 sal->pc = pc;
9003
9004 /* If this SAL corresponds to a breakpoint inserted using a line
9005 number, then skip the function prologue if necessary. */
9006 if (sal->explicit_line)
9007 skip_prologue_sal (sal);
9008 }
9009
9010 if (sal->section == 0 && sal->symtab != NULL)
9011 {
9012 struct blockvector *bv;
9013 struct block *b;
9014 struct symbol *sym;
9015
9016 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9017 if (bv != NULL)
9018 {
9019 sym = block_linkage_function (b);
9020 if (sym != NULL)
9021 {
9022 fixup_symbol_section (sym, sal->symtab->objfile);
9023 sal->section = SYMBOL_OBJ_SECTION (sym);
9024 }
9025 else
9026 {
9027 /* It really is worthwhile to have the section, so we'll
9028 just have to look harder. This case can be executed
9029 if we have line numbers but no functions (as can
9030 happen in assembly source). */
9031
9032 struct minimal_symbol *msym;
9033 struct cleanup *old_chain = save_current_space_and_thread ();
9034
9035 switch_to_program_space_and_thread (sal->pspace);
9036
9037 msym = lookup_minimal_symbol_by_pc (sal->pc);
9038 if (msym)
9039 sal->section = SYMBOL_OBJ_SECTION (msym);
9040
9041 do_cleanups (old_chain);
9042 }
9043 }
9044 }
9045 }
9046
9047 void
9048 break_command (char *arg, int from_tty)
9049 {
9050 break_command_1 (arg, 0, from_tty);
9051 }
9052
9053 void
9054 tbreak_command (char *arg, int from_tty)
9055 {
9056 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9057 }
9058
9059 static void
9060 hbreak_command (char *arg, int from_tty)
9061 {
9062 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9063 }
9064
9065 static void
9066 thbreak_command (char *arg, int from_tty)
9067 {
9068 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9069 }
9070
9071 static void
9072 stop_command (char *arg, int from_tty)
9073 {
9074 printf_filtered (_("Specify the type of breakpoint to set.\n\
9075 Usage: stop in <function | address>\n\
9076 stop at <line>\n"));
9077 }
9078
9079 static void
9080 stopin_command (char *arg, int from_tty)
9081 {
9082 int badInput = 0;
9083
9084 if (arg == (char *) NULL)
9085 badInput = 1;
9086 else if (*arg != '*')
9087 {
9088 char *argptr = arg;
9089 int hasColon = 0;
9090
9091 /* Look for a ':'. If this is a line number specification, then
9092 say it is bad, otherwise, it should be an address or
9093 function/method name. */
9094 while (*argptr && !hasColon)
9095 {
9096 hasColon = (*argptr == ':');
9097 argptr++;
9098 }
9099
9100 if (hasColon)
9101 badInput = (*argptr != ':'); /* Not a class::method */
9102 else
9103 badInput = isdigit (*arg); /* a simple line number */
9104 }
9105
9106 if (badInput)
9107 printf_filtered (_("Usage: stop in <function | address>\n"));
9108 else
9109 break_command_1 (arg, 0, from_tty);
9110 }
9111
9112 static void
9113 stopat_command (char *arg, int from_tty)
9114 {
9115 int badInput = 0;
9116
9117 if (arg == (char *) NULL || *arg == '*') /* no line number */
9118 badInput = 1;
9119 else
9120 {
9121 char *argptr = arg;
9122 int hasColon = 0;
9123
9124 /* Look for a ':'. If there is a '::' then get out, otherwise
9125 it is probably a line number. */
9126 while (*argptr && !hasColon)
9127 {
9128 hasColon = (*argptr == ':');
9129 argptr++;
9130 }
9131
9132 if (hasColon)
9133 badInput = (*argptr == ':'); /* we have class::method */
9134 else
9135 badInput = !isdigit (*arg); /* not a line number */
9136 }
9137
9138 if (badInput)
9139 printf_filtered (_("Usage: stop at <line>\n"));
9140 else
9141 break_command_1 (arg, 0, from_tty);
9142 }
9143
9144 /* Implement the "breakpoint_hit" breakpoint_ops method for
9145 ranged breakpoints. */
9146
9147 static int
9148 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9149 struct address_space *aspace,
9150 CORE_ADDR bp_addr,
9151 const struct target_waitstatus *ws)
9152 {
9153 if (ws->kind != TARGET_WAITKIND_STOPPED
9154 || ws->value.sig != TARGET_SIGNAL_TRAP)
9155 return 0;
9156
9157 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9158 bl->length, aspace, bp_addr);
9159 }
9160
9161 /* Implement the "resources_needed" breakpoint_ops method for
9162 ranged breakpoints. */
9163
9164 static int
9165 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9166 {
9167 return target_ranged_break_num_registers ();
9168 }
9169
9170 /* Implement the "print_it" breakpoint_ops method for
9171 ranged breakpoints. */
9172
9173 static enum print_stop_action
9174 print_it_ranged_breakpoint (bpstat bs)
9175 {
9176 struct breakpoint *b = bs->breakpoint_at;
9177 struct bp_location *bl = b->loc;
9178 struct ui_out *uiout = current_uiout;
9179
9180 gdb_assert (b->type == bp_hardware_breakpoint);
9181
9182 /* Ranged breakpoints have only one location. */
9183 gdb_assert (bl && bl->next == NULL);
9184
9185 annotate_breakpoint (b->number);
9186 if (b->disposition == disp_del)
9187 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9188 else
9189 ui_out_text (uiout, "\nRanged breakpoint ");
9190 if (ui_out_is_mi_like_p (uiout))
9191 {
9192 ui_out_field_string (uiout, "reason",
9193 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9194 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9195 }
9196 ui_out_field_int (uiout, "bkptno", b->number);
9197 ui_out_text (uiout, ", ");
9198
9199 return PRINT_SRC_AND_LOC;
9200 }
9201
9202 /* Implement the "print_one" breakpoint_ops method for
9203 ranged breakpoints. */
9204
9205 static void
9206 print_one_ranged_breakpoint (struct breakpoint *b,
9207 struct bp_location **last_loc)
9208 {
9209 struct bp_location *bl = b->loc;
9210 struct value_print_options opts;
9211 struct ui_out *uiout = current_uiout;
9212
9213 /* Ranged breakpoints have only one location. */
9214 gdb_assert (bl && bl->next == NULL);
9215
9216 get_user_print_options (&opts);
9217
9218 if (opts.addressprint)
9219 /* We don't print the address range here, it will be printed later
9220 by print_one_detail_ranged_breakpoint. */
9221 ui_out_field_skip (uiout, "addr");
9222 annotate_field (5);
9223 print_breakpoint_location (b, bl);
9224 *last_loc = bl;
9225 }
9226
9227 /* Implement the "print_one_detail" breakpoint_ops method for
9228 ranged breakpoints. */
9229
9230 static void
9231 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9232 struct ui_out *uiout)
9233 {
9234 CORE_ADDR address_start, address_end;
9235 struct bp_location *bl = b->loc;
9236 struct ui_file *stb = mem_fileopen ();
9237 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
9238
9239 gdb_assert (bl);
9240
9241 address_start = bl->address;
9242 address_end = address_start + bl->length - 1;
9243
9244 ui_out_text (uiout, "\taddress range: ");
9245 fprintf_unfiltered (stb, "[%s, %s]",
9246 print_core_address (bl->gdbarch, address_start),
9247 print_core_address (bl->gdbarch, address_end));
9248 ui_out_field_stream (uiout, "addr", stb);
9249 ui_out_text (uiout, "\n");
9250
9251 do_cleanups (cleanup);
9252 }
9253
9254 /* Implement the "print_mention" breakpoint_ops method for
9255 ranged breakpoints. */
9256
9257 static void
9258 print_mention_ranged_breakpoint (struct breakpoint *b)
9259 {
9260 struct bp_location *bl = b->loc;
9261 struct ui_out *uiout = current_uiout;
9262
9263 gdb_assert (bl);
9264 gdb_assert (b->type == bp_hardware_breakpoint);
9265
9266 if (ui_out_is_mi_like_p (uiout))
9267 return;
9268
9269 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9270 b->number, paddress (bl->gdbarch, bl->address),
9271 paddress (bl->gdbarch, bl->address + bl->length - 1));
9272 }
9273
9274 /* Implement the "print_recreate" breakpoint_ops method for
9275 ranged breakpoints. */
9276
9277 static void
9278 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9279 {
9280 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9281 b->addr_string_range_end);
9282 print_recreate_thread (b, fp);
9283 }
9284
9285 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9286
9287 static struct breakpoint_ops ranged_breakpoint_ops;
9288
9289 /* Find the address where the end of the breakpoint range should be
9290 placed, given the SAL of the end of the range. This is so that if
9291 the user provides a line number, the end of the range is set to the
9292 last instruction of the given line. */
9293
9294 static CORE_ADDR
9295 find_breakpoint_range_end (struct symtab_and_line sal)
9296 {
9297 CORE_ADDR end;
9298
9299 /* If the user provided a PC value, use it. Otherwise,
9300 find the address of the end of the given location. */
9301 if (sal.explicit_pc)
9302 end = sal.pc;
9303 else
9304 {
9305 int ret;
9306 CORE_ADDR start;
9307
9308 ret = find_line_pc_range (sal, &start, &end);
9309 if (!ret)
9310 error (_("Could not find location of the end of the range."));
9311
9312 /* find_line_pc_range returns the start of the next line. */
9313 end--;
9314 }
9315
9316 return end;
9317 }
9318
9319 /* Implement the "break-range" CLI command. */
9320
9321 static void
9322 break_range_command (char *arg, int from_tty)
9323 {
9324 char *arg_start, *addr_string_start, *addr_string_end;
9325 struct linespec_result canonical_start, canonical_end;
9326 int bp_count, can_use_bp, length;
9327 CORE_ADDR end;
9328 struct breakpoint *b;
9329 struct symtab_and_line sal_start, sal_end;
9330 struct cleanup *cleanup_bkpt;
9331 struct linespec_sals *lsal_start, *lsal_end;
9332
9333 /* We don't support software ranged breakpoints. */
9334 if (target_ranged_break_num_registers () < 0)
9335 error (_("This target does not support hardware ranged breakpoints."));
9336
9337 bp_count = hw_breakpoint_used_count ();
9338 bp_count += target_ranged_break_num_registers ();
9339 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9340 bp_count, 0);
9341 if (can_use_bp < 0)
9342 error (_("Hardware breakpoints used exceeds limit."));
9343
9344 arg = skip_spaces (arg);
9345 if (arg == NULL || arg[0] == '\0')
9346 error(_("No address range specified."));
9347
9348 init_linespec_result (&canonical_start);
9349
9350 arg_start = arg;
9351 parse_breakpoint_sals (&arg, &canonical_start);
9352
9353 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
9354
9355 if (arg[0] != ',')
9356 error (_("Too few arguments."));
9357 else if (VEC_empty (linespec_sals, canonical_start.sals))
9358 error (_("Could not find location of the beginning of the range."));
9359
9360 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9361
9362 if (VEC_length (linespec_sals, canonical_start.sals) > 1
9363 || lsal_start->sals.nelts != 1)
9364 error (_("Cannot create a ranged breakpoint with multiple locations."));
9365
9366 sal_start = lsal_start->sals.sals[0];
9367 addr_string_start = savestring (arg_start, arg - arg_start);
9368 make_cleanup (xfree, addr_string_start);
9369
9370 arg++; /* Skip the comma. */
9371 arg = skip_spaces (arg);
9372
9373 /* Parse the end location. */
9374
9375 init_linespec_result (&canonical_end);
9376 arg_start = arg;
9377
9378 /* We call decode_line_full directly here instead of using
9379 parse_breakpoint_sals because we need to specify the start location's
9380 symtab and line as the default symtab and line for the end of the
9381 range. This makes it possible to have ranges like "foo.c:27, +14",
9382 where +14 means 14 lines from the start location. */
9383 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9384 sal_start.symtab, sal_start.line,
9385 &canonical_end, NULL, NULL);
9386
9387 make_cleanup_destroy_linespec_result (&canonical_end);
9388
9389 if (VEC_empty (linespec_sals, canonical_end.sals))
9390 error (_("Could not find location of the end of the range."));
9391
9392 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9393 if (VEC_length (linespec_sals, canonical_end.sals) > 1
9394 || lsal_end->sals.nelts != 1)
9395 error (_("Cannot create a ranged breakpoint with multiple locations."));
9396
9397 sal_end = lsal_end->sals.sals[0];
9398 addr_string_end = savestring (arg_start, arg - arg_start);
9399 make_cleanup (xfree, addr_string_end);
9400
9401 end = find_breakpoint_range_end (sal_end);
9402 if (sal_start.pc > end)
9403 error (_("Invalid address range, end precedes start."));
9404
9405 length = end - sal_start.pc + 1;
9406 if (length < 0)
9407 /* Length overflowed. */
9408 error (_("Address range too large."));
9409 else if (length == 1)
9410 {
9411 /* This range is simple enough to be handled by
9412 the `hbreak' command. */
9413 hbreak_command (addr_string_start, 1);
9414
9415 do_cleanups (cleanup_bkpt);
9416
9417 return;
9418 }
9419
9420 /* Now set up the breakpoint. */
9421 b = set_raw_breakpoint (get_current_arch (), sal_start,
9422 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9423 set_breakpoint_count (breakpoint_count + 1);
9424 b->number = breakpoint_count;
9425 b->disposition = disp_donttouch;
9426 b->addr_string = xstrdup (addr_string_start);
9427 b->addr_string_range_end = xstrdup (addr_string_end);
9428 b->loc->length = length;
9429
9430 do_cleanups (cleanup_bkpt);
9431
9432 mention (b);
9433 observer_notify_breakpoint_created (b);
9434 update_global_location_list (1);
9435 }
9436
9437 /* Return non-zero if EXP is verified as constant. Returned zero
9438 means EXP is variable. Also the constant detection may fail for
9439 some constant expressions and in such case still falsely return
9440 zero. */
9441
9442 static int
9443 watchpoint_exp_is_const (const struct expression *exp)
9444 {
9445 int i = exp->nelts;
9446
9447 while (i > 0)
9448 {
9449 int oplenp, argsp;
9450
9451 /* We are only interested in the descriptor of each element. */
9452 operator_length (exp, i, &oplenp, &argsp);
9453 i -= oplenp;
9454
9455 switch (exp->elts[i].opcode)
9456 {
9457 case BINOP_ADD:
9458 case BINOP_SUB:
9459 case BINOP_MUL:
9460 case BINOP_DIV:
9461 case BINOP_REM:
9462 case BINOP_MOD:
9463 case BINOP_LSH:
9464 case BINOP_RSH:
9465 case BINOP_LOGICAL_AND:
9466 case BINOP_LOGICAL_OR:
9467 case BINOP_BITWISE_AND:
9468 case BINOP_BITWISE_IOR:
9469 case BINOP_BITWISE_XOR:
9470 case BINOP_EQUAL:
9471 case BINOP_NOTEQUAL:
9472 case BINOP_LESS:
9473 case BINOP_GTR:
9474 case BINOP_LEQ:
9475 case BINOP_GEQ:
9476 case BINOP_REPEAT:
9477 case BINOP_COMMA:
9478 case BINOP_EXP:
9479 case BINOP_MIN:
9480 case BINOP_MAX:
9481 case BINOP_INTDIV:
9482 case BINOP_CONCAT:
9483 case BINOP_IN:
9484 case BINOP_RANGE:
9485 case TERNOP_COND:
9486 case TERNOP_SLICE:
9487 case TERNOP_SLICE_COUNT:
9488
9489 case OP_LONG:
9490 case OP_DOUBLE:
9491 case OP_DECFLOAT:
9492 case OP_LAST:
9493 case OP_COMPLEX:
9494 case OP_STRING:
9495 case OP_BITSTRING:
9496 case OP_ARRAY:
9497 case OP_TYPE:
9498 case OP_NAME:
9499 case OP_OBJC_NSSTRING:
9500
9501 case UNOP_NEG:
9502 case UNOP_LOGICAL_NOT:
9503 case UNOP_COMPLEMENT:
9504 case UNOP_ADDR:
9505 case UNOP_HIGH:
9506 case UNOP_CAST:
9507 /* Unary, binary and ternary operators: We have to check
9508 their operands. If they are constant, then so is the
9509 result of that operation. For instance, if A and B are
9510 determined to be constants, then so is "A + B".
9511
9512 UNOP_IND is one exception to the rule above, because the
9513 value of *ADDR is not necessarily a constant, even when
9514 ADDR is. */
9515 break;
9516
9517 case OP_VAR_VALUE:
9518 /* Check whether the associated symbol is a constant.
9519
9520 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9521 possible that a buggy compiler could mark a variable as
9522 constant even when it is not, and TYPE_CONST would return
9523 true in this case, while SYMBOL_CLASS wouldn't.
9524
9525 We also have to check for function symbols because they
9526 are always constant. */
9527 {
9528 struct symbol *s = exp->elts[i + 2].symbol;
9529
9530 if (SYMBOL_CLASS (s) != LOC_BLOCK
9531 && SYMBOL_CLASS (s) != LOC_CONST
9532 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9533 return 0;
9534 break;
9535 }
9536
9537 /* The default action is to return 0 because we are using
9538 the optimistic approach here: If we don't know something,
9539 then it is not a constant. */
9540 default:
9541 return 0;
9542 }
9543 }
9544
9545 return 1;
9546 }
9547
9548 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
9549
9550 static void
9551 dtor_watchpoint (struct breakpoint *self)
9552 {
9553 struct watchpoint *w = (struct watchpoint *) self;
9554
9555 xfree (w->cond_exp);
9556 xfree (w->exp);
9557 xfree (w->exp_string);
9558 xfree (w->exp_string_reparse);
9559 value_free (w->val);
9560
9561 base_breakpoint_ops.dtor (self);
9562 }
9563
9564 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
9565
9566 static void
9567 re_set_watchpoint (struct breakpoint *b)
9568 {
9569 struct watchpoint *w = (struct watchpoint *) b;
9570
9571 /* Watchpoint can be either on expression using entirely global
9572 variables, or it can be on local variables.
9573
9574 Watchpoints of the first kind are never auto-deleted, and even
9575 persist across program restarts. Since they can use variables
9576 from shared libraries, we need to reparse expression as libraries
9577 are loaded and unloaded.
9578
9579 Watchpoints on local variables can also change meaning as result
9580 of solib event. For example, if a watchpoint uses both a local
9581 and a global variables in expression, it's a local watchpoint,
9582 but unloading of a shared library will make the expression
9583 invalid. This is not a very common use case, but we still
9584 re-evaluate expression, to avoid surprises to the user.
9585
9586 Note that for local watchpoints, we re-evaluate it only if
9587 watchpoints frame id is still valid. If it's not, it means the
9588 watchpoint is out of scope and will be deleted soon. In fact,
9589 I'm not sure we'll ever be called in this case.
9590
9591 If a local watchpoint's frame id is still valid, then
9592 w->exp_valid_block is likewise valid, and we can safely use it.
9593
9594 Don't do anything about disabled watchpoints, since they will be
9595 reevaluated again when enabled. */
9596 update_watchpoint (w, 1 /* reparse */);
9597 }
9598
9599 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
9600
9601 static int
9602 insert_watchpoint (struct bp_location *bl)
9603 {
9604 struct watchpoint *w = (struct watchpoint *) bl->owner;
9605 int length = w->exact ? 1 : bl->length;
9606
9607 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9608 w->cond_exp);
9609 }
9610
9611 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
9612
9613 static int
9614 remove_watchpoint (struct bp_location *bl)
9615 {
9616 struct watchpoint *w = (struct watchpoint *) bl->owner;
9617 int length = w->exact ? 1 : bl->length;
9618
9619 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9620 w->cond_exp);
9621 }
9622
9623 static int
9624 breakpoint_hit_watchpoint (const struct bp_location *bl,
9625 struct address_space *aspace, CORE_ADDR bp_addr,
9626 const struct target_waitstatus *ws)
9627 {
9628 struct breakpoint *b = bl->owner;
9629 struct watchpoint *w = (struct watchpoint *) b;
9630
9631 /* Continuable hardware watchpoints are treated as non-existent if the
9632 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9633 some data address). Otherwise gdb won't stop on a break instruction
9634 in the code (not from a breakpoint) when a hardware watchpoint has
9635 been defined. Also skip watchpoints which we know did not trigger
9636 (did not match the data address). */
9637 if (is_hardware_watchpoint (b)
9638 && w->watchpoint_triggered == watch_triggered_no)
9639 return 0;
9640
9641 return 1;
9642 }
9643
9644 static void
9645 check_status_watchpoint (bpstat bs)
9646 {
9647 gdb_assert (is_watchpoint (bs->breakpoint_at));
9648
9649 bpstat_check_watchpoint (bs);
9650 }
9651
9652 /* Implement the "resources_needed" breakpoint_ops method for
9653 hardware watchpoints. */
9654
9655 static int
9656 resources_needed_watchpoint (const struct bp_location *bl)
9657 {
9658 struct watchpoint *w = (struct watchpoint *) bl->owner;
9659 int length = w->exact? 1 : bl->length;
9660
9661 return target_region_ok_for_hw_watchpoint (bl->address, length);
9662 }
9663
9664 /* Implement the "works_in_software_mode" breakpoint_ops method for
9665 hardware watchpoints. */
9666
9667 static int
9668 works_in_software_mode_watchpoint (const struct breakpoint *b)
9669 {
9670 /* Read and access watchpoints only work with hardware support. */
9671 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9672 }
9673
9674 static enum print_stop_action
9675 print_it_watchpoint (bpstat bs)
9676 {
9677 struct cleanup *old_chain;
9678 struct breakpoint *b;
9679 const struct bp_location *bl;
9680 struct ui_file *stb;
9681 enum print_stop_action result;
9682 struct watchpoint *w;
9683 struct ui_out *uiout = current_uiout;
9684
9685 gdb_assert (bs->bp_location_at != NULL);
9686
9687 bl = bs->bp_location_at;
9688 b = bs->breakpoint_at;
9689 w = (struct watchpoint *) b;
9690
9691 stb = mem_fileopen ();
9692 old_chain = make_cleanup_ui_file_delete (stb);
9693
9694 switch (b->type)
9695 {
9696 case bp_watchpoint:
9697 case bp_hardware_watchpoint:
9698 annotate_watchpoint (b->number);
9699 if (ui_out_is_mi_like_p (uiout))
9700 ui_out_field_string
9701 (uiout, "reason",
9702 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9703 mention (b);
9704 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
9705 ui_out_text (uiout, "\nOld value = ");
9706 watchpoint_value_print (bs->old_val, stb);
9707 ui_out_field_stream (uiout, "old", stb);
9708 ui_out_text (uiout, "\nNew value = ");
9709 watchpoint_value_print (w->val, stb);
9710 ui_out_field_stream (uiout, "new", stb);
9711 ui_out_text (uiout, "\n");
9712 /* More than one watchpoint may have been triggered. */
9713 result = PRINT_UNKNOWN;
9714 break;
9715
9716 case bp_read_watchpoint:
9717 if (ui_out_is_mi_like_p (uiout))
9718 ui_out_field_string
9719 (uiout, "reason",
9720 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9721 mention (b);
9722 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
9723 ui_out_text (uiout, "\nValue = ");
9724 watchpoint_value_print (w->val, stb);
9725 ui_out_field_stream (uiout, "value", stb);
9726 ui_out_text (uiout, "\n");
9727 result = PRINT_UNKNOWN;
9728 break;
9729
9730 case bp_access_watchpoint:
9731 if (bs->old_val != NULL)
9732 {
9733 annotate_watchpoint (b->number);
9734 if (ui_out_is_mi_like_p (uiout))
9735 ui_out_field_string
9736 (uiout, "reason",
9737 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9738 mention (b);
9739 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
9740 ui_out_text (uiout, "\nOld value = ");
9741 watchpoint_value_print (bs->old_val, stb);
9742 ui_out_field_stream (uiout, "old", stb);
9743 ui_out_text (uiout, "\nNew value = ");
9744 }
9745 else
9746 {
9747 mention (b);
9748 if (ui_out_is_mi_like_p (uiout))
9749 ui_out_field_string
9750 (uiout, "reason",
9751 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9752 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
9753 ui_out_text (uiout, "\nValue = ");
9754 }
9755 watchpoint_value_print (w->val, stb);
9756 ui_out_field_stream (uiout, "new", stb);
9757 ui_out_text (uiout, "\n");
9758 result = PRINT_UNKNOWN;
9759 break;
9760 default:
9761 result = PRINT_UNKNOWN;
9762 }
9763
9764 do_cleanups (old_chain);
9765 return result;
9766 }
9767
9768 /* Implement the "print_mention" breakpoint_ops method for hardware
9769 watchpoints. */
9770
9771 static void
9772 print_mention_watchpoint (struct breakpoint *b)
9773 {
9774 struct cleanup *ui_out_chain;
9775 struct watchpoint *w = (struct watchpoint *) b;
9776 struct ui_out *uiout = current_uiout;
9777
9778 switch (b->type)
9779 {
9780 case bp_watchpoint:
9781 ui_out_text (uiout, "Watchpoint ");
9782 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9783 break;
9784 case bp_hardware_watchpoint:
9785 ui_out_text (uiout, "Hardware watchpoint ");
9786 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9787 break;
9788 case bp_read_watchpoint:
9789 ui_out_text (uiout, "Hardware read watchpoint ");
9790 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9791 break;
9792 case bp_access_watchpoint:
9793 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
9794 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9795 break;
9796 default:
9797 internal_error (__FILE__, __LINE__,
9798 _("Invalid hardware watchpoint type."));
9799 }
9800
9801 ui_out_field_int (uiout, "number", b->number);
9802 ui_out_text (uiout, ": ");
9803 ui_out_field_string (uiout, "exp", w->exp_string);
9804 do_cleanups (ui_out_chain);
9805 }
9806
9807 /* Implement the "print_recreate" breakpoint_ops method for
9808 watchpoints. */
9809
9810 static void
9811 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
9812 {
9813 struct watchpoint *w = (struct watchpoint *) b;
9814
9815 switch (b->type)
9816 {
9817 case bp_watchpoint:
9818 case bp_hardware_watchpoint:
9819 fprintf_unfiltered (fp, "watch");
9820 break;
9821 case bp_read_watchpoint:
9822 fprintf_unfiltered (fp, "rwatch");
9823 break;
9824 case bp_access_watchpoint:
9825 fprintf_unfiltered (fp, "awatch");
9826 break;
9827 default:
9828 internal_error (__FILE__, __LINE__,
9829 _("Invalid watchpoint type."));
9830 }
9831
9832 fprintf_unfiltered (fp, " %s", w->exp_string);
9833 print_recreate_thread (b, fp);
9834 }
9835
9836 /* The breakpoint_ops structure to be used in hardware watchpoints. */
9837
9838 static struct breakpoint_ops watchpoint_breakpoint_ops;
9839
9840 /* Implement the "insert" breakpoint_ops method for
9841 masked hardware watchpoints. */
9842
9843 static int
9844 insert_masked_watchpoint (struct bp_location *bl)
9845 {
9846 struct watchpoint *w = (struct watchpoint *) bl->owner;
9847
9848 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
9849 bl->watchpoint_type);
9850 }
9851
9852 /* Implement the "remove" breakpoint_ops method for
9853 masked hardware watchpoints. */
9854
9855 static int
9856 remove_masked_watchpoint (struct bp_location *bl)
9857 {
9858 struct watchpoint *w = (struct watchpoint *) bl->owner;
9859
9860 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
9861 bl->watchpoint_type);
9862 }
9863
9864 /* Implement the "resources_needed" breakpoint_ops method for
9865 masked hardware watchpoints. */
9866
9867 static int
9868 resources_needed_masked_watchpoint (const struct bp_location *bl)
9869 {
9870 struct watchpoint *w = (struct watchpoint *) bl->owner;
9871
9872 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
9873 }
9874
9875 /* Implement the "works_in_software_mode" breakpoint_ops method for
9876 masked hardware watchpoints. */
9877
9878 static int
9879 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
9880 {
9881 return 0;
9882 }
9883
9884 /* Implement the "print_it" breakpoint_ops method for
9885 masked hardware watchpoints. */
9886
9887 static enum print_stop_action
9888 print_it_masked_watchpoint (bpstat bs)
9889 {
9890 struct breakpoint *b = bs->breakpoint_at;
9891 struct ui_out *uiout = current_uiout;
9892
9893 /* Masked watchpoints have only one location. */
9894 gdb_assert (b->loc && b->loc->next == NULL);
9895
9896 switch (b->type)
9897 {
9898 case bp_hardware_watchpoint:
9899 annotate_watchpoint (b->number);
9900 if (ui_out_is_mi_like_p (uiout))
9901 ui_out_field_string
9902 (uiout, "reason",
9903 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9904 break;
9905
9906 case bp_read_watchpoint:
9907 if (ui_out_is_mi_like_p (uiout))
9908 ui_out_field_string
9909 (uiout, "reason",
9910 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9911 break;
9912
9913 case bp_access_watchpoint:
9914 if (ui_out_is_mi_like_p (uiout))
9915 ui_out_field_string
9916 (uiout, "reason",
9917 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9918 break;
9919 default:
9920 internal_error (__FILE__, __LINE__,
9921 _("Invalid hardware watchpoint type."));
9922 }
9923
9924 mention (b);
9925 ui_out_text (uiout, _("\n\
9926 Check the underlying instruction at PC for the memory\n\
9927 address and value which triggered this watchpoint.\n"));
9928 ui_out_text (uiout, "\n");
9929
9930 /* More than one watchpoint may have been triggered. */
9931 return PRINT_UNKNOWN;
9932 }
9933
9934 /* Implement the "print_one_detail" breakpoint_ops method for
9935 masked hardware watchpoints. */
9936
9937 static void
9938 print_one_detail_masked_watchpoint (const struct breakpoint *b,
9939 struct ui_out *uiout)
9940 {
9941 struct watchpoint *w = (struct watchpoint *) b;
9942
9943 /* Masked watchpoints have only one location. */
9944 gdb_assert (b->loc && b->loc->next == NULL);
9945
9946 ui_out_text (uiout, "\tmask ");
9947 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9948 ui_out_text (uiout, "\n");
9949 }
9950
9951 /* Implement the "print_mention" breakpoint_ops method for
9952 masked hardware watchpoints. */
9953
9954 static void
9955 print_mention_masked_watchpoint (struct breakpoint *b)
9956 {
9957 struct watchpoint *w = (struct watchpoint *) b;
9958 struct ui_out *uiout = current_uiout;
9959 struct cleanup *ui_out_chain;
9960
9961 switch (b->type)
9962 {
9963 case bp_hardware_watchpoint:
9964 ui_out_text (uiout, "Masked hardware watchpoint ");
9965 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9966 break;
9967 case bp_read_watchpoint:
9968 ui_out_text (uiout, "Masked hardware read watchpoint ");
9969 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9970 break;
9971 case bp_access_watchpoint:
9972 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
9973 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9974 break;
9975 default:
9976 internal_error (__FILE__, __LINE__,
9977 _("Invalid hardware watchpoint type."));
9978 }
9979
9980 ui_out_field_int (uiout, "number", b->number);
9981 ui_out_text (uiout, ": ");
9982 ui_out_field_string (uiout, "exp", w->exp_string);
9983 do_cleanups (ui_out_chain);
9984 }
9985
9986 /* Implement the "print_recreate" breakpoint_ops method for
9987 masked hardware watchpoints. */
9988
9989 static void
9990 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
9991 {
9992 struct watchpoint *w = (struct watchpoint *) b;
9993 char tmp[40];
9994
9995 switch (b->type)
9996 {
9997 case bp_hardware_watchpoint:
9998 fprintf_unfiltered (fp, "watch");
9999 break;
10000 case bp_read_watchpoint:
10001 fprintf_unfiltered (fp, "rwatch");
10002 break;
10003 case bp_access_watchpoint:
10004 fprintf_unfiltered (fp, "awatch");
10005 break;
10006 default:
10007 internal_error (__FILE__, __LINE__,
10008 _("Invalid hardware watchpoint type."));
10009 }
10010
10011 sprintf_vma (tmp, w->hw_wp_mask);
10012 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10013 print_recreate_thread (b, fp);
10014 }
10015
10016 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10017
10018 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10019
10020 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10021
10022 static int
10023 is_masked_watchpoint (const struct breakpoint *b)
10024 {
10025 return b->ops == &masked_watchpoint_breakpoint_ops;
10026 }
10027
10028 /* accessflag: hw_write: watch write,
10029 hw_read: watch read,
10030 hw_access: watch access (read or write) */
10031 static void
10032 watch_command_1 (char *arg, int accessflag, int from_tty,
10033 int just_location, int internal)
10034 {
10035 volatile struct gdb_exception e;
10036 struct breakpoint *b, *scope_breakpoint = NULL;
10037 struct expression *exp;
10038 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10039 struct value *val, *mark, *result;
10040 struct frame_info *frame;
10041 char *exp_start = NULL;
10042 char *exp_end = NULL;
10043 char *tok, *end_tok;
10044 int toklen = -1;
10045 char *cond_start = NULL;
10046 char *cond_end = NULL;
10047 enum bptype bp_type;
10048 int thread = -1;
10049 int pc = 0;
10050 /* Flag to indicate whether we are going to use masks for
10051 the hardware watchpoint. */
10052 int use_mask = 0;
10053 CORE_ADDR mask = 0;
10054 struct watchpoint *w;
10055
10056 /* Make sure that we actually have parameters to parse. */
10057 if (arg != NULL && arg[0] != '\0')
10058 {
10059 char *value_start;
10060
10061 /* Look for "parameter value" pairs at the end
10062 of the arguments string. */
10063 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10064 {
10065 /* Skip whitespace at the end of the argument list. */
10066 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10067 tok--;
10068
10069 /* Find the beginning of the last token.
10070 This is the value of the parameter. */
10071 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10072 tok--;
10073 value_start = tok + 1;
10074
10075 /* Skip whitespace. */
10076 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10077 tok--;
10078
10079 end_tok = tok;
10080
10081 /* Find the beginning of the second to last token.
10082 This is the parameter itself. */
10083 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10084 tok--;
10085 tok++;
10086 toklen = end_tok - tok + 1;
10087
10088 if (toklen == 6 && !strncmp (tok, "thread", 6))
10089 {
10090 /* At this point we've found a "thread" token, which means
10091 the user is trying to set a watchpoint that triggers
10092 only in a specific thread. */
10093 char *endp;
10094
10095 if (thread != -1)
10096 error(_("You can specify only one thread."));
10097
10098 /* Extract the thread ID from the next token. */
10099 thread = strtol (value_start, &endp, 0);
10100
10101 /* Check if the user provided a valid numeric value for the
10102 thread ID. */
10103 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10104 error (_("Invalid thread ID specification %s."), value_start);
10105
10106 /* Check if the thread actually exists. */
10107 if (!valid_thread_id (thread))
10108 error (_("Unknown thread %d."), thread);
10109 }
10110 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10111 {
10112 /* We've found a "mask" token, which means the user wants to
10113 create a hardware watchpoint that is going to have the mask
10114 facility. */
10115 struct value *mask_value, *mark;
10116
10117 if (use_mask)
10118 error(_("You can specify only one mask."));
10119
10120 use_mask = just_location = 1;
10121
10122 mark = value_mark ();
10123 mask_value = parse_to_comma_and_eval (&value_start);
10124 mask = value_as_address (mask_value);
10125 value_free_to_mark (mark);
10126 }
10127 else
10128 /* We didn't recognize what we found. We should stop here. */
10129 break;
10130
10131 /* Truncate the string and get rid of the "parameter value" pair before
10132 the arguments string is parsed by the parse_exp_1 function. */
10133 *tok = '\0';
10134 }
10135 }
10136
10137 /* Parse the rest of the arguments. */
10138 innermost_block = NULL;
10139 exp_start = arg;
10140 exp = parse_exp_1 (&arg, 0, 0);
10141 exp_end = arg;
10142 /* Remove trailing whitespace from the expression before saving it.
10143 This makes the eventual display of the expression string a bit
10144 prettier. */
10145 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10146 --exp_end;
10147
10148 /* Checking if the expression is not constant. */
10149 if (watchpoint_exp_is_const (exp))
10150 {
10151 int len;
10152
10153 len = exp_end - exp_start;
10154 while (len > 0 && isspace (exp_start[len - 1]))
10155 len--;
10156 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10157 }
10158
10159 exp_valid_block = innermost_block;
10160 mark = value_mark ();
10161 fetch_subexp_value (exp, &pc, &val, &result, NULL);
10162
10163 if (just_location)
10164 {
10165 int ret;
10166
10167 exp_valid_block = NULL;
10168 val = value_addr (result);
10169 release_value (val);
10170 value_free_to_mark (mark);
10171
10172 if (use_mask)
10173 {
10174 ret = target_masked_watch_num_registers (value_as_address (val),
10175 mask);
10176 if (ret == -1)
10177 error (_("This target does not support masked watchpoints."));
10178 else if (ret == -2)
10179 error (_("Invalid mask or memory region."));
10180 }
10181 }
10182 else if (val != NULL)
10183 release_value (val);
10184
10185 tok = skip_spaces (arg);
10186 end_tok = skip_to_space (tok);
10187
10188 toklen = end_tok - tok;
10189 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10190 {
10191 struct expression *cond;
10192
10193 innermost_block = NULL;
10194 tok = cond_start = end_tok + 1;
10195 cond = parse_exp_1 (&tok, 0, 0);
10196
10197 /* The watchpoint expression may not be local, but the condition
10198 may still be. E.g.: `watch global if local > 0'. */
10199 cond_exp_valid_block = innermost_block;
10200
10201 xfree (cond);
10202 cond_end = tok;
10203 }
10204 if (*tok)
10205 error (_("Junk at end of command."));
10206
10207 if (accessflag == hw_read)
10208 bp_type = bp_read_watchpoint;
10209 else if (accessflag == hw_access)
10210 bp_type = bp_access_watchpoint;
10211 else
10212 bp_type = bp_hardware_watchpoint;
10213
10214 frame = block_innermost_frame (exp_valid_block);
10215
10216 /* If the expression is "local", then set up a "watchpoint scope"
10217 breakpoint at the point where we've left the scope of the watchpoint
10218 expression. Create the scope breakpoint before the watchpoint, so
10219 that we will encounter it first in bpstat_stop_status. */
10220 if (exp_valid_block && frame)
10221 {
10222 if (frame_id_p (frame_unwind_caller_id (frame)))
10223 {
10224 scope_breakpoint
10225 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10226 frame_unwind_caller_pc (frame),
10227 bp_watchpoint_scope,
10228 &momentary_breakpoint_ops);
10229
10230 scope_breakpoint->enable_state = bp_enabled;
10231
10232 /* Automatically delete the breakpoint when it hits. */
10233 scope_breakpoint->disposition = disp_del;
10234
10235 /* Only break in the proper frame (help with recursion). */
10236 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
10237
10238 /* Set the address at which we will stop. */
10239 scope_breakpoint->loc->gdbarch
10240 = frame_unwind_caller_arch (frame);
10241 scope_breakpoint->loc->requested_address
10242 = frame_unwind_caller_pc (frame);
10243 scope_breakpoint->loc->address
10244 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10245 scope_breakpoint->loc->requested_address,
10246 scope_breakpoint->type);
10247 }
10248 }
10249
10250 /* Now set up the breakpoint. */
10251
10252 w = XCNEW (struct watchpoint);
10253 b = &w->base;
10254 if (use_mask)
10255 init_raw_breakpoint_without_location (b, NULL, bp_type,
10256 &masked_watchpoint_breakpoint_ops);
10257 else
10258 init_raw_breakpoint_without_location (b, NULL, bp_type,
10259 &watchpoint_breakpoint_ops);
10260 b->thread = thread;
10261 b->disposition = disp_donttouch;
10262 b->pspace = current_program_space;
10263 w->exp = exp;
10264 w->exp_valid_block = exp_valid_block;
10265 w->cond_exp_valid_block = cond_exp_valid_block;
10266 if (just_location)
10267 {
10268 struct type *t = value_type (val);
10269 CORE_ADDR addr = value_as_address (val);
10270 char *name;
10271
10272 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10273 name = type_to_string (t);
10274
10275 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
10276 core_addr_to_string (addr));
10277 xfree (name);
10278
10279 w->exp_string = xstrprintf ("-location %.*s",
10280 (int) (exp_end - exp_start), exp_start);
10281
10282 /* The above expression is in C. */
10283 b->language = language_c;
10284 }
10285 else
10286 w->exp_string = savestring (exp_start, exp_end - exp_start);
10287
10288 if (use_mask)
10289 {
10290 w->hw_wp_mask = mask;
10291 }
10292 else
10293 {
10294 w->val = val;
10295 w->val_valid = 1;
10296 }
10297
10298 if (cond_start)
10299 b->cond_string = savestring (cond_start, cond_end - cond_start);
10300 else
10301 b->cond_string = 0;
10302
10303 if (frame)
10304 {
10305 w->watchpoint_frame = get_frame_id (frame);
10306 w->watchpoint_thread = inferior_ptid;
10307 }
10308 else
10309 {
10310 w->watchpoint_frame = null_frame_id;
10311 w->watchpoint_thread = null_ptid;
10312 }
10313
10314 if (scope_breakpoint != NULL)
10315 {
10316 /* The scope breakpoint is related to the watchpoint. We will
10317 need to act on them together. */
10318 b->related_breakpoint = scope_breakpoint;
10319 scope_breakpoint->related_breakpoint = b;
10320 }
10321
10322 if (!just_location)
10323 value_free_to_mark (mark);
10324
10325 TRY_CATCH (e, RETURN_MASK_ALL)
10326 {
10327 /* Finally update the new watchpoint. This creates the locations
10328 that should be inserted. */
10329 update_watchpoint (w, 1);
10330 }
10331 if (e.reason < 0)
10332 {
10333 delete_breakpoint (b);
10334 throw_exception (e);
10335 }
10336
10337 install_breakpoint (internal, b, 1);
10338 }
10339
10340 /* Return count of debug registers needed to watch the given expression.
10341 If the watchpoint cannot be handled in hardware return zero. */
10342
10343 static int
10344 can_use_hardware_watchpoint (struct value *v)
10345 {
10346 int found_memory_cnt = 0;
10347 struct value *head = v;
10348
10349 /* Did the user specifically forbid us to use hardware watchpoints? */
10350 if (!can_use_hw_watchpoints)
10351 return 0;
10352
10353 /* Make sure that the value of the expression depends only upon
10354 memory contents, and values computed from them within GDB. If we
10355 find any register references or function calls, we can't use a
10356 hardware watchpoint.
10357
10358 The idea here is that evaluating an expression generates a series
10359 of values, one holding the value of every subexpression. (The
10360 expression a*b+c has five subexpressions: a, b, a*b, c, and
10361 a*b+c.) GDB's values hold almost enough information to establish
10362 the criteria given above --- they identify memory lvalues,
10363 register lvalues, computed values, etcetera. So we can evaluate
10364 the expression, and then scan the chain of values that leaves
10365 behind to decide whether we can detect any possible change to the
10366 expression's final value using only hardware watchpoints.
10367
10368 However, I don't think that the values returned by inferior
10369 function calls are special in any way. So this function may not
10370 notice that an expression involving an inferior function call
10371 can't be watched with hardware watchpoints. FIXME. */
10372 for (; v; v = value_next (v))
10373 {
10374 if (VALUE_LVAL (v) == lval_memory)
10375 {
10376 if (v != head && value_lazy (v))
10377 /* A lazy memory lvalue in the chain is one that GDB never
10378 needed to fetch; we either just used its address (e.g.,
10379 `a' in `a.b') or we never needed it at all (e.g., `a'
10380 in `a,b'). This doesn't apply to HEAD; if that is
10381 lazy then it was not readable, but watch it anyway. */
10382 ;
10383 else
10384 {
10385 /* Ahh, memory we actually used! Check if we can cover
10386 it with hardware watchpoints. */
10387 struct type *vtype = check_typedef (value_type (v));
10388
10389 /* We only watch structs and arrays if user asked for it
10390 explicitly, never if they just happen to appear in a
10391 middle of some value chain. */
10392 if (v == head
10393 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10394 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10395 {
10396 CORE_ADDR vaddr = value_address (v);
10397 int len;
10398 int num_regs;
10399
10400 len = (target_exact_watchpoints
10401 && is_scalar_type_recursive (vtype))?
10402 1 : TYPE_LENGTH (value_type (v));
10403
10404 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10405 if (!num_regs)
10406 return 0;
10407 else
10408 found_memory_cnt += num_regs;
10409 }
10410 }
10411 }
10412 else if (VALUE_LVAL (v) != not_lval
10413 && deprecated_value_modifiable (v) == 0)
10414 return 0; /* These are values from the history (e.g., $1). */
10415 else if (VALUE_LVAL (v) == lval_register)
10416 return 0; /* Cannot watch a register with a HW watchpoint. */
10417 }
10418
10419 /* The expression itself looks suitable for using a hardware
10420 watchpoint, but give the target machine a chance to reject it. */
10421 return found_memory_cnt;
10422 }
10423
10424 void
10425 watch_command_wrapper (char *arg, int from_tty, int internal)
10426 {
10427 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10428 }
10429
10430 /* A helper function that looks for an argument at the start of a
10431 string. The argument must also either be at the end of the string,
10432 or be followed by whitespace. Returns 1 if it finds the argument,
10433 0 otherwise. If the argument is found, it updates *STR. */
10434
10435 static int
10436 check_for_argument (char **str, char *arg, int arg_len)
10437 {
10438 if (strncmp (*str, arg, arg_len) == 0
10439 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10440 {
10441 *str += arg_len;
10442 return 1;
10443 }
10444 return 0;
10445 }
10446
10447 /* A helper function that looks for the "-location" argument and then
10448 calls watch_command_1. */
10449
10450 static void
10451 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10452 {
10453 int just_location = 0;
10454
10455 if (arg
10456 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10457 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10458 {
10459 arg = skip_spaces (arg);
10460 just_location = 1;
10461 }
10462
10463 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10464 }
10465
10466 static void
10467 watch_command (char *arg, int from_tty)
10468 {
10469 watch_maybe_just_location (arg, hw_write, from_tty);
10470 }
10471
10472 void
10473 rwatch_command_wrapper (char *arg, int from_tty, int internal)
10474 {
10475 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10476 }
10477
10478 static void
10479 rwatch_command (char *arg, int from_tty)
10480 {
10481 watch_maybe_just_location (arg, hw_read, from_tty);
10482 }
10483
10484 void
10485 awatch_command_wrapper (char *arg, int from_tty, int internal)
10486 {
10487 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10488 }
10489
10490 static void
10491 awatch_command (char *arg, int from_tty)
10492 {
10493 watch_maybe_just_location (arg, hw_access, from_tty);
10494 }
10495 \f
10496
10497 /* Helper routines for the until_command routine in infcmd.c. Here
10498 because it uses the mechanisms of breakpoints. */
10499
10500 struct until_break_command_continuation_args
10501 {
10502 struct breakpoint *breakpoint;
10503 struct breakpoint *breakpoint2;
10504 int thread_num;
10505 };
10506
10507 /* This function is called by fetch_inferior_event via the
10508 cmd_continuation pointer, to complete the until command. It takes
10509 care of cleaning up the temporary breakpoints set up by the until
10510 command. */
10511 static void
10512 until_break_command_continuation (void *arg, int err)
10513 {
10514 struct until_break_command_continuation_args *a = arg;
10515
10516 delete_breakpoint (a->breakpoint);
10517 if (a->breakpoint2)
10518 delete_breakpoint (a->breakpoint2);
10519 delete_longjmp_breakpoint (a->thread_num);
10520 }
10521
10522 void
10523 until_break_command (char *arg, int from_tty, int anywhere)
10524 {
10525 struct symtabs_and_lines sals;
10526 struct symtab_and_line sal;
10527 struct frame_info *frame = get_selected_frame (NULL);
10528 struct gdbarch *frame_gdbarch = get_frame_arch (frame);
10529 struct frame_id stack_frame_id = get_stack_frame_id (frame);
10530 struct frame_id caller_frame_id = frame_unwind_caller_id (frame);
10531 struct breakpoint *breakpoint;
10532 struct breakpoint *breakpoint2 = NULL;
10533 struct cleanup *old_chain;
10534 int thread;
10535 struct thread_info *tp;
10536
10537 clear_proceed_status ();
10538
10539 /* Set a breakpoint where the user wants it and at return from
10540 this function. */
10541
10542 if (last_displayed_sal_is_valid ())
10543 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10544 get_last_displayed_symtab (),
10545 get_last_displayed_line ());
10546 else
10547 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10548 (struct symtab *) NULL, 0);
10549
10550 if (sals.nelts != 1)
10551 error (_("Couldn't get information on specified line."));
10552
10553 sal = sals.sals[0];
10554 xfree (sals.sals); /* malloc'd, so freed. */
10555
10556 if (*arg)
10557 error (_("Junk at end of arguments."));
10558
10559 resolve_sal_pc (&sal);
10560
10561 tp = inferior_thread ();
10562 thread = tp->num;
10563
10564 old_chain = make_cleanup (null_cleanup, NULL);
10565
10566 /* Installing a breakpoint invalidates the frame chain (as it may
10567 need to switch threads), so do any frame handling first. */
10568
10569 /* Keep within the current frame, or in frames called by the current
10570 one. */
10571
10572 if (frame_id_p (caller_frame_id))
10573 {
10574 struct symtab_and_line sal2;
10575
10576 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10577 sal2.pc = frame_unwind_caller_pc (frame);
10578 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
10579 sal2,
10580 caller_frame_id,
10581 bp_until);
10582 make_cleanup_delete_breakpoint (breakpoint2);
10583
10584 set_longjmp_breakpoint (tp, caller_frame_id);
10585 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
10586 }
10587
10588 /* set_momentary_breakpoint could invalidate FRAME. */
10589 frame = NULL;
10590
10591 if (anywhere)
10592 /* If the user told us to continue until a specified location,
10593 we don't specify a frame at which we need to stop. */
10594 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10595 null_frame_id, bp_until);
10596 else
10597 /* Otherwise, specify the selected frame, because we want to stop
10598 only at the very same frame. */
10599 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10600 stack_frame_id, bp_until);
10601 make_cleanup_delete_breakpoint (breakpoint);
10602
10603 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
10604
10605 /* If we are running asynchronously, and proceed call above has
10606 actually managed to start the target, arrange for breakpoints to
10607 be deleted when the target stops. Otherwise, we're already
10608 stopped and delete breakpoints via cleanup chain. */
10609
10610 if (target_can_async_p () && is_running (inferior_ptid))
10611 {
10612 struct until_break_command_continuation_args *args;
10613 args = xmalloc (sizeof (*args));
10614
10615 args->breakpoint = breakpoint;
10616 args->breakpoint2 = breakpoint2;
10617 args->thread_num = thread;
10618
10619 discard_cleanups (old_chain);
10620 add_continuation (inferior_thread (),
10621 until_break_command_continuation, args,
10622 xfree);
10623 }
10624 else
10625 do_cleanups (old_chain);
10626 }
10627
10628 /* This function attempts to parse an optional "if <cond>" clause
10629 from the arg string. If one is not found, it returns NULL.
10630
10631 Else, it returns a pointer to the condition string. (It does not
10632 attempt to evaluate the string against a particular block.) And,
10633 it updates arg to point to the first character following the parsed
10634 if clause in the arg string. */
10635
10636 static char *
10637 ep_parse_optional_if_clause (char **arg)
10638 {
10639 char *cond_string;
10640
10641 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
10642 return NULL;
10643
10644 /* Skip the "if" keyword. */
10645 (*arg) += 2;
10646
10647 /* Skip any extra leading whitespace, and record the start of the
10648 condition string. */
10649 *arg = skip_spaces (*arg);
10650 cond_string = *arg;
10651
10652 /* Assume that the condition occupies the remainder of the arg
10653 string. */
10654 (*arg) += strlen (cond_string);
10655
10656 return cond_string;
10657 }
10658
10659 /* Commands to deal with catching events, such as signals, exceptions,
10660 process start/exit, etc. */
10661
10662 typedef enum
10663 {
10664 catch_fork_temporary, catch_vfork_temporary,
10665 catch_fork_permanent, catch_vfork_permanent
10666 }
10667 catch_fork_kind;
10668
10669 static void
10670 catch_fork_command_1 (char *arg, int from_tty,
10671 struct cmd_list_element *command)
10672 {
10673 struct gdbarch *gdbarch = get_current_arch ();
10674 char *cond_string = NULL;
10675 catch_fork_kind fork_kind;
10676 int tempflag;
10677
10678 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
10679 tempflag = (fork_kind == catch_fork_temporary
10680 || fork_kind == catch_vfork_temporary);
10681
10682 if (!arg)
10683 arg = "";
10684 arg = skip_spaces (arg);
10685
10686 /* The allowed syntax is:
10687 catch [v]fork
10688 catch [v]fork if <cond>
10689
10690 First, check if there's an if clause. */
10691 cond_string = ep_parse_optional_if_clause (&arg);
10692
10693 if ((*arg != '\0') && !isspace (*arg))
10694 error (_("Junk at end of arguments."));
10695
10696 /* If this target supports it, create a fork or vfork catchpoint
10697 and enable reporting of such events. */
10698 switch (fork_kind)
10699 {
10700 case catch_fork_temporary:
10701 case catch_fork_permanent:
10702 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
10703 &catch_fork_breakpoint_ops);
10704 break;
10705 case catch_vfork_temporary:
10706 case catch_vfork_permanent:
10707 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
10708 &catch_vfork_breakpoint_ops);
10709 break;
10710 default:
10711 error (_("unsupported or unknown fork kind; cannot catch it"));
10712 break;
10713 }
10714 }
10715
10716 static void
10717 catch_exec_command_1 (char *arg, int from_tty,
10718 struct cmd_list_element *command)
10719 {
10720 struct exec_catchpoint *c;
10721 struct gdbarch *gdbarch = get_current_arch ();
10722 int tempflag;
10723 char *cond_string = NULL;
10724
10725 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10726
10727 if (!arg)
10728 arg = "";
10729 arg = skip_spaces (arg);
10730
10731 /* The allowed syntax is:
10732 catch exec
10733 catch exec if <cond>
10734
10735 First, check if there's an if clause. */
10736 cond_string = ep_parse_optional_if_clause (&arg);
10737
10738 if ((*arg != '\0') && !isspace (*arg))
10739 error (_("Junk at end of arguments."));
10740
10741 c = XNEW (struct exec_catchpoint);
10742 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
10743 &catch_exec_breakpoint_ops);
10744 c->exec_pathname = NULL;
10745
10746 install_breakpoint (0, &c->base, 1);
10747 }
10748
10749 static enum print_stop_action
10750 print_it_exception_catchpoint (bpstat bs)
10751 {
10752 struct ui_out *uiout = current_uiout;
10753 struct breakpoint *b = bs->breakpoint_at;
10754 int bp_temp, bp_throw;
10755
10756 annotate_catchpoint (b->number);
10757
10758 bp_throw = strstr (b->addr_string, "throw") != NULL;
10759 if (b->loc->address != b->loc->requested_address)
10760 breakpoint_adjustment_warning (b->loc->requested_address,
10761 b->loc->address,
10762 b->number, 1);
10763 bp_temp = b->disposition == disp_del;
10764 ui_out_text (uiout,
10765 bp_temp ? "Temporary catchpoint "
10766 : "Catchpoint ");
10767 if (!ui_out_is_mi_like_p (uiout))
10768 ui_out_field_int (uiout, "bkptno", b->number);
10769 ui_out_text (uiout,
10770 bp_throw ? " (exception thrown), "
10771 : " (exception caught), ");
10772 if (ui_out_is_mi_like_p (uiout))
10773 {
10774 ui_out_field_string (uiout, "reason",
10775 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10776 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10777 ui_out_field_int (uiout, "bkptno", b->number);
10778 }
10779 return PRINT_SRC_AND_LOC;
10780 }
10781
10782 static void
10783 print_one_exception_catchpoint (struct breakpoint *b,
10784 struct bp_location **last_loc)
10785 {
10786 struct value_print_options opts;
10787 struct ui_out *uiout = current_uiout;
10788
10789 get_user_print_options (&opts);
10790 if (opts.addressprint)
10791 {
10792 annotate_field (4);
10793 if (b->loc == NULL || b->loc->shlib_disabled)
10794 ui_out_field_string (uiout, "addr", "<PENDING>");
10795 else
10796 ui_out_field_core_addr (uiout, "addr",
10797 b->loc->gdbarch, b->loc->address);
10798 }
10799 annotate_field (5);
10800 if (b->loc)
10801 *last_loc = b->loc;
10802 if (strstr (b->addr_string, "throw") != NULL)
10803 ui_out_field_string (uiout, "what", "exception throw");
10804 else
10805 ui_out_field_string (uiout, "what", "exception catch");
10806 }
10807
10808 static void
10809 print_mention_exception_catchpoint (struct breakpoint *b)
10810 {
10811 struct ui_out *uiout = current_uiout;
10812 int bp_temp;
10813 int bp_throw;
10814
10815 bp_temp = b->disposition == disp_del;
10816 bp_throw = strstr (b->addr_string, "throw") != NULL;
10817 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
10818 : _("Catchpoint "));
10819 ui_out_field_int (uiout, "bkptno", b->number);
10820 ui_out_text (uiout, bp_throw ? _(" (throw)")
10821 : _(" (catch)"));
10822 }
10823
10824 /* Implement the "print_recreate" breakpoint_ops method for throw and
10825 catch catchpoints. */
10826
10827 static void
10828 print_recreate_exception_catchpoint (struct breakpoint *b,
10829 struct ui_file *fp)
10830 {
10831 int bp_temp;
10832 int bp_throw;
10833
10834 bp_temp = b->disposition == disp_del;
10835 bp_throw = strstr (b->addr_string, "throw") != NULL;
10836 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
10837 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
10838 print_recreate_thread (b, fp);
10839 }
10840
10841 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
10842
10843 static int
10844 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
10845 enum exception_event_kind ex_event, int from_tty)
10846 {
10847 char *trigger_func_name;
10848
10849 if (ex_event == EX_EVENT_CATCH)
10850 trigger_func_name = "__cxa_begin_catch";
10851 else
10852 trigger_func_name = "__cxa_throw";
10853
10854 create_breakpoint (get_current_arch (),
10855 trigger_func_name, cond_string, -1,
10856 0 /* condition and thread are valid. */,
10857 tempflag, bp_breakpoint,
10858 0,
10859 AUTO_BOOLEAN_TRUE /* pending */,
10860 &gnu_v3_exception_catchpoint_ops, from_tty,
10861 1 /* enabled */,
10862 0 /* internal */,
10863 0);
10864
10865 return 1;
10866 }
10867
10868 /* Deal with "catch catch" and "catch throw" commands. */
10869
10870 static void
10871 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
10872 int tempflag, int from_tty)
10873 {
10874 char *cond_string = NULL;
10875
10876 if (!arg)
10877 arg = "";
10878 arg = skip_spaces (arg);
10879
10880 cond_string = ep_parse_optional_if_clause (&arg);
10881
10882 if ((*arg != '\0') && !isspace (*arg))
10883 error (_("Junk at end of arguments."));
10884
10885 if (ex_event != EX_EVENT_THROW
10886 && ex_event != EX_EVENT_CATCH)
10887 error (_("Unsupported or unknown exception event; cannot catch it"));
10888
10889 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
10890 return;
10891
10892 warning (_("Unsupported with this platform/compiler combination."));
10893 }
10894
10895 /* Implementation of "catch catch" command. */
10896
10897 static void
10898 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
10899 {
10900 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10901
10902 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
10903 }
10904
10905 /* Implementation of "catch throw" command. */
10906
10907 static void
10908 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
10909 {
10910 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10911
10912 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
10913 }
10914
10915 void
10916 init_ada_exception_breakpoint (struct breakpoint *b,
10917 struct gdbarch *gdbarch,
10918 struct symtab_and_line sal,
10919 char *addr_string,
10920 const struct breakpoint_ops *ops,
10921 int tempflag,
10922 int from_tty)
10923 {
10924 if (from_tty)
10925 {
10926 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10927 if (!loc_gdbarch)
10928 loc_gdbarch = gdbarch;
10929
10930 describe_other_breakpoints (loc_gdbarch,
10931 sal.pspace, sal.pc, sal.section, -1);
10932 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10933 version for exception catchpoints, because two catchpoints
10934 used for different exception names will use the same address.
10935 In this case, a "breakpoint ... also set at..." warning is
10936 unproductive. Besides, the warning phrasing is also a bit
10937 inappropriate, we should use the word catchpoint, and tell
10938 the user what type of catchpoint it is. The above is good
10939 enough for now, though. */
10940 }
10941
10942 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
10943
10944 b->enable_state = bp_enabled;
10945 b->disposition = tempflag ? disp_del : disp_donttouch;
10946 b->addr_string = addr_string;
10947 b->language = language_ada;
10948 }
10949
10950 /* Splits the argument using space as delimiter. Returns an xmalloc'd
10951 filter list, or NULL if no filtering is required. */
10952 static VEC(int) *
10953 catch_syscall_split_args (char *arg)
10954 {
10955 VEC(int) *result = NULL;
10956 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
10957
10958 while (*arg != '\0')
10959 {
10960 int i, syscall_number;
10961 char *endptr;
10962 char cur_name[128];
10963 struct syscall s;
10964
10965 /* Skip whitespace. */
10966 while (isspace (*arg))
10967 arg++;
10968
10969 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
10970 cur_name[i] = arg[i];
10971 cur_name[i] = '\0';
10972 arg += i;
10973
10974 /* Check if the user provided a syscall name or a number. */
10975 syscall_number = (int) strtol (cur_name, &endptr, 0);
10976 if (*endptr == '\0')
10977 get_syscall_by_number (syscall_number, &s);
10978 else
10979 {
10980 /* We have a name. Let's check if it's valid and convert it
10981 to a number. */
10982 get_syscall_by_name (cur_name, &s);
10983
10984 if (s.number == UNKNOWN_SYSCALL)
10985 /* Here we have to issue an error instead of a warning,
10986 because GDB cannot do anything useful if there's no
10987 syscall number to be caught. */
10988 error (_("Unknown syscall name '%s'."), cur_name);
10989 }
10990
10991 /* Ok, it's valid. */
10992 VEC_safe_push (int, result, s.number);
10993 }
10994
10995 discard_cleanups (cleanup);
10996 return result;
10997 }
10998
10999 /* Implement the "catch syscall" command. */
11000
11001 static void
11002 catch_syscall_command_1 (char *arg, int from_tty,
11003 struct cmd_list_element *command)
11004 {
11005 int tempflag;
11006 VEC(int) *filter;
11007 struct syscall s;
11008 struct gdbarch *gdbarch = get_current_arch ();
11009
11010 /* Checking if the feature if supported. */
11011 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11012 error (_("The feature 'catch syscall' is not supported on \
11013 this architecture yet."));
11014
11015 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11016
11017 arg = skip_spaces (arg);
11018
11019 /* We need to do this first "dummy" translation in order
11020 to get the syscall XML file loaded or, most important,
11021 to display a warning to the user if there's no XML file
11022 for his/her architecture. */
11023 get_syscall_by_number (0, &s);
11024
11025 /* The allowed syntax is:
11026 catch syscall
11027 catch syscall <name | number> [<name | number> ... <name | number>]
11028
11029 Let's check if there's a syscall name. */
11030
11031 if (arg != NULL)
11032 filter = catch_syscall_split_args (arg);
11033 else
11034 filter = NULL;
11035
11036 create_syscall_event_catchpoint (tempflag, filter,
11037 &catch_syscall_breakpoint_ops);
11038 }
11039
11040 static void
11041 catch_command (char *arg, int from_tty)
11042 {
11043 error (_("Catch requires an event name."));
11044 }
11045 \f
11046
11047 static void
11048 tcatch_command (char *arg, int from_tty)
11049 {
11050 error (_("Catch requires an event name."));
11051 }
11052
11053 /* A qsort comparison function that sorts breakpoints in order. */
11054
11055 static int
11056 compare_breakpoints (const void *a, const void *b)
11057 {
11058 const breakpoint_p *ba = a;
11059 uintptr_t ua = (uintptr_t) *ba;
11060 const breakpoint_p *bb = b;
11061 uintptr_t ub = (uintptr_t) *bb;
11062
11063 if ((*ba)->number < (*bb)->number)
11064 return -1;
11065 else if ((*ba)->number > (*bb)->number)
11066 return 1;
11067
11068 /* Now sort by address, in case we see, e..g, two breakpoints with
11069 the number 0. */
11070 if (ua < ub)
11071 return -1;
11072 return ub > ub ? 1 : 0;
11073 }
11074
11075 /* Delete breakpoints by address or line. */
11076
11077 static void
11078 clear_command (char *arg, int from_tty)
11079 {
11080 struct breakpoint *b, *prev;
11081 VEC(breakpoint_p) *found = 0;
11082 int ix;
11083 int default_match;
11084 struct symtabs_and_lines sals;
11085 struct symtab_and_line sal;
11086 int i;
11087 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11088
11089 if (arg)
11090 {
11091 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
11092 | DECODE_LINE_LIST_MODE));
11093 default_match = 0;
11094 }
11095 else
11096 {
11097 sals.sals = (struct symtab_and_line *)
11098 xmalloc (sizeof (struct symtab_and_line));
11099 make_cleanup (xfree, sals.sals);
11100 init_sal (&sal); /* Initialize to zeroes. */
11101
11102 /* Set sal's line, symtab, pc, and pspace to the values
11103 corresponding to the last call to print_frame_info. If the
11104 codepoint is not valid, this will set all the fields to 0. */
11105 get_last_displayed_sal (&sal);
11106 if (sal.symtab == 0)
11107 error (_("No source file specified."));
11108
11109 sals.sals[0] = sal;
11110 sals.nelts = 1;
11111
11112 default_match = 1;
11113 }
11114
11115 /* We don't call resolve_sal_pc here. That's not as bad as it
11116 seems, because all existing breakpoints typically have both
11117 file/line and pc set. So, if clear is given file/line, we can
11118 match this to existing breakpoint without obtaining pc at all.
11119
11120 We only support clearing given the address explicitly
11121 present in breakpoint table. Say, we've set breakpoint
11122 at file:line. There were several PC values for that file:line,
11123 due to optimization, all in one block.
11124
11125 We've picked one PC value. If "clear" is issued with another
11126 PC corresponding to the same file:line, the breakpoint won't
11127 be cleared. We probably can still clear the breakpoint, but
11128 since the other PC value is never presented to user, user
11129 can only find it by guessing, and it does not seem important
11130 to support that. */
11131
11132 /* For each line spec given, delete bps which correspond to it. Do
11133 it in two passes, solely to preserve the current behavior that
11134 from_tty is forced true if we delete more than one
11135 breakpoint. */
11136
11137 found = NULL;
11138 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11139 for (i = 0; i < sals.nelts; i++)
11140 {
11141 int is_abs, sal_name_len;
11142
11143 /* If exact pc given, clear bpts at that pc.
11144 If line given (pc == 0), clear all bpts on specified line.
11145 If defaulting, clear all bpts on default line
11146 or at default pc.
11147
11148 defaulting sal.pc != 0 tests to do
11149
11150 0 1 pc
11151 1 1 pc _and_ line
11152 0 0 line
11153 1 0 <can't happen> */
11154
11155 sal = sals.sals[i];
11156 is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11157 sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
11158
11159 /* Find all matching breakpoints and add them to 'found'. */
11160 ALL_BREAKPOINTS (b)
11161 {
11162 int match = 0;
11163 /* Are we going to delete b? */
11164 if (b->type != bp_none && !is_watchpoint (b))
11165 {
11166 struct bp_location *loc = b->loc;
11167 for (; loc; loc = loc->next)
11168 {
11169 /* If the user specified file:line, don't allow a PC
11170 match. This matches historical gdb behavior. */
11171 int pc_match = (!sal.explicit_line
11172 && sal.pc
11173 && (loc->pspace == sal.pspace)
11174 && (loc->address == sal.pc)
11175 && (!section_is_overlay (loc->section)
11176 || loc->section == sal.section));
11177 int line_match = 0;
11178
11179 if ((default_match || sal.explicit_line)
11180 && loc->source_file != NULL
11181 && sal.symtab != NULL
11182 && sal.pspace == loc->pspace
11183 && loc->line_number == sal.line)
11184 {
11185 if (filename_cmp (loc->source_file,
11186 sal.symtab->filename) == 0)
11187 line_match = 1;
11188 else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11189 && compare_filenames_for_search (loc->source_file,
11190 sal.symtab->filename,
11191 sal_name_len))
11192 line_match = 1;
11193 }
11194
11195 if (pc_match || line_match)
11196 {
11197 match = 1;
11198 break;
11199 }
11200 }
11201 }
11202
11203 if (match)
11204 VEC_safe_push(breakpoint_p, found, b);
11205 }
11206 }
11207
11208 /* Now go thru the 'found' chain and delete them. */
11209 if (VEC_empty(breakpoint_p, found))
11210 {
11211 if (arg)
11212 error (_("No breakpoint at %s."), arg);
11213 else
11214 error (_("No breakpoint at this line."));
11215 }
11216
11217 /* Remove duplicates from the vec. */
11218 qsort (VEC_address (breakpoint_p, found),
11219 VEC_length (breakpoint_p, found),
11220 sizeof (breakpoint_p),
11221 compare_breakpoints);
11222 prev = VEC_index (breakpoint_p, found, 0);
11223 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11224 {
11225 if (b == prev)
11226 {
11227 VEC_ordered_remove (breakpoint_p, found, ix);
11228 --ix;
11229 }
11230 }
11231
11232 if (VEC_length(breakpoint_p, found) > 1)
11233 from_tty = 1; /* Always report if deleted more than one. */
11234 if (from_tty)
11235 {
11236 if (VEC_length(breakpoint_p, found) == 1)
11237 printf_unfiltered (_("Deleted breakpoint "));
11238 else
11239 printf_unfiltered (_("Deleted breakpoints "));
11240 }
11241 breakpoints_changed ();
11242
11243 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11244 {
11245 if (from_tty)
11246 printf_unfiltered ("%d ", b->number);
11247 delete_breakpoint (b);
11248 }
11249 if (from_tty)
11250 putchar_unfiltered ('\n');
11251
11252 do_cleanups (cleanups);
11253 }
11254 \f
11255 /* Delete breakpoint in BS if they are `delete' breakpoints and
11256 all breakpoints that are marked for deletion, whether hit or not.
11257 This is called after any breakpoint is hit, or after errors. */
11258
11259 void
11260 breakpoint_auto_delete (bpstat bs)
11261 {
11262 struct breakpoint *b, *b_tmp;
11263
11264 for (; bs; bs = bs->next)
11265 if (bs->breakpoint_at
11266 && bs->breakpoint_at->disposition == disp_del
11267 && bs->stop)
11268 delete_breakpoint (bs->breakpoint_at);
11269
11270 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11271 {
11272 if (b->disposition == disp_del_at_next_stop)
11273 delete_breakpoint (b);
11274 }
11275 }
11276
11277 /* A comparison function for bp_location AP and BP being interfaced to
11278 qsort. Sort elements primarily by their ADDRESS (no matter what
11279 does breakpoint_address_is_meaningful say for its OWNER),
11280 secondarily by ordering first bp_permanent OWNERed elements and
11281 terciarily just ensuring the array is sorted stable way despite
11282 qsort being an unstable algorithm. */
11283
11284 static int
11285 bp_location_compare (const void *ap, const void *bp)
11286 {
11287 struct bp_location *a = *(void **) ap;
11288 struct bp_location *b = *(void **) bp;
11289 /* A and B come from existing breakpoints having non-NULL OWNER. */
11290 int a_perm = a->owner->enable_state == bp_permanent;
11291 int b_perm = b->owner->enable_state == bp_permanent;
11292
11293 if (a->address != b->address)
11294 return (a->address > b->address) - (a->address < b->address);
11295
11296 /* Sort locations at the same address by their pspace number, keeping
11297 locations of the same inferior (in a multi-inferior environment)
11298 grouped. */
11299
11300 if (a->pspace->num != b->pspace->num)
11301 return ((a->pspace->num > b->pspace->num)
11302 - (a->pspace->num < b->pspace->num));
11303
11304 /* Sort permanent breakpoints first. */
11305 if (a_perm != b_perm)
11306 return (a_perm < b_perm) - (a_perm > b_perm);
11307
11308 /* Make the internal GDB representation stable across GDB runs
11309 where A and B memory inside GDB can differ. Breakpoint locations of
11310 the same type at the same address can be sorted in arbitrary order. */
11311
11312 if (a->owner->number != b->owner->number)
11313 return ((a->owner->number > b->owner->number)
11314 - (a->owner->number < b->owner->number));
11315
11316 return (a > b) - (a < b);
11317 }
11318
11319 /* Set bp_location_placed_address_before_address_max and
11320 bp_location_shadow_len_after_address_max according to the current
11321 content of the bp_location array. */
11322
11323 static void
11324 bp_location_target_extensions_update (void)
11325 {
11326 struct bp_location *bl, **blp_tmp;
11327
11328 bp_location_placed_address_before_address_max = 0;
11329 bp_location_shadow_len_after_address_max = 0;
11330
11331 ALL_BP_LOCATIONS (bl, blp_tmp)
11332 {
11333 CORE_ADDR start, end, addr;
11334
11335 if (!bp_location_has_shadow (bl))
11336 continue;
11337
11338 start = bl->target_info.placed_address;
11339 end = start + bl->target_info.shadow_len;
11340
11341 gdb_assert (bl->address >= start);
11342 addr = bl->address - start;
11343 if (addr > bp_location_placed_address_before_address_max)
11344 bp_location_placed_address_before_address_max = addr;
11345
11346 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11347
11348 gdb_assert (bl->address < end);
11349 addr = end - bl->address;
11350 if (addr > bp_location_shadow_len_after_address_max)
11351 bp_location_shadow_len_after_address_max = addr;
11352 }
11353 }
11354
11355 /* Download tracepoint locations if they haven't been. */
11356
11357 static void
11358 download_tracepoint_locations (void)
11359 {
11360 struct bp_location *bl, **blp_tmp;
11361 struct cleanup *old_chain;
11362
11363 if (!target_can_download_tracepoint ())
11364 return;
11365
11366 old_chain = save_current_space_and_thread ();
11367
11368 ALL_BP_LOCATIONS (bl, blp_tmp)
11369 {
11370 struct tracepoint *t;
11371
11372 if (!is_tracepoint (bl->owner))
11373 continue;
11374
11375 if ((bl->owner->type == bp_fast_tracepoint
11376 ? !may_insert_fast_tracepoints
11377 : !may_insert_tracepoints))
11378 continue;
11379
11380 /* In tracepoint, locations are _never_ duplicated, so
11381 should_be_inserted is equivalent to
11382 unduplicated_should_be_inserted. */
11383 if (!should_be_inserted (bl) || bl->inserted)
11384 continue;
11385
11386 switch_to_program_space_and_thread (bl->pspace);
11387
11388 target_download_tracepoint (bl);
11389
11390 bl->inserted = 1;
11391 t = (struct tracepoint *) bl->owner;
11392 t->number_on_target = bl->owner->number;
11393 }
11394
11395 do_cleanups (old_chain);
11396 }
11397
11398 /* Swap the insertion/duplication state between two locations. */
11399
11400 static void
11401 swap_insertion (struct bp_location *left, struct bp_location *right)
11402 {
11403 const int left_inserted = left->inserted;
11404 const int left_duplicate = left->duplicate;
11405 const int left_needs_update = left->needs_update;
11406 const struct bp_target_info left_target_info = left->target_info;
11407
11408 /* Locations of tracepoints can never be duplicated. */
11409 if (is_tracepoint (left->owner))
11410 gdb_assert (!left->duplicate);
11411 if (is_tracepoint (right->owner))
11412 gdb_assert (!right->duplicate);
11413
11414 left->inserted = right->inserted;
11415 left->duplicate = right->duplicate;
11416 left->needs_update = right->needs_update;
11417 left->target_info = right->target_info;
11418 right->inserted = left_inserted;
11419 right->duplicate = left_duplicate;
11420 right->needs_update = left_needs_update;
11421 right->target_info = left_target_info;
11422 }
11423
11424 /* Force the re-insertion of the locations at ADDRESS. This is called
11425 once a new/deleted/modified duplicate location is found and we are evaluating
11426 conditions on the target's side. Such conditions need to be updated on
11427 the target. */
11428
11429 static void
11430 force_breakpoint_reinsertion (struct bp_location *bl)
11431 {
11432 struct bp_location **locp = NULL, **loc2p;
11433 struct bp_location *loc;
11434 CORE_ADDR address = 0;
11435 int pspace_num;
11436
11437 address = bl->address;
11438 pspace_num = bl->pspace->num;
11439
11440 /* This is only meaningful if the target is
11441 evaluating conditions and if the user has
11442 opted for condition evaluation on the target's
11443 side. */
11444 if (gdb_evaluates_breakpoint_condition_p ()
11445 || !target_supports_evaluation_of_breakpoint_conditions ())
11446 return;
11447
11448 /* Flag all breakpoint locations with this address and
11449 the same program space as the location
11450 as "its condition has changed". We need to
11451 update the conditions on the target's side. */
11452 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11453 {
11454 loc = *loc2p;
11455
11456 if (!is_breakpoint (loc->owner)
11457 || pspace_num != loc->pspace->num)
11458 continue;
11459
11460 /* Flag the location appropriately. We use a different state to
11461 let everyone know that we already updated the set of locations
11462 with addr bl->address and program space bl->pspace. This is so
11463 we don't have to keep calling these functions just to mark locations
11464 that have already been marked. */
11465 loc->condition_changed = condition_updated;
11466
11467 /* Free the agent expression bytecode as well. We will compute
11468 it later on. */
11469 if (loc->cond_bytecode)
11470 {
11471 free_agent_expr (loc->cond_bytecode);
11472 loc->cond_bytecode = NULL;
11473 }
11474 }
11475 }
11476
11477 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
11478 into the inferior, only remove already-inserted locations that no
11479 longer should be inserted. Functions that delete a breakpoint or
11480 breakpoints should pass false, so that deleting a breakpoint
11481 doesn't have the side effect of inserting the locations of other
11482 breakpoints that are marked not-inserted, but should_be_inserted
11483 returns true on them.
11484
11485 This behaviour is useful is situations close to tear-down -- e.g.,
11486 after an exec, while the target still has execution, but breakpoint
11487 shadows of the previous executable image should *NOT* be restored
11488 to the new image; or before detaching, where the target still has
11489 execution and wants to delete breakpoints from GDB's lists, and all
11490 breakpoints had already been removed from the inferior. */
11491
11492 static void
11493 update_global_location_list (int should_insert)
11494 {
11495 struct breakpoint *b;
11496 struct bp_location **locp, *loc;
11497 struct cleanup *cleanups;
11498 /* Last breakpoint location address that was marked for update. */
11499 CORE_ADDR last_addr = 0;
11500 /* Last breakpoint location program space that was marked for update. */
11501 int last_pspace_num = -1;
11502
11503 /* Used in the duplicates detection below. When iterating over all
11504 bp_locations, points to the first bp_location of a given address.
11505 Breakpoints and watchpoints of different types are never
11506 duplicates of each other. Keep one pointer for each type of
11507 breakpoint/watchpoint, so we only need to loop over all locations
11508 once. */
11509 struct bp_location *bp_loc_first; /* breakpoint */
11510 struct bp_location *wp_loc_first; /* hardware watchpoint */
11511 struct bp_location *awp_loc_first; /* access watchpoint */
11512 struct bp_location *rwp_loc_first; /* read watchpoint */
11513
11514 /* Saved former bp_location array which we compare against the newly
11515 built bp_location from the current state of ALL_BREAKPOINTS. */
11516 struct bp_location **old_location, **old_locp;
11517 unsigned old_location_count;
11518
11519 old_location = bp_location;
11520 old_location_count = bp_location_count;
11521 bp_location = NULL;
11522 bp_location_count = 0;
11523 cleanups = make_cleanup (xfree, old_location);
11524
11525 ALL_BREAKPOINTS (b)
11526 for (loc = b->loc; loc; loc = loc->next)
11527 bp_location_count++;
11528
11529 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11530 locp = bp_location;
11531 ALL_BREAKPOINTS (b)
11532 for (loc = b->loc; loc; loc = loc->next)
11533 *locp++ = loc;
11534 qsort (bp_location, bp_location_count, sizeof (*bp_location),
11535 bp_location_compare);
11536
11537 bp_location_target_extensions_update ();
11538
11539 /* Identify bp_location instances that are no longer present in the
11540 new list, and therefore should be freed. Note that it's not
11541 necessary that those locations should be removed from inferior --
11542 if there's another location at the same address (previously
11543 marked as duplicate), we don't need to remove/insert the
11544 location.
11545
11546 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11547 and former bp_location array state respectively. */
11548
11549 locp = bp_location;
11550 for (old_locp = old_location; old_locp < old_location + old_location_count;
11551 old_locp++)
11552 {
11553 struct bp_location *old_loc = *old_locp;
11554 struct bp_location **loc2p;
11555
11556 /* Tells if 'old_loc' is found among the new locations. If
11557 not, we have to free it. */
11558 int found_object = 0;
11559 /* Tells if the location should remain inserted in the target. */
11560 int keep_in_target = 0;
11561 int removed = 0;
11562
11563 /* Skip LOCP entries which will definitely never be needed.
11564 Stop either at or being the one matching OLD_LOC. */
11565 while (locp < bp_location + bp_location_count
11566 && (*locp)->address < old_loc->address)
11567 locp++;
11568
11569 for (loc2p = locp;
11570 (loc2p < bp_location + bp_location_count
11571 && (*loc2p)->address == old_loc->address);
11572 loc2p++)
11573 {
11574 /* Check if this is a new/duplicated location or a duplicated
11575 location that had its condition modified. If so, we want to send
11576 its condition to the target if evaluation of conditions is taking
11577 place there. */
11578 if ((*loc2p)->condition_changed == condition_modified
11579 && (last_addr != old_loc->address
11580 || last_pspace_num != old_loc->pspace->num))
11581 {
11582 force_breakpoint_reinsertion (*loc2p);
11583 last_pspace_num = old_loc->pspace->num;
11584 }
11585
11586 if (*loc2p == old_loc)
11587 found_object = 1;
11588 }
11589
11590 /* We have already handled this address, update it so that we don't
11591 have to go through updates again. */
11592 last_addr = old_loc->address;
11593
11594 /* Target-side condition evaluation: Handle deleted locations. */
11595 if (!found_object)
11596 force_breakpoint_reinsertion (old_loc);
11597
11598 /* If this location is no longer present, and inserted, look if
11599 there's maybe a new location at the same address. If so,
11600 mark that one inserted, and don't remove this one. This is
11601 needed so that we don't have a time window where a breakpoint
11602 at certain location is not inserted. */
11603
11604 if (old_loc->inserted)
11605 {
11606 /* If the location is inserted now, we might have to remove
11607 it. */
11608
11609 if (found_object && should_be_inserted (old_loc))
11610 {
11611 /* The location is still present in the location list,
11612 and still should be inserted. Don't do anything. */
11613 keep_in_target = 1;
11614 }
11615 else
11616 {
11617 /* This location still exists, but it won't be kept in the
11618 target since it may have been disabled. We proceed to
11619 remove its target-side condition. */
11620
11621 /* The location is either no longer present, or got
11622 disabled. See if there's another location at the
11623 same address, in which case we don't need to remove
11624 this one from the target. */
11625
11626 /* OLD_LOC comes from existing struct breakpoint. */
11627 if (breakpoint_address_is_meaningful (old_loc->owner))
11628 {
11629 for (loc2p = locp;
11630 (loc2p < bp_location + bp_location_count
11631 && (*loc2p)->address == old_loc->address);
11632 loc2p++)
11633 {
11634 struct bp_location *loc2 = *loc2p;
11635
11636 if (breakpoint_locations_match (loc2, old_loc))
11637 {
11638 /* Read watchpoint locations are switched to
11639 access watchpoints, if the former are not
11640 supported, but the latter are. */
11641 if (is_hardware_watchpoint (old_loc->owner))
11642 {
11643 gdb_assert (is_hardware_watchpoint (loc2->owner));
11644 loc2->watchpoint_type = old_loc->watchpoint_type;
11645 }
11646
11647 /* loc2 is a duplicated location. We need to check
11648 if it should be inserted in case it will be
11649 unduplicated. */
11650 if (loc2 != old_loc
11651 && unduplicated_should_be_inserted (loc2))
11652 {
11653 swap_insertion (old_loc, loc2);
11654 keep_in_target = 1;
11655 break;
11656 }
11657 }
11658 }
11659 }
11660 }
11661
11662 if (!keep_in_target)
11663 {
11664 if (remove_breakpoint (old_loc, mark_uninserted))
11665 {
11666 /* This is just about all we can do. We could keep
11667 this location on the global list, and try to
11668 remove it next time, but there's no particular
11669 reason why we will succeed next time.
11670
11671 Note that at this point, old_loc->owner is still
11672 valid, as delete_breakpoint frees the breakpoint
11673 only after calling us. */
11674 printf_filtered (_("warning: Error removing "
11675 "breakpoint %d\n"),
11676 old_loc->owner->number);
11677 }
11678 removed = 1;
11679 }
11680 }
11681
11682 if (!found_object)
11683 {
11684 if (removed && non_stop
11685 && breakpoint_address_is_meaningful (old_loc->owner)
11686 && !is_hardware_watchpoint (old_loc->owner))
11687 {
11688 /* This location was removed from the target. In
11689 non-stop mode, a race condition is possible where
11690 we've removed a breakpoint, but stop events for that
11691 breakpoint are already queued and will arrive later.
11692 We apply an heuristic to be able to distinguish such
11693 SIGTRAPs from other random SIGTRAPs: we keep this
11694 breakpoint location for a bit, and will retire it
11695 after we see some number of events. The theory here
11696 is that reporting of events should, "on the average",
11697 be fair, so after a while we'll see events from all
11698 threads that have anything of interest, and no longer
11699 need to keep this breakpoint location around. We
11700 don't hold locations forever so to reduce chances of
11701 mistaking a non-breakpoint SIGTRAP for a breakpoint
11702 SIGTRAP.
11703
11704 The heuristic failing can be disastrous on
11705 decr_pc_after_break targets.
11706
11707 On decr_pc_after_break targets, like e.g., x86-linux,
11708 if we fail to recognize a late breakpoint SIGTRAP,
11709 because events_till_retirement has reached 0 too
11710 soon, we'll fail to do the PC adjustment, and report
11711 a random SIGTRAP to the user. When the user resumes
11712 the inferior, it will most likely immediately crash
11713 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11714 corrupted, because of being resumed e.g., in the
11715 middle of a multi-byte instruction, or skipped a
11716 one-byte instruction. This was actually seen happen
11717 on native x86-linux, and should be less rare on
11718 targets that do not support new thread events, like
11719 remote, due to the heuristic depending on
11720 thread_count.
11721
11722 Mistaking a random SIGTRAP for a breakpoint trap
11723 causes similar symptoms (PC adjustment applied when
11724 it shouldn't), but then again, playing with SIGTRAPs
11725 behind the debugger's back is asking for trouble.
11726
11727 Since hardware watchpoint traps are always
11728 distinguishable from other traps, so we don't need to
11729 apply keep hardware watchpoint moribund locations
11730 around. We simply always ignore hardware watchpoint
11731 traps we can no longer explain. */
11732
11733 old_loc->events_till_retirement = 3 * (thread_count () + 1);
11734 old_loc->owner = NULL;
11735
11736 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
11737 }
11738 else
11739 {
11740 old_loc->owner = NULL;
11741 decref_bp_location (&old_loc);
11742 }
11743 }
11744 }
11745
11746 /* Rescan breakpoints at the same address and section, marking the
11747 first one as "first" and any others as "duplicates". This is so
11748 that the bpt instruction is only inserted once. If we have a
11749 permanent breakpoint at the same place as BPT, make that one the
11750 official one, and the rest as duplicates. Permanent breakpoints
11751 are sorted first for the same address.
11752
11753 Do the same for hardware watchpoints, but also considering the
11754 watchpoint's type (regular/access/read) and length. */
11755
11756 bp_loc_first = NULL;
11757 wp_loc_first = NULL;
11758 awp_loc_first = NULL;
11759 rwp_loc_first = NULL;
11760 ALL_BP_LOCATIONS (loc, locp)
11761 {
11762 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11763 non-NULL. */
11764 struct bp_location **loc_first_p;
11765 b = loc->owner;
11766
11767 if (!should_be_inserted (loc)
11768 || !breakpoint_address_is_meaningful (b)
11769 /* Don't detect duplicate for tracepoint locations because they are
11770 never duplicated. See the comments in field `duplicate' of
11771 `struct bp_location'. */
11772 || is_tracepoint (b))
11773 {
11774 /* Clear the condition modification flag. */
11775 loc->condition_changed = condition_unchanged;
11776 continue;
11777 }
11778
11779 /* Permanent breakpoint should always be inserted. */
11780 if (b->enable_state == bp_permanent && ! loc->inserted)
11781 internal_error (__FILE__, __LINE__,
11782 _("allegedly permanent breakpoint is not "
11783 "actually inserted"));
11784
11785 if (b->type == bp_hardware_watchpoint)
11786 loc_first_p = &wp_loc_first;
11787 else if (b->type == bp_read_watchpoint)
11788 loc_first_p = &rwp_loc_first;
11789 else if (b->type == bp_access_watchpoint)
11790 loc_first_p = &awp_loc_first;
11791 else
11792 loc_first_p = &bp_loc_first;
11793
11794 if (*loc_first_p == NULL
11795 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11796 || !breakpoint_locations_match (loc, *loc_first_p))
11797 {
11798 *loc_first_p = loc;
11799 loc->duplicate = 0;
11800
11801 if (is_breakpoint (loc->owner) && loc->condition_changed)
11802 {
11803 loc->needs_update = 1;
11804 /* Clear the condition modification flag. */
11805 loc->condition_changed = condition_unchanged;
11806 }
11807 continue;
11808 }
11809
11810
11811 /* This and the above ensure the invariant that the first location
11812 is not duplicated, and is the inserted one.
11813 All following are marked as duplicated, and are not inserted. */
11814 if (loc->inserted)
11815 swap_insertion (loc, *loc_first_p);
11816 loc->duplicate = 1;
11817
11818 /* Clear the condition modification flag. */
11819 loc->condition_changed = condition_unchanged;
11820
11821 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
11822 && b->enable_state != bp_permanent)
11823 internal_error (__FILE__, __LINE__,
11824 _("another breakpoint was inserted on top of "
11825 "a permanent breakpoint"));
11826 }
11827
11828 if (breakpoints_always_inserted_mode ()
11829 && (have_live_inferiors ()
11830 || (gdbarch_has_global_breakpoints (target_gdbarch))))
11831 {
11832 if (should_insert)
11833 insert_breakpoint_locations ();
11834 else
11835 {
11836 /* Though should_insert is false, we may need to update conditions
11837 on the target's side if it is evaluating such conditions. We
11838 only update conditions for locations that are marked
11839 "needs_update". */
11840 update_inserted_breakpoint_locations ();
11841 }
11842 }
11843
11844 if (should_insert)
11845 download_tracepoint_locations ();
11846
11847 do_cleanups (cleanups);
11848 }
11849
11850 void
11851 breakpoint_retire_moribund (void)
11852 {
11853 struct bp_location *loc;
11854 int ix;
11855
11856 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
11857 if (--(loc->events_till_retirement) == 0)
11858 {
11859 decref_bp_location (&loc);
11860 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
11861 --ix;
11862 }
11863 }
11864
11865 static void
11866 update_global_location_list_nothrow (int inserting)
11867 {
11868 volatile struct gdb_exception e;
11869
11870 TRY_CATCH (e, RETURN_MASK_ERROR)
11871 update_global_location_list (inserting);
11872 }
11873
11874 /* Clear BKP from a BPS. */
11875
11876 static void
11877 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
11878 {
11879 bpstat bs;
11880
11881 for (bs = bps; bs; bs = bs->next)
11882 if (bs->breakpoint_at == bpt)
11883 {
11884 bs->breakpoint_at = NULL;
11885 bs->old_val = NULL;
11886 /* bs->commands will be freed later. */
11887 }
11888 }
11889
11890 /* Callback for iterate_over_threads. */
11891 static int
11892 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11893 {
11894 struct breakpoint *bpt = data;
11895
11896 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11897 return 0;
11898 }
11899
11900 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
11901 callbacks. */
11902
11903 static void
11904 say_where (struct breakpoint *b)
11905 {
11906 struct ui_out *uiout = current_uiout;
11907 struct value_print_options opts;
11908
11909 get_user_print_options (&opts);
11910
11911 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11912 single string. */
11913 if (b->loc == NULL)
11914 {
11915 printf_filtered (_(" (%s) pending."), b->addr_string);
11916 }
11917 else
11918 {
11919 if (opts.addressprint || b->loc->source_file == NULL)
11920 {
11921 printf_filtered (" at ");
11922 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
11923 gdb_stdout);
11924 }
11925 if (b->loc->source_file)
11926 {
11927 /* If there is a single location, we can print the location
11928 more nicely. */
11929 if (b->loc->next == NULL)
11930 printf_filtered (": file %s, line %d.",
11931 b->loc->source_file, b->loc->line_number);
11932 else
11933 /* This is not ideal, but each location may have a
11934 different file name, and this at least reflects the
11935 real situation somewhat. */
11936 printf_filtered (": %s.", b->addr_string);
11937 }
11938
11939 if (b->loc->next)
11940 {
11941 struct bp_location *loc = b->loc;
11942 int n = 0;
11943 for (; loc; loc = loc->next)
11944 ++n;
11945 printf_filtered (" (%d locations)", n);
11946 }
11947 }
11948 }
11949
11950 /* Default bp_location_ops methods. */
11951
11952 static void
11953 bp_location_dtor (struct bp_location *self)
11954 {
11955 xfree (self->cond);
11956 if (self->cond_bytecode)
11957 free_agent_expr (self->cond_bytecode);
11958 xfree (self->function_name);
11959 xfree (self->source_file);
11960 }
11961
11962 static const struct bp_location_ops bp_location_ops =
11963 {
11964 bp_location_dtor
11965 };
11966
11967 /* Default breakpoint_ops methods all breakpoint_ops ultimately
11968 inherit from. */
11969
11970 static void
11971 base_breakpoint_dtor (struct breakpoint *self)
11972 {
11973 decref_counted_command_line (&self->commands);
11974 xfree (self->cond_string);
11975 xfree (self->addr_string);
11976 xfree (self->filter);
11977 xfree (self->addr_string_range_end);
11978 }
11979
11980 static struct bp_location *
11981 base_breakpoint_allocate_location (struct breakpoint *self)
11982 {
11983 struct bp_location *loc;
11984
11985 loc = XNEW (struct bp_location);
11986 init_bp_location (loc, &bp_location_ops, self);
11987 return loc;
11988 }
11989
11990 static void
11991 base_breakpoint_re_set (struct breakpoint *b)
11992 {
11993 /* Nothing to re-set. */
11994 }
11995
11996 #define internal_error_pure_virtual_called() \
11997 gdb_assert_not_reached ("pure virtual function called")
11998
11999 static int
12000 base_breakpoint_insert_location (struct bp_location *bl)
12001 {
12002 internal_error_pure_virtual_called ();
12003 }
12004
12005 static int
12006 base_breakpoint_remove_location (struct bp_location *bl)
12007 {
12008 internal_error_pure_virtual_called ();
12009 }
12010
12011 static int
12012 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12013 struct address_space *aspace,
12014 CORE_ADDR bp_addr,
12015 const struct target_waitstatus *ws)
12016 {
12017 internal_error_pure_virtual_called ();
12018 }
12019
12020 static void
12021 base_breakpoint_check_status (bpstat bs)
12022 {
12023 /* Always stop. */
12024 }
12025
12026 /* A "works_in_software_mode" breakpoint_ops method that just internal
12027 errors. */
12028
12029 static int
12030 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12031 {
12032 internal_error_pure_virtual_called ();
12033 }
12034
12035 /* A "resources_needed" breakpoint_ops method that just internal
12036 errors. */
12037
12038 static int
12039 base_breakpoint_resources_needed (const struct bp_location *bl)
12040 {
12041 internal_error_pure_virtual_called ();
12042 }
12043
12044 static enum print_stop_action
12045 base_breakpoint_print_it (bpstat bs)
12046 {
12047 internal_error_pure_virtual_called ();
12048 }
12049
12050 static void
12051 base_breakpoint_print_one_detail (const struct breakpoint *self,
12052 struct ui_out *uiout)
12053 {
12054 /* nothing */
12055 }
12056
12057 static void
12058 base_breakpoint_print_mention (struct breakpoint *b)
12059 {
12060 internal_error_pure_virtual_called ();
12061 }
12062
12063 static void
12064 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12065 {
12066 internal_error_pure_virtual_called ();
12067 }
12068
12069 static void
12070 base_breakpoint_create_sals_from_address (char **arg,
12071 struct linespec_result *canonical,
12072 enum bptype type_wanted,
12073 char *addr_start,
12074 char **copy_arg)
12075 {
12076 internal_error_pure_virtual_called ();
12077 }
12078
12079 static void
12080 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12081 struct linespec_result *c,
12082 struct linespec_sals *lsal,
12083 char *cond_string,
12084 enum bptype type_wanted,
12085 enum bpdisp disposition,
12086 int thread,
12087 int task, int ignore_count,
12088 const struct breakpoint_ops *o,
12089 int from_tty, int enabled,
12090 int internal, unsigned flags)
12091 {
12092 internal_error_pure_virtual_called ();
12093 }
12094
12095 static void
12096 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12097 struct symtabs_and_lines *sals)
12098 {
12099 internal_error_pure_virtual_called ();
12100 }
12101
12102 static struct breakpoint_ops base_breakpoint_ops =
12103 {
12104 base_breakpoint_dtor,
12105 base_breakpoint_allocate_location,
12106 base_breakpoint_re_set,
12107 base_breakpoint_insert_location,
12108 base_breakpoint_remove_location,
12109 base_breakpoint_breakpoint_hit,
12110 base_breakpoint_check_status,
12111 base_breakpoint_resources_needed,
12112 base_breakpoint_works_in_software_mode,
12113 base_breakpoint_print_it,
12114 NULL,
12115 base_breakpoint_print_one_detail,
12116 base_breakpoint_print_mention,
12117 base_breakpoint_print_recreate,
12118 base_breakpoint_create_sals_from_address,
12119 base_breakpoint_create_breakpoints_sal,
12120 base_breakpoint_decode_linespec,
12121 };
12122
12123 /* Default breakpoint_ops methods. */
12124
12125 static void
12126 bkpt_re_set (struct breakpoint *b)
12127 {
12128 /* FIXME: is this still reachable? */
12129 if (b->addr_string == NULL)
12130 {
12131 /* Anything without a string can't be re-set. */
12132 delete_breakpoint (b);
12133 return;
12134 }
12135
12136 breakpoint_re_set_default (b);
12137 }
12138
12139 static int
12140 bkpt_insert_location (struct bp_location *bl)
12141 {
12142 if (bl->loc_type == bp_loc_hardware_breakpoint)
12143 return target_insert_hw_breakpoint (bl->gdbarch,
12144 &bl->target_info);
12145 else
12146 return target_insert_breakpoint (bl->gdbarch,
12147 &bl->target_info);
12148 }
12149
12150 static int
12151 bkpt_remove_location (struct bp_location *bl)
12152 {
12153 if (bl->loc_type == bp_loc_hardware_breakpoint)
12154 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12155 else
12156 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12157 }
12158
12159 static int
12160 bkpt_breakpoint_hit (const struct bp_location *bl,
12161 struct address_space *aspace, CORE_ADDR bp_addr,
12162 const struct target_waitstatus *ws)
12163 {
12164 struct breakpoint *b = bl->owner;
12165
12166 if (ws->kind != TARGET_WAITKIND_STOPPED
12167 || ws->value.sig != TARGET_SIGNAL_TRAP)
12168 return 0;
12169
12170 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12171 aspace, bp_addr))
12172 return 0;
12173
12174 if (overlay_debugging /* unmapped overlay section */
12175 && section_is_overlay (bl->section)
12176 && !section_is_mapped (bl->section))
12177 return 0;
12178
12179 return 1;
12180 }
12181
12182 static int
12183 bkpt_resources_needed (const struct bp_location *bl)
12184 {
12185 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12186
12187 return 1;
12188 }
12189
12190 static enum print_stop_action
12191 bkpt_print_it (bpstat bs)
12192 {
12193 struct breakpoint *b;
12194 const struct bp_location *bl;
12195 int bp_temp;
12196 struct ui_out *uiout = current_uiout;
12197
12198 gdb_assert (bs->bp_location_at != NULL);
12199
12200 bl = bs->bp_location_at;
12201 b = bs->breakpoint_at;
12202
12203 bp_temp = b->disposition == disp_del;
12204 if (bl->address != bl->requested_address)
12205 breakpoint_adjustment_warning (bl->requested_address,
12206 bl->address,
12207 b->number, 1);
12208 annotate_breakpoint (b->number);
12209 if (bp_temp)
12210 ui_out_text (uiout, "\nTemporary breakpoint ");
12211 else
12212 ui_out_text (uiout, "\nBreakpoint ");
12213 if (ui_out_is_mi_like_p (uiout))
12214 {
12215 ui_out_field_string (uiout, "reason",
12216 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12217 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
12218 }
12219 ui_out_field_int (uiout, "bkptno", b->number);
12220 ui_out_text (uiout, ", ");
12221
12222 return PRINT_SRC_AND_LOC;
12223 }
12224
12225 static void
12226 bkpt_print_mention (struct breakpoint *b)
12227 {
12228 if (ui_out_is_mi_like_p (current_uiout))
12229 return;
12230
12231 switch (b->type)
12232 {
12233 case bp_breakpoint:
12234 case bp_gnu_ifunc_resolver:
12235 if (b->disposition == disp_del)
12236 printf_filtered (_("Temporary breakpoint"));
12237 else
12238 printf_filtered (_("Breakpoint"));
12239 printf_filtered (_(" %d"), b->number);
12240 if (b->type == bp_gnu_ifunc_resolver)
12241 printf_filtered (_(" at gnu-indirect-function resolver"));
12242 break;
12243 case bp_hardware_breakpoint:
12244 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12245 break;
12246 }
12247
12248 say_where (b);
12249 }
12250
12251 static void
12252 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12253 {
12254 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12255 fprintf_unfiltered (fp, "tbreak");
12256 else if (tp->type == bp_breakpoint)
12257 fprintf_unfiltered (fp, "break");
12258 else if (tp->type == bp_hardware_breakpoint
12259 && tp->disposition == disp_del)
12260 fprintf_unfiltered (fp, "thbreak");
12261 else if (tp->type == bp_hardware_breakpoint)
12262 fprintf_unfiltered (fp, "hbreak");
12263 else
12264 internal_error (__FILE__, __LINE__,
12265 _("unhandled breakpoint type %d"), (int) tp->type);
12266
12267 fprintf_unfiltered (fp, " %s", tp->addr_string);
12268 print_recreate_thread (tp, fp);
12269 }
12270
12271 static void
12272 bkpt_create_sals_from_address (char **arg,
12273 struct linespec_result *canonical,
12274 enum bptype type_wanted,
12275 char *addr_start, char **copy_arg)
12276 {
12277 create_sals_from_address_default (arg, canonical, type_wanted,
12278 addr_start, copy_arg);
12279 }
12280
12281 static void
12282 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12283 struct linespec_result *canonical,
12284 struct linespec_sals *lsal,
12285 char *cond_string,
12286 enum bptype type_wanted,
12287 enum bpdisp disposition,
12288 int thread,
12289 int task, int ignore_count,
12290 const struct breakpoint_ops *ops,
12291 int from_tty, int enabled,
12292 int internal, unsigned flags)
12293 {
12294 create_breakpoints_sal_default (gdbarch, canonical, lsal,
12295 cond_string, type_wanted,
12296 disposition, thread, task,
12297 ignore_count, ops, from_tty,
12298 enabled, internal, flags);
12299 }
12300
12301 static void
12302 bkpt_decode_linespec (struct breakpoint *b, char **s,
12303 struct symtabs_and_lines *sals)
12304 {
12305 decode_linespec_default (b, s, sals);
12306 }
12307
12308 /* Virtual table for internal breakpoints. */
12309
12310 static void
12311 internal_bkpt_re_set (struct breakpoint *b)
12312 {
12313 switch (b->type)
12314 {
12315 /* Delete overlay event and longjmp master breakpoints; they
12316 will be reset later by breakpoint_re_set. */
12317 case bp_overlay_event:
12318 case bp_longjmp_master:
12319 case bp_std_terminate_master:
12320 case bp_exception_master:
12321 delete_breakpoint (b);
12322 break;
12323
12324 /* This breakpoint is special, it's set up when the inferior
12325 starts and we really don't want to touch it. */
12326 case bp_shlib_event:
12327
12328 /* Like bp_shlib_event, this breakpoint type is special. Once
12329 it is set up, we do not want to touch it. */
12330 case bp_thread_event:
12331 break;
12332 }
12333 }
12334
12335 static void
12336 internal_bkpt_check_status (bpstat bs)
12337 {
12338 if (bs->breakpoint_at->type == bp_shlib_event)
12339 {
12340 /* If requested, stop when the dynamic linker notifies GDB of
12341 events. This allows the user to get control and place
12342 breakpoints in initializer routines for dynamically loaded
12343 objects (among other things). */
12344 bs->stop = stop_on_solib_events;
12345 bs->print = stop_on_solib_events;
12346 }
12347 else
12348 bs->stop = 0;
12349 }
12350
12351 static enum print_stop_action
12352 internal_bkpt_print_it (bpstat bs)
12353 {
12354 struct ui_out *uiout = current_uiout;
12355 struct breakpoint *b;
12356
12357 b = bs->breakpoint_at;
12358
12359 switch (b->type)
12360 {
12361 case bp_shlib_event:
12362 /* Did we stop because the user set the stop_on_solib_events
12363 variable? (If so, we report this as a generic, "Stopped due
12364 to shlib event" message.) */
12365 print_solib_event (0);
12366 break;
12367
12368 case bp_thread_event:
12369 /* Not sure how we will get here.
12370 GDB should not stop for these breakpoints. */
12371 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12372 break;
12373
12374 case bp_overlay_event:
12375 /* By analogy with the thread event, GDB should not stop for these. */
12376 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12377 break;
12378
12379 case bp_longjmp_master:
12380 /* These should never be enabled. */
12381 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12382 break;
12383
12384 case bp_std_terminate_master:
12385 /* These should never be enabled. */
12386 printf_filtered (_("std::terminate Master Breakpoint: "
12387 "gdb should not stop!\n"));
12388 break;
12389
12390 case bp_exception_master:
12391 /* These should never be enabled. */
12392 printf_filtered (_("Exception Master Breakpoint: "
12393 "gdb should not stop!\n"));
12394 break;
12395 }
12396
12397 return PRINT_NOTHING;
12398 }
12399
12400 static void
12401 internal_bkpt_print_mention (struct breakpoint *b)
12402 {
12403 /* Nothing to mention. These breakpoints are internal. */
12404 }
12405
12406 /* Virtual table for momentary breakpoints */
12407
12408 static void
12409 momentary_bkpt_re_set (struct breakpoint *b)
12410 {
12411 /* Keep temporary breakpoints, which can be encountered when we step
12412 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12413 Otherwise these should have been blown away via the cleanup chain
12414 or by breakpoint_init_inferior when we rerun the executable. */
12415 }
12416
12417 static void
12418 momentary_bkpt_check_status (bpstat bs)
12419 {
12420 /* Nothing. The point of these breakpoints is causing a stop. */
12421 }
12422
12423 static enum print_stop_action
12424 momentary_bkpt_print_it (bpstat bs)
12425 {
12426 struct ui_out *uiout = current_uiout;
12427
12428 if (ui_out_is_mi_like_p (uiout))
12429 {
12430 struct breakpoint *b = bs->breakpoint_at;
12431
12432 switch (b->type)
12433 {
12434 case bp_finish:
12435 ui_out_field_string
12436 (uiout, "reason",
12437 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12438 break;
12439
12440 case bp_until:
12441 ui_out_field_string
12442 (uiout, "reason",
12443 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12444 break;
12445 }
12446 }
12447
12448 return PRINT_UNKNOWN;
12449 }
12450
12451 static void
12452 momentary_bkpt_print_mention (struct breakpoint *b)
12453 {
12454 /* Nothing to mention. These breakpoints are internal. */
12455 }
12456
12457 /* The breakpoint_ops structure to be used in tracepoints. */
12458
12459 static void
12460 tracepoint_re_set (struct breakpoint *b)
12461 {
12462 breakpoint_re_set_default (b);
12463 }
12464
12465 static int
12466 tracepoint_breakpoint_hit (const struct bp_location *bl,
12467 struct address_space *aspace, CORE_ADDR bp_addr,
12468 const struct target_waitstatus *ws)
12469 {
12470 /* By definition, the inferior does not report stops at
12471 tracepoints. */
12472 return 0;
12473 }
12474
12475 static void
12476 tracepoint_print_one_detail (const struct breakpoint *self,
12477 struct ui_out *uiout)
12478 {
12479 struct tracepoint *tp = (struct tracepoint *) self;
12480 if (tp->static_trace_marker_id)
12481 {
12482 gdb_assert (self->type == bp_static_tracepoint);
12483
12484 ui_out_text (uiout, "\tmarker id is ");
12485 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
12486 tp->static_trace_marker_id);
12487 ui_out_text (uiout, "\n");
12488 }
12489 }
12490
12491 static void
12492 tracepoint_print_mention (struct breakpoint *b)
12493 {
12494 if (ui_out_is_mi_like_p (current_uiout))
12495 return;
12496
12497 switch (b->type)
12498 {
12499 case bp_tracepoint:
12500 printf_filtered (_("Tracepoint"));
12501 printf_filtered (_(" %d"), b->number);
12502 break;
12503 case bp_fast_tracepoint:
12504 printf_filtered (_("Fast tracepoint"));
12505 printf_filtered (_(" %d"), b->number);
12506 break;
12507 case bp_static_tracepoint:
12508 printf_filtered (_("Static tracepoint"));
12509 printf_filtered (_(" %d"), b->number);
12510 break;
12511 default:
12512 internal_error (__FILE__, __LINE__,
12513 _("unhandled tracepoint type %d"), (int) b->type);
12514 }
12515
12516 say_where (b);
12517 }
12518
12519 static void
12520 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12521 {
12522 struct tracepoint *tp = (struct tracepoint *) self;
12523
12524 if (self->type == bp_fast_tracepoint)
12525 fprintf_unfiltered (fp, "ftrace");
12526 if (self->type == bp_static_tracepoint)
12527 fprintf_unfiltered (fp, "strace");
12528 else if (self->type == bp_tracepoint)
12529 fprintf_unfiltered (fp, "trace");
12530 else
12531 internal_error (__FILE__, __LINE__,
12532 _("unhandled tracepoint type %d"), (int) self->type);
12533
12534 fprintf_unfiltered (fp, " %s", self->addr_string);
12535 print_recreate_thread (self, fp);
12536
12537 if (tp->pass_count)
12538 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12539 }
12540
12541 static void
12542 tracepoint_create_sals_from_address (char **arg,
12543 struct linespec_result *canonical,
12544 enum bptype type_wanted,
12545 char *addr_start, char **copy_arg)
12546 {
12547 create_sals_from_address_default (arg, canonical, type_wanted,
12548 addr_start, copy_arg);
12549 }
12550
12551 static void
12552 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12553 struct linespec_result *canonical,
12554 struct linespec_sals *lsal,
12555 char *cond_string,
12556 enum bptype type_wanted,
12557 enum bpdisp disposition,
12558 int thread,
12559 int task, int ignore_count,
12560 const struct breakpoint_ops *ops,
12561 int from_tty, int enabled,
12562 int internal, unsigned flags)
12563 {
12564 create_breakpoints_sal_default (gdbarch, canonical, lsal,
12565 cond_string, type_wanted,
12566 disposition, thread, task,
12567 ignore_count, ops, from_tty,
12568 enabled, internal, flags);
12569 }
12570
12571 static void
12572 tracepoint_decode_linespec (struct breakpoint *b, char **s,
12573 struct symtabs_and_lines *sals)
12574 {
12575 decode_linespec_default (b, s, sals);
12576 }
12577
12578 struct breakpoint_ops tracepoint_breakpoint_ops;
12579
12580 /* The breakpoint_ops structure to be used on static tracepoints with
12581 markers (`-m'). */
12582
12583 static void
12584 strace_marker_create_sals_from_address (char **arg,
12585 struct linespec_result *canonical,
12586 enum bptype type_wanted,
12587 char *addr_start, char **copy_arg)
12588 {
12589 struct linespec_sals lsal;
12590
12591 lsal.sals = decode_static_tracepoint_spec (arg);
12592
12593 *copy_arg = savestring (addr_start, *arg - addr_start);
12594
12595 canonical->addr_string = xstrdup (*copy_arg);
12596 lsal.canonical = xstrdup (*copy_arg);
12597 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12598 }
12599
12600 static void
12601 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12602 struct linespec_result *canonical,
12603 struct linespec_sals *lsal,
12604 char *cond_string,
12605 enum bptype type_wanted,
12606 enum bpdisp disposition,
12607 int thread,
12608 int task, int ignore_count,
12609 const struct breakpoint_ops *ops,
12610 int from_tty, int enabled,
12611 int internal, unsigned flags)
12612 {
12613 int i;
12614
12615 /* If the user is creating a static tracepoint by marker id
12616 (strace -m MARKER_ID), then store the sals index, so that
12617 breakpoint_re_set can try to match up which of the newly
12618 found markers corresponds to this one, and, don't try to
12619 expand multiple locations for each sal, given than SALS
12620 already should contain all sals for MARKER_ID. */
12621
12622 for (i = 0; i < lsal->sals.nelts; ++i)
12623 {
12624 struct symtabs_and_lines expanded;
12625 struct tracepoint *tp;
12626 struct cleanup *old_chain;
12627 char *addr_string;
12628
12629 expanded.nelts = 1;
12630 expanded.sals = &lsal->sals.sals[i];
12631
12632 addr_string = xstrdup (canonical->addr_string);
12633 old_chain = make_cleanup (xfree, addr_string);
12634
12635 tp = XCNEW (struct tracepoint);
12636 init_breakpoint_sal (&tp->base, gdbarch, expanded,
12637 addr_string, NULL,
12638 cond_string, type_wanted, disposition,
12639 thread, task, ignore_count, ops,
12640 from_tty, enabled, internal, flags,
12641 canonical->special_display);
12642 /* Given that its possible to have multiple markers with
12643 the same string id, if the user is creating a static
12644 tracepoint by marker id ("strace -m MARKER_ID"), then
12645 store the sals index, so that breakpoint_re_set can
12646 try to match up which of the newly found markers
12647 corresponds to this one */
12648 tp->static_trace_marker_id_idx = i;
12649
12650 install_breakpoint (internal, &tp->base, 0);
12651
12652 discard_cleanups (old_chain);
12653 }
12654 }
12655
12656 static void
12657 strace_marker_decode_linespec (struct breakpoint *b, char **s,
12658 struct symtabs_and_lines *sals)
12659 {
12660 struct tracepoint *tp = (struct tracepoint *) b;
12661
12662 *sals = decode_static_tracepoint_spec (s);
12663 if (sals->nelts > tp->static_trace_marker_id_idx)
12664 {
12665 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
12666 sals->nelts = 1;
12667 }
12668 else
12669 error (_("marker %s not found"), tp->static_trace_marker_id);
12670 }
12671
12672 static struct breakpoint_ops strace_marker_breakpoint_ops;
12673
12674 static int
12675 strace_marker_p (struct breakpoint *b)
12676 {
12677 return b->ops == &strace_marker_breakpoint_ops;
12678 }
12679
12680 /* Delete a breakpoint and clean up all traces of it in the data
12681 structures. */
12682
12683 void
12684 delete_breakpoint (struct breakpoint *bpt)
12685 {
12686 struct breakpoint *b;
12687
12688 gdb_assert (bpt != NULL);
12689
12690 /* Has this bp already been deleted? This can happen because
12691 multiple lists can hold pointers to bp's. bpstat lists are
12692 especial culprits.
12693
12694 One example of this happening is a watchpoint's scope bp. When
12695 the scope bp triggers, we notice that the watchpoint is out of
12696 scope, and delete it. We also delete its scope bp. But the
12697 scope bp is marked "auto-deleting", and is already on a bpstat.
12698 That bpstat is then checked for auto-deleting bp's, which are
12699 deleted.
12700
12701 A real solution to this problem might involve reference counts in
12702 bp's, and/or giving them pointers back to their referencing
12703 bpstat's, and teaching delete_breakpoint to only free a bp's
12704 storage when no more references were extent. A cheaper bandaid
12705 was chosen. */
12706 if (bpt->type == bp_none)
12707 return;
12708
12709 /* At least avoid this stale reference until the reference counting
12710 of breakpoints gets resolved. */
12711 if (bpt->related_breakpoint != bpt)
12712 {
12713 struct breakpoint *related;
12714 struct watchpoint *w;
12715
12716 if (bpt->type == bp_watchpoint_scope)
12717 w = (struct watchpoint *) bpt->related_breakpoint;
12718 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12719 w = (struct watchpoint *) bpt;
12720 else
12721 w = NULL;
12722 if (w != NULL)
12723 watchpoint_del_at_next_stop (w);
12724
12725 /* Unlink bpt from the bpt->related_breakpoint ring. */
12726 for (related = bpt; related->related_breakpoint != bpt;
12727 related = related->related_breakpoint);
12728 related->related_breakpoint = bpt->related_breakpoint;
12729 bpt->related_breakpoint = bpt;
12730 }
12731
12732 /* watch_command_1 creates a watchpoint but only sets its number if
12733 update_watchpoint succeeds in creating its bp_locations. If there's
12734 a problem in that process, we'll be asked to delete the half-created
12735 watchpoint. In that case, don't announce the deletion. */
12736 if (bpt->number)
12737 observer_notify_breakpoint_deleted (bpt);
12738
12739 if (breakpoint_chain == bpt)
12740 breakpoint_chain = bpt->next;
12741
12742 ALL_BREAKPOINTS (b)
12743 if (b->next == bpt)
12744 {
12745 b->next = bpt->next;
12746 break;
12747 }
12748
12749 /* Be sure no bpstat's are pointing at the breakpoint after it's
12750 been freed. */
12751 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12752 in all threads for now. Note that we cannot just remove bpstats
12753 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12754 commands are associated with the bpstat; if we remove it here,
12755 then the later call to bpstat_do_actions (&stop_bpstat); in
12756 event-top.c won't do anything, and temporary breakpoints with
12757 commands won't work. */
12758
12759 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12760
12761 /* Now that breakpoint is removed from breakpoint list, update the
12762 global location list. This will remove locations that used to
12763 belong to this breakpoint. Do this before freeing the breakpoint
12764 itself, since remove_breakpoint looks at location's owner. It
12765 might be better design to have location completely
12766 self-contained, but it's not the case now. */
12767 update_global_location_list (0);
12768
12769 bpt->ops->dtor (bpt);
12770 /* On the chance that someone will soon try again to delete this
12771 same bp, we mark it as deleted before freeing its storage. */
12772 bpt->type = bp_none;
12773 xfree (bpt);
12774 }
12775
12776 static void
12777 do_delete_breakpoint_cleanup (void *b)
12778 {
12779 delete_breakpoint (b);
12780 }
12781
12782 struct cleanup *
12783 make_cleanup_delete_breakpoint (struct breakpoint *b)
12784 {
12785 return make_cleanup (do_delete_breakpoint_cleanup, b);
12786 }
12787
12788 /* Iterator function to call a user-provided callback function once
12789 for each of B and its related breakpoints. */
12790
12791 static void
12792 iterate_over_related_breakpoints (struct breakpoint *b,
12793 void (*function) (struct breakpoint *,
12794 void *),
12795 void *data)
12796 {
12797 struct breakpoint *related;
12798
12799 related = b;
12800 do
12801 {
12802 struct breakpoint *next;
12803
12804 /* FUNCTION may delete RELATED. */
12805 next = related->related_breakpoint;
12806
12807 if (next == related)
12808 {
12809 /* RELATED is the last ring entry. */
12810 function (related, data);
12811
12812 /* FUNCTION may have deleted it, so we'd never reach back to
12813 B. There's nothing left to do anyway, so just break
12814 out. */
12815 break;
12816 }
12817 else
12818 function (related, data);
12819
12820 related = next;
12821 }
12822 while (related != b);
12823 }
12824
12825 static void
12826 do_delete_breakpoint (struct breakpoint *b, void *ignore)
12827 {
12828 delete_breakpoint (b);
12829 }
12830
12831 /* A callback for map_breakpoint_numbers that calls
12832 delete_breakpoint. */
12833
12834 static void
12835 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
12836 {
12837 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
12838 }
12839
12840 void
12841 delete_command (char *arg, int from_tty)
12842 {
12843 struct breakpoint *b, *b_tmp;
12844
12845 dont_repeat ();
12846
12847 if (arg == 0)
12848 {
12849 int breaks_to_delete = 0;
12850
12851 /* Delete all breakpoints if no argument. Do not delete
12852 internal breakpoints, these have to be deleted with an
12853 explicit breakpoint number argument. */
12854 ALL_BREAKPOINTS (b)
12855 if (user_breakpoint_p (b))
12856 {
12857 breaks_to_delete = 1;
12858 break;
12859 }
12860
12861 /* Ask user only if there are some breakpoints to delete. */
12862 if (!from_tty
12863 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12864 {
12865 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12866 if (user_breakpoint_p (b))
12867 delete_breakpoint (b);
12868 }
12869 }
12870 else
12871 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
12872 }
12873
12874 static int
12875 all_locations_are_pending (struct bp_location *loc)
12876 {
12877 for (; loc; loc = loc->next)
12878 if (!loc->shlib_disabled
12879 && !loc->pspace->executing_startup)
12880 return 0;
12881 return 1;
12882 }
12883
12884 /* Subroutine of update_breakpoint_locations to simplify it.
12885 Return non-zero if multiple fns in list LOC have the same name.
12886 Null names are ignored. */
12887
12888 static int
12889 ambiguous_names_p (struct bp_location *loc)
12890 {
12891 struct bp_location *l;
12892 htab_t htab = htab_create_alloc (13, htab_hash_string,
12893 (int (*) (const void *,
12894 const void *)) streq,
12895 NULL, xcalloc, xfree);
12896
12897 for (l = loc; l != NULL; l = l->next)
12898 {
12899 const char **slot;
12900 const char *name = l->function_name;
12901
12902 /* Allow for some names to be NULL, ignore them. */
12903 if (name == NULL)
12904 continue;
12905
12906 slot = (const char **) htab_find_slot (htab, (const void *) name,
12907 INSERT);
12908 /* NOTE: We can assume slot != NULL here because xcalloc never
12909 returns NULL. */
12910 if (*slot != NULL)
12911 {
12912 htab_delete (htab);
12913 return 1;
12914 }
12915 *slot = name;
12916 }
12917
12918 htab_delete (htab);
12919 return 0;
12920 }
12921
12922 /* When symbols change, it probably means the sources changed as well,
12923 and it might mean the static tracepoint markers are no longer at
12924 the same address or line numbers they used to be at last we
12925 checked. Losing your static tracepoints whenever you rebuild is
12926 undesirable. This function tries to resync/rematch gdb static
12927 tracepoints with the markers on the target, for static tracepoints
12928 that have not been set by marker id. Static tracepoint that have
12929 been set by marker id are reset by marker id in breakpoint_re_set.
12930 The heuristic is:
12931
12932 1) For a tracepoint set at a specific address, look for a marker at
12933 the old PC. If one is found there, assume to be the same marker.
12934 If the name / string id of the marker found is different from the
12935 previous known name, assume that means the user renamed the marker
12936 in the sources, and output a warning.
12937
12938 2) For a tracepoint set at a given line number, look for a marker
12939 at the new address of the old line number. If one is found there,
12940 assume to be the same marker. If the name / string id of the
12941 marker found is different from the previous known name, assume that
12942 means the user renamed the marker in the sources, and output a
12943 warning.
12944
12945 3) If a marker is no longer found at the same address or line, it
12946 may mean the marker no longer exists. But it may also just mean
12947 the code changed a bit. Maybe the user added a few lines of code
12948 that made the marker move up or down (in line number terms). Ask
12949 the target for info about the marker with the string id as we knew
12950 it. If found, update line number and address in the matching
12951 static tracepoint. This will get confused if there's more than one
12952 marker with the same ID (possible in UST, although unadvised
12953 precisely because it confuses tools). */
12954
12955 static struct symtab_and_line
12956 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12957 {
12958 struct tracepoint *tp = (struct tracepoint *) b;
12959 struct static_tracepoint_marker marker;
12960 CORE_ADDR pc;
12961 int i;
12962
12963 pc = sal.pc;
12964 if (sal.line)
12965 find_line_pc (sal.symtab, sal.line, &pc);
12966
12967 if (target_static_tracepoint_marker_at (pc, &marker))
12968 {
12969 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
12970 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12971 b->number,
12972 tp->static_trace_marker_id, marker.str_id);
12973
12974 xfree (tp->static_trace_marker_id);
12975 tp->static_trace_marker_id = xstrdup (marker.str_id);
12976 release_static_tracepoint_marker (&marker);
12977
12978 return sal;
12979 }
12980
12981 /* Old marker wasn't found on target at lineno. Try looking it up
12982 by string ID. */
12983 if (!sal.explicit_pc
12984 && sal.line != 0
12985 && sal.symtab != NULL
12986 && tp->static_trace_marker_id != NULL)
12987 {
12988 VEC(static_tracepoint_marker_p) *markers;
12989
12990 markers
12991 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
12992
12993 if (!VEC_empty(static_tracepoint_marker_p, markers))
12994 {
12995 struct symtab_and_line sal2;
12996 struct symbol *sym;
12997 struct static_tracepoint_marker *tpmarker;
12998 struct ui_out *uiout = current_uiout;
12999
13000 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13001
13002 xfree (tp->static_trace_marker_id);
13003 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13004
13005 warning (_("marker for static tracepoint %d (%s) not "
13006 "found at previous line number"),
13007 b->number, tp->static_trace_marker_id);
13008
13009 init_sal (&sal2);
13010
13011 sal2.pc = tpmarker->address;
13012
13013 sal2 = find_pc_line (tpmarker->address, 0);
13014 sym = find_pc_sect_function (tpmarker->address, NULL);
13015 ui_out_text (uiout, "Now in ");
13016 if (sym)
13017 {
13018 ui_out_field_string (uiout, "func",
13019 SYMBOL_PRINT_NAME (sym));
13020 ui_out_text (uiout, " at ");
13021 }
13022 ui_out_field_string (uiout, "file", sal2.symtab->filename);
13023 ui_out_text (uiout, ":");
13024
13025 if (ui_out_is_mi_like_p (uiout))
13026 {
13027 char *fullname = symtab_to_fullname (sal2.symtab);
13028
13029 if (fullname)
13030 ui_out_field_string (uiout, "fullname", fullname);
13031 }
13032
13033 ui_out_field_int (uiout, "line", sal2.line);
13034 ui_out_text (uiout, "\n");
13035
13036 b->loc->line_number = sal2.line;
13037
13038 xfree (b->loc->source_file);
13039 if (sym)
13040 b->loc->source_file = xstrdup (sal2.symtab->filename);
13041 else
13042 b->loc->source_file = NULL;
13043
13044 xfree (b->addr_string);
13045 b->addr_string = xstrprintf ("%s:%d",
13046 sal2.symtab->filename,
13047 b->loc->line_number);
13048
13049 /* Might be nice to check if function changed, and warn if
13050 so. */
13051
13052 release_static_tracepoint_marker (tpmarker);
13053 }
13054 }
13055 return sal;
13056 }
13057
13058 /* Returns 1 iff locations A and B are sufficiently same that
13059 we don't need to report breakpoint as changed. */
13060
13061 static int
13062 locations_are_equal (struct bp_location *a, struct bp_location *b)
13063 {
13064 while (a && b)
13065 {
13066 if (a->address != b->address)
13067 return 0;
13068
13069 if (a->shlib_disabled != b->shlib_disabled)
13070 return 0;
13071
13072 if (a->enabled != b->enabled)
13073 return 0;
13074
13075 a = a->next;
13076 b = b->next;
13077 }
13078
13079 if ((a == NULL) != (b == NULL))
13080 return 0;
13081
13082 return 1;
13083 }
13084
13085 /* Create new breakpoint locations for B (a hardware or software breakpoint)
13086 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13087 a ranged breakpoint. */
13088
13089 void
13090 update_breakpoint_locations (struct breakpoint *b,
13091 struct symtabs_and_lines sals,
13092 struct symtabs_and_lines sals_end)
13093 {
13094 int i;
13095 struct bp_location *existing_locations = b->loc;
13096
13097 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13098 {
13099 /* Ranged breakpoints have only one start location and one end
13100 location. */
13101 b->enable_state = bp_disabled;
13102 update_global_location_list (1);
13103 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13104 "multiple locations found\n"),
13105 b->number);
13106 return;
13107 }
13108
13109 /* If there's no new locations, and all existing locations are
13110 pending, don't do anything. This optimizes the common case where
13111 all locations are in the same shared library, that was unloaded.
13112 We'd like to retain the location, so that when the library is
13113 loaded again, we don't loose the enabled/disabled status of the
13114 individual locations. */
13115 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
13116 return;
13117
13118 b->loc = NULL;
13119
13120 for (i = 0; i < sals.nelts; ++i)
13121 {
13122 struct bp_location *new_loc;
13123
13124 switch_to_program_space_and_thread (sals.sals[i].pspace);
13125
13126 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
13127
13128 /* Reparse conditions, they might contain references to the
13129 old symtab. */
13130 if (b->cond_string != NULL)
13131 {
13132 char *s;
13133 volatile struct gdb_exception e;
13134
13135 s = b->cond_string;
13136 TRY_CATCH (e, RETURN_MASK_ERROR)
13137 {
13138 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
13139 0);
13140 }
13141 if (e.reason < 0)
13142 {
13143 warning (_("failed to reevaluate condition "
13144 "for breakpoint %d: %s"),
13145 b->number, e.message);
13146 new_loc->enabled = 0;
13147 }
13148 }
13149
13150 if (sals_end.nelts)
13151 {
13152 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13153
13154 new_loc->length = end - sals.sals[0].pc + 1;
13155 }
13156 }
13157
13158 /* Update locations of permanent breakpoints. */
13159 if (b->enable_state == bp_permanent)
13160 make_breakpoint_permanent (b);
13161
13162 /* If possible, carry over 'disable' status from existing
13163 breakpoints. */
13164 {
13165 struct bp_location *e = existing_locations;
13166 /* If there are multiple breakpoints with the same function name,
13167 e.g. for inline functions, comparing function names won't work.
13168 Instead compare pc addresses; this is just a heuristic as things
13169 may have moved, but in practice it gives the correct answer
13170 often enough until a better solution is found. */
13171 int have_ambiguous_names = ambiguous_names_p (b->loc);
13172
13173 for (; e; e = e->next)
13174 {
13175 if (!e->enabled && e->function_name)
13176 {
13177 struct bp_location *l = b->loc;
13178 if (have_ambiguous_names)
13179 {
13180 for (; l; l = l->next)
13181 if (breakpoint_locations_match (e, l))
13182 {
13183 l->enabled = 0;
13184 break;
13185 }
13186 }
13187 else
13188 {
13189 for (; l; l = l->next)
13190 if (l->function_name
13191 && strcmp (e->function_name, l->function_name) == 0)
13192 {
13193 l->enabled = 0;
13194 break;
13195 }
13196 }
13197 }
13198 }
13199 }
13200
13201 if (!locations_are_equal (existing_locations, b->loc))
13202 observer_notify_breakpoint_modified (b);
13203
13204 update_global_location_list (1);
13205 }
13206
13207 /* Find the SaL locations corresponding to the given ADDR_STRING.
13208 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13209
13210 static struct symtabs_and_lines
13211 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13212 {
13213 char *s;
13214 struct symtabs_and_lines sals = {0};
13215 volatile struct gdb_exception e;
13216
13217 gdb_assert (b->ops != NULL);
13218 s = addr_string;
13219
13220 TRY_CATCH (e, RETURN_MASK_ERROR)
13221 {
13222 b->ops->decode_linespec (b, &s, &sals);
13223 }
13224 if (e.reason < 0)
13225 {
13226 int not_found_and_ok = 0;
13227 /* For pending breakpoints, it's expected that parsing will
13228 fail until the right shared library is loaded. User has
13229 already told to create pending breakpoints and don't need
13230 extra messages. If breakpoint is in bp_shlib_disabled
13231 state, then user already saw the message about that
13232 breakpoint being disabled, and don't want to see more
13233 errors. */
13234 if (e.error == NOT_FOUND_ERROR
13235 && (b->condition_not_parsed
13236 || (b->loc && b->loc->shlib_disabled)
13237 || (b->loc && b->loc->pspace->executing_startup)
13238 || b->enable_state == bp_disabled))
13239 not_found_and_ok = 1;
13240
13241 if (!not_found_and_ok)
13242 {
13243 /* We surely don't want to warn about the same breakpoint
13244 10 times. One solution, implemented here, is disable
13245 the breakpoint on error. Another solution would be to
13246 have separate 'warning emitted' flag. Since this
13247 happens only when a binary has changed, I don't know
13248 which approach is better. */
13249 b->enable_state = bp_disabled;
13250 throw_exception (e);
13251 }
13252 }
13253
13254 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
13255 {
13256 int i;
13257
13258 for (i = 0; i < sals.nelts; ++i)
13259 resolve_sal_pc (&sals.sals[i]);
13260 if (b->condition_not_parsed && s && s[0])
13261 {
13262 char *cond_string = 0;
13263 int thread = -1;
13264 int task = 0;
13265
13266 find_condition_and_thread (s, sals.sals[0].pc,
13267 &cond_string, &thread, &task);
13268 if (cond_string)
13269 b->cond_string = cond_string;
13270 b->thread = thread;
13271 b->task = task;
13272 b->condition_not_parsed = 0;
13273 }
13274
13275 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13276 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
13277
13278 *found = 1;
13279 }
13280 else
13281 *found = 0;
13282
13283 return sals;
13284 }
13285
13286 /* The default re_set method, for typical hardware or software
13287 breakpoints. Reevaluate the breakpoint and recreate its
13288 locations. */
13289
13290 static void
13291 breakpoint_re_set_default (struct breakpoint *b)
13292 {
13293 int found;
13294 struct symtabs_and_lines sals, sals_end;
13295 struct symtabs_and_lines expanded = {0};
13296 struct symtabs_and_lines expanded_end = {0};
13297
13298 sals = addr_string_to_sals (b, b->addr_string, &found);
13299 if (found)
13300 {
13301 make_cleanup (xfree, sals.sals);
13302 expanded = sals;
13303 }
13304
13305 if (b->addr_string_range_end)
13306 {
13307 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13308 if (found)
13309 {
13310 make_cleanup (xfree, sals_end.sals);
13311 expanded_end = sals_end;
13312 }
13313 }
13314
13315 update_breakpoint_locations (b, expanded, expanded_end);
13316 }
13317
13318 /* Default method for creating SALs from an address string. It basically
13319 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13320
13321 static void
13322 create_sals_from_address_default (char **arg,
13323 struct linespec_result *canonical,
13324 enum bptype type_wanted,
13325 char *addr_start, char **copy_arg)
13326 {
13327 parse_breakpoint_sals (arg, canonical);
13328 }
13329
13330 /* Call create_breakpoints_sal for the given arguments. This is the default
13331 function for the `create_breakpoints_sal' method of
13332 breakpoint_ops. */
13333
13334 static void
13335 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13336 struct linespec_result *canonical,
13337 struct linespec_sals *lsal,
13338 char *cond_string,
13339 enum bptype type_wanted,
13340 enum bpdisp disposition,
13341 int thread,
13342 int task, int ignore_count,
13343 const struct breakpoint_ops *ops,
13344 int from_tty, int enabled,
13345 int internal, unsigned flags)
13346 {
13347 create_breakpoints_sal (gdbarch, canonical, cond_string,
13348 type_wanted, disposition,
13349 thread, task, ignore_count, ops, from_tty,
13350 enabled, internal, flags);
13351 }
13352
13353 /* Decode the line represented by S by calling decode_line_full. This is the
13354 default function for the `decode_linespec' method of breakpoint_ops. */
13355
13356 static void
13357 decode_linespec_default (struct breakpoint *b, char **s,
13358 struct symtabs_and_lines *sals)
13359 {
13360 struct linespec_result canonical;
13361
13362 init_linespec_result (&canonical);
13363 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13364 (struct symtab *) NULL, 0,
13365 &canonical, multiple_symbols_all,
13366 b->filter);
13367
13368 /* We should get 0 or 1 resulting SALs. */
13369 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13370
13371 if (VEC_length (linespec_sals, canonical.sals) > 0)
13372 {
13373 struct linespec_sals *lsal;
13374
13375 lsal = VEC_index (linespec_sals, canonical.sals, 0);
13376 *sals = lsal->sals;
13377 /* Arrange it so the destructor does not free the
13378 contents. */
13379 lsal->sals.sals = NULL;
13380 }
13381
13382 destroy_linespec_result (&canonical);
13383 }
13384
13385 /* Prepare the global context for a re-set of breakpoint B. */
13386
13387 static struct cleanup *
13388 prepare_re_set_context (struct breakpoint *b)
13389 {
13390 struct cleanup *cleanups;
13391
13392 input_radix = b->input_radix;
13393 cleanups = save_current_space_and_thread ();
13394 if (b->pspace != NULL)
13395 switch_to_program_space_and_thread (b->pspace);
13396 set_language (b->language);
13397
13398 return cleanups;
13399 }
13400
13401 /* Reset a breakpoint given it's struct breakpoint * BINT.
13402 The value we return ends up being the return value from catch_errors.
13403 Unused in this case. */
13404
13405 static int
13406 breakpoint_re_set_one (void *bint)
13407 {
13408 /* Get past catch_errs. */
13409 struct breakpoint *b = (struct breakpoint *) bint;
13410 struct cleanup *cleanups;
13411
13412 cleanups = prepare_re_set_context (b);
13413 b->ops->re_set (b);
13414 do_cleanups (cleanups);
13415 return 0;
13416 }
13417
13418 /* Re-set all breakpoints after symbols have been re-loaded. */
13419 void
13420 breakpoint_re_set (void)
13421 {
13422 struct breakpoint *b, *b_tmp;
13423 enum language save_language;
13424 int save_input_radix;
13425 struct cleanup *old_chain;
13426
13427 save_language = current_language->la_language;
13428 save_input_radix = input_radix;
13429 old_chain = save_current_program_space ();
13430
13431 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13432 {
13433 /* Format possible error msg. */
13434 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
13435 b->number);
13436 struct cleanup *cleanups = make_cleanup (xfree, message);
13437 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
13438 do_cleanups (cleanups);
13439 }
13440 set_language (save_language);
13441 input_radix = save_input_radix;
13442
13443 jit_breakpoint_re_set ();
13444
13445 do_cleanups (old_chain);
13446
13447 create_overlay_event_breakpoint ();
13448 create_longjmp_master_breakpoint ();
13449 create_std_terminate_master_breakpoint ();
13450 create_exception_master_breakpoint ();
13451
13452 /* While we're at it, reset the skip list too. */
13453 skip_re_set ();
13454 }
13455 \f
13456 /* Reset the thread number of this breakpoint:
13457
13458 - If the breakpoint is for all threads, leave it as-is.
13459 - Else, reset it to the current thread for inferior_ptid. */
13460 void
13461 breakpoint_re_set_thread (struct breakpoint *b)
13462 {
13463 if (b->thread != -1)
13464 {
13465 if (in_thread_list (inferior_ptid))
13466 b->thread = pid_to_thread_id (inferior_ptid);
13467
13468 /* We're being called after following a fork. The new fork is
13469 selected as current, and unless this was a vfork will have a
13470 different program space from the original thread. Reset that
13471 as well. */
13472 b->loc->pspace = current_program_space;
13473 }
13474 }
13475
13476 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13477 If from_tty is nonzero, it prints a message to that effect,
13478 which ends with a period (no newline). */
13479
13480 void
13481 set_ignore_count (int bptnum, int count, int from_tty)
13482 {
13483 struct breakpoint *b;
13484
13485 if (count < 0)
13486 count = 0;
13487
13488 ALL_BREAKPOINTS (b)
13489 if (b->number == bptnum)
13490 {
13491 if (is_tracepoint (b))
13492 {
13493 if (from_tty && count != 0)
13494 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13495 bptnum);
13496 return;
13497 }
13498
13499 b->ignore_count = count;
13500 if (from_tty)
13501 {
13502 if (count == 0)
13503 printf_filtered (_("Will stop next time "
13504 "breakpoint %d is reached."),
13505 bptnum);
13506 else if (count == 1)
13507 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13508 bptnum);
13509 else
13510 printf_filtered (_("Will ignore next %d "
13511 "crossings of breakpoint %d."),
13512 count, bptnum);
13513 }
13514 breakpoints_changed ();
13515 observer_notify_breakpoint_modified (b);
13516 return;
13517 }
13518
13519 error (_("No breakpoint number %d."), bptnum);
13520 }
13521
13522 /* Command to set ignore-count of breakpoint N to COUNT. */
13523
13524 static void
13525 ignore_command (char *args, int from_tty)
13526 {
13527 char *p = args;
13528 int num;
13529
13530 if (p == 0)
13531 error_no_arg (_("a breakpoint number"));
13532
13533 num = get_number (&p);
13534 if (num == 0)
13535 error (_("bad breakpoint number: '%s'"), args);
13536 if (*p == 0)
13537 error (_("Second argument (specified ignore-count) is missing."));
13538
13539 set_ignore_count (num,
13540 longest_to_int (value_as_long (parse_and_eval (p))),
13541 from_tty);
13542 if (from_tty)
13543 printf_filtered ("\n");
13544 }
13545 \f
13546 /* Call FUNCTION on each of the breakpoints
13547 whose numbers are given in ARGS. */
13548
13549 static void
13550 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
13551 void *),
13552 void *data)
13553 {
13554 int num;
13555 struct breakpoint *b, *tmp;
13556 int match;
13557 struct get_number_or_range_state state;
13558
13559 if (args == 0)
13560 error_no_arg (_("one or more breakpoint numbers"));
13561
13562 init_number_or_range (&state, args);
13563
13564 while (!state.finished)
13565 {
13566 char *p = state.string;
13567
13568 match = 0;
13569
13570 num = get_number_or_range (&state);
13571 if (num == 0)
13572 {
13573 warning (_("bad breakpoint number at or near '%s'"), p);
13574 }
13575 else
13576 {
13577 ALL_BREAKPOINTS_SAFE (b, tmp)
13578 if (b->number == num)
13579 {
13580 match = 1;
13581 function (b, data);
13582 break;
13583 }
13584 if (match == 0)
13585 printf_unfiltered (_("No breakpoint number %d.\n"), num);
13586 }
13587 }
13588 }
13589
13590 static struct bp_location *
13591 find_location_by_number (char *number)
13592 {
13593 char *dot = strchr (number, '.');
13594 char *p1;
13595 int bp_num;
13596 int loc_num;
13597 struct breakpoint *b;
13598 struct bp_location *loc;
13599
13600 *dot = '\0';
13601
13602 p1 = number;
13603 bp_num = get_number (&p1);
13604 if (bp_num == 0)
13605 error (_("Bad breakpoint number '%s'"), number);
13606
13607 ALL_BREAKPOINTS (b)
13608 if (b->number == bp_num)
13609 {
13610 break;
13611 }
13612
13613 if (!b || b->number != bp_num)
13614 error (_("Bad breakpoint number '%s'"), number);
13615
13616 p1 = dot+1;
13617 loc_num = get_number (&p1);
13618 if (loc_num == 0)
13619 error (_("Bad breakpoint location number '%s'"), number);
13620
13621 --loc_num;
13622 loc = b->loc;
13623 for (;loc_num && loc; --loc_num, loc = loc->next)
13624 ;
13625 if (!loc)
13626 error (_("Bad breakpoint location number '%s'"), dot+1);
13627
13628 return loc;
13629 }
13630
13631
13632 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13633 If from_tty is nonzero, it prints a message to that effect,
13634 which ends with a period (no newline). */
13635
13636 void
13637 disable_breakpoint (struct breakpoint *bpt)
13638 {
13639 /* Never disable a watchpoint scope breakpoint; we want to
13640 hit them when we leave scope so we can delete both the
13641 watchpoint and its scope breakpoint at that time. */
13642 if (bpt->type == bp_watchpoint_scope)
13643 return;
13644
13645 /* You can't disable permanent breakpoints. */
13646 if (bpt->enable_state == bp_permanent)
13647 return;
13648
13649 bpt->enable_state = bp_disabled;
13650
13651 /* Mark breakpoint locations modified. */
13652 mark_breakpoint_modified (bpt);
13653
13654 if (target_supports_enable_disable_tracepoint ()
13655 && current_trace_status ()->running && is_tracepoint (bpt))
13656 {
13657 struct bp_location *location;
13658
13659 for (location = bpt->loc; location; location = location->next)
13660 target_disable_tracepoint (location);
13661 }
13662
13663 update_global_location_list (0);
13664
13665 observer_notify_breakpoint_modified (bpt);
13666 }
13667
13668 /* A callback for iterate_over_related_breakpoints. */
13669
13670 static void
13671 do_disable_breakpoint (struct breakpoint *b, void *ignore)
13672 {
13673 disable_breakpoint (b);
13674 }
13675
13676 /* A callback for map_breakpoint_numbers that calls
13677 disable_breakpoint. */
13678
13679 static void
13680 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
13681 {
13682 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
13683 }
13684
13685 static void
13686 disable_command (char *args, int from_tty)
13687 {
13688 if (args == 0)
13689 {
13690 struct breakpoint *bpt;
13691
13692 ALL_BREAKPOINTS (bpt)
13693 if (user_breakpoint_p (bpt))
13694 disable_breakpoint (bpt);
13695 }
13696 else if (strchr (args, '.'))
13697 {
13698 struct bp_location *loc = find_location_by_number (args);
13699 if (loc)
13700 {
13701 if (loc->enabled)
13702 {
13703 loc->enabled = 0;
13704 mark_breakpoint_location_modified (loc);
13705 }
13706 if (target_supports_enable_disable_tracepoint ()
13707 && current_trace_status ()->running && loc->owner
13708 && is_tracepoint (loc->owner))
13709 target_disable_tracepoint (loc);
13710 }
13711 update_global_location_list (0);
13712 }
13713 else
13714 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
13715 }
13716
13717 static void
13718 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13719 int count)
13720 {
13721 int target_resources_ok;
13722
13723 if (bpt->type == bp_hardware_breakpoint)
13724 {
13725 int i;
13726 i = hw_breakpoint_used_count ();
13727 target_resources_ok =
13728 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13729 i + 1, 0);
13730 if (target_resources_ok == 0)
13731 error (_("No hardware breakpoint support in the target."));
13732 else if (target_resources_ok < 0)
13733 error (_("Hardware breakpoints used exceeds limit."));
13734 }
13735
13736 if (is_watchpoint (bpt))
13737 {
13738 /* Initialize it just to avoid a GCC false warning. */
13739 enum enable_state orig_enable_state = 0;
13740 volatile struct gdb_exception e;
13741
13742 TRY_CATCH (e, RETURN_MASK_ALL)
13743 {
13744 struct watchpoint *w = (struct watchpoint *) bpt;
13745
13746 orig_enable_state = bpt->enable_state;
13747 bpt->enable_state = bp_enabled;
13748 update_watchpoint (w, 1 /* reparse */);
13749 }
13750 if (e.reason < 0)
13751 {
13752 bpt->enable_state = orig_enable_state;
13753 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13754 bpt->number);
13755 return;
13756 }
13757 }
13758
13759 if (bpt->enable_state != bp_permanent)
13760 bpt->enable_state = bp_enabled;
13761
13762 bpt->enable_state = bp_enabled;
13763
13764 /* Mark breakpoint locations modified. */
13765 mark_breakpoint_modified (bpt);
13766
13767 if (target_supports_enable_disable_tracepoint ()
13768 && current_trace_status ()->running && is_tracepoint (bpt))
13769 {
13770 struct bp_location *location;
13771
13772 for (location = bpt->loc; location; location = location->next)
13773 target_enable_tracepoint (location);
13774 }
13775
13776 bpt->disposition = disposition;
13777 bpt->enable_count = count;
13778 update_global_location_list (1);
13779 breakpoints_changed ();
13780
13781 observer_notify_breakpoint_modified (bpt);
13782 }
13783
13784
13785 void
13786 enable_breakpoint (struct breakpoint *bpt)
13787 {
13788 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13789 }
13790
13791 static void
13792 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
13793 {
13794 enable_breakpoint (bpt);
13795 }
13796
13797 /* A callback for map_breakpoint_numbers that calls
13798 enable_breakpoint. */
13799
13800 static void
13801 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
13802 {
13803 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
13804 }
13805
13806 /* The enable command enables the specified breakpoints (or all defined
13807 breakpoints) so they once again become (or continue to be) effective
13808 in stopping the inferior. */
13809
13810 static void
13811 enable_command (char *args, int from_tty)
13812 {
13813 if (args == 0)
13814 {
13815 struct breakpoint *bpt;
13816
13817 ALL_BREAKPOINTS (bpt)
13818 if (user_breakpoint_p (bpt))
13819 enable_breakpoint (bpt);
13820 }
13821 else if (strchr (args, '.'))
13822 {
13823 struct bp_location *loc = find_location_by_number (args);
13824 if (loc)
13825 {
13826 if (!loc->enabled)
13827 {
13828 loc->enabled = 1;
13829 mark_breakpoint_location_modified (loc);
13830 }
13831 if (target_supports_enable_disable_tracepoint ()
13832 && current_trace_status ()->running && loc->owner
13833 && is_tracepoint (loc->owner))
13834 target_enable_tracepoint (loc);
13835 }
13836 update_global_location_list (1);
13837 }
13838 else
13839 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
13840 }
13841
13842 /* This struct packages up disposition data for application to multiple
13843 breakpoints. */
13844
13845 struct disp_data
13846 {
13847 enum bpdisp disp;
13848 int count;
13849 };
13850
13851 static void
13852 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
13853 {
13854 struct disp_data disp_data = *(struct disp_data *) arg;
13855
13856 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
13857 }
13858
13859 static void
13860 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
13861 {
13862 struct disp_data disp = { disp_disable, 1 };
13863
13864 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
13865 }
13866
13867 static void
13868 enable_once_command (char *args, int from_tty)
13869 {
13870 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
13871 }
13872
13873 static void
13874 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
13875 {
13876 struct disp_data disp = { disp_disable, *(int *) countptr };
13877
13878 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
13879 }
13880
13881 static void
13882 enable_count_command (char *args, int from_tty)
13883 {
13884 int count = get_number (&args);
13885
13886 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
13887 }
13888
13889 static void
13890 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
13891 {
13892 struct disp_data disp = { disp_del, 1 };
13893
13894 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
13895 }
13896
13897 static void
13898 enable_delete_command (char *args, int from_tty)
13899 {
13900 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
13901 }
13902 \f
13903 static void
13904 set_breakpoint_cmd (char *args, int from_tty)
13905 {
13906 }
13907
13908 static void
13909 show_breakpoint_cmd (char *args, int from_tty)
13910 {
13911 }
13912
13913 /* Invalidate last known value of any hardware watchpoint if
13914 the memory which that value represents has been written to by
13915 GDB itself. */
13916
13917 static void
13918 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
13919 const bfd_byte *data)
13920 {
13921 struct breakpoint *bp;
13922
13923 ALL_BREAKPOINTS (bp)
13924 if (bp->enable_state == bp_enabled
13925 && bp->type == bp_hardware_watchpoint)
13926 {
13927 struct watchpoint *wp = (struct watchpoint *) bp;
13928
13929 if (wp->val_valid && wp->val)
13930 {
13931 struct bp_location *loc;
13932
13933 for (loc = bp->loc; loc != NULL; loc = loc->next)
13934 if (loc->loc_type == bp_loc_hardware_watchpoint
13935 && loc->address + loc->length > addr
13936 && addr + len > loc->address)
13937 {
13938 value_free (wp->val);
13939 wp->val = NULL;
13940 wp->val_valid = 0;
13941 }
13942 }
13943 }
13944 }
13945
13946 /* Use the last displayed codepoint's values, or nothing
13947 if they aren't valid. */
13948
13949 struct symtabs_and_lines
13950 decode_line_spec_1 (char *string, int flags)
13951 {
13952 struct symtabs_and_lines sals;
13953
13954 if (string == 0)
13955 error (_("Empty line specification."));
13956 if (last_displayed_sal_is_valid ())
13957 sals = decode_line_1 (&string, flags,
13958 get_last_displayed_symtab (),
13959 get_last_displayed_line ());
13960 else
13961 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
13962 if (*string)
13963 error (_("Junk at end of line specification: %s"), string);
13964 return sals;
13965 }
13966
13967 /* Create and insert a raw software breakpoint at PC. Return an
13968 identifier, which should be used to remove the breakpoint later.
13969 In general, places which call this should be using something on the
13970 breakpoint chain instead; this function should be eliminated
13971 someday. */
13972
13973 void *
13974 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
13975 struct address_space *aspace, CORE_ADDR pc)
13976 {
13977 struct bp_target_info *bp_tgt;
13978
13979 bp_tgt = XZALLOC (struct bp_target_info);
13980
13981 bp_tgt->placed_address_space = aspace;
13982 bp_tgt->placed_address = pc;
13983
13984 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
13985 {
13986 /* Could not insert the breakpoint. */
13987 xfree (bp_tgt);
13988 return NULL;
13989 }
13990
13991 return bp_tgt;
13992 }
13993
13994 /* Remove a breakpoint BP inserted by
13995 deprecated_insert_raw_breakpoint. */
13996
13997 int
13998 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
13999 {
14000 struct bp_target_info *bp_tgt = bp;
14001 int ret;
14002
14003 ret = target_remove_breakpoint (gdbarch, bp_tgt);
14004 xfree (bp_tgt);
14005
14006 return ret;
14007 }
14008
14009 /* One (or perhaps two) breakpoints used for software single
14010 stepping. */
14011
14012 static void *single_step_breakpoints[2];
14013 static struct gdbarch *single_step_gdbarch[2];
14014
14015 /* Create and insert a breakpoint for software single step. */
14016
14017 void
14018 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14019 struct address_space *aspace,
14020 CORE_ADDR next_pc)
14021 {
14022 void **bpt_p;
14023
14024 if (single_step_breakpoints[0] == NULL)
14025 {
14026 bpt_p = &single_step_breakpoints[0];
14027 single_step_gdbarch[0] = gdbarch;
14028 }
14029 else
14030 {
14031 gdb_assert (single_step_breakpoints[1] == NULL);
14032 bpt_p = &single_step_breakpoints[1];
14033 single_step_gdbarch[1] = gdbarch;
14034 }
14035
14036 /* NOTE drow/2006-04-11: A future improvement to this function would
14037 be to only create the breakpoints once, and actually put them on
14038 the breakpoint chain. That would let us use set_raw_breakpoint.
14039 We could adjust the addresses each time they were needed. Doing
14040 this requires corresponding changes elsewhere where single step
14041 breakpoints are handled, however. So, for now, we use this. */
14042
14043 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
14044 if (*bpt_p == NULL)
14045 error (_("Could not insert single-step breakpoint at %s"),
14046 paddress (gdbarch, next_pc));
14047 }
14048
14049 /* Check if the breakpoints used for software single stepping
14050 were inserted or not. */
14051
14052 int
14053 single_step_breakpoints_inserted (void)
14054 {
14055 return (single_step_breakpoints[0] != NULL
14056 || single_step_breakpoints[1] != NULL);
14057 }
14058
14059 /* Remove and delete any breakpoints used for software single step. */
14060
14061 void
14062 remove_single_step_breakpoints (void)
14063 {
14064 gdb_assert (single_step_breakpoints[0] != NULL);
14065
14066 /* See insert_single_step_breakpoint for more about this deprecated
14067 call. */
14068 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14069 single_step_breakpoints[0]);
14070 single_step_gdbarch[0] = NULL;
14071 single_step_breakpoints[0] = NULL;
14072
14073 if (single_step_breakpoints[1] != NULL)
14074 {
14075 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14076 single_step_breakpoints[1]);
14077 single_step_gdbarch[1] = NULL;
14078 single_step_breakpoints[1] = NULL;
14079 }
14080 }
14081
14082 /* Delete software single step breakpoints without removing them from
14083 the inferior. This is intended to be used if the inferior's address
14084 space where they were inserted is already gone, e.g. after exit or
14085 exec. */
14086
14087 void
14088 cancel_single_step_breakpoints (void)
14089 {
14090 int i;
14091
14092 for (i = 0; i < 2; i++)
14093 if (single_step_breakpoints[i])
14094 {
14095 xfree (single_step_breakpoints[i]);
14096 single_step_breakpoints[i] = NULL;
14097 single_step_gdbarch[i] = NULL;
14098 }
14099 }
14100
14101 /* Detach software single-step breakpoints from INFERIOR_PTID without
14102 removing them. */
14103
14104 static void
14105 detach_single_step_breakpoints (void)
14106 {
14107 int i;
14108
14109 for (i = 0; i < 2; i++)
14110 if (single_step_breakpoints[i])
14111 target_remove_breakpoint (single_step_gdbarch[i],
14112 single_step_breakpoints[i]);
14113 }
14114
14115 /* Check whether a software single-step breakpoint is inserted at
14116 PC. */
14117
14118 static int
14119 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14120 CORE_ADDR pc)
14121 {
14122 int i;
14123
14124 for (i = 0; i < 2; i++)
14125 {
14126 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
14127 if (bp_tgt
14128 && breakpoint_address_match (bp_tgt->placed_address_space,
14129 bp_tgt->placed_address,
14130 aspace, pc))
14131 return 1;
14132 }
14133
14134 return 0;
14135 }
14136
14137 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
14138 non-zero otherwise. */
14139 static int
14140 is_syscall_catchpoint_enabled (struct breakpoint *bp)
14141 {
14142 if (syscall_catchpoint_p (bp)
14143 && bp->enable_state != bp_disabled
14144 && bp->enable_state != bp_call_disabled)
14145 return 1;
14146 else
14147 return 0;
14148 }
14149
14150 int
14151 catch_syscall_enabled (void)
14152 {
14153 struct catch_syscall_inferior_data *inf_data
14154 = get_catch_syscall_inferior_data (current_inferior ());
14155
14156 return inf_data->total_syscalls_count != 0;
14157 }
14158
14159 int
14160 catching_syscall_number (int syscall_number)
14161 {
14162 struct breakpoint *bp;
14163
14164 ALL_BREAKPOINTS (bp)
14165 if (is_syscall_catchpoint_enabled (bp))
14166 {
14167 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14168
14169 if (c->syscalls_to_be_caught)
14170 {
14171 int i, iter;
14172 for (i = 0;
14173 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
14174 i++)
14175 if (syscall_number == iter)
14176 return 1;
14177 }
14178 else
14179 return 1;
14180 }
14181
14182 return 0;
14183 }
14184
14185 /* Complete syscall names. Used by "catch syscall". */
14186 static char **
14187 catch_syscall_completer (struct cmd_list_element *cmd,
14188 char *text, char *word)
14189 {
14190 const char **list = get_syscall_names ();
14191 char **retlist
14192 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
14193
14194 xfree (list);
14195 return retlist;
14196 }
14197
14198 /* Tracepoint-specific operations. */
14199
14200 /* Set tracepoint count to NUM. */
14201 static void
14202 set_tracepoint_count (int num)
14203 {
14204 tracepoint_count = num;
14205 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14206 }
14207
14208 static void
14209 trace_command (char *arg, int from_tty)
14210 {
14211 if (create_breakpoint (get_current_arch (),
14212 arg,
14213 NULL, 0, 1 /* parse arg */,
14214 0 /* tempflag */,
14215 bp_tracepoint /* type_wanted */,
14216 0 /* Ignore count */,
14217 pending_break_support,
14218 &tracepoint_breakpoint_ops,
14219 from_tty,
14220 1 /* enabled */,
14221 0 /* internal */, 0))
14222 set_tracepoint_count (breakpoint_count);
14223 }
14224
14225 static void
14226 ftrace_command (char *arg, int from_tty)
14227 {
14228 if (create_breakpoint (get_current_arch (),
14229 arg,
14230 NULL, 0, 1 /* parse arg */,
14231 0 /* tempflag */,
14232 bp_fast_tracepoint /* type_wanted */,
14233 0 /* Ignore count */,
14234 pending_break_support,
14235 &tracepoint_breakpoint_ops,
14236 from_tty,
14237 1 /* enabled */,
14238 0 /* internal */, 0))
14239 set_tracepoint_count (breakpoint_count);
14240 }
14241
14242 /* strace command implementation. Creates a static tracepoint. */
14243
14244 static void
14245 strace_command (char *arg, int from_tty)
14246 {
14247 struct breakpoint_ops *ops;
14248
14249 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14250 or with a normal static tracepoint. */
14251 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14252 ops = &strace_marker_breakpoint_ops;
14253 else
14254 ops = &tracepoint_breakpoint_ops;
14255
14256 if (create_breakpoint (get_current_arch (),
14257 arg,
14258 NULL, 0, 1 /* parse arg */,
14259 0 /* tempflag */,
14260 bp_static_tracepoint /* type_wanted */,
14261 0 /* Ignore count */,
14262 pending_break_support,
14263 ops,
14264 from_tty,
14265 1 /* enabled */,
14266 0 /* internal */, 0))
14267 set_tracepoint_count (breakpoint_count);
14268 }
14269
14270 /* Set up a fake reader function that gets command lines from a linked
14271 list that was acquired during tracepoint uploading. */
14272
14273 static struct uploaded_tp *this_utp;
14274 static int next_cmd;
14275
14276 static char *
14277 read_uploaded_action (void)
14278 {
14279 char *rslt;
14280
14281 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14282
14283 next_cmd++;
14284
14285 return rslt;
14286 }
14287
14288 /* Given information about a tracepoint as recorded on a target (which
14289 can be either a live system or a trace file), attempt to create an
14290 equivalent GDB tracepoint. This is not a reliable process, since
14291 the target does not necessarily have all the information used when
14292 the tracepoint was originally defined. */
14293
14294 struct tracepoint *
14295 create_tracepoint_from_upload (struct uploaded_tp *utp)
14296 {
14297 char *addr_str, small_buf[100];
14298 struct tracepoint *tp;
14299
14300 if (utp->at_string)
14301 addr_str = utp->at_string;
14302 else
14303 {
14304 /* In the absence of a source location, fall back to raw
14305 address. Since there is no way to confirm that the address
14306 means the same thing as when the trace was started, warn the
14307 user. */
14308 warning (_("Uploaded tracepoint %d has no "
14309 "source location, using raw address"),
14310 utp->number);
14311 sprintf (small_buf, "*%s", hex_string (utp->addr));
14312 addr_str = small_buf;
14313 }
14314
14315 /* There's not much we can do with a sequence of bytecodes. */
14316 if (utp->cond && !utp->cond_string)
14317 warning (_("Uploaded tracepoint %d condition "
14318 "has no source form, ignoring it"),
14319 utp->number);
14320
14321 if (!create_breakpoint (get_current_arch (),
14322 addr_str,
14323 utp->cond_string, -1, 0 /* parse cond/thread */,
14324 0 /* tempflag */,
14325 utp->type /* type_wanted */,
14326 0 /* Ignore count */,
14327 pending_break_support,
14328 &tracepoint_breakpoint_ops,
14329 0 /* from_tty */,
14330 utp->enabled /* enabled */,
14331 0 /* internal */,
14332 CREATE_BREAKPOINT_FLAGS_INSERTED))
14333 return NULL;
14334
14335 set_tracepoint_count (breakpoint_count);
14336
14337 /* Get the tracepoint we just created. */
14338 tp = get_tracepoint (tracepoint_count);
14339 gdb_assert (tp != NULL);
14340
14341 if (utp->pass > 0)
14342 {
14343 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
14344
14345 trace_pass_command (small_buf, 0);
14346 }
14347
14348 /* If we have uploaded versions of the original commands, set up a
14349 special-purpose "reader" function and call the usual command line
14350 reader, then pass the result to the breakpoint command-setting
14351 function. */
14352 if (!VEC_empty (char_ptr, utp->cmd_strings))
14353 {
14354 struct command_line *cmd_list;
14355
14356 this_utp = utp;
14357 next_cmd = 0;
14358
14359 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14360
14361 breakpoint_set_commands (&tp->base, cmd_list);
14362 }
14363 else if (!VEC_empty (char_ptr, utp->actions)
14364 || !VEC_empty (char_ptr, utp->step_actions))
14365 warning (_("Uploaded tracepoint %d actions "
14366 "have no source form, ignoring them"),
14367 utp->number);
14368
14369 /* Copy any status information that might be available. */
14370 tp->base.hit_count = utp->hit_count;
14371 tp->traceframe_usage = utp->traceframe_usage;
14372
14373 return tp;
14374 }
14375
14376 /* Print information on tracepoint number TPNUM_EXP, or all if
14377 omitted. */
14378
14379 static void
14380 tracepoints_info (char *args, int from_tty)
14381 {
14382 struct ui_out *uiout = current_uiout;
14383 int num_printed;
14384
14385 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14386
14387 if (num_printed == 0)
14388 {
14389 if (args == NULL || *args == '\0')
14390 ui_out_message (uiout, 0, "No tracepoints.\n");
14391 else
14392 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
14393 }
14394
14395 default_collect_info ();
14396 }
14397
14398 /* The 'enable trace' command enables tracepoints.
14399 Not supported by all targets. */
14400 static void
14401 enable_trace_command (char *args, int from_tty)
14402 {
14403 enable_command (args, from_tty);
14404 }
14405
14406 /* The 'disable trace' command disables tracepoints.
14407 Not supported by all targets. */
14408 static void
14409 disable_trace_command (char *args, int from_tty)
14410 {
14411 disable_command (args, from_tty);
14412 }
14413
14414 /* Remove a tracepoint (or all if no argument). */
14415 static void
14416 delete_trace_command (char *arg, int from_tty)
14417 {
14418 struct breakpoint *b, *b_tmp;
14419
14420 dont_repeat ();
14421
14422 if (arg == 0)
14423 {
14424 int breaks_to_delete = 0;
14425
14426 /* Delete all breakpoints if no argument.
14427 Do not delete internal or call-dummy breakpoints, these
14428 have to be deleted with an explicit breakpoint number
14429 argument. */
14430 ALL_TRACEPOINTS (b)
14431 if (is_tracepoint (b) && user_breakpoint_p (b))
14432 {
14433 breaks_to_delete = 1;
14434 break;
14435 }
14436
14437 /* Ask user only if there are some breakpoints to delete. */
14438 if (!from_tty
14439 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14440 {
14441 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14442 if (is_tracepoint (b) && user_breakpoint_p (b))
14443 delete_breakpoint (b);
14444 }
14445 }
14446 else
14447 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14448 }
14449
14450 /* Helper function for trace_pass_command. */
14451
14452 static void
14453 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14454 {
14455 tp->pass_count = count;
14456 observer_notify_tracepoint_modified (tp->base.number);
14457 if (from_tty)
14458 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14459 tp->base.number, count);
14460 }
14461
14462 /* Set passcount for tracepoint.
14463
14464 First command argument is passcount, second is tracepoint number.
14465 If tracepoint number omitted, apply to most recently defined.
14466 Also accepts special argument "all". */
14467
14468 static void
14469 trace_pass_command (char *args, int from_tty)
14470 {
14471 struct tracepoint *t1;
14472 unsigned int count;
14473
14474 if (args == 0 || *args == 0)
14475 error (_("passcount command requires an "
14476 "argument (count + optional TP num)"));
14477
14478 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
14479
14480 while (*args && isspace ((int) *args))
14481 args++;
14482
14483 if (*args && strncasecmp (args, "all", 3) == 0)
14484 {
14485 struct breakpoint *b;
14486
14487 args += 3; /* Skip special argument "all". */
14488 if (*args)
14489 error (_("Junk at end of arguments."));
14490
14491 ALL_TRACEPOINTS (b)
14492 {
14493 t1 = (struct tracepoint *) b;
14494 trace_pass_set_count (t1, count, from_tty);
14495 }
14496 }
14497 else if (*args == '\0')
14498 {
14499 t1 = get_tracepoint_by_number (&args, NULL, 1);
14500 if (t1)
14501 trace_pass_set_count (t1, count, from_tty);
14502 }
14503 else
14504 {
14505 struct get_number_or_range_state state;
14506
14507 init_number_or_range (&state, args);
14508 while (!state.finished)
14509 {
14510 t1 = get_tracepoint_by_number (&args, &state, 1);
14511 if (t1)
14512 trace_pass_set_count (t1, count, from_tty);
14513 }
14514 }
14515 }
14516
14517 struct tracepoint *
14518 get_tracepoint (int num)
14519 {
14520 struct breakpoint *t;
14521
14522 ALL_TRACEPOINTS (t)
14523 if (t->number == num)
14524 return (struct tracepoint *) t;
14525
14526 return NULL;
14527 }
14528
14529 /* Find the tracepoint with the given target-side number (which may be
14530 different from the tracepoint number after disconnecting and
14531 reconnecting). */
14532
14533 struct tracepoint *
14534 get_tracepoint_by_number_on_target (int num)
14535 {
14536 struct breakpoint *b;
14537
14538 ALL_TRACEPOINTS (b)
14539 {
14540 struct tracepoint *t = (struct tracepoint *) b;
14541
14542 if (t->number_on_target == num)
14543 return t;
14544 }
14545
14546 return NULL;
14547 }
14548
14549 /* Utility: parse a tracepoint number and look it up in the list.
14550 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14551 If OPTIONAL_P is true, then if the argument is missing, the most
14552 recent tracepoint (tracepoint_count) is returned. */
14553 struct tracepoint *
14554 get_tracepoint_by_number (char **arg,
14555 struct get_number_or_range_state *state,
14556 int optional_p)
14557 {
14558 extern int tracepoint_count;
14559 struct breakpoint *t;
14560 int tpnum;
14561 char *instring = arg == NULL ? NULL : *arg;
14562
14563 if (state)
14564 {
14565 gdb_assert (!state->finished);
14566 tpnum = get_number_or_range (state);
14567 }
14568 else if (arg == NULL || *arg == NULL || ! **arg)
14569 {
14570 if (optional_p)
14571 tpnum = tracepoint_count;
14572 else
14573 error_no_arg (_("tracepoint number"));
14574 }
14575 else
14576 tpnum = get_number (arg);
14577
14578 if (tpnum <= 0)
14579 {
14580 if (instring && *instring)
14581 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14582 instring);
14583 else
14584 printf_filtered (_("Tracepoint argument missing "
14585 "and no previous tracepoint\n"));
14586 return NULL;
14587 }
14588
14589 ALL_TRACEPOINTS (t)
14590 if (t->number == tpnum)
14591 {
14592 return (struct tracepoint *) t;
14593 }
14594
14595 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14596 return NULL;
14597 }
14598
14599 void
14600 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14601 {
14602 if (b->thread != -1)
14603 fprintf_unfiltered (fp, " thread %d", b->thread);
14604
14605 if (b->task != 0)
14606 fprintf_unfiltered (fp, " task %d", b->task);
14607
14608 fprintf_unfiltered (fp, "\n");
14609 }
14610
14611 /* Save information on user settable breakpoints (watchpoints, etc) to
14612 a new script file named FILENAME. If FILTER is non-NULL, call it
14613 on each breakpoint and only include the ones for which it returns
14614 non-zero. */
14615
14616 static void
14617 save_breakpoints (char *filename, int from_tty,
14618 int (*filter) (const struct breakpoint *))
14619 {
14620 struct breakpoint *tp;
14621 int any = 0;
14622 char *pathname;
14623 struct cleanup *cleanup;
14624 struct ui_file *fp;
14625 int extra_trace_bits = 0;
14626
14627 if (filename == 0 || *filename == 0)
14628 error (_("Argument required (file name in which to save)"));
14629
14630 /* See if we have anything to save. */
14631 ALL_BREAKPOINTS (tp)
14632 {
14633 /* Skip internal and momentary breakpoints. */
14634 if (!user_breakpoint_p (tp))
14635 continue;
14636
14637 /* If we have a filter, only save the breakpoints it accepts. */
14638 if (filter && !filter (tp))
14639 continue;
14640
14641 any = 1;
14642
14643 if (is_tracepoint (tp))
14644 {
14645 extra_trace_bits = 1;
14646
14647 /* We can stop searching. */
14648 break;
14649 }
14650 }
14651
14652 if (!any)
14653 {
14654 warning (_("Nothing to save."));
14655 return;
14656 }
14657
14658 pathname = tilde_expand (filename);
14659 cleanup = make_cleanup (xfree, pathname);
14660 fp = gdb_fopen (pathname, "w");
14661 if (!fp)
14662 error (_("Unable to open file '%s' for saving (%s)"),
14663 filename, safe_strerror (errno));
14664 make_cleanup_ui_file_delete (fp);
14665
14666 if (extra_trace_bits)
14667 save_trace_state_variables (fp);
14668
14669 ALL_BREAKPOINTS (tp)
14670 {
14671 /* Skip internal and momentary breakpoints. */
14672 if (!user_breakpoint_p (tp))
14673 continue;
14674
14675 /* If we have a filter, only save the breakpoints it accepts. */
14676 if (filter && !filter (tp))
14677 continue;
14678
14679 tp->ops->print_recreate (tp, fp);
14680
14681 /* Note, we can't rely on tp->number for anything, as we can't
14682 assume the recreated breakpoint numbers will match. Use $bpnum
14683 instead. */
14684
14685 if (tp->cond_string)
14686 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
14687
14688 if (tp->ignore_count)
14689 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
14690
14691 if (tp->commands)
14692 {
14693 volatile struct gdb_exception ex;
14694
14695 fprintf_unfiltered (fp, " commands\n");
14696
14697 ui_out_redirect (current_uiout, fp);
14698 TRY_CATCH (ex, RETURN_MASK_ALL)
14699 {
14700 print_command_lines (current_uiout, tp->commands->commands, 2);
14701 }
14702 ui_out_redirect (current_uiout, NULL);
14703
14704 if (ex.reason < 0)
14705 throw_exception (ex);
14706
14707 fprintf_unfiltered (fp, " end\n");
14708 }
14709
14710 if (tp->enable_state == bp_disabled)
14711 fprintf_unfiltered (fp, "disable\n");
14712
14713 /* If this is a multi-location breakpoint, check if the locations
14714 should be individually disabled. Watchpoint locations are
14715 special, and not user visible. */
14716 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14717 {
14718 struct bp_location *loc;
14719 int n = 1;
14720
14721 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
14722 if (!loc->enabled)
14723 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
14724 }
14725 }
14726
14727 if (extra_trace_bits && *default_collect)
14728 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
14729
14730 do_cleanups (cleanup);
14731 if (from_tty)
14732 printf_filtered (_("Saved to file '%s'.\n"), filename);
14733 }
14734
14735 /* The `save breakpoints' command. */
14736
14737 static void
14738 save_breakpoints_command (char *args, int from_tty)
14739 {
14740 save_breakpoints (args, from_tty, NULL);
14741 }
14742
14743 /* The `save tracepoints' command. */
14744
14745 static void
14746 save_tracepoints_command (char *args, int from_tty)
14747 {
14748 save_breakpoints (args, from_tty, is_tracepoint);
14749 }
14750
14751 /* Create a vector of all tracepoints. */
14752
14753 VEC(breakpoint_p) *
14754 all_tracepoints (void)
14755 {
14756 VEC(breakpoint_p) *tp_vec = 0;
14757 struct breakpoint *tp;
14758
14759 ALL_TRACEPOINTS (tp)
14760 {
14761 VEC_safe_push (breakpoint_p, tp_vec, tp);
14762 }
14763
14764 return tp_vec;
14765 }
14766
14767 \f
14768 /* This help string is used for the break, hbreak, tbreak and thbreak
14769 commands. It is defined as a macro to prevent duplication.
14770 COMMAND should be a string constant containing the name of the
14771 command. */
14772 #define BREAK_ARGS_HELP(command) \
14773 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
14774 LOCATION may be a line number, function name, or \"*\" and an address.\n\
14775 If a line number is specified, break at start of code for that line.\n\
14776 If a function is specified, break at start of code for that function.\n\
14777 If an address is specified, break at that exact address.\n\
14778 With no LOCATION, uses current execution address of the selected\n\
14779 stack frame. This is useful for breaking on return to a stack frame.\n\
14780 \n\
14781 THREADNUM is the number from \"info threads\".\n\
14782 CONDITION is a boolean expression.\n\
14783 \n\
14784 Multiple breakpoints at one place are permitted, and useful if their\n\
14785 conditions are different.\n\
14786 \n\
14787 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14788
14789 /* List of subcommands for "catch". */
14790 static struct cmd_list_element *catch_cmdlist;
14791
14792 /* List of subcommands for "tcatch". */
14793 static struct cmd_list_element *tcatch_cmdlist;
14794
14795 void
14796 add_catch_command (char *name, char *docstring,
14797 void (*sfunc) (char *args, int from_tty,
14798 struct cmd_list_element *command),
14799 char **(*completer) (struct cmd_list_element *cmd,
14800 char *text, char *word),
14801 void *user_data_catch,
14802 void *user_data_tcatch)
14803 {
14804 struct cmd_list_element *command;
14805
14806 command = add_cmd (name, class_breakpoint, NULL, docstring,
14807 &catch_cmdlist);
14808 set_cmd_sfunc (command, sfunc);
14809 set_cmd_context (command, user_data_catch);
14810 set_cmd_completer (command, completer);
14811
14812 command = add_cmd (name, class_breakpoint, NULL, docstring,
14813 &tcatch_cmdlist);
14814 set_cmd_sfunc (command, sfunc);
14815 set_cmd_context (command, user_data_tcatch);
14816 set_cmd_completer (command, completer);
14817 }
14818
14819 static void
14820 clear_syscall_counts (struct inferior *inf)
14821 {
14822 struct catch_syscall_inferior_data *inf_data
14823 = get_catch_syscall_inferior_data (inf);
14824
14825 inf_data->total_syscalls_count = 0;
14826 inf_data->any_syscall_count = 0;
14827 VEC_free (int, inf_data->syscalls_counts);
14828 }
14829
14830 static void
14831 save_command (char *arg, int from_tty)
14832 {
14833 printf_unfiltered (_("\"save\" must be followed by "
14834 "the name of a save subcommand.\n"));
14835 help_list (save_cmdlist, "save ", -1, gdb_stdout);
14836 }
14837
14838 struct breakpoint *
14839 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
14840 void *data)
14841 {
14842 struct breakpoint *b, *b_tmp;
14843
14844 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14845 {
14846 if ((*callback) (b, data))
14847 return b;
14848 }
14849
14850 return NULL;
14851 }
14852
14853 /* Zero if any of the breakpoint's locations could be a location where
14854 functions have been inlined, nonzero otherwise. */
14855
14856 static int
14857 is_non_inline_function (struct breakpoint *b)
14858 {
14859 /* The shared library event breakpoint is set on the address of a
14860 non-inline function. */
14861 if (b->type == bp_shlib_event)
14862 return 1;
14863
14864 return 0;
14865 }
14866
14867 /* Nonzero if the specified PC cannot be a location where functions
14868 have been inlined. */
14869
14870 int
14871 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
14872 const struct target_waitstatus *ws)
14873 {
14874 struct breakpoint *b;
14875 struct bp_location *bl;
14876
14877 ALL_BREAKPOINTS (b)
14878 {
14879 if (!is_non_inline_function (b))
14880 continue;
14881
14882 for (bl = b->loc; bl != NULL; bl = bl->next)
14883 {
14884 if (!bl->shlib_disabled
14885 && bpstat_check_location (bl, aspace, pc, ws))
14886 return 1;
14887 }
14888 }
14889
14890 return 0;
14891 }
14892
14893 void
14894 initialize_breakpoint_ops (void)
14895 {
14896 static int initialized = 0;
14897
14898 struct breakpoint_ops *ops;
14899
14900 if (initialized)
14901 return;
14902 initialized = 1;
14903
14904 /* The breakpoint_ops structure to be inherit by all kinds of
14905 breakpoints (real breakpoints, i.e., user "break" breakpoints,
14906 internal and momentary breakpoints, etc.). */
14907 ops = &bkpt_base_breakpoint_ops;
14908 *ops = base_breakpoint_ops;
14909 ops->re_set = bkpt_re_set;
14910 ops->insert_location = bkpt_insert_location;
14911 ops->remove_location = bkpt_remove_location;
14912 ops->breakpoint_hit = bkpt_breakpoint_hit;
14913 ops->create_sals_from_address = bkpt_create_sals_from_address;
14914 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
14915 ops->decode_linespec = bkpt_decode_linespec;
14916
14917 /* The breakpoint_ops structure to be used in regular breakpoints. */
14918 ops = &bkpt_breakpoint_ops;
14919 *ops = bkpt_base_breakpoint_ops;
14920 ops->re_set = bkpt_re_set;
14921 ops->resources_needed = bkpt_resources_needed;
14922 ops->print_it = bkpt_print_it;
14923 ops->print_mention = bkpt_print_mention;
14924 ops->print_recreate = bkpt_print_recreate;
14925
14926 /* Ranged breakpoints. */
14927 ops = &ranged_breakpoint_ops;
14928 *ops = bkpt_breakpoint_ops;
14929 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
14930 ops->resources_needed = resources_needed_ranged_breakpoint;
14931 ops->print_it = print_it_ranged_breakpoint;
14932 ops->print_one = print_one_ranged_breakpoint;
14933 ops->print_one_detail = print_one_detail_ranged_breakpoint;
14934 ops->print_mention = print_mention_ranged_breakpoint;
14935 ops->print_recreate = print_recreate_ranged_breakpoint;
14936
14937 /* Internal breakpoints. */
14938 ops = &internal_breakpoint_ops;
14939 *ops = bkpt_base_breakpoint_ops;
14940 ops->re_set = internal_bkpt_re_set;
14941 ops->check_status = internal_bkpt_check_status;
14942 ops->print_it = internal_bkpt_print_it;
14943 ops->print_mention = internal_bkpt_print_mention;
14944
14945 /* Momentary breakpoints. */
14946 ops = &momentary_breakpoint_ops;
14947 *ops = bkpt_base_breakpoint_ops;
14948 ops->re_set = momentary_bkpt_re_set;
14949 ops->check_status = momentary_bkpt_check_status;
14950 ops->print_it = momentary_bkpt_print_it;
14951 ops->print_mention = momentary_bkpt_print_mention;
14952
14953 /* GNU v3 exception catchpoints. */
14954 ops = &gnu_v3_exception_catchpoint_ops;
14955 *ops = bkpt_breakpoint_ops;
14956 ops->print_it = print_it_exception_catchpoint;
14957 ops->print_one = print_one_exception_catchpoint;
14958 ops->print_mention = print_mention_exception_catchpoint;
14959 ops->print_recreate = print_recreate_exception_catchpoint;
14960
14961 /* Watchpoints. */
14962 ops = &watchpoint_breakpoint_ops;
14963 *ops = base_breakpoint_ops;
14964 ops->dtor = dtor_watchpoint;
14965 ops->re_set = re_set_watchpoint;
14966 ops->insert_location = insert_watchpoint;
14967 ops->remove_location = remove_watchpoint;
14968 ops->breakpoint_hit = breakpoint_hit_watchpoint;
14969 ops->check_status = check_status_watchpoint;
14970 ops->resources_needed = resources_needed_watchpoint;
14971 ops->works_in_software_mode = works_in_software_mode_watchpoint;
14972 ops->print_it = print_it_watchpoint;
14973 ops->print_mention = print_mention_watchpoint;
14974 ops->print_recreate = print_recreate_watchpoint;
14975
14976 /* Masked watchpoints. */
14977 ops = &masked_watchpoint_breakpoint_ops;
14978 *ops = watchpoint_breakpoint_ops;
14979 ops->insert_location = insert_masked_watchpoint;
14980 ops->remove_location = remove_masked_watchpoint;
14981 ops->resources_needed = resources_needed_masked_watchpoint;
14982 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
14983 ops->print_it = print_it_masked_watchpoint;
14984 ops->print_one_detail = print_one_detail_masked_watchpoint;
14985 ops->print_mention = print_mention_masked_watchpoint;
14986 ops->print_recreate = print_recreate_masked_watchpoint;
14987
14988 /* Tracepoints. */
14989 ops = &tracepoint_breakpoint_ops;
14990 *ops = base_breakpoint_ops;
14991 ops->re_set = tracepoint_re_set;
14992 ops->breakpoint_hit = tracepoint_breakpoint_hit;
14993 ops->print_one_detail = tracepoint_print_one_detail;
14994 ops->print_mention = tracepoint_print_mention;
14995 ops->print_recreate = tracepoint_print_recreate;
14996 ops->create_sals_from_address = tracepoint_create_sals_from_address;
14997 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
14998 ops->decode_linespec = tracepoint_decode_linespec;
14999
15000 /* Static tracepoints with marker (`-m'). */
15001 ops = &strace_marker_breakpoint_ops;
15002 *ops = tracepoint_breakpoint_ops;
15003 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15004 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15005 ops->decode_linespec = strace_marker_decode_linespec;
15006
15007 /* Fork catchpoints. */
15008 ops = &catch_fork_breakpoint_ops;
15009 *ops = base_breakpoint_ops;
15010 ops->insert_location = insert_catch_fork;
15011 ops->remove_location = remove_catch_fork;
15012 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15013 ops->print_it = print_it_catch_fork;
15014 ops->print_one = print_one_catch_fork;
15015 ops->print_mention = print_mention_catch_fork;
15016 ops->print_recreate = print_recreate_catch_fork;
15017
15018 /* Vfork catchpoints. */
15019 ops = &catch_vfork_breakpoint_ops;
15020 *ops = base_breakpoint_ops;
15021 ops->insert_location = insert_catch_vfork;
15022 ops->remove_location = remove_catch_vfork;
15023 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15024 ops->print_it = print_it_catch_vfork;
15025 ops->print_one = print_one_catch_vfork;
15026 ops->print_mention = print_mention_catch_vfork;
15027 ops->print_recreate = print_recreate_catch_vfork;
15028
15029 /* Exec catchpoints. */
15030 ops = &catch_exec_breakpoint_ops;
15031 *ops = base_breakpoint_ops;
15032 ops->dtor = dtor_catch_exec;
15033 ops->insert_location = insert_catch_exec;
15034 ops->remove_location = remove_catch_exec;
15035 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15036 ops->print_it = print_it_catch_exec;
15037 ops->print_one = print_one_catch_exec;
15038 ops->print_mention = print_mention_catch_exec;
15039 ops->print_recreate = print_recreate_catch_exec;
15040
15041 /* Syscall catchpoints. */
15042 ops = &catch_syscall_breakpoint_ops;
15043 *ops = base_breakpoint_ops;
15044 ops->dtor = dtor_catch_syscall;
15045 ops->insert_location = insert_catch_syscall;
15046 ops->remove_location = remove_catch_syscall;
15047 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15048 ops->print_it = print_it_catch_syscall;
15049 ops->print_one = print_one_catch_syscall;
15050 ops->print_mention = print_mention_catch_syscall;
15051 ops->print_recreate = print_recreate_catch_syscall;
15052
15053 /* Solib-related catchpoints. */
15054 ops = &catch_solib_breakpoint_ops;
15055 *ops = base_breakpoint_ops;
15056 ops->dtor = dtor_catch_solib;
15057 ops->insert_location = insert_catch_solib;
15058 ops->remove_location = remove_catch_solib;
15059 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15060 ops->check_status = check_status_catch_solib;
15061 ops->print_it = print_it_catch_solib;
15062 ops->print_one = print_one_catch_solib;
15063 ops->print_mention = print_mention_catch_solib;
15064 ops->print_recreate = print_recreate_catch_solib;
15065 }
15066
15067 void
15068 _initialize_breakpoint (void)
15069 {
15070 struct cmd_list_element *c;
15071
15072 initialize_breakpoint_ops ();
15073
15074 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15075 observer_attach_inferior_exit (clear_syscall_counts);
15076 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15077
15078 breakpoint_objfile_key = register_objfile_data ();
15079
15080 catch_syscall_inferior_data
15081 = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15082
15083 breakpoint_chain = 0;
15084 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15085 before a breakpoint is set. */
15086 breakpoint_count = 0;
15087
15088 tracepoint_count = 0;
15089
15090 add_com ("ignore", class_breakpoint, ignore_command, _("\
15091 Set ignore-count of breakpoint number N to COUNT.\n\
15092 Usage is `ignore N COUNT'."));
15093 if (xdb_commands)
15094 add_com_alias ("bc", "ignore", class_breakpoint, 1);
15095
15096 add_com ("commands", class_breakpoint, commands_command, _("\
15097 Set commands to be executed when a breakpoint is hit.\n\
15098 Give breakpoint number as argument after \"commands\".\n\
15099 With no argument, the targeted breakpoint is the last one set.\n\
15100 The commands themselves follow starting on the next line.\n\
15101 Type a line containing \"end\" to indicate the end of them.\n\
15102 Give \"silent\" as the first line to make the breakpoint silent;\n\
15103 then no output is printed when it is hit, except what the commands print."));
15104
15105 add_com ("condition", class_breakpoint, condition_command, _("\
15106 Specify breakpoint number N to break only if COND is true.\n\
15107 Usage is `condition N COND', where N is an integer and COND is an\n\
15108 expression to be evaluated whenever breakpoint N is reached."));
15109
15110 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15111 Set a temporary breakpoint.\n\
15112 Like \"break\" except the breakpoint is only temporary,\n\
15113 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15114 by using \"enable delete\" on the breakpoint number.\n\
15115 \n"
15116 BREAK_ARGS_HELP ("tbreak")));
15117 set_cmd_completer (c, location_completer);
15118
15119 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15120 Set a hardware assisted breakpoint.\n\
15121 Like \"break\" except the breakpoint requires hardware support,\n\
15122 some target hardware may not have this support.\n\
15123 \n"
15124 BREAK_ARGS_HELP ("hbreak")));
15125 set_cmd_completer (c, location_completer);
15126
15127 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15128 Set a temporary hardware assisted breakpoint.\n\
15129 Like \"hbreak\" except the breakpoint is only temporary,\n\
15130 so it will be deleted when hit.\n\
15131 \n"
15132 BREAK_ARGS_HELP ("thbreak")));
15133 set_cmd_completer (c, location_completer);
15134
15135 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15136 Enable some breakpoints.\n\
15137 Give breakpoint numbers (separated by spaces) as arguments.\n\
15138 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15139 This is used to cancel the effect of the \"disable\" command.\n\
15140 With a subcommand you can enable temporarily."),
15141 &enablelist, "enable ", 1, &cmdlist);
15142 if (xdb_commands)
15143 add_com ("ab", class_breakpoint, enable_command, _("\
15144 Enable some breakpoints.\n\
15145 Give breakpoint numbers (separated by spaces) as arguments.\n\
15146 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15147 This is used to cancel the effect of the \"disable\" command.\n\
15148 With a subcommand you can enable temporarily."));
15149
15150 add_com_alias ("en", "enable", class_breakpoint, 1);
15151
15152 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15153 Enable some breakpoints.\n\
15154 Give breakpoint numbers (separated by spaces) as arguments.\n\
15155 This is used to cancel the effect of the \"disable\" command.\n\
15156 May be abbreviated to simply \"enable\".\n"),
15157 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15158
15159 add_cmd ("once", no_class, enable_once_command, _("\
15160 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15161 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15162 &enablebreaklist);
15163
15164 add_cmd ("delete", no_class, enable_delete_command, _("\
15165 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15166 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15167 &enablebreaklist);
15168
15169 add_cmd ("count", no_class, enable_count_command, _("\
15170 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15171 If a breakpoint is hit while enabled in this fashion,\n\
15172 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15173 &enablebreaklist);
15174
15175 add_cmd ("delete", no_class, enable_delete_command, _("\
15176 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15177 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15178 &enablelist);
15179
15180 add_cmd ("once", no_class, enable_once_command, _("\
15181 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15182 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15183 &enablelist);
15184
15185 add_cmd ("count", no_class, enable_count_command, _("\
15186 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15187 If a breakpoint is hit while enabled in this fashion,\n\
15188 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15189 &enablelist);
15190
15191 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15192 Disable some breakpoints.\n\
15193 Arguments are breakpoint numbers with spaces in between.\n\
15194 To disable all breakpoints, give no argument.\n\
15195 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15196 &disablelist, "disable ", 1, &cmdlist);
15197 add_com_alias ("dis", "disable", class_breakpoint, 1);
15198 add_com_alias ("disa", "disable", class_breakpoint, 1);
15199 if (xdb_commands)
15200 add_com ("sb", class_breakpoint, disable_command, _("\
15201 Disable some breakpoints.\n\
15202 Arguments are breakpoint numbers with spaces in between.\n\
15203 To disable all breakpoints, give no argument.\n\
15204 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
15205
15206 add_cmd ("breakpoints", class_alias, disable_command, _("\
15207 Disable some breakpoints.\n\
15208 Arguments are breakpoint numbers with spaces in between.\n\
15209 To disable all breakpoints, give no argument.\n\
15210 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15211 This command may be abbreviated \"disable\"."),
15212 &disablelist);
15213
15214 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15215 Delete some breakpoints or auto-display expressions.\n\
15216 Arguments are breakpoint numbers with spaces in between.\n\
15217 To delete all breakpoints, give no argument.\n\
15218 \n\
15219 Also a prefix command for deletion of other GDB objects.\n\
15220 The \"unset\" command is also an alias for \"delete\"."),
15221 &deletelist, "delete ", 1, &cmdlist);
15222 add_com_alias ("d", "delete", class_breakpoint, 1);
15223 add_com_alias ("del", "delete", class_breakpoint, 1);
15224 if (xdb_commands)
15225 add_com ("db", class_breakpoint, delete_command, _("\
15226 Delete some breakpoints.\n\
15227 Arguments are breakpoint numbers with spaces in between.\n\
15228 To delete all breakpoints, give no argument.\n"));
15229
15230 add_cmd ("breakpoints", class_alias, delete_command, _("\
15231 Delete some breakpoints or auto-display expressions.\n\
15232 Arguments are breakpoint numbers with spaces in between.\n\
15233 To delete all breakpoints, give no argument.\n\
15234 This command may be abbreviated \"delete\"."),
15235 &deletelist);
15236
15237 add_com ("clear", class_breakpoint, clear_command, _("\
15238 Clear breakpoint at specified line or function.\n\
15239 Argument may be line number, function name, or \"*\" and an address.\n\
15240 If line number is specified, all breakpoints in that line are cleared.\n\
15241 If function is specified, breakpoints at beginning of function are cleared.\n\
15242 If an address is specified, breakpoints at that address are cleared.\n\
15243 \n\
15244 With no argument, clears all breakpoints in the line that the selected frame\n\
15245 is executing in.\n\
15246 \n\
15247 See also the \"delete\" command which clears breakpoints by number."));
15248 add_com_alias ("cl", "clear", class_breakpoint, 1);
15249
15250 c = add_com ("break", class_breakpoint, break_command, _("\
15251 Set breakpoint at specified line or function.\n"
15252 BREAK_ARGS_HELP ("break")));
15253 set_cmd_completer (c, location_completer);
15254
15255 add_com_alias ("b", "break", class_run, 1);
15256 add_com_alias ("br", "break", class_run, 1);
15257 add_com_alias ("bre", "break", class_run, 1);
15258 add_com_alias ("brea", "break", class_run, 1);
15259
15260 if (xdb_commands)
15261 add_com_alias ("ba", "break", class_breakpoint, 1);
15262
15263 if (dbx_commands)
15264 {
15265 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15266 Break in function/address or break at a line in the current file."),
15267 &stoplist, "stop ", 1, &cmdlist);
15268 add_cmd ("in", class_breakpoint, stopin_command,
15269 _("Break in function or address."), &stoplist);
15270 add_cmd ("at", class_breakpoint, stopat_command,
15271 _("Break at a line in the current file."), &stoplist);
15272 add_com ("status", class_info, breakpoints_info, _("\
15273 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15274 The \"Type\" column indicates one of:\n\
15275 \tbreakpoint - normal breakpoint\n\
15276 \twatchpoint - watchpoint\n\
15277 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15278 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15279 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15280 address and file/line number respectively.\n\
15281 \n\
15282 Convenience variable \"$_\" and default examine address for \"x\"\n\
15283 are set to the address of the last breakpoint listed unless the command\n\
15284 is prefixed with \"server \".\n\n\
15285 Convenience variable \"$bpnum\" contains the number of the last\n\
15286 breakpoint set."));
15287 }
15288
15289 add_info ("breakpoints", breakpoints_info, _("\
15290 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15291 The \"Type\" column indicates one of:\n\
15292 \tbreakpoint - normal breakpoint\n\
15293 \twatchpoint - watchpoint\n\
15294 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15295 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15296 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15297 address and file/line number respectively.\n\
15298 \n\
15299 Convenience variable \"$_\" and default examine address for \"x\"\n\
15300 are set to the address of the last breakpoint listed unless the command\n\
15301 is prefixed with \"server \".\n\n\
15302 Convenience variable \"$bpnum\" contains the number of the last\n\
15303 breakpoint set."));
15304
15305 add_info_alias ("b", "breakpoints", 1);
15306
15307 if (xdb_commands)
15308 add_com ("lb", class_breakpoint, breakpoints_info, _("\
15309 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15310 The \"Type\" column indicates one of:\n\
15311 \tbreakpoint - normal breakpoint\n\
15312 \twatchpoint - watchpoint\n\
15313 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15314 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15315 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15316 address and file/line number respectively.\n\
15317 \n\
15318 Convenience variable \"$_\" and default examine address for \"x\"\n\
15319 are set to the address of the last breakpoint listed unless the command\n\
15320 is prefixed with \"server \".\n\n\
15321 Convenience variable \"$bpnum\" contains the number of the last\n\
15322 breakpoint set."));
15323
15324 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15325 Status of all breakpoints, or breakpoint number NUMBER.\n\
15326 The \"Type\" column indicates one of:\n\
15327 \tbreakpoint - normal breakpoint\n\
15328 \twatchpoint - watchpoint\n\
15329 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15330 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15331 \tuntil - internal breakpoint used by the \"until\" command\n\
15332 \tfinish - internal breakpoint used by the \"finish\" command\n\
15333 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15334 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15335 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15336 address and file/line number respectively.\n\
15337 \n\
15338 Convenience variable \"$_\" and default examine address for \"x\"\n\
15339 are set to the address of the last breakpoint listed unless the command\n\
15340 is prefixed with \"server \".\n\n\
15341 Convenience variable \"$bpnum\" contains the number of the last\n\
15342 breakpoint set."),
15343 &maintenanceinfolist);
15344
15345 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15346 Set catchpoints to catch events."),
15347 &catch_cmdlist, "catch ",
15348 0/*allow-unknown*/, &cmdlist);
15349
15350 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15351 Set temporary catchpoints to catch events."),
15352 &tcatch_cmdlist, "tcatch ",
15353 0/*allow-unknown*/, &cmdlist);
15354
15355 /* Add catch and tcatch sub-commands. */
15356 add_catch_command ("catch", _("\
15357 Catch an exception, when caught."),
15358 catch_catch_command,
15359 NULL,
15360 CATCH_PERMANENT,
15361 CATCH_TEMPORARY);
15362 add_catch_command ("throw", _("\
15363 Catch an exception, when thrown."),
15364 catch_throw_command,
15365 NULL,
15366 CATCH_PERMANENT,
15367 CATCH_TEMPORARY);
15368 add_catch_command ("fork", _("Catch calls to fork."),
15369 catch_fork_command_1,
15370 NULL,
15371 (void *) (uintptr_t) catch_fork_permanent,
15372 (void *) (uintptr_t) catch_fork_temporary);
15373 add_catch_command ("vfork", _("Catch calls to vfork."),
15374 catch_fork_command_1,
15375 NULL,
15376 (void *) (uintptr_t) catch_vfork_permanent,
15377 (void *) (uintptr_t) catch_vfork_temporary);
15378 add_catch_command ("exec", _("Catch calls to exec."),
15379 catch_exec_command_1,
15380 NULL,
15381 CATCH_PERMANENT,
15382 CATCH_TEMPORARY);
15383 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15384 Usage: catch load [REGEX]\n\
15385 If REGEX is given, only stop for libraries matching the regular expression."),
15386 catch_load_command_1,
15387 NULL,
15388 CATCH_PERMANENT,
15389 CATCH_TEMPORARY);
15390 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15391 Usage: catch unload [REGEX]\n\
15392 If REGEX is given, only stop for libraries matching the regular expression."),
15393 catch_unload_command_1,
15394 NULL,
15395 CATCH_PERMANENT,
15396 CATCH_TEMPORARY);
15397 add_catch_command ("syscall", _("\
15398 Catch system calls by their names and/or numbers.\n\
15399 Arguments say which system calls to catch. If no arguments\n\
15400 are given, every system call will be caught.\n\
15401 Arguments, if given, should be one or more system call names\n\
15402 (if your system supports that), or system call numbers."),
15403 catch_syscall_command_1,
15404 catch_syscall_completer,
15405 CATCH_PERMANENT,
15406 CATCH_TEMPORARY);
15407
15408 c = add_com ("watch", class_breakpoint, watch_command, _("\
15409 Set a watchpoint for an expression.\n\
15410 Usage: watch [-l|-location] EXPRESSION\n\
15411 A watchpoint stops execution of your program whenever the value of\n\
15412 an expression changes.\n\
15413 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15414 the memory to which it refers."));
15415 set_cmd_completer (c, expression_completer);
15416
15417 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15418 Set a read watchpoint for an expression.\n\
15419 Usage: rwatch [-l|-location] EXPRESSION\n\
15420 A watchpoint stops execution of your program whenever the value of\n\
15421 an expression is read.\n\
15422 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15423 the memory to which it refers."));
15424 set_cmd_completer (c, expression_completer);
15425
15426 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15427 Set a watchpoint for an expression.\n\
15428 Usage: awatch [-l|-location] EXPRESSION\n\
15429 A watchpoint stops execution of your program whenever the value of\n\
15430 an expression is either read or written.\n\
15431 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15432 the memory to which it refers."));
15433 set_cmd_completer (c, expression_completer);
15434
15435 add_info ("watchpoints", watchpoints_info, _("\
15436 Status of specified watchpoints (all watchpoints if no argument)."));
15437
15438 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15439 respond to changes - contrary to the description. */
15440 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15441 &can_use_hw_watchpoints, _("\
15442 Set debugger's willingness to use watchpoint hardware."), _("\
15443 Show debugger's willingness to use watchpoint hardware."), _("\
15444 If zero, gdb will not use hardware for new watchpoints, even if\n\
15445 such is available. (However, any hardware watchpoints that were\n\
15446 created before setting this to nonzero, will continue to use watchpoint\n\
15447 hardware.)"),
15448 NULL,
15449 show_can_use_hw_watchpoints,
15450 &setlist, &showlist);
15451
15452 can_use_hw_watchpoints = 1;
15453
15454 /* Tracepoint manipulation commands. */
15455
15456 c = add_com ("trace", class_breakpoint, trace_command, _("\
15457 Set a tracepoint at specified line or function.\n\
15458 \n"
15459 BREAK_ARGS_HELP ("trace") "\n\
15460 Do \"help tracepoints\" for info on other tracepoint commands."));
15461 set_cmd_completer (c, location_completer);
15462
15463 add_com_alias ("tp", "trace", class_alias, 0);
15464 add_com_alias ("tr", "trace", class_alias, 1);
15465 add_com_alias ("tra", "trace", class_alias, 1);
15466 add_com_alias ("trac", "trace", class_alias, 1);
15467
15468 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15469 Set a fast tracepoint at specified line or function.\n\
15470 \n"
15471 BREAK_ARGS_HELP ("ftrace") "\n\
15472 Do \"help tracepoints\" for info on other tracepoint commands."));
15473 set_cmd_completer (c, location_completer);
15474
15475 c = add_com ("strace", class_breakpoint, strace_command, _("\
15476 Set a static tracepoint at specified line, function or marker.\n\
15477 \n\
15478 strace [LOCATION] [if CONDITION]\n\
15479 LOCATION may be a line number, function name, \"*\" and an address,\n\
15480 or -m MARKER_ID.\n\
15481 If a line number is specified, probe the marker at start of code\n\
15482 for that line. If a function is specified, probe the marker at start\n\
15483 of code for that function. If an address is specified, probe the marker\n\
15484 at that exact address. If a marker id is specified, probe the marker\n\
15485 with that name. With no LOCATION, uses current execution address of\n\
15486 the selected stack frame.\n\
15487 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15488 This collects arbitrary user data passed in the probe point call to the\n\
15489 tracing library. You can inspect it when analyzing the trace buffer,\n\
15490 by printing the $_sdata variable like any other convenience variable.\n\
15491 \n\
15492 CONDITION is a boolean expression.\n\
15493 \n\
15494 Multiple tracepoints at one place are permitted, and useful if their\n\
15495 conditions are different.\n\
15496 \n\
15497 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15498 Do \"help tracepoints\" for info on other tracepoint commands."));
15499 set_cmd_completer (c, location_completer);
15500
15501 add_info ("tracepoints", tracepoints_info, _("\
15502 Status of specified tracepoints (all tracepoints if no argument).\n\
15503 Convenience variable \"$tpnum\" contains the number of the\n\
15504 last tracepoint set."));
15505
15506 add_info_alias ("tp", "tracepoints", 1);
15507
15508 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15509 Delete specified tracepoints.\n\
15510 Arguments are tracepoint numbers, separated by spaces.\n\
15511 No argument means delete all tracepoints."),
15512 &deletelist);
15513
15514 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15515 Disable specified tracepoints.\n\
15516 Arguments are tracepoint numbers, separated by spaces.\n\
15517 No argument means disable all tracepoints."),
15518 &disablelist);
15519 deprecate_cmd (c, "disable");
15520
15521 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15522 Enable specified tracepoints.\n\
15523 Arguments are tracepoint numbers, separated by spaces.\n\
15524 No argument means enable all tracepoints."),
15525 &enablelist);
15526 deprecate_cmd (c, "enable");
15527
15528 add_com ("passcount", class_trace, trace_pass_command, _("\
15529 Set the passcount for a tracepoint.\n\
15530 The trace will end when the tracepoint has been passed 'count' times.\n\
15531 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15532 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15533
15534 add_prefix_cmd ("save", class_breakpoint, save_command,
15535 _("Save breakpoint definitions as a script."),
15536 &save_cmdlist, "save ",
15537 0/*allow-unknown*/, &cmdlist);
15538
15539 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15540 Save current breakpoint definitions as a script.\n\
15541 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15542 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15543 session to restore them."),
15544 &save_cmdlist);
15545 set_cmd_completer (c, filename_completer);
15546
15547 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15548 Save current tracepoint definitions as a script.\n\
15549 Use the 'source' command in another debug session to restore them."),
15550 &save_cmdlist);
15551 set_cmd_completer (c, filename_completer);
15552
15553 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15554 deprecate_cmd (c, "save tracepoints");
15555
15556 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15557 Breakpoint specific settings\n\
15558 Configure various breakpoint-specific variables such as\n\
15559 pending breakpoint behavior"),
15560 &breakpoint_set_cmdlist, "set breakpoint ",
15561 0/*allow-unknown*/, &setlist);
15562 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15563 Breakpoint specific settings\n\
15564 Configure various breakpoint-specific variables such as\n\
15565 pending breakpoint behavior"),
15566 &breakpoint_show_cmdlist, "show breakpoint ",
15567 0/*allow-unknown*/, &showlist);
15568
15569 add_setshow_auto_boolean_cmd ("pending", no_class,
15570 &pending_break_support, _("\
15571 Set debugger's behavior regarding pending breakpoints."), _("\
15572 Show debugger's behavior regarding pending breakpoints."), _("\
15573 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15574 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15575 an error. If auto, an unrecognized breakpoint location results in a\n\
15576 user-query to see if a pending breakpoint should be created."),
15577 NULL,
15578 show_pending_break_support,
15579 &breakpoint_set_cmdlist,
15580 &breakpoint_show_cmdlist);
15581
15582 pending_break_support = AUTO_BOOLEAN_AUTO;
15583
15584 add_setshow_boolean_cmd ("auto-hw", no_class,
15585 &automatic_hardware_breakpoints, _("\
15586 Set automatic usage of hardware breakpoints."), _("\
15587 Show automatic usage of hardware breakpoints."), _("\
15588 If set, the debugger will automatically use hardware breakpoints for\n\
15589 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15590 a warning will be emitted for such breakpoints."),
15591 NULL,
15592 show_automatic_hardware_breakpoints,
15593 &breakpoint_set_cmdlist,
15594 &breakpoint_show_cmdlist);
15595
15596 add_setshow_enum_cmd ("always-inserted", class_support,
15597 always_inserted_enums, &always_inserted_mode, _("\
15598 Set mode for inserting breakpoints."), _("\
15599 Show mode for inserting breakpoints."), _("\
15600 When this mode is off, breakpoints are inserted in inferior when it is\n\
15601 resumed, and removed when execution stops. When this mode is on,\n\
15602 breakpoints are inserted immediately and removed only when the user\n\
15603 deletes the breakpoint. When this mode is auto (which is the default),\n\
15604 the behaviour depends on the non-stop setting (see help set non-stop).\n\
15605 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
15606 behaves as if always-inserted mode is on; if gdb is controlling the\n\
15607 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
15608 NULL,
15609 &show_always_inserted_mode,
15610 &breakpoint_set_cmdlist,
15611 &breakpoint_show_cmdlist);
15612
15613 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15614 condition_evaluation_enums,
15615 &condition_evaluation_mode_1, _("\
15616 Set mode of breakpoint condition evaluation."), _("\
15617 Show mode of breakpoint condition evaluation."), _("\
15618 When this is set to \"host\", breakpoint conditions will be\n\
15619 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15620 breakpoint conditions will be downloaded to the target (if the target\n\
15621 supports such feature) and conditions will be evaluated on the target's side.\n\
15622 If this is set to \"auto\" (default), this will be automatically set to\n\
15623 \"target\" if it supports condition evaluation, otherwise it will\n\
15624 be set to \"gdb\""),
15625 &set_condition_evaluation_mode,
15626 &show_condition_evaluation_mode,
15627 &breakpoint_set_cmdlist,
15628 &breakpoint_show_cmdlist);
15629
15630 add_com ("break-range", class_breakpoint, break_range_command, _("\
15631 Set a breakpoint for an address range.\n\
15632 break-range START-LOCATION, END-LOCATION\n\
15633 where START-LOCATION and END-LOCATION can be one of the following:\n\
15634 LINENUM, for that line in the current file,\n\
15635 FILE:LINENUM, for that line in that file,\n\
15636 +OFFSET, for that number of lines after the current line\n\
15637 or the start of the range\n\
15638 FUNCTION, for the first line in that function,\n\
15639 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15640 *ADDRESS, for the instruction at that address.\n\
15641 \n\
15642 The breakpoint will stop execution of the inferior whenever it executes\n\
15643 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15644 range (including START-LOCATION and END-LOCATION)."));
15645
15646 automatic_hardware_breakpoints = 1;
15647
15648 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
15649 }
This page took 0.371265 seconds and 5 git commands to generate.