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