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