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