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