484a831040aeb135db0a6e21d29885b98ef5bf96
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include <ctype.h>
25 #include "hashtab.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "breakpoint.h"
29 #include "tracepoint.h"
30 #include "gdbtypes.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "value.h"
35 #include "command.h"
36 #include "inferior.h"
37 #include "gdbthread.h"
38 #include "target.h"
39 #include "language.h"
40 #include "gdb_string.h"
41 #include "demangle.h"
42 #include "annotate.h"
43 #include "symfile.h"
44 #include "objfiles.h"
45 #include "source.h"
46 #include "linespec.h"
47 #include "completer.h"
48 #include "gdb.h"
49 #include "ui-out.h"
50 #include "cli/cli-script.h"
51 #include "gdb_assert.h"
52 #include "block.h"
53 #include "solib.h"
54 #include "solist.h"
55 #include "observer.h"
56 #include "exceptions.h"
57 #include "memattr.h"
58 #include "ada-lang.h"
59 #include "top.h"
60 #include "wrapper.h"
61 #include "valprint.h"
62 #include "jit.h"
63 #include "xml-syscall.h"
64 #include "parser-defs.h"
65 #include "cli/cli-utils.h"
66
67 /* readline include files */
68 #include "readline/readline.h"
69 #include "readline/history.h"
70
71 /* readline defines this. */
72 #undef savestring
73
74 #include "mi/mi-common.h"
75
76 /* Arguments to pass as context to some catch command handlers. */
77 #define CATCH_PERMANENT ((void *) (uintptr_t) 0)
78 #define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
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 disable_command (char *, int);
87
88 static void enable_command (char *, int);
89
90 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
91 void *),
92 void *);
93
94 static void ignore_command (char *, int);
95
96 static int breakpoint_re_set_one (void *);
97
98 static void clear_command (char *, int);
99
100 static void catch_command (char *, int);
101
102 static int can_use_hardware_watchpoint (struct value *, int);
103
104 static void break_command_1 (char *, int, int);
105
106 static void mention (struct breakpoint *);
107
108 /* This function is used in gdbtk sources and thus can not be made
109 static. */
110 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
111 struct symtab_and_line,
112 enum bptype);
113
114 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
115
116 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
117 CORE_ADDR bpaddr,
118 enum bptype bptype);
119
120 static void describe_other_breakpoints (struct gdbarch *,
121 struct program_space *, CORE_ADDR,
122 struct obj_section *, int);
123
124 static int breakpoint_address_match (struct address_space *aspace1,
125 CORE_ADDR addr1,
126 struct address_space *aspace2,
127 CORE_ADDR addr2);
128
129 static int watchpoint_locations_match (struct bp_location *loc1,
130 struct bp_location *loc2);
131
132 static void breakpoints_info (char *, int);
133
134 static void watchpoints_info (char *, int);
135
136 static int breakpoint_1 (char *, int,
137 int (*) (const struct breakpoint *));
138
139 static int breakpoint_cond_eval (void *);
140
141 static void cleanup_executing_breakpoints (void *);
142
143 static void commands_command (char *, int);
144
145 static void condition_command (char *, int);
146
147 typedef enum
148 {
149 mark_inserted,
150 mark_uninserted
151 }
152 insertion_state_t;
153
154 static int remove_breakpoint (struct bp_location *, insertion_state_t);
155 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
156
157 static enum print_stop_action print_it_typical (bpstat);
158
159 static enum print_stop_action print_bp_stop_message (bpstat bs);
160
161 static int watchpoint_check (void *);
162
163 static void maintenance_info_breakpoints (char *, int);
164
165 static int hw_breakpoint_used_count (void);
166
167 static int hw_watchpoint_used_count (enum bptype, int *);
168
169 static void hbreak_command (char *, int);
170
171 static void thbreak_command (char *, int);
172
173 static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
174
175 static void stop_command (char *arg, int from_tty);
176
177 static void stopin_command (char *arg, int from_tty);
178
179 static void stopat_command (char *arg, int from_tty);
180
181 static char *ep_parse_optional_if_clause (char **arg);
182
183 static void catch_exception_command_1 (enum exception_event_kind ex_event,
184 char *arg, int tempflag, int from_tty);
185
186 static void tcatch_command (char *arg, int from_tty);
187
188 static void detach_single_step_breakpoints (void);
189
190 static int single_step_breakpoint_inserted_here_p (struct address_space *,
191 CORE_ADDR pc);
192
193 static void free_bp_location (struct bp_location *loc);
194 static void incref_bp_location (struct bp_location *loc);
195 static void decref_bp_location (struct bp_location **loc);
196
197 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
198
199 static void update_global_location_list (int);
200
201 static void update_global_location_list_nothrow (int);
202
203 static int is_hardware_watchpoint (const struct breakpoint *bpt);
204
205 static int is_watchpoint (const struct breakpoint *bpt);
206
207 static void insert_breakpoint_locations (void);
208
209 static int syscall_catchpoint_p (struct breakpoint *b);
210
211 static void tracepoints_info (char *, int);
212
213 static void delete_trace_command (char *, int);
214
215 static void enable_trace_command (char *, int);
216
217 static void disable_trace_command (char *, int);
218
219 static void trace_pass_command (char *, int);
220
221 /* Assuming we're creating a static tracepoint, does S look like a
222 static tracepoint marker spec ("-m MARKER_ID")? */
223 #define is_marker_spec(s) \
224 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
225
226 /* A reference-counted struct command_line. This lets multiple
227 breakpoints share a single command list. */
228 struct counted_command_line
229 {
230 /* The reference count. */
231 int refc;
232
233 /* The command list. */
234 struct command_line *commands;
235 };
236
237 struct command_line *
238 breakpoint_commands (struct breakpoint *b)
239 {
240 return b->commands ? b->commands->commands : NULL;
241 }
242
243 /* Flag indicating that a command has proceeded the inferior past the
244 current breakpoint. */
245
246 static int breakpoint_proceeded;
247
248 static const char *
249 bpdisp_text (enum bpdisp disp)
250 {
251 /* NOTE: the following values are a part of MI protocol and
252 represent values of 'disp' field returned when inferior stops at
253 a breakpoint. */
254 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
255
256 return bpdisps[(int) disp];
257 }
258
259 /* Prototypes for exported functions. */
260 /* If FALSE, gdb will not use hardware support for watchpoints, even
261 if such is available. */
262 static int can_use_hw_watchpoints;
263
264 static void
265 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
266 struct cmd_list_element *c,
267 const char *value)
268 {
269 fprintf_filtered (file,
270 _("Debugger's willingness to use "
271 "watchpoint hardware is %s.\n"),
272 value);
273 }
274
275 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
276 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
277 for unrecognized breakpoint locations.
278 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
279 static enum auto_boolean pending_break_support;
280 static void
281 show_pending_break_support (struct ui_file *file, int from_tty,
282 struct cmd_list_element *c,
283 const char *value)
284 {
285 fprintf_filtered (file,
286 _("Debugger's behavior regarding "
287 "pending breakpoints is %s.\n"),
288 value);
289 }
290
291 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
292 set with "break" but falling in read-only memory.
293 If 0, gdb will warn about such breakpoints, but won't automatically
294 use hardware breakpoints. */
295 static int automatic_hardware_breakpoints;
296 static void
297 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
298 struct cmd_list_element *c,
299 const char *value)
300 {
301 fprintf_filtered (file,
302 _("Automatic usage of hardware breakpoints is %s.\n"),
303 value);
304 }
305
306 /* If on, gdb will keep breakpoints inserted even as inferior is
307 stopped, and immediately insert any new breakpoints. If off, gdb
308 will insert breakpoints into inferior only when resuming it, and
309 will remove breakpoints upon stop. If auto, GDB will behave as ON
310 if in non-stop mode, and as OFF if all-stop mode.*/
311
312 static const char always_inserted_auto[] = "auto";
313 static const char always_inserted_on[] = "on";
314 static const char always_inserted_off[] = "off";
315 static const char *always_inserted_enums[] = {
316 always_inserted_auto,
317 always_inserted_off,
318 always_inserted_on,
319 NULL
320 };
321 static const char *always_inserted_mode = always_inserted_auto;
322 static void
323 show_always_inserted_mode (struct ui_file *file, int from_tty,
324 struct cmd_list_element *c, const char *value)
325 {
326 if (always_inserted_mode == always_inserted_auto)
327 fprintf_filtered (file,
328 _("Always inserted breakpoint "
329 "mode is %s (currently %s).\n"),
330 value,
331 breakpoints_always_inserted_mode () ? "on" : "off");
332 else
333 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
334 value);
335 }
336
337 int
338 breakpoints_always_inserted_mode (void)
339 {
340 return (always_inserted_mode == always_inserted_on
341 || (always_inserted_mode == always_inserted_auto && non_stop));
342 }
343
344 void _initialize_breakpoint (void);
345
346 /* Are we executing breakpoint commands? */
347 static int executing_breakpoint_commands;
348
349 /* Are overlay event breakpoints enabled? */
350 static int overlay_events_enabled;
351
352 /* See description in breakpoint.h. */
353 int target_exact_watchpoints = 0;
354
355 /* Walk the following statement or block through all breakpoints.
356 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
357 current breakpoint. */
358
359 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
360
361 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
362 for (B = breakpoint_chain; \
363 B ? (TMP=B->next, 1): 0; \
364 B = TMP)
365
366 /* Similar iterator for the low-level breakpoints. SAFE variant is
367 not provided so update_global_location_list must not be called
368 while executing the block of ALL_BP_LOCATIONS. */
369
370 #define ALL_BP_LOCATIONS(B,BP_TMP) \
371 for (BP_TMP = bp_location; \
372 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
373 BP_TMP++)
374
375 /* Iterator for tracepoints only. */
376
377 #define ALL_TRACEPOINTS(B) \
378 for (B = breakpoint_chain; B; B = B->next) \
379 if (is_tracepoint (B))
380
381 /* Chains of all breakpoints defined. */
382
383 struct breakpoint *breakpoint_chain;
384
385 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
386
387 static struct bp_location **bp_location;
388
389 /* Number of elements of BP_LOCATION. */
390
391 static unsigned bp_location_count;
392
393 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
394 ADDRESS for the current elements of BP_LOCATION which get a valid
395 result from bp_location_has_shadow. You can use it for roughly
396 limiting the subrange of BP_LOCATION to scan for shadow bytes for
397 an address you need to read. */
398
399 static CORE_ADDR bp_location_placed_address_before_address_max;
400
401 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
402 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
403 BP_LOCATION which get a valid result from bp_location_has_shadow.
404 You can use it for roughly limiting the subrange of BP_LOCATION to
405 scan for shadow bytes for an address you need to read. */
406
407 static CORE_ADDR bp_location_shadow_len_after_address_max;
408
409 /* The locations that no longer correspond to any breakpoint, unlinked
410 from bp_location array, but for which a hit may still be reported
411 by a target. */
412 VEC(bp_location_p) *moribund_locations = NULL;
413
414 /* Number of last breakpoint made. */
415
416 static int breakpoint_count;
417
418 /* The value of `breakpoint_count' before the last command that
419 created breakpoints. If the last (break-like) command created more
420 than one breakpoint, then the difference between BREAKPOINT_COUNT
421 and PREV_BREAKPOINT_COUNT is more than one. */
422 static int prev_breakpoint_count;
423
424 /* Number of last tracepoint made. */
425
426 static int tracepoint_count;
427
428 static struct cmd_list_element *breakpoint_set_cmdlist;
429 static struct cmd_list_element *breakpoint_show_cmdlist;
430 struct cmd_list_element *save_cmdlist;
431
432 /* Return whether a breakpoint is an active enabled breakpoint. */
433 static int
434 breakpoint_enabled (struct breakpoint *b)
435 {
436 return (b->enable_state == bp_enabled);
437 }
438
439 /* Set breakpoint count to NUM. */
440
441 static void
442 set_breakpoint_count (int num)
443 {
444 prev_breakpoint_count = breakpoint_count;
445 breakpoint_count = num;
446 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
447 }
448
449 /* Used by `start_rbreak_breakpoints' below, to record the current
450 breakpoint count before "rbreak" creates any breakpoint. */
451 static int rbreak_start_breakpoint_count;
452
453 /* Called at the start an "rbreak" command to record the first
454 breakpoint made. */
455
456 void
457 start_rbreak_breakpoints (void)
458 {
459 rbreak_start_breakpoint_count = breakpoint_count;
460 }
461
462 /* Called at the end of an "rbreak" command to record the last
463 breakpoint made. */
464
465 void
466 end_rbreak_breakpoints (void)
467 {
468 prev_breakpoint_count = rbreak_start_breakpoint_count;
469 }
470
471 /* Used in run_command to zero the hit count when a new run starts. */
472
473 void
474 clear_breakpoint_hit_counts (void)
475 {
476 struct breakpoint *b;
477
478 ALL_BREAKPOINTS (b)
479 b->hit_count = 0;
480 }
481
482 /* Allocate a new counted_command_line with reference count of 1.
483 The new structure owns COMMANDS. */
484
485 static struct counted_command_line *
486 alloc_counted_command_line (struct command_line *commands)
487 {
488 struct counted_command_line *result
489 = xmalloc (sizeof (struct counted_command_line));
490
491 result->refc = 1;
492 result->commands = commands;
493 return result;
494 }
495
496 /* Increment reference count. This does nothing if CMD is NULL. */
497
498 static void
499 incref_counted_command_line (struct counted_command_line *cmd)
500 {
501 if (cmd)
502 ++cmd->refc;
503 }
504
505 /* Decrement reference count. If the reference count reaches 0,
506 destroy the counted_command_line. Sets *CMDP to NULL. This does
507 nothing if *CMDP is NULL. */
508
509 static void
510 decref_counted_command_line (struct counted_command_line **cmdp)
511 {
512 if (*cmdp)
513 {
514 if (--(*cmdp)->refc == 0)
515 {
516 free_command_lines (&(*cmdp)->commands);
517 xfree (*cmdp);
518 }
519 *cmdp = NULL;
520 }
521 }
522
523 /* A cleanup function that calls decref_counted_command_line. */
524
525 static void
526 do_cleanup_counted_command_line (void *arg)
527 {
528 decref_counted_command_line (arg);
529 }
530
531 /* Create a cleanup that calls decref_counted_command_line on the
532 argument. */
533
534 static struct cleanup *
535 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
536 {
537 return make_cleanup (do_cleanup_counted_command_line, cmdp);
538 }
539
540 /* Default address, symtab and line to put a breakpoint at
541 for "break" command with no arg.
542 If default_breakpoint_valid is zero, the other three are
543 not valid, and "break" with no arg is an error.
544
545 This set by print_stack_frame, which calls set_default_breakpoint. */
546
547 int default_breakpoint_valid;
548 CORE_ADDR default_breakpoint_address;
549 struct symtab *default_breakpoint_symtab;
550 int default_breakpoint_line;
551 struct program_space *default_breakpoint_pspace;
552
553 \f
554 /* Return the breakpoint with the specified number, or NULL
555 if the number does not refer to an existing breakpoint. */
556
557 struct breakpoint *
558 get_breakpoint (int num)
559 {
560 struct breakpoint *b;
561
562 ALL_BREAKPOINTS (b)
563 if (b->number == num)
564 return b;
565
566 return NULL;
567 }
568
569 \f
570
571 void
572 set_breakpoint_condition (struct breakpoint *b, char *exp,
573 int from_tty)
574 {
575 struct bp_location *loc = b->loc;
576
577 for (; loc; loc = loc->next)
578 {
579 xfree (loc->cond);
580 loc->cond = NULL;
581 }
582 xfree (b->cond_string);
583 b->cond_string = NULL;
584 xfree (b->cond_exp);
585 b->cond_exp = NULL;
586
587 if (*exp == 0)
588 {
589 if (from_tty)
590 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
591 }
592 else
593 {
594 char *arg = exp;
595
596 /* I don't know if it matters whether this is the string the user
597 typed in or the decompiled expression. */
598 b->cond_string = xstrdup (arg);
599 b->condition_not_parsed = 0;
600
601 if (is_watchpoint (b))
602 {
603 innermost_block = NULL;
604 arg = exp;
605 b->cond_exp = parse_exp_1 (&arg, 0, 0);
606 if (*arg)
607 error (_("Junk at end of expression"));
608 b->cond_exp_valid_block = innermost_block;
609 }
610 else
611 {
612 for (loc = b->loc; loc; loc = loc->next)
613 {
614 arg = exp;
615 loc->cond =
616 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
617 if (*arg)
618 error (_("Junk at end of expression"));
619 }
620 }
621 }
622 breakpoints_changed ();
623 observer_notify_breakpoint_modified (b->number);
624 }
625
626 /* condition N EXP -- set break condition of breakpoint N to EXP. */
627
628 static void
629 condition_command (char *arg, int from_tty)
630 {
631 struct breakpoint *b;
632 char *p;
633 int bnum;
634
635 if (arg == 0)
636 error_no_arg (_("breakpoint number"));
637
638 p = arg;
639 bnum = get_number (&p);
640 if (bnum == 0)
641 error (_("Bad breakpoint argument: '%s'"), arg);
642
643 ALL_BREAKPOINTS (b)
644 if (b->number == bnum)
645 {
646 set_breakpoint_condition (b, p, from_tty);
647 return;
648 }
649
650 error (_("No breakpoint number %d."), bnum);
651 }
652
653 /* Check that COMMAND do not contain commands that are suitable
654 only for tracepoints and not suitable for ordinary breakpoints.
655 Throw if any such commands is found. */
656
657 static void
658 check_no_tracepoint_commands (struct command_line *commands)
659 {
660 struct command_line *c;
661
662 for (c = commands; c; c = c->next)
663 {
664 int i;
665
666 if (c->control_type == while_stepping_control)
667 error (_("The 'while-stepping' command can "
668 "only be used for tracepoints"));
669
670 for (i = 0; i < c->body_count; ++i)
671 check_no_tracepoint_commands ((c->body_list)[i]);
672
673 /* Not that command parsing removes leading whitespace and comment
674 lines and also empty lines. So, we only need to check for
675 command directly. */
676 if (strstr (c->line, "collect ") == c->line)
677 error (_("The 'collect' command can only be used for tracepoints"));
678
679 if (strstr (c->line, "teval ") == c->line)
680 error (_("The 'teval' command can only be used for tracepoints"));
681 }
682 }
683
684 /* Encapsulate tests for different types of tracepoints. */
685
686 int
687 is_tracepoint (const struct breakpoint *b)
688 {
689 return (b->type == bp_tracepoint
690 || b->type == bp_fast_tracepoint
691 || b->type == bp_static_tracepoint);
692 }
693
694 /* A helper function that validsates that COMMANDS are valid for a
695 breakpoint. This function will throw an exception if a problem is
696 found. */
697
698 static void
699 validate_commands_for_breakpoint (struct breakpoint *b,
700 struct command_line *commands)
701 {
702 if (is_tracepoint (b))
703 {
704 /* We need to verify that each top-level element of commands is
705 valid for tracepoints, that there's at most one
706 while-stepping element, and that while-stepping's body has
707 valid tracing commands excluding nested while-stepping. */
708 struct command_line *c;
709 struct command_line *while_stepping = 0;
710 for (c = commands; c; c = c->next)
711 {
712 if (c->control_type == while_stepping_control)
713 {
714 if (b->type == bp_fast_tracepoint)
715 error (_("The 'while-stepping' command "
716 "cannot be used for fast tracepoint"));
717 else if (b->type == bp_static_tracepoint)
718 error (_("The 'while-stepping' command "
719 "cannot be used for static tracepoint"));
720
721 if (while_stepping)
722 error (_("The 'while-stepping' command "
723 "can be used only once"));
724 else
725 while_stepping = c;
726 }
727 }
728 if (while_stepping)
729 {
730 struct command_line *c2;
731
732 gdb_assert (while_stepping->body_count == 1);
733 c2 = while_stepping->body_list[0];
734 for (; c2; c2 = c2->next)
735 {
736 if (c2->control_type == while_stepping_control)
737 error (_("The 'while-stepping' command cannot be nested"));
738 }
739 }
740 }
741 else
742 {
743 check_no_tracepoint_commands (commands);
744 }
745 }
746
747 /* Return a vector of all the static tracepoints set at ADDR. The
748 caller is responsible for releasing the vector. */
749
750 VEC(breakpoint_p) *
751 static_tracepoints_here (CORE_ADDR addr)
752 {
753 struct breakpoint *b;
754 VEC(breakpoint_p) *found = 0;
755 struct bp_location *loc;
756
757 ALL_BREAKPOINTS (b)
758 if (b->type == bp_static_tracepoint)
759 {
760 for (loc = b->loc; loc; loc = loc->next)
761 if (loc->address == addr)
762 VEC_safe_push(breakpoint_p, found, b);
763 }
764
765 return found;
766 }
767
768 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
769 validate that only allowed commands are included. */
770
771 void
772 breakpoint_set_commands (struct breakpoint *b,
773 struct command_line *commands)
774 {
775 validate_commands_for_breakpoint (b, commands);
776
777 decref_counted_command_line (&b->commands);
778 b->commands = alloc_counted_command_line (commands);
779 breakpoints_changed ();
780 observer_notify_breakpoint_modified (b->number);
781 }
782
783 /* Set the internal `silent' flag on the breakpoint. Note that this
784 is not the same as the "silent" that may appear in the breakpoint's
785 commands. */
786
787 void
788 breakpoint_set_silent (struct breakpoint *b, int silent)
789 {
790 int old_silent = b->silent;
791
792 b->silent = silent;
793 if (old_silent != silent)
794 observer_notify_breakpoint_modified (b->number);
795 }
796
797 /* Set the thread for this breakpoint. If THREAD is -1, make the
798 breakpoint work for any thread. */
799
800 void
801 breakpoint_set_thread (struct breakpoint *b, int thread)
802 {
803 int old_thread = b->thread;
804
805 b->thread = thread;
806 if (old_thread != thread)
807 observer_notify_breakpoint_modified (b->number);
808 }
809
810 /* Set the task for this breakpoint. If TASK is 0, make the
811 breakpoint work for any task. */
812
813 void
814 breakpoint_set_task (struct breakpoint *b, int task)
815 {
816 int old_task = b->task;
817
818 b->task = task;
819 if (old_task != task)
820 observer_notify_breakpoint_modified (b->number);
821 }
822
823 void
824 check_tracepoint_command (char *line, void *closure)
825 {
826 struct breakpoint *b = closure;
827
828 validate_actionline (&line, b);
829 }
830
831 /* A structure used to pass information through
832 map_breakpoint_numbers. */
833
834 struct commands_info
835 {
836 /* True if the command was typed at a tty. */
837 int from_tty;
838
839 /* The breakpoint range spec. */
840 char *arg;
841
842 /* Non-NULL if the body of the commands are being read from this
843 already-parsed command. */
844 struct command_line *control;
845
846 /* The command lines read from the user, or NULL if they have not
847 yet been read. */
848 struct counted_command_line *cmd;
849 };
850
851 /* A callback for map_breakpoint_numbers that sets the commands for
852 commands_command. */
853
854 static void
855 do_map_commands_command (struct breakpoint *b, void *data)
856 {
857 struct commands_info *info = data;
858
859 if (info->cmd == NULL)
860 {
861 struct command_line *l;
862
863 if (info->control != NULL)
864 l = copy_command_lines (info->control->body_list[0]);
865 else
866 {
867 struct cleanup *old_chain;
868 char *str;
869
870 str = xstrprintf (_("Type commands for breakpoint(s) "
871 "%s, one per line."),
872 info->arg);
873
874 old_chain = make_cleanup (xfree, str);
875
876 l = read_command_lines (str,
877 info->from_tty, 1,
878 (is_tracepoint (b)
879 ? check_tracepoint_command : 0),
880 b);
881
882 do_cleanups (old_chain);
883 }
884
885 info->cmd = alloc_counted_command_line (l);
886 }
887
888 /* If a breakpoint was on the list more than once, we don't need to
889 do anything. */
890 if (b->commands != info->cmd)
891 {
892 validate_commands_for_breakpoint (b, info->cmd->commands);
893 incref_counted_command_line (info->cmd);
894 decref_counted_command_line (&b->commands);
895 b->commands = info->cmd;
896 breakpoints_changed ();
897 observer_notify_breakpoint_modified (b->number);
898 }
899 }
900
901 static void
902 commands_command_1 (char *arg, int from_tty,
903 struct command_line *control)
904 {
905 struct cleanup *cleanups;
906 struct commands_info info;
907
908 info.from_tty = from_tty;
909 info.control = control;
910 info.cmd = NULL;
911 /* If we read command lines from the user, then `info' will hold an
912 extra reference to the commands that we must clean up. */
913 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
914
915 if (arg == NULL || !*arg)
916 {
917 if (breakpoint_count - prev_breakpoint_count > 1)
918 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
919 breakpoint_count);
920 else if (breakpoint_count > 0)
921 arg = xstrprintf ("%d", breakpoint_count);
922 else
923 {
924 /* So that we don't try to free the incoming non-NULL
925 argument in the cleanup below. Mapping breakpoint
926 numbers will fail in this case. */
927 arg = NULL;
928 }
929 }
930 else
931 /* The command loop has some static state, so we need to preserve
932 our argument. */
933 arg = xstrdup (arg);
934
935 if (arg != NULL)
936 make_cleanup (xfree, arg);
937
938 info.arg = arg;
939
940 map_breakpoint_numbers (arg, do_map_commands_command, &info);
941
942 if (info.cmd == NULL)
943 error (_("No breakpoints specified."));
944
945 do_cleanups (cleanups);
946 }
947
948 static void
949 commands_command (char *arg, int from_tty)
950 {
951 commands_command_1 (arg, from_tty, NULL);
952 }
953
954 /* Like commands_command, but instead of reading the commands from
955 input stream, takes them from an already parsed command structure.
956
957 This is used by cli-script.c to DTRT with breakpoint commands
958 that are part of if and while bodies. */
959 enum command_control_type
960 commands_from_control_command (char *arg, struct command_line *cmd)
961 {
962 commands_command_1 (arg, 0, cmd);
963 return simple_control;
964 }
965
966 /* Return non-zero if BL->TARGET_INFO contains valid information. */
967
968 static int
969 bp_location_has_shadow (struct bp_location *bl)
970 {
971 if (bl->loc_type != bp_loc_software_breakpoint)
972 return 0;
973 if (!bl->inserted)
974 return 0;
975 if (bl->target_info.shadow_len == 0)
976 /* bp isn't valid, or doesn't shadow memory. */
977 return 0;
978 return 1;
979 }
980
981 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
982 by replacing any memory breakpoints with their shadowed contents.
983
984 The range of shadowed area by each bp_location is:
985 bl->address - bp_location_placed_address_before_address_max
986 up to bl->address + bp_location_shadow_len_after_address_max
987 The range we were requested to resolve shadows for is:
988 memaddr ... memaddr + len
989 Thus the safe cutoff boundaries for performance optimization are
990 memaddr + len <= (bl->address
991 - bp_location_placed_address_before_address_max)
992 and:
993 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
994
995 void
996 breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
997 {
998 /* Left boundary, right boundary and median element of our binary
999 search. */
1000 unsigned bc_l, bc_r, bc;
1001
1002 /* Find BC_L which is a leftmost element which may affect BUF
1003 content. It is safe to report lower value but a failure to
1004 report higher one. */
1005
1006 bc_l = 0;
1007 bc_r = bp_location_count;
1008 while (bc_l + 1 < bc_r)
1009 {
1010 struct bp_location *bl;
1011
1012 bc = (bc_l + bc_r) / 2;
1013 bl = bp_location[bc];
1014
1015 /* Check first BL->ADDRESS will not overflow due to the added
1016 constant. Then advance the left boundary only if we are sure
1017 the BC element can in no way affect the BUF content (MEMADDR
1018 to MEMADDR + LEN range).
1019
1020 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1021 offset so that we cannot miss a breakpoint with its shadow
1022 range tail still reaching MEMADDR. */
1023
1024 if ((bl->address + bp_location_shadow_len_after_address_max
1025 >= bl->address)
1026 && (bl->address + bp_location_shadow_len_after_address_max
1027 <= memaddr))
1028 bc_l = bc;
1029 else
1030 bc_r = bc;
1031 }
1032
1033 /* Due to the binary search above, we need to make sure we pick the
1034 first location that's at BC_L's address. E.g., if there are
1035 multiple locations at the same address, BC_L may end up pointing
1036 at a duplicate location, and miss the "master"/"inserted"
1037 location. Say, given locations L1, L2 and L3 at addresses A and
1038 B:
1039
1040 L1@A, L2@A, L3@B, ...
1041
1042 BC_L could end up pointing at location L2, while the "master"
1043 location could be L1. Since the `loc->inserted' flag is only set
1044 on "master" locations, we'd forget to restore the shadow of L1
1045 and L2. */
1046 while (bc_l > 0
1047 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1048 bc_l--;
1049
1050 /* Now do full processing of the found relevant range of elements. */
1051
1052 for (bc = bc_l; bc < bp_location_count; bc++)
1053 {
1054 struct bp_location *bl = bp_location[bc];
1055 CORE_ADDR bp_addr = 0;
1056 int bp_size = 0;
1057 int bptoffset = 0;
1058
1059 /* bp_location array has BL->OWNER always non-NULL. */
1060 if (bl->owner->type == bp_none)
1061 warning (_("reading through apparently deleted breakpoint #%d?"),
1062 bl->owner->number);
1063
1064 /* Performance optimization: any futher element can no longer affect BUF
1065 content. */
1066
1067 if (bl->address >= bp_location_placed_address_before_address_max
1068 && memaddr + len <= (bl->address
1069 - bp_location_placed_address_before_address_max))
1070 break;
1071
1072 if (!bp_location_has_shadow (bl))
1073 continue;
1074 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1075 current_program_space->aspace, 0))
1076 continue;
1077
1078 /* Addresses and length of the part of the breakpoint that
1079 we need to copy. */
1080 bp_addr = bl->target_info.placed_address;
1081 bp_size = bl->target_info.shadow_len;
1082
1083 if (bp_addr + bp_size <= memaddr)
1084 /* The breakpoint is entirely before the chunk of memory we
1085 are reading. */
1086 continue;
1087
1088 if (bp_addr >= memaddr + len)
1089 /* The breakpoint is entirely after the chunk of memory we are
1090 reading. */
1091 continue;
1092
1093 /* Offset within shadow_contents. */
1094 if (bp_addr < memaddr)
1095 {
1096 /* Only copy the second part of the breakpoint. */
1097 bp_size -= memaddr - bp_addr;
1098 bptoffset = memaddr - bp_addr;
1099 bp_addr = memaddr;
1100 }
1101
1102 if (bp_addr + bp_size > memaddr + len)
1103 {
1104 /* Only copy the first part of the breakpoint. */
1105 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1106 }
1107
1108 memcpy (buf + bp_addr - memaddr,
1109 bl->target_info.shadow_contents + bptoffset, bp_size);
1110 }
1111 }
1112 \f
1113
1114 /* Return true if BPT is of any hardware watchpoint kind. */
1115
1116 static int
1117 is_hardware_watchpoint (const struct breakpoint *bpt)
1118 {
1119 return (bpt->type == bp_hardware_watchpoint
1120 || bpt->type == bp_read_watchpoint
1121 || bpt->type == bp_access_watchpoint);
1122 }
1123
1124 /* Return true if BPT is of any watchpoint kind, hardware or
1125 software. */
1126
1127 static int
1128 is_watchpoint (const struct breakpoint *bpt)
1129 {
1130 return (is_hardware_watchpoint (bpt)
1131 || bpt->type == bp_watchpoint);
1132 }
1133
1134 /* Assuming that B is a watchpoint: returns true if the current thread
1135 and its running state are safe to evaluate or update watchpoint B.
1136 Watchpoints on local expressions need to be evaluated in the
1137 context of the thread that was current when the watchpoint was
1138 created, and, that thread needs to be stopped to be able to select
1139 the correct frame context. Watchpoints on global expressions can
1140 be evaluated on any thread, and in any state. It is presently left
1141 to the target allowing memory accesses when threads are
1142 running. */
1143
1144 static int
1145 watchpoint_in_thread_scope (struct breakpoint *b)
1146 {
1147 return (ptid_equal (b->watchpoint_thread, null_ptid)
1148 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1149 && !is_executing (inferior_ptid)));
1150 }
1151
1152 /* Assuming that B is a watchpoint:
1153 - Reparse watchpoint expression, if REPARSE is non-zero
1154 - Evaluate expression and store the result in B->val
1155 - Evaluate the condition if there is one, and store the result
1156 in b->loc->cond.
1157 - Update the list of values that must be watched in B->loc.
1158
1159 If the watchpoint disposition is disp_del_at_next_stop, then do
1160 nothing. If this is local watchpoint that is out of scope, delete
1161 it.
1162
1163 Even with `set breakpoint always-inserted on' the watchpoints are
1164 removed + inserted on each stop here. Normal breakpoints must
1165 never be removed because they might be missed by a running thread
1166 when debugging in non-stop mode. On the other hand, hardware
1167 watchpoints (is_hardware_watchpoint; processed here) are specific
1168 to each LWP since they are stored in each LWP's hardware debug
1169 registers. Therefore, such LWP must be stopped first in order to
1170 be able to modify its hardware watchpoints.
1171
1172 Hardware watchpoints must be reset exactly once after being
1173 presented to the user. It cannot be done sooner, because it would
1174 reset the data used to present the watchpoint hit to the user. And
1175 it must not be done later because it could display the same single
1176 watchpoint hit during multiple GDB stops. Note that the latter is
1177 relevant only to the hardware watchpoint types bp_read_watchpoint
1178 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1179 not user-visible - its hit is suppressed if the memory content has
1180 not changed.
1181
1182 The following constraints influence the location where we can reset
1183 hardware watchpoints:
1184
1185 * target_stopped_by_watchpoint and target_stopped_data_address are
1186 called several times when GDB stops.
1187
1188 [linux]
1189 * Multiple hardware watchpoints can be hit at the same time,
1190 causing GDB to stop. GDB only presents one hardware watchpoint
1191 hit at a time as the reason for stopping, and all the other hits
1192 are presented later, one after the other, each time the user
1193 requests the execution to be resumed. Execution is not resumed
1194 for the threads still having pending hit event stored in
1195 LWP_INFO->STATUS. While the watchpoint is already removed from
1196 the inferior on the first stop the thread hit event is kept being
1197 reported from its cached value by linux_nat_stopped_data_address
1198 until the real thread resume happens after the watchpoint gets
1199 presented and thus its LWP_INFO->STATUS gets reset.
1200
1201 Therefore the hardware watchpoint hit can get safely reset on the
1202 watchpoint removal from inferior. */
1203
1204 static void
1205 update_watchpoint (struct breakpoint *b, int reparse)
1206 {
1207 int within_current_scope;
1208 struct frame_id saved_frame_id;
1209 int frame_saved;
1210
1211 /* If this is a local watchpoint, we only want to check if the
1212 watchpoint frame is in scope if the current thread is the thread
1213 that was used to create the watchpoint. */
1214 if (!watchpoint_in_thread_scope (b))
1215 return;
1216
1217 if (b->disposition == disp_del_at_next_stop)
1218 return;
1219
1220 frame_saved = 0;
1221
1222 /* Determine if the watchpoint is within scope. */
1223 if (b->exp_valid_block == NULL)
1224 within_current_scope = 1;
1225 else
1226 {
1227 struct frame_info *fi = get_current_frame ();
1228 struct gdbarch *frame_arch = get_frame_arch (fi);
1229 CORE_ADDR frame_pc = get_frame_pc (fi);
1230
1231 /* If we're in a function epilogue, unwinding may not work
1232 properly, so do not attempt to recreate locations at this
1233 point. See similar comments in watchpoint_check. */
1234 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1235 return;
1236
1237 /* Save the current frame's ID so we can restore it after
1238 evaluating the watchpoint expression on its own frame. */
1239 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1240 took a frame parameter, so that we didn't have to change the
1241 selected frame. */
1242 frame_saved = 1;
1243 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1244
1245 fi = frame_find_by_id (b->watchpoint_frame);
1246 within_current_scope = (fi != NULL);
1247 if (within_current_scope)
1248 select_frame (fi);
1249 }
1250
1251 /* We don't free locations. They are stored in the bp_location array
1252 and update_global_location_list will eventually delete them and
1253 remove breakpoints if needed. */
1254 b->loc = NULL;
1255
1256 if (within_current_scope && reparse)
1257 {
1258 char *s;
1259
1260 if (b->exp)
1261 {
1262 xfree (b->exp);
1263 b->exp = NULL;
1264 }
1265 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1266 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1267 /* If the meaning of expression itself changed, the old value is
1268 no longer relevant. We don't want to report a watchpoint hit
1269 to the user when the old value and the new value may actually
1270 be completely different objects. */
1271 value_free (b->val);
1272 b->val = NULL;
1273 b->val_valid = 0;
1274
1275 /* Note that unlike with breakpoints, the watchpoint's condition
1276 expression is stored in the breakpoint object, not in the
1277 locations (re)created below. */
1278 if (b->cond_string != NULL)
1279 {
1280 if (b->cond_exp != NULL)
1281 {
1282 xfree (b->cond_exp);
1283 b->cond_exp = NULL;
1284 }
1285
1286 s = b->cond_string;
1287 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1288 }
1289 }
1290
1291 /* If we failed to parse the expression, for example because
1292 it refers to a global variable in a not-yet-loaded shared library,
1293 don't try to insert watchpoint. We don't automatically delete
1294 such watchpoint, though, since failure to parse expression
1295 is different from out-of-scope watchpoint. */
1296 if ( !target_has_execution)
1297 {
1298 /* Without execution, memory can't change. No use to try and
1299 set watchpoint locations. The watchpoint will be reset when
1300 the target gains execution, through breakpoint_re_set. */
1301 }
1302 else if (within_current_scope && b->exp)
1303 {
1304 int pc = 0;
1305 struct value *val_chain, *v, *result, *next;
1306 struct program_space *frame_pspace;
1307
1308 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1309
1310 /* Avoid setting b->val if it's already set. The meaning of
1311 b->val is 'the last value' user saw, and we should update
1312 it only if we reported that last value to user. As it
1313 happens, the code that reports it updates b->val directly. */
1314 if (!b->val_valid)
1315 {
1316 b->val = v;
1317 b->val_valid = 1;
1318 }
1319
1320 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1321
1322 /* Look at each value on the value chain. */
1323 for (v = val_chain; v; v = value_next (v))
1324 {
1325 /* If it's a memory location, and GDB actually needed
1326 its contents to evaluate the expression, then we
1327 must watch it. If the first value returned is
1328 still lazy, that means an error occurred reading it;
1329 watch it anyway in case it becomes readable. */
1330 if (VALUE_LVAL (v) == lval_memory
1331 && (v == val_chain || ! value_lazy (v)))
1332 {
1333 struct type *vtype = check_typedef (value_type (v));
1334
1335 /* We only watch structs and arrays if user asked
1336 for it explicitly, never if they just happen to
1337 appear in the middle of some value chain. */
1338 if (v == result
1339 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1340 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1341 {
1342 CORE_ADDR addr;
1343 int len, type;
1344 struct bp_location *loc, **tmp;
1345
1346 addr = value_address (v);
1347 len = TYPE_LENGTH (value_type (v));
1348 type = hw_write;
1349 if (b->type == bp_read_watchpoint)
1350 type = hw_read;
1351 else if (b->type == bp_access_watchpoint)
1352 type = hw_access;
1353
1354 loc = allocate_bp_location (b);
1355 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1356 ;
1357 *tmp = loc;
1358 loc->gdbarch = get_type_arch (value_type (v));
1359
1360 loc->pspace = frame_pspace;
1361 loc->address = addr;
1362 loc->length = len;
1363 loc->watchpoint_type = type;
1364 }
1365 }
1366 }
1367
1368 /* Change the type of breakpoint between hardware assisted or
1369 an ordinary watchpoint depending on the hardware support
1370 and free hardware slots. REPARSE is set when the inferior
1371 is started. */
1372 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1373 && reparse)
1374 {
1375 int reg_cnt;
1376 enum bp_loc_type loc_type;
1377 struct bp_location *bl;
1378
1379 reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1380
1381 if (reg_cnt)
1382 {
1383 int i, target_resources_ok, other_type_used;
1384 enum enable_state orig_enable_state;
1385
1386 /* We need to determine how many resources are already
1387 used for all other hardware watchpoints plus this one
1388 to see if we still have enough resources to also fit
1389 this watchpoint in as well. To guarantee the
1390 hw_watchpoint_used_count call below counts this
1391 watchpoint, make sure that it is marked as a hardware
1392 watchpoint. */
1393 b->type = bp_hardware_watchpoint;
1394
1395 /* hw_watchpoint_used_count ignores disabled watchpoints,
1396 and b might be disabled if we're being called from
1397 do_enable_breakpoint. */
1398 orig_enable_state = b->enable_state;
1399 b->enable_state = bp_enabled;
1400
1401 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1402 &other_type_used);
1403
1404 b->enable_state = orig_enable_state;
1405
1406 target_resources_ok = target_can_use_hardware_watchpoint
1407 (bp_hardware_watchpoint, i, other_type_used);
1408 if (target_resources_ok <= 0)
1409 b->type = bp_watchpoint;
1410 }
1411 else
1412 b->type = bp_watchpoint;
1413
1414 loc_type = (b->type == bp_watchpoint? bp_loc_other
1415 : bp_loc_hardware_watchpoint);
1416 for (bl = b->loc; bl; bl = bl->next)
1417 bl->loc_type = loc_type;
1418 }
1419
1420 for (v = val_chain; v; v = next)
1421 {
1422 next = value_next (v);
1423 if (v != b->val)
1424 value_free (v);
1425 }
1426
1427 /* If a software watchpoint is not watching any memory, then the
1428 above left it without any location set up. But,
1429 bpstat_stop_status requires a location to be able to report
1430 stops, so make sure there's at least a dummy one. */
1431 if (b->type == bp_watchpoint && b->loc == NULL)
1432 {
1433 b->loc = allocate_bp_location (b);
1434 b->loc->pspace = frame_pspace;
1435 b->loc->address = -1;
1436 b->loc->length = -1;
1437 b->loc->watchpoint_type = -1;
1438 }
1439 }
1440 else if (!within_current_scope)
1441 {
1442 printf_filtered (_("\
1443 Watchpoint %d deleted because the program has left the block\n\
1444 in which its expression is valid.\n"),
1445 b->number);
1446 if (b->related_breakpoint)
1447 {
1448 b->related_breakpoint->disposition = disp_del_at_next_stop;
1449 b->related_breakpoint->related_breakpoint = NULL;
1450 b->related_breakpoint= NULL;
1451 }
1452 b->disposition = disp_del_at_next_stop;
1453 }
1454
1455 /* Restore the selected frame. */
1456 if (frame_saved)
1457 select_frame (frame_find_by_id (saved_frame_id));
1458 }
1459
1460
1461 /* Returns 1 iff breakpoint location should be
1462 inserted in the inferior. */
1463 static int
1464 should_be_inserted (struct bp_location *bl)
1465 {
1466 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1467 return 0;
1468
1469 if (bl->owner->disposition == disp_del_at_next_stop)
1470 return 0;
1471
1472 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1473 return 0;
1474
1475 /* This is set for example, when we're attached to the parent of a
1476 vfork, and have detached from the child. The child is running
1477 free, and we expect it to do an exec or exit, at which point the
1478 OS makes the parent schedulable again (and the target reports
1479 that the vfork is done). Until the child is done with the shared
1480 memory region, do not insert breakpoints in the parent, otherwise
1481 the child could still trip on the parent's breakpoints. Since
1482 the parent is blocked anyway, it won't miss any breakpoint. */
1483 if (bl->pspace->breakpoints_not_allowed)
1484 return 0;
1485
1486 /* Tracepoints are inserted by the target at a time of its choosing,
1487 not by us. */
1488 if (is_tracepoint (bl->owner))
1489 return 0;
1490
1491 return 1;
1492 }
1493
1494 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1495 location. Any error messages are printed to TMP_ERROR_STREAM; and
1496 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
1497
1498 NOTE drow/2003-09-09: This routine could be broken down to an
1499 object-style method for each breakpoint or catchpoint type. */
1500 static int
1501 insert_bp_location (struct bp_location *bl,
1502 struct ui_file *tmp_error_stream,
1503 int *disabled_breaks,
1504 int *hw_breakpoint_error)
1505 {
1506 int val = 0;
1507
1508 if (!should_be_inserted (bl) || bl->inserted)
1509 return 0;
1510
1511 /* Initialize the target-specific information. */
1512 memset (&bl->target_info, 0, sizeof (bl->target_info));
1513 bl->target_info.placed_address = bl->address;
1514 bl->target_info.placed_address_space = bl->pspace->aspace;
1515
1516 if (bl->loc_type == bp_loc_software_breakpoint
1517 || bl->loc_type == bp_loc_hardware_breakpoint)
1518 {
1519 if (bl->owner->type != bp_hardware_breakpoint)
1520 {
1521 /* If the explicitly specified breakpoint type
1522 is not hardware breakpoint, check the memory map to see
1523 if the breakpoint address is in read only memory or not.
1524
1525 Two important cases are:
1526 - location type is not hardware breakpoint, memory
1527 is readonly. We change the type of the location to
1528 hardware breakpoint.
1529 - location type is hardware breakpoint, memory is
1530 read-write. This means we've previously made the
1531 location hardware one, but then the memory map changed,
1532 so we undo.
1533
1534 When breakpoints are removed, remove_breakpoints will use
1535 location types we've just set here, the only possible
1536 problem is that memory map has changed during running
1537 program, but it's not going to work anyway with current
1538 gdb. */
1539 struct mem_region *mr
1540 = lookup_mem_region (bl->target_info.placed_address);
1541
1542 if (mr)
1543 {
1544 if (automatic_hardware_breakpoints)
1545 {
1546 enum bp_loc_type new_type;
1547
1548 if (mr->attrib.mode != MEM_RW)
1549 new_type = bp_loc_hardware_breakpoint;
1550 else
1551 new_type = bp_loc_software_breakpoint;
1552
1553 if (new_type != bl->loc_type)
1554 {
1555 static int said = 0;
1556
1557 bl->loc_type = new_type;
1558 if (!said)
1559 {
1560 fprintf_filtered (gdb_stdout,
1561 _("Note: automatically using "
1562 "hardware breakpoints for "
1563 "read-only addresses.\n"));
1564 said = 1;
1565 }
1566 }
1567 }
1568 else if (bl->loc_type == bp_loc_software_breakpoint
1569 && mr->attrib.mode != MEM_RW)
1570 warning (_("cannot set software breakpoint "
1571 "at readonly address %s"),
1572 paddress (bl->gdbarch, bl->address));
1573 }
1574 }
1575
1576 /* First check to see if we have to handle an overlay. */
1577 if (overlay_debugging == ovly_off
1578 || bl->section == NULL
1579 || !(section_is_overlay (bl->section)))
1580 {
1581 /* No overlay handling: just set the breakpoint. */
1582
1583 if (bl->loc_type == bp_loc_hardware_breakpoint)
1584 val = target_insert_hw_breakpoint (bl->gdbarch,
1585 &bl->target_info);
1586 else
1587 val = target_insert_breakpoint (bl->gdbarch,
1588 &bl->target_info);
1589 }
1590 else
1591 {
1592 /* This breakpoint is in an overlay section.
1593 Shall we set a breakpoint at the LMA? */
1594 if (!overlay_events_enabled)
1595 {
1596 /* Yes -- overlay event support is not active,
1597 so we must try to set a breakpoint at the LMA.
1598 This will not work for a hardware breakpoint. */
1599 if (bl->loc_type == bp_loc_hardware_breakpoint)
1600 warning (_("hardware breakpoint %d not supported in overlay!"),
1601 bl->owner->number);
1602 else
1603 {
1604 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1605 bl->section);
1606 /* Set a software (trap) breakpoint at the LMA. */
1607 bl->overlay_target_info = bl->target_info;
1608 bl->overlay_target_info.placed_address = addr;
1609 val = target_insert_breakpoint (bl->gdbarch,
1610 &bl->overlay_target_info);
1611 if (val != 0)
1612 fprintf_unfiltered (tmp_error_stream,
1613 "Overlay breakpoint %d "
1614 "failed: in ROM?\n",
1615 bl->owner->number);
1616 }
1617 }
1618 /* Shall we set a breakpoint at the VMA? */
1619 if (section_is_mapped (bl->section))
1620 {
1621 /* Yes. This overlay section is mapped into memory. */
1622 if (bl->loc_type == bp_loc_hardware_breakpoint)
1623 val = target_insert_hw_breakpoint (bl->gdbarch,
1624 &bl->target_info);
1625 else
1626 val = target_insert_breakpoint (bl->gdbarch,
1627 &bl->target_info);
1628 }
1629 else
1630 {
1631 /* No. This breakpoint will not be inserted.
1632 No error, but do not mark the bp as 'inserted'. */
1633 return 0;
1634 }
1635 }
1636
1637 if (val)
1638 {
1639 /* Can't set the breakpoint. */
1640 if (solib_name_from_address (bl->pspace, bl->address))
1641 {
1642 /* See also: disable_breakpoints_in_shlibs. */
1643 val = 0;
1644 bl->shlib_disabled = 1;
1645 if (!*disabled_breaks)
1646 {
1647 fprintf_unfiltered (tmp_error_stream,
1648 "Cannot insert breakpoint %d.\n",
1649 bl->owner->number);
1650 fprintf_unfiltered (tmp_error_stream,
1651 "Temporarily disabling shared "
1652 "library breakpoints:\n");
1653 }
1654 *disabled_breaks = 1;
1655 fprintf_unfiltered (tmp_error_stream,
1656 "breakpoint #%d\n", bl->owner->number);
1657 }
1658 else
1659 {
1660 if (bl->loc_type == bp_loc_hardware_breakpoint)
1661 {
1662 *hw_breakpoint_error = 1;
1663 fprintf_unfiltered (tmp_error_stream,
1664 "Cannot insert hardware "
1665 "breakpoint %d.\n",
1666 bl->owner->number);
1667 }
1668 else
1669 {
1670 fprintf_unfiltered (tmp_error_stream,
1671 "Cannot insert breakpoint %d.\n",
1672 bl->owner->number);
1673 fprintf_filtered (tmp_error_stream,
1674 "Error accessing memory address ");
1675 fputs_filtered (paddress (bl->gdbarch, bl->address),
1676 tmp_error_stream);
1677 fprintf_filtered (tmp_error_stream, ": %s.\n",
1678 safe_strerror (val));
1679 }
1680
1681 }
1682 }
1683 else
1684 bl->inserted = 1;
1685
1686 return val;
1687 }
1688
1689 else if (bl->loc_type == bp_loc_hardware_watchpoint
1690 /* NOTE drow/2003-09-08: This state only exists for removing
1691 watchpoints. It's not clear that it's necessary... */
1692 && bl->owner->disposition != disp_del_at_next_stop)
1693 {
1694 gdb_assert (bl->owner->ops != NULL
1695 && bl->owner->ops->insert_location != NULL);
1696
1697 val = bl->owner->ops->insert_location (bl);
1698
1699 /* If trying to set a read-watchpoint, and it turns out it's not
1700 supported, try emulating one with an access watchpoint. */
1701 if (val == 1 && bl->watchpoint_type == hw_read)
1702 {
1703 struct bp_location *loc, **loc_temp;
1704
1705 /* But don't try to insert it, if there's already another
1706 hw_access location that would be considered a duplicate
1707 of this one. */
1708 ALL_BP_LOCATIONS (loc, loc_temp)
1709 if (loc != bl
1710 && loc->watchpoint_type == hw_access
1711 && watchpoint_locations_match (bl, loc))
1712 {
1713 bl->duplicate = 1;
1714 bl->inserted = 1;
1715 bl->target_info = loc->target_info;
1716 bl->watchpoint_type = hw_access;
1717 val = 0;
1718 break;
1719 }
1720
1721 if (val == 1)
1722 {
1723 bl->watchpoint_type = hw_access;
1724 val = bl->owner->ops->insert_location (bl);
1725
1726 if (val)
1727 /* Back to the original value. */
1728 bl->watchpoint_type = hw_read;
1729 }
1730 }
1731
1732 bl->inserted = (val == 0);
1733 }
1734
1735 else if (bl->owner->type == bp_catchpoint)
1736 {
1737 gdb_assert (bl->owner->ops != NULL
1738 && bl->owner->ops->insert_location != NULL);
1739
1740 val = bl->owner->ops->insert_location (bl);
1741 if (val)
1742 {
1743 bl->owner->enable_state = bp_disabled;
1744
1745 if (val == 1)
1746 warning (_("\
1747 Error inserting catchpoint %d: Your system does not support this type\n\
1748 of catchpoint."), bl->owner->number);
1749 else
1750 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1751 }
1752
1753 bl->inserted = (val == 0);
1754
1755 /* We've already printed an error message if there was a problem
1756 inserting this catchpoint, and we've disabled the catchpoint,
1757 so just return success. */
1758 return 0;
1759 }
1760
1761 return 0;
1762 }
1763
1764 /* This function is called when program space PSPACE is about to be
1765 deleted. It takes care of updating breakpoints to not reference
1766 PSPACE anymore. */
1767
1768 void
1769 breakpoint_program_space_exit (struct program_space *pspace)
1770 {
1771 struct breakpoint *b, *b_temp;
1772 struct bp_location *loc, **loc_temp;
1773
1774 /* Remove any breakpoint that was set through this program space. */
1775 ALL_BREAKPOINTS_SAFE (b, b_temp)
1776 {
1777 if (b->pspace == pspace)
1778 delete_breakpoint (b);
1779 }
1780
1781 /* Breakpoints set through other program spaces could have locations
1782 bound to PSPACE as well. Remove those. */
1783 ALL_BP_LOCATIONS (loc, loc_temp)
1784 {
1785 struct bp_location *tmp;
1786
1787 if (loc->pspace == pspace)
1788 {
1789 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
1790 if (loc->owner->loc == loc)
1791 loc->owner->loc = loc->next;
1792 else
1793 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1794 if (tmp->next == loc)
1795 {
1796 tmp->next = loc->next;
1797 break;
1798 }
1799 }
1800 }
1801
1802 /* Now update the global location list to permanently delete the
1803 removed locations above. */
1804 update_global_location_list (0);
1805 }
1806
1807 /* Make sure all breakpoints are inserted in inferior.
1808 Throws exception on any error.
1809 A breakpoint that is already inserted won't be inserted
1810 again, so calling this function twice is safe. */
1811 void
1812 insert_breakpoints (void)
1813 {
1814 struct breakpoint *bpt;
1815
1816 ALL_BREAKPOINTS (bpt)
1817 if (is_hardware_watchpoint (bpt))
1818 update_watchpoint (bpt, 0 /* don't reparse. */);
1819
1820 update_global_location_list (1);
1821
1822 /* update_global_location_list does not insert breakpoints when
1823 always_inserted_mode is not enabled. Explicitly insert them
1824 now. */
1825 if (!breakpoints_always_inserted_mode ())
1826 insert_breakpoint_locations ();
1827 }
1828
1829 /* insert_breakpoints is used when starting or continuing the program.
1830 remove_breakpoints is used when the program stops.
1831 Both return zero if successful,
1832 or an `errno' value if could not write the inferior. */
1833
1834 static void
1835 insert_breakpoint_locations (void)
1836 {
1837 struct breakpoint *bpt;
1838 struct bp_location *bl, **blp_tmp;
1839 int error = 0;
1840 int val = 0;
1841 int disabled_breaks = 0;
1842 int hw_breakpoint_error = 0;
1843
1844 struct ui_file *tmp_error_stream = mem_fileopen ();
1845 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
1846
1847 /* Explicitly mark the warning -- this will only be printed if
1848 there was an error. */
1849 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1850
1851 save_current_space_and_thread ();
1852
1853 ALL_BP_LOCATIONS (bl, blp_tmp)
1854 {
1855 if (!should_be_inserted (bl) || bl->inserted)
1856 continue;
1857
1858 /* There is no point inserting thread-specific breakpoints if
1859 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1860 has BL->OWNER always non-NULL. */
1861 if (bl->owner->thread != -1
1862 && !valid_thread_id (bl->owner->thread))
1863 continue;
1864
1865 switch_to_program_space_and_thread (bl->pspace);
1866
1867 /* For targets that support global breakpoints, there's no need
1868 to select an inferior to insert breakpoint to. In fact, even
1869 if we aren't attached to any process yet, we should still
1870 insert breakpoints. */
1871 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1872 && ptid_equal (inferior_ptid, null_ptid))
1873 continue;
1874
1875 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1876 &hw_breakpoint_error);
1877 if (val)
1878 error = val;
1879 }
1880
1881 /* If we failed to insert all locations of a watchpoint, remove
1882 them, as half-inserted watchpoint is of limited use. */
1883 ALL_BREAKPOINTS (bpt)
1884 {
1885 int some_failed = 0;
1886 struct bp_location *loc;
1887
1888 if (!is_hardware_watchpoint (bpt))
1889 continue;
1890
1891 if (!breakpoint_enabled (bpt))
1892 continue;
1893
1894 if (bpt->disposition == disp_del_at_next_stop)
1895 continue;
1896
1897 for (loc = bpt->loc; loc; loc = loc->next)
1898 if (!loc->inserted && should_be_inserted (loc))
1899 {
1900 some_failed = 1;
1901 break;
1902 }
1903 if (some_failed)
1904 {
1905 for (loc = bpt->loc; loc; loc = loc->next)
1906 if (loc->inserted)
1907 remove_breakpoint (loc, mark_uninserted);
1908
1909 hw_breakpoint_error = 1;
1910 fprintf_unfiltered (tmp_error_stream,
1911 "Could not insert hardware watchpoint %d.\n",
1912 bpt->number);
1913 error = -1;
1914 }
1915 }
1916
1917 if (error)
1918 {
1919 /* If a hardware breakpoint or watchpoint was inserted, add a
1920 message about possibly exhausted resources. */
1921 if (hw_breakpoint_error)
1922 {
1923 fprintf_unfiltered (tmp_error_stream,
1924 "Could not insert hardware breakpoints:\n\
1925 You may have requested too many hardware breakpoints/watchpoints.\n");
1926 }
1927 target_terminal_ours_for_output ();
1928 error_stream (tmp_error_stream);
1929 }
1930
1931 do_cleanups (cleanups);
1932 }
1933
1934 int
1935 remove_breakpoints (void)
1936 {
1937 struct bp_location *bl, **blp_tmp;
1938 int val = 0;
1939
1940 ALL_BP_LOCATIONS (bl, blp_tmp)
1941 {
1942 if (bl->inserted)
1943 val |= remove_breakpoint (bl, mark_uninserted);
1944 }
1945 return val;
1946 }
1947
1948 /* Remove breakpoints of process PID. */
1949
1950 int
1951 remove_breakpoints_pid (int pid)
1952 {
1953 struct bp_location *bl, **blp_tmp;
1954 int val;
1955 struct inferior *inf = find_inferior_pid (pid);
1956
1957 ALL_BP_LOCATIONS (bl, blp_tmp)
1958 {
1959 if (bl->pspace != inf->pspace)
1960 continue;
1961
1962 if (bl->inserted)
1963 {
1964 val = remove_breakpoint (bl, mark_uninserted);
1965 if (val != 0)
1966 return val;
1967 }
1968 }
1969 return 0;
1970 }
1971
1972 int
1973 remove_hw_watchpoints (void)
1974 {
1975 struct bp_location *bl, **blp_tmp;
1976 int val = 0;
1977
1978 ALL_BP_LOCATIONS (bl, blp_tmp)
1979 {
1980 if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
1981 val |= remove_breakpoint (bl, mark_uninserted);
1982 }
1983 return val;
1984 }
1985
1986 int
1987 reattach_breakpoints (int pid)
1988 {
1989 struct cleanup *old_chain;
1990 struct bp_location *bl, **blp_tmp;
1991 int val;
1992 struct ui_file *tmp_error_stream = mem_fileopen ();
1993 int dummy1 = 0, dummy2 = 0;
1994 struct inferior *inf;
1995 struct thread_info *tp;
1996
1997 tp = any_live_thread_of_process (pid);
1998 if (tp == NULL)
1999 return 1;
2000
2001 inf = find_inferior_pid (pid);
2002 old_chain = save_inferior_ptid ();
2003
2004 inferior_ptid = tp->ptid;
2005
2006 make_cleanup_ui_file_delete (tmp_error_stream);
2007
2008 ALL_BP_LOCATIONS (bl, blp_tmp)
2009 {
2010 if (bl->pspace != inf->pspace)
2011 continue;
2012
2013 if (bl->inserted)
2014 {
2015 bl->inserted = 0;
2016 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2017 if (val != 0)
2018 {
2019 do_cleanups (old_chain);
2020 return val;
2021 }
2022 }
2023 }
2024 do_cleanups (old_chain);
2025 return 0;
2026 }
2027
2028 static int internal_breakpoint_number = -1;
2029
2030 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2031 If INTERNAL is non-zero, the breakpoint number will be populated
2032 from internal_breakpoint_number and that variable decremented.
2033 Otherwis the breakpoint number will be populated from
2034 breakpoint_count and that value incremented. Internal breakpoints
2035 do not set the internal var bpnum. */
2036 static void
2037 set_breakpoint_number (int internal, struct breakpoint *b)
2038 {
2039 if (internal)
2040 b->number = internal_breakpoint_number--;
2041 else
2042 {
2043 set_breakpoint_count (breakpoint_count + 1);
2044 b->number = breakpoint_count;
2045 }
2046 }
2047
2048 static struct breakpoint *
2049 create_internal_breakpoint (struct gdbarch *gdbarch,
2050 CORE_ADDR address, enum bptype type)
2051 {
2052 struct symtab_and_line sal;
2053 struct breakpoint *b;
2054
2055 init_sal (&sal); /* Initialize to zeroes. */
2056
2057 sal.pc = address;
2058 sal.section = find_pc_overlay (sal.pc);
2059 sal.pspace = current_program_space;
2060
2061 b = set_raw_breakpoint (gdbarch, sal, type);
2062 b->number = internal_breakpoint_number--;
2063 b->disposition = disp_donttouch;
2064
2065 return b;
2066 }
2067
2068 static const char *const longjmp_names[] =
2069 {
2070 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2071 };
2072 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2073
2074 /* Per-objfile data private to breakpoint.c. */
2075 struct breakpoint_objfile_data
2076 {
2077 /* Minimal symbol for "_ovly_debug_event" (if any). */
2078 struct minimal_symbol *overlay_msym;
2079
2080 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2081 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2082
2083 /* Minimal symbol for "std::terminate()" (if any). */
2084 struct minimal_symbol *terminate_msym;
2085
2086 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2087 struct minimal_symbol *exception_msym;
2088 };
2089
2090 static const struct objfile_data *breakpoint_objfile_key;
2091
2092 /* Minimal symbol not found sentinel. */
2093 static struct minimal_symbol msym_not_found;
2094
2095 /* Returns TRUE if MSYM point to the "not found" sentinel. */
2096
2097 static int
2098 msym_not_found_p (const struct minimal_symbol *msym)
2099 {
2100 return msym == &msym_not_found;
2101 }
2102
2103 /* Return per-objfile data needed by breakpoint.c.
2104 Allocate the data if necessary. */
2105
2106 static struct breakpoint_objfile_data *
2107 get_breakpoint_objfile_data (struct objfile *objfile)
2108 {
2109 struct breakpoint_objfile_data *bp_objfile_data;
2110
2111 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2112 if (bp_objfile_data == NULL)
2113 {
2114 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2115 sizeof (*bp_objfile_data));
2116
2117 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2118 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2119 }
2120 return bp_objfile_data;
2121 }
2122
2123 static void
2124 create_overlay_event_breakpoint (void)
2125 {
2126 struct objfile *objfile;
2127 const char *const func_name = "_ovly_debug_event";
2128
2129 ALL_OBJFILES (objfile)
2130 {
2131 struct breakpoint *b;
2132 struct breakpoint_objfile_data *bp_objfile_data;
2133 CORE_ADDR addr;
2134
2135 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2136
2137 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2138 continue;
2139
2140 if (bp_objfile_data->overlay_msym == NULL)
2141 {
2142 struct minimal_symbol *m;
2143
2144 m = lookup_minimal_symbol_text (func_name, objfile);
2145 if (m == NULL)
2146 {
2147 /* Avoid future lookups in this objfile. */
2148 bp_objfile_data->overlay_msym = &msym_not_found;
2149 continue;
2150 }
2151 bp_objfile_data->overlay_msym = m;
2152 }
2153
2154 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2155 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2156 bp_overlay_event);
2157 b->addr_string = xstrdup (func_name);
2158
2159 if (overlay_debugging == ovly_auto)
2160 {
2161 b->enable_state = bp_enabled;
2162 overlay_events_enabled = 1;
2163 }
2164 else
2165 {
2166 b->enable_state = bp_disabled;
2167 overlay_events_enabled = 0;
2168 }
2169 }
2170 update_global_location_list (1);
2171 }
2172
2173 static void
2174 create_longjmp_master_breakpoint (void)
2175 {
2176 struct program_space *pspace;
2177 struct cleanup *old_chain;
2178
2179 old_chain = save_current_program_space ();
2180
2181 ALL_PSPACES (pspace)
2182 {
2183 struct objfile *objfile;
2184
2185 set_current_program_space (pspace);
2186
2187 ALL_OBJFILES (objfile)
2188 {
2189 int i;
2190 struct gdbarch *gdbarch;
2191 struct breakpoint_objfile_data *bp_objfile_data;
2192
2193 gdbarch = get_objfile_arch (objfile);
2194 if (!gdbarch_get_longjmp_target_p (gdbarch))
2195 continue;
2196
2197 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2198
2199 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2200 {
2201 struct breakpoint *b;
2202 const char *func_name;
2203 CORE_ADDR addr;
2204
2205 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2206 continue;
2207
2208 func_name = longjmp_names[i];
2209 if (bp_objfile_data->longjmp_msym[i] == NULL)
2210 {
2211 struct minimal_symbol *m;
2212
2213 m = lookup_minimal_symbol_text (func_name, objfile);
2214 if (m == NULL)
2215 {
2216 /* Prevent future lookups in this objfile. */
2217 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2218 continue;
2219 }
2220 bp_objfile_data->longjmp_msym[i] = m;
2221 }
2222
2223 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2224 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
2225 b->addr_string = xstrdup (func_name);
2226 b->enable_state = bp_disabled;
2227 }
2228 }
2229 }
2230 update_global_location_list (1);
2231
2232 do_cleanups (old_chain);
2233 }
2234
2235 /* Create a master std::terminate breakpoint. */
2236 static void
2237 create_std_terminate_master_breakpoint (void)
2238 {
2239 struct program_space *pspace;
2240 struct cleanup *old_chain;
2241 const char *const func_name = "std::terminate()";
2242
2243 old_chain = save_current_program_space ();
2244
2245 ALL_PSPACES (pspace)
2246 {
2247 struct objfile *objfile;
2248 CORE_ADDR addr;
2249
2250 set_current_program_space (pspace);
2251
2252 ALL_OBJFILES (objfile)
2253 {
2254 struct breakpoint *b;
2255 struct breakpoint_objfile_data *bp_objfile_data;
2256
2257 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2258
2259 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2260 continue;
2261
2262 if (bp_objfile_data->terminate_msym == NULL)
2263 {
2264 struct minimal_symbol *m;
2265
2266 m = lookup_minimal_symbol (func_name, NULL, objfile);
2267 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2268 && MSYMBOL_TYPE (m) != mst_file_text))
2269 {
2270 /* Prevent future lookups in this objfile. */
2271 bp_objfile_data->terminate_msym = &msym_not_found;
2272 continue;
2273 }
2274 bp_objfile_data->terminate_msym = m;
2275 }
2276
2277 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2278 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2279 bp_std_terminate_master);
2280 b->addr_string = xstrdup (func_name);
2281 b->enable_state = bp_disabled;
2282 }
2283 }
2284
2285 update_global_location_list (1);
2286
2287 do_cleanups (old_chain);
2288 }
2289
2290 /* Install a master breakpoint on the unwinder's debug hook. */
2291
2292 void
2293 create_exception_master_breakpoint (void)
2294 {
2295 struct objfile *objfile;
2296 const char *const func_name = "_Unwind_DebugHook";
2297
2298 ALL_OBJFILES (objfile)
2299 {
2300 struct breakpoint *b;
2301 struct gdbarch *gdbarch;
2302 struct breakpoint_objfile_data *bp_objfile_data;
2303 CORE_ADDR addr;
2304
2305 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2306
2307 if (msym_not_found_p (bp_objfile_data->exception_msym))
2308 continue;
2309
2310 gdbarch = get_objfile_arch (objfile);
2311
2312 if (bp_objfile_data->exception_msym == NULL)
2313 {
2314 struct minimal_symbol *debug_hook;
2315
2316 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2317 if (debug_hook == NULL)
2318 {
2319 bp_objfile_data->exception_msym = &msym_not_found;
2320 continue;
2321 }
2322
2323 bp_objfile_data->exception_msym = debug_hook;
2324 }
2325
2326 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2327 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2328 &current_target);
2329 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2330 b->addr_string = xstrdup (func_name);
2331 b->enable_state = bp_disabled;
2332 }
2333
2334 update_global_location_list (1);
2335 }
2336
2337 void
2338 update_breakpoints_after_exec (void)
2339 {
2340 struct breakpoint *b, *b_tmp;
2341 struct bp_location *bploc, **bplocp_tmp;
2342
2343 /* We're about to delete breakpoints from GDB's lists. If the
2344 INSERTED flag is true, GDB will try to lift the breakpoints by
2345 writing the breakpoints' "shadow contents" back into memory. The
2346 "shadow contents" are NOT valid after an exec, so GDB should not
2347 do that. Instead, the target is responsible from marking
2348 breakpoints out as soon as it detects an exec. We don't do that
2349 here instead, because there may be other attempts to delete
2350 breakpoints after detecting an exec and before reaching here. */
2351 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
2352 if (bploc->pspace == current_program_space)
2353 gdb_assert (!bploc->inserted);
2354
2355 ALL_BREAKPOINTS_SAFE (b, b_tmp)
2356 {
2357 if (b->pspace != current_program_space)
2358 continue;
2359
2360 /* Solib breakpoints must be explicitly reset after an exec(). */
2361 if (b->type == bp_shlib_event)
2362 {
2363 delete_breakpoint (b);
2364 continue;
2365 }
2366
2367 /* JIT breakpoints must be explicitly reset after an exec(). */
2368 if (b->type == bp_jit_event)
2369 {
2370 delete_breakpoint (b);
2371 continue;
2372 }
2373
2374 /* Thread event breakpoints must be set anew after an exec(),
2375 as must overlay event and longjmp master breakpoints. */
2376 if (b->type == bp_thread_event || b->type == bp_overlay_event
2377 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2378 || b->type == bp_exception_master)
2379 {
2380 delete_breakpoint (b);
2381 continue;
2382 }
2383
2384 /* Step-resume breakpoints are meaningless after an exec(). */
2385 if (b->type == bp_step_resume)
2386 {
2387 delete_breakpoint (b);
2388 continue;
2389 }
2390
2391 /* Longjmp and longjmp-resume breakpoints are also meaningless
2392 after an exec. */
2393 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2394 || b->type == bp_exception || b->type == bp_exception_resume)
2395 {
2396 delete_breakpoint (b);
2397 continue;
2398 }
2399
2400 if (b->type == bp_catchpoint)
2401 {
2402 /* For now, none of the bp_catchpoint breakpoints need to
2403 do anything at this point. In the future, if some of
2404 the catchpoints need to something, we will need to add
2405 a new method, and call this method from here. */
2406 continue;
2407 }
2408
2409 /* bp_finish is a special case. The only way we ought to be able
2410 to see one of these when an exec() has happened, is if the user
2411 caught a vfork, and then said "finish". Ordinarily a finish just
2412 carries them to the call-site of the current callee, by setting
2413 a temporary bp there and resuming. But in this case, the finish
2414 will carry them entirely through the vfork & exec.
2415
2416 We don't want to allow a bp_finish to remain inserted now. But
2417 we can't safely delete it, 'cause finish_command has a handle to
2418 the bp on a bpstat, and will later want to delete it. There's a
2419 chance (and I've seen it happen) that if we delete the bp_finish
2420 here, that its storage will get reused by the time finish_command
2421 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2422 We really must allow finish_command to delete a bp_finish.
2423
2424 In the absense of a general solution for the "how do we know
2425 it's safe to delete something others may have handles to?"
2426 problem, what we'll do here is just uninsert the bp_finish, and
2427 let finish_command delete it.
2428
2429 (We know the bp_finish is "doomed" in the sense that it's
2430 momentary, and will be deleted as soon as finish_command sees
2431 the inferior stopped. So it doesn't matter that the bp's
2432 address is probably bogus in the new a.out, unlike e.g., the
2433 solib breakpoints.) */
2434
2435 if (b->type == bp_finish)
2436 {
2437 continue;
2438 }
2439
2440 /* Without a symbolic address, we have little hope of the
2441 pre-exec() address meaning the same thing in the post-exec()
2442 a.out. */
2443 if (b->addr_string == NULL)
2444 {
2445 delete_breakpoint (b);
2446 continue;
2447 }
2448 }
2449 /* FIXME what about longjmp breakpoints? Re-create them here? */
2450 create_overlay_event_breakpoint ();
2451 create_longjmp_master_breakpoint ();
2452 create_std_terminate_master_breakpoint ();
2453 create_exception_master_breakpoint ();
2454 }
2455
2456 int
2457 detach_breakpoints (int pid)
2458 {
2459 struct bp_location *bl, **blp_tmp;
2460 int val = 0;
2461 struct cleanup *old_chain = save_inferior_ptid ();
2462 struct inferior *inf = current_inferior ();
2463
2464 if (pid == PIDGET (inferior_ptid))
2465 error (_("Cannot detach breakpoints of inferior_ptid"));
2466
2467 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
2468 inferior_ptid = pid_to_ptid (pid);
2469 ALL_BP_LOCATIONS (bl, blp_tmp)
2470 {
2471 if (bl->pspace != inf->pspace)
2472 continue;
2473
2474 if (bl->inserted)
2475 val |= remove_breakpoint_1 (bl, mark_inserted);
2476 }
2477
2478 /* Detach single-step breakpoints as well. */
2479 detach_single_step_breakpoints ();
2480
2481 do_cleanups (old_chain);
2482 return val;
2483 }
2484
2485 /* Remove the breakpoint location BL from the current address space.
2486 Note that this is used to detach breakpoints from a child fork.
2487 When we get here, the child isn't in the inferior list, and neither
2488 do we have objects to represent its address space --- we should
2489 *not* look at bl->pspace->aspace here. */
2490
2491 static int
2492 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
2493 {
2494 int val;
2495
2496 /* BL is never in moribund_locations by our callers. */
2497 gdb_assert (bl->owner != NULL);
2498
2499 if (bl->owner->enable_state == bp_permanent)
2500 /* Permanent breakpoints cannot be inserted or removed. */
2501 return 0;
2502
2503 /* The type of none suggests that owner is actually deleted.
2504 This should not ever happen. */
2505 gdb_assert (bl->owner->type != bp_none);
2506
2507 if (bl->loc_type == bp_loc_software_breakpoint
2508 || bl->loc_type == bp_loc_hardware_breakpoint)
2509 {
2510 /* "Normal" instruction breakpoint: either the standard
2511 trap-instruction bp (bp_breakpoint), or a
2512 bp_hardware_breakpoint. */
2513
2514 /* First check to see if we have to handle an overlay. */
2515 if (overlay_debugging == ovly_off
2516 || bl->section == NULL
2517 || !(section_is_overlay (bl->section)))
2518 {
2519 /* No overlay handling: just remove the breakpoint. */
2520
2521 if (bl->loc_type == bp_loc_hardware_breakpoint)
2522 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
2523 else
2524 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
2525 }
2526 else
2527 {
2528 /* This breakpoint is in an overlay section.
2529 Did we set a breakpoint at the LMA? */
2530 if (!overlay_events_enabled)
2531 {
2532 /* Yes -- overlay event support is not active, so we
2533 should have set a breakpoint at the LMA. Remove it.
2534 */
2535 /* Ignore any failures: if the LMA is in ROM, we will
2536 have already warned when we failed to insert it. */
2537 if (bl->loc_type == bp_loc_hardware_breakpoint)
2538 target_remove_hw_breakpoint (bl->gdbarch,
2539 &bl->overlay_target_info);
2540 else
2541 target_remove_breakpoint (bl->gdbarch,
2542 &bl->overlay_target_info);
2543 }
2544 /* Did we set a breakpoint at the VMA?
2545 If so, we will have marked the breakpoint 'inserted'. */
2546 if (bl->inserted)
2547 {
2548 /* Yes -- remove it. Previously we did not bother to
2549 remove the breakpoint if the section had been
2550 unmapped, but let's not rely on that being safe. We
2551 don't know what the overlay manager might do. */
2552 if (bl->loc_type == bp_loc_hardware_breakpoint)
2553 val = target_remove_hw_breakpoint (bl->gdbarch,
2554 &bl->target_info);
2555
2556 /* However, we should remove *software* breakpoints only
2557 if the section is still mapped, or else we overwrite
2558 wrong code with the saved shadow contents. */
2559 else if (section_is_mapped (bl->section))
2560 val = target_remove_breakpoint (bl->gdbarch,
2561 &bl->target_info);
2562 else
2563 val = 0;
2564 }
2565 else
2566 {
2567 /* No -- not inserted, so no need to remove. No error. */
2568 val = 0;
2569 }
2570 }
2571
2572 /* In some cases, we might not be able to remove a breakpoint
2573 in a shared library that has already been removed, but we
2574 have not yet processed the shlib unload event. */
2575 if (val && solib_name_from_address (bl->pspace, bl->address))
2576 val = 0;
2577
2578 if (val)
2579 return val;
2580 bl->inserted = (is == mark_inserted);
2581 }
2582 else if (bl->loc_type == bp_loc_hardware_watchpoint)
2583 {
2584 gdb_assert (bl->owner->ops != NULL
2585 && bl->owner->ops->remove_location != NULL);
2586
2587 bl->inserted = (is == mark_inserted);
2588 bl->owner->ops->remove_location (bl);
2589
2590 /* Failure to remove any of the hardware watchpoints comes here. */
2591 if ((is == mark_uninserted) && (bl->inserted))
2592 warning (_("Could not remove hardware watchpoint %d."),
2593 bl->owner->number);
2594 }
2595 else if (bl->owner->type == bp_catchpoint
2596 && breakpoint_enabled (bl->owner)
2597 && !bl->duplicate)
2598 {
2599 gdb_assert (bl->owner->ops != NULL
2600 && bl->owner->ops->remove_location != NULL);
2601
2602 val = bl->owner->ops->remove_location (bl);
2603 if (val)
2604 return val;
2605
2606 bl->inserted = (is == mark_inserted);
2607 }
2608
2609 return 0;
2610 }
2611
2612 static int
2613 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
2614 {
2615 int ret;
2616 struct cleanup *old_chain;
2617
2618 /* BL is never in moribund_locations by our callers. */
2619 gdb_assert (bl->owner != NULL);
2620
2621 if (bl->owner->enable_state == bp_permanent)
2622 /* Permanent breakpoints cannot be inserted or removed. */
2623 return 0;
2624
2625 /* The type of none suggests that owner is actually deleted.
2626 This should not ever happen. */
2627 gdb_assert (bl->owner->type != bp_none);
2628
2629 old_chain = save_current_space_and_thread ();
2630
2631 switch_to_program_space_and_thread (bl->pspace);
2632
2633 ret = remove_breakpoint_1 (bl, is);
2634
2635 do_cleanups (old_chain);
2636 return ret;
2637 }
2638
2639 /* Clear the "inserted" flag in all breakpoints. */
2640
2641 void
2642 mark_breakpoints_out (void)
2643 {
2644 struct bp_location *bl, **blp_tmp;
2645
2646 ALL_BP_LOCATIONS (bl, blp_tmp)
2647 if (bl->pspace == current_program_space)
2648 bl->inserted = 0;
2649 }
2650
2651 /* Clear the "inserted" flag in all breakpoints and delete any
2652 breakpoints which should go away between runs of the program.
2653
2654 Plus other such housekeeping that has to be done for breakpoints
2655 between runs.
2656
2657 Note: this function gets called at the end of a run (by
2658 generic_mourn_inferior) and when a run begins (by
2659 init_wait_for_inferior). */
2660
2661
2662
2663 void
2664 breakpoint_init_inferior (enum inf_context context)
2665 {
2666 struct breakpoint *b, *b_tmp;
2667 struct bp_location *bl, **blp_tmp;
2668 int ix;
2669 struct program_space *pspace = current_program_space;
2670
2671 /* If breakpoint locations are shared across processes, then there's
2672 nothing to do. */
2673 if (gdbarch_has_global_breakpoints (target_gdbarch))
2674 return;
2675
2676 ALL_BP_LOCATIONS (bl, blp_tmp)
2677 {
2678 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2679 if (bl->pspace == pspace
2680 && bl->owner->enable_state != bp_permanent)
2681 bl->inserted = 0;
2682 }
2683
2684 ALL_BREAKPOINTS_SAFE (b, b_tmp)
2685 {
2686 if (b->loc && b->loc->pspace != pspace)
2687 continue;
2688
2689 switch (b->type)
2690 {
2691 case bp_call_dummy:
2692
2693 /* If the call dummy breakpoint is at the entry point it will
2694 cause problems when the inferior is rerun, so we better get
2695 rid of it. */
2696
2697 case bp_watchpoint_scope:
2698
2699 /* Also get rid of scope breakpoints. */
2700
2701 case bp_shlib_event:
2702
2703 /* Also remove solib event breakpoints. Their addresses may
2704 have changed since the last time we ran the program.
2705 Actually we may now be debugging against different target;
2706 and so the solib backend that installed this breakpoint may
2707 not be used in by the target. E.g.,
2708
2709 (gdb) file prog-linux
2710 (gdb) run # native linux target
2711 ...
2712 (gdb) kill
2713 (gdb) file prog-win.exe
2714 (gdb) tar rem :9999 # remote Windows gdbserver.
2715 */
2716
2717 delete_breakpoint (b);
2718 break;
2719
2720 case bp_watchpoint:
2721 case bp_hardware_watchpoint:
2722 case bp_read_watchpoint:
2723 case bp_access_watchpoint:
2724
2725 /* Likewise for watchpoints on local expressions. */
2726 if (b->exp_valid_block != NULL)
2727 delete_breakpoint (b);
2728 else if (context == inf_starting)
2729 {
2730 /* Reset val field to force reread of starting value in
2731 insert_breakpoints. */
2732 if (b->val)
2733 value_free (b->val);
2734 b->val = NULL;
2735 b->val_valid = 0;
2736 }
2737 break;
2738 default:
2739 break;
2740 }
2741 }
2742
2743 /* Get rid of the moribund locations. */
2744 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2745 decref_bp_location (&bl);
2746 VEC_free (bp_location_p, moribund_locations);
2747 }
2748
2749 /* These functions concern about actual breakpoints inserted in the
2750 target --- to e.g. check if we need to do decr_pc adjustment or if
2751 we need to hop over the bkpt --- so we check for address space
2752 match, not program space. */
2753
2754 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2755 exists at PC. It returns ordinary_breakpoint_here if it's an
2756 ordinary breakpoint, or permanent_breakpoint_here if it's a
2757 permanent breakpoint.
2758 - When continuing from a location with an ordinary breakpoint, we
2759 actually single step once before calling insert_breakpoints.
2760 - When continuing from a localion with a permanent breakpoint, we
2761 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2762 the target, to advance the PC past the breakpoint. */
2763
2764 enum breakpoint_here
2765 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2766 {
2767 struct bp_location *bl, **blp_tmp;
2768 int any_breakpoint_here = 0;
2769
2770 ALL_BP_LOCATIONS (bl, blp_tmp)
2771 {
2772 if (bl->loc_type != bp_loc_software_breakpoint
2773 && bl->loc_type != bp_loc_hardware_breakpoint)
2774 continue;
2775
2776 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2777 if ((breakpoint_enabled (bl->owner)
2778 || bl->owner->enable_state == bp_permanent)
2779 && breakpoint_address_match (bl->pspace->aspace, bl->address,
2780 aspace, pc))
2781 {
2782 if (overlay_debugging
2783 && section_is_overlay (bl->section)
2784 && !section_is_mapped (bl->section))
2785 continue; /* unmapped overlay -- can't be a match */
2786 else if (bl->owner->enable_state == bp_permanent)
2787 return permanent_breakpoint_here;
2788 else
2789 any_breakpoint_here = 1;
2790 }
2791 }
2792
2793 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
2794 }
2795
2796 /* Return true if there's a moribund breakpoint at PC. */
2797
2798 int
2799 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2800 {
2801 struct bp_location *loc;
2802 int ix;
2803
2804 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
2805 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2806 aspace, pc))
2807 return 1;
2808
2809 return 0;
2810 }
2811
2812 /* Returns non-zero if there's a breakpoint inserted at PC, which is
2813 inserted using regular breakpoint_chain / bp_location array
2814 mechanism. This does not check for single-step breakpoints, which
2815 are inserted and removed using direct target manipulation. */
2816
2817 int
2818 regular_breakpoint_inserted_here_p (struct address_space *aspace,
2819 CORE_ADDR pc)
2820 {
2821 struct bp_location *bl, **blp_tmp;
2822
2823 ALL_BP_LOCATIONS (bl, blp_tmp)
2824 {
2825 if (bl->loc_type != bp_loc_software_breakpoint
2826 && bl->loc_type != bp_loc_hardware_breakpoint)
2827 continue;
2828
2829 if (bl->inserted
2830 && breakpoint_address_match (bl->pspace->aspace, bl->address,
2831 aspace, pc))
2832 {
2833 if (overlay_debugging
2834 && section_is_overlay (bl->section)
2835 && !section_is_mapped (bl->section))
2836 continue; /* unmapped overlay -- can't be a match */
2837 else
2838 return 1;
2839 }
2840 }
2841 return 0;
2842 }
2843
2844 /* Returns non-zero iff there's either regular breakpoint
2845 or a single step breakpoint inserted at PC. */
2846
2847 int
2848 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
2849 {
2850 if (regular_breakpoint_inserted_here_p (aspace, pc))
2851 return 1;
2852
2853 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2854 return 1;
2855
2856 return 0;
2857 }
2858
2859 /* This function returns non-zero iff there is a software breakpoint
2860 inserted at PC. */
2861
2862 int
2863 software_breakpoint_inserted_here_p (struct address_space *aspace,
2864 CORE_ADDR pc)
2865 {
2866 struct bp_location *bl, **blp_tmp;
2867
2868 ALL_BP_LOCATIONS (bl, blp_tmp)
2869 {
2870 if (bl->loc_type != bp_loc_software_breakpoint)
2871 continue;
2872
2873 if (bl->inserted
2874 && breakpoint_address_match (bl->pspace->aspace, bl->address,
2875 aspace, pc))
2876 {
2877 if (overlay_debugging
2878 && section_is_overlay (bl->section)
2879 && !section_is_mapped (bl->section))
2880 continue; /* unmapped overlay -- can't be a match */
2881 else
2882 return 1;
2883 }
2884 }
2885
2886 /* Also check for software single-step breakpoints. */
2887 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2888 return 1;
2889
2890 return 0;
2891 }
2892
2893 int
2894 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2895 CORE_ADDR addr, ULONGEST len)
2896 {
2897 struct breakpoint *bpt;
2898
2899 ALL_BREAKPOINTS (bpt)
2900 {
2901 struct bp_location *loc;
2902
2903 if (bpt->type != bp_hardware_watchpoint
2904 && bpt->type != bp_access_watchpoint)
2905 continue;
2906
2907 if (!breakpoint_enabled (bpt))
2908 continue;
2909
2910 for (loc = bpt->loc; loc; loc = loc->next)
2911 if (loc->pspace->aspace == aspace && loc->inserted)
2912 {
2913 CORE_ADDR l, h;
2914
2915 /* Check for intersection. */
2916 l = max (loc->address, addr);
2917 h = min (loc->address + loc->length, addr + len);
2918 if (l < h)
2919 return 1;
2920 }
2921 }
2922 return 0;
2923 }
2924
2925 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2926 PC is valid for process/thread PTID. */
2927
2928 int
2929 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2930 ptid_t ptid)
2931 {
2932 struct bp_location *bl, **blp_tmp;
2933 /* The thread and task IDs associated to PTID, computed lazily. */
2934 int thread = -1;
2935 int task = 0;
2936
2937 ALL_BP_LOCATIONS (bl, blp_tmp)
2938 {
2939 if (bl->loc_type != bp_loc_software_breakpoint
2940 && bl->loc_type != bp_loc_hardware_breakpoint)
2941 continue;
2942
2943 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2944 if (!breakpoint_enabled (bl->owner)
2945 && bl->owner->enable_state != bp_permanent)
2946 continue;
2947
2948 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
2949 aspace, pc))
2950 continue;
2951
2952 if (bl->owner->thread != -1)
2953 {
2954 /* This is a thread-specific breakpoint. Check that ptid
2955 matches that thread. If thread hasn't been computed yet,
2956 it is now time to do so. */
2957 if (thread == -1)
2958 thread = pid_to_thread_id (ptid);
2959 if (bl->owner->thread != thread)
2960 continue;
2961 }
2962
2963 if (bl->owner->task != 0)
2964 {
2965 /* This is a task-specific breakpoint. Check that ptid
2966 matches that task. If task hasn't been computed yet,
2967 it is now time to do so. */
2968 if (task == 0)
2969 task = ada_get_task_number (ptid);
2970 if (bl->owner->task != task)
2971 continue;
2972 }
2973
2974 if (overlay_debugging
2975 && section_is_overlay (bl->section)
2976 && !section_is_mapped (bl->section))
2977 continue; /* unmapped overlay -- can't be a match */
2978
2979 return 1;
2980 }
2981
2982 return 0;
2983 }
2984 \f
2985
2986 /* bpstat stuff. External routines' interfaces are documented
2987 in breakpoint.h. */
2988
2989 int
2990 ep_is_catchpoint (struct breakpoint *ep)
2991 {
2992 return (ep->type == bp_catchpoint);
2993 }
2994
2995 /* Frees any storage that is part of a bpstat. Does not walk the
2996 'next' chain. */
2997
2998 static void
2999 bpstat_free (bpstat bs)
3000 {
3001 if (bs->old_val != NULL)
3002 value_free (bs->old_val);
3003 decref_counted_command_line (&bs->commands);
3004 decref_bp_location (&bs->bp_location_at);
3005 xfree (bs);
3006 }
3007
3008 /* Clear a bpstat so that it says we are not at any breakpoint.
3009 Also free any storage that is part of a bpstat. */
3010
3011 void
3012 bpstat_clear (bpstat *bsp)
3013 {
3014 bpstat p;
3015 bpstat q;
3016
3017 if (bsp == 0)
3018 return;
3019 p = *bsp;
3020 while (p != NULL)
3021 {
3022 q = p->next;
3023 bpstat_free (p);
3024 p = q;
3025 }
3026 *bsp = NULL;
3027 }
3028
3029 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3030 is part of the bpstat is copied as well. */
3031
3032 bpstat
3033 bpstat_copy (bpstat bs)
3034 {
3035 bpstat p = NULL;
3036 bpstat tmp;
3037 bpstat retval = NULL;
3038
3039 if (bs == NULL)
3040 return bs;
3041
3042 for (; bs != NULL; bs = bs->next)
3043 {
3044 tmp = (bpstat) xmalloc (sizeof (*tmp));
3045 memcpy (tmp, bs, sizeof (*tmp));
3046 incref_counted_command_line (tmp->commands);
3047 incref_bp_location (tmp->bp_location_at);
3048 if (bs->old_val != NULL)
3049 {
3050 tmp->old_val = value_copy (bs->old_val);
3051 release_value (tmp->old_val);
3052 }
3053
3054 if (p == NULL)
3055 /* This is the first thing in the chain. */
3056 retval = tmp;
3057 else
3058 p->next = tmp;
3059 p = tmp;
3060 }
3061 p->next = NULL;
3062 return retval;
3063 }
3064
3065 /* Find the bpstat associated with this breakpoint. */
3066
3067 bpstat
3068 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3069 {
3070 if (bsp == NULL)
3071 return NULL;
3072
3073 for (; bsp != NULL; bsp = bsp->next)
3074 {
3075 if (bsp->breakpoint_at == breakpoint)
3076 return bsp;
3077 }
3078 return NULL;
3079 }
3080
3081 /* Put in *NUM the breakpoint number of the first breakpoint we are
3082 stopped at. *BSP upon return is a bpstat which points to the
3083 remaining breakpoints stopped at (but which is not guaranteed to be
3084 good for anything but further calls to bpstat_num).
3085
3086 Return 0 if passed a bpstat which does not indicate any breakpoints.
3087 Return -1 if stopped at a breakpoint that has been deleted since
3088 we set it.
3089 Return 1 otherwise. */
3090
3091 int
3092 bpstat_num (bpstat *bsp, int *num)
3093 {
3094 struct breakpoint *b;
3095
3096 if ((*bsp) == NULL)
3097 return 0; /* No more breakpoint values */
3098
3099 /* We assume we'll never have several bpstats that correspond to a
3100 single breakpoint -- otherwise, this function might return the
3101 same number more than once and this will look ugly. */
3102 b = (*bsp)->breakpoint_at;
3103 *bsp = (*bsp)->next;
3104 if (b == NULL)
3105 return -1; /* breakpoint that's been deleted since */
3106
3107 *num = b->number; /* We have its number */
3108 return 1;
3109 }
3110
3111 /* Modify BS so that the actions will not be performed. */
3112
3113 void
3114 bpstat_clear_actions (bpstat bs)
3115 {
3116 for (; bs != NULL; bs = bs->next)
3117 {
3118 decref_counted_command_line (&bs->commands);
3119 bs->commands_left = NULL;
3120 if (bs->old_val != NULL)
3121 {
3122 value_free (bs->old_val);
3123 bs->old_val = NULL;
3124 }
3125 }
3126 }
3127
3128 /* Called when a command is about to proceed the inferior. */
3129
3130 static void
3131 breakpoint_about_to_proceed (void)
3132 {
3133 if (!ptid_equal (inferior_ptid, null_ptid))
3134 {
3135 struct thread_info *tp = inferior_thread ();
3136
3137 /* Allow inferior function calls in breakpoint commands to not
3138 interrupt the command list. When the call finishes
3139 successfully, the inferior will be standing at the same
3140 breakpoint as if nothing happened. */
3141 if (tp->control.in_infcall)
3142 return;
3143 }
3144
3145 breakpoint_proceeded = 1;
3146 }
3147
3148 /* Stub for cleaning up our state if we error-out of a breakpoint
3149 command. */
3150 static void
3151 cleanup_executing_breakpoints (void *ignore)
3152 {
3153 executing_breakpoint_commands = 0;
3154 }
3155
3156 /* Execute all the commands associated with all the breakpoints at
3157 this location. Any of these commands could cause the process to
3158 proceed beyond this point, etc. We look out for such changes by
3159 checking the global "breakpoint_proceeded" after each command.
3160
3161 Returns true if a breakpoint command resumed the inferior. In that
3162 case, it is the caller's responsibility to recall it again with the
3163 bpstat of the current thread. */
3164
3165 static int
3166 bpstat_do_actions_1 (bpstat *bsp)
3167 {
3168 bpstat bs;
3169 struct cleanup *old_chain;
3170 int again = 0;
3171
3172 /* Avoid endless recursion if a `source' command is contained
3173 in bs->commands. */
3174 if (executing_breakpoint_commands)
3175 return 0;
3176
3177 executing_breakpoint_commands = 1;
3178 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3179
3180 /* This pointer will iterate over the list of bpstat's. */
3181 bs = *bsp;
3182
3183 breakpoint_proceeded = 0;
3184 for (; bs != NULL; bs = bs->next)
3185 {
3186 struct counted_command_line *ccmd;
3187 struct command_line *cmd;
3188 struct cleanup *this_cmd_tree_chain;
3189
3190 /* Take ownership of the BSP's command tree, if it has one.
3191
3192 The command tree could legitimately contain commands like
3193 'step' and 'next', which call clear_proceed_status, which
3194 frees stop_bpstat's command tree. To make sure this doesn't
3195 free the tree we're executing out from under us, we need to
3196 take ownership of the tree ourselves. Since a given bpstat's
3197 commands are only executed once, we don't need to copy it; we
3198 can clear the pointer in the bpstat, and make sure we free
3199 the tree when we're done. */
3200 ccmd = bs->commands;
3201 bs->commands = NULL;
3202 this_cmd_tree_chain
3203 = make_cleanup_decref_counted_command_line (&ccmd);
3204 cmd = bs->commands_left;
3205 bs->commands_left = NULL;
3206
3207 while (cmd != NULL)
3208 {
3209 execute_control_command (cmd);
3210
3211 if (breakpoint_proceeded)
3212 break;
3213 else
3214 cmd = cmd->next;
3215 }
3216
3217 /* We can free this command tree now. */
3218 do_cleanups (this_cmd_tree_chain);
3219
3220 if (breakpoint_proceeded)
3221 {
3222 if (target_can_async_p ())
3223 /* If we are in async mode, then the target might be still
3224 running, not stopped at any breakpoint, so nothing for
3225 us to do here -- just return to the event loop. */
3226 ;
3227 else
3228 /* In sync mode, when execute_control_command returns
3229 we're already standing on the next breakpoint.
3230 Breakpoint commands for that stop were not run, since
3231 execute_command does not run breakpoint commands --
3232 only command_line_handler does, but that one is not
3233 involved in execution of breakpoint commands. So, we
3234 can now execute breakpoint commands. It should be
3235 noted that making execute_command do bpstat actions is
3236 not an option -- in this case we'll have recursive
3237 invocation of bpstat for each breakpoint with a
3238 command, and can easily blow up GDB stack. Instead, we
3239 return true, which will trigger the caller to recall us
3240 with the new stop_bpstat. */
3241 again = 1;
3242 break;
3243 }
3244 }
3245 do_cleanups (old_chain);
3246 return again;
3247 }
3248
3249 void
3250 bpstat_do_actions (void)
3251 {
3252 /* Do any commands attached to breakpoint we are stopped at. */
3253 while (!ptid_equal (inferior_ptid, null_ptid)
3254 && target_has_execution
3255 && !is_exited (inferior_ptid)
3256 && !is_executing (inferior_ptid))
3257 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3258 and only return when it is stopped at the next breakpoint, we
3259 keep doing breakpoint actions until it returns false to
3260 indicate the inferior was not resumed. */
3261 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
3262 break;
3263 }
3264
3265 /* Print out the (old or new) value associated with a watchpoint. */
3266
3267 static void
3268 watchpoint_value_print (struct value *val, struct ui_file *stream)
3269 {
3270 if (val == NULL)
3271 fprintf_unfiltered (stream, _("<unreadable>"));
3272 else
3273 {
3274 struct value_print_options opts;
3275 get_user_print_options (&opts);
3276 value_print (val, stream, &opts);
3277 }
3278 }
3279
3280 /* This is the normal print function for a bpstat. In the future,
3281 much of this logic could (should?) be moved to bpstat_stop_status,
3282 by having it set different print_it values.
3283
3284 Current scheme: When we stop, bpstat_print() is called. It loops
3285 through the bpstat list of things causing this stop, calling the
3286 print_bp_stop_message function on each one. The behavior of the
3287 print_bp_stop_message function depends on the print_it field of
3288 bpstat. If such field so indicates, call this function here.
3289
3290 Return values from this routine (ultimately used by bpstat_print()
3291 and normal_stop() to decide what to do):
3292 PRINT_NOTHING: Means we already printed all we needed to print,
3293 don't print anything else.
3294 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3295 that something to be followed by a location.
3296 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3297 that something to be followed by a location.
3298 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3299 analysis. */
3300
3301 static enum print_stop_action
3302 print_it_typical (bpstat bs)
3303 {
3304 struct cleanup *old_chain;
3305 struct breakpoint *b;
3306 const struct bp_location *bl;
3307 struct ui_stream *stb;
3308 int bp_temp = 0;
3309 enum print_stop_action result;
3310
3311 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3312 which has since been deleted. */
3313 if (bs->breakpoint_at == NULL)
3314 return PRINT_UNKNOWN;
3315
3316 gdb_assert (bs->bp_location_at != NULL);
3317
3318 bl = bs->bp_location_at;
3319 b = bs->breakpoint_at;
3320
3321 stb = ui_out_stream_new (uiout);
3322 old_chain = make_cleanup_ui_out_stream_delete (stb);
3323
3324 switch (b->type)
3325 {
3326 case bp_breakpoint:
3327 case bp_hardware_breakpoint:
3328 bp_temp = b->disposition == disp_del;
3329 if (bl->address != bl->requested_address)
3330 breakpoint_adjustment_warning (bl->requested_address,
3331 bl->address,
3332 b->number, 1);
3333 annotate_breakpoint (b->number);
3334 if (bp_temp)
3335 ui_out_text (uiout, "\nTemporary breakpoint ");
3336 else
3337 ui_out_text (uiout, "\nBreakpoint ");
3338 if (ui_out_is_mi_like_p (uiout))
3339 {
3340 ui_out_field_string (uiout, "reason",
3341 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3342 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3343 }
3344 ui_out_field_int (uiout, "bkptno", b->number);
3345 ui_out_text (uiout, ", ");
3346 result = PRINT_SRC_AND_LOC;
3347 break;
3348
3349 case bp_shlib_event:
3350 /* Did we stop because the user set the stop_on_solib_events
3351 variable? (If so, we report this as a generic, "Stopped due
3352 to shlib event" message.) */
3353 printf_filtered (_("Stopped due to shared library event\n"));
3354 result = PRINT_NOTHING;
3355 break;
3356
3357 case bp_thread_event:
3358 /* Not sure how we will get here.
3359 GDB should not stop for these breakpoints. */
3360 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
3361 result = PRINT_NOTHING;
3362 break;
3363
3364 case bp_overlay_event:
3365 /* By analogy with the thread event, GDB should not stop for these. */
3366 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
3367 result = PRINT_NOTHING;
3368 break;
3369
3370 case bp_longjmp_master:
3371 /* These should never be enabled. */
3372 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3373 result = PRINT_NOTHING;
3374 break;
3375
3376 case bp_std_terminate_master:
3377 /* These should never be enabled. */
3378 printf_filtered (_("std::terminate Master Breakpoint: "
3379 "gdb should not stop!\n"));
3380 result = PRINT_NOTHING;
3381 break;
3382
3383 case bp_exception_master:
3384 /* These should never be enabled. */
3385 printf_filtered (_("Exception Master Breakpoint: "
3386 "gdb should not stop!\n"));
3387 result = PRINT_NOTHING;
3388 break;
3389
3390 case bp_watchpoint:
3391 case bp_hardware_watchpoint:
3392 annotate_watchpoint (b->number);
3393 if (ui_out_is_mi_like_p (uiout))
3394 ui_out_field_string
3395 (uiout, "reason",
3396 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3397 mention (b);
3398 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3399 ui_out_text (uiout, "\nOld value = ");
3400 watchpoint_value_print (bs->old_val, stb->stream);
3401 ui_out_field_stream (uiout, "old", stb);
3402 ui_out_text (uiout, "\nNew value = ");
3403 watchpoint_value_print (b->val, stb->stream);
3404 ui_out_field_stream (uiout, "new", stb);
3405 ui_out_text (uiout, "\n");
3406 /* More than one watchpoint may have been triggered. */
3407 result = PRINT_UNKNOWN;
3408 break;
3409
3410 case bp_read_watchpoint:
3411 if (ui_out_is_mi_like_p (uiout))
3412 ui_out_field_string
3413 (uiout, "reason",
3414 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
3415 mention (b);
3416 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3417 ui_out_text (uiout, "\nValue = ");
3418 watchpoint_value_print (b->val, stb->stream);
3419 ui_out_field_stream (uiout, "value", stb);
3420 ui_out_text (uiout, "\n");
3421 result = PRINT_UNKNOWN;
3422 break;
3423
3424 case bp_access_watchpoint:
3425 if (bs->old_val != NULL)
3426 {
3427 annotate_watchpoint (b->number);
3428 if (ui_out_is_mi_like_p (uiout))
3429 ui_out_field_string
3430 (uiout, "reason",
3431 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
3432 mention (b);
3433 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3434 ui_out_text (uiout, "\nOld value = ");
3435 watchpoint_value_print (bs->old_val, stb->stream);
3436 ui_out_field_stream (uiout, "old", stb);
3437 ui_out_text (uiout, "\nNew value = ");
3438 }
3439 else
3440 {
3441 mention (b);
3442 if (ui_out_is_mi_like_p (uiout))
3443 ui_out_field_string
3444 (uiout, "reason",
3445 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
3446 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3447 ui_out_text (uiout, "\nValue = ");
3448 }
3449 watchpoint_value_print (b->val, stb->stream);
3450 ui_out_field_stream (uiout, "new", stb);
3451 ui_out_text (uiout, "\n");
3452 result = PRINT_UNKNOWN;
3453 break;
3454
3455 /* Fall through, we don't deal with these types of breakpoints
3456 here. */
3457
3458 case bp_finish:
3459 if (ui_out_is_mi_like_p (uiout))
3460 ui_out_field_string
3461 (uiout, "reason",
3462 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
3463 result = PRINT_UNKNOWN;
3464 break;
3465
3466 case bp_until:
3467 if (ui_out_is_mi_like_p (uiout))
3468 ui_out_field_string
3469 (uiout, "reason",
3470 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
3471 result = PRINT_UNKNOWN;
3472 break;
3473
3474 case bp_none:
3475 case bp_longjmp:
3476 case bp_longjmp_resume:
3477 case bp_exception:
3478 case bp_exception_resume:
3479 case bp_step_resume:
3480 case bp_watchpoint_scope:
3481 case bp_call_dummy:
3482 case bp_std_terminate:
3483 case bp_tracepoint:
3484 case bp_fast_tracepoint:
3485 case bp_jit_event:
3486 default:
3487 result = PRINT_UNKNOWN;
3488 break;
3489 }
3490
3491 do_cleanups (old_chain);
3492 return result;
3493 }
3494
3495 /* Generic routine for printing messages indicating why we
3496 stopped. The behavior of this function depends on the value
3497 'print_it' in the bpstat structure. Under some circumstances we
3498 may decide not to print anything here and delegate the task to
3499 normal_stop(). */
3500
3501 static enum print_stop_action
3502 print_bp_stop_message (bpstat bs)
3503 {
3504 switch (bs->print_it)
3505 {
3506 case print_it_noop:
3507 /* Nothing should be printed for this bpstat entry. */
3508 return PRINT_UNKNOWN;
3509 break;
3510
3511 case print_it_done:
3512 /* We still want to print the frame, but we already printed the
3513 relevant messages. */
3514 return PRINT_SRC_AND_LOC;
3515 break;
3516
3517 case print_it_normal:
3518 {
3519 struct breakpoint *b = bs->breakpoint_at;
3520
3521 /* Normal case. Call the breakpoint's print_it method, or
3522 print_it_typical. */
3523 /* FIXME: how breakpoint can ever be NULL here? */
3524 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
3525 return b->ops->print_it (b);
3526 else
3527 return print_it_typical (bs);
3528 }
3529 break;
3530
3531 default:
3532 internal_error (__FILE__, __LINE__,
3533 _("print_bp_stop_message: unrecognized enum value"));
3534 break;
3535 }
3536 }
3537
3538 /* Print a message indicating what happened. This is called from
3539 normal_stop(). The input to this routine is the head of the bpstat
3540 list - a list of the eventpoints that caused this stop. This
3541 routine calls the generic print routine for printing a message
3542 about reasons for stopping. This will print (for example) the
3543 "Breakpoint n," part of the output. The return value of this
3544 routine is one of:
3545
3546 PRINT_UNKNOWN: Means we printed nothing.
3547 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
3548 code to print the location. An example is
3549 "Breakpoint 1, " which should be followed by
3550 the location.
3551 PRINT_SRC_ONLY: Means we printed something, but there is no need
3552 to also print the location part of the message.
3553 An example is the catch/throw messages, which
3554 don't require a location appended to the end.
3555 PRINT_NOTHING: We have done some printing and we don't need any
3556 further info to be printed. */
3557
3558 enum print_stop_action
3559 bpstat_print (bpstat bs)
3560 {
3561 int val;
3562
3563 /* Maybe another breakpoint in the chain caused us to stop.
3564 (Currently all watchpoints go on the bpstat whether hit or not.
3565 That probably could (should) be changed, provided care is taken
3566 with respect to bpstat_explains_signal). */
3567 for (; bs; bs = bs->next)
3568 {
3569 val = print_bp_stop_message (bs);
3570 if (val == PRINT_SRC_ONLY
3571 || val == PRINT_SRC_AND_LOC
3572 || val == PRINT_NOTHING)
3573 return val;
3574 }
3575
3576 /* We reached the end of the chain, or we got a null BS to start
3577 with and nothing was printed. */
3578 return PRINT_UNKNOWN;
3579 }
3580
3581 /* Evaluate the expression EXP and return 1 if value is zero. This is
3582 used inside a catch_errors to evaluate the breakpoint condition.
3583 The argument is a "struct expression *" that has been cast to a
3584 "char *" to make it pass through catch_errors. */
3585
3586 static int
3587 breakpoint_cond_eval (void *exp)
3588 {
3589 struct value *mark = value_mark ();
3590 int i = !value_true (evaluate_expression ((struct expression *) exp));
3591
3592 value_free_to_mark (mark);
3593 return i;
3594 }
3595
3596 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
3597
3598 static bpstat
3599 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
3600 {
3601 bpstat bs;
3602
3603 bs = (bpstat) xmalloc (sizeof (*bs));
3604 bs->next = NULL;
3605 **bs_link_pointer = bs;
3606 *bs_link_pointer = &bs->next;
3607 bs->breakpoint_at = bl->owner;
3608 bs->bp_location_at = bl;
3609 incref_bp_location (bl);
3610 /* If the condition is false, etc., don't do the commands. */
3611 bs->commands = NULL;
3612 bs->commands_left = NULL;
3613 bs->old_val = NULL;
3614 bs->print_it = print_it_normal;
3615 return bs;
3616 }
3617 \f
3618 /* The target has stopped with waitstatus WS. Check if any hardware
3619 watchpoints have triggered, according to the target. */
3620
3621 int
3622 watchpoints_triggered (struct target_waitstatus *ws)
3623 {
3624 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
3625 CORE_ADDR addr;
3626 struct breakpoint *b;
3627
3628 if (!stopped_by_watchpoint)
3629 {
3630 /* We were not stopped by a watchpoint. Mark all watchpoints
3631 as not triggered. */
3632 ALL_BREAKPOINTS (b)
3633 if (is_hardware_watchpoint (b))
3634 b->watchpoint_triggered = watch_triggered_no;
3635
3636 return 0;
3637 }
3638
3639 if (!target_stopped_data_address (&current_target, &addr))
3640 {
3641 /* We were stopped by a watchpoint, but we don't know where.
3642 Mark all watchpoints as unknown. */
3643 ALL_BREAKPOINTS (b)
3644 if (is_hardware_watchpoint (b))
3645 b->watchpoint_triggered = watch_triggered_unknown;
3646
3647 return stopped_by_watchpoint;
3648 }
3649
3650 /* The target could report the data address. Mark watchpoints
3651 affected by this data address as triggered, and all others as not
3652 triggered. */
3653
3654 ALL_BREAKPOINTS (b)
3655 if (is_hardware_watchpoint (b))
3656 {
3657 struct bp_location *loc;
3658
3659 b->watchpoint_triggered = watch_triggered_no;
3660 for (loc = b->loc; loc; loc = loc->next)
3661 /* Exact match not required. Within range is
3662 sufficient. */
3663 if (target_watchpoint_addr_within_range (&current_target,
3664 addr, loc->address,
3665 loc->length))
3666 {
3667 b->watchpoint_triggered = watch_triggered_yes;
3668 break;
3669 }
3670 }
3671
3672 return 1;
3673 }
3674
3675 /* Possible return values for watchpoint_check (this can't be an enum
3676 because of check_errors). */
3677 /* The watchpoint has been deleted. */
3678 #define WP_DELETED 1
3679 /* The value has changed. */
3680 #define WP_VALUE_CHANGED 2
3681 /* The value has not changed. */
3682 #define WP_VALUE_NOT_CHANGED 3
3683 /* Ignore this watchpoint, no matter if the value changed or not. */
3684 #define WP_IGNORE 4
3685
3686 #define BP_TEMPFLAG 1
3687 #define BP_HARDWAREFLAG 2
3688
3689 /* Evaluate watchpoint condition expression and check if its value
3690 changed.
3691
3692 P should be a pointer to struct bpstat, but is defined as a void *
3693 in order for this function to be usable with catch_errors. */
3694
3695 static int
3696 watchpoint_check (void *p)
3697 {
3698 bpstat bs = (bpstat) p;
3699 struct breakpoint *b;
3700 struct frame_info *fr;
3701 int within_current_scope;
3702
3703 /* BS is built from an existing struct breakpoint. */
3704 gdb_assert (bs->breakpoint_at != NULL);
3705 b = bs->breakpoint_at;
3706
3707 /* If this is a local watchpoint, we only want to check if the
3708 watchpoint frame is in scope if the current thread is the thread
3709 that was used to create the watchpoint. */
3710 if (!watchpoint_in_thread_scope (b))
3711 return WP_IGNORE;
3712
3713 if (b->exp_valid_block == NULL)
3714 within_current_scope = 1;
3715 else
3716 {
3717 struct frame_info *frame = get_current_frame ();
3718 struct gdbarch *frame_arch = get_frame_arch (frame);
3719 CORE_ADDR frame_pc = get_frame_pc (frame);
3720
3721 /* in_function_epilogue_p() returns a non-zero value if we're
3722 still in the function but the stack frame has already been
3723 invalidated. Since we can't rely on the values of local
3724 variables after the stack has been destroyed, we are treating
3725 the watchpoint in that state as `not changed' without further
3726 checking. Don't mark watchpoints as changed if the current
3727 frame is in an epilogue - even if they are in some other
3728 frame, our view of the stack is likely to be wrong and
3729 frame_find_by_id could error out. */
3730 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
3731 return WP_IGNORE;
3732
3733 fr = frame_find_by_id (b->watchpoint_frame);
3734 within_current_scope = (fr != NULL);
3735
3736 /* If we've gotten confused in the unwinder, we might have
3737 returned a frame that can't describe this variable. */
3738 if (within_current_scope)
3739 {
3740 struct symbol *function;
3741
3742 function = get_frame_function (fr);
3743 if (function == NULL
3744 || !contained_in (b->exp_valid_block,
3745 SYMBOL_BLOCK_VALUE (function)))
3746 within_current_scope = 0;
3747 }
3748
3749 if (within_current_scope)
3750 /* If we end up stopping, the current frame will get selected
3751 in normal_stop. So this call to select_frame won't affect
3752 the user. */
3753 select_frame (fr);
3754 }
3755
3756 if (within_current_scope)
3757 {
3758 /* We use value_{,free_to_}mark because it could be a *long*
3759 time before we return to the command level and call
3760 free_all_values. We can't call free_all_values because we
3761 might be in the middle of evaluating a function call. */
3762
3763 int pc = 0;
3764 struct value *mark = value_mark ();
3765 struct value *new_val;
3766
3767 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
3768
3769 /* We use value_equal_contents instead of value_equal because
3770 the latter coerces an array to a pointer, thus comparing just
3771 the address of the array instead of its contents. This is
3772 not what we want. */
3773 if ((b->val != NULL) != (new_val != NULL)
3774 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
3775 {
3776 if (new_val != NULL)
3777 {
3778 release_value (new_val);
3779 value_free_to_mark (mark);
3780 }
3781 bs->old_val = b->val;
3782 b->val = new_val;
3783 b->val_valid = 1;
3784 return WP_VALUE_CHANGED;
3785 }
3786 else
3787 {
3788 /* Nothing changed. */
3789 value_free_to_mark (mark);
3790 return WP_VALUE_NOT_CHANGED;
3791 }
3792 }
3793 else
3794 {
3795 /* This seems like the only logical thing to do because
3796 if we temporarily ignored the watchpoint, then when
3797 we reenter the block in which it is valid it contains
3798 garbage (in the case of a function, it may have two
3799 garbage values, one before and one after the prologue).
3800 So we can't even detect the first assignment to it and
3801 watch after that (since the garbage may or may not equal
3802 the first value assigned). */
3803 /* We print all the stop information in print_it_typical(), but
3804 in this case, by the time we call print_it_typical() this bp
3805 will be deleted already. So we have no choice but print the
3806 information here. */
3807 if (ui_out_is_mi_like_p (uiout))
3808 ui_out_field_string
3809 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
3810 ui_out_text (uiout, "\nWatchpoint ");
3811 ui_out_field_int (uiout, "wpnum", b->number);
3812 ui_out_text (uiout,
3813 " deleted because the program has left the block in\n\
3814 which its expression is valid.\n");
3815
3816 if (b->related_breakpoint)
3817 {
3818 b->related_breakpoint->disposition = disp_del_at_next_stop;
3819 b->related_breakpoint->related_breakpoint = NULL;
3820 b->related_breakpoint = NULL;
3821 }
3822 b->disposition = disp_del_at_next_stop;
3823
3824 return WP_DELETED;
3825 }
3826 }
3827
3828 /* Return true if it looks like target has stopped due to hitting
3829 breakpoint location BL. This function does not check if we
3830 should stop, only if BL explains the stop. */
3831 static int
3832 bpstat_check_location (const struct bp_location *bl,
3833 struct address_space *aspace, CORE_ADDR bp_addr)
3834 {
3835 struct breakpoint *b = bl->owner;
3836
3837 /* BL is from existing struct breakpoint. */
3838 gdb_assert (b != NULL);
3839
3840 /* By definition, the inferior does not report stops at
3841 tracepoints. */
3842 if (is_tracepoint (b))
3843 return 0;
3844
3845 if (!is_watchpoint (b)
3846 && b->type != bp_hardware_breakpoint
3847 && b->type != bp_catchpoint) /* a non-watchpoint bp */
3848 {
3849 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3850 aspace, bp_addr))
3851 return 0;
3852 if (overlay_debugging /* unmapped overlay section */
3853 && section_is_overlay (bl->section)
3854 && !section_is_mapped (bl->section))
3855 return 0;
3856 }
3857
3858 /* Continuable hardware watchpoints are treated as non-existent if the
3859 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3860 some data address). Otherwise gdb won't stop on a break instruction
3861 in the code (not from a breakpoint) when a hardware watchpoint has
3862 been defined. Also skip watchpoints which we know did not trigger
3863 (did not match the data address). */
3864
3865 if (is_hardware_watchpoint (b)
3866 && b->watchpoint_triggered == watch_triggered_no)
3867 return 0;
3868
3869 if (b->type == bp_hardware_breakpoint)
3870 {
3871 if (bl->address != bp_addr)
3872 return 0;
3873 if (overlay_debugging /* unmapped overlay section */
3874 && section_is_overlay (bl->section)
3875 && !section_is_mapped (bl->section))
3876 return 0;
3877 }
3878
3879 if (b->type == bp_catchpoint)
3880 {
3881 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
3882 if (!b->ops->breakpoint_hit (b))
3883 return 0;
3884 }
3885
3886 return 1;
3887 }
3888
3889 /* If BS refers to a watchpoint, determine if the watched values
3890 has actually changed, and we should stop. If not, set BS->stop
3891 to 0. */
3892 static void
3893 bpstat_check_watchpoint (bpstat bs)
3894 {
3895 const struct bp_location *bl;
3896 struct breakpoint *b;
3897
3898 /* BS is built for existing struct breakpoint. */
3899 bl = bs->bp_location_at;
3900 gdb_assert (bl != NULL);
3901 b = bs->breakpoint_at;
3902 gdb_assert (b != NULL);
3903
3904 if (is_watchpoint (b))
3905 {
3906 int must_check_value = 0;
3907
3908 if (b->type == bp_watchpoint)
3909 /* For a software watchpoint, we must always check the
3910 watched value. */
3911 must_check_value = 1;
3912 else if (b->watchpoint_triggered == watch_triggered_yes)
3913 /* We have a hardware watchpoint (read, write, or access)
3914 and the target earlier reported an address watched by
3915 this watchpoint. */
3916 must_check_value = 1;
3917 else if (b->watchpoint_triggered == watch_triggered_unknown
3918 && b->type == bp_hardware_watchpoint)
3919 /* We were stopped by a hardware watchpoint, but the target could
3920 not report the data address. We must check the watchpoint's
3921 value. Access and read watchpoints are out of luck; without
3922 a data address, we can't figure it out. */
3923 must_check_value = 1;
3924
3925 if (must_check_value)
3926 {
3927 char *message
3928 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3929 b->number);
3930 struct cleanup *cleanups = make_cleanup (xfree, message);
3931 int e = catch_errors (watchpoint_check, bs, message,
3932 RETURN_MASK_ALL);
3933 do_cleanups (cleanups);
3934 switch (e)
3935 {
3936 case WP_DELETED:
3937 /* We've already printed what needs to be printed. */
3938 bs->print_it = print_it_done;
3939 /* Stop. */
3940 break;
3941 case WP_IGNORE:
3942 bs->print_it = print_it_noop;
3943 bs->stop = 0;
3944 break;
3945 case WP_VALUE_CHANGED:
3946 if (b->type == bp_read_watchpoint)
3947 {
3948 /* There are two cases to consider here:
3949
3950 1. We're watching the triggered memory for reads.
3951 In that case, trust the target, and always report
3952 the watchpoint hit to the user. Even though
3953 reads don't cause value changes, the value may
3954 have changed since the last time it was read, and
3955 since we're not trapping writes, we will not see
3956 those, and as such we should ignore our notion of
3957 old value.
3958
3959 2. We're watching the triggered memory for both
3960 reads and writes. There are two ways this may
3961 happen:
3962
3963 2.1. This is a target that can't break on data
3964 reads only, but can break on accesses (reads or
3965 writes), such as e.g., x86. We detect this case
3966 at the time we try to insert read watchpoints.
3967
3968 2.2. Otherwise, the target supports read
3969 watchpoints, but, the user set an access or write
3970 watchpoint watching the same memory as this read
3971 watchpoint.
3972
3973 If we're watching memory writes as well as reads,
3974 ignore watchpoint hits when we find that the
3975 value hasn't changed, as reads don't cause
3976 changes. This still gives false positives when
3977 the program writes the same value to memory as
3978 what there was already in memory (we will confuse
3979 it for a read), but it's much better than
3980 nothing. */
3981
3982 int other_write_watchpoint = 0;
3983
3984 if (bl->watchpoint_type == hw_read)
3985 {
3986 struct breakpoint *other_b;
3987
3988 ALL_BREAKPOINTS (other_b)
3989 if ((other_b->type == bp_hardware_watchpoint
3990 || other_b->type == bp_access_watchpoint)
3991 && (other_b->watchpoint_triggered
3992 == watch_triggered_yes))
3993 {
3994 other_write_watchpoint = 1;
3995 break;
3996 }
3997 }
3998
3999 if (other_write_watchpoint
4000 || bl->watchpoint_type == hw_access)
4001 {
4002 /* We're watching the same memory for writes,
4003 and the value changed since the last time we
4004 updated it, so this trap must be for a write.
4005 Ignore it. */
4006 bs->print_it = print_it_noop;
4007 bs->stop = 0;
4008 }
4009 }
4010 break;
4011 case WP_VALUE_NOT_CHANGED:
4012 if (b->type == bp_hardware_watchpoint
4013 || b->type == bp_watchpoint)
4014 {
4015 /* Don't stop: write watchpoints shouldn't fire if
4016 the value hasn't changed. */
4017 bs->print_it = print_it_noop;
4018 bs->stop = 0;
4019 }
4020 /* Stop. */
4021 break;
4022 default:
4023 /* Can't happen. */
4024 case 0:
4025 /* Error from catch_errors. */
4026 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
4027 if (b->related_breakpoint)
4028 b->related_breakpoint->disposition = disp_del_at_next_stop;
4029 b->disposition = disp_del_at_next_stop;
4030 /* We've already printed what needs to be printed. */
4031 bs->print_it = print_it_done;
4032 break;
4033 }
4034 }
4035 else /* must_check_value == 0 */
4036 {
4037 /* This is a case where some watchpoint(s) triggered, but
4038 not at the address of this watchpoint, or else no
4039 watchpoint triggered after all. So don't print
4040 anything for this watchpoint. */
4041 bs->print_it = print_it_noop;
4042 bs->stop = 0;
4043 }
4044 }
4045 }
4046
4047
4048 /* Check conditions (condition proper, frame, thread and ignore count)
4049 of breakpoint referred to by BS. If we should not stop for this
4050 breakpoint, set BS->stop to 0. */
4051
4052 static void
4053 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4054 {
4055 int thread_id = pid_to_thread_id (ptid);
4056 const struct bp_location *bl;
4057 struct breakpoint *b;
4058
4059 /* BS is built for existing struct breakpoint. */
4060 bl = bs->bp_location_at;
4061 gdb_assert (bl != NULL);
4062 b = bs->breakpoint_at;
4063 gdb_assert (b != NULL);
4064
4065 if (frame_id_p (b->frame_id)
4066 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
4067 bs->stop = 0;
4068 else if (bs->stop)
4069 {
4070 int value_is_zero = 0;
4071 struct expression *cond;
4072
4073 if (is_watchpoint (b))
4074 cond = b->cond_exp;
4075 else
4076 cond = bl->cond;
4077
4078 if (cond && b->disposition != disp_del_at_next_stop)
4079 {
4080 int within_current_scope = 1;
4081
4082 /* We use value_mark and value_free_to_mark because it could
4083 be a long time before we return to the command level and
4084 call free_all_values. We can't call free_all_values
4085 because we might be in the middle of evaluating a
4086 function call. */
4087 struct value *mark = value_mark ();
4088
4089 /* Need to select the frame, with all that implies so that
4090 the conditions will have the right context. Because we
4091 use the frame, we will not see an inlined function's
4092 variables when we arrive at a breakpoint at the start
4093 of the inlined function; the current frame will be the
4094 call site. */
4095 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4096 select_frame (get_current_frame ());
4097 else
4098 {
4099 struct frame_info *frame;
4100
4101 /* For local watchpoint expressions, which particular
4102 instance of a local is being watched matters, so we
4103 keep track of the frame to evaluate the expression
4104 in. To evaluate the condition however, it doesn't
4105 really matter which instantiation of the function
4106 where the condition makes sense triggers the
4107 watchpoint. This allows an expression like "watch
4108 global if q > 10" set in `func', catch writes to
4109 global on all threads that call `func', or catch
4110 writes on all recursive calls of `func' by a single
4111 thread. We simply always evaluate the condition in
4112 the innermost frame that's executing where it makes
4113 sense to evaluate the condition. It seems
4114 intuitive. */
4115 frame = block_innermost_frame (b->cond_exp_valid_block);
4116 if (frame != NULL)
4117 select_frame (frame);
4118 else
4119 within_current_scope = 0;
4120 }
4121 if (within_current_scope)
4122 value_is_zero
4123 = catch_errors (breakpoint_cond_eval, cond,
4124 "Error in testing breakpoint condition:\n",
4125 RETURN_MASK_ALL);
4126 else
4127 {
4128 warning (_("Watchpoint condition cannot be tested "
4129 "in the current scope"));
4130 /* If we failed to set the right context for this
4131 watchpoint, unconditionally report it. */
4132 value_is_zero = 0;
4133 }
4134 /* FIXME-someday, should give breakpoint #. */
4135 value_free_to_mark (mark);
4136 }
4137
4138 if (cond && value_is_zero)
4139 {
4140 bs->stop = 0;
4141 }
4142 else if (b->thread != -1 && b->thread != thread_id)
4143 {
4144 bs->stop = 0;
4145 }
4146 else if (b->ignore_count > 0)
4147 {
4148 b->ignore_count--;
4149 annotate_ignore_count_change ();
4150 bs->stop = 0;
4151 /* Increase the hit count even though we don't stop. */
4152 ++(b->hit_count);
4153 }
4154 }
4155 }
4156
4157
4158 /* Get a bpstat associated with having just stopped at address
4159 BP_ADDR in thread PTID.
4160
4161 Determine whether we stopped at a breakpoint, etc, or whether we
4162 don't understand this stop. Result is a chain of bpstat's such
4163 that:
4164
4165 if we don't understand the stop, the result is a null pointer.
4166
4167 if we understand why we stopped, the result is not null.
4168
4169 Each element of the chain refers to a particular breakpoint or
4170 watchpoint at which we have stopped. (We may have stopped for
4171 several reasons concurrently.)
4172
4173 Each element of the chain has valid next, breakpoint_at,
4174 commands, FIXME??? fields. */
4175
4176 bpstat
4177 bpstat_stop_status (struct address_space *aspace,
4178 CORE_ADDR bp_addr, ptid_t ptid)
4179 {
4180 struct breakpoint *b = NULL;
4181 struct bp_location *bl;
4182 struct bp_location *loc;
4183 /* First item of allocated bpstat's. */
4184 bpstat bs_head = NULL, *bs_link = &bs_head;
4185 /* Pointer to the last thing in the chain currently. */
4186 bpstat bs;
4187 int ix;
4188 int need_remove_insert;
4189 int removed_any;
4190
4191 /* First, build the bpstat chain with locations that explain a
4192 target stop, while being careful to not set the target running,
4193 as that may invalidate locations (in particular watchpoint
4194 locations are recreated). Resuming will happen here with
4195 breakpoint conditions or watchpoint expressions that include
4196 inferior function calls. */
4197
4198 ALL_BREAKPOINTS (b)
4199 {
4200 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4201 continue;
4202
4203 for (bl = b->loc; bl != NULL; bl = bl->next)
4204 {
4205 /* For hardware watchpoints, we look only at the first
4206 location. The watchpoint_check function will work on the
4207 entire expression, not the individual locations. For
4208 read watchpoints, the watchpoints_triggered function has
4209 checked all locations already. */
4210 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4211 break;
4212
4213 if (bl->shlib_disabled)
4214 continue;
4215
4216 if (!bpstat_check_location (bl, aspace, bp_addr))
4217 continue;
4218
4219 /* Come here if it's a watchpoint, or if the break address
4220 matches. */
4221
4222 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4223 explain stop. */
4224
4225 /* Assume we stop. Should we find a watchpoint that is not
4226 actually triggered, or if the condition of the breakpoint
4227 evaluates as false, we'll reset 'stop' to 0. */
4228 bs->stop = 1;
4229 bs->print = 1;
4230
4231 /* If this is a scope breakpoint, mark the associated
4232 watchpoint as triggered so that we will handle the
4233 out-of-scope event. We'll get to the watchpoint next
4234 iteration. */
4235 if (b->type == bp_watchpoint_scope)
4236 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4237 }
4238 }
4239
4240 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4241 {
4242 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
4243 aspace, bp_addr))
4244 {
4245 bs = bpstat_alloc (loc, &bs_link);
4246 /* For hits of moribund locations, we should just proceed. */
4247 bs->stop = 0;
4248 bs->print = 0;
4249 bs->print_it = print_it_noop;
4250 }
4251 }
4252
4253 /* Now go through the locations that caused the target to stop, and
4254 check whether we're interested in reporting this stop to higher
4255 layers, or whether we should resume the target transparently. */
4256
4257 removed_any = 0;
4258
4259 for (bs = bs_head; bs != NULL; bs = bs->next)
4260 {
4261 if (!bs->stop)
4262 continue;
4263
4264 bpstat_check_watchpoint (bs);
4265 if (!bs->stop)
4266 continue;
4267
4268 b = bs->breakpoint_at;
4269
4270 if (b->type == bp_thread_event || b->type == bp_overlay_event
4271 || b->type == bp_longjmp_master
4272 || b->type == bp_std_terminate_master
4273 || b->type == bp_exception_master)
4274 /* We do not stop for these. */
4275 bs->stop = 0;
4276 else
4277 bpstat_check_breakpoint_conditions (bs, ptid);
4278
4279 if (bs->stop)
4280 {
4281 ++(b->hit_count);
4282
4283 /* We will stop here. */
4284 if (b->disposition == disp_disable)
4285 {
4286 if (b->enable_state != bp_permanent)
4287 b->enable_state = bp_disabled;
4288 removed_any = 1;
4289 }
4290 if (b->silent)
4291 bs->print = 0;
4292 bs->commands = b->commands;
4293 incref_counted_command_line (bs->commands);
4294 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4295 if (bs->commands_left
4296 && (strcmp ("silent", bs->commands_left->line) == 0
4297 || (xdb_commands
4298 && strcmp ("Q",
4299 bs->commands_left->line) == 0)))
4300 {
4301 bs->commands_left = bs->commands_left->next;
4302 bs->print = 0;
4303 }
4304 }
4305
4306 /* Print nothing for this entry if we dont stop or dont print. */
4307 if (bs->stop == 0 || bs->print == 0)
4308 bs->print_it = print_it_noop;
4309 }
4310
4311 /* If we aren't stopping, the value of some hardware watchpoint may
4312 not have changed, but the intermediate memory locations we are
4313 watching may have. Don't bother if we're stopping; this will get
4314 done later. */
4315 need_remove_insert = 0;
4316 if (! bpstat_causes_stop (bs_head))
4317 for (bs = bs_head; bs != NULL; bs = bs->next)
4318 if (!bs->stop
4319 && bs->breakpoint_at
4320 && is_hardware_watchpoint (bs->breakpoint_at))
4321 {
4322 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
4323 need_remove_insert = 1;
4324 }
4325
4326 if (need_remove_insert)
4327 update_global_location_list (1);
4328 else if (removed_any)
4329 update_global_location_list (0);
4330
4331 return bs_head;
4332 }
4333
4334 static void
4335 handle_jit_event (void)
4336 {
4337 struct frame_info *frame;
4338 struct gdbarch *gdbarch;
4339
4340 /* Switch terminal for any messages produced by
4341 breakpoint_re_set. */
4342 target_terminal_ours_for_output ();
4343
4344 frame = get_current_frame ();
4345 gdbarch = get_frame_arch (frame);
4346
4347 jit_event_handler (gdbarch);
4348
4349 target_terminal_inferior ();
4350 }
4351
4352 /* Prepare WHAT final decision for infrun. */
4353
4354 /* Decide what infrun needs to do with this bpstat. */
4355
4356 struct bpstat_what
4357 bpstat_what (bpstat bs)
4358 {
4359 struct bpstat_what retval;
4360 /* We need to defer calling `solib_add', as adding new symbols
4361 resets breakpoints, which in turn deletes breakpoint locations,
4362 and hence may clear unprocessed entries in the BS chain. */
4363 int shlib_event = 0;
4364 int jit_event = 0;
4365
4366 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
4367 retval.call_dummy = STOP_NONE;
4368 retval.is_longjmp = 0;
4369
4370 for (; bs != NULL; bs = bs->next)
4371 {
4372 /* Extract this BS's action. After processing each BS, we check
4373 if its action overrides all we've seem so far. */
4374 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4375 enum bptype bptype;
4376
4377 if (bs->breakpoint_at == NULL)
4378 {
4379 /* I suspect this can happen if it was a momentary
4380 breakpoint which has since been deleted. */
4381 bptype = bp_none;
4382 }
4383 else if (bs->breakpoint_at == NULL)
4384 bptype = bp_none;
4385 else
4386 bptype = bs->breakpoint_at->type;
4387
4388 switch (bptype)
4389 {
4390 case bp_none:
4391 break;
4392 case bp_breakpoint:
4393 case bp_hardware_breakpoint:
4394 case bp_until:
4395 case bp_finish:
4396 if (bs->stop)
4397 {
4398 if (bs->print)
4399 this_action = BPSTAT_WHAT_STOP_NOISY;
4400 else
4401 this_action = BPSTAT_WHAT_STOP_SILENT;
4402 }
4403 else
4404 this_action = BPSTAT_WHAT_SINGLE;
4405 break;
4406 case bp_watchpoint:
4407 case bp_hardware_watchpoint:
4408 case bp_read_watchpoint:
4409 case bp_access_watchpoint:
4410 if (bs->stop)
4411 {
4412 if (bs->print)
4413 this_action = BPSTAT_WHAT_STOP_NOISY;
4414 else
4415 this_action = BPSTAT_WHAT_STOP_SILENT;
4416 }
4417 else
4418 {
4419 /* There was a watchpoint, but we're not stopping.
4420 This requires no further action. */
4421 }
4422 break;
4423 case bp_longjmp:
4424 case bp_exception:
4425 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
4426 retval.is_longjmp = bptype == bp_longjmp;
4427 break;
4428 case bp_longjmp_resume:
4429 case bp_exception_resume:
4430 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
4431 retval.is_longjmp = bptype == bp_longjmp_resume;
4432 break;
4433 case bp_step_resume:
4434 if (bs->stop)
4435 this_action = BPSTAT_WHAT_STEP_RESUME;
4436 else
4437 {
4438 /* It is for the wrong frame. */
4439 this_action = BPSTAT_WHAT_SINGLE;
4440 }
4441 break;
4442 case bp_watchpoint_scope:
4443 case bp_thread_event:
4444 case bp_overlay_event:
4445 case bp_longjmp_master:
4446 case bp_std_terminate_master:
4447 case bp_exception_master:
4448 this_action = BPSTAT_WHAT_SINGLE;
4449 break;
4450 case bp_catchpoint:
4451 if (bs->stop)
4452 {
4453 if (bs->print)
4454 this_action = BPSTAT_WHAT_STOP_NOISY;
4455 else
4456 this_action = BPSTAT_WHAT_STOP_SILENT;
4457 }
4458 else
4459 {
4460 /* There was a catchpoint, but we're not stopping.
4461 This requires no further action. */
4462 }
4463 break;
4464 case bp_shlib_event:
4465 shlib_event = 1;
4466
4467 /* If requested, stop when the dynamic linker notifies GDB
4468 of events. This allows the user to get control and place
4469 breakpoints in initializer routines for dynamically
4470 loaded objects (among other things). */
4471 if (stop_on_solib_events)
4472 this_action = BPSTAT_WHAT_STOP_NOISY;
4473 else
4474 this_action = BPSTAT_WHAT_SINGLE;
4475 break;
4476 case bp_jit_event:
4477 jit_event = 1;
4478 this_action = BPSTAT_WHAT_SINGLE;
4479 break;
4480 case bp_call_dummy:
4481 /* Make sure the action is stop (silent or noisy),
4482 so infrun.c pops the dummy frame. */
4483 retval.call_dummy = STOP_STACK_DUMMY;
4484 this_action = BPSTAT_WHAT_STOP_SILENT;
4485 break;
4486 case bp_std_terminate:
4487 /* Make sure the action is stop (silent or noisy),
4488 so infrun.c pops the dummy frame. */
4489 retval.call_dummy = STOP_STD_TERMINATE;
4490 this_action = BPSTAT_WHAT_STOP_SILENT;
4491 break;
4492 case bp_tracepoint:
4493 case bp_fast_tracepoint:
4494 case bp_static_tracepoint:
4495 /* Tracepoint hits should not be reported back to GDB, and
4496 if one got through somehow, it should have been filtered
4497 out already. */
4498 internal_error (__FILE__, __LINE__,
4499 _("bpstat_what: tracepoint encountered"));
4500 default:
4501 internal_error (__FILE__, __LINE__,
4502 _("bpstat_what: unhandled bptype %d"), (int) bptype);
4503 }
4504
4505 retval.main_action = max (retval.main_action, this_action);
4506 }
4507
4508 if (shlib_event)
4509 {
4510 if (debug_infrun)
4511 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4512
4513 /* Check for any newly added shared libraries if we're supposed
4514 to be adding them automatically. */
4515
4516 /* Switch terminal for any messages produced by
4517 breakpoint_re_set. */
4518 target_terminal_ours_for_output ();
4519
4520 #ifdef SOLIB_ADD
4521 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4522 #else
4523 solib_add (NULL, 0, &current_target, auto_solib_add);
4524 #endif
4525
4526 target_terminal_inferior ();
4527 }
4528
4529 if (jit_event)
4530 {
4531 if (debug_infrun)
4532 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4533
4534 handle_jit_event ();
4535 }
4536
4537 return retval;
4538 }
4539
4540 /* Nonzero if we should step constantly (e.g. watchpoints on machines
4541 without hardware support). This isn't related to a specific bpstat,
4542 just to things like whether watchpoints are set. */
4543
4544 int
4545 bpstat_should_step (void)
4546 {
4547 struct breakpoint *b;
4548
4549 ALL_BREAKPOINTS (b)
4550 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4551 return 1;
4552 return 0;
4553 }
4554
4555 int
4556 bpstat_causes_stop (bpstat bs)
4557 {
4558 for (; bs != NULL; bs = bs->next)
4559 if (bs->stop)
4560 return 1;
4561
4562 return 0;
4563 }
4564
4565 \f
4566
4567 /* Print the LOC location out of the list of B->LOC locations. */
4568
4569 static void print_breakpoint_location (struct breakpoint *b,
4570 struct bp_location *loc,
4571 char *wrap_indent,
4572 struct ui_stream *stb)
4573 {
4574 struct cleanup *old_chain = save_current_program_space ();
4575
4576 if (loc != NULL && loc->shlib_disabled)
4577 loc = NULL;
4578
4579 if (loc != NULL)
4580 set_current_program_space (loc->pspace);
4581
4582 if (b->source_file && loc)
4583 {
4584 struct symbol *sym
4585 = find_pc_sect_function (loc->address, loc->section);
4586 if (sym)
4587 {
4588 ui_out_text (uiout, "in ");
4589 ui_out_field_string (uiout, "func",
4590 SYMBOL_PRINT_NAME (sym));
4591 ui_out_wrap_hint (uiout, wrap_indent);
4592 ui_out_text (uiout, " at ");
4593 }
4594 ui_out_field_string (uiout, "file", b->source_file);
4595 ui_out_text (uiout, ":");
4596
4597 if (ui_out_is_mi_like_p (uiout))
4598 {
4599 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4600 char *fullname = symtab_to_fullname (sal.symtab);
4601
4602 if (fullname)
4603 ui_out_field_string (uiout, "fullname", fullname);
4604 }
4605
4606 ui_out_field_int (uiout, "line", b->line_number);
4607 }
4608 else if (loc)
4609 {
4610 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4611 demangle, "");
4612 ui_out_field_stream (uiout, "at", stb);
4613 }
4614 else
4615 ui_out_field_string (uiout, "pending", b->addr_string);
4616
4617 do_cleanups (old_chain);
4618 }
4619
4620 static const char *
4621 bptype_string (enum bptype type)
4622 {
4623 struct ep_type_description
4624 {
4625 enum bptype type;
4626 char *description;
4627 };
4628 static struct ep_type_description bptypes[] =
4629 {
4630 {bp_none, "?deleted?"},
4631 {bp_breakpoint, "breakpoint"},
4632 {bp_hardware_breakpoint, "hw breakpoint"},
4633 {bp_until, "until"},
4634 {bp_finish, "finish"},
4635 {bp_watchpoint, "watchpoint"},
4636 {bp_hardware_watchpoint, "hw watchpoint"},
4637 {bp_read_watchpoint, "read watchpoint"},
4638 {bp_access_watchpoint, "acc watchpoint"},
4639 {bp_longjmp, "longjmp"},
4640 {bp_longjmp_resume, "longjmp resume"},
4641 {bp_exception, "exception"},
4642 {bp_exception_resume, "exception resume"},
4643 {bp_step_resume, "step resume"},
4644 {bp_watchpoint_scope, "watchpoint scope"},
4645 {bp_call_dummy, "call dummy"},
4646 {bp_std_terminate, "std::terminate"},
4647 {bp_shlib_event, "shlib events"},
4648 {bp_thread_event, "thread events"},
4649 {bp_overlay_event, "overlay events"},
4650 {bp_longjmp_master, "longjmp master"},
4651 {bp_std_terminate_master, "std::terminate master"},
4652 {bp_exception_master, "exception master"},
4653 {bp_catchpoint, "catchpoint"},
4654 {bp_tracepoint, "tracepoint"},
4655 {bp_fast_tracepoint, "fast tracepoint"},
4656 {bp_static_tracepoint, "static tracepoint"},
4657 {bp_jit_event, "jit events"},
4658 };
4659
4660 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4661 || ((int) type != bptypes[(int) type].type))
4662 internal_error (__FILE__, __LINE__,
4663 _("bptypes table does not describe type #%d."),
4664 (int) type);
4665
4666 return bptypes[(int) type].description;
4667 }
4668
4669 /* Print B to gdb_stdout. */
4670
4671 static void
4672 print_one_breakpoint_location (struct breakpoint *b,
4673 struct bp_location *loc,
4674 int loc_number,
4675 struct bp_location **last_loc,
4676 int print_address_bits,
4677 int allflag)
4678 {
4679 struct command_line *l;
4680 static char bpenables[] = "nynny";
4681 char wrap_indent[80];
4682 struct ui_stream *stb = ui_out_stream_new (uiout);
4683 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
4684 struct cleanup *bkpt_chain;
4685
4686 int header_of_multiple = 0;
4687 int part_of_multiple = (loc != NULL);
4688 struct value_print_options opts;
4689
4690 get_user_print_options (&opts);
4691
4692 gdb_assert (!loc || loc_number != 0);
4693 /* See comment in print_one_breakpoint concerning treatment of
4694 breakpoints with single disabled location. */
4695 if (loc == NULL
4696 && (b->loc != NULL
4697 && (b->loc->next != NULL || !b->loc->enabled)))
4698 header_of_multiple = 1;
4699 if (loc == NULL)
4700 loc = b->loc;
4701
4702 annotate_record ();
4703 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4704
4705 /* 1 */
4706 annotate_field (0);
4707 if (part_of_multiple)
4708 {
4709 char *formatted;
4710 formatted = xstrprintf ("%d.%d", b->number, loc_number);
4711 ui_out_field_string (uiout, "number", formatted);
4712 xfree (formatted);
4713 }
4714 else
4715 {
4716 ui_out_field_int (uiout, "number", b->number);
4717 }
4718
4719 /* 2 */
4720 annotate_field (1);
4721 if (part_of_multiple)
4722 ui_out_field_skip (uiout, "type");
4723 else
4724 ui_out_field_string (uiout, "type", bptype_string (b->type));
4725
4726 /* 3 */
4727 annotate_field (2);
4728 if (part_of_multiple)
4729 ui_out_field_skip (uiout, "disp");
4730 else
4731 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
4732
4733
4734 /* 4 */
4735 annotate_field (3);
4736 if (part_of_multiple)
4737 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
4738 else
4739 ui_out_field_fmt (uiout, "enabled", "%c",
4740 bpenables[(int) b->enable_state]);
4741 ui_out_spaces (uiout, 2);
4742
4743
4744 /* 5 and 6 */
4745 strcpy (wrap_indent, " ");
4746 if (opts.addressprint)
4747 {
4748 if (print_address_bits <= 32)
4749 strcat (wrap_indent, " ");
4750 else
4751 strcat (wrap_indent, " ");
4752 }
4753
4754 if (b->ops != NULL && b->ops->print_one != NULL)
4755 {
4756 /* Although the print_one can possibly print all locations,
4757 calling it here is not likely to get any nice result. So,
4758 make sure there's just one location. */
4759 gdb_assert (b->loc == NULL || b->loc->next == NULL);
4760 b->ops->print_one (b, last_loc);
4761 }
4762 else
4763 switch (b->type)
4764 {
4765 case bp_none:
4766 internal_error (__FILE__, __LINE__,
4767 _("print_one_breakpoint: bp_none encountered\n"));
4768 break;
4769
4770 case bp_watchpoint:
4771 case bp_hardware_watchpoint:
4772 case bp_read_watchpoint:
4773 case bp_access_watchpoint:
4774 /* Field 4, the address, is omitted (which makes the columns
4775 not line up too nicely with the headers, but the effect
4776 is relatively readable). */
4777 if (opts.addressprint)
4778 ui_out_field_skip (uiout, "addr");
4779 annotate_field (5);
4780 ui_out_field_string (uiout, "what", b->exp_string);
4781 break;
4782
4783 case bp_breakpoint:
4784 case bp_hardware_breakpoint:
4785 case bp_until:
4786 case bp_finish:
4787 case bp_longjmp:
4788 case bp_longjmp_resume:
4789 case bp_exception:
4790 case bp_exception_resume:
4791 case bp_step_resume:
4792 case bp_watchpoint_scope:
4793 case bp_call_dummy:
4794 case bp_std_terminate:
4795 case bp_shlib_event:
4796 case bp_thread_event:
4797 case bp_overlay_event:
4798 case bp_longjmp_master:
4799 case bp_std_terminate_master:
4800 case bp_exception_master:
4801 case bp_tracepoint:
4802 case bp_fast_tracepoint:
4803 case bp_static_tracepoint:
4804 case bp_jit_event:
4805 if (opts.addressprint)
4806 {
4807 annotate_field (4);
4808 if (header_of_multiple)
4809 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
4810 else if (b->loc == NULL || loc->shlib_disabled)
4811 ui_out_field_string (uiout, "addr", "<PENDING>");
4812 else
4813 ui_out_field_core_addr (uiout, "addr",
4814 loc->gdbarch, loc->address);
4815 }
4816 annotate_field (5);
4817 if (!header_of_multiple)
4818 print_breakpoint_location (b, loc, wrap_indent, stb);
4819 if (b->loc)
4820 *last_loc = b->loc;
4821 break;
4822 }
4823
4824
4825 /* For backward compatibility, don't display inferiors unless there
4826 are several. */
4827 if (loc != NULL
4828 && !header_of_multiple
4829 && (allflag
4830 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4831 && (number_of_program_spaces () > 1
4832 || number_of_inferiors () > 1)
4833 /* LOC is for existing B, it cannot be in
4834 moribund_locations and thus having NULL OWNER. */
4835 && loc->owner->type != bp_catchpoint)))
4836 {
4837 struct inferior *inf;
4838 int first = 1;
4839
4840 for (inf = inferior_list; inf != NULL; inf = inf->next)
4841 {
4842 if (inf->pspace == loc->pspace)
4843 {
4844 if (first)
4845 {
4846 first = 0;
4847 ui_out_text (uiout, " inf ");
4848 }
4849 else
4850 ui_out_text (uiout, ", ");
4851 ui_out_text (uiout, plongest (inf->num));
4852 }
4853 }
4854 }
4855
4856 if (!part_of_multiple)
4857 {
4858 if (b->thread != -1)
4859 {
4860 /* FIXME: This seems to be redundant and lost here; see the
4861 "stop only in" line a little further down. */
4862 ui_out_text (uiout, " thread ");
4863 ui_out_field_int (uiout, "thread", b->thread);
4864 }
4865 else if (b->task != 0)
4866 {
4867 ui_out_text (uiout, " task ");
4868 ui_out_field_int (uiout, "task", b->task);
4869 }
4870 }
4871
4872 ui_out_text (uiout, "\n");
4873
4874 if (!part_of_multiple && b->static_trace_marker_id)
4875 {
4876 gdb_assert (b->type == bp_static_tracepoint);
4877
4878 ui_out_text (uiout, "\tmarker id is ");
4879 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4880 b->static_trace_marker_id);
4881 ui_out_text (uiout, "\n");
4882 }
4883
4884 if (part_of_multiple && frame_id_p (b->frame_id))
4885 {
4886 annotate_field (6);
4887 ui_out_text (uiout, "\tstop only in stack frame at ");
4888 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4889 the frame ID. */
4890 ui_out_field_core_addr (uiout, "frame",
4891 b->gdbarch, b->frame_id.stack_addr);
4892 ui_out_text (uiout, "\n");
4893 }
4894
4895 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
4896 {
4897 /* We do not print the condition for Ada exception catchpoints
4898 because the condition is an internal implementation detail
4899 that we do not want to expose to the user. */
4900 annotate_field (7);
4901 if (is_tracepoint (b))
4902 ui_out_text (uiout, "\ttrace only if ");
4903 else
4904 ui_out_text (uiout, "\tstop only if ");
4905 ui_out_field_string (uiout, "cond", b->cond_string);
4906 ui_out_text (uiout, "\n");
4907 }
4908
4909 if (!part_of_multiple && b->thread != -1)
4910 {
4911 /* FIXME should make an annotation for this. */
4912 ui_out_text (uiout, "\tstop only in thread ");
4913 ui_out_field_int (uiout, "thread", b->thread);
4914 ui_out_text (uiout, "\n");
4915 }
4916
4917 if (!part_of_multiple && b->hit_count)
4918 {
4919 /* FIXME should make an annotation for this. */
4920 if (ep_is_catchpoint (b))
4921 ui_out_text (uiout, "\tcatchpoint");
4922 else
4923 ui_out_text (uiout, "\tbreakpoint");
4924 ui_out_text (uiout, " already hit ");
4925 ui_out_field_int (uiout, "times", b->hit_count);
4926 if (b->hit_count == 1)
4927 ui_out_text (uiout, " time\n");
4928 else
4929 ui_out_text (uiout, " times\n");
4930 }
4931
4932 /* Output the count also if it is zero, but only if this is mi.
4933 FIXME: Should have a better test for this. */
4934 if (ui_out_is_mi_like_p (uiout))
4935 if (!part_of_multiple && b->hit_count == 0)
4936 ui_out_field_int (uiout, "times", b->hit_count);
4937
4938 if (!part_of_multiple && b->ignore_count)
4939 {
4940 annotate_field (8);
4941 ui_out_text (uiout, "\tignore next ");
4942 ui_out_field_int (uiout, "ignore", b->ignore_count);
4943 ui_out_text (uiout, " hits\n");
4944 }
4945
4946 l = b->commands ? b->commands->commands : NULL;
4947 if (!part_of_multiple && l)
4948 {
4949 struct cleanup *script_chain;
4950
4951 annotate_field (9);
4952 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
4953 print_command_lines (uiout, l, 4);
4954 do_cleanups (script_chain);
4955 }
4956
4957 if (!part_of_multiple && b->pass_count)
4958 {
4959 annotate_field (10);
4960 ui_out_text (uiout, "\tpass count ");
4961 ui_out_field_int (uiout, "pass", b->pass_count);
4962 ui_out_text (uiout, " \n");
4963 }
4964
4965 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4966 {
4967 if (b->addr_string)
4968 ui_out_field_string (uiout, "original-location", b->addr_string);
4969 else if (b->exp_string)
4970 ui_out_field_string (uiout, "original-location", b->exp_string);
4971 }
4972
4973 do_cleanups (bkpt_chain);
4974 do_cleanups (old_chain);
4975 }
4976
4977 static void
4978 print_one_breakpoint (struct breakpoint *b,
4979 struct bp_location **last_loc,
4980 int print_address_bits,
4981 int allflag)
4982 {
4983 print_one_breakpoint_location (b, NULL, 0, last_loc,
4984 print_address_bits, allflag);
4985
4986 /* If this breakpoint has custom print function,
4987 it's already printed. Otherwise, print individual
4988 locations, if any. */
4989 if (b->ops == NULL || b->ops->print_one == NULL)
4990 {
4991 /* If breakpoint has a single location that is disabled, we
4992 print it as if it had several locations, since otherwise it's
4993 hard to represent "breakpoint enabled, location disabled"
4994 situation.
4995
4996 Note that while hardware watchpoints have several locations
4997 internally, that's not a property exposed to user. */
4998 if (b->loc
4999 && !is_hardware_watchpoint (b)
5000 && (b->loc->next || !b->loc->enabled)
5001 && !ui_out_is_mi_like_p (uiout))
5002 {
5003 struct bp_location *loc;
5004 int n = 1;
5005 for (loc = b->loc; loc; loc = loc->next, ++n)
5006 print_one_breakpoint_location (b, loc, n, last_loc,
5007 print_address_bits, allflag);
5008 }
5009 }
5010 }
5011
5012 static int
5013 breakpoint_address_bits (struct breakpoint *b)
5014 {
5015 int print_address_bits = 0;
5016 struct bp_location *loc;
5017
5018 for (loc = b->loc; loc; loc = loc->next)
5019 {
5020 int addr_bit;
5021
5022 /* Software watchpoints that aren't watching memory don't have
5023 an address to print. */
5024 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5025 continue;
5026
5027 addr_bit = gdbarch_addr_bit (loc->gdbarch);
5028 if (addr_bit > print_address_bits)
5029 print_address_bits = addr_bit;
5030 }
5031
5032 return print_address_bits;
5033 }
5034
5035 struct captured_breakpoint_query_args
5036 {
5037 int bnum;
5038 };
5039
5040 static int
5041 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5042 {
5043 struct captured_breakpoint_query_args *args = data;
5044 struct breakpoint *b;
5045 struct bp_location *dummy_loc = NULL;
5046
5047 ALL_BREAKPOINTS (b)
5048 {
5049 if (args->bnum == b->number)
5050 {
5051 int print_address_bits = breakpoint_address_bits (b);
5052
5053 print_one_breakpoint (b, &dummy_loc, print_address_bits, 0);
5054 return GDB_RC_OK;
5055 }
5056 }
5057 return GDB_RC_NONE;
5058 }
5059
5060 enum gdb_rc
5061 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5062 char **error_message)
5063 {
5064 struct captured_breakpoint_query_args args;
5065
5066 args.bnum = bnum;
5067 /* For the moment we don't trust print_one_breakpoint() to not throw
5068 an error. */
5069 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5070 error_message, RETURN_MASK_ALL) < 0)
5071 return GDB_RC_FAIL;
5072 else
5073 return GDB_RC_OK;
5074 }
5075
5076 /* Return non-zero if B is user settable (breakpoints, watchpoints,
5077 catchpoints, et.al.). */
5078
5079 static int
5080 user_settable_breakpoint (const struct breakpoint *b)
5081 {
5082 return (b->type == bp_breakpoint
5083 || b->type == bp_catchpoint
5084 || b->type == bp_hardware_breakpoint
5085 || is_tracepoint (b)
5086 || is_watchpoint (b));
5087 }
5088
5089 /* Return true if this breakpoint was set by the user, false if it is
5090 internal or momentary. */
5091
5092 int
5093 user_breakpoint_p (struct breakpoint *b)
5094 {
5095 return user_settable_breakpoint (b) && b->number > 0;
5096 }
5097
5098 /* Print information on user settable breakpoint (watchpoint, etc)
5099 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5100 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5101 FILTER is non-NULL, call it on each breakpoint and only include the
5102 ones for which it returns non-zero. Return the total number of
5103 breakpoints listed. */
5104
5105 static int
5106 breakpoint_1 (char *args, int allflag,
5107 int (*filter) (const struct breakpoint *))
5108 {
5109 struct breakpoint *b;
5110 struct bp_location *last_loc = NULL;
5111 int nr_printable_breakpoints;
5112 struct cleanup *bkpttbl_chain;
5113 struct value_print_options opts;
5114 int print_address_bits = 0;
5115 int print_type_col_width = 14;
5116
5117 get_user_print_options (&opts);
5118
5119 /* Compute the number of rows in the table, as well as the size
5120 required for address fields. */
5121 nr_printable_breakpoints = 0;
5122 ALL_BREAKPOINTS (b)
5123 {
5124 /* If we have a filter, only list the breakpoints it accepts. */
5125 if (filter && !filter (b))
5126 continue;
5127
5128 /* If we have an "args" string, it is a list of breakpoints to
5129 accept. Skip the others. */
5130 if (args != NULL && *args != '\0')
5131 {
5132 if (allflag && parse_and_eval_long (args) != b->number)
5133 continue;
5134 if (!allflag && !number_is_in_list (args, b->number))
5135 continue;
5136 }
5137
5138 if (allflag || user_breakpoint_p (b))
5139 {
5140 int addr_bit, type_len;
5141
5142 addr_bit = breakpoint_address_bits (b);
5143 if (addr_bit > print_address_bits)
5144 print_address_bits = addr_bit;
5145
5146 type_len = strlen (bptype_string (b->type));
5147 if (type_len > print_type_col_width)
5148 print_type_col_width = type_len;
5149
5150 nr_printable_breakpoints++;
5151 }
5152 }
5153
5154 if (opts.addressprint)
5155 bkpttbl_chain
5156 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5157 nr_printable_breakpoints,
5158 "BreakpointTable");
5159 else
5160 bkpttbl_chain
5161 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5162 nr_printable_breakpoints,
5163 "BreakpointTable");
5164
5165 if (nr_printable_breakpoints > 0)
5166 annotate_breakpoints_headers ();
5167 if (nr_printable_breakpoints > 0)
5168 annotate_field (0);
5169 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
5170 if (nr_printable_breakpoints > 0)
5171 annotate_field (1);
5172 ui_out_table_header (uiout, print_type_col_width, ui_left,
5173 "type", "Type"); /* 2 */
5174 if (nr_printable_breakpoints > 0)
5175 annotate_field (2);
5176 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
5177 if (nr_printable_breakpoints > 0)
5178 annotate_field (3);
5179 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
5180 if (opts.addressprint)
5181 {
5182 if (nr_printable_breakpoints > 0)
5183 annotate_field (4);
5184 if (print_address_bits <= 32)
5185 ui_out_table_header (uiout, 10, ui_left,
5186 "addr", "Address"); /* 5 */
5187 else
5188 ui_out_table_header (uiout, 18, ui_left,
5189 "addr", "Address"); /* 5 */
5190 }
5191 if (nr_printable_breakpoints > 0)
5192 annotate_field (5);
5193 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5194 ui_out_table_body (uiout);
5195 if (nr_printable_breakpoints > 0)
5196 annotate_breakpoints_table ();
5197
5198 ALL_BREAKPOINTS (b)
5199 {
5200 QUIT;
5201 /* If we have a filter, only list the breakpoints it accepts. */
5202 if (filter && !filter (b))
5203 continue;
5204
5205 /* If we have an "args" string, it is a list of breakpoints to
5206 accept. Skip the others. */
5207
5208 if (args != NULL && *args != '\0')
5209 {
5210 if (allflag) /* maintenance info breakpoint */
5211 {
5212 if (parse_and_eval_long (args) != b->number)
5213 continue;
5214 }
5215 else /* all others */
5216 {
5217 if (!number_is_in_list (args, b->number))
5218 continue;
5219 }
5220 }
5221 /* We only print out user settable breakpoints unless the
5222 allflag is set. */
5223 if (allflag || user_breakpoint_p (b))
5224 print_one_breakpoint (b, &last_loc, print_address_bits, allflag);
5225 }
5226
5227 do_cleanups (bkpttbl_chain);
5228
5229 if (nr_printable_breakpoints == 0)
5230 {
5231 /* If there's a filter, let the caller decide how to report
5232 empty list. */
5233 if (!filter)
5234 {
5235 if (args == NULL || *args == '\0')
5236 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5237 else
5238 ui_out_message (uiout, 0,
5239 "No breakpoint or watchpoint matching '%s'.\n",
5240 args);
5241 }
5242 }
5243 else
5244 {
5245 if (last_loc && !server_command)
5246 set_next_address (last_loc->gdbarch, last_loc->address);
5247 }
5248
5249 /* FIXME? Should this be moved up so that it is only called when
5250 there have been breakpoints? */
5251 annotate_breakpoints_table_end ();
5252
5253 return nr_printable_breakpoints;
5254 }
5255
5256 /* Display the value of default-collect in a way that is generally
5257 compatible with the breakpoint list. */
5258
5259 static void
5260 default_collect_info (void)
5261 {
5262 /* If it has no value (which is frequently the case), say nothing; a
5263 message like "No default-collect." gets in user's face when it's
5264 not wanted. */
5265 if (!*default_collect)
5266 return;
5267
5268 /* The following phrase lines up nicely with per-tracepoint collect
5269 actions. */
5270 ui_out_text (uiout, "default collect ");
5271 ui_out_field_string (uiout, "default-collect", default_collect);
5272 ui_out_text (uiout, " \n");
5273 }
5274
5275 static void
5276 breakpoints_info (char *args, int from_tty)
5277 {
5278 breakpoint_1 (args, 0, NULL);
5279
5280 default_collect_info ();
5281 }
5282
5283 static void
5284 watchpoints_info (char *args, int from_tty)
5285 {
5286 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
5287
5288 if (num_printed == 0)
5289 {
5290 if (args == NULL || *args == '\0')
5291 ui_out_message (uiout, 0, "No watchpoints.\n");
5292 else
5293 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
5294 }
5295 }
5296
5297 static void
5298 maintenance_info_breakpoints (char *args, int from_tty)
5299 {
5300 breakpoint_1 (args, 1, NULL);
5301
5302 default_collect_info ();
5303 }
5304
5305 static int
5306 breakpoint_has_pc (struct breakpoint *b,
5307 struct program_space *pspace,
5308 CORE_ADDR pc, struct obj_section *section)
5309 {
5310 struct bp_location *bl = b->loc;
5311
5312 for (; bl; bl = bl->next)
5313 {
5314 if (bl->pspace == pspace
5315 && bl->address == pc
5316 && (!overlay_debugging || bl->section == section))
5317 return 1;
5318 }
5319 return 0;
5320 }
5321
5322 /* Print a message describing any breakpoints set at PC. This
5323 concerns with logical breakpoints, so we match program spaces, not
5324 address spaces. */
5325
5326 static void
5327 describe_other_breakpoints (struct gdbarch *gdbarch,
5328 struct program_space *pspace, CORE_ADDR pc,
5329 struct obj_section *section, int thread)
5330 {
5331 int others = 0;
5332 struct breakpoint *b;
5333
5334 ALL_BREAKPOINTS (b)
5335 others += breakpoint_has_pc (b, pspace, pc, section);
5336 if (others > 0)
5337 {
5338 if (others == 1)
5339 printf_filtered (_("Note: breakpoint "));
5340 else /* if (others == ???) */
5341 printf_filtered (_("Note: breakpoints "));
5342 ALL_BREAKPOINTS (b)
5343 if (breakpoint_has_pc (b, pspace, pc, section))
5344 {
5345 others--;
5346 printf_filtered ("%d", b->number);
5347 if (b->thread == -1 && thread != -1)
5348 printf_filtered (" (all threads)");
5349 else if (b->thread != -1)
5350 printf_filtered (" (thread %d)", b->thread);
5351 printf_filtered ("%s%s ",
5352 ((b->enable_state == bp_disabled
5353 || b->enable_state == bp_call_disabled
5354 || b->enable_state == bp_startup_disabled)
5355 ? " (disabled)"
5356 : b->enable_state == bp_permanent
5357 ? " (permanent)"
5358 : ""),
5359 (others > 1) ? ","
5360 : ((others == 1) ? " and" : ""));
5361 }
5362 printf_filtered (_("also set at pc "));
5363 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
5364 printf_filtered (".\n");
5365 }
5366 }
5367 \f
5368 /* Set the default place to put a breakpoint
5369 for the `break' command with no arguments. */
5370
5371 void
5372 set_default_breakpoint (int valid, struct program_space *pspace,
5373 CORE_ADDR addr, struct symtab *symtab,
5374 int line)
5375 {
5376 default_breakpoint_valid = valid;
5377 default_breakpoint_pspace = pspace;
5378 default_breakpoint_address = addr;
5379 default_breakpoint_symtab = symtab;
5380 default_breakpoint_line = line;
5381 }
5382
5383 /* Return true iff it is meaningful to use the address member of
5384 BPT. For some breakpoint types, the address member is irrelevant
5385 and it makes no sense to attempt to compare it to other addresses
5386 (or use it for any other purpose either).
5387
5388 More specifically, each of the following breakpoint types will
5389 always have a zero valued address and we don't want to mark
5390 breakpoints of any of these types to be a duplicate of an actual
5391 breakpoint at address zero:
5392
5393 bp_watchpoint
5394 bp_catchpoint
5395
5396 */
5397
5398 static int
5399 breakpoint_address_is_meaningful (struct breakpoint *bpt)
5400 {
5401 enum bptype type = bpt->type;
5402
5403 return (type != bp_watchpoint && type != bp_catchpoint);
5404 }
5405
5406 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5407 true if LOC1 and LOC2 represent the same watchpoint location. */
5408
5409 static int
5410 watchpoint_locations_match (struct bp_location *loc1,
5411 struct bp_location *loc2)
5412 {
5413 /* Both of them must not be in moribund_locations. */
5414 gdb_assert (loc1->owner != NULL);
5415 gdb_assert (loc2->owner != NULL);
5416
5417 /* If the target can evaluate the condition expression in hardware,
5418 then we we need to insert both watchpoints even if they are at
5419 the same place. Otherwise the watchpoint will only trigger when
5420 the condition of whichever watchpoint was inserted evaluates to
5421 true, not giving a chance for GDB to check the condition of the
5422 other watchpoint. */
5423 if ((loc1->owner->cond_exp
5424 && target_can_accel_watchpoint_condition (loc1->address,
5425 loc1->length,
5426 loc1->watchpoint_type,
5427 loc1->owner->cond_exp))
5428 || (loc2->owner->cond_exp
5429 && target_can_accel_watchpoint_condition (loc2->address,
5430 loc2->length,
5431 loc2->watchpoint_type,
5432 loc2->owner->cond_exp)))
5433 return 0;
5434
5435 /* Note that this checks the owner's type, not the location's. In
5436 case the target does not support read watchpoints, but does
5437 support access watchpoints, we'll have bp_read_watchpoint
5438 watchpoints with hw_access locations. Those should be considered
5439 duplicates of hw_read locations. The hw_read locations will
5440 become hw_access locations later. */
5441 return (loc1->owner->type == loc2->owner->type
5442 && loc1->pspace->aspace == loc2->pspace->aspace
5443 && loc1->address == loc2->address
5444 && loc1->length == loc2->length);
5445 }
5446
5447 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5448 same breakpoint location. In most targets, this can only be true
5449 if ASPACE1 matches ASPACE2. On targets that have global
5450 breakpoints, the address space doesn't really matter. */
5451
5452 static int
5453 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5454 struct address_space *aspace2, CORE_ADDR addr2)
5455 {
5456 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5457 || aspace1 == aspace2)
5458 && addr1 == addr2);
5459 }
5460
5461 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
5462 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5463 represent the same location. */
5464
5465 static int
5466 breakpoint_locations_match (struct bp_location *loc1,
5467 struct bp_location *loc2)
5468 {
5469 int hw_point1, hw_point2;
5470
5471 /* Both of them must not be in moribund_locations. */
5472 gdb_assert (loc1->owner != NULL);
5473 gdb_assert (loc2->owner != NULL);
5474
5475 hw_point1 = is_hardware_watchpoint (loc1->owner);
5476 hw_point2 = is_hardware_watchpoint (loc2->owner);
5477
5478 if (hw_point1 != hw_point2)
5479 return 0;
5480 else if (hw_point1)
5481 return watchpoint_locations_match (loc1, loc2);
5482 else
5483 return breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5484 loc2->pspace->aspace, loc2->address);
5485 }
5486
5487 static void
5488 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5489 int bnum, int have_bnum)
5490 {
5491 char astr1[40];
5492 char astr2[40];
5493
5494 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5495 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
5496 if (have_bnum)
5497 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
5498 bnum, astr1, astr2);
5499 else
5500 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
5501 }
5502
5503 /* Adjust a breakpoint's address to account for architectural
5504 constraints on breakpoint placement. Return the adjusted address.
5505 Note: Very few targets require this kind of adjustment. For most
5506 targets, this function is simply the identity function. */
5507
5508 static CORE_ADDR
5509 adjust_breakpoint_address (struct gdbarch *gdbarch,
5510 CORE_ADDR bpaddr, enum bptype bptype)
5511 {
5512 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
5513 {
5514 /* Very few targets need any kind of breakpoint adjustment. */
5515 return bpaddr;
5516 }
5517 else if (bptype == bp_watchpoint
5518 || bptype == bp_hardware_watchpoint
5519 || bptype == bp_read_watchpoint
5520 || bptype == bp_access_watchpoint
5521 || bptype == bp_catchpoint)
5522 {
5523 /* Watchpoints and the various bp_catch_* eventpoints should not
5524 have their addresses modified. */
5525 return bpaddr;
5526 }
5527 else
5528 {
5529 CORE_ADDR adjusted_bpaddr;
5530
5531 /* Some targets have architectural constraints on the placement
5532 of breakpoint instructions. Obtain the adjusted address. */
5533 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
5534
5535 /* An adjusted breakpoint address can significantly alter
5536 a user's expectations. Print a warning if an adjustment
5537 is required. */
5538 if (adjusted_bpaddr != bpaddr)
5539 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5540
5541 return adjusted_bpaddr;
5542 }
5543 }
5544
5545 /* Allocate a struct bp_location. */
5546
5547 static struct bp_location *
5548 allocate_bp_location (struct breakpoint *bpt)
5549 {
5550 struct bp_location *loc;
5551
5552 loc = xmalloc (sizeof (struct bp_location));
5553 memset (loc, 0, sizeof (*loc));
5554
5555 loc->owner = bpt;
5556 loc->cond = NULL;
5557 loc->shlib_disabled = 0;
5558 loc->enabled = 1;
5559
5560 switch (bpt->type)
5561 {
5562 case bp_breakpoint:
5563 case bp_until:
5564 case bp_finish:
5565 case bp_longjmp:
5566 case bp_longjmp_resume:
5567 case bp_exception:
5568 case bp_exception_resume:
5569 case bp_step_resume:
5570 case bp_watchpoint_scope:
5571 case bp_call_dummy:
5572 case bp_std_terminate:
5573 case bp_shlib_event:
5574 case bp_thread_event:
5575 case bp_overlay_event:
5576 case bp_jit_event:
5577 case bp_longjmp_master:
5578 case bp_std_terminate_master:
5579 case bp_exception_master:
5580 loc->loc_type = bp_loc_software_breakpoint;
5581 break;
5582 case bp_hardware_breakpoint:
5583 loc->loc_type = bp_loc_hardware_breakpoint;
5584 break;
5585 case bp_hardware_watchpoint:
5586 case bp_read_watchpoint:
5587 case bp_access_watchpoint:
5588 loc->loc_type = bp_loc_hardware_watchpoint;
5589 break;
5590 case bp_watchpoint:
5591 case bp_catchpoint:
5592 case bp_tracepoint:
5593 case bp_fast_tracepoint:
5594 case bp_static_tracepoint:
5595 loc->loc_type = bp_loc_other;
5596 break;
5597 default:
5598 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
5599 }
5600
5601 loc->refc = 1;
5602 return loc;
5603 }
5604
5605 static void
5606 free_bp_location (struct bp_location *loc)
5607 {
5608 if (loc->cond)
5609 xfree (loc->cond);
5610
5611 if (loc->function_name)
5612 xfree (loc->function_name);
5613
5614 xfree (loc);
5615 }
5616
5617 /* Increment reference count. */
5618
5619 static void
5620 incref_bp_location (struct bp_location *bl)
5621 {
5622 ++bl->refc;
5623 }
5624
5625 /* Decrement reference count. If the reference count reaches 0,
5626 destroy the bp_location. Sets *BLP to NULL. */
5627
5628 static void
5629 decref_bp_location (struct bp_location **blp)
5630 {
5631 gdb_assert ((*blp)->refc > 0);
5632
5633 if (--(*blp)->refc == 0)
5634 free_bp_location (*blp);
5635 *blp = NULL;
5636 }
5637
5638 /* Helper to set_raw_breakpoint below. Creates a breakpoint that has
5639 type BPTYPE and has no locations as yet. */
5640 /* This function is used in gdbtk sources and thus can not be made
5641 static. */
5642
5643 static struct breakpoint *
5644 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5645 enum bptype bptype)
5646 {
5647 struct breakpoint *b, *b1;
5648
5649 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5650 memset (b, 0, sizeof (*b));
5651
5652 b->type = bptype;
5653 b->gdbarch = gdbarch;
5654 b->language = current_language->la_language;
5655 b->input_radix = input_radix;
5656 b->thread = -1;
5657 b->enable_state = bp_enabled;
5658 b->next = 0;
5659 b->silent = 0;
5660 b->ignore_count = 0;
5661 b->commands = NULL;
5662 b->frame_id = null_frame_id;
5663 b->forked_inferior_pid = null_ptid;
5664 b->exec_pathname = NULL;
5665 b->syscalls_to_be_caught = NULL;
5666 b->ops = NULL;
5667 b->condition_not_parsed = 0;
5668 b->py_bp_object = NULL;
5669
5670 /* Add this breakpoint to the end of the chain so that a list of
5671 breakpoints will come out in order of increasing numbers. */
5672
5673 b1 = breakpoint_chain;
5674 if (b1 == 0)
5675 breakpoint_chain = b;
5676 else
5677 {
5678 while (b1->next)
5679 b1 = b1->next;
5680 b1->next = b;
5681 }
5682 return b;
5683 }
5684
5685 /* Initialize loc->function_name. */
5686 static void
5687 set_breakpoint_location_function (struct bp_location *loc)
5688 {
5689 gdb_assert (loc->owner != NULL);
5690
5691 if (loc->owner->type == bp_breakpoint
5692 || loc->owner->type == bp_hardware_breakpoint
5693 || is_tracepoint (loc->owner))
5694 {
5695 find_pc_partial_function (loc->address, &(loc->function_name),
5696 NULL, NULL);
5697 if (loc->function_name)
5698 loc->function_name = xstrdup (loc->function_name);
5699 }
5700 }
5701
5702 /* Attempt to determine architecture of location identified by SAL. */
5703 static struct gdbarch *
5704 get_sal_arch (struct symtab_and_line sal)
5705 {
5706 if (sal.section)
5707 return get_objfile_arch (sal.section->objfile);
5708 if (sal.symtab)
5709 return get_objfile_arch (sal.symtab->objfile);
5710
5711 return NULL;
5712 }
5713
5714 /* set_raw_breakpoint is a low level routine for allocating and
5715 partially initializing a breakpoint of type BPTYPE. The newly
5716 created breakpoint's address, section, source file name, and line
5717 number are provided by SAL. The newly created and partially
5718 initialized breakpoint is added to the breakpoint chain and
5719 is also returned as the value of this function.
5720
5721 It is expected that the caller will complete the initialization of
5722 the newly created breakpoint struct as well as output any status
5723 information regarding the creation of a new breakpoint. In
5724 particular, set_raw_breakpoint does NOT set the breakpoint
5725 number! Care should be taken to not allow an error to occur
5726 prior to completing the initialization of the breakpoint. If this
5727 should happen, a bogus breakpoint will be left on the chain. */
5728
5729 struct breakpoint *
5730 set_raw_breakpoint (struct gdbarch *gdbarch,
5731 struct symtab_and_line sal, enum bptype bptype)
5732 {
5733 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch,
5734 bptype);
5735 CORE_ADDR adjusted_address;
5736 struct gdbarch *loc_gdbarch;
5737
5738 loc_gdbarch = get_sal_arch (sal);
5739 if (!loc_gdbarch)
5740 loc_gdbarch = b->gdbarch;
5741
5742 if (bptype != bp_catchpoint)
5743 gdb_assert (sal.pspace != NULL);
5744
5745 /* Adjust the breakpoint's address prior to allocating a location.
5746 Once we call allocate_bp_location(), that mostly uninitialized
5747 location will be placed on the location chain. Adjustment of the
5748 breakpoint may cause target_read_memory() to be called and we do
5749 not want its scan of the location chain to find a breakpoint and
5750 location that's only been partially initialized. */
5751 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5752 sal.pc, b->type);
5753
5754 b->loc = allocate_bp_location (b);
5755 b->loc->gdbarch = loc_gdbarch;
5756 b->loc->requested_address = sal.pc;
5757 b->loc->address = adjusted_address;
5758 b->loc->pspace = sal.pspace;
5759
5760 /* Store the program space that was used to set the breakpoint, for
5761 breakpoint resetting. */
5762 b->pspace = sal.pspace;
5763
5764 if (sal.symtab == NULL)
5765 b->source_file = NULL;
5766 else
5767 b->source_file = xstrdup (sal.symtab->filename);
5768 b->loc->section = sal.section;
5769 b->line_number = sal.line;
5770
5771 set_breakpoint_location_function (b->loc);
5772
5773 breakpoints_changed ();
5774
5775 return b;
5776 }
5777
5778
5779 /* Note that the breakpoint object B describes a permanent breakpoint
5780 instruction, hard-wired into the inferior's code. */
5781 void
5782 make_breakpoint_permanent (struct breakpoint *b)
5783 {
5784 struct bp_location *bl;
5785
5786 b->enable_state = bp_permanent;
5787
5788 /* By definition, permanent breakpoints are already present in the
5789 code. Mark all locations as inserted. For now,
5790 make_breakpoint_permanent is called in just one place, so it's
5791 hard to say if it's reasonable to have permanent breakpoint with
5792 multiple locations or not, but it's easy to implmement. */
5793 for (bl = b->loc; bl; bl = bl->next)
5794 bl->inserted = 1;
5795 }
5796
5797 /* Call this routine when stepping and nexting to enable a breakpoint
5798 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5799 initiated the operation. */
5800
5801 void
5802 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
5803 {
5804 struct breakpoint *b, *b_tmp;
5805 int thread = tp->num;
5806
5807 /* To avoid having to rescan all objfile symbols at every step,
5808 we maintain a list of continually-inserted but always disabled
5809 longjmp "master" breakpoints. Here, we simply create momentary
5810 clones of those and enable them for the requested thread. */
5811 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5812 if (b->pspace == current_program_space
5813 && (b->type == bp_longjmp_master
5814 || b->type == bp_exception_master))
5815 {
5816 struct breakpoint *clone = clone_momentary_breakpoint (b);
5817
5818 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5819 clone->thread = thread;
5820 }
5821
5822 tp->initiating_frame = frame;
5823 }
5824
5825 /* Delete all longjmp breakpoints from THREAD. */
5826 void
5827 delete_longjmp_breakpoint (int thread)
5828 {
5829 struct breakpoint *b, *b_tmp;
5830
5831 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5832 if (b->type == bp_longjmp || b->type == bp_exception)
5833 {
5834 if (b->thread == thread)
5835 delete_breakpoint (b);
5836 }
5837 }
5838
5839 void
5840 enable_overlay_breakpoints (void)
5841 {
5842 struct breakpoint *b;
5843
5844 ALL_BREAKPOINTS (b)
5845 if (b->type == bp_overlay_event)
5846 {
5847 b->enable_state = bp_enabled;
5848 update_global_location_list (1);
5849 overlay_events_enabled = 1;
5850 }
5851 }
5852
5853 void
5854 disable_overlay_breakpoints (void)
5855 {
5856 struct breakpoint *b;
5857
5858 ALL_BREAKPOINTS (b)
5859 if (b->type == bp_overlay_event)
5860 {
5861 b->enable_state = bp_disabled;
5862 update_global_location_list (0);
5863 overlay_events_enabled = 0;
5864 }
5865 }
5866
5867 /* Set an active std::terminate breakpoint for each std::terminate
5868 master breakpoint. */
5869 void
5870 set_std_terminate_breakpoint (void)
5871 {
5872 struct breakpoint *b, *b_tmp;
5873
5874 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5875 if (b->pspace == current_program_space
5876 && b->type == bp_std_terminate_master)
5877 {
5878 struct breakpoint *clone = clone_momentary_breakpoint (b);
5879 clone->type = bp_std_terminate;
5880 }
5881 }
5882
5883 /* Delete all the std::terminate breakpoints. */
5884 void
5885 delete_std_terminate_breakpoint (void)
5886 {
5887 struct breakpoint *b, *b_tmp;
5888
5889 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5890 if (b->type == bp_std_terminate)
5891 delete_breakpoint (b);
5892 }
5893
5894 struct breakpoint *
5895 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5896 {
5897 struct breakpoint *b;
5898
5899 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
5900
5901 b->enable_state = bp_enabled;
5902 /* addr_string has to be used or breakpoint_re_set will delete me. */
5903 b->addr_string
5904 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
5905
5906 update_global_location_list_nothrow (1);
5907
5908 return b;
5909 }
5910
5911 void
5912 remove_thread_event_breakpoints (void)
5913 {
5914 struct breakpoint *b, *b_tmp;
5915
5916 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5917 if (b->type == bp_thread_event
5918 && b->loc->pspace == current_program_space)
5919 delete_breakpoint (b);
5920 }
5921
5922 struct captured_parse_breakpoint_args
5923 {
5924 char **arg_p;
5925 struct symtabs_and_lines *sals_p;
5926 char ***addr_string_p;
5927 int *not_found_ptr;
5928 };
5929
5930 struct lang_and_radix
5931 {
5932 enum language lang;
5933 int radix;
5934 };
5935
5936 /* Create a breakpoint for JIT code registration and unregistration. */
5937
5938 struct breakpoint *
5939 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5940 {
5941 struct breakpoint *b;
5942
5943 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
5944 update_global_location_list_nothrow (1);
5945 return b;
5946 }
5947
5948 /* Remove JIT code registration and unregistration breakpoint(s). */
5949
5950 void
5951 remove_jit_event_breakpoints (void)
5952 {
5953 struct breakpoint *b, *b_tmp;
5954
5955 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5956 if (b->type == bp_jit_event
5957 && b->loc->pspace == current_program_space)
5958 delete_breakpoint (b);
5959 }
5960
5961 void
5962 remove_solib_event_breakpoints (void)
5963 {
5964 struct breakpoint *b, *b_tmp;
5965
5966 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5967 if (b->type == bp_shlib_event
5968 && b->loc->pspace == current_program_space)
5969 delete_breakpoint (b);
5970 }
5971
5972 struct breakpoint *
5973 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5974 {
5975 struct breakpoint *b;
5976
5977 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
5978 update_global_location_list_nothrow (1);
5979 return b;
5980 }
5981
5982 /* Disable any breakpoints that are on code in shared libraries. Only
5983 apply to enabled breakpoints, disabled ones can just stay disabled. */
5984
5985 void
5986 disable_breakpoints_in_shlibs (void)
5987 {
5988 struct bp_location *loc, **locp_tmp;
5989
5990 ALL_BP_LOCATIONS (loc, locp_tmp)
5991 {
5992 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
5993 struct breakpoint *b = loc->owner;
5994
5995 /* We apply the check to all breakpoints, including disabled for
5996 those with loc->duplicate set. This is so that when breakpoint
5997 becomes enabled, or the duplicate is removed, gdb will try to
5998 insert all breakpoints. If we don't set shlib_disabled here,
5999 we'll try to insert those breakpoints and fail. */
6000 if (((b->type == bp_breakpoint)
6001 || (b->type == bp_jit_event)
6002 || (b->type == bp_hardware_breakpoint)
6003 || (is_tracepoint (b)))
6004 && loc->pspace == current_program_space
6005 && !loc->shlib_disabled
6006 #ifdef PC_SOLIB
6007 && PC_SOLIB (loc->address)
6008 #else
6009 && solib_name_from_address (loc->pspace, loc->address)
6010 #endif
6011 )
6012 {
6013 loc->shlib_disabled = 1;
6014 }
6015 }
6016 }
6017
6018 /* Disable any breakpoints that are in in an unloaded shared library.
6019 Only apply to enabled breakpoints, disabled ones can just stay
6020 disabled. */
6021
6022 static void
6023 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6024 {
6025 struct bp_location *loc, **locp_tmp;
6026 int disabled_shlib_breaks = 0;
6027
6028 /* SunOS a.out shared libraries are always mapped, so do not
6029 disable breakpoints; they will only be reported as unloaded
6030 through clear_solib when GDB discards its shared library
6031 list. See clear_solib for more information. */
6032 if (exec_bfd != NULL
6033 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6034 return;
6035
6036 ALL_BP_LOCATIONS (loc, locp_tmp)
6037 {
6038 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6039 struct breakpoint *b = loc->owner;
6040
6041 if ((loc->loc_type == bp_loc_hardware_breakpoint
6042 || loc->loc_type == bp_loc_software_breakpoint)
6043 && solib->pspace == loc->pspace
6044 && !loc->shlib_disabled
6045 && (b->type == bp_breakpoint
6046 || b->type == bp_jit_event
6047 || b->type == bp_hardware_breakpoint)
6048 && solib_contains_address_p (solib, loc->address))
6049 {
6050 loc->shlib_disabled = 1;
6051 /* At this point, we cannot rely on remove_breakpoint
6052 succeeding so we must mark the breakpoint as not inserted
6053 to prevent future errors occurring in remove_breakpoints. */
6054 loc->inserted = 0;
6055 if (!disabled_shlib_breaks)
6056 {
6057 target_terminal_ours_for_output ();
6058 warning (_("Temporarily disabling breakpoints "
6059 "for unloaded shared library \"%s\""),
6060 solib->so_name);
6061 }
6062 disabled_shlib_breaks = 1;
6063 }
6064 }
6065 }
6066
6067 /* FORK & VFORK catchpoints. */
6068
6069 /* Implement the "insert" breakpoint_ops method for fork
6070 catchpoints. */
6071
6072 static int
6073 insert_catch_fork (struct bp_location *bl)
6074 {
6075 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
6076 }
6077
6078 /* Implement the "remove" breakpoint_ops method for fork
6079 catchpoints. */
6080
6081 static int
6082 remove_catch_fork (struct bp_location *bl)
6083 {
6084 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6085 }
6086
6087 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
6088 catchpoints. */
6089
6090 static int
6091 breakpoint_hit_catch_fork (struct breakpoint *b)
6092 {
6093 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
6094 }
6095
6096 /* Implement the "print_it" breakpoint_ops method for fork
6097 catchpoints. */
6098
6099 static enum print_stop_action
6100 print_it_catch_fork (struct breakpoint *b)
6101 {
6102 annotate_catchpoint (b->number);
6103 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6104 b->number, ptid_get_pid (b->forked_inferior_pid));
6105 return PRINT_SRC_AND_LOC;
6106 }
6107
6108 /* Implement the "print_one" breakpoint_ops method for fork
6109 catchpoints. */
6110
6111 static void
6112 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
6113 {
6114 struct value_print_options opts;
6115
6116 get_user_print_options (&opts);
6117
6118 /* Field 4, the address, is omitted (which makes the columns not
6119 line up too nicely with the headers, but the effect is relatively
6120 readable). */
6121 if (opts.addressprint)
6122 ui_out_field_skip (uiout, "addr");
6123 annotate_field (5);
6124 ui_out_text (uiout, "fork");
6125 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6126 {
6127 ui_out_text (uiout, ", process ");
6128 ui_out_field_int (uiout, "what",
6129 ptid_get_pid (b->forked_inferior_pid));
6130 ui_out_spaces (uiout, 1);
6131 }
6132 }
6133
6134 /* Implement the "print_mention" breakpoint_ops method for fork
6135 catchpoints. */
6136
6137 static void
6138 print_mention_catch_fork (struct breakpoint *b)
6139 {
6140 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6141 }
6142
6143 /* Implement the "print_recreate" breakpoint_ops method for fork
6144 catchpoints. */
6145
6146 static void
6147 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6148 {
6149 fprintf_unfiltered (fp, "catch fork");
6150 }
6151
6152 /* The breakpoint_ops structure to be used in fork catchpoints. */
6153
6154 static struct breakpoint_ops catch_fork_breakpoint_ops =
6155 {
6156 insert_catch_fork,
6157 remove_catch_fork,
6158 breakpoint_hit_catch_fork,
6159 NULL, /* resources_needed */
6160 print_it_catch_fork,
6161 print_one_catch_fork,
6162 print_mention_catch_fork,
6163 print_recreate_catch_fork
6164 };
6165
6166 /* Implement the "insert" breakpoint_ops method for vfork
6167 catchpoints. */
6168
6169 static int
6170 insert_catch_vfork (struct bp_location *bl)
6171 {
6172 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
6173 }
6174
6175 /* Implement the "remove" breakpoint_ops method for vfork
6176 catchpoints. */
6177
6178 static int
6179 remove_catch_vfork (struct bp_location *bl)
6180 {
6181 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6182 }
6183
6184 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6185 catchpoints. */
6186
6187 static int
6188 breakpoint_hit_catch_vfork (struct breakpoint *b)
6189 {
6190 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
6191 }
6192
6193 /* Implement the "print_it" breakpoint_ops method for vfork
6194 catchpoints. */
6195
6196 static enum print_stop_action
6197 print_it_catch_vfork (struct breakpoint *b)
6198 {
6199 annotate_catchpoint (b->number);
6200 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6201 b->number, ptid_get_pid (b->forked_inferior_pid));
6202 return PRINT_SRC_AND_LOC;
6203 }
6204
6205 /* Implement the "print_one" breakpoint_ops method for vfork
6206 catchpoints. */
6207
6208 static void
6209 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
6210 {
6211 struct value_print_options opts;
6212
6213 get_user_print_options (&opts);
6214 /* Field 4, the address, is omitted (which makes the columns not
6215 line up too nicely with the headers, but the effect is relatively
6216 readable). */
6217 if (opts.addressprint)
6218 ui_out_field_skip (uiout, "addr");
6219 annotate_field (5);
6220 ui_out_text (uiout, "vfork");
6221 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6222 {
6223 ui_out_text (uiout, ", process ");
6224 ui_out_field_int (uiout, "what",
6225 ptid_get_pid (b->forked_inferior_pid));
6226 ui_out_spaces (uiout, 1);
6227 }
6228 }
6229
6230 /* Implement the "print_mention" breakpoint_ops method for vfork
6231 catchpoints. */
6232
6233 static void
6234 print_mention_catch_vfork (struct breakpoint *b)
6235 {
6236 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6237 }
6238
6239 /* Implement the "print_recreate" breakpoint_ops method for vfork
6240 catchpoints. */
6241
6242 static void
6243 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6244 {
6245 fprintf_unfiltered (fp, "catch vfork");
6246 }
6247
6248 /* The breakpoint_ops structure to be used in vfork catchpoints. */
6249
6250 static struct breakpoint_ops catch_vfork_breakpoint_ops =
6251 {
6252 insert_catch_vfork,
6253 remove_catch_vfork,
6254 breakpoint_hit_catch_vfork,
6255 NULL, /* resources_needed */
6256 print_it_catch_vfork,
6257 print_one_catch_vfork,
6258 print_mention_catch_vfork,
6259 print_recreate_catch_vfork
6260 };
6261
6262 /* Implement the "insert" breakpoint_ops method for syscall
6263 catchpoints. */
6264
6265 static int
6266 insert_catch_syscall (struct bp_location *bl)
6267 {
6268 struct inferior *inf = current_inferior ();
6269
6270 ++inf->total_syscalls_count;
6271 if (!bl->owner->syscalls_to_be_caught)
6272 ++inf->any_syscall_count;
6273 else
6274 {
6275 int i, iter;
6276
6277 for (i = 0;
6278 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
6279 i++)
6280 {
6281 int elem;
6282
6283 if (iter >= VEC_length (int, inf->syscalls_counts))
6284 {
6285 int old_size = VEC_length (int, inf->syscalls_counts);
6286 uintptr_t vec_addr_offset
6287 = old_size * ((uintptr_t) sizeof (int));
6288 uintptr_t vec_addr;
6289 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6290 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6291 vec_addr_offset;
6292 memset ((void *) vec_addr, 0,
6293 (iter + 1 - old_size) * sizeof (int));
6294 }
6295 elem = VEC_index (int, inf->syscalls_counts, iter);
6296 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6297 }
6298 }
6299
6300 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6301 inf->total_syscalls_count != 0,
6302 inf->any_syscall_count,
6303 VEC_length (int, inf->syscalls_counts),
6304 VEC_address (int, inf->syscalls_counts));
6305 }
6306
6307 /* Implement the "remove" breakpoint_ops method for syscall
6308 catchpoints. */
6309
6310 static int
6311 remove_catch_syscall (struct bp_location *bl)
6312 {
6313 struct inferior *inf = current_inferior ();
6314
6315 --inf->total_syscalls_count;
6316 if (!bl->owner->syscalls_to_be_caught)
6317 --inf->any_syscall_count;
6318 else
6319 {
6320 int i, iter;
6321
6322 for (i = 0;
6323 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
6324 i++)
6325 {
6326 int elem;
6327 if (iter >= VEC_length (int, inf->syscalls_counts))
6328 /* Shouldn't happen. */
6329 continue;
6330 elem = VEC_index (int, inf->syscalls_counts, iter);
6331 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6332 }
6333 }
6334
6335 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6336 inf->total_syscalls_count != 0,
6337 inf->any_syscall_count,
6338 VEC_length (int, inf->syscalls_counts),
6339 VEC_address (int,
6340 inf->syscalls_counts));
6341 }
6342
6343 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6344 catchpoints. */
6345
6346 static int
6347 breakpoint_hit_catch_syscall (struct breakpoint *b)
6348 {
6349 /* We must check if we are catching specific syscalls in this
6350 breakpoint. If we are, then we must guarantee that the called
6351 syscall is the same syscall we are catching. */
6352 int syscall_number = 0;
6353
6354 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6355 return 0;
6356
6357 /* Now, checking if the syscall is the same. */
6358 if (b->syscalls_to_be_caught)
6359 {
6360 int i, iter;
6361
6362 for (i = 0;
6363 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6364 i++)
6365 if (syscall_number == iter)
6366 break;
6367 /* Not the same. */
6368 if (!iter)
6369 return 0;
6370 }
6371
6372 return 1;
6373 }
6374
6375 /* Implement the "print_it" breakpoint_ops method for syscall
6376 catchpoints. */
6377
6378 static enum print_stop_action
6379 print_it_catch_syscall (struct breakpoint *b)
6380 {
6381 /* These are needed because we want to know in which state a
6382 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6383 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6384 must print "called syscall" or "returned from syscall". */
6385 ptid_t ptid;
6386 struct target_waitstatus last;
6387 struct syscall s;
6388 struct cleanup *old_chain;
6389 char *syscall_id;
6390
6391 get_last_target_status (&ptid, &last);
6392
6393 get_syscall_by_number (last.value.syscall_number, &s);
6394
6395 annotate_catchpoint (b->number);
6396
6397 if (s.name == NULL)
6398 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6399 else
6400 syscall_id = xstrprintf ("'%s'", s.name);
6401
6402 old_chain = make_cleanup (xfree, syscall_id);
6403
6404 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6405 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6406 b->number, syscall_id);
6407 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6408 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6409 b->number, syscall_id);
6410
6411 do_cleanups (old_chain);
6412
6413 return PRINT_SRC_AND_LOC;
6414 }
6415
6416 /* Implement the "print_one" breakpoint_ops method for syscall
6417 catchpoints. */
6418
6419 static void
6420 print_one_catch_syscall (struct breakpoint *b,
6421 struct bp_location **last_loc)
6422 {
6423 struct value_print_options opts;
6424
6425 get_user_print_options (&opts);
6426 /* Field 4, the address, is omitted (which makes the columns not
6427 line up too nicely with the headers, but the effect is relatively
6428 readable). */
6429 if (opts.addressprint)
6430 ui_out_field_skip (uiout, "addr");
6431 annotate_field (5);
6432
6433 if (b->syscalls_to_be_caught
6434 && VEC_length (int, b->syscalls_to_be_caught) > 1)
6435 ui_out_text (uiout, "syscalls \"");
6436 else
6437 ui_out_text (uiout, "syscall \"");
6438
6439 if (b->syscalls_to_be_caught)
6440 {
6441 int i, iter;
6442 char *text = xstrprintf ("%s", "");
6443
6444 for (i = 0;
6445 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6446 i++)
6447 {
6448 char *x = text;
6449 struct syscall s;
6450 get_syscall_by_number (iter, &s);
6451
6452 if (s.name != NULL)
6453 text = xstrprintf ("%s%s, ", text, s.name);
6454 else
6455 text = xstrprintf ("%s%d, ", text, iter);
6456
6457 /* We have to xfree the last 'text' (now stored at 'x')
6458 because xstrprintf dinamically allocates new space for it
6459 on every call. */
6460 xfree (x);
6461 }
6462 /* Remove the last comma. */
6463 text[strlen (text) - 2] = '\0';
6464 ui_out_field_string (uiout, "what", text);
6465 }
6466 else
6467 ui_out_field_string (uiout, "what", "<any syscall>");
6468 ui_out_text (uiout, "\" ");
6469 }
6470
6471 /* Implement the "print_mention" breakpoint_ops method for syscall
6472 catchpoints. */
6473
6474 static void
6475 print_mention_catch_syscall (struct breakpoint *b)
6476 {
6477 if (b->syscalls_to_be_caught)
6478 {
6479 int i, iter;
6480
6481 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6482 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6483 else
6484 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6485
6486 for (i = 0;
6487 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6488 i++)
6489 {
6490 struct syscall s;
6491 get_syscall_by_number (iter, &s);
6492
6493 if (s.name)
6494 printf_filtered (" '%s' [%d]", s.name, s.number);
6495 else
6496 printf_filtered (" %d", s.number);
6497 }
6498 printf_filtered (")");
6499 }
6500 else
6501 printf_filtered (_("Catchpoint %d (any syscall)"),
6502 b->number);
6503 }
6504
6505 /* Implement the "print_recreate" breakpoint_ops method for syscall
6506 catchpoints. */
6507
6508 static void
6509 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6510 {
6511 fprintf_unfiltered (fp, "catch syscall");
6512
6513 if (b->syscalls_to_be_caught)
6514 {
6515 int i, iter;
6516
6517 for (i = 0;
6518 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6519 i++)
6520 {
6521 struct syscall s;
6522
6523 get_syscall_by_number (iter, &s);
6524 if (s.name)
6525 fprintf_unfiltered (fp, " %s", s.name);
6526 else
6527 fprintf_unfiltered (fp, " %d", s.number);
6528 }
6529 }
6530 }
6531
6532 /* The breakpoint_ops structure to be used in syscall catchpoints. */
6533
6534 static struct breakpoint_ops catch_syscall_breakpoint_ops =
6535 {
6536 insert_catch_syscall,
6537 remove_catch_syscall,
6538 breakpoint_hit_catch_syscall,
6539 NULL, /* resources_needed */
6540 print_it_catch_syscall,
6541 print_one_catch_syscall,
6542 print_mention_catch_syscall,
6543 print_recreate_catch_syscall
6544 };
6545
6546 /* Returns non-zero if 'b' is a syscall catchpoint. */
6547
6548 static int
6549 syscall_catchpoint_p (struct breakpoint *b)
6550 {
6551 return (b->ops == &catch_syscall_breakpoint_ops);
6552 }
6553
6554 /* Create a new breakpoint of the bp_catchpoint kind and return it,
6555 but does NOT mention it nor update the global location list.
6556 This is useful if you need to fill more fields in the
6557 struct breakpoint before calling mention.
6558
6559 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6560 If COND_STRING is not NULL, then store it in the breakpoint.
6561 OPS, if not NULL, is the breakpoint_ops structure associated
6562 to the catchpoint. */
6563
6564 static struct breakpoint *
6565 create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6566 char *cond_string,
6567 struct breakpoint_ops *ops)
6568 {
6569 struct symtab_and_line sal;
6570 struct breakpoint *b;
6571
6572 init_sal (&sal);
6573 sal.pspace = current_program_space;
6574
6575 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
6576 set_breakpoint_count (breakpoint_count + 1);
6577 b->number = breakpoint_count;
6578
6579 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
6580 b->thread = -1;
6581 b->addr_string = NULL;
6582 b->enable_state = bp_enabled;
6583 b->disposition = tempflag ? disp_del : disp_donttouch;
6584 b->ops = ops;
6585
6586 return b;
6587 }
6588
6589 /* Create a new breakpoint of the bp_catchpoint kind and return it.
6590
6591 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6592 If COND_STRING is not NULL, then store it in the breakpoint.
6593 OPS, if not NULL, is the breakpoint_ops structure associated
6594 to the catchpoint. */
6595
6596 static struct breakpoint *
6597 create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6598 char *cond_string, struct breakpoint_ops *ops)
6599 {
6600 struct breakpoint *b =
6601 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6602
6603 mention (b);
6604 update_global_location_list (1);
6605
6606 return b;
6607 }
6608
6609 static void
6610 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6611 int tempflag, char *cond_string,
6612 struct breakpoint_ops *ops)
6613 {
6614 struct breakpoint *b
6615 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
6616
6617 /* FIXME: We should put this information in a breakpoint private data
6618 area. */
6619 b->forked_inferior_pid = null_ptid;
6620 }
6621
6622 /* Exec catchpoints. */
6623
6624 static int
6625 insert_catch_exec (struct bp_location *bl)
6626 {
6627 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
6628 }
6629
6630 static int
6631 remove_catch_exec (struct bp_location *bl)
6632 {
6633 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6634 }
6635
6636 static int
6637 breakpoint_hit_catch_exec (struct breakpoint *b)
6638 {
6639 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
6640 }
6641
6642 static enum print_stop_action
6643 print_it_catch_exec (struct breakpoint *b)
6644 {
6645 annotate_catchpoint (b->number);
6646 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6647 b->exec_pathname);
6648 return PRINT_SRC_AND_LOC;
6649 }
6650
6651 static void
6652 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
6653 {
6654 struct value_print_options opts;
6655
6656 get_user_print_options (&opts);
6657
6658 /* Field 4, the address, is omitted (which makes the columns
6659 not line up too nicely with the headers, but the effect
6660 is relatively readable). */
6661 if (opts.addressprint)
6662 ui_out_field_skip (uiout, "addr");
6663 annotate_field (5);
6664 ui_out_text (uiout, "exec");
6665 if (b->exec_pathname != NULL)
6666 {
6667 ui_out_text (uiout, ", program \"");
6668 ui_out_field_string (uiout, "what", b->exec_pathname);
6669 ui_out_text (uiout, "\" ");
6670 }
6671 }
6672
6673 static void
6674 print_mention_catch_exec (struct breakpoint *b)
6675 {
6676 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6677 }
6678
6679 /* Implement the "print_recreate" breakpoint_ops method for exec
6680 catchpoints. */
6681
6682 static void
6683 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6684 {
6685 fprintf_unfiltered (fp, "catch exec");
6686 }
6687
6688 static struct breakpoint_ops catch_exec_breakpoint_ops =
6689 {
6690 insert_catch_exec,
6691 remove_catch_exec,
6692 breakpoint_hit_catch_exec,
6693 NULL, /* resources_needed */
6694 print_it_catch_exec,
6695 print_one_catch_exec,
6696 print_mention_catch_exec,
6697 print_recreate_catch_exec
6698 };
6699
6700 static void
6701 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6702 struct breakpoint_ops *ops)
6703 {
6704 struct gdbarch *gdbarch = get_current_arch ();
6705 struct breakpoint *b =
6706 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6707
6708 b->syscalls_to_be_caught = filter;
6709
6710 /* Now, we have to mention the breakpoint and update the global
6711 location list. */
6712 mention (b);
6713 update_global_location_list (1);
6714 }
6715
6716 static int
6717 hw_breakpoint_used_count (void)
6718 {
6719 struct breakpoint *b;
6720 int i = 0;
6721
6722 ALL_BREAKPOINTS (b)
6723 {
6724 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
6725 i++;
6726 }
6727
6728 return i;
6729 }
6730
6731 static int
6732 hw_watchpoint_used_count (enum bptype type, int *other_type_used)
6733 {
6734 int i = 0;
6735 struct breakpoint *b;
6736 struct bp_location *bl;
6737
6738 *other_type_used = 0;
6739 ALL_BREAKPOINTS (b)
6740 {
6741 if (!breakpoint_enabled (b))
6742 continue;
6743
6744 if (b->type == type)
6745 for (bl = b->loc; bl; bl = bl->next)
6746 {
6747 /* Special types of hardware watchpoints may use more than
6748 one register. */
6749 if (b->ops && b->ops->resources_needed)
6750 i += b->ops->resources_needed (bl);
6751 else
6752 i++;
6753 }
6754 else if (is_hardware_watchpoint (b))
6755 *other_type_used = 1;
6756 }
6757
6758 return i;
6759 }
6760
6761 void
6762 disable_watchpoints_before_interactive_call_start (void)
6763 {
6764 struct breakpoint *b;
6765
6766 ALL_BREAKPOINTS (b)
6767 {
6768 if (is_watchpoint (b) && breakpoint_enabled (b))
6769 {
6770 b->enable_state = bp_call_disabled;
6771 update_global_location_list (0);
6772 }
6773 }
6774 }
6775
6776 void
6777 enable_watchpoints_after_interactive_call_stop (void)
6778 {
6779 struct breakpoint *b;
6780
6781 ALL_BREAKPOINTS (b)
6782 {
6783 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
6784 {
6785 b->enable_state = bp_enabled;
6786 update_global_location_list (1);
6787 }
6788 }
6789 }
6790
6791 void
6792 disable_breakpoints_before_startup (void)
6793 {
6794 struct breakpoint *b;
6795 int found = 0;
6796
6797 ALL_BREAKPOINTS (b)
6798 {
6799 if (b->pspace != current_program_space)
6800 continue;
6801
6802 if ((b->type == bp_breakpoint
6803 || b->type == bp_hardware_breakpoint)
6804 && breakpoint_enabled (b))
6805 {
6806 b->enable_state = bp_startup_disabled;
6807 found = 1;
6808 }
6809 }
6810
6811 if (found)
6812 update_global_location_list (0);
6813
6814 current_program_space->executing_startup = 1;
6815 }
6816
6817 void
6818 enable_breakpoints_after_startup (void)
6819 {
6820 struct breakpoint *b;
6821 int found = 0;
6822
6823 current_program_space->executing_startup = 0;
6824
6825 ALL_BREAKPOINTS (b)
6826 {
6827 if (b->pspace != current_program_space)
6828 continue;
6829
6830 if ((b->type == bp_breakpoint
6831 || b->type == bp_hardware_breakpoint)
6832 && b->enable_state == bp_startup_disabled)
6833 {
6834 b->enable_state = bp_enabled;
6835 found = 1;
6836 }
6837 }
6838
6839 if (found)
6840 breakpoint_re_set ();
6841 }
6842
6843
6844 /* Set a breakpoint that will evaporate an end of command
6845 at address specified by SAL.
6846 Restrict it to frame FRAME if FRAME is nonzero. */
6847
6848 struct breakpoint *
6849 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6850 struct frame_id frame_id, enum bptype type)
6851 {
6852 struct breakpoint *b;
6853
6854 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6855 one. */
6856 gdb_assert (!frame_id_inlined_p (frame_id));
6857
6858 b = set_raw_breakpoint (gdbarch, sal, type);
6859 b->enable_state = bp_enabled;
6860 b->disposition = disp_donttouch;
6861 b->frame_id = frame_id;
6862
6863 /* If we're debugging a multi-threaded program, then we want
6864 momentary breakpoints to be active in only a single thread of
6865 control. */
6866 if (in_thread_list (inferior_ptid))
6867 b->thread = pid_to_thread_id (inferior_ptid);
6868
6869 update_global_location_list_nothrow (1);
6870
6871 return b;
6872 }
6873
6874 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6875 ORIG is NULL. */
6876
6877 struct breakpoint *
6878 clone_momentary_breakpoint (struct breakpoint *orig)
6879 {
6880 struct breakpoint *copy;
6881
6882 /* If there's nothing to clone, then return nothing. */
6883 if (orig == NULL)
6884 return NULL;
6885
6886 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
6887 copy->loc = allocate_bp_location (copy);
6888 set_breakpoint_location_function (copy->loc);
6889
6890 copy->loc->gdbarch = orig->loc->gdbarch;
6891 copy->loc->requested_address = orig->loc->requested_address;
6892 copy->loc->address = orig->loc->address;
6893 copy->loc->section = orig->loc->section;
6894 copy->loc->pspace = orig->loc->pspace;
6895
6896 if (orig->source_file == NULL)
6897 copy->source_file = NULL;
6898 else
6899 copy->source_file = xstrdup (orig->source_file);
6900
6901 copy->line_number = orig->line_number;
6902 copy->frame_id = orig->frame_id;
6903 copy->thread = orig->thread;
6904 copy->pspace = orig->pspace;
6905
6906 copy->enable_state = bp_enabled;
6907 copy->disposition = disp_donttouch;
6908 copy->number = internal_breakpoint_number--;
6909
6910 update_global_location_list_nothrow (0);
6911 return copy;
6912 }
6913
6914 struct breakpoint *
6915 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6916 enum bptype type)
6917 {
6918 struct symtab_and_line sal;
6919
6920 sal = find_pc_line (pc, 0);
6921 sal.pc = pc;
6922 sal.section = find_pc_overlay (pc);
6923 sal.explicit_pc = 1;
6924
6925 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
6926 }
6927 \f
6928
6929 /* Tell the user we have just set a breakpoint B. */
6930
6931 static void
6932 mention (struct breakpoint *b)
6933 {
6934 int say_where = 0;
6935 struct cleanup *ui_out_chain;
6936 struct value_print_options opts;
6937
6938 get_user_print_options (&opts);
6939
6940 /* FIXME: This is misplaced; mention() is called by things (like
6941 hitting a watchpoint) other than breakpoint creation. It should
6942 be possible to clean this up and at the same time replace the
6943 random calls to breakpoint_changed with this hook. */
6944 observer_notify_breakpoint_created (b->number);
6945
6946 if (b->ops != NULL && b->ops->print_mention != NULL)
6947 b->ops->print_mention (b);
6948 else
6949 switch (b->type)
6950 {
6951 case bp_none:
6952 printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
6953 b->number);
6954 break;
6955 case bp_watchpoint:
6956 ui_out_text (uiout, "Watchpoint ");
6957 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6958 ui_out_field_int (uiout, "number", b->number);
6959 ui_out_text (uiout, ": ");
6960 ui_out_field_string (uiout, "exp", b->exp_string);
6961 do_cleanups (ui_out_chain);
6962 break;
6963 case bp_hardware_watchpoint:
6964 ui_out_text (uiout, "Hardware watchpoint ");
6965 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6966 ui_out_field_int (uiout, "number", b->number);
6967 ui_out_text (uiout, ": ");
6968 ui_out_field_string (uiout, "exp", b->exp_string);
6969 do_cleanups (ui_out_chain);
6970 break;
6971 case bp_read_watchpoint:
6972 ui_out_text (uiout, "Hardware read watchpoint ");
6973 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
6974 ui_out_field_int (uiout, "number", b->number);
6975 ui_out_text (uiout, ": ");
6976 ui_out_field_string (uiout, "exp", b->exp_string);
6977 do_cleanups (ui_out_chain);
6978 break;
6979 case bp_access_watchpoint:
6980 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
6981 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
6982 ui_out_field_int (uiout, "number", b->number);
6983 ui_out_text (uiout, ": ");
6984 ui_out_field_string (uiout, "exp", b->exp_string);
6985 do_cleanups (ui_out_chain);
6986 break;
6987 case bp_breakpoint:
6988 if (ui_out_is_mi_like_p (uiout))
6989 {
6990 say_where = 0;
6991 break;
6992 }
6993 if (b->disposition == disp_del)
6994 printf_filtered (_("Temporary breakpoint"));
6995 else
6996 printf_filtered (_("Breakpoint"));
6997 printf_filtered (_(" %d"), b->number);
6998 say_where = 1;
6999 break;
7000 case bp_hardware_breakpoint:
7001 if (ui_out_is_mi_like_p (uiout))
7002 {
7003 say_where = 0;
7004 break;
7005 }
7006 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
7007 say_where = 1;
7008 break;
7009 case bp_tracepoint:
7010 if (ui_out_is_mi_like_p (uiout))
7011 {
7012 say_where = 0;
7013 break;
7014 }
7015 printf_filtered (_("Tracepoint"));
7016 printf_filtered (_(" %d"), b->number);
7017 say_where = 1;
7018 break;
7019 case bp_fast_tracepoint:
7020 if (ui_out_is_mi_like_p (uiout))
7021 {
7022 say_where = 0;
7023 break;
7024 }
7025 printf_filtered (_("Fast tracepoint"));
7026 printf_filtered (_(" %d"), b->number);
7027 say_where = 1;
7028 break;
7029 case bp_static_tracepoint:
7030 if (ui_out_is_mi_like_p (uiout))
7031 {
7032 say_where = 0;
7033 break;
7034 }
7035 printf_filtered (_("Static tracepoint"));
7036 printf_filtered (_(" %d"), b->number);
7037 say_where = 1;
7038 break;
7039
7040 case bp_until:
7041 case bp_finish:
7042 case bp_longjmp:
7043 case bp_longjmp_resume:
7044 case bp_exception:
7045 case bp_exception_resume:
7046 case bp_step_resume:
7047 case bp_call_dummy:
7048 case bp_std_terminate:
7049 case bp_watchpoint_scope:
7050 case bp_shlib_event:
7051 case bp_thread_event:
7052 case bp_overlay_event:
7053 case bp_jit_event:
7054 case bp_longjmp_master:
7055 case bp_std_terminate_master:
7056 case bp_exception_master:
7057 break;
7058 }
7059
7060 if (say_where)
7061 {
7062 /* i18n: cagney/2005-02-11: Below needs to be merged into a
7063 single string. */
7064 if (b->loc == NULL)
7065 {
7066 printf_filtered (_(" (%s) pending."), b->addr_string);
7067 }
7068 else
7069 {
7070 if (opts.addressprint || b->source_file == NULL)
7071 {
7072 printf_filtered (" at ");
7073 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7074 gdb_stdout);
7075 }
7076 if (b->source_file)
7077 printf_filtered (": file %s, line %d.",
7078 b->source_file, b->line_number);
7079
7080 if (b->loc->next)
7081 {
7082 struct bp_location *loc = b->loc;
7083 int n = 0;
7084 for (; loc; loc = loc->next)
7085 ++n;
7086 printf_filtered (" (%d locations)", n);
7087 }
7088
7089 }
7090 }
7091 if (ui_out_is_mi_like_p (uiout))
7092 return;
7093 printf_filtered ("\n");
7094 }
7095 \f
7096
7097 static struct bp_location *
7098 add_location_to_breakpoint (struct breakpoint *b,
7099 const struct symtab_and_line *sal)
7100 {
7101 struct bp_location *loc, **tmp;
7102
7103 loc = allocate_bp_location (b);
7104 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7105 ;
7106 *tmp = loc;
7107 loc->gdbarch = get_sal_arch (*sal);
7108 if (!loc->gdbarch)
7109 loc->gdbarch = b->gdbarch;
7110 loc->requested_address = sal->pc;
7111 loc->address = adjust_breakpoint_address (loc->gdbarch,
7112 loc->requested_address, b->type);
7113 loc->pspace = sal->pspace;
7114 gdb_assert (loc->pspace != NULL);
7115 loc->section = sal->section;
7116
7117 set_breakpoint_location_function (loc);
7118 return loc;
7119 }
7120 \f
7121
7122 /* Return 1 if LOC is pointing to a permanent breakpoint,
7123 return 0 otherwise. */
7124
7125 static int
7126 bp_loc_is_permanent (struct bp_location *loc)
7127 {
7128 int len;
7129 CORE_ADDR addr;
7130 const gdb_byte *brk;
7131 gdb_byte *target_mem;
7132 struct cleanup *cleanup;
7133 int retval = 0;
7134
7135 gdb_assert (loc != NULL);
7136
7137 addr = loc->address;
7138 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
7139
7140 /* Software breakpoints unsupported? */
7141 if (brk == NULL)
7142 return 0;
7143
7144 target_mem = alloca (len);
7145
7146 /* Enable the automatic memory restoration from breakpoints while
7147 we read the memory. Otherwise we could say about our temporary
7148 breakpoints they are permanent. */
7149 cleanup = save_current_space_and_thread ();
7150
7151 switch_to_program_space_and_thread (loc->pspace);
7152 make_show_memory_breakpoints_cleanup (0);
7153
7154 if (target_read_memory (loc->address, target_mem, len) == 0
7155 && memcmp (target_mem, brk, len) == 0)
7156 retval = 1;
7157
7158 do_cleanups (cleanup);
7159
7160 return retval;
7161 }
7162
7163
7164
7165 /* Create a breakpoint with SAL as location. Use ADDR_STRING
7166 as textual description of the location, and COND_STRING
7167 as condition expression. */
7168
7169 static void
7170 create_breakpoint_sal (struct gdbarch *gdbarch,
7171 struct symtabs_and_lines sals, char *addr_string,
7172 char *cond_string,
7173 enum bptype type, enum bpdisp disposition,
7174 int thread, int task, int ignore_count,
7175 struct breakpoint_ops *ops, int from_tty,
7176 int enabled, int internal)
7177 {
7178 struct breakpoint *b = NULL;
7179 int i;
7180
7181 if (type == bp_hardware_breakpoint)
7182 {
7183 int i = hw_breakpoint_used_count ();
7184 int target_resources_ok =
7185 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
7186 i + 1, 0);
7187 if (target_resources_ok == 0)
7188 error (_("No hardware breakpoint support in the target."));
7189 else if (target_resources_ok < 0)
7190 error (_("Hardware breakpoints used exceeds limit."));
7191 }
7192
7193 gdb_assert (sals.nelts > 0);
7194
7195 for (i = 0; i < sals.nelts; ++i)
7196 {
7197 struct symtab_and_line sal = sals.sals[i];
7198 struct bp_location *loc;
7199
7200 if (from_tty)
7201 {
7202 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7203 if (!loc_gdbarch)
7204 loc_gdbarch = gdbarch;
7205
7206 describe_other_breakpoints (loc_gdbarch,
7207 sal.pspace, sal.pc, sal.section, thread);
7208 }
7209
7210 if (i == 0)
7211 {
7212 b = set_raw_breakpoint (gdbarch, sal, type);
7213 set_breakpoint_number (internal, b);
7214 b->thread = thread;
7215 b->task = task;
7216
7217 b->cond_string = cond_string;
7218 b->ignore_count = ignore_count;
7219 b->enable_state = enabled ? bp_enabled : bp_disabled;
7220 b->disposition = disposition;
7221 b->pspace = sals.sals[0].pspace;
7222
7223 if (type == bp_static_tracepoint)
7224 {
7225 struct static_tracepoint_marker marker;
7226
7227 if (is_marker_spec (addr_string))
7228 {
7229 /* We already know the marker exists, otherwise, we
7230 wouldn't see a sal for it. */
7231 char *p = &addr_string[3];
7232 char *endp;
7233 char *marker_str;
7234 int i;
7235
7236 p = skip_spaces (p);
7237
7238 endp = skip_to_space (p);
7239
7240 marker_str = savestring (p, endp - p);
7241 b->static_trace_marker_id = marker_str;
7242
7243 printf_filtered (_("Probed static tracepoint "
7244 "marker \"%s\"\n"),
7245 b->static_trace_marker_id);
7246 }
7247 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7248 {
7249 b->static_trace_marker_id = xstrdup (marker.str_id);
7250 release_static_tracepoint_marker (&marker);
7251
7252 printf_filtered (_("Probed static tracepoint "
7253 "marker \"%s\"\n"),
7254 b->static_trace_marker_id);
7255 }
7256 else
7257 warning (_("Couldn't determine the static "
7258 "tracepoint marker to probe"));
7259 }
7260
7261 if (enabled && b->pspace->executing_startup
7262 && (b->type == bp_breakpoint
7263 || b->type == bp_hardware_breakpoint))
7264 b->enable_state = bp_startup_disabled;
7265
7266 loc = b->loc;
7267 }
7268 else
7269 {
7270 loc = add_location_to_breakpoint (b, &sal);
7271 }
7272
7273 if (bp_loc_is_permanent (loc))
7274 make_breakpoint_permanent (b);
7275
7276 if (b->cond_string)
7277 {
7278 char *arg = b->cond_string;
7279 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
7280 if (*arg)
7281 error (_("Garbage %s follows condition"), arg);
7282 }
7283 }
7284
7285 if (addr_string)
7286 b->addr_string = addr_string;
7287 else
7288 /* addr_string has to be used or breakpoint_re_set will delete
7289 me. */
7290 b->addr_string
7291 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7292
7293 b->ops = ops;
7294 if (internal)
7295 /* Do not mention breakpoints with a negative number, but do
7296 notify observers. */
7297 observer_notify_breakpoint_created (b->number);
7298 else
7299 mention (b);
7300 }
7301
7302 /* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7303 elements to fill the void space. */
7304 static void
7305 remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
7306 {
7307 int i = index_to_remove+1;
7308 int last_index = sal->nelts-1;
7309
7310 for (;i <= last_index; ++i)
7311 sal->sals[i-1] = sal->sals[i];
7312
7313 --(sal->nelts);
7314 }
7315
7316 /* If appropriate, obtains all sals that correspond to the same file
7317 and line as SAL, in all program spaces. Users debugging with IDEs,
7318 will want to set a breakpoint at foo.c:line, and not really care
7319 about program spaces. This is done only if SAL does not have
7320 explicit PC and has line and file information. If we got just a
7321 single expanded sal, return the original.
7322
7323 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7324 which the name of enclosing function is different from SAL. This
7325 makes sure that if we have breakpoint originally set in template
7326 instantiation, say foo<int>(), we won't expand SAL to locations at
7327 the same line in all existing instantiations of 'foo'. */
7328
7329 static struct symtabs_and_lines
7330 expand_line_sal_maybe (struct symtab_and_line sal)
7331 {
7332 struct symtabs_and_lines expanded;
7333 CORE_ADDR original_pc = sal.pc;
7334 char *original_function = NULL;
7335 int found;
7336 int i;
7337 struct cleanup *old_chain;
7338
7339 /* If we have explicit pc, don't expand.
7340 If we have no line number, we can't expand. */
7341 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7342 {
7343 expanded.nelts = 1;
7344 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7345 expanded.sals[0] = sal;
7346 return expanded;
7347 }
7348
7349 sal.pc = 0;
7350
7351 old_chain = save_current_space_and_thread ();
7352
7353 switch_to_program_space_and_thread (sal.pspace);
7354
7355 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
7356
7357 /* Note that expand_line_sal visits *all* program spaces. */
7358 expanded = expand_line_sal (sal);
7359
7360 if (expanded.nelts == 1)
7361 {
7362 /* We had one sal, we got one sal. Return that sal, adjusting it
7363 past the function prologue if necessary. */
7364 xfree (expanded.sals);
7365 expanded.nelts = 1;
7366 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7367 sal.pc = original_pc;
7368 expanded.sals[0] = sal;
7369 skip_prologue_sal (&expanded.sals[0]);
7370 do_cleanups (old_chain);
7371 return expanded;
7372 }
7373
7374 if (!sal.explicit_line)
7375 {
7376 CORE_ADDR func_addr, func_end;
7377 for (i = 0; i < expanded.nelts; ++i)
7378 {
7379 CORE_ADDR pc = expanded.sals[i].pc;
7380 char *this_function;
7381
7382 /* We need to switch threads as well since we're about to
7383 read memory. */
7384 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7385
7386 if (find_pc_partial_function (pc, &this_function,
7387 &func_addr, &func_end))
7388 {
7389 if (this_function
7390 && strcmp (this_function, original_function) != 0)
7391 {
7392 remove_sal (&expanded, i);
7393 --i;
7394 }
7395 }
7396 }
7397 }
7398
7399 /* Skip the function prologue if necessary. */
7400 for (i = 0; i < expanded.nelts; ++i)
7401 skip_prologue_sal (&expanded.sals[i]);
7402
7403 do_cleanups (old_chain);
7404
7405 if (expanded.nelts <= 1)
7406 {
7407 /* This is un ugly workaround. If we get zero expanded sals
7408 then something is really wrong. Fix that by returning the
7409 original sal. */
7410
7411 xfree (expanded.sals);
7412 expanded.nelts = 1;
7413 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7414 sal.pc = original_pc;
7415 expanded.sals[0] = sal;
7416 return expanded;
7417 }
7418
7419 if (original_pc)
7420 {
7421 found = 0;
7422 for (i = 0; i < expanded.nelts; ++i)
7423 if (expanded.sals[i].pc == original_pc)
7424 {
7425 found = 1;
7426 break;
7427 }
7428 gdb_assert (found);
7429 }
7430
7431 return expanded;
7432 }
7433
7434 /* Add SALS.nelts breakpoints to the breakpoint table. For each
7435 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7436 value. COND_STRING, if not NULL, specified the condition to be
7437 used for all breakpoints. Essentially the only case where
7438 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7439 function. In that case, it's still not possible to specify
7440 separate conditions for different overloaded functions, so
7441 we take just a single condition string.
7442
7443 NOTE: If the function succeeds, the caller is expected to cleanup
7444 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
7445 array contents). If the function fails (error() is called), the
7446 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
7447 COND and SALS arrays and each of those arrays contents. */
7448
7449 static void
7450 create_breakpoints_sal (struct gdbarch *gdbarch,
7451 struct symtabs_and_lines sals, char **addr_string,
7452 char *cond_string,
7453 enum bptype type, enum bpdisp disposition,
7454 int thread, int task, int ignore_count,
7455 struct breakpoint_ops *ops, int from_tty,
7456 int enabled, int internal)
7457 {
7458 int i;
7459
7460 for (i = 0; i < sals.nelts; ++i)
7461 {
7462 struct symtabs_and_lines expanded =
7463 expand_line_sal_maybe (sals.sals[i]);
7464
7465 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7466 cond_string, type, disposition,
7467 thread, task, ignore_count, ops,
7468 from_tty, enabled, internal);
7469 }
7470 }
7471
7472 /* Parse ADDRESS which is assumed to be a SAL specification possibly
7473 followed by conditionals. On return, SALS contains an array of SAL
7474 addresses found. ADDR_STRING contains a vector of (canonical)
7475 address strings. ADDRESS points to the end of the SAL.
7476
7477 The array and the line spec strings are allocated on the heap, it is
7478 the caller's responsibility to free them. */
7479
7480 static void
7481 parse_breakpoint_sals (char **address,
7482 struct symtabs_and_lines *sals,
7483 char ***addr_string,
7484 int *not_found_ptr)
7485 {
7486 char *addr_start = *address;
7487
7488 *addr_string = NULL;
7489 /* If no arg given, or if first arg is 'if ', use the default
7490 breakpoint. */
7491 if ((*address) == NULL
7492 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
7493 {
7494 if (default_breakpoint_valid)
7495 {
7496 struct symtab_and_line sal;
7497
7498 init_sal (&sal); /* Initialize to zeroes. */
7499 sals->sals = (struct symtab_and_line *)
7500 xmalloc (sizeof (struct symtab_and_line));
7501 sal.pc = default_breakpoint_address;
7502 sal.line = default_breakpoint_line;
7503 sal.symtab = default_breakpoint_symtab;
7504 sal.pspace = default_breakpoint_pspace;
7505 sal.section = find_pc_overlay (sal.pc);
7506
7507 /* "break" without arguments is equivalent to "break *PC"
7508 where PC is the default_breakpoint_address. So make sure
7509 to set sal.explicit_pc to prevent GDB from trying to
7510 expand the list of sals to include all other instances
7511 with the same symtab and line. */
7512 sal.explicit_pc = 1;
7513
7514 sals->sals[0] = sal;
7515 sals->nelts = 1;
7516 }
7517 else
7518 error (_("No default breakpoint address now."));
7519 }
7520 else
7521 {
7522 /* Force almost all breakpoints to be in terms of the
7523 current_source_symtab (which is decode_line_1's default).
7524 This should produce the results we want almost all of the
7525 time while leaving default_breakpoint_* alone.
7526
7527 ObjC: However, don't match an Objective-C method name which
7528 may have a '+' or '-' succeeded by a '[' */
7529
7530 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
7531
7532 if (default_breakpoint_valid
7533 && (!cursal.symtab
7534 || ((strchr ("+-", (*address)[0]) != NULL)
7535 && ((*address)[1] != '['))))
7536 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
7537 default_breakpoint_line, addr_string,
7538 not_found_ptr);
7539 else
7540 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7541 addr_string, not_found_ptr);
7542 }
7543 /* For any SAL that didn't have a canonical string, fill one in. */
7544 if (sals->nelts > 0 && *addr_string == NULL)
7545 *addr_string = xcalloc (sals->nelts, sizeof (char **));
7546 if (addr_start != (*address))
7547 {
7548 int i;
7549
7550 for (i = 0; i < sals->nelts; i++)
7551 {
7552 /* Add the string if not present. */
7553 if ((*addr_string)[i] == NULL)
7554 (*addr_string)[i] = savestring (addr_start,
7555 (*address) - addr_start);
7556 }
7557 }
7558 }
7559
7560
7561 /* Convert each SAL into a real PC. Verify that the PC can be
7562 inserted as a breakpoint. If it can't throw an error. */
7563
7564 static void
7565 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
7566 {
7567 int i;
7568
7569 for (i = 0; i < sals->nelts; i++)
7570 resolve_sal_pc (&sals->sals[i]);
7571 }
7572
7573 /* Fast tracepoints may have restrictions on valid locations. For
7574 instance, a fast tracepoint using a jump instead of a trap will
7575 likely have to overwrite more bytes than a trap would, and so can
7576 only be placed where the instruction is longer than the jump, or a
7577 multi-instruction sequence does not have a jump into the middle of
7578 it, etc. */
7579
7580 static void
7581 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7582 struct symtabs_and_lines *sals)
7583 {
7584 int i, rslt;
7585 struct symtab_and_line *sal;
7586 char *msg;
7587 struct cleanup *old_chain;
7588
7589 for (i = 0; i < sals->nelts; i++)
7590 {
7591 sal = &sals->sals[i];
7592
7593 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7594 NULL, &msg);
7595 old_chain = make_cleanup (xfree, msg);
7596
7597 if (!rslt)
7598 error (_("May not have a fast tracepoint at 0x%s%s"),
7599 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7600
7601 do_cleanups (old_chain);
7602 }
7603 }
7604
7605 static void
7606 do_captured_parse_breakpoint (struct ui_out *ui, void *data)
7607 {
7608 struct captured_parse_breakpoint_args *args = data;
7609
7610 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
7611 args->not_found_ptr);
7612 }
7613
7614 /* Given TOK, a string specification of condition and thread, as
7615 accepted by the 'break' command, extract the condition
7616 string and thread number and set *COND_STRING and *THREAD.
7617 PC identifies the context at which the condition should be parsed.
7618 If no condition is found, *COND_STRING is set to NULL.
7619 If no thread is found, *THREAD is set to -1. */
7620 static void
7621 find_condition_and_thread (char *tok, CORE_ADDR pc,
7622 char **cond_string, int *thread, int *task)
7623 {
7624 *cond_string = NULL;
7625 *thread = -1;
7626 while (tok && *tok)
7627 {
7628 char *end_tok;
7629 int toklen;
7630 char *cond_start = NULL;
7631 char *cond_end = NULL;
7632
7633 tok = skip_spaces (tok);
7634
7635 end_tok = skip_to_space (tok);
7636
7637 toklen = end_tok - tok;
7638
7639 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7640 {
7641 struct expression *expr;
7642
7643 tok = cond_start = end_tok + 1;
7644 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7645 xfree (expr);
7646 cond_end = tok;
7647 *cond_string = savestring (cond_start,
7648 cond_end - cond_start);
7649 }
7650 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7651 {
7652 char *tmptok;
7653
7654 tok = end_tok + 1;
7655 tmptok = tok;
7656 *thread = strtol (tok, &tok, 0);
7657 if (tok == tmptok)
7658 error (_("Junk after thread keyword."));
7659 if (!valid_thread_id (*thread))
7660 error (_("Unknown thread %d."), *thread);
7661 }
7662 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7663 {
7664 char *tmptok;
7665
7666 tok = end_tok + 1;
7667 tmptok = tok;
7668 *task = strtol (tok, &tok, 0);
7669 if (tok == tmptok)
7670 error (_("Junk after task keyword."));
7671 if (!valid_task_id (*task))
7672 error (_("Unknown task %d."), *task);
7673 }
7674 else
7675 error (_("Junk at end of arguments."));
7676 }
7677 }
7678
7679 /* Decode a static tracepoint marker spec. */
7680
7681 static struct symtabs_and_lines
7682 decode_static_tracepoint_spec (char **arg_p)
7683 {
7684 VEC(static_tracepoint_marker_p) *markers = NULL;
7685 struct symtabs_and_lines sals;
7686 struct symtab_and_line sal;
7687 struct symbol *sym;
7688 struct cleanup *old_chain;
7689 char *p = &(*arg_p)[3];
7690 char *endp;
7691 char *marker_str;
7692 int i;
7693
7694 p = skip_spaces (p);
7695
7696 endp = skip_to_space (p);
7697
7698 marker_str = savestring (p, endp - p);
7699 old_chain = make_cleanup (xfree, marker_str);
7700
7701 markers = target_static_tracepoint_markers_by_strid (marker_str);
7702 if (VEC_empty(static_tracepoint_marker_p, markers))
7703 error (_("No known static tracepoint marker named %s"), marker_str);
7704
7705 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7706 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7707
7708 for (i = 0; i < sals.nelts; i++)
7709 {
7710 struct static_tracepoint_marker *marker;
7711
7712 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7713
7714 init_sal (&sals.sals[i]);
7715
7716 sals.sals[i] = find_pc_line (marker->address, 0);
7717 sals.sals[i].pc = marker->address;
7718
7719 release_static_tracepoint_marker (marker);
7720 }
7721
7722 do_cleanups (old_chain);
7723
7724 *arg_p = endp;
7725 return sals;
7726 }
7727
7728 /* Set a breakpoint. This function is shared between CLI and MI
7729 functions for setting a breakpoint. This function has two major
7730 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7731 parameter. If non-zero, the function will parse arg, extracting
7732 breakpoint location, address and thread. Otherwise, ARG is just
7733 the location of breakpoint, with condition and thread specified by
7734 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7735 the breakpoint number will be allocated from the internal
7736 breakpoint count. Returns true if any breakpoint was created;
7737 false otherwise. */
7738
7739 int
7740 create_breakpoint (struct gdbarch *gdbarch,
7741 char *arg, char *cond_string, int thread,
7742 int parse_condition_and_thread,
7743 int tempflag, enum bptype type_wanted,
7744 int ignore_count,
7745 enum auto_boolean pending_break_support,
7746 struct breakpoint_ops *ops,
7747 int from_tty, int enabled, int internal)
7748 {
7749 struct gdb_exception e;
7750 struct symtabs_and_lines sals;
7751 struct symtab_and_line pending_sal;
7752 char *copy_arg;
7753 char *addr_start = arg;
7754 char **addr_string;
7755 struct cleanup *old_chain;
7756 struct cleanup *bkpt_chain = NULL;
7757 struct captured_parse_breakpoint_args parse_args;
7758 int i;
7759 int pending = 0;
7760 int not_found = 0;
7761 int task = 0;
7762 int prev_bkpt_count = breakpoint_count;
7763
7764 sals.sals = NULL;
7765 sals.nelts = 0;
7766 addr_string = NULL;
7767
7768 parse_args.arg_p = &arg;
7769 parse_args.sals_p = &sals;
7770 parse_args.addr_string_p = &addr_string;
7771 parse_args.not_found_ptr = &not_found;
7772
7773 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7774 {
7775 int i;
7776
7777 sals = decode_static_tracepoint_spec (&arg);
7778
7779 copy_arg = savestring (addr_start, arg - addr_start);
7780 addr_string = xcalloc (sals.nelts, sizeof (char **));
7781 for (i = 0; i < sals.nelts; i++)
7782 addr_string[i] = xstrdup (copy_arg);
7783 goto done;
7784 }
7785
7786 e = catch_exception (uiout, do_captured_parse_breakpoint,
7787 &parse_args, RETURN_MASK_ALL);
7788
7789 /* If caller is interested in rc value from parse, set value. */
7790 switch (e.reason)
7791 {
7792 case RETURN_QUIT:
7793 throw_exception (e);
7794 case RETURN_ERROR:
7795 switch (e.error)
7796 {
7797 case NOT_FOUND_ERROR:
7798
7799 /* If pending breakpoint support is turned off, throw
7800 error. */
7801
7802 if (pending_break_support == AUTO_BOOLEAN_FALSE)
7803 throw_exception (e);
7804
7805 exception_print (gdb_stderr, e);
7806
7807 /* If pending breakpoint support is auto query and the user
7808 selects no, then simply return the error code. */
7809 if (pending_break_support == AUTO_BOOLEAN_AUTO
7810 && !nquery (_("Make breakpoint pending on "
7811 "future shared library load? ")))
7812 return 0;
7813
7814 /* At this point, either the user was queried about setting
7815 a pending breakpoint and selected yes, or pending
7816 breakpoint behavior is on and thus a pending breakpoint
7817 is defaulted on behalf of the user. */
7818 copy_arg = xstrdup (addr_start);
7819 addr_string = &copy_arg;
7820 sals.nelts = 1;
7821 sals.sals = &pending_sal;
7822 pending_sal.pc = 0;
7823 pending = 1;
7824 break;
7825 default:
7826 throw_exception (e);
7827 }
7828 default:
7829 if (!sals.nelts)
7830 return 0;
7831 }
7832
7833 done:
7834
7835 /* Create a chain of things that always need to be cleaned up. */
7836 old_chain = make_cleanup (null_cleanup, 0);
7837
7838 if (!pending)
7839 {
7840 /* Make sure that all storage allocated to SALS gets freed. */
7841 make_cleanup (xfree, sals.sals);
7842
7843 /* Cleanup the addr_string array but not its contents. */
7844 make_cleanup (xfree, addr_string);
7845 }
7846
7847 /* ----------------------------- SNIP -----------------------------
7848 Anything added to the cleanup chain beyond this point is assumed
7849 to be part of a breakpoint. If the breakpoint create succeeds
7850 then the memory is not reclaimed. */
7851 bkpt_chain = make_cleanup (null_cleanup, 0);
7852
7853 /* Mark the contents of the addr_string for cleanup. These go on
7854 the bkpt_chain and only occur if the breakpoint create fails. */
7855 for (i = 0; i < sals.nelts; i++)
7856 {
7857 if (addr_string[i] != NULL)
7858 make_cleanup (xfree, addr_string[i]);
7859 }
7860
7861 /* Resolve all line numbers to PC's and verify that the addresses
7862 are ok for the target. */
7863 if (!pending)
7864 breakpoint_sals_to_pc (&sals);
7865
7866 /* Fast tracepoints may have additional restrictions on location. */
7867 if (type_wanted == bp_fast_tracepoint)
7868 check_fast_tracepoint_sals (gdbarch, &sals);
7869
7870 /* Verify that condition can be parsed, before setting any
7871 breakpoints. Allocate a separate condition expression for each
7872 breakpoint. */
7873 if (!pending)
7874 {
7875 if (parse_condition_and_thread)
7876 {
7877 /* Here we only parse 'arg' to separate condition
7878 from thread number, so parsing in context of first
7879 sal is OK. When setting the breakpoint we'll
7880 re-parse it in context of each sal. */
7881 cond_string = NULL;
7882 thread = -1;
7883 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7884 &thread, &task);
7885 if (cond_string)
7886 make_cleanup (xfree, cond_string);
7887 }
7888 else
7889 {
7890 /* Create a private copy of condition string. */
7891 if (cond_string)
7892 {
7893 cond_string = xstrdup (cond_string);
7894 make_cleanup (xfree, cond_string);
7895 }
7896 }
7897
7898 /* If the user is creating a static tracepoint by marker id
7899 (strace -m MARKER_ID), then store the sals index, so that
7900 breakpoint_re_set can try to match up which of the newly
7901 found markers corresponds to this one, and, don't try to
7902 expand multiple locations for each sal, given than SALS
7903 already should contain all sals for MARKER_ID. */
7904 if (type_wanted == bp_static_tracepoint
7905 && is_marker_spec (addr_string[0]))
7906 {
7907 int i;
7908
7909 for (i = 0; i < sals.nelts; ++i)
7910 {
7911 struct symtabs_and_lines expanded;
7912 struct breakpoint *tp;
7913 struct cleanup *old_chain;
7914
7915 expanded.nelts = 1;
7916 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7917 expanded.sals[0] = sals.sals[i];
7918 old_chain = make_cleanup (xfree, expanded.sals);
7919
7920 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7921 cond_string, type_wanted,
7922 tempflag ? disp_del : disp_donttouch,
7923 thread, task, ignore_count, ops,
7924 from_tty, enabled, internal);
7925
7926 do_cleanups (old_chain);
7927
7928 /* Get the tracepoint we just created. */
7929 if (internal)
7930 tp = get_breakpoint (internal_breakpoint_number);
7931 else
7932 tp = get_breakpoint (breakpoint_count);
7933 gdb_assert (tp != NULL);
7934
7935 /* Given that its possible to have multiple markers with
7936 the same string id, if the user is creating a static
7937 tracepoint by marker id ("strace -m MARKER_ID"), then
7938 store the sals index, so that breakpoint_re_set can
7939 try to match up which of the newly found markers
7940 corresponds to this one */
7941 tp->static_trace_marker_id_idx = i;
7942 }
7943 }
7944 else
7945 create_breakpoints_sal (gdbarch, sals, addr_string, cond_string,
7946 type_wanted,
7947 tempflag ? disp_del : disp_donttouch,
7948 thread, task, ignore_count, ops, from_tty,
7949 enabled, internal);
7950 }
7951 else
7952 {
7953 struct breakpoint *b;
7954
7955 make_cleanup (xfree, copy_arg);
7956
7957 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
7958 set_breakpoint_number (internal, b);
7959 b->thread = -1;
7960 b->addr_string = addr_string[0];
7961 b->cond_string = NULL;
7962 b->ignore_count = ignore_count;
7963 b->disposition = tempflag ? disp_del : disp_donttouch;
7964 b->condition_not_parsed = 1;
7965 b->ops = ops;
7966 b->enable_state = enabled ? bp_enabled : bp_disabled;
7967 b->pspace = current_program_space;
7968 b->py_bp_object = NULL;
7969
7970 if (enabled && b->pspace->executing_startup
7971 && (b->type == bp_breakpoint
7972 || b->type == bp_hardware_breakpoint))
7973 b->enable_state = bp_startup_disabled;
7974
7975 if (internal)
7976 /* Do not mention breakpoints with a negative number,
7977 but do notify observers. */
7978 observer_notify_breakpoint_created (b->number);
7979 else
7980 mention (b);
7981 }
7982
7983 if (sals.nelts > 1)
7984 {
7985 warning (_("Multiple breakpoints were set.\nUse the "
7986 "\"delete\" command to delete unwanted breakpoints."));
7987 prev_breakpoint_count = prev_bkpt_count;
7988 }
7989
7990 /* That's it. Discard the cleanups for data inserted into the
7991 breakpoint. */
7992 discard_cleanups (bkpt_chain);
7993 /* But cleanup everything else. */
7994 do_cleanups (old_chain);
7995
7996 /* error call may happen here - have BKPT_CHAIN already discarded. */
7997 update_global_location_list (1);
7998
7999 return 1;
8000 }
8001
8002 /* Set a breakpoint.
8003 ARG is a string describing breakpoint address,
8004 condition, and thread.
8005 FLAG specifies if a breakpoint is hardware on,
8006 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8007 and BP_TEMPFLAG. */
8008
8009 static void
8010 break_command_1 (char *arg, int flag, int from_tty)
8011 {
8012 int tempflag = flag & BP_TEMPFLAG;
8013 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8014 ? bp_hardware_breakpoint
8015 : bp_breakpoint);
8016
8017 create_breakpoint (get_current_arch (),
8018 arg,
8019 NULL, 0, 1 /* parse arg */,
8020 tempflag, type_wanted,
8021 0 /* Ignore count */,
8022 pending_break_support,
8023 NULL /* breakpoint_ops */,
8024 from_tty,
8025 1 /* enabled */,
8026 0 /* internal */);
8027 }
8028
8029
8030 /* Helper function for break_command_1 and disassemble_command. */
8031
8032 void
8033 resolve_sal_pc (struct symtab_and_line *sal)
8034 {
8035 CORE_ADDR pc;
8036
8037 if (sal->pc == 0 && sal->symtab != NULL)
8038 {
8039 if (!find_line_pc (sal->symtab, sal->line, &pc))
8040 error (_("No line %d in file \"%s\"."),
8041 sal->line, sal->symtab->filename);
8042 sal->pc = pc;
8043
8044 /* If this SAL corresponds to a breakpoint inserted using a line
8045 number, then skip the function prologue if necessary. */
8046 if (sal->explicit_line)
8047 skip_prologue_sal (sal);
8048 }
8049
8050 if (sal->section == 0 && sal->symtab != NULL)
8051 {
8052 struct blockvector *bv;
8053 struct block *b;
8054 struct symbol *sym;
8055
8056 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
8057 if (bv != NULL)
8058 {
8059 sym = block_linkage_function (b);
8060 if (sym != NULL)
8061 {
8062 fixup_symbol_section (sym, sal->symtab->objfile);
8063 sal->section = SYMBOL_OBJ_SECTION (sym);
8064 }
8065 else
8066 {
8067 /* It really is worthwhile to have the section, so we'll
8068 just have to look harder. This case can be executed
8069 if we have line numbers but no functions (as can
8070 happen in assembly source). */
8071
8072 struct minimal_symbol *msym;
8073 struct cleanup *old_chain = save_current_space_and_thread ();
8074
8075 switch_to_program_space_and_thread (sal->pspace);
8076
8077 msym = lookup_minimal_symbol_by_pc (sal->pc);
8078 if (msym)
8079 sal->section = SYMBOL_OBJ_SECTION (msym);
8080
8081 do_cleanups (old_chain);
8082 }
8083 }
8084 }
8085 }
8086
8087 void
8088 break_command (char *arg, int from_tty)
8089 {
8090 break_command_1 (arg, 0, from_tty);
8091 }
8092
8093 void
8094 tbreak_command (char *arg, int from_tty)
8095 {
8096 break_command_1 (arg, BP_TEMPFLAG, from_tty);
8097 }
8098
8099 static void
8100 hbreak_command (char *arg, int from_tty)
8101 {
8102 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
8103 }
8104
8105 static void
8106 thbreak_command (char *arg, int from_tty)
8107 {
8108 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
8109 }
8110
8111 static void
8112 stop_command (char *arg, int from_tty)
8113 {
8114 printf_filtered (_("Specify the type of breakpoint to set.\n\
8115 Usage: stop in <function | address>\n\
8116 stop at <line>\n"));
8117 }
8118
8119 static void
8120 stopin_command (char *arg, int from_tty)
8121 {
8122 int badInput = 0;
8123
8124 if (arg == (char *) NULL)
8125 badInput = 1;
8126 else if (*arg != '*')
8127 {
8128 char *argptr = arg;
8129 int hasColon = 0;
8130
8131 /* Look for a ':'. If this is a line number specification, then
8132 say it is bad, otherwise, it should be an address or
8133 function/method name. */
8134 while (*argptr && !hasColon)
8135 {
8136 hasColon = (*argptr == ':');
8137 argptr++;
8138 }
8139
8140 if (hasColon)
8141 badInput = (*argptr != ':'); /* Not a class::method */
8142 else
8143 badInput = isdigit (*arg); /* a simple line number */
8144 }
8145
8146 if (badInput)
8147 printf_filtered (_("Usage: stop in <function | address>\n"));
8148 else
8149 break_command_1 (arg, 0, from_tty);
8150 }
8151
8152 static void
8153 stopat_command (char *arg, int from_tty)
8154 {
8155 int badInput = 0;
8156
8157 if (arg == (char *) NULL || *arg == '*') /* no line number */
8158 badInput = 1;
8159 else
8160 {
8161 char *argptr = arg;
8162 int hasColon = 0;
8163
8164 /* Look for a ':'. If there is a '::' then get out, otherwise
8165 it is probably a line number. */
8166 while (*argptr && !hasColon)
8167 {
8168 hasColon = (*argptr == ':');
8169 argptr++;
8170 }
8171
8172 if (hasColon)
8173 badInput = (*argptr == ':'); /* we have class::method */
8174 else
8175 badInput = !isdigit (*arg); /* not a line number */
8176 }
8177
8178 if (badInput)
8179 printf_filtered (_("Usage: stop at <line>\n"));
8180 else
8181 break_command_1 (arg, 0, from_tty);
8182 }
8183
8184 /* Return non-zero if EXP is verified as constant. Returned zero
8185 means EXP is variable. Also the constant detection may fail for
8186 some constant expressions and in such case still falsely return
8187 zero. */
8188 static int
8189 watchpoint_exp_is_const (const struct expression *exp)
8190 {
8191 int i = exp->nelts;
8192
8193 while (i > 0)
8194 {
8195 int oplenp, argsp;
8196
8197 /* We are only interested in the descriptor of each element. */
8198 operator_length (exp, i, &oplenp, &argsp);
8199 i -= oplenp;
8200
8201 switch (exp->elts[i].opcode)
8202 {
8203 case BINOP_ADD:
8204 case BINOP_SUB:
8205 case BINOP_MUL:
8206 case BINOP_DIV:
8207 case BINOP_REM:
8208 case BINOP_MOD:
8209 case BINOP_LSH:
8210 case BINOP_RSH:
8211 case BINOP_LOGICAL_AND:
8212 case BINOP_LOGICAL_OR:
8213 case BINOP_BITWISE_AND:
8214 case BINOP_BITWISE_IOR:
8215 case BINOP_BITWISE_XOR:
8216 case BINOP_EQUAL:
8217 case BINOP_NOTEQUAL:
8218 case BINOP_LESS:
8219 case BINOP_GTR:
8220 case BINOP_LEQ:
8221 case BINOP_GEQ:
8222 case BINOP_REPEAT:
8223 case BINOP_COMMA:
8224 case BINOP_EXP:
8225 case BINOP_MIN:
8226 case BINOP_MAX:
8227 case BINOP_INTDIV:
8228 case BINOP_CONCAT:
8229 case BINOP_IN:
8230 case BINOP_RANGE:
8231 case TERNOP_COND:
8232 case TERNOP_SLICE:
8233 case TERNOP_SLICE_COUNT:
8234
8235 case OP_LONG:
8236 case OP_DOUBLE:
8237 case OP_DECFLOAT:
8238 case OP_LAST:
8239 case OP_COMPLEX:
8240 case OP_STRING:
8241 case OP_BITSTRING:
8242 case OP_ARRAY:
8243 case OP_TYPE:
8244 case OP_NAME:
8245 case OP_OBJC_NSSTRING:
8246
8247 case UNOP_NEG:
8248 case UNOP_LOGICAL_NOT:
8249 case UNOP_COMPLEMENT:
8250 case UNOP_ADDR:
8251 case UNOP_HIGH:
8252 /* Unary, binary and ternary operators: We have to check
8253 their operands. If they are constant, then so is the
8254 result of that operation. For instance, if A and B are
8255 determined to be constants, then so is "A + B".
8256
8257 UNOP_IND is one exception to the rule above, because the
8258 value of *ADDR is not necessarily a constant, even when
8259 ADDR is. */
8260 break;
8261
8262 case OP_VAR_VALUE:
8263 /* Check whether the associated symbol is a constant.
8264
8265 We use SYMBOL_CLASS rather than TYPE_CONST because it's
8266 possible that a buggy compiler could mark a variable as
8267 constant even when it is not, and TYPE_CONST would return
8268 true in this case, while SYMBOL_CLASS wouldn't.
8269
8270 We also have to check for function symbols because they
8271 are always constant. */
8272 {
8273 struct symbol *s = exp->elts[i + 2].symbol;
8274
8275 if (SYMBOL_CLASS (s) != LOC_BLOCK
8276 && SYMBOL_CLASS (s) != LOC_CONST
8277 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8278 return 0;
8279 break;
8280 }
8281
8282 /* The default action is to return 0 because we are using
8283 the optimistic approach here: If we don't know something,
8284 then it is not a constant. */
8285 default:
8286 return 0;
8287 }
8288 }
8289
8290 return 1;
8291 }
8292
8293 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8294
8295 static int
8296 insert_watchpoint (struct bp_location *bl)
8297 {
8298 int length = bl->owner->exact? 1 : bl->length;
8299
8300 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8301 bl->owner->cond_exp);
8302 }
8303
8304 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8305
8306 static int
8307 remove_watchpoint (struct bp_location *bl)
8308 {
8309 int length = bl->owner->exact? 1 : bl->length;
8310
8311 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8312 bl->owner->cond_exp);
8313 }
8314
8315 /* Implement the "resources_needed" breakpoint_ops method for
8316 hardware watchpoints. */
8317
8318 static int
8319 resources_needed_watchpoint (const struct bp_location *bl)
8320 {
8321 int length = bl->owner->exact? 1 : bl->length;
8322
8323 return target_region_ok_for_hw_watchpoint (bl->address, length);
8324 }
8325
8326 /* The breakpoint_ops structure to be used in hardware watchpoints. */
8327
8328 static struct breakpoint_ops watchpoint_breakpoint_ops =
8329 {
8330 insert_watchpoint,
8331 remove_watchpoint,
8332 NULL, /* breakpoint_hit */
8333 resources_needed_watchpoint,
8334 NULL, /* print_it */
8335 NULL, /* print_one */
8336 NULL, /* print_mention */
8337 NULL /* print_recreate */
8338 };
8339
8340 /* accessflag: hw_write: watch write,
8341 hw_read: watch read,
8342 hw_access: watch access (read or write) */
8343 static void
8344 watch_command_1 (char *arg, int accessflag, int from_tty,
8345 int just_location, int internal)
8346 {
8347 struct breakpoint *b, *scope_breakpoint = NULL;
8348 struct expression *exp;
8349 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
8350 struct value *val, *mark, *result;
8351 struct frame_info *frame;
8352 char *exp_start = NULL;
8353 char *exp_end = NULL;
8354 char *tok, *id_tok_start, *end_tok;
8355 int toklen;
8356 char *cond_start = NULL;
8357 char *cond_end = NULL;
8358 int i, other_type_used, target_resources_ok = 0;
8359 enum bptype bp_type;
8360 int reg_cnt = 0;
8361 int thread = -1;
8362 int pc = 0;
8363
8364 /* Make sure that we actually have parameters to parse. */
8365 if (arg != NULL && arg[0] != '\0')
8366 {
8367 toklen = strlen (arg); /* Size of argument list. */
8368
8369 /* Points tok to the end of the argument list. */
8370 tok = arg + toklen - 1;
8371
8372 /* Go backwards in the parameters list. Skip the last
8373 parameter. If we're expecting a 'thread <thread_num>'
8374 parameter, this should be the thread identifier. */
8375 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8376 tok--;
8377 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8378 tok--;
8379
8380 /* Points end_tok to the beginning of the last token. */
8381 id_tok_start = tok + 1;
8382
8383 /* Go backwards in the parameters list. Skip one more
8384 parameter. If we're expecting a 'thread <thread_num>'
8385 parameter, we should reach a "thread" token. */
8386 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8387 tok--;
8388
8389 end_tok = tok;
8390
8391 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8392 tok--;
8393
8394 /* Move the pointer forward to skip the whitespace and
8395 calculate the length of the token. */
8396 tok++;
8397 toklen = end_tok - tok;
8398
8399 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8400 {
8401 /* At this point we've found a "thread" token, which means
8402 the user is trying to set a watchpoint that triggers
8403 only in a specific thread. */
8404 char *endp;
8405
8406 /* Extract the thread ID from the next token. */
8407 thread = strtol (id_tok_start, &endp, 0);
8408
8409 /* Check if the user provided a valid numeric value for the
8410 thread ID. */
8411 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8412 error (_("Invalid thread ID specification %s."), id_tok_start);
8413
8414 /* Check if the thread actually exists. */
8415 if (!valid_thread_id (thread))
8416 error (_("Unknown thread %d."), thread);
8417
8418 /* Truncate the string and get rid of the thread <thread_num>
8419 parameter before the parameter list is parsed by the
8420 evaluate_expression() function. */
8421 *tok = '\0';
8422 }
8423 }
8424
8425 /* Parse the rest of the arguments. */
8426 innermost_block = NULL;
8427 exp_start = arg;
8428 exp = parse_exp_1 (&arg, 0, 0);
8429 exp_end = arg;
8430 /* Remove trailing whitespace from the expression before saving it.
8431 This makes the eventual display of the expression string a bit
8432 prettier. */
8433 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8434 --exp_end;
8435
8436 /* Checking if the expression is not constant. */
8437 if (watchpoint_exp_is_const (exp))
8438 {
8439 int len;
8440
8441 len = exp_end - exp_start;
8442 while (len > 0 && isspace (exp_start[len - 1]))
8443 len--;
8444 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8445 }
8446
8447 exp_valid_block = innermost_block;
8448 mark = value_mark ();
8449 fetch_subexp_value (exp, &pc, &val, &result, NULL);
8450
8451 if (just_location)
8452 {
8453 exp_valid_block = NULL;
8454 val = value_addr (result);
8455 release_value (val);
8456 value_free_to_mark (mark);
8457 }
8458 else if (val != NULL)
8459 release_value (val);
8460
8461 tok = skip_spaces (arg);
8462 end_tok = skip_to_space (tok);
8463
8464 toklen = end_tok - tok;
8465 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8466 {
8467 struct expression *cond;
8468
8469 innermost_block = NULL;
8470 tok = cond_start = end_tok + 1;
8471 cond = parse_exp_1 (&tok, 0, 0);
8472
8473 /* The watchpoint expression may not be local, but the condition
8474 may still be. E.g.: `watch global if local > 0'. */
8475 cond_exp_valid_block = innermost_block;
8476
8477 xfree (cond);
8478 cond_end = tok;
8479 }
8480 if (*tok)
8481 error (_("Junk at end of command."));
8482
8483 if (accessflag == hw_read)
8484 bp_type = bp_read_watchpoint;
8485 else if (accessflag == hw_access)
8486 bp_type = bp_access_watchpoint;
8487 else
8488 bp_type = bp_hardware_watchpoint;
8489
8490 reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8491 if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8492 error (_("Expression cannot be implemented with read/access watchpoint."));
8493 if (reg_cnt != 0)
8494 {
8495 i = hw_watchpoint_used_count (bp_type, &other_type_used);
8496 target_resources_ok =
8497 target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
8498 other_type_used);
8499 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8500 error (_("Target does not support this type of hardware watchpoint."));
8501
8502 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8503 error (_("Target can only support one kind "
8504 "of HW watchpoint at a time."));
8505 }
8506
8507 /* Change the type of breakpoint to an ordinary watchpoint if a
8508 hardware watchpoint could not be set. */
8509 if (!reg_cnt || target_resources_ok <= 0)
8510 bp_type = bp_watchpoint;
8511
8512 frame = block_innermost_frame (exp_valid_block);
8513
8514 /* If the expression is "local", then set up a "watchpoint scope"
8515 breakpoint at the point where we've left the scope of the watchpoint
8516 expression. Create the scope breakpoint before the watchpoint, so
8517 that we will encounter it first in bpstat_stop_status. */
8518 if (exp_valid_block && frame)
8519 {
8520 if (frame_id_p (frame_unwind_caller_id (frame)))
8521 {
8522 scope_breakpoint
8523 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8524 frame_unwind_caller_pc (frame),
8525 bp_watchpoint_scope);
8526
8527 scope_breakpoint->enable_state = bp_enabled;
8528
8529 /* Automatically delete the breakpoint when it hits. */
8530 scope_breakpoint->disposition = disp_del;
8531
8532 /* Only break in the proper frame (help with recursion). */
8533 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
8534
8535 /* Set the address at which we will stop. */
8536 scope_breakpoint->loc->gdbarch
8537 = frame_unwind_caller_arch (frame);
8538 scope_breakpoint->loc->requested_address
8539 = frame_unwind_caller_pc (frame);
8540 scope_breakpoint->loc->address
8541 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8542 scope_breakpoint->loc->requested_address,
8543 scope_breakpoint->type);
8544 }
8545 }
8546
8547 /* Now set up the breakpoint. */
8548 b = set_raw_breakpoint_without_location (NULL, bp_type);
8549 set_breakpoint_number (internal, b);
8550 b->thread = thread;
8551 b->disposition = disp_donttouch;
8552 b->exp = exp;
8553 b->exp_valid_block = exp_valid_block;
8554 b->cond_exp_valid_block = cond_exp_valid_block;
8555 if (just_location)
8556 {
8557 struct type *t = value_type (val);
8558 CORE_ADDR addr = value_as_address (val);
8559 char *name;
8560
8561 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
8562 name = type_to_string (t);
8563
8564 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
8565 core_addr_to_string (addr));
8566 xfree (name);
8567
8568 b->exp_string = xstrprintf ("-location: %.*s",
8569 (int) (exp_end - exp_start), exp_start);
8570
8571 /* The above expression is in C. */
8572 b->language = language_c;
8573 }
8574 else
8575 b->exp_string = savestring (exp_start, exp_end - exp_start);
8576 b->val = val;
8577 b->val_valid = 1;
8578 b->ops = &watchpoint_breakpoint_ops;
8579
8580 /* Use an exact watchpoint when there's only one memory region to be
8581 watched, and only one debug register is needed to watch it. */
8582 b->exact = target_exact_watchpoints && reg_cnt == 1;
8583
8584 if (cond_start)
8585 b->cond_string = savestring (cond_start, cond_end - cond_start);
8586 else
8587 b->cond_string = 0;
8588
8589 if (frame)
8590 {
8591 b->watchpoint_frame = get_frame_id (frame);
8592 b->watchpoint_thread = inferior_ptid;
8593 }
8594 else
8595 {
8596 b->watchpoint_frame = null_frame_id;
8597 b->watchpoint_thread = null_ptid;
8598 }
8599
8600 if (scope_breakpoint != NULL)
8601 {
8602 /* The scope breakpoint is related to the watchpoint. We will
8603 need to act on them together. */
8604 b->related_breakpoint = scope_breakpoint;
8605 scope_breakpoint->related_breakpoint = b;
8606 }
8607
8608 if (!just_location)
8609 value_free_to_mark (mark);
8610
8611 /* Finally update the new watchpoint. This creates the locations
8612 that should be inserted. */
8613 update_watchpoint (b, 1);
8614 if (internal)
8615 /* Do not mention breakpoints with a negative number, but do
8616 notify observers. */
8617 observer_notify_breakpoint_created (b->number);
8618 else
8619 mention (b);
8620 update_global_location_list (1);
8621 }
8622
8623 /* Return count of debug registers needed to watch the given expression.
8624 If EXACT_WATCHPOINTS is 1, then consider that only the address of
8625 the start of the watched region will be monitored (i.e., all accesses
8626 will be aligned). This uses less debug registers on some targets.
8627
8628 If the watchpoint cannot be handled in hardware return zero. */
8629
8630 static int
8631 can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
8632 {
8633 int found_memory_cnt = 0;
8634 struct value *head = v;
8635
8636 /* Did the user specifically forbid us to use hardware watchpoints? */
8637 if (!can_use_hw_watchpoints)
8638 return 0;
8639
8640 /* Make sure that the value of the expression depends only upon
8641 memory contents, and values computed from them within GDB. If we
8642 find any register references or function calls, we can't use a
8643 hardware watchpoint.
8644
8645 The idea here is that evaluating an expression generates a series
8646 of values, one holding the value of every subexpression. (The
8647 expression a*b+c has five subexpressions: a, b, a*b, c, and
8648 a*b+c.) GDB's values hold almost enough information to establish
8649 the criteria given above --- they identify memory lvalues,
8650 register lvalues, computed values, etcetera. So we can evaluate
8651 the expression, and then scan the chain of values that leaves
8652 behind to decide whether we can detect any possible change to the
8653 expression's final value using only hardware watchpoints.
8654
8655 However, I don't think that the values returned by inferior
8656 function calls are special in any way. So this function may not
8657 notice that an expression involving an inferior function call
8658 can't be watched with hardware watchpoints. FIXME. */
8659 for (; v; v = value_next (v))
8660 {
8661 if (VALUE_LVAL (v) == lval_memory)
8662 {
8663 if (v != head && value_lazy (v))
8664 /* A lazy memory lvalue in the chain is one that GDB never
8665 needed to fetch; we either just used its address (e.g.,
8666 `a' in `a.b') or we never needed it at all (e.g., `a'
8667 in `a,b'). This doesn't apply to HEAD; if that is
8668 lazy then it was not readable, but watch it anyway. */
8669 ;
8670 else
8671 {
8672 /* Ahh, memory we actually used! Check if we can cover
8673 it with hardware watchpoints. */
8674 struct type *vtype = check_typedef (value_type (v));
8675
8676 /* We only watch structs and arrays if user asked for it
8677 explicitly, never if they just happen to appear in a
8678 middle of some value chain. */
8679 if (v == head
8680 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
8681 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
8682 {
8683 CORE_ADDR vaddr = value_address (v);
8684 int len;
8685 int num_regs;
8686
8687 len = (exact_watchpoints
8688 && is_scalar_type_recursive (vtype))?
8689 1 : TYPE_LENGTH (value_type (v));
8690
8691 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
8692 if (!num_regs)
8693 return 0;
8694 else
8695 found_memory_cnt += num_regs;
8696 }
8697 }
8698 }
8699 else if (VALUE_LVAL (v) != not_lval
8700 && deprecated_value_modifiable (v) == 0)
8701 return 0; /* These are values from the history (e.g., $1). */
8702 else if (VALUE_LVAL (v) == lval_register)
8703 return 0; /* Cannot watch a register with a HW watchpoint. */
8704 }
8705
8706 /* The expression itself looks suitable for using a hardware
8707 watchpoint, but give the target machine a chance to reject it. */
8708 return found_memory_cnt;
8709 }
8710
8711 void
8712 watch_command_wrapper (char *arg, int from_tty, int internal)
8713 {
8714 watch_command_1 (arg, hw_write, from_tty, 0, internal);
8715 }
8716
8717 /* A helper function that looks for an argument at the start of a
8718 string. The argument must also either be at the end of the string,
8719 or be followed by whitespace. Returns 1 if it finds the argument,
8720 0 otherwise. If the argument is found, it updates *STR. */
8721
8722 static int
8723 check_for_argument (char **str, char *arg, int arg_len)
8724 {
8725 if (strncmp (*str, arg, arg_len) == 0
8726 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
8727 {
8728 *str += arg_len;
8729 return 1;
8730 }
8731 return 0;
8732 }
8733
8734 /* A helper function that looks for the "-location" argument and then
8735 calls watch_command_1. */
8736
8737 static void
8738 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
8739 {
8740 int just_location = 0;
8741
8742 if (arg
8743 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
8744 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
8745 {
8746 arg = skip_spaces (arg);
8747 just_location = 1;
8748 }
8749
8750 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8751 }
8752
8753 static void
8754 watch_command (char *arg, int from_tty)
8755 {
8756 watch_maybe_just_location (arg, hw_write, from_tty);
8757 }
8758
8759 void
8760 rwatch_command_wrapper (char *arg, int from_tty, int internal)
8761 {
8762 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8763 }
8764
8765 static void
8766 rwatch_command (char *arg, int from_tty)
8767 {
8768 watch_maybe_just_location (arg, hw_read, from_tty);
8769 }
8770
8771 void
8772 awatch_command_wrapper (char *arg, int from_tty, int internal)
8773 {
8774 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8775 }
8776
8777 static void
8778 awatch_command (char *arg, int from_tty)
8779 {
8780 watch_maybe_just_location (arg, hw_access, from_tty);
8781 }
8782 \f
8783
8784 /* Helper routines for the until_command routine in infcmd.c. Here
8785 because it uses the mechanisms of breakpoints. */
8786
8787 struct until_break_command_continuation_args
8788 {
8789 struct breakpoint *breakpoint;
8790 struct breakpoint *breakpoint2;
8791 int thread_num;
8792 };
8793
8794 /* This function is called by fetch_inferior_event via the
8795 cmd_continuation pointer, to complete the until command. It takes
8796 care of cleaning up the temporary breakpoints set up by the until
8797 command. */
8798 static void
8799 until_break_command_continuation (void *arg)
8800 {
8801 struct until_break_command_continuation_args *a = arg;
8802
8803 delete_breakpoint (a->breakpoint);
8804 if (a->breakpoint2)
8805 delete_breakpoint (a->breakpoint2);
8806 delete_longjmp_breakpoint (a->thread_num);
8807 }
8808
8809 void
8810 until_break_command (char *arg, int from_tty, int anywhere)
8811 {
8812 struct symtabs_and_lines sals;
8813 struct symtab_and_line sal;
8814 struct frame_info *frame = get_selected_frame (NULL);
8815 struct breakpoint *breakpoint;
8816 struct breakpoint *breakpoint2 = NULL;
8817 struct cleanup *old_chain;
8818 int thread;
8819 struct thread_info *tp;
8820
8821 clear_proceed_status ();
8822
8823 /* Set a breakpoint where the user wants it and at return from
8824 this function. */
8825
8826 if (default_breakpoint_valid)
8827 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
8828 default_breakpoint_line, (char ***) NULL, NULL);
8829 else
8830 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
8831 0, (char ***) NULL, NULL);
8832
8833 if (sals.nelts != 1)
8834 error (_("Couldn't get information on specified line."));
8835
8836 sal = sals.sals[0];
8837 xfree (sals.sals); /* malloc'd, so freed. */
8838
8839 if (*arg)
8840 error (_("Junk at end of arguments."));
8841
8842 resolve_sal_pc (&sal);
8843
8844 if (anywhere)
8845 /* If the user told us to continue until a specified location,
8846 we don't specify a frame at which we need to stop. */
8847 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8848 null_frame_id, bp_until);
8849 else
8850 /* Otherwise, specify the selected frame, because we want to stop
8851 only at the very same frame. */
8852 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8853 get_stack_frame_id (frame),
8854 bp_until);
8855
8856 old_chain = make_cleanup_delete_breakpoint (breakpoint);
8857
8858 tp = inferior_thread ();
8859 thread = tp->num;
8860
8861 /* Keep within the current frame, or in frames called by the current
8862 one. */
8863
8864 if (frame_id_p (frame_unwind_caller_id (frame)))
8865 {
8866 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
8867 sal.pc = frame_unwind_caller_pc (frame);
8868 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
8869 sal,
8870 frame_unwind_caller_id (frame),
8871 bp_until);
8872 make_cleanup_delete_breakpoint (breakpoint2);
8873
8874 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
8875 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
8876 }
8877
8878 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
8879
8880 /* If we are running asynchronously, and proceed call above has
8881 actually managed to start the target, arrange for breakpoints to
8882 be deleted when the target stops. Otherwise, we're already
8883 stopped and delete breakpoints via cleanup chain. */
8884
8885 if (target_can_async_p () && is_running (inferior_ptid))
8886 {
8887 struct until_break_command_continuation_args *args;
8888 args = xmalloc (sizeof (*args));
8889
8890 args->breakpoint = breakpoint;
8891 args->breakpoint2 = breakpoint2;
8892 args->thread_num = thread;
8893
8894 discard_cleanups (old_chain);
8895 add_continuation (inferior_thread (),
8896 until_break_command_continuation, args,
8897 xfree);
8898 }
8899 else
8900 do_cleanups (old_chain);
8901 }
8902
8903 /* This function attempts to parse an optional "if <cond>" clause
8904 from the arg string. If one is not found, it returns NULL.
8905
8906 Else, it returns a pointer to the condition string. (It does not
8907 attempt to evaluate the string against a particular block.) And,
8908 it updates arg to point to the first character following the parsed
8909 if clause in the arg string. */
8910
8911 static char *
8912 ep_parse_optional_if_clause (char **arg)
8913 {
8914 char *cond_string;
8915
8916 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
8917 return NULL;
8918
8919 /* Skip the "if" keyword. */
8920 (*arg) += 2;
8921
8922 /* Skip any extra leading whitespace, and record the start of the
8923 condition string. */
8924 *arg = skip_spaces (*arg);
8925 cond_string = *arg;
8926
8927 /* Assume that the condition occupies the remainder of the arg
8928 string. */
8929 (*arg) += strlen (cond_string);
8930
8931 return cond_string;
8932 }
8933
8934 /* Commands to deal with catching events, such as signals, exceptions,
8935 process start/exit, etc. */
8936
8937 typedef enum
8938 {
8939 catch_fork_temporary, catch_vfork_temporary,
8940 catch_fork_permanent, catch_vfork_permanent
8941 }
8942 catch_fork_kind;
8943
8944 static void
8945 catch_fork_command_1 (char *arg, int from_tty,
8946 struct cmd_list_element *command)
8947 {
8948 struct gdbarch *gdbarch = get_current_arch ();
8949 char *cond_string = NULL;
8950 catch_fork_kind fork_kind;
8951 int tempflag;
8952
8953 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
8954 tempflag = (fork_kind == catch_fork_temporary
8955 || fork_kind == catch_vfork_temporary);
8956
8957 if (!arg)
8958 arg = "";
8959 arg = skip_spaces (arg);
8960
8961 /* The allowed syntax is:
8962 catch [v]fork
8963 catch [v]fork if <cond>
8964
8965 First, check if there's an if clause. */
8966 cond_string = ep_parse_optional_if_clause (&arg);
8967
8968 if ((*arg != '\0') && !isspace (*arg))
8969 error (_("Junk at end of arguments."));
8970
8971 /* If this target supports it, create a fork or vfork catchpoint
8972 and enable reporting of such events. */
8973 switch (fork_kind)
8974 {
8975 case catch_fork_temporary:
8976 case catch_fork_permanent:
8977 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
8978 &catch_fork_breakpoint_ops);
8979 break;
8980 case catch_vfork_temporary:
8981 case catch_vfork_permanent:
8982 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
8983 &catch_vfork_breakpoint_ops);
8984 break;
8985 default:
8986 error (_("unsupported or unknown fork kind; cannot catch it"));
8987 break;
8988 }
8989 }
8990
8991 static void
8992 catch_exec_command_1 (char *arg, int from_tty,
8993 struct cmd_list_element *command)
8994 {
8995 struct gdbarch *gdbarch = get_current_arch ();
8996 int tempflag;
8997 char *cond_string = NULL;
8998
8999 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9000
9001 if (!arg)
9002 arg = "";
9003 arg = skip_spaces (arg);
9004
9005 /* The allowed syntax is:
9006 catch exec
9007 catch exec if <cond>
9008
9009 First, check if there's an if clause. */
9010 cond_string = ep_parse_optional_if_clause (&arg);
9011
9012 if ((*arg != '\0') && !isspace (*arg))
9013 error (_("Junk at end of arguments."));
9014
9015 /* If this target supports it, create an exec catchpoint
9016 and enable reporting of such events. */
9017 create_catchpoint (gdbarch, tempflag, cond_string,
9018 &catch_exec_breakpoint_ops);
9019 }
9020
9021 static enum print_stop_action
9022 print_exception_catchpoint (struct breakpoint *b)
9023 {
9024 int bp_temp, bp_throw;
9025
9026 annotate_catchpoint (b->number);
9027
9028 bp_throw = strstr (b->addr_string, "throw") != NULL;
9029 if (b->loc->address != b->loc->requested_address)
9030 breakpoint_adjustment_warning (b->loc->requested_address,
9031 b->loc->address,
9032 b->number, 1);
9033 bp_temp = b->disposition == disp_del;
9034 ui_out_text (uiout,
9035 bp_temp ? "Temporary catchpoint "
9036 : "Catchpoint ");
9037 if (!ui_out_is_mi_like_p (uiout))
9038 ui_out_field_int (uiout, "bkptno", b->number);
9039 ui_out_text (uiout,
9040 bp_throw ? " (exception thrown), "
9041 : " (exception caught), ");
9042 if (ui_out_is_mi_like_p (uiout))
9043 {
9044 ui_out_field_string (uiout, "reason",
9045 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9046 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9047 ui_out_field_int (uiout, "bkptno", b->number);
9048 }
9049 return PRINT_SRC_AND_LOC;
9050 }
9051
9052 static void
9053 print_one_exception_catchpoint (struct breakpoint *b,
9054 struct bp_location **last_loc)
9055 {
9056 struct value_print_options opts;
9057
9058 get_user_print_options (&opts);
9059 if (opts.addressprint)
9060 {
9061 annotate_field (4);
9062 if (b->loc == NULL || b->loc->shlib_disabled)
9063 ui_out_field_string (uiout, "addr", "<PENDING>");
9064 else
9065 ui_out_field_core_addr (uiout, "addr",
9066 b->loc->gdbarch, b->loc->address);
9067 }
9068 annotate_field (5);
9069 if (b->loc)
9070 *last_loc = b->loc;
9071 if (strstr (b->addr_string, "throw") != NULL)
9072 ui_out_field_string (uiout, "what", "exception throw");
9073 else
9074 ui_out_field_string (uiout, "what", "exception catch");
9075 }
9076
9077 static void
9078 print_mention_exception_catchpoint (struct breakpoint *b)
9079 {
9080 int bp_temp;
9081 int bp_throw;
9082
9083 bp_temp = b->disposition == disp_del;
9084 bp_throw = strstr (b->addr_string, "throw") != NULL;
9085 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9086 : _("Catchpoint "));
9087 ui_out_field_int (uiout, "bkptno", b->number);
9088 ui_out_text (uiout, bp_throw ? _(" (throw)")
9089 : _(" (catch)"));
9090 }
9091
9092 /* Implement the "print_recreate" breakpoint_ops method for throw and
9093 catch catchpoints. */
9094
9095 static void
9096 print_recreate_exception_catchpoint (struct breakpoint *b,
9097 struct ui_file *fp)
9098 {
9099 int bp_temp;
9100 int bp_throw;
9101
9102 bp_temp = b->disposition == disp_del;
9103 bp_throw = strstr (b->addr_string, "throw") != NULL;
9104 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9105 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9106 }
9107
9108 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
9109 NULL, /* insert */
9110 NULL, /* remove */
9111 NULL, /* breakpoint_hit */
9112 NULL, /* resources_needed */
9113 print_exception_catchpoint,
9114 print_one_exception_catchpoint,
9115 print_mention_exception_catchpoint,
9116 print_recreate_exception_catchpoint
9117 };
9118
9119 static int
9120 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9121 enum exception_event_kind ex_event, int from_tty)
9122 {
9123 char *trigger_func_name;
9124
9125 if (ex_event == EX_EVENT_CATCH)
9126 trigger_func_name = "__cxa_begin_catch";
9127 else
9128 trigger_func_name = "__cxa_throw";
9129
9130 create_breakpoint (get_current_arch (),
9131 trigger_func_name, cond_string, -1,
9132 0 /* condition and thread are valid. */,
9133 tempflag, bp_breakpoint,
9134 0,
9135 AUTO_BOOLEAN_TRUE /* pending */,
9136 &gnu_v3_exception_catchpoint_ops, from_tty,
9137 1 /* enabled */,
9138 0 /* internal */);
9139
9140 return 1;
9141 }
9142
9143 /* Deal with "catch catch" and "catch throw" commands. */
9144
9145 static void
9146 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9147 int tempflag, int from_tty)
9148 {
9149 char *cond_string = NULL;
9150
9151 if (!arg)
9152 arg = "";
9153 arg = skip_spaces (arg);
9154
9155 cond_string = ep_parse_optional_if_clause (&arg);
9156
9157 if ((*arg != '\0') && !isspace (*arg))
9158 error (_("Junk at end of arguments."));
9159
9160 if (ex_event != EX_EVENT_THROW
9161 && ex_event != EX_EVENT_CATCH)
9162 error (_("Unsupported or unknown exception event; cannot catch it"));
9163
9164 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9165 return;
9166
9167 warning (_("Unsupported with this platform/compiler combination."));
9168 }
9169
9170 /* Implementation of "catch catch" command. */
9171
9172 static void
9173 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9174 {
9175 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9176
9177 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9178 }
9179
9180 /* Implementation of "catch throw" command. */
9181
9182 static void
9183 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9184 {
9185 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9186
9187 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9188 }
9189
9190 /* Create a breakpoint struct for Ada exception catchpoints. */
9191
9192 static void
9193 create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9194 struct symtab_and_line sal,
9195 char *addr_string,
9196 char *exp_string,
9197 char *cond_string,
9198 struct expression *cond,
9199 struct breakpoint_ops *ops,
9200 int tempflag,
9201 int from_tty)
9202 {
9203 struct breakpoint *b;
9204
9205 if (from_tty)
9206 {
9207 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9208 if (!loc_gdbarch)
9209 loc_gdbarch = gdbarch;
9210
9211 describe_other_breakpoints (loc_gdbarch,
9212 sal.pspace, sal.pc, sal.section, -1);
9213 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9214 version for exception catchpoints, because two catchpoints
9215 used for different exception names will use the same address.
9216 In this case, a "breakpoint ... also set at..." warning is
9217 unproductive. Besides, the warning phrasing is also a bit
9218 inapropriate, we should use the word catchpoint, and tell
9219 the user what type of catchpoint it is. The above is good
9220 enough for now, though. */
9221 }
9222
9223 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
9224 set_breakpoint_count (breakpoint_count + 1);
9225
9226 b->enable_state = bp_enabled;
9227 b->disposition = tempflag ? disp_del : disp_donttouch;
9228 b->number = breakpoint_count;
9229 b->ignore_count = 0;
9230 b->loc->cond = cond;
9231 b->addr_string = addr_string;
9232 b->language = language_ada;
9233 b->cond_string = cond_string;
9234 b->exp_string = exp_string;
9235 b->thread = -1;
9236 b->ops = ops;
9237
9238 mention (b);
9239 update_global_location_list (1);
9240 }
9241
9242 /* Implement the "catch exception" command. */
9243
9244 static void
9245 catch_ada_exception_command (char *arg, int from_tty,
9246 struct cmd_list_element *command)
9247 {
9248 struct gdbarch *gdbarch = get_current_arch ();
9249 int tempflag;
9250 struct symtab_and_line sal;
9251 char *addr_string = NULL;
9252 char *exp_string = NULL;
9253 char *cond_string = NULL;
9254 struct expression *cond = NULL;
9255 struct breakpoint_ops *ops = NULL;
9256
9257 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9258
9259 if (!arg)
9260 arg = "";
9261 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9262 &cond_string, &cond, &ops);
9263 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
9264 cond_string, cond, ops, tempflag,
9265 from_tty);
9266 }
9267
9268 /* Cleanup function for a syscall filter list. */
9269 static void
9270 clean_up_filters (void *arg)
9271 {
9272 VEC(int) *iter = *(VEC(int) **) arg;
9273 VEC_free (int, iter);
9274 }
9275
9276 /* Splits the argument using space as delimiter. Returns an xmalloc'd
9277 filter list, or NULL if no filtering is required. */
9278 static VEC(int) *
9279 catch_syscall_split_args (char *arg)
9280 {
9281 VEC(int) *result = NULL;
9282 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9283
9284 while (*arg != '\0')
9285 {
9286 int i, syscall_number;
9287 char *endptr;
9288 char cur_name[128];
9289 struct syscall s;
9290
9291 /* Skip whitespace. */
9292 while (isspace (*arg))
9293 arg++;
9294
9295 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9296 cur_name[i] = arg[i];
9297 cur_name[i] = '\0';
9298 arg += i;
9299
9300 /* Check if the user provided a syscall name or a number. */
9301 syscall_number = (int) strtol (cur_name, &endptr, 0);
9302 if (*endptr == '\0')
9303 get_syscall_by_number (syscall_number, &s);
9304 else
9305 {
9306 /* We have a name. Let's check if it's valid and convert it
9307 to a number. */
9308 get_syscall_by_name (cur_name, &s);
9309
9310 if (s.number == UNKNOWN_SYSCALL)
9311 /* Here we have to issue an error instead of a warning,
9312 because GDB cannot do anything useful if there's no
9313 syscall number to be caught. */
9314 error (_("Unknown syscall name '%s'."), cur_name);
9315 }
9316
9317 /* Ok, it's valid. */
9318 VEC_safe_push (int, result, s.number);
9319 }
9320
9321 discard_cleanups (cleanup);
9322 return result;
9323 }
9324
9325 /* Implement the "catch syscall" command. */
9326
9327 static void
9328 catch_syscall_command_1 (char *arg, int from_tty,
9329 struct cmd_list_element *command)
9330 {
9331 int tempflag;
9332 VEC(int) *filter;
9333 struct syscall s;
9334 struct gdbarch *gdbarch = get_current_arch ();
9335
9336 /* Checking if the feature if supported. */
9337 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9338 error (_("The feature 'catch syscall' is not supported on \
9339 this architecture yet."));
9340
9341 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9342
9343 arg = skip_spaces (arg);
9344
9345 /* We need to do this first "dummy" translation in order
9346 to get the syscall XML file loaded or, most important,
9347 to display a warning to the user if there's no XML file
9348 for his/her architecture. */
9349 get_syscall_by_number (0, &s);
9350
9351 /* The allowed syntax is:
9352 catch syscall
9353 catch syscall <name | number> [<name | number> ... <name | number>]
9354
9355 Let's check if there's a syscall name. */
9356
9357 if (arg != NULL)
9358 filter = catch_syscall_split_args (arg);
9359 else
9360 filter = NULL;
9361
9362 create_syscall_event_catchpoint (tempflag, filter,
9363 &catch_syscall_breakpoint_ops);
9364 }
9365
9366 /* Implement the "catch assert" command. */
9367
9368 static void
9369 catch_assert_command (char *arg, int from_tty,
9370 struct cmd_list_element *command)
9371 {
9372 struct gdbarch *gdbarch = get_current_arch ();
9373 int tempflag;
9374 struct symtab_and_line sal;
9375 char *addr_string = NULL;
9376 struct breakpoint_ops *ops = NULL;
9377
9378 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9379
9380 if (!arg)
9381 arg = "";
9382 sal = ada_decode_assert_location (arg, &addr_string, &ops);
9383 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9384 ops, tempflag, from_tty);
9385 }
9386
9387 static void
9388 catch_command (char *arg, int from_tty)
9389 {
9390 error (_("Catch requires an event name."));
9391 }
9392 \f
9393
9394 static void
9395 tcatch_command (char *arg, int from_tty)
9396 {
9397 error (_("Catch requires an event name."));
9398 }
9399
9400 /* Delete breakpoints by address or line. */
9401
9402 static void
9403 clear_command (char *arg, int from_tty)
9404 {
9405 struct breakpoint *b;
9406 VEC(breakpoint_p) *found = 0;
9407 int ix;
9408 int default_match;
9409 struct symtabs_and_lines sals;
9410 struct symtab_and_line sal;
9411 int i;
9412
9413 if (arg)
9414 {
9415 sals = decode_line_spec (arg, 1);
9416 default_match = 0;
9417 }
9418 else
9419 {
9420 sals.sals = (struct symtab_and_line *)
9421 xmalloc (sizeof (struct symtab_and_line));
9422 make_cleanup (xfree, sals.sals);
9423 init_sal (&sal); /* Initialize to zeroes. */
9424 sal.line = default_breakpoint_line;
9425 sal.symtab = default_breakpoint_symtab;
9426 sal.pc = default_breakpoint_address;
9427 sal.pspace = default_breakpoint_pspace;
9428 if (sal.symtab == 0)
9429 error (_("No source file specified."));
9430
9431 sals.sals[0] = sal;
9432 sals.nelts = 1;
9433
9434 default_match = 1;
9435 }
9436
9437 /* We don't call resolve_sal_pc here. That's not as bad as it
9438 seems, because all existing breakpoints typically have both
9439 file/line and pc set. So, if clear is given file/line, we can
9440 match this to existing breakpoint without obtaining pc at all.
9441
9442 We only support clearing given the address explicitly
9443 present in breakpoint table. Say, we've set breakpoint
9444 at file:line. There were several PC values for that file:line,
9445 due to optimization, all in one block.
9446
9447 We've picked one PC value. If "clear" is issued with another
9448 PC corresponding to the same file:line, the breakpoint won't
9449 be cleared. We probably can still clear the breakpoint, but
9450 since the other PC value is never presented to user, user
9451 can only find it by guessing, and it does not seem important
9452 to support that. */
9453
9454 /* For each line spec given, delete bps which correspond to it. Do
9455 it in two passes, solely to preserve the current behavior that
9456 from_tty is forced true if we delete more than one
9457 breakpoint. */
9458
9459 found = NULL;
9460 for (i = 0; i < sals.nelts; i++)
9461 {
9462 /* If exact pc given, clear bpts at that pc.
9463 If line given (pc == 0), clear all bpts on specified line.
9464 If defaulting, clear all bpts on default line
9465 or at default pc.
9466
9467 defaulting sal.pc != 0 tests to do
9468
9469 0 1 pc
9470 1 1 pc _and_ line
9471 0 0 line
9472 1 0 <can't happen> */
9473
9474 sal = sals.sals[i];
9475
9476 /* Find all matching breakpoints and add them to 'found'. */
9477 ALL_BREAKPOINTS (b)
9478 {
9479 int match = 0;
9480 /* Are we going to delete b? */
9481 if (b->type != bp_none && !is_watchpoint (b))
9482 {
9483 struct bp_location *loc = b->loc;
9484 for (; loc; loc = loc->next)
9485 {
9486 int pc_match = sal.pc
9487 && (loc->pspace == sal.pspace)
9488 && (loc->address == sal.pc)
9489 && (!section_is_overlay (loc->section)
9490 || loc->section == sal.section);
9491 int line_match = ((default_match || (0 == sal.pc))
9492 && b->source_file != NULL
9493 && sal.symtab != NULL
9494 && sal.pspace == loc->pspace
9495 && strcmp (b->source_file,
9496 sal.symtab->filename) == 0
9497 && b->line_number == sal.line);
9498 if (pc_match || line_match)
9499 {
9500 match = 1;
9501 break;
9502 }
9503 }
9504 }
9505
9506 if (match)
9507 VEC_safe_push(breakpoint_p, found, b);
9508 }
9509 }
9510 /* Now go thru the 'found' chain and delete them. */
9511 if (VEC_empty(breakpoint_p, found))
9512 {
9513 if (arg)
9514 error (_("No breakpoint at %s."), arg);
9515 else
9516 error (_("No breakpoint at this line."));
9517 }
9518
9519 if (VEC_length(breakpoint_p, found) > 1)
9520 from_tty = 1; /* Always report if deleted more than one. */
9521 if (from_tty)
9522 {
9523 if (VEC_length(breakpoint_p, found) == 1)
9524 printf_unfiltered (_("Deleted breakpoint "));
9525 else
9526 printf_unfiltered (_("Deleted breakpoints "));
9527 }
9528 breakpoints_changed ();
9529
9530 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
9531 {
9532 if (from_tty)
9533 printf_unfiltered ("%d ", b->number);
9534 delete_breakpoint (b);
9535 }
9536 if (from_tty)
9537 putchar_unfiltered ('\n');
9538 }
9539 \f
9540 /* Delete breakpoint in BS if they are `delete' breakpoints and
9541 all breakpoints that are marked for deletion, whether hit or not.
9542 This is called after any breakpoint is hit, or after errors. */
9543
9544 void
9545 breakpoint_auto_delete (bpstat bs)
9546 {
9547 struct breakpoint *b, *b_tmp;
9548
9549 for (; bs; bs = bs->next)
9550 if (bs->breakpoint_at
9551 && bs->breakpoint_at->disposition == disp_del
9552 && bs->stop)
9553 delete_breakpoint (bs->breakpoint_at);
9554
9555 ALL_BREAKPOINTS_SAFE (b, b_tmp)
9556 {
9557 if (b->disposition == disp_del_at_next_stop)
9558 delete_breakpoint (b);
9559 }
9560 }
9561
9562 /* A comparison function for bp_location AP and BP being interfaced to
9563 qsort. Sort elements primarily by their ADDRESS (no matter what
9564 does breakpoint_address_is_meaningful say for its OWNER),
9565 secondarily by ordering first bp_permanent OWNERed elements and
9566 terciarily just ensuring the array is sorted stable way despite
9567 qsort being an instable algorithm. */
9568
9569 static int
9570 bp_location_compare (const void *ap, const void *bp)
9571 {
9572 struct bp_location *a = *(void **) ap;
9573 struct bp_location *b = *(void **) bp;
9574 /* A and B come from existing breakpoints having non-NULL OWNER. */
9575 int a_perm = a->owner->enable_state == bp_permanent;
9576 int b_perm = b->owner->enable_state == bp_permanent;
9577
9578 if (a->address != b->address)
9579 return (a->address > b->address) - (a->address < b->address);
9580
9581 /* Sort permanent breakpoints first. */
9582 if (a_perm != b_perm)
9583 return (a_perm < b_perm) - (a_perm > b_perm);
9584
9585 /* Make the user-visible order stable across GDB runs. Locations of
9586 the same breakpoint can be sorted in arbitrary order. */
9587
9588 if (a->owner->number != b->owner->number)
9589 return (a->owner->number > b->owner->number)
9590 - (a->owner->number < b->owner->number);
9591
9592 return (a > b) - (a < b);
9593 }
9594
9595 /* Set bp_location_placed_address_before_address_max and
9596 bp_location_shadow_len_after_address_max according to the current
9597 content of the bp_location array. */
9598
9599 static void
9600 bp_location_target_extensions_update (void)
9601 {
9602 struct bp_location *bl, **blp_tmp;
9603
9604 bp_location_placed_address_before_address_max = 0;
9605 bp_location_shadow_len_after_address_max = 0;
9606
9607 ALL_BP_LOCATIONS (bl, blp_tmp)
9608 {
9609 CORE_ADDR start, end, addr;
9610
9611 if (!bp_location_has_shadow (bl))
9612 continue;
9613
9614 start = bl->target_info.placed_address;
9615 end = start + bl->target_info.shadow_len;
9616
9617 gdb_assert (bl->address >= start);
9618 addr = bl->address - start;
9619 if (addr > bp_location_placed_address_before_address_max)
9620 bp_location_placed_address_before_address_max = addr;
9621
9622 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
9623
9624 gdb_assert (bl->address < end);
9625 addr = end - bl->address;
9626 if (addr > bp_location_shadow_len_after_address_max)
9627 bp_location_shadow_len_after_address_max = addr;
9628 }
9629 }
9630
9631 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
9632 into the inferior, only remove already-inserted locations that no
9633 longer should be inserted. Functions that delete a breakpoint or
9634 breakpoints should pass false, so that deleting a breakpoint
9635 doesn't have the side effect of inserting the locations of other
9636 breakpoints that are marked not-inserted, but should_be_inserted
9637 returns true on them.
9638
9639 This behaviour is useful is situations close to tear-down -- e.g.,
9640 after an exec, while the target still has execution, but breakpoint
9641 shadows of the previous executable image should *NOT* be restored
9642 to the new image; or before detaching, where the target still has
9643 execution and wants to delete breakpoints from GDB's lists, and all
9644 breakpoints had already been removed from the inferior. */
9645
9646 static void
9647 update_global_location_list (int should_insert)
9648 {
9649 struct breakpoint *b;
9650 struct bp_location **locp, *loc;
9651 struct cleanup *cleanups;
9652
9653 /* Used in the duplicates detection below. When iterating over all
9654 bp_locations, points to the first bp_location of a given address.
9655 Breakpoints and watchpoints of different types are never
9656 duplicates of each other. Keep one pointer for each type of
9657 breakpoint/watchpoint, so we only need to loop over all locations
9658 once. */
9659 struct bp_location *bp_loc_first; /* breakpoint */
9660 struct bp_location *wp_loc_first; /* hardware watchpoint */
9661 struct bp_location *awp_loc_first; /* access watchpoint */
9662 struct bp_location *rwp_loc_first; /* read watchpoint */
9663
9664 /* Saved former bp_location array which we compare against the newly
9665 built bp_location from the current state of ALL_BREAKPOINTS. */
9666 struct bp_location **old_location, **old_locp;
9667 unsigned old_location_count;
9668
9669 old_location = bp_location;
9670 old_location_count = bp_location_count;
9671 bp_location = NULL;
9672 bp_location_count = 0;
9673 cleanups = make_cleanup (xfree, old_location);
9674
9675 ALL_BREAKPOINTS (b)
9676 for (loc = b->loc; loc; loc = loc->next)
9677 bp_location_count++;
9678
9679 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
9680 locp = bp_location;
9681 ALL_BREAKPOINTS (b)
9682 for (loc = b->loc; loc; loc = loc->next)
9683 *locp++ = loc;
9684 qsort (bp_location, bp_location_count, sizeof (*bp_location),
9685 bp_location_compare);
9686
9687 bp_location_target_extensions_update ();
9688
9689 /* Identify bp_location instances that are no longer present in the
9690 new list, and therefore should be freed. Note that it's not
9691 necessary that those locations should be removed from inferior --
9692 if there's another location at the same address (previously
9693 marked as duplicate), we don't need to remove/insert the
9694 location.
9695
9696 LOCP is kept in sync with OLD_LOCP, each pointing to the current
9697 and former bp_location array state respectively. */
9698
9699 locp = bp_location;
9700 for (old_locp = old_location; old_locp < old_location + old_location_count;
9701 old_locp++)
9702 {
9703 struct bp_location *old_loc = *old_locp;
9704 struct bp_location **loc2p;
9705
9706 /* Tells if 'old_loc' is found amoung the new locations. If
9707 not, we have to free it. */
9708 int found_object = 0;
9709 /* Tells if the location should remain inserted in the target. */
9710 int keep_in_target = 0;
9711 int removed = 0;
9712
9713 /* Skip LOCP entries which will definitely never be needed.
9714 Stop either at or being the one matching OLD_LOC. */
9715 while (locp < bp_location + bp_location_count
9716 && (*locp)->address < old_loc->address)
9717 locp++;
9718
9719 for (loc2p = locp;
9720 (loc2p < bp_location + bp_location_count
9721 && (*loc2p)->address == old_loc->address);
9722 loc2p++)
9723 {
9724 if (*loc2p == old_loc)
9725 {
9726 found_object = 1;
9727 break;
9728 }
9729 }
9730
9731 /* If this location is no longer present, and inserted, look if
9732 there's maybe a new location at the same address. If so,
9733 mark that one inserted, and don't remove this one. This is
9734 needed so that we don't have a time window where a breakpoint
9735 at certain location is not inserted. */
9736
9737 if (old_loc->inserted)
9738 {
9739 /* If the location is inserted now, we might have to remove
9740 it. */
9741
9742 if (found_object && should_be_inserted (old_loc))
9743 {
9744 /* The location is still present in the location list,
9745 and still should be inserted. Don't do anything. */
9746 keep_in_target = 1;
9747 }
9748 else
9749 {
9750 /* The location is either no longer present, or got
9751 disabled. See if there's another location at the
9752 same address, in which case we don't need to remove
9753 this one from the target. */
9754
9755 /* OLD_LOC comes from existing struct breakpoint. */
9756 if (breakpoint_address_is_meaningful (old_loc->owner))
9757 {
9758 for (loc2p = locp;
9759 (loc2p < bp_location + bp_location_count
9760 && (*loc2p)->address == old_loc->address);
9761 loc2p++)
9762 {
9763 struct bp_location *loc2 = *loc2p;
9764
9765 if (breakpoint_locations_match (loc2, old_loc))
9766 {
9767 /* For the sake of should_be_inserted.
9768 Duplicates check below will fix up this
9769 later. */
9770 loc2->duplicate = 0;
9771
9772 /* Read watchpoint locations are switched to
9773 access watchpoints, if the former are not
9774 supported, but the latter are. */
9775 if (is_hardware_watchpoint (old_loc->owner))
9776 {
9777 gdb_assert (is_hardware_watchpoint (loc2->owner));
9778 loc2->watchpoint_type = old_loc->watchpoint_type;
9779 }
9780
9781 if (loc2 != old_loc && should_be_inserted (loc2))
9782 {
9783 loc2->inserted = 1;
9784 loc2->target_info = old_loc->target_info;
9785 keep_in_target = 1;
9786 break;
9787 }
9788 }
9789 }
9790 }
9791 }
9792
9793 if (!keep_in_target)
9794 {
9795 if (remove_breakpoint (old_loc, mark_uninserted))
9796 {
9797 /* This is just about all we can do. We could keep
9798 this location on the global list, and try to
9799 remove it next time, but there's no particular
9800 reason why we will succeed next time.
9801
9802 Note that at this point, old_loc->owner is still
9803 valid, as delete_breakpoint frees the breakpoint
9804 only after calling us. */
9805 printf_filtered (_("warning: Error removing "
9806 "breakpoint %d\n"),
9807 old_loc->owner->number);
9808 }
9809 removed = 1;
9810 }
9811 }
9812
9813 if (!found_object)
9814 {
9815 if (removed && non_stop
9816 && breakpoint_address_is_meaningful (old_loc->owner)
9817 && !is_hardware_watchpoint (old_loc->owner))
9818 {
9819 /* This location was removed from the target. In
9820 non-stop mode, a race condition is possible where
9821 we've removed a breakpoint, but stop events for that
9822 breakpoint are already queued and will arrive later.
9823 We apply an heuristic to be able to distinguish such
9824 SIGTRAPs from other random SIGTRAPs: we keep this
9825 breakpoint location for a bit, and will retire it
9826 after we see some number of events. The theory here
9827 is that reporting of events should, "on the average",
9828 be fair, so after a while we'll see events from all
9829 threads that have anything of interest, and no longer
9830 need to keep this breakpoint location around. We
9831 don't hold locations forever so to reduce chances of
9832 mistaking a non-breakpoint SIGTRAP for a breakpoint
9833 SIGTRAP.
9834
9835 The heuristic failing can be disastrous on
9836 decr_pc_after_break targets.
9837
9838 On decr_pc_after_break targets, like e.g., x86-linux,
9839 if we fail to recognize a late breakpoint SIGTRAP,
9840 because events_till_retirement has reached 0 too
9841 soon, we'll fail to do the PC adjustment, and report
9842 a random SIGTRAP to the user. When the user resumes
9843 the inferior, it will most likely immediately crash
9844 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
9845 corrupted, because of being resumed e.g., in the
9846 middle of a multi-byte instruction, or skipped a
9847 one-byte instruction. This was actually seen happen
9848 on native x86-linux, and should be less rare on
9849 targets that do not support new thread events, like
9850 remote, due to the heuristic depending on
9851 thread_count.
9852
9853 Mistaking a random SIGTRAP for a breakpoint trap
9854 causes similar symptoms (PC adjustment applied when
9855 it shouldn't), but then again, playing with SIGTRAPs
9856 behind the debugger's back is asking for trouble.
9857
9858 Since hardware watchpoint traps are always
9859 distinguishable from other traps, so we don't need to
9860 apply keep hardware watchpoint moribund locations
9861 around. We simply always ignore hardware watchpoint
9862 traps we can no longer explain. */
9863
9864 old_loc->events_till_retirement = 3 * (thread_count () + 1);
9865 old_loc->owner = NULL;
9866
9867 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
9868 }
9869 else
9870 {
9871 old_loc->owner = NULL;
9872 decref_bp_location (&old_loc);
9873 }
9874 }
9875 }
9876
9877 /* Rescan breakpoints at the same address and section, marking the
9878 first one as "first" and any others as "duplicates". This is so
9879 that the bpt instruction is only inserted once. If we have a
9880 permanent breakpoint at the same place as BPT, make that one the
9881 official one, and the rest as duplicates. Permanent breakpoints
9882 are sorted first for the same address.
9883
9884 Do the same for hardware watchpoints, but also considering the
9885 watchpoint's type (regular/access/read) and length. */
9886
9887 bp_loc_first = NULL;
9888 wp_loc_first = NULL;
9889 awp_loc_first = NULL;
9890 rwp_loc_first = NULL;
9891 ALL_BP_LOCATIONS (loc, locp)
9892 {
9893 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
9894 non-NULL. */
9895 struct breakpoint *b = loc->owner;
9896 struct bp_location **loc_first_p;
9897
9898 if (b->enable_state == bp_disabled
9899 || b->enable_state == bp_call_disabled
9900 || b->enable_state == bp_startup_disabled
9901 || !loc->enabled
9902 || loc->shlib_disabled
9903 || !breakpoint_address_is_meaningful (b)
9904 || is_tracepoint (b))
9905 continue;
9906
9907 /* Permanent breakpoint should always be inserted. */
9908 if (b->enable_state == bp_permanent && ! loc->inserted)
9909 internal_error (__FILE__, __LINE__,
9910 _("allegedly permanent breakpoint is not "
9911 "actually inserted"));
9912
9913 if (b->type == bp_hardware_watchpoint)
9914 loc_first_p = &wp_loc_first;
9915 else if (b->type == bp_read_watchpoint)
9916 loc_first_p = &rwp_loc_first;
9917 else if (b->type == bp_access_watchpoint)
9918 loc_first_p = &awp_loc_first;
9919 else
9920 loc_first_p = &bp_loc_first;
9921
9922 if (*loc_first_p == NULL
9923 || (overlay_debugging && loc->section != (*loc_first_p)->section)
9924 || !breakpoint_locations_match (loc, *loc_first_p))
9925 {
9926 *loc_first_p = loc;
9927 loc->duplicate = 0;
9928 continue;
9929 }
9930
9931 loc->duplicate = 1;
9932
9933 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
9934 && b->enable_state != bp_permanent)
9935 internal_error (__FILE__, __LINE__,
9936 _("another breakpoint was inserted on top of "
9937 "a permanent breakpoint"));
9938 }
9939
9940 if (breakpoints_always_inserted_mode () && should_insert
9941 && (have_live_inferiors ()
9942 || (gdbarch_has_global_breakpoints (target_gdbarch))))
9943 insert_breakpoint_locations ();
9944
9945 do_cleanups (cleanups);
9946 }
9947
9948 void
9949 breakpoint_retire_moribund (void)
9950 {
9951 struct bp_location *loc;
9952 int ix;
9953
9954 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
9955 if (--(loc->events_till_retirement) == 0)
9956 {
9957 decref_bp_location (&loc);
9958 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
9959 --ix;
9960 }
9961 }
9962
9963 static void
9964 update_global_location_list_nothrow (int inserting)
9965 {
9966 struct gdb_exception e;
9967
9968 TRY_CATCH (e, RETURN_MASK_ERROR)
9969 update_global_location_list (inserting);
9970 }
9971
9972 /* Clear BKP from a BPS. */
9973
9974 static void
9975 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
9976 {
9977 bpstat bs;
9978
9979 for (bs = bps; bs; bs = bs->next)
9980 if (bs->breakpoint_at == bpt)
9981 {
9982 bs->breakpoint_at = NULL;
9983 bs->old_val = NULL;
9984 /* bs->commands will be freed later. */
9985 }
9986 }
9987
9988 /* Callback for iterate_over_threads. */
9989 static int
9990 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
9991 {
9992 struct breakpoint *bpt = data;
9993
9994 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
9995 return 0;
9996 }
9997
9998 /* Delete a breakpoint and clean up all traces of it in the data
9999 structures. */
10000
10001 void
10002 delete_breakpoint (struct breakpoint *bpt)
10003 {
10004 struct breakpoint *b;
10005
10006 gdb_assert (bpt != NULL);
10007
10008 /* Has this bp already been deleted? This can happen because
10009 multiple lists can hold pointers to bp's. bpstat lists are
10010 especial culprits.
10011
10012 One example of this happening is a watchpoint's scope bp. When
10013 the scope bp triggers, we notice that the watchpoint is out of
10014 scope, and delete it. We also delete its scope bp. But the
10015 scope bp is marked "auto-deleting", and is already on a bpstat.
10016 That bpstat is then checked for auto-deleting bp's, which are
10017 deleted.
10018
10019 A real solution to this problem might involve reference counts in
10020 bp's, and/or giving them pointers back to their referencing
10021 bpstat's, and teaching delete_breakpoint to only free a bp's
10022 storage when no more references were extent. A cheaper bandaid
10023 was chosen. */
10024 if (bpt->type == bp_none)
10025 return;
10026
10027 /* At least avoid this stale reference until the reference counting
10028 of breakpoints gets resolved. */
10029 if (bpt->related_breakpoint != NULL)
10030 {
10031 gdb_assert (bpt->related_breakpoint->related_breakpoint == bpt);
10032 bpt->related_breakpoint->disposition = disp_del_at_next_stop;
10033 bpt->related_breakpoint->related_breakpoint = NULL;
10034 bpt->related_breakpoint = NULL;
10035 }
10036
10037 observer_notify_breakpoint_deleted (bpt->number);
10038
10039 if (breakpoint_chain == bpt)
10040 breakpoint_chain = bpt->next;
10041
10042 ALL_BREAKPOINTS (b)
10043 if (b->next == bpt)
10044 {
10045 b->next = bpt->next;
10046 break;
10047 }
10048
10049 decref_counted_command_line (&bpt->commands);
10050 xfree (bpt->cond_string);
10051 xfree (bpt->cond_exp);
10052 xfree (bpt->addr_string);
10053 xfree (bpt->exp);
10054 xfree (bpt->exp_string);
10055 xfree (bpt->exp_string_reparse);
10056 value_free (bpt->val);
10057 xfree (bpt->source_file);
10058 xfree (bpt->exec_pathname);
10059 clean_up_filters (&bpt->syscalls_to_be_caught);
10060
10061
10062 /* Be sure no bpstat's are pointing at the breakpoint after it's
10063 been freed. */
10064 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
10065 in all threeds for now. Note that we cannot just remove bpstats
10066 pointing at bpt from the stop_bpstat list entirely, as breakpoint
10067 commands are associated with the bpstat; if we remove it here,
10068 then the later call to bpstat_do_actions (&stop_bpstat); in
10069 event-top.c won't do anything, and temporary breakpoints with
10070 commands won't work. */
10071
10072 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10073
10074 /* Now that breakpoint is removed from breakpoint list, update the
10075 global location list. This will remove locations that used to
10076 belong to this breakpoint. Do this before freeing the breakpoint
10077 itself, since remove_breakpoint looks at location's owner. It
10078 might be better design to have location completely
10079 self-contained, but it's not the case now. */
10080 update_global_location_list (0);
10081
10082
10083 /* On the chance that someone will soon try again to delete this
10084 same bp, we mark it as deleted before freeing its storage. */
10085 bpt->type = bp_none;
10086
10087 xfree (bpt);
10088 }
10089
10090 static void
10091 do_delete_breakpoint_cleanup (void *b)
10092 {
10093 delete_breakpoint (b);
10094 }
10095
10096 struct cleanup *
10097 make_cleanup_delete_breakpoint (struct breakpoint *b)
10098 {
10099 return make_cleanup (do_delete_breakpoint_cleanup, b);
10100 }
10101
10102 /* A callback for map_breakpoint_numbers that calls
10103 delete_breakpoint. */
10104
10105 static void
10106 do_delete_breakpoint (struct breakpoint *b, void *ignore)
10107 {
10108 delete_breakpoint (b);
10109 }
10110
10111 void
10112 delete_command (char *arg, int from_tty)
10113 {
10114 struct breakpoint *b, *b_tmp;
10115
10116 dont_repeat ();
10117
10118 if (arg == 0)
10119 {
10120 int breaks_to_delete = 0;
10121
10122 /* Delete all breakpoints if no argument.
10123 Do not delete internal or call-dummy breakpoints, these have
10124 to be deleted with an explicit breakpoint number argument. */
10125 ALL_BREAKPOINTS (b)
10126 {
10127 if (b->type != bp_call_dummy
10128 && b->type != bp_std_terminate
10129 && b->type != bp_shlib_event
10130 && b->type != bp_jit_event
10131 && b->type != bp_thread_event
10132 && b->type != bp_overlay_event
10133 && b->type != bp_longjmp_master
10134 && b->type != bp_std_terminate_master
10135 && b->type != bp_exception_master
10136 && b->number >= 0)
10137 {
10138 breaks_to_delete = 1;
10139 break;
10140 }
10141 }
10142
10143 /* Ask user only if there are some breakpoints to delete. */
10144 if (!from_tty
10145 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
10146 {
10147 ALL_BREAKPOINTS_SAFE (b, b_tmp)
10148 {
10149 if (b->type != bp_call_dummy
10150 && b->type != bp_std_terminate
10151 && b->type != bp_shlib_event
10152 && b->type != bp_thread_event
10153 && b->type != bp_jit_event
10154 && b->type != bp_overlay_event
10155 && b->type != bp_longjmp_master
10156 && b->type != bp_std_terminate_master
10157 && b->type != bp_exception_master
10158 && b->number >= 0)
10159 delete_breakpoint (b);
10160 }
10161 }
10162 }
10163 else
10164 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
10165 }
10166
10167 static int
10168 all_locations_are_pending (struct bp_location *loc)
10169 {
10170 for (; loc; loc = loc->next)
10171 if (!loc->shlib_disabled)
10172 return 0;
10173 return 1;
10174 }
10175
10176 /* Subroutine of update_breakpoint_locations to simplify it.
10177 Return non-zero if multiple fns in list LOC have the same name.
10178 Null names are ignored. */
10179
10180 static int
10181 ambiguous_names_p (struct bp_location *loc)
10182 {
10183 struct bp_location *l;
10184 htab_t htab = htab_create_alloc (13, htab_hash_string,
10185 (int (*) (const void *,
10186 const void *)) streq,
10187 NULL, xcalloc, xfree);
10188
10189 for (l = loc; l != NULL; l = l->next)
10190 {
10191 const char **slot;
10192 const char *name = l->function_name;
10193
10194 /* Allow for some names to be NULL, ignore them. */
10195 if (name == NULL)
10196 continue;
10197
10198 slot = (const char **) htab_find_slot (htab, (const void *) name,
10199 INSERT);
10200 /* NOTE: We can assume slot != NULL here because xcalloc never
10201 returns NULL. */
10202 if (*slot != NULL)
10203 {
10204 htab_delete (htab);
10205 return 1;
10206 }
10207 *slot = name;
10208 }
10209
10210 htab_delete (htab);
10211 return 0;
10212 }
10213
10214 /* When symbols change, it probably means the sources changed as well,
10215 and it might mean the static tracepoint markers are no longer at
10216 the same address or line numbers they used to be at last we
10217 checked. Losing your static tracepoints whenever you rebuild is
10218 undesirable. This function tries to resync/rematch gdb static
10219 tracepoints with the markers on the target, for static tracepoints
10220 that have not been set by marker id. Static tracepoint that have
10221 been set by marker id are reset by marker id in breakpoint_re_set.
10222 The heuristic is:
10223
10224 1) For a tracepoint set at a specific address, look for a marker at
10225 the old PC. If one is found there, assume to be the same marker.
10226 If the name / string id of the marker found is different from the
10227 previous known name, assume that means the user renamed the marker
10228 in the sources, and output a warning.
10229
10230 2) For a tracepoint set at a given line number, look for a marker
10231 at the new address of the old line number. If one is found there,
10232 assume to be the same marker. If the name / string id of the
10233 marker found is different from the previous known name, assume that
10234 means the user renamed the marker in the sources, and output a
10235 warning.
10236
10237 3) If a marker is no longer found at the same address or line, it
10238 may mean the marker no longer exists. But it may also just mean
10239 the code changed a bit. Maybe the user added a few lines of code
10240 that made the marker move up or down (in line number terms). Ask
10241 the target for info about the marker with the string id as we knew
10242 it. If found, update line number and address in the matching
10243 static tracepoint. This will get confused if there's more than one
10244 marker with the same ID (possible in UST, although unadvised
10245 precisely because it confuses tools). */
10246
10247 static struct symtab_and_line
10248 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10249 {
10250 struct static_tracepoint_marker marker;
10251 CORE_ADDR pc;
10252 int i;
10253
10254 pc = sal.pc;
10255 if (sal.line)
10256 find_line_pc (sal.symtab, sal.line, &pc);
10257
10258 if (target_static_tracepoint_marker_at (pc, &marker))
10259 {
10260 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10261 warning (_("static tracepoint %d changed probed marker from %s to %s"),
10262 b->number,
10263 b->static_trace_marker_id, marker.str_id);
10264
10265 xfree (b->static_trace_marker_id);
10266 b->static_trace_marker_id = xstrdup (marker.str_id);
10267 release_static_tracepoint_marker (&marker);
10268
10269 return sal;
10270 }
10271
10272 /* Old marker wasn't found on target at lineno. Try looking it up
10273 by string ID. */
10274 if (!sal.explicit_pc
10275 && sal.line != 0
10276 && sal.symtab != NULL
10277 && b->static_trace_marker_id != NULL)
10278 {
10279 VEC(static_tracepoint_marker_p) *markers;
10280
10281 markers
10282 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10283
10284 if (!VEC_empty(static_tracepoint_marker_p, markers))
10285 {
10286 struct symtab_and_line sal;
10287 struct symbol *sym;
10288 struct static_tracepoint_marker *marker;
10289
10290 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10291
10292 xfree (b->static_trace_marker_id);
10293 b->static_trace_marker_id = xstrdup (marker->str_id);
10294
10295 warning (_("marker for static tracepoint %d (%s) not "
10296 "found at previous line number"),
10297 b->number, b->static_trace_marker_id);
10298
10299 init_sal (&sal);
10300
10301 sal.pc = marker->address;
10302
10303 sal = find_pc_line (marker->address, 0);
10304 sym = find_pc_sect_function (marker->address, NULL);
10305 ui_out_text (uiout, "Now in ");
10306 if (sym)
10307 {
10308 ui_out_field_string (uiout, "func",
10309 SYMBOL_PRINT_NAME (sym));
10310 ui_out_text (uiout, " at ");
10311 }
10312 ui_out_field_string (uiout, "file", sal.symtab->filename);
10313 ui_out_text (uiout, ":");
10314
10315 if (ui_out_is_mi_like_p (uiout))
10316 {
10317 char *fullname = symtab_to_fullname (sal.symtab);
10318
10319 if (fullname)
10320 ui_out_field_string (uiout, "fullname", fullname);
10321 }
10322
10323 ui_out_field_int (uiout, "line", sal.line);
10324 ui_out_text (uiout, "\n");
10325
10326 b->line_number = sal.line;
10327
10328 xfree (b->source_file);
10329 if (sym)
10330 b->source_file = xstrdup (sal.symtab->filename);
10331 else
10332 b->source_file = NULL;
10333
10334 xfree (b->addr_string);
10335 b->addr_string = xstrprintf ("%s:%d",
10336 sal.symtab->filename, b->line_number);
10337
10338 /* Might be nice to check if function changed, and warn if
10339 so. */
10340
10341 release_static_tracepoint_marker (marker);
10342 }
10343 }
10344 return sal;
10345 }
10346
10347 static void
10348 update_breakpoint_locations (struct breakpoint *b,
10349 struct symtabs_and_lines sals)
10350 {
10351 int i;
10352 char *s;
10353 struct bp_location *existing_locations = b->loc;
10354
10355 /* If there's no new locations, and all existing locations are
10356 pending, don't do anything. This optimizes the common case where
10357 all locations are in the same shared library, that was unloaded.
10358 We'd like to retain the location, so that when the library is
10359 loaded again, we don't loose the enabled/disabled status of the
10360 individual locations. */
10361 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
10362 return;
10363
10364 b->loc = NULL;
10365
10366 for (i = 0; i < sals.nelts; ++i)
10367 {
10368 struct bp_location *new_loc =
10369 add_location_to_breakpoint (b, &(sals.sals[i]));
10370
10371 /* Reparse conditions, they might contain references to the
10372 old symtab. */
10373 if (b->cond_string != NULL)
10374 {
10375 struct gdb_exception e;
10376
10377 s = b->cond_string;
10378 TRY_CATCH (e, RETURN_MASK_ERROR)
10379 {
10380 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10381 0);
10382 }
10383 if (e.reason < 0)
10384 {
10385 warning (_("failed to reevaluate condition "
10386 "for breakpoint %d: %s"),
10387 b->number, e.message);
10388 new_loc->enabled = 0;
10389 }
10390 }
10391
10392 if (b->source_file != NULL)
10393 xfree (b->source_file);
10394 if (sals.sals[i].symtab == NULL)
10395 b->source_file = NULL;
10396 else
10397 b->source_file = xstrdup (sals.sals[i].symtab->filename);
10398
10399 if (b->line_number == 0)
10400 b->line_number = sals.sals[i].line;
10401 }
10402
10403 /* Update locations of permanent breakpoints. */
10404 if (b->enable_state == bp_permanent)
10405 make_breakpoint_permanent (b);
10406
10407 /* If possible, carry over 'disable' status from existing
10408 breakpoints. */
10409 {
10410 struct bp_location *e = existing_locations;
10411 /* If there are multiple breakpoints with the same function name,
10412 e.g. for inline functions, comparing function names won't work.
10413 Instead compare pc addresses; this is just a heuristic as things
10414 may have moved, but in practice it gives the correct answer
10415 often enough until a better solution is found. */
10416 int have_ambiguous_names = ambiguous_names_p (b->loc);
10417
10418 for (; e; e = e->next)
10419 {
10420 if (!e->enabled && e->function_name)
10421 {
10422 struct bp_location *l = b->loc;
10423 if (have_ambiguous_names)
10424 {
10425 for (; l; l = l->next)
10426 if (breakpoint_address_match (e->pspace->aspace, e->address,
10427 l->pspace->aspace, l->address))
10428 {
10429 l->enabled = 0;
10430 break;
10431 }
10432 }
10433 else
10434 {
10435 for (; l; l = l->next)
10436 if (l->function_name
10437 && strcmp (e->function_name, l->function_name) == 0)
10438 {
10439 l->enabled = 0;
10440 break;
10441 }
10442 }
10443 }
10444 }
10445 }
10446
10447 update_global_location_list (1);
10448 }
10449
10450 /* Reset a breakpoint given it's struct breakpoint * BINT.
10451 The value we return ends up being the return value from catch_errors.
10452 Unused in this case. */
10453
10454 static int
10455 breakpoint_re_set_one (void *bint)
10456 {
10457 /* Get past catch_errs. */
10458 struct breakpoint *b = (struct breakpoint *) bint;
10459 int not_found = 0;
10460 int *not_found_ptr = &not_found;
10461 struct symtabs_and_lines sals = {0};
10462 struct symtabs_and_lines expanded = {0};
10463 char *s;
10464 struct gdb_exception e;
10465 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
10466 int marker_spec = 0;
10467
10468 switch (b->type)
10469 {
10470 case bp_none:
10471 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
10472 b->number);
10473 return 0;
10474 case bp_breakpoint:
10475 case bp_hardware_breakpoint:
10476 case bp_tracepoint:
10477 case bp_fast_tracepoint:
10478 case bp_static_tracepoint:
10479 /* Do not attempt to re-set breakpoints disabled during startup. */
10480 if (b->enable_state == bp_startup_disabled)
10481 return 0;
10482
10483 if (b->addr_string == NULL)
10484 {
10485 /* Anything without a string can't be re-set. */
10486 delete_breakpoint (b);
10487 return 0;
10488 }
10489
10490 input_radix = b->input_radix;
10491 s = b->addr_string;
10492
10493 save_current_space_and_thread ();
10494 switch_to_program_space_and_thread (b->pspace);
10495
10496 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10497
10498 set_language (b->language);
10499 TRY_CATCH (e, RETURN_MASK_ERROR)
10500 {
10501 if (marker_spec)
10502 {
10503 sals = decode_static_tracepoint_spec (&s);
10504 if (sals.nelts > b->static_trace_marker_id_idx)
10505 {
10506 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10507 sals.nelts = 1;
10508 }
10509 else
10510 error (_("marker %s not found"), b->static_trace_marker_id);
10511 }
10512 else
10513 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0,
10514 (char ***) NULL, not_found_ptr);
10515 }
10516 if (e.reason < 0)
10517 {
10518 int not_found_and_ok = 0;
10519 /* For pending breakpoints, it's expected that parsing will
10520 fail until the right shared library is loaded. User has
10521 already told to create pending breakpoints and don't need
10522 extra messages. If breakpoint is in bp_shlib_disabled
10523 state, then user already saw the message about that
10524 breakpoint being disabled, and don't want to see more
10525 errors. */
10526 if (not_found
10527 && (b->condition_not_parsed
10528 || (b->loc && b->loc->shlib_disabled)
10529 || b->enable_state == bp_disabled))
10530 not_found_and_ok = 1;
10531
10532 if (!not_found_and_ok)
10533 {
10534 /* We surely don't want to warn about the same breakpoint
10535 10 times. One solution, implemented here, is disable
10536 the breakpoint on error. Another solution would be to
10537 have separate 'warning emitted' flag. Since this
10538 happens only when a binary has changed, I don't know
10539 which approach is better. */
10540 b->enable_state = bp_disabled;
10541 throw_exception (e);
10542 }
10543 }
10544
10545 if (!not_found)
10546 {
10547 gdb_assert (sals.nelts == 1);
10548
10549 resolve_sal_pc (&sals.sals[0]);
10550 if (b->condition_not_parsed && s && s[0])
10551 {
10552 char *cond_string = 0;
10553 int thread = -1;
10554 int task = 0;
10555
10556 find_condition_and_thread (s, sals.sals[0].pc,
10557 &cond_string, &thread, &task);
10558 if (cond_string)
10559 b->cond_string = cond_string;
10560 b->thread = thread;
10561 b->task = task;
10562 b->condition_not_parsed = 0;
10563 }
10564
10565 if (b->type == bp_static_tracepoint && !marker_spec)
10566 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
10567
10568 expanded = expand_line_sal_maybe (sals.sals[0]);
10569 }
10570
10571 make_cleanup (xfree, sals.sals);
10572 update_breakpoint_locations (b, expanded);
10573 break;
10574
10575 case bp_watchpoint:
10576 case bp_hardware_watchpoint:
10577 case bp_read_watchpoint:
10578 case bp_access_watchpoint:
10579 /* Watchpoint can be either on expression using entirely global
10580 variables, or it can be on local variables.
10581
10582 Watchpoints of the first kind are never auto-deleted, and
10583 even persist across program restarts. Since they can use
10584 variables from shared libraries, we need to reparse
10585 expression as libraries are loaded and unloaded.
10586
10587 Watchpoints on local variables can also change meaning as
10588 result of solib event. For example, if a watchpoint uses
10589 both a local and a global variables in expression, it's a
10590 local watchpoint, but unloading of a shared library will make
10591 the expression invalid. This is not a very common use case,
10592 but we still re-evaluate expression, to avoid surprises to
10593 the user.
10594
10595 Note that for local watchpoints, we re-evaluate it only if
10596 watchpoints frame id is still valid. If it's not, it means
10597 the watchpoint is out of scope and will be deleted soon. In
10598 fact, I'm not sure we'll ever be called in this case.
10599
10600 If a local watchpoint's frame id is still valid, then
10601 b->exp_valid_block is likewise valid, and we can safely use it.
10602
10603 Don't do anything about disabled watchpoints, since they will
10604 be reevaluated again when enabled. */
10605 update_watchpoint (b, 1 /* reparse */);
10606 break;
10607 /* We needn't really do anything to reset these, since the mask
10608 that requests them is unaffected by e.g., new libraries being
10609 loaded. */
10610 case bp_catchpoint:
10611 break;
10612
10613 default:
10614 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
10615 /* fall through */
10616 /* Delete overlay event and longjmp master breakpoints; they will be
10617 reset later by breakpoint_re_set. */
10618 case bp_overlay_event:
10619 case bp_longjmp_master:
10620 case bp_std_terminate_master:
10621 case bp_exception_master:
10622 delete_breakpoint (b);
10623 break;
10624
10625 /* This breakpoint is special, it's set up when the inferior
10626 starts and we really don't want to touch it. */
10627 case bp_shlib_event:
10628
10629 /* Like bp_shlib_event, this breakpoint type is special.
10630 Once it is set up, we do not want to touch it. */
10631 case bp_thread_event:
10632
10633 /* Keep temporary breakpoints, which can be encountered when we
10634 step over a dlopen call and SOLIB_ADD is resetting the
10635 breakpoints. Otherwise these should have been blown away via
10636 the cleanup chain or by breakpoint_init_inferior when we
10637 rerun the executable. */
10638 case bp_until:
10639 case bp_finish:
10640 case bp_watchpoint_scope:
10641 case bp_call_dummy:
10642 case bp_std_terminate:
10643 case bp_step_resume:
10644 case bp_longjmp:
10645 case bp_longjmp_resume:
10646 case bp_exception:
10647 case bp_exception_resume:
10648 case bp_jit_event:
10649 break;
10650 }
10651
10652 do_cleanups (cleanups);
10653 return 0;
10654 }
10655
10656 /* Re-set all breakpoints after symbols have been re-loaded. */
10657 void
10658 breakpoint_re_set (void)
10659 {
10660 struct breakpoint *b, *b_tmp;
10661 enum language save_language;
10662 int save_input_radix;
10663 struct cleanup *old_chain;
10664
10665 save_language = current_language->la_language;
10666 save_input_radix = input_radix;
10667 old_chain = save_current_program_space ();
10668
10669 ALL_BREAKPOINTS_SAFE (b, b_tmp)
10670 {
10671 /* Format possible error msg. */
10672 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
10673 b->number);
10674 struct cleanup *cleanups = make_cleanup (xfree, message);
10675 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
10676 do_cleanups (cleanups);
10677 }
10678 set_language (save_language);
10679 input_radix = save_input_radix;
10680
10681 jit_breakpoint_re_set ();
10682
10683 do_cleanups (old_chain);
10684
10685 create_overlay_event_breakpoint ();
10686 create_longjmp_master_breakpoint ();
10687 create_std_terminate_master_breakpoint ();
10688 create_exception_master_breakpoint ();
10689 }
10690 \f
10691 /* Reset the thread number of this breakpoint:
10692
10693 - If the breakpoint is for all threads, leave it as-is.
10694 - Else, reset it to the current thread for inferior_ptid. */
10695 void
10696 breakpoint_re_set_thread (struct breakpoint *b)
10697 {
10698 if (b->thread != -1)
10699 {
10700 if (in_thread_list (inferior_ptid))
10701 b->thread = pid_to_thread_id (inferior_ptid);
10702
10703 /* We're being called after following a fork. The new fork is
10704 selected as current, and unless this was a vfork will have a
10705 different program space from the original thread. Reset that
10706 as well. */
10707 b->loc->pspace = current_program_space;
10708 }
10709 }
10710
10711 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
10712 If from_tty is nonzero, it prints a message to that effect,
10713 which ends with a period (no newline). */
10714
10715 void
10716 set_ignore_count (int bptnum, int count, int from_tty)
10717 {
10718 struct breakpoint *b;
10719
10720 if (count < 0)
10721 count = 0;
10722
10723 ALL_BREAKPOINTS (b)
10724 if (b->number == bptnum)
10725 {
10726 if (is_tracepoint (b))
10727 {
10728 if (from_tty && count != 0)
10729 printf_filtered (_("Ignore count ignored for tracepoint %d."),
10730 bptnum);
10731 return;
10732 }
10733
10734 b->ignore_count = count;
10735 if (from_tty)
10736 {
10737 if (count == 0)
10738 printf_filtered (_("Will stop next time "
10739 "breakpoint %d is reached."),
10740 bptnum);
10741 else if (count == 1)
10742 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
10743 bptnum);
10744 else
10745 printf_filtered (_("Will ignore next %d "
10746 "crossings of breakpoint %d."),
10747 count, bptnum);
10748 }
10749 breakpoints_changed ();
10750 observer_notify_breakpoint_modified (b->number);
10751 return;
10752 }
10753
10754 error (_("No breakpoint number %d."), bptnum);
10755 }
10756
10757 /* Command to set ignore-count of breakpoint N to COUNT. */
10758
10759 static void
10760 ignore_command (char *args, int from_tty)
10761 {
10762 char *p = args;
10763 int num;
10764
10765 if (p == 0)
10766 error_no_arg (_("a breakpoint number"));
10767
10768 num = get_number (&p);
10769 if (num == 0)
10770 error (_("bad breakpoint number: '%s'"), args);
10771 if (*p == 0)
10772 error (_("Second argument (specified ignore-count) is missing."));
10773
10774 set_ignore_count (num,
10775 longest_to_int (value_as_long (parse_and_eval (p))),
10776 from_tty);
10777 if (from_tty)
10778 printf_filtered ("\n");
10779 }
10780 \f
10781 /* Call FUNCTION on each of the breakpoints
10782 whose numbers are given in ARGS. */
10783
10784 static void
10785 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
10786 void *),
10787 void *data)
10788 {
10789 char *p = args;
10790 char *p1;
10791 int num;
10792 struct breakpoint *b, *tmp;
10793 int match;
10794
10795 if (p == 0)
10796 error_no_arg (_("one or more breakpoint numbers"));
10797
10798 while (*p)
10799 {
10800 match = 0;
10801 p1 = p;
10802
10803 num = get_number_or_range (&p1);
10804 if (num == 0)
10805 {
10806 warning (_("bad breakpoint number at or near '%s'"), p);
10807 }
10808 else
10809 {
10810 ALL_BREAKPOINTS_SAFE (b, tmp)
10811 if (b->number == num)
10812 {
10813 struct breakpoint *related_breakpoint = b->related_breakpoint;
10814 match = 1;
10815 function (b, data);
10816 if (related_breakpoint)
10817 function (related_breakpoint, data);
10818 break;
10819 }
10820 if (match == 0)
10821 printf_unfiltered (_("No breakpoint number %d.\n"), num);
10822 }
10823 p = p1;
10824 }
10825 }
10826
10827 static struct bp_location *
10828 find_location_by_number (char *number)
10829 {
10830 char *dot = strchr (number, '.');
10831 char *p1;
10832 int bp_num;
10833 int loc_num;
10834 struct breakpoint *b;
10835 struct bp_location *loc;
10836
10837 *dot = '\0';
10838
10839 p1 = number;
10840 bp_num = get_number_or_range (&p1);
10841 if (bp_num == 0)
10842 error (_("Bad breakpoint number '%s'"), number);
10843
10844 ALL_BREAKPOINTS (b)
10845 if (b->number == bp_num)
10846 {
10847 break;
10848 }
10849
10850 if (!b || b->number != bp_num)
10851 error (_("Bad breakpoint number '%s'"), number);
10852
10853 p1 = dot+1;
10854 loc_num = get_number_or_range (&p1);
10855 if (loc_num == 0)
10856 error (_("Bad breakpoint location number '%s'"), number);
10857
10858 --loc_num;
10859 loc = b->loc;
10860 for (;loc_num && loc; --loc_num, loc = loc->next)
10861 ;
10862 if (!loc)
10863 error (_("Bad breakpoint location number '%s'"), dot+1);
10864
10865 return loc;
10866 }
10867
10868
10869 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
10870 If from_tty is nonzero, it prints a message to that effect,
10871 which ends with a period (no newline). */
10872
10873 void
10874 disable_breakpoint (struct breakpoint *bpt)
10875 {
10876 /* Never disable a watchpoint scope breakpoint; we want to
10877 hit them when we leave scope so we can delete both the
10878 watchpoint and its scope breakpoint at that time. */
10879 if (bpt->type == bp_watchpoint_scope)
10880 return;
10881
10882 /* You can't disable permanent breakpoints. */
10883 if (bpt->enable_state == bp_permanent)
10884 return;
10885
10886 bpt->enable_state = bp_disabled;
10887
10888 update_global_location_list (0);
10889
10890 observer_notify_breakpoint_modified (bpt->number);
10891 }
10892
10893 /* A callback for map_breakpoint_numbers that calls
10894 disable_breakpoint. */
10895
10896 static void
10897 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
10898 {
10899 disable_breakpoint (b);
10900 }
10901
10902 static void
10903 disable_command (char *args, int from_tty)
10904 {
10905 struct breakpoint *bpt;
10906
10907 if (args == 0)
10908 ALL_BREAKPOINTS (bpt)
10909 switch (bpt->type)
10910 {
10911 case bp_none:
10912 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
10913 bpt->number);
10914 continue;
10915 case bp_breakpoint:
10916 case bp_tracepoint:
10917 case bp_fast_tracepoint:
10918 case bp_static_tracepoint:
10919 case bp_catchpoint:
10920 case bp_hardware_breakpoint:
10921 case bp_watchpoint:
10922 case bp_hardware_watchpoint:
10923 case bp_read_watchpoint:
10924 case bp_access_watchpoint:
10925 disable_breakpoint (bpt);
10926 default:
10927 continue;
10928 }
10929 else if (strchr (args, '.'))
10930 {
10931 struct bp_location *loc = find_location_by_number (args);
10932 if (loc)
10933 loc->enabled = 0;
10934 update_global_location_list (0);
10935 }
10936 else
10937 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
10938 }
10939
10940 static void
10941 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
10942 {
10943 int target_resources_ok;
10944
10945 if (bpt->type == bp_hardware_breakpoint)
10946 {
10947 int i;
10948 i = hw_breakpoint_used_count ();
10949 target_resources_ok =
10950 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10951 i + 1, 0);
10952 if (target_resources_ok == 0)
10953 error (_("No hardware breakpoint support in the target."));
10954 else if (target_resources_ok < 0)
10955 error (_("Hardware breakpoints used exceeds limit."));
10956 }
10957
10958 if (is_watchpoint (bpt))
10959 {
10960 struct gdb_exception e;
10961
10962 TRY_CATCH (e, RETURN_MASK_ALL)
10963 {
10964 update_watchpoint (bpt, 1 /* reparse */);
10965 }
10966 if (e.reason < 0)
10967 {
10968 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
10969 bpt->number);
10970 return;
10971 }
10972 }
10973
10974 if (bpt->enable_state != bp_permanent)
10975 bpt->enable_state = bp_enabled;
10976 bpt->disposition = disposition;
10977 update_global_location_list (1);
10978 breakpoints_changed ();
10979
10980 observer_notify_breakpoint_modified (bpt->number);
10981 }
10982
10983
10984 void
10985 enable_breakpoint (struct breakpoint *bpt)
10986 {
10987 do_enable_breakpoint (bpt, bpt->disposition);
10988 }
10989
10990 /* A callback for map_breakpoint_numbers that calls
10991 enable_breakpoint. */
10992
10993 static void
10994 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
10995 {
10996 enable_breakpoint (b);
10997 }
10998
10999 /* The enable command enables the specified breakpoints (or all defined
11000 breakpoints) so they once again become (or continue to be) effective
11001 in stopping the inferior. */
11002
11003 static void
11004 enable_command (char *args, int from_tty)
11005 {
11006 struct breakpoint *bpt;
11007
11008 if (args == 0)
11009 ALL_BREAKPOINTS (bpt)
11010 switch (bpt->type)
11011 {
11012 case bp_none:
11013 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
11014 bpt->number);
11015 continue;
11016 case bp_breakpoint:
11017 case bp_tracepoint:
11018 case bp_fast_tracepoint:
11019 case bp_static_tracepoint:
11020 case bp_catchpoint:
11021 case bp_hardware_breakpoint:
11022 case bp_watchpoint:
11023 case bp_hardware_watchpoint:
11024 case bp_read_watchpoint:
11025 case bp_access_watchpoint:
11026 enable_breakpoint (bpt);
11027 default:
11028 continue;
11029 }
11030 else if (strchr (args, '.'))
11031 {
11032 struct bp_location *loc = find_location_by_number (args);
11033 if (loc)
11034 loc->enabled = 1;
11035 update_global_location_list (1);
11036 }
11037 else
11038 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
11039 }
11040
11041 static void
11042 enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
11043 {
11044 do_enable_breakpoint (bpt, disp_disable);
11045 }
11046
11047 static void
11048 enable_once_command (char *args, int from_tty)
11049 {
11050 map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
11051 }
11052
11053 static void
11054 enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
11055 {
11056 do_enable_breakpoint (bpt, disp_del);
11057 }
11058
11059 static void
11060 enable_delete_command (char *args, int from_tty)
11061 {
11062 map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
11063 }
11064 \f
11065 static void
11066 set_breakpoint_cmd (char *args, int from_tty)
11067 {
11068 }
11069
11070 static void
11071 show_breakpoint_cmd (char *args, int from_tty)
11072 {
11073 }
11074
11075 /* Invalidate last known value of any hardware watchpoint if
11076 the memory which that value represents has been written to by
11077 GDB itself. */
11078
11079 static void
11080 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11081 const bfd_byte *data)
11082 {
11083 struct breakpoint *bp;
11084
11085 ALL_BREAKPOINTS (bp)
11086 if (bp->enable_state == bp_enabled
11087 && bp->type == bp_hardware_watchpoint
11088 && bp->val_valid && bp->val)
11089 {
11090 struct bp_location *loc;
11091
11092 for (loc = bp->loc; loc != NULL; loc = loc->next)
11093 if (loc->loc_type == bp_loc_hardware_watchpoint
11094 && loc->address + loc->length > addr
11095 && addr + len > loc->address)
11096 {
11097 value_free (bp->val);
11098 bp->val = NULL;
11099 bp->val_valid = 0;
11100 }
11101 }
11102 }
11103
11104 /* Use default_breakpoint_'s, or nothing if they aren't valid. */
11105
11106 struct symtabs_and_lines
11107 decode_line_spec_1 (char *string, int funfirstline)
11108 {
11109 struct symtabs_and_lines sals;
11110
11111 if (string == 0)
11112 error (_("Empty line specification."));
11113 if (default_breakpoint_valid)
11114 sals = decode_line_1 (&string, funfirstline,
11115 default_breakpoint_symtab,
11116 default_breakpoint_line,
11117 (char ***) NULL, NULL);
11118 else
11119 sals = decode_line_1 (&string, funfirstline,
11120 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
11121 if (*string)
11122 error (_("Junk at end of line specification: %s"), string);
11123 return sals;
11124 }
11125
11126 /* Create and insert a raw software breakpoint at PC. Return an
11127 identifier, which should be used to remove the breakpoint later.
11128 In general, places which call this should be using something on the
11129 breakpoint chain instead; this function should be eliminated
11130 someday. */
11131
11132 void *
11133 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11134 struct address_space *aspace, CORE_ADDR pc)
11135 {
11136 struct bp_target_info *bp_tgt;
11137
11138 bp_tgt = XZALLOC (struct bp_target_info);
11139
11140 bp_tgt->placed_address_space = aspace;
11141 bp_tgt->placed_address = pc;
11142
11143 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
11144 {
11145 /* Could not insert the breakpoint. */
11146 xfree (bp_tgt);
11147 return NULL;
11148 }
11149
11150 return bp_tgt;
11151 }
11152
11153 /* Remove a breakpoint BP inserted by
11154 deprecated_insert_raw_breakpoint. */
11155
11156 int
11157 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
11158 {
11159 struct bp_target_info *bp_tgt = bp;
11160 int ret;
11161
11162 ret = target_remove_breakpoint (gdbarch, bp_tgt);
11163 xfree (bp_tgt);
11164
11165 return ret;
11166 }
11167
11168 /* One (or perhaps two) breakpoints used for software single
11169 stepping. */
11170
11171 static void *single_step_breakpoints[2];
11172 static struct gdbarch *single_step_gdbarch[2];
11173
11174 /* Create and insert a breakpoint for software single step. */
11175
11176 void
11177 insert_single_step_breakpoint (struct gdbarch *gdbarch,
11178 struct address_space *aspace,
11179 CORE_ADDR next_pc)
11180 {
11181 void **bpt_p;
11182
11183 if (single_step_breakpoints[0] == NULL)
11184 {
11185 bpt_p = &single_step_breakpoints[0];
11186 single_step_gdbarch[0] = gdbarch;
11187 }
11188 else
11189 {
11190 gdb_assert (single_step_breakpoints[1] == NULL);
11191 bpt_p = &single_step_breakpoints[1];
11192 single_step_gdbarch[1] = gdbarch;
11193 }
11194
11195 /* NOTE drow/2006-04-11: A future improvement to this function would
11196 be to only create the breakpoints once, and actually put them on
11197 the breakpoint chain. That would let us use set_raw_breakpoint.
11198 We could adjust the addresses each time they were needed. Doing
11199 this requires corresponding changes elsewhere where single step
11200 breakpoints are handled, however. So, for now, we use this. */
11201
11202 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
11203 if (*bpt_p == NULL)
11204 error (_("Could not insert single-step breakpoint at %s"),
11205 paddress (gdbarch, next_pc));
11206 }
11207
11208 /* Check if the breakpoints used for software single stepping
11209 were inserted or not. */
11210
11211 int
11212 single_step_breakpoints_inserted (void)
11213 {
11214 return (single_step_breakpoints[0] != NULL
11215 || single_step_breakpoints[1] != NULL);
11216 }
11217
11218 /* Remove and delete any breakpoints used for software single step. */
11219
11220 void
11221 remove_single_step_breakpoints (void)
11222 {
11223 gdb_assert (single_step_breakpoints[0] != NULL);
11224
11225 /* See insert_single_step_breakpoint for more about this deprecated
11226 call. */
11227 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11228 single_step_breakpoints[0]);
11229 single_step_gdbarch[0] = NULL;
11230 single_step_breakpoints[0] = NULL;
11231
11232 if (single_step_breakpoints[1] != NULL)
11233 {
11234 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11235 single_step_breakpoints[1]);
11236 single_step_gdbarch[1] = NULL;
11237 single_step_breakpoints[1] = NULL;
11238 }
11239 }
11240
11241 /* Delete software single step breakpoints without removing them from
11242 the inferior. This is intended to be used if the inferior's address
11243 space where they were inserted is already gone, e.g. after exit or
11244 exec. */
11245
11246 void
11247 cancel_single_step_breakpoints (void)
11248 {
11249 int i;
11250
11251 for (i = 0; i < 2; i++)
11252 if (single_step_breakpoints[i])
11253 {
11254 xfree (single_step_breakpoints[i]);
11255 single_step_breakpoints[i] = NULL;
11256 single_step_gdbarch[i] = NULL;
11257 }
11258 }
11259
11260 /* Detach software single-step breakpoints from INFERIOR_PTID without
11261 removing them. */
11262
11263 static void
11264 detach_single_step_breakpoints (void)
11265 {
11266 int i;
11267
11268 for (i = 0; i < 2; i++)
11269 if (single_step_breakpoints[i])
11270 target_remove_breakpoint (single_step_gdbarch[i],
11271 single_step_breakpoints[i]);
11272 }
11273
11274 /* Check whether a software single-step breakpoint is inserted at
11275 PC. */
11276
11277 static int
11278 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11279 CORE_ADDR pc)
11280 {
11281 int i;
11282
11283 for (i = 0; i < 2; i++)
11284 {
11285 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
11286 if (bp_tgt
11287 && breakpoint_address_match (bp_tgt->placed_address_space,
11288 bp_tgt->placed_address,
11289 aspace, pc))
11290 return 1;
11291 }
11292
11293 return 0;
11294 }
11295
11296 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
11297 non-zero otherwise. */
11298 static int
11299 is_syscall_catchpoint_enabled (struct breakpoint *bp)
11300 {
11301 if (syscall_catchpoint_p (bp)
11302 && bp->enable_state != bp_disabled
11303 && bp->enable_state != bp_call_disabled)
11304 return 1;
11305 else
11306 return 0;
11307 }
11308
11309 int
11310 catch_syscall_enabled (void)
11311 {
11312 struct inferior *inf = current_inferior ();
11313
11314 return inf->total_syscalls_count != 0;
11315 }
11316
11317 int
11318 catching_syscall_number (int syscall_number)
11319 {
11320 struct breakpoint *bp;
11321
11322 ALL_BREAKPOINTS (bp)
11323 if (is_syscall_catchpoint_enabled (bp))
11324 {
11325 if (bp->syscalls_to_be_caught)
11326 {
11327 int i, iter;
11328 for (i = 0;
11329 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11330 i++)
11331 if (syscall_number == iter)
11332 return 1;
11333 }
11334 else
11335 return 1;
11336 }
11337
11338 return 0;
11339 }
11340
11341 /* Complete syscall names. Used by "catch syscall". */
11342 static char **
11343 catch_syscall_completer (struct cmd_list_element *cmd,
11344 char *text, char *word)
11345 {
11346 const char **list = get_syscall_names ();
11347
11348 return (list == NULL) ? NULL : complete_on_enum (list, text, word);
11349 }
11350
11351 /* Tracepoint-specific operations. */
11352
11353 /* Set tracepoint count to NUM. */
11354 static void
11355 set_tracepoint_count (int num)
11356 {
11357 tracepoint_count = num;
11358 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
11359 }
11360
11361 void
11362 trace_command (char *arg, int from_tty)
11363 {
11364 if (create_breakpoint (get_current_arch (),
11365 arg,
11366 NULL, 0, 1 /* parse arg */,
11367 0 /* tempflag */,
11368 bp_tracepoint /* type_wanted */,
11369 0 /* Ignore count */,
11370 pending_break_support,
11371 NULL,
11372 from_tty,
11373 1 /* enabled */,
11374 0 /* internal */))
11375 set_tracepoint_count (breakpoint_count);
11376 }
11377
11378 void
11379 ftrace_command (char *arg, int from_tty)
11380 {
11381 if (create_breakpoint (get_current_arch (),
11382 arg,
11383 NULL, 0, 1 /* parse arg */,
11384 0 /* tempflag */,
11385 bp_fast_tracepoint /* type_wanted */,
11386 0 /* Ignore count */,
11387 pending_break_support,
11388 NULL,
11389 from_tty,
11390 1 /* enabled */,
11391 0 /* internal */))
11392 set_tracepoint_count (breakpoint_count);
11393 }
11394
11395 /* strace command implementation. Creates a static tracepoint. */
11396
11397 void
11398 strace_command (char *arg, int from_tty)
11399 {
11400 if (create_breakpoint (get_current_arch (),
11401 arg,
11402 NULL, 0, 1 /* parse arg */,
11403 0 /* tempflag */,
11404 bp_static_tracepoint /* type_wanted */,
11405 0 /* Ignore count */,
11406 pending_break_support,
11407 NULL,
11408 from_tty,
11409 1 /* enabled */,
11410 0 /* internal */))
11411 set_tracepoint_count (breakpoint_count);
11412 }
11413
11414 /* Set up a fake reader function that gets command lines from a linked
11415 list that was acquired during tracepoint uploading. */
11416
11417 static struct uploaded_tp *this_utp;
11418 static int next_cmd;
11419
11420 static char *
11421 read_uploaded_action (void)
11422 {
11423 char *rslt;
11424
11425 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
11426
11427 next_cmd++;
11428
11429 return rslt;
11430 }
11431
11432 /* Given information about a tracepoint as recorded on a target (which
11433 can be either a live system or a trace file), attempt to create an
11434 equivalent GDB tracepoint. This is not a reliable process, since
11435 the target does not necessarily have all the information used when
11436 the tracepoint was originally defined. */
11437
11438 struct breakpoint *
11439 create_tracepoint_from_upload (struct uploaded_tp *utp)
11440 {
11441 char *addr_str, small_buf[100];
11442 struct breakpoint *tp;
11443
11444 if (utp->at_string)
11445 addr_str = utp->at_string;
11446 else
11447 {
11448 /* In the absence of a source location, fall back to raw
11449 address. Since there is no way to confirm that the address
11450 means the same thing as when the trace was started, warn the
11451 user. */
11452 warning (_("Uploaded tracepoint %d has no "
11453 "source location, using raw address"),
11454 utp->number);
11455 sprintf (small_buf, "*%s", hex_string (utp->addr));
11456 addr_str = small_buf;
11457 }
11458
11459 /* There's not much we can do with a sequence of bytecodes. */
11460 if (utp->cond && !utp->cond_string)
11461 warning (_("Uploaded tracepoint %d condition "
11462 "has no source form, ignoring it"),
11463 utp->number);
11464
11465 if (!create_breakpoint (get_current_arch (),
11466 addr_str,
11467 utp->cond_string, -1, 0 /* parse cond/thread */,
11468 0 /* tempflag */,
11469 utp->type /* type_wanted */,
11470 0 /* Ignore count */,
11471 pending_break_support,
11472 NULL,
11473 0 /* from_tty */,
11474 utp->enabled /* enabled */,
11475 0 /* internal */))
11476 return NULL;
11477
11478 set_tracepoint_count (breakpoint_count);
11479
11480 /* Get the tracepoint we just created. */
11481 tp = get_tracepoint (tracepoint_count);
11482 gdb_assert (tp != NULL);
11483
11484 if (utp->pass > 0)
11485 {
11486 sprintf (small_buf, "%d %d", utp->pass, tp->number);
11487
11488 trace_pass_command (small_buf, 0);
11489 }
11490
11491 /* If we have uploaded versions of the original commands, set up a
11492 special-purpose "reader" function and call the usual command line
11493 reader, then pass the result to the breakpoint command-setting
11494 function. */
11495 if (!VEC_empty (char_ptr, utp->cmd_strings))
11496 {
11497 struct command_line *cmd_list;
11498
11499 this_utp = utp;
11500 next_cmd = 0;
11501
11502 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
11503
11504 breakpoint_set_commands (tp, cmd_list);
11505 }
11506 else if (!VEC_empty (char_ptr, utp->actions)
11507 || !VEC_empty (char_ptr, utp->step_actions))
11508 warning (_("Uploaded tracepoint %d actions "
11509 "have no source form, ignoring them"),
11510 utp->number);
11511
11512 return tp;
11513 }
11514
11515 /* Print information on tracepoint number TPNUM_EXP, or all if
11516 omitted. */
11517
11518 static void
11519 tracepoints_info (char *args, int from_tty)
11520 {
11521 int num_printed;
11522
11523 num_printed = breakpoint_1 (args, 0, is_tracepoint);
11524
11525 if (num_printed == 0)
11526 {
11527 if (args == NULL || *args == '\0')
11528 ui_out_message (uiout, 0, "No tracepoints.\n");
11529 else
11530 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
11531 }
11532
11533 default_collect_info ();
11534 }
11535
11536 /* The 'enable trace' command enables tracepoints.
11537 Not supported by all targets. */
11538 static void
11539 enable_trace_command (char *args, int from_tty)
11540 {
11541 enable_command (args, from_tty);
11542 }
11543
11544 /* The 'disable trace' command disables tracepoints.
11545 Not supported by all targets. */
11546 static void
11547 disable_trace_command (char *args, int from_tty)
11548 {
11549 disable_command (args, from_tty);
11550 }
11551
11552 /* Remove a tracepoint (or all if no argument). */
11553 static void
11554 delete_trace_command (char *arg, int from_tty)
11555 {
11556 struct breakpoint *b, *b_tmp;
11557
11558 dont_repeat ();
11559
11560 if (arg == 0)
11561 {
11562 int breaks_to_delete = 0;
11563
11564 /* Delete all breakpoints if no argument.
11565 Do not delete internal or call-dummy breakpoints, these
11566 have to be deleted with an explicit breakpoint number
11567 argument. */
11568 ALL_TRACEPOINTS (b)
11569 {
11570 if (b->number >= 0)
11571 {
11572 breaks_to_delete = 1;
11573 break;
11574 }
11575 }
11576
11577 /* Ask user only if there are some breakpoints to delete. */
11578 if (!from_tty
11579 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
11580 {
11581 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11582 {
11583 if (is_tracepoint (b)
11584 && b->number >= 0)
11585 delete_breakpoint (b);
11586 }
11587 }
11588 }
11589 else
11590 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
11591 }
11592
11593 /* Set passcount for tracepoint.
11594
11595 First command argument is passcount, second is tracepoint number.
11596 If tracepoint number omitted, apply to most recently defined.
11597 Also accepts special argument "all". */
11598
11599 static void
11600 trace_pass_command (char *args, int from_tty)
11601 {
11602 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
11603 unsigned int count;
11604 int all = 0;
11605
11606 if (args == 0 || *args == 0)
11607 error (_("passcount command requires an "
11608 "argument (count + optional TP num)"));
11609
11610 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
11611
11612 while (*args && isspace ((int) *args))
11613 args++;
11614
11615 if (*args && strncasecmp (args, "all", 3) == 0)
11616 {
11617 args += 3; /* Skip special argument "all". */
11618 all = 1;
11619 if (*args)
11620 error (_("Junk at end of arguments."));
11621 }
11622 else
11623 t1 = get_tracepoint_by_number (&args, 1, 1);
11624
11625 do
11626 {
11627 if (t1)
11628 {
11629 ALL_TRACEPOINTS (t2)
11630 if (t1 == (struct breakpoint *) -1 || t1 == t2)
11631 {
11632 t2->pass_count = count;
11633 observer_notify_tracepoint_modified (t2->number);
11634 if (from_tty)
11635 printf_filtered (_("Setting tracepoint %d's "
11636 "passcount to %d\n"),
11637 t2->number, count);
11638 }
11639 if (! all && *args)
11640 t1 = get_tracepoint_by_number (&args, 1, 0);
11641 }
11642 }
11643 while (*args);
11644 }
11645
11646 struct breakpoint *
11647 get_tracepoint (int num)
11648 {
11649 struct breakpoint *t;
11650
11651 ALL_TRACEPOINTS (t)
11652 if (t->number == num)
11653 return t;
11654
11655 return NULL;
11656 }
11657
11658 /* Find the tracepoint with the given target-side number (which may be
11659 different from the tracepoint number after disconnecting and
11660 reconnecting). */
11661
11662 struct breakpoint *
11663 get_tracepoint_by_number_on_target (int num)
11664 {
11665 struct breakpoint *t;
11666
11667 ALL_TRACEPOINTS (t)
11668 if (t->number_on_target == num)
11669 return t;
11670
11671 return NULL;
11672 }
11673
11674 /* Utility: parse a tracepoint number and look it up in the list.
11675 If MULTI_P is true, there might be a range of tracepoints in ARG.
11676 if OPTIONAL_P is true, then if the argument is missing, the most
11677 recent tracepoint (tracepoint_count) is returned. */
11678 struct breakpoint *
11679 get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
11680 {
11681 extern int tracepoint_count;
11682 struct breakpoint *t;
11683 int tpnum;
11684 char *instring = arg == NULL ? NULL : *arg;
11685
11686 if (arg == NULL || *arg == NULL || ! **arg)
11687 {
11688 if (optional_p)
11689 tpnum = tracepoint_count;
11690 else
11691 error_no_arg (_("tracepoint number"));
11692 }
11693 else
11694 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
11695
11696 if (tpnum <= 0)
11697 {
11698 if (instring && *instring)
11699 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
11700 instring);
11701 else
11702 printf_filtered (_("Tracepoint argument missing "
11703 "and no previous tracepoint\n"));
11704 return NULL;
11705 }
11706
11707 ALL_TRACEPOINTS (t)
11708 if (t->number == tpnum)
11709 {
11710 return t;
11711 }
11712
11713 /* FIXME: if we are in the middle of a range we don't want to give
11714 a message. The current interface to get_number_or_range doesn't
11715 allow us to discover this. */
11716 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
11717 return NULL;
11718 }
11719
11720 /* Save information on user settable breakpoints (watchpoints, etc) to
11721 a new script file named FILENAME. If FILTER is non-NULL, call it
11722 on each breakpoint and only include the ones for which it returns
11723 non-zero. */
11724
11725 static void
11726 save_breakpoints (char *filename, int from_tty,
11727 int (*filter) (const struct breakpoint *))
11728 {
11729 struct breakpoint *tp;
11730 int any = 0;
11731 char *pathname;
11732 struct cleanup *cleanup;
11733 struct ui_file *fp;
11734 int extra_trace_bits = 0;
11735
11736 if (filename == 0 || *filename == 0)
11737 error (_("Argument required (file name in which to save)"));
11738
11739 /* See if we have anything to save. */
11740 ALL_BREAKPOINTS (tp)
11741 {
11742 /* Skip internal and momentary breakpoints. */
11743 if (!user_breakpoint_p (tp))
11744 continue;
11745
11746 /* If we have a filter, only save the breakpoints it accepts. */
11747 if (filter && !filter (tp))
11748 continue;
11749
11750 any = 1;
11751
11752 if (is_tracepoint (tp))
11753 {
11754 extra_trace_bits = 1;
11755
11756 /* We can stop searching. */
11757 break;
11758 }
11759 }
11760
11761 if (!any)
11762 {
11763 warning (_("Nothing to save."));
11764 return;
11765 }
11766
11767 pathname = tilde_expand (filename);
11768 cleanup = make_cleanup (xfree, pathname);
11769 fp = gdb_fopen (pathname, "w");
11770 if (!fp)
11771 error (_("Unable to open file '%s' for saving (%s)"),
11772 filename, safe_strerror (errno));
11773 make_cleanup_ui_file_delete (fp);
11774
11775 if (extra_trace_bits)
11776 save_trace_state_variables (fp);
11777
11778 ALL_BREAKPOINTS (tp)
11779 {
11780 /* Skip internal and momentary breakpoints. */
11781 if (!user_breakpoint_p (tp))
11782 continue;
11783
11784 /* If we have a filter, only save the breakpoints it accepts. */
11785 if (filter && !filter (tp))
11786 continue;
11787
11788 if (tp->ops != NULL)
11789 (tp->ops->print_recreate) (tp, fp);
11790 else
11791 {
11792 if (tp->type == bp_fast_tracepoint)
11793 fprintf_unfiltered (fp, "ftrace");
11794 if (tp->type == bp_static_tracepoint)
11795 fprintf_unfiltered (fp, "strace");
11796 else if (tp->type == bp_tracepoint)
11797 fprintf_unfiltered (fp, "trace");
11798 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11799 fprintf_unfiltered (fp, "tbreak");
11800 else if (tp->type == bp_breakpoint)
11801 fprintf_unfiltered (fp, "break");
11802 else if (tp->type == bp_hardware_breakpoint
11803 && tp->disposition == disp_del)
11804 fprintf_unfiltered (fp, "thbreak");
11805 else if (tp->type == bp_hardware_breakpoint)
11806 fprintf_unfiltered (fp, "hbreak");
11807 else if (tp->type == bp_watchpoint)
11808 fprintf_unfiltered (fp, "watch");
11809 else if (tp->type == bp_hardware_watchpoint)
11810 fprintf_unfiltered (fp, "watch");
11811 else if (tp->type == bp_read_watchpoint)
11812 fprintf_unfiltered (fp, "rwatch");
11813 else if (tp->type == bp_access_watchpoint)
11814 fprintf_unfiltered (fp, "awatch");
11815 else
11816 internal_error (__FILE__, __LINE__,
11817 _("unhandled breakpoint type %d"), (int) tp->type);
11818
11819 if (tp->exp_string)
11820 fprintf_unfiltered (fp, " %s", tp->exp_string);
11821 else if (tp->addr_string)
11822 fprintf_unfiltered (fp, " %s", tp->addr_string);
11823 else
11824 {
11825 char tmp[40];
11826
11827 sprintf_vma (tmp, tp->loc->address);
11828 fprintf_unfiltered (fp, " *0x%s", tmp);
11829 }
11830 }
11831
11832 if (tp->thread != -1)
11833 fprintf_unfiltered (fp, " thread %d", tp->thread);
11834
11835 if (tp->task != 0)
11836 fprintf_unfiltered (fp, " task %d", tp->task);
11837
11838 fprintf_unfiltered (fp, "\n");
11839
11840 /* Note, we can't rely on tp->number for anything, as we can't
11841 assume the recreated breakpoint numbers will match. Use $bpnum
11842 instead. */
11843
11844 if (tp->cond_string)
11845 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
11846
11847 if (tp->ignore_count)
11848 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
11849
11850 if (tp->pass_count)
11851 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
11852
11853 if (tp->commands)
11854 {
11855 volatile struct gdb_exception ex;
11856
11857 fprintf_unfiltered (fp, " commands\n");
11858
11859 ui_out_redirect (uiout, fp);
11860 TRY_CATCH (ex, RETURN_MASK_ALL)
11861 {
11862 print_command_lines (uiout, tp->commands->commands, 2);
11863 }
11864 ui_out_redirect (uiout, NULL);
11865
11866 if (ex.reason < 0)
11867 throw_exception (ex);
11868
11869 fprintf_unfiltered (fp, " end\n");
11870 }
11871
11872 if (tp->enable_state == bp_disabled)
11873 fprintf_unfiltered (fp, "disable\n");
11874
11875 /* If this is a multi-location breakpoint, check if the locations
11876 should be individually disabled. Watchpoint locations are
11877 special, and not user visible. */
11878 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
11879 {
11880 struct bp_location *loc;
11881 int n = 1;
11882
11883 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
11884 if (!loc->enabled)
11885 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
11886 }
11887 }
11888
11889 if (extra_trace_bits && *default_collect)
11890 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
11891
11892 do_cleanups (cleanup);
11893 if (from_tty)
11894 printf_filtered (_("Saved to file '%s'.\n"), filename);
11895 }
11896
11897 /* The `save breakpoints' command. */
11898
11899 static void
11900 save_breakpoints_command (char *args, int from_tty)
11901 {
11902 save_breakpoints (args, from_tty, NULL);
11903 }
11904
11905 /* The `save tracepoints' command. */
11906
11907 static void
11908 save_tracepoints_command (char *args, int from_tty)
11909 {
11910 save_breakpoints (args, from_tty, is_tracepoint);
11911 }
11912
11913 /* Create a vector of all tracepoints. */
11914
11915 VEC(breakpoint_p) *
11916 all_tracepoints ()
11917 {
11918 VEC(breakpoint_p) *tp_vec = 0;
11919 struct breakpoint *tp;
11920
11921 ALL_TRACEPOINTS (tp)
11922 {
11923 VEC_safe_push (breakpoint_p, tp_vec, tp);
11924 }
11925
11926 return tp_vec;
11927 }
11928
11929 \f
11930 /* This help string is used for the break, hbreak, tbreak and thbreak
11931 commands. It is defined as a macro to prevent duplication.
11932 COMMAND should be a string constant containing the name of the
11933 command. */
11934 #define BREAK_ARGS_HELP(command) \
11935 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
11936 LOCATION may be a line number, function name, or \"*\" and an address.\n\
11937 If a line number is specified, break at start of code for that line.\n\
11938 If a function is specified, break at start of code for that function.\n\
11939 If an address is specified, break at that exact address.\n\
11940 With no LOCATION, uses current execution address of the selected\n\
11941 stack frame. This is useful for breaking on return to a stack frame.\n\
11942 \n\
11943 THREADNUM is the number from \"info threads\".\n\
11944 CONDITION is a boolean expression.\n\
11945 \n\
11946 Multiple breakpoints at one place are permitted, and useful if their\n\
11947 conditions are different.\n\
11948 \n\
11949 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
11950
11951 /* List of subcommands for "catch". */
11952 static struct cmd_list_element *catch_cmdlist;
11953
11954 /* List of subcommands for "tcatch". */
11955 static struct cmd_list_element *tcatch_cmdlist;
11956
11957 /* Like add_cmd, but add the command to both the "catch" and "tcatch"
11958 lists, and pass some additional user data to the command function. */
11959 static void
11960 add_catch_command (char *name, char *docstring,
11961 void (*sfunc) (char *args, int from_tty,
11962 struct cmd_list_element *command),
11963 char **(*completer) (struct cmd_list_element *cmd,
11964 char *text, char *word),
11965 void *user_data_catch,
11966 void *user_data_tcatch)
11967 {
11968 struct cmd_list_element *command;
11969
11970 command = add_cmd (name, class_breakpoint, NULL, docstring,
11971 &catch_cmdlist);
11972 set_cmd_sfunc (command, sfunc);
11973 set_cmd_context (command, user_data_catch);
11974 set_cmd_completer (command, completer);
11975
11976 command = add_cmd (name, class_breakpoint, NULL, docstring,
11977 &tcatch_cmdlist);
11978 set_cmd_sfunc (command, sfunc);
11979 set_cmd_context (command, user_data_tcatch);
11980 set_cmd_completer (command, completer);
11981 }
11982
11983 static void
11984 clear_syscall_counts (struct inferior *inf)
11985 {
11986 inf->total_syscalls_count = 0;
11987 inf->any_syscall_count = 0;
11988 VEC_free (int, inf->syscalls_counts);
11989 }
11990
11991 static void
11992 save_command (char *arg, int from_tty)
11993 {
11994 printf_unfiltered (_("\"save\" must be followed by "
11995 "the name of a save subcommand.\n"));
11996 help_list (save_cmdlist, "save ", -1, gdb_stdout);
11997 }
11998
11999 struct breakpoint *
12000 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12001 void *data)
12002 {
12003 struct breakpoint *b, *b_tmp;
12004
12005 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12006 {
12007 if ((*callback) (b, data))
12008 return b;
12009 }
12010
12011 return NULL;
12012 }
12013
12014 void
12015 _initialize_breakpoint (void)
12016 {
12017 struct cmd_list_element *c;
12018
12019 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
12020 observer_attach_inferior_exit (clear_syscall_counts);
12021 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
12022
12023 breakpoint_objfile_key = register_objfile_data ();
12024
12025 breakpoint_chain = 0;
12026 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
12027 before a breakpoint is set. */
12028 breakpoint_count = 0;
12029
12030 tracepoint_count = 0;
12031
12032 add_com ("ignore", class_breakpoint, ignore_command, _("\
12033 Set ignore-count of breakpoint number N to COUNT.\n\
12034 Usage is `ignore N COUNT'."));
12035 if (xdb_commands)
12036 add_com_alias ("bc", "ignore", class_breakpoint, 1);
12037
12038 add_com ("commands", class_breakpoint, commands_command, _("\
12039 Set commands to be executed when a breakpoint is hit.\n\
12040 Give breakpoint number as argument after \"commands\".\n\
12041 With no argument, the targeted breakpoint is the last one set.\n\
12042 The commands themselves follow starting on the next line.\n\
12043 Type a line containing \"end\" to indicate the end of them.\n\
12044 Give \"silent\" as the first line to make the breakpoint silent;\n\
12045 then no output is printed when it is hit, except what the commands print."));
12046
12047 add_com ("condition", class_breakpoint, condition_command, _("\
12048 Specify breakpoint number N to break only if COND is true.\n\
12049 Usage is `condition N COND', where N is an integer and COND is an\n\
12050 expression to be evaluated whenever breakpoint N is reached."));
12051
12052 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
12053 Set a temporary breakpoint.\n\
12054 Like \"break\" except the breakpoint is only temporary,\n\
12055 so it will be deleted when hit. Equivalent to \"break\" followed\n\
12056 by using \"enable delete\" on the breakpoint number.\n\
12057 \n"
12058 BREAK_ARGS_HELP ("tbreak")));
12059 set_cmd_completer (c, location_completer);
12060
12061 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
12062 Set a hardware assisted breakpoint.\n\
12063 Like \"break\" except the breakpoint requires hardware support,\n\
12064 some target hardware may not have this support.\n\
12065 \n"
12066 BREAK_ARGS_HELP ("hbreak")));
12067 set_cmd_completer (c, location_completer);
12068
12069 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
12070 Set a temporary hardware assisted breakpoint.\n\
12071 Like \"hbreak\" except the breakpoint is only temporary,\n\
12072 so it will be deleted when hit.\n\
12073 \n"
12074 BREAK_ARGS_HELP ("thbreak")));
12075 set_cmd_completer (c, location_completer);
12076
12077 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12078 Enable some breakpoints.\n\
12079 Give breakpoint numbers (separated by spaces) as arguments.\n\
12080 With no subcommand, breakpoints are enabled until you command otherwise.\n\
12081 This is used to cancel the effect of the \"disable\" command.\n\
12082 With a subcommand you can enable temporarily."),
12083 &enablelist, "enable ", 1, &cmdlist);
12084 if (xdb_commands)
12085 add_com ("ab", class_breakpoint, enable_command, _("\
12086 Enable some breakpoints.\n\
12087 Give breakpoint numbers (separated by spaces) as arguments.\n\
12088 With no subcommand, breakpoints are enabled until you command otherwise.\n\
12089 This is used to cancel the effect of the \"disable\" command.\n\
12090 With a subcommand you can enable temporarily."));
12091
12092 add_com_alias ("en", "enable", class_breakpoint, 1);
12093
12094 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
12095 Enable some breakpoints.\n\
12096 Give breakpoint numbers (separated by spaces) as arguments.\n\
12097 This is used to cancel the effect of the \"disable\" command.\n\
12098 May be abbreviated to simply \"enable\".\n"),
12099 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
12100
12101 add_cmd ("once", no_class, enable_once_command, _("\
12102 Enable breakpoints for one hit. Give breakpoint numbers.\n\
12103 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
12104 &enablebreaklist);
12105
12106 add_cmd ("delete", no_class, enable_delete_command, _("\
12107 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12108 If a breakpoint is hit while enabled in this fashion, it is deleted."),
12109 &enablebreaklist);
12110
12111 add_cmd ("delete", no_class, enable_delete_command, _("\
12112 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12113 If a breakpoint is hit while enabled in this fashion, it is deleted."),
12114 &enablelist);
12115
12116 add_cmd ("once", no_class, enable_once_command, _("\
12117 Enable breakpoints for one hit. Give breakpoint numbers.\n\
12118 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
12119 &enablelist);
12120
12121 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12122 Disable some breakpoints.\n\
12123 Arguments are breakpoint numbers with spaces in between.\n\
12124 To disable all breakpoints, give no argument.\n\
12125 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
12126 &disablelist, "disable ", 1, &cmdlist);
12127 add_com_alias ("dis", "disable", class_breakpoint, 1);
12128 add_com_alias ("disa", "disable", class_breakpoint, 1);
12129 if (xdb_commands)
12130 add_com ("sb", class_breakpoint, disable_command, _("\
12131 Disable some breakpoints.\n\
12132 Arguments are breakpoint numbers with spaces in between.\n\
12133 To disable all breakpoints, give no argument.\n\
12134 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
12135
12136 add_cmd ("breakpoints", class_alias, disable_command, _("\
12137 Disable some breakpoints.\n\
12138 Arguments are breakpoint numbers with spaces in between.\n\
12139 To disable all breakpoints, give no argument.\n\
12140 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
12141 This command may be abbreviated \"disable\"."),
12142 &disablelist);
12143
12144 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12145 Delete some breakpoints or auto-display expressions.\n\
12146 Arguments are breakpoint numbers with spaces in between.\n\
12147 To delete all breakpoints, give no argument.\n\
12148 \n\
12149 Also a prefix command for deletion of other GDB objects.\n\
12150 The \"unset\" command is also an alias for \"delete\"."),
12151 &deletelist, "delete ", 1, &cmdlist);
12152 add_com_alias ("d", "delete", class_breakpoint, 1);
12153 add_com_alias ("del", "delete", class_breakpoint, 1);
12154 if (xdb_commands)
12155 add_com ("db", class_breakpoint, delete_command, _("\
12156 Delete some breakpoints.\n\
12157 Arguments are breakpoint numbers with spaces in between.\n\
12158 To delete all breakpoints, give no argument.\n"));
12159
12160 add_cmd ("breakpoints", class_alias, delete_command, _("\
12161 Delete some breakpoints or auto-display expressions.\n\
12162 Arguments are breakpoint numbers with spaces in between.\n\
12163 To delete all breakpoints, give no argument.\n\
12164 This command may be abbreviated \"delete\"."),
12165 &deletelist);
12166
12167 add_com ("clear", class_breakpoint, clear_command, _("\
12168 Clear breakpoint at specified line or function.\n\
12169 Argument may be line number, function name, or \"*\" and an address.\n\
12170 If line number is specified, all breakpoints in that line are cleared.\n\
12171 If function is specified, breakpoints at beginning of function are cleared.\n\
12172 If an address is specified, breakpoints at that address are cleared.\n\
12173 \n\
12174 With no argument, clears all breakpoints in the line that the selected frame\n\
12175 is executing in.\n\
12176 \n\
12177 See also the \"delete\" command which clears breakpoints by number."));
12178 add_com_alias ("cl", "clear", class_breakpoint, 1);
12179
12180 c = add_com ("break", class_breakpoint, break_command, _("\
12181 Set breakpoint at specified line or function.\n"
12182 BREAK_ARGS_HELP ("break")));
12183 set_cmd_completer (c, location_completer);
12184
12185 add_com_alias ("b", "break", class_run, 1);
12186 add_com_alias ("br", "break", class_run, 1);
12187 add_com_alias ("bre", "break", class_run, 1);
12188 add_com_alias ("brea", "break", class_run, 1);
12189
12190 if (xdb_commands)
12191 add_com_alias ("ba", "break", class_breakpoint, 1);
12192
12193 if (dbx_commands)
12194 {
12195 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12196 Break in function/address or break at a line in the current file."),
12197 &stoplist, "stop ", 1, &cmdlist);
12198 add_cmd ("in", class_breakpoint, stopin_command,
12199 _("Break in function or address."), &stoplist);
12200 add_cmd ("at", class_breakpoint, stopat_command,
12201 _("Break at a line in the current file."), &stoplist);
12202 add_com ("status", class_info, breakpoints_info, _("\
12203 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
12204 The \"Type\" column indicates one of:\n\
12205 \tbreakpoint - normal breakpoint\n\
12206 \twatchpoint - watchpoint\n\
12207 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12208 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12209 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
12210 address and file/line number respectively.\n\
12211 \n\
12212 Convenience variable \"$_\" and default examine address for \"x\"\n\
12213 are set to the address of the last breakpoint listed unless the command\n\
12214 is prefixed with \"server \".\n\n\
12215 Convenience variable \"$bpnum\" contains the number of the last\n\
12216 breakpoint set."));
12217 }
12218
12219 add_info ("breakpoints", breakpoints_info, _("\
12220 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
12221 The \"Type\" column indicates one of:\n\
12222 \tbreakpoint - normal breakpoint\n\
12223 \twatchpoint - watchpoint\n\
12224 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12225 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12226 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
12227 address and file/line number respectively.\n\
12228 \n\
12229 Convenience variable \"$_\" and default examine address for \"x\"\n\
12230 are set to the address of the last breakpoint listed unless the command\n\
12231 is prefixed with \"server \".\n\n\
12232 Convenience variable \"$bpnum\" contains the number of the last\n\
12233 breakpoint set."));
12234
12235 add_info_alias ("b", "breakpoints", 1);
12236
12237 if (xdb_commands)
12238 add_com ("lb", class_breakpoint, breakpoints_info, _("\
12239 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
12240 The \"Type\" column indicates one of:\n\
12241 \tbreakpoint - normal breakpoint\n\
12242 \twatchpoint - watchpoint\n\
12243 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12244 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12245 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
12246 address and file/line number respectively.\n\
12247 \n\
12248 Convenience variable \"$_\" and default examine address for \"x\"\n\
12249 are set to the address of the last breakpoint listed unless the command\n\
12250 is prefixed with \"server \".\n\n\
12251 Convenience variable \"$bpnum\" contains the number of the last\n\
12252 breakpoint set."));
12253
12254 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12255 Status of all breakpoints, or breakpoint number NUMBER.\n\
12256 The \"Type\" column indicates one of:\n\
12257 \tbreakpoint - normal breakpoint\n\
12258 \twatchpoint - watchpoint\n\
12259 \tlongjmp - internal breakpoint used to step through longjmp()\n\
12260 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12261 \tuntil - internal breakpoint used by the \"until\" command\n\
12262 \tfinish - internal breakpoint used by the \"finish\" command\n\
12263 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12264 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12265 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
12266 address and file/line number respectively.\n\
12267 \n\
12268 Convenience variable \"$_\" and default examine address for \"x\"\n\
12269 are set to the address of the last breakpoint listed unless the command\n\
12270 is prefixed with \"server \".\n\n\
12271 Convenience variable \"$bpnum\" contains the number of the last\n\
12272 breakpoint set."),
12273 &maintenanceinfolist);
12274
12275 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12276 Set catchpoints to catch events."),
12277 &catch_cmdlist, "catch ",
12278 0/*allow-unknown*/, &cmdlist);
12279
12280 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12281 Set temporary catchpoints to catch events."),
12282 &tcatch_cmdlist, "tcatch ",
12283 0/*allow-unknown*/, &cmdlist);
12284
12285 /* Add catch and tcatch sub-commands. */
12286 add_catch_command ("catch", _("\
12287 Catch an exception, when caught.\n\
12288 With an argument, catch only exceptions with the given name."),
12289 catch_catch_command,
12290 NULL,
12291 CATCH_PERMANENT,
12292 CATCH_TEMPORARY);
12293 add_catch_command ("throw", _("\
12294 Catch an exception, when thrown.\n\
12295 With an argument, catch only exceptions with the given name."),
12296 catch_throw_command,
12297 NULL,
12298 CATCH_PERMANENT,
12299 CATCH_TEMPORARY);
12300 add_catch_command ("fork", _("Catch calls to fork."),
12301 catch_fork_command_1,
12302 NULL,
12303 (void *) (uintptr_t) catch_fork_permanent,
12304 (void *) (uintptr_t) catch_fork_temporary);
12305 add_catch_command ("vfork", _("Catch calls to vfork."),
12306 catch_fork_command_1,
12307 NULL,
12308 (void *) (uintptr_t) catch_vfork_permanent,
12309 (void *) (uintptr_t) catch_vfork_temporary);
12310 add_catch_command ("exec", _("Catch calls to exec."),
12311 catch_exec_command_1,
12312 NULL,
12313 CATCH_PERMANENT,
12314 CATCH_TEMPORARY);
12315 add_catch_command ("syscall", _("\
12316 Catch system calls by their names and/or numbers.\n\
12317 Arguments say which system calls to catch. If no arguments\n\
12318 are given, every system call will be caught.\n\
12319 Arguments, if given, should be one or more system call names\n\
12320 (if your system supports that), or system call numbers."),
12321 catch_syscall_command_1,
12322 catch_syscall_completer,
12323 CATCH_PERMANENT,
12324 CATCH_TEMPORARY);
12325 add_catch_command ("exception", _("\
12326 Catch Ada exceptions, when raised.\n\
12327 With an argument, catch only exceptions with the given name."),
12328 catch_ada_exception_command,
12329 NULL,
12330 CATCH_PERMANENT,
12331 CATCH_TEMPORARY);
12332 add_catch_command ("assert", _("\
12333 Catch failed Ada assertions, when raised.\n\
12334 With an argument, catch only exceptions with the given name."),
12335 catch_assert_command,
12336 NULL,
12337 CATCH_PERMANENT,
12338 CATCH_TEMPORARY);
12339
12340 c = add_com ("watch", class_breakpoint, watch_command, _("\
12341 Set a watchpoint for an expression.\n\
12342 Usage: watch [-l|-location] EXPRESSION\n\
12343 A watchpoint stops execution of your program whenever the value of\n\
12344 an expression changes.\n\
12345 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12346 the memory to which it refers."));
12347 set_cmd_completer (c, expression_completer);
12348
12349 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12350 Set a read watchpoint for an expression.\n\
12351 Usage: rwatch [-l|-location] EXPRESSION\n\
12352 A watchpoint stops execution of your program whenever the value of\n\
12353 an expression is read.\n\
12354 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12355 the memory to which it refers."));
12356 set_cmd_completer (c, expression_completer);
12357
12358 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12359 Set a watchpoint for an expression.\n\
12360 Usage: awatch [-l|-location] EXPRESSION\n\
12361 A watchpoint stops execution of your program whenever the value of\n\
12362 an expression is either read or written.\n\
12363 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12364 the memory to which it refers."));
12365 set_cmd_completer (c, expression_completer);
12366
12367 add_info ("watchpoints", watchpoints_info, _("\
12368 Status of specified watchpoints (all watchpoints if no argument)."));
12369
12370 /* XXX: cagney/2005-02-23: This should be a boolean, and should
12371 respond to changes - contrary to the description. */
12372 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12373 &can_use_hw_watchpoints, _("\
12374 Set debugger's willingness to use watchpoint hardware."), _("\
12375 Show debugger's willingness to use watchpoint hardware."), _("\
12376 If zero, gdb will not use hardware for new watchpoints, even if\n\
12377 such is available. (However, any hardware watchpoints that were\n\
12378 created before setting this to nonzero, will continue to use watchpoint\n\
12379 hardware.)"),
12380 NULL,
12381 show_can_use_hw_watchpoints,
12382 &setlist, &showlist);
12383
12384 can_use_hw_watchpoints = 1;
12385
12386 /* Tracepoint manipulation commands. */
12387
12388 c = add_com ("trace", class_breakpoint, trace_command, _("\
12389 Set a tracepoint at specified line or function.\n\
12390 \n"
12391 BREAK_ARGS_HELP ("trace") "\n\
12392 Do \"help tracepoints\" for info on other tracepoint commands."));
12393 set_cmd_completer (c, location_completer);
12394
12395 add_com_alias ("tp", "trace", class_alias, 0);
12396 add_com_alias ("tr", "trace", class_alias, 1);
12397 add_com_alias ("tra", "trace", class_alias, 1);
12398 add_com_alias ("trac", "trace", class_alias, 1);
12399
12400 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12401 Set a fast tracepoint at specified line or function.\n\
12402 \n"
12403 BREAK_ARGS_HELP ("ftrace") "\n\
12404 Do \"help tracepoints\" for info on other tracepoint commands."));
12405 set_cmd_completer (c, location_completer);
12406
12407 c = add_com ("strace", class_breakpoint, strace_command, _("\
12408 Set a static tracepoint at specified line, function or marker.\n\
12409 \n\
12410 strace [LOCATION] [if CONDITION]\n\
12411 LOCATION may be a line number, function name, \"*\" and an address,\n\
12412 or -m MARKER_ID.\n\
12413 If a line number is specified, probe the marker at start of code\n\
12414 for that line. If a function is specified, probe the marker at start\n\
12415 of code for that function. If an address is specified, probe the marker\n\
12416 at that exact address. If a marker id is specified, probe the marker\n\
12417 with that name. With no LOCATION, uses current execution address of\n\
12418 the selected stack frame.\n\
12419 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12420 This collects arbitrary user data passed in the probe point call to the\n\
12421 tracing library. You can inspect it when analyzing the trace buffer,\n\
12422 by printing the $_sdata variable like any other convenience variable.\n\
12423 \n\
12424 CONDITION is a boolean expression.\n\
12425 \n\
12426 Multiple tracepoints at one place are permitted, and useful if their\n\
12427 conditions are different.\n\
12428 \n\
12429 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12430 Do \"help tracepoints\" for info on other tracepoint commands."));
12431 set_cmd_completer (c, location_completer);
12432
12433 add_info ("tracepoints", tracepoints_info, _("\
12434 Status of specified tracepoints (all tracepoints if no argument).\n\
12435 Convenience variable \"$tpnum\" contains the number of the\n\
12436 last tracepoint set."));
12437
12438 add_info_alias ("tp", "tracepoints", 1);
12439
12440 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12441 Delete specified tracepoints.\n\
12442 Arguments are tracepoint numbers, separated by spaces.\n\
12443 No argument means delete all tracepoints."),
12444 &deletelist);
12445
12446 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
12447 Disable specified tracepoints.\n\
12448 Arguments are tracepoint numbers, separated by spaces.\n\
12449 No argument means disable all tracepoints."),
12450 &disablelist);
12451 deprecate_cmd (c, "disable");
12452
12453 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
12454 Enable specified tracepoints.\n\
12455 Arguments are tracepoint numbers, separated by spaces.\n\
12456 No argument means enable all tracepoints."),
12457 &enablelist);
12458 deprecate_cmd (c, "enable");
12459
12460 add_com ("passcount", class_trace, trace_pass_command, _("\
12461 Set the passcount for a tracepoint.\n\
12462 The trace will end when the tracepoint has been passed 'count' times.\n\
12463 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
12464 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
12465
12466 add_prefix_cmd ("save", class_breakpoint, save_command,
12467 _("Save breakpoint definitions as a script."),
12468 &save_cmdlist, "save ",
12469 0/*allow-unknown*/, &cmdlist);
12470
12471 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
12472 Save current breakpoint definitions as a script.\n\
12473 This includes all types of breakpoints (breakpoints, watchpoints,\n\
12474 catchpoints, tracepoints). Use the 'source' command in another debug\n\
12475 session to restore them."),
12476 &save_cmdlist);
12477 set_cmd_completer (c, filename_completer);
12478
12479 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
12480 Save current tracepoint definitions as a script.\n\
12481 Use the 'source' command in another debug session to restore them."),
12482 &save_cmdlist);
12483 set_cmd_completer (c, filename_completer);
12484
12485 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
12486 deprecate_cmd (c, "save tracepoints");
12487
12488 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
12489 Breakpoint specific settings\n\
12490 Configure various breakpoint-specific variables such as\n\
12491 pending breakpoint behavior"),
12492 &breakpoint_set_cmdlist, "set breakpoint ",
12493 0/*allow-unknown*/, &setlist);
12494 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
12495 Breakpoint specific settings\n\
12496 Configure various breakpoint-specific variables such as\n\
12497 pending breakpoint behavior"),
12498 &breakpoint_show_cmdlist, "show breakpoint ",
12499 0/*allow-unknown*/, &showlist);
12500
12501 add_setshow_auto_boolean_cmd ("pending", no_class,
12502 &pending_break_support, _("\
12503 Set debugger's behavior regarding pending breakpoints."), _("\
12504 Show debugger's behavior regarding pending breakpoints."), _("\
12505 If on, an unrecognized breakpoint location will cause gdb to create a\n\
12506 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
12507 an error. If auto, an unrecognized breakpoint location results in a\n\
12508 user-query to see if a pending breakpoint should be created."),
12509 NULL,
12510 show_pending_break_support,
12511 &breakpoint_set_cmdlist,
12512 &breakpoint_show_cmdlist);
12513
12514 pending_break_support = AUTO_BOOLEAN_AUTO;
12515
12516 add_setshow_boolean_cmd ("auto-hw", no_class,
12517 &automatic_hardware_breakpoints, _("\
12518 Set automatic usage of hardware breakpoints."), _("\
12519 Show automatic usage of hardware breakpoints."), _("\
12520 If set, the debugger will automatically use hardware breakpoints for\n\
12521 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
12522 a warning will be emitted for such breakpoints."),
12523 NULL,
12524 show_automatic_hardware_breakpoints,
12525 &breakpoint_set_cmdlist,
12526 &breakpoint_show_cmdlist);
12527
12528 add_setshow_enum_cmd ("always-inserted", class_support,
12529 always_inserted_enums, &always_inserted_mode, _("\
12530 Set mode for inserting breakpoints."), _("\
12531 Show mode for inserting breakpoints."), _("\
12532 When this mode is off, breakpoints are inserted in inferior when it is\n\
12533 resumed, and removed when execution stops. When this mode is on,\n\
12534 breakpoints are inserted immediately and removed only when the user\n\
12535 deletes the breakpoint. When this mode is auto (which is the default),\n\
12536 the behaviour depends on the non-stop setting (see help set non-stop).\n\
12537 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
12538 behaves as if always-inserted mode is on; if gdb is controlling the\n\
12539 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
12540 NULL,
12541 &show_always_inserted_mode,
12542 &breakpoint_set_cmdlist,
12543 &breakpoint_show_cmdlist);
12544
12545 automatic_hardware_breakpoints = 1;
12546
12547 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
12548 }
This page took 0.406503 seconds and 4 git commands to generate.