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