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