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