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