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