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