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