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