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