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