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