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