]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/breakpoint.c
* defs.h (strlen_paddr, paddr, paddr_nz): Remove.
[thirdparty/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 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
63 /* readline include files */
64 #include "readline/readline.h"
65 #include "readline/history.h"
66
67 /* readline defines this. */
68 #undef savestring
69
70 #include "mi/mi-common.h"
71
72 /* Arguments to pass as context to some catch command handlers. */
73 #define CATCH_PERMANENT ((void *) (uintptr_t) 0)
74 #define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
75
76 /* Prototypes for local functions. */
77
78 static void enable_delete_command (char *, int);
79
80 static void enable_delete_breakpoint (struct breakpoint *);
81
82 static void enable_once_command (char *, int);
83
84 static void enable_once_breakpoint (struct breakpoint *);
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
92 static void ignore_command (char *, int);
93
94 static int breakpoint_re_set_one (void *);
95
96 static void clear_command (char *, int);
97
98 static void catch_command (char *, int);
99
100 static void watch_command (char *, int);
101
102 static int can_use_hardware_watchpoint (struct value *);
103
104 static void break_command_1 (char *, int, int);
105
106 static void mention (struct breakpoint *);
107
108 static struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
109 struct symtab_and_line,
110 enum bptype);
111
112 static void check_duplicates (struct breakpoint *);
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 *, CORE_ADDR,
121 struct obj_section *, int);
122
123 static void breakpoints_info (char *, int);
124
125 static void breakpoint_1 (int, int);
126
127 static bpstat bpstat_alloc (const struct bp_location *, bpstat);
128
129 static int breakpoint_cond_eval (void *);
130
131 static void cleanup_executing_breakpoints (void *);
132
133 static void commands_command (char *, int);
134
135 static void condition_command (char *, int);
136
137 static int get_number_trailer (char **, int);
138
139 void set_breakpoint_count (int);
140
141 typedef enum
142 {
143 mark_inserted,
144 mark_uninserted
145 }
146 insertion_state_t;
147
148 static int remove_breakpoint (struct bp_location *, insertion_state_t);
149
150 static enum print_stop_action print_it_typical (bpstat);
151
152 static enum print_stop_action print_bp_stop_message (bpstat bs);
153
154 static int watchpoint_check (void *);
155
156 static void maintenance_info_breakpoints (char *, int);
157
158 static int hw_breakpoint_used_count (void);
159
160 static int hw_watchpoint_used_count (enum bptype, int *);
161
162 static void hbreak_command (char *, int);
163
164 static void thbreak_command (char *, int);
165
166 static void watch_command_1 (char *, int, int);
167
168 static void rwatch_command (char *, int);
169
170 static void awatch_command (char *, int);
171
172 static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
173
174 static void stop_command (char *arg, int from_tty);
175
176 static void stopin_command (char *arg, int from_tty);
177
178 static void stopat_command (char *arg, int from_tty);
179
180 static char *ep_parse_optional_if_clause (char **arg);
181
182 static char *ep_parse_optional_filename (char **arg);
183
184 static void catch_exception_command_1 (enum exception_event_kind ex_event,
185 char *arg, int tempflag, int from_tty);
186
187 static void tcatch_command (char *arg, int from_tty);
188
189 static void ep_skip_leading_whitespace (char **s);
190
191 static int single_step_breakpoint_inserted_here_p (CORE_ADDR pc);
192
193 static void free_bp_location (struct bp_location *loc);
194
195 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
196
197 static void update_global_location_list (int);
198
199 static void update_global_location_list_nothrow (int);
200
201 static int is_hardware_watchpoint (struct breakpoint *bpt);
202
203 static void insert_breakpoint_locations (void);
204
205 static void tracepoints_info (char *, int);
206
207 static void delete_trace_command (char *, int);
208
209 static void enable_trace_command (char *, int);
210
211 static void disable_trace_command (char *, int);
212
213 static void trace_pass_command (char *, int);
214
215 static void skip_prologue_sal (struct symtab_and_line *sal);
216
217
218 /* Flag indicating that a command has proceeded the inferior past the
219 current breakpoint. */
220
221 static int breakpoint_proceeded;
222
223 static const char *
224 bpdisp_text (enum bpdisp disp)
225 {
226 /* NOTE: the following values are a part of MI protocol and represent
227 values of 'disp' field returned when inferior stops at a breakpoint. */
228 static char *bpdisps[] = {"del", "dstp", "dis", "keep"};
229 return bpdisps[(int) disp];
230 }
231
232 /* Prototypes for exported functions. */
233 /* If FALSE, gdb will not use hardware support for watchpoints, even
234 if such is available. */
235 static int can_use_hw_watchpoints;
236
237 static void
238 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
239 struct cmd_list_element *c,
240 const char *value)
241 {
242 fprintf_filtered (file, _("\
243 Debugger's willingness to use watchpoint hardware is %s.\n"),
244 value);
245 }
246
247 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
248 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
249 for unrecognized breakpoint locations.
250 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
251 static enum auto_boolean pending_break_support;
252 static void
253 show_pending_break_support (struct ui_file *file, int from_tty,
254 struct cmd_list_element *c,
255 const char *value)
256 {
257 fprintf_filtered (file, _("\
258 Debugger's behavior regarding pending breakpoints is %s.\n"),
259 value);
260 }
261
262 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
263 set with "break" but falling in read-only memory.
264 If 0, gdb will warn about such breakpoints, but won't automatically
265 use hardware breakpoints. */
266 static int automatic_hardware_breakpoints;
267 static void
268 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
269 struct cmd_list_element *c,
270 const char *value)
271 {
272 fprintf_filtered (file, _("\
273 Automatic usage of hardware breakpoints is %s.\n"),
274 value);
275 }
276
277 /* If on, gdb will keep breakpoints inserted even as inferior is
278 stopped, and immediately insert any new breakpoints. If off, gdb
279 will insert breakpoints into inferior only when resuming it, and
280 will remove breakpoints upon stop. If auto, GDB will behave as ON
281 if in non-stop mode, and as OFF if all-stop mode.*/
282
283 static const char always_inserted_auto[] = "auto";
284 static const char always_inserted_on[] = "on";
285 static const char always_inserted_off[] = "off";
286 static const char *always_inserted_enums[] = {
287 always_inserted_auto,
288 always_inserted_off,
289 always_inserted_on,
290 NULL
291 };
292 static const char *always_inserted_mode = always_inserted_auto;
293 static void
294 show_always_inserted_mode (struct ui_file *file, int from_tty,
295 struct cmd_list_element *c, const char *value)
296 {
297 if (always_inserted_mode == always_inserted_auto)
298 fprintf_filtered (file, _("\
299 Always inserted breakpoint mode is %s (currently %s).\n"),
300 value,
301 breakpoints_always_inserted_mode () ? "on" : "off");
302 else
303 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"), value);
304 }
305
306 int
307 breakpoints_always_inserted_mode (void)
308 {
309 return (always_inserted_mode == always_inserted_on
310 || (always_inserted_mode == always_inserted_auto && non_stop));
311 }
312
313 void _initialize_breakpoint (void);
314
315 /* Are we executing breakpoint commands? */
316 static int executing_breakpoint_commands;
317
318 /* Are overlay event breakpoints enabled? */
319 static int overlay_events_enabled;
320
321 /* Walk the following statement or block through all breakpoints.
322 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
323 breakpoint. */
324
325 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
326
327 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
328 for (B = breakpoint_chain; \
329 B ? (TMP=B->next, 1): 0; \
330 B = TMP)
331
332 /* Similar iterators for the low-level breakpoints. */
333
334 #define ALL_BP_LOCATIONS(B) for (B = bp_location_chain; B; B = B->global_next)
335
336 #define ALL_BP_LOCATIONS_SAFE(B,TMP) \
337 for (B = bp_location_chain; \
338 B ? (TMP=B->global_next, 1): 0; \
339 B = TMP)
340
341 /* Iterator for tracepoints only. */
342
343 #define ALL_TRACEPOINTS(B) \
344 for (B = breakpoint_chain; B; B = B->next) \
345 if ((B)->type == bp_tracepoint)
346
347 /* Chains of all breakpoints defined. */
348
349 struct breakpoint *breakpoint_chain;
350
351 struct bp_location *bp_location_chain;
352
353 /* The locations that no longer correspond to any breakpoint,
354 unlinked from bp_location_chain, but for which a hit
355 may still be reported by a target. */
356 VEC(bp_location_p) *moribund_locations = NULL;
357
358 /* Number of last breakpoint made. */
359
360 int breakpoint_count;
361
362 /* Number of last tracepoint made. */
363
364 int tracepoint_count;
365
366 /* Return whether a breakpoint is an active enabled breakpoint. */
367 static int
368 breakpoint_enabled (struct breakpoint *b)
369 {
370 return (b->enable_state == bp_enabled);
371 }
372
373 /* Set breakpoint count to NUM. */
374
375 void
376 set_breakpoint_count (int num)
377 {
378 breakpoint_count = num;
379 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
380 }
381
382 /* Used in run_command to zero the hit count when a new run starts. */
383
384 void
385 clear_breakpoint_hit_counts (void)
386 {
387 struct breakpoint *b;
388
389 ALL_BREAKPOINTS (b)
390 b->hit_count = 0;
391 }
392
393 /* Default address, symtab and line to put a breakpoint at
394 for "break" command with no arg.
395 if default_breakpoint_valid is zero, the other three are
396 not valid, and "break" with no arg is an error.
397
398 This set by print_stack_frame, which calls set_default_breakpoint. */
399
400 int default_breakpoint_valid;
401 CORE_ADDR default_breakpoint_address;
402 struct symtab *default_breakpoint_symtab;
403 int default_breakpoint_line;
404 \f
405 /* *PP is a string denoting a breakpoint. Get the number of the breakpoint.
406 Advance *PP after the string and any trailing whitespace.
407
408 Currently the string can either be a number or "$" followed by the name
409 of a convenience variable. Making it an expression wouldn't work well
410 for map_breakpoint_numbers (e.g. "4 + 5 + 6").
411
412 If the string is a NULL pointer, that denotes the last breakpoint.
413
414 TRAILER is a character which can be found after the number; most
415 commonly this is `-'. If you don't want a trailer, use \0. */
416 static int
417 get_number_trailer (char **pp, int trailer)
418 {
419 int retval = 0; /* default */
420 char *p = *pp;
421
422 if (p == NULL)
423 /* Empty line means refer to the last breakpoint. */
424 return breakpoint_count;
425 else if (*p == '$')
426 {
427 /* Make a copy of the name, so we can null-terminate it
428 to pass to lookup_internalvar(). */
429 char *varname;
430 char *start = ++p;
431 LONGEST val;
432
433 while (isalnum (*p) || *p == '_')
434 p++;
435 varname = (char *) alloca (p - start + 1);
436 strncpy (varname, start, p - start);
437 varname[p - start] = '\0';
438 if (get_internalvar_integer (lookup_internalvar (varname), &val))
439 retval = (int) val;
440 else
441 {
442 printf_filtered (_("Convenience variable must have integer value.\n"));
443 retval = 0;
444 }
445 }
446 else
447 {
448 if (*p == '-')
449 ++p;
450 while (*p >= '0' && *p <= '9')
451 ++p;
452 if (p == *pp)
453 /* There is no number here. (e.g. "cond a == b"). */
454 {
455 /* Skip non-numeric token */
456 while (*p && !isspace((int) *p))
457 ++p;
458 /* Return zero, which caller must interpret as error. */
459 retval = 0;
460 }
461 else
462 retval = atoi (*pp);
463 }
464 if (!(isspace (*p) || *p == '\0' || *p == trailer))
465 {
466 /* Trailing junk: return 0 and let caller print error msg. */
467 while (!(isspace (*p) || *p == '\0' || *p == trailer))
468 ++p;
469 retval = 0;
470 }
471 while (isspace (*p))
472 p++;
473 *pp = p;
474 return retval;
475 }
476
477
478 /* Like get_number_trailer, but don't allow a trailer. */
479 int
480 get_number (char **pp)
481 {
482 return get_number_trailer (pp, '\0');
483 }
484
485 /* Parse a number or a range.
486 * A number will be of the form handled by get_number.
487 * A range will be of the form <number1> - <number2>, and
488 * will represent all the integers between number1 and number2,
489 * inclusive.
490 *
491 * While processing a range, this fuction is called iteratively;
492 * At each call it will return the next value in the range.
493 *
494 * At the beginning of parsing a range, the char pointer PP will
495 * be advanced past <number1> and left pointing at the '-' token.
496 * Subsequent calls will not advance the pointer until the range
497 * is completed. The call that completes the range will advance
498 * pointer PP past <number2>.
499 */
500
501 int
502 get_number_or_range (char **pp)
503 {
504 static int last_retval, end_value;
505 static char *end_ptr;
506 static int in_range = 0;
507
508 if (**pp != '-')
509 {
510 /* Default case: pp is pointing either to a solo number,
511 or to the first number of a range. */
512 last_retval = get_number_trailer (pp, '-');
513 if (**pp == '-')
514 {
515 char **temp;
516
517 /* This is the start of a range (<number1> - <number2>).
518 Skip the '-', parse and remember the second number,
519 and also remember the end of the final token. */
520
521 temp = &end_ptr;
522 end_ptr = *pp + 1;
523 while (isspace ((int) *end_ptr))
524 end_ptr++; /* skip white space */
525 end_value = get_number (temp);
526 if (end_value < last_retval)
527 {
528 error (_("inverted range"));
529 }
530 else if (end_value == last_retval)
531 {
532 /* degenerate range (number1 == number2). Advance the
533 token pointer so that the range will be treated as a
534 single number. */
535 *pp = end_ptr;
536 }
537 else
538 in_range = 1;
539 }
540 }
541 else if (! in_range)
542 error (_("negative value"));
543 else
544 {
545 /* pp points to the '-' that betokens a range. All
546 number-parsing has already been done. Return the next
547 integer value (one greater than the saved previous value).
548 Do not advance the token pointer 'pp' until the end of range
549 is reached. */
550
551 if (++last_retval == end_value)
552 {
553 /* End of range reached; advance token pointer. */
554 *pp = end_ptr;
555 in_range = 0;
556 }
557 }
558 return last_retval;
559 }
560
561
562 \f
563 /* condition N EXP -- set break condition of breakpoint N to EXP. */
564
565 static void
566 condition_command (char *arg, int from_tty)
567 {
568 struct breakpoint *b;
569 char *p;
570 int bnum;
571
572 if (arg == 0)
573 error_no_arg (_("breakpoint number"));
574
575 p = arg;
576 bnum = get_number (&p);
577 if (bnum == 0)
578 error (_("Bad breakpoint argument: '%s'"), arg);
579
580 ALL_BREAKPOINTS (b)
581 if (b->number == bnum)
582 {
583 struct bp_location *loc = b->loc;
584 for (; loc; loc = loc->next)
585 {
586 if (loc->cond)
587 {
588 xfree (loc->cond);
589 loc->cond = 0;
590 }
591 }
592 if (b->cond_string != NULL)
593 xfree (b->cond_string);
594
595 if (*p == 0)
596 {
597 b->cond_string = NULL;
598 if (from_tty)
599 printf_filtered (_("Breakpoint %d now unconditional.\n"), bnum);
600 }
601 else
602 {
603 arg = p;
604 /* I don't know if it matters whether this is the string the user
605 typed in or the decompiled expression. */
606 b->cond_string = xstrdup (arg);
607 b->condition_not_parsed = 0;
608 for (loc = b->loc; loc; loc = loc->next)
609 {
610 arg = p;
611 loc->cond =
612 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
613 if (*arg)
614 error (_("Junk at end of expression"));
615 }
616 }
617 breakpoints_changed ();
618 observer_notify_breakpoint_modified (b->number);
619 return;
620 }
621
622 error (_("No breakpoint number %d."), bnum);
623 }
624
625 static void
626 commands_command (char *arg, int from_tty)
627 {
628 struct breakpoint *b;
629 char *p;
630 int bnum;
631 struct command_line *l;
632
633 /* If we allowed this, we would have problems with when to
634 free the storage, if we change the commands currently
635 being read from. */
636
637 if (executing_breakpoint_commands)
638 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
639
640 p = arg;
641 bnum = get_number (&p);
642
643 if (p && *p)
644 error (_("Unexpected extra arguments following breakpoint number."));
645
646 ALL_BREAKPOINTS (b)
647 if (b->number == bnum)
648 {
649 char *tmpbuf = xstrprintf ("Type commands for when breakpoint %d is hit, one per line.",
650 bnum);
651 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
652 l = read_command_lines (tmpbuf, from_tty, 1);
653 do_cleanups (cleanups);
654 free_command_lines (&b->commands);
655 b->commands = l;
656 breakpoints_changed ();
657 observer_notify_breakpoint_modified (b->number);
658 return;
659 }
660 error (_("No breakpoint number %d."), bnum);
661 }
662
663 /* Like commands_command, but instead of reading the commands from
664 input stream, takes them from an already parsed command structure.
665
666 This is used by cli-script.c to DTRT with breakpoint commands
667 that are part of if and while bodies. */
668 enum command_control_type
669 commands_from_control_command (char *arg, struct command_line *cmd)
670 {
671 struct breakpoint *b;
672 char *p;
673 int bnum;
674
675 /* If we allowed this, we would have problems with when to
676 free the storage, if we change the commands currently
677 being read from. */
678
679 if (executing_breakpoint_commands)
680 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
681
682 /* An empty string for the breakpoint number means the last
683 breakpoint, but get_number expects a NULL pointer. */
684 if (arg && !*arg)
685 p = NULL;
686 else
687 p = arg;
688 bnum = get_number (&p);
689
690 if (p && *p)
691 error (_("Unexpected extra arguments following breakpoint number."));
692
693 ALL_BREAKPOINTS (b)
694 if (b->number == bnum)
695 {
696 free_command_lines (&b->commands);
697 if (cmd->body_count != 1)
698 error (_("Invalid \"commands\" block structure."));
699 /* We need to copy the commands because if/while will free the
700 list after it finishes execution. */
701 b->commands = copy_command_lines (cmd->body_list[0]);
702 breakpoints_changed ();
703 observer_notify_breakpoint_modified (b->number);
704 return simple_control;
705 }
706 error (_("No breakpoint number %d."), bnum);
707 }
708 \f
709 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
710 by replacing any memory breakpoints with their shadowed contents. */
711
712 void
713 breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
714 {
715 struct bp_location *b;
716 CORE_ADDR bp_addr = 0;
717 int bp_size = 0;
718 int bptoffset = 0;
719
720 ALL_BP_LOCATIONS (b)
721 {
722 if (b->owner->type == bp_none)
723 warning (_("reading through apparently deleted breakpoint #%d?"),
724 b->owner->number);
725
726 if (b->loc_type != bp_loc_software_breakpoint)
727 continue;
728 if (!b->inserted)
729 continue;
730 /* Addresses and length of the part of the breakpoint that
731 we need to copy. */
732 bp_addr = b->target_info.placed_address;
733 bp_size = b->target_info.shadow_len;
734 if (bp_size == 0)
735 /* bp isn't valid, or doesn't shadow memory. */
736 continue;
737
738 if (bp_addr + bp_size <= memaddr)
739 /* The breakpoint is entirely before the chunk of memory we
740 are reading. */
741 continue;
742
743 if (bp_addr >= memaddr + len)
744 /* The breakpoint is entirely after the chunk of memory we are
745 reading. */
746 continue;
747
748 /* Offset within shadow_contents. */
749 if (bp_addr < memaddr)
750 {
751 /* Only copy the second part of the breakpoint. */
752 bp_size -= memaddr - bp_addr;
753 bptoffset = memaddr - bp_addr;
754 bp_addr = memaddr;
755 }
756
757 if (bp_addr + bp_size > memaddr + len)
758 {
759 /* Only copy the first part of the breakpoint. */
760 bp_size -= (bp_addr + bp_size) - (memaddr + len);
761 }
762
763 memcpy (buf + bp_addr - memaddr,
764 b->target_info.shadow_contents + bptoffset, bp_size);
765 }
766 }
767 \f
768
769 /* A wrapper function for inserting catchpoints. */
770 static void
771 insert_catchpoint (struct ui_out *uo, void *args)
772 {
773 struct breakpoint *b = (struct breakpoint *) args;
774 int val = -1;
775
776 gdb_assert (b->type == bp_catchpoint);
777 gdb_assert (b->ops != NULL && b->ops->insert != NULL);
778
779 b->ops->insert (b);
780 }
781
782 static int
783 is_hardware_watchpoint (struct breakpoint *bpt)
784 {
785 return (bpt->type == bp_hardware_watchpoint
786 || bpt->type == bp_read_watchpoint
787 || bpt->type == bp_access_watchpoint);
788 }
789
790 /* Find the current value of a watchpoint on EXP. Return the value in
791 *VALP and *RESULTP and the chain of intermediate and final values
792 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
793 not need them.
794
795 If a memory error occurs while evaluating the expression, *RESULTP will
796 be set to NULL. *RESULTP may be a lazy value, if the result could
797 not be read from memory. It is used to determine whether a value
798 is user-specified (we should watch the whole value) or intermediate
799 (we should watch only the bit used to locate the final value).
800
801 If the final value, or any intermediate value, could not be read
802 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
803 set to any referenced values. *VALP will never be a lazy value.
804 This is the value which we store in struct breakpoint.
805
806 If VAL_CHAIN is non-NULL, *VAL_CHAIN will be released from the
807 value chain. The caller must free the values individually. If
808 VAL_CHAIN is NULL, all generated values will be left on the value
809 chain. */
810
811 static void
812 fetch_watchpoint_value (struct expression *exp, struct value **valp,
813 struct value **resultp, struct value **val_chain)
814 {
815 struct value *mark, *new_mark, *result;
816 volatile struct gdb_exception ex;
817
818 *valp = NULL;
819 if (resultp)
820 *resultp = NULL;
821 if (val_chain)
822 *val_chain = NULL;
823
824 /* Evaluate the expression. */
825 mark = value_mark ();
826 result = NULL;
827
828 TRY_CATCH (ex, RETURN_MASK_ALL)
829 {
830 result = evaluate_expression (exp);
831 }
832 if (ex.reason < 0)
833 {
834 /* Ignore memory errors, we want watchpoints pointing at
835 inaccessible memory to still be created; otherwise, throw the
836 error to some higher catcher. */
837 switch (ex.error)
838 {
839 case MEMORY_ERROR:
840 break;
841 default:
842 throw_exception (ex);
843 break;
844 }
845 }
846
847 new_mark = value_mark ();
848 if (mark == new_mark)
849 return;
850 if (resultp)
851 *resultp = result;
852
853 /* Make sure it's not lazy, so that after the target stops again we
854 have a non-lazy previous value to compare with. */
855 if (result != NULL
856 && (!value_lazy (result) || gdb_value_fetch_lazy (result)))
857 *valp = result;
858
859 if (val_chain)
860 {
861 /* Return the chain of intermediate values. We use this to
862 decide which addresses to watch. */
863 *val_chain = new_mark;
864 value_release_to_mark (mark);
865 }
866 }
867
868 /* Assuming that B is a watchpoint:
869 - Reparse watchpoint expression, if REPARSE is non-zero
870 - Evaluate expression and store the result in B->val
871 - Evaluate the condition if there is one, and store the result
872 in b->loc->cond.
873 - Update the list of values that must be watched in B->loc.
874
875 If the watchpoint disposition is disp_del_at_next_stop, then do nothing.
876 If this is local watchpoint that is out of scope, delete it. */
877 static void
878 update_watchpoint (struct breakpoint *b, int reparse)
879 {
880 int within_current_scope;
881 struct frame_id saved_frame_id;
882 struct bp_location *loc;
883 bpstat bs;
884
885 /* We don't free locations. They are stored in bp_location_chain and
886 update_global_locations will eventually delete them and remove
887 breakpoints if needed. */
888 b->loc = NULL;
889
890 if (b->disposition == disp_del_at_next_stop)
891 return;
892
893 /* Save the current frame's ID so we can restore it after
894 evaluating the watchpoint expression on its own frame. */
895 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
896 took a frame parameter, so that we didn't have to change the
897 selected frame. */
898 saved_frame_id = get_frame_id (get_selected_frame (NULL));
899
900 /* Determine if the watchpoint is within scope. */
901 if (b->exp_valid_block == NULL)
902 within_current_scope = 1;
903 else
904 {
905 struct frame_info *fi;
906 fi = frame_find_by_id (b->watchpoint_frame);
907 within_current_scope = (fi != NULL);
908 if (within_current_scope)
909 select_frame (fi);
910 }
911
912 if (within_current_scope && reparse)
913 {
914 char *s;
915 if (b->exp)
916 {
917 xfree (b->exp);
918 b->exp = NULL;
919 }
920 s = b->exp_string;
921 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
922 /* If the meaning of expression itself changed, the old value is
923 no longer relevant. We don't want to report a watchpoint hit
924 to the user when the old value and the new value may actually
925 be completely different objects. */
926 value_free (b->val);
927 b->val = NULL;
928 b->val_valid = 0;
929 }
930
931 /* If we failed to parse the expression, for example because
932 it refers to a global variable in a not-yet-loaded shared library,
933 don't try to insert watchpoint. We don't automatically delete
934 such watchpoint, though, since failure to parse expression
935 is different from out-of-scope watchpoint. */
936 if (within_current_scope && b->exp)
937 {
938 struct value *val_chain, *v, *result, *next;
939
940 fetch_watchpoint_value (b->exp, &v, &result, &val_chain);
941
942 /* Avoid setting b->val if it's already set. The meaning of
943 b->val is 'the last value' user saw, and we should update
944 it only if we reported that last value to user. As it
945 happens, the code that reports it updates b->val directly. */
946 if (!b->val_valid)
947 {
948 b->val = v;
949 b->val_valid = 1;
950 }
951
952 /* Change the type of breakpoint between hardware assisted or an
953 ordinary watchpoint depending on the hardware support and free
954 hardware slots. REPARSE is set when the inferior is started. */
955 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
956 && reparse)
957 {
958 int i, mem_cnt, other_type_used;
959
960 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
961 &other_type_used);
962 mem_cnt = can_use_hardware_watchpoint (val_chain);
963
964 if (!mem_cnt)
965 b->type = bp_watchpoint;
966 else
967 {
968 int target_resources_ok = target_can_use_hardware_watchpoint
969 (bp_hardware_watchpoint, i + mem_cnt, other_type_used);
970 if (target_resources_ok <= 0)
971 b->type = bp_watchpoint;
972 else
973 b->type = bp_hardware_watchpoint;
974 }
975 }
976
977 /* Look at each value on the value chain. */
978 for (v = val_chain; v; v = next)
979 {
980 /* If it's a memory location, and GDB actually needed
981 its contents to evaluate the expression, then we
982 must watch it. If the first value returned is
983 still lazy, that means an error occurred reading it;
984 watch it anyway in case it becomes readable. */
985 if (VALUE_LVAL (v) == lval_memory
986 && (v == val_chain || ! value_lazy (v)))
987 {
988 struct type *vtype = check_typedef (value_type (v));
989
990 /* We only watch structs and arrays if user asked
991 for it explicitly, never if they just happen to
992 appear in the middle of some value chain. */
993 if (v == result
994 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
995 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
996 {
997 CORE_ADDR addr;
998 int len, type;
999 struct bp_location *loc, **tmp;
1000
1001 addr = value_address (v);
1002 len = TYPE_LENGTH (value_type (v));
1003 type = hw_write;
1004 if (b->type == bp_read_watchpoint)
1005 type = hw_read;
1006 else if (b->type == bp_access_watchpoint)
1007 type = hw_access;
1008
1009 loc = allocate_bp_location (b);
1010 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1011 ;
1012 *tmp = loc;
1013 loc->gdbarch = get_type_arch (value_type (v));
1014 loc->address = addr;
1015 loc->length = len;
1016 loc->watchpoint_type = type;
1017 }
1018 }
1019
1020 next = value_next (v);
1021 if (v != b->val)
1022 value_free (v);
1023 }
1024
1025 /* We just regenerated the list of breakpoint locations.
1026 The new location does not have its condition field set to anything
1027 and therefore, we must always reparse the cond_string, independently
1028 of the value of the reparse flag. */
1029 if (b->cond_string != NULL)
1030 {
1031 char *s = b->cond_string;
1032 b->loc->cond = parse_exp_1 (&s, b->exp_valid_block, 0);
1033 }
1034 }
1035 else if (!within_current_scope)
1036 {
1037 printf_filtered (_("\
1038 Watchpoint %d deleted because the program has left the block \n\
1039 in which its expression is valid.\n"),
1040 b->number);
1041 if (b->related_breakpoint)
1042 b->related_breakpoint->disposition = disp_del_at_next_stop;
1043 b->disposition = disp_del_at_next_stop;
1044 }
1045
1046 /* Restore the selected frame. */
1047 select_frame (frame_find_by_id (saved_frame_id));
1048 }
1049
1050
1051 /* Returns 1 iff breakpoint location should be
1052 inserted in the inferior. */
1053 static int
1054 should_be_inserted (struct bp_location *bpt)
1055 {
1056 if (!breakpoint_enabled (bpt->owner))
1057 return 0;
1058
1059 if (bpt->owner->disposition == disp_del_at_next_stop)
1060 return 0;
1061
1062 if (!bpt->enabled || bpt->shlib_disabled || bpt->duplicate)
1063 return 0;
1064
1065 /* Tracepoints are inserted by the target at a time of its choosing,
1066 not by us. */
1067 if (bpt->owner->type == bp_tracepoint)
1068 return 0;
1069
1070 return 1;
1071 }
1072
1073 /* Insert a low-level "breakpoint" of some type. BPT is the breakpoint.
1074 Any error messages are printed to TMP_ERROR_STREAM; and DISABLED_BREAKS,
1075 and HW_BREAKPOINT_ERROR are used to report problems.
1076
1077 NOTE drow/2003-09-09: This routine could be broken down to an object-style
1078 method for each breakpoint or catchpoint type. */
1079 static int
1080 insert_bp_location (struct bp_location *bpt,
1081 struct ui_file *tmp_error_stream,
1082 int *disabled_breaks,
1083 int *hw_breakpoint_error)
1084 {
1085 int val = 0;
1086
1087 if (!should_be_inserted (bpt) || bpt->inserted)
1088 return 0;
1089
1090 /* Initialize the target-specific information. */
1091 memset (&bpt->target_info, 0, sizeof (bpt->target_info));
1092 bpt->target_info.placed_address = bpt->address;
1093
1094 if (bpt->loc_type == bp_loc_software_breakpoint
1095 || bpt->loc_type == bp_loc_hardware_breakpoint)
1096 {
1097 if (bpt->owner->type != bp_hardware_breakpoint)
1098 {
1099 /* If the explicitly specified breakpoint type
1100 is not hardware breakpoint, check the memory map to see
1101 if the breakpoint address is in read only memory or not.
1102 Two important cases are:
1103 - location type is not hardware breakpoint, memory
1104 is readonly. We change the type of the location to
1105 hardware breakpoint.
1106 - location type is hardware breakpoint, memory is read-write.
1107 This means we've previously made the location hardware one, but
1108 then the memory map changed, so we undo.
1109
1110 When breakpoints are removed, remove_breakpoints will
1111 use location types we've just set here, the only possible
1112 problem is that memory map has changed during running program,
1113 but it's not going to work anyway with current gdb. */
1114 struct mem_region *mr
1115 = lookup_mem_region (bpt->target_info.placed_address);
1116
1117 if (mr)
1118 {
1119 if (automatic_hardware_breakpoints)
1120 {
1121 int changed = 0;
1122 enum bp_loc_type new_type;
1123
1124 if (mr->attrib.mode != MEM_RW)
1125 new_type = bp_loc_hardware_breakpoint;
1126 else
1127 new_type = bp_loc_software_breakpoint;
1128
1129 if (new_type != bpt->loc_type)
1130 {
1131 static int said = 0;
1132 bpt->loc_type = new_type;
1133 if (!said)
1134 {
1135 fprintf_filtered (gdb_stdout, _("\
1136 Note: automatically using hardware breakpoints for read-only addresses.\n"));
1137 said = 1;
1138 }
1139 }
1140 }
1141 else if (bpt->loc_type == bp_loc_software_breakpoint
1142 && mr->attrib.mode != MEM_RW)
1143 warning (_("cannot set software breakpoint at readonly address %s"),
1144 paddress (bpt->gdbarch, bpt->address));
1145 }
1146 }
1147
1148 /* First check to see if we have to handle an overlay. */
1149 if (overlay_debugging == ovly_off
1150 || bpt->section == NULL
1151 || !(section_is_overlay (bpt->section)))
1152 {
1153 /* No overlay handling: just set the breakpoint. */
1154
1155 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1156 val = target_insert_hw_breakpoint (bpt->gdbarch,
1157 &bpt->target_info);
1158 else
1159 val = target_insert_breakpoint (bpt->gdbarch,
1160 &bpt->target_info);
1161 }
1162 else
1163 {
1164 /* This breakpoint is in an overlay section.
1165 Shall we set a breakpoint at the LMA? */
1166 if (!overlay_events_enabled)
1167 {
1168 /* Yes -- overlay event support is not active,
1169 so we must try to set a breakpoint at the LMA.
1170 This will not work for a hardware breakpoint. */
1171 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1172 warning (_("hardware breakpoint %d not supported in overlay!"),
1173 bpt->owner->number);
1174 else
1175 {
1176 CORE_ADDR addr = overlay_unmapped_address (bpt->address,
1177 bpt->section);
1178 /* Set a software (trap) breakpoint at the LMA. */
1179 bpt->overlay_target_info = bpt->target_info;
1180 bpt->overlay_target_info.placed_address = addr;
1181 val = target_insert_breakpoint (bpt->gdbarch,
1182 &bpt->overlay_target_info);
1183 if (val != 0)
1184 fprintf_unfiltered (tmp_error_stream,
1185 "Overlay breakpoint %d failed: in ROM?\n",
1186 bpt->owner->number);
1187 }
1188 }
1189 /* Shall we set a breakpoint at the VMA? */
1190 if (section_is_mapped (bpt->section))
1191 {
1192 /* Yes. This overlay section is mapped into memory. */
1193 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1194 val = target_insert_hw_breakpoint (bpt->gdbarch,
1195 &bpt->target_info);
1196 else
1197 val = target_insert_breakpoint (bpt->gdbarch,
1198 &bpt->target_info);
1199 }
1200 else
1201 {
1202 /* No. This breakpoint will not be inserted.
1203 No error, but do not mark the bp as 'inserted'. */
1204 return 0;
1205 }
1206 }
1207
1208 if (val)
1209 {
1210 /* Can't set the breakpoint. */
1211 if (solib_name_from_address (bpt->address))
1212 {
1213 /* See also: disable_breakpoints_in_shlibs. */
1214 val = 0;
1215 bpt->shlib_disabled = 1;
1216 if (!*disabled_breaks)
1217 {
1218 fprintf_unfiltered (tmp_error_stream,
1219 "Cannot insert breakpoint %d.\n",
1220 bpt->owner->number);
1221 fprintf_unfiltered (tmp_error_stream,
1222 "Temporarily disabling shared library breakpoints:\n");
1223 }
1224 *disabled_breaks = 1;
1225 fprintf_unfiltered (tmp_error_stream,
1226 "breakpoint #%d\n", bpt->owner->number);
1227 }
1228 else
1229 {
1230 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1231 {
1232 *hw_breakpoint_error = 1;
1233 fprintf_unfiltered (tmp_error_stream,
1234 "Cannot insert hardware breakpoint %d.\n",
1235 bpt->owner->number);
1236 }
1237 else
1238 {
1239 fprintf_unfiltered (tmp_error_stream,
1240 "Cannot insert breakpoint %d.\n",
1241 bpt->owner->number);
1242 fprintf_filtered (tmp_error_stream,
1243 "Error accessing memory address ");
1244 fputs_filtered (paddress (bpt->gdbarch, bpt->address),
1245 tmp_error_stream);
1246 fprintf_filtered (tmp_error_stream, ": %s.\n",
1247 safe_strerror (val));
1248 }
1249
1250 }
1251 }
1252 else
1253 bpt->inserted = 1;
1254
1255 return val;
1256 }
1257
1258 else if (bpt->loc_type == bp_loc_hardware_watchpoint
1259 /* NOTE drow/2003-09-08: This state only exists for removing
1260 watchpoints. It's not clear that it's necessary... */
1261 && bpt->owner->disposition != disp_del_at_next_stop)
1262 {
1263 val = target_insert_watchpoint (bpt->address,
1264 bpt->length,
1265 bpt->watchpoint_type);
1266 bpt->inserted = (val != -1);
1267 }
1268
1269 else if (bpt->owner->type == bp_catchpoint)
1270 {
1271 struct gdb_exception e = catch_exception (uiout, insert_catchpoint,
1272 bpt->owner, RETURN_MASK_ERROR);
1273 exception_fprintf (gdb_stderr, e, "warning: inserting catchpoint %d: ",
1274 bpt->owner->number);
1275 if (e.reason < 0)
1276 bpt->owner->enable_state = bp_disabled;
1277 else
1278 bpt->inserted = 1;
1279
1280 /* We've already printed an error message if there was a problem
1281 inserting this catchpoint, and we've disabled the catchpoint,
1282 so just return success. */
1283 return 0;
1284 }
1285
1286 return 0;
1287 }
1288
1289 /* Make sure all breakpoints are inserted in inferior.
1290 Throws exception on any error.
1291 A breakpoint that is already inserted won't be inserted
1292 again, so calling this function twice is safe. */
1293 void
1294 insert_breakpoints (void)
1295 {
1296 struct breakpoint *bpt;
1297
1298 ALL_BREAKPOINTS (bpt)
1299 if (is_hardware_watchpoint (bpt))
1300 update_watchpoint (bpt, 0 /* don't reparse. */);
1301
1302 update_global_location_list (1);
1303
1304 /* update_global_location_list does not insert breakpoints when
1305 always_inserted_mode is not enabled. Explicitly insert them
1306 now. */
1307 if (!breakpoints_always_inserted_mode ())
1308 insert_breakpoint_locations ();
1309 }
1310
1311 /* insert_breakpoints is used when starting or continuing the program.
1312 remove_breakpoints is used when the program stops.
1313 Both return zero if successful,
1314 or an `errno' value if could not write the inferior. */
1315
1316 static void
1317 insert_breakpoint_locations (void)
1318 {
1319 struct breakpoint *bpt;
1320 struct bp_location *b, *temp;
1321 int error = 0;
1322 int val = 0;
1323 int disabled_breaks = 0;
1324 int hw_breakpoint_error = 0;
1325
1326 struct ui_file *tmp_error_stream = mem_fileopen ();
1327 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
1328
1329 /* Explicitly mark the warning -- this will only be printed if
1330 there was an error. */
1331 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1332
1333 ALL_BP_LOCATIONS_SAFE (b, temp)
1334 {
1335 if (!should_be_inserted (b) || b->inserted)
1336 continue;
1337
1338 /* There is no point inserting thread-specific breakpoints if the
1339 thread no longer exists. */
1340 if (b->owner->thread != -1
1341 && !valid_thread_id (b->owner->thread))
1342 continue;
1343
1344 val = insert_bp_location (b, tmp_error_stream,
1345 &disabled_breaks,
1346 &hw_breakpoint_error);
1347 if (val)
1348 error = val;
1349 }
1350
1351 /* If we failed to insert all locations of a watchpoint,
1352 remove them, as half-inserted watchpoint is of limited use. */
1353 ALL_BREAKPOINTS (bpt)
1354 {
1355 int some_failed = 0;
1356 struct bp_location *loc;
1357
1358 if (!is_hardware_watchpoint (bpt))
1359 continue;
1360
1361 if (!breakpoint_enabled (bpt))
1362 continue;
1363
1364 if (bpt->disposition == disp_del_at_next_stop)
1365 continue;
1366
1367 for (loc = bpt->loc; loc; loc = loc->next)
1368 if (!loc->inserted)
1369 {
1370 some_failed = 1;
1371 break;
1372 }
1373 if (some_failed)
1374 {
1375 for (loc = bpt->loc; loc; loc = loc->next)
1376 if (loc->inserted)
1377 remove_breakpoint (loc, mark_uninserted);
1378
1379 hw_breakpoint_error = 1;
1380 fprintf_unfiltered (tmp_error_stream,
1381 "Could not insert hardware watchpoint %d.\n",
1382 bpt->number);
1383 error = -1;
1384 }
1385 }
1386
1387 if (error)
1388 {
1389 /* If a hardware breakpoint or watchpoint was inserted, add a
1390 message about possibly exhausted resources. */
1391 if (hw_breakpoint_error)
1392 {
1393 fprintf_unfiltered (tmp_error_stream,
1394 "Could not insert hardware breakpoints:\n\
1395 You may have requested too many hardware breakpoints/watchpoints.\n");
1396 }
1397 target_terminal_ours_for_output ();
1398 error_stream (tmp_error_stream);
1399 }
1400
1401 do_cleanups (cleanups);
1402 }
1403
1404 int
1405 remove_breakpoints (void)
1406 {
1407 struct bp_location *b;
1408 int val;
1409
1410 ALL_BP_LOCATIONS (b)
1411 {
1412 if (b->inserted)
1413 {
1414 val = remove_breakpoint (b, mark_uninserted);
1415 if (val != 0)
1416 return val;
1417 }
1418 }
1419 return 0;
1420 }
1421
1422 int
1423 remove_hw_watchpoints (void)
1424 {
1425 struct bp_location *b;
1426 int val;
1427
1428 ALL_BP_LOCATIONS (b)
1429 {
1430 if (b->inserted && b->loc_type == bp_loc_hardware_watchpoint)
1431 {
1432 val = remove_breakpoint (b, mark_uninserted);
1433 if (val != 0)
1434 return val;
1435 }
1436 }
1437 return 0;
1438 }
1439
1440 int
1441 reattach_breakpoints (int pid)
1442 {
1443 struct bp_location *b;
1444 int val;
1445 struct cleanup *old_chain = save_inferior_ptid ();
1446 struct ui_file *tmp_error_stream = mem_fileopen ();
1447 int dummy1 = 0, dummy2 = 0;
1448
1449 make_cleanup_ui_file_delete (tmp_error_stream);
1450
1451 inferior_ptid = pid_to_ptid (pid);
1452 ALL_BP_LOCATIONS (b)
1453 {
1454 if (b->inserted)
1455 {
1456 b->inserted = 0;
1457 val = insert_bp_location (b, tmp_error_stream,
1458 &dummy1, &dummy2);
1459 if (val != 0)
1460 {
1461 do_cleanups (old_chain);
1462 return val;
1463 }
1464 }
1465 }
1466 do_cleanups (old_chain);
1467 return 0;
1468 }
1469
1470 static int internal_breakpoint_number = -1;
1471
1472 static struct breakpoint *
1473 create_internal_breakpoint (struct gdbarch *gdbarch,
1474 CORE_ADDR address, enum bptype type)
1475 {
1476 struct symtab_and_line sal;
1477 struct breakpoint *b;
1478
1479 init_sal (&sal); /* initialize to zeroes */
1480
1481 sal.pc = address;
1482 sal.section = find_pc_overlay (sal.pc);
1483
1484 b = set_raw_breakpoint (gdbarch, sal, type);
1485 b->number = internal_breakpoint_number--;
1486 b->disposition = disp_donttouch;
1487
1488 return b;
1489 }
1490
1491 static void
1492 create_overlay_event_breakpoint (char *func_name)
1493 {
1494 struct objfile *objfile;
1495
1496 ALL_OBJFILES (objfile)
1497 {
1498 struct breakpoint *b;
1499 struct minimal_symbol *m;
1500
1501 m = lookup_minimal_symbol_text (func_name, objfile);
1502 if (m == NULL)
1503 continue;
1504
1505 b = create_internal_breakpoint (get_objfile_arch (objfile),
1506 SYMBOL_VALUE_ADDRESS (m),
1507 bp_overlay_event);
1508 b->addr_string = xstrdup (func_name);
1509
1510 if (overlay_debugging == ovly_auto)
1511 {
1512 b->enable_state = bp_enabled;
1513 overlay_events_enabled = 1;
1514 }
1515 else
1516 {
1517 b->enable_state = bp_disabled;
1518 overlay_events_enabled = 0;
1519 }
1520 }
1521 update_global_location_list (1);
1522 }
1523
1524 static void
1525 create_longjmp_master_breakpoint (char *func_name)
1526 {
1527 struct objfile *objfile;
1528
1529 ALL_OBJFILES (objfile)
1530 {
1531 struct breakpoint *b;
1532 struct minimal_symbol *m;
1533
1534 if (!gdbarch_get_longjmp_target_p (get_objfile_arch (objfile)))
1535 continue;
1536
1537 m = lookup_minimal_symbol_text (func_name, objfile);
1538 if (m == NULL)
1539 continue;
1540
1541 b = create_internal_breakpoint (get_objfile_arch (objfile),
1542 SYMBOL_VALUE_ADDRESS (m),
1543 bp_longjmp_master);
1544 b->addr_string = xstrdup (func_name);
1545 b->enable_state = bp_disabled;
1546 }
1547 update_global_location_list (1);
1548 }
1549
1550 void
1551 update_breakpoints_after_exec (void)
1552 {
1553 struct breakpoint *b;
1554 struct breakpoint *temp;
1555 struct bp_location *bploc;
1556
1557 /* We're about to delete breakpoints from GDB's lists. If the
1558 INSERTED flag is true, GDB will try to lift the breakpoints by
1559 writing the breakpoints' "shadow contents" back into memory. The
1560 "shadow contents" are NOT valid after an exec, so GDB should not
1561 do that. Instead, the target is responsible from marking
1562 breakpoints out as soon as it detects an exec. We don't do that
1563 here instead, because there may be other attempts to delete
1564 breakpoints after detecting an exec and before reaching here. */
1565 ALL_BP_LOCATIONS (bploc)
1566 gdb_assert (!bploc->inserted);
1567
1568 ALL_BREAKPOINTS_SAFE (b, temp)
1569 {
1570 /* Solib breakpoints must be explicitly reset after an exec(). */
1571 if (b->type == bp_shlib_event)
1572 {
1573 delete_breakpoint (b);
1574 continue;
1575 }
1576
1577 /* Thread event breakpoints must be set anew after an exec(),
1578 as must overlay event and longjmp master breakpoints. */
1579 if (b->type == bp_thread_event || b->type == bp_overlay_event
1580 || b->type == bp_longjmp_master)
1581 {
1582 delete_breakpoint (b);
1583 continue;
1584 }
1585
1586 /* Step-resume breakpoints are meaningless after an exec(). */
1587 if (b->type == bp_step_resume)
1588 {
1589 delete_breakpoint (b);
1590 continue;
1591 }
1592
1593 /* Longjmp and longjmp-resume breakpoints are also meaningless
1594 after an exec. */
1595 if (b->type == bp_longjmp || b->type == bp_longjmp_resume)
1596 {
1597 delete_breakpoint (b);
1598 continue;
1599 }
1600
1601 if (b->type == bp_catchpoint)
1602 {
1603 /* For now, none of the bp_catchpoint breakpoints need to
1604 do anything at this point. In the future, if some of
1605 the catchpoints need to something, we will need to add
1606 a new method, and call this method from here. */
1607 continue;
1608 }
1609
1610 /* bp_finish is a special case. The only way we ought to be able
1611 to see one of these when an exec() has happened, is if the user
1612 caught a vfork, and then said "finish". Ordinarily a finish just
1613 carries them to the call-site of the current callee, by setting
1614 a temporary bp there and resuming. But in this case, the finish
1615 will carry them entirely through the vfork & exec.
1616
1617 We don't want to allow a bp_finish to remain inserted now. But
1618 we can't safely delete it, 'cause finish_command has a handle to
1619 the bp on a bpstat, and will later want to delete it. There's a
1620 chance (and I've seen it happen) that if we delete the bp_finish
1621 here, that its storage will get reused by the time finish_command
1622 gets 'round to deleting the "use to be a bp_finish" breakpoint.
1623 We really must allow finish_command to delete a bp_finish.
1624
1625 In the absense of a general solution for the "how do we know
1626 it's safe to delete something others may have handles to?"
1627 problem, what we'll do here is just uninsert the bp_finish, and
1628 let finish_command delete it.
1629
1630 (We know the bp_finish is "doomed" in the sense that it's
1631 momentary, and will be deleted as soon as finish_command sees
1632 the inferior stopped. So it doesn't matter that the bp's
1633 address is probably bogus in the new a.out, unlike e.g., the
1634 solib breakpoints.) */
1635
1636 if (b->type == bp_finish)
1637 {
1638 continue;
1639 }
1640
1641 /* Without a symbolic address, we have little hope of the
1642 pre-exec() address meaning the same thing in the post-exec()
1643 a.out. */
1644 if (b->addr_string == NULL)
1645 {
1646 delete_breakpoint (b);
1647 continue;
1648 }
1649 }
1650 /* FIXME what about longjmp breakpoints? Re-create them here? */
1651 create_overlay_event_breakpoint ("_ovly_debug_event");
1652 create_longjmp_master_breakpoint ("longjmp");
1653 create_longjmp_master_breakpoint ("_longjmp");
1654 create_longjmp_master_breakpoint ("siglongjmp");
1655 create_longjmp_master_breakpoint ("_siglongjmp");
1656 }
1657
1658 int
1659 detach_breakpoints (int pid)
1660 {
1661 struct bp_location *b;
1662 int val;
1663 struct cleanup *old_chain = save_inferior_ptid ();
1664
1665 if (pid == PIDGET (inferior_ptid))
1666 error (_("Cannot detach breakpoints of inferior_ptid"));
1667
1668 /* Set inferior_ptid; remove_breakpoint uses this global. */
1669 inferior_ptid = pid_to_ptid (pid);
1670 ALL_BP_LOCATIONS (b)
1671 {
1672 if (b->inserted)
1673 {
1674 val = remove_breakpoint (b, mark_inserted);
1675 if (val != 0)
1676 {
1677 do_cleanups (old_chain);
1678 return val;
1679 }
1680 }
1681 }
1682 do_cleanups (old_chain);
1683 return 0;
1684 }
1685
1686 static int
1687 remove_breakpoint (struct bp_location *b, insertion_state_t is)
1688 {
1689 int val;
1690
1691 if (b->owner->enable_state == bp_permanent)
1692 /* Permanent breakpoints cannot be inserted or removed. */
1693 return 0;
1694
1695 /* The type of none suggests that owner is actually deleted.
1696 This should not ever happen. */
1697 gdb_assert (b->owner->type != bp_none);
1698
1699 if (b->loc_type == bp_loc_software_breakpoint
1700 || b->loc_type == bp_loc_hardware_breakpoint)
1701 {
1702 /* "Normal" instruction breakpoint: either the standard
1703 trap-instruction bp (bp_breakpoint), or a
1704 bp_hardware_breakpoint. */
1705
1706 /* First check to see if we have to handle an overlay. */
1707 if (overlay_debugging == ovly_off
1708 || b->section == NULL
1709 || !(section_is_overlay (b->section)))
1710 {
1711 /* No overlay handling: just remove the breakpoint. */
1712
1713 if (b->loc_type == bp_loc_hardware_breakpoint)
1714 val = target_remove_hw_breakpoint (b->gdbarch, &b->target_info);
1715 else
1716 val = target_remove_breakpoint (b->gdbarch, &b->target_info);
1717 }
1718 else
1719 {
1720 /* This breakpoint is in an overlay section.
1721 Did we set a breakpoint at the LMA? */
1722 if (!overlay_events_enabled)
1723 {
1724 /* Yes -- overlay event support is not active, so we
1725 should have set a breakpoint at the LMA. Remove it.
1726 */
1727 /* Ignore any failures: if the LMA is in ROM, we will
1728 have already warned when we failed to insert it. */
1729 if (b->loc_type == bp_loc_hardware_breakpoint)
1730 target_remove_hw_breakpoint (b->gdbarch,
1731 &b->overlay_target_info);
1732 else
1733 target_remove_breakpoint (b->gdbarch,
1734 &b->overlay_target_info);
1735 }
1736 /* Did we set a breakpoint at the VMA?
1737 If so, we will have marked the breakpoint 'inserted'. */
1738 if (b->inserted)
1739 {
1740 /* Yes -- remove it. Previously we did not bother to
1741 remove the breakpoint if the section had been
1742 unmapped, but let's not rely on that being safe. We
1743 don't know what the overlay manager might do. */
1744 if (b->loc_type == bp_loc_hardware_breakpoint)
1745 val = target_remove_hw_breakpoint (b->gdbarch,
1746 &b->target_info);
1747
1748 /* However, we should remove *software* breakpoints only
1749 if the section is still mapped, or else we overwrite
1750 wrong code with the saved shadow contents. */
1751 else if (section_is_mapped (b->section))
1752 val = target_remove_breakpoint (b->gdbarch,
1753 &b->target_info);
1754 else
1755 val = 0;
1756 }
1757 else
1758 {
1759 /* No -- not inserted, so no need to remove. No error. */
1760 val = 0;
1761 }
1762 }
1763
1764 /* In some cases, we might not be able to remove a breakpoint
1765 in a shared library that has already been removed, but we
1766 have not yet processed the shlib unload event. */
1767 if (val && solib_name_from_address (b->address))
1768 val = 0;
1769
1770 if (val)
1771 return val;
1772 b->inserted = (is == mark_inserted);
1773 }
1774 else if (b->loc_type == bp_loc_hardware_watchpoint)
1775 {
1776 struct value *v;
1777 struct value *n;
1778
1779 b->inserted = (is == mark_inserted);
1780 val = target_remove_watchpoint (b->address, b->length,
1781 b->watchpoint_type);
1782
1783 /* Failure to remove any of the hardware watchpoints comes here. */
1784 if ((is == mark_uninserted) && (b->inserted))
1785 warning (_("Could not remove hardware watchpoint %d."),
1786 b->owner->number);
1787 }
1788 else if (b->owner->type == bp_catchpoint
1789 && breakpoint_enabled (b->owner)
1790 && !b->duplicate)
1791 {
1792 gdb_assert (b->owner->ops != NULL && b->owner->ops->remove != NULL);
1793
1794 val = b->owner->ops->remove (b->owner);
1795 if (val)
1796 return val;
1797 b->inserted = (is == mark_inserted);
1798 }
1799
1800 return 0;
1801 }
1802
1803 /* Clear the "inserted" flag in all breakpoints. */
1804
1805 void
1806 mark_breakpoints_out (void)
1807 {
1808 struct bp_location *bpt;
1809
1810 ALL_BP_LOCATIONS (bpt)
1811 bpt->inserted = 0;
1812 }
1813
1814 /* Clear the "inserted" flag in all breakpoints and delete any
1815 breakpoints which should go away between runs of the program.
1816
1817 Plus other such housekeeping that has to be done for breakpoints
1818 between runs.
1819
1820 Note: this function gets called at the end of a run (by
1821 generic_mourn_inferior) and when a run begins (by
1822 init_wait_for_inferior). */
1823
1824
1825
1826 void
1827 breakpoint_init_inferior (enum inf_context context)
1828 {
1829 struct breakpoint *b, *temp;
1830 struct bp_location *bpt;
1831 int ix;
1832
1833 /* If breakpoint locations are shared across processes, then there's
1834 nothing to do. */
1835 if (gdbarch_has_global_breakpoints (target_gdbarch))
1836 return;
1837
1838 ALL_BP_LOCATIONS (bpt)
1839 if (bpt->owner->enable_state != bp_permanent)
1840 bpt->inserted = 0;
1841
1842 ALL_BREAKPOINTS_SAFE (b, temp)
1843 {
1844 switch (b->type)
1845 {
1846 case bp_call_dummy:
1847 case bp_watchpoint_scope:
1848
1849 /* If the call dummy breakpoint is at the entry point it will
1850 cause problems when the inferior is rerun, so we better
1851 get rid of it.
1852
1853 Also get rid of scope breakpoints. */
1854 delete_breakpoint (b);
1855 break;
1856
1857 case bp_watchpoint:
1858 case bp_hardware_watchpoint:
1859 case bp_read_watchpoint:
1860 case bp_access_watchpoint:
1861
1862 /* Likewise for watchpoints on local expressions. */
1863 if (b->exp_valid_block != NULL)
1864 delete_breakpoint (b);
1865 else if (context == inf_starting)
1866 {
1867 /* Reset val field to force reread of starting value
1868 in insert_breakpoints. */
1869 if (b->val)
1870 value_free (b->val);
1871 b->val = NULL;
1872 b->val_valid = 0;
1873 }
1874 break;
1875 default:
1876 break;
1877 }
1878 }
1879
1880 /* Get rid of the moribund locations. */
1881 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bpt); ++ix)
1882 free_bp_location (bpt);
1883 VEC_free (bp_location_p, moribund_locations);
1884 }
1885
1886 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
1887 exists at PC. It returns ordinary_breakpoint_here if it's an
1888 ordinary breakpoint, or permanent_breakpoint_here if it's a
1889 permanent breakpoint.
1890 - When continuing from a location with an ordinary breakpoint, we
1891 actually single step once before calling insert_breakpoints.
1892 - When continuing from a localion with a permanent breakpoint, we
1893 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
1894 the target, to advance the PC past the breakpoint. */
1895
1896 enum breakpoint_here
1897 breakpoint_here_p (CORE_ADDR pc)
1898 {
1899 const struct bp_location *bpt;
1900 int any_breakpoint_here = 0;
1901
1902 ALL_BP_LOCATIONS (bpt)
1903 {
1904 if (bpt->loc_type != bp_loc_software_breakpoint
1905 && bpt->loc_type != bp_loc_hardware_breakpoint)
1906 continue;
1907
1908 if ((breakpoint_enabled (bpt->owner)
1909 || bpt->owner->enable_state == bp_permanent)
1910 && bpt->address == pc) /* bp is enabled and matches pc */
1911 {
1912 if (overlay_debugging
1913 && section_is_overlay (bpt->section)
1914 && !section_is_mapped (bpt->section))
1915 continue; /* unmapped overlay -- can't be a match */
1916 else if (bpt->owner->enable_state == bp_permanent)
1917 return permanent_breakpoint_here;
1918 else
1919 any_breakpoint_here = 1;
1920 }
1921 }
1922
1923 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
1924 }
1925
1926 /* Return true if there's a moribund breakpoint at PC. */
1927
1928 int
1929 moribund_breakpoint_here_p (CORE_ADDR pc)
1930 {
1931 struct bp_location *loc;
1932 int ix;
1933
1934 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
1935 if (loc->address == pc)
1936 return 1;
1937
1938 return 0;
1939 }
1940
1941 /* Returns non-zero if there's a breakpoint inserted at PC, which is
1942 inserted using regular breakpoint_chain/bp_location_chain mechanism.
1943 This does not check for single-step breakpoints, which are
1944 inserted and removed using direct target manipulation. */
1945
1946 int
1947 regular_breakpoint_inserted_here_p (CORE_ADDR pc)
1948 {
1949 const struct bp_location *bpt;
1950
1951 ALL_BP_LOCATIONS (bpt)
1952 {
1953 if (bpt->loc_type != bp_loc_software_breakpoint
1954 && bpt->loc_type != bp_loc_hardware_breakpoint)
1955 continue;
1956
1957 if (bpt->inserted
1958 && bpt->address == pc) /* bp is inserted and matches pc */
1959 {
1960 if (overlay_debugging
1961 && section_is_overlay (bpt->section)
1962 && !section_is_mapped (bpt->section))
1963 continue; /* unmapped overlay -- can't be a match */
1964 else
1965 return 1;
1966 }
1967 }
1968 return 0;
1969 }
1970
1971 /* Returns non-zero iff there's either regular breakpoint
1972 or a single step breakpoint inserted at PC. */
1973
1974 int
1975 breakpoint_inserted_here_p (CORE_ADDR pc)
1976 {
1977 if (regular_breakpoint_inserted_here_p (pc))
1978 return 1;
1979
1980 if (single_step_breakpoint_inserted_here_p (pc))
1981 return 1;
1982
1983 return 0;
1984 }
1985
1986 /* This function returns non-zero iff there is a software breakpoint
1987 inserted at PC. */
1988
1989 int
1990 software_breakpoint_inserted_here_p (CORE_ADDR pc)
1991 {
1992 const struct bp_location *bpt;
1993 int any_breakpoint_here = 0;
1994
1995 ALL_BP_LOCATIONS (bpt)
1996 {
1997 if (bpt->loc_type != bp_loc_software_breakpoint)
1998 continue;
1999
2000 if (bpt->inserted
2001 && bpt->address == pc) /* bp is enabled and matches pc */
2002 {
2003 if (overlay_debugging
2004 && section_is_overlay (bpt->section)
2005 && !section_is_mapped (bpt->section))
2006 continue; /* unmapped overlay -- can't be a match */
2007 else
2008 return 1;
2009 }
2010 }
2011
2012 /* Also check for software single-step breakpoints. */
2013 if (single_step_breakpoint_inserted_here_p (pc))
2014 return 1;
2015
2016 return 0;
2017 }
2018
2019 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2020 PC is valid for process/thread PTID. */
2021
2022 int
2023 breakpoint_thread_match (CORE_ADDR pc, ptid_t ptid)
2024 {
2025 const struct bp_location *bpt;
2026 /* The thread and task IDs associated to PTID, computed lazily. */
2027 int thread = -1;
2028 int task = 0;
2029
2030 ALL_BP_LOCATIONS (bpt)
2031 {
2032 if (bpt->loc_type != bp_loc_software_breakpoint
2033 && bpt->loc_type != bp_loc_hardware_breakpoint)
2034 continue;
2035
2036 if (!breakpoint_enabled (bpt->owner)
2037 && bpt->owner->enable_state != bp_permanent)
2038 continue;
2039
2040 if (bpt->address != pc)
2041 continue;
2042
2043 if (bpt->owner->thread != -1)
2044 {
2045 /* This is a thread-specific breakpoint. Check that ptid
2046 matches that thread. If thread hasn't been computed yet,
2047 it is now time to do so. */
2048 if (thread == -1)
2049 thread = pid_to_thread_id (ptid);
2050 if (bpt->owner->thread != thread)
2051 continue;
2052 }
2053
2054 if (bpt->owner->task != 0)
2055 {
2056 /* This is a task-specific breakpoint. Check that ptid
2057 matches that task. If task hasn't been computed yet,
2058 it is now time to do so. */
2059 if (task == 0)
2060 task = ada_get_task_number (ptid);
2061 if (bpt->owner->task != task)
2062 continue;
2063 }
2064
2065 if (overlay_debugging
2066 && section_is_overlay (bpt->section)
2067 && !section_is_mapped (bpt->section))
2068 continue; /* unmapped overlay -- can't be a match */
2069
2070 return 1;
2071 }
2072
2073 return 0;
2074 }
2075 \f
2076
2077 /* bpstat stuff. External routines' interfaces are documented
2078 in breakpoint.h. */
2079
2080 int
2081 ep_is_catchpoint (struct breakpoint *ep)
2082 {
2083 return (ep->type == bp_catchpoint);
2084 }
2085
2086 void
2087 bpstat_free (bpstat bs)
2088 {
2089 if (bs->old_val != NULL)
2090 value_free (bs->old_val);
2091 free_command_lines (&bs->commands);
2092 xfree (bs);
2093 }
2094
2095 /* Clear a bpstat so that it says we are not at any breakpoint.
2096 Also free any storage that is part of a bpstat. */
2097
2098 void
2099 bpstat_clear (bpstat *bsp)
2100 {
2101 bpstat p;
2102 bpstat q;
2103
2104 if (bsp == 0)
2105 return;
2106 p = *bsp;
2107 while (p != NULL)
2108 {
2109 q = p->next;
2110 bpstat_free (p);
2111 p = q;
2112 }
2113 *bsp = NULL;
2114 }
2115
2116 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
2117 is part of the bpstat is copied as well. */
2118
2119 bpstat
2120 bpstat_copy (bpstat bs)
2121 {
2122 bpstat p = NULL;
2123 bpstat tmp;
2124 bpstat retval = NULL;
2125
2126 if (bs == NULL)
2127 return bs;
2128
2129 for (; bs != NULL; bs = bs->next)
2130 {
2131 tmp = (bpstat) xmalloc (sizeof (*tmp));
2132 memcpy (tmp, bs, sizeof (*tmp));
2133 if (bs->commands != NULL)
2134 tmp->commands = copy_command_lines (bs->commands);
2135 if (bs->old_val != NULL)
2136 {
2137 tmp->old_val = value_copy (bs->old_val);
2138 release_value (tmp->old_val);
2139 }
2140
2141 if (p == NULL)
2142 /* This is the first thing in the chain. */
2143 retval = tmp;
2144 else
2145 p->next = tmp;
2146 p = tmp;
2147 }
2148 p->next = NULL;
2149 return retval;
2150 }
2151
2152 /* Find the bpstat associated with this breakpoint */
2153
2154 bpstat
2155 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
2156 {
2157 if (bsp == NULL)
2158 return NULL;
2159
2160 for (; bsp != NULL; bsp = bsp->next)
2161 {
2162 if (bsp->breakpoint_at && bsp->breakpoint_at->owner == breakpoint)
2163 return bsp;
2164 }
2165 return NULL;
2166 }
2167
2168 /* Find a step_resume breakpoint associated with this bpstat.
2169 (If there are multiple step_resume bp's on the list, this function
2170 will arbitrarily pick one.)
2171
2172 It is an error to use this function if BPSTAT doesn't contain a
2173 step_resume breakpoint.
2174
2175 See wait_for_inferior's use of this function. */
2176 struct breakpoint *
2177 bpstat_find_step_resume_breakpoint (bpstat bsp)
2178 {
2179 int current_thread;
2180
2181 gdb_assert (bsp != NULL);
2182
2183 current_thread = pid_to_thread_id (inferior_ptid);
2184
2185 for (; bsp != NULL; bsp = bsp->next)
2186 {
2187 if ((bsp->breakpoint_at != NULL)
2188 && (bsp->breakpoint_at->owner->type == bp_step_resume)
2189 && (bsp->breakpoint_at->owner->thread == current_thread
2190 || bsp->breakpoint_at->owner->thread == -1))
2191 return bsp->breakpoint_at->owner;
2192 }
2193
2194 internal_error (__FILE__, __LINE__, _("No step_resume breakpoint found."));
2195 }
2196
2197
2198 /* Put in *NUM the breakpoint number of the first breakpoint we are stopped
2199 at. *BSP upon return is a bpstat which points to the remaining
2200 breakpoints stopped at (but which is not guaranteed to be good for
2201 anything but further calls to bpstat_num).
2202 Return 0 if passed a bpstat which does not indicate any breakpoints.
2203 Return -1 if stopped at a breakpoint that has been deleted since
2204 we set it.
2205 Return 1 otherwise. */
2206
2207 int
2208 bpstat_num (bpstat *bsp, int *num)
2209 {
2210 struct breakpoint *b;
2211
2212 if ((*bsp) == NULL)
2213 return 0; /* No more breakpoint values */
2214
2215 /* We assume we'll never have several bpstats that
2216 correspond to a single breakpoint -- otherwise,
2217 this function might return the same number more
2218 than once and this will look ugly. */
2219 b = (*bsp)->breakpoint_at ? (*bsp)->breakpoint_at->owner : NULL;
2220 *bsp = (*bsp)->next;
2221 if (b == NULL)
2222 return -1; /* breakpoint that's been deleted since */
2223
2224 *num = b->number; /* We have its number */
2225 return 1;
2226 }
2227
2228 /* Modify BS so that the actions will not be performed. */
2229
2230 void
2231 bpstat_clear_actions (bpstat bs)
2232 {
2233 for (; bs != NULL; bs = bs->next)
2234 {
2235 free_command_lines (&bs->commands);
2236 if (bs->old_val != NULL)
2237 {
2238 value_free (bs->old_val);
2239 bs->old_val = NULL;
2240 }
2241 }
2242 }
2243
2244 /* Called when a command is about to proceed the inferior. */
2245
2246 static void
2247 breakpoint_about_to_proceed (void)
2248 {
2249 if (!ptid_equal (inferior_ptid, null_ptid))
2250 {
2251 struct thread_info *tp = inferior_thread ();
2252
2253 /* Allow inferior function calls in breakpoint commands to not
2254 interrupt the command list. When the call finishes
2255 successfully, the inferior will be standing at the same
2256 breakpoint as if nothing happened. */
2257 if (tp->in_infcall)
2258 return;
2259 }
2260
2261 breakpoint_proceeded = 1;
2262 }
2263
2264 /* Stub for cleaning up our state if we error-out of a breakpoint command */
2265 static void
2266 cleanup_executing_breakpoints (void *ignore)
2267 {
2268 executing_breakpoint_commands = 0;
2269 }
2270
2271 /* Execute all the commands associated with all the breakpoints at this
2272 location. Any of these commands could cause the process to proceed
2273 beyond this point, etc. We look out for such changes by checking
2274 the global "breakpoint_proceeded" after each command.
2275
2276 Returns true if a breakpoint command resumed the inferior. In that
2277 case, it is the caller's responsibility to recall it again with the
2278 bpstat of the current thread. */
2279
2280 static int
2281 bpstat_do_actions_1 (bpstat *bsp)
2282 {
2283 bpstat bs;
2284 struct cleanup *old_chain;
2285 int again = 0;
2286
2287 /* Avoid endless recursion if a `source' command is contained
2288 in bs->commands. */
2289 if (executing_breakpoint_commands)
2290 return 0;
2291
2292 executing_breakpoint_commands = 1;
2293 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
2294
2295 /* This pointer will iterate over the list of bpstat's. */
2296 bs = *bsp;
2297
2298 breakpoint_proceeded = 0;
2299 for (; bs != NULL; bs = bs->next)
2300 {
2301 struct command_line *cmd;
2302 struct cleanup *this_cmd_tree_chain;
2303
2304 /* Take ownership of the BSP's command tree, if it has one.
2305
2306 The command tree could legitimately contain commands like
2307 'step' and 'next', which call clear_proceed_status, which
2308 frees stop_bpstat's command tree. To make sure this doesn't
2309 free the tree we're executing out from under us, we need to
2310 take ownership of the tree ourselves. Since a given bpstat's
2311 commands are only executed once, we don't need to copy it; we
2312 can clear the pointer in the bpstat, and make sure we free
2313 the tree when we're done. */
2314 cmd = bs->commands;
2315 bs->commands = 0;
2316 this_cmd_tree_chain = make_cleanup_free_command_lines (&cmd);
2317
2318 while (cmd != NULL)
2319 {
2320 execute_control_command (cmd);
2321
2322 if (breakpoint_proceeded)
2323 break;
2324 else
2325 cmd = cmd->next;
2326 }
2327
2328 /* We can free this command tree now. */
2329 do_cleanups (this_cmd_tree_chain);
2330
2331 if (breakpoint_proceeded)
2332 {
2333 if (target_can_async_p ())
2334 /* If we are in async mode, then the target might be still
2335 running, not stopped at any breakpoint, so nothing for
2336 us to do here -- just return to the event loop. */
2337 ;
2338 else
2339 /* In sync mode, when execute_control_command returns
2340 we're already standing on the next breakpoint.
2341 Breakpoint commands for that stop were not run, since
2342 execute_command does not run breakpoint commands --
2343 only command_line_handler does, but that one is not
2344 involved in execution of breakpoint commands. So, we
2345 can now execute breakpoint commands. It should be
2346 noted that making execute_command do bpstat actions is
2347 not an option -- in this case we'll have recursive
2348 invocation of bpstat for each breakpoint with a
2349 command, and can easily blow up GDB stack. Instead, we
2350 return true, which will trigger the caller to recall us
2351 with the new stop_bpstat. */
2352 again = 1;
2353 break;
2354 }
2355 }
2356 do_cleanups (old_chain);
2357 return again;
2358 }
2359
2360 void
2361 bpstat_do_actions (void)
2362 {
2363 /* Do any commands attached to breakpoint we are stopped at. */
2364 while (!ptid_equal (inferior_ptid, null_ptid)
2365 && target_has_execution
2366 && !is_exited (inferior_ptid)
2367 && !is_executing (inferior_ptid))
2368 /* Since in sync mode, bpstat_do_actions may resume the inferior,
2369 and only return when it is stopped at the next breakpoint, we
2370 keep doing breakpoint actions until it returns false to
2371 indicate the inferior was not resumed. */
2372 if (!bpstat_do_actions_1 (&inferior_thread ()->stop_bpstat))
2373 break;
2374 }
2375
2376 /* Print out the (old or new) value associated with a watchpoint. */
2377
2378 static void
2379 watchpoint_value_print (struct value *val, struct ui_file *stream)
2380 {
2381 if (val == NULL)
2382 fprintf_unfiltered (stream, _("<unreadable>"));
2383 else
2384 {
2385 struct value_print_options opts;
2386 get_user_print_options (&opts);
2387 value_print (val, stream, &opts);
2388 }
2389 }
2390
2391 /* This is the normal print function for a bpstat. In the future,
2392 much of this logic could (should?) be moved to bpstat_stop_status,
2393 by having it set different print_it values.
2394
2395 Current scheme: When we stop, bpstat_print() is called. It loops
2396 through the bpstat list of things causing this stop, calling the
2397 print_bp_stop_message function on each one. The behavior of the
2398 print_bp_stop_message function depends on the print_it field of
2399 bpstat. If such field so indicates, call this function here.
2400
2401 Return values from this routine (ultimately used by bpstat_print()
2402 and normal_stop() to decide what to do):
2403 PRINT_NOTHING: Means we already printed all we needed to print,
2404 don't print anything else.
2405 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
2406 that something to be followed by a location.
2407 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
2408 that something to be followed by a location.
2409 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
2410 analysis. */
2411
2412 static enum print_stop_action
2413 print_it_typical (bpstat bs)
2414 {
2415 struct cleanup *old_chain;
2416 struct breakpoint *b;
2417 const struct bp_location *bl;
2418 struct ui_stream *stb;
2419 int bp_temp = 0;
2420 enum print_stop_action result;
2421
2422 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
2423 which has since been deleted. */
2424 if (bs->breakpoint_at == NULL)
2425 return PRINT_UNKNOWN;
2426 bl = bs->breakpoint_at;
2427 b = bl->owner;
2428
2429 stb = ui_out_stream_new (uiout);
2430 old_chain = make_cleanup_ui_out_stream_delete (stb);
2431
2432 switch (b->type)
2433 {
2434 case bp_breakpoint:
2435 case bp_hardware_breakpoint:
2436 bp_temp = bs->breakpoint_at->owner->disposition == disp_del;
2437 if (bl->address != bl->requested_address)
2438 breakpoint_adjustment_warning (bl->requested_address,
2439 bl->address,
2440 b->number, 1);
2441 annotate_breakpoint (b->number);
2442 if (bp_temp)
2443 ui_out_text (uiout, "\nTemporary breakpoint ");
2444 else
2445 ui_out_text (uiout, "\nBreakpoint ");
2446 if (ui_out_is_mi_like_p (uiout))
2447 {
2448 ui_out_field_string (uiout, "reason",
2449 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
2450 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
2451 }
2452 ui_out_field_int (uiout, "bkptno", b->number);
2453 ui_out_text (uiout, ", ");
2454 result = PRINT_SRC_AND_LOC;
2455 break;
2456
2457 case bp_shlib_event:
2458 /* Did we stop because the user set the stop_on_solib_events
2459 variable? (If so, we report this as a generic, "Stopped due
2460 to shlib event" message.) */
2461 printf_filtered (_("Stopped due to shared library event\n"));
2462 result = PRINT_NOTHING;
2463 break;
2464
2465 case bp_thread_event:
2466 /* Not sure how we will get here.
2467 GDB should not stop for these breakpoints. */
2468 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
2469 result = PRINT_NOTHING;
2470 break;
2471
2472 case bp_overlay_event:
2473 /* By analogy with the thread event, GDB should not stop for these. */
2474 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
2475 result = PRINT_NOTHING;
2476 break;
2477
2478 case bp_longjmp_master:
2479 /* These should never be enabled. */
2480 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
2481 result = PRINT_NOTHING;
2482 break;
2483
2484 case bp_watchpoint:
2485 case bp_hardware_watchpoint:
2486 annotate_watchpoint (b->number);
2487 if (ui_out_is_mi_like_p (uiout))
2488 ui_out_field_string
2489 (uiout, "reason",
2490 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
2491 mention (b);
2492 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2493 ui_out_text (uiout, "\nOld value = ");
2494 watchpoint_value_print (bs->old_val, stb->stream);
2495 ui_out_field_stream (uiout, "old", stb);
2496 ui_out_text (uiout, "\nNew value = ");
2497 watchpoint_value_print (b->val, stb->stream);
2498 ui_out_field_stream (uiout, "new", stb);
2499 ui_out_text (uiout, "\n");
2500 /* More than one watchpoint may have been triggered. */
2501 result = PRINT_UNKNOWN;
2502 break;
2503
2504 case bp_read_watchpoint:
2505 if (ui_out_is_mi_like_p (uiout))
2506 ui_out_field_string
2507 (uiout, "reason",
2508 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
2509 mention (b);
2510 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2511 ui_out_text (uiout, "\nValue = ");
2512 watchpoint_value_print (b->val, stb->stream);
2513 ui_out_field_stream (uiout, "value", stb);
2514 ui_out_text (uiout, "\n");
2515 result = PRINT_UNKNOWN;
2516 break;
2517
2518 case bp_access_watchpoint:
2519 if (bs->old_val != NULL)
2520 {
2521 annotate_watchpoint (b->number);
2522 if (ui_out_is_mi_like_p (uiout))
2523 ui_out_field_string
2524 (uiout, "reason",
2525 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
2526 mention (b);
2527 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2528 ui_out_text (uiout, "\nOld value = ");
2529 watchpoint_value_print (bs->old_val, stb->stream);
2530 ui_out_field_stream (uiout, "old", stb);
2531 ui_out_text (uiout, "\nNew value = ");
2532 }
2533 else
2534 {
2535 mention (b);
2536 if (ui_out_is_mi_like_p (uiout))
2537 ui_out_field_string
2538 (uiout, "reason",
2539 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
2540 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2541 ui_out_text (uiout, "\nValue = ");
2542 }
2543 watchpoint_value_print (b->val, stb->stream);
2544 ui_out_field_stream (uiout, "new", stb);
2545 ui_out_text (uiout, "\n");
2546 result = PRINT_UNKNOWN;
2547 break;
2548
2549 /* Fall through, we don't deal with these types of breakpoints
2550 here. */
2551
2552 case bp_finish:
2553 if (ui_out_is_mi_like_p (uiout))
2554 ui_out_field_string
2555 (uiout, "reason",
2556 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
2557 result = PRINT_UNKNOWN;
2558 break;
2559
2560 case bp_until:
2561 if (ui_out_is_mi_like_p (uiout))
2562 ui_out_field_string
2563 (uiout, "reason",
2564 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
2565 result = PRINT_UNKNOWN;
2566 break;
2567
2568 case bp_none:
2569 case bp_longjmp:
2570 case bp_longjmp_resume:
2571 case bp_step_resume:
2572 case bp_watchpoint_scope:
2573 case bp_call_dummy:
2574 case bp_tracepoint:
2575 default:
2576 result = PRINT_UNKNOWN;
2577 break;
2578 }
2579
2580 do_cleanups (old_chain);
2581 return result;
2582 }
2583
2584 /* Generic routine for printing messages indicating why we
2585 stopped. The behavior of this function depends on the value
2586 'print_it' in the bpstat structure. Under some circumstances we
2587 may decide not to print anything here and delegate the task to
2588 normal_stop(). */
2589
2590 static enum print_stop_action
2591 print_bp_stop_message (bpstat bs)
2592 {
2593 switch (bs->print_it)
2594 {
2595 case print_it_noop:
2596 /* Nothing should be printed for this bpstat entry. */
2597 return PRINT_UNKNOWN;
2598 break;
2599
2600 case print_it_done:
2601 /* We still want to print the frame, but we already printed the
2602 relevant messages. */
2603 return PRINT_SRC_AND_LOC;
2604 break;
2605
2606 case print_it_normal:
2607 {
2608 const struct bp_location *bl = bs->breakpoint_at;
2609 struct breakpoint *b = bl ? bl->owner : NULL;
2610
2611 /* Normal case. Call the breakpoint's print_it method, or
2612 print_it_typical. */
2613 /* FIXME: how breakpoint can ever be NULL here? */
2614 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
2615 return b->ops->print_it (b);
2616 else
2617 return print_it_typical (bs);
2618 }
2619 break;
2620
2621 default:
2622 internal_error (__FILE__, __LINE__,
2623 _("print_bp_stop_message: unrecognized enum value"));
2624 break;
2625 }
2626 }
2627
2628 /* Print a message indicating what happened. This is called from
2629 normal_stop(). The input to this routine is the head of the bpstat
2630 list - a list of the eventpoints that caused this stop. This
2631 routine calls the generic print routine for printing a message
2632 about reasons for stopping. This will print (for example) the
2633 "Breakpoint n," part of the output. The return value of this
2634 routine is one of:
2635
2636 PRINT_UNKNOWN: Means we printed nothing
2637 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
2638 code to print the location. An example is
2639 "Breakpoint 1, " which should be followed by
2640 the location.
2641 PRINT_SRC_ONLY: Means we printed something, but there is no need
2642 to also print the location part of the message.
2643 An example is the catch/throw messages, which
2644 don't require a location appended to the end.
2645 PRINT_NOTHING: We have done some printing and we don't need any
2646 further info to be printed.*/
2647
2648 enum print_stop_action
2649 bpstat_print (bpstat bs)
2650 {
2651 int val;
2652
2653 /* Maybe another breakpoint in the chain caused us to stop.
2654 (Currently all watchpoints go on the bpstat whether hit or not.
2655 That probably could (should) be changed, provided care is taken
2656 with respect to bpstat_explains_signal). */
2657 for (; bs; bs = bs->next)
2658 {
2659 val = print_bp_stop_message (bs);
2660 if (val == PRINT_SRC_ONLY
2661 || val == PRINT_SRC_AND_LOC
2662 || val == PRINT_NOTHING)
2663 return val;
2664 }
2665
2666 /* We reached the end of the chain, or we got a null BS to start
2667 with and nothing was printed. */
2668 return PRINT_UNKNOWN;
2669 }
2670
2671 /* Evaluate the expression EXP and return 1 if value is zero.
2672 This is used inside a catch_errors to evaluate the breakpoint condition.
2673 The argument is a "struct expression *" that has been cast to char * to
2674 make it pass through catch_errors. */
2675
2676 static int
2677 breakpoint_cond_eval (void *exp)
2678 {
2679 struct value *mark = value_mark ();
2680 int i = !value_true (evaluate_expression ((struct expression *) exp));
2681 value_free_to_mark (mark);
2682 return i;
2683 }
2684
2685 /* Allocate a new bpstat and chain it to the current one. */
2686
2687 static bpstat
2688 bpstat_alloc (const struct bp_location *bl, bpstat cbs /* Current "bs" value */ )
2689 {
2690 bpstat bs;
2691
2692 bs = (bpstat) xmalloc (sizeof (*bs));
2693 cbs->next = bs;
2694 bs->breakpoint_at = bl;
2695 /* If the condition is false, etc., don't do the commands. */
2696 bs->commands = NULL;
2697 bs->old_val = NULL;
2698 bs->print_it = print_it_normal;
2699 return bs;
2700 }
2701 \f
2702 /* The target has stopped with waitstatus WS. Check if any hardware
2703 watchpoints have triggered, according to the target. */
2704
2705 int
2706 watchpoints_triggered (struct target_waitstatus *ws)
2707 {
2708 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
2709 CORE_ADDR addr;
2710 struct breakpoint *b;
2711
2712 if (!stopped_by_watchpoint)
2713 {
2714 /* We were not stopped by a watchpoint. Mark all watchpoints
2715 as not triggered. */
2716 ALL_BREAKPOINTS (b)
2717 if (b->type == bp_hardware_watchpoint
2718 || b->type == bp_read_watchpoint
2719 || b->type == bp_access_watchpoint)
2720 b->watchpoint_triggered = watch_triggered_no;
2721
2722 return 0;
2723 }
2724
2725 if (!target_stopped_data_address (&current_target, &addr))
2726 {
2727 /* We were stopped by a watchpoint, but we don't know where.
2728 Mark all watchpoints as unknown. */
2729 ALL_BREAKPOINTS (b)
2730 if (b->type == bp_hardware_watchpoint
2731 || b->type == bp_read_watchpoint
2732 || b->type == bp_access_watchpoint)
2733 b->watchpoint_triggered = watch_triggered_unknown;
2734
2735 return stopped_by_watchpoint;
2736 }
2737
2738 /* The target could report the data address. Mark watchpoints
2739 affected by this data address as triggered, and all others as not
2740 triggered. */
2741
2742 ALL_BREAKPOINTS (b)
2743 if (b->type == bp_hardware_watchpoint
2744 || b->type == bp_read_watchpoint
2745 || b->type == bp_access_watchpoint)
2746 {
2747 struct bp_location *loc;
2748 struct value *v;
2749
2750 b->watchpoint_triggered = watch_triggered_no;
2751 for (loc = b->loc; loc; loc = loc->next)
2752 /* Exact match not required. Within range is
2753 sufficient. */
2754 if (target_watchpoint_addr_within_range (&current_target,
2755 addr, loc->address,
2756 loc->length))
2757 {
2758 b->watchpoint_triggered = watch_triggered_yes;
2759 break;
2760 }
2761 }
2762
2763 return 1;
2764 }
2765
2766 /* Possible return values for watchpoint_check (this can't be an enum
2767 because of check_errors). */
2768 /* The watchpoint has been deleted. */
2769 #define WP_DELETED 1
2770 /* The value has changed. */
2771 #define WP_VALUE_CHANGED 2
2772 /* The value has not changed. */
2773 #define WP_VALUE_NOT_CHANGED 3
2774
2775 #define BP_TEMPFLAG 1
2776 #define BP_HARDWAREFLAG 2
2777
2778 /* Check watchpoint condition. */
2779
2780 static int
2781 watchpoint_check (void *p)
2782 {
2783 bpstat bs = (bpstat) p;
2784 struct breakpoint *b;
2785 struct frame_info *fr;
2786 int within_current_scope;
2787
2788 b = bs->breakpoint_at->owner;
2789
2790 if (b->exp_valid_block == NULL)
2791 within_current_scope = 1;
2792 else
2793 {
2794 struct frame_info *frame = get_current_frame ();
2795 struct gdbarch *frame_arch = get_frame_arch (frame);
2796 CORE_ADDR frame_pc = get_frame_pc (frame);
2797
2798 fr = frame_find_by_id (b->watchpoint_frame);
2799 within_current_scope = (fr != NULL);
2800
2801 /* If we've gotten confused in the unwinder, we might have
2802 returned a frame that can't describe this variable. */
2803 if (within_current_scope)
2804 {
2805 struct symbol *function;
2806
2807 function = get_frame_function (fr);
2808 if (function == NULL
2809 || !contained_in (b->exp_valid_block,
2810 SYMBOL_BLOCK_VALUE (function)))
2811 within_current_scope = 0;
2812 }
2813
2814 /* in_function_epilogue_p() returns a non-zero value if we're still
2815 in the function but the stack frame has already been invalidated.
2816 Since we can't rely on the values of local variables after the
2817 stack has been destroyed, we are treating the watchpoint in that
2818 state as `not changed' without further checking. Don't mark
2819 watchpoints as changed if the current frame is in an epilogue -
2820 even if they are in some other frame, our view of the stack
2821 is likely to be wrong. */
2822 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
2823 return WP_VALUE_NOT_CHANGED;
2824
2825 if (within_current_scope)
2826 /* If we end up stopping, the current frame will get selected
2827 in normal_stop. So this call to select_frame won't affect
2828 the user. */
2829 select_frame (fr);
2830 }
2831
2832 if (within_current_scope)
2833 {
2834 /* We use value_{,free_to_}mark because it could be a
2835 *long* time before we return to the command level and
2836 call free_all_values. We can't call free_all_values because
2837 we might be in the middle of evaluating a function call. */
2838
2839 struct value *mark = value_mark ();
2840 struct value *new_val;
2841
2842 fetch_watchpoint_value (b->exp, &new_val, NULL, NULL);
2843 if ((b->val != NULL) != (new_val != NULL)
2844 || (b->val != NULL && !value_equal (b->val, new_val)))
2845 {
2846 if (new_val != NULL)
2847 {
2848 release_value (new_val);
2849 value_free_to_mark (mark);
2850 }
2851 bs->old_val = b->val;
2852 b->val = new_val;
2853 b->val_valid = 1;
2854 /* We will stop here */
2855 return WP_VALUE_CHANGED;
2856 }
2857 else
2858 {
2859 /* Nothing changed, don't do anything. */
2860 value_free_to_mark (mark);
2861 /* We won't stop here */
2862 return WP_VALUE_NOT_CHANGED;
2863 }
2864 }
2865 else
2866 {
2867 /* This seems like the only logical thing to do because
2868 if we temporarily ignored the watchpoint, then when
2869 we reenter the block in which it is valid it contains
2870 garbage (in the case of a function, it may have two
2871 garbage values, one before and one after the prologue).
2872 So we can't even detect the first assignment to it and
2873 watch after that (since the garbage may or may not equal
2874 the first value assigned). */
2875 /* We print all the stop information in print_it_typical(), but
2876 in this case, by the time we call print_it_typical() this bp
2877 will be deleted already. So we have no choice but print the
2878 information here. */
2879 if (ui_out_is_mi_like_p (uiout))
2880 ui_out_field_string
2881 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
2882 ui_out_text (uiout, "\nWatchpoint ");
2883 ui_out_field_int (uiout, "wpnum", b->number);
2884 ui_out_text (uiout, " deleted because the program has left the block in\n\
2885 which its expression is valid.\n");
2886
2887 if (b->related_breakpoint)
2888 b->related_breakpoint->disposition = disp_del_at_next_stop;
2889 b->disposition = disp_del_at_next_stop;
2890
2891 return WP_DELETED;
2892 }
2893 }
2894
2895 /* Return true if it looks like target has stopped due to hitting
2896 breakpoint location BL. This function does not check if we
2897 should stop, only if BL explains the stop. */
2898 static int
2899 bpstat_check_location (const struct bp_location *bl, CORE_ADDR bp_addr)
2900 {
2901 struct breakpoint *b = bl->owner;
2902
2903 if (b->type != bp_watchpoint
2904 && b->type != bp_hardware_watchpoint
2905 && b->type != bp_read_watchpoint
2906 && b->type != bp_access_watchpoint
2907 && b->type != bp_hardware_breakpoint
2908 && b->type != bp_catchpoint) /* a non-watchpoint bp */
2909 {
2910 if (bl->address != bp_addr) /* address doesn't match */
2911 return 0;
2912 if (overlay_debugging /* unmapped overlay section */
2913 && section_is_overlay (bl->section)
2914 && !section_is_mapped (bl->section))
2915 return 0;
2916 }
2917
2918 /* Continuable hardware watchpoints are treated as non-existent if the
2919 reason we stopped wasn't a hardware watchpoint (we didn't stop on
2920 some data address). Otherwise gdb won't stop on a break instruction
2921 in the code (not from a breakpoint) when a hardware watchpoint has
2922 been defined. Also skip watchpoints which we know did not trigger
2923 (did not match the data address). */
2924
2925 if ((b->type == bp_hardware_watchpoint
2926 || b->type == bp_read_watchpoint
2927 || b->type == bp_access_watchpoint)
2928 && b->watchpoint_triggered == watch_triggered_no)
2929 return 0;
2930
2931 if (b->type == bp_hardware_breakpoint)
2932 {
2933 if (bl->address != bp_addr)
2934 return 0;
2935 if (overlay_debugging /* unmapped overlay section */
2936 && section_is_overlay (bl->section)
2937 && !section_is_mapped (bl->section))
2938 return 0;
2939 }
2940
2941 if (b->type == bp_catchpoint)
2942 {
2943 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
2944 if (!b->ops->breakpoint_hit (b))
2945 return 0;
2946 }
2947
2948 return 1;
2949 }
2950
2951 /* If BS refers to a watchpoint, determine if the watched values
2952 has actually changed, and we should stop. If not, set BS->stop
2953 to 0. */
2954 static void
2955 bpstat_check_watchpoint (bpstat bs)
2956 {
2957 const struct bp_location *bl = bs->breakpoint_at;
2958 struct breakpoint *b = bl->owner;
2959
2960 if (b->type == bp_watchpoint
2961 || b->type == bp_read_watchpoint
2962 || b->type == bp_access_watchpoint
2963 || b->type == bp_hardware_watchpoint)
2964 {
2965 CORE_ADDR addr;
2966 struct value *v;
2967 int must_check_value = 0;
2968
2969 if (b->type == bp_watchpoint)
2970 /* For a software watchpoint, we must always check the
2971 watched value. */
2972 must_check_value = 1;
2973 else if (b->watchpoint_triggered == watch_triggered_yes)
2974 /* We have a hardware watchpoint (read, write, or access)
2975 and the target earlier reported an address watched by
2976 this watchpoint. */
2977 must_check_value = 1;
2978 else if (b->watchpoint_triggered == watch_triggered_unknown
2979 && b->type == bp_hardware_watchpoint)
2980 /* We were stopped by a hardware watchpoint, but the target could
2981 not report the data address. We must check the watchpoint's
2982 value. Access and read watchpoints are out of luck; without
2983 a data address, we can't figure it out. */
2984 must_check_value = 1;
2985
2986 if (must_check_value)
2987 {
2988 char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
2989 b->number);
2990 struct cleanup *cleanups = make_cleanup (xfree, message);
2991 int e = catch_errors (watchpoint_check, bs, message,
2992 RETURN_MASK_ALL);
2993 do_cleanups (cleanups);
2994 switch (e)
2995 {
2996 case WP_DELETED:
2997 /* We've already printed what needs to be printed. */
2998 bs->print_it = print_it_done;
2999 /* Stop. */
3000 break;
3001 case WP_VALUE_CHANGED:
3002 if (b->type == bp_read_watchpoint)
3003 {
3004 /* Don't stop: read watchpoints shouldn't fire if
3005 the value has changed. This is for targets
3006 which cannot set read-only watchpoints. */
3007 bs->print_it = print_it_noop;
3008 bs->stop = 0;
3009 }
3010 break;
3011 case WP_VALUE_NOT_CHANGED:
3012 if (b->type == bp_hardware_watchpoint
3013 || b->type == bp_watchpoint)
3014 {
3015 /* Don't stop: write watchpoints shouldn't fire if
3016 the value hasn't changed. */
3017 bs->print_it = print_it_noop;
3018 bs->stop = 0;
3019 }
3020 /* Stop. */
3021 break;
3022 default:
3023 /* Can't happen. */
3024 case 0:
3025 /* Error from catch_errors. */
3026 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
3027 if (b->related_breakpoint)
3028 b->related_breakpoint->disposition = disp_del_at_next_stop;
3029 b->disposition = disp_del_at_next_stop;
3030 /* We've already printed what needs to be printed. */
3031 bs->print_it = print_it_done;
3032 break;
3033 }
3034 }
3035 else /* must_check_value == 0 */
3036 {
3037 /* This is a case where some watchpoint(s) triggered, but
3038 not at the address of this watchpoint, or else no
3039 watchpoint triggered after all. So don't print
3040 anything for this watchpoint. */
3041 bs->print_it = print_it_noop;
3042 bs->stop = 0;
3043 }
3044 }
3045 }
3046
3047
3048 /* Check conditions (condition proper, frame, thread and ignore count)
3049 of breakpoint referred to by BS. If we should not stop for this
3050 breakpoint, set BS->stop to 0. */
3051 static void
3052 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3053 {
3054 int thread_id = pid_to_thread_id (ptid);
3055 const struct bp_location *bl = bs->breakpoint_at;
3056 struct breakpoint *b = bl->owner;
3057
3058 if (frame_id_p (b->frame_id)
3059 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
3060 bs->stop = 0;
3061 else if (bs->stop)
3062 {
3063 int value_is_zero = 0;
3064
3065 /* If this is a scope breakpoint, mark the associated
3066 watchpoint as triggered so that we will handle the
3067 out-of-scope event. We'll get to the watchpoint next
3068 iteration. */
3069 if (b->type == bp_watchpoint_scope)
3070 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
3071
3072 if (bl->cond && bl->owner->disposition != disp_del_at_next_stop)
3073 {
3074 /* We use value_mark and value_free_to_mark because it could
3075 be a long time before we return to the command level and
3076 call free_all_values. We can't call free_all_values
3077 because we might be in the middle of evaluating a
3078 function call. */
3079 struct value *mark = value_mark ();
3080
3081 /* Need to select the frame, with all that implies so that
3082 the conditions will have the right context. Because we
3083 use the frame, we will not see an inlined function's
3084 variables when we arrive at a breakpoint at the start
3085 of the inlined function; the current frame will be the
3086 call site. */
3087 select_frame (get_current_frame ());
3088 value_is_zero
3089 = catch_errors (breakpoint_cond_eval, (bl->cond),
3090 "Error in testing breakpoint condition:\n",
3091 RETURN_MASK_ALL);
3092 /* FIXME-someday, should give breakpoint # */
3093 value_free_to_mark (mark);
3094 }
3095 if (bl->cond && value_is_zero)
3096 {
3097 bs->stop = 0;
3098 }
3099 else if (b->thread != -1 && b->thread != thread_id)
3100 {
3101 bs->stop = 0;
3102 }
3103 else if (b->ignore_count > 0)
3104 {
3105 b->ignore_count--;
3106 annotate_ignore_count_change ();
3107 bs->stop = 0;
3108 /* Increase the hit count even though we don't
3109 stop. */
3110 ++(b->hit_count);
3111 }
3112 }
3113 }
3114
3115
3116 /* Get a bpstat associated with having just stopped at address
3117 BP_ADDR in thread PTID.
3118
3119 Determine whether we stopped at a breakpoint, etc, or whether we
3120 don't understand this stop. Result is a chain of bpstat's such that:
3121
3122 if we don't understand the stop, the result is a null pointer.
3123
3124 if we understand why we stopped, the result is not null.
3125
3126 Each element of the chain refers to a particular breakpoint or
3127 watchpoint at which we have stopped. (We may have stopped for
3128 several reasons concurrently.)
3129
3130 Each element of the chain has valid next, breakpoint_at,
3131 commands, FIXME??? fields. */
3132
3133 bpstat
3134 bpstat_stop_status (CORE_ADDR bp_addr, ptid_t ptid)
3135 {
3136 struct breakpoint *b = NULL;
3137 const struct bp_location *bl;
3138 struct bp_location *loc;
3139 /* Root of the chain of bpstat's */
3140 struct bpstats root_bs[1];
3141 /* Pointer to the last thing in the chain currently. */
3142 bpstat bs = root_bs;
3143 int ix;
3144 int need_remove_insert;
3145
3146 ALL_BP_LOCATIONS (bl)
3147 {
3148 b = bl->owner;
3149 gdb_assert (b);
3150 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
3151 continue;
3152
3153 /* For hardware watchpoints, we look only at the first location.
3154 The watchpoint_check function will work on entire expression,
3155 not the individual locations. For read watchopints, the
3156 watchpoints_triggered function have checked all locations
3157 alrea
3158 */
3159 if (b->type == bp_hardware_watchpoint && bl != b->loc)
3160 continue;
3161
3162 if (!bpstat_check_location (bl, bp_addr))
3163 continue;
3164
3165 /* Come here if it's a watchpoint, or if the break address matches */
3166
3167 bs = bpstat_alloc (bl, bs); /* Alloc a bpstat to explain stop */
3168
3169 /* Assume we stop. Should we find watchpoint that is not actually
3170 triggered, or if condition of breakpoint is false, we'll reset
3171 'stop' to 0. */
3172 bs->stop = 1;
3173 bs->print = 1;
3174
3175 bpstat_check_watchpoint (bs);
3176 if (!bs->stop)
3177 continue;
3178
3179 if (b->type == bp_thread_event || b->type == bp_overlay_event
3180 || b->type == bp_longjmp_master)
3181 /* We do not stop for these. */
3182 bs->stop = 0;
3183 else
3184 bpstat_check_breakpoint_conditions (bs, ptid);
3185
3186 if (bs->stop)
3187 {
3188 ++(b->hit_count);
3189
3190 /* We will stop here */
3191 if (b->disposition == disp_disable)
3192 {
3193 if (b->enable_state != bp_permanent)
3194 b->enable_state = bp_disabled;
3195 update_global_location_list (0);
3196 }
3197 if (b->silent)
3198 bs->print = 0;
3199 bs->commands = b->commands;
3200 if (bs->commands
3201 && (strcmp ("silent", bs->commands->line) == 0
3202 || (xdb_commands && strcmp ("Q", bs->commands->line) == 0)))
3203 {
3204 bs->commands = bs->commands->next;
3205 bs->print = 0;
3206 }
3207 bs->commands = copy_command_lines (bs->commands);
3208 }
3209
3210 /* Print nothing for this entry if we dont stop or if we dont print. */
3211 if (bs->stop == 0 || bs->print == 0)
3212 bs->print_it = print_it_noop;
3213 }
3214
3215 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3216 {
3217 if (loc->address == bp_addr)
3218 {
3219 bs = bpstat_alloc (loc, bs);
3220 /* For hits of moribund locations, we should just proceed. */
3221 bs->stop = 0;
3222 bs->print = 0;
3223 bs->print_it = print_it_noop;
3224 }
3225 }
3226
3227 bs->next = NULL; /* Terminate the chain */
3228 bs = root_bs->next; /* Re-grab the head of the chain */
3229
3230 /* If we aren't stopping, the value of some hardware watchpoint may
3231 not have changed, but the intermediate memory locations we are
3232 watching may have. Don't bother if we're stopping; this will get
3233 done later. */
3234 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3235 if (bs->stop)
3236 break;
3237
3238 need_remove_insert = 0;
3239 if (bs == NULL)
3240 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3241 if (!bs->stop
3242 && bs->breakpoint_at->owner
3243 && (bs->breakpoint_at->owner->type == bp_hardware_watchpoint
3244 || bs->breakpoint_at->owner->type == bp_read_watchpoint
3245 || bs->breakpoint_at->owner->type == bp_access_watchpoint))
3246 {
3247 /* remove/insert can invalidate bs->breakpoint_at, if this
3248 location is no longer used by the watchpoint. Prevent
3249 further code from trying to use it. */
3250 bs->breakpoint_at = NULL;
3251 need_remove_insert = 1;
3252 }
3253
3254 if (need_remove_insert)
3255 {
3256 remove_breakpoints ();
3257 insert_breakpoints ();
3258 }
3259
3260 return root_bs->next;
3261 }
3262 \f
3263 /* Tell what to do about this bpstat. */
3264 struct bpstat_what
3265 bpstat_what (bpstat bs)
3266 {
3267 /* Classify each bpstat as one of the following. */
3268 enum class
3269 {
3270 /* This bpstat element has no effect on the main_action. */
3271 no_effect = 0,
3272
3273 /* There was a watchpoint, stop but don't print. */
3274 wp_silent,
3275
3276 /* There was a watchpoint, stop and print. */
3277 wp_noisy,
3278
3279 /* There was a breakpoint but we're not stopping. */
3280 bp_nostop,
3281
3282 /* There was a breakpoint, stop but don't print. */
3283 bp_silent,
3284
3285 /* There was a breakpoint, stop and print. */
3286 bp_noisy,
3287
3288 /* We hit the longjmp breakpoint. */
3289 long_jump,
3290
3291 /* We hit the longjmp_resume breakpoint. */
3292 long_resume,
3293
3294 /* We hit the step_resume breakpoint. */
3295 step_resume,
3296
3297 /* We hit the shared library event breakpoint. */
3298 shlib_event,
3299
3300 /* This is just used to count how many enums there are. */
3301 class_last
3302 };
3303
3304 /* Here is the table which drives this routine. So that we can
3305 format it pretty, we define some abbreviations for the
3306 enum bpstat_what codes. */
3307 #define kc BPSTAT_WHAT_KEEP_CHECKING
3308 #define ss BPSTAT_WHAT_STOP_SILENT
3309 #define sn BPSTAT_WHAT_STOP_NOISY
3310 #define sgl BPSTAT_WHAT_SINGLE
3311 #define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
3312 #define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
3313 #define sr BPSTAT_WHAT_STEP_RESUME
3314 #define shl BPSTAT_WHAT_CHECK_SHLIBS
3315
3316 /* "Can't happen." Might want to print an error message.
3317 abort() is not out of the question, but chances are GDB is just
3318 a bit confused, not unusable. */
3319 #define err BPSTAT_WHAT_STOP_NOISY
3320
3321 /* Given an old action and a class, come up with a new action. */
3322 /* One interesting property of this table is that wp_silent is the same
3323 as bp_silent and wp_noisy is the same as bp_noisy. That is because
3324 after stopping, the check for whether to step over a breakpoint
3325 (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
3326 reference to how we stopped. We retain separate wp_silent and
3327 bp_silent codes in case we want to change that someday.
3328
3329 Another possibly interesting property of this table is that
3330 there's a partial ordering, priority-like, of the actions. Once
3331 you've decided that some action is appropriate, you'll never go
3332 back and decide something of a lower priority is better. The
3333 ordering is:
3334
3335 kc < clr sgl shl slr sn sr ss
3336 sgl < shl slr sn sr ss
3337 slr < err shl sn sr ss
3338 clr < err shl sn sr ss
3339 ss < shl sn sr
3340 sn < shl sr
3341 shl < sr
3342 sr <
3343
3344 What I think this means is that we don't need a damned table
3345 here. If you just put the rows and columns in the right order,
3346 it'd look awfully regular. We could simply walk the bpstat list
3347 and choose the highest priority action we find, with a little
3348 logic to handle the 'err' cases. */
3349
3350 /* step_resume entries: a step resume breakpoint overrides another
3351 breakpoint of signal handling (see comment in wait_for_inferior
3352 at where we set the step_resume breakpoint). */
3353
3354 static const enum bpstat_what_main_action
3355 table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
3356 {
3357 /* old action */
3358 /* kc ss sn sgl slr clr sr shl
3359 */
3360 /*no_effect */
3361 {kc, ss, sn, sgl, slr, clr, sr, shl},
3362 /*wp_silent */
3363 {ss, ss, sn, ss, ss, ss, sr, shl},
3364 /*wp_noisy */
3365 {sn, sn, sn, sn, sn, sn, sr, shl},
3366 /*bp_nostop */
3367 {sgl, ss, sn, sgl, slr, slr, sr, shl},
3368 /*bp_silent */
3369 {ss, ss, sn, ss, ss, ss, sr, shl},
3370 /*bp_noisy */
3371 {sn, sn, sn, sn, sn, sn, sr, shl},
3372 /*long_jump */
3373 {slr, ss, sn, slr, slr, err, sr, shl},
3374 /*long_resume */
3375 {clr, ss, sn, err, err, err, sr, shl},
3376 /*step_resume */
3377 {sr, sr, sr, sr, sr, sr, sr, sr},
3378 /*shlib */
3379 {shl, shl, shl, shl, shl, shl, sr, shl}
3380 };
3381
3382 #undef kc
3383 #undef ss
3384 #undef sn
3385 #undef sgl
3386 #undef slr
3387 #undef clr
3388 #undef err
3389 #undef sr
3390 #undef ts
3391 #undef shl
3392 enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
3393 struct bpstat_what retval;
3394
3395 retval.call_dummy = 0;
3396 for (; bs != NULL; bs = bs->next)
3397 {
3398 enum class bs_class = no_effect;
3399 if (bs->breakpoint_at == NULL)
3400 /* I suspect this can happen if it was a momentary breakpoint
3401 which has since been deleted. */
3402 continue;
3403 if (bs->breakpoint_at->owner == NULL)
3404 bs_class = bp_nostop;
3405 else
3406 switch (bs->breakpoint_at->owner->type)
3407 {
3408 case bp_none:
3409 continue;
3410
3411 case bp_breakpoint:
3412 case bp_hardware_breakpoint:
3413 case bp_until:
3414 case bp_finish:
3415 if (bs->stop)
3416 {
3417 if (bs->print)
3418 bs_class = bp_noisy;
3419 else
3420 bs_class = bp_silent;
3421 }
3422 else
3423 bs_class = bp_nostop;
3424 break;
3425 case bp_watchpoint:
3426 case bp_hardware_watchpoint:
3427 case bp_read_watchpoint:
3428 case bp_access_watchpoint:
3429 if (bs->stop)
3430 {
3431 if (bs->print)
3432 bs_class = wp_noisy;
3433 else
3434 bs_class = wp_silent;
3435 }
3436 else
3437 /* There was a watchpoint, but we're not stopping.
3438 This requires no further action. */
3439 bs_class = no_effect;
3440 break;
3441 case bp_longjmp:
3442 bs_class = long_jump;
3443 break;
3444 case bp_longjmp_resume:
3445 bs_class = long_resume;
3446 break;
3447 case bp_step_resume:
3448 if (bs->stop)
3449 {
3450 bs_class = step_resume;
3451 }
3452 else
3453 /* It is for the wrong frame. */
3454 bs_class = bp_nostop;
3455 break;
3456 case bp_watchpoint_scope:
3457 bs_class = bp_nostop;
3458 break;
3459 case bp_shlib_event:
3460 bs_class = shlib_event;
3461 break;
3462 case bp_thread_event:
3463 case bp_overlay_event:
3464 case bp_longjmp_master:
3465 bs_class = bp_nostop;
3466 break;
3467 case bp_catchpoint:
3468 if (bs->stop)
3469 {
3470 if (bs->print)
3471 bs_class = bp_noisy;
3472 else
3473 bs_class = bp_silent;
3474 }
3475 else
3476 /* There was a catchpoint, but we're not stopping.
3477 This requires no further action. */
3478 bs_class = no_effect;
3479 break;
3480 case bp_call_dummy:
3481 /* Make sure the action is stop (silent or noisy),
3482 so infrun.c pops the dummy frame. */
3483 bs_class = bp_silent;
3484 retval.call_dummy = 1;
3485 break;
3486 case bp_tracepoint:
3487 /* Tracepoint hits should not be reported back to GDB, and
3488 if one got through somehow, it should have been filtered
3489 out already. */
3490 internal_error (__FILE__, __LINE__,
3491 _("bpstat_what: bp_tracepoint encountered"));
3492 break;
3493 }
3494 current_action = table[(int) bs_class][(int) current_action];
3495 }
3496 retval.main_action = current_action;
3497 return retval;
3498 }
3499
3500 /* Nonzero if we should step constantly (e.g. watchpoints on machines
3501 without hardware support). This isn't related to a specific bpstat,
3502 just to things like whether watchpoints are set. */
3503
3504 int
3505 bpstat_should_step (void)
3506 {
3507 struct breakpoint *b;
3508 ALL_BREAKPOINTS (b)
3509 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3510 return 1;
3511 return 0;
3512 }
3513
3514 \f
3515
3516 static void print_breakpoint_location (struct breakpoint *b,
3517 struct bp_location *loc,
3518 char *wrap_indent,
3519 struct ui_stream *stb)
3520 {
3521 if (b->source_file)
3522 {
3523 struct symbol *sym
3524 = find_pc_sect_function (loc->address, loc->section);
3525 if (sym)
3526 {
3527 ui_out_text (uiout, "in ");
3528 ui_out_field_string (uiout, "func",
3529 SYMBOL_PRINT_NAME (sym));
3530 ui_out_wrap_hint (uiout, wrap_indent);
3531 ui_out_text (uiout, " at ");
3532 }
3533 ui_out_field_string (uiout, "file", b->source_file);
3534 ui_out_text (uiout, ":");
3535
3536 if (ui_out_is_mi_like_p (uiout))
3537 {
3538 struct symtab_and_line sal = find_pc_line (loc->address, 0);
3539 char *fullname = symtab_to_fullname (sal.symtab);
3540
3541 if (fullname)
3542 ui_out_field_string (uiout, "fullname", fullname);
3543 }
3544
3545 ui_out_field_int (uiout, "line", b->line_number);
3546 }
3547 else if (!b->loc)
3548 {
3549 ui_out_field_string (uiout, "pending", b->addr_string);
3550 }
3551 else
3552 {
3553 print_address_symbolic (loc->address, stb->stream, demangle, "");
3554 ui_out_field_stream (uiout, "at", stb);
3555 }
3556 }
3557
3558 /* Print B to gdb_stdout. */
3559 static void
3560 print_one_breakpoint_location (struct breakpoint *b,
3561 struct bp_location *loc,
3562 int loc_number,
3563 struct bp_location **last_loc,
3564 int print_address_bits)
3565 {
3566 struct command_line *l;
3567 struct symbol *sym;
3568 struct ep_type_description
3569 {
3570 enum bptype type;
3571 char *description;
3572 };
3573 static struct ep_type_description bptypes[] =
3574 {
3575 {bp_none, "?deleted?"},
3576 {bp_breakpoint, "breakpoint"},
3577 {bp_hardware_breakpoint, "hw breakpoint"},
3578 {bp_until, "until"},
3579 {bp_finish, "finish"},
3580 {bp_watchpoint, "watchpoint"},
3581 {bp_hardware_watchpoint, "hw watchpoint"},
3582 {bp_read_watchpoint, "read watchpoint"},
3583 {bp_access_watchpoint, "acc watchpoint"},
3584 {bp_longjmp, "longjmp"},
3585 {bp_longjmp_resume, "longjmp resume"},
3586 {bp_step_resume, "step resume"},
3587 {bp_watchpoint_scope, "watchpoint scope"},
3588 {bp_call_dummy, "call dummy"},
3589 {bp_shlib_event, "shlib events"},
3590 {bp_thread_event, "thread events"},
3591 {bp_overlay_event, "overlay events"},
3592 {bp_longjmp_master, "longjmp master"},
3593 {bp_catchpoint, "catchpoint"},
3594 {bp_tracepoint, "tracepoint"},
3595 };
3596
3597 static char bpenables[] = "nynny";
3598 char wrap_indent[80];
3599 struct ui_stream *stb = ui_out_stream_new (uiout);
3600 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3601 struct cleanup *bkpt_chain;
3602
3603 int header_of_multiple = 0;
3604 int part_of_multiple = (loc != NULL);
3605 struct value_print_options opts;
3606
3607 get_user_print_options (&opts);
3608
3609 gdb_assert (!loc || loc_number != 0);
3610 /* See comment in print_one_breakpoint concerning
3611 treatment of breakpoints with single disabled
3612 location. */
3613 if (loc == NULL
3614 && (b->loc != NULL
3615 && (b->loc->next != NULL || !b->loc->enabled)))
3616 header_of_multiple = 1;
3617 if (loc == NULL)
3618 loc = b->loc;
3619
3620 annotate_record ();
3621 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
3622
3623 /* 1 */
3624 annotate_field (0);
3625 if (part_of_multiple)
3626 {
3627 char *formatted;
3628 formatted = xstrprintf ("%d.%d", b->number, loc_number);
3629 ui_out_field_string (uiout, "number", formatted);
3630 xfree (formatted);
3631 }
3632 else
3633 {
3634 ui_out_field_int (uiout, "number", b->number);
3635 }
3636
3637 /* 2 */
3638 annotate_field (1);
3639 if (part_of_multiple)
3640 ui_out_field_skip (uiout, "type");
3641 else
3642 {
3643 if (((int) b->type >= (sizeof (bptypes) / sizeof (bptypes[0])))
3644 || ((int) b->type != bptypes[(int) b->type].type))
3645 internal_error (__FILE__, __LINE__,
3646 _("bptypes table does not describe type #%d."),
3647 (int) b->type);
3648 ui_out_field_string (uiout, "type", bptypes[(int) b->type].description);
3649 }
3650
3651 /* 3 */
3652 annotate_field (2);
3653 if (part_of_multiple)
3654 ui_out_field_skip (uiout, "disp");
3655 else
3656 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3657
3658
3659 /* 4 */
3660 annotate_field (3);
3661 if (part_of_multiple)
3662 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
3663 else
3664 ui_out_field_fmt (uiout, "enabled", "%c",
3665 bpenables[(int) b->enable_state]);
3666 ui_out_spaces (uiout, 2);
3667
3668
3669 /* 5 and 6 */
3670 strcpy (wrap_indent, " ");
3671 if (opts.addressprint)
3672 {
3673 if (print_address_bits <= 32)
3674 strcat (wrap_indent, " ");
3675 else
3676 strcat (wrap_indent, " ");
3677 }
3678
3679 if (b->ops != NULL && b->ops->print_one != NULL)
3680 {
3681 /* Although the print_one can possibly print
3682 all locations, calling it here is not likely
3683 to get any nice result. So, make sure there's
3684 just one location. */
3685 gdb_assert (b->loc == NULL || b->loc->next == NULL);
3686 b->ops->print_one (b, last_loc);
3687 }
3688 else
3689 switch (b->type)
3690 {
3691 case bp_none:
3692 internal_error (__FILE__, __LINE__,
3693 _("print_one_breakpoint: bp_none encountered\n"));
3694 break;
3695
3696 case bp_watchpoint:
3697 case bp_hardware_watchpoint:
3698 case bp_read_watchpoint:
3699 case bp_access_watchpoint:
3700 /* Field 4, the address, is omitted (which makes the columns
3701 not line up too nicely with the headers, but the effect
3702 is relatively readable). */
3703 if (opts.addressprint)
3704 ui_out_field_skip (uiout, "addr");
3705 annotate_field (5);
3706 ui_out_field_string (uiout, "what", b->exp_string);
3707 break;
3708
3709 case bp_breakpoint:
3710 case bp_hardware_breakpoint:
3711 case bp_until:
3712 case bp_finish:
3713 case bp_longjmp:
3714 case bp_longjmp_resume:
3715 case bp_step_resume:
3716 case bp_watchpoint_scope:
3717 case bp_call_dummy:
3718 case bp_shlib_event:
3719 case bp_thread_event:
3720 case bp_overlay_event:
3721 case bp_longjmp_master:
3722 case bp_tracepoint:
3723 if (opts.addressprint)
3724 {
3725 annotate_field (4);
3726 if (header_of_multiple)
3727 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
3728 else if (b->loc == NULL || loc->shlib_disabled)
3729 ui_out_field_string (uiout, "addr", "<PENDING>");
3730 else
3731 ui_out_field_core_addr (uiout, "addr",
3732 loc->gdbarch, loc->address);
3733 }
3734 annotate_field (5);
3735 if (!header_of_multiple)
3736 print_breakpoint_location (b, loc, wrap_indent, stb);
3737 if (b->loc)
3738 *last_loc = b->loc;
3739 break;
3740 }
3741
3742 if (!part_of_multiple)
3743 {
3744 if (b->thread != -1)
3745 {
3746 /* FIXME: This seems to be redundant and lost here; see the
3747 "stop only in" line a little further down. */
3748 ui_out_text (uiout, " thread ");
3749 ui_out_field_int (uiout, "thread", b->thread);
3750 }
3751 else if (b->task != 0)
3752 {
3753 ui_out_text (uiout, " task ");
3754 ui_out_field_int (uiout, "task", b->task);
3755 }
3756 }
3757
3758 ui_out_text (uiout, "\n");
3759
3760 if (part_of_multiple && frame_id_p (b->frame_id))
3761 {
3762 annotate_field (6);
3763 ui_out_text (uiout, "\tstop only in stack frame at ");
3764 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
3765 the frame ID. */
3766 ui_out_field_core_addr (uiout, "frame",
3767 b->gdbarch, b->frame_id.stack_addr);
3768 ui_out_text (uiout, "\n");
3769 }
3770
3771 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
3772 {
3773 /* We do not print the condition for Ada exception catchpoints
3774 because the condition is an internal implementation detail
3775 that we do not want to expose to the user. */
3776 annotate_field (7);
3777 if (b->type == bp_tracepoint)
3778 ui_out_text (uiout, "\ttrace only if ");
3779 else
3780 ui_out_text (uiout, "\tstop only if ");
3781 ui_out_field_string (uiout, "cond", b->cond_string);
3782 ui_out_text (uiout, "\n");
3783 }
3784
3785 if (!part_of_multiple && b->thread != -1)
3786 {
3787 /* FIXME should make an annotation for this */
3788 ui_out_text (uiout, "\tstop only in thread ");
3789 ui_out_field_int (uiout, "thread", b->thread);
3790 ui_out_text (uiout, "\n");
3791 }
3792
3793 if (!part_of_multiple && b->hit_count)
3794 {
3795 /* FIXME should make an annotation for this */
3796 if (ep_is_catchpoint (b))
3797 ui_out_text (uiout, "\tcatchpoint");
3798 else
3799 ui_out_text (uiout, "\tbreakpoint");
3800 ui_out_text (uiout, " already hit ");
3801 ui_out_field_int (uiout, "times", b->hit_count);
3802 if (b->hit_count == 1)
3803 ui_out_text (uiout, " time\n");
3804 else
3805 ui_out_text (uiout, " times\n");
3806 }
3807
3808 /* Output the count also if it is zero, but only if this is
3809 mi. FIXME: Should have a better test for this. */
3810 if (ui_out_is_mi_like_p (uiout))
3811 if (!part_of_multiple && b->hit_count == 0)
3812 ui_out_field_int (uiout, "times", b->hit_count);
3813
3814 if (!part_of_multiple && b->ignore_count)
3815 {
3816 annotate_field (8);
3817 ui_out_text (uiout, "\tignore next ");
3818 ui_out_field_int (uiout, "ignore", b->ignore_count);
3819 ui_out_text (uiout, " hits\n");
3820 }
3821
3822 l = b->commands;
3823 if (!part_of_multiple && l)
3824 {
3825 struct cleanup *script_chain;
3826
3827 annotate_field (9);
3828 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
3829 print_command_lines (uiout, l, 4);
3830 do_cleanups (script_chain);
3831 }
3832
3833 if (!part_of_multiple && b->pass_count)
3834 {
3835 annotate_field (10);
3836 ui_out_text (uiout, "\tpass count ");
3837 ui_out_field_int (uiout, "pass", b->pass_count);
3838 ui_out_text (uiout, " \n");
3839 }
3840
3841 if (!part_of_multiple && b->step_count)
3842 {
3843 annotate_field (11);
3844 ui_out_text (uiout, "\tstep count ");
3845 ui_out_field_int (uiout, "step", b->step_count);
3846 ui_out_text (uiout, " \n");
3847 }
3848
3849 if (!part_of_multiple && b->actions)
3850 {
3851 struct action_line *action;
3852 annotate_field (12);
3853 for (action = b->actions; action; action = action->next)
3854 {
3855 ui_out_text (uiout, " A\t");
3856 ui_out_text (uiout, action->action);
3857 ui_out_text (uiout, "\n");
3858 }
3859 }
3860
3861 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
3862 {
3863 if (b->addr_string)
3864 ui_out_field_string (uiout, "original-location", b->addr_string);
3865 else if (b->exp_string)
3866 ui_out_field_string (uiout, "original-location", b->exp_string);
3867 }
3868
3869 do_cleanups (bkpt_chain);
3870 do_cleanups (old_chain);
3871 }
3872
3873 static void
3874 print_one_breakpoint (struct breakpoint *b,
3875 struct bp_location **last_loc, int print_address_bits)
3876 {
3877 print_one_breakpoint_location (b, NULL, 0, last_loc, print_address_bits);
3878
3879 /* If this breakpoint has custom print function,
3880 it's already printed. Otherwise, print individual
3881 locations, if any. */
3882 if (b->ops == NULL || b->ops->print_one == NULL)
3883 {
3884 /* If breakpoint has a single location that is
3885 disabled, we print it as if it had
3886 several locations, since otherwise it's hard to
3887 represent "breakpoint enabled, location disabled"
3888 situation.
3889 Note that while hardware watchpoints have
3890 several locations internally, that's no a property
3891 exposed to user. */
3892 if (b->loc
3893 && !is_hardware_watchpoint (b)
3894 && (b->loc->next || !b->loc->enabled)
3895 && !ui_out_is_mi_like_p (uiout))
3896 {
3897 struct bp_location *loc;
3898 int n = 1;
3899 for (loc = b->loc; loc; loc = loc->next, ++n)
3900 print_one_breakpoint_location (b, loc, n, last_loc,
3901 print_address_bits);
3902 }
3903 }
3904 }
3905
3906 static int
3907 breakpoint_address_bits (struct breakpoint *b)
3908 {
3909 int print_address_bits = 0;
3910 struct bp_location *loc;
3911
3912 for (loc = b->loc; loc; loc = loc->next)
3913 {
3914 int addr_bit = gdbarch_addr_bit (b->gdbarch);
3915 if (addr_bit > print_address_bits)
3916 print_address_bits = addr_bit;
3917 }
3918
3919 return print_address_bits;
3920 }
3921
3922 struct captured_breakpoint_query_args
3923 {
3924 int bnum;
3925 };
3926
3927 static int
3928 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
3929 {
3930 struct captured_breakpoint_query_args *args = data;
3931 struct breakpoint *b;
3932 struct bp_location *dummy_loc = NULL;
3933 ALL_BREAKPOINTS (b)
3934 {
3935 if (args->bnum == b->number)
3936 {
3937 int print_address_bits = breakpoint_address_bits (b);
3938 print_one_breakpoint (b, &dummy_loc, print_address_bits);
3939 return GDB_RC_OK;
3940 }
3941 }
3942 return GDB_RC_NONE;
3943 }
3944
3945 enum gdb_rc
3946 gdb_breakpoint_query (struct ui_out *uiout, int bnum, char **error_message)
3947 {
3948 struct captured_breakpoint_query_args args;
3949 args.bnum = bnum;
3950 /* For the moment we don't trust print_one_breakpoint() to not throw
3951 an error. */
3952 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
3953 error_message, RETURN_MASK_ALL) < 0)
3954 return GDB_RC_FAIL;
3955 else
3956 return GDB_RC_OK;
3957 }
3958
3959 /* Return non-zero if B is user settable (breakpoints, watchpoints,
3960 catchpoints, et.al.). */
3961
3962 static int
3963 user_settable_breakpoint (const struct breakpoint *b)
3964 {
3965 return (b->type == bp_breakpoint
3966 || b->type == bp_catchpoint
3967 || b->type == bp_hardware_breakpoint
3968 || b->type == bp_tracepoint
3969 || b->type == bp_watchpoint
3970 || b->type == bp_read_watchpoint
3971 || b->type == bp_access_watchpoint
3972 || b->type == bp_hardware_watchpoint);
3973 }
3974
3975 /* Print information on user settable breakpoint (watchpoint, etc)
3976 number BNUM. If BNUM is -1 print all user settable breakpoints.
3977 If ALLFLAG is non-zero, include non- user settable breakpoints. */
3978
3979 static void
3980 breakpoint_1 (int bnum, int allflag)
3981 {
3982 struct breakpoint *b;
3983 struct bp_location *last_loc = NULL;
3984 int nr_printable_breakpoints;
3985 struct cleanup *bkpttbl_chain;
3986 struct value_print_options opts;
3987 int print_address_bits = 0;
3988
3989 get_user_print_options (&opts);
3990
3991 /* Compute the number of rows in the table, as well as the
3992 size required for address fields. */
3993 nr_printable_breakpoints = 0;
3994 ALL_BREAKPOINTS (b)
3995 if (bnum == -1
3996 || bnum == b->number)
3997 {
3998 if (allflag || user_settable_breakpoint (b))
3999 {
4000 int addr_bit = breakpoint_address_bits (b);
4001 if (addr_bit > print_address_bits)
4002 print_address_bits = addr_bit;
4003
4004 nr_printable_breakpoints++;
4005 }
4006 }
4007
4008 if (opts.addressprint)
4009 bkpttbl_chain
4010 = make_cleanup_ui_out_table_begin_end (uiout, 6, nr_printable_breakpoints,
4011 "BreakpointTable");
4012 else
4013 bkpttbl_chain
4014 = make_cleanup_ui_out_table_begin_end (uiout, 5, nr_printable_breakpoints,
4015 "BreakpointTable");
4016
4017 if (nr_printable_breakpoints > 0)
4018 annotate_breakpoints_headers ();
4019 if (nr_printable_breakpoints > 0)
4020 annotate_field (0);
4021 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
4022 if (nr_printable_breakpoints > 0)
4023 annotate_field (1);
4024 ui_out_table_header (uiout, 14, ui_left, "type", "Type"); /* 2 */
4025 if (nr_printable_breakpoints > 0)
4026 annotate_field (2);
4027 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
4028 if (nr_printable_breakpoints > 0)
4029 annotate_field (3);
4030 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
4031 if (opts.addressprint)
4032 {
4033 if (nr_printable_breakpoints > 0)
4034 annotate_field (4);
4035 if (print_address_bits <= 32)
4036 ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
4037 else
4038 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
4039 }
4040 if (nr_printable_breakpoints > 0)
4041 annotate_field (5);
4042 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
4043 ui_out_table_body (uiout);
4044 if (nr_printable_breakpoints > 0)
4045 annotate_breakpoints_table ();
4046
4047 ALL_BREAKPOINTS (b)
4048 if (bnum == -1
4049 || bnum == b->number)
4050 {
4051 /* We only print out user settable breakpoints unless the
4052 allflag is set. */
4053 if (allflag || user_settable_breakpoint (b))
4054 print_one_breakpoint (b, &last_loc, print_address_bits);
4055 }
4056
4057 do_cleanups (bkpttbl_chain);
4058
4059 if (nr_printable_breakpoints == 0)
4060 {
4061 if (bnum == -1)
4062 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
4063 else
4064 ui_out_message (uiout, 0, "No breakpoint or watchpoint number %d.\n",
4065 bnum);
4066 }
4067 else
4068 {
4069 if (last_loc && !server_command)
4070 set_next_address (last_loc->gdbarch, last_loc->address);
4071 }
4072
4073 /* FIXME? Should this be moved up so that it is only called when
4074 there have been breakpoints? */
4075 annotate_breakpoints_table_end ();
4076 }
4077
4078 static void
4079 breakpoints_info (char *bnum_exp, int from_tty)
4080 {
4081 int bnum = -1;
4082
4083 if (bnum_exp)
4084 bnum = parse_and_eval_long (bnum_exp);
4085
4086 breakpoint_1 (bnum, 0);
4087 }
4088
4089 static void
4090 maintenance_info_breakpoints (char *bnum_exp, int from_tty)
4091 {
4092 int bnum = -1;
4093
4094 if (bnum_exp)
4095 bnum = parse_and_eval_long (bnum_exp);
4096
4097 breakpoint_1 (bnum, 1);
4098 }
4099
4100 static int
4101 breakpoint_has_pc (struct breakpoint *b,
4102 CORE_ADDR pc, struct obj_section *section)
4103 {
4104 struct bp_location *bl = b->loc;
4105 for (; bl; bl = bl->next)
4106 {
4107 if (bl->address == pc
4108 && (!overlay_debugging || bl->section == section))
4109 return 1;
4110 }
4111 return 0;
4112 }
4113
4114 /* Print a message describing any breakpoints set at PC. */
4115
4116 static void
4117 describe_other_breakpoints (struct gdbarch *gdbarch, CORE_ADDR pc,
4118 struct obj_section *section, int thread)
4119 {
4120 int others = 0;
4121 struct breakpoint *b;
4122
4123 ALL_BREAKPOINTS (b)
4124 others += breakpoint_has_pc (b, pc, section);
4125 if (others > 0)
4126 {
4127 if (others == 1)
4128 printf_filtered (_("Note: breakpoint "));
4129 else /* if (others == ???) */
4130 printf_filtered (_("Note: breakpoints "));
4131 ALL_BREAKPOINTS (b)
4132 if (breakpoint_has_pc (b, pc, section))
4133 {
4134 others--;
4135 printf_filtered ("%d", b->number);
4136 if (b->thread == -1 && thread != -1)
4137 printf_filtered (" (all threads)");
4138 else if (b->thread != -1)
4139 printf_filtered (" (thread %d)", b->thread);
4140 printf_filtered ("%s%s ",
4141 ((b->enable_state == bp_disabled
4142 || b->enable_state == bp_call_disabled)
4143 ? " (disabled)"
4144 : b->enable_state == bp_permanent
4145 ? " (permanent)"
4146 : ""),
4147 (others > 1) ? ","
4148 : ((others == 1) ? " and" : ""));
4149 }
4150 printf_filtered (_("also set at pc "));
4151 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
4152 printf_filtered (".\n");
4153 }
4154 }
4155 \f
4156 /* Set the default place to put a breakpoint
4157 for the `break' command with no arguments. */
4158
4159 void
4160 set_default_breakpoint (int valid, CORE_ADDR addr, struct symtab *symtab,
4161 int line)
4162 {
4163 default_breakpoint_valid = valid;
4164 default_breakpoint_address = addr;
4165 default_breakpoint_symtab = symtab;
4166 default_breakpoint_line = line;
4167 }
4168
4169 /* Return true iff it is meaningful to use the address member of
4170 BPT. For some breakpoint types, the address member is irrelevant
4171 and it makes no sense to attempt to compare it to other addresses
4172 (or use it for any other purpose either).
4173
4174 More specifically, each of the following breakpoint types will always
4175 have a zero valued address and we don't want check_duplicates() to mark
4176 breakpoints of any of these types to be a duplicate of an actual
4177 breakpoint at address zero:
4178
4179 bp_watchpoint
4180 bp_hardware_watchpoint
4181 bp_read_watchpoint
4182 bp_access_watchpoint
4183 bp_catchpoint */
4184
4185 static int
4186 breakpoint_address_is_meaningful (struct breakpoint *bpt)
4187 {
4188 enum bptype type = bpt->type;
4189
4190 return (type != bp_watchpoint
4191 && type != bp_hardware_watchpoint
4192 && type != bp_read_watchpoint
4193 && type != bp_access_watchpoint
4194 && type != bp_catchpoint);
4195 }
4196
4197 /* Rescan breakpoints at the same address and section as BPT,
4198 marking the first one as "first" and any others as "duplicates".
4199 This is so that the bpt instruction is only inserted once.
4200 If we have a permanent breakpoint at the same place as BPT, make
4201 that one the official one, and the rest as duplicates. */
4202
4203 static void
4204 check_duplicates_for (CORE_ADDR address, struct obj_section *section)
4205 {
4206 struct bp_location *b;
4207 int count = 0;
4208 struct bp_location *perm_bp = 0;
4209
4210 ALL_BP_LOCATIONS (b)
4211 if (b->owner->enable_state != bp_disabled
4212 && b->owner->enable_state != bp_call_disabled
4213 && b->enabled
4214 && !b->shlib_disabled
4215 && b->address == address /* address / overlay match */
4216 && (!overlay_debugging || b->section == section)
4217 && breakpoint_address_is_meaningful (b->owner))
4218 {
4219 /* Have we found a permanent breakpoint? */
4220 if (b->owner->enable_state == bp_permanent)
4221 {
4222 perm_bp = b;
4223 break;
4224 }
4225
4226 count++;
4227 b->duplicate = count > 1;
4228 }
4229
4230 /* If we found a permanent breakpoint at this address, go over the
4231 list again and declare all the other breakpoints there (except
4232 other permanent breakpoints) to be the duplicates. */
4233 if (perm_bp)
4234 {
4235 perm_bp->duplicate = 0;
4236
4237 /* Permanent breakpoint should always be inserted. */
4238 if (! perm_bp->inserted)
4239 internal_error (__FILE__, __LINE__,
4240 _("allegedly permanent breakpoint is not "
4241 "actually inserted"));
4242
4243 ALL_BP_LOCATIONS (b)
4244 if (b != perm_bp)
4245 {
4246 if (b->owner->enable_state != bp_permanent
4247 && b->owner->enable_state != bp_disabled
4248 && b->owner->enable_state != bp_call_disabled
4249 && b->enabled && !b->shlib_disabled
4250 && b->address == address /* address / overlay match */
4251 && (!overlay_debugging || b->section == section)
4252 && breakpoint_address_is_meaningful (b->owner))
4253 {
4254 if (b->inserted)
4255 internal_error (__FILE__, __LINE__,
4256 _("another breakpoint was inserted on top of "
4257 "a permanent breakpoint"));
4258
4259 b->duplicate = 1;
4260 }
4261 }
4262 }
4263 }
4264
4265 static void
4266 check_duplicates (struct breakpoint *bpt)
4267 {
4268 struct bp_location *bl = bpt->loc;
4269
4270 if (! breakpoint_address_is_meaningful (bpt))
4271 return;
4272
4273 for (; bl; bl = bl->next)
4274 check_duplicates_for (bl->address, bl->section);
4275 }
4276
4277 static void
4278 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
4279 int bnum, int have_bnum)
4280 {
4281 char astr1[40];
4282 char astr2[40];
4283
4284 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
4285 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
4286 if (have_bnum)
4287 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
4288 bnum, astr1, astr2);
4289 else
4290 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
4291 }
4292
4293 /* Adjust a breakpoint's address to account for architectural constraints
4294 on breakpoint placement. Return the adjusted address. Note: Very
4295 few targets require this kind of adjustment. For most targets,
4296 this function is simply the identity function. */
4297
4298 static CORE_ADDR
4299 adjust_breakpoint_address (struct gdbarch *gdbarch,
4300 CORE_ADDR bpaddr, enum bptype bptype)
4301 {
4302 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
4303 {
4304 /* Very few targets need any kind of breakpoint adjustment. */
4305 return bpaddr;
4306 }
4307 else if (bptype == bp_watchpoint
4308 || bptype == bp_hardware_watchpoint
4309 || bptype == bp_read_watchpoint
4310 || bptype == bp_access_watchpoint
4311 || bptype == bp_catchpoint)
4312 {
4313 /* Watchpoints and the various bp_catch_* eventpoints should not
4314 have their addresses modified. */
4315 return bpaddr;
4316 }
4317 else
4318 {
4319 CORE_ADDR adjusted_bpaddr;
4320
4321 /* Some targets have architectural constraints on the placement
4322 of breakpoint instructions. Obtain the adjusted address. */
4323 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
4324
4325 /* An adjusted breakpoint address can significantly alter
4326 a user's expectations. Print a warning if an adjustment
4327 is required. */
4328 if (adjusted_bpaddr != bpaddr)
4329 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
4330
4331 return adjusted_bpaddr;
4332 }
4333 }
4334
4335 /* Allocate a struct bp_location. */
4336
4337 static struct bp_location *
4338 allocate_bp_location (struct breakpoint *bpt)
4339 {
4340 struct bp_location *loc, *loc_p;
4341
4342 loc = xmalloc (sizeof (struct bp_location));
4343 memset (loc, 0, sizeof (*loc));
4344
4345 loc->owner = bpt;
4346 loc->cond = NULL;
4347 loc->shlib_disabled = 0;
4348 loc->enabled = 1;
4349
4350 switch (bpt->type)
4351 {
4352 case bp_breakpoint:
4353 case bp_tracepoint:
4354 case bp_until:
4355 case bp_finish:
4356 case bp_longjmp:
4357 case bp_longjmp_resume:
4358 case bp_step_resume:
4359 case bp_watchpoint_scope:
4360 case bp_call_dummy:
4361 case bp_shlib_event:
4362 case bp_thread_event:
4363 case bp_overlay_event:
4364 case bp_longjmp_master:
4365 loc->loc_type = bp_loc_software_breakpoint;
4366 break;
4367 case bp_hardware_breakpoint:
4368 loc->loc_type = bp_loc_hardware_breakpoint;
4369 break;
4370 case bp_hardware_watchpoint:
4371 case bp_read_watchpoint:
4372 case bp_access_watchpoint:
4373 loc->loc_type = bp_loc_hardware_watchpoint;
4374 break;
4375 case bp_watchpoint:
4376 case bp_catchpoint:
4377 loc->loc_type = bp_loc_other;
4378 break;
4379 default:
4380 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
4381 }
4382
4383 return loc;
4384 }
4385
4386 static void free_bp_location (struct bp_location *loc)
4387 {
4388 if (loc->cond)
4389 xfree (loc->cond);
4390
4391 if (loc->function_name)
4392 xfree (loc->function_name);
4393
4394 xfree (loc);
4395 }
4396
4397 /* Helper to set_raw_breakpoint below. Creates a breakpoint
4398 that has type BPTYPE and has no locations as yet. */
4399
4400 static struct breakpoint *
4401 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
4402 enum bptype bptype)
4403 {
4404 struct breakpoint *b, *b1;
4405
4406 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
4407 memset (b, 0, sizeof (*b));
4408
4409 b->type = bptype;
4410 b->gdbarch = gdbarch;
4411 b->language = current_language->la_language;
4412 b->input_radix = input_radix;
4413 b->thread = -1;
4414 b->enable_state = bp_enabled;
4415 b->next = 0;
4416 b->silent = 0;
4417 b->ignore_count = 0;
4418 b->commands = NULL;
4419 b->frame_id = null_frame_id;
4420 b->forked_inferior_pid = null_ptid;
4421 b->exec_pathname = NULL;
4422 b->ops = NULL;
4423 b->condition_not_parsed = 0;
4424
4425 /* Add this breakpoint to the end of the chain
4426 so that a list of breakpoints will come out in order
4427 of increasing numbers. */
4428
4429 b1 = breakpoint_chain;
4430 if (b1 == 0)
4431 breakpoint_chain = b;
4432 else
4433 {
4434 while (b1->next)
4435 b1 = b1->next;
4436 b1->next = b;
4437 }
4438 return b;
4439 }
4440
4441 /* Initialize loc->function_name. */
4442 static void
4443 set_breakpoint_location_function (struct bp_location *loc)
4444 {
4445 if (loc->owner->type == bp_breakpoint
4446 || loc->owner->type == bp_hardware_breakpoint
4447 || loc->owner->type == bp_tracepoint)
4448 {
4449 find_pc_partial_function (loc->address, &(loc->function_name),
4450 NULL, NULL);
4451 if (loc->function_name)
4452 loc->function_name = xstrdup (loc->function_name);
4453 }
4454 }
4455
4456 /* Attempt to determine architecture of location identified by SAL. */
4457 static struct gdbarch *
4458 get_sal_arch (struct symtab_and_line sal)
4459 {
4460 if (sal.section)
4461 return get_objfile_arch (sal.section->objfile);
4462 if (sal.symtab)
4463 return get_objfile_arch (sal.symtab->objfile);
4464
4465 return NULL;
4466 }
4467
4468 /* set_raw_breakpoint is a low level routine for allocating and
4469 partially initializing a breakpoint of type BPTYPE. The newly
4470 created breakpoint's address, section, source file name, and line
4471 number are provided by SAL. The newly created and partially
4472 initialized breakpoint is added to the breakpoint chain and
4473 is also returned as the value of this function.
4474
4475 It is expected that the caller will complete the initialization of
4476 the newly created breakpoint struct as well as output any status
4477 information regarding the creation of a new breakpoint. In
4478 particular, set_raw_breakpoint does NOT set the breakpoint
4479 number! Care should be taken to not allow an error to occur
4480 prior to completing the initialization of the breakpoint. If this
4481 should happen, a bogus breakpoint will be left on the chain. */
4482
4483 static struct breakpoint *
4484 set_raw_breakpoint (struct gdbarch *gdbarch,
4485 struct symtab_and_line sal, enum bptype bptype)
4486 {
4487 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch, bptype);
4488 CORE_ADDR adjusted_address;
4489 struct gdbarch *loc_gdbarch;
4490
4491 loc_gdbarch = get_sal_arch (sal);
4492 if (!loc_gdbarch)
4493 loc_gdbarch = b->gdbarch;
4494
4495 /* Adjust the breakpoint's address prior to allocating a location.
4496 Once we call allocate_bp_location(), that mostly uninitialized
4497 location will be placed on the location chain. Adjustment of the
4498 breakpoint may cause target_read_memory() to be called and we do
4499 not want its scan of the location chain to find a breakpoint and
4500 location that's only been partially initialized. */
4501 adjusted_address = adjust_breakpoint_address (loc_gdbarch, sal.pc, b->type);
4502
4503 b->loc = allocate_bp_location (b);
4504 b->loc->gdbarch = loc_gdbarch;
4505 b->loc->requested_address = sal.pc;
4506 b->loc->address = adjusted_address;
4507
4508 if (sal.symtab == NULL)
4509 b->source_file = NULL;
4510 else
4511 b->source_file = xstrdup (sal.symtab->filename);
4512 b->loc->section = sal.section;
4513 b->line_number = sal.line;
4514
4515 set_breakpoint_location_function (b->loc);
4516
4517 breakpoints_changed ();
4518
4519 return b;
4520 }
4521
4522
4523 /* Note that the breakpoint object B describes a permanent breakpoint
4524 instruction, hard-wired into the inferior's code. */
4525 void
4526 make_breakpoint_permanent (struct breakpoint *b)
4527 {
4528 struct bp_location *bl;
4529 b->enable_state = bp_permanent;
4530
4531 /* By definition, permanent breakpoints are already present in the code.
4532 Mark all locations as inserted. For now, make_breakpoint_permanent
4533 is called in just one place, so it's hard to say if it's reasonable
4534 to have permanent breakpoint with multiple locations or not,
4535 but it's easy to implmement. */
4536 for (bl = b->loc; bl; bl = bl->next)
4537 bl->inserted = 1;
4538 }
4539
4540 /* Call this routine when stepping and nexting to enable a breakpoint
4541 if we do a longjmp() in THREAD. When we hit that breakpoint, call
4542 set_longjmp_resume_breakpoint() to figure out where we are going. */
4543
4544 void
4545 set_longjmp_breakpoint (int thread)
4546 {
4547 struct breakpoint *b, *temp;
4548
4549 /* To avoid having to rescan all objfile symbols at every step,
4550 we maintain a list of continually-inserted but always disabled
4551 longjmp "master" breakpoints. Here, we simply create momentary
4552 clones of those and enable them for the requested thread. */
4553 ALL_BREAKPOINTS_SAFE (b, temp)
4554 if (b->type == bp_longjmp_master)
4555 {
4556 struct breakpoint *clone = clone_momentary_breakpoint (b);
4557 clone->type = bp_longjmp;
4558 clone->thread = thread;
4559 }
4560 }
4561
4562 /* Delete all longjmp breakpoints from THREAD. */
4563 void
4564 delete_longjmp_breakpoint (int thread)
4565 {
4566 struct breakpoint *b, *temp;
4567
4568 ALL_BREAKPOINTS_SAFE (b, temp)
4569 if (b->type == bp_longjmp)
4570 {
4571 if (b->thread == thread)
4572 delete_breakpoint (b);
4573 }
4574 }
4575
4576 void
4577 enable_overlay_breakpoints (void)
4578 {
4579 struct breakpoint *b;
4580
4581 ALL_BREAKPOINTS (b)
4582 if (b->type == bp_overlay_event)
4583 {
4584 b->enable_state = bp_enabled;
4585 update_global_location_list (1);
4586 overlay_events_enabled = 1;
4587 }
4588 }
4589
4590 void
4591 disable_overlay_breakpoints (void)
4592 {
4593 struct breakpoint *b;
4594
4595 ALL_BREAKPOINTS (b)
4596 if (b->type == bp_overlay_event)
4597 {
4598 b->enable_state = bp_disabled;
4599 update_global_location_list (0);
4600 overlay_events_enabled = 0;
4601 }
4602 }
4603
4604 struct breakpoint *
4605 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
4606 {
4607 struct breakpoint *b;
4608
4609 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
4610
4611 b->enable_state = bp_enabled;
4612 /* addr_string has to be used or breakpoint_re_set will delete me. */
4613 b->addr_string
4614 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
4615
4616 update_global_location_list_nothrow (1);
4617
4618 return b;
4619 }
4620
4621 void
4622 remove_thread_event_breakpoints (void)
4623 {
4624 struct breakpoint *b, *temp;
4625
4626 ALL_BREAKPOINTS_SAFE (b, temp)
4627 if (b->type == bp_thread_event)
4628 delete_breakpoint (b);
4629 }
4630
4631 struct captured_parse_breakpoint_args
4632 {
4633 char **arg_p;
4634 struct symtabs_and_lines *sals_p;
4635 char ***addr_string_p;
4636 int *not_found_ptr;
4637 };
4638
4639 struct lang_and_radix
4640 {
4641 enum language lang;
4642 int radix;
4643 };
4644
4645
4646 void
4647 remove_solib_event_breakpoints (void)
4648 {
4649 struct breakpoint *b, *temp;
4650
4651 ALL_BREAKPOINTS_SAFE (b, temp)
4652 if (b->type == bp_shlib_event)
4653 delete_breakpoint (b);
4654 }
4655
4656 struct breakpoint *
4657 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
4658 {
4659 struct breakpoint *b;
4660
4661 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
4662 update_global_location_list_nothrow (1);
4663 return b;
4664 }
4665
4666 /* Disable any breakpoints that are on code in shared libraries. Only
4667 apply to enabled breakpoints, disabled ones can just stay disabled. */
4668
4669 void
4670 disable_breakpoints_in_shlibs (void)
4671 {
4672 struct bp_location *loc;
4673
4674 ALL_BP_LOCATIONS (loc)
4675 {
4676 struct breakpoint *b = loc->owner;
4677 /* We apply the check to all breakpoints, including disabled
4678 for those with loc->duplicate set. This is so that when breakpoint
4679 becomes enabled, or the duplicate is removed, gdb will try to insert
4680 all breakpoints. If we don't set shlib_disabled here, we'll try
4681 to insert those breakpoints and fail. */
4682 if (((b->type == bp_breakpoint)
4683 || (b->type == bp_hardware_breakpoint)
4684 || (b->type == bp_tracepoint))
4685 && !loc->shlib_disabled
4686 #ifdef PC_SOLIB
4687 && PC_SOLIB (loc->address)
4688 #else
4689 && solib_name_from_address (loc->address)
4690 #endif
4691 )
4692 {
4693 loc->shlib_disabled = 1;
4694 }
4695 }
4696 }
4697
4698 /* Disable any breakpoints that are in in an unloaded shared library. Only
4699 apply to enabled breakpoints, disabled ones can just stay disabled. */
4700
4701 static void
4702 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
4703 {
4704 struct bp_location *loc;
4705 int disabled_shlib_breaks = 0;
4706
4707 /* SunOS a.out shared libraries are always mapped, so do not
4708 disable breakpoints; they will only be reported as unloaded
4709 through clear_solib when GDB discards its shared library
4710 list. See clear_solib for more information. */
4711 if (exec_bfd != NULL
4712 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
4713 return;
4714
4715 ALL_BP_LOCATIONS (loc)
4716 {
4717 struct breakpoint *b = loc->owner;
4718 if ((loc->loc_type == bp_loc_hardware_breakpoint
4719 || loc->loc_type == bp_loc_software_breakpoint)
4720 && !loc->shlib_disabled
4721 && (b->type == bp_breakpoint || b->type == bp_hardware_breakpoint)
4722 && solib_contains_address_p (solib, loc->address))
4723 {
4724 loc->shlib_disabled = 1;
4725 /* At this point, we cannot rely on remove_breakpoint
4726 succeeding so we must mark the breakpoint as not inserted
4727 to prevent future errors occurring in remove_breakpoints. */
4728 loc->inserted = 0;
4729 if (!disabled_shlib_breaks)
4730 {
4731 target_terminal_ours_for_output ();
4732 warning (_("Temporarily disabling breakpoints for unloaded shared library \"%s\""),
4733 solib->so_name);
4734 }
4735 disabled_shlib_breaks = 1;
4736 }
4737 }
4738 }
4739
4740 /* FORK & VFORK catchpoints. */
4741
4742 /* Implement the "insert" breakpoint_ops method for fork catchpoints. */
4743
4744 static void
4745 insert_catch_fork (struct breakpoint *b)
4746 {
4747 target_insert_fork_catchpoint (PIDGET (inferior_ptid));
4748 }
4749
4750 /* Implement the "remove" breakpoint_ops method for fork catchpoints. */
4751
4752 static int
4753 remove_catch_fork (struct breakpoint *b)
4754 {
4755 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
4756 }
4757
4758 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
4759 catchpoints. */
4760
4761 static int
4762 breakpoint_hit_catch_fork (struct breakpoint *b)
4763 {
4764 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
4765 }
4766
4767 /* Implement the "print_it" breakpoint_ops method for fork catchpoints. */
4768
4769 static enum print_stop_action
4770 print_it_catch_fork (struct breakpoint *b)
4771 {
4772 annotate_catchpoint (b->number);
4773 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
4774 b->number, ptid_get_pid (b->forked_inferior_pid));
4775 return PRINT_SRC_AND_LOC;
4776 }
4777
4778 /* Implement the "print_one" breakpoint_ops method for fork catchpoints. */
4779
4780 static void
4781 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
4782 {
4783 struct value_print_options opts;
4784
4785 get_user_print_options (&opts);
4786
4787 /* Field 4, the address, is omitted (which makes the columns
4788 not line up too nicely with the headers, but the effect
4789 is relatively readable). */
4790 if (opts.addressprint)
4791 ui_out_field_skip (uiout, "addr");
4792 annotate_field (5);
4793 ui_out_text (uiout, "fork");
4794 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
4795 {
4796 ui_out_text (uiout, ", process ");
4797 ui_out_field_int (uiout, "what",
4798 ptid_get_pid (b->forked_inferior_pid));
4799 ui_out_spaces (uiout, 1);
4800 }
4801 }
4802
4803 /* Implement the "print_mention" breakpoint_ops method for fork
4804 catchpoints. */
4805
4806 static void
4807 print_mention_catch_fork (struct breakpoint *b)
4808 {
4809 printf_filtered (_("Catchpoint %d (fork)"), b->number);
4810 }
4811
4812 /* The breakpoint_ops structure to be used in fork catchpoints. */
4813
4814 static struct breakpoint_ops catch_fork_breakpoint_ops =
4815 {
4816 insert_catch_fork,
4817 remove_catch_fork,
4818 breakpoint_hit_catch_fork,
4819 print_it_catch_fork,
4820 print_one_catch_fork,
4821 print_mention_catch_fork
4822 };
4823
4824 /* Implement the "insert" breakpoint_ops method for vfork catchpoints. */
4825
4826 static void
4827 insert_catch_vfork (struct breakpoint *b)
4828 {
4829 target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
4830 }
4831
4832 /* Implement the "remove" breakpoint_ops method for vfork catchpoints. */
4833
4834 static int
4835 remove_catch_vfork (struct breakpoint *b)
4836 {
4837 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
4838 }
4839
4840 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
4841 catchpoints. */
4842
4843 static int
4844 breakpoint_hit_catch_vfork (struct breakpoint *b)
4845 {
4846 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
4847 }
4848
4849 /* Implement the "print_it" breakpoint_ops method for vfork catchpoints. */
4850
4851 static enum print_stop_action
4852 print_it_catch_vfork (struct breakpoint *b)
4853 {
4854 annotate_catchpoint (b->number);
4855 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
4856 b->number, ptid_get_pid (b->forked_inferior_pid));
4857 return PRINT_SRC_AND_LOC;
4858 }
4859
4860 /* Implement the "print_one" breakpoint_ops method for vfork catchpoints. */
4861
4862 static void
4863 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
4864 {
4865 struct value_print_options opts;
4866
4867 get_user_print_options (&opts);
4868 /* Field 4, the address, is omitted (which makes the columns
4869 not line up too nicely with the headers, but the effect
4870 is relatively readable). */
4871 if (opts.addressprint)
4872 ui_out_field_skip (uiout, "addr");
4873 annotate_field (5);
4874 ui_out_text (uiout, "vfork");
4875 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
4876 {
4877 ui_out_text (uiout, ", process ");
4878 ui_out_field_int (uiout, "what",
4879 ptid_get_pid (b->forked_inferior_pid));
4880 ui_out_spaces (uiout, 1);
4881 }
4882 }
4883
4884 /* Implement the "print_mention" breakpoint_ops method for vfork
4885 catchpoints. */
4886
4887 static void
4888 print_mention_catch_vfork (struct breakpoint *b)
4889 {
4890 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
4891 }
4892
4893 /* The breakpoint_ops structure to be used in vfork catchpoints. */
4894
4895 static struct breakpoint_ops catch_vfork_breakpoint_ops =
4896 {
4897 insert_catch_vfork,
4898 remove_catch_vfork,
4899 breakpoint_hit_catch_vfork,
4900 print_it_catch_vfork,
4901 print_one_catch_vfork,
4902 print_mention_catch_vfork
4903 };
4904
4905 /* Create a new breakpoint of the bp_catchpoint kind and return it.
4906
4907 If TEMPFLAG is non-zero, then make the breakpoint temporary.
4908 If COND_STRING is not NULL, then store it in the breakpoint.
4909 OPS, if not NULL, is the breakpoint_ops structure associated
4910 to the catchpoint. */
4911
4912 static struct breakpoint *
4913 create_catchpoint (struct gdbarch *gdbarch, int tempflag,
4914 char *cond_string, struct breakpoint_ops *ops)
4915 {
4916 struct symtab_and_line sal;
4917 struct breakpoint *b;
4918
4919 init_sal (&sal);
4920 sal.pc = 0;
4921 sal.symtab = NULL;
4922 sal.line = 0;
4923
4924 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
4925 set_breakpoint_count (breakpoint_count + 1);
4926 b->number = breakpoint_count;
4927
4928 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
4929 b->thread = -1;
4930 b->addr_string = NULL;
4931 b->enable_state = bp_enabled;
4932 b->disposition = tempflag ? disp_del : disp_donttouch;
4933 b->ops = ops;
4934
4935 mention (b);
4936 update_global_location_list (1);
4937
4938 return b;
4939 }
4940
4941 static void
4942 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
4943 int tempflag, char *cond_string,
4944 struct breakpoint_ops *ops)
4945 {
4946 struct breakpoint *b
4947 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
4948
4949 /* FIXME: We should put this information in a breakpoint private data
4950 area. */
4951 b->forked_inferior_pid = null_ptid;
4952 }
4953
4954 /* Exec catchpoints. */
4955
4956 static void
4957 insert_catch_exec (struct breakpoint *b)
4958 {
4959 target_insert_exec_catchpoint (PIDGET (inferior_ptid));
4960 }
4961
4962 static int
4963 remove_catch_exec (struct breakpoint *b)
4964 {
4965 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
4966 }
4967
4968 static int
4969 breakpoint_hit_catch_exec (struct breakpoint *b)
4970 {
4971 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
4972 }
4973
4974 static enum print_stop_action
4975 print_it_catch_exec (struct breakpoint *b)
4976 {
4977 annotate_catchpoint (b->number);
4978 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
4979 b->exec_pathname);
4980 return PRINT_SRC_AND_LOC;
4981 }
4982
4983 static void
4984 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
4985 {
4986 struct value_print_options opts;
4987
4988 get_user_print_options (&opts);
4989
4990 /* Field 4, the address, is omitted (which makes the columns
4991 not line up too nicely with the headers, but the effect
4992 is relatively readable). */
4993 if (opts.addressprint)
4994 ui_out_field_skip (uiout, "addr");
4995 annotate_field (5);
4996 ui_out_text (uiout, "exec");
4997 if (b->exec_pathname != NULL)
4998 {
4999 ui_out_text (uiout, ", program \"");
5000 ui_out_field_string (uiout, "what", b->exec_pathname);
5001 ui_out_text (uiout, "\" ");
5002 }
5003 }
5004
5005 static void
5006 print_mention_catch_exec (struct breakpoint *b)
5007 {
5008 printf_filtered (_("Catchpoint %d (exec)"), b->number);
5009 }
5010
5011 static struct breakpoint_ops catch_exec_breakpoint_ops =
5012 {
5013 insert_catch_exec,
5014 remove_catch_exec,
5015 breakpoint_hit_catch_exec,
5016 print_it_catch_exec,
5017 print_one_catch_exec,
5018 print_mention_catch_exec
5019 };
5020
5021 static int
5022 hw_breakpoint_used_count (void)
5023 {
5024 struct breakpoint *b;
5025 int i = 0;
5026
5027 ALL_BREAKPOINTS (b)
5028 {
5029 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
5030 i++;
5031 }
5032
5033 return i;
5034 }
5035
5036 static int
5037 hw_watchpoint_used_count (enum bptype type, int *other_type_used)
5038 {
5039 struct breakpoint *b;
5040 int i = 0;
5041
5042 *other_type_used = 0;
5043 ALL_BREAKPOINTS (b)
5044 {
5045 if (breakpoint_enabled (b))
5046 {
5047 if (b->type == type)
5048 i++;
5049 else if ((b->type == bp_hardware_watchpoint
5050 || b->type == bp_read_watchpoint
5051 || b->type == bp_access_watchpoint))
5052 *other_type_used = 1;
5053 }
5054 }
5055 return i;
5056 }
5057
5058 void
5059 disable_watchpoints_before_interactive_call_start (void)
5060 {
5061 struct breakpoint *b;
5062
5063 ALL_BREAKPOINTS (b)
5064 {
5065 if (((b->type == bp_watchpoint)
5066 || (b->type == bp_hardware_watchpoint)
5067 || (b->type == bp_read_watchpoint)
5068 || (b->type == bp_access_watchpoint))
5069 && breakpoint_enabled (b))
5070 {
5071 b->enable_state = bp_call_disabled;
5072 update_global_location_list (0);
5073 }
5074 }
5075 }
5076
5077 void
5078 enable_watchpoints_after_interactive_call_stop (void)
5079 {
5080 struct breakpoint *b;
5081
5082 ALL_BREAKPOINTS (b)
5083 {
5084 if (((b->type == bp_watchpoint)
5085 || (b->type == bp_hardware_watchpoint)
5086 || (b->type == bp_read_watchpoint)
5087 || (b->type == bp_access_watchpoint))
5088 && (b->enable_state == bp_call_disabled))
5089 {
5090 b->enable_state = bp_enabled;
5091 update_global_location_list (1);
5092 }
5093 }
5094 }
5095
5096
5097 /* Set a breakpoint that will evaporate an end of command
5098 at address specified by SAL.
5099 Restrict it to frame FRAME if FRAME is nonzero. */
5100
5101 struct breakpoint *
5102 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
5103 struct frame_id frame_id, enum bptype type)
5104 {
5105 struct breakpoint *b;
5106
5107 /* If FRAME_ID is valid, it should be a real frame, not an inlined
5108 one. */
5109 gdb_assert (!frame_id_inlined_p (frame_id));
5110
5111 b = set_raw_breakpoint (gdbarch, sal, type);
5112 b->enable_state = bp_enabled;
5113 b->disposition = disp_donttouch;
5114 b->frame_id = frame_id;
5115
5116 /* If we're debugging a multi-threaded program, then we
5117 want momentary breakpoints to be active in only a
5118 single thread of control. */
5119 if (in_thread_list (inferior_ptid))
5120 b->thread = pid_to_thread_id (inferior_ptid);
5121
5122 update_global_location_list_nothrow (1);
5123
5124 return b;
5125 }
5126
5127 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
5128 ORIG is NULL. */
5129
5130 struct breakpoint *
5131 clone_momentary_breakpoint (struct breakpoint *orig)
5132 {
5133 struct breakpoint *copy;
5134
5135 /* If there's nothing to clone, then return nothing. */
5136 if (orig == NULL)
5137 return NULL;
5138
5139 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
5140 copy->loc = allocate_bp_location (copy);
5141 set_breakpoint_location_function (copy->loc);
5142
5143 copy->loc->gdbarch = orig->loc->gdbarch;
5144 copy->loc->requested_address = orig->loc->requested_address;
5145 copy->loc->address = orig->loc->address;
5146 copy->loc->section = orig->loc->section;
5147
5148 if (orig->source_file == NULL)
5149 copy->source_file = NULL;
5150 else
5151 copy->source_file = xstrdup (orig->source_file);
5152
5153 copy->line_number = orig->line_number;
5154 copy->frame_id = orig->frame_id;
5155 copy->thread = orig->thread;
5156
5157 copy->enable_state = bp_enabled;
5158 copy->disposition = disp_donttouch;
5159 copy->number = internal_breakpoint_number--;
5160
5161 update_global_location_list_nothrow (0);
5162 return copy;
5163 }
5164
5165 struct breakpoint *
5166 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
5167 enum bptype type)
5168 {
5169 struct symtab_and_line sal;
5170
5171 sal = find_pc_line (pc, 0);
5172 sal.pc = pc;
5173 sal.section = find_pc_overlay (pc);
5174 sal.explicit_pc = 1;
5175
5176 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
5177 }
5178 \f
5179
5180 /* Tell the user we have just set a breakpoint B. */
5181
5182 static void
5183 mention (struct breakpoint *b)
5184 {
5185 int say_where = 0;
5186 struct cleanup *ui_out_chain;
5187 struct value_print_options opts;
5188
5189 get_user_print_options (&opts);
5190
5191 /* FIXME: This is misplaced; mention() is called by things (like
5192 hitting a watchpoint) other than breakpoint creation. It should
5193 be possible to clean this up and at the same time replace the
5194 random calls to breakpoint_changed with this hook. */
5195 observer_notify_breakpoint_created (b->number);
5196
5197 if (b->ops != NULL && b->ops->print_mention != NULL)
5198 b->ops->print_mention (b);
5199 else
5200 switch (b->type)
5201 {
5202 case bp_none:
5203 printf_filtered (_("(apparently deleted?) Eventpoint %d: "), b->number);
5204 break;
5205 case bp_watchpoint:
5206 ui_out_text (uiout, "Watchpoint ");
5207 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
5208 ui_out_field_int (uiout, "number", b->number);
5209 ui_out_text (uiout, ": ");
5210 ui_out_field_string (uiout, "exp", b->exp_string);
5211 do_cleanups (ui_out_chain);
5212 break;
5213 case bp_hardware_watchpoint:
5214 ui_out_text (uiout, "Hardware watchpoint ");
5215 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
5216 ui_out_field_int (uiout, "number", b->number);
5217 ui_out_text (uiout, ": ");
5218 ui_out_field_string (uiout, "exp", b->exp_string);
5219 do_cleanups (ui_out_chain);
5220 break;
5221 case bp_read_watchpoint:
5222 ui_out_text (uiout, "Hardware read watchpoint ");
5223 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
5224 ui_out_field_int (uiout, "number", b->number);
5225 ui_out_text (uiout, ": ");
5226 ui_out_field_string (uiout, "exp", b->exp_string);
5227 do_cleanups (ui_out_chain);
5228 break;
5229 case bp_access_watchpoint:
5230 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
5231 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
5232 ui_out_field_int (uiout, "number", b->number);
5233 ui_out_text (uiout, ": ");
5234 ui_out_field_string (uiout, "exp", b->exp_string);
5235 do_cleanups (ui_out_chain);
5236 break;
5237 case bp_breakpoint:
5238 if (ui_out_is_mi_like_p (uiout))
5239 {
5240 say_where = 0;
5241 break;
5242 }
5243 if (b->disposition == disp_del)
5244 printf_filtered (_("Temporary breakpoint"));
5245 else
5246 printf_filtered (_("Breakpoint"));
5247 printf_filtered (_(" %d"), b->number);
5248 say_where = 1;
5249 break;
5250 case bp_hardware_breakpoint:
5251 if (ui_out_is_mi_like_p (uiout))
5252 {
5253 say_where = 0;
5254 break;
5255 }
5256 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
5257 say_where = 1;
5258 break;
5259 case bp_tracepoint:
5260 if (ui_out_is_mi_like_p (uiout))
5261 {
5262 say_where = 0;
5263 break;
5264 }
5265 printf_filtered (_("Tracepoint"));
5266 printf_filtered (_(" %d"), b->number);
5267 say_where = 1;
5268 break;
5269
5270 case bp_until:
5271 case bp_finish:
5272 case bp_longjmp:
5273 case bp_longjmp_resume:
5274 case bp_step_resume:
5275 case bp_call_dummy:
5276 case bp_watchpoint_scope:
5277 case bp_shlib_event:
5278 case bp_thread_event:
5279 case bp_overlay_event:
5280 case bp_longjmp_master:
5281 break;
5282 }
5283
5284 if (say_where)
5285 {
5286 /* i18n: cagney/2005-02-11: Below needs to be merged into a
5287 single string. */
5288 if (b->loc == NULL)
5289 {
5290 printf_filtered (_(" (%s) pending."), b->addr_string);
5291 }
5292 else
5293 {
5294 if (opts.addressprint || b->source_file == NULL)
5295 {
5296 printf_filtered (" at ");
5297 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
5298 gdb_stdout);
5299 }
5300 if (b->source_file)
5301 printf_filtered (": file %s, line %d.",
5302 b->source_file, b->line_number);
5303
5304 if (b->loc->next)
5305 {
5306 struct bp_location *loc = b->loc;
5307 int n = 0;
5308 for (; loc; loc = loc->next)
5309 ++n;
5310 printf_filtered (" (%d locations)", n);
5311 }
5312
5313 }
5314 }
5315 if (ui_out_is_mi_like_p (uiout))
5316 return;
5317 printf_filtered ("\n");
5318 }
5319 \f
5320
5321 static struct bp_location *
5322 add_location_to_breakpoint (struct breakpoint *b,
5323 const struct symtab_and_line *sal)
5324 {
5325 struct bp_location *loc, **tmp;
5326
5327 loc = allocate_bp_location (b);
5328 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
5329 ;
5330 *tmp = loc;
5331 loc->gdbarch = get_sal_arch (*sal);
5332 if (!loc->gdbarch)
5333 loc->gdbarch = b->gdbarch;
5334 loc->requested_address = sal->pc;
5335 loc->address = adjust_breakpoint_address (loc->gdbarch,
5336 loc->requested_address, b->type);
5337 loc->section = sal->section;
5338
5339 set_breakpoint_location_function (loc);
5340 return loc;
5341 }
5342 \f
5343
5344 /* Return 1 if LOC is pointing to a permanent breakpoint,
5345 return 0 otherwise. */
5346
5347 static int
5348 bp_loc_is_permanent (struct bp_location *loc)
5349 {
5350 int len;
5351 CORE_ADDR addr;
5352 const gdb_byte *brk;
5353 gdb_byte *target_mem;
5354 struct cleanup *cleanup;
5355 int retval = 0;
5356
5357 gdb_assert (loc != NULL);
5358
5359 addr = loc->address;
5360 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
5361
5362 /* Software breakpoints unsupported? */
5363 if (brk == NULL)
5364 return 0;
5365
5366 target_mem = alloca (len);
5367
5368 /* Enable the automatic memory restoration from breakpoints while
5369 we read the memory. Otherwise we could say about our temporary
5370 breakpoints they are permanent. */
5371 cleanup = make_show_memory_breakpoints_cleanup (0);
5372
5373 if (target_read_memory (loc->address, target_mem, len) == 0
5374 && memcmp (target_mem, brk, len) == 0)
5375 retval = 1;
5376
5377 do_cleanups (cleanup);
5378
5379 return retval;
5380 }
5381
5382
5383
5384 /* Create a breakpoint with SAL as location. Use ADDR_STRING
5385 as textual description of the location, and COND_STRING
5386 as condition expression. */
5387
5388 static void
5389 create_breakpoint (struct gdbarch *gdbarch,
5390 struct symtabs_and_lines sals, char *addr_string,
5391 char *cond_string,
5392 enum bptype type, enum bpdisp disposition,
5393 int thread, int task, int ignore_count,
5394 struct breakpoint_ops *ops, int from_tty, int enabled)
5395 {
5396 struct breakpoint *b = NULL;
5397 int i;
5398
5399 if (type == bp_hardware_breakpoint)
5400 {
5401 int i = hw_breakpoint_used_count ();
5402 int target_resources_ok =
5403 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
5404 i + 1, 0);
5405 if (target_resources_ok == 0)
5406 error (_("No hardware breakpoint support in the target."));
5407 else if (target_resources_ok < 0)
5408 error (_("Hardware breakpoints used exceeds limit."));
5409 }
5410
5411 for (i = 0; i < sals.nelts; ++i)
5412 {
5413 struct symtab_and_line sal = sals.sals[i];
5414 struct bp_location *loc;
5415
5416 if (from_tty)
5417 {
5418 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
5419 if (!loc_gdbarch)
5420 loc_gdbarch = gdbarch;
5421
5422 describe_other_breakpoints (loc_gdbarch,
5423 sal.pc, sal.section, thread);
5424 }
5425
5426 if (i == 0)
5427 {
5428 b = set_raw_breakpoint (gdbarch, sal, type);
5429 set_breakpoint_count (breakpoint_count + 1);
5430 b->number = breakpoint_count;
5431 b->thread = thread;
5432 b->task = task;
5433
5434 b->cond_string = cond_string;
5435 b->ignore_count = ignore_count;
5436 b->enable_state = enabled ? bp_enabled : bp_disabled;
5437 b->disposition = disposition;
5438
5439 loc = b->loc;
5440 }
5441 else
5442 {
5443 loc = add_location_to_breakpoint (b, &sal);
5444 }
5445
5446 if (bp_loc_is_permanent (loc))
5447 make_breakpoint_permanent (b);
5448
5449 if (b->cond_string)
5450 {
5451 char *arg = b->cond_string;
5452 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
5453 if (*arg)
5454 error (_("Garbage %s follows condition"), arg);
5455 }
5456 }
5457
5458 if (addr_string)
5459 b->addr_string = addr_string;
5460 else
5461 /* addr_string has to be used or breakpoint_re_set will delete
5462 me. */
5463 b->addr_string
5464 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
5465
5466 b->ops = ops;
5467 mention (b);
5468 }
5469
5470 /* Remove element at INDEX_TO_REMOVE from SAL, shifting other
5471 elements to fill the void space. */
5472 static void
5473 remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
5474 {
5475 int i = index_to_remove+1;
5476 int last_index = sal->nelts-1;
5477
5478 for (;i <= last_index; ++i)
5479 sal->sals[i-1] = sal->sals[i];
5480
5481 --(sal->nelts);
5482 }
5483
5484 /* If appropriate, obtains all sals that correspond
5485 to the same file and line as SAL. This is done
5486 only if SAL does not have explicit PC and has
5487 line and file information. If we got just a single
5488 expanded sal, return the original.
5489
5490 Otherwise, if SAL.explicit_line is not set, filter out
5491 all sals for which the name of enclosing function
5492 is different from SAL. This makes sure that if we have
5493 breakpoint originally set in template instantiation, say
5494 foo<int>(), we won't expand SAL to locations at the same
5495 line in all existing instantiations of 'foo'.
5496
5497 */
5498 static struct symtabs_and_lines
5499 expand_line_sal_maybe (struct symtab_and_line sal)
5500 {
5501 struct symtabs_and_lines expanded;
5502 CORE_ADDR original_pc = sal.pc;
5503 char *original_function = NULL;
5504 int found;
5505 int i;
5506
5507 /* If we have explicit pc, don't expand.
5508 If we have no line number, we can't expand. */
5509 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
5510 {
5511 expanded.nelts = 1;
5512 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5513 expanded.sals[0] = sal;
5514 return expanded;
5515 }
5516
5517 sal.pc = 0;
5518 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
5519
5520 expanded = expand_line_sal (sal);
5521 if (expanded.nelts == 1)
5522 {
5523 /* We had one sal, we got one sal. Without futher
5524 processing, just return the original sal. */
5525 xfree (expanded.sals);
5526 expanded.nelts = 1;
5527 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5528 sal.pc = original_pc;
5529 expanded.sals[0] = sal;
5530 return expanded;
5531 }
5532
5533 if (!sal.explicit_line)
5534 {
5535 CORE_ADDR func_addr, func_end;
5536 for (i = 0; i < expanded.nelts; ++i)
5537 {
5538 CORE_ADDR pc = expanded.sals[i].pc;
5539 char *this_function;
5540 if (find_pc_partial_function (pc, &this_function,
5541 &func_addr, &func_end))
5542 {
5543 if (this_function
5544 && strcmp (this_function, original_function) != 0)
5545 {
5546 remove_sal (&expanded, i);
5547 --i;
5548 }
5549 else if (func_addr == pc)
5550 {
5551 /* We're at beginning of a function, and should
5552 skip prologue. */
5553 struct symbol *sym = find_pc_function (pc);
5554 if (sym)
5555 expanded.sals[i] = find_function_start_sal (sym, 1);
5556 else
5557 {
5558 /* Since find_pc_partial_function returned true,
5559 we should really always find the section here. */
5560 struct obj_section *section = find_pc_section (pc);
5561 if (section)
5562 {
5563 struct gdbarch *gdbarch
5564 = get_objfile_arch (section->objfile);
5565 expanded.sals[i].pc
5566 = gdbarch_skip_prologue (gdbarch, pc);
5567 }
5568 }
5569 }
5570 }
5571 }
5572 }
5573 else
5574 {
5575 for (i = 0; i < expanded.nelts; ++i)
5576 {
5577 /* If this SAL corresponds to a breakpoint inserted using a
5578 line number, then skip the function prologue if necessary. */
5579 skip_prologue_sal (&expanded.sals[i]);
5580 }
5581 }
5582
5583
5584 if (expanded.nelts <= 1)
5585 {
5586 /* This is un ugly workaround. If we get zero
5587 expanded sals then something is really wrong.
5588 Fix that by returnign the original sal. */
5589 xfree (expanded.sals);
5590 expanded.nelts = 1;
5591 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5592 sal.pc = original_pc;
5593 expanded.sals[0] = sal;
5594 return expanded;
5595 }
5596
5597 if (original_pc)
5598 {
5599 found = 0;
5600 for (i = 0; i < expanded.nelts; ++i)
5601 if (expanded.sals[i].pc == original_pc)
5602 {
5603 found = 1;
5604 break;
5605 }
5606 gdb_assert (found);
5607 }
5608
5609 return expanded;
5610 }
5611
5612 /* Add SALS.nelts breakpoints to the breakpoint table. For each
5613 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
5614 value. COND_STRING, if not NULL, specified the condition to be
5615 used for all breakpoints. Essentially the only case where
5616 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
5617 function. In that case, it's still not possible to specify
5618 separate conditions for different overloaded functions, so
5619 we take just a single condition string.
5620
5621 NOTE: If the function succeeds, the caller is expected to cleanup
5622 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
5623 array contents). If the function fails (error() is called), the
5624 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
5625 COND and SALS arrays and each of those arrays contents. */
5626
5627 static void
5628 create_breakpoints (struct gdbarch *gdbarch,
5629 struct symtabs_and_lines sals, char **addr_string,
5630 char *cond_string,
5631 enum bptype type, enum bpdisp disposition,
5632 int thread, int task, int ignore_count,
5633 struct breakpoint_ops *ops, int from_tty,
5634 int enabled)
5635 {
5636 int i;
5637 for (i = 0; i < sals.nelts; ++i)
5638 {
5639 struct symtabs_and_lines expanded =
5640 expand_line_sal_maybe (sals.sals[i]);
5641
5642 create_breakpoint (gdbarch, expanded, addr_string[i],
5643 cond_string, type, disposition,
5644 thread, task, ignore_count, ops, from_tty, enabled);
5645 }
5646 }
5647
5648 /* Parse ARG which is assumed to be a SAL specification possibly
5649 followed by conditionals. On return, SALS contains an array of SAL
5650 addresses found. ADDR_STRING contains a vector of (canonical)
5651 address strings. ARG points to the end of the SAL. */
5652
5653 static void
5654 parse_breakpoint_sals (char **address,
5655 struct symtabs_and_lines *sals,
5656 char ***addr_string,
5657 int *not_found_ptr)
5658 {
5659 char *addr_start = *address;
5660 *addr_string = NULL;
5661 /* If no arg given, or if first arg is 'if ', use the default
5662 breakpoint. */
5663 if ((*address) == NULL
5664 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
5665 {
5666 if (default_breakpoint_valid)
5667 {
5668 struct symtab_and_line sal;
5669 init_sal (&sal); /* initialize to zeroes */
5670 sals->sals = (struct symtab_and_line *)
5671 xmalloc (sizeof (struct symtab_and_line));
5672 sal.pc = default_breakpoint_address;
5673 sal.line = default_breakpoint_line;
5674 sal.symtab = default_breakpoint_symtab;
5675 sal.section = find_pc_overlay (sal.pc);
5676
5677 /* "break" without arguments is equivalent to "break *PC" where PC is
5678 the default_breakpoint_address. So make sure to set
5679 sal.explicit_pc to prevent GDB from trying to expand the list of
5680 sals to include all other instances with the same symtab and line.
5681 */
5682 sal.explicit_pc = 1;
5683
5684 sals->sals[0] = sal;
5685 sals->nelts = 1;
5686 }
5687 else
5688 error (_("No default breakpoint address now."));
5689 }
5690 else
5691 {
5692 /* Force almost all breakpoints to be in terms of the
5693 current_source_symtab (which is decode_line_1's default). This
5694 should produce the results we want almost all of the time while
5695 leaving default_breakpoint_* alone.
5696 ObjC: However, don't match an Objective-C method name which
5697 may have a '+' or '-' succeeded by a '[' */
5698
5699 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
5700
5701 if (default_breakpoint_valid
5702 && (!cursal.symtab
5703 || ((strchr ("+-", (*address)[0]) != NULL)
5704 && ((*address)[1] != '['))))
5705 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
5706 default_breakpoint_line, addr_string,
5707 not_found_ptr);
5708 else
5709 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
5710 addr_string, not_found_ptr);
5711 }
5712 /* For any SAL that didn't have a canonical string, fill one in. */
5713 if (sals->nelts > 0 && *addr_string == NULL)
5714 *addr_string = xcalloc (sals->nelts, sizeof (char **));
5715 if (addr_start != (*address))
5716 {
5717 int i;
5718 for (i = 0; i < sals->nelts; i++)
5719 {
5720 /* Add the string if not present. */
5721 if ((*addr_string)[i] == NULL)
5722 (*addr_string)[i] = savestring (addr_start, (*address) - addr_start);
5723 }
5724 }
5725 }
5726
5727
5728 /* Convert each SAL into a real PC. Verify that the PC can be
5729 inserted as a breakpoint. If it can't throw an error. */
5730
5731 static void
5732 breakpoint_sals_to_pc (struct symtabs_and_lines *sals,
5733 char *address)
5734 {
5735 int i;
5736 for (i = 0; i < sals->nelts; i++)
5737 resolve_sal_pc (&sals->sals[i]);
5738 }
5739
5740 static void
5741 do_captured_parse_breakpoint (struct ui_out *ui, void *data)
5742 {
5743 struct captured_parse_breakpoint_args *args = data;
5744
5745 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
5746 args->not_found_ptr);
5747 }
5748
5749 /* Given TOK, a string specification of condition and thread, as
5750 accepted by the 'break' command, extract the condition
5751 string and thread number and set *COND_STRING and *THREAD.
5752 PC identifies the context at which the condition should be parsed.
5753 If no condition is found, *COND_STRING is set to NULL.
5754 If no thread is found, *THREAD is set to -1. */
5755 static void
5756 find_condition_and_thread (char *tok, CORE_ADDR pc,
5757 char **cond_string, int *thread, int *task)
5758 {
5759 *cond_string = NULL;
5760 *thread = -1;
5761 while (tok && *tok)
5762 {
5763 char *end_tok;
5764 int toklen;
5765 char *cond_start = NULL;
5766 char *cond_end = NULL;
5767 while (*tok == ' ' || *tok == '\t')
5768 tok++;
5769
5770 end_tok = tok;
5771
5772 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5773 end_tok++;
5774
5775 toklen = end_tok - tok;
5776
5777 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
5778 {
5779 struct expression *expr;
5780
5781 tok = cond_start = end_tok + 1;
5782 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
5783 xfree (expr);
5784 cond_end = tok;
5785 *cond_string = savestring (cond_start,
5786 cond_end - cond_start);
5787 }
5788 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
5789 {
5790 char *tmptok;
5791
5792 tok = end_tok + 1;
5793 tmptok = tok;
5794 *thread = strtol (tok, &tok, 0);
5795 if (tok == tmptok)
5796 error (_("Junk after thread keyword."));
5797 if (!valid_thread_id (*thread))
5798 error (_("Unknown thread %d."), *thread);
5799 }
5800 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
5801 {
5802 char *tmptok;
5803
5804 tok = end_tok + 1;
5805 tmptok = tok;
5806 *task = strtol (tok, &tok, 0);
5807 if (tok == tmptok)
5808 error (_("Junk after task keyword."));
5809 if (!valid_task_id (*task))
5810 error (_("Unknown task %d\n"), *task);
5811 }
5812 else
5813 error (_("Junk at end of arguments."));
5814 }
5815 }
5816
5817 /* Set a breakpoint. This function is shared between
5818 CLI and MI functions for setting a breakpoint.
5819 This function has two major modes of operations,
5820 selected by the PARSE_CONDITION_AND_THREAD parameter.
5821 If non-zero, the function will parse arg, extracting
5822 breakpoint location, address and thread. Otherwise,
5823 ARG is just the location of breakpoint, with condition
5824 and thread specified by the COND_STRING and THREAD
5825 parameters. */
5826
5827 static void
5828 break_command_really (struct gdbarch *gdbarch,
5829 char *arg, char *cond_string, int thread,
5830 int parse_condition_and_thread,
5831 int tempflag, int hardwareflag, int traceflag,
5832 int ignore_count,
5833 enum auto_boolean pending_break_support,
5834 struct breakpoint_ops *ops,
5835 int from_tty,
5836 int enabled)
5837 {
5838 struct gdb_exception e;
5839 struct symtabs_and_lines sals;
5840 struct symtab_and_line pending_sal;
5841 char *copy_arg;
5842 char *err_msg;
5843 char *addr_start = arg;
5844 char **addr_string;
5845 struct cleanup *old_chain;
5846 struct cleanup *bkpt_chain = NULL;
5847 struct captured_parse_breakpoint_args parse_args;
5848 int i;
5849 int pending = 0;
5850 int not_found = 0;
5851 enum bptype type_wanted;
5852 int task = 0;
5853
5854 sals.sals = NULL;
5855 sals.nelts = 0;
5856 addr_string = NULL;
5857
5858 parse_args.arg_p = &arg;
5859 parse_args.sals_p = &sals;
5860 parse_args.addr_string_p = &addr_string;
5861 parse_args.not_found_ptr = &not_found;
5862
5863 e = catch_exception (uiout, do_captured_parse_breakpoint,
5864 &parse_args, RETURN_MASK_ALL);
5865
5866 /* If caller is interested in rc value from parse, set value. */
5867 switch (e.reason)
5868 {
5869 case RETURN_QUIT:
5870 throw_exception (e);
5871 case RETURN_ERROR:
5872 switch (e.error)
5873 {
5874 case NOT_FOUND_ERROR:
5875
5876 /* If pending breakpoint support is turned off, throw
5877 error. */
5878
5879 if (pending_break_support == AUTO_BOOLEAN_FALSE)
5880 throw_exception (e);
5881
5882 exception_print (gdb_stderr, e);
5883
5884 /* If pending breakpoint support is auto query and the user
5885 selects no, then simply return the error code. */
5886 if (pending_break_support == AUTO_BOOLEAN_AUTO
5887 && !nquery ("Make breakpoint pending on future shared library load? "))
5888 return;
5889
5890 /* At this point, either the user was queried about setting
5891 a pending breakpoint and selected yes, or pending
5892 breakpoint behavior is on and thus a pending breakpoint
5893 is defaulted on behalf of the user. */
5894 copy_arg = xstrdup (addr_start);
5895 addr_string = &copy_arg;
5896 sals.nelts = 1;
5897 sals.sals = &pending_sal;
5898 pending_sal.pc = 0;
5899 pending = 1;
5900 break;
5901 default:
5902 throw_exception (e);
5903 }
5904 default:
5905 if (!sals.nelts)
5906 return;
5907 }
5908
5909 /* Create a chain of things that always need to be cleaned up. */
5910 old_chain = make_cleanup (null_cleanup, 0);
5911
5912 if (!pending)
5913 {
5914 /* Make sure that all storage allocated to SALS gets freed. */
5915 make_cleanup (xfree, sals.sals);
5916
5917 /* Cleanup the addr_string array but not its contents. */
5918 make_cleanup (xfree, addr_string);
5919 }
5920
5921 /* ----------------------------- SNIP -----------------------------
5922 Anything added to the cleanup chain beyond this point is assumed
5923 to be part of a breakpoint. If the breakpoint create succeeds
5924 then the memory is not reclaimed. */
5925 bkpt_chain = make_cleanup (null_cleanup, 0);
5926
5927 /* Mark the contents of the addr_string for cleanup. These go on
5928 the bkpt_chain and only occur if the breakpoint create fails. */
5929 for (i = 0; i < sals.nelts; i++)
5930 {
5931 if (addr_string[i] != NULL)
5932 make_cleanup (xfree, addr_string[i]);
5933 }
5934
5935 /* Resolve all line numbers to PC's and verify that the addresses
5936 are ok for the target. */
5937 if (!pending)
5938 breakpoint_sals_to_pc (&sals, addr_start);
5939
5940 type_wanted = (traceflag
5941 ? bp_tracepoint
5942 : (hardwareflag ? bp_hardware_breakpoint : bp_breakpoint));
5943
5944 /* Verify that condition can be parsed, before setting any
5945 breakpoints. Allocate a separate condition expression for each
5946 breakpoint. */
5947 if (!pending)
5948 {
5949 if (parse_condition_and_thread)
5950 {
5951 /* Here we only parse 'arg' to separate condition
5952 from thread number, so parsing in context of first
5953 sal is OK. When setting the breakpoint we'll
5954 re-parse it in context of each sal. */
5955 cond_string = NULL;
5956 thread = -1;
5957 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
5958 &thread, &task);
5959 if (cond_string)
5960 make_cleanup (xfree, cond_string);
5961 }
5962 else
5963 {
5964 /* Create a private copy of condition string. */
5965 if (cond_string)
5966 {
5967 cond_string = xstrdup (cond_string);
5968 make_cleanup (xfree, cond_string);
5969 }
5970 }
5971 create_breakpoints (gdbarch, sals, addr_string, cond_string, type_wanted,
5972 tempflag ? disp_del : disp_donttouch,
5973 thread, task, ignore_count, ops, from_tty, enabled);
5974 }
5975 else
5976 {
5977 struct symtab_and_line sal = {0};
5978 struct breakpoint *b;
5979
5980 make_cleanup (xfree, copy_arg);
5981
5982 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
5983 set_breakpoint_count (breakpoint_count + 1);
5984 b->number = breakpoint_count;
5985 b->thread = -1;
5986 b->addr_string = addr_string[0];
5987 b->cond_string = NULL;
5988 b->ignore_count = ignore_count;
5989 b->disposition = tempflag ? disp_del : disp_donttouch;
5990 b->condition_not_parsed = 1;
5991 b->ops = ops;
5992 b->enable_state = enabled ? bp_enabled : bp_disabled;
5993
5994 mention (b);
5995 }
5996
5997 if (sals.nelts > 1)
5998 warning (_("Multiple breakpoints were set.\n"
5999 "Use the \"delete\" command to delete unwanted breakpoints."));
6000 /* That's it. Discard the cleanups for data inserted into the
6001 breakpoint. */
6002 discard_cleanups (bkpt_chain);
6003 /* But cleanup everything else. */
6004 do_cleanups (old_chain);
6005
6006 /* error call may happen here - have BKPT_CHAIN already discarded. */
6007 update_global_location_list (1);
6008 }
6009
6010 /* Set a breakpoint.
6011 ARG is a string describing breakpoint address,
6012 condition, and thread.
6013 FLAG specifies if a breakpoint is hardware on,
6014 and if breakpoint is temporary, using BP_HARDWARE_FLAG
6015 and BP_TEMPFLAG. */
6016
6017 static void
6018 break_command_1 (char *arg, int flag, int from_tty)
6019 {
6020 int hardwareflag = flag & BP_HARDWAREFLAG;
6021 int tempflag = flag & BP_TEMPFLAG;
6022
6023 break_command_really (get_current_arch (),
6024 arg,
6025 NULL, 0, 1 /* parse arg */,
6026 tempflag, hardwareflag, 0 /* traceflag */,
6027 0 /* Ignore count */,
6028 pending_break_support,
6029 NULL /* breakpoint_ops */,
6030 from_tty,
6031 1 /* enabled */);
6032 }
6033
6034
6035 void
6036 set_breakpoint (struct gdbarch *gdbarch,
6037 char *address, char *condition,
6038 int hardwareflag, int tempflag,
6039 int thread, int ignore_count,
6040 int pending, int enabled)
6041 {
6042 break_command_really (gdbarch,
6043 address, condition, thread,
6044 0 /* condition and thread are valid. */,
6045 tempflag, hardwareflag, 0 /* traceflag */,
6046 ignore_count,
6047 pending
6048 ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
6049 NULL, 0, enabled);
6050 }
6051
6052 /* Adjust SAL to the first instruction past the function prologue.
6053 The end of the prologue is determined using the line table from
6054 the debugging information. explicit_pc and explicit_line are
6055 not modified.
6056
6057 If SAL is already past the prologue, then do nothing. */
6058
6059 static void
6060 skip_prologue_sal (struct symtab_and_line *sal)
6061 {
6062 struct symbol *sym = find_pc_function (sal->pc);
6063 struct symtab_and_line start_sal;
6064
6065 if (sym == NULL)
6066 return;
6067
6068 start_sal = find_function_start_sal (sym, 1);
6069 if (sal->pc < start_sal.pc)
6070 {
6071 start_sal.explicit_line = sal->explicit_line;
6072 start_sal.explicit_pc = sal->explicit_pc;
6073 *sal = start_sal;
6074 }
6075 }
6076
6077 /* Helper function for break_command_1 and disassemble_command. */
6078
6079 void
6080 resolve_sal_pc (struct symtab_and_line *sal)
6081 {
6082 CORE_ADDR pc;
6083
6084 if (sal->pc == 0 && sal->symtab != NULL)
6085 {
6086 if (!find_line_pc (sal->symtab, sal->line, &pc))
6087 error (_("No line %d in file \"%s\"."),
6088 sal->line, sal->symtab->filename);
6089 sal->pc = pc;
6090
6091 /* If this SAL corresponds to a breakpoint inserted using
6092 a line number, then skip the function prologue if necessary. */
6093 if (sal->explicit_line)
6094 {
6095 /* Preserve the original line number. */
6096 int saved_line = sal->line;
6097 skip_prologue_sal (sal);
6098 sal->line = saved_line;
6099 }
6100 }
6101
6102 if (sal->section == 0 && sal->symtab != NULL)
6103 {
6104 struct blockvector *bv;
6105 struct block *b;
6106 struct symbol *sym;
6107
6108 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
6109 if (bv != NULL)
6110 {
6111 sym = block_linkage_function (b);
6112 if (sym != NULL)
6113 {
6114 fixup_symbol_section (sym, sal->symtab->objfile);
6115 sal->section = SYMBOL_OBJ_SECTION (sym);
6116 }
6117 else
6118 {
6119 /* It really is worthwhile to have the section, so we'll just
6120 have to look harder. This case can be executed if we have
6121 line numbers but no functions (as can happen in assembly
6122 source). */
6123
6124 struct minimal_symbol *msym;
6125
6126 msym = lookup_minimal_symbol_by_pc (sal->pc);
6127 if (msym)
6128 sal->section = SYMBOL_OBJ_SECTION (msym);
6129 }
6130 }
6131 }
6132 }
6133
6134 void
6135 break_command (char *arg, int from_tty)
6136 {
6137 break_command_1 (arg, 0, from_tty);
6138 }
6139
6140 void
6141 tbreak_command (char *arg, int from_tty)
6142 {
6143 break_command_1 (arg, BP_TEMPFLAG, from_tty);
6144 }
6145
6146 static void
6147 hbreak_command (char *arg, int from_tty)
6148 {
6149 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
6150 }
6151
6152 static void
6153 thbreak_command (char *arg, int from_tty)
6154 {
6155 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
6156 }
6157
6158 static void
6159 stop_command (char *arg, int from_tty)
6160 {
6161 printf_filtered (_("Specify the type of breakpoint to set.\n\
6162 Usage: stop in <function | address>\n\
6163 stop at <line>\n"));
6164 }
6165
6166 static void
6167 stopin_command (char *arg, int from_tty)
6168 {
6169 int badInput = 0;
6170
6171 if (arg == (char *) NULL)
6172 badInput = 1;
6173 else if (*arg != '*')
6174 {
6175 char *argptr = arg;
6176 int hasColon = 0;
6177
6178 /* look for a ':'. If this is a line number specification, then
6179 say it is bad, otherwise, it should be an address or
6180 function/method name */
6181 while (*argptr && !hasColon)
6182 {
6183 hasColon = (*argptr == ':');
6184 argptr++;
6185 }
6186
6187 if (hasColon)
6188 badInput = (*argptr != ':'); /* Not a class::method */
6189 else
6190 badInput = isdigit (*arg); /* a simple line number */
6191 }
6192
6193 if (badInput)
6194 printf_filtered (_("Usage: stop in <function | address>\n"));
6195 else
6196 break_command_1 (arg, 0, from_tty);
6197 }
6198
6199 static void
6200 stopat_command (char *arg, int from_tty)
6201 {
6202 int badInput = 0;
6203
6204 if (arg == (char *) NULL || *arg == '*') /* no line number */
6205 badInput = 1;
6206 else
6207 {
6208 char *argptr = arg;
6209 int hasColon = 0;
6210
6211 /* look for a ':'. If there is a '::' then get out, otherwise
6212 it is probably a line number. */
6213 while (*argptr && !hasColon)
6214 {
6215 hasColon = (*argptr == ':');
6216 argptr++;
6217 }
6218
6219 if (hasColon)
6220 badInput = (*argptr == ':'); /* we have class::method */
6221 else
6222 badInput = !isdigit (*arg); /* not a line number */
6223 }
6224
6225 if (badInput)
6226 printf_filtered (_("Usage: stop at <line>\n"));
6227 else
6228 break_command_1 (arg, 0, from_tty);
6229 }
6230
6231 /* accessflag: hw_write: watch write,
6232 hw_read: watch read,
6233 hw_access: watch access (read or write) */
6234 static void
6235 watch_command_1 (char *arg, int accessflag, int from_tty)
6236 {
6237 struct gdbarch *gdbarch = get_current_arch ();
6238 struct breakpoint *b, *scope_breakpoint = NULL;
6239 struct symtab_and_line sal;
6240 struct expression *exp;
6241 struct block *exp_valid_block;
6242 struct value *val, *mark;
6243 struct frame_info *frame;
6244 char *exp_start = NULL;
6245 char *exp_end = NULL;
6246 char *tok, *id_tok_start, *end_tok;
6247 int toklen;
6248 char *cond_start = NULL;
6249 char *cond_end = NULL;
6250 struct expression *cond = NULL;
6251 int i, other_type_used, target_resources_ok = 0;
6252 enum bptype bp_type;
6253 int mem_cnt = 0;
6254 int thread = -1;
6255
6256 init_sal (&sal); /* initialize to zeroes */
6257
6258 /* Make sure that we actually have parameters to parse. */
6259 if (arg != NULL && arg[0] != '\0')
6260 {
6261 toklen = strlen (arg); /* Size of argument list. */
6262
6263 /* Points tok to the end of the argument list. */
6264 tok = arg + toklen - 1;
6265
6266 /* Go backwards in the parameters list. Skip the last parameter.
6267 If we're expecting a 'thread <thread_num>' parameter, this should
6268 be the thread identifier. */
6269 while (tok > arg && (*tok == ' ' || *tok == '\t'))
6270 tok--;
6271 while (tok > arg && (*tok != ' ' && *tok != '\t'))
6272 tok--;
6273
6274 /* Points end_tok to the beginning of the last token. */
6275 id_tok_start = tok + 1;
6276
6277 /* Go backwards in the parameters list. Skip one more parameter.
6278 If we're expecting a 'thread <thread_num>' parameter, we should
6279 reach a "thread" token. */
6280 while (tok > arg && (*tok == ' ' || *tok == '\t'))
6281 tok--;
6282
6283 end_tok = tok;
6284
6285 while (tok > arg && (*tok != ' ' && *tok != '\t'))
6286 tok--;
6287
6288 /* Move the pointer forward to skip the whitespace and
6289 calculate the length of the token. */
6290 tok++;
6291 toklen = end_tok - tok;
6292
6293 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
6294 {
6295 /* At this point we've found a "thread" token, which means
6296 the user is trying to set a watchpoint that triggers
6297 only in a specific thread. */
6298 char *endp;
6299
6300 /* Extract the thread ID from the next token. */
6301 thread = strtol (id_tok_start, &endp, 0);
6302
6303 /* Check if the user provided a valid numeric value for the
6304 thread ID. */
6305 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
6306 error (_("Invalid thread ID specification %s."), id_tok_start);
6307
6308 /* Check if the thread actually exists. */
6309 if (!valid_thread_id (thread))
6310 error (_("Unknown thread %d."), thread);
6311
6312 /* Truncate the string and get rid of the thread <thread_num>
6313 parameter before the parameter list is parsed by the
6314 evaluate_expression() function. */
6315 *tok = '\0';
6316 }
6317 }
6318
6319 /* Parse the rest of the arguments. */
6320 innermost_block = NULL;
6321 exp_start = arg;
6322 exp = parse_exp_1 (&arg, 0, 0);
6323 exp_end = arg;
6324 /* Remove trailing whitespace from the expression before saving it.
6325 This makes the eventual display of the expression string a bit
6326 prettier. */
6327 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
6328 --exp_end;
6329
6330 exp_valid_block = innermost_block;
6331 mark = value_mark ();
6332 fetch_watchpoint_value (exp, &val, NULL, NULL);
6333 if (val != NULL)
6334 release_value (val);
6335
6336 tok = arg;
6337 while (*tok == ' ' || *tok == '\t')
6338 tok++;
6339 end_tok = tok;
6340
6341 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
6342 end_tok++;
6343
6344 toklen = end_tok - tok;
6345 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
6346 {
6347 tok = cond_start = end_tok + 1;
6348 cond = parse_exp_1 (&tok, 0, 0);
6349 cond_end = tok;
6350 }
6351 if (*tok)
6352 error (_("Junk at end of command."));
6353
6354 if (accessflag == hw_read)
6355 bp_type = bp_read_watchpoint;
6356 else if (accessflag == hw_access)
6357 bp_type = bp_access_watchpoint;
6358 else
6359 bp_type = bp_hardware_watchpoint;
6360
6361 mem_cnt = can_use_hardware_watchpoint (val);
6362 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
6363 error (_("Expression cannot be implemented with read/access watchpoint."));
6364 if (mem_cnt != 0)
6365 {
6366 i = hw_watchpoint_used_count (bp_type, &other_type_used);
6367 target_resources_ok =
6368 target_can_use_hardware_watchpoint (bp_type, i + mem_cnt,
6369 other_type_used);
6370 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
6371 error (_("Target does not support this type of hardware watchpoint."));
6372
6373 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
6374 error (_("Target can only support one kind of HW watchpoint at a time."));
6375 }
6376
6377 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
6378 watchpoint could not be set. */
6379 if (!mem_cnt || target_resources_ok <= 0)
6380 bp_type = bp_watchpoint;
6381
6382 frame = block_innermost_frame (exp_valid_block);
6383
6384 /* If the expression is "local", then set up a "watchpoint scope"
6385 breakpoint at the point where we've left the scope of the watchpoint
6386 expression. Create the scope breakpoint before the watchpoint, so
6387 that we will encounter it first in bpstat_stop_status. */
6388 if (innermost_block && frame)
6389 {
6390 if (frame_id_p (frame_unwind_caller_id (frame)))
6391 {
6392 scope_breakpoint
6393 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
6394 frame_unwind_caller_pc (frame),
6395 bp_watchpoint_scope);
6396
6397 scope_breakpoint->enable_state = bp_enabled;
6398
6399 /* Automatically delete the breakpoint when it hits. */
6400 scope_breakpoint->disposition = disp_del;
6401
6402 /* Only break in the proper frame (help with recursion). */
6403 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
6404
6405 /* Set the address at which we will stop. */
6406 scope_breakpoint->loc->gdbarch
6407 = frame_unwind_caller_arch (frame);
6408 scope_breakpoint->loc->requested_address
6409 = frame_unwind_caller_pc (frame);
6410 scope_breakpoint->loc->address
6411 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
6412 scope_breakpoint->loc->requested_address,
6413 scope_breakpoint->type);
6414 }
6415 }
6416
6417 /* Now set up the breakpoint. */
6418 b = set_raw_breakpoint (gdbarch, sal, bp_type);
6419 set_breakpoint_count (breakpoint_count + 1);
6420 b->number = breakpoint_count;
6421 b->thread = thread;
6422 b->disposition = disp_donttouch;
6423 b->exp = exp;
6424 b->exp_valid_block = exp_valid_block;
6425 b->exp_string = savestring (exp_start, exp_end - exp_start);
6426 b->val = val;
6427 b->val_valid = 1;
6428 b->loc->cond = cond;
6429 if (cond_start)
6430 b->cond_string = savestring (cond_start, cond_end - cond_start);
6431 else
6432 b->cond_string = 0;
6433
6434 if (frame)
6435 b->watchpoint_frame = get_frame_id (frame);
6436 else
6437 b->watchpoint_frame = null_frame_id;
6438
6439 if (scope_breakpoint != NULL)
6440 {
6441 /* The scope breakpoint is related to the watchpoint. We will
6442 need to act on them together. */
6443 b->related_breakpoint = scope_breakpoint;
6444 scope_breakpoint->related_breakpoint = b;
6445 }
6446
6447 value_free_to_mark (mark);
6448 mention (b);
6449 update_global_location_list (1);
6450 }
6451
6452 /* Return count of locations need to be watched and can be handled
6453 in hardware. If the watchpoint can not be handled
6454 in hardware return zero. */
6455
6456 static int
6457 can_use_hardware_watchpoint (struct value *v)
6458 {
6459 int found_memory_cnt = 0;
6460 struct value *head = v;
6461
6462 /* Did the user specifically forbid us to use hardware watchpoints? */
6463 if (!can_use_hw_watchpoints)
6464 return 0;
6465
6466 /* Make sure that the value of the expression depends only upon
6467 memory contents, and values computed from them within GDB. If we
6468 find any register references or function calls, we can't use a
6469 hardware watchpoint.
6470
6471 The idea here is that evaluating an expression generates a series
6472 of values, one holding the value of every subexpression. (The
6473 expression a*b+c has five subexpressions: a, b, a*b, c, and
6474 a*b+c.) GDB's values hold almost enough information to establish
6475 the criteria given above --- they identify memory lvalues,
6476 register lvalues, computed values, etcetera. So we can evaluate
6477 the expression, and then scan the chain of values that leaves
6478 behind to decide whether we can detect any possible change to the
6479 expression's final value using only hardware watchpoints.
6480
6481 However, I don't think that the values returned by inferior
6482 function calls are special in any way. So this function may not
6483 notice that an expression involving an inferior function call
6484 can't be watched with hardware watchpoints. FIXME. */
6485 for (; v; v = value_next (v))
6486 {
6487 if (VALUE_LVAL (v) == lval_memory)
6488 {
6489 if (value_lazy (v))
6490 /* A lazy memory lvalue is one that GDB never needed to fetch;
6491 we either just used its address (e.g., `a' in `a.b') or
6492 we never needed it at all (e.g., `a' in `a,b'). */
6493 ;
6494 else
6495 {
6496 /* Ahh, memory we actually used! Check if we can cover
6497 it with hardware watchpoints. */
6498 struct type *vtype = check_typedef (value_type (v));
6499
6500 /* We only watch structs and arrays if user asked for it
6501 explicitly, never if they just happen to appear in a
6502 middle of some value chain. */
6503 if (v == head
6504 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
6505 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
6506 {
6507 CORE_ADDR vaddr = value_address (v);
6508 int len = TYPE_LENGTH (value_type (v));
6509
6510 if (!target_region_ok_for_hw_watchpoint (vaddr, len))
6511 return 0;
6512 else
6513 found_memory_cnt++;
6514 }
6515 }
6516 }
6517 else if (VALUE_LVAL (v) != not_lval
6518 && deprecated_value_modifiable (v) == 0)
6519 return 0; /* ??? What does this represent? */
6520 else if (VALUE_LVAL (v) == lval_register)
6521 return 0; /* cannot watch a register with a HW watchpoint */
6522 }
6523
6524 /* The expression itself looks suitable for using a hardware
6525 watchpoint, but give the target machine a chance to reject it. */
6526 return found_memory_cnt;
6527 }
6528
6529 void
6530 watch_command_wrapper (char *arg, int from_tty)
6531 {
6532 watch_command (arg, from_tty);
6533 }
6534
6535 static void
6536 watch_command (char *arg, int from_tty)
6537 {
6538 watch_command_1 (arg, hw_write, from_tty);
6539 }
6540
6541 void
6542 rwatch_command_wrapper (char *arg, int from_tty)
6543 {
6544 rwatch_command (arg, from_tty);
6545 }
6546
6547 static void
6548 rwatch_command (char *arg, int from_tty)
6549 {
6550 watch_command_1 (arg, hw_read, from_tty);
6551 }
6552
6553 void
6554 awatch_command_wrapper (char *arg, int from_tty)
6555 {
6556 awatch_command (arg, from_tty);
6557 }
6558
6559 static void
6560 awatch_command (char *arg, int from_tty)
6561 {
6562 watch_command_1 (arg, hw_access, from_tty);
6563 }
6564 \f
6565
6566 /* Helper routines for the until_command routine in infcmd.c. Here
6567 because it uses the mechanisms of breakpoints. */
6568
6569 struct until_break_command_continuation_args
6570 {
6571 struct breakpoint *breakpoint;
6572 struct breakpoint *breakpoint2;
6573 };
6574
6575 /* This function is called by fetch_inferior_event via the
6576 cmd_continuation pointer, to complete the until command. It takes
6577 care of cleaning up the temporary breakpoints set up by the until
6578 command. */
6579 static void
6580 until_break_command_continuation (void *arg)
6581 {
6582 struct until_break_command_continuation_args *a = arg;
6583
6584 delete_breakpoint (a->breakpoint);
6585 if (a->breakpoint2)
6586 delete_breakpoint (a->breakpoint2);
6587 }
6588
6589 void
6590 until_break_command (char *arg, int from_tty, int anywhere)
6591 {
6592 struct symtabs_and_lines sals;
6593 struct symtab_and_line sal;
6594 struct frame_info *frame = get_selected_frame (NULL);
6595 struct breakpoint *breakpoint;
6596 struct breakpoint *breakpoint2 = NULL;
6597 struct cleanup *old_chain;
6598
6599 clear_proceed_status ();
6600
6601 /* Set a breakpoint where the user wants it and at return from
6602 this function */
6603
6604 if (default_breakpoint_valid)
6605 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
6606 default_breakpoint_line, (char ***) NULL, NULL);
6607 else
6608 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
6609 0, (char ***) NULL, NULL);
6610
6611 if (sals.nelts != 1)
6612 error (_("Couldn't get information on specified line."));
6613
6614 sal = sals.sals[0];
6615 xfree (sals.sals); /* malloc'd, so freed */
6616
6617 if (*arg)
6618 error (_("Junk at end of arguments."));
6619
6620 resolve_sal_pc (&sal);
6621
6622 if (anywhere)
6623 /* If the user told us to continue until a specified location,
6624 we don't specify a frame at which we need to stop. */
6625 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
6626 null_frame_id, bp_until);
6627 else
6628 /* Otherwise, specify the selected frame, because we want to stop only
6629 at the very same frame. */
6630 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
6631 get_stack_frame_id (frame),
6632 bp_until);
6633
6634 old_chain = make_cleanup_delete_breakpoint (breakpoint);
6635
6636 /* Keep within the current frame, or in frames called by the current
6637 one. */
6638
6639 if (frame_id_p (frame_unwind_caller_id (frame)))
6640 {
6641 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
6642 sal.pc = frame_unwind_caller_pc (frame);
6643 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
6644 sal,
6645 frame_unwind_caller_id (frame),
6646 bp_until);
6647 make_cleanup_delete_breakpoint (breakpoint2);
6648 }
6649
6650 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
6651
6652 /* If we are running asynchronously, and proceed call above has actually
6653 managed to start the target, arrange for breakpoints to be
6654 deleted when the target stops. Otherwise, we're already stopped and
6655 delete breakpoints via cleanup chain. */
6656
6657 if (target_can_async_p () && is_running (inferior_ptid))
6658 {
6659 struct until_break_command_continuation_args *args;
6660 args = xmalloc (sizeof (*args));
6661
6662 args->breakpoint = breakpoint;
6663 args->breakpoint2 = breakpoint2;
6664
6665 discard_cleanups (old_chain);
6666 add_continuation (inferior_thread (),
6667 until_break_command_continuation, args,
6668 xfree);
6669 }
6670 else
6671 do_cleanups (old_chain);
6672 }
6673
6674 static void
6675 ep_skip_leading_whitespace (char **s)
6676 {
6677 if ((s == NULL) || (*s == NULL))
6678 return;
6679 while (isspace (**s))
6680 *s += 1;
6681 }
6682
6683 /* This function attempts to parse an optional "if <cond>" clause
6684 from the arg string. If one is not found, it returns NULL.
6685
6686 Else, it returns a pointer to the condition string. (It does not
6687 attempt to evaluate the string against a particular block.) And,
6688 it updates arg to point to the first character following the parsed
6689 if clause in the arg string. */
6690
6691 static char *
6692 ep_parse_optional_if_clause (char **arg)
6693 {
6694 char *cond_string;
6695
6696 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
6697 return NULL;
6698
6699 /* Skip the "if" keyword. */
6700 (*arg) += 2;
6701
6702 /* Skip any extra leading whitespace, and record the start of the
6703 condition string. */
6704 ep_skip_leading_whitespace (arg);
6705 cond_string = *arg;
6706
6707 /* Assume that the condition occupies the remainder of the arg string. */
6708 (*arg) += strlen (cond_string);
6709
6710 return cond_string;
6711 }
6712
6713 /* This function attempts to parse an optional filename from the arg
6714 string. If one is not found, it returns NULL.
6715
6716 Else, it returns a pointer to the parsed filename. (This function
6717 makes no attempt to verify that a file of that name exists, or is
6718 accessible.) And, it updates arg to point to the first character
6719 following the parsed filename in the arg string.
6720
6721 Note that clients needing to preserve the returned filename for
6722 future access should copy it to their own buffers. */
6723 static char *
6724 ep_parse_optional_filename (char **arg)
6725 {
6726 static char filename[1024];
6727 char *arg_p = *arg;
6728 int i;
6729 char c;
6730
6731 if ((*arg_p == '\0') || isspace (*arg_p))
6732 return NULL;
6733
6734 for (i = 0;; i++)
6735 {
6736 c = *arg_p;
6737 if (isspace (c))
6738 c = '\0';
6739 filename[i] = c;
6740 if (c == '\0')
6741 break;
6742 arg_p++;
6743 }
6744 *arg = arg_p;
6745
6746 return filename;
6747 }
6748
6749 /* Commands to deal with catching events, such as signals, exceptions,
6750 process start/exit, etc. */
6751
6752 typedef enum
6753 {
6754 catch_fork_temporary, catch_vfork_temporary,
6755 catch_fork_permanent, catch_vfork_permanent
6756 }
6757 catch_fork_kind;
6758
6759 static void
6760 catch_fork_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
6761 {
6762 struct gdbarch *gdbarch = get_current_arch ();
6763 char *cond_string = NULL;
6764 catch_fork_kind fork_kind;
6765 int tempflag;
6766
6767 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
6768 tempflag = (fork_kind == catch_fork_temporary
6769 || fork_kind == catch_vfork_temporary);
6770
6771 if (!arg)
6772 arg = "";
6773 ep_skip_leading_whitespace (&arg);
6774
6775 /* The allowed syntax is:
6776 catch [v]fork
6777 catch [v]fork if <cond>
6778
6779 First, check if there's an if clause. */
6780 cond_string = ep_parse_optional_if_clause (&arg);
6781
6782 if ((*arg != '\0') && !isspace (*arg))
6783 error (_("Junk at end of arguments."));
6784
6785 /* If this target supports it, create a fork or vfork catchpoint
6786 and enable reporting of such events. */
6787 switch (fork_kind)
6788 {
6789 case catch_fork_temporary:
6790 case catch_fork_permanent:
6791 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
6792 &catch_fork_breakpoint_ops);
6793 break;
6794 case catch_vfork_temporary:
6795 case catch_vfork_permanent:
6796 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
6797 &catch_vfork_breakpoint_ops);
6798 break;
6799 default:
6800 error (_("unsupported or unknown fork kind; cannot catch it"));
6801 break;
6802 }
6803 }
6804
6805 static void
6806 catch_exec_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
6807 {
6808 struct gdbarch *gdbarch = get_current_arch ();
6809 int tempflag;
6810 char *cond_string = NULL;
6811
6812 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6813
6814 if (!arg)
6815 arg = "";
6816 ep_skip_leading_whitespace (&arg);
6817
6818 /* The allowed syntax is:
6819 catch exec
6820 catch exec if <cond>
6821
6822 First, check if there's an if clause. */
6823 cond_string = ep_parse_optional_if_clause (&arg);
6824
6825 if ((*arg != '\0') && !isspace (*arg))
6826 error (_("Junk at end of arguments."));
6827
6828 /* If this target supports it, create an exec catchpoint
6829 and enable reporting of such events. */
6830 create_catchpoint (gdbarch, tempflag, cond_string,
6831 &catch_exec_breakpoint_ops);
6832 }
6833
6834 static enum print_stop_action
6835 print_exception_catchpoint (struct breakpoint *b)
6836 {
6837 int bp_temp, bp_throw;
6838
6839 annotate_catchpoint (b->number);
6840
6841 bp_throw = strstr (b->addr_string, "throw") != NULL;
6842 if (b->loc->address != b->loc->requested_address)
6843 breakpoint_adjustment_warning (b->loc->requested_address,
6844 b->loc->address,
6845 b->number, 1);
6846 bp_temp = b->disposition == disp_del;
6847 ui_out_text (uiout,
6848 bp_temp ? "Temporary catchpoint "
6849 : "Catchpoint ");
6850 if (!ui_out_is_mi_like_p (uiout))
6851 ui_out_field_int (uiout, "bkptno", b->number);
6852 ui_out_text (uiout,
6853 bp_throw ? " (exception thrown), "
6854 : " (exception caught), ");
6855 if (ui_out_is_mi_like_p (uiout))
6856 {
6857 ui_out_field_string (uiout, "reason",
6858 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
6859 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6860 ui_out_field_int (uiout, "bkptno", b->number);
6861 }
6862 return PRINT_SRC_AND_LOC;
6863 }
6864
6865 static void
6866 print_one_exception_catchpoint (struct breakpoint *b, struct bp_location **last_loc)
6867 {
6868 struct value_print_options opts;
6869 get_user_print_options (&opts);
6870 if (opts.addressprint)
6871 {
6872 annotate_field (4);
6873 if (b->loc == NULL || b->loc->shlib_disabled)
6874 ui_out_field_string (uiout, "addr", "<PENDING>");
6875 else
6876 ui_out_field_core_addr (uiout, "addr",
6877 b->loc->gdbarch, b->loc->address);
6878 }
6879 annotate_field (5);
6880 if (b->loc)
6881 *last_loc = b->loc;
6882 if (strstr (b->addr_string, "throw") != NULL)
6883 ui_out_field_string (uiout, "what", "exception throw");
6884 else
6885 ui_out_field_string (uiout, "what", "exception catch");
6886 }
6887
6888 static void
6889 print_mention_exception_catchpoint (struct breakpoint *b)
6890 {
6891 int bp_temp;
6892 int bp_throw;
6893
6894 bp_temp = b->disposition == disp_del;
6895 bp_throw = strstr (b->addr_string, "throw") != NULL;
6896 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
6897 : _("Catchpoint "));
6898 ui_out_field_int (uiout, "bkptno", b->number);
6899 ui_out_text (uiout, bp_throw ? _(" (throw)")
6900 : _(" (catch)"));
6901 }
6902
6903 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
6904 NULL, /* insert */
6905 NULL, /* remove */
6906 NULL, /* breakpoint_hit */
6907 print_exception_catchpoint,
6908 print_one_exception_catchpoint,
6909 print_mention_exception_catchpoint
6910 };
6911
6912 static int
6913 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
6914 enum exception_event_kind ex_event, int from_tty)
6915 {
6916 char *trigger_func_name;
6917
6918 if (ex_event == EX_EVENT_CATCH)
6919 trigger_func_name = "__cxa_begin_catch";
6920 else
6921 trigger_func_name = "__cxa_throw";
6922
6923 break_command_really (get_current_arch (),
6924 trigger_func_name, cond_string, -1,
6925 0 /* condition and thread are valid. */,
6926 tempflag, 0, 0,
6927 0,
6928 AUTO_BOOLEAN_TRUE /* pending */,
6929 &gnu_v3_exception_catchpoint_ops, from_tty,
6930 1 /* enabled */);
6931
6932 return 1;
6933 }
6934
6935 /* Deal with "catch catch" and "catch throw" commands */
6936
6937 static void
6938 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
6939 int tempflag, int from_tty)
6940 {
6941 char *cond_string = NULL;
6942 struct symtab_and_line *sal = NULL;
6943
6944 if (!arg)
6945 arg = "";
6946 ep_skip_leading_whitespace (&arg);
6947
6948 cond_string = ep_parse_optional_if_clause (&arg);
6949
6950 if ((*arg != '\0') && !isspace (*arg))
6951 error (_("Junk at end of arguments."));
6952
6953 if (ex_event != EX_EVENT_THROW
6954 && ex_event != EX_EVENT_CATCH)
6955 error (_("Unsupported or unknown exception event; cannot catch it"));
6956
6957 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
6958 return;
6959
6960 warning (_("Unsupported with this platform/compiler combination."));
6961 }
6962
6963 /* Implementation of "catch catch" command. */
6964
6965 static void
6966 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
6967 {
6968 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6969 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
6970 }
6971
6972 /* Implementation of "catch throw" command. */
6973
6974 static void
6975 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
6976 {
6977 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6978 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
6979 }
6980
6981 /* Create a breakpoint struct for Ada exception catchpoints. */
6982
6983 static void
6984 create_ada_exception_breakpoint (struct gdbarch *gdbarch,
6985 struct symtab_and_line sal,
6986 char *addr_string,
6987 char *exp_string,
6988 char *cond_string,
6989 struct expression *cond,
6990 struct breakpoint_ops *ops,
6991 int tempflag,
6992 int from_tty)
6993 {
6994 struct breakpoint *b;
6995
6996 if (from_tty)
6997 {
6998 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
6999 if (!loc_gdbarch)
7000 loc_gdbarch = gdbarch;
7001
7002 describe_other_breakpoints (loc_gdbarch, sal.pc, sal.section, -1);
7003 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
7004 version for exception catchpoints, because two catchpoints
7005 used for different exception names will use the same address.
7006 In this case, a "breakpoint ... also set at..." warning is
7007 unproductive. Besides. the warning phrasing is also a bit
7008 inapropriate, we should use the word catchpoint, and tell
7009 the user what type of catchpoint it is. The above is good
7010 enough for now, though. */
7011 }
7012
7013 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
7014 set_breakpoint_count (breakpoint_count + 1);
7015
7016 b->enable_state = bp_enabled;
7017 b->disposition = tempflag ? disp_del : disp_donttouch;
7018 b->number = breakpoint_count;
7019 b->ignore_count = 0;
7020 b->loc->cond = cond;
7021 b->addr_string = addr_string;
7022 b->language = language_ada;
7023 b->cond_string = cond_string;
7024 b->exp_string = exp_string;
7025 b->thread = -1;
7026 b->ops = ops;
7027
7028 mention (b);
7029 update_global_location_list (1);
7030 }
7031
7032 /* Implement the "catch exception" command. */
7033
7034 static void
7035 catch_ada_exception_command (char *arg, int from_tty,
7036 struct cmd_list_element *command)
7037 {
7038 struct gdbarch *gdbarch = get_current_arch ();
7039 int tempflag;
7040 struct symtab_and_line sal;
7041 enum bptype type;
7042 char *addr_string = NULL;
7043 char *exp_string = NULL;
7044 char *cond_string = NULL;
7045 struct expression *cond = NULL;
7046 struct breakpoint_ops *ops = NULL;
7047
7048 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7049
7050 if (!arg)
7051 arg = "";
7052 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
7053 &cond_string, &cond, &ops);
7054 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
7055 cond_string, cond, ops, tempflag,
7056 from_tty);
7057 }
7058
7059 /* Implement the "catch assert" command. */
7060
7061 static void
7062 catch_assert_command (char *arg, int from_tty, struct cmd_list_element *command)
7063 {
7064 struct gdbarch *gdbarch = get_current_arch ();
7065 int tempflag;
7066 struct symtab_and_line sal;
7067 char *addr_string = NULL;
7068 struct breakpoint_ops *ops = NULL;
7069
7070 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7071
7072 if (!arg)
7073 arg = "";
7074 sal = ada_decode_assert_location (arg, &addr_string, &ops);
7075 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
7076 ops, tempflag, from_tty);
7077 }
7078
7079 static void
7080 catch_command (char *arg, int from_tty)
7081 {
7082 error (_("Catch requires an event name."));
7083 }
7084 \f
7085
7086 static void
7087 tcatch_command (char *arg, int from_tty)
7088 {
7089 error (_("Catch requires an event name."));
7090 }
7091
7092 /* Delete breakpoints by address or line. */
7093
7094 static void
7095 clear_command (char *arg, int from_tty)
7096 {
7097 struct breakpoint *b;
7098 VEC(breakpoint_p) *found = 0;
7099 int ix;
7100 int default_match;
7101 struct symtabs_and_lines sals;
7102 struct symtab_and_line sal;
7103 int i;
7104
7105 if (arg)
7106 {
7107 sals = decode_line_spec (arg, 1);
7108 default_match = 0;
7109 }
7110 else
7111 {
7112 sals.sals = (struct symtab_and_line *)
7113 xmalloc (sizeof (struct symtab_and_line));
7114 make_cleanup (xfree, sals.sals);
7115 init_sal (&sal); /* initialize to zeroes */
7116 sal.line = default_breakpoint_line;
7117 sal.symtab = default_breakpoint_symtab;
7118 sal.pc = default_breakpoint_address;
7119 if (sal.symtab == 0)
7120 error (_("No source file specified."));
7121
7122 sals.sals[0] = sal;
7123 sals.nelts = 1;
7124
7125 default_match = 1;
7126 }
7127
7128 /* We don't call resolve_sal_pc here. That's not
7129 as bad as it seems, because all existing breakpoints
7130 typically have both file/line and pc set. So, if
7131 clear is given file/line, we can match this to existing
7132 breakpoint without obtaining pc at all.
7133
7134 We only support clearing given the address explicitly
7135 present in breakpoint table. Say, we've set breakpoint
7136 at file:line. There were several PC values for that file:line,
7137 due to optimization, all in one block.
7138 We've picked one PC value. If "clear" is issued with another
7139 PC corresponding to the same file:line, the breakpoint won't
7140 be cleared. We probably can still clear the breakpoint, but
7141 since the other PC value is never presented to user, user
7142 can only find it by guessing, and it does not seem important
7143 to support that. */
7144
7145 /* For each line spec given, delete bps which correspond
7146 to it. Do it in two passes, solely to preserve the current
7147 behavior that from_tty is forced true if we delete more than
7148 one breakpoint. */
7149
7150 found = NULL;
7151 for (i = 0; i < sals.nelts; i++)
7152 {
7153 /* If exact pc given, clear bpts at that pc.
7154 If line given (pc == 0), clear all bpts on specified line.
7155 If defaulting, clear all bpts on default line
7156 or at default pc.
7157
7158 defaulting sal.pc != 0 tests to do
7159
7160 0 1 pc
7161 1 1 pc _and_ line
7162 0 0 line
7163 1 0 <can't happen> */
7164
7165 sal = sals.sals[i];
7166
7167 /* Find all matching breakpoints and add them to
7168 'found'. */
7169 ALL_BREAKPOINTS (b)
7170 {
7171 int match = 0;
7172 /* Are we going to delete b? */
7173 if (b->type != bp_none
7174 && b->type != bp_watchpoint
7175 && b->type != bp_hardware_watchpoint
7176 && b->type != bp_read_watchpoint
7177 && b->type != bp_access_watchpoint)
7178 {
7179 struct bp_location *loc = b->loc;
7180 for (; loc; loc = loc->next)
7181 {
7182 int pc_match = sal.pc
7183 && (loc->address == sal.pc)
7184 && (!section_is_overlay (loc->section)
7185 || loc->section == sal.section);
7186 int line_match = ((default_match || (0 == sal.pc))
7187 && b->source_file != NULL
7188 && sal.symtab != NULL
7189 && strcmp (b->source_file, sal.symtab->filename) == 0
7190 && b->line_number == sal.line);
7191 if (pc_match || line_match)
7192 {
7193 match = 1;
7194 break;
7195 }
7196 }
7197 }
7198
7199 if (match)
7200 VEC_safe_push(breakpoint_p, found, b);
7201 }
7202 }
7203 /* Now go thru the 'found' chain and delete them. */
7204 if (VEC_empty(breakpoint_p, found))
7205 {
7206 if (arg)
7207 error (_("No breakpoint at %s."), arg);
7208 else
7209 error (_("No breakpoint at this line."));
7210 }
7211
7212 if (VEC_length(breakpoint_p, found) > 1)
7213 from_tty = 1; /* Always report if deleted more than one */
7214 if (from_tty)
7215 {
7216 if (VEC_length(breakpoint_p, found) == 1)
7217 printf_unfiltered (_("Deleted breakpoint "));
7218 else
7219 printf_unfiltered (_("Deleted breakpoints "));
7220 }
7221 breakpoints_changed ();
7222
7223 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
7224 {
7225 if (from_tty)
7226 printf_unfiltered ("%d ", b->number);
7227 delete_breakpoint (b);
7228 }
7229 if (from_tty)
7230 putchar_unfiltered ('\n');
7231 }
7232 \f
7233 /* Delete breakpoint in BS if they are `delete' breakpoints and
7234 all breakpoints that are marked for deletion, whether hit or not.
7235 This is called after any breakpoint is hit, or after errors. */
7236
7237 void
7238 breakpoint_auto_delete (bpstat bs)
7239 {
7240 struct breakpoint *b, *temp;
7241
7242 for (; bs; bs = bs->next)
7243 if (bs->breakpoint_at
7244 && bs->breakpoint_at->owner
7245 && bs->breakpoint_at->owner->disposition == disp_del
7246 && bs->stop)
7247 delete_breakpoint (bs->breakpoint_at->owner);
7248
7249 ALL_BREAKPOINTS_SAFE (b, temp)
7250 {
7251 if (b->disposition == disp_del_at_next_stop)
7252 delete_breakpoint (b);
7253 }
7254 }
7255
7256 /* A cleanup function which destroys a vector. */
7257
7258 static void
7259 do_vec_free (void *p)
7260 {
7261 VEC(bp_location_p) **vec = p;
7262 if (*vec)
7263 VEC_free (bp_location_p, *vec);
7264 }
7265
7266 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
7267 into the inferior, only remove already-inserted locations that no
7268 longer should be inserted. Functions that delete a breakpoint or
7269 breakpoints should pass false, so that deleting a breakpoint
7270 doesn't have the side effect of inserting the locations of other
7271 breakpoints that are marked not-inserted, but should_be_inserted
7272 returns true on them.
7273
7274 This behaviour is useful is situations close to tear-down -- e.g.,
7275 after an exec, while the target still has execution, but breakpoint
7276 shadows of the previous executable image should *NOT* be restored
7277 to the new image; or before detaching, where the target still has
7278 execution and wants to delete breakpoints from GDB's lists, and all
7279 breakpoints had already been removed from the inferior. */
7280
7281 static void
7282 update_global_location_list (int should_insert)
7283 {
7284 struct breakpoint *b;
7285 struct bp_location **next = &bp_location_chain;
7286 struct bp_location *loc;
7287 struct bp_location *loc2;
7288 VEC(bp_location_p) *old_locations = NULL;
7289 int ret;
7290 int ix;
7291 struct cleanup *cleanups;
7292
7293 cleanups = make_cleanup (do_vec_free, &old_locations);
7294 /* Store old locations for future reference. */
7295 for (loc = bp_location_chain; loc; loc = loc->global_next)
7296 VEC_safe_push (bp_location_p, old_locations, loc);
7297
7298 bp_location_chain = NULL;
7299 ALL_BREAKPOINTS (b)
7300 {
7301 for (loc = b->loc; loc; loc = loc->next)
7302 {
7303 *next = loc;
7304 next = &(loc->global_next);
7305 *next = NULL;
7306 }
7307 }
7308
7309 /* Identify bp_location instances that are no longer present in the new
7310 list, and therefore should be freed. Note that it's not necessary that
7311 those locations should be removed from inferior -- if there's another
7312 location at the same address (previously marked as duplicate),
7313 we don't need to remove/insert the location. */
7314 for (ix = 0; VEC_iterate(bp_location_p, old_locations, ix, loc); ++ix)
7315 {
7316 /* Tells if 'loc' is found amoung the new locations. If not, we
7317 have to free it. */
7318 int found_object = 0;
7319 /* Tells if the location should remain inserted in the target. */
7320 int keep_in_target = 0;
7321 int removed = 0;
7322 for (loc2 = bp_location_chain; loc2; loc2 = loc2->global_next)
7323 if (loc2 == loc)
7324 {
7325 found_object = 1;
7326 break;
7327 }
7328
7329 /* If this location is no longer present, and inserted, look if there's
7330 maybe a new location at the same address. If so, mark that one
7331 inserted, and don't remove this one. This is needed so that we
7332 don't have a time window where a breakpoint at certain location is not
7333 inserted. */
7334
7335 if (loc->inserted)
7336 {
7337 /* If the location is inserted now, we might have to remove it. */
7338
7339 if (found_object && should_be_inserted (loc))
7340 {
7341 /* The location is still present in the location list, and still
7342 should be inserted. Don't do anything. */
7343 keep_in_target = 1;
7344 }
7345 else
7346 {
7347 /* The location is either no longer present, or got disabled.
7348 See if there's another location at the same address, in which
7349 case we don't need to remove this one from the target. */
7350 if (breakpoint_address_is_meaningful (loc->owner))
7351 for (loc2 = bp_location_chain; loc2; loc2 = loc2->global_next)
7352 {
7353 /* For the sake of should_insert_location. The
7354 call to check_duplicates will fix up this later. */
7355 loc2->duplicate = 0;
7356 if (should_be_inserted (loc2)
7357 && loc2 != loc && loc2->address == loc->address)
7358 {
7359 loc2->inserted = 1;
7360 loc2->target_info = loc->target_info;
7361 keep_in_target = 1;
7362 break;
7363 }
7364 }
7365 }
7366
7367 if (!keep_in_target)
7368 {
7369 if (remove_breakpoint (loc, mark_uninserted))
7370 {
7371 /* This is just about all we can do. We could keep this
7372 location on the global list, and try to remove it next
7373 time, but there's no particular reason why we will
7374 succeed next time.
7375
7376 Note that at this point, loc->owner is still valid,
7377 as delete_breakpoint frees the breakpoint only
7378 after calling us. */
7379 printf_filtered (_("warning: Error removing breakpoint %d\n"),
7380 loc->owner->number);
7381 }
7382 removed = 1;
7383 }
7384 }
7385
7386 if (!found_object)
7387 {
7388 if (removed && non_stop)
7389 {
7390 /* This location was removed from the targets. In non-stop mode,
7391 a race condition is possible where we've removed a breakpoint,
7392 but stop events for that breakpoint are already queued and will
7393 arrive later. To suppress spurious SIGTRAPs reported to user,
7394 we keep this breakpoint location for a bit, and will retire it
7395 after we see 3 * thread_count events.
7396 The theory here is that reporting of events should,
7397 "on the average", be fair, so after that many event we'll see
7398 events from all threads that have anything of interest, and no
7399 longer need to keep this breakpoint. This is just a
7400 heuristic, but if it's wrong, we'll report unexpected SIGTRAP,
7401 which is usability issue, but not a correctness problem. */
7402 loc->events_till_retirement = 3 * (thread_count () + 1);
7403 loc->owner = NULL;
7404
7405 VEC_safe_push (bp_location_p, moribund_locations, loc);
7406 }
7407 else
7408 free_bp_location (loc);
7409 }
7410 }
7411
7412 ALL_BREAKPOINTS (b)
7413 {
7414 check_duplicates (b);
7415 }
7416
7417 if (breakpoints_always_inserted_mode () && should_insert
7418 && (have_live_inferiors ()
7419 || (gdbarch_has_global_breakpoints (target_gdbarch))))
7420 insert_breakpoint_locations ();
7421
7422 do_cleanups (cleanups);
7423 }
7424
7425 void
7426 breakpoint_retire_moribund (void)
7427 {
7428 struct bp_location *loc;
7429 int ix;
7430
7431 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
7432 if (--(loc->events_till_retirement) == 0)
7433 {
7434 free_bp_location (loc);
7435 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
7436 --ix;
7437 }
7438 }
7439
7440 static void
7441 update_global_location_list_nothrow (int inserting)
7442 {
7443 struct gdb_exception e;
7444 TRY_CATCH (e, RETURN_MASK_ERROR)
7445 update_global_location_list (inserting);
7446 }
7447
7448 /* Clear BPT from a BPS. */
7449 static void
7450 bpstat_remove_breakpoint (bpstat bps, struct breakpoint *bpt)
7451 {
7452 bpstat bs;
7453 for (bs = bps; bs; bs = bs->next)
7454 if (bs->breakpoint_at && bs->breakpoint_at->owner == bpt)
7455 {
7456 bs->breakpoint_at = NULL;
7457 bs->old_val = NULL;
7458 /* bs->commands will be freed later. */
7459 }
7460 }
7461
7462 /* Callback for iterate_over_threads. */
7463 static int
7464 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
7465 {
7466 struct breakpoint *bpt = data;
7467 bpstat_remove_breakpoint (th->stop_bpstat, bpt);
7468 return 0;
7469 }
7470
7471 /* Delete a breakpoint and clean up all traces of it in the data
7472 structures. */
7473
7474 void
7475 delete_breakpoint (struct breakpoint *bpt)
7476 {
7477 struct breakpoint *b;
7478 struct bp_location *loc, *next;
7479
7480 gdb_assert (bpt != NULL);
7481
7482 /* Has this bp already been deleted? This can happen because multiple
7483 lists can hold pointers to bp's. bpstat lists are especial culprits.
7484
7485 One example of this happening is a watchpoint's scope bp. When the
7486 scope bp triggers, we notice that the watchpoint is out of scope, and
7487 delete it. We also delete its scope bp. But the scope bp is marked
7488 "auto-deleting", and is already on a bpstat. That bpstat is then
7489 checked for auto-deleting bp's, which are deleted.
7490
7491 A real solution to this problem might involve reference counts in bp's,
7492 and/or giving them pointers back to their referencing bpstat's, and
7493 teaching delete_breakpoint to only free a bp's storage when no more
7494 references were extent. A cheaper bandaid was chosen. */
7495 if (bpt->type == bp_none)
7496 return;
7497
7498 observer_notify_breakpoint_deleted (bpt->number);
7499
7500 if (breakpoint_chain == bpt)
7501 breakpoint_chain = bpt->next;
7502
7503 ALL_BREAKPOINTS (b)
7504 if (b->next == bpt)
7505 {
7506 b->next = bpt->next;
7507 break;
7508 }
7509
7510 free_command_lines (&bpt->commands);
7511 if (bpt->cond_string != NULL)
7512 xfree (bpt->cond_string);
7513 if (bpt->addr_string != NULL)
7514 xfree (bpt->addr_string);
7515 if (bpt->exp != NULL)
7516 xfree (bpt->exp);
7517 if (bpt->exp_string != NULL)
7518 xfree (bpt->exp_string);
7519 if (bpt->val != NULL)
7520 value_free (bpt->val);
7521 if (bpt->source_file != NULL)
7522 xfree (bpt->source_file);
7523 if (bpt->exec_pathname != NULL)
7524 xfree (bpt->exec_pathname);
7525
7526 /* Be sure no bpstat's are pointing at it after it's been freed. */
7527 /* FIXME, how can we find all bpstat's?
7528 We just check stop_bpstat for now. Note that we cannot just
7529 remove bpstats pointing at bpt from the stop_bpstat list
7530 entirely, as breakpoint commands are associated with the bpstat;
7531 if we remove it here, then the later call to
7532 bpstat_do_actions (&stop_bpstat);
7533 in event-top.c won't do anything, and temporary breakpoints
7534 with commands won't work. */
7535
7536 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
7537
7538 /* Now that breakpoint is removed from breakpoint
7539 list, update the global location list. This
7540 will remove locations that used to belong to
7541 this breakpoint. Do this before freeing
7542 the breakpoint itself, since remove_breakpoint
7543 looks at location's owner. It might be better
7544 design to have location completely self-contained,
7545 but it's not the case now. */
7546 update_global_location_list (0);
7547
7548
7549 /* On the chance that someone will soon try again to delete this same
7550 bp, we mark it as deleted before freeing its storage. */
7551 bpt->type = bp_none;
7552
7553 xfree (bpt);
7554 }
7555
7556 static void
7557 do_delete_breakpoint_cleanup (void *b)
7558 {
7559 delete_breakpoint (b);
7560 }
7561
7562 struct cleanup *
7563 make_cleanup_delete_breakpoint (struct breakpoint *b)
7564 {
7565 return make_cleanup (do_delete_breakpoint_cleanup, b);
7566 }
7567
7568 void
7569 delete_command (char *arg, int from_tty)
7570 {
7571 struct breakpoint *b, *temp;
7572
7573 dont_repeat ();
7574
7575 if (arg == 0)
7576 {
7577 int breaks_to_delete = 0;
7578
7579 /* Delete all breakpoints if no argument.
7580 Do not delete internal or call-dummy breakpoints, these
7581 have to be deleted with an explicit breakpoint number argument. */
7582 ALL_BREAKPOINTS (b)
7583 {
7584 if (b->type != bp_call_dummy
7585 && b->type != bp_shlib_event
7586 && b->type != bp_thread_event
7587 && b->type != bp_overlay_event
7588 && b->type != bp_longjmp_master
7589 && b->number >= 0)
7590 {
7591 breaks_to_delete = 1;
7592 break;
7593 }
7594 }
7595
7596 /* Ask user only if there are some breakpoints to delete. */
7597 if (!from_tty
7598 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
7599 {
7600 ALL_BREAKPOINTS_SAFE (b, temp)
7601 {
7602 if (b->type != bp_call_dummy
7603 && b->type != bp_shlib_event
7604 && b->type != bp_thread_event
7605 && b->type != bp_overlay_event
7606 && b->type != bp_longjmp_master
7607 && b->number >= 0)
7608 delete_breakpoint (b);
7609 }
7610 }
7611 }
7612 else
7613 map_breakpoint_numbers (arg, delete_breakpoint);
7614 }
7615
7616 static int
7617 all_locations_are_pending (struct bp_location *loc)
7618 {
7619 for (; loc; loc = loc->next)
7620 if (!loc->shlib_disabled)
7621 return 0;
7622 return 1;
7623 }
7624
7625 /* Subroutine of update_breakpoint_locations to simplify it.
7626 Return non-zero if multiple fns in list LOC have the same name.
7627 Null names are ignored. */
7628
7629 static int
7630 ambiguous_names_p (struct bp_location *loc)
7631 {
7632 struct bp_location *l;
7633 htab_t htab = htab_create_alloc (13, htab_hash_string,
7634 (int (*) (const void *, const void *)) streq,
7635 NULL, xcalloc, xfree);
7636
7637 for (l = loc; l != NULL; l = l->next)
7638 {
7639 const char **slot;
7640 const char *name = l->function_name;
7641
7642 /* Allow for some names to be NULL, ignore them. */
7643 if (name == NULL)
7644 continue;
7645
7646 slot = (const char **) htab_find_slot (htab, (const void *) name,
7647 INSERT);
7648 /* NOTE: We can assume slot != NULL here because xcalloc never returns
7649 NULL. */
7650 if (*slot != NULL)
7651 {
7652 htab_delete (htab);
7653 return 1;
7654 }
7655 *slot = name;
7656 }
7657
7658 htab_delete (htab);
7659 return 0;
7660 }
7661
7662 static void
7663 update_breakpoint_locations (struct breakpoint *b,
7664 struct symtabs_and_lines sals)
7665 {
7666 int i;
7667 char *s;
7668 struct bp_location *existing_locations = b->loc;
7669
7670 /* If there's no new locations, and all existing locations
7671 are pending, don't do anything. This optimizes
7672 the common case where all locations are in the same
7673 shared library, that was unloaded. We'd like to
7674 retain the location, so that when the library
7675 is loaded again, we don't loose the enabled/disabled
7676 status of the individual locations. */
7677 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
7678 return;
7679
7680 b->loc = NULL;
7681
7682 for (i = 0; i < sals.nelts; ++i)
7683 {
7684 struct bp_location *new_loc =
7685 add_location_to_breakpoint (b, &(sals.sals[i]));
7686
7687 /* Reparse conditions, they might contain references to the
7688 old symtab. */
7689 if (b->cond_string != NULL)
7690 {
7691 struct gdb_exception e;
7692
7693 s = b->cond_string;
7694 TRY_CATCH (e, RETURN_MASK_ERROR)
7695 {
7696 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
7697 0);
7698 }
7699 if (e.reason < 0)
7700 {
7701 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
7702 b->number, e.message);
7703 new_loc->enabled = 0;
7704 }
7705 }
7706
7707 if (b->source_file != NULL)
7708 xfree (b->source_file);
7709 if (sals.sals[i].symtab == NULL)
7710 b->source_file = NULL;
7711 else
7712 b->source_file = xstrdup (sals.sals[i].symtab->filename);
7713
7714 if (b->line_number == 0)
7715 b->line_number = sals.sals[i].line;
7716 }
7717
7718 /* Update locations of permanent breakpoints. */
7719 if (b->enable_state == bp_permanent)
7720 make_breakpoint_permanent (b);
7721
7722 /* If possible, carry over 'disable' status from existing breakpoints. */
7723 {
7724 struct bp_location *e = existing_locations;
7725 /* If there are multiple breakpoints with the same function name,
7726 e.g. for inline functions, comparing function names won't work.
7727 Instead compare pc addresses; this is just a heuristic as things
7728 may have moved, but in practice it gives the correct answer
7729 often enough until a better solution is found. */
7730 int have_ambiguous_names = ambiguous_names_p (b->loc);
7731
7732 for (; e; e = e->next)
7733 {
7734 if (!e->enabled && e->function_name)
7735 {
7736 struct bp_location *l = b->loc;
7737 if (have_ambiguous_names)
7738 {
7739 for (; l; l = l->next)
7740 if (e->address == l->address)
7741 {
7742 l->enabled = 0;
7743 break;
7744 }
7745 }
7746 else
7747 {
7748 for (; l; l = l->next)
7749 if (l->function_name
7750 && strcmp (e->function_name, l->function_name) == 0)
7751 {
7752 l->enabled = 0;
7753 break;
7754 }
7755 }
7756 }
7757 }
7758 }
7759
7760 update_global_location_list (1);
7761 }
7762
7763
7764 /* Reset a breakpoint given it's struct breakpoint * BINT.
7765 The value we return ends up being the return value from catch_errors.
7766 Unused in this case. */
7767
7768 static int
7769 breakpoint_re_set_one (void *bint)
7770 {
7771 /* get past catch_errs */
7772 struct breakpoint *b = (struct breakpoint *) bint;
7773 struct value *mark;
7774 int i;
7775 int not_found = 0;
7776 int *not_found_ptr = &not_found;
7777 struct symtabs_and_lines sals = {};
7778 struct symtabs_and_lines expanded;
7779 char *s;
7780 enum enable_state save_enable;
7781 struct gdb_exception e;
7782 struct cleanup *cleanups;
7783
7784 switch (b->type)
7785 {
7786 case bp_none:
7787 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
7788 b->number);
7789 return 0;
7790 case bp_breakpoint:
7791 case bp_hardware_breakpoint:
7792 case bp_tracepoint:
7793 if (b->addr_string == NULL)
7794 {
7795 /* Anything without a string can't be re-set. */
7796 delete_breakpoint (b);
7797 return 0;
7798 }
7799
7800 set_language (b->language);
7801 input_radix = b->input_radix;
7802 s = b->addr_string;
7803 TRY_CATCH (e, RETURN_MASK_ERROR)
7804 {
7805 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
7806 not_found_ptr);
7807 }
7808 if (e.reason < 0)
7809 {
7810 int not_found_and_ok = 0;
7811 /* For pending breakpoints, it's expected that parsing
7812 will fail until the right shared library is loaded.
7813 User has already told to create pending breakpoints and
7814 don't need extra messages. If breakpoint is in bp_shlib_disabled
7815 state, then user already saw the message about that breakpoint
7816 being disabled, and don't want to see more errors. */
7817 if (not_found
7818 && (b->condition_not_parsed
7819 || (b->loc && b->loc->shlib_disabled)
7820 || b->enable_state == bp_disabled))
7821 not_found_and_ok = 1;
7822
7823 if (!not_found_and_ok)
7824 {
7825 /* We surely don't want to warn about the same breakpoint
7826 10 times. One solution, implemented here, is disable
7827 the breakpoint on error. Another solution would be to
7828 have separate 'warning emitted' flag. Since this
7829 happens only when a binary has changed, I don't know
7830 which approach is better. */
7831 b->enable_state = bp_disabled;
7832 throw_exception (e);
7833 }
7834 }
7835
7836 if (not_found)
7837 break;
7838
7839 gdb_assert (sals.nelts == 1);
7840 resolve_sal_pc (&sals.sals[0]);
7841 if (b->condition_not_parsed && s && s[0])
7842 {
7843 char *cond_string = 0;
7844 int thread = -1;
7845 int task = 0;
7846
7847 find_condition_and_thread (s, sals.sals[0].pc,
7848 &cond_string, &thread, &task);
7849 if (cond_string)
7850 b->cond_string = cond_string;
7851 b->thread = thread;
7852 b->task = task;
7853 b->condition_not_parsed = 0;
7854 }
7855 expanded = expand_line_sal_maybe (sals.sals[0]);
7856 cleanups = make_cleanup (xfree, sals.sals);
7857 update_breakpoint_locations (b, expanded);
7858 do_cleanups (cleanups);
7859 break;
7860
7861 case bp_watchpoint:
7862 case bp_hardware_watchpoint:
7863 case bp_read_watchpoint:
7864 case bp_access_watchpoint:
7865 /* Watchpoint can be either on expression using entirely global variables,
7866 or it can be on local variables.
7867
7868 Watchpoints of the first kind are never auto-deleted, and even persist
7869 across program restarts. Since they can use variables from shared
7870 libraries, we need to reparse expression as libraries are loaded
7871 and unloaded.
7872
7873 Watchpoints on local variables can also change meaning as result
7874 of solib event. For example, if a watchpoint uses both a local and
7875 a global variables in expression, it's a local watchpoint, but
7876 unloading of a shared library will make the expression invalid.
7877 This is not a very common use case, but we still re-evaluate
7878 expression, to avoid surprises to the user.
7879
7880 Note that for local watchpoints, we re-evaluate it only if
7881 watchpoints frame id is still valid. If it's not, it means
7882 the watchpoint is out of scope and will be deleted soon. In fact,
7883 I'm not sure we'll ever be called in this case.
7884
7885 If a local watchpoint's frame id is still valid, then
7886 b->exp_valid_block is likewise valid, and we can safely use it.
7887
7888 Don't do anything about disabled watchpoints, since they will
7889 be reevaluated again when enabled. */
7890 update_watchpoint (b, 1 /* reparse */);
7891 break;
7892 /* We needn't really do anything to reset these, since the mask
7893 that requests them is unaffected by e.g., new libraries being
7894 loaded. */
7895 case bp_catchpoint:
7896 break;
7897
7898 default:
7899 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
7900 /* fall through */
7901 /* Delete overlay event and longjmp master breakpoints; they will be
7902 reset later by breakpoint_re_set. */
7903 case bp_overlay_event:
7904 case bp_longjmp_master:
7905 delete_breakpoint (b);
7906 break;
7907
7908 /* This breakpoint is special, it's set up when the inferior
7909 starts and we really don't want to touch it. */
7910 case bp_shlib_event:
7911
7912 /* Like bp_shlib_event, this breakpoint type is special.
7913 Once it is set up, we do not want to touch it. */
7914 case bp_thread_event:
7915
7916 /* Keep temporary breakpoints, which can be encountered when we step
7917 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
7918 Otherwise these should have been blown away via the cleanup chain
7919 or by breakpoint_init_inferior when we rerun the executable. */
7920 case bp_until:
7921 case bp_finish:
7922 case bp_watchpoint_scope:
7923 case bp_call_dummy:
7924 case bp_step_resume:
7925 case bp_longjmp:
7926 case bp_longjmp_resume:
7927 break;
7928 }
7929
7930 return 0;
7931 }
7932
7933 /* Re-set all breakpoints after symbols have been re-loaded. */
7934 void
7935 breakpoint_re_set (void)
7936 {
7937 struct breakpoint *b, *temp;
7938 enum language save_language;
7939 int save_input_radix;
7940
7941 save_language = current_language->la_language;
7942 save_input_radix = input_radix;
7943 ALL_BREAKPOINTS_SAFE (b, temp)
7944 {
7945 /* Format possible error msg */
7946 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
7947 b->number);
7948 struct cleanup *cleanups = make_cleanup (xfree, message);
7949 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
7950 do_cleanups (cleanups);
7951 }
7952 set_language (save_language);
7953 input_radix = save_input_radix;
7954
7955 create_overlay_event_breakpoint ("_ovly_debug_event");
7956 create_longjmp_master_breakpoint ("longjmp");
7957 create_longjmp_master_breakpoint ("_longjmp");
7958 create_longjmp_master_breakpoint ("siglongjmp");
7959 create_longjmp_master_breakpoint ("_siglongjmp");
7960 }
7961 \f
7962 /* Reset the thread number of this breakpoint:
7963
7964 - If the breakpoint is for all threads, leave it as-is.
7965 - Else, reset it to the current thread for inferior_ptid. */
7966 void
7967 breakpoint_re_set_thread (struct breakpoint *b)
7968 {
7969 if (b->thread != -1)
7970 {
7971 if (in_thread_list (inferior_ptid))
7972 b->thread = pid_to_thread_id (inferior_ptid);
7973 }
7974 }
7975
7976 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
7977 If from_tty is nonzero, it prints a message to that effect,
7978 which ends with a period (no newline). */
7979
7980 void
7981 set_ignore_count (int bptnum, int count, int from_tty)
7982 {
7983 struct breakpoint *b;
7984
7985 if (count < 0)
7986 count = 0;
7987
7988 ALL_BREAKPOINTS (b)
7989 if (b->number == bptnum)
7990 {
7991 b->ignore_count = count;
7992 if (from_tty)
7993 {
7994 if (count == 0)
7995 printf_filtered (_("Will stop next time breakpoint %d is reached."),
7996 bptnum);
7997 else if (count == 1)
7998 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
7999 bptnum);
8000 else
8001 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
8002 count, bptnum);
8003 }
8004 breakpoints_changed ();
8005 observer_notify_breakpoint_modified (b->number);
8006 return;
8007 }
8008
8009 error (_("No breakpoint number %d."), bptnum);
8010 }
8011
8012 void
8013 make_breakpoint_silent (struct breakpoint *b)
8014 {
8015 /* Silence the breakpoint. */
8016 b->silent = 1;
8017 }
8018
8019 /* Command to set ignore-count of breakpoint N to COUNT. */
8020
8021 static void
8022 ignore_command (char *args, int from_tty)
8023 {
8024 char *p = args;
8025 int num;
8026
8027 if (p == 0)
8028 error_no_arg (_("a breakpoint number"));
8029
8030 num = get_number (&p);
8031 if (num == 0)
8032 error (_("bad breakpoint number: '%s'"), args);
8033 if (*p == 0)
8034 error (_("Second argument (specified ignore-count) is missing."));
8035
8036 set_ignore_count (num,
8037 longest_to_int (value_as_long (parse_and_eval (p))),
8038 from_tty);
8039 if (from_tty)
8040 printf_filtered ("\n");
8041 }
8042 \f
8043 /* Call FUNCTION on each of the breakpoints
8044 whose numbers are given in ARGS. */
8045
8046 static void
8047 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
8048 {
8049 char *p = args;
8050 char *p1;
8051 int num;
8052 struct breakpoint *b, *tmp;
8053 int match;
8054
8055 if (p == 0)
8056 error_no_arg (_("one or more breakpoint numbers"));
8057
8058 while (*p)
8059 {
8060 match = 0;
8061 p1 = p;
8062
8063 num = get_number_or_range (&p1);
8064 if (num == 0)
8065 {
8066 warning (_("bad breakpoint number at or near '%s'"), p);
8067 }
8068 else
8069 {
8070 ALL_BREAKPOINTS_SAFE (b, tmp)
8071 if (b->number == num)
8072 {
8073 struct breakpoint *related_breakpoint = b->related_breakpoint;
8074 match = 1;
8075 function (b);
8076 if (related_breakpoint)
8077 function (related_breakpoint);
8078 break;
8079 }
8080 if (match == 0)
8081 printf_unfiltered (_("No breakpoint number %d.\n"), num);
8082 }
8083 p = p1;
8084 }
8085 }
8086
8087 static struct bp_location *
8088 find_location_by_number (char *number)
8089 {
8090 char *dot = strchr (number, '.');
8091 char *p1;
8092 int bp_num;
8093 int loc_num;
8094 struct breakpoint *b;
8095 struct bp_location *loc;
8096
8097 *dot = '\0';
8098
8099 p1 = number;
8100 bp_num = get_number_or_range (&p1);
8101 if (bp_num == 0)
8102 error (_("Bad breakpoint number '%s'"), number);
8103
8104 ALL_BREAKPOINTS (b)
8105 if (b->number == bp_num)
8106 {
8107 break;
8108 }
8109
8110 if (!b || b->number != bp_num)
8111 error (_("Bad breakpoint number '%s'"), number);
8112
8113 p1 = dot+1;
8114 loc_num = get_number_or_range (&p1);
8115 if (loc_num == 0)
8116 error (_("Bad breakpoint location number '%s'"), number);
8117
8118 --loc_num;
8119 loc = b->loc;
8120 for (;loc_num && loc; --loc_num, loc = loc->next)
8121 ;
8122 if (!loc)
8123 error (_("Bad breakpoint location number '%s'"), dot+1);
8124
8125 return loc;
8126 }
8127
8128
8129 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
8130 If from_tty is nonzero, it prints a message to that effect,
8131 which ends with a period (no newline). */
8132
8133 void
8134 disable_breakpoint (struct breakpoint *bpt)
8135 {
8136 /* Never disable a watchpoint scope breakpoint; we want to
8137 hit them when we leave scope so we can delete both the
8138 watchpoint and its scope breakpoint at that time. */
8139 if (bpt->type == bp_watchpoint_scope)
8140 return;
8141
8142 /* You can't disable permanent breakpoints. */
8143 if (bpt->enable_state == bp_permanent)
8144 return;
8145
8146 bpt->enable_state = bp_disabled;
8147
8148 update_global_location_list (0);
8149
8150 observer_notify_breakpoint_modified (bpt->number);
8151 }
8152
8153 static void
8154 disable_command (char *args, int from_tty)
8155 {
8156 struct breakpoint *bpt;
8157 if (args == 0)
8158 ALL_BREAKPOINTS (bpt)
8159 switch (bpt->type)
8160 {
8161 case bp_none:
8162 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
8163 bpt->number);
8164 continue;
8165 case bp_breakpoint:
8166 case bp_tracepoint:
8167 case bp_catchpoint:
8168 case bp_hardware_breakpoint:
8169 case bp_watchpoint:
8170 case bp_hardware_watchpoint:
8171 case bp_read_watchpoint:
8172 case bp_access_watchpoint:
8173 disable_breakpoint (bpt);
8174 default:
8175 continue;
8176 }
8177 else if (strchr (args, '.'))
8178 {
8179 struct bp_location *loc = find_location_by_number (args);
8180 if (loc)
8181 loc->enabled = 0;
8182 update_global_location_list (0);
8183 }
8184 else
8185 map_breakpoint_numbers (args, disable_breakpoint);
8186 }
8187
8188 static void
8189 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
8190 {
8191 int target_resources_ok, other_type_used;
8192 struct value *mark;
8193
8194 if (bpt->type == bp_hardware_breakpoint)
8195 {
8196 int i;
8197 i = hw_breakpoint_used_count ();
8198 target_resources_ok =
8199 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8200 i + 1, 0);
8201 if (target_resources_ok == 0)
8202 error (_("No hardware breakpoint support in the target."));
8203 else if (target_resources_ok < 0)
8204 error (_("Hardware breakpoints used exceeds limit."));
8205 }
8206
8207 if (bpt->type == bp_watchpoint
8208 || bpt->type == bp_hardware_watchpoint
8209 || bpt->type == bp_read_watchpoint
8210 || bpt->type == bp_access_watchpoint)
8211 {
8212 struct gdb_exception e;
8213
8214 TRY_CATCH (e, RETURN_MASK_ALL)
8215 {
8216 update_watchpoint (bpt, 1 /* reparse */);
8217 }
8218 if (e.reason < 0)
8219 {
8220 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
8221 bpt->number);
8222 return;
8223 }
8224 }
8225
8226 if (bpt->enable_state != bp_permanent)
8227 bpt->enable_state = bp_enabled;
8228 bpt->disposition = disposition;
8229 update_global_location_list (1);
8230 breakpoints_changed ();
8231
8232 observer_notify_breakpoint_modified (bpt->number);
8233 }
8234
8235
8236 void
8237 enable_breakpoint (struct breakpoint *bpt)
8238 {
8239 do_enable_breakpoint (bpt, bpt->disposition);
8240 }
8241
8242 /* The enable command enables the specified breakpoints (or all defined
8243 breakpoints) so they once again become (or continue to be) effective
8244 in stopping the inferior. */
8245
8246 static void
8247 enable_command (char *args, int from_tty)
8248 {
8249 struct breakpoint *bpt;
8250 if (args == 0)
8251 ALL_BREAKPOINTS (bpt)
8252 switch (bpt->type)
8253 {
8254 case bp_none:
8255 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
8256 bpt->number);
8257 continue;
8258 case bp_breakpoint:
8259 case bp_tracepoint:
8260 case bp_catchpoint:
8261 case bp_hardware_breakpoint:
8262 case bp_watchpoint:
8263 case bp_hardware_watchpoint:
8264 case bp_read_watchpoint:
8265 case bp_access_watchpoint:
8266 enable_breakpoint (bpt);
8267 default:
8268 continue;
8269 }
8270 else if (strchr (args, '.'))
8271 {
8272 struct bp_location *loc = find_location_by_number (args);
8273 if (loc)
8274 loc->enabled = 1;
8275 update_global_location_list (1);
8276 }
8277 else
8278 map_breakpoint_numbers (args, enable_breakpoint);
8279 }
8280
8281 static void
8282 enable_once_breakpoint (struct breakpoint *bpt)
8283 {
8284 do_enable_breakpoint (bpt, disp_disable);
8285 }
8286
8287 static void
8288 enable_once_command (char *args, int from_tty)
8289 {
8290 map_breakpoint_numbers (args, enable_once_breakpoint);
8291 }
8292
8293 static void
8294 enable_delete_breakpoint (struct breakpoint *bpt)
8295 {
8296 do_enable_breakpoint (bpt, disp_del);
8297 }
8298
8299 static void
8300 enable_delete_command (char *args, int from_tty)
8301 {
8302 map_breakpoint_numbers (args, enable_delete_breakpoint);
8303 }
8304 \f
8305 static void
8306 set_breakpoint_cmd (char *args, int from_tty)
8307 {
8308 }
8309
8310 static void
8311 show_breakpoint_cmd (char *args, int from_tty)
8312 {
8313 }
8314
8315 /* Use default_breakpoint_'s, or nothing if they aren't valid. */
8316
8317 struct symtabs_and_lines
8318 decode_line_spec_1 (char *string, int funfirstline)
8319 {
8320 struct symtabs_and_lines sals;
8321 if (string == 0)
8322 error (_("Empty line specification."));
8323 if (default_breakpoint_valid)
8324 sals = decode_line_1 (&string, funfirstline,
8325 default_breakpoint_symtab,
8326 default_breakpoint_line,
8327 (char ***) NULL, NULL);
8328 else
8329 sals = decode_line_1 (&string, funfirstline,
8330 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
8331 if (*string)
8332 error (_("Junk at end of line specification: %s"), string);
8333 return sals;
8334 }
8335
8336 /* Create and insert a raw software breakpoint at PC. Return an
8337 identifier, which should be used to remove the breakpoint later.
8338 In general, places which call this should be using something on the
8339 breakpoint chain instead; this function should be eliminated
8340 someday. */
8341
8342 void *
8343 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
8344 {
8345 struct bp_target_info *bp_tgt;
8346
8347 bp_tgt = xmalloc (sizeof (struct bp_target_info));
8348 memset (bp_tgt, 0, sizeof (struct bp_target_info));
8349
8350 bp_tgt->placed_address = pc;
8351 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8352 {
8353 /* Could not insert the breakpoint. */
8354 xfree (bp_tgt);
8355 return NULL;
8356 }
8357
8358 return bp_tgt;
8359 }
8360
8361 /* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
8362
8363 int
8364 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8365 {
8366 struct bp_target_info *bp_tgt = bp;
8367 int ret;
8368
8369 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8370 xfree (bp_tgt);
8371
8372 return ret;
8373 }
8374
8375 /* One (or perhaps two) breakpoints used for software single stepping. */
8376
8377 static void *single_step_breakpoints[2];
8378 static struct gdbarch *single_step_gdbarch[2];
8379
8380 /* Create and insert a breakpoint for software single step. */
8381
8382 void
8383 insert_single_step_breakpoint (struct gdbarch *gdbarch, CORE_ADDR next_pc)
8384 {
8385 void **bpt_p;
8386
8387 if (single_step_breakpoints[0] == NULL)
8388 {
8389 bpt_p = &single_step_breakpoints[0];
8390 single_step_gdbarch[0] = gdbarch;
8391 }
8392 else
8393 {
8394 gdb_assert (single_step_breakpoints[1] == NULL);
8395 bpt_p = &single_step_breakpoints[1];
8396 single_step_gdbarch[1] = gdbarch;
8397 }
8398
8399 /* NOTE drow/2006-04-11: A future improvement to this function would be
8400 to only create the breakpoints once, and actually put them on the
8401 breakpoint chain. That would let us use set_raw_breakpoint. We could
8402 adjust the addresses each time they were needed. Doing this requires
8403 corresponding changes elsewhere where single step breakpoints are
8404 handled, however. So, for now, we use this. */
8405
8406 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, next_pc);
8407 if (*bpt_p == NULL)
8408 error (_("Could not insert single-step breakpoint at %s"),
8409 paddress (gdbarch, next_pc));
8410 }
8411
8412 /* Remove and delete any breakpoints used for software single step. */
8413
8414 void
8415 remove_single_step_breakpoints (void)
8416 {
8417 gdb_assert (single_step_breakpoints[0] != NULL);
8418
8419 /* See insert_single_step_breakpoint for more about this deprecated
8420 call. */
8421 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
8422 single_step_breakpoints[0]);
8423 single_step_gdbarch[0] = NULL;
8424 single_step_breakpoints[0] = NULL;
8425
8426 if (single_step_breakpoints[1] != NULL)
8427 {
8428 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
8429 single_step_breakpoints[1]);
8430 single_step_gdbarch[1] = NULL;
8431 single_step_breakpoints[1] = NULL;
8432 }
8433 }
8434
8435 /* Check whether a software single-step breakpoint is inserted at PC. */
8436
8437 static int
8438 single_step_breakpoint_inserted_here_p (CORE_ADDR pc)
8439 {
8440 int i;
8441
8442 for (i = 0; i < 2; i++)
8443 {
8444 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
8445 if (bp_tgt && bp_tgt->placed_address == pc)
8446 return 1;
8447 }
8448
8449 return 0;
8450 }
8451
8452 /* Tracepoint-specific operations. */
8453
8454 /* Set tracepoint count to NUM. */
8455 static void
8456 set_tracepoint_count (int num)
8457 {
8458 tracepoint_count = num;
8459 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
8460 }
8461
8462 void
8463 trace_command (char *arg, int from_tty)
8464 {
8465 break_command_really (get_current_arch (),
8466 arg,
8467 NULL, 0, 1 /* parse arg */,
8468 0 /* tempflag */, 0 /* hardwareflag */,
8469 1 /* traceflag */,
8470 0 /* Ignore count */,
8471 pending_break_support,
8472 NULL,
8473 from_tty,
8474 1 /* enabled */);
8475 set_tracepoint_count (breakpoint_count);
8476 }
8477
8478 /* Print information on tracepoint number TPNUM_EXP, or all if
8479 omitted. */
8480
8481 static void
8482 tracepoints_info (char *tpnum_exp, int from_tty)
8483 {
8484 struct breakpoint *b;
8485 int tps_to_list = 0;
8486
8487 /* In the no-arguments case, say "No tracepoints" if none found. */
8488 if (tpnum_exp == 0)
8489 {
8490 ALL_TRACEPOINTS (b)
8491 {
8492 if (b->number >= 0)
8493 {
8494 tps_to_list = 1;
8495 break;
8496 }
8497 }
8498 if (!tps_to_list)
8499 {
8500 ui_out_message (uiout, 0, "No tracepoints.\n");
8501 return;
8502 }
8503 }
8504
8505 /* Otherwise be the same as "info break". */
8506 breakpoints_info (tpnum_exp, from_tty);
8507 }
8508
8509 /* The 'enable trace' command enables tracepoints.
8510 Not supported by all targets. */
8511 static void
8512 enable_trace_command (char *args, int from_tty)
8513 {
8514 enable_command (args, from_tty);
8515 }
8516
8517 /* The 'disable trace' command disables tracepoints.
8518 Not supported by all targets. */
8519 static void
8520 disable_trace_command (char *args, int from_tty)
8521 {
8522 disable_command (args, from_tty);
8523 }
8524
8525 /* Remove a tracepoint (or all if no argument) */
8526 static void
8527 delete_trace_command (char *arg, int from_tty)
8528 {
8529 struct breakpoint *b, *temp;
8530
8531 dont_repeat ();
8532
8533 if (arg == 0)
8534 {
8535 int breaks_to_delete = 0;
8536
8537 /* Delete all breakpoints if no argument.
8538 Do not delete internal or call-dummy breakpoints, these
8539 have to be deleted with an explicit breakpoint number argument. */
8540 ALL_TRACEPOINTS (b)
8541 {
8542 if (b->number >= 0)
8543 {
8544 breaks_to_delete = 1;
8545 break;
8546 }
8547 }
8548
8549 /* Ask user only if there are some breakpoints to delete. */
8550 if (!from_tty
8551 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
8552 {
8553 ALL_BREAKPOINTS_SAFE (b, temp)
8554 {
8555 if (b->type == bp_tracepoint
8556 && b->number >= 0)
8557 delete_breakpoint (b);
8558 }
8559 }
8560 }
8561 else
8562 map_breakpoint_numbers (arg, delete_breakpoint);
8563 }
8564
8565 /* Set passcount for tracepoint.
8566
8567 First command argument is passcount, second is tracepoint number.
8568 If tracepoint number omitted, apply to most recently defined.
8569 Also accepts special argument "all". */
8570
8571 static void
8572 trace_pass_command (char *args, int from_tty)
8573 {
8574 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
8575 unsigned int count;
8576 int all = 0;
8577
8578 if (args == 0 || *args == 0)
8579 error (_("passcount command requires an argument (count + optional TP num)"));
8580
8581 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
8582
8583 while (*args && isspace ((int) *args))
8584 args++;
8585
8586 if (*args && strncasecmp (args, "all", 3) == 0)
8587 {
8588 args += 3; /* Skip special argument "all". */
8589 all = 1;
8590 if (*args)
8591 error (_("Junk at end of arguments."));
8592 }
8593 else
8594 t1 = get_tracepoint_by_number (&args, 1, 1);
8595
8596 do
8597 {
8598 if (t1)
8599 {
8600 ALL_TRACEPOINTS (t2)
8601 if (t1 == (struct breakpoint *) -1 || t1 == t2)
8602 {
8603 t2->pass_count = count;
8604 observer_notify_tracepoint_modified (t2->number);
8605 if (from_tty)
8606 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
8607 t2->number, count);
8608 }
8609 if (! all && *args)
8610 t1 = get_tracepoint_by_number (&args, 1, 0);
8611 }
8612 }
8613 while (*args);
8614 }
8615
8616 struct breakpoint *
8617 get_tracepoint (int num)
8618 {
8619 struct breakpoint *t;
8620
8621 ALL_TRACEPOINTS (t)
8622 if (t->number == num)
8623 return t;
8624
8625 return NULL;
8626 }
8627
8628 /* Utility: parse a tracepoint number and look it up in the list.
8629 If MULTI_P is true, there might be a range of tracepoints in ARG.
8630 if OPTIONAL_P is true, then if the argument is missing, the most
8631 recent tracepoint (tracepoint_count) is returned. */
8632 struct breakpoint *
8633 get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
8634 {
8635 extern int tracepoint_count;
8636 struct breakpoint *t;
8637 int tpnum;
8638 char *instring = arg == NULL ? NULL : *arg;
8639
8640 if (arg == NULL || *arg == NULL || ! **arg)
8641 {
8642 if (optional_p)
8643 tpnum = tracepoint_count;
8644 else
8645 error_no_arg (_("tracepoint number"));
8646 }
8647 else
8648 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
8649
8650 if (tpnum <= 0)
8651 {
8652 if (instring && *instring)
8653 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
8654 instring);
8655 else
8656 printf_filtered (_("Tracepoint argument missing and no previous tracepoint\n"));
8657 return NULL;
8658 }
8659
8660 ALL_TRACEPOINTS (t)
8661 if (t->number == tpnum)
8662 {
8663 return t;
8664 }
8665
8666 /* FIXME: if we are in the middle of a range we don't want to give
8667 a message. The current interface to get_number_or_range doesn't
8668 allow us to discover this. */
8669 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
8670 return NULL;
8671 }
8672
8673 /* save-tracepoints command */
8674 static void
8675 tracepoint_save_command (char *args, int from_tty)
8676 {
8677 struct breakpoint *tp;
8678 int any_tp = 0;
8679 struct action_line *line;
8680 FILE *fp;
8681 char *i1 = " ", *i2 = " ";
8682 char *indent, *actionline, *pathname;
8683 char tmp[40];
8684 struct cleanup *cleanup;
8685
8686 if (args == 0 || *args == 0)
8687 error (_("Argument required (file name in which to save tracepoints)"));
8688
8689 /* See if we have anything to save. */
8690 ALL_TRACEPOINTS (tp)
8691 {
8692 any_tp = 1;
8693 break;
8694 }
8695 if (!any_tp)
8696 {
8697 warning (_("save-tracepoints: no tracepoints to save."));
8698 return;
8699 }
8700
8701 pathname = tilde_expand (args);
8702 cleanup = make_cleanup (xfree, pathname);
8703 fp = fopen (pathname, "w");
8704 if (!fp)
8705 error (_("Unable to open file '%s' for saving tracepoints (%s)"),
8706 args, safe_strerror (errno));
8707 make_cleanup_fclose (fp);
8708
8709 ALL_TRACEPOINTS (tp)
8710 {
8711 if (tp->addr_string)
8712 fprintf (fp, "trace %s\n", tp->addr_string);
8713 else
8714 {
8715 sprintf_vma (tmp, tp->loc->address);
8716 fprintf (fp, "trace *0x%s\n", tmp);
8717 }
8718
8719 if (tp->pass_count)
8720 fprintf (fp, " passcount %d\n", tp->pass_count);
8721
8722 if (tp->actions)
8723 {
8724 fprintf (fp, " actions\n");
8725 indent = i1;
8726 for (line = tp->actions; line; line = line->next)
8727 {
8728 struct cmd_list_element *cmd;
8729
8730 QUIT; /* allow user to bail out with ^C */
8731 actionline = line->action;
8732 while (isspace ((int) *actionline))
8733 actionline++;
8734
8735 fprintf (fp, "%s%s\n", indent, actionline);
8736 if (*actionline != '#') /* skip for comment lines */
8737 {
8738 cmd = lookup_cmd (&actionline, cmdlist, "", -1, 1);
8739 if (cmd == 0)
8740 error (_("Bad action list item: %s"), actionline);
8741 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
8742 indent = i2;
8743 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
8744 indent = i1;
8745 }
8746 }
8747 }
8748 }
8749 do_cleanups (cleanup);
8750 if (from_tty)
8751 printf_filtered (_("Tracepoints saved to file '%s'.\n"), args);
8752 return;
8753 }
8754
8755 /* Create a vector of all tracepoints. */
8756
8757 VEC(breakpoint_p) *
8758 all_tracepoints ()
8759 {
8760 VEC(breakpoint_p) *tp_vec = 0;
8761 struct breakpoint *tp;
8762
8763 ALL_TRACEPOINTS (tp)
8764 {
8765 VEC_safe_push (breakpoint_p, tp_vec, tp);
8766 }
8767
8768 return tp_vec;
8769 }
8770
8771 \f
8772 /* This help string is used for the break, hbreak, tbreak and thbreak commands.
8773 It is defined as a macro to prevent duplication.
8774 COMMAND should be a string constant containing the name of the command. */
8775 #define BREAK_ARGS_HELP(command) \
8776 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
8777 LOCATION may be a line number, function name, or \"*\" and an address.\n\
8778 If a line number is specified, break at start of code for that line.\n\
8779 If a function is specified, break at start of code for that function.\n\
8780 If an address is specified, break at that exact address.\n\
8781 With no LOCATION, uses current execution address of selected stack frame.\n\
8782 This is useful for breaking on return to a stack frame.\n\
8783 \n\
8784 THREADNUM is the number from \"info threads\".\n\
8785 CONDITION is a boolean expression.\n\
8786 \n\
8787 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
8788 \n\
8789 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
8790
8791 /* List of subcommands for "catch". */
8792 static struct cmd_list_element *catch_cmdlist;
8793
8794 /* List of subcommands for "tcatch". */
8795 static struct cmd_list_element *tcatch_cmdlist;
8796
8797 /* Like add_cmd, but add the command to both the "catch" and "tcatch"
8798 lists, and pass some additional user data to the command function. */
8799 static void
8800 add_catch_command (char *name, char *docstring,
8801 void (*sfunc) (char *args, int from_tty,
8802 struct cmd_list_element *command),
8803 void *user_data_catch,
8804 void *user_data_tcatch)
8805 {
8806 struct cmd_list_element *command;
8807
8808 command = add_cmd (name, class_breakpoint, NULL, docstring,
8809 &catch_cmdlist);
8810 set_cmd_sfunc (command, sfunc);
8811 set_cmd_context (command, user_data_catch);
8812
8813 command = add_cmd (name, class_breakpoint, NULL, docstring,
8814 &tcatch_cmdlist);
8815 set_cmd_sfunc (command, sfunc);
8816 set_cmd_context (command, user_data_tcatch);
8817 }
8818
8819 void
8820 _initialize_breakpoint (void)
8821 {
8822 static struct cmd_list_element *breakpoint_set_cmdlist;
8823 static struct cmd_list_element *breakpoint_show_cmdlist;
8824 struct cmd_list_element *c;
8825
8826 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
8827
8828 breakpoint_chain = 0;
8829 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
8830 before a breakpoint is set. */
8831 breakpoint_count = 0;
8832
8833 tracepoint_count = 0;
8834
8835 add_com ("ignore", class_breakpoint, ignore_command, _("\
8836 Set ignore-count of breakpoint number N to COUNT.\n\
8837 Usage is `ignore N COUNT'."));
8838 if (xdb_commands)
8839 add_com_alias ("bc", "ignore", class_breakpoint, 1);
8840
8841 add_com ("commands", class_breakpoint, commands_command, _("\
8842 Set commands to be executed when a breakpoint is hit.\n\
8843 Give breakpoint number as argument after \"commands\".\n\
8844 With no argument, the targeted breakpoint is the last one set.\n\
8845 The commands themselves follow starting on the next line.\n\
8846 Type a line containing \"end\" to indicate the end of them.\n\
8847 Give \"silent\" as the first line to make the breakpoint silent;\n\
8848 then no output is printed when it is hit, except what the commands print."));
8849
8850 add_com ("condition", class_breakpoint, condition_command, _("\
8851 Specify breakpoint number N to break only if COND is true.\n\
8852 Usage is `condition N COND', where N is an integer and COND is an\n\
8853 expression to be evaluated whenever breakpoint N is reached."));
8854
8855 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
8856 Set a temporary breakpoint.\n\
8857 Like \"break\" except the breakpoint is only temporary,\n\
8858 so it will be deleted when hit. Equivalent to \"break\" followed\n\
8859 by using \"enable delete\" on the breakpoint number.\n\
8860 \n"
8861 BREAK_ARGS_HELP ("tbreak")));
8862 set_cmd_completer (c, location_completer);
8863
8864 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
8865 Set a hardware assisted breakpoint.\n\
8866 Like \"break\" except the breakpoint requires hardware support,\n\
8867 some target hardware may not have this support.\n\
8868 \n"
8869 BREAK_ARGS_HELP ("hbreak")));
8870 set_cmd_completer (c, location_completer);
8871
8872 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
8873 Set a temporary hardware assisted breakpoint.\n\
8874 Like \"hbreak\" except the breakpoint is only temporary,\n\
8875 so it will be deleted when hit.\n\
8876 \n"
8877 BREAK_ARGS_HELP ("thbreak")));
8878 set_cmd_completer (c, location_completer);
8879
8880 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
8881 Enable some breakpoints.\n\
8882 Give breakpoint numbers (separated by spaces) as arguments.\n\
8883 With no subcommand, breakpoints are enabled until you command otherwise.\n\
8884 This is used to cancel the effect of the \"disable\" command.\n\
8885 With a subcommand you can enable temporarily."),
8886 &enablelist, "enable ", 1, &cmdlist);
8887 if (xdb_commands)
8888 add_com ("ab", class_breakpoint, enable_command, _("\
8889 Enable some breakpoints.\n\
8890 Give breakpoint numbers (separated by spaces) as arguments.\n\
8891 With no subcommand, breakpoints are enabled until you command otherwise.\n\
8892 This is used to cancel the effect of the \"disable\" command.\n\
8893 With a subcommand you can enable temporarily."));
8894
8895 add_com_alias ("en", "enable", class_breakpoint, 1);
8896
8897 add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
8898 Enable some breakpoints.\n\
8899 Give breakpoint numbers (separated by spaces) as arguments.\n\
8900 This is used to cancel the effect of the \"disable\" command.\n\
8901 May be abbreviated to simply \"enable\".\n"),
8902 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
8903
8904 add_cmd ("once", no_class, enable_once_command, _("\
8905 Enable breakpoints for one hit. Give breakpoint numbers.\n\
8906 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
8907 &enablebreaklist);
8908
8909 add_cmd ("delete", no_class, enable_delete_command, _("\
8910 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8911 If a breakpoint is hit while enabled in this fashion, it is deleted."),
8912 &enablebreaklist);
8913
8914 add_cmd ("delete", no_class, enable_delete_command, _("\
8915 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8916 If a breakpoint is hit while enabled in this fashion, it is deleted."),
8917 &enablelist);
8918
8919 add_cmd ("once", no_class, enable_once_command, _("\
8920 Enable breakpoints for one hit. Give breakpoint numbers.\n\
8921 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
8922 &enablelist);
8923
8924 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
8925 Disable some breakpoints.\n\
8926 Arguments are breakpoint numbers with spaces in between.\n\
8927 To disable all breakpoints, give no argument.\n\
8928 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
8929 &disablelist, "disable ", 1, &cmdlist);
8930 add_com_alias ("dis", "disable", class_breakpoint, 1);
8931 add_com_alias ("disa", "disable", class_breakpoint, 1);
8932 if (xdb_commands)
8933 add_com ("sb", class_breakpoint, disable_command, _("\
8934 Disable some breakpoints.\n\
8935 Arguments are breakpoint numbers with spaces in between.\n\
8936 To disable all breakpoints, give no argument.\n\
8937 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
8938
8939 add_cmd ("breakpoints", class_alias, disable_command, _("\
8940 Disable some breakpoints.\n\
8941 Arguments are breakpoint numbers with spaces in between.\n\
8942 To disable all breakpoints, give no argument.\n\
8943 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
8944 This command may be abbreviated \"disable\"."),
8945 &disablelist);
8946
8947 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
8948 Delete some breakpoints or auto-display expressions.\n\
8949 Arguments are breakpoint numbers with spaces in between.\n\
8950 To delete all breakpoints, give no argument.\n\
8951 \n\
8952 Also a prefix command for deletion of other GDB objects.\n\
8953 The \"unset\" command is also an alias for \"delete\"."),
8954 &deletelist, "delete ", 1, &cmdlist);
8955 add_com_alias ("d", "delete", class_breakpoint, 1);
8956 add_com_alias ("del", "delete", class_breakpoint, 1);
8957 if (xdb_commands)
8958 add_com ("db", class_breakpoint, delete_command, _("\
8959 Delete some breakpoints.\n\
8960 Arguments are breakpoint numbers with spaces in between.\n\
8961 To delete all breakpoints, give no argument.\n"));
8962
8963 add_cmd ("breakpoints", class_alias, delete_command, _("\
8964 Delete some breakpoints or auto-display expressions.\n\
8965 Arguments are breakpoint numbers with spaces in between.\n\
8966 To delete all breakpoints, give no argument.\n\
8967 This command may be abbreviated \"delete\"."),
8968 &deletelist);
8969
8970 add_com ("clear", class_breakpoint, clear_command, _("\
8971 Clear breakpoint at specified line or function.\n\
8972 Argument may be line number, function name, or \"*\" and an address.\n\
8973 If line number is specified, all breakpoints in that line are cleared.\n\
8974 If function is specified, breakpoints at beginning of function are cleared.\n\
8975 If an address is specified, breakpoints at that address are cleared.\n\
8976 \n\
8977 With no argument, clears all breakpoints in the line that the selected frame\n\
8978 is executing in.\n\
8979 \n\
8980 See also the \"delete\" command which clears breakpoints by number."));
8981
8982 c = add_com ("break", class_breakpoint, break_command, _("\
8983 Set breakpoint at specified line or function.\n"
8984 BREAK_ARGS_HELP ("break")));
8985 set_cmd_completer (c, location_completer);
8986
8987 add_com_alias ("b", "break", class_run, 1);
8988 add_com_alias ("br", "break", class_run, 1);
8989 add_com_alias ("bre", "break", class_run, 1);
8990 add_com_alias ("brea", "break", class_run, 1);
8991
8992 if (xdb_commands)
8993 add_com_alias ("ba", "break", class_breakpoint, 1);
8994
8995 if (dbx_commands)
8996 {
8997 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
8998 Break in function/address or break at a line in the current file."),
8999 &stoplist, "stop ", 1, &cmdlist);
9000 add_cmd ("in", class_breakpoint, stopin_command,
9001 _("Break in function or address."), &stoplist);
9002 add_cmd ("at", class_breakpoint, stopat_command,
9003 _("Break at a line in the current file."), &stoplist);
9004 add_com ("status", class_info, breakpoints_info, _("\
9005 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
9006 The \"Type\" column indicates one of:\n\
9007 \tbreakpoint - normal breakpoint\n\
9008 \twatchpoint - watchpoint\n\
9009 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
9010 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
9011 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
9012 address and file/line number respectively.\n\
9013 \n\
9014 Convenience variable \"$_\" and default examine address for \"x\"\n\
9015 are set to the address of the last breakpoint listed unless the command\n\
9016 is prefixed with \"server \".\n\n\
9017 Convenience variable \"$bpnum\" contains the number of the last\n\
9018 breakpoint set."));
9019 }
9020
9021 add_info ("breakpoints", breakpoints_info, _("\
9022 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
9023 The \"Type\" column indicates one of:\n\
9024 \tbreakpoint - normal breakpoint\n\
9025 \twatchpoint - watchpoint\n\
9026 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
9027 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
9028 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
9029 address and file/line number respectively.\n\
9030 \n\
9031 Convenience variable \"$_\" and default examine address for \"x\"\n\
9032 are set to the address of the last breakpoint listed unless the command\n\
9033 is prefixed with \"server \".\n\n\
9034 Convenience variable \"$bpnum\" contains the number of the last\n\
9035 breakpoint set."));
9036
9037 if (xdb_commands)
9038 add_com ("lb", class_breakpoint, breakpoints_info, _("\
9039 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
9040 The \"Type\" column indicates one of:\n\
9041 \tbreakpoint - normal breakpoint\n\
9042 \twatchpoint - watchpoint\n\
9043 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
9044 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
9045 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
9046 address and file/line number respectively.\n\
9047 \n\
9048 Convenience variable \"$_\" and default examine address for \"x\"\n\
9049 are set to the address of the last breakpoint listed unless the command\n\
9050 is prefixed with \"server \".\n\n\
9051 Convenience variable \"$bpnum\" contains the number of the last\n\
9052 breakpoint set."));
9053
9054 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
9055 Status of all breakpoints, or breakpoint number NUMBER.\n\
9056 The \"Type\" column indicates one of:\n\
9057 \tbreakpoint - normal breakpoint\n\
9058 \twatchpoint - watchpoint\n\
9059 \tlongjmp - internal breakpoint used to step through longjmp()\n\
9060 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
9061 \tuntil - internal breakpoint used by the \"until\" command\n\
9062 \tfinish - internal breakpoint used by the \"finish\" command\n\
9063 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
9064 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
9065 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
9066 address and file/line number respectively.\n\
9067 \n\
9068 Convenience variable \"$_\" and default examine address for \"x\"\n\
9069 are set to the address of the last breakpoint listed unless the command\n\
9070 is prefixed with \"server \".\n\n\
9071 Convenience variable \"$bpnum\" contains the number of the last\n\
9072 breakpoint set."),
9073 &maintenanceinfolist);
9074
9075 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
9076 Set catchpoints to catch events."),
9077 &catch_cmdlist, "catch ",
9078 0/*allow-unknown*/, &cmdlist);
9079
9080 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
9081 Set temporary catchpoints to catch events."),
9082 &tcatch_cmdlist, "tcatch ",
9083 0/*allow-unknown*/, &cmdlist);
9084
9085 /* Add catch and tcatch sub-commands. */
9086 add_catch_command ("catch", _("\
9087 Catch an exception, when caught.\n\
9088 With an argument, catch only exceptions with the given name."),
9089 catch_catch_command,
9090 CATCH_PERMANENT,
9091 CATCH_TEMPORARY);
9092 add_catch_command ("throw", _("\
9093 Catch an exception, when thrown.\n\
9094 With an argument, catch only exceptions with the given name."),
9095 catch_throw_command,
9096 CATCH_PERMANENT,
9097 CATCH_TEMPORARY);
9098 add_catch_command ("fork", _("Catch calls to fork."),
9099 catch_fork_command_1,
9100 (void *) (uintptr_t) catch_fork_permanent,
9101 (void *) (uintptr_t) catch_fork_temporary);
9102 add_catch_command ("vfork", _("Catch calls to vfork."),
9103 catch_fork_command_1,
9104 (void *) (uintptr_t) catch_vfork_permanent,
9105 (void *) (uintptr_t) catch_vfork_temporary);
9106 add_catch_command ("exec", _("Catch calls to exec."),
9107 catch_exec_command_1,
9108 CATCH_PERMANENT,
9109 CATCH_TEMPORARY);
9110 add_catch_command ("exception", _("\
9111 Catch Ada exceptions, when raised.\n\
9112 With an argument, catch only exceptions with the given name."),
9113 catch_ada_exception_command,
9114 CATCH_PERMANENT,
9115 CATCH_TEMPORARY);
9116 add_catch_command ("assert", _("\
9117 Catch failed Ada assertions, when raised.\n\
9118 With an argument, catch only exceptions with the given name."),
9119 catch_assert_command,
9120 CATCH_PERMANENT,
9121 CATCH_TEMPORARY);
9122
9123 c = add_com ("watch", class_breakpoint, watch_command, _("\
9124 Set a watchpoint for an expression.\n\
9125 A watchpoint stops execution of your program whenever the value of\n\
9126 an expression changes."));
9127 set_cmd_completer (c, expression_completer);
9128
9129 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
9130 Set a read watchpoint for an expression.\n\
9131 A watchpoint stops execution of your program whenever the value of\n\
9132 an expression is read."));
9133 set_cmd_completer (c, expression_completer);
9134
9135 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
9136 Set a watchpoint for an expression.\n\
9137 A watchpoint stops execution of your program whenever the value of\n\
9138 an expression is either read or written."));
9139 set_cmd_completer (c, expression_completer);
9140
9141 add_info ("watchpoints", breakpoints_info,
9142 _("Synonym for ``info breakpoints''."));
9143
9144
9145 /* XXX: cagney/2005-02-23: This should be a boolean, and should
9146 respond to changes - contrary to the description. */
9147 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
9148 &can_use_hw_watchpoints, _("\
9149 Set debugger's willingness to use watchpoint hardware."), _("\
9150 Show debugger's willingness to use watchpoint hardware."), _("\
9151 If zero, gdb will not use hardware for new watchpoints, even if\n\
9152 such is available. (However, any hardware watchpoints that were\n\
9153 created before setting this to nonzero, will continue to use watchpoint\n\
9154 hardware.)"),
9155 NULL,
9156 show_can_use_hw_watchpoints,
9157 &setlist, &showlist);
9158
9159 can_use_hw_watchpoints = 1;
9160
9161 /* Tracepoint manipulation commands. */
9162
9163 c = add_com ("trace", class_breakpoint, trace_command, _("\
9164 Set a tracepoint at specified line or function.\n\
9165 \n"
9166 BREAK_ARGS_HELP ("trace") "\n\
9167 Do \"help tracepoints\" for info on other tracepoint commands."));
9168 set_cmd_completer (c, location_completer);
9169
9170 add_com_alias ("tp", "trace", class_alias, 0);
9171 add_com_alias ("tr", "trace", class_alias, 1);
9172 add_com_alias ("tra", "trace", class_alias, 1);
9173 add_com_alias ("trac", "trace", class_alias, 1);
9174
9175 add_info ("tracepoints", tracepoints_info, _("\
9176 Status of tracepoints, or tracepoint number NUMBER.\n\
9177 Convenience variable \"$tpnum\" contains the number of the\n\
9178 last tracepoint set."));
9179
9180 add_info_alias ("tp", "tracepoints", 1);
9181
9182 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
9183 Delete specified tracepoints.\n\
9184 Arguments are tracepoint numbers, separated by spaces.\n\
9185 No argument means delete all tracepoints."),
9186 &deletelist);
9187
9188 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
9189 Disable specified tracepoints.\n\
9190 Arguments are tracepoint numbers, separated by spaces.\n\
9191 No argument means disable all tracepoints."),
9192 &disablelist);
9193 deprecate_cmd (c, "disable");
9194
9195 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
9196 Enable specified tracepoints.\n\
9197 Arguments are tracepoint numbers, separated by spaces.\n\
9198 No argument means enable all tracepoints."),
9199 &enablelist);
9200 deprecate_cmd (c, "enable");
9201
9202 add_com ("passcount", class_trace, trace_pass_command, _("\
9203 Set the passcount for a tracepoint.\n\
9204 The trace will end when the tracepoint has been passed 'count' times.\n\
9205 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
9206 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
9207
9208 c = add_com ("save-tracepoints", class_trace, tracepoint_save_command, _("\
9209 Save current tracepoint definitions as a script.\n\
9210 Use the 'source' command in another debug session to restore them."));
9211 set_cmd_completer (c, filename_completer);
9212
9213 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
9214 Breakpoint specific settings\n\
9215 Configure various breakpoint-specific variables such as\n\
9216 pending breakpoint behavior"),
9217 &breakpoint_set_cmdlist, "set breakpoint ",
9218 0/*allow-unknown*/, &setlist);
9219 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
9220 Breakpoint specific settings\n\
9221 Configure various breakpoint-specific variables such as\n\
9222 pending breakpoint behavior"),
9223 &breakpoint_show_cmdlist, "show breakpoint ",
9224 0/*allow-unknown*/, &showlist);
9225
9226 add_setshow_auto_boolean_cmd ("pending", no_class,
9227 &pending_break_support, _("\
9228 Set debugger's behavior regarding pending breakpoints."), _("\
9229 Show debugger's behavior regarding pending breakpoints."), _("\
9230 If on, an unrecognized breakpoint location will cause gdb to create a\n\
9231 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
9232 an error. If auto, an unrecognized breakpoint location results in a\n\
9233 user-query to see if a pending breakpoint should be created."),
9234 NULL,
9235 show_pending_break_support,
9236 &breakpoint_set_cmdlist,
9237 &breakpoint_show_cmdlist);
9238
9239 pending_break_support = AUTO_BOOLEAN_AUTO;
9240
9241 add_setshow_boolean_cmd ("auto-hw", no_class,
9242 &automatic_hardware_breakpoints, _("\
9243 Set automatic usage of hardware breakpoints."), _("\
9244 Show automatic usage of hardware breakpoints."), _("\
9245 If set, the debugger will automatically use hardware breakpoints for\n\
9246 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
9247 a warning will be emitted for such breakpoints."),
9248 NULL,
9249 show_automatic_hardware_breakpoints,
9250 &breakpoint_set_cmdlist,
9251 &breakpoint_show_cmdlist);
9252
9253 add_setshow_enum_cmd ("always-inserted", class_support,
9254 always_inserted_enums, &always_inserted_mode, _("\
9255 Set mode for inserting breakpoints."), _("\
9256 Show mode for inserting breakpoints."), _("\
9257 When this mode is off, breakpoints are inserted in inferior when it is\n\
9258 resumed, and removed when execution stops. When this mode is on,\n\
9259 breakpoints are inserted immediately and removed only when the user\n\
9260 deletes the breakpoint. When this mode is auto (which is the default),\n\
9261 the behaviour depends on the non-stop setting (see help set non-stop).\n\
9262 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
9263 behaves as if always-inserted mode is on; if gdb is controlling the\n\
9264 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
9265 NULL,
9266 &show_always_inserted_mode,
9267 &breakpoint_set_cmdlist,
9268 &breakpoint_show_cmdlist);
9269
9270 automatic_hardware_breakpoints = 1;
9271
9272 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
9273 }