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