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