import strstr and strerror modules
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
... / ...
CommitLineData
1/* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "arch-utils.h"
22#include <ctype.h>
23#include "hashtab.h"
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
27#include "tracepoint.h"
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
38#include "gdb_string.h"
39#include "gdb-demangle.h"
40#include "filenames.h"
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
44#include "source.h"
45#include "linespec.h"
46#include "completer.h"
47#include "gdb.h"
48#include "ui-out.h"
49#include "cli/cli-script.h"
50#include "gdb_assert.h"
51#include "block.h"
52#include "solib.h"
53#include "solist.h"
54#include "observer.h"
55#include "exceptions.h"
56#include "memattr.h"
57#include "ada-lang.h"
58#include "top.h"
59#include "valprint.h"
60#include "jit.h"
61#include "xml-syscall.h"
62#include "parser-defs.h"
63#include "gdb_regex.h"
64#include "probe.h"
65#include "cli/cli-utils.h"
66#include "continuations.h"
67#include "stack.h"
68#include "skip.h"
69#include "gdb_regex.h"
70#include "ax-gdb.h"
71#include "dummy-frame.h"
72
73#include "format.h"
74
75/* readline include files */
76#include "readline/readline.h"
77#include "readline/history.h"
78
79/* readline defines this. */
80#undef savestring
81
82#include "mi/mi-common.h"
83#include "python/python.h"
84
85/* Enums for exception-handling support. */
86enum exception_event_kind
87{
88 EX_EVENT_THROW,
89 EX_EVENT_RETHROW,
90 EX_EVENT_CATCH
91};
92
93/* Prototypes for local functions. */
94
95static void enable_delete_command (char *, int);
96
97static void enable_once_command (char *, int);
98
99static void enable_count_command (char *, int);
100
101static void disable_command (char *, int);
102
103static void enable_command (char *, int);
104
105static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
106 void *),
107 void *);
108
109static void ignore_command (char *, int);
110
111static int breakpoint_re_set_one (void *);
112
113static void breakpoint_re_set_default (struct breakpoint *);
114
115static void create_sals_from_address_default (char **,
116 struct linespec_result *,
117 enum bptype, char *,
118 char **);
119
120static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
122 char *, char *, enum bptype,
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
126 int, int, int, unsigned);
127
128static void decode_linespec_default (struct breakpoint *, char **,
129 struct symtabs_and_lines *);
130
131static void clear_command (char *, int);
132
133static void catch_command (char *, int);
134
135static int can_use_hardware_watchpoint (struct value *);
136
137static void break_command_1 (char *, int, int);
138
139static void mention (struct breakpoint *);
140
141static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
142 enum bptype,
143 const struct breakpoint_ops *);
144static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
145 const struct symtab_and_line *);
146
147/* This function is used in gdbtk sources and thus can not be made
148 static. */
149struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
150 struct symtab_and_line,
151 enum bptype,
152 const struct breakpoint_ops *);
153
154static struct breakpoint *
155 momentary_breakpoint_from_master (struct breakpoint *orig,
156 enum bptype type,
157 const struct breakpoint_ops *ops);
158
159static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
160
161static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
162 CORE_ADDR bpaddr,
163 enum bptype bptype);
164
165static void describe_other_breakpoints (struct gdbarch *,
166 struct program_space *, CORE_ADDR,
167 struct obj_section *, int);
168
169static int breakpoint_address_match (struct address_space *aspace1,
170 CORE_ADDR addr1,
171 struct address_space *aspace2,
172 CORE_ADDR addr2);
173
174static int watchpoint_locations_match (struct bp_location *loc1,
175 struct bp_location *loc2);
176
177static int breakpoint_location_address_match (struct bp_location *bl,
178 struct address_space *aspace,
179 CORE_ADDR addr);
180
181static void breakpoints_info (char *, int);
182
183static void watchpoints_info (char *, int);
184
185static int breakpoint_1 (char *, int,
186 int (*) (const struct breakpoint *));
187
188static int breakpoint_cond_eval (void *);
189
190static void cleanup_executing_breakpoints (void *);
191
192static void commands_command (char *, int);
193
194static void condition_command (char *, int);
195
196typedef enum
197 {
198 mark_inserted,
199 mark_uninserted
200 }
201insertion_state_t;
202
203static int remove_breakpoint (struct bp_location *, insertion_state_t);
204static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
205
206static enum print_stop_action print_bp_stop_message (bpstat bs);
207
208static int watchpoint_check (void *);
209
210static void maintenance_info_breakpoints (char *, int);
211
212static int hw_breakpoint_used_count (void);
213
214static int hw_watchpoint_use_count (struct breakpoint *);
215
216static int hw_watchpoint_used_count_others (struct breakpoint *except,
217 enum bptype type,
218 int *other_type_used);
219
220static void hbreak_command (char *, int);
221
222static void thbreak_command (char *, int);
223
224static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
225 int count);
226
227static void stop_command (char *arg, int from_tty);
228
229static void stopin_command (char *arg, int from_tty);
230
231static void stopat_command (char *arg, int from_tty);
232
233static void tcatch_command (char *arg, int from_tty);
234
235static void detach_single_step_breakpoints (void);
236
237static int single_step_breakpoint_inserted_here_p (struct address_space *,
238 CORE_ADDR pc);
239
240static void free_bp_location (struct bp_location *loc);
241static void incref_bp_location (struct bp_location *loc);
242static void decref_bp_location (struct bp_location **loc);
243
244static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
245
246static void update_global_location_list (int);
247
248static void update_global_location_list_nothrow (int);
249
250static int is_hardware_watchpoint (const struct breakpoint *bpt);
251
252static void insert_breakpoint_locations (void);
253
254static int syscall_catchpoint_p (struct breakpoint *b);
255
256static void tracepoints_info (char *, int);
257
258static void delete_trace_command (char *, int);
259
260static void enable_trace_command (char *, int);
261
262static void disable_trace_command (char *, int);
263
264static void trace_pass_command (char *, int);
265
266static void set_tracepoint_count (int num);
267
268static int is_masked_watchpoint (const struct breakpoint *b);
269
270static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
271
272/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
273 otherwise. */
274
275static int strace_marker_p (struct breakpoint *b);
276
277/* The abstract base class all breakpoint_ops structures inherit
278 from. */
279struct breakpoint_ops base_breakpoint_ops;
280
281/* The breakpoint_ops structure to be inherited by all breakpoint_ops
282 that are implemented on top of software or hardware breakpoints
283 (user breakpoints, internal and momentary breakpoints, etc.). */
284static struct breakpoint_ops bkpt_base_breakpoint_ops;
285
286/* Internal breakpoints class type. */
287static struct breakpoint_ops internal_breakpoint_ops;
288
289/* Momentary breakpoints class type. */
290static struct breakpoint_ops momentary_breakpoint_ops;
291
292/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
293static struct breakpoint_ops longjmp_breakpoint_ops;
294
295/* The breakpoint_ops structure to be used in regular user created
296 breakpoints. */
297struct breakpoint_ops bkpt_breakpoint_ops;
298
299/* Breakpoints set on probes. */
300static struct breakpoint_ops bkpt_probe_breakpoint_ops;
301
302/* Dynamic printf class type. */
303struct breakpoint_ops dprintf_breakpoint_ops;
304
305/* The style in which to perform a dynamic printf. This is a user
306 option because different output options have different tradeoffs;
307 if GDB does the printing, there is better error handling if there
308 is a problem with any of the arguments, but using an inferior
309 function lets you have special-purpose printers and sending of
310 output to the same place as compiled-in print functions. */
311
312static const char dprintf_style_gdb[] = "gdb";
313static const char dprintf_style_call[] = "call";
314static const char dprintf_style_agent[] = "agent";
315static const char *const dprintf_style_enums[] = {
316 dprintf_style_gdb,
317 dprintf_style_call,
318 dprintf_style_agent,
319 NULL
320};
321static const char *dprintf_style = dprintf_style_gdb;
322
323/* The function to use for dynamic printf if the preferred style is to
324 call into the inferior. The value is simply a string that is
325 copied into the command, so it can be anything that GDB can
326 evaluate to a callable address, not necessarily a function name. */
327
328static char *dprintf_function = "";
329
330/* The channel to use for dynamic printf if the preferred style is to
331 call into the inferior; if a nonempty string, it will be passed to
332 the call as the first argument, with the format string as the
333 second. As with the dprintf function, this can be anything that
334 GDB knows how to evaluate, so in addition to common choices like
335 "stderr", this could be an app-specific expression like
336 "mystreams[curlogger]". */
337
338static char *dprintf_channel = "";
339
340/* True if dprintf commands should continue to operate even if GDB
341 has disconnected. */
342static int disconnected_dprintf = 1;
343
344/* A reference-counted struct command_line. This lets multiple
345 breakpoints share a single command list. */
346struct counted_command_line
347{
348 /* The reference count. */
349 int refc;
350
351 /* The command list. */
352 struct command_line *commands;
353};
354
355struct command_line *
356breakpoint_commands (struct breakpoint *b)
357{
358 return b->commands ? b->commands->commands : NULL;
359}
360
361/* Flag indicating that a command has proceeded the inferior past the
362 current breakpoint. */
363
364static int breakpoint_proceeded;
365
366const char *
367bpdisp_text (enum bpdisp disp)
368{
369 /* NOTE: the following values are a part of MI protocol and
370 represent values of 'disp' field returned when inferior stops at
371 a breakpoint. */
372 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
373
374 return bpdisps[(int) disp];
375}
376
377/* Prototypes for exported functions. */
378/* If FALSE, gdb will not use hardware support for watchpoints, even
379 if such is available. */
380static int can_use_hw_watchpoints;
381
382static void
383show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
384 struct cmd_list_element *c,
385 const char *value)
386{
387 fprintf_filtered (file,
388 _("Debugger's willingness to use "
389 "watchpoint hardware is %s.\n"),
390 value);
391}
392
393/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
394 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
395 for unrecognized breakpoint locations.
396 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
397static enum auto_boolean pending_break_support;
398static void
399show_pending_break_support (struct ui_file *file, int from_tty,
400 struct cmd_list_element *c,
401 const char *value)
402{
403 fprintf_filtered (file,
404 _("Debugger's behavior regarding "
405 "pending breakpoints is %s.\n"),
406 value);
407}
408
409/* If 1, gdb will automatically use hardware breakpoints for breakpoints
410 set with "break" but falling in read-only memory.
411 If 0, gdb will warn about such breakpoints, but won't automatically
412 use hardware breakpoints. */
413static int automatic_hardware_breakpoints;
414static void
415show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
416 struct cmd_list_element *c,
417 const char *value)
418{
419 fprintf_filtered (file,
420 _("Automatic usage of hardware breakpoints is %s.\n"),
421 value);
422}
423
424/* If on, gdb will keep breakpoints inserted even as inferior is
425 stopped, and immediately insert any new breakpoints. If off, gdb
426 will insert breakpoints into inferior only when resuming it, and
427 will remove breakpoints upon stop. If auto, GDB will behave as ON
428 if in non-stop mode, and as OFF if all-stop mode.*/
429
430static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
431
432static void
433show_always_inserted_mode (struct ui_file *file, int from_tty,
434 struct cmd_list_element *c, const char *value)
435{
436 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
437 fprintf_filtered (file,
438 _("Always inserted breakpoint "
439 "mode is %s (currently %s).\n"),
440 value,
441 breakpoints_always_inserted_mode () ? "on" : "off");
442 else
443 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
444 value);
445}
446
447int
448breakpoints_always_inserted_mode (void)
449{
450 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
451 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
452}
453
454static const char condition_evaluation_both[] = "host or target";
455
456/* Modes for breakpoint condition evaluation. */
457static const char condition_evaluation_auto[] = "auto";
458static const char condition_evaluation_host[] = "host";
459static const char condition_evaluation_target[] = "target";
460static const char *const condition_evaluation_enums[] = {
461 condition_evaluation_auto,
462 condition_evaluation_host,
463 condition_evaluation_target,
464 NULL
465};
466
467/* Global that holds the current mode for breakpoint condition evaluation. */
468static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
469
470/* Global that we use to display information to the user (gets its value from
471 condition_evaluation_mode_1. */
472static const char *condition_evaluation_mode = condition_evaluation_auto;
473
474/* Translate a condition evaluation mode MODE into either "host"
475 or "target". This is used mostly to translate from "auto" to the
476 real setting that is being used. It returns the translated
477 evaluation mode. */
478
479static const char *
480translate_condition_evaluation_mode (const char *mode)
481{
482 if (mode == condition_evaluation_auto)
483 {
484 if (target_supports_evaluation_of_breakpoint_conditions ())
485 return condition_evaluation_target;
486 else
487 return condition_evaluation_host;
488 }
489 else
490 return mode;
491}
492
493/* Discovers what condition_evaluation_auto translates to. */
494
495static const char *
496breakpoint_condition_evaluation_mode (void)
497{
498 return translate_condition_evaluation_mode (condition_evaluation_mode);
499}
500
501/* Return true if GDB should evaluate breakpoint conditions or false
502 otherwise. */
503
504static int
505gdb_evaluates_breakpoint_condition_p (void)
506{
507 const char *mode = breakpoint_condition_evaluation_mode ();
508
509 return (mode == condition_evaluation_host);
510}
511
512void _initialize_breakpoint (void);
513
514/* Are we executing breakpoint commands? */
515static int executing_breakpoint_commands;
516
517/* Are overlay event breakpoints enabled? */
518static int overlay_events_enabled;
519
520/* See description in breakpoint.h. */
521int target_exact_watchpoints = 0;
522
523/* Walk the following statement or block through all breakpoints.
524 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
525 current breakpoint. */
526
527#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
528
529#define ALL_BREAKPOINTS_SAFE(B,TMP) \
530 for (B = breakpoint_chain; \
531 B ? (TMP=B->next, 1): 0; \
532 B = TMP)
533
534/* Similar iterator for the low-level breakpoints. SAFE variant is
535 not provided so update_global_location_list must not be called
536 while executing the block of ALL_BP_LOCATIONS. */
537
538#define ALL_BP_LOCATIONS(B,BP_TMP) \
539 for (BP_TMP = bp_location; \
540 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
541 BP_TMP++)
542
543/* Iterates through locations with address ADDRESS for the currently selected
544 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
545 to where the loop should start from.
546 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
547 appropriate location to start with. */
548
549#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
550 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
551 BP_LOCP_TMP = BP_LOCP_START; \
552 BP_LOCP_START \
553 && (BP_LOCP_TMP < bp_location + bp_location_count \
554 && (*BP_LOCP_TMP)->address == ADDRESS); \
555 BP_LOCP_TMP++)
556
557/* Iterator for tracepoints only. */
558
559#define ALL_TRACEPOINTS(B) \
560 for (B = breakpoint_chain; B; B = B->next) \
561 if (is_tracepoint (B))
562
563/* Chains of all breakpoints defined. */
564
565struct breakpoint *breakpoint_chain;
566
567/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
568
569static struct bp_location **bp_location;
570
571/* Number of elements of BP_LOCATION. */
572
573static unsigned bp_location_count;
574
575/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
576 ADDRESS for the current elements of BP_LOCATION which get a valid
577 result from bp_location_has_shadow. You can use it for roughly
578 limiting the subrange of BP_LOCATION to scan for shadow bytes for
579 an address you need to read. */
580
581static CORE_ADDR bp_location_placed_address_before_address_max;
582
583/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
584 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
585 BP_LOCATION which get a valid result from bp_location_has_shadow.
586 You can use it for roughly limiting the subrange of BP_LOCATION to
587 scan for shadow bytes for an address you need to read. */
588
589static CORE_ADDR bp_location_shadow_len_after_address_max;
590
591/* The locations that no longer correspond to any breakpoint, unlinked
592 from bp_location array, but for which a hit may still be reported
593 by a target. */
594VEC(bp_location_p) *moribund_locations = NULL;
595
596/* Number of last breakpoint made. */
597
598static int breakpoint_count;
599
600/* The value of `breakpoint_count' before the last command that
601 created breakpoints. If the last (break-like) command created more
602 than one breakpoint, then the difference between BREAKPOINT_COUNT
603 and PREV_BREAKPOINT_COUNT is more than one. */
604static int prev_breakpoint_count;
605
606/* Number of last tracepoint made. */
607
608static int tracepoint_count;
609
610static struct cmd_list_element *breakpoint_set_cmdlist;
611static struct cmd_list_element *breakpoint_show_cmdlist;
612struct cmd_list_element *save_cmdlist;
613
614/* Return whether a breakpoint is an active enabled breakpoint. */
615static int
616breakpoint_enabled (struct breakpoint *b)
617{
618 return (b->enable_state == bp_enabled);
619}
620
621/* Set breakpoint count to NUM. */
622
623static void
624set_breakpoint_count (int num)
625{
626 prev_breakpoint_count = breakpoint_count;
627 breakpoint_count = num;
628 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
629}
630
631/* Used by `start_rbreak_breakpoints' below, to record the current
632 breakpoint count before "rbreak" creates any breakpoint. */
633static int rbreak_start_breakpoint_count;
634
635/* Called at the start an "rbreak" command to record the first
636 breakpoint made. */
637
638void
639start_rbreak_breakpoints (void)
640{
641 rbreak_start_breakpoint_count = breakpoint_count;
642}
643
644/* Called at the end of an "rbreak" command to record the last
645 breakpoint made. */
646
647void
648end_rbreak_breakpoints (void)
649{
650 prev_breakpoint_count = rbreak_start_breakpoint_count;
651}
652
653/* Used in run_command to zero the hit count when a new run starts. */
654
655void
656clear_breakpoint_hit_counts (void)
657{
658 struct breakpoint *b;
659
660 ALL_BREAKPOINTS (b)
661 b->hit_count = 0;
662}
663
664/* Allocate a new counted_command_line with reference count of 1.
665 The new structure owns COMMANDS. */
666
667static struct counted_command_line *
668alloc_counted_command_line (struct command_line *commands)
669{
670 struct counted_command_line *result
671 = xmalloc (sizeof (struct counted_command_line));
672
673 result->refc = 1;
674 result->commands = commands;
675 return result;
676}
677
678/* Increment reference count. This does nothing if CMD is NULL. */
679
680static void
681incref_counted_command_line (struct counted_command_line *cmd)
682{
683 if (cmd)
684 ++cmd->refc;
685}
686
687/* Decrement reference count. If the reference count reaches 0,
688 destroy the counted_command_line. Sets *CMDP to NULL. This does
689 nothing if *CMDP is NULL. */
690
691static void
692decref_counted_command_line (struct counted_command_line **cmdp)
693{
694 if (*cmdp)
695 {
696 if (--(*cmdp)->refc == 0)
697 {
698 free_command_lines (&(*cmdp)->commands);
699 xfree (*cmdp);
700 }
701 *cmdp = NULL;
702 }
703}
704
705/* A cleanup function that calls decref_counted_command_line. */
706
707static void
708do_cleanup_counted_command_line (void *arg)
709{
710 decref_counted_command_line (arg);
711}
712
713/* Create a cleanup that calls decref_counted_command_line on the
714 argument. */
715
716static struct cleanup *
717make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
718{
719 return make_cleanup (do_cleanup_counted_command_line, cmdp);
720}
721
722\f
723/* Return the breakpoint with the specified number, or NULL
724 if the number does not refer to an existing breakpoint. */
725
726struct breakpoint *
727get_breakpoint (int num)
728{
729 struct breakpoint *b;
730
731 ALL_BREAKPOINTS (b)
732 if (b->number == num)
733 return b;
734
735 return NULL;
736}
737
738\f
739
740/* Mark locations as "conditions have changed" in case the target supports
741 evaluating conditions on its side. */
742
743static void
744mark_breakpoint_modified (struct breakpoint *b)
745{
746 struct bp_location *loc;
747
748 /* This is only meaningful if the target is
749 evaluating conditions and if the user has
750 opted for condition evaluation on the target's
751 side. */
752 if (gdb_evaluates_breakpoint_condition_p ()
753 || !target_supports_evaluation_of_breakpoint_conditions ())
754 return;
755
756 if (!is_breakpoint (b))
757 return;
758
759 for (loc = b->loc; loc; loc = loc->next)
760 loc->condition_changed = condition_modified;
761}
762
763/* Mark location as "conditions have changed" in case the target supports
764 evaluating conditions on its side. */
765
766static void
767mark_breakpoint_location_modified (struct bp_location *loc)
768{
769 /* This is only meaningful if the target is
770 evaluating conditions and if the user has
771 opted for condition evaluation on the target's
772 side. */
773 if (gdb_evaluates_breakpoint_condition_p ()
774 || !target_supports_evaluation_of_breakpoint_conditions ())
775
776 return;
777
778 if (!is_breakpoint (loc->owner))
779 return;
780
781 loc->condition_changed = condition_modified;
782}
783
784/* Sets the condition-evaluation mode using the static global
785 condition_evaluation_mode. */
786
787static void
788set_condition_evaluation_mode (char *args, int from_tty,
789 struct cmd_list_element *c)
790{
791 const char *old_mode, *new_mode;
792
793 if ((condition_evaluation_mode_1 == condition_evaluation_target)
794 && !target_supports_evaluation_of_breakpoint_conditions ())
795 {
796 condition_evaluation_mode_1 = condition_evaluation_mode;
797 warning (_("Target does not support breakpoint condition evaluation.\n"
798 "Using host evaluation mode instead."));
799 return;
800 }
801
802 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
803 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
804
805 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
806 settings was "auto". */
807 condition_evaluation_mode = condition_evaluation_mode_1;
808
809 /* Only update the mode if the user picked a different one. */
810 if (new_mode != old_mode)
811 {
812 struct bp_location *loc, **loc_tmp;
813 /* If the user switched to a different evaluation mode, we
814 need to synch the changes with the target as follows:
815
816 "host" -> "target": Send all (valid) conditions to the target.
817 "target" -> "host": Remove all the conditions from the target.
818 */
819
820 if (new_mode == condition_evaluation_target)
821 {
822 /* Mark everything modified and synch conditions with the
823 target. */
824 ALL_BP_LOCATIONS (loc, loc_tmp)
825 mark_breakpoint_location_modified (loc);
826 }
827 else
828 {
829 /* Manually mark non-duplicate locations to synch conditions
830 with the target. We do this to remove all the conditions the
831 target knows about. */
832 ALL_BP_LOCATIONS (loc, loc_tmp)
833 if (is_breakpoint (loc->owner) && loc->inserted)
834 loc->needs_update = 1;
835 }
836
837 /* Do the update. */
838 update_global_location_list (1);
839 }
840
841 return;
842}
843
844/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
845 what "auto" is translating to. */
846
847static void
848show_condition_evaluation_mode (struct ui_file *file, int from_tty,
849 struct cmd_list_element *c, const char *value)
850{
851 if (condition_evaluation_mode == condition_evaluation_auto)
852 fprintf_filtered (file,
853 _("Breakpoint condition evaluation "
854 "mode is %s (currently %s).\n"),
855 value,
856 breakpoint_condition_evaluation_mode ());
857 else
858 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
859 value);
860}
861
862/* A comparison function for bp_location AP and BP that is used by
863 bsearch. This comparison function only cares about addresses, unlike
864 the more general bp_location_compare function. */
865
866static int
867bp_location_compare_addrs (const void *ap, const void *bp)
868{
869 struct bp_location *a = *(void **) ap;
870 struct bp_location *b = *(void **) bp;
871
872 if (a->address == b->address)
873 return 0;
874 else
875 return ((a->address > b->address) - (a->address < b->address));
876}
877
878/* Helper function to skip all bp_locations with addresses
879 less than ADDRESS. It returns the first bp_location that
880 is greater than or equal to ADDRESS. If none is found, just
881 return NULL. */
882
883static struct bp_location **
884get_first_locp_gte_addr (CORE_ADDR address)
885{
886 struct bp_location dummy_loc;
887 struct bp_location *dummy_locp = &dummy_loc;
888 struct bp_location **locp_found = NULL;
889
890 /* Initialize the dummy location's address field. */
891 memset (&dummy_loc, 0, sizeof (struct bp_location));
892 dummy_loc.address = address;
893
894 /* Find a close match to the first location at ADDRESS. */
895 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
896 sizeof (struct bp_location **),
897 bp_location_compare_addrs);
898
899 /* Nothing was found, nothing left to do. */
900 if (locp_found == NULL)
901 return NULL;
902
903 /* We may have found a location that is at ADDRESS but is not the first in the
904 location's list. Go backwards (if possible) and locate the first one. */
905 while ((locp_found - 1) >= bp_location
906 && (*(locp_found - 1))->address == address)
907 locp_found--;
908
909 return locp_found;
910}
911
912void
913set_breakpoint_condition (struct breakpoint *b, char *exp,
914 int from_tty)
915{
916 xfree (b->cond_string);
917 b->cond_string = NULL;
918
919 if (is_watchpoint (b))
920 {
921 struct watchpoint *w = (struct watchpoint *) b;
922
923 xfree (w->cond_exp);
924 w->cond_exp = NULL;
925 }
926 else
927 {
928 struct bp_location *loc;
929
930 for (loc = b->loc; loc; loc = loc->next)
931 {
932 xfree (loc->cond);
933 loc->cond = NULL;
934
935 /* No need to free the condition agent expression
936 bytecode (if we have one). We will handle this
937 when we go through update_global_location_list. */
938 }
939 }
940
941 if (*exp == 0)
942 {
943 if (from_tty)
944 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
945 }
946 else
947 {
948 const char *arg = exp;
949
950 /* I don't know if it matters whether this is the string the user
951 typed in or the decompiled expression. */
952 b->cond_string = xstrdup (arg);
953 b->condition_not_parsed = 0;
954
955 if (is_watchpoint (b))
956 {
957 struct watchpoint *w = (struct watchpoint *) b;
958
959 innermost_block = NULL;
960 arg = exp;
961 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
962 if (*arg)
963 error (_("Junk at end of expression"));
964 w->cond_exp_valid_block = innermost_block;
965 }
966 else
967 {
968 struct bp_location *loc;
969
970 for (loc = b->loc; loc; loc = loc->next)
971 {
972 arg = exp;
973 loc->cond =
974 parse_exp_1 (&arg, loc->address,
975 block_for_pc (loc->address), 0);
976 if (*arg)
977 error (_("Junk at end of expression"));
978 }
979 }
980 }
981 mark_breakpoint_modified (b);
982
983 observer_notify_breakpoint_modified (b);
984}
985
986/* Completion for the "condition" command. */
987
988static VEC (char_ptr) *
989condition_completer (struct cmd_list_element *cmd,
990 const char *text, const char *word)
991{
992 const char *space;
993
994 text = skip_spaces_const (text);
995 space = skip_to_space_const (text);
996 if (*space == '\0')
997 {
998 int len;
999 struct breakpoint *b;
1000 VEC (char_ptr) *result = NULL;
1001
1002 if (text[0] == '$')
1003 {
1004 /* We don't support completion of history indices. */
1005 if (isdigit (text[1]))
1006 return NULL;
1007 return complete_internalvar (&text[1]);
1008 }
1009
1010 /* We're completing the breakpoint number. */
1011 len = strlen (text);
1012
1013 ALL_BREAKPOINTS (b)
1014 {
1015 char number[50];
1016
1017 xsnprintf (number, sizeof (number), "%d", b->number);
1018
1019 if (strncmp (number, text, len) == 0)
1020 VEC_safe_push (char_ptr, result, xstrdup (number));
1021 }
1022
1023 return result;
1024 }
1025
1026 /* We're completing the expression part. */
1027 text = skip_spaces_const (space);
1028 return expression_completer (cmd, text, word);
1029}
1030
1031/* condition N EXP -- set break condition of breakpoint N to EXP. */
1032
1033static void
1034condition_command (char *arg, int from_tty)
1035{
1036 struct breakpoint *b;
1037 char *p;
1038 int bnum;
1039
1040 if (arg == 0)
1041 error_no_arg (_("breakpoint number"));
1042
1043 p = arg;
1044 bnum = get_number (&p);
1045 if (bnum == 0)
1046 error (_("Bad breakpoint argument: '%s'"), arg);
1047
1048 ALL_BREAKPOINTS (b)
1049 if (b->number == bnum)
1050 {
1051 /* Check if this breakpoint has a Python object assigned to
1052 it, and if it has a definition of the "stop"
1053 method. This method and conditions entered into GDB from
1054 the CLI are mutually exclusive. */
1055 if (b->py_bp_object
1056 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1057 error (_("Cannot set a condition where a Python 'stop' "
1058 "method has been defined in the breakpoint."));
1059 set_breakpoint_condition (b, p, from_tty);
1060
1061 if (is_breakpoint (b))
1062 update_global_location_list (1);
1063
1064 return;
1065 }
1066
1067 error (_("No breakpoint number %d."), bnum);
1068}
1069
1070/* Check that COMMAND do not contain commands that are suitable
1071 only for tracepoints and not suitable for ordinary breakpoints.
1072 Throw if any such commands is found. */
1073
1074static void
1075check_no_tracepoint_commands (struct command_line *commands)
1076{
1077 struct command_line *c;
1078
1079 for (c = commands; c; c = c->next)
1080 {
1081 int i;
1082
1083 if (c->control_type == while_stepping_control)
1084 error (_("The 'while-stepping' command can "
1085 "only be used for tracepoints"));
1086
1087 for (i = 0; i < c->body_count; ++i)
1088 check_no_tracepoint_commands ((c->body_list)[i]);
1089
1090 /* Not that command parsing removes leading whitespace and comment
1091 lines and also empty lines. So, we only need to check for
1092 command directly. */
1093 if (strstr (c->line, "collect ") == c->line)
1094 error (_("The 'collect' command can only be used for tracepoints"));
1095
1096 if (strstr (c->line, "teval ") == c->line)
1097 error (_("The 'teval' command can only be used for tracepoints"));
1098 }
1099}
1100
1101/* Encapsulate tests for different types of tracepoints. */
1102
1103static int
1104is_tracepoint_type (enum bptype type)
1105{
1106 return (type == bp_tracepoint
1107 || type == bp_fast_tracepoint
1108 || type == bp_static_tracepoint);
1109}
1110
1111int
1112is_tracepoint (const struct breakpoint *b)
1113{
1114 return is_tracepoint_type (b->type);
1115}
1116
1117/* A helper function that validates that COMMANDS are valid for a
1118 breakpoint. This function will throw an exception if a problem is
1119 found. */
1120
1121static void
1122validate_commands_for_breakpoint (struct breakpoint *b,
1123 struct command_line *commands)
1124{
1125 if (is_tracepoint (b))
1126 {
1127 struct tracepoint *t = (struct tracepoint *) b;
1128 struct command_line *c;
1129 struct command_line *while_stepping = 0;
1130
1131 /* Reset the while-stepping step count. The previous commands
1132 might have included a while-stepping action, while the new
1133 ones might not. */
1134 t->step_count = 0;
1135
1136 /* We need to verify that each top-level element of commands is
1137 valid for tracepoints, that there's at most one
1138 while-stepping element, and that the while-stepping's body
1139 has valid tracing commands excluding nested while-stepping.
1140 We also need to validate the tracepoint action line in the
1141 context of the tracepoint --- validate_actionline actually
1142 has side effects, like setting the tracepoint's
1143 while-stepping STEP_COUNT, in addition to checking if the
1144 collect/teval actions parse and make sense in the
1145 tracepoint's context. */
1146 for (c = commands; c; c = c->next)
1147 {
1148 if (c->control_type == while_stepping_control)
1149 {
1150 if (b->type == bp_fast_tracepoint)
1151 error (_("The 'while-stepping' command "
1152 "cannot be used for fast tracepoint"));
1153 else if (b->type == bp_static_tracepoint)
1154 error (_("The 'while-stepping' command "
1155 "cannot be used for static tracepoint"));
1156
1157 if (while_stepping)
1158 error (_("The 'while-stepping' command "
1159 "can be used only once"));
1160 else
1161 while_stepping = c;
1162 }
1163
1164 validate_actionline (c->line, b);
1165 }
1166 if (while_stepping)
1167 {
1168 struct command_line *c2;
1169
1170 gdb_assert (while_stepping->body_count == 1);
1171 c2 = while_stepping->body_list[0];
1172 for (; c2; c2 = c2->next)
1173 {
1174 if (c2->control_type == while_stepping_control)
1175 error (_("The 'while-stepping' command cannot be nested"));
1176 }
1177 }
1178 }
1179 else
1180 {
1181 check_no_tracepoint_commands (commands);
1182 }
1183}
1184
1185/* Return a vector of all the static tracepoints set at ADDR. The
1186 caller is responsible for releasing the vector. */
1187
1188VEC(breakpoint_p) *
1189static_tracepoints_here (CORE_ADDR addr)
1190{
1191 struct breakpoint *b;
1192 VEC(breakpoint_p) *found = 0;
1193 struct bp_location *loc;
1194
1195 ALL_BREAKPOINTS (b)
1196 if (b->type == bp_static_tracepoint)
1197 {
1198 for (loc = b->loc; loc; loc = loc->next)
1199 if (loc->address == addr)
1200 VEC_safe_push(breakpoint_p, found, b);
1201 }
1202
1203 return found;
1204}
1205
1206/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1207 validate that only allowed commands are included. */
1208
1209void
1210breakpoint_set_commands (struct breakpoint *b,
1211 struct command_line *commands)
1212{
1213 validate_commands_for_breakpoint (b, commands);
1214
1215 decref_counted_command_line (&b->commands);
1216 b->commands = alloc_counted_command_line (commands);
1217 observer_notify_breakpoint_modified (b);
1218}
1219
1220/* Set the internal `silent' flag on the breakpoint. Note that this
1221 is not the same as the "silent" that may appear in the breakpoint's
1222 commands. */
1223
1224void
1225breakpoint_set_silent (struct breakpoint *b, int silent)
1226{
1227 int old_silent = b->silent;
1228
1229 b->silent = silent;
1230 if (old_silent != silent)
1231 observer_notify_breakpoint_modified (b);
1232}
1233
1234/* Set the thread for this breakpoint. If THREAD is -1, make the
1235 breakpoint work for any thread. */
1236
1237void
1238breakpoint_set_thread (struct breakpoint *b, int thread)
1239{
1240 int old_thread = b->thread;
1241
1242 b->thread = thread;
1243 if (old_thread != thread)
1244 observer_notify_breakpoint_modified (b);
1245}
1246
1247/* Set the task for this breakpoint. If TASK is 0, make the
1248 breakpoint work for any task. */
1249
1250void
1251breakpoint_set_task (struct breakpoint *b, int task)
1252{
1253 int old_task = b->task;
1254
1255 b->task = task;
1256 if (old_task != task)
1257 observer_notify_breakpoint_modified (b);
1258}
1259
1260void
1261check_tracepoint_command (char *line, void *closure)
1262{
1263 struct breakpoint *b = closure;
1264
1265 validate_actionline (line, b);
1266}
1267
1268/* A structure used to pass information through
1269 map_breakpoint_numbers. */
1270
1271struct commands_info
1272{
1273 /* True if the command was typed at a tty. */
1274 int from_tty;
1275
1276 /* The breakpoint range spec. */
1277 char *arg;
1278
1279 /* Non-NULL if the body of the commands are being read from this
1280 already-parsed command. */
1281 struct command_line *control;
1282
1283 /* The command lines read from the user, or NULL if they have not
1284 yet been read. */
1285 struct counted_command_line *cmd;
1286};
1287
1288/* A callback for map_breakpoint_numbers that sets the commands for
1289 commands_command. */
1290
1291static void
1292do_map_commands_command (struct breakpoint *b, void *data)
1293{
1294 struct commands_info *info = data;
1295
1296 if (info->cmd == NULL)
1297 {
1298 struct command_line *l;
1299
1300 if (info->control != NULL)
1301 l = copy_command_lines (info->control->body_list[0]);
1302 else
1303 {
1304 struct cleanup *old_chain;
1305 char *str;
1306
1307 str = xstrprintf (_("Type commands for breakpoint(s) "
1308 "%s, one per line."),
1309 info->arg);
1310
1311 old_chain = make_cleanup (xfree, str);
1312
1313 l = read_command_lines (str,
1314 info->from_tty, 1,
1315 (is_tracepoint (b)
1316 ? check_tracepoint_command : 0),
1317 b);
1318
1319 do_cleanups (old_chain);
1320 }
1321
1322 info->cmd = alloc_counted_command_line (l);
1323 }
1324
1325 /* If a breakpoint was on the list more than once, we don't need to
1326 do anything. */
1327 if (b->commands != info->cmd)
1328 {
1329 validate_commands_for_breakpoint (b, info->cmd->commands);
1330 incref_counted_command_line (info->cmd);
1331 decref_counted_command_line (&b->commands);
1332 b->commands = info->cmd;
1333 observer_notify_breakpoint_modified (b);
1334 }
1335}
1336
1337static void
1338commands_command_1 (char *arg, int from_tty,
1339 struct command_line *control)
1340{
1341 struct cleanup *cleanups;
1342 struct commands_info info;
1343
1344 info.from_tty = from_tty;
1345 info.control = control;
1346 info.cmd = NULL;
1347 /* If we read command lines from the user, then `info' will hold an
1348 extra reference to the commands that we must clean up. */
1349 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1350
1351 if (arg == NULL || !*arg)
1352 {
1353 if (breakpoint_count - prev_breakpoint_count > 1)
1354 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1355 breakpoint_count);
1356 else if (breakpoint_count > 0)
1357 arg = xstrprintf ("%d", breakpoint_count);
1358 else
1359 {
1360 /* So that we don't try to free the incoming non-NULL
1361 argument in the cleanup below. Mapping breakpoint
1362 numbers will fail in this case. */
1363 arg = NULL;
1364 }
1365 }
1366 else
1367 /* The command loop has some static state, so we need to preserve
1368 our argument. */
1369 arg = xstrdup (arg);
1370
1371 if (arg != NULL)
1372 make_cleanup (xfree, arg);
1373
1374 info.arg = arg;
1375
1376 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1377
1378 if (info.cmd == NULL)
1379 error (_("No breakpoints specified."));
1380
1381 do_cleanups (cleanups);
1382}
1383
1384static void
1385commands_command (char *arg, int from_tty)
1386{
1387 commands_command_1 (arg, from_tty, NULL);
1388}
1389
1390/* Like commands_command, but instead of reading the commands from
1391 input stream, takes them from an already parsed command structure.
1392
1393 This is used by cli-script.c to DTRT with breakpoint commands
1394 that are part of if and while bodies. */
1395enum command_control_type
1396commands_from_control_command (char *arg, struct command_line *cmd)
1397{
1398 commands_command_1 (arg, 0, cmd);
1399 return simple_control;
1400}
1401
1402/* Return non-zero if BL->TARGET_INFO contains valid information. */
1403
1404static int
1405bp_location_has_shadow (struct bp_location *bl)
1406{
1407 if (bl->loc_type != bp_loc_software_breakpoint)
1408 return 0;
1409 if (!bl->inserted)
1410 return 0;
1411 if (bl->target_info.shadow_len == 0)
1412 /* BL isn't valid, or doesn't shadow memory. */
1413 return 0;
1414 return 1;
1415}
1416
1417/* Update BUF, which is LEN bytes read from the target address MEMADDR,
1418 by replacing any memory breakpoints with their shadowed contents.
1419
1420 If READBUF is not NULL, this buffer must not overlap with any of
1421 the breakpoint location's shadow_contents buffers. Otherwise,
1422 a failed assertion internal error will be raised.
1423
1424 The range of shadowed area by each bp_location is:
1425 bl->address - bp_location_placed_address_before_address_max
1426 up to bl->address + bp_location_shadow_len_after_address_max
1427 The range we were requested to resolve shadows for is:
1428 memaddr ... memaddr + len
1429 Thus the safe cutoff boundaries for performance optimization are
1430 memaddr + len <= (bl->address
1431 - bp_location_placed_address_before_address_max)
1432 and:
1433 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1434
1435void
1436breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1437 const gdb_byte *writebuf_org,
1438 ULONGEST memaddr, LONGEST len)
1439{
1440 /* Left boundary, right boundary and median element of our binary
1441 search. */
1442 unsigned bc_l, bc_r, bc;
1443
1444 /* Find BC_L which is a leftmost element which may affect BUF
1445 content. It is safe to report lower value but a failure to
1446 report higher one. */
1447
1448 bc_l = 0;
1449 bc_r = bp_location_count;
1450 while (bc_l + 1 < bc_r)
1451 {
1452 struct bp_location *bl;
1453
1454 bc = (bc_l + bc_r) / 2;
1455 bl = bp_location[bc];
1456
1457 /* Check first BL->ADDRESS will not overflow due to the added
1458 constant. Then advance the left boundary only if we are sure
1459 the BC element can in no way affect the BUF content (MEMADDR
1460 to MEMADDR + LEN range).
1461
1462 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1463 offset so that we cannot miss a breakpoint with its shadow
1464 range tail still reaching MEMADDR. */
1465
1466 if ((bl->address + bp_location_shadow_len_after_address_max
1467 >= bl->address)
1468 && (bl->address + bp_location_shadow_len_after_address_max
1469 <= memaddr))
1470 bc_l = bc;
1471 else
1472 bc_r = bc;
1473 }
1474
1475 /* Due to the binary search above, we need to make sure we pick the
1476 first location that's at BC_L's address. E.g., if there are
1477 multiple locations at the same address, BC_L may end up pointing
1478 at a duplicate location, and miss the "master"/"inserted"
1479 location. Say, given locations L1, L2 and L3 at addresses A and
1480 B:
1481
1482 L1@A, L2@A, L3@B, ...
1483
1484 BC_L could end up pointing at location L2, while the "master"
1485 location could be L1. Since the `loc->inserted' flag is only set
1486 on "master" locations, we'd forget to restore the shadow of L1
1487 and L2. */
1488 while (bc_l > 0
1489 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1490 bc_l--;
1491
1492 /* Now do full processing of the found relevant range of elements. */
1493
1494 for (bc = bc_l; bc < bp_location_count; bc++)
1495 {
1496 struct bp_location *bl = bp_location[bc];
1497 CORE_ADDR bp_addr = 0;
1498 int bp_size = 0;
1499 int bptoffset = 0;
1500
1501 /* bp_location array has BL->OWNER always non-NULL. */
1502 if (bl->owner->type == bp_none)
1503 warning (_("reading through apparently deleted breakpoint #%d?"),
1504 bl->owner->number);
1505
1506 /* Performance optimization: any further element can no longer affect BUF
1507 content. */
1508
1509 if (bl->address >= bp_location_placed_address_before_address_max
1510 && memaddr + len <= (bl->address
1511 - bp_location_placed_address_before_address_max))
1512 break;
1513
1514 if (!bp_location_has_shadow (bl))
1515 continue;
1516 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1517 current_program_space->aspace, 0))
1518 continue;
1519
1520 /* Addresses and length of the part of the breakpoint that
1521 we need to copy. */
1522 bp_addr = bl->target_info.placed_address;
1523 bp_size = bl->target_info.shadow_len;
1524
1525 if (bp_addr + bp_size <= memaddr)
1526 /* The breakpoint is entirely before the chunk of memory we
1527 are reading. */
1528 continue;
1529
1530 if (bp_addr >= memaddr + len)
1531 /* The breakpoint is entirely after the chunk of memory we are
1532 reading. */
1533 continue;
1534
1535 /* Offset within shadow_contents. */
1536 if (bp_addr < memaddr)
1537 {
1538 /* Only copy the second part of the breakpoint. */
1539 bp_size -= memaddr - bp_addr;
1540 bptoffset = memaddr - bp_addr;
1541 bp_addr = memaddr;
1542 }
1543
1544 if (bp_addr + bp_size > memaddr + len)
1545 {
1546 /* Only copy the first part of the breakpoint. */
1547 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1548 }
1549
1550 if (readbuf != NULL)
1551 {
1552 /* Verify that the readbuf buffer does not overlap with
1553 the shadow_contents buffer. */
1554 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1555 || readbuf >= (bl->target_info.shadow_contents
1556 + bl->target_info.shadow_len));
1557
1558 /* Update the read buffer with this inserted breakpoint's
1559 shadow. */
1560 memcpy (readbuf + bp_addr - memaddr,
1561 bl->target_info.shadow_contents + bptoffset, bp_size);
1562 }
1563 else
1564 {
1565 struct gdbarch *gdbarch = bl->gdbarch;
1566 const unsigned char *bp;
1567 CORE_ADDR placed_address = bl->target_info.placed_address;
1568 int placed_size = bl->target_info.placed_size;
1569
1570 /* Update the shadow with what we want to write to memory. */
1571 memcpy (bl->target_info.shadow_contents + bptoffset,
1572 writebuf_org + bp_addr - memaddr, bp_size);
1573
1574 /* Determine appropriate breakpoint contents and size for this
1575 address. */
1576 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1577
1578 /* Update the final write buffer with this inserted
1579 breakpoint's INSN. */
1580 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1581 }
1582 }
1583}
1584\f
1585
1586/* Return true if BPT is either a software breakpoint or a hardware
1587 breakpoint. */
1588
1589int
1590is_breakpoint (const struct breakpoint *bpt)
1591{
1592 return (bpt->type == bp_breakpoint
1593 || bpt->type == bp_hardware_breakpoint
1594 || bpt->type == bp_dprintf);
1595}
1596
1597/* Return true if BPT is of any hardware watchpoint kind. */
1598
1599static int
1600is_hardware_watchpoint (const struct breakpoint *bpt)
1601{
1602 return (bpt->type == bp_hardware_watchpoint
1603 || bpt->type == bp_read_watchpoint
1604 || bpt->type == bp_access_watchpoint);
1605}
1606
1607/* Return true if BPT is of any watchpoint kind, hardware or
1608 software. */
1609
1610int
1611is_watchpoint (const struct breakpoint *bpt)
1612{
1613 return (is_hardware_watchpoint (bpt)
1614 || bpt->type == bp_watchpoint);
1615}
1616
1617/* Returns true if the current thread and its running state are safe
1618 to evaluate or update watchpoint B. Watchpoints on local
1619 expressions need to be evaluated in the context of the thread that
1620 was current when the watchpoint was created, and, that thread needs
1621 to be stopped to be able to select the correct frame context.
1622 Watchpoints on global expressions can be evaluated on any thread,
1623 and in any state. It is presently left to the target allowing
1624 memory accesses when threads are running. */
1625
1626static int
1627watchpoint_in_thread_scope (struct watchpoint *b)
1628{
1629 return (b->base.pspace == current_program_space
1630 && (ptid_equal (b->watchpoint_thread, null_ptid)
1631 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1632 && !is_executing (inferior_ptid))));
1633}
1634
1635/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1636 associated bp_watchpoint_scope breakpoint. */
1637
1638static void
1639watchpoint_del_at_next_stop (struct watchpoint *w)
1640{
1641 struct breakpoint *b = &w->base;
1642
1643 if (b->related_breakpoint != b)
1644 {
1645 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1646 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1647 b->related_breakpoint->disposition = disp_del_at_next_stop;
1648 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1649 b->related_breakpoint = b;
1650 }
1651 b->disposition = disp_del_at_next_stop;
1652}
1653
1654/* Assuming that B is a watchpoint:
1655 - Reparse watchpoint expression, if REPARSE is non-zero
1656 - Evaluate expression and store the result in B->val
1657 - Evaluate the condition if there is one, and store the result
1658 in b->loc->cond.
1659 - Update the list of values that must be watched in B->loc.
1660
1661 If the watchpoint disposition is disp_del_at_next_stop, then do
1662 nothing. If this is local watchpoint that is out of scope, delete
1663 it.
1664
1665 Even with `set breakpoint always-inserted on' the watchpoints are
1666 removed + inserted on each stop here. Normal breakpoints must
1667 never be removed because they might be missed by a running thread
1668 when debugging in non-stop mode. On the other hand, hardware
1669 watchpoints (is_hardware_watchpoint; processed here) are specific
1670 to each LWP since they are stored in each LWP's hardware debug
1671 registers. Therefore, such LWP must be stopped first in order to
1672 be able to modify its hardware watchpoints.
1673
1674 Hardware watchpoints must be reset exactly once after being
1675 presented to the user. It cannot be done sooner, because it would
1676 reset the data used to present the watchpoint hit to the user. And
1677 it must not be done later because it could display the same single
1678 watchpoint hit during multiple GDB stops. Note that the latter is
1679 relevant only to the hardware watchpoint types bp_read_watchpoint
1680 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1681 not user-visible - its hit is suppressed if the memory content has
1682 not changed.
1683
1684 The following constraints influence the location where we can reset
1685 hardware watchpoints:
1686
1687 * target_stopped_by_watchpoint and target_stopped_data_address are
1688 called several times when GDB stops.
1689
1690 [linux]
1691 * Multiple hardware watchpoints can be hit at the same time,
1692 causing GDB to stop. GDB only presents one hardware watchpoint
1693 hit at a time as the reason for stopping, and all the other hits
1694 are presented later, one after the other, each time the user
1695 requests the execution to be resumed. Execution is not resumed
1696 for the threads still having pending hit event stored in
1697 LWP_INFO->STATUS. While the watchpoint is already removed from
1698 the inferior on the first stop the thread hit event is kept being
1699 reported from its cached value by linux_nat_stopped_data_address
1700 until the real thread resume happens after the watchpoint gets
1701 presented and thus its LWP_INFO->STATUS gets reset.
1702
1703 Therefore the hardware watchpoint hit can get safely reset on the
1704 watchpoint removal from inferior. */
1705
1706static void
1707update_watchpoint (struct watchpoint *b, int reparse)
1708{
1709 int within_current_scope;
1710 struct frame_id saved_frame_id;
1711 int frame_saved;
1712
1713 /* If this is a local watchpoint, we only want to check if the
1714 watchpoint frame is in scope if the current thread is the thread
1715 that was used to create the watchpoint. */
1716 if (!watchpoint_in_thread_scope (b))
1717 return;
1718
1719 if (b->base.disposition == disp_del_at_next_stop)
1720 return;
1721
1722 frame_saved = 0;
1723
1724 /* Determine if the watchpoint is within scope. */
1725 if (b->exp_valid_block == NULL)
1726 within_current_scope = 1;
1727 else
1728 {
1729 struct frame_info *fi = get_current_frame ();
1730 struct gdbarch *frame_arch = get_frame_arch (fi);
1731 CORE_ADDR frame_pc = get_frame_pc (fi);
1732
1733 /* If we're in a function epilogue, unwinding may not work
1734 properly, so do not attempt to recreate locations at this
1735 point. See similar comments in watchpoint_check. */
1736 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1737 return;
1738
1739 /* Save the current frame's ID so we can restore it after
1740 evaluating the watchpoint expression on its own frame. */
1741 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1742 took a frame parameter, so that we didn't have to change the
1743 selected frame. */
1744 frame_saved = 1;
1745 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1746
1747 fi = frame_find_by_id (b->watchpoint_frame);
1748 within_current_scope = (fi != NULL);
1749 if (within_current_scope)
1750 select_frame (fi);
1751 }
1752
1753 /* We don't free locations. They are stored in the bp_location array
1754 and update_global_location_list will eventually delete them and
1755 remove breakpoints if needed. */
1756 b->base.loc = NULL;
1757
1758 if (within_current_scope && reparse)
1759 {
1760 const char *s;
1761
1762 if (b->exp)
1763 {
1764 xfree (b->exp);
1765 b->exp = NULL;
1766 }
1767 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1768 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1769 /* If the meaning of expression itself changed, the old value is
1770 no longer relevant. We don't want to report a watchpoint hit
1771 to the user when the old value and the new value may actually
1772 be completely different objects. */
1773 value_free (b->val);
1774 b->val = NULL;
1775 b->val_valid = 0;
1776
1777 /* Note that unlike with breakpoints, the watchpoint's condition
1778 expression is stored in the breakpoint object, not in the
1779 locations (re)created below. */
1780 if (b->base.cond_string != NULL)
1781 {
1782 if (b->cond_exp != NULL)
1783 {
1784 xfree (b->cond_exp);
1785 b->cond_exp = NULL;
1786 }
1787
1788 s = b->base.cond_string;
1789 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1790 }
1791 }
1792
1793 /* If we failed to parse the expression, for example because
1794 it refers to a global variable in a not-yet-loaded shared library,
1795 don't try to insert watchpoint. We don't automatically delete
1796 such watchpoint, though, since failure to parse expression
1797 is different from out-of-scope watchpoint. */
1798 if (!target_has_execution)
1799 {
1800 /* Without execution, memory can't change. No use to try and
1801 set watchpoint locations. The watchpoint will be reset when
1802 the target gains execution, through breakpoint_re_set. */
1803 if (!can_use_hw_watchpoints)
1804 {
1805 if (b->base.ops->works_in_software_mode (&b->base))
1806 b->base.type = bp_watchpoint;
1807 else
1808 error (_("Can't set read/access watchpoint when "
1809 "hardware watchpoints are disabled."));
1810 }
1811 }
1812 else if (within_current_scope && b->exp)
1813 {
1814 int pc = 0;
1815 struct value *val_chain, *v, *result, *next;
1816 struct program_space *frame_pspace;
1817
1818 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1819
1820 /* Avoid setting b->val if it's already set. The meaning of
1821 b->val is 'the last value' user saw, and we should update
1822 it only if we reported that last value to user. As it
1823 happens, the code that reports it updates b->val directly.
1824 We don't keep track of the memory value for masked
1825 watchpoints. */
1826 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1827 {
1828 b->val = v;
1829 b->val_valid = 1;
1830 }
1831
1832 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1833
1834 /* Look at each value on the value chain. */
1835 for (v = val_chain; v; v = value_next (v))
1836 {
1837 /* If it's a memory location, and GDB actually needed
1838 its contents to evaluate the expression, then we
1839 must watch it. If the first value returned is
1840 still lazy, that means an error occurred reading it;
1841 watch it anyway in case it becomes readable. */
1842 if (VALUE_LVAL (v) == lval_memory
1843 && (v == val_chain || ! value_lazy (v)))
1844 {
1845 struct type *vtype = check_typedef (value_type (v));
1846
1847 /* We only watch structs and arrays if user asked
1848 for it explicitly, never if they just happen to
1849 appear in the middle of some value chain. */
1850 if (v == result
1851 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1852 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1853 {
1854 CORE_ADDR addr;
1855 int type;
1856 struct bp_location *loc, **tmp;
1857
1858 addr = value_address (v);
1859 type = hw_write;
1860 if (b->base.type == bp_read_watchpoint)
1861 type = hw_read;
1862 else if (b->base.type == bp_access_watchpoint)
1863 type = hw_access;
1864
1865 loc = allocate_bp_location (&b->base);
1866 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1867 ;
1868 *tmp = loc;
1869 loc->gdbarch = get_type_arch (value_type (v));
1870
1871 loc->pspace = frame_pspace;
1872 loc->address = addr;
1873 loc->length = TYPE_LENGTH (value_type (v));
1874 loc->watchpoint_type = type;
1875 }
1876 }
1877 }
1878
1879 /* Change the type of breakpoint between hardware assisted or
1880 an ordinary watchpoint depending on the hardware support
1881 and free hardware slots. REPARSE is set when the inferior
1882 is started. */
1883 if (reparse)
1884 {
1885 int reg_cnt;
1886 enum bp_loc_type loc_type;
1887 struct bp_location *bl;
1888
1889 reg_cnt = can_use_hardware_watchpoint (val_chain);
1890
1891 if (reg_cnt)
1892 {
1893 int i, target_resources_ok, other_type_used;
1894 enum bptype type;
1895
1896 /* Use an exact watchpoint when there's only one memory region to be
1897 watched, and only one debug register is needed to watch it. */
1898 b->exact = target_exact_watchpoints && reg_cnt == 1;
1899
1900 /* We need to determine how many resources are already
1901 used for all other hardware watchpoints plus this one
1902 to see if we still have enough resources to also fit
1903 this watchpoint in as well. */
1904
1905 /* If this is a software watchpoint, we try to turn it
1906 to a hardware one -- count resources as if B was of
1907 hardware watchpoint type. */
1908 type = b->base.type;
1909 if (type == bp_watchpoint)
1910 type = bp_hardware_watchpoint;
1911
1912 /* This watchpoint may or may not have been placed on
1913 the list yet at this point (it won't be in the list
1914 if we're trying to create it for the first time,
1915 through watch_command), so always account for it
1916 manually. */
1917
1918 /* Count resources used by all watchpoints except B. */
1919 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1920
1921 /* Add in the resources needed for B. */
1922 i += hw_watchpoint_use_count (&b->base);
1923
1924 target_resources_ok
1925 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1926 if (target_resources_ok <= 0)
1927 {
1928 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1929
1930 if (target_resources_ok == 0 && !sw_mode)
1931 error (_("Target does not support this type of "
1932 "hardware watchpoint."));
1933 else if (target_resources_ok < 0 && !sw_mode)
1934 error (_("There are not enough available hardware "
1935 "resources for this watchpoint."));
1936
1937 /* Downgrade to software watchpoint. */
1938 b->base.type = bp_watchpoint;
1939 }
1940 else
1941 {
1942 /* If this was a software watchpoint, we've just
1943 found we have enough resources to turn it to a
1944 hardware watchpoint. Otherwise, this is a
1945 nop. */
1946 b->base.type = type;
1947 }
1948 }
1949 else if (!b->base.ops->works_in_software_mode (&b->base))
1950 {
1951 if (!can_use_hw_watchpoints)
1952 error (_("Can't set read/access watchpoint when "
1953 "hardware watchpoints are disabled."));
1954 else
1955 error (_("Expression cannot be implemented with "
1956 "read/access watchpoint."));
1957 }
1958 else
1959 b->base.type = bp_watchpoint;
1960
1961 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1962 : bp_loc_hardware_watchpoint);
1963 for (bl = b->base.loc; bl; bl = bl->next)
1964 bl->loc_type = loc_type;
1965 }
1966
1967 for (v = val_chain; v; v = next)
1968 {
1969 next = value_next (v);
1970 if (v != b->val)
1971 value_free (v);
1972 }
1973
1974 /* If a software watchpoint is not watching any memory, then the
1975 above left it without any location set up. But,
1976 bpstat_stop_status requires a location to be able to report
1977 stops, so make sure there's at least a dummy one. */
1978 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1979 {
1980 struct breakpoint *base = &b->base;
1981 base->loc = allocate_bp_location (base);
1982 base->loc->pspace = frame_pspace;
1983 base->loc->address = -1;
1984 base->loc->length = -1;
1985 base->loc->watchpoint_type = -1;
1986 }
1987 }
1988 else if (!within_current_scope)
1989 {
1990 printf_filtered (_("\
1991Watchpoint %d deleted because the program has left the block\n\
1992in which its expression is valid.\n"),
1993 b->base.number);
1994 watchpoint_del_at_next_stop (b);
1995 }
1996
1997 /* Restore the selected frame. */
1998 if (frame_saved)
1999 select_frame (frame_find_by_id (saved_frame_id));
2000}
2001
2002
2003/* Returns 1 iff breakpoint location should be
2004 inserted in the inferior. We don't differentiate the type of BL's owner
2005 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2006 breakpoint_ops is not defined, because in insert_bp_location,
2007 tracepoint's insert_location will not be called. */
2008static int
2009should_be_inserted (struct bp_location *bl)
2010{
2011 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2012 return 0;
2013
2014 if (bl->owner->disposition == disp_del_at_next_stop)
2015 return 0;
2016
2017 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2018 return 0;
2019
2020 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2021 return 0;
2022
2023 /* This is set for example, when we're attached to the parent of a
2024 vfork, and have detached from the child. The child is running
2025 free, and we expect it to do an exec or exit, at which point the
2026 OS makes the parent schedulable again (and the target reports
2027 that the vfork is done). Until the child is done with the shared
2028 memory region, do not insert breakpoints in the parent, otherwise
2029 the child could still trip on the parent's breakpoints. Since
2030 the parent is blocked anyway, it won't miss any breakpoint. */
2031 if (bl->pspace->breakpoints_not_allowed)
2032 return 0;
2033
2034 return 1;
2035}
2036
2037/* Same as should_be_inserted but does the check assuming
2038 that the location is not duplicated. */
2039
2040static int
2041unduplicated_should_be_inserted (struct bp_location *bl)
2042{
2043 int result;
2044 const int save_duplicate = bl->duplicate;
2045
2046 bl->duplicate = 0;
2047 result = should_be_inserted (bl);
2048 bl->duplicate = save_duplicate;
2049 return result;
2050}
2051
2052/* Parses a conditional described by an expression COND into an
2053 agent expression bytecode suitable for evaluation
2054 by the bytecode interpreter. Return NULL if there was
2055 any error during parsing. */
2056
2057static struct agent_expr *
2058parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2059{
2060 struct agent_expr *aexpr = NULL;
2061 volatile struct gdb_exception ex;
2062
2063 if (!cond)
2064 return NULL;
2065
2066 /* We don't want to stop processing, so catch any errors
2067 that may show up. */
2068 TRY_CATCH (ex, RETURN_MASK_ERROR)
2069 {
2070 aexpr = gen_eval_for_expr (scope, cond);
2071 }
2072
2073 if (ex.reason < 0)
2074 {
2075 /* If we got here, it means the condition could not be parsed to a valid
2076 bytecode expression and thus can't be evaluated on the target's side.
2077 It's no use iterating through the conditions. */
2078 return NULL;
2079 }
2080
2081 /* We have a valid agent expression. */
2082 return aexpr;
2083}
2084
2085/* Based on location BL, create a list of breakpoint conditions to be
2086 passed on to the target. If we have duplicated locations with different
2087 conditions, we will add such conditions to the list. The idea is that the
2088 target will evaluate the list of conditions and will only notify GDB when
2089 one of them is true. */
2090
2091static void
2092build_target_condition_list (struct bp_location *bl)
2093{
2094 struct bp_location **locp = NULL, **loc2p;
2095 int null_condition_or_parse_error = 0;
2096 int modified = bl->needs_update;
2097 struct bp_location *loc;
2098
2099 /* This is only meaningful if the target is
2100 evaluating conditions and if the user has
2101 opted for condition evaluation on the target's
2102 side. */
2103 if (gdb_evaluates_breakpoint_condition_p ()
2104 || !target_supports_evaluation_of_breakpoint_conditions ())
2105 return;
2106
2107 /* Do a first pass to check for locations with no assigned
2108 conditions or conditions that fail to parse to a valid agent expression
2109 bytecode. If any of these happen, then it's no use to send conditions
2110 to the target since this location will always trigger and generate a
2111 response back to GDB. */
2112 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2113 {
2114 loc = (*loc2p);
2115 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2116 {
2117 if (modified)
2118 {
2119 struct agent_expr *aexpr;
2120
2121 /* Re-parse the conditions since something changed. In that
2122 case we already freed the condition bytecodes (see
2123 force_breakpoint_reinsertion). We just
2124 need to parse the condition to bytecodes again. */
2125 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2126 loc->cond_bytecode = aexpr;
2127
2128 /* Check if we managed to parse the conditional expression
2129 correctly. If not, we will not send this condition
2130 to the target. */
2131 if (aexpr)
2132 continue;
2133 }
2134
2135 /* If we have a NULL bytecode expression, it means something
2136 went wrong or we have a null condition expression. */
2137 if (!loc->cond_bytecode)
2138 {
2139 null_condition_or_parse_error = 1;
2140 break;
2141 }
2142 }
2143 }
2144
2145 /* If any of these happened, it means we will have to evaluate the conditions
2146 for the location's address on gdb's side. It is no use keeping bytecodes
2147 for all the other duplicate locations, thus we free all of them here.
2148
2149 This is so we have a finer control over which locations' conditions are
2150 being evaluated by GDB or the remote stub. */
2151 if (null_condition_or_parse_error)
2152 {
2153 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2154 {
2155 loc = (*loc2p);
2156 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2157 {
2158 /* Only go as far as the first NULL bytecode is
2159 located. */
2160 if (!loc->cond_bytecode)
2161 return;
2162
2163 free_agent_expr (loc->cond_bytecode);
2164 loc->cond_bytecode = NULL;
2165 }
2166 }
2167 }
2168
2169 /* No NULL conditions or failed bytecode generation. Build a condition list
2170 for this location's address. */
2171 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2172 {
2173 loc = (*loc2p);
2174 if (loc->cond
2175 && is_breakpoint (loc->owner)
2176 && loc->pspace->num == bl->pspace->num
2177 && loc->owner->enable_state == bp_enabled
2178 && loc->enabled)
2179 /* Add the condition to the vector. This will be used later to send the
2180 conditions to the target. */
2181 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2182 loc->cond_bytecode);
2183 }
2184
2185 return;
2186}
2187
2188/* Parses a command described by string CMD into an agent expression
2189 bytecode suitable for evaluation by the bytecode interpreter.
2190 Return NULL if there was any error during parsing. */
2191
2192static struct agent_expr *
2193parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2194{
2195 struct cleanup *old_cleanups = 0;
2196 struct expression *expr, **argvec;
2197 struct agent_expr *aexpr = NULL;
2198 volatile struct gdb_exception ex;
2199 const char *cmdrest;
2200 const char *format_start, *format_end;
2201 struct format_piece *fpieces;
2202 int nargs;
2203 struct gdbarch *gdbarch = get_current_arch ();
2204
2205 if (!cmd)
2206 return NULL;
2207
2208 cmdrest = cmd;
2209
2210 if (*cmdrest == ',')
2211 ++cmdrest;
2212 cmdrest = skip_spaces_const (cmdrest);
2213
2214 if (*cmdrest++ != '"')
2215 error (_("No format string following the location"));
2216
2217 format_start = cmdrest;
2218
2219 fpieces = parse_format_string (&cmdrest);
2220
2221 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2222
2223 format_end = cmdrest;
2224
2225 if (*cmdrest++ != '"')
2226 error (_("Bad format string, non-terminated '\"'."));
2227
2228 cmdrest = skip_spaces_const (cmdrest);
2229
2230 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2231 error (_("Invalid argument syntax"));
2232
2233 if (*cmdrest == ',')
2234 cmdrest++;
2235 cmdrest = skip_spaces_const (cmdrest);
2236
2237 /* For each argument, make an expression. */
2238
2239 argvec = (struct expression **) alloca (strlen (cmd)
2240 * sizeof (struct expression *));
2241
2242 nargs = 0;
2243 while (*cmdrest != '\0')
2244 {
2245 const char *cmd1;
2246
2247 cmd1 = cmdrest;
2248 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2249 argvec[nargs++] = expr;
2250 cmdrest = cmd1;
2251 if (*cmdrest == ',')
2252 ++cmdrest;
2253 }
2254
2255 /* We don't want to stop processing, so catch any errors
2256 that may show up. */
2257 TRY_CATCH (ex, RETURN_MASK_ERROR)
2258 {
2259 aexpr = gen_printf (scope, gdbarch, 0, 0,
2260 format_start, format_end - format_start,
2261 fpieces, nargs, argvec);
2262 }
2263
2264 do_cleanups (old_cleanups);
2265
2266 if (ex.reason < 0)
2267 {
2268 /* If we got here, it means the command could not be parsed to a valid
2269 bytecode expression and thus can't be evaluated on the target's side.
2270 It's no use iterating through the other commands. */
2271 return NULL;
2272 }
2273
2274 /* We have a valid agent expression, return it. */
2275 return aexpr;
2276}
2277
2278/* Based on location BL, create a list of breakpoint commands to be
2279 passed on to the target. If we have duplicated locations with
2280 different commands, we will add any such to the list. */
2281
2282static void
2283build_target_command_list (struct bp_location *bl)
2284{
2285 struct bp_location **locp = NULL, **loc2p;
2286 int null_command_or_parse_error = 0;
2287 int modified = bl->needs_update;
2288 struct bp_location *loc;
2289
2290 /* For now, limit to agent-style dprintf breakpoints. */
2291 if (bl->owner->type != bp_dprintf
2292 || strcmp (dprintf_style, dprintf_style_agent) != 0)
2293 return;
2294
2295 if (!target_can_run_breakpoint_commands ())
2296 return;
2297
2298 /* Do a first pass to check for locations with no assigned
2299 conditions or conditions that fail to parse to a valid agent expression
2300 bytecode. If any of these happen, then it's no use to send conditions
2301 to the target since this location will always trigger and generate a
2302 response back to GDB. */
2303 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2304 {
2305 loc = (*loc2p);
2306 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2307 {
2308 if (modified)
2309 {
2310 struct agent_expr *aexpr;
2311
2312 /* Re-parse the commands since something changed. In that
2313 case we already freed the command bytecodes (see
2314 force_breakpoint_reinsertion). We just
2315 need to parse the command to bytecodes again. */
2316 aexpr = parse_cmd_to_aexpr (bl->address,
2317 loc->owner->extra_string);
2318 loc->cmd_bytecode = aexpr;
2319
2320 if (!aexpr)
2321 continue;
2322 }
2323
2324 /* If we have a NULL bytecode expression, it means something
2325 went wrong or we have a null command expression. */
2326 if (!loc->cmd_bytecode)
2327 {
2328 null_command_or_parse_error = 1;
2329 break;
2330 }
2331 }
2332 }
2333
2334 /* If anything failed, then we're not doing target-side commands,
2335 and so clean up. */
2336 if (null_command_or_parse_error)
2337 {
2338 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2339 {
2340 loc = (*loc2p);
2341 if (is_breakpoint (loc->owner)
2342 && loc->pspace->num == bl->pspace->num)
2343 {
2344 /* Only go as far as the first NULL bytecode is
2345 located. */
2346 if (loc->cmd_bytecode == NULL)
2347 return;
2348
2349 free_agent_expr (loc->cmd_bytecode);
2350 loc->cmd_bytecode = NULL;
2351 }
2352 }
2353 }
2354
2355 /* No NULL commands or failed bytecode generation. Build a command list
2356 for this location's address. */
2357 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2358 {
2359 loc = (*loc2p);
2360 if (loc->owner->extra_string
2361 && is_breakpoint (loc->owner)
2362 && loc->pspace->num == bl->pspace->num
2363 && loc->owner->enable_state == bp_enabled
2364 && loc->enabled)
2365 /* Add the command to the vector. This will be used later
2366 to send the commands to the target. */
2367 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2368 loc->cmd_bytecode);
2369 }
2370
2371 bl->target_info.persist = 0;
2372 /* Maybe flag this location as persistent. */
2373 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2374 bl->target_info.persist = 1;
2375}
2376
2377/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2378 location. Any error messages are printed to TMP_ERROR_STREAM; and
2379 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2380 Returns 0 for success, 1 if the bp_location type is not supported or
2381 -1 for failure.
2382
2383 NOTE drow/2003-09-09: This routine could be broken down to an
2384 object-style method for each breakpoint or catchpoint type. */
2385static int
2386insert_bp_location (struct bp_location *bl,
2387 struct ui_file *tmp_error_stream,
2388 int *disabled_breaks,
2389 int *hw_breakpoint_error,
2390 int *hw_bp_error_explained_already)
2391{
2392 int val = 0;
2393 char *hw_bp_err_string = NULL;
2394 struct gdb_exception e;
2395
2396 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2397 return 0;
2398
2399 /* Note we don't initialize bl->target_info, as that wipes out
2400 the breakpoint location's shadow_contents if the breakpoint
2401 is still inserted at that location. This in turn breaks
2402 target_read_memory which depends on these buffers when
2403 a memory read is requested at the breakpoint location:
2404 Once the target_info has been wiped, we fail to see that
2405 we have a breakpoint inserted at that address and thus
2406 read the breakpoint instead of returning the data saved in
2407 the breakpoint location's shadow contents. */
2408 bl->target_info.placed_address = bl->address;
2409 bl->target_info.placed_address_space = bl->pspace->aspace;
2410 bl->target_info.length = bl->length;
2411
2412 /* When working with target-side conditions, we must pass all the conditions
2413 for the same breakpoint address down to the target since GDB will not
2414 insert those locations. With a list of breakpoint conditions, the target
2415 can decide when to stop and notify GDB. */
2416
2417 if (is_breakpoint (bl->owner))
2418 {
2419 build_target_condition_list (bl);
2420 build_target_command_list (bl);
2421 /* Reset the modification marker. */
2422 bl->needs_update = 0;
2423 }
2424
2425 if (bl->loc_type == bp_loc_software_breakpoint
2426 || bl->loc_type == bp_loc_hardware_breakpoint)
2427 {
2428 if (bl->owner->type != bp_hardware_breakpoint)
2429 {
2430 /* If the explicitly specified breakpoint type
2431 is not hardware breakpoint, check the memory map to see
2432 if the breakpoint address is in read only memory or not.
2433
2434 Two important cases are:
2435 - location type is not hardware breakpoint, memory
2436 is readonly. We change the type of the location to
2437 hardware breakpoint.
2438 - location type is hardware breakpoint, memory is
2439 read-write. This means we've previously made the
2440 location hardware one, but then the memory map changed,
2441 so we undo.
2442
2443 When breakpoints are removed, remove_breakpoints will use
2444 location types we've just set here, the only possible
2445 problem is that memory map has changed during running
2446 program, but it's not going to work anyway with current
2447 gdb. */
2448 struct mem_region *mr
2449 = lookup_mem_region (bl->target_info.placed_address);
2450
2451 if (mr)
2452 {
2453 if (automatic_hardware_breakpoints)
2454 {
2455 enum bp_loc_type new_type;
2456
2457 if (mr->attrib.mode != MEM_RW)
2458 new_type = bp_loc_hardware_breakpoint;
2459 else
2460 new_type = bp_loc_software_breakpoint;
2461
2462 if (new_type != bl->loc_type)
2463 {
2464 static int said = 0;
2465
2466 bl->loc_type = new_type;
2467 if (!said)
2468 {
2469 fprintf_filtered (gdb_stdout,
2470 _("Note: automatically using "
2471 "hardware breakpoints for "
2472 "read-only addresses.\n"));
2473 said = 1;
2474 }
2475 }
2476 }
2477 else if (bl->loc_type == bp_loc_software_breakpoint
2478 && mr->attrib.mode != MEM_RW)
2479 warning (_("cannot set software breakpoint "
2480 "at readonly address %s"),
2481 paddress (bl->gdbarch, bl->address));
2482 }
2483 }
2484
2485 /* First check to see if we have to handle an overlay. */
2486 if (overlay_debugging == ovly_off
2487 || bl->section == NULL
2488 || !(section_is_overlay (bl->section)))
2489 {
2490 /* No overlay handling: just set the breakpoint. */
2491 TRY_CATCH (e, RETURN_MASK_ALL)
2492 {
2493 val = bl->owner->ops->insert_location (bl);
2494 }
2495 if (e.reason < 0)
2496 {
2497 val = 1;
2498 hw_bp_err_string = (char *) e.message;
2499 }
2500 }
2501 else
2502 {
2503 /* This breakpoint is in an overlay section.
2504 Shall we set a breakpoint at the LMA? */
2505 if (!overlay_events_enabled)
2506 {
2507 /* Yes -- overlay event support is not active,
2508 so we must try to set a breakpoint at the LMA.
2509 This will not work for a hardware breakpoint. */
2510 if (bl->loc_type == bp_loc_hardware_breakpoint)
2511 warning (_("hardware breakpoint %d not supported in overlay!"),
2512 bl->owner->number);
2513 else
2514 {
2515 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2516 bl->section);
2517 /* Set a software (trap) breakpoint at the LMA. */
2518 bl->overlay_target_info = bl->target_info;
2519 bl->overlay_target_info.placed_address = addr;
2520 val = target_insert_breakpoint (bl->gdbarch,
2521 &bl->overlay_target_info);
2522 if (val != 0)
2523 fprintf_unfiltered (tmp_error_stream,
2524 "Overlay breakpoint %d "
2525 "failed: in ROM?\n",
2526 bl->owner->number);
2527 }
2528 }
2529 /* Shall we set a breakpoint at the VMA? */
2530 if (section_is_mapped (bl->section))
2531 {
2532 /* Yes. This overlay section is mapped into memory. */
2533 TRY_CATCH (e, RETURN_MASK_ALL)
2534 {
2535 val = bl->owner->ops->insert_location (bl);
2536 }
2537 if (e.reason < 0)
2538 {
2539 val = 1;
2540 hw_bp_err_string = (char *) e.message;
2541 }
2542 }
2543 else
2544 {
2545 /* No. This breakpoint will not be inserted.
2546 No error, but do not mark the bp as 'inserted'. */
2547 return 0;
2548 }
2549 }
2550
2551 if (val)
2552 {
2553 /* Can't set the breakpoint. */
2554 if (solib_name_from_address (bl->pspace, bl->address))
2555 {
2556 /* See also: disable_breakpoints_in_shlibs. */
2557 val = 0;
2558 bl->shlib_disabled = 1;
2559 observer_notify_breakpoint_modified (bl->owner);
2560 if (!*disabled_breaks)
2561 {
2562 fprintf_unfiltered (tmp_error_stream,
2563 "Cannot insert breakpoint %d.\n",
2564 bl->owner->number);
2565 fprintf_unfiltered (tmp_error_stream,
2566 "Temporarily disabling shared "
2567 "library breakpoints:\n");
2568 }
2569 *disabled_breaks = 1;
2570 fprintf_unfiltered (tmp_error_stream,
2571 "breakpoint #%d\n", bl->owner->number);
2572 }
2573 else
2574 {
2575 if (bl->loc_type == bp_loc_hardware_breakpoint)
2576 {
2577 *hw_breakpoint_error = 1;
2578 *hw_bp_error_explained_already = hw_bp_err_string != NULL;
2579 fprintf_unfiltered (tmp_error_stream,
2580 "Cannot insert hardware breakpoint %d%s",
2581 bl->owner->number, hw_bp_err_string ? ":" : ".\n");
2582 if (hw_bp_err_string)
2583 fprintf_unfiltered (tmp_error_stream, "%s.\n", hw_bp_err_string);
2584 }
2585 else
2586 {
2587 char *message = memory_error_message (TARGET_XFER_E_IO,
2588 bl->gdbarch, bl->address);
2589 struct cleanup *old_chain = make_cleanup (xfree, message);
2590
2591 fprintf_unfiltered (tmp_error_stream,
2592 "Cannot insert breakpoint %d.\n"
2593 "%s\n",
2594 bl->owner->number, message);
2595
2596 do_cleanups (old_chain);
2597 }
2598
2599 }
2600 }
2601 else
2602 bl->inserted = 1;
2603
2604 return val;
2605 }
2606
2607 else if (bl->loc_type == bp_loc_hardware_watchpoint
2608 /* NOTE drow/2003-09-08: This state only exists for removing
2609 watchpoints. It's not clear that it's necessary... */
2610 && bl->owner->disposition != disp_del_at_next_stop)
2611 {
2612 gdb_assert (bl->owner->ops != NULL
2613 && bl->owner->ops->insert_location != NULL);
2614
2615 val = bl->owner->ops->insert_location (bl);
2616
2617 /* If trying to set a read-watchpoint, and it turns out it's not
2618 supported, try emulating one with an access watchpoint. */
2619 if (val == 1 && bl->watchpoint_type == hw_read)
2620 {
2621 struct bp_location *loc, **loc_temp;
2622
2623 /* But don't try to insert it, if there's already another
2624 hw_access location that would be considered a duplicate
2625 of this one. */
2626 ALL_BP_LOCATIONS (loc, loc_temp)
2627 if (loc != bl
2628 && loc->watchpoint_type == hw_access
2629 && watchpoint_locations_match (bl, loc))
2630 {
2631 bl->duplicate = 1;
2632 bl->inserted = 1;
2633 bl->target_info = loc->target_info;
2634 bl->watchpoint_type = hw_access;
2635 val = 0;
2636 break;
2637 }
2638
2639 if (val == 1)
2640 {
2641 bl->watchpoint_type = hw_access;
2642 val = bl->owner->ops->insert_location (bl);
2643
2644 if (val)
2645 /* Back to the original value. */
2646 bl->watchpoint_type = hw_read;
2647 }
2648 }
2649
2650 bl->inserted = (val == 0);
2651 }
2652
2653 else if (bl->owner->type == bp_catchpoint)
2654 {
2655 gdb_assert (bl->owner->ops != NULL
2656 && bl->owner->ops->insert_location != NULL);
2657
2658 val = bl->owner->ops->insert_location (bl);
2659 if (val)
2660 {
2661 bl->owner->enable_state = bp_disabled;
2662
2663 if (val == 1)
2664 warning (_("\
2665Error inserting catchpoint %d: Your system does not support this type\n\
2666of catchpoint."), bl->owner->number);
2667 else
2668 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2669 }
2670
2671 bl->inserted = (val == 0);
2672
2673 /* We've already printed an error message if there was a problem
2674 inserting this catchpoint, and we've disabled the catchpoint,
2675 so just return success. */
2676 return 0;
2677 }
2678
2679 return 0;
2680}
2681
2682/* This function is called when program space PSPACE is about to be
2683 deleted. It takes care of updating breakpoints to not reference
2684 PSPACE anymore. */
2685
2686void
2687breakpoint_program_space_exit (struct program_space *pspace)
2688{
2689 struct breakpoint *b, *b_temp;
2690 struct bp_location *loc, **loc_temp;
2691
2692 /* Remove any breakpoint that was set through this program space. */
2693 ALL_BREAKPOINTS_SAFE (b, b_temp)
2694 {
2695 if (b->pspace == pspace)
2696 delete_breakpoint (b);
2697 }
2698
2699 /* Breakpoints set through other program spaces could have locations
2700 bound to PSPACE as well. Remove those. */
2701 ALL_BP_LOCATIONS (loc, loc_temp)
2702 {
2703 struct bp_location *tmp;
2704
2705 if (loc->pspace == pspace)
2706 {
2707 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2708 if (loc->owner->loc == loc)
2709 loc->owner->loc = loc->next;
2710 else
2711 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2712 if (tmp->next == loc)
2713 {
2714 tmp->next = loc->next;
2715 break;
2716 }
2717 }
2718 }
2719
2720 /* Now update the global location list to permanently delete the
2721 removed locations above. */
2722 update_global_location_list (0);
2723}
2724
2725/* Make sure all breakpoints are inserted in inferior.
2726 Throws exception on any error.
2727 A breakpoint that is already inserted won't be inserted
2728 again, so calling this function twice is safe. */
2729void
2730insert_breakpoints (void)
2731{
2732 struct breakpoint *bpt;
2733
2734 ALL_BREAKPOINTS (bpt)
2735 if (is_hardware_watchpoint (bpt))
2736 {
2737 struct watchpoint *w = (struct watchpoint *) bpt;
2738
2739 update_watchpoint (w, 0 /* don't reparse. */);
2740 }
2741
2742 update_global_location_list (1);
2743
2744 /* update_global_location_list does not insert breakpoints when
2745 always_inserted_mode is not enabled. Explicitly insert them
2746 now. */
2747 if (!breakpoints_always_inserted_mode ())
2748 insert_breakpoint_locations ();
2749}
2750
2751/* Invoke CALLBACK for each of bp_location. */
2752
2753void
2754iterate_over_bp_locations (walk_bp_location_callback callback)
2755{
2756 struct bp_location *loc, **loc_tmp;
2757
2758 ALL_BP_LOCATIONS (loc, loc_tmp)
2759 {
2760 callback (loc, NULL);
2761 }
2762}
2763
2764/* This is used when we need to synch breakpoint conditions between GDB and the
2765 target. It is the case with deleting and disabling of breakpoints when using
2766 always-inserted mode. */
2767
2768static void
2769update_inserted_breakpoint_locations (void)
2770{
2771 struct bp_location *bl, **blp_tmp;
2772 int error_flag = 0;
2773 int val = 0;
2774 int disabled_breaks = 0;
2775 int hw_breakpoint_error = 0;
2776 int hw_bp_details_reported = 0;
2777
2778 struct ui_file *tmp_error_stream = mem_fileopen ();
2779 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2780
2781 /* Explicitly mark the warning -- this will only be printed if
2782 there was an error. */
2783 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2784
2785 save_current_space_and_thread ();
2786
2787 ALL_BP_LOCATIONS (bl, blp_tmp)
2788 {
2789 /* We only want to update software breakpoints and hardware
2790 breakpoints. */
2791 if (!is_breakpoint (bl->owner))
2792 continue;
2793
2794 /* We only want to update locations that are already inserted
2795 and need updating. This is to avoid unwanted insertion during
2796 deletion of breakpoints. */
2797 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2798 continue;
2799
2800 switch_to_program_space_and_thread (bl->pspace);
2801
2802 /* For targets that support global breakpoints, there's no need
2803 to select an inferior to insert breakpoint to. In fact, even
2804 if we aren't attached to any process yet, we should still
2805 insert breakpoints. */
2806 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2807 && ptid_equal (inferior_ptid, null_ptid))
2808 continue;
2809
2810 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2811 &hw_breakpoint_error, &hw_bp_details_reported);
2812 if (val)
2813 error_flag = val;
2814 }
2815
2816 if (error_flag)
2817 {
2818 target_terminal_ours_for_output ();
2819 error_stream (tmp_error_stream);
2820 }
2821
2822 do_cleanups (cleanups);
2823}
2824
2825/* Used when starting or continuing the program. */
2826
2827static void
2828insert_breakpoint_locations (void)
2829{
2830 struct breakpoint *bpt;
2831 struct bp_location *bl, **blp_tmp;
2832 int error_flag = 0;
2833 int val = 0;
2834 int disabled_breaks = 0;
2835 int hw_breakpoint_error = 0;
2836 int hw_bp_error_explained_already = 0;
2837
2838 struct ui_file *tmp_error_stream = mem_fileopen ();
2839 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2840
2841 /* Explicitly mark the warning -- this will only be printed if
2842 there was an error. */
2843 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2844
2845 save_current_space_and_thread ();
2846
2847 ALL_BP_LOCATIONS (bl, blp_tmp)
2848 {
2849 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2850 continue;
2851
2852 /* There is no point inserting thread-specific breakpoints if
2853 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2854 has BL->OWNER always non-NULL. */
2855 if (bl->owner->thread != -1
2856 && !valid_thread_id (bl->owner->thread))
2857 continue;
2858
2859 switch_to_program_space_and_thread (bl->pspace);
2860
2861 /* For targets that support global breakpoints, there's no need
2862 to select an inferior to insert breakpoint to. In fact, even
2863 if we aren't attached to any process yet, we should still
2864 insert breakpoints. */
2865 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2866 && ptid_equal (inferior_ptid, null_ptid))
2867 continue;
2868
2869 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2870 &hw_breakpoint_error, &hw_bp_error_explained_already);
2871 if (val)
2872 error_flag = val;
2873 }
2874
2875 /* If we failed to insert all locations of a watchpoint, remove
2876 them, as half-inserted watchpoint is of limited use. */
2877 ALL_BREAKPOINTS (bpt)
2878 {
2879 int some_failed = 0;
2880 struct bp_location *loc;
2881
2882 if (!is_hardware_watchpoint (bpt))
2883 continue;
2884
2885 if (!breakpoint_enabled (bpt))
2886 continue;
2887
2888 if (bpt->disposition == disp_del_at_next_stop)
2889 continue;
2890
2891 for (loc = bpt->loc; loc; loc = loc->next)
2892 if (!loc->inserted && should_be_inserted (loc))
2893 {
2894 some_failed = 1;
2895 break;
2896 }
2897 if (some_failed)
2898 {
2899 for (loc = bpt->loc; loc; loc = loc->next)
2900 if (loc->inserted)
2901 remove_breakpoint (loc, mark_uninserted);
2902
2903 hw_breakpoint_error = 1;
2904 fprintf_unfiltered (tmp_error_stream,
2905 "Could not insert hardware watchpoint %d.\n",
2906 bpt->number);
2907 error_flag = -1;
2908 }
2909 }
2910
2911 if (error_flag)
2912 {
2913 /* If a hardware breakpoint or watchpoint was inserted, add a
2914 message about possibly exhausted resources. */
2915 if (hw_breakpoint_error && !hw_bp_error_explained_already)
2916 {
2917 fprintf_unfiltered (tmp_error_stream,
2918 "Could not insert hardware breakpoints:\n\
2919You may have requested too many hardware breakpoints/watchpoints.\n");
2920 }
2921 target_terminal_ours_for_output ();
2922 error_stream (tmp_error_stream);
2923 }
2924
2925 do_cleanups (cleanups);
2926}
2927
2928/* Used when the program stops.
2929 Returns zero if successful, or non-zero if there was a problem
2930 removing a breakpoint location. */
2931
2932int
2933remove_breakpoints (void)
2934{
2935 struct bp_location *bl, **blp_tmp;
2936 int val = 0;
2937
2938 ALL_BP_LOCATIONS (bl, blp_tmp)
2939 {
2940 if (bl->inserted && !is_tracepoint (bl->owner))
2941 val |= remove_breakpoint (bl, mark_uninserted);
2942 }
2943 return val;
2944}
2945
2946/* When a thread exits, remove breakpoints that are related to
2947 that thread. */
2948
2949static void
2950remove_threaded_breakpoints (struct thread_info *tp, int silent)
2951{
2952 struct breakpoint *b, *b_tmp;
2953
2954 ALL_BREAKPOINTS_SAFE (b, b_tmp)
2955 {
2956 if (b->thread == tp->num && user_breakpoint_p (b))
2957 {
2958 b->disposition = disp_del_at_next_stop;
2959
2960 printf_filtered (_("\
2961Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
2962 b->number, tp->num);
2963
2964 /* Hide it from the user. */
2965 b->number = 0;
2966 }
2967 }
2968}
2969
2970/* Remove breakpoints of process PID. */
2971
2972int
2973remove_breakpoints_pid (int pid)
2974{
2975 struct bp_location *bl, **blp_tmp;
2976 int val;
2977 struct inferior *inf = find_inferior_pid (pid);
2978
2979 ALL_BP_LOCATIONS (bl, blp_tmp)
2980 {
2981 if (bl->pspace != inf->pspace)
2982 continue;
2983
2984 if (bl->owner->type == bp_dprintf)
2985 continue;
2986
2987 if (bl->inserted)
2988 {
2989 val = remove_breakpoint (bl, mark_uninserted);
2990 if (val != 0)
2991 return val;
2992 }
2993 }
2994 return 0;
2995}
2996
2997int
2998reattach_breakpoints (int pid)
2999{
3000 struct cleanup *old_chain;
3001 struct bp_location *bl, **blp_tmp;
3002 int val;
3003 struct ui_file *tmp_error_stream;
3004 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3005 struct inferior *inf;
3006 struct thread_info *tp;
3007
3008 tp = any_live_thread_of_process (pid);
3009 if (tp == NULL)
3010 return 1;
3011
3012 inf = find_inferior_pid (pid);
3013 old_chain = save_inferior_ptid ();
3014
3015 inferior_ptid = tp->ptid;
3016
3017 tmp_error_stream = mem_fileopen ();
3018 make_cleanup_ui_file_delete (tmp_error_stream);
3019
3020 ALL_BP_LOCATIONS (bl, blp_tmp)
3021 {
3022 if (bl->pspace != inf->pspace)
3023 continue;
3024
3025 if (bl->inserted)
3026 {
3027 bl->inserted = 0;
3028 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3029 if (val != 0)
3030 {
3031 do_cleanups (old_chain);
3032 return val;
3033 }
3034 }
3035 }
3036 do_cleanups (old_chain);
3037 return 0;
3038}
3039
3040static int internal_breakpoint_number = -1;
3041
3042/* Set the breakpoint number of B, depending on the value of INTERNAL.
3043 If INTERNAL is non-zero, the breakpoint number will be populated
3044 from internal_breakpoint_number and that variable decremented.
3045 Otherwise the breakpoint number will be populated from
3046 breakpoint_count and that value incremented. Internal breakpoints
3047 do not set the internal var bpnum. */
3048static void
3049set_breakpoint_number (int internal, struct breakpoint *b)
3050{
3051 if (internal)
3052 b->number = internal_breakpoint_number--;
3053 else
3054 {
3055 set_breakpoint_count (breakpoint_count + 1);
3056 b->number = breakpoint_count;
3057 }
3058}
3059
3060static struct breakpoint *
3061create_internal_breakpoint (struct gdbarch *gdbarch,
3062 CORE_ADDR address, enum bptype type,
3063 const struct breakpoint_ops *ops)
3064{
3065 struct symtab_and_line sal;
3066 struct breakpoint *b;
3067
3068 init_sal (&sal); /* Initialize to zeroes. */
3069
3070 sal.pc = address;
3071 sal.section = find_pc_overlay (sal.pc);
3072 sal.pspace = current_program_space;
3073
3074 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3075 b->number = internal_breakpoint_number--;
3076 b->disposition = disp_donttouch;
3077
3078 return b;
3079}
3080
3081static const char *const longjmp_names[] =
3082 {
3083 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3084 };
3085#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3086
3087/* Per-objfile data private to breakpoint.c. */
3088struct breakpoint_objfile_data
3089{
3090 /* Minimal symbol for "_ovly_debug_event" (if any). */
3091 struct minimal_symbol *overlay_msym;
3092
3093 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3094 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
3095
3096 /* True if we have looked for longjmp probes. */
3097 int longjmp_searched;
3098
3099 /* SystemTap probe points for longjmp (if any). */
3100 VEC (probe_p) *longjmp_probes;
3101
3102 /* Minimal symbol for "std::terminate()" (if any). */
3103 struct minimal_symbol *terminate_msym;
3104
3105 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3106 struct minimal_symbol *exception_msym;
3107
3108 /* True if we have looked for exception probes. */
3109 int exception_searched;
3110
3111 /* SystemTap probe points for unwinding (if any). */
3112 VEC (probe_p) *exception_probes;
3113};
3114
3115static const struct objfile_data *breakpoint_objfile_key;
3116
3117/* Minimal symbol not found sentinel. */
3118static struct minimal_symbol msym_not_found;
3119
3120/* Returns TRUE if MSYM point to the "not found" sentinel. */
3121
3122static int
3123msym_not_found_p (const struct minimal_symbol *msym)
3124{
3125 return msym == &msym_not_found;
3126}
3127
3128/* Return per-objfile data needed by breakpoint.c.
3129 Allocate the data if necessary. */
3130
3131static struct breakpoint_objfile_data *
3132get_breakpoint_objfile_data (struct objfile *objfile)
3133{
3134 struct breakpoint_objfile_data *bp_objfile_data;
3135
3136 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3137 if (bp_objfile_data == NULL)
3138 {
3139 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3140 sizeof (*bp_objfile_data));
3141
3142 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3143 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3144 }
3145 return bp_objfile_data;
3146}
3147
3148static void
3149free_breakpoint_probes (struct objfile *obj, void *data)
3150{
3151 struct breakpoint_objfile_data *bp_objfile_data = data;
3152
3153 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3154 VEC_free (probe_p, bp_objfile_data->exception_probes);
3155}
3156
3157static void
3158create_overlay_event_breakpoint (void)
3159{
3160 struct objfile *objfile;
3161 const char *const func_name = "_ovly_debug_event";
3162
3163 ALL_OBJFILES (objfile)
3164 {
3165 struct breakpoint *b;
3166 struct breakpoint_objfile_data *bp_objfile_data;
3167 CORE_ADDR addr;
3168
3169 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3170
3171 if (msym_not_found_p (bp_objfile_data->overlay_msym))
3172 continue;
3173
3174 if (bp_objfile_data->overlay_msym == NULL)
3175 {
3176 struct minimal_symbol *m;
3177
3178 m = lookup_minimal_symbol_text (func_name, objfile);
3179 if (m == NULL)
3180 {
3181 /* Avoid future lookups in this objfile. */
3182 bp_objfile_data->overlay_msym = &msym_not_found;
3183 continue;
3184 }
3185 bp_objfile_data->overlay_msym = m;
3186 }
3187
3188 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3189 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3190 bp_overlay_event,
3191 &internal_breakpoint_ops);
3192 b->addr_string = xstrdup (func_name);
3193
3194 if (overlay_debugging == ovly_auto)
3195 {
3196 b->enable_state = bp_enabled;
3197 overlay_events_enabled = 1;
3198 }
3199 else
3200 {
3201 b->enable_state = bp_disabled;
3202 overlay_events_enabled = 0;
3203 }
3204 }
3205 update_global_location_list (1);
3206}
3207
3208static void
3209create_longjmp_master_breakpoint (void)
3210{
3211 struct program_space *pspace;
3212 struct cleanup *old_chain;
3213
3214 old_chain = save_current_program_space ();
3215
3216 ALL_PSPACES (pspace)
3217 {
3218 struct objfile *objfile;
3219
3220 set_current_program_space (pspace);
3221
3222 ALL_OBJFILES (objfile)
3223 {
3224 int i;
3225 struct gdbarch *gdbarch;
3226 struct breakpoint_objfile_data *bp_objfile_data;
3227
3228 gdbarch = get_objfile_arch (objfile);
3229
3230 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3231
3232 if (!bp_objfile_data->longjmp_searched)
3233 {
3234 VEC (probe_p) *ret;
3235
3236 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3237 if (ret != NULL)
3238 {
3239 /* We are only interested in checking one element. */
3240 struct probe *p = VEC_index (probe_p, ret, 0);
3241
3242 if (!can_evaluate_probe_arguments (p))
3243 {
3244 /* We cannot use the probe interface here, because it does
3245 not know how to evaluate arguments. */
3246 VEC_free (probe_p, ret);
3247 ret = NULL;
3248 }
3249 }
3250 bp_objfile_data->longjmp_probes = ret;
3251 bp_objfile_data->longjmp_searched = 1;
3252 }
3253
3254 if (bp_objfile_data->longjmp_probes != NULL)
3255 {
3256 int i;
3257 struct probe *probe;
3258 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3259
3260 for (i = 0;
3261 VEC_iterate (probe_p,
3262 bp_objfile_data->longjmp_probes,
3263 i, probe);
3264 ++i)
3265 {
3266 struct breakpoint *b;
3267
3268 b = create_internal_breakpoint (gdbarch, probe->address,
3269 bp_longjmp_master,
3270 &internal_breakpoint_ops);
3271 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3272 b->enable_state = bp_disabled;
3273 }
3274
3275 continue;
3276 }
3277
3278 if (!gdbarch_get_longjmp_target_p (gdbarch))
3279 continue;
3280
3281 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3282 {
3283 struct breakpoint *b;
3284 const char *func_name;
3285 CORE_ADDR addr;
3286
3287 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
3288 continue;
3289
3290 func_name = longjmp_names[i];
3291 if (bp_objfile_data->longjmp_msym[i] == NULL)
3292 {
3293 struct minimal_symbol *m;
3294
3295 m = lookup_minimal_symbol_text (func_name, objfile);
3296 if (m == NULL)
3297 {
3298 /* Prevent future lookups in this objfile. */
3299 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
3300 continue;
3301 }
3302 bp_objfile_data->longjmp_msym[i] = m;
3303 }
3304
3305 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3306 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3307 &internal_breakpoint_ops);
3308 b->addr_string = xstrdup (func_name);
3309 b->enable_state = bp_disabled;
3310 }
3311 }
3312 }
3313 update_global_location_list (1);
3314
3315 do_cleanups (old_chain);
3316}
3317
3318/* Create a master std::terminate breakpoint. */
3319static void
3320create_std_terminate_master_breakpoint (void)
3321{
3322 struct program_space *pspace;
3323 struct cleanup *old_chain;
3324 const char *const func_name = "std::terminate()";
3325
3326 old_chain = save_current_program_space ();
3327
3328 ALL_PSPACES (pspace)
3329 {
3330 struct objfile *objfile;
3331 CORE_ADDR addr;
3332
3333 set_current_program_space (pspace);
3334
3335 ALL_OBJFILES (objfile)
3336 {
3337 struct breakpoint *b;
3338 struct breakpoint_objfile_data *bp_objfile_data;
3339
3340 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3341
3342 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3343 continue;
3344
3345 if (bp_objfile_data->terminate_msym == NULL)
3346 {
3347 struct minimal_symbol *m;
3348
3349 m = lookup_minimal_symbol (func_name, NULL, objfile);
3350 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3351 && MSYMBOL_TYPE (m) != mst_file_text))
3352 {
3353 /* Prevent future lookups in this objfile. */
3354 bp_objfile_data->terminate_msym = &msym_not_found;
3355 continue;
3356 }
3357 bp_objfile_data->terminate_msym = m;
3358 }
3359
3360 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3361 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3362 bp_std_terminate_master,
3363 &internal_breakpoint_ops);
3364 b->addr_string = xstrdup (func_name);
3365 b->enable_state = bp_disabled;
3366 }
3367 }
3368
3369 update_global_location_list (1);
3370
3371 do_cleanups (old_chain);
3372}
3373
3374/* Install a master breakpoint on the unwinder's debug hook. */
3375
3376static void
3377create_exception_master_breakpoint (void)
3378{
3379 struct objfile *objfile;
3380 const char *const func_name = "_Unwind_DebugHook";
3381
3382 ALL_OBJFILES (objfile)
3383 {
3384 struct breakpoint *b;
3385 struct gdbarch *gdbarch;
3386 struct breakpoint_objfile_data *bp_objfile_data;
3387 CORE_ADDR addr;
3388
3389 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3390
3391 /* We prefer the SystemTap probe point if it exists. */
3392 if (!bp_objfile_data->exception_searched)
3393 {
3394 VEC (probe_p) *ret;
3395
3396 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3397
3398 if (ret != NULL)
3399 {
3400 /* We are only interested in checking one element. */
3401 struct probe *p = VEC_index (probe_p, ret, 0);
3402
3403 if (!can_evaluate_probe_arguments (p))
3404 {
3405 /* We cannot use the probe interface here, because it does
3406 not know how to evaluate arguments. */
3407 VEC_free (probe_p, ret);
3408 ret = NULL;
3409 }
3410 }
3411 bp_objfile_data->exception_probes = ret;
3412 bp_objfile_data->exception_searched = 1;
3413 }
3414
3415 if (bp_objfile_data->exception_probes != NULL)
3416 {
3417 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3418 int i;
3419 struct probe *probe;
3420
3421 for (i = 0;
3422 VEC_iterate (probe_p,
3423 bp_objfile_data->exception_probes,
3424 i, probe);
3425 ++i)
3426 {
3427 struct breakpoint *b;
3428
3429 b = create_internal_breakpoint (gdbarch, probe->address,
3430 bp_exception_master,
3431 &internal_breakpoint_ops);
3432 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3433 b->enable_state = bp_disabled;
3434 }
3435
3436 continue;
3437 }
3438
3439 /* Otherwise, try the hook function. */
3440
3441 if (msym_not_found_p (bp_objfile_data->exception_msym))
3442 continue;
3443
3444 gdbarch = get_objfile_arch (objfile);
3445
3446 if (bp_objfile_data->exception_msym == NULL)
3447 {
3448 struct minimal_symbol *debug_hook;
3449
3450 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3451 if (debug_hook == NULL)
3452 {
3453 bp_objfile_data->exception_msym = &msym_not_found;
3454 continue;
3455 }
3456
3457 bp_objfile_data->exception_msym = debug_hook;
3458 }
3459
3460 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3461 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3462 &current_target);
3463 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3464 &internal_breakpoint_ops);
3465 b->addr_string = xstrdup (func_name);
3466 b->enable_state = bp_disabled;
3467 }
3468
3469 update_global_location_list (1);
3470}
3471
3472void
3473update_breakpoints_after_exec (void)
3474{
3475 struct breakpoint *b, *b_tmp;
3476 struct bp_location *bploc, **bplocp_tmp;
3477
3478 /* We're about to delete breakpoints from GDB's lists. If the
3479 INSERTED flag is true, GDB will try to lift the breakpoints by
3480 writing the breakpoints' "shadow contents" back into memory. The
3481 "shadow contents" are NOT valid after an exec, so GDB should not
3482 do that. Instead, the target is responsible from marking
3483 breakpoints out as soon as it detects an exec. We don't do that
3484 here instead, because there may be other attempts to delete
3485 breakpoints after detecting an exec and before reaching here. */
3486 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3487 if (bploc->pspace == current_program_space)
3488 gdb_assert (!bploc->inserted);
3489
3490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3491 {
3492 if (b->pspace != current_program_space)
3493 continue;
3494
3495 /* Solib breakpoints must be explicitly reset after an exec(). */
3496 if (b->type == bp_shlib_event)
3497 {
3498 delete_breakpoint (b);
3499 continue;
3500 }
3501
3502 /* JIT breakpoints must be explicitly reset after an exec(). */
3503 if (b->type == bp_jit_event)
3504 {
3505 delete_breakpoint (b);
3506 continue;
3507 }
3508
3509 /* Thread event breakpoints must be set anew after an exec(),
3510 as must overlay event and longjmp master breakpoints. */
3511 if (b->type == bp_thread_event || b->type == bp_overlay_event
3512 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3513 || b->type == bp_exception_master)
3514 {
3515 delete_breakpoint (b);
3516 continue;
3517 }
3518
3519 /* Step-resume breakpoints are meaningless after an exec(). */
3520 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3521 {
3522 delete_breakpoint (b);
3523 continue;
3524 }
3525
3526 /* Longjmp and longjmp-resume breakpoints are also meaningless
3527 after an exec. */
3528 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3529 || b->type == bp_longjmp_call_dummy
3530 || b->type == bp_exception || b->type == bp_exception_resume)
3531 {
3532 delete_breakpoint (b);
3533 continue;
3534 }
3535
3536 if (b->type == bp_catchpoint)
3537 {
3538 /* For now, none of the bp_catchpoint breakpoints need to
3539 do anything at this point. In the future, if some of
3540 the catchpoints need to something, we will need to add
3541 a new method, and call this method from here. */
3542 continue;
3543 }
3544
3545 /* bp_finish is a special case. The only way we ought to be able
3546 to see one of these when an exec() has happened, is if the user
3547 caught a vfork, and then said "finish". Ordinarily a finish just
3548 carries them to the call-site of the current callee, by setting
3549 a temporary bp there and resuming. But in this case, the finish
3550 will carry them entirely through the vfork & exec.
3551
3552 We don't want to allow a bp_finish to remain inserted now. But
3553 we can't safely delete it, 'cause finish_command has a handle to
3554 the bp on a bpstat, and will later want to delete it. There's a
3555 chance (and I've seen it happen) that if we delete the bp_finish
3556 here, that its storage will get reused by the time finish_command
3557 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3558 We really must allow finish_command to delete a bp_finish.
3559
3560 In the absence of a general solution for the "how do we know
3561 it's safe to delete something others may have handles to?"
3562 problem, what we'll do here is just uninsert the bp_finish, and
3563 let finish_command delete it.
3564
3565 (We know the bp_finish is "doomed" in the sense that it's
3566 momentary, and will be deleted as soon as finish_command sees
3567 the inferior stopped. So it doesn't matter that the bp's
3568 address is probably bogus in the new a.out, unlike e.g., the
3569 solib breakpoints.) */
3570
3571 if (b->type == bp_finish)
3572 {
3573 continue;
3574 }
3575
3576 /* Without a symbolic address, we have little hope of the
3577 pre-exec() address meaning the same thing in the post-exec()
3578 a.out. */
3579 if (b->addr_string == NULL)
3580 {
3581 delete_breakpoint (b);
3582 continue;
3583 }
3584 }
3585 /* FIXME what about longjmp breakpoints? Re-create them here? */
3586 create_overlay_event_breakpoint ();
3587 create_longjmp_master_breakpoint ();
3588 create_std_terminate_master_breakpoint ();
3589 create_exception_master_breakpoint ();
3590}
3591
3592int
3593detach_breakpoints (ptid_t ptid)
3594{
3595 struct bp_location *bl, **blp_tmp;
3596 int val = 0;
3597 struct cleanup *old_chain = save_inferior_ptid ();
3598 struct inferior *inf = current_inferior ();
3599
3600 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3601 error (_("Cannot detach breakpoints of inferior_ptid"));
3602
3603 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3604 inferior_ptid = ptid;
3605 ALL_BP_LOCATIONS (bl, blp_tmp)
3606 {
3607 if (bl->pspace != inf->pspace)
3608 continue;
3609
3610 /* This function must physically remove breakpoints locations
3611 from the specified ptid, without modifying the breakpoint
3612 package's state. Locations of type bp_loc_other are only
3613 maintained at GDB side. So, there is no need to remove
3614 these bp_loc_other locations. Moreover, removing these
3615 would modify the breakpoint package's state. */
3616 if (bl->loc_type == bp_loc_other)
3617 continue;
3618
3619 if (bl->inserted)
3620 val |= remove_breakpoint_1 (bl, mark_inserted);
3621 }
3622
3623 /* Detach single-step breakpoints as well. */
3624 detach_single_step_breakpoints ();
3625
3626 do_cleanups (old_chain);
3627 return val;
3628}
3629
3630/* Remove the breakpoint location BL from the current address space.
3631 Note that this is used to detach breakpoints from a child fork.
3632 When we get here, the child isn't in the inferior list, and neither
3633 do we have objects to represent its address space --- we should
3634 *not* look at bl->pspace->aspace here. */
3635
3636static int
3637remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3638{
3639 int val;
3640
3641 /* BL is never in moribund_locations by our callers. */
3642 gdb_assert (bl->owner != NULL);
3643
3644 if (bl->owner->enable_state == bp_permanent)
3645 /* Permanent breakpoints cannot be inserted or removed. */
3646 return 0;
3647
3648 /* The type of none suggests that owner is actually deleted.
3649 This should not ever happen. */
3650 gdb_assert (bl->owner->type != bp_none);
3651
3652 if (bl->loc_type == bp_loc_software_breakpoint
3653 || bl->loc_type == bp_loc_hardware_breakpoint)
3654 {
3655 /* "Normal" instruction breakpoint: either the standard
3656 trap-instruction bp (bp_breakpoint), or a
3657 bp_hardware_breakpoint. */
3658
3659 /* First check to see if we have to handle an overlay. */
3660 if (overlay_debugging == ovly_off
3661 || bl->section == NULL
3662 || !(section_is_overlay (bl->section)))
3663 {
3664 /* No overlay handling: just remove the breakpoint. */
3665 val = bl->owner->ops->remove_location (bl);
3666 }
3667 else
3668 {
3669 /* This breakpoint is in an overlay section.
3670 Did we set a breakpoint at the LMA? */
3671 if (!overlay_events_enabled)
3672 {
3673 /* Yes -- overlay event support is not active, so we
3674 should have set a breakpoint at the LMA. Remove it.
3675 */
3676 /* Ignore any failures: if the LMA is in ROM, we will
3677 have already warned when we failed to insert it. */
3678 if (bl->loc_type == bp_loc_hardware_breakpoint)
3679 target_remove_hw_breakpoint (bl->gdbarch,
3680 &bl->overlay_target_info);
3681 else
3682 target_remove_breakpoint (bl->gdbarch,
3683 &bl->overlay_target_info);
3684 }
3685 /* Did we set a breakpoint at the VMA?
3686 If so, we will have marked the breakpoint 'inserted'. */
3687 if (bl->inserted)
3688 {
3689 /* Yes -- remove it. Previously we did not bother to
3690 remove the breakpoint if the section had been
3691 unmapped, but let's not rely on that being safe. We
3692 don't know what the overlay manager might do. */
3693
3694 /* However, we should remove *software* breakpoints only
3695 if the section is still mapped, or else we overwrite
3696 wrong code with the saved shadow contents. */
3697 if (bl->loc_type == bp_loc_hardware_breakpoint
3698 || section_is_mapped (bl->section))
3699 val = bl->owner->ops->remove_location (bl);
3700 else
3701 val = 0;
3702 }
3703 else
3704 {
3705 /* No -- not inserted, so no need to remove. No error. */
3706 val = 0;
3707 }
3708 }
3709
3710 /* In some cases, we might not be able to remove a breakpoint
3711 in a shared library that has already been removed, but we
3712 have not yet processed the shlib unload event. */
3713 if (val && solib_name_from_address (bl->pspace, bl->address))
3714 val = 0;
3715
3716 if (val)
3717 return val;
3718 bl->inserted = (is == mark_inserted);
3719 }
3720 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3721 {
3722 gdb_assert (bl->owner->ops != NULL
3723 && bl->owner->ops->remove_location != NULL);
3724
3725 bl->inserted = (is == mark_inserted);
3726 bl->owner->ops->remove_location (bl);
3727
3728 /* Failure to remove any of the hardware watchpoints comes here. */
3729 if ((is == mark_uninserted) && (bl->inserted))
3730 warning (_("Could not remove hardware watchpoint %d."),
3731 bl->owner->number);
3732 }
3733 else if (bl->owner->type == bp_catchpoint
3734 && breakpoint_enabled (bl->owner)
3735 && !bl->duplicate)
3736 {
3737 gdb_assert (bl->owner->ops != NULL
3738 && bl->owner->ops->remove_location != NULL);
3739
3740 val = bl->owner->ops->remove_location (bl);
3741 if (val)
3742 return val;
3743
3744 bl->inserted = (is == mark_inserted);
3745 }
3746
3747 return 0;
3748}
3749
3750static int
3751remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3752{
3753 int ret;
3754 struct cleanup *old_chain;
3755
3756 /* BL is never in moribund_locations by our callers. */
3757 gdb_assert (bl->owner != NULL);
3758
3759 if (bl->owner->enable_state == bp_permanent)
3760 /* Permanent breakpoints cannot be inserted or removed. */
3761 return 0;
3762
3763 /* The type of none suggests that owner is actually deleted.
3764 This should not ever happen. */
3765 gdb_assert (bl->owner->type != bp_none);
3766
3767 old_chain = save_current_space_and_thread ();
3768
3769 switch_to_program_space_and_thread (bl->pspace);
3770
3771 ret = remove_breakpoint_1 (bl, is);
3772
3773 do_cleanups (old_chain);
3774 return ret;
3775}
3776
3777/* Clear the "inserted" flag in all breakpoints. */
3778
3779void
3780mark_breakpoints_out (void)
3781{
3782 struct bp_location *bl, **blp_tmp;
3783
3784 ALL_BP_LOCATIONS (bl, blp_tmp)
3785 if (bl->pspace == current_program_space)
3786 bl->inserted = 0;
3787}
3788
3789/* Clear the "inserted" flag in all breakpoints and delete any
3790 breakpoints which should go away between runs of the program.
3791
3792 Plus other such housekeeping that has to be done for breakpoints
3793 between runs.
3794
3795 Note: this function gets called at the end of a run (by
3796 generic_mourn_inferior) and when a run begins (by
3797 init_wait_for_inferior). */
3798
3799
3800
3801void
3802breakpoint_init_inferior (enum inf_context context)
3803{
3804 struct breakpoint *b, *b_tmp;
3805 struct bp_location *bl, **blp_tmp;
3806 int ix;
3807 struct program_space *pspace = current_program_space;
3808
3809 /* If breakpoint locations are shared across processes, then there's
3810 nothing to do. */
3811 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3812 return;
3813
3814 ALL_BP_LOCATIONS (bl, blp_tmp)
3815 {
3816 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3817 if (bl->pspace == pspace
3818 && bl->owner->enable_state != bp_permanent)
3819 bl->inserted = 0;
3820 }
3821
3822 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3823 {
3824 if (b->loc && b->loc->pspace != pspace)
3825 continue;
3826
3827 switch (b->type)
3828 {
3829 case bp_call_dummy:
3830 case bp_longjmp_call_dummy:
3831
3832 /* If the call dummy breakpoint is at the entry point it will
3833 cause problems when the inferior is rerun, so we better get
3834 rid of it. */
3835
3836 case bp_watchpoint_scope:
3837
3838 /* Also get rid of scope breakpoints. */
3839
3840 case bp_shlib_event:
3841
3842 /* Also remove solib event breakpoints. Their addresses may
3843 have changed since the last time we ran the program.
3844 Actually we may now be debugging against different target;
3845 and so the solib backend that installed this breakpoint may
3846 not be used in by the target. E.g.,
3847
3848 (gdb) file prog-linux
3849 (gdb) run # native linux target
3850 ...
3851 (gdb) kill
3852 (gdb) file prog-win.exe
3853 (gdb) tar rem :9999 # remote Windows gdbserver.
3854 */
3855
3856 case bp_step_resume:
3857
3858 /* Also remove step-resume breakpoints. */
3859
3860 delete_breakpoint (b);
3861 break;
3862
3863 case bp_watchpoint:
3864 case bp_hardware_watchpoint:
3865 case bp_read_watchpoint:
3866 case bp_access_watchpoint:
3867 {
3868 struct watchpoint *w = (struct watchpoint *) b;
3869
3870 /* Likewise for watchpoints on local expressions. */
3871 if (w->exp_valid_block != NULL)
3872 delete_breakpoint (b);
3873 else if (context == inf_starting)
3874 {
3875 /* Reset val field to force reread of starting value in
3876 insert_breakpoints. */
3877 if (w->val)
3878 value_free (w->val);
3879 w->val = NULL;
3880 w->val_valid = 0;
3881 }
3882 }
3883 break;
3884 default:
3885 break;
3886 }
3887 }
3888
3889 /* Get rid of the moribund locations. */
3890 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3891 decref_bp_location (&bl);
3892 VEC_free (bp_location_p, moribund_locations);
3893}
3894
3895/* These functions concern about actual breakpoints inserted in the
3896 target --- to e.g. check if we need to do decr_pc adjustment or if
3897 we need to hop over the bkpt --- so we check for address space
3898 match, not program space. */
3899
3900/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3901 exists at PC. It returns ordinary_breakpoint_here if it's an
3902 ordinary breakpoint, or permanent_breakpoint_here if it's a
3903 permanent breakpoint.
3904 - When continuing from a location with an ordinary breakpoint, we
3905 actually single step once before calling insert_breakpoints.
3906 - When continuing from a location with a permanent breakpoint, we
3907 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3908 the target, to advance the PC past the breakpoint. */
3909
3910enum breakpoint_here
3911breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3912{
3913 struct bp_location *bl, **blp_tmp;
3914 int any_breakpoint_here = 0;
3915
3916 ALL_BP_LOCATIONS (bl, blp_tmp)
3917 {
3918 if (bl->loc_type != bp_loc_software_breakpoint
3919 && bl->loc_type != bp_loc_hardware_breakpoint)
3920 continue;
3921
3922 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3923 if ((breakpoint_enabled (bl->owner)
3924 || bl->owner->enable_state == bp_permanent)
3925 && breakpoint_location_address_match (bl, aspace, pc))
3926 {
3927 if (overlay_debugging
3928 && section_is_overlay (bl->section)
3929 && !section_is_mapped (bl->section))
3930 continue; /* unmapped overlay -- can't be a match */
3931 else if (bl->owner->enable_state == bp_permanent)
3932 return permanent_breakpoint_here;
3933 else
3934 any_breakpoint_here = 1;
3935 }
3936 }
3937
3938 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3939}
3940
3941/* Return true if there's a moribund breakpoint at PC. */
3942
3943int
3944moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3945{
3946 struct bp_location *loc;
3947 int ix;
3948
3949 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3950 if (breakpoint_location_address_match (loc, aspace, pc))
3951 return 1;
3952
3953 return 0;
3954}
3955
3956/* Returns non-zero if there's a breakpoint inserted at PC, which is
3957 inserted using regular breakpoint_chain / bp_location array
3958 mechanism. This does not check for single-step breakpoints, which
3959 are inserted and removed using direct target manipulation. */
3960
3961int
3962regular_breakpoint_inserted_here_p (struct address_space *aspace,
3963 CORE_ADDR pc)
3964{
3965 struct bp_location *bl, **blp_tmp;
3966
3967 ALL_BP_LOCATIONS (bl, blp_tmp)
3968 {
3969 if (bl->loc_type != bp_loc_software_breakpoint
3970 && bl->loc_type != bp_loc_hardware_breakpoint)
3971 continue;
3972
3973 if (bl->inserted
3974 && breakpoint_location_address_match (bl, aspace, pc))
3975 {
3976 if (overlay_debugging
3977 && section_is_overlay (bl->section)
3978 && !section_is_mapped (bl->section))
3979 continue; /* unmapped overlay -- can't be a match */
3980 else
3981 return 1;
3982 }
3983 }
3984 return 0;
3985}
3986
3987/* Returns non-zero iff there's either regular breakpoint
3988 or a single step breakpoint inserted at PC. */
3989
3990int
3991breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
3992{
3993 if (regular_breakpoint_inserted_here_p (aspace, pc))
3994 return 1;
3995
3996 if (single_step_breakpoint_inserted_here_p (aspace, pc))
3997 return 1;
3998
3999 return 0;
4000}
4001
4002/* This function returns non-zero iff there is a software breakpoint
4003 inserted at PC. */
4004
4005int
4006software_breakpoint_inserted_here_p (struct address_space *aspace,
4007 CORE_ADDR pc)
4008{
4009 struct bp_location *bl, **blp_tmp;
4010
4011 ALL_BP_LOCATIONS (bl, blp_tmp)
4012 {
4013 if (bl->loc_type != bp_loc_software_breakpoint)
4014 continue;
4015
4016 if (bl->inserted
4017 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4018 aspace, pc))
4019 {
4020 if (overlay_debugging
4021 && section_is_overlay (bl->section)
4022 && !section_is_mapped (bl->section))
4023 continue; /* unmapped overlay -- can't be a match */
4024 else
4025 return 1;
4026 }
4027 }
4028
4029 /* Also check for software single-step breakpoints. */
4030 if (single_step_breakpoint_inserted_here_p (aspace, pc))
4031 return 1;
4032
4033 return 0;
4034}
4035
4036int
4037hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4038 CORE_ADDR addr, ULONGEST len)
4039{
4040 struct breakpoint *bpt;
4041
4042 ALL_BREAKPOINTS (bpt)
4043 {
4044 struct bp_location *loc;
4045
4046 if (bpt->type != bp_hardware_watchpoint
4047 && bpt->type != bp_access_watchpoint)
4048 continue;
4049
4050 if (!breakpoint_enabled (bpt))
4051 continue;
4052
4053 for (loc = bpt->loc; loc; loc = loc->next)
4054 if (loc->pspace->aspace == aspace && loc->inserted)
4055 {
4056 CORE_ADDR l, h;
4057
4058 /* Check for intersection. */
4059 l = max (loc->address, addr);
4060 h = min (loc->address + loc->length, addr + len);
4061 if (l < h)
4062 return 1;
4063 }
4064 }
4065 return 0;
4066}
4067
4068/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4069 PC is valid for process/thread PTID. */
4070
4071int
4072breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4073 ptid_t ptid)
4074{
4075 struct bp_location *bl, **blp_tmp;
4076 /* The thread and task IDs associated to PTID, computed lazily. */
4077 int thread = -1;
4078 int task = 0;
4079
4080 ALL_BP_LOCATIONS (bl, blp_tmp)
4081 {
4082 if (bl->loc_type != bp_loc_software_breakpoint
4083 && bl->loc_type != bp_loc_hardware_breakpoint)
4084 continue;
4085
4086 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4087 if (!breakpoint_enabled (bl->owner)
4088 && bl->owner->enable_state != bp_permanent)
4089 continue;
4090
4091 if (!breakpoint_location_address_match (bl, aspace, pc))
4092 continue;
4093
4094 if (bl->owner->thread != -1)
4095 {
4096 /* This is a thread-specific breakpoint. Check that ptid
4097 matches that thread. If thread hasn't been computed yet,
4098 it is now time to do so. */
4099 if (thread == -1)
4100 thread = pid_to_thread_id (ptid);
4101 if (bl->owner->thread != thread)
4102 continue;
4103 }
4104
4105 if (bl->owner->task != 0)
4106 {
4107 /* This is a task-specific breakpoint. Check that ptid
4108 matches that task. If task hasn't been computed yet,
4109 it is now time to do so. */
4110 if (task == 0)
4111 task = ada_get_task_number (ptid);
4112 if (bl->owner->task != task)
4113 continue;
4114 }
4115
4116 if (overlay_debugging
4117 && section_is_overlay (bl->section)
4118 && !section_is_mapped (bl->section))
4119 continue; /* unmapped overlay -- can't be a match */
4120
4121 return 1;
4122 }
4123
4124 return 0;
4125}
4126\f
4127
4128/* bpstat stuff. External routines' interfaces are documented
4129 in breakpoint.h. */
4130
4131int
4132is_catchpoint (struct breakpoint *ep)
4133{
4134 return (ep->type == bp_catchpoint);
4135}
4136
4137/* Frees any storage that is part of a bpstat. Does not walk the
4138 'next' chain. */
4139
4140static void
4141bpstat_free (bpstat bs)
4142{
4143 if (bs->old_val != NULL)
4144 value_free (bs->old_val);
4145 decref_counted_command_line (&bs->commands);
4146 decref_bp_location (&bs->bp_location_at);
4147 xfree (bs);
4148}
4149
4150/* Clear a bpstat so that it says we are not at any breakpoint.
4151 Also free any storage that is part of a bpstat. */
4152
4153void
4154bpstat_clear (bpstat *bsp)
4155{
4156 bpstat p;
4157 bpstat q;
4158
4159 if (bsp == 0)
4160 return;
4161 p = *bsp;
4162 while (p != NULL)
4163 {
4164 q = p->next;
4165 bpstat_free (p);
4166 p = q;
4167 }
4168 *bsp = NULL;
4169}
4170
4171/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4172 is part of the bpstat is copied as well. */
4173
4174bpstat
4175bpstat_copy (bpstat bs)
4176{
4177 bpstat p = NULL;
4178 bpstat tmp;
4179 bpstat retval = NULL;
4180
4181 if (bs == NULL)
4182 return bs;
4183
4184 for (; bs != NULL; bs = bs->next)
4185 {
4186 tmp = (bpstat) xmalloc (sizeof (*tmp));
4187 memcpy (tmp, bs, sizeof (*tmp));
4188 incref_counted_command_line (tmp->commands);
4189 incref_bp_location (tmp->bp_location_at);
4190 if (bs->old_val != NULL)
4191 {
4192 tmp->old_val = value_copy (bs->old_val);
4193 release_value (tmp->old_val);
4194 }
4195
4196 if (p == NULL)
4197 /* This is the first thing in the chain. */
4198 retval = tmp;
4199 else
4200 p->next = tmp;
4201 p = tmp;
4202 }
4203 p->next = NULL;
4204 return retval;
4205}
4206
4207/* Find the bpstat associated with this breakpoint. */
4208
4209bpstat
4210bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4211{
4212 if (bsp == NULL)
4213 return NULL;
4214
4215 for (; bsp != NULL; bsp = bsp->next)
4216 {
4217 if (bsp->breakpoint_at == breakpoint)
4218 return bsp;
4219 }
4220 return NULL;
4221}
4222
4223/* See breakpoint.h. */
4224
4225int
4226bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4227{
4228 for (; bsp != NULL; bsp = bsp->next)
4229 {
4230 if (bsp->breakpoint_at == NULL)
4231 {
4232 /* A moribund location can never explain a signal other than
4233 GDB_SIGNAL_TRAP. */
4234 if (sig == GDB_SIGNAL_TRAP)
4235 return 1;
4236 }
4237 else
4238 {
4239 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4240 sig))
4241 return 1;
4242 }
4243 }
4244
4245 return 0;
4246}
4247
4248/* Put in *NUM the breakpoint number of the first breakpoint we are
4249 stopped at. *BSP upon return is a bpstat which points to the
4250 remaining breakpoints stopped at (but which is not guaranteed to be
4251 good for anything but further calls to bpstat_num).
4252
4253 Return 0 if passed a bpstat which does not indicate any breakpoints.
4254 Return -1 if stopped at a breakpoint that has been deleted since
4255 we set it.
4256 Return 1 otherwise. */
4257
4258int
4259bpstat_num (bpstat *bsp, int *num)
4260{
4261 struct breakpoint *b;
4262
4263 if ((*bsp) == NULL)
4264 return 0; /* No more breakpoint values */
4265
4266 /* We assume we'll never have several bpstats that correspond to a
4267 single breakpoint -- otherwise, this function might return the
4268 same number more than once and this will look ugly. */
4269 b = (*bsp)->breakpoint_at;
4270 *bsp = (*bsp)->next;
4271 if (b == NULL)
4272 return -1; /* breakpoint that's been deleted since */
4273
4274 *num = b->number; /* We have its number */
4275 return 1;
4276}
4277
4278/* See breakpoint.h. */
4279
4280void
4281bpstat_clear_actions (void)
4282{
4283 struct thread_info *tp;
4284 bpstat bs;
4285
4286 if (ptid_equal (inferior_ptid, null_ptid))
4287 return;
4288
4289 tp = find_thread_ptid (inferior_ptid);
4290 if (tp == NULL)
4291 return;
4292
4293 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4294 {
4295 decref_counted_command_line (&bs->commands);
4296
4297 if (bs->old_val != NULL)
4298 {
4299 value_free (bs->old_val);
4300 bs->old_val = NULL;
4301 }
4302 }
4303}
4304
4305/* Called when a command is about to proceed the inferior. */
4306
4307static void
4308breakpoint_about_to_proceed (void)
4309{
4310 if (!ptid_equal (inferior_ptid, null_ptid))
4311 {
4312 struct thread_info *tp = inferior_thread ();
4313
4314 /* Allow inferior function calls in breakpoint commands to not
4315 interrupt the command list. When the call finishes
4316 successfully, the inferior will be standing at the same
4317 breakpoint as if nothing happened. */
4318 if (tp->control.in_infcall)
4319 return;
4320 }
4321
4322 breakpoint_proceeded = 1;
4323}
4324
4325/* Stub for cleaning up our state if we error-out of a breakpoint
4326 command. */
4327static void
4328cleanup_executing_breakpoints (void *ignore)
4329{
4330 executing_breakpoint_commands = 0;
4331}
4332
4333/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4334 or its equivalent. */
4335
4336static int
4337command_line_is_silent (struct command_line *cmd)
4338{
4339 return cmd && (strcmp ("silent", cmd->line) == 0
4340 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4341}
4342
4343/* Execute all the commands associated with all the breakpoints at
4344 this location. Any of these commands could cause the process to
4345 proceed beyond this point, etc. We look out for such changes by
4346 checking the global "breakpoint_proceeded" after each command.
4347
4348 Returns true if a breakpoint command resumed the inferior. In that
4349 case, it is the caller's responsibility to recall it again with the
4350 bpstat of the current thread. */
4351
4352static int
4353bpstat_do_actions_1 (bpstat *bsp)
4354{
4355 bpstat bs;
4356 struct cleanup *old_chain;
4357 int again = 0;
4358
4359 /* Avoid endless recursion if a `source' command is contained
4360 in bs->commands. */
4361 if (executing_breakpoint_commands)
4362 return 0;
4363
4364 executing_breakpoint_commands = 1;
4365 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4366
4367 prevent_dont_repeat ();
4368
4369 /* This pointer will iterate over the list of bpstat's. */
4370 bs = *bsp;
4371
4372 breakpoint_proceeded = 0;
4373 for (; bs != NULL; bs = bs->next)
4374 {
4375 struct counted_command_line *ccmd;
4376 struct command_line *cmd;
4377 struct cleanup *this_cmd_tree_chain;
4378
4379 /* Take ownership of the BSP's command tree, if it has one.
4380
4381 The command tree could legitimately contain commands like
4382 'step' and 'next', which call clear_proceed_status, which
4383 frees stop_bpstat's command tree. To make sure this doesn't
4384 free the tree we're executing out from under us, we need to
4385 take ownership of the tree ourselves. Since a given bpstat's
4386 commands are only executed once, we don't need to copy it; we
4387 can clear the pointer in the bpstat, and make sure we free
4388 the tree when we're done. */
4389 ccmd = bs->commands;
4390 bs->commands = NULL;
4391 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4392 cmd = ccmd ? ccmd->commands : NULL;
4393 if (command_line_is_silent (cmd))
4394 {
4395 /* The action has been already done by bpstat_stop_status. */
4396 cmd = cmd->next;
4397 }
4398
4399 while (cmd != NULL)
4400 {
4401 execute_control_command (cmd);
4402
4403 if (breakpoint_proceeded)
4404 break;
4405 else
4406 cmd = cmd->next;
4407 }
4408
4409 /* We can free this command tree now. */
4410 do_cleanups (this_cmd_tree_chain);
4411
4412 if (breakpoint_proceeded)
4413 {
4414 if (target_can_async_p ())
4415 /* If we are in async mode, then the target might be still
4416 running, not stopped at any breakpoint, so nothing for
4417 us to do here -- just return to the event loop. */
4418 ;
4419 else
4420 /* In sync mode, when execute_control_command returns
4421 we're already standing on the next breakpoint.
4422 Breakpoint commands for that stop were not run, since
4423 execute_command does not run breakpoint commands --
4424 only command_line_handler does, but that one is not
4425 involved in execution of breakpoint commands. So, we
4426 can now execute breakpoint commands. It should be
4427 noted that making execute_command do bpstat actions is
4428 not an option -- in this case we'll have recursive
4429 invocation of bpstat for each breakpoint with a
4430 command, and can easily blow up GDB stack. Instead, we
4431 return true, which will trigger the caller to recall us
4432 with the new stop_bpstat. */
4433 again = 1;
4434 break;
4435 }
4436 }
4437 do_cleanups (old_chain);
4438 return again;
4439}
4440
4441void
4442bpstat_do_actions (void)
4443{
4444 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4445
4446 /* Do any commands attached to breakpoint we are stopped at. */
4447 while (!ptid_equal (inferior_ptid, null_ptid)
4448 && target_has_execution
4449 && !is_exited (inferior_ptid)
4450 && !is_executing (inferior_ptid))
4451 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4452 and only return when it is stopped at the next breakpoint, we
4453 keep doing breakpoint actions until it returns false to
4454 indicate the inferior was not resumed. */
4455 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4456 break;
4457
4458 discard_cleanups (cleanup_if_error);
4459}
4460
4461/* Print out the (old or new) value associated with a watchpoint. */
4462
4463static void
4464watchpoint_value_print (struct value *val, struct ui_file *stream)
4465{
4466 if (val == NULL)
4467 fprintf_unfiltered (stream, _("<unreadable>"));
4468 else
4469 {
4470 struct value_print_options opts;
4471 get_user_print_options (&opts);
4472 value_print (val, stream, &opts);
4473 }
4474}
4475
4476/* Generic routine for printing messages indicating why we
4477 stopped. The behavior of this function depends on the value
4478 'print_it' in the bpstat structure. Under some circumstances we
4479 may decide not to print anything here and delegate the task to
4480 normal_stop(). */
4481
4482static enum print_stop_action
4483print_bp_stop_message (bpstat bs)
4484{
4485 switch (bs->print_it)
4486 {
4487 case print_it_noop:
4488 /* Nothing should be printed for this bpstat entry. */
4489 return PRINT_UNKNOWN;
4490 break;
4491
4492 case print_it_done:
4493 /* We still want to print the frame, but we already printed the
4494 relevant messages. */
4495 return PRINT_SRC_AND_LOC;
4496 break;
4497
4498 case print_it_normal:
4499 {
4500 struct breakpoint *b = bs->breakpoint_at;
4501
4502 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4503 which has since been deleted. */
4504 if (b == NULL)
4505 return PRINT_UNKNOWN;
4506
4507 /* Normal case. Call the breakpoint's print_it method. */
4508 return b->ops->print_it (bs);
4509 }
4510 break;
4511
4512 default:
4513 internal_error (__FILE__, __LINE__,
4514 _("print_bp_stop_message: unrecognized enum value"));
4515 break;
4516 }
4517}
4518
4519/* A helper function that prints a shared library stopped event. */
4520
4521static void
4522print_solib_event (int is_catchpoint)
4523{
4524 int any_deleted
4525 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4526 int any_added
4527 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4528
4529 if (!is_catchpoint)
4530 {
4531 if (any_added || any_deleted)
4532 ui_out_text (current_uiout,
4533 _("Stopped due to shared library event:\n"));
4534 else
4535 ui_out_text (current_uiout,
4536 _("Stopped due to shared library event (no "
4537 "libraries added or removed)\n"));
4538 }
4539
4540 if (ui_out_is_mi_like_p (current_uiout))
4541 ui_out_field_string (current_uiout, "reason",
4542 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4543
4544 if (any_deleted)
4545 {
4546 struct cleanup *cleanup;
4547 char *name;
4548 int ix;
4549
4550 ui_out_text (current_uiout, _(" Inferior unloaded "));
4551 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4552 "removed");
4553 for (ix = 0;
4554 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4555 ix, name);
4556 ++ix)
4557 {
4558 if (ix > 0)
4559 ui_out_text (current_uiout, " ");
4560 ui_out_field_string (current_uiout, "library", name);
4561 ui_out_text (current_uiout, "\n");
4562 }
4563
4564 do_cleanups (cleanup);
4565 }
4566
4567 if (any_added)
4568 {
4569 struct so_list *iter;
4570 int ix;
4571 struct cleanup *cleanup;
4572
4573 ui_out_text (current_uiout, _(" Inferior loaded "));
4574 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4575 "added");
4576 for (ix = 0;
4577 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4578 ix, iter);
4579 ++ix)
4580 {
4581 if (ix > 0)
4582 ui_out_text (current_uiout, " ");
4583 ui_out_field_string (current_uiout, "library", iter->so_name);
4584 ui_out_text (current_uiout, "\n");
4585 }
4586
4587 do_cleanups (cleanup);
4588 }
4589}
4590
4591/* Print a message indicating what happened. This is called from
4592 normal_stop(). The input to this routine is the head of the bpstat
4593 list - a list of the eventpoints that caused this stop. KIND is
4594 the target_waitkind for the stopping event. This
4595 routine calls the generic print routine for printing a message
4596 about reasons for stopping. This will print (for example) the
4597 "Breakpoint n," part of the output. The return value of this
4598 routine is one of:
4599
4600 PRINT_UNKNOWN: Means we printed nothing.
4601 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4602 code to print the location. An example is
4603 "Breakpoint 1, " which should be followed by
4604 the location.
4605 PRINT_SRC_ONLY: Means we printed something, but there is no need
4606 to also print the location part of the message.
4607 An example is the catch/throw messages, which
4608 don't require a location appended to the end.
4609 PRINT_NOTHING: We have done some printing and we don't need any
4610 further info to be printed. */
4611
4612enum print_stop_action
4613bpstat_print (bpstat bs, int kind)
4614{
4615 int val;
4616
4617 /* Maybe another breakpoint in the chain caused us to stop.
4618 (Currently all watchpoints go on the bpstat whether hit or not.
4619 That probably could (should) be changed, provided care is taken
4620 with respect to bpstat_explains_signal). */
4621 for (; bs; bs = bs->next)
4622 {
4623 val = print_bp_stop_message (bs);
4624 if (val == PRINT_SRC_ONLY
4625 || val == PRINT_SRC_AND_LOC
4626 || val == PRINT_NOTHING)
4627 return val;
4628 }
4629
4630 /* If we had hit a shared library event breakpoint,
4631 print_bp_stop_message would print out this message. If we hit an
4632 OS-level shared library event, do the same thing. */
4633 if (kind == TARGET_WAITKIND_LOADED)
4634 {
4635 print_solib_event (0);
4636 return PRINT_NOTHING;
4637 }
4638
4639 /* We reached the end of the chain, or we got a null BS to start
4640 with and nothing was printed. */
4641 return PRINT_UNKNOWN;
4642}
4643
4644/* Evaluate the expression EXP and return 1 if value is zero.
4645 This returns the inverse of the condition because it is called
4646 from catch_errors which returns 0 if an exception happened, and if an
4647 exception happens we want execution to stop.
4648 The argument is a "struct expression *" that has been cast to a
4649 "void *" to make it pass through catch_errors. */
4650
4651static int
4652breakpoint_cond_eval (void *exp)
4653{
4654 struct value *mark = value_mark ();
4655 int i = !value_true (evaluate_expression ((struct expression *) exp));
4656
4657 value_free_to_mark (mark);
4658 return i;
4659}
4660
4661/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4662
4663static bpstat
4664bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4665{
4666 bpstat bs;
4667
4668 bs = (bpstat) xmalloc (sizeof (*bs));
4669 bs->next = NULL;
4670 **bs_link_pointer = bs;
4671 *bs_link_pointer = &bs->next;
4672 bs->breakpoint_at = bl->owner;
4673 bs->bp_location_at = bl;
4674 incref_bp_location (bl);
4675 /* If the condition is false, etc., don't do the commands. */
4676 bs->commands = NULL;
4677 bs->old_val = NULL;
4678 bs->print_it = print_it_normal;
4679 return bs;
4680}
4681\f
4682/* The target has stopped with waitstatus WS. Check if any hardware
4683 watchpoints have triggered, according to the target. */
4684
4685int
4686watchpoints_triggered (struct target_waitstatus *ws)
4687{
4688 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4689 CORE_ADDR addr;
4690 struct breakpoint *b;
4691
4692 if (!stopped_by_watchpoint)
4693 {
4694 /* We were not stopped by a watchpoint. Mark all watchpoints
4695 as not triggered. */
4696 ALL_BREAKPOINTS (b)
4697 if (is_hardware_watchpoint (b))
4698 {
4699 struct watchpoint *w = (struct watchpoint *) b;
4700
4701 w->watchpoint_triggered = watch_triggered_no;
4702 }
4703
4704 return 0;
4705 }
4706
4707 if (!target_stopped_data_address (&current_target, &addr))
4708 {
4709 /* We were stopped by a watchpoint, but we don't know where.
4710 Mark all watchpoints as unknown. */
4711 ALL_BREAKPOINTS (b)
4712 if (is_hardware_watchpoint (b))
4713 {
4714 struct watchpoint *w = (struct watchpoint *) b;
4715
4716 w->watchpoint_triggered = watch_triggered_unknown;
4717 }
4718
4719 return 1;
4720 }
4721
4722 /* The target could report the data address. Mark watchpoints
4723 affected by this data address as triggered, and all others as not
4724 triggered. */
4725
4726 ALL_BREAKPOINTS (b)
4727 if (is_hardware_watchpoint (b))
4728 {
4729 struct watchpoint *w = (struct watchpoint *) b;
4730 struct bp_location *loc;
4731
4732 w->watchpoint_triggered = watch_triggered_no;
4733 for (loc = b->loc; loc; loc = loc->next)
4734 {
4735 if (is_masked_watchpoint (b))
4736 {
4737 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4738 CORE_ADDR start = loc->address & w->hw_wp_mask;
4739
4740 if (newaddr == start)
4741 {
4742 w->watchpoint_triggered = watch_triggered_yes;
4743 break;
4744 }
4745 }
4746 /* Exact match not required. Within range is sufficient. */
4747 else if (target_watchpoint_addr_within_range (&current_target,
4748 addr, loc->address,
4749 loc->length))
4750 {
4751 w->watchpoint_triggered = watch_triggered_yes;
4752 break;
4753 }
4754 }
4755 }
4756
4757 return 1;
4758}
4759
4760/* Possible return values for watchpoint_check (this can't be an enum
4761 because of check_errors). */
4762/* The watchpoint has been deleted. */
4763#define WP_DELETED 1
4764/* The value has changed. */
4765#define WP_VALUE_CHANGED 2
4766/* The value has not changed. */
4767#define WP_VALUE_NOT_CHANGED 3
4768/* Ignore this watchpoint, no matter if the value changed or not. */
4769#define WP_IGNORE 4
4770
4771#define BP_TEMPFLAG 1
4772#define BP_HARDWAREFLAG 2
4773
4774/* Evaluate watchpoint condition expression and check if its value
4775 changed.
4776
4777 P should be a pointer to struct bpstat, but is defined as a void *
4778 in order for this function to be usable with catch_errors. */
4779
4780static int
4781watchpoint_check (void *p)
4782{
4783 bpstat bs = (bpstat) p;
4784 struct watchpoint *b;
4785 struct frame_info *fr;
4786 int within_current_scope;
4787
4788 /* BS is built from an existing struct breakpoint. */
4789 gdb_assert (bs->breakpoint_at != NULL);
4790 b = (struct watchpoint *) bs->breakpoint_at;
4791
4792 /* If this is a local watchpoint, we only want to check if the
4793 watchpoint frame is in scope if the current thread is the thread
4794 that was used to create the watchpoint. */
4795 if (!watchpoint_in_thread_scope (b))
4796 return WP_IGNORE;
4797
4798 if (b->exp_valid_block == NULL)
4799 within_current_scope = 1;
4800 else
4801 {
4802 struct frame_info *frame = get_current_frame ();
4803 struct gdbarch *frame_arch = get_frame_arch (frame);
4804 CORE_ADDR frame_pc = get_frame_pc (frame);
4805
4806 /* in_function_epilogue_p() returns a non-zero value if we're
4807 still in the function but the stack frame has already been
4808 invalidated. Since we can't rely on the values of local
4809 variables after the stack has been destroyed, we are treating
4810 the watchpoint in that state as `not changed' without further
4811 checking. Don't mark watchpoints as changed if the current
4812 frame is in an epilogue - even if they are in some other
4813 frame, our view of the stack is likely to be wrong and
4814 frame_find_by_id could error out. */
4815 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4816 return WP_IGNORE;
4817
4818 fr = frame_find_by_id (b->watchpoint_frame);
4819 within_current_scope = (fr != NULL);
4820
4821 /* If we've gotten confused in the unwinder, we might have
4822 returned a frame that can't describe this variable. */
4823 if (within_current_scope)
4824 {
4825 struct symbol *function;
4826
4827 function = get_frame_function (fr);
4828 if (function == NULL
4829 || !contained_in (b->exp_valid_block,
4830 SYMBOL_BLOCK_VALUE (function)))
4831 within_current_scope = 0;
4832 }
4833
4834 if (within_current_scope)
4835 /* If we end up stopping, the current frame will get selected
4836 in normal_stop. So this call to select_frame won't affect
4837 the user. */
4838 select_frame (fr);
4839 }
4840
4841 if (within_current_scope)
4842 {
4843 /* We use value_{,free_to_}mark because it could be a *long*
4844 time before we return to the command level and call
4845 free_all_values. We can't call free_all_values because we
4846 might be in the middle of evaluating a function call. */
4847
4848 int pc = 0;
4849 struct value *mark;
4850 struct value *new_val;
4851
4852 if (is_masked_watchpoint (&b->base))
4853 /* Since we don't know the exact trigger address (from
4854 stopped_data_address), just tell the user we've triggered
4855 a mask watchpoint. */
4856 return WP_VALUE_CHANGED;
4857
4858 mark = value_mark ();
4859 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
4860
4861 /* We use value_equal_contents instead of value_equal because
4862 the latter coerces an array to a pointer, thus comparing just
4863 the address of the array instead of its contents. This is
4864 not what we want. */
4865 if ((b->val != NULL) != (new_val != NULL)
4866 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4867 {
4868 if (new_val != NULL)
4869 {
4870 release_value (new_val);
4871 value_free_to_mark (mark);
4872 }
4873 bs->old_val = b->val;
4874 b->val = new_val;
4875 b->val_valid = 1;
4876 return WP_VALUE_CHANGED;
4877 }
4878 else
4879 {
4880 /* Nothing changed. */
4881 value_free_to_mark (mark);
4882 return WP_VALUE_NOT_CHANGED;
4883 }
4884 }
4885 else
4886 {
4887 struct ui_out *uiout = current_uiout;
4888
4889 /* This seems like the only logical thing to do because
4890 if we temporarily ignored the watchpoint, then when
4891 we reenter the block in which it is valid it contains
4892 garbage (in the case of a function, it may have two
4893 garbage values, one before and one after the prologue).
4894 So we can't even detect the first assignment to it and
4895 watch after that (since the garbage may or may not equal
4896 the first value assigned). */
4897 /* We print all the stop information in
4898 breakpoint_ops->print_it, but in this case, by the time we
4899 call breakpoint_ops->print_it this bp will be deleted
4900 already. So we have no choice but print the information
4901 here. */
4902 if (ui_out_is_mi_like_p (uiout))
4903 ui_out_field_string
4904 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4905 ui_out_text (uiout, "\nWatchpoint ");
4906 ui_out_field_int (uiout, "wpnum", b->base.number);
4907 ui_out_text (uiout,
4908 " deleted because the program has left the block in\n\
4909which its expression is valid.\n");
4910
4911 /* Make sure the watchpoint's commands aren't executed. */
4912 decref_counted_command_line (&b->base.commands);
4913 watchpoint_del_at_next_stop (b);
4914
4915 return WP_DELETED;
4916 }
4917}
4918
4919/* Return true if it looks like target has stopped due to hitting
4920 breakpoint location BL. This function does not check if we should
4921 stop, only if BL explains the stop. */
4922
4923static int
4924bpstat_check_location (const struct bp_location *bl,
4925 struct address_space *aspace, CORE_ADDR bp_addr,
4926 const struct target_waitstatus *ws)
4927{
4928 struct breakpoint *b = bl->owner;
4929
4930 /* BL is from an existing breakpoint. */
4931 gdb_assert (b != NULL);
4932
4933 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4934}
4935
4936/* Determine if the watched values have actually changed, and we
4937 should stop. If not, set BS->stop to 0. */
4938
4939static void
4940bpstat_check_watchpoint (bpstat bs)
4941{
4942 const struct bp_location *bl;
4943 struct watchpoint *b;
4944
4945 /* BS is built for existing struct breakpoint. */
4946 bl = bs->bp_location_at;
4947 gdb_assert (bl != NULL);
4948 b = (struct watchpoint *) bs->breakpoint_at;
4949 gdb_assert (b != NULL);
4950
4951 {
4952 int must_check_value = 0;
4953
4954 if (b->base.type == bp_watchpoint)
4955 /* For a software watchpoint, we must always check the
4956 watched value. */
4957 must_check_value = 1;
4958 else if (b->watchpoint_triggered == watch_triggered_yes)
4959 /* We have a hardware watchpoint (read, write, or access)
4960 and the target earlier reported an address watched by
4961 this watchpoint. */
4962 must_check_value = 1;
4963 else if (b->watchpoint_triggered == watch_triggered_unknown
4964 && b->base.type == bp_hardware_watchpoint)
4965 /* We were stopped by a hardware watchpoint, but the target could
4966 not report the data address. We must check the watchpoint's
4967 value. Access and read watchpoints are out of luck; without
4968 a data address, we can't figure it out. */
4969 must_check_value = 1;
4970
4971 if (must_check_value)
4972 {
4973 char *message
4974 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4975 b->base.number);
4976 struct cleanup *cleanups = make_cleanup (xfree, message);
4977 int e = catch_errors (watchpoint_check, bs, message,
4978 RETURN_MASK_ALL);
4979 do_cleanups (cleanups);
4980 switch (e)
4981 {
4982 case WP_DELETED:
4983 /* We've already printed what needs to be printed. */
4984 bs->print_it = print_it_done;
4985 /* Stop. */
4986 break;
4987 case WP_IGNORE:
4988 bs->print_it = print_it_noop;
4989 bs->stop = 0;
4990 break;
4991 case WP_VALUE_CHANGED:
4992 if (b->base.type == bp_read_watchpoint)
4993 {
4994 /* There are two cases to consider here:
4995
4996 1. We're watching the triggered memory for reads.
4997 In that case, trust the target, and always report
4998 the watchpoint hit to the user. Even though
4999 reads don't cause value changes, the value may
5000 have changed since the last time it was read, and
5001 since we're not trapping writes, we will not see
5002 those, and as such we should ignore our notion of
5003 old value.
5004
5005 2. We're watching the triggered memory for both
5006 reads and writes. There are two ways this may
5007 happen:
5008
5009 2.1. This is a target that can't break on data
5010 reads only, but can break on accesses (reads or
5011 writes), such as e.g., x86. We detect this case
5012 at the time we try to insert read watchpoints.
5013
5014 2.2. Otherwise, the target supports read
5015 watchpoints, but, the user set an access or write
5016 watchpoint watching the same memory as this read
5017 watchpoint.
5018
5019 If we're watching memory writes as well as reads,
5020 ignore watchpoint hits when we find that the
5021 value hasn't changed, as reads don't cause
5022 changes. This still gives false positives when
5023 the program writes the same value to memory as
5024 what there was already in memory (we will confuse
5025 it for a read), but it's much better than
5026 nothing. */
5027
5028 int other_write_watchpoint = 0;
5029
5030 if (bl->watchpoint_type == hw_read)
5031 {
5032 struct breakpoint *other_b;
5033
5034 ALL_BREAKPOINTS (other_b)
5035 if (other_b->type == bp_hardware_watchpoint
5036 || other_b->type == bp_access_watchpoint)
5037 {
5038 struct watchpoint *other_w =
5039 (struct watchpoint *) other_b;
5040
5041 if (other_w->watchpoint_triggered
5042 == watch_triggered_yes)
5043 {
5044 other_write_watchpoint = 1;
5045 break;
5046 }
5047 }
5048 }
5049
5050 if (other_write_watchpoint
5051 || bl->watchpoint_type == hw_access)
5052 {
5053 /* We're watching the same memory for writes,
5054 and the value changed since the last time we
5055 updated it, so this trap must be for a write.
5056 Ignore it. */
5057 bs->print_it = print_it_noop;
5058 bs->stop = 0;
5059 }
5060 }
5061 break;
5062 case WP_VALUE_NOT_CHANGED:
5063 if (b->base.type == bp_hardware_watchpoint
5064 || b->base.type == bp_watchpoint)
5065 {
5066 /* Don't stop: write watchpoints shouldn't fire if
5067 the value hasn't changed. */
5068 bs->print_it = print_it_noop;
5069 bs->stop = 0;
5070 }
5071 /* Stop. */
5072 break;
5073 default:
5074 /* Can't happen. */
5075 case 0:
5076 /* Error from catch_errors. */
5077 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5078 watchpoint_del_at_next_stop (b);
5079 /* We've already printed what needs to be printed. */
5080 bs->print_it = print_it_done;
5081 break;
5082 }
5083 }
5084 else /* must_check_value == 0 */
5085 {
5086 /* This is a case where some watchpoint(s) triggered, but
5087 not at the address of this watchpoint, or else no
5088 watchpoint triggered after all. So don't print
5089 anything for this watchpoint. */
5090 bs->print_it = print_it_noop;
5091 bs->stop = 0;
5092 }
5093 }
5094}
5095
5096/* For breakpoints that are currently marked as telling gdb to stop,
5097 check conditions (condition proper, frame, thread and ignore count)
5098 of breakpoint referred to by BS. If we should not stop for this
5099 breakpoint, set BS->stop to 0. */
5100
5101static void
5102bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5103{
5104 int thread_id = pid_to_thread_id (ptid);
5105 const struct bp_location *bl;
5106 struct breakpoint *b;
5107 int value_is_zero = 0;
5108 struct expression *cond;
5109
5110 gdb_assert (bs->stop);
5111
5112 /* BS is built for existing struct breakpoint. */
5113 bl = bs->bp_location_at;
5114 gdb_assert (bl != NULL);
5115 b = bs->breakpoint_at;
5116 gdb_assert (b != NULL);
5117
5118 /* Even if the target evaluated the condition on its end and notified GDB, we
5119 need to do so again since GDB does not know if we stopped due to a
5120 breakpoint or a single step breakpoint. */
5121
5122 if (frame_id_p (b->frame_id)
5123 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5124 {
5125 bs->stop = 0;
5126 return;
5127 }
5128
5129 /* If this is a thread-specific breakpoint, don't waste cpu evaluating the
5130 condition if this isn't the specified thread. */
5131 if (b->thread != -1 && b->thread != thread_id)
5132 {
5133 bs->stop = 0;
5134 return;
5135 }
5136
5137 /* Evaluate Python breakpoints that have a "stop" method implemented. */
5138 if (b->py_bp_object)
5139 bs->stop = gdbpy_should_stop (b->py_bp_object);
5140
5141 if (is_watchpoint (b))
5142 {
5143 struct watchpoint *w = (struct watchpoint *) b;
5144
5145 cond = w->cond_exp;
5146 }
5147 else
5148 cond = bl->cond;
5149
5150 if (cond && b->disposition != disp_del_at_next_stop)
5151 {
5152 int within_current_scope = 1;
5153 struct watchpoint * w;
5154
5155 /* We use value_mark and value_free_to_mark because it could
5156 be a long time before we return to the command level and
5157 call free_all_values. We can't call free_all_values
5158 because we might be in the middle of evaluating a
5159 function call. */
5160 struct value *mark = value_mark ();
5161
5162 if (is_watchpoint (b))
5163 w = (struct watchpoint *) b;
5164 else
5165 w = NULL;
5166
5167 /* Need to select the frame, with all that implies so that
5168 the conditions will have the right context. Because we
5169 use the frame, we will not see an inlined function's
5170 variables when we arrive at a breakpoint at the start
5171 of the inlined function; the current frame will be the
5172 call site. */
5173 if (w == NULL || w->cond_exp_valid_block == NULL)
5174 select_frame (get_current_frame ());
5175 else
5176 {
5177 struct frame_info *frame;
5178
5179 /* For local watchpoint expressions, which particular
5180 instance of a local is being watched matters, so we
5181 keep track of the frame to evaluate the expression
5182 in. To evaluate the condition however, it doesn't
5183 really matter which instantiation of the function
5184 where the condition makes sense triggers the
5185 watchpoint. This allows an expression like "watch
5186 global if q > 10" set in `func', catch writes to
5187 global on all threads that call `func', or catch
5188 writes on all recursive calls of `func' by a single
5189 thread. We simply always evaluate the condition in
5190 the innermost frame that's executing where it makes
5191 sense to evaluate the condition. It seems
5192 intuitive. */
5193 frame = block_innermost_frame (w->cond_exp_valid_block);
5194 if (frame != NULL)
5195 select_frame (frame);
5196 else
5197 within_current_scope = 0;
5198 }
5199 if (within_current_scope)
5200 value_is_zero
5201 = catch_errors (breakpoint_cond_eval, cond,
5202 "Error in testing breakpoint condition:\n",
5203 RETURN_MASK_ALL);
5204 else
5205 {
5206 warning (_("Watchpoint condition cannot be tested "
5207 "in the current scope"));
5208 /* If we failed to set the right context for this
5209 watchpoint, unconditionally report it. */
5210 value_is_zero = 0;
5211 }
5212 /* FIXME-someday, should give breakpoint #. */
5213 value_free_to_mark (mark);
5214 }
5215
5216 if (cond && value_is_zero)
5217 {
5218 bs->stop = 0;
5219 }
5220 else if (b->ignore_count > 0)
5221 {
5222 b->ignore_count--;
5223 bs->stop = 0;
5224 /* Increase the hit count even though we don't stop. */
5225 ++(b->hit_count);
5226 observer_notify_breakpoint_modified (b);
5227 }
5228}
5229
5230
5231/* Get a bpstat associated with having just stopped at address
5232 BP_ADDR in thread PTID.
5233
5234 Determine whether we stopped at a breakpoint, etc, or whether we
5235 don't understand this stop. Result is a chain of bpstat's such
5236 that:
5237
5238 if we don't understand the stop, the result is a null pointer.
5239
5240 if we understand why we stopped, the result is not null.
5241
5242 Each element of the chain refers to a particular breakpoint or
5243 watchpoint at which we have stopped. (We may have stopped for
5244 several reasons concurrently.)
5245
5246 Each element of the chain has valid next, breakpoint_at,
5247 commands, FIXME??? fields. */
5248
5249bpstat
5250bpstat_stop_status (struct address_space *aspace,
5251 CORE_ADDR bp_addr, ptid_t ptid,
5252 const struct target_waitstatus *ws)
5253{
5254 struct breakpoint *b = NULL;
5255 struct bp_location *bl;
5256 struct bp_location *loc;
5257 /* First item of allocated bpstat's. */
5258 bpstat bs_head = NULL, *bs_link = &bs_head;
5259 /* Pointer to the last thing in the chain currently. */
5260 bpstat bs;
5261 int ix;
5262 int need_remove_insert;
5263 int removed_any;
5264
5265 /* First, build the bpstat chain with locations that explain a
5266 target stop, while being careful to not set the target running,
5267 as that may invalidate locations (in particular watchpoint
5268 locations are recreated). Resuming will happen here with
5269 breakpoint conditions or watchpoint expressions that include
5270 inferior function calls. */
5271
5272 ALL_BREAKPOINTS (b)
5273 {
5274 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5275 continue;
5276
5277 for (bl = b->loc; bl != NULL; bl = bl->next)
5278 {
5279 /* For hardware watchpoints, we look only at the first
5280 location. The watchpoint_check function will work on the
5281 entire expression, not the individual locations. For
5282 read watchpoints, the watchpoints_triggered function has
5283 checked all locations already. */
5284 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5285 break;
5286
5287 if (!bl->enabled || bl->shlib_disabled)
5288 continue;
5289
5290 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5291 continue;
5292
5293 /* Come here if it's a watchpoint, or if the break address
5294 matches. */
5295
5296 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5297 explain stop. */
5298
5299 /* Assume we stop. Should we find a watchpoint that is not
5300 actually triggered, or if the condition of the breakpoint
5301 evaluates as false, we'll reset 'stop' to 0. */
5302 bs->stop = 1;
5303 bs->print = 1;
5304
5305 /* If this is a scope breakpoint, mark the associated
5306 watchpoint as triggered so that we will handle the
5307 out-of-scope event. We'll get to the watchpoint next
5308 iteration. */
5309 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5310 {
5311 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5312
5313 w->watchpoint_triggered = watch_triggered_yes;
5314 }
5315 }
5316 }
5317
5318 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5319 {
5320 if (breakpoint_location_address_match (loc, aspace, bp_addr))
5321 {
5322 bs = bpstat_alloc (loc, &bs_link);
5323 /* For hits of moribund locations, we should just proceed. */
5324 bs->stop = 0;
5325 bs->print = 0;
5326 bs->print_it = print_it_noop;
5327 }
5328 }
5329
5330 /* A bit of special processing for shlib breakpoints. We need to
5331 process solib loading here, so that the lists of loaded and
5332 unloaded libraries are correct before we handle "catch load" and
5333 "catch unload". */
5334 for (bs = bs_head; bs != NULL; bs = bs->next)
5335 {
5336 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5337 {
5338 handle_solib_event ();
5339 break;
5340 }
5341 }
5342
5343 /* Now go through the locations that caused the target to stop, and
5344 check whether we're interested in reporting this stop to higher
5345 layers, or whether we should resume the target transparently. */
5346
5347 removed_any = 0;
5348
5349 for (bs = bs_head; bs != NULL; bs = bs->next)
5350 {
5351 if (!bs->stop)
5352 continue;
5353
5354 b = bs->breakpoint_at;
5355 b->ops->check_status (bs);
5356 if (bs->stop)
5357 {
5358 bpstat_check_breakpoint_conditions (bs, ptid);
5359
5360 if (bs->stop)
5361 {
5362 ++(b->hit_count);
5363 observer_notify_breakpoint_modified (b);
5364
5365 /* We will stop here. */
5366 if (b->disposition == disp_disable)
5367 {
5368 --(b->enable_count);
5369 if (b->enable_count <= 0
5370 && b->enable_state != bp_permanent)
5371 b->enable_state = bp_disabled;
5372 removed_any = 1;
5373 }
5374 if (b->silent)
5375 bs->print = 0;
5376 bs->commands = b->commands;
5377 incref_counted_command_line (bs->commands);
5378 if (command_line_is_silent (bs->commands
5379 ? bs->commands->commands : NULL))
5380 bs->print = 0;
5381
5382 b->ops->after_condition_true (bs);
5383 }
5384
5385 }
5386
5387 /* Print nothing for this entry if we don't stop or don't
5388 print. */
5389 if (!bs->stop || !bs->print)
5390 bs->print_it = print_it_noop;
5391 }
5392
5393 /* If we aren't stopping, the value of some hardware watchpoint may
5394 not have changed, but the intermediate memory locations we are
5395 watching may have. Don't bother if we're stopping; this will get
5396 done later. */
5397 need_remove_insert = 0;
5398 if (! bpstat_causes_stop (bs_head))
5399 for (bs = bs_head; bs != NULL; bs = bs->next)
5400 if (!bs->stop
5401 && bs->breakpoint_at
5402 && is_hardware_watchpoint (bs->breakpoint_at))
5403 {
5404 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5405
5406 update_watchpoint (w, 0 /* don't reparse. */);
5407 need_remove_insert = 1;
5408 }
5409
5410 if (need_remove_insert)
5411 update_global_location_list (1);
5412 else if (removed_any)
5413 update_global_location_list (0);
5414
5415 return bs_head;
5416}
5417
5418static void
5419handle_jit_event (void)
5420{
5421 struct frame_info *frame;
5422 struct gdbarch *gdbarch;
5423
5424 /* Switch terminal for any messages produced by
5425 breakpoint_re_set. */
5426 target_terminal_ours_for_output ();
5427
5428 frame = get_current_frame ();
5429 gdbarch = get_frame_arch (frame);
5430
5431 jit_event_handler (gdbarch);
5432
5433 target_terminal_inferior ();
5434}
5435
5436/* Prepare WHAT final decision for infrun. */
5437
5438/* Decide what infrun needs to do with this bpstat. */
5439
5440struct bpstat_what
5441bpstat_what (bpstat bs_head)
5442{
5443 struct bpstat_what retval;
5444 int jit_event = 0;
5445 bpstat bs;
5446
5447 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5448 retval.call_dummy = STOP_NONE;
5449 retval.is_longjmp = 0;
5450
5451 for (bs = bs_head; bs != NULL; bs = bs->next)
5452 {
5453 /* Extract this BS's action. After processing each BS, we check
5454 if its action overrides all we've seem so far. */
5455 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5456 enum bptype bptype;
5457
5458 if (bs->breakpoint_at == NULL)
5459 {
5460 /* I suspect this can happen if it was a momentary
5461 breakpoint which has since been deleted. */
5462 bptype = bp_none;
5463 }
5464 else
5465 bptype = bs->breakpoint_at->type;
5466
5467 switch (bptype)
5468 {
5469 case bp_none:
5470 break;
5471 case bp_breakpoint:
5472 case bp_hardware_breakpoint:
5473 case bp_until:
5474 case bp_finish:
5475 case bp_shlib_event:
5476 if (bs->stop)
5477 {
5478 if (bs->print)
5479 this_action = BPSTAT_WHAT_STOP_NOISY;
5480 else
5481 this_action = BPSTAT_WHAT_STOP_SILENT;
5482 }
5483 else
5484 this_action = BPSTAT_WHAT_SINGLE;
5485 break;
5486 case bp_watchpoint:
5487 case bp_hardware_watchpoint:
5488 case bp_read_watchpoint:
5489 case bp_access_watchpoint:
5490 if (bs->stop)
5491 {
5492 if (bs->print)
5493 this_action = BPSTAT_WHAT_STOP_NOISY;
5494 else
5495 this_action = BPSTAT_WHAT_STOP_SILENT;
5496 }
5497 else
5498 {
5499 /* There was a watchpoint, but we're not stopping.
5500 This requires no further action. */
5501 }
5502 break;
5503 case bp_longjmp:
5504 case bp_longjmp_call_dummy:
5505 case bp_exception:
5506 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5507 retval.is_longjmp = bptype != bp_exception;
5508 break;
5509 case bp_longjmp_resume:
5510 case bp_exception_resume:
5511 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5512 retval.is_longjmp = bptype == bp_longjmp_resume;
5513 break;
5514 case bp_step_resume:
5515 if (bs->stop)
5516 this_action = BPSTAT_WHAT_STEP_RESUME;
5517 else
5518 {
5519 /* It is for the wrong frame. */
5520 this_action = BPSTAT_WHAT_SINGLE;
5521 }
5522 break;
5523 case bp_hp_step_resume:
5524 if (bs->stop)
5525 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5526 else
5527 {
5528 /* It is for the wrong frame. */
5529 this_action = BPSTAT_WHAT_SINGLE;
5530 }
5531 break;
5532 case bp_watchpoint_scope:
5533 case bp_thread_event:
5534 case bp_overlay_event:
5535 case bp_longjmp_master:
5536 case bp_std_terminate_master:
5537 case bp_exception_master:
5538 this_action = BPSTAT_WHAT_SINGLE;
5539 break;
5540 case bp_catchpoint:
5541 if (bs->stop)
5542 {
5543 if (bs->print)
5544 this_action = BPSTAT_WHAT_STOP_NOISY;
5545 else
5546 this_action = BPSTAT_WHAT_STOP_SILENT;
5547 }
5548 else
5549 {
5550 /* There was a catchpoint, but we're not stopping.
5551 This requires no further action. */
5552 }
5553 break;
5554 case bp_jit_event:
5555 jit_event = 1;
5556 this_action = BPSTAT_WHAT_SINGLE;
5557 break;
5558 case bp_call_dummy:
5559 /* Make sure the action is stop (silent or noisy),
5560 so infrun.c pops the dummy frame. */
5561 retval.call_dummy = STOP_STACK_DUMMY;
5562 this_action = BPSTAT_WHAT_STOP_SILENT;
5563 break;
5564 case bp_std_terminate:
5565 /* Make sure the action is stop (silent or noisy),
5566 so infrun.c pops the dummy frame. */
5567 retval.call_dummy = STOP_STD_TERMINATE;
5568 this_action = BPSTAT_WHAT_STOP_SILENT;
5569 break;
5570 case bp_tracepoint:
5571 case bp_fast_tracepoint:
5572 case bp_static_tracepoint:
5573 /* Tracepoint hits should not be reported back to GDB, and
5574 if one got through somehow, it should have been filtered
5575 out already. */
5576 internal_error (__FILE__, __LINE__,
5577 _("bpstat_what: tracepoint encountered"));
5578 break;
5579 case bp_gnu_ifunc_resolver:
5580 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5581 this_action = BPSTAT_WHAT_SINGLE;
5582 break;
5583 case bp_gnu_ifunc_resolver_return:
5584 /* The breakpoint will be removed, execution will restart from the
5585 PC of the former breakpoint. */
5586 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5587 break;
5588
5589 case bp_dprintf:
5590 if (bs->stop)
5591 this_action = BPSTAT_WHAT_STOP_SILENT;
5592 else
5593 this_action = BPSTAT_WHAT_SINGLE;
5594 break;
5595
5596 default:
5597 internal_error (__FILE__, __LINE__,
5598 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5599 }
5600
5601 retval.main_action = max (retval.main_action, this_action);
5602 }
5603
5604 /* These operations may affect the bs->breakpoint_at state so they are
5605 delayed after MAIN_ACTION is decided above. */
5606
5607 if (jit_event)
5608 {
5609 if (debug_infrun)
5610 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5611
5612 handle_jit_event ();
5613 }
5614
5615 for (bs = bs_head; bs != NULL; bs = bs->next)
5616 {
5617 struct breakpoint *b = bs->breakpoint_at;
5618
5619 if (b == NULL)
5620 continue;
5621 switch (b->type)
5622 {
5623 case bp_gnu_ifunc_resolver:
5624 gnu_ifunc_resolver_stop (b);
5625 break;
5626 case bp_gnu_ifunc_resolver_return:
5627 gnu_ifunc_resolver_return_stop (b);
5628 break;
5629 }
5630 }
5631
5632 return retval;
5633}
5634
5635/* Nonzero if we should step constantly (e.g. watchpoints on machines
5636 without hardware support). This isn't related to a specific bpstat,
5637 just to things like whether watchpoints are set. */
5638
5639int
5640bpstat_should_step (void)
5641{
5642 struct breakpoint *b;
5643
5644 ALL_BREAKPOINTS (b)
5645 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5646 return 1;
5647 return 0;
5648}
5649
5650int
5651bpstat_causes_stop (bpstat bs)
5652{
5653 for (; bs != NULL; bs = bs->next)
5654 if (bs->stop)
5655 return 1;
5656
5657 return 0;
5658}
5659
5660\f
5661
5662/* Compute a string of spaces suitable to indent the next line
5663 so it starts at the position corresponding to the table column
5664 named COL_NAME in the currently active table of UIOUT. */
5665
5666static char *
5667wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5668{
5669 static char wrap_indent[80];
5670 int i, total_width, width, align;
5671 char *text;
5672
5673 total_width = 0;
5674 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5675 {
5676 if (strcmp (text, col_name) == 0)
5677 {
5678 gdb_assert (total_width < sizeof wrap_indent);
5679 memset (wrap_indent, ' ', total_width);
5680 wrap_indent[total_width] = 0;
5681
5682 return wrap_indent;
5683 }
5684
5685 total_width += width + 1;
5686 }
5687
5688 return NULL;
5689}
5690
5691/* Determine if the locations of this breakpoint will have their conditions
5692 evaluated by the target, host or a mix of both. Returns the following:
5693
5694 "host": Host evals condition.
5695 "host or target": Host or Target evals condition.
5696 "target": Target evals condition.
5697*/
5698
5699static const char *
5700bp_condition_evaluator (struct breakpoint *b)
5701{
5702 struct bp_location *bl;
5703 char host_evals = 0;
5704 char target_evals = 0;
5705
5706 if (!b)
5707 return NULL;
5708
5709 if (!is_breakpoint (b))
5710 return NULL;
5711
5712 if (gdb_evaluates_breakpoint_condition_p ()
5713 || !target_supports_evaluation_of_breakpoint_conditions ())
5714 return condition_evaluation_host;
5715
5716 for (bl = b->loc; bl; bl = bl->next)
5717 {
5718 if (bl->cond_bytecode)
5719 target_evals++;
5720 else
5721 host_evals++;
5722 }
5723
5724 if (host_evals && target_evals)
5725 return condition_evaluation_both;
5726 else if (target_evals)
5727 return condition_evaluation_target;
5728 else
5729 return condition_evaluation_host;
5730}
5731
5732/* Determine the breakpoint location's condition evaluator. This is
5733 similar to bp_condition_evaluator, but for locations. */
5734
5735static const char *
5736bp_location_condition_evaluator (struct bp_location *bl)
5737{
5738 if (bl && !is_breakpoint (bl->owner))
5739 return NULL;
5740
5741 if (gdb_evaluates_breakpoint_condition_p ()
5742 || !target_supports_evaluation_of_breakpoint_conditions ())
5743 return condition_evaluation_host;
5744
5745 if (bl && bl->cond_bytecode)
5746 return condition_evaluation_target;
5747 else
5748 return condition_evaluation_host;
5749}
5750
5751/* Print the LOC location out of the list of B->LOC locations. */
5752
5753static void
5754print_breakpoint_location (struct breakpoint *b,
5755 struct bp_location *loc)
5756{
5757 struct ui_out *uiout = current_uiout;
5758 struct cleanup *old_chain = save_current_program_space ();
5759
5760 if (loc != NULL && loc->shlib_disabled)
5761 loc = NULL;
5762
5763 if (loc != NULL)
5764 set_current_program_space (loc->pspace);
5765
5766 if (b->display_canonical)
5767 ui_out_field_string (uiout, "what", b->addr_string);
5768 else if (loc && loc->symtab)
5769 {
5770 struct symbol *sym
5771 = find_pc_sect_function (loc->address, loc->section);
5772 if (sym)
5773 {
5774 ui_out_text (uiout, "in ");
5775 ui_out_field_string (uiout, "func",
5776 SYMBOL_PRINT_NAME (sym));
5777 ui_out_text (uiout, " ");
5778 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5779 ui_out_text (uiout, "at ");
5780 }
5781 ui_out_field_string (uiout, "file",
5782 symtab_to_filename_for_display (loc->symtab));
5783 ui_out_text (uiout, ":");
5784
5785 if (ui_out_is_mi_like_p (uiout))
5786 ui_out_field_string (uiout, "fullname",
5787 symtab_to_fullname (loc->symtab));
5788
5789 ui_out_field_int (uiout, "line", loc->line_number);
5790 }
5791 else if (loc)
5792 {
5793 struct ui_file *stb = mem_fileopen ();
5794 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5795
5796 print_address_symbolic (loc->gdbarch, loc->address, stb,
5797 demangle, "");
5798 ui_out_field_stream (uiout, "at", stb);
5799
5800 do_cleanups (stb_chain);
5801 }
5802 else
5803 ui_out_field_string (uiout, "pending", b->addr_string);
5804
5805 if (loc && is_breakpoint (b)
5806 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5807 && bp_condition_evaluator (b) == condition_evaluation_both)
5808 {
5809 ui_out_text (uiout, " (");
5810 ui_out_field_string (uiout, "evaluated-by",
5811 bp_location_condition_evaluator (loc));
5812 ui_out_text (uiout, ")");
5813 }
5814
5815 do_cleanups (old_chain);
5816}
5817
5818static const char *
5819bptype_string (enum bptype type)
5820{
5821 struct ep_type_description
5822 {
5823 enum bptype type;
5824 char *description;
5825 };
5826 static struct ep_type_description bptypes[] =
5827 {
5828 {bp_none, "?deleted?"},
5829 {bp_breakpoint, "breakpoint"},
5830 {bp_hardware_breakpoint, "hw breakpoint"},
5831 {bp_until, "until"},
5832 {bp_finish, "finish"},
5833 {bp_watchpoint, "watchpoint"},
5834 {bp_hardware_watchpoint, "hw watchpoint"},
5835 {bp_read_watchpoint, "read watchpoint"},
5836 {bp_access_watchpoint, "acc watchpoint"},
5837 {bp_longjmp, "longjmp"},
5838 {bp_longjmp_resume, "longjmp resume"},
5839 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5840 {bp_exception, "exception"},
5841 {bp_exception_resume, "exception resume"},
5842 {bp_step_resume, "step resume"},
5843 {bp_hp_step_resume, "high-priority step resume"},
5844 {bp_watchpoint_scope, "watchpoint scope"},
5845 {bp_call_dummy, "call dummy"},
5846 {bp_std_terminate, "std::terminate"},
5847 {bp_shlib_event, "shlib events"},
5848 {bp_thread_event, "thread events"},
5849 {bp_overlay_event, "overlay events"},
5850 {bp_longjmp_master, "longjmp master"},
5851 {bp_std_terminate_master, "std::terminate master"},
5852 {bp_exception_master, "exception master"},
5853 {bp_catchpoint, "catchpoint"},
5854 {bp_tracepoint, "tracepoint"},
5855 {bp_fast_tracepoint, "fast tracepoint"},
5856 {bp_static_tracepoint, "static tracepoint"},
5857 {bp_dprintf, "dprintf"},
5858 {bp_jit_event, "jit events"},
5859 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5860 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5861 };
5862
5863 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5864 || ((int) type != bptypes[(int) type].type))
5865 internal_error (__FILE__, __LINE__,
5866 _("bptypes table does not describe type #%d."),
5867 (int) type);
5868
5869 return bptypes[(int) type].description;
5870}
5871
5872/* For MI, output a field named 'thread-groups' with a list as the value.
5873 For CLI, prefix the list with the string 'inf'. */
5874
5875static void
5876output_thread_groups (struct ui_out *uiout,
5877 const char *field_name,
5878 VEC(int) *inf_num,
5879 int mi_only)
5880{
5881 struct cleanup *back_to;
5882 int is_mi = ui_out_is_mi_like_p (uiout);
5883 int inf;
5884 int i;
5885
5886 /* For backward compatibility, don't display inferiors in CLI unless
5887 there are several. Always display them for MI. */
5888 if (!is_mi && mi_only)
5889 return;
5890
5891 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
5892
5893 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
5894 {
5895 if (is_mi)
5896 {
5897 char mi_group[10];
5898
5899 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
5900 ui_out_field_string (uiout, NULL, mi_group);
5901 }
5902 else
5903 {
5904 if (i == 0)
5905 ui_out_text (uiout, " inf ");
5906 else
5907 ui_out_text (uiout, ", ");
5908
5909 ui_out_text (uiout, plongest (inf));
5910 }
5911 }
5912
5913 do_cleanups (back_to);
5914}
5915
5916/* Print B to gdb_stdout. */
5917
5918static void
5919print_one_breakpoint_location (struct breakpoint *b,
5920 struct bp_location *loc,
5921 int loc_number,
5922 struct bp_location **last_loc,
5923 int allflag)
5924{
5925 struct command_line *l;
5926 static char bpenables[] = "nynny";
5927
5928 struct ui_out *uiout = current_uiout;
5929 int header_of_multiple = 0;
5930 int part_of_multiple = (loc != NULL);
5931 struct value_print_options opts;
5932
5933 get_user_print_options (&opts);
5934
5935 gdb_assert (!loc || loc_number != 0);
5936 /* See comment in print_one_breakpoint concerning treatment of
5937 breakpoints with single disabled location. */
5938 if (loc == NULL
5939 && (b->loc != NULL
5940 && (b->loc->next != NULL || !b->loc->enabled)))
5941 header_of_multiple = 1;
5942 if (loc == NULL)
5943 loc = b->loc;
5944
5945 annotate_record ();
5946
5947 /* 1 */
5948 annotate_field (0);
5949 if (part_of_multiple)
5950 {
5951 char *formatted;
5952 formatted = xstrprintf ("%d.%d", b->number, loc_number);
5953 ui_out_field_string (uiout, "number", formatted);
5954 xfree (formatted);
5955 }
5956 else
5957 {
5958 ui_out_field_int (uiout, "number", b->number);
5959 }
5960
5961 /* 2 */
5962 annotate_field (1);
5963 if (part_of_multiple)
5964 ui_out_field_skip (uiout, "type");
5965 else
5966 ui_out_field_string (uiout, "type", bptype_string (b->type));
5967
5968 /* 3 */
5969 annotate_field (2);
5970 if (part_of_multiple)
5971 ui_out_field_skip (uiout, "disp");
5972 else
5973 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
5974
5975
5976 /* 4 */
5977 annotate_field (3);
5978 if (part_of_multiple)
5979 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
5980 else
5981 ui_out_field_fmt (uiout, "enabled", "%c",
5982 bpenables[(int) b->enable_state]);
5983 ui_out_spaces (uiout, 2);
5984
5985
5986 /* 5 and 6 */
5987 if (b->ops != NULL && b->ops->print_one != NULL)
5988 {
5989 /* Although the print_one can possibly print all locations,
5990 calling it here is not likely to get any nice result. So,
5991 make sure there's just one location. */
5992 gdb_assert (b->loc == NULL || b->loc->next == NULL);
5993 b->ops->print_one (b, last_loc);
5994 }
5995 else
5996 switch (b->type)
5997 {
5998 case bp_none:
5999 internal_error (__FILE__, __LINE__,
6000 _("print_one_breakpoint: bp_none encountered\n"));
6001 break;
6002
6003 case bp_watchpoint:
6004 case bp_hardware_watchpoint:
6005 case bp_read_watchpoint:
6006 case bp_access_watchpoint:
6007 {
6008 struct watchpoint *w = (struct watchpoint *) b;
6009
6010 /* Field 4, the address, is omitted (which makes the columns
6011 not line up too nicely with the headers, but the effect
6012 is relatively readable). */
6013 if (opts.addressprint)
6014 ui_out_field_skip (uiout, "addr");
6015 annotate_field (5);
6016 ui_out_field_string (uiout, "what", w->exp_string);
6017 }
6018 break;
6019
6020 case bp_breakpoint:
6021 case bp_hardware_breakpoint:
6022 case bp_until:
6023 case bp_finish:
6024 case bp_longjmp:
6025 case bp_longjmp_resume:
6026 case bp_longjmp_call_dummy:
6027 case bp_exception:
6028 case bp_exception_resume:
6029 case bp_step_resume:
6030 case bp_hp_step_resume:
6031 case bp_watchpoint_scope:
6032 case bp_call_dummy:
6033 case bp_std_terminate:
6034 case bp_shlib_event:
6035 case bp_thread_event:
6036 case bp_overlay_event:
6037 case bp_longjmp_master:
6038 case bp_std_terminate_master:
6039 case bp_exception_master:
6040 case bp_tracepoint:
6041 case bp_fast_tracepoint:
6042 case bp_static_tracepoint:
6043 case bp_dprintf:
6044 case bp_jit_event:
6045 case bp_gnu_ifunc_resolver:
6046 case bp_gnu_ifunc_resolver_return:
6047 if (opts.addressprint)
6048 {
6049 annotate_field (4);
6050 if (header_of_multiple)
6051 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6052 else if (b->loc == NULL || loc->shlib_disabled)
6053 ui_out_field_string (uiout, "addr", "<PENDING>");
6054 else
6055 ui_out_field_core_addr (uiout, "addr",
6056 loc->gdbarch, loc->address);
6057 }
6058 annotate_field (5);
6059 if (!header_of_multiple)
6060 print_breakpoint_location (b, loc);
6061 if (b->loc)
6062 *last_loc = b->loc;
6063 break;
6064 }
6065
6066
6067 if (loc != NULL && !header_of_multiple)
6068 {
6069 struct inferior *inf;
6070 VEC(int) *inf_num = NULL;
6071 int mi_only = 1;
6072
6073 ALL_INFERIORS (inf)
6074 {
6075 if (inf->pspace == loc->pspace)
6076 VEC_safe_push (int, inf_num, inf->num);
6077 }
6078
6079 /* For backward compatibility, don't display inferiors in CLI unless
6080 there are several. Always display for MI. */
6081 if (allflag
6082 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6083 && (number_of_program_spaces () > 1
6084 || number_of_inferiors () > 1)
6085 /* LOC is for existing B, it cannot be in
6086 moribund_locations and thus having NULL OWNER. */
6087 && loc->owner->type != bp_catchpoint))
6088 mi_only = 0;
6089 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6090 VEC_free (int, inf_num);
6091 }
6092
6093 if (!part_of_multiple)
6094 {
6095 if (b->thread != -1)
6096 {
6097 /* FIXME: This seems to be redundant and lost here; see the
6098 "stop only in" line a little further down. */
6099 ui_out_text (uiout, " thread ");
6100 ui_out_field_int (uiout, "thread", b->thread);
6101 }
6102 else if (b->task != 0)
6103 {
6104 ui_out_text (uiout, " task ");
6105 ui_out_field_int (uiout, "task", b->task);
6106 }
6107 }
6108
6109 ui_out_text (uiout, "\n");
6110
6111 if (!part_of_multiple)
6112 b->ops->print_one_detail (b, uiout);
6113
6114 if (part_of_multiple && frame_id_p (b->frame_id))
6115 {
6116 annotate_field (6);
6117 ui_out_text (uiout, "\tstop only in stack frame at ");
6118 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6119 the frame ID. */
6120 ui_out_field_core_addr (uiout, "frame",
6121 b->gdbarch, b->frame_id.stack_addr);
6122 ui_out_text (uiout, "\n");
6123 }
6124
6125 if (!part_of_multiple && b->cond_string)
6126 {
6127 annotate_field (7);
6128 if (is_tracepoint (b))
6129 ui_out_text (uiout, "\ttrace only if ");
6130 else
6131 ui_out_text (uiout, "\tstop only if ");
6132 ui_out_field_string (uiout, "cond", b->cond_string);
6133
6134 /* Print whether the target is doing the breakpoint's condition
6135 evaluation. If GDB is doing the evaluation, don't print anything. */
6136 if (is_breakpoint (b)
6137 && breakpoint_condition_evaluation_mode ()
6138 == condition_evaluation_target)
6139 {
6140 ui_out_text (uiout, " (");
6141 ui_out_field_string (uiout, "evaluated-by",
6142 bp_condition_evaluator (b));
6143 ui_out_text (uiout, " evals)");
6144 }
6145 ui_out_text (uiout, "\n");
6146 }
6147
6148 if (!part_of_multiple && b->thread != -1)
6149 {
6150 /* FIXME should make an annotation for this. */
6151 ui_out_text (uiout, "\tstop only in thread ");
6152 ui_out_field_int (uiout, "thread", b->thread);
6153 ui_out_text (uiout, "\n");
6154 }
6155
6156 if (!part_of_multiple)
6157 {
6158 if (b->hit_count)
6159 {
6160 /* FIXME should make an annotation for this. */
6161 if (is_catchpoint (b))
6162 ui_out_text (uiout, "\tcatchpoint");
6163 else if (is_tracepoint (b))
6164 ui_out_text (uiout, "\ttracepoint");
6165 else
6166 ui_out_text (uiout, "\tbreakpoint");
6167 ui_out_text (uiout, " already hit ");
6168 ui_out_field_int (uiout, "times", b->hit_count);
6169 if (b->hit_count == 1)
6170 ui_out_text (uiout, " time\n");
6171 else
6172 ui_out_text (uiout, " times\n");
6173 }
6174 else
6175 {
6176 /* Output the count also if it is zero, but only if this is mi. */
6177 if (ui_out_is_mi_like_p (uiout))
6178 ui_out_field_int (uiout, "times", b->hit_count);
6179 }
6180 }
6181
6182 if (!part_of_multiple && b->ignore_count)
6183 {
6184 annotate_field (8);
6185 ui_out_text (uiout, "\tignore next ");
6186 ui_out_field_int (uiout, "ignore", b->ignore_count);
6187 ui_out_text (uiout, " hits\n");
6188 }
6189
6190 /* Note that an enable count of 1 corresponds to "enable once"
6191 behavior, which is reported by the combination of enablement and
6192 disposition, so we don't need to mention it here. */
6193 if (!part_of_multiple && b->enable_count > 1)
6194 {
6195 annotate_field (8);
6196 ui_out_text (uiout, "\tdisable after ");
6197 /* Tweak the wording to clarify that ignore and enable counts
6198 are distinct, and have additive effect. */
6199 if (b->ignore_count)
6200 ui_out_text (uiout, "additional ");
6201 else
6202 ui_out_text (uiout, "next ");
6203 ui_out_field_int (uiout, "enable", b->enable_count);
6204 ui_out_text (uiout, " hits\n");
6205 }
6206
6207 if (!part_of_multiple && is_tracepoint (b))
6208 {
6209 struct tracepoint *tp = (struct tracepoint *) b;
6210
6211 if (tp->traceframe_usage)
6212 {
6213 ui_out_text (uiout, "\ttrace buffer usage ");
6214 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6215 ui_out_text (uiout, " bytes\n");
6216 }
6217 }
6218
6219 l = b->commands ? b->commands->commands : NULL;
6220 if (!part_of_multiple && l)
6221 {
6222 struct cleanup *script_chain;
6223
6224 annotate_field (9);
6225 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6226 print_command_lines (uiout, l, 4);
6227 do_cleanups (script_chain);
6228 }
6229
6230 if (is_tracepoint (b))
6231 {
6232 struct tracepoint *t = (struct tracepoint *) b;
6233
6234 if (!part_of_multiple && t->pass_count)
6235 {
6236 annotate_field (10);
6237 ui_out_text (uiout, "\tpass count ");
6238 ui_out_field_int (uiout, "pass", t->pass_count);
6239 ui_out_text (uiout, " \n");
6240 }
6241
6242 /* Don't display it when tracepoint or tracepoint location is
6243 pending. */
6244 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6245 {
6246 annotate_field (11);
6247
6248 if (ui_out_is_mi_like_p (uiout))
6249 ui_out_field_string (uiout, "installed",
6250 loc->inserted ? "y" : "n");
6251 else
6252 {
6253 if (loc->inserted)
6254 ui_out_text (uiout, "\t");
6255 else
6256 ui_out_text (uiout, "\tnot ");
6257 ui_out_text (uiout, "installed on target\n");
6258 }
6259 }
6260 }
6261
6262 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6263 {
6264 if (is_watchpoint (b))
6265 {
6266 struct watchpoint *w = (struct watchpoint *) b;
6267
6268 ui_out_field_string (uiout, "original-location", w->exp_string);
6269 }
6270 else if (b->addr_string)
6271 ui_out_field_string (uiout, "original-location", b->addr_string);
6272 }
6273}
6274
6275static void
6276print_one_breakpoint (struct breakpoint *b,
6277 struct bp_location **last_loc,
6278 int allflag)
6279{
6280 struct cleanup *bkpt_chain;
6281 struct ui_out *uiout = current_uiout;
6282
6283 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6284
6285 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6286 do_cleanups (bkpt_chain);
6287
6288 /* If this breakpoint has custom print function,
6289 it's already printed. Otherwise, print individual
6290 locations, if any. */
6291 if (b->ops == NULL || b->ops->print_one == NULL)
6292 {
6293 /* If breakpoint has a single location that is disabled, we
6294 print it as if it had several locations, since otherwise it's
6295 hard to represent "breakpoint enabled, location disabled"
6296 situation.
6297
6298 Note that while hardware watchpoints have several locations
6299 internally, that's not a property exposed to user. */
6300 if (b->loc
6301 && !is_hardware_watchpoint (b)
6302 && (b->loc->next || !b->loc->enabled))
6303 {
6304 struct bp_location *loc;
6305 int n = 1;
6306
6307 for (loc = b->loc; loc; loc = loc->next, ++n)
6308 {
6309 struct cleanup *inner2 =
6310 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6311 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6312 do_cleanups (inner2);
6313 }
6314 }
6315 }
6316}
6317
6318static int
6319breakpoint_address_bits (struct breakpoint *b)
6320{
6321 int print_address_bits = 0;
6322 struct bp_location *loc;
6323
6324 for (loc = b->loc; loc; loc = loc->next)
6325 {
6326 int addr_bit;
6327
6328 /* Software watchpoints that aren't watching memory don't have
6329 an address to print. */
6330 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6331 continue;
6332
6333 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6334 if (addr_bit > print_address_bits)
6335 print_address_bits = addr_bit;
6336 }
6337
6338 return print_address_bits;
6339}
6340
6341struct captured_breakpoint_query_args
6342 {
6343 int bnum;
6344 };
6345
6346static int
6347do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6348{
6349 struct captured_breakpoint_query_args *args = data;
6350 struct breakpoint *b;
6351 struct bp_location *dummy_loc = NULL;
6352
6353 ALL_BREAKPOINTS (b)
6354 {
6355 if (args->bnum == b->number)
6356 {
6357 print_one_breakpoint (b, &dummy_loc, 0);
6358 return GDB_RC_OK;
6359 }
6360 }
6361 return GDB_RC_NONE;
6362}
6363
6364enum gdb_rc
6365gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6366 char **error_message)
6367{
6368 struct captured_breakpoint_query_args args;
6369
6370 args.bnum = bnum;
6371 /* For the moment we don't trust print_one_breakpoint() to not throw
6372 an error. */
6373 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6374 error_message, RETURN_MASK_ALL) < 0)
6375 return GDB_RC_FAIL;
6376 else
6377 return GDB_RC_OK;
6378}
6379
6380/* Return true if this breakpoint was set by the user, false if it is
6381 internal or momentary. */
6382
6383int
6384user_breakpoint_p (struct breakpoint *b)
6385{
6386 return b->number > 0;
6387}
6388
6389/* Print information on user settable breakpoint (watchpoint, etc)
6390 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6391 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6392 FILTER is non-NULL, call it on each breakpoint and only include the
6393 ones for which it returns non-zero. Return the total number of
6394 breakpoints listed. */
6395
6396static int
6397breakpoint_1 (char *args, int allflag,
6398 int (*filter) (const struct breakpoint *))
6399{
6400 struct breakpoint *b;
6401 struct bp_location *last_loc = NULL;
6402 int nr_printable_breakpoints;
6403 struct cleanup *bkpttbl_chain;
6404 struct value_print_options opts;
6405 int print_address_bits = 0;
6406 int print_type_col_width = 14;
6407 struct ui_out *uiout = current_uiout;
6408
6409 get_user_print_options (&opts);
6410
6411 /* Compute the number of rows in the table, as well as the size
6412 required for address fields. */
6413 nr_printable_breakpoints = 0;
6414 ALL_BREAKPOINTS (b)
6415 {
6416 /* If we have a filter, only list the breakpoints it accepts. */
6417 if (filter && !filter (b))
6418 continue;
6419
6420 /* If we have an "args" string, it is a list of breakpoints to
6421 accept. Skip the others. */
6422 if (args != NULL && *args != '\0')
6423 {
6424 if (allflag && parse_and_eval_long (args) != b->number)
6425 continue;
6426 if (!allflag && !number_is_in_list (args, b->number))
6427 continue;
6428 }
6429
6430 if (allflag || user_breakpoint_p (b))
6431 {
6432 int addr_bit, type_len;
6433
6434 addr_bit = breakpoint_address_bits (b);
6435 if (addr_bit > print_address_bits)
6436 print_address_bits = addr_bit;
6437
6438 type_len = strlen (bptype_string (b->type));
6439 if (type_len > print_type_col_width)
6440 print_type_col_width = type_len;
6441
6442 nr_printable_breakpoints++;
6443 }
6444 }
6445
6446 if (opts.addressprint)
6447 bkpttbl_chain
6448 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6449 nr_printable_breakpoints,
6450 "BreakpointTable");
6451 else
6452 bkpttbl_chain
6453 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6454 nr_printable_breakpoints,
6455 "BreakpointTable");
6456
6457 if (nr_printable_breakpoints > 0)
6458 annotate_breakpoints_headers ();
6459 if (nr_printable_breakpoints > 0)
6460 annotate_field (0);
6461 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6462 if (nr_printable_breakpoints > 0)
6463 annotate_field (1);
6464 ui_out_table_header (uiout, print_type_col_width, ui_left,
6465 "type", "Type"); /* 2 */
6466 if (nr_printable_breakpoints > 0)
6467 annotate_field (2);
6468 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6469 if (nr_printable_breakpoints > 0)
6470 annotate_field (3);
6471 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6472 if (opts.addressprint)
6473 {
6474 if (nr_printable_breakpoints > 0)
6475 annotate_field (4);
6476 if (print_address_bits <= 32)
6477 ui_out_table_header (uiout, 10, ui_left,
6478 "addr", "Address"); /* 5 */
6479 else
6480 ui_out_table_header (uiout, 18, ui_left,
6481 "addr", "Address"); /* 5 */
6482 }
6483 if (nr_printable_breakpoints > 0)
6484 annotate_field (5);
6485 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6486 ui_out_table_body (uiout);
6487 if (nr_printable_breakpoints > 0)
6488 annotate_breakpoints_table ();
6489
6490 ALL_BREAKPOINTS (b)
6491 {
6492 QUIT;
6493 /* If we have a filter, only list the breakpoints it accepts. */
6494 if (filter && !filter (b))
6495 continue;
6496
6497 /* If we have an "args" string, it is a list of breakpoints to
6498 accept. Skip the others. */
6499
6500 if (args != NULL && *args != '\0')
6501 {
6502 if (allflag) /* maintenance info breakpoint */
6503 {
6504 if (parse_and_eval_long (args) != b->number)
6505 continue;
6506 }
6507 else /* all others */
6508 {
6509 if (!number_is_in_list (args, b->number))
6510 continue;
6511 }
6512 }
6513 /* We only print out user settable breakpoints unless the
6514 allflag is set. */
6515 if (allflag || user_breakpoint_p (b))
6516 print_one_breakpoint (b, &last_loc, allflag);
6517 }
6518
6519 do_cleanups (bkpttbl_chain);
6520
6521 if (nr_printable_breakpoints == 0)
6522 {
6523 /* If there's a filter, let the caller decide how to report
6524 empty list. */
6525 if (!filter)
6526 {
6527 if (args == NULL || *args == '\0')
6528 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6529 else
6530 ui_out_message (uiout, 0,
6531 "No breakpoint or watchpoint matching '%s'.\n",
6532 args);
6533 }
6534 }
6535 else
6536 {
6537 if (last_loc && !server_command)
6538 set_next_address (last_loc->gdbarch, last_loc->address);
6539 }
6540
6541 /* FIXME? Should this be moved up so that it is only called when
6542 there have been breakpoints? */
6543 annotate_breakpoints_table_end ();
6544
6545 return nr_printable_breakpoints;
6546}
6547
6548/* Display the value of default-collect in a way that is generally
6549 compatible with the breakpoint list. */
6550
6551static void
6552default_collect_info (void)
6553{
6554 struct ui_out *uiout = current_uiout;
6555
6556 /* If it has no value (which is frequently the case), say nothing; a
6557 message like "No default-collect." gets in user's face when it's
6558 not wanted. */
6559 if (!*default_collect)
6560 return;
6561
6562 /* The following phrase lines up nicely with per-tracepoint collect
6563 actions. */
6564 ui_out_text (uiout, "default collect ");
6565 ui_out_field_string (uiout, "default-collect", default_collect);
6566 ui_out_text (uiout, " \n");
6567}
6568
6569static void
6570breakpoints_info (char *args, int from_tty)
6571{
6572 breakpoint_1 (args, 0, NULL);
6573
6574 default_collect_info ();
6575}
6576
6577static void
6578watchpoints_info (char *args, int from_tty)
6579{
6580 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6581 struct ui_out *uiout = current_uiout;
6582
6583 if (num_printed == 0)
6584 {
6585 if (args == NULL || *args == '\0')
6586 ui_out_message (uiout, 0, "No watchpoints.\n");
6587 else
6588 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6589 }
6590}
6591
6592static void
6593maintenance_info_breakpoints (char *args, int from_tty)
6594{
6595 breakpoint_1 (args, 1, NULL);
6596
6597 default_collect_info ();
6598}
6599
6600static int
6601breakpoint_has_pc (struct breakpoint *b,
6602 struct program_space *pspace,
6603 CORE_ADDR pc, struct obj_section *section)
6604{
6605 struct bp_location *bl = b->loc;
6606
6607 for (; bl; bl = bl->next)
6608 {
6609 if (bl->pspace == pspace
6610 && bl->address == pc
6611 && (!overlay_debugging || bl->section == section))
6612 return 1;
6613 }
6614 return 0;
6615}
6616
6617/* Print a message describing any user-breakpoints set at PC. This
6618 concerns with logical breakpoints, so we match program spaces, not
6619 address spaces. */
6620
6621static void
6622describe_other_breakpoints (struct gdbarch *gdbarch,
6623 struct program_space *pspace, CORE_ADDR pc,
6624 struct obj_section *section, int thread)
6625{
6626 int others = 0;
6627 struct breakpoint *b;
6628
6629 ALL_BREAKPOINTS (b)
6630 others += (user_breakpoint_p (b)
6631 && breakpoint_has_pc (b, pspace, pc, section));
6632 if (others > 0)
6633 {
6634 if (others == 1)
6635 printf_filtered (_("Note: breakpoint "));
6636 else /* if (others == ???) */
6637 printf_filtered (_("Note: breakpoints "));
6638 ALL_BREAKPOINTS (b)
6639 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6640 {
6641 others--;
6642 printf_filtered ("%d", b->number);
6643 if (b->thread == -1 && thread != -1)
6644 printf_filtered (" (all threads)");
6645 else if (b->thread != -1)
6646 printf_filtered (" (thread %d)", b->thread);
6647 printf_filtered ("%s%s ",
6648 ((b->enable_state == bp_disabled
6649 || b->enable_state == bp_call_disabled)
6650 ? " (disabled)"
6651 : b->enable_state == bp_permanent
6652 ? " (permanent)"
6653 : ""),
6654 (others > 1) ? ","
6655 : ((others == 1) ? " and" : ""));
6656 }
6657 printf_filtered (_("also set at pc "));
6658 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6659 printf_filtered (".\n");
6660 }
6661}
6662\f
6663
6664/* Return true iff it is meaningful to use the address member of
6665 BPT. For some breakpoint types, the address member is irrelevant
6666 and it makes no sense to attempt to compare it to other addresses
6667 (or use it for any other purpose either).
6668
6669 More specifically, each of the following breakpoint types will
6670 always have a zero valued address and we don't want to mark
6671 breakpoints of any of these types to be a duplicate of an actual
6672 breakpoint at address zero:
6673
6674 bp_watchpoint
6675 bp_catchpoint
6676
6677*/
6678
6679static int
6680breakpoint_address_is_meaningful (struct breakpoint *bpt)
6681{
6682 enum bptype type = bpt->type;
6683
6684 return (type != bp_watchpoint && type != bp_catchpoint);
6685}
6686
6687/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6688 true if LOC1 and LOC2 represent the same watchpoint location. */
6689
6690static int
6691watchpoint_locations_match (struct bp_location *loc1,
6692 struct bp_location *loc2)
6693{
6694 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6695 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6696
6697 /* Both of them must exist. */
6698 gdb_assert (w1 != NULL);
6699 gdb_assert (w2 != NULL);
6700
6701 /* If the target can evaluate the condition expression in hardware,
6702 then we we need to insert both watchpoints even if they are at
6703 the same place. Otherwise the watchpoint will only trigger when
6704 the condition of whichever watchpoint was inserted evaluates to
6705 true, not giving a chance for GDB to check the condition of the
6706 other watchpoint. */
6707 if ((w1->cond_exp
6708 && target_can_accel_watchpoint_condition (loc1->address,
6709 loc1->length,
6710 loc1->watchpoint_type,
6711 w1->cond_exp))
6712 || (w2->cond_exp
6713 && target_can_accel_watchpoint_condition (loc2->address,
6714 loc2->length,
6715 loc2->watchpoint_type,
6716 w2->cond_exp)))
6717 return 0;
6718
6719 /* Note that this checks the owner's type, not the location's. In
6720 case the target does not support read watchpoints, but does
6721 support access watchpoints, we'll have bp_read_watchpoint
6722 watchpoints with hw_access locations. Those should be considered
6723 duplicates of hw_read locations. The hw_read locations will
6724 become hw_access locations later. */
6725 return (loc1->owner->type == loc2->owner->type
6726 && loc1->pspace->aspace == loc2->pspace->aspace
6727 && loc1->address == loc2->address
6728 && loc1->length == loc2->length);
6729}
6730
6731/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6732 same breakpoint location. In most targets, this can only be true
6733 if ASPACE1 matches ASPACE2. On targets that have global
6734 breakpoints, the address space doesn't really matter. */
6735
6736static int
6737breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6738 struct address_space *aspace2, CORE_ADDR addr2)
6739{
6740 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6741 || aspace1 == aspace2)
6742 && addr1 == addr2);
6743}
6744
6745/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6746 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6747 matches ASPACE2. On targets that have global breakpoints, the address
6748 space doesn't really matter. */
6749
6750static int
6751breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6752 int len1, struct address_space *aspace2,
6753 CORE_ADDR addr2)
6754{
6755 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6756 || aspace1 == aspace2)
6757 && addr2 >= addr1 && addr2 < addr1 + len1);
6758}
6759
6760/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6761 a ranged breakpoint. In most targets, a match happens only if ASPACE
6762 matches the breakpoint's address space. On targets that have global
6763 breakpoints, the address space doesn't really matter. */
6764
6765static int
6766breakpoint_location_address_match (struct bp_location *bl,
6767 struct address_space *aspace,
6768 CORE_ADDR addr)
6769{
6770 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6771 aspace, addr)
6772 || (bl->length
6773 && breakpoint_address_match_range (bl->pspace->aspace,
6774 bl->address, bl->length,
6775 aspace, addr)));
6776}
6777
6778/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6779 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6780 true, otherwise returns false. */
6781
6782static int
6783tracepoint_locations_match (struct bp_location *loc1,
6784 struct bp_location *loc2)
6785{
6786 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6787 /* Since tracepoint locations are never duplicated with others', tracepoint
6788 locations at the same address of different tracepoints are regarded as
6789 different locations. */
6790 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6791 else
6792 return 0;
6793}
6794
6795/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6796 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6797 represent the same location. */
6798
6799static int
6800breakpoint_locations_match (struct bp_location *loc1,
6801 struct bp_location *loc2)
6802{
6803 int hw_point1, hw_point2;
6804
6805 /* Both of them must not be in moribund_locations. */
6806 gdb_assert (loc1->owner != NULL);
6807 gdb_assert (loc2->owner != NULL);
6808
6809 hw_point1 = is_hardware_watchpoint (loc1->owner);
6810 hw_point2 = is_hardware_watchpoint (loc2->owner);
6811
6812 if (hw_point1 != hw_point2)
6813 return 0;
6814 else if (hw_point1)
6815 return watchpoint_locations_match (loc1, loc2);
6816 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6817 return tracepoint_locations_match (loc1, loc2);
6818 else
6819 /* We compare bp_location.length in order to cover ranged breakpoints. */
6820 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6821 loc2->pspace->aspace, loc2->address)
6822 && loc1->length == loc2->length);
6823}
6824
6825static void
6826breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6827 int bnum, int have_bnum)
6828{
6829 /* The longest string possibly returned by hex_string_custom
6830 is 50 chars. These must be at least that big for safety. */
6831 char astr1[64];
6832 char astr2[64];
6833
6834 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6835 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6836 if (have_bnum)
6837 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6838 bnum, astr1, astr2);
6839 else
6840 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6841}
6842
6843/* Adjust a breakpoint's address to account for architectural
6844 constraints on breakpoint placement. Return the adjusted address.
6845 Note: Very few targets require this kind of adjustment. For most
6846 targets, this function is simply the identity function. */
6847
6848static CORE_ADDR
6849adjust_breakpoint_address (struct gdbarch *gdbarch,
6850 CORE_ADDR bpaddr, enum bptype bptype)
6851{
6852 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6853 {
6854 /* Very few targets need any kind of breakpoint adjustment. */
6855 return bpaddr;
6856 }
6857 else if (bptype == bp_watchpoint
6858 || bptype == bp_hardware_watchpoint
6859 || bptype == bp_read_watchpoint
6860 || bptype == bp_access_watchpoint
6861 || bptype == bp_catchpoint)
6862 {
6863 /* Watchpoints and the various bp_catch_* eventpoints should not
6864 have their addresses modified. */
6865 return bpaddr;
6866 }
6867 else
6868 {
6869 CORE_ADDR adjusted_bpaddr;
6870
6871 /* Some targets have architectural constraints on the placement
6872 of breakpoint instructions. Obtain the adjusted address. */
6873 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6874
6875 /* An adjusted breakpoint address can significantly alter
6876 a user's expectations. Print a warning if an adjustment
6877 is required. */
6878 if (adjusted_bpaddr != bpaddr)
6879 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6880
6881 return adjusted_bpaddr;
6882 }
6883}
6884
6885void
6886init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6887 struct breakpoint *owner)
6888{
6889 memset (loc, 0, sizeof (*loc));
6890
6891 gdb_assert (ops != NULL);
6892
6893 loc->ops = ops;
6894 loc->owner = owner;
6895 loc->cond = NULL;
6896 loc->cond_bytecode = NULL;
6897 loc->shlib_disabled = 0;
6898 loc->enabled = 1;
6899
6900 switch (owner->type)
6901 {
6902 case bp_breakpoint:
6903 case bp_until:
6904 case bp_finish:
6905 case bp_longjmp:
6906 case bp_longjmp_resume:
6907 case bp_longjmp_call_dummy:
6908 case bp_exception:
6909 case bp_exception_resume:
6910 case bp_step_resume:
6911 case bp_hp_step_resume:
6912 case bp_watchpoint_scope:
6913 case bp_call_dummy:
6914 case bp_std_terminate:
6915 case bp_shlib_event:
6916 case bp_thread_event:
6917 case bp_overlay_event:
6918 case bp_jit_event:
6919 case bp_longjmp_master:
6920 case bp_std_terminate_master:
6921 case bp_exception_master:
6922 case bp_gnu_ifunc_resolver:
6923 case bp_gnu_ifunc_resolver_return:
6924 case bp_dprintf:
6925 loc->loc_type = bp_loc_software_breakpoint;
6926 mark_breakpoint_location_modified (loc);
6927 break;
6928 case bp_hardware_breakpoint:
6929 loc->loc_type = bp_loc_hardware_breakpoint;
6930 mark_breakpoint_location_modified (loc);
6931 break;
6932 case bp_hardware_watchpoint:
6933 case bp_read_watchpoint:
6934 case bp_access_watchpoint:
6935 loc->loc_type = bp_loc_hardware_watchpoint;
6936 break;
6937 case bp_watchpoint:
6938 case bp_catchpoint:
6939 case bp_tracepoint:
6940 case bp_fast_tracepoint:
6941 case bp_static_tracepoint:
6942 loc->loc_type = bp_loc_other;
6943 break;
6944 default:
6945 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6946 }
6947
6948 loc->refc = 1;
6949}
6950
6951/* Allocate a struct bp_location. */
6952
6953static struct bp_location *
6954allocate_bp_location (struct breakpoint *bpt)
6955{
6956 return bpt->ops->allocate_location (bpt);
6957}
6958
6959static void
6960free_bp_location (struct bp_location *loc)
6961{
6962 loc->ops->dtor (loc);
6963 xfree (loc);
6964}
6965
6966/* Increment reference count. */
6967
6968static void
6969incref_bp_location (struct bp_location *bl)
6970{
6971 ++bl->refc;
6972}
6973
6974/* Decrement reference count. If the reference count reaches 0,
6975 destroy the bp_location. Sets *BLP to NULL. */
6976
6977static void
6978decref_bp_location (struct bp_location **blp)
6979{
6980 gdb_assert ((*blp)->refc > 0);
6981
6982 if (--(*blp)->refc == 0)
6983 free_bp_location (*blp);
6984 *blp = NULL;
6985}
6986
6987/* Add breakpoint B at the end of the global breakpoint chain. */
6988
6989static void
6990add_to_breakpoint_chain (struct breakpoint *b)
6991{
6992 struct breakpoint *b1;
6993
6994 /* Add this breakpoint to the end of the chain so that a list of
6995 breakpoints will come out in order of increasing numbers. */
6996
6997 b1 = breakpoint_chain;
6998 if (b1 == 0)
6999 breakpoint_chain = b;
7000 else
7001 {
7002 while (b1->next)
7003 b1 = b1->next;
7004 b1->next = b;
7005 }
7006}
7007
7008/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7009
7010static void
7011init_raw_breakpoint_without_location (struct breakpoint *b,
7012 struct gdbarch *gdbarch,
7013 enum bptype bptype,
7014 const struct breakpoint_ops *ops)
7015{
7016 memset (b, 0, sizeof (*b));
7017
7018 gdb_assert (ops != NULL);
7019
7020 b->ops = ops;
7021 b->type = bptype;
7022 b->gdbarch = gdbarch;
7023 b->language = current_language->la_language;
7024 b->input_radix = input_radix;
7025 b->thread = -1;
7026 b->enable_state = bp_enabled;
7027 b->next = 0;
7028 b->silent = 0;
7029 b->ignore_count = 0;
7030 b->commands = NULL;
7031 b->frame_id = null_frame_id;
7032 b->condition_not_parsed = 0;
7033 b->py_bp_object = NULL;
7034 b->related_breakpoint = b;
7035}
7036
7037/* Helper to set_raw_breakpoint below. Creates a breakpoint
7038 that has type BPTYPE and has no locations as yet. */
7039
7040static struct breakpoint *
7041set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7042 enum bptype bptype,
7043 const struct breakpoint_ops *ops)
7044{
7045 struct breakpoint *b = XNEW (struct breakpoint);
7046
7047 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7048 add_to_breakpoint_chain (b);
7049 return b;
7050}
7051
7052/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7053 resolutions should be made as the user specified the location explicitly
7054 enough. */
7055
7056static void
7057set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7058{
7059 gdb_assert (loc->owner != NULL);
7060
7061 if (loc->owner->type == bp_breakpoint
7062 || loc->owner->type == bp_hardware_breakpoint
7063 || is_tracepoint (loc->owner))
7064 {
7065 int is_gnu_ifunc;
7066 const char *function_name;
7067 CORE_ADDR func_addr;
7068
7069 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7070 &func_addr, NULL, &is_gnu_ifunc);
7071
7072 if (is_gnu_ifunc && !explicit_loc)
7073 {
7074 struct breakpoint *b = loc->owner;
7075
7076 gdb_assert (loc->pspace == current_program_space);
7077 if (gnu_ifunc_resolve_name (function_name,
7078 &loc->requested_address))
7079 {
7080 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7081 loc->address = adjust_breakpoint_address (loc->gdbarch,
7082 loc->requested_address,
7083 b->type);
7084 }
7085 else if (b->type == bp_breakpoint && b->loc == loc
7086 && loc->next == NULL && b->related_breakpoint == b)
7087 {
7088 /* Create only the whole new breakpoint of this type but do not
7089 mess more complicated breakpoints with multiple locations. */
7090 b->type = bp_gnu_ifunc_resolver;
7091 /* Remember the resolver's address for use by the return
7092 breakpoint. */
7093 loc->related_address = func_addr;
7094 }
7095 }
7096
7097 if (function_name)
7098 loc->function_name = xstrdup (function_name);
7099 }
7100}
7101
7102/* Attempt to determine architecture of location identified by SAL. */
7103struct gdbarch *
7104get_sal_arch (struct symtab_and_line sal)
7105{
7106 if (sal.section)
7107 return get_objfile_arch (sal.section->objfile);
7108 if (sal.symtab)
7109 return get_objfile_arch (sal.symtab->objfile);
7110
7111 return NULL;
7112}
7113
7114/* Low level routine for partially initializing a breakpoint of type
7115 BPTYPE. The newly created breakpoint's address, section, source
7116 file name, and line number are provided by SAL.
7117
7118 It is expected that the caller will complete the initialization of
7119 the newly created breakpoint struct as well as output any status
7120 information regarding the creation of a new breakpoint. */
7121
7122static void
7123init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7124 struct symtab_and_line sal, enum bptype bptype,
7125 const struct breakpoint_ops *ops)
7126{
7127 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7128
7129 add_location_to_breakpoint (b, &sal);
7130
7131 if (bptype != bp_catchpoint)
7132 gdb_assert (sal.pspace != NULL);
7133
7134 /* Store the program space that was used to set the breakpoint,
7135 except for ordinary breakpoints, which are independent of the
7136 program space. */
7137 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7138 b->pspace = sal.pspace;
7139}
7140
7141/* set_raw_breakpoint is a low level routine for allocating and
7142 partially initializing a breakpoint of type BPTYPE. The newly
7143 created breakpoint's address, section, source file name, and line
7144 number are provided by SAL. The newly created and partially
7145 initialized breakpoint is added to the breakpoint chain and
7146 is also returned as the value of this function.
7147
7148 It is expected that the caller will complete the initialization of
7149 the newly created breakpoint struct as well as output any status
7150 information regarding the creation of a new breakpoint. In
7151 particular, set_raw_breakpoint does NOT set the breakpoint
7152 number! Care should be taken to not allow an error to occur
7153 prior to completing the initialization of the breakpoint. If this
7154 should happen, a bogus breakpoint will be left on the chain. */
7155
7156struct breakpoint *
7157set_raw_breakpoint (struct gdbarch *gdbarch,
7158 struct symtab_and_line sal, enum bptype bptype,
7159 const struct breakpoint_ops *ops)
7160{
7161 struct breakpoint *b = XNEW (struct breakpoint);
7162
7163 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7164 add_to_breakpoint_chain (b);
7165 return b;
7166}
7167
7168
7169/* Note that the breakpoint object B describes a permanent breakpoint
7170 instruction, hard-wired into the inferior's code. */
7171void
7172make_breakpoint_permanent (struct breakpoint *b)
7173{
7174 struct bp_location *bl;
7175
7176 b->enable_state = bp_permanent;
7177
7178 /* By definition, permanent breakpoints are already present in the
7179 code. Mark all locations as inserted. For now,
7180 make_breakpoint_permanent is called in just one place, so it's
7181 hard to say if it's reasonable to have permanent breakpoint with
7182 multiple locations or not, but it's easy to implement. */
7183 for (bl = b->loc; bl; bl = bl->next)
7184 bl->inserted = 1;
7185}
7186
7187/* Call this routine when stepping and nexting to enable a breakpoint
7188 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7189 initiated the operation. */
7190
7191void
7192set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7193{
7194 struct breakpoint *b, *b_tmp;
7195 int thread = tp->num;
7196
7197 /* To avoid having to rescan all objfile symbols at every step,
7198 we maintain a list of continually-inserted but always disabled
7199 longjmp "master" breakpoints. Here, we simply create momentary
7200 clones of those and enable them for the requested thread. */
7201 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7202 if (b->pspace == current_program_space
7203 && (b->type == bp_longjmp_master
7204 || b->type == bp_exception_master))
7205 {
7206 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7207 struct breakpoint *clone;
7208
7209 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7210 after their removal. */
7211 clone = momentary_breakpoint_from_master (b, type,
7212 &longjmp_breakpoint_ops);
7213 clone->thread = thread;
7214 }
7215
7216 tp->initiating_frame = frame;
7217}
7218
7219/* Delete all longjmp breakpoints from THREAD. */
7220void
7221delete_longjmp_breakpoint (int thread)
7222{
7223 struct breakpoint *b, *b_tmp;
7224
7225 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7226 if (b->type == bp_longjmp || b->type == bp_exception)
7227 {
7228 if (b->thread == thread)
7229 delete_breakpoint (b);
7230 }
7231}
7232
7233void
7234delete_longjmp_breakpoint_at_next_stop (int thread)
7235{
7236 struct breakpoint *b, *b_tmp;
7237
7238 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7239 if (b->type == bp_longjmp || b->type == bp_exception)
7240 {
7241 if (b->thread == thread)
7242 b->disposition = disp_del_at_next_stop;
7243 }
7244}
7245
7246/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7247 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7248 pointer to any of them. Return NULL if this system cannot place longjmp
7249 breakpoints. */
7250
7251struct breakpoint *
7252set_longjmp_breakpoint_for_call_dummy (void)
7253{
7254 struct breakpoint *b, *retval = NULL;
7255
7256 ALL_BREAKPOINTS (b)
7257 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7258 {
7259 struct breakpoint *new_b;
7260
7261 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7262 &momentary_breakpoint_ops);
7263 new_b->thread = pid_to_thread_id (inferior_ptid);
7264
7265 /* Link NEW_B into the chain of RETVAL breakpoints. */
7266
7267 gdb_assert (new_b->related_breakpoint == new_b);
7268 if (retval == NULL)
7269 retval = new_b;
7270 new_b->related_breakpoint = retval;
7271 while (retval->related_breakpoint != new_b->related_breakpoint)
7272 retval = retval->related_breakpoint;
7273 retval->related_breakpoint = new_b;
7274 }
7275
7276 return retval;
7277}
7278
7279/* Verify all existing dummy frames and their associated breakpoints for
7280 THREAD. Remove those which can no longer be found in the current frame
7281 stack.
7282
7283 You should call this function only at places where it is safe to currently
7284 unwind the whole stack. Failed stack unwind would discard live dummy
7285 frames. */
7286
7287void
7288check_longjmp_breakpoint_for_call_dummy (int thread)
7289{
7290 struct breakpoint *b, *b_tmp;
7291
7292 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7293 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7294 {
7295 struct breakpoint *dummy_b = b->related_breakpoint;
7296
7297 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7298 dummy_b = dummy_b->related_breakpoint;
7299 if (dummy_b->type != bp_call_dummy
7300 || frame_find_by_id (dummy_b->frame_id) != NULL)
7301 continue;
7302
7303 dummy_frame_discard (dummy_b->frame_id);
7304
7305 while (b->related_breakpoint != b)
7306 {
7307 if (b_tmp == b->related_breakpoint)
7308 b_tmp = b->related_breakpoint->next;
7309 delete_breakpoint (b->related_breakpoint);
7310 }
7311 delete_breakpoint (b);
7312 }
7313}
7314
7315void
7316enable_overlay_breakpoints (void)
7317{
7318 struct breakpoint *b;
7319
7320 ALL_BREAKPOINTS (b)
7321 if (b->type == bp_overlay_event)
7322 {
7323 b->enable_state = bp_enabled;
7324 update_global_location_list (1);
7325 overlay_events_enabled = 1;
7326 }
7327}
7328
7329void
7330disable_overlay_breakpoints (void)
7331{
7332 struct breakpoint *b;
7333
7334 ALL_BREAKPOINTS (b)
7335 if (b->type == bp_overlay_event)
7336 {
7337 b->enable_state = bp_disabled;
7338 update_global_location_list (0);
7339 overlay_events_enabled = 0;
7340 }
7341}
7342
7343/* Set an active std::terminate breakpoint for each std::terminate
7344 master breakpoint. */
7345void
7346set_std_terminate_breakpoint (void)
7347{
7348 struct breakpoint *b, *b_tmp;
7349
7350 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7351 if (b->pspace == current_program_space
7352 && b->type == bp_std_terminate_master)
7353 {
7354 momentary_breakpoint_from_master (b, bp_std_terminate,
7355 &momentary_breakpoint_ops);
7356 }
7357}
7358
7359/* Delete all the std::terminate breakpoints. */
7360void
7361delete_std_terminate_breakpoint (void)
7362{
7363 struct breakpoint *b, *b_tmp;
7364
7365 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7366 if (b->type == bp_std_terminate)
7367 delete_breakpoint (b);
7368}
7369
7370struct breakpoint *
7371create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7372{
7373 struct breakpoint *b;
7374
7375 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7376 &internal_breakpoint_ops);
7377
7378 b->enable_state = bp_enabled;
7379 /* addr_string has to be used or breakpoint_re_set will delete me. */
7380 b->addr_string
7381 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7382
7383 update_global_location_list_nothrow (1);
7384
7385 return b;
7386}
7387
7388void
7389remove_thread_event_breakpoints (void)
7390{
7391 struct breakpoint *b, *b_tmp;
7392
7393 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7394 if (b->type == bp_thread_event
7395 && b->loc->pspace == current_program_space)
7396 delete_breakpoint (b);
7397}
7398
7399struct lang_and_radix
7400 {
7401 enum language lang;
7402 int radix;
7403 };
7404
7405/* Create a breakpoint for JIT code registration and unregistration. */
7406
7407struct breakpoint *
7408create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7409{
7410 struct breakpoint *b;
7411
7412 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7413 &internal_breakpoint_ops);
7414 update_global_location_list_nothrow (1);
7415 return b;
7416}
7417
7418/* Remove JIT code registration and unregistration breakpoint(s). */
7419
7420void
7421remove_jit_event_breakpoints (void)
7422{
7423 struct breakpoint *b, *b_tmp;
7424
7425 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7426 if (b->type == bp_jit_event
7427 && b->loc->pspace == current_program_space)
7428 delete_breakpoint (b);
7429}
7430
7431void
7432remove_solib_event_breakpoints (void)
7433{
7434 struct breakpoint *b, *b_tmp;
7435
7436 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7437 if (b->type == bp_shlib_event
7438 && b->loc->pspace == current_program_space)
7439 delete_breakpoint (b);
7440}
7441
7442struct breakpoint *
7443create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7444{
7445 struct breakpoint *b;
7446
7447 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7448 &internal_breakpoint_ops);
7449 update_global_location_list_nothrow (1);
7450 return b;
7451}
7452
7453/* Disable any breakpoints that are on code in shared libraries. Only
7454 apply to enabled breakpoints, disabled ones can just stay disabled. */
7455
7456void
7457disable_breakpoints_in_shlibs (void)
7458{
7459 struct bp_location *loc, **locp_tmp;
7460
7461 ALL_BP_LOCATIONS (loc, locp_tmp)
7462 {
7463 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7464 struct breakpoint *b = loc->owner;
7465
7466 /* We apply the check to all breakpoints, including disabled for
7467 those with loc->duplicate set. This is so that when breakpoint
7468 becomes enabled, or the duplicate is removed, gdb will try to
7469 insert all breakpoints. If we don't set shlib_disabled here,
7470 we'll try to insert those breakpoints and fail. */
7471 if (((b->type == bp_breakpoint)
7472 || (b->type == bp_jit_event)
7473 || (b->type == bp_hardware_breakpoint)
7474 || (is_tracepoint (b)))
7475 && loc->pspace == current_program_space
7476 && !loc->shlib_disabled
7477 && solib_name_from_address (loc->pspace, loc->address)
7478 )
7479 {
7480 loc->shlib_disabled = 1;
7481 }
7482 }
7483}
7484
7485/* Disable any breakpoints and tracepoints that are in SOLIB upon
7486 notification of unloaded_shlib. Only apply to enabled breakpoints,
7487 disabled ones can just stay disabled. */
7488
7489static void
7490disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7491{
7492 struct bp_location *loc, **locp_tmp;
7493 int disabled_shlib_breaks = 0;
7494
7495 /* SunOS a.out shared libraries are always mapped, so do not
7496 disable breakpoints; they will only be reported as unloaded
7497 through clear_solib when GDB discards its shared library
7498 list. See clear_solib for more information. */
7499 if (exec_bfd != NULL
7500 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7501 return;
7502
7503 ALL_BP_LOCATIONS (loc, locp_tmp)
7504 {
7505 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7506 struct breakpoint *b = loc->owner;
7507
7508 if (solib->pspace == loc->pspace
7509 && !loc->shlib_disabled
7510 && (((b->type == bp_breakpoint
7511 || b->type == bp_jit_event
7512 || b->type == bp_hardware_breakpoint)
7513 && (loc->loc_type == bp_loc_hardware_breakpoint
7514 || loc->loc_type == bp_loc_software_breakpoint))
7515 || is_tracepoint (b))
7516 && solib_contains_address_p (solib, loc->address))
7517 {
7518 loc->shlib_disabled = 1;
7519 /* At this point, we cannot rely on remove_breakpoint
7520 succeeding so we must mark the breakpoint as not inserted
7521 to prevent future errors occurring in remove_breakpoints. */
7522 loc->inserted = 0;
7523
7524 /* This may cause duplicate notifications for the same breakpoint. */
7525 observer_notify_breakpoint_modified (b);
7526
7527 if (!disabled_shlib_breaks)
7528 {
7529 target_terminal_ours_for_output ();
7530 warning (_("Temporarily disabling breakpoints "
7531 "for unloaded shared library \"%s\""),
7532 solib->so_name);
7533 }
7534 disabled_shlib_breaks = 1;
7535 }
7536 }
7537}
7538
7539/* Disable any breakpoints and tracepoints in OBJFILE upon
7540 notification of free_objfile. Only apply to enabled breakpoints,
7541 disabled ones can just stay disabled. */
7542
7543static void
7544disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7545{
7546 struct breakpoint *b;
7547
7548 if (objfile == NULL)
7549 return;
7550
7551 /* If the file is a shared library not loaded by the user then
7552 solib_unloaded was notified and disable_breakpoints_in_unloaded_shlib
7553 was called. In that case there is no need to take action again. */
7554 if ((objfile->flags & OBJF_SHARED) && !(objfile->flags & OBJF_USERLOADED))
7555 return;
7556
7557 ALL_BREAKPOINTS (b)
7558 {
7559 struct bp_location *loc;
7560 int bp_modified = 0;
7561
7562 if (!is_breakpoint (b) && !is_tracepoint (b))
7563 continue;
7564
7565 for (loc = b->loc; loc != NULL; loc = loc->next)
7566 {
7567 CORE_ADDR loc_addr = loc->address;
7568
7569 if (loc->loc_type != bp_loc_hardware_breakpoint
7570 && loc->loc_type != bp_loc_software_breakpoint)
7571 continue;
7572
7573 if (loc->shlib_disabled != 0)
7574 continue;
7575
7576 if (objfile->pspace != loc->pspace)
7577 continue;
7578
7579 if (loc->loc_type != bp_loc_hardware_breakpoint
7580 && loc->loc_type != bp_loc_software_breakpoint)
7581 continue;
7582
7583 if (is_addr_in_objfile (loc_addr, objfile))
7584 {
7585 loc->shlib_disabled = 1;
7586 loc->inserted = 0;
7587
7588 mark_breakpoint_location_modified (loc);
7589
7590 bp_modified = 1;
7591 }
7592 }
7593
7594 if (bp_modified)
7595 observer_notify_breakpoint_modified (b);
7596 }
7597}
7598
7599/* FORK & VFORK catchpoints. */
7600
7601/* An instance of this type is used to represent a fork or vfork
7602 catchpoint. It includes a "struct breakpoint" as a kind of base
7603 class; users downcast to "struct breakpoint *" when needed. A
7604 breakpoint is really of this type iff its ops pointer points to
7605 CATCH_FORK_BREAKPOINT_OPS. */
7606
7607struct fork_catchpoint
7608{
7609 /* The base class. */
7610 struct breakpoint base;
7611
7612 /* Process id of a child process whose forking triggered this
7613 catchpoint. This field is only valid immediately after this
7614 catchpoint has triggered. */
7615 ptid_t forked_inferior_pid;
7616};
7617
7618/* Implement the "insert" breakpoint_ops method for fork
7619 catchpoints. */
7620
7621static int
7622insert_catch_fork (struct bp_location *bl)
7623{
7624 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7625}
7626
7627/* Implement the "remove" breakpoint_ops method for fork
7628 catchpoints. */
7629
7630static int
7631remove_catch_fork (struct bp_location *bl)
7632{
7633 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7634}
7635
7636/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7637 catchpoints. */
7638
7639static int
7640breakpoint_hit_catch_fork (const struct bp_location *bl,
7641 struct address_space *aspace, CORE_ADDR bp_addr,
7642 const struct target_waitstatus *ws)
7643{
7644 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7645
7646 if (ws->kind != TARGET_WAITKIND_FORKED)
7647 return 0;
7648
7649 c->forked_inferior_pid = ws->value.related_pid;
7650 return 1;
7651}
7652
7653/* Implement the "print_it" breakpoint_ops method for fork
7654 catchpoints. */
7655
7656static enum print_stop_action
7657print_it_catch_fork (bpstat bs)
7658{
7659 struct ui_out *uiout = current_uiout;
7660 struct breakpoint *b = bs->breakpoint_at;
7661 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7662
7663 annotate_catchpoint (b->number);
7664 if (b->disposition == disp_del)
7665 ui_out_text (uiout, "\nTemporary catchpoint ");
7666 else
7667 ui_out_text (uiout, "\nCatchpoint ");
7668 if (ui_out_is_mi_like_p (uiout))
7669 {
7670 ui_out_field_string (uiout, "reason",
7671 async_reason_lookup (EXEC_ASYNC_FORK));
7672 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7673 }
7674 ui_out_field_int (uiout, "bkptno", b->number);
7675 ui_out_text (uiout, " (forked process ");
7676 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7677 ui_out_text (uiout, "), ");
7678 return PRINT_SRC_AND_LOC;
7679}
7680
7681/* Implement the "print_one" breakpoint_ops method for fork
7682 catchpoints. */
7683
7684static void
7685print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7686{
7687 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7688 struct value_print_options opts;
7689 struct ui_out *uiout = current_uiout;
7690
7691 get_user_print_options (&opts);
7692
7693 /* Field 4, the address, is omitted (which makes the columns not
7694 line up too nicely with the headers, but the effect is relatively
7695 readable). */
7696 if (opts.addressprint)
7697 ui_out_field_skip (uiout, "addr");
7698 annotate_field (5);
7699 ui_out_text (uiout, "fork");
7700 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7701 {
7702 ui_out_text (uiout, ", process ");
7703 ui_out_field_int (uiout, "what",
7704 ptid_get_pid (c->forked_inferior_pid));
7705 ui_out_spaces (uiout, 1);
7706 }
7707
7708 if (ui_out_is_mi_like_p (uiout))
7709 ui_out_field_string (uiout, "catch-type", "fork");
7710}
7711
7712/* Implement the "print_mention" breakpoint_ops method for fork
7713 catchpoints. */
7714
7715static void
7716print_mention_catch_fork (struct breakpoint *b)
7717{
7718 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7719}
7720
7721/* Implement the "print_recreate" breakpoint_ops method for fork
7722 catchpoints. */
7723
7724static void
7725print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7726{
7727 fprintf_unfiltered (fp, "catch fork");
7728 print_recreate_thread (b, fp);
7729}
7730
7731/* The breakpoint_ops structure to be used in fork catchpoints. */
7732
7733static struct breakpoint_ops catch_fork_breakpoint_ops;
7734
7735/* Implement the "insert" breakpoint_ops method for vfork
7736 catchpoints. */
7737
7738static int
7739insert_catch_vfork (struct bp_location *bl)
7740{
7741 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7742}
7743
7744/* Implement the "remove" breakpoint_ops method for vfork
7745 catchpoints. */
7746
7747static int
7748remove_catch_vfork (struct bp_location *bl)
7749{
7750 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7751}
7752
7753/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7754 catchpoints. */
7755
7756static int
7757breakpoint_hit_catch_vfork (const struct bp_location *bl,
7758 struct address_space *aspace, CORE_ADDR bp_addr,
7759 const struct target_waitstatus *ws)
7760{
7761 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7762
7763 if (ws->kind != TARGET_WAITKIND_VFORKED)
7764 return 0;
7765
7766 c->forked_inferior_pid = ws->value.related_pid;
7767 return 1;
7768}
7769
7770/* Implement the "print_it" breakpoint_ops method for vfork
7771 catchpoints. */
7772
7773static enum print_stop_action
7774print_it_catch_vfork (bpstat bs)
7775{
7776 struct ui_out *uiout = current_uiout;
7777 struct breakpoint *b = bs->breakpoint_at;
7778 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7779
7780 annotate_catchpoint (b->number);
7781 if (b->disposition == disp_del)
7782 ui_out_text (uiout, "\nTemporary catchpoint ");
7783 else
7784 ui_out_text (uiout, "\nCatchpoint ");
7785 if (ui_out_is_mi_like_p (uiout))
7786 {
7787 ui_out_field_string (uiout, "reason",
7788 async_reason_lookup (EXEC_ASYNC_VFORK));
7789 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7790 }
7791 ui_out_field_int (uiout, "bkptno", b->number);
7792 ui_out_text (uiout, " (vforked process ");
7793 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7794 ui_out_text (uiout, "), ");
7795 return PRINT_SRC_AND_LOC;
7796}
7797
7798/* Implement the "print_one" breakpoint_ops method for vfork
7799 catchpoints. */
7800
7801static void
7802print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7803{
7804 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7805 struct value_print_options opts;
7806 struct ui_out *uiout = current_uiout;
7807
7808 get_user_print_options (&opts);
7809 /* Field 4, the address, is omitted (which makes the columns not
7810 line up too nicely with the headers, but the effect is relatively
7811 readable). */
7812 if (opts.addressprint)
7813 ui_out_field_skip (uiout, "addr");
7814 annotate_field (5);
7815 ui_out_text (uiout, "vfork");
7816 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7817 {
7818 ui_out_text (uiout, ", process ");
7819 ui_out_field_int (uiout, "what",
7820 ptid_get_pid (c->forked_inferior_pid));
7821 ui_out_spaces (uiout, 1);
7822 }
7823
7824 if (ui_out_is_mi_like_p (uiout))
7825 ui_out_field_string (uiout, "catch-type", "vfork");
7826}
7827
7828/* Implement the "print_mention" breakpoint_ops method for vfork
7829 catchpoints. */
7830
7831static void
7832print_mention_catch_vfork (struct breakpoint *b)
7833{
7834 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7835}
7836
7837/* Implement the "print_recreate" breakpoint_ops method for vfork
7838 catchpoints. */
7839
7840static void
7841print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7842{
7843 fprintf_unfiltered (fp, "catch vfork");
7844 print_recreate_thread (b, fp);
7845}
7846
7847/* The breakpoint_ops structure to be used in vfork catchpoints. */
7848
7849static struct breakpoint_ops catch_vfork_breakpoint_ops;
7850
7851/* An instance of this type is used to represent an solib catchpoint.
7852 It includes a "struct breakpoint" as a kind of base class; users
7853 downcast to "struct breakpoint *" when needed. A breakpoint is
7854 really of this type iff its ops pointer points to
7855 CATCH_SOLIB_BREAKPOINT_OPS. */
7856
7857struct solib_catchpoint
7858{
7859 /* The base class. */
7860 struct breakpoint base;
7861
7862 /* True for "catch load", false for "catch unload". */
7863 unsigned char is_load;
7864
7865 /* Regular expression to match, if any. COMPILED is only valid when
7866 REGEX is non-NULL. */
7867 char *regex;
7868 regex_t compiled;
7869};
7870
7871static void
7872dtor_catch_solib (struct breakpoint *b)
7873{
7874 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7875
7876 if (self->regex)
7877 regfree (&self->compiled);
7878 xfree (self->regex);
7879
7880 base_breakpoint_ops.dtor (b);
7881}
7882
7883static int
7884insert_catch_solib (struct bp_location *ignore)
7885{
7886 return 0;
7887}
7888
7889static int
7890remove_catch_solib (struct bp_location *ignore)
7891{
7892 return 0;
7893}
7894
7895static int
7896breakpoint_hit_catch_solib (const struct bp_location *bl,
7897 struct address_space *aspace,
7898 CORE_ADDR bp_addr,
7899 const struct target_waitstatus *ws)
7900{
7901 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7902 struct breakpoint *other;
7903
7904 if (ws->kind == TARGET_WAITKIND_LOADED)
7905 return 1;
7906
7907 ALL_BREAKPOINTS (other)
7908 {
7909 struct bp_location *other_bl;
7910
7911 if (other == bl->owner)
7912 continue;
7913
7914 if (other->type != bp_shlib_event)
7915 continue;
7916
7917 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7918 continue;
7919
7920 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7921 {
7922 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7923 return 1;
7924 }
7925 }
7926
7927 return 0;
7928}
7929
7930static void
7931check_status_catch_solib (struct bpstats *bs)
7932{
7933 struct solib_catchpoint *self
7934 = (struct solib_catchpoint *) bs->breakpoint_at;
7935 int ix;
7936
7937 if (self->is_load)
7938 {
7939 struct so_list *iter;
7940
7941 for (ix = 0;
7942 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7943 ix, iter);
7944 ++ix)
7945 {
7946 if (!self->regex
7947 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7948 return;
7949 }
7950 }
7951 else
7952 {
7953 char *iter;
7954
7955 for (ix = 0;
7956 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7957 ix, iter);
7958 ++ix)
7959 {
7960 if (!self->regex
7961 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7962 return;
7963 }
7964 }
7965
7966 bs->stop = 0;
7967 bs->print_it = print_it_noop;
7968}
7969
7970static enum print_stop_action
7971print_it_catch_solib (bpstat bs)
7972{
7973 struct breakpoint *b = bs->breakpoint_at;
7974 struct ui_out *uiout = current_uiout;
7975
7976 annotate_catchpoint (b->number);
7977 if (b->disposition == disp_del)
7978 ui_out_text (uiout, "\nTemporary catchpoint ");
7979 else
7980 ui_out_text (uiout, "\nCatchpoint ");
7981 ui_out_field_int (uiout, "bkptno", b->number);
7982 ui_out_text (uiout, "\n");
7983 if (ui_out_is_mi_like_p (uiout))
7984 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7985 print_solib_event (1);
7986 return PRINT_SRC_AND_LOC;
7987}
7988
7989static void
7990print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7991{
7992 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7993 struct value_print_options opts;
7994 struct ui_out *uiout = current_uiout;
7995 char *msg;
7996
7997 get_user_print_options (&opts);
7998 /* Field 4, the address, is omitted (which makes the columns not
7999 line up too nicely with the headers, but the effect is relatively
8000 readable). */
8001 if (opts.addressprint)
8002 {
8003 annotate_field (4);
8004 ui_out_field_skip (uiout, "addr");
8005 }
8006
8007 annotate_field (5);
8008 if (self->is_load)
8009 {
8010 if (self->regex)
8011 msg = xstrprintf (_("load of library matching %s"), self->regex);
8012 else
8013 msg = xstrdup (_("load of library"));
8014 }
8015 else
8016 {
8017 if (self->regex)
8018 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8019 else
8020 msg = xstrdup (_("unload of library"));
8021 }
8022 ui_out_field_string (uiout, "what", msg);
8023 xfree (msg);
8024
8025 if (ui_out_is_mi_like_p (uiout))
8026 ui_out_field_string (uiout, "catch-type",
8027 self->is_load ? "load" : "unload");
8028}
8029
8030static void
8031print_mention_catch_solib (struct breakpoint *b)
8032{
8033 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8034
8035 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8036 self->is_load ? "load" : "unload");
8037}
8038
8039static void
8040print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8041{
8042 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8043
8044 fprintf_unfiltered (fp, "%s %s",
8045 b->disposition == disp_del ? "tcatch" : "catch",
8046 self->is_load ? "load" : "unload");
8047 if (self->regex)
8048 fprintf_unfiltered (fp, " %s", self->regex);
8049 fprintf_unfiltered (fp, "\n");
8050}
8051
8052static struct breakpoint_ops catch_solib_breakpoint_ops;
8053
8054/* Shared helper function (MI and CLI) for creating and installing
8055 a shared object event catchpoint. If IS_LOAD is non-zero then
8056 the events to be caught are load events, otherwise they are
8057 unload events. If IS_TEMP is non-zero the catchpoint is a
8058 temporary one. If ENABLED is non-zero the catchpoint is
8059 created in an enabled state. */
8060
8061void
8062add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8063{
8064 struct solib_catchpoint *c;
8065 struct gdbarch *gdbarch = get_current_arch ();
8066 struct cleanup *cleanup;
8067
8068 if (!arg)
8069 arg = "";
8070 arg = skip_spaces (arg);
8071
8072 c = XCNEW (struct solib_catchpoint);
8073 cleanup = make_cleanup (xfree, c);
8074
8075 if (*arg != '\0')
8076 {
8077 int errcode;
8078
8079 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8080 if (errcode != 0)
8081 {
8082 char *err = get_regcomp_error (errcode, &c->compiled);
8083
8084 make_cleanup (xfree, err);
8085 error (_("Invalid regexp (%s): %s"), err, arg);
8086 }
8087 c->regex = xstrdup (arg);
8088 }
8089
8090 c->is_load = is_load;
8091 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8092 &catch_solib_breakpoint_ops);
8093
8094 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8095
8096 discard_cleanups (cleanup);
8097 install_breakpoint (0, &c->base, 1);
8098}
8099
8100/* A helper function that does all the work for "catch load" and
8101 "catch unload". */
8102
8103static void
8104catch_load_or_unload (char *arg, int from_tty, int is_load,
8105 struct cmd_list_element *command)
8106{
8107 int tempflag;
8108 const int enabled = 1;
8109
8110 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8111
8112 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8113}
8114
8115static void
8116catch_load_command_1 (char *arg, int from_tty,
8117 struct cmd_list_element *command)
8118{
8119 catch_load_or_unload (arg, from_tty, 1, command);
8120}
8121
8122static void
8123catch_unload_command_1 (char *arg, int from_tty,
8124 struct cmd_list_element *command)
8125{
8126 catch_load_or_unload (arg, from_tty, 0, command);
8127}
8128
8129/* An instance of this type is used to represent a syscall catchpoint.
8130 It includes a "struct breakpoint" as a kind of base class; users
8131 downcast to "struct breakpoint *" when needed. A breakpoint is
8132 really of this type iff its ops pointer points to
8133 CATCH_SYSCALL_BREAKPOINT_OPS. */
8134
8135struct syscall_catchpoint
8136{
8137 /* The base class. */
8138 struct breakpoint base;
8139
8140 /* Syscall numbers used for the 'catch syscall' feature. If no
8141 syscall has been specified for filtering, its value is NULL.
8142 Otherwise, it holds a list of all syscalls to be caught. The
8143 list elements are allocated with xmalloc. */
8144 VEC(int) *syscalls_to_be_caught;
8145};
8146
8147/* Implement the "dtor" breakpoint_ops method for syscall
8148 catchpoints. */
8149
8150static void
8151dtor_catch_syscall (struct breakpoint *b)
8152{
8153 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8154
8155 VEC_free (int, c->syscalls_to_be_caught);
8156
8157 base_breakpoint_ops.dtor (b);
8158}
8159
8160static const struct inferior_data *catch_syscall_inferior_data = NULL;
8161
8162struct catch_syscall_inferior_data
8163{
8164 /* We keep a count of the number of times the user has requested a
8165 particular syscall to be tracked, and pass this information to the
8166 target. This lets capable targets implement filtering directly. */
8167
8168 /* Number of times that "any" syscall is requested. */
8169 int any_syscall_count;
8170
8171 /* Count of each system call. */
8172 VEC(int) *syscalls_counts;
8173
8174 /* This counts all syscall catch requests, so we can readily determine
8175 if any catching is necessary. */
8176 int total_syscalls_count;
8177};
8178
8179static struct catch_syscall_inferior_data*
8180get_catch_syscall_inferior_data (struct inferior *inf)
8181{
8182 struct catch_syscall_inferior_data *inf_data;
8183
8184 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8185 if (inf_data == NULL)
8186 {
8187 inf_data = XZALLOC (struct catch_syscall_inferior_data);
8188 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8189 }
8190
8191 return inf_data;
8192}
8193
8194static void
8195catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8196{
8197 xfree (arg);
8198}
8199
8200
8201/* Implement the "insert" breakpoint_ops method for syscall
8202 catchpoints. */
8203
8204static int
8205insert_catch_syscall (struct bp_location *bl)
8206{
8207 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8208 struct inferior *inf = current_inferior ();
8209 struct catch_syscall_inferior_data *inf_data
8210 = get_catch_syscall_inferior_data (inf);
8211
8212 ++inf_data->total_syscalls_count;
8213 if (!c->syscalls_to_be_caught)
8214 ++inf_data->any_syscall_count;
8215 else
8216 {
8217 int i, iter;
8218
8219 for (i = 0;
8220 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8221 i++)
8222 {
8223 int elem;
8224
8225 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8226 {
8227 int old_size = VEC_length (int, inf_data->syscalls_counts);
8228 uintptr_t vec_addr_offset
8229 = old_size * ((uintptr_t) sizeof (int));
8230 uintptr_t vec_addr;
8231 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8232 vec_addr = ((uintptr_t) VEC_address (int,
8233 inf_data->syscalls_counts)
8234 + vec_addr_offset);
8235 memset ((void *) vec_addr, 0,
8236 (iter + 1 - old_size) * sizeof (int));
8237 }
8238 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8239 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8240 }
8241 }
8242
8243 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8244 inf_data->total_syscalls_count != 0,
8245 inf_data->any_syscall_count,
8246 VEC_length (int,
8247 inf_data->syscalls_counts),
8248 VEC_address (int,
8249 inf_data->syscalls_counts));
8250}
8251
8252/* Implement the "remove" breakpoint_ops method for syscall
8253 catchpoints. */
8254
8255static int
8256remove_catch_syscall (struct bp_location *bl)
8257{
8258 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8259 struct inferior *inf = current_inferior ();
8260 struct catch_syscall_inferior_data *inf_data
8261 = get_catch_syscall_inferior_data (inf);
8262
8263 --inf_data->total_syscalls_count;
8264 if (!c->syscalls_to_be_caught)
8265 --inf_data->any_syscall_count;
8266 else
8267 {
8268 int i, iter;
8269
8270 for (i = 0;
8271 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8272 i++)
8273 {
8274 int elem;
8275 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8276 /* Shouldn't happen. */
8277 continue;
8278 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8279 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8280 }
8281 }
8282
8283 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8284 inf_data->total_syscalls_count != 0,
8285 inf_data->any_syscall_count,
8286 VEC_length (int,
8287 inf_data->syscalls_counts),
8288 VEC_address (int,
8289 inf_data->syscalls_counts));
8290}
8291
8292/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8293 catchpoints. */
8294
8295static int
8296breakpoint_hit_catch_syscall (const struct bp_location *bl,
8297 struct address_space *aspace, CORE_ADDR bp_addr,
8298 const struct target_waitstatus *ws)
8299{
8300 /* We must check if we are catching specific syscalls in this
8301 breakpoint. If we are, then we must guarantee that the called
8302 syscall is the same syscall we are catching. */
8303 int syscall_number = 0;
8304 const struct syscall_catchpoint *c
8305 = (const struct syscall_catchpoint *) bl->owner;
8306
8307 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8308 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8309 return 0;
8310
8311 syscall_number = ws->value.syscall_number;
8312
8313 /* Now, checking if the syscall is the same. */
8314 if (c->syscalls_to_be_caught)
8315 {
8316 int i, iter;
8317
8318 for (i = 0;
8319 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8320 i++)
8321 if (syscall_number == iter)
8322 break;
8323 /* Not the same. */
8324 if (!iter)
8325 return 0;
8326 }
8327
8328 return 1;
8329}
8330
8331/* Implement the "print_it" breakpoint_ops method for syscall
8332 catchpoints. */
8333
8334static enum print_stop_action
8335print_it_catch_syscall (bpstat bs)
8336{
8337 struct ui_out *uiout = current_uiout;
8338 struct breakpoint *b = bs->breakpoint_at;
8339 /* These are needed because we want to know in which state a
8340 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8341 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8342 must print "called syscall" or "returned from syscall". */
8343 ptid_t ptid;
8344 struct target_waitstatus last;
8345 struct syscall s;
8346
8347 get_last_target_status (&ptid, &last);
8348
8349 get_syscall_by_number (last.value.syscall_number, &s);
8350
8351 annotate_catchpoint (b->number);
8352
8353 if (b->disposition == disp_del)
8354 ui_out_text (uiout, "\nTemporary catchpoint ");
8355 else
8356 ui_out_text (uiout, "\nCatchpoint ");
8357 if (ui_out_is_mi_like_p (uiout))
8358 {
8359 ui_out_field_string (uiout, "reason",
8360 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8361 ? EXEC_ASYNC_SYSCALL_ENTRY
8362 : EXEC_ASYNC_SYSCALL_RETURN));
8363 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8364 }
8365 ui_out_field_int (uiout, "bkptno", b->number);
8366
8367 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8368 ui_out_text (uiout, " (call to syscall ");
8369 else
8370 ui_out_text (uiout, " (returned from syscall ");
8371
8372 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8373 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8374 if (s.name != NULL)
8375 ui_out_field_string (uiout, "syscall-name", s.name);
8376
8377 ui_out_text (uiout, "), ");
8378
8379 return PRINT_SRC_AND_LOC;
8380}
8381
8382/* Implement the "print_one" breakpoint_ops method for syscall
8383 catchpoints. */
8384
8385static void
8386print_one_catch_syscall (struct breakpoint *b,
8387 struct bp_location **last_loc)
8388{
8389 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8390 struct value_print_options opts;
8391 struct ui_out *uiout = current_uiout;
8392
8393 get_user_print_options (&opts);
8394 /* Field 4, the address, is omitted (which makes the columns not
8395 line up too nicely with the headers, but the effect is relatively
8396 readable). */
8397 if (opts.addressprint)
8398 ui_out_field_skip (uiout, "addr");
8399 annotate_field (5);
8400
8401 if (c->syscalls_to_be_caught
8402 && VEC_length (int, c->syscalls_to_be_caught) > 1)
8403 ui_out_text (uiout, "syscalls \"");
8404 else
8405 ui_out_text (uiout, "syscall \"");
8406
8407 if (c->syscalls_to_be_caught)
8408 {
8409 int i, iter;
8410 char *text = xstrprintf ("%s", "");
8411
8412 for (i = 0;
8413 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8414 i++)
8415 {
8416 char *x = text;
8417 struct syscall s;
8418 get_syscall_by_number (iter, &s);
8419
8420 if (s.name != NULL)
8421 text = xstrprintf ("%s%s, ", text, s.name);
8422 else
8423 text = xstrprintf ("%s%d, ", text, iter);
8424
8425 /* We have to xfree the last 'text' (now stored at 'x')
8426 because xstrprintf dynamically allocates new space for it
8427 on every call. */
8428 xfree (x);
8429 }
8430 /* Remove the last comma. */
8431 text[strlen (text) - 2] = '\0';
8432 ui_out_field_string (uiout, "what", text);
8433 }
8434 else
8435 ui_out_field_string (uiout, "what", "<any syscall>");
8436 ui_out_text (uiout, "\" ");
8437
8438 if (ui_out_is_mi_like_p (uiout))
8439 ui_out_field_string (uiout, "catch-type", "syscall");
8440}
8441
8442/* Implement the "print_mention" breakpoint_ops method for syscall
8443 catchpoints. */
8444
8445static void
8446print_mention_catch_syscall (struct breakpoint *b)
8447{
8448 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8449
8450 if (c->syscalls_to_be_caught)
8451 {
8452 int i, iter;
8453
8454 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8455 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8456 else
8457 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8458
8459 for (i = 0;
8460 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8461 i++)
8462 {
8463 struct syscall s;
8464 get_syscall_by_number (iter, &s);
8465
8466 if (s.name)
8467 printf_filtered (" '%s' [%d]", s.name, s.number);
8468 else
8469 printf_filtered (" %d", s.number);
8470 }
8471 printf_filtered (")");
8472 }
8473 else
8474 printf_filtered (_("Catchpoint %d (any syscall)"),
8475 b->number);
8476}
8477
8478/* Implement the "print_recreate" breakpoint_ops method for syscall
8479 catchpoints. */
8480
8481static void
8482print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8483{
8484 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8485
8486 fprintf_unfiltered (fp, "catch syscall");
8487
8488 if (c->syscalls_to_be_caught)
8489 {
8490 int i, iter;
8491
8492 for (i = 0;
8493 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8494 i++)
8495 {
8496 struct syscall s;
8497
8498 get_syscall_by_number (iter, &s);
8499 if (s.name)
8500 fprintf_unfiltered (fp, " %s", s.name);
8501 else
8502 fprintf_unfiltered (fp, " %d", s.number);
8503 }
8504 }
8505 print_recreate_thread (b, fp);
8506}
8507
8508/* The breakpoint_ops structure to be used in syscall catchpoints. */
8509
8510static struct breakpoint_ops catch_syscall_breakpoint_ops;
8511
8512/* Returns non-zero if 'b' is a syscall catchpoint. */
8513
8514static int
8515syscall_catchpoint_p (struct breakpoint *b)
8516{
8517 return (b->ops == &catch_syscall_breakpoint_ops);
8518}
8519
8520/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8521 is non-zero, then make the breakpoint temporary. If COND_STRING is
8522 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8523 the breakpoint_ops structure associated to the catchpoint. */
8524
8525void
8526init_catchpoint (struct breakpoint *b,
8527 struct gdbarch *gdbarch, int tempflag,
8528 char *cond_string,
8529 const struct breakpoint_ops *ops)
8530{
8531 struct symtab_and_line sal;
8532
8533 init_sal (&sal);
8534 sal.pspace = current_program_space;
8535
8536 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8537
8538 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8539 b->disposition = tempflag ? disp_del : disp_donttouch;
8540}
8541
8542void
8543install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8544{
8545 add_to_breakpoint_chain (b);
8546 set_breakpoint_number (internal, b);
8547 if (is_tracepoint (b))
8548 set_tracepoint_count (breakpoint_count);
8549 if (!internal)
8550 mention (b);
8551 observer_notify_breakpoint_created (b);
8552
8553 if (update_gll)
8554 update_global_location_list (1);
8555}
8556
8557static void
8558create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8559 int tempflag, char *cond_string,
8560 const struct breakpoint_ops *ops)
8561{
8562 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8563
8564 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8565
8566 c->forked_inferior_pid = null_ptid;
8567
8568 install_breakpoint (0, &c->base, 1);
8569}
8570
8571/* Exec catchpoints. */
8572
8573/* An instance of this type is used to represent an exec catchpoint.
8574 It includes a "struct breakpoint" as a kind of base class; users
8575 downcast to "struct breakpoint *" when needed. A breakpoint is
8576 really of this type iff its ops pointer points to
8577 CATCH_EXEC_BREAKPOINT_OPS. */
8578
8579struct exec_catchpoint
8580{
8581 /* The base class. */
8582 struct breakpoint base;
8583
8584 /* Filename of a program whose exec triggered this catchpoint.
8585 This field is only valid immediately after this catchpoint has
8586 triggered. */
8587 char *exec_pathname;
8588};
8589
8590/* Implement the "dtor" breakpoint_ops method for exec
8591 catchpoints. */
8592
8593static void
8594dtor_catch_exec (struct breakpoint *b)
8595{
8596 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8597
8598 xfree (c->exec_pathname);
8599
8600 base_breakpoint_ops.dtor (b);
8601}
8602
8603static int
8604insert_catch_exec (struct bp_location *bl)
8605{
8606 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8607}
8608
8609static int
8610remove_catch_exec (struct bp_location *bl)
8611{
8612 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8613}
8614
8615static int
8616breakpoint_hit_catch_exec (const struct bp_location *bl,
8617 struct address_space *aspace, CORE_ADDR bp_addr,
8618 const struct target_waitstatus *ws)
8619{
8620 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8621
8622 if (ws->kind != TARGET_WAITKIND_EXECD)
8623 return 0;
8624
8625 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8626 return 1;
8627}
8628
8629static enum print_stop_action
8630print_it_catch_exec (bpstat bs)
8631{
8632 struct ui_out *uiout = current_uiout;
8633 struct breakpoint *b = bs->breakpoint_at;
8634 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8635
8636 annotate_catchpoint (b->number);
8637 if (b->disposition == disp_del)
8638 ui_out_text (uiout, "\nTemporary catchpoint ");
8639 else
8640 ui_out_text (uiout, "\nCatchpoint ");
8641 if (ui_out_is_mi_like_p (uiout))
8642 {
8643 ui_out_field_string (uiout, "reason",
8644 async_reason_lookup (EXEC_ASYNC_EXEC));
8645 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8646 }
8647 ui_out_field_int (uiout, "bkptno", b->number);
8648 ui_out_text (uiout, " (exec'd ");
8649 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8650 ui_out_text (uiout, "), ");
8651
8652 return PRINT_SRC_AND_LOC;
8653}
8654
8655static void
8656print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8657{
8658 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8659 struct value_print_options opts;
8660 struct ui_out *uiout = current_uiout;
8661
8662 get_user_print_options (&opts);
8663
8664 /* Field 4, the address, is omitted (which makes the columns
8665 not line up too nicely with the headers, but the effect
8666 is relatively readable). */
8667 if (opts.addressprint)
8668 ui_out_field_skip (uiout, "addr");
8669 annotate_field (5);
8670 ui_out_text (uiout, "exec");
8671 if (c->exec_pathname != NULL)
8672 {
8673 ui_out_text (uiout, ", program \"");
8674 ui_out_field_string (uiout, "what", c->exec_pathname);
8675 ui_out_text (uiout, "\" ");
8676 }
8677
8678 if (ui_out_is_mi_like_p (uiout))
8679 ui_out_field_string (uiout, "catch-type", "exec");
8680}
8681
8682static void
8683print_mention_catch_exec (struct breakpoint *b)
8684{
8685 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8686}
8687
8688/* Implement the "print_recreate" breakpoint_ops method for exec
8689 catchpoints. */
8690
8691static void
8692print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8693{
8694 fprintf_unfiltered (fp, "catch exec");
8695 print_recreate_thread (b, fp);
8696}
8697
8698static struct breakpoint_ops catch_exec_breakpoint_ops;
8699
8700static void
8701create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8702 const struct breakpoint_ops *ops)
8703{
8704 struct syscall_catchpoint *c;
8705 struct gdbarch *gdbarch = get_current_arch ();
8706
8707 c = XNEW (struct syscall_catchpoint);
8708 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8709 c->syscalls_to_be_caught = filter;
8710
8711 install_breakpoint (0, &c->base, 1);
8712}
8713
8714static int
8715hw_breakpoint_used_count (void)
8716{
8717 int i = 0;
8718 struct breakpoint *b;
8719 struct bp_location *bl;
8720
8721 ALL_BREAKPOINTS (b)
8722 {
8723 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8724 for (bl = b->loc; bl; bl = bl->next)
8725 {
8726 /* Special types of hardware breakpoints may use more than
8727 one register. */
8728 i += b->ops->resources_needed (bl);
8729 }
8730 }
8731
8732 return i;
8733}
8734
8735/* Returns the resources B would use if it were a hardware
8736 watchpoint. */
8737
8738static int
8739hw_watchpoint_use_count (struct breakpoint *b)
8740{
8741 int i = 0;
8742 struct bp_location *bl;
8743
8744 if (!breakpoint_enabled (b))
8745 return 0;
8746
8747 for (bl = b->loc; bl; bl = bl->next)
8748 {
8749 /* Special types of hardware watchpoints may use more than
8750 one register. */
8751 i += b->ops->resources_needed (bl);
8752 }
8753
8754 return i;
8755}
8756
8757/* Returns the sum the used resources of all hardware watchpoints of
8758 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8759 the sum of the used resources of all hardware watchpoints of other
8760 types _not_ TYPE. */
8761
8762static int
8763hw_watchpoint_used_count_others (struct breakpoint *except,
8764 enum bptype type, int *other_type_used)
8765{
8766 int i = 0;
8767 struct breakpoint *b;
8768
8769 *other_type_used = 0;
8770 ALL_BREAKPOINTS (b)
8771 {
8772 if (b == except)
8773 continue;
8774 if (!breakpoint_enabled (b))
8775 continue;
8776
8777 if (b->type == type)
8778 i += hw_watchpoint_use_count (b);
8779 else if (is_hardware_watchpoint (b))
8780 *other_type_used = 1;
8781 }
8782
8783 return i;
8784}
8785
8786void
8787disable_watchpoints_before_interactive_call_start (void)
8788{
8789 struct breakpoint *b;
8790
8791 ALL_BREAKPOINTS (b)
8792 {
8793 if (is_watchpoint (b) && breakpoint_enabled (b))
8794 {
8795 b->enable_state = bp_call_disabled;
8796 update_global_location_list (0);
8797 }
8798 }
8799}
8800
8801void
8802enable_watchpoints_after_interactive_call_stop (void)
8803{
8804 struct breakpoint *b;
8805
8806 ALL_BREAKPOINTS (b)
8807 {
8808 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8809 {
8810 b->enable_state = bp_enabled;
8811 update_global_location_list (1);
8812 }
8813 }
8814}
8815
8816void
8817disable_breakpoints_before_startup (void)
8818{
8819 current_program_space->executing_startup = 1;
8820 update_global_location_list (0);
8821}
8822
8823void
8824enable_breakpoints_after_startup (void)
8825{
8826 current_program_space->executing_startup = 0;
8827 breakpoint_re_set ();
8828}
8829
8830
8831/* Set a breakpoint that will evaporate an end of command
8832 at address specified by SAL.
8833 Restrict it to frame FRAME if FRAME is nonzero. */
8834
8835struct breakpoint *
8836set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8837 struct frame_id frame_id, enum bptype type)
8838{
8839 struct breakpoint *b;
8840
8841 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8842 tail-called one. */
8843 gdb_assert (!frame_id_artificial_p (frame_id));
8844
8845 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8846 b->enable_state = bp_enabled;
8847 b->disposition = disp_donttouch;
8848 b->frame_id = frame_id;
8849
8850 /* If we're debugging a multi-threaded program, then we want
8851 momentary breakpoints to be active in only a single thread of
8852 control. */
8853 if (in_thread_list (inferior_ptid))
8854 b->thread = pid_to_thread_id (inferior_ptid);
8855
8856 update_global_location_list_nothrow (1);
8857
8858 return b;
8859}
8860
8861/* Make a momentary breakpoint based on the master breakpoint ORIG.
8862 The new breakpoint will have type TYPE, and use OPS as it
8863 breakpoint_ops. */
8864
8865static struct breakpoint *
8866momentary_breakpoint_from_master (struct breakpoint *orig,
8867 enum bptype type,
8868 const struct breakpoint_ops *ops)
8869{
8870 struct breakpoint *copy;
8871
8872 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8873 copy->loc = allocate_bp_location (copy);
8874 set_breakpoint_location_function (copy->loc, 1);
8875
8876 copy->loc->gdbarch = orig->loc->gdbarch;
8877 copy->loc->requested_address = orig->loc->requested_address;
8878 copy->loc->address = orig->loc->address;
8879 copy->loc->section = orig->loc->section;
8880 copy->loc->pspace = orig->loc->pspace;
8881 copy->loc->probe = orig->loc->probe;
8882 copy->loc->line_number = orig->loc->line_number;
8883 copy->loc->symtab = orig->loc->symtab;
8884 copy->frame_id = orig->frame_id;
8885 copy->thread = orig->thread;
8886 copy->pspace = orig->pspace;
8887
8888 copy->enable_state = bp_enabled;
8889 copy->disposition = disp_donttouch;
8890 copy->number = internal_breakpoint_number--;
8891
8892 update_global_location_list_nothrow (0);
8893 return copy;
8894}
8895
8896/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8897 ORIG is NULL. */
8898
8899struct breakpoint *
8900clone_momentary_breakpoint (struct breakpoint *orig)
8901{
8902 /* If there's nothing to clone, then return nothing. */
8903 if (orig == NULL)
8904 return NULL;
8905
8906 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8907}
8908
8909struct breakpoint *
8910set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8911 enum bptype type)
8912{
8913 struct symtab_and_line sal;
8914
8915 sal = find_pc_line (pc, 0);
8916 sal.pc = pc;
8917 sal.section = find_pc_overlay (pc);
8918 sal.explicit_pc = 1;
8919
8920 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8921}
8922\f
8923
8924/* Tell the user we have just set a breakpoint B. */
8925
8926static void
8927mention (struct breakpoint *b)
8928{
8929 b->ops->print_mention (b);
8930 if (ui_out_is_mi_like_p (current_uiout))
8931 return;
8932 printf_filtered ("\n");
8933}
8934\f
8935
8936static struct bp_location *
8937add_location_to_breakpoint (struct breakpoint *b,
8938 const struct symtab_and_line *sal)
8939{
8940 struct bp_location *loc, **tmp;
8941 CORE_ADDR adjusted_address;
8942 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8943
8944 if (loc_gdbarch == NULL)
8945 loc_gdbarch = b->gdbarch;
8946
8947 /* Adjust the breakpoint's address prior to allocating a location.
8948 Once we call allocate_bp_location(), that mostly uninitialized
8949 location will be placed on the location chain. Adjustment of the
8950 breakpoint may cause target_read_memory() to be called and we do
8951 not want its scan of the location chain to find a breakpoint and
8952 location that's only been partially initialized. */
8953 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8954 sal->pc, b->type);
8955
8956 /* Sort the locations by their ADDRESS. */
8957 loc = allocate_bp_location (b);
8958 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8959 tmp = &((*tmp)->next))
8960 ;
8961 loc->next = *tmp;
8962 *tmp = loc;
8963
8964 loc->requested_address = sal->pc;
8965 loc->address = adjusted_address;
8966 loc->pspace = sal->pspace;
8967 loc->probe = sal->probe;
8968 gdb_assert (loc->pspace != NULL);
8969 loc->section = sal->section;
8970 loc->gdbarch = loc_gdbarch;
8971 loc->line_number = sal->line;
8972 loc->symtab = sal->symtab;
8973
8974 set_breakpoint_location_function (loc,
8975 sal->explicit_pc || sal->explicit_line);
8976 return loc;
8977}
8978\f
8979
8980/* Return 1 if LOC is pointing to a permanent breakpoint,
8981 return 0 otherwise. */
8982
8983static int
8984bp_loc_is_permanent (struct bp_location *loc)
8985{
8986 int len;
8987 CORE_ADDR addr;
8988 const gdb_byte *bpoint;
8989 gdb_byte *target_mem;
8990 struct cleanup *cleanup;
8991 int retval = 0;
8992
8993 gdb_assert (loc != NULL);
8994
8995 addr = loc->address;
8996 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
8997
8998 /* Software breakpoints unsupported? */
8999 if (bpoint == NULL)
9000 return 0;
9001
9002 target_mem = alloca (len);
9003
9004 /* Enable the automatic memory restoration from breakpoints while
9005 we read the memory. Otherwise we could say about our temporary
9006 breakpoints they are permanent. */
9007 cleanup = save_current_space_and_thread ();
9008
9009 switch_to_program_space_and_thread (loc->pspace);
9010 make_show_memory_breakpoints_cleanup (0);
9011
9012 if (target_read_memory (loc->address, target_mem, len) == 0
9013 && memcmp (target_mem, bpoint, len) == 0)
9014 retval = 1;
9015
9016 do_cleanups (cleanup);
9017
9018 return retval;
9019}
9020
9021/* Build a command list for the dprintf corresponding to the current
9022 settings of the dprintf style options. */
9023
9024static void
9025update_dprintf_command_list (struct breakpoint *b)
9026{
9027 char *dprintf_args = b->extra_string;
9028 char *printf_line = NULL;
9029
9030 if (!dprintf_args)
9031 return;
9032
9033 dprintf_args = skip_spaces (dprintf_args);
9034
9035 /* Allow a comma, as it may have terminated a location, but don't
9036 insist on it. */
9037 if (*dprintf_args == ',')
9038 ++dprintf_args;
9039 dprintf_args = skip_spaces (dprintf_args);
9040
9041 if (*dprintf_args != '"')
9042 error (_("Bad format string, missing '\"'."));
9043
9044 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9045 printf_line = xstrprintf ("printf %s", dprintf_args);
9046 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9047 {
9048 if (!dprintf_function)
9049 error (_("No function supplied for dprintf call"));
9050
9051 if (dprintf_channel && strlen (dprintf_channel) > 0)
9052 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9053 dprintf_function,
9054 dprintf_channel,
9055 dprintf_args);
9056 else
9057 printf_line = xstrprintf ("call (void) %s (%s)",
9058 dprintf_function,
9059 dprintf_args);
9060 }
9061 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9062 {
9063 if (target_can_run_breakpoint_commands ())
9064 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9065 else
9066 {
9067 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9068 printf_line = xstrprintf ("printf %s", dprintf_args);
9069 }
9070 }
9071 else
9072 internal_error (__FILE__, __LINE__,
9073 _("Invalid dprintf style."));
9074
9075 gdb_assert (printf_line != NULL);
9076 /* Manufacture a printf sequence. */
9077 {
9078 struct command_line *printf_cmd_line
9079 = xmalloc (sizeof (struct command_line));
9080
9081 printf_cmd_line = xmalloc (sizeof (struct command_line));
9082 printf_cmd_line->control_type = simple_control;
9083 printf_cmd_line->body_count = 0;
9084 printf_cmd_line->body_list = NULL;
9085 printf_cmd_line->next = NULL;
9086 printf_cmd_line->line = printf_line;
9087
9088 breakpoint_set_commands (b, printf_cmd_line);
9089 }
9090}
9091
9092/* Update all dprintf commands, making their command lists reflect
9093 current style settings. */
9094
9095static void
9096update_dprintf_commands (char *args, int from_tty,
9097 struct cmd_list_element *c)
9098{
9099 struct breakpoint *b;
9100
9101 ALL_BREAKPOINTS (b)
9102 {
9103 if (b->type == bp_dprintf)
9104 update_dprintf_command_list (b);
9105 }
9106}
9107
9108/* Create a breakpoint with SAL as location. Use ADDR_STRING
9109 as textual description of the location, and COND_STRING
9110 as condition expression. */
9111
9112static void
9113init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9114 struct symtabs_and_lines sals, char *addr_string,
9115 char *filter, char *cond_string,
9116 char *extra_string,
9117 enum bptype type, enum bpdisp disposition,
9118 int thread, int task, int ignore_count,
9119 const struct breakpoint_ops *ops, int from_tty,
9120 int enabled, int internal, unsigned flags,
9121 int display_canonical)
9122{
9123 int i;
9124
9125 if (type == bp_hardware_breakpoint)
9126 {
9127 int target_resources_ok;
9128
9129 i = hw_breakpoint_used_count ();
9130 target_resources_ok =
9131 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9132 i + 1, 0);
9133 if (target_resources_ok == 0)
9134 error (_("No hardware breakpoint support in the target."));
9135 else if (target_resources_ok < 0)
9136 error (_("Hardware breakpoints used exceeds limit."));
9137 }
9138
9139 gdb_assert (sals.nelts > 0);
9140
9141 for (i = 0; i < sals.nelts; ++i)
9142 {
9143 struct symtab_and_line sal = sals.sals[i];
9144 struct bp_location *loc;
9145
9146 if (from_tty)
9147 {
9148 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9149 if (!loc_gdbarch)
9150 loc_gdbarch = gdbarch;
9151
9152 describe_other_breakpoints (loc_gdbarch,
9153 sal.pspace, sal.pc, sal.section, thread);
9154 }
9155
9156 if (i == 0)
9157 {
9158 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9159 b->thread = thread;
9160 b->task = task;
9161
9162 b->cond_string = cond_string;
9163 b->extra_string = extra_string;
9164 b->ignore_count = ignore_count;
9165 b->enable_state = enabled ? bp_enabled : bp_disabled;
9166 b->disposition = disposition;
9167
9168 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9169 b->loc->inserted = 1;
9170
9171 if (type == bp_static_tracepoint)
9172 {
9173 struct tracepoint *t = (struct tracepoint *) b;
9174 struct static_tracepoint_marker marker;
9175
9176 if (strace_marker_p (b))
9177 {
9178 /* We already know the marker exists, otherwise, we
9179 wouldn't see a sal for it. */
9180 char *p = &addr_string[3];
9181 char *endp;
9182 char *marker_str;
9183
9184 p = skip_spaces (p);
9185
9186 endp = skip_to_space (p);
9187
9188 marker_str = savestring (p, endp - p);
9189 t->static_trace_marker_id = marker_str;
9190
9191 printf_filtered (_("Probed static tracepoint "
9192 "marker \"%s\"\n"),
9193 t->static_trace_marker_id);
9194 }
9195 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9196 {
9197 t->static_trace_marker_id = xstrdup (marker.str_id);
9198 release_static_tracepoint_marker (&marker);
9199
9200 printf_filtered (_("Probed static tracepoint "
9201 "marker \"%s\"\n"),
9202 t->static_trace_marker_id);
9203 }
9204 else
9205 warning (_("Couldn't determine the static "
9206 "tracepoint marker to probe"));
9207 }
9208
9209 loc = b->loc;
9210 }
9211 else
9212 {
9213 loc = add_location_to_breakpoint (b, &sal);
9214 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9215 loc->inserted = 1;
9216 }
9217
9218 if (bp_loc_is_permanent (loc))
9219 make_breakpoint_permanent (b);
9220
9221 if (b->cond_string)
9222 {
9223 const char *arg = b->cond_string;
9224
9225 loc->cond = parse_exp_1 (&arg, loc->address,
9226 block_for_pc (loc->address), 0);
9227 if (*arg)
9228 error (_("Garbage '%s' follows condition"), arg);
9229 }
9230
9231 /* Dynamic printf requires and uses additional arguments on the
9232 command line, otherwise it's an error. */
9233 if (type == bp_dprintf)
9234 {
9235 if (b->extra_string)
9236 update_dprintf_command_list (b);
9237 else
9238 error (_("Format string required"));
9239 }
9240 else if (b->extra_string)
9241 error (_("Garbage '%s' at end of command"), b->extra_string);
9242 }
9243
9244 b->display_canonical = display_canonical;
9245 if (addr_string)
9246 b->addr_string = addr_string;
9247 else
9248 /* addr_string has to be used or breakpoint_re_set will delete
9249 me. */
9250 b->addr_string
9251 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9252 b->filter = filter;
9253}
9254
9255static void
9256create_breakpoint_sal (struct gdbarch *gdbarch,
9257 struct symtabs_and_lines sals, char *addr_string,
9258 char *filter, char *cond_string,
9259 char *extra_string,
9260 enum bptype type, enum bpdisp disposition,
9261 int thread, int task, int ignore_count,
9262 const struct breakpoint_ops *ops, int from_tty,
9263 int enabled, int internal, unsigned flags,
9264 int display_canonical)
9265{
9266 struct breakpoint *b;
9267 struct cleanup *old_chain;
9268
9269 if (is_tracepoint_type (type))
9270 {
9271 struct tracepoint *t;
9272
9273 t = XCNEW (struct tracepoint);
9274 b = &t->base;
9275 }
9276 else
9277 b = XNEW (struct breakpoint);
9278
9279 old_chain = make_cleanup (xfree, b);
9280
9281 init_breakpoint_sal (b, gdbarch,
9282 sals, addr_string,
9283 filter, cond_string, extra_string,
9284 type, disposition,
9285 thread, task, ignore_count,
9286 ops, from_tty,
9287 enabled, internal, flags,
9288 display_canonical);
9289 discard_cleanups (old_chain);
9290
9291 install_breakpoint (internal, b, 0);
9292}
9293
9294/* Add SALS.nelts breakpoints to the breakpoint table. For each
9295 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9296 value. COND_STRING, if not NULL, specified the condition to be
9297 used for all breakpoints. Essentially the only case where
9298 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9299 function. In that case, it's still not possible to specify
9300 separate conditions for different overloaded functions, so
9301 we take just a single condition string.
9302
9303 NOTE: If the function succeeds, the caller is expected to cleanup
9304 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9305 array contents). If the function fails (error() is called), the
9306 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9307 COND and SALS arrays and each of those arrays contents. */
9308
9309static void
9310create_breakpoints_sal (struct gdbarch *gdbarch,
9311 struct linespec_result *canonical,
9312 char *cond_string, char *extra_string,
9313 enum bptype type, enum bpdisp disposition,
9314 int thread, int task, int ignore_count,
9315 const struct breakpoint_ops *ops, int from_tty,
9316 int enabled, int internal, unsigned flags)
9317{
9318 int i;
9319 struct linespec_sals *lsal;
9320
9321 if (canonical->pre_expanded)
9322 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9323
9324 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9325 {
9326 /* Note that 'addr_string' can be NULL in the case of a plain
9327 'break', without arguments. */
9328 char *addr_string = (canonical->addr_string
9329 ? xstrdup (canonical->addr_string)
9330 : NULL);
9331 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9332 struct cleanup *inner = make_cleanup (xfree, addr_string);
9333
9334 make_cleanup (xfree, filter_string);
9335 create_breakpoint_sal (gdbarch, lsal->sals,
9336 addr_string,
9337 filter_string,
9338 cond_string, extra_string,
9339 type, disposition,
9340 thread, task, ignore_count, ops,
9341 from_tty, enabled, internal, flags,
9342 canonical->special_display);
9343 discard_cleanups (inner);
9344 }
9345}
9346
9347/* Parse ADDRESS which is assumed to be a SAL specification possibly
9348 followed by conditionals. On return, SALS contains an array of SAL
9349 addresses found. ADDR_STRING contains a vector of (canonical)
9350 address strings. ADDRESS points to the end of the SAL.
9351
9352 The array and the line spec strings are allocated on the heap, it is
9353 the caller's responsibility to free them. */
9354
9355static void
9356parse_breakpoint_sals (char **address,
9357 struct linespec_result *canonical)
9358{
9359 /* If no arg given, or if first arg is 'if ', use the default
9360 breakpoint. */
9361 if ((*address) == NULL
9362 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
9363 {
9364 /* The last displayed codepoint, if it's valid, is our default breakpoint
9365 address. */
9366 if (last_displayed_sal_is_valid ())
9367 {
9368 struct linespec_sals lsal;
9369 struct symtab_and_line sal;
9370 CORE_ADDR pc;
9371
9372 init_sal (&sal); /* Initialize to zeroes. */
9373 lsal.sals.sals = (struct symtab_and_line *)
9374 xmalloc (sizeof (struct symtab_and_line));
9375
9376 /* Set sal's pspace, pc, symtab, and line to the values
9377 corresponding to the last call to print_frame_info.
9378 Be sure to reinitialize LINE with NOTCURRENT == 0
9379 as the breakpoint line number is inappropriate otherwise.
9380 find_pc_line would adjust PC, re-set it back. */
9381 get_last_displayed_sal (&sal);
9382 pc = sal.pc;
9383 sal = find_pc_line (pc, 0);
9384
9385 /* "break" without arguments is equivalent to "break *PC"
9386 where PC is the last displayed codepoint's address. So
9387 make sure to set sal.explicit_pc to prevent GDB from
9388 trying to expand the list of sals to include all other
9389 instances with the same symtab and line. */
9390 sal.pc = pc;
9391 sal.explicit_pc = 1;
9392
9393 lsal.sals.sals[0] = sal;
9394 lsal.sals.nelts = 1;
9395 lsal.canonical = NULL;
9396
9397 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9398 }
9399 else
9400 error (_("No default breakpoint address now."));
9401 }
9402 else
9403 {
9404 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9405
9406 /* Force almost all breakpoints to be in terms of the
9407 current_source_symtab (which is decode_line_1's default).
9408 This should produce the results we want almost all of the
9409 time while leaving default_breakpoint_* alone.
9410
9411 ObjC: However, don't match an Objective-C method name which
9412 may have a '+' or '-' succeeded by a '['. */
9413 if (last_displayed_sal_is_valid ()
9414 && (!cursal.symtab
9415 || ((strchr ("+-", (*address)[0]) != NULL)
9416 && ((*address)[1] != '['))))
9417 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9418 get_last_displayed_symtab (),
9419 get_last_displayed_line (),
9420 canonical, NULL, NULL);
9421 else
9422 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9423 cursal.symtab, cursal.line, canonical, NULL, NULL);
9424 }
9425}
9426
9427
9428/* Convert each SAL into a real PC. Verify that the PC can be
9429 inserted as a breakpoint. If it can't throw an error. */
9430
9431static void
9432breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9433{
9434 int i;
9435
9436 for (i = 0; i < sals->nelts; i++)
9437 resolve_sal_pc (&sals->sals[i]);
9438}
9439
9440/* Fast tracepoints may have restrictions on valid locations. For
9441 instance, a fast tracepoint using a jump instead of a trap will
9442 likely have to overwrite more bytes than a trap would, and so can
9443 only be placed where the instruction is longer than the jump, or a
9444 multi-instruction sequence does not have a jump into the middle of
9445 it, etc. */
9446
9447static void
9448check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9449 struct symtabs_and_lines *sals)
9450{
9451 int i, rslt;
9452 struct symtab_and_line *sal;
9453 char *msg;
9454 struct cleanup *old_chain;
9455
9456 for (i = 0; i < sals->nelts; i++)
9457 {
9458 struct gdbarch *sarch;
9459
9460 sal = &sals->sals[i];
9461
9462 sarch = get_sal_arch (*sal);
9463 /* We fall back to GDBARCH if there is no architecture
9464 associated with SAL. */
9465 if (sarch == NULL)
9466 sarch = gdbarch;
9467 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9468 NULL, &msg);
9469 old_chain = make_cleanup (xfree, msg);
9470
9471 if (!rslt)
9472 error (_("May not have a fast tracepoint at 0x%s%s"),
9473 paddress (sarch, sal->pc), (msg ? msg : ""));
9474
9475 do_cleanups (old_chain);
9476 }
9477}
9478
9479/* Issue an invalid thread ID error. */
9480
9481static void ATTRIBUTE_NORETURN
9482invalid_thread_id_error (int id)
9483{
9484 error (_("Unknown thread %d."), id);
9485}
9486
9487/* Given TOK, a string specification of condition and thread, as
9488 accepted by the 'break' command, extract the condition
9489 string and thread number and set *COND_STRING and *THREAD.
9490 PC identifies the context at which the condition should be parsed.
9491 If no condition is found, *COND_STRING is set to NULL.
9492 If no thread is found, *THREAD is set to -1. */
9493
9494static void
9495find_condition_and_thread (const char *tok, CORE_ADDR pc,
9496 char **cond_string, int *thread, int *task,
9497 char **rest)
9498{
9499 *cond_string = NULL;
9500 *thread = -1;
9501 *task = 0;
9502 *rest = NULL;
9503
9504 while (tok && *tok)
9505 {
9506 const char *end_tok;
9507 int toklen;
9508 const char *cond_start = NULL;
9509 const char *cond_end = NULL;
9510
9511 tok = skip_spaces_const (tok);
9512
9513 if ((*tok == '"' || *tok == ',') && rest)
9514 {
9515 *rest = savestring (tok, strlen (tok));
9516 return;
9517 }
9518
9519 end_tok = skip_to_space_const (tok);
9520
9521 toklen = end_tok - tok;
9522
9523 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9524 {
9525 struct expression *expr;
9526
9527 tok = cond_start = end_tok + 1;
9528 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9529 xfree (expr);
9530 cond_end = tok;
9531 *cond_string = savestring (cond_start, cond_end - cond_start);
9532 }
9533 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9534 {
9535 char *tmptok;
9536
9537 tok = end_tok + 1;
9538 *thread = strtol (tok, &tmptok, 0);
9539 if (tok == tmptok)
9540 error (_("Junk after thread keyword."));
9541 if (!valid_thread_id (*thread))
9542 invalid_thread_id_error (*thread);
9543 tok = tmptok;
9544 }
9545 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9546 {
9547 char *tmptok;
9548
9549 tok = end_tok + 1;
9550 *task = strtol (tok, &tmptok, 0);
9551 if (tok == tmptok)
9552 error (_("Junk after task keyword."));
9553 if (!valid_task_id (*task))
9554 error (_("Unknown task %d."), *task);
9555 tok = tmptok;
9556 }
9557 else if (rest)
9558 {
9559 *rest = savestring (tok, strlen (tok));
9560 return;
9561 }
9562 else
9563 error (_("Junk at end of arguments."));
9564 }
9565}
9566
9567/* Decode a static tracepoint marker spec. */
9568
9569static struct symtabs_and_lines
9570decode_static_tracepoint_spec (char **arg_p)
9571{
9572 VEC(static_tracepoint_marker_p) *markers = NULL;
9573 struct symtabs_and_lines sals;
9574 struct cleanup *old_chain;
9575 char *p = &(*arg_p)[3];
9576 char *endp;
9577 char *marker_str;
9578 int i;
9579
9580 p = skip_spaces (p);
9581
9582 endp = skip_to_space (p);
9583
9584 marker_str = savestring (p, endp - p);
9585 old_chain = make_cleanup (xfree, marker_str);
9586
9587 markers = target_static_tracepoint_markers_by_strid (marker_str);
9588 if (VEC_empty(static_tracepoint_marker_p, markers))
9589 error (_("No known static tracepoint marker named %s"), marker_str);
9590
9591 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9592 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9593
9594 for (i = 0; i < sals.nelts; i++)
9595 {
9596 struct static_tracepoint_marker *marker;
9597
9598 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9599
9600 init_sal (&sals.sals[i]);
9601
9602 sals.sals[i] = find_pc_line (marker->address, 0);
9603 sals.sals[i].pc = marker->address;
9604
9605 release_static_tracepoint_marker (marker);
9606 }
9607
9608 do_cleanups (old_chain);
9609
9610 *arg_p = endp;
9611 return sals;
9612}
9613
9614/* Set a breakpoint. This function is shared between CLI and MI
9615 functions for setting a breakpoint. This function has two major
9616 modes of operations, selected by the PARSE_ARG parameter. If
9617 non-zero, the function will parse ARG, extracting location,
9618 condition, thread and extra string. Otherwise, ARG is just the
9619 breakpoint's location, with condition, thread, and extra string
9620 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9621 If INTERNAL is non-zero, the breakpoint number will be allocated
9622 from the internal breakpoint count. Returns true if any breakpoint
9623 was created; false otherwise. */
9624
9625int
9626create_breakpoint (struct gdbarch *gdbarch,
9627 char *arg, char *cond_string,
9628 int thread, char *extra_string,
9629 int parse_arg,
9630 int tempflag, enum bptype type_wanted,
9631 int ignore_count,
9632 enum auto_boolean pending_break_support,
9633 const struct breakpoint_ops *ops,
9634 int from_tty, int enabled, int internal,
9635 unsigned flags)
9636{
9637 volatile struct gdb_exception e;
9638 char *copy_arg = NULL;
9639 char *addr_start = arg;
9640 struct linespec_result canonical;
9641 struct cleanup *old_chain;
9642 struct cleanup *bkpt_chain = NULL;
9643 int pending = 0;
9644 int task = 0;
9645 int prev_bkpt_count = breakpoint_count;
9646
9647 gdb_assert (ops != NULL);
9648
9649 init_linespec_result (&canonical);
9650
9651 TRY_CATCH (e, RETURN_MASK_ALL)
9652 {
9653 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9654 addr_start, &copy_arg);
9655 }
9656
9657 /* If caller is interested in rc value from parse, set value. */
9658 switch (e.reason)
9659 {
9660 case GDB_NO_ERROR:
9661 if (VEC_empty (linespec_sals, canonical.sals))
9662 return 0;
9663 break;
9664 case RETURN_ERROR:
9665 switch (e.error)
9666 {
9667 case NOT_FOUND_ERROR:
9668
9669 /* If pending breakpoint support is turned off, throw
9670 error. */
9671
9672 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9673 throw_exception (e);
9674
9675 exception_print (gdb_stderr, e);
9676
9677 /* If pending breakpoint support is auto query and the user
9678 selects no, then simply return the error code. */
9679 if (pending_break_support == AUTO_BOOLEAN_AUTO
9680 && !nquery (_("Make %s pending on future shared library load? "),
9681 bptype_string (type_wanted)))
9682 return 0;
9683
9684 /* At this point, either the user was queried about setting
9685 a pending breakpoint and selected yes, or pending
9686 breakpoint behavior is on and thus a pending breakpoint
9687 is defaulted on behalf of the user. */
9688 {
9689 struct linespec_sals lsal;
9690
9691 copy_arg = xstrdup (addr_start);
9692 lsal.canonical = xstrdup (copy_arg);
9693 lsal.sals.nelts = 1;
9694 lsal.sals.sals = XNEW (struct symtab_and_line);
9695 init_sal (&lsal.sals.sals[0]);
9696 pending = 1;
9697 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9698 }
9699 break;
9700 default:
9701 throw_exception (e);
9702 }
9703 break;
9704 default:
9705 throw_exception (e);
9706 }
9707
9708 /* Create a chain of things that always need to be cleaned up. */
9709 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9710
9711 /* ----------------------------- SNIP -----------------------------
9712 Anything added to the cleanup chain beyond this point is assumed
9713 to be part of a breakpoint. If the breakpoint create succeeds
9714 then the memory is not reclaimed. */
9715 bkpt_chain = make_cleanup (null_cleanup, 0);
9716
9717 /* Resolve all line numbers to PC's and verify that the addresses
9718 are ok for the target. */
9719 if (!pending)
9720 {
9721 int ix;
9722 struct linespec_sals *iter;
9723
9724 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9725 breakpoint_sals_to_pc (&iter->sals);
9726 }
9727
9728 /* Fast tracepoints may have additional restrictions on location. */
9729 if (!pending && type_wanted == bp_fast_tracepoint)
9730 {
9731 int ix;
9732 struct linespec_sals *iter;
9733
9734 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9735 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9736 }
9737
9738 /* Verify that condition can be parsed, before setting any
9739 breakpoints. Allocate a separate condition expression for each
9740 breakpoint. */
9741 if (!pending)
9742 {
9743 if (parse_arg)
9744 {
9745 char *rest;
9746 struct linespec_sals *lsal;
9747
9748 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9749
9750 /* Here we only parse 'arg' to separate condition
9751 from thread number, so parsing in context of first
9752 sal is OK. When setting the breakpoint we'll
9753 re-parse it in context of each sal. */
9754
9755 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9756 &thread, &task, &rest);
9757 if (cond_string)
9758 make_cleanup (xfree, cond_string);
9759 if (rest)
9760 make_cleanup (xfree, rest);
9761 if (rest)
9762 extra_string = rest;
9763 }
9764 else
9765 {
9766 if (*arg != '\0')
9767 error (_("Garbage '%s' at end of location"), arg);
9768
9769 /* Create a private copy of condition string. */
9770 if (cond_string)
9771 {
9772 cond_string = xstrdup (cond_string);
9773 make_cleanup (xfree, cond_string);
9774 }
9775 /* Create a private copy of any extra string. */
9776 if (extra_string)
9777 {
9778 extra_string = xstrdup (extra_string);
9779 make_cleanup (xfree, extra_string);
9780 }
9781 }
9782
9783 ops->create_breakpoints_sal (gdbarch, &canonical,
9784 cond_string, extra_string, type_wanted,
9785 tempflag ? disp_del : disp_donttouch,
9786 thread, task, ignore_count, ops,
9787 from_tty, enabled, internal, flags);
9788 }
9789 else
9790 {
9791 struct breakpoint *b;
9792
9793 make_cleanup (xfree, copy_arg);
9794
9795 if (is_tracepoint_type (type_wanted))
9796 {
9797 struct tracepoint *t;
9798
9799 t = XCNEW (struct tracepoint);
9800 b = &t->base;
9801 }
9802 else
9803 b = XNEW (struct breakpoint);
9804
9805 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9806
9807 b->addr_string = copy_arg;
9808 if (parse_arg)
9809 b->cond_string = NULL;
9810 else
9811 {
9812 /* Create a private copy of condition string. */
9813 if (cond_string)
9814 {
9815 cond_string = xstrdup (cond_string);
9816 make_cleanup (xfree, cond_string);
9817 }
9818 b->cond_string = cond_string;
9819 }
9820 b->extra_string = NULL;
9821 b->ignore_count = ignore_count;
9822 b->disposition = tempflag ? disp_del : disp_donttouch;
9823 b->condition_not_parsed = 1;
9824 b->enable_state = enabled ? bp_enabled : bp_disabled;
9825 if ((type_wanted != bp_breakpoint
9826 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9827 b->pspace = current_program_space;
9828
9829 install_breakpoint (internal, b, 0);
9830 }
9831
9832 if (VEC_length (linespec_sals, canonical.sals) > 1)
9833 {
9834 warning (_("Multiple breakpoints were set.\nUse the "
9835 "\"delete\" command to delete unwanted breakpoints."));
9836 prev_breakpoint_count = prev_bkpt_count;
9837 }
9838
9839 /* That's it. Discard the cleanups for data inserted into the
9840 breakpoint. */
9841 discard_cleanups (bkpt_chain);
9842 /* But cleanup everything else. */
9843 do_cleanups (old_chain);
9844
9845 /* error call may happen here - have BKPT_CHAIN already discarded. */
9846 update_global_location_list (1);
9847
9848 return 1;
9849}
9850
9851/* Set a breakpoint.
9852 ARG is a string describing breakpoint address,
9853 condition, and thread.
9854 FLAG specifies if a breakpoint is hardware on,
9855 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9856 and BP_TEMPFLAG. */
9857
9858static void
9859break_command_1 (char *arg, int flag, int from_tty)
9860{
9861 int tempflag = flag & BP_TEMPFLAG;
9862 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9863 ? bp_hardware_breakpoint
9864 : bp_breakpoint);
9865 struct breakpoint_ops *ops;
9866 const char *arg_cp = arg;
9867
9868 /* Matching breakpoints on probes. */
9869 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9870 ops = &bkpt_probe_breakpoint_ops;
9871 else
9872 ops = &bkpt_breakpoint_ops;
9873
9874 create_breakpoint (get_current_arch (),
9875 arg,
9876 NULL, 0, NULL, 1 /* parse arg */,
9877 tempflag, type_wanted,
9878 0 /* Ignore count */,
9879 pending_break_support,
9880 ops,
9881 from_tty,
9882 1 /* enabled */,
9883 0 /* internal */,
9884 0);
9885}
9886
9887/* Helper function for break_command_1 and disassemble_command. */
9888
9889void
9890resolve_sal_pc (struct symtab_and_line *sal)
9891{
9892 CORE_ADDR pc;
9893
9894 if (sal->pc == 0 && sal->symtab != NULL)
9895 {
9896 if (!find_line_pc (sal->symtab, sal->line, &pc))
9897 error (_("No line %d in file \"%s\"."),
9898 sal->line, symtab_to_filename_for_display (sal->symtab));
9899 sal->pc = pc;
9900
9901 /* If this SAL corresponds to a breakpoint inserted using a line
9902 number, then skip the function prologue if necessary. */
9903 if (sal->explicit_line)
9904 skip_prologue_sal (sal);
9905 }
9906
9907 if (sal->section == 0 && sal->symtab != NULL)
9908 {
9909 struct blockvector *bv;
9910 struct block *b;
9911 struct symbol *sym;
9912
9913 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9914 if (bv != NULL)
9915 {
9916 sym = block_linkage_function (b);
9917 if (sym != NULL)
9918 {
9919 fixup_symbol_section (sym, sal->symtab->objfile);
9920 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
9921 }
9922 else
9923 {
9924 /* It really is worthwhile to have the section, so we'll
9925 just have to look harder. This case can be executed
9926 if we have line numbers but no functions (as can
9927 happen in assembly source). */
9928
9929 struct bound_minimal_symbol msym;
9930 struct cleanup *old_chain = save_current_space_and_thread ();
9931
9932 switch_to_program_space_and_thread (sal->pspace);
9933
9934 msym = lookup_minimal_symbol_by_pc (sal->pc);
9935 if (msym.minsym)
9936 sal->section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9937
9938 do_cleanups (old_chain);
9939 }
9940 }
9941 }
9942}
9943
9944void
9945break_command (char *arg, int from_tty)
9946{
9947 break_command_1 (arg, 0, from_tty);
9948}
9949
9950void
9951tbreak_command (char *arg, int from_tty)
9952{
9953 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9954}
9955
9956static void
9957hbreak_command (char *arg, int from_tty)
9958{
9959 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9960}
9961
9962static void
9963thbreak_command (char *arg, int from_tty)
9964{
9965 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9966}
9967
9968static void
9969stop_command (char *arg, int from_tty)
9970{
9971 printf_filtered (_("Specify the type of breakpoint to set.\n\
9972Usage: stop in <function | address>\n\
9973 stop at <line>\n"));
9974}
9975
9976static void
9977stopin_command (char *arg, int from_tty)
9978{
9979 int badInput = 0;
9980
9981 if (arg == (char *) NULL)
9982 badInput = 1;
9983 else if (*arg != '*')
9984 {
9985 char *argptr = arg;
9986 int hasColon = 0;
9987
9988 /* Look for a ':'. If this is a line number specification, then
9989 say it is bad, otherwise, it should be an address or
9990 function/method name. */
9991 while (*argptr && !hasColon)
9992 {
9993 hasColon = (*argptr == ':');
9994 argptr++;
9995 }
9996
9997 if (hasColon)
9998 badInput = (*argptr != ':'); /* Not a class::method */
9999 else
10000 badInput = isdigit (*arg); /* a simple line number */
10001 }
10002
10003 if (badInput)
10004 printf_filtered (_("Usage: stop in <function | address>\n"));
10005 else
10006 break_command_1 (arg, 0, from_tty);
10007}
10008
10009static void
10010stopat_command (char *arg, int from_tty)
10011{
10012 int badInput = 0;
10013
10014 if (arg == (char *) NULL || *arg == '*') /* no line number */
10015 badInput = 1;
10016 else
10017 {
10018 char *argptr = arg;
10019 int hasColon = 0;
10020
10021 /* Look for a ':'. If there is a '::' then get out, otherwise
10022 it is probably a line number. */
10023 while (*argptr && !hasColon)
10024 {
10025 hasColon = (*argptr == ':');
10026 argptr++;
10027 }
10028
10029 if (hasColon)
10030 badInput = (*argptr == ':'); /* we have class::method */
10031 else
10032 badInput = !isdigit (*arg); /* not a line number */
10033 }
10034
10035 if (badInput)
10036 printf_filtered (_("Usage: stop at <line>\n"));
10037 else
10038 break_command_1 (arg, 0, from_tty);
10039}
10040
10041/* The dynamic printf command is mostly like a regular breakpoint, but
10042 with a prewired command list consisting of a single output command,
10043 built from extra arguments supplied on the dprintf command
10044 line. */
10045
10046static void
10047dprintf_command (char *arg, int from_tty)
10048{
10049 create_breakpoint (get_current_arch (),
10050 arg,
10051 NULL, 0, NULL, 1 /* parse arg */,
10052 0, bp_dprintf,
10053 0 /* Ignore count */,
10054 pending_break_support,
10055 &dprintf_breakpoint_ops,
10056 from_tty,
10057 1 /* enabled */,
10058 0 /* internal */,
10059 0);
10060}
10061
10062static void
10063agent_printf_command (char *arg, int from_tty)
10064{
10065 error (_("May only run agent-printf on the target"));
10066}
10067
10068/* Implement the "breakpoint_hit" breakpoint_ops method for
10069 ranged breakpoints. */
10070
10071static int
10072breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10073 struct address_space *aspace,
10074 CORE_ADDR bp_addr,
10075 const struct target_waitstatus *ws)
10076{
10077 if (ws->kind != TARGET_WAITKIND_STOPPED
10078 || ws->value.sig != GDB_SIGNAL_TRAP)
10079 return 0;
10080
10081 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10082 bl->length, aspace, bp_addr);
10083}
10084
10085/* Implement the "resources_needed" breakpoint_ops method for
10086 ranged breakpoints. */
10087
10088static int
10089resources_needed_ranged_breakpoint (const struct bp_location *bl)
10090{
10091 return target_ranged_break_num_registers ();
10092}
10093
10094/* Implement the "print_it" breakpoint_ops method for
10095 ranged breakpoints. */
10096
10097static enum print_stop_action
10098print_it_ranged_breakpoint (bpstat bs)
10099{
10100 struct breakpoint *b = bs->breakpoint_at;
10101 struct bp_location *bl = b->loc;
10102 struct ui_out *uiout = current_uiout;
10103
10104 gdb_assert (b->type == bp_hardware_breakpoint);
10105
10106 /* Ranged breakpoints have only one location. */
10107 gdb_assert (bl && bl->next == NULL);
10108
10109 annotate_breakpoint (b->number);
10110 if (b->disposition == disp_del)
10111 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10112 else
10113 ui_out_text (uiout, "\nRanged breakpoint ");
10114 if (ui_out_is_mi_like_p (uiout))
10115 {
10116 ui_out_field_string (uiout, "reason",
10117 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10118 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10119 }
10120 ui_out_field_int (uiout, "bkptno", b->number);
10121 ui_out_text (uiout, ", ");
10122
10123 return PRINT_SRC_AND_LOC;
10124}
10125
10126/* Implement the "print_one" breakpoint_ops method for
10127 ranged breakpoints. */
10128
10129static void
10130print_one_ranged_breakpoint (struct breakpoint *b,
10131 struct bp_location **last_loc)
10132{
10133 struct bp_location *bl = b->loc;
10134 struct value_print_options opts;
10135 struct ui_out *uiout = current_uiout;
10136
10137 /* Ranged breakpoints have only one location. */
10138 gdb_assert (bl && bl->next == NULL);
10139
10140 get_user_print_options (&opts);
10141
10142 if (opts.addressprint)
10143 /* We don't print the address range here, it will be printed later
10144 by print_one_detail_ranged_breakpoint. */
10145 ui_out_field_skip (uiout, "addr");
10146 annotate_field (5);
10147 print_breakpoint_location (b, bl);
10148 *last_loc = bl;
10149}
10150
10151/* Implement the "print_one_detail" breakpoint_ops method for
10152 ranged breakpoints. */
10153
10154static void
10155print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10156 struct ui_out *uiout)
10157{
10158 CORE_ADDR address_start, address_end;
10159 struct bp_location *bl = b->loc;
10160 struct ui_file *stb = mem_fileopen ();
10161 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10162
10163 gdb_assert (bl);
10164
10165 address_start = bl->address;
10166 address_end = address_start + bl->length - 1;
10167
10168 ui_out_text (uiout, "\taddress range: ");
10169 fprintf_unfiltered (stb, "[%s, %s]",
10170 print_core_address (bl->gdbarch, address_start),
10171 print_core_address (bl->gdbarch, address_end));
10172 ui_out_field_stream (uiout, "addr", stb);
10173 ui_out_text (uiout, "\n");
10174
10175 do_cleanups (cleanup);
10176}
10177
10178/* Implement the "print_mention" breakpoint_ops method for
10179 ranged breakpoints. */
10180
10181static void
10182print_mention_ranged_breakpoint (struct breakpoint *b)
10183{
10184 struct bp_location *bl = b->loc;
10185 struct ui_out *uiout = current_uiout;
10186
10187 gdb_assert (bl);
10188 gdb_assert (b->type == bp_hardware_breakpoint);
10189
10190 if (ui_out_is_mi_like_p (uiout))
10191 return;
10192
10193 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10194 b->number, paddress (bl->gdbarch, bl->address),
10195 paddress (bl->gdbarch, bl->address + bl->length - 1));
10196}
10197
10198/* Implement the "print_recreate" breakpoint_ops method for
10199 ranged breakpoints. */
10200
10201static void
10202print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10203{
10204 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10205 b->addr_string_range_end);
10206 print_recreate_thread (b, fp);
10207}
10208
10209/* The breakpoint_ops structure to be used in ranged breakpoints. */
10210
10211static struct breakpoint_ops ranged_breakpoint_ops;
10212
10213/* Find the address where the end of the breakpoint range should be
10214 placed, given the SAL of the end of the range. This is so that if
10215 the user provides a line number, the end of the range is set to the
10216 last instruction of the given line. */
10217
10218static CORE_ADDR
10219find_breakpoint_range_end (struct symtab_and_line sal)
10220{
10221 CORE_ADDR end;
10222
10223 /* If the user provided a PC value, use it. Otherwise,
10224 find the address of the end of the given location. */
10225 if (sal.explicit_pc)
10226 end = sal.pc;
10227 else
10228 {
10229 int ret;
10230 CORE_ADDR start;
10231
10232 ret = find_line_pc_range (sal, &start, &end);
10233 if (!ret)
10234 error (_("Could not find location of the end of the range."));
10235
10236 /* find_line_pc_range returns the start of the next line. */
10237 end--;
10238 }
10239
10240 return end;
10241}
10242
10243/* Implement the "break-range" CLI command. */
10244
10245static void
10246break_range_command (char *arg, int from_tty)
10247{
10248 char *arg_start, *addr_string_start, *addr_string_end;
10249 struct linespec_result canonical_start, canonical_end;
10250 int bp_count, can_use_bp, length;
10251 CORE_ADDR end;
10252 struct breakpoint *b;
10253 struct symtab_and_line sal_start, sal_end;
10254 struct cleanup *cleanup_bkpt;
10255 struct linespec_sals *lsal_start, *lsal_end;
10256
10257 /* We don't support software ranged breakpoints. */
10258 if (target_ranged_break_num_registers () < 0)
10259 error (_("This target does not support hardware ranged breakpoints."));
10260
10261 bp_count = hw_breakpoint_used_count ();
10262 bp_count += target_ranged_break_num_registers ();
10263 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10264 bp_count, 0);
10265 if (can_use_bp < 0)
10266 error (_("Hardware breakpoints used exceeds limit."));
10267
10268 arg = skip_spaces (arg);
10269 if (arg == NULL || arg[0] == '\0')
10270 error(_("No address range specified."));
10271
10272 init_linespec_result (&canonical_start);
10273
10274 arg_start = arg;
10275 parse_breakpoint_sals (&arg, &canonical_start);
10276
10277 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10278
10279 if (arg[0] != ',')
10280 error (_("Too few arguments."));
10281 else if (VEC_empty (linespec_sals, canonical_start.sals))
10282 error (_("Could not find location of the beginning of the range."));
10283
10284 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10285
10286 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10287 || lsal_start->sals.nelts != 1)
10288 error (_("Cannot create a ranged breakpoint with multiple locations."));
10289
10290 sal_start = lsal_start->sals.sals[0];
10291 addr_string_start = savestring (arg_start, arg - arg_start);
10292 make_cleanup (xfree, addr_string_start);
10293
10294 arg++; /* Skip the comma. */
10295 arg = skip_spaces (arg);
10296
10297 /* Parse the end location. */
10298
10299 init_linespec_result (&canonical_end);
10300 arg_start = arg;
10301
10302 /* We call decode_line_full directly here instead of using
10303 parse_breakpoint_sals because we need to specify the start location's
10304 symtab and line as the default symtab and line for the end of the
10305 range. This makes it possible to have ranges like "foo.c:27, +14",
10306 where +14 means 14 lines from the start location. */
10307 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10308 sal_start.symtab, sal_start.line,
10309 &canonical_end, NULL, NULL);
10310
10311 make_cleanup_destroy_linespec_result (&canonical_end);
10312
10313 if (VEC_empty (linespec_sals, canonical_end.sals))
10314 error (_("Could not find location of the end of the range."));
10315
10316 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10317 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10318 || lsal_end->sals.nelts != 1)
10319 error (_("Cannot create a ranged breakpoint with multiple locations."));
10320
10321 sal_end = lsal_end->sals.sals[0];
10322 addr_string_end = savestring (arg_start, arg - arg_start);
10323 make_cleanup (xfree, addr_string_end);
10324
10325 end = find_breakpoint_range_end (sal_end);
10326 if (sal_start.pc > end)
10327 error (_("Invalid address range, end precedes start."));
10328
10329 length = end - sal_start.pc + 1;
10330 if (length < 0)
10331 /* Length overflowed. */
10332 error (_("Address range too large."));
10333 else if (length == 1)
10334 {
10335 /* This range is simple enough to be handled by
10336 the `hbreak' command. */
10337 hbreak_command (addr_string_start, 1);
10338
10339 do_cleanups (cleanup_bkpt);
10340
10341 return;
10342 }
10343
10344 /* Now set up the breakpoint. */
10345 b = set_raw_breakpoint (get_current_arch (), sal_start,
10346 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10347 set_breakpoint_count (breakpoint_count + 1);
10348 b->number = breakpoint_count;
10349 b->disposition = disp_donttouch;
10350 b->addr_string = xstrdup (addr_string_start);
10351 b->addr_string_range_end = xstrdup (addr_string_end);
10352 b->loc->length = length;
10353
10354 do_cleanups (cleanup_bkpt);
10355
10356 mention (b);
10357 observer_notify_breakpoint_created (b);
10358 update_global_location_list (1);
10359}
10360
10361/* Return non-zero if EXP is verified as constant. Returned zero
10362 means EXP is variable. Also the constant detection may fail for
10363 some constant expressions and in such case still falsely return
10364 zero. */
10365
10366static int
10367watchpoint_exp_is_const (const struct expression *exp)
10368{
10369 int i = exp->nelts;
10370
10371 while (i > 0)
10372 {
10373 int oplenp, argsp;
10374
10375 /* We are only interested in the descriptor of each element. */
10376 operator_length (exp, i, &oplenp, &argsp);
10377 i -= oplenp;
10378
10379 switch (exp->elts[i].opcode)
10380 {
10381 case BINOP_ADD:
10382 case BINOP_SUB:
10383 case BINOP_MUL:
10384 case BINOP_DIV:
10385 case BINOP_REM:
10386 case BINOP_MOD:
10387 case BINOP_LSH:
10388 case BINOP_RSH:
10389 case BINOP_LOGICAL_AND:
10390 case BINOP_LOGICAL_OR:
10391 case BINOP_BITWISE_AND:
10392 case BINOP_BITWISE_IOR:
10393 case BINOP_BITWISE_XOR:
10394 case BINOP_EQUAL:
10395 case BINOP_NOTEQUAL:
10396 case BINOP_LESS:
10397 case BINOP_GTR:
10398 case BINOP_LEQ:
10399 case BINOP_GEQ:
10400 case BINOP_REPEAT:
10401 case BINOP_COMMA:
10402 case BINOP_EXP:
10403 case BINOP_MIN:
10404 case BINOP_MAX:
10405 case BINOP_INTDIV:
10406 case BINOP_CONCAT:
10407 case BINOP_IN:
10408 case BINOP_RANGE:
10409 case TERNOP_COND:
10410 case TERNOP_SLICE:
10411
10412 case OP_LONG:
10413 case OP_DOUBLE:
10414 case OP_DECFLOAT:
10415 case OP_LAST:
10416 case OP_COMPLEX:
10417 case OP_STRING:
10418 case OP_ARRAY:
10419 case OP_TYPE:
10420 case OP_TYPEOF:
10421 case OP_DECLTYPE:
10422 case OP_TYPEID:
10423 case OP_NAME:
10424 case OP_OBJC_NSSTRING:
10425
10426 case UNOP_NEG:
10427 case UNOP_LOGICAL_NOT:
10428 case UNOP_COMPLEMENT:
10429 case UNOP_ADDR:
10430 case UNOP_HIGH:
10431 case UNOP_CAST:
10432
10433 case UNOP_CAST_TYPE:
10434 case UNOP_REINTERPRET_CAST:
10435 case UNOP_DYNAMIC_CAST:
10436 /* Unary, binary and ternary operators: We have to check
10437 their operands. If they are constant, then so is the
10438 result of that operation. For instance, if A and B are
10439 determined to be constants, then so is "A + B".
10440
10441 UNOP_IND is one exception to the rule above, because the
10442 value of *ADDR is not necessarily a constant, even when
10443 ADDR is. */
10444 break;
10445
10446 case OP_VAR_VALUE:
10447 /* Check whether the associated symbol is a constant.
10448
10449 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10450 possible that a buggy compiler could mark a variable as
10451 constant even when it is not, and TYPE_CONST would return
10452 true in this case, while SYMBOL_CLASS wouldn't.
10453
10454 We also have to check for function symbols because they
10455 are always constant. */
10456 {
10457 struct symbol *s = exp->elts[i + 2].symbol;
10458
10459 if (SYMBOL_CLASS (s) != LOC_BLOCK
10460 && SYMBOL_CLASS (s) != LOC_CONST
10461 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10462 return 0;
10463 break;
10464 }
10465
10466 /* The default action is to return 0 because we are using
10467 the optimistic approach here: If we don't know something,
10468 then it is not a constant. */
10469 default:
10470 return 0;
10471 }
10472 }
10473
10474 return 1;
10475}
10476
10477/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10478
10479static void
10480dtor_watchpoint (struct breakpoint *self)
10481{
10482 struct watchpoint *w = (struct watchpoint *) self;
10483
10484 xfree (w->cond_exp);
10485 xfree (w->exp);
10486 xfree (w->exp_string);
10487 xfree (w->exp_string_reparse);
10488 value_free (w->val);
10489
10490 base_breakpoint_ops.dtor (self);
10491}
10492
10493/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10494
10495static void
10496re_set_watchpoint (struct breakpoint *b)
10497{
10498 struct watchpoint *w = (struct watchpoint *) b;
10499
10500 /* Watchpoint can be either on expression using entirely global
10501 variables, or it can be on local variables.
10502
10503 Watchpoints of the first kind are never auto-deleted, and even
10504 persist across program restarts. Since they can use variables
10505 from shared libraries, we need to reparse expression as libraries
10506 are loaded and unloaded.
10507
10508 Watchpoints on local variables can also change meaning as result
10509 of solib event. For example, if a watchpoint uses both a local
10510 and a global variables in expression, it's a local watchpoint,
10511 but unloading of a shared library will make the expression
10512 invalid. This is not a very common use case, but we still
10513 re-evaluate expression, to avoid surprises to the user.
10514
10515 Note that for local watchpoints, we re-evaluate it only if
10516 watchpoints frame id is still valid. If it's not, it means the
10517 watchpoint is out of scope and will be deleted soon. In fact,
10518 I'm not sure we'll ever be called in this case.
10519
10520 If a local watchpoint's frame id is still valid, then
10521 w->exp_valid_block is likewise valid, and we can safely use it.
10522
10523 Don't do anything about disabled watchpoints, since they will be
10524 reevaluated again when enabled. */
10525 update_watchpoint (w, 1 /* reparse */);
10526}
10527
10528/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10529
10530static int
10531insert_watchpoint (struct bp_location *bl)
10532{
10533 struct watchpoint *w = (struct watchpoint *) bl->owner;
10534 int length = w->exact ? 1 : bl->length;
10535
10536 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10537 w->cond_exp);
10538}
10539
10540/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10541
10542static int
10543remove_watchpoint (struct bp_location *bl)
10544{
10545 struct watchpoint *w = (struct watchpoint *) bl->owner;
10546 int length = w->exact ? 1 : bl->length;
10547
10548 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10549 w->cond_exp);
10550}
10551
10552static int
10553breakpoint_hit_watchpoint (const struct bp_location *bl,
10554 struct address_space *aspace, CORE_ADDR bp_addr,
10555 const struct target_waitstatus *ws)
10556{
10557 struct breakpoint *b = bl->owner;
10558 struct watchpoint *w = (struct watchpoint *) b;
10559
10560 /* Continuable hardware watchpoints are treated as non-existent if the
10561 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10562 some data address). Otherwise gdb won't stop on a break instruction
10563 in the code (not from a breakpoint) when a hardware watchpoint has
10564 been defined. Also skip watchpoints which we know did not trigger
10565 (did not match the data address). */
10566 if (is_hardware_watchpoint (b)
10567 && w->watchpoint_triggered == watch_triggered_no)
10568 return 0;
10569
10570 return 1;
10571}
10572
10573static void
10574check_status_watchpoint (bpstat bs)
10575{
10576 gdb_assert (is_watchpoint (bs->breakpoint_at));
10577
10578 bpstat_check_watchpoint (bs);
10579}
10580
10581/* Implement the "resources_needed" breakpoint_ops method for
10582 hardware watchpoints. */
10583
10584static int
10585resources_needed_watchpoint (const struct bp_location *bl)
10586{
10587 struct watchpoint *w = (struct watchpoint *) bl->owner;
10588 int length = w->exact? 1 : bl->length;
10589
10590 return target_region_ok_for_hw_watchpoint (bl->address, length);
10591}
10592
10593/* Implement the "works_in_software_mode" breakpoint_ops method for
10594 hardware watchpoints. */
10595
10596static int
10597works_in_software_mode_watchpoint (const struct breakpoint *b)
10598{
10599 /* Read and access watchpoints only work with hardware support. */
10600 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10601}
10602
10603static enum print_stop_action
10604print_it_watchpoint (bpstat bs)
10605{
10606 struct cleanup *old_chain;
10607 struct breakpoint *b;
10608 struct ui_file *stb;
10609 enum print_stop_action result;
10610 struct watchpoint *w;
10611 struct ui_out *uiout = current_uiout;
10612
10613 gdb_assert (bs->bp_location_at != NULL);
10614
10615 b = bs->breakpoint_at;
10616 w = (struct watchpoint *) b;
10617
10618 stb = mem_fileopen ();
10619 old_chain = make_cleanup_ui_file_delete (stb);
10620
10621 switch (b->type)
10622 {
10623 case bp_watchpoint:
10624 case bp_hardware_watchpoint:
10625 annotate_watchpoint (b->number);
10626 if (ui_out_is_mi_like_p (uiout))
10627 ui_out_field_string
10628 (uiout, "reason",
10629 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10630 mention (b);
10631 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10632 ui_out_text (uiout, "\nOld value = ");
10633 watchpoint_value_print (bs->old_val, stb);
10634 ui_out_field_stream (uiout, "old", stb);
10635 ui_out_text (uiout, "\nNew value = ");
10636 watchpoint_value_print (w->val, stb);
10637 ui_out_field_stream (uiout, "new", stb);
10638 ui_out_text (uiout, "\n");
10639 /* More than one watchpoint may have been triggered. */
10640 result = PRINT_UNKNOWN;
10641 break;
10642
10643 case bp_read_watchpoint:
10644 if (ui_out_is_mi_like_p (uiout))
10645 ui_out_field_string
10646 (uiout, "reason",
10647 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10648 mention (b);
10649 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10650 ui_out_text (uiout, "\nValue = ");
10651 watchpoint_value_print (w->val, stb);
10652 ui_out_field_stream (uiout, "value", stb);
10653 ui_out_text (uiout, "\n");
10654 result = PRINT_UNKNOWN;
10655 break;
10656
10657 case bp_access_watchpoint:
10658 if (bs->old_val != NULL)
10659 {
10660 annotate_watchpoint (b->number);
10661 if (ui_out_is_mi_like_p (uiout))
10662 ui_out_field_string
10663 (uiout, "reason",
10664 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10665 mention (b);
10666 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10667 ui_out_text (uiout, "\nOld value = ");
10668 watchpoint_value_print (bs->old_val, stb);
10669 ui_out_field_stream (uiout, "old", stb);
10670 ui_out_text (uiout, "\nNew value = ");
10671 }
10672 else
10673 {
10674 mention (b);
10675 if (ui_out_is_mi_like_p (uiout))
10676 ui_out_field_string
10677 (uiout, "reason",
10678 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10679 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10680 ui_out_text (uiout, "\nValue = ");
10681 }
10682 watchpoint_value_print (w->val, stb);
10683 ui_out_field_stream (uiout, "new", stb);
10684 ui_out_text (uiout, "\n");
10685 result = PRINT_UNKNOWN;
10686 break;
10687 default:
10688 result = PRINT_UNKNOWN;
10689 }
10690
10691 do_cleanups (old_chain);
10692 return result;
10693}
10694
10695/* Implement the "print_mention" breakpoint_ops method for hardware
10696 watchpoints. */
10697
10698static void
10699print_mention_watchpoint (struct breakpoint *b)
10700{
10701 struct cleanup *ui_out_chain;
10702 struct watchpoint *w = (struct watchpoint *) b;
10703 struct ui_out *uiout = current_uiout;
10704
10705 switch (b->type)
10706 {
10707 case bp_watchpoint:
10708 ui_out_text (uiout, "Watchpoint ");
10709 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10710 break;
10711 case bp_hardware_watchpoint:
10712 ui_out_text (uiout, "Hardware watchpoint ");
10713 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10714 break;
10715 case bp_read_watchpoint:
10716 ui_out_text (uiout, "Hardware read watchpoint ");
10717 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10718 break;
10719 case bp_access_watchpoint:
10720 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10721 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10722 break;
10723 default:
10724 internal_error (__FILE__, __LINE__,
10725 _("Invalid hardware watchpoint type."));
10726 }
10727
10728 ui_out_field_int (uiout, "number", b->number);
10729 ui_out_text (uiout, ": ");
10730 ui_out_field_string (uiout, "exp", w->exp_string);
10731 do_cleanups (ui_out_chain);
10732}
10733
10734/* Implement the "print_recreate" breakpoint_ops method for
10735 watchpoints. */
10736
10737static void
10738print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10739{
10740 struct watchpoint *w = (struct watchpoint *) b;
10741
10742 switch (b->type)
10743 {
10744 case bp_watchpoint:
10745 case bp_hardware_watchpoint:
10746 fprintf_unfiltered (fp, "watch");
10747 break;
10748 case bp_read_watchpoint:
10749 fprintf_unfiltered (fp, "rwatch");
10750 break;
10751 case bp_access_watchpoint:
10752 fprintf_unfiltered (fp, "awatch");
10753 break;
10754 default:
10755 internal_error (__FILE__, __LINE__,
10756 _("Invalid watchpoint type."));
10757 }
10758
10759 fprintf_unfiltered (fp, " %s", w->exp_string);
10760 print_recreate_thread (b, fp);
10761}
10762
10763/* Implement the "explains_signal" breakpoint_ops method for
10764 watchpoints. */
10765
10766static int
10767explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10768{
10769 /* A software watchpoint cannot cause a signal other than
10770 GDB_SIGNAL_TRAP. */
10771 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10772 return 0;
10773
10774 return 1;
10775}
10776
10777/* The breakpoint_ops structure to be used in hardware watchpoints. */
10778
10779static struct breakpoint_ops watchpoint_breakpoint_ops;
10780
10781/* Implement the "insert" breakpoint_ops method for
10782 masked hardware watchpoints. */
10783
10784static int
10785insert_masked_watchpoint (struct bp_location *bl)
10786{
10787 struct watchpoint *w = (struct watchpoint *) bl->owner;
10788
10789 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10790 bl->watchpoint_type);
10791}
10792
10793/* Implement the "remove" breakpoint_ops method for
10794 masked hardware watchpoints. */
10795
10796static int
10797remove_masked_watchpoint (struct bp_location *bl)
10798{
10799 struct watchpoint *w = (struct watchpoint *) bl->owner;
10800
10801 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10802 bl->watchpoint_type);
10803}
10804
10805/* Implement the "resources_needed" breakpoint_ops method for
10806 masked hardware watchpoints. */
10807
10808static int
10809resources_needed_masked_watchpoint (const struct bp_location *bl)
10810{
10811 struct watchpoint *w = (struct watchpoint *) bl->owner;
10812
10813 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10814}
10815
10816/* Implement the "works_in_software_mode" breakpoint_ops method for
10817 masked hardware watchpoints. */
10818
10819static int
10820works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10821{
10822 return 0;
10823}
10824
10825/* Implement the "print_it" breakpoint_ops method for
10826 masked hardware watchpoints. */
10827
10828static enum print_stop_action
10829print_it_masked_watchpoint (bpstat bs)
10830{
10831 struct breakpoint *b = bs->breakpoint_at;
10832 struct ui_out *uiout = current_uiout;
10833
10834 /* Masked watchpoints have only one location. */
10835 gdb_assert (b->loc && b->loc->next == NULL);
10836
10837 switch (b->type)
10838 {
10839 case bp_hardware_watchpoint:
10840 annotate_watchpoint (b->number);
10841 if (ui_out_is_mi_like_p (uiout))
10842 ui_out_field_string
10843 (uiout, "reason",
10844 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10845 break;
10846
10847 case bp_read_watchpoint:
10848 if (ui_out_is_mi_like_p (uiout))
10849 ui_out_field_string
10850 (uiout, "reason",
10851 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10852 break;
10853
10854 case bp_access_watchpoint:
10855 if (ui_out_is_mi_like_p (uiout))
10856 ui_out_field_string
10857 (uiout, "reason",
10858 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10859 break;
10860 default:
10861 internal_error (__FILE__, __LINE__,
10862 _("Invalid hardware watchpoint type."));
10863 }
10864
10865 mention (b);
10866 ui_out_text (uiout, _("\n\
10867Check the underlying instruction at PC for the memory\n\
10868address and value which triggered this watchpoint.\n"));
10869 ui_out_text (uiout, "\n");
10870
10871 /* More than one watchpoint may have been triggered. */
10872 return PRINT_UNKNOWN;
10873}
10874
10875/* Implement the "print_one_detail" breakpoint_ops method for
10876 masked hardware watchpoints. */
10877
10878static void
10879print_one_detail_masked_watchpoint (const struct breakpoint *b,
10880 struct ui_out *uiout)
10881{
10882 struct watchpoint *w = (struct watchpoint *) b;
10883
10884 /* Masked watchpoints have only one location. */
10885 gdb_assert (b->loc && b->loc->next == NULL);
10886
10887 ui_out_text (uiout, "\tmask ");
10888 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10889 ui_out_text (uiout, "\n");
10890}
10891
10892/* Implement the "print_mention" breakpoint_ops method for
10893 masked hardware watchpoints. */
10894
10895static void
10896print_mention_masked_watchpoint (struct breakpoint *b)
10897{
10898 struct watchpoint *w = (struct watchpoint *) b;
10899 struct ui_out *uiout = current_uiout;
10900 struct cleanup *ui_out_chain;
10901
10902 switch (b->type)
10903 {
10904 case bp_hardware_watchpoint:
10905 ui_out_text (uiout, "Masked hardware watchpoint ");
10906 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10907 break;
10908 case bp_read_watchpoint:
10909 ui_out_text (uiout, "Masked hardware read watchpoint ");
10910 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10911 break;
10912 case bp_access_watchpoint:
10913 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10914 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10915 break;
10916 default:
10917 internal_error (__FILE__, __LINE__,
10918 _("Invalid hardware watchpoint type."));
10919 }
10920
10921 ui_out_field_int (uiout, "number", b->number);
10922 ui_out_text (uiout, ": ");
10923 ui_out_field_string (uiout, "exp", w->exp_string);
10924 do_cleanups (ui_out_chain);
10925}
10926
10927/* Implement the "print_recreate" breakpoint_ops method for
10928 masked hardware watchpoints. */
10929
10930static void
10931print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10932{
10933 struct watchpoint *w = (struct watchpoint *) b;
10934 char tmp[40];
10935
10936 switch (b->type)
10937 {
10938 case bp_hardware_watchpoint:
10939 fprintf_unfiltered (fp, "watch");
10940 break;
10941 case bp_read_watchpoint:
10942 fprintf_unfiltered (fp, "rwatch");
10943 break;
10944 case bp_access_watchpoint:
10945 fprintf_unfiltered (fp, "awatch");
10946 break;
10947 default:
10948 internal_error (__FILE__, __LINE__,
10949 _("Invalid hardware watchpoint type."));
10950 }
10951
10952 sprintf_vma (tmp, w->hw_wp_mask);
10953 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10954 print_recreate_thread (b, fp);
10955}
10956
10957/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10958
10959static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10960
10961/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10962
10963static int
10964is_masked_watchpoint (const struct breakpoint *b)
10965{
10966 return b->ops == &masked_watchpoint_breakpoint_ops;
10967}
10968
10969/* accessflag: hw_write: watch write,
10970 hw_read: watch read,
10971 hw_access: watch access (read or write) */
10972static void
10973watch_command_1 (const char *arg, int accessflag, int from_tty,
10974 int just_location, int internal)
10975{
10976 volatile struct gdb_exception e;
10977 struct breakpoint *b, *scope_breakpoint = NULL;
10978 struct expression *exp;
10979 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10980 struct value *val, *mark, *result;
10981 struct frame_info *frame;
10982 const char *exp_start = NULL;
10983 const char *exp_end = NULL;
10984 const char *tok, *end_tok;
10985 int toklen = -1;
10986 const char *cond_start = NULL;
10987 const char *cond_end = NULL;
10988 enum bptype bp_type;
10989 int thread = -1;
10990 int pc = 0;
10991 /* Flag to indicate whether we are going to use masks for
10992 the hardware watchpoint. */
10993 int use_mask = 0;
10994 CORE_ADDR mask = 0;
10995 struct watchpoint *w;
10996 char *expression;
10997 struct cleanup *back_to;
10998
10999 /* Make sure that we actually have parameters to parse. */
11000 if (arg != NULL && arg[0] != '\0')
11001 {
11002 const char *value_start;
11003
11004 exp_end = arg + strlen (arg);
11005
11006 /* Look for "parameter value" pairs at the end
11007 of the arguments string. */
11008 for (tok = exp_end - 1; tok > arg; tok--)
11009 {
11010 /* Skip whitespace at the end of the argument list. */
11011 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11012 tok--;
11013
11014 /* Find the beginning of the last token.
11015 This is the value of the parameter. */
11016 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11017 tok--;
11018 value_start = tok + 1;
11019
11020 /* Skip whitespace. */
11021 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11022 tok--;
11023
11024 end_tok = tok;
11025
11026 /* Find the beginning of the second to last token.
11027 This is the parameter itself. */
11028 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11029 tok--;
11030 tok++;
11031 toklen = end_tok - tok + 1;
11032
11033 if (toklen == 6 && !strncmp (tok, "thread", 6))
11034 {
11035 /* At this point we've found a "thread" token, which means
11036 the user is trying to set a watchpoint that triggers
11037 only in a specific thread. */
11038 char *endp;
11039
11040 if (thread != -1)
11041 error(_("You can specify only one thread."));
11042
11043 /* Extract the thread ID from the next token. */
11044 thread = strtol (value_start, &endp, 0);
11045
11046 /* Check if the user provided a valid numeric value for the
11047 thread ID. */
11048 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11049 error (_("Invalid thread ID specification %s."), value_start);
11050
11051 /* Check if the thread actually exists. */
11052 if (!valid_thread_id (thread))
11053 invalid_thread_id_error (thread);
11054 }
11055 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11056 {
11057 /* We've found a "mask" token, which means the user wants to
11058 create a hardware watchpoint that is going to have the mask
11059 facility. */
11060 struct value *mask_value, *mark;
11061
11062 if (use_mask)
11063 error(_("You can specify only one mask."));
11064
11065 use_mask = just_location = 1;
11066
11067 mark = value_mark ();
11068 mask_value = parse_to_comma_and_eval (&value_start);
11069 mask = value_as_address (mask_value);
11070 value_free_to_mark (mark);
11071 }
11072 else
11073 /* We didn't recognize what we found. We should stop here. */
11074 break;
11075
11076 /* Truncate the string and get rid of the "parameter value" pair before
11077 the arguments string is parsed by the parse_exp_1 function. */
11078 exp_end = tok;
11079 }
11080 }
11081 else
11082 exp_end = arg;
11083
11084 /* Parse the rest of the arguments. From here on out, everything
11085 is in terms of a newly allocated string instead of the original
11086 ARG. */
11087 innermost_block = NULL;
11088 expression = savestring (arg, exp_end - arg);
11089 back_to = make_cleanup (xfree, expression);
11090 exp_start = arg = expression;
11091 exp = parse_exp_1 (&arg, 0, 0, 0);
11092 exp_end = arg;
11093 /* Remove trailing whitespace from the expression before saving it.
11094 This makes the eventual display of the expression string a bit
11095 prettier. */
11096 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11097 --exp_end;
11098
11099 /* Checking if the expression is not constant. */
11100 if (watchpoint_exp_is_const (exp))
11101 {
11102 int len;
11103
11104 len = exp_end - exp_start;
11105 while (len > 0 && isspace (exp_start[len - 1]))
11106 len--;
11107 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11108 }
11109
11110 exp_valid_block = innermost_block;
11111 mark = value_mark ();
11112 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11113
11114 if (just_location)
11115 {
11116 int ret;
11117
11118 exp_valid_block = NULL;
11119 val = value_addr (result);
11120 release_value (val);
11121 value_free_to_mark (mark);
11122
11123 if (use_mask)
11124 {
11125 ret = target_masked_watch_num_registers (value_as_address (val),
11126 mask);
11127 if (ret == -1)
11128 error (_("This target does not support masked watchpoints."));
11129 else if (ret == -2)
11130 error (_("Invalid mask or memory region."));
11131 }
11132 }
11133 else if (val != NULL)
11134 release_value (val);
11135
11136 tok = skip_spaces_const (arg);
11137 end_tok = skip_to_space_const (tok);
11138
11139 toklen = end_tok - tok;
11140 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11141 {
11142 struct expression *cond;
11143
11144 innermost_block = NULL;
11145 tok = cond_start = end_tok + 1;
11146 cond = parse_exp_1 (&tok, 0, 0, 0);
11147
11148 /* The watchpoint expression may not be local, but the condition
11149 may still be. E.g.: `watch global if local > 0'. */
11150 cond_exp_valid_block = innermost_block;
11151
11152 xfree (cond);
11153 cond_end = tok;
11154 }
11155 if (*tok)
11156 error (_("Junk at end of command."));
11157
11158 frame = block_innermost_frame (exp_valid_block);
11159
11160 /* If the expression is "local", then set up a "watchpoint scope"
11161 breakpoint at the point where we've left the scope of the watchpoint
11162 expression. Create the scope breakpoint before the watchpoint, so
11163 that we will encounter it first in bpstat_stop_status. */
11164 if (exp_valid_block && frame)
11165 {
11166 if (frame_id_p (frame_unwind_caller_id (frame)))
11167 {
11168 scope_breakpoint
11169 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11170 frame_unwind_caller_pc (frame),
11171 bp_watchpoint_scope,
11172 &momentary_breakpoint_ops);
11173
11174 scope_breakpoint->enable_state = bp_enabled;
11175
11176 /* Automatically delete the breakpoint when it hits. */
11177 scope_breakpoint->disposition = disp_del;
11178
11179 /* Only break in the proper frame (help with recursion). */
11180 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11181
11182 /* Set the address at which we will stop. */
11183 scope_breakpoint->loc->gdbarch
11184 = frame_unwind_caller_arch (frame);
11185 scope_breakpoint->loc->requested_address
11186 = frame_unwind_caller_pc (frame);
11187 scope_breakpoint->loc->address
11188 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11189 scope_breakpoint->loc->requested_address,
11190 scope_breakpoint->type);
11191 }
11192 }
11193
11194 /* Now set up the breakpoint. We create all watchpoints as hardware
11195 watchpoints here even if hardware watchpoints are turned off, a call
11196 to update_watchpoint later in this function will cause the type to
11197 drop back to bp_watchpoint (software watchpoint) if required. */
11198
11199 if (accessflag == hw_read)
11200 bp_type = bp_read_watchpoint;
11201 else if (accessflag == hw_access)
11202 bp_type = bp_access_watchpoint;
11203 else
11204 bp_type = bp_hardware_watchpoint;
11205
11206 w = XCNEW (struct watchpoint);
11207 b = &w->base;
11208 if (use_mask)
11209 init_raw_breakpoint_without_location (b, NULL, bp_type,
11210 &masked_watchpoint_breakpoint_ops);
11211 else
11212 init_raw_breakpoint_without_location (b, NULL, bp_type,
11213 &watchpoint_breakpoint_ops);
11214 b->thread = thread;
11215 b->disposition = disp_donttouch;
11216 b->pspace = current_program_space;
11217 w->exp = exp;
11218 w->exp_valid_block = exp_valid_block;
11219 w->cond_exp_valid_block = cond_exp_valid_block;
11220 if (just_location)
11221 {
11222 struct type *t = value_type (val);
11223 CORE_ADDR addr = value_as_address (val);
11224 char *name;
11225
11226 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11227 name = type_to_string (t);
11228
11229 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11230 core_addr_to_string (addr));
11231 xfree (name);
11232
11233 w->exp_string = xstrprintf ("-location %.*s",
11234 (int) (exp_end - exp_start), exp_start);
11235
11236 /* The above expression is in C. */
11237 b->language = language_c;
11238 }
11239 else
11240 w->exp_string = savestring (exp_start, exp_end - exp_start);
11241
11242 if (use_mask)
11243 {
11244 w->hw_wp_mask = mask;
11245 }
11246 else
11247 {
11248 w->val = val;
11249 w->val_valid = 1;
11250 }
11251
11252 if (cond_start)
11253 b->cond_string = savestring (cond_start, cond_end - cond_start);
11254 else
11255 b->cond_string = 0;
11256
11257 if (frame)
11258 {
11259 w->watchpoint_frame = get_frame_id (frame);
11260 w->watchpoint_thread = inferior_ptid;
11261 }
11262 else
11263 {
11264 w->watchpoint_frame = null_frame_id;
11265 w->watchpoint_thread = null_ptid;
11266 }
11267
11268 if (scope_breakpoint != NULL)
11269 {
11270 /* The scope breakpoint is related to the watchpoint. We will
11271 need to act on them together. */
11272 b->related_breakpoint = scope_breakpoint;
11273 scope_breakpoint->related_breakpoint = b;
11274 }
11275
11276 if (!just_location)
11277 value_free_to_mark (mark);
11278
11279 TRY_CATCH (e, RETURN_MASK_ALL)
11280 {
11281 /* Finally update the new watchpoint. This creates the locations
11282 that should be inserted. */
11283 update_watchpoint (w, 1);
11284 }
11285 if (e.reason < 0)
11286 {
11287 delete_breakpoint (b);
11288 throw_exception (e);
11289 }
11290
11291 install_breakpoint (internal, b, 1);
11292 do_cleanups (back_to);
11293}
11294
11295/* Return count of debug registers needed to watch the given expression.
11296 If the watchpoint cannot be handled in hardware return zero. */
11297
11298static int
11299can_use_hardware_watchpoint (struct value *v)
11300{
11301 int found_memory_cnt = 0;
11302 struct value *head = v;
11303
11304 /* Did the user specifically forbid us to use hardware watchpoints? */
11305 if (!can_use_hw_watchpoints)
11306 return 0;
11307
11308 /* Make sure that the value of the expression depends only upon
11309 memory contents, and values computed from them within GDB. If we
11310 find any register references or function calls, we can't use a
11311 hardware watchpoint.
11312
11313 The idea here is that evaluating an expression generates a series
11314 of values, one holding the value of every subexpression. (The
11315 expression a*b+c has five subexpressions: a, b, a*b, c, and
11316 a*b+c.) GDB's values hold almost enough information to establish
11317 the criteria given above --- they identify memory lvalues,
11318 register lvalues, computed values, etcetera. So we can evaluate
11319 the expression, and then scan the chain of values that leaves
11320 behind to decide whether we can detect any possible change to the
11321 expression's final value using only hardware watchpoints.
11322
11323 However, I don't think that the values returned by inferior
11324 function calls are special in any way. So this function may not
11325 notice that an expression involving an inferior function call
11326 can't be watched with hardware watchpoints. FIXME. */
11327 for (; v; v = value_next (v))
11328 {
11329 if (VALUE_LVAL (v) == lval_memory)
11330 {
11331 if (v != head && value_lazy (v))
11332 /* A lazy memory lvalue in the chain is one that GDB never
11333 needed to fetch; we either just used its address (e.g.,
11334 `a' in `a.b') or we never needed it at all (e.g., `a'
11335 in `a,b'). This doesn't apply to HEAD; if that is
11336 lazy then it was not readable, but watch it anyway. */
11337 ;
11338 else
11339 {
11340 /* Ahh, memory we actually used! Check if we can cover
11341 it with hardware watchpoints. */
11342 struct type *vtype = check_typedef (value_type (v));
11343
11344 /* We only watch structs and arrays if user asked for it
11345 explicitly, never if they just happen to appear in a
11346 middle of some value chain. */
11347 if (v == head
11348 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11349 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11350 {
11351 CORE_ADDR vaddr = value_address (v);
11352 int len;
11353 int num_regs;
11354
11355 len = (target_exact_watchpoints
11356 && is_scalar_type_recursive (vtype))?
11357 1 : TYPE_LENGTH (value_type (v));
11358
11359 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11360 if (!num_regs)
11361 return 0;
11362 else
11363 found_memory_cnt += num_regs;
11364 }
11365 }
11366 }
11367 else if (VALUE_LVAL (v) != not_lval
11368 && deprecated_value_modifiable (v) == 0)
11369 return 0; /* These are values from the history (e.g., $1). */
11370 else if (VALUE_LVAL (v) == lval_register)
11371 return 0; /* Cannot watch a register with a HW watchpoint. */
11372 }
11373
11374 /* The expression itself looks suitable for using a hardware
11375 watchpoint, but give the target machine a chance to reject it. */
11376 return found_memory_cnt;
11377}
11378
11379void
11380watch_command_wrapper (char *arg, int from_tty, int internal)
11381{
11382 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11383}
11384
11385/* A helper function that looks for the "-location" argument and then
11386 calls watch_command_1. */
11387
11388static void
11389watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11390{
11391 int just_location = 0;
11392
11393 if (arg
11394 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11395 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11396 {
11397 arg = skip_spaces (arg);
11398 just_location = 1;
11399 }
11400
11401 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11402}
11403
11404static void
11405watch_command (char *arg, int from_tty)
11406{
11407 watch_maybe_just_location (arg, hw_write, from_tty);
11408}
11409
11410void
11411rwatch_command_wrapper (char *arg, int from_tty, int internal)
11412{
11413 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11414}
11415
11416static void
11417rwatch_command (char *arg, int from_tty)
11418{
11419 watch_maybe_just_location (arg, hw_read, from_tty);
11420}
11421
11422void
11423awatch_command_wrapper (char *arg, int from_tty, int internal)
11424{
11425 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11426}
11427
11428static void
11429awatch_command (char *arg, int from_tty)
11430{
11431 watch_maybe_just_location (arg, hw_access, from_tty);
11432}
11433\f
11434
11435/* Helper routines for the until_command routine in infcmd.c. Here
11436 because it uses the mechanisms of breakpoints. */
11437
11438struct until_break_command_continuation_args
11439{
11440 struct breakpoint *breakpoint;
11441 struct breakpoint *breakpoint2;
11442 int thread_num;
11443};
11444
11445/* This function is called by fetch_inferior_event via the
11446 cmd_continuation pointer, to complete the until command. It takes
11447 care of cleaning up the temporary breakpoints set up by the until
11448 command. */
11449static void
11450until_break_command_continuation (void *arg, int err)
11451{
11452 struct until_break_command_continuation_args *a = arg;
11453
11454 delete_breakpoint (a->breakpoint);
11455 if (a->breakpoint2)
11456 delete_breakpoint (a->breakpoint2);
11457 delete_longjmp_breakpoint (a->thread_num);
11458}
11459
11460void
11461until_break_command (char *arg, int from_tty, int anywhere)
11462{
11463 struct symtabs_and_lines sals;
11464 struct symtab_and_line sal;
11465 struct frame_info *frame;
11466 struct gdbarch *frame_gdbarch;
11467 struct frame_id stack_frame_id;
11468 struct frame_id caller_frame_id;
11469 struct breakpoint *breakpoint;
11470 struct breakpoint *breakpoint2 = NULL;
11471 struct cleanup *old_chain;
11472 int thread;
11473 struct thread_info *tp;
11474
11475 clear_proceed_status ();
11476
11477 /* Set a breakpoint where the user wants it and at return from
11478 this function. */
11479
11480 if (last_displayed_sal_is_valid ())
11481 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11482 get_last_displayed_symtab (),
11483 get_last_displayed_line ());
11484 else
11485 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11486 (struct symtab *) NULL, 0);
11487
11488 if (sals.nelts != 1)
11489 error (_("Couldn't get information on specified line."));
11490
11491 sal = sals.sals[0];
11492 xfree (sals.sals); /* malloc'd, so freed. */
11493
11494 if (*arg)
11495 error (_("Junk at end of arguments."));
11496
11497 resolve_sal_pc (&sal);
11498
11499 tp = inferior_thread ();
11500 thread = tp->num;
11501
11502 old_chain = make_cleanup (null_cleanup, NULL);
11503
11504 /* Note linespec handling above invalidates the frame chain.
11505 Installing a breakpoint also invalidates the frame chain (as it
11506 may need to switch threads), so do any frame handling before
11507 that. */
11508
11509 frame = get_selected_frame (NULL);
11510 frame_gdbarch = get_frame_arch (frame);
11511 stack_frame_id = get_stack_frame_id (frame);
11512 caller_frame_id = frame_unwind_caller_id (frame);
11513
11514 /* Keep within the current frame, or in frames called by the current
11515 one. */
11516
11517 if (frame_id_p (caller_frame_id))
11518 {
11519 struct symtab_and_line sal2;
11520
11521 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11522 sal2.pc = frame_unwind_caller_pc (frame);
11523 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11524 sal2,
11525 caller_frame_id,
11526 bp_until);
11527 make_cleanup_delete_breakpoint (breakpoint2);
11528
11529 set_longjmp_breakpoint (tp, caller_frame_id);
11530 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11531 }
11532
11533 /* set_momentary_breakpoint could invalidate FRAME. */
11534 frame = NULL;
11535
11536 if (anywhere)
11537 /* If the user told us to continue until a specified location,
11538 we don't specify a frame at which we need to stop. */
11539 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11540 null_frame_id, bp_until);
11541 else
11542 /* Otherwise, specify the selected frame, because we want to stop
11543 only at the very same frame. */
11544 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11545 stack_frame_id, bp_until);
11546 make_cleanup_delete_breakpoint (breakpoint);
11547
11548 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11549
11550 /* If we are running asynchronously, and proceed call above has
11551 actually managed to start the target, arrange for breakpoints to
11552 be deleted when the target stops. Otherwise, we're already
11553 stopped and delete breakpoints via cleanup chain. */
11554
11555 if (target_can_async_p () && is_running (inferior_ptid))
11556 {
11557 struct until_break_command_continuation_args *args;
11558 args = xmalloc (sizeof (*args));
11559
11560 args->breakpoint = breakpoint;
11561 args->breakpoint2 = breakpoint2;
11562 args->thread_num = thread;
11563
11564 discard_cleanups (old_chain);
11565 add_continuation (inferior_thread (),
11566 until_break_command_continuation, args,
11567 xfree);
11568 }
11569 else
11570 do_cleanups (old_chain);
11571}
11572
11573/* This function attempts to parse an optional "if <cond>" clause
11574 from the arg string. If one is not found, it returns NULL.
11575
11576 Else, it returns a pointer to the condition string. (It does not
11577 attempt to evaluate the string against a particular block.) And,
11578 it updates arg to point to the first character following the parsed
11579 if clause in the arg string. */
11580
11581char *
11582ep_parse_optional_if_clause (char **arg)
11583{
11584 char *cond_string;
11585
11586 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11587 return NULL;
11588
11589 /* Skip the "if" keyword. */
11590 (*arg) += 2;
11591
11592 /* Skip any extra leading whitespace, and record the start of the
11593 condition string. */
11594 *arg = skip_spaces (*arg);
11595 cond_string = *arg;
11596
11597 /* Assume that the condition occupies the remainder of the arg
11598 string. */
11599 (*arg) += strlen (cond_string);
11600
11601 return cond_string;
11602}
11603
11604/* Commands to deal with catching events, such as signals, exceptions,
11605 process start/exit, etc. */
11606
11607typedef enum
11608{
11609 catch_fork_temporary, catch_vfork_temporary,
11610 catch_fork_permanent, catch_vfork_permanent
11611}
11612catch_fork_kind;
11613
11614static void
11615catch_fork_command_1 (char *arg, int from_tty,
11616 struct cmd_list_element *command)
11617{
11618 struct gdbarch *gdbarch = get_current_arch ();
11619 char *cond_string = NULL;
11620 catch_fork_kind fork_kind;
11621 int tempflag;
11622
11623 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11624 tempflag = (fork_kind == catch_fork_temporary
11625 || fork_kind == catch_vfork_temporary);
11626
11627 if (!arg)
11628 arg = "";
11629 arg = skip_spaces (arg);
11630
11631 /* The allowed syntax is:
11632 catch [v]fork
11633 catch [v]fork if <cond>
11634
11635 First, check if there's an if clause. */
11636 cond_string = ep_parse_optional_if_clause (&arg);
11637
11638 if ((*arg != '\0') && !isspace (*arg))
11639 error (_("Junk at end of arguments."));
11640
11641 /* If this target supports it, create a fork or vfork catchpoint
11642 and enable reporting of such events. */
11643 switch (fork_kind)
11644 {
11645 case catch_fork_temporary:
11646 case catch_fork_permanent:
11647 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11648 &catch_fork_breakpoint_ops);
11649 break;
11650 case catch_vfork_temporary:
11651 case catch_vfork_permanent:
11652 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11653 &catch_vfork_breakpoint_ops);
11654 break;
11655 default:
11656 error (_("unsupported or unknown fork kind; cannot catch it"));
11657 break;
11658 }
11659}
11660
11661static void
11662catch_exec_command_1 (char *arg, int from_tty,
11663 struct cmd_list_element *command)
11664{
11665 struct exec_catchpoint *c;
11666 struct gdbarch *gdbarch = get_current_arch ();
11667 int tempflag;
11668 char *cond_string = NULL;
11669
11670 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11671
11672 if (!arg)
11673 arg = "";
11674 arg = skip_spaces (arg);
11675
11676 /* The allowed syntax is:
11677 catch exec
11678 catch exec if <cond>
11679
11680 First, check if there's an if clause. */
11681 cond_string = ep_parse_optional_if_clause (&arg);
11682
11683 if ((*arg != '\0') && !isspace (*arg))
11684 error (_("Junk at end of arguments."));
11685
11686 c = XNEW (struct exec_catchpoint);
11687 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11688 &catch_exec_breakpoint_ops);
11689 c->exec_pathname = NULL;
11690
11691 install_breakpoint (0, &c->base, 1);
11692}
11693
11694void
11695init_ada_exception_breakpoint (struct breakpoint *b,
11696 struct gdbarch *gdbarch,
11697 struct symtab_and_line sal,
11698 char *addr_string,
11699 const struct breakpoint_ops *ops,
11700 int tempflag,
11701 int enabled,
11702 int from_tty)
11703{
11704 if (from_tty)
11705 {
11706 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11707 if (!loc_gdbarch)
11708 loc_gdbarch = gdbarch;
11709
11710 describe_other_breakpoints (loc_gdbarch,
11711 sal.pspace, sal.pc, sal.section, -1);
11712 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11713 version for exception catchpoints, because two catchpoints
11714 used for different exception names will use the same address.
11715 In this case, a "breakpoint ... also set at..." warning is
11716 unproductive. Besides, the warning phrasing is also a bit
11717 inappropriate, we should use the word catchpoint, and tell
11718 the user what type of catchpoint it is. The above is good
11719 enough for now, though. */
11720 }
11721
11722 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11723
11724 b->enable_state = enabled ? bp_enabled : bp_disabled;
11725 b->disposition = tempflag ? disp_del : disp_donttouch;
11726 b->addr_string = addr_string;
11727 b->language = language_ada;
11728}
11729
11730/* Splits the argument using space as delimiter. Returns an xmalloc'd
11731 filter list, or NULL if no filtering is required. */
11732static VEC(int) *
11733catch_syscall_split_args (char *arg)
11734{
11735 VEC(int) *result = NULL;
11736 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
11737
11738 while (*arg != '\0')
11739 {
11740 int i, syscall_number;
11741 char *endptr;
11742 char cur_name[128];
11743 struct syscall s;
11744
11745 /* Skip whitespace. */
11746 arg = skip_spaces (arg);
11747
11748 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11749 cur_name[i] = arg[i];
11750 cur_name[i] = '\0';
11751 arg += i;
11752
11753 /* Check if the user provided a syscall name or a number. */
11754 syscall_number = (int) strtol (cur_name, &endptr, 0);
11755 if (*endptr == '\0')
11756 get_syscall_by_number (syscall_number, &s);
11757 else
11758 {
11759 /* We have a name. Let's check if it's valid and convert it
11760 to a number. */
11761 get_syscall_by_name (cur_name, &s);
11762
11763 if (s.number == UNKNOWN_SYSCALL)
11764 /* Here we have to issue an error instead of a warning,
11765 because GDB cannot do anything useful if there's no
11766 syscall number to be caught. */
11767 error (_("Unknown syscall name '%s'."), cur_name);
11768 }
11769
11770 /* Ok, it's valid. */
11771 VEC_safe_push (int, result, s.number);
11772 }
11773
11774 discard_cleanups (cleanup);
11775 return result;
11776}
11777
11778/* Implement the "catch syscall" command. */
11779
11780static void
11781catch_syscall_command_1 (char *arg, int from_tty,
11782 struct cmd_list_element *command)
11783{
11784 int tempflag;
11785 VEC(int) *filter;
11786 struct syscall s;
11787 struct gdbarch *gdbarch = get_current_arch ();
11788
11789 /* Checking if the feature if supported. */
11790 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11791 error (_("The feature 'catch syscall' is not supported on \
11792this architecture yet."));
11793
11794 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11795
11796 arg = skip_spaces (arg);
11797
11798 /* We need to do this first "dummy" translation in order
11799 to get the syscall XML file loaded or, most important,
11800 to display a warning to the user if there's no XML file
11801 for his/her architecture. */
11802 get_syscall_by_number (0, &s);
11803
11804 /* The allowed syntax is:
11805 catch syscall
11806 catch syscall <name | number> [<name | number> ... <name | number>]
11807
11808 Let's check if there's a syscall name. */
11809
11810 if (arg != NULL)
11811 filter = catch_syscall_split_args (arg);
11812 else
11813 filter = NULL;
11814
11815 create_syscall_event_catchpoint (tempflag, filter,
11816 &catch_syscall_breakpoint_ops);
11817}
11818
11819static void
11820catch_command (char *arg, int from_tty)
11821{
11822 error (_("Catch requires an event name."));
11823}
11824\f
11825
11826static void
11827tcatch_command (char *arg, int from_tty)
11828{
11829 error (_("Catch requires an event name."));
11830}
11831
11832/* A qsort comparison function that sorts breakpoints in order. */
11833
11834static int
11835compare_breakpoints (const void *a, const void *b)
11836{
11837 const breakpoint_p *ba = a;
11838 uintptr_t ua = (uintptr_t) *ba;
11839 const breakpoint_p *bb = b;
11840 uintptr_t ub = (uintptr_t) *bb;
11841
11842 if ((*ba)->number < (*bb)->number)
11843 return -1;
11844 else if ((*ba)->number > (*bb)->number)
11845 return 1;
11846
11847 /* Now sort by address, in case we see, e..g, two breakpoints with
11848 the number 0. */
11849 if (ua < ub)
11850 return -1;
11851 return ua > ub ? 1 : 0;
11852}
11853
11854/* Delete breakpoints by address or line. */
11855
11856static void
11857clear_command (char *arg, int from_tty)
11858{
11859 struct breakpoint *b, *prev;
11860 VEC(breakpoint_p) *found = 0;
11861 int ix;
11862 int default_match;
11863 struct symtabs_and_lines sals;
11864 struct symtab_and_line sal;
11865 int i;
11866 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11867
11868 if (arg)
11869 {
11870 sals = decode_line_with_current_source (arg,
11871 (DECODE_LINE_FUNFIRSTLINE
11872 | DECODE_LINE_LIST_MODE));
11873 make_cleanup (xfree, sals.sals);
11874 default_match = 0;
11875 }
11876 else
11877 {
11878 sals.sals = (struct symtab_and_line *)
11879 xmalloc (sizeof (struct symtab_and_line));
11880 make_cleanup (xfree, sals.sals);
11881 init_sal (&sal); /* Initialize to zeroes. */
11882
11883 /* Set sal's line, symtab, pc, and pspace to the values
11884 corresponding to the last call to print_frame_info. If the
11885 codepoint is not valid, this will set all the fields to 0. */
11886 get_last_displayed_sal (&sal);
11887 if (sal.symtab == 0)
11888 error (_("No source file specified."));
11889
11890 sals.sals[0] = sal;
11891 sals.nelts = 1;
11892
11893 default_match = 1;
11894 }
11895
11896 /* We don't call resolve_sal_pc here. That's not as bad as it
11897 seems, because all existing breakpoints typically have both
11898 file/line and pc set. So, if clear is given file/line, we can
11899 match this to existing breakpoint without obtaining pc at all.
11900
11901 We only support clearing given the address explicitly
11902 present in breakpoint table. Say, we've set breakpoint
11903 at file:line. There were several PC values for that file:line,
11904 due to optimization, all in one block.
11905
11906 We've picked one PC value. If "clear" is issued with another
11907 PC corresponding to the same file:line, the breakpoint won't
11908 be cleared. We probably can still clear the breakpoint, but
11909 since the other PC value is never presented to user, user
11910 can only find it by guessing, and it does not seem important
11911 to support that. */
11912
11913 /* For each line spec given, delete bps which correspond to it. Do
11914 it in two passes, solely to preserve the current behavior that
11915 from_tty is forced true if we delete more than one
11916 breakpoint. */
11917
11918 found = NULL;
11919 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11920 for (i = 0; i < sals.nelts; i++)
11921 {
11922 const char *sal_fullname;
11923
11924 /* If exact pc given, clear bpts at that pc.
11925 If line given (pc == 0), clear all bpts on specified line.
11926 If defaulting, clear all bpts on default line
11927 or at default pc.
11928
11929 defaulting sal.pc != 0 tests to do
11930
11931 0 1 pc
11932 1 1 pc _and_ line
11933 0 0 line
11934 1 0 <can't happen> */
11935
11936 sal = sals.sals[i];
11937 sal_fullname = (sal.symtab == NULL
11938 ? NULL : symtab_to_fullname (sal.symtab));
11939
11940 /* Find all matching breakpoints and add them to 'found'. */
11941 ALL_BREAKPOINTS (b)
11942 {
11943 int match = 0;
11944 /* Are we going to delete b? */
11945 if (b->type != bp_none && !is_watchpoint (b))
11946 {
11947 struct bp_location *loc = b->loc;
11948 for (; loc; loc = loc->next)
11949 {
11950 /* If the user specified file:line, don't allow a PC
11951 match. This matches historical gdb behavior. */
11952 int pc_match = (!sal.explicit_line
11953 && sal.pc
11954 && (loc->pspace == sal.pspace)
11955 && (loc->address == sal.pc)
11956 && (!section_is_overlay (loc->section)
11957 || loc->section == sal.section));
11958 int line_match = 0;
11959
11960 if ((default_match || sal.explicit_line)
11961 && loc->symtab != NULL
11962 && sal_fullname != NULL
11963 && sal.pspace == loc->pspace
11964 && loc->line_number == sal.line
11965 && filename_cmp (symtab_to_fullname (loc->symtab),
11966 sal_fullname) == 0)
11967 line_match = 1;
11968
11969 if (pc_match || line_match)
11970 {
11971 match = 1;
11972 break;
11973 }
11974 }
11975 }
11976
11977 if (match)
11978 VEC_safe_push(breakpoint_p, found, b);
11979 }
11980 }
11981
11982 /* Now go thru the 'found' chain and delete them. */
11983 if (VEC_empty(breakpoint_p, found))
11984 {
11985 if (arg)
11986 error (_("No breakpoint at %s."), arg);
11987 else
11988 error (_("No breakpoint at this line."));
11989 }
11990
11991 /* Remove duplicates from the vec. */
11992 qsort (VEC_address (breakpoint_p, found),
11993 VEC_length (breakpoint_p, found),
11994 sizeof (breakpoint_p),
11995 compare_breakpoints);
11996 prev = VEC_index (breakpoint_p, found, 0);
11997 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11998 {
11999 if (b == prev)
12000 {
12001 VEC_ordered_remove (breakpoint_p, found, ix);
12002 --ix;
12003 }
12004 }
12005
12006 if (VEC_length(breakpoint_p, found) > 1)
12007 from_tty = 1; /* Always report if deleted more than one. */
12008 if (from_tty)
12009 {
12010 if (VEC_length(breakpoint_p, found) == 1)
12011 printf_unfiltered (_("Deleted breakpoint "));
12012 else
12013 printf_unfiltered (_("Deleted breakpoints "));
12014 }
12015
12016 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12017 {
12018 if (from_tty)
12019 printf_unfiltered ("%d ", b->number);
12020 delete_breakpoint (b);
12021 }
12022 if (from_tty)
12023 putchar_unfiltered ('\n');
12024
12025 do_cleanups (cleanups);
12026}
12027\f
12028/* Delete breakpoint in BS if they are `delete' breakpoints and
12029 all breakpoints that are marked for deletion, whether hit or not.
12030 This is called after any breakpoint is hit, or after errors. */
12031
12032void
12033breakpoint_auto_delete (bpstat bs)
12034{
12035 struct breakpoint *b, *b_tmp;
12036
12037 for (; bs; bs = bs->next)
12038 if (bs->breakpoint_at
12039 && bs->breakpoint_at->disposition == disp_del
12040 && bs->stop)
12041 delete_breakpoint (bs->breakpoint_at);
12042
12043 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12044 {
12045 if (b->disposition == disp_del_at_next_stop)
12046 delete_breakpoint (b);
12047 }
12048}
12049
12050/* A comparison function for bp_location AP and BP being interfaced to
12051 qsort. Sort elements primarily by their ADDRESS (no matter what
12052 does breakpoint_address_is_meaningful say for its OWNER),
12053 secondarily by ordering first bp_permanent OWNERed elements and
12054 terciarily just ensuring the array is sorted stable way despite
12055 qsort being an unstable algorithm. */
12056
12057static int
12058bp_location_compare (const void *ap, const void *bp)
12059{
12060 struct bp_location *a = *(void **) ap;
12061 struct bp_location *b = *(void **) bp;
12062 /* A and B come from existing breakpoints having non-NULL OWNER. */
12063 int a_perm = a->owner->enable_state == bp_permanent;
12064 int b_perm = b->owner->enable_state == bp_permanent;
12065
12066 if (a->address != b->address)
12067 return (a->address > b->address) - (a->address < b->address);
12068
12069 /* Sort locations at the same address by their pspace number, keeping
12070 locations of the same inferior (in a multi-inferior environment)
12071 grouped. */
12072
12073 if (a->pspace->num != b->pspace->num)
12074 return ((a->pspace->num > b->pspace->num)
12075 - (a->pspace->num < b->pspace->num));
12076
12077 /* Sort permanent breakpoints first. */
12078 if (a_perm != b_perm)
12079 return (a_perm < b_perm) - (a_perm > b_perm);
12080
12081 /* Make the internal GDB representation stable across GDB runs
12082 where A and B memory inside GDB can differ. Breakpoint locations of
12083 the same type at the same address can be sorted in arbitrary order. */
12084
12085 if (a->owner->number != b->owner->number)
12086 return ((a->owner->number > b->owner->number)
12087 - (a->owner->number < b->owner->number));
12088
12089 return (a > b) - (a < b);
12090}
12091
12092/* Set bp_location_placed_address_before_address_max and
12093 bp_location_shadow_len_after_address_max according to the current
12094 content of the bp_location array. */
12095
12096static void
12097bp_location_target_extensions_update (void)
12098{
12099 struct bp_location *bl, **blp_tmp;
12100
12101 bp_location_placed_address_before_address_max = 0;
12102 bp_location_shadow_len_after_address_max = 0;
12103
12104 ALL_BP_LOCATIONS (bl, blp_tmp)
12105 {
12106 CORE_ADDR start, end, addr;
12107
12108 if (!bp_location_has_shadow (bl))
12109 continue;
12110
12111 start = bl->target_info.placed_address;
12112 end = start + bl->target_info.shadow_len;
12113
12114 gdb_assert (bl->address >= start);
12115 addr = bl->address - start;
12116 if (addr > bp_location_placed_address_before_address_max)
12117 bp_location_placed_address_before_address_max = addr;
12118
12119 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12120
12121 gdb_assert (bl->address < end);
12122 addr = end - bl->address;
12123 if (addr > bp_location_shadow_len_after_address_max)
12124 bp_location_shadow_len_after_address_max = addr;
12125 }
12126}
12127
12128/* Download tracepoint locations if they haven't been. */
12129
12130static void
12131download_tracepoint_locations (void)
12132{
12133 struct breakpoint *b;
12134 struct cleanup *old_chain;
12135
12136 if (!target_can_download_tracepoint ())
12137 return;
12138
12139 old_chain = save_current_space_and_thread ();
12140
12141 ALL_TRACEPOINTS (b)
12142 {
12143 struct bp_location *bl;
12144 struct tracepoint *t;
12145 int bp_location_downloaded = 0;
12146
12147 if ((b->type == bp_fast_tracepoint
12148 ? !may_insert_fast_tracepoints
12149 : !may_insert_tracepoints))
12150 continue;
12151
12152 for (bl = b->loc; bl; bl = bl->next)
12153 {
12154 /* In tracepoint, locations are _never_ duplicated, so
12155 should_be_inserted is equivalent to
12156 unduplicated_should_be_inserted. */
12157 if (!should_be_inserted (bl) || bl->inserted)
12158 continue;
12159
12160 switch_to_program_space_and_thread (bl->pspace);
12161
12162 target_download_tracepoint (bl);
12163
12164 bl->inserted = 1;
12165 bp_location_downloaded = 1;
12166 }
12167 t = (struct tracepoint *) b;
12168 t->number_on_target = b->number;
12169 if (bp_location_downloaded)
12170 observer_notify_breakpoint_modified (b);
12171 }
12172
12173 do_cleanups (old_chain);
12174}
12175
12176/* Swap the insertion/duplication state between two locations. */
12177
12178static void
12179swap_insertion (struct bp_location *left, struct bp_location *right)
12180{
12181 const int left_inserted = left->inserted;
12182 const int left_duplicate = left->duplicate;
12183 const int left_needs_update = left->needs_update;
12184 const struct bp_target_info left_target_info = left->target_info;
12185
12186 /* Locations of tracepoints can never be duplicated. */
12187 if (is_tracepoint (left->owner))
12188 gdb_assert (!left->duplicate);
12189 if (is_tracepoint (right->owner))
12190 gdb_assert (!right->duplicate);
12191
12192 left->inserted = right->inserted;
12193 left->duplicate = right->duplicate;
12194 left->needs_update = right->needs_update;
12195 left->target_info = right->target_info;
12196 right->inserted = left_inserted;
12197 right->duplicate = left_duplicate;
12198 right->needs_update = left_needs_update;
12199 right->target_info = left_target_info;
12200}
12201
12202/* Force the re-insertion of the locations at ADDRESS. This is called
12203 once a new/deleted/modified duplicate location is found and we are evaluating
12204 conditions on the target's side. Such conditions need to be updated on
12205 the target. */
12206
12207static void
12208force_breakpoint_reinsertion (struct bp_location *bl)
12209{
12210 struct bp_location **locp = NULL, **loc2p;
12211 struct bp_location *loc;
12212 CORE_ADDR address = 0;
12213 int pspace_num;
12214
12215 address = bl->address;
12216 pspace_num = bl->pspace->num;
12217
12218 /* This is only meaningful if the target is
12219 evaluating conditions and if the user has
12220 opted for condition evaluation on the target's
12221 side. */
12222 if (gdb_evaluates_breakpoint_condition_p ()
12223 || !target_supports_evaluation_of_breakpoint_conditions ())
12224 return;
12225
12226 /* Flag all breakpoint locations with this address and
12227 the same program space as the location
12228 as "its condition has changed". We need to
12229 update the conditions on the target's side. */
12230 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12231 {
12232 loc = *loc2p;
12233
12234 if (!is_breakpoint (loc->owner)
12235 || pspace_num != loc->pspace->num)
12236 continue;
12237
12238 /* Flag the location appropriately. We use a different state to
12239 let everyone know that we already updated the set of locations
12240 with addr bl->address and program space bl->pspace. This is so
12241 we don't have to keep calling these functions just to mark locations
12242 that have already been marked. */
12243 loc->condition_changed = condition_updated;
12244
12245 /* Free the agent expression bytecode as well. We will compute
12246 it later on. */
12247 if (loc->cond_bytecode)
12248 {
12249 free_agent_expr (loc->cond_bytecode);
12250 loc->cond_bytecode = NULL;
12251 }
12252 }
12253}
12254
12255/* If SHOULD_INSERT is false, do not insert any breakpoint locations
12256 into the inferior, only remove already-inserted locations that no
12257 longer should be inserted. Functions that delete a breakpoint or
12258 breakpoints should pass false, so that deleting a breakpoint
12259 doesn't have the side effect of inserting the locations of other
12260 breakpoints that are marked not-inserted, but should_be_inserted
12261 returns true on them.
12262
12263 This behaviour is useful is situations close to tear-down -- e.g.,
12264 after an exec, while the target still has execution, but breakpoint
12265 shadows of the previous executable image should *NOT* be restored
12266 to the new image; or before detaching, where the target still has
12267 execution and wants to delete breakpoints from GDB's lists, and all
12268 breakpoints had already been removed from the inferior. */
12269
12270static void
12271update_global_location_list (int should_insert)
12272{
12273 struct breakpoint *b;
12274 struct bp_location **locp, *loc;
12275 struct cleanup *cleanups;
12276 /* Last breakpoint location address that was marked for update. */
12277 CORE_ADDR last_addr = 0;
12278 /* Last breakpoint location program space that was marked for update. */
12279 int last_pspace_num = -1;
12280
12281 /* Used in the duplicates detection below. When iterating over all
12282 bp_locations, points to the first bp_location of a given address.
12283 Breakpoints and watchpoints of different types are never
12284 duplicates of each other. Keep one pointer for each type of
12285 breakpoint/watchpoint, so we only need to loop over all locations
12286 once. */
12287 struct bp_location *bp_loc_first; /* breakpoint */
12288 struct bp_location *wp_loc_first; /* hardware watchpoint */
12289 struct bp_location *awp_loc_first; /* access watchpoint */
12290 struct bp_location *rwp_loc_first; /* read watchpoint */
12291
12292 /* Saved former bp_location array which we compare against the newly
12293 built bp_location from the current state of ALL_BREAKPOINTS. */
12294 struct bp_location **old_location, **old_locp;
12295 unsigned old_location_count;
12296
12297 old_location = bp_location;
12298 old_location_count = bp_location_count;
12299 bp_location = NULL;
12300 bp_location_count = 0;
12301 cleanups = make_cleanup (xfree, old_location);
12302
12303 ALL_BREAKPOINTS (b)
12304 for (loc = b->loc; loc; loc = loc->next)
12305 bp_location_count++;
12306
12307 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12308 locp = bp_location;
12309 ALL_BREAKPOINTS (b)
12310 for (loc = b->loc; loc; loc = loc->next)
12311 *locp++ = loc;
12312 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12313 bp_location_compare);
12314
12315 bp_location_target_extensions_update ();
12316
12317 /* Identify bp_location instances that are no longer present in the
12318 new list, and therefore should be freed. Note that it's not
12319 necessary that those locations should be removed from inferior --
12320 if there's another location at the same address (previously
12321 marked as duplicate), we don't need to remove/insert the
12322 location.
12323
12324 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12325 and former bp_location array state respectively. */
12326
12327 locp = bp_location;
12328 for (old_locp = old_location; old_locp < old_location + old_location_count;
12329 old_locp++)
12330 {
12331 struct bp_location *old_loc = *old_locp;
12332 struct bp_location **loc2p;
12333
12334 /* Tells if 'old_loc' is found among the new locations. If
12335 not, we have to free it. */
12336 int found_object = 0;
12337 /* Tells if the location should remain inserted in the target. */
12338 int keep_in_target = 0;
12339 int removed = 0;
12340
12341 /* Skip LOCP entries which will definitely never be needed.
12342 Stop either at or being the one matching OLD_LOC. */
12343 while (locp < bp_location + bp_location_count
12344 && (*locp)->address < old_loc->address)
12345 locp++;
12346
12347 for (loc2p = locp;
12348 (loc2p < bp_location + bp_location_count
12349 && (*loc2p)->address == old_loc->address);
12350 loc2p++)
12351 {
12352 /* Check if this is a new/duplicated location or a duplicated
12353 location that had its condition modified. If so, we want to send
12354 its condition to the target if evaluation of conditions is taking
12355 place there. */
12356 if ((*loc2p)->condition_changed == condition_modified
12357 && (last_addr != old_loc->address
12358 || last_pspace_num != old_loc->pspace->num))
12359 {
12360 force_breakpoint_reinsertion (*loc2p);
12361 last_pspace_num = old_loc->pspace->num;
12362 }
12363
12364 if (*loc2p == old_loc)
12365 found_object = 1;
12366 }
12367
12368 /* We have already handled this address, update it so that we don't
12369 have to go through updates again. */
12370 last_addr = old_loc->address;
12371
12372 /* Target-side condition evaluation: Handle deleted locations. */
12373 if (!found_object)
12374 force_breakpoint_reinsertion (old_loc);
12375
12376 /* If this location is no longer present, and inserted, look if
12377 there's maybe a new location at the same address. If so,
12378 mark that one inserted, and don't remove this one. This is
12379 needed so that we don't have a time window where a breakpoint
12380 at certain location is not inserted. */
12381
12382 if (old_loc->inserted)
12383 {
12384 /* If the location is inserted now, we might have to remove
12385 it. */
12386
12387 if (found_object && should_be_inserted (old_loc))
12388 {
12389 /* The location is still present in the location list,
12390 and still should be inserted. Don't do anything. */
12391 keep_in_target = 1;
12392 }
12393 else
12394 {
12395 /* This location still exists, but it won't be kept in the
12396 target since it may have been disabled. We proceed to
12397 remove its target-side condition. */
12398
12399 /* The location is either no longer present, or got
12400 disabled. See if there's another location at the
12401 same address, in which case we don't need to remove
12402 this one from the target. */
12403
12404 /* OLD_LOC comes from existing struct breakpoint. */
12405 if (breakpoint_address_is_meaningful (old_loc->owner))
12406 {
12407 for (loc2p = locp;
12408 (loc2p < bp_location + bp_location_count
12409 && (*loc2p)->address == old_loc->address);
12410 loc2p++)
12411 {
12412 struct bp_location *loc2 = *loc2p;
12413
12414 if (breakpoint_locations_match (loc2, old_loc))
12415 {
12416 /* Read watchpoint locations are switched to
12417 access watchpoints, if the former are not
12418 supported, but the latter are. */
12419 if (is_hardware_watchpoint (old_loc->owner))
12420 {
12421 gdb_assert (is_hardware_watchpoint (loc2->owner));
12422 loc2->watchpoint_type = old_loc->watchpoint_type;
12423 }
12424
12425 /* loc2 is a duplicated location. We need to check
12426 if it should be inserted in case it will be
12427 unduplicated. */
12428 if (loc2 != old_loc
12429 && unduplicated_should_be_inserted (loc2))
12430 {
12431 swap_insertion (old_loc, loc2);
12432 keep_in_target = 1;
12433 break;
12434 }
12435 }
12436 }
12437 }
12438 }
12439
12440 if (!keep_in_target)
12441 {
12442 if (remove_breakpoint (old_loc, mark_uninserted))
12443 {
12444 /* This is just about all we can do. We could keep
12445 this location on the global list, and try to
12446 remove it next time, but there's no particular
12447 reason why we will succeed next time.
12448
12449 Note that at this point, old_loc->owner is still
12450 valid, as delete_breakpoint frees the breakpoint
12451 only after calling us. */
12452 printf_filtered (_("warning: Error removing "
12453 "breakpoint %d\n"),
12454 old_loc->owner->number);
12455 }
12456 removed = 1;
12457 }
12458 }
12459
12460 if (!found_object)
12461 {
12462 if (removed && non_stop
12463 && breakpoint_address_is_meaningful (old_loc->owner)
12464 && !is_hardware_watchpoint (old_loc->owner))
12465 {
12466 /* This location was removed from the target. In
12467 non-stop mode, a race condition is possible where
12468 we've removed a breakpoint, but stop events for that
12469 breakpoint are already queued and will arrive later.
12470 We apply an heuristic to be able to distinguish such
12471 SIGTRAPs from other random SIGTRAPs: we keep this
12472 breakpoint location for a bit, and will retire it
12473 after we see some number of events. The theory here
12474 is that reporting of events should, "on the average",
12475 be fair, so after a while we'll see events from all
12476 threads that have anything of interest, and no longer
12477 need to keep this breakpoint location around. We
12478 don't hold locations forever so to reduce chances of
12479 mistaking a non-breakpoint SIGTRAP for a breakpoint
12480 SIGTRAP.
12481
12482 The heuristic failing can be disastrous on
12483 decr_pc_after_break targets.
12484
12485 On decr_pc_after_break targets, like e.g., x86-linux,
12486 if we fail to recognize a late breakpoint SIGTRAP,
12487 because events_till_retirement has reached 0 too
12488 soon, we'll fail to do the PC adjustment, and report
12489 a random SIGTRAP to the user. When the user resumes
12490 the inferior, it will most likely immediately crash
12491 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12492 corrupted, because of being resumed e.g., in the
12493 middle of a multi-byte instruction, or skipped a
12494 one-byte instruction. This was actually seen happen
12495 on native x86-linux, and should be less rare on
12496 targets that do not support new thread events, like
12497 remote, due to the heuristic depending on
12498 thread_count.
12499
12500 Mistaking a random SIGTRAP for a breakpoint trap
12501 causes similar symptoms (PC adjustment applied when
12502 it shouldn't), but then again, playing with SIGTRAPs
12503 behind the debugger's back is asking for trouble.
12504
12505 Since hardware watchpoint traps are always
12506 distinguishable from other traps, so we don't need to
12507 apply keep hardware watchpoint moribund locations
12508 around. We simply always ignore hardware watchpoint
12509 traps we can no longer explain. */
12510
12511 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12512 old_loc->owner = NULL;
12513
12514 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12515 }
12516 else
12517 {
12518 old_loc->owner = NULL;
12519 decref_bp_location (&old_loc);
12520 }
12521 }
12522 }
12523
12524 /* Rescan breakpoints at the same address and section, marking the
12525 first one as "first" and any others as "duplicates". This is so
12526 that the bpt instruction is only inserted once. If we have a
12527 permanent breakpoint at the same place as BPT, make that one the
12528 official one, and the rest as duplicates. Permanent breakpoints
12529 are sorted first for the same address.
12530
12531 Do the same for hardware watchpoints, but also considering the
12532 watchpoint's type (regular/access/read) and length. */
12533
12534 bp_loc_first = NULL;
12535 wp_loc_first = NULL;
12536 awp_loc_first = NULL;
12537 rwp_loc_first = NULL;
12538 ALL_BP_LOCATIONS (loc, locp)
12539 {
12540 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12541 non-NULL. */
12542 struct bp_location **loc_first_p;
12543 b = loc->owner;
12544
12545 if (!unduplicated_should_be_inserted (loc)
12546 || !breakpoint_address_is_meaningful (b)
12547 /* Don't detect duplicate for tracepoint locations because they are
12548 never duplicated. See the comments in field `duplicate' of
12549 `struct bp_location'. */
12550 || is_tracepoint (b))
12551 {
12552 /* Clear the condition modification flag. */
12553 loc->condition_changed = condition_unchanged;
12554 continue;
12555 }
12556
12557 /* Permanent breakpoint should always be inserted. */
12558 if (b->enable_state == bp_permanent && ! loc->inserted)
12559 internal_error (__FILE__, __LINE__,
12560 _("allegedly permanent breakpoint is not "
12561 "actually inserted"));
12562
12563 if (b->type == bp_hardware_watchpoint)
12564 loc_first_p = &wp_loc_first;
12565 else if (b->type == bp_read_watchpoint)
12566 loc_first_p = &rwp_loc_first;
12567 else if (b->type == bp_access_watchpoint)
12568 loc_first_p = &awp_loc_first;
12569 else
12570 loc_first_p = &bp_loc_first;
12571
12572 if (*loc_first_p == NULL
12573 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12574 || !breakpoint_locations_match (loc, *loc_first_p))
12575 {
12576 *loc_first_p = loc;
12577 loc->duplicate = 0;
12578
12579 if (is_breakpoint (loc->owner) && loc->condition_changed)
12580 {
12581 loc->needs_update = 1;
12582 /* Clear the condition modification flag. */
12583 loc->condition_changed = condition_unchanged;
12584 }
12585 continue;
12586 }
12587
12588
12589 /* This and the above ensure the invariant that the first location
12590 is not duplicated, and is the inserted one.
12591 All following are marked as duplicated, and are not inserted. */
12592 if (loc->inserted)
12593 swap_insertion (loc, *loc_first_p);
12594 loc->duplicate = 1;
12595
12596 /* Clear the condition modification flag. */
12597 loc->condition_changed = condition_unchanged;
12598
12599 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12600 && b->enable_state != bp_permanent)
12601 internal_error (__FILE__, __LINE__,
12602 _("another breakpoint was inserted on top of "
12603 "a permanent breakpoint"));
12604 }
12605
12606 if (breakpoints_always_inserted_mode ()
12607 && (have_live_inferiors ()
12608 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
12609 {
12610 if (should_insert)
12611 insert_breakpoint_locations ();
12612 else
12613 {
12614 /* Though should_insert is false, we may need to update conditions
12615 on the target's side if it is evaluating such conditions. We
12616 only update conditions for locations that are marked
12617 "needs_update". */
12618 update_inserted_breakpoint_locations ();
12619 }
12620 }
12621
12622 if (should_insert)
12623 download_tracepoint_locations ();
12624
12625 do_cleanups (cleanups);
12626}
12627
12628void
12629breakpoint_retire_moribund (void)
12630{
12631 struct bp_location *loc;
12632 int ix;
12633
12634 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12635 if (--(loc->events_till_retirement) == 0)
12636 {
12637 decref_bp_location (&loc);
12638 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12639 --ix;
12640 }
12641}
12642
12643static void
12644update_global_location_list_nothrow (int inserting)
12645{
12646 volatile struct gdb_exception e;
12647
12648 TRY_CATCH (e, RETURN_MASK_ERROR)
12649 update_global_location_list (inserting);
12650}
12651
12652/* Clear BKP from a BPS. */
12653
12654static void
12655bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12656{
12657 bpstat bs;
12658
12659 for (bs = bps; bs; bs = bs->next)
12660 if (bs->breakpoint_at == bpt)
12661 {
12662 bs->breakpoint_at = NULL;
12663 bs->old_val = NULL;
12664 /* bs->commands will be freed later. */
12665 }
12666}
12667
12668/* Callback for iterate_over_threads. */
12669static int
12670bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12671{
12672 struct breakpoint *bpt = data;
12673
12674 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12675 return 0;
12676}
12677
12678/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12679 callbacks. */
12680
12681static void
12682say_where (struct breakpoint *b)
12683{
12684 struct value_print_options opts;
12685
12686 get_user_print_options (&opts);
12687
12688 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12689 single string. */
12690 if (b->loc == NULL)
12691 {
12692 printf_filtered (_(" (%s) pending."), b->addr_string);
12693 }
12694 else
12695 {
12696 if (opts.addressprint || b->loc->symtab == NULL)
12697 {
12698 printf_filtered (" at ");
12699 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12700 gdb_stdout);
12701 }
12702 if (b->loc->symtab != NULL)
12703 {
12704 /* If there is a single location, we can print the location
12705 more nicely. */
12706 if (b->loc->next == NULL)
12707 printf_filtered (": file %s, line %d.",
12708 symtab_to_filename_for_display (b->loc->symtab),
12709 b->loc->line_number);
12710 else
12711 /* This is not ideal, but each location may have a
12712 different file name, and this at least reflects the
12713 real situation somewhat. */
12714 printf_filtered (": %s.", b->addr_string);
12715 }
12716
12717 if (b->loc->next)
12718 {
12719 struct bp_location *loc = b->loc;
12720 int n = 0;
12721 for (; loc; loc = loc->next)
12722 ++n;
12723 printf_filtered (" (%d locations)", n);
12724 }
12725 }
12726}
12727
12728/* Default bp_location_ops methods. */
12729
12730static void
12731bp_location_dtor (struct bp_location *self)
12732{
12733 xfree (self->cond);
12734 if (self->cond_bytecode)
12735 free_agent_expr (self->cond_bytecode);
12736 xfree (self->function_name);
12737}
12738
12739static const struct bp_location_ops bp_location_ops =
12740{
12741 bp_location_dtor
12742};
12743
12744/* Default breakpoint_ops methods all breakpoint_ops ultimately
12745 inherit from. */
12746
12747static void
12748base_breakpoint_dtor (struct breakpoint *self)
12749{
12750 decref_counted_command_line (&self->commands);
12751 xfree (self->cond_string);
12752 xfree (self->extra_string);
12753 xfree (self->addr_string);
12754 xfree (self->filter);
12755 xfree (self->addr_string_range_end);
12756}
12757
12758static struct bp_location *
12759base_breakpoint_allocate_location (struct breakpoint *self)
12760{
12761 struct bp_location *loc;
12762
12763 loc = XNEW (struct bp_location);
12764 init_bp_location (loc, &bp_location_ops, self);
12765 return loc;
12766}
12767
12768static void
12769base_breakpoint_re_set (struct breakpoint *b)
12770{
12771 /* Nothing to re-set. */
12772}
12773
12774#define internal_error_pure_virtual_called() \
12775 gdb_assert_not_reached ("pure virtual function called")
12776
12777static int
12778base_breakpoint_insert_location (struct bp_location *bl)
12779{
12780 internal_error_pure_virtual_called ();
12781}
12782
12783static int
12784base_breakpoint_remove_location (struct bp_location *bl)
12785{
12786 internal_error_pure_virtual_called ();
12787}
12788
12789static int
12790base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12791 struct address_space *aspace,
12792 CORE_ADDR bp_addr,
12793 const struct target_waitstatus *ws)
12794{
12795 internal_error_pure_virtual_called ();
12796}
12797
12798static void
12799base_breakpoint_check_status (bpstat bs)
12800{
12801 /* Always stop. */
12802}
12803
12804/* A "works_in_software_mode" breakpoint_ops method that just internal
12805 errors. */
12806
12807static int
12808base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12809{
12810 internal_error_pure_virtual_called ();
12811}
12812
12813/* A "resources_needed" breakpoint_ops method that just internal
12814 errors. */
12815
12816static int
12817base_breakpoint_resources_needed (const struct bp_location *bl)
12818{
12819 internal_error_pure_virtual_called ();
12820}
12821
12822static enum print_stop_action
12823base_breakpoint_print_it (bpstat bs)
12824{
12825 internal_error_pure_virtual_called ();
12826}
12827
12828static void
12829base_breakpoint_print_one_detail (const struct breakpoint *self,
12830 struct ui_out *uiout)
12831{
12832 /* nothing */
12833}
12834
12835static void
12836base_breakpoint_print_mention (struct breakpoint *b)
12837{
12838 internal_error_pure_virtual_called ();
12839}
12840
12841static void
12842base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12843{
12844 internal_error_pure_virtual_called ();
12845}
12846
12847static void
12848base_breakpoint_create_sals_from_address (char **arg,
12849 struct linespec_result *canonical,
12850 enum bptype type_wanted,
12851 char *addr_start,
12852 char **copy_arg)
12853{
12854 internal_error_pure_virtual_called ();
12855}
12856
12857static void
12858base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12859 struct linespec_result *c,
12860 char *cond_string,
12861 char *extra_string,
12862 enum bptype type_wanted,
12863 enum bpdisp disposition,
12864 int thread,
12865 int task, int ignore_count,
12866 const struct breakpoint_ops *o,
12867 int from_tty, int enabled,
12868 int internal, unsigned flags)
12869{
12870 internal_error_pure_virtual_called ();
12871}
12872
12873static void
12874base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12875 struct symtabs_and_lines *sals)
12876{
12877 internal_error_pure_virtual_called ();
12878}
12879
12880/* The default 'explains_signal' method. */
12881
12882static int
12883base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12884{
12885 return 1;
12886}
12887
12888/* The default "after_condition_true" method. */
12889
12890static void
12891base_breakpoint_after_condition_true (struct bpstats *bs)
12892{
12893 /* Nothing to do. */
12894}
12895
12896struct breakpoint_ops base_breakpoint_ops =
12897{
12898 base_breakpoint_dtor,
12899 base_breakpoint_allocate_location,
12900 base_breakpoint_re_set,
12901 base_breakpoint_insert_location,
12902 base_breakpoint_remove_location,
12903 base_breakpoint_breakpoint_hit,
12904 base_breakpoint_check_status,
12905 base_breakpoint_resources_needed,
12906 base_breakpoint_works_in_software_mode,
12907 base_breakpoint_print_it,
12908 NULL,
12909 base_breakpoint_print_one_detail,
12910 base_breakpoint_print_mention,
12911 base_breakpoint_print_recreate,
12912 base_breakpoint_create_sals_from_address,
12913 base_breakpoint_create_breakpoints_sal,
12914 base_breakpoint_decode_linespec,
12915 base_breakpoint_explains_signal,
12916 base_breakpoint_after_condition_true,
12917};
12918
12919/* Default breakpoint_ops methods. */
12920
12921static void
12922bkpt_re_set (struct breakpoint *b)
12923{
12924 /* FIXME: is this still reachable? */
12925 if (b->addr_string == NULL)
12926 {
12927 /* Anything without a string can't be re-set. */
12928 delete_breakpoint (b);
12929 return;
12930 }
12931
12932 breakpoint_re_set_default (b);
12933}
12934
12935static int
12936bkpt_insert_location (struct bp_location *bl)
12937{
12938 if (bl->loc_type == bp_loc_hardware_breakpoint)
12939 return target_insert_hw_breakpoint (bl->gdbarch,
12940 &bl->target_info);
12941 else
12942 return target_insert_breakpoint (bl->gdbarch,
12943 &bl->target_info);
12944}
12945
12946static int
12947bkpt_remove_location (struct bp_location *bl)
12948{
12949 if (bl->loc_type == bp_loc_hardware_breakpoint)
12950 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12951 else
12952 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12953}
12954
12955static int
12956bkpt_breakpoint_hit (const struct bp_location *bl,
12957 struct address_space *aspace, CORE_ADDR bp_addr,
12958 const struct target_waitstatus *ws)
12959{
12960 if (ws->kind != TARGET_WAITKIND_STOPPED
12961 || ws->value.sig != GDB_SIGNAL_TRAP)
12962 return 0;
12963
12964 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12965 aspace, bp_addr))
12966 return 0;
12967
12968 if (overlay_debugging /* unmapped overlay section */
12969 && section_is_overlay (bl->section)
12970 && !section_is_mapped (bl->section))
12971 return 0;
12972
12973 return 1;
12974}
12975
12976static int
12977bkpt_resources_needed (const struct bp_location *bl)
12978{
12979 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12980
12981 return 1;
12982}
12983
12984static enum print_stop_action
12985bkpt_print_it (bpstat bs)
12986{
12987 struct breakpoint *b;
12988 const struct bp_location *bl;
12989 int bp_temp;
12990 struct ui_out *uiout = current_uiout;
12991
12992 gdb_assert (bs->bp_location_at != NULL);
12993
12994 bl = bs->bp_location_at;
12995 b = bs->breakpoint_at;
12996
12997 bp_temp = b->disposition == disp_del;
12998 if (bl->address != bl->requested_address)
12999 breakpoint_adjustment_warning (bl->requested_address,
13000 bl->address,
13001 b->number, 1);
13002 annotate_breakpoint (b->number);
13003 if (bp_temp)
13004 ui_out_text (uiout, "\nTemporary breakpoint ");
13005 else
13006 ui_out_text (uiout, "\nBreakpoint ");
13007 if (ui_out_is_mi_like_p (uiout))
13008 {
13009 ui_out_field_string (uiout, "reason",
13010 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13011 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13012 }
13013 ui_out_field_int (uiout, "bkptno", b->number);
13014 ui_out_text (uiout, ", ");
13015
13016 return PRINT_SRC_AND_LOC;
13017}
13018
13019static void
13020bkpt_print_mention (struct breakpoint *b)
13021{
13022 if (ui_out_is_mi_like_p (current_uiout))
13023 return;
13024
13025 switch (b->type)
13026 {
13027 case bp_breakpoint:
13028 case bp_gnu_ifunc_resolver:
13029 if (b->disposition == disp_del)
13030 printf_filtered (_("Temporary breakpoint"));
13031 else
13032 printf_filtered (_("Breakpoint"));
13033 printf_filtered (_(" %d"), b->number);
13034 if (b->type == bp_gnu_ifunc_resolver)
13035 printf_filtered (_(" at gnu-indirect-function resolver"));
13036 break;
13037 case bp_hardware_breakpoint:
13038 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13039 break;
13040 case bp_dprintf:
13041 printf_filtered (_("Dprintf %d"), b->number);
13042 break;
13043 }
13044
13045 say_where (b);
13046}
13047
13048static void
13049bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13050{
13051 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13052 fprintf_unfiltered (fp, "tbreak");
13053 else if (tp->type == bp_breakpoint)
13054 fprintf_unfiltered (fp, "break");
13055 else if (tp->type == bp_hardware_breakpoint
13056 && tp->disposition == disp_del)
13057 fprintf_unfiltered (fp, "thbreak");
13058 else if (tp->type == bp_hardware_breakpoint)
13059 fprintf_unfiltered (fp, "hbreak");
13060 else
13061 internal_error (__FILE__, __LINE__,
13062 _("unhandled breakpoint type %d"), (int) tp->type);
13063
13064 fprintf_unfiltered (fp, " %s", tp->addr_string);
13065 print_recreate_thread (tp, fp);
13066}
13067
13068static void
13069bkpt_create_sals_from_address (char **arg,
13070 struct linespec_result *canonical,
13071 enum bptype type_wanted,
13072 char *addr_start, char **copy_arg)
13073{
13074 create_sals_from_address_default (arg, canonical, type_wanted,
13075 addr_start, copy_arg);
13076}
13077
13078static void
13079bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13080 struct linespec_result *canonical,
13081 char *cond_string,
13082 char *extra_string,
13083 enum bptype type_wanted,
13084 enum bpdisp disposition,
13085 int thread,
13086 int task, int ignore_count,
13087 const struct breakpoint_ops *ops,
13088 int from_tty, int enabled,
13089 int internal, unsigned flags)
13090{
13091 create_breakpoints_sal_default (gdbarch, canonical,
13092 cond_string, extra_string,
13093 type_wanted,
13094 disposition, thread, task,
13095 ignore_count, ops, from_tty,
13096 enabled, internal, flags);
13097}
13098
13099static void
13100bkpt_decode_linespec (struct breakpoint *b, char **s,
13101 struct symtabs_and_lines *sals)
13102{
13103 decode_linespec_default (b, s, sals);
13104}
13105
13106/* Virtual table for internal breakpoints. */
13107
13108static void
13109internal_bkpt_re_set (struct breakpoint *b)
13110{
13111 switch (b->type)
13112 {
13113 /* Delete overlay event and longjmp master breakpoints; they
13114 will be reset later by breakpoint_re_set. */
13115 case bp_overlay_event:
13116 case bp_longjmp_master:
13117 case bp_std_terminate_master:
13118 case bp_exception_master:
13119 delete_breakpoint (b);
13120 break;
13121
13122 /* This breakpoint is special, it's set up when the inferior
13123 starts and we really don't want to touch it. */
13124 case bp_shlib_event:
13125
13126 /* Like bp_shlib_event, this breakpoint type is special. Once
13127 it is set up, we do not want to touch it. */
13128 case bp_thread_event:
13129 break;
13130 }
13131}
13132
13133static void
13134internal_bkpt_check_status (bpstat bs)
13135{
13136 if (bs->breakpoint_at->type == bp_shlib_event)
13137 {
13138 /* If requested, stop when the dynamic linker notifies GDB of
13139 events. This allows the user to get control and place
13140 breakpoints in initializer routines for dynamically loaded
13141 objects (among other things). */
13142 bs->stop = stop_on_solib_events;
13143 bs->print = stop_on_solib_events;
13144 }
13145 else
13146 bs->stop = 0;
13147}
13148
13149static enum print_stop_action
13150internal_bkpt_print_it (bpstat bs)
13151{
13152 struct breakpoint *b;
13153
13154 b = bs->breakpoint_at;
13155
13156 switch (b->type)
13157 {
13158 case bp_shlib_event:
13159 /* Did we stop because the user set the stop_on_solib_events
13160 variable? (If so, we report this as a generic, "Stopped due
13161 to shlib event" message.) */
13162 print_solib_event (0);
13163 break;
13164
13165 case bp_thread_event:
13166 /* Not sure how we will get here.
13167 GDB should not stop for these breakpoints. */
13168 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13169 break;
13170
13171 case bp_overlay_event:
13172 /* By analogy with the thread event, GDB should not stop for these. */
13173 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13174 break;
13175
13176 case bp_longjmp_master:
13177 /* These should never be enabled. */
13178 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13179 break;
13180
13181 case bp_std_terminate_master:
13182 /* These should never be enabled. */
13183 printf_filtered (_("std::terminate Master Breakpoint: "
13184 "gdb should not stop!\n"));
13185 break;
13186
13187 case bp_exception_master:
13188 /* These should never be enabled. */
13189 printf_filtered (_("Exception Master Breakpoint: "
13190 "gdb should not stop!\n"));
13191 break;
13192 }
13193
13194 return PRINT_NOTHING;
13195}
13196
13197static void
13198internal_bkpt_print_mention (struct breakpoint *b)
13199{
13200 /* Nothing to mention. These breakpoints are internal. */
13201}
13202
13203/* Virtual table for momentary breakpoints */
13204
13205static void
13206momentary_bkpt_re_set (struct breakpoint *b)
13207{
13208 /* Keep temporary breakpoints, which can be encountered when we step
13209 over a dlopen call and solib_add is resetting the breakpoints.
13210 Otherwise these should have been blown away via the cleanup chain
13211 or by breakpoint_init_inferior when we rerun the executable. */
13212}
13213
13214static void
13215momentary_bkpt_check_status (bpstat bs)
13216{
13217 /* Nothing. The point of these breakpoints is causing a stop. */
13218}
13219
13220static enum print_stop_action
13221momentary_bkpt_print_it (bpstat bs)
13222{
13223 struct ui_out *uiout = current_uiout;
13224
13225 if (ui_out_is_mi_like_p (uiout))
13226 {
13227 struct breakpoint *b = bs->breakpoint_at;
13228
13229 switch (b->type)
13230 {
13231 case bp_finish:
13232 ui_out_field_string
13233 (uiout, "reason",
13234 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13235 break;
13236
13237 case bp_until:
13238 ui_out_field_string
13239 (uiout, "reason",
13240 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13241 break;
13242 }
13243 }
13244
13245 return PRINT_UNKNOWN;
13246}
13247
13248static void
13249momentary_bkpt_print_mention (struct breakpoint *b)
13250{
13251 /* Nothing to mention. These breakpoints are internal. */
13252}
13253
13254/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13255
13256 It gets cleared already on the removal of the first one of such placed
13257 breakpoints. This is OK as they get all removed altogether. */
13258
13259static void
13260longjmp_bkpt_dtor (struct breakpoint *self)
13261{
13262 struct thread_info *tp = find_thread_id (self->thread);
13263
13264 if (tp)
13265 tp->initiating_frame = null_frame_id;
13266
13267 momentary_breakpoint_ops.dtor (self);
13268}
13269
13270/* Specific methods for probe breakpoints. */
13271
13272static int
13273bkpt_probe_insert_location (struct bp_location *bl)
13274{
13275 int v = bkpt_insert_location (bl);
13276
13277 if (v == 0)
13278 {
13279 /* The insertion was successful, now let's set the probe's semaphore
13280 if needed. */
13281 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
13282 }
13283
13284 return v;
13285}
13286
13287static int
13288bkpt_probe_remove_location (struct bp_location *bl)
13289{
13290 /* Let's clear the semaphore before removing the location. */
13291 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
13292
13293 return bkpt_remove_location (bl);
13294}
13295
13296static void
13297bkpt_probe_create_sals_from_address (char **arg,
13298 struct linespec_result *canonical,
13299 enum bptype type_wanted,
13300 char *addr_start, char **copy_arg)
13301{
13302 struct linespec_sals lsal;
13303
13304 lsal.sals = parse_probes (arg, canonical);
13305
13306 *copy_arg = xstrdup (canonical->addr_string);
13307 lsal.canonical = xstrdup (*copy_arg);
13308
13309 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13310}
13311
13312static void
13313bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13314 struct symtabs_and_lines *sals)
13315{
13316 *sals = parse_probes (s, NULL);
13317 if (!sals->sals)
13318 error (_("probe not found"));
13319}
13320
13321/* The breakpoint_ops structure to be used in tracepoints. */
13322
13323static void
13324tracepoint_re_set (struct breakpoint *b)
13325{
13326 breakpoint_re_set_default (b);
13327}
13328
13329static int
13330tracepoint_breakpoint_hit (const struct bp_location *bl,
13331 struct address_space *aspace, CORE_ADDR bp_addr,
13332 const struct target_waitstatus *ws)
13333{
13334 /* By definition, the inferior does not report stops at
13335 tracepoints. */
13336 return 0;
13337}
13338
13339static void
13340tracepoint_print_one_detail (const struct breakpoint *self,
13341 struct ui_out *uiout)
13342{
13343 struct tracepoint *tp = (struct tracepoint *) self;
13344 if (tp->static_trace_marker_id)
13345 {
13346 gdb_assert (self->type == bp_static_tracepoint);
13347
13348 ui_out_text (uiout, "\tmarker id is ");
13349 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13350 tp->static_trace_marker_id);
13351 ui_out_text (uiout, "\n");
13352 }
13353}
13354
13355static void
13356tracepoint_print_mention (struct breakpoint *b)
13357{
13358 if (ui_out_is_mi_like_p (current_uiout))
13359 return;
13360
13361 switch (b->type)
13362 {
13363 case bp_tracepoint:
13364 printf_filtered (_("Tracepoint"));
13365 printf_filtered (_(" %d"), b->number);
13366 break;
13367 case bp_fast_tracepoint:
13368 printf_filtered (_("Fast tracepoint"));
13369 printf_filtered (_(" %d"), b->number);
13370 break;
13371 case bp_static_tracepoint:
13372 printf_filtered (_("Static tracepoint"));
13373 printf_filtered (_(" %d"), b->number);
13374 break;
13375 default:
13376 internal_error (__FILE__, __LINE__,
13377 _("unhandled tracepoint type %d"), (int) b->type);
13378 }
13379
13380 say_where (b);
13381}
13382
13383static void
13384tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13385{
13386 struct tracepoint *tp = (struct tracepoint *) self;
13387
13388 if (self->type == bp_fast_tracepoint)
13389 fprintf_unfiltered (fp, "ftrace");
13390 if (self->type == bp_static_tracepoint)
13391 fprintf_unfiltered (fp, "strace");
13392 else if (self->type == bp_tracepoint)
13393 fprintf_unfiltered (fp, "trace");
13394 else
13395 internal_error (__FILE__, __LINE__,
13396 _("unhandled tracepoint type %d"), (int) self->type);
13397
13398 fprintf_unfiltered (fp, " %s", self->addr_string);
13399 print_recreate_thread (self, fp);
13400
13401 if (tp->pass_count)
13402 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13403}
13404
13405static void
13406tracepoint_create_sals_from_address (char **arg,
13407 struct linespec_result *canonical,
13408 enum bptype type_wanted,
13409 char *addr_start, char **copy_arg)
13410{
13411 create_sals_from_address_default (arg, canonical, type_wanted,
13412 addr_start, copy_arg);
13413}
13414
13415static void
13416tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13417 struct linespec_result *canonical,
13418 char *cond_string,
13419 char *extra_string,
13420 enum bptype type_wanted,
13421 enum bpdisp disposition,
13422 int thread,
13423 int task, int ignore_count,
13424 const struct breakpoint_ops *ops,
13425 int from_tty, int enabled,
13426 int internal, unsigned flags)
13427{
13428 create_breakpoints_sal_default (gdbarch, canonical,
13429 cond_string, extra_string,
13430 type_wanted,
13431 disposition, thread, task,
13432 ignore_count, ops, from_tty,
13433 enabled, internal, flags);
13434}
13435
13436static void
13437tracepoint_decode_linespec (struct breakpoint *b, char **s,
13438 struct symtabs_and_lines *sals)
13439{
13440 decode_linespec_default (b, s, sals);
13441}
13442
13443struct breakpoint_ops tracepoint_breakpoint_ops;
13444
13445/* The breakpoint_ops structure to be use on tracepoints placed in a
13446 static probe. */
13447
13448static void
13449tracepoint_probe_create_sals_from_address (char **arg,
13450 struct linespec_result *canonical,
13451 enum bptype type_wanted,
13452 char *addr_start, char **copy_arg)
13453{
13454 /* We use the same method for breakpoint on probes. */
13455 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13456 addr_start, copy_arg);
13457}
13458
13459static void
13460tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13461 struct symtabs_and_lines *sals)
13462{
13463 /* We use the same method for breakpoint on probes. */
13464 bkpt_probe_decode_linespec (b, s, sals);
13465}
13466
13467static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13468
13469/* Dprintf breakpoint_ops methods. */
13470
13471static void
13472dprintf_re_set (struct breakpoint *b)
13473{
13474 breakpoint_re_set_default (b);
13475
13476 /* This breakpoint could have been pending, and be resolved now, and
13477 if so, we should now have the extra string. If we don't, the
13478 dprintf was malformed when created, but we couldn't tell because
13479 we can't extract the extra string until the location is
13480 resolved. */
13481 if (b->loc != NULL && b->extra_string == NULL)
13482 error (_("Format string required"));
13483
13484 /* 1 - connect to target 1, that can run breakpoint commands.
13485 2 - create a dprintf, which resolves fine.
13486 3 - disconnect from target 1
13487 4 - connect to target 2, that can NOT run breakpoint commands.
13488
13489 After steps #3/#4, you'll want the dprintf command list to
13490 be updated, because target 1 and 2 may well return different
13491 answers for target_can_run_breakpoint_commands().
13492 Given absence of finer grained resetting, we get to do
13493 it all the time. */
13494 if (b->extra_string != NULL)
13495 update_dprintf_command_list (b);
13496}
13497
13498/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13499
13500static void
13501dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13502{
13503 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13504 tp->extra_string);
13505 print_recreate_thread (tp, fp);
13506}
13507
13508/* Implement the "after_condition_true" breakpoint_ops method for
13509 dprintf.
13510
13511 dprintf's are implemented with regular commands in their command
13512 list, but we run the commands here instead of before presenting the
13513 stop to the user, as dprintf's don't actually cause a stop. This
13514 also makes it so that the commands of multiple dprintfs at the same
13515 address are all handled. */
13516
13517static void
13518dprintf_after_condition_true (struct bpstats *bs)
13519{
13520 struct cleanup *old_chain;
13521 struct bpstats tmp_bs = { NULL };
13522 struct bpstats *tmp_bs_p = &tmp_bs;
13523
13524 /* dprintf's never cause a stop. This wasn't set in the
13525 check_status hook instead because that would make the dprintf's
13526 condition not be evaluated. */
13527 bs->stop = 0;
13528
13529 /* Run the command list here. Take ownership of it instead of
13530 copying. We never want these commands to run later in
13531 bpstat_do_actions, if a breakpoint that causes a stop happens to
13532 be set at same address as this dprintf, or even if running the
13533 commands here throws. */
13534 tmp_bs.commands = bs->commands;
13535 bs->commands = NULL;
13536 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13537
13538 bpstat_do_actions_1 (&tmp_bs_p);
13539
13540 /* 'tmp_bs.commands' will usually be NULL by now, but
13541 bpstat_do_actions_1 may return early without processing the whole
13542 list. */
13543 do_cleanups (old_chain);
13544}
13545
13546/* The breakpoint_ops structure to be used on static tracepoints with
13547 markers (`-m'). */
13548
13549static void
13550strace_marker_create_sals_from_address (char **arg,
13551 struct linespec_result *canonical,
13552 enum bptype type_wanted,
13553 char *addr_start, char **copy_arg)
13554{
13555 struct linespec_sals lsal;
13556
13557 lsal.sals = decode_static_tracepoint_spec (arg);
13558
13559 *copy_arg = savestring (addr_start, *arg - addr_start);
13560
13561 canonical->addr_string = xstrdup (*copy_arg);
13562 lsal.canonical = xstrdup (*copy_arg);
13563 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13564}
13565
13566static void
13567strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13568 struct linespec_result *canonical,
13569 char *cond_string,
13570 char *extra_string,
13571 enum bptype type_wanted,
13572 enum bpdisp disposition,
13573 int thread,
13574 int task, int ignore_count,
13575 const struct breakpoint_ops *ops,
13576 int from_tty, int enabled,
13577 int internal, unsigned flags)
13578{
13579 int i;
13580 struct linespec_sals *lsal = VEC_index (linespec_sals,
13581 canonical->sals, 0);
13582
13583 /* If the user is creating a static tracepoint by marker id
13584 (strace -m MARKER_ID), then store the sals index, so that
13585 breakpoint_re_set can try to match up which of the newly
13586 found markers corresponds to this one, and, don't try to
13587 expand multiple locations for each sal, given than SALS
13588 already should contain all sals for MARKER_ID. */
13589
13590 for (i = 0; i < lsal->sals.nelts; ++i)
13591 {
13592 struct symtabs_and_lines expanded;
13593 struct tracepoint *tp;
13594 struct cleanup *old_chain;
13595 char *addr_string;
13596
13597 expanded.nelts = 1;
13598 expanded.sals = &lsal->sals.sals[i];
13599
13600 addr_string = xstrdup (canonical->addr_string);
13601 old_chain = make_cleanup (xfree, addr_string);
13602
13603 tp = XCNEW (struct tracepoint);
13604 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13605 addr_string, NULL,
13606 cond_string, extra_string,
13607 type_wanted, disposition,
13608 thread, task, ignore_count, ops,
13609 from_tty, enabled, internal, flags,
13610 canonical->special_display);
13611 /* Given that its possible to have multiple markers with
13612 the same string id, if the user is creating a static
13613 tracepoint by marker id ("strace -m MARKER_ID"), then
13614 store the sals index, so that breakpoint_re_set can
13615 try to match up which of the newly found markers
13616 corresponds to this one */
13617 tp->static_trace_marker_id_idx = i;
13618
13619 install_breakpoint (internal, &tp->base, 0);
13620
13621 discard_cleanups (old_chain);
13622 }
13623}
13624
13625static void
13626strace_marker_decode_linespec (struct breakpoint *b, char **s,
13627 struct symtabs_and_lines *sals)
13628{
13629 struct tracepoint *tp = (struct tracepoint *) b;
13630
13631 *sals = decode_static_tracepoint_spec (s);
13632 if (sals->nelts > tp->static_trace_marker_id_idx)
13633 {
13634 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13635 sals->nelts = 1;
13636 }
13637 else
13638 error (_("marker %s not found"), tp->static_trace_marker_id);
13639}
13640
13641static struct breakpoint_ops strace_marker_breakpoint_ops;
13642
13643static int
13644strace_marker_p (struct breakpoint *b)
13645{
13646 return b->ops == &strace_marker_breakpoint_ops;
13647}
13648
13649/* Delete a breakpoint and clean up all traces of it in the data
13650 structures. */
13651
13652void
13653delete_breakpoint (struct breakpoint *bpt)
13654{
13655 struct breakpoint *b;
13656
13657 gdb_assert (bpt != NULL);
13658
13659 /* Has this bp already been deleted? This can happen because
13660 multiple lists can hold pointers to bp's. bpstat lists are
13661 especial culprits.
13662
13663 One example of this happening is a watchpoint's scope bp. When
13664 the scope bp triggers, we notice that the watchpoint is out of
13665 scope, and delete it. We also delete its scope bp. But the
13666 scope bp is marked "auto-deleting", and is already on a bpstat.
13667 That bpstat is then checked for auto-deleting bp's, which are
13668 deleted.
13669
13670 A real solution to this problem might involve reference counts in
13671 bp's, and/or giving them pointers back to their referencing
13672 bpstat's, and teaching delete_breakpoint to only free a bp's
13673 storage when no more references were extent. A cheaper bandaid
13674 was chosen. */
13675 if (bpt->type == bp_none)
13676 return;
13677
13678 /* At least avoid this stale reference until the reference counting
13679 of breakpoints gets resolved. */
13680 if (bpt->related_breakpoint != bpt)
13681 {
13682 struct breakpoint *related;
13683 struct watchpoint *w;
13684
13685 if (bpt->type == bp_watchpoint_scope)
13686 w = (struct watchpoint *) bpt->related_breakpoint;
13687 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13688 w = (struct watchpoint *) bpt;
13689 else
13690 w = NULL;
13691 if (w != NULL)
13692 watchpoint_del_at_next_stop (w);
13693
13694 /* Unlink bpt from the bpt->related_breakpoint ring. */
13695 for (related = bpt; related->related_breakpoint != bpt;
13696 related = related->related_breakpoint);
13697 related->related_breakpoint = bpt->related_breakpoint;
13698 bpt->related_breakpoint = bpt;
13699 }
13700
13701 /* watch_command_1 creates a watchpoint but only sets its number if
13702 update_watchpoint succeeds in creating its bp_locations. If there's
13703 a problem in that process, we'll be asked to delete the half-created
13704 watchpoint. In that case, don't announce the deletion. */
13705 if (bpt->number)
13706 observer_notify_breakpoint_deleted (bpt);
13707
13708 if (breakpoint_chain == bpt)
13709 breakpoint_chain = bpt->next;
13710
13711 ALL_BREAKPOINTS (b)
13712 if (b->next == bpt)
13713 {
13714 b->next = bpt->next;
13715 break;
13716 }
13717
13718 /* Be sure no bpstat's are pointing at the breakpoint after it's
13719 been freed. */
13720 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13721 in all threads for now. Note that we cannot just remove bpstats
13722 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13723 commands are associated with the bpstat; if we remove it here,
13724 then the later call to bpstat_do_actions (&stop_bpstat); in
13725 event-top.c won't do anything, and temporary breakpoints with
13726 commands won't work. */
13727
13728 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13729
13730 /* Now that breakpoint is removed from breakpoint list, update the
13731 global location list. This will remove locations that used to
13732 belong to this breakpoint. Do this before freeing the breakpoint
13733 itself, since remove_breakpoint looks at location's owner. It
13734 might be better design to have location completely
13735 self-contained, but it's not the case now. */
13736 update_global_location_list (0);
13737
13738 bpt->ops->dtor (bpt);
13739 /* On the chance that someone will soon try again to delete this
13740 same bp, we mark it as deleted before freeing its storage. */
13741 bpt->type = bp_none;
13742 xfree (bpt);
13743}
13744
13745static void
13746do_delete_breakpoint_cleanup (void *b)
13747{
13748 delete_breakpoint (b);
13749}
13750
13751struct cleanup *
13752make_cleanup_delete_breakpoint (struct breakpoint *b)
13753{
13754 return make_cleanup (do_delete_breakpoint_cleanup, b);
13755}
13756
13757/* Iterator function to call a user-provided callback function once
13758 for each of B and its related breakpoints. */
13759
13760static void
13761iterate_over_related_breakpoints (struct breakpoint *b,
13762 void (*function) (struct breakpoint *,
13763 void *),
13764 void *data)
13765{
13766 struct breakpoint *related;
13767
13768 related = b;
13769 do
13770 {
13771 struct breakpoint *next;
13772
13773 /* FUNCTION may delete RELATED. */
13774 next = related->related_breakpoint;
13775
13776 if (next == related)
13777 {
13778 /* RELATED is the last ring entry. */
13779 function (related, data);
13780
13781 /* FUNCTION may have deleted it, so we'd never reach back to
13782 B. There's nothing left to do anyway, so just break
13783 out. */
13784 break;
13785 }
13786 else
13787 function (related, data);
13788
13789 related = next;
13790 }
13791 while (related != b);
13792}
13793
13794static void
13795do_delete_breakpoint (struct breakpoint *b, void *ignore)
13796{
13797 delete_breakpoint (b);
13798}
13799
13800/* A callback for map_breakpoint_numbers that calls
13801 delete_breakpoint. */
13802
13803static void
13804do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13805{
13806 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13807}
13808
13809void
13810delete_command (char *arg, int from_tty)
13811{
13812 struct breakpoint *b, *b_tmp;
13813
13814 dont_repeat ();
13815
13816 if (arg == 0)
13817 {
13818 int breaks_to_delete = 0;
13819
13820 /* Delete all breakpoints if no argument. Do not delete
13821 internal breakpoints, these have to be deleted with an
13822 explicit breakpoint number argument. */
13823 ALL_BREAKPOINTS (b)
13824 if (user_breakpoint_p (b))
13825 {
13826 breaks_to_delete = 1;
13827 break;
13828 }
13829
13830 /* Ask user only if there are some breakpoints to delete. */
13831 if (!from_tty
13832 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13833 {
13834 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13835 if (user_breakpoint_p (b))
13836 delete_breakpoint (b);
13837 }
13838 }
13839 else
13840 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13841}
13842
13843static int
13844all_locations_are_pending (struct bp_location *loc)
13845{
13846 for (; loc; loc = loc->next)
13847 if (!loc->shlib_disabled
13848 && !loc->pspace->executing_startup)
13849 return 0;
13850 return 1;
13851}
13852
13853/* Subroutine of update_breakpoint_locations to simplify it.
13854 Return non-zero if multiple fns in list LOC have the same name.
13855 Null names are ignored. */
13856
13857static int
13858ambiguous_names_p (struct bp_location *loc)
13859{
13860 struct bp_location *l;
13861 htab_t htab = htab_create_alloc (13, htab_hash_string,
13862 (int (*) (const void *,
13863 const void *)) streq,
13864 NULL, xcalloc, xfree);
13865
13866 for (l = loc; l != NULL; l = l->next)
13867 {
13868 const char **slot;
13869 const char *name = l->function_name;
13870
13871 /* Allow for some names to be NULL, ignore them. */
13872 if (name == NULL)
13873 continue;
13874
13875 slot = (const char **) htab_find_slot (htab, (const void *) name,
13876 INSERT);
13877 /* NOTE: We can assume slot != NULL here because xcalloc never
13878 returns NULL. */
13879 if (*slot != NULL)
13880 {
13881 htab_delete (htab);
13882 return 1;
13883 }
13884 *slot = name;
13885 }
13886
13887 htab_delete (htab);
13888 return 0;
13889}
13890
13891/* When symbols change, it probably means the sources changed as well,
13892 and it might mean the static tracepoint markers are no longer at
13893 the same address or line numbers they used to be at last we
13894 checked. Losing your static tracepoints whenever you rebuild is
13895 undesirable. This function tries to resync/rematch gdb static
13896 tracepoints with the markers on the target, for static tracepoints
13897 that have not been set by marker id. Static tracepoint that have
13898 been set by marker id are reset by marker id in breakpoint_re_set.
13899 The heuristic is:
13900
13901 1) For a tracepoint set at a specific address, look for a marker at
13902 the old PC. If one is found there, assume to be the same marker.
13903 If the name / string id of the marker found is different from the
13904 previous known name, assume that means the user renamed the marker
13905 in the sources, and output a warning.
13906
13907 2) For a tracepoint set at a given line number, look for a marker
13908 at the new address of the old line number. If one is found there,
13909 assume to be the same marker. If the name / string id of the
13910 marker found is different from the previous known name, assume that
13911 means the user renamed the marker in the sources, and output a
13912 warning.
13913
13914 3) If a marker is no longer found at the same address or line, it
13915 may mean the marker no longer exists. But it may also just mean
13916 the code changed a bit. Maybe the user added a few lines of code
13917 that made the marker move up or down (in line number terms). Ask
13918 the target for info about the marker with the string id as we knew
13919 it. If found, update line number and address in the matching
13920 static tracepoint. This will get confused if there's more than one
13921 marker with the same ID (possible in UST, although unadvised
13922 precisely because it confuses tools). */
13923
13924static struct symtab_and_line
13925update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13926{
13927 struct tracepoint *tp = (struct tracepoint *) b;
13928 struct static_tracepoint_marker marker;
13929 CORE_ADDR pc;
13930
13931 pc = sal.pc;
13932 if (sal.line)
13933 find_line_pc (sal.symtab, sal.line, &pc);
13934
13935 if (target_static_tracepoint_marker_at (pc, &marker))
13936 {
13937 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13938 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13939 b->number,
13940 tp->static_trace_marker_id, marker.str_id);
13941
13942 xfree (tp->static_trace_marker_id);
13943 tp->static_trace_marker_id = xstrdup (marker.str_id);
13944 release_static_tracepoint_marker (&marker);
13945
13946 return sal;
13947 }
13948
13949 /* Old marker wasn't found on target at lineno. Try looking it up
13950 by string ID. */
13951 if (!sal.explicit_pc
13952 && sal.line != 0
13953 && sal.symtab != NULL
13954 && tp->static_trace_marker_id != NULL)
13955 {
13956 VEC(static_tracepoint_marker_p) *markers;
13957
13958 markers
13959 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13960
13961 if (!VEC_empty(static_tracepoint_marker_p, markers))
13962 {
13963 struct symtab_and_line sal2;
13964 struct symbol *sym;
13965 struct static_tracepoint_marker *tpmarker;
13966 struct ui_out *uiout = current_uiout;
13967
13968 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13969
13970 xfree (tp->static_trace_marker_id);
13971 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13972
13973 warning (_("marker for static tracepoint %d (%s) not "
13974 "found at previous line number"),
13975 b->number, tp->static_trace_marker_id);
13976
13977 init_sal (&sal2);
13978
13979 sal2.pc = tpmarker->address;
13980
13981 sal2 = find_pc_line (tpmarker->address, 0);
13982 sym = find_pc_sect_function (tpmarker->address, NULL);
13983 ui_out_text (uiout, "Now in ");
13984 if (sym)
13985 {
13986 ui_out_field_string (uiout, "func",
13987 SYMBOL_PRINT_NAME (sym));
13988 ui_out_text (uiout, " at ");
13989 }
13990 ui_out_field_string (uiout, "file",
13991 symtab_to_filename_for_display (sal2.symtab));
13992 ui_out_text (uiout, ":");
13993
13994 if (ui_out_is_mi_like_p (uiout))
13995 {
13996 const char *fullname = symtab_to_fullname (sal2.symtab);
13997
13998 ui_out_field_string (uiout, "fullname", fullname);
13999 }
14000
14001 ui_out_field_int (uiout, "line", sal2.line);
14002 ui_out_text (uiout, "\n");
14003
14004 b->loc->line_number = sal2.line;
14005 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14006
14007 xfree (b->addr_string);
14008 b->addr_string = xstrprintf ("%s:%d",
14009 symtab_to_filename_for_display (sal2.symtab),
14010 b->loc->line_number);
14011
14012 /* Might be nice to check if function changed, and warn if
14013 so. */
14014
14015 release_static_tracepoint_marker (tpmarker);
14016 }
14017 }
14018 return sal;
14019}
14020
14021/* Returns 1 iff locations A and B are sufficiently same that
14022 we don't need to report breakpoint as changed. */
14023
14024static int
14025locations_are_equal (struct bp_location *a, struct bp_location *b)
14026{
14027 while (a && b)
14028 {
14029 if (a->address != b->address)
14030 return 0;
14031
14032 if (a->shlib_disabled != b->shlib_disabled)
14033 return 0;
14034
14035 if (a->enabled != b->enabled)
14036 return 0;
14037
14038 a = a->next;
14039 b = b->next;
14040 }
14041
14042 if ((a == NULL) != (b == NULL))
14043 return 0;
14044
14045 return 1;
14046}
14047
14048/* Create new breakpoint locations for B (a hardware or software breakpoint)
14049 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14050 a ranged breakpoint. */
14051
14052void
14053update_breakpoint_locations (struct breakpoint *b,
14054 struct symtabs_and_lines sals,
14055 struct symtabs_and_lines sals_end)
14056{
14057 int i;
14058 struct bp_location *existing_locations = b->loc;
14059
14060 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14061 {
14062 /* Ranged breakpoints have only one start location and one end
14063 location. */
14064 b->enable_state = bp_disabled;
14065 update_global_location_list (1);
14066 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14067 "multiple locations found\n"),
14068 b->number);
14069 return;
14070 }
14071
14072 /* If there's no new locations, and all existing locations are
14073 pending, don't do anything. This optimizes the common case where
14074 all locations are in the same shared library, that was unloaded.
14075 We'd like to retain the location, so that when the library is
14076 loaded again, we don't loose the enabled/disabled status of the
14077 individual locations. */
14078 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14079 return;
14080
14081 b->loc = NULL;
14082
14083 for (i = 0; i < sals.nelts; ++i)
14084 {
14085 struct bp_location *new_loc;
14086
14087 switch_to_program_space_and_thread (sals.sals[i].pspace);
14088
14089 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14090
14091 /* Reparse conditions, they might contain references to the
14092 old symtab. */
14093 if (b->cond_string != NULL)
14094 {
14095 const char *s;
14096 volatile struct gdb_exception e;
14097
14098 s = b->cond_string;
14099 TRY_CATCH (e, RETURN_MASK_ERROR)
14100 {
14101 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14102 block_for_pc (sals.sals[i].pc),
14103 0);
14104 }
14105 if (e.reason < 0)
14106 {
14107 warning (_("failed to reevaluate condition "
14108 "for breakpoint %d: %s"),
14109 b->number, e.message);
14110 new_loc->enabled = 0;
14111 }
14112 }
14113
14114 if (sals_end.nelts)
14115 {
14116 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14117
14118 new_loc->length = end - sals.sals[0].pc + 1;
14119 }
14120 }
14121
14122 /* Update locations of permanent breakpoints. */
14123 if (b->enable_state == bp_permanent)
14124 make_breakpoint_permanent (b);
14125
14126 /* If possible, carry over 'disable' status from existing
14127 breakpoints. */
14128 {
14129 struct bp_location *e = existing_locations;
14130 /* If there are multiple breakpoints with the same function name,
14131 e.g. for inline functions, comparing function names won't work.
14132 Instead compare pc addresses; this is just a heuristic as things
14133 may have moved, but in practice it gives the correct answer
14134 often enough until a better solution is found. */
14135 int have_ambiguous_names = ambiguous_names_p (b->loc);
14136
14137 for (; e; e = e->next)
14138 {
14139 if (!e->enabled && e->function_name)
14140 {
14141 struct bp_location *l = b->loc;
14142 if (have_ambiguous_names)
14143 {
14144 for (; l; l = l->next)
14145 if (breakpoint_locations_match (e, l))
14146 {
14147 l->enabled = 0;
14148 break;
14149 }
14150 }
14151 else
14152 {
14153 for (; l; l = l->next)
14154 if (l->function_name
14155 && strcmp (e->function_name, l->function_name) == 0)
14156 {
14157 l->enabled = 0;
14158 break;
14159 }
14160 }
14161 }
14162 }
14163 }
14164
14165 if (!locations_are_equal (existing_locations, b->loc))
14166 observer_notify_breakpoint_modified (b);
14167
14168 update_global_location_list (1);
14169}
14170
14171/* Find the SaL locations corresponding to the given ADDR_STRING.
14172 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14173
14174static struct symtabs_and_lines
14175addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14176{
14177 char *s;
14178 struct symtabs_and_lines sals = {0};
14179 volatile struct gdb_exception e;
14180
14181 gdb_assert (b->ops != NULL);
14182 s = addr_string;
14183
14184 TRY_CATCH (e, RETURN_MASK_ERROR)
14185 {
14186 b->ops->decode_linespec (b, &s, &sals);
14187 }
14188 if (e.reason < 0)
14189 {
14190 int not_found_and_ok = 0;
14191 /* For pending breakpoints, it's expected that parsing will
14192 fail until the right shared library is loaded. User has
14193 already told to create pending breakpoints and don't need
14194 extra messages. If breakpoint is in bp_shlib_disabled
14195 state, then user already saw the message about that
14196 breakpoint being disabled, and don't want to see more
14197 errors. */
14198 if (e.error == NOT_FOUND_ERROR
14199 && (b->condition_not_parsed
14200 || (b->loc && b->loc->shlib_disabled)
14201 || (b->loc && b->loc->pspace->executing_startup)
14202 || b->enable_state == bp_disabled))
14203 not_found_and_ok = 1;
14204
14205 if (!not_found_and_ok)
14206 {
14207 /* We surely don't want to warn about the same breakpoint
14208 10 times. One solution, implemented here, is disable
14209 the breakpoint on error. Another solution would be to
14210 have separate 'warning emitted' flag. Since this
14211 happens only when a binary has changed, I don't know
14212 which approach is better. */
14213 b->enable_state = bp_disabled;
14214 throw_exception (e);
14215 }
14216 }
14217
14218 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
14219 {
14220 int i;
14221
14222 for (i = 0; i < sals.nelts; ++i)
14223 resolve_sal_pc (&sals.sals[i]);
14224 if (b->condition_not_parsed && s && s[0])
14225 {
14226 char *cond_string, *extra_string;
14227 int thread, task;
14228
14229 find_condition_and_thread (s, sals.sals[0].pc,
14230 &cond_string, &thread, &task,
14231 &extra_string);
14232 if (cond_string)
14233 b->cond_string = cond_string;
14234 b->thread = thread;
14235 b->task = task;
14236 if (extra_string)
14237 b->extra_string = extra_string;
14238 b->condition_not_parsed = 0;
14239 }
14240
14241 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14242 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14243
14244 *found = 1;
14245 }
14246 else
14247 *found = 0;
14248
14249 return sals;
14250}
14251
14252/* The default re_set method, for typical hardware or software
14253 breakpoints. Reevaluate the breakpoint and recreate its
14254 locations. */
14255
14256static void
14257breakpoint_re_set_default (struct breakpoint *b)
14258{
14259 int found;
14260 struct symtabs_and_lines sals, sals_end;
14261 struct symtabs_and_lines expanded = {0};
14262 struct symtabs_and_lines expanded_end = {0};
14263
14264 sals = addr_string_to_sals (b, b->addr_string, &found);
14265 if (found)
14266 {
14267 make_cleanup (xfree, sals.sals);
14268 expanded = sals;
14269 }
14270
14271 if (b->addr_string_range_end)
14272 {
14273 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14274 if (found)
14275 {
14276 make_cleanup (xfree, sals_end.sals);
14277 expanded_end = sals_end;
14278 }
14279 }
14280
14281 update_breakpoint_locations (b, expanded, expanded_end);
14282}
14283
14284/* Default method for creating SALs from an address string. It basically
14285 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14286
14287static void
14288create_sals_from_address_default (char **arg,
14289 struct linespec_result *canonical,
14290 enum bptype type_wanted,
14291 char *addr_start, char **copy_arg)
14292{
14293 parse_breakpoint_sals (arg, canonical);
14294}
14295
14296/* Call create_breakpoints_sal for the given arguments. This is the default
14297 function for the `create_breakpoints_sal' method of
14298 breakpoint_ops. */
14299
14300static void
14301create_breakpoints_sal_default (struct gdbarch *gdbarch,
14302 struct linespec_result *canonical,
14303 char *cond_string,
14304 char *extra_string,
14305 enum bptype type_wanted,
14306 enum bpdisp disposition,
14307 int thread,
14308 int task, int ignore_count,
14309 const struct breakpoint_ops *ops,
14310 int from_tty, int enabled,
14311 int internal, unsigned flags)
14312{
14313 create_breakpoints_sal (gdbarch, canonical, cond_string,
14314 extra_string,
14315 type_wanted, disposition,
14316 thread, task, ignore_count, ops, from_tty,
14317 enabled, internal, flags);
14318}
14319
14320/* Decode the line represented by S by calling decode_line_full. This is the
14321 default function for the `decode_linespec' method of breakpoint_ops. */
14322
14323static void
14324decode_linespec_default (struct breakpoint *b, char **s,
14325 struct symtabs_and_lines *sals)
14326{
14327 struct linespec_result canonical;
14328
14329 init_linespec_result (&canonical);
14330 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14331 (struct symtab *) NULL, 0,
14332 &canonical, multiple_symbols_all,
14333 b->filter);
14334
14335 /* We should get 0 or 1 resulting SALs. */
14336 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14337
14338 if (VEC_length (linespec_sals, canonical.sals) > 0)
14339 {
14340 struct linespec_sals *lsal;
14341
14342 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14343 *sals = lsal->sals;
14344 /* Arrange it so the destructor does not free the
14345 contents. */
14346 lsal->sals.sals = NULL;
14347 }
14348
14349 destroy_linespec_result (&canonical);
14350}
14351
14352/* Prepare the global context for a re-set of breakpoint B. */
14353
14354static struct cleanup *
14355prepare_re_set_context (struct breakpoint *b)
14356{
14357 struct cleanup *cleanups;
14358
14359 input_radix = b->input_radix;
14360 cleanups = save_current_space_and_thread ();
14361 if (b->pspace != NULL)
14362 switch_to_program_space_and_thread (b->pspace);
14363 set_language (b->language);
14364
14365 return cleanups;
14366}
14367
14368/* Reset a breakpoint given it's struct breakpoint * BINT.
14369 The value we return ends up being the return value from catch_errors.
14370 Unused in this case. */
14371
14372static int
14373breakpoint_re_set_one (void *bint)
14374{
14375 /* Get past catch_errs. */
14376 struct breakpoint *b = (struct breakpoint *) bint;
14377 struct cleanup *cleanups;
14378
14379 cleanups = prepare_re_set_context (b);
14380 b->ops->re_set (b);
14381 do_cleanups (cleanups);
14382 return 0;
14383}
14384
14385/* Re-set all breakpoints after symbols have been re-loaded. */
14386void
14387breakpoint_re_set (void)
14388{
14389 struct breakpoint *b, *b_tmp;
14390 enum language save_language;
14391 int save_input_radix;
14392 struct cleanup *old_chain;
14393
14394 save_language = current_language->la_language;
14395 save_input_radix = input_radix;
14396 old_chain = save_current_program_space ();
14397
14398 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14399 {
14400 /* Format possible error msg. */
14401 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14402 b->number);
14403 struct cleanup *cleanups = make_cleanup (xfree, message);
14404 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14405 do_cleanups (cleanups);
14406 }
14407 set_language (save_language);
14408 input_radix = save_input_radix;
14409
14410 jit_breakpoint_re_set ();
14411
14412 do_cleanups (old_chain);
14413
14414 create_overlay_event_breakpoint ();
14415 create_longjmp_master_breakpoint ();
14416 create_std_terminate_master_breakpoint ();
14417 create_exception_master_breakpoint ();
14418}
14419\f
14420/* Reset the thread number of this breakpoint:
14421
14422 - If the breakpoint is for all threads, leave it as-is.
14423 - Else, reset it to the current thread for inferior_ptid. */
14424void
14425breakpoint_re_set_thread (struct breakpoint *b)
14426{
14427 if (b->thread != -1)
14428 {
14429 if (in_thread_list (inferior_ptid))
14430 b->thread = pid_to_thread_id (inferior_ptid);
14431
14432 /* We're being called after following a fork. The new fork is
14433 selected as current, and unless this was a vfork will have a
14434 different program space from the original thread. Reset that
14435 as well. */
14436 b->loc->pspace = current_program_space;
14437 }
14438}
14439
14440/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14441 If from_tty is nonzero, it prints a message to that effect,
14442 which ends with a period (no newline). */
14443
14444void
14445set_ignore_count (int bptnum, int count, int from_tty)
14446{
14447 struct breakpoint *b;
14448
14449 if (count < 0)
14450 count = 0;
14451
14452 ALL_BREAKPOINTS (b)
14453 if (b->number == bptnum)
14454 {
14455 if (is_tracepoint (b))
14456 {
14457 if (from_tty && count != 0)
14458 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14459 bptnum);
14460 return;
14461 }
14462
14463 b->ignore_count = count;
14464 if (from_tty)
14465 {
14466 if (count == 0)
14467 printf_filtered (_("Will stop next time "
14468 "breakpoint %d is reached."),
14469 bptnum);
14470 else if (count == 1)
14471 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14472 bptnum);
14473 else
14474 printf_filtered (_("Will ignore next %d "
14475 "crossings of breakpoint %d."),
14476 count, bptnum);
14477 }
14478 observer_notify_breakpoint_modified (b);
14479 return;
14480 }
14481
14482 error (_("No breakpoint number %d."), bptnum);
14483}
14484
14485/* Command to set ignore-count of breakpoint N to COUNT. */
14486
14487static void
14488ignore_command (char *args, int from_tty)
14489{
14490 char *p = args;
14491 int num;
14492
14493 if (p == 0)
14494 error_no_arg (_("a breakpoint number"));
14495
14496 num = get_number (&p);
14497 if (num == 0)
14498 error (_("bad breakpoint number: '%s'"), args);
14499 if (*p == 0)
14500 error (_("Second argument (specified ignore-count) is missing."));
14501
14502 set_ignore_count (num,
14503 longest_to_int (value_as_long (parse_and_eval (p))),
14504 from_tty);
14505 if (from_tty)
14506 printf_filtered ("\n");
14507}
14508\f
14509/* Call FUNCTION on each of the breakpoints
14510 whose numbers are given in ARGS. */
14511
14512static void
14513map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14514 void *),
14515 void *data)
14516{
14517 int num;
14518 struct breakpoint *b, *tmp;
14519 int match;
14520 struct get_number_or_range_state state;
14521
14522 if (args == 0)
14523 error_no_arg (_("one or more breakpoint numbers"));
14524
14525 init_number_or_range (&state, args);
14526
14527 while (!state.finished)
14528 {
14529 char *p = state.string;
14530
14531 match = 0;
14532
14533 num = get_number_or_range (&state);
14534 if (num == 0)
14535 {
14536 warning (_("bad breakpoint number at or near '%s'"), p);
14537 }
14538 else
14539 {
14540 ALL_BREAKPOINTS_SAFE (b, tmp)
14541 if (b->number == num)
14542 {
14543 match = 1;
14544 function (b, data);
14545 break;
14546 }
14547 if (match == 0)
14548 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14549 }
14550 }
14551}
14552
14553static struct bp_location *
14554find_location_by_number (char *number)
14555{
14556 char *dot = strchr (number, '.');
14557 char *p1;
14558 int bp_num;
14559 int loc_num;
14560 struct breakpoint *b;
14561 struct bp_location *loc;
14562
14563 *dot = '\0';
14564
14565 p1 = number;
14566 bp_num = get_number (&p1);
14567 if (bp_num == 0)
14568 error (_("Bad breakpoint number '%s'"), number);
14569
14570 ALL_BREAKPOINTS (b)
14571 if (b->number == bp_num)
14572 {
14573 break;
14574 }
14575
14576 if (!b || b->number != bp_num)
14577 error (_("Bad breakpoint number '%s'"), number);
14578
14579 p1 = dot+1;
14580 loc_num = get_number (&p1);
14581 if (loc_num == 0)
14582 error (_("Bad breakpoint location number '%s'"), number);
14583
14584 --loc_num;
14585 loc = b->loc;
14586 for (;loc_num && loc; --loc_num, loc = loc->next)
14587 ;
14588 if (!loc)
14589 error (_("Bad breakpoint location number '%s'"), dot+1);
14590
14591 return loc;
14592}
14593
14594
14595/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14596 If from_tty is nonzero, it prints a message to that effect,
14597 which ends with a period (no newline). */
14598
14599void
14600disable_breakpoint (struct breakpoint *bpt)
14601{
14602 /* Never disable a watchpoint scope breakpoint; we want to
14603 hit them when we leave scope so we can delete both the
14604 watchpoint and its scope breakpoint at that time. */
14605 if (bpt->type == bp_watchpoint_scope)
14606 return;
14607
14608 /* You can't disable permanent breakpoints. */
14609 if (bpt->enable_state == bp_permanent)
14610 return;
14611
14612 bpt->enable_state = bp_disabled;
14613
14614 /* Mark breakpoint locations modified. */
14615 mark_breakpoint_modified (bpt);
14616
14617 if (target_supports_enable_disable_tracepoint ()
14618 && current_trace_status ()->running && is_tracepoint (bpt))
14619 {
14620 struct bp_location *location;
14621
14622 for (location = bpt->loc; location; location = location->next)
14623 target_disable_tracepoint (location);
14624 }
14625
14626 update_global_location_list (0);
14627
14628 observer_notify_breakpoint_modified (bpt);
14629}
14630
14631/* A callback for iterate_over_related_breakpoints. */
14632
14633static void
14634do_disable_breakpoint (struct breakpoint *b, void *ignore)
14635{
14636 disable_breakpoint (b);
14637}
14638
14639/* A callback for map_breakpoint_numbers that calls
14640 disable_breakpoint. */
14641
14642static void
14643do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14644{
14645 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14646}
14647
14648static void
14649disable_command (char *args, int from_tty)
14650{
14651 if (args == 0)
14652 {
14653 struct breakpoint *bpt;
14654
14655 ALL_BREAKPOINTS (bpt)
14656 if (user_breakpoint_p (bpt))
14657 disable_breakpoint (bpt);
14658 }
14659 else
14660 {
14661 char *num = extract_arg (&args);
14662
14663 while (num)
14664 {
14665 if (strchr (num, '.'))
14666 {
14667 struct bp_location *loc = find_location_by_number (num);
14668
14669 if (loc)
14670 {
14671 if (loc->enabled)
14672 {
14673 loc->enabled = 0;
14674 mark_breakpoint_location_modified (loc);
14675 }
14676 if (target_supports_enable_disable_tracepoint ()
14677 && current_trace_status ()->running && loc->owner
14678 && is_tracepoint (loc->owner))
14679 target_disable_tracepoint (loc);
14680 }
14681 update_global_location_list (0);
14682 }
14683 else
14684 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14685 num = extract_arg (&args);
14686 }
14687 }
14688}
14689
14690static void
14691enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14692 int count)
14693{
14694 int target_resources_ok;
14695
14696 if (bpt->type == bp_hardware_breakpoint)
14697 {
14698 int i;
14699 i = hw_breakpoint_used_count ();
14700 target_resources_ok =
14701 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14702 i + 1, 0);
14703 if (target_resources_ok == 0)
14704 error (_("No hardware breakpoint support in the target."));
14705 else if (target_resources_ok < 0)
14706 error (_("Hardware breakpoints used exceeds limit."));
14707 }
14708
14709 if (is_watchpoint (bpt))
14710 {
14711 /* Initialize it just to avoid a GCC false warning. */
14712 enum enable_state orig_enable_state = 0;
14713 volatile struct gdb_exception e;
14714
14715 TRY_CATCH (e, RETURN_MASK_ALL)
14716 {
14717 struct watchpoint *w = (struct watchpoint *) bpt;
14718
14719 orig_enable_state = bpt->enable_state;
14720 bpt->enable_state = bp_enabled;
14721 update_watchpoint (w, 1 /* reparse */);
14722 }
14723 if (e.reason < 0)
14724 {
14725 bpt->enable_state = orig_enable_state;
14726 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14727 bpt->number);
14728 return;
14729 }
14730 }
14731
14732 if (bpt->enable_state != bp_permanent)
14733 bpt->enable_state = bp_enabled;
14734
14735 bpt->enable_state = bp_enabled;
14736
14737 /* Mark breakpoint locations modified. */
14738 mark_breakpoint_modified (bpt);
14739
14740 if (target_supports_enable_disable_tracepoint ()
14741 && current_trace_status ()->running && is_tracepoint (bpt))
14742 {
14743 struct bp_location *location;
14744
14745 for (location = bpt->loc; location; location = location->next)
14746 target_enable_tracepoint (location);
14747 }
14748
14749 bpt->disposition = disposition;
14750 bpt->enable_count = count;
14751 update_global_location_list (1);
14752
14753 observer_notify_breakpoint_modified (bpt);
14754}
14755
14756
14757void
14758enable_breakpoint (struct breakpoint *bpt)
14759{
14760 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14761}
14762
14763static void
14764do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14765{
14766 enable_breakpoint (bpt);
14767}
14768
14769/* A callback for map_breakpoint_numbers that calls
14770 enable_breakpoint. */
14771
14772static void
14773do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14774{
14775 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14776}
14777
14778/* The enable command enables the specified breakpoints (or all defined
14779 breakpoints) so they once again become (or continue to be) effective
14780 in stopping the inferior. */
14781
14782static void
14783enable_command (char *args, int from_tty)
14784{
14785 if (args == 0)
14786 {
14787 struct breakpoint *bpt;
14788
14789 ALL_BREAKPOINTS (bpt)
14790 if (user_breakpoint_p (bpt))
14791 enable_breakpoint (bpt);
14792 }
14793 else
14794 {
14795 char *num = extract_arg (&args);
14796
14797 while (num)
14798 {
14799 if (strchr (num, '.'))
14800 {
14801 struct bp_location *loc = find_location_by_number (num);
14802
14803 if (loc)
14804 {
14805 if (!loc->enabled)
14806 {
14807 loc->enabled = 1;
14808 mark_breakpoint_location_modified (loc);
14809 }
14810 if (target_supports_enable_disable_tracepoint ()
14811 && current_trace_status ()->running && loc->owner
14812 && is_tracepoint (loc->owner))
14813 target_enable_tracepoint (loc);
14814 }
14815 update_global_location_list (1);
14816 }
14817 else
14818 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14819 num = extract_arg (&args);
14820 }
14821 }
14822}
14823
14824/* This struct packages up disposition data for application to multiple
14825 breakpoints. */
14826
14827struct disp_data
14828{
14829 enum bpdisp disp;
14830 int count;
14831};
14832
14833static void
14834do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14835{
14836 struct disp_data disp_data = *(struct disp_data *) arg;
14837
14838 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14839}
14840
14841static void
14842do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14843{
14844 struct disp_data disp = { disp_disable, 1 };
14845
14846 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14847}
14848
14849static void
14850enable_once_command (char *args, int from_tty)
14851{
14852 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14853}
14854
14855static void
14856do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14857{
14858 struct disp_data disp = { disp_disable, *(int *) countptr };
14859
14860 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14861}
14862
14863static void
14864enable_count_command (char *args, int from_tty)
14865{
14866 int count = get_number (&args);
14867
14868 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14869}
14870
14871static void
14872do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14873{
14874 struct disp_data disp = { disp_del, 1 };
14875
14876 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14877}
14878
14879static void
14880enable_delete_command (char *args, int from_tty)
14881{
14882 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14883}
14884\f
14885static void
14886set_breakpoint_cmd (char *args, int from_tty)
14887{
14888}
14889
14890static void
14891show_breakpoint_cmd (char *args, int from_tty)
14892{
14893}
14894
14895/* Invalidate last known value of any hardware watchpoint if
14896 the memory which that value represents has been written to by
14897 GDB itself. */
14898
14899static void
14900invalidate_bp_value_on_memory_change (struct inferior *inferior,
14901 CORE_ADDR addr, ssize_t len,
14902 const bfd_byte *data)
14903{
14904 struct breakpoint *bp;
14905
14906 ALL_BREAKPOINTS (bp)
14907 if (bp->enable_state == bp_enabled
14908 && bp->type == bp_hardware_watchpoint)
14909 {
14910 struct watchpoint *wp = (struct watchpoint *) bp;
14911
14912 if (wp->val_valid && wp->val)
14913 {
14914 struct bp_location *loc;
14915
14916 for (loc = bp->loc; loc != NULL; loc = loc->next)
14917 if (loc->loc_type == bp_loc_hardware_watchpoint
14918 && loc->address + loc->length > addr
14919 && addr + len > loc->address)
14920 {
14921 value_free (wp->val);
14922 wp->val = NULL;
14923 wp->val_valid = 0;
14924 }
14925 }
14926 }
14927}
14928
14929/* Create and insert a raw software breakpoint at PC. Return an
14930 identifier, which should be used to remove the breakpoint later.
14931 In general, places which call this should be using something on the
14932 breakpoint chain instead; this function should be eliminated
14933 someday. */
14934
14935void *
14936deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14937 struct address_space *aspace, CORE_ADDR pc)
14938{
14939 struct bp_target_info *bp_tgt;
14940
14941 bp_tgt = XZALLOC (struct bp_target_info);
14942
14943 bp_tgt->placed_address_space = aspace;
14944 bp_tgt->placed_address = pc;
14945
14946 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
14947 {
14948 /* Could not insert the breakpoint. */
14949 xfree (bp_tgt);
14950 return NULL;
14951 }
14952
14953 return bp_tgt;
14954}
14955
14956/* Remove a breakpoint BP inserted by
14957 deprecated_insert_raw_breakpoint. */
14958
14959int
14960deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
14961{
14962 struct bp_target_info *bp_tgt = bp;
14963 int ret;
14964
14965 ret = target_remove_breakpoint (gdbarch, bp_tgt);
14966 xfree (bp_tgt);
14967
14968 return ret;
14969}
14970
14971/* One (or perhaps two) breakpoints used for software single
14972 stepping. */
14973
14974static void *single_step_breakpoints[2];
14975static struct gdbarch *single_step_gdbarch[2];
14976
14977/* Create and insert a breakpoint for software single step. */
14978
14979void
14980insert_single_step_breakpoint (struct gdbarch *gdbarch,
14981 struct address_space *aspace,
14982 CORE_ADDR next_pc)
14983{
14984 void **bpt_p;
14985
14986 if (single_step_breakpoints[0] == NULL)
14987 {
14988 bpt_p = &single_step_breakpoints[0];
14989 single_step_gdbarch[0] = gdbarch;
14990 }
14991 else
14992 {
14993 gdb_assert (single_step_breakpoints[1] == NULL);
14994 bpt_p = &single_step_breakpoints[1];
14995 single_step_gdbarch[1] = gdbarch;
14996 }
14997
14998 /* NOTE drow/2006-04-11: A future improvement to this function would
14999 be to only create the breakpoints once, and actually put them on
15000 the breakpoint chain. That would let us use set_raw_breakpoint.
15001 We could adjust the addresses each time they were needed. Doing
15002 this requires corresponding changes elsewhere where single step
15003 breakpoints are handled, however. So, for now, we use this. */
15004
15005 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
15006 if (*bpt_p == NULL)
15007 error (_("Could not insert single-step breakpoint at %s"),
15008 paddress (gdbarch, next_pc));
15009}
15010
15011/* Check if the breakpoints used for software single stepping
15012 were inserted or not. */
15013
15014int
15015single_step_breakpoints_inserted (void)
15016{
15017 return (single_step_breakpoints[0] != NULL
15018 || single_step_breakpoints[1] != NULL);
15019}
15020
15021/* Remove and delete any breakpoints used for software single step. */
15022
15023void
15024remove_single_step_breakpoints (void)
15025{
15026 gdb_assert (single_step_breakpoints[0] != NULL);
15027
15028 /* See insert_single_step_breakpoint for more about this deprecated
15029 call. */
15030 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15031 single_step_breakpoints[0]);
15032 single_step_gdbarch[0] = NULL;
15033 single_step_breakpoints[0] = NULL;
15034
15035 if (single_step_breakpoints[1] != NULL)
15036 {
15037 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15038 single_step_breakpoints[1]);
15039 single_step_gdbarch[1] = NULL;
15040 single_step_breakpoints[1] = NULL;
15041 }
15042}
15043
15044/* Delete software single step breakpoints without removing them from
15045 the inferior. This is intended to be used if the inferior's address
15046 space where they were inserted is already gone, e.g. after exit or
15047 exec. */
15048
15049void
15050cancel_single_step_breakpoints (void)
15051{
15052 int i;
15053
15054 for (i = 0; i < 2; i++)
15055 if (single_step_breakpoints[i])
15056 {
15057 xfree (single_step_breakpoints[i]);
15058 single_step_breakpoints[i] = NULL;
15059 single_step_gdbarch[i] = NULL;
15060 }
15061}
15062
15063/* Detach software single-step breakpoints from INFERIOR_PTID without
15064 removing them. */
15065
15066static void
15067detach_single_step_breakpoints (void)
15068{
15069 int i;
15070
15071 for (i = 0; i < 2; i++)
15072 if (single_step_breakpoints[i])
15073 target_remove_breakpoint (single_step_gdbarch[i],
15074 single_step_breakpoints[i]);
15075}
15076
15077/* Check whether a software single-step breakpoint is inserted at
15078 PC. */
15079
15080static int
15081single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15082 CORE_ADDR pc)
15083{
15084 int i;
15085
15086 for (i = 0; i < 2; i++)
15087 {
15088 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
15089 if (bp_tgt
15090 && breakpoint_address_match (bp_tgt->placed_address_space,
15091 bp_tgt->placed_address,
15092 aspace, pc))
15093 return 1;
15094 }
15095
15096 return 0;
15097}
15098
15099/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15100 non-zero otherwise. */
15101static int
15102is_syscall_catchpoint_enabled (struct breakpoint *bp)
15103{
15104 if (syscall_catchpoint_p (bp)
15105 && bp->enable_state != bp_disabled
15106 && bp->enable_state != bp_call_disabled)
15107 return 1;
15108 else
15109 return 0;
15110}
15111
15112int
15113catch_syscall_enabled (void)
15114{
15115 struct catch_syscall_inferior_data *inf_data
15116 = get_catch_syscall_inferior_data (current_inferior ());
15117
15118 return inf_data->total_syscalls_count != 0;
15119}
15120
15121int
15122catching_syscall_number (int syscall_number)
15123{
15124 struct breakpoint *bp;
15125
15126 ALL_BREAKPOINTS (bp)
15127 if (is_syscall_catchpoint_enabled (bp))
15128 {
15129 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15130
15131 if (c->syscalls_to_be_caught)
15132 {
15133 int i, iter;
15134 for (i = 0;
15135 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15136 i++)
15137 if (syscall_number == iter)
15138 return 1;
15139 }
15140 else
15141 return 1;
15142 }
15143
15144 return 0;
15145}
15146
15147/* Complete syscall names. Used by "catch syscall". */
15148static VEC (char_ptr) *
15149catch_syscall_completer (struct cmd_list_element *cmd,
15150 const char *text, const char *word)
15151{
15152 const char **list = get_syscall_names ();
15153 VEC (char_ptr) *retlist
15154 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15155
15156 xfree (list);
15157 return retlist;
15158}
15159
15160/* Tracepoint-specific operations. */
15161
15162/* Set tracepoint count to NUM. */
15163static void
15164set_tracepoint_count (int num)
15165{
15166 tracepoint_count = num;
15167 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15168}
15169
15170static void
15171trace_command (char *arg, int from_tty)
15172{
15173 struct breakpoint_ops *ops;
15174 const char *arg_cp = arg;
15175
15176 if (arg && probe_linespec_to_ops (&arg_cp))
15177 ops = &tracepoint_probe_breakpoint_ops;
15178 else
15179 ops = &tracepoint_breakpoint_ops;
15180
15181 create_breakpoint (get_current_arch (),
15182 arg,
15183 NULL, 0, NULL, 1 /* parse arg */,
15184 0 /* tempflag */,
15185 bp_tracepoint /* type_wanted */,
15186 0 /* Ignore count */,
15187 pending_break_support,
15188 ops,
15189 from_tty,
15190 1 /* enabled */,
15191 0 /* internal */, 0);
15192}
15193
15194static void
15195ftrace_command (char *arg, int from_tty)
15196{
15197 create_breakpoint (get_current_arch (),
15198 arg,
15199 NULL, 0, NULL, 1 /* parse arg */,
15200 0 /* tempflag */,
15201 bp_fast_tracepoint /* type_wanted */,
15202 0 /* Ignore count */,
15203 pending_break_support,
15204 &tracepoint_breakpoint_ops,
15205 from_tty,
15206 1 /* enabled */,
15207 0 /* internal */, 0);
15208}
15209
15210/* strace command implementation. Creates a static tracepoint. */
15211
15212static void
15213strace_command (char *arg, int from_tty)
15214{
15215 struct breakpoint_ops *ops;
15216
15217 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15218 or with a normal static tracepoint. */
15219 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15220 ops = &strace_marker_breakpoint_ops;
15221 else
15222 ops = &tracepoint_breakpoint_ops;
15223
15224 create_breakpoint (get_current_arch (),
15225 arg,
15226 NULL, 0, NULL, 1 /* parse arg */,
15227 0 /* tempflag */,
15228 bp_static_tracepoint /* type_wanted */,
15229 0 /* Ignore count */,
15230 pending_break_support,
15231 ops,
15232 from_tty,
15233 1 /* enabled */,
15234 0 /* internal */, 0);
15235}
15236
15237/* Set up a fake reader function that gets command lines from a linked
15238 list that was acquired during tracepoint uploading. */
15239
15240static struct uploaded_tp *this_utp;
15241static int next_cmd;
15242
15243static char *
15244read_uploaded_action (void)
15245{
15246 char *rslt;
15247
15248 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15249
15250 next_cmd++;
15251
15252 return rslt;
15253}
15254
15255/* Given information about a tracepoint as recorded on a target (which
15256 can be either a live system or a trace file), attempt to create an
15257 equivalent GDB tracepoint. This is not a reliable process, since
15258 the target does not necessarily have all the information used when
15259 the tracepoint was originally defined. */
15260
15261struct tracepoint *
15262create_tracepoint_from_upload (struct uploaded_tp *utp)
15263{
15264 char *addr_str, small_buf[100];
15265 struct tracepoint *tp;
15266
15267 if (utp->at_string)
15268 addr_str = utp->at_string;
15269 else
15270 {
15271 /* In the absence of a source location, fall back to raw
15272 address. Since there is no way to confirm that the address
15273 means the same thing as when the trace was started, warn the
15274 user. */
15275 warning (_("Uploaded tracepoint %d has no "
15276 "source location, using raw address"),
15277 utp->number);
15278 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15279 addr_str = small_buf;
15280 }
15281
15282 /* There's not much we can do with a sequence of bytecodes. */
15283 if (utp->cond && !utp->cond_string)
15284 warning (_("Uploaded tracepoint %d condition "
15285 "has no source form, ignoring it"),
15286 utp->number);
15287
15288 if (!create_breakpoint (get_current_arch (),
15289 addr_str,
15290 utp->cond_string, -1, NULL,
15291 0 /* parse cond/thread */,
15292 0 /* tempflag */,
15293 utp->type /* type_wanted */,
15294 0 /* Ignore count */,
15295 pending_break_support,
15296 &tracepoint_breakpoint_ops,
15297 0 /* from_tty */,
15298 utp->enabled /* enabled */,
15299 0 /* internal */,
15300 CREATE_BREAKPOINT_FLAGS_INSERTED))
15301 return NULL;
15302
15303 /* Get the tracepoint we just created. */
15304 tp = get_tracepoint (tracepoint_count);
15305 gdb_assert (tp != NULL);
15306
15307 if (utp->pass > 0)
15308 {
15309 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15310 tp->base.number);
15311
15312 trace_pass_command (small_buf, 0);
15313 }
15314
15315 /* If we have uploaded versions of the original commands, set up a
15316 special-purpose "reader" function and call the usual command line
15317 reader, then pass the result to the breakpoint command-setting
15318 function. */
15319 if (!VEC_empty (char_ptr, utp->cmd_strings))
15320 {
15321 struct command_line *cmd_list;
15322
15323 this_utp = utp;
15324 next_cmd = 0;
15325
15326 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15327
15328 breakpoint_set_commands (&tp->base, cmd_list);
15329 }
15330 else if (!VEC_empty (char_ptr, utp->actions)
15331 || !VEC_empty (char_ptr, utp->step_actions))
15332 warning (_("Uploaded tracepoint %d actions "
15333 "have no source form, ignoring them"),
15334 utp->number);
15335
15336 /* Copy any status information that might be available. */
15337 tp->base.hit_count = utp->hit_count;
15338 tp->traceframe_usage = utp->traceframe_usage;
15339
15340 return tp;
15341}
15342
15343/* Print information on tracepoint number TPNUM_EXP, or all if
15344 omitted. */
15345
15346static void
15347tracepoints_info (char *args, int from_tty)
15348{
15349 struct ui_out *uiout = current_uiout;
15350 int num_printed;
15351
15352 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15353
15354 if (num_printed == 0)
15355 {
15356 if (args == NULL || *args == '\0')
15357 ui_out_message (uiout, 0, "No tracepoints.\n");
15358 else
15359 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15360 }
15361
15362 default_collect_info ();
15363}
15364
15365/* The 'enable trace' command enables tracepoints.
15366 Not supported by all targets. */
15367static void
15368enable_trace_command (char *args, int from_tty)
15369{
15370 enable_command (args, from_tty);
15371}
15372
15373/* The 'disable trace' command disables tracepoints.
15374 Not supported by all targets. */
15375static void
15376disable_trace_command (char *args, int from_tty)
15377{
15378 disable_command (args, from_tty);
15379}
15380
15381/* Remove a tracepoint (or all if no argument). */
15382static void
15383delete_trace_command (char *arg, int from_tty)
15384{
15385 struct breakpoint *b, *b_tmp;
15386
15387 dont_repeat ();
15388
15389 if (arg == 0)
15390 {
15391 int breaks_to_delete = 0;
15392
15393 /* Delete all breakpoints if no argument.
15394 Do not delete internal or call-dummy breakpoints, these
15395 have to be deleted with an explicit breakpoint number
15396 argument. */
15397 ALL_TRACEPOINTS (b)
15398 if (is_tracepoint (b) && user_breakpoint_p (b))
15399 {
15400 breaks_to_delete = 1;
15401 break;
15402 }
15403
15404 /* Ask user only if there are some breakpoints to delete. */
15405 if (!from_tty
15406 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15407 {
15408 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15409 if (is_tracepoint (b) && user_breakpoint_p (b))
15410 delete_breakpoint (b);
15411 }
15412 }
15413 else
15414 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15415}
15416
15417/* Helper function for trace_pass_command. */
15418
15419static void
15420trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15421{
15422 tp->pass_count = count;
15423 observer_notify_breakpoint_modified (&tp->base);
15424 if (from_tty)
15425 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15426 tp->base.number, count);
15427}
15428
15429/* Set passcount for tracepoint.
15430
15431 First command argument is passcount, second is tracepoint number.
15432 If tracepoint number omitted, apply to most recently defined.
15433 Also accepts special argument "all". */
15434
15435static void
15436trace_pass_command (char *args, int from_tty)
15437{
15438 struct tracepoint *t1;
15439 unsigned int count;
15440
15441 if (args == 0 || *args == 0)
15442 error (_("passcount command requires an "
15443 "argument (count + optional TP num)"));
15444
15445 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15446
15447 args = skip_spaces (args);
15448 if (*args && strncasecmp (args, "all", 3) == 0)
15449 {
15450 struct breakpoint *b;
15451
15452 args += 3; /* Skip special argument "all". */
15453 if (*args)
15454 error (_("Junk at end of arguments."));
15455
15456 ALL_TRACEPOINTS (b)
15457 {
15458 t1 = (struct tracepoint *) b;
15459 trace_pass_set_count (t1, count, from_tty);
15460 }
15461 }
15462 else if (*args == '\0')
15463 {
15464 t1 = get_tracepoint_by_number (&args, NULL, 1);
15465 if (t1)
15466 trace_pass_set_count (t1, count, from_tty);
15467 }
15468 else
15469 {
15470 struct get_number_or_range_state state;
15471
15472 init_number_or_range (&state, args);
15473 while (!state.finished)
15474 {
15475 t1 = get_tracepoint_by_number (&args, &state, 1);
15476 if (t1)
15477 trace_pass_set_count (t1, count, from_tty);
15478 }
15479 }
15480}
15481
15482struct tracepoint *
15483get_tracepoint (int num)
15484{
15485 struct breakpoint *t;
15486
15487 ALL_TRACEPOINTS (t)
15488 if (t->number == num)
15489 return (struct tracepoint *) t;
15490
15491 return NULL;
15492}
15493
15494/* Find the tracepoint with the given target-side number (which may be
15495 different from the tracepoint number after disconnecting and
15496 reconnecting). */
15497
15498struct tracepoint *
15499get_tracepoint_by_number_on_target (int num)
15500{
15501 struct breakpoint *b;
15502
15503 ALL_TRACEPOINTS (b)
15504 {
15505 struct tracepoint *t = (struct tracepoint *) b;
15506
15507 if (t->number_on_target == num)
15508 return t;
15509 }
15510
15511 return NULL;
15512}
15513
15514/* Utility: parse a tracepoint number and look it up in the list.
15515 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15516 If OPTIONAL_P is true, then if the argument is missing, the most
15517 recent tracepoint (tracepoint_count) is returned. */
15518struct tracepoint *
15519get_tracepoint_by_number (char **arg,
15520 struct get_number_or_range_state *state,
15521 int optional_p)
15522{
15523 struct breakpoint *t;
15524 int tpnum;
15525 char *instring = arg == NULL ? NULL : *arg;
15526
15527 if (state)
15528 {
15529 gdb_assert (!state->finished);
15530 tpnum = get_number_or_range (state);
15531 }
15532 else if (arg == NULL || *arg == NULL || ! **arg)
15533 {
15534 if (optional_p)
15535 tpnum = tracepoint_count;
15536 else
15537 error_no_arg (_("tracepoint number"));
15538 }
15539 else
15540 tpnum = get_number (arg);
15541
15542 if (tpnum <= 0)
15543 {
15544 if (instring && *instring)
15545 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15546 instring);
15547 else
15548 printf_filtered (_("Tracepoint argument missing "
15549 "and no previous tracepoint\n"));
15550 return NULL;
15551 }
15552
15553 ALL_TRACEPOINTS (t)
15554 if (t->number == tpnum)
15555 {
15556 return (struct tracepoint *) t;
15557 }
15558
15559 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15560 return NULL;
15561}
15562
15563void
15564print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15565{
15566 if (b->thread != -1)
15567 fprintf_unfiltered (fp, " thread %d", b->thread);
15568
15569 if (b->task != 0)
15570 fprintf_unfiltered (fp, " task %d", b->task);
15571
15572 fprintf_unfiltered (fp, "\n");
15573}
15574
15575/* Save information on user settable breakpoints (watchpoints, etc) to
15576 a new script file named FILENAME. If FILTER is non-NULL, call it
15577 on each breakpoint and only include the ones for which it returns
15578 non-zero. */
15579
15580static void
15581save_breakpoints (char *filename, int from_tty,
15582 int (*filter) (const struct breakpoint *))
15583{
15584 struct breakpoint *tp;
15585 int any = 0;
15586 struct cleanup *cleanup;
15587 struct ui_file *fp;
15588 int extra_trace_bits = 0;
15589
15590 if (filename == 0 || *filename == 0)
15591 error (_("Argument required (file name in which to save)"));
15592
15593 /* See if we have anything to save. */
15594 ALL_BREAKPOINTS (tp)
15595 {
15596 /* Skip internal and momentary breakpoints. */
15597 if (!user_breakpoint_p (tp))
15598 continue;
15599
15600 /* If we have a filter, only save the breakpoints it accepts. */
15601 if (filter && !filter (tp))
15602 continue;
15603
15604 any = 1;
15605
15606 if (is_tracepoint (tp))
15607 {
15608 extra_trace_bits = 1;
15609
15610 /* We can stop searching. */
15611 break;
15612 }
15613 }
15614
15615 if (!any)
15616 {
15617 warning (_("Nothing to save."));
15618 return;
15619 }
15620
15621 filename = tilde_expand (filename);
15622 cleanup = make_cleanup (xfree, filename);
15623 fp = gdb_fopen (filename, "w");
15624 if (!fp)
15625 error (_("Unable to open file '%s' for saving (%s)"),
15626 filename, safe_strerror (errno));
15627 make_cleanup_ui_file_delete (fp);
15628
15629 if (extra_trace_bits)
15630 save_trace_state_variables (fp);
15631
15632 ALL_BREAKPOINTS (tp)
15633 {
15634 /* Skip internal and momentary breakpoints. */
15635 if (!user_breakpoint_p (tp))
15636 continue;
15637
15638 /* If we have a filter, only save the breakpoints it accepts. */
15639 if (filter && !filter (tp))
15640 continue;
15641
15642 tp->ops->print_recreate (tp, fp);
15643
15644 /* Note, we can't rely on tp->number for anything, as we can't
15645 assume the recreated breakpoint numbers will match. Use $bpnum
15646 instead. */
15647
15648 if (tp->cond_string)
15649 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15650
15651 if (tp->ignore_count)
15652 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15653
15654 if (tp->type != bp_dprintf && tp->commands)
15655 {
15656 volatile struct gdb_exception ex;
15657
15658 fprintf_unfiltered (fp, " commands\n");
15659
15660 ui_out_redirect (current_uiout, fp);
15661 TRY_CATCH (ex, RETURN_MASK_ALL)
15662 {
15663 print_command_lines (current_uiout, tp->commands->commands, 2);
15664 }
15665 ui_out_redirect (current_uiout, NULL);
15666
15667 if (ex.reason < 0)
15668 throw_exception (ex);
15669
15670 fprintf_unfiltered (fp, " end\n");
15671 }
15672
15673 if (tp->enable_state == bp_disabled)
15674 fprintf_unfiltered (fp, "disable\n");
15675
15676 /* If this is a multi-location breakpoint, check if the locations
15677 should be individually disabled. Watchpoint locations are
15678 special, and not user visible. */
15679 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15680 {
15681 struct bp_location *loc;
15682 int n = 1;
15683
15684 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15685 if (!loc->enabled)
15686 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15687 }
15688 }
15689
15690 if (extra_trace_bits && *default_collect)
15691 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15692
15693 if (from_tty)
15694 printf_filtered (_("Saved to file '%s'.\n"), filename);
15695 do_cleanups (cleanup);
15696}
15697
15698/* The `save breakpoints' command. */
15699
15700static void
15701save_breakpoints_command (char *args, int from_tty)
15702{
15703 save_breakpoints (args, from_tty, NULL);
15704}
15705
15706/* The `save tracepoints' command. */
15707
15708static void
15709save_tracepoints_command (char *args, int from_tty)
15710{
15711 save_breakpoints (args, from_tty, is_tracepoint);
15712}
15713
15714/* Create a vector of all tracepoints. */
15715
15716VEC(breakpoint_p) *
15717all_tracepoints (void)
15718{
15719 VEC(breakpoint_p) *tp_vec = 0;
15720 struct breakpoint *tp;
15721
15722 ALL_TRACEPOINTS (tp)
15723 {
15724 VEC_safe_push (breakpoint_p, tp_vec, tp);
15725 }
15726
15727 return tp_vec;
15728}
15729
15730\f
15731/* This help string is used for the break, hbreak, tbreak and thbreak
15732 commands. It is defined as a macro to prevent duplication.
15733 COMMAND should be a string constant containing the name of the
15734 command. */
15735#define BREAK_ARGS_HELP(command) \
15736command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15737PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15738probe point. Accepted values are `-probe' (for a generic, automatically\n\
15739guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
15740LOCATION may be a line number, function name, or \"*\" and an address.\n\
15741If a line number is specified, break at start of code for that line.\n\
15742If a function is specified, break at start of code for that function.\n\
15743If an address is specified, break at that exact address.\n\
15744With no LOCATION, uses current execution address of the selected\n\
15745stack frame. This is useful for breaking on return to a stack frame.\n\
15746\n\
15747THREADNUM is the number from \"info threads\".\n\
15748CONDITION is a boolean expression.\n\
15749\n\
15750Multiple breakpoints at one place are permitted, and useful if their\n\
15751conditions are different.\n\
15752\n\
15753Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15754
15755/* List of subcommands for "catch". */
15756static struct cmd_list_element *catch_cmdlist;
15757
15758/* List of subcommands for "tcatch". */
15759static struct cmd_list_element *tcatch_cmdlist;
15760
15761void
15762add_catch_command (char *name, char *docstring,
15763 void (*sfunc) (char *args, int from_tty,
15764 struct cmd_list_element *command),
15765 completer_ftype *completer,
15766 void *user_data_catch,
15767 void *user_data_tcatch)
15768{
15769 struct cmd_list_element *command;
15770
15771 command = add_cmd (name, class_breakpoint, NULL, docstring,
15772 &catch_cmdlist);
15773 set_cmd_sfunc (command, sfunc);
15774 set_cmd_context (command, user_data_catch);
15775 set_cmd_completer (command, completer);
15776
15777 command = add_cmd (name, class_breakpoint, NULL, docstring,
15778 &tcatch_cmdlist);
15779 set_cmd_sfunc (command, sfunc);
15780 set_cmd_context (command, user_data_tcatch);
15781 set_cmd_completer (command, completer);
15782}
15783
15784static void
15785clear_syscall_counts (struct inferior *inf)
15786{
15787 struct catch_syscall_inferior_data *inf_data
15788 = get_catch_syscall_inferior_data (inf);
15789
15790 inf_data->total_syscalls_count = 0;
15791 inf_data->any_syscall_count = 0;
15792 VEC_free (int, inf_data->syscalls_counts);
15793}
15794
15795static void
15796save_command (char *arg, int from_tty)
15797{
15798 printf_unfiltered (_("\"save\" must be followed by "
15799 "the name of a save subcommand.\n"));
15800 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15801}
15802
15803struct breakpoint *
15804iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15805 void *data)
15806{
15807 struct breakpoint *b, *b_tmp;
15808
15809 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15810 {
15811 if ((*callback) (b, data))
15812 return b;
15813 }
15814
15815 return NULL;
15816}
15817
15818/* Zero if any of the breakpoint's locations could be a location where
15819 functions have been inlined, nonzero otherwise. */
15820
15821static int
15822is_non_inline_function (struct breakpoint *b)
15823{
15824 /* The shared library event breakpoint is set on the address of a
15825 non-inline function. */
15826 if (b->type == bp_shlib_event)
15827 return 1;
15828
15829 return 0;
15830}
15831
15832/* Nonzero if the specified PC cannot be a location where functions
15833 have been inlined. */
15834
15835int
15836pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15837 const struct target_waitstatus *ws)
15838{
15839 struct breakpoint *b;
15840 struct bp_location *bl;
15841
15842 ALL_BREAKPOINTS (b)
15843 {
15844 if (!is_non_inline_function (b))
15845 continue;
15846
15847 for (bl = b->loc; bl != NULL; bl = bl->next)
15848 {
15849 if (!bl->shlib_disabled
15850 && bpstat_check_location (bl, aspace, pc, ws))
15851 return 1;
15852 }
15853 }
15854
15855 return 0;
15856}
15857
15858/* Remove any references to OBJFILE which is going to be freed. */
15859
15860void
15861breakpoint_free_objfile (struct objfile *objfile)
15862{
15863 struct bp_location **locp, *loc;
15864
15865 ALL_BP_LOCATIONS (loc, locp)
15866 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
15867 loc->symtab = NULL;
15868}
15869
15870void
15871initialize_breakpoint_ops (void)
15872{
15873 static int initialized = 0;
15874
15875 struct breakpoint_ops *ops;
15876
15877 if (initialized)
15878 return;
15879 initialized = 1;
15880
15881 /* The breakpoint_ops structure to be inherit by all kinds of
15882 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15883 internal and momentary breakpoints, etc.). */
15884 ops = &bkpt_base_breakpoint_ops;
15885 *ops = base_breakpoint_ops;
15886 ops->re_set = bkpt_re_set;
15887 ops->insert_location = bkpt_insert_location;
15888 ops->remove_location = bkpt_remove_location;
15889 ops->breakpoint_hit = bkpt_breakpoint_hit;
15890 ops->create_sals_from_address = bkpt_create_sals_from_address;
15891 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15892 ops->decode_linespec = bkpt_decode_linespec;
15893
15894 /* The breakpoint_ops structure to be used in regular breakpoints. */
15895 ops = &bkpt_breakpoint_ops;
15896 *ops = bkpt_base_breakpoint_ops;
15897 ops->re_set = bkpt_re_set;
15898 ops->resources_needed = bkpt_resources_needed;
15899 ops->print_it = bkpt_print_it;
15900 ops->print_mention = bkpt_print_mention;
15901 ops->print_recreate = bkpt_print_recreate;
15902
15903 /* Ranged breakpoints. */
15904 ops = &ranged_breakpoint_ops;
15905 *ops = bkpt_breakpoint_ops;
15906 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15907 ops->resources_needed = resources_needed_ranged_breakpoint;
15908 ops->print_it = print_it_ranged_breakpoint;
15909 ops->print_one = print_one_ranged_breakpoint;
15910 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15911 ops->print_mention = print_mention_ranged_breakpoint;
15912 ops->print_recreate = print_recreate_ranged_breakpoint;
15913
15914 /* Internal breakpoints. */
15915 ops = &internal_breakpoint_ops;
15916 *ops = bkpt_base_breakpoint_ops;
15917 ops->re_set = internal_bkpt_re_set;
15918 ops->check_status = internal_bkpt_check_status;
15919 ops->print_it = internal_bkpt_print_it;
15920 ops->print_mention = internal_bkpt_print_mention;
15921
15922 /* Momentary breakpoints. */
15923 ops = &momentary_breakpoint_ops;
15924 *ops = bkpt_base_breakpoint_ops;
15925 ops->re_set = momentary_bkpt_re_set;
15926 ops->check_status = momentary_bkpt_check_status;
15927 ops->print_it = momentary_bkpt_print_it;
15928 ops->print_mention = momentary_bkpt_print_mention;
15929
15930 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15931 ops = &longjmp_breakpoint_ops;
15932 *ops = momentary_breakpoint_ops;
15933 ops->dtor = longjmp_bkpt_dtor;
15934
15935 /* Probe breakpoints. */
15936 ops = &bkpt_probe_breakpoint_ops;
15937 *ops = bkpt_breakpoint_ops;
15938 ops->insert_location = bkpt_probe_insert_location;
15939 ops->remove_location = bkpt_probe_remove_location;
15940 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15941 ops->decode_linespec = bkpt_probe_decode_linespec;
15942
15943 /* Watchpoints. */
15944 ops = &watchpoint_breakpoint_ops;
15945 *ops = base_breakpoint_ops;
15946 ops->dtor = dtor_watchpoint;
15947 ops->re_set = re_set_watchpoint;
15948 ops->insert_location = insert_watchpoint;
15949 ops->remove_location = remove_watchpoint;
15950 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15951 ops->check_status = check_status_watchpoint;
15952 ops->resources_needed = resources_needed_watchpoint;
15953 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15954 ops->print_it = print_it_watchpoint;
15955 ops->print_mention = print_mention_watchpoint;
15956 ops->print_recreate = print_recreate_watchpoint;
15957 ops->explains_signal = explains_signal_watchpoint;
15958
15959 /* Masked watchpoints. */
15960 ops = &masked_watchpoint_breakpoint_ops;
15961 *ops = watchpoint_breakpoint_ops;
15962 ops->insert_location = insert_masked_watchpoint;
15963 ops->remove_location = remove_masked_watchpoint;
15964 ops->resources_needed = resources_needed_masked_watchpoint;
15965 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15966 ops->print_it = print_it_masked_watchpoint;
15967 ops->print_one_detail = print_one_detail_masked_watchpoint;
15968 ops->print_mention = print_mention_masked_watchpoint;
15969 ops->print_recreate = print_recreate_masked_watchpoint;
15970
15971 /* Tracepoints. */
15972 ops = &tracepoint_breakpoint_ops;
15973 *ops = base_breakpoint_ops;
15974 ops->re_set = tracepoint_re_set;
15975 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15976 ops->print_one_detail = tracepoint_print_one_detail;
15977 ops->print_mention = tracepoint_print_mention;
15978 ops->print_recreate = tracepoint_print_recreate;
15979 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15980 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15981 ops->decode_linespec = tracepoint_decode_linespec;
15982
15983 /* Probe tracepoints. */
15984 ops = &tracepoint_probe_breakpoint_ops;
15985 *ops = tracepoint_breakpoint_ops;
15986 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15987 ops->decode_linespec = tracepoint_probe_decode_linespec;
15988
15989 /* Static tracepoints with marker (`-m'). */
15990 ops = &strace_marker_breakpoint_ops;
15991 *ops = tracepoint_breakpoint_ops;
15992 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15993 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15994 ops->decode_linespec = strace_marker_decode_linespec;
15995
15996 /* Fork catchpoints. */
15997 ops = &catch_fork_breakpoint_ops;
15998 *ops = base_breakpoint_ops;
15999 ops->insert_location = insert_catch_fork;
16000 ops->remove_location = remove_catch_fork;
16001 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16002 ops->print_it = print_it_catch_fork;
16003 ops->print_one = print_one_catch_fork;
16004 ops->print_mention = print_mention_catch_fork;
16005 ops->print_recreate = print_recreate_catch_fork;
16006
16007 /* Vfork catchpoints. */
16008 ops = &catch_vfork_breakpoint_ops;
16009 *ops = base_breakpoint_ops;
16010 ops->insert_location = insert_catch_vfork;
16011 ops->remove_location = remove_catch_vfork;
16012 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16013 ops->print_it = print_it_catch_vfork;
16014 ops->print_one = print_one_catch_vfork;
16015 ops->print_mention = print_mention_catch_vfork;
16016 ops->print_recreate = print_recreate_catch_vfork;
16017
16018 /* Exec catchpoints. */
16019 ops = &catch_exec_breakpoint_ops;
16020 *ops = base_breakpoint_ops;
16021 ops->dtor = dtor_catch_exec;
16022 ops->insert_location = insert_catch_exec;
16023 ops->remove_location = remove_catch_exec;
16024 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16025 ops->print_it = print_it_catch_exec;
16026 ops->print_one = print_one_catch_exec;
16027 ops->print_mention = print_mention_catch_exec;
16028 ops->print_recreate = print_recreate_catch_exec;
16029
16030 /* Syscall catchpoints. */
16031 ops = &catch_syscall_breakpoint_ops;
16032 *ops = base_breakpoint_ops;
16033 ops->dtor = dtor_catch_syscall;
16034 ops->insert_location = insert_catch_syscall;
16035 ops->remove_location = remove_catch_syscall;
16036 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16037 ops->print_it = print_it_catch_syscall;
16038 ops->print_one = print_one_catch_syscall;
16039 ops->print_mention = print_mention_catch_syscall;
16040 ops->print_recreate = print_recreate_catch_syscall;
16041
16042 /* Solib-related catchpoints. */
16043 ops = &catch_solib_breakpoint_ops;
16044 *ops = base_breakpoint_ops;
16045 ops->dtor = dtor_catch_solib;
16046 ops->insert_location = insert_catch_solib;
16047 ops->remove_location = remove_catch_solib;
16048 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16049 ops->check_status = check_status_catch_solib;
16050 ops->print_it = print_it_catch_solib;
16051 ops->print_one = print_one_catch_solib;
16052 ops->print_mention = print_mention_catch_solib;
16053 ops->print_recreate = print_recreate_catch_solib;
16054
16055 ops = &dprintf_breakpoint_ops;
16056 *ops = bkpt_base_breakpoint_ops;
16057 ops->re_set = dprintf_re_set;
16058 ops->resources_needed = bkpt_resources_needed;
16059 ops->print_it = bkpt_print_it;
16060 ops->print_mention = bkpt_print_mention;
16061 ops->print_recreate = dprintf_print_recreate;
16062 ops->after_condition_true = dprintf_after_condition_true;
16063}
16064
16065/* Chain containing all defined "enable breakpoint" subcommands. */
16066
16067static struct cmd_list_element *enablebreaklist = NULL;
16068
16069void
16070_initialize_breakpoint (void)
16071{
16072 struct cmd_list_element *c;
16073
16074 initialize_breakpoint_ops ();
16075
16076 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16077 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16078 observer_attach_inferior_exit (clear_syscall_counts);
16079 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16080
16081 breakpoint_objfile_key
16082 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16083
16084 catch_syscall_inferior_data
16085 = register_inferior_data_with_cleanup (NULL,
16086 catch_syscall_inferior_data_cleanup);
16087
16088 breakpoint_chain = 0;
16089 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16090 before a breakpoint is set. */
16091 breakpoint_count = 0;
16092
16093 tracepoint_count = 0;
16094
16095 add_com ("ignore", class_breakpoint, ignore_command, _("\
16096Set ignore-count of breakpoint number N to COUNT.\n\
16097Usage is `ignore N COUNT'."));
16098 if (xdb_commands)
16099 add_com_alias ("bc", "ignore", class_breakpoint, 1);
16100
16101 add_com ("commands", class_breakpoint, commands_command, _("\
16102Set commands to be executed when a breakpoint is hit.\n\
16103Give breakpoint number as argument after \"commands\".\n\
16104With no argument, the targeted breakpoint is the last one set.\n\
16105The commands themselves follow starting on the next line.\n\
16106Type a line containing \"end\" to indicate the end of them.\n\
16107Give \"silent\" as the first line to make the breakpoint silent;\n\
16108then no output is printed when it is hit, except what the commands print."));
16109
16110 c = add_com ("condition", class_breakpoint, condition_command, _("\
16111Specify breakpoint number N to break only if COND is true.\n\
16112Usage is `condition N COND', where N is an integer and COND is an\n\
16113expression to be evaluated whenever breakpoint N is reached."));
16114 set_cmd_completer (c, condition_completer);
16115
16116 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16117Set a temporary breakpoint.\n\
16118Like \"break\" except the breakpoint is only temporary,\n\
16119so it will be deleted when hit. Equivalent to \"break\" followed\n\
16120by using \"enable delete\" on the breakpoint number.\n\
16121\n"
16122BREAK_ARGS_HELP ("tbreak")));
16123 set_cmd_completer (c, location_completer);
16124
16125 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16126Set a hardware assisted breakpoint.\n\
16127Like \"break\" except the breakpoint requires hardware support,\n\
16128some target hardware may not have this support.\n\
16129\n"
16130BREAK_ARGS_HELP ("hbreak")));
16131 set_cmd_completer (c, location_completer);
16132
16133 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16134Set a temporary hardware assisted breakpoint.\n\
16135Like \"hbreak\" except the breakpoint is only temporary,\n\
16136so it will be deleted when hit.\n\
16137\n"
16138BREAK_ARGS_HELP ("thbreak")));
16139 set_cmd_completer (c, location_completer);
16140
16141 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16142Enable some breakpoints.\n\
16143Give breakpoint numbers (separated by spaces) as arguments.\n\
16144With no subcommand, breakpoints are enabled until you command otherwise.\n\
16145This is used to cancel the effect of the \"disable\" command.\n\
16146With a subcommand you can enable temporarily."),
16147 &enablelist, "enable ", 1, &cmdlist);
16148 if (xdb_commands)
16149 add_com ("ab", class_breakpoint, enable_command, _("\
16150Enable some breakpoints.\n\
16151Give breakpoint numbers (separated by spaces) as arguments.\n\
16152With no subcommand, breakpoints are enabled until you command otherwise.\n\
16153This is used to cancel the effect of the \"disable\" command.\n\
16154With a subcommand you can enable temporarily."));
16155
16156 add_com_alias ("en", "enable", class_breakpoint, 1);
16157
16158 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16159Enable some breakpoints.\n\
16160Give breakpoint numbers (separated by spaces) as arguments.\n\
16161This is used to cancel the effect of the \"disable\" command.\n\
16162May be abbreviated to simply \"enable\".\n"),
16163 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16164
16165 add_cmd ("once", no_class, enable_once_command, _("\
16166Enable breakpoints for one hit. Give breakpoint numbers.\n\
16167If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16168 &enablebreaklist);
16169
16170 add_cmd ("delete", no_class, enable_delete_command, _("\
16171Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16172If a breakpoint is hit while enabled in this fashion, it is deleted."),
16173 &enablebreaklist);
16174
16175 add_cmd ("count", no_class, enable_count_command, _("\
16176Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16177If a breakpoint is hit while enabled in this fashion,\n\
16178the count is decremented; when it reaches zero, the breakpoint is disabled."),
16179 &enablebreaklist);
16180
16181 add_cmd ("delete", no_class, enable_delete_command, _("\
16182Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16183If a breakpoint is hit while enabled in this fashion, it is deleted."),
16184 &enablelist);
16185
16186 add_cmd ("once", no_class, enable_once_command, _("\
16187Enable breakpoints for one hit. Give breakpoint numbers.\n\
16188If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16189 &enablelist);
16190
16191 add_cmd ("count", no_class, enable_count_command, _("\
16192Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16193If a breakpoint is hit while enabled in this fashion,\n\
16194the count is decremented; when it reaches zero, the breakpoint is disabled."),
16195 &enablelist);
16196
16197 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16198Disable some breakpoints.\n\
16199Arguments are breakpoint numbers with spaces in between.\n\
16200To disable all breakpoints, give no argument.\n\
16201A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16202 &disablelist, "disable ", 1, &cmdlist);
16203 add_com_alias ("dis", "disable", class_breakpoint, 1);
16204 add_com_alias ("disa", "disable", class_breakpoint, 1);
16205 if (xdb_commands)
16206 add_com ("sb", class_breakpoint, disable_command, _("\
16207Disable some breakpoints.\n\
16208Arguments are breakpoint numbers with spaces in between.\n\
16209To disable all breakpoints, give no argument.\n\
16210A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16211
16212 add_cmd ("breakpoints", class_alias, disable_command, _("\
16213Disable some breakpoints.\n\
16214Arguments are breakpoint numbers with spaces in between.\n\
16215To disable all breakpoints, give no argument.\n\
16216A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16217This command may be abbreviated \"disable\"."),
16218 &disablelist);
16219
16220 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16221Delete some breakpoints or auto-display expressions.\n\
16222Arguments are breakpoint numbers with spaces in between.\n\
16223To delete all breakpoints, give no argument.\n\
16224\n\
16225Also a prefix command for deletion of other GDB objects.\n\
16226The \"unset\" command is also an alias for \"delete\"."),
16227 &deletelist, "delete ", 1, &cmdlist);
16228 add_com_alias ("d", "delete", class_breakpoint, 1);
16229 add_com_alias ("del", "delete", class_breakpoint, 1);
16230 if (xdb_commands)
16231 add_com ("db", class_breakpoint, delete_command, _("\
16232Delete some breakpoints.\n\
16233Arguments are breakpoint numbers with spaces in between.\n\
16234To delete all breakpoints, give no argument.\n"));
16235
16236 add_cmd ("breakpoints", class_alias, delete_command, _("\
16237Delete some breakpoints or auto-display expressions.\n\
16238Arguments are breakpoint numbers with spaces in between.\n\
16239To delete all breakpoints, give no argument.\n\
16240This command may be abbreviated \"delete\"."),
16241 &deletelist);
16242
16243 add_com ("clear", class_breakpoint, clear_command, _("\
16244Clear breakpoint at specified line or function.\n\
16245Argument may be line number, function name, or \"*\" and an address.\n\
16246If line number is specified, all breakpoints in that line are cleared.\n\
16247If function is specified, breakpoints at beginning of function are cleared.\n\
16248If an address is specified, breakpoints at that address are cleared.\n\
16249\n\
16250With no argument, clears all breakpoints in the line that the selected frame\n\
16251is executing in.\n\
16252\n\
16253See also the \"delete\" command which clears breakpoints by number."));
16254 add_com_alias ("cl", "clear", class_breakpoint, 1);
16255
16256 c = add_com ("break", class_breakpoint, break_command, _("\
16257Set breakpoint at specified line or function.\n"
16258BREAK_ARGS_HELP ("break")));
16259 set_cmd_completer (c, location_completer);
16260
16261 add_com_alias ("b", "break", class_run, 1);
16262 add_com_alias ("br", "break", class_run, 1);
16263 add_com_alias ("bre", "break", class_run, 1);
16264 add_com_alias ("brea", "break", class_run, 1);
16265
16266 if (xdb_commands)
16267 add_com_alias ("ba", "break", class_breakpoint, 1);
16268
16269 if (dbx_commands)
16270 {
16271 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16272Break in function/address or break at a line in the current file."),
16273 &stoplist, "stop ", 1, &cmdlist);
16274 add_cmd ("in", class_breakpoint, stopin_command,
16275 _("Break in function or address."), &stoplist);
16276 add_cmd ("at", class_breakpoint, stopat_command,
16277 _("Break at a line in the current file."), &stoplist);
16278 add_com ("status", class_info, breakpoints_info, _("\
16279Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16280The \"Type\" column indicates one of:\n\
16281\tbreakpoint - normal breakpoint\n\
16282\twatchpoint - watchpoint\n\
16283The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16284the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16285breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16286address and file/line number respectively.\n\
16287\n\
16288Convenience variable \"$_\" and default examine address for \"x\"\n\
16289are set to the address of the last breakpoint listed unless the command\n\
16290is prefixed with \"server \".\n\n\
16291Convenience variable \"$bpnum\" contains the number of the last\n\
16292breakpoint set."));
16293 }
16294
16295 add_info ("breakpoints", breakpoints_info, _("\
16296Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16297The \"Type\" column indicates one of:\n\
16298\tbreakpoint - normal breakpoint\n\
16299\twatchpoint - watchpoint\n\
16300The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16301the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16302breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16303address and file/line number respectively.\n\
16304\n\
16305Convenience variable \"$_\" and default examine address for \"x\"\n\
16306are set to the address of the last breakpoint listed unless the command\n\
16307is prefixed with \"server \".\n\n\
16308Convenience variable \"$bpnum\" contains the number of the last\n\
16309breakpoint set."));
16310
16311 add_info_alias ("b", "breakpoints", 1);
16312
16313 if (xdb_commands)
16314 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16315Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16316The \"Type\" column indicates one of:\n\
16317\tbreakpoint - normal breakpoint\n\
16318\twatchpoint - watchpoint\n\
16319The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16320the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16321breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16322address and file/line number respectively.\n\
16323\n\
16324Convenience variable \"$_\" and default examine address for \"x\"\n\
16325are set to the address of the last breakpoint listed unless the command\n\
16326is prefixed with \"server \".\n\n\
16327Convenience variable \"$bpnum\" contains the number of the last\n\
16328breakpoint set."));
16329
16330 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16331Status of all breakpoints, or breakpoint number NUMBER.\n\
16332The \"Type\" column indicates one of:\n\
16333\tbreakpoint - normal breakpoint\n\
16334\twatchpoint - watchpoint\n\
16335\tlongjmp - internal breakpoint used to step through longjmp()\n\
16336\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16337\tuntil - internal breakpoint used by the \"until\" command\n\
16338\tfinish - internal breakpoint used by the \"finish\" command\n\
16339The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16340the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16341breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16342address and file/line number respectively.\n\
16343\n\
16344Convenience variable \"$_\" and default examine address for \"x\"\n\
16345are set to the address of the last breakpoint listed unless the command\n\
16346is prefixed with \"server \".\n\n\
16347Convenience variable \"$bpnum\" contains the number of the last\n\
16348breakpoint set."),
16349 &maintenanceinfolist);
16350
16351 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16352Set catchpoints to catch events."),
16353 &catch_cmdlist, "catch ",
16354 0/*allow-unknown*/, &cmdlist);
16355
16356 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16357Set temporary catchpoints to catch events."),
16358 &tcatch_cmdlist, "tcatch ",
16359 0/*allow-unknown*/, &cmdlist);
16360
16361 add_catch_command ("fork", _("Catch calls to fork."),
16362 catch_fork_command_1,
16363 NULL,
16364 (void *) (uintptr_t) catch_fork_permanent,
16365 (void *) (uintptr_t) catch_fork_temporary);
16366 add_catch_command ("vfork", _("Catch calls to vfork."),
16367 catch_fork_command_1,
16368 NULL,
16369 (void *) (uintptr_t) catch_vfork_permanent,
16370 (void *) (uintptr_t) catch_vfork_temporary);
16371 add_catch_command ("exec", _("Catch calls to exec."),
16372 catch_exec_command_1,
16373 NULL,
16374 CATCH_PERMANENT,
16375 CATCH_TEMPORARY);
16376 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16377Usage: catch load [REGEX]\n\
16378If REGEX is given, only stop for libraries matching the regular expression."),
16379 catch_load_command_1,
16380 NULL,
16381 CATCH_PERMANENT,
16382 CATCH_TEMPORARY);
16383 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16384Usage: catch unload [REGEX]\n\
16385If REGEX is given, only stop for libraries matching the regular expression."),
16386 catch_unload_command_1,
16387 NULL,
16388 CATCH_PERMANENT,
16389 CATCH_TEMPORARY);
16390 add_catch_command ("syscall", _("\
16391Catch system calls by their names and/or numbers.\n\
16392Arguments say which system calls to catch. If no arguments\n\
16393are given, every system call will be caught.\n\
16394Arguments, if given, should be one or more system call names\n\
16395(if your system supports that), or system call numbers."),
16396 catch_syscall_command_1,
16397 catch_syscall_completer,
16398 CATCH_PERMANENT,
16399 CATCH_TEMPORARY);
16400
16401 c = add_com ("watch", class_breakpoint, watch_command, _("\
16402Set a watchpoint for an expression.\n\
16403Usage: watch [-l|-location] EXPRESSION\n\
16404A watchpoint stops execution of your program whenever the value of\n\
16405an expression changes.\n\
16406If -l or -location is given, this evaluates EXPRESSION and watches\n\
16407the memory to which it refers."));
16408 set_cmd_completer (c, expression_completer);
16409
16410 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16411Set a read watchpoint for an expression.\n\
16412Usage: rwatch [-l|-location] EXPRESSION\n\
16413A watchpoint stops execution of your program whenever the value of\n\
16414an expression is read.\n\
16415If -l or -location is given, this evaluates EXPRESSION and watches\n\
16416the memory to which it refers."));
16417 set_cmd_completer (c, expression_completer);
16418
16419 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16420Set a watchpoint for an expression.\n\
16421Usage: awatch [-l|-location] EXPRESSION\n\
16422A watchpoint stops execution of your program whenever the value of\n\
16423an expression is either read or written.\n\
16424If -l or -location is given, this evaluates EXPRESSION and watches\n\
16425the memory to which it refers."));
16426 set_cmd_completer (c, expression_completer);
16427
16428 add_info ("watchpoints", watchpoints_info, _("\
16429Status of specified watchpoints (all watchpoints if no argument)."));
16430
16431 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16432 respond to changes - contrary to the description. */
16433 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16434 &can_use_hw_watchpoints, _("\
16435Set debugger's willingness to use watchpoint hardware."), _("\
16436Show debugger's willingness to use watchpoint hardware."), _("\
16437If zero, gdb will not use hardware for new watchpoints, even if\n\
16438such is available. (However, any hardware watchpoints that were\n\
16439created before setting this to nonzero, will continue to use watchpoint\n\
16440hardware.)"),
16441 NULL,
16442 show_can_use_hw_watchpoints,
16443 &setlist, &showlist);
16444
16445 can_use_hw_watchpoints = 1;
16446
16447 /* Tracepoint manipulation commands. */
16448
16449 c = add_com ("trace", class_breakpoint, trace_command, _("\
16450Set a tracepoint at specified line or function.\n\
16451\n"
16452BREAK_ARGS_HELP ("trace") "\n\
16453Do \"help tracepoints\" for info on other tracepoint commands."));
16454 set_cmd_completer (c, location_completer);
16455
16456 add_com_alias ("tp", "trace", class_alias, 0);
16457 add_com_alias ("tr", "trace", class_alias, 1);
16458 add_com_alias ("tra", "trace", class_alias, 1);
16459 add_com_alias ("trac", "trace", class_alias, 1);
16460
16461 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16462Set a fast tracepoint at specified line or function.\n\
16463\n"
16464BREAK_ARGS_HELP ("ftrace") "\n\
16465Do \"help tracepoints\" for info on other tracepoint commands."));
16466 set_cmd_completer (c, location_completer);
16467
16468 c = add_com ("strace", class_breakpoint, strace_command, _("\
16469Set a static tracepoint at specified line, function or marker.\n\
16470\n\
16471strace [LOCATION] [if CONDITION]\n\
16472LOCATION may be a line number, function name, \"*\" and an address,\n\
16473or -m MARKER_ID.\n\
16474If a line number is specified, probe the marker at start of code\n\
16475for that line. If a function is specified, probe the marker at start\n\
16476of code for that function. If an address is specified, probe the marker\n\
16477at that exact address. If a marker id is specified, probe the marker\n\
16478with that name. With no LOCATION, uses current execution address of\n\
16479the selected stack frame.\n\
16480Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16481This collects arbitrary user data passed in the probe point call to the\n\
16482tracing library. You can inspect it when analyzing the trace buffer,\n\
16483by printing the $_sdata variable like any other convenience variable.\n\
16484\n\
16485CONDITION is a boolean expression.\n\
16486\n\
16487Multiple tracepoints at one place are permitted, and useful if their\n\
16488conditions are different.\n\
16489\n\
16490Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16491Do \"help tracepoints\" for info on other tracepoint commands."));
16492 set_cmd_completer (c, location_completer);
16493
16494 add_info ("tracepoints", tracepoints_info, _("\
16495Status of specified tracepoints (all tracepoints if no argument).\n\
16496Convenience variable \"$tpnum\" contains the number of the\n\
16497last tracepoint set."));
16498
16499 add_info_alias ("tp", "tracepoints", 1);
16500
16501 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16502Delete specified tracepoints.\n\
16503Arguments are tracepoint numbers, separated by spaces.\n\
16504No argument means delete all tracepoints."),
16505 &deletelist);
16506 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16507
16508 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16509Disable specified tracepoints.\n\
16510Arguments are tracepoint numbers, separated by spaces.\n\
16511No argument means disable all tracepoints."),
16512 &disablelist);
16513 deprecate_cmd (c, "disable");
16514
16515 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16516Enable specified tracepoints.\n\
16517Arguments are tracepoint numbers, separated by spaces.\n\
16518No argument means enable all tracepoints."),
16519 &enablelist);
16520 deprecate_cmd (c, "enable");
16521
16522 add_com ("passcount", class_trace, trace_pass_command, _("\
16523Set the passcount for a tracepoint.\n\
16524The trace will end when the tracepoint has been passed 'count' times.\n\
16525Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16526if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16527
16528 add_prefix_cmd ("save", class_breakpoint, save_command,
16529 _("Save breakpoint definitions as a script."),
16530 &save_cmdlist, "save ",
16531 0/*allow-unknown*/, &cmdlist);
16532
16533 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16534Save current breakpoint definitions as a script.\n\
16535This includes all types of breakpoints (breakpoints, watchpoints,\n\
16536catchpoints, tracepoints). Use the 'source' command in another debug\n\
16537session to restore them."),
16538 &save_cmdlist);
16539 set_cmd_completer (c, filename_completer);
16540
16541 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16542Save current tracepoint definitions as a script.\n\
16543Use the 'source' command in another debug session to restore them."),
16544 &save_cmdlist);
16545 set_cmd_completer (c, filename_completer);
16546
16547 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16548 deprecate_cmd (c, "save tracepoints");
16549
16550 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16551Breakpoint specific settings\n\
16552Configure various breakpoint-specific variables such as\n\
16553pending breakpoint behavior"),
16554 &breakpoint_set_cmdlist, "set breakpoint ",
16555 0/*allow-unknown*/, &setlist);
16556 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16557Breakpoint specific settings\n\
16558Configure various breakpoint-specific variables such as\n\
16559pending breakpoint behavior"),
16560 &breakpoint_show_cmdlist, "show breakpoint ",
16561 0/*allow-unknown*/, &showlist);
16562
16563 add_setshow_auto_boolean_cmd ("pending", no_class,
16564 &pending_break_support, _("\
16565Set debugger's behavior regarding pending breakpoints."), _("\
16566Show debugger's behavior regarding pending breakpoints."), _("\
16567If on, an unrecognized breakpoint location will cause gdb to create a\n\
16568pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16569an error. If auto, an unrecognized breakpoint location results in a\n\
16570user-query to see if a pending breakpoint should be created."),
16571 NULL,
16572 show_pending_break_support,
16573 &breakpoint_set_cmdlist,
16574 &breakpoint_show_cmdlist);
16575
16576 pending_break_support = AUTO_BOOLEAN_AUTO;
16577
16578 add_setshow_boolean_cmd ("auto-hw", no_class,
16579 &automatic_hardware_breakpoints, _("\
16580Set automatic usage of hardware breakpoints."), _("\
16581Show automatic usage of hardware breakpoints."), _("\
16582If set, the debugger will automatically use hardware breakpoints for\n\
16583breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16584a warning will be emitted for such breakpoints."),
16585 NULL,
16586 show_automatic_hardware_breakpoints,
16587 &breakpoint_set_cmdlist,
16588 &breakpoint_show_cmdlist);
16589
16590 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16591 &always_inserted_mode, _("\
16592Set mode for inserting breakpoints."), _("\
16593Show mode for inserting breakpoints."), _("\
16594When this mode is off, breakpoints are inserted in inferior when it is\n\
16595resumed, and removed when execution stops. When this mode is on,\n\
16596breakpoints are inserted immediately and removed only when the user\n\
16597deletes the breakpoint. When this mode is auto (which is the default),\n\
16598the behaviour depends on the non-stop setting (see help set non-stop).\n\
16599In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16600behaves as if always-inserted mode is on; if gdb is controlling the\n\
16601inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
16602 NULL,
16603 &show_always_inserted_mode,
16604 &breakpoint_set_cmdlist,
16605 &breakpoint_show_cmdlist);
16606
16607 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16608 condition_evaluation_enums,
16609 &condition_evaluation_mode_1, _("\
16610Set mode of breakpoint condition evaluation."), _("\
16611Show mode of breakpoint condition evaluation."), _("\
16612When this is set to \"host\", breakpoint conditions will be\n\
16613evaluated on the host's side by GDB. When it is set to \"target\",\n\
16614breakpoint conditions will be downloaded to the target (if the target\n\
16615supports such feature) and conditions will be evaluated on the target's side.\n\
16616If this is set to \"auto\" (default), this will be automatically set to\n\
16617\"target\" if it supports condition evaluation, otherwise it will\n\
16618be set to \"gdb\""),
16619 &set_condition_evaluation_mode,
16620 &show_condition_evaluation_mode,
16621 &breakpoint_set_cmdlist,
16622 &breakpoint_show_cmdlist);
16623
16624 add_com ("break-range", class_breakpoint, break_range_command, _("\
16625Set a breakpoint for an address range.\n\
16626break-range START-LOCATION, END-LOCATION\n\
16627where START-LOCATION and END-LOCATION can be one of the following:\n\
16628 LINENUM, for that line in the current file,\n\
16629 FILE:LINENUM, for that line in that file,\n\
16630 +OFFSET, for that number of lines after the current line\n\
16631 or the start of the range\n\
16632 FUNCTION, for the first line in that function,\n\
16633 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16634 *ADDRESS, for the instruction at that address.\n\
16635\n\
16636The breakpoint will stop execution of the inferior whenever it executes\n\
16637an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16638range (including START-LOCATION and END-LOCATION)."));
16639
16640 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16641Set a dynamic printf at specified line or function.\n\
16642dprintf location,format string,arg1,arg2,...\n\
16643location may be a line number, function name, or \"*\" and an address.\n\
16644If a line number is specified, break at start of code for that line.\n\
16645If a function is specified, break at start of code for that function."));
16646 set_cmd_completer (c, location_completer);
16647
16648 add_setshow_enum_cmd ("dprintf-style", class_support,
16649 dprintf_style_enums, &dprintf_style, _("\
16650Set the style of usage for dynamic printf."), _("\
16651Show the style of usage for dynamic printf."), _("\
16652This setting chooses how GDB will do a dynamic printf.\n\
16653If the value is \"gdb\", then the printing is done by GDB to its own\n\
16654console, as with the \"printf\" command.\n\
16655If the value is \"call\", the print is done by calling a function in your\n\
16656program; by default printf(), but you can choose a different function or\n\
16657output stream by setting dprintf-function and dprintf-channel."),
16658 update_dprintf_commands, NULL,
16659 &setlist, &showlist);
16660
16661 dprintf_function = xstrdup ("printf");
16662 add_setshow_string_cmd ("dprintf-function", class_support,
16663 &dprintf_function, _("\
16664Set the function to use for dynamic printf"), _("\
16665Show the function to use for dynamic printf"), NULL,
16666 update_dprintf_commands, NULL,
16667 &setlist, &showlist);
16668
16669 dprintf_channel = xstrdup ("");
16670 add_setshow_string_cmd ("dprintf-channel", class_support,
16671 &dprintf_channel, _("\
16672Set the channel to use for dynamic printf"), _("\
16673Show the channel to use for dynamic printf"), NULL,
16674 update_dprintf_commands, NULL,
16675 &setlist, &showlist);
16676
16677 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16678 &disconnected_dprintf, _("\
16679Set whether dprintf continues after GDB disconnects."), _("\
16680Show whether dprintf continues after GDB disconnects."), _("\
16681Use this to let dprintf commands continue to hit and produce output\n\
16682even if GDB disconnects or detaches from the target."),
16683 NULL,
16684 NULL,
16685 &setlist, &showlist);
16686
16687 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16688agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16689(target agent only) This is useful for formatted output in user-defined commands."));
16690
16691 automatic_hardware_breakpoints = 1;
16692
16693 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16694 observer_attach_thread_exit (remove_threaded_breakpoints);
16695}
This page took 0.079653 seconds and 4 git commands to generate.