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