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