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