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