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