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