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