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