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