cli-script.c: Simplify using std::string, eliminate cleanups
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
... / ...
CommitLineData
1/* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2016 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 "infrun.h"
36#include "gdbthread.h"
37#include "target.h"
38#include "language.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 "block.h"
51#include "solib.h"
52#include "solist.h"
53#include "observer.h"
54#include "memattr.h"
55#include "ada-lang.h"
56#include "top.h"
57#include "valprint.h"
58#include "jit.h"
59#include "parser-defs.h"
60#include "gdb_regex.h"
61#include "probe.h"
62#include "cli/cli-utils.h"
63#include "continuations.h"
64#include "stack.h"
65#include "skip.h"
66#include "ax-gdb.h"
67#include "dummy-frame.h"
68#include "interps.h"
69#include "format.h"
70#include "location.h"
71#include "thread-fsm.h"
72#include "tid-parse.h"
73
74/* readline include files */
75#include "readline/readline.h"
76#include "readline/history.h"
77
78/* readline defines this. */
79#undef savestring
80
81#include "mi/mi-common.h"
82#include "extension.h"
83#include <algorithm>
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 (const char *,
106 void (*) (struct breakpoint *,
107 void *),
108 void *);
109
110static void ignore_command (char *, int);
111
112static int breakpoint_re_set_one (void *);
113
114static void breakpoint_re_set_default (struct breakpoint *);
115
116static void
117 create_sals_from_location_default (const struct event_location *location,
118 struct linespec_result *canonical,
119 enum bptype type_wanted);
120
121static void create_breakpoints_sal_default (struct gdbarch *,
122 struct linespec_result *,
123 char *, char *, enum bptype,
124 enum bpdisp, int, int,
125 int,
126 const struct breakpoint_ops *,
127 int, int, int, unsigned);
128
129static void decode_location_default (struct breakpoint *b,
130 const struct event_location *location,
131 struct program_space *search_pspace,
132 struct symtabs_and_lines *sals);
133
134static void clear_command (char *, int);
135
136static void catch_command (char *, int);
137
138static int can_use_hardware_watchpoint (struct value *);
139
140static void break_command_1 (char *, int, int);
141
142static void mention (struct breakpoint *);
143
144static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
145 enum bptype,
146 const struct breakpoint_ops *);
147static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
148 const struct symtab_and_line *);
149
150/* This function is used in gdbtk sources and thus can not be made
151 static. */
152struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
153 struct symtab_and_line,
154 enum bptype,
155 const struct breakpoint_ops *);
156
157static struct breakpoint *
158 momentary_breakpoint_from_master (struct breakpoint *orig,
159 enum bptype type,
160 const struct breakpoint_ops *ops,
161 int loc_enabled);
162
163static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
164
165static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
166 CORE_ADDR bpaddr,
167 enum bptype bptype);
168
169static void describe_other_breakpoints (struct gdbarch *,
170 struct program_space *, CORE_ADDR,
171 struct obj_section *, int);
172
173static int watchpoint_locations_match (struct bp_location *loc1,
174 struct bp_location *loc2);
175
176static int breakpoint_location_address_match (struct bp_location *bl,
177 struct address_space *aspace,
178 CORE_ADDR addr);
179
180static int breakpoint_location_address_range_overlap (struct bp_location *,
181 struct address_space *,
182 CORE_ADDR, int);
183
184static void breakpoints_info (char *, int);
185
186static void watchpoints_info (char *, int);
187
188static int breakpoint_1 (char *, int,
189 int (*) (const struct breakpoint *));
190
191static int breakpoint_cond_eval (void *);
192
193static void cleanup_executing_breakpoints (void *);
194
195static void commands_command (char *, int);
196
197static void condition_command (char *, int);
198
199static int remove_breakpoint (struct bp_location *);
200static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
201
202static enum print_stop_action print_bp_stop_message (bpstat bs);
203
204static int watchpoint_check (void *);
205
206static void maintenance_info_breakpoints (char *, int);
207
208static int hw_breakpoint_used_count (void);
209
210static int hw_watchpoint_use_count (struct breakpoint *);
211
212static int hw_watchpoint_used_count_others (struct breakpoint *except,
213 enum bptype type,
214 int *other_type_used);
215
216static void hbreak_command (char *, int);
217
218static void thbreak_command (char *, int);
219
220static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
221 int count);
222
223static void stop_command (char *arg, int from_tty);
224
225static void stopin_command (char *arg, int from_tty);
226
227static void stopat_command (char *arg, int from_tty);
228
229static void tcatch_command (char *arg, int from_tty);
230
231static void free_bp_location (struct bp_location *loc);
232static void incref_bp_location (struct bp_location *loc);
233static void decref_bp_location (struct bp_location **loc);
234
235static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
236
237/* update_global_location_list's modes of operation wrt to whether to
238 insert locations now. */
239enum ugll_insert_mode
240{
241 /* Don't insert any breakpoint locations into the inferior, only
242 remove already-inserted locations that no longer should be
243 inserted. Functions that delete a breakpoint or breakpoints
244 should specify this mode, so that deleting a breakpoint doesn't
245 have the side effect of inserting the locations of other
246 breakpoints that are marked not-inserted, but should_be_inserted
247 returns true on them.
248
249 This behavior is useful is situations close to tear-down -- e.g.,
250 after an exec, while the target still has execution, but
251 breakpoint shadows of the previous executable image should *NOT*
252 be restored to the new image; or before detaching, where the
253 target still has execution and wants to delete breakpoints from
254 GDB's lists, and all breakpoints had already been removed from
255 the inferior. */
256 UGLL_DONT_INSERT,
257
258 /* May insert breakpoints iff breakpoints_should_be_inserted_now
259 claims breakpoints should be inserted now. */
260 UGLL_MAY_INSERT,
261
262 /* Insert locations now, irrespective of
263 breakpoints_should_be_inserted_now. E.g., say all threads are
264 stopped right now, and the user did "continue". We need to
265 insert breakpoints _before_ resuming the target, but
266 UGLL_MAY_INSERT wouldn't insert them, because
267 breakpoints_should_be_inserted_now returns false at that point,
268 as no thread is running yet. */
269 UGLL_INSERT
270};
271
272static void update_global_location_list (enum ugll_insert_mode);
273
274static void update_global_location_list_nothrow (enum ugll_insert_mode);
275
276static int is_hardware_watchpoint (const struct breakpoint *bpt);
277
278static void insert_breakpoint_locations (void);
279
280static void tracepoints_info (char *, int);
281
282static void delete_trace_command (char *, int);
283
284static void enable_trace_command (char *, int);
285
286static void disable_trace_command (char *, int);
287
288static void trace_pass_command (char *, int);
289
290static void set_tracepoint_count (int num);
291
292static int is_masked_watchpoint (const struct breakpoint *b);
293
294static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
295
296/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
297 otherwise. */
298
299static int strace_marker_p (struct breakpoint *b);
300
301/* The breakpoint_ops structure to be inherited by all breakpoint_ops
302 that are implemented on top of software or hardware breakpoints
303 (user breakpoints, internal and momentary breakpoints, etc.). */
304static struct breakpoint_ops bkpt_base_breakpoint_ops;
305
306/* Internal breakpoints class type. */
307static struct breakpoint_ops internal_breakpoint_ops;
308
309/* Momentary breakpoints class type. */
310static struct breakpoint_ops momentary_breakpoint_ops;
311
312/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
313static struct breakpoint_ops longjmp_breakpoint_ops;
314
315/* The breakpoint_ops structure to be used in regular user created
316 breakpoints. */
317struct breakpoint_ops bkpt_breakpoint_ops;
318
319/* Breakpoints set on probes. */
320static struct breakpoint_ops bkpt_probe_breakpoint_ops;
321
322/* Dynamic printf class type. */
323struct breakpoint_ops dprintf_breakpoint_ops;
324
325/* The style in which to perform a dynamic printf. This is a user
326 option because different output options have different tradeoffs;
327 if GDB does the printing, there is better error handling if there
328 is a problem with any of the arguments, but using an inferior
329 function lets you have special-purpose printers and sending of
330 output to the same place as compiled-in print functions. */
331
332static const char dprintf_style_gdb[] = "gdb";
333static const char dprintf_style_call[] = "call";
334static const char dprintf_style_agent[] = "agent";
335static const char *const dprintf_style_enums[] = {
336 dprintf_style_gdb,
337 dprintf_style_call,
338 dprintf_style_agent,
339 NULL
340};
341static const char *dprintf_style = dprintf_style_gdb;
342
343/* The function to use for dynamic printf if the preferred style is to
344 call into the inferior. The value is simply a string that is
345 copied into the command, so it can be anything that GDB can
346 evaluate to a callable address, not necessarily a function name. */
347
348static char *dprintf_function = "";
349
350/* The channel to use for dynamic printf if the preferred style is to
351 call into the inferior; if a nonempty string, it will be passed to
352 the call as the first argument, with the format string as the
353 second. As with the dprintf function, this can be anything that
354 GDB knows how to evaluate, so in addition to common choices like
355 "stderr", this could be an app-specific expression like
356 "mystreams[curlogger]". */
357
358static char *dprintf_channel = "";
359
360/* True if dprintf commands should continue to operate even if GDB
361 has disconnected. */
362static int disconnected_dprintf = 1;
363
364/* A reference-counted struct command_line. This lets multiple
365 breakpoints share a single command list. */
366struct counted_command_line
367{
368 /* The reference count. */
369 int refc;
370
371 /* The command list. */
372 struct command_line *commands;
373};
374
375struct command_line *
376breakpoint_commands (struct breakpoint *b)
377{
378 return b->commands ? b->commands->commands : NULL;
379}
380
381/* Flag indicating that a command has proceeded the inferior past the
382 current breakpoint. */
383
384static int breakpoint_proceeded;
385
386const char *
387bpdisp_text (enum bpdisp disp)
388{
389 /* NOTE: the following values are a part of MI protocol and
390 represent values of 'disp' field returned when inferior stops at
391 a breakpoint. */
392 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
393
394 return bpdisps[(int) disp];
395}
396
397/* Prototypes for exported functions. */
398/* If FALSE, gdb will not use hardware support for watchpoints, even
399 if such is available. */
400static int can_use_hw_watchpoints;
401
402static void
403show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406{
407 fprintf_filtered (file,
408 _("Debugger's willingness to use "
409 "watchpoint hardware is %s.\n"),
410 value);
411}
412
413/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
414 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
415 for unrecognized breakpoint locations.
416 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
417static enum auto_boolean pending_break_support;
418static void
419show_pending_break_support (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c,
421 const char *value)
422{
423 fprintf_filtered (file,
424 _("Debugger's behavior regarding "
425 "pending breakpoints is %s.\n"),
426 value);
427}
428
429/* If 1, gdb will automatically use hardware breakpoints for breakpoints
430 set with "break" but falling in read-only memory.
431 If 0, gdb will warn about such breakpoints, but won't automatically
432 use hardware breakpoints. */
433static int automatic_hardware_breakpoints;
434static void
435show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
436 struct cmd_list_element *c,
437 const char *value)
438{
439 fprintf_filtered (file,
440 _("Automatic usage of hardware breakpoints is %s.\n"),
441 value);
442}
443
444/* If on, GDB keeps breakpoints inserted even if the inferior is
445 stopped, and immediately inserts any new breakpoints as soon as
446 they're created. If off (default), GDB keeps breakpoints off of
447 the target as long as possible. That is, it delays inserting
448 breakpoints until the next resume, and removes them again when the
449 target fully stops. This is a bit safer in case GDB crashes while
450 processing user input. */
451static int always_inserted_mode = 0;
452
453static void
454show_always_inserted_mode (struct ui_file *file, int from_tty,
455 struct cmd_list_element *c, const char *value)
456{
457 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
458 value);
459}
460
461/* See breakpoint.h. */
462
463int
464breakpoints_should_be_inserted_now (void)
465{
466 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
467 {
468 /* If breakpoints are global, they should be inserted even if no
469 thread under gdb's control is running, or even if there are
470 no threads under GDB's control yet. */
471 return 1;
472 }
473 else if (target_has_execution)
474 {
475 struct thread_info *tp;
476
477 if (always_inserted_mode)
478 {
479 /* The user wants breakpoints inserted even if all threads
480 are stopped. */
481 return 1;
482 }
483
484 if (threads_are_executing ())
485 return 1;
486
487 /* Don't remove breakpoints yet if, even though all threads are
488 stopped, we still have events to process. */
489 ALL_NON_EXITED_THREADS (tp)
490 if (tp->resumed
491 && tp->suspend.waitstatus_pending_p)
492 return 1;
493 }
494 return 0;
495}
496
497static const char condition_evaluation_both[] = "host or target";
498
499/* Modes for breakpoint condition evaluation. */
500static const char condition_evaluation_auto[] = "auto";
501static const char condition_evaluation_host[] = "host";
502static const char condition_evaluation_target[] = "target";
503static const char *const condition_evaluation_enums[] = {
504 condition_evaluation_auto,
505 condition_evaluation_host,
506 condition_evaluation_target,
507 NULL
508};
509
510/* Global that holds the current mode for breakpoint condition evaluation. */
511static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
512
513/* Global that we use to display information to the user (gets its value from
514 condition_evaluation_mode_1. */
515static const char *condition_evaluation_mode = condition_evaluation_auto;
516
517/* Translate a condition evaluation mode MODE into either "host"
518 or "target". This is used mostly to translate from "auto" to the
519 real setting that is being used. It returns the translated
520 evaluation mode. */
521
522static const char *
523translate_condition_evaluation_mode (const char *mode)
524{
525 if (mode == condition_evaluation_auto)
526 {
527 if (target_supports_evaluation_of_breakpoint_conditions ())
528 return condition_evaluation_target;
529 else
530 return condition_evaluation_host;
531 }
532 else
533 return mode;
534}
535
536/* Discovers what condition_evaluation_auto translates to. */
537
538static const char *
539breakpoint_condition_evaluation_mode (void)
540{
541 return translate_condition_evaluation_mode (condition_evaluation_mode);
542}
543
544/* Return true if GDB should evaluate breakpoint conditions or false
545 otherwise. */
546
547static int
548gdb_evaluates_breakpoint_condition_p (void)
549{
550 const char *mode = breakpoint_condition_evaluation_mode ();
551
552 return (mode == condition_evaluation_host);
553}
554
555void _initialize_breakpoint (void);
556
557/* Are we executing breakpoint commands? */
558static int executing_breakpoint_commands;
559
560/* Are overlay event breakpoints enabled? */
561static int overlay_events_enabled;
562
563/* See description in breakpoint.h. */
564int target_exact_watchpoints = 0;
565
566/* Walk the following statement or block through all breakpoints.
567 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
568 current breakpoint. */
569
570#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
571
572#define ALL_BREAKPOINTS_SAFE(B,TMP) \
573 for (B = breakpoint_chain; \
574 B ? (TMP=B->next, 1): 0; \
575 B = TMP)
576
577/* Similar iterator for the low-level breakpoints. SAFE variant is
578 not provided so update_global_location_list must not be called
579 while executing the block of ALL_BP_LOCATIONS. */
580
581#define ALL_BP_LOCATIONS(B,BP_TMP) \
582 for (BP_TMP = bp_location; \
583 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
584 BP_TMP++)
585
586/* Iterates through locations with address ADDRESS for the currently selected
587 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
588 to where the loop should start from.
589 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
590 appropriate location to start with. */
591
592#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
593 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
594 BP_LOCP_TMP = BP_LOCP_START; \
595 BP_LOCP_START \
596 && (BP_LOCP_TMP < bp_location + bp_location_count \
597 && (*BP_LOCP_TMP)->address == ADDRESS); \
598 BP_LOCP_TMP++)
599
600/* Iterator for tracepoints only. */
601
602#define ALL_TRACEPOINTS(B) \
603 for (B = breakpoint_chain; B; B = B->next) \
604 if (is_tracepoint (B))
605
606/* Chains of all breakpoints defined. */
607
608struct breakpoint *breakpoint_chain;
609
610/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
611
612static struct bp_location **bp_location;
613
614/* Number of elements of BP_LOCATION. */
615
616static unsigned bp_location_count;
617
618/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
619 ADDRESS for the current elements of BP_LOCATION which get a valid
620 result from bp_location_has_shadow. You can use it for roughly
621 limiting the subrange of BP_LOCATION to scan for shadow bytes for
622 an address you need to read. */
623
624static CORE_ADDR bp_location_placed_address_before_address_max;
625
626/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
627 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
628 BP_LOCATION which get a valid result from bp_location_has_shadow.
629 You can use it for roughly limiting the subrange of BP_LOCATION to
630 scan for shadow bytes for an address you need to read. */
631
632static CORE_ADDR bp_location_shadow_len_after_address_max;
633
634/* The locations that no longer correspond to any breakpoint, unlinked
635 from bp_location array, but for which a hit may still be reported
636 by a target. */
637VEC(bp_location_p) *moribund_locations = NULL;
638
639/* Number of last breakpoint made. */
640
641static int breakpoint_count;
642
643/* The value of `breakpoint_count' before the last command that
644 created breakpoints. If the last (break-like) command created more
645 than one breakpoint, then the difference between BREAKPOINT_COUNT
646 and PREV_BREAKPOINT_COUNT is more than one. */
647static int prev_breakpoint_count;
648
649/* Number of last tracepoint made. */
650
651static int tracepoint_count;
652
653static struct cmd_list_element *breakpoint_set_cmdlist;
654static struct cmd_list_element *breakpoint_show_cmdlist;
655struct cmd_list_element *save_cmdlist;
656
657/* See declaration at breakpoint.h. */
658
659struct breakpoint *
660breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
661 void *user_data)
662{
663 struct breakpoint *b = NULL;
664
665 ALL_BREAKPOINTS (b)
666 {
667 if (func (b, user_data) != 0)
668 break;
669 }
670
671 return b;
672}
673
674/* Return whether a breakpoint is an active enabled breakpoint. */
675static int
676breakpoint_enabled (struct breakpoint *b)
677{
678 return (b->enable_state == bp_enabled);
679}
680
681/* Set breakpoint count to NUM. */
682
683static void
684set_breakpoint_count (int num)
685{
686 prev_breakpoint_count = breakpoint_count;
687 breakpoint_count = num;
688 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
689}
690
691/* Used by `start_rbreak_breakpoints' below, to record the current
692 breakpoint count before "rbreak" creates any breakpoint. */
693static int rbreak_start_breakpoint_count;
694
695/* Called at the start an "rbreak" command to record the first
696 breakpoint made. */
697
698void
699start_rbreak_breakpoints (void)
700{
701 rbreak_start_breakpoint_count = breakpoint_count;
702}
703
704/* Called at the end of an "rbreak" command to record the last
705 breakpoint made. */
706
707void
708end_rbreak_breakpoints (void)
709{
710 prev_breakpoint_count = rbreak_start_breakpoint_count;
711}
712
713/* Used in run_command to zero the hit count when a new run starts. */
714
715void
716clear_breakpoint_hit_counts (void)
717{
718 struct breakpoint *b;
719
720 ALL_BREAKPOINTS (b)
721 b->hit_count = 0;
722}
723
724/* Allocate a new counted_command_line with reference count of 1.
725 The new structure owns COMMANDS. */
726
727static struct counted_command_line *
728alloc_counted_command_line (struct command_line *commands)
729{
730 struct counted_command_line *result = XNEW (struct counted_command_line);
731
732 result->refc = 1;
733 result->commands = commands;
734
735 return result;
736}
737
738/* Increment reference count. This does nothing if CMD is NULL. */
739
740static void
741incref_counted_command_line (struct counted_command_line *cmd)
742{
743 if (cmd)
744 ++cmd->refc;
745}
746
747/* Decrement reference count. If the reference count reaches 0,
748 destroy the counted_command_line. Sets *CMDP to NULL. This does
749 nothing if *CMDP is NULL. */
750
751static void
752decref_counted_command_line (struct counted_command_line **cmdp)
753{
754 if (*cmdp)
755 {
756 if (--(*cmdp)->refc == 0)
757 {
758 free_command_lines (&(*cmdp)->commands);
759 xfree (*cmdp);
760 }
761 *cmdp = NULL;
762 }
763}
764
765/* A cleanup function that calls decref_counted_command_line. */
766
767static void
768do_cleanup_counted_command_line (void *arg)
769{
770 decref_counted_command_line ((struct counted_command_line **) arg);
771}
772
773/* Create a cleanup that calls decref_counted_command_line on the
774 argument. */
775
776static struct cleanup *
777make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
778{
779 return make_cleanup (do_cleanup_counted_command_line, cmdp);
780}
781
782\f
783/* Return the breakpoint with the specified number, or NULL
784 if the number does not refer to an existing breakpoint. */
785
786struct breakpoint *
787get_breakpoint (int num)
788{
789 struct breakpoint *b;
790
791 ALL_BREAKPOINTS (b)
792 if (b->number == num)
793 return b;
794
795 return NULL;
796}
797
798\f
799
800/* Mark locations as "conditions have changed" in case the target supports
801 evaluating conditions on its side. */
802
803static void
804mark_breakpoint_modified (struct breakpoint *b)
805{
806 struct bp_location *loc;
807
808 /* This is only meaningful if the target is
809 evaluating conditions and if the user has
810 opted for condition evaluation on the target's
811 side. */
812 if (gdb_evaluates_breakpoint_condition_p ()
813 || !target_supports_evaluation_of_breakpoint_conditions ())
814 return;
815
816 if (!is_breakpoint (b))
817 return;
818
819 for (loc = b->loc; loc; loc = loc->next)
820 loc->condition_changed = condition_modified;
821}
822
823/* Mark location as "conditions have changed" in case the target supports
824 evaluating conditions on its side. */
825
826static void
827mark_breakpoint_location_modified (struct bp_location *loc)
828{
829 /* This is only meaningful if the target is
830 evaluating conditions and if the user has
831 opted for condition evaluation on the target's
832 side. */
833 if (gdb_evaluates_breakpoint_condition_p ()
834 || !target_supports_evaluation_of_breakpoint_conditions ())
835
836 return;
837
838 if (!is_breakpoint (loc->owner))
839 return;
840
841 loc->condition_changed = condition_modified;
842}
843
844/* Sets the condition-evaluation mode using the static global
845 condition_evaluation_mode. */
846
847static void
848set_condition_evaluation_mode (char *args, int from_tty,
849 struct cmd_list_element *c)
850{
851 const char *old_mode, *new_mode;
852
853 if ((condition_evaluation_mode_1 == condition_evaluation_target)
854 && !target_supports_evaluation_of_breakpoint_conditions ())
855 {
856 condition_evaluation_mode_1 = condition_evaluation_mode;
857 warning (_("Target does not support breakpoint condition evaluation.\n"
858 "Using host evaluation mode instead."));
859 return;
860 }
861
862 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
863 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
864
865 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
866 settings was "auto". */
867 condition_evaluation_mode = condition_evaluation_mode_1;
868
869 /* Only update the mode if the user picked a different one. */
870 if (new_mode != old_mode)
871 {
872 struct bp_location *loc, **loc_tmp;
873 /* If the user switched to a different evaluation mode, we
874 need to synch the changes with the target as follows:
875
876 "host" -> "target": Send all (valid) conditions to the target.
877 "target" -> "host": Remove all the conditions from the target.
878 */
879
880 if (new_mode == condition_evaluation_target)
881 {
882 /* Mark everything modified and synch conditions with the
883 target. */
884 ALL_BP_LOCATIONS (loc, loc_tmp)
885 mark_breakpoint_location_modified (loc);
886 }
887 else
888 {
889 /* Manually mark non-duplicate locations to synch conditions
890 with the target. We do this to remove all the conditions the
891 target knows about. */
892 ALL_BP_LOCATIONS (loc, loc_tmp)
893 if (is_breakpoint (loc->owner) && loc->inserted)
894 loc->needs_update = 1;
895 }
896
897 /* Do the update. */
898 update_global_location_list (UGLL_MAY_INSERT);
899 }
900
901 return;
902}
903
904/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
905 what "auto" is translating to. */
906
907static void
908show_condition_evaluation_mode (struct ui_file *file, int from_tty,
909 struct cmd_list_element *c, const char *value)
910{
911 if (condition_evaluation_mode == condition_evaluation_auto)
912 fprintf_filtered (file,
913 _("Breakpoint condition evaluation "
914 "mode is %s (currently %s).\n"),
915 value,
916 breakpoint_condition_evaluation_mode ());
917 else
918 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
919 value);
920}
921
922/* A comparison function for bp_location AP and BP that is used by
923 bsearch. This comparison function only cares about addresses, unlike
924 the more general bp_location_compare function. */
925
926static int
927bp_location_compare_addrs (const void *ap, const void *bp)
928{
929 const struct bp_location *a = *(const struct bp_location **) ap;
930 const struct bp_location *b = *(const struct bp_location **) bp;
931
932 if (a->address == b->address)
933 return 0;
934 else
935 return ((a->address > b->address) - (a->address < b->address));
936}
937
938/* Helper function to skip all bp_locations with addresses
939 less than ADDRESS. It returns the first bp_location that
940 is greater than or equal to ADDRESS. If none is found, just
941 return NULL. */
942
943static struct bp_location **
944get_first_locp_gte_addr (CORE_ADDR address)
945{
946 struct bp_location dummy_loc;
947 struct bp_location *dummy_locp = &dummy_loc;
948 struct bp_location **locp_found = NULL;
949
950 /* Initialize the dummy location's address field. */
951 memset (&dummy_loc, 0, sizeof (struct bp_location));
952 dummy_loc.address = address;
953
954 /* Find a close match to the first location at ADDRESS. */
955 locp_found = ((struct bp_location **)
956 bsearch (&dummy_locp, bp_location, bp_location_count,
957 sizeof (struct bp_location **),
958 bp_location_compare_addrs));
959
960 /* Nothing was found, nothing left to do. */
961 if (locp_found == NULL)
962 return NULL;
963
964 /* We may have found a location that is at ADDRESS but is not the first in the
965 location's list. Go backwards (if possible) and locate the first one. */
966 while ((locp_found - 1) >= bp_location
967 && (*(locp_found - 1))->address == address)
968 locp_found--;
969
970 return locp_found;
971}
972
973void
974set_breakpoint_condition (struct breakpoint *b, const char *exp,
975 int from_tty)
976{
977 xfree (b->cond_string);
978 b->cond_string = NULL;
979
980 if (is_watchpoint (b))
981 {
982 struct watchpoint *w = (struct watchpoint *) b;
983
984 xfree (w->cond_exp);
985 w->cond_exp = NULL;
986 }
987 else
988 {
989 struct bp_location *loc;
990
991 for (loc = b->loc; loc; loc = loc->next)
992 {
993 xfree (loc->cond);
994 loc->cond = NULL;
995
996 /* No need to free the condition agent expression
997 bytecode (if we have one). We will handle this
998 when we go through update_global_location_list. */
999 }
1000 }
1001
1002 if (*exp == 0)
1003 {
1004 if (from_tty)
1005 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1006 }
1007 else
1008 {
1009 const char *arg = exp;
1010
1011 /* I don't know if it matters whether this is the string the user
1012 typed in or the decompiled expression. */
1013 b->cond_string = xstrdup (arg);
1014 b->condition_not_parsed = 0;
1015
1016 if (is_watchpoint (b))
1017 {
1018 struct watchpoint *w = (struct watchpoint *) b;
1019
1020 innermost_block = NULL;
1021 arg = exp;
1022 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1023 if (*arg)
1024 error (_("Junk at end of expression"));
1025 w->cond_exp_valid_block = innermost_block;
1026 }
1027 else
1028 {
1029 struct bp_location *loc;
1030
1031 for (loc = b->loc; loc; loc = loc->next)
1032 {
1033 arg = exp;
1034 loc->cond =
1035 parse_exp_1 (&arg, loc->address,
1036 block_for_pc (loc->address), 0);
1037 if (*arg)
1038 error (_("Junk at end of expression"));
1039 }
1040 }
1041 }
1042 mark_breakpoint_modified (b);
1043
1044 observer_notify_breakpoint_modified (b);
1045}
1046
1047/* Completion for the "condition" command. */
1048
1049static VEC (char_ptr) *
1050condition_completer (struct cmd_list_element *cmd,
1051 const char *text, const char *word)
1052{
1053 const char *space;
1054
1055 text = skip_spaces_const (text);
1056 space = skip_to_space_const (text);
1057 if (*space == '\0')
1058 {
1059 int len;
1060 struct breakpoint *b;
1061 VEC (char_ptr) *result = NULL;
1062
1063 if (text[0] == '$')
1064 {
1065 /* We don't support completion of history indices. */
1066 if (isdigit (text[1]))
1067 return NULL;
1068 return complete_internalvar (&text[1]);
1069 }
1070
1071 /* We're completing the breakpoint number. */
1072 len = strlen (text);
1073
1074 ALL_BREAKPOINTS (b)
1075 {
1076 char number[50];
1077
1078 xsnprintf (number, sizeof (number), "%d", b->number);
1079
1080 if (strncmp (number, text, len) == 0)
1081 VEC_safe_push (char_ptr, result, xstrdup (number));
1082 }
1083
1084 return result;
1085 }
1086
1087 /* We're completing the expression part. */
1088 text = skip_spaces_const (space);
1089 return expression_completer (cmd, text, word);
1090}
1091
1092/* condition N EXP -- set break condition of breakpoint N to EXP. */
1093
1094static void
1095condition_command (char *arg, int from_tty)
1096{
1097 struct breakpoint *b;
1098 char *p;
1099 int bnum;
1100
1101 if (arg == 0)
1102 error_no_arg (_("breakpoint number"));
1103
1104 p = arg;
1105 bnum = get_number (&p);
1106 if (bnum == 0)
1107 error (_("Bad breakpoint argument: '%s'"), arg);
1108
1109 ALL_BREAKPOINTS (b)
1110 if (b->number == bnum)
1111 {
1112 /* Check if this breakpoint has a "stop" method implemented in an
1113 extension language. This method and conditions entered into GDB
1114 from the CLI are mutually exclusive. */
1115 const struct extension_language_defn *extlang
1116 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1117
1118 if (extlang != NULL)
1119 {
1120 error (_("Only one stop condition allowed. There is currently"
1121 " a %s stop condition defined for this breakpoint."),
1122 ext_lang_capitalized_name (extlang));
1123 }
1124 set_breakpoint_condition (b, p, from_tty);
1125
1126 if (is_breakpoint (b))
1127 update_global_location_list (UGLL_MAY_INSERT);
1128
1129 return;
1130 }
1131
1132 error (_("No breakpoint number %d."), bnum);
1133}
1134
1135/* Check that COMMAND do not contain commands that are suitable
1136 only for tracepoints and not suitable for ordinary breakpoints.
1137 Throw if any such commands is found. */
1138
1139static void
1140check_no_tracepoint_commands (struct command_line *commands)
1141{
1142 struct command_line *c;
1143
1144 for (c = commands; c; c = c->next)
1145 {
1146 int i;
1147
1148 if (c->control_type == while_stepping_control)
1149 error (_("The 'while-stepping' command can "
1150 "only be used for tracepoints"));
1151
1152 for (i = 0; i < c->body_count; ++i)
1153 check_no_tracepoint_commands ((c->body_list)[i]);
1154
1155 /* Not that command parsing removes leading whitespace and comment
1156 lines and also empty lines. So, we only need to check for
1157 command directly. */
1158 if (strstr (c->line, "collect ") == c->line)
1159 error (_("The 'collect' command can only be used for tracepoints"));
1160
1161 if (strstr (c->line, "teval ") == c->line)
1162 error (_("The 'teval' command can only be used for tracepoints"));
1163 }
1164}
1165
1166/* Encapsulate tests for different types of tracepoints. */
1167
1168static int
1169is_tracepoint_type (enum bptype type)
1170{
1171 return (type == bp_tracepoint
1172 || type == bp_fast_tracepoint
1173 || type == bp_static_tracepoint);
1174}
1175
1176int
1177is_tracepoint (const struct breakpoint *b)
1178{
1179 return is_tracepoint_type (b->type);
1180}
1181
1182/* A helper function that validates that COMMANDS are valid for a
1183 breakpoint. This function will throw an exception if a problem is
1184 found. */
1185
1186static void
1187validate_commands_for_breakpoint (struct breakpoint *b,
1188 struct command_line *commands)
1189{
1190 if (is_tracepoint (b))
1191 {
1192 struct tracepoint *t = (struct tracepoint *) b;
1193 struct command_line *c;
1194 struct command_line *while_stepping = 0;
1195
1196 /* Reset the while-stepping step count. The previous commands
1197 might have included a while-stepping action, while the new
1198 ones might not. */
1199 t->step_count = 0;
1200
1201 /* We need to verify that each top-level element of commands is
1202 valid for tracepoints, that there's at most one
1203 while-stepping element, and that the while-stepping's body
1204 has valid tracing commands excluding nested while-stepping.
1205 We also need to validate the tracepoint action line in the
1206 context of the tracepoint --- validate_actionline actually
1207 has side effects, like setting the tracepoint's
1208 while-stepping STEP_COUNT, in addition to checking if the
1209 collect/teval actions parse and make sense in the
1210 tracepoint's context. */
1211 for (c = commands; c; c = c->next)
1212 {
1213 if (c->control_type == while_stepping_control)
1214 {
1215 if (b->type == bp_fast_tracepoint)
1216 error (_("The 'while-stepping' command "
1217 "cannot be used for fast tracepoint"));
1218 else if (b->type == bp_static_tracepoint)
1219 error (_("The 'while-stepping' command "
1220 "cannot be used for static tracepoint"));
1221
1222 if (while_stepping)
1223 error (_("The 'while-stepping' command "
1224 "can be used only once"));
1225 else
1226 while_stepping = c;
1227 }
1228
1229 validate_actionline (c->line, b);
1230 }
1231 if (while_stepping)
1232 {
1233 struct command_line *c2;
1234
1235 gdb_assert (while_stepping->body_count == 1);
1236 c2 = while_stepping->body_list[0];
1237 for (; c2; c2 = c2->next)
1238 {
1239 if (c2->control_type == while_stepping_control)
1240 error (_("The 'while-stepping' command cannot be nested"));
1241 }
1242 }
1243 }
1244 else
1245 {
1246 check_no_tracepoint_commands (commands);
1247 }
1248}
1249
1250/* Return a vector of all the static tracepoints set at ADDR. The
1251 caller is responsible for releasing the vector. */
1252
1253VEC(breakpoint_p) *
1254static_tracepoints_here (CORE_ADDR addr)
1255{
1256 struct breakpoint *b;
1257 VEC(breakpoint_p) *found = 0;
1258 struct bp_location *loc;
1259
1260 ALL_BREAKPOINTS (b)
1261 if (b->type == bp_static_tracepoint)
1262 {
1263 for (loc = b->loc; loc; loc = loc->next)
1264 if (loc->address == addr)
1265 VEC_safe_push(breakpoint_p, found, b);
1266 }
1267
1268 return found;
1269}
1270
1271/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1272 validate that only allowed commands are included. */
1273
1274void
1275breakpoint_set_commands (struct breakpoint *b,
1276 struct command_line *commands)
1277{
1278 validate_commands_for_breakpoint (b, commands);
1279
1280 decref_counted_command_line (&b->commands);
1281 b->commands = alloc_counted_command_line (commands);
1282 observer_notify_breakpoint_modified (b);
1283}
1284
1285/* Set the internal `silent' flag on the breakpoint. Note that this
1286 is not the same as the "silent" that may appear in the breakpoint's
1287 commands. */
1288
1289void
1290breakpoint_set_silent (struct breakpoint *b, int silent)
1291{
1292 int old_silent = b->silent;
1293
1294 b->silent = silent;
1295 if (old_silent != silent)
1296 observer_notify_breakpoint_modified (b);
1297}
1298
1299/* Set the thread for this breakpoint. If THREAD is -1, make the
1300 breakpoint work for any thread. */
1301
1302void
1303breakpoint_set_thread (struct breakpoint *b, int thread)
1304{
1305 int old_thread = b->thread;
1306
1307 b->thread = thread;
1308 if (old_thread != thread)
1309 observer_notify_breakpoint_modified (b);
1310}
1311
1312/* Set the task for this breakpoint. If TASK is 0, make the
1313 breakpoint work for any task. */
1314
1315void
1316breakpoint_set_task (struct breakpoint *b, int task)
1317{
1318 int old_task = b->task;
1319
1320 b->task = task;
1321 if (old_task != task)
1322 observer_notify_breakpoint_modified (b);
1323}
1324
1325void
1326check_tracepoint_command (char *line, void *closure)
1327{
1328 struct breakpoint *b = (struct breakpoint *) closure;
1329
1330 validate_actionline (line, b);
1331}
1332
1333/* A structure used to pass information through
1334 map_breakpoint_numbers. */
1335
1336struct commands_info
1337{
1338 /* True if the command was typed at a tty. */
1339 int from_tty;
1340
1341 /* The breakpoint range spec. */
1342 const char *arg;
1343
1344 /* Non-NULL if the body of the commands are being read from this
1345 already-parsed command. */
1346 struct command_line *control;
1347
1348 /* The command lines read from the user, or NULL if they have not
1349 yet been read. */
1350 struct counted_command_line *cmd;
1351};
1352
1353/* A callback for map_breakpoint_numbers that sets the commands for
1354 commands_command. */
1355
1356static void
1357do_map_commands_command (struct breakpoint *b, void *data)
1358{
1359 struct commands_info *info = (struct commands_info *) data;
1360
1361 if (info->cmd == NULL)
1362 {
1363 struct command_line *l;
1364
1365 if (info->control != NULL)
1366 l = copy_command_lines (info->control->body_list[0]);
1367 else
1368 {
1369 struct cleanup *old_chain;
1370 char *str;
1371
1372 str = xstrprintf (_("Type commands for breakpoint(s) "
1373 "%s, one per line."),
1374 info->arg);
1375
1376 old_chain = make_cleanup (xfree, str);
1377
1378 l = read_command_lines (str,
1379 info->from_tty, 1,
1380 (is_tracepoint (b)
1381 ? check_tracepoint_command : 0),
1382 b);
1383
1384 do_cleanups (old_chain);
1385 }
1386
1387 info->cmd = alloc_counted_command_line (l);
1388 }
1389
1390 /* If a breakpoint was on the list more than once, we don't need to
1391 do anything. */
1392 if (b->commands != info->cmd)
1393 {
1394 validate_commands_for_breakpoint (b, info->cmd->commands);
1395 incref_counted_command_line (info->cmd);
1396 decref_counted_command_line (&b->commands);
1397 b->commands = info->cmd;
1398 observer_notify_breakpoint_modified (b);
1399 }
1400}
1401
1402static void
1403commands_command_1 (const char *arg, int from_tty,
1404 struct command_line *control)
1405{
1406 struct cleanup *cleanups;
1407 struct commands_info info;
1408
1409 info.from_tty = from_tty;
1410 info.control = control;
1411 info.cmd = NULL;
1412 /* If we read command lines from the user, then `info' will hold an
1413 extra reference to the commands that we must clean up. */
1414 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1415
1416 std::string new_arg;
1417
1418 if (arg == NULL || !*arg)
1419 {
1420 if (breakpoint_count - prev_breakpoint_count > 1)
1421 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1422 breakpoint_count);
1423 else if (breakpoint_count > 0)
1424 new_arg = string_printf ("%d", breakpoint_count);
1425 }
1426 else
1427 new_arg = arg;
1428
1429 info.arg = new_arg.c_str ();
1430
1431 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1432
1433 if (info.cmd == NULL)
1434 error (_("No breakpoints specified."));
1435
1436 do_cleanups (cleanups);
1437}
1438
1439static void
1440commands_command (char *arg, int from_tty)
1441{
1442 commands_command_1 (arg, from_tty, NULL);
1443}
1444
1445/* Like commands_command, but instead of reading the commands from
1446 input stream, takes them from an already parsed command structure.
1447
1448 This is used by cli-script.c to DTRT with breakpoint commands
1449 that are part of if and while bodies. */
1450enum command_control_type
1451commands_from_control_command (const char *arg, struct command_line *cmd)
1452{
1453 commands_command_1 (arg, 0, cmd);
1454 return simple_control;
1455}
1456
1457/* Return non-zero if BL->TARGET_INFO contains valid information. */
1458
1459static int
1460bp_location_has_shadow (struct bp_location *bl)
1461{
1462 if (bl->loc_type != bp_loc_software_breakpoint)
1463 return 0;
1464 if (!bl->inserted)
1465 return 0;
1466 if (bl->target_info.shadow_len == 0)
1467 /* BL isn't valid, or doesn't shadow memory. */
1468 return 0;
1469 return 1;
1470}
1471
1472/* Update BUF, which is LEN bytes read from the target address
1473 MEMADDR, by replacing a memory breakpoint with its shadowed
1474 contents.
1475
1476 If READBUF is not NULL, this buffer must not overlap with the of
1477 the breakpoint location's shadow_contents buffer. Otherwise, a
1478 failed assertion internal error will be raised. */
1479
1480static void
1481one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1482 const gdb_byte *writebuf_org,
1483 ULONGEST memaddr, LONGEST len,
1484 struct bp_target_info *target_info,
1485 struct gdbarch *gdbarch)
1486{
1487 /* Now do full processing of the found relevant range of elements. */
1488 CORE_ADDR bp_addr = 0;
1489 int bp_size = 0;
1490 int bptoffset = 0;
1491
1492 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1493 current_program_space->aspace, 0))
1494 {
1495 /* The breakpoint is inserted in a different address space. */
1496 return;
1497 }
1498
1499 /* Addresses and length of the part of the breakpoint that
1500 we need to copy. */
1501 bp_addr = target_info->placed_address;
1502 bp_size = target_info->shadow_len;
1503
1504 if (bp_addr + bp_size <= memaddr)
1505 {
1506 /* The breakpoint is entirely before the chunk of memory we are
1507 reading. */
1508 return;
1509 }
1510
1511 if (bp_addr >= memaddr + len)
1512 {
1513 /* The breakpoint is entirely after the chunk of memory we are
1514 reading. */
1515 return;
1516 }
1517
1518 /* Offset within shadow_contents. */
1519 if (bp_addr < memaddr)
1520 {
1521 /* Only copy the second part of the breakpoint. */
1522 bp_size -= memaddr - bp_addr;
1523 bptoffset = memaddr - bp_addr;
1524 bp_addr = memaddr;
1525 }
1526
1527 if (bp_addr + bp_size > memaddr + len)
1528 {
1529 /* Only copy the first part of the breakpoint. */
1530 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1531 }
1532
1533 if (readbuf != NULL)
1534 {
1535 /* Verify that the readbuf buffer does not overlap with the
1536 shadow_contents buffer. */
1537 gdb_assert (target_info->shadow_contents >= readbuf + len
1538 || readbuf >= (target_info->shadow_contents
1539 + target_info->shadow_len));
1540
1541 /* Update the read buffer with this inserted breakpoint's
1542 shadow. */
1543 memcpy (readbuf + bp_addr - memaddr,
1544 target_info->shadow_contents + bptoffset, bp_size);
1545 }
1546 else
1547 {
1548 const unsigned char *bp;
1549 CORE_ADDR addr = target_info->reqstd_address;
1550 int placed_size;
1551
1552 /* Update the shadow with what we want to write to memory. */
1553 memcpy (target_info->shadow_contents + bptoffset,
1554 writebuf_org + bp_addr - memaddr, bp_size);
1555
1556 /* Determine appropriate breakpoint contents and size for this
1557 address. */
1558 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1559
1560 /* Update the final write buffer with this inserted
1561 breakpoint's INSN. */
1562 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1563 }
1564}
1565
1566/* Update BUF, which is LEN bytes read from the target address MEMADDR,
1567 by replacing any memory breakpoints with their shadowed contents.
1568
1569 If READBUF is not NULL, this buffer must not overlap with any of
1570 the breakpoint location's shadow_contents buffers. Otherwise,
1571 a failed assertion internal error will be raised.
1572
1573 The range of shadowed area by each bp_location is:
1574 bl->address - bp_location_placed_address_before_address_max
1575 up to bl->address + bp_location_shadow_len_after_address_max
1576 The range we were requested to resolve shadows for is:
1577 memaddr ... memaddr + len
1578 Thus the safe cutoff boundaries for performance optimization are
1579 memaddr + len <= (bl->address
1580 - bp_location_placed_address_before_address_max)
1581 and:
1582 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1583
1584void
1585breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1586 const gdb_byte *writebuf_org,
1587 ULONGEST memaddr, LONGEST len)
1588{
1589 /* Left boundary, right boundary and median element of our binary
1590 search. */
1591 unsigned bc_l, bc_r, bc;
1592
1593 /* Find BC_L which is a leftmost element which may affect BUF
1594 content. It is safe to report lower value but a failure to
1595 report higher one. */
1596
1597 bc_l = 0;
1598 bc_r = bp_location_count;
1599 while (bc_l + 1 < bc_r)
1600 {
1601 struct bp_location *bl;
1602
1603 bc = (bc_l + bc_r) / 2;
1604 bl = bp_location[bc];
1605
1606 /* Check first BL->ADDRESS will not overflow due to the added
1607 constant. Then advance the left boundary only if we are sure
1608 the BC element can in no way affect the BUF content (MEMADDR
1609 to MEMADDR + LEN range).
1610
1611 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1612 offset so that we cannot miss a breakpoint with its shadow
1613 range tail still reaching MEMADDR. */
1614
1615 if ((bl->address + bp_location_shadow_len_after_address_max
1616 >= bl->address)
1617 && (bl->address + bp_location_shadow_len_after_address_max
1618 <= memaddr))
1619 bc_l = bc;
1620 else
1621 bc_r = bc;
1622 }
1623
1624 /* Due to the binary search above, we need to make sure we pick the
1625 first location that's at BC_L's address. E.g., if there are
1626 multiple locations at the same address, BC_L may end up pointing
1627 at a duplicate location, and miss the "master"/"inserted"
1628 location. Say, given locations L1, L2 and L3 at addresses A and
1629 B:
1630
1631 L1@A, L2@A, L3@B, ...
1632
1633 BC_L could end up pointing at location L2, while the "master"
1634 location could be L1. Since the `loc->inserted' flag is only set
1635 on "master" locations, we'd forget to restore the shadow of L1
1636 and L2. */
1637 while (bc_l > 0
1638 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1639 bc_l--;
1640
1641 /* Now do full processing of the found relevant range of elements. */
1642
1643 for (bc = bc_l; bc < bp_location_count; bc++)
1644 {
1645 struct bp_location *bl = bp_location[bc];
1646
1647 /* bp_location array has BL->OWNER always non-NULL. */
1648 if (bl->owner->type == bp_none)
1649 warning (_("reading through apparently deleted breakpoint #%d?"),
1650 bl->owner->number);
1651
1652 /* Performance optimization: any further element can no longer affect BUF
1653 content. */
1654
1655 if (bl->address >= bp_location_placed_address_before_address_max
1656 && memaddr + len <= (bl->address
1657 - bp_location_placed_address_before_address_max))
1658 break;
1659
1660 if (!bp_location_has_shadow (bl))
1661 continue;
1662
1663 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1664 memaddr, len, &bl->target_info, bl->gdbarch);
1665 }
1666}
1667
1668\f
1669
1670/* Return true if BPT is either a software breakpoint or a hardware
1671 breakpoint. */
1672
1673int
1674is_breakpoint (const struct breakpoint *bpt)
1675{
1676 return (bpt->type == bp_breakpoint
1677 || bpt->type == bp_hardware_breakpoint
1678 || bpt->type == bp_dprintf);
1679}
1680
1681/* Return true if BPT is of any hardware watchpoint kind. */
1682
1683static int
1684is_hardware_watchpoint (const struct breakpoint *bpt)
1685{
1686 return (bpt->type == bp_hardware_watchpoint
1687 || bpt->type == bp_read_watchpoint
1688 || bpt->type == bp_access_watchpoint);
1689}
1690
1691/* Return true if BPT is of any watchpoint kind, hardware or
1692 software. */
1693
1694int
1695is_watchpoint (const struct breakpoint *bpt)
1696{
1697 return (is_hardware_watchpoint (bpt)
1698 || bpt->type == bp_watchpoint);
1699}
1700
1701/* Returns true if the current thread and its running state are safe
1702 to evaluate or update watchpoint B. Watchpoints on local
1703 expressions need to be evaluated in the context of the thread that
1704 was current when the watchpoint was created, and, that thread needs
1705 to be stopped to be able to select the correct frame context.
1706 Watchpoints on global expressions can be evaluated on any thread,
1707 and in any state. It is presently left to the target allowing
1708 memory accesses when threads are running. */
1709
1710static int
1711watchpoint_in_thread_scope (struct watchpoint *b)
1712{
1713 return (b->base.pspace == current_program_space
1714 && (ptid_equal (b->watchpoint_thread, null_ptid)
1715 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1716 && !is_executing (inferior_ptid))));
1717}
1718
1719/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1720 associated bp_watchpoint_scope breakpoint. */
1721
1722static void
1723watchpoint_del_at_next_stop (struct watchpoint *w)
1724{
1725 struct breakpoint *b = &w->base;
1726
1727 if (b->related_breakpoint != b)
1728 {
1729 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1730 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1731 b->related_breakpoint->disposition = disp_del_at_next_stop;
1732 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1733 b->related_breakpoint = b;
1734 }
1735 b->disposition = disp_del_at_next_stop;
1736}
1737
1738/* Extract a bitfield value from value VAL using the bit parameters contained in
1739 watchpoint W. */
1740
1741static struct value *
1742extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1743{
1744 struct value *bit_val;
1745
1746 if (val == NULL)
1747 return NULL;
1748
1749 bit_val = allocate_value (value_type (val));
1750
1751 unpack_value_bitfield (bit_val,
1752 w->val_bitpos,
1753 w->val_bitsize,
1754 value_contents_for_printing (val),
1755 value_offset (val),
1756 val);
1757
1758 return bit_val;
1759}
1760
1761/* Allocate a dummy location and add it to B, which must be a software
1762 watchpoint. This is required because even if a software watchpoint
1763 is not watching any memory, bpstat_stop_status requires a location
1764 to be able to report stops. */
1765
1766static void
1767software_watchpoint_add_no_memory_location (struct breakpoint *b,
1768 struct program_space *pspace)
1769{
1770 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1771
1772 b->loc = allocate_bp_location (b);
1773 b->loc->pspace = pspace;
1774 b->loc->address = -1;
1775 b->loc->length = -1;
1776}
1777
1778/* Returns true if B is a software watchpoint that is not watching any
1779 memory (e.g., "watch $pc"). */
1780
1781static int
1782is_no_memory_software_watchpoint (struct breakpoint *b)
1783{
1784 return (b->type == bp_watchpoint
1785 && b->loc != NULL
1786 && b->loc->next == NULL
1787 && b->loc->address == -1
1788 && b->loc->length == -1);
1789}
1790
1791/* Assuming that B is a watchpoint:
1792 - Reparse watchpoint expression, if REPARSE is non-zero
1793 - Evaluate expression and store the result in B->val
1794 - Evaluate the condition if there is one, and store the result
1795 in b->loc->cond.
1796 - Update the list of values that must be watched in B->loc.
1797
1798 If the watchpoint disposition is disp_del_at_next_stop, then do
1799 nothing. If this is local watchpoint that is out of scope, delete
1800 it.
1801
1802 Even with `set breakpoint always-inserted on' the watchpoints are
1803 removed + inserted on each stop here. Normal breakpoints must
1804 never be removed because they might be missed by a running thread
1805 when debugging in non-stop mode. On the other hand, hardware
1806 watchpoints (is_hardware_watchpoint; processed here) are specific
1807 to each LWP since they are stored in each LWP's hardware debug
1808 registers. Therefore, such LWP must be stopped first in order to
1809 be able to modify its hardware watchpoints.
1810
1811 Hardware watchpoints must be reset exactly once after being
1812 presented to the user. It cannot be done sooner, because it would
1813 reset the data used to present the watchpoint hit to the user. And
1814 it must not be done later because it could display the same single
1815 watchpoint hit during multiple GDB stops. Note that the latter is
1816 relevant only to the hardware watchpoint types bp_read_watchpoint
1817 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1818 not user-visible - its hit is suppressed if the memory content has
1819 not changed.
1820
1821 The following constraints influence the location where we can reset
1822 hardware watchpoints:
1823
1824 * target_stopped_by_watchpoint and target_stopped_data_address are
1825 called several times when GDB stops.
1826
1827 [linux]
1828 * Multiple hardware watchpoints can be hit at the same time,
1829 causing GDB to stop. GDB only presents one hardware watchpoint
1830 hit at a time as the reason for stopping, and all the other hits
1831 are presented later, one after the other, each time the user
1832 requests the execution to be resumed. Execution is not resumed
1833 for the threads still having pending hit event stored in
1834 LWP_INFO->STATUS. While the watchpoint is already removed from
1835 the inferior on the first stop the thread hit event is kept being
1836 reported from its cached value by linux_nat_stopped_data_address
1837 until the real thread resume happens after the watchpoint gets
1838 presented and thus its LWP_INFO->STATUS gets reset.
1839
1840 Therefore the hardware watchpoint hit can get safely reset on the
1841 watchpoint removal from inferior. */
1842
1843static void
1844update_watchpoint (struct watchpoint *b, int reparse)
1845{
1846 int within_current_scope;
1847 struct frame_id saved_frame_id;
1848 int frame_saved;
1849
1850 /* If this is a local watchpoint, we only want to check if the
1851 watchpoint frame is in scope if the current thread is the thread
1852 that was used to create the watchpoint. */
1853 if (!watchpoint_in_thread_scope (b))
1854 return;
1855
1856 if (b->base.disposition == disp_del_at_next_stop)
1857 return;
1858
1859 frame_saved = 0;
1860
1861 /* Determine if the watchpoint is within scope. */
1862 if (b->exp_valid_block == NULL)
1863 within_current_scope = 1;
1864 else
1865 {
1866 struct frame_info *fi = get_current_frame ();
1867 struct gdbarch *frame_arch = get_frame_arch (fi);
1868 CORE_ADDR frame_pc = get_frame_pc (fi);
1869
1870 /* If we're at a point where the stack has been destroyed
1871 (e.g. in a function epilogue), unwinding may not work
1872 properly. Do not attempt to recreate locations at this
1873 point. See similar comments in watchpoint_check. */
1874 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1875 return;
1876
1877 /* Save the current frame's ID so we can restore it after
1878 evaluating the watchpoint expression on its own frame. */
1879 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1880 took a frame parameter, so that we didn't have to change the
1881 selected frame. */
1882 frame_saved = 1;
1883 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1884
1885 fi = frame_find_by_id (b->watchpoint_frame);
1886 within_current_scope = (fi != NULL);
1887 if (within_current_scope)
1888 select_frame (fi);
1889 }
1890
1891 /* We don't free locations. They are stored in the bp_location array
1892 and update_global_location_list will eventually delete them and
1893 remove breakpoints if needed. */
1894 b->base.loc = NULL;
1895
1896 if (within_current_scope && reparse)
1897 {
1898 const char *s;
1899
1900 if (b->exp)
1901 {
1902 xfree (b->exp);
1903 b->exp = NULL;
1904 }
1905 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1906 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1907 /* If the meaning of expression itself changed, the old value is
1908 no longer relevant. We don't want to report a watchpoint hit
1909 to the user when the old value and the new value may actually
1910 be completely different objects. */
1911 value_free (b->val);
1912 b->val = NULL;
1913 b->val_valid = 0;
1914
1915 /* Note that unlike with breakpoints, the watchpoint's condition
1916 expression is stored in the breakpoint object, not in the
1917 locations (re)created below. */
1918 if (b->base.cond_string != NULL)
1919 {
1920 if (b->cond_exp != NULL)
1921 {
1922 xfree (b->cond_exp);
1923 b->cond_exp = NULL;
1924 }
1925
1926 s = b->base.cond_string;
1927 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1928 }
1929 }
1930
1931 /* If we failed to parse the expression, for example because
1932 it refers to a global variable in a not-yet-loaded shared library,
1933 don't try to insert watchpoint. We don't automatically delete
1934 such watchpoint, though, since failure to parse expression
1935 is different from out-of-scope watchpoint. */
1936 if (!target_has_execution)
1937 {
1938 /* Without execution, memory can't change. No use to try and
1939 set watchpoint locations. The watchpoint will be reset when
1940 the target gains execution, through breakpoint_re_set. */
1941 if (!can_use_hw_watchpoints)
1942 {
1943 if (b->base.ops->works_in_software_mode (&b->base))
1944 b->base.type = bp_watchpoint;
1945 else
1946 error (_("Can't set read/access watchpoint when "
1947 "hardware watchpoints are disabled."));
1948 }
1949 }
1950 else if (within_current_scope && b->exp)
1951 {
1952 int pc = 0;
1953 struct value *val_chain, *v, *result, *next;
1954 struct program_space *frame_pspace;
1955
1956 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1957
1958 /* Avoid setting b->val if it's already set. The meaning of
1959 b->val is 'the last value' user saw, and we should update
1960 it only if we reported that last value to user. As it
1961 happens, the code that reports it updates b->val directly.
1962 We don't keep track of the memory value for masked
1963 watchpoints. */
1964 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1965 {
1966 if (b->val_bitsize != 0)
1967 {
1968 v = extract_bitfield_from_watchpoint_value (b, v);
1969 if (v != NULL)
1970 release_value (v);
1971 }
1972 b->val = v;
1973 b->val_valid = 1;
1974 }
1975
1976 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1977
1978 /* Look at each value on the value chain. */
1979 for (v = val_chain; v; v = value_next (v))
1980 {
1981 /* If it's a memory location, and GDB actually needed
1982 its contents to evaluate the expression, then we
1983 must watch it. If the first value returned is
1984 still lazy, that means an error occurred reading it;
1985 watch it anyway in case it becomes readable. */
1986 if (VALUE_LVAL (v) == lval_memory
1987 && (v == val_chain || ! value_lazy (v)))
1988 {
1989 struct type *vtype = check_typedef (value_type (v));
1990
1991 /* We only watch structs and arrays if user asked
1992 for it explicitly, never if they just happen to
1993 appear in the middle of some value chain. */
1994 if (v == result
1995 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1996 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1997 {
1998 CORE_ADDR addr;
1999 enum target_hw_bp_type type;
2000 struct bp_location *loc, **tmp;
2001 int bitpos = 0, bitsize = 0;
2002
2003 if (value_bitsize (v) != 0)
2004 {
2005 /* Extract the bit parameters out from the bitfield
2006 sub-expression. */
2007 bitpos = value_bitpos (v);
2008 bitsize = value_bitsize (v);
2009 }
2010 else if (v == result && b->val_bitsize != 0)
2011 {
2012 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2013 lvalue whose bit parameters are saved in the fields
2014 VAL_BITPOS and VAL_BITSIZE. */
2015 bitpos = b->val_bitpos;
2016 bitsize = b->val_bitsize;
2017 }
2018
2019 addr = value_address (v);
2020 if (bitsize != 0)
2021 {
2022 /* Skip the bytes that don't contain the bitfield. */
2023 addr += bitpos / 8;
2024 }
2025
2026 type = hw_write;
2027 if (b->base.type == bp_read_watchpoint)
2028 type = hw_read;
2029 else if (b->base.type == bp_access_watchpoint)
2030 type = hw_access;
2031
2032 loc = allocate_bp_location (&b->base);
2033 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2034 ;
2035 *tmp = loc;
2036 loc->gdbarch = get_type_arch (value_type (v));
2037
2038 loc->pspace = frame_pspace;
2039 loc->address = addr;
2040
2041 if (bitsize != 0)
2042 {
2043 /* Just cover the bytes that make up the bitfield. */
2044 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2045 }
2046 else
2047 loc->length = TYPE_LENGTH (value_type (v));
2048
2049 loc->watchpoint_type = type;
2050 }
2051 }
2052 }
2053
2054 /* Change the type of breakpoint between hardware assisted or
2055 an ordinary watchpoint depending on the hardware support
2056 and free hardware slots. REPARSE is set when the inferior
2057 is started. */
2058 if (reparse)
2059 {
2060 int reg_cnt;
2061 enum bp_loc_type loc_type;
2062 struct bp_location *bl;
2063
2064 reg_cnt = can_use_hardware_watchpoint (val_chain);
2065
2066 if (reg_cnt)
2067 {
2068 int i, target_resources_ok, other_type_used;
2069 enum bptype type;
2070
2071 /* Use an exact watchpoint when there's only one memory region to be
2072 watched, and only one debug register is needed to watch it. */
2073 b->exact = target_exact_watchpoints && reg_cnt == 1;
2074
2075 /* We need to determine how many resources are already
2076 used for all other hardware watchpoints plus this one
2077 to see if we still have enough resources to also fit
2078 this watchpoint in as well. */
2079
2080 /* If this is a software watchpoint, we try to turn it
2081 to a hardware one -- count resources as if B was of
2082 hardware watchpoint type. */
2083 type = b->base.type;
2084 if (type == bp_watchpoint)
2085 type = bp_hardware_watchpoint;
2086
2087 /* This watchpoint may or may not have been placed on
2088 the list yet at this point (it won't be in the list
2089 if we're trying to create it for the first time,
2090 through watch_command), so always account for it
2091 manually. */
2092
2093 /* Count resources used by all watchpoints except B. */
2094 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2095
2096 /* Add in the resources needed for B. */
2097 i += hw_watchpoint_use_count (&b->base);
2098
2099 target_resources_ok
2100 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2101 if (target_resources_ok <= 0)
2102 {
2103 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2104
2105 if (target_resources_ok == 0 && !sw_mode)
2106 error (_("Target does not support this type of "
2107 "hardware watchpoint."));
2108 else if (target_resources_ok < 0 && !sw_mode)
2109 error (_("There are not enough available hardware "
2110 "resources for this watchpoint."));
2111
2112 /* Downgrade to software watchpoint. */
2113 b->base.type = bp_watchpoint;
2114 }
2115 else
2116 {
2117 /* If this was a software watchpoint, we've just
2118 found we have enough resources to turn it to a
2119 hardware watchpoint. Otherwise, this is a
2120 nop. */
2121 b->base.type = type;
2122 }
2123 }
2124 else if (!b->base.ops->works_in_software_mode (&b->base))
2125 {
2126 if (!can_use_hw_watchpoints)
2127 error (_("Can't set read/access watchpoint when "
2128 "hardware watchpoints are disabled."));
2129 else
2130 error (_("Expression cannot be implemented with "
2131 "read/access watchpoint."));
2132 }
2133 else
2134 b->base.type = bp_watchpoint;
2135
2136 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2137 : bp_loc_hardware_watchpoint);
2138 for (bl = b->base.loc; bl; bl = bl->next)
2139 bl->loc_type = loc_type;
2140 }
2141
2142 for (v = val_chain; v; v = next)
2143 {
2144 next = value_next (v);
2145 if (v != b->val)
2146 value_free (v);
2147 }
2148
2149 /* If a software watchpoint is not watching any memory, then the
2150 above left it without any location set up. But,
2151 bpstat_stop_status requires a location to be able to report
2152 stops, so make sure there's at least a dummy one. */
2153 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2154 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2155 }
2156 else if (!within_current_scope)
2157 {
2158 printf_filtered (_("\
2159Watchpoint %d deleted because the program has left the block\n\
2160in which its expression is valid.\n"),
2161 b->base.number);
2162 watchpoint_del_at_next_stop (b);
2163 }
2164
2165 /* Restore the selected frame. */
2166 if (frame_saved)
2167 select_frame (frame_find_by_id (saved_frame_id));
2168}
2169
2170
2171/* Returns 1 iff breakpoint location should be
2172 inserted in the inferior. We don't differentiate the type of BL's owner
2173 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2174 breakpoint_ops is not defined, because in insert_bp_location,
2175 tracepoint's insert_location will not be called. */
2176static int
2177should_be_inserted (struct bp_location *bl)
2178{
2179 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2180 return 0;
2181
2182 if (bl->owner->disposition == disp_del_at_next_stop)
2183 return 0;
2184
2185 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2186 return 0;
2187
2188 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2189 return 0;
2190
2191 /* This is set for example, when we're attached to the parent of a
2192 vfork, and have detached from the child. The child is running
2193 free, and we expect it to do an exec or exit, at which point the
2194 OS makes the parent schedulable again (and the target reports
2195 that the vfork is done). Until the child is done with the shared
2196 memory region, do not insert breakpoints in the parent, otherwise
2197 the child could still trip on the parent's breakpoints. Since
2198 the parent is blocked anyway, it won't miss any breakpoint. */
2199 if (bl->pspace->breakpoints_not_allowed)
2200 return 0;
2201
2202 /* Don't insert a breakpoint if we're trying to step past its
2203 location, except if the breakpoint is a single-step breakpoint,
2204 and the breakpoint's thread is the thread which is stepping past
2205 a breakpoint. */
2206 if ((bl->loc_type == bp_loc_software_breakpoint
2207 || bl->loc_type == bp_loc_hardware_breakpoint)
2208 && stepping_past_instruction_at (bl->pspace->aspace,
2209 bl->address)
2210 /* The single-step breakpoint may be inserted at the location
2211 we're trying to step if the instruction branches to itself.
2212 However, the instruction won't be executed at all and it may
2213 break the semantics of the instruction, for example, the
2214 instruction is a conditional branch or updates some flags.
2215 We can't fix it unless GDB is able to emulate the instruction
2216 or switch to displaced stepping. */
2217 && !(bl->owner->type == bp_single_step
2218 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2219 {
2220 if (debug_infrun)
2221 {
2222 fprintf_unfiltered (gdb_stdlog,
2223 "infrun: skipping breakpoint: "
2224 "stepping past insn at: %s\n",
2225 paddress (bl->gdbarch, bl->address));
2226 }
2227 return 0;
2228 }
2229
2230 /* Don't insert watchpoints if we're trying to step past the
2231 instruction that triggered one. */
2232 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2233 && stepping_past_nonsteppable_watchpoint ())
2234 {
2235 if (debug_infrun)
2236 {
2237 fprintf_unfiltered (gdb_stdlog,
2238 "infrun: stepping past non-steppable watchpoint. "
2239 "skipping watchpoint at %s:%d\n",
2240 paddress (bl->gdbarch, bl->address),
2241 bl->length);
2242 }
2243 return 0;
2244 }
2245
2246 return 1;
2247}
2248
2249/* Same as should_be_inserted but does the check assuming
2250 that the location is not duplicated. */
2251
2252static int
2253unduplicated_should_be_inserted (struct bp_location *bl)
2254{
2255 int result;
2256 const int save_duplicate = bl->duplicate;
2257
2258 bl->duplicate = 0;
2259 result = should_be_inserted (bl);
2260 bl->duplicate = save_duplicate;
2261 return result;
2262}
2263
2264/* Parses a conditional described by an expression COND into an
2265 agent expression bytecode suitable for evaluation
2266 by the bytecode interpreter. Return NULL if there was
2267 any error during parsing. */
2268
2269static struct agent_expr *
2270parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2271{
2272 struct agent_expr *aexpr = NULL;
2273
2274 if (!cond)
2275 return NULL;
2276
2277 /* We don't want to stop processing, so catch any errors
2278 that may show up. */
2279 TRY
2280 {
2281 aexpr = gen_eval_for_expr (scope, cond);
2282 }
2283
2284 CATCH (ex, RETURN_MASK_ERROR)
2285 {
2286 /* If we got here, it means the condition could not be parsed to a valid
2287 bytecode expression and thus can't be evaluated on the target's side.
2288 It's no use iterating through the conditions. */
2289 return NULL;
2290 }
2291 END_CATCH
2292
2293 /* We have a valid agent expression. */
2294 return aexpr;
2295}
2296
2297/* Based on location BL, create a list of breakpoint conditions to be
2298 passed on to the target. If we have duplicated locations with different
2299 conditions, we will add such conditions to the list. The idea is that the
2300 target will evaluate the list of conditions and will only notify GDB when
2301 one of them is true. */
2302
2303static void
2304build_target_condition_list (struct bp_location *bl)
2305{
2306 struct bp_location **locp = NULL, **loc2p;
2307 int null_condition_or_parse_error = 0;
2308 int modified = bl->needs_update;
2309 struct bp_location *loc;
2310
2311 /* Release conditions left over from a previous insert. */
2312 VEC_free (agent_expr_p, bl->target_info.conditions);
2313
2314 /* This is only meaningful if the target is
2315 evaluating conditions and if the user has
2316 opted for condition evaluation on the target's
2317 side. */
2318 if (gdb_evaluates_breakpoint_condition_p ()
2319 || !target_supports_evaluation_of_breakpoint_conditions ())
2320 return;
2321
2322 /* Do a first pass to check for locations with no assigned
2323 conditions or conditions that fail to parse to a valid agent expression
2324 bytecode. If any of these happen, then it's no use to send conditions
2325 to the target since this location will always trigger and generate a
2326 response back to GDB. */
2327 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2328 {
2329 loc = (*loc2p);
2330 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2331 {
2332 if (modified)
2333 {
2334 struct agent_expr *aexpr;
2335
2336 /* Re-parse the conditions since something changed. In that
2337 case we already freed the condition bytecodes (see
2338 force_breakpoint_reinsertion). We just
2339 need to parse the condition to bytecodes again. */
2340 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2341 loc->cond_bytecode = aexpr;
2342 }
2343
2344 /* If we have a NULL bytecode expression, it means something
2345 went wrong or we have a null condition expression. */
2346 if (!loc->cond_bytecode)
2347 {
2348 null_condition_or_parse_error = 1;
2349 break;
2350 }
2351 }
2352 }
2353
2354 /* If any of these happened, it means we will have to evaluate the conditions
2355 for the location's address on gdb's side. It is no use keeping bytecodes
2356 for all the other duplicate locations, thus we free all of them here.
2357
2358 This is so we have a finer control over which locations' conditions are
2359 being evaluated by GDB or the remote stub. */
2360 if (null_condition_or_parse_error)
2361 {
2362 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2363 {
2364 loc = (*loc2p);
2365 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2366 {
2367 /* Only go as far as the first NULL bytecode is
2368 located. */
2369 if (!loc->cond_bytecode)
2370 return;
2371
2372 free_agent_expr (loc->cond_bytecode);
2373 loc->cond_bytecode = NULL;
2374 }
2375 }
2376 }
2377
2378 /* No NULL conditions or failed bytecode generation. Build a condition list
2379 for this location's address. */
2380 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2381 {
2382 loc = (*loc2p);
2383 if (loc->cond
2384 && is_breakpoint (loc->owner)
2385 && loc->pspace->num == bl->pspace->num
2386 && loc->owner->enable_state == bp_enabled
2387 && loc->enabled)
2388 /* Add the condition to the vector. This will be used later to send the
2389 conditions to the target. */
2390 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2391 loc->cond_bytecode);
2392 }
2393
2394 return;
2395}
2396
2397/* Parses a command described by string CMD into an agent expression
2398 bytecode suitable for evaluation by the bytecode interpreter.
2399 Return NULL if there was any error during parsing. */
2400
2401static struct agent_expr *
2402parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2403{
2404 struct cleanup *old_cleanups = 0;
2405 struct expression *expr, **argvec;
2406 struct agent_expr *aexpr = NULL;
2407 const char *cmdrest;
2408 const char *format_start, *format_end;
2409 struct format_piece *fpieces;
2410 int nargs;
2411 struct gdbarch *gdbarch = get_current_arch ();
2412
2413 if (!cmd)
2414 return NULL;
2415
2416 cmdrest = cmd;
2417
2418 if (*cmdrest == ',')
2419 ++cmdrest;
2420 cmdrest = skip_spaces_const (cmdrest);
2421
2422 if (*cmdrest++ != '"')
2423 error (_("No format string following the location"));
2424
2425 format_start = cmdrest;
2426
2427 fpieces = parse_format_string (&cmdrest);
2428
2429 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2430
2431 format_end = cmdrest;
2432
2433 if (*cmdrest++ != '"')
2434 error (_("Bad format string, non-terminated '\"'."));
2435
2436 cmdrest = skip_spaces_const (cmdrest);
2437
2438 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2439 error (_("Invalid argument syntax"));
2440
2441 if (*cmdrest == ',')
2442 cmdrest++;
2443 cmdrest = skip_spaces_const (cmdrest);
2444
2445 /* For each argument, make an expression. */
2446
2447 argvec = (struct expression **) alloca (strlen (cmd)
2448 * sizeof (struct expression *));
2449
2450 nargs = 0;
2451 while (*cmdrest != '\0')
2452 {
2453 const char *cmd1;
2454
2455 cmd1 = cmdrest;
2456 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2457 argvec[nargs++] = expr;
2458 cmdrest = cmd1;
2459 if (*cmdrest == ',')
2460 ++cmdrest;
2461 }
2462
2463 /* We don't want to stop processing, so catch any errors
2464 that may show up. */
2465 TRY
2466 {
2467 aexpr = gen_printf (scope, gdbarch, 0, 0,
2468 format_start, format_end - format_start,
2469 fpieces, nargs, argvec);
2470 }
2471 CATCH (ex, RETURN_MASK_ERROR)
2472 {
2473 /* If we got here, it means the command could not be parsed to a valid
2474 bytecode expression and thus can't be evaluated on the target's side.
2475 It's no use iterating through the other commands. */
2476 aexpr = NULL;
2477 }
2478 END_CATCH
2479
2480 do_cleanups (old_cleanups);
2481
2482 /* We have a valid agent expression, return it. */
2483 return aexpr;
2484}
2485
2486/* Based on location BL, create a list of breakpoint commands to be
2487 passed on to the target. If we have duplicated locations with
2488 different commands, we will add any such to the list. */
2489
2490static void
2491build_target_command_list (struct bp_location *bl)
2492{
2493 struct bp_location **locp = NULL, **loc2p;
2494 int null_command_or_parse_error = 0;
2495 int modified = bl->needs_update;
2496 struct bp_location *loc;
2497
2498 /* Release commands left over from a previous insert. */
2499 VEC_free (agent_expr_p, bl->target_info.tcommands);
2500
2501 if (!target_can_run_breakpoint_commands ())
2502 return;
2503
2504 /* For now, limit to agent-style dprintf breakpoints. */
2505 if (dprintf_style != dprintf_style_agent)
2506 return;
2507
2508 /* For now, if we have any duplicate location that isn't a dprintf,
2509 don't install the target-side commands, as that would make the
2510 breakpoint not be reported to the core, and we'd lose
2511 control. */
2512 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2513 {
2514 loc = (*loc2p);
2515 if (is_breakpoint (loc->owner)
2516 && loc->pspace->num == bl->pspace->num
2517 && loc->owner->type != bp_dprintf)
2518 return;
2519 }
2520
2521 /* Do a first pass to check for locations with no assigned
2522 conditions or conditions that fail to parse to a valid agent expression
2523 bytecode. If any of these happen, then it's no use to send conditions
2524 to the target since this location will always trigger and generate a
2525 response back to GDB. */
2526 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2527 {
2528 loc = (*loc2p);
2529 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2530 {
2531 if (modified)
2532 {
2533 struct agent_expr *aexpr;
2534
2535 /* Re-parse the commands since something changed. In that
2536 case we already freed the command bytecodes (see
2537 force_breakpoint_reinsertion). We just
2538 need to parse the command to bytecodes again. */
2539 aexpr = parse_cmd_to_aexpr (bl->address,
2540 loc->owner->extra_string);
2541 loc->cmd_bytecode = aexpr;
2542 }
2543
2544 /* If we have a NULL bytecode expression, it means something
2545 went wrong or we have a null command expression. */
2546 if (!loc->cmd_bytecode)
2547 {
2548 null_command_or_parse_error = 1;
2549 break;
2550 }
2551 }
2552 }
2553
2554 /* If anything failed, then we're not doing target-side commands,
2555 and so clean up. */
2556 if (null_command_or_parse_error)
2557 {
2558 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2559 {
2560 loc = (*loc2p);
2561 if (is_breakpoint (loc->owner)
2562 && loc->pspace->num == bl->pspace->num)
2563 {
2564 /* Only go as far as the first NULL bytecode is
2565 located. */
2566 if (loc->cmd_bytecode == NULL)
2567 return;
2568
2569 free_agent_expr (loc->cmd_bytecode);
2570 loc->cmd_bytecode = NULL;
2571 }
2572 }
2573 }
2574
2575 /* No NULL commands or failed bytecode generation. Build a command list
2576 for this location's address. */
2577 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2578 {
2579 loc = (*loc2p);
2580 if (loc->owner->extra_string
2581 && is_breakpoint (loc->owner)
2582 && loc->pspace->num == bl->pspace->num
2583 && loc->owner->enable_state == bp_enabled
2584 && loc->enabled)
2585 /* Add the command to the vector. This will be used later
2586 to send the commands to the target. */
2587 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2588 loc->cmd_bytecode);
2589 }
2590
2591 bl->target_info.persist = 0;
2592 /* Maybe flag this location as persistent. */
2593 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2594 bl->target_info.persist = 1;
2595}
2596
2597/* Return the kind of breakpoint on address *ADDR. Get the kind
2598 of breakpoint according to ADDR except single-step breakpoint.
2599 Get the kind of single-step breakpoint according to the current
2600 registers state. */
2601
2602static int
2603breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2604{
2605 if (bl->owner->type == bp_single_step)
2606 {
2607 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2608 struct regcache *regcache;
2609
2610 regcache = get_thread_regcache (thr->ptid);
2611
2612 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2613 regcache, addr);
2614 }
2615 else
2616 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2617}
2618
2619/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2620 location. Any error messages are printed to TMP_ERROR_STREAM; and
2621 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2622 Returns 0 for success, 1 if the bp_location type is not supported or
2623 -1 for failure.
2624
2625 NOTE drow/2003-09-09: This routine could be broken down to an
2626 object-style method for each breakpoint or catchpoint type. */
2627static int
2628insert_bp_location (struct bp_location *bl,
2629 struct ui_file *tmp_error_stream,
2630 int *disabled_breaks,
2631 int *hw_breakpoint_error,
2632 int *hw_bp_error_explained_already)
2633{
2634 enum errors bp_err = GDB_NO_ERROR;
2635 const char *bp_err_message = NULL;
2636
2637 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2638 return 0;
2639
2640 /* Note we don't initialize bl->target_info, as that wipes out
2641 the breakpoint location's shadow_contents if the breakpoint
2642 is still inserted at that location. This in turn breaks
2643 target_read_memory which depends on these buffers when
2644 a memory read is requested at the breakpoint location:
2645 Once the target_info has been wiped, we fail to see that
2646 we have a breakpoint inserted at that address and thus
2647 read the breakpoint instead of returning the data saved in
2648 the breakpoint location's shadow contents. */
2649 bl->target_info.reqstd_address = bl->address;
2650 bl->target_info.placed_address_space = bl->pspace->aspace;
2651 bl->target_info.length = bl->length;
2652
2653 /* When working with target-side conditions, we must pass all the conditions
2654 for the same breakpoint address down to the target since GDB will not
2655 insert those locations. With a list of breakpoint conditions, the target
2656 can decide when to stop and notify GDB. */
2657
2658 if (is_breakpoint (bl->owner))
2659 {
2660 build_target_condition_list (bl);
2661 build_target_command_list (bl);
2662 /* Reset the modification marker. */
2663 bl->needs_update = 0;
2664 }
2665
2666 if (bl->loc_type == bp_loc_software_breakpoint
2667 || bl->loc_type == bp_loc_hardware_breakpoint)
2668 {
2669 if (bl->owner->type != bp_hardware_breakpoint)
2670 {
2671 /* If the explicitly specified breakpoint type
2672 is not hardware breakpoint, check the memory map to see
2673 if the breakpoint address is in read only memory or not.
2674
2675 Two important cases are:
2676 - location type is not hardware breakpoint, memory
2677 is readonly. We change the type of the location to
2678 hardware breakpoint.
2679 - location type is hardware breakpoint, memory is
2680 read-write. This means we've previously made the
2681 location hardware one, but then the memory map changed,
2682 so we undo.
2683
2684 When breakpoints are removed, remove_breakpoints will use
2685 location types we've just set here, the only possible
2686 problem is that memory map has changed during running
2687 program, but it's not going to work anyway with current
2688 gdb. */
2689 struct mem_region *mr
2690 = lookup_mem_region (bl->target_info.reqstd_address);
2691
2692 if (mr)
2693 {
2694 if (automatic_hardware_breakpoints)
2695 {
2696 enum bp_loc_type new_type;
2697
2698 if (mr->attrib.mode != MEM_RW)
2699 new_type = bp_loc_hardware_breakpoint;
2700 else
2701 new_type = bp_loc_software_breakpoint;
2702
2703 if (new_type != bl->loc_type)
2704 {
2705 static int said = 0;
2706
2707 bl->loc_type = new_type;
2708 if (!said)
2709 {
2710 fprintf_filtered (gdb_stdout,
2711 _("Note: automatically using "
2712 "hardware breakpoints for "
2713 "read-only addresses.\n"));
2714 said = 1;
2715 }
2716 }
2717 }
2718 else if (bl->loc_type == bp_loc_software_breakpoint
2719 && mr->attrib.mode != MEM_RW)
2720 {
2721 fprintf_unfiltered (tmp_error_stream,
2722 _("Cannot insert breakpoint %d.\n"
2723 "Cannot set software breakpoint "
2724 "at read-only address %s\n"),
2725 bl->owner->number,
2726 paddress (bl->gdbarch, bl->address));
2727 return 1;
2728 }
2729 }
2730 }
2731
2732 /* First check to see if we have to handle an overlay. */
2733 if (overlay_debugging == ovly_off
2734 || bl->section == NULL
2735 || !(section_is_overlay (bl->section)))
2736 {
2737 /* No overlay handling: just set the breakpoint. */
2738 TRY
2739 {
2740 int val;
2741
2742 val = bl->owner->ops->insert_location (bl);
2743 if (val)
2744 bp_err = GENERIC_ERROR;
2745 }
2746 CATCH (e, RETURN_MASK_ALL)
2747 {
2748 bp_err = e.error;
2749 bp_err_message = e.message;
2750 }
2751 END_CATCH
2752 }
2753 else
2754 {
2755 /* This breakpoint is in an overlay section.
2756 Shall we set a breakpoint at the LMA? */
2757 if (!overlay_events_enabled)
2758 {
2759 /* Yes -- overlay event support is not active,
2760 so we must try to set a breakpoint at the LMA.
2761 This will not work for a hardware breakpoint. */
2762 if (bl->loc_type == bp_loc_hardware_breakpoint)
2763 warning (_("hardware breakpoint %d not supported in overlay!"),
2764 bl->owner->number);
2765 else
2766 {
2767 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2768 bl->section);
2769 /* Set a software (trap) breakpoint at the LMA. */
2770 bl->overlay_target_info = bl->target_info;
2771 bl->overlay_target_info.reqstd_address = addr;
2772
2773 /* No overlay handling: just set the breakpoint. */
2774 TRY
2775 {
2776 int val;
2777
2778 bl->overlay_target_info.kind
2779 = breakpoint_kind (bl, &addr);
2780 bl->overlay_target_info.placed_address = addr;
2781 val = target_insert_breakpoint (bl->gdbarch,
2782 &bl->overlay_target_info);
2783 if (val)
2784 bp_err = GENERIC_ERROR;
2785 }
2786 CATCH (e, RETURN_MASK_ALL)
2787 {
2788 bp_err = e.error;
2789 bp_err_message = e.message;
2790 }
2791 END_CATCH
2792
2793 if (bp_err != GDB_NO_ERROR)
2794 fprintf_unfiltered (tmp_error_stream,
2795 "Overlay breakpoint %d "
2796 "failed: in ROM?\n",
2797 bl->owner->number);
2798 }
2799 }
2800 /* Shall we set a breakpoint at the VMA? */
2801 if (section_is_mapped (bl->section))
2802 {
2803 /* Yes. This overlay section is mapped into memory. */
2804 TRY
2805 {
2806 int val;
2807
2808 val = bl->owner->ops->insert_location (bl);
2809 if (val)
2810 bp_err = GENERIC_ERROR;
2811 }
2812 CATCH (e, RETURN_MASK_ALL)
2813 {
2814 bp_err = e.error;
2815 bp_err_message = e.message;
2816 }
2817 END_CATCH
2818 }
2819 else
2820 {
2821 /* No. This breakpoint will not be inserted.
2822 No error, but do not mark the bp as 'inserted'. */
2823 return 0;
2824 }
2825 }
2826
2827 if (bp_err != GDB_NO_ERROR)
2828 {
2829 /* Can't set the breakpoint. */
2830
2831 /* In some cases, we might not be able to insert a
2832 breakpoint in a shared library that has already been
2833 removed, but we have not yet processed the shlib unload
2834 event. Unfortunately, some targets that implement
2835 breakpoint insertion themselves can't tell why the
2836 breakpoint insertion failed (e.g., the remote target
2837 doesn't define error codes), so we must treat generic
2838 errors as memory errors. */
2839 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2840 && bl->loc_type == bp_loc_software_breakpoint
2841 && (solib_name_from_address (bl->pspace, bl->address)
2842 || shared_objfile_contains_address_p (bl->pspace,
2843 bl->address)))
2844 {
2845 /* See also: disable_breakpoints_in_shlibs. */
2846 bl->shlib_disabled = 1;
2847 observer_notify_breakpoint_modified (bl->owner);
2848 if (!*disabled_breaks)
2849 {
2850 fprintf_unfiltered (tmp_error_stream,
2851 "Cannot insert breakpoint %d.\n",
2852 bl->owner->number);
2853 fprintf_unfiltered (tmp_error_stream,
2854 "Temporarily disabling shared "
2855 "library breakpoints:\n");
2856 }
2857 *disabled_breaks = 1;
2858 fprintf_unfiltered (tmp_error_stream,
2859 "breakpoint #%d\n", bl->owner->number);
2860 return 0;
2861 }
2862 else
2863 {
2864 if (bl->loc_type == bp_loc_hardware_breakpoint)
2865 {
2866 *hw_breakpoint_error = 1;
2867 *hw_bp_error_explained_already = bp_err_message != NULL;
2868 fprintf_unfiltered (tmp_error_stream,
2869 "Cannot insert hardware breakpoint %d%s",
2870 bl->owner->number, bp_err_message ? ":" : ".\n");
2871 if (bp_err_message != NULL)
2872 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2873 }
2874 else
2875 {
2876 if (bp_err_message == NULL)
2877 {
2878 char *message
2879 = memory_error_message (TARGET_XFER_E_IO,
2880 bl->gdbarch, bl->address);
2881 struct cleanup *old_chain = make_cleanup (xfree, message);
2882
2883 fprintf_unfiltered (tmp_error_stream,
2884 "Cannot insert breakpoint %d.\n"
2885 "%s\n",
2886 bl->owner->number, message);
2887 do_cleanups (old_chain);
2888 }
2889 else
2890 {
2891 fprintf_unfiltered (tmp_error_stream,
2892 "Cannot insert breakpoint %d: %s\n",
2893 bl->owner->number,
2894 bp_err_message);
2895 }
2896 }
2897 return 1;
2898
2899 }
2900 }
2901 else
2902 bl->inserted = 1;
2903
2904 return 0;
2905 }
2906
2907 else if (bl->loc_type == bp_loc_hardware_watchpoint
2908 /* NOTE drow/2003-09-08: This state only exists for removing
2909 watchpoints. It's not clear that it's necessary... */
2910 && bl->owner->disposition != disp_del_at_next_stop)
2911 {
2912 int val;
2913
2914 gdb_assert (bl->owner->ops != NULL
2915 && bl->owner->ops->insert_location != NULL);
2916
2917 val = bl->owner->ops->insert_location (bl);
2918
2919 /* If trying to set a read-watchpoint, and it turns out it's not
2920 supported, try emulating one with an access watchpoint. */
2921 if (val == 1 && bl->watchpoint_type == hw_read)
2922 {
2923 struct bp_location *loc, **loc_temp;
2924
2925 /* But don't try to insert it, if there's already another
2926 hw_access location that would be considered a duplicate
2927 of this one. */
2928 ALL_BP_LOCATIONS (loc, loc_temp)
2929 if (loc != bl
2930 && loc->watchpoint_type == hw_access
2931 && watchpoint_locations_match (bl, loc))
2932 {
2933 bl->duplicate = 1;
2934 bl->inserted = 1;
2935 bl->target_info = loc->target_info;
2936 bl->watchpoint_type = hw_access;
2937 val = 0;
2938 break;
2939 }
2940
2941 if (val == 1)
2942 {
2943 bl->watchpoint_type = hw_access;
2944 val = bl->owner->ops->insert_location (bl);
2945
2946 if (val)
2947 /* Back to the original value. */
2948 bl->watchpoint_type = hw_read;
2949 }
2950 }
2951
2952 bl->inserted = (val == 0);
2953 }
2954
2955 else if (bl->owner->type == bp_catchpoint)
2956 {
2957 int val;
2958
2959 gdb_assert (bl->owner->ops != NULL
2960 && bl->owner->ops->insert_location != NULL);
2961
2962 val = bl->owner->ops->insert_location (bl);
2963 if (val)
2964 {
2965 bl->owner->enable_state = bp_disabled;
2966
2967 if (val == 1)
2968 warning (_("\
2969Error inserting catchpoint %d: Your system does not support this type\n\
2970of catchpoint."), bl->owner->number);
2971 else
2972 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2973 }
2974
2975 bl->inserted = (val == 0);
2976
2977 /* We've already printed an error message if there was a problem
2978 inserting this catchpoint, and we've disabled the catchpoint,
2979 so just return success. */
2980 return 0;
2981 }
2982
2983 return 0;
2984}
2985
2986/* This function is called when program space PSPACE is about to be
2987 deleted. It takes care of updating breakpoints to not reference
2988 PSPACE anymore. */
2989
2990void
2991breakpoint_program_space_exit (struct program_space *pspace)
2992{
2993 struct breakpoint *b, *b_temp;
2994 struct bp_location *loc, **loc_temp;
2995
2996 /* Remove any breakpoint that was set through this program space. */
2997 ALL_BREAKPOINTS_SAFE (b, b_temp)
2998 {
2999 if (b->pspace == pspace)
3000 delete_breakpoint (b);
3001 }
3002
3003 /* Breakpoints set through other program spaces could have locations
3004 bound to PSPACE as well. Remove those. */
3005 ALL_BP_LOCATIONS (loc, loc_temp)
3006 {
3007 struct bp_location *tmp;
3008
3009 if (loc->pspace == pspace)
3010 {
3011 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3012 if (loc->owner->loc == loc)
3013 loc->owner->loc = loc->next;
3014 else
3015 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3016 if (tmp->next == loc)
3017 {
3018 tmp->next = loc->next;
3019 break;
3020 }
3021 }
3022 }
3023
3024 /* Now update the global location list to permanently delete the
3025 removed locations above. */
3026 update_global_location_list (UGLL_DONT_INSERT);
3027}
3028
3029/* Make sure all breakpoints are inserted in inferior.
3030 Throws exception on any error.
3031 A breakpoint that is already inserted won't be inserted
3032 again, so calling this function twice is safe. */
3033void
3034insert_breakpoints (void)
3035{
3036 struct breakpoint *bpt;
3037
3038 ALL_BREAKPOINTS (bpt)
3039 if (is_hardware_watchpoint (bpt))
3040 {
3041 struct watchpoint *w = (struct watchpoint *) bpt;
3042
3043 update_watchpoint (w, 0 /* don't reparse. */);
3044 }
3045
3046 /* Updating watchpoints creates new locations, so update the global
3047 location list. Explicitly tell ugll to insert locations and
3048 ignore breakpoints_always_inserted_mode. */
3049 update_global_location_list (UGLL_INSERT);
3050}
3051
3052/* Invoke CALLBACK for each of bp_location. */
3053
3054void
3055iterate_over_bp_locations (walk_bp_location_callback callback)
3056{
3057 struct bp_location *loc, **loc_tmp;
3058
3059 ALL_BP_LOCATIONS (loc, loc_tmp)
3060 {
3061 callback (loc, NULL);
3062 }
3063}
3064
3065/* This is used when we need to synch breakpoint conditions between GDB and the
3066 target. It is the case with deleting and disabling of breakpoints when using
3067 always-inserted mode. */
3068
3069static void
3070update_inserted_breakpoint_locations (void)
3071{
3072 struct bp_location *bl, **blp_tmp;
3073 int error_flag = 0;
3074 int val = 0;
3075 int disabled_breaks = 0;
3076 int hw_breakpoint_error = 0;
3077 int hw_bp_details_reported = 0;
3078
3079 struct ui_file *tmp_error_stream = mem_fileopen ();
3080 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3081
3082 /* Explicitly mark the warning -- this will only be printed if
3083 there was an error. */
3084 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3085
3086 save_current_space_and_thread ();
3087
3088 ALL_BP_LOCATIONS (bl, blp_tmp)
3089 {
3090 /* We only want to update software breakpoints and hardware
3091 breakpoints. */
3092 if (!is_breakpoint (bl->owner))
3093 continue;
3094
3095 /* We only want to update locations that are already inserted
3096 and need updating. This is to avoid unwanted insertion during
3097 deletion of breakpoints. */
3098 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3099 continue;
3100
3101 switch_to_program_space_and_thread (bl->pspace);
3102
3103 /* For targets that support global breakpoints, there's no need
3104 to select an inferior to insert breakpoint to. In fact, even
3105 if we aren't attached to any process yet, we should still
3106 insert breakpoints. */
3107 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3108 && ptid_equal (inferior_ptid, null_ptid))
3109 continue;
3110
3111 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3112 &hw_breakpoint_error, &hw_bp_details_reported);
3113 if (val)
3114 error_flag = val;
3115 }
3116
3117 if (error_flag)
3118 {
3119 target_terminal_ours_for_output ();
3120 error_stream (tmp_error_stream);
3121 }
3122
3123 do_cleanups (cleanups);
3124}
3125
3126/* Used when starting or continuing the program. */
3127
3128static void
3129insert_breakpoint_locations (void)
3130{
3131 struct breakpoint *bpt;
3132 struct bp_location *bl, **blp_tmp;
3133 int error_flag = 0;
3134 int val = 0;
3135 int disabled_breaks = 0;
3136 int hw_breakpoint_error = 0;
3137 int hw_bp_error_explained_already = 0;
3138
3139 struct ui_file *tmp_error_stream = mem_fileopen ();
3140 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3141
3142 /* Explicitly mark the warning -- this will only be printed if
3143 there was an error. */
3144 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3145
3146 save_current_space_and_thread ();
3147
3148 ALL_BP_LOCATIONS (bl, blp_tmp)
3149 {
3150 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3151 continue;
3152
3153 /* There is no point inserting thread-specific breakpoints if
3154 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3155 has BL->OWNER always non-NULL. */
3156 if (bl->owner->thread != -1
3157 && !valid_global_thread_id (bl->owner->thread))
3158 continue;
3159
3160 switch_to_program_space_and_thread (bl->pspace);
3161
3162 /* For targets that support global breakpoints, there's no need
3163 to select an inferior to insert breakpoint to. In fact, even
3164 if we aren't attached to any process yet, we should still
3165 insert breakpoints. */
3166 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3167 && ptid_equal (inferior_ptid, null_ptid))
3168 continue;
3169
3170 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3171 &hw_breakpoint_error, &hw_bp_error_explained_already);
3172 if (val)
3173 error_flag = val;
3174 }
3175
3176 /* If we failed to insert all locations of a watchpoint, remove
3177 them, as half-inserted watchpoint is of limited use. */
3178 ALL_BREAKPOINTS (bpt)
3179 {
3180 int some_failed = 0;
3181 struct bp_location *loc;
3182
3183 if (!is_hardware_watchpoint (bpt))
3184 continue;
3185
3186 if (!breakpoint_enabled (bpt))
3187 continue;
3188
3189 if (bpt->disposition == disp_del_at_next_stop)
3190 continue;
3191
3192 for (loc = bpt->loc; loc; loc = loc->next)
3193 if (!loc->inserted && should_be_inserted (loc))
3194 {
3195 some_failed = 1;
3196 break;
3197 }
3198 if (some_failed)
3199 {
3200 for (loc = bpt->loc; loc; loc = loc->next)
3201 if (loc->inserted)
3202 remove_breakpoint (loc);
3203
3204 hw_breakpoint_error = 1;
3205 fprintf_unfiltered (tmp_error_stream,
3206 "Could not insert hardware watchpoint %d.\n",
3207 bpt->number);
3208 error_flag = -1;
3209 }
3210 }
3211
3212 if (error_flag)
3213 {
3214 /* If a hardware breakpoint or watchpoint was inserted, add a
3215 message about possibly exhausted resources. */
3216 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3217 {
3218 fprintf_unfiltered (tmp_error_stream,
3219 "Could not insert hardware breakpoints:\n\
3220You may have requested too many hardware breakpoints/watchpoints.\n");
3221 }
3222 target_terminal_ours_for_output ();
3223 error_stream (tmp_error_stream);
3224 }
3225
3226 do_cleanups (cleanups);
3227}
3228
3229/* Used when the program stops.
3230 Returns zero if successful, or non-zero if there was a problem
3231 removing a breakpoint location. */
3232
3233int
3234remove_breakpoints (void)
3235{
3236 struct bp_location *bl, **blp_tmp;
3237 int val = 0;
3238
3239 ALL_BP_LOCATIONS (bl, blp_tmp)
3240 {
3241 if (bl->inserted && !is_tracepoint (bl->owner))
3242 val |= remove_breakpoint (bl);
3243 }
3244 return val;
3245}
3246
3247/* When a thread exits, remove breakpoints that are related to
3248 that thread. */
3249
3250static void
3251remove_threaded_breakpoints (struct thread_info *tp, int silent)
3252{
3253 struct breakpoint *b, *b_tmp;
3254
3255 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3256 {
3257 if (b->thread == tp->global_num && user_breakpoint_p (b))
3258 {
3259 b->disposition = disp_del_at_next_stop;
3260
3261 printf_filtered (_("\
3262Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3263 b->number, print_thread_id (tp));
3264
3265 /* Hide it from the user. */
3266 b->number = 0;
3267 }
3268 }
3269}
3270
3271/* Remove breakpoints of process PID. */
3272
3273int
3274remove_breakpoints_pid (int pid)
3275{
3276 struct bp_location *bl, **blp_tmp;
3277 int val;
3278 struct inferior *inf = find_inferior_pid (pid);
3279
3280 ALL_BP_LOCATIONS (bl, blp_tmp)
3281 {
3282 if (bl->pspace != inf->pspace)
3283 continue;
3284
3285 if (bl->inserted && !bl->target_info.persist)
3286 {
3287 val = remove_breakpoint (bl);
3288 if (val != 0)
3289 return val;
3290 }
3291 }
3292 return 0;
3293}
3294
3295int
3296reattach_breakpoints (int pid)
3297{
3298 struct cleanup *old_chain;
3299 struct bp_location *bl, **blp_tmp;
3300 int val;
3301 struct ui_file *tmp_error_stream;
3302 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3303 struct inferior *inf;
3304 struct thread_info *tp;
3305
3306 tp = any_live_thread_of_process (pid);
3307 if (tp == NULL)
3308 return 1;
3309
3310 inf = find_inferior_pid (pid);
3311 old_chain = save_inferior_ptid ();
3312
3313 inferior_ptid = tp->ptid;
3314
3315 tmp_error_stream = mem_fileopen ();
3316 make_cleanup_ui_file_delete (tmp_error_stream);
3317
3318 ALL_BP_LOCATIONS (bl, blp_tmp)
3319 {
3320 if (bl->pspace != inf->pspace)
3321 continue;
3322
3323 if (bl->inserted)
3324 {
3325 bl->inserted = 0;
3326 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3327 if (val != 0)
3328 {
3329 do_cleanups (old_chain);
3330 return val;
3331 }
3332 }
3333 }
3334 do_cleanups (old_chain);
3335 return 0;
3336}
3337
3338static int internal_breakpoint_number = -1;
3339
3340/* Set the breakpoint number of B, depending on the value of INTERNAL.
3341 If INTERNAL is non-zero, the breakpoint number will be populated
3342 from internal_breakpoint_number and that variable decremented.
3343 Otherwise the breakpoint number will be populated from
3344 breakpoint_count and that value incremented. Internal breakpoints
3345 do not set the internal var bpnum. */
3346static void
3347set_breakpoint_number (int internal, struct breakpoint *b)
3348{
3349 if (internal)
3350 b->number = internal_breakpoint_number--;
3351 else
3352 {
3353 set_breakpoint_count (breakpoint_count + 1);
3354 b->number = breakpoint_count;
3355 }
3356}
3357
3358static struct breakpoint *
3359create_internal_breakpoint (struct gdbarch *gdbarch,
3360 CORE_ADDR address, enum bptype type,
3361 const struct breakpoint_ops *ops)
3362{
3363 struct symtab_and_line sal;
3364 struct breakpoint *b;
3365
3366 init_sal (&sal); /* Initialize to zeroes. */
3367
3368 sal.pc = address;
3369 sal.section = find_pc_overlay (sal.pc);
3370 sal.pspace = current_program_space;
3371
3372 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3373 b->number = internal_breakpoint_number--;
3374 b->disposition = disp_donttouch;
3375
3376 return b;
3377}
3378
3379static const char *const longjmp_names[] =
3380 {
3381 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3382 };
3383#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3384
3385/* Per-objfile data private to breakpoint.c. */
3386struct breakpoint_objfile_data
3387{
3388 /* Minimal symbol for "_ovly_debug_event" (if any). */
3389 struct bound_minimal_symbol overlay_msym;
3390
3391 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3392 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3393
3394 /* True if we have looked for longjmp probes. */
3395 int longjmp_searched;
3396
3397 /* SystemTap probe points for longjmp (if any). */
3398 VEC (probe_p) *longjmp_probes;
3399
3400 /* Minimal symbol for "std::terminate()" (if any). */
3401 struct bound_minimal_symbol terminate_msym;
3402
3403 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3404 struct bound_minimal_symbol exception_msym;
3405
3406 /* True if we have looked for exception probes. */
3407 int exception_searched;
3408
3409 /* SystemTap probe points for unwinding (if any). */
3410 VEC (probe_p) *exception_probes;
3411};
3412
3413static const struct objfile_data *breakpoint_objfile_key;
3414
3415/* Minimal symbol not found sentinel. */
3416static struct minimal_symbol msym_not_found;
3417
3418/* Returns TRUE if MSYM point to the "not found" sentinel. */
3419
3420static int
3421msym_not_found_p (const struct minimal_symbol *msym)
3422{
3423 return msym == &msym_not_found;
3424}
3425
3426/* Return per-objfile data needed by breakpoint.c.
3427 Allocate the data if necessary. */
3428
3429static struct breakpoint_objfile_data *
3430get_breakpoint_objfile_data (struct objfile *objfile)
3431{
3432 struct breakpoint_objfile_data *bp_objfile_data;
3433
3434 bp_objfile_data = ((struct breakpoint_objfile_data *)
3435 objfile_data (objfile, breakpoint_objfile_key));
3436 if (bp_objfile_data == NULL)
3437 {
3438 bp_objfile_data =
3439 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3440
3441 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3442 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3443 }
3444 return bp_objfile_data;
3445}
3446
3447static void
3448free_breakpoint_probes (struct objfile *obj, void *data)
3449{
3450 struct breakpoint_objfile_data *bp_objfile_data
3451 = (struct breakpoint_objfile_data *) data;
3452
3453 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3454 VEC_free (probe_p, bp_objfile_data->exception_probes);
3455}
3456
3457static void
3458create_overlay_event_breakpoint (void)
3459{
3460 struct objfile *objfile;
3461 const char *const func_name = "_ovly_debug_event";
3462
3463 ALL_OBJFILES (objfile)
3464 {
3465 struct breakpoint *b;
3466 struct breakpoint_objfile_data *bp_objfile_data;
3467 CORE_ADDR addr;
3468 struct explicit_location explicit_loc;
3469
3470 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3471
3472 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3473 continue;
3474
3475 if (bp_objfile_data->overlay_msym.minsym == NULL)
3476 {
3477 struct bound_minimal_symbol m;
3478
3479 m = lookup_minimal_symbol_text (func_name, objfile);
3480 if (m.minsym == NULL)
3481 {
3482 /* Avoid future lookups in this objfile. */
3483 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3484 continue;
3485 }
3486 bp_objfile_data->overlay_msym = m;
3487 }
3488
3489 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3490 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3491 bp_overlay_event,
3492 &internal_breakpoint_ops);
3493 initialize_explicit_location (&explicit_loc);
3494 explicit_loc.function_name = ASTRDUP (func_name);
3495 b->location = new_explicit_location (&explicit_loc);
3496
3497 if (overlay_debugging == ovly_auto)
3498 {
3499 b->enable_state = bp_enabled;
3500 overlay_events_enabled = 1;
3501 }
3502 else
3503 {
3504 b->enable_state = bp_disabled;
3505 overlay_events_enabled = 0;
3506 }
3507 }
3508}
3509
3510static void
3511create_longjmp_master_breakpoint (void)
3512{
3513 struct program_space *pspace;
3514 struct cleanup *old_chain;
3515
3516 old_chain = save_current_program_space ();
3517
3518 ALL_PSPACES (pspace)
3519 {
3520 struct objfile *objfile;
3521
3522 set_current_program_space (pspace);
3523
3524 ALL_OBJFILES (objfile)
3525 {
3526 int i;
3527 struct gdbarch *gdbarch;
3528 struct breakpoint_objfile_data *bp_objfile_data;
3529
3530 gdbarch = get_objfile_arch (objfile);
3531
3532 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3533
3534 if (!bp_objfile_data->longjmp_searched)
3535 {
3536 VEC (probe_p) *ret;
3537
3538 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3539 if (ret != NULL)
3540 {
3541 /* We are only interested in checking one element. */
3542 struct probe *p = VEC_index (probe_p, ret, 0);
3543
3544 if (!can_evaluate_probe_arguments (p))
3545 {
3546 /* We cannot use the probe interface here, because it does
3547 not know how to evaluate arguments. */
3548 VEC_free (probe_p, ret);
3549 ret = NULL;
3550 }
3551 }
3552 bp_objfile_data->longjmp_probes = ret;
3553 bp_objfile_data->longjmp_searched = 1;
3554 }
3555
3556 if (bp_objfile_data->longjmp_probes != NULL)
3557 {
3558 int i;
3559 struct probe *probe;
3560 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3561
3562 for (i = 0;
3563 VEC_iterate (probe_p,
3564 bp_objfile_data->longjmp_probes,
3565 i, probe);
3566 ++i)
3567 {
3568 struct breakpoint *b;
3569
3570 b = create_internal_breakpoint (gdbarch,
3571 get_probe_address (probe,
3572 objfile),
3573 bp_longjmp_master,
3574 &internal_breakpoint_ops);
3575 b->location
3576 = new_probe_location ("-probe-stap libc:longjmp");
3577 b->enable_state = bp_disabled;
3578 }
3579
3580 continue;
3581 }
3582
3583 if (!gdbarch_get_longjmp_target_p (gdbarch))
3584 continue;
3585
3586 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3587 {
3588 struct breakpoint *b;
3589 const char *func_name;
3590 CORE_ADDR addr;
3591 struct explicit_location explicit_loc;
3592
3593 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3594 continue;
3595
3596 func_name = longjmp_names[i];
3597 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3598 {
3599 struct bound_minimal_symbol m;
3600
3601 m = lookup_minimal_symbol_text (func_name, objfile);
3602 if (m.minsym == NULL)
3603 {
3604 /* Prevent future lookups in this objfile. */
3605 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3606 continue;
3607 }
3608 bp_objfile_data->longjmp_msym[i] = m;
3609 }
3610
3611 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3612 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3613 &internal_breakpoint_ops);
3614 initialize_explicit_location (&explicit_loc);
3615 explicit_loc.function_name = ASTRDUP (func_name);
3616 b->location = new_explicit_location (&explicit_loc);
3617 b->enable_state = bp_disabled;
3618 }
3619 }
3620 }
3621
3622 do_cleanups (old_chain);
3623}
3624
3625/* Create a master std::terminate breakpoint. */
3626static void
3627create_std_terminate_master_breakpoint (void)
3628{
3629 struct program_space *pspace;
3630 struct cleanup *old_chain;
3631 const char *const func_name = "std::terminate()";
3632
3633 old_chain = save_current_program_space ();
3634
3635 ALL_PSPACES (pspace)
3636 {
3637 struct objfile *objfile;
3638 CORE_ADDR addr;
3639
3640 set_current_program_space (pspace);
3641
3642 ALL_OBJFILES (objfile)
3643 {
3644 struct breakpoint *b;
3645 struct breakpoint_objfile_data *bp_objfile_data;
3646 struct explicit_location explicit_loc;
3647
3648 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3649
3650 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3651 continue;
3652
3653 if (bp_objfile_data->terminate_msym.minsym == NULL)
3654 {
3655 struct bound_minimal_symbol m;
3656
3657 m = lookup_minimal_symbol (func_name, NULL, objfile);
3658 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3659 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3660 {
3661 /* Prevent future lookups in this objfile. */
3662 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3663 continue;
3664 }
3665 bp_objfile_data->terminate_msym = m;
3666 }
3667
3668 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3669 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3670 bp_std_terminate_master,
3671 &internal_breakpoint_ops);
3672 initialize_explicit_location (&explicit_loc);
3673 explicit_loc.function_name = ASTRDUP (func_name);
3674 b->location = new_explicit_location (&explicit_loc);
3675 b->enable_state = bp_disabled;
3676 }
3677 }
3678
3679 do_cleanups (old_chain);
3680}
3681
3682/* Install a master breakpoint on the unwinder's debug hook. */
3683
3684static void
3685create_exception_master_breakpoint (void)
3686{
3687 struct objfile *objfile;
3688 const char *const func_name = "_Unwind_DebugHook";
3689
3690 ALL_OBJFILES (objfile)
3691 {
3692 struct breakpoint *b;
3693 struct gdbarch *gdbarch;
3694 struct breakpoint_objfile_data *bp_objfile_data;
3695 CORE_ADDR addr;
3696 struct explicit_location explicit_loc;
3697
3698 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3699
3700 /* We prefer the SystemTap probe point if it exists. */
3701 if (!bp_objfile_data->exception_searched)
3702 {
3703 VEC (probe_p) *ret;
3704
3705 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3706
3707 if (ret != NULL)
3708 {
3709 /* We are only interested in checking one element. */
3710 struct probe *p = VEC_index (probe_p, ret, 0);
3711
3712 if (!can_evaluate_probe_arguments (p))
3713 {
3714 /* We cannot use the probe interface here, because it does
3715 not know how to evaluate arguments. */
3716 VEC_free (probe_p, ret);
3717 ret = NULL;
3718 }
3719 }
3720 bp_objfile_data->exception_probes = ret;
3721 bp_objfile_data->exception_searched = 1;
3722 }
3723
3724 if (bp_objfile_data->exception_probes != NULL)
3725 {
3726 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3727 int i;
3728 struct probe *probe;
3729
3730 for (i = 0;
3731 VEC_iterate (probe_p,
3732 bp_objfile_data->exception_probes,
3733 i, probe);
3734 ++i)
3735 {
3736 struct breakpoint *b;
3737
3738 b = create_internal_breakpoint (gdbarch,
3739 get_probe_address (probe,
3740 objfile),
3741 bp_exception_master,
3742 &internal_breakpoint_ops);
3743 b->location
3744 = new_probe_location ("-probe-stap libgcc:unwind");
3745 b->enable_state = bp_disabled;
3746 }
3747
3748 continue;
3749 }
3750
3751 /* Otherwise, try the hook function. */
3752
3753 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3754 continue;
3755
3756 gdbarch = get_objfile_arch (objfile);
3757
3758 if (bp_objfile_data->exception_msym.minsym == NULL)
3759 {
3760 struct bound_minimal_symbol debug_hook;
3761
3762 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3763 if (debug_hook.minsym == NULL)
3764 {
3765 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3766 continue;
3767 }
3768
3769 bp_objfile_data->exception_msym = debug_hook;
3770 }
3771
3772 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3773 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3774 &current_target);
3775 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3776 &internal_breakpoint_ops);
3777 initialize_explicit_location (&explicit_loc);
3778 explicit_loc.function_name = ASTRDUP (func_name);
3779 b->location = new_explicit_location (&explicit_loc);
3780 b->enable_state = bp_disabled;
3781 }
3782}
3783
3784/* Does B have a location spec? */
3785
3786static int
3787breakpoint_event_location_empty_p (const struct breakpoint *b)
3788{
3789 return b->location != NULL && event_location_empty_p (b->location);
3790}
3791
3792void
3793update_breakpoints_after_exec (void)
3794{
3795 struct breakpoint *b, *b_tmp;
3796 struct bp_location *bploc, **bplocp_tmp;
3797
3798 /* We're about to delete breakpoints from GDB's lists. If the
3799 INSERTED flag is true, GDB will try to lift the breakpoints by
3800 writing the breakpoints' "shadow contents" back into memory. The
3801 "shadow contents" are NOT valid after an exec, so GDB should not
3802 do that. Instead, the target is responsible from marking
3803 breakpoints out as soon as it detects an exec. We don't do that
3804 here instead, because there may be other attempts to delete
3805 breakpoints after detecting an exec and before reaching here. */
3806 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3807 if (bploc->pspace == current_program_space)
3808 gdb_assert (!bploc->inserted);
3809
3810 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3811 {
3812 if (b->pspace != current_program_space)
3813 continue;
3814
3815 /* Solib breakpoints must be explicitly reset after an exec(). */
3816 if (b->type == bp_shlib_event)
3817 {
3818 delete_breakpoint (b);
3819 continue;
3820 }
3821
3822 /* JIT breakpoints must be explicitly reset after an exec(). */
3823 if (b->type == bp_jit_event)
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
3829 /* Thread event breakpoints must be set anew after an exec(),
3830 as must overlay event and longjmp master breakpoints. */
3831 if (b->type == bp_thread_event || b->type == bp_overlay_event
3832 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3833 || b->type == bp_exception_master)
3834 {
3835 delete_breakpoint (b);
3836 continue;
3837 }
3838
3839 /* Step-resume breakpoints are meaningless after an exec(). */
3840 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3841 {
3842 delete_breakpoint (b);
3843 continue;
3844 }
3845
3846 /* Just like single-step breakpoints. */
3847 if (b->type == bp_single_step)
3848 {
3849 delete_breakpoint (b);
3850 continue;
3851 }
3852
3853 /* Longjmp and longjmp-resume breakpoints are also meaningless
3854 after an exec. */
3855 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3856 || b->type == bp_longjmp_call_dummy
3857 || b->type == bp_exception || b->type == bp_exception_resume)
3858 {
3859 delete_breakpoint (b);
3860 continue;
3861 }
3862
3863 if (b->type == bp_catchpoint)
3864 {
3865 /* For now, none of the bp_catchpoint breakpoints need to
3866 do anything at this point. In the future, if some of
3867 the catchpoints need to something, we will need to add
3868 a new method, and call this method from here. */
3869 continue;
3870 }
3871
3872 /* bp_finish is a special case. The only way we ought to be able
3873 to see one of these when an exec() has happened, is if the user
3874 caught a vfork, and then said "finish". Ordinarily a finish just
3875 carries them to the call-site of the current callee, by setting
3876 a temporary bp there and resuming. But in this case, the finish
3877 will carry them entirely through the vfork & exec.
3878
3879 We don't want to allow a bp_finish to remain inserted now. But
3880 we can't safely delete it, 'cause finish_command has a handle to
3881 the bp on a bpstat, and will later want to delete it. There's a
3882 chance (and I've seen it happen) that if we delete the bp_finish
3883 here, that its storage will get reused by the time finish_command
3884 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3885 We really must allow finish_command to delete a bp_finish.
3886
3887 In the absence of a general solution for the "how do we know
3888 it's safe to delete something others may have handles to?"
3889 problem, what we'll do here is just uninsert the bp_finish, and
3890 let finish_command delete it.
3891
3892 (We know the bp_finish is "doomed" in the sense that it's
3893 momentary, and will be deleted as soon as finish_command sees
3894 the inferior stopped. So it doesn't matter that the bp's
3895 address is probably bogus in the new a.out, unlike e.g., the
3896 solib breakpoints.) */
3897
3898 if (b->type == bp_finish)
3899 {
3900 continue;
3901 }
3902
3903 /* Without a symbolic address, we have little hope of the
3904 pre-exec() address meaning the same thing in the post-exec()
3905 a.out. */
3906 if (breakpoint_event_location_empty_p (b))
3907 {
3908 delete_breakpoint (b);
3909 continue;
3910 }
3911 }
3912}
3913
3914int
3915detach_breakpoints (ptid_t ptid)
3916{
3917 struct bp_location *bl, **blp_tmp;
3918 int val = 0;
3919 struct cleanup *old_chain = save_inferior_ptid ();
3920 struct inferior *inf = current_inferior ();
3921
3922 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3923 error (_("Cannot detach breakpoints of inferior_ptid"));
3924
3925 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3926 inferior_ptid = ptid;
3927 ALL_BP_LOCATIONS (bl, blp_tmp)
3928 {
3929 if (bl->pspace != inf->pspace)
3930 continue;
3931
3932 /* This function must physically remove breakpoints locations
3933 from the specified ptid, without modifying the breakpoint
3934 package's state. Locations of type bp_loc_other are only
3935 maintained at GDB side. So, there is no need to remove
3936 these bp_loc_other locations. Moreover, removing these
3937 would modify the breakpoint package's state. */
3938 if (bl->loc_type == bp_loc_other)
3939 continue;
3940
3941 if (bl->inserted)
3942 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3943 }
3944
3945 do_cleanups (old_chain);
3946 return val;
3947}
3948
3949/* Remove the breakpoint location BL from the current address space.
3950 Note that this is used to detach breakpoints from a child fork.
3951 When we get here, the child isn't in the inferior list, and neither
3952 do we have objects to represent its address space --- we should
3953 *not* look at bl->pspace->aspace here. */
3954
3955static int
3956remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3957{
3958 int val;
3959
3960 /* BL is never in moribund_locations by our callers. */
3961 gdb_assert (bl->owner != NULL);
3962
3963 /* The type of none suggests that owner is actually deleted.
3964 This should not ever happen. */
3965 gdb_assert (bl->owner->type != bp_none);
3966
3967 if (bl->loc_type == bp_loc_software_breakpoint
3968 || bl->loc_type == bp_loc_hardware_breakpoint)
3969 {
3970 /* "Normal" instruction breakpoint: either the standard
3971 trap-instruction bp (bp_breakpoint), or a
3972 bp_hardware_breakpoint. */
3973
3974 /* First check to see if we have to handle an overlay. */
3975 if (overlay_debugging == ovly_off
3976 || bl->section == NULL
3977 || !(section_is_overlay (bl->section)))
3978 {
3979 /* No overlay handling: just remove the breakpoint. */
3980
3981 /* If we're trying to uninsert a memory breakpoint that we
3982 know is set in a dynamic object that is marked
3983 shlib_disabled, then either the dynamic object was
3984 removed with "remove-symbol-file" or with
3985 "nosharedlibrary". In the former case, we don't know
3986 whether another dynamic object might have loaded over the
3987 breakpoint's address -- the user might well let us know
3988 about it next with add-symbol-file (the whole point of
3989 add-symbol-file is letting the user manually maintain a
3990 list of dynamically loaded objects). If we have the
3991 breakpoint's shadow memory, that is, this is a software
3992 breakpoint managed by GDB, check whether the breakpoint
3993 is still inserted in memory, to avoid overwriting wrong
3994 code with stale saved shadow contents. Note that HW
3995 breakpoints don't have shadow memory, as they're
3996 implemented using a mechanism that is not dependent on
3997 being able to modify the target's memory, and as such
3998 they should always be removed. */
3999 if (bl->shlib_disabled
4000 && bl->target_info.shadow_len != 0
4001 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
4002 val = 0;
4003 else
4004 val = bl->owner->ops->remove_location (bl, reason);
4005 }
4006 else
4007 {
4008 /* This breakpoint is in an overlay section.
4009 Did we set a breakpoint at the LMA? */
4010 if (!overlay_events_enabled)
4011 {
4012 /* Yes -- overlay event support is not active, so we
4013 should have set a breakpoint at the LMA. Remove it.
4014 */
4015 /* Ignore any failures: if the LMA is in ROM, we will
4016 have already warned when we failed to insert it. */
4017 if (bl->loc_type == bp_loc_hardware_breakpoint)
4018 target_remove_hw_breakpoint (bl->gdbarch,
4019 &bl->overlay_target_info);
4020 else
4021 target_remove_breakpoint (bl->gdbarch,
4022 &bl->overlay_target_info,
4023 reason);
4024 }
4025 /* Did we set a breakpoint at the VMA?
4026 If so, we will have marked the breakpoint 'inserted'. */
4027 if (bl->inserted)
4028 {
4029 /* Yes -- remove it. Previously we did not bother to
4030 remove the breakpoint if the section had been
4031 unmapped, but let's not rely on that being safe. We
4032 don't know what the overlay manager might do. */
4033
4034 /* However, we should remove *software* breakpoints only
4035 if the section is still mapped, or else we overwrite
4036 wrong code with the saved shadow contents. */
4037 if (bl->loc_type == bp_loc_hardware_breakpoint
4038 || section_is_mapped (bl->section))
4039 val = bl->owner->ops->remove_location (bl, reason);
4040 else
4041 val = 0;
4042 }
4043 else
4044 {
4045 /* No -- not inserted, so no need to remove. No error. */
4046 val = 0;
4047 }
4048 }
4049
4050 /* In some cases, we might not be able to remove a breakpoint in
4051 a shared library that has already been removed, but we have
4052 not yet processed the shlib unload event. Similarly for an
4053 unloaded add-symbol-file object - the user might not yet have
4054 had the chance to remove-symbol-file it. shlib_disabled will
4055 be set if the library/object has already been removed, but
4056 the breakpoint hasn't been uninserted yet, e.g., after
4057 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4058 always-inserted mode. */
4059 if (val
4060 && (bl->loc_type == bp_loc_software_breakpoint
4061 && (bl->shlib_disabled
4062 || solib_name_from_address (bl->pspace, bl->address)
4063 || shared_objfile_contains_address_p (bl->pspace,
4064 bl->address))))
4065 val = 0;
4066
4067 if (val)
4068 return val;
4069 bl->inserted = (reason == DETACH_BREAKPOINT);
4070 }
4071 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4072 {
4073 gdb_assert (bl->owner->ops != NULL
4074 && bl->owner->ops->remove_location != NULL);
4075
4076 bl->inserted = (reason == DETACH_BREAKPOINT);
4077 bl->owner->ops->remove_location (bl, reason);
4078
4079 /* Failure to remove any of the hardware watchpoints comes here. */
4080 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4081 warning (_("Could not remove hardware watchpoint %d."),
4082 bl->owner->number);
4083 }
4084 else if (bl->owner->type == bp_catchpoint
4085 && breakpoint_enabled (bl->owner)
4086 && !bl->duplicate)
4087 {
4088 gdb_assert (bl->owner->ops != NULL
4089 && bl->owner->ops->remove_location != NULL);
4090
4091 val = bl->owner->ops->remove_location (bl, reason);
4092 if (val)
4093 return val;
4094
4095 bl->inserted = (reason == DETACH_BREAKPOINT);
4096 }
4097
4098 return 0;
4099}
4100
4101static int
4102remove_breakpoint (struct bp_location *bl)
4103{
4104 int ret;
4105 struct cleanup *old_chain;
4106
4107 /* BL is never in moribund_locations by our callers. */
4108 gdb_assert (bl->owner != NULL);
4109
4110 /* The type of none suggests that owner is actually deleted.
4111 This should not ever happen. */
4112 gdb_assert (bl->owner->type != bp_none);
4113
4114 old_chain = save_current_space_and_thread ();
4115
4116 switch_to_program_space_and_thread (bl->pspace);
4117
4118 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4119
4120 do_cleanups (old_chain);
4121 return ret;
4122}
4123
4124/* Clear the "inserted" flag in all breakpoints. */
4125
4126void
4127mark_breakpoints_out (void)
4128{
4129 struct bp_location *bl, **blp_tmp;
4130
4131 ALL_BP_LOCATIONS (bl, blp_tmp)
4132 if (bl->pspace == current_program_space)
4133 bl->inserted = 0;
4134}
4135
4136/* Clear the "inserted" flag in all breakpoints and delete any
4137 breakpoints which should go away between runs of the program.
4138
4139 Plus other such housekeeping that has to be done for breakpoints
4140 between runs.
4141
4142 Note: this function gets called at the end of a run (by
4143 generic_mourn_inferior) and when a run begins (by
4144 init_wait_for_inferior). */
4145
4146
4147
4148void
4149breakpoint_init_inferior (enum inf_context context)
4150{
4151 struct breakpoint *b, *b_tmp;
4152 struct bp_location *bl;
4153 int ix;
4154 struct program_space *pspace = current_program_space;
4155
4156 /* If breakpoint locations are shared across processes, then there's
4157 nothing to do. */
4158 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4159 return;
4160
4161 mark_breakpoints_out ();
4162
4163 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4164 {
4165 if (b->loc && b->loc->pspace != pspace)
4166 continue;
4167
4168 switch (b->type)
4169 {
4170 case bp_call_dummy:
4171 case bp_longjmp_call_dummy:
4172
4173 /* If the call dummy breakpoint is at the entry point it will
4174 cause problems when the inferior is rerun, so we better get
4175 rid of it. */
4176
4177 case bp_watchpoint_scope:
4178
4179 /* Also get rid of scope breakpoints. */
4180
4181 case bp_shlib_event:
4182
4183 /* Also remove solib event breakpoints. Their addresses may
4184 have changed since the last time we ran the program.
4185 Actually we may now be debugging against different target;
4186 and so the solib backend that installed this breakpoint may
4187 not be used in by the target. E.g.,
4188
4189 (gdb) file prog-linux
4190 (gdb) run # native linux target
4191 ...
4192 (gdb) kill
4193 (gdb) file prog-win.exe
4194 (gdb) tar rem :9999 # remote Windows gdbserver.
4195 */
4196
4197 case bp_step_resume:
4198
4199 /* Also remove step-resume breakpoints. */
4200
4201 case bp_single_step:
4202
4203 /* Also remove single-step breakpoints. */
4204
4205 delete_breakpoint (b);
4206 break;
4207
4208 case bp_watchpoint:
4209 case bp_hardware_watchpoint:
4210 case bp_read_watchpoint:
4211 case bp_access_watchpoint:
4212 {
4213 struct watchpoint *w = (struct watchpoint *) b;
4214
4215 /* Likewise for watchpoints on local expressions. */
4216 if (w->exp_valid_block != NULL)
4217 delete_breakpoint (b);
4218 else
4219 {
4220 /* Get rid of existing locations, which are no longer
4221 valid. New ones will be created in
4222 update_watchpoint, when the inferior is restarted.
4223 The next update_global_location_list call will
4224 garbage collect them. */
4225 b->loc = NULL;
4226
4227 if (context == inf_starting)
4228 {
4229 /* Reset val field to force reread of starting value in
4230 insert_breakpoints. */
4231 if (w->val)
4232 value_free (w->val);
4233 w->val = NULL;
4234 w->val_valid = 0;
4235 }
4236 }
4237 }
4238 break;
4239 default:
4240 break;
4241 }
4242 }
4243
4244 /* Get rid of the moribund locations. */
4245 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4246 decref_bp_location (&bl);
4247 VEC_free (bp_location_p, moribund_locations);
4248}
4249
4250/* These functions concern about actual breakpoints inserted in the
4251 target --- to e.g. check if we need to do decr_pc adjustment or if
4252 we need to hop over the bkpt --- so we check for address space
4253 match, not program space. */
4254
4255/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4256 exists at PC. It returns ordinary_breakpoint_here if it's an
4257 ordinary breakpoint, or permanent_breakpoint_here if it's a
4258 permanent breakpoint.
4259 - When continuing from a location with an ordinary breakpoint, we
4260 actually single step once before calling insert_breakpoints.
4261 - When continuing from a location with a permanent breakpoint, we
4262 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4263 the target, to advance the PC past the breakpoint. */
4264
4265enum breakpoint_here
4266breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4267{
4268 struct bp_location *bl, **blp_tmp;
4269 int any_breakpoint_here = 0;
4270
4271 ALL_BP_LOCATIONS (bl, blp_tmp)
4272 {
4273 if (bl->loc_type != bp_loc_software_breakpoint
4274 && bl->loc_type != bp_loc_hardware_breakpoint)
4275 continue;
4276
4277 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4278 if ((breakpoint_enabled (bl->owner)
4279 || bl->permanent)
4280 && breakpoint_location_address_match (bl, aspace, pc))
4281 {
4282 if (overlay_debugging
4283 && section_is_overlay (bl->section)
4284 && !section_is_mapped (bl->section))
4285 continue; /* unmapped overlay -- can't be a match */
4286 else if (bl->permanent)
4287 return permanent_breakpoint_here;
4288 else
4289 any_breakpoint_here = 1;
4290 }
4291 }
4292
4293 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4294}
4295
4296/* See breakpoint.h. */
4297
4298int
4299breakpoint_in_range_p (struct address_space *aspace,
4300 CORE_ADDR addr, ULONGEST len)
4301{
4302 struct bp_location *bl, **blp_tmp;
4303
4304 ALL_BP_LOCATIONS (bl, blp_tmp)
4305 {
4306 if (bl->loc_type != bp_loc_software_breakpoint
4307 && bl->loc_type != bp_loc_hardware_breakpoint)
4308 continue;
4309
4310 if ((breakpoint_enabled (bl->owner)
4311 || bl->permanent)
4312 && breakpoint_location_address_range_overlap (bl, aspace,
4313 addr, len))
4314 {
4315 if (overlay_debugging
4316 && section_is_overlay (bl->section)
4317 && !section_is_mapped (bl->section))
4318 {
4319 /* Unmapped overlay -- can't be a match. */
4320 continue;
4321 }
4322
4323 return 1;
4324 }
4325 }
4326
4327 return 0;
4328}
4329
4330/* Return true if there's a moribund breakpoint at PC. */
4331
4332int
4333moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4334{
4335 struct bp_location *loc;
4336 int ix;
4337
4338 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4339 if (breakpoint_location_address_match (loc, aspace, pc))
4340 return 1;
4341
4342 return 0;
4343}
4344
4345/* Returns non-zero iff BL is inserted at PC, in address space
4346 ASPACE. */
4347
4348static int
4349bp_location_inserted_here_p (struct bp_location *bl,
4350 struct address_space *aspace, CORE_ADDR pc)
4351{
4352 if (bl->inserted
4353 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4354 aspace, pc))
4355 {
4356 if (overlay_debugging
4357 && section_is_overlay (bl->section)
4358 && !section_is_mapped (bl->section))
4359 return 0; /* unmapped overlay -- can't be a match */
4360 else
4361 return 1;
4362 }
4363 return 0;
4364}
4365
4366/* Returns non-zero iff there's a breakpoint inserted at PC. */
4367
4368int
4369breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4370{
4371 struct bp_location **blp, **blp_tmp = NULL;
4372
4373 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4374 {
4375 struct bp_location *bl = *blp;
4376
4377 if (bl->loc_type != bp_loc_software_breakpoint
4378 && bl->loc_type != bp_loc_hardware_breakpoint)
4379 continue;
4380
4381 if (bp_location_inserted_here_p (bl, aspace, pc))
4382 return 1;
4383 }
4384 return 0;
4385}
4386
4387/* This function returns non-zero iff there is a software breakpoint
4388 inserted at PC. */
4389
4390int
4391software_breakpoint_inserted_here_p (struct address_space *aspace,
4392 CORE_ADDR pc)
4393{
4394 struct bp_location **blp, **blp_tmp = NULL;
4395
4396 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4397 {
4398 struct bp_location *bl = *blp;
4399
4400 if (bl->loc_type != bp_loc_software_breakpoint)
4401 continue;
4402
4403 if (bp_location_inserted_here_p (bl, aspace, pc))
4404 return 1;
4405 }
4406
4407 return 0;
4408}
4409
4410/* See breakpoint.h. */
4411
4412int
4413hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4414 CORE_ADDR pc)
4415{
4416 struct bp_location **blp, **blp_tmp = NULL;
4417
4418 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4419 {
4420 struct bp_location *bl = *blp;
4421
4422 if (bl->loc_type != bp_loc_hardware_breakpoint)
4423 continue;
4424
4425 if (bp_location_inserted_here_p (bl, aspace, pc))
4426 return 1;
4427 }
4428
4429 return 0;
4430}
4431
4432int
4433hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4434 CORE_ADDR addr, ULONGEST len)
4435{
4436 struct breakpoint *bpt;
4437
4438 ALL_BREAKPOINTS (bpt)
4439 {
4440 struct bp_location *loc;
4441
4442 if (bpt->type != bp_hardware_watchpoint
4443 && bpt->type != bp_access_watchpoint)
4444 continue;
4445
4446 if (!breakpoint_enabled (bpt))
4447 continue;
4448
4449 for (loc = bpt->loc; loc; loc = loc->next)
4450 if (loc->pspace->aspace == aspace && loc->inserted)
4451 {
4452 CORE_ADDR l, h;
4453
4454 /* Check for intersection. */
4455 l = std::max<CORE_ADDR> (loc->address, addr);
4456 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4457 if (l < h)
4458 return 1;
4459 }
4460 }
4461 return 0;
4462}
4463\f
4464
4465/* bpstat stuff. External routines' interfaces are documented
4466 in breakpoint.h. */
4467
4468int
4469is_catchpoint (struct breakpoint *ep)
4470{
4471 return (ep->type == bp_catchpoint);
4472}
4473
4474/* Frees any storage that is part of a bpstat. Does not walk the
4475 'next' chain. */
4476
4477static void
4478bpstat_free (bpstat bs)
4479{
4480 if (bs->old_val != NULL)
4481 value_free (bs->old_val);
4482 decref_counted_command_line (&bs->commands);
4483 decref_bp_location (&bs->bp_location_at);
4484 xfree (bs);
4485}
4486
4487/* Clear a bpstat so that it says we are not at any breakpoint.
4488 Also free any storage that is part of a bpstat. */
4489
4490void
4491bpstat_clear (bpstat *bsp)
4492{
4493 bpstat p;
4494 bpstat q;
4495
4496 if (bsp == 0)
4497 return;
4498 p = *bsp;
4499 while (p != NULL)
4500 {
4501 q = p->next;
4502 bpstat_free (p);
4503 p = q;
4504 }
4505 *bsp = NULL;
4506}
4507
4508/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4509 is part of the bpstat is copied as well. */
4510
4511bpstat
4512bpstat_copy (bpstat bs)
4513{
4514 bpstat p = NULL;
4515 bpstat tmp;
4516 bpstat retval = NULL;
4517
4518 if (bs == NULL)
4519 return bs;
4520
4521 for (; bs != NULL; bs = bs->next)
4522 {
4523 tmp = (bpstat) xmalloc (sizeof (*tmp));
4524 memcpy (tmp, bs, sizeof (*tmp));
4525 incref_counted_command_line (tmp->commands);
4526 incref_bp_location (tmp->bp_location_at);
4527 if (bs->old_val != NULL)
4528 {
4529 tmp->old_val = value_copy (bs->old_val);
4530 release_value (tmp->old_val);
4531 }
4532
4533 if (p == NULL)
4534 /* This is the first thing in the chain. */
4535 retval = tmp;
4536 else
4537 p->next = tmp;
4538 p = tmp;
4539 }
4540 p->next = NULL;
4541 return retval;
4542}
4543
4544/* Find the bpstat associated with this breakpoint. */
4545
4546bpstat
4547bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4548{
4549 if (bsp == NULL)
4550 return NULL;
4551
4552 for (; bsp != NULL; bsp = bsp->next)
4553 {
4554 if (bsp->breakpoint_at == breakpoint)
4555 return bsp;
4556 }
4557 return NULL;
4558}
4559
4560/* See breakpoint.h. */
4561
4562int
4563bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4564{
4565 for (; bsp != NULL; bsp = bsp->next)
4566 {
4567 if (bsp->breakpoint_at == NULL)
4568 {
4569 /* A moribund location can never explain a signal other than
4570 GDB_SIGNAL_TRAP. */
4571 if (sig == GDB_SIGNAL_TRAP)
4572 return 1;
4573 }
4574 else
4575 {
4576 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4577 sig))
4578 return 1;
4579 }
4580 }
4581
4582 return 0;
4583}
4584
4585/* Put in *NUM the breakpoint number of the first breakpoint we are
4586 stopped at. *BSP upon return is a bpstat which points to the
4587 remaining breakpoints stopped at (but which is not guaranteed to be
4588 good for anything but further calls to bpstat_num).
4589
4590 Return 0 if passed a bpstat which does not indicate any breakpoints.
4591 Return -1 if stopped at a breakpoint that has been deleted since
4592 we set it.
4593 Return 1 otherwise. */
4594
4595int
4596bpstat_num (bpstat *bsp, int *num)
4597{
4598 struct breakpoint *b;
4599
4600 if ((*bsp) == NULL)
4601 return 0; /* No more breakpoint values */
4602
4603 /* We assume we'll never have several bpstats that correspond to a
4604 single breakpoint -- otherwise, this function might return the
4605 same number more than once and this will look ugly. */
4606 b = (*bsp)->breakpoint_at;
4607 *bsp = (*bsp)->next;
4608 if (b == NULL)
4609 return -1; /* breakpoint that's been deleted since */
4610
4611 *num = b->number; /* We have its number */
4612 return 1;
4613}
4614
4615/* See breakpoint.h. */
4616
4617void
4618bpstat_clear_actions (void)
4619{
4620 struct thread_info *tp;
4621 bpstat bs;
4622
4623 if (ptid_equal (inferior_ptid, null_ptid))
4624 return;
4625
4626 tp = find_thread_ptid (inferior_ptid);
4627 if (tp == NULL)
4628 return;
4629
4630 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4631 {
4632 decref_counted_command_line (&bs->commands);
4633
4634 if (bs->old_val != NULL)
4635 {
4636 value_free (bs->old_val);
4637 bs->old_val = NULL;
4638 }
4639 }
4640}
4641
4642/* Called when a command is about to proceed the inferior. */
4643
4644static void
4645breakpoint_about_to_proceed (void)
4646{
4647 if (!ptid_equal (inferior_ptid, null_ptid))
4648 {
4649 struct thread_info *tp = inferior_thread ();
4650
4651 /* Allow inferior function calls in breakpoint commands to not
4652 interrupt the command list. When the call finishes
4653 successfully, the inferior will be standing at the same
4654 breakpoint as if nothing happened. */
4655 if (tp->control.in_infcall)
4656 return;
4657 }
4658
4659 breakpoint_proceeded = 1;
4660}
4661
4662/* Stub for cleaning up our state if we error-out of a breakpoint
4663 command. */
4664static void
4665cleanup_executing_breakpoints (void *ignore)
4666{
4667 executing_breakpoint_commands = 0;
4668}
4669
4670/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4671 or its equivalent. */
4672
4673static int
4674command_line_is_silent (struct command_line *cmd)
4675{
4676 return cmd && (strcmp ("silent", cmd->line) == 0);
4677}
4678
4679/* Execute all the commands associated with all the breakpoints at
4680 this location. Any of these commands could cause the process to
4681 proceed beyond this point, etc. We look out for such changes by
4682 checking the global "breakpoint_proceeded" after each command.
4683
4684 Returns true if a breakpoint command resumed the inferior. In that
4685 case, it is the caller's responsibility to recall it again with the
4686 bpstat of the current thread. */
4687
4688static int
4689bpstat_do_actions_1 (bpstat *bsp)
4690{
4691 bpstat bs;
4692 struct cleanup *old_chain;
4693 int again = 0;
4694
4695 /* Avoid endless recursion if a `source' command is contained
4696 in bs->commands. */
4697 if (executing_breakpoint_commands)
4698 return 0;
4699
4700 executing_breakpoint_commands = 1;
4701 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4702
4703 prevent_dont_repeat ();
4704
4705 /* This pointer will iterate over the list of bpstat's. */
4706 bs = *bsp;
4707
4708 breakpoint_proceeded = 0;
4709 for (; bs != NULL; bs = bs->next)
4710 {
4711 struct counted_command_line *ccmd;
4712 struct command_line *cmd;
4713 struct cleanup *this_cmd_tree_chain;
4714
4715 /* Take ownership of the BSP's command tree, if it has one.
4716
4717 The command tree could legitimately contain commands like
4718 'step' and 'next', which call clear_proceed_status, which
4719 frees stop_bpstat's command tree. To make sure this doesn't
4720 free the tree we're executing out from under us, we need to
4721 take ownership of the tree ourselves. Since a given bpstat's
4722 commands are only executed once, we don't need to copy it; we
4723 can clear the pointer in the bpstat, and make sure we free
4724 the tree when we're done. */
4725 ccmd = bs->commands;
4726 bs->commands = NULL;
4727 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4728 cmd = ccmd ? ccmd->commands : NULL;
4729 if (command_line_is_silent (cmd))
4730 {
4731 /* The action has been already done by bpstat_stop_status. */
4732 cmd = cmd->next;
4733 }
4734
4735 while (cmd != NULL)
4736 {
4737 execute_control_command (cmd);
4738
4739 if (breakpoint_proceeded)
4740 break;
4741 else
4742 cmd = cmd->next;
4743 }
4744
4745 /* We can free this command tree now. */
4746 do_cleanups (this_cmd_tree_chain);
4747
4748 if (breakpoint_proceeded)
4749 {
4750 if (current_ui->async)
4751 /* If we are in async mode, then the target might be still
4752 running, not stopped at any breakpoint, so nothing for
4753 us to do here -- just return to the event loop. */
4754 ;
4755 else
4756 /* In sync mode, when execute_control_command returns
4757 we're already standing on the next breakpoint.
4758 Breakpoint commands for that stop were not run, since
4759 execute_command does not run breakpoint commands --
4760 only command_line_handler does, but that one is not
4761 involved in execution of breakpoint commands. So, we
4762 can now execute breakpoint commands. It should be
4763 noted that making execute_command do bpstat actions is
4764 not an option -- in this case we'll have recursive
4765 invocation of bpstat for each breakpoint with a
4766 command, and can easily blow up GDB stack. Instead, we
4767 return true, which will trigger the caller to recall us
4768 with the new stop_bpstat. */
4769 again = 1;
4770 break;
4771 }
4772 }
4773 do_cleanups (old_chain);
4774 return again;
4775}
4776
4777void
4778bpstat_do_actions (void)
4779{
4780 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4781
4782 /* Do any commands attached to breakpoint we are stopped at. */
4783 while (!ptid_equal (inferior_ptid, null_ptid)
4784 && target_has_execution
4785 && !is_exited (inferior_ptid)
4786 && !is_executing (inferior_ptid))
4787 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4788 and only return when it is stopped at the next breakpoint, we
4789 keep doing breakpoint actions until it returns false to
4790 indicate the inferior was not resumed. */
4791 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4792 break;
4793
4794 discard_cleanups (cleanup_if_error);
4795}
4796
4797/* Print out the (old or new) value associated with a watchpoint. */
4798
4799static void
4800watchpoint_value_print (struct value *val, struct ui_file *stream)
4801{
4802 if (val == NULL)
4803 fprintf_unfiltered (stream, _("<unreadable>"));
4804 else
4805 {
4806 struct value_print_options opts;
4807 get_user_print_options (&opts);
4808 value_print (val, stream, &opts);
4809 }
4810}
4811
4812/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4813 debugging multiple threads. */
4814
4815void
4816maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4817{
4818 if (ui_out_is_mi_like_p (uiout))
4819 return;
4820
4821 ui_out_text (uiout, "\n");
4822
4823 if (show_thread_that_caused_stop ())
4824 {
4825 const char *name;
4826 struct thread_info *thr = inferior_thread ();
4827
4828 ui_out_text (uiout, "Thread ");
4829 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4830
4831 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4832 if (name != NULL)
4833 {
4834 ui_out_text (uiout, " \"");
4835 ui_out_field_fmt (uiout, "name", "%s", name);
4836 ui_out_text (uiout, "\"");
4837 }
4838
4839 ui_out_text (uiout, " hit ");
4840 }
4841}
4842
4843/* Generic routine for printing messages indicating why we
4844 stopped. The behavior of this function depends on the value
4845 'print_it' in the bpstat structure. Under some circumstances we
4846 may decide not to print anything here and delegate the task to
4847 normal_stop(). */
4848
4849static enum print_stop_action
4850print_bp_stop_message (bpstat bs)
4851{
4852 switch (bs->print_it)
4853 {
4854 case print_it_noop:
4855 /* Nothing should be printed for this bpstat entry. */
4856 return PRINT_UNKNOWN;
4857 break;
4858
4859 case print_it_done:
4860 /* We still want to print the frame, but we already printed the
4861 relevant messages. */
4862 return PRINT_SRC_AND_LOC;
4863 break;
4864
4865 case print_it_normal:
4866 {
4867 struct breakpoint *b = bs->breakpoint_at;
4868
4869 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4870 which has since been deleted. */
4871 if (b == NULL)
4872 return PRINT_UNKNOWN;
4873
4874 /* Normal case. Call the breakpoint's print_it method. */
4875 return b->ops->print_it (bs);
4876 }
4877 break;
4878
4879 default:
4880 internal_error (__FILE__, __LINE__,
4881 _("print_bp_stop_message: unrecognized enum value"));
4882 break;
4883 }
4884}
4885
4886/* A helper function that prints a shared library stopped event. */
4887
4888static void
4889print_solib_event (int is_catchpoint)
4890{
4891 int any_deleted
4892 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4893 int any_added
4894 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4895
4896 if (!is_catchpoint)
4897 {
4898 if (any_added || any_deleted)
4899 ui_out_text (current_uiout,
4900 _("Stopped due to shared library event:\n"));
4901 else
4902 ui_out_text (current_uiout,
4903 _("Stopped due to shared library event (no "
4904 "libraries added or removed)\n"));
4905 }
4906
4907 if (ui_out_is_mi_like_p (current_uiout))
4908 ui_out_field_string (current_uiout, "reason",
4909 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4910
4911 if (any_deleted)
4912 {
4913 struct cleanup *cleanup;
4914 char *name;
4915 int ix;
4916
4917 ui_out_text (current_uiout, _(" Inferior unloaded "));
4918 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4919 "removed");
4920 for (ix = 0;
4921 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4922 ix, name);
4923 ++ix)
4924 {
4925 if (ix > 0)
4926 ui_out_text (current_uiout, " ");
4927 ui_out_field_string (current_uiout, "library", name);
4928 ui_out_text (current_uiout, "\n");
4929 }
4930
4931 do_cleanups (cleanup);
4932 }
4933
4934 if (any_added)
4935 {
4936 struct so_list *iter;
4937 int ix;
4938 struct cleanup *cleanup;
4939
4940 ui_out_text (current_uiout, _(" Inferior loaded "));
4941 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4942 "added");
4943 for (ix = 0;
4944 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4945 ix, iter);
4946 ++ix)
4947 {
4948 if (ix > 0)
4949 ui_out_text (current_uiout, " ");
4950 ui_out_field_string (current_uiout, "library", iter->so_name);
4951 ui_out_text (current_uiout, "\n");
4952 }
4953
4954 do_cleanups (cleanup);
4955 }
4956}
4957
4958/* Print a message indicating what happened. This is called from
4959 normal_stop(). The input to this routine is the head of the bpstat
4960 list - a list of the eventpoints that caused this stop. KIND is
4961 the target_waitkind for the stopping event. This
4962 routine calls the generic print routine for printing a message
4963 about reasons for stopping. This will print (for example) the
4964 "Breakpoint n," part of the output. The return value of this
4965 routine is one of:
4966
4967 PRINT_UNKNOWN: Means we printed nothing.
4968 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4969 code to print the location. An example is
4970 "Breakpoint 1, " which should be followed by
4971 the location.
4972 PRINT_SRC_ONLY: Means we printed something, but there is no need
4973 to also print the location part of the message.
4974 An example is the catch/throw messages, which
4975 don't require a location appended to the end.
4976 PRINT_NOTHING: We have done some printing and we don't need any
4977 further info to be printed. */
4978
4979enum print_stop_action
4980bpstat_print (bpstat bs, int kind)
4981{
4982 enum print_stop_action val;
4983
4984 /* Maybe another breakpoint in the chain caused us to stop.
4985 (Currently all watchpoints go on the bpstat whether hit or not.
4986 That probably could (should) be changed, provided care is taken
4987 with respect to bpstat_explains_signal). */
4988 for (; bs; bs = bs->next)
4989 {
4990 val = print_bp_stop_message (bs);
4991 if (val == PRINT_SRC_ONLY
4992 || val == PRINT_SRC_AND_LOC
4993 || val == PRINT_NOTHING)
4994 return val;
4995 }
4996
4997 /* If we had hit a shared library event breakpoint,
4998 print_bp_stop_message would print out this message. If we hit an
4999 OS-level shared library event, do the same thing. */
5000 if (kind == TARGET_WAITKIND_LOADED)
5001 {
5002 print_solib_event (0);
5003 return PRINT_NOTHING;
5004 }
5005
5006 /* We reached the end of the chain, or we got a null BS to start
5007 with and nothing was printed. */
5008 return PRINT_UNKNOWN;
5009}
5010
5011/* Evaluate the expression EXP and return 1 if value is zero.
5012 This returns the inverse of the condition because it is called
5013 from catch_errors which returns 0 if an exception happened, and if an
5014 exception happens we want execution to stop.
5015 The argument is a "struct expression *" that has been cast to a
5016 "void *" to make it pass through catch_errors. */
5017
5018static int
5019breakpoint_cond_eval (void *exp)
5020{
5021 struct value *mark = value_mark ();
5022 int i = !value_true (evaluate_expression ((struct expression *) exp));
5023
5024 value_free_to_mark (mark);
5025 return i;
5026}
5027
5028/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5029
5030static bpstat
5031bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5032{
5033 bpstat bs;
5034
5035 bs = (bpstat) xmalloc (sizeof (*bs));
5036 bs->next = NULL;
5037 **bs_link_pointer = bs;
5038 *bs_link_pointer = &bs->next;
5039 bs->breakpoint_at = bl->owner;
5040 bs->bp_location_at = bl;
5041 incref_bp_location (bl);
5042 /* If the condition is false, etc., don't do the commands. */
5043 bs->commands = NULL;
5044 bs->old_val = NULL;
5045 bs->print_it = print_it_normal;
5046 return bs;
5047}
5048\f
5049/* The target has stopped with waitstatus WS. Check if any hardware
5050 watchpoints have triggered, according to the target. */
5051
5052int
5053watchpoints_triggered (struct target_waitstatus *ws)
5054{
5055 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5056 CORE_ADDR addr;
5057 struct breakpoint *b;
5058
5059 if (!stopped_by_watchpoint)
5060 {
5061 /* We were not stopped by a watchpoint. Mark all watchpoints
5062 as not triggered. */
5063 ALL_BREAKPOINTS (b)
5064 if (is_hardware_watchpoint (b))
5065 {
5066 struct watchpoint *w = (struct watchpoint *) b;
5067
5068 w->watchpoint_triggered = watch_triggered_no;
5069 }
5070
5071 return 0;
5072 }
5073
5074 if (!target_stopped_data_address (&current_target, &addr))
5075 {
5076 /* We were stopped by a watchpoint, but we don't know where.
5077 Mark all watchpoints as unknown. */
5078 ALL_BREAKPOINTS (b)
5079 if (is_hardware_watchpoint (b))
5080 {
5081 struct watchpoint *w = (struct watchpoint *) b;
5082
5083 w->watchpoint_triggered = watch_triggered_unknown;
5084 }
5085
5086 return 1;
5087 }
5088
5089 /* The target could report the data address. Mark watchpoints
5090 affected by this data address as triggered, and all others as not
5091 triggered. */
5092
5093 ALL_BREAKPOINTS (b)
5094 if (is_hardware_watchpoint (b))
5095 {
5096 struct watchpoint *w = (struct watchpoint *) b;
5097 struct bp_location *loc;
5098
5099 w->watchpoint_triggered = watch_triggered_no;
5100 for (loc = b->loc; loc; loc = loc->next)
5101 {
5102 if (is_masked_watchpoint (b))
5103 {
5104 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5105 CORE_ADDR start = loc->address & w->hw_wp_mask;
5106
5107 if (newaddr == start)
5108 {
5109 w->watchpoint_triggered = watch_triggered_yes;
5110 break;
5111 }
5112 }
5113 /* Exact match not required. Within range is sufficient. */
5114 else if (target_watchpoint_addr_within_range (&current_target,
5115 addr, loc->address,
5116 loc->length))
5117 {
5118 w->watchpoint_triggered = watch_triggered_yes;
5119 break;
5120 }
5121 }
5122 }
5123
5124 return 1;
5125}
5126
5127/* Possible return values for watchpoint_check (this can't be an enum
5128 because of check_errors). */
5129/* The watchpoint has been deleted. */
5130#define WP_DELETED 1
5131/* The value has changed. */
5132#define WP_VALUE_CHANGED 2
5133/* The value has not changed. */
5134#define WP_VALUE_NOT_CHANGED 3
5135/* Ignore this watchpoint, no matter if the value changed or not. */
5136#define WP_IGNORE 4
5137
5138#define BP_TEMPFLAG 1
5139#define BP_HARDWAREFLAG 2
5140
5141/* Evaluate watchpoint condition expression and check if its value
5142 changed.
5143
5144 P should be a pointer to struct bpstat, but is defined as a void *
5145 in order for this function to be usable with catch_errors. */
5146
5147static int
5148watchpoint_check (void *p)
5149{
5150 bpstat bs = (bpstat) p;
5151 struct watchpoint *b;
5152 struct frame_info *fr;
5153 int within_current_scope;
5154
5155 /* BS is built from an existing struct breakpoint. */
5156 gdb_assert (bs->breakpoint_at != NULL);
5157 b = (struct watchpoint *) bs->breakpoint_at;
5158
5159 /* If this is a local watchpoint, we only want to check if the
5160 watchpoint frame is in scope if the current thread is the thread
5161 that was used to create the watchpoint. */
5162 if (!watchpoint_in_thread_scope (b))
5163 return WP_IGNORE;
5164
5165 if (b->exp_valid_block == NULL)
5166 within_current_scope = 1;
5167 else
5168 {
5169 struct frame_info *frame = get_current_frame ();
5170 struct gdbarch *frame_arch = get_frame_arch (frame);
5171 CORE_ADDR frame_pc = get_frame_pc (frame);
5172
5173 /* stack_frame_destroyed_p() returns a non-zero value if we're
5174 still in the function but the stack frame has already been
5175 invalidated. Since we can't rely on the values of local
5176 variables after the stack has been destroyed, we are treating
5177 the watchpoint in that state as `not changed' without further
5178 checking. Don't mark watchpoints as changed if the current
5179 frame is in an epilogue - even if they are in some other
5180 frame, our view of the stack is likely to be wrong and
5181 frame_find_by_id could error out. */
5182 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5183 return WP_IGNORE;
5184
5185 fr = frame_find_by_id (b->watchpoint_frame);
5186 within_current_scope = (fr != NULL);
5187
5188 /* If we've gotten confused in the unwinder, we might have
5189 returned a frame that can't describe this variable. */
5190 if (within_current_scope)
5191 {
5192 struct symbol *function;
5193
5194 function = get_frame_function (fr);
5195 if (function == NULL
5196 || !contained_in (b->exp_valid_block,
5197 SYMBOL_BLOCK_VALUE (function)))
5198 within_current_scope = 0;
5199 }
5200
5201 if (within_current_scope)
5202 /* If we end up stopping, the current frame will get selected
5203 in normal_stop. So this call to select_frame won't affect
5204 the user. */
5205 select_frame (fr);
5206 }
5207
5208 if (within_current_scope)
5209 {
5210 /* We use value_{,free_to_}mark because it could be a *long*
5211 time before we return to the command level and call
5212 free_all_values. We can't call free_all_values because we
5213 might be in the middle of evaluating a function call. */
5214
5215 int pc = 0;
5216 struct value *mark;
5217 struct value *new_val;
5218
5219 if (is_masked_watchpoint (&b->base))
5220 /* Since we don't know the exact trigger address (from
5221 stopped_data_address), just tell the user we've triggered
5222 a mask watchpoint. */
5223 return WP_VALUE_CHANGED;
5224
5225 mark = value_mark ();
5226 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5227
5228 if (b->val_bitsize != 0)
5229 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5230
5231 /* We use value_equal_contents instead of value_equal because
5232 the latter coerces an array to a pointer, thus comparing just
5233 the address of the array instead of its contents. This is
5234 not what we want. */
5235 if ((b->val != NULL) != (new_val != NULL)
5236 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5237 {
5238 if (new_val != NULL)
5239 {
5240 release_value (new_val);
5241 value_free_to_mark (mark);
5242 }
5243 bs->old_val = b->val;
5244 b->val = new_val;
5245 b->val_valid = 1;
5246 return WP_VALUE_CHANGED;
5247 }
5248 else
5249 {
5250 /* Nothing changed. */
5251 value_free_to_mark (mark);
5252 return WP_VALUE_NOT_CHANGED;
5253 }
5254 }
5255 else
5256 {
5257 /* This seems like the only logical thing to do because
5258 if we temporarily ignored the watchpoint, then when
5259 we reenter the block in which it is valid it contains
5260 garbage (in the case of a function, it may have two
5261 garbage values, one before and one after the prologue).
5262 So we can't even detect the first assignment to it and
5263 watch after that (since the garbage may or may not equal
5264 the first value assigned). */
5265 /* We print all the stop information in
5266 breakpoint_ops->print_it, but in this case, by the time we
5267 call breakpoint_ops->print_it this bp will be deleted
5268 already. So we have no choice but print the information
5269 here. */
5270
5271 SWITCH_THRU_ALL_UIS ()
5272 {
5273 struct ui_out *uiout = current_uiout;
5274
5275 if (ui_out_is_mi_like_p (uiout))
5276 ui_out_field_string
5277 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5278 ui_out_text (uiout, "\nWatchpoint ");
5279 ui_out_field_int (uiout, "wpnum", b->base.number);
5280 ui_out_text (uiout,
5281 " deleted because the program has left the block in\n"
5282 "which its expression is valid.\n");
5283 }
5284
5285 /* Make sure the watchpoint's commands aren't executed. */
5286 decref_counted_command_line (&b->base.commands);
5287 watchpoint_del_at_next_stop (b);
5288
5289 return WP_DELETED;
5290 }
5291}
5292
5293/* Return true if it looks like target has stopped due to hitting
5294 breakpoint location BL. This function does not check if we should
5295 stop, only if BL explains the stop. */
5296
5297static int
5298bpstat_check_location (const struct bp_location *bl,
5299 struct address_space *aspace, CORE_ADDR bp_addr,
5300 const struct target_waitstatus *ws)
5301{
5302 struct breakpoint *b = bl->owner;
5303
5304 /* BL is from an existing breakpoint. */
5305 gdb_assert (b != NULL);
5306
5307 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5308}
5309
5310/* Determine if the watched values have actually changed, and we
5311 should stop. If not, set BS->stop to 0. */
5312
5313static void
5314bpstat_check_watchpoint (bpstat bs)
5315{
5316 const struct bp_location *bl;
5317 struct watchpoint *b;
5318
5319 /* BS is built for existing struct breakpoint. */
5320 bl = bs->bp_location_at;
5321 gdb_assert (bl != NULL);
5322 b = (struct watchpoint *) bs->breakpoint_at;
5323 gdb_assert (b != NULL);
5324
5325 {
5326 int must_check_value = 0;
5327
5328 if (b->base.type == bp_watchpoint)
5329 /* For a software watchpoint, we must always check the
5330 watched value. */
5331 must_check_value = 1;
5332 else if (b->watchpoint_triggered == watch_triggered_yes)
5333 /* We have a hardware watchpoint (read, write, or access)
5334 and the target earlier reported an address watched by
5335 this watchpoint. */
5336 must_check_value = 1;
5337 else if (b->watchpoint_triggered == watch_triggered_unknown
5338 && b->base.type == bp_hardware_watchpoint)
5339 /* We were stopped by a hardware watchpoint, but the target could
5340 not report the data address. We must check the watchpoint's
5341 value. Access and read watchpoints are out of luck; without
5342 a data address, we can't figure it out. */
5343 must_check_value = 1;
5344
5345 if (must_check_value)
5346 {
5347 char *message
5348 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5349 b->base.number);
5350 struct cleanup *cleanups = make_cleanup (xfree, message);
5351 int e = catch_errors (watchpoint_check, bs, message,
5352 RETURN_MASK_ALL);
5353 do_cleanups (cleanups);
5354 switch (e)
5355 {
5356 case WP_DELETED:
5357 /* We've already printed what needs to be printed. */
5358 bs->print_it = print_it_done;
5359 /* Stop. */
5360 break;
5361 case WP_IGNORE:
5362 bs->print_it = print_it_noop;
5363 bs->stop = 0;
5364 break;
5365 case WP_VALUE_CHANGED:
5366 if (b->base.type == bp_read_watchpoint)
5367 {
5368 /* There are two cases to consider here:
5369
5370 1. We're watching the triggered memory for reads.
5371 In that case, trust the target, and always report
5372 the watchpoint hit to the user. Even though
5373 reads don't cause value changes, the value may
5374 have changed since the last time it was read, and
5375 since we're not trapping writes, we will not see
5376 those, and as such we should ignore our notion of
5377 old value.
5378
5379 2. We're watching the triggered memory for both
5380 reads and writes. There are two ways this may
5381 happen:
5382
5383 2.1. This is a target that can't break on data
5384 reads only, but can break on accesses (reads or
5385 writes), such as e.g., x86. We detect this case
5386 at the time we try to insert read watchpoints.
5387
5388 2.2. Otherwise, the target supports read
5389 watchpoints, but, the user set an access or write
5390 watchpoint watching the same memory as this read
5391 watchpoint.
5392
5393 If we're watching memory writes as well as reads,
5394 ignore watchpoint hits when we find that the
5395 value hasn't changed, as reads don't cause
5396 changes. This still gives false positives when
5397 the program writes the same value to memory as
5398 what there was already in memory (we will confuse
5399 it for a read), but it's much better than
5400 nothing. */
5401
5402 int other_write_watchpoint = 0;
5403
5404 if (bl->watchpoint_type == hw_read)
5405 {
5406 struct breakpoint *other_b;
5407
5408 ALL_BREAKPOINTS (other_b)
5409 if (other_b->type == bp_hardware_watchpoint
5410 || other_b->type == bp_access_watchpoint)
5411 {
5412 struct watchpoint *other_w =
5413 (struct watchpoint *) other_b;
5414
5415 if (other_w->watchpoint_triggered
5416 == watch_triggered_yes)
5417 {
5418 other_write_watchpoint = 1;
5419 break;
5420 }
5421 }
5422 }
5423
5424 if (other_write_watchpoint
5425 || bl->watchpoint_type == hw_access)
5426 {
5427 /* We're watching the same memory for writes,
5428 and the value changed since the last time we
5429 updated it, so this trap must be for a write.
5430 Ignore it. */
5431 bs->print_it = print_it_noop;
5432 bs->stop = 0;
5433 }
5434 }
5435 break;
5436 case WP_VALUE_NOT_CHANGED:
5437 if (b->base.type == bp_hardware_watchpoint
5438 || b->base.type == bp_watchpoint)
5439 {
5440 /* Don't stop: write watchpoints shouldn't fire if
5441 the value hasn't changed. */
5442 bs->print_it = print_it_noop;
5443 bs->stop = 0;
5444 }
5445 /* Stop. */
5446 break;
5447 default:
5448 /* Can't happen. */
5449 case 0:
5450 /* Error from catch_errors. */
5451 {
5452 SWITCH_THRU_ALL_UIS ()
5453 {
5454 printf_filtered (_("Watchpoint %d deleted.\n"),
5455 b->base.number);
5456 }
5457 watchpoint_del_at_next_stop (b);
5458 /* We've already printed what needs to be printed. */
5459 bs->print_it = print_it_done;
5460 }
5461 break;
5462 }
5463 }
5464 else /* must_check_value == 0 */
5465 {
5466 /* This is a case where some watchpoint(s) triggered, but
5467 not at the address of this watchpoint, or else no
5468 watchpoint triggered after all. So don't print
5469 anything for this watchpoint. */
5470 bs->print_it = print_it_noop;
5471 bs->stop = 0;
5472 }
5473 }
5474}
5475
5476/* For breakpoints that are currently marked as telling gdb to stop,
5477 check conditions (condition proper, frame, thread and ignore count)
5478 of breakpoint referred to by BS. If we should not stop for this
5479 breakpoint, set BS->stop to 0. */
5480
5481static void
5482bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5483{
5484 const struct bp_location *bl;
5485 struct breakpoint *b;
5486 int value_is_zero = 0;
5487 struct expression *cond;
5488
5489 gdb_assert (bs->stop);
5490
5491 /* BS is built for existing struct breakpoint. */
5492 bl = bs->bp_location_at;
5493 gdb_assert (bl != NULL);
5494 b = bs->breakpoint_at;
5495 gdb_assert (b != NULL);
5496
5497 /* Even if the target evaluated the condition on its end and notified GDB, we
5498 need to do so again since GDB does not know if we stopped due to a
5499 breakpoint or a single step breakpoint. */
5500
5501 if (frame_id_p (b->frame_id)
5502 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5503 {
5504 bs->stop = 0;
5505 return;
5506 }
5507
5508 /* If this is a thread/task-specific breakpoint, don't waste cpu
5509 evaluating the condition if this isn't the specified
5510 thread/task. */
5511 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5512 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5513
5514 {
5515 bs->stop = 0;
5516 return;
5517 }
5518
5519 /* Evaluate extension language breakpoints that have a "stop" method
5520 implemented. */
5521 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5522
5523 if (is_watchpoint (b))
5524 {
5525 struct watchpoint *w = (struct watchpoint *) b;
5526
5527 cond = w->cond_exp;
5528 }
5529 else
5530 cond = bl->cond;
5531
5532 if (cond && b->disposition != disp_del_at_next_stop)
5533 {
5534 int within_current_scope = 1;
5535 struct watchpoint * w;
5536
5537 /* We use value_mark and value_free_to_mark because it could
5538 be a long time before we return to the command level and
5539 call free_all_values. We can't call free_all_values
5540 because we might be in the middle of evaluating a
5541 function call. */
5542 struct value *mark = value_mark ();
5543
5544 if (is_watchpoint (b))
5545 w = (struct watchpoint *) b;
5546 else
5547 w = NULL;
5548
5549 /* Need to select the frame, with all that implies so that
5550 the conditions will have the right context. Because we
5551 use the frame, we will not see an inlined function's
5552 variables when we arrive at a breakpoint at the start
5553 of the inlined function; the current frame will be the
5554 call site. */
5555 if (w == NULL || w->cond_exp_valid_block == NULL)
5556 select_frame (get_current_frame ());
5557 else
5558 {
5559 struct frame_info *frame;
5560
5561 /* For local watchpoint expressions, which particular
5562 instance of a local is being watched matters, so we
5563 keep track of the frame to evaluate the expression
5564 in. To evaluate the condition however, it doesn't
5565 really matter which instantiation of the function
5566 where the condition makes sense triggers the
5567 watchpoint. This allows an expression like "watch
5568 global if q > 10" set in `func', catch writes to
5569 global on all threads that call `func', or catch
5570 writes on all recursive calls of `func' by a single
5571 thread. We simply always evaluate the condition in
5572 the innermost frame that's executing where it makes
5573 sense to evaluate the condition. It seems
5574 intuitive. */
5575 frame = block_innermost_frame (w->cond_exp_valid_block);
5576 if (frame != NULL)
5577 select_frame (frame);
5578 else
5579 within_current_scope = 0;
5580 }
5581 if (within_current_scope)
5582 value_is_zero
5583 = catch_errors (breakpoint_cond_eval, cond,
5584 "Error in testing breakpoint condition:\n",
5585 RETURN_MASK_ALL);
5586 else
5587 {
5588 warning (_("Watchpoint condition cannot be tested "
5589 "in the current scope"));
5590 /* If we failed to set the right context for this
5591 watchpoint, unconditionally report it. */
5592 value_is_zero = 0;
5593 }
5594 /* FIXME-someday, should give breakpoint #. */
5595 value_free_to_mark (mark);
5596 }
5597
5598 if (cond && value_is_zero)
5599 {
5600 bs->stop = 0;
5601 }
5602 else if (b->ignore_count > 0)
5603 {
5604 b->ignore_count--;
5605 bs->stop = 0;
5606 /* Increase the hit count even though we don't stop. */
5607 ++(b->hit_count);
5608 observer_notify_breakpoint_modified (b);
5609 }
5610}
5611
5612/* Returns true if we need to track moribund locations of LOC's type
5613 on the current target. */
5614
5615static int
5616need_moribund_for_location_type (struct bp_location *loc)
5617{
5618 return ((loc->loc_type == bp_loc_software_breakpoint
5619 && !target_supports_stopped_by_sw_breakpoint ())
5620 || (loc->loc_type == bp_loc_hardware_breakpoint
5621 && !target_supports_stopped_by_hw_breakpoint ()));
5622}
5623
5624
5625/* Get a bpstat associated with having just stopped at address
5626 BP_ADDR in thread PTID.
5627
5628 Determine whether we stopped at a breakpoint, etc, or whether we
5629 don't understand this stop. Result is a chain of bpstat's such
5630 that:
5631
5632 if we don't understand the stop, the result is a null pointer.
5633
5634 if we understand why we stopped, the result is not null.
5635
5636 Each element of the chain refers to a particular breakpoint or
5637 watchpoint at which we have stopped. (We may have stopped for
5638 several reasons concurrently.)
5639
5640 Each element of the chain has valid next, breakpoint_at,
5641 commands, FIXME??? fields. */
5642
5643bpstat
5644bpstat_stop_status (struct address_space *aspace,
5645 CORE_ADDR bp_addr, ptid_t ptid,
5646 const struct target_waitstatus *ws)
5647{
5648 struct breakpoint *b = NULL;
5649 struct bp_location *bl;
5650 struct bp_location *loc;
5651 /* First item of allocated bpstat's. */
5652 bpstat bs_head = NULL, *bs_link = &bs_head;
5653 /* Pointer to the last thing in the chain currently. */
5654 bpstat bs;
5655 int ix;
5656 int need_remove_insert;
5657 int removed_any;
5658
5659 /* First, build the bpstat chain with locations that explain a
5660 target stop, while being careful to not set the target running,
5661 as that may invalidate locations (in particular watchpoint
5662 locations are recreated). Resuming will happen here with
5663 breakpoint conditions or watchpoint expressions that include
5664 inferior function calls. */
5665
5666 ALL_BREAKPOINTS (b)
5667 {
5668 if (!breakpoint_enabled (b))
5669 continue;
5670
5671 for (bl = b->loc; bl != NULL; bl = bl->next)
5672 {
5673 /* For hardware watchpoints, we look only at the first
5674 location. The watchpoint_check function will work on the
5675 entire expression, not the individual locations. For
5676 read watchpoints, the watchpoints_triggered function has
5677 checked all locations already. */
5678 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5679 break;
5680
5681 if (!bl->enabled || bl->shlib_disabled)
5682 continue;
5683
5684 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5685 continue;
5686
5687 /* Come here if it's a watchpoint, or if the break address
5688 matches. */
5689
5690 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5691 explain stop. */
5692
5693 /* Assume we stop. Should we find a watchpoint that is not
5694 actually triggered, or if the condition of the breakpoint
5695 evaluates as false, we'll reset 'stop' to 0. */
5696 bs->stop = 1;
5697 bs->print = 1;
5698
5699 /* If this is a scope breakpoint, mark the associated
5700 watchpoint as triggered so that we will handle the
5701 out-of-scope event. We'll get to the watchpoint next
5702 iteration. */
5703 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5704 {
5705 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5706
5707 w->watchpoint_triggered = watch_triggered_yes;
5708 }
5709 }
5710 }
5711
5712 /* Check if a moribund breakpoint explains the stop. */
5713 if (!target_supports_stopped_by_sw_breakpoint ()
5714 || !target_supports_stopped_by_hw_breakpoint ())
5715 {
5716 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5717 {
5718 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5719 && need_moribund_for_location_type (loc))
5720 {
5721 bs = bpstat_alloc (loc, &bs_link);
5722 /* For hits of moribund locations, we should just proceed. */
5723 bs->stop = 0;
5724 bs->print = 0;
5725 bs->print_it = print_it_noop;
5726 }
5727 }
5728 }
5729
5730 /* A bit of special processing for shlib breakpoints. We need to
5731 process solib loading here, so that the lists of loaded and
5732 unloaded libraries are correct before we handle "catch load" and
5733 "catch unload". */
5734 for (bs = bs_head; bs != NULL; bs = bs->next)
5735 {
5736 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5737 {
5738 handle_solib_event ();
5739 break;
5740 }
5741 }
5742
5743 /* Now go through the locations that caused the target to stop, and
5744 check whether we're interested in reporting this stop to higher
5745 layers, or whether we should resume the target transparently. */
5746
5747 removed_any = 0;
5748
5749 for (bs = bs_head; bs != NULL; bs = bs->next)
5750 {
5751 if (!bs->stop)
5752 continue;
5753
5754 b = bs->breakpoint_at;
5755 b->ops->check_status (bs);
5756 if (bs->stop)
5757 {
5758 bpstat_check_breakpoint_conditions (bs, ptid);
5759
5760 if (bs->stop)
5761 {
5762 ++(b->hit_count);
5763 observer_notify_breakpoint_modified (b);
5764
5765 /* We will stop here. */
5766 if (b->disposition == disp_disable)
5767 {
5768 --(b->enable_count);
5769 if (b->enable_count <= 0)
5770 b->enable_state = bp_disabled;
5771 removed_any = 1;
5772 }
5773 if (b->silent)
5774 bs->print = 0;
5775 bs->commands = b->commands;
5776 incref_counted_command_line (bs->commands);
5777 if (command_line_is_silent (bs->commands
5778 ? bs->commands->commands : NULL))
5779 bs->print = 0;
5780
5781 b->ops->after_condition_true (bs);
5782 }
5783
5784 }
5785
5786 /* Print nothing for this entry if we don't stop or don't
5787 print. */
5788 if (!bs->stop || !bs->print)
5789 bs->print_it = print_it_noop;
5790 }
5791
5792 /* If we aren't stopping, the value of some hardware watchpoint may
5793 not have changed, but the intermediate memory locations we are
5794 watching may have. Don't bother if we're stopping; this will get
5795 done later. */
5796 need_remove_insert = 0;
5797 if (! bpstat_causes_stop (bs_head))
5798 for (bs = bs_head; bs != NULL; bs = bs->next)
5799 if (!bs->stop
5800 && bs->breakpoint_at
5801 && is_hardware_watchpoint (bs->breakpoint_at))
5802 {
5803 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5804
5805 update_watchpoint (w, 0 /* don't reparse. */);
5806 need_remove_insert = 1;
5807 }
5808
5809 if (need_remove_insert)
5810 update_global_location_list (UGLL_MAY_INSERT);
5811 else if (removed_any)
5812 update_global_location_list (UGLL_DONT_INSERT);
5813
5814 return bs_head;
5815}
5816
5817static void
5818handle_jit_event (void)
5819{
5820 struct frame_info *frame;
5821 struct gdbarch *gdbarch;
5822
5823 if (debug_infrun)
5824 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5825
5826 /* Switch terminal for any messages produced by
5827 breakpoint_re_set. */
5828 target_terminal_ours_for_output ();
5829
5830 frame = get_current_frame ();
5831 gdbarch = get_frame_arch (frame);
5832
5833 jit_event_handler (gdbarch);
5834
5835 target_terminal_inferior ();
5836}
5837
5838/* Prepare WHAT final decision for infrun. */
5839
5840/* Decide what infrun needs to do with this bpstat. */
5841
5842struct bpstat_what
5843bpstat_what (bpstat bs_head)
5844{
5845 struct bpstat_what retval;
5846 bpstat bs;
5847
5848 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5849 retval.call_dummy = STOP_NONE;
5850 retval.is_longjmp = 0;
5851
5852 for (bs = bs_head; bs != NULL; bs = bs->next)
5853 {
5854 /* Extract this BS's action. After processing each BS, we check
5855 if its action overrides all we've seem so far. */
5856 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5857 enum bptype bptype;
5858
5859 if (bs->breakpoint_at == NULL)
5860 {
5861 /* I suspect this can happen if it was a momentary
5862 breakpoint which has since been deleted. */
5863 bptype = bp_none;
5864 }
5865 else
5866 bptype = bs->breakpoint_at->type;
5867
5868 switch (bptype)
5869 {
5870 case bp_none:
5871 break;
5872 case bp_breakpoint:
5873 case bp_hardware_breakpoint:
5874 case bp_single_step:
5875 case bp_until:
5876 case bp_finish:
5877 case bp_shlib_event:
5878 if (bs->stop)
5879 {
5880 if (bs->print)
5881 this_action = BPSTAT_WHAT_STOP_NOISY;
5882 else
5883 this_action = BPSTAT_WHAT_STOP_SILENT;
5884 }
5885 else
5886 this_action = BPSTAT_WHAT_SINGLE;
5887 break;
5888 case bp_watchpoint:
5889 case bp_hardware_watchpoint:
5890 case bp_read_watchpoint:
5891 case bp_access_watchpoint:
5892 if (bs->stop)
5893 {
5894 if (bs->print)
5895 this_action = BPSTAT_WHAT_STOP_NOISY;
5896 else
5897 this_action = BPSTAT_WHAT_STOP_SILENT;
5898 }
5899 else
5900 {
5901 /* There was a watchpoint, but we're not stopping.
5902 This requires no further action. */
5903 }
5904 break;
5905 case bp_longjmp:
5906 case bp_longjmp_call_dummy:
5907 case bp_exception:
5908 if (bs->stop)
5909 {
5910 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5911 retval.is_longjmp = bptype != bp_exception;
5912 }
5913 else
5914 this_action = BPSTAT_WHAT_SINGLE;
5915 break;
5916 case bp_longjmp_resume:
5917 case bp_exception_resume:
5918 if (bs->stop)
5919 {
5920 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5921 retval.is_longjmp = bptype == bp_longjmp_resume;
5922 }
5923 else
5924 this_action = BPSTAT_WHAT_SINGLE;
5925 break;
5926 case bp_step_resume:
5927 if (bs->stop)
5928 this_action = BPSTAT_WHAT_STEP_RESUME;
5929 else
5930 {
5931 /* It is for the wrong frame. */
5932 this_action = BPSTAT_WHAT_SINGLE;
5933 }
5934 break;
5935 case bp_hp_step_resume:
5936 if (bs->stop)
5937 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5938 else
5939 {
5940 /* It is for the wrong frame. */
5941 this_action = BPSTAT_WHAT_SINGLE;
5942 }
5943 break;
5944 case bp_watchpoint_scope:
5945 case bp_thread_event:
5946 case bp_overlay_event:
5947 case bp_longjmp_master:
5948 case bp_std_terminate_master:
5949 case bp_exception_master:
5950 this_action = BPSTAT_WHAT_SINGLE;
5951 break;
5952 case bp_catchpoint:
5953 if (bs->stop)
5954 {
5955 if (bs->print)
5956 this_action = BPSTAT_WHAT_STOP_NOISY;
5957 else
5958 this_action = BPSTAT_WHAT_STOP_SILENT;
5959 }
5960 else
5961 {
5962 /* There was a catchpoint, but we're not stopping.
5963 This requires no further action. */
5964 }
5965 break;
5966 case bp_jit_event:
5967 this_action = BPSTAT_WHAT_SINGLE;
5968 break;
5969 case bp_call_dummy:
5970 /* Make sure the action is stop (silent or noisy),
5971 so infrun.c pops the dummy frame. */
5972 retval.call_dummy = STOP_STACK_DUMMY;
5973 this_action = BPSTAT_WHAT_STOP_SILENT;
5974 break;
5975 case bp_std_terminate:
5976 /* Make sure the action is stop (silent or noisy),
5977 so infrun.c pops the dummy frame. */
5978 retval.call_dummy = STOP_STD_TERMINATE;
5979 this_action = BPSTAT_WHAT_STOP_SILENT;
5980 break;
5981 case bp_tracepoint:
5982 case bp_fast_tracepoint:
5983 case bp_static_tracepoint:
5984 /* Tracepoint hits should not be reported back to GDB, and
5985 if one got through somehow, it should have been filtered
5986 out already. */
5987 internal_error (__FILE__, __LINE__,
5988 _("bpstat_what: tracepoint encountered"));
5989 break;
5990 case bp_gnu_ifunc_resolver:
5991 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5992 this_action = BPSTAT_WHAT_SINGLE;
5993 break;
5994 case bp_gnu_ifunc_resolver_return:
5995 /* The breakpoint will be removed, execution will restart from the
5996 PC of the former breakpoint. */
5997 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5998 break;
5999
6000 case bp_dprintf:
6001 if (bs->stop)
6002 this_action = BPSTAT_WHAT_STOP_SILENT;
6003 else
6004 this_action = BPSTAT_WHAT_SINGLE;
6005 break;
6006
6007 default:
6008 internal_error (__FILE__, __LINE__,
6009 _("bpstat_what: unhandled bptype %d"), (int) bptype);
6010 }
6011
6012 retval.main_action = std::max (retval.main_action, this_action);
6013 }
6014
6015 return retval;
6016}
6017
6018void
6019bpstat_run_callbacks (bpstat bs_head)
6020{
6021 bpstat bs;
6022
6023 for (bs = bs_head; bs != NULL; bs = bs->next)
6024 {
6025 struct breakpoint *b = bs->breakpoint_at;
6026
6027 if (b == NULL)
6028 continue;
6029 switch (b->type)
6030 {
6031 case bp_jit_event:
6032 handle_jit_event ();
6033 break;
6034 case bp_gnu_ifunc_resolver:
6035 gnu_ifunc_resolver_stop (b);
6036 break;
6037 case bp_gnu_ifunc_resolver_return:
6038 gnu_ifunc_resolver_return_stop (b);
6039 break;
6040 }
6041 }
6042}
6043
6044/* Nonzero if we should step constantly (e.g. watchpoints on machines
6045 without hardware support). This isn't related to a specific bpstat,
6046 just to things like whether watchpoints are set. */
6047
6048int
6049bpstat_should_step (void)
6050{
6051 struct breakpoint *b;
6052
6053 ALL_BREAKPOINTS (b)
6054 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6055 return 1;
6056 return 0;
6057}
6058
6059int
6060bpstat_causes_stop (bpstat bs)
6061{
6062 for (; bs != NULL; bs = bs->next)
6063 if (bs->stop)
6064 return 1;
6065
6066 return 0;
6067}
6068
6069\f
6070
6071/* Compute a string of spaces suitable to indent the next line
6072 so it starts at the position corresponding to the table column
6073 named COL_NAME in the currently active table of UIOUT. */
6074
6075static char *
6076wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6077{
6078 static char wrap_indent[80];
6079 int i, total_width, width, align;
6080 char *text;
6081
6082 total_width = 0;
6083 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6084 {
6085 if (strcmp (text, col_name) == 0)
6086 {
6087 gdb_assert (total_width < sizeof wrap_indent);
6088 memset (wrap_indent, ' ', total_width);
6089 wrap_indent[total_width] = 0;
6090
6091 return wrap_indent;
6092 }
6093
6094 total_width += width + 1;
6095 }
6096
6097 return NULL;
6098}
6099
6100/* Determine if the locations of this breakpoint will have their conditions
6101 evaluated by the target, host or a mix of both. Returns the following:
6102
6103 "host": Host evals condition.
6104 "host or target": Host or Target evals condition.
6105 "target": Target evals condition.
6106*/
6107
6108static const char *
6109bp_condition_evaluator (struct breakpoint *b)
6110{
6111 struct bp_location *bl;
6112 char host_evals = 0;
6113 char target_evals = 0;
6114
6115 if (!b)
6116 return NULL;
6117
6118 if (!is_breakpoint (b))
6119 return NULL;
6120
6121 if (gdb_evaluates_breakpoint_condition_p ()
6122 || !target_supports_evaluation_of_breakpoint_conditions ())
6123 return condition_evaluation_host;
6124
6125 for (bl = b->loc; bl; bl = bl->next)
6126 {
6127 if (bl->cond_bytecode)
6128 target_evals++;
6129 else
6130 host_evals++;
6131 }
6132
6133 if (host_evals && target_evals)
6134 return condition_evaluation_both;
6135 else if (target_evals)
6136 return condition_evaluation_target;
6137 else
6138 return condition_evaluation_host;
6139}
6140
6141/* Determine the breakpoint location's condition evaluator. This is
6142 similar to bp_condition_evaluator, but for locations. */
6143
6144static const char *
6145bp_location_condition_evaluator (struct bp_location *bl)
6146{
6147 if (bl && !is_breakpoint (bl->owner))
6148 return NULL;
6149
6150 if (gdb_evaluates_breakpoint_condition_p ()
6151 || !target_supports_evaluation_of_breakpoint_conditions ())
6152 return condition_evaluation_host;
6153
6154 if (bl && bl->cond_bytecode)
6155 return condition_evaluation_target;
6156 else
6157 return condition_evaluation_host;
6158}
6159
6160/* Print the LOC location out of the list of B->LOC locations. */
6161
6162static void
6163print_breakpoint_location (struct breakpoint *b,
6164 struct bp_location *loc)
6165{
6166 struct ui_out *uiout = current_uiout;
6167 struct cleanup *old_chain = save_current_program_space ();
6168
6169 if (loc != NULL && loc->shlib_disabled)
6170 loc = NULL;
6171
6172 if (loc != NULL)
6173 set_current_program_space (loc->pspace);
6174
6175 if (b->display_canonical)
6176 ui_out_field_string (uiout, "what",
6177 event_location_to_string (b->location));
6178 else if (loc && loc->symtab)
6179 {
6180 struct symbol *sym
6181 = find_pc_sect_function (loc->address, loc->section);
6182 if (sym)
6183 {
6184 ui_out_text (uiout, "in ");
6185 ui_out_field_string (uiout, "func",
6186 SYMBOL_PRINT_NAME (sym));
6187 ui_out_text (uiout, " ");
6188 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6189 ui_out_text (uiout, "at ");
6190 }
6191 ui_out_field_string (uiout, "file",
6192 symtab_to_filename_for_display (loc->symtab));
6193 ui_out_text (uiout, ":");
6194
6195 if (ui_out_is_mi_like_p (uiout))
6196 ui_out_field_string (uiout, "fullname",
6197 symtab_to_fullname (loc->symtab));
6198
6199 ui_out_field_int (uiout, "line", loc->line_number);
6200 }
6201 else if (loc)
6202 {
6203 struct ui_file *stb = mem_fileopen ();
6204 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6205
6206 print_address_symbolic (loc->gdbarch, loc->address, stb,
6207 demangle, "");
6208 ui_out_field_stream (uiout, "at", stb);
6209
6210 do_cleanups (stb_chain);
6211 }
6212 else
6213 {
6214 ui_out_field_string (uiout, "pending",
6215 event_location_to_string (b->location));
6216 /* If extra_string is available, it could be holding a condition
6217 or dprintf arguments. In either case, make sure it is printed,
6218 too, but only for non-MI streams. */
6219 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6220 {
6221 if (b->type == bp_dprintf)
6222 ui_out_text (uiout, ",");
6223 else
6224 ui_out_text (uiout, " ");
6225 ui_out_text (uiout, b->extra_string);
6226 }
6227 }
6228
6229 if (loc && is_breakpoint (b)
6230 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6231 && bp_condition_evaluator (b) == condition_evaluation_both)
6232 {
6233 ui_out_text (uiout, " (");
6234 ui_out_field_string (uiout, "evaluated-by",
6235 bp_location_condition_evaluator (loc));
6236 ui_out_text (uiout, ")");
6237 }
6238
6239 do_cleanups (old_chain);
6240}
6241
6242static const char *
6243bptype_string (enum bptype type)
6244{
6245 struct ep_type_description
6246 {
6247 enum bptype type;
6248 char *description;
6249 };
6250 static struct ep_type_description bptypes[] =
6251 {
6252 {bp_none, "?deleted?"},
6253 {bp_breakpoint, "breakpoint"},
6254 {bp_hardware_breakpoint, "hw breakpoint"},
6255 {bp_single_step, "sw single-step"},
6256 {bp_until, "until"},
6257 {bp_finish, "finish"},
6258 {bp_watchpoint, "watchpoint"},
6259 {bp_hardware_watchpoint, "hw watchpoint"},
6260 {bp_read_watchpoint, "read watchpoint"},
6261 {bp_access_watchpoint, "acc watchpoint"},
6262 {bp_longjmp, "longjmp"},
6263 {bp_longjmp_resume, "longjmp resume"},
6264 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6265 {bp_exception, "exception"},
6266 {bp_exception_resume, "exception resume"},
6267 {bp_step_resume, "step resume"},
6268 {bp_hp_step_resume, "high-priority step resume"},
6269 {bp_watchpoint_scope, "watchpoint scope"},
6270 {bp_call_dummy, "call dummy"},
6271 {bp_std_terminate, "std::terminate"},
6272 {bp_shlib_event, "shlib events"},
6273 {bp_thread_event, "thread events"},
6274 {bp_overlay_event, "overlay events"},
6275 {bp_longjmp_master, "longjmp master"},
6276 {bp_std_terminate_master, "std::terminate master"},
6277 {bp_exception_master, "exception master"},
6278 {bp_catchpoint, "catchpoint"},
6279 {bp_tracepoint, "tracepoint"},
6280 {bp_fast_tracepoint, "fast tracepoint"},
6281 {bp_static_tracepoint, "static tracepoint"},
6282 {bp_dprintf, "dprintf"},
6283 {bp_jit_event, "jit events"},
6284 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6285 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6286 };
6287
6288 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6289 || ((int) type != bptypes[(int) type].type))
6290 internal_error (__FILE__, __LINE__,
6291 _("bptypes table does not describe type #%d."),
6292 (int) type);
6293
6294 return bptypes[(int) type].description;
6295}
6296
6297/* For MI, output a field named 'thread-groups' with a list as the value.
6298 For CLI, prefix the list with the string 'inf'. */
6299
6300static void
6301output_thread_groups (struct ui_out *uiout,
6302 const char *field_name,
6303 VEC(int) *inf_num,
6304 int mi_only)
6305{
6306 struct cleanup *back_to;
6307 int is_mi = ui_out_is_mi_like_p (uiout);
6308 int inf;
6309 int i;
6310
6311 /* For backward compatibility, don't display inferiors in CLI unless
6312 there are several. Always display them for MI. */
6313 if (!is_mi && mi_only)
6314 return;
6315
6316 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6317
6318 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6319 {
6320 if (is_mi)
6321 {
6322 char mi_group[10];
6323
6324 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6325 ui_out_field_string (uiout, NULL, mi_group);
6326 }
6327 else
6328 {
6329 if (i == 0)
6330 ui_out_text (uiout, " inf ");
6331 else
6332 ui_out_text (uiout, ", ");
6333
6334 ui_out_text (uiout, plongest (inf));
6335 }
6336 }
6337
6338 do_cleanups (back_to);
6339}
6340
6341/* Print B to gdb_stdout. */
6342
6343static void
6344print_one_breakpoint_location (struct breakpoint *b,
6345 struct bp_location *loc,
6346 int loc_number,
6347 struct bp_location **last_loc,
6348 int allflag)
6349{
6350 struct command_line *l;
6351 static char bpenables[] = "nynny";
6352
6353 struct ui_out *uiout = current_uiout;
6354 int header_of_multiple = 0;
6355 int part_of_multiple = (loc != NULL);
6356 struct value_print_options opts;
6357
6358 get_user_print_options (&opts);
6359
6360 gdb_assert (!loc || loc_number != 0);
6361 /* See comment in print_one_breakpoint concerning treatment of
6362 breakpoints with single disabled location. */
6363 if (loc == NULL
6364 && (b->loc != NULL
6365 && (b->loc->next != NULL || !b->loc->enabled)))
6366 header_of_multiple = 1;
6367 if (loc == NULL)
6368 loc = b->loc;
6369
6370 annotate_record ();
6371
6372 /* 1 */
6373 annotate_field (0);
6374 if (part_of_multiple)
6375 {
6376 char *formatted;
6377 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6378 ui_out_field_string (uiout, "number", formatted);
6379 xfree (formatted);
6380 }
6381 else
6382 {
6383 ui_out_field_int (uiout, "number", b->number);
6384 }
6385
6386 /* 2 */
6387 annotate_field (1);
6388 if (part_of_multiple)
6389 ui_out_field_skip (uiout, "type");
6390 else
6391 ui_out_field_string (uiout, "type", bptype_string (b->type));
6392
6393 /* 3 */
6394 annotate_field (2);
6395 if (part_of_multiple)
6396 ui_out_field_skip (uiout, "disp");
6397 else
6398 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6399
6400
6401 /* 4 */
6402 annotate_field (3);
6403 if (part_of_multiple)
6404 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6405 else
6406 ui_out_field_fmt (uiout, "enabled", "%c",
6407 bpenables[(int) b->enable_state]);
6408 ui_out_spaces (uiout, 2);
6409
6410
6411 /* 5 and 6 */
6412 if (b->ops != NULL && b->ops->print_one != NULL)
6413 {
6414 /* Although the print_one can possibly print all locations,
6415 calling it here is not likely to get any nice result. So,
6416 make sure there's just one location. */
6417 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6418 b->ops->print_one (b, last_loc);
6419 }
6420 else
6421 switch (b->type)
6422 {
6423 case bp_none:
6424 internal_error (__FILE__, __LINE__,
6425 _("print_one_breakpoint: bp_none encountered\n"));
6426 break;
6427
6428 case bp_watchpoint:
6429 case bp_hardware_watchpoint:
6430 case bp_read_watchpoint:
6431 case bp_access_watchpoint:
6432 {
6433 struct watchpoint *w = (struct watchpoint *) b;
6434
6435 /* Field 4, the address, is omitted (which makes the columns
6436 not line up too nicely with the headers, but the effect
6437 is relatively readable). */
6438 if (opts.addressprint)
6439 ui_out_field_skip (uiout, "addr");
6440 annotate_field (5);
6441 ui_out_field_string (uiout, "what", w->exp_string);
6442 }
6443 break;
6444
6445 case bp_breakpoint:
6446 case bp_hardware_breakpoint:
6447 case bp_single_step:
6448 case bp_until:
6449 case bp_finish:
6450 case bp_longjmp:
6451 case bp_longjmp_resume:
6452 case bp_longjmp_call_dummy:
6453 case bp_exception:
6454 case bp_exception_resume:
6455 case bp_step_resume:
6456 case bp_hp_step_resume:
6457 case bp_watchpoint_scope:
6458 case bp_call_dummy:
6459 case bp_std_terminate:
6460 case bp_shlib_event:
6461 case bp_thread_event:
6462 case bp_overlay_event:
6463 case bp_longjmp_master:
6464 case bp_std_terminate_master:
6465 case bp_exception_master:
6466 case bp_tracepoint:
6467 case bp_fast_tracepoint:
6468 case bp_static_tracepoint:
6469 case bp_dprintf:
6470 case bp_jit_event:
6471 case bp_gnu_ifunc_resolver:
6472 case bp_gnu_ifunc_resolver_return:
6473 if (opts.addressprint)
6474 {
6475 annotate_field (4);
6476 if (header_of_multiple)
6477 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6478 else if (b->loc == NULL || loc->shlib_disabled)
6479 ui_out_field_string (uiout, "addr", "<PENDING>");
6480 else
6481 ui_out_field_core_addr (uiout, "addr",
6482 loc->gdbarch, loc->address);
6483 }
6484 annotate_field (5);
6485 if (!header_of_multiple)
6486 print_breakpoint_location (b, loc);
6487 if (b->loc)
6488 *last_loc = b->loc;
6489 break;
6490 }
6491
6492
6493 if (loc != NULL && !header_of_multiple)
6494 {
6495 struct inferior *inf;
6496 VEC(int) *inf_num = NULL;
6497 int mi_only = 1;
6498
6499 ALL_INFERIORS (inf)
6500 {
6501 if (inf->pspace == loc->pspace)
6502 VEC_safe_push (int, inf_num, inf->num);
6503 }
6504
6505 /* For backward compatibility, don't display inferiors in CLI unless
6506 there are several. Always display for MI. */
6507 if (allflag
6508 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6509 && (number_of_program_spaces () > 1
6510 || number_of_inferiors () > 1)
6511 /* LOC is for existing B, it cannot be in
6512 moribund_locations and thus having NULL OWNER. */
6513 && loc->owner->type != bp_catchpoint))
6514 mi_only = 0;
6515 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6516 VEC_free (int, inf_num);
6517 }
6518
6519 if (!part_of_multiple)
6520 {
6521 if (b->thread != -1)
6522 {
6523 /* FIXME: This seems to be redundant and lost here; see the
6524 "stop only in" line a little further down. */
6525 ui_out_text (uiout, " thread ");
6526 ui_out_field_int (uiout, "thread", b->thread);
6527 }
6528 else if (b->task != 0)
6529 {
6530 ui_out_text (uiout, " task ");
6531 ui_out_field_int (uiout, "task", b->task);
6532 }
6533 }
6534
6535 ui_out_text (uiout, "\n");
6536
6537 if (!part_of_multiple)
6538 b->ops->print_one_detail (b, uiout);
6539
6540 if (part_of_multiple && frame_id_p (b->frame_id))
6541 {
6542 annotate_field (6);
6543 ui_out_text (uiout, "\tstop only in stack frame at ");
6544 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6545 the frame ID. */
6546 ui_out_field_core_addr (uiout, "frame",
6547 b->gdbarch, b->frame_id.stack_addr);
6548 ui_out_text (uiout, "\n");
6549 }
6550
6551 if (!part_of_multiple && b->cond_string)
6552 {
6553 annotate_field (7);
6554 if (is_tracepoint (b))
6555 ui_out_text (uiout, "\ttrace only if ");
6556 else
6557 ui_out_text (uiout, "\tstop only if ");
6558 ui_out_field_string (uiout, "cond", b->cond_string);
6559
6560 /* Print whether the target is doing the breakpoint's condition
6561 evaluation. If GDB is doing the evaluation, don't print anything. */
6562 if (is_breakpoint (b)
6563 && breakpoint_condition_evaluation_mode ()
6564 == condition_evaluation_target)
6565 {
6566 ui_out_text (uiout, " (");
6567 ui_out_field_string (uiout, "evaluated-by",
6568 bp_condition_evaluator (b));
6569 ui_out_text (uiout, " evals)");
6570 }
6571 ui_out_text (uiout, "\n");
6572 }
6573
6574 if (!part_of_multiple && b->thread != -1)
6575 {
6576 /* FIXME should make an annotation for this. */
6577 ui_out_text (uiout, "\tstop only in thread ");
6578 if (ui_out_is_mi_like_p (uiout))
6579 ui_out_field_int (uiout, "thread", b->thread);
6580 else
6581 {
6582 struct thread_info *thr = find_thread_global_id (b->thread);
6583
6584 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6585 }
6586 ui_out_text (uiout, "\n");
6587 }
6588
6589 if (!part_of_multiple)
6590 {
6591 if (b->hit_count)
6592 {
6593 /* FIXME should make an annotation for this. */
6594 if (is_catchpoint (b))
6595 ui_out_text (uiout, "\tcatchpoint");
6596 else if (is_tracepoint (b))
6597 ui_out_text (uiout, "\ttracepoint");
6598 else
6599 ui_out_text (uiout, "\tbreakpoint");
6600 ui_out_text (uiout, " already hit ");
6601 ui_out_field_int (uiout, "times", b->hit_count);
6602 if (b->hit_count == 1)
6603 ui_out_text (uiout, " time\n");
6604 else
6605 ui_out_text (uiout, " times\n");
6606 }
6607 else
6608 {
6609 /* Output the count also if it is zero, but only if this is mi. */
6610 if (ui_out_is_mi_like_p (uiout))
6611 ui_out_field_int (uiout, "times", b->hit_count);
6612 }
6613 }
6614
6615 if (!part_of_multiple && b->ignore_count)
6616 {
6617 annotate_field (8);
6618 ui_out_text (uiout, "\tignore next ");
6619 ui_out_field_int (uiout, "ignore", b->ignore_count);
6620 ui_out_text (uiout, " hits\n");
6621 }
6622
6623 /* Note that an enable count of 1 corresponds to "enable once"
6624 behavior, which is reported by the combination of enablement and
6625 disposition, so we don't need to mention it here. */
6626 if (!part_of_multiple && b->enable_count > 1)
6627 {
6628 annotate_field (8);
6629 ui_out_text (uiout, "\tdisable after ");
6630 /* Tweak the wording to clarify that ignore and enable counts
6631 are distinct, and have additive effect. */
6632 if (b->ignore_count)
6633 ui_out_text (uiout, "additional ");
6634 else
6635 ui_out_text (uiout, "next ");
6636 ui_out_field_int (uiout, "enable", b->enable_count);
6637 ui_out_text (uiout, " hits\n");
6638 }
6639
6640 if (!part_of_multiple && is_tracepoint (b))
6641 {
6642 struct tracepoint *tp = (struct tracepoint *) b;
6643
6644 if (tp->traceframe_usage)
6645 {
6646 ui_out_text (uiout, "\ttrace buffer usage ");
6647 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6648 ui_out_text (uiout, " bytes\n");
6649 }
6650 }
6651
6652 l = b->commands ? b->commands->commands : NULL;
6653 if (!part_of_multiple && l)
6654 {
6655 struct cleanup *script_chain;
6656
6657 annotate_field (9);
6658 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6659 print_command_lines (uiout, l, 4);
6660 do_cleanups (script_chain);
6661 }
6662
6663 if (is_tracepoint (b))
6664 {
6665 struct tracepoint *t = (struct tracepoint *) b;
6666
6667 if (!part_of_multiple && t->pass_count)
6668 {
6669 annotate_field (10);
6670 ui_out_text (uiout, "\tpass count ");
6671 ui_out_field_int (uiout, "pass", t->pass_count);
6672 ui_out_text (uiout, " \n");
6673 }
6674
6675 /* Don't display it when tracepoint or tracepoint location is
6676 pending. */
6677 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6678 {
6679 annotate_field (11);
6680
6681 if (ui_out_is_mi_like_p (uiout))
6682 ui_out_field_string (uiout, "installed",
6683 loc->inserted ? "y" : "n");
6684 else
6685 {
6686 if (loc->inserted)
6687 ui_out_text (uiout, "\t");
6688 else
6689 ui_out_text (uiout, "\tnot ");
6690 ui_out_text (uiout, "installed on target\n");
6691 }
6692 }
6693 }
6694
6695 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6696 {
6697 if (is_watchpoint (b))
6698 {
6699 struct watchpoint *w = (struct watchpoint *) b;
6700
6701 ui_out_field_string (uiout, "original-location", w->exp_string);
6702 }
6703 else if (b->location != NULL
6704 && event_location_to_string (b->location) != NULL)
6705 ui_out_field_string (uiout, "original-location",
6706 event_location_to_string (b->location));
6707 }
6708}
6709
6710static void
6711print_one_breakpoint (struct breakpoint *b,
6712 struct bp_location **last_loc,
6713 int allflag)
6714{
6715 struct cleanup *bkpt_chain;
6716 struct ui_out *uiout = current_uiout;
6717
6718 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6719
6720 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6721 do_cleanups (bkpt_chain);
6722
6723 /* If this breakpoint has custom print function,
6724 it's already printed. Otherwise, print individual
6725 locations, if any. */
6726 if (b->ops == NULL || b->ops->print_one == NULL)
6727 {
6728 /* If breakpoint has a single location that is disabled, we
6729 print it as if it had several locations, since otherwise it's
6730 hard to represent "breakpoint enabled, location disabled"
6731 situation.
6732
6733 Note that while hardware watchpoints have several locations
6734 internally, that's not a property exposed to user. */
6735 if (b->loc
6736 && !is_hardware_watchpoint (b)
6737 && (b->loc->next || !b->loc->enabled))
6738 {
6739 struct bp_location *loc;
6740 int n = 1;
6741
6742 for (loc = b->loc; loc; loc = loc->next, ++n)
6743 {
6744 struct cleanup *inner2 =
6745 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6746 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6747 do_cleanups (inner2);
6748 }
6749 }
6750 }
6751}
6752
6753static int
6754breakpoint_address_bits (struct breakpoint *b)
6755{
6756 int print_address_bits = 0;
6757 struct bp_location *loc;
6758
6759 /* Software watchpoints that aren't watching memory don't have an
6760 address to print. */
6761 if (is_no_memory_software_watchpoint (b))
6762 return 0;
6763
6764 for (loc = b->loc; loc; loc = loc->next)
6765 {
6766 int addr_bit;
6767
6768 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6769 if (addr_bit > print_address_bits)
6770 print_address_bits = addr_bit;
6771 }
6772
6773 return print_address_bits;
6774}
6775
6776struct captured_breakpoint_query_args
6777 {
6778 int bnum;
6779 };
6780
6781static int
6782do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6783{
6784 struct captured_breakpoint_query_args *args
6785 = (struct captured_breakpoint_query_args *) data;
6786 struct breakpoint *b;
6787 struct bp_location *dummy_loc = NULL;
6788
6789 ALL_BREAKPOINTS (b)
6790 {
6791 if (args->bnum == b->number)
6792 {
6793 print_one_breakpoint (b, &dummy_loc, 0);
6794 return GDB_RC_OK;
6795 }
6796 }
6797 return GDB_RC_NONE;
6798}
6799
6800enum gdb_rc
6801gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6802 char **error_message)
6803{
6804 struct captured_breakpoint_query_args args;
6805
6806 args.bnum = bnum;
6807 /* For the moment we don't trust print_one_breakpoint() to not throw
6808 an error. */
6809 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6810 error_message, RETURN_MASK_ALL) < 0)
6811 return GDB_RC_FAIL;
6812 else
6813 return GDB_RC_OK;
6814}
6815
6816/* Return true if this breakpoint was set by the user, false if it is
6817 internal or momentary. */
6818
6819int
6820user_breakpoint_p (struct breakpoint *b)
6821{
6822 return b->number > 0;
6823}
6824
6825/* See breakpoint.h. */
6826
6827int
6828pending_breakpoint_p (struct breakpoint *b)
6829{
6830 return b->loc == NULL;
6831}
6832
6833/* Print information on user settable breakpoint (watchpoint, etc)
6834 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6835 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6836 FILTER is non-NULL, call it on each breakpoint and only include the
6837 ones for which it returns non-zero. Return the total number of
6838 breakpoints listed. */
6839
6840static int
6841breakpoint_1 (char *args, int allflag,
6842 int (*filter) (const struct breakpoint *))
6843{
6844 struct breakpoint *b;
6845 struct bp_location *last_loc = NULL;
6846 int nr_printable_breakpoints;
6847 struct cleanup *bkpttbl_chain;
6848 struct value_print_options opts;
6849 int print_address_bits = 0;
6850 int print_type_col_width = 14;
6851 struct ui_out *uiout = current_uiout;
6852
6853 get_user_print_options (&opts);
6854
6855 /* Compute the number of rows in the table, as well as the size
6856 required for address fields. */
6857 nr_printable_breakpoints = 0;
6858 ALL_BREAKPOINTS (b)
6859 {
6860 /* If we have a filter, only list the breakpoints it accepts. */
6861 if (filter && !filter (b))
6862 continue;
6863
6864 /* If we have an "args" string, it is a list of breakpoints to
6865 accept. Skip the others. */
6866 if (args != NULL && *args != '\0')
6867 {
6868 if (allflag && parse_and_eval_long (args) != b->number)
6869 continue;
6870 if (!allflag && !number_is_in_list (args, b->number))
6871 continue;
6872 }
6873
6874 if (allflag || user_breakpoint_p (b))
6875 {
6876 int addr_bit, type_len;
6877
6878 addr_bit = breakpoint_address_bits (b);
6879 if (addr_bit > print_address_bits)
6880 print_address_bits = addr_bit;
6881
6882 type_len = strlen (bptype_string (b->type));
6883 if (type_len > print_type_col_width)
6884 print_type_col_width = type_len;
6885
6886 nr_printable_breakpoints++;
6887 }
6888 }
6889
6890 if (opts.addressprint)
6891 bkpttbl_chain
6892 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6893 nr_printable_breakpoints,
6894 "BreakpointTable");
6895 else
6896 bkpttbl_chain
6897 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6898 nr_printable_breakpoints,
6899 "BreakpointTable");
6900
6901 if (nr_printable_breakpoints > 0)
6902 annotate_breakpoints_headers ();
6903 if (nr_printable_breakpoints > 0)
6904 annotate_field (0);
6905 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6906 if (nr_printable_breakpoints > 0)
6907 annotate_field (1);
6908 ui_out_table_header (uiout, print_type_col_width, ui_left,
6909 "type", "Type"); /* 2 */
6910 if (nr_printable_breakpoints > 0)
6911 annotate_field (2);
6912 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6913 if (nr_printable_breakpoints > 0)
6914 annotate_field (3);
6915 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6916 if (opts.addressprint)
6917 {
6918 if (nr_printable_breakpoints > 0)
6919 annotate_field (4);
6920 if (print_address_bits <= 32)
6921 ui_out_table_header (uiout, 10, ui_left,
6922 "addr", "Address"); /* 5 */
6923 else
6924 ui_out_table_header (uiout, 18, ui_left,
6925 "addr", "Address"); /* 5 */
6926 }
6927 if (nr_printable_breakpoints > 0)
6928 annotate_field (5);
6929 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6930 ui_out_table_body (uiout);
6931 if (nr_printable_breakpoints > 0)
6932 annotate_breakpoints_table ();
6933
6934 ALL_BREAKPOINTS (b)
6935 {
6936 QUIT;
6937 /* If we have a filter, only list the breakpoints it accepts. */
6938 if (filter && !filter (b))
6939 continue;
6940
6941 /* If we have an "args" string, it is a list of breakpoints to
6942 accept. Skip the others. */
6943
6944 if (args != NULL && *args != '\0')
6945 {
6946 if (allflag) /* maintenance info breakpoint */
6947 {
6948 if (parse_and_eval_long (args) != b->number)
6949 continue;
6950 }
6951 else /* all others */
6952 {
6953 if (!number_is_in_list (args, b->number))
6954 continue;
6955 }
6956 }
6957 /* We only print out user settable breakpoints unless the
6958 allflag is set. */
6959 if (allflag || user_breakpoint_p (b))
6960 print_one_breakpoint (b, &last_loc, allflag);
6961 }
6962
6963 do_cleanups (bkpttbl_chain);
6964
6965 if (nr_printable_breakpoints == 0)
6966 {
6967 /* If there's a filter, let the caller decide how to report
6968 empty list. */
6969 if (!filter)
6970 {
6971 if (args == NULL || *args == '\0')
6972 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6973 else
6974 ui_out_message (uiout, 0,
6975 "No breakpoint or watchpoint matching '%s'.\n",
6976 args);
6977 }
6978 }
6979 else
6980 {
6981 if (last_loc && !server_command)
6982 set_next_address (last_loc->gdbarch, last_loc->address);
6983 }
6984
6985 /* FIXME? Should this be moved up so that it is only called when
6986 there have been breakpoints? */
6987 annotate_breakpoints_table_end ();
6988
6989 return nr_printable_breakpoints;
6990}
6991
6992/* Display the value of default-collect in a way that is generally
6993 compatible with the breakpoint list. */
6994
6995static void
6996default_collect_info (void)
6997{
6998 struct ui_out *uiout = current_uiout;
6999
7000 /* If it has no value (which is frequently the case), say nothing; a
7001 message like "No default-collect." gets in user's face when it's
7002 not wanted. */
7003 if (!*default_collect)
7004 return;
7005
7006 /* The following phrase lines up nicely with per-tracepoint collect
7007 actions. */
7008 ui_out_text (uiout, "default collect ");
7009 ui_out_field_string (uiout, "default-collect", default_collect);
7010 ui_out_text (uiout, " \n");
7011}
7012
7013static void
7014breakpoints_info (char *args, int from_tty)
7015{
7016 breakpoint_1 (args, 0, NULL);
7017
7018 default_collect_info ();
7019}
7020
7021static void
7022watchpoints_info (char *args, int from_tty)
7023{
7024 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
7025 struct ui_out *uiout = current_uiout;
7026
7027 if (num_printed == 0)
7028 {
7029 if (args == NULL || *args == '\0')
7030 ui_out_message (uiout, 0, "No watchpoints.\n");
7031 else
7032 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
7033 }
7034}
7035
7036static void
7037maintenance_info_breakpoints (char *args, int from_tty)
7038{
7039 breakpoint_1 (args, 1, NULL);
7040
7041 default_collect_info ();
7042}
7043
7044static int
7045breakpoint_has_pc (struct breakpoint *b,
7046 struct program_space *pspace,
7047 CORE_ADDR pc, struct obj_section *section)
7048{
7049 struct bp_location *bl = b->loc;
7050
7051 for (; bl; bl = bl->next)
7052 {
7053 if (bl->pspace == pspace
7054 && bl->address == pc
7055 && (!overlay_debugging || bl->section == section))
7056 return 1;
7057 }
7058 return 0;
7059}
7060
7061/* Print a message describing any user-breakpoints set at PC. This
7062 concerns with logical breakpoints, so we match program spaces, not
7063 address spaces. */
7064
7065static void
7066describe_other_breakpoints (struct gdbarch *gdbarch,
7067 struct program_space *pspace, CORE_ADDR pc,
7068 struct obj_section *section, int thread)
7069{
7070 int others = 0;
7071 struct breakpoint *b;
7072
7073 ALL_BREAKPOINTS (b)
7074 others += (user_breakpoint_p (b)
7075 && breakpoint_has_pc (b, pspace, pc, section));
7076 if (others > 0)
7077 {
7078 if (others == 1)
7079 printf_filtered (_("Note: breakpoint "));
7080 else /* if (others == ???) */
7081 printf_filtered (_("Note: breakpoints "));
7082 ALL_BREAKPOINTS (b)
7083 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7084 {
7085 others--;
7086 printf_filtered ("%d", b->number);
7087 if (b->thread == -1 && thread != -1)
7088 printf_filtered (" (all threads)");
7089 else if (b->thread != -1)
7090 printf_filtered (" (thread %d)", b->thread);
7091 printf_filtered ("%s%s ",
7092 ((b->enable_state == bp_disabled
7093 || b->enable_state == bp_call_disabled)
7094 ? " (disabled)"
7095 : ""),
7096 (others > 1) ? ","
7097 : ((others == 1) ? " and" : ""));
7098 }
7099 printf_filtered (_("also set at pc "));
7100 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7101 printf_filtered (".\n");
7102 }
7103}
7104\f
7105
7106/* Return true iff it is meaningful to use the address member of
7107 BPT locations. For some breakpoint types, the locations' address members
7108 are irrelevant and it makes no sense to attempt to compare them to other
7109 addresses (or use them for any other purpose either).
7110
7111 More specifically, each of the following breakpoint types will
7112 always have a zero valued location address and we don't want to mark
7113 breakpoints of any of these types to be a duplicate of an actual
7114 breakpoint location at address zero:
7115
7116 bp_watchpoint
7117 bp_catchpoint
7118
7119*/
7120
7121static int
7122breakpoint_address_is_meaningful (struct breakpoint *bpt)
7123{
7124 enum bptype type = bpt->type;
7125
7126 return (type != bp_watchpoint && type != bp_catchpoint);
7127}
7128
7129/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7130 true if LOC1 and LOC2 represent the same watchpoint location. */
7131
7132static int
7133watchpoint_locations_match (struct bp_location *loc1,
7134 struct bp_location *loc2)
7135{
7136 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7137 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7138
7139 /* Both of them must exist. */
7140 gdb_assert (w1 != NULL);
7141 gdb_assert (w2 != NULL);
7142
7143 /* If the target can evaluate the condition expression in hardware,
7144 then we we need to insert both watchpoints even if they are at
7145 the same place. Otherwise the watchpoint will only trigger when
7146 the condition of whichever watchpoint was inserted evaluates to
7147 true, not giving a chance for GDB to check the condition of the
7148 other watchpoint. */
7149 if ((w1->cond_exp
7150 && target_can_accel_watchpoint_condition (loc1->address,
7151 loc1->length,
7152 loc1->watchpoint_type,
7153 w1->cond_exp))
7154 || (w2->cond_exp
7155 && target_can_accel_watchpoint_condition (loc2->address,
7156 loc2->length,
7157 loc2->watchpoint_type,
7158 w2->cond_exp)))
7159 return 0;
7160
7161 /* Note that this checks the owner's type, not the location's. In
7162 case the target does not support read watchpoints, but does
7163 support access watchpoints, we'll have bp_read_watchpoint
7164 watchpoints with hw_access locations. Those should be considered
7165 duplicates of hw_read locations. The hw_read locations will
7166 become hw_access locations later. */
7167 return (loc1->owner->type == loc2->owner->type
7168 && loc1->pspace->aspace == loc2->pspace->aspace
7169 && loc1->address == loc2->address
7170 && loc1->length == loc2->length);
7171}
7172
7173/* See breakpoint.h. */
7174
7175int
7176breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7177 struct address_space *aspace2, CORE_ADDR addr2)
7178{
7179 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7180 || aspace1 == aspace2)
7181 && addr1 == addr2);
7182}
7183
7184/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7185 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7186 matches ASPACE2. On targets that have global breakpoints, the address
7187 space doesn't really matter. */
7188
7189static int
7190breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7191 int len1, struct address_space *aspace2,
7192 CORE_ADDR addr2)
7193{
7194 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7195 || aspace1 == aspace2)
7196 && addr2 >= addr1 && addr2 < addr1 + len1);
7197}
7198
7199/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7200 a ranged breakpoint. In most targets, a match happens only if ASPACE
7201 matches the breakpoint's address space. On targets that have global
7202 breakpoints, the address space doesn't really matter. */
7203
7204static int
7205breakpoint_location_address_match (struct bp_location *bl,
7206 struct address_space *aspace,
7207 CORE_ADDR addr)
7208{
7209 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7210 aspace, addr)
7211 || (bl->length
7212 && breakpoint_address_match_range (bl->pspace->aspace,
7213 bl->address, bl->length,
7214 aspace, addr)));
7215}
7216
7217/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7218 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7219 match happens only if ASPACE matches the breakpoint's address
7220 space. On targets that have global breakpoints, the address space
7221 doesn't really matter. */
7222
7223static int
7224breakpoint_location_address_range_overlap (struct bp_location *bl,
7225 struct address_space *aspace,
7226 CORE_ADDR addr, int len)
7227{
7228 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7229 || bl->pspace->aspace == aspace)
7230 {
7231 int bl_len = bl->length != 0 ? bl->length : 1;
7232
7233 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7234 return 1;
7235 }
7236 return 0;
7237}
7238
7239/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7240 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7241 true, otherwise returns false. */
7242
7243static int
7244tracepoint_locations_match (struct bp_location *loc1,
7245 struct bp_location *loc2)
7246{
7247 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7248 /* Since tracepoint locations are never duplicated with others', tracepoint
7249 locations at the same address of different tracepoints are regarded as
7250 different locations. */
7251 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7252 else
7253 return 0;
7254}
7255
7256/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7257 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7258 represent the same location. */
7259
7260static int
7261breakpoint_locations_match (struct bp_location *loc1,
7262 struct bp_location *loc2)
7263{
7264 int hw_point1, hw_point2;
7265
7266 /* Both of them must not be in moribund_locations. */
7267 gdb_assert (loc1->owner != NULL);
7268 gdb_assert (loc2->owner != NULL);
7269
7270 hw_point1 = is_hardware_watchpoint (loc1->owner);
7271 hw_point2 = is_hardware_watchpoint (loc2->owner);
7272
7273 if (hw_point1 != hw_point2)
7274 return 0;
7275 else if (hw_point1)
7276 return watchpoint_locations_match (loc1, loc2);
7277 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7278 return tracepoint_locations_match (loc1, loc2);
7279 else
7280 /* We compare bp_location.length in order to cover ranged breakpoints. */
7281 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7282 loc2->pspace->aspace, loc2->address)
7283 && loc1->length == loc2->length);
7284}
7285
7286static void
7287breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7288 int bnum, int have_bnum)
7289{
7290 /* The longest string possibly returned by hex_string_custom
7291 is 50 chars. These must be at least that big for safety. */
7292 char astr1[64];
7293 char astr2[64];
7294
7295 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7296 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7297 if (have_bnum)
7298 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7299 bnum, astr1, astr2);
7300 else
7301 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7302}
7303
7304/* Adjust a breakpoint's address to account for architectural
7305 constraints on breakpoint placement. Return the adjusted address.
7306 Note: Very few targets require this kind of adjustment. For most
7307 targets, this function is simply the identity function. */
7308
7309static CORE_ADDR
7310adjust_breakpoint_address (struct gdbarch *gdbarch,
7311 CORE_ADDR bpaddr, enum bptype bptype)
7312{
7313 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7314 {
7315 /* Very few targets need any kind of breakpoint adjustment. */
7316 return bpaddr;
7317 }
7318 else if (bptype == bp_watchpoint
7319 || bptype == bp_hardware_watchpoint
7320 || bptype == bp_read_watchpoint
7321 || bptype == bp_access_watchpoint
7322 || bptype == bp_catchpoint)
7323 {
7324 /* Watchpoints and the various bp_catch_* eventpoints should not
7325 have their addresses modified. */
7326 return bpaddr;
7327 }
7328 else if (bptype == bp_single_step)
7329 {
7330 /* Single-step breakpoints should not have their addresses
7331 modified. If there's any architectural constrain that
7332 applies to this address, then it should have already been
7333 taken into account when the breakpoint was created in the
7334 first place. If we didn't do this, stepping through e.g.,
7335 Thumb-2 IT blocks would break. */
7336 return bpaddr;
7337 }
7338 else
7339 {
7340 CORE_ADDR adjusted_bpaddr;
7341
7342 /* Some targets have architectural constraints on the placement
7343 of breakpoint instructions. Obtain the adjusted address. */
7344 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7345
7346 /* An adjusted breakpoint address can significantly alter
7347 a user's expectations. Print a warning if an adjustment
7348 is required. */
7349 if (adjusted_bpaddr != bpaddr)
7350 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7351
7352 return adjusted_bpaddr;
7353 }
7354}
7355
7356void
7357init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7358 struct breakpoint *owner)
7359{
7360 memset (loc, 0, sizeof (*loc));
7361
7362 gdb_assert (ops != NULL);
7363
7364 loc->ops = ops;
7365 loc->owner = owner;
7366 loc->cond = NULL;
7367 loc->cond_bytecode = NULL;
7368 loc->shlib_disabled = 0;
7369 loc->enabled = 1;
7370
7371 switch (owner->type)
7372 {
7373 case bp_breakpoint:
7374 case bp_single_step:
7375 case bp_until:
7376 case bp_finish:
7377 case bp_longjmp:
7378 case bp_longjmp_resume:
7379 case bp_longjmp_call_dummy:
7380 case bp_exception:
7381 case bp_exception_resume:
7382 case bp_step_resume:
7383 case bp_hp_step_resume:
7384 case bp_watchpoint_scope:
7385 case bp_call_dummy:
7386 case bp_std_terminate:
7387 case bp_shlib_event:
7388 case bp_thread_event:
7389 case bp_overlay_event:
7390 case bp_jit_event:
7391 case bp_longjmp_master:
7392 case bp_std_terminate_master:
7393 case bp_exception_master:
7394 case bp_gnu_ifunc_resolver:
7395 case bp_gnu_ifunc_resolver_return:
7396 case bp_dprintf:
7397 loc->loc_type = bp_loc_software_breakpoint;
7398 mark_breakpoint_location_modified (loc);
7399 break;
7400 case bp_hardware_breakpoint:
7401 loc->loc_type = bp_loc_hardware_breakpoint;
7402 mark_breakpoint_location_modified (loc);
7403 break;
7404 case bp_hardware_watchpoint:
7405 case bp_read_watchpoint:
7406 case bp_access_watchpoint:
7407 loc->loc_type = bp_loc_hardware_watchpoint;
7408 break;
7409 case bp_watchpoint:
7410 case bp_catchpoint:
7411 case bp_tracepoint:
7412 case bp_fast_tracepoint:
7413 case bp_static_tracepoint:
7414 loc->loc_type = bp_loc_other;
7415 break;
7416 default:
7417 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7418 }
7419
7420 loc->refc = 1;
7421}
7422
7423/* Allocate a struct bp_location. */
7424
7425static struct bp_location *
7426allocate_bp_location (struct breakpoint *bpt)
7427{
7428 return bpt->ops->allocate_location (bpt);
7429}
7430
7431static void
7432free_bp_location (struct bp_location *loc)
7433{
7434 loc->ops->dtor (loc);
7435 xfree (loc);
7436}
7437
7438/* Increment reference count. */
7439
7440static void
7441incref_bp_location (struct bp_location *bl)
7442{
7443 ++bl->refc;
7444}
7445
7446/* Decrement reference count. If the reference count reaches 0,
7447 destroy the bp_location. Sets *BLP to NULL. */
7448
7449static void
7450decref_bp_location (struct bp_location **blp)
7451{
7452 gdb_assert ((*blp)->refc > 0);
7453
7454 if (--(*blp)->refc == 0)
7455 free_bp_location (*blp);
7456 *blp = NULL;
7457}
7458
7459/* Add breakpoint B at the end of the global breakpoint chain. */
7460
7461static void
7462add_to_breakpoint_chain (struct breakpoint *b)
7463{
7464 struct breakpoint *b1;
7465
7466 /* Add this breakpoint to the end of the chain so that a list of
7467 breakpoints will come out in order of increasing numbers. */
7468
7469 b1 = breakpoint_chain;
7470 if (b1 == 0)
7471 breakpoint_chain = b;
7472 else
7473 {
7474 while (b1->next)
7475 b1 = b1->next;
7476 b1->next = b;
7477 }
7478}
7479
7480/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7481
7482static void
7483init_raw_breakpoint_without_location (struct breakpoint *b,
7484 struct gdbarch *gdbarch,
7485 enum bptype bptype,
7486 const struct breakpoint_ops *ops)
7487{
7488 memset (b, 0, sizeof (*b));
7489
7490 gdb_assert (ops != NULL);
7491
7492 b->ops = ops;
7493 b->type = bptype;
7494 b->gdbarch = gdbarch;
7495 b->language = current_language->la_language;
7496 b->input_radix = input_radix;
7497 b->thread = -1;
7498 b->enable_state = bp_enabled;
7499 b->next = 0;
7500 b->silent = 0;
7501 b->ignore_count = 0;
7502 b->commands = NULL;
7503 b->frame_id = null_frame_id;
7504 b->condition_not_parsed = 0;
7505 b->py_bp_object = NULL;
7506 b->related_breakpoint = b;
7507 b->location = NULL;
7508}
7509
7510/* Helper to set_raw_breakpoint below. Creates a breakpoint
7511 that has type BPTYPE and has no locations as yet. */
7512
7513static struct breakpoint *
7514set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7515 enum bptype bptype,
7516 const struct breakpoint_ops *ops)
7517{
7518 struct breakpoint *b = XNEW (struct breakpoint);
7519
7520 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7521 add_to_breakpoint_chain (b);
7522 return b;
7523}
7524
7525/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7526 resolutions should be made as the user specified the location explicitly
7527 enough. */
7528
7529static void
7530set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7531{
7532 gdb_assert (loc->owner != NULL);
7533
7534 if (loc->owner->type == bp_breakpoint
7535 || loc->owner->type == bp_hardware_breakpoint
7536 || is_tracepoint (loc->owner))
7537 {
7538 int is_gnu_ifunc;
7539 const char *function_name;
7540 CORE_ADDR func_addr;
7541
7542 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7543 &func_addr, NULL, &is_gnu_ifunc);
7544
7545 if (is_gnu_ifunc && !explicit_loc)
7546 {
7547 struct breakpoint *b = loc->owner;
7548
7549 gdb_assert (loc->pspace == current_program_space);
7550 if (gnu_ifunc_resolve_name (function_name,
7551 &loc->requested_address))
7552 {
7553 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7554 loc->address = adjust_breakpoint_address (loc->gdbarch,
7555 loc->requested_address,
7556 b->type);
7557 }
7558 else if (b->type == bp_breakpoint && b->loc == loc
7559 && loc->next == NULL && b->related_breakpoint == b)
7560 {
7561 /* Create only the whole new breakpoint of this type but do not
7562 mess more complicated breakpoints with multiple locations. */
7563 b->type = bp_gnu_ifunc_resolver;
7564 /* Remember the resolver's address for use by the return
7565 breakpoint. */
7566 loc->related_address = func_addr;
7567 }
7568 }
7569
7570 if (function_name)
7571 loc->function_name = xstrdup (function_name);
7572 }
7573}
7574
7575/* Attempt to determine architecture of location identified by SAL. */
7576struct gdbarch *
7577get_sal_arch (struct symtab_and_line sal)
7578{
7579 if (sal.section)
7580 return get_objfile_arch (sal.section->objfile);
7581 if (sal.symtab)
7582 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7583
7584 return NULL;
7585}
7586
7587/* Low level routine for partially initializing a breakpoint of type
7588 BPTYPE. The newly created breakpoint's address, section, source
7589 file name, and line number are provided by SAL.
7590
7591 It is expected that the caller will complete the initialization of
7592 the newly created breakpoint struct as well as output any status
7593 information regarding the creation of a new breakpoint. */
7594
7595static void
7596init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7597 struct symtab_and_line sal, enum bptype bptype,
7598 const struct breakpoint_ops *ops)
7599{
7600 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7601
7602 add_location_to_breakpoint (b, &sal);
7603
7604 if (bptype != bp_catchpoint)
7605 gdb_assert (sal.pspace != NULL);
7606
7607 /* Store the program space that was used to set the breakpoint,
7608 except for ordinary breakpoints, which are independent of the
7609 program space. */
7610 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7611 b->pspace = sal.pspace;
7612}
7613
7614/* set_raw_breakpoint is a low level routine for allocating and
7615 partially initializing a breakpoint of type BPTYPE. The newly
7616 created breakpoint's address, section, source file name, and line
7617 number are provided by SAL. The newly created and partially
7618 initialized breakpoint is added to the breakpoint chain and
7619 is also returned as the value of this function.
7620
7621 It is expected that the caller will complete the initialization of
7622 the newly created breakpoint struct as well as output any status
7623 information regarding the creation of a new breakpoint. In
7624 particular, set_raw_breakpoint does NOT set the breakpoint
7625 number! Care should be taken to not allow an error to occur
7626 prior to completing the initialization of the breakpoint. If this
7627 should happen, a bogus breakpoint will be left on the chain. */
7628
7629struct breakpoint *
7630set_raw_breakpoint (struct gdbarch *gdbarch,
7631 struct symtab_and_line sal, enum bptype bptype,
7632 const struct breakpoint_ops *ops)
7633{
7634 struct breakpoint *b = XNEW (struct breakpoint);
7635
7636 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7637 add_to_breakpoint_chain (b);
7638 return b;
7639}
7640
7641/* Call this routine when stepping and nexting to enable a breakpoint
7642 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7643 initiated the operation. */
7644
7645void
7646set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7647{
7648 struct breakpoint *b, *b_tmp;
7649 int thread = tp->global_num;
7650
7651 /* To avoid having to rescan all objfile symbols at every step,
7652 we maintain a list of continually-inserted but always disabled
7653 longjmp "master" breakpoints. Here, we simply create momentary
7654 clones of those and enable them for the requested thread. */
7655 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7656 if (b->pspace == current_program_space
7657 && (b->type == bp_longjmp_master
7658 || b->type == bp_exception_master))
7659 {
7660 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7661 struct breakpoint *clone;
7662
7663 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7664 after their removal. */
7665 clone = momentary_breakpoint_from_master (b, type,
7666 &longjmp_breakpoint_ops, 1);
7667 clone->thread = thread;
7668 }
7669
7670 tp->initiating_frame = frame;
7671}
7672
7673/* Delete all longjmp breakpoints from THREAD. */
7674void
7675delete_longjmp_breakpoint (int thread)
7676{
7677 struct breakpoint *b, *b_tmp;
7678
7679 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7680 if (b->type == bp_longjmp || b->type == bp_exception)
7681 {
7682 if (b->thread == thread)
7683 delete_breakpoint (b);
7684 }
7685}
7686
7687void
7688delete_longjmp_breakpoint_at_next_stop (int thread)
7689{
7690 struct breakpoint *b, *b_tmp;
7691
7692 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7693 if (b->type == bp_longjmp || b->type == bp_exception)
7694 {
7695 if (b->thread == thread)
7696 b->disposition = disp_del_at_next_stop;
7697 }
7698}
7699
7700/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7701 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7702 pointer to any of them. Return NULL if this system cannot place longjmp
7703 breakpoints. */
7704
7705struct breakpoint *
7706set_longjmp_breakpoint_for_call_dummy (void)
7707{
7708 struct breakpoint *b, *retval = NULL;
7709
7710 ALL_BREAKPOINTS (b)
7711 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7712 {
7713 struct breakpoint *new_b;
7714
7715 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7716 &momentary_breakpoint_ops,
7717 1);
7718 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7719
7720 /* Link NEW_B into the chain of RETVAL breakpoints. */
7721
7722 gdb_assert (new_b->related_breakpoint == new_b);
7723 if (retval == NULL)
7724 retval = new_b;
7725 new_b->related_breakpoint = retval;
7726 while (retval->related_breakpoint != new_b->related_breakpoint)
7727 retval = retval->related_breakpoint;
7728 retval->related_breakpoint = new_b;
7729 }
7730
7731 return retval;
7732}
7733
7734/* Verify all existing dummy frames and their associated breakpoints for
7735 TP. Remove those which can no longer be found in the current frame
7736 stack.
7737
7738 You should call this function only at places where it is safe to currently
7739 unwind the whole stack. Failed stack unwind would discard live dummy
7740 frames. */
7741
7742void
7743check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7744{
7745 struct breakpoint *b, *b_tmp;
7746
7747 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7748 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7749 {
7750 struct breakpoint *dummy_b = b->related_breakpoint;
7751
7752 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7753 dummy_b = dummy_b->related_breakpoint;
7754 if (dummy_b->type != bp_call_dummy
7755 || frame_find_by_id (dummy_b->frame_id) != NULL)
7756 continue;
7757
7758 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7759
7760 while (b->related_breakpoint != b)
7761 {
7762 if (b_tmp == b->related_breakpoint)
7763 b_tmp = b->related_breakpoint->next;
7764 delete_breakpoint (b->related_breakpoint);
7765 }
7766 delete_breakpoint (b);
7767 }
7768}
7769
7770void
7771enable_overlay_breakpoints (void)
7772{
7773 struct breakpoint *b;
7774
7775 ALL_BREAKPOINTS (b)
7776 if (b->type == bp_overlay_event)
7777 {
7778 b->enable_state = bp_enabled;
7779 update_global_location_list (UGLL_MAY_INSERT);
7780 overlay_events_enabled = 1;
7781 }
7782}
7783
7784void
7785disable_overlay_breakpoints (void)
7786{
7787 struct breakpoint *b;
7788
7789 ALL_BREAKPOINTS (b)
7790 if (b->type == bp_overlay_event)
7791 {
7792 b->enable_state = bp_disabled;
7793 update_global_location_list (UGLL_DONT_INSERT);
7794 overlay_events_enabled = 0;
7795 }
7796}
7797
7798/* Set an active std::terminate breakpoint for each std::terminate
7799 master breakpoint. */
7800void
7801set_std_terminate_breakpoint (void)
7802{
7803 struct breakpoint *b, *b_tmp;
7804
7805 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7806 if (b->pspace == current_program_space
7807 && b->type == bp_std_terminate_master)
7808 {
7809 momentary_breakpoint_from_master (b, bp_std_terminate,
7810 &momentary_breakpoint_ops, 1);
7811 }
7812}
7813
7814/* Delete all the std::terminate breakpoints. */
7815void
7816delete_std_terminate_breakpoint (void)
7817{
7818 struct breakpoint *b, *b_tmp;
7819
7820 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7821 if (b->type == bp_std_terminate)
7822 delete_breakpoint (b);
7823}
7824
7825struct breakpoint *
7826create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7827{
7828 struct breakpoint *b;
7829
7830 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7831 &internal_breakpoint_ops);
7832
7833 b->enable_state = bp_enabled;
7834 /* location has to be used or breakpoint_re_set will delete me. */
7835 b->location = new_address_location (b->loc->address, NULL, 0);
7836
7837 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7838
7839 return b;
7840}
7841
7842struct lang_and_radix
7843 {
7844 enum language lang;
7845 int radix;
7846 };
7847
7848/* Create a breakpoint for JIT code registration and unregistration. */
7849
7850struct breakpoint *
7851create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7852{
7853 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7854 &internal_breakpoint_ops);
7855}
7856
7857/* Remove JIT code registration and unregistration breakpoint(s). */
7858
7859void
7860remove_jit_event_breakpoints (void)
7861{
7862 struct breakpoint *b, *b_tmp;
7863
7864 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7865 if (b->type == bp_jit_event
7866 && b->loc->pspace == current_program_space)
7867 delete_breakpoint (b);
7868}
7869
7870void
7871remove_solib_event_breakpoints (void)
7872{
7873 struct breakpoint *b, *b_tmp;
7874
7875 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7876 if (b->type == bp_shlib_event
7877 && b->loc->pspace == current_program_space)
7878 delete_breakpoint (b);
7879}
7880
7881/* See breakpoint.h. */
7882
7883void
7884remove_solib_event_breakpoints_at_next_stop (void)
7885{
7886 struct breakpoint *b, *b_tmp;
7887
7888 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7889 if (b->type == bp_shlib_event
7890 && b->loc->pspace == current_program_space)
7891 b->disposition = disp_del_at_next_stop;
7892}
7893
7894/* Helper for create_solib_event_breakpoint /
7895 create_and_insert_solib_event_breakpoint. Allows specifying which
7896 INSERT_MODE to pass through to update_global_location_list. */
7897
7898static struct breakpoint *
7899create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7900 enum ugll_insert_mode insert_mode)
7901{
7902 struct breakpoint *b;
7903
7904 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7905 &internal_breakpoint_ops);
7906 update_global_location_list_nothrow (insert_mode);
7907 return b;
7908}
7909
7910struct breakpoint *
7911create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7912{
7913 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7914}
7915
7916/* See breakpoint.h. */
7917
7918struct breakpoint *
7919create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7920{
7921 struct breakpoint *b;
7922
7923 /* Explicitly tell update_global_location_list to insert
7924 locations. */
7925 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7926 if (!b->loc->inserted)
7927 {
7928 delete_breakpoint (b);
7929 return NULL;
7930 }
7931 return b;
7932}
7933
7934/* Disable any breakpoints that are on code in shared libraries. Only
7935 apply to enabled breakpoints, disabled ones can just stay disabled. */
7936
7937void
7938disable_breakpoints_in_shlibs (void)
7939{
7940 struct bp_location *loc, **locp_tmp;
7941
7942 ALL_BP_LOCATIONS (loc, locp_tmp)
7943 {
7944 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7945 struct breakpoint *b = loc->owner;
7946
7947 /* We apply the check to all breakpoints, including disabled for
7948 those with loc->duplicate set. This is so that when breakpoint
7949 becomes enabled, or the duplicate is removed, gdb will try to
7950 insert all breakpoints. If we don't set shlib_disabled here,
7951 we'll try to insert those breakpoints and fail. */
7952 if (((b->type == bp_breakpoint)
7953 || (b->type == bp_jit_event)
7954 || (b->type == bp_hardware_breakpoint)
7955 || (is_tracepoint (b)))
7956 && loc->pspace == current_program_space
7957 && !loc->shlib_disabled
7958 && solib_name_from_address (loc->pspace, loc->address)
7959 )
7960 {
7961 loc->shlib_disabled = 1;
7962 }
7963 }
7964}
7965
7966/* Disable any breakpoints and tracepoints that are in SOLIB upon
7967 notification of unloaded_shlib. Only apply to enabled breakpoints,
7968 disabled ones can just stay disabled. */
7969
7970static void
7971disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7972{
7973 struct bp_location *loc, **locp_tmp;
7974 int disabled_shlib_breaks = 0;
7975
7976 /* SunOS a.out shared libraries are always mapped, so do not
7977 disable breakpoints; they will only be reported as unloaded
7978 through clear_solib when GDB discards its shared library
7979 list. See clear_solib for more information. */
7980 if (exec_bfd != NULL
7981 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7982 return;
7983
7984 ALL_BP_LOCATIONS (loc, locp_tmp)
7985 {
7986 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7987 struct breakpoint *b = loc->owner;
7988
7989 if (solib->pspace == loc->pspace
7990 && !loc->shlib_disabled
7991 && (((b->type == bp_breakpoint
7992 || b->type == bp_jit_event
7993 || b->type == bp_hardware_breakpoint)
7994 && (loc->loc_type == bp_loc_hardware_breakpoint
7995 || loc->loc_type == bp_loc_software_breakpoint))
7996 || is_tracepoint (b))
7997 && solib_contains_address_p (solib, loc->address))
7998 {
7999 loc->shlib_disabled = 1;
8000 /* At this point, we cannot rely on remove_breakpoint
8001 succeeding so we must mark the breakpoint as not inserted
8002 to prevent future errors occurring in remove_breakpoints. */
8003 loc->inserted = 0;
8004
8005 /* This may cause duplicate notifications for the same breakpoint. */
8006 observer_notify_breakpoint_modified (b);
8007
8008 if (!disabled_shlib_breaks)
8009 {
8010 target_terminal_ours_for_output ();
8011 warning (_("Temporarily disabling breakpoints "
8012 "for unloaded shared library \"%s\""),
8013 solib->so_name);
8014 }
8015 disabled_shlib_breaks = 1;
8016 }
8017 }
8018}
8019
8020/* Disable any breakpoints and tracepoints in OBJFILE upon
8021 notification of free_objfile. Only apply to enabled breakpoints,
8022 disabled ones can just stay disabled. */
8023
8024static void
8025disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8026{
8027 struct breakpoint *b;
8028
8029 if (objfile == NULL)
8030 return;
8031
8032 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8033 managed by the user with add-symbol-file/remove-symbol-file.
8034 Similarly to how breakpoints in shared libraries are handled in
8035 response to "nosharedlibrary", mark breakpoints in such modules
8036 shlib_disabled so they end up uninserted on the next global
8037 location list update. Shared libraries not loaded by the user
8038 aren't handled here -- they're already handled in
8039 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8040 solib_unloaded observer. We skip objfiles that are not
8041 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8042 main objfile). */
8043 if ((objfile->flags & OBJF_SHARED) == 0
8044 || (objfile->flags & OBJF_USERLOADED) == 0)
8045 return;
8046
8047 ALL_BREAKPOINTS (b)
8048 {
8049 struct bp_location *loc;
8050 int bp_modified = 0;
8051
8052 if (!is_breakpoint (b) && !is_tracepoint (b))
8053 continue;
8054
8055 for (loc = b->loc; loc != NULL; loc = loc->next)
8056 {
8057 CORE_ADDR loc_addr = loc->address;
8058
8059 if (loc->loc_type != bp_loc_hardware_breakpoint
8060 && loc->loc_type != bp_loc_software_breakpoint)
8061 continue;
8062
8063 if (loc->shlib_disabled != 0)
8064 continue;
8065
8066 if (objfile->pspace != loc->pspace)
8067 continue;
8068
8069 if (loc->loc_type != bp_loc_hardware_breakpoint
8070 && loc->loc_type != bp_loc_software_breakpoint)
8071 continue;
8072
8073 if (is_addr_in_objfile (loc_addr, objfile))
8074 {
8075 loc->shlib_disabled = 1;
8076 /* At this point, we don't know whether the object was
8077 unmapped from the inferior or not, so leave the
8078 inserted flag alone. We'll handle failure to
8079 uninsert quietly, in case the object was indeed
8080 unmapped. */
8081
8082 mark_breakpoint_location_modified (loc);
8083
8084 bp_modified = 1;
8085 }
8086 }
8087
8088 if (bp_modified)
8089 observer_notify_breakpoint_modified (b);
8090 }
8091}
8092
8093/* FORK & VFORK catchpoints. */
8094
8095/* An instance of this type is used to represent a fork or vfork
8096 catchpoint. It includes a "struct breakpoint" as a kind of base
8097 class; users downcast to "struct breakpoint *" when needed. A
8098 breakpoint is really of this type iff its ops pointer points to
8099 CATCH_FORK_BREAKPOINT_OPS. */
8100
8101struct fork_catchpoint
8102{
8103 /* The base class. */
8104 struct breakpoint base;
8105
8106 /* Process id of a child process whose forking triggered this
8107 catchpoint. This field is only valid immediately after this
8108 catchpoint has triggered. */
8109 ptid_t forked_inferior_pid;
8110};
8111
8112/* Implement the "insert" breakpoint_ops method for fork
8113 catchpoints. */
8114
8115static int
8116insert_catch_fork (struct bp_location *bl)
8117{
8118 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8119}
8120
8121/* Implement the "remove" breakpoint_ops method for fork
8122 catchpoints. */
8123
8124static int
8125remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8126{
8127 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8128}
8129
8130/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8131 catchpoints. */
8132
8133static int
8134breakpoint_hit_catch_fork (const struct bp_location *bl,
8135 struct address_space *aspace, CORE_ADDR bp_addr,
8136 const struct target_waitstatus *ws)
8137{
8138 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8139
8140 if (ws->kind != TARGET_WAITKIND_FORKED)
8141 return 0;
8142
8143 c->forked_inferior_pid = ws->value.related_pid;
8144 return 1;
8145}
8146
8147/* Implement the "print_it" breakpoint_ops method for fork
8148 catchpoints. */
8149
8150static enum print_stop_action
8151print_it_catch_fork (bpstat bs)
8152{
8153 struct ui_out *uiout = current_uiout;
8154 struct breakpoint *b = bs->breakpoint_at;
8155 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8156
8157 annotate_catchpoint (b->number);
8158 maybe_print_thread_hit_breakpoint (uiout);
8159 if (b->disposition == disp_del)
8160 ui_out_text (uiout, "Temporary catchpoint ");
8161 else
8162 ui_out_text (uiout, "Catchpoint ");
8163 if (ui_out_is_mi_like_p (uiout))
8164 {
8165 ui_out_field_string (uiout, "reason",
8166 async_reason_lookup (EXEC_ASYNC_FORK));
8167 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8168 }
8169 ui_out_field_int (uiout, "bkptno", b->number);
8170 ui_out_text (uiout, " (forked process ");
8171 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8172 ui_out_text (uiout, "), ");
8173 return PRINT_SRC_AND_LOC;
8174}
8175
8176/* Implement the "print_one" breakpoint_ops method for fork
8177 catchpoints. */
8178
8179static void
8180print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8181{
8182 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8183 struct value_print_options opts;
8184 struct ui_out *uiout = current_uiout;
8185
8186 get_user_print_options (&opts);
8187
8188 /* Field 4, the address, is omitted (which makes the columns not
8189 line up too nicely with the headers, but the effect is relatively
8190 readable). */
8191 if (opts.addressprint)
8192 ui_out_field_skip (uiout, "addr");
8193 annotate_field (5);
8194 ui_out_text (uiout, "fork");
8195 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8196 {
8197 ui_out_text (uiout, ", process ");
8198 ui_out_field_int (uiout, "what",
8199 ptid_get_pid (c->forked_inferior_pid));
8200 ui_out_spaces (uiout, 1);
8201 }
8202
8203 if (ui_out_is_mi_like_p (uiout))
8204 ui_out_field_string (uiout, "catch-type", "fork");
8205}
8206
8207/* Implement the "print_mention" breakpoint_ops method for fork
8208 catchpoints. */
8209
8210static void
8211print_mention_catch_fork (struct breakpoint *b)
8212{
8213 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8214}
8215
8216/* Implement the "print_recreate" breakpoint_ops method for fork
8217 catchpoints. */
8218
8219static void
8220print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8221{
8222 fprintf_unfiltered (fp, "catch fork");
8223 print_recreate_thread (b, fp);
8224}
8225
8226/* The breakpoint_ops structure to be used in fork catchpoints. */
8227
8228static struct breakpoint_ops catch_fork_breakpoint_ops;
8229
8230/* Implement the "insert" breakpoint_ops method for vfork
8231 catchpoints. */
8232
8233static int
8234insert_catch_vfork (struct bp_location *bl)
8235{
8236 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8237}
8238
8239/* Implement the "remove" breakpoint_ops method for vfork
8240 catchpoints. */
8241
8242static int
8243remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8244{
8245 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8246}
8247
8248/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8249 catchpoints. */
8250
8251static int
8252breakpoint_hit_catch_vfork (const struct bp_location *bl,
8253 struct address_space *aspace, CORE_ADDR bp_addr,
8254 const struct target_waitstatus *ws)
8255{
8256 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8257
8258 if (ws->kind != TARGET_WAITKIND_VFORKED)
8259 return 0;
8260
8261 c->forked_inferior_pid = ws->value.related_pid;
8262 return 1;
8263}
8264
8265/* Implement the "print_it" breakpoint_ops method for vfork
8266 catchpoints. */
8267
8268static enum print_stop_action
8269print_it_catch_vfork (bpstat bs)
8270{
8271 struct ui_out *uiout = current_uiout;
8272 struct breakpoint *b = bs->breakpoint_at;
8273 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8274
8275 annotate_catchpoint (b->number);
8276 maybe_print_thread_hit_breakpoint (uiout);
8277 if (b->disposition == disp_del)
8278 ui_out_text (uiout, "Temporary catchpoint ");
8279 else
8280 ui_out_text (uiout, "Catchpoint ");
8281 if (ui_out_is_mi_like_p (uiout))
8282 {
8283 ui_out_field_string (uiout, "reason",
8284 async_reason_lookup (EXEC_ASYNC_VFORK));
8285 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8286 }
8287 ui_out_field_int (uiout, "bkptno", b->number);
8288 ui_out_text (uiout, " (vforked process ");
8289 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8290 ui_out_text (uiout, "), ");
8291 return PRINT_SRC_AND_LOC;
8292}
8293
8294/* Implement the "print_one" breakpoint_ops method for vfork
8295 catchpoints. */
8296
8297static void
8298print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8299{
8300 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8301 struct value_print_options opts;
8302 struct ui_out *uiout = current_uiout;
8303
8304 get_user_print_options (&opts);
8305 /* Field 4, the address, is omitted (which makes the columns not
8306 line up too nicely with the headers, but the effect is relatively
8307 readable). */
8308 if (opts.addressprint)
8309 ui_out_field_skip (uiout, "addr");
8310 annotate_field (5);
8311 ui_out_text (uiout, "vfork");
8312 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8313 {
8314 ui_out_text (uiout, ", process ");
8315 ui_out_field_int (uiout, "what",
8316 ptid_get_pid (c->forked_inferior_pid));
8317 ui_out_spaces (uiout, 1);
8318 }
8319
8320 if (ui_out_is_mi_like_p (uiout))
8321 ui_out_field_string (uiout, "catch-type", "vfork");
8322}
8323
8324/* Implement the "print_mention" breakpoint_ops method for vfork
8325 catchpoints. */
8326
8327static void
8328print_mention_catch_vfork (struct breakpoint *b)
8329{
8330 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8331}
8332
8333/* Implement the "print_recreate" breakpoint_ops method for vfork
8334 catchpoints. */
8335
8336static void
8337print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8338{
8339 fprintf_unfiltered (fp, "catch vfork");
8340 print_recreate_thread (b, fp);
8341}
8342
8343/* The breakpoint_ops structure to be used in vfork catchpoints. */
8344
8345static struct breakpoint_ops catch_vfork_breakpoint_ops;
8346
8347/* An instance of this type is used to represent an solib catchpoint.
8348 It includes a "struct breakpoint" as a kind of base class; users
8349 downcast to "struct breakpoint *" when needed. A breakpoint is
8350 really of this type iff its ops pointer points to
8351 CATCH_SOLIB_BREAKPOINT_OPS. */
8352
8353struct solib_catchpoint
8354{
8355 /* The base class. */
8356 struct breakpoint base;
8357
8358 /* True for "catch load", false for "catch unload". */
8359 unsigned char is_load;
8360
8361 /* Regular expression to match, if any. COMPILED is only valid when
8362 REGEX is non-NULL. */
8363 char *regex;
8364 regex_t compiled;
8365};
8366
8367static void
8368dtor_catch_solib (struct breakpoint *b)
8369{
8370 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8371
8372 if (self->regex)
8373 regfree (&self->compiled);
8374 xfree (self->regex);
8375
8376 base_breakpoint_ops.dtor (b);
8377}
8378
8379static int
8380insert_catch_solib (struct bp_location *ignore)
8381{
8382 return 0;
8383}
8384
8385static int
8386remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8387{
8388 return 0;
8389}
8390
8391static int
8392breakpoint_hit_catch_solib (const struct bp_location *bl,
8393 struct address_space *aspace,
8394 CORE_ADDR bp_addr,
8395 const struct target_waitstatus *ws)
8396{
8397 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8398 struct breakpoint *other;
8399
8400 if (ws->kind == TARGET_WAITKIND_LOADED)
8401 return 1;
8402
8403 ALL_BREAKPOINTS (other)
8404 {
8405 struct bp_location *other_bl;
8406
8407 if (other == bl->owner)
8408 continue;
8409
8410 if (other->type != bp_shlib_event)
8411 continue;
8412
8413 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8414 continue;
8415
8416 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8417 {
8418 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8419 return 1;
8420 }
8421 }
8422
8423 return 0;
8424}
8425
8426static void
8427check_status_catch_solib (struct bpstats *bs)
8428{
8429 struct solib_catchpoint *self
8430 = (struct solib_catchpoint *) bs->breakpoint_at;
8431 int ix;
8432
8433 if (self->is_load)
8434 {
8435 struct so_list *iter;
8436
8437 for (ix = 0;
8438 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8439 ix, iter);
8440 ++ix)
8441 {
8442 if (!self->regex
8443 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8444 return;
8445 }
8446 }
8447 else
8448 {
8449 char *iter;
8450
8451 for (ix = 0;
8452 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8453 ix, iter);
8454 ++ix)
8455 {
8456 if (!self->regex
8457 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8458 return;
8459 }
8460 }
8461
8462 bs->stop = 0;
8463 bs->print_it = print_it_noop;
8464}
8465
8466static enum print_stop_action
8467print_it_catch_solib (bpstat bs)
8468{
8469 struct breakpoint *b = bs->breakpoint_at;
8470 struct ui_out *uiout = current_uiout;
8471
8472 annotate_catchpoint (b->number);
8473 maybe_print_thread_hit_breakpoint (uiout);
8474 if (b->disposition == disp_del)
8475 ui_out_text (uiout, "Temporary catchpoint ");
8476 else
8477 ui_out_text (uiout, "Catchpoint ");
8478 ui_out_field_int (uiout, "bkptno", b->number);
8479 ui_out_text (uiout, "\n");
8480 if (ui_out_is_mi_like_p (uiout))
8481 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8482 print_solib_event (1);
8483 return PRINT_SRC_AND_LOC;
8484}
8485
8486static void
8487print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8488{
8489 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8490 struct value_print_options opts;
8491 struct ui_out *uiout = current_uiout;
8492 char *msg;
8493
8494 get_user_print_options (&opts);
8495 /* Field 4, the address, is omitted (which makes the columns not
8496 line up too nicely with the headers, but the effect is relatively
8497 readable). */
8498 if (opts.addressprint)
8499 {
8500 annotate_field (4);
8501 ui_out_field_skip (uiout, "addr");
8502 }
8503
8504 annotate_field (5);
8505 if (self->is_load)
8506 {
8507 if (self->regex)
8508 msg = xstrprintf (_("load of library matching %s"), self->regex);
8509 else
8510 msg = xstrdup (_("load of library"));
8511 }
8512 else
8513 {
8514 if (self->regex)
8515 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8516 else
8517 msg = xstrdup (_("unload of library"));
8518 }
8519 ui_out_field_string (uiout, "what", msg);
8520 xfree (msg);
8521
8522 if (ui_out_is_mi_like_p (uiout))
8523 ui_out_field_string (uiout, "catch-type",
8524 self->is_load ? "load" : "unload");
8525}
8526
8527static void
8528print_mention_catch_solib (struct breakpoint *b)
8529{
8530 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8531
8532 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8533 self->is_load ? "load" : "unload");
8534}
8535
8536static void
8537print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8538{
8539 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8540
8541 fprintf_unfiltered (fp, "%s %s",
8542 b->disposition == disp_del ? "tcatch" : "catch",
8543 self->is_load ? "load" : "unload");
8544 if (self->regex)
8545 fprintf_unfiltered (fp, " %s", self->regex);
8546 fprintf_unfiltered (fp, "\n");
8547}
8548
8549static struct breakpoint_ops catch_solib_breakpoint_ops;
8550
8551/* Shared helper function (MI and CLI) for creating and installing
8552 a shared object event catchpoint. If IS_LOAD is non-zero then
8553 the events to be caught are load events, otherwise they are
8554 unload events. If IS_TEMP is non-zero the catchpoint is a
8555 temporary one. If ENABLED is non-zero the catchpoint is
8556 created in an enabled state. */
8557
8558void
8559add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8560{
8561 struct solib_catchpoint *c;
8562 struct gdbarch *gdbarch = get_current_arch ();
8563 struct cleanup *cleanup;
8564
8565 if (!arg)
8566 arg = "";
8567 arg = skip_spaces (arg);
8568
8569 c = XCNEW (struct solib_catchpoint);
8570 cleanup = make_cleanup (xfree, c);
8571
8572 if (*arg != '\0')
8573 {
8574 int errcode;
8575
8576 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8577 if (errcode != 0)
8578 {
8579 char *err = get_regcomp_error (errcode, &c->compiled);
8580
8581 make_cleanup (xfree, err);
8582 error (_("Invalid regexp (%s): %s"), err, arg);
8583 }
8584 c->regex = xstrdup (arg);
8585 }
8586
8587 c->is_load = is_load;
8588 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8589 &catch_solib_breakpoint_ops);
8590
8591 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8592
8593 discard_cleanups (cleanup);
8594 install_breakpoint (0, &c->base, 1);
8595}
8596
8597/* A helper function that does all the work for "catch load" and
8598 "catch unload". */
8599
8600static void
8601catch_load_or_unload (char *arg, int from_tty, int is_load,
8602 struct cmd_list_element *command)
8603{
8604 int tempflag;
8605 const int enabled = 1;
8606
8607 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8608
8609 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8610}
8611
8612static void
8613catch_load_command_1 (char *arg, int from_tty,
8614 struct cmd_list_element *command)
8615{
8616 catch_load_or_unload (arg, from_tty, 1, command);
8617}
8618
8619static void
8620catch_unload_command_1 (char *arg, int from_tty,
8621 struct cmd_list_element *command)
8622{
8623 catch_load_or_unload (arg, from_tty, 0, command);
8624}
8625
8626/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8627 is non-zero, then make the breakpoint temporary. If COND_STRING is
8628 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8629 the breakpoint_ops structure associated to the catchpoint. */
8630
8631void
8632init_catchpoint (struct breakpoint *b,
8633 struct gdbarch *gdbarch, int tempflag,
8634 char *cond_string,
8635 const struct breakpoint_ops *ops)
8636{
8637 struct symtab_and_line sal;
8638
8639 init_sal (&sal);
8640 sal.pspace = current_program_space;
8641
8642 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8643
8644 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8645 b->disposition = tempflag ? disp_del : disp_donttouch;
8646}
8647
8648void
8649install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8650{
8651 add_to_breakpoint_chain (b);
8652 set_breakpoint_number (internal, b);
8653 if (is_tracepoint (b))
8654 set_tracepoint_count (breakpoint_count);
8655 if (!internal)
8656 mention (b);
8657 observer_notify_breakpoint_created (b);
8658
8659 if (update_gll)
8660 update_global_location_list (UGLL_MAY_INSERT);
8661}
8662
8663static void
8664create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8665 int tempflag, char *cond_string,
8666 const struct breakpoint_ops *ops)
8667{
8668 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8669
8670 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8671
8672 c->forked_inferior_pid = null_ptid;
8673
8674 install_breakpoint (0, &c->base, 1);
8675}
8676
8677/* Exec catchpoints. */
8678
8679/* An instance of this type is used to represent an exec catchpoint.
8680 It includes a "struct breakpoint" as a kind of base class; users
8681 downcast to "struct breakpoint *" when needed. A breakpoint is
8682 really of this type iff its ops pointer points to
8683 CATCH_EXEC_BREAKPOINT_OPS. */
8684
8685struct exec_catchpoint
8686{
8687 /* The base class. */
8688 struct breakpoint base;
8689
8690 /* Filename of a program whose exec triggered this catchpoint.
8691 This field is only valid immediately after this catchpoint has
8692 triggered. */
8693 char *exec_pathname;
8694};
8695
8696/* Implement the "dtor" breakpoint_ops method for exec
8697 catchpoints. */
8698
8699static void
8700dtor_catch_exec (struct breakpoint *b)
8701{
8702 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8703
8704 xfree (c->exec_pathname);
8705
8706 base_breakpoint_ops.dtor (b);
8707}
8708
8709static int
8710insert_catch_exec (struct bp_location *bl)
8711{
8712 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8713}
8714
8715static int
8716remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8717{
8718 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8719}
8720
8721static int
8722breakpoint_hit_catch_exec (const struct bp_location *bl,
8723 struct address_space *aspace, CORE_ADDR bp_addr,
8724 const struct target_waitstatus *ws)
8725{
8726 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8727
8728 if (ws->kind != TARGET_WAITKIND_EXECD)
8729 return 0;
8730
8731 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8732 return 1;
8733}
8734
8735static enum print_stop_action
8736print_it_catch_exec (bpstat bs)
8737{
8738 struct ui_out *uiout = current_uiout;
8739 struct breakpoint *b = bs->breakpoint_at;
8740 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8741
8742 annotate_catchpoint (b->number);
8743 maybe_print_thread_hit_breakpoint (uiout);
8744 if (b->disposition == disp_del)
8745 ui_out_text (uiout, "Temporary catchpoint ");
8746 else
8747 ui_out_text (uiout, "Catchpoint ");
8748 if (ui_out_is_mi_like_p (uiout))
8749 {
8750 ui_out_field_string (uiout, "reason",
8751 async_reason_lookup (EXEC_ASYNC_EXEC));
8752 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8753 }
8754 ui_out_field_int (uiout, "bkptno", b->number);
8755 ui_out_text (uiout, " (exec'd ");
8756 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8757 ui_out_text (uiout, "), ");
8758
8759 return PRINT_SRC_AND_LOC;
8760}
8761
8762static void
8763print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8764{
8765 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8766 struct value_print_options opts;
8767 struct ui_out *uiout = current_uiout;
8768
8769 get_user_print_options (&opts);
8770
8771 /* Field 4, the address, is omitted (which makes the columns
8772 not line up too nicely with the headers, but the effect
8773 is relatively readable). */
8774 if (opts.addressprint)
8775 ui_out_field_skip (uiout, "addr");
8776 annotate_field (5);
8777 ui_out_text (uiout, "exec");
8778 if (c->exec_pathname != NULL)
8779 {
8780 ui_out_text (uiout, ", program \"");
8781 ui_out_field_string (uiout, "what", c->exec_pathname);
8782 ui_out_text (uiout, "\" ");
8783 }
8784
8785 if (ui_out_is_mi_like_p (uiout))
8786 ui_out_field_string (uiout, "catch-type", "exec");
8787}
8788
8789static void
8790print_mention_catch_exec (struct breakpoint *b)
8791{
8792 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8793}
8794
8795/* Implement the "print_recreate" breakpoint_ops method for exec
8796 catchpoints. */
8797
8798static void
8799print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8800{
8801 fprintf_unfiltered (fp, "catch exec");
8802 print_recreate_thread (b, fp);
8803}
8804
8805static struct breakpoint_ops catch_exec_breakpoint_ops;
8806
8807static int
8808hw_breakpoint_used_count (void)
8809{
8810 int i = 0;
8811 struct breakpoint *b;
8812 struct bp_location *bl;
8813
8814 ALL_BREAKPOINTS (b)
8815 {
8816 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8817 for (bl = b->loc; bl; bl = bl->next)
8818 {
8819 /* Special types of hardware breakpoints may use more than
8820 one register. */
8821 i += b->ops->resources_needed (bl);
8822 }
8823 }
8824
8825 return i;
8826}
8827
8828/* Returns the resources B would use if it were a hardware
8829 watchpoint. */
8830
8831static int
8832hw_watchpoint_use_count (struct breakpoint *b)
8833{
8834 int i = 0;
8835 struct bp_location *bl;
8836
8837 if (!breakpoint_enabled (b))
8838 return 0;
8839
8840 for (bl = b->loc; bl; bl = bl->next)
8841 {
8842 /* Special types of hardware watchpoints may use more than
8843 one register. */
8844 i += b->ops->resources_needed (bl);
8845 }
8846
8847 return i;
8848}
8849
8850/* Returns the sum the used resources of all hardware watchpoints of
8851 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8852 the sum of the used resources of all hardware watchpoints of other
8853 types _not_ TYPE. */
8854
8855static int
8856hw_watchpoint_used_count_others (struct breakpoint *except,
8857 enum bptype type, int *other_type_used)
8858{
8859 int i = 0;
8860 struct breakpoint *b;
8861
8862 *other_type_used = 0;
8863 ALL_BREAKPOINTS (b)
8864 {
8865 if (b == except)
8866 continue;
8867 if (!breakpoint_enabled (b))
8868 continue;
8869
8870 if (b->type == type)
8871 i += hw_watchpoint_use_count (b);
8872 else if (is_hardware_watchpoint (b))
8873 *other_type_used = 1;
8874 }
8875
8876 return i;
8877}
8878
8879void
8880disable_watchpoints_before_interactive_call_start (void)
8881{
8882 struct breakpoint *b;
8883
8884 ALL_BREAKPOINTS (b)
8885 {
8886 if (is_watchpoint (b) && breakpoint_enabled (b))
8887 {
8888 b->enable_state = bp_call_disabled;
8889 update_global_location_list (UGLL_DONT_INSERT);
8890 }
8891 }
8892}
8893
8894void
8895enable_watchpoints_after_interactive_call_stop (void)
8896{
8897 struct breakpoint *b;
8898
8899 ALL_BREAKPOINTS (b)
8900 {
8901 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8902 {
8903 b->enable_state = bp_enabled;
8904 update_global_location_list (UGLL_MAY_INSERT);
8905 }
8906 }
8907}
8908
8909void
8910disable_breakpoints_before_startup (void)
8911{
8912 current_program_space->executing_startup = 1;
8913 update_global_location_list (UGLL_DONT_INSERT);
8914}
8915
8916void
8917enable_breakpoints_after_startup (void)
8918{
8919 current_program_space->executing_startup = 0;
8920 breakpoint_re_set ();
8921}
8922
8923/* Create a new single-step breakpoint for thread THREAD, with no
8924 locations. */
8925
8926static struct breakpoint *
8927new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8928{
8929 struct breakpoint *b = XNEW (struct breakpoint);
8930
8931 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8932 &momentary_breakpoint_ops);
8933
8934 b->disposition = disp_donttouch;
8935 b->frame_id = null_frame_id;
8936
8937 b->thread = thread;
8938 gdb_assert (b->thread != 0);
8939
8940 add_to_breakpoint_chain (b);
8941
8942 return b;
8943}
8944
8945/* Set a momentary breakpoint of type TYPE at address specified by
8946 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8947 frame. */
8948
8949struct breakpoint *
8950set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8951 struct frame_id frame_id, enum bptype type)
8952{
8953 struct breakpoint *b;
8954
8955 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8956 tail-called one. */
8957 gdb_assert (!frame_id_artificial_p (frame_id));
8958
8959 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8960 b->enable_state = bp_enabled;
8961 b->disposition = disp_donttouch;
8962 b->frame_id = frame_id;
8963
8964 /* If we're debugging a multi-threaded program, then we want
8965 momentary breakpoints to be active in only a single thread of
8966 control. */
8967 if (in_thread_list (inferior_ptid))
8968 b->thread = ptid_to_global_thread_id (inferior_ptid);
8969
8970 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8971
8972 return b;
8973}
8974
8975/* Make a momentary breakpoint based on the master breakpoint ORIG.
8976 The new breakpoint will have type TYPE, use OPS as its
8977 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8978
8979static struct breakpoint *
8980momentary_breakpoint_from_master (struct breakpoint *orig,
8981 enum bptype type,
8982 const struct breakpoint_ops *ops,
8983 int loc_enabled)
8984{
8985 struct breakpoint *copy;
8986
8987 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8988 copy->loc = allocate_bp_location (copy);
8989 set_breakpoint_location_function (copy->loc, 1);
8990
8991 copy->loc->gdbarch = orig->loc->gdbarch;
8992 copy->loc->requested_address = orig->loc->requested_address;
8993 copy->loc->address = orig->loc->address;
8994 copy->loc->section = orig->loc->section;
8995 copy->loc->pspace = orig->loc->pspace;
8996 copy->loc->probe = orig->loc->probe;
8997 copy->loc->line_number = orig->loc->line_number;
8998 copy->loc->symtab = orig->loc->symtab;
8999 copy->loc->enabled = loc_enabled;
9000 copy->frame_id = orig->frame_id;
9001 copy->thread = orig->thread;
9002 copy->pspace = orig->pspace;
9003
9004 copy->enable_state = bp_enabled;
9005 copy->disposition = disp_donttouch;
9006 copy->number = internal_breakpoint_number--;
9007
9008 update_global_location_list_nothrow (UGLL_DONT_INSERT);
9009 return copy;
9010}
9011
9012/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9013 ORIG is NULL. */
9014
9015struct breakpoint *
9016clone_momentary_breakpoint (struct breakpoint *orig)
9017{
9018 /* If there's nothing to clone, then return nothing. */
9019 if (orig == NULL)
9020 return NULL;
9021
9022 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9023}
9024
9025struct breakpoint *
9026set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9027 enum bptype type)
9028{
9029 struct symtab_and_line sal;
9030
9031 sal = find_pc_line (pc, 0);
9032 sal.pc = pc;
9033 sal.section = find_pc_overlay (pc);
9034 sal.explicit_pc = 1;
9035
9036 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9037}
9038\f
9039
9040/* Tell the user we have just set a breakpoint B. */
9041
9042static void
9043mention (struct breakpoint *b)
9044{
9045 b->ops->print_mention (b);
9046 if (ui_out_is_mi_like_p (current_uiout))
9047 return;
9048 printf_filtered ("\n");
9049}
9050\f
9051
9052static int bp_loc_is_permanent (struct bp_location *loc);
9053
9054static struct bp_location *
9055add_location_to_breakpoint (struct breakpoint *b,
9056 const struct symtab_and_line *sal)
9057{
9058 struct bp_location *loc, **tmp;
9059 CORE_ADDR adjusted_address;
9060 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9061
9062 if (loc_gdbarch == NULL)
9063 loc_gdbarch = b->gdbarch;
9064
9065 /* Adjust the breakpoint's address prior to allocating a location.
9066 Once we call allocate_bp_location(), that mostly uninitialized
9067 location will be placed on the location chain. Adjustment of the
9068 breakpoint may cause target_read_memory() to be called and we do
9069 not want its scan of the location chain to find a breakpoint and
9070 location that's only been partially initialized. */
9071 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9072 sal->pc, b->type);
9073
9074 /* Sort the locations by their ADDRESS. */
9075 loc = allocate_bp_location (b);
9076 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9077 tmp = &((*tmp)->next))
9078 ;
9079 loc->next = *tmp;
9080 *tmp = loc;
9081
9082 loc->requested_address = sal->pc;
9083 loc->address = adjusted_address;
9084 loc->pspace = sal->pspace;
9085 loc->probe.probe = sal->probe;
9086 loc->probe.objfile = sal->objfile;
9087 gdb_assert (loc->pspace != NULL);
9088 loc->section = sal->section;
9089 loc->gdbarch = loc_gdbarch;
9090 loc->line_number = sal->line;
9091 loc->symtab = sal->symtab;
9092
9093 set_breakpoint_location_function (loc,
9094 sal->explicit_pc || sal->explicit_line);
9095
9096 /* While by definition, permanent breakpoints are already present in the
9097 code, we don't mark the location as inserted. Normally one would expect
9098 that GDB could rely on that breakpoint instruction to stop the program,
9099 thus removing the need to insert its own breakpoint, except that executing
9100 the breakpoint instruction can kill the target instead of reporting a
9101 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9102 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9103 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9104 breakpoint be inserted normally results in QEMU knowing about the GDB
9105 breakpoint, and thus trap before the breakpoint instruction is executed.
9106 (If GDB later needs to continue execution past the permanent breakpoint,
9107 it manually increments the PC, thus avoiding executing the breakpoint
9108 instruction.) */
9109 if (bp_loc_is_permanent (loc))
9110 loc->permanent = 1;
9111
9112 return loc;
9113}
9114\f
9115
9116/* See breakpoint.h. */
9117
9118int
9119program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9120{
9121 int len;
9122 CORE_ADDR addr;
9123 const gdb_byte *bpoint;
9124 gdb_byte *target_mem;
9125 struct cleanup *cleanup;
9126 int retval = 0;
9127
9128 addr = address;
9129 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9130
9131 /* Software breakpoints unsupported? */
9132 if (bpoint == NULL)
9133 return 0;
9134
9135 target_mem = (gdb_byte *) alloca (len);
9136
9137 /* Enable the automatic memory restoration from breakpoints while
9138 we read the memory. Otherwise we could say about our temporary
9139 breakpoints they are permanent. */
9140 cleanup = make_show_memory_breakpoints_cleanup (0);
9141
9142 if (target_read_memory (address, target_mem, len) == 0
9143 && memcmp (target_mem, bpoint, len) == 0)
9144 retval = 1;
9145
9146 do_cleanups (cleanup);
9147
9148 return retval;
9149}
9150
9151/* Return 1 if LOC is pointing to a permanent breakpoint,
9152 return 0 otherwise. */
9153
9154static int
9155bp_loc_is_permanent (struct bp_location *loc)
9156{
9157 struct cleanup *cleanup;
9158 int retval;
9159
9160 gdb_assert (loc != NULL);
9161
9162 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9163 attempt to read from the addresses the locations of these breakpoint types
9164 point to. program_breakpoint_here_p, below, will attempt to read
9165 memory. */
9166 if (!breakpoint_address_is_meaningful (loc->owner))
9167 return 0;
9168
9169 cleanup = save_current_space_and_thread ();
9170 switch_to_program_space_and_thread (loc->pspace);
9171
9172 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9173
9174 do_cleanups (cleanup);
9175
9176 return retval;
9177}
9178
9179/* Build a command list for the dprintf corresponding to the current
9180 settings of the dprintf style options. */
9181
9182static void
9183update_dprintf_command_list (struct breakpoint *b)
9184{
9185 char *dprintf_args = b->extra_string;
9186 char *printf_line = NULL;
9187
9188 if (!dprintf_args)
9189 return;
9190
9191 dprintf_args = skip_spaces (dprintf_args);
9192
9193 /* Allow a comma, as it may have terminated a location, but don't
9194 insist on it. */
9195 if (*dprintf_args == ',')
9196 ++dprintf_args;
9197 dprintf_args = skip_spaces (dprintf_args);
9198
9199 if (*dprintf_args != '"')
9200 error (_("Bad format string, missing '\"'."));
9201
9202 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9203 printf_line = xstrprintf ("printf %s", dprintf_args);
9204 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9205 {
9206 if (!dprintf_function)
9207 error (_("No function supplied for dprintf call"));
9208
9209 if (dprintf_channel && strlen (dprintf_channel) > 0)
9210 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9211 dprintf_function,
9212 dprintf_channel,
9213 dprintf_args);
9214 else
9215 printf_line = xstrprintf ("call (void) %s (%s)",
9216 dprintf_function,
9217 dprintf_args);
9218 }
9219 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9220 {
9221 if (target_can_run_breakpoint_commands ())
9222 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9223 else
9224 {
9225 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9226 printf_line = xstrprintf ("printf %s", dprintf_args);
9227 }
9228 }
9229 else
9230 internal_error (__FILE__, __LINE__,
9231 _("Invalid dprintf style."));
9232
9233 gdb_assert (printf_line != NULL);
9234 /* Manufacture a printf sequence. */
9235 {
9236 struct command_line *printf_cmd_line = XNEW (struct command_line);
9237
9238 printf_cmd_line->control_type = simple_control;
9239 printf_cmd_line->body_count = 0;
9240 printf_cmd_line->body_list = NULL;
9241 printf_cmd_line->next = NULL;
9242 printf_cmd_line->line = printf_line;
9243
9244 breakpoint_set_commands (b, printf_cmd_line);
9245 }
9246}
9247
9248/* Update all dprintf commands, making their command lists reflect
9249 current style settings. */
9250
9251static void
9252update_dprintf_commands (char *args, int from_tty,
9253 struct cmd_list_element *c)
9254{
9255 struct breakpoint *b;
9256
9257 ALL_BREAKPOINTS (b)
9258 {
9259 if (b->type == bp_dprintf)
9260 update_dprintf_command_list (b);
9261 }
9262}
9263
9264/* Create a breakpoint with SAL as location. Use LOCATION
9265 as a description of the location, and COND_STRING
9266 as condition expression. If LOCATION is NULL then create an
9267 "address location" from the address in the SAL. */
9268
9269static void
9270init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9271 struct symtabs_and_lines sals,
9272 struct event_location *location,
9273 char *filter, char *cond_string,
9274 char *extra_string,
9275 enum bptype type, enum bpdisp disposition,
9276 int thread, int task, int ignore_count,
9277 const struct breakpoint_ops *ops, int from_tty,
9278 int enabled, int internal, unsigned flags,
9279 int display_canonical)
9280{
9281 int i;
9282
9283 if (type == bp_hardware_breakpoint)
9284 {
9285 int target_resources_ok;
9286
9287 i = hw_breakpoint_used_count ();
9288 target_resources_ok =
9289 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9290 i + 1, 0);
9291 if (target_resources_ok == 0)
9292 error (_("No hardware breakpoint support in the target."));
9293 else if (target_resources_ok < 0)
9294 error (_("Hardware breakpoints used exceeds limit."));
9295 }
9296
9297 gdb_assert (sals.nelts > 0);
9298
9299 for (i = 0; i < sals.nelts; ++i)
9300 {
9301 struct symtab_and_line sal = sals.sals[i];
9302 struct bp_location *loc;
9303
9304 if (from_tty)
9305 {
9306 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9307 if (!loc_gdbarch)
9308 loc_gdbarch = gdbarch;
9309
9310 describe_other_breakpoints (loc_gdbarch,
9311 sal.pspace, sal.pc, sal.section, thread);
9312 }
9313
9314 if (i == 0)
9315 {
9316 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9317 b->thread = thread;
9318 b->task = task;
9319
9320 b->cond_string = cond_string;
9321 b->extra_string = extra_string;
9322 b->ignore_count = ignore_count;
9323 b->enable_state = enabled ? bp_enabled : bp_disabled;
9324 b->disposition = disposition;
9325
9326 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9327 b->loc->inserted = 1;
9328
9329 if (type == bp_static_tracepoint)
9330 {
9331 struct tracepoint *t = (struct tracepoint *) b;
9332 struct static_tracepoint_marker marker;
9333
9334 if (strace_marker_p (b))
9335 {
9336 /* We already know the marker exists, otherwise, we
9337 wouldn't see a sal for it. */
9338 const char *p = &event_location_to_string (b->location)[3];
9339 const char *endp;
9340 char *marker_str;
9341
9342 p = skip_spaces_const (p);
9343
9344 endp = skip_to_space_const (p);
9345
9346 marker_str = savestring (p, endp - p);
9347 t->static_trace_marker_id = marker_str;
9348
9349 printf_filtered (_("Probed static tracepoint "
9350 "marker \"%s\"\n"),
9351 t->static_trace_marker_id);
9352 }
9353 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9354 {
9355 t->static_trace_marker_id = xstrdup (marker.str_id);
9356 release_static_tracepoint_marker (&marker);
9357
9358 printf_filtered (_("Probed static tracepoint "
9359 "marker \"%s\"\n"),
9360 t->static_trace_marker_id);
9361 }
9362 else
9363 warning (_("Couldn't determine the static "
9364 "tracepoint marker to probe"));
9365 }
9366
9367 loc = b->loc;
9368 }
9369 else
9370 {
9371 loc = add_location_to_breakpoint (b, &sal);
9372 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9373 loc->inserted = 1;
9374 }
9375
9376 if (b->cond_string)
9377 {
9378 const char *arg = b->cond_string;
9379
9380 loc->cond = parse_exp_1 (&arg, loc->address,
9381 block_for_pc (loc->address), 0);
9382 if (*arg)
9383 error (_("Garbage '%s' follows condition"), arg);
9384 }
9385
9386 /* Dynamic printf requires and uses additional arguments on the
9387 command line, otherwise it's an error. */
9388 if (type == bp_dprintf)
9389 {
9390 if (b->extra_string)
9391 update_dprintf_command_list (b);
9392 else
9393 error (_("Format string required"));
9394 }
9395 else if (b->extra_string)
9396 error (_("Garbage '%s' at end of command"), b->extra_string);
9397 }
9398
9399 b->display_canonical = display_canonical;
9400 if (location != NULL)
9401 b->location = location;
9402 else
9403 {
9404 const char *addr_string = NULL;
9405 int addr_string_len = 0;
9406
9407 if (location != NULL)
9408 addr_string = event_location_to_string (location);
9409 if (addr_string != NULL)
9410 addr_string_len = strlen (addr_string);
9411
9412 b->location = new_address_location (b->loc->address,
9413 addr_string, addr_string_len);
9414 }
9415 b->filter = filter;
9416}
9417
9418static void
9419create_breakpoint_sal (struct gdbarch *gdbarch,
9420 struct symtabs_and_lines sals,
9421 struct event_location *location,
9422 char *filter, char *cond_string,
9423 char *extra_string,
9424 enum bptype type, enum bpdisp disposition,
9425 int thread, int task, int ignore_count,
9426 const struct breakpoint_ops *ops, int from_tty,
9427 int enabled, int internal, unsigned flags,
9428 int display_canonical)
9429{
9430 struct breakpoint *b;
9431 struct cleanup *old_chain;
9432
9433 if (is_tracepoint_type (type))
9434 {
9435 struct tracepoint *t;
9436
9437 t = XCNEW (struct tracepoint);
9438 b = &t->base;
9439 }
9440 else
9441 b = XNEW (struct breakpoint);
9442
9443 old_chain = make_cleanup (xfree, b);
9444
9445 init_breakpoint_sal (b, gdbarch,
9446 sals, location,
9447 filter, cond_string, extra_string,
9448 type, disposition,
9449 thread, task, ignore_count,
9450 ops, from_tty,
9451 enabled, internal, flags,
9452 display_canonical);
9453 discard_cleanups (old_chain);
9454
9455 install_breakpoint (internal, b, 0);
9456}
9457
9458/* Add SALS.nelts breakpoints to the breakpoint table. For each
9459 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9460 value. COND_STRING, if not NULL, specified the condition to be
9461 used for all breakpoints. Essentially the only case where
9462 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9463 function. In that case, it's still not possible to specify
9464 separate conditions for different overloaded functions, so
9465 we take just a single condition string.
9466
9467 NOTE: If the function succeeds, the caller is expected to cleanup
9468 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9469 array contents). If the function fails (error() is called), the
9470 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9471 COND and SALS arrays and each of those arrays contents. */
9472
9473static void
9474create_breakpoints_sal (struct gdbarch *gdbarch,
9475 struct linespec_result *canonical,
9476 char *cond_string, char *extra_string,
9477 enum bptype type, enum bpdisp disposition,
9478 int thread, int task, int ignore_count,
9479 const struct breakpoint_ops *ops, int from_tty,
9480 int enabled, int internal, unsigned flags)
9481{
9482 int i;
9483 struct linespec_sals *lsal;
9484
9485 if (canonical->pre_expanded)
9486 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9487
9488 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9489 {
9490 /* Note that 'location' can be NULL in the case of a plain
9491 'break', without arguments. */
9492 struct event_location *location
9493 = (canonical->location != NULL
9494 ? copy_event_location (canonical->location) : NULL);
9495 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9496 struct cleanup *inner = make_cleanup_delete_event_location (location);
9497
9498 make_cleanup (xfree, filter_string);
9499 create_breakpoint_sal (gdbarch, lsal->sals,
9500 location,
9501 filter_string,
9502 cond_string, extra_string,
9503 type, disposition,
9504 thread, task, ignore_count, ops,
9505 from_tty, enabled, internal, flags,
9506 canonical->special_display);
9507 discard_cleanups (inner);
9508 }
9509}
9510
9511/* Parse LOCATION which is assumed to be a SAL specification possibly
9512 followed by conditionals. On return, SALS contains an array of SAL
9513 addresses found. LOCATION points to the end of the SAL (for
9514 linespec locations).
9515
9516 The array and the line spec strings are allocated on the heap, it is
9517 the caller's responsibility to free them. */
9518
9519static void
9520parse_breakpoint_sals (const struct event_location *location,
9521 struct linespec_result *canonical)
9522{
9523 struct symtab_and_line cursal;
9524
9525 if (event_location_type (location) == LINESPEC_LOCATION)
9526 {
9527 const char *address = get_linespec_location (location);
9528
9529 if (address == NULL)
9530 {
9531 /* The last displayed codepoint, if it's valid, is our default
9532 breakpoint address. */
9533 if (last_displayed_sal_is_valid ())
9534 {
9535 struct linespec_sals lsal;
9536 struct symtab_and_line sal;
9537 CORE_ADDR pc;
9538
9539 init_sal (&sal); /* Initialize to zeroes. */
9540 lsal.sals.sals = XNEW (struct symtab_and_line);
9541
9542 /* Set sal's pspace, pc, symtab, and line to the values
9543 corresponding to the last call to print_frame_info.
9544 Be sure to reinitialize LINE with NOTCURRENT == 0
9545 as the breakpoint line number is inappropriate otherwise.
9546 find_pc_line would adjust PC, re-set it back. */
9547 get_last_displayed_sal (&sal);
9548 pc = sal.pc;
9549 sal = find_pc_line (pc, 0);
9550
9551 /* "break" without arguments is equivalent to "break *PC"
9552 where PC is the last displayed codepoint's address. So
9553 make sure to set sal.explicit_pc to prevent GDB from
9554 trying to expand the list of sals to include all other
9555 instances with the same symtab and line. */
9556 sal.pc = pc;
9557 sal.explicit_pc = 1;
9558
9559 lsal.sals.sals[0] = sal;
9560 lsal.sals.nelts = 1;
9561 lsal.canonical = NULL;
9562
9563 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9564 return;
9565 }
9566 else
9567 error (_("No default breakpoint address now."));
9568 }
9569 }
9570
9571 /* Force almost all breakpoints to be in terms of the
9572 current_source_symtab (which is decode_line_1's default).
9573 This should produce the results we want almost all of the
9574 time while leaving default_breakpoint_* alone.
9575
9576 ObjC: However, don't match an Objective-C method name which
9577 may have a '+' or '-' succeeded by a '['. */
9578 cursal = get_current_source_symtab_and_line ();
9579 if (last_displayed_sal_is_valid ())
9580 {
9581 const char *address = NULL;
9582
9583 if (event_location_type (location) == LINESPEC_LOCATION)
9584 address = get_linespec_location (location);
9585
9586 if (!cursal.symtab
9587 || (address != NULL
9588 && strchr ("+-", address[0]) != NULL
9589 && address[1] != '['))
9590 {
9591 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9592 get_last_displayed_symtab (),
9593 get_last_displayed_line (),
9594 canonical, NULL, NULL);
9595 return;
9596 }
9597 }
9598
9599 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9600 cursal.symtab, cursal.line, canonical, NULL, NULL);
9601}
9602
9603
9604/* Convert each SAL into a real PC. Verify that the PC can be
9605 inserted as a breakpoint. If it can't throw an error. */
9606
9607static void
9608breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9609{
9610 int i;
9611
9612 for (i = 0; i < sals->nelts; i++)
9613 resolve_sal_pc (&sals->sals[i]);
9614}
9615
9616/* Fast tracepoints may have restrictions on valid locations. For
9617 instance, a fast tracepoint using a jump instead of a trap will
9618 likely have to overwrite more bytes than a trap would, and so can
9619 only be placed where the instruction is longer than the jump, or a
9620 multi-instruction sequence does not have a jump into the middle of
9621 it, etc. */
9622
9623static void
9624check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9625 struct symtabs_and_lines *sals)
9626{
9627 int i, rslt;
9628 struct symtab_and_line *sal;
9629 char *msg;
9630 struct cleanup *old_chain;
9631
9632 for (i = 0; i < sals->nelts; i++)
9633 {
9634 struct gdbarch *sarch;
9635
9636 sal = &sals->sals[i];
9637
9638 sarch = get_sal_arch (*sal);
9639 /* We fall back to GDBARCH if there is no architecture
9640 associated with SAL. */
9641 if (sarch == NULL)
9642 sarch = gdbarch;
9643 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9644 old_chain = make_cleanup (xfree, msg);
9645
9646 if (!rslt)
9647 error (_("May not have a fast tracepoint at %s%s"),
9648 paddress (sarch, sal->pc), (msg ? msg : ""));
9649
9650 do_cleanups (old_chain);
9651 }
9652}
9653
9654/* Given TOK, a string specification of condition and thread, as
9655 accepted by the 'break' command, extract the condition
9656 string and thread number and set *COND_STRING and *THREAD.
9657 PC identifies the context at which the condition should be parsed.
9658 If no condition is found, *COND_STRING is set to NULL.
9659 If no thread is found, *THREAD is set to -1. */
9660
9661static void
9662find_condition_and_thread (const char *tok, CORE_ADDR pc,
9663 char **cond_string, int *thread, int *task,
9664 char **rest)
9665{
9666 *cond_string = NULL;
9667 *thread = -1;
9668 *task = 0;
9669 *rest = NULL;
9670
9671 while (tok && *tok)
9672 {
9673 const char *end_tok;
9674 int toklen;
9675 const char *cond_start = NULL;
9676 const char *cond_end = NULL;
9677
9678 tok = skip_spaces_const (tok);
9679
9680 if ((*tok == '"' || *tok == ',') && rest)
9681 {
9682 *rest = savestring (tok, strlen (tok));
9683 return;
9684 }
9685
9686 end_tok = skip_to_space_const (tok);
9687
9688 toklen = end_tok - tok;
9689
9690 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9691 {
9692 struct expression *expr;
9693
9694 tok = cond_start = end_tok + 1;
9695 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9696 xfree (expr);
9697 cond_end = tok;
9698 *cond_string = savestring (cond_start, cond_end - cond_start);
9699 }
9700 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9701 {
9702 const char *tmptok;
9703 struct thread_info *thr;
9704
9705 tok = end_tok + 1;
9706 thr = parse_thread_id (tok, &tmptok);
9707 if (tok == tmptok)
9708 error (_("Junk after thread keyword."));
9709 *thread = thr->global_num;
9710 tok = tmptok;
9711 }
9712 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9713 {
9714 char *tmptok;
9715
9716 tok = end_tok + 1;
9717 *task = strtol (tok, &tmptok, 0);
9718 if (tok == tmptok)
9719 error (_("Junk after task keyword."));
9720 if (!valid_task_id (*task))
9721 error (_("Unknown task %d."), *task);
9722 tok = tmptok;
9723 }
9724 else if (rest)
9725 {
9726 *rest = savestring (tok, strlen (tok));
9727 return;
9728 }
9729 else
9730 error (_("Junk at end of arguments."));
9731 }
9732}
9733
9734/* Decode a static tracepoint marker spec. */
9735
9736static struct symtabs_and_lines
9737decode_static_tracepoint_spec (const char **arg_p)
9738{
9739 VEC(static_tracepoint_marker_p) *markers = NULL;
9740 struct symtabs_and_lines sals;
9741 struct cleanup *old_chain;
9742 const char *p = &(*arg_p)[3];
9743 const char *endp;
9744 char *marker_str;
9745 int i;
9746
9747 p = skip_spaces_const (p);
9748
9749 endp = skip_to_space_const (p);
9750
9751 marker_str = savestring (p, endp - p);
9752 old_chain = make_cleanup (xfree, marker_str);
9753
9754 markers = target_static_tracepoint_markers_by_strid (marker_str);
9755 if (VEC_empty(static_tracepoint_marker_p, markers))
9756 error (_("No known static tracepoint marker named %s"), marker_str);
9757
9758 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9759 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9760
9761 for (i = 0; i < sals.nelts; i++)
9762 {
9763 struct static_tracepoint_marker *marker;
9764
9765 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9766
9767 init_sal (&sals.sals[i]);
9768
9769 sals.sals[i] = find_pc_line (marker->address, 0);
9770 sals.sals[i].pc = marker->address;
9771
9772 release_static_tracepoint_marker (marker);
9773 }
9774
9775 do_cleanups (old_chain);
9776
9777 *arg_p = endp;
9778 return sals;
9779}
9780
9781/* See breakpoint.h. */
9782
9783int
9784create_breakpoint (struct gdbarch *gdbarch,
9785 const struct event_location *location, char *cond_string,
9786 int thread, char *extra_string,
9787 int parse_extra,
9788 int tempflag, enum bptype type_wanted,
9789 int ignore_count,
9790 enum auto_boolean pending_break_support,
9791 const struct breakpoint_ops *ops,
9792 int from_tty, int enabled, int internal,
9793 unsigned flags)
9794{
9795 struct linespec_result canonical;
9796 struct cleanup *old_chain;
9797 struct cleanup *bkpt_chain = NULL;
9798 int pending = 0;
9799 int task = 0;
9800 int prev_bkpt_count = breakpoint_count;
9801
9802 gdb_assert (ops != NULL);
9803
9804 /* If extra_string isn't useful, set it to NULL. */
9805 if (extra_string != NULL && *extra_string == '\0')
9806 extra_string = NULL;
9807
9808 init_linespec_result (&canonical);
9809
9810 TRY
9811 {
9812 ops->create_sals_from_location (location, &canonical, type_wanted);
9813 }
9814 CATCH (e, RETURN_MASK_ERROR)
9815 {
9816 /* If caller is interested in rc value from parse, set
9817 value. */
9818 if (e.error == NOT_FOUND_ERROR)
9819 {
9820 /* If pending breakpoint support is turned off, throw
9821 error. */
9822
9823 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9824 throw_exception (e);
9825
9826 exception_print (gdb_stderr, e);
9827
9828 /* If pending breakpoint support is auto query and the user
9829 selects no, then simply return the error code. */
9830 if (pending_break_support == AUTO_BOOLEAN_AUTO
9831 && !nquery (_("Make %s pending on future shared library load? "),
9832 bptype_string (type_wanted)))
9833 return 0;
9834
9835 /* At this point, either the user was queried about setting
9836 a pending breakpoint and selected yes, or pending
9837 breakpoint behavior is on and thus a pending breakpoint
9838 is defaulted on behalf of the user. */
9839 pending = 1;
9840 }
9841 else
9842 throw_exception (e);
9843 }
9844 END_CATCH
9845
9846 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9847 return 0;
9848
9849 /* Create a chain of things that always need to be cleaned up. */
9850 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9851
9852 /* ----------------------------- SNIP -----------------------------
9853 Anything added to the cleanup chain beyond this point is assumed
9854 to be part of a breakpoint. If the breakpoint create succeeds
9855 then the memory is not reclaimed. */
9856 bkpt_chain = make_cleanup (null_cleanup, 0);
9857
9858 /* Resolve all line numbers to PC's and verify that the addresses
9859 are ok for the target. */
9860 if (!pending)
9861 {
9862 int ix;
9863 struct linespec_sals *iter;
9864
9865 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9866 breakpoint_sals_to_pc (&iter->sals);
9867 }
9868
9869 /* Fast tracepoints may have additional restrictions on location. */
9870 if (!pending && type_wanted == bp_fast_tracepoint)
9871 {
9872 int ix;
9873 struct linespec_sals *iter;
9874
9875 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9876 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9877 }
9878
9879 /* Verify that condition can be parsed, before setting any
9880 breakpoints. Allocate a separate condition expression for each
9881 breakpoint. */
9882 if (!pending)
9883 {
9884 if (parse_extra)
9885 {
9886 char *rest;
9887 struct linespec_sals *lsal;
9888
9889 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9890
9891 /* Here we only parse 'arg' to separate condition
9892 from thread number, so parsing in context of first
9893 sal is OK. When setting the breakpoint we'll
9894 re-parse it in context of each sal. */
9895
9896 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9897 &cond_string, &thread, &task, &rest);
9898 if (cond_string)
9899 make_cleanup (xfree, cond_string);
9900 if (rest)
9901 make_cleanup (xfree, rest);
9902 if (rest)
9903 extra_string = rest;
9904 else
9905 extra_string = NULL;
9906 }
9907 else
9908 {
9909 if (type_wanted != bp_dprintf
9910 && extra_string != NULL && *extra_string != '\0')
9911 error (_("Garbage '%s' at end of location"), extra_string);
9912
9913 /* Create a private copy of condition string. */
9914 if (cond_string)
9915 {
9916 cond_string = xstrdup (cond_string);
9917 make_cleanup (xfree, cond_string);
9918 }
9919 /* Create a private copy of any extra string. */
9920 if (extra_string)
9921 {
9922 extra_string = xstrdup (extra_string);
9923 make_cleanup (xfree, extra_string);
9924 }
9925 }
9926
9927 ops->create_breakpoints_sal (gdbarch, &canonical,
9928 cond_string, extra_string, type_wanted,
9929 tempflag ? disp_del : disp_donttouch,
9930 thread, task, ignore_count, ops,
9931 from_tty, enabled, internal, flags);
9932 }
9933 else
9934 {
9935 struct breakpoint *b;
9936
9937 if (is_tracepoint_type (type_wanted))
9938 {
9939 struct tracepoint *t;
9940
9941 t = XCNEW (struct tracepoint);
9942 b = &t->base;
9943 }
9944 else
9945 b = XNEW (struct breakpoint);
9946
9947 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9948 b->location = copy_event_location (location);
9949
9950 if (parse_extra)
9951 b->cond_string = NULL;
9952 else
9953 {
9954 /* Create a private copy of condition string. */
9955 if (cond_string)
9956 {
9957 cond_string = xstrdup (cond_string);
9958 make_cleanup (xfree, cond_string);
9959 }
9960 b->cond_string = cond_string;
9961 b->thread = thread;
9962 }
9963
9964 /* Create a private copy of any extra string. */
9965 if (extra_string != NULL)
9966 {
9967 extra_string = xstrdup (extra_string);
9968 make_cleanup (xfree, extra_string);
9969 }
9970 b->extra_string = extra_string;
9971 b->ignore_count = ignore_count;
9972 b->disposition = tempflag ? disp_del : disp_donttouch;
9973 b->condition_not_parsed = 1;
9974 b->enable_state = enabled ? bp_enabled : bp_disabled;
9975 if ((type_wanted != bp_breakpoint
9976 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9977 b->pspace = current_program_space;
9978
9979 install_breakpoint (internal, b, 0);
9980 }
9981
9982 if (VEC_length (linespec_sals, canonical.sals) > 1)
9983 {
9984 warning (_("Multiple breakpoints were set.\nUse the "
9985 "\"delete\" command to delete unwanted breakpoints."));
9986 prev_breakpoint_count = prev_bkpt_count;
9987 }
9988
9989 /* That's it. Discard the cleanups for data inserted into the
9990 breakpoint. */
9991 discard_cleanups (bkpt_chain);
9992 /* But cleanup everything else. */
9993 do_cleanups (old_chain);
9994
9995 /* error call may happen here - have BKPT_CHAIN already discarded. */
9996 update_global_location_list (UGLL_MAY_INSERT);
9997
9998 return 1;
9999}
10000
10001/* Set a breakpoint.
10002 ARG is a string describing breakpoint address,
10003 condition, and thread.
10004 FLAG specifies if a breakpoint is hardware on,
10005 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10006 and BP_TEMPFLAG. */
10007
10008static void
10009break_command_1 (char *arg, int flag, int from_tty)
10010{
10011 int tempflag = flag & BP_TEMPFLAG;
10012 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10013 ? bp_hardware_breakpoint
10014 : bp_breakpoint);
10015 struct breakpoint_ops *ops;
10016 struct event_location *location;
10017 struct cleanup *cleanup;
10018
10019 location = string_to_event_location (&arg, current_language);
10020 cleanup = make_cleanup_delete_event_location (location);
10021
10022 /* Matching breakpoints on probes. */
10023 if (location != NULL
10024 && event_location_type (location) == PROBE_LOCATION)
10025 ops = &bkpt_probe_breakpoint_ops;
10026 else
10027 ops = &bkpt_breakpoint_ops;
10028
10029 create_breakpoint (get_current_arch (),
10030 location,
10031 NULL, 0, arg, 1 /* parse arg */,
10032 tempflag, type_wanted,
10033 0 /* Ignore count */,
10034 pending_break_support,
10035 ops,
10036 from_tty,
10037 1 /* enabled */,
10038 0 /* internal */,
10039 0);
10040 do_cleanups (cleanup);
10041}
10042
10043/* Helper function for break_command_1 and disassemble_command. */
10044
10045void
10046resolve_sal_pc (struct symtab_and_line *sal)
10047{
10048 CORE_ADDR pc;
10049
10050 if (sal->pc == 0 && sal->symtab != NULL)
10051 {
10052 if (!find_line_pc (sal->symtab, sal->line, &pc))
10053 error (_("No line %d in file \"%s\"."),
10054 sal->line, symtab_to_filename_for_display (sal->symtab));
10055 sal->pc = pc;
10056
10057 /* If this SAL corresponds to a breakpoint inserted using a line
10058 number, then skip the function prologue if necessary. */
10059 if (sal->explicit_line)
10060 skip_prologue_sal (sal);
10061 }
10062
10063 if (sal->section == 0 && sal->symtab != NULL)
10064 {
10065 const struct blockvector *bv;
10066 const struct block *b;
10067 struct symbol *sym;
10068
10069 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10070 SYMTAB_COMPUNIT (sal->symtab));
10071 if (bv != NULL)
10072 {
10073 sym = block_linkage_function (b);
10074 if (sym != NULL)
10075 {
10076 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10077 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10078 sym);
10079 }
10080 else
10081 {
10082 /* It really is worthwhile to have the section, so we'll
10083 just have to look harder. This case can be executed
10084 if we have line numbers but no functions (as can
10085 happen in assembly source). */
10086
10087 struct bound_minimal_symbol msym;
10088 struct cleanup *old_chain = save_current_space_and_thread ();
10089
10090 switch_to_program_space_and_thread (sal->pspace);
10091
10092 msym = lookup_minimal_symbol_by_pc (sal->pc);
10093 if (msym.minsym)
10094 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10095
10096 do_cleanups (old_chain);
10097 }
10098 }
10099 }
10100}
10101
10102void
10103break_command (char *arg, int from_tty)
10104{
10105 break_command_1 (arg, 0, from_tty);
10106}
10107
10108void
10109tbreak_command (char *arg, int from_tty)
10110{
10111 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10112}
10113
10114static void
10115hbreak_command (char *arg, int from_tty)
10116{
10117 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10118}
10119
10120static void
10121thbreak_command (char *arg, int from_tty)
10122{
10123 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10124}
10125
10126static void
10127stop_command (char *arg, int from_tty)
10128{
10129 printf_filtered (_("Specify the type of breakpoint to set.\n\
10130Usage: stop in <function | address>\n\
10131 stop at <line>\n"));
10132}
10133
10134static void
10135stopin_command (char *arg, int from_tty)
10136{
10137 int badInput = 0;
10138
10139 if (arg == (char *) NULL)
10140 badInput = 1;
10141 else if (*arg != '*')
10142 {
10143 char *argptr = arg;
10144 int hasColon = 0;
10145
10146 /* Look for a ':'. If this is a line number specification, then
10147 say it is bad, otherwise, it should be an address or
10148 function/method name. */
10149 while (*argptr && !hasColon)
10150 {
10151 hasColon = (*argptr == ':');
10152 argptr++;
10153 }
10154
10155 if (hasColon)
10156 badInput = (*argptr != ':'); /* Not a class::method */
10157 else
10158 badInput = isdigit (*arg); /* a simple line number */
10159 }
10160
10161 if (badInput)
10162 printf_filtered (_("Usage: stop in <function | address>\n"));
10163 else
10164 break_command_1 (arg, 0, from_tty);
10165}
10166
10167static void
10168stopat_command (char *arg, int from_tty)
10169{
10170 int badInput = 0;
10171
10172 if (arg == (char *) NULL || *arg == '*') /* no line number */
10173 badInput = 1;
10174 else
10175 {
10176 char *argptr = arg;
10177 int hasColon = 0;
10178
10179 /* Look for a ':'. If there is a '::' then get out, otherwise
10180 it is probably a line number. */
10181 while (*argptr && !hasColon)
10182 {
10183 hasColon = (*argptr == ':');
10184 argptr++;
10185 }
10186
10187 if (hasColon)
10188 badInput = (*argptr == ':'); /* we have class::method */
10189 else
10190 badInput = !isdigit (*arg); /* not a line number */
10191 }
10192
10193 if (badInput)
10194 printf_filtered (_("Usage: stop at <line>\n"));
10195 else
10196 break_command_1 (arg, 0, from_tty);
10197}
10198
10199/* The dynamic printf command is mostly like a regular breakpoint, but
10200 with a prewired command list consisting of a single output command,
10201 built from extra arguments supplied on the dprintf command
10202 line. */
10203
10204static void
10205dprintf_command (char *arg, int from_tty)
10206{
10207 struct event_location *location;
10208 struct cleanup *cleanup;
10209
10210 location = string_to_event_location (&arg, current_language);
10211 cleanup = make_cleanup_delete_event_location (location);
10212
10213 /* If non-NULL, ARG should have been advanced past the location;
10214 the next character must be ','. */
10215 if (arg != NULL)
10216 {
10217 if (arg[0] != ',' || arg[1] == '\0')
10218 error (_("Format string required"));
10219 else
10220 {
10221 /* Skip the comma. */
10222 ++arg;
10223 }
10224 }
10225
10226 create_breakpoint (get_current_arch (),
10227 location,
10228 NULL, 0, arg, 1 /* parse arg */,
10229 0, bp_dprintf,
10230 0 /* Ignore count */,
10231 pending_break_support,
10232 &dprintf_breakpoint_ops,
10233 from_tty,
10234 1 /* enabled */,
10235 0 /* internal */,
10236 0);
10237 do_cleanups (cleanup);
10238}
10239
10240static void
10241agent_printf_command (char *arg, int from_tty)
10242{
10243 error (_("May only run agent-printf on the target"));
10244}
10245
10246/* Implement the "breakpoint_hit" breakpoint_ops method for
10247 ranged breakpoints. */
10248
10249static int
10250breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10251 struct address_space *aspace,
10252 CORE_ADDR bp_addr,
10253 const struct target_waitstatus *ws)
10254{
10255 if (ws->kind != TARGET_WAITKIND_STOPPED
10256 || ws->value.sig != GDB_SIGNAL_TRAP)
10257 return 0;
10258
10259 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10260 bl->length, aspace, bp_addr);
10261}
10262
10263/* Implement the "resources_needed" breakpoint_ops method for
10264 ranged breakpoints. */
10265
10266static int
10267resources_needed_ranged_breakpoint (const struct bp_location *bl)
10268{
10269 return target_ranged_break_num_registers ();
10270}
10271
10272/* Implement the "print_it" breakpoint_ops method for
10273 ranged breakpoints. */
10274
10275static enum print_stop_action
10276print_it_ranged_breakpoint (bpstat bs)
10277{
10278 struct breakpoint *b = bs->breakpoint_at;
10279 struct bp_location *bl = b->loc;
10280 struct ui_out *uiout = current_uiout;
10281
10282 gdb_assert (b->type == bp_hardware_breakpoint);
10283
10284 /* Ranged breakpoints have only one location. */
10285 gdb_assert (bl && bl->next == NULL);
10286
10287 annotate_breakpoint (b->number);
10288
10289 maybe_print_thread_hit_breakpoint (uiout);
10290
10291 if (b->disposition == disp_del)
10292 ui_out_text (uiout, "Temporary ranged breakpoint ");
10293 else
10294 ui_out_text (uiout, "Ranged breakpoint ");
10295 if (ui_out_is_mi_like_p (uiout))
10296 {
10297 ui_out_field_string (uiout, "reason",
10298 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10299 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10300 }
10301 ui_out_field_int (uiout, "bkptno", b->number);
10302 ui_out_text (uiout, ", ");
10303
10304 return PRINT_SRC_AND_LOC;
10305}
10306
10307/* Implement the "print_one" breakpoint_ops method for
10308 ranged breakpoints. */
10309
10310static void
10311print_one_ranged_breakpoint (struct breakpoint *b,
10312 struct bp_location **last_loc)
10313{
10314 struct bp_location *bl = b->loc;
10315 struct value_print_options opts;
10316 struct ui_out *uiout = current_uiout;
10317
10318 /* Ranged breakpoints have only one location. */
10319 gdb_assert (bl && bl->next == NULL);
10320
10321 get_user_print_options (&opts);
10322
10323 if (opts.addressprint)
10324 /* We don't print the address range here, it will be printed later
10325 by print_one_detail_ranged_breakpoint. */
10326 ui_out_field_skip (uiout, "addr");
10327 annotate_field (5);
10328 print_breakpoint_location (b, bl);
10329 *last_loc = bl;
10330}
10331
10332/* Implement the "print_one_detail" breakpoint_ops method for
10333 ranged breakpoints. */
10334
10335static void
10336print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10337 struct ui_out *uiout)
10338{
10339 CORE_ADDR address_start, address_end;
10340 struct bp_location *bl = b->loc;
10341 struct ui_file *stb = mem_fileopen ();
10342 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10343
10344 gdb_assert (bl);
10345
10346 address_start = bl->address;
10347 address_end = address_start + bl->length - 1;
10348
10349 ui_out_text (uiout, "\taddress range: ");
10350 fprintf_unfiltered (stb, "[%s, %s]",
10351 print_core_address (bl->gdbarch, address_start),
10352 print_core_address (bl->gdbarch, address_end));
10353 ui_out_field_stream (uiout, "addr", stb);
10354 ui_out_text (uiout, "\n");
10355
10356 do_cleanups (cleanup);
10357}
10358
10359/* Implement the "print_mention" breakpoint_ops method for
10360 ranged breakpoints. */
10361
10362static void
10363print_mention_ranged_breakpoint (struct breakpoint *b)
10364{
10365 struct bp_location *bl = b->loc;
10366 struct ui_out *uiout = current_uiout;
10367
10368 gdb_assert (bl);
10369 gdb_assert (b->type == bp_hardware_breakpoint);
10370
10371 if (ui_out_is_mi_like_p (uiout))
10372 return;
10373
10374 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10375 b->number, paddress (bl->gdbarch, bl->address),
10376 paddress (bl->gdbarch, bl->address + bl->length - 1));
10377}
10378
10379/* Implement the "print_recreate" breakpoint_ops method for
10380 ranged breakpoints. */
10381
10382static void
10383print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10384{
10385 fprintf_unfiltered (fp, "break-range %s, %s",
10386 event_location_to_string (b->location),
10387 event_location_to_string (b->location_range_end));
10388 print_recreate_thread (b, fp);
10389}
10390
10391/* The breakpoint_ops structure to be used in ranged breakpoints. */
10392
10393static struct breakpoint_ops ranged_breakpoint_ops;
10394
10395/* Find the address where the end of the breakpoint range should be
10396 placed, given the SAL of the end of the range. This is so that if
10397 the user provides a line number, the end of the range is set to the
10398 last instruction of the given line. */
10399
10400static CORE_ADDR
10401find_breakpoint_range_end (struct symtab_and_line sal)
10402{
10403 CORE_ADDR end;
10404
10405 /* If the user provided a PC value, use it. Otherwise,
10406 find the address of the end of the given location. */
10407 if (sal.explicit_pc)
10408 end = sal.pc;
10409 else
10410 {
10411 int ret;
10412 CORE_ADDR start;
10413
10414 ret = find_line_pc_range (sal, &start, &end);
10415 if (!ret)
10416 error (_("Could not find location of the end of the range."));
10417
10418 /* find_line_pc_range returns the start of the next line. */
10419 end--;
10420 }
10421
10422 return end;
10423}
10424
10425/* Implement the "break-range" CLI command. */
10426
10427static void
10428break_range_command (char *arg, int from_tty)
10429{
10430 char *arg_start, *addr_string_start;
10431 struct linespec_result canonical_start, canonical_end;
10432 int bp_count, can_use_bp, length;
10433 CORE_ADDR end;
10434 struct breakpoint *b;
10435 struct symtab_and_line sal_start, sal_end;
10436 struct cleanup *cleanup_bkpt;
10437 struct linespec_sals *lsal_start, *lsal_end;
10438 struct event_location *start_location, *end_location;
10439
10440 /* We don't support software ranged breakpoints. */
10441 if (target_ranged_break_num_registers () < 0)
10442 error (_("This target does not support hardware ranged breakpoints."));
10443
10444 bp_count = hw_breakpoint_used_count ();
10445 bp_count += target_ranged_break_num_registers ();
10446 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10447 bp_count, 0);
10448 if (can_use_bp < 0)
10449 error (_("Hardware breakpoints used exceeds limit."));
10450
10451 arg = skip_spaces (arg);
10452 if (arg == NULL || arg[0] == '\0')
10453 error(_("No address range specified."));
10454
10455 init_linespec_result (&canonical_start);
10456
10457 arg_start = arg;
10458 start_location = string_to_event_location (&arg, current_language);
10459 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10460 parse_breakpoint_sals (start_location, &canonical_start);
10461 make_cleanup_destroy_linespec_result (&canonical_start);
10462
10463 if (arg[0] != ',')
10464 error (_("Too few arguments."));
10465 else if (VEC_empty (linespec_sals, canonical_start.sals))
10466 error (_("Could not find location of the beginning of the range."));
10467
10468 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10469
10470 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10471 || lsal_start->sals.nelts != 1)
10472 error (_("Cannot create a ranged breakpoint with multiple locations."));
10473
10474 sal_start = lsal_start->sals.sals[0];
10475 addr_string_start = savestring (arg_start, arg - arg_start);
10476 make_cleanup (xfree, addr_string_start);
10477
10478 arg++; /* Skip the comma. */
10479 arg = skip_spaces (arg);
10480
10481 /* Parse the end location. */
10482
10483 init_linespec_result (&canonical_end);
10484 arg_start = arg;
10485
10486 /* We call decode_line_full directly here instead of using
10487 parse_breakpoint_sals because we need to specify the start location's
10488 symtab and line as the default symtab and line for the end of the
10489 range. This makes it possible to have ranges like "foo.c:27, +14",
10490 where +14 means 14 lines from the start location. */
10491 end_location = string_to_event_location (&arg, current_language);
10492 make_cleanup_delete_event_location (end_location);
10493 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
10494 sal_start.symtab, sal_start.line,
10495 &canonical_end, NULL, NULL);
10496
10497 make_cleanup_destroy_linespec_result (&canonical_end);
10498
10499 if (VEC_empty (linespec_sals, canonical_end.sals))
10500 error (_("Could not find location of the end of the range."));
10501
10502 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10503 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10504 || lsal_end->sals.nelts != 1)
10505 error (_("Cannot create a ranged breakpoint with multiple locations."));
10506
10507 sal_end = lsal_end->sals.sals[0];
10508
10509 end = find_breakpoint_range_end (sal_end);
10510 if (sal_start.pc > end)
10511 error (_("Invalid address range, end precedes start."));
10512
10513 length = end - sal_start.pc + 1;
10514 if (length < 0)
10515 /* Length overflowed. */
10516 error (_("Address range too large."));
10517 else if (length == 1)
10518 {
10519 /* This range is simple enough to be handled by
10520 the `hbreak' command. */
10521 hbreak_command (addr_string_start, 1);
10522
10523 do_cleanups (cleanup_bkpt);
10524
10525 return;
10526 }
10527
10528 /* Now set up the breakpoint. */
10529 b = set_raw_breakpoint (get_current_arch (), sal_start,
10530 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10531 set_breakpoint_count (breakpoint_count + 1);
10532 b->number = breakpoint_count;
10533 b->disposition = disp_donttouch;
10534 b->location = copy_event_location (start_location);
10535 b->location_range_end = copy_event_location (end_location);
10536 b->loc->length = length;
10537
10538 do_cleanups (cleanup_bkpt);
10539
10540 mention (b);
10541 observer_notify_breakpoint_created (b);
10542 update_global_location_list (UGLL_MAY_INSERT);
10543}
10544
10545/* Return non-zero if EXP is verified as constant. Returned zero
10546 means EXP is variable. Also the constant detection may fail for
10547 some constant expressions and in such case still falsely return
10548 zero. */
10549
10550static int
10551watchpoint_exp_is_const (const struct expression *exp)
10552{
10553 int i = exp->nelts;
10554
10555 while (i > 0)
10556 {
10557 int oplenp, argsp;
10558
10559 /* We are only interested in the descriptor of each element. */
10560 operator_length (exp, i, &oplenp, &argsp);
10561 i -= oplenp;
10562
10563 switch (exp->elts[i].opcode)
10564 {
10565 case BINOP_ADD:
10566 case BINOP_SUB:
10567 case BINOP_MUL:
10568 case BINOP_DIV:
10569 case BINOP_REM:
10570 case BINOP_MOD:
10571 case BINOP_LSH:
10572 case BINOP_RSH:
10573 case BINOP_LOGICAL_AND:
10574 case BINOP_LOGICAL_OR:
10575 case BINOP_BITWISE_AND:
10576 case BINOP_BITWISE_IOR:
10577 case BINOP_BITWISE_XOR:
10578 case BINOP_EQUAL:
10579 case BINOP_NOTEQUAL:
10580 case BINOP_LESS:
10581 case BINOP_GTR:
10582 case BINOP_LEQ:
10583 case BINOP_GEQ:
10584 case BINOP_REPEAT:
10585 case BINOP_COMMA:
10586 case BINOP_EXP:
10587 case BINOP_MIN:
10588 case BINOP_MAX:
10589 case BINOP_INTDIV:
10590 case BINOP_CONCAT:
10591 case TERNOP_COND:
10592 case TERNOP_SLICE:
10593
10594 case OP_LONG:
10595 case OP_DOUBLE:
10596 case OP_DECFLOAT:
10597 case OP_LAST:
10598 case OP_COMPLEX:
10599 case OP_STRING:
10600 case OP_ARRAY:
10601 case OP_TYPE:
10602 case OP_TYPEOF:
10603 case OP_DECLTYPE:
10604 case OP_TYPEID:
10605 case OP_NAME:
10606 case OP_OBJC_NSSTRING:
10607
10608 case UNOP_NEG:
10609 case UNOP_LOGICAL_NOT:
10610 case UNOP_COMPLEMENT:
10611 case UNOP_ADDR:
10612 case UNOP_HIGH:
10613 case UNOP_CAST:
10614
10615 case UNOP_CAST_TYPE:
10616 case UNOP_REINTERPRET_CAST:
10617 case UNOP_DYNAMIC_CAST:
10618 /* Unary, binary and ternary operators: We have to check
10619 their operands. If they are constant, then so is the
10620 result of that operation. For instance, if A and B are
10621 determined to be constants, then so is "A + B".
10622
10623 UNOP_IND is one exception to the rule above, because the
10624 value of *ADDR is not necessarily a constant, even when
10625 ADDR is. */
10626 break;
10627
10628 case OP_VAR_VALUE:
10629 /* Check whether the associated symbol is a constant.
10630
10631 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10632 possible that a buggy compiler could mark a variable as
10633 constant even when it is not, and TYPE_CONST would return
10634 true in this case, while SYMBOL_CLASS wouldn't.
10635
10636 We also have to check for function symbols because they
10637 are always constant. */
10638 {
10639 struct symbol *s = exp->elts[i + 2].symbol;
10640
10641 if (SYMBOL_CLASS (s) != LOC_BLOCK
10642 && SYMBOL_CLASS (s) != LOC_CONST
10643 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10644 return 0;
10645 break;
10646 }
10647
10648 /* The default action is to return 0 because we are using
10649 the optimistic approach here: If we don't know something,
10650 then it is not a constant. */
10651 default:
10652 return 0;
10653 }
10654 }
10655
10656 return 1;
10657}
10658
10659/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10660
10661static void
10662dtor_watchpoint (struct breakpoint *self)
10663{
10664 struct watchpoint *w = (struct watchpoint *) self;
10665
10666 xfree (w->cond_exp);
10667 xfree (w->exp);
10668 xfree (w->exp_string);
10669 xfree (w->exp_string_reparse);
10670 value_free (w->val);
10671
10672 base_breakpoint_ops.dtor (self);
10673}
10674
10675/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10676
10677static void
10678re_set_watchpoint (struct breakpoint *b)
10679{
10680 struct watchpoint *w = (struct watchpoint *) b;
10681
10682 /* Watchpoint can be either on expression using entirely global
10683 variables, or it can be on local variables.
10684
10685 Watchpoints of the first kind are never auto-deleted, and even
10686 persist across program restarts. Since they can use variables
10687 from shared libraries, we need to reparse expression as libraries
10688 are loaded and unloaded.
10689
10690 Watchpoints on local variables can also change meaning as result
10691 of solib event. For example, if a watchpoint uses both a local
10692 and a global variables in expression, it's a local watchpoint,
10693 but unloading of a shared library will make the expression
10694 invalid. This is not a very common use case, but we still
10695 re-evaluate expression, to avoid surprises to the user.
10696
10697 Note that for local watchpoints, we re-evaluate it only if
10698 watchpoints frame id is still valid. If it's not, it means the
10699 watchpoint is out of scope and will be deleted soon. In fact,
10700 I'm not sure we'll ever be called in this case.
10701
10702 If a local watchpoint's frame id is still valid, then
10703 w->exp_valid_block is likewise valid, and we can safely use it.
10704
10705 Don't do anything about disabled watchpoints, since they will be
10706 reevaluated again when enabled. */
10707 update_watchpoint (w, 1 /* reparse */);
10708}
10709
10710/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10711
10712static int
10713insert_watchpoint (struct bp_location *bl)
10714{
10715 struct watchpoint *w = (struct watchpoint *) bl->owner;
10716 int length = w->exact ? 1 : bl->length;
10717
10718 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10719 w->cond_exp);
10720}
10721
10722/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10723
10724static int
10725remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10726{
10727 struct watchpoint *w = (struct watchpoint *) bl->owner;
10728 int length = w->exact ? 1 : bl->length;
10729
10730 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10731 w->cond_exp);
10732}
10733
10734static int
10735breakpoint_hit_watchpoint (const struct bp_location *bl,
10736 struct address_space *aspace, CORE_ADDR bp_addr,
10737 const struct target_waitstatus *ws)
10738{
10739 struct breakpoint *b = bl->owner;
10740 struct watchpoint *w = (struct watchpoint *) b;
10741
10742 /* Continuable hardware watchpoints are treated as non-existent if the
10743 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10744 some data address). Otherwise gdb won't stop on a break instruction
10745 in the code (not from a breakpoint) when a hardware watchpoint has
10746 been defined. Also skip watchpoints which we know did not trigger
10747 (did not match the data address). */
10748 if (is_hardware_watchpoint (b)
10749 && w->watchpoint_triggered == watch_triggered_no)
10750 return 0;
10751
10752 return 1;
10753}
10754
10755static void
10756check_status_watchpoint (bpstat bs)
10757{
10758 gdb_assert (is_watchpoint (bs->breakpoint_at));
10759
10760 bpstat_check_watchpoint (bs);
10761}
10762
10763/* Implement the "resources_needed" breakpoint_ops method for
10764 hardware watchpoints. */
10765
10766static int
10767resources_needed_watchpoint (const struct bp_location *bl)
10768{
10769 struct watchpoint *w = (struct watchpoint *) bl->owner;
10770 int length = w->exact? 1 : bl->length;
10771
10772 return target_region_ok_for_hw_watchpoint (bl->address, length);
10773}
10774
10775/* Implement the "works_in_software_mode" breakpoint_ops method for
10776 hardware watchpoints. */
10777
10778static int
10779works_in_software_mode_watchpoint (const struct breakpoint *b)
10780{
10781 /* Read and access watchpoints only work with hardware support. */
10782 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10783}
10784
10785static enum print_stop_action
10786print_it_watchpoint (bpstat bs)
10787{
10788 struct cleanup *old_chain;
10789 struct breakpoint *b;
10790 struct ui_file *stb;
10791 enum print_stop_action result;
10792 struct watchpoint *w;
10793 struct ui_out *uiout = current_uiout;
10794
10795 gdb_assert (bs->bp_location_at != NULL);
10796
10797 b = bs->breakpoint_at;
10798 w = (struct watchpoint *) b;
10799
10800 stb = mem_fileopen ();
10801 old_chain = make_cleanup_ui_file_delete (stb);
10802
10803 annotate_watchpoint (b->number);
10804 maybe_print_thread_hit_breakpoint (uiout);
10805
10806 switch (b->type)
10807 {
10808 case bp_watchpoint:
10809 case bp_hardware_watchpoint:
10810 if (ui_out_is_mi_like_p (uiout))
10811 ui_out_field_string
10812 (uiout, "reason",
10813 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10814 mention (b);
10815 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10816 ui_out_text (uiout, "\nOld value = ");
10817 watchpoint_value_print (bs->old_val, stb);
10818 ui_out_field_stream (uiout, "old", stb);
10819 ui_out_text (uiout, "\nNew value = ");
10820 watchpoint_value_print (w->val, stb);
10821 ui_out_field_stream (uiout, "new", stb);
10822 ui_out_text (uiout, "\n");
10823 /* More than one watchpoint may have been triggered. */
10824 result = PRINT_UNKNOWN;
10825 break;
10826
10827 case bp_read_watchpoint:
10828 if (ui_out_is_mi_like_p (uiout))
10829 ui_out_field_string
10830 (uiout, "reason",
10831 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10832 mention (b);
10833 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10834 ui_out_text (uiout, "\nValue = ");
10835 watchpoint_value_print (w->val, stb);
10836 ui_out_field_stream (uiout, "value", stb);
10837 ui_out_text (uiout, "\n");
10838 result = PRINT_UNKNOWN;
10839 break;
10840
10841 case bp_access_watchpoint:
10842 if (bs->old_val != NULL)
10843 {
10844 if (ui_out_is_mi_like_p (uiout))
10845 ui_out_field_string
10846 (uiout, "reason",
10847 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10848 mention (b);
10849 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10850 ui_out_text (uiout, "\nOld value = ");
10851 watchpoint_value_print (bs->old_val, stb);
10852 ui_out_field_stream (uiout, "old", stb);
10853 ui_out_text (uiout, "\nNew value = ");
10854 }
10855 else
10856 {
10857 mention (b);
10858 if (ui_out_is_mi_like_p (uiout))
10859 ui_out_field_string
10860 (uiout, "reason",
10861 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10862 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10863 ui_out_text (uiout, "\nValue = ");
10864 }
10865 watchpoint_value_print (w->val, stb);
10866 ui_out_field_stream (uiout, "new", stb);
10867 ui_out_text (uiout, "\n");
10868 result = PRINT_UNKNOWN;
10869 break;
10870 default:
10871 result = PRINT_UNKNOWN;
10872 }
10873
10874 do_cleanups (old_chain);
10875 return result;
10876}
10877
10878/* Implement the "print_mention" breakpoint_ops method for hardware
10879 watchpoints. */
10880
10881static void
10882print_mention_watchpoint (struct breakpoint *b)
10883{
10884 struct cleanup *ui_out_chain;
10885 struct watchpoint *w = (struct watchpoint *) b;
10886 struct ui_out *uiout = current_uiout;
10887
10888 switch (b->type)
10889 {
10890 case bp_watchpoint:
10891 ui_out_text (uiout, "Watchpoint ");
10892 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10893 break;
10894 case bp_hardware_watchpoint:
10895 ui_out_text (uiout, "Hardware watchpoint ");
10896 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10897 break;
10898 case bp_read_watchpoint:
10899 ui_out_text (uiout, "Hardware read watchpoint ");
10900 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10901 break;
10902 case bp_access_watchpoint:
10903 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10904 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10905 break;
10906 default:
10907 internal_error (__FILE__, __LINE__,
10908 _("Invalid hardware watchpoint type."));
10909 }
10910
10911 ui_out_field_int (uiout, "number", b->number);
10912 ui_out_text (uiout, ": ");
10913 ui_out_field_string (uiout, "exp", w->exp_string);
10914 do_cleanups (ui_out_chain);
10915}
10916
10917/* Implement the "print_recreate" breakpoint_ops method for
10918 watchpoints. */
10919
10920static void
10921print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10922{
10923 struct watchpoint *w = (struct watchpoint *) b;
10924
10925 switch (b->type)
10926 {
10927 case bp_watchpoint:
10928 case bp_hardware_watchpoint:
10929 fprintf_unfiltered (fp, "watch");
10930 break;
10931 case bp_read_watchpoint:
10932 fprintf_unfiltered (fp, "rwatch");
10933 break;
10934 case bp_access_watchpoint:
10935 fprintf_unfiltered (fp, "awatch");
10936 break;
10937 default:
10938 internal_error (__FILE__, __LINE__,
10939 _("Invalid watchpoint type."));
10940 }
10941
10942 fprintf_unfiltered (fp, " %s", w->exp_string);
10943 print_recreate_thread (b, fp);
10944}
10945
10946/* Implement the "explains_signal" breakpoint_ops method for
10947 watchpoints. */
10948
10949static int
10950explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10951{
10952 /* A software watchpoint cannot cause a signal other than
10953 GDB_SIGNAL_TRAP. */
10954 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10955 return 0;
10956
10957 return 1;
10958}
10959
10960/* The breakpoint_ops structure to be used in hardware watchpoints. */
10961
10962static struct breakpoint_ops watchpoint_breakpoint_ops;
10963
10964/* Implement the "insert" breakpoint_ops method for
10965 masked hardware watchpoints. */
10966
10967static int
10968insert_masked_watchpoint (struct bp_location *bl)
10969{
10970 struct watchpoint *w = (struct watchpoint *) bl->owner;
10971
10972 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10973 bl->watchpoint_type);
10974}
10975
10976/* Implement the "remove" breakpoint_ops method for
10977 masked hardware watchpoints. */
10978
10979static int
10980remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10981{
10982 struct watchpoint *w = (struct watchpoint *) bl->owner;
10983
10984 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10985 bl->watchpoint_type);
10986}
10987
10988/* Implement the "resources_needed" breakpoint_ops method for
10989 masked hardware watchpoints. */
10990
10991static int
10992resources_needed_masked_watchpoint (const struct bp_location *bl)
10993{
10994 struct watchpoint *w = (struct watchpoint *) bl->owner;
10995
10996 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10997}
10998
10999/* Implement the "works_in_software_mode" breakpoint_ops method for
11000 masked hardware watchpoints. */
11001
11002static int
11003works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11004{
11005 return 0;
11006}
11007
11008/* Implement the "print_it" breakpoint_ops method for
11009 masked hardware watchpoints. */
11010
11011static enum print_stop_action
11012print_it_masked_watchpoint (bpstat bs)
11013{
11014 struct breakpoint *b = bs->breakpoint_at;
11015 struct ui_out *uiout = current_uiout;
11016
11017 /* Masked watchpoints have only one location. */
11018 gdb_assert (b->loc && b->loc->next == NULL);
11019
11020 annotate_watchpoint (b->number);
11021 maybe_print_thread_hit_breakpoint (uiout);
11022
11023 switch (b->type)
11024 {
11025 case bp_hardware_watchpoint:
11026 if (ui_out_is_mi_like_p (uiout))
11027 ui_out_field_string
11028 (uiout, "reason",
11029 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11030 break;
11031
11032 case bp_read_watchpoint:
11033 if (ui_out_is_mi_like_p (uiout))
11034 ui_out_field_string
11035 (uiout, "reason",
11036 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11037 break;
11038
11039 case bp_access_watchpoint:
11040 if (ui_out_is_mi_like_p (uiout))
11041 ui_out_field_string
11042 (uiout, "reason",
11043 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11044 break;
11045 default:
11046 internal_error (__FILE__, __LINE__,
11047 _("Invalid hardware watchpoint type."));
11048 }
11049
11050 mention (b);
11051 ui_out_text (uiout, _("\n\
11052Check the underlying instruction at PC for the memory\n\
11053address and value which triggered this watchpoint.\n"));
11054 ui_out_text (uiout, "\n");
11055
11056 /* More than one watchpoint may have been triggered. */
11057 return PRINT_UNKNOWN;
11058}
11059
11060/* Implement the "print_one_detail" breakpoint_ops method for
11061 masked hardware watchpoints. */
11062
11063static void
11064print_one_detail_masked_watchpoint (const struct breakpoint *b,
11065 struct ui_out *uiout)
11066{
11067 struct watchpoint *w = (struct watchpoint *) b;
11068
11069 /* Masked watchpoints have only one location. */
11070 gdb_assert (b->loc && b->loc->next == NULL);
11071
11072 ui_out_text (uiout, "\tmask ");
11073 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11074 ui_out_text (uiout, "\n");
11075}
11076
11077/* Implement the "print_mention" breakpoint_ops method for
11078 masked hardware watchpoints. */
11079
11080static void
11081print_mention_masked_watchpoint (struct breakpoint *b)
11082{
11083 struct watchpoint *w = (struct watchpoint *) b;
11084 struct ui_out *uiout = current_uiout;
11085 struct cleanup *ui_out_chain;
11086
11087 switch (b->type)
11088 {
11089 case bp_hardware_watchpoint:
11090 ui_out_text (uiout, "Masked hardware watchpoint ");
11091 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11092 break;
11093 case bp_read_watchpoint:
11094 ui_out_text (uiout, "Masked hardware read watchpoint ");
11095 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11096 break;
11097 case bp_access_watchpoint:
11098 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11099 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11100 break;
11101 default:
11102 internal_error (__FILE__, __LINE__,
11103 _("Invalid hardware watchpoint type."));
11104 }
11105
11106 ui_out_field_int (uiout, "number", b->number);
11107 ui_out_text (uiout, ": ");
11108 ui_out_field_string (uiout, "exp", w->exp_string);
11109 do_cleanups (ui_out_chain);
11110}
11111
11112/* Implement the "print_recreate" breakpoint_ops method for
11113 masked hardware watchpoints. */
11114
11115static void
11116print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11117{
11118 struct watchpoint *w = (struct watchpoint *) b;
11119 char tmp[40];
11120
11121 switch (b->type)
11122 {
11123 case bp_hardware_watchpoint:
11124 fprintf_unfiltered (fp, "watch");
11125 break;
11126 case bp_read_watchpoint:
11127 fprintf_unfiltered (fp, "rwatch");
11128 break;
11129 case bp_access_watchpoint:
11130 fprintf_unfiltered (fp, "awatch");
11131 break;
11132 default:
11133 internal_error (__FILE__, __LINE__,
11134 _("Invalid hardware watchpoint type."));
11135 }
11136
11137 sprintf_vma (tmp, w->hw_wp_mask);
11138 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11139 print_recreate_thread (b, fp);
11140}
11141
11142/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11143
11144static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11145
11146/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11147
11148static int
11149is_masked_watchpoint (const struct breakpoint *b)
11150{
11151 return b->ops == &masked_watchpoint_breakpoint_ops;
11152}
11153
11154/* accessflag: hw_write: watch write,
11155 hw_read: watch read,
11156 hw_access: watch access (read or write) */
11157static void
11158watch_command_1 (const char *arg, int accessflag, int from_tty,
11159 int just_location, int internal)
11160{
11161 struct breakpoint *b, *scope_breakpoint = NULL;
11162 struct expression *exp;
11163 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11164 struct value *val, *mark, *result;
11165 int saved_bitpos = 0, saved_bitsize = 0;
11166 struct frame_info *frame;
11167 const char *exp_start = NULL;
11168 const char *exp_end = NULL;
11169 const char *tok, *end_tok;
11170 int toklen = -1;
11171 const char *cond_start = NULL;
11172 const char *cond_end = NULL;
11173 enum bptype bp_type;
11174 int thread = -1;
11175 int pc = 0;
11176 /* Flag to indicate whether we are going to use masks for
11177 the hardware watchpoint. */
11178 int use_mask = 0;
11179 CORE_ADDR mask = 0;
11180 struct watchpoint *w;
11181 char *expression;
11182 struct cleanup *back_to;
11183
11184 /* Make sure that we actually have parameters to parse. */
11185 if (arg != NULL && arg[0] != '\0')
11186 {
11187 const char *value_start;
11188
11189 exp_end = arg + strlen (arg);
11190
11191 /* Look for "parameter value" pairs at the end
11192 of the arguments string. */
11193 for (tok = exp_end - 1; tok > arg; tok--)
11194 {
11195 /* Skip whitespace at the end of the argument list. */
11196 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11197 tok--;
11198
11199 /* Find the beginning of the last token.
11200 This is the value of the parameter. */
11201 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11202 tok--;
11203 value_start = tok + 1;
11204
11205 /* Skip whitespace. */
11206 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11207 tok--;
11208
11209 end_tok = tok;
11210
11211 /* Find the beginning of the second to last token.
11212 This is the parameter itself. */
11213 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11214 tok--;
11215 tok++;
11216 toklen = end_tok - tok + 1;
11217
11218 if (toklen == 6 && startswith (tok, "thread"))
11219 {
11220 struct thread_info *thr;
11221 /* At this point we've found a "thread" token, which means
11222 the user is trying to set a watchpoint that triggers
11223 only in a specific thread. */
11224 const char *endp;
11225
11226 if (thread != -1)
11227 error(_("You can specify only one thread."));
11228
11229 /* Extract the thread ID from the next token. */
11230 thr = parse_thread_id (value_start, &endp);
11231
11232 /* Check if the user provided a valid thread ID. */
11233 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11234 invalid_thread_id_error (value_start);
11235
11236 thread = thr->global_num;
11237 }
11238 else if (toklen == 4 && startswith (tok, "mask"))
11239 {
11240 /* We've found a "mask" token, which means the user wants to
11241 create a hardware watchpoint that is going to have the mask
11242 facility. */
11243 struct value *mask_value, *mark;
11244
11245 if (use_mask)
11246 error(_("You can specify only one mask."));
11247
11248 use_mask = just_location = 1;
11249
11250 mark = value_mark ();
11251 mask_value = parse_to_comma_and_eval (&value_start);
11252 mask = value_as_address (mask_value);
11253 value_free_to_mark (mark);
11254 }
11255 else
11256 /* We didn't recognize what we found. We should stop here. */
11257 break;
11258
11259 /* Truncate the string and get rid of the "parameter value" pair before
11260 the arguments string is parsed by the parse_exp_1 function. */
11261 exp_end = tok;
11262 }
11263 }
11264 else
11265 exp_end = arg;
11266
11267 /* Parse the rest of the arguments. From here on out, everything
11268 is in terms of a newly allocated string instead of the original
11269 ARG. */
11270 innermost_block = NULL;
11271 expression = savestring (arg, exp_end - arg);
11272 back_to = make_cleanup (xfree, expression);
11273 exp_start = arg = expression;
11274 exp = parse_exp_1 (&arg, 0, 0, 0);
11275 exp_end = arg;
11276 /* Remove trailing whitespace from the expression before saving it.
11277 This makes the eventual display of the expression string a bit
11278 prettier. */
11279 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11280 --exp_end;
11281
11282 /* Checking if the expression is not constant. */
11283 if (watchpoint_exp_is_const (exp))
11284 {
11285 int len;
11286
11287 len = exp_end - exp_start;
11288 while (len > 0 && isspace (exp_start[len - 1]))
11289 len--;
11290 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11291 }
11292
11293 exp_valid_block = innermost_block;
11294 mark = value_mark ();
11295 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11296
11297 if (val != NULL && just_location)
11298 {
11299 saved_bitpos = value_bitpos (val);
11300 saved_bitsize = value_bitsize (val);
11301 }
11302
11303 if (just_location)
11304 {
11305 int ret;
11306
11307 exp_valid_block = NULL;
11308 val = value_addr (result);
11309 release_value (val);
11310 value_free_to_mark (mark);
11311
11312 if (use_mask)
11313 {
11314 ret = target_masked_watch_num_registers (value_as_address (val),
11315 mask);
11316 if (ret == -1)
11317 error (_("This target does not support masked watchpoints."));
11318 else if (ret == -2)
11319 error (_("Invalid mask or memory region."));
11320 }
11321 }
11322 else if (val != NULL)
11323 release_value (val);
11324
11325 tok = skip_spaces_const (arg);
11326 end_tok = skip_to_space_const (tok);
11327
11328 toklen = end_tok - tok;
11329 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11330 {
11331 struct expression *cond;
11332
11333 innermost_block = NULL;
11334 tok = cond_start = end_tok + 1;
11335 cond = parse_exp_1 (&tok, 0, 0, 0);
11336
11337 /* The watchpoint expression may not be local, but the condition
11338 may still be. E.g.: `watch global if local > 0'. */
11339 cond_exp_valid_block = innermost_block;
11340
11341 xfree (cond);
11342 cond_end = tok;
11343 }
11344 if (*tok)
11345 error (_("Junk at end of command."));
11346
11347 frame = block_innermost_frame (exp_valid_block);
11348
11349 /* If the expression is "local", then set up a "watchpoint scope"
11350 breakpoint at the point where we've left the scope of the watchpoint
11351 expression. Create the scope breakpoint before the watchpoint, so
11352 that we will encounter it first in bpstat_stop_status. */
11353 if (exp_valid_block && frame)
11354 {
11355 if (frame_id_p (frame_unwind_caller_id (frame)))
11356 {
11357 scope_breakpoint
11358 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11359 frame_unwind_caller_pc (frame),
11360 bp_watchpoint_scope,
11361 &momentary_breakpoint_ops);
11362
11363 scope_breakpoint->enable_state = bp_enabled;
11364
11365 /* Automatically delete the breakpoint when it hits. */
11366 scope_breakpoint->disposition = disp_del;
11367
11368 /* Only break in the proper frame (help with recursion). */
11369 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11370
11371 /* Set the address at which we will stop. */
11372 scope_breakpoint->loc->gdbarch
11373 = frame_unwind_caller_arch (frame);
11374 scope_breakpoint->loc->requested_address
11375 = frame_unwind_caller_pc (frame);
11376 scope_breakpoint->loc->address
11377 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11378 scope_breakpoint->loc->requested_address,
11379 scope_breakpoint->type);
11380 }
11381 }
11382
11383 /* Now set up the breakpoint. We create all watchpoints as hardware
11384 watchpoints here even if hardware watchpoints are turned off, a call
11385 to update_watchpoint later in this function will cause the type to
11386 drop back to bp_watchpoint (software watchpoint) if required. */
11387
11388 if (accessflag == hw_read)
11389 bp_type = bp_read_watchpoint;
11390 else if (accessflag == hw_access)
11391 bp_type = bp_access_watchpoint;
11392 else
11393 bp_type = bp_hardware_watchpoint;
11394
11395 w = XCNEW (struct watchpoint);
11396 b = &w->base;
11397 if (use_mask)
11398 init_raw_breakpoint_without_location (b, NULL, bp_type,
11399 &masked_watchpoint_breakpoint_ops);
11400 else
11401 init_raw_breakpoint_without_location (b, NULL, bp_type,
11402 &watchpoint_breakpoint_ops);
11403 b->thread = thread;
11404 b->disposition = disp_donttouch;
11405 b->pspace = current_program_space;
11406 w->exp = exp;
11407 w->exp_valid_block = exp_valid_block;
11408 w->cond_exp_valid_block = cond_exp_valid_block;
11409 if (just_location)
11410 {
11411 struct type *t = value_type (val);
11412 CORE_ADDR addr = value_as_address (val);
11413 char *name;
11414
11415 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11416 name = type_to_string (t);
11417
11418 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11419 core_addr_to_string (addr));
11420 xfree (name);
11421
11422 w->exp_string = xstrprintf ("-location %.*s",
11423 (int) (exp_end - exp_start), exp_start);
11424
11425 /* The above expression is in C. */
11426 b->language = language_c;
11427 }
11428 else
11429 w->exp_string = savestring (exp_start, exp_end - exp_start);
11430
11431 if (use_mask)
11432 {
11433 w->hw_wp_mask = mask;
11434 }
11435 else
11436 {
11437 w->val = val;
11438 w->val_bitpos = saved_bitpos;
11439 w->val_bitsize = saved_bitsize;
11440 w->val_valid = 1;
11441 }
11442
11443 if (cond_start)
11444 b->cond_string = savestring (cond_start, cond_end - cond_start);
11445 else
11446 b->cond_string = 0;
11447
11448 if (frame)
11449 {
11450 w->watchpoint_frame = get_frame_id (frame);
11451 w->watchpoint_thread = inferior_ptid;
11452 }
11453 else
11454 {
11455 w->watchpoint_frame = null_frame_id;
11456 w->watchpoint_thread = null_ptid;
11457 }
11458
11459 if (scope_breakpoint != NULL)
11460 {
11461 /* The scope breakpoint is related to the watchpoint. We will
11462 need to act on them together. */
11463 b->related_breakpoint = scope_breakpoint;
11464 scope_breakpoint->related_breakpoint = b;
11465 }
11466
11467 if (!just_location)
11468 value_free_to_mark (mark);
11469
11470 TRY
11471 {
11472 /* Finally update the new watchpoint. This creates the locations
11473 that should be inserted. */
11474 update_watchpoint (w, 1);
11475 }
11476 CATCH (e, RETURN_MASK_ALL)
11477 {
11478 delete_breakpoint (b);
11479 throw_exception (e);
11480 }
11481 END_CATCH
11482
11483 install_breakpoint (internal, b, 1);
11484 do_cleanups (back_to);
11485}
11486
11487/* Return count of debug registers needed to watch the given expression.
11488 If the watchpoint cannot be handled in hardware return zero. */
11489
11490static int
11491can_use_hardware_watchpoint (struct value *v)
11492{
11493 int found_memory_cnt = 0;
11494 struct value *head = v;
11495
11496 /* Did the user specifically forbid us to use hardware watchpoints? */
11497 if (!can_use_hw_watchpoints)
11498 return 0;
11499
11500 /* Make sure that the value of the expression depends only upon
11501 memory contents, and values computed from them within GDB. If we
11502 find any register references or function calls, we can't use a
11503 hardware watchpoint.
11504
11505 The idea here is that evaluating an expression generates a series
11506 of values, one holding the value of every subexpression. (The
11507 expression a*b+c has five subexpressions: a, b, a*b, c, and
11508 a*b+c.) GDB's values hold almost enough information to establish
11509 the criteria given above --- they identify memory lvalues,
11510 register lvalues, computed values, etcetera. So we can evaluate
11511 the expression, and then scan the chain of values that leaves
11512 behind to decide whether we can detect any possible change to the
11513 expression's final value using only hardware watchpoints.
11514
11515 However, I don't think that the values returned by inferior
11516 function calls are special in any way. So this function may not
11517 notice that an expression involving an inferior function call
11518 can't be watched with hardware watchpoints. FIXME. */
11519 for (; v; v = value_next (v))
11520 {
11521 if (VALUE_LVAL (v) == lval_memory)
11522 {
11523 if (v != head && value_lazy (v))
11524 /* A lazy memory lvalue in the chain is one that GDB never
11525 needed to fetch; we either just used its address (e.g.,
11526 `a' in `a.b') or we never needed it at all (e.g., `a'
11527 in `a,b'). This doesn't apply to HEAD; if that is
11528 lazy then it was not readable, but watch it anyway. */
11529 ;
11530 else
11531 {
11532 /* Ahh, memory we actually used! Check if we can cover
11533 it with hardware watchpoints. */
11534 struct type *vtype = check_typedef (value_type (v));
11535
11536 /* We only watch structs and arrays if user asked for it
11537 explicitly, never if they just happen to appear in a
11538 middle of some value chain. */
11539 if (v == head
11540 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11541 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11542 {
11543 CORE_ADDR vaddr = value_address (v);
11544 int len;
11545 int num_regs;
11546
11547 len = (target_exact_watchpoints
11548 && is_scalar_type_recursive (vtype))?
11549 1 : TYPE_LENGTH (value_type (v));
11550
11551 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11552 if (!num_regs)
11553 return 0;
11554 else
11555 found_memory_cnt += num_regs;
11556 }
11557 }
11558 }
11559 else if (VALUE_LVAL (v) != not_lval
11560 && deprecated_value_modifiable (v) == 0)
11561 return 0; /* These are values from the history (e.g., $1). */
11562 else if (VALUE_LVAL (v) == lval_register)
11563 return 0; /* Cannot watch a register with a HW watchpoint. */
11564 }
11565
11566 /* The expression itself looks suitable for using a hardware
11567 watchpoint, but give the target machine a chance to reject it. */
11568 return found_memory_cnt;
11569}
11570
11571void
11572watch_command_wrapper (char *arg, int from_tty, int internal)
11573{
11574 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11575}
11576
11577/* A helper function that looks for the "-location" argument and then
11578 calls watch_command_1. */
11579
11580static void
11581watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11582{
11583 int just_location = 0;
11584
11585 if (arg
11586 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11587 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11588 {
11589 arg = skip_spaces (arg);
11590 just_location = 1;
11591 }
11592
11593 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11594}
11595
11596static void
11597watch_command (char *arg, int from_tty)
11598{
11599 watch_maybe_just_location (arg, hw_write, from_tty);
11600}
11601
11602void
11603rwatch_command_wrapper (char *arg, int from_tty, int internal)
11604{
11605 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11606}
11607
11608static void
11609rwatch_command (char *arg, int from_tty)
11610{
11611 watch_maybe_just_location (arg, hw_read, from_tty);
11612}
11613
11614void
11615awatch_command_wrapper (char *arg, int from_tty, int internal)
11616{
11617 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11618}
11619
11620static void
11621awatch_command (char *arg, int from_tty)
11622{
11623 watch_maybe_just_location (arg, hw_access, from_tty);
11624}
11625\f
11626
11627/* Data for the FSM that manages the until(location)/advance commands
11628 in infcmd.c. Here because it uses the mechanisms of
11629 breakpoints. */
11630
11631struct until_break_fsm
11632{
11633 /* The base class. */
11634 struct thread_fsm thread_fsm;
11635
11636 /* The thread that as current when the command was executed. */
11637 int thread;
11638
11639 /* The breakpoint set at the destination location. */
11640 struct breakpoint *location_breakpoint;
11641
11642 /* Breakpoint set at the return address in the caller frame. May be
11643 NULL. */
11644 struct breakpoint *caller_breakpoint;
11645};
11646
11647static void until_break_fsm_clean_up (struct thread_fsm *self,
11648 struct thread_info *thread);
11649static int until_break_fsm_should_stop (struct thread_fsm *self,
11650 struct thread_info *thread);
11651static enum async_reply_reason
11652 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11653
11654/* until_break_fsm's vtable. */
11655
11656static struct thread_fsm_ops until_break_fsm_ops =
11657{
11658 NULL, /* dtor */
11659 until_break_fsm_clean_up,
11660 until_break_fsm_should_stop,
11661 NULL, /* return_value */
11662 until_break_fsm_async_reply_reason,
11663};
11664
11665/* Allocate a new until_break_command_fsm. */
11666
11667static struct until_break_fsm *
11668new_until_break_fsm (struct interp *cmd_interp, int thread,
11669 struct breakpoint *location_breakpoint,
11670 struct breakpoint *caller_breakpoint)
11671{
11672 struct until_break_fsm *sm;
11673
11674 sm = XCNEW (struct until_break_fsm);
11675 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11676
11677 sm->thread = thread;
11678 sm->location_breakpoint = location_breakpoint;
11679 sm->caller_breakpoint = caller_breakpoint;
11680
11681 return sm;
11682}
11683
11684/* Implementation of the 'should_stop' FSM method for the
11685 until(location)/advance commands. */
11686
11687static int
11688until_break_fsm_should_stop (struct thread_fsm *self,
11689 struct thread_info *tp)
11690{
11691 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11692
11693 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11694 sm->location_breakpoint) != NULL
11695 || (sm->caller_breakpoint != NULL
11696 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11697 sm->caller_breakpoint) != NULL))
11698 thread_fsm_set_finished (self);
11699
11700 return 1;
11701}
11702
11703/* Implementation of the 'clean_up' FSM method for the
11704 until(location)/advance commands. */
11705
11706static void
11707until_break_fsm_clean_up (struct thread_fsm *self,
11708 struct thread_info *thread)
11709{
11710 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11711
11712 /* Clean up our temporary breakpoints. */
11713 if (sm->location_breakpoint != NULL)
11714 {
11715 delete_breakpoint (sm->location_breakpoint);
11716 sm->location_breakpoint = NULL;
11717 }
11718 if (sm->caller_breakpoint != NULL)
11719 {
11720 delete_breakpoint (sm->caller_breakpoint);
11721 sm->caller_breakpoint = NULL;
11722 }
11723 delete_longjmp_breakpoint (sm->thread);
11724}
11725
11726/* Implementation of the 'async_reply_reason' FSM method for the
11727 until(location)/advance commands. */
11728
11729static enum async_reply_reason
11730until_break_fsm_async_reply_reason (struct thread_fsm *self)
11731{
11732 return EXEC_ASYNC_LOCATION_REACHED;
11733}
11734
11735void
11736until_break_command (char *arg, int from_tty, int anywhere)
11737{
11738 struct symtabs_and_lines sals;
11739 struct symtab_and_line sal;
11740 struct frame_info *frame;
11741 struct gdbarch *frame_gdbarch;
11742 struct frame_id stack_frame_id;
11743 struct frame_id caller_frame_id;
11744 struct breakpoint *location_breakpoint;
11745 struct breakpoint *caller_breakpoint = NULL;
11746 struct cleanup *old_chain, *cleanup;
11747 int thread;
11748 struct thread_info *tp;
11749 struct event_location *location;
11750 struct until_break_fsm *sm;
11751
11752 clear_proceed_status (0);
11753
11754 /* Set a breakpoint where the user wants it and at return from
11755 this function. */
11756
11757 location = string_to_event_location (&arg, current_language);
11758 cleanup = make_cleanup_delete_event_location (location);
11759
11760 if (last_displayed_sal_is_valid ())
11761 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
11762 get_last_displayed_symtab (),
11763 get_last_displayed_line ());
11764 else
11765 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11766 NULL, (struct symtab *) NULL, 0);
11767
11768 if (sals.nelts != 1)
11769 error (_("Couldn't get information on specified line."));
11770
11771 sal = sals.sals[0];
11772 xfree (sals.sals); /* malloc'd, so freed. */
11773
11774 if (*arg)
11775 error (_("Junk at end of arguments."));
11776
11777 resolve_sal_pc (&sal);
11778
11779 tp = inferior_thread ();
11780 thread = tp->global_num;
11781
11782 old_chain = make_cleanup (null_cleanup, NULL);
11783
11784 /* Note linespec handling above invalidates the frame chain.
11785 Installing a breakpoint also invalidates the frame chain (as it
11786 may need to switch threads), so do any frame handling before
11787 that. */
11788
11789 frame = get_selected_frame (NULL);
11790 frame_gdbarch = get_frame_arch (frame);
11791 stack_frame_id = get_stack_frame_id (frame);
11792 caller_frame_id = frame_unwind_caller_id (frame);
11793
11794 /* Keep within the current frame, or in frames called by the current
11795 one. */
11796
11797 if (frame_id_p (caller_frame_id))
11798 {
11799 struct symtab_and_line sal2;
11800 struct gdbarch *caller_gdbarch;
11801
11802 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11803 sal2.pc = frame_unwind_caller_pc (frame);
11804 caller_gdbarch = frame_unwind_caller_arch (frame);
11805 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11806 sal2,
11807 caller_frame_id,
11808 bp_until);
11809 make_cleanup_delete_breakpoint (caller_breakpoint);
11810
11811 set_longjmp_breakpoint (tp, caller_frame_id);
11812 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11813 }
11814
11815 /* set_momentary_breakpoint could invalidate FRAME. */
11816 frame = NULL;
11817
11818 if (anywhere)
11819 /* If the user told us to continue until a specified location,
11820 we don't specify a frame at which we need to stop. */
11821 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11822 null_frame_id, bp_until);
11823 else
11824 /* Otherwise, specify the selected frame, because we want to stop
11825 only at the very same frame. */
11826 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11827 stack_frame_id, bp_until);
11828 make_cleanup_delete_breakpoint (location_breakpoint);
11829
11830 sm = new_until_break_fsm (command_interp (), tp->global_num,
11831 location_breakpoint, caller_breakpoint);
11832 tp->thread_fsm = &sm->thread_fsm;
11833
11834 discard_cleanups (old_chain);
11835
11836 proceed (-1, GDB_SIGNAL_DEFAULT);
11837
11838 do_cleanups (cleanup);
11839}
11840
11841/* This function attempts to parse an optional "if <cond>" clause
11842 from the arg string. If one is not found, it returns NULL.
11843
11844 Else, it returns a pointer to the condition string. (It does not
11845 attempt to evaluate the string against a particular block.) And,
11846 it updates arg to point to the first character following the parsed
11847 if clause in the arg string. */
11848
11849char *
11850ep_parse_optional_if_clause (char **arg)
11851{
11852 char *cond_string;
11853
11854 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11855 return NULL;
11856
11857 /* Skip the "if" keyword. */
11858 (*arg) += 2;
11859
11860 /* Skip any extra leading whitespace, and record the start of the
11861 condition string. */
11862 *arg = skip_spaces (*arg);
11863 cond_string = *arg;
11864
11865 /* Assume that the condition occupies the remainder of the arg
11866 string. */
11867 (*arg) += strlen (cond_string);
11868
11869 return cond_string;
11870}
11871
11872/* Commands to deal with catching events, such as signals, exceptions,
11873 process start/exit, etc. */
11874
11875typedef enum
11876{
11877 catch_fork_temporary, catch_vfork_temporary,
11878 catch_fork_permanent, catch_vfork_permanent
11879}
11880catch_fork_kind;
11881
11882static void
11883catch_fork_command_1 (char *arg, int from_tty,
11884 struct cmd_list_element *command)
11885{
11886 struct gdbarch *gdbarch = get_current_arch ();
11887 char *cond_string = NULL;
11888 catch_fork_kind fork_kind;
11889 int tempflag;
11890
11891 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11892 tempflag = (fork_kind == catch_fork_temporary
11893 || fork_kind == catch_vfork_temporary);
11894
11895 if (!arg)
11896 arg = "";
11897 arg = skip_spaces (arg);
11898
11899 /* The allowed syntax is:
11900 catch [v]fork
11901 catch [v]fork if <cond>
11902
11903 First, check if there's an if clause. */
11904 cond_string = ep_parse_optional_if_clause (&arg);
11905
11906 if ((*arg != '\0') && !isspace (*arg))
11907 error (_("Junk at end of arguments."));
11908
11909 /* If this target supports it, create a fork or vfork catchpoint
11910 and enable reporting of such events. */
11911 switch (fork_kind)
11912 {
11913 case catch_fork_temporary:
11914 case catch_fork_permanent:
11915 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11916 &catch_fork_breakpoint_ops);
11917 break;
11918 case catch_vfork_temporary:
11919 case catch_vfork_permanent:
11920 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11921 &catch_vfork_breakpoint_ops);
11922 break;
11923 default:
11924 error (_("unsupported or unknown fork kind; cannot catch it"));
11925 break;
11926 }
11927}
11928
11929static void
11930catch_exec_command_1 (char *arg, int from_tty,
11931 struct cmd_list_element *command)
11932{
11933 struct exec_catchpoint *c;
11934 struct gdbarch *gdbarch = get_current_arch ();
11935 int tempflag;
11936 char *cond_string = NULL;
11937
11938 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11939
11940 if (!arg)
11941 arg = "";
11942 arg = skip_spaces (arg);
11943
11944 /* The allowed syntax is:
11945 catch exec
11946 catch exec if <cond>
11947
11948 First, check if there's an if clause. */
11949 cond_string = ep_parse_optional_if_clause (&arg);
11950
11951 if ((*arg != '\0') && !isspace (*arg))
11952 error (_("Junk at end of arguments."));
11953
11954 c = XNEW (struct exec_catchpoint);
11955 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11956 &catch_exec_breakpoint_ops);
11957 c->exec_pathname = NULL;
11958
11959 install_breakpoint (0, &c->base, 1);
11960}
11961
11962void
11963init_ada_exception_breakpoint (struct breakpoint *b,
11964 struct gdbarch *gdbarch,
11965 struct symtab_and_line sal,
11966 char *addr_string,
11967 const struct breakpoint_ops *ops,
11968 int tempflag,
11969 int enabled,
11970 int from_tty)
11971{
11972 if (from_tty)
11973 {
11974 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11975 if (!loc_gdbarch)
11976 loc_gdbarch = gdbarch;
11977
11978 describe_other_breakpoints (loc_gdbarch,
11979 sal.pspace, sal.pc, sal.section, -1);
11980 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11981 version for exception catchpoints, because two catchpoints
11982 used for different exception names will use the same address.
11983 In this case, a "breakpoint ... also set at..." warning is
11984 unproductive. Besides, the warning phrasing is also a bit
11985 inappropriate, we should use the word catchpoint, and tell
11986 the user what type of catchpoint it is. The above is good
11987 enough for now, though. */
11988 }
11989
11990 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11991
11992 b->enable_state = enabled ? bp_enabled : bp_disabled;
11993 b->disposition = tempflag ? disp_del : disp_donttouch;
11994 b->location = string_to_event_location (&addr_string,
11995 language_def (language_ada));
11996 b->language = language_ada;
11997}
11998
11999static void
12000catch_command (char *arg, int from_tty)
12001{
12002 error (_("Catch requires an event name."));
12003}
12004\f
12005
12006static void
12007tcatch_command (char *arg, int from_tty)
12008{
12009 error (_("Catch requires an event name."));
12010}
12011
12012/* A qsort comparison function that sorts breakpoints in order. */
12013
12014static int
12015compare_breakpoints (const void *a, const void *b)
12016{
12017 const breakpoint_p *ba = (const breakpoint_p *) a;
12018 uintptr_t ua = (uintptr_t) *ba;
12019 const breakpoint_p *bb = (const breakpoint_p *) b;
12020 uintptr_t ub = (uintptr_t) *bb;
12021
12022 if ((*ba)->number < (*bb)->number)
12023 return -1;
12024 else if ((*ba)->number > (*bb)->number)
12025 return 1;
12026
12027 /* Now sort by address, in case we see, e..g, two breakpoints with
12028 the number 0. */
12029 if (ua < ub)
12030 return -1;
12031 return ua > ub ? 1 : 0;
12032}
12033
12034/* Delete breakpoints by address or line. */
12035
12036static void
12037clear_command (char *arg, int from_tty)
12038{
12039 struct breakpoint *b, *prev;
12040 VEC(breakpoint_p) *found = 0;
12041 int ix;
12042 int default_match;
12043 struct symtabs_and_lines sals;
12044 struct symtab_and_line sal;
12045 int i;
12046 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12047
12048 if (arg)
12049 {
12050 sals = decode_line_with_current_source (arg,
12051 (DECODE_LINE_FUNFIRSTLINE
12052 | DECODE_LINE_LIST_MODE));
12053 make_cleanup (xfree, sals.sals);
12054 default_match = 0;
12055 }
12056 else
12057 {
12058 sals.sals = XNEW (struct symtab_and_line);
12059 make_cleanup (xfree, sals.sals);
12060 init_sal (&sal); /* Initialize to zeroes. */
12061
12062 /* Set sal's line, symtab, pc, and pspace to the values
12063 corresponding to the last call to print_frame_info. If the
12064 codepoint is not valid, this will set all the fields to 0. */
12065 get_last_displayed_sal (&sal);
12066 if (sal.symtab == 0)
12067 error (_("No source file specified."));
12068
12069 sals.sals[0] = sal;
12070 sals.nelts = 1;
12071
12072 default_match = 1;
12073 }
12074
12075 /* We don't call resolve_sal_pc here. That's not as bad as it
12076 seems, because all existing breakpoints typically have both
12077 file/line and pc set. So, if clear is given file/line, we can
12078 match this to existing breakpoint without obtaining pc at all.
12079
12080 We only support clearing given the address explicitly
12081 present in breakpoint table. Say, we've set breakpoint
12082 at file:line. There were several PC values for that file:line,
12083 due to optimization, all in one block.
12084
12085 We've picked one PC value. If "clear" is issued with another
12086 PC corresponding to the same file:line, the breakpoint won't
12087 be cleared. We probably can still clear the breakpoint, but
12088 since the other PC value is never presented to user, user
12089 can only find it by guessing, and it does not seem important
12090 to support that. */
12091
12092 /* For each line spec given, delete bps which correspond to it. Do
12093 it in two passes, solely to preserve the current behavior that
12094 from_tty is forced true if we delete more than one
12095 breakpoint. */
12096
12097 found = NULL;
12098 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12099 for (i = 0; i < sals.nelts; i++)
12100 {
12101 const char *sal_fullname;
12102
12103 /* If exact pc given, clear bpts at that pc.
12104 If line given (pc == 0), clear all bpts on specified line.
12105 If defaulting, clear all bpts on default line
12106 or at default pc.
12107
12108 defaulting sal.pc != 0 tests to do
12109
12110 0 1 pc
12111 1 1 pc _and_ line
12112 0 0 line
12113 1 0 <can't happen> */
12114
12115 sal = sals.sals[i];
12116 sal_fullname = (sal.symtab == NULL
12117 ? NULL : symtab_to_fullname (sal.symtab));
12118
12119 /* Find all matching breakpoints and add them to 'found'. */
12120 ALL_BREAKPOINTS (b)
12121 {
12122 int match = 0;
12123 /* Are we going to delete b? */
12124 if (b->type != bp_none && !is_watchpoint (b))
12125 {
12126 struct bp_location *loc = b->loc;
12127 for (; loc; loc = loc->next)
12128 {
12129 /* If the user specified file:line, don't allow a PC
12130 match. This matches historical gdb behavior. */
12131 int pc_match = (!sal.explicit_line
12132 && sal.pc
12133 && (loc->pspace == sal.pspace)
12134 && (loc->address == sal.pc)
12135 && (!section_is_overlay (loc->section)
12136 || loc->section == sal.section));
12137 int line_match = 0;
12138
12139 if ((default_match || sal.explicit_line)
12140 && loc->symtab != NULL
12141 && sal_fullname != NULL
12142 && sal.pspace == loc->pspace
12143 && loc->line_number == sal.line
12144 && filename_cmp (symtab_to_fullname (loc->symtab),
12145 sal_fullname) == 0)
12146 line_match = 1;
12147
12148 if (pc_match || line_match)
12149 {
12150 match = 1;
12151 break;
12152 }
12153 }
12154 }
12155
12156 if (match)
12157 VEC_safe_push(breakpoint_p, found, b);
12158 }
12159 }
12160
12161 /* Now go thru the 'found' chain and delete them. */
12162 if (VEC_empty(breakpoint_p, found))
12163 {
12164 if (arg)
12165 error (_("No breakpoint at %s."), arg);
12166 else
12167 error (_("No breakpoint at this line."));
12168 }
12169
12170 /* Remove duplicates from the vec. */
12171 qsort (VEC_address (breakpoint_p, found),
12172 VEC_length (breakpoint_p, found),
12173 sizeof (breakpoint_p),
12174 compare_breakpoints);
12175 prev = VEC_index (breakpoint_p, found, 0);
12176 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12177 {
12178 if (b == prev)
12179 {
12180 VEC_ordered_remove (breakpoint_p, found, ix);
12181 --ix;
12182 }
12183 }
12184
12185 if (VEC_length(breakpoint_p, found) > 1)
12186 from_tty = 1; /* Always report if deleted more than one. */
12187 if (from_tty)
12188 {
12189 if (VEC_length(breakpoint_p, found) == 1)
12190 printf_unfiltered (_("Deleted breakpoint "));
12191 else
12192 printf_unfiltered (_("Deleted breakpoints "));
12193 }
12194
12195 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12196 {
12197 if (from_tty)
12198 printf_unfiltered ("%d ", b->number);
12199 delete_breakpoint (b);
12200 }
12201 if (from_tty)
12202 putchar_unfiltered ('\n');
12203
12204 do_cleanups (cleanups);
12205}
12206\f
12207/* Delete breakpoint in BS if they are `delete' breakpoints and
12208 all breakpoints that are marked for deletion, whether hit or not.
12209 This is called after any breakpoint is hit, or after errors. */
12210
12211void
12212breakpoint_auto_delete (bpstat bs)
12213{
12214 struct breakpoint *b, *b_tmp;
12215
12216 for (; bs; bs = bs->next)
12217 if (bs->breakpoint_at
12218 && bs->breakpoint_at->disposition == disp_del
12219 && bs->stop)
12220 delete_breakpoint (bs->breakpoint_at);
12221
12222 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12223 {
12224 if (b->disposition == disp_del_at_next_stop)
12225 delete_breakpoint (b);
12226 }
12227}
12228
12229/* A comparison function for bp_location AP and BP being interfaced to
12230 qsort. Sort elements primarily by their ADDRESS (no matter what
12231 does breakpoint_address_is_meaningful say for its OWNER),
12232 secondarily by ordering first permanent elements and
12233 terciarily just ensuring the array is sorted stable way despite
12234 qsort being an unstable algorithm. */
12235
12236static int
12237bp_location_compare (const void *ap, const void *bp)
12238{
12239 const struct bp_location *a = *(const struct bp_location **) ap;
12240 const struct bp_location *b = *(const struct bp_location **) bp;
12241
12242 if (a->address != b->address)
12243 return (a->address > b->address) - (a->address < b->address);
12244
12245 /* Sort locations at the same address by their pspace number, keeping
12246 locations of the same inferior (in a multi-inferior environment)
12247 grouped. */
12248
12249 if (a->pspace->num != b->pspace->num)
12250 return ((a->pspace->num > b->pspace->num)
12251 - (a->pspace->num < b->pspace->num));
12252
12253 /* Sort permanent breakpoints first. */
12254 if (a->permanent != b->permanent)
12255 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12256
12257 /* Make the internal GDB representation stable across GDB runs
12258 where A and B memory inside GDB can differ. Breakpoint locations of
12259 the same type at the same address can be sorted in arbitrary order. */
12260
12261 if (a->owner->number != b->owner->number)
12262 return ((a->owner->number > b->owner->number)
12263 - (a->owner->number < b->owner->number));
12264
12265 return (a > b) - (a < b);
12266}
12267
12268/* Set bp_location_placed_address_before_address_max and
12269 bp_location_shadow_len_after_address_max according to the current
12270 content of the bp_location array. */
12271
12272static void
12273bp_location_target_extensions_update (void)
12274{
12275 struct bp_location *bl, **blp_tmp;
12276
12277 bp_location_placed_address_before_address_max = 0;
12278 bp_location_shadow_len_after_address_max = 0;
12279
12280 ALL_BP_LOCATIONS (bl, blp_tmp)
12281 {
12282 CORE_ADDR start, end, addr;
12283
12284 if (!bp_location_has_shadow (bl))
12285 continue;
12286
12287 start = bl->target_info.placed_address;
12288 end = start + bl->target_info.shadow_len;
12289
12290 gdb_assert (bl->address >= start);
12291 addr = bl->address - start;
12292 if (addr > bp_location_placed_address_before_address_max)
12293 bp_location_placed_address_before_address_max = addr;
12294
12295 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12296
12297 gdb_assert (bl->address < end);
12298 addr = end - bl->address;
12299 if (addr > bp_location_shadow_len_after_address_max)
12300 bp_location_shadow_len_after_address_max = addr;
12301 }
12302}
12303
12304/* Download tracepoint locations if they haven't been. */
12305
12306static void
12307download_tracepoint_locations (void)
12308{
12309 struct breakpoint *b;
12310 struct cleanup *old_chain;
12311 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12312
12313 old_chain = save_current_space_and_thread ();
12314
12315 ALL_TRACEPOINTS (b)
12316 {
12317 struct bp_location *bl;
12318 struct tracepoint *t;
12319 int bp_location_downloaded = 0;
12320
12321 if ((b->type == bp_fast_tracepoint
12322 ? !may_insert_fast_tracepoints
12323 : !may_insert_tracepoints))
12324 continue;
12325
12326 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12327 {
12328 if (target_can_download_tracepoint ())
12329 can_download_tracepoint = TRIBOOL_TRUE;
12330 else
12331 can_download_tracepoint = TRIBOOL_FALSE;
12332 }
12333
12334 if (can_download_tracepoint == TRIBOOL_FALSE)
12335 break;
12336
12337 for (bl = b->loc; bl; bl = bl->next)
12338 {
12339 /* In tracepoint, locations are _never_ duplicated, so
12340 should_be_inserted is equivalent to
12341 unduplicated_should_be_inserted. */
12342 if (!should_be_inserted (bl) || bl->inserted)
12343 continue;
12344
12345 switch_to_program_space_and_thread (bl->pspace);
12346
12347 target_download_tracepoint (bl);
12348
12349 bl->inserted = 1;
12350 bp_location_downloaded = 1;
12351 }
12352 t = (struct tracepoint *) b;
12353 t->number_on_target = b->number;
12354 if (bp_location_downloaded)
12355 observer_notify_breakpoint_modified (b);
12356 }
12357
12358 do_cleanups (old_chain);
12359}
12360
12361/* Swap the insertion/duplication state between two locations. */
12362
12363static void
12364swap_insertion (struct bp_location *left, struct bp_location *right)
12365{
12366 const int left_inserted = left->inserted;
12367 const int left_duplicate = left->duplicate;
12368 const int left_needs_update = left->needs_update;
12369 const struct bp_target_info left_target_info = left->target_info;
12370
12371 /* Locations of tracepoints can never be duplicated. */
12372 if (is_tracepoint (left->owner))
12373 gdb_assert (!left->duplicate);
12374 if (is_tracepoint (right->owner))
12375 gdb_assert (!right->duplicate);
12376
12377 left->inserted = right->inserted;
12378 left->duplicate = right->duplicate;
12379 left->needs_update = right->needs_update;
12380 left->target_info = right->target_info;
12381 right->inserted = left_inserted;
12382 right->duplicate = left_duplicate;
12383 right->needs_update = left_needs_update;
12384 right->target_info = left_target_info;
12385}
12386
12387/* Force the re-insertion of the locations at ADDRESS. This is called
12388 once a new/deleted/modified duplicate location is found and we are evaluating
12389 conditions on the target's side. Such conditions need to be updated on
12390 the target. */
12391
12392static void
12393force_breakpoint_reinsertion (struct bp_location *bl)
12394{
12395 struct bp_location **locp = NULL, **loc2p;
12396 struct bp_location *loc;
12397 CORE_ADDR address = 0;
12398 int pspace_num;
12399
12400 address = bl->address;
12401 pspace_num = bl->pspace->num;
12402
12403 /* This is only meaningful if the target is
12404 evaluating conditions and if the user has
12405 opted for condition evaluation on the target's
12406 side. */
12407 if (gdb_evaluates_breakpoint_condition_p ()
12408 || !target_supports_evaluation_of_breakpoint_conditions ())
12409 return;
12410
12411 /* Flag all breakpoint locations with this address and
12412 the same program space as the location
12413 as "its condition has changed". We need to
12414 update the conditions on the target's side. */
12415 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12416 {
12417 loc = *loc2p;
12418
12419 if (!is_breakpoint (loc->owner)
12420 || pspace_num != loc->pspace->num)
12421 continue;
12422
12423 /* Flag the location appropriately. We use a different state to
12424 let everyone know that we already updated the set of locations
12425 with addr bl->address and program space bl->pspace. This is so
12426 we don't have to keep calling these functions just to mark locations
12427 that have already been marked. */
12428 loc->condition_changed = condition_updated;
12429
12430 /* Free the agent expression bytecode as well. We will compute
12431 it later on. */
12432 if (loc->cond_bytecode)
12433 {
12434 free_agent_expr (loc->cond_bytecode);
12435 loc->cond_bytecode = NULL;
12436 }
12437 }
12438}
12439/* Called whether new breakpoints are created, or existing breakpoints
12440 deleted, to update the global location list and recompute which
12441 locations are duplicate of which.
12442
12443 The INSERT_MODE flag determines whether locations may not, may, or
12444 shall be inserted now. See 'enum ugll_insert_mode' for more
12445 info. */
12446
12447static void
12448update_global_location_list (enum ugll_insert_mode insert_mode)
12449{
12450 struct breakpoint *b;
12451 struct bp_location **locp, *loc;
12452 struct cleanup *cleanups;
12453 /* Last breakpoint location address that was marked for update. */
12454 CORE_ADDR last_addr = 0;
12455 /* Last breakpoint location program space that was marked for update. */
12456 int last_pspace_num = -1;
12457
12458 /* Used in the duplicates detection below. When iterating over all
12459 bp_locations, points to the first bp_location of a given address.
12460 Breakpoints and watchpoints of different types are never
12461 duplicates of each other. Keep one pointer for each type of
12462 breakpoint/watchpoint, so we only need to loop over all locations
12463 once. */
12464 struct bp_location *bp_loc_first; /* breakpoint */
12465 struct bp_location *wp_loc_first; /* hardware watchpoint */
12466 struct bp_location *awp_loc_first; /* access watchpoint */
12467 struct bp_location *rwp_loc_first; /* read watchpoint */
12468
12469 /* Saved former bp_location array which we compare against the newly
12470 built bp_location from the current state of ALL_BREAKPOINTS. */
12471 struct bp_location **old_location, **old_locp;
12472 unsigned old_location_count;
12473
12474 old_location = bp_location;
12475 old_location_count = bp_location_count;
12476 bp_location = NULL;
12477 bp_location_count = 0;
12478 cleanups = make_cleanup (xfree, old_location);
12479
12480 ALL_BREAKPOINTS (b)
12481 for (loc = b->loc; loc; loc = loc->next)
12482 bp_location_count++;
12483
12484 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12485 locp = bp_location;
12486 ALL_BREAKPOINTS (b)
12487 for (loc = b->loc; loc; loc = loc->next)
12488 *locp++ = loc;
12489 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12490 bp_location_compare);
12491
12492 bp_location_target_extensions_update ();
12493
12494 /* Identify bp_location instances that are no longer present in the
12495 new list, and therefore should be freed. Note that it's not
12496 necessary that those locations should be removed from inferior --
12497 if there's another location at the same address (previously
12498 marked as duplicate), we don't need to remove/insert the
12499 location.
12500
12501 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12502 and former bp_location array state respectively. */
12503
12504 locp = bp_location;
12505 for (old_locp = old_location; old_locp < old_location + old_location_count;
12506 old_locp++)
12507 {
12508 struct bp_location *old_loc = *old_locp;
12509 struct bp_location **loc2p;
12510
12511 /* Tells if 'old_loc' is found among the new locations. If
12512 not, we have to free it. */
12513 int found_object = 0;
12514 /* Tells if the location should remain inserted in the target. */
12515 int keep_in_target = 0;
12516 int removed = 0;
12517
12518 /* Skip LOCP entries which will definitely never be needed.
12519 Stop either at or being the one matching OLD_LOC. */
12520 while (locp < bp_location + bp_location_count
12521 && (*locp)->address < old_loc->address)
12522 locp++;
12523
12524 for (loc2p = locp;
12525 (loc2p < bp_location + bp_location_count
12526 && (*loc2p)->address == old_loc->address);
12527 loc2p++)
12528 {
12529 /* Check if this is a new/duplicated location or a duplicated
12530 location that had its condition modified. If so, we want to send
12531 its condition to the target if evaluation of conditions is taking
12532 place there. */
12533 if ((*loc2p)->condition_changed == condition_modified
12534 && (last_addr != old_loc->address
12535 || last_pspace_num != old_loc->pspace->num))
12536 {
12537 force_breakpoint_reinsertion (*loc2p);
12538 last_pspace_num = old_loc->pspace->num;
12539 }
12540
12541 if (*loc2p == old_loc)
12542 found_object = 1;
12543 }
12544
12545 /* We have already handled this address, update it so that we don't
12546 have to go through updates again. */
12547 last_addr = old_loc->address;
12548
12549 /* Target-side condition evaluation: Handle deleted locations. */
12550 if (!found_object)
12551 force_breakpoint_reinsertion (old_loc);
12552
12553 /* If this location is no longer present, and inserted, look if
12554 there's maybe a new location at the same address. If so,
12555 mark that one inserted, and don't remove this one. This is
12556 needed so that we don't have a time window where a breakpoint
12557 at certain location is not inserted. */
12558
12559 if (old_loc->inserted)
12560 {
12561 /* If the location is inserted now, we might have to remove
12562 it. */
12563
12564 if (found_object && should_be_inserted (old_loc))
12565 {
12566 /* The location is still present in the location list,
12567 and still should be inserted. Don't do anything. */
12568 keep_in_target = 1;
12569 }
12570 else
12571 {
12572 /* This location still exists, but it won't be kept in the
12573 target since it may have been disabled. We proceed to
12574 remove its target-side condition. */
12575
12576 /* The location is either no longer present, or got
12577 disabled. See if there's another location at the
12578 same address, in which case we don't need to remove
12579 this one from the target. */
12580
12581 /* OLD_LOC comes from existing struct breakpoint. */
12582 if (breakpoint_address_is_meaningful (old_loc->owner))
12583 {
12584 for (loc2p = locp;
12585 (loc2p < bp_location + bp_location_count
12586 && (*loc2p)->address == old_loc->address);
12587 loc2p++)
12588 {
12589 struct bp_location *loc2 = *loc2p;
12590
12591 if (breakpoint_locations_match (loc2, old_loc))
12592 {
12593 /* Read watchpoint locations are switched to
12594 access watchpoints, if the former are not
12595 supported, but the latter are. */
12596 if (is_hardware_watchpoint (old_loc->owner))
12597 {
12598 gdb_assert (is_hardware_watchpoint (loc2->owner));
12599 loc2->watchpoint_type = old_loc->watchpoint_type;
12600 }
12601
12602 /* loc2 is a duplicated location. We need to check
12603 if it should be inserted in case it will be
12604 unduplicated. */
12605 if (loc2 != old_loc
12606 && unduplicated_should_be_inserted (loc2))
12607 {
12608 swap_insertion (old_loc, loc2);
12609 keep_in_target = 1;
12610 break;
12611 }
12612 }
12613 }
12614 }
12615 }
12616
12617 if (!keep_in_target)
12618 {
12619 if (remove_breakpoint (old_loc))
12620 {
12621 /* This is just about all we can do. We could keep
12622 this location on the global list, and try to
12623 remove it next time, but there's no particular
12624 reason why we will succeed next time.
12625
12626 Note that at this point, old_loc->owner is still
12627 valid, as delete_breakpoint frees the breakpoint
12628 only after calling us. */
12629 printf_filtered (_("warning: Error removing "
12630 "breakpoint %d\n"),
12631 old_loc->owner->number);
12632 }
12633 removed = 1;
12634 }
12635 }
12636
12637 if (!found_object)
12638 {
12639 if (removed && target_is_non_stop_p ()
12640 && need_moribund_for_location_type (old_loc))
12641 {
12642 /* This location was removed from the target. In
12643 non-stop mode, a race condition is possible where
12644 we've removed a breakpoint, but stop events for that
12645 breakpoint are already queued and will arrive later.
12646 We apply an heuristic to be able to distinguish such
12647 SIGTRAPs from other random SIGTRAPs: we keep this
12648 breakpoint location for a bit, and will retire it
12649 after we see some number of events. The theory here
12650 is that reporting of events should, "on the average",
12651 be fair, so after a while we'll see events from all
12652 threads that have anything of interest, and no longer
12653 need to keep this breakpoint location around. We
12654 don't hold locations forever so to reduce chances of
12655 mistaking a non-breakpoint SIGTRAP for a breakpoint
12656 SIGTRAP.
12657
12658 The heuristic failing can be disastrous on
12659 decr_pc_after_break targets.
12660
12661 On decr_pc_after_break targets, like e.g., x86-linux,
12662 if we fail to recognize a late breakpoint SIGTRAP,
12663 because events_till_retirement has reached 0 too
12664 soon, we'll fail to do the PC adjustment, and report
12665 a random SIGTRAP to the user. When the user resumes
12666 the inferior, it will most likely immediately crash
12667 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12668 corrupted, because of being resumed e.g., in the
12669 middle of a multi-byte instruction, or skipped a
12670 one-byte instruction. This was actually seen happen
12671 on native x86-linux, and should be less rare on
12672 targets that do not support new thread events, like
12673 remote, due to the heuristic depending on
12674 thread_count.
12675
12676 Mistaking a random SIGTRAP for a breakpoint trap
12677 causes similar symptoms (PC adjustment applied when
12678 it shouldn't), but then again, playing with SIGTRAPs
12679 behind the debugger's back is asking for trouble.
12680
12681 Since hardware watchpoint traps are always
12682 distinguishable from other traps, so we don't need to
12683 apply keep hardware watchpoint moribund locations
12684 around. We simply always ignore hardware watchpoint
12685 traps we can no longer explain. */
12686
12687 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12688 old_loc->owner = NULL;
12689
12690 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12691 }
12692 else
12693 {
12694 old_loc->owner = NULL;
12695 decref_bp_location (&old_loc);
12696 }
12697 }
12698 }
12699
12700 /* Rescan breakpoints at the same address and section, marking the
12701 first one as "first" and any others as "duplicates". This is so
12702 that the bpt instruction is only inserted once. If we have a
12703 permanent breakpoint at the same place as BPT, make that one the
12704 official one, and the rest as duplicates. Permanent breakpoints
12705 are sorted first for the same address.
12706
12707 Do the same for hardware watchpoints, but also considering the
12708 watchpoint's type (regular/access/read) and length. */
12709
12710 bp_loc_first = NULL;
12711 wp_loc_first = NULL;
12712 awp_loc_first = NULL;
12713 rwp_loc_first = NULL;
12714 ALL_BP_LOCATIONS (loc, locp)
12715 {
12716 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12717 non-NULL. */
12718 struct bp_location **loc_first_p;
12719 b = loc->owner;
12720
12721 if (!unduplicated_should_be_inserted (loc)
12722 || !breakpoint_address_is_meaningful (b)
12723 /* Don't detect duplicate for tracepoint locations because they are
12724 never duplicated. See the comments in field `duplicate' of
12725 `struct bp_location'. */
12726 || is_tracepoint (b))
12727 {
12728 /* Clear the condition modification flag. */
12729 loc->condition_changed = condition_unchanged;
12730 continue;
12731 }
12732
12733 if (b->type == bp_hardware_watchpoint)
12734 loc_first_p = &wp_loc_first;
12735 else if (b->type == bp_read_watchpoint)
12736 loc_first_p = &rwp_loc_first;
12737 else if (b->type == bp_access_watchpoint)
12738 loc_first_p = &awp_loc_first;
12739 else
12740 loc_first_p = &bp_loc_first;
12741
12742 if (*loc_first_p == NULL
12743 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12744 || !breakpoint_locations_match (loc, *loc_first_p))
12745 {
12746 *loc_first_p = loc;
12747 loc->duplicate = 0;
12748
12749 if (is_breakpoint (loc->owner) && loc->condition_changed)
12750 {
12751 loc->needs_update = 1;
12752 /* Clear the condition modification flag. */
12753 loc->condition_changed = condition_unchanged;
12754 }
12755 continue;
12756 }
12757
12758
12759 /* This and the above ensure the invariant that the first location
12760 is not duplicated, and is the inserted one.
12761 All following are marked as duplicated, and are not inserted. */
12762 if (loc->inserted)
12763 swap_insertion (loc, *loc_first_p);
12764 loc->duplicate = 1;
12765
12766 /* Clear the condition modification flag. */
12767 loc->condition_changed = condition_unchanged;
12768 }
12769
12770 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12771 {
12772 if (insert_mode != UGLL_DONT_INSERT)
12773 insert_breakpoint_locations ();
12774 else
12775 {
12776 /* Even though the caller told us to not insert new
12777 locations, we may still need to update conditions on the
12778 target's side of breakpoints that were already inserted
12779 if the target is evaluating breakpoint conditions. We
12780 only update conditions for locations that are marked
12781 "needs_update". */
12782 update_inserted_breakpoint_locations ();
12783 }
12784 }
12785
12786 if (insert_mode != UGLL_DONT_INSERT)
12787 download_tracepoint_locations ();
12788
12789 do_cleanups (cleanups);
12790}
12791
12792void
12793breakpoint_retire_moribund (void)
12794{
12795 struct bp_location *loc;
12796 int ix;
12797
12798 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12799 if (--(loc->events_till_retirement) == 0)
12800 {
12801 decref_bp_location (&loc);
12802 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12803 --ix;
12804 }
12805}
12806
12807static void
12808update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12809{
12810
12811 TRY
12812 {
12813 update_global_location_list (insert_mode);
12814 }
12815 CATCH (e, RETURN_MASK_ERROR)
12816 {
12817 }
12818 END_CATCH
12819}
12820
12821/* Clear BKP from a BPS. */
12822
12823static void
12824bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12825{
12826 bpstat bs;
12827
12828 for (bs = bps; bs; bs = bs->next)
12829 if (bs->breakpoint_at == bpt)
12830 {
12831 bs->breakpoint_at = NULL;
12832 bs->old_val = NULL;
12833 /* bs->commands will be freed later. */
12834 }
12835}
12836
12837/* Callback for iterate_over_threads. */
12838static int
12839bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12840{
12841 struct breakpoint *bpt = (struct breakpoint *) data;
12842
12843 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12844 return 0;
12845}
12846
12847/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12848 callbacks. */
12849
12850static void
12851say_where (struct breakpoint *b)
12852{
12853 struct value_print_options opts;
12854
12855 get_user_print_options (&opts);
12856
12857 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12858 single string. */
12859 if (b->loc == NULL)
12860 {
12861 /* For pending locations, the output differs slightly based
12862 on b->extra_string. If this is non-NULL, it contains either
12863 a condition or dprintf arguments. */
12864 if (b->extra_string == NULL)
12865 {
12866 printf_filtered (_(" (%s) pending."),
12867 event_location_to_string (b->location));
12868 }
12869 else if (b->type == bp_dprintf)
12870 {
12871 printf_filtered (_(" (%s,%s) pending."),
12872 event_location_to_string (b->location),
12873 b->extra_string);
12874 }
12875 else
12876 {
12877 printf_filtered (_(" (%s %s) pending."),
12878 event_location_to_string (b->location),
12879 b->extra_string);
12880 }
12881 }
12882 else
12883 {
12884 if (opts.addressprint || b->loc->symtab == NULL)
12885 {
12886 printf_filtered (" at ");
12887 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12888 gdb_stdout);
12889 }
12890 if (b->loc->symtab != NULL)
12891 {
12892 /* If there is a single location, we can print the location
12893 more nicely. */
12894 if (b->loc->next == NULL)
12895 printf_filtered (": file %s, line %d.",
12896 symtab_to_filename_for_display (b->loc->symtab),
12897 b->loc->line_number);
12898 else
12899 /* This is not ideal, but each location may have a
12900 different file name, and this at least reflects the
12901 real situation somewhat. */
12902 printf_filtered (": %s.",
12903 event_location_to_string (b->location));
12904 }
12905
12906 if (b->loc->next)
12907 {
12908 struct bp_location *loc = b->loc;
12909 int n = 0;
12910 for (; loc; loc = loc->next)
12911 ++n;
12912 printf_filtered (" (%d locations)", n);
12913 }
12914 }
12915}
12916
12917/* Default bp_location_ops methods. */
12918
12919static void
12920bp_location_dtor (struct bp_location *self)
12921{
12922 xfree (self->cond);
12923 if (self->cond_bytecode)
12924 free_agent_expr (self->cond_bytecode);
12925 xfree (self->function_name);
12926
12927 VEC_free (agent_expr_p, self->target_info.conditions);
12928 VEC_free (agent_expr_p, self->target_info.tcommands);
12929}
12930
12931static const struct bp_location_ops bp_location_ops =
12932{
12933 bp_location_dtor
12934};
12935
12936/* Default breakpoint_ops methods all breakpoint_ops ultimately
12937 inherit from. */
12938
12939static void
12940base_breakpoint_dtor (struct breakpoint *self)
12941{
12942 decref_counted_command_line (&self->commands);
12943 xfree (self->cond_string);
12944 xfree (self->extra_string);
12945 xfree (self->filter);
12946 delete_event_location (self->location);
12947 delete_event_location (self->location_range_end);
12948}
12949
12950static struct bp_location *
12951base_breakpoint_allocate_location (struct breakpoint *self)
12952{
12953 struct bp_location *loc;
12954
12955 loc = XNEW (struct bp_location);
12956 init_bp_location (loc, &bp_location_ops, self);
12957 return loc;
12958}
12959
12960static void
12961base_breakpoint_re_set (struct breakpoint *b)
12962{
12963 /* Nothing to re-set. */
12964}
12965
12966#define internal_error_pure_virtual_called() \
12967 gdb_assert_not_reached ("pure virtual function called")
12968
12969static int
12970base_breakpoint_insert_location (struct bp_location *bl)
12971{
12972 internal_error_pure_virtual_called ();
12973}
12974
12975static int
12976base_breakpoint_remove_location (struct bp_location *bl,
12977 enum remove_bp_reason reason)
12978{
12979 internal_error_pure_virtual_called ();
12980}
12981
12982static int
12983base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12984 struct address_space *aspace,
12985 CORE_ADDR bp_addr,
12986 const struct target_waitstatus *ws)
12987{
12988 internal_error_pure_virtual_called ();
12989}
12990
12991static void
12992base_breakpoint_check_status (bpstat bs)
12993{
12994 /* Always stop. */
12995}
12996
12997/* A "works_in_software_mode" breakpoint_ops method that just internal
12998 errors. */
12999
13000static int
13001base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13002{
13003 internal_error_pure_virtual_called ();
13004}
13005
13006/* A "resources_needed" breakpoint_ops method that just internal
13007 errors. */
13008
13009static int
13010base_breakpoint_resources_needed (const struct bp_location *bl)
13011{
13012 internal_error_pure_virtual_called ();
13013}
13014
13015static enum print_stop_action
13016base_breakpoint_print_it (bpstat bs)
13017{
13018 internal_error_pure_virtual_called ();
13019}
13020
13021static void
13022base_breakpoint_print_one_detail (const struct breakpoint *self,
13023 struct ui_out *uiout)
13024{
13025 /* nothing */
13026}
13027
13028static void
13029base_breakpoint_print_mention (struct breakpoint *b)
13030{
13031 internal_error_pure_virtual_called ();
13032}
13033
13034static void
13035base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13036{
13037 internal_error_pure_virtual_called ();
13038}
13039
13040static void
13041base_breakpoint_create_sals_from_location
13042 (const struct event_location *location,
13043 struct linespec_result *canonical,
13044 enum bptype type_wanted)
13045{
13046 internal_error_pure_virtual_called ();
13047}
13048
13049static void
13050base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13051 struct linespec_result *c,
13052 char *cond_string,
13053 char *extra_string,
13054 enum bptype type_wanted,
13055 enum bpdisp disposition,
13056 int thread,
13057 int task, int ignore_count,
13058 const struct breakpoint_ops *o,
13059 int from_tty, int enabled,
13060 int internal, unsigned flags)
13061{
13062 internal_error_pure_virtual_called ();
13063}
13064
13065static void
13066base_breakpoint_decode_location (struct breakpoint *b,
13067 const struct event_location *location,
13068 struct program_space *search_pspace,
13069 struct symtabs_and_lines *sals)
13070{
13071 internal_error_pure_virtual_called ();
13072}
13073
13074/* The default 'explains_signal' method. */
13075
13076static int
13077base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13078{
13079 return 1;
13080}
13081
13082/* The default "after_condition_true" method. */
13083
13084static void
13085base_breakpoint_after_condition_true (struct bpstats *bs)
13086{
13087 /* Nothing to do. */
13088}
13089
13090struct breakpoint_ops base_breakpoint_ops =
13091{
13092 base_breakpoint_dtor,
13093 base_breakpoint_allocate_location,
13094 base_breakpoint_re_set,
13095 base_breakpoint_insert_location,
13096 base_breakpoint_remove_location,
13097 base_breakpoint_breakpoint_hit,
13098 base_breakpoint_check_status,
13099 base_breakpoint_resources_needed,
13100 base_breakpoint_works_in_software_mode,
13101 base_breakpoint_print_it,
13102 NULL,
13103 base_breakpoint_print_one_detail,
13104 base_breakpoint_print_mention,
13105 base_breakpoint_print_recreate,
13106 base_breakpoint_create_sals_from_location,
13107 base_breakpoint_create_breakpoints_sal,
13108 base_breakpoint_decode_location,
13109 base_breakpoint_explains_signal,
13110 base_breakpoint_after_condition_true,
13111};
13112
13113/* Default breakpoint_ops methods. */
13114
13115static void
13116bkpt_re_set (struct breakpoint *b)
13117{
13118 /* FIXME: is this still reachable? */
13119 if (breakpoint_event_location_empty_p (b))
13120 {
13121 /* Anything without a location can't be re-set. */
13122 delete_breakpoint (b);
13123 return;
13124 }
13125
13126 breakpoint_re_set_default (b);
13127}
13128
13129static int
13130bkpt_insert_location (struct bp_location *bl)
13131{
13132 CORE_ADDR addr = bl->target_info.reqstd_address;
13133
13134 bl->target_info.kind = breakpoint_kind (bl, &addr);
13135 bl->target_info.placed_address = addr;
13136
13137 if (bl->loc_type == bp_loc_hardware_breakpoint)
13138 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13139 else
13140 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13141}
13142
13143static int
13144bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13145{
13146 if (bl->loc_type == bp_loc_hardware_breakpoint)
13147 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13148 else
13149 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13150}
13151
13152static int
13153bkpt_breakpoint_hit (const struct bp_location *bl,
13154 struct address_space *aspace, CORE_ADDR bp_addr,
13155 const struct target_waitstatus *ws)
13156{
13157 if (ws->kind != TARGET_WAITKIND_STOPPED
13158 || ws->value.sig != GDB_SIGNAL_TRAP)
13159 return 0;
13160
13161 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13162 aspace, bp_addr))
13163 return 0;
13164
13165 if (overlay_debugging /* unmapped overlay section */
13166 && section_is_overlay (bl->section)
13167 && !section_is_mapped (bl->section))
13168 return 0;
13169
13170 return 1;
13171}
13172
13173static int
13174dprintf_breakpoint_hit (const struct bp_location *bl,
13175 struct address_space *aspace, CORE_ADDR bp_addr,
13176 const struct target_waitstatus *ws)
13177{
13178 if (dprintf_style == dprintf_style_agent
13179 && target_can_run_breakpoint_commands ())
13180 {
13181 /* An agent-style dprintf never causes a stop. If we see a trap
13182 for this address it must be for a breakpoint that happens to
13183 be set at the same address. */
13184 return 0;
13185 }
13186
13187 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13188}
13189
13190static int
13191bkpt_resources_needed (const struct bp_location *bl)
13192{
13193 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13194
13195 return 1;
13196}
13197
13198static enum print_stop_action
13199bkpt_print_it (bpstat bs)
13200{
13201 struct breakpoint *b;
13202 const struct bp_location *bl;
13203 int bp_temp;
13204 struct ui_out *uiout = current_uiout;
13205
13206 gdb_assert (bs->bp_location_at != NULL);
13207
13208 bl = bs->bp_location_at;
13209 b = bs->breakpoint_at;
13210
13211 bp_temp = b->disposition == disp_del;
13212 if (bl->address != bl->requested_address)
13213 breakpoint_adjustment_warning (bl->requested_address,
13214 bl->address,
13215 b->number, 1);
13216 annotate_breakpoint (b->number);
13217 maybe_print_thread_hit_breakpoint (uiout);
13218
13219 if (bp_temp)
13220 ui_out_text (uiout, "Temporary breakpoint ");
13221 else
13222 ui_out_text (uiout, "Breakpoint ");
13223 if (ui_out_is_mi_like_p (uiout))
13224 {
13225 ui_out_field_string (uiout, "reason",
13226 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13227 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13228 }
13229 ui_out_field_int (uiout, "bkptno", b->number);
13230 ui_out_text (uiout, ", ");
13231
13232 return PRINT_SRC_AND_LOC;
13233}
13234
13235static void
13236bkpt_print_mention (struct breakpoint *b)
13237{
13238 if (ui_out_is_mi_like_p (current_uiout))
13239 return;
13240
13241 switch (b->type)
13242 {
13243 case bp_breakpoint:
13244 case bp_gnu_ifunc_resolver:
13245 if (b->disposition == disp_del)
13246 printf_filtered (_("Temporary breakpoint"));
13247 else
13248 printf_filtered (_("Breakpoint"));
13249 printf_filtered (_(" %d"), b->number);
13250 if (b->type == bp_gnu_ifunc_resolver)
13251 printf_filtered (_(" at gnu-indirect-function resolver"));
13252 break;
13253 case bp_hardware_breakpoint:
13254 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13255 break;
13256 case bp_dprintf:
13257 printf_filtered (_("Dprintf %d"), b->number);
13258 break;
13259 }
13260
13261 say_where (b);
13262}
13263
13264static void
13265bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13266{
13267 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13268 fprintf_unfiltered (fp, "tbreak");
13269 else if (tp->type == bp_breakpoint)
13270 fprintf_unfiltered (fp, "break");
13271 else if (tp->type == bp_hardware_breakpoint
13272 && tp->disposition == disp_del)
13273 fprintf_unfiltered (fp, "thbreak");
13274 else if (tp->type == bp_hardware_breakpoint)
13275 fprintf_unfiltered (fp, "hbreak");
13276 else
13277 internal_error (__FILE__, __LINE__,
13278 _("unhandled breakpoint type %d"), (int) tp->type);
13279
13280 fprintf_unfiltered (fp, " %s",
13281 event_location_to_string (tp->location));
13282
13283 /* Print out extra_string if this breakpoint is pending. It might
13284 contain, for example, conditions that were set by the user. */
13285 if (tp->loc == NULL && tp->extra_string != NULL)
13286 fprintf_unfiltered (fp, " %s", tp->extra_string);
13287
13288 print_recreate_thread (tp, fp);
13289}
13290
13291static void
13292bkpt_create_sals_from_location (const struct event_location *location,
13293 struct linespec_result *canonical,
13294 enum bptype type_wanted)
13295{
13296 create_sals_from_location_default (location, canonical, type_wanted);
13297}
13298
13299static void
13300bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13301 struct linespec_result *canonical,
13302 char *cond_string,
13303 char *extra_string,
13304 enum bptype type_wanted,
13305 enum bpdisp disposition,
13306 int thread,
13307 int task, int ignore_count,
13308 const struct breakpoint_ops *ops,
13309 int from_tty, int enabled,
13310 int internal, unsigned flags)
13311{
13312 create_breakpoints_sal_default (gdbarch, canonical,
13313 cond_string, extra_string,
13314 type_wanted,
13315 disposition, thread, task,
13316 ignore_count, ops, from_tty,
13317 enabled, internal, flags);
13318}
13319
13320static void
13321bkpt_decode_location (struct breakpoint *b,
13322 const struct event_location *location,
13323 struct program_space *search_pspace,
13324 struct symtabs_and_lines *sals)
13325{
13326 decode_location_default (b, location, search_pspace, sals);
13327}
13328
13329/* Virtual table for internal breakpoints. */
13330
13331static void
13332internal_bkpt_re_set (struct breakpoint *b)
13333{
13334 switch (b->type)
13335 {
13336 /* Delete overlay event and longjmp master breakpoints; they
13337 will be reset later by breakpoint_re_set. */
13338 case bp_overlay_event:
13339 case bp_longjmp_master:
13340 case bp_std_terminate_master:
13341 case bp_exception_master:
13342 delete_breakpoint (b);
13343 break;
13344
13345 /* This breakpoint is special, it's set up when the inferior
13346 starts and we really don't want to touch it. */
13347 case bp_shlib_event:
13348
13349 /* Like bp_shlib_event, this breakpoint type is special. Once
13350 it is set up, we do not want to touch it. */
13351 case bp_thread_event:
13352 break;
13353 }
13354}
13355
13356static void
13357internal_bkpt_check_status (bpstat bs)
13358{
13359 if (bs->breakpoint_at->type == bp_shlib_event)
13360 {
13361 /* If requested, stop when the dynamic linker notifies GDB of
13362 events. This allows the user to get control and place
13363 breakpoints in initializer routines for dynamically loaded
13364 objects (among other things). */
13365 bs->stop = stop_on_solib_events;
13366 bs->print = stop_on_solib_events;
13367 }
13368 else
13369 bs->stop = 0;
13370}
13371
13372static enum print_stop_action
13373internal_bkpt_print_it (bpstat bs)
13374{
13375 struct breakpoint *b;
13376
13377 b = bs->breakpoint_at;
13378
13379 switch (b->type)
13380 {
13381 case bp_shlib_event:
13382 /* Did we stop because the user set the stop_on_solib_events
13383 variable? (If so, we report this as a generic, "Stopped due
13384 to shlib event" message.) */
13385 print_solib_event (0);
13386 break;
13387
13388 case bp_thread_event:
13389 /* Not sure how we will get here.
13390 GDB should not stop for these breakpoints. */
13391 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13392 break;
13393
13394 case bp_overlay_event:
13395 /* By analogy with the thread event, GDB should not stop for these. */
13396 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13397 break;
13398
13399 case bp_longjmp_master:
13400 /* These should never be enabled. */
13401 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13402 break;
13403
13404 case bp_std_terminate_master:
13405 /* These should never be enabled. */
13406 printf_filtered (_("std::terminate Master Breakpoint: "
13407 "gdb should not stop!\n"));
13408 break;
13409
13410 case bp_exception_master:
13411 /* These should never be enabled. */
13412 printf_filtered (_("Exception Master Breakpoint: "
13413 "gdb should not stop!\n"));
13414 break;
13415 }
13416
13417 return PRINT_NOTHING;
13418}
13419
13420static void
13421internal_bkpt_print_mention (struct breakpoint *b)
13422{
13423 /* Nothing to mention. These breakpoints are internal. */
13424}
13425
13426/* Virtual table for momentary breakpoints */
13427
13428static void
13429momentary_bkpt_re_set (struct breakpoint *b)
13430{
13431 /* Keep temporary breakpoints, which can be encountered when we step
13432 over a dlopen call and solib_add is resetting the breakpoints.
13433 Otherwise these should have been blown away via the cleanup chain
13434 or by breakpoint_init_inferior when we rerun the executable. */
13435}
13436
13437static void
13438momentary_bkpt_check_status (bpstat bs)
13439{
13440 /* Nothing. The point of these breakpoints is causing a stop. */
13441}
13442
13443static enum print_stop_action
13444momentary_bkpt_print_it (bpstat bs)
13445{
13446 return PRINT_UNKNOWN;
13447}
13448
13449static void
13450momentary_bkpt_print_mention (struct breakpoint *b)
13451{
13452 /* Nothing to mention. These breakpoints are internal. */
13453}
13454
13455/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13456
13457 It gets cleared already on the removal of the first one of such placed
13458 breakpoints. This is OK as they get all removed altogether. */
13459
13460static void
13461longjmp_bkpt_dtor (struct breakpoint *self)
13462{
13463 struct thread_info *tp = find_thread_global_id (self->thread);
13464
13465 if (tp)
13466 tp->initiating_frame = null_frame_id;
13467
13468 momentary_breakpoint_ops.dtor (self);
13469}
13470
13471/* Specific methods for probe breakpoints. */
13472
13473static int
13474bkpt_probe_insert_location (struct bp_location *bl)
13475{
13476 int v = bkpt_insert_location (bl);
13477
13478 if (v == 0)
13479 {
13480 /* The insertion was successful, now let's set the probe's semaphore
13481 if needed. */
13482 if (bl->probe.probe->pops->set_semaphore != NULL)
13483 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13484 bl->probe.objfile,
13485 bl->gdbarch);
13486 }
13487
13488 return v;
13489}
13490
13491static int
13492bkpt_probe_remove_location (struct bp_location *bl,
13493 enum remove_bp_reason reason)
13494{
13495 /* Let's clear the semaphore before removing the location. */
13496 if (bl->probe.probe->pops->clear_semaphore != NULL)
13497 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13498 bl->probe.objfile,
13499 bl->gdbarch);
13500
13501 return bkpt_remove_location (bl, reason);
13502}
13503
13504static void
13505bkpt_probe_create_sals_from_location (const struct event_location *location,
13506 struct linespec_result *canonical,
13507 enum bptype type_wanted)
13508{
13509 struct linespec_sals lsal;
13510
13511 lsal.sals = parse_probes (location, NULL, canonical);
13512 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13513 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13514}
13515
13516static void
13517bkpt_probe_decode_location (struct breakpoint *b,
13518 const struct event_location *location,
13519 struct program_space *search_pspace,
13520 struct symtabs_and_lines *sals)
13521{
13522 *sals = parse_probes (location, search_pspace, NULL);
13523 if (!sals->sals)
13524 error (_("probe not found"));
13525}
13526
13527/* The breakpoint_ops structure to be used in tracepoints. */
13528
13529static void
13530tracepoint_re_set (struct breakpoint *b)
13531{
13532 breakpoint_re_set_default (b);
13533}
13534
13535static int
13536tracepoint_breakpoint_hit (const struct bp_location *bl,
13537 struct address_space *aspace, CORE_ADDR bp_addr,
13538 const struct target_waitstatus *ws)
13539{
13540 /* By definition, the inferior does not report stops at
13541 tracepoints. */
13542 return 0;
13543}
13544
13545static void
13546tracepoint_print_one_detail (const struct breakpoint *self,
13547 struct ui_out *uiout)
13548{
13549 struct tracepoint *tp = (struct tracepoint *) self;
13550 if (tp->static_trace_marker_id)
13551 {
13552 gdb_assert (self->type == bp_static_tracepoint);
13553
13554 ui_out_text (uiout, "\tmarker id is ");
13555 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13556 tp->static_trace_marker_id);
13557 ui_out_text (uiout, "\n");
13558 }
13559}
13560
13561static void
13562tracepoint_print_mention (struct breakpoint *b)
13563{
13564 if (ui_out_is_mi_like_p (current_uiout))
13565 return;
13566
13567 switch (b->type)
13568 {
13569 case bp_tracepoint:
13570 printf_filtered (_("Tracepoint"));
13571 printf_filtered (_(" %d"), b->number);
13572 break;
13573 case bp_fast_tracepoint:
13574 printf_filtered (_("Fast tracepoint"));
13575 printf_filtered (_(" %d"), b->number);
13576 break;
13577 case bp_static_tracepoint:
13578 printf_filtered (_("Static tracepoint"));
13579 printf_filtered (_(" %d"), b->number);
13580 break;
13581 default:
13582 internal_error (__FILE__, __LINE__,
13583 _("unhandled tracepoint type %d"), (int) b->type);
13584 }
13585
13586 say_where (b);
13587}
13588
13589static void
13590tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13591{
13592 struct tracepoint *tp = (struct tracepoint *) self;
13593
13594 if (self->type == bp_fast_tracepoint)
13595 fprintf_unfiltered (fp, "ftrace");
13596 else if (self->type == bp_static_tracepoint)
13597 fprintf_unfiltered (fp, "strace");
13598 else if (self->type == bp_tracepoint)
13599 fprintf_unfiltered (fp, "trace");
13600 else
13601 internal_error (__FILE__, __LINE__,
13602 _("unhandled tracepoint type %d"), (int) self->type);
13603
13604 fprintf_unfiltered (fp, " %s",
13605 event_location_to_string (self->location));
13606 print_recreate_thread (self, fp);
13607
13608 if (tp->pass_count)
13609 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13610}
13611
13612static void
13613tracepoint_create_sals_from_location (const struct event_location *location,
13614 struct linespec_result *canonical,
13615 enum bptype type_wanted)
13616{
13617 create_sals_from_location_default (location, canonical, type_wanted);
13618}
13619
13620static void
13621tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13622 struct linespec_result *canonical,
13623 char *cond_string,
13624 char *extra_string,
13625 enum bptype type_wanted,
13626 enum bpdisp disposition,
13627 int thread,
13628 int task, int ignore_count,
13629 const struct breakpoint_ops *ops,
13630 int from_tty, int enabled,
13631 int internal, unsigned flags)
13632{
13633 create_breakpoints_sal_default (gdbarch, canonical,
13634 cond_string, extra_string,
13635 type_wanted,
13636 disposition, thread, task,
13637 ignore_count, ops, from_tty,
13638 enabled, internal, flags);
13639}
13640
13641static void
13642tracepoint_decode_location (struct breakpoint *b,
13643 const struct event_location *location,
13644 struct program_space *search_pspace,
13645 struct symtabs_and_lines *sals)
13646{
13647 decode_location_default (b, location, search_pspace, sals);
13648}
13649
13650struct breakpoint_ops tracepoint_breakpoint_ops;
13651
13652/* The breakpoint_ops structure to be use on tracepoints placed in a
13653 static probe. */
13654
13655static void
13656tracepoint_probe_create_sals_from_location
13657 (const struct event_location *location,
13658 struct linespec_result *canonical,
13659 enum bptype type_wanted)
13660{
13661 /* We use the same method for breakpoint on probes. */
13662 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13663}
13664
13665static void
13666tracepoint_probe_decode_location (struct breakpoint *b,
13667 const struct event_location *location,
13668 struct program_space *search_pspace,
13669 struct symtabs_and_lines *sals)
13670{
13671 /* We use the same method for breakpoint on probes. */
13672 bkpt_probe_decode_location (b, location, search_pspace, sals);
13673}
13674
13675static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13676
13677/* Dprintf breakpoint_ops methods. */
13678
13679static void
13680dprintf_re_set (struct breakpoint *b)
13681{
13682 breakpoint_re_set_default (b);
13683
13684 /* extra_string should never be non-NULL for dprintf. */
13685 gdb_assert (b->extra_string != NULL);
13686
13687 /* 1 - connect to target 1, that can run breakpoint commands.
13688 2 - create a dprintf, which resolves fine.
13689 3 - disconnect from target 1
13690 4 - connect to target 2, that can NOT run breakpoint commands.
13691
13692 After steps #3/#4, you'll want the dprintf command list to
13693 be updated, because target 1 and 2 may well return different
13694 answers for target_can_run_breakpoint_commands().
13695 Given absence of finer grained resetting, we get to do
13696 it all the time. */
13697 if (b->extra_string != NULL)
13698 update_dprintf_command_list (b);
13699}
13700
13701/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13702
13703static void
13704dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13705{
13706 fprintf_unfiltered (fp, "dprintf %s,%s",
13707 event_location_to_string (tp->location),
13708 tp->extra_string);
13709 print_recreate_thread (tp, fp);
13710}
13711
13712/* Implement the "after_condition_true" breakpoint_ops method for
13713 dprintf.
13714
13715 dprintf's are implemented with regular commands in their command
13716 list, but we run the commands here instead of before presenting the
13717 stop to the user, as dprintf's don't actually cause a stop. This
13718 also makes it so that the commands of multiple dprintfs at the same
13719 address are all handled. */
13720
13721static void
13722dprintf_after_condition_true (struct bpstats *bs)
13723{
13724 struct cleanup *old_chain;
13725 struct bpstats tmp_bs = { NULL };
13726 struct bpstats *tmp_bs_p = &tmp_bs;
13727
13728 /* dprintf's never cause a stop. This wasn't set in the
13729 check_status hook instead because that would make the dprintf's
13730 condition not be evaluated. */
13731 bs->stop = 0;
13732
13733 /* Run the command list here. Take ownership of it instead of
13734 copying. We never want these commands to run later in
13735 bpstat_do_actions, if a breakpoint that causes a stop happens to
13736 be set at same address as this dprintf, or even if running the
13737 commands here throws. */
13738 tmp_bs.commands = bs->commands;
13739 bs->commands = NULL;
13740 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13741
13742 bpstat_do_actions_1 (&tmp_bs_p);
13743
13744 /* 'tmp_bs.commands' will usually be NULL by now, but
13745 bpstat_do_actions_1 may return early without processing the whole
13746 list. */
13747 do_cleanups (old_chain);
13748}
13749
13750/* The breakpoint_ops structure to be used on static tracepoints with
13751 markers (`-m'). */
13752
13753static void
13754strace_marker_create_sals_from_location (const struct event_location *location,
13755 struct linespec_result *canonical,
13756 enum bptype type_wanted)
13757{
13758 struct linespec_sals lsal;
13759 const char *arg_start, *arg;
13760 char *str;
13761 struct cleanup *cleanup;
13762
13763 arg = arg_start = get_linespec_location (location);
13764 lsal.sals = decode_static_tracepoint_spec (&arg);
13765
13766 str = savestring (arg_start, arg - arg_start);
13767 cleanup = make_cleanup (xfree, str);
13768 canonical->location = new_linespec_location (&str);
13769 do_cleanups (cleanup);
13770
13771 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13772 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13773}
13774
13775static void
13776strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13777 struct linespec_result *canonical,
13778 char *cond_string,
13779 char *extra_string,
13780 enum bptype type_wanted,
13781 enum bpdisp disposition,
13782 int thread,
13783 int task, int ignore_count,
13784 const struct breakpoint_ops *ops,
13785 int from_tty, int enabled,
13786 int internal, unsigned flags)
13787{
13788 int i;
13789 struct linespec_sals *lsal = VEC_index (linespec_sals,
13790 canonical->sals, 0);
13791
13792 /* If the user is creating a static tracepoint by marker id
13793 (strace -m MARKER_ID), then store the sals index, so that
13794 breakpoint_re_set can try to match up which of the newly
13795 found markers corresponds to this one, and, don't try to
13796 expand multiple locations for each sal, given than SALS
13797 already should contain all sals for MARKER_ID. */
13798
13799 for (i = 0; i < lsal->sals.nelts; ++i)
13800 {
13801 struct symtabs_and_lines expanded;
13802 struct tracepoint *tp;
13803 struct cleanup *old_chain;
13804 struct event_location *location;
13805
13806 expanded.nelts = 1;
13807 expanded.sals = &lsal->sals.sals[i];
13808
13809 location = copy_event_location (canonical->location);
13810 old_chain = make_cleanup_delete_event_location (location);
13811
13812 tp = XCNEW (struct tracepoint);
13813 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13814 location, NULL,
13815 cond_string, extra_string,
13816 type_wanted, disposition,
13817 thread, task, ignore_count, ops,
13818 from_tty, enabled, internal, flags,
13819 canonical->special_display);
13820 /* Given that its possible to have multiple markers with
13821 the same string id, if the user is creating a static
13822 tracepoint by marker id ("strace -m MARKER_ID"), then
13823 store the sals index, so that breakpoint_re_set can
13824 try to match up which of the newly found markers
13825 corresponds to this one */
13826 tp->static_trace_marker_id_idx = i;
13827
13828 install_breakpoint (internal, &tp->base, 0);
13829
13830 discard_cleanups (old_chain);
13831 }
13832}
13833
13834static void
13835strace_marker_decode_location (struct breakpoint *b,
13836 const struct event_location *location,
13837 struct program_space *search_pspace,
13838 struct symtabs_and_lines *sals)
13839{
13840 struct tracepoint *tp = (struct tracepoint *) b;
13841 const char *s = get_linespec_location (location);
13842
13843 *sals = decode_static_tracepoint_spec (&s);
13844 if (sals->nelts > tp->static_trace_marker_id_idx)
13845 {
13846 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13847 sals->nelts = 1;
13848 }
13849 else
13850 error (_("marker %s not found"), tp->static_trace_marker_id);
13851}
13852
13853static struct breakpoint_ops strace_marker_breakpoint_ops;
13854
13855static int
13856strace_marker_p (struct breakpoint *b)
13857{
13858 return b->ops == &strace_marker_breakpoint_ops;
13859}
13860
13861/* Delete a breakpoint and clean up all traces of it in the data
13862 structures. */
13863
13864void
13865delete_breakpoint (struct breakpoint *bpt)
13866{
13867 struct breakpoint *b;
13868
13869 gdb_assert (bpt != NULL);
13870
13871 /* Has this bp already been deleted? This can happen because
13872 multiple lists can hold pointers to bp's. bpstat lists are
13873 especial culprits.
13874
13875 One example of this happening is a watchpoint's scope bp. When
13876 the scope bp triggers, we notice that the watchpoint is out of
13877 scope, and delete it. We also delete its scope bp. But the
13878 scope bp is marked "auto-deleting", and is already on a bpstat.
13879 That bpstat is then checked for auto-deleting bp's, which are
13880 deleted.
13881
13882 A real solution to this problem might involve reference counts in
13883 bp's, and/or giving them pointers back to their referencing
13884 bpstat's, and teaching delete_breakpoint to only free a bp's
13885 storage when no more references were extent. A cheaper bandaid
13886 was chosen. */
13887 if (bpt->type == bp_none)
13888 return;
13889
13890 /* At least avoid this stale reference until the reference counting
13891 of breakpoints gets resolved. */
13892 if (bpt->related_breakpoint != bpt)
13893 {
13894 struct breakpoint *related;
13895 struct watchpoint *w;
13896
13897 if (bpt->type == bp_watchpoint_scope)
13898 w = (struct watchpoint *) bpt->related_breakpoint;
13899 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13900 w = (struct watchpoint *) bpt;
13901 else
13902 w = NULL;
13903 if (w != NULL)
13904 watchpoint_del_at_next_stop (w);
13905
13906 /* Unlink bpt from the bpt->related_breakpoint ring. */
13907 for (related = bpt; related->related_breakpoint != bpt;
13908 related = related->related_breakpoint);
13909 related->related_breakpoint = bpt->related_breakpoint;
13910 bpt->related_breakpoint = bpt;
13911 }
13912
13913 /* watch_command_1 creates a watchpoint but only sets its number if
13914 update_watchpoint succeeds in creating its bp_locations. If there's
13915 a problem in that process, we'll be asked to delete the half-created
13916 watchpoint. In that case, don't announce the deletion. */
13917 if (bpt->number)
13918 observer_notify_breakpoint_deleted (bpt);
13919
13920 if (breakpoint_chain == bpt)
13921 breakpoint_chain = bpt->next;
13922
13923 ALL_BREAKPOINTS (b)
13924 if (b->next == bpt)
13925 {
13926 b->next = bpt->next;
13927 break;
13928 }
13929
13930 /* Be sure no bpstat's are pointing at the breakpoint after it's
13931 been freed. */
13932 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13933 in all threads for now. Note that we cannot just remove bpstats
13934 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13935 commands are associated with the bpstat; if we remove it here,
13936 then the later call to bpstat_do_actions (&stop_bpstat); in
13937 event-top.c won't do anything, and temporary breakpoints with
13938 commands won't work. */
13939
13940 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13941
13942 /* Now that breakpoint is removed from breakpoint list, update the
13943 global location list. This will remove locations that used to
13944 belong to this breakpoint. Do this before freeing the breakpoint
13945 itself, since remove_breakpoint looks at location's owner. It
13946 might be better design to have location completely
13947 self-contained, but it's not the case now. */
13948 update_global_location_list (UGLL_DONT_INSERT);
13949
13950 bpt->ops->dtor (bpt);
13951 /* On the chance that someone will soon try again to delete this
13952 same bp, we mark it as deleted before freeing its storage. */
13953 bpt->type = bp_none;
13954 xfree (bpt);
13955}
13956
13957static void
13958do_delete_breakpoint_cleanup (void *b)
13959{
13960 delete_breakpoint ((struct breakpoint *) b);
13961}
13962
13963struct cleanup *
13964make_cleanup_delete_breakpoint (struct breakpoint *b)
13965{
13966 return make_cleanup (do_delete_breakpoint_cleanup, b);
13967}
13968
13969/* Iterator function to call a user-provided callback function once
13970 for each of B and its related breakpoints. */
13971
13972static void
13973iterate_over_related_breakpoints (struct breakpoint *b,
13974 void (*function) (struct breakpoint *,
13975 void *),
13976 void *data)
13977{
13978 struct breakpoint *related;
13979
13980 related = b;
13981 do
13982 {
13983 struct breakpoint *next;
13984
13985 /* FUNCTION may delete RELATED. */
13986 next = related->related_breakpoint;
13987
13988 if (next == related)
13989 {
13990 /* RELATED is the last ring entry. */
13991 function (related, data);
13992
13993 /* FUNCTION may have deleted it, so we'd never reach back to
13994 B. There's nothing left to do anyway, so just break
13995 out. */
13996 break;
13997 }
13998 else
13999 function (related, data);
14000
14001 related = next;
14002 }
14003 while (related != b);
14004}
14005
14006static void
14007do_delete_breakpoint (struct breakpoint *b, void *ignore)
14008{
14009 delete_breakpoint (b);
14010}
14011
14012/* A callback for map_breakpoint_numbers that calls
14013 delete_breakpoint. */
14014
14015static void
14016do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14017{
14018 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14019}
14020
14021void
14022delete_command (char *arg, int from_tty)
14023{
14024 struct breakpoint *b, *b_tmp;
14025
14026 dont_repeat ();
14027
14028 if (arg == 0)
14029 {
14030 int breaks_to_delete = 0;
14031
14032 /* Delete all breakpoints if no argument. Do not delete
14033 internal breakpoints, these have to be deleted with an
14034 explicit breakpoint number argument. */
14035 ALL_BREAKPOINTS (b)
14036 if (user_breakpoint_p (b))
14037 {
14038 breaks_to_delete = 1;
14039 break;
14040 }
14041
14042 /* Ask user only if there are some breakpoints to delete. */
14043 if (!from_tty
14044 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14045 {
14046 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14047 if (user_breakpoint_p (b))
14048 delete_breakpoint (b);
14049 }
14050 }
14051 else
14052 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14053}
14054
14055/* Return true if all locations of B bound to PSPACE are pending. If
14056 PSPACE is NULL, all locations of all program spaces are
14057 considered. */
14058
14059static int
14060all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
14061{
14062 struct bp_location *loc;
14063
14064 for (loc = b->loc; loc != NULL; loc = loc->next)
14065 if ((pspace == NULL
14066 || loc->pspace == pspace)
14067 && !loc->shlib_disabled
14068 && !loc->pspace->executing_startup)
14069 return 0;
14070 return 1;
14071}
14072
14073/* Subroutine of update_breakpoint_locations to simplify it.
14074 Return non-zero if multiple fns in list LOC have the same name.
14075 Null names are ignored. */
14076
14077static int
14078ambiguous_names_p (struct bp_location *loc)
14079{
14080 struct bp_location *l;
14081 htab_t htab = htab_create_alloc (13, htab_hash_string,
14082 (int (*) (const void *,
14083 const void *)) streq,
14084 NULL, xcalloc, xfree);
14085
14086 for (l = loc; l != NULL; l = l->next)
14087 {
14088 const char **slot;
14089 const char *name = l->function_name;
14090
14091 /* Allow for some names to be NULL, ignore them. */
14092 if (name == NULL)
14093 continue;
14094
14095 slot = (const char **) htab_find_slot (htab, (const void *) name,
14096 INSERT);
14097 /* NOTE: We can assume slot != NULL here because xcalloc never
14098 returns NULL. */
14099 if (*slot != NULL)
14100 {
14101 htab_delete (htab);
14102 return 1;
14103 }
14104 *slot = name;
14105 }
14106
14107 htab_delete (htab);
14108 return 0;
14109}
14110
14111/* When symbols change, it probably means the sources changed as well,
14112 and it might mean the static tracepoint markers are no longer at
14113 the same address or line numbers they used to be at last we
14114 checked. Losing your static tracepoints whenever you rebuild is
14115 undesirable. This function tries to resync/rematch gdb static
14116 tracepoints with the markers on the target, for static tracepoints
14117 that have not been set by marker id. Static tracepoint that have
14118 been set by marker id are reset by marker id in breakpoint_re_set.
14119 The heuristic is:
14120
14121 1) For a tracepoint set at a specific address, look for a marker at
14122 the old PC. If one is found there, assume to be the same marker.
14123 If the name / string id of the marker found is different from the
14124 previous known name, assume that means the user renamed the marker
14125 in the sources, and output a warning.
14126
14127 2) For a tracepoint set at a given line number, look for a marker
14128 at the new address of the old line number. If one is found there,
14129 assume to be the same marker. If the name / string id of the
14130 marker found is different from the previous known name, assume that
14131 means the user renamed the marker in the sources, and output a
14132 warning.
14133
14134 3) If a marker is no longer found at the same address or line, it
14135 may mean the marker no longer exists. But it may also just mean
14136 the code changed a bit. Maybe the user added a few lines of code
14137 that made the marker move up or down (in line number terms). Ask
14138 the target for info about the marker with the string id as we knew
14139 it. If found, update line number and address in the matching
14140 static tracepoint. This will get confused if there's more than one
14141 marker with the same ID (possible in UST, although unadvised
14142 precisely because it confuses tools). */
14143
14144static struct symtab_and_line
14145update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14146{
14147 struct tracepoint *tp = (struct tracepoint *) b;
14148 struct static_tracepoint_marker marker;
14149 CORE_ADDR pc;
14150
14151 pc = sal.pc;
14152 if (sal.line)
14153 find_line_pc (sal.symtab, sal.line, &pc);
14154
14155 if (target_static_tracepoint_marker_at (pc, &marker))
14156 {
14157 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14158 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14159 b->number,
14160 tp->static_trace_marker_id, marker.str_id);
14161
14162 xfree (tp->static_trace_marker_id);
14163 tp->static_trace_marker_id = xstrdup (marker.str_id);
14164 release_static_tracepoint_marker (&marker);
14165
14166 return sal;
14167 }
14168
14169 /* Old marker wasn't found on target at lineno. Try looking it up
14170 by string ID. */
14171 if (!sal.explicit_pc
14172 && sal.line != 0
14173 && sal.symtab != NULL
14174 && tp->static_trace_marker_id != NULL)
14175 {
14176 VEC(static_tracepoint_marker_p) *markers;
14177
14178 markers
14179 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14180
14181 if (!VEC_empty(static_tracepoint_marker_p, markers))
14182 {
14183 struct symtab_and_line sal2;
14184 struct symbol *sym;
14185 struct static_tracepoint_marker *tpmarker;
14186 struct ui_out *uiout = current_uiout;
14187 struct explicit_location explicit_loc;
14188
14189 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14190
14191 xfree (tp->static_trace_marker_id);
14192 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14193
14194 warning (_("marker for static tracepoint %d (%s) not "
14195 "found at previous line number"),
14196 b->number, tp->static_trace_marker_id);
14197
14198 init_sal (&sal2);
14199
14200 sal2.pc = tpmarker->address;
14201
14202 sal2 = find_pc_line (tpmarker->address, 0);
14203 sym = find_pc_sect_function (tpmarker->address, NULL);
14204 ui_out_text (uiout, "Now in ");
14205 if (sym)
14206 {
14207 ui_out_field_string (uiout, "func",
14208 SYMBOL_PRINT_NAME (sym));
14209 ui_out_text (uiout, " at ");
14210 }
14211 ui_out_field_string (uiout, "file",
14212 symtab_to_filename_for_display (sal2.symtab));
14213 ui_out_text (uiout, ":");
14214
14215 if (ui_out_is_mi_like_p (uiout))
14216 {
14217 const char *fullname = symtab_to_fullname (sal2.symtab);
14218
14219 ui_out_field_string (uiout, "fullname", fullname);
14220 }
14221
14222 ui_out_field_int (uiout, "line", sal2.line);
14223 ui_out_text (uiout, "\n");
14224
14225 b->loc->line_number = sal2.line;
14226 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14227
14228 delete_event_location (b->location);
14229 initialize_explicit_location (&explicit_loc);
14230 explicit_loc.source_filename
14231 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14232 explicit_loc.line_offset.offset = b->loc->line_number;
14233 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14234 b->location = new_explicit_location (&explicit_loc);
14235
14236 /* Might be nice to check if function changed, and warn if
14237 so. */
14238
14239 release_static_tracepoint_marker (tpmarker);
14240 }
14241 }
14242 return sal;
14243}
14244
14245/* Returns 1 iff locations A and B are sufficiently same that
14246 we don't need to report breakpoint as changed. */
14247
14248static int
14249locations_are_equal (struct bp_location *a, struct bp_location *b)
14250{
14251 while (a && b)
14252 {
14253 if (a->address != b->address)
14254 return 0;
14255
14256 if (a->shlib_disabled != b->shlib_disabled)
14257 return 0;
14258
14259 if (a->enabled != b->enabled)
14260 return 0;
14261
14262 a = a->next;
14263 b = b->next;
14264 }
14265
14266 if ((a == NULL) != (b == NULL))
14267 return 0;
14268
14269 return 1;
14270}
14271
14272/* Split all locations of B that are bound to PSPACE out of B's
14273 location list to a separate list and return that list's head. If
14274 PSPACE is NULL, hoist out all locations of B. */
14275
14276static struct bp_location *
14277hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14278{
14279 struct bp_location head;
14280 struct bp_location *i = b->loc;
14281 struct bp_location **i_link = &b->loc;
14282 struct bp_location *hoisted = &head;
14283
14284 if (pspace == NULL)
14285 {
14286 i = b->loc;
14287 b->loc = NULL;
14288 return i;
14289 }
14290
14291 head.next = NULL;
14292
14293 while (i != NULL)
14294 {
14295 if (i->pspace == pspace)
14296 {
14297 *i_link = i->next;
14298 i->next = NULL;
14299 hoisted->next = i;
14300 hoisted = i;
14301 }
14302 else
14303 i_link = &i->next;
14304 i = *i_link;
14305 }
14306
14307 return head.next;
14308}
14309
14310/* Create new breakpoint locations for B (a hardware or software
14311 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14312 zero, then B is a ranged breakpoint. Only recreates locations for
14313 FILTER_PSPACE. Locations of other program spaces are left
14314 untouched. */
14315
14316void
14317update_breakpoint_locations (struct breakpoint *b,
14318 struct program_space *filter_pspace,
14319 struct symtabs_and_lines sals,
14320 struct symtabs_and_lines sals_end)
14321{
14322 int i;
14323 struct bp_location *existing_locations;
14324
14325 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14326 {
14327 /* Ranged breakpoints have only one start location and one end
14328 location. */
14329 b->enable_state = bp_disabled;
14330 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14331 "multiple locations found\n"),
14332 b->number);
14333 return;
14334 }
14335
14336 /* If there's no new locations, and all existing locations are
14337 pending, don't do anything. This optimizes the common case where
14338 all locations are in the same shared library, that was unloaded.
14339 We'd like to retain the location, so that when the library is
14340 loaded again, we don't loose the enabled/disabled status of the
14341 individual locations. */
14342 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14343 return;
14344
14345 existing_locations = hoist_existing_locations (b, filter_pspace);
14346
14347 for (i = 0; i < sals.nelts; ++i)
14348 {
14349 struct bp_location *new_loc;
14350
14351 switch_to_program_space_and_thread (sals.sals[i].pspace);
14352
14353 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14354
14355 /* Reparse conditions, they might contain references to the
14356 old symtab. */
14357 if (b->cond_string != NULL)
14358 {
14359 const char *s;
14360
14361 s = b->cond_string;
14362 TRY
14363 {
14364 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14365 block_for_pc (sals.sals[i].pc),
14366 0);
14367 }
14368 CATCH (e, RETURN_MASK_ERROR)
14369 {
14370 warning (_("failed to reevaluate condition "
14371 "for breakpoint %d: %s"),
14372 b->number, e.message);
14373 new_loc->enabled = 0;
14374 }
14375 END_CATCH
14376 }
14377
14378 if (sals_end.nelts)
14379 {
14380 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14381
14382 new_loc->length = end - sals.sals[0].pc + 1;
14383 }
14384 }
14385
14386 /* If possible, carry over 'disable' status from existing
14387 breakpoints. */
14388 {
14389 struct bp_location *e = existing_locations;
14390 /* If there are multiple breakpoints with the same function name,
14391 e.g. for inline functions, comparing function names won't work.
14392 Instead compare pc addresses; this is just a heuristic as things
14393 may have moved, but in practice it gives the correct answer
14394 often enough until a better solution is found. */
14395 int have_ambiguous_names = ambiguous_names_p (b->loc);
14396
14397 for (; e; e = e->next)
14398 {
14399 if (!e->enabled && e->function_name)
14400 {
14401 struct bp_location *l = b->loc;
14402 if (have_ambiguous_names)
14403 {
14404 for (; l; l = l->next)
14405 if (breakpoint_locations_match (e, l))
14406 {
14407 l->enabled = 0;
14408 break;
14409 }
14410 }
14411 else
14412 {
14413 for (; l; l = l->next)
14414 if (l->function_name
14415 && strcmp (e->function_name, l->function_name) == 0)
14416 {
14417 l->enabled = 0;
14418 break;
14419 }
14420 }
14421 }
14422 }
14423 }
14424
14425 if (!locations_are_equal (existing_locations, b->loc))
14426 observer_notify_breakpoint_modified (b);
14427}
14428
14429/* Find the SaL locations corresponding to the given LOCATION.
14430 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14431
14432static struct symtabs_and_lines
14433location_to_sals (struct breakpoint *b, struct event_location *location,
14434 struct program_space *search_pspace, int *found)
14435{
14436 struct symtabs_and_lines sals = {0};
14437 struct gdb_exception exception = exception_none;
14438
14439 gdb_assert (b->ops != NULL);
14440
14441 TRY
14442 {
14443 b->ops->decode_location (b, location, search_pspace, &sals);
14444 }
14445 CATCH (e, RETURN_MASK_ERROR)
14446 {
14447 int not_found_and_ok = 0;
14448
14449 exception = e;
14450
14451 /* For pending breakpoints, it's expected that parsing will
14452 fail until the right shared library is loaded. User has
14453 already told to create pending breakpoints and don't need
14454 extra messages. If breakpoint is in bp_shlib_disabled
14455 state, then user already saw the message about that
14456 breakpoint being disabled, and don't want to see more
14457 errors. */
14458 if (e.error == NOT_FOUND_ERROR
14459 && (b->condition_not_parsed
14460 || (b->loc != NULL
14461 && search_pspace != NULL
14462 && b->loc->pspace != search_pspace)
14463 || (b->loc && b->loc->shlib_disabled)
14464 || (b->loc && b->loc->pspace->executing_startup)
14465 || b->enable_state == bp_disabled))
14466 not_found_and_ok = 1;
14467
14468 if (!not_found_and_ok)
14469 {
14470 /* We surely don't want to warn about the same breakpoint
14471 10 times. One solution, implemented here, is disable
14472 the breakpoint on error. Another solution would be to
14473 have separate 'warning emitted' flag. Since this
14474 happens only when a binary has changed, I don't know
14475 which approach is better. */
14476 b->enable_state = bp_disabled;
14477 throw_exception (e);
14478 }
14479 }
14480 END_CATCH
14481
14482 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14483 {
14484 int i;
14485
14486 for (i = 0; i < sals.nelts; ++i)
14487 resolve_sal_pc (&sals.sals[i]);
14488 if (b->condition_not_parsed && b->extra_string != NULL)
14489 {
14490 char *cond_string, *extra_string;
14491 int thread, task;
14492
14493 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14494 &cond_string, &thread, &task,
14495 &extra_string);
14496 gdb_assert (b->cond_string == NULL);
14497 if (cond_string)
14498 b->cond_string = cond_string;
14499 b->thread = thread;
14500 b->task = task;
14501 if (extra_string)
14502 {
14503 xfree (b->extra_string);
14504 b->extra_string = extra_string;
14505 }
14506 b->condition_not_parsed = 0;
14507 }
14508
14509 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14510 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14511
14512 *found = 1;
14513 }
14514 else
14515 *found = 0;
14516
14517 return sals;
14518}
14519
14520/* The default re_set method, for typical hardware or software
14521 breakpoints. Reevaluate the breakpoint and recreate its
14522 locations. */
14523
14524static void
14525breakpoint_re_set_default (struct breakpoint *b)
14526{
14527 int found;
14528 struct symtabs_and_lines sals, sals_end;
14529 struct symtabs_and_lines expanded = {0};
14530 struct symtabs_and_lines expanded_end = {0};
14531 struct program_space *filter_pspace = current_program_space;
14532
14533 sals = location_to_sals (b, b->location, filter_pspace, &found);
14534 if (found)
14535 {
14536 make_cleanup (xfree, sals.sals);
14537 expanded = sals;
14538 }
14539
14540 if (b->location_range_end != NULL)
14541 {
14542 sals_end = location_to_sals (b, b->location_range_end,
14543 filter_pspace, &found);
14544 if (found)
14545 {
14546 make_cleanup (xfree, sals_end.sals);
14547 expanded_end = sals_end;
14548 }
14549 }
14550
14551 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14552}
14553
14554/* Default method for creating SALs from an address string. It basically
14555 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14556
14557static void
14558create_sals_from_location_default (const struct event_location *location,
14559 struct linespec_result *canonical,
14560 enum bptype type_wanted)
14561{
14562 parse_breakpoint_sals (location, canonical);
14563}
14564
14565/* Call create_breakpoints_sal for the given arguments. This is the default
14566 function for the `create_breakpoints_sal' method of
14567 breakpoint_ops. */
14568
14569static void
14570create_breakpoints_sal_default (struct gdbarch *gdbarch,
14571 struct linespec_result *canonical,
14572 char *cond_string,
14573 char *extra_string,
14574 enum bptype type_wanted,
14575 enum bpdisp disposition,
14576 int thread,
14577 int task, int ignore_count,
14578 const struct breakpoint_ops *ops,
14579 int from_tty, int enabled,
14580 int internal, unsigned flags)
14581{
14582 create_breakpoints_sal (gdbarch, canonical, cond_string,
14583 extra_string,
14584 type_wanted, disposition,
14585 thread, task, ignore_count, ops, from_tty,
14586 enabled, internal, flags);
14587}
14588
14589/* Decode the line represented by S by calling decode_line_full. This is the
14590 default function for the `decode_location' method of breakpoint_ops. */
14591
14592static void
14593decode_location_default (struct breakpoint *b,
14594 const struct event_location *location,
14595 struct program_space *search_pspace,
14596 struct symtabs_and_lines *sals)
14597{
14598 struct linespec_result canonical;
14599
14600 init_linespec_result (&canonical);
14601 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14602 (struct symtab *) NULL, 0,
14603 &canonical, multiple_symbols_all,
14604 b->filter);
14605
14606 /* We should get 0 or 1 resulting SALs. */
14607 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14608
14609 if (VEC_length (linespec_sals, canonical.sals) > 0)
14610 {
14611 struct linespec_sals *lsal;
14612
14613 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14614 *sals = lsal->sals;
14615 /* Arrange it so the destructor does not free the
14616 contents. */
14617 lsal->sals.sals = NULL;
14618 }
14619
14620 destroy_linespec_result (&canonical);
14621}
14622
14623/* Prepare the global context for a re-set of breakpoint B. */
14624
14625static struct cleanup *
14626prepare_re_set_context (struct breakpoint *b)
14627{
14628 input_radix = b->input_radix;
14629 set_language (b->language);
14630
14631 return make_cleanup (null_cleanup, NULL);
14632}
14633
14634/* Reset a breakpoint given it's struct breakpoint * BINT.
14635 The value we return ends up being the return value from catch_errors.
14636 Unused in this case. */
14637
14638static int
14639breakpoint_re_set_one (void *bint)
14640{
14641 /* Get past catch_errs. */
14642 struct breakpoint *b = (struct breakpoint *) bint;
14643 struct cleanup *cleanups;
14644
14645 cleanups = prepare_re_set_context (b);
14646 b->ops->re_set (b);
14647 do_cleanups (cleanups);
14648 return 0;
14649}
14650
14651/* Re-set breakpoint locations for the current program space.
14652 Locations bound to other program spaces are left untouched. */
14653
14654void
14655breakpoint_re_set (void)
14656{
14657 struct breakpoint *b, *b_tmp;
14658 enum language save_language;
14659 int save_input_radix;
14660 struct cleanup *old_chain;
14661
14662 save_language = current_language->la_language;
14663 save_input_radix = input_radix;
14664 old_chain = save_current_space_and_thread ();
14665
14666 /* Note: we must not try to insert locations until after all
14667 breakpoints have been re-set. Otherwise, e.g., when re-setting
14668 breakpoint 1, we'd insert the locations of breakpoint 2, which
14669 hadn't been re-set yet, and thus may have stale locations. */
14670
14671 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14672 {
14673 /* Format possible error msg. */
14674 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14675 b->number);
14676 struct cleanup *cleanups = make_cleanup (xfree, message);
14677 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14678 do_cleanups (cleanups);
14679 }
14680 set_language (save_language);
14681 input_radix = save_input_radix;
14682
14683 jit_breakpoint_re_set ();
14684
14685 do_cleanups (old_chain);
14686
14687 create_overlay_event_breakpoint ();
14688 create_longjmp_master_breakpoint ();
14689 create_std_terminate_master_breakpoint ();
14690 create_exception_master_breakpoint ();
14691
14692 /* Now we can insert. */
14693 update_global_location_list (UGLL_MAY_INSERT);
14694}
14695\f
14696/* Reset the thread number of this breakpoint:
14697
14698 - If the breakpoint is for all threads, leave it as-is.
14699 - Else, reset it to the current thread for inferior_ptid. */
14700void
14701breakpoint_re_set_thread (struct breakpoint *b)
14702{
14703 if (b->thread != -1)
14704 {
14705 if (in_thread_list (inferior_ptid))
14706 b->thread = ptid_to_global_thread_id (inferior_ptid);
14707
14708 /* We're being called after following a fork. The new fork is
14709 selected as current, and unless this was a vfork will have a
14710 different program space from the original thread. Reset that
14711 as well. */
14712 b->loc->pspace = current_program_space;
14713 }
14714}
14715
14716/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14717 If from_tty is nonzero, it prints a message to that effect,
14718 which ends with a period (no newline). */
14719
14720void
14721set_ignore_count (int bptnum, int count, int from_tty)
14722{
14723 struct breakpoint *b;
14724
14725 if (count < 0)
14726 count = 0;
14727
14728 ALL_BREAKPOINTS (b)
14729 if (b->number == bptnum)
14730 {
14731 if (is_tracepoint (b))
14732 {
14733 if (from_tty && count != 0)
14734 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14735 bptnum);
14736 return;
14737 }
14738
14739 b->ignore_count = count;
14740 if (from_tty)
14741 {
14742 if (count == 0)
14743 printf_filtered (_("Will stop next time "
14744 "breakpoint %d is reached."),
14745 bptnum);
14746 else if (count == 1)
14747 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14748 bptnum);
14749 else
14750 printf_filtered (_("Will ignore next %d "
14751 "crossings of breakpoint %d."),
14752 count, bptnum);
14753 }
14754 observer_notify_breakpoint_modified (b);
14755 return;
14756 }
14757
14758 error (_("No breakpoint number %d."), bptnum);
14759}
14760
14761/* Command to set ignore-count of breakpoint N to COUNT. */
14762
14763static void
14764ignore_command (char *args, int from_tty)
14765{
14766 char *p = args;
14767 int num;
14768
14769 if (p == 0)
14770 error_no_arg (_("a breakpoint number"));
14771
14772 num = get_number (&p);
14773 if (num == 0)
14774 error (_("bad breakpoint number: '%s'"), args);
14775 if (*p == 0)
14776 error (_("Second argument (specified ignore-count) is missing."));
14777
14778 set_ignore_count (num,
14779 longest_to_int (value_as_long (parse_and_eval (p))),
14780 from_tty);
14781 if (from_tty)
14782 printf_filtered ("\n");
14783}
14784\f
14785/* Call FUNCTION on each of the breakpoints
14786 whose numbers are given in ARGS. */
14787
14788static void
14789map_breakpoint_numbers (const char *args,
14790 void (*function) (struct breakpoint *,
14791 void *),
14792 void *data)
14793{
14794 int num;
14795 struct breakpoint *b, *tmp;
14796
14797 if (args == 0 || *args == '\0')
14798 error_no_arg (_("one or more breakpoint numbers"));
14799
14800 number_or_range_parser parser (args);
14801
14802 while (!parser.finished ())
14803 {
14804 const char *p = parser.cur_tok ();
14805 bool match = false;
14806
14807 num = parser.get_number ();
14808 if (num == 0)
14809 {
14810 warning (_("bad breakpoint number at or near '%s'"), p);
14811 }
14812 else
14813 {
14814 ALL_BREAKPOINTS_SAFE (b, tmp)
14815 if (b->number == num)
14816 {
14817 match = true;
14818 function (b, data);
14819 break;
14820 }
14821 if (!match)
14822 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14823 }
14824 }
14825}
14826
14827static struct bp_location *
14828find_location_by_number (char *number)
14829{
14830 char *dot = strchr (number, '.');
14831 char *p1;
14832 int bp_num;
14833 int loc_num;
14834 struct breakpoint *b;
14835 struct bp_location *loc;
14836
14837 *dot = '\0';
14838
14839 p1 = number;
14840 bp_num = get_number (&p1);
14841 if (bp_num == 0)
14842 error (_("Bad breakpoint number '%s'"), number);
14843
14844 ALL_BREAKPOINTS (b)
14845 if (b->number == bp_num)
14846 {
14847 break;
14848 }
14849
14850 if (!b || b->number != bp_num)
14851 error (_("Bad breakpoint number '%s'"), number);
14852
14853 p1 = dot+1;
14854 loc_num = get_number (&p1);
14855 if (loc_num == 0)
14856 error (_("Bad breakpoint location number '%s'"), number);
14857
14858 --loc_num;
14859 loc = b->loc;
14860 for (;loc_num && loc; --loc_num, loc = loc->next)
14861 ;
14862 if (!loc)
14863 error (_("Bad breakpoint location number '%s'"), dot+1);
14864
14865 return loc;
14866}
14867
14868
14869/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14870 If from_tty is nonzero, it prints a message to that effect,
14871 which ends with a period (no newline). */
14872
14873void
14874disable_breakpoint (struct breakpoint *bpt)
14875{
14876 /* Never disable a watchpoint scope breakpoint; we want to
14877 hit them when we leave scope so we can delete both the
14878 watchpoint and its scope breakpoint at that time. */
14879 if (bpt->type == bp_watchpoint_scope)
14880 return;
14881
14882 bpt->enable_state = bp_disabled;
14883
14884 /* Mark breakpoint locations modified. */
14885 mark_breakpoint_modified (bpt);
14886
14887 if (target_supports_enable_disable_tracepoint ()
14888 && current_trace_status ()->running && is_tracepoint (bpt))
14889 {
14890 struct bp_location *location;
14891
14892 for (location = bpt->loc; location; location = location->next)
14893 target_disable_tracepoint (location);
14894 }
14895
14896 update_global_location_list (UGLL_DONT_INSERT);
14897
14898 observer_notify_breakpoint_modified (bpt);
14899}
14900
14901/* A callback for iterate_over_related_breakpoints. */
14902
14903static void
14904do_disable_breakpoint (struct breakpoint *b, void *ignore)
14905{
14906 disable_breakpoint (b);
14907}
14908
14909/* A callback for map_breakpoint_numbers that calls
14910 disable_breakpoint. */
14911
14912static void
14913do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14914{
14915 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14916}
14917
14918static void
14919disable_command (char *args, int from_tty)
14920{
14921 if (args == 0)
14922 {
14923 struct breakpoint *bpt;
14924
14925 ALL_BREAKPOINTS (bpt)
14926 if (user_breakpoint_p (bpt))
14927 disable_breakpoint (bpt);
14928 }
14929 else
14930 {
14931 char *num = extract_arg (&args);
14932
14933 while (num)
14934 {
14935 if (strchr (num, '.'))
14936 {
14937 struct bp_location *loc = find_location_by_number (num);
14938
14939 if (loc)
14940 {
14941 if (loc->enabled)
14942 {
14943 loc->enabled = 0;
14944 mark_breakpoint_location_modified (loc);
14945 }
14946 if (target_supports_enable_disable_tracepoint ()
14947 && current_trace_status ()->running && loc->owner
14948 && is_tracepoint (loc->owner))
14949 target_disable_tracepoint (loc);
14950 }
14951 update_global_location_list (UGLL_DONT_INSERT);
14952 }
14953 else
14954 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14955 num = extract_arg (&args);
14956 }
14957 }
14958}
14959
14960static void
14961enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14962 int count)
14963{
14964 int target_resources_ok;
14965
14966 if (bpt->type == bp_hardware_breakpoint)
14967 {
14968 int i;
14969 i = hw_breakpoint_used_count ();
14970 target_resources_ok =
14971 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14972 i + 1, 0);
14973 if (target_resources_ok == 0)
14974 error (_("No hardware breakpoint support in the target."));
14975 else if (target_resources_ok < 0)
14976 error (_("Hardware breakpoints used exceeds limit."));
14977 }
14978
14979 if (is_watchpoint (bpt))
14980 {
14981 /* Initialize it just to avoid a GCC false warning. */
14982 enum enable_state orig_enable_state = bp_disabled;
14983
14984 TRY
14985 {
14986 struct watchpoint *w = (struct watchpoint *) bpt;
14987
14988 orig_enable_state = bpt->enable_state;
14989 bpt->enable_state = bp_enabled;
14990 update_watchpoint (w, 1 /* reparse */);
14991 }
14992 CATCH (e, RETURN_MASK_ALL)
14993 {
14994 bpt->enable_state = orig_enable_state;
14995 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14996 bpt->number);
14997 return;
14998 }
14999 END_CATCH
15000 }
15001
15002 bpt->enable_state = bp_enabled;
15003
15004 /* Mark breakpoint locations modified. */
15005 mark_breakpoint_modified (bpt);
15006
15007 if (target_supports_enable_disable_tracepoint ()
15008 && current_trace_status ()->running && is_tracepoint (bpt))
15009 {
15010 struct bp_location *location;
15011
15012 for (location = bpt->loc; location; location = location->next)
15013 target_enable_tracepoint (location);
15014 }
15015
15016 bpt->disposition = disposition;
15017 bpt->enable_count = count;
15018 update_global_location_list (UGLL_MAY_INSERT);
15019
15020 observer_notify_breakpoint_modified (bpt);
15021}
15022
15023
15024void
15025enable_breakpoint (struct breakpoint *bpt)
15026{
15027 enable_breakpoint_disp (bpt, bpt->disposition, 0);
15028}
15029
15030static void
15031do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15032{
15033 enable_breakpoint (bpt);
15034}
15035
15036/* A callback for map_breakpoint_numbers that calls
15037 enable_breakpoint. */
15038
15039static void
15040do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15041{
15042 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15043}
15044
15045/* The enable command enables the specified breakpoints (or all defined
15046 breakpoints) so they once again become (or continue to be) effective
15047 in stopping the inferior. */
15048
15049static void
15050enable_command (char *args, int from_tty)
15051{
15052 if (args == 0)
15053 {
15054 struct breakpoint *bpt;
15055
15056 ALL_BREAKPOINTS (bpt)
15057 if (user_breakpoint_p (bpt))
15058 enable_breakpoint (bpt);
15059 }
15060 else
15061 {
15062 char *num = extract_arg (&args);
15063
15064 while (num)
15065 {
15066 if (strchr (num, '.'))
15067 {
15068 struct bp_location *loc = find_location_by_number (num);
15069
15070 if (loc)
15071 {
15072 if (!loc->enabled)
15073 {
15074 loc->enabled = 1;
15075 mark_breakpoint_location_modified (loc);
15076 }
15077 if (target_supports_enable_disable_tracepoint ()
15078 && current_trace_status ()->running && loc->owner
15079 && is_tracepoint (loc->owner))
15080 target_enable_tracepoint (loc);
15081 }
15082 update_global_location_list (UGLL_MAY_INSERT);
15083 }
15084 else
15085 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15086 num = extract_arg (&args);
15087 }
15088 }
15089}
15090
15091/* This struct packages up disposition data for application to multiple
15092 breakpoints. */
15093
15094struct disp_data
15095{
15096 enum bpdisp disp;
15097 int count;
15098};
15099
15100static void
15101do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15102{
15103 struct disp_data disp_data = *(struct disp_data *) arg;
15104
15105 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15106}
15107
15108static void
15109do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15110{
15111 struct disp_data disp = { disp_disable, 1 };
15112
15113 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15114}
15115
15116static void
15117enable_once_command (char *args, int from_tty)
15118{
15119 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15120}
15121
15122static void
15123do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15124{
15125 struct disp_data disp = { disp_disable, *(int *) countptr };
15126
15127 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15128}
15129
15130static void
15131enable_count_command (char *args, int from_tty)
15132{
15133 int count;
15134
15135 if (args == NULL)
15136 error_no_arg (_("hit count"));
15137
15138 count = get_number (&args);
15139
15140 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15141}
15142
15143static void
15144do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15145{
15146 struct disp_data disp = { disp_del, 1 };
15147
15148 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15149}
15150
15151static void
15152enable_delete_command (char *args, int from_tty)
15153{
15154 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15155}
15156\f
15157static void
15158set_breakpoint_cmd (char *args, int from_tty)
15159{
15160}
15161
15162static void
15163show_breakpoint_cmd (char *args, int from_tty)
15164{
15165}
15166
15167/* Invalidate last known value of any hardware watchpoint if
15168 the memory which that value represents has been written to by
15169 GDB itself. */
15170
15171static void
15172invalidate_bp_value_on_memory_change (struct inferior *inferior,
15173 CORE_ADDR addr, ssize_t len,
15174 const bfd_byte *data)
15175{
15176 struct breakpoint *bp;
15177
15178 ALL_BREAKPOINTS (bp)
15179 if (bp->enable_state == bp_enabled
15180 && bp->type == bp_hardware_watchpoint)
15181 {
15182 struct watchpoint *wp = (struct watchpoint *) bp;
15183
15184 if (wp->val_valid && wp->val)
15185 {
15186 struct bp_location *loc;
15187
15188 for (loc = bp->loc; loc != NULL; loc = loc->next)
15189 if (loc->loc_type == bp_loc_hardware_watchpoint
15190 && loc->address + loc->length > addr
15191 && addr + len > loc->address)
15192 {
15193 value_free (wp->val);
15194 wp->val = NULL;
15195 wp->val_valid = 0;
15196 }
15197 }
15198 }
15199}
15200
15201/* Create and insert a breakpoint for software single step. */
15202
15203void
15204insert_single_step_breakpoint (struct gdbarch *gdbarch,
15205 struct address_space *aspace,
15206 CORE_ADDR next_pc)
15207{
15208 struct thread_info *tp = inferior_thread ();
15209 struct symtab_and_line sal;
15210 CORE_ADDR pc = next_pc;
15211
15212 if (tp->control.single_step_breakpoints == NULL)
15213 {
15214 tp->control.single_step_breakpoints
15215 = new_single_step_breakpoint (tp->global_num, gdbarch);
15216 }
15217
15218 sal = find_pc_line (pc, 0);
15219 sal.pc = pc;
15220 sal.section = find_pc_overlay (pc);
15221 sal.explicit_pc = 1;
15222 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15223
15224 update_global_location_list (UGLL_INSERT);
15225}
15226
15227/* Insert single step breakpoints according to the current state. */
15228
15229int
15230insert_single_step_breakpoints (struct gdbarch *gdbarch)
15231{
15232 struct frame_info *frame = get_current_frame ();
15233 VEC (CORE_ADDR) * next_pcs;
15234
15235 next_pcs = gdbarch_software_single_step (gdbarch, frame);
15236
15237 if (next_pcs != NULL)
15238 {
15239 int i;
15240 CORE_ADDR pc;
15241 struct address_space *aspace = get_frame_address_space (frame);
15242
15243 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
15244 insert_single_step_breakpoint (gdbarch, aspace, pc);
15245
15246 VEC_free (CORE_ADDR, next_pcs);
15247
15248 return 1;
15249 }
15250 else
15251 return 0;
15252}
15253
15254/* See breakpoint.h. */
15255
15256int
15257breakpoint_has_location_inserted_here (struct breakpoint *bp,
15258 struct address_space *aspace,
15259 CORE_ADDR pc)
15260{
15261 struct bp_location *loc;
15262
15263 for (loc = bp->loc; loc != NULL; loc = loc->next)
15264 if (loc->inserted
15265 && breakpoint_location_address_match (loc, aspace, pc))
15266 return 1;
15267
15268 return 0;
15269}
15270
15271/* Check whether a software single-step breakpoint is inserted at
15272 PC. */
15273
15274int
15275single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15276 CORE_ADDR pc)
15277{
15278 struct breakpoint *bpt;
15279
15280 ALL_BREAKPOINTS (bpt)
15281 {
15282 if (bpt->type == bp_single_step
15283 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15284 return 1;
15285 }
15286 return 0;
15287}
15288
15289/* Tracepoint-specific operations. */
15290
15291/* Set tracepoint count to NUM. */
15292static void
15293set_tracepoint_count (int num)
15294{
15295 tracepoint_count = num;
15296 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15297}
15298
15299static void
15300trace_command (char *arg, int from_tty)
15301{
15302 struct breakpoint_ops *ops;
15303 struct event_location *location;
15304 struct cleanup *back_to;
15305
15306 location = string_to_event_location (&arg, current_language);
15307 back_to = make_cleanup_delete_event_location (location);
15308 if (location != NULL
15309 && event_location_type (location) == PROBE_LOCATION)
15310 ops = &tracepoint_probe_breakpoint_ops;
15311 else
15312 ops = &tracepoint_breakpoint_ops;
15313
15314 create_breakpoint (get_current_arch (),
15315 location,
15316 NULL, 0, arg, 1 /* parse arg */,
15317 0 /* tempflag */,
15318 bp_tracepoint /* type_wanted */,
15319 0 /* Ignore count */,
15320 pending_break_support,
15321 ops,
15322 from_tty,
15323 1 /* enabled */,
15324 0 /* internal */, 0);
15325 do_cleanups (back_to);
15326}
15327
15328static void
15329ftrace_command (char *arg, int from_tty)
15330{
15331 struct event_location *location;
15332 struct cleanup *back_to;
15333
15334 location = string_to_event_location (&arg, current_language);
15335 back_to = make_cleanup_delete_event_location (location);
15336 create_breakpoint (get_current_arch (),
15337 location,
15338 NULL, 0, arg, 1 /* parse arg */,
15339 0 /* tempflag */,
15340 bp_fast_tracepoint /* type_wanted */,
15341 0 /* Ignore count */,
15342 pending_break_support,
15343 &tracepoint_breakpoint_ops,
15344 from_tty,
15345 1 /* enabled */,
15346 0 /* internal */, 0);
15347 do_cleanups (back_to);
15348}
15349
15350/* strace command implementation. Creates a static tracepoint. */
15351
15352static void
15353strace_command (char *arg, int from_tty)
15354{
15355 struct breakpoint_ops *ops;
15356 struct event_location *location;
15357 struct cleanup *back_to;
15358
15359 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15360 or with a normal static tracepoint. */
15361 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15362 {
15363 ops = &strace_marker_breakpoint_ops;
15364 location = new_linespec_location (&arg);
15365 }
15366 else
15367 {
15368 ops = &tracepoint_breakpoint_ops;
15369 location = string_to_event_location (&arg, current_language);
15370 }
15371
15372 back_to = make_cleanup_delete_event_location (location);
15373 create_breakpoint (get_current_arch (),
15374 location,
15375 NULL, 0, arg, 1 /* parse arg */,
15376 0 /* tempflag */,
15377 bp_static_tracepoint /* type_wanted */,
15378 0 /* Ignore count */,
15379 pending_break_support,
15380 ops,
15381 from_tty,
15382 1 /* enabled */,
15383 0 /* internal */, 0);
15384 do_cleanups (back_to);
15385}
15386
15387/* Set up a fake reader function that gets command lines from a linked
15388 list that was acquired during tracepoint uploading. */
15389
15390static struct uploaded_tp *this_utp;
15391static int next_cmd;
15392
15393static char *
15394read_uploaded_action (void)
15395{
15396 char *rslt;
15397
15398 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15399
15400 next_cmd++;
15401
15402 return rslt;
15403}
15404
15405/* Given information about a tracepoint as recorded on a target (which
15406 can be either a live system or a trace file), attempt to create an
15407 equivalent GDB tracepoint. This is not a reliable process, since
15408 the target does not necessarily have all the information used when
15409 the tracepoint was originally defined. */
15410
15411struct tracepoint *
15412create_tracepoint_from_upload (struct uploaded_tp *utp)
15413{
15414 char *addr_str, small_buf[100];
15415 struct tracepoint *tp;
15416 struct event_location *location;
15417 struct cleanup *cleanup;
15418
15419 if (utp->at_string)
15420 addr_str = utp->at_string;
15421 else
15422 {
15423 /* In the absence of a source location, fall back to raw
15424 address. Since there is no way to confirm that the address
15425 means the same thing as when the trace was started, warn the
15426 user. */
15427 warning (_("Uploaded tracepoint %d has no "
15428 "source location, using raw address"),
15429 utp->number);
15430 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15431 addr_str = small_buf;
15432 }
15433
15434 /* There's not much we can do with a sequence of bytecodes. */
15435 if (utp->cond && !utp->cond_string)
15436 warning (_("Uploaded tracepoint %d condition "
15437 "has no source form, ignoring it"),
15438 utp->number);
15439
15440 location = string_to_event_location (&addr_str, current_language);
15441 cleanup = make_cleanup_delete_event_location (location);
15442 if (!create_breakpoint (get_current_arch (),
15443 location,
15444 utp->cond_string, -1, addr_str,
15445 0 /* parse cond/thread */,
15446 0 /* tempflag */,
15447 utp->type /* type_wanted */,
15448 0 /* Ignore count */,
15449 pending_break_support,
15450 &tracepoint_breakpoint_ops,
15451 0 /* from_tty */,
15452 utp->enabled /* enabled */,
15453 0 /* internal */,
15454 CREATE_BREAKPOINT_FLAGS_INSERTED))
15455 {
15456 do_cleanups (cleanup);
15457 return NULL;
15458 }
15459
15460 do_cleanups (cleanup);
15461
15462 /* Get the tracepoint we just created. */
15463 tp = get_tracepoint (tracepoint_count);
15464 gdb_assert (tp != NULL);
15465
15466 if (utp->pass > 0)
15467 {
15468 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15469 tp->base.number);
15470
15471 trace_pass_command (small_buf, 0);
15472 }
15473
15474 /* If we have uploaded versions of the original commands, set up a
15475 special-purpose "reader" function and call the usual command line
15476 reader, then pass the result to the breakpoint command-setting
15477 function. */
15478 if (!VEC_empty (char_ptr, utp->cmd_strings))
15479 {
15480 struct command_line *cmd_list;
15481
15482 this_utp = utp;
15483 next_cmd = 0;
15484
15485 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15486
15487 breakpoint_set_commands (&tp->base, cmd_list);
15488 }
15489 else if (!VEC_empty (char_ptr, utp->actions)
15490 || !VEC_empty (char_ptr, utp->step_actions))
15491 warning (_("Uploaded tracepoint %d actions "
15492 "have no source form, ignoring them"),
15493 utp->number);
15494
15495 /* Copy any status information that might be available. */
15496 tp->base.hit_count = utp->hit_count;
15497 tp->traceframe_usage = utp->traceframe_usage;
15498
15499 return tp;
15500}
15501
15502/* Print information on tracepoint number TPNUM_EXP, or all if
15503 omitted. */
15504
15505static void
15506tracepoints_info (char *args, int from_tty)
15507{
15508 struct ui_out *uiout = current_uiout;
15509 int num_printed;
15510
15511 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15512
15513 if (num_printed == 0)
15514 {
15515 if (args == NULL || *args == '\0')
15516 ui_out_message (uiout, 0, "No tracepoints.\n");
15517 else
15518 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15519 }
15520
15521 default_collect_info ();
15522}
15523
15524/* The 'enable trace' command enables tracepoints.
15525 Not supported by all targets. */
15526static void
15527enable_trace_command (char *args, int from_tty)
15528{
15529 enable_command (args, from_tty);
15530}
15531
15532/* The 'disable trace' command disables tracepoints.
15533 Not supported by all targets. */
15534static void
15535disable_trace_command (char *args, int from_tty)
15536{
15537 disable_command (args, from_tty);
15538}
15539
15540/* Remove a tracepoint (or all if no argument). */
15541static void
15542delete_trace_command (char *arg, int from_tty)
15543{
15544 struct breakpoint *b, *b_tmp;
15545
15546 dont_repeat ();
15547
15548 if (arg == 0)
15549 {
15550 int breaks_to_delete = 0;
15551
15552 /* Delete all breakpoints if no argument.
15553 Do not delete internal or call-dummy breakpoints, these
15554 have to be deleted with an explicit breakpoint number
15555 argument. */
15556 ALL_TRACEPOINTS (b)
15557 if (is_tracepoint (b) && user_breakpoint_p (b))
15558 {
15559 breaks_to_delete = 1;
15560 break;
15561 }
15562
15563 /* Ask user only if there are some breakpoints to delete. */
15564 if (!from_tty
15565 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15566 {
15567 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15568 if (is_tracepoint (b) && user_breakpoint_p (b))
15569 delete_breakpoint (b);
15570 }
15571 }
15572 else
15573 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15574}
15575
15576/* Helper function for trace_pass_command. */
15577
15578static void
15579trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15580{
15581 tp->pass_count = count;
15582 observer_notify_breakpoint_modified (&tp->base);
15583 if (from_tty)
15584 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15585 tp->base.number, count);
15586}
15587
15588/* Set passcount for tracepoint.
15589
15590 First command argument is passcount, second is tracepoint number.
15591 If tracepoint number omitted, apply to most recently defined.
15592 Also accepts special argument "all". */
15593
15594static void
15595trace_pass_command (char *args, int from_tty)
15596{
15597 struct tracepoint *t1;
15598 unsigned int count;
15599
15600 if (args == 0 || *args == 0)
15601 error (_("passcount command requires an "
15602 "argument (count + optional TP num)"));
15603
15604 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15605
15606 args = skip_spaces (args);
15607 if (*args && strncasecmp (args, "all", 3) == 0)
15608 {
15609 struct breakpoint *b;
15610
15611 args += 3; /* Skip special argument "all". */
15612 if (*args)
15613 error (_("Junk at end of arguments."));
15614
15615 ALL_TRACEPOINTS (b)
15616 {
15617 t1 = (struct tracepoint *) b;
15618 trace_pass_set_count (t1, count, from_tty);
15619 }
15620 }
15621 else if (*args == '\0')
15622 {
15623 t1 = get_tracepoint_by_number (&args, NULL);
15624 if (t1)
15625 trace_pass_set_count (t1, count, from_tty);
15626 }
15627 else
15628 {
15629 number_or_range_parser parser (args);
15630 while (!parser.finished ())
15631 {
15632 t1 = get_tracepoint_by_number (&args, &parser);
15633 if (t1)
15634 trace_pass_set_count (t1, count, from_tty);
15635 }
15636 }
15637}
15638
15639struct tracepoint *
15640get_tracepoint (int num)
15641{
15642 struct breakpoint *t;
15643
15644 ALL_TRACEPOINTS (t)
15645 if (t->number == num)
15646 return (struct tracepoint *) t;
15647
15648 return NULL;
15649}
15650
15651/* Find the tracepoint with the given target-side number (which may be
15652 different from the tracepoint number after disconnecting and
15653 reconnecting). */
15654
15655struct tracepoint *
15656get_tracepoint_by_number_on_target (int num)
15657{
15658 struct breakpoint *b;
15659
15660 ALL_TRACEPOINTS (b)
15661 {
15662 struct tracepoint *t = (struct tracepoint *) b;
15663
15664 if (t->number_on_target == num)
15665 return t;
15666 }
15667
15668 return NULL;
15669}
15670
15671/* Utility: parse a tracepoint number and look it up in the list.
15672 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15673 If the argument is missing, the most recent tracepoint
15674 (tracepoint_count) is returned. */
15675
15676struct tracepoint *
15677get_tracepoint_by_number (char **arg,
15678 number_or_range_parser *parser)
15679{
15680 struct breakpoint *t;
15681 int tpnum;
15682 char *instring = arg == NULL ? NULL : *arg;
15683
15684 if (parser != NULL)
15685 {
15686 gdb_assert (!parser->finished ());
15687 tpnum = parser->get_number ();
15688 }
15689 else if (arg == NULL || *arg == NULL || ! **arg)
15690 tpnum = tracepoint_count;
15691 else
15692 tpnum = get_number (arg);
15693
15694 if (tpnum <= 0)
15695 {
15696 if (instring && *instring)
15697 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15698 instring);
15699 else
15700 printf_filtered (_("No previous tracepoint\n"));
15701 return NULL;
15702 }
15703
15704 ALL_TRACEPOINTS (t)
15705 if (t->number == tpnum)
15706 {
15707 return (struct tracepoint *) t;
15708 }
15709
15710 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15711 return NULL;
15712}
15713
15714void
15715print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15716{
15717 if (b->thread != -1)
15718 fprintf_unfiltered (fp, " thread %d", b->thread);
15719
15720 if (b->task != 0)
15721 fprintf_unfiltered (fp, " task %d", b->task);
15722
15723 fprintf_unfiltered (fp, "\n");
15724}
15725
15726/* Save information on user settable breakpoints (watchpoints, etc) to
15727 a new script file named FILENAME. If FILTER is non-NULL, call it
15728 on each breakpoint and only include the ones for which it returns
15729 non-zero. */
15730
15731static void
15732save_breakpoints (char *filename, int from_tty,
15733 int (*filter) (const struct breakpoint *))
15734{
15735 struct breakpoint *tp;
15736 int any = 0;
15737 struct cleanup *cleanup;
15738 struct ui_file *fp;
15739 int extra_trace_bits = 0;
15740
15741 if (filename == 0 || *filename == 0)
15742 error (_("Argument required (file name in which to save)"));
15743
15744 /* See if we have anything to save. */
15745 ALL_BREAKPOINTS (tp)
15746 {
15747 /* Skip internal and momentary breakpoints. */
15748 if (!user_breakpoint_p (tp))
15749 continue;
15750
15751 /* If we have a filter, only save the breakpoints it accepts. */
15752 if (filter && !filter (tp))
15753 continue;
15754
15755 any = 1;
15756
15757 if (is_tracepoint (tp))
15758 {
15759 extra_trace_bits = 1;
15760
15761 /* We can stop searching. */
15762 break;
15763 }
15764 }
15765
15766 if (!any)
15767 {
15768 warning (_("Nothing to save."));
15769 return;
15770 }
15771
15772 filename = tilde_expand (filename);
15773 cleanup = make_cleanup (xfree, filename);
15774 fp = gdb_fopen (filename, "w");
15775 if (!fp)
15776 error (_("Unable to open file '%s' for saving (%s)"),
15777 filename, safe_strerror (errno));
15778 make_cleanup_ui_file_delete (fp);
15779
15780 if (extra_trace_bits)
15781 save_trace_state_variables (fp);
15782
15783 ALL_BREAKPOINTS (tp)
15784 {
15785 /* Skip internal and momentary breakpoints. */
15786 if (!user_breakpoint_p (tp))
15787 continue;
15788
15789 /* If we have a filter, only save the breakpoints it accepts. */
15790 if (filter && !filter (tp))
15791 continue;
15792
15793 tp->ops->print_recreate (tp, fp);
15794
15795 /* Note, we can't rely on tp->number for anything, as we can't
15796 assume the recreated breakpoint numbers will match. Use $bpnum
15797 instead. */
15798
15799 if (tp->cond_string)
15800 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15801
15802 if (tp->ignore_count)
15803 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15804
15805 if (tp->type != bp_dprintf && tp->commands)
15806 {
15807 fprintf_unfiltered (fp, " commands\n");
15808
15809 ui_out_redirect (current_uiout, fp);
15810 TRY
15811 {
15812 print_command_lines (current_uiout, tp->commands->commands, 2);
15813 }
15814 CATCH (ex, RETURN_MASK_ALL)
15815 {
15816 ui_out_redirect (current_uiout, NULL);
15817 throw_exception (ex);
15818 }
15819 END_CATCH
15820
15821 ui_out_redirect (current_uiout, NULL);
15822 fprintf_unfiltered (fp, " end\n");
15823 }
15824
15825 if (tp->enable_state == bp_disabled)
15826 fprintf_unfiltered (fp, "disable $bpnum\n");
15827
15828 /* If this is a multi-location breakpoint, check if the locations
15829 should be individually disabled. Watchpoint locations are
15830 special, and not user visible. */
15831 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15832 {
15833 struct bp_location *loc;
15834 int n = 1;
15835
15836 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15837 if (!loc->enabled)
15838 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15839 }
15840 }
15841
15842 if (extra_trace_bits && *default_collect)
15843 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15844
15845 if (from_tty)
15846 printf_filtered (_("Saved to file '%s'.\n"), filename);
15847 do_cleanups (cleanup);
15848}
15849
15850/* The `save breakpoints' command. */
15851
15852static void
15853save_breakpoints_command (char *args, int from_tty)
15854{
15855 save_breakpoints (args, from_tty, NULL);
15856}
15857
15858/* The `save tracepoints' command. */
15859
15860static void
15861save_tracepoints_command (char *args, int from_tty)
15862{
15863 save_breakpoints (args, from_tty, is_tracepoint);
15864}
15865
15866/* Create a vector of all tracepoints. */
15867
15868VEC(breakpoint_p) *
15869all_tracepoints (void)
15870{
15871 VEC(breakpoint_p) *tp_vec = 0;
15872 struct breakpoint *tp;
15873
15874 ALL_TRACEPOINTS (tp)
15875 {
15876 VEC_safe_push (breakpoint_p, tp_vec, tp);
15877 }
15878
15879 return tp_vec;
15880}
15881
15882\f
15883/* This help string is used to consolidate all the help string for specifying
15884 locations used by several commands. */
15885
15886#define LOCATION_HELP_STRING \
15887"Linespecs are colon-separated lists of location parameters, such as\n\
15888source filename, function name, label name, and line number.\n\
15889Example: To specify the start of a label named \"the_top\" in the\n\
15890function \"fact\" in the file \"factorial.c\", use\n\
15891\"factorial.c:fact:the_top\".\n\
15892\n\
15893Address locations begin with \"*\" and specify an exact address in the\n\
15894program. Example: To specify the fourth byte past the start function\n\
15895\"main\", use \"*main + 4\".\n\
15896\n\
15897Explicit locations are similar to linespecs but use an option/argument\n\
15898syntax to specify location parameters.\n\
15899Example: To specify the start of the label named \"the_top\" in the\n\
15900function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15901-function fact -label the_top\".\n"
15902
15903/* This help string is used for the break, hbreak, tbreak and thbreak
15904 commands. It is defined as a macro to prevent duplication.
15905 COMMAND should be a string constant containing the name of the
15906 command. */
15907
15908#define BREAK_ARGS_HELP(command) \
15909command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15910PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15911probe point. Accepted values are `-probe' (for a generic, automatically\n\
15912guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15913`-probe-dtrace' (for a DTrace probe).\n\
15914LOCATION may be a linespec, address, or explicit location as described\n\
15915below.\n\
15916\n\
15917With no LOCATION, uses current execution address of the selected\n\
15918stack frame. This is useful for breaking on return to a stack frame.\n\
15919\n\
15920THREADNUM is the number from \"info threads\".\n\
15921CONDITION is a boolean expression.\n\
15922\n" LOCATION_HELP_STRING "\n\
15923Multiple breakpoints at one place are permitted, and useful if their\n\
15924conditions are different.\n\
15925\n\
15926Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15927
15928/* List of subcommands for "catch". */
15929static struct cmd_list_element *catch_cmdlist;
15930
15931/* List of subcommands for "tcatch". */
15932static struct cmd_list_element *tcatch_cmdlist;
15933
15934void
15935add_catch_command (char *name, char *docstring,
15936 cmd_sfunc_ftype *sfunc,
15937 completer_ftype *completer,
15938 void *user_data_catch,
15939 void *user_data_tcatch)
15940{
15941 struct cmd_list_element *command;
15942
15943 command = add_cmd (name, class_breakpoint, NULL, docstring,
15944 &catch_cmdlist);
15945 set_cmd_sfunc (command, sfunc);
15946 set_cmd_context (command, user_data_catch);
15947 set_cmd_completer (command, completer);
15948
15949 command = add_cmd (name, class_breakpoint, NULL, docstring,
15950 &tcatch_cmdlist);
15951 set_cmd_sfunc (command, sfunc);
15952 set_cmd_context (command, user_data_tcatch);
15953 set_cmd_completer (command, completer);
15954}
15955
15956static void
15957save_command (char *arg, int from_tty)
15958{
15959 printf_unfiltered (_("\"save\" must be followed by "
15960 "the name of a save subcommand.\n"));
15961 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15962}
15963
15964struct breakpoint *
15965iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15966 void *data)
15967{
15968 struct breakpoint *b, *b_tmp;
15969
15970 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15971 {
15972 if ((*callback) (b, data))
15973 return b;
15974 }
15975
15976 return NULL;
15977}
15978
15979/* Zero if any of the breakpoint's locations could be a location where
15980 functions have been inlined, nonzero otherwise. */
15981
15982static int
15983is_non_inline_function (struct breakpoint *b)
15984{
15985 /* The shared library event breakpoint is set on the address of a
15986 non-inline function. */
15987 if (b->type == bp_shlib_event)
15988 return 1;
15989
15990 return 0;
15991}
15992
15993/* Nonzero if the specified PC cannot be a location where functions
15994 have been inlined. */
15995
15996int
15997pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15998 const struct target_waitstatus *ws)
15999{
16000 struct breakpoint *b;
16001 struct bp_location *bl;
16002
16003 ALL_BREAKPOINTS (b)
16004 {
16005 if (!is_non_inline_function (b))
16006 continue;
16007
16008 for (bl = b->loc; bl != NULL; bl = bl->next)
16009 {
16010 if (!bl->shlib_disabled
16011 && bpstat_check_location (bl, aspace, pc, ws))
16012 return 1;
16013 }
16014 }
16015
16016 return 0;
16017}
16018
16019/* Remove any references to OBJFILE which is going to be freed. */
16020
16021void
16022breakpoint_free_objfile (struct objfile *objfile)
16023{
16024 struct bp_location **locp, *loc;
16025
16026 ALL_BP_LOCATIONS (loc, locp)
16027 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
16028 loc->symtab = NULL;
16029}
16030
16031void
16032initialize_breakpoint_ops (void)
16033{
16034 static int initialized = 0;
16035
16036 struct breakpoint_ops *ops;
16037
16038 if (initialized)
16039 return;
16040 initialized = 1;
16041
16042 /* The breakpoint_ops structure to be inherit by all kinds of
16043 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16044 internal and momentary breakpoints, etc.). */
16045 ops = &bkpt_base_breakpoint_ops;
16046 *ops = base_breakpoint_ops;
16047 ops->re_set = bkpt_re_set;
16048 ops->insert_location = bkpt_insert_location;
16049 ops->remove_location = bkpt_remove_location;
16050 ops->breakpoint_hit = bkpt_breakpoint_hit;
16051 ops->create_sals_from_location = bkpt_create_sals_from_location;
16052 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16053 ops->decode_location = bkpt_decode_location;
16054
16055 /* The breakpoint_ops structure to be used in regular breakpoints. */
16056 ops = &bkpt_breakpoint_ops;
16057 *ops = bkpt_base_breakpoint_ops;
16058 ops->re_set = bkpt_re_set;
16059 ops->resources_needed = bkpt_resources_needed;
16060 ops->print_it = bkpt_print_it;
16061 ops->print_mention = bkpt_print_mention;
16062 ops->print_recreate = bkpt_print_recreate;
16063
16064 /* Ranged breakpoints. */
16065 ops = &ranged_breakpoint_ops;
16066 *ops = bkpt_breakpoint_ops;
16067 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16068 ops->resources_needed = resources_needed_ranged_breakpoint;
16069 ops->print_it = print_it_ranged_breakpoint;
16070 ops->print_one = print_one_ranged_breakpoint;
16071 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16072 ops->print_mention = print_mention_ranged_breakpoint;
16073 ops->print_recreate = print_recreate_ranged_breakpoint;
16074
16075 /* Internal breakpoints. */
16076 ops = &internal_breakpoint_ops;
16077 *ops = bkpt_base_breakpoint_ops;
16078 ops->re_set = internal_bkpt_re_set;
16079 ops->check_status = internal_bkpt_check_status;
16080 ops->print_it = internal_bkpt_print_it;
16081 ops->print_mention = internal_bkpt_print_mention;
16082
16083 /* Momentary breakpoints. */
16084 ops = &momentary_breakpoint_ops;
16085 *ops = bkpt_base_breakpoint_ops;
16086 ops->re_set = momentary_bkpt_re_set;
16087 ops->check_status = momentary_bkpt_check_status;
16088 ops->print_it = momentary_bkpt_print_it;
16089 ops->print_mention = momentary_bkpt_print_mention;
16090
16091 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16092 ops = &longjmp_breakpoint_ops;
16093 *ops = momentary_breakpoint_ops;
16094 ops->dtor = longjmp_bkpt_dtor;
16095
16096 /* Probe breakpoints. */
16097 ops = &bkpt_probe_breakpoint_ops;
16098 *ops = bkpt_breakpoint_ops;
16099 ops->insert_location = bkpt_probe_insert_location;
16100 ops->remove_location = bkpt_probe_remove_location;
16101 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16102 ops->decode_location = bkpt_probe_decode_location;
16103
16104 /* Watchpoints. */
16105 ops = &watchpoint_breakpoint_ops;
16106 *ops = base_breakpoint_ops;
16107 ops->dtor = dtor_watchpoint;
16108 ops->re_set = re_set_watchpoint;
16109 ops->insert_location = insert_watchpoint;
16110 ops->remove_location = remove_watchpoint;
16111 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16112 ops->check_status = check_status_watchpoint;
16113 ops->resources_needed = resources_needed_watchpoint;
16114 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16115 ops->print_it = print_it_watchpoint;
16116 ops->print_mention = print_mention_watchpoint;
16117 ops->print_recreate = print_recreate_watchpoint;
16118 ops->explains_signal = explains_signal_watchpoint;
16119
16120 /* Masked watchpoints. */
16121 ops = &masked_watchpoint_breakpoint_ops;
16122 *ops = watchpoint_breakpoint_ops;
16123 ops->insert_location = insert_masked_watchpoint;
16124 ops->remove_location = remove_masked_watchpoint;
16125 ops->resources_needed = resources_needed_masked_watchpoint;
16126 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16127 ops->print_it = print_it_masked_watchpoint;
16128 ops->print_one_detail = print_one_detail_masked_watchpoint;
16129 ops->print_mention = print_mention_masked_watchpoint;
16130 ops->print_recreate = print_recreate_masked_watchpoint;
16131
16132 /* Tracepoints. */
16133 ops = &tracepoint_breakpoint_ops;
16134 *ops = base_breakpoint_ops;
16135 ops->re_set = tracepoint_re_set;
16136 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16137 ops->print_one_detail = tracepoint_print_one_detail;
16138 ops->print_mention = tracepoint_print_mention;
16139 ops->print_recreate = tracepoint_print_recreate;
16140 ops->create_sals_from_location = tracepoint_create_sals_from_location;
16141 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16142 ops->decode_location = tracepoint_decode_location;
16143
16144 /* Probe tracepoints. */
16145 ops = &tracepoint_probe_breakpoint_ops;
16146 *ops = tracepoint_breakpoint_ops;
16147 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16148 ops->decode_location = tracepoint_probe_decode_location;
16149
16150 /* Static tracepoints with marker (`-m'). */
16151 ops = &strace_marker_breakpoint_ops;
16152 *ops = tracepoint_breakpoint_ops;
16153 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16154 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16155 ops->decode_location = strace_marker_decode_location;
16156
16157 /* Fork catchpoints. */
16158 ops = &catch_fork_breakpoint_ops;
16159 *ops = base_breakpoint_ops;
16160 ops->insert_location = insert_catch_fork;
16161 ops->remove_location = remove_catch_fork;
16162 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16163 ops->print_it = print_it_catch_fork;
16164 ops->print_one = print_one_catch_fork;
16165 ops->print_mention = print_mention_catch_fork;
16166 ops->print_recreate = print_recreate_catch_fork;
16167
16168 /* Vfork catchpoints. */
16169 ops = &catch_vfork_breakpoint_ops;
16170 *ops = base_breakpoint_ops;
16171 ops->insert_location = insert_catch_vfork;
16172 ops->remove_location = remove_catch_vfork;
16173 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16174 ops->print_it = print_it_catch_vfork;
16175 ops->print_one = print_one_catch_vfork;
16176 ops->print_mention = print_mention_catch_vfork;
16177 ops->print_recreate = print_recreate_catch_vfork;
16178
16179 /* Exec catchpoints. */
16180 ops = &catch_exec_breakpoint_ops;
16181 *ops = base_breakpoint_ops;
16182 ops->dtor = dtor_catch_exec;
16183 ops->insert_location = insert_catch_exec;
16184 ops->remove_location = remove_catch_exec;
16185 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16186 ops->print_it = print_it_catch_exec;
16187 ops->print_one = print_one_catch_exec;
16188 ops->print_mention = print_mention_catch_exec;
16189 ops->print_recreate = print_recreate_catch_exec;
16190
16191 /* Solib-related catchpoints. */
16192 ops = &catch_solib_breakpoint_ops;
16193 *ops = base_breakpoint_ops;
16194 ops->dtor = dtor_catch_solib;
16195 ops->insert_location = insert_catch_solib;
16196 ops->remove_location = remove_catch_solib;
16197 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16198 ops->check_status = check_status_catch_solib;
16199 ops->print_it = print_it_catch_solib;
16200 ops->print_one = print_one_catch_solib;
16201 ops->print_mention = print_mention_catch_solib;
16202 ops->print_recreate = print_recreate_catch_solib;
16203
16204 ops = &dprintf_breakpoint_ops;
16205 *ops = bkpt_base_breakpoint_ops;
16206 ops->re_set = dprintf_re_set;
16207 ops->resources_needed = bkpt_resources_needed;
16208 ops->print_it = bkpt_print_it;
16209 ops->print_mention = bkpt_print_mention;
16210 ops->print_recreate = dprintf_print_recreate;
16211 ops->after_condition_true = dprintf_after_condition_true;
16212 ops->breakpoint_hit = dprintf_breakpoint_hit;
16213}
16214
16215/* Chain containing all defined "enable breakpoint" subcommands. */
16216
16217static struct cmd_list_element *enablebreaklist = NULL;
16218
16219void
16220_initialize_breakpoint (void)
16221{
16222 struct cmd_list_element *c;
16223
16224 initialize_breakpoint_ops ();
16225
16226 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16227 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16228 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16229
16230 breakpoint_objfile_key
16231 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16232
16233 breakpoint_chain = 0;
16234 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16235 before a breakpoint is set. */
16236 breakpoint_count = 0;
16237
16238 tracepoint_count = 0;
16239
16240 add_com ("ignore", class_breakpoint, ignore_command, _("\
16241Set ignore-count of breakpoint number N to COUNT.\n\
16242Usage is `ignore N COUNT'."));
16243
16244 add_com ("commands", class_breakpoint, commands_command, _("\
16245Set commands to be executed when a breakpoint is hit.\n\
16246Give breakpoint number as argument after \"commands\".\n\
16247With no argument, the targeted breakpoint is the last one set.\n\
16248The commands themselves follow starting on the next line.\n\
16249Type a line containing \"end\" to indicate the end of them.\n\
16250Give \"silent\" as the first line to make the breakpoint silent;\n\
16251then no output is printed when it is hit, except what the commands print."));
16252
16253 c = add_com ("condition", class_breakpoint, condition_command, _("\
16254Specify breakpoint number N to break only if COND is true.\n\
16255Usage is `condition N COND', where N is an integer and COND is an\n\
16256expression to be evaluated whenever breakpoint N is reached."));
16257 set_cmd_completer (c, condition_completer);
16258
16259 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16260Set a temporary breakpoint.\n\
16261Like \"break\" except the breakpoint is only temporary,\n\
16262so it will be deleted when hit. Equivalent to \"break\" followed\n\
16263by using \"enable delete\" on the breakpoint number.\n\
16264\n"
16265BREAK_ARGS_HELP ("tbreak")));
16266 set_cmd_completer (c, location_completer);
16267
16268 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16269Set a hardware assisted breakpoint.\n\
16270Like \"break\" except the breakpoint requires hardware support,\n\
16271some target hardware may not have this support.\n\
16272\n"
16273BREAK_ARGS_HELP ("hbreak")));
16274 set_cmd_completer (c, location_completer);
16275
16276 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16277Set a temporary hardware assisted breakpoint.\n\
16278Like \"hbreak\" except the breakpoint is only temporary,\n\
16279so it will be deleted when hit.\n\
16280\n"
16281BREAK_ARGS_HELP ("thbreak")));
16282 set_cmd_completer (c, location_completer);
16283
16284 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16285Enable some breakpoints.\n\
16286Give breakpoint numbers (separated by spaces) as arguments.\n\
16287With no subcommand, breakpoints are enabled until you command otherwise.\n\
16288This is used to cancel the effect of the \"disable\" command.\n\
16289With a subcommand you can enable temporarily."),
16290 &enablelist, "enable ", 1, &cmdlist);
16291
16292 add_com_alias ("en", "enable", class_breakpoint, 1);
16293
16294 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16295Enable some breakpoints.\n\
16296Give breakpoint numbers (separated by spaces) as arguments.\n\
16297This is used to cancel the effect of the \"disable\" command.\n\
16298May be abbreviated to simply \"enable\".\n"),
16299 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16300
16301 add_cmd ("once", no_class, enable_once_command, _("\
16302Enable breakpoints for one hit. Give breakpoint numbers.\n\
16303If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16304 &enablebreaklist);
16305
16306 add_cmd ("delete", no_class, enable_delete_command, _("\
16307Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16308If a breakpoint is hit while enabled in this fashion, it is deleted."),
16309 &enablebreaklist);
16310
16311 add_cmd ("count", no_class, enable_count_command, _("\
16312Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16313If a breakpoint is hit while enabled in this fashion,\n\
16314the count is decremented; when it reaches zero, the breakpoint is disabled."),
16315 &enablebreaklist);
16316
16317 add_cmd ("delete", no_class, enable_delete_command, _("\
16318Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16319If a breakpoint is hit while enabled in this fashion, it is deleted."),
16320 &enablelist);
16321
16322 add_cmd ("once", no_class, enable_once_command, _("\
16323Enable breakpoints for one hit. Give breakpoint numbers.\n\
16324If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16325 &enablelist);
16326
16327 add_cmd ("count", no_class, enable_count_command, _("\
16328Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16329If a breakpoint is hit while enabled in this fashion,\n\
16330the count is decremented; when it reaches zero, the breakpoint is disabled."),
16331 &enablelist);
16332
16333 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16334Disable some breakpoints.\n\
16335Arguments are breakpoint numbers with spaces in between.\n\
16336To disable all breakpoints, give no argument.\n\
16337A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16338 &disablelist, "disable ", 1, &cmdlist);
16339 add_com_alias ("dis", "disable", class_breakpoint, 1);
16340 add_com_alias ("disa", "disable", class_breakpoint, 1);
16341
16342 add_cmd ("breakpoints", class_alias, disable_command, _("\
16343Disable some breakpoints.\n\
16344Arguments are breakpoint numbers with spaces in between.\n\
16345To disable all breakpoints, give no argument.\n\
16346A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16347This command may be abbreviated \"disable\"."),
16348 &disablelist);
16349
16350 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16351Delete some breakpoints or auto-display expressions.\n\
16352Arguments are breakpoint numbers with spaces in between.\n\
16353To delete all breakpoints, give no argument.\n\
16354\n\
16355Also a prefix command for deletion of other GDB objects.\n\
16356The \"unset\" command is also an alias for \"delete\"."),
16357 &deletelist, "delete ", 1, &cmdlist);
16358 add_com_alias ("d", "delete", class_breakpoint, 1);
16359 add_com_alias ("del", "delete", class_breakpoint, 1);
16360
16361 add_cmd ("breakpoints", class_alias, delete_command, _("\
16362Delete some breakpoints or auto-display expressions.\n\
16363Arguments are breakpoint numbers with spaces in between.\n\
16364To delete all breakpoints, give no argument.\n\
16365This command may be abbreviated \"delete\"."),
16366 &deletelist);
16367
16368 add_com ("clear", class_breakpoint, clear_command, _("\
16369Clear breakpoint at specified location.\n\
16370Argument may be a linespec, explicit, or address location as described below.\n\
16371\n\
16372With no argument, clears all breakpoints in the line that the selected frame\n\
16373is executing in.\n"
16374"\n" LOCATION_HELP_STRING "\n\
16375See also the \"delete\" command which clears breakpoints by number."));
16376 add_com_alias ("cl", "clear", class_breakpoint, 1);
16377
16378 c = add_com ("break", class_breakpoint, break_command, _("\
16379Set breakpoint at specified location.\n"
16380BREAK_ARGS_HELP ("break")));
16381 set_cmd_completer (c, location_completer);
16382
16383 add_com_alias ("b", "break", class_run, 1);
16384 add_com_alias ("br", "break", class_run, 1);
16385 add_com_alias ("bre", "break", class_run, 1);
16386 add_com_alias ("brea", "break", class_run, 1);
16387
16388 if (dbx_commands)
16389 {
16390 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16391Break in function/address or break at a line in the current file."),
16392 &stoplist, "stop ", 1, &cmdlist);
16393 add_cmd ("in", class_breakpoint, stopin_command,
16394 _("Break in function or address."), &stoplist);
16395 add_cmd ("at", class_breakpoint, stopat_command,
16396 _("Break at a line in the current file."), &stoplist);
16397 add_com ("status", class_info, breakpoints_info, _("\
16398Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16399The \"Type\" column indicates one of:\n\
16400\tbreakpoint - normal breakpoint\n\
16401\twatchpoint - watchpoint\n\
16402The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16403the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16404breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16405address and file/line number respectively.\n\
16406\n\
16407Convenience variable \"$_\" and default examine address for \"x\"\n\
16408are set to the address of the last breakpoint listed unless the command\n\
16409is prefixed with \"server \".\n\n\
16410Convenience variable \"$bpnum\" contains the number of the last\n\
16411breakpoint set."));
16412 }
16413
16414 add_info ("breakpoints", breakpoints_info, _("\
16415Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16416The \"Type\" column indicates one of:\n\
16417\tbreakpoint - normal breakpoint\n\
16418\twatchpoint - watchpoint\n\
16419The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16420the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16421breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16422address and file/line number respectively.\n\
16423\n\
16424Convenience variable \"$_\" and default examine address for \"x\"\n\
16425are set to the address of the last breakpoint listed unless the command\n\
16426is prefixed with \"server \".\n\n\
16427Convenience variable \"$bpnum\" contains the number of the last\n\
16428breakpoint set."));
16429
16430 add_info_alias ("b", "breakpoints", 1);
16431
16432 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16433Status of all breakpoints, or breakpoint number NUMBER.\n\
16434The \"Type\" column indicates one of:\n\
16435\tbreakpoint - normal breakpoint\n\
16436\twatchpoint - watchpoint\n\
16437\tlongjmp - internal breakpoint used to step through longjmp()\n\
16438\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16439\tuntil - internal breakpoint used by the \"until\" command\n\
16440\tfinish - internal breakpoint used by the \"finish\" command\n\
16441The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16442the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16443breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16444address and file/line number respectively.\n\
16445\n\
16446Convenience variable \"$_\" and default examine address for \"x\"\n\
16447are set to the address of the last breakpoint listed unless the command\n\
16448is prefixed with \"server \".\n\n\
16449Convenience variable \"$bpnum\" contains the number of the last\n\
16450breakpoint set."),
16451 &maintenanceinfolist);
16452
16453 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16454Set catchpoints to catch events."),
16455 &catch_cmdlist, "catch ",
16456 0/*allow-unknown*/, &cmdlist);
16457
16458 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16459Set temporary catchpoints to catch events."),
16460 &tcatch_cmdlist, "tcatch ",
16461 0/*allow-unknown*/, &cmdlist);
16462
16463 add_catch_command ("fork", _("Catch calls to fork."),
16464 catch_fork_command_1,
16465 NULL,
16466 (void *) (uintptr_t) catch_fork_permanent,
16467 (void *) (uintptr_t) catch_fork_temporary);
16468 add_catch_command ("vfork", _("Catch calls to vfork."),
16469 catch_fork_command_1,
16470 NULL,
16471 (void *) (uintptr_t) catch_vfork_permanent,
16472 (void *) (uintptr_t) catch_vfork_temporary);
16473 add_catch_command ("exec", _("Catch calls to exec."),
16474 catch_exec_command_1,
16475 NULL,
16476 CATCH_PERMANENT,
16477 CATCH_TEMPORARY);
16478 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16479Usage: catch load [REGEX]\n\
16480If REGEX is given, only stop for libraries matching the regular expression."),
16481 catch_load_command_1,
16482 NULL,
16483 CATCH_PERMANENT,
16484 CATCH_TEMPORARY);
16485 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16486Usage: catch unload [REGEX]\n\
16487If REGEX is given, only stop for libraries matching the regular expression."),
16488 catch_unload_command_1,
16489 NULL,
16490 CATCH_PERMANENT,
16491 CATCH_TEMPORARY);
16492
16493 c = add_com ("watch", class_breakpoint, watch_command, _("\
16494Set a watchpoint for an expression.\n\
16495Usage: watch [-l|-location] EXPRESSION\n\
16496A watchpoint stops execution of your program whenever the value of\n\
16497an expression changes.\n\
16498If -l or -location is given, this evaluates EXPRESSION and watches\n\
16499the memory to which it refers."));
16500 set_cmd_completer (c, expression_completer);
16501
16502 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16503Set a read watchpoint for an expression.\n\
16504Usage: rwatch [-l|-location] EXPRESSION\n\
16505A watchpoint stops execution of your program whenever the value of\n\
16506an expression is read.\n\
16507If -l or -location is given, this evaluates EXPRESSION and watches\n\
16508the memory to which it refers."));
16509 set_cmd_completer (c, expression_completer);
16510
16511 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16512Set a watchpoint for an expression.\n\
16513Usage: awatch [-l|-location] EXPRESSION\n\
16514A watchpoint stops execution of your program whenever the value of\n\
16515an expression is either read or written.\n\
16516If -l or -location is given, this evaluates EXPRESSION and watches\n\
16517the memory to which it refers."));
16518 set_cmd_completer (c, expression_completer);
16519
16520 add_info ("watchpoints", watchpoints_info, _("\
16521Status of specified watchpoints (all watchpoints if no argument)."));
16522
16523 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16524 respond to changes - contrary to the description. */
16525 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16526 &can_use_hw_watchpoints, _("\
16527Set debugger's willingness to use watchpoint hardware."), _("\
16528Show debugger's willingness to use watchpoint hardware."), _("\
16529If zero, gdb will not use hardware for new watchpoints, even if\n\
16530such is available. (However, any hardware watchpoints that were\n\
16531created before setting this to nonzero, will continue to use watchpoint\n\
16532hardware.)"),
16533 NULL,
16534 show_can_use_hw_watchpoints,
16535 &setlist, &showlist);
16536
16537 can_use_hw_watchpoints = 1;
16538
16539 /* Tracepoint manipulation commands. */
16540
16541 c = add_com ("trace", class_breakpoint, trace_command, _("\
16542Set a tracepoint at specified location.\n\
16543\n"
16544BREAK_ARGS_HELP ("trace") "\n\
16545Do \"help tracepoints\" for info on other tracepoint commands."));
16546 set_cmd_completer (c, location_completer);
16547
16548 add_com_alias ("tp", "trace", class_alias, 0);
16549 add_com_alias ("tr", "trace", class_alias, 1);
16550 add_com_alias ("tra", "trace", class_alias, 1);
16551 add_com_alias ("trac", "trace", class_alias, 1);
16552
16553 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16554Set a fast tracepoint at specified location.\n\
16555\n"
16556BREAK_ARGS_HELP ("ftrace") "\n\
16557Do \"help tracepoints\" for info on other tracepoint commands."));
16558 set_cmd_completer (c, location_completer);
16559
16560 c = add_com ("strace", class_breakpoint, strace_command, _("\
16561Set a static tracepoint at location or marker.\n\
16562\n\
16563strace [LOCATION] [if CONDITION]\n\
16564LOCATION may be a linespec, explicit, or address location (described below) \n\
16565or -m MARKER_ID.\n\n\
16566If a marker id is specified, probe the marker with that name. With\n\
16567no LOCATION, uses current execution address of the selected stack frame.\n\
16568Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16569This collects arbitrary user data passed in the probe point call to the\n\
16570tracing library. You can inspect it when analyzing the trace buffer,\n\
16571by printing the $_sdata variable like any other convenience variable.\n\
16572\n\
16573CONDITION is a boolean expression.\n\
16574\n" LOCATION_HELP_STRING "\n\
16575Multiple tracepoints at one place are permitted, and useful if their\n\
16576conditions are different.\n\
16577\n\
16578Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16579Do \"help tracepoints\" for info on other tracepoint commands."));
16580 set_cmd_completer (c, location_completer);
16581
16582 add_info ("tracepoints", tracepoints_info, _("\
16583Status of specified tracepoints (all tracepoints if no argument).\n\
16584Convenience variable \"$tpnum\" contains the number of the\n\
16585last tracepoint set."));
16586
16587 add_info_alias ("tp", "tracepoints", 1);
16588
16589 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16590Delete specified tracepoints.\n\
16591Arguments are tracepoint numbers, separated by spaces.\n\
16592No argument means delete all tracepoints."),
16593 &deletelist);
16594 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16595
16596 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16597Disable specified tracepoints.\n\
16598Arguments are tracepoint numbers, separated by spaces.\n\
16599No argument means disable all tracepoints."),
16600 &disablelist);
16601 deprecate_cmd (c, "disable");
16602
16603 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16604Enable specified tracepoints.\n\
16605Arguments are tracepoint numbers, separated by spaces.\n\
16606No argument means enable all tracepoints."),
16607 &enablelist);
16608 deprecate_cmd (c, "enable");
16609
16610 add_com ("passcount", class_trace, trace_pass_command, _("\
16611Set the passcount for a tracepoint.\n\
16612The trace will end when the tracepoint has been passed 'count' times.\n\
16613Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16614if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16615
16616 add_prefix_cmd ("save", class_breakpoint, save_command,
16617 _("Save breakpoint definitions as a script."),
16618 &save_cmdlist, "save ",
16619 0/*allow-unknown*/, &cmdlist);
16620
16621 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16622Save current breakpoint definitions as a script.\n\
16623This includes all types of breakpoints (breakpoints, watchpoints,\n\
16624catchpoints, tracepoints). Use the 'source' command in another debug\n\
16625session to restore them."),
16626 &save_cmdlist);
16627 set_cmd_completer (c, filename_completer);
16628
16629 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16630Save current tracepoint definitions as a script.\n\
16631Use the 'source' command in another debug session to restore them."),
16632 &save_cmdlist);
16633 set_cmd_completer (c, filename_completer);
16634
16635 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16636 deprecate_cmd (c, "save tracepoints");
16637
16638 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16639Breakpoint specific settings\n\
16640Configure various breakpoint-specific variables such as\n\
16641pending breakpoint behavior"),
16642 &breakpoint_set_cmdlist, "set breakpoint ",
16643 0/*allow-unknown*/, &setlist);
16644 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16645Breakpoint specific settings\n\
16646Configure various breakpoint-specific variables such as\n\
16647pending breakpoint behavior"),
16648 &breakpoint_show_cmdlist, "show breakpoint ",
16649 0/*allow-unknown*/, &showlist);
16650
16651 add_setshow_auto_boolean_cmd ("pending", no_class,
16652 &pending_break_support, _("\
16653Set debugger's behavior regarding pending breakpoints."), _("\
16654Show debugger's behavior regarding pending breakpoints."), _("\
16655If on, an unrecognized breakpoint location will cause gdb to create a\n\
16656pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16657an error. If auto, an unrecognized breakpoint location results in a\n\
16658user-query to see if a pending breakpoint should be created."),
16659 NULL,
16660 show_pending_break_support,
16661 &breakpoint_set_cmdlist,
16662 &breakpoint_show_cmdlist);
16663
16664 pending_break_support = AUTO_BOOLEAN_AUTO;
16665
16666 add_setshow_boolean_cmd ("auto-hw", no_class,
16667 &automatic_hardware_breakpoints, _("\
16668Set automatic usage of hardware breakpoints."), _("\
16669Show automatic usage of hardware breakpoints."), _("\
16670If set, the debugger will automatically use hardware breakpoints for\n\
16671breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16672a warning will be emitted for such breakpoints."),
16673 NULL,
16674 show_automatic_hardware_breakpoints,
16675 &breakpoint_set_cmdlist,
16676 &breakpoint_show_cmdlist);
16677
16678 add_setshow_boolean_cmd ("always-inserted", class_support,
16679 &always_inserted_mode, _("\
16680Set mode for inserting breakpoints."), _("\
16681Show mode for inserting breakpoints."), _("\
16682When this mode is on, breakpoints are inserted immediately as soon as\n\
16683they're created, kept inserted even when execution stops, and removed\n\
16684only when the user deletes them. When this mode is off (the default),\n\
16685breakpoints are inserted only when execution continues, and removed\n\
16686when execution stops."),
16687 NULL,
16688 &show_always_inserted_mode,
16689 &breakpoint_set_cmdlist,
16690 &breakpoint_show_cmdlist);
16691
16692 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16693 condition_evaluation_enums,
16694 &condition_evaluation_mode_1, _("\
16695Set mode of breakpoint condition evaluation."), _("\
16696Show mode of breakpoint condition evaluation."), _("\
16697When this is set to \"host\", breakpoint conditions will be\n\
16698evaluated on the host's side by GDB. When it is set to \"target\",\n\
16699breakpoint conditions will be downloaded to the target (if the target\n\
16700supports such feature) and conditions will be evaluated on the target's side.\n\
16701If this is set to \"auto\" (default), this will be automatically set to\n\
16702\"target\" if it supports condition evaluation, otherwise it will\n\
16703be set to \"gdb\""),
16704 &set_condition_evaluation_mode,
16705 &show_condition_evaluation_mode,
16706 &breakpoint_set_cmdlist,
16707 &breakpoint_show_cmdlist);
16708
16709 add_com ("break-range", class_breakpoint, break_range_command, _("\
16710Set a breakpoint for an address range.\n\
16711break-range START-LOCATION, END-LOCATION\n\
16712where START-LOCATION and END-LOCATION can be one of the following:\n\
16713 LINENUM, for that line in the current file,\n\
16714 FILE:LINENUM, for that line in that file,\n\
16715 +OFFSET, for that number of lines after the current line\n\
16716 or the start of the range\n\
16717 FUNCTION, for the first line in that function,\n\
16718 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16719 *ADDRESS, for the instruction at that address.\n\
16720\n\
16721The breakpoint will stop execution of the inferior whenever it executes\n\
16722an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16723range (including START-LOCATION and END-LOCATION)."));
16724
16725 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16726Set a dynamic printf at specified location.\n\
16727dprintf location,format string,arg1,arg2,...\n\
16728location may be a linespec, explicit, or address location.\n"
16729"\n" LOCATION_HELP_STRING));
16730 set_cmd_completer (c, location_completer);
16731
16732 add_setshow_enum_cmd ("dprintf-style", class_support,
16733 dprintf_style_enums, &dprintf_style, _("\
16734Set the style of usage for dynamic printf."), _("\
16735Show the style of usage for dynamic printf."), _("\
16736This setting chooses how GDB will do a dynamic printf.\n\
16737If the value is \"gdb\", then the printing is done by GDB to its own\n\
16738console, as with the \"printf\" command.\n\
16739If the value is \"call\", the print is done by calling a function in your\n\
16740program; by default printf(), but you can choose a different function or\n\
16741output stream by setting dprintf-function and dprintf-channel."),
16742 update_dprintf_commands, NULL,
16743 &setlist, &showlist);
16744
16745 dprintf_function = xstrdup ("printf");
16746 add_setshow_string_cmd ("dprintf-function", class_support,
16747 &dprintf_function, _("\
16748Set the function to use for dynamic printf"), _("\
16749Show the function to use for dynamic printf"), NULL,
16750 update_dprintf_commands, NULL,
16751 &setlist, &showlist);
16752
16753 dprintf_channel = xstrdup ("");
16754 add_setshow_string_cmd ("dprintf-channel", class_support,
16755 &dprintf_channel, _("\
16756Set the channel to use for dynamic printf"), _("\
16757Show the channel to use for dynamic printf"), NULL,
16758 update_dprintf_commands, NULL,
16759 &setlist, &showlist);
16760
16761 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16762 &disconnected_dprintf, _("\
16763Set whether dprintf continues after GDB disconnects."), _("\
16764Show whether dprintf continues after GDB disconnects."), _("\
16765Use this to let dprintf commands continue to hit and produce output\n\
16766even if GDB disconnects or detaches from the target."),
16767 NULL,
16768 NULL,
16769 &setlist, &showlist);
16770
16771 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16772agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16773(target agent only) This is useful for formatted output in user-defined commands."));
16774
16775 automatic_hardware_breakpoints = 1;
16776
16777 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16778 observer_attach_thread_exit (remove_threaded_breakpoints);
16779}
This page took 0.089871 seconds and 4 git commands to generate.