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