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