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