* Makefile.in (linuw_low_h): Move higher.
[binutils-gdb.git] / gdb / breakpoint.c
blob5affe4e6835908ad2d4dbe223ff70ce9bee62eb2
1 /* Everything about breakpoints, for GDB.
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.
7 This file is part of GDB.
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.
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.
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/>. */
22 #include "defs.h"
23 #include <ctype.h>
24 #include "hashtab.h"
25 #include "symtab.h"
26 #include "frame.h"
27 #include "breakpoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "gdbthread.h"
36 #include "target.h"
37 #include "language.h"
38 #include "gdb_string.h"
39 #include "demangle.h"
40 #include "annotate.h"
41 #include "symfile.h"
42 #include "objfiles.h"
43 #include "source.h"
44 #include "linespec.h"
45 #include "completer.h"
46 #include "gdb.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "gdb_assert.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "exceptions.h"
55 #include "memattr.h"
56 #include "ada-lang.h"
57 #include "top.h"
58 #include "wrapper.h"
59 #include "valprint.h"
61 #include "mi/mi-common.h"
63 /* Arguments to pass as context to some catch command handlers. */
64 #define CATCH_PERMANENT ((void *) (uintptr_t) 0)
65 #define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
67 /* Prototypes for local functions. */
69 static void enable_delete_command (char *, int);
71 static void enable_delete_breakpoint (struct breakpoint *);
73 static void enable_once_command (char *, int);
75 static void enable_once_breakpoint (struct breakpoint *);
77 static void disable_command (char *, int);
79 static void enable_command (char *, int);
81 static void map_breakpoint_numbers (char *, void (*)(struct breakpoint *));
83 static void ignore_command (char *, int);
85 static int breakpoint_re_set_one (void *);
87 static void clear_command (char *, int);
89 static void catch_command (char *, int);
91 static void watch_command (char *, int);
93 static int can_use_hardware_watchpoint (struct value *);
95 static void break_command_1 (char *, int, int);
97 static void mention (struct breakpoint *);
99 struct breakpoint *set_raw_breakpoint (struct symtab_and_line, enum bptype);
101 static void check_duplicates (struct breakpoint *);
103 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
105 static CORE_ADDR adjust_breakpoint_address (CORE_ADDR bpaddr,
106 enum bptype bptype);
108 static void describe_other_breakpoints (CORE_ADDR, struct obj_section *, int);
110 static void breakpoints_info (char *, int);
112 static void breakpoint_1 (int, int);
114 static bpstat bpstat_alloc (const struct bp_location *, bpstat);
116 static int breakpoint_cond_eval (void *);
118 static void cleanup_executing_breakpoints (void *);
120 static void commands_command (char *, int);
122 static void condition_command (char *, int);
124 static int get_number_trailer (char **, int);
126 void set_breakpoint_count (int);
128 typedef enum
130 mark_inserted,
131 mark_uninserted
133 insertion_state_t;
135 static int remove_breakpoint (struct bp_location *, insertion_state_t);
137 static enum print_stop_action print_it_typical (bpstat);
139 static enum print_stop_action print_bp_stop_message (bpstat bs);
141 static int watchpoint_check (void *);
143 static void maintenance_info_breakpoints (char *, int);
145 static void create_overlay_event_breakpoint (char *);
147 static int hw_breakpoint_used_count (void);
149 static int hw_watchpoint_used_count (enum bptype, int *);
151 static void hbreak_command (char *, int);
153 static void thbreak_command (char *, int);
155 static void watch_command_1 (char *, int, int);
157 static void rwatch_command (char *, int);
159 static void awatch_command (char *, int);
161 static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
163 static void stop_command (char *arg, int from_tty);
165 static void stopin_command (char *arg, int from_tty);
167 static void stopat_command (char *arg, int from_tty);
169 static char *ep_parse_optional_if_clause (char **arg);
171 static char *ep_parse_optional_filename (char **arg);
173 static void catch_exception_command_1 (enum exception_event_kind ex_event,
174 char *arg, int tempflag, int from_tty);
176 static void tcatch_command (char *arg, int from_tty);
178 static void ep_skip_leading_whitespace (char **s);
180 static int single_step_breakpoint_inserted_here_p (CORE_ADDR pc);
182 static void free_bp_location (struct bp_location *loc);
184 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
186 static void update_global_location_list (int);
188 static void update_global_location_list_nothrow (int);
190 static int is_hardware_watchpoint (struct breakpoint *bpt);
192 static void insert_breakpoint_locations (void);
194 /* Flag indicating that a command has proceeded the inferior past the
195 current breakpoint. */
197 static int breakpoint_proceeded;
199 static const char *
200 bpdisp_text (enum bpdisp disp)
202 /* NOTE: the following values are a part of MI protocol and represent
203 values of 'disp' field returned when inferior stops at a breakpoint. */
204 static char *bpdisps[] = {"del", "dstp", "dis", "keep"};
205 return bpdisps[(int) disp];
208 /* Prototypes for exported functions. */
209 /* If FALSE, gdb will not use hardware support for watchpoints, even
210 if such is available. */
211 static int can_use_hw_watchpoints;
213 static void
214 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
215 struct cmd_list_element *c,
216 const char *value)
218 fprintf_filtered (file, _("\
219 Debugger's willingness to use watchpoint hardware is %s.\n"),
220 value);
223 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
224 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
225 for unrecognized breakpoint locations.
226 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
227 static enum auto_boolean pending_break_support;
228 static void
229 show_pending_break_support (struct ui_file *file, int from_tty,
230 struct cmd_list_element *c,
231 const char *value)
233 fprintf_filtered (file, _("\
234 Debugger's behavior regarding pending breakpoints is %s.\n"),
235 value);
238 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
239 set with "break" but falling in read-only memory.
240 If 0, gdb will warn about such breakpoints, but won't automatically
241 use hardware breakpoints. */
242 static int automatic_hardware_breakpoints;
243 static void
244 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
245 struct cmd_list_element *c,
246 const char *value)
248 fprintf_filtered (file, _("\
249 Automatic usage of hardware breakpoints is %s.\n"),
250 value);
253 /* If on, gdb will keep breakpoints inserted even as inferior is
254 stopped, and immediately insert any new breakpoints. If off, gdb
255 will insert breakpoints into inferior only when resuming it, and
256 will remove breakpoints upon stop. If auto, GDB will behave as ON
257 if in non-stop mode, and as OFF if all-stop mode.*/
259 static const char always_inserted_auto[] = "auto";
260 static const char always_inserted_on[] = "on";
261 static const char always_inserted_off[] = "off";
262 static const char *always_inserted_enums[] = {
263 always_inserted_auto,
264 always_inserted_off,
265 always_inserted_on,
266 NULL
268 static const char *always_inserted_mode = always_inserted_auto;
269 static void
270 show_always_inserted_mode (struct ui_file *file, int from_tty,
271 struct cmd_list_element *c, const char *value)
273 if (always_inserted_mode == always_inserted_auto)
274 fprintf_filtered (file, _("\
275 Always inserted breakpoint mode is %s (currently %s).\n"),
276 value,
277 breakpoints_always_inserted_mode () ? "on" : "off");
278 else
279 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"), value);
283 breakpoints_always_inserted_mode (void)
285 return (always_inserted_mode == always_inserted_on
286 || (always_inserted_mode == always_inserted_auto && non_stop));
289 void _initialize_breakpoint (void);
291 /* Are we executing breakpoint commands? */
292 static int executing_breakpoint_commands;
294 /* Are overlay event breakpoints enabled? */
295 static int overlay_events_enabled;
297 /* Walk the following statement or block through all breakpoints.
298 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
299 breakpoint. */
301 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
303 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
304 for (B = breakpoint_chain; \
305 B ? (TMP=B->next, 1): 0; \
306 B = TMP)
308 /* Similar iterators for the low-level breakpoints. */
310 #define ALL_BP_LOCATIONS(B) for (B = bp_location_chain; B; B = B->global_next)
312 #define ALL_BP_LOCATIONS_SAFE(B,TMP) \
313 for (B = bp_location_chain; \
314 B ? (TMP=B->global_next, 1): 0; \
315 B = TMP)
317 /* Chains of all breakpoints defined. */
319 struct breakpoint *breakpoint_chain;
321 struct bp_location *bp_location_chain;
323 /* The locations that no longer correspond to any breakpoint,
324 unlinked from bp_location_chain, but for which a hit
325 may still be reported by a target. */
326 VEC(bp_location_p) *moribund_locations = NULL;
328 /* Number of last breakpoint made. */
330 int breakpoint_count;
332 /* Return whether a breakpoint is an active enabled breakpoint. */
333 static int
334 breakpoint_enabled (struct breakpoint *b)
336 return (b->enable_state == bp_enabled);
339 /* Set breakpoint count to NUM. */
341 void
342 set_breakpoint_count (int num)
344 breakpoint_count = num;
345 set_internalvar (lookup_internalvar ("bpnum"),
346 value_from_longest (builtin_type_int32, (LONGEST) num));
349 /* Used in run_command to zero the hit count when a new run starts. */
351 void
352 clear_breakpoint_hit_counts (void)
354 struct breakpoint *b;
356 ALL_BREAKPOINTS (b)
357 b->hit_count = 0;
360 /* Default address, symtab and line to put a breakpoint at
361 for "break" command with no arg.
362 if default_breakpoint_valid is zero, the other three are
363 not valid, and "break" with no arg is an error.
365 This set by print_stack_frame, which calls set_default_breakpoint. */
367 int default_breakpoint_valid;
368 CORE_ADDR default_breakpoint_address;
369 struct symtab *default_breakpoint_symtab;
370 int default_breakpoint_line;
372 /* *PP is a string denoting a breakpoint. Get the number of the breakpoint.
373 Advance *PP after the string and any trailing whitespace.
375 Currently the string can either be a number or "$" followed by the name
376 of a convenience variable. Making it an expression wouldn't work well
377 for map_breakpoint_numbers (e.g. "4 + 5 + 6").
379 If the string is a NULL pointer, that denotes the last breakpoint.
381 TRAILER is a character which can be found after the number; most
382 commonly this is `-'. If you don't want a trailer, use \0. */
383 static int
384 get_number_trailer (char **pp, int trailer)
386 int retval = 0; /* default */
387 char *p = *pp;
389 if (p == NULL)
390 /* Empty line means refer to the last breakpoint. */
391 return breakpoint_count;
392 else if (*p == '$')
394 /* Make a copy of the name, so we can null-terminate it
395 to pass to lookup_internalvar(). */
396 char *varname;
397 char *start = ++p;
398 struct value *val;
400 while (isalnum (*p) || *p == '_')
401 p++;
402 varname = (char *) alloca (p - start + 1);
403 strncpy (varname, start, p - start);
404 varname[p - start] = '\0';
405 val = value_of_internalvar (lookup_internalvar (varname));
406 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
407 retval = (int) value_as_long (val);
408 else
410 printf_filtered (_("Convenience variable must have integer value.\n"));
411 retval = 0;
414 else
416 if (*p == '-')
417 ++p;
418 while (*p >= '0' && *p <= '9')
419 ++p;
420 if (p == *pp)
421 /* There is no number here. (e.g. "cond a == b"). */
423 /* Skip non-numeric token */
424 while (*p && !isspace((int) *p))
425 ++p;
426 /* Return zero, which caller must interpret as error. */
427 retval = 0;
429 else
430 retval = atoi (*pp);
432 if (!(isspace (*p) || *p == '\0' || *p == trailer))
434 /* Trailing junk: return 0 and let caller print error msg. */
435 while (!(isspace (*p) || *p == '\0' || *p == trailer))
436 ++p;
437 retval = 0;
439 while (isspace (*p))
440 p++;
441 *pp = p;
442 return retval;
446 /* Like get_number_trailer, but don't allow a trailer. */
448 get_number (char **pp)
450 return get_number_trailer (pp, '\0');
453 /* Parse a number or a range.
454 * A number will be of the form handled by get_number.
455 * A range will be of the form <number1> - <number2>, and
456 * will represent all the integers between number1 and number2,
457 * inclusive.
459 * While processing a range, this fuction is called iteratively;
460 * At each call it will return the next value in the range.
462 * At the beginning of parsing a range, the char pointer PP will
463 * be advanced past <number1> and left pointing at the '-' token.
464 * Subsequent calls will not advance the pointer until the range
465 * is completed. The call that completes the range will advance
466 * pointer PP past <number2>.
469 int
470 get_number_or_range (char **pp)
472 static int last_retval, end_value;
473 static char *end_ptr;
474 static int in_range = 0;
476 if (**pp != '-')
478 /* Default case: pp is pointing either to a solo number,
479 or to the first number of a range. */
480 last_retval = get_number_trailer (pp, '-');
481 if (**pp == '-')
483 char **temp;
485 /* This is the start of a range (<number1> - <number2>).
486 Skip the '-', parse and remember the second number,
487 and also remember the end of the final token. */
489 temp = &end_ptr;
490 end_ptr = *pp + 1;
491 while (isspace ((int) *end_ptr))
492 end_ptr++; /* skip white space */
493 end_value = get_number (temp);
494 if (end_value < last_retval)
496 error (_("inverted range"));
498 else if (end_value == last_retval)
500 /* degenerate range (number1 == number2). Advance the
501 token pointer so that the range will be treated as a
502 single number. */
503 *pp = end_ptr;
505 else
506 in_range = 1;
509 else if (! in_range)
510 error (_("negative value"));
511 else
513 /* pp points to the '-' that betokens a range. All
514 number-parsing has already been done. Return the next
515 integer value (one greater than the saved previous value).
516 Do not advance the token pointer 'pp' until the end of range
517 is reached. */
519 if (++last_retval == end_value)
521 /* End of range reached; advance token pointer. */
522 *pp = end_ptr;
523 in_range = 0;
526 return last_retval;
531 /* condition N EXP -- set break condition of breakpoint N to EXP. */
533 static void
534 condition_command (char *arg, int from_tty)
536 struct breakpoint *b;
537 char *p;
538 int bnum;
540 if (arg == 0)
541 error_no_arg (_("breakpoint number"));
543 p = arg;
544 bnum = get_number (&p);
545 if (bnum == 0)
546 error (_("Bad breakpoint argument: '%s'"), arg);
548 ALL_BREAKPOINTS (b)
549 if (b->number == bnum)
551 struct bp_location *loc = b->loc;
552 for (; loc; loc = loc->next)
554 if (loc->cond)
556 xfree (loc->cond);
557 loc->cond = 0;
560 if (b->cond_string != NULL)
561 xfree (b->cond_string);
563 if (*p == 0)
565 b->cond_string = NULL;
566 if (from_tty)
567 printf_filtered (_("Breakpoint %d now unconditional.\n"), bnum);
569 else
571 arg = p;
572 /* I don't know if it matters whether this is the string the user
573 typed in or the decompiled expression. */
574 b->cond_string = savestring (arg, strlen (arg));
575 b->condition_not_parsed = 0;
576 for (loc = b->loc; loc; loc = loc->next)
578 arg = p;
579 loc->cond =
580 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
581 if (*arg)
582 error (_("Junk at end of expression"));
585 breakpoints_changed ();
586 observer_notify_breakpoint_modified (b->number);
587 return;
590 error (_("No breakpoint number %d."), bnum);
593 static void
594 commands_command (char *arg, int from_tty)
596 struct breakpoint *b;
597 char *p;
598 int bnum;
599 struct command_line *l;
601 /* If we allowed this, we would have problems with when to
602 free the storage, if we change the commands currently
603 being read from. */
605 if (executing_breakpoint_commands)
606 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
608 p = arg;
609 bnum = get_number (&p);
611 if (p && *p)
612 error (_("Unexpected extra arguments following breakpoint number."));
614 ALL_BREAKPOINTS (b)
615 if (b->number == bnum)
617 char *tmpbuf = xstrprintf ("Type commands for when breakpoint %d is hit, one per line.",
618 bnum);
619 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
620 l = read_command_lines (tmpbuf, from_tty, 1);
621 do_cleanups (cleanups);
622 free_command_lines (&b->commands);
623 b->commands = l;
624 breakpoints_changed ();
625 observer_notify_breakpoint_modified (b->number);
626 return;
628 error (_("No breakpoint number %d."), bnum);
631 /* Like commands_command, but instead of reading the commands from
632 input stream, takes them from an already parsed command structure.
634 This is used by cli-script.c to DTRT with breakpoint commands
635 that are part of if and while bodies. */
636 enum command_control_type
637 commands_from_control_command (char *arg, struct command_line *cmd)
639 struct breakpoint *b;
640 char *p;
641 int bnum;
643 /* If we allowed this, we would have problems with when to
644 free the storage, if we change the commands currently
645 being read from. */
647 if (executing_breakpoint_commands)
648 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
650 /* An empty string for the breakpoint number means the last
651 breakpoint, but get_number expects a NULL pointer. */
652 if (arg && !*arg)
653 p = NULL;
654 else
655 p = arg;
656 bnum = get_number (&p);
658 if (p && *p)
659 error (_("Unexpected extra arguments following breakpoint number."));
661 ALL_BREAKPOINTS (b)
662 if (b->number == bnum)
664 free_command_lines (&b->commands);
665 if (cmd->body_count != 1)
666 error (_("Invalid \"commands\" block structure."));
667 /* We need to copy the commands because if/while will free the
668 list after it finishes execution. */
669 b->commands = copy_command_lines (cmd->body_list[0]);
670 breakpoints_changed ();
671 observer_notify_breakpoint_modified (b->number);
672 return simple_control;
674 error (_("No breakpoint number %d."), bnum);
677 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
678 by replacing any memory breakpoints with their shadowed contents. */
680 void
681 breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
683 struct bp_location *b;
684 CORE_ADDR bp_addr = 0;
685 int bp_size = 0;
686 int bptoffset = 0;
688 ALL_BP_LOCATIONS (b)
690 if (b->owner->type == bp_none)
691 warning (_("reading through apparently deleted breakpoint #%d?"),
692 b->owner->number);
694 if (b->loc_type != bp_loc_software_breakpoint)
695 continue;
696 if (!b->inserted)
697 continue;
698 /* Addresses and length of the part of the breakpoint that
699 we need to copy. */
700 bp_addr = b->target_info.placed_address;
701 bp_size = b->target_info.shadow_len;
702 if (bp_size == 0)
703 /* bp isn't valid, or doesn't shadow memory. */
704 continue;
706 if (bp_addr + bp_size <= memaddr)
707 /* The breakpoint is entirely before the chunk of memory we
708 are reading. */
709 continue;
711 if (bp_addr >= memaddr + len)
712 /* The breakpoint is entirely after the chunk of memory we are
713 reading. */
714 continue;
716 /* Offset within shadow_contents. */
717 if (bp_addr < memaddr)
719 /* Only copy the second part of the breakpoint. */
720 bp_size -= memaddr - bp_addr;
721 bptoffset = memaddr - bp_addr;
722 bp_addr = memaddr;
725 if (bp_addr + bp_size > memaddr + len)
727 /* Only copy the first part of the breakpoint. */
728 bp_size -= (bp_addr + bp_size) - (memaddr + len);
731 memcpy (buf + bp_addr - memaddr,
732 b->target_info.shadow_contents + bptoffset, bp_size);
737 /* A wrapper function for inserting catchpoints. */
738 static void
739 insert_catchpoint (struct ui_out *uo, void *args)
741 struct breakpoint *b = (struct breakpoint *) args;
742 int val = -1;
744 gdb_assert (b->type == bp_catchpoint);
745 gdb_assert (b->ops != NULL && b->ops->insert != NULL);
747 b->ops->insert (b);
750 static int
751 is_hardware_watchpoint (struct breakpoint *bpt)
753 return (bpt->type == bp_hardware_watchpoint
754 || bpt->type == bp_read_watchpoint
755 || bpt->type == bp_access_watchpoint);
758 /* Find the current value of a watchpoint on EXP. Return the value in
759 *VALP and *RESULTP and the chain of intermediate and final values
760 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
761 not need them.
763 If a memory error occurs while evaluating the expression, *RESULTP will
764 be set to NULL. *RESULTP may be a lazy value, if the result could
765 not be read from memory. It is used to determine whether a value
766 is user-specified (we should watch the whole value) or intermediate
767 (we should watch only the bit used to locate the final value).
769 If the final value, or any intermediate value, could not be read
770 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
771 set to any referenced values. *VALP will never be a lazy value.
772 This is the value which we store in struct breakpoint.
774 If VAL_CHAIN is non-NULL, *VAL_CHAIN will be released from the
775 value chain. The caller must free the values individually. If
776 VAL_CHAIN is NULL, all generated values will be left on the value
777 chain. */
779 static void
780 fetch_watchpoint_value (struct expression *exp, struct value **valp,
781 struct value **resultp, struct value **val_chain)
783 struct value *mark, *new_mark, *result;
784 volatile struct gdb_exception ex;
786 *valp = NULL;
787 if (resultp)
788 *resultp = NULL;
789 if (val_chain)
790 *val_chain = NULL;
792 /* Evaluate the expression. */
793 mark = value_mark ();
794 result = NULL;
796 TRY_CATCH (ex, RETURN_MASK_ALL)
798 result = evaluate_expression (exp);
800 if (ex.reason < 0)
802 /* Ignore memory errors, we want watchpoints pointing at
803 inaccessible memory to still be created; otherwise, throw the
804 error to some higher catcher. */
805 switch (ex.error)
807 case MEMORY_ERROR:
808 break;
809 default:
810 throw_exception (ex);
811 break;
815 new_mark = value_mark ();
816 if (mark == new_mark)
817 return;
818 if (resultp)
819 *resultp = result;
821 /* Make sure it's not lazy, so that after the target stops again we
822 have a non-lazy previous value to compare with. */
823 if (result != NULL
824 && (!value_lazy (result) || gdb_value_fetch_lazy (result)))
825 *valp = result;
827 if (val_chain)
829 /* Return the chain of intermediate values. We use this to
830 decide which addresses to watch. */
831 *val_chain = new_mark;
832 value_release_to_mark (mark);
836 /* Assuming that B is a watchpoint:
837 - Reparse watchpoint expression, if REPARSE is non-zero
838 - Evaluate expression and store the result in B->val
839 - Evaluate the condition if there is one, and store the result
840 in b->loc->cond.
841 - Update the list of values that must be watched in B->loc.
843 If the watchpoint disposition is disp_del_at_next_stop, then do nothing.
844 If this is local watchpoint that is out of scope, delete it. */
845 static void
846 update_watchpoint (struct breakpoint *b, int reparse)
848 int within_current_scope;
849 struct frame_id saved_frame_id;
850 struct bp_location *loc;
851 bpstat bs;
853 /* We don't free locations. They are stored in bp_location_chain and
854 update_global_locations will eventually delete them and remove
855 breakpoints if needed. */
856 b->loc = NULL;
858 if (b->disposition == disp_del_at_next_stop)
859 return;
861 /* Save the current frame's ID so we can restore it after
862 evaluating the watchpoint expression on its own frame. */
863 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
864 took a frame parameter, so that we didn't have to change the
865 selected frame. */
866 saved_frame_id = get_frame_id (get_selected_frame (NULL));
868 /* Determine if the watchpoint is within scope. */
869 if (b->exp_valid_block == NULL)
870 within_current_scope = 1;
871 else
873 struct frame_info *fi;
874 fi = frame_find_by_id (b->watchpoint_frame);
875 within_current_scope = (fi != NULL);
876 if (within_current_scope)
877 select_frame (fi);
880 if (within_current_scope && reparse)
882 char *s;
883 if (b->exp)
885 xfree (b->exp);
886 b->exp = NULL;
888 s = b->exp_string;
889 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
890 /* If the meaning of expression itself changed, the old value is
891 no longer relevant. We don't want to report a watchpoint hit
892 to the user when the old value and the new value may actually
893 be completely different objects. */
894 value_free (b->val);
895 b->val = NULL;
896 b->val_valid = 0;
899 /* If we failed to parse the expression, for example because
900 it refers to a global variable in a not-yet-loaded shared library,
901 don't try to insert watchpoint. We don't automatically delete
902 such watchpoint, though, since failure to parse expression
903 is different from out-of-scope watchpoint. */
904 if (within_current_scope && b->exp)
906 struct value *val_chain, *v, *result, *next;
908 fetch_watchpoint_value (b->exp, &v, &result, &val_chain);
910 /* Avoid setting b->val if it's already set. The meaning of
911 b->val is 'the last value' user saw, and we should update
912 it only if we reported that last value to user. As it
913 happens, the code that reports it updates b->val directly. */
914 if (!b->val_valid)
916 b->val = v;
917 b->val_valid = 1;
920 /* Change the type of breakpoint between hardware assisted or an
921 ordinary watchpoint depending on the hardware support and free
922 hardware slots. REPARSE is set when the inferior is started. */
923 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
924 && reparse)
926 int i, mem_cnt, other_type_used;
928 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
929 &other_type_used);
930 mem_cnt = can_use_hardware_watchpoint (val_chain);
932 if (!mem_cnt)
933 b->type = bp_watchpoint;
934 else
936 int target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT
937 (bp_hardware_watchpoint, i + mem_cnt, other_type_used);
938 if (target_resources_ok <= 0)
939 b->type = bp_watchpoint;
940 else
941 b->type = bp_hardware_watchpoint;
945 /* Look at each value on the value chain. */
946 for (v = val_chain; v; v = next)
948 /* If it's a memory location, and GDB actually needed
949 its contents to evaluate the expression, then we
950 must watch it. If the first value returned is
951 still lazy, that means an error occurred reading it;
952 watch it anyway in case it becomes readable. */
953 if (VALUE_LVAL (v) == lval_memory
954 && (v == val_chain || ! value_lazy (v)))
956 struct type *vtype = check_typedef (value_type (v));
958 /* We only watch structs and arrays if user asked
959 for it explicitly, never if they just happen to
960 appear in the middle of some value chain. */
961 if (v == result
962 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
963 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
965 CORE_ADDR addr;
966 int len, type;
967 struct bp_location *loc, **tmp;
969 addr = VALUE_ADDRESS (v) + value_offset (v);
970 len = TYPE_LENGTH (value_type (v));
971 type = hw_write;
972 if (b->type == bp_read_watchpoint)
973 type = hw_read;
974 else if (b->type == bp_access_watchpoint)
975 type = hw_access;
977 loc = allocate_bp_location (b);
978 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
980 *tmp = loc;
981 loc->address = addr;
982 loc->length = len;
983 loc->watchpoint_type = type;
987 next = value_next (v);
988 if (v != b->val)
989 value_free (v);
992 /* We just regenerated the list of breakpoint locations.
993 The new location does not have its condition field set to anything
994 and therefore, we must always reparse the cond_string, independently
995 of the value of the reparse flag. */
996 if (b->cond_string != NULL)
998 char *s = b->cond_string;
999 b->loc->cond = parse_exp_1 (&s, b->exp_valid_block, 0);
1002 else if (!within_current_scope)
1004 printf_filtered (_("\
1005 Watchpoint %d deleted because the program has left the block \n\
1006 in which its expression is valid.\n"),
1007 b->number);
1008 if (b->related_breakpoint)
1009 b->related_breakpoint->disposition = disp_del_at_next_stop;
1010 b->disposition = disp_del_at_next_stop;
1013 /* Restore the selected frame. */
1014 select_frame (frame_find_by_id (saved_frame_id));
1018 /* Returns 1 iff breakpoint location should be
1019 inserted in the inferior. */
1020 static int
1021 should_be_inserted (struct bp_location *bpt)
1023 if (!breakpoint_enabled (bpt->owner))
1024 return 0;
1026 if (bpt->owner->disposition == disp_del_at_next_stop)
1027 return 0;
1029 if (!bpt->enabled || bpt->shlib_disabled || bpt->duplicate)
1030 return 0;
1032 return 1;
1035 /* Insert a low-level "breakpoint" of some type. BPT is the breakpoint.
1036 Any error messages are printed to TMP_ERROR_STREAM; and DISABLED_BREAKS,
1037 and HW_BREAKPOINT_ERROR are used to report problems.
1039 NOTE drow/2003-09-09: This routine could be broken down to an object-style
1040 method for each breakpoint or catchpoint type. */
1041 static int
1042 insert_bp_location (struct bp_location *bpt,
1043 struct ui_file *tmp_error_stream,
1044 int *disabled_breaks,
1045 int *hw_breakpoint_error)
1047 int val = 0;
1049 if (!should_be_inserted (bpt) || bpt->inserted)
1050 return 0;
1052 /* Initialize the target-specific information. */
1053 memset (&bpt->target_info, 0, sizeof (bpt->target_info));
1054 bpt->target_info.placed_address = bpt->address;
1056 if (bpt->loc_type == bp_loc_software_breakpoint
1057 || bpt->loc_type == bp_loc_hardware_breakpoint)
1059 if (bpt->owner->type != bp_hardware_breakpoint)
1061 /* If the explicitly specified breakpoint type
1062 is not hardware breakpoint, check the memory map to see
1063 if the breakpoint address is in read only memory or not.
1064 Two important cases are:
1065 - location type is not hardware breakpoint, memory
1066 is readonly. We change the type of the location to
1067 hardware breakpoint.
1068 - location type is hardware breakpoint, memory is read-write.
1069 This means we've previously made the location hardware one, but
1070 then the memory map changed, so we undo.
1072 When breakpoints are removed, remove_breakpoints will
1073 use location types we've just set here, the only possible
1074 problem is that memory map has changed during running program,
1075 but it's not going to work anyway with current gdb. */
1076 struct mem_region *mr
1077 = lookup_mem_region (bpt->target_info.placed_address);
1079 if (mr)
1081 if (automatic_hardware_breakpoints)
1083 int changed = 0;
1084 enum bp_loc_type new_type;
1086 if (mr->attrib.mode != MEM_RW)
1087 new_type = bp_loc_hardware_breakpoint;
1088 else
1089 new_type = bp_loc_software_breakpoint;
1091 if (new_type != bpt->loc_type)
1093 static int said = 0;
1094 bpt->loc_type = new_type;
1095 if (!said)
1097 fprintf_filtered (gdb_stdout, _("\
1098 Note: automatically using hardware breakpoints for read-only addresses.\n"));
1099 said = 1;
1103 else if (bpt->loc_type == bp_loc_software_breakpoint
1104 && mr->attrib.mode != MEM_RW)
1105 warning (_("cannot set software breakpoint at readonly address %s"),
1106 paddr (bpt->address));
1110 /* First check to see if we have to handle an overlay. */
1111 if (overlay_debugging == ovly_off
1112 || bpt->section == NULL
1113 || !(section_is_overlay (bpt->section)))
1115 /* No overlay handling: just set the breakpoint. */
1117 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1118 val = target_insert_hw_breakpoint (&bpt->target_info);
1119 else
1120 val = target_insert_breakpoint (&bpt->target_info);
1122 else
1124 /* This breakpoint is in an overlay section.
1125 Shall we set a breakpoint at the LMA? */
1126 if (!overlay_events_enabled)
1128 /* Yes -- overlay event support is not active,
1129 so we must try to set a breakpoint at the LMA.
1130 This will not work for a hardware breakpoint. */
1131 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1132 warning (_("hardware breakpoint %d not supported in overlay!"),
1133 bpt->owner->number);
1134 else
1136 CORE_ADDR addr = overlay_unmapped_address (bpt->address,
1137 bpt->section);
1138 /* Set a software (trap) breakpoint at the LMA. */
1139 bpt->overlay_target_info = bpt->target_info;
1140 bpt->overlay_target_info.placed_address = addr;
1141 val = target_insert_breakpoint (&bpt->overlay_target_info);
1142 if (val != 0)
1143 fprintf_unfiltered (tmp_error_stream,
1144 "Overlay breakpoint %d failed: in ROM?\n",
1145 bpt->owner->number);
1148 /* Shall we set a breakpoint at the VMA? */
1149 if (section_is_mapped (bpt->section))
1151 /* Yes. This overlay section is mapped into memory. */
1152 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1153 val = target_insert_hw_breakpoint (&bpt->target_info);
1154 else
1155 val = target_insert_breakpoint (&bpt->target_info);
1157 else
1159 /* No. This breakpoint will not be inserted.
1160 No error, but do not mark the bp as 'inserted'. */
1161 return 0;
1165 if (val)
1167 /* Can't set the breakpoint. */
1168 if (solib_name_from_address (bpt->address))
1170 /* See also: disable_breakpoints_in_shlibs. */
1171 val = 0;
1172 bpt->shlib_disabled = 1;
1173 if (!*disabled_breaks)
1175 fprintf_unfiltered (tmp_error_stream,
1176 "Cannot insert breakpoint %d.\n",
1177 bpt->owner->number);
1178 fprintf_unfiltered (tmp_error_stream,
1179 "Temporarily disabling shared library breakpoints:\n");
1181 *disabled_breaks = 1;
1182 fprintf_unfiltered (tmp_error_stream,
1183 "breakpoint #%d\n", bpt->owner->number);
1185 else
1187 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1189 *hw_breakpoint_error = 1;
1190 fprintf_unfiltered (tmp_error_stream,
1191 "Cannot insert hardware breakpoint %d.\n",
1192 bpt->owner->number);
1194 else
1196 fprintf_unfiltered (tmp_error_stream,
1197 "Cannot insert breakpoint %d.\n",
1198 bpt->owner->number);
1199 fprintf_filtered (tmp_error_stream,
1200 "Error accessing memory address ");
1201 fputs_filtered (paddress (bpt->address), tmp_error_stream);
1202 fprintf_filtered (tmp_error_stream, ": %s.\n",
1203 safe_strerror (val));
1208 else
1209 bpt->inserted = 1;
1211 return val;
1214 else if (bpt->loc_type == bp_loc_hardware_watchpoint
1215 /* NOTE drow/2003-09-08: This state only exists for removing
1216 watchpoints. It's not clear that it's necessary... */
1217 && bpt->owner->disposition != disp_del_at_next_stop)
1219 val = target_insert_watchpoint (bpt->address,
1220 bpt->length,
1221 bpt->watchpoint_type);
1222 bpt->inserted = (val != -1);
1225 else if (bpt->owner->type == bp_catchpoint)
1227 struct gdb_exception e = catch_exception (uiout, insert_catchpoint,
1228 bpt->owner, RETURN_MASK_ERROR);
1229 exception_fprintf (gdb_stderr, e, "warning: inserting catchpoint %d: ",
1230 bpt->owner->number);
1231 if (e.reason < 0)
1232 bpt->owner->enable_state = bp_disabled;
1233 else
1234 bpt->inserted = 1;
1236 /* We've already printed an error message if there was a problem
1237 inserting this catchpoint, and we've disabled the catchpoint,
1238 so just return success. */
1239 return 0;
1242 return 0;
1245 /* Make sure all breakpoints are inserted in inferior.
1246 Throws exception on any error.
1247 A breakpoint that is already inserted won't be inserted
1248 again, so calling this function twice is safe. */
1249 void
1250 insert_breakpoints (void)
1252 struct breakpoint *bpt;
1254 ALL_BREAKPOINTS (bpt)
1255 if (is_hardware_watchpoint (bpt))
1256 update_watchpoint (bpt, 0 /* don't reparse. */);
1258 update_global_location_list (1);
1260 if (!breakpoints_always_inserted_mode ()
1261 && (target_has_execution
1262 || (gdbarch_has_global_solist (target_gdbarch)
1263 && target_supports_multi_process ())))
1264 /* update_global_location_list does not insert breakpoints
1265 when always_inserted_mode is not enabled. Explicitly
1266 insert them now. */
1267 insert_breakpoint_locations ();
1270 /* insert_breakpoints is used when starting or continuing the program.
1271 remove_breakpoints is used when the program stops.
1272 Both return zero if successful,
1273 or an `errno' value if could not write the inferior. */
1275 static void
1276 insert_breakpoint_locations (void)
1278 struct breakpoint *bpt;
1279 struct bp_location *b, *temp;
1280 int error = 0;
1281 int val = 0;
1282 int disabled_breaks = 0;
1283 int hw_breakpoint_error = 0;
1285 struct ui_file *tmp_error_stream = mem_fileopen ();
1286 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
1288 /* Explicitly mark the warning -- this will only be printed if
1289 there was an error. */
1290 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1292 ALL_BP_LOCATIONS_SAFE (b, temp)
1294 if (!should_be_inserted (b) || b->inserted)
1295 continue;
1297 /* There is no point inserting thread-specific breakpoints if the
1298 thread no longer exists. */
1299 if (b->owner->thread != -1
1300 && !valid_thread_id (b->owner->thread))
1301 continue;
1303 val = insert_bp_location (b, tmp_error_stream,
1304 &disabled_breaks,
1305 &hw_breakpoint_error);
1306 if (val)
1307 error = val;
1310 /* If we failed to insert all locations of a watchpoint,
1311 remove them, as half-inserted watchpoint is of limited use. */
1312 ALL_BREAKPOINTS (bpt)
1314 int some_failed = 0;
1315 struct bp_location *loc;
1317 if (!is_hardware_watchpoint (bpt))
1318 continue;
1320 if (!breakpoint_enabled (bpt))
1321 continue;
1323 if (bpt->disposition == disp_del_at_next_stop)
1324 continue;
1326 for (loc = bpt->loc; loc; loc = loc->next)
1327 if (!loc->inserted)
1329 some_failed = 1;
1330 break;
1332 if (some_failed)
1334 for (loc = bpt->loc; loc; loc = loc->next)
1335 if (loc->inserted)
1336 remove_breakpoint (loc, mark_uninserted);
1338 hw_breakpoint_error = 1;
1339 fprintf_unfiltered (tmp_error_stream,
1340 "Could not insert hardware watchpoint %d.\n",
1341 bpt->number);
1342 error = -1;
1346 if (error)
1348 /* If a hardware breakpoint or watchpoint was inserted, add a
1349 message about possibly exhausted resources. */
1350 if (hw_breakpoint_error)
1352 fprintf_unfiltered (tmp_error_stream,
1353 "Could not insert hardware breakpoints:\n\
1354 You may have requested too many hardware breakpoints/watchpoints.\n");
1356 target_terminal_ours_for_output ();
1357 error_stream (tmp_error_stream);
1360 do_cleanups (cleanups);
1364 remove_breakpoints (void)
1366 struct bp_location *b;
1367 int val;
1369 ALL_BP_LOCATIONS (b)
1371 if (b->inserted)
1373 val = remove_breakpoint (b, mark_uninserted);
1374 if (val != 0)
1375 return val;
1378 return 0;
1382 remove_hw_watchpoints (void)
1384 struct bp_location *b;
1385 int val;
1387 ALL_BP_LOCATIONS (b)
1389 if (b->inserted && b->loc_type == bp_loc_hardware_watchpoint)
1391 val = remove_breakpoint (b, mark_uninserted);
1392 if (val != 0)
1393 return val;
1396 return 0;
1400 reattach_breakpoints (int pid)
1402 struct bp_location *b;
1403 int val;
1404 struct cleanup *old_chain = save_inferior_ptid ();
1405 struct ui_file *tmp_error_stream = mem_fileopen ();
1406 int dummy1 = 0, dummy2 = 0;
1408 make_cleanup_ui_file_delete (tmp_error_stream);
1410 inferior_ptid = pid_to_ptid (pid);
1411 ALL_BP_LOCATIONS (b)
1413 if (b->inserted)
1415 b->inserted = 0;
1416 val = insert_bp_location (b, tmp_error_stream,
1417 &dummy1, &dummy2);
1418 if (val != 0)
1420 do_cleanups (old_chain);
1421 return val;
1425 do_cleanups (old_chain);
1426 return 0;
1429 void
1430 update_breakpoints_after_exec (void)
1432 struct breakpoint *b;
1433 struct breakpoint *temp;
1434 struct bp_location *bploc;
1436 /* We're about to delete breakpoints from GDB's lists. If the
1437 INSERTED flag is true, GDB will try to lift the breakpoints by
1438 writing the breakpoints' "shadow contents" back into memory. The
1439 "shadow contents" are NOT valid after an exec, so GDB should not
1440 do that. Instead, the target is responsible from marking
1441 breakpoints out as soon as it detects an exec. We don't do that
1442 here instead, because there may be other attempts to delete
1443 breakpoints after detecting an exec and before reaching here. */
1444 ALL_BP_LOCATIONS (bploc)
1445 gdb_assert (!bploc->inserted);
1447 ALL_BREAKPOINTS_SAFE (b, temp)
1449 /* Solib breakpoints must be explicitly reset after an exec(). */
1450 if (b->type == bp_shlib_event)
1452 delete_breakpoint (b);
1453 continue;
1456 /* Thread event breakpoints must be set anew after an exec(),
1457 as must overlay event breakpoints. */
1458 if (b->type == bp_thread_event || b->type == bp_overlay_event)
1460 delete_breakpoint (b);
1461 continue;
1464 /* Step-resume breakpoints are meaningless after an exec(). */
1465 if (b->type == bp_step_resume)
1467 delete_breakpoint (b);
1468 continue;
1471 /* Longjmp and longjmp-resume breakpoints are also meaningless
1472 after an exec. */
1473 if (b->type == bp_longjmp || b->type == bp_longjmp_resume)
1475 delete_breakpoint (b);
1476 continue;
1479 if (b->type == bp_catchpoint)
1481 /* For now, none of the bp_catchpoint breakpoints need to
1482 do anything at this point. In the future, if some of
1483 the catchpoints need to something, we will need to add
1484 a new method, and call this method from here. */
1485 continue;
1488 /* bp_finish is a special case. The only way we ought to be able
1489 to see one of these when an exec() has happened, is if the user
1490 caught a vfork, and then said "finish". Ordinarily a finish just
1491 carries them to the call-site of the current callee, by setting
1492 a temporary bp there and resuming. But in this case, the finish
1493 will carry them entirely through the vfork & exec.
1495 We don't want to allow a bp_finish to remain inserted now. But
1496 we can't safely delete it, 'cause finish_command has a handle to
1497 the bp on a bpstat, and will later want to delete it. There's a
1498 chance (and I've seen it happen) that if we delete the bp_finish
1499 here, that its storage will get reused by the time finish_command
1500 gets 'round to deleting the "use to be a bp_finish" breakpoint.
1501 We really must allow finish_command to delete a bp_finish.
1503 In the absense of a general solution for the "how do we know
1504 it's safe to delete something others may have handles to?"
1505 problem, what we'll do here is just uninsert the bp_finish, and
1506 let finish_command delete it.
1508 (We know the bp_finish is "doomed" in the sense that it's
1509 momentary, and will be deleted as soon as finish_command sees
1510 the inferior stopped. So it doesn't matter that the bp's
1511 address is probably bogus in the new a.out, unlike e.g., the
1512 solib breakpoints.) */
1514 if (b->type == bp_finish)
1516 continue;
1519 /* Without a symbolic address, we have little hope of the
1520 pre-exec() address meaning the same thing in the post-exec()
1521 a.out. */
1522 if (b->addr_string == NULL)
1524 delete_breakpoint (b);
1525 continue;
1528 /* FIXME what about longjmp breakpoints? Re-create them here? */
1529 create_overlay_event_breakpoint ("_ovly_debug_event");
1533 detach_breakpoints (int pid)
1535 struct bp_location *b;
1536 int val;
1537 struct cleanup *old_chain = save_inferior_ptid ();
1539 if (pid == PIDGET (inferior_ptid))
1540 error (_("Cannot detach breakpoints of inferior_ptid"));
1542 /* Set inferior_ptid; remove_breakpoint uses this global. */
1543 inferior_ptid = pid_to_ptid (pid);
1544 ALL_BP_LOCATIONS (b)
1546 if (b->inserted)
1548 val = remove_breakpoint (b, mark_inserted);
1549 if (val != 0)
1551 do_cleanups (old_chain);
1552 return val;
1556 do_cleanups (old_chain);
1557 return 0;
1560 static int
1561 remove_breakpoint (struct bp_location *b, insertion_state_t is)
1563 int val;
1565 if (b->owner->enable_state == bp_permanent)
1566 /* Permanent breakpoints cannot be inserted or removed. */
1567 return 0;
1569 /* The type of none suggests that owner is actually deleted.
1570 This should not ever happen. */
1571 gdb_assert (b->owner->type != bp_none);
1573 if (b->loc_type == bp_loc_software_breakpoint
1574 || b->loc_type == bp_loc_hardware_breakpoint)
1576 /* "Normal" instruction breakpoint: either the standard
1577 trap-instruction bp (bp_breakpoint), or a
1578 bp_hardware_breakpoint. */
1580 /* First check to see if we have to handle an overlay. */
1581 if (overlay_debugging == ovly_off
1582 || b->section == NULL
1583 || !(section_is_overlay (b->section)))
1585 /* No overlay handling: just remove the breakpoint. */
1587 if (b->loc_type == bp_loc_hardware_breakpoint)
1588 val = target_remove_hw_breakpoint (&b->target_info);
1589 else
1590 val = target_remove_breakpoint (&b->target_info);
1592 else
1594 /* This breakpoint is in an overlay section.
1595 Did we set a breakpoint at the LMA? */
1596 if (!overlay_events_enabled)
1598 /* Yes -- overlay event support is not active, so we
1599 should have set a breakpoint at the LMA. Remove it.
1601 /* Ignore any failures: if the LMA is in ROM, we will
1602 have already warned when we failed to insert it. */
1603 if (b->loc_type == bp_loc_hardware_breakpoint)
1604 target_remove_hw_breakpoint (&b->overlay_target_info);
1605 else
1606 target_remove_breakpoint (&b->overlay_target_info);
1608 /* Did we set a breakpoint at the VMA?
1609 If so, we will have marked the breakpoint 'inserted'. */
1610 if (b->inserted)
1612 /* Yes -- remove it. Previously we did not bother to
1613 remove the breakpoint if the section had been
1614 unmapped, but let's not rely on that being safe. We
1615 don't know what the overlay manager might do. */
1616 if (b->loc_type == bp_loc_hardware_breakpoint)
1617 val = target_remove_hw_breakpoint (&b->target_info);
1619 /* However, we should remove *software* breakpoints only
1620 if the section is still mapped, or else we overwrite
1621 wrong code with the saved shadow contents. */
1622 else if (section_is_mapped (b->section))
1623 val = target_remove_breakpoint (&b->target_info);
1624 else
1625 val = 0;
1627 else
1629 /* No -- not inserted, so no need to remove. No error. */
1630 val = 0;
1634 /* In some cases, we might not be able to remove a breakpoint
1635 in a shared library that has already been removed, but we
1636 have not yet processed the shlib unload event. */
1637 if (val && solib_name_from_address (b->address))
1638 val = 0;
1640 if (val)
1641 return val;
1642 b->inserted = (is == mark_inserted);
1644 else if (b->loc_type == bp_loc_hardware_watchpoint)
1646 struct value *v;
1647 struct value *n;
1649 b->inserted = (is == mark_inserted);
1650 val = target_remove_watchpoint (b->address, b->length,
1651 b->watchpoint_type);
1653 /* Failure to remove any of the hardware watchpoints comes here. */
1654 if ((is == mark_uninserted) && (b->inserted))
1655 warning (_("Could not remove hardware watchpoint %d."),
1656 b->owner->number);
1658 else if (b->owner->type == bp_catchpoint
1659 && breakpoint_enabled (b->owner)
1660 && !b->duplicate)
1662 gdb_assert (b->owner->ops != NULL && b->owner->ops->remove != NULL);
1664 val = b->owner->ops->remove (b->owner);
1665 if (val)
1666 return val;
1667 b->inserted = (is == mark_inserted);
1670 return 0;
1673 /* Clear the "inserted" flag in all breakpoints. */
1675 void
1676 mark_breakpoints_out (void)
1678 struct bp_location *bpt;
1680 ALL_BP_LOCATIONS (bpt)
1681 bpt->inserted = 0;
1684 /* Clear the "inserted" flag in all breakpoints and delete any
1685 breakpoints which should go away between runs of the program.
1687 Plus other such housekeeping that has to be done for breakpoints
1688 between runs.
1690 Note: this function gets called at the end of a run (by
1691 generic_mourn_inferior) and when a run begins (by
1692 init_wait_for_inferior). */
1696 void
1697 breakpoint_init_inferior (enum inf_context context)
1699 struct breakpoint *b, *temp;
1700 struct bp_location *bpt;
1701 int ix;
1703 /* If breakpoint locations are shared across processes, then there's
1704 nothing to do. */
1705 if (gdbarch_has_global_solist (target_gdbarch))
1706 return;
1708 ALL_BP_LOCATIONS (bpt)
1709 if (bpt->owner->enable_state != bp_permanent)
1710 bpt->inserted = 0;
1712 ALL_BREAKPOINTS_SAFE (b, temp)
1714 switch (b->type)
1716 case bp_call_dummy:
1717 case bp_watchpoint_scope:
1719 /* If the call dummy breakpoint is at the entry point it will
1720 cause problems when the inferior is rerun, so we better
1721 get rid of it.
1723 Also get rid of scope breakpoints. */
1724 delete_breakpoint (b);
1725 break;
1727 case bp_watchpoint:
1728 case bp_hardware_watchpoint:
1729 case bp_read_watchpoint:
1730 case bp_access_watchpoint:
1732 /* Likewise for watchpoints on local expressions. */
1733 if (b->exp_valid_block != NULL)
1734 delete_breakpoint (b);
1735 else if (context == inf_starting)
1737 /* Reset val field to force reread of starting value
1738 in insert_breakpoints. */
1739 if (b->val)
1740 value_free (b->val);
1741 b->val = NULL;
1742 b->val_valid = 0;
1744 break;
1745 default:
1746 break;
1750 /* Get rid of the moribund locations. */
1751 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bpt); ++ix)
1752 free_bp_location (bpt);
1753 VEC_free (bp_location_p, moribund_locations);
1756 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
1757 exists at PC. It returns ordinary_breakpoint_here if it's an
1758 ordinary breakpoint, or permanent_breakpoint_here if it's a
1759 permanent breakpoint.
1760 - When continuing from a location with an ordinary breakpoint, we
1761 actually single step once before calling insert_breakpoints.
1762 - When continuing from a localion with a permanent breakpoint, we
1763 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
1764 the target, to advance the PC past the breakpoint. */
1766 enum breakpoint_here
1767 breakpoint_here_p (CORE_ADDR pc)
1769 const struct bp_location *bpt;
1770 int any_breakpoint_here = 0;
1772 ALL_BP_LOCATIONS (bpt)
1774 if (bpt->loc_type != bp_loc_software_breakpoint
1775 && bpt->loc_type != bp_loc_hardware_breakpoint)
1776 continue;
1778 if ((breakpoint_enabled (bpt->owner)
1779 || bpt->owner->enable_state == bp_permanent)
1780 && bpt->address == pc) /* bp is enabled and matches pc */
1782 if (overlay_debugging
1783 && section_is_overlay (bpt->section)
1784 && !section_is_mapped (bpt->section))
1785 continue; /* unmapped overlay -- can't be a match */
1786 else if (bpt->owner->enable_state == bp_permanent)
1787 return permanent_breakpoint_here;
1788 else
1789 any_breakpoint_here = 1;
1793 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
1796 /* Return true if there's a moribund breakpoint at PC. */
1799 moribund_breakpoint_here_p (CORE_ADDR pc)
1801 struct bp_location *loc;
1802 int ix;
1804 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
1805 if (loc->address == pc)
1806 return 1;
1808 return 0;
1811 /* Returns non-zero if there's a breakpoint inserted at PC, which is
1812 inserted using regular breakpoint_chain/bp_location_chain mechanism.
1813 This does not check for single-step breakpoints, which are
1814 inserted and removed using direct target manipulation. */
1817 regular_breakpoint_inserted_here_p (CORE_ADDR pc)
1819 const struct bp_location *bpt;
1821 ALL_BP_LOCATIONS (bpt)
1823 if (bpt->loc_type != bp_loc_software_breakpoint
1824 && bpt->loc_type != bp_loc_hardware_breakpoint)
1825 continue;
1827 if (bpt->inserted
1828 && bpt->address == pc) /* bp is inserted and matches pc */
1830 if (overlay_debugging
1831 && section_is_overlay (bpt->section)
1832 && !section_is_mapped (bpt->section))
1833 continue; /* unmapped overlay -- can't be a match */
1834 else
1835 return 1;
1838 return 0;
1841 /* Returns non-zero iff there's either regular breakpoint
1842 or a single step breakpoint inserted at PC. */
1845 breakpoint_inserted_here_p (CORE_ADDR pc)
1847 if (regular_breakpoint_inserted_here_p (pc))
1848 return 1;
1850 if (single_step_breakpoint_inserted_here_p (pc))
1851 return 1;
1853 return 0;
1856 /* This function returns non-zero iff there is a software breakpoint
1857 inserted at PC. */
1860 software_breakpoint_inserted_here_p (CORE_ADDR pc)
1862 const struct bp_location *bpt;
1863 int any_breakpoint_here = 0;
1865 ALL_BP_LOCATIONS (bpt)
1867 if (bpt->loc_type != bp_loc_software_breakpoint)
1868 continue;
1870 if (bpt->inserted
1871 && bpt->address == pc) /* bp is enabled and matches pc */
1873 if (overlay_debugging
1874 && section_is_overlay (bpt->section)
1875 && !section_is_mapped (bpt->section))
1876 continue; /* unmapped overlay -- can't be a match */
1877 else
1878 return 1;
1882 /* Also check for software single-step breakpoints. */
1883 if (single_step_breakpoint_inserted_here_p (pc))
1884 return 1;
1886 return 0;
1889 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
1890 PC is valid for process/thread PTID. */
1893 breakpoint_thread_match (CORE_ADDR pc, ptid_t ptid)
1895 const struct bp_location *bpt;
1896 int thread;
1898 thread = pid_to_thread_id (ptid);
1900 ALL_BP_LOCATIONS (bpt)
1902 if (bpt->loc_type != bp_loc_software_breakpoint
1903 && bpt->loc_type != bp_loc_hardware_breakpoint)
1904 continue;
1906 if ((breakpoint_enabled (bpt->owner)
1907 || bpt->owner->enable_state == bp_permanent)
1908 && bpt->address == pc
1909 && (bpt->owner->thread == -1 || bpt->owner->thread == thread))
1911 if (overlay_debugging
1912 && section_is_overlay (bpt->section)
1913 && !section_is_mapped (bpt->section))
1914 continue; /* unmapped overlay -- can't be a match */
1915 else
1916 return 1;
1920 return 0;
1924 /* bpstat stuff. External routines' interfaces are documented
1925 in breakpoint.h. */
1928 ep_is_catchpoint (struct breakpoint *ep)
1930 return (ep->type == bp_catchpoint);
1933 void
1934 bpstat_free (bpstat bs)
1936 if (bs->old_val != NULL)
1937 value_free (bs->old_val);
1938 free_command_lines (&bs->commands);
1939 xfree (bs);
1942 /* Clear a bpstat so that it says we are not at any breakpoint.
1943 Also free any storage that is part of a bpstat. */
1945 void
1946 bpstat_clear (bpstat *bsp)
1948 bpstat p;
1949 bpstat q;
1951 if (bsp == 0)
1952 return;
1953 p = *bsp;
1954 while (p != NULL)
1956 q = p->next;
1957 bpstat_free (p);
1958 p = q;
1960 *bsp = NULL;
1963 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
1964 is part of the bpstat is copied as well. */
1966 bpstat
1967 bpstat_copy (bpstat bs)
1969 bpstat p = NULL;
1970 bpstat tmp;
1971 bpstat retval = NULL;
1973 if (bs == NULL)
1974 return bs;
1976 for (; bs != NULL; bs = bs->next)
1978 tmp = (bpstat) xmalloc (sizeof (*tmp));
1979 memcpy (tmp, bs, sizeof (*tmp));
1980 if (bs->commands != NULL)
1981 tmp->commands = copy_command_lines (bs->commands);
1982 if (bs->old_val != NULL)
1984 tmp->old_val = value_copy (bs->old_val);
1985 release_value (tmp->old_val);
1988 if (p == NULL)
1989 /* This is the first thing in the chain. */
1990 retval = tmp;
1991 else
1992 p->next = tmp;
1993 p = tmp;
1995 p->next = NULL;
1996 return retval;
1999 /* Find the bpstat associated with this breakpoint */
2001 bpstat
2002 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
2004 if (bsp == NULL)
2005 return NULL;
2007 for (; bsp != NULL; bsp = bsp->next)
2009 if (bsp->breakpoint_at && bsp->breakpoint_at->owner == breakpoint)
2010 return bsp;
2012 return NULL;
2015 /* Find a step_resume breakpoint associated with this bpstat.
2016 (If there are multiple step_resume bp's on the list, this function
2017 will arbitrarily pick one.)
2019 It is an error to use this function if BPSTAT doesn't contain a
2020 step_resume breakpoint.
2022 See wait_for_inferior's use of this function. */
2023 struct breakpoint *
2024 bpstat_find_step_resume_breakpoint (bpstat bsp)
2026 int current_thread;
2028 gdb_assert (bsp != NULL);
2030 current_thread = pid_to_thread_id (inferior_ptid);
2032 for (; bsp != NULL; bsp = bsp->next)
2034 if ((bsp->breakpoint_at != NULL) &&
2035 (bsp->breakpoint_at->owner->type == bp_step_resume) &&
2036 (bsp->breakpoint_at->owner->thread == current_thread ||
2037 bsp->breakpoint_at->owner->thread == -1))
2038 return bsp->breakpoint_at->owner;
2041 internal_error (__FILE__, __LINE__, _("No step_resume breakpoint found."));
2045 /* Put in *NUM the breakpoint number of the first breakpoint we are stopped
2046 at. *BSP upon return is a bpstat which points to the remaining
2047 breakpoints stopped at (but which is not guaranteed to be good for
2048 anything but further calls to bpstat_num).
2049 Return 0 if passed a bpstat which does not indicate any breakpoints.
2050 Return -1 if stopped at a breakpoint that has been deleted since
2051 we set it.
2052 Return 1 otherwise. */
2055 bpstat_num (bpstat *bsp, int *num)
2057 struct breakpoint *b;
2059 if ((*bsp) == NULL)
2060 return 0; /* No more breakpoint values */
2062 /* We assume we'll never have several bpstats that
2063 correspond to a single breakpoint -- otherwise,
2064 this function might return the same number more
2065 than once and this will look ugly. */
2066 b = (*bsp)->breakpoint_at ? (*bsp)->breakpoint_at->owner : NULL;
2067 *bsp = (*bsp)->next;
2068 if (b == NULL)
2069 return -1; /* breakpoint that's been deleted since */
2071 *num = b->number; /* We have its number */
2072 return 1;
2075 /* Modify BS so that the actions will not be performed. */
2077 void
2078 bpstat_clear_actions (bpstat bs)
2080 for (; bs != NULL; bs = bs->next)
2082 free_command_lines (&bs->commands);
2083 if (bs->old_val != NULL)
2085 value_free (bs->old_val);
2086 bs->old_val = NULL;
2091 /* Called when a command is about to proceed the inferior. */
2093 static void
2094 breakpoint_about_to_proceed (void)
2096 if (!ptid_equal (inferior_ptid, null_ptid))
2098 struct thread_info *tp = inferior_thread ();
2100 /* Allow inferior function calls in breakpoint commands to not
2101 interrupt the command list. When the call finishes
2102 successfully, the inferior will be standing at the same
2103 breakpoint as if nothing happened. */
2104 if (tp->in_infcall)
2105 return;
2108 breakpoint_proceeded = 1;
2111 /* Stub for cleaning up our state if we error-out of a breakpoint command */
2112 static void
2113 cleanup_executing_breakpoints (void *ignore)
2115 executing_breakpoint_commands = 0;
2118 /* Execute all the commands associated with all the breakpoints at this
2119 location. Any of these commands could cause the process to proceed
2120 beyond this point, etc. We look out for such changes by checking
2121 the global "breakpoint_proceeded" after each command.
2123 Returns true if a breakpoint command resumed the inferior. In that
2124 case, it is the caller's responsibility to recall it again with the
2125 bpstat of the current thread. */
2127 static int
2128 bpstat_do_actions_1 (bpstat *bsp)
2130 bpstat bs;
2131 struct cleanup *old_chain;
2132 int again = 0;
2134 /* Avoid endless recursion if a `source' command is contained
2135 in bs->commands. */
2136 if (executing_breakpoint_commands)
2137 return 0;
2139 executing_breakpoint_commands = 1;
2140 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
2142 /* This pointer will iterate over the list of bpstat's. */
2143 bs = *bsp;
2145 breakpoint_proceeded = 0;
2146 for (; bs != NULL; bs = bs->next)
2148 struct command_line *cmd;
2149 struct cleanup *this_cmd_tree_chain;
2151 /* Take ownership of the BSP's command tree, if it has one.
2153 The command tree could legitimately contain commands like
2154 'step' and 'next', which call clear_proceed_status, which
2155 frees stop_bpstat's command tree. To make sure this doesn't
2156 free the tree we're executing out from under us, we need to
2157 take ownership of the tree ourselves. Since a given bpstat's
2158 commands are only executed once, we don't need to copy it; we
2159 can clear the pointer in the bpstat, and make sure we free
2160 the tree when we're done. */
2161 cmd = bs->commands;
2162 bs->commands = 0;
2163 this_cmd_tree_chain = make_cleanup_free_command_lines (&cmd);
2165 while (cmd != NULL)
2167 execute_control_command (cmd);
2169 if (breakpoint_proceeded)
2170 break;
2171 else
2172 cmd = cmd->next;
2175 /* We can free this command tree now. */
2176 do_cleanups (this_cmd_tree_chain);
2178 if (breakpoint_proceeded)
2180 if (target_can_async_p ())
2181 /* If we are in async mode, then the target might be still
2182 running, not stopped at any breakpoint, so nothing for
2183 us to do here -- just return to the event loop. */
2185 else
2186 /* In sync mode, when execute_control_command returns
2187 we're already standing on the next breakpoint.
2188 Breakpoint commands for that stop were not run, since
2189 execute_command does not run breakpoint commands --
2190 only command_line_handler does, but that one is not
2191 involved in execution of breakpoint commands. So, we
2192 can now execute breakpoint commands. It should be
2193 noted that making execute_command do bpstat actions is
2194 not an option -- in this case we'll have recursive
2195 invocation of bpstat for each breakpoint with a
2196 command, and can easily blow up GDB stack. Instead, we
2197 return true, which will trigger the caller to recall us
2198 with the new stop_bpstat. */
2199 again = 1;
2200 break;
2203 do_cleanups (old_chain);
2204 return again;
2207 void
2208 bpstat_do_actions (void)
2210 /* Do any commands attached to breakpoint we are stopped at. */
2211 while (!ptid_equal (inferior_ptid, null_ptid)
2212 && target_has_execution
2213 && !is_exited (inferior_ptid)
2214 && !is_executing (inferior_ptid))
2215 /* Since in sync mode, bpstat_do_actions may resume the inferior,
2216 and only return when it is stopped at the next breakpoint, we
2217 keep doing breakpoint actions until it returns false to
2218 indicate the inferior was not resumed. */
2219 if (!bpstat_do_actions_1 (&inferior_thread ()->stop_bpstat))
2220 break;
2223 /* Print out the (old or new) value associated with a watchpoint. */
2225 static void
2226 watchpoint_value_print (struct value *val, struct ui_file *stream)
2228 if (val == NULL)
2229 fprintf_unfiltered (stream, _("<unreadable>"));
2230 else
2232 struct value_print_options opts;
2233 get_user_print_options (&opts);
2234 value_print (val, stream, &opts);
2238 /* This is the normal print function for a bpstat. In the future,
2239 much of this logic could (should?) be moved to bpstat_stop_status,
2240 by having it set different print_it values.
2242 Current scheme: When we stop, bpstat_print() is called. It loops
2243 through the bpstat list of things causing this stop, calling the
2244 print_bp_stop_message function on each one. The behavior of the
2245 print_bp_stop_message function depends on the print_it field of
2246 bpstat. If such field so indicates, call this function here.
2248 Return values from this routine (ultimately used by bpstat_print()
2249 and normal_stop() to decide what to do):
2250 PRINT_NOTHING: Means we already printed all we needed to print,
2251 don't print anything else.
2252 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
2253 that something to be followed by a location.
2254 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
2255 that something to be followed by a location.
2256 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
2257 analysis. */
2259 static enum print_stop_action
2260 print_it_typical (bpstat bs)
2262 struct cleanup *old_chain;
2263 struct breakpoint *b;
2264 const struct bp_location *bl;
2265 struct ui_stream *stb;
2266 int bp_temp = 0;
2267 enum print_stop_action result;
2269 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
2270 which has since been deleted. */
2271 if (bs->breakpoint_at == NULL)
2272 return PRINT_UNKNOWN;
2273 bl = bs->breakpoint_at;
2274 b = bl->owner;
2276 stb = ui_out_stream_new (uiout);
2277 old_chain = make_cleanup_ui_out_stream_delete (stb);
2279 switch (b->type)
2281 case bp_breakpoint:
2282 case bp_hardware_breakpoint:
2283 bp_temp = bs->breakpoint_at->owner->disposition == disp_del;
2284 if (bl->address != bl->requested_address)
2285 breakpoint_adjustment_warning (bl->requested_address,
2286 bl->address,
2287 b->number, 1);
2288 annotate_breakpoint (b->number);
2289 if (bp_temp)
2290 ui_out_text (uiout, "\nTemporary breakpoint ");
2291 else
2292 ui_out_text (uiout, "\nBreakpoint ");
2293 if (ui_out_is_mi_like_p (uiout))
2295 ui_out_field_string (uiout, "reason",
2296 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
2297 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
2299 ui_out_field_int (uiout, "bkptno", b->number);
2300 ui_out_text (uiout, ", ");
2301 result = PRINT_SRC_AND_LOC;
2302 break;
2304 case bp_shlib_event:
2305 /* Did we stop because the user set the stop_on_solib_events
2306 variable? (If so, we report this as a generic, "Stopped due
2307 to shlib event" message.) */
2308 printf_filtered (_("Stopped due to shared library event\n"));
2309 result = PRINT_NOTHING;
2310 break;
2312 case bp_thread_event:
2313 /* Not sure how we will get here.
2314 GDB should not stop for these breakpoints. */
2315 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
2316 result = PRINT_NOTHING;
2317 break;
2319 case bp_overlay_event:
2320 /* By analogy with the thread event, GDB should not stop for these. */
2321 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
2322 result = PRINT_NOTHING;
2323 break;
2325 case bp_watchpoint:
2326 case bp_hardware_watchpoint:
2327 annotate_watchpoint (b->number);
2328 if (ui_out_is_mi_like_p (uiout))
2329 ui_out_field_string
2330 (uiout, "reason",
2331 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
2332 mention (b);
2333 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2334 ui_out_text (uiout, "\nOld value = ");
2335 watchpoint_value_print (bs->old_val, stb->stream);
2336 ui_out_field_stream (uiout, "old", stb);
2337 ui_out_text (uiout, "\nNew value = ");
2338 watchpoint_value_print (b->val, stb->stream);
2339 ui_out_field_stream (uiout, "new", stb);
2340 ui_out_text (uiout, "\n");
2341 /* More than one watchpoint may have been triggered. */
2342 result = PRINT_UNKNOWN;
2343 break;
2345 case bp_read_watchpoint:
2346 if (ui_out_is_mi_like_p (uiout))
2347 ui_out_field_string
2348 (uiout, "reason",
2349 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
2350 mention (b);
2351 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2352 ui_out_text (uiout, "\nValue = ");
2353 watchpoint_value_print (b->val, stb->stream);
2354 ui_out_field_stream (uiout, "value", stb);
2355 ui_out_text (uiout, "\n");
2356 result = PRINT_UNKNOWN;
2357 break;
2359 case bp_access_watchpoint:
2360 if (bs->old_val != NULL)
2362 annotate_watchpoint (b->number);
2363 if (ui_out_is_mi_like_p (uiout))
2364 ui_out_field_string
2365 (uiout, "reason",
2366 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
2367 mention (b);
2368 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2369 ui_out_text (uiout, "\nOld value = ");
2370 watchpoint_value_print (bs->old_val, stb->stream);
2371 ui_out_field_stream (uiout, "old", stb);
2372 ui_out_text (uiout, "\nNew value = ");
2374 else
2376 mention (b);
2377 if (ui_out_is_mi_like_p (uiout))
2378 ui_out_field_string
2379 (uiout, "reason",
2380 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
2381 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2382 ui_out_text (uiout, "\nValue = ");
2384 watchpoint_value_print (b->val, stb->stream);
2385 ui_out_field_stream (uiout, "new", stb);
2386 ui_out_text (uiout, "\n");
2387 result = PRINT_UNKNOWN;
2388 break;
2390 /* Fall through, we don't deal with these types of breakpoints
2391 here. */
2393 case bp_finish:
2394 if (ui_out_is_mi_like_p (uiout))
2395 ui_out_field_string
2396 (uiout, "reason",
2397 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
2398 result = PRINT_UNKNOWN;
2399 break;
2401 case bp_until:
2402 if (ui_out_is_mi_like_p (uiout))
2403 ui_out_field_string
2404 (uiout, "reason",
2405 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
2406 result = PRINT_UNKNOWN;
2407 break;
2409 case bp_none:
2410 case bp_longjmp:
2411 case bp_longjmp_resume:
2412 case bp_step_resume:
2413 case bp_watchpoint_scope:
2414 case bp_call_dummy:
2415 default:
2416 result = PRINT_UNKNOWN;
2417 break;
2420 do_cleanups (old_chain);
2421 return result;
2424 /* Generic routine for printing messages indicating why we
2425 stopped. The behavior of this function depends on the value
2426 'print_it' in the bpstat structure. Under some circumstances we
2427 may decide not to print anything here and delegate the task to
2428 normal_stop(). */
2430 static enum print_stop_action
2431 print_bp_stop_message (bpstat bs)
2433 switch (bs->print_it)
2435 case print_it_noop:
2436 /* Nothing should be printed for this bpstat entry. */
2437 return PRINT_UNKNOWN;
2438 break;
2440 case print_it_done:
2441 /* We still want to print the frame, but we already printed the
2442 relevant messages. */
2443 return PRINT_SRC_AND_LOC;
2444 break;
2446 case print_it_normal:
2448 const struct bp_location *bl = bs->breakpoint_at;
2449 struct breakpoint *b = bl ? bl->owner : NULL;
2451 /* Normal case. Call the breakpoint's print_it method, or
2452 print_it_typical. */
2453 /* FIXME: how breakpoint can ever be NULL here? */
2454 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
2455 return b->ops->print_it (b);
2456 else
2457 return print_it_typical (bs);
2459 break;
2461 default:
2462 internal_error (__FILE__, __LINE__,
2463 _("print_bp_stop_message: unrecognized enum value"));
2464 break;
2468 /* Print a message indicating what happened. This is called from
2469 normal_stop(). The input to this routine is the head of the bpstat
2470 list - a list of the eventpoints that caused this stop. This
2471 routine calls the generic print routine for printing a message
2472 about reasons for stopping. This will print (for example) the
2473 "Breakpoint n," part of the output. The return value of this
2474 routine is one of:
2476 PRINT_UNKNOWN: Means we printed nothing
2477 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
2478 code to print the location. An example is
2479 "Breakpoint 1, " which should be followed by
2480 the location.
2481 PRINT_SRC_ONLY: Means we printed something, but there is no need
2482 to also print the location part of the message.
2483 An example is the catch/throw messages, which
2484 don't require a location appended to the end.
2485 PRINT_NOTHING: We have done some printing and we don't need any
2486 further info to be printed.*/
2488 enum print_stop_action
2489 bpstat_print (bpstat bs)
2491 int val;
2493 /* Maybe another breakpoint in the chain caused us to stop.
2494 (Currently all watchpoints go on the bpstat whether hit or not.
2495 That probably could (should) be changed, provided care is taken
2496 with respect to bpstat_explains_signal). */
2497 for (; bs; bs = bs->next)
2499 val = print_bp_stop_message (bs);
2500 if (val == PRINT_SRC_ONLY
2501 || val == PRINT_SRC_AND_LOC
2502 || val == PRINT_NOTHING)
2503 return val;
2506 /* We reached the end of the chain, or we got a null BS to start
2507 with and nothing was printed. */
2508 return PRINT_UNKNOWN;
2511 /* Evaluate the expression EXP and return 1 if value is zero.
2512 This is used inside a catch_errors to evaluate the breakpoint condition.
2513 The argument is a "struct expression *" that has been cast to char * to
2514 make it pass through catch_errors. */
2516 static int
2517 breakpoint_cond_eval (void *exp)
2519 struct value *mark = value_mark ();
2520 int i = !value_true (evaluate_expression ((struct expression *) exp));
2521 value_free_to_mark (mark);
2522 return i;
2525 /* Allocate a new bpstat and chain it to the current one. */
2527 static bpstat
2528 bpstat_alloc (const struct bp_location *bl, bpstat cbs /* Current "bs" value */ )
2530 bpstat bs;
2532 bs = (bpstat) xmalloc (sizeof (*bs));
2533 cbs->next = bs;
2534 bs->breakpoint_at = bl;
2535 /* If the condition is false, etc., don't do the commands. */
2536 bs->commands = NULL;
2537 bs->old_val = NULL;
2538 bs->print_it = print_it_normal;
2539 return bs;
2542 /* The target has stopped with waitstatus WS. Check if any hardware
2543 watchpoints have triggered, according to the target. */
2546 watchpoints_triggered (struct target_waitstatus *ws)
2548 int stopped_by_watchpoint = STOPPED_BY_WATCHPOINT (*ws);
2549 CORE_ADDR addr;
2550 struct breakpoint *b;
2552 if (!stopped_by_watchpoint)
2554 /* We were not stopped by a watchpoint. Mark all watchpoints
2555 as not triggered. */
2556 ALL_BREAKPOINTS (b)
2557 if (b->type == bp_hardware_watchpoint
2558 || b->type == bp_read_watchpoint
2559 || b->type == bp_access_watchpoint)
2560 b->watchpoint_triggered = watch_triggered_no;
2562 return 0;
2565 if (!target_stopped_data_address (&current_target, &addr))
2567 /* We were stopped by a watchpoint, but we don't know where.
2568 Mark all watchpoints as unknown. */
2569 ALL_BREAKPOINTS (b)
2570 if (b->type == bp_hardware_watchpoint
2571 || b->type == bp_read_watchpoint
2572 || b->type == bp_access_watchpoint)
2573 b->watchpoint_triggered = watch_triggered_unknown;
2575 return stopped_by_watchpoint;
2578 /* The target could report the data address. Mark watchpoints
2579 affected by this data address as triggered, and all others as not
2580 triggered. */
2582 ALL_BREAKPOINTS (b)
2583 if (b->type == bp_hardware_watchpoint
2584 || b->type == bp_read_watchpoint
2585 || b->type == bp_access_watchpoint)
2587 struct bp_location *loc;
2588 struct value *v;
2590 b->watchpoint_triggered = watch_triggered_no;
2591 for (loc = b->loc; loc; loc = loc->next)
2592 /* Exact match not required. Within range is
2593 sufficient. */
2594 if (target_watchpoint_addr_within_range (&current_target,
2595 addr, loc->address,
2596 loc->length))
2598 b->watchpoint_triggered = watch_triggered_yes;
2599 break;
2603 return 1;
2606 /* Possible return values for watchpoint_check (this can't be an enum
2607 because of check_errors). */
2608 /* The watchpoint has been deleted. */
2609 #define WP_DELETED 1
2610 /* The value has changed. */
2611 #define WP_VALUE_CHANGED 2
2612 /* The value has not changed. */
2613 #define WP_VALUE_NOT_CHANGED 3
2615 #define BP_TEMPFLAG 1
2616 #define BP_HARDWAREFLAG 2
2618 /* Check watchpoint condition. */
2620 static int
2621 watchpoint_check (void *p)
2623 bpstat bs = (bpstat) p;
2624 struct breakpoint *b;
2625 struct frame_info *fr;
2626 int within_current_scope;
2628 b = bs->breakpoint_at->owner;
2630 if (b->exp_valid_block == NULL)
2631 within_current_scope = 1;
2632 else
2634 /* There is no current frame at this moment. If we're going to have
2635 any chance of handling watchpoints on local variables, we'll need
2636 the frame chain (so we can determine if we're in scope). */
2637 reinit_frame_cache ();
2638 fr = frame_find_by_id (b->watchpoint_frame);
2639 within_current_scope = (fr != NULL);
2641 /* If we've gotten confused in the unwinder, we might have
2642 returned a frame that can't describe this variable. */
2643 if (within_current_scope
2644 && (block_linkage_function (b->exp_valid_block)
2645 != get_frame_function (fr)))
2646 within_current_scope = 0;
2648 /* in_function_epilogue_p() returns a non-zero value if we're still
2649 in the function but the stack frame has already been invalidated.
2650 Since we can't rely on the values of local variables after the
2651 stack has been destroyed, we are treating the watchpoint in that
2652 state as `not changed' without further checking.
2654 vinschen/2003-09-04: The former implementation left out the case
2655 that the watchpoint frame couldn't be found by frame_find_by_id()
2656 because the current PC is currently in an epilogue. Calling
2657 gdbarch_in_function_epilogue_p() also when fr == NULL fixes that. */
2658 if ((!within_current_scope || fr == get_current_frame ())
2659 && gdbarch_in_function_epilogue_p (current_gdbarch, read_pc ()))
2660 return WP_VALUE_NOT_CHANGED;
2661 if (fr && within_current_scope)
2662 /* If we end up stopping, the current frame will get selected
2663 in normal_stop. So this call to select_frame won't affect
2664 the user. */
2665 select_frame (fr);
2668 if (within_current_scope)
2670 /* We use value_{,free_to_}mark because it could be a
2671 *long* time before we return to the command level and
2672 call free_all_values. We can't call free_all_values because
2673 we might be in the middle of evaluating a function call. */
2675 struct value *mark = value_mark ();
2676 struct value *new_val;
2678 fetch_watchpoint_value (b->exp, &new_val, NULL, NULL);
2679 if ((b->val != NULL) != (new_val != NULL)
2680 || (b->val != NULL && !value_equal (b->val, new_val)))
2682 if (new_val != NULL)
2684 release_value (new_val);
2685 value_free_to_mark (mark);
2687 bs->old_val = b->val;
2688 b->val = new_val;
2689 b->val_valid = 1;
2690 /* We will stop here */
2691 return WP_VALUE_CHANGED;
2693 else
2695 /* Nothing changed, don't do anything. */
2696 value_free_to_mark (mark);
2697 /* We won't stop here */
2698 return WP_VALUE_NOT_CHANGED;
2701 else
2703 /* This seems like the only logical thing to do because
2704 if we temporarily ignored the watchpoint, then when
2705 we reenter the block in which it is valid it contains
2706 garbage (in the case of a function, it may have two
2707 garbage values, one before and one after the prologue).
2708 So we can't even detect the first assignment to it and
2709 watch after that (since the garbage may or may not equal
2710 the first value assigned). */
2711 /* We print all the stop information in print_it_typical(), but
2712 in this case, by the time we call print_it_typical() this bp
2713 will be deleted already. So we have no choice but print the
2714 information here. */
2715 if (ui_out_is_mi_like_p (uiout))
2716 ui_out_field_string
2717 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
2718 ui_out_text (uiout, "\nWatchpoint ");
2719 ui_out_field_int (uiout, "wpnum", b->number);
2720 ui_out_text (uiout, " deleted because the program has left the block in\n\
2721 which its expression is valid.\n");
2723 if (b->related_breakpoint)
2724 b->related_breakpoint->disposition = disp_del_at_next_stop;
2725 b->disposition = disp_del_at_next_stop;
2727 return WP_DELETED;
2731 /* Return true if it looks like target has stopped due to hitting
2732 breakpoint location BL. This function does not check if we
2733 should stop, only if BL explains the stop. */
2734 static int
2735 bpstat_check_location (const struct bp_location *bl, CORE_ADDR bp_addr)
2737 struct breakpoint *b = bl->owner;
2739 if (b->type != bp_watchpoint
2740 && b->type != bp_hardware_watchpoint
2741 && b->type != bp_read_watchpoint
2742 && b->type != bp_access_watchpoint
2743 && b->type != bp_hardware_breakpoint
2744 && b->type != bp_catchpoint) /* a non-watchpoint bp */
2746 if (bl->address != bp_addr) /* address doesn't match */
2747 return 0;
2748 if (overlay_debugging /* unmapped overlay section */
2749 && section_is_overlay (bl->section)
2750 && !section_is_mapped (bl->section))
2751 return 0;
2754 /* Continuable hardware watchpoints are treated as non-existent if the
2755 reason we stopped wasn't a hardware watchpoint (we didn't stop on
2756 some data address). Otherwise gdb won't stop on a break instruction
2757 in the code (not from a breakpoint) when a hardware watchpoint has
2758 been defined. Also skip watchpoints which we know did not trigger
2759 (did not match the data address). */
2761 if ((b->type == bp_hardware_watchpoint
2762 || b->type == bp_read_watchpoint
2763 || b->type == bp_access_watchpoint)
2764 && b->watchpoint_triggered == watch_triggered_no)
2765 return 0;
2767 if (b->type == bp_hardware_breakpoint)
2769 if (bl->address != bp_addr)
2770 return 0;
2771 if (overlay_debugging /* unmapped overlay section */
2772 && section_is_overlay (bl->section)
2773 && !section_is_mapped (bl->section))
2774 return 0;
2777 if (b->type == bp_catchpoint)
2779 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
2780 if (!b->ops->breakpoint_hit (b))
2781 return 0;
2784 return 1;
2787 /* If BS refers to a watchpoint, determine if the watched values
2788 has actually changed, and we should stop. If not, set BS->stop
2789 to 0. */
2790 static void
2791 bpstat_check_watchpoint (bpstat bs)
2793 const struct bp_location *bl = bs->breakpoint_at;
2794 struct breakpoint *b = bl->owner;
2796 if (b->type == bp_watchpoint
2797 || b->type == bp_read_watchpoint
2798 || b->type == bp_access_watchpoint
2799 || b->type == bp_hardware_watchpoint)
2801 CORE_ADDR addr;
2802 struct value *v;
2803 int must_check_value = 0;
2805 if (b->type == bp_watchpoint)
2806 /* For a software watchpoint, we must always check the
2807 watched value. */
2808 must_check_value = 1;
2809 else if (b->watchpoint_triggered == watch_triggered_yes)
2810 /* We have a hardware watchpoint (read, write, or access)
2811 and the target earlier reported an address watched by
2812 this watchpoint. */
2813 must_check_value = 1;
2814 else if (b->watchpoint_triggered == watch_triggered_unknown
2815 && b->type == bp_hardware_watchpoint)
2816 /* We were stopped by a hardware watchpoint, but the target could
2817 not report the data address. We must check the watchpoint's
2818 value. Access and read watchpoints are out of luck; without
2819 a data address, we can't figure it out. */
2820 must_check_value = 1;
2822 if (must_check_value)
2824 char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
2825 b->number);
2826 struct cleanup *cleanups = make_cleanup (xfree, message);
2827 int e = catch_errors (watchpoint_check, bs, message,
2828 RETURN_MASK_ALL);
2829 do_cleanups (cleanups);
2830 switch (e)
2832 case WP_DELETED:
2833 /* We've already printed what needs to be printed. */
2834 bs->print_it = print_it_done;
2835 /* Stop. */
2836 break;
2837 case WP_VALUE_CHANGED:
2838 if (b->type == bp_read_watchpoint)
2840 /* Don't stop: read watchpoints shouldn't fire if
2841 the value has changed. This is for targets
2842 which cannot set read-only watchpoints. */
2843 bs->print_it = print_it_noop;
2844 bs->stop = 0;
2846 break;
2847 case WP_VALUE_NOT_CHANGED:
2848 if (b->type == bp_hardware_watchpoint
2849 || b->type == bp_watchpoint)
2851 /* Don't stop: write watchpoints shouldn't fire if
2852 the value hasn't changed. */
2853 bs->print_it = print_it_noop;
2854 bs->stop = 0;
2856 /* Stop. */
2857 break;
2858 default:
2859 /* Can't happen. */
2860 case 0:
2861 /* Error from catch_errors. */
2862 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
2863 if (b->related_breakpoint)
2864 b->related_breakpoint->disposition = disp_del_at_next_stop;
2865 b->disposition = disp_del_at_next_stop;
2866 /* We've already printed what needs to be printed. */
2867 bs->print_it = print_it_done;
2868 break;
2871 else /* must_check_value == 0 */
2873 /* This is a case where some watchpoint(s) triggered, but
2874 not at the address of this watchpoint, or else no
2875 watchpoint triggered after all. So don't print
2876 anything for this watchpoint. */
2877 bs->print_it = print_it_noop;
2878 bs->stop = 0;
2884 /* Check conditions (condition proper, frame, thread and ignore count)
2885 of breakpoint referred to by BS. If we should not stop for this
2886 breakpoint, set BS->stop to 0. */
2887 static void
2888 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
2890 int thread_id = pid_to_thread_id (ptid);
2891 const struct bp_location *bl = bs->breakpoint_at;
2892 struct breakpoint *b = bl->owner;
2894 if (frame_id_p (b->frame_id)
2895 && !frame_id_eq (b->frame_id, get_frame_id (get_current_frame ())))
2896 bs->stop = 0;
2897 else if (bs->stop)
2899 int value_is_zero = 0;
2901 /* If this is a scope breakpoint, mark the associated
2902 watchpoint as triggered so that we will handle the
2903 out-of-scope event. We'll get to the watchpoint next
2904 iteration. */
2905 if (b->type == bp_watchpoint_scope)
2906 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
2908 if (bl->cond && bl->owner->disposition != disp_del_at_next_stop)
2910 /* We use value_mark and value_free_to_mark because it could
2911 be a long time before we return to the command level and
2912 call free_all_values. We can't call free_all_values
2913 because we might be in the middle of evaluating a
2914 function call. */
2915 struct value *mark = value_mark ();
2917 /* Need to select the frame, with all that implies
2918 so that the conditions will have the right context. */
2919 select_frame (get_current_frame ());
2920 value_is_zero
2921 = catch_errors (breakpoint_cond_eval, (bl->cond),
2922 "Error in testing breakpoint condition:\n",
2923 RETURN_MASK_ALL);
2924 /* FIXME-someday, should give breakpoint # */
2925 value_free_to_mark (mark);
2927 if (bl->cond && value_is_zero)
2929 bs->stop = 0;
2931 else if (b->thread != -1 && b->thread != thread_id)
2933 bs->stop = 0;
2935 else if (b->ignore_count > 0)
2937 b->ignore_count--;
2938 annotate_ignore_count_change ();
2939 bs->stop = 0;
2940 /* Increase the hit count even though we don't
2941 stop. */
2942 ++(b->hit_count);
2948 /* Get a bpstat associated with having just stopped at address
2949 BP_ADDR in thread PTID.
2951 Determine whether we stopped at a breakpoint, etc, or whether we
2952 don't understand this stop. Result is a chain of bpstat's such that:
2954 if we don't understand the stop, the result is a null pointer.
2956 if we understand why we stopped, the result is not null.
2958 Each element of the chain refers to a particular breakpoint or
2959 watchpoint at which we have stopped. (We may have stopped for
2960 several reasons concurrently.)
2962 Each element of the chain has valid next, breakpoint_at,
2963 commands, FIXME??? fields. */
2965 bpstat
2966 bpstat_stop_status (CORE_ADDR bp_addr, ptid_t ptid)
2968 struct breakpoint *b = NULL;
2969 const struct bp_location *bl;
2970 struct bp_location *loc;
2971 /* Root of the chain of bpstat's */
2972 struct bpstats root_bs[1];
2973 /* Pointer to the last thing in the chain currently. */
2974 bpstat bs = root_bs;
2975 int ix;
2976 int need_remove_insert;
2978 ALL_BP_LOCATIONS (bl)
2980 b = bl->owner;
2981 gdb_assert (b);
2982 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
2983 continue;
2985 /* For hardware watchpoints, we look only at the first location.
2986 The watchpoint_check function will work on entire expression,
2987 not the individual locations. For read watchopints, the
2988 watchpoints_triggered function have checked all locations
2989 alrea
2991 if (b->type == bp_hardware_watchpoint && bl != b->loc)
2992 continue;
2994 if (!bpstat_check_location (bl, bp_addr))
2995 continue;
2997 /* Come here if it's a watchpoint, or if the break address matches */
2999 bs = bpstat_alloc (bl, bs); /* Alloc a bpstat to explain stop */
3001 /* Assume we stop. Should we find watchpoint that is not actually
3002 triggered, or if condition of breakpoint is false, we'll reset
3003 'stop' to 0. */
3004 bs->stop = 1;
3005 bs->print = 1;
3007 bpstat_check_watchpoint (bs);
3008 if (!bs->stop)
3009 continue;
3011 if (b->type == bp_thread_event || b->type == bp_overlay_event)
3012 /* We do not stop for these. */
3013 bs->stop = 0;
3014 else
3015 bpstat_check_breakpoint_conditions (bs, ptid);
3017 if (bs->stop)
3019 ++(b->hit_count);
3021 /* We will stop here */
3022 if (b->disposition == disp_disable)
3024 if (b->enable_state != bp_permanent)
3025 b->enable_state = bp_disabled;
3026 update_global_location_list (0);
3028 if (b->silent)
3029 bs->print = 0;
3030 bs->commands = b->commands;
3031 if (bs->commands &&
3032 (strcmp ("silent", bs->commands->line) == 0
3033 || (xdb_commands && strcmp ("Q", bs->commands->line) == 0)))
3035 bs->commands = bs->commands->next;
3036 bs->print = 0;
3038 bs->commands = copy_command_lines (bs->commands);
3041 /* Print nothing for this entry if we dont stop or if we dont print. */
3042 if (bs->stop == 0 || bs->print == 0)
3043 bs->print_it = print_it_noop;
3046 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3048 if (loc->address == bp_addr)
3050 bs = bpstat_alloc (loc, bs);
3051 /* For hits of moribund locations, we should just proceed. */
3052 bs->stop = 0;
3053 bs->print = 0;
3054 bs->print_it = print_it_noop;
3058 bs->next = NULL; /* Terminate the chain */
3059 bs = root_bs->next; /* Re-grab the head of the chain */
3061 /* If we aren't stopping, the value of some hardware watchpoint may
3062 not have changed, but the intermediate memory locations we are
3063 watching may have. Don't bother if we're stopping; this will get
3064 done later. */
3065 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3066 if (bs->stop)
3067 break;
3069 need_remove_insert = 0;
3070 if (bs == NULL)
3071 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3072 if (!bs->stop
3073 && bs->breakpoint_at->owner
3074 && (bs->breakpoint_at->owner->type == bp_hardware_watchpoint
3075 || bs->breakpoint_at->owner->type == bp_read_watchpoint
3076 || bs->breakpoint_at->owner->type == bp_access_watchpoint))
3078 /* remove/insert can invalidate bs->breakpoint_at, if this
3079 location is no longer used by the watchpoint. Prevent
3080 further code from trying to use it. */
3081 bs->breakpoint_at = NULL;
3082 need_remove_insert = 1;
3085 if (need_remove_insert)
3087 remove_breakpoints ();
3088 insert_breakpoints ();
3091 return root_bs->next;
3094 /* Tell what to do about this bpstat. */
3095 struct bpstat_what
3096 bpstat_what (bpstat bs)
3098 /* Classify each bpstat as one of the following. */
3099 enum class
3101 /* This bpstat element has no effect on the main_action. */
3102 no_effect = 0,
3104 /* There was a watchpoint, stop but don't print. */
3105 wp_silent,
3107 /* There was a watchpoint, stop and print. */
3108 wp_noisy,
3110 /* There was a breakpoint but we're not stopping. */
3111 bp_nostop,
3113 /* There was a breakpoint, stop but don't print. */
3114 bp_silent,
3116 /* There was a breakpoint, stop and print. */
3117 bp_noisy,
3119 /* We hit the longjmp breakpoint. */
3120 long_jump,
3122 /* We hit the longjmp_resume breakpoint. */
3123 long_resume,
3125 /* We hit the step_resume breakpoint. */
3126 step_resume,
3128 /* We hit the shared library event breakpoint. */
3129 shlib_event,
3131 /* This is just used to count how many enums there are. */
3132 class_last
3135 /* Here is the table which drives this routine. So that we can
3136 format it pretty, we define some abbreviations for the
3137 enum bpstat_what codes. */
3138 #define kc BPSTAT_WHAT_KEEP_CHECKING
3139 #define ss BPSTAT_WHAT_STOP_SILENT
3140 #define sn BPSTAT_WHAT_STOP_NOISY
3141 #define sgl BPSTAT_WHAT_SINGLE
3142 #define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
3143 #define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
3144 #define sr BPSTAT_WHAT_STEP_RESUME
3145 #define shl BPSTAT_WHAT_CHECK_SHLIBS
3147 /* "Can't happen." Might want to print an error message.
3148 abort() is not out of the question, but chances are GDB is just
3149 a bit confused, not unusable. */
3150 #define err BPSTAT_WHAT_STOP_NOISY
3152 /* Given an old action and a class, come up with a new action. */
3153 /* One interesting property of this table is that wp_silent is the same
3154 as bp_silent and wp_noisy is the same as bp_noisy. That is because
3155 after stopping, the check for whether to step over a breakpoint
3156 (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
3157 reference to how we stopped. We retain separate wp_silent and
3158 bp_silent codes in case we want to change that someday.
3160 Another possibly interesting property of this table is that
3161 there's a partial ordering, priority-like, of the actions. Once
3162 you've decided that some action is appropriate, you'll never go
3163 back and decide something of a lower priority is better. The
3164 ordering is:
3166 kc < clr sgl shl slr sn sr ss
3167 sgl < shl slr sn sr ss
3168 slr < err shl sn sr ss
3169 clr < err shl sn sr ss
3170 ss < shl sn sr
3171 sn < shl sr
3172 shl < sr
3173 sr <
3175 What I think this means is that we don't need a damned table
3176 here. If you just put the rows and columns in the right order,
3177 it'd look awfully regular. We could simply walk the bpstat list
3178 and choose the highest priority action we find, with a little
3179 logic to handle the 'err' cases. */
3181 /* step_resume entries: a step resume breakpoint overrides another
3182 breakpoint of signal handling (see comment in wait_for_inferior
3183 at where we set the step_resume breakpoint). */
3185 static const enum bpstat_what_main_action
3186 table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
3188 /* old action */
3189 /* kc ss sn sgl slr clr sr shl
3191 /*no_effect */
3192 {kc, ss, sn, sgl, slr, clr, sr, shl},
3193 /*wp_silent */
3194 {ss, ss, sn, ss, ss, ss, sr, shl},
3195 /*wp_noisy */
3196 {sn, sn, sn, sn, sn, sn, sr, shl},
3197 /*bp_nostop */
3198 {sgl, ss, sn, sgl, slr, slr, sr, shl},
3199 /*bp_silent */
3200 {ss, ss, sn, ss, ss, ss, sr, shl},
3201 /*bp_noisy */
3202 {sn, sn, sn, sn, sn, sn, sr, shl},
3203 /*long_jump */
3204 {slr, ss, sn, slr, slr, err, sr, shl},
3205 /*long_resume */
3206 {clr, ss, sn, err, err, err, sr, shl},
3207 /*step_resume */
3208 {sr, sr, sr, sr, sr, sr, sr, sr},
3209 /*shlib */
3210 {shl, shl, shl, shl, shl, shl, sr, shl}
3213 #undef kc
3214 #undef ss
3215 #undef sn
3216 #undef sgl
3217 #undef slr
3218 #undef clr
3219 #undef err
3220 #undef sr
3221 #undef ts
3222 #undef shl
3223 enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
3224 struct bpstat_what retval;
3226 retval.call_dummy = 0;
3227 for (; bs != NULL; bs = bs->next)
3229 enum class bs_class = no_effect;
3230 if (bs->breakpoint_at == NULL)
3231 /* I suspect this can happen if it was a momentary breakpoint
3232 which has since been deleted. */
3233 continue;
3234 if (bs->breakpoint_at->owner == NULL)
3235 bs_class = bp_nostop;
3236 else
3237 switch (bs->breakpoint_at->owner->type)
3239 case bp_none:
3240 continue;
3242 case bp_breakpoint:
3243 case bp_hardware_breakpoint:
3244 case bp_until:
3245 case bp_finish:
3246 if (bs->stop)
3248 if (bs->print)
3249 bs_class = bp_noisy;
3250 else
3251 bs_class = bp_silent;
3253 else
3254 bs_class = bp_nostop;
3255 break;
3256 case bp_watchpoint:
3257 case bp_hardware_watchpoint:
3258 case bp_read_watchpoint:
3259 case bp_access_watchpoint:
3260 if (bs->stop)
3262 if (bs->print)
3263 bs_class = wp_noisy;
3264 else
3265 bs_class = wp_silent;
3267 else
3268 /* There was a watchpoint, but we're not stopping.
3269 This requires no further action. */
3270 bs_class = no_effect;
3271 break;
3272 case bp_longjmp:
3273 bs_class = long_jump;
3274 break;
3275 case bp_longjmp_resume:
3276 bs_class = long_resume;
3277 break;
3278 case bp_step_resume:
3279 if (bs->stop)
3281 bs_class = step_resume;
3283 else
3284 /* It is for the wrong frame. */
3285 bs_class = bp_nostop;
3286 break;
3287 case bp_watchpoint_scope:
3288 bs_class = bp_nostop;
3289 break;
3290 case bp_shlib_event:
3291 bs_class = shlib_event;
3292 break;
3293 case bp_thread_event:
3294 case bp_overlay_event:
3295 bs_class = bp_nostop;
3296 break;
3297 case bp_catchpoint:
3298 if (bs->stop)
3300 if (bs->print)
3301 bs_class = bp_noisy;
3302 else
3303 bs_class = bp_silent;
3305 else
3306 /* There was a catchpoint, but we're not stopping.
3307 This requires no further action. */
3308 bs_class = no_effect;
3309 break;
3310 case bp_call_dummy:
3311 /* Make sure the action is stop (silent or noisy),
3312 so infrun.c pops the dummy frame. */
3313 bs_class = bp_silent;
3314 retval.call_dummy = 1;
3315 break;
3317 current_action = table[(int) bs_class][(int) current_action];
3319 retval.main_action = current_action;
3320 return retval;
3323 /* Nonzero if we should step constantly (e.g. watchpoints on machines
3324 without hardware support). This isn't related to a specific bpstat,
3325 just to things like whether watchpoints are set. */
3328 bpstat_should_step (void)
3330 struct breakpoint *b;
3331 ALL_BREAKPOINTS (b)
3332 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3333 return 1;
3334 return 0;
3339 static void print_breakpoint_location (struct breakpoint *b,
3340 struct bp_location *loc,
3341 char *wrap_indent,
3342 struct ui_stream *stb)
3344 if (b->source_file)
3346 struct symbol *sym
3347 = find_pc_sect_function (loc->address, loc->section);
3348 if (sym)
3350 ui_out_text (uiout, "in ");
3351 ui_out_field_string (uiout, "func",
3352 SYMBOL_PRINT_NAME (sym));
3353 ui_out_wrap_hint (uiout, wrap_indent);
3354 ui_out_text (uiout, " at ");
3356 ui_out_field_string (uiout, "file", b->source_file);
3357 ui_out_text (uiout, ":");
3359 if (ui_out_is_mi_like_p (uiout))
3361 struct symtab_and_line sal = find_pc_line (loc->address, 0);
3362 char *fullname = symtab_to_fullname (sal.symtab);
3364 if (fullname)
3365 ui_out_field_string (uiout, "fullname", fullname);
3368 ui_out_field_int (uiout, "line", b->line_number);
3370 else if (!b->loc)
3372 ui_out_field_string (uiout, "pending", b->addr_string);
3374 else
3376 print_address_symbolic (loc->address, stb->stream, demangle, "");
3377 ui_out_field_stream (uiout, "at", stb);
3381 /* Print B to gdb_stdout. */
3382 static void
3383 print_one_breakpoint_location (struct breakpoint *b,
3384 struct bp_location *loc,
3385 int loc_number,
3386 CORE_ADDR *last_addr)
3388 struct command_line *l;
3389 struct symbol *sym;
3390 struct ep_type_description
3392 enum bptype type;
3393 char *description;
3395 static struct ep_type_description bptypes[] =
3397 {bp_none, "?deleted?"},
3398 {bp_breakpoint, "breakpoint"},
3399 {bp_hardware_breakpoint, "hw breakpoint"},
3400 {bp_until, "until"},
3401 {bp_finish, "finish"},
3402 {bp_watchpoint, "watchpoint"},
3403 {bp_hardware_watchpoint, "hw watchpoint"},
3404 {bp_read_watchpoint, "read watchpoint"},
3405 {bp_access_watchpoint, "acc watchpoint"},
3406 {bp_longjmp, "longjmp"},
3407 {bp_longjmp_resume, "longjmp resume"},
3408 {bp_step_resume, "step resume"},
3409 {bp_watchpoint_scope, "watchpoint scope"},
3410 {bp_call_dummy, "call dummy"},
3411 {bp_shlib_event, "shlib events"},
3412 {bp_thread_event, "thread events"},
3413 {bp_overlay_event, "overlay events"},
3414 {bp_catchpoint, "catchpoint"},
3417 static char bpenables[] = "nynny";
3418 char wrap_indent[80];
3419 struct ui_stream *stb = ui_out_stream_new (uiout);
3420 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3421 struct cleanup *bkpt_chain;
3423 int header_of_multiple = 0;
3424 int part_of_multiple = (loc != NULL);
3425 struct value_print_options opts;
3427 get_user_print_options (&opts);
3429 gdb_assert (!loc || loc_number != 0);
3430 /* See comment in print_one_breakpoint concerning
3431 treatment of breakpoints with single disabled
3432 location. */
3433 if (loc == NULL
3434 && (b->loc != NULL
3435 && (b->loc->next != NULL || !b->loc->enabled)))
3436 header_of_multiple = 1;
3437 if (loc == NULL)
3438 loc = b->loc;
3440 annotate_record ();
3441 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
3443 /* 1 */
3444 annotate_field (0);
3445 if (part_of_multiple)
3447 char *formatted;
3448 formatted = xstrprintf ("%d.%d", b->number, loc_number);
3449 ui_out_field_string (uiout, "number", formatted);
3450 xfree (formatted);
3452 else
3454 ui_out_field_int (uiout, "number", b->number);
3457 /* 2 */
3458 annotate_field (1);
3459 if (part_of_multiple)
3460 ui_out_field_skip (uiout, "type");
3461 else
3463 if (((int) b->type >= (sizeof (bptypes) / sizeof (bptypes[0])))
3464 || ((int) b->type != bptypes[(int) b->type].type))
3465 internal_error (__FILE__, __LINE__,
3466 _("bptypes table does not describe type #%d."),
3467 (int) b->type);
3468 ui_out_field_string (uiout, "type", bptypes[(int) b->type].description);
3471 /* 3 */
3472 annotate_field (2);
3473 if (part_of_multiple)
3474 ui_out_field_skip (uiout, "disp");
3475 else
3476 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3479 /* 4 */
3480 annotate_field (3);
3481 if (part_of_multiple)
3482 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
3483 else
3484 ui_out_field_fmt (uiout, "enabled", "%c",
3485 bpenables[(int) b->enable_state]);
3486 ui_out_spaces (uiout, 2);
3489 /* 5 and 6 */
3490 strcpy (wrap_indent, " ");
3491 if (opts.addressprint)
3493 if (gdbarch_addr_bit (current_gdbarch) <= 32)
3494 strcat (wrap_indent, " ");
3495 else
3496 strcat (wrap_indent, " ");
3499 if (b->ops != NULL && b->ops->print_one != NULL)
3501 /* Although the print_one can possibly print
3502 all locations, calling it here is not likely
3503 to get any nice result. So, make sure there's
3504 just one location. */
3505 gdb_assert (b->loc == NULL || b->loc->next == NULL);
3506 b->ops->print_one (b, last_addr);
3508 else
3509 switch (b->type)
3511 case bp_none:
3512 internal_error (__FILE__, __LINE__,
3513 _("print_one_breakpoint: bp_none encountered\n"));
3514 break;
3516 case bp_watchpoint:
3517 case bp_hardware_watchpoint:
3518 case bp_read_watchpoint:
3519 case bp_access_watchpoint:
3520 /* Field 4, the address, is omitted (which makes the columns
3521 not line up too nicely with the headers, but the effect
3522 is relatively readable). */
3523 if (opts.addressprint)
3524 ui_out_field_skip (uiout, "addr");
3525 annotate_field (5);
3526 ui_out_field_string (uiout, "what", b->exp_string);
3527 break;
3529 case bp_breakpoint:
3530 case bp_hardware_breakpoint:
3531 case bp_until:
3532 case bp_finish:
3533 case bp_longjmp:
3534 case bp_longjmp_resume:
3535 case bp_step_resume:
3536 case bp_watchpoint_scope:
3537 case bp_call_dummy:
3538 case bp_shlib_event:
3539 case bp_thread_event:
3540 case bp_overlay_event:
3541 if (opts.addressprint)
3543 annotate_field (4);
3544 if (header_of_multiple)
3545 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
3546 else if (b->loc == NULL || loc->shlib_disabled)
3547 ui_out_field_string (uiout, "addr", "<PENDING>");
3548 else
3549 ui_out_field_core_addr (uiout, "addr", loc->address);
3551 annotate_field (5);
3552 if (!header_of_multiple)
3553 print_breakpoint_location (b, loc, wrap_indent, stb);
3554 if (b->loc)
3555 *last_addr = b->loc->address;
3556 break;
3559 if (!part_of_multiple && b->thread != -1)
3561 /* FIXME: This seems to be redundant and lost here; see the
3562 "stop only in" line a little further down. */
3563 ui_out_text (uiout, " thread ");
3564 ui_out_field_int (uiout, "thread", b->thread);
3567 ui_out_text (uiout, "\n");
3569 if (part_of_multiple && frame_id_p (b->frame_id))
3571 annotate_field (6);
3572 ui_out_text (uiout, "\tstop only in stack frame at ");
3573 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
3574 the frame ID. */
3575 ui_out_field_core_addr (uiout, "frame", b->frame_id.stack_addr);
3576 ui_out_text (uiout, "\n");
3579 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
3581 /* We do not print the condition for Ada exception catchpoints
3582 because the condition is an internal implementation detail
3583 that we do not want to expose to the user. */
3584 annotate_field (7);
3585 ui_out_text (uiout, "\tstop only if ");
3586 ui_out_field_string (uiout, "cond", b->cond_string);
3587 ui_out_text (uiout, "\n");
3590 if (!part_of_multiple && b->thread != -1)
3592 /* FIXME should make an annotation for this */
3593 ui_out_text (uiout, "\tstop only in thread ");
3594 ui_out_field_int (uiout, "thread", b->thread);
3595 ui_out_text (uiout, "\n");
3598 if (!part_of_multiple && b->hit_count)
3600 /* FIXME should make an annotation for this */
3601 if (ep_is_catchpoint (b))
3602 ui_out_text (uiout, "\tcatchpoint");
3603 else
3604 ui_out_text (uiout, "\tbreakpoint");
3605 ui_out_text (uiout, " already hit ");
3606 ui_out_field_int (uiout, "times", b->hit_count);
3607 if (b->hit_count == 1)
3608 ui_out_text (uiout, " time\n");
3609 else
3610 ui_out_text (uiout, " times\n");
3613 /* Output the count also if it is zero, but only if this is
3614 mi. FIXME: Should have a better test for this. */
3615 if (ui_out_is_mi_like_p (uiout))
3616 if (!part_of_multiple && b->hit_count == 0)
3617 ui_out_field_int (uiout, "times", b->hit_count);
3619 if (!part_of_multiple && b->ignore_count)
3621 annotate_field (8);
3622 ui_out_text (uiout, "\tignore next ");
3623 ui_out_field_int (uiout, "ignore", b->ignore_count);
3624 ui_out_text (uiout, " hits\n");
3627 if (!part_of_multiple && (l = b->commands))
3629 struct cleanup *script_chain;
3631 annotate_field (9);
3632 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
3633 print_command_lines (uiout, l, 4);
3634 do_cleanups (script_chain);
3637 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
3639 if (b->addr_string)
3640 ui_out_field_string (uiout, "original-location", b->addr_string);
3641 else if (b->exp_string)
3642 ui_out_field_string (uiout, "original-location", b->exp_string);
3645 do_cleanups (bkpt_chain);
3646 do_cleanups (old_chain);
3649 static void
3650 print_one_breakpoint (struct breakpoint *b,
3651 CORE_ADDR *last_addr)
3653 print_one_breakpoint_location (b, NULL, 0, last_addr);
3655 /* If this breakpoint has custom print function,
3656 it's already printed. Otherwise, print individual
3657 locations, if any. */
3658 if (b->ops == NULL || b->ops->print_one == NULL)
3660 /* If breakpoint has a single location that is
3661 disabled, we print it as if it had
3662 several locations, since otherwise it's hard to
3663 represent "breakpoint enabled, location disabled"
3664 situation.
3665 Note that while hardware watchpoints have
3666 several locations internally, that's no a property
3667 exposed to user. */
3668 if (b->loc
3669 && !is_hardware_watchpoint (b)
3670 && (b->loc->next || !b->loc->enabled)
3671 && !ui_out_is_mi_like_p (uiout))
3673 struct bp_location *loc;
3674 int n = 1;
3675 for (loc = b->loc; loc; loc = loc->next, ++n)
3676 print_one_breakpoint_location (b, loc, n, last_addr);
3682 struct captured_breakpoint_query_args
3684 int bnum;
3687 static int
3688 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
3690 struct captured_breakpoint_query_args *args = data;
3691 struct breakpoint *b;
3692 CORE_ADDR dummy_addr = 0;
3693 ALL_BREAKPOINTS (b)
3695 if (args->bnum == b->number)
3697 print_one_breakpoint (b, &dummy_addr);
3698 return GDB_RC_OK;
3701 return GDB_RC_NONE;
3704 enum gdb_rc
3705 gdb_breakpoint_query (struct ui_out *uiout, int bnum, char **error_message)
3707 struct captured_breakpoint_query_args args;
3708 args.bnum = bnum;
3709 /* For the moment we don't trust print_one_breakpoint() to not throw
3710 an error. */
3711 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
3712 error_message, RETURN_MASK_ALL) < 0)
3713 return GDB_RC_FAIL;
3714 else
3715 return GDB_RC_OK;
3718 /* Return non-zero if B is user settable (breakpoints, watchpoints,
3719 catchpoints, et.al.). */
3721 static int
3722 user_settable_breakpoint (const struct breakpoint *b)
3724 return (b->type == bp_breakpoint
3725 || b->type == bp_catchpoint
3726 || b->type == bp_hardware_breakpoint
3727 || b->type == bp_watchpoint
3728 || b->type == bp_read_watchpoint
3729 || b->type == bp_access_watchpoint
3730 || b->type == bp_hardware_watchpoint);
3733 /* Print information on user settable breakpoint (watchpoint, etc)
3734 number BNUM. If BNUM is -1 print all user settable breakpoints.
3735 If ALLFLAG is non-zero, include non- user settable breakpoints. */
3737 static void
3738 breakpoint_1 (int bnum, int allflag)
3740 struct breakpoint *b;
3741 CORE_ADDR last_addr = (CORE_ADDR) -1;
3742 int nr_printable_breakpoints;
3743 struct cleanup *bkpttbl_chain;
3744 struct value_print_options opts;
3746 get_user_print_options (&opts);
3748 /* Compute the number of rows in the table. */
3749 nr_printable_breakpoints = 0;
3750 ALL_BREAKPOINTS (b)
3751 if (bnum == -1
3752 || bnum == b->number)
3754 if (allflag || user_settable_breakpoint (b))
3755 nr_printable_breakpoints++;
3758 if (opts.addressprint)
3759 bkpttbl_chain
3760 = make_cleanup_ui_out_table_begin_end (uiout, 6, nr_printable_breakpoints,
3761 "BreakpointTable");
3762 else
3763 bkpttbl_chain
3764 = make_cleanup_ui_out_table_begin_end (uiout, 5, nr_printable_breakpoints,
3765 "BreakpointTable");
3767 if (nr_printable_breakpoints > 0)
3768 annotate_breakpoints_headers ();
3769 if (nr_printable_breakpoints > 0)
3770 annotate_field (0);
3771 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
3772 if (nr_printable_breakpoints > 0)
3773 annotate_field (1);
3774 ui_out_table_header (uiout, 14, ui_left, "type", "Type"); /* 2 */
3775 if (nr_printable_breakpoints > 0)
3776 annotate_field (2);
3777 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
3778 if (nr_printable_breakpoints > 0)
3779 annotate_field (3);
3780 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
3781 if (opts.addressprint)
3783 if (nr_printable_breakpoints > 0)
3784 annotate_field (4);
3785 if (gdbarch_addr_bit (current_gdbarch) <= 32)
3786 ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
3787 else
3788 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
3790 if (nr_printable_breakpoints > 0)
3791 annotate_field (5);
3792 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
3793 ui_out_table_body (uiout);
3794 if (nr_printable_breakpoints > 0)
3795 annotate_breakpoints_table ();
3797 ALL_BREAKPOINTS (b)
3798 if (bnum == -1
3799 || bnum == b->number)
3801 /* We only print out user settable breakpoints unless the
3802 allflag is set. */
3803 if (allflag || user_settable_breakpoint (b))
3804 print_one_breakpoint (b, &last_addr);
3807 do_cleanups (bkpttbl_chain);
3809 if (nr_printable_breakpoints == 0)
3811 if (bnum == -1)
3812 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
3813 else
3814 ui_out_message (uiout, 0, "No breakpoint or watchpoint number %d.\n",
3815 bnum);
3817 else
3819 /* Compare against (CORE_ADDR)-1 in case some compiler decides
3820 that a comparison of an unsigned with -1 is always false. */
3821 if (last_addr != (CORE_ADDR) -1 && !server_command)
3822 set_next_address (current_gdbarch, last_addr);
3825 /* FIXME? Should this be moved up so that it is only called when
3826 there have been breakpoints? */
3827 annotate_breakpoints_table_end ();
3830 static void
3831 breakpoints_info (char *bnum_exp, int from_tty)
3833 int bnum = -1;
3835 if (bnum_exp)
3836 bnum = parse_and_eval_long (bnum_exp);
3838 breakpoint_1 (bnum, 0);
3841 static void
3842 maintenance_info_breakpoints (char *bnum_exp, int from_tty)
3844 int bnum = -1;
3846 if (bnum_exp)
3847 bnum = parse_and_eval_long (bnum_exp);
3849 breakpoint_1 (bnum, 1);
3852 static int
3853 breakpoint_has_pc (struct breakpoint *b,
3854 CORE_ADDR pc, struct obj_section *section)
3856 struct bp_location *bl = b->loc;
3857 for (; bl; bl = bl->next)
3859 if (bl->address == pc
3860 && (!overlay_debugging || bl->section == section))
3861 return 1;
3863 return 0;
3866 /* Print a message describing any breakpoints set at PC. */
3868 static void
3869 describe_other_breakpoints (CORE_ADDR pc, struct obj_section *section,
3870 int thread)
3872 int others = 0;
3873 struct breakpoint *b;
3875 ALL_BREAKPOINTS (b)
3876 others += breakpoint_has_pc (b, pc, section);
3877 if (others > 0)
3879 if (others == 1)
3880 printf_filtered (_("Note: breakpoint "));
3881 else /* if (others == ???) */
3882 printf_filtered (_("Note: breakpoints "));
3883 ALL_BREAKPOINTS (b)
3884 if (breakpoint_has_pc (b, pc, section))
3886 others--;
3887 printf_filtered ("%d", b->number);
3888 if (b->thread == -1 && thread != -1)
3889 printf_filtered (" (all threads)");
3890 else if (b->thread != -1)
3891 printf_filtered (" (thread %d)", b->thread);
3892 printf_filtered ("%s%s ",
3893 ((b->enable_state == bp_disabled ||
3894 b->enable_state == bp_call_disabled)
3895 ? " (disabled)"
3896 : b->enable_state == bp_permanent
3897 ? " (permanent)"
3898 : ""),
3899 (others > 1) ? ","
3900 : ((others == 1) ? " and" : ""));
3902 printf_filtered (_("also set at pc "));
3903 fputs_filtered (paddress (pc), gdb_stdout);
3904 printf_filtered (".\n");
3908 /* Set the default place to put a breakpoint
3909 for the `break' command with no arguments. */
3911 void
3912 set_default_breakpoint (int valid, CORE_ADDR addr, struct symtab *symtab,
3913 int line)
3915 default_breakpoint_valid = valid;
3916 default_breakpoint_address = addr;
3917 default_breakpoint_symtab = symtab;
3918 default_breakpoint_line = line;
3921 /* Return true iff it is meaningful to use the address member of
3922 BPT. For some breakpoint types, the address member is irrelevant
3923 and it makes no sense to attempt to compare it to other addresses
3924 (or use it for any other purpose either).
3926 More specifically, each of the following breakpoint types will always
3927 have a zero valued address and we don't want check_duplicates() to mark
3928 breakpoints of any of these types to be a duplicate of an actual
3929 breakpoint at address zero:
3931 bp_watchpoint
3932 bp_hardware_watchpoint
3933 bp_read_watchpoint
3934 bp_access_watchpoint
3935 bp_catchpoint */
3937 static int
3938 breakpoint_address_is_meaningful (struct breakpoint *bpt)
3940 enum bptype type = bpt->type;
3942 return (type != bp_watchpoint
3943 && type != bp_hardware_watchpoint
3944 && type != bp_read_watchpoint
3945 && type != bp_access_watchpoint
3946 && type != bp_catchpoint);
3949 /* Rescan breakpoints at the same address and section as BPT,
3950 marking the first one as "first" and any others as "duplicates".
3951 This is so that the bpt instruction is only inserted once.
3952 If we have a permanent breakpoint at the same place as BPT, make
3953 that one the official one, and the rest as duplicates. */
3955 static void
3956 check_duplicates_for (CORE_ADDR address, struct obj_section *section)
3958 struct bp_location *b;
3959 int count = 0;
3960 struct bp_location *perm_bp = 0;
3962 ALL_BP_LOCATIONS (b)
3963 if (b->owner->enable_state != bp_disabled
3964 && b->owner->enable_state != bp_call_disabled
3965 && b->enabled
3966 && !b->shlib_disabled
3967 && b->address == address /* address / overlay match */
3968 && (!overlay_debugging || b->section == section)
3969 && breakpoint_address_is_meaningful (b->owner))
3971 /* Have we found a permanent breakpoint? */
3972 if (b->owner->enable_state == bp_permanent)
3974 perm_bp = b;
3975 break;
3978 count++;
3979 b->duplicate = count > 1;
3982 /* If we found a permanent breakpoint at this address, go over the
3983 list again and declare all the other breakpoints there (except
3984 other permanent breakpoints) to be the duplicates. */
3985 if (perm_bp)
3987 perm_bp->duplicate = 0;
3989 /* Permanent breakpoint should always be inserted. */
3990 if (! perm_bp->inserted)
3991 internal_error (__FILE__, __LINE__,
3992 _("allegedly permanent breakpoint is not "
3993 "actually inserted"));
3995 ALL_BP_LOCATIONS (b)
3996 if (b != perm_bp)
3998 if (b->owner->enable_state != bp_permanent
3999 && b->owner->enable_state != bp_disabled
4000 && b->owner->enable_state != bp_call_disabled
4001 && b->enabled && !b->shlib_disabled
4002 && b->address == address /* address / overlay match */
4003 && (!overlay_debugging || b->section == section)
4004 && breakpoint_address_is_meaningful (b->owner))
4006 if (b->inserted)
4007 internal_error (__FILE__, __LINE__,
4008 _("another breakpoint was inserted on top of "
4009 "a permanent breakpoint"));
4011 b->duplicate = 1;
4017 static void
4018 check_duplicates (struct breakpoint *bpt)
4020 struct bp_location *bl = bpt->loc;
4022 if (! breakpoint_address_is_meaningful (bpt))
4023 return;
4025 for (; bl; bl = bl->next)
4026 check_duplicates_for (bl->address, bl->section);
4029 static void
4030 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
4031 int bnum, int have_bnum)
4033 char astr1[40];
4034 char astr2[40];
4036 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
4037 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
4038 if (have_bnum)
4039 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
4040 bnum, astr1, astr2);
4041 else
4042 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
4045 /* Adjust a breakpoint's address to account for architectural constraints
4046 on breakpoint placement. Return the adjusted address. Note: Very
4047 few targets require this kind of adjustment. For most targets,
4048 this function is simply the identity function. */
4050 static CORE_ADDR
4051 adjust_breakpoint_address (CORE_ADDR bpaddr, enum bptype bptype)
4053 if (!gdbarch_adjust_breakpoint_address_p (current_gdbarch))
4055 /* Very few targets need any kind of breakpoint adjustment. */
4056 return bpaddr;
4058 else if (bptype == bp_watchpoint
4059 || bptype == bp_hardware_watchpoint
4060 || bptype == bp_read_watchpoint
4061 || bptype == bp_access_watchpoint
4062 || bptype == bp_catchpoint)
4064 /* Watchpoints and the various bp_catch_* eventpoints should not
4065 have their addresses modified. */
4066 return bpaddr;
4068 else
4070 CORE_ADDR adjusted_bpaddr;
4072 /* Some targets have architectural constraints on the placement
4073 of breakpoint instructions. Obtain the adjusted address. */
4074 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (current_gdbarch,
4075 bpaddr);
4077 /* An adjusted breakpoint address can significantly alter
4078 a user's expectations. Print a warning if an adjustment
4079 is required. */
4080 if (adjusted_bpaddr != bpaddr)
4081 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
4083 return adjusted_bpaddr;
4087 /* Allocate a struct bp_location. */
4089 static struct bp_location *
4090 allocate_bp_location (struct breakpoint *bpt)
4092 struct bp_location *loc, *loc_p;
4094 loc = xmalloc (sizeof (struct bp_location));
4095 memset (loc, 0, sizeof (*loc));
4097 loc->owner = bpt;
4098 loc->cond = NULL;
4099 loc->shlib_disabled = 0;
4100 loc->enabled = 1;
4102 switch (bpt->type)
4104 case bp_breakpoint:
4105 case bp_until:
4106 case bp_finish:
4107 case bp_longjmp:
4108 case bp_longjmp_resume:
4109 case bp_step_resume:
4110 case bp_watchpoint_scope:
4111 case bp_call_dummy:
4112 case bp_shlib_event:
4113 case bp_thread_event:
4114 case bp_overlay_event:
4115 loc->loc_type = bp_loc_software_breakpoint;
4116 break;
4117 case bp_hardware_breakpoint:
4118 loc->loc_type = bp_loc_hardware_breakpoint;
4119 break;
4120 case bp_hardware_watchpoint:
4121 case bp_read_watchpoint:
4122 case bp_access_watchpoint:
4123 loc->loc_type = bp_loc_hardware_watchpoint;
4124 break;
4125 case bp_watchpoint:
4126 case bp_catchpoint:
4127 loc->loc_type = bp_loc_other;
4128 break;
4129 default:
4130 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
4133 return loc;
4136 static void free_bp_location (struct bp_location *loc)
4138 if (loc->cond)
4139 xfree (loc->cond);
4141 if (loc->function_name)
4142 xfree (loc->function_name);
4144 xfree (loc);
4147 /* Helper to set_raw_breakpoint below. Creates a breakpoint
4148 that has type BPTYPE and has no locations as yet. */
4150 static struct breakpoint *
4151 set_raw_breakpoint_without_location (enum bptype bptype)
4153 struct breakpoint *b, *b1;
4155 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
4156 memset (b, 0, sizeof (*b));
4158 b->type = bptype;
4159 b->language = current_language->la_language;
4160 b->input_radix = input_radix;
4161 b->thread = -1;
4162 b->enable_state = bp_enabled;
4163 b->next = 0;
4164 b->silent = 0;
4165 b->ignore_count = 0;
4166 b->commands = NULL;
4167 b->frame_id = null_frame_id;
4168 b->forked_inferior_pid = null_ptid;
4169 b->exec_pathname = NULL;
4170 b->ops = NULL;
4171 b->condition_not_parsed = 0;
4173 /* Add this breakpoint to the end of the chain
4174 so that a list of breakpoints will come out in order
4175 of increasing numbers. */
4177 b1 = breakpoint_chain;
4178 if (b1 == 0)
4179 breakpoint_chain = b;
4180 else
4182 while (b1->next)
4183 b1 = b1->next;
4184 b1->next = b;
4186 return b;
4189 /* Initialize loc->function_name. */
4190 static void
4191 set_breakpoint_location_function (struct bp_location *loc)
4193 if (loc->owner->type == bp_breakpoint
4194 || loc->owner->type == bp_hardware_breakpoint)
4196 find_pc_partial_function (loc->address, &(loc->function_name),
4197 NULL, NULL);
4198 if (loc->function_name)
4199 loc->function_name = xstrdup (loc->function_name);
4203 /* set_raw_breakpoint is a low level routine for allocating and
4204 partially initializing a breakpoint of type BPTYPE. The newly
4205 created breakpoint's address, section, source file name, and line
4206 number are provided by SAL. The newly created and partially
4207 initialized breakpoint is added to the breakpoint chain and
4208 is also returned as the value of this function.
4210 It is expected that the caller will complete the initialization of
4211 the newly created breakpoint struct as well as output any status
4212 information regarding the creation of a new breakpoint. In
4213 particular, set_raw_breakpoint does NOT set the breakpoint
4214 number! Care should be taken to not allow an error to occur
4215 prior to completing the initialization of the breakpoint. If this
4216 should happen, a bogus breakpoint will be left on the chain. */
4218 struct breakpoint *
4219 set_raw_breakpoint (struct symtab_and_line sal, enum bptype bptype)
4221 struct breakpoint *b = set_raw_breakpoint_without_location (bptype);
4222 CORE_ADDR adjusted_address;
4224 /* Adjust the breakpoint's address prior to allocating a location.
4225 Once we call allocate_bp_location(), that mostly uninitialized
4226 location will be placed on the location chain. Adjustment of the
4227 breakpoint may cause target_read_memory() to be called and we do
4228 not want its scan of the location chain to find a breakpoint and
4229 location that's only been partially initialized. */
4230 adjusted_address = adjust_breakpoint_address (sal.pc, b->type);
4232 b->loc = allocate_bp_location (b);
4233 b->loc->requested_address = sal.pc;
4234 b->loc->address = adjusted_address;
4236 if (sal.symtab == NULL)
4237 b->source_file = NULL;
4238 else
4239 b->source_file = savestring (sal.symtab->filename,
4240 strlen (sal.symtab->filename));
4241 b->loc->section = sal.section;
4242 b->line_number = sal.line;
4244 set_breakpoint_location_function (b->loc);
4246 breakpoints_changed ();
4248 return b;
4252 /* Note that the breakpoint object B describes a permanent breakpoint
4253 instruction, hard-wired into the inferior's code. */
4254 void
4255 make_breakpoint_permanent (struct breakpoint *b)
4257 struct bp_location *bl;
4258 b->enable_state = bp_permanent;
4260 /* By definition, permanent breakpoints are already present in the code.
4261 Mark all locations as inserted. For now, make_breakpoint_permanent
4262 is called in just one place, so it's hard to say if it's reasonable
4263 to have permanent breakpoint with multiple locations or not,
4264 but it's easy to implmement. */
4265 for (bl = b->loc; bl; bl = bl->next)
4266 bl->inserted = 1;
4269 static struct breakpoint *
4270 create_internal_breakpoint (CORE_ADDR address, enum bptype type)
4272 static int internal_breakpoint_number = -1;
4273 struct symtab_and_line sal;
4274 struct breakpoint *b;
4276 init_sal (&sal); /* initialize to zeroes */
4278 sal.pc = address;
4279 sal.section = find_pc_overlay (sal.pc);
4281 b = set_raw_breakpoint (sal, type);
4282 b->number = internal_breakpoint_number--;
4283 b->disposition = disp_donttouch;
4285 return b;
4289 static void
4290 create_longjmp_breakpoint (char *func_name)
4292 struct minimal_symbol *m;
4294 if ((m = lookup_minimal_symbol_text (func_name, NULL)) == NULL)
4295 return;
4296 set_momentary_breakpoint_at_pc (SYMBOL_VALUE_ADDRESS (m), bp_longjmp);
4297 update_global_location_list (1);
4300 /* Call this routine when stepping and nexting to enable a breakpoint
4301 if we do a longjmp(). When we hit that breakpoint, call
4302 set_longjmp_resume_breakpoint() to figure out where we are going. */
4304 void
4305 set_longjmp_breakpoint (void)
4307 if (gdbarch_get_longjmp_target_p (current_gdbarch))
4309 create_longjmp_breakpoint ("longjmp");
4310 create_longjmp_breakpoint ("_longjmp");
4311 create_longjmp_breakpoint ("siglongjmp");
4312 create_longjmp_breakpoint ("_siglongjmp");
4316 /* Delete all longjmp breakpoints from THREAD. */
4317 void
4318 delete_longjmp_breakpoint (int thread)
4320 struct breakpoint *b, *temp;
4322 ALL_BREAKPOINTS_SAFE (b, temp)
4323 if (b->type == bp_longjmp)
4325 if (b->thread == thread)
4326 delete_breakpoint (b);
4330 static void
4331 create_overlay_event_breakpoint_1 (char *func_name, struct objfile *objfile)
4333 struct breakpoint *b;
4334 struct minimal_symbol *m;
4336 if ((m = lookup_minimal_symbol_text (func_name, objfile)) == NULL)
4337 return;
4339 b = create_internal_breakpoint (SYMBOL_VALUE_ADDRESS (m),
4340 bp_overlay_event);
4341 b->addr_string = xstrdup (func_name);
4343 if (overlay_debugging == ovly_auto)
4345 b->enable_state = bp_enabled;
4346 overlay_events_enabled = 1;
4348 else
4350 b->enable_state = bp_disabled;
4351 overlay_events_enabled = 0;
4353 update_global_location_list (1);
4356 static void
4357 create_overlay_event_breakpoint (char *func_name)
4359 struct objfile *objfile;
4360 ALL_OBJFILES (objfile)
4361 create_overlay_event_breakpoint_1 (func_name, objfile);
4364 void
4365 enable_overlay_breakpoints (void)
4367 struct breakpoint *b;
4369 ALL_BREAKPOINTS (b)
4370 if (b->type == bp_overlay_event)
4372 b->enable_state = bp_enabled;
4373 update_global_location_list (1);
4374 overlay_events_enabled = 1;
4378 void
4379 disable_overlay_breakpoints (void)
4381 struct breakpoint *b;
4383 ALL_BREAKPOINTS (b)
4384 if (b->type == bp_overlay_event)
4386 b->enable_state = bp_disabled;
4387 update_global_location_list (0);
4388 overlay_events_enabled = 0;
4392 struct breakpoint *
4393 create_thread_event_breakpoint (CORE_ADDR address)
4395 struct breakpoint *b;
4397 b = create_internal_breakpoint (address, bp_thread_event);
4399 b->enable_state = bp_enabled;
4400 /* addr_string has to be used or breakpoint_re_set will delete me. */
4401 b->addr_string = xstrprintf ("*0x%s", paddr (b->loc->address));
4403 update_global_location_list_nothrow (1);
4405 return b;
4408 void
4409 remove_thread_event_breakpoints (void)
4411 struct breakpoint *b, *temp;
4413 ALL_BREAKPOINTS_SAFE (b, temp)
4414 if (b->type == bp_thread_event)
4415 delete_breakpoint (b);
4418 struct captured_parse_breakpoint_args
4420 char **arg_p;
4421 struct symtabs_and_lines *sals_p;
4422 char ***addr_string_p;
4423 int *not_found_ptr;
4426 struct lang_and_radix
4428 enum language lang;
4429 int radix;
4433 void
4434 remove_solib_event_breakpoints (void)
4436 struct breakpoint *b, *temp;
4438 ALL_BREAKPOINTS_SAFE (b, temp)
4439 if (b->type == bp_shlib_event)
4440 delete_breakpoint (b);
4443 struct breakpoint *
4444 create_solib_event_breakpoint (CORE_ADDR address)
4446 struct breakpoint *b;
4448 b = create_internal_breakpoint (address, bp_shlib_event);
4449 update_global_location_list_nothrow (1);
4450 return b;
4453 /* Disable any breakpoints that are on code in shared libraries. Only
4454 apply to enabled breakpoints, disabled ones can just stay disabled. */
4456 void
4457 disable_breakpoints_in_shlibs (void)
4459 struct bp_location *loc;
4461 ALL_BP_LOCATIONS (loc)
4463 struct breakpoint *b = loc->owner;
4464 /* We apply the check to all breakpoints, including disabled
4465 for those with loc->duplicate set. This is so that when breakpoint
4466 becomes enabled, or the duplicate is removed, gdb will try to insert
4467 all breakpoints. If we don't set shlib_disabled here, we'll try
4468 to insert those breakpoints and fail. */
4469 if (((b->type == bp_breakpoint) || (b->type == bp_hardware_breakpoint))
4470 && !loc->shlib_disabled
4471 #ifdef PC_SOLIB
4472 && PC_SOLIB (loc->address)
4473 #else
4474 && solib_name_from_address (loc->address)
4475 #endif
4478 loc->shlib_disabled = 1;
4483 /* Disable any breakpoints that are in in an unloaded shared library. Only
4484 apply to enabled breakpoints, disabled ones can just stay disabled. */
4486 static void
4487 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
4489 struct bp_location *loc;
4490 int disabled_shlib_breaks = 0;
4492 /* SunOS a.out shared libraries are always mapped, so do not
4493 disable breakpoints; they will only be reported as unloaded
4494 through clear_solib when GDB discards its shared library
4495 list. See clear_solib for more information. */
4496 if (exec_bfd != NULL
4497 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
4498 return;
4500 ALL_BP_LOCATIONS (loc)
4502 struct breakpoint *b = loc->owner;
4503 if ((loc->loc_type == bp_loc_hardware_breakpoint
4504 || loc->loc_type == bp_loc_software_breakpoint)
4505 && !loc->shlib_disabled)
4507 #ifdef PC_SOLIB
4508 char *so_name = PC_SOLIB (loc->address);
4509 #else
4510 char *so_name = solib_name_from_address (loc->address);
4511 #endif
4512 if (so_name && !strcmp (so_name, solib->so_name))
4514 loc->shlib_disabled = 1;
4515 /* At this point, we cannot rely on remove_breakpoint
4516 succeeding so we must mark the breakpoint as not inserted
4517 to prevent future errors occurring in remove_breakpoints. */
4518 loc->inserted = 0;
4519 if (!disabled_shlib_breaks)
4521 target_terminal_ours_for_output ();
4522 warning (_("Temporarily disabling breakpoints for unloaded shared library \"%s\""),
4523 so_name);
4525 disabled_shlib_breaks = 1;
4531 /* FORK & VFORK catchpoints. */
4533 /* Implement the "insert" breakpoint_ops method for fork catchpoints. */
4535 static void
4536 insert_catch_fork (struct breakpoint *b)
4538 target_insert_fork_catchpoint (PIDGET (inferior_ptid));
4541 /* Implement the "remove" breakpoint_ops method for fork catchpoints. */
4543 static int
4544 remove_catch_fork (struct breakpoint *b)
4546 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
4549 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
4550 catchpoints. */
4552 static int
4553 breakpoint_hit_catch_fork (struct breakpoint *b)
4555 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
4558 /* Implement the "print_it" breakpoint_ops method for fork catchpoints. */
4560 static enum print_stop_action
4561 print_it_catch_fork (struct breakpoint *b)
4563 annotate_catchpoint (b->number);
4564 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
4565 b->number, ptid_get_pid (b->forked_inferior_pid));
4566 return PRINT_SRC_AND_LOC;
4569 /* Implement the "print_one" breakpoint_ops method for fork catchpoints. */
4571 static void
4572 print_one_catch_fork (struct breakpoint *b, CORE_ADDR *last_addr)
4574 struct value_print_options opts;
4576 get_user_print_options (&opts);
4578 /* Field 4, the address, is omitted (which makes the columns
4579 not line up too nicely with the headers, but the effect
4580 is relatively readable). */
4581 if (opts.addressprint)
4582 ui_out_field_skip (uiout, "addr");
4583 annotate_field (5);
4584 ui_out_text (uiout, "fork");
4585 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
4587 ui_out_text (uiout, ", process ");
4588 ui_out_field_int (uiout, "what",
4589 ptid_get_pid (b->forked_inferior_pid));
4590 ui_out_spaces (uiout, 1);
4594 /* Implement the "print_mention" breakpoint_ops method for fork
4595 catchpoints. */
4597 static void
4598 print_mention_catch_fork (struct breakpoint *b)
4600 printf_filtered (_("Catchpoint %d (fork)"), b->number);
4603 /* The breakpoint_ops structure to be used in fork catchpoints. */
4605 static struct breakpoint_ops catch_fork_breakpoint_ops =
4607 insert_catch_fork,
4608 remove_catch_fork,
4609 breakpoint_hit_catch_fork,
4610 print_it_catch_fork,
4611 print_one_catch_fork,
4612 print_mention_catch_fork
4615 /* Implement the "insert" breakpoint_ops method for vfork catchpoints. */
4617 static void
4618 insert_catch_vfork (struct breakpoint *b)
4620 target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
4623 /* Implement the "remove" breakpoint_ops method for vfork catchpoints. */
4625 static int
4626 remove_catch_vfork (struct breakpoint *b)
4628 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
4631 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
4632 catchpoints. */
4634 static int
4635 breakpoint_hit_catch_vfork (struct breakpoint *b)
4637 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
4640 /* Implement the "print_it" breakpoint_ops method for vfork catchpoints. */
4642 static enum print_stop_action
4643 print_it_catch_vfork (struct breakpoint *b)
4645 annotate_catchpoint (b->number);
4646 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
4647 b->number, ptid_get_pid (b->forked_inferior_pid));
4648 return PRINT_SRC_AND_LOC;
4651 /* Implement the "print_one" breakpoint_ops method for vfork catchpoints. */
4653 static void
4654 print_one_catch_vfork (struct breakpoint *b, CORE_ADDR *last_addr)
4656 struct value_print_options opts;
4658 get_user_print_options (&opts);
4659 /* Field 4, the address, is omitted (which makes the columns
4660 not line up too nicely with the headers, but the effect
4661 is relatively readable). */
4662 if (opts.addressprint)
4663 ui_out_field_skip (uiout, "addr");
4664 annotate_field (5);
4665 ui_out_text (uiout, "vfork");
4666 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
4668 ui_out_text (uiout, ", process ");
4669 ui_out_field_int (uiout, "what",
4670 ptid_get_pid (b->forked_inferior_pid));
4671 ui_out_spaces (uiout, 1);
4675 /* Implement the "print_mention" breakpoint_ops method for vfork
4676 catchpoints. */
4678 static void
4679 print_mention_catch_vfork (struct breakpoint *b)
4681 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
4684 /* The breakpoint_ops structure to be used in vfork catchpoints. */
4686 static struct breakpoint_ops catch_vfork_breakpoint_ops =
4688 insert_catch_vfork,
4689 remove_catch_vfork,
4690 breakpoint_hit_catch_vfork,
4691 print_it_catch_vfork,
4692 print_one_catch_vfork,
4693 print_mention_catch_vfork
4696 /* Create a new breakpoint of the bp_catchpoint kind and return it.
4698 If TEMPFLAG is non-zero, then make the breakpoint temporary.
4699 If COND_STRING is not NULL, then store it in the breakpoint.
4700 OPS, if not NULL, is the breakpoint_ops structure associated
4701 to the catchpoint. */
4703 static struct breakpoint *
4704 create_catchpoint (int tempflag, char *cond_string,
4705 struct breakpoint_ops *ops)
4707 struct symtab_and_line sal;
4708 struct breakpoint *b;
4710 init_sal (&sal);
4711 sal.pc = 0;
4712 sal.symtab = NULL;
4713 sal.line = 0;
4715 b = set_raw_breakpoint (sal, bp_catchpoint);
4716 set_breakpoint_count (breakpoint_count + 1);
4717 b->number = breakpoint_count;
4719 b->cond_string = (cond_string == NULL) ?
4720 NULL : savestring (cond_string, strlen (cond_string));
4721 b->thread = -1;
4722 b->addr_string = NULL;
4723 b->enable_state = bp_enabled;
4724 b->disposition = tempflag ? disp_del : disp_donttouch;
4725 b->ops = ops;
4727 mention (b);
4728 update_global_location_list (1);
4730 return b;
4733 static void
4734 create_fork_vfork_event_catchpoint (int tempflag, char *cond_string,
4735 struct breakpoint_ops *ops)
4737 struct breakpoint *b = create_catchpoint (tempflag, cond_string, ops);
4739 /* FIXME: We should put this information in a breakpoint private data
4740 area. */
4741 b->forked_inferior_pid = null_ptid;
4744 /* Exec catchpoints. */
4746 static void
4747 insert_catch_exec (struct breakpoint *b)
4749 target_insert_exec_catchpoint (PIDGET (inferior_ptid));
4752 static int
4753 remove_catch_exec (struct breakpoint *b)
4755 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
4758 static int
4759 breakpoint_hit_catch_exec (struct breakpoint *b)
4761 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
4764 static enum print_stop_action
4765 print_it_catch_exec (struct breakpoint *b)
4767 annotate_catchpoint (b->number);
4768 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
4769 b->exec_pathname);
4770 return PRINT_SRC_AND_LOC;
4773 static void
4774 print_one_catch_exec (struct breakpoint *b, CORE_ADDR *last_addr)
4776 struct value_print_options opts;
4778 get_user_print_options (&opts);
4780 /* Field 4, the address, is omitted (which makes the columns
4781 not line up too nicely with the headers, but the effect
4782 is relatively readable). */
4783 if (opts.addressprint)
4784 ui_out_field_skip (uiout, "addr");
4785 annotate_field (5);
4786 ui_out_text (uiout, "exec");
4787 if (b->exec_pathname != NULL)
4789 ui_out_text (uiout, ", program \"");
4790 ui_out_field_string (uiout, "what", b->exec_pathname);
4791 ui_out_text (uiout, "\" ");
4795 static void
4796 print_mention_catch_exec (struct breakpoint *b)
4798 printf_filtered (_("Catchpoint %d (exec)"), b->number);
4801 static struct breakpoint_ops catch_exec_breakpoint_ops =
4803 insert_catch_exec,
4804 remove_catch_exec,
4805 breakpoint_hit_catch_exec,
4806 print_it_catch_exec,
4807 print_one_catch_exec,
4808 print_mention_catch_exec
4811 static int
4812 hw_breakpoint_used_count (void)
4814 struct breakpoint *b;
4815 int i = 0;
4817 ALL_BREAKPOINTS (b)
4819 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
4820 i++;
4823 return i;
4826 static int
4827 hw_watchpoint_used_count (enum bptype type, int *other_type_used)
4829 struct breakpoint *b;
4830 int i = 0;
4832 *other_type_used = 0;
4833 ALL_BREAKPOINTS (b)
4835 if (breakpoint_enabled (b))
4837 if (b->type == type)
4838 i++;
4839 else if ((b->type == bp_hardware_watchpoint ||
4840 b->type == bp_read_watchpoint ||
4841 b->type == bp_access_watchpoint))
4842 *other_type_used = 1;
4845 return i;
4848 void
4849 disable_watchpoints_before_interactive_call_start (void)
4851 struct breakpoint *b;
4853 ALL_BREAKPOINTS (b)
4855 if (((b->type == bp_watchpoint)
4856 || (b->type == bp_hardware_watchpoint)
4857 || (b->type == bp_read_watchpoint)
4858 || (b->type == bp_access_watchpoint))
4859 && breakpoint_enabled (b))
4861 b->enable_state = bp_call_disabled;
4862 update_global_location_list (0);
4867 void
4868 enable_watchpoints_after_interactive_call_stop (void)
4870 struct breakpoint *b;
4872 ALL_BREAKPOINTS (b)
4874 if (((b->type == bp_watchpoint)
4875 || (b->type == bp_hardware_watchpoint)
4876 || (b->type == bp_read_watchpoint)
4877 || (b->type == bp_access_watchpoint))
4878 && (b->enable_state == bp_call_disabled))
4880 b->enable_state = bp_enabled;
4881 update_global_location_list (1);
4887 /* Set a breakpoint that will evaporate an end of command
4888 at address specified by SAL.
4889 Restrict it to frame FRAME if FRAME is nonzero. */
4891 struct breakpoint *
4892 set_momentary_breakpoint (struct symtab_and_line sal, struct frame_id frame_id,
4893 enum bptype type)
4895 struct breakpoint *b;
4896 b = set_raw_breakpoint (sal, type);
4897 b->enable_state = bp_enabled;
4898 b->disposition = disp_donttouch;
4899 b->frame_id = frame_id;
4901 /* If we're debugging a multi-threaded program, then we
4902 want momentary breakpoints to be active in only a
4903 single thread of control. */
4904 if (in_thread_list (inferior_ptid))
4905 b->thread = pid_to_thread_id (inferior_ptid);
4907 update_global_location_list_nothrow (1);
4909 return b;
4912 struct breakpoint *
4913 set_momentary_breakpoint_at_pc (CORE_ADDR pc, enum bptype type)
4915 struct symtab_and_line sal;
4917 sal = find_pc_line (pc, 0);
4918 sal.pc = pc;
4919 sal.section = find_pc_overlay (pc);
4920 sal.explicit_pc = 1;
4922 return set_momentary_breakpoint (sal, null_frame_id, type);
4926 /* Tell the user we have just set a breakpoint B. */
4928 static void
4929 mention (struct breakpoint *b)
4931 int say_where = 0;
4932 struct cleanup *ui_out_chain;
4933 struct value_print_options opts;
4935 get_user_print_options (&opts);
4937 /* FIXME: This is misplaced; mention() is called by things (like
4938 hitting a watchpoint) other than breakpoint creation. It should
4939 be possible to clean this up and at the same time replace the
4940 random calls to breakpoint_changed with this hook. */
4941 observer_notify_breakpoint_created (b->number);
4943 if (b->ops != NULL && b->ops->print_mention != NULL)
4944 b->ops->print_mention (b);
4945 else
4946 switch (b->type)
4948 case bp_none:
4949 printf_filtered (_("(apparently deleted?) Eventpoint %d: "), b->number);
4950 break;
4951 case bp_watchpoint:
4952 ui_out_text (uiout, "Watchpoint ");
4953 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
4954 ui_out_field_int (uiout, "number", b->number);
4955 ui_out_text (uiout, ": ");
4956 ui_out_field_string (uiout, "exp", b->exp_string);
4957 do_cleanups (ui_out_chain);
4958 break;
4959 case bp_hardware_watchpoint:
4960 ui_out_text (uiout, "Hardware watchpoint ");
4961 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
4962 ui_out_field_int (uiout, "number", b->number);
4963 ui_out_text (uiout, ": ");
4964 ui_out_field_string (uiout, "exp", b->exp_string);
4965 do_cleanups (ui_out_chain);
4966 break;
4967 case bp_read_watchpoint:
4968 ui_out_text (uiout, "Hardware read watchpoint ");
4969 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
4970 ui_out_field_int (uiout, "number", b->number);
4971 ui_out_text (uiout, ": ");
4972 ui_out_field_string (uiout, "exp", b->exp_string);
4973 do_cleanups (ui_out_chain);
4974 break;
4975 case bp_access_watchpoint:
4976 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
4977 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
4978 ui_out_field_int (uiout, "number", b->number);
4979 ui_out_text (uiout, ": ");
4980 ui_out_field_string (uiout, "exp", b->exp_string);
4981 do_cleanups (ui_out_chain);
4982 break;
4983 case bp_breakpoint:
4984 if (ui_out_is_mi_like_p (uiout))
4986 say_where = 0;
4987 break;
4989 if (b->disposition == disp_del)
4990 printf_filtered (_("Temporary breakpoint"));
4991 else
4992 printf_filtered (_("Breakpoint"));
4993 printf_filtered (_(" %d"), b->number);
4994 say_where = 1;
4995 break;
4996 case bp_hardware_breakpoint:
4997 if (ui_out_is_mi_like_p (uiout))
4999 say_where = 0;
5000 break;
5002 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
5003 say_where = 1;
5004 break;
5006 case bp_until:
5007 case bp_finish:
5008 case bp_longjmp:
5009 case bp_longjmp_resume:
5010 case bp_step_resume:
5011 case bp_call_dummy:
5012 case bp_watchpoint_scope:
5013 case bp_shlib_event:
5014 case bp_thread_event:
5015 case bp_overlay_event:
5016 break;
5019 if (say_where)
5021 /* i18n: cagney/2005-02-11: Below needs to be merged into a
5022 single string. */
5023 if (b->loc == NULL)
5025 printf_filtered (_(" (%s) pending."), b->addr_string);
5027 else
5029 if (opts.addressprint || b->source_file == NULL)
5031 printf_filtered (" at ");
5032 fputs_filtered (paddress (b->loc->address), gdb_stdout);
5034 if (b->source_file)
5035 printf_filtered (": file %s, line %d.",
5036 b->source_file, b->line_number);
5038 if (b->loc->next)
5040 struct bp_location *loc = b->loc;
5041 int n = 0;
5042 for (; loc; loc = loc->next)
5043 ++n;
5044 printf_filtered (" (%d locations)", n);
5049 if (ui_out_is_mi_like_p (uiout))
5050 return;
5051 printf_filtered ("\n");
5055 static struct bp_location *
5056 add_location_to_breakpoint (struct breakpoint *b,
5057 const struct symtab_and_line *sal)
5059 struct bp_location *loc, **tmp;
5061 loc = allocate_bp_location (b);
5062 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
5064 *tmp = loc;
5065 loc->requested_address = sal->pc;
5066 loc->address = adjust_breakpoint_address (loc->requested_address, b->type);
5067 loc->section = sal->section;
5069 set_breakpoint_location_function (loc);
5070 return loc;
5074 /* Return 1 if LOC is pointing to a permanent breakpoint,
5075 return 0 otherwise. */
5077 static int
5078 bp_loc_is_permanent (struct bp_location *loc)
5080 int len;
5081 CORE_ADDR addr;
5082 const gdb_byte *brk;
5083 gdb_byte *target_mem;
5084 struct cleanup *cleanup;
5085 int retval = 0;
5087 gdb_assert (loc != NULL);
5089 addr = loc->address;
5090 brk = gdbarch_breakpoint_from_pc (current_gdbarch, &addr, &len);
5092 /* Software breakpoints unsupported? */
5093 if (brk == NULL)
5094 return 0;
5096 target_mem = alloca (len);
5098 /* Enable the automatic memory restoration from breakpoints while
5099 we read the memory. Otherwise we could say about our temporary
5100 breakpoints they are permanent. */
5101 cleanup = make_show_memory_breakpoints_cleanup (0);
5103 if (target_read_memory (loc->address, target_mem, len) == 0
5104 && memcmp (target_mem, brk, len) == 0)
5105 retval = 1;
5107 do_cleanups (cleanup);
5109 return retval;
5114 /* Create a breakpoint with SAL as location. Use ADDR_STRING
5115 as textual description of the location, and COND_STRING
5116 as condition expression. */
5118 static void
5119 create_breakpoint (struct symtabs_and_lines sals, char *addr_string,
5120 char *cond_string,
5121 enum bptype type, enum bpdisp disposition,
5122 int thread, int ignore_count,
5123 struct breakpoint_ops *ops, int from_tty, int enabled)
5125 struct breakpoint *b = NULL;
5126 int i;
5128 if (type == bp_hardware_breakpoint)
5130 int i = hw_breakpoint_used_count ();
5131 int target_resources_ok =
5132 TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint,
5133 i + 1, 0);
5134 if (target_resources_ok == 0)
5135 error (_("No hardware breakpoint support in the target."));
5136 else if (target_resources_ok < 0)
5137 error (_("Hardware breakpoints used exceeds limit."));
5140 for (i = 0; i < sals.nelts; ++i)
5142 struct symtab_and_line sal = sals.sals[i];
5143 struct bp_location *loc;
5145 if (from_tty)
5146 describe_other_breakpoints (sal.pc, sal.section, thread);
5148 if (i == 0)
5150 b = set_raw_breakpoint (sal, type);
5151 set_breakpoint_count (breakpoint_count + 1);
5152 b->number = breakpoint_count;
5153 b->thread = thread;
5155 b->cond_string = cond_string;
5156 b->ignore_count = ignore_count;
5157 b->enable_state = enabled ? bp_enabled : bp_disabled;
5158 b->disposition = disposition;
5160 loc = b->loc;
5162 else
5164 loc = add_location_to_breakpoint (b, &sal);
5167 if (bp_loc_is_permanent (loc))
5168 make_breakpoint_permanent (b);
5170 if (b->cond_string)
5172 char *arg = b->cond_string;
5173 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
5174 if (*arg)
5175 error (_("Garbage %s follows condition"), arg);
5179 if (addr_string)
5180 b->addr_string = addr_string;
5181 else
5182 /* addr_string has to be used or breakpoint_re_set will delete
5183 me. */
5184 b->addr_string = xstrprintf ("*0x%s", paddr (b->loc->address));
5186 b->ops = ops;
5187 mention (b);
5190 /* Remove element at INDEX_TO_REMOVE from SAL, shifting other
5191 elements to fill the void space. */
5192 static void
5193 remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
5195 int i = index_to_remove+1;
5196 int last_index = sal->nelts-1;
5198 for (;i <= last_index; ++i)
5199 sal->sals[i-1] = sal->sals[i];
5201 --(sal->nelts);
5204 /* If appropriate, obtains all sals that correspond
5205 to the same file and line as SAL. This is done
5206 only if SAL does not have explicit PC and has
5207 line and file information. If we got just a single
5208 expanded sal, return the original.
5210 Otherwise, if SAL.explicit_line is not set, filter out
5211 all sals for which the name of enclosing function
5212 is different from SAL. This makes sure that if we have
5213 breakpoint originally set in template instantiation, say
5214 foo<int>(), we won't expand SAL to locations at the same
5215 line in all existing instantiations of 'foo'.
5218 static struct symtabs_and_lines
5219 expand_line_sal_maybe (struct symtab_and_line sal)
5221 struct symtabs_and_lines expanded;
5222 CORE_ADDR original_pc = sal.pc;
5223 char *original_function = NULL;
5224 int found;
5225 int i;
5227 /* If we have explicit pc, don't expand.
5228 If we have no line number, we can't expand. */
5229 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
5231 expanded.nelts = 1;
5232 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5233 expanded.sals[0] = sal;
5234 return expanded;
5237 sal.pc = 0;
5238 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
5240 expanded = expand_line_sal (sal);
5241 if (expanded.nelts == 1)
5243 /* We had one sal, we got one sal. Without futher
5244 processing, just return the original sal. */
5245 xfree (expanded.sals);
5246 expanded.nelts = 1;
5247 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5248 sal.pc = original_pc;
5249 expanded.sals[0] = sal;
5250 return expanded;
5253 if (!sal.explicit_line)
5255 CORE_ADDR func_addr, func_end;
5256 for (i = 0; i < expanded.nelts; ++i)
5258 CORE_ADDR pc = expanded.sals[i].pc;
5259 char *this_function;
5260 if (find_pc_partial_function (pc, &this_function,
5261 &func_addr, &func_end))
5263 if (this_function &&
5264 strcmp (this_function, original_function) != 0)
5266 remove_sal (&expanded, i);
5267 --i;
5269 else if (func_addr == pc)
5271 /* We're at beginning of a function, and should
5272 skip prologue. */
5273 struct symbol *sym = find_pc_function (pc);
5274 if (sym)
5275 expanded.sals[i] = find_function_start_sal (sym, 1);
5276 else
5277 expanded.sals[i].pc
5278 = gdbarch_skip_prologue (current_gdbarch, pc);
5285 if (expanded.nelts <= 1)
5287 /* This is un ugly workaround. If we get zero
5288 expanded sals then something is really wrong.
5289 Fix that by returnign the original sal. */
5290 xfree (expanded.sals);
5291 expanded.nelts = 1;
5292 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5293 sal.pc = original_pc;
5294 expanded.sals[0] = sal;
5295 return expanded;
5298 if (original_pc)
5300 found = 0;
5301 for (i = 0; i < expanded.nelts; ++i)
5302 if (expanded.sals[i].pc == original_pc)
5304 found = 1;
5305 break;
5307 gdb_assert (found);
5310 return expanded;
5313 /* Add SALS.nelts breakpoints to the breakpoint table. For each
5314 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
5315 value. COND_STRING, if not NULL, specified the condition to be
5316 used for all breakpoints. Essentially the only case where
5317 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
5318 function. In that case, it's still not possible to specify
5319 separate conditions for different overloaded functions, so
5320 we take just a single condition string.
5322 NOTE: If the function succeeds, the caller is expected to cleanup
5323 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
5324 array contents). If the function fails (error() is called), the
5325 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
5326 COND and SALS arrays and each of those arrays contents. */
5328 static void
5329 create_breakpoints (struct symtabs_and_lines sals, char **addr_string,
5330 char *cond_string,
5331 enum bptype type, enum bpdisp disposition,
5332 int thread, int ignore_count,
5333 struct breakpoint_ops *ops, int from_tty,
5334 int enabled)
5336 int i;
5337 for (i = 0; i < sals.nelts; ++i)
5339 struct symtabs_and_lines expanded =
5340 expand_line_sal_maybe (sals.sals[i]);
5342 create_breakpoint (expanded, addr_string[i],
5343 cond_string, type, disposition,
5344 thread, ignore_count, ops, from_tty, enabled);
5347 update_global_location_list (1);
5350 /* Parse ARG which is assumed to be a SAL specification possibly
5351 followed by conditionals. On return, SALS contains an array of SAL
5352 addresses found. ADDR_STRING contains a vector of (canonical)
5353 address strings. ARG points to the end of the SAL. */
5355 static void
5356 parse_breakpoint_sals (char **address,
5357 struct symtabs_and_lines *sals,
5358 char ***addr_string,
5359 int *not_found_ptr)
5361 char *addr_start = *address;
5362 *addr_string = NULL;
5363 /* If no arg given, or if first arg is 'if ', use the default
5364 breakpoint. */
5365 if ((*address) == NULL
5366 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
5368 if (default_breakpoint_valid)
5370 struct symtab_and_line sal;
5371 init_sal (&sal); /* initialize to zeroes */
5372 sals->sals = (struct symtab_and_line *)
5373 xmalloc (sizeof (struct symtab_and_line));
5374 sal.pc = default_breakpoint_address;
5375 sal.line = default_breakpoint_line;
5376 sal.symtab = default_breakpoint_symtab;
5377 sal.section = find_pc_overlay (sal.pc);
5378 sals->sals[0] = sal;
5379 sals->nelts = 1;
5381 else
5382 error (_("No default breakpoint address now."));
5384 else
5386 /* Force almost all breakpoints to be in terms of the
5387 current_source_symtab (which is decode_line_1's default). This
5388 should produce the results we want almost all of the time while
5389 leaving default_breakpoint_* alone.
5390 ObjC: However, don't match an Objective-C method name which
5391 may have a '+' or '-' succeeded by a '[' */
5393 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
5395 if (default_breakpoint_valid
5396 && (!cursal.symtab
5397 || ((strchr ("+-", (*address)[0]) != NULL)
5398 && ((*address)[1] != '['))))
5399 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
5400 default_breakpoint_line, addr_string,
5401 not_found_ptr);
5402 else
5403 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
5404 addr_string, not_found_ptr);
5406 /* For any SAL that didn't have a canonical string, fill one in. */
5407 if (sals->nelts > 0 && *addr_string == NULL)
5408 *addr_string = xcalloc (sals->nelts, sizeof (char **));
5409 if (addr_start != (*address))
5411 int i;
5412 for (i = 0; i < sals->nelts; i++)
5414 /* Add the string if not present. */
5415 if ((*addr_string)[i] == NULL)
5416 (*addr_string)[i] = savestring (addr_start, (*address) - addr_start);
5422 /* Convert each SAL into a real PC. Verify that the PC can be
5423 inserted as a breakpoint. If it can't throw an error. */
5425 static void
5426 breakpoint_sals_to_pc (struct symtabs_and_lines *sals,
5427 char *address)
5429 int i;
5430 for (i = 0; i < sals->nelts; i++)
5431 resolve_sal_pc (&sals->sals[i]);
5434 static void
5435 do_captured_parse_breakpoint (struct ui_out *ui, void *data)
5437 struct captured_parse_breakpoint_args *args = data;
5439 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
5440 args->not_found_ptr);
5443 /* Given TOK, a string specification of condition and thread, as
5444 accepted by the 'break' command, extract the condition
5445 string and thread number and set *COND_STRING and *THREAD.
5446 PC identifies the context at which the condition should be parsed.
5447 If no condition is found, *COND_STRING is set to NULL.
5448 If no thread is found, *THREAD is set to -1. */
5449 static void
5450 find_condition_and_thread (char *tok, CORE_ADDR pc,
5451 char **cond_string, int *thread)
5453 *cond_string = NULL;
5454 *thread = -1;
5455 while (tok && *tok)
5457 char *end_tok;
5458 int toklen;
5459 char *cond_start = NULL;
5460 char *cond_end = NULL;
5461 while (*tok == ' ' || *tok == '\t')
5462 tok++;
5464 end_tok = tok;
5466 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5467 end_tok++;
5469 toklen = end_tok - tok;
5471 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
5473 struct expression *expr;
5475 tok = cond_start = end_tok + 1;
5476 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
5477 xfree (expr);
5478 cond_end = tok;
5479 *cond_string = savestring (cond_start,
5480 cond_end - cond_start);
5482 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
5484 char *tmptok;
5486 tok = end_tok + 1;
5487 tmptok = tok;
5488 *thread = strtol (tok, &tok, 0);
5489 if (tok == tmptok)
5490 error (_("Junk after thread keyword."));
5491 if (!valid_thread_id (*thread))
5492 error (_("Unknown thread %d."), *thread);
5494 else
5495 error (_("Junk at end of arguments."));
5499 /* Set a breakpoint. This function is shared between
5500 CLI and MI functions for setting a breakpoint.
5501 This function has two major modes of operations,
5502 selected by the PARSE_CONDITION_AND_THREAD parameter.
5503 If non-zero, the function will parse arg, extracting
5504 breakpoint location, address and thread. Otherwise,
5505 ARG is just the location of breakpoint, with condition
5506 and thread specified by the COND_STRING and THREAD
5507 parameters. */
5509 static void
5510 break_command_really (char *arg, char *cond_string, int thread,
5511 int parse_condition_and_thread,
5512 int tempflag, int hardwareflag,
5513 int ignore_count,
5514 enum auto_boolean pending_break_support,
5515 struct breakpoint_ops *ops,
5516 int from_tty,
5517 int enabled)
5519 struct gdb_exception e;
5520 struct symtabs_and_lines sals;
5521 struct symtab_and_line pending_sal;
5522 char *copy_arg;
5523 char *err_msg;
5524 char *addr_start = arg;
5525 char **addr_string;
5526 struct cleanup *old_chain;
5527 struct cleanup *breakpoint_chain = NULL;
5528 struct captured_parse_breakpoint_args parse_args;
5529 int i;
5530 int pending = 0;
5531 int not_found = 0;
5533 sals.sals = NULL;
5534 sals.nelts = 0;
5535 addr_string = NULL;
5537 parse_args.arg_p = &arg;
5538 parse_args.sals_p = &sals;
5539 parse_args.addr_string_p = &addr_string;
5540 parse_args.not_found_ptr = &not_found;
5542 e = catch_exception (uiout, do_captured_parse_breakpoint,
5543 &parse_args, RETURN_MASK_ALL);
5545 /* If caller is interested in rc value from parse, set value. */
5546 switch (e.reason)
5548 case RETURN_QUIT:
5549 throw_exception (e);
5550 case RETURN_ERROR:
5551 switch (e.error)
5553 case NOT_FOUND_ERROR:
5555 /* If pending breakpoint support is turned off, throw
5556 error. */
5558 if (pending_break_support == AUTO_BOOLEAN_FALSE)
5559 throw_exception (e);
5561 exception_print (gdb_stderr, e);
5563 /* If pending breakpoint support is auto query and the user
5564 selects no, then simply return the error code. */
5565 if (pending_break_support == AUTO_BOOLEAN_AUTO &&
5566 !nquery ("Make breakpoint pending on future shared library load? "))
5567 return;
5569 /* At this point, either the user was queried about setting
5570 a pending breakpoint and selected yes, or pending
5571 breakpoint behavior is on and thus a pending breakpoint
5572 is defaulted on behalf of the user. */
5573 copy_arg = xstrdup (addr_start);
5574 addr_string = &copy_arg;
5575 sals.nelts = 1;
5576 sals.sals = &pending_sal;
5577 pending_sal.pc = 0;
5578 pending = 1;
5579 break;
5580 default:
5581 throw_exception (e);
5583 default:
5584 if (!sals.nelts)
5585 return;
5588 /* Create a chain of things that always need to be cleaned up. */
5589 old_chain = make_cleanup (null_cleanup, 0);
5591 if (!pending)
5593 /* Make sure that all storage allocated to SALS gets freed. */
5594 make_cleanup (xfree, sals.sals);
5596 /* Cleanup the addr_string array but not its contents. */
5597 make_cleanup (xfree, addr_string);
5600 /* ----------------------------- SNIP -----------------------------
5601 Anything added to the cleanup chain beyond this point is assumed
5602 to be part of a breakpoint. If the breakpoint create succeeds
5603 then the memory is not reclaimed. */
5604 breakpoint_chain = make_cleanup (null_cleanup, 0);
5606 /* Mark the contents of the addr_string for cleanup. These go on
5607 the breakpoint_chain and only occure if the breakpoint create
5608 fails. */
5609 for (i = 0; i < sals.nelts; i++)
5611 if (addr_string[i] != NULL)
5612 make_cleanup (xfree, addr_string[i]);
5615 /* Resolve all line numbers to PC's and verify that the addresses
5616 are ok for the target. */
5617 if (!pending)
5618 breakpoint_sals_to_pc (&sals, addr_start);
5620 /* Verify that condition can be parsed, before setting any
5621 breakpoints. Allocate a separate condition expression for each
5622 breakpoint. */
5623 if (!pending)
5625 if (parse_condition_and_thread)
5627 /* Here we only parse 'arg' to separate condition
5628 from thread number, so parsing in context of first
5629 sal is OK. When setting the breakpoint we'll
5630 re-parse it in context of each sal. */
5631 cond_string = NULL;
5632 thread = -1;
5633 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string, &thread);
5634 if (cond_string)
5635 make_cleanup (xfree, cond_string);
5637 else
5639 /* Create a private copy of condition string. */
5640 if (cond_string)
5642 cond_string = xstrdup (cond_string);
5643 make_cleanup (xfree, cond_string);
5646 create_breakpoints (sals, addr_string, cond_string,
5647 hardwareflag ? bp_hardware_breakpoint
5648 : bp_breakpoint,
5649 tempflag ? disp_del : disp_donttouch,
5650 thread, ignore_count, ops, from_tty, enabled);
5652 else
5654 struct symtab_and_line sal = {0};
5655 struct breakpoint *b;
5657 make_cleanup (xfree, copy_arg);
5659 b = set_raw_breakpoint_without_location (hardwareflag
5660 ? bp_hardware_breakpoint
5661 : bp_breakpoint);
5662 set_breakpoint_count (breakpoint_count + 1);
5663 b->number = breakpoint_count;
5664 b->thread = -1;
5665 b->addr_string = addr_string[0];
5666 b->cond_string = NULL;
5667 b->ignore_count = ignore_count;
5668 b->disposition = tempflag ? disp_del : disp_donttouch;
5669 b->condition_not_parsed = 1;
5670 b->ops = ops;
5671 b->enable_state = enabled ? bp_enabled : bp_disabled;
5673 update_global_location_list (1);
5674 mention (b);
5677 if (sals.nelts > 1)
5678 warning (_("Multiple breakpoints were set.\n"
5679 "Use the \"delete\" command to delete unwanted breakpoints."));
5680 /* That's it. Discard the cleanups for data inserted into the
5681 breakpoint. */
5682 discard_cleanups (breakpoint_chain);
5683 /* But cleanup everything else. */
5684 do_cleanups (old_chain);
5687 /* Set a breakpoint.
5688 ARG is a string describing breakpoint address,
5689 condition, and thread.
5690 FLAG specifies if a breakpoint is hardware on,
5691 and if breakpoint is temporary, using BP_HARDWARE_FLAG
5692 and BP_TEMPFLAG. */
5694 static void
5695 break_command_1 (char *arg, int flag, int from_tty)
5697 int hardwareflag = flag & BP_HARDWAREFLAG;
5698 int tempflag = flag & BP_TEMPFLAG;
5700 break_command_really (arg,
5701 NULL, 0, 1 /* parse arg */,
5702 tempflag, hardwareflag,
5703 0 /* Ignore count */,
5704 pending_break_support,
5705 NULL /* breakpoint_ops */,
5706 from_tty,
5707 1 /* enabled */);
5711 void
5712 set_breakpoint (char *address, char *condition,
5713 int hardwareflag, int tempflag,
5714 int thread, int ignore_count,
5715 int pending, int enabled)
5717 break_command_really (address, condition, thread,
5718 0 /* condition and thread are valid. */,
5719 tempflag, hardwareflag,
5720 ignore_count,
5721 pending
5722 ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
5723 NULL, 0, enabled);
5726 /* Adjust SAL to the first instruction past the function prologue.
5727 The end of the prologue is determined using the line table from
5728 the debugging information.
5730 If SAL is already past the prologue, then do nothing. */
5732 static void
5733 skip_prologue_sal (struct symtab_and_line *sal)
5735 struct symbol *sym = find_pc_function (sal->pc);
5736 struct symtab_and_line start_sal;
5738 if (sym == NULL)
5739 return;
5741 start_sal = find_function_start_sal (sym, 1);
5742 if (sal->pc < start_sal.pc)
5743 *sal = start_sal;
5746 /* Helper function for break_command_1 and disassemble_command. */
5748 void
5749 resolve_sal_pc (struct symtab_and_line *sal)
5751 CORE_ADDR pc;
5753 if (sal->pc == 0 && sal->symtab != NULL)
5755 if (!find_line_pc (sal->symtab, sal->line, &pc))
5756 error (_("No line %d in file \"%s\"."),
5757 sal->line, sal->symtab->filename);
5758 sal->pc = pc;
5760 /* If this SAL corresponds to a breakpoint inserted using
5761 a line number, then skip the function prologue if necessary. */
5762 if (sal->explicit_line)
5763 skip_prologue_sal (sal);
5766 if (sal->section == 0 && sal->symtab != NULL)
5768 struct blockvector *bv;
5769 struct block *b;
5770 struct symbol *sym;
5772 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
5773 if (bv != NULL)
5775 sym = block_linkage_function (b);
5776 if (sym != NULL)
5778 fixup_symbol_section (sym, sal->symtab->objfile);
5779 sal->section = SYMBOL_OBJ_SECTION (sym);
5781 else
5783 /* It really is worthwhile to have the section, so we'll just
5784 have to look harder. This case can be executed if we have
5785 line numbers but no functions (as can happen in assembly
5786 source). */
5788 struct minimal_symbol *msym;
5790 msym = lookup_minimal_symbol_by_pc (sal->pc);
5791 if (msym)
5792 sal->section = SYMBOL_OBJ_SECTION (msym);
5798 void
5799 break_command (char *arg, int from_tty)
5801 break_command_1 (arg, 0, from_tty);
5804 void
5805 tbreak_command (char *arg, int from_tty)
5807 break_command_1 (arg, BP_TEMPFLAG, from_tty);
5810 static void
5811 hbreak_command (char *arg, int from_tty)
5813 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
5816 static void
5817 thbreak_command (char *arg, int from_tty)
5819 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
5822 static void
5823 stop_command (char *arg, int from_tty)
5825 printf_filtered (_("Specify the type of breakpoint to set.\n\
5826 Usage: stop in <function | address>\n\
5827 stop at <line>\n"));
5830 static void
5831 stopin_command (char *arg, int from_tty)
5833 int badInput = 0;
5835 if (arg == (char *) NULL)
5836 badInput = 1;
5837 else if (*arg != '*')
5839 char *argptr = arg;
5840 int hasColon = 0;
5842 /* look for a ':'. If this is a line number specification, then
5843 say it is bad, otherwise, it should be an address or
5844 function/method name */
5845 while (*argptr && !hasColon)
5847 hasColon = (*argptr == ':');
5848 argptr++;
5851 if (hasColon)
5852 badInput = (*argptr != ':'); /* Not a class::method */
5853 else
5854 badInput = isdigit (*arg); /* a simple line number */
5857 if (badInput)
5858 printf_filtered (_("Usage: stop in <function | address>\n"));
5859 else
5860 break_command_1 (arg, 0, from_tty);
5863 static void
5864 stopat_command (char *arg, int from_tty)
5866 int badInput = 0;
5868 if (arg == (char *) NULL || *arg == '*') /* no line number */
5869 badInput = 1;
5870 else
5872 char *argptr = arg;
5873 int hasColon = 0;
5875 /* look for a ':'. If there is a '::' then get out, otherwise
5876 it is probably a line number. */
5877 while (*argptr && !hasColon)
5879 hasColon = (*argptr == ':');
5880 argptr++;
5883 if (hasColon)
5884 badInput = (*argptr == ':'); /* we have class::method */
5885 else
5886 badInput = !isdigit (*arg); /* not a line number */
5889 if (badInput)
5890 printf_filtered (_("Usage: stop at <line>\n"));
5891 else
5892 break_command_1 (arg, 0, from_tty);
5895 /* accessflag: hw_write: watch write,
5896 hw_read: watch read,
5897 hw_access: watch access (read or write) */
5898 static void
5899 watch_command_1 (char *arg, int accessflag, int from_tty)
5901 struct breakpoint *b, *scope_breakpoint = NULL;
5902 struct symtab_and_line sal;
5903 struct expression *exp;
5904 struct block *exp_valid_block;
5905 struct value *val, *mark;
5906 struct frame_info *frame;
5907 struct frame_info *prev_frame = NULL;
5908 char *exp_start = NULL;
5909 char *exp_end = NULL;
5910 char *tok, *id_tok_start, *end_tok;
5911 int toklen;
5912 char *cond_start = NULL;
5913 char *cond_end = NULL;
5914 struct expression *cond = NULL;
5915 int i, other_type_used, target_resources_ok = 0;
5916 enum bptype bp_type;
5917 int mem_cnt = 0;
5918 int thread = -1;
5920 init_sal (&sal); /* initialize to zeroes */
5922 /* Make sure that we actually have parameters to parse. */
5923 if (arg != NULL && arg[0] != '\0')
5925 toklen = strlen (arg); /* Size of argument list. */
5927 /* Points tok to the end of the argument list. */
5928 tok = arg + toklen - 1;
5930 /* Go backwards in the parameters list. Skip the last parameter.
5931 If we're expecting a 'thread <thread_num>' parameter, this should
5932 be the thread identifier. */
5933 while (tok > arg && (*tok == ' ' || *tok == '\t'))
5934 tok--;
5935 while (tok > arg && (*tok != ' ' && *tok != '\t'))
5936 tok--;
5938 /* Points end_tok to the beginning of the last token. */
5939 id_tok_start = tok + 1;
5941 /* Go backwards in the parameters list. Skip one more parameter.
5942 If we're expecting a 'thread <thread_num>' parameter, we should
5943 reach a "thread" token. */
5944 while (tok > arg && (*tok == ' ' || *tok == '\t'))
5945 tok--;
5947 end_tok = tok;
5949 while (tok > arg && (*tok != ' ' && *tok != '\t'))
5950 tok--;
5952 /* Move the pointer forward to skip the whitespace and
5953 calculate the length of the token. */
5954 tok++;
5955 toklen = end_tok - tok;
5957 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
5959 /* At this point we've found a "thread" token, which means
5960 the user is trying to set a watchpoint that triggers
5961 only in a specific thread. */
5962 char *endp;
5964 /* Extract the thread ID from the next token. */
5965 thread = strtol (id_tok_start, &endp, 0);
5967 /* Check if the user provided a valid numeric value for the
5968 thread ID. */
5969 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5970 error (_("Invalid thread ID specification %s."), id_tok_start);
5972 /* Check if the thread actually exists. */
5973 if (!valid_thread_id (thread))
5974 error (_("Unknown thread %d."), thread);
5976 /* Truncate the string and get rid of the thread <thread_num>
5977 parameter before the parameter list is parsed by the
5978 evaluate_expression() function. */
5979 *tok = '\0';
5983 /* Parse the rest of the arguments. */
5984 innermost_block = NULL;
5985 exp_start = arg;
5986 exp = parse_exp_1 (&arg, 0, 0);
5987 exp_end = arg;
5988 /* Remove trailing whitespace from the expression before saving it.
5989 This makes the eventual display of the expression string a bit
5990 prettier. */
5991 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
5992 --exp_end;
5994 exp_valid_block = innermost_block;
5995 mark = value_mark ();
5996 fetch_watchpoint_value (exp, &val, NULL, NULL);
5997 if (val != NULL)
5998 release_value (val);
6000 tok = arg;
6001 while (*tok == ' ' || *tok == '\t')
6002 tok++;
6003 end_tok = tok;
6005 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
6006 end_tok++;
6008 toklen = end_tok - tok;
6009 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
6011 tok = cond_start = end_tok + 1;
6012 cond = parse_exp_1 (&tok, 0, 0);
6013 cond_end = tok;
6015 if (*tok)
6016 error (_("Junk at end of command."));
6018 if (accessflag == hw_read)
6019 bp_type = bp_read_watchpoint;
6020 else if (accessflag == hw_access)
6021 bp_type = bp_access_watchpoint;
6022 else
6023 bp_type = bp_hardware_watchpoint;
6025 mem_cnt = can_use_hardware_watchpoint (val);
6026 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
6027 error (_("Expression cannot be implemented with read/access watchpoint."));
6028 if (mem_cnt != 0)
6030 i = hw_watchpoint_used_count (bp_type, &other_type_used);
6031 target_resources_ok =
6032 TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_type, i + mem_cnt,
6033 other_type_used);
6034 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
6035 error (_("Target does not support this type of hardware watchpoint."));
6037 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
6038 error (_("Target can only support one kind of HW watchpoint at a time."));
6041 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
6042 watchpoint could not be set. */
6043 if (!mem_cnt || target_resources_ok <= 0)
6044 bp_type = bp_watchpoint;
6046 frame = block_innermost_frame (exp_valid_block);
6047 if (frame)
6048 prev_frame = get_prev_frame (frame);
6049 else
6050 prev_frame = NULL;
6052 /* If the expression is "local", then set up a "watchpoint scope"
6053 breakpoint at the point where we've left the scope of the watchpoint
6054 expression. Create the scope breakpoint before the watchpoint, so
6055 that we will encounter it first in bpstat_stop_status. */
6056 if (innermost_block && prev_frame)
6058 scope_breakpoint = create_internal_breakpoint (get_frame_pc (prev_frame),
6059 bp_watchpoint_scope);
6061 scope_breakpoint->enable_state = bp_enabled;
6063 /* Automatically delete the breakpoint when it hits. */
6064 scope_breakpoint->disposition = disp_del;
6066 /* Only break in the proper frame (help with recursion). */
6067 scope_breakpoint->frame_id = get_frame_id (prev_frame);
6069 /* Set the address at which we will stop. */
6070 scope_breakpoint->loc->requested_address
6071 = get_frame_pc (prev_frame);
6072 scope_breakpoint->loc->address
6073 = adjust_breakpoint_address (scope_breakpoint->loc->requested_address,
6074 scope_breakpoint->type);
6077 /* Now set up the breakpoint. */
6078 b = set_raw_breakpoint (sal, bp_type);
6079 set_breakpoint_count (breakpoint_count + 1);
6080 b->number = breakpoint_count;
6081 b->thread = thread;
6082 b->disposition = disp_donttouch;
6083 b->exp = exp;
6084 b->exp_valid_block = exp_valid_block;
6085 b->exp_string = savestring (exp_start, exp_end - exp_start);
6086 b->val = val;
6087 b->val_valid = 1;
6088 b->loc->cond = cond;
6089 if (cond_start)
6090 b->cond_string = savestring (cond_start, cond_end - cond_start);
6091 else
6092 b->cond_string = 0;
6094 if (frame)
6095 b->watchpoint_frame = get_frame_id (frame);
6096 else
6097 b->watchpoint_frame = null_frame_id;
6099 if (scope_breakpoint != NULL)
6101 /* The scope breakpoint is related to the watchpoint. We will
6102 need to act on them together. */
6103 b->related_breakpoint = scope_breakpoint;
6104 scope_breakpoint->related_breakpoint = b;
6107 value_free_to_mark (mark);
6108 mention (b);
6109 update_global_location_list (1);
6112 /* Return count of locations need to be watched and can be handled
6113 in hardware. If the watchpoint can not be handled
6114 in hardware return zero. */
6116 static int
6117 can_use_hardware_watchpoint (struct value *v)
6119 int found_memory_cnt = 0;
6120 struct value *head = v;
6122 /* Did the user specifically forbid us to use hardware watchpoints? */
6123 if (!can_use_hw_watchpoints)
6124 return 0;
6126 /* Make sure that the value of the expression depends only upon
6127 memory contents, and values computed from them within GDB. If we
6128 find any register references or function calls, we can't use a
6129 hardware watchpoint.
6131 The idea here is that evaluating an expression generates a series
6132 of values, one holding the value of every subexpression. (The
6133 expression a*b+c has five subexpressions: a, b, a*b, c, and
6134 a*b+c.) GDB's values hold almost enough information to establish
6135 the criteria given above --- they identify memory lvalues,
6136 register lvalues, computed values, etcetera. So we can evaluate
6137 the expression, and then scan the chain of values that leaves
6138 behind to decide whether we can detect any possible change to the
6139 expression's final value using only hardware watchpoints.
6141 However, I don't think that the values returned by inferior
6142 function calls are special in any way. So this function may not
6143 notice that an expression involving an inferior function call
6144 can't be watched with hardware watchpoints. FIXME. */
6145 for (; v; v = value_next (v))
6147 if (VALUE_LVAL (v) == lval_memory)
6149 if (value_lazy (v))
6150 /* A lazy memory lvalue is one that GDB never needed to fetch;
6151 we either just used its address (e.g., `a' in `a.b') or
6152 we never needed it at all (e.g., `a' in `a,b'). */
6154 else
6156 /* Ahh, memory we actually used! Check if we can cover
6157 it with hardware watchpoints. */
6158 struct type *vtype = check_typedef (value_type (v));
6160 /* We only watch structs and arrays if user asked for it
6161 explicitly, never if they just happen to appear in a
6162 middle of some value chain. */
6163 if (v == head
6164 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
6165 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
6167 CORE_ADDR vaddr = VALUE_ADDRESS (v) + value_offset (v);
6168 int len = TYPE_LENGTH (value_type (v));
6170 if (!TARGET_REGION_OK_FOR_HW_WATCHPOINT (vaddr, len))
6171 return 0;
6172 else
6173 found_memory_cnt++;
6177 else if (VALUE_LVAL (v) != not_lval
6178 && deprecated_value_modifiable (v) == 0)
6179 return 0; /* ??? What does this represent? */
6180 else if (VALUE_LVAL (v) == lval_register)
6181 return 0; /* cannot watch a register with a HW watchpoint */
6184 /* The expression itself looks suitable for using a hardware
6185 watchpoint, but give the target machine a chance to reject it. */
6186 return found_memory_cnt;
6189 void
6190 watch_command_wrapper (char *arg, int from_tty)
6192 watch_command (arg, from_tty);
6195 static void
6196 watch_command (char *arg, int from_tty)
6198 watch_command_1 (arg, hw_write, from_tty);
6201 void
6202 rwatch_command_wrapper (char *arg, int from_tty)
6204 rwatch_command (arg, from_tty);
6207 static void
6208 rwatch_command (char *arg, int from_tty)
6210 watch_command_1 (arg, hw_read, from_tty);
6213 void
6214 awatch_command_wrapper (char *arg, int from_tty)
6216 awatch_command (arg, from_tty);
6219 static void
6220 awatch_command (char *arg, int from_tty)
6222 watch_command_1 (arg, hw_access, from_tty);
6226 /* Helper routines for the until_command routine in infcmd.c. Here
6227 because it uses the mechanisms of breakpoints. */
6229 struct until_break_command_continuation_args
6231 struct breakpoint *breakpoint;
6232 struct breakpoint *breakpoint2;
6235 /* This function is called by fetch_inferior_event via the
6236 cmd_continuation pointer, to complete the until command. It takes
6237 care of cleaning up the temporary breakpoints set up by the until
6238 command. */
6239 static void
6240 until_break_command_continuation (void *arg)
6242 struct until_break_command_continuation_args *a = arg;
6244 delete_breakpoint (a->breakpoint);
6245 if (a->breakpoint2)
6246 delete_breakpoint (a->breakpoint2);
6249 void
6250 until_break_command (char *arg, int from_tty, int anywhere)
6252 struct symtabs_and_lines sals;
6253 struct symtab_and_line sal;
6254 struct frame_info *frame = get_selected_frame (NULL);
6255 struct frame_info *prev_frame = get_prev_frame (frame);
6256 struct breakpoint *breakpoint;
6257 struct breakpoint *breakpoint2 = NULL;
6258 struct cleanup *old_chain;
6260 clear_proceed_status ();
6262 /* Set a breakpoint where the user wants it and at return from
6263 this function */
6265 if (default_breakpoint_valid)
6266 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
6267 default_breakpoint_line, (char ***) NULL, NULL);
6268 else
6269 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
6270 0, (char ***) NULL, NULL);
6272 if (sals.nelts != 1)
6273 error (_("Couldn't get information on specified line."));
6275 sal = sals.sals[0];
6276 xfree (sals.sals); /* malloc'd, so freed */
6278 if (*arg)
6279 error (_("Junk at end of arguments."));
6281 resolve_sal_pc (&sal);
6283 if (anywhere)
6284 /* If the user told us to continue until a specified location,
6285 we don't specify a frame at which we need to stop. */
6286 breakpoint = set_momentary_breakpoint (sal, null_frame_id, bp_until);
6287 else
6288 /* Otherwise, specify the current frame, because we want to stop only
6289 at the very same frame. */
6290 breakpoint = set_momentary_breakpoint (sal, get_frame_id (frame),
6291 bp_until);
6293 old_chain = make_cleanup_delete_breakpoint (breakpoint);
6295 /* Keep within the current frame, or in frames called by the current
6296 one. */
6297 if (prev_frame)
6299 sal = find_pc_line (get_frame_pc (prev_frame), 0);
6300 sal.pc = get_frame_pc (prev_frame);
6301 breakpoint2 = set_momentary_breakpoint (sal, get_frame_id (prev_frame),
6302 bp_until);
6303 make_cleanup_delete_breakpoint (breakpoint2);
6306 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
6308 /* If we are running asynchronously, and proceed call above has actually
6309 managed to start the target, arrange for breakpoints to be
6310 deleted when the target stops. Otherwise, we're already stopped and
6311 delete breakpoints via cleanup chain. */
6313 if (target_can_async_p () && is_running (inferior_ptid))
6315 struct until_break_command_continuation_args *args;
6316 args = xmalloc (sizeof (*args));
6318 args->breakpoint = breakpoint;
6319 args->breakpoint2 = breakpoint2;
6321 discard_cleanups (old_chain);
6322 add_continuation (inferior_thread (),
6323 until_break_command_continuation, args,
6324 xfree);
6326 else
6327 do_cleanups (old_chain);
6330 static void
6331 ep_skip_leading_whitespace (char **s)
6333 if ((s == NULL) || (*s == NULL))
6334 return;
6335 while (isspace (**s))
6336 *s += 1;
6339 /* This function attempts to parse an optional "if <cond>" clause
6340 from the arg string. If one is not found, it returns NULL.
6342 Else, it returns a pointer to the condition string. (It does not
6343 attempt to evaluate the string against a particular block.) And,
6344 it updates arg to point to the first character following the parsed
6345 if clause in the arg string. */
6347 static char *
6348 ep_parse_optional_if_clause (char **arg)
6350 char *cond_string;
6352 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
6353 return NULL;
6355 /* Skip the "if" keyword. */
6356 (*arg) += 2;
6358 /* Skip any extra leading whitespace, and record the start of the
6359 condition string. */
6360 ep_skip_leading_whitespace (arg);
6361 cond_string = *arg;
6363 /* Assume that the condition occupies the remainder of the arg string. */
6364 (*arg) += strlen (cond_string);
6366 return cond_string;
6369 /* This function attempts to parse an optional filename from the arg
6370 string. If one is not found, it returns NULL.
6372 Else, it returns a pointer to the parsed filename. (This function
6373 makes no attempt to verify that a file of that name exists, or is
6374 accessible.) And, it updates arg to point to the first character
6375 following the parsed filename in the arg string.
6377 Note that clients needing to preserve the returned filename for
6378 future access should copy it to their own buffers. */
6379 static char *
6380 ep_parse_optional_filename (char **arg)
6382 static char filename[1024];
6383 char *arg_p = *arg;
6384 int i;
6385 char c;
6387 if ((*arg_p == '\0') || isspace (*arg_p))
6388 return NULL;
6390 for (i = 0;; i++)
6392 c = *arg_p;
6393 if (isspace (c))
6394 c = '\0';
6395 filename[i] = c;
6396 if (c == '\0')
6397 break;
6398 arg_p++;
6400 *arg = arg_p;
6402 return filename;
6405 /* Commands to deal with catching events, such as signals, exceptions,
6406 process start/exit, etc. */
6408 typedef enum
6410 catch_fork_temporary, catch_vfork_temporary,
6411 catch_fork_permanent, catch_vfork_permanent
6413 catch_fork_kind;
6415 static void
6416 catch_fork_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
6418 char *cond_string = NULL;
6419 catch_fork_kind fork_kind;
6420 int tempflag;
6422 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
6423 tempflag = (fork_kind == catch_fork_temporary
6424 || fork_kind == catch_vfork_temporary);
6426 if (!arg)
6427 arg = "";
6428 ep_skip_leading_whitespace (&arg);
6430 /* The allowed syntax is:
6431 catch [v]fork
6432 catch [v]fork if <cond>
6434 First, check if there's an if clause. */
6435 cond_string = ep_parse_optional_if_clause (&arg);
6437 if ((*arg != '\0') && !isspace (*arg))
6438 error (_("Junk at end of arguments."));
6440 /* If this target supports it, create a fork or vfork catchpoint
6441 and enable reporting of such events. */
6442 switch (fork_kind)
6444 case catch_fork_temporary:
6445 case catch_fork_permanent:
6446 create_fork_vfork_event_catchpoint (tempflag, cond_string,
6447 &catch_fork_breakpoint_ops);
6448 break;
6449 case catch_vfork_temporary:
6450 case catch_vfork_permanent:
6451 create_fork_vfork_event_catchpoint (tempflag, cond_string,
6452 &catch_vfork_breakpoint_ops);
6453 break;
6454 default:
6455 error (_("unsupported or unknown fork kind; cannot catch it"));
6456 break;
6460 static void
6461 catch_exec_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
6463 int tempflag;
6464 char *cond_string = NULL;
6466 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6468 if (!arg)
6469 arg = "";
6470 ep_skip_leading_whitespace (&arg);
6472 /* The allowed syntax is:
6473 catch exec
6474 catch exec if <cond>
6476 First, check if there's an if clause. */
6477 cond_string = ep_parse_optional_if_clause (&arg);
6479 if ((*arg != '\0') && !isspace (*arg))
6480 error (_("Junk at end of arguments."));
6482 /* If this target supports it, create an exec catchpoint
6483 and enable reporting of such events. */
6484 create_catchpoint (tempflag, cond_string, &catch_exec_breakpoint_ops);
6487 static enum print_stop_action
6488 print_exception_catchpoint (struct breakpoint *b)
6490 int bp_temp, bp_throw;
6492 annotate_catchpoint (b->number);
6494 bp_throw = strstr (b->addr_string, "throw") != NULL;
6495 if (b->loc->address != b->loc->requested_address)
6496 breakpoint_adjustment_warning (b->loc->requested_address,
6497 b->loc->address,
6498 b->number, 1);
6499 bp_temp = b->loc->owner->disposition == disp_del;
6500 ui_out_text (uiout,
6501 bp_temp ? "Temporary catchpoint "
6502 : "Catchpoint ");
6503 if (!ui_out_is_mi_like_p (uiout))
6504 ui_out_field_int (uiout, "bkptno", b->number);
6505 ui_out_text (uiout,
6506 bp_throw ? " (exception thrown), "
6507 : " (exception caught), ");
6508 if (ui_out_is_mi_like_p (uiout))
6510 ui_out_field_string (uiout, "reason",
6511 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
6512 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6513 ui_out_field_int (uiout, "bkptno", b->number);
6515 return PRINT_SRC_AND_LOC;
6518 static void
6519 print_one_exception_catchpoint (struct breakpoint *b, CORE_ADDR *last_addr)
6521 struct value_print_options opts;
6522 get_user_print_options (&opts);
6523 if (opts.addressprint)
6525 annotate_field (4);
6526 if (b->loc == NULL || b->loc->shlib_disabled)
6527 ui_out_field_string (uiout, "addr", "<PENDING>");
6528 else
6529 ui_out_field_core_addr (uiout, "addr", b->loc->address);
6531 annotate_field (5);
6532 if (b->loc)
6533 *last_addr = b->loc->address;
6534 if (strstr (b->addr_string, "throw") != NULL)
6535 ui_out_field_string (uiout, "what", "exception throw");
6536 else
6537 ui_out_field_string (uiout, "what", "exception catch");
6540 static void
6541 print_mention_exception_catchpoint (struct breakpoint *b)
6543 int bp_temp;
6544 int bp_throw;
6546 bp_temp = b->loc->owner->disposition == disp_del;
6547 bp_throw = strstr (b->addr_string, "throw") != NULL;
6548 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
6549 : _("Catchpoint "));
6550 ui_out_field_int (uiout, "bkptno", b->number);
6551 ui_out_text (uiout, bp_throw ? _(" (throw)")
6552 : _(" (catch)"));
6555 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
6556 NULL, /* insert */
6557 NULL, /* remove */
6558 NULL, /* breakpoint_hit */
6559 print_exception_catchpoint,
6560 print_one_exception_catchpoint,
6561 print_mention_exception_catchpoint
6564 static int
6565 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
6566 enum exception_event_kind ex_event, int from_tty)
6568 char *trigger_func_name;
6570 if (ex_event == EX_EVENT_CATCH)
6571 trigger_func_name = "__cxa_begin_catch";
6572 else
6573 trigger_func_name = "__cxa_throw";
6575 break_command_really (trigger_func_name, cond_string, -1,
6576 0 /* condition and thread are valid. */,
6577 tempflag, 0,
6579 AUTO_BOOLEAN_TRUE /* pending */,
6580 &gnu_v3_exception_catchpoint_ops, from_tty,
6581 1 /* enabled */);
6583 return 1;
6586 /* Deal with "catch catch" and "catch throw" commands */
6588 static void
6589 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
6590 int tempflag, int from_tty)
6592 char *cond_string = NULL;
6593 struct symtab_and_line *sal = NULL;
6595 if (!arg)
6596 arg = "";
6597 ep_skip_leading_whitespace (&arg);
6599 cond_string = ep_parse_optional_if_clause (&arg);
6601 if ((*arg != '\0') && !isspace (*arg))
6602 error (_("Junk at end of arguments."));
6604 if ((ex_event != EX_EVENT_THROW) &&
6605 (ex_event != EX_EVENT_CATCH))
6606 error (_("Unsupported or unknown exception event; cannot catch it"));
6608 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
6609 return;
6611 warning (_("Unsupported with this platform/compiler combination."));
6614 /* Implementation of "catch catch" command. */
6616 static void
6617 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
6619 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6620 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
6623 /* Implementation of "catch throw" command. */
6625 static void
6626 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
6628 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6629 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
6632 /* Create a breakpoint struct for Ada exception catchpoints. */
6634 static void
6635 create_ada_exception_breakpoint (struct symtab_and_line sal,
6636 char *addr_string,
6637 char *exp_string,
6638 char *cond_string,
6639 struct expression *cond,
6640 struct breakpoint_ops *ops,
6641 int tempflag,
6642 int from_tty)
6644 struct breakpoint *b;
6646 if (from_tty)
6648 describe_other_breakpoints (sal.pc, sal.section, -1);
6649 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
6650 version for exception catchpoints, because two catchpoints
6651 used for different exception names will use the same address.
6652 In this case, a "breakpoint ... also set at..." warning is
6653 unproductive. Besides. the warning phrasing is also a bit
6654 inapropriate, we should use the word catchpoint, and tell
6655 the user what type of catchpoint it is. The above is good
6656 enough for now, though. */
6659 b = set_raw_breakpoint (sal, bp_breakpoint);
6660 set_breakpoint_count (breakpoint_count + 1);
6662 b->enable_state = bp_enabled;
6663 b->disposition = tempflag ? disp_del : disp_donttouch;
6664 b->number = breakpoint_count;
6665 b->ignore_count = 0;
6666 b->loc->cond = cond;
6667 b->addr_string = addr_string;
6668 b->language = language_ada;
6669 b->cond_string = cond_string;
6670 b->exp_string = exp_string;
6671 b->thread = -1;
6672 b->ops = ops;
6674 mention (b);
6675 update_global_location_list (1);
6678 /* Implement the "catch exception" command. */
6680 static void
6681 catch_ada_exception_command (char *arg, int from_tty,
6682 struct cmd_list_element *command)
6684 int tempflag;
6685 struct symtab_and_line sal;
6686 enum bptype type;
6687 char *addr_string = NULL;
6688 char *exp_string = NULL;
6689 char *cond_string = NULL;
6690 struct expression *cond = NULL;
6691 struct breakpoint_ops *ops = NULL;
6693 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6695 if (!arg)
6696 arg = "";
6697 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
6698 &cond_string, &cond, &ops);
6699 create_ada_exception_breakpoint (sal, addr_string, exp_string,
6700 cond_string, cond, ops, tempflag,
6701 from_tty);
6704 /* Implement the "catch assert" command. */
6706 static void
6707 catch_assert_command (char *arg, int from_tty, struct cmd_list_element *command)
6709 int tempflag;
6710 struct symtab_and_line sal;
6711 char *addr_string = NULL;
6712 struct breakpoint_ops *ops = NULL;
6714 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6716 if (!arg)
6717 arg = "";
6718 sal = ada_decode_assert_location (arg, &addr_string, &ops);
6719 create_ada_exception_breakpoint (sal, addr_string, NULL, NULL, NULL, ops,
6720 tempflag, from_tty);
6723 static void
6724 catch_command (char *arg, int from_tty)
6726 error (_("Catch requires an event name."));
6730 static void
6731 tcatch_command (char *arg, int from_tty)
6733 error (_("Catch requires an event name."));
6736 /* Delete breakpoints by address or line. */
6738 static void
6739 clear_command (char *arg, int from_tty)
6741 struct breakpoint *b;
6742 VEC(breakpoint_p) *found = 0;
6743 int ix;
6744 int default_match;
6745 struct symtabs_and_lines sals;
6746 struct symtab_and_line sal;
6747 int i;
6749 if (arg)
6751 sals = decode_line_spec (arg, 1);
6752 default_match = 0;
6754 else
6756 sals.sals = (struct symtab_and_line *)
6757 xmalloc (sizeof (struct symtab_and_line));
6758 make_cleanup (xfree, sals.sals);
6759 init_sal (&sal); /* initialize to zeroes */
6760 sal.line = default_breakpoint_line;
6761 sal.symtab = default_breakpoint_symtab;
6762 sal.pc = default_breakpoint_address;
6763 if (sal.symtab == 0)
6764 error (_("No source file specified."));
6766 sals.sals[0] = sal;
6767 sals.nelts = 1;
6769 default_match = 1;
6772 /* We don't call resolve_sal_pc here. That's not
6773 as bad as it seems, because all existing breakpoints
6774 typically have both file/line and pc set. So, if
6775 clear is given file/line, we can match this to existing
6776 breakpoint without obtaining pc at all.
6778 We only support clearing given the address explicitly
6779 present in breakpoint table. Say, we've set breakpoint
6780 at file:line. There were several PC values for that file:line,
6781 due to optimization, all in one block.
6782 We've picked one PC value. If "clear" is issued with another
6783 PC corresponding to the same file:line, the breakpoint won't
6784 be cleared. We probably can still clear the breakpoint, but
6785 since the other PC value is never presented to user, user
6786 can only find it by guessing, and it does not seem important
6787 to support that. */
6789 /* For each line spec given, delete bps which correspond
6790 to it. Do it in two passes, solely to preserve the current
6791 behavior that from_tty is forced true if we delete more than
6792 one breakpoint. */
6794 found = NULL;
6795 for (i = 0; i < sals.nelts; i++)
6797 /* If exact pc given, clear bpts at that pc.
6798 If line given (pc == 0), clear all bpts on specified line.
6799 If defaulting, clear all bpts on default line
6800 or at default pc.
6802 defaulting sal.pc != 0 tests to do
6804 0 1 pc
6805 1 1 pc _and_ line
6806 0 0 line
6807 1 0 <can't happen> */
6809 sal = sals.sals[i];
6811 /* Find all matching breakpoints and add them to
6812 'found'. */
6813 ALL_BREAKPOINTS (b)
6815 int match = 0;
6816 /* Are we going to delete b? */
6817 if (b->type != bp_none
6818 && b->type != bp_watchpoint
6819 && b->type != bp_hardware_watchpoint
6820 && b->type != bp_read_watchpoint
6821 && b->type != bp_access_watchpoint)
6823 struct bp_location *loc = b->loc;
6824 for (; loc; loc = loc->next)
6826 int pc_match = sal.pc
6827 && (loc->address == sal.pc)
6828 && (!section_is_overlay (loc->section)
6829 || loc->section == sal.section);
6830 int line_match = ((default_match || (0 == sal.pc))
6831 && b->source_file != NULL
6832 && sal.symtab != NULL
6833 && strcmp (b->source_file, sal.symtab->filename) == 0
6834 && b->line_number == sal.line);
6835 if (pc_match || line_match)
6837 match = 1;
6838 break;
6843 if (match)
6844 VEC_safe_push(breakpoint_p, found, b);
6847 /* Now go thru the 'found' chain and delete them. */
6848 if (VEC_empty(breakpoint_p, found))
6850 if (arg)
6851 error (_("No breakpoint at %s."), arg);
6852 else
6853 error (_("No breakpoint at this line."));
6856 if (VEC_length(breakpoint_p, found) > 1)
6857 from_tty = 1; /* Always report if deleted more than one */
6858 if (from_tty)
6860 if (VEC_length(breakpoint_p, found) == 1)
6861 printf_unfiltered (_("Deleted breakpoint "));
6862 else
6863 printf_unfiltered (_("Deleted breakpoints "));
6865 breakpoints_changed ();
6867 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
6869 if (from_tty)
6870 printf_unfiltered ("%d ", b->number);
6871 delete_breakpoint (b);
6873 if (from_tty)
6874 putchar_unfiltered ('\n');
6877 /* Delete breakpoint in BS if they are `delete' breakpoints and
6878 all breakpoints that are marked for deletion, whether hit or not.
6879 This is called after any breakpoint is hit, or after errors. */
6881 void
6882 breakpoint_auto_delete (bpstat bs)
6884 struct breakpoint *b, *temp;
6886 for (; bs; bs = bs->next)
6887 if (bs->breakpoint_at
6888 && bs->breakpoint_at->owner
6889 && bs->breakpoint_at->owner->disposition == disp_del
6890 && bs->stop)
6891 delete_breakpoint (bs->breakpoint_at->owner);
6893 ALL_BREAKPOINTS_SAFE (b, temp)
6895 if (b->disposition == disp_del_at_next_stop)
6896 delete_breakpoint (b);
6900 /* A cleanup function which destroys a vector. */
6902 static void
6903 do_vec_free (void *p)
6905 VEC(bp_location_p) **vec = p;
6906 if (*vec)
6907 VEC_free (bp_location_p, *vec);
6910 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
6911 into the inferior, only remove already-inserted locations that no
6912 longer should be inserted. Functions that delete a breakpoint or
6913 breakpoints should pass false, so that deleting a breakpoint
6914 doesn't have the side effect of inserting the locations of other
6915 breakpoints that are marked not-inserted, but should_be_inserted
6916 returns true on them.
6918 This behaviour is useful is situations close to tear-down -- e.g.,
6919 after an exec, while the target still has execution, but breakpoint
6920 shadows of the previous executable image should *NOT* be restored
6921 to the new image; or before detaching, where the target still has
6922 execution and wants to delete breakpoints from GDB's lists, and all
6923 breakpoints had already been removed from the inferior. */
6925 static void
6926 update_global_location_list (int should_insert)
6928 struct breakpoint *b;
6929 struct bp_location **next = &bp_location_chain;
6930 struct bp_location *loc;
6931 struct bp_location *loc2;
6932 VEC(bp_location_p) *old_locations = NULL;
6933 int ret;
6934 int ix;
6935 struct cleanup *cleanups;
6937 cleanups = make_cleanup (do_vec_free, &old_locations);
6938 /* Store old locations for future reference. */
6939 for (loc = bp_location_chain; loc; loc = loc->global_next)
6940 VEC_safe_push (bp_location_p, old_locations, loc);
6942 bp_location_chain = NULL;
6943 ALL_BREAKPOINTS (b)
6945 for (loc = b->loc; loc; loc = loc->next)
6947 *next = loc;
6948 next = &(loc->global_next);
6949 *next = NULL;
6953 /* Identify bp_location instances that are no longer present in the new
6954 list, and therefore should be freed. Note that it's not necessary that
6955 those locations should be removed from inferior -- if there's another
6956 location at the same address (previously marked as duplicate),
6957 we don't need to remove/insert the location. */
6958 for (ix = 0; VEC_iterate(bp_location_p, old_locations, ix, loc); ++ix)
6960 /* Tells if 'loc' is found amoung the new locations. If not, we
6961 have to free it. */
6962 int found_object = 0;
6963 /* Tells if the location should remain inserted in the target. */
6964 int keep_in_target = 0;
6965 int removed = 0;
6966 for (loc2 = bp_location_chain; loc2; loc2 = loc2->global_next)
6967 if (loc2 == loc)
6969 found_object = 1;
6970 break;
6973 /* If this location is no longer present, and inserted, look if there's
6974 maybe a new location at the same address. If so, mark that one
6975 inserted, and don't remove this one. This is needed so that we
6976 don't have a time window where a breakpoint at certain location is not
6977 inserted. */
6979 if (loc->inserted)
6981 /* If the location is inserted now, we might have to remove it. */
6983 if (found_object && should_be_inserted (loc))
6985 /* The location is still present in the location list, and still
6986 should be inserted. Don't do anything. */
6987 keep_in_target = 1;
6989 else
6991 /* The location is either no longer present, or got disabled.
6992 See if there's another location at the same address, in which
6993 case we don't need to remove this one from the target. */
6994 if (breakpoint_address_is_meaningful (loc->owner))
6995 for (loc2 = bp_location_chain; loc2; loc2 = loc2->global_next)
6997 /* For the sake of should_insert_location. The
6998 call to check_duplicates will fix up this later. */
6999 loc2->duplicate = 0;
7000 if (should_be_inserted (loc2)
7001 && loc2 != loc && loc2->address == loc->address)
7003 loc2->inserted = 1;
7004 loc2->target_info = loc->target_info;
7005 keep_in_target = 1;
7006 break;
7011 if (!keep_in_target)
7013 if (remove_breakpoint (loc, mark_uninserted))
7015 /* This is just about all we can do. We could keep this
7016 location on the global list, and try to remove it next
7017 time, but there's no particular reason why we will
7018 succeed next time.
7020 Note that at this point, loc->owner is still valid,
7021 as delete_breakpoint frees the breakpoint only
7022 after calling us. */
7023 printf_filtered (_("warning: Error removing breakpoint %d\n"),
7024 loc->owner->number);
7026 removed = 1;
7030 if (!found_object)
7032 if (removed && non_stop)
7034 /* This location was removed from the targets. In non-stop mode,
7035 a race condition is possible where we've removed a breakpoint,
7036 but stop events for that breakpoint are already queued and will
7037 arrive later. To suppress spurious SIGTRAPs reported to user,
7038 we keep this breakpoint location for a bit, and will retire it
7039 after we see 3 * thread_count events.
7040 The theory here is that reporting of events should,
7041 "on the average", be fair, so after that many event we'll see
7042 events from all threads that have anything of interest, and no
7043 longer need to keep this breakpoint. This is just a
7044 heuristic, but if it's wrong, we'll report unexpected SIGTRAP,
7045 which is usability issue, but not a correctness problem. */
7046 loc->events_till_retirement = 3 * (thread_count () + 1);
7047 loc->owner = NULL;
7049 VEC_safe_push (bp_location_p, moribund_locations, loc);
7051 else
7052 free_bp_location (loc);
7056 ALL_BREAKPOINTS (b)
7058 check_duplicates (b);
7061 if (breakpoints_always_inserted_mode () && should_insert
7062 && (target_has_execution
7063 || (gdbarch_has_global_solist (target_gdbarch)
7064 && target_supports_multi_process ())))
7065 insert_breakpoint_locations ();
7067 do_cleanups (cleanups);
7070 void
7071 breakpoint_retire_moribund (void)
7073 struct bp_location *loc;
7074 int ix;
7076 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
7077 if (--(loc->events_till_retirement) == 0)
7079 free_bp_location (loc);
7080 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
7081 --ix;
7085 static void
7086 update_global_location_list_nothrow (int inserting)
7088 struct gdb_exception e;
7089 TRY_CATCH (e, RETURN_MASK_ERROR)
7090 update_global_location_list (inserting);
7093 /* Clear BPT from a BPS. */
7094 static void
7095 bpstat_remove_breakpoint (bpstat bps, struct breakpoint *bpt)
7097 bpstat bs;
7098 for (bs = bps; bs; bs = bs->next)
7099 if (bs->breakpoint_at && bs->breakpoint_at->owner == bpt)
7101 bs->breakpoint_at = NULL;
7102 bs->old_val = NULL;
7103 /* bs->commands will be freed later. */
7107 /* Callback for iterate_over_threads. */
7108 static int
7109 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
7111 struct breakpoint *bpt = data;
7112 bpstat_remove_breakpoint (th->stop_bpstat, bpt);
7113 return 0;
7116 /* Delete a breakpoint and clean up all traces of it in the data
7117 structures. */
7119 void
7120 delete_breakpoint (struct breakpoint *bpt)
7122 struct breakpoint *b;
7123 struct bp_location *loc, *next;
7125 gdb_assert (bpt != NULL);
7127 /* Has this bp already been deleted? This can happen because multiple
7128 lists can hold pointers to bp's. bpstat lists are especial culprits.
7130 One example of this happening is a watchpoint's scope bp. When the
7131 scope bp triggers, we notice that the watchpoint is out of scope, and
7132 delete it. We also delete its scope bp. But the scope bp is marked
7133 "auto-deleting", and is already on a bpstat. That bpstat is then
7134 checked for auto-deleting bp's, which are deleted.
7136 A real solution to this problem might involve reference counts in bp's,
7137 and/or giving them pointers back to their referencing bpstat's, and
7138 teaching delete_breakpoint to only free a bp's storage when no more
7139 references were extent. A cheaper bandaid was chosen. */
7140 if (bpt->type == bp_none)
7141 return;
7143 observer_notify_breakpoint_deleted (bpt->number);
7145 if (breakpoint_chain == bpt)
7146 breakpoint_chain = bpt->next;
7148 ALL_BREAKPOINTS (b)
7149 if (b->next == bpt)
7151 b->next = bpt->next;
7152 break;
7155 free_command_lines (&bpt->commands);
7156 if (bpt->cond_string != NULL)
7157 xfree (bpt->cond_string);
7158 if (bpt->addr_string != NULL)
7159 xfree (bpt->addr_string);
7160 if (bpt->exp != NULL)
7161 xfree (bpt->exp);
7162 if (bpt->exp_string != NULL)
7163 xfree (bpt->exp_string);
7164 if (bpt->val != NULL)
7165 value_free (bpt->val);
7166 if (bpt->source_file != NULL)
7167 xfree (bpt->source_file);
7168 if (bpt->exec_pathname != NULL)
7169 xfree (bpt->exec_pathname);
7171 /* Be sure no bpstat's are pointing at it after it's been freed. */
7172 /* FIXME, how can we find all bpstat's?
7173 We just check stop_bpstat for now. Note that we cannot just
7174 remove bpstats pointing at bpt from the stop_bpstat list
7175 entirely, as breakpoint commands are associated with the bpstat;
7176 if we remove it here, then the later call to
7177 bpstat_do_actions (&stop_bpstat);
7178 in event-top.c won't do anything, and temporary breakpoints
7179 with commands won't work. */
7181 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
7183 /* Now that breakpoint is removed from breakpoint
7184 list, update the global location list. This
7185 will remove locations that used to belong to
7186 this breakpoint. Do this before freeing
7187 the breakpoint itself, since remove_breakpoint
7188 looks at location's owner. It might be better
7189 design to have location completely self-contained,
7190 but it's not the case now. */
7191 update_global_location_list (0);
7194 /* On the chance that someone will soon try again to delete this same
7195 bp, we mark it as deleted before freeing its storage. */
7196 bpt->type = bp_none;
7198 xfree (bpt);
7201 static void
7202 do_delete_breakpoint_cleanup (void *b)
7204 delete_breakpoint (b);
7207 struct cleanup *
7208 make_cleanup_delete_breakpoint (struct breakpoint *b)
7210 return make_cleanup (do_delete_breakpoint_cleanup, b);
7213 void
7214 delete_command (char *arg, int from_tty)
7216 struct breakpoint *b, *temp;
7218 dont_repeat ();
7220 if (arg == 0)
7222 int breaks_to_delete = 0;
7224 /* Delete all breakpoints if no argument.
7225 Do not delete internal or call-dummy breakpoints, these
7226 have to be deleted with an explicit breakpoint number argument. */
7227 ALL_BREAKPOINTS (b)
7229 if (b->type != bp_call_dummy &&
7230 b->type != bp_shlib_event &&
7231 b->type != bp_thread_event &&
7232 b->type != bp_overlay_event &&
7233 b->number >= 0)
7235 breaks_to_delete = 1;
7236 break;
7240 /* Ask user only if there are some breakpoints to delete. */
7241 if (!from_tty
7242 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
7244 ALL_BREAKPOINTS_SAFE (b, temp)
7246 if (b->type != bp_call_dummy &&
7247 b->type != bp_shlib_event &&
7248 b->type != bp_thread_event &&
7249 b->type != bp_overlay_event &&
7250 b->number >= 0)
7251 delete_breakpoint (b);
7255 else
7256 map_breakpoint_numbers (arg, delete_breakpoint);
7259 static int
7260 all_locations_are_pending (struct bp_location *loc)
7262 for (; loc; loc = loc->next)
7263 if (!loc->shlib_disabled)
7264 return 0;
7265 return 1;
7268 /* Subroutine of update_breakpoint_locations to simplify it.
7269 Return non-zero if multiple fns in list LOC have the same name.
7270 Null names are ignored. */
7272 static int
7273 ambiguous_names_p (struct bp_location *loc)
7275 struct bp_location *l;
7276 htab_t htab = htab_create_alloc (13, htab_hash_string,
7277 (int (*) (const void *, const void *)) streq,
7278 NULL, xcalloc, xfree);
7280 for (l = loc; l != NULL; l = l->next)
7282 const char **slot;
7283 const char *name = l->function_name;
7285 /* Allow for some names to be NULL, ignore them. */
7286 if (name == NULL)
7287 continue;
7289 slot = (const char **) htab_find_slot (htab, (const void *) name,
7290 INSERT);
7291 /* NOTE: We can assume slot != NULL here because xcalloc never returns
7292 NULL. */
7293 if (*slot != NULL)
7295 htab_delete (htab);
7296 return 1;
7298 *slot = name;
7301 htab_delete (htab);
7302 return 0;
7305 static void
7306 update_breakpoint_locations (struct breakpoint *b,
7307 struct symtabs_and_lines sals)
7309 int i;
7310 char *s;
7311 struct bp_location *existing_locations = b->loc;
7313 /* If there's no new locations, and all existing locations
7314 are pending, don't do anything. This optimizes
7315 the common case where all locations are in the same
7316 shared library, that was unloaded. We'd like to
7317 retain the location, so that when the library
7318 is loaded again, we don't loose the enabled/disabled
7319 status of the individual locations. */
7320 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
7321 return;
7323 b->loc = NULL;
7325 for (i = 0; i < sals.nelts; ++i)
7327 struct bp_location *new_loc =
7328 add_location_to_breakpoint (b, &(sals.sals[i]));
7330 /* Reparse conditions, they might contain references to the
7331 old symtab. */
7332 if (b->cond_string != NULL)
7334 struct gdb_exception e;
7336 s = b->cond_string;
7337 TRY_CATCH (e, RETURN_MASK_ERROR)
7339 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
7342 if (e.reason < 0)
7344 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
7345 b->number, e.message);
7346 new_loc->enabled = 0;
7350 if (b->source_file != NULL)
7351 xfree (b->source_file);
7352 if (sals.sals[i].symtab == NULL)
7353 b->source_file = NULL;
7354 else
7355 b->source_file =
7356 savestring (sals.sals[i].symtab->filename,
7357 strlen (sals.sals[i].symtab->filename));
7359 if (b->line_number == 0)
7360 b->line_number = sals.sals[i].line;
7363 /* Update locations of permanent breakpoints. */
7364 if (b->enable_state == bp_permanent)
7365 make_breakpoint_permanent (b);
7367 /* If possible, carry over 'disable' status from existing breakpoints. */
7369 struct bp_location *e = existing_locations;
7370 /* If there are multiple breakpoints with the same function name,
7371 e.g. for inline functions, comparing function names won't work.
7372 Instead compare pc addresses; this is just a heuristic as things
7373 may have moved, but in practice it gives the correct answer
7374 often enough until a better solution is found. */
7375 int have_ambiguous_names = ambiguous_names_p (b->loc);
7377 for (; e; e = e->next)
7379 if (!e->enabled && e->function_name)
7381 struct bp_location *l = b->loc;
7382 if (have_ambiguous_names)
7384 for (; l; l = l->next)
7385 if (e->address == l->address)
7387 l->enabled = 0;
7388 break;
7391 else
7393 for (; l; l = l->next)
7394 if (l->function_name
7395 && strcmp (e->function_name, l->function_name) == 0)
7397 l->enabled = 0;
7398 break;
7405 update_global_location_list (1);
7409 /* Reset a breakpoint given it's struct breakpoint * BINT.
7410 The value we return ends up being the return value from catch_errors.
7411 Unused in this case. */
7413 static int
7414 breakpoint_re_set_one (void *bint)
7416 /* get past catch_errs */
7417 struct breakpoint *b = (struct breakpoint *) bint;
7418 struct value *mark;
7419 int i;
7420 int not_found = 0;
7421 int *not_found_ptr = &not_found;
7422 struct symtabs_and_lines sals = {};
7423 struct symtabs_and_lines expanded;
7424 char *s;
7425 enum enable_state save_enable;
7426 struct gdb_exception e;
7427 struct cleanup *cleanups;
7429 switch (b->type)
7431 case bp_none:
7432 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
7433 b->number);
7434 return 0;
7435 case bp_breakpoint:
7436 case bp_hardware_breakpoint:
7437 if (b->addr_string == NULL)
7439 /* Anything without a string can't be re-set. */
7440 delete_breakpoint (b);
7441 return 0;
7444 set_language (b->language);
7445 input_radix = b->input_radix;
7446 s = b->addr_string;
7447 TRY_CATCH (e, RETURN_MASK_ERROR)
7449 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
7450 not_found_ptr);
7452 if (e.reason < 0)
7454 int not_found_and_ok = 0;
7455 /* For pending breakpoints, it's expected that parsing
7456 will fail until the right shared library is loaded.
7457 User has already told to create pending breakpoints and
7458 don't need extra messages. If breakpoint is in bp_shlib_disabled
7459 state, then user already saw the message about that breakpoint
7460 being disabled, and don't want to see more errors. */
7461 if (not_found
7462 && (b->condition_not_parsed
7463 || (b->loc && b->loc->shlib_disabled)
7464 || b->enable_state == bp_disabled))
7465 not_found_and_ok = 1;
7467 if (!not_found_and_ok)
7469 /* We surely don't want to warn about the same breakpoint
7470 10 times. One solution, implemented here, is disable
7471 the breakpoint on error. Another solution would be to
7472 have separate 'warning emitted' flag. Since this
7473 happens only when a binary has changed, I don't know
7474 which approach is better. */
7475 b->enable_state = bp_disabled;
7476 throw_exception (e);
7480 if (not_found)
7481 break;
7483 gdb_assert (sals.nelts == 1);
7484 resolve_sal_pc (&sals.sals[0]);
7485 if (b->condition_not_parsed && s && s[0])
7487 char *cond_string = 0;
7488 int thread = -1;
7489 find_condition_and_thread (s, sals.sals[0].pc,
7490 &cond_string, &thread);
7491 if (cond_string)
7492 b->cond_string = cond_string;
7493 b->thread = thread;
7494 b->condition_not_parsed = 0;
7496 expanded = expand_line_sal_maybe (sals.sals[0]);
7497 cleanups = make_cleanup (xfree, sals.sals);
7498 update_breakpoint_locations (b, expanded);
7499 do_cleanups (cleanups);
7500 break;
7502 case bp_watchpoint:
7503 case bp_hardware_watchpoint:
7504 case bp_read_watchpoint:
7505 case bp_access_watchpoint:
7506 /* Watchpoint can be either on expression using entirely global variables,
7507 or it can be on local variables.
7509 Watchpoints of the first kind are never auto-deleted, and even persist
7510 across program restarts. Since they can use variables from shared
7511 libraries, we need to reparse expression as libraries are loaded
7512 and unloaded.
7514 Watchpoints on local variables can also change meaning as result
7515 of solib event. For example, if a watchpoint uses both a local and
7516 a global variables in expression, it's a local watchpoint, but
7517 unloading of a shared library will make the expression invalid.
7518 This is not a very common use case, but we still re-evaluate
7519 expression, to avoid surprises to the user.
7521 Note that for local watchpoints, we re-evaluate it only if
7522 watchpoints frame id is still valid. If it's not, it means
7523 the watchpoint is out of scope and will be deleted soon. In fact,
7524 I'm not sure we'll ever be called in this case.
7526 If a local watchpoint's frame id is still valid, then
7527 b->exp_valid_block is likewise valid, and we can safely use it.
7529 Don't do anything about disabled watchpoints, since they will
7530 be reevaluated again when enabled. */
7531 update_watchpoint (b, 1 /* reparse */);
7532 break;
7533 /* We needn't really do anything to reset these, since the mask
7534 that requests them is unaffected by e.g., new libraries being
7535 loaded. */
7536 case bp_catchpoint:
7537 break;
7539 default:
7540 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
7541 /* fall through */
7542 /* Delete overlay event breakpoints; they will be reset later by
7543 breakpoint_re_set. */
7544 case bp_overlay_event:
7545 delete_breakpoint (b);
7546 break;
7548 /* This breakpoint is special, it's set up when the inferior
7549 starts and we really don't want to touch it. */
7550 case bp_shlib_event:
7552 /* Like bp_shlib_event, this breakpoint type is special.
7553 Once it is set up, we do not want to touch it. */
7554 case bp_thread_event:
7556 /* Keep temporary breakpoints, which can be encountered when we step
7557 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
7558 Otherwise these should have been blown away via the cleanup chain
7559 or by breakpoint_init_inferior when we rerun the executable. */
7560 case bp_until:
7561 case bp_finish:
7562 case bp_watchpoint_scope:
7563 case bp_call_dummy:
7564 case bp_step_resume:
7565 case bp_longjmp:
7566 case bp_longjmp_resume:
7567 break;
7570 return 0;
7573 /* Re-set all breakpoints after symbols have been re-loaded. */
7574 void
7575 breakpoint_re_set (void)
7577 struct breakpoint *b, *temp;
7578 enum language save_language;
7579 int save_input_radix;
7581 save_language = current_language->la_language;
7582 save_input_radix = input_radix;
7583 ALL_BREAKPOINTS_SAFE (b, temp)
7585 /* Format possible error msg */
7586 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
7587 b->number);
7588 struct cleanup *cleanups = make_cleanup (xfree, message);
7589 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
7590 do_cleanups (cleanups);
7592 set_language (save_language);
7593 input_radix = save_input_radix;
7595 create_overlay_event_breakpoint ("_ovly_debug_event");
7598 /* Reset the thread number of this breakpoint:
7600 - If the breakpoint is for all threads, leave it as-is.
7601 - Else, reset it to the current thread for inferior_ptid. */
7602 void
7603 breakpoint_re_set_thread (struct breakpoint *b)
7605 if (b->thread != -1)
7607 if (in_thread_list (inferior_ptid))
7608 b->thread = pid_to_thread_id (inferior_ptid);
7612 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
7613 If from_tty is nonzero, it prints a message to that effect,
7614 which ends with a period (no newline). */
7616 void
7617 set_ignore_count (int bptnum, int count, int from_tty)
7619 struct breakpoint *b;
7621 if (count < 0)
7622 count = 0;
7624 ALL_BREAKPOINTS (b)
7625 if (b->number == bptnum)
7627 b->ignore_count = count;
7628 if (from_tty)
7630 if (count == 0)
7631 printf_filtered (_("Will stop next time breakpoint %d is reached."),
7632 bptnum);
7633 else if (count == 1)
7634 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
7635 bptnum);
7636 else
7637 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
7638 count, bptnum);
7640 breakpoints_changed ();
7641 observer_notify_breakpoint_modified (b->number);
7642 return;
7645 error (_("No breakpoint number %d."), bptnum);
7648 void
7649 make_breakpoint_silent (struct breakpoint *b)
7651 /* Silence the breakpoint. */
7652 b->silent = 1;
7655 /* Command to set ignore-count of breakpoint N to COUNT. */
7657 static void
7658 ignore_command (char *args, int from_tty)
7660 char *p = args;
7661 int num;
7663 if (p == 0)
7664 error_no_arg (_("a breakpoint number"));
7666 num = get_number (&p);
7667 if (num == 0)
7668 error (_("bad breakpoint number: '%s'"), args);
7669 if (*p == 0)
7670 error (_("Second argument (specified ignore-count) is missing."));
7672 set_ignore_count (num,
7673 longest_to_int (value_as_long (parse_and_eval (p))),
7674 from_tty);
7675 if (from_tty)
7676 printf_filtered ("\n");
7679 /* Call FUNCTION on each of the breakpoints
7680 whose numbers are given in ARGS. */
7682 static void
7683 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
7685 char *p = args;
7686 char *p1;
7687 int num;
7688 struct breakpoint *b, *tmp;
7689 int match;
7691 if (p == 0)
7692 error_no_arg (_("one or more breakpoint numbers"));
7694 while (*p)
7696 match = 0;
7697 p1 = p;
7699 num = get_number_or_range (&p1);
7700 if (num == 0)
7702 warning (_("bad breakpoint number at or near '%s'"), p);
7704 else
7706 ALL_BREAKPOINTS_SAFE (b, tmp)
7707 if (b->number == num)
7709 struct breakpoint *related_breakpoint = b->related_breakpoint;
7710 match = 1;
7711 function (b);
7712 if (related_breakpoint)
7713 function (related_breakpoint);
7714 break;
7716 if (match == 0)
7717 printf_unfiltered (_("No breakpoint number %d.\n"), num);
7719 p = p1;
7723 static struct bp_location *
7724 find_location_by_number (char *number)
7726 char *dot = strchr (number, '.');
7727 char *p1;
7728 int bp_num;
7729 int loc_num;
7730 struct breakpoint *b;
7731 struct bp_location *loc;
7733 *dot = '\0';
7735 p1 = number;
7736 bp_num = get_number_or_range (&p1);
7737 if (bp_num == 0)
7738 error (_("Bad breakpoint number '%s'"), number);
7740 ALL_BREAKPOINTS (b)
7741 if (b->number == bp_num)
7743 break;
7746 if (!b || b->number != bp_num)
7747 error (_("Bad breakpoint number '%s'"), number);
7749 p1 = dot+1;
7750 loc_num = get_number_or_range (&p1);
7751 if (loc_num == 0)
7752 error (_("Bad breakpoint location number '%s'"), number);
7754 --loc_num;
7755 loc = b->loc;
7756 for (;loc_num && loc; --loc_num, loc = loc->next)
7758 if (!loc)
7759 error (_("Bad breakpoint location number '%s'"), dot+1);
7761 return loc;
7765 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
7766 If from_tty is nonzero, it prints a message to that effect,
7767 which ends with a period (no newline). */
7769 void
7770 disable_breakpoint (struct breakpoint *bpt)
7772 /* Never disable a watchpoint scope breakpoint; we want to
7773 hit them when we leave scope so we can delete both the
7774 watchpoint and its scope breakpoint at that time. */
7775 if (bpt->type == bp_watchpoint_scope)
7776 return;
7778 /* You can't disable permanent breakpoints. */
7779 if (bpt->enable_state == bp_permanent)
7780 return;
7782 bpt->enable_state = bp_disabled;
7784 update_global_location_list (0);
7786 observer_notify_breakpoint_modified (bpt->number);
7789 static void
7790 disable_command (char *args, int from_tty)
7792 struct breakpoint *bpt;
7793 if (args == 0)
7794 ALL_BREAKPOINTS (bpt)
7795 switch (bpt->type)
7797 case bp_none:
7798 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
7799 bpt->number);
7800 continue;
7801 case bp_breakpoint:
7802 case bp_catchpoint:
7803 case bp_hardware_breakpoint:
7804 case bp_watchpoint:
7805 case bp_hardware_watchpoint:
7806 case bp_read_watchpoint:
7807 case bp_access_watchpoint:
7808 disable_breakpoint (bpt);
7809 default:
7810 continue;
7812 else if (strchr (args, '.'))
7814 struct bp_location *loc = find_location_by_number (args);
7815 if (loc)
7816 loc->enabled = 0;
7817 update_global_location_list (0);
7819 else
7820 map_breakpoint_numbers (args, disable_breakpoint);
7823 static void
7824 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
7826 int target_resources_ok, other_type_used;
7827 struct value *mark;
7829 if (bpt->type == bp_hardware_breakpoint)
7831 int i;
7832 i = hw_breakpoint_used_count ();
7833 target_resources_ok =
7834 TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint,
7835 i + 1, 0);
7836 if (target_resources_ok == 0)
7837 error (_("No hardware breakpoint support in the target."));
7838 else if (target_resources_ok < 0)
7839 error (_("Hardware breakpoints used exceeds limit."));
7842 if (bpt->type == bp_watchpoint ||
7843 bpt->type == bp_hardware_watchpoint ||
7844 bpt->type == bp_read_watchpoint ||
7845 bpt->type == bp_access_watchpoint)
7847 struct gdb_exception e;
7849 TRY_CATCH (e, RETURN_MASK_ALL)
7851 update_watchpoint (bpt, 1 /* reparse */);
7853 if (e.reason < 0)
7855 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
7856 bpt->number);
7857 return;
7861 if (bpt->enable_state != bp_permanent)
7862 bpt->enable_state = bp_enabled;
7863 bpt->disposition = disposition;
7864 update_global_location_list (1);
7865 breakpoints_changed ();
7867 observer_notify_breakpoint_modified (bpt->number);
7871 void
7872 enable_breakpoint (struct breakpoint *bpt)
7874 do_enable_breakpoint (bpt, bpt->disposition);
7877 /* The enable command enables the specified breakpoints (or all defined
7878 breakpoints) so they once again become (or continue to be) effective
7879 in stopping the inferior. */
7881 static void
7882 enable_command (char *args, int from_tty)
7884 struct breakpoint *bpt;
7885 if (args == 0)
7886 ALL_BREAKPOINTS (bpt)
7887 switch (bpt->type)
7889 case bp_none:
7890 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
7891 bpt->number);
7892 continue;
7893 case bp_breakpoint:
7894 case bp_catchpoint:
7895 case bp_hardware_breakpoint:
7896 case bp_watchpoint:
7897 case bp_hardware_watchpoint:
7898 case bp_read_watchpoint:
7899 case bp_access_watchpoint:
7900 enable_breakpoint (bpt);
7901 default:
7902 continue;
7904 else if (strchr (args, '.'))
7906 struct bp_location *loc = find_location_by_number (args);
7907 if (loc)
7908 loc->enabled = 1;
7909 update_global_location_list (1);
7911 else
7912 map_breakpoint_numbers (args, enable_breakpoint);
7915 static void
7916 enable_once_breakpoint (struct breakpoint *bpt)
7918 do_enable_breakpoint (bpt, disp_disable);
7921 static void
7922 enable_once_command (char *args, int from_tty)
7924 map_breakpoint_numbers (args, enable_once_breakpoint);
7927 static void
7928 enable_delete_breakpoint (struct breakpoint *bpt)
7930 do_enable_breakpoint (bpt, disp_del);
7933 static void
7934 enable_delete_command (char *args, int from_tty)
7936 map_breakpoint_numbers (args, enable_delete_breakpoint);
7939 static void
7940 set_breakpoint_cmd (char *args, int from_tty)
7944 static void
7945 show_breakpoint_cmd (char *args, int from_tty)
7949 /* Use default_breakpoint_'s, or nothing if they aren't valid. */
7951 struct symtabs_and_lines
7952 decode_line_spec_1 (char *string, int funfirstline)
7954 struct symtabs_and_lines sals;
7955 if (string == 0)
7956 error (_("Empty line specification."));
7957 if (default_breakpoint_valid)
7958 sals = decode_line_1 (&string, funfirstline,
7959 default_breakpoint_symtab,
7960 default_breakpoint_line,
7961 (char ***) NULL, NULL);
7962 else
7963 sals = decode_line_1 (&string, funfirstline,
7964 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
7965 if (*string)
7966 error (_("Junk at end of line specification: %s"), string);
7967 return sals;
7970 /* Create and insert a raw software breakpoint at PC. Return an
7971 identifier, which should be used to remove the breakpoint later.
7972 In general, places which call this should be using something on the
7973 breakpoint chain instead; this function should be eliminated
7974 someday. */
7976 void *
7977 deprecated_insert_raw_breakpoint (CORE_ADDR pc)
7979 struct bp_target_info *bp_tgt;
7981 bp_tgt = xmalloc (sizeof (struct bp_target_info));
7982 memset (bp_tgt, 0, sizeof (struct bp_target_info));
7984 bp_tgt->placed_address = pc;
7985 if (target_insert_breakpoint (bp_tgt) != 0)
7987 /* Could not insert the breakpoint. */
7988 xfree (bp_tgt);
7989 return NULL;
7992 return bp_tgt;
7995 /* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
7998 deprecated_remove_raw_breakpoint (void *bp)
8000 struct bp_target_info *bp_tgt = bp;
8001 int ret;
8003 ret = target_remove_breakpoint (bp_tgt);
8004 xfree (bp_tgt);
8006 return ret;
8009 /* One (or perhaps two) breakpoints used for software single stepping. */
8011 static void *single_step_breakpoints[2];
8013 /* Create and insert a breakpoint for software single step. */
8015 void
8016 insert_single_step_breakpoint (CORE_ADDR next_pc)
8018 void **bpt_p;
8020 if (single_step_breakpoints[0] == NULL)
8021 bpt_p = &single_step_breakpoints[0];
8022 else
8024 gdb_assert (single_step_breakpoints[1] == NULL);
8025 bpt_p = &single_step_breakpoints[1];
8028 /* NOTE drow/2006-04-11: A future improvement to this function would be
8029 to only create the breakpoints once, and actually put them on the
8030 breakpoint chain. That would let us use set_raw_breakpoint. We could
8031 adjust the addresses each time they were needed. Doing this requires
8032 corresponding changes elsewhere where single step breakpoints are
8033 handled, however. So, for now, we use this. */
8035 *bpt_p = deprecated_insert_raw_breakpoint (next_pc);
8036 if (*bpt_p == NULL)
8037 error (_("Could not insert single-step breakpoint at 0x%s"),
8038 paddr_nz (next_pc));
8041 /* Remove and delete any breakpoints used for software single step. */
8043 void
8044 remove_single_step_breakpoints (void)
8046 gdb_assert (single_step_breakpoints[0] != NULL);
8048 /* See insert_single_step_breakpoint for more about this deprecated
8049 call. */
8050 deprecated_remove_raw_breakpoint (single_step_breakpoints[0]);
8051 single_step_breakpoints[0] = NULL;
8053 if (single_step_breakpoints[1] != NULL)
8055 deprecated_remove_raw_breakpoint (single_step_breakpoints[1]);
8056 single_step_breakpoints[1] = NULL;
8060 /* Check whether a software single-step breakpoint is inserted at PC. */
8062 static int
8063 single_step_breakpoint_inserted_here_p (CORE_ADDR pc)
8065 int i;
8067 for (i = 0; i < 2; i++)
8069 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
8070 if (bp_tgt && bp_tgt->placed_address == pc)
8071 return 1;
8074 return 0;
8078 /* This help string is used for the break, hbreak, tbreak and thbreak commands.
8079 It is defined as a macro to prevent duplication.
8080 COMMAND should be a string constant containing the name of the command. */
8081 #define BREAK_ARGS_HELP(command) \
8082 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
8083 LOCATION may be a line number, function name, or \"*\" and an address.\n\
8084 If a line number is specified, break at start of code for that line.\n\
8085 If a function is specified, break at start of code for that function.\n\
8086 If an address is specified, break at that exact address.\n\
8087 With no LOCATION, uses current execution address of selected stack frame.\n\
8088 This is useful for breaking on return to a stack frame.\n\
8090 THREADNUM is the number from \"info threads\".\n\
8091 CONDITION is a boolean expression.\n\
8093 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
8095 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
8097 /* List of subcommands for "catch". */
8098 static struct cmd_list_element *catch_cmdlist;
8100 /* List of subcommands for "tcatch". */
8101 static struct cmd_list_element *tcatch_cmdlist;
8103 /* Like add_cmd, but add the command to both the "catch" and "tcatch"
8104 lists, and pass some additional user data to the command function. */
8105 static void
8106 add_catch_command (char *name, char *docstring,
8107 void (*sfunc) (char *args, int from_tty,
8108 struct cmd_list_element *command),
8109 void *user_data_catch,
8110 void *user_data_tcatch)
8112 struct cmd_list_element *command;
8114 command = add_cmd (name, class_breakpoint, NULL, docstring,
8115 &catch_cmdlist);
8116 set_cmd_sfunc (command, sfunc);
8117 set_cmd_context (command, user_data_catch);
8119 command = add_cmd (name, class_breakpoint, NULL, docstring,
8120 &tcatch_cmdlist);
8121 set_cmd_sfunc (command, sfunc);
8122 set_cmd_context (command, user_data_tcatch);
8125 void
8126 _initialize_breakpoint (void)
8128 static struct cmd_list_element *breakpoint_set_cmdlist;
8129 static struct cmd_list_element *breakpoint_show_cmdlist;
8130 struct cmd_list_element *c;
8132 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
8134 breakpoint_chain = 0;
8135 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
8136 before a breakpoint is set. */
8137 breakpoint_count = 0;
8139 add_com ("ignore", class_breakpoint, ignore_command, _("\
8140 Set ignore-count of breakpoint number N to COUNT.\n\
8141 Usage is `ignore N COUNT'."));
8142 if (xdb_commands)
8143 add_com_alias ("bc", "ignore", class_breakpoint, 1);
8145 add_com ("commands", class_breakpoint, commands_command, _("\
8146 Set commands to be executed when a breakpoint is hit.\n\
8147 Give breakpoint number as argument after \"commands\".\n\
8148 With no argument, the targeted breakpoint is the last one set.\n\
8149 The commands themselves follow starting on the next line.\n\
8150 Type a line containing \"end\" to indicate the end of them.\n\
8151 Give \"silent\" as the first line to make the breakpoint silent;\n\
8152 then no output is printed when it is hit, except what the commands print."));
8154 add_com ("condition", class_breakpoint, condition_command, _("\
8155 Specify breakpoint number N to break only if COND is true.\n\
8156 Usage is `condition N COND', where N is an integer and COND is an\n\
8157 expression to be evaluated whenever breakpoint N is reached."));
8159 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
8160 Set a temporary breakpoint.\n\
8161 Like \"break\" except the breakpoint is only temporary,\n\
8162 so it will be deleted when hit. Equivalent to \"break\" followed\n\
8163 by using \"enable delete\" on the breakpoint number.\n\
8165 BREAK_ARGS_HELP ("tbreak")));
8166 set_cmd_completer (c, location_completer);
8168 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
8169 Set a hardware assisted breakpoint.\n\
8170 Like \"break\" except the breakpoint requires hardware support,\n\
8171 some target hardware may not have this support.\n\
8173 BREAK_ARGS_HELP ("hbreak")));
8174 set_cmd_completer (c, location_completer);
8176 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
8177 Set a temporary hardware assisted breakpoint.\n\
8178 Like \"hbreak\" except the breakpoint is only temporary,\n\
8179 so it will be deleted when hit.\n\
8181 BREAK_ARGS_HELP ("thbreak")));
8182 set_cmd_completer (c, location_completer);
8184 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
8185 Enable some breakpoints.\n\
8186 Give breakpoint numbers (separated by spaces) as arguments.\n\
8187 With no subcommand, breakpoints are enabled until you command otherwise.\n\
8188 This is used to cancel the effect of the \"disable\" command.\n\
8189 With a subcommand you can enable temporarily."),
8190 &enablelist, "enable ", 1, &cmdlist);
8191 if (xdb_commands)
8192 add_com ("ab", class_breakpoint, enable_command, _("\
8193 Enable some breakpoints.\n\
8194 Give breakpoint numbers (separated by spaces) as arguments.\n\
8195 With no subcommand, breakpoints are enabled until you command otherwise.\n\
8196 This is used to cancel the effect of the \"disable\" command.\n\
8197 With a subcommand you can enable temporarily."));
8199 add_com_alias ("en", "enable", class_breakpoint, 1);
8201 add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
8202 Enable some breakpoints.\n\
8203 Give breakpoint numbers (separated by spaces) as arguments.\n\
8204 This is used to cancel the effect of the \"disable\" command.\n\
8205 May be abbreviated to simply \"enable\".\n"),
8206 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
8208 add_cmd ("once", no_class, enable_once_command, _("\
8209 Enable breakpoints for one hit. Give breakpoint numbers.\n\
8210 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
8211 &enablebreaklist);
8213 add_cmd ("delete", no_class, enable_delete_command, _("\
8214 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8215 If a breakpoint is hit while enabled in this fashion, it is deleted."),
8216 &enablebreaklist);
8218 add_cmd ("delete", no_class, enable_delete_command, _("\
8219 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8220 If a breakpoint is hit while enabled in this fashion, it is deleted."),
8221 &enablelist);
8223 add_cmd ("once", no_class, enable_once_command, _("\
8224 Enable breakpoints for one hit. Give breakpoint numbers.\n\
8225 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
8226 &enablelist);
8228 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
8229 Disable some breakpoints.\n\
8230 Arguments are breakpoint numbers with spaces in between.\n\
8231 To disable all breakpoints, give no argument.\n\
8232 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
8233 &disablelist, "disable ", 1, &cmdlist);
8234 add_com_alias ("dis", "disable", class_breakpoint, 1);
8235 add_com_alias ("disa", "disable", class_breakpoint, 1);
8236 if (xdb_commands)
8237 add_com ("sb", class_breakpoint, disable_command, _("\
8238 Disable some breakpoints.\n\
8239 Arguments are breakpoint numbers with spaces in between.\n\
8240 To disable all breakpoints, give no argument.\n\
8241 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
8243 add_cmd ("breakpoints", class_alias, disable_command, _("\
8244 Disable some breakpoints.\n\
8245 Arguments are breakpoint numbers with spaces in between.\n\
8246 To disable all breakpoints, give no argument.\n\
8247 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
8248 This command may be abbreviated \"disable\"."),
8249 &disablelist);
8251 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
8252 Delete some breakpoints or auto-display expressions.\n\
8253 Arguments are breakpoint numbers with spaces in between.\n\
8254 To delete all breakpoints, give no argument.\n\
8256 Also a prefix command for deletion of other GDB objects.\n\
8257 The \"unset\" command is also an alias for \"delete\"."),
8258 &deletelist, "delete ", 1, &cmdlist);
8259 add_com_alias ("d", "delete", class_breakpoint, 1);
8260 add_com_alias ("del", "delete", class_breakpoint, 1);
8261 if (xdb_commands)
8262 add_com ("db", class_breakpoint, delete_command, _("\
8263 Delete some breakpoints.\n\
8264 Arguments are breakpoint numbers with spaces in between.\n\
8265 To delete all breakpoints, give no argument.\n"));
8267 add_cmd ("breakpoints", class_alias, delete_command, _("\
8268 Delete some breakpoints or auto-display expressions.\n\
8269 Arguments are breakpoint numbers with spaces in between.\n\
8270 To delete all breakpoints, give no argument.\n\
8271 This command may be abbreviated \"delete\"."),
8272 &deletelist);
8274 add_com ("clear", class_breakpoint, clear_command, _("\
8275 Clear breakpoint at specified line or function.\n\
8276 Argument may be line number, function name, or \"*\" and an address.\n\
8277 If line number is specified, all breakpoints in that line are cleared.\n\
8278 If function is specified, breakpoints at beginning of function are cleared.\n\
8279 If an address is specified, breakpoints at that address are cleared.\n\
8281 With no argument, clears all breakpoints in the line that the selected frame\n\
8282 is executing in.\n\
8284 See also the \"delete\" command which clears breakpoints by number."));
8286 c = add_com ("break", class_breakpoint, break_command, _("\
8287 Set breakpoint at specified line or function.\n"
8288 BREAK_ARGS_HELP ("break")));
8289 set_cmd_completer (c, location_completer);
8291 add_com_alias ("b", "break", class_run, 1);
8292 add_com_alias ("br", "break", class_run, 1);
8293 add_com_alias ("bre", "break", class_run, 1);
8294 add_com_alias ("brea", "break", class_run, 1);
8296 if (xdb_commands)
8298 add_com_alias ("ba", "break", class_breakpoint, 1);
8299 add_com_alias ("bu", "ubreak", class_breakpoint, 1);
8302 if (dbx_commands)
8304 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
8305 Break in function/address or break at a line in the current file."),
8306 &stoplist, "stop ", 1, &cmdlist);
8307 add_cmd ("in", class_breakpoint, stopin_command,
8308 _("Break in function or address."), &stoplist);
8309 add_cmd ("at", class_breakpoint, stopat_command,
8310 _("Break at a line in the current file."), &stoplist);
8311 add_com ("status", class_info, breakpoints_info, _("\
8312 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
8313 The \"Type\" column indicates one of:\n\
8314 \tbreakpoint - normal breakpoint\n\
8315 \twatchpoint - watchpoint\n\
8316 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8317 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8318 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
8319 address and file/line number respectively.\n\
8321 Convenience variable \"$_\" and default examine address for \"x\"\n\
8322 are set to the address of the last breakpoint listed unless the command\n\
8323 is prefixed with \"server \".\n\n\
8324 Convenience variable \"$bpnum\" contains the number of the last\n\
8325 breakpoint set."));
8328 add_info ("breakpoints", breakpoints_info, _("\
8329 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
8330 The \"Type\" column indicates one of:\n\
8331 \tbreakpoint - normal breakpoint\n\
8332 \twatchpoint - watchpoint\n\
8333 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8334 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8335 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
8336 address and file/line number respectively.\n\
8338 Convenience variable \"$_\" and default examine address for \"x\"\n\
8339 are set to the address of the last breakpoint listed unless the command\n\
8340 is prefixed with \"server \".\n\n\
8341 Convenience variable \"$bpnum\" contains the number of the last\n\
8342 breakpoint set."));
8344 if (xdb_commands)
8345 add_com ("lb", class_breakpoint, breakpoints_info, _("\
8346 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
8347 The \"Type\" column indicates one of:\n\
8348 \tbreakpoint - normal breakpoint\n\
8349 \twatchpoint - watchpoint\n\
8350 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8351 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8352 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
8353 address and file/line number respectively.\n\
8355 Convenience variable \"$_\" and default examine address for \"x\"\n\
8356 are set to the address of the last breakpoint listed unless the command\n\
8357 is prefixed with \"server \".\n\n\
8358 Convenience variable \"$bpnum\" contains the number of the last\n\
8359 breakpoint set."));
8361 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
8362 Status of all breakpoints, or breakpoint number NUMBER.\n\
8363 The \"Type\" column indicates one of:\n\
8364 \tbreakpoint - normal breakpoint\n\
8365 \twatchpoint - watchpoint\n\
8366 \tlongjmp - internal breakpoint used to step through longjmp()\n\
8367 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
8368 \tuntil - internal breakpoint used by the \"until\" command\n\
8369 \tfinish - internal breakpoint used by the \"finish\" command\n\
8370 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8371 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8372 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
8373 address and file/line number respectively.\n\
8375 Convenience variable \"$_\" and default examine address for \"x\"\n\
8376 are set to the address of the last breakpoint listed unless the command\n\
8377 is prefixed with \"server \".\n\n\
8378 Convenience variable \"$bpnum\" contains the number of the last\n\
8379 breakpoint set."),
8380 &maintenanceinfolist);
8382 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
8383 Set catchpoints to catch events."),
8384 &catch_cmdlist, "catch ",
8385 0/*allow-unknown*/, &cmdlist);
8387 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
8388 Set temporary catchpoints to catch events."),
8389 &tcatch_cmdlist, "tcatch ",
8390 0/*allow-unknown*/, &cmdlist);
8392 /* Add catch and tcatch sub-commands. */
8393 add_catch_command ("catch", _("\
8394 Catch an exception, when caught.\n\
8395 With an argument, catch only exceptions with the given name."),
8396 catch_catch_command,
8397 CATCH_PERMANENT,
8398 CATCH_TEMPORARY);
8399 add_catch_command ("throw", _("\
8400 Catch an exception, when thrown.\n\
8401 With an argument, catch only exceptions with the given name."),
8402 catch_throw_command,
8403 CATCH_PERMANENT,
8404 CATCH_TEMPORARY);
8405 add_catch_command ("fork", _("Catch calls to fork."),
8406 catch_fork_command_1,
8407 (void *) (uintptr_t) catch_fork_permanent,
8408 (void *) (uintptr_t) catch_fork_temporary);
8409 add_catch_command ("vfork", _("Catch calls to vfork."),
8410 catch_fork_command_1,
8411 (void *) (uintptr_t) catch_vfork_permanent,
8412 (void *) (uintptr_t) catch_vfork_temporary);
8413 add_catch_command ("exec", _("Catch calls to exec."),
8414 catch_exec_command_1,
8415 CATCH_PERMANENT,
8416 CATCH_TEMPORARY);
8417 add_catch_command ("exception", _("\
8418 Catch Ada exceptions, when raised.\n\
8419 With an argument, catch only exceptions with the given name."),
8420 catch_ada_exception_command,
8421 CATCH_PERMANENT,
8422 CATCH_TEMPORARY);
8423 add_catch_command ("assert", _("\
8424 Catch failed Ada assertions, when raised.\n\
8425 With an argument, catch only exceptions with the given name."),
8426 catch_assert_command,
8427 CATCH_PERMANENT,
8428 CATCH_TEMPORARY);
8430 c = add_com ("watch", class_breakpoint, watch_command, _("\
8431 Set a watchpoint for an expression.\n\
8432 A watchpoint stops execution of your program whenever the value of\n\
8433 an expression changes."));
8434 set_cmd_completer (c, expression_completer);
8436 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
8437 Set a read watchpoint for an expression.\n\
8438 A watchpoint stops execution of your program whenever the value of\n\
8439 an expression is read."));
8440 set_cmd_completer (c, expression_completer);
8442 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
8443 Set a watchpoint for an expression.\n\
8444 A watchpoint stops execution of your program whenever the value of\n\
8445 an expression is either read or written."));
8446 set_cmd_completer (c, expression_completer);
8448 add_info ("watchpoints", breakpoints_info,
8449 _("Synonym for ``info breakpoints''."));
8452 /* XXX: cagney/2005-02-23: This should be a boolean, and should
8453 respond to changes - contrary to the description. */
8454 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
8455 &can_use_hw_watchpoints, _("\
8456 Set debugger's willingness to use watchpoint hardware."), _("\
8457 Show debugger's willingness to use watchpoint hardware."), _("\
8458 If zero, gdb will not use hardware for new watchpoints, even if\n\
8459 such is available. (However, any hardware watchpoints that were\n\
8460 created before setting this to nonzero, will continue to use watchpoint\n\
8461 hardware.)"),
8462 NULL,
8463 show_can_use_hw_watchpoints,
8464 &setlist, &showlist);
8466 can_use_hw_watchpoints = 1;
8468 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
8469 Breakpoint specific settings\n\
8470 Configure various breakpoint-specific variables such as\n\
8471 pending breakpoint behavior"),
8472 &breakpoint_set_cmdlist, "set breakpoint ",
8473 0/*allow-unknown*/, &setlist);
8474 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
8475 Breakpoint specific settings\n\
8476 Configure various breakpoint-specific variables such as\n\
8477 pending breakpoint behavior"),
8478 &breakpoint_show_cmdlist, "show breakpoint ",
8479 0/*allow-unknown*/, &showlist);
8481 add_setshow_auto_boolean_cmd ("pending", no_class,
8482 &pending_break_support, _("\
8483 Set debugger's behavior regarding pending breakpoints."), _("\
8484 Show debugger's behavior regarding pending breakpoints."), _("\
8485 If on, an unrecognized breakpoint location will cause gdb to create a\n\
8486 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
8487 an error. If auto, an unrecognized breakpoint location results in a\n\
8488 user-query to see if a pending breakpoint should be created."),
8489 NULL,
8490 show_pending_break_support,
8491 &breakpoint_set_cmdlist,
8492 &breakpoint_show_cmdlist);
8494 pending_break_support = AUTO_BOOLEAN_AUTO;
8496 add_setshow_boolean_cmd ("auto-hw", no_class,
8497 &automatic_hardware_breakpoints, _("\
8498 Set automatic usage of hardware breakpoints."), _("\
8499 Show automatic usage of hardware breakpoints."), _("\
8500 If set, the debugger will automatically use hardware breakpoints for\n\
8501 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
8502 a warning will be emitted for such breakpoints."),
8503 NULL,
8504 show_automatic_hardware_breakpoints,
8505 &breakpoint_set_cmdlist,
8506 &breakpoint_show_cmdlist);
8508 add_setshow_enum_cmd ("always-inserted", class_support,
8509 always_inserted_enums, &always_inserted_mode, _("\
8510 Set mode for inserting breakpoints."), _("\
8511 Show mode for inserting breakpoints."), _("\
8512 When this mode is off, breakpoints are inserted in inferior when it is\n\
8513 resumed, and removed when execution stops. When this mode is on,\n\
8514 breakpoints are inserted immediately and removed only when the user\n\
8515 deletes the breakpoint. When this mode is auto (which is the default),\n\
8516 the behaviour depends on the non-stop setting (see help set non-stop).\n\
8517 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
8518 behaves as if always-inserted mode is on; if gdb is controlling the\n\
8519 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
8520 NULL,
8521 &show_always_inserted_mode,
8522 &breakpoint_set_cmdlist,
8523 &breakpoint_show_cmdlist);
8525 automatic_hardware_breakpoints = 1;
8527 observer_attach_about_to_proceed (breakpoint_about_to_proceed);