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