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