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