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