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