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