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