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