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