add a default method for gdbarch_skip_permanent_breakpoint
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
... / ...
CommitLineData
1/* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2014 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 "xml-syscall.h"
60#include "parser-defs.h"
61#include "gdb_regex.h"
62#include "probe.h"
63#include "cli/cli-utils.h"
64#include "continuations.h"
65#include "stack.h"
66#include "skip.h"
67#include "ax-gdb.h"
68#include "dummy-frame.h"
69
70#include "format.h"
71
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
79#include "mi/mi-common.h"
80#include "extension.h"
81
82/* Enums for exception-handling support. */
83enum exception_event_kind
84{
85 EX_EVENT_THROW,
86 EX_EVENT_RETHROW,
87 EX_EVENT_CATCH
88};
89
90/* Prototypes for local functions. */
91
92static void enable_delete_command (char *, int);
93
94static void enable_once_command (char *, int);
95
96static void enable_count_command (char *, int);
97
98static void disable_command (char *, int);
99
100static void enable_command (char *, int);
101
102static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
105
106static void ignore_command (char *, int);
107
108static int breakpoint_re_set_one (void *);
109
110static void breakpoint_re_set_default (struct breakpoint *);
111
112static void create_sals_from_address_default (char **,
113 struct linespec_result *,
114 enum bptype, char *,
115 char **);
116
117static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
119 char *, char *, enum bptype,
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
123 int, int, int, unsigned);
124
125static void decode_linespec_default (struct breakpoint *, char **,
126 struct symtabs_and_lines *);
127
128static void clear_command (char *, int);
129
130static void catch_command (char *, int);
131
132static int can_use_hardware_watchpoint (struct value *);
133
134static void break_command_1 (char *, int, int);
135
136static void mention (struct breakpoint *);
137
138static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
139 enum bptype,
140 const struct breakpoint_ops *);
141static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
142 const struct symtab_and_line *);
143
144/* This function is used in gdbtk sources and thus can not be made
145 static. */
146struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
147 struct symtab_and_line,
148 enum bptype,
149 const struct breakpoint_ops *);
150
151static struct breakpoint *
152 momentary_breakpoint_from_master (struct breakpoint *orig,
153 enum bptype type,
154 const struct breakpoint_ops *ops,
155 int loc_enabled);
156
157static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
158
159static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
160 CORE_ADDR bpaddr,
161 enum bptype bptype);
162
163static void describe_other_breakpoints (struct gdbarch *,
164 struct program_space *, CORE_ADDR,
165 struct obj_section *, int);
166
167static int watchpoint_locations_match (struct bp_location *loc1,
168 struct bp_location *loc2);
169
170static int breakpoint_location_address_match (struct bp_location *bl,
171 struct address_space *aspace,
172 CORE_ADDR addr);
173
174static void breakpoints_info (char *, int);
175
176static void watchpoints_info (char *, int);
177
178static int breakpoint_1 (char *, int,
179 int (*) (const struct breakpoint *));
180
181static int breakpoint_cond_eval (void *);
182
183static void cleanup_executing_breakpoints (void *);
184
185static void commands_command (char *, int);
186
187static void condition_command (char *, int);
188
189typedef enum
190 {
191 mark_inserted,
192 mark_uninserted
193 }
194insertion_state_t;
195
196static int remove_breakpoint (struct bp_location *, insertion_state_t);
197static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
198
199static enum print_stop_action print_bp_stop_message (bpstat bs);
200
201static int watchpoint_check (void *);
202
203static void maintenance_info_breakpoints (char *, int);
204
205static int hw_breakpoint_used_count (void);
206
207static int hw_watchpoint_use_count (struct breakpoint *);
208
209static int hw_watchpoint_used_count_others (struct breakpoint *except,
210 enum bptype type,
211 int *other_type_used);
212
213static void hbreak_command (char *, int);
214
215static void thbreak_command (char *, int);
216
217static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
218 int count);
219
220static void stop_command (char *arg, int from_tty);
221
222static void stopin_command (char *arg, int from_tty);
223
224static void stopat_command (char *arg, int from_tty);
225
226static void tcatch_command (char *arg, int from_tty);
227
228static void free_bp_location (struct bp_location *loc);
229static void incref_bp_location (struct bp_location *loc);
230static void decref_bp_location (struct bp_location **loc);
231
232static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
233
234/* update_global_location_list's modes of operation wrt to whether to
235 insert locations now. */
236enum ugll_insert_mode
237{
238 /* Don't insert any breakpoint locations into the inferior, only
239 remove already-inserted locations that no longer should be
240 inserted. Functions that delete a breakpoint or breakpoints
241 should specify this mode, so that deleting a breakpoint doesn't
242 have the side effect of inserting the locations of other
243 breakpoints that are marked not-inserted, but should_be_inserted
244 returns true on them.
245
246 This behavior is useful is situations close to tear-down -- e.g.,
247 after an exec, while the target still has execution, but
248 breakpoint shadows of the previous executable image should *NOT*
249 be restored to the new image; or before detaching, where the
250 target still has execution and wants to delete breakpoints from
251 GDB's lists, and all breakpoints had already been removed from
252 the inferior. */
253 UGLL_DONT_INSERT,
254
255 /* May insert breakpoints iff breakpoints_should_be_inserted_now
256 claims breakpoints should be inserted now. */
257 UGLL_MAY_INSERT,
258
259 /* Insert locations now, irrespective of
260 breakpoints_should_be_inserted_now. E.g., say all threads are
261 stopped right now, and the user did "continue". We need to
262 insert breakpoints _before_ resuming the target, but
263 UGLL_MAY_INSERT wouldn't insert them, because
264 breakpoints_should_be_inserted_now returns false at that point,
265 as no thread is running yet. */
266 UGLL_INSERT
267};
268
269static void update_global_location_list (enum ugll_insert_mode);
270
271static void update_global_location_list_nothrow (enum ugll_insert_mode);
272
273static int is_hardware_watchpoint (const struct breakpoint *bpt);
274
275static void insert_breakpoint_locations (void);
276
277static int syscall_catchpoint_p (struct breakpoint *b);
278
279static void tracepoints_info (char *, int);
280
281static void delete_trace_command (char *, int);
282
283static void enable_trace_command (char *, int);
284
285static void disable_trace_command (char *, int);
286
287static void trace_pass_command (char *, int);
288
289static void set_tracepoint_count (int num);
290
291static int is_masked_watchpoint (const struct breakpoint *b);
292
293static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
295/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298static int strace_marker_p (struct breakpoint *b);
299
300/* The abstract base class all breakpoint_ops structures inherit
301 from. */
302struct breakpoint_ops base_breakpoint_ops;
303
304/* The breakpoint_ops structure to be inherited by all breakpoint_ops
305 that are implemented on top of software or hardware breakpoints
306 (user breakpoints, internal and momentary breakpoints, etc.). */
307static struct breakpoint_ops bkpt_base_breakpoint_ops;
308
309/* Internal breakpoints class type. */
310static struct breakpoint_ops internal_breakpoint_ops;
311
312/* Momentary breakpoints class type. */
313static struct breakpoint_ops momentary_breakpoint_ops;
314
315/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
316static struct breakpoint_ops longjmp_breakpoint_ops;
317
318/* The breakpoint_ops structure to be used in regular user created
319 breakpoints. */
320struct breakpoint_ops bkpt_breakpoint_ops;
321
322/* Breakpoints set on probes. */
323static struct breakpoint_ops bkpt_probe_breakpoint_ops;
324
325/* Dynamic printf class type. */
326struct breakpoint_ops dprintf_breakpoint_ops;
327
328/* The style in which to perform a dynamic printf. This is a user
329 option because different output options have different tradeoffs;
330 if GDB does the printing, there is better error handling if there
331 is a problem with any of the arguments, but using an inferior
332 function lets you have special-purpose printers and sending of
333 output to the same place as compiled-in print functions. */
334
335static const char dprintf_style_gdb[] = "gdb";
336static const char dprintf_style_call[] = "call";
337static const char dprintf_style_agent[] = "agent";
338static const char *const dprintf_style_enums[] = {
339 dprintf_style_gdb,
340 dprintf_style_call,
341 dprintf_style_agent,
342 NULL
343};
344static const char *dprintf_style = dprintf_style_gdb;
345
346/* The function to use for dynamic printf if the preferred style is to
347 call into the inferior. The value is simply a string that is
348 copied into the command, so it can be anything that GDB can
349 evaluate to a callable address, not necessarily a function name. */
350
351static char *dprintf_function = "";
352
353/* The channel to use for dynamic printf if the preferred style is to
354 call into the inferior; if a nonempty string, it will be passed to
355 the call as the first argument, with the format string as the
356 second. As with the dprintf function, this can be anything that
357 GDB knows how to evaluate, so in addition to common choices like
358 "stderr", this could be an app-specific expression like
359 "mystreams[curlogger]". */
360
361static char *dprintf_channel = "";
362
363/* True if dprintf commands should continue to operate even if GDB
364 has disconnected. */
365static int disconnected_dprintf = 1;
366
367/* A reference-counted struct command_line. This lets multiple
368 breakpoints share a single command list. */
369struct counted_command_line
370{
371 /* The reference count. */
372 int refc;
373
374 /* The command list. */
375 struct command_line *commands;
376};
377
378struct command_line *
379breakpoint_commands (struct breakpoint *b)
380{
381 return b->commands ? b->commands->commands : NULL;
382}
383
384/* Flag indicating that a command has proceeded the inferior past the
385 current breakpoint. */
386
387static int breakpoint_proceeded;
388
389const char *
390bpdisp_text (enum bpdisp disp)
391{
392 /* NOTE: the following values are a part of MI protocol and
393 represent values of 'disp' field returned when inferior stops at
394 a breakpoint. */
395 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
396
397 return bpdisps[(int) disp];
398}
399
400/* Prototypes for exported functions. */
401/* If FALSE, gdb will not use hardware support for watchpoints, even
402 if such is available. */
403static int can_use_hw_watchpoints;
404
405static void
406show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
407 struct cmd_list_element *c,
408 const char *value)
409{
410 fprintf_filtered (file,
411 _("Debugger's willingness to use "
412 "watchpoint hardware is %s.\n"),
413 value);
414}
415
416/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
417 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
418 for unrecognized breakpoint locations.
419 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
420static enum auto_boolean pending_break_support;
421static void
422show_pending_break_support (struct ui_file *file, int from_tty,
423 struct cmd_list_element *c,
424 const char *value)
425{
426 fprintf_filtered (file,
427 _("Debugger's behavior regarding "
428 "pending breakpoints is %s.\n"),
429 value);
430}
431
432/* If 1, gdb will automatically use hardware breakpoints for breakpoints
433 set with "break" but falling in read-only memory.
434 If 0, gdb will warn about such breakpoints, but won't automatically
435 use hardware breakpoints. */
436static int automatic_hardware_breakpoints;
437static void
438show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
439 struct cmd_list_element *c,
440 const char *value)
441{
442 fprintf_filtered (file,
443 _("Automatic usage of hardware breakpoints is %s.\n"),
444 value);
445}
446
447/* If on, GDB keeps breakpoints inserted even if the inferior is
448 stopped, and immediately inserts any new breakpoints as soon as
449 they're created. If off (default), GDB keeps breakpoints off of
450 the target as long as possible. That is, it delays inserting
451 breakpoints until the next resume, and removes them again when the
452 target fully stops. This is a bit safer in case GDB crashes while
453 processing user input. */
454static int always_inserted_mode = 0;
455
456static void
457show_always_inserted_mode (struct ui_file *file, int from_tty,
458 struct cmd_list_element *c, const char *value)
459{
460 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
461 value);
462}
463
464/* See breakpoint.h. */
465
466int
467breakpoints_should_be_inserted_now (void)
468{
469 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
470 {
471 /* If breakpoints are global, they should be inserted even if no
472 thread under gdb's control is running, or even if there are
473 no threads under GDB's control yet. */
474 return 1;
475 }
476 else if (target_has_execution)
477 {
478 if (always_inserted_mode)
479 {
480 /* The user wants breakpoints inserted even if all threads
481 are stopped. */
482 return 1;
483 }
484
485 if (threads_are_executing ())
486 return 1;
487 }
488 return 0;
489}
490
491static const char condition_evaluation_both[] = "host or target";
492
493/* Modes for breakpoint condition evaluation. */
494static const char condition_evaluation_auto[] = "auto";
495static const char condition_evaluation_host[] = "host";
496static const char condition_evaluation_target[] = "target";
497static const char *const condition_evaluation_enums[] = {
498 condition_evaluation_auto,
499 condition_evaluation_host,
500 condition_evaluation_target,
501 NULL
502};
503
504/* Global that holds the current mode for breakpoint condition evaluation. */
505static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
506
507/* Global that we use to display information to the user (gets its value from
508 condition_evaluation_mode_1. */
509static const char *condition_evaluation_mode = condition_evaluation_auto;
510
511/* Translate a condition evaluation mode MODE into either "host"
512 or "target". This is used mostly to translate from "auto" to the
513 real setting that is being used. It returns the translated
514 evaluation mode. */
515
516static const char *
517translate_condition_evaluation_mode (const char *mode)
518{
519 if (mode == condition_evaluation_auto)
520 {
521 if (target_supports_evaluation_of_breakpoint_conditions ())
522 return condition_evaluation_target;
523 else
524 return condition_evaluation_host;
525 }
526 else
527 return mode;
528}
529
530/* Discovers what condition_evaluation_auto translates to. */
531
532static const char *
533breakpoint_condition_evaluation_mode (void)
534{
535 return translate_condition_evaluation_mode (condition_evaluation_mode);
536}
537
538/* Return true if GDB should evaluate breakpoint conditions or false
539 otherwise. */
540
541static int
542gdb_evaluates_breakpoint_condition_p (void)
543{
544 const char *mode = breakpoint_condition_evaluation_mode ();
545
546 return (mode == condition_evaluation_host);
547}
548
549void _initialize_breakpoint (void);
550
551/* Are we executing breakpoint commands? */
552static int executing_breakpoint_commands;
553
554/* Are overlay event breakpoints enabled? */
555static int overlay_events_enabled;
556
557/* See description in breakpoint.h. */
558int target_exact_watchpoints = 0;
559
560/* Walk the following statement or block through all breakpoints.
561 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
562 current breakpoint. */
563
564#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
565
566#define ALL_BREAKPOINTS_SAFE(B,TMP) \
567 for (B = breakpoint_chain; \
568 B ? (TMP=B->next, 1): 0; \
569 B = TMP)
570
571/* Similar iterator for the low-level breakpoints. SAFE variant is
572 not provided so update_global_location_list must not be called
573 while executing the block of ALL_BP_LOCATIONS. */
574
575#define ALL_BP_LOCATIONS(B,BP_TMP) \
576 for (BP_TMP = bp_location; \
577 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
578 BP_TMP++)
579
580/* Iterates through locations with address ADDRESS for the currently selected
581 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
582 to where the loop should start from.
583 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
584 appropriate location to start with. */
585
586#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
587 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
588 BP_LOCP_TMP = BP_LOCP_START; \
589 BP_LOCP_START \
590 && (BP_LOCP_TMP < bp_location + bp_location_count \
591 && (*BP_LOCP_TMP)->address == ADDRESS); \
592 BP_LOCP_TMP++)
593
594/* Iterator for tracepoints only. */
595
596#define ALL_TRACEPOINTS(B) \
597 for (B = breakpoint_chain; B; B = B->next) \
598 if (is_tracepoint (B))
599
600/* Chains of all breakpoints defined. */
601
602struct breakpoint *breakpoint_chain;
603
604/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
605
606static struct bp_location **bp_location;
607
608/* Number of elements of BP_LOCATION. */
609
610static unsigned bp_location_count;
611
612/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
613 ADDRESS for the current elements of BP_LOCATION which get a valid
614 result from bp_location_has_shadow. You can use it for roughly
615 limiting the subrange of BP_LOCATION to scan for shadow bytes for
616 an address you need to read. */
617
618static CORE_ADDR bp_location_placed_address_before_address_max;
619
620/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
621 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
622 BP_LOCATION which get a valid result from bp_location_has_shadow.
623 You can use it for roughly limiting the subrange of BP_LOCATION to
624 scan for shadow bytes for an address you need to read. */
625
626static CORE_ADDR bp_location_shadow_len_after_address_max;
627
628/* The locations that no longer correspond to any breakpoint, unlinked
629 from bp_location array, but for which a hit may still be reported
630 by a target. */
631VEC(bp_location_p) *moribund_locations = NULL;
632
633/* Number of last breakpoint made. */
634
635static int breakpoint_count;
636
637/* The value of `breakpoint_count' before the last command that
638 created breakpoints. If the last (break-like) command created more
639 than one breakpoint, then the difference between BREAKPOINT_COUNT
640 and PREV_BREAKPOINT_COUNT is more than one. */
641static int prev_breakpoint_count;
642
643/* Number of last tracepoint made. */
644
645static int tracepoint_count;
646
647static struct cmd_list_element *breakpoint_set_cmdlist;
648static struct cmd_list_element *breakpoint_show_cmdlist;
649struct cmd_list_element *save_cmdlist;
650
651/* Return whether a breakpoint is an active enabled breakpoint. */
652static int
653breakpoint_enabled (struct breakpoint *b)
654{
655 return (b->enable_state == bp_enabled);
656}
657
658/* Set breakpoint count to NUM. */
659
660static void
661set_breakpoint_count (int num)
662{
663 prev_breakpoint_count = breakpoint_count;
664 breakpoint_count = num;
665 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
666}
667
668/* Used by `start_rbreak_breakpoints' below, to record the current
669 breakpoint count before "rbreak" creates any breakpoint. */
670static int rbreak_start_breakpoint_count;
671
672/* Called at the start an "rbreak" command to record the first
673 breakpoint made. */
674
675void
676start_rbreak_breakpoints (void)
677{
678 rbreak_start_breakpoint_count = breakpoint_count;
679}
680
681/* Called at the end of an "rbreak" command to record the last
682 breakpoint made. */
683
684void
685end_rbreak_breakpoints (void)
686{
687 prev_breakpoint_count = rbreak_start_breakpoint_count;
688}
689
690/* Used in run_command to zero the hit count when a new run starts. */
691
692void
693clear_breakpoint_hit_counts (void)
694{
695 struct breakpoint *b;
696
697 ALL_BREAKPOINTS (b)
698 b->hit_count = 0;
699}
700
701/* Allocate a new counted_command_line with reference count of 1.
702 The new structure owns COMMANDS. */
703
704static struct counted_command_line *
705alloc_counted_command_line (struct command_line *commands)
706{
707 struct counted_command_line *result
708 = xmalloc (sizeof (struct counted_command_line));
709
710 result->refc = 1;
711 result->commands = commands;
712 return result;
713}
714
715/* Increment reference count. This does nothing if CMD is NULL. */
716
717static void
718incref_counted_command_line (struct counted_command_line *cmd)
719{
720 if (cmd)
721 ++cmd->refc;
722}
723
724/* Decrement reference count. If the reference count reaches 0,
725 destroy the counted_command_line. Sets *CMDP to NULL. This does
726 nothing if *CMDP is NULL. */
727
728static void
729decref_counted_command_line (struct counted_command_line **cmdp)
730{
731 if (*cmdp)
732 {
733 if (--(*cmdp)->refc == 0)
734 {
735 free_command_lines (&(*cmdp)->commands);
736 xfree (*cmdp);
737 }
738 *cmdp = NULL;
739 }
740}
741
742/* A cleanup function that calls decref_counted_command_line. */
743
744static void
745do_cleanup_counted_command_line (void *arg)
746{
747 decref_counted_command_line (arg);
748}
749
750/* Create a cleanup that calls decref_counted_command_line on the
751 argument. */
752
753static struct cleanup *
754make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
755{
756 return make_cleanup (do_cleanup_counted_command_line, cmdp);
757}
758
759\f
760/* Return the breakpoint with the specified number, or NULL
761 if the number does not refer to an existing breakpoint. */
762
763struct breakpoint *
764get_breakpoint (int num)
765{
766 struct breakpoint *b;
767
768 ALL_BREAKPOINTS (b)
769 if (b->number == num)
770 return b;
771
772 return NULL;
773}
774
775\f
776
777/* Mark locations as "conditions have changed" in case the target supports
778 evaluating conditions on its side. */
779
780static void
781mark_breakpoint_modified (struct breakpoint *b)
782{
783 struct bp_location *loc;
784
785 /* This is only meaningful if the target is
786 evaluating conditions and if the user has
787 opted for condition evaluation on the target's
788 side. */
789 if (gdb_evaluates_breakpoint_condition_p ()
790 || !target_supports_evaluation_of_breakpoint_conditions ())
791 return;
792
793 if (!is_breakpoint (b))
794 return;
795
796 for (loc = b->loc; loc; loc = loc->next)
797 loc->condition_changed = condition_modified;
798}
799
800/* Mark location as "conditions have changed" in case the target supports
801 evaluating conditions on its side. */
802
803static void
804mark_breakpoint_location_modified (struct bp_location *loc)
805{
806 /* This is only meaningful if the target is
807 evaluating conditions and if the user has
808 opted for condition evaluation on the target's
809 side. */
810 if (gdb_evaluates_breakpoint_condition_p ()
811 || !target_supports_evaluation_of_breakpoint_conditions ())
812
813 return;
814
815 if (!is_breakpoint (loc->owner))
816 return;
817
818 loc->condition_changed = condition_modified;
819}
820
821/* Sets the condition-evaluation mode using the static global
822 condition_evaluation_mode. */
823
824static void
825set_condition_evaluation_mode (char *args, int from_tty,
826 struct cmd_list_element *c)
827{
828 const char *old_mode, *new_mode;
829
830 if ((condition_evaluation_mode_1 == condition_evaluation_target)
831 && !target_supports_evaluation_of_breakpoint_conditions ())
832 {
833 condition_evaluation_mode_1 = condition_evaluation_mode;
834 warning (_("Target does not support breakpoint condition evaluation.\n"
835 "Using host evaluation mode instead."));
836 return;
837 }
838
839 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
840 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
841
842 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
843 settings was "auto". */
844 condition_evaluation_mode = condition_evaluation_mode_1;
845
846 /* Only update the mode if the user picked a different one. */
847 if (new_mode != old_mode)
848 {
849 struct bp_location *loc, **loc_tmp;
850 /* If the user switched to a different evaluation mode, we
851 need to synch the changes with the target as follows:
852
853 "host" -> "target": Send all (valid) conditions to the target.
854 "target" -> "host": Remove all the conditions from the target.
855 */
856
857 if (new_mode == condition_evaluation_target)
858 {
859 /* Mark everything modified and synch conditions with the
860 target. */
861 ALL_BP_LOCATIONS (loc, loc_tmp)
862 mark_breakpoint_location_modified (loc);
863 }
864 else
865 {
866 /* Manually mark non-duplicate locations to synch conditions
867 with the target. We do this to remove all the conditions the
868 target knows about. */
869 ALL_BP_LOCATIONS (loc, loc_tmp)
870 if (is_breakpoint (loc->owner) && loc->inserted)
871 loc->needs_update = 1;
872 }
873
874 /* Do the update. */
875 update_global_location_list (UGLL_MAY_INSERT);
876 }
877
878 return;
879}
880
881/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
882 what "auto" is translating to. */
883
884static void
885show_condition_evaluation_mode (struct ui_file *file, int from_tty,
886 struct cmd_list_element *c, const char *value)
887{
888 if (condition_evaluation_mode == condition_evaluation_auto)
889 fprintf_filtered (file,
890 _("Breakpoint condition evaluation "
891 "mode is %s (currently %s).\n"),
892 value,
893 breakpoint_condition_evaluation_mode ());
894 else
895 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
896 value);
897}
898
899/* A comparison function for bp_location AP and BP that is used by
900 bsearch. This comparison function only cares about addresses, unlike
901 the more general bp_location_compare function. */
902
903static int
904bp_location_compare_addrs (const void *ap, const void *bp)
905{
906 struct bp_location *a = *(void **) ap;
907 struct bp_location *b = *(void **) bp;
908
909 if (a->address == b->address)
910 return 0;
911 else
912 return ((a->address > b->address) - (a->address < b->address));
913}
914
915/* Helper function to skip all bp_locations with addresses
916 less than ADDRESS. It returns the first bp_location that
917 is greater than or equal to ADDRESS. If none is found, just
918 return NULL. */
919
920static struct bp_location **
921get_first_locp_gte_addr (CORE_ADDR address)
922{
923 struct bp_location dummy_loc;
924 struct bp_location *dummy_locp = &dummy_loc;
925 struct bp_location **locp_found = NULL;
926
927 /* Initialize the dummy location's address field. */
928 memset (&dummy_loc, 0, sizeof (struct bp_location));
929 dummy_loc.address = address;
930
931 /* Find a close match to the first location at ADDRESS. */
932 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
933 sizeof (struct bp_location **),
934 bp_location_compare_addrs);
935
936 /* Nothing was found, nothing left to do. */
937 if (locp_found == NULL)
938 return NULL;
939
940 /* We may have found a location that is at ADDRESS but is not the first in the
941 location's list. Go backwards (if possible) and locate the first one. */
942 while ((locp_found - 1) >= bp_location
943 && (*(locp_found - 1))->address == address)
944 locp_found--;
945
946 return locp_found;
947}
948
949void
950set_breakpoint_condition (struct breakpoint *b, char *exp,
951 int from_tty)
952{
953 xfree (b->cond_string);
954 b->cond_string = NULL;
955
956 if (is_watchpoint (b))
957 {
958 struct watchpoint *w = (struct watchpoint *) b;
959
960 xfree (w->cond_exp);
961 w->cond_exp = NULL;
962 }
963 else
964 {
965 struct bp_location *loc;
966
967 for (loc = b->loc; loc; loc = loc->next)
968 {
969 xfree (loc->cond);
970 loc->cond = NULL;
971
972 /* No need to free the condition agent expression
973 bytecode (if we have one). We will handle this
974 when we go through update_global_location_list. */
975 }
976 }
977
978 if (*exp == 0)
979 {
980 if (from_tty)
981 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
982 }
983 else
984 {
985 const char *arg = exp;
986
987 /* I don't know if it matters whether this is the string the user
988 typed in or the decompiled expression. */
989 b->cond_string = xstrdup (arg);
990 b->condition_not_parsed = 0;
991
992 if (is_watchpoint (b))
993 {
994 struct watchpoint *w = (struct watchpoint *) b;
995
996 innermost_block = NULL;
997 arg = exp;
998 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
999 if (*arg)
1000 error (_("Junk at end of expression"));
1001 w->cond_exp_valid_block = innermost_block;
1002 }
1003 else
1004 {
1005 struct bp_location *loc;
1006
1007 for (loc = b->loc; loc; loc = loc->next)
1008 {
1009 arg = exp;
1010 loc->cond =
1011 parse_exp_1 (&arg, loc->address,
1012 block_for_pc (loc->address), 0);
1013 if (*arg)
1014 error (_("Junk at end of expression"));
1015 }
1016 }
1017 }
1018 mark_breakpoint_modified (b);
1019
1020 observer_notify_breakpoint_modified (b);
1021}
1022
1023/* Completion for the "condition" command. */
1024
1025static VEC (char_ptr) *
1026condition_completer (struct cmd_list_element *cmd,
1027 const char *text, const char *word)
1028{
1029 const char *space;
1030
1031 text = skip_spaces_const (text);
1032 space = skip_to_space_const (text);
1033 if (*space == '\0')
1034 {
1035 int len;
1036 struct breakpoint *b;
1037 VEC (char_ptr) *result = NULL;
1038
1039 if (text[0] == '$')
1040 {
1041 /* We don't support completion of history indices. */
1042 if (isdigit (text[1]))
1043 return NULL;
1044 return complete_internalvar (&text[1]);
1045 }
1046
1047 /* We're completing the breakpoint number. */
1048 len = strlen (text);
1049
1050 ALL_BREAKPOINTS (b)
1051 {
1052 char number[50];
1053
1054 xsnprintf (number, sizeof (number), "%d", b->number);
1055
1056 if (strncmp (number, text, len) == 0)
1057 VEC_safe_push (char_ptr, result, xstrdup (number));
1058 }
1059
1060 return result;
1061 }
1062
1063 /* We're completing the expression part. */
1064 text = skip_spaces_const (space);
1065 return expression_completer (cmd, text, word);
1066}
1067
1068/* condition N EXP -- set break condition of breakpoint N to EXP. */
1069
1070static void
1071condition_command (char *arg, int from_tty)
1072{
1073 struct breakpoint *b;
1074 char *p;
1075 int bnum;
1076
1077 if (arg == 0)
1078 error_no_arg (_("breakpoint number"));
1079
1080 p = arg;
1081 bnum = get_number (&p);
1082 if (bnum == 0)
1083 error (_("Bad breakpoint argument: '%s'"), arg);
1084
1085 ALL_BREAKPOINTS (b)
1086 if (b->number == bnum)
1087 {
1088 /* Check if this breakpoint has a "stop" method implemented in an
1089 extension language. This method and conditions entered into GDB
1090 from the CLI are mutually exclusive. */
1091 const struct extension_language_defn *extlang
1092 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1093
1094 if (extlang != NULL)
1095 {
1096 error (_("Only one stop condition allowed. There is currently"
1097 " a %s stop condition defined for this breakpoint."),
1098 ext_lang_capitalized_name (extlang));
1099 }
1100 set_breakpoint_condition (b, p, from_tty);
1101
1102 if (is_breakpoint (b))
1103 update_global_location_list (UGLL_MAY_INSERT);
1104
1105 return;
1106 }
1107
1108 error (_("No breakpoint number %d."), bnum);
1109}
1110
1111/* Check that COMMAND do not contain commands that are suitable
1112 only for tracepoints and not suitable for ordinary breakpoints.
1113 Throw if any such commands is found. */
1114
1115static void
1116check_no_tracepoint_commands (struct command_line *commands)
1117{
1118 struct command_line *c;
1119
1120 for (c = commands; c; c = c->next)
1121 {
1122 int i;
1123
1124 if (c->control_type == while_stepping_control)
1125 error (_("The 'while-stepping' command can "
1126 "only be used for tracepoints"));
1127
1128 for (i = 0; i < c->body_count; ++i)
1129 check_no_tracepoint_commands ((c->body_list)[i]);
1130
1131 /* Not that command parsing removes leading whitespace and comment
1132 lines and also empty lines. So, we only need to check for
1133 command directly. */
1134 if (strstr (c->line, "collect ") == c->line)
1135 error (_("The 'collect' command can only be used for tracepoints"));
1136
1137 if (strstr (c->line, "teval ") == c->line)
1138 error (_("The 'teval' command can only be used for tracepoints"));
1139 }
1140}
1141
1142/* Encapsulate tests for different types of tracepoints. */
1143
1144static int
1145is_tracepoint_type (enum bptype type)
1146{
1147 return (type == bp_tracepoint
1148 || type == bp_fast_tracepoint
1149 || type == bp_static_tracepoint);
1150}
1151
1152int
1153is_tracepoint (const struct breakpoint *b)
1154{
1155 return is_tracepoint_type (b->type);
1156}
1157
1158/* A helper function that validates that COMMANDS are valid for a
1159 breakpoint. This function will throw an exception if a problem is
1160 found. */
1161
1162static void
1163validate_commands_for_breakpoint (struct breakpoint *b,
1164 struct command_line *commands)
1165{
1166 if (is_tracepoint (b))
1167 {
1168 struct tracepoint *t = (struct tracepoint *) b;
1169 struct command_line *c;
1170 struct command_line *while_stepping = 0;
1171
1172 /* Reset the while-stepping step count. The previous commands
1173 might have included a while-stepping action, while the new
1174 ones might not. */
1175 t->step_count = 0;
1176
1177 /* We need to verify that each top-level element of commands is
1178 valid for tracepoints, that there's at most one
1179 while-stepping element, and that the while-stepping's body
1180 has valid tracing commands excluding nested while-stepping.
1181 We also need to validate the tracepoint action line in the
1182 context of the tracepoint --- validate_actionline actually
1183 has side effects, like setting the tracepoint's
1184 while-stepping STEP_COUNT, in addition to checking if the
1185 collect/teval actions parse and make sense in the
1186 tracepoint's context. */
1187 for (c = commands; c; c = c->next)
1188 {
1189 if (c->control_type == while_stepping_control)
1190 {
1191 if (b->type == bp_fast_tracepoint)
1192 error (_("The 'while-stepping' command "
1193 "cannot be used for fast tracepoint"));
1194 else if (b->type == bp_static_tracepoint)
1195 error (_("The 'while-stepping' command "
1196 "cannot be used for static tracepoint"));
1197
1198 if (while_stepping)
1199 error (_("The 'while-stepping' command "
1200 "can be used only once"));
1201 else
1202 while_stepping = c;
1203 }
1204
1205 validate_actionline (c->line, b);
1206 }
1207 if (while_stepping)
1208 {
1209 struct command_line *c2;
1210
1211 gdb_assert (while_stepping->body_count == 1);
1212 c2 = while_stepping->body_list[0];
1213 for (; c2; c2 = c2->next)
1214 {
1215 if (c2->control_type == while_stepping_control)
1216 error (_("The 'while-stepping' command cannot be nested"));
1217 }
1218 }
1219 }
1220 else
1221 {
1222 check_no_tracepoint_commands (commands);
1223 }
1224}
1225
1226/* Return a vector of all the static tracepoints set at ADDR. The
1227 caller is responsible for releasing the vector. */
1228
1229VEC(breakpoint_p) *
1230static_tracepoints_here (CORE_ADDR addr)
1231{
1232 struct breakpoint *b;
1233 VEC(breakpoint_p) *found = 0;
1234 struct bp_location *loc;
1235
1236 ALL_BREAKPOINTS (b)
1237 if (b->type == bp_static_tracepoint)
1238 {
1239 for (loc = b->loc; loc; loc = loc->next)
1240 if (loc->address == addr)
1241 VEC_safe_push(breakpoint_p, found, b);
1242 }
1243
1244 return found;
1245}
1246
1247/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1248 validate that only allowed commands are included. */
1249
1250void
1251breakpoint_set_commands (struct breakpoint *b,
1252 struct command_line *commands)
1253{
1254 validate_commands_for_breakpoint (b, commands);
1255
1256 decref_counted_command_line (&b->commands);
1257 b->commands = alloc_counted_command_line (commands);
1258 observer_notify_breakpoint_modified (b);
1259}
1260
1261/* Set the internal `silent' flag on the breakpoint. Note that this
1262 is not the same as the "silent" that may appear in the breakpoint's
1263 commands. */
1264
1265void
1266breakpoint_set_silent (struct breakpoint *b, int silent)
1267{
1268 int old_silent = b->silent;
1269
1270 b->silent = silent;
1271 if (old_silent != silent)
1272 observer_notify_breakpoint_modified (b);
1273}
1274
1275/* Set the thread for this breakpoint. If THREAD is -1, make the
1276 breakpoint work for any thread. */
1277
1278void
1279breakpoint_set_thread (struct breakpoint *b, int thread)
1280{
1281 int old_thread = b->thread;
1282
1283 b->thread = thread;
1284 if (old_thread != thread)
1285 observer_notify_breakpoint_modified (b);
1286}
1287
1288/* Set the task for this breakpoint. If TASK is 0, make the
1289 breakpoint work for any task. */
1290
1291void
1292breakpoint_set_task (struct breakpoint *b, int task)
1293{
1294 int old_task = b->task;
1295
1296 b->task = task;
1297 if (old_task != task)
1298 observer_notify_breakpoint_modified (b);
1299}
1300
1301void
1302check_tracepoint_command (char *line, void *closure)
1303{
1304 struct breakpoint *b = closure;
1305
1306 validate_actionline (line, b);
1307}
1308
1309/* A structure used to pass information through
1310 map_breakpoint_numbers. */
1311
1312struct commands_info
1313{
1314 /* True if the command was typed at a tty. */
1315 int from_tty;
1316
1317 /* The breakpoint range spec. */
1318 char *arg;
1319
1320 /* Non-NULL if the body of the commands are being read from this
1321 already-parsed command. */
1322 struct command_line *control;
1323
1324 /* The command lines read from the user, or NULL if they have not
1325 yet been read. */
1326 struct counted_command_line *cmd;
1327};
1328
1329/* A callback for map_breakpoint_numbers that sets the commands for
1330 commands_command. */
1331
1332static void
1333do_map_commands_command (struct breakpoint *b, void *data)
1334{
1335 struct commands_info *info = data;
1336
1337 if (info->cmd == NULL)
1338 {
1339 struct command_line *l;
1340
1341 if (info->control != NULL)
1342 l = copy_command_lines (info->control->body_list[0]);
1343 else
1344 {
1345 struct cleanup *old_chain;
1346 char *str;
1347
1348 str = xstrprintf (_("Type commands for breakpoint(s) "
1349 "%s, one per line."),
1350 info->arg);
1351
1352 old_chain = make_cleanup (xfree, str);
1353
1354 l = read_command_lines (str,
1355 info->from_tty, 1,
1356 (is_tracepoint (b)
1357 ? check_tracepoint_command : 0),
1358 b);
1359
1360 do_cleanups (old_chain);
1361 }
1362
1363 info->cmd = alloc_counted_command_line (l);
1364 }
1365
1366 /* If a breakpoint was on the list more than once, we don't need to
1367 do anything. */
1368 if (b->commands != info->cmd)
1369 {
1370 validate_commands_for_breakpoint (b, info->cmd->commands);
1371 incref_counted_command_line (info->cmd);
1372 decref_counted_command_line (&b->commands);
1373 b->commands = info->cmd;
1374 observer_notify_breakpoint_modified (b);
1375 }
1376}
1377
1378static void
1379commands_command_1 (char *arg, int from_tty,
1380 struct command_line *control)
1381{
1382 struct cleanup *cleanups;
1383 struct commands_info info;
1384
1385 info.from_tty = from_tty;
1386 info.control = control;
1387 info.cmd = NULL;
1388 /* If we read command lines from the user, then `info' will hold an
1389 extra reference to the commands that we must clean up. */
1390 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1391
1392 if (arg == NULL || !*arg)
1393 {
1394 if (breakpoint_count - prev_breakpoint_count > 1)
1395 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1396 breakpoint_count);
1397 else if (breakpoint_count > 0)
1398 arg = xstrprintf ("%d", breakpoint_count);
1399 else
1400 {
1401 /* So that we don't try to free the incoming non-NULL
1402 argument in the cleanup below. Mapping breakpoint
1403 numbers will fail in this case. */
1404 arg = NULL;
1405 }
1406 }
1407 else
1408 /* The command loop has some static state, so we need to preserve
1409 our argument. */
1410 arg = xstrdup (arg);
1411
1412 if (arg != NULL)
1413 make_cleanup (xfree, arg);
1414
1415 info.arg = arg;
1416
1417 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1418
1419 if (info.cmd == NULL)
1420 error (_("No breakpoints specified."));
1421
1422 do_cleanups (cleanups);
1423}
1424
1425static void
1426commands_command (char *arg, int from_tty)
1427{
1428 commands_command_1 (arg, from_tty, NULL);
1429}
1430
1431/* Like commands_command, but instead of reading the commands from
1432 input stream, takes them from an already parsed command structure.
1433
1434 This is used by cli-script.c to DTRT with breakpoint commands
1435 that are part of if and while bodies. */
1436enum command_control_type
1437commands_from_control_command (char *arg, struct command_line *cmd)
1438{
1439 commands_command_1 (arg, 0, cmd);
1440 return simple_control;
1441}
1442
1443/* Return non-zero if BL->TARGET_INFO contains valid information. */
1444
1445static int
1446bp_location_has_shadow (struct bp_location *bl)
1447{
1448 if (bl->loc_type != bp_loc_software_breakpoint)
1449 return 0;
1450 if (!bl->inserted)
1451 return 0;
1452 if (bl->target_info.shadow_len == 0)
1453 /* BL isn't valid, or doesn't shadow memory. */
1454 return 0;
1455 return 1;
1456}
1457
1458/* Update BUF, which is LEN bytes read from the target address
1459 MEMADDR, by replacing a memory breakpoint with its shadowed
1460 contents.
1461
1462 If READBUF is not NULL, this buffer must not overlap with the of
1463 the breakpoint location's shadow_contents buffer. Otherwise, a
1464 failed assertion internal error will be raised. */
1465
1466static void
1467one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1468 const gdb_byte *writebuf_org,
1469 ULONGEST memaddr, LONGEST len,
1470 struct bp_target_info *target_info,
1471 struct gdbarch *gdbarch)
1472{
1473 /* Now do full processing of the found relevant range of elements. */
1474 CORE_ADDR bp_addr = 0;
1475 int bp_size = 0;
1476 int bptoffset = 0;
1477
1478 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1479 current_program_space->aspace, 0))
1480 {
1481 /* The breakpoint is inserted in a different address space. */
1482 return;
1483 }
1484
1485 /* Addresses and length of the part of the breakpoint that
1486 we need to copy. */
1487 bp_addr = target_info->placed_address;
1488 bp_size = target_info->shadow_len;
1489
1490 if (bp_addr + bp_size <= memaddr)
1491 {
1492 /* The breakpoint is entirely before the chunk of memory we are
1493 reading. */
1494 return;
1495 }
1496
1497 if (bp_addr >= memaddr + len)
1498 {
1499 /* The breakpoint is entirely after the chunk of memory we are
1500 reading. */
1501 return;
1502 }
1503
1504 /* Offset within shadow_contents. */
1505 if (bp_addr < memaddr)
1506 {
1507 /* Only copy the second part of the breakpoint. */
1508 bp_size -= memaddr - bp_addr;
1509 bptoffset = memaddr - bp_addr;
1510 bp_addr = memaddr;
1511 }
1512
1513 if (bp_addr + bp_size > memaddr + len)
1514 {
1515 /* Only copy the first part of the breakpoint. */
1516 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1517 }
1518
1519 if (readbuf != NULL)
1520 {
1521 /* Verify that the readbuf buffer does not overlap with the
1522 shadow_contents buffer. */
1523 gdb_assert (target_info->shadow_contents >= readbuf + len
1524 || readbuf >= (target_info->shadow_contents
1525 + target_info->shadow_len));
1526
1527 /* Update the read buffer with this inserted breakpoint's
1528 shadow. */
1529 memcpy (readbuf + bp_addr - memaddr,
1530 target_info->shadow_contents + bptoffset, bp_size);
1531 }
1532 else
1533 {
1534 const unsigned char *bp;
1535 CORE_ADDR addr = target_info->reqstd_address;
1536 int placed_size;
1537
1538 /* Update the shadow with what we want to write to memory. */
1539 memcpy (target_info->shadow_contents + bptoffset,
1540 writebuf_org + bp_addr - memaddr, bp_size);
1541
1542 /* Determine appropriate breakpoint contents and size for this
1543 address. */
1544 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1545
1546 /* Update the final write buffer with this inserted
1547 breakpoint's INSN. */
1548 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1549 }
1550}
1551
1552/* Update BUF, which is LEN bytes read from the target address MEMADDR,
1553 by replacing any memory breakpoints with their shadowed contents.
1554
1555 If READBUF is not NULL, this buffer must not overlap with any of
1556 the breakpoint location's shadow_contents buffers. Otherwise,
1557 a failed assertion internal error will be raised.
1558
1559 The range of shadowed area by each bp_location is:
1560 bl->address - bp_location_placed_address_before_address_max
1561 up to bl->address + bp_location_shadow_len_after_address_max
1562 The range we were requested to resolve shadows for is:
1563 memaddr ... memaddr + len
1564 Thus the safe cutoff boundaries for performance optimization are
1565 memaddr + len <= (bl->address
1566 - bp_location_placed_address_before_address_max)
1567 and:
1568 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1569
1570void
1571breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1572 const gdb_byte *writebuf_org,
1573 ULONGEST memaddr, LONGEST len)
1574{
1575 /* Left boundary, right boundary and median element of our binary
1576 search. */
1577 unsigned bc_l, bc_r, bc;
1578 size_t i;
1579
1580 /* Find BC_L which is a leftmost element which may affect BUF
1581 content. It is safe to report lower value but a failure to
1582 report higher one. */
1583
1584 bc_l = 0;
1585 bc_r = bp_location_count;
1586 while (bc_l + 1 < bc_r)
1587 {
1588 struct bp_location *bl;
1589
1590 bc = (bc_l + bc_r) / 2;
1591 bl = bp_location[bc];
1592
1593 /* Check first BL->ADDRESS will not overflow due to the added
1594 constant. Then advance the left boundary only if we are sure
1595 the BC element can in no way affect the BUF content (MEMADDR
1596 to MEMADDR + LEN range).
1597
1598 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1599 offset so that we cannot miss a breakpoint with its shadow
1600 range tail still reaching MEMADDR. */
1601
1602 if ((bl->address + bp_location_shadow_len_after_address_max
1603 >= bl->address)
1604 && (bl->address + bp_location_shadow_len_after_address_max
1605 <= memaddr))
1606 bc_l = bc;
1607 else
1608 bc_r = bc;
1609 }
1610
1611 /* Due to the binary search above, we need to make sure we pick the
1612 first location that's at BC_L's address. E.g., if there are
1613 multiple locations at the same address, BC_L may end up pointing
1614 at a duplicate location, and miss the "master"/"inserted"
1615 location. Say, given locations L1, L2 and L3 at addresses A and
1616 B:
1617
1618 L1@A, L2@A, L3@B, ...
1619
1620 BC_L could end up pointing at location L2, while the "master"
1621 location could be L1. Since the `loc->inserted' flag is only set
1622 on "master" locations, we'd forget to restore the shadow of L1
1623 and L2. */
1624 while (bc_l > 0
1625 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1626 bc_l--;
1627
1628 /* Now do full processing of the found relevant range of elements. */
1629
1630 for (bc = bc_l; bc < bp_location_count; bc++)
1631 {
1632 struct bp_location *bl = bp_location[bc];
1633 CORE_ADDR bp_addr = 0;
1634 int bp_size = 0;
1635 int bptoffset = 0;
1636
1637 /* bp_location array has BL->OWNER always non-NULL. */
1638 if (bl->owner->type == bp_none)
1639 warning (_("reading through apparently deleted breakpoint #%d?"),
1640 bl->owner->number);
1641
1642 /* Performance optimization: any further element can no longer affect BUF
1643 content. */
1644
1645 if (bl->address >= bp_location_placed_address_before_address_max
1646 && memaddr + len <= (bl->address
1647 - bp_location_placed_address_before_address_max))
1648 break;
1649
1650 if (!bp_location_has_shadow (bl))
1651 continue;
1652
1653 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1654 memaddr, len, &bl->target_info, bl->gdbarch);
1655 }
1656}
1657
1658\f
1659
1660/* Return true if BPT is either a software breakpoint or a hardware
1661 breakpoint. */
1662
1663int
1664is_breakpoint (const struct breakpoint *bpt)
1665{
1666 return (bpt->type == bp_breakpoint
1667 || bpt->type == bp_hardware_breakpoint
1668 || bpt->type == bp_dprintf);
1669}
1670
1671/* Return true if BPT is of any hardware watchpoint kind. */
1672
1673static int
1674is_hardware_watchpoint (const struct breakpoint *bpt)
1675{
1676 return (bpt->type == bp_hardware_watchpoint
1677 || bpt->type == bp_read_watchpoint
1678 || bpt->type == bp_access_watchpoint);
1679}
1680
1681/* Return true if BPT is of any watchpoint kind, hardware or
1682 software. */
1683
1684int
1685is_watchpoint (const struct breakpoint *bpt)
1686{
1687 return (is_hardware_watchpoint (bpt)
1688 || bpt->type == bp_watchpoint);
1689}
1690
1691/* Returns true if the current thread and its running state are safe
1692 to evaluate or update watchpoint B. Watchpoints on local
1693 expressions need to be evaluated in the context of the thread that
1694 was current when the watchpoint was created, and, that thread needs
1695 to be stopped to be able to select the correct frame context.
1696 Watchpoints on global expressions can be evaluated on any thread,
1697 and in any state. It is presently left to the target allowing
1698 memory accesses when threads are running. */
1699
1700static int
1701watchpoint_in_thread_scope (struct watchpoint *b)
1702{
1703 return (b->base.pspace == current_program_space
1704 && (ptid_equal (b->watchpoint_thread, null_ptid)
1705 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1706 && !is_executing (inferior_ptid))));
1707}
1708
1709/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1710 associated bp_watchpoint_scope breakpoint. */
1711
1712static void
1713watchpoint_del_at_next_stop (struct watchpoint *w)
1714{
1715 struct breakpoint *b = &w->base;
1716
1717 if (b->related_breakpoint != b)
1718 {
1719 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1720 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1721 b->related_breakpoint->disposition = disp_del_at_next_stop;
1722 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1723 b->related_breakpoint = b;
1724 }
1725 b->disposition = disp_del_at_next_stop;
1726}
1727
1728/* Extract a bitfield value from value VAL using the bit parameters contained in
1729 watchpoint W. */
1730
1731static struct value *
1732extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1733{
1734 struct value *bit_val;
1735
1736 if (val == NULL)
1737 return NULL;
1738
1739 bit_val = allocate_value (value_type (val));
1740
1741 unpack_value_bitfield (bit_val,
1742 w->val_bitpos,
1743 w->val_bitsize,
1744 value_contents_for_printing (val),
1745 value_offset (val),
1746 val);
1747
1748 return bit_val;
1749}
1750
1751/* Assuming that B is a watchpoint:
1752 - Reparse watchpoint expression, if REPARSE is non-zero
1753 - Evaluate expression and store the result in B->val
1754 - Evaluate the condition if there is one, and store the result
1755 in b->loc->cond.
1756 - Update the list of values that must be watched in B->loc.
1757
1758 If the watchpoint disposition is disp_del_at_next_stop, then do
1759 nothing. If this is local watchpoint that is out of scope, delete
1760 it.
1761
1762 Even with `set breakpoint always-inserted on' the watchpoints are
1763 removed + inserted on each stop here. Normal breakpoints must
1764 never be removed because they might be missed by a running thread
1765 when debugging in non-stop mode. On the other hand, hardware
1766 watchpoints (is_hardware_watchpoint; processed here) are specific
1767 to each LWP since they are stored in each LWP's hardware debug
1768 registers. Therefore, such LWP must be stopped first in order to
1769 be able to modify its hardware watchpoints.
1770
1771 Hardware watchpoints must be reset exactly once after being
1772 presented to the user. It cannot be done sooner, because it would
1773 reset the data used to present the watchpoint hit to the user. And
1774 it must not be done later because it could display the same single
1775 watchpoint hit during multiple GDB stops. Note that the latter is
1776 relevant only to the hardware watchpoint types bp_read_watchpoint
1777 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1778 not user-visible - its hit is suppressed if the memory content has
1779 not changed.
1780
1781 The following constraints influence the location where we can reset
1782 hardware watchpoints:
1783
1784 * target_stopped_by_watchpoint and target_stopped_data_address are
1785 called several times when GDB stops.
1786
1787 [linux]
1788 * Multiple hardware watchpoints can be hit at the same time,
1789 causing GDB to stop. GDB only presents one hardware watchpoint
1790 hit at a time as the reason for stopping, and all the other hits
1791 are presented later, one after the other, each time the user
1792 requests the execution to be resumed. Execution is not resumed
1793 for the threads still having pending hit event stored in
1794 LWP_INFO->STATUS. While the watchpoint is already removed from
1795 the inferior on the first stop the thread hit event is kept being
1796 reported from its cached value by linux_nat_stopped_data_address
1797 until the real thread resume happens after the watchpoint gets
1798 presented and thus its LWP_INFO->STATUS gets reset.
1799
1800 Therefore the hardware watchpoint hit can get safely reset on the
1801 watchpoint removal from inferior. */
1802
1803static void
1804update_watchpoint (struct watchpoint *b, int reparse)
1805{
1806 int within_current_scope;
1807 struct frame_id saved_frame_id;
1808 int frame_saved;
1809
1810 /* If this is a local watchpoint, we only want to check if the
1811 watchpoint frame is in scope if the current thread is the thread
1812 that was used to create the watchpoint. */
1813 if (!watchpoint_in_thread_scope (b))
1814 return;
1815
1816 if (b->base.disposition == disp_del_at_next_stop)
1817 return;
1818
1819 frame_saved = 0;
1820
1821 /* Determine if the watchpoint is within scope. */
1822 if (b->exp_valid_block == NULL)
1823 within_current_scope = 1;
1824 else
1825 {
1826 struct frame_info *fi = get_current_frame ();
1827 struct gdbarch *frame_arch = get_frame_arch (fi);
1828 CORE_ADDR frame_pc = get_frame_pc (fi);
1829
1830 /* If we're in a function epilogue, unwinding may not work
1831 properly, so do not attempt to recreate locations at this
1832 point. See similar comments in watchpoint_check. */
1833 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1834 return;
1835
1836 /* Save the current frame's ID so we can restore it after
1837 evaluating the watchpoint expression on its own frame. */
1838 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1839 took a frame parameter, so that we didn't have to change the
1840 selected frame. */
1841 frame_saved = 1;
1842 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1843
1844 fi = frame_find_by_id (b->watchpoint_frame);
1845 within_current_scope = (fi != NULL);
1846 if (within_current_scope)
1847 select_frame (fi);
1848 }
1849
1850 /* We don't free locations. They are stored in the bp_location array
1851 and update_global_location_list will eventually delete them and
1852 remove breakpoints if needed. */
1853 b->base.loc = NULL;
1854
1855 if (within_current_scope && reparse)
1856 {
1857 const char *s;
1858
1859 if (b->exp)
1860 {
1861 xfree (b->exp);
1862 b->exp = NULL;
1863 }
1864 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1865 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1866 /* If the meaning of expression itself changed, the old value is
1867 no longer relevant. We don't want to report a watchpoint hit
1868 to the user when the old value and the new value may actually
1869 be completely different objects. */
1870 value_free (b->val);
1871 b->val = NULL;
1872 b->val_valid = 0;
1873
1874 /* Note that unlike with breakpoints, the watchpoint's condition
1875 expression is stored in the breakpoint object, not in the
1876 locations (re)created below. */
1877 if (b->base.cond_string != NULL)
1878 {
1879 if (b->cond_exp != NULL)
1880 {
1881 xfree (b->cond_exp);
1882 b->cond_exp = NULL;
1883 }
1884
1885 s = b->base.cond_string;
1886 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1887 }
1888 }
1889
1890 /* If we failed to parse the expression, for example because
1891 it refers to a global variable in a not-yet-loaded shared library,
1892 don't try to insert watchpoint. We don't automatically delete
1893 such watchpoint, though, since failure to parse expression
1894 is different from out-of-scope watchpoint. */
1895 if (!target_has_execution)
1896 {
1897 /* Without execution, memory can't change. No use to try and
1898 set watchpoint locations. The watchpoint will be reset when
1899 the target gains execution, through breakpoint_re_set. */
1900 if (!can_use_hw_watchpoints)
1901 {
1902 if (b->base.ops->works_in_software_mode (&b->base))
1903 b->base.type = bp_watchpoint;
1904 else
1905 error (_("Can't set read/access watchpoint when "
1906 "hardware watchpoints are disabled."));
1907 }
1908 }
1909 else if (within_current_scope && b->exp)
1910 {
1911 int pc = 0;
1912 struct value *val_chain, *v, *result, *next;
1913 struct program_space *frame_pspace;
1914
1915 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1916
1917 /* Avoid setting b->val if it's already set. The meaning of
1918 b->val is 'the last value' user saw, and we should update
1919 it only if we reported that last value to user. As it
1920 happens, the code that reports it updates b->val directly.
1921 We don't keep track of the memory value for masked
1922 watchpoints. */
1923 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1924 {
1925 if (b->val_bitsize != 0)
1926 {
1927 v = extract_bitfield_from_watchpoint_value (b, v);
1928 if (v != NULL)
1929 release_value (v);
1930 }
1931 b->val = v;
1932 b->val_valid = 1;
1933 }
1934
1935 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1936
1937 /* Look at each value on the value chain. */
1938 for (v = val_chain; v; v = value_next (v))
1939 {
1940 /* If it's a memory location, and GDB actually needed
1941 its contents to evaluate the expression, then we
1942 must watch it. If the first value returned is
1943 still lazy, that means an error occurred reading it;
1944 watch it anyway in case it becomes readable. */
1945 if (VALUE_LVAL (v) == lval_memory
1946 && (v == val_chain || ! value_lazy (v)))
1947 {
1948 struct type *vtype = check_typedef (value_type (v));
1949
1950 /* We only watch structs and arrays if user asked
1951 for it explicitly, never if they just happen to
1952 appear in the middle of some value chain. */
1953 if (v == result
1954 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1955 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1956 {
1957 CORE_ADDR addr;
1958 int type;
1959 struct bp_location *loc, **tmp;
1960 int bitpos = 0, bitsize = 0;
1961
1962 if (value_bitsize (v) != 0)
1963 {
1964 /* Extract the bit parameters out from the bitfield
1965 sub-expression. */
1966 bitpos = value_bitpos (v);
1967 bitsize = value_bitsize (v);
1968 }
1969 else if (v == result && b->val_bitsize != 0)
1970 {
1971 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1972 lvalue whose bit parameters are saved in the fields
1973 VAL_BITPOS and VAL_BITSIZE. */
1974 bitpos = b->val_bitpos;
1975 bitsize = b->val_bitsize;
1976 }
1977
1978 addr = value_address (v);
1979 if (bitsize != 0)
1980 {
1981 /* Skip the bytes that don't contain the bitfield. */
1982 addr += bitpos / 8;
1983 }
1984
1985 type = hw_write;
1986 if (b->base.type == bp_read_watchpoint)
1987 type = hw_read;
1988 else if (b->base.type == bp_access_watchpoint)
1989 type = hw_access;
1990
1991 loc = allocate_bp_location (&b->base);
1992 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1993 ;
1994 *tmp = loc;
1995 loc->gdbarch = get_type_arch (value_type (v));
1996
1997 loc->pspace = frame_pspace;
1998 loc->address = addr;
1999
2000 if (bitsize != 0)
2001 {
2002 /* Just cover the bytes that make up the bitfield. */
2003 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2004 }
2005 else
2006 loc->length = TYPE_LENGTH (value_type (v));
2007
2008 loc->watchpoint_type = type;
2009 }
2010 }
2011 }
2012
2013 /* Change the type of breakpoint between hardware assisted or
2014 an ordinary watchpoint depending on the hardware support
2015 and free hardware slots. REPARSE is set when the inferior
2016 is started. */
2017 if (reparse)
2018 {
2019 int reg_cnt;
2020 enum bp_loc_type loc_type;
2021 struct bp_location *bl;
2022
2023 reg_cnt = can_use_hardware_watchpoint (val_chain);
2024
2025 if (reg_cnt)
2026 {
2027 int i, target_resources_ok, other_type_used;
2028 enum bptype type;
2029
2030 /* Use an exact watchpoint when there's only one memory region to be
2031 watched, and only one debug register is needed to watch it. */
2032 b->exact = target_exact_watchpoints && reg_cnt == 1;
2033
2034 /* We need to determine how many resources are already
2035 used for all other hardware watchpoints plus this one
2036 to see if we still have enough resources to also fit
2037 this watchpoint in as well. */
2038
2039 /* If this is a software watchpoint, we try to turn it
2040 to a hardware one -- count resources as if B was of
2041 hardware watchpoint type. */
2042 type = b->base.type;
2043 if (type == bp_watchpoint)
2044 type = bp_hardware_watchpoint;
2045
2046 /* This watchpoint may or may not have been placed on
2047 the list yet at this point (it won't be in the list
2048 if we're trying to create it for the first time,
2049 through watch_command), so always account for it
2050 manually. */
2051
2052 /* Count resources used by all watchpoints except B. */
2053 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2054
2055 /* Add in the resources needed for B. */
2056 i += hw_watchpoint_use_count (&b->base);
2057
2058 target_resources_ok
2059 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2060 if (target_resources_ok <= 0)
2061 {
2062 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2063
2064 if (target_resources_ok == 0 && !sw_mode)
2065 error (_("Target does not support this type of "
2066 "hardware watchpoint."));
2067 else if (target_resources_ok < 0 && !sw_mode)
2068 error (_("There are not enough available hardware "
2069 "resources for this watchpoint."));
2070
2071 /* Downgrade to software watchpoint. */
2072 b->base.type = bp_watchpoint;
2073 }
2074 else
2075 {
2076 /* If this was a software watchpoint, we've just
2077 found we have enough resources to turn it to a
2078 hardware watchpoint. Otherwise, this is a
2079 nop. */
2080 b->base.type = type;
2081 }
2082 }
2083 else if (!b->base.ops->works_in_software_mode (&b->base))
2084 {
2085 if (!can_use_hw_watchpoints)
2086 error (_("Can't set read/access watchpoint when "
2087 "hardware watchpoints are disabled."));
2088 else
2089 error (_("Expression cannot be implemented with "
2090 "read/access watchpoint."));
2091 }
2092 else
2093 b->base.type = bp_watchpoint;
2094
2095 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2096 : bp_loc_hardware_watchpoint);
2097 for (bl = b->base.loc; bl; bl = bl->next)
2098 bl->loc_type = loc_type;
2099 }
2100
2101 for (v = val_chain; v; v = next)
2102 {
2103 next = value_next (v);
2104 if (v != b->val)
2105 value_free (v);
2106 }
2107
2108 /* If a software watchpoint is not watching any memory, then the
2109 above left it without any location set up. But,
2110 bpstat_stop_status requires a location to be able to report
2111 stops, so make sure there's at least a dummy one. */
2112 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2113 {
2114 struct breakpoint *base = &b->base;
2115 base->loc = allocate_bp_location (base);
2116 base->loc->pspace = frame_pspace;
2117 base->loc->address = -1;
2118 base->loc->length = -1;
2119 base->loc->watchpoint_type = -1;
2120 }
2121 }
2122 else if (!within_current_scope)
2123 {
2124 printf_filtered (_("\
2125Watchpoint %d deleted because the program has left the block\n\
2126in which its expression is valid.\n"),
2127 b->base.number);
2128 watchpoint_del_at_next_stop (b);
2129 }
2130
2131 /* Restore the selected frame. */
2132 if (frame_saved)
2133 select_frame (frame_find_by_id (saved_frame_id));
2134}
2135
2136
2137/* Returns 1 iff breakpoint location should be
2138 inserted in the inferior. We don't differentiate the type of BL's owner
2139 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2140 breakpoint_ops is not defined, because in insert_bp_location,
2141 tracepoint's insert_location will not be called. */
2142static int
2143should_be_inserted (struct bp_location *bl)
2144{
2145 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2146 return 0;
2147
2148 if (bl->owner->disposition == disp_del_at_next_stop)
2149 return 0;
2150
2151 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2152 return 0;
2153
2154 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2155 return 0;
2156
2157 /* This is set for example, when we're attached to the parent of a
2158 vfork, and have detached from the child. The child is running
2159 free, and we expect it to do an exec or exit, at which point the
2160 OS makes the parent schedulable again (and the target reports
2161 that the vfork is done). Until the child is done with the shared
2162 memory region, do not insert breakpoints in the parent, otherwise
2163 the child could still trip on the parent's breakpoints. Since
2164 the parent is blocked anyway, it won't miss any breakpoint. */
2165 if (bl->pspace->breakpoints_not_allowed)
2166 return 0;
2167
2168 /* Don't insert a breakpoint if we're trying to step past its
2169 location. */
2170 if ((bl->loc_type == bp_loc_software_breakpoint
2171 || bl->loc_type == bp_loc_hardware_breakpoint)
2172 && stepping_past_instruction_at (bl->pspace->aspace,
2173 bl->address))
2174 {
2175 if (debug_infrun)
2176 {
2177 fprintf_unfiltered (gdb_stdlog,
2178 "infrun: skipping breakpoint: "
2179 "stepping past insn at: %s\n",
2180 paddress (bl->gdbarch, bl->address));
2181 }
2182 return 0;
2183 }
2184
2185 /* Don't insert watchpoints if we're trying to step past the
2186 instruction that triggered one. */
2187 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2188 && stepping_past_nonsteppable_watchpoint ())
2189 {
2190 if (debug_infrun)
2191 {
2192 fprintf_unfiltered (gdb_stdlog,
2193 "infrun: stepping past non-steppable watchpoint. "
2194 "skipping watchpoint at %s:%d\n",
2195 paddress (bl->gdbarch, bl->address),
2196 bl->length);
2197 }
2198 return 0;
2199 }
2200
2201 return 1;
2202}
2203
2204/* Same as should_be_inserted but does the check assuming
2205 that the location is not duplicated. */
2206
2207static int
2208unduplicated_should_be_inserted (struct bp_location *bl)
2209{
2210 int result;
2211 const int save_duplicate = bl->duplicate;
2212
2213 bl->duplicate = 0;
2214 result = should_be_inserted (bl);
2215 bl->duplicate = save_duplicate;
2216 return result;
2217}
2218
2219/* Parses a conditional described by an expression COND into an
2220 agent expression bytecode suitable for evaluation
2221 by the bytecode interpreter. Return NULL if there was
2222 any error during parsing. */
2223
2224static struct agent_expr *
2225parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2226{
2227 struct agent_expr *aexpr = NULL;
2228 volatile struct gdb_exception ex;
2229
2230 if (!cond)
2231 return NULL;
2232
2233 /* We don't want to stop processing, so catch any errors
2234 that may show up. */
2235 TRY_CATCH (ex, RETURN_MASK_ERROR)
2236 {
2237 aexpr = gen_eval_for_expr (scope, cond);
2238 }
2239
2240 if (ex.reason < 0)
2241 {
2242 /* If we got here, it means the condition could not be parsed to a valid
2243 bytecode expression and thus can't be evaluated on the target's side.
2244 It's no use iterating through the conditions. */
2245 return NULL;
2246 }
2247
2248 /* We have a valid agent expression. */
2249 return aexpr;
2250}
2251
2252/* Based on location BL, create a list of breakpoint conditions to be
2253 passed on to the target. If we have duplicated locations with different
2254 conditions, we will add such conditions to the list. The idea is that the
2255 target will evaluate the list of conditions and will only notify GDB when
2256 one of them is true. */
2257
2258static void
2259build_target_condition_list (struct bp_location *bl)
2260{
2261 struct bp_location **locp = NULL, **loc2p;
2262 int null_condition_or_parse_error = 0;
2263 int modified = bl->needs_update;
2264 struct bp_location *loc;
2265
2266 /* Release conditions left over from a previous insert. */
2267 VEC_free (agent_expr_p, bl->target_info.conditions);
2268
2269 /* This is only meaningful if the target is
2270 evaluating conditions and if the user has
2271 opted for condition evaluation on the target's
2272 side. */
2273 if (gdb_evaluates_breakpoint_condition_p ()
2274 || !target_supports_evaluation_of_breakpoint_conditions ())
2275 return;
2276
2277 /* Do a first pass to check for locations with no assigned
2278 conditions or conditions that fail to parse to a valid agent expression
2279 bytecode. If any of these happen, then it's no use to send conditions
2280 to the target since this location will always trigger and generate a
2281 response back to GDB. */
2282 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2283 {
2284 loc = (*loc2p);
2285 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2286 {
2287 if (modified)
2288 {
2289 struct agent_expr *aexpr;
2290
2291 /* Re-parse the conditions since something changed. In that
2292 case we already freed the condition bytecodes (see
2293 force_breakpoint_reinsertion). We just
2294 need to parse the condition to bytecodes again. */
2295 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2296 loc->cond_bytecode = aexpr;
2297
2298 /* Check if we managed to parse the conditional expression
2299 correctly. If not, we will not send this condition
2300 to the target. */
2301 if (aexpr)
2302 continue;
2303 }
2304
2305 /* If we have a NULL bytecode expression, it means something
2306 went wrong or we have a null condition expression. */
2307 if (!loc->cond_bytecode)
2308 {
2309 null_condition_or_parse_error = 1;
2310 break;
2311 }
2312 }
2313 }
2314
2315 /* If any of these happened, it means we will have to evaluate the conditions
2316 for the location's address on gdb's side. It is no use keeping bytecodes
2317 for all the other duplicate locations, thus we free all of them here.
2318
2319 This is so we have a finer control over which locations' conditions are
2320 being evaluated by GDB or the remote stub. */
2321 if (null_condition_or_parse_error)
2322 {
2323 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2324 {
2325 loc = (*loc2p);
2326 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2327 {
2328 /* Only go as far as the first NULL bytecode is
2329 located. */
2330 if (!loc->cond_bytecode)
2331 return;
2332
2333 free_agent_expr (loc->cond_bytecode);
2334 loc->cond_bytecode = NULL;
2335 }
2336 }
2337 }
2338
2339 /* No NULL conditions or failed bytecode generation. Build a condition list
2340 for this location's address. */
2341 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2342 {
2343 loc = (*loc2p);
2344 if (loc->cond
2345 && is_breakpoint (loc->owner)
2346 && loc->pspace->num == bl->pspace->num
2347 && loc->owner->enable_state == bp_enabled
2348 && loc->enabled)
2349 /* Add the condition to the vector. This will be used later to send the
2350 conditions to the target. */
2351 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2352 loc->cond_bytecode);
2353 }
2354
2355 return;
2356}
2357
2358/* Parses a command described by string CMD into an agent expression
2359 bytecode suitable for evaluation by the bytecode interpreter.
2360 Return NULL if there was any error during parsing. */
2361
2362static struct agent_expr *
2363parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2364{
2365 struct cleanup *old_cleanups = 0;
2366 struct expression *expr, **argvec;
2367 struct agent_expr *aexpr = NULL;
2368 volatile struct gdb_exception ex;
2369 const char *cmdrest;
2370 const char *format_start, *format_end;
2371 struct format_piece *fpieces;
2372 int nargs;
2373 struct gdbarch *gdbarch = get_current_arch ();
2374
2375 if (!cmd)
2376 return NULL;
2377
2378 cmdrest = cmd;
2379
2380 if (*cmdrest == ',')
2381 ++cmdrest;
2382 cmdrest = skip_spaces_const (cmdrest);
2383
2384 if (*cmdrest++ != '"')
2385 error (_("No format string following the location"));
2386
2387 format_start = cmdrest;
2388
2389 fpieces = parse_format_string (&cmdrest);
2390
2391 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2392
2393 format_end = cmdrest;
2394
2395 if (*cmdrest++ != '"')
2396 error (_("Bad format string, non-terminated '\"'."));
2397
2398 cmdrest = skip_spaces_const (cmdrest);
2399
2400 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2401 error (_("Invalid argument syntax"));
2402
2403 if (*cmdrest == ',')
2404 cmdrest++;
2405 cmdrest = skip_spaces_const (cmdrest);
2406
2407 /* For each argument, make an expression. */
2408
2409 argvec = (struct expression **) alloca (strlen (cmd)
2410 * sizeof (struct expression *));
2411
2412 nargs = 0;
2413 while (*cmdrest != '\0')
2414 {
2415 const char *cmd1;
2416
2417 cmd1 = cmdrest;
2418 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2419 argvec[nargs++] = expr;
2420 cmdrest = cmd1;
2421 if (*cmdrest == ',')
2422 ++cmdrest;
2423 }
2424
2425 /* We don't want to stop processing, so catch any errors
2426 that may show up. */
2427 TRY_CATCH (ex, RETURN_MASK_ERROR)
2428 {
2429 aexpr = gen_printf (scope, gdbarch, 0, 0,
2430 format_start, format_end - format_start,
2431 fpieces, nargs, argvec);
2432 }
2433
2434 do_cleanups (old_cleanups);
2435
2436 if (ex.reason < 0)
2437 {
2438 /* If we got here, it means the command could not be parsed to a valid
2439 bytecode expression and thus can't be evaluated on the target's side.
2440 It's no use iterating through the other commands. */
2441 return NULL;
2442 }
2443
2444 /* We have a valid agent expression, return it. */
2445 return aexpr;
2446}
2447
2448/* Based on location BL, create a list of breakpoint commands to be
2449 passed on to the target. If we have duplicated locations with
2450 different commands, we will add any such to the list. */
2451
2452static void
2453build_target_command_list (struct bp_location *bl)
2454{
2455 struct bp_location **locp = NULL, **loc2p;
2456 int null_command_or_parse_error = 0;
2457 int modified = bl->needs_update;
2458 struct bp_location *loc;
2459
2460 /* Release commands left over from a previous insert. */
2461 VEC_free (agent_expr_p, bl->target_info.tcommands);
2462
2463 if (!target_can_run_breakpoint_commands ())
2464 return;
2465
2466 /* For now, limit to agent-style dprintf breakpoints. */
2467 if (dprintf_style != dprintf_style_agent)
2468 return;
2469
2470 /* For now, if we have any duplicate location that isn't a dprintf,
2471 don't install the target-side commands, as that would make the
2472 breakpoint not be reported to the core, and we'd lose
2473 control. */
2474 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2475 {
2476 loc = (*loc2p);
2477 if (is_breakpoint (loc->owner)
2478 && loc->pspace->num == bl->pspace->num
2479 && loc->owner->type != bp_dprintf)
2480 return;
2481 }
2482
2483 /* Do a first pass to check for locations with no assigned
2484 conditions or conditions that fail to parse to a valid agent expression
2485 bytecode. If any of these happen, then it's no use to send conditions
2486 to the target since this location will always trigger and generate a
2487 response back to GDB. */
2488 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2489 {
2490 loc = (*loc2p);
2491 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2492 {
2493 if (modified)
2494 {
2495 struct agent_expr *aexpr;
2496
2497 /* Re-parse the commands since something changed. In that
2498 case we already freed the command bytecodes (see
2499 force_breakpoint_reinsertion). We just
2500 need to parse the command to bytecodes again. */
2501 aexpr = parse_cmd_to_aexpr (bl->address,
2502 loc->owner->extra_string);
2503 loc->cmd_bytecode = aexpr;
2504
2505 if (!aexpr)
2506 continue;
2507 }
2508
2509 /* If we have a NULL bytecode expression, it means something
2510 went wrong or we have a null command expression. */
2511 if (!loc->cmd_bytecode)
2512 {
2513 null_command_or_parse_error = 1;
2514 break;
2515 }
2516 }
2517 }
2518
2519 /* If anything failed, then we're not doing target-side commands,
2520 and so clean up. */
2521 if (null_command_or_parse_error)
2522 {
2523 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2524 {
2525 loc = (*loc2p);
2526 if (is_breakpoint (loc->owner)
2527 && loc->pspace->num == bl->pspace->num)
2528 {
2529 /* Only go as far as the first NULL bytecode is
2530 located. */
2531 if (loc->cmd_bytecode == NULL)
2532 return;
2533
2534 free_agent_expr (loc->cmd_bytecode);
2535 loc->cmd_bytecode = NULL;
2536 }
2537 }
2538 }
2539
2540 /* No NULL commands or failed bytecode generation. Build a command list
2541 for this location's address. */
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (loc->owner->extra_string
2546 && is_breakpoint (loc->owner)
2547 && loc->pspace->num == bl->pspace->num
2548 && loc->owner->enable_state == bp_enabled
2549 && loc->enabled)
2550 /* Add the command to the vector. This will be used later
2551 to send the commands to the target. */
2552 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2553 loc->cmd_bytecode);
2554 }
2555
2556 bl->target_info.persist = 0;
2557 /* Maybe flag this location as persistent. */
2558 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2559 bl->target_info.persist = 1;
2560}
2561
2562/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2563 location. Any error messages are printed to TMP_ERROR_STREAM; and
2564 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2565 Returns 0 for success, 1 if the bp_location type is not supported or
2566 -1 for failure.
2567
2568 NOTE drow/2003-09-09: This routine could be broken down to an
2569 object-style method for each breakpoint or catchpoint type. */
2570static int
2571insert_bp_location (struct bp_location *bl,
2572 struct ui_file *tmp_error_stream,
2573 int *disabled_breaks,
2574 int *hw_breakpoint_error,
2575 int *hw_bp_error_explained_already)
2576{
2577 enum errors bp_err = GDB_NO_ERROR;
2578 const char *bp_err_message = NULL;
2579 volatile struct gdb_exception e;
2580
2581 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2582 return 0;
2583
2584 /* Note we don't initialize bl->target_info, as that wipes out
2585 the breakpoint location's shadow_contents if the breakpoint
2586 is still inserted at that location. This in turn breaks
2587 target_read_memory which depends on these buffers when
2588 a memory read is requested at the breakpoint location:
2589 Once the target_info has been wiped, we fail to see that
2590 we have a breakpoint inserted at that address and thus
2591 read the breakpoint instead of returning the data saved in
2592 the breakpoint location's shadow contents. */
2593 bl->target_info.reqstd_address = bl->address;
2594 bl->target_info.placed_address_space = bl->pspace->aspace;
2595 bl->target_info.length = bl->length;
2596
2597 /* When working with target-side conditions, we must pass all the conditions
2598 for the same breakpoint address down to the target since GDB will not
2599 insert those locations. With a list of breakpoint conditions, the target
2600 can decide when to stop and notify GDB. */
2601
2602 if (is_breakpoint (bl->owner))
2603 {
2604 build_target_condition_list (bl);
2605 build_target_command_list (bl);
2606 /* Reset the modification marker. */
2607 bl->needs_update = 0;
2608 }
2609
2610 if (bl->loc_type == bp_loc_software_breakpoint
2611 || bl->loc_type == bp_loc_hardware_breakpoint)
2612 {
2613 if (bl->owner->type != bp_hardware_breakpoint)
2614 {
2615 /* If the explicitly specified breakpoint type
2616 is not hardware breakpoint, check the memory map to see
2617 if the breakpoint address is in read only memory or not.
2618
2619 Two important cases are:
2620 - location type is not hardware breakpoint, memory
2621 is readonly. We change the type of the location to
2622 hardware breakpoint.
2623 - location type is hardware breakpoint, memory is
2624 read-write. This means we've previously made the
2625 location hardware one, but then the memory map changed,
2626 so we undo.
2627
2628 When breakpoints are removed, remove_breakpoints will use
2629 location types we've just set here, the only possible
2630 problem is that memory map has changed during running
2631 program, but it's not going to work anyway with current
2632 gdb. */
2633 struct mem_region *mr
2634 = lookup_mem_region (bl->target_info.reqstd_address);
2635
2636 if (mr)
2637 {
2638 if (automatic_hardware_breakpoints)
2639 {
2640 enum bp_loc_type new_type;
2641
2642 if (mr->attrib.mode != MEM_RW)
2643 new_type = bp_loc_hardware_breakpoint;
2644 else
2645 new_type = bp_loc_software_breakpoint;
2646
2647 if (new_type != bl->loc_type)
2648 {
2649 static int said = 0;
2650
2651 bl->loc_type = new_type;
2652 if (!said)
2653 {
2654 fprintf_filtered (gdb_stdout,
2655 _("Note: automatically using "
2656 "hardware breakpoints for "
2657 "read-only addresses.\n"));
2658 said = 1;
2659 }
2660 }
2661 }
2662 else if (bl->loc_type == bp_loc_software_breakpoint
2663 && mr->attrib.mode != MEM_RW)
2664 {
2665 fprintf_unfiltered (tmp_error_stream,
2666 _("Cannot insert breakpoint %d.\n"
2667 "Cannot set software breakpoint "
2668 "at read-only address %s\n"),
2669 bl->owner->number,
2670 paddress (bl->gdbarch, bl->address));
2671 return 1;
2672 }
2673 }
2674 }
2675
2676 /* First check to see if we have to handle an overlay. */
2677 if (overlay_debugging == ovly_off
2678 || bl->section == NULL
2679 || !(section_is_overlay (bl->section)))
2680 {
2681 /* No overlay handling: just set the breakpoint. */
2682 TRY_CATCH (e, RETURN_MASK_ALL)
2683 {
2684 int val;
2685
2686 val = bl->owner->ops->insert_location (bl);
2687 if (val)
2688 bp_err = GENERIC_ERROR;
2689 }
2690 if (e.reason < 0)
2691 {
2692 bp_err = e.error;
2693 bp_err_message = e.message;
2694 }
2695 }
2696 else
2697 {
2698 /* This breakpoint is in an overlay section.
2699 Shall we set a breakpoint at the LMA? */
2700 if (!overlay_events_enabled)
2701 {
2702 /* Yes -- overlay event support is not active,
2703 so we must try to set a breakpoint at the LMA.
2704 This will not work for a hardware breakpoint. */
2705 if (bl->loc_type == bp_loc_hardware_breakpoint)
2706 warning (_("hardware breakpoint %d not supported in overlay!"),
2707 bl->owner->number);
2708 else
2709 {
2710 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2711 bl->section);
2712 /* Set a software (trap) breakpoint at the LMA. */
2713 bl->overlay_target_info = bl->target_info;
2714 bl->overlay_target_info.reqstd_address = addr;
2715
2716 /* No overlay handling: just set the breakpoint. */
2717 TRY_CATCH (e, RETURN_MASK_ALL)
2718 {
2719 int val;
2720
2721 val = target_insert_breakpoint (bl->gdbarch,
2722 &bl->overlay_target_info);
2723 if (val)
2724 bp_err = GENERIC_ERROR;
2725 }
2726 if (e.reason < 0)
2727 {
2728 bp_err = e.error;
2729 bp_err_message = e.message;
2730 }
2731
2732 if (bp_err != GDB_NO_ERROR)
2733 fprintf_unfiltered (tmp_error_stream,
2734 "Overlay breakpoint %d "
2735 "failed: in ROM?\n",
2736 bl->owner->number);
2737 }
2738 }
2739 /* Shall we set a breakpoint at the VMA? */
2740 if (section_is_mapped (bl->section))
2741 {
2742 /* Yes. This overlay section is mapped into memory. */
2743 TRY_CATCH (e, RETURN_MASK_ALL)
2744 {
2745 int val;
2746
2747 val = bl->owner->ops->insert_location (bl);
2748 if (val)
2749 bp_err = GENERIC_ERROR;
2750 }
2751 if (e.reason < 0)
2752 {
2753 bp_err = e.error;
2754 bp_err_message = e.message;
2755 }
2756 }
2757 else
2758 {
2759 /* No. This breakpoint will not be inserted.
2760 No error, but do not mark the bp as 'inserted'. */
2761 return 0;
2762 }
2763 }
2764
2765 if (bp_err != GDB_NO_ERROR)
2766 {
2767 /* Can't set the breakpoint. */
2768
2769 /* In some cases, we might not be able to insert a
2770 breakpoint in a shared library that has already been
2771 removed, but we have not yet processed the shlib unload
2772 event. Unfortunately, some targets that implement
2773 breakpoint insertion themselves can't tell why the
2774 breakpoint insertion failed (e.g., the remote target
2775 doesn't define error codes), so we must treat generic
2776 errors as memory errors. */
2777 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2778 && bl->loc_type == bp_loc_software_breakpoint
2779 && (solib_name_from_address (bl->pspace, bl->address)
2780 || shared_objfile_contains_address_p (bl->pspace,
2781 bl->address)))
2782 {
2783 /* See also: disable_breakpoints_in_shlibs. */
2784 bl->shlib_disabled = 1;
2785 observer_notify_breakpoint_modified (bl->owner);
2786 if (!*disabled_breaks)
2787 {
2788 fprintf_unfiltered (tmp_error_stream,
2789 "Cannot insert breakpoint %d.\n",
2790 bl->owner->number);
2791 fprintf_unfiltered (tmp_error_stream,
2792 "Temporarily disabling shared "
2793 "library breakpoints:\n");
2794 }
2795 *disabled_breaks = 1;
2796 fprintf_unfiltered (tmp_error_stream,
2797 "breakpoint #%d\n", bl->owner->number);
2798 return 0;
2799 }
2800 else
2801 {
2802 if (bl->loc_type == bp_loc_hardware_breakpoint)
2803 {
2804 *hw_breakpoint_error = 1;
2805 *hw_bp_error_explained_already = bp_err_message != NULL;
2806 fprintf_unfiltered (tmp_error_stream,
2807 "Cannot insert hardware breakpoint %d%s",
2808 bl->owner->number, bp_err_message ? ":" : ".\n");
2809 if (bp_err_message != NULL)
2810 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2811 }
2812 else
2813 {
2814 if (bp_err_message == NULL)
2815 {
2816 char *message
2817 = memory_error_message (TARGET_XFER_E_IO,
2818 bl->gdbarch, bl->address);
2819 struct cleanup *old_chain = make_cleanup (xfree, message);
2820
2821 fprintf_unfiltered (tmp_error_stream,
2822 "Cannot insert breakpoint %d.\n"
2823 "%s\n",
2824 bl->owner->number, message);
2825 do_cleanups (old_chain);
2826 }
2827 else
2828 {
2829 fprintf_unfiltered (tmp_error_stream,
2830 "Cannot insert breakpoint %d: %s\n",
2831 bl->owner->number,
2832 bp_err_message);
2833 }
2834 }
2835 return 1;
2836
2837 }
2838 }
2839 else
2840 bl->inserted = 1;
2841
2842 return 0;
2843 }
2844
2845 else if (bl->loc_type == bp_loc_hardware_watchpoint
2846 /* NOTE drow/2003-09-08: This state only exists for removing
2847 watchpoints. It's not clear that it's necessary... */
2848 && bl->owner->disposition != disp_del_at_next_stop)
2849 {
2850 int val;
2851
2852 gdb_assert (bl->owner->ops != NULL
2853 && bl->owner->ops->insert_location != NULL);
2854
2855 val = bl->owner->ops->insert_location (bl);
2856
2857 /* If trying to set a read-watchpoint, and it turns out it's not
2858 supported, try emulating one with an access watchpoint. */
2859 if (val == 1 && bl->watchpoint_type == hw_read)
2860 {
2861 struct bp_location *loc, **loc_temp;
2862
2863 /* But don't try to insert it, if there's already another
2864 hw_access location that would be considered a duplicate
2865 of this one. */
2866 ALL_BP_LOCATIONS (loc, loc_temp)
2867 if (loc != bl
2868 && loc->watchpoint_type == hw_access
2869 && watchpoint_locations_match (bl, loc))
2870 {
2871 bl->duplicate = 1;
2872 bl->inserted = 1;
2873 bl->target_info = loc->target_info;
2874 bl->watchpoint_type = hw_access;
2875 val = 0;
2876 break;
2877 }
2878
2879 if (val == 1)
2880 {
2881 bl->watchpoint_type = hw_access;
2882 val = bl->owner->ops->insert_location (bl);
2883
2884 if (val)
2885 /* Back to the original value. */
2886 bl->watchpoint_type = hw_read;
2887 }
2888 }
2889
2890 bl->inserted = (val == 0);
2891 }
2892
2893 else if (bl->owner->type == bp_catchpoint)
2894 {
2895 int val;
2896
2897 gdb_assert (bl->owner->ops != NULL
2898 && bl->owner->ops->insert_location != NULL);
2899
2900 val = bl->owner->ops->insert_location (bl);
2901 if (val)
2902 {
2903 bl->owner->enable_state = bp_disabled;
2904
2905 if (val == 1)
2906 warning (_("\
2907Error inserting catchpoint %d: Your system does not support this type\n\
2908of catchpoint."), bl->owner->number);
2909 else
2910 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2911 }
2912
2913 bl->inserted = (val == 0);
2914
2915 /* We've already printed an error message if there was a problem
2916 inserting this catchpoint, and we've disabled the catchpoint,
2917 so just return success. */
2918 return 0;
2919 }
2920
2921 return 0;
2922}
2923
2924/* This function is called when program space PSPACE is about to be
2925 deleted. It takes care of updating breakpoints to not reference
2926 PSPACE anymore. */
2927
2928void
2929breakpoint_program_space_exit (struct program_space *pspace)
2930{
2931 struct breakpoint *b, *b_temp;
2932 struct bp_location *loc, **loc_temp;
2933
2934 /* Remove any breakpoint that was set through this program space. */
2935 ALL_BREAKPOINTS_SAFE (b, b_temp)
2936 {
2937 if (b->pspace == pspace)
2938 delete_breakpoint (b);
2939 }
2940
2941 /* Breakpoints set through other program spaces could have locations
2942 bound to PSPACE as well. Remove those. */
2943 ALL_BP_LOCATIONS (loc, loc_temp)
2944 {
2945 struct bp_location *tmp;
2946
2947 if (loc->pspace == pspace)
2948 {
2949 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2950 if (loc->owner->loc == loc)
2951 loc->owner->loc = loc->next;
2952 else
2953 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2954 if (tmp->next == loc)
2955 {
2956 tmp->next = loc->next;
2957 break;
2958 }
2959 }
2960 }
2961
2962 /* Now update the global location list to permanently delete the
2963 removed locations above. */
2964 update_global_location_list (UGLL_DONT_INSERT);
2965}
2966
2967/* Make sure all breakpoints are inserted in inferior.
2968 Throws exception on any error.
2969 A breakpoint that is already inserted won't be inserted
2970 again, so calling this function twice is safe. */
2971void
2972insert_breakpoints (void)
2973{
2974 struct breakpoint *bpt;
2975
2976 ALL_BREAKPOINTS (bpt)
2977 if (is_hardware_watchpoint (bpt))
2978 {
2979 struct watchpoint *w = (struct watchpoint *) bpt;
2980
2981 update_watchpoint (w, 0 /* don't reparse. */);
2982 }
2983
2984 /* Updating watchpoints creates new locations, so update the global
2985 location list. Explicitly tell ugll to insert locations and
2986 ignore breakpoints_always_inserted_mode. */
2987 update_global_location_list (UGLL_INSERT);
2988}
2989
2990/* Invoke CALLBACK for each of bp_location. */
2991
2992void
2993iterate_over_bp_locations (walk_bp_location_callback callback)
2994{
2995 struct bp_location *loc, **loc_tmp;
2996
2997 ALL_BP_LOCATIONS (loc, loc_tmp)
2998 {
2999 callback (loc, NULL);
3000 }
3001}
3002
3003/* This is used when we need to synch breakpoint conditions between GDB and the
3004 target. It is the case with deleting and disabling of breakpoints when using
3005 always-inserted mode. */
3006
3007static void
3008update_inserted_breakpoint_locations (void)
3009{
3010 struct bp_location *bl, **blp_tmp;
3011 int error_flag = 0;
3012 int val = 0;
3013 int disabled_breaks = 0;
3014 int hw_breakpoint_error = 0;
3015 int hw_bp_details_reported = 0;
3016
3017 struct ui_file *tmp_error_stream = mem_fileopen ();
3018 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3019
3020 /* Explicitly mark the warning -- this will only be printed if
3021 there was an error. */
3022 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3023
3024 save_current_space_and_thread ();
3025
3026 ALL_BP_LOCATIONS (bl, blp_tmp)
3027 {
3028 /* We only want to update software breakpoints and hardware
3029 breakpoints. */
3030 if (!is_breakpoint (bl->owner))
3031 continue;
3032
3033 /* We only want to update locations that are already inserted
3034 and need updating. This is to avoid unwanted insertion during
3035 deletion of breakpoints. */
3036 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3037 continue;
3038
3039 switch_to_program_space_and_thread (bl->pspace);
3040
3041 /* For targets that support global breakpoints, there's no need
3042 to select an inferior to insert breakpoint to. In fact, even
3043 if we aren't attached to any process yet, we should still
3044 insert breakpoints. */
3045 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3046 && ptid_equal (inferior_ptid, null_ptid))
3047 continue;
3048
3049 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3050 &hw_breakpoint_error, &hw_bp_details_reported);
3051 if (val)
3052 error_flag = val;
3053 }
3054
3055 if (error_flag)
3056 {
3057 target_terminal_ours_for_output ();
3058 error_stream (tmp_error_stream);
3059 }
3060
3061 do_cleanups (cleanups);
3062}
3063
3064/* Used when starting or continuing the program. */
3065
3066static void
3067insert_breakpoint_locations (void)
3068{
3069 struct breakpoint *bpt;
3070 struct bp_location *bl, **blp_tmp;
3071 int error_flag = 0;
3072 int val = 0;
3073 int disabled_breaks = 0;
3074 int hw_breakpoint_error = 0;
3075 int hw_bp_error_explained_already = 0;
3076
3077 struct ui_file *tmp_error_stream = mem_fileopen ();
3078 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3079
3080 /* Explicitly mark the warning -- this will only be printed if
3081 there was an error. */
3082 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3083
3084 save_current_space_and_thread ();
3085
3086 ALL_BP_LOCATIONS (bl, blp_tmp)
3087 {
3088 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3089 continue;
3090
3091 /* There is no point inserting thread-specific breakpoints if
3092 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3093 has BL->OWNER always non-NULL. */
3094 if (bl->owner->thread != -1
3095 && !valid_thread_id (bl->owner->thread))
3096 continue;
3097
3098 switch_to_program_space_and_thread (bl->pspace);
3099
3100 /* For targets that support global breakpoints, there's no need
3101 to select an inferior to insert breakpoint to. In fact, even
3102 if we aren't attached to any process yet, we should still
3103 insert breakpoints. */
3104 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3105 && ptid_equal (inferior_ptid, null_ptid))
3106 continue;
3107
3108 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3109 &hw_breakpoint_error, &hw_bp_error_explained_already);
3110 if (val)
3111 error_flag = val;
3112 }
3113
3114 /* If we failed to insert all locations of a watchpoint, remove
3115 them, as half-inserted watchpoint is of limited use. */
3116 ALL_BREAKPOINTS (bpt)
3117 {
3118 int some_failed = 0;
3119 struct bp_location *loc;
3120
3121 if (!is_hardware_watchpoint (bpt))
3122 continue;
3123
3124 if (!breakpoint_enabled (bpt))
3125 continue;
3126
3127 if (bpt->disposition == disp_del_at_next_stop)
3128 continue;
3129
3130 for (loc = bpt->loc; loc; loc = loc->next)
3131 if (!loc->inserted && should_be_inserted (loc))
3132 {
3133 some_failed = 1;
3134 break;
3135 }
3136 if (some_failed)
3137 {
3138 for (loc = bpt->loc; loc; loc = loc->next)
3139 if (loc->inserted)
3140 remove_breakpoint (loc, mark_uninserted);
3141
3142 hw_breakpoint_error = 1;
3143 fprintf_unfiltered (tmp_error_stream,
3144 "Could not insert hardware watchpoint %d.\n",
3145 bpt->number);
3146 error_flag = -1;
3147 }
3148 }
3149
3150 if (error_flag)
3151 {
3152 /* If a hardware breakpoint or watchpoint was inserted, add a
3153 message about possibly exhausted resources. */
3154 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3155 {
3156 fprintf_unfiltered (tmp_error_stream,
3157 "Could not insert hardware breakpoints:\n\
3158You may have requested too many hardware breakpoints/watchpoints.\n");
3159 }
3160 target_terminal_ours_for_output ();
3161 error_stream (tmp_error_stream);
3162 }
3163
3164 do_cleanups (cleanups);
3165}
3166
3167/* Used when the program stops.
3168 Returns zero if successful, or non-zero if there was a problem
3169 removing a breakpoint location. */
3170
3171int
3172remove_breakpoints (void)
3173{
3174 struct bp_location *bl, **blp_tmp;
3175 int val = 0;
3176
3177 ALL_BP_LOCATIONS (bl, blp_tmp)
3178 {
3179 if (bl->inserted && !is_tracepoint (bl->owner))
3180 val |= remove_breakpoint (bl, mark_uninserted);
3181 }
3182 return val;
3183}
3184
3185/* When a thread exits, remove breakpoints that are related to
3186 that thread. */
3187
3188static void
3189remove_threaded_breakpoints (struct thread_info *tp, int silent)
3190{
3191 struct breakpoint *b, *b_tmp;
3192
3193 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3194 {
3195 if (b->thread == tp->num && user_breakpoint_p (b))
3196 {
3197 b->disposition = disp_del_at_next_stop;
3198
3199 printf_filtered (_("\
3200Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
3201 b->number, tp->num);
3202
3203 /* Hide it from the user. */
3204 b->number = 0;
3205 }
3206 }
3207}
3208
3209/* Remove breakpoints of process PID. */
3210
3211int
3212remove_breakpoints_pid (int pid)
3213{
3214 struct bp_location *bl, **blp_tmp;
3215 int val;
3216 struct inferior *inf = find_inferior_pid (pid);
3217
3218 ALL_BP_LOCATIONS (bl, blp_tmp)
3219 {
3220 if (bl->pspace != inf->pspace)
3221 continue;
3222
3223 if (bl->owner->type == bp_dprintf)
3224 continue;
3225
3226 if (bl->inserted)
3227 {
3228 val = remove_breakpoint (bl, mark_uninserted);
3229 if (val != 0)
3230 return val;
3231 }
3232 }
3233 return 0;
3234}
3235
3236int
3237reattach_breakpoints (int pid)
3238{
3239 struct cleanup *old_chain;
3240 struct bp_location *bl, **blp_tmp;
3241 int val;
3242 struct ui_file *tmp_error_stream;
3243 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3244 struct inferior *inf;
3245 struct thread_info *tp;
3246
3247 tp = any_live_thread_of_process (pid);
3248 if (tp == NULL)
3249 return 1;
3250
3251 inf = find_inferior_pid (pid);
3252 old_chain = save_inferior_ptid ();
3253
3254 inferior_ptid = tp->ptid;
3255
3256 tmp_error_stream = mem_fileopen ();
3257 make_cleanup_ui_file_delete (tmp_error_stream);
3258
3259 ALL_BP_LOCATIONS (bl, blp_tmp)
3260 {
3261 if (bl->pspace != inf->pspace)
3262 continue;
3263
3264 if (bl->inserted)
3265 {
3266 bl->inserted = 0;
3267 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3268 if (val != 0)
3269 {
3270 do_cleanups (old_chain);
3271 return val;
3272 }
3273 }
3274 }
3275 do_cleanups (old_chain);
3276 return 0;
3277}
3278
3279static int internal_breakpoint_number = -1;
3280
3281/* Set the breakpoint number of B, depending on the value of INTERNAL.
3282 If INTERNAL is non-zero, the breakpoint number will be populated
3283 from internal_breakpoint_number and that variable decremented.
3284 Otherwise the breakpoint number will be populated from
3285 breakpoint_count and that value incremented. Internal breakpoints
3286 do not set the internal var bpnum. */
3287static void
3288set_breakpoint_number (int internal, struct breakpoint *b)
3289{
3290 if (internal)
3291 b->number = internal_breakpoint_number--;
3292 else
3293 {
3294 set_breakpoint_count (breakpoint_count + 1);
3295 b->number = breakpoint_count;
3296 }
3297}
3298
3299static struct breakpoint *
3300create_internal_breakpoint (struct gdbarch *gdbarch,
3301 CORE_ADDR address, enum bptype type,
3302 const struct breakpoint_ops *ops)
3303{
3304 struct symtab_and_line sal;
3305 struct breakpoint *b;
3306
3307 init_sal (&sal); /* Initialize to zeroes. */
3308
3309 sal.pc = address;
3310 sal.section = find_pc_overlay (sal.pc);
3311 sal.pspace = current_program_space;
3312
3313 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3314 b->number = internal_breakpoint_number--;
3315 b->disposition = disp_donttouch;
3316
3317 return b;
3318}
3319
3320static const char *const longjmp_names[] =
3321 {
3322 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3323 };
3324#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3325
3326/* Per-objfile data private to breakpoint.c. */
3327struct breakpoint_objfile_data
3328{
3329 /* Minimal symbol for "_ovly_debug_event" (if any). */
3330 struct bound_minimal_symbol overlay_msym;
3331
3332 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3333 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3334
3335 /* True if we have looked for longjmp probes. */
3336 int longjmp_searched;
3337
3338 /* SystemTap probe points for longjmp (if any). */
3339 VEC (probe_p) *longjmp_probes;
3340
3341 /* Minimal symbol for "std::terminate()" (if any). */
3342 struct bound_minimal_symbol terminate_msym;
3343
3344 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3345 struct bound_minimal_symbol exception_msym;
3346
3347 /* True if we have looked for exception probes. */
3348 int exception_searched;
3349
3350 /* SystemTap probe points for unwinding (if any). */
3351 VEC (probe_p) *exception_probes;
3352};
3353
3354static const struct objfile_data *breakpoint_objfile_key;
3355
3356/* Minimal symbol not found sentinel. */
3357static struct minimal_symbol msym_not_found;
3358
3359/* Returns TRUE if MSYM point to the "not found" sentinel. */
3360
3361static int
3362msym_not_found_p (const struct minimal_symbol *msym)
3363{
3364 return msym == &msym_not_found;
3365}
3366
3367/* Return per-objfile data needed by breakpoint.c.
3368 Allocate the data if necessary. */
3369
3370static struct breakpoint_objfile_data *
3371get_breakpoint_objfile_data (struct objfile *objfile)
3372{
3373 struct breakpoint_objfile_data *bp_objfile_data;
3374
3375 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3376 if (bp_objfile_data == NULL)
3377 {
3378 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3379 sizeof (*bp_objfile_data));
3380
3381 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3382 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3383 }
3384 return bp_objfile_data;
3385}
3386
3387static void
3388free_breakpoint_probes (struct objfile *obj, void *data)
3389{
3390 struct breakpoint_objfile_data *bp_objfile_data = data;
3391
3392 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3393 VEC_free (probe_p, bp_objfile_data->exception_probes);
3394}
3395
3396static void
3397create_overlay_event_breakpoint (void)
3398{
3399 struct objfile *objfile;
3400 const char *const func_name = "_ovly_debug_event";
3401
3402 ALL_OBJFILES (objfile)
3403 {
3404 struct breakpoint *b;
3405 struct breakpoint_objfile_data *bp_objfile_data;
3406 CORE_ADDR addr;
3407
3408 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3409
3410 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3411 continue;
3412
3413 if (bp_objfile_data->overlay_msym.minsym == NULL)
3414 {
3415 struct bound_minimal_symbol m;
3416
3417 m = lookup_minimal_symbol_text (func_name, objfile);
3418 if (m.minsym == NULL)
3419 {
3420 /* Avoid future lookups in this objfile. */
3421 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3422 continue;
3423 }
3424 bp_objfile_data->overlay_msym = m;
3425 }
3426
3427 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3428 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3429 bp_overlay_event,
3430 &internal_breakpoint_ops);
3431 b->addr_string = xstrdup (func_name);
3432
3433 if (overlay_debugging == ovly_auto)
3434 {
3435 b->enable_state = bp_enabled;
3436 overlay_events_enabled = 1;
3437 }
3438 else
3439 {
3440 b->enable_state = bp_disabled;
3441 overlay_events_enabled = 0;
3442 }
3443 }
3444 update_global_location_list (UGLL_MAY_INSERT);
3445}
3446
3447static void
3448create_longjmp_master_breakpoint (void)
3449{
3450 struct program_space *pspace;
3451 struct cleanup *old_chain;
3452
3453 old_chain = save_current_program_space ();
3454
3455 ALL_PSPACES (pspace)
3456 {
3457 struct objfile *objfile;
3458
3459 set_current_program_space (pspace);
3460
3461 ALL_OBJFILES (objfile)
3462 {
3463 int i;
3464 struct gdbarch *gdbarch;
3465 struct breakpoint_objfile_data *bp_objfile_data;
3466
3467 gdbarch = get_objfile_arch (objfile);
3468
3469 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3470
3471 if (!bp_objfile_data->longjmp_searched)
3472 {
3473 VEC (probe_p) *ret;
3474
3475 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3476 if (ret != NULL)
3477 {
3478 /* We are only interested in checking one element. */
3479 struct probe *p = VEC_index (probe_p, ret, 0);
3480
3481 if (!can_evaluate_probe_arguments (p))
3482 {
3483 /* We cannot use the probe interface here, because it does
3484 not know how to evaluate arguments. */
3485 VEC_free (probe_p, ret);
3486 ret = NULL;
3487 }
3488 }
3489 bp_objfile_data->longjmp_probes = ret;
3490 bp_objfile_data->longjmp_searched = 1;
3491 }
3492
3493 if (bp_objfile_data->longjmp_probes != NULL)
3494 {
3495 int i;
3496 struct probe *probe;
3497 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3498
3499 for (i = 0;
3500 VEC_iterate (probe_p,
3501 bp_objfile_data->longjmp_probes,
3502 i, probe);
3503 ++i)
3504 {
3505 struct breakpoint *b;
3506
3507 b = create_internal_breakpoint (gdbarch,
3508 get_probe_address (probe,
3509 objfile),
3510 bp_longjmp_master,
3511 &internal_breakpoint_ops);
3512 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3513 b->enable_state = bp_disabled;
3514 }
3515
3516 continue;
3517 }
3518
3519 if (!gdbarch_get_longjmp_target_p (gdbarch))
3520 continue;
3521
3522 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3523 {
3524 struct breakpoint *b;
3525 const char *func_name;
3526 CORE_ADDR addr;
3527
3528 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3529 continue;
3530
3531 func_name = longjmp_names[i];
3532 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3533 {
3534 struct bound_minimal_symbol m;
3535
3536 m = lookup_minimal_symbol_text (func_name, objfile);
3537 if (m.minsym == NULL)
3538 {
3539 /* Prevent future lookups in this objfile. */
3540 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3541 continue;
3542 }
3543 bp_objfile_data->longjmp_msym[i] = m;
3544 }
3545
3546 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3547 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3548 &internal_breakpoint_ops);
3549 b->addr_string = xstrdup (func_name);
3550 b->enable_state = bp_disabled;
3551 }
3552 }
3553 }
3554 update_global_location_list (UGLL_MAY_INSERT);
3555
3556 do_cleanups (old_chain);
3557}
3558
3559/* Create a master std::terminate breakpoint. */
3560static void
3561create_std_terminate_master_breakpoint (void)
3562{
3563 struct program_space *pspace;
3564 struct cleanup *old_chain;
3565 const char *const func_name = "std::terminate()";
3566
3567 old_chain = save_current_program_space ();
3568
3569 ALL_PSPACES (pspace)
3570 {
3571 struct objfile *objfile;
3572 CORE_ADDR addr;
3573
3574 set_current_program_space (pspace);
3575
3576 ALL_OBJFILES (objfile)
3577 {
3578 struct breakpoint *b;
3579 struct breakpoint_objfile_data *bp_objfile_data;
3580
3581 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3582
3583 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3584 continue;
3585
3586 if (bp_objfile_data->terminate_msym.minsym == NULL)
3587 {
3588 struct bound_minimal_symbol m;
3589
3590 m = lookup_minimal_symbol (func_name, NULL, objfile);
3591 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3592 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3593 {
3594 /* Prevent future lookups in this objfile. */
3595 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3596 continue;
3597 }
3598 bp_objfile_data->terminate_msym = m;
3599 }
3600
3601 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3602 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3603 bp_std_terminate_master,
3604 &internal_breakpoint_ops);
3605 b->addr_string = xstrdup (func_name);
3606 b->enable_state = bp_disabled;
3607 }
3608 }
3609
3610 update_global_location_list (UGLL_MAY_INSERT);
3611
3612 do_cleanups (old_chain);
3613}
3614
3615/* Install a master breakpoint on the unwinder's debug hook. */
3616
3617static void
3618create_exception_master_breakpoint (void)
3619{
3620 struct objfile *objfile;
3621 const char *const func_name = "_Unwind_DebugHook";
3622
3623 ALL_OBJFILES (objfile)
3624 {
3625 struct breakpoint *b;
3626 struct gdbarch *gdbarch;
3627 struct breakpoint_objfile_data *bp_objfile_data;
3628 CORE_ADDR addr;
3629
3630 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3631
3632 /* We prefer the SystemTap probe point if it exists. */
3633 if (!bp_objfile_data->exception_searched)
3634 {
3635 VEC (probe_p) *ret;
3636
3637 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3638
3639 if (ret != NULL)
3640 {
3641 /* We are only interested in checking one element. */
3642 struct probe *p = VEC_index (probe_p, ret, 0);
3643
3644 if (!can_evaluate_probe_arguments (p))
3645 {
3646 /* We cannot use the probe interface here, because it does
3647 not know how to evaluate arguments. */
3648 VEC_free (probe_p, ret);
3649 ret = NULL;
3650 }
3651 }
3652 bp_objfile_data->exception_probes = ret;
3653 bp_objfile_data->exception_searched = 1;
3654 }
3655
3656 if (bp_objfile_data->exception_probes != NULL)
3657 {
3658 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3659 int i;
3660 struct probe *probe;
3661
3662 for (i = 0;
3663 VEC_iterate (probe_p,
3664 bp_objfile_data->exception_probes,
3665 i, probe);
3666 ++i)
3667 {
3668 struct breakpoint *b;
3669
3670 b = create_internal_breakpoint (gdbarch,
3671 get_probe_address (probe,
3672 objfile),
3673 bp_exception_master,
3674 &internal_breakpoint_ops);
3675 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3676 b->enable_state = bp_disabled;
3677 }
3678
3679 continue;
3680 }
3681
3682 /* Otherwise, try the hook function. */
3683
3684 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3685 continue;
3686
3687 gdbarch = get_objfile_arch (objfile);
3688
3689 if (bp_objfile_data->exception_msym.minsym == NULL)
3690 {
3691 struct bound_minimal_symbol debug_hook;
3692
3693 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3694 if (debug_hook.minsym == NULL)
3695 {
3696 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3697 continue;
3698 }
3699
3700 bp_objfile_data->exception_msym = debug_hook;
3701 }
3702
3703 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3704 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3705 &current_target);
3706 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3707 &internal_breakpoint_ops);
3708 b->addr_string = xstrdup (func_name);
3709 b->enable_state = bp_disabled;
3710 }
3711
3712 update_global_location_list (UGLL_MAY_INSERT);
3713}
3714
3715void
3716update_breakpoints_after_exec (void)
3717{
3718 struct breakpoint *b, *b_tmp;
3719 struct bp_location *bploc, **bplocp_tmp;
3720
3721 /* We're about to delete breakpoints from GDB's lists. If the
3722 INSERTED flag is true, GDB will try to lift the breakpoints by
3723 writing the breakpoints' "shadow contents" back into memory. The
3724 "shadow contents" are NOT valid after an exec, so GDB should not
3725 do that. Instead, the target is responsible from marking
3726 breakpoints out as soon as it detects an exec. We don't do that
3727 here instead, because there may be other attempts to delete
3728 breakpoints after detecting an exec and before reaching here. */
3729 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3730 if (bploc->pspace == current_program_space)
3731 gdb_assert (!bploc->inserted);
3732
3733 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3734 {
3735 if (b->pspace != current_program_space)
3736 continue;
3737
3738 /* Solib breakpoints must be explicitly reset after an exec(). */
3739 if (b->type == bp_shlib_event)
3740 {
3741 delete_breakpoint (b);
3742 continue;
3743 }
3744
3745 /* JIT breakpoints must be explicitly reset after an exec(). */
3746 if (b->type == bp_jit_event)
3747 {
3748 delete_breakpoint (b);
3749 continue;
3750 }
3751
3752 /* Thread event breakpoints must be set anew after an exec(),
3753 as must overlay event and longjmp master breakpoints. */
3754 if (b->type == bp_thread_event || b->type == bp_overlay_event
3755 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3756 || b->type == bp_exception_master)
3757 {
3758 delete_breakpoint (b);
3759 continue;
3760 }
3761
3762 /* Step-resume breakpoints are meaningless after an exec(). */
3763 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3764 {
3765 delete_breakpoint (b);
3766 continue;
3767 }
3768
3769 /* Just like single-step breakpoints. */
3770 if (b->type == bp_single_step)
3771 {
3772 delete_breakpoint (b);
3773 continue;
3774 }
3775
3776 /* Longjmp and longjmp-resume breakpoints are also meaningless
3777 after an exec. */
3778 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3779 || b->type == bp_longjmp_call_dummy
3780 || b->type == bp_exception || b->type == bp_exception_resume)
3781 {
3782 delete_breakpoint (b);
3783 continue;
3784 }
3785
3786 if (b->type == bp_catchpoint)
3787 {
3788 /* For now, none of the bp_catchpoint breakpoints need to
3789 do anything at this point. In the future, if some of
3790 the catchpoints need to something, we will need to add
3791 a new method, and call this method from here. */
3792 continue;
3793 }
3794
3795 /* bp_finish is a special case. The only way we ought to be able
3796 to see one of these when an exec() has happened, is if the user
3797 caught a vfork, and then said "finish". Ordinarily a finish just
3798 carries them to the call-site of the current callee, by setting
3799 a temporary bp there and resuming. But in this case, the finish
3800 will carry them entirely through the vfork & exec.
3801
3802 We don't want to allow a bp_finish to remain inserted now. But
3803 we can't safely delete it, 'cause finish_command has a handle to
3804 the bp on a bpstat, and will later want to delete it. There's a
3805 chance (and I've seen it happen) that if we delete the bp_finish
3806 here, that its storage will get reused by the time finish_command
3807 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3808 We really must allow finish_command to delete a bp_finish.
3809
3810 In the absence of a general solution for the "how do we know
3811 it's safe to delete something others may have handles to?"
3812 problem, what we'll do here is just uninsert the bp_finish, and
3813 let finish_command delete it.
3814
3815 (We know the bp_finish is "doomed" in the sense that it's
3816 momentary, and will be deleted as soon as finish_command sees
3817 the inferior stopped. So it doesn't matter that the bp's
3818 address is probably bogus in the new a.out, unlike e.g., the
3819 solib breakpoints.) */
3820
3821 if (b->type == bp_finish)
3822 {
3823 continue;
3824 }
3825
3826 /* Without a symbolic address, we have little hope of the
3827 pre-exec() address meaning the same thing in the post-exec()
3828 a.out. */
3829 if (b->addr_string == NULL)
3830 {
3831 delete_breakpoint (b);
3832 continue;
3833 }
3834 }
3835}
3836
3837int
3838detach_breakpoints (ptid_t ptid)
3839{
3840 struct bp_location *bl, **blp_tmp;
3841 int val = 0;
3842 struct cleanup *old_chain = save_inferior_ptid ();
3843 struct inferior *inf = current_inferior ();
3844
3845 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3846 error (_("Cannot detach breakpoints of inferior_ptid"));
3847
3848 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3849 inferior_ptid = ptid;
3850 ALL_BP_LOCATIONS (bl, blp_tmp)
3851 {
3852 if (bl->pspace != inf->pspace)
3853 continue;
3854
3855 /* This function must physically remove breakpoints locations
3856 from the specified ptid, without modifying the breakpoint
3857 package's state. Locations of type bp_loc_other are only
3858 maintained at GDB side. So, there is no need to remove
3859 these bp_loc_other locations. Moreover, removing these
3860 would modify the breakpoint package's state. */
3861 if (bl->loc_type == bp_loc_other)
3862 continue;
3863
3864 if (bl->inserted)
3865 val |= remove_breakpoint_1 (bl, mark_inserted);
3866 }
3867
3868 do_cleanups (old_chain);
3869 return val;
3870}
3871
3872/* Remove the breakpoint location BL from the current address space.
3873 Note that this is used to detach breakpoints from a child fork.
3874 When we get here, the child isn't in the inferior list, and neither
3875 do we have objects to represent its address space --- we should
3876 *not* look at bl->pspace->aspace here. */
3877
3878static int
3879remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3880{
3881 int val;
3882
3883 /* BL is never in moribund_locations by our callers. */
3884 gdb_assert (bl->owner != NULL);
3885
3886 if (bl->owner->enable_state == bp_permanent)
3887 /* Permanent breakpoints cannot be inserted or removed. */
3888 return 0;
3889
3890 /* The type of none suggests that owner is actually deleted.
3891 This should not ever happen. */
3892 gdb_assert (bl->owner->type != bp_none);
3893
3894 if (bl->loc_type == bp_loc_software_breakpoint
3895 || bl->loc_type == bp_loc_hardware_breakpoint)
3896 {
3897 /* "Normal" instruction breakpoint: either the standard
3898 trap-instruction bp (bp_breakpoint), or a
3899 bp_hardware_breakpoint. */
3900
3901 /* First check to see if we have to handle an overlay. */
3902 if (overlay_debugging == ovly_off
3903 || bl->section == NULL
3904 || !(section_is_overlay (bl->section)))
3905 {
3906 /* No overlay handling: just remove the breakpoint. */
3907
3908 /* If we're trying to uninsert a memory breakpoint that we
3909 know is set in a dynamic object that is marked
3910 shlib_disabled, then either the dynamic object was
3911 removed with "remove-symbol-file" or with
3912 "nosharedlibrary". In the former case, we don't know
3913 whether another dynamic object might have loaded over the
3914 breakpoint's address -- the user might well let us know
3915 about it next with add-symbol-file (the whole point of
3916 add-symbol-file is letting the user manually maintain a
3917 list of dynamically loaded objects). If we have the
3918 breakpoint's shadow memory, that is, this is a software
3919 breakpoint managed by GDB, check whether the breakpoint
3920 is still inserted in memory, to avoid overwriting wrong
3921 code with stale saved shadow contents. Note that HW
3922 breakpoints don't have shadow memory, as they're
3923 implemented using a mechanism that is not dependent on
3924 being able to modify the target's memory, and as such
3925 they should always be removed. */
3926 if (bl->shlib_disabled
3927 && bl->target_info.shadow_len != 0
3928 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3929 val = 0;
3930 else
3931 val = bl->owner->ops->remove_location (bl);
3932 }
3933 else
3934 {
3935 /* This breakpoint is in an overlay section.
3936 Did we set a breakpoint at the LMA? */
3937 if (!overlay_events_enabled)
3938 {
3939 /* Yes -- overlay event support is not active, so we
3940 should have set a breakpoint at the LMA. Remove it.
3941 */
3942 /* Ignore any failures: if the LMA is in ROM, we will
3943 have already warned when we failed to insert it. */
3944 if (bl->loc_type == bp_loc_hardware_breakpoint)
3945 target_remove_hw_breakpoint (bl->gdbarch,
3946 &bl->overlay_target_info);
3947 else
3948 target_remove_breakpoint (bl->gdbarch,
3949 &bl->overlay_target_info);
3950 }
3951 /* Did we set a breakpoint at the VMA?
3952 If so, we will have marked the breakpoint 'inserted'. */
3953 if (bl->inserted)
3954 {
3955 /* Yes -- remove it. Previously we did not bother to
3956 remove the breakpoint if the section had been
3957 unmapped, but let's not rely on that being safe. We
3958 don't know what the overlay manager might do. */
3959
3960 /* However, we should remove *software* breakpoints only
3961 if the section is still mapped, or else we overwrite
3962 wrong code with the saved shadow contents. */
3963 if (bl->loc_type == bp_loc_hardware_breakpoint
3964 || section_is_mapped (bl->section))
3965 val = bl->owner->ops->remove_location (bl);
3966 else
3967 val = 0;
3968 }
3969 else
3970 {
3971 /* No -- not inserted, so no need to remove. No error. */
3972 val = 0;
3973 }
3974 }
3975
3976 /* In some cases, we might not be able to remove a breakpoint in
3977 a shared library that has already been removed, but we have
3978 not yet processed the shlib unload event. Similarly for an
3979 unloaded add-symbol-file object - the user might not yet have
3980 had the chance to remove-symbol-file it. shlib_disabled will
3981 be set if the library/object has already been removed, but
3982 the breakpoint hasn't been uninserted yet, e.g., after
3983 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3984 always-inserted mode. */
3985 if (val
3986 && (bl->loc_type == bp_loc_software_breakpoint
3987 && (bl->shlib_disabled
3988 || solib_name_from_address (bl->pspace, bl->address)
3989 || shared_objfile_contains_address_p (bl->pspace,
3990 bl->address))))
3991 val = 0;
3992
3993 if (val)
3994 return val;
3995 bl->inserted = (is == mark_inserted);
3996 }
3997 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3998 {
3999 gdb_assert (bl->owner->ops != NULL
4000 && bl->owner->ops->remove_location != NULL);
4001
4002 bl->inserted = (is == mark_inserted);
4003 bl->owner->ops->remove_location (bl);
4004
4005 /* Failure to remove any of the hardware watchpoints comes here. */
4006 if ((is == mark_uninserted) && (bl->inserted))
4007 warning (_("Could not remove hardware watchpoint %d."),
4008 bl->owner->number);
4009 }
4010 else if (bl->owner->type == bp_catchpoint
4011 && breakpoint_enabled (bl->owner)
4012 && !bl->duplicate)
4013 {
4014 gdb_assert (bl->owner->ops != NULL
4015 && bl->owner->ops->remove_location != NULL);
4016
4017 val = bl->owner->ops->remove_location (bl);
4018 if (val)
4019 return val;
4020
4021 bl->inserted = (is == mark_inserted);
4022 }
4023
4024 return 0;
4025}
4026
4027static int
4028remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4029{
4030 int ret;
4031 struct cleanup *old_chain;
4032
4033 /* BL is never in moribund_locations by our callers. */
4034 gdb_assert (bl->owner != NULL);
4035
4036 if (bl->owner->enable_state == bp_permanent)
4037 /* Permanent breakpoints cannot be inserted or removed. */
4038 return 0;
4039
4040 /* The type of none suggests that owner is actually deleted.
4041 This should not ever happen. */
4042 gdb_assert (bl->owner->type != bp_none);
4043
4044 old_chain = save_current_space_and_thread ();
4045
4046 switch_to_program_space_and_thread (bl->pspace);
4047
4048 ret = remove_breakpoint_1 (bl, is);
4049
4050 do_cleanups (old_chain);
4051 return ret;
4052}
4053
4054/* Clear the "inserted" flag in all breakpoints. */
4055
4056void
4057mark_breakpoints_out (void)
4058{
4059 struct bp_location *bl, **blp_tmp;
4060
4061 ALL_BP_LOCATIONS (bl, blp_tmp)
4062 if (bl->pspace == current_program_space)
4063 bl->inserted = 0;
4064}
4065
4066/* Clear the "inserted" flag in all breakpoints and delete any
4067 breakpoints which should go away between runs of the program.
4068
4069 Plus other such housekeeping that has to be done for breakpoints
4070 between runs.
4071
4072 Note: this function gets called at the end of a run (by
4073 generic_mourn_inferior) and when a run begins (by
4074 init_wait_for_inferior). */
4075
4076
4077
4078void
4079breakpoint_init_inferior (enum inf_context context)
4080{
4081 struct breakpoint *b, *b_tmp;
4082 struct bp_location *bl, **blp_tmp;
4083 int ix;
4084 struct program_space *pspace = current_program_space;
4085
4086 /* If breakpoint locations are shared across processes, then there's
4087 nothing to do. */
4088 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4089 return;
4090
4091 ALL_BP_LOCATIONS (bl, blp_tmp)
4092 {
4093 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4094 if (bl->pspace == pspace
4095 && bl->owner->enable_state != bp_permanent)
4096 bl->inserted = 0;
4097 }
4098
4099 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4100 {
4101 if (b->loc && b->loc->pspace != pspace)
4102 continue;
4103
4104 switch (b->type)
4105 {
4106 case bp_call_dummy:
4107 case bp_longjmp_call_dummy:
4108
4109 /* If the call dummy breakpoint is at the entry point it will
4110 cause problems when the inferior is rerun, so we better get
4111 rid of it. */
4112
4113 case bp_watchpoint_scope:
4114
4115 /* Also get rid of scope breakpoints. */
4116
4117 case bp_shlib_event:
4118
4119 /* Also remove solib event breakpoints. Their addresses may
4120 have changed since the last time we ran the program.
4121 Actually we may now be debugging against different target;
4122 and so the solib backend that installed this breakpoint may
4123 not be used in by the target. E.g.,
4124
4125 (gdb) file prog-linux
4126 (gdb) run # native linux target
4127 ...
4128 (gdb) kill
4129 (gdb) file prog-win.exe
4130 (gdb) tar rem :9999 # remote Windows gdbserver.
4131 */
4132
4133 case bp_step_resume:
4134
4135 /* Also remove step-resume breakpoints. */
4136
4137 case bp_single_step:
4138
4139 /* Also remove single-step breakpoints. */
4140
4141 delete_breakpoint (b);
4142 break;
4143
4144 case bp_watchpoint:
4145 case bp_hardware_watchpoint:
4146 case bp_read_watchpoint:
4147 case bp_access_watchpoint:
4148 {
4149 struct watchpoint *w = (struct watchpoint *) b;
4150
4151 /* Likewise for watchpoints on local expressions. */
4152 if (w->exp_valid_block != NULL)
4153 delete_breakpoint (b);
4154 else if (context == inf_starting)
4155 {
4156 /* Reset val field to force reread of starting value in
4157 insert_breakpoints. */
4158 if (w->val)
4159 value_free (w->val);
4160 w->val = NULL;
4161 w->val_valid = 0;
4162 }
4163 }
4164 break;
4165 default:
4166 break;
4167 }
4168 }
4169
4170 /* Get rid of the moribund locations. */
4171 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4172 decref_bp_location (&bl);
4173 VEC_free (bp_location_p, moribund_locations);
4174}
4175
4176/* These functions concern about actual breakpoints inserted in the
4177 target --- to e.g. check if we need to do decr_pc adjustment or if
4178 we need to hop over the bkpt --- so we check for address space
4179 match, not program space. */
4180
4181/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4182 exists at PC. It returns ordinary_breakpoint_here if it's an
4183 ordinary breakpoint, or permanent_breakpoint_here if it's a
4184 permanent breakpoint.
4185 - When continuing from a location with an ordinary breakpoint, we
4186 actually single step once before calling insert_breakpoints.
4187 - When continuing from a location with a permanent breakpoint, we
4188 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4189 the target, to advance the PC past the breakpoint. */
4190
4191enum breakpoint_here
4192breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4193{
4194 struct bp_location *bl, **blp_tmp;
4195 int any_breakpoint_here = 0;
4196
4197 ALL_BP_LOCATIONS (bl, blp_tmp)
4198 {
4199 if (bl->loc_type != bp_loc_software_breakpoint
4200 && bl->loc_type != bp_loc_hardware_breakpoint)
4201 continue;
4202
4203 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4204 if ((breakpoint_enabled (bl->owner)
4205 || bl->owner->enable_state == bp_permanent)
4206 && breakpoint_location_address_match (bl, aspace, pc))
4207 {
4208 if (overlay_debugging
4209 && section_is_overlay (bl->section)
4210 && !section_is_mapped (bl->section))
4211 continue; /* unmapped overlay -- can't be a match */
4212 else if (bl->owner->enable_state == bp_permanent)
4213 return permanent_breakpoint_here;
4214 else
4215 any_breakpoint_here = 1;
4216 }
4217 }
4218
4219 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
4220}
4221
4222/* Return true if there's a moribund breakpoint at PC. */
4223
4224int
4225moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4226{
4227 struct bp_location *loc;
4228 int ix;
4229
4230 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4231 if (breakpoint_location_address_match (loc, aspace, pc))
4232 return 1;
4233
4234 return 0;
4235}
4236
4237/* Returns non-zero iff there's a breakpoint inserted at PC. */
4238
4239int
4240breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4241{
4242 struct bp_location *bl, **blp_tmp;
4243
4244 ALL_BP_LOCATIONS (bl, blp_tmp)
4245 {
4246 if (bl->loc_type != bp_loc_software_breakpoint
4247 && bl->loc_type != bp_loc_hardware_breakpoint)
4248 continue;
4249
4250 if (bl->inserted
4251 && breakpoint_location_address_match (bl, aspace, pc))
4252 {
4253 if (overlay_debugging
4254 && section_is_overlay (bl->section)
4255 && !section_is_mapped (bl->section))
4256 continue; /* unmapped overlay -- can't be a match */
4257 else
4258 return 1;
4259 }
4260 }
4261 return 0;
4262}
4263
4264/* This function returns non-zero iff there is a software breakpoint
4265 inserted at PC. */
4266
4267int
4268software_breakpoint_inserted_here_p (struct address_space *aspace,
4269 CORE_ADDR pc)
4270{
4271 struct bp_location *bl, **blp_tmp;
4272
4273 ALL_BP_LOCATIONS (bl, blp_tmp)
4274 {
4275 if (bl->loc_type != bp_loc_software_breakpoint)
4276 continue;
4277
4278 if (bl->inserted
4279 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4280 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
4287 return 1;
4288 }
4289 }
4290
4291 return 0;
4292}
4293
4294int
4295hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4296 CORE_ADDR addr, ULONGEST len)
4297{
4298 struct breakpoint *bpt;
4299
4300 ALL_BREAKPOINTS (bpt)
4301 {
4302 struct bp_location *loc;
4303
4304 if (bpt->type != bp_hardware_watchpoint
4305 && bpt->type != bp_access_watchpoint)
4306 continue;
4307
4308 if (!breakpoint_enabled (bpt))
4309 continue;
4310
4311 for (loc = bpt->loc; loc; loc = loc->next)
4312 if (loc->pspace->aspace == aspace && loc->inserted)
4313 {
4314 CORE_ADDR l, h;
4315
4316 /* Check for intersection. */
4317 l = max (loc->address, addr);
4318 h = min (loc->address + loc->length, addr + len);
4319 if (l < h)
4320 return 1;
4321 }
4322 }
4323 return 0;
4324}
4325\f
4326
4327/* bpstat stuff. External routines' interfaces are documented
4328 in breakpoint.h. */
4329
4330int
4331is_catchpoint (struct breakpoint *ep)
4332{
4333 return (ep->type == bp_catchpoint);
4334}
4335
4336/* Frees any storage that is part of a bpstat. Does not walk the
4337 'next' chain. */
4338
4339static void
4340bpstat_free (bpstat bs)
4341{
4342 if (bs->old_val != NULL)
4343 value_free (bs->old_val);
4344 decref_counted_command_line (&bs->commands);
4345 decref_bp_location (&bs->bp_location_at);
4346 xfree (bs);
4347}
4348
4349/* Clear a bpstat so that it says we are not at any breakpoint.
4350 Also free any storage that is part of a bpstat. */
4351
4352void
4353bpstat_clear (bpstat *bsp)
4354{
4355 bpstat p;
4356 bpstat q;
4357
4358 if (bsp == 0)
4359 return;
4360 p = *bsp;
4361 while (p != NULL)
4362 {
4363 q = p->next;
4364 bpstat_free (p);
4365 p = q;
4366 }
4367 *bsp = NULL;
4368}
4369
4370/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4371 is part of the bpstat is copied as well. */
4372
4373bpstat
4374bpstat_copy (bpstat bs)
4375{
4376 bpstat p = NULL;
4377 bpstat tmp;
4378 bpstat retval = NULL;
4379
4380 if (bs == NULL)
4381 return bs;
4382
4383 for (; bs != NULL; bs = bs->next)
4384 {
4385 tmp = (bpstat) xmalloc (sizeof (*tmp));
4386 memcpy (tmp, bs, sizeof (*tmp));
4387 incref_counted_command_line (tmp->commands);
4388 incref_bp_location (tmp->bp_location_at);
4389 if (bs->old_val != NULL)
4390 {
4391 tmp->old_val = value_copy (bs->old_val);
4392 release_value (tmp->old_val);
4393 }
4394
4395 if (p == NULL)
4396 /* This is the first thing in the chain. */
4397 retval = tmp;
4398 else
4399 p->next = tmp;
4400 p = tmp;
4401 }
4402 p->next = NULL;
4403 return retval;
4404}
4405
4406/* Find the bpstat associated with this breakpoint. */
4407
4408bpstat
4409bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4410{
4411 if (bsp == NULL)
4412 return NULL;
4413
4414 for (; bsp != NULL; bsp = bsp->next)
4415 {
4416 if (bsp->breakpoint_at == breakpoint)
4417 return bsp;
4418 }
4419 return NULL;
4420}
4421
4422/* See breakpoint.h. */
4423
4424int
4425bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4426{
4427 for (; bsp != NULL; bsp = bsp->next)
4428 {
4429 if (bsp->breakpoint_at == NULL)
4430 {
4431 /* A moribund location can never explain a signal other than
4432 GDB_SIGNAL_TRAP. */
4433 if (sig == GDB_SIGNAL_TRAP)
4434 return 1;
4435 }
4436 else
4437 {
4438 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4439 sig))
4440 return 1;
4441 }
4442 }
4443
4444 return 0;
4445}
4446
4447/* Put in *NUM the breakpoint number of the first breakpoint we are
4448 stopped at. *BSP upon return is a bpstat which points to the
4449 remaining breakpoints stopped at (but which is not guaranteed to be
4450 good for anything but further calls to bpstat_num).
4451
4452 Return 0 if passed a bpstat which does not indicate any breakpoints.
4453 Return -1 if stopped at a breakpoint that has been deleted since
4454 we set it.
4455 Return 1 otherwise. */
4456
4457int
4458bpstat_num (bpstat *bsp, int *num)
4459{
4460 struct breakpoint *b;
4461
4462 if ((*bsp) == NULL)
4463 return 0; /* No more breakpoint values */
4464
4465 /* We assume we'll never have several bpstats that correspond to a
4466 single breakpoint -- otherwise, this function might return the
4467 same number more than once and this will look ugly. */
4468 b = (*bsp)->breakpoint_at;
4469 *bsp = (*bsp)->next;
4470 if (b == NULL)
4471 return -1; /* breakpoint that's been deleted since */
4472
4473 *num = b->number; /* We have its number */
4474 return 1;
4475}
4476
4477/* See breakpoint.h. */
4478
4479void
4480bpstat_clear_actions (void)
4481{
4482 struct thread_info *tp;
4483 bpstat bs;
4484
4485 if (ptid_equal (inferior_ptid, null_ptid))
4486 return;
4487
4488 tp = find_thread_ptid (inferior_ptid);
4489 if (tp == NULL)
4490 return;
4491
4492 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4493 {
4494 decref_counted_command_line (&bs->commands);
4495
4496 if (bs->old_val != NULL)
4497 {
4498 value_free (bs->old_val);
4499 bs->old_val = NULL;
4500 }
4501 }
4502}
4503
4504/* Called when a command is about to proceed the inferior. */
4505
4506static void
4507breakpoint_about_to_proceed (void)
4508{
4509 if (!ptid_equal (inferior_ptid, null_ptid))
4510 {
4511 struct thread_info *tp = inferior_thread ();
4512
4513 /* Allow inferior function calls in breakpoint commands to not
4514 interrupt the command list. When the call finishes
4515 successfully, the inferior will be standing at the same
4516 breakpoint as if nothing happened. */
4517 if (tp->control.in_infcall)
4518 return;
4519 }
4520
4521 breakpoint_proceeded = 1;
4522}
4523
4524/* Stub for cleaning up our state if we error-out of a breakpoint
4525 command. */
4526static void
4527cleanup_executing_breakpoints (void *ignore)
4528{
4529 executing_breakpoint_commands = 0;
4530}
4531
4532/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4533 or its equivalent. */
4534
4535static int
4536command_line_is_silent (struct command_line *cmd)
4537{
4538 return cmd && (strcmp ("silent", cmd->line) == 0
4539 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4540}
4541
4542/* Execute all the commands associated with all the breakpoints at
4543 this location. Any of these commands could cause the process to
4544 proceed beyond this point, etc. We look out for such changes by
4545 checking the global "breakpoint_proceeded" after each command.
4546
4547 Returns true if a breakpoint command resumed the inferior. In that
4548 case, it is the caller's responsibility to recall it again with the
4549 bpstat of the current thread. */
4550
4551static int
4552bpstat_do_actions_1 (bpstat *bsp)
4553{
4554 bpstat bs;
4555 struct cleanup *old_chain;
4556 int again = 0;
4557
4558 /* Avoid endless recursion if a `source' command is contained
4559 in bs->commands. */
4560 if (executing_breakpoint_commands)
4561 return 0;
4562
4563 executing_breakpoint_commands = 1;
4564 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4565
4566 prevent_dont_repeat ();
4567
4568 /* This pointer will iterate over the list of bpstat's. */
4569 bs = *bsp;
4570
4571 breakpoint_proceeded = 0;
4572 for (; bs != NULL; bs = bs->next)
4573 {
4574 struct counted_command_line *ccmd;
4575 struct command_line *cmd;
4576 struct cleanup *this_cmd_tree_chain;
4577
4578 /* Take ownership of the BSP's command tree, if it has one.
4579
4580 The command tree could legitimately contain commands like
4581 'step' and 'next', which call clear_proceed_status, which
4582 frees stop_bpstat's command tree. To make sure this doesn't
4583 free the tree we're executing out from under us, we need to
4584 take ownership of the tree ourselves. Since a given bpstat's
4585 commands are only executed once, we don't need to copy it; we
4586 can clear the pointer in the bpstat, and make sure we free
4587 the tree when we're done. */
4588 ccmd = bs->commands;
4589 bs->commands = NULL;
4590 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4591 cmd = ccmd ? ccmd->commands : NULL;
4592 if (command_line_is_silent (cmd))
4593 {
4594 /* The action has been already done by bpstat_stop_status. */
4595 cmd = cmd->next;
4596 }
4597
4598 while (cmd != NULL)
4599 {
4600 execute_control_command (cmd);
4601
4602 if (breakpoint_proceeded)
4603 break;
4604 else
4605 cmd = cmd->next;
4606 }
4607
4608 /* We can free this command tree now. */
4609 do_cleanups (this_cmd_tree_chain);
4610
4611 if (breakpoint_proceeded)
4612 {
4613 if (target_can_async_p ())
4614 /* If we are in async mode, then the target might be still
4615 running, not stopped at any breakpoint, so nothing for
4616 us to do here -- just return to the event loop. */
4617 ;
4618 else
4619 /* In sync mode, when execute_control_command returns
4620 we're already standing on the next breakpoint.
4621 Breakpoint commands for that stop were not run, since
4622 execute_command does not run breakpoint commands --
4623 only command_line_handler does, but that one is not
4624 involved in execution of breakpoint commands. So, we
4625 can now execute breakpoint commands. It should be
4626 noted that making execute_command do bpstat actions is
4627 not an option -- in this case we'll have recursive
4628 invocation of bpstat for each breakpoint with a
4629 command, and can easily blow up GDB stack. Instead, we
4630 return true, which will trigger the caller to recall us
4631 with the new stop_bpstat. */
4632 again = 1;
4633 break;
4634 }
4635 }
4636 do_cleanups (old_chain);
4637 return again;
4638}
4639
4640void
4641bpstat_do_actions (void)
4642{
4643 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4644
4645 /* Do any commands attached to breakpoint we are stopped at. */
4646 while (!ptid_equal (inferior_ptid, null_ptid)
4647 && target_has_execution
4648 && !is_exited (inferior_ptid)
4649 && !is_executing (inferior_ptid))
4650 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4651 and only return when it is stopped at the next breakpoint, we
4652 keep doing breakpoint actions until it returns false to
4653 indicate the inferior was not resumed. */
4654 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4655 break;
4656
4657 discard_cleanups (cleanup_if_error);
4658}
4659
4660/* Print out the (old or new) value associated with a watchpoint. */
4661
4662static void
4663watchpoint_value_print (struct value *val, struct ui_file *stream)
4664{
4665 if (val == NULL)
4666 fprintf_unfiltered (stream, _("<unreadable>"));
4667 else
4668 {
4669 struct value_print_options opts;
4670 get_user_print_options (&opts);
4671 value_print (val, stream, &opts);
4672 }
4673}
4674
4675/* Generic routine for printing messages indicating why we
4676 stopped. The behavior of this function depends on the value
4677 'print_it' in the bpstat structure. Under some circumstances we
4678 may decide not to print anything here and delegate the task to
4679 normal_stop(). */
4680
4681static enum print_stop_action
4682print_bp_stop_message (bpstat bs)
4683{
4684 switch (bs->print_it)
4685 {
4686 case print_it_noop:
4687 /* Nothing should be printed for this bpstat entry. */
4688 return PRINT_UNKNOWN;
4689 break;
4690
4691 case print_it_done:
4692 /* We still want to print the frame, but we already printed the
4693 relevant messages. */
4694 return PRINT_SRC_AND_LOC;
4695 break;
4696
4697 case print_it_normal:
4698 {
4699 struct breakpoint *b = bs->breakpoint_at;
4700
4701 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4702 which has since been deleted. */
4703 if (b == NULL)
4704 return PRINT_UNKNOWN;
4705
4706 /* Normal case. Call the breakpoint's print_it method. */
4707 return b->ops->print_it (bs);
4708 }
4709 break;
4710
4711 default:
4712 internal_error (__FILE__, __LINE__,
4713 _("print_bp_stop_message: unrecognized enum value"));
4714 break;
4715 }
4716}
4717
4718/* A helper function that prints a shared library stopped event. */
4719
4720static void
4721print_solib_event (int is_catchpoint)
4722{
4723 int any_deleted
4724 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4725 int any_added
4726 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4727
4728 if (!is_catchpoint)
4729 {
4730 if (any_added || any_deleted)
4731 ui_out_text (current_uiout,
4732 _("Stopped due to shared library event:\n"));
4733 else
4734 ui_out_text (current_uiout,
4735 _("Stopped due to shared library event (no "
4736 "libraries added or removed)\n"));
4737 }
4738
4739 if (ui_out_is_mi_like_p (current_uiout))
4740 ui_out_field_string (current_uiout, "reason",
4741 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4742
4743 if (any_deleted)
4744 {
4745 struct cleanup *cleanup;
4746 char *name;
4747 int ix;
4748
4749 ui_out_text (current_uiout, _(" Inferior unloaded "));
4750 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4751 "removed");
4752 for (ix = 0;
4753 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4754 ix, name);
4755 ++ix)
4756 {
4757 if (ix > 0)
4758 ui_out_text (current_uiout, " ");
4759 ui_out_field_string (current_uiout, "library", name);
4760 ui_out_text (current_uiout, "\n");
4761 }
4762
4763 do_cleanups (cleanup);
4764 }
4765
4766 if (any_added)
4767 {
4768 struct so_list *iter;
4769 int ix;
4770 struct cleanup *cleanup;
4771
4772 ui_out_text (current_uiout, _(" Inferior loaded "));
4773 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4774 "added");
4775 for (ix = 0;
4776 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4777 ix, iter);
4778 ++ix)
4779 {
4780 if (ix > 0)
4781 ui_out_text (current_uiout, " ");
4782 ui_out_field_string (current_uiout, "library", iter->so_name);
4783 ui_out_text (current_uiout, "\n");
4784 }
4785
4786 do_cleanups (cleanup);
4787 }
4788}
4789
4790/* Print a message indicating what happened. This is called from
4791 normal_stop(). The input to this routine is the head of the bpstat
4792 list - a list of the eventpoints that caused this stop. KIND is
4793 the target_waitkind for the stopping event. This
4794 routine calls the generic print routine for printing a message
4795 about reasons for stopping. This will print (for example) the
4796 "Breakpoint n," part of the output. The return value of this
4797 routine is one of:
4798
4799 PRINT_UNKNOWN: Means we printed nothing.
4800 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4801 code to print the location. An example is
4802 "Breakpoint 1, " which should be followed by
4803 the location.
4804 PRINT_SRC_ONLY: Means we printed something, but there is no need
4805 to also print the location part of the message.
4806 An example is the catch/throw messages, which
4807 don't require a location appended to the end.
4808 PRINT_NOTHING: We have done some printing and we don't need any
4809 further info to be printed. */
4810
4811enum print_stop_action
4812bpstat_print (bpstat bs, int kind)
4813{
4814 int val;
4815
4816 /* Maybe another breakpoint in the chain caused us to stop.
4817 (Currently all watchpoints go on the bpstat whether hit or not.
4818 That probably could (should) be changed, provided care is taken
4819 with respect to bpstat_explains_signal). */
4820 for (; bs; bs = bs->next)
4821 {
4822 val = print_bp_stop_message (bs);
4823 if (val == PRINT_SRC_ONLY
4824 || val == PRINT_SRC_AND_LOC
4825 || val == PRINT_NOTHING)
4826 return val;
4827 }
4828
4829 /* If we had hit a shared library event breakpoint,
4830 print_bp_stop_message would print out this message. If we hit an
4831 OS-level shared library event, do the same thing. */
4832 if (kind == TARGET_WAITKIND_LOADED)
4833 {
4834 print_solib_event (0);
4835 return PRINT_NOTHING;
4836 }
4837
4838 /* We reached the end of the chain, or we got a null BS to start
4839 with and nothing was printed. */
4840 return PRINT_UNKNOWN;
4841}
4842
4843/* Evaluate the expression EXP and return 1 if value is zero.
4844 This returns the inverse of the condition because it is called
4845 from catch_errors which returns 0 if an exception happened, and if an
4846 exception happens we want execution to stop.
4847 The argument is a "struct expression *" that has been cast to a
4848 "void *" to make it pass through catch_errors. */
4849
4850static int
4851breakpoint_cond_eval (void *exp)
4852{
4853 struct value *mark = value_mark ();
4854 int i = !value_true (evaluate_expression ((struct expression *) exp));
4855
4856 value_free_to_mark (mark);
4857 return i;
4858}
4859
4860/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4861
4862static bpstat
4863bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4864{
4865 bpstat bs;
4866
4867 bs = (bpstat) xmalloc (sizeof (*bs));
4868 bs->next = NULL;
4869 **bs_link_pointer = bs;
4870 *bs_link_pointer = &bs->next;
4871 bs->breakpoint_at = bl->owner;
4872 bs->bp_location_at = bl;
4873 incref_bp_location (bl);
4874 /* If the condition is false, etc., don't do the commands. */
4875 bs->commands = NULL;
4876 bs->old_val = NULL;
4877 bs->print_it = print_it_normal;
4878 return bs;
4879}
4880\f
4881/* The target has stopped with waitstatus WS. Check if any hardware
4882 watchpoints have triggered, according to the target. */
4883
4884int
4885watchpoints_triggered (struct target_waitstatus *ws)
4886{
4887 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4888 CORE_ADDR addr;
4889 struct breakpoint *b;
4890
4891 if (!stopped_by_watchpoint)
4892 {
4893 /* We were not stopped by a watchpoint. Mark all watchpoints
4894 as not triggered. */
4895 ALL_BREAKPOINTS (b)
4896 if (is_hardware_watchpoint (b))
4897 {
4898 struct watchpoint *w = (struct watchpoint *) b;
4899
4900 w->watchpoint_triggered = watch_triggered_no;
4901 }
4902
4903 return 0;
4904 }
4905
4906 if (!target_stopped_data_address (&current_target, &addr))
4907 {
4908 /* We were stopped by a watchpoint, but we don't know where.
4909 Mark all watchpoints as unknown. */
4910 ALL_BREAKPOINTS (b)
4911 if (is_hardware_watchpoint (b))
4912 {
4913 struct watchpoint *w = (struct watchpoint *) b;
4914
4915 w->watchpoint_triggered = watch_triggered_unknown;
4916 }
4917
4918 return 1;
4919 }
4920
4921 /* The target could report the data address. Mark watchpoints
4922 affected by this data address as triggered, and all others as not
4923 triggered. */
4924
4925 ALL_BREAKPOINTS (b)
4926 if (is_hardware_watchpoint (b))
4927 {
4928 struct watchpoint *w = (struct watchpoint *) b;
4929 struct bp_location *loc;
4930
4931 w->watchpoint_triggered = watch_triggered_no;
4932 for (loc = b->loc; loc; loc = loc->next)
4933 {
4934 if (is_masked_watchpoint (b))
4935 {
4936 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4937 CORE_ADDR start = loc->address & w->hw_wp_mask;
4938
4939 if (newaddr == start)
4940 {
4941 w->watchpoint_triggered = watch_triggered_yes;
4942 break;
4943 }
4944 }
4945 /* Exact match not required. Within range is sufficient. */
4946 else if (target_watchpoint_addr_within_range (&current_target,
4947 addr, loc->address,
4948 loc->length))
4949 {
4950 w->watchpoint_triggered = watch_triggered_yes;
4951 break;
4952 }
4953 }
4954 }
4955
4956 return 1;
4957}
4958
4959/* Possible return values for watchpoint_check (this can't be an enum
4960 because of check_errors). */
4961/* The watchpoint has been deleted. */
4962#define WP_DELETED 1
4963/* The value has changed. */
4964#define WP_VALUE_CHANGED 2
4965/* The value has not changed. */
4966#define WP_VALUE_NOT_CHANGED 3
4967/* Ignore this watchpoint, no matter if the value changed or not. */
4968#define WP_IGNORE 4
4969
4970#define BP_TEMPFLAG 1
4971#define BP_HARDWAREFLAG 2
4972
4973/* Evaluate watchpoint condition expression and check if its value
4974 changed.
4975
4976 P should be a pointer to struct bpstat, but is defined as a void *
4977 in order for this function to be usable with catch_errors. */
4978
4979static int
4980watchpoint_check (void *p)
4981{
4982 bpstat bs = (bpstat) p;
4983 struct watchpoint *b;
4984 struct frame_info *fr;
4985 int within_current_scope;
4986
4987 /* BS is built from an existing struct breakpoint. */
4988 gdb_assert (bs->breakpoint_at != NULL);
4989 b = (struct watchpoint *) bs->breakpoint_at;
4990
4991 /* If this is a local watchpoint, we only want to check if the
4992 watchpoint frame is in scope if the current thread is the thread
4993 that was used to create the watchpoint. */
4994 if (!watchpoint_in_thread_scope (b))
4995 return WP_IGNORE;
4996
4997 if (b->exp_valid_block == NULL)
4998 within_current_scope = 1;
4999 else
5000 {
5001 struct frame_info *frame = get_current_frame ();
5002 struct gdbarch *frame_arch = get_frame_arch (frame);
5003 CORE_ADDR frame_pc = get_frame_pc (frame);
5004
5005 /* in_function_epilogue_p() returns a non-zero value if we're
5006 still in the function but the stack frame has already been
5007 invalidated. Since we can't rely on the values of local
5008 variables after the stack has been destroyed, we are treating
5009 the watchpoint in that state as `not changed' without further
5010 checking. Don't mark watchpoints as changed if the current
5011 frame is in an epilogue - even if they are in some other
5012 frame, our view of the stack is likely to be wrong and
5013 frame_find_by_id could error out. */
5014 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
5015 return WP_IGNORE;
5016
5017 fr = frame_find_by_id (b->watchpoint_frame);
5018 within_current_scope = (fr != NULL);
5019
5020 /* If we've gotten confused in the unwinder, we might have
5021 returned a frame that can't describe this variable. */
5022 if (within_current_scope)
5023 {
5024 struct symbol *function;
5025
5026 function = get_frame_function (fr);
5027 if (function == NULL
5028 || !contained_in (b->exp_valid_block,
5029 SYMBOL_BLOCK_VALUE (function)))
5030 within_current_scope = 0;
5031 }
5032
5033 if (within_current_scope)
5034 /* If we end up stopping, the current frame will get selected
5035 in normal_stop. So this call to select_frame won't affect
5036 the user. */
5037 select_frame (fr);
5038 }
5039
5040 if (within_current_scope)
5041 {
5042 /* We use value_{,free_to_}mark because it could be a *long*
5043 time before we return to the command level and call
5044 free_all_values. We can't call free_all_values because we
5045 might be in the middle of evaluating a function call. */
5046
5047 int pc = 0;
5048 struct value *mark;
5049 struct value *new_val;
5050
5051 if (is_masked_watchpoint (&b->base))
5052 /* Since we don't know the exact trigger address (from
5053 stopped_data_address), just tell the user we've triggered
5054 a mask watchpoint. */
5055 return WP_VALUE_CHANGED;
5056
5057 mark = value_mark ();
5058 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5059
5060 if (b->val_bitsize != 0)
5061 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5062
5063 /* We use value_equal_contents instead of value_equal because
5064 the latter coerces an array to a pointer, thus comparing just
5065 the address of the array instead of its contents. This is
5066 not what we want. */
5067 if ((b->val != NULL) != (new_val != NULL)
5068 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5069 {
5070 if (new_val != NULL)
5071 {
5072 release_value (new_val);
5073 value_free_to_mark (mark);
5074 }
5075 bs->old_val = b->val;
5076 b->val = new_val;
5077 b->val_valid = 1;
5078 return WP_VALUE_CHANGED;
5079 }
5080 else
5081 {
5082 /* Nothing changed. */
5083 value_free_to_mark (mark);
5084 return WP_VALUE_NOT_CHANGED;
5085 }
5086 }
5087 else
5088 {
5089 struct ui_out *uiout = current_uiout;
5090
5091 /* This seems like the only logical thing to do because
5092 if we temporarily ignored the watchpoint, then when
5093 we reenter the block in which it is valid it contains
5094 garbage (in the case of a function, it may have two
5095 garbage values, one before and one after the prologue).
5096 So we can't even detect the first assignment to it and
5097 watch after that (since the garbage may or may not equal
5098 the first value assigned). */
5099 /* We print all the stop information in
5100 breakpoint_ops->print_it, but in this case, by the time we
5101 call breakpoint_ops->print_it this bp will be deleted
5102 already. So we have no choice but print the information
5103 here. */
5104 if (ui_out_is_mi_like_p (uiout))
5105 ui_out_field_string
5106 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5107 ui_out_text (uiout, "\nWatchpoint ");
5108 ui_out_field_int (uiout, "wpnum", b->base.number);
5109 ui_out_text (uiout,
5110 " deleted because the program has left the block in\n\
5111which its expression is valid.\n");
5112
5113 /* Make sure the watchpoint's commands aren't executed. */
5114 decref_counted_command_line (&b->base.commands);
5115 watchpoint_del_at_next_stop (b);
5116
5117 return WP_DELETED;
5118 }
5119}
5120
5121/* Return true if it looks like target has stopped due to hitting
5122 breakpoint location BL. This function does not check if we should
5123 stop, only if BL explains the stop. */
5124
5125static int
5126bpstat_check_location (const struct bp_location *bl,
5127 struct address_space *aspace, CORE_ADDR bp_addr,
5128 const struct target_waitstatus *ws)
5129{
5130 struct breakpoint *b = bl->owner;
5131
5132 /* BL is from an existing breakpoint. */
5133 gdb_assert (b != NULL);
5134
5135 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5136}
5137
5138/* Determine if the watched values have actually changed, and we
5139 should stop. If not, set BS->stop to 0. */
5140
5141static void
5142bpstat_check_watchpoint (bpstat bs)
5143{
5144 const struct bp_location *bl;
5145 struct watchpoint *b;
5146
5147 /* BS is built for existing struct breakpoint. */
5148 bl = bs->bp_location_at;
5149 gdb_assert (bl != NULL);
5150 b = (struct watchpoint *) bs->breakpoint_at;
5151 gdb_assert (b != NULL);
5152
5153 {
5154 int must_check_value = 0;
5155
5156 if (b->base.type == bp_watchpoint)
5157 /* For a software watchpoint, we must always check the
5158 watched value. */
5159 must_check_value = 1;
5160 else if (b->watchpoint_triggered == watch_triggered_yes)
5161 /* We have a hardware watchpoint (read, write, or access)
5162 and the target earlier reported an address watched by
5163 this watchpoint. */
5164 must_check_value = 1;
5165 else if (b->watchpoint_triggered == watch_triggered_unknown
5166 && b->base.type == bp_hardware_watchpoint)
5167 /* We were stopped by a hardware watchpoint, but the target could
5168 not report the data address. We must check the watchpoint's
5169 value. Access and read watchpoints are out of luck; without
5170 a data address, we can't figure it out. */
5171 must_check_value = 1;
5172
5173 if (must_check_value)
5174 {
5175 char *message
5176 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5177 b->base.number);
5178 struct cleanup *cleanups = make_cleanup (xfree, message);
5179 int e = catch_errors (watchpoint_check, bs, message,
5180 RETURN_MASK_ALL);
5181 do_cleanups (cleanups);
5182 switch (e)
5183 {
5184 case WP_DELETED:
5185 /* We've already printed what needs to be printed. */
5186 bs->print_it = print_it_done;
5187 /* Stop. */
5188 break;
5189 case WP_IGNORE:
5190 bs->print_it = print_it_noop;
5191 bs->stop = 0;
5192 break;
5193 case WP_VALUE_CHANGED:
5194 if (b->base.type == bp_read_watchpoint)
5195 {
5196 /* There are two cases to consider here:
5197
5198 1. We're watching the triggered memory for reads.
5199 In that case, trust the target, and always report
5200 the watchpoint hit to the user. Even though
5201 reads don't cause value changes, the value may
5202 have changed since the last time it was read, and
5203 since we're not trapping writes, we will not see
5204 those, and as such we should ignore our notion of
5205 old value.
5206
5207 2. We're watching the triggered memory for both
5208 reads and writes. There are two ways this may
5209 happen:
5210
5211 2.1. This is a target that can't break on data
5212 reads only, but can break on accesses (reads or
5213 writes), such as e.g., x86. We detect this case
5214 at the time we try to insert read watchpoints.
5215
5216 2.2. Otherwise, the target supports read
5217 watchpoints, but, the user set an access or write
5218 watchpoint watching the same memory as this read
5219 watchpoint.
5220
5221 If we're watching memory writes as well as reads,
5222 ignore watchpoint hits when we find that the
5223 value hasn't changed, as reads don't cause
5224 changes. This still gives false positives when
5225 the program writes the same value to memory as
5226 what there was already in memory (we will confuse
5227 it for a read), but it's much better than
5228 nothing. */
5229
5230 int other_write_watchpoint = 0;
5231
5232 if (bl->watchpoint_type == hw_read)
5233 {
5234 struct breakpoint *other_b;
5235
5236 ALL_BREAKPOINTS (other_b)
5237 if (other_b->type == bp_hardware_watchpoint
5238 || other_b->type == bp_access_watchpoint)
5239 {
5240 struct watchpoint *other_w =
5241 (struct watchpoint *) other_b;
5242
5243 if (other_w->watchpoint_triggered
5244 == watch_triggered_yes)
5245 {
5246 other_write_watchpoint = 1;
5247 break;
5248 }
5249 }
5250 }
5251
5252 if (other_write_watchpoint
5253 || bl->watchpoint_type == hw_access)
5254 {
5255 /* We're watching the same memory for writes,
5256 and the value changed since the last time we
5257 updated it, so this trap must be for a write.
5258 Ignore it. */
5259 bs->print_it = print_it_noop;
5260 bs->stop = 0;
5261 }
5262 }
5263 break;
5264 case WP_VALUE_NOT_CHANGED:
5265 if (b->base.type == bp_hardware_watchpoint
5266 || b->base.type == bp_watchpoint)
5267 {
5268 /* Don't stop: write watchpoints shouldn't fire if
5269 the value hasn't changed. */
5270 bs->print_it = print_it_noop;
5271 bs->stop = 0;
5272 }
5273 /* Stop. */
5274 break;
5275 default:
5276 /* Can't happen. */
5277 case 0:
5278 /* Error from catch_errors. */
5279 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5280 watchpoint_del_at_next_stop (b);
5281 /* We've already printed what needs to be printed. */
5282 bs->print_it = print_it_done;
5283 break;
5284 }
5285 }
5286 else /* must_check_value == 0 */
5287 {
5288 /* This is a case where some watchpoint(s) triggered, but
5289 not at the address of this watchpoint, or else no
5290 watchpoint triggered after all. So don't print
5291 anything for this watchpoint. */
5292 bs->print_it = print_it_noop;
5293 bs->stop = 0;
5294 }
5295 }
5296}
5297
5298/* For breakpoints that are currently marked as telling gdb to stop,
5299 check conditions (condition proper, frame, thread and ignore count)
5300 of breakpoint referred to by BS. If we should not stop for this
5301 breakpoint, set BS->stop to 0. */
5302
5303static void
5304bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5305{
5306 const struct bp_location *bl;
5307 struct breakpoint *b;
5308 int value_is_zero = 0;
5309 struct expression *cond;
5310
5311 gdb_assert (bs->stop);
5312
5313 /* BS is built for existing struct breakpoint. */
5314 bl = bs->bp_location_at;
5315 gdb_assert (bl != NULL);
5316 b = bs->breakpoint_at;
5317 gdb_assert (b != NULL);
5318
5319 /* Even if the target evaluated the condition on its end and notified GDB, we
5320 need to do so again since GDB does not know if we stopped due to a
5321 breakpoint or a single step breakpoint. */
5322
5323 if (frame_id_p (b->frame_id)
5324 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5325 {
5326 bs->stop = 0;
5327 return;
5328 }
5329
5330 /* If this is a thread/task-specific breakpoint, don't waste cpu
5331 evaluating the condition if this isn't the specified
5332 thread/task. */
5333 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5334 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5335
5336 {
5337 bs->stop = 0;
5338 return;
5339 }
5340
5341 /* Evaluate extension language breakpoints that have a "stop" method
5342 implemented. */
5343 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5344
5345 if (is_watchpoint (b))
5346 {
5347 struct watchpoint *w = (struct watchpoint *) b;
5348
5349 cond = w->cond_exp;
5350 }
5351 else
5352 cond = bl->cond;
5353
5354 if (cond && b->disposition != disp_del_at_next_stop)
5355 {
5356 int within_current_scope = 1;
5357 struct watchpoint * w;
5358
5359 /* We use value_mark and value_free_to_mark because it could
5360 be a long time before we return to the command level and
5361 call free_all_values. We can't call free_all_values
5362 because we might be in the middle of evaluating a
5363 function call. */
5364 struct value *mark = value_mark ();
5365
5366 if (is_watchpoint (b))
5367 w = (struct watchpoint *) b;
5368 else
5369 w = NULL;
5370
5371 /* Need to select the frame, with all that implies so that
5372 the conditions will have the right context. Because we
5373 use the frame, we will not see an inlined function's
5374 variables when we arrive at a breakpoint at the start
5375 of the inlined function; the current frame will be the
5376 call site. */
5377 if (w == NULL || w->cond_exp_valid_block == NULL)
5378 select_frame (get_current_frame ());
5379 else
5380 {
5381 struct frame_info *frame;
5382
5383 /* For local watchpoint expressions, which particular
5384 instance of a local is being watched matters, so we
5385 keep track of the frame to evaluate the expression
5386 in. To evaluate the condition however, it doesn't
5387 really matter which instantiation of the function
5388 where the condition makes sense triggers the
5389 watchpoint. This allows an expression like "watch
5390 global if q > 10" set in `func', catch writes to
5391 global on all threads that call `func', or catch
5392 writes on all recursive calls of `func' by a single
5393 thread. We simply always evaluate the condition in
5394 the innermost frame that's executing where it makes
5395 sense to evaluate the condition. It seems
5396 intuitive. */
5397 frame = block_innermost_frame (w->cond_exp_valid_block);
5398 if (frame != NULL)
5399 select_frame (frame);
5400 else
5401 within_current_scope = 0;
5402 }
5403 if (within_current_scope)
5404 value_is_zero
5405 = catch_errors (breakpoint_cond_eval, cond,
5406 "Error in testing breakpoint condition:\n",
5407 RETURN_MASK_ALL);
5408 else
5409 {
5410 warning (_("Watchpoint condition cannot be tested "
5411 "in the current scope"));
5412 /* If we failed to set the right context for this
5413 watchpoint, unconditionally report it. */
5414 value_is_zero = 0;
5415 }
5416 /* FIXME-someday, should give breakpoint #. */
5417 value_free_to_mark (mark);
5418 }
5419
5420 if (cond && value_is_zero)
5421 {
5422 bs->stop = 0;
5423 }
5424 else if (b->ignore_count > 0)
5425 {
5426 b->ignore_count--;
5427 bs->stop = 0;
5428 /* Increase the hit count even though we don't stop. */
5429 ++(b->hit_count);
5430 observer_notify_breakpoint_modified (b);
5431 }
5432}
5433
5434
5435/* Get a bpstat associated with having just stopped at address
5436 BP_ADDR in thread PTID.
5437
5438 Determine whether we stopped at a breakpoint, etc, or whether we
5439 don't understand this stop. Result is a chain of bpstat's such
5440 that:
5441
5442 if we don't understand the stop, the result is a null pointer.
5443
5444 if we understand why we stopped, the result is not null.
5445
5446 Each element of the chain refers to a particular breakpoint or
5447 watchpoint at which we have stopped. (We may have stopped for
5448 several reasons concurrently.)
5449
5450 Each element of the chain has valid next, breakpoint_at,
5451 commands, FIXME??? fields. */
5452
5453bpstat
5454bpstat_stop_status (struct address_space *aspace,
5455 CORE_ADDR bp_addr, ptid_t ptid,
5456 const struct target_waitstatus *ws)
5457{
5458 struct breakpoint *b = NULL;
5459 struct bp_location *bl;
5460 struct bp_location *loc;
5461 /* First item of allocated bpstat's. */
5462 bpstat bs_head = NULL, *bs_link = &bs_head;
5463 /* Pointer to the last thing in the chain currently. */
5464 bpstat bs;
5465 int ix;
5466 int need_remove_insert;
5467 int removed_any;
5468
5469 /* First, build the bpstat chain with locations that explain a
5470 target stop, while being careful to not set the target running,
5471 as that may invalidate locations (in particular watchpoint
5472 locations are recreated). Resuming will happen here with
5473 breakpoint conditions or watchpoint expressions that include
5474 inferior function calls. */
5475
5476 ALL_BREAKPOINTS (b)
5477 {
5478 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5479 continue;
5480
5481 for (bl = b->loc; bl != NULL; bl = bl->next)
5482 {
5483 /* For hardware watchpoints, we look only at the first
5484 location. The watchpoint_check function will work on the
5485 entire expression, not the individual locations. For
5486 read watchpoints, the watchpoints_triggered function has
5487 checked all locations already. */
5488 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5489 break;
5490
5491 if (!bl->enabled || bl->shlib_disabled)
5492 continue;
5493
5494 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5495 continue;
5496
5497 /* Come here if it's a watchpoint, or if the break address
5498 matches. */
5499
5500 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5501 explain stop. */
5502
5503 /* Assume we stop. Should we find a watchpoint that is not
5504 actually triggered, or if the condition of the breakpoint
5505 evaluates as false, we'll reset 'stop' to 0. */
5506 bs->stop = 1;
5507 bs->print = 1;
5508
5509 /* If this is a scope breakpoint, mark the associated
5510 watchpoint as triggered so that we will handle the
5511 out-of-scope event. We'll get to the watchpoint next
5512 iteration. */
5513 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5514 {
5515 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5516
5517 w->watchpoint_triggered = watch_triggered_yes;
5518 }
5519 }
5520 }
5521
5522 /* Check if a moribund breakpoint explains the stop. */
5523 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5524 {
5525 if (breakpoint_location_address_match (loc, aspace, bp_addr))
5526 {
5527 bs = bpstat_alloc (loc, &bs_link);
5528 /* For hits of moribund locations, we should just proceed. */
5529 bs->stop = 0;
5530 bs->print = 0;
5531 bs->print_it = print_it_noop;
5532 }
5533 }
5534
5535 /* A bit of special processing for shlib breakpoints. We need to
5536 process solib loading here, so that the lists of loaded and
5537 unloaded libraries are correct before we handle "catch load" and
5538 "catch unload". */
5539 for (bs = bs_head; bs != NULL; bs = bs->next)
5540 {
5541 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5542 {
5543 handle_solib_event ();
5544 break;
5545 }
5546 }
5547
5548 /* Now go through the locations that caused the target to stop, and
5549 check whether we're interested in reporting this stop to higher
5550 layers, or whether we should resume the target transparently. */
5551
5552 removed_any = 0;
5553
5554 for (bs = bs_head; bs != NULL; bs = bs->next)
5555 {
5556 if (!bs->stop)
5557 continue;
5558
5559 b = bs->breakpoint_at;
5560 b->ops->check_status (bs);
5561 if (bs->stop)
5562 {
5563 bpstat_check_breakpoint_conditions (bs, ptid);
5564
5565 if (bs->stop)
5566 {
5567 ++(b->hit_count);
5568 observer_notify_breakpoint_modified (b);
5569
5570 /* We will stop here. */
5571 if (b->disposition == disp_disable)
5572 {
5573 --(b->enable_count);
5574 if (b->enable_count <= 0
5575 && b->enable_state != bp_permanent)
5576 b->enable_state = bp_disabled;
5577 removed_any = 1;
5578 }
5579 if (b->silent)
5580 bs->print = 0;
5581 bs->commands = b->commands;
5582 incref_counted_command_line (bs->commands);
5583 if (command_line_is_silent (bs->commands
5584 ? bs->commands->commands : NULL))
5585 bs->print = 0;
5586
5587 b->ops->after_condition_true (bs);
5588 }
5589
5590 }
5591
5592 /* Print nothing for this entry if we don't stop or don't
5593 print. */
5594 if (!bs->stop || !bs->print)
5595 bs->print_it = print_it_noop;
5596 }
5597
5598 /* If we aren't stopping, the value of some hardware watchpoint may
5599 not have changed, but the intermediate memory locations we are
5600 watching may have. Don't bother if we're stopping; this will get
5601 done later. */
5602 need_remove_insert = 0;
5603 if (! bpstat_causes_stop (bs_head))
5604 for (bs = bs_head; bs != NULL; bs = bs->next)
5605 if (!bs->stop
5606 && bs->breakpoint_at
5607 && is_hardware_watchpoint (bs->breakpoint_at))
5608 {
5609 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5610
5611 update_watchpoint (w, 0 /* don't reparse. */);
5612 need_remove_insert = 1;
5613 }
5614
5615 if (need_remove_insert)
5616 update_global_location_list (UGLL_MAY_INSERT);
5617 else if (removed_any)
5618 update_global_location_list (UGLL_DONT_INSERT);
5619
5620 return bs_head;
5621}
5622
5623static void
5624handle_jit_event (void)
5625{
5626 struct frame_info *frame;
5627 struct gdbarch *gdbarch;
5628
5629 /* Switch terminal for any messages produced by
5630 breakpoint_re_set. */
5631 target_terminal_ours_for_output ();
5632
5633 frame = get_current_frame ();
5634 gdbarch = get_frame_arch (frame);
5635
5636 jit_event_handler (gdbarch);
5637
5638 target_terminal_inferior ();
5639}
5640
5641/* Prepare WHAT final decision for infrun. */
5642
5643/* Decide what infrun needs to do with this bpstat. */
5644
5645struct bpstat_what
5646bpstat_what (bpstat bs_head)
5647{
5648 struct bpstat_what retval;
5649 int jit_event = 0;
5650 bpstat bs;
5651
5652 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5653 retval.call_dummy = STOP_NONE;
5654 retval.is_longjmp = 0;
5655
5656 for (bs = bs_head; bs != NULL; bs = bs->next)
5657 {
5658 /* Extract this BS's action. After processing each BS, we check
5659 if its action overrides all we've seem so far. */
5660 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5661 enum bptype bptype;
5662
5663 if (bs->breakpoint_at == NULL)
5664 {
5665 /* I suspect this can happen if it was a momentary
5666 breakpoint which has since been deleted. */
5667 bptype = bp_none;
5668 }
5669 else
5670 bptype = bs->breakpoint_at->type;
5671
5672 switch (bptype)
5673 {
5674 case bp_none:
5675 break;
5676 case bp_breakpoint:
5677 case bp_hardware_breakpoint:
5678 case bp_single_step:
5679 case bp_until:
5680 case bp_finish:
5681 case bp_shlib_event:
5682 if (bs->stop)
5683 {
5684 if (bs->print)
5685 this_action = BPSTAT_WHAT_STOP_NOISY;
5686 else
5687 this_action = BPSTAT_WHAT_STOP_SILENT;
5688 }
5689 else
5690 this_action = BPSTAT_WHAT_SINGLE;
5691 break;
5692 case bp_watchpoint:
5693 case bp_hardware_watchpoint:
5694 case bp_read_watchpoint:
5695 case bp_access_watchpoint:
5696 if (bs->stop)
5697 {
5698 if (bs->print)
5699 this_action = BPSTAT_WHAT_STOP_NOISY;
5700 else
5701 this_action = BPSTAT_WHAT_STOP_SILENT;
5702 }
5703 else
5704 {
5705 /* There was a watchpoint, but we're not stopping.
5706 This requires no further action. */
5707 }
5708 break;
5709 case bp_longjmp:
5710 case bp_longjmp_call_dummy:
5711 case bp_exception:
5712 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5713 retval.is_longjmp = bptype != bp_exception;
5714 break;
5715 case bp_longjmp_resume:
5716 case bp_exception_resume:
5717 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5718 retval.is_longjmp = bptype == bp_longjmp_resume;
5719 break;
5720 case bp_step_resume:
5721 if (bs->stop)
5722 this_action = BPSTAT_WHAT_STEP_RESUME;
5723 else
5724 {
5725 /* It is for the wrong frame. */
5726 this_action = BPSTAT_WHAT_SINGLE;
5727 }
5728 break;
5729 case bp_hp_step_resume:
5730 if (bs->stop)
5731 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5732 else
5733 {
5734 /* It is for the wrong frame. */
5735 this_action = BPSTAT_WHAT_SINGLE;
5736 }
5737 break;
5738 case bp_watchpoint_scope:
5739 case bp_thread_event:
5740 case bp_overlay_event:
5741 case bp_longjmp_master:
5742 case bp_std_terminate_master:
5743 case bp_exception_master:
5744 this_action = BPSTAT_WHAT_SINGLE;
5745 break;
5746 case bp_catchpoint:
5747 if (bs->stop)
5748 {
5749 if (bs->print)
5750 this_action = BPSTAT_WHAT_STOP_NOISY;
5751 else
5752 this_action = BPSTAT_WHAT_STOP_SILENT;
5753 }
5754 else
5755 {
5756 /* There was a catchpoint, but we're not stopping.
5757 This requires no further action. */
5758 }
5759 break;
5760 case bp_jit_event:
5761 jit_event = 1;
5762 this_action = BPSTAT_WHAT_SINGLE;
5763 break;
5764 case bp_call_dummy:
5765 /* Make sure the action is stop (silent or noisy),
5766 so infrun.c pops the dummy frame. */
5767 retval.call_dummy = STOP_STACK_DUMMY;
5768 this_action = BPSTAT_WHAT_STOP_SILENT;
5769 break;
5770 case bp_std_terminate:
5771 /* Make sure the action is stop (silent or noisy),
5772 so infrun.c pops the dummy frame. */
5773 retval.call_dummy = STOP_STD_TERMINATE;
5774 this_action = BPSTAT_WHAT_STOP_SILENT;
5775 break;
5776 case bp_tracepoint:
5777 case bp_fast_tracepoint:
5778 case bp_static_tracepoint:
5779 /* Tracepoint hits should not be reported back to GDB, and
5780 if one got through somehow, it should have been filtered
5781 out already. */
5782 internal_error (__FILE__, __LINE__,
5783 _("bpstat_what: tracepoint encountered"));
5784 break;
5785 case bp_gnu_ifunc_resolver:
5786 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5787 this_action = BPSTAT_WHAT_SINGLE;
5788 break;
5789 case bp_gnu_ifunc_resolver_return:
5790 /* The breakpoint will be removed, execution will restart from the
5791 PC of the former breakpoint. */
5792 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5793 break;
5794
5795 case bp_dprintf:
5796 if (bs->stop)
5797 this_action = BPSTAT_WHAT_STOP_SILENT;
5798 else
5799 this_action = BPSTAT_WHAT_SINGLE;
5800 break;
5801
5802 default:
5803 internal_error (__FILE__, __LINE__,
5804 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5805 }
5806
5807 retval.main_action = max (retval.main_action, this_action);
5808 }
5809
5810 /* These operations may affect the bs->breakpoint_at state so they are
5811 delayed after MAIN_ACTION is decided above. */
5812
5813 if (jit_event)
5814 {
5815 if (debug_infrun)
5816 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5817
5818 handle_jit_event ();
5819 }
5820
5821 for (bs = bs_head; bs != NULL; bs = bs->next)
5822 {
5823 struct breakpoint *b = bs->breakpoint_at;
5824
5825 if (b == NULL)
5826 continue;
5827 switch (b->type)
5828 {
5829 case bp_gnu_ifunc_resolver:
5830 gnu_ifunc_resolver_stop (b);
5831 break;
5832 case bp_gnu_ifunc_resolver_return:
5833 gnu_ifunc_resolver_return_stop (b);
5834 break;
5835 }
5836 }
5837
5838 return retval;
5839}
5840
5841/* Nonzero if we should step constantly (e.g. watchpoints on machines
5842 without hardware support). This isn't related to a specific bpstat,
5843 just to things like whether watchpoints are set. */
5844
5845int
5846bpstat_should_step (void)
5847{
5848 struct breakpoint *b;
5849
5850 ALL_BREAKPOINTS (b)
5851 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5852 return 1;
5853 return 0;
5854}
5855
5856int
5857bpstat_causes_stop (bpstat bs)
5858{
5859 for (; bs != NULL; bs = bs->next)
5860 if (bs->stop)
5861 return 1;
5862
5863 return 0;
5864}
5865
5866\f
5867
5868/* Compute a string of spaces suitable to indent the next line
5869 so it starts at the position corresponding to the table column
5870 named COL_NAME in the currently active table of UIOUT. */
5871
5872static char *
5873wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5874{
5875 static char wrap_indent[80];
5876 int i, total_width, width, align;
5877 char *text;
5878
5879 total_width = 0;
5880 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5881 {
5882 if (strcmp (text, col_name) == 0)
5883 {
5884 gdb_assert (total_width < sizeof wrap_indent);
5885 memset (wrap_indent, ' ', total_width);
5886 wrap_indent[total_width] = 0;
5887
5888 return wrap_indent;
5889 }
5890
5891 total_width += width + 1;
5892 }
5893
5894 return NULL;
5895}
5896
5897/* Determine if the locations of this breakpoint will have their conditions
5898 evaluated by the target, host or a mix of both. Returns the following:
5899
5900 "host": Host evals condition.
5901 "host or target": Host or Target evals condition.
5902 "target": Target evals condition.
5903*/
5904
5905static const char *
5906bp_condition_evaluator (struct breakpoint *b)
5907{
5908 struct bp_location *bl;
5909 char host_evals = 0;
5910 char target_evals = 0;
5911
5912 if (!b)
5913 return NULL;
5914
5915 if (!is_breakpoint (b))
5916 return NULL;
5917
5918 if (gdb_evaluates_breakpoint_condition_p ()
5919 || !target_supports_evaluation_of_breakpoint_conditions ())
5920 return condition_evaluation_host;
5921
5922 for (bl = b->loc; bl; bl = bl->next)
5923 {
5924 if (bl->cond_bytecode)
5925 target_evals++;
5926 else
5927 host_evals++;
5928 }
5929
5930 if (host_evals && target_evals)
5931 return condition_evaluation_both;
5932 else if (target_evals)
5933 return condition_evaluation_target;
5934 else
5935 return condition_evaluation_host;
5936}
5937
5938/* Determine the breakpoint location's condition evaluator. This is
5939 similar to bp_condition_evaluator, but for locations. */
5940
5941static const char *
5942bp_location_condition_evaluator (struct bp_location *bl)
5943{
5944 if (bl && !is_breakpoint (bl->owner))
5945 return NULL;
5946
5947 if (gdb_evaluates_breakpoint_condition_p ()
5948 || !target_supports_evaluation_of_breakpoint_conditions ())
5949 return condition_evaluation_host;
5950
5951 if (bl && bl->cond_bytecode)
5952 return condition_evaluation_target;
5953 else
5954 return condition_evaluation_host;
5955}
5956
5957/* Print the LOC location out of the list of B->LOC locations. */
5958
5959static void
5960print_breakpoint_location (struct breakpoint *b,
5961 struct bp_location *loc)
5962{
5963 struct ui_out *uiout = current_uiout;
5964 struct cleanup *old_chain = save_current_program_space ();
5965
5966 if (loc != NULL && loc->shlib_disabled)
5967 loc = NULL;
5968
5969 if (loc != NULL)
5970 set_current_program_space (loc->pspace);
5971
5972 if (b->display_canonical)
5973 ui_out_field_string (uiout, "what", b->addr_string);
5974 else if (loc && loc->symtab)
5975 {
5976 struct symbol *sym
5977 = find_pc_sect_function (loc->address, loc->section);
5978 if (sym)
5979 {
5980 ui_out_text (uiout, "in ");
5981 ui_out_field_string (uiout, "func",
5982 SYMBOL_PRINT_NAME (sym));
5983 ui_out_text (uiout, " ");
5984 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5985 ui_out_text (uiout, "at ");
5986 }
5987 ui_out_field_string (uiout, "file",
5988 symtab_to_filename_for_display (loc->symtab));
5989 ui_out_text (uiout, ":");
5990
5991 if (ui_out_is_mi_like_p (uiout))
5992 ui_out_field_string (uiout, "fullname",
5993 symtab_to_fullname (loc->symtab));
5994
5995 ui_out_field_int (uiout, "line", loc->line_number);
5996 }
5997 else if (loc)
5998 {
5999 struct ui_file *stb = mem_fileopen ();
6000 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6001
6002 print_address_symbolic (loc->gdbarch, loc->address, stb,
6003 demangle, "");
6004 ui_out_field_stream (uiout, "at", stb);
6005
6006 do_cleanups (stb_chain);
6007 }
6008 else
6009 ui_out_field_string (uiout, "pending", b->addr_string);
6010
6011 if (loc && is_breakpoint (b)
6012 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6013 && bp_condition_evaluator (b) == condition_evaluation_both)
6014 {
6015 ui_out_text (uiout, " (");
6016 ui_out_field_string (uiout, "evaluated-by",
6017 bp_location_condition_evaluator (loc));
6018 ui_out_text (uiout, ")");
6019 }
6020
6021 do_cleanups (old_chain);
6022}
6023
6024static const char *
6025bptype_string (enum bptype type)
6026{
6027 struct ep_type_description
6028 {
6029 enum bptype type;
6030 char *description;
6031 };
6032 static struct ep_type_description bptypes[] =
6033 {
6034 {bp_none, "?deleted?"},
6035 {bp_breakpoint, "breakpoint"},
6036 {bp_hardware_breakpoint, "hw breakpoint"},
6037 {bp_single_step, "sw single-step"},
6038 {bp_until, "until"},
6039 {bp_finish, "finish"},
6040 {bp_watchpoint, "watchpoint"},
6041 {bp_hardware_watchpoint, "hw watchpoint"},
6042 {bp_read_watchpoint, "read watchpoint"},
6043 {bp_access_watchpoint, "acc watchpoint"},
6044 {bp_longjmp, "longjmp"},
6045 {bp_longjmp_resume, "longjmp resume"},
6046 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6047 {bp_exception, "exception"},
6048 {bp_exception_resume, "exception resume"},
6049 {bp_step_resume, "step resume"},
6050 {bp_hp_step_resume, "high-priority step resume"},
6051 {bp_watchpoint_scope, "watchpoint scope"},
6052 {bp_call_dummy, "call dummy"},
6053 {bp_std_terminate, "std::terminate"},
6054 {bp_shlib_event, "shlib events"},
6055 {bp_thread_event, "thread events"},
6056 {bp_overlay_event, "overlay events"},
6057 {bp_longjmp_master, "longjmp master"},
6058 {bp_std_terminate_master, "std::terminate master"},
6059 {bp_exception_master, "exception master"},
6060 {bp_catchpoint, "catchpoint"},
6061 {bp_tracepoint, "tracepoint"},
6062 {bp_fast_tracepoint, "fast tracepoint"},
6063 {bp_static_tracepoint, "static tracepoint"},
6064 {bp_dprintf, "dprintf"},
6065 {bp_jit_event, "jit events"},
6066 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6067 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6068 };
6069
6070 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6071 || ((int) type != bptypes[(int) type].type))
6072 internal_error (__FILE__, __LINE__,
6073 _("bptypes table does not describe type #%d."),
6074 (int) type);
6075
6076 return bptypes[(int) type].description;
6077}
6078
6079/* For MI, output a field named 'thread-groups' with a list as the value.
6080 For CLI, prefix the list with the string 'inf'. */
6081
6082static void
6083output_thread_groups (struct ui_out *uiout,
6084 const char *field_name,
6085 VEC(int) *inf_num,
6086 int mi_only)
6087{
6088 struct cleanup *back_to;
6089 int is_mi = ui_out_is_mi_like_p (uiout);
6090 int inf;
6091 int i;
6092
6093 /* For backward compatibility, don't display inferiors in CLI unless
6094 there are several. Always display them for MI. */
6095 if (!is_mi && mi_only)
6096 return;
6097
6098 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6099
6100 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6101 {
6102 if (is_mi)
6103 {
6104 char mi_group[10];
6105
6106 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6107 ui_out_field_string (uiout, NULL, mi_group);
6108 }
6109 else
6110 {
6111 if (i == 0)
6112 ui_out_text (uiout, " inf ");
6113 else
6114 ui_out_text (uiout, ", ");
6115
6116 ui_out_text (uiout, plongest (inf));
6117 }
6118 }
6119
6120 do_cleanups (back_to);
6121}
6122
6123/* Print B to gdb_stdout. */
6124
6125static void
6126print_one_breakpoint_location (struct breakpoint *b,
6127 struct bp_location *loc,
6128 int loc_number,
6129 struct bp_location **last_loc,
6130 int allflag)
6131{
6132 struct command_line *l;
6133 static char bpenables[] = "nynny";
6134
6135 struct ui_out *uiout = current_uiout;
6136 int header_of_multiple = 0;
6137 int part_of_multiple = (loc != NULL);
6138 struct value_print_options opts;
6139
6140 get_user_print_options (&opts);
6141
6142 gdb_assert (!loc || loc_number != 0);
6143 /* See comment in print_one_breakpoint concerning treatment of
6144 breakpoints with single disabled location. */
6145 if (loc == NULL
6146 && (b->loc != NULL
6147 && (b->loc->next != NULL || !b->loc->enabled)))
6148 header_of_multiple = 1;
6149 if (loc == NULL)
6150 loc = b->loc;
6151
6152 annotate_record ();
6153
6154 /* 1 */
6155 annotate_field (0);
6156 if (part_of_multiple)
6157 {
6158 char *formatted;
6159 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6160 ui_out_field_string (uiout, "number", formatted);
6161 xfree (formatted);
6162 }
6163 else
6164 {
6165 ui_out_field_int (uiout, "number", b->number);
6166 }
6167
6168 /* 2 */
6169 annotate_field (1);
6170 if (part_of_multiple)
6171 ui_out_field_skip (uiout, "type");
6172 else
6173 ui_out_field_string (uiout, "type", bptype_string (b->type));
6174
6175 /* 3 */
6176 annotate_field (2);
6177 if (part_of_multiple)
6178 ui_out_field_skip (uiout, "disp");
6179 else
6180 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6181
6182
6183 /* 4 */
6184 annotate_field (3);
6185 if (part_of_multiple)
6186 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6187 else
6188 ui_out_field_fmt (uiout, "enabled", "%c",
6189 bpenables[(int) b->enable_state]);
6190 ui_out_spaces (uiout, 2);
6191
6192
6193 /* 5 and 6 */
6194 if (b->ops != NULL && b->ops->print_one != NULL)
6195 {
6196 /* Although the print_one can possibly print all locations,
6197 calling it here is not likely to get any nice result. So,
6198 make sure there's just one location. */
6199 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6200 b->ops->print_one (b, last_loc);
6201 }
6202 else
6203 switch (b->type)
6204 {
6205 case bp_none:
6206 internal_error (__FILE__, __LINE__,
6207 _("print_one_breakpoint: bp_none encountered\n"));
6208 break;
6209
6210 case bp_watchpoint:
6211 case bp_hardware_watchpoint:
6212 case bp_read_watchpoint:
6213 case bp_access_watchpoint:
6214 {
6215 struct watchpoint *w = (struct watchpoint *) b;
6216
6217 /* Field 4, the address, is omitted (which makes the columns
6218 not line up too nicely with the headers, but the effect
6219 is relatively readable). */
6220 if (opts.addressprint)
6221 ui_out_field_skip (uiout, "addr");
6222 annotate_field (5);
6223 ui_out_field_string (uiout, "what", w->exp_string);
6224 }
6225 break;
6226
6227 case bp_breakpoint:
6228 case bp_hardware_breakpoint:
6229 case bp_single_step:
6230 case bp_until:
6231 case bp_finish:
6232 case bp_longjmp:
6233 case bp_longjmp_resume:
6234 case bp_longjmp_call_dummy:
6235 case bp_exception:
6236 case bp_exception_resume:
6237 case bp_step_resume:
6238 case bp_hp_step_resume:
6239 case bp_watchpoint_scope:
6240 case bp_call_dummy:
6241 case bp_std_terminate:
6242 case bp_shlib_event:
6243 case bp_thread_event:
6244 case bp_overlay_event:
6245 case bp_longjmp_master:
6246 case bp_std_terminate_master:
6247 case bp_exception_master:
6248 case bp_tracepoint:
6249 case bp_fast_tracepoint:
6250 case bp_static_tracepoint:
6251 case bp_dprintf:
6252 case bp_jit_event:
6253 case bp_gnu_ifunc_resolver:
6254 case bp_gnu_ifunc_resolver_return:
6255 if (opts.addressprint)
6256 {
6257 annotate_field (4);
6258 if (header_of_multiple)
6259 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6260 else if (b->loc == NULL || loc->shlib_disabled)
6261 ui_out_field_string (uiout, "addr", "<PENDING>");
6262 else
6263 ui_out_field_core_addr (uiout, "addr",
6264 loc->gdbarch, loc->address);
6265 }
6266 annotate_field (5);
6267 if (!header_of_multiple)
6268 print_breakpoint_location (b, loc);
6269 if (b->loc)
6270 *last_loc = b->loc;
6271 break;
6272 }
6273
6274
6275 if (loc != NULL && !header_of_multiple)
6276 {
6277 struct inferior *inf;
6278 VEC(int) *inf_num = NULL;
6279 int mi_only = 1;
6280
6281 ALL_INFERIORS (inf)
6282 {
6283 if (inf->pspace == loc->pspace)
6284 VEC_safe_push (int, inf_num, inf->num);
6285 }
6286
6287 /* For backward compatibility, don't display inferiors in CLI unless
6288 there are several. Always display for MI. */
6289 if (allflag
6290 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6291 && (number_of_program_spaces () > 1
6292 || number_of_inferiors () > 1)
6293 /* LOC is for existing B, it cannot be in
6294 moribund_locations and thus having NULL OWNER. */
6295 && loc->owner->type != bp_catchpoint))
6296 mi_only = 0;
6297 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6298 VEC_free (int, inf_num);
6299 }
6300
6301 if (!part_of_multiple)
6302 {
6303 if (b->thread != -1)
6304 {
6305 /* FIXME: This seems to be redundant and lost here; see the
6306 "stop only in" line a little further down. */
6307 ui_out_text (uiout, " thread ");
6308 ui_out_field_int (uiout, "thread", b->thread);
6309 }
6310 else if (b->task != 0)
6311 {
6312 ui_out_text (uiout, " task ");
6313 ui_out_field_int (uiout, "task", b->task);
6314 }
6315 }
6316
6317 ui_out_text (uiout, "\n");
6318
6319 if (!part_of_multiple)
6320 b->ops->print_one_detail (b, uiout);
6321
6322 if (part_of_multiple && frame_id_p (b->frame_id))
6323 {
6324 annotate_field (6);
6325 ui_out_text (uiout, "\tstop only in stack frame at ");
6326 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6327 the frame ID. */
6328 ui_out_field_core_addr (uiout, "frame",
6329 b->gdbarch, b->frame_id.stack_addr);
6330 ui_out_text (uiout, "\n");
6331 }
6332
6333 if (!part_of_multiple && b->cond_string)
6334 {
6335 annotate_field (7);
6336 if (is_tracepoint (b))
6337 ui_out_text (uiout, "\ttrace only if ");
6338 else
6339 ui_out_text (uiout, "\tstop only if ");
6340 ui_out_field_string (uiout, "cond", b->cond_string);
6341
6342 /* Print whether the target is doing the breakpoint's condition
6343 evaluation. If GDB is doing the evaluation, don't print anything. */
6344 if (is_breakpoint (b)
6345 && breakpoint_condition_evaluation_mode ()
6346 == condition_evaluation_target)
6347 {
6348 ui_out_text (uiout, " (");
6349 ui_out_field_string (uiout, "evaluated-by",
6350 bp_condition_evaluator (b));
6351 ui_out_text (uiout, " evals)");
6352 }
6353 ui_out_text (uiout, "\n");
6354 }
6355
6356 if (!part_of_multiple && b->thread != -1)
6357 {
6358 /* FIXME should make an annotation for this. */
6359 ui_out_text (uiout, "\tstop only in thread ");
6360 ui_out_field_int (uiout, "thread", b->thread);
6361 ui_out_text (uiout, "\n");
6362 }
6363
6364 if (!part_of_multiple)
6365 {
6366 if (b->hit_count)
6367 {
6368 /* FIXME should make an annotation for this. */
6369 if (is_catchpoint (b))
6370 ui_out_text (uiout, "\tcatchpoint");
6371 else if (is_tracepoint (b))
6372 ui_out_text (uiout, "\ttracepoint");
6373 else
6374 ui_out_text (uiout, "\tbreakpoint");
6375 ui_out_text (uiout, " already hit ");
6376 ui_out_field_int (uiout, "times", b->hit_count);
6377 if (b->hit_count == 1)
6378 ui_out_text (uiout, " time\n");
6379 else
6380 ui_out_text (uiout, " times\n");
6381 }
6382 else
6383 {
6384 /* Output the count also if it is zero, but only if this is mi. */
6385 if (ui_out_is_mi_like_p (uiout))
6386 ui_out_field_int (uiout, "times", b->hit_count);
6387 }
6388 }
6389
6390 if (!part_of_multiple && b->ignore_count)
6391 {
6392 annotate_field (8);
6393 ui_out_text (uiout, "\tignore next ");
6394 ui_out_field_int (uiout, "ignore", b->ignore_count);
6395 ui_out_text (uiout, " hits\n");
6396 }
6397
6398 /* Note that an enable count of 1 corresponds to "enable once"
6399 behavior, which is reported by the combination of enablement and
6400 disposition, so we don't need to mention it here. */
6401 if (!part_of_multiple && b->enable_count > 1)
6402 {
6403 annotate_field (8);
6404 ui_out_text (uiout, "\tdisable after ");
6405 /* Tweak the wording to clarify that ignore and enable counts
6406 are distinct, and have additive effect. */
6407 if (b->ignore_count)
6408 ui_out_text (uiout, "additional ");
6409 else
6410 ui_out_text (uiout, "next ");
6411 ui_out_field_int (uiout, "enable", b->enable_count);
6412 ui_out_text (uiout, " hits\n");
6413 }
6414
6415 if (!part_of_multiple && is_tracepoint (b))
6416 {
6417 struct tracepoint *tp = (struct tracepoint *) b;
6418
6419 if (tp->traceframe_usage)
6420 {
6421 ui_out_text (uiout, "\ttrace buffer usage ");
6422 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6423 ui_out_text (uiout, " bytes\n");
6424 }
6425 }
6426
6427 l = b->commands ? b->commands->commands : NULL;
6428 if (!part_of_multiple && l)
6429 {
6430 struct cleanup *script_chain;
6431
6432 annotate_field (9);
6433 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6434 print_command_lines (uiout, l, 4);
6435 do_cleanups (script_chain);
6436 }
6437
6438 if (is_tracepoint (b))
6439 {
6440 struct tracepoint *t = (struct tracepoint *) b;
6441
6442 if (!part_of_multiple && t->pass_count)
6443 {
6444 annotate_field (10);
6445 ui_out_text (uiout, "\tpass count ");
6446 ui_out_field_int (uiout, "pass", t->pass_count);
6447 ui_out_text (uiout, " \n");
6448 }
6449
6450 /* Don't display it when tracepoint or tracepoint location is
6451 pending. */
6452 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6453 {
6454 annotate_field (11);
6455
6456 if (ui_out_is_mi_like_p (uiout))
6457 ui_out_field_string (uiout, "installed",
6458 loc->inserted ? "y" : "n");
6459 else
6460 {
6461 if (loc->inserted)
6462 ui_out_text (uiout, "\t");
6463 else
6464 ui_out_text (uiout, "\tnot ");
6465 ui_out_text (uiout, "installed on target\n");
6466 }
6467 }
6468 }
6469
6470 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6471 {
6472 if (is_watchpoint (b))
6473 {
6474 struct watchpoint *w = (struct watchpoint *) b;
6475
6476 ui_out_field_string (uiout, "original-location", w->exp_string);
6477 }
6478 else if (b->addr_string)
6479 ui_out_field_string (uiout, "original-location", b->addr_string);
6480 }
6481}
6482
6483static void
6484print_one_breakpoint (struct breakpoint *b,
6485 struct bp_location **last_loc,
6486 int allflag)
6487{
6488 struct cleanup *bkpt_chain;
6489 struct ui_out *uiout = current_uiout;
6490
6491 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6492
6493 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6494 do_cleanups (bkpt_chain);
6495
6496 /* If this breakpoint has custom print function,
6497 it's already printed. Otherwise, print individual
6498 locations, if any. */
6499 if (b->ops == NULL || b->ops->print_one == NULL)
6500 {
6501 /* If breakpoint has a single location that is disabled, we
6502 print it as if it had several locations, since otherwise it's
6503 hard to represent "breakpoint enabled, location disabled"
6504 situation.
6505
6506 Note that while hardware watchpoints have several locations
6507 internally, that's not a property exposed to user. */
6508 if (b->loc
6509 && !is_hardware_watchpoint (b)
6510 && (b->loc->next || !b->loc->enabled))
6511 {
6512 struct bp_location *loc;
6513 int n = 1;
6514
6515 for (loc = b->loc; loc; loc = loc->next, ++n)
6516 {
6517 struct cleanup *inner2 =
6518 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6519 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6520 do_cleanups (inner2);
6521 }
6522 }
6523 }
6524}
6525
6526static int
6527breakpoint_address_bits (struct breakpoint *b)
6528{
6529 int print_address_bits = 0;
6530 struct bp_location *loc;
6531
6532 for (loc = b->loc; loc; loc = loc->next)
6533 {
6534 int addr_bit;
6535
6536 /* Software watchpoints that aren't watching memory don't have
6537 an address to print. */
6538 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6539 continue;
6540
6541 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6542 if (addr_bit > print_address_bits)
6543 print_address_bits = addr_bit;
6544 }
6545
6546 return print_address_bits;
6547}
6548
6549struct captured_breakpoint_query_args
6550 {
6551 int bnum;
6552 };
6553
6554static int
6555do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6556{
6557 struct captured_breakpoint_query_args *args = data;
6558 struct breakpoint *b;
6559 struct bp_location *dummy_loc = NULL;
6560
6561 ALL_BREAKPOINTS (b)
6562 {
6563 if (args->bnum == b->number)
6564 {
6565 print_one_breakpoint (b, &dummy_loc, 0);
6566 return GDB_RC_OK;
6567 }
6568 }
6569 return GDB_RC_NONE;
6570}
6571
6572enum gdb_rc
6573gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6574 char **error_message)
6575{
6576 struct captured_breakpoint_query_args args;
6577
6578 args.bnum = bnum;
6579 /* For the moment we don't trust print_one_breakpoint() to not throw
6580 an error. */
6581 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6582 error_message, RETURN_MASK_ALL) < 0)
6583 return GDB_RC_FAIL;
6584 else
6585 return GDB_RC_OK;
6586}
6587
6588/* Return true if this breakpoint was set by the user, false if it is
6589 internal or momentary. */
6590
6591int
6592user_breakpoint_p (struct breakpoint *b)
6593{
6594 return b->number > 0;
6595}
6596
6597/* Print information on user settable breakpoint (watchpoint, etc)
6598 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6599 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6600 FILTER is non-NULL, call it on each breakpoint and only include the
6601 ones for which it returns non-zero. Return the total number of
6602 breakpoints listed. */
6603
6604static int
6605breakpoint_1 (char *args, int allflag,
6606 int (*filter) (const struct breakpoint *))
6607{
6608 struct breakpoint *b;
6609 struct bp_location *last_loc = NULL;
6610 int nr_printable_breakpoints;
6611 struct cleanup *bkpttbl_chain;
6612 struct value_print_options opts;
6613 int print_address_bits = 0;
6614 int print_type_col_width = 14;
6615 struct ui_out *uiout = current_uiout;
6616
6617 get_user_print_options (&opts);
6618
6619 /* Compute the number of rows in the table, as well as the size
6620 required for address fields. */
6621 nr_printable_breakpoints = 0;
6622 ALL_BREAKPOINTS (b)
6623 {
6624 /* If we have a filter, only list the breakpoints it accepts. */
6625 if (filter && !filter (b))
6626 continue;
6627
6628 /* If we have an "args" string, it is a list of breakpoints to
6629 accept. Skip the others. */
6630 if (args != NULL && *args != '\0')
6631 {
6632 if (allflag && parse_and_eval_long (args) != b->number)
6633 continue;
6634 if (!allflag && !number_is_in_list (args, b->number))
6635 continue;
6636 }
6637
6638 if (allflag || user_breakpoint_p (b))
6639 {
6640 int addr_bit, type_len;
6641
6642 addr_bit = breakpoint_address_bits (b);
6643 if (addr_bit > print_address_bits)
6644 print_address_bits = addr_bit;
6645
6646 type_len = strlen (bptype_string (b->type));
6647 if (type_len > print_type_col_width)
6648 print_type_col_width = type_len;
6649
6650 nr_printable_breakpoints++;
6651 }
6652 }
6653
6654 if (opts.addressprint)
6655 bkpttbl_chain
6656 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6657 nr_printable_breakpoints,
6658 "BreakpointTable");
6659 else
6660 bkpttbl_chain
6661 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6662 nr_printable_breakpoints,
6663 "BreakpointTable");
6664
6665 if (nr_printable_breakpoints > 0)
6666 annotate_breakpoints_headers ();
6667 if (nr_printable_breakpoints > 0)
6668 annotate_field (0);
6669 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6670 if (nr_printable_breakpoints > 0)
6671 annotate_field (1);
6672 ui_out_table_header (uiout, print_type_col_width, ui_left,
6673 "type", "Type"); /* 2 */
6674 if (nr_printable_breakpoints > 0)
6675 annotate_field (2);
6676 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6677 if (nr_printable_breakpoints > 0)
6678 annotate_field (3);
6679 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6680 if (opts.addressprint)
6681 {
6682 if (nr_printable_breakpoints > 0)
6683 annotate_field (4);
6684 if (print_address_bits <= 32)
6685 ui_out_table_header (uiout, 10, ui_left,
6686 "addr", "Address"); /* 5 */
6687 else
6688 ui_out_table_header (uiout, 18, ui_left,
6689 "addr", "Address"); /* 5 */
6690 }
6691 if (nr_printable_breakpoints > 0)
6692 annotate_field (5);
6693 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6694 ui_out_table_body (uiout);
6695 if (nr_printable_breakpoints > 0)
6696 annotate_breakpoints_table ();
6697
6698 ALL_BREAKPOINTS (b)
6699 {
6700 QUIT;
6701 /* If we have a filter, only list the breakpoints it accepts. */
6702 if (filter && !filter (b))
6703 continue;
6704
6705 /* If we have an "args" string, it is a list of breakpoints to
6706 accept. Skip the others. */
6707
6708 if (args != NULL && *args != '\0')
6709 {
6710 if (allflag) /* maintenance info breakpoint */
6711 {
6712 if (parse_and_eval_long (args) != b->number)
6713 continue;
6714 }
6715 else /* all others */
6716 {
6717 if (!number_is_in_list (args, b->number))
6718 continue;
6719 }
6720 }
6721 /* We only print out user settable breakpoints unless the
6722 allflag is set. */
6723 if (allflag || user_breakpoint_p (b))
6724 print_one_breakpoint (b, &last_loc, allflag);
6725 }
6726
6727 do_cleanups (bkpttbl_chain);
6728
6729 if (nr_printable_breakpoints == 0)
6730 {
6731 /* If there's a filter, let the caller decide how to report
6732 empty list. */
6733 if (!filter)
6734 {
6735 if (args == NULL || *args == '\0')
6736 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6737 else
6738 ui_out_message (uiout, 0,
6739 "No breakpoint or watchpoint matching '%s'.\n",
6740 args);
6741 }
6742 }
6743 else
6744 {
6745 if (last_loc && !server_command)
6746 set_next_address (last_loc->gdbarch, last_loc->address);
6747 }
6748
6749 /* FIXME? Should this be moved up so that it is only called when
6750 there have been breakpoints? */
6751 annotate_breakpoints_table_end ();
6752
6753 return nr_printable_breakpoints;
6754}
6755
6756/* Display the value of default-collect in a way that is generally
6757 compatible with the breakpoint list. */
6758
6759static void
6760default_collect_info (void)
6761{
6762 struct ui_out *uiout = current_uiout;
6763
6764 /* If it has no value (which is frequently the case), say nothing; a
6765 message like "No default-collect." gets in user's face when it's
6766 not wanted. */
6767 if (!*default_collect)
6768 return;
6769
6770 /* The following phrase lines up nicely with per-tracepoint collect
6771 actions. */
6772 ui_out_text (uiout, "default collect ");
6773 ui_out_field_string (uiout, "default-collect", default_collect);
6774 ui_out_text (uiout, " \n");
6775}
6776
6777static void
6778breakpoints_info (char *args, int from_tty)
6779{
6780 breakpoint_1 (args, 0, NULL);
6781
6782 default_collect_info ();
6783}
6784
6785static void
6786watchpoints_info (char *args, int from_tty)
6787{
6788 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6789 struct ui_out *uiout = current_uiout;
6790
6791 if (num_printed == 0)
6792 {
6793 if (args == NULL || *args == '\0')
6794 ui_out_message (uiout, 0, "No watchpoints.\n");
6795 else
6796 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6797 }
6798}
6799
6800static void
6801maintenance_info_breakpoints (char *args, int from_tty)
6802{
6803 breakpoint_1 (args, 1, NULL);
6804
6805 default_collect_info ();
6806}
6807
6808static int
6809breakpoint_has_pc (struct breakpoint *b,
6810 struct program_space *pspace,
6811 CORE_ADDR pc, struct obj_section *section)
6812{
6813 struct bp_location *bl = b->loc;
6814
6815 for (; bl; bl = bl->next)
6816 {
6817 if (bl->pspace == pspace
6818 && bl->address == pc
6819 && (!overlay_debugging || bl->section == section))
6820 return 1;
6821 }
6822 return 0;
6823}
6824
6825/* Print a message describing any user-breakpoints set at PC. This
6826 concerns with logical breakpoints, so we match program spaces, not
6827 address spaces. */
6828
6829static void
6830describe_other_breakpoints (struct gdbarch *gdbarch,
6831 struct program_space *pspace, CORE_ADDR pc,
6832 struct obj_section *section, int thread)
6833{
6834 int others = 0;
6835 struct breakpoint *b;
6836
6837 ALL_BREAKPOINTS (b)
6838 others += (user_breakpoint_p (b)
6839 && breakpoint_has_pc (b, pspace, pc, section));
6840 if (others > 0)
6841 {
6842 if (others == 1)
6843 printf_filtered (_("Note: breakpoint "));
6844 else /* if (others == ???) */
6845 printf_filtered (_("Note: breakpoints "));
6846 ALL_BREAKPOINTS (b)
6847 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6848 {
6849 others--;
6850 printf_filtered ("%d", b->number);
6851 if (b->thread == -1 && thread != -1)
6852 printf_filtered (" (all threads)");
6853 else if (b->thread != -1)
6854 printf_filtered (" (thread %d)", b->thread);
6855 printf_filtered ("%s%s ",
6856 ((b->enable_state == bp_disabled
6857 || b->enable_state == bp_call_disabled)
6858 ? " (disabled)"
6859 : b->enable_state == bp_permanent
6860 ? " (permanent)"
6861 : ""),
6862 (others > 1) ? ","
6863 : ((others == 1) ? " and" : ""));
6864 }
6865 printf_filtered (_("also set at pc "));
6866 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6867 printf_filtered (".\n");
6868 }
6869}
6870\f
6871
6872/* Return true iff it is meaningful to use the address member of
6873 BPT. For some breakpoint types, the address member is irrelevant
6874 and it makes no sense to attempt to compare it to other addresses
6875 (or use it for any other purpose either).
6876
6877 More specifically, each of the following breakpoint types will
6878 always have a zero valued address and we don't want to mark
6879 breakpoints of any of these types to be a duplicate of an actual
6880 breakpoint at address zero:
6881
6882 bp_watchpoint
6883 bp_catchpoint
6884
6885*/
6886
6887static int
6888breakpoint_address_is_meaningful (struct breakpoint *bpt)
6889{
6890 enum bptype type = bpt->type;
6891
6892 return (type != bp_watchpoint && type != bp_catchpoint);
6893}
6894
6895/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6896 true if LOC1 and LOC2 represent the same watchpoint location. */
6897
6898static int
6899watchpoint_locations_match (struct bp_location *loc1,
6900 struct bp_location *loc2)
6901{
6902 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6903 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6904
6905 /* Both of them must exist. */
6906 gdb_assert (w1 != NULL);
6907 gdb_assert (w2 != NULL);
6908
6909 /* If the target can evaluate the condition expression in hardware,
6910 then we we need to insert both watchpoints even if they are at
6911 the same place. Otherwise the watchpoint will only trigger when
6912 the condition of whichever watchpoint was inserted evaluates to
6913 true, not giving a chance for GDB to check the condition of the
6914 other watchpoint. */
6915 if ((w1->cond_exp
6916 && target_can_accel_watchpoint_condition (loc1->address,
6917 loc1->length,
6918 loc1->watchpoint_type,
6919 w1->cond_exp))
6920 || (w2->cond_exp
6921 && target_can_accel_watchpoint_condition (loc2->address,
6922 loc2->length,
6923 loc2->watchpoint_type,
6924 w2->cond_exp)))
6925 return 0;
6926
6927 /* Note that this checks the owner's type, not the location's. In
6928 case the target does not support read watchpoints, but does
6929 support access watchpoints, we'll have bp_read_watchpoint
6930 watchpoints with hw_access locations. Those should be considered
6931 duplicates of hw_read locations. The hw_read locations will
6932 become hw_access locations later. */
6933 return (loc1->owner->type == loc2->owner->type
6934 && loc1->pspace->aspace == loc2->pspace->aspace
6935 && loc1->address == loc2->address
6936 && loc1->length == loc2->length);
6937}
6938
6939/* See breakpoint.h. */
6940
6941int
6942breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6943 struct address_space *aspace2, CORE_ADDR addr2)
6944{
6945 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6946 || aspace1 == aspace2)
6947 && addr1 == addr2);
6948}
6949
6950/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6951 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6952 matches ASPACE2. On targets that have global breakpoints, the address
6953 space doesn't really matter. */
6954
6955static int
6956breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6957 int len1, struct address_space *aspace2,
6958 CORE_ADDR addr2)
6959{
6960 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6961 || aspace1 == aspace2)
6962 && addr2 >= addr1 && addr2 < addr1 + len1);
6963}
6964
6965/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6966 a ranged breakpoint. In most targets, a match happens only if ASPACE
6967 matches the breakpoint's address space. On targets that have global
6968 breakpoints, the address space doesn't really matter. */
6969
6970static int
6971breakpoint_location_address_match (struct bp_location *bl,
6972 struct address_space *aspace,
6973 CORE_ADDR addr)
6974{
6975 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6976 aspace, addr)
6977 || (bl->length
6978 && breakpoint_address_match_range (bl->pspace->aspace,
6979 bl->address, bl->length,
6980 aspace, addr)));
6981}
6982
6983/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6984 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6985 true, otherwise returns false. */
6986
6987static int
6988tracepoint_locations_match (struct bp_location *loc1,
6989 struct bp_location *loc2)
6990{
6991 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6992 /* Since tracepoint locations are never duplicated with others', tracepoint
6993 locations at the same address of different tracepoints are regarded as
6994 different locations. */
6995 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6996 else
6997 return 0;
6998}
6999
7000/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7001 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7002 represent the same location. */
7003
7004static int
7005breakpoint_locations_match (struct bp_location *loc1,
7006 struct bp_location *loc2)
7007{
7008 int hw_point1, hw_point2;
7009
7010 /* Both of them must not be in moribund_locations. */
7011 gdb_assert (loc1->owner != NULL);
7012 gdb_assert (loc2->owner != NULL);
7013
7014 hw_point1 = is_hardware_watchpoint (loc1->owner);
7015 hw_point2 = is_hardware_watchpoint (loc2->owner);
7016
7017 if (hw_point1 != hw_point2)
7018 return 0;
7019 else if (hw_point1)
7020 return watchpoint_locations_match (loc1, loc2);
7021 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7022 return tracepoint_locations_match (loc1, loc2);
7023 else
7024 /* We compare bp_location.length in order to cover ranged breakpoints. */
7025 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7026 loc2->pspace->aspace, loc2->address)
7027 && loc1->length == loc2->length);
7028}
7029
7030static void
7031breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7032 int bnum, int have_bnum)
7033{
7034 /* The longest string possibly returned by hex_string_custom
7035 is 50 chars. These must be at least that big for safety. */
7036 char astr1[64];
7037 char astr2[64];
7038
7039 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7040 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7041 if (have_bnum)
7042 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7043 bnum, astr1, astr2);
7044 else
7045 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7046}
7047
7048/* Adjust a breakpoint's address to account for architectural
7049 constraints on breakpoint placement. Return the adjusted address.
7050 Note: Very few targets require this kind of adjustment. For most
7051 targets, this function is simply the identity function. */
7052
7053static CORE_ADDR
7054adjust_breakpoint_address (struct gdbarch *gdbarch,
7055 CORE_ADDR bpaddr, enum bptype bptype)
7056{
7057 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7058 {
7059 /* Very few targets need any kind of breakpoint adjustment. */
7060 return bpaddr;
7061 }
7062 else if (bptype == bp_watchpoint
7063 || bptype == bp_hardware_watchpoint
7064 || bptype == bp_read_watchpoint
7065 || bptype == bp_access_watchpoint
7066 || bptype == bp_catchpoint)
7067 {
7068 /* Watchpoints and the various bp_catch_* eventpoints should not
7069 have their addresses modified. */
7070 return bpaddr;
7071 }
7072 else if (bptype == bp_single_step)
7073 {
7074 /* Single-step breakpoints should not have their addresses
7075 modified. If there's any architectural constrain that
7076 applies to this address, then it should have already been
7077 taken into account when the breakpoint was created in the
7078 first place. If we didn't do this, stepping through e.g.,
7079 Thumb-2 IT blocks would break. */
7080 return bpaddr;
7081 }
7082 else
7083 {
7084 CORE_ADDR adjusted_bpaddr;
7085
7086 /* Some targets have architectural constraints on the placement
7087 of breakpoint instructions. Obtain the adjusted address. */
7088 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7089
7090 /* An adjusted breakpoint address can significantly alter
7091 a user's expectations. Print a warning if an adjustment
7092 is required. */
7093 if (adjusted_bpaddr != bpaddr)
7094 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7095
7096 return adjusted_bpaddr;
7097 }
7098}
7099
7100void
7101init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7102 struct breakpoint *owner)
7103{
7104 memset (loc, 0, sizeof (*loc));
7105
7106 gdb_assert (ops != NULL);
7107
7108 loc->ops = ops;
7109 loc->owner = owner;
7110 loc->cond = NULL;
7111 loc->cond_bytecode = NULL;
7112 loc->shlib_disabled = 0;
7113 loc->enabled = 1;
7114
7115 switch (owner->type)
7116 {
7117 case bp_breakpoint:
7118 case bp_single_step:
7119 case bp_until:
7120 case bp_finish:
7121 case bp_longjmp:
7122 case bp_longjmp_resume:
7123 case bp_longjmp_call_dummy:
7124 case bp_exception:
7125 case bp_exception_resume:
7126 case bp_step_resume:
7127 case bp_hp_step_resume:
7128 case bp_watchpoint_scope:
7129 case bp_call_dummy:
7130 case bp_std_terminate:
7131 case bp_shlib_event:
7132 case bp_thread_event:
7133 case bp_overlay_event:
7134 case bp_jit_event:
7135 case bp_longjmp_master:
7136 case bp_std_terminate_master:
7137 case bp_exception_master:
7138 case bp_gnu_ifunc_resolver:
7139 case bp_gnu_ifunc_resolver_return:
7140 case bp_dprintf:
7141 loc->loc_type = bp_loc_software_breakpoint;
7142 mark_breakpoint_location_modified (loc);
7143 break;
7144 case bp_hardware_breakpoint:
7145 loc->loc_type = bp_loc_hardware_breakpoint;
7146 mark_breakpoint_location_modified (loc);
7147 break;
7148 case bp_hardware_watchpoint:
7149 case bp_read_watchpoint:
7150 case bp_access_watchpoint:
7151 loc->loc_type = bp_loc_hardware_watchpoint;
7152 break;
7153 case bp_watchpoint:
7154 case bp_catchpoint:
7155 case bp_tracepoint:
7156 case bp_fast_tracepoint:
7157 case bp_static_tracepoint:
7158 loc->loc_type = bp_loc_other;
7159 break;
7160 default:
7161 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7162 }
7163
7164 loc->refc = 1;
7165}
7166
7167/* Allocate a struct bp_location. */
7168
7169static struct bp_location *
7170allocate_bp_location (struct breakpoint *bpt)
7171{
7172 return bpt->ops->allocate_location (bpt);
7173}
7174
7175static void
7176free_bp_location (struct bp_location *loc)
7177{
7178 loc->ops->dtor (loc);
7179 xfree (loc);
7180}
7181
7182/* Increment reference count. */
7183
7184static void
7185incref_bp_location (struct bp_location *bl)
7186{
7187 ++bl->refc;
7188}
7189
7190/* Decrement reference count. If the reference count reaches 0,
7191 destroy the bp_location. Sets *BLP to NULL. */
7192
7193static void
7194decref_bp_location (struct bp_location **blp)
7195{
7196 gdb_assert ((*blp)->refc > 0);
7197
7198 if (--(*blp)->refc == 0)
7199 free_bp_location (*blp);
7200 *blp = NULL;
7201}
7202
7203/* Add breakpoint B at the end of the global breakpoint chain. */
7204
7205static void
7206add_to_breakpoint_chain (struct breakpoint *b)
7207{
7208 struct breakpoint *b1;
7209
7210 /* Add this breakpoint to the end of the chain so that a list of
7211 breakpoints will come out in order of increasing numbers. */
7212
7213 b1 = breakpoint_chain;
7214 if (b1 == 0)
7215 breakpoint_chain = b;
7216 else
7217 {
7218 while (b1->next)
7219 b1 = b1->next;
7220 b1->next = b;
7221 }
7222}
7223
7224/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7225
7226static void
7227init_raw_breakpoint_without_location (struct breakpoint *b,
7228 struct gdbarch *gdbarch,
7229 enum bptype bptype,
7230 const struct breakpoint_ops *ops)
7231{
7232 memset (b, 0, sizeof (*b));
7233
7234 gdb_assert (ops != NULL);
7235
7236 b->ops = ops;
7237 b->type = bptype;
7238 b->gdbarch = gdbarch;
7239 b->language = current_language->la_language;
7240 b->input_radix = input_radix;
7241 b->thread = -1;
7242 b->enable_state = bp_enabled;
7243 b->next = 0;
7244 b->silent = 0;
7245 b->ignore_count = 0;
7246 b->commands = NULL;
7247 b->frame_id = null_frame_id;
7248 b->condition_not_parsed = 0;
7249 b->py_bp_object = NULL;
7250 b->related_breakpoint = b;
7251}
7252
7253/* Helper to set_raw_breakpoint below. Creates a breakpoint
7254 that has type BPTYPE and has no locations as yet. */
7255
7256static struct breakpoint *
7257set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7258 enum bptype bptype,
7259 const struct breakpoint_ops *ops)
7260{
7261 struct breakpoint *b = XNEW (struct breakpoint);
7262
7263 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7264 add_to_breakpoint_chain (b);
7265 return b;
7266}
7267
7268/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7269 resolutions should be made as the user specified the location explicitly
7270 enough. */
7271
7272static void
7273set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7274{
7275 gdb_assert (loc->owner != NULL);
7276
7277 if (loc->owner->type == bp_breakpoint
7278 || loc->owner->type == bp_hardware_breakpoint
7279 || is_tracepoint (loc->owner))
7280 {
7281 int is_gnu_ifunc;
7282 const char *function_name;
7283 CORE_ADDR func_addr;
7284
7285 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7286 &func_addr, NULL, &is_gnu_ifunc);
7287
7288 if (is_gnu_ifunc && !explicit_loc)
7289 {
7290 struct breakpoint *b = loc->owner;
7291
7292 gdb_assert (loc->pspace == current_program_space);
7293 if (gnu_ifunc_resolve_name (function_name,
7294 &loc->requested_address))
7295 {
7296 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7297 loc->address = adjust_breakpoint_address (loc->gdbarch,
7298 loc->requested_address,
7299 b->type);
7300 }
7301 else if (b->type == bp_breakpoint && b->loc == loc
7302 && loc->next == NULL && b->related_breakpoint == b)
7303 {
7304 /* Create only the whole new breakpoint of this type but do not
7305 mess more complicated breakpoints with multiple locations. */
7306 b->type = bp_gnu_ifunc_resolver;
7307 /* Remember the resolver's address for use by the return
7308 breakpoint. */
7309 loc->related_address = func_addr;
7310 }
7311 }
7312
7313 if (function_name)
7314 loc->function_name = xstrdup (function_name);
7315 }
7316}
7317
7318/* Attempt to determine architecture of location identified by SAL. */
7319struct gdbarch *
7320get_sal_arch (struct symtab_and_line sal)
7321{
7322 if (sal.section)
7323 return get_objfile_arch (sal.section->objfile);
7324 if (sal.symtab)
7325 return get_objfile_arch (sal.symtab->objfile);
7326
7327 return NULL;
7328}
7329
7330/* Low level routine for partially initializing a breakpoint of type
7331 BPTYPE. The newly created breakpoint's address, section, source
7332 file name, and line number are provided by SAL.
7333
7334 It is expected that the caller will complete the initialization of
7335 the newly created breakpoint struct as well as output any status
7336 information regarding the creation of a new breakpoint. */
7337
7338static void
7339init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7340 struct symtab_and_line sal, enum bptype bptype,
7341 const struct breakpoint_ops *ops)
7342{
7343 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7344
7345 add_location_to_breakpoint (b, &sal);
7346
7347 if (bptype != bp_catchpoint)
7348 gdb_assert (sal.pspace != NULL);
7349
7350 /* Store the program space that was used to set the breakpoint,
7351 except for ordinary breakpoints, which are independent of the
7352 program space. */
7353 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7354 b->pspace = sal.pspace;
7355}
7356
7357/* set_raw_breakpoint is a low level routine for allocating and
7358 partially initializing a breakpoint of type BPTYPE. The newly
7359 created breakpoint's address, section, source file name, and line
7360 number are provided by SAL. The newly created and partially
7361 initialized breakpoint is added to the breakpoint chain and
7362 is also returned as the value of this function.
7363
7364 It is expected that the caller will complete the initialization of
7365 the newly created breakpoint struct as well as output any status
7366 information regarding the creation of a new breakpoint. In
7367 particular, set_raw_breakpoint does NOT set the breakpoint
7368 number! Care should be taken to not allow an error to occur
7369 prior to completing the initialization of the breakpoint. If this
7370 should happen, a bogus breakpoint will be left on the chain. */
7371
7372struct breakpoint *
7373set_raw_breakpoint (struct gdbarch *gdbarch,
7374 struct symtab_and_line sal, enum bptype bptype,
7375 const struct breakpoint_ops *ops)
7376{
7377 struct breakpoint *b = XNEW (struct breakpoint);
7378
7379 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7380 add_to_breakpoint_chain (b);
7381 return b;
7382}
7383
7384
7385/* Note that the breakpoint object B describes a permanent breakpoint
7386 instruction, hard-wired into the inferior's code. */
7387void
7388make_breakpoint_permanent (struct breakpoint *b)
7389{
7390 struct bp_location *bl;
7391
7392 b->enable_state = bp_permanent;
7393
7394 /* By definition, permanent breakpoints are already present in the
7395 code. Mark all locations as inserted. For now,
7396 make_breakpoint_permanent is called in just one place, so it's
7397 hard to say if it's reasonable to have permanent breakpoint with
7398 multiple locations or not, but it's easy to implement. */
7399 for (bl = b->loc; bl; bl = bl->next)
7400 bl->inserted = 1;
7401}
7402
7403/* Call this routine when stepping and nexting to enable a breakpoint
7404 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7405 initiated the operation. */
7406
7407void
7408set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7409{
7410 struct breakpoint *b, *b_tmp;
7411 int thread = tp->num;
7412
7413 /* To avoid having to rescan all objfile symbols at every step,
7414 we maintain a list of continually-inserted but always disabled
7415 longjmp "master" breakpoints. Here, we simply create momentary
7416 clones of those and enable them for the requested thread. */
7417 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7418 if (b->pspace == current_program_space
7419 && (b->type == bp_longjmp_master
7420 || b->type == bp_exception_master))
7421 {
7422 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7423 struct breakpoint *clone;
7424
7425 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7426 after their removal. */
7427 clone = momentary_breakpoint_from_master (b, type,
7428 &longjmp_breakpoint_ops, 1);
7429 clone->thread = thread;
7430 }
7431
7432 tp->initiating_frame = frame;
7433}
7434
7435/* Delete all longjmp breakpoints from THREAD. */
7436void
7437delete_longjmp_breakpoint (int thread)
7438{
7439 struct breakpoint *b, *b_tmp;
7440
7441 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7442 if (b->type == bp_longjmp || b->type == bp_exception)
7443 {
7444 if (b->thread == thread)
7445 delete_breakpoint (b);
7446 }
7447}
7448
7449void
7450delete_longjmp_breakpoint_at_next_stop (int thread)
7451{
7452 struct breakpoint *b, *b_tmp;
7453
7454 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7455 if (b->type == bp_longjmp || b->type == bp_exception)
7456 {
7457 if (b->thread == thread)
7458 b->disposition = disp_del_at_next_stop;
7459 }
7460}
7461
7462/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7463 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7464 pointer to any of them. Return NULL if this system cannot place longjmp
7465 breakpoints. */
7466
7467struct breakpoint *
7468set_longjmp_breakpoint_for_call_dummy (void)
7469{
7470 struct breakpoint *b, *retval = NULL;
7471
7472 ALL_BREAKPOINTS (b)
7473 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7474 {
7475 struct breakpoint *new_b;
7476
7477 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7478 &momentary_breakpoint_ops,
7479 1);
7480 new_b->thread = pid_to_thread_id (inferior_ptid);
7481
7482 /* Link NEW_B into the chain of RETVAL breakpoints. */
7483
7484 gdb_assert (new_b->related_breakpoint == new_b);
7485 if (retval == NULL)
7486 retval = new_b;
7487 new_b->related_breakpoint = retval;
7488 while (retval->related_breakpoint != new_b->related_breakpoint)
7489 retval = retval->related_breakpoint;
7490 retval->related_breakpoint = new_b;
7491 }
7492
7493 return retval;
7494}
7495
7496/* Verify all existing dummy frames and their associated breakpoints for
7497 TP. Remove those which can no longer be found in the current frame
7498 stack.
7499
7500 You should call this function only at places where it is safe to currently
7501 unwind the whole stack. Failed stack unwind would discard live dummy
7502 frames. */
7503
7504void
7505check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7506{
7507 struct breakpoint *b, *b_tmp;
7508
7509 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7510 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
7511 {
7512 struct breakpoint *dummy_b = b->related_breakpoint;
7513
7514 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7515 dummy_b = dummy_b->related_breakpoint;
7516 if (dummy_b->type != bp_call_dummy
7517 || frame_find_by_id (dummy_b->frame_id) != NULL)
7518 continue;
7519
7520 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7521
7522 while (b->related_breakpoint != b)
7523 {
7524 if (b_tmp == b->related_breakpoint)
7525 b_tmp = b->related_breakpoint->next;
7526 delete_breakpoint (b->related_breakpoint);
7527 }
7528 delete_breakpoint (b);
7529 }
7530}
7531
7532void
7533enable_overlay_breakpoints (void)
7534{
7535 struct breakpoint *b;
7536
7537 ALL_BREAKPOINTS (b)
7538 if (b->type == bp_overlay_event)
7539 {
7540 b->enable_state = bp_enabled;
7541 update_global_location_list (UGLL_MAY_INSERT);
7542 overlay_events_enabled = 1;
7543 }
7544}
7545
7546void
7547disable_overlay_breakpoints (void)
7548{
7549 struct breakpoint *b;
7550
7551 ALL_BREAKPOINTS (b)
7552 if (b->type == bp_overlay_event)
7553 {
7554 b->enable_state = bp_disabled;
7555 update_global_location_list (UGLL_DONT_INSERT);
7556 overlay_events_enabled = 0;
7557 }
7558}
7559
7560/* Set an active std::terminate breakpoint for each std::terminate
7561 master breakpoint. */
7562void
7563set_std_terminate_breakpoint (void)
7564{
7565 struct breakpoint *b, *b_tmp;
7566
7567 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7568 if (b->pspace == current_program_space
7569 && b->type == bp_std_terminate_master)
7570 {
7571 momentary_breakpoint_from_master (b, bp_std_terminate,
7572 &momentary_breakpoint_ops, 1);
7573 }
7574}
7575
7576/* Delete all the std::terminate breakpoints. */
7577void
7578delete_std_terminate_breakpoint (void)
7579{
7580 struct breakpoint *b, *b_tmp;
7581
7582 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7583 if (b->type == bp_std_terminate)
7584 delete_breakpoint (b);
7585}
7586
7587struct breakpoint *
7588create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7589{
7590 struct breakpoint *b;
7591
7592 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7593 &internal_breakpoint_ops);
7594
7595 b->enable_state = bp_enabled;
7596 /* addr_string has to be used or breakpoint_re_set will delete me. */
7597 b->addr_string
7598 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7599
7600 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7601
7602 return b;
7603}
7604
7605void
7606remove_thread_event_breakpoints (void)
7607{
7608 struct breakpoint *b, *b_tmp;
7609
7610 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7611 if (b->type == bp_thread_event
7612 && b->loc->pspace == current_program_space)
7613 delete_breakpoint (b);
7614}
7615
7616struct lang_and_radix
7617 {
7618 enum language lang;
7619 int radix;
7620 };
7621
7622/* Create a breakpoint for JIT code registration and unregistration. */
7623
7624struct breakpoint *
7625create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7626{
7627 struct breakpoint *b;
7628
7629 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7630 &internal_breakpoint_ops);
7631 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7632 return b;
7633}
7634
7635/* Remove JIT code registration and unregistration breakpoint(s). */
7636
7637void
7638remove_jit_event_breakpoints (void)
7639{
7640 struct breakpoint *b, *b_tmp;
7641
7642 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7643 if (b->type == bp_jit_event
7644 && b->loc->pspace == current_program_space)
7645 delete_breakpoint (b);
7646}
7647
7648void
7649remove_solib_event_breakpoints (void)
7650{
7651 struct breakpoint *b, *b_tmp;
7652
7653 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7654 if (b->type == bp_shlib_event
7655 && b->loc->pspace == current_program_space)
7656 delete_breakpoint (b);
7657}
7658
7659/* See breakpoint.h. */
7660
7661void
7662remove_solib_event_breakpoints_at_next_stop (void)
7663{
7664 struct breakpoint *b, *b_tmp;
7665
7666 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7667 if (b->type == bp_shlib_event
7668 && b->loc->pspace == current_program_space)
7669 b->disposition = disp_del_at_next_stop;
7670}
7671
7672/* Helper for create_solib_event_breakpoint /
7673 create_and_insert_solib_event_breakpoint. Allows specifying which
7674 INSERT_MODE to pass through to update_global_location_list. */
7675
7676static struct breakpoint *
7677create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7678 enum ugll_insert_mode insert_mode)
7679{
7680 struct breakpoint *b;
7681
7682 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7683 &internal_breakpoint_ops);
7684 update_global_location_list_nothrow (insert_mode);
7685 return b;
7686}
7687
7688struct breakpoint *
7689create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7690{
7691 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7692}
7693
7694/* See breakpoint.h. */
7695
7696struct breakpoint *
7697create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7698{
7699 struct breakpoint *b;
7700
7701 /* Explicitly tell update_global_location_list to insert
7702 locations. */
7703 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7704 if (!b->loc->inserted)
7705 {
7706 delete_breakpoint (b);
7707 return NULL;
7708 }
7709 return b;
7710}
7711
7712/* Disable any breakpoints that are on code in shared libraries. Only
7713 apply to enabled breakpoints, disabled ones can just stay disabled. */
7714
7715void
7716disable_breakpoints_in_shlibs (void)
7717{
7718 struct bp_location *loc, **locp_tmp;
7719
7720 ALL_BP_LOCATIONS (loc, locp_tmp)
7721 {
7722 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7723 struct breakpoint *b = loc->owner;
7724
7725 /* We apply the check to all breakpoints, including disabled for
7726 those with loc->duplicate set. This is so that when breakpoint
7727 becomes enabled, or the duplicate is removed, gdb will try to
7728 insert all breakpoints. If we don't set shlib_disabled here,
7729 we'll try to insert those breakpoints and fail. */
7730 if (((b->type == bp_breakpoint)
7731 || (b->type == bp_jit_event)
7732 || (b->type == bp_hardware_breakpoint)
7733 || (is_tracepoint (b)))
7734 && loc->pspace == current_program_space
7735 && !loc->shlib_disabled
7736 && solib_name_from_address (loc->pspace, loc->address)
7737 )
7738 {
7739 loc->shlib_disabled = 1;
7740 }
7741 }
7742}
7743
7744/* Disable any breakpoints and tracepoints that are in SOLIB upon
7745 notification of unloaded_shlib. Only apply to enabled breakpoints,
7746 disabled ones can just stay disabled. */
7747
7748static void
7749disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7750{
7751 struct bp_location *loc, **locp_tmp;
7752 int disabled_shlib_breaks = 0;
7753
7754 /* SunOS a.out shared libraries are always mapped, so do not
7755 disable breakpoints; they will only be reported as unloaded
7756 through clear_solib when GDB discards its shared library
7757 list. See clear_solib for more information. */
7758 if (exec_bfd != NULL
7759 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7760 return;
7761
7762 ALL_BP_LOCATIONS (loc, locp_tmp)
7763 {
7764 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7765 struct breakpoint *b = loc->owner;
7766
7767 if (solib->pspace == loc->pspace
7768 && !loc->shlib_disabled
7769 && (((b->type == bp_breakpoint
7770 || b->type == bp_jit_event
7771 || b->type == bp_hardware_breakpoint)
7772 && (loc->loc_type == bp_loc_hardware_breakpoint
7773 || loc->loc_type == bp_loc_software_breakpoint))
7774 || is_tracepoint (b))
7775 && solib_contains_address_p (solib, loc->address))
7776 {
7777 loc->shlib_disabled = 1;
7778 /* At this point, we cannot rely on remove_breakpoint
7779 succeeding so we must mark the breakpoint as not inserted
7780 to prevent future errors occurring in remove_breakpoints. */
7781 loc->inserted = 0;
7782
7783 /* This may cause duplicate notifications for the same breakpoint. */
7784 observer_notify_breakpoint_modified (b);
7785
7786 if (!disabled_shlib_breaks)
7787 {
7788 target_terminal_ours_for_output ();
7789 warning (_("Temporarily disabling breakpoints "
7790 "for unloaded shared library \"%s\""),
7791 solib->so_name);
7792 }
7793 disabled_shlib_breaks = 1;
7794 }
7795 }
7796}
7797
7798/* Disable any breakpoints and tracepoints in OBJFILE upon
7799 notification of free_objfile. Only apply to enabled breakpoints,
7800 disabled ones can just stay disabled. */
7801
7802static void
7803disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7804{
7805 struct breakpoint *b;
7806
7807 if (objfile == NULL)
7808 return;
7809
7810 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7811 managed by the user with add-symbol-file/remove-symbol-file.
7812 Similarly to how breakpoints in shared libraries are handled in
7813 response to "nosharedlibrary", mark breakpoints in such modules
7814 shlib_disabled so they end up uninserted on the next global
7815 location list update. Shared libraries not loaded by the user
7816 aren't handled here -- they're already handled in
7817 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7818 solib_unloaded observer. We skip objfiles that are not
7819 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7820 main objfile). */
7821 if ((objfile->flags & OBJF_SHARED) == 0
7822 || (objfile->flags & OBJF_USERLOADED) == 0)
7823 return;
7824
7825 ALL_BREAKPOINTS (b)
7826 {
7827 struct bp_location *loc;
7828 int bp_modified = 0;
7829
7830 if (!is_breakpoint (b) && !is_tracepoint (b))
7831 continue;
7832
7833 for (loc = b->loc; loc != NULL; loc = loc->next)
7834 {
7835 CORE_ADDR loc_addr = loc->address;
7836
7837 if (loc->loc_type != bp_loc_hardware_breakpoint
7838 && loc->loc_type != bp_loc_software_breakpoint)
7839 continue;
7840
7841 if (loc->shlib_disabled != 0)
7842 continue;
7843
7844 if (objfile->pspace != loc->pspace)
7845 continue;
7846
7847 if (loc->loc_type != bp_loc_hardware_breakpoint
7848 && loc->loc_type != bp_loc_software_breakpoint)
7849 continue;
7850
7851 if (is_addr_in_objfile (loc_addr, objfile))
7852 {
7853 loc->shlib_disabled = 1;
7854 /* At this point, we don't know whether the object was
7855 unmapped from the inferior or not, so leave the
7856 inserted flag alone. We'll handle failure to
7857 uninsert quietly, in case the object was indeed
7858 unmapped. */
7859
7860 mark_breakpoint_location_modified (loc);
7861
7862 bp_modified = 1;
7863 }
7864 }
7865
7866 if (bp_modified)
7867 observer_notify_breakpoint_modified (b);
7868 }
7869}
7870
7871/* FORK & VFORK catchpoints. */
7872
7873/* An instance of this type is used to represent a fork or vfork
7874 catchpoint. It includes a "struct breakpoint" as a kind of base
7875 class; users downcast to "struct breakpoint *" when needed. A
7876 breakpoint is really of this type iff its ops pointer points to
7877 CATCH_FORK_BREAKPOINT_OPS. */
7878
7879struct fork_catchpoint
7880{
7881 /* The base class. */
7882 struct breakpoint base;
7883
7884 /* Process id of a child process whose forking triggered this
7885 catchpoint. This field is only valid immediately after this
7886 catchpoint has triggered. */
7887 ptid_t forked_inferior_pid;
7888};
7889
7890/* Implement the "insert" breakpoint_ops method for fork
7891 catchpoints. */
7892
7893static int
7894insert_catch_fork (struct bp_location *bl)
7895{
7896 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7897}
7898
7899/* Implement the "remove" breakpoint_ops method for fork
7900 catchpoints. */
7901
7902static int
7903remove_catch_fork (struct bp_location *bl)
7904{
7905 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7906}
7907
7908/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7909 catchpoints. */
7910
7911static int
7912breakpoint_hit_catch_fork (const struct bp_location *bl,
7913 struct address_space *aspace, CORE_ADDR bp_addr,
7914 const struct target_waitstatus *ws)
7915{
7916 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7917
7918 if (ws->kind != TARGET_WAITKIND_FORKED)
7919 return 0;
7920
7921 c->forked_inferior_pid = ws->value.related_pid;
7922 return 1;
7923}
7924
7925/* Implement the "print_it" breakpoint_ops method for fork
7926 catchpoints. */
7927
7928static enum print_stop_action
7929print_it_catch_fork (bpstat bs)
7930{
7931 struct ui_out *uiout = current_uiout;
7932 struct breakpoint *b = bs->breakpoint_at;
7933 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7934
7935 annotate_catchpoint (b->number);
7936 if (b->disposition == disp_del)
7937 ui_out_text (uiout, "\nTemporary catchpoint ");
7938 else
7939 ui_out_text (uiout, "\nCatchpoint ");
7940 if (ui_out_is_mi_like_p (uiout))
7941 {
7942 ui_out_field_string (uiout, "reason",
7943 async_reason_lookup (EXEC_ASYNC_FORK));
7944 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7945 }
7946 ui_out_field_int (uiout, "bkptno", b->number);
7947 ui_out_text (uiout, " (forked process ");
7948 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7949 ui_out_text (uiout, "), ");
7950 return PRINT_SRC_AND_LOC;
7951}
7952
7953/* Implement the "print_one" breakpoint_ops method for fork
7954 catchpoints. */
7955
7956static void
7957print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7958{
7959 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7960 struct value_print_options opts;
7961 struct ui_out *uiout = current_uiout;
7962
7963 get_user_print_options (&opts);
7964
7965 /* Field 4, the address, is omitted (which makes the columns not
7966 line up too nicely with the headers, but the effect is relatively
7967 readable). */
7968 if (opts.addressprint)
7969 ui_out_field_skip (uiout, "addr");
7970 annotate_field (5);
7971 ui_out_text (uiout, "fork");
7972 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7973 {
7974 ui_out_text (uiout, ", process ");
7975 ui_out_field_int (uiout, "what",
7976 ptid_get_pid (c->forked_inferior_pid));
7977 ui_out_spaces (uiout, 1);
7978 }
7979
7980 if (ui_out_is_mi_like_p (uiout))
7981 ui_out_field_string (uiout, "catch-type", "fork");
7982}
7983
7984/* Implement the "print_mention" breakpoint_ops method for fork
7985 catchpoints. */
7986
7987static void
7988print_mention_catch_fork (struct breakpoint *b)
7989{
7990 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7991}
7992
7993/* Implement the "print_recreate" breakpoint_ops method for fork
7994 catchpoints. */
7995
7996static void
7997print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7998{
7999 fprintf_unfiltered (fp, "catch fork");
8000 print_recreate_thread (b, fp);
8001}
8002
8003/* The breakpoint_ops structure to be used in fork catchpoints. */
8004
8005static struct breakpoint_ops catch_fork_breakpoint_ops;
8006
8007/* Implement the "insert" breakpoint_ops method for vfork
8008 catchpoints. */
8009
8010static int
8011insert_catch_vfork (struct bp_location *bl)
8012{
8013 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8014}
8015
8016/* Implement the "remove" breakpoint_ops method for vfork
8017 catchpoints. */
8018
8019static int
8020remove_catch_vfork (struct bp_location *bl)
8021{
8022 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8023}
8024
8025/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8026 catchpoints. */
8027
8028static int
8029breakpoint_hit_catch_vfork (const struct bp_location *bl,
8030 struct address_space *aspace, CORE_ADDR bp_addr,
8031 const struct target_waitstatus *ws)
8032{
8033 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8034
8035 if (ws->kind != TARGET_WAITKIND_VFORKED)
8036 return 0;
8037
8038 c->forked_inferior_pid = ws->value.related_pid;
8039 return 1;
8040}
8041
8042/* Implement the "print_it" breakpoint_ops method for vfork
8043 catchpoints. */
8044
8045static enum print_stop_action
8046print_it_catch_vfork (bpstat bs)
8047{
8048 struct ui_out *uiout = current_uiout;
8049 struct breakpoint *b = bs->breakpoint_at;
8050 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8051
8052 annotate_catchpoint (b->number);
8053 if (b->disposition == disp_del)
8054 ui_out_text (uiout, "\nTemporary catchpoint ");
8055 else
8056 ui_out_text (uiout, "\nCatchpoint ");
8057 if (ui_out_is_mi_like_p (uiout))
8058 {
8059 ui_out_field_string (uiout, "reason",
8060 async_reason_lookup (EXEC_ASYNC_VFORK));
8061 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8062 }
8063 ui_out_field_int (uiout, "bkptno", b->number);
8064 ui_out_text (uiout, " (vforked process ");
8065 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8066 ui_out_text (uiout, "), ");
8067 return PRINT_SRC_AND_LOC;
8068}
8069
8070/* Implement the "print_one" breakpoint_ops method for vfork
8071 catchpoints. */
8072
8073static void
8074print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8075{
8076 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8077 struct value_print_options opts;
8078 struct ui_out *uiout = current_uiout;
8079
8080 get_user_print_options (&opts);
8081 /* Field 4, the address, is omitted (which makes the columns not
8082 line up too nicely with the headers, but the effect is relatively
8083 readable). */
8084 if (opts.addressprint)
8085 ui_out_field_skip (uiout, "addr");
8086 annotate_field (5);
8087 ui_out_text (uiout, "vfork");
8088 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8089 {
8090 ui_out_text (uiout, ", process ");
8091 ui_out_field_int (uiout, "what",
8092 ptid_get_pid (c->forked_inferior_pid));
8093 ui_out_spaces (uiout, 1);
8094 }
8095
8096 if (ui_out_is_mi_like_p (uiout))
8097 ui_out_field_string (uiout, "catch-type", "vfork");
8098}
8099
8100/* Implement the "print_mention" breakpoint_ops method for vfork
8101 catchpoints. */
8102
8103static void
8104print_mention_catch_vfork (struct breakpoint *b)
8105{
8106 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8107}
8108
8109/* Implement the "print_recreate" breakpoint_ops method for vfork
8110 catchpoints. */
8111
8112static void
8113print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8114{
8115 fprintf_unfiltered (fp, "catch vfork");
8116 print_recreate_thread (b, fp);
8117}
8118
8119/* The breakpoint_ops structure to be used in vfork catchpoints. */
8120
8121static struct breakpoint_ops catch_vfork_breakpoint_ops;
8122
8123/* An instance of this type is used to represent an solib catchpoint.
8124 It includes a "struct breakpoint" as a kind of base class; users
8125 downcast to "struct breakpoint *" when needed. A breakpoint is
8126 really of this type iff its ops pointer points to
8127 CATCH_SOLIB_BREAKPOINT_OPS. */
8128
8129struct solib_catchpoint
8130{
8131 /* The base class. */
8132 struct breakpoint base;
8133
8134 /* True for "catch load", false for "catch unload". */
8135 unsigned char is_load;
8136
8137 /* Regular expression to match, if any. COMPILED is only valid when
8138 REGEX is non-NULL. */
8139 char *regex;
8140 regex_t compiled;
8141};
8142
8143static void
8144dtor_catch_solib (struct breakpoint *b)
8145{
8146 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8147
8148 if (self->regex)
8149 regfree (&self->compiled);
8150 xfree (self->regex);
8151
8152 base_breakpoint_ops.dtor (b);
8153}
8154
8155static int
8156insert_catch_solib (struct bp_location *ignore)
8157{
8158 return 0;
8159}
8160
8161static int
8162remove_catch_solib (struct bp_location *ignore)
8163{
8164 return 0;
8165}
8166
8167static int
8168breakpoint_hit_catch_solib (const struct bp_location *bl,
8169 struct address_space *aspace,
8170 CORE_ADDR bp_addr,
8171 const struct target_waitstatus *ws)
8172{
8173 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8174 struct breakpoint *other;
8175
8176 if (ws->kind == TARGET_WAITKIND_LOADED)
8177 return 1;
8178
8179 ALL_BREAKPOINTS (other)
8180 {
8181 struct bp_location *other_bl;
8182
8183 if (other == bl->owner)
8184 continue;
8185
8186 if (other->type != bp_shlib_event)
8187 continue;
8188
8189 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8190 continue;
8191
8192 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8193 {
8194 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8195 return 1;
8196 }
8197 }
8198
8199 return 0;
8200}
8201
8202static void
8203check_status_catch_solib (struct bpstats *bs)
8204{
8205 struct solib_catchpoint *self
8206 = (struct solib_catchpoint *) bs->breakpoint_at;
8207 int ix;
8208
8209 if (self->is_load)
8210 {
8211 struct so_list *iter;
8212
8213 for (ix = 0;
8214 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8215 ix, iter);
8216 ++ix)
8217 {
8218 if (!self->regex
8219 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8220 return;
8221 }
8222 }
8223 else
8224 {
8225 char *iter;
8226
8227 for (ix = 0;
8228 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8229 ix, iter);
8230 ++ix)
8231 {
8232 if (!self->regex
8233 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8234 return;
8235 }
8236 }
8237
8238 bs->stop = 0;
8239 bs->print_it = print_it_noop;
8240}
8241
8242static enum print_stop_action
8243print_it_catch_solib (bpstat bs)
8244{
8245 struct breakpoint *b = bs->breakpoint_at;
8246 struct ui_out *uiout = current_uiout;
8247
8248 annotate_catchpoint (b->number);
8249 if (b->disposition == disp_del)
8250 ui_out_text (uiout, "\nTemporary catchpoint ");
8251 else
8252 ui_out_text (uiout, "\nCatchpoint ");
8253 ui_out_field_int (uiout, "bkptno", b->number);
8254 ui_out_text (uiout, "\n");
8255 if (ui_out_is_mi_like_p (uiout))
8256 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8257 print_solib_event (1);
8258 return PRINT_SRC_AND_LOC;
8259}
8260
8261static void
8262print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8263{
8264 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8265 struct value_print_options opts;
8266 struct ui_out *uiout = current_uiout;
8267 char *msg;
8268
8269 get_user_print_options (&opts);
8270 /* Field 4, the address, is omitted (which makes the columns not
8271 line up too nicely with the headers, but the effect is relatively
8272 readable). */
8273 if (opts.addressprint)
8274 {
8275 annotate_field (4);
8276 ui_out_field_skip (uiout, "addr");
8277 }
8278
8279 annotate_field (5);
8280 if (self->is_load)
8281 {
8282 if (self->regex)
8283 msg = xstrprintf (_("load of library matching %s"), self->regex);
8284 else
8285 msg = xstrdup (_("load of library"));
8286 }
8287 else
8288 {
8289 if (self->regex)
8290 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8291 else
8292 msg = xstrdup (_("unload of library"));
8293 }
8294 ui_out_field_string (uiout, "what", msg);
8295 xfree (msg);
8296
8297 if (ui_out_is_mi_like_p (uiout))
8298 ui_out_field_string (uiout, "catch-type",
8299 self->is_load ? "load" : "unload");
8300}
8301
8302static void
8303print_mention_catch_solib (struct breakpoint *b)
8304{
8305 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8306
8307 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8308 self->is_load ? "load" : "unload");
8309}
8310
8311static void
8312print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8313{
8314 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8315
8316 fprintf_unfiltered (fp, "%s %s",
8317 b->disposition == disp_del ? "tcatch" : "catch",
8318 self->is_load ? "load" : "unload");
8319 if (self->regex)
8320 fprintf_unfiltered (fp, " %s", self->regex);
8321 fprintf_unfiltered (fp, "\n");
8322}
8323
8324static struct breakpoint_ops catch_solib_breakpoint_ops;
8325
8326/* Shared helper function (MI and CLI) for creating and installing
8327 a shared object event catchpoint. If IS_LOAD is non-zero then
8328 the events to be caught are load events, otherwise they are
8329 unload events. If IS_TEMP is non-zero the catchpoint is a
8330 temporary one. If ENABLED is non-zero the catchpoint is
8331 created in an enabled state. */
8332
8333void
8334add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8335{
8336 struct solib_catchpoint *c;
8337 struct gdbarch *gdbarch = get_current_arch ();
8338 struct cleanup *cleanup;
8339
8340 if (!arg)
8341 arg = "";
8342 arg = skip_spaces (arg);
8343
8344 c = XCNEW (struct solib_catchpoint);
8345 cleanup = make_cleanup (xfree, c);
8346
8347 if (*arg != '\0')
8348 {
8349 int errcode;
8350
8351 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8352 if (errcode != 0)
8353 {
8354 char *err = get_regcomp_error (errcode, &c->compiled);
8355
8356 make_cleanup (xfree, err);
8357 error (_("Invalid regexp (%s): %s"), err, arg);
8358 }
8359 c->regex = xstrdup (arg);
8360 }
8361
8362 c->is_load = is_load;
8363 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8364 &catch_solib_breakpoint_ops);
8365
8366 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8367
8368 discard_cleanups (cleanup);
8369 install_breakpoint (0, &c->base, 1);
8370}
8371
8372/* A helper function that does all the work for "catch load" and
8373 "catch unload". */
8374
8375static void
8376catch_load_or_unload (char *arg, int from_tty, int is_load,
8377 struct cmd_list_element *command)
8378{
8379 int tempflag;
8380 const int enabled = 1;
8381
8382 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8383
8384 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8385}
8386
8387static void
8388catch_load_command_1 (char *arg, int from_tty,
8389 struct cmd_list_element *command)
8390{
8391 catch_load_or_unload (arg, from_tty, 1, command);
8392}
8393
8394static void
8395catch_unload_command_1 (char *arg, int from_tty,
8396 struct cmd_list_element *command)
8397{
8398 catch_load_or_unload (arg, from_tty, 0, command);
8399}
8400
8401/* An instance of this type is used to represent a syscall catchpoint.
8402 It includes a "struct breakpoint" as a kind of base class; users
8403 downcast to "struct breakpoint *" when needed. A breakpoint is
8404 really of this type iff its ops pointer points to
8405 CATCH_SYSCALL_BREAKPOINT_OPS. */
8406
8407struct syscall_catchpoint
8408{
8409 /* The base class. */
8410 struct breakpoint base;
8411
8412 /* Syscall numbers used for the 'catch syscall' feature. If no
8413 syscall has been specified for filtering, its value is NULL.
8414 Otherwise, it holds a list of all syscalls to be caught. The
8415 list elements are allocated with xmalloc. */
8416 VEC(int) *syscalls_to_be_caught;
8417};
8418
8419/* Implement the "dtor" breakpoint_ops method for syscall
8420 catchpoints. */
8421
8422static void
8423dtor_catch_syscall (struct breakpoint *b)
8424{
8425 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8426
8427 VEC_free (int, c->syscalls_to_be_caught);
8428
8429 base_breakpoint_ops.dtor (b);
8430}
8431
8432static const struct inferior_data *catch_syscall_inferior_data = NULL;
8433
8434struct catch_syscall_inferior_data
8435{
8436 /* We keep a count of the number of times the user has requested a
8437 particular syscall to be tracked, and pass this information to the
8438 target. This lets capable targets implement filtering directly. */
8439
8440 /* Number of times that "any" syscall is requested. */
8441 int any_syscall_count;
8442
8443 /* Count of each system call. */
8444 VEC(int) *syscalls_counts;
8445
8446 /* This counts all syscall catch requests, so we can readily determine
8447 if any catching is necessary. */
8448 int total_syscalls_count;
8449};
8450
8451static struct catch_syscall_inferior_data*
8452get_catch_syscall_inferior_data (struct inferior *inf)
8453{
8454 struct catch_syscall_inferior_data *inf_data;
8455
8456 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8457 if (inf_data == NULL)
8458 {
8459 inf_data = XCNEW (struct catch_syscall_inferior_data);
8460 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8461 }
8462
8463 return inf_data;
8464}
8465
8466static void
8467catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8468{
8469 xfree (arg);
8470}
8471
8472
8473/* Implement the "insert" breakpoint_ops method for syscall
8474 catchpoints. */
8475
8476static int
8477insert_catch_syscall (struct bp_location *bl)
8478{
8479 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8480 struct inferior *inf = current_inferior ();
8481 struct catch_syscall_inferior_data *inf_data
8482 = get_catch_syscall_inferior_data (inf);
8483
8484 ++inf_data->total_syscalls_count;
8485 if (!c->syscalls_to_be_caught)
8486 ++inf_data->any_syscall_count;
8487 else
8488 {
8489 int i, iter;
8490
8491 for (i = 0;
8492 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8493 i++)
8494 {
8495 int elem;
8496
8497 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8498 {
8499 int old_size = VEC_length (int, inf_data->syscalls_counts);
8500 uintptr_t vec_addr_offset
8501 = old_size * ((uintptr_t) sizeof (int));
8502 uintptr_t vec_addr;
8503 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8504 vec_addr = ((uintptr_t) VEC_address (int,
8505 inf_data->syscalls_counts)
8506 + vec_addr_offset);
8507 memset ((void *) vec_addr, 0,
8508 (iter + 1 - old_size) * sizeof (int));
8509 }
8510 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8511 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8512 }
8513 }
8514
8515 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8516 inf_data->total_syscalls_count != 0,
8517 inf_data->any_syscall_count,
8518 VEC_length (int,
8519 inf_data->syscalls_counts),
8520 VEC_address (int,
8521 inf_data->syscalls_counts));
8522}
8523
8524/* Implement the "remove" breakpoint_ops method for syscall
8525 catchpoints. */
8526
8527static int
8528remove_catch_syscall (struct bp_location *bl)
8529{
8530 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8531 struct inferior *inf = current_inferior ();
8532 struct catch_syscall_inferior_data *inf_data
8533 = get_catch_syscall_inferior_data (inf);
8534
8535 --inf_data->total_syscalls_count;
8536 if (!c->syscalls_to_be_caught)
8537 --inf_data->any_syscall_count;
8538 else
8539 {
8540 int i, iter;
8541
8542 for (i = 0;
8543 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8544 i++)
8545 {
8546 int elem;
8547 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8548 /* Shouldn't happen. */
8549 continue;
8550 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8551 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8552 }
8553 }
8554
8555 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8556 inf_data->total_syscalls_count != 0,
8557 inf_data->any_syscall_count,
8558 VEC_length (int,
8559 inf_data->syscalls_counts),
8560 VEC_address (int,
8561 inf_data->syscalls_counts));
8562}
8563
8564/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8565 catchpoints. */
8566
8567static int
8568breakpoint_hit_catch_syscall (const struct bp_location *bl,
8569 struct address_space *aspace, CORE_ADDR bp_addr,
8570 const struct target_waitstatus *ws)
8571{
8572 /* We must check if we are catching specific syscalls in this
8573 breakpoint. If we are, then we must guarantee that the called
8574 syscall is the same syscall we are catching. */
8575 int syscall_number = 0;
8576 const struct syscall_catchpoint *c
8577 = (const struct syscall_catchpoint *) bl->owner;
8578
8579 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8580 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8581 return 0;
8582
8583 syscall_number = ws->value.syscall_number;
8584
8585 /* Now, checking if the syscall is the same. */
8586 if (c->syscalls_to_be_caught)
8587 {
8588 int i, iter;
8589
8590 for (i = 0;
8591 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8592 i++)
8593 if (syscall_number == iter)
8594 return 1;
8595
8596 return 0;
8597 }
8598
8599 return 1;
8600}
8601
8602/* Implement the "print_it" breakpoint_ops method for syscall
8603 catchpoints. */
8604
8605static enum print_stop_action
8606print_it_catch_syscall (bpstat bs)
8607{
8608 struct ui_out *uiout = current_uiout;
8609 struct breakpoint *b = bs->breakpoint_at;
8610 /* These are needed because we want to know in which state a
8611 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8612 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8613 must print "called syscall" or "returned from syscall". */
8614 ptid_t ptid;
8615 struct target_waitstatus last;
8616 struct syscall s;
8617
8618 get_last_target_status (&ptid, &last);
8619
8620 get_syscall_by_number (last.value.syscall_number, &s);
8621
8622 annotate_catchpoint (b->number);
8623
8624 if (b->disposition == disp_del)
8625 ui_out_text (uiout, "\nTemporary catchpoint ");
8626 else
8627 ui_out_text (uiout, "\nCatchpoint ");
8628 if (ui_out_is_mi_like_p (uiout))
8629 {
8630 ui_out_field_string (uiout, "reason",
8631 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8632 ? EXEC_ASYNC_SYSCALL_ENTRY
8633 : EXEC_ASYNC_SYSCALL_RETURN));
8634 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8635 }
8636 ui_out_field_int (uiout, "bkptno", b->number);
8637
8638 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8639 ui_out_text (uiout, " (call to syscall ");
8640 else
8641 ui_out_text (uiout, " (returned from syscall ");
8642
8643 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8644 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8645 if (s.name != NULL)
8646 ui_out_field_string (uiout, "syscall-name", s.name);
8647
8648 ui_out_text (uiout, "), ");
8649
8650 return PRINT_SRC_AND_LOC;
8651}
8652
8653/* Implement the "print_one" breakpoint_ops method for syscall
8654 catchpoints. */
8655
8656static void
8657print_one_catch_syscall (struct breakpoint *b,
8658 struct bp_location **last_loc)
8659{
8660 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8661 struct value_print_options opts;
8662 struct ui_out *uiout = current_uiout;
8663
8664 get_user_print_options (&opts);
8665 /* Field 4, the address, is omitted (which makes the columns not
8666 line up too nicely with the headers, but the effect is relatively
8667 readable). */
8668 if (opts.addressprint)
8669 ui_out_field_skip (uiout, "addr");
8670 annotate_field (5);
8671
8672 if (c->syscalls_to_be_caught
8673 && VEC_length (int, c->syscalls_to_be_caught) > 1)
8674 ui_out_text (uiout, "syscalls \"");
8675 else
8676 ui_out_text (uiout, "syscall \"");
8677
8678 if (c->syscalls_to_be_caught)
8679 {
8680 int i, iter;
8681 char *text = xstrprintf ("%s", "");
8682
8683 for (i = 0;
8684 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8685 i++)
8686 {
8687 char *x = text;
8688 struct syscall s;
8689 get_syscall_by_number (iter, &s);
8690
8691 if (s.name != NULL)
8692 text = xstrprintf ("%s%s, ", text, s.name);
8693 else
8694 text = xstrprintf ("%s%d, ", text, iter);
8695
8696 /* We have to xfree the last 'text' (now stored at 'x')
8697 because xstrprintf dynamically allocates new space for it
8698 on every call. */
8699 xfree (x);
8700 }
8701 /* Remove the last comma. */
8702 text[strlen (text) - 2] = '\0';
8703 ui_out_field_string (uiout, "what", text);
8704 }
8705 else
8706 ui_out_field_string (uiout, "what", "<any syscall>");
8707 ui_out_text (uiout, "\" ");
8708
8709 if (ui_out_is_mi_like_p (uiout))
8710 ui_out_field_string (uiout, "catch-type", "syscall");
8711}
8712
8713/* Implement the "print_mention" breakpoint_ops method for syscall
8714 catchpoints. */
8715
8716static void
8717print_mention_catch_syscall (struct breakpoint *b)
8718{
8719 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8720
8721 if (c->syscalls_to_be_caught)
8722 {
8723 int i, iter;
8724
8725 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8726 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8727 else
8728 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8729
8730 for (i = 0;
8731 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8732 i++)
8733 {
8734 struct syscall s;
8735 get_syscall_by_number (iter, &s);
8736
8737 if (s.name)
8738 printf_filtered (" '%s' [%d]", s.name, s.number);
8739 else
8740 printf_filtered (" %d", s.number);
8741 }
8742 printf_filtered (")");
8743 }
8744 else
8745 printf_filtered (_("Catchpoint %d (any syscall)"),
8746 b->number);
8747}
8748
8749/* Implement the "print_recreate" breakpoint_ops method for syscall
8750 catchpoints. */
8751
8752static void
8753print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8754{
8755 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8756
8757 fprintf_unfiltered (fp, "catch syscall");
8758
8759 if (c->syscalls_to_be_caught)
8760 {
8761 int i, iter;
8762
8763 for (i = 0;
8764 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8765 i++)
8766 {
8767 struct syscall s;
8768
8769 get_syscall_by_number (iter, &s);
8770 if (s.name)
8771 fprintf_unfiltered (fp, " %s", s.name);
8772 else
8773 fprintf_unfiltered (fp, " %d", s.number);
8774 }
8775 }
8776 print_recreate_thread (b, fp);
8777}
8778
8779/* The breakpoint_ops structure to be used in syscall catchpoints. */
8780
8781static struct breakpoint_ops catch_syscall_breakpoint_ops;
8782
8783/* Returns non-zero if 'b' is a syscall catchpoint. */
8784
8785static int
8786syscall_catchpoint_p (struct breakpoint *b)
8787{
8788 return (b->ops == &catch_syscall_breakpoint_ops);
8789}
8790
8791/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8792 is non-zero, then make the breakpoint temporary. If COND_STRING is
8793 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8794 the breakpoint_ops structure associated to the catchpoint. */
8795
8796void
8797init_catchpoint (struct breakpoint *b,
8798 struct gdbarch *gdbarch, int tempflag,
8799 char *cond_string,
8800 const struct breakpoint_ops *ops)
8801{
8802 struct symtab_and_line sal;
8803
8804 init_sal (&sal);
8805 sal.pspace = current_program_space;
8806
8807 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8808
8809 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8810 b->disposition = tempflag ? disp_del : disp_donttouch;
8811}
8812
8813void
8814install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8815{
8816 add_to_breakpoint_chain (b);
8817 set_breakpoint_number (internal, b);
8818 if (is_tracepoint (b))
8819 set_tracepoint_count (breakpoint_count);
8820 if (!internal)
8821 mention (b);
8822 observer_notify_breakpoint_created (b);
8823
8824 if (update_gll)
8825 update_global_location_list (UGLL_MAY_INSERT);
8826}
8827
8828static void
8829create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8830 int tempflag, char *cond_string,
8831 const struct breakpoint_ops *ops)
8832{
8833 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8834
8835 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8836
8837 c->forked_inferior_pid = null_ptid;
8838
8839 install_breakpoint (0, &c->base, 1);
8840}
8841
8842/* Exec catchpoints. */
8843
8844/* An instance of this type is used to represent an exec catchpoint.
8845 It includes a "struct breakpoint" as a kind of base class; users
8846 downcast to "struct breakpoint *" when needed. A breakpoint is
8847 really of this type iff its ops pointer points to
8848 CATCH_EXEC_BREAKPOINT_OPS. */
8849
8850struct exec_catchpoint
8851{
8852 /* The base class. */
8853 struct breakpoint base;
8854
8855 /* Filename of a program whose exec triggered this catchpoint.
8856 This field is only valid immediately after this catchpoint has
8857 triggered. */
8858 char *exec_pathname;
8859};
8860
8861/* Implement the "dtor" breakpoint_ops method for exec
8862 catchpoints. */
8863
8864static void
8865dtor_catch_exec (struct breakpoint *b)
8866{
8867 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8868
8869 xfree (c->exec_pathname);
8870
8871 base_breakpoint_ops.dtor (b);
8872}
8873
8874static int
8875insert_catch_exec (struct bp_location *bl)
8876{
8877 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8878}
8879
8880static int
8881remove_catch_exec (struct bp_location *bl)
8882{
8883 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8884}
8885
8886static int
8887breakpoint_hit_catch_exec (const struct bp_location *bl,
8888 struct address_space *aspace, CORE_ADDR bp_addr,
8889 const struct target_waitstatus *ws)
8890{
8891 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8892
8893 if (ws->kind != TARGET_WAITKIND_EXECD)
8894 return 0;
8895
8896 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8897 return 1;
8898}
8899
8900static enum print_stop_action
8901print_it_catch_exec (bpstat bs)
8902{
8903 struct ui_out *uiout = current_uiout;
8904 struct breakpoint *b = bs->breakpoint_at;
8905 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8906
8907 annotate_catchpoint (b->number);
8908 if (b->disposition == disp_del)
8909 ui_out_text (uiout, "\nTemporary catchpoint ");
8910 else
8911 ui_out_text (uiout, "\nCatchpoint ");
8912 if (ui_out_is_mi_like_p (uiout))
8913 {
8914 ui_out_field_string (uiout, "reason",
8915 async_reason_lookup (EXEC_ASYNC_EXEC));
8916 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8917 }
8918 ui_out_field_int (uiout, "bkptno", b->number);
8919 ui_out_text (uiout, " (exec'd ");
8920 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8921 ui_out_text (uiout, "), ");
8922
8923 return PRINT_SRC_AND_LOC;
8924}
8925
8926static void
8927print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8928{
8929 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8930 struct value_print_options opts;
8931 struct ui_out *uiout = current_uiout;
8932
8933 get_user_print_options (&opts);
8934
8935 /* Field 4, the address, is omitted (which makes the columns
8936 not line up too nicely with the headers, but the effect
8937 is relatively readable). */
8938 if (opts.addressprint)
8939 ui_out_field_skip (uiout, "addr");
8940 annotate_field (5);
8941 ui_out_text (uiout, "exec");
8942 if (c->exec_pathname != NULL)
8943 {
8944 ui_out_text (uiout, ", program \"");
8945 ui_out_field_string (uiout, "what", c->exec_pathname);
8946 ui_out_text (uiout, "\" ");
8947 }
8948
8949 if (ui_out_is_mi_like_p (uiout))
8950 ui_out_field_string (uiout, "catch-type", "exec");
8951}
8952
8953static void
8954print_mention_catch_exec (struct breakpoint *b)
8955{
8956 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8957}
8958
8959/* Implement the "print_recreate" breakpoint_ops method for exec
8960 catchpoints. */
8961
8962static void
8963print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8964{
8965 fprintf_unfiltered (fp, "catch exec");
8966 print_recreate_thread (b, fp);
8967}
8968
8969static struct breakpoint_ops catch_exec_breakpoint_ops;
8970
8971static void
8972create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8973 const struct breakpoint_ops *ops)
8974{
8975 struct syscall_catchpoint *c;
8976 struct gdbarch *gdbarch = get_current_arch ();
8977
8978 c = XNEW (struct syscall_catchpoint);
8979 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8980 c->syscalls_to_be_caught = filter;
8981
8982 install_breakpoint (0, &c->base, 1);
8983}
8984
8985static int
8986hw_breakpoint_used_count (void)
8987{
8988 int i = 0;
8989 struct breakpoint *b;
8990 struct bp_location *bl;
8991
8992 ALL_BREAKPOINTS (b)
8993 {
8994 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8995 for (bl = b->loc; bl; bl = bl->next)
8996 {
8997 /* Special types of hardware breakpoints may use more than
8998 one register. */
8999 i += b->ops->resources_needed (bl);
9000 }
9001 }
9002
9003 return i;
9004}
9005
9006/* Returns the resources B would use if it were a hardware
9007 watchpoint. */
9008
9009static int
9010hw_watchpoint_use_count (struct breakpoint *b)
9011{
9012 int i = 0;
9013 struct bp_location *bl;
9014
9015 if (!breakpoint_enabled (b))
9016 return 0;
9017
9018 for (bl = b->loc; bl; bl = bl->next)
9019 {
9020 /* Special types of hardware watchpoints may use more than
9021 one register. */
9022 i += b->ops->resources_needed (bl);
9023 }
9024
9025 return i;
9026}
9027
9028/* Returns the sum the used resources of all hardware watchpoints of
9029 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9030 the sum of the used resources of all hardware watchpoints of other
9031 types _not_ TYPE. */
9032
9033static int
9034hw_watchpoint_used_count_others (struct breakpoint *except,
9035 enum bptype type, int *other_type_used)
9036{
9037 int i = 0;
9038 struct breakpoint *b;
9039
9040 *other_type_used = 0;
9041 ALL_BREAKPOINTS (b)
9042 {
9043 if (b == except)
9044 continue;
9045 if (!breakpoint_enabled (b))
9046 continue;
9047
9048 if (b->type == type)
9049 i += hw_watchpoint_use_count (b);
9050 else if (is_hardware_watchpoint (b))
9051 *other_type_used = 1;
9052 }
9053
9054 return i;
9055}
9056
9057void
9058disable_watchpoints_before_interactive_call_start (void)
9059{
9060 struct breakpoint *b;
9061
9062 ALL_BREAKPOINTS (b)
9063 {
9064 if (is_watchpoint (b) && breakpoint_enabled (b))
9065 {
9066 b->enable_state = bp_call_disabled;
9067 update_global_location_list (UGLL_DONT_INSERT);
9068 }
9069 }
9070}
9071
9072void
9073enable_watchpoints_after_interactive_call_stop (void)
9074{
9075 struct breakpoint *b;
9076
9077 ALL_BREAKPOINTS (b)
9078 {
9079 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
9080 {
9081 b->enable_state = bp_enabled;
9082 update_global_location_list (UGLL_MAY_INSERT);
9083 }
9084 }
9085}
9086
9087void
9088disable_breakpoints_before_startup (void)
9089{
9090 current_program_space->executing_startup = 1;
9091 update_global_location_list (UGLL_DONT_INSERT);
9092}
9093
9094void
9095enable_breakpoints_after_startup (void)
9096{
9097 current_program_space->executing_startup = 0;
9098 breakpoint_re_set ();
9099}
9100
9101/* Create a new single-step breakpoint for thread THREAD, with no
9102 locations. */
9103
9104static struct breakpoint *
9105new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9106{
9107 struct breakpoint *b = XNEW (struct breakpoint);
9108
9109 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9110 &momentary_breakpoint_ops);
9111
9112 b->disposition = disp_donttouch;
9113 b->frame_id = null_frame_id;
9114
9115 b->thread = thread;
9116 gdb_assert (b->thread != 0);
9117
9118 add_to_breakpoint_chain (b);
9119
9120 return b;
9121}
9122
9123/* Set a momentary breakpoint of type TYPE at address specified by
9124 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9125 frame. */
9126
9127struct breakpoint *
9128set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9129 struct frame_id frame_id, enum bptype type)
9130{
9131 struct breakpoint *b;
9132
9133 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9134 tail-called one. */
9135 gdb_assert (!frame_id_artificial_p (frame_id));
9136
9137 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
9138 b->enable_state = bp_enabled;
9139 b->disposition = disp_donttouch;
9140 b->frame_id = frame_id;
9141
9142 /* If we're debugging a multi-threaded program, then we want
9143 momentary breakpoints to be active in only a single thread of
9144 control. */
9145 if (in_thread_list (inferior_ptid))
9146 b->thread = pid_to_thread_id (inferior_ptid);
9147
9148 update_global_location_list_nothrow (UGLL_MAY_INSERT);
9149
9150 return b;
9151}
9152
9153/* Make a momentary breakpoint based on the master breakpoint ORIG.
9154 The new breakpoint will have type TYPE, use OPS as its
9155 breakpoint_ops, and will set enabled to LOC_ENABLED. */
9156
9157static struct breakpoint *
9158momentary_breakpoint_from_master (struct breakpoint *orig,
9159 enum bptype type,
9160 const struct breakpoint_ops *ops,
9161 int loc_enabled)
9162{
9163 struct breakpoint *copy;
9164
9165 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
9166 copy->loc = allocate_bp_location (copy);
9167 set_breakpoint_location_function (copy->loc, 1);
9168
9169 copy->loc->gdbarch = orig->loc->gdbarch;
9170 copy->loc->requested_address = orig->loc->requested_address;
9171 copy->loc->address = orig->loc->address;
9172 copy->loc->section = orig->loc->section;
9173 copy->loc->pspace = orig->loc->pspace;
9174 copy->loc->probe = orig->loc->probe;
9175 copy->loc->line_number = orig->loc->line_number;
9176 copy->loc->symtab = orig->loc->symtab;
9177 copy->loc->enabled = loc_enabled;
9178 copy->frame_id = orig->frame_id;
9179 copy->thread = orig->thread;
9180 copy->pspace = orig->pspace;
9181
9182 copy->enable_state = bp_enabled;
9183 copy->disposition = disp_donttouch;
9184 copy->number = internal_breakpoint_number--;
9185
9186 update_global_location_list_nothrow (UGLL_DONT_INSERT);
9187 return copy;
9188}
9189
9190/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9191 ORIG is NULL. */
9192
9193struct breakpoint *
9194clone_momentary_breakpoint (struct breakpoint *orig)
9195{
9196 /* If there's nothing to clone, then return nothing. */
9197 if (orig == NULL)
9198 return NULL;
9199
9200 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9201}
9202
9203struct breakpoint *
9204set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9205 enum bptype type)
9206{
9207 struct symtab_and_line sal;
9208
9209 sal = find_pc_line (pc, 0);
9210 sal.pc = pc;
9211 sal.section = find_pc_overlay (pc);
9212 sal.explicit_pc = 1;
9213
9214 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9215}
9216\f
9217
9218/* Tell the user we have just set a breakpoint B. */
9219
9220static void
9221mention (struct breakpoint *b)
9222{
9223 b->ops->print_mention (b);
9224 if (ui_out_is_mi_like_p (current_uiout))
9225 return;
9226 printf_filtered ("\n");
9227}
9228\f
9229
9230static struct bp_location *
9231add_location_to_breakpoint (struct breakpoint *b,
9232 const struct symtab_and_line *sal)
9233{
9234 struct bp_location *loc, **tmp;
9235 CORE_ADDR adjusted_address;
9236 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9237
9238 if (loc_gdbarch == NULL)
9239 loc_gdbarch = b->gdbarch;
9240
9241 /* Adjust the breakpoint's address prior to allocating a location.
9242 Once we call allocate_bp_location(), that mostly uninitialized
9243 location will be placed on the location chain. Adjustment of the
9244 breakpoint may cause target_read_memory() to be called and we do
9245 not want its scan of the location chain to find a breakpoint and
9246 location that's only been partially initialized. */
9247 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9248 sal->pc, b->type);
9249
9250 /* Sort the locations by their ADDRESS. */
9251 loc = allocate_bp_location (b);
9252 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9253 tmp = &((*tmp)->next))
9254 ;
9255 loc->next = *tmp;
9256 *tmp = loc;
9257
9258 loc->requested_address = sal->pc;
9259 loc->address = adjusted_address;
9260 loc->pspace = sal->pspace;
9261 loc->probe.probe = sal->probe;
9262 loc->probe.objfile = sal->objfile;
9263 gdb_assert (loc->pspace != NULL);
9264 loc->section = sal->section;
9265 loc->gdbarch = loc_gdbarch;
9266 loc->line_number = sal->line;
9267 loc->symtab = sal->symtab;
9268
9269 set_breakpoint_location_function (loc,
9270 sal->explicit_pc || sal->explicit_line);
9271 return loc;
9272}
9273\f
9274
9275/* Return 1 if LOC is pointing to a permanent breakpoint,
9276 return 0 otherwise. */
9277
9278static int
9279bp_loc_is_permanent (struct bp_location *loc)
9280{
9281 int len;
9282 CORE_ADDR addr;
9283 const gdb_byte *bpoint;
9284 gdb_byte *target_mem;
9285 struct cleanup *cleanup;
9286 int retval = 0;
9287
9288 gdb_assert (loc != NULL);
9289
9290 addr = loc->address;
9291 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
9292
9293 /* Software breakpoints unsupported? */
9294 if (bpoint == NULL)
9295 return 0;
9296
9297 target_mem = alloca (len);
9298
9299 /* Enable the automatic memory restoration from breakpoints while
9300 we read the memory. Otherwise we could say about our temporary
9301 breakpoints they are permanent. */
9302 cleanup = save_current_space_and_thread ();
9303
9304 switch_to_program_space_and_thread (loc->pspace);
9305 make_show_memory_breakpoints_cleanup (0);
9306
9307 if (target_read_memory (loc->address, target_mem, len) == 0
9308 && memcmp (target_mem, bpoint, len) == 0)
9309 retval = 1;
9310
9311 do_cleanups (cleanup);
9312
9313 return retval;
9314}
9315
9316/* Build a command list for the dprintf corresponding to the current
9317 settings of the dprintf style options. */
9318
9319static void
9320update_dprintf_command_list (struct breakpoint *b)
9321{
9322 char *dprintf_args = b->extra_string;
9323 char *printf_line = NULL;
9324
9325 if (!dprintf_args)
9326 return;
9327
9328 dprintf_args = skip_spaces (dprintf_args);
9329
9330 /* Allow a comma, as it may have terminated a location, but don't
9331 insist on it. */
9332 if (*dprintf_args == ',')
9333 ++dprintf_args;
9334 dprintf_args = skip_spaces (dprintf_args);
9335
9336 if (*dprintf_args != '"')
9337 error (_("Bad format string, missing '\"'."));
9338
9339 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9340 printf_line = xstrprintf ("printf %s", dprintf_args);
9341 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9342 {
9343 if (!dprintf_function)
9344 error (_("No function supplied for dprintf call"));
9345
9346 if (dprintf_channel && strlen (dprintf_channel) > 0)
9347 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9348 dprintf_function,
9349 dprintf_channel,
9350 dprintf_args);
9351 else
9352 printf_line = xstrprintf ("call (void) %s (%s)",
9353 dprintf_function,
9354 dprintf_args);
9355 }
9356 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9357 {
9358 if (target_can_run_breakpoint_commands ())
9359 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9360 else
9361 {
9362 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9363 printf_line = xstrprintf ("printf %s", dprintf_args);
9364 }
9365 }
9366 else
9367 internal_error (__FILE__, __LINE__,
9368 _("Invalid dprintf style."));
9369
9370 gdb_assert (printf_line != NULL);
9371 /* Manufacture a printf sequence. */
9372 {
9373 struct command_line *printf_cmd_line
9374 = xmalloc (sizeof (struct command_line));
9375
9376 printf_cmd_line = xmalloc (sizeof (struct command_line));
9377 printf_cmd_line->control_type = simple_control;
9378 printf_cmd_line->body_count = 0;
9379 printf_cmd_line->body_list = NULL;
9380 printf_cmd_line->next = NULL;
9381 printf_cmd_line->line = printf_line;
9382
9383 breakpoint_set_commands (b, printf_cmd_line);
9384 }
9385}
9386
9387/* Update all dprintf commands, making their command lists reflect
9388 current style settings. */
9389
9390static void
9391update_dprintf_commands (char *args, int from_tty,
9392 struct cmd_list_element *c)
9393{
9394 struct breakpoint *b;
9395
9396 ALL_BREAKPOINTS (b)
9397 {
9398 if (b->type == bp_dprintf)
9399 update_dprintf_command_list (b);
9400 }
9401}
9402
9403/* Create a breakpoint with SAL as location. Use ADDR_STRING
9404 as textual description of the location, and COND_STRING
9405 as condition expression. */
9406
9407static void
9408init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9409 struct symtabs_and_lines sals, char *addr_string,
9410 char *filter, char *cond_string,
9411 char *extra_string,
9412 enum bptype type, enum bpdisp disposition,
9413 int thread, int task, int ignore_count,
9414 const struct breakpoint_ops *ops, int from_tty,
9415 int enabled, int internal, unsigned flags,
9416 int display_canonical)
9417{
9418 int i;
9419
9420 if (type == bp_hardware_breakpoint)
9421 {
9422 int target_resources_ok;
9423
9424 i = hw_breakpoint_used_count ();
9425 target_resources_ok =
9426 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9427 i + 1, 0);
9428 if (target_resources_ok == 0)
9429 error (_("No hardware breakpoint support in the target."));
9430 else if (target_resources_ok < 0)
9431 error (_("Hardware breakpoints used exceeds limit."));
9432 }
9433
9434 gdb_assert (sals.nelts > 0);
9435
9436 for (i = 0; i < sals.nelts; ++i)
9437 {
9438 struct symtab_and_line sal = sals.sals[i];
9439 struct bp_location *loc;
9440
9441 if (from_tty)
9442 {
9443 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9444 if (!loc_gdbarch)
9445 loc_gdbarch = gdbarch;
9446
9447 describe_other_breakpoints (loc_gdbarch,
9448 sal.pspace, sal.pc, sal.section, thread);
9449 }
9450
9451 if (i == 0)
9452 {
9453 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9454 b->thread = thread;
9455 b->task = task;
9456
9457 b->cond_string = cond_string;
9458 b->extra_string = extra_string;
9459 b->ignore_count = ignore_count;
9460 b->enable_state = enabled ? bp_enabled : bp_disabled;
9461 b->disposition = disposition;
9462
9463 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9464 b->loc->inserted = 1;
9465
9466 if (type == bp_static_tracepoint)
9467 {
9468 struct tracepoint *t = (struct tracepoint *) b;
9469 struct static_tracepoint_marker marker;
9470
9471 if (strace_marker_p (b))
9472 {
9473 /* We already know the marker exists, otherwise, we
9474 wouldn't see a sal for it. */
9475 char *p = &addr_string[3];
9476 char *endp;
9477 char *marker_str;
9478
9479 p = skip_spaces (p);
9480
9481 endp = skip_to_space (p);
9482
9483 marker_str = savestring (p, endp - p);
9484 t->static_trace_marker_id = marker_str;
9485
9486 printf_filtered (_("Probed static tracepoint "
9487 "marker \"%s\"\n"),
9488 t->static_trace_marker_id);
9489 }
9490 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9491 {
9492 t->static_trace_marker_id = xstrdup (marker.str_id);
9493 release_static_tracepoint_marker (&marker);
9494
9495 printf_filtered (_("Probed static tracepoint "
9496 "marker \"%s\"\n"),
9497 t->static_trace_marker_id);
9498 }
9499 else
9500 warning (_("Couldn't determine the static "
9501 "tracepoint marker to probe"));
9502 }
9503
9504 loc = b->loc;
9505 }
9506 else
9507 {
9508 loc = add_location_to_breakpoint (b, &sal);
9509 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9510 loc->inserted = 1;
9511 }
9512
9513 if (bp_loc_is_permanent (loc))
9514 make_breakpoint_permanent (b);
9515
9516 if (b->cond_string)
9517 {
9518 const char *arg = b->cond_string;
9519
9520 loc->cond = parse_exp_1 (&arg, loc->address,
9521 block_for_pc (loc->address), 0);
9522 if (*arg)
9523 error (_("Garbage '%s' follows condition"), arg);
9524 }
9525
9526 /* Dynamic printf requires and uses additional arguments on the
9527 command line, otherwise it's an error. */
9528 if (type == bp_dprintf)
9529 {
9530 if (b->extra_string)
9531 update_dprintf_command_list (b);
9532 else
9533 error (_("Format string required"));
9534 }
9535 else if (b->extra_string)
9536 error (_("Garbage '%s' at end of command"), b->extra_string);
9537 }
9538
9539 b->display_canonical = display_canonical;
9540 if (addr_string)
9541 b->addr_string = addr_string;
9542 else
9543 /* addr_string has to be used or breakpoint_re_set will delete
9544 me. */
9545 b->addr_string
9546 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9547 b->filter = filter;
9548}
9549
9550static void
9551create_breakpoint_sal (struct gdbarch *gdbarch,
9552 struct symtabs_and_lines sals, char *addr_string,
9553 char *filter, char *cond_string,
9554 char *extra_string,
9555 enum bptype type, enum bpdisp disposition,
9556 int thread, int task, int ignore_count,
9557 const struct breakpoint_ops *ops, int from_tty,
9558 int enabled, int internal, unsigned flags,
9559 int display_canonical)
9560{
9561 struct breakpoint *b;
9562 struct cleanup *old_chain;
9563
9564 if (is_tracepoint_type (type))
9565 {
9566 struct tracepoint *t;
9567
9568 t = XCNEW (struct tracepoint);
9569 b = &t->base;
9570 }
9571 else
9572 b = XNEW (struct breakpoint);
9573
9574 old_chain = make_cleanup (xfree, b);
9575
9576 init_breakpoint_sal (b, gdbarch,
9577 sals, addr_string,
9578 filter, cond_string, extra_string,
9579 type, disposition,
9580 thread, task, ignore_count,
9581 ops, from_tty,
9582 enabled, internal, flags,
9583 display_canonical);
9584 discard_cleanups (old_chain);
9585
9586 install_breakpoint (internal, b, 0);
9587}
9588
9589/* Add SALS.nelts breakpoints to the breakpoint table. For each
9590 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9591 value. COND_STRING, if not NULL, specified the condition to be
9592 used for all breakpoints. Essentially the only case where
9593 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9594 function. In that case, it's still not possible to specify
9595 separate conditions for different overloaded functions, so
9596 we take just a single condition string.
9597
9598 NOTE: If the function succeeds, the caller is expected to cleanup
9599 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9600 array contents). If the function fails (error() is called), the
9601 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9602 COND and SALS arrays and each of those arrays contents. */
9603
9604static void
9605create_breakpoints_sal (struct gdbarch *gdbarch,
9606 struct linespec_result *canonical,
9607 char *cond_string, char *extra_string,
9608 enum bptype type, enum bpdisp disposition,
9609 int thread, int task, int ignore_count,
9610 const struct breakpoint_ops *ops, int from_tty,
9611 int enabled, int internal, unsigned flags)
9612{
9613 int i;
9614 struct linespec_sals *lsal;
9615
9616 if (canonical->pre_expanded)
9617 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9618
9619 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9620 {
9621 /* Note that 'addr_string' can be NULL in the case of a plain
9622 'break', without arguments. */
9623 char *addr_string = (canonical->addr_string
9624 ? xstrdup (canonical->addr_string)
9625 : NULL);
9626 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9627 struct cleanup *inner = make_cleanup (xfree, addr_string);
9628
9629 make_cleanup (xfree, filter_string);
9630 create_breakpoint_sal (gdbarch, lsal->sals,
9631 addr_string,
9632 filter_string,
9633 cond_string, extra_string,
9634 type, disposition,
9635 thread, task, ignore_count, ops,
9636 from_tty, enabled, internal, flags,
9637 canonical->special_display);
9638 discard_cleanups (inner);
9639 }
9640}
9641
9642/* Parse ADDRESS which is assumed to be a SAL specification possibly
9643 followed by conditionals. On return, SALS contains an array of SAL
9644 addresses found. ADDR_STRING contains a vector of (canonical)
9645 address strings. ADDRESS points to the end of the SAL.
9646
9647 The array and the line spec strings are allocated on the heap, it is
9648 the caller's responsibility to free them. */
9649
9650static void
9651parse_breakpoint_sals (char **address,
9652 struct linespec_result *canonical)
9653{
9654 /* If no arg given, or if first arg is 'if ', use the default
9655 breakpoint. */
9656 if ((*address) == NULL
9657 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
9658 {
9659 /* The last displayed codepoint, if it's valid, is our default breakpoint
9660 address. */
9661 if (last_displayed_sal_is_valid ())
9662 {
9663 struct linespec_sals lsal;
9664 struct symtab_and_line sal;
9665 CORE_ADDR pc;
9666
9667 init_sal (&sal); /* Initialize to zeroes. */
9668 lsal.sals.sals = (struct symtab_and_line *)
9669 xmalloc (sizeof (struct symtab_and_line));
9670
9671 /* Set sal's pspace, pc, symtab, and line to the values
9672 corresponding to the last call to print_frame_info.
9673 Be sure to reinitialize LINE with NOTCURRENT == 0
9674 as the breakpoint line number is inappropriate otherwise.
9675 find_pc_line would adjust PC, re-set it back. */
9676 get_last_displayed_sal (&sal);
9677 pc = sal.pc;
9678 sal = find_pc_line (pc, 0);
9679
9680 /* "break" without arguments is equivalent to "break *PC"
9681 where PC is the last displayed codepoint's address. So
9682 make sure to set sal.explicit_pc to prevent GDB from
9683 trying to expand the list of sals to include all other
9684 instances with the same symtab and line. */
9685 sal.pc = pc;
9686 sal.explicit_pc = 1;
9687
9688 lsal.sals.sals[0] = sal;
9689 lsal.sals.nelts = 1;
9690 lsal.canonical = NULL;
9691
9692 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9693 }
9694 else
9695 error (_("No default breakpoint address now."));
9696 }
9697 else
9698 {
9699 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9700
9701 /* Force almost all breakpoints to be in terms of the
9702 current_source_symtab (which is decode_line_1's default).
9703 This should produce the results we want almost all of the
9704 time while leaving default_breakpoint_* alone.
9705
9706 ObjC: However, don't match an Objective-C method name which
9707 may have a '+' or '-' succeeded by a '['. */
9708 if (last_displayed_sal_is_valid ()
9709 && (!cursal.symtab
9710 || ((strchr ("+-", (*address)[0]) != NULL)
9711 && ((*address)[1] != '['))))
9712 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9713 get_last_displayed_symtab (),
9714 get_last_displayed_line (),
9715 canonical, NULL, NULL);
9716 else
9717 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9718 cursal.symtab, cursal.line, canonical, NULL, NULL);
9719 }
9720}
9721
9722
9723/* Convert each SAL into a real PC. Verify that the PC can be
9724 inserted as a breakpoint. If it can't throw an error. */
9725
9726static void
9727breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9728{
9729 int i;
9730
9731 for (i = 0; i < sals->nelts; i++)
9732 resolve_sal_pc (&sals->sals[i]);
9733}
9734
9735/* Fast tracepoints may have restrictions on valid locations. For
9736 instance, a fast tracepoint using a jump instead of a trap will
9737 likely have to overwrite more bytes than a trap would, and so can
9738 only be placed where the instruction is longer than the jump, or a
9739 multi-instruction sequence does not have a jump into the middle of
9740 it, etc. */
9741
9742static void
9743check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9744 struct symtabs_and_lines *sals)
9745{
9746 int i, rslt;
9747 struct symtab_and_line *sal;
9748 char *msg;
9749 struct cleanup *old_chain;
9750
9751 for (i = 0; i < sals->nelts; i++)
9752 {
9753 struct gdbarch *sarch;
9754
9755 sal = &sals->sals[i];
9756
9757 sarch = get_sal_arch (*sal);
9758 /* We fall back to GDBARCH if there is no architecture
9759 associated with SAL. */
9760 if (sarch == NULL)
9761 sarch = gdbarch;
9762 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9763 NULL, &msg);
9764 old_chain = make_cleanup (xfree, msg);
9765
9766 if (!rslt)
9767 error (_("May not have a fast tracepoint at 0x%s%s"),
9768 paddress (sarch, sal->pc), (msg ? msg : ""));
9769
9770 do_cleanups (old_chain);
9771 }
9772}
9773
9774/* Issue an invalid thread ID error. */
9775
9776static void ATTRIBUTE_NORETURN
9777invalid_thread_id_error (int id)
9778{
9779 error (_("Unknown thread %d."), id);
9780}
9781
9782/* Given TOK, a string specification of condition and thread, as
9783 accepted by the 'break' command, extract the condition
9784 string and thread number and set *COND_STRING and *THREAD.
9785 PC identifies the context at which the condition should be parsed.
9786 If no condition is found, *COND_STRING is set to NULL.
9787 If no thread is found, *THREAD is set to -1. */
9788
9789static void
9790find_condition_and_thread (const char *tok, CORE_ADDR pc,
9791 char **cond_string, int *thread, int *task,
9792 char **rest)
9793{
9794 *cond_string = NULL;
9795 *thread = -1;
9796 *task = 0;
9797 *rest = NULL;
9798
9799 while (tok && *tok)
9800 {
9801 const char *end_tok;
9802 int toklen;
9803 const char *cond_start = NULL;
9804 const char *cond_end = NULL;
9805
9806 tok = skip_spaces_const (tok);
9807
9808 if ((*tok == '"' || *tok == ',') && rest)
9809 {
9810 *rest = savestring (tok, strlen (tok));
9811 return;
9812 }
9813
9814 end_tok = skip_to_space_const (tok);
9815
9816 toklen = end_tok - tok;
9817
9818 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9819 {
9820 struct expression *expr;
9821
9822 tok = cond_start = end_tok + 1;
9823 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9824 xfree (expr);
9825 cond_end = tok;
9826 *cond_string = savestring (cond_start, cond_end - cond_start);
9827 }
9828 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9829 {
9830 char *tmptok;
9831
9832 tok = end_tok + 1;
9833 *thread = strtol (tok, &tmptok, 0);
9834 if (tok == tmptok)
9835 error (_("Junk after thread keyword."));
9836 if (!valid_thread_id (*thread))
9837 invalid_thread_id_error (*thread);
9838 tok = tmptok;
9839 }
9840 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9841 {
9842 char *tmptok;
9843
9844 tok = end_tok + 1;
9845 *task = strtol (tok, &tmptok, 0);
9846 if (tok == tmptok)
9847 error (_("Junk after task keyword."));
9848 if (!valid_task_id (*task))
9849 error (_("Unknown task %d."), *task);
9850 tok = tmptok;
9851 }
9852 else if (rest)
9853 {
9854 *rest = savestring (tok, strlen (tok));
9855 return;
9856 }
9857 else
9858 error (_("Junk at end of arguments."));
9859 }
9860}
9861
9862/* Decode a static tracepoint marker spec. */
9863
9864static struct symtabs_and_lines
9865decode_static_tracepoint_spec (char **arg_p)
9866{
9867 VEC(static_tracepoint_marker_p) *markers = NULL;
9868 struct symtabs_and_lines sals;
9869 struct cleanup *old_chain;
9870 char *p = &(*arg_p)[3];
9871 char *endp;
9872 char *marker_str;
9873 int i;
9874
9875 p = skip_spaces (p);
9876
9877 endp = skip_to_space (p);
9878
9879 marker_str = savestring (p, endp - p);
9880 old_chain = make_cleanup (xfree, marker_str);
9881
9882 markers = target_static_tracepoint_markers_by_strid (marker_str);
9883 if (VEC_empty(static_tracepoint_marker_p, markers))
9884 error (_("No known static tracepoint marker named %s"), marker_str);
9885
9886 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9887 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9888
9889 for (i = 0; i < sals.nelts; i++)
9890 {
9891 struct static_tracepoint_marker *marker;
9892
9893 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9894
9895 init_sal (&sals.sals[i]);
9896
9897 sals.sals[i] = find_pc_line (marker->address, 0);
9898 sals.sals[i].pc = marker->address;
9899
9900 release_static_tracepoint_marker (marker);
9901 }
9902
9903 do_cleanups (old_chain);
9904
9905 *arg_p = endp;
9906 return sals;
9907}
9908
9909/* Set a breakpoint. This function is shared between CLI and MI
9910 functions for setting a breakpoint. This function has two major
9911 modes of operations, selected by the PARSE_ARG parameter. If
9912 non-zero, the function will parse ARG, extracting location,
9913 condition, thread and extra string. Otherwise, ARG is just the
9914 breakpoint's location, with condition, thread, and extra string
9915 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9916 If INTERNAL is non-zero, the breakpoint number will be allocated
9917 from the internal breakpoint count. Returns true if any breakpoint
9918 was created; false otherwise. */
9919
9920int
9921create_breakpoint (struct gdbarch *gdbarch,
9922 char *arg, char *cond_string,
9923 int thread, char *extra_string,
9924 int parse_arg,
9925 int tempflag, enum bptype type_wanted,
9926 int ignore_count,
9927 enum auto_boolean pending_break_support,
9928 const struct breakpoint_ops *ops,
9929 int from_tty, int enabled, int internal,
9930 unsigned flags)
9931{
9932 volatile struct gdb_exception e;
9933 char *copy_arg = NULL;
9934 char *addr_start = arg;
9935 struct linespec_result canonical;
9936 struct cleanup *old_chain;
9937 struct cleanup *bkpt_chain = NULL;
9938 int pending = 0;
9939 int task = 0;
9940 int prev_bkpt_count = breakpoint_count;
9941
9942 gdb_assert (ops != NULL);
9943
9944 init_linespec_result (&canonical);
9945
9946 TRY_CATCH (e, RETURN_MASK_ALL)
9947 {
9948 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9949 addr_start, &copy_arg);
9950 }
9951
9952 /* If caller is interested in rc value from parse, set value. */
9953 switch (e.reason)
9954 {
9955 case GDB_NO_ERROR:
9956 if (VEC_empty (linespec_sals, canonical.sals))
9957 return 0;
9958 break;
9959 case RETURN_ERROR:
9960 switch (e.error)
9961 {
9962 case NOT_FOUND_ERROR:
9963
9964 /* If pending breakpoint support is turned off, throw
9965 error. */
9966
9967 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9968 throw_exception (e);
9969
9970 exception_print (gdb_stderr, e);
9971
9972 /* If pending breakpoint support is auto query and the user
9973 selects no, then simply return the error code. */
9974 if (pending_break_support == AUTO_BOOLEAN_AUTO
9975 && !nquery (_("Make %s pending on future shared library load? "),
9976 bptype_string (type_wanted)))
9977 return 0;
9978
9979 /* At this point, either the user was queried about setting
9980 a pending breakpoint and selected yes, or pending
9981 breakpoint behavior is on and thus a pending breakpoint
9982 is defaulted on behalf of the user. */
9983 {
9984 struct linespec_sals lsal;
9985
9986 copy_arg = xstrdup (addr_start);
9987 lsal.canonical = xstrdup (copy_arg);
9988 lsal.sals.nelts = 1;
9989 lsal.sals.sals = XNEW (struct symtab_and_line);
9990 init_sal (&lsal.sals.sals[0]);
9991 pending = 1;
9992 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9993 }
9994 break;
9995 default:
9996 throw_exception (e);
9997 }
9998 break;
9999 default:
10000 throw_exception (e);
10001 }
10002
10003 /* Create a chain of things that always need to be cleaned up. */
10004 old_chain = make_cleanup_destroy_linespec_result (&canonical);
10005
10006 /* ----------------------------- SNIP -----------------------------
10007 Anything added to the cleanup chain beyond this point is assumed
10008 to be part of a breakpoint. If the breakpoint create succeeds
10009 then the memory is not reclaimed. */
10010 bkpt_chain = make_cleanup (null_cleanup, 0);
10011
10012 /* Resolve all line numbers to PC's and verify that the addresses
10013 are ok for the target. */
10014 if (!pending)
10015 {
10016 int ix;
10017 struct linespec_sals *iter;
10018
10019 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10020 breakpoint_sals_to_pc (&iter->sals);
10021 }
10022
10023 /* Fast tracepoints may have additional restrictions on location. */
10024 if (!pending && type_wanted == bp_fast_tracepoint)
10025 {
10026 int ix;
10027 struct linespec_sals *iter;
10028
10029 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10030 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10031 }
10032
10033 /* Verify that condition can be parsed, before setting any
10034 breakpoints. Allocate a separate condition expression for each
10035 breakpoint. */
10036 if (!pending)
10037 {
10038 if (parse_arg)
10039 {
10040 char *rest;
10041 struct linespec_sals *lsal;
10042
10043 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10044
10045 /* Here we only parse 'arg' to separate condition
10046 from thread number, so parsing in context of first
10047 sal is OK. When setting the breakpoint we'll
10048 re-parse it in context of each sal. */
10049
10050 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10051 &thread, &task, &rest);
10052 if (cond_string)
10053 make_cleanup (xfree, cond_string);
10054 if (rest)
10055 make_cleanup (xfree, rest);
10056 if (rest)
10057 extra_string = rest;
10058 }
10059 else
10060 {
10061 if (*arg != '\0')
10062 error (_("Garbage '%s' at end of location"), arg);
10063
10064 /* Create a private copy of condition string. */
10065 if (cond_string)
10066 {
10067 cond_string = xstrdup (cond_string);
10068 make_cleanup (xfree, cond_string);
10069 }
10070 /* Create a private copy of any extra string. */
10071 if (extra_string)
10072 {
10073 extra_string = xstrdup (extra_string);
10074 make_cleanup (xfree, extra_string);
10075 }
10076 }
10077
10078 ops->create_breakpoints_sal (gdbarch, &canonical,
10079 cond_string, extra_string, type_wanted,
10080 tempflag ? disp_del : disp_donttouch,
10081 thread, task, ignore_count, ops,
10082 from_tty, enabled, internal, flags);
10083 }
10084 else
10085 {
10086 struct breakpoint *b;
10087
10088 make_cleanup (xfree, copy_arg);
10089
10090 if (is_tracepoint_type (type_wanted))
10091 {
10092 struct tracepoint *t;
10093
10094 t = XCNEW (struct tracepoint);
10095 b = &t->base;
10096 }
10097 else
10098 b = XNEW (struct breakpoint);
10099
10100 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10101
10102 b->addr_string = copy_arg;
10103 if (parse_arg)
10104 b->cond_string = NULL;
10105 else
10106 {
10107 /* Create a private copy of condition string. */
10108 if (cond_string)
10109 {
10110 cond_string = xstrdup (cond_string);
10111 make_cleanup (xfree, cond_string);
10112 }
10113 b->cond_string = cond_string;
10114 }
10115 b->extra_string = NULL;
10116 b->ignore_count = ignore_count;
10117 b->disposition = tempflag ? disp_del : disp_donttouch;
10118 b->condition_not_parsed = 1;
10119 b->enable_state = enabled ? bp_enabled : bp_disabled;
10120 if ((type_wanted != bp_breakpoint
10121 && type_wanted != bp_hardware_breakpoint) || thread != -1)
10122 b->pspace = current_program_space;
10123
10124 install_breakpoint (internal, b, 0);
10125 }
10126
10127 if (VEC_length (linespec_sals, canonical.sals) > 1)
10128 {
10129 warning (_("Multiple breakpoints were set.\nUse the "
10130 "\"delete\" command to delete unwanted breakpoints."));
10131 prev_breakpoint_count = prev_bkpt_count;
10132 }
10133
10134 /* That's it. Discard the cleanups for data inserted into the
10135 breakpoint. */
10136 discard_cleanups (bkpt_chain);
10137 /* But cleanup everything else. */
10138 do_cleanups (old_chain);
10139
10140 /* error call may happen here - have BKPT_CHAIN already discarded. */
10141 update_global_location_list (UGLL_MAY_INSERT);
10142
10143 return 1;
10144}
10145
10146/* Set a breakpoint.
10147 ARG is a string describing breakpoint address,
10148 condition, and thread.
10149 FLAG specifies if a breakpoint is hardware on,
10150 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10151 and BP_TEMPFLAG. */
10152
10153static void
10154break_command_1 (char *arg, int flag, int from_tty)
10155{
10156 int tempflag = flag & BP_TEMPFLAG;
10157 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10158 ? bp_hardware_breakpoint
10159 : bp_breakpoint);
10160 struct breakpoint_ops *ops;
10161 const char *arg_cp = arg;
10162
10163 /* Matching breakpoints on probes. */
10164 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10165 ops = &bkpt_probe_breakpoint_ops;
10166 else
10167 ops = &bkpt_breakpoint_ops;
10168
10169 create_breakpoint (get_current_arch (),
10170 arg,
10171 NULL, 0, NULL, 1 /* parse arg */,
10172 tempflag, type_wanted,
10173 0 /* Ignore count */,
10174 pending_break_support,
10175 ops,
10176 from_tty,
10177 1 /* enabled */,
10178 0 /* internal */,
10179 0);
10180}
10181
10182/* Helper function for break_command_1 and disassemble_command. */
10183
10184void
10185resolve_sal_pc (struct symtab_and_line *sal)
10186{
10187 CORE_ADDR pc;
10188
10189 if (sal->pc == 0 && sal->symtab != NULL)
10190 {
10191 if (!find_line_pc (sal->symtab, sal->line, &pc))
10192 error (_("No line %d in file \"%s\"."),
10193 sal->line, symtab_to_filename_for_display (sal->symtab));
10194 sal->pc = pc;
10195
10196 /* If this SAL corresponds to a breakpoint inserted using a line
10197 number, then skip the function prologue if necessary. */
10198 if (sal->explicit_line)
10199 skip_prologue_sal (sal);
10200 }
10201
10202 if (sal->section == 0 && sal->symtab != NULL)
10203 {
10204 const struct blockvector *bv;
10205 const struct block *b;
10206 struct symbol *sym;
10207
10208 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
10209 if (bv != NULL)
10210 {
10211 sym = block_linkage_function (b);
10212 if (sym != NULL)
10213 {
10214 fixup_symbol_section (sym, sal->symtab->objfile);
10215 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
10216 }
10217 else
10218 {
10219 /* It really is worthwhile to have the section, so we'll
10220 just have to look harder. This case can be executed
10221 if we have line numbers but no functions (as can
10222 happen in assembly source). */
10223
10224 struct bound_minimal_symbol msym;
10225 struct cleanup *old_chain = save_current_space_and_thread ();
10226
10227 switch_to_program_space_and_thread (sal->pspace);
10228
10229 msym = lookup_minimal_symbol_by_pc (sal->pc);
10230 if (msym.minsym)
10231 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10232
10233 do_cleanups (old_chain);
10234 }
10235 }
10236 }
10237}
10238
10239void
10240break_command (char *arg, int from_tty)
10241{
10242 break_command_1 (arg, 0, from_tty);
10243}
10244
10245void
10246tbreak_command (char *arg, int from_tty)
10247{
10248 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10249}
10250
10251static void
10252hbreak_command (char *arg, int from_tty)
10253{
10254 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10255}
10256
10257static void
10258thbreak_command (char *arg, int from_tty)
10259{
10260 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10261}
10262
10263static void
10264stop_command (char *arg, int from_tty)
10265{
10266 printf_filtered (_("Specify the type of breakpoint to set.\n\
10267Usage: stop in <function | address>\n\
10268 stop at <line>\n"));
10269}
10270
10271static void
10272stopin_command (char *arg, int from_tty)
10273{
10274 int badInput = 0;
10275
10276 if (arg == (char *) NULL)
10277 badInput = 1;
10278 else if (*arg != '*')
10279 {
10280 char *argptr = arg;
10281 int hasColon = 0;
10282
10283 /* Look for a ':'. If this is a line number specification, then
10284 say it is bad, otherwise, it should be an address or
10285 function/method name. */
10286 while (*argptr && !hasColon)
10287 {
10288 hasColon = (*argptr == ':');
10289 argptr++;
10290 }
10291
10292 if (hasColon)
10293 badInput = (*argptr != ':'); /* Not a class::method */
10294 else
10295 badInput = isdigit (*arg); /* a simple line number */
10296 }
10297
10298 if (badInput)
10299 printf_filtered (_("Usage: stop in <function | address>\n"));
10300 else
10301 break_command_1 (arg, 0, from_tty);
10302}
10303
10304static void
10305stopat_command (char *arg, int from_tty)
10306{
10307 int badInput = 0;
10308
10309 if (arg == (char *) NULL || *arg == '*') /* no line number */
10310 badInput = 1;
10311 else
10312 {
10313 char *argptr = arg;
10314 int hasColon = 0;
10315
10316 /* Look for a ':'. If there is a '::' then get out, otherwise
10317 it is probably a line number. */
10318 while (*argptr && !hasColon)
10319 {
10320 hasColon = (*argptr == ':');
10321 argptr++;
10322 }
10323
10324 if (hasColon)
10325 badInput = (*argptr == ':'); /* we have class::method */
10326 else
10327 badInput = !isdigit (*arg); /* not a line number */
10328 }
10329
10330 if (badInput)
10331 printf_filtered (_("Usage: stop at <line>\n"));
10332 else
10333 break_command_1 (arg, 0, from_tty);
10334}
10335
10336/* The dynamic printf command is mostly like a regular breakpoint, but
10337 with a prewired command list consisting of a single output command,
10338 built from extra arguments supplied on the dprintf command
10339 line. */
10340
10341static void
10342dprintf_command (char *arg, int from_tty)
10343{
10344 create_breakpoint (get_current_arch (),
10345 arg,
10346 NULL, 0, NULL, 1 /* parse arg */,
10347 0, bp_dprintf,
10348 0 /* Ignore count */,
10349 pending_break_support,
10350 &dprintf_breakpoint_ops,
10351 from_tty,
10352 1 /* enabled */,
10353 0 /* internal */,
10354 0);
10355}
10356
10357static void
10358agent_printf_command (char *arg, int from_tty)
10359{
10360 error (_("May only run agent-printf on the target"));
10361}
10362
10363/* Implement the "breakpoint_hit" breakpoint_ops method for
10364 ranged breakpoints. */
10365
10366static int
10367breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10368 struct address_space *aspace,
10369 CORE_ADDR bp_addr,
10370 const struct target_waitstatus *ws)
10371{
10372 if (ws->kind != TARGET_WAITKIND_STOPPED
10373 || ws->value.sig != GDB_SIGNAL_TRAP)
10374 return 0;
10375
10376 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10377 bl->length, aspace, bp_addr);
10378}
10379
10380/* Implement the "resources_needed" breakpoint_ops method for
10381 ranged breakpoints. */
10382
10383static int
10384resources_needed_ranged_breakpoint (const struct bp_location *bl)
10385{
10386 return target_ranged_break_num_registers ();
10387}
10388
10389/* Implement the "print_it" breakpoint_ops method for
10390 ranged breakpoints. */
10391
10392static enum print_stop_action
10393print_it_ranged_breakpoint (bpstat bs)
10394{
10395 struct breakpoint *b = bs->breakpoint_at;
10396 struct bp_location *bl = b->loc;
10397 struct ui_out *uiout = current_uiout;
10398
10399 gdb_assert (b->type == bp_hardware_breakpoint);
10400
10401 /* Ranged breakpoints have only one location. */
10402 gdb_assert (bl && bl->next == NULL);
10403
10404 annotate_breakpoint (b->number);
10405 if (b->disposition == disp_del)
10406 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10407 else
10408 ui_out_text (uiout, "\nRanged breakpoint ");
10409 if (ui_out_is_mi_like_p (uiout))
10410 {
10411 ui_out_field_string (uiout, "reason",
10412 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10413 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10414 }
10415 ui_out_field_int (uiout, "bkptno", b->number);
10416 ui_out_text (uiout, ", ");
10417
10418 return PRINT_SRC_AND_LOC;
10419}
10420
10421/* Implement the "print_one" breakpoint_ops method for
10422 ranged breakpoints. */
10423
10424static void
10425print_one_ranged_breakpoint (struct breakpoint *b,
10426 struct bp_location **last_loc)
10427{
10428 struct bp_location *bl = b->loc;
10429 struct value_print_options opts;
10430 struct ui_out *uiout = current_uiout;
10431
10432 /* Ranged breakpoints have only one location. */
10433 gdb_assert (bl && bl->next == NULL);
10434
10435 get_user_print_options (&opts);
10436
10437 if (opts.addressprint)
10438 /* We don't print the address range here, it will be printed later
10439 by print_one_detail_ranged_breakpoint. */
10440 ui_out_field_skip (uiout, "addr");
10441 annotate_field (5);
10442 print_breakpoint_location (b, bl);
10443 *last_loc = bl;
10444}
10445
10446/* Implement the "print_one_detail" breakpoint_ops method for
10447 ranged breakpoints. */
10448
10449static void
10450print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10451 struct ui_out *uiout)
10452{
10453 CORE_ADDR address_start, address_end;
10454 struct bp_location *bl = b->loc;
10455 struct ui_file *stb = mem_fileopen ();
10456 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10457
10458 gdb_assert (bl);
10459
10460 address_start = bl->address;
10461 address_end = address_start + bl->length - 1;
10462
10463 ui_out_text (uiout, "\taddress range: ");
10464 fprintf_unfiltered (stb, "[%s, %s]",
10465 print_core_address (bl->gdbarch, address_start),
10466 print_core_address (bl->gdbarch, address_end));
10467 ui_out_field_stream (uiout, "addr", stb);
10468 ui_out_text (uiout, "\n");
10469
10470 do_cleanups (cleanup);
10471}
10472
10473/* Implement the "print_mention" breakpoint_ops method for
10474 ranged breakpoints. */
10475
10476static void
10477print_mention_ranged_breakpoint (struct breakpoint *b)
10478{
10479 struct bp_location *bl = b->loc;
10480 struct ui_out *uiout = current_uiout;
10481
10482 gdb_assert (bl);
10483 gdb_assert (b->type == bp_hardware_breakpoint);
10484
10485 if (ui_out_is_mi_like_p (uiout))
10486 return;
10487
10488 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10489 b->number, paddress (bl->gdbarch, bl->address),
10490 paddress (bl->gdbarch, bl->address + bl->length - 1));
10491}
10492
10493/* Implement the "print_recreate" breakpoint_ops method for
10494 ranged breakpoints. */
10495
10496static void
10497print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10498{
10499 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10500 b->addr_string_range_end);
10501 print_recreate_thread (b, fp);
10502}
10503
10504/* The breakpoint_ops structure to be used in ranged breakpoints. */
10505
10506static struct breakpoint_ops ranged_breakpoint_ops;
10507
10508/* Find the address where the end of the breakpoint range should be
10509 placed, given the SAL of the end of the range. This is so that if
10510 the user provides a line number, the end of the range is set to the
10511 last instruction of the given line. */
10512
10513static CORE_ADDR
10514find_breakpoint_range_end (struct symtab_and_line sal)
10515{
10516 CORE_ADDR end;
10517
10518 /* If the user provided a PC value, use it. Otherwise,
10519 find the address of the end of the given location. */
10520 if (sal.explicit_pc)
10521 end = sal.pc;
10522 else
10523 {
10524 int ret;
10525 CORE_ADDR start;
10526
10527 ret = find_line_pc_range (sal, &start, &end);
10528 if (!ret)
10529 error (_("Could not find location of the end of the range."));
10530
10531 /* find_line_pc_range returns the start of the next line. */
10532 end--;
10533 }
10534
10535 return end;
10536}
10537
10538/* Implement the "break-range" CLI command. */
10539
10540static void
10541break_range_command (char *arg, int from_tty)
10542{
10543 char *arg_start, *addr_string_start, *addr_string_end;
10544 struct linespec_result canonical_start, canonical_end;
10545 int bp_count, can_use_bp, length;
10546 CORE_ADDR end;
10547 struct breakpoint *b;
10548 struct symtab_and_line sal_start, sal_end;
10549 struct cleanup *cleanup_bkpt;
10550 struct linespec_sals *lsal_start, *lsal_end;
10551
10552 /* We don't support software ranged breakpoints. */
10553 if (target_ranged_break_num_registers () < 0)
10554 error (_("This target does not support hardware ranged breakpoints."));
10555
10556 bp_count = hw_breakpoint_used_count ();
10557 bp_count += target_ranged_break_num_registers ();
10558 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10559 bp_count, 0);
10560 if (can_use_bp < 0)
10561 error (_("Hardware breakpoints used exceeds limit."));
10562
10563 arg = skip_spaces (arg);
10564 if (arg == NULL || arg[0] == '\0')
10565 error(_("No address range specified."));
10566
10567 init_linespec_result (&canonical_start);
10568
10569 arg_start = arg;
10570 parse_breakpoint_sals (&arg, &canonical_start);
10571
10572 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10573
10574 if (arg[0] != ',')
10575 error (_("Too few arguments."));
10576 else if (VEC_empty (linespec_sals, canonical_start.sals))
10577 error (_("Could not find location of the beginning of the range."));
10578
10579 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10580
10581 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10582 || lsal_start->sals.nelts != 1)
10583 error (_("Cannot create a ranged breakpoint with multiple locations."));
10584
10585 sal_start = lsal_start->sals.sals[0];
10586 addr_string_start = savestring (arg_start, arg - arg_start);
10587 make_cleanup (xfree, addr_string_start);
10588
10589 arg++; /* Skip the comma. */
10590 arg = skip_spaces (arg);
10591
10592 /* Parse the end location. */
10593
10594 init_linespec_result (&canonical_end);
10595 arg_start = arg;
10596
10597 /* We call decode_line_full directly here instead of using
10598 parse_breakpoint_sals because we need to specify the start location's
10599 symtab and line as the default symtab and line for the end of the
10600 range. This makes it possible to have ranges like "foo.c:27, +14",
10601 where +14 means 14 lines from the start location. */
10602 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10603 sal_start.symtab, sal_start.line,
10604 &canonical_end, NULL, NULL);
10605
10606 make_cleanup_destroy_linespec_result (&canonical_end);
10607
10608 if (VEC_empty (linespec_sals, canonical_end.sals))
10609 error (_("Could not find location of the end of the range."));
10610
10611 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10612 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10613 || lsal_end->sals.nelts != 1)
10614 error (_("Cannot create a ranged breakpoint with multiple locations."));
10615
10616 sal_end = lsal_end->sals.sals[0];
10617 addr_string_end = savestring (arg_start, arg - arg_start);
10618 make_cleanup (xfree, addr_string_end);
10619
10620 end = find_breakpoint_range_end (sal_end);
10621 if (sal_start.pc > end)
10622 error (_("Invalid address range, end precedes start."));
10623
10624 length = end - sal_start.pc + 1;
10625 if (length < 0)
10626 /* Length overflowed. */
10627 error (_("Address range too large."));
10628 else if (length == 1)
10629 {
10630 /* This range is simple enough to be handled by
10631 the `hbreak' command. */
10632 hbreak_command (addr_string_start, 1);
10633
10634 do_cleanups (cleanup_bkpt);
10635
10636 return;
10637 }
10638
10639 /* Now set up the breakpoint. */
10640 b = set_raw_breakpoint (get_current_arch (), sal_start,
10641 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10642 set_breakpoint_count (breakpoint_count + 1);
10643 b->number = breakpoint_count;
10644 b->disposition = disp_donttouch;
10645 b->addr_string = xstrdup (addr_string_start);
10646 b->addr_string_range_end = xstrdup (addr_string_end);
10647 b->loc->length = length;
10648
10649 do_cleanups (cleanup_bkpt);
10650
10651 mention (b);
10652 observer_notify_breakpoint_created (b);
10653 update_global_location_list (UGLL_MAY_INSERT);
10654}
10655
10656/* Return non-zero if EXP is verified as constant. Returned zero
10657 means EXP is variable. Also the constant detection may fail for
10658 some constant expressions and in such case still falsely return
10659 zero. */
10660
10661static int
10662watchpoint_exp_is_const (const struct expression *exp)
10663{
10664 int i = exp->nelts;
10665
10666 while (i > 0)
10667 {
10668 int oplenp, argsp;
10669
10670 /* We are only interested in the descriptor of each element. */
10671 operator_length (exp, i, &oplenp, &argsp);
10672 i -= oplenp;
10673
10674 switch (exp->elts[i].opcode)
10675 {
10676 case BINOP_ADD:
10677 case BINOP_SUB:
10678 case BINOP_MUL:
10679 case BINOP_DIV:
10680 case BINOP_REM:
10681 case BINOP_MOD:
10682 case BINOP_LSH:
10683 case BINOP_RSH:
10684 case BINOP_LOGICAL_AND:
10685 case BINOP_LOGICAL_OR:
10686 case BINOP_BITWISE_AND:
10687 case BINOP_BITWISE_IOR:
10688 case BINOP_BITWISE_XOR:
10689 case BINOP_EQUAL:
10690 case BINOP_NOTEQUAL:
10691 case BINOP_LESS:
10692 case BINOP_GTR:
10693 case BINOP_LEQ:
10694 case BINOP_GEQ:
10695 case BINOP_REPEAT:
10696 case BINOP_COMMA:
10697 case BINOP_EXP:
10698 case BINOP_MIN:
10699 case BINOP_MAX:
10700 case BINOP_INTDIV:
10701 case BINOP_CONCAT:
10702 case TERNOP_COND:
10703 case TERNOP_SLICE:
10704
10705 case OP_LONG:
10706 case OP_DOUBLE:
10707 case OP_DECFLOAT:
10708 case OP_LAST:
10709 case OP_COMPLEX:
10710 case OP_STRING:
10711 case OP_ARRAY:
10712 case OP_TYPE:
10713 case OP_TYPEOF:
10714 case OP_DECLTYPE:
10715 case OP_TYPEID:
10716 case OP_NAME:
10717 case OP_OBJC_NSSTRING:
10718
10719 case UNOP_NEG:
10720 case UNOP_LOGICAL_NOT:
10721 case UNOP_COMPLEMENT:
10722 case UNOP_ADDR:
10723 case UNOP_HIGH:
10724 case UNOP_CAST:
10725
10726 case UNOP_CAST_TYPE:
10727 case UNOP_REINTERPRET_CAST:
10728 case UNOP_DYNAMIC_CAST:
10729 /* Unary, binary and ternary operators: We have to check
10730 their operands. If they are constant, then so is the
10731 result of that operation. For instance, if A and B are
10732 determined to be constants, then so is "A + B".
10733
10734 UNOP_IND is one exception to the rule above, because the
10735 value of *ADDR is not necessarily a constant, even when
10736 ADDR is. */
10737 break;
10738
10739 case OP_VAR_VALUE:
10740 /* Check whether the associated symbol is a constant.
10741
10742 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10743 possible that a buggy compiler could mark a variable as
10744 constant even when it is not, and TYPE_CONST would return
10745 true in this case, while SYMBOL_CLASS wouldn't.
10746
10747 We also have to check for function symbols because they
10748 are always constant. */
10749 {
10750 struct symbol *s = exp->elts[i + 2].symbol;
10751
10752 if (SYMBOL_CLASS (s) != LOC_BLOCK
10753 && SYMBOL_CLASS (s) != LOC_CONST
10754 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10755 return 0;
10756 break;
10757 }
10758
10759 /* The default action is to return 0 because we are using
10760 the optimistic approach here: If we don't know something,
10761 then it is not a constant. */
10762 default:
10763 return 0;
10764 }
10765 }
10766
10767 return 1;
10768}
10769
10770/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10771
10772static void
10773dtor_watchpoint (struct breakpoint *self)
10774{
10775 struct watchpoint *w = (struct watchpoint *) self;
10776
10777 xfree (w->cond_exp);
10778 xfree (w->exp);
10779 xfree (w->exp_string);
10780 xfree (w->exp_string_reparse);
10781 value_free (w->val);
10782
10783 base_breakpoint_ops.dtor (self);
10784}
10785
10786/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10787
10788static void
10789re_set_watchpoint (struct breakpoint *b)
10790{
10791 struct watchpoint *w = (struct watchpoint *) b;
10792
10793 /* Watchpoint can be either on expression using entirely global
10794 variables, or it can be on local variables.
10795
10796 Watchpoints of the first kind are never auto-deleted, and even
10797 persist across program restarts. Since they can use variables
10798 from shared libraries, we need to reparse expression as libraries
10799 are loaded and unloaded.
10800
10801 Watchpoints on local variables can also change meaning as result
10802 of solib event. For example, if a watchpoint uses both a local
10803 and a global variables in expression, it's a local watchpoint,
10804 but unloading of a shared library will make the expression
10805 invalid. This is not a very common use case, but we still
10806 re-evaluate expression, to avoid surprises to the user.
10807
10808 Note that for local watchpoints, we re-evaluate it only if
10809 watchpoints frame id is still valid. If it's not, it means the
10810 watchpoint is out of scope and will be deleted soon. In fact,
10811 I'm not sure we'll ever be called in this case.
10812
10813 If a local watchpoint's frame id is still valid, then
10814 w->exp_valid_block is likewise valid, and we can safely use it.
10815
10816 Don't do anything about disabled watchpoints, since they will be
10817 reevaluated again when enabled. */
10818 update_watchpoint (w, 1 /* reparse */);
10819}
10820
10821/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10822
10823static int
10824insert_watchpoint (struct bp_location *bl)
10825{
10826 struct watchpoint *w = (struct watchpoint *) bl->owner;
10827 int length = w->exact ? 1 : bl->length;
10828
10829 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10830 w->cond_exp);
10831}
10832
10833/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10834
10835static int
10836remove_watchpoint (struct bp_location *bl)
10837{
10838 struct watchpoint *w = (struct watchpoint *) bl->owner;
10839 int length = w->exact ? 1 : bl->length;
10840
10841 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10842 w->cond_exp);
10843}
10844
10845static int
10846breakpoint_hit_watchpoint (const struct bp_location *bl,
10847 struct address_space *aspace, CORE_ADDR bp_addr,
10848 const struct target_waitstatus *ws)
10849{
10850 struct breakpoint *b = bl->owner;
10851 struct watchpoint *w = (struct watchpoint *) b;
10852
10853 /* Continuable hardware watchpoints are treated as non-existent if the
10854 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10855 some data address). Otherwise gdb won't stop on a break instruction
10856 in the code (not from a breakpoint) when a hardware watchpoint has
10857 been defined. Also skip watchpoints which we know did not trigger
10858 (did not match the data address). */
10859 if (is_hardware_watchpoint (b)
10860 && w->watchpoint_triggered == watch_triggered_no)
10861 return 0;
10862
10863 return 1;
10864}
10865
10866static void
10867check_status_watchpoint (bpstat bs)
10868{
10869 gdb_assert (is_watchpoint (bs->breakpoint_at));
10870
10871 bpstat_check_watchpoint (bs);
10872}
10873
10874/* Implement the "resources_needed" breakpoint_ops method for
10875 hardware watchpoints. */
10876
10877static int
10878resources_needed_watchpoint (const struct bp_location *bl)
10879{
10880 struct watchpoint *w = (struct watchpoint *) bl->owner;
10881 int length = w->exact? 1 : bl->length;
10882
10883 return target_region_ok_for_hw_watchpoint (bl->address, length);
10884}
10885
10886/* Implement the "works_in_software_mode" breakpoint_ops method for
10887 hardware watchpoints. */
10888
10889static int
10890works_in_software_mode_watchpoint (const struct breakpoint *b)
10891{
10892 /* Read and access watchpoints only work with hardware support. */
10893 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10894}
10895
10896static enum print_stop_action
10897print_it_watchpoint (bpstat bs)
10898{
10899 struct cleanup *old_chain;
10900 struct breakpoint *b;
10901 struct ui_file *stb;
10902 enum print_stop_action result;
10903 struct watchpoint *w;
10904 struct ui_out *uiout = current_uiout;
10905
10906 gdb_assert (bs->bp_location_at != NULL);
10907
10908 b = bs->breakpoint_at;
10909 w = (struct watchpoint *) b;
10910
10911 stb = mem_fileopen ();
10912 old_chain = make_cleanup_ui_file_delete (stb);
10913
10914 switch (b->type)
10915 {
10916 case bp_watchpoint:
10917 case bp_hardware_watchpoint:
10918 annotate_watchpoint (b->number);
10919 if (ui_out_is_mi_like_p (uiout))
10920 ui_out_field_string
10921 (uiout, "reason",
10922 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10923 mention (b);
10924 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10925 ui_out_text (uiout, "\nOld value = ");
10926 watchpoint_value_print (bs->old_val, stb);
10927 ui_out_field_stream (uiout, "old", stb);
10928 ui_out_text (uiout, "\nNew value = ");
10929 watchpoint_value_print (w->val, stb);
10930 ui_out_field_stream (uiout, "new", stb);
10931 ui_out_text (uiout, "\n");
10932 /* More than one watchpoint may have been triggered. */
10933 result = PRINT_UNKNOWN;
10934 break;
10935
10936 case bp_read_watchpoint:
10937 if (ui_out_is_mi_like_p (uiout))
10938 ui_out_field_string
10939 (uiout, "reason",
10940 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10941 mention (b);
10942 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10943 ui_out_text (uiout, "\nValue = ");
10944 watchpoint_value_print (w->val, stb);
10945 ui_out_field_stream (uiout, "value", stb);
10946 ui_out_text (uiout, "\n");
10947 result = PRINT_UNKNOWN;
10948 break;
10949
10950 case bp_access_watchpoint:
10951 if (bs->old_val != NULL)
10952 {
10953 annotate_watchpoint (b->number);
10954 if (ui_out_is_mi_like_p (uiout))
10955 ui_out_field_string
10956 (uiout, "reason",
10957 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10958 mention (b);
10959 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10960 ui_out_text (uiout, "\nOld value = ");
10961 watchpoint_value_print (bs->old_val, stb);
10962 ui_out_field_stream (uiout, "old", stb);
10963 ui_out_text (uiout, "\nNew value = ");
10964 }
10965 else
10966 {
10967 mention (b);
10968 if (ui_out_is_mi_like_p (uiout))
10969 ui_out_field_string
10970 (uiout, "reason",
10971 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10972 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10973 ui_out_text (uiout, "\nValue = ");
10974 }
10975 watchpoint_value_print (w->val, stb);
10976 ui_out_field_stream (uiout, "new", stb);
10977 ui_out_text (uiout, "\n");
10978 result = PRINT_UNKNOWN;
10979 break;
10980 default:
10981 result = PRINT_UNKNOWN;
10982 }
10983
10984 do_cleanups (old_chain);
10985 return result;
10986}
10987
10988/* Implement the "print_mention" breakpoint_ops method for hardware
10989 watchpoints. */
10990
10991static void
10992print_mention_watchpoint (struct breakpoint *b)
10993{
10994 struct cleanup *ui_out_chain;
10995 struct watchpoint *w = (struct watchpoint *) b;
10996 struct ui_out *uiout = current_uiout;
10997
10998 switch (b->type)
10999 {
11000 case bp_watchpoint:
11001 ui_out_text (uiout, "Watchpoint ");
11002 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11003 break;
11004 case bp_hardware_watchpoint:
11005 ui_out_text (uiout, "Hardware watchpoint ");
11006 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11007 break;
11008 case bp_read_watchpoint:
11009 ui_out_text (uiout, "Hardware read watchpoint ");
11010 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11011 break;
11012 case bp_access_watchpoint:
11013 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11014 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11015 break;
11016 default:
11017 internal_error (__FILE__, __LINE__,
11018 _("Invalid hardware watchpoint type."));
11019 }
11020
11021 ui_out_field_int (uiout, "number", b->number);
11022 ui_out_text (uiout, ": ");
11023 ui_out_field_string (uiout, "exp", w->exp_string);
11024 do_cleanups (ui_out_chain);
11025}
11026
11027/* Implement the "print_recreate" breakpoint_ops method for
11028 watchpoints. */
11029
11030static void
11031print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11032{
11033 struct watchpoint *w = (struct watchpoint *) b;
11034
11035 switch (b->type)
11036 {
11037 case bp_watchpoint:
11038 case bp_hardware_watchpoint:
11039 fprintf_unfiltered (fp, "watch");
11040 break;
11041 case bp_read_watchpoint:
11042 fprintf_unfiltered (fp, "rwatch");
11043 break;
11044 case bp_access_watchpoint:
11045 fprintf_unfiltered (fp, "awatch");
11046 break;
11047 default:
11048 internal_error (__FILE__, __LINE__,
11049 _("Invalid watchpoint type."));
11050 }
11051
11052 fprintf_unfiltered (fp, " %s", w->exp_string);
11053 print_recreate_thread (b, fp);
11054}
11055
11056/* Implement the "explains_signal" breakpoint_ops method for
11057 watchpoints. */
11058
11059static int
11060explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11061{
11062 /* A software watchpoint cannot cause a signal other than
11063 GDB_SIGNAL_TRAP. */
11064 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
11065 return 0;
11066
11067 return 1;
11068}
11069
11070/* The breakpoint_ops structure to be used in hardware watchpoints. */
11071
11072static struct breakpoint_ops watchpoint_breakpoint_ops;
11073
11074/* Implement the "insert" breakpoint_ops method for
11075 masked hardware watchpoints. */
11076
11077static int
11078insert_masked_watchpoint (struct bp_location *bl)
11079{
11080 struct watchpoint *w = (struct watchpoint *) bl->owner;
11081
11082 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
11083 bl->watchpoint_type);
11084}
11085
11086/* Implement the "remove" breakpoint_ops method for
11087 masked hardware watchpoints. */
11088
11089static int
11090remove_masked_watchpoint (struct bp_location *bl)
11091{
11092 struct watchpoint *w = (struct watchpoint *) bl->owner;
11093
11094 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
11095 bl->watchpoint_type);
11096}
11097
11098/* Implement the "resources_needed" breakpoint_ops method for
11099 masked hardware watchpoints. */
11100
11101static int
11102resources_needed_masked_watchpoint (const struct bp_location *bl)
11103{
11104 struct watchpoint *w = (struct watchpoint *) bl->owner;
11105
11106 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
11107}
11108
11109/* Implement the "works_in_software_mode" breakpoint_ops method for
11110 masked hardware watchpoints. */
11111
11112static int
11113works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11114{
11115 return 0;
11116}
11117
11118/* Implement the "print_it" breakpoint_ops method for
11119 masked hardware watchpoints. */
11120
11121static enum print_stop_action
11122print_it_masked_watchpoint (bpstat bs)
11123{
11124 struct breakpoint *b = bs->breakpoint_at;
11125 struct ui_out *uiout = current_uiout;
11126
11127 /* Masked watchpoints have only one location. */
11128 gdb_assert (b->loc && b->loc->next == NULL);
11129
11130 switch (b->type)
11131 {
11132 case bp_hardware_watchpoint:
11133 annotate_watchpoint (b->number);
11134 if (ui_out_is_mi_like_p (uiout))
11135 ui_out_field_string
11136 (uiout, "reason",
11137 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11138 break;
11139
11140 case bp_read_watchpoint:
11141 if (ui_out_is_mi_like_p (uiout))
11142 ui_out_field_string
11143 (uiout, "reason",
11144 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11145 break;
11146
11147 case bp_access_watchpoint:
11148 if (ui_out_is_mi_like_p (uiout))
11149 ui_out_field_string
11150 (uiout, "reason",
11151 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11152 break;
11153 default:
11154 internal_error (__FILE__, __LINE__,
11155 _("Invalid hardware watchpoint type."));
11156 }
11157
11158 mention (b);
11159 ui_out_text (uiout, _("\n\
11160Check the underlying instruction at PC for the memory\n\
11161address and value which triggered this watchpoint.\n"));
11162 ui_out_text (uiout, "\n");
11163
11164 /* More than one watchpoint may have been triggered. */
11165 return PRINT_UNKNOWN;
11166}
11167
11168/* Implement the "print_one_detail" breakpoint_ops method for
11169 masked hardware watchpoints. */
11170
11171static void
11172print_one_detail_masked_watchpoint (const struct breakpoint *b,
11173 struct ui_out *uiout)
11174{
11175 struct watchpoint *w = (struct watchpoint *) b;
11176
11177 /* Masked watchpoints have only one location. */
11178 gdb_assert (b->loc && b->loc->next == NULL);
11179
11180 ui_out_text (uiout, "\tmask ");
11181 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11182 ui_out_text (uiout, "\n");
11183}
11184
11185/* Implement the "print_mention" breakpoint_ops method for
11186 masked hardware watchpoints. */
11187
11188static void
11189print_mention_masked_watchpoint (struct breakpoint *b)
11190{
11191 struct watchpoint *w = (struct watchpoint *) b;
11192 struct ui_out *uiout = current_uiout;
11193 struct cleanup *ui_out_chain;
11194
11195 switch (b->type)
11196 {
11197 case bp_hardware_watchpoint:
11198 ui_out_text (uiout, "Masked hardware watchpoint ");
11199 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11200 break;
11201 case bp_read_watchpoint:
11202 ui_out_text (uiout, "Masked hardware read watchpoint ");
11203 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11204 break;
11205 case bp_access_watchpoint:
11206 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11207 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11208 break;
11209 default:
11210 internal_error (__FILE__, __LINE__,
11211 _("Invalid hardware watchpoint type."));
11212 }
11213
11214 ui_out_field_int (uiout, "number", b->number);
11215 ui_out_text (uiout, ": ");
11216 ui_out_field_string (uiout, "exp", w->exp_string);
11217 do_cleanups (ui_out_chain);
11218}
11219
11220/* Implement the "print_recreate" breakpoint_ops method for
11221 masked hardware watchpoints. */
11222
11223static void
11224print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11225{
11226 struct watchpoint *w = (struct watchpoint *) b;
11227 char tmp[40];
11228
11229 switch (b->type)
11230 {
11231 case bp_hardware_watchpoint:
11232 fprintf_unfiltered (fp, "watch");
11233 break;
11234 case bp_read_watchpoint:
11235 fprintf_unfiltered (fp, "rwatch");
11236 break;
11237 case bp_access_watchpoint:
11238 fprintf_unfiltered (fp, "awatch");
11239 break;
11240 default:
11241 internal_error (__FILE__, __LINE__,
11242 _("Invalid hardware watchpoint type."));
11243 }
11244
11245 sprintf_vma (tmp, w->hw_wp_mask);
11246 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11247 print_recreate_thread (b, fp);
11248}
11249
11250/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11251
11252static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11253
11254/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11255
11256static int
11257is_masked_watchpoint (const struct breakpoint *b)
11258{
11259 return b->ops == &masked_watchpoint_breakpoint_ops;
11260}
11261
11262/* accessflag: hw_write: watch write,
11263 hw_read: watch read,
11264 hw_access: watch access (read or write) */
11265static void
11266watch_command_1 (const char *arg, int accessflag, int from_tty,
11267 int just_location, int internal)
11268{
11269 volatile struct gdb_exception e;
11270 struct breakpoint *b, *scope_breakpoint = NULL;
11271 struct expression *exp;
11272 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11273 struct value *val, *mark, *result;
11274 int saved_bitpos = 0, saved_bitsize = 0;
11275 struct frame_info *frame;
11276 const char *exp_start = NULL;
11277 const char *exp_end = NULL;
11278 const char *tok, *end_tok;
11279 int toklen = -1;
11280 const char *cond_start = NULL;
11281 const char *cond_end = NULL;
11282 enum bptype bp_type;
11283 int thread = -1;
11284 int pc = 0;
11285 /* Flag to indicate whether we are going to use masks for
11286 the hardware watchpoint. */
11287 int use_mask = 0;
11288 CORE_ADDR mask = 0;
11289 struct watchpoint *w;
11290 char *expression;
11291 struct cleanup *back_to;
11292
11293 /* Make sure that we actually have parameters to parse. */
11294 if (arg != NULL && arg[0] != '\0')
11295 {
11296 const char *value_start;
11297
11298 exp_end = arg + strlen (arg);
11299
11300 /* Look for "parameter value" pairs at the end
11301 of the arguments string. */
11302 for (tok = exp_end - 1; tok > arg; tok--)
11303 {
11304 /* Skip whitespace at the end of the argument list. */
11305 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11306 tok--;
11307
11308 /* Find the beginning of the last token.
11309 This is the value of the parameter. */
11310 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11311 tok--;
11312 value_start = tok + 1;
11313
11314 /* Skip whitespace. */
11315 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11316 tok--;
11317
11318 end_tok = tok;
11319
11320 /* Find the beginning of the second to last token.
11321 This is the parameter itself. */
11322 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11323 tok--;
11324 tok++;
11325 toklen = end_tok - tok + 1;
11326
11327 if (toklen == 6 && !strncmp (tok, "thread", 6))
11328 {
11329 /* At this point we've found a "thread" token, which means
11330 the user is trying to set a watchpoint that triggers
11331 only in a specific thread. */
11332 char *endp;
11333
11334 if (thread != -1)
11335 error(_("You can specify only one thread."));
11336
11337 /* Extract the thread ID from the next token. */
11338 thread = strtol (value_start, &endp, 0);
11339
11340 /* Check if the user provided a valid numeric value for the
11341 thread ID. */
11342 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11343 error (_("Invalid thread ID specification %s."), value_start);
11344
11345 /* Check if the thread actually exists. */
11346 if (!valid_thread_id (thread))
11347 invalid_thread_id_error (thread);
11348 }
11349 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11350 {
11351 /* We've found a "mask" token, which means the user wants to
11352 create a hardware watchpoint that is going to have the mask
11353 facility. */
11354 struct value *mask_value, *mark;
11355
11356 if (use_mask)
11357 error(_("You can specify only one mask."));
11358
11359 use_mask = just_location = 1;
11360
11361 mark = value_mark ();
11362 mask_value = parse_to_comma_and_eval (&value_start);
11363 mask = value_as_address (mask_value);
11364 value_free_to_mark (mark);
11365 }
11366 else
11367 /* We didn't recognize what we found. We should stop here. */
11368 break;
11369
11370 /* Truncate the string and get rid of the "parameter value" pair before
11371 the arguments string is parsed by the parse_exp_1 function. */
11372 exp_end = tok;
11373 }
11374 }
11375 else
11376 exp_end = arg;
11377
11378 /* Parse the rest of the arguments. From here on out, everything
11379 is in terms of a newly allocated string instead of the original
11380 ARG. */
11381 innermost_block = NULL;
11382 expression = savestring (arg, exp_end - arg);
11383 back_to = make_cleanup (xfree, expression);
11384 exp_start = arg = expression;
11385 exp = parse_exp_1 (&arg, 0, 0, 0);
11386 exp_end = arg;
11387 /* Remove trailing whitespace from the expression before saving it.
11388 This makes the eventual display of the expression string a bit
11389 prettier. */
11390 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11391 --exp_end;
11392
11393 /* Checking if the expression is not constant. */
11394 if (watchpoint_exp_is_const (exp))
11395 {
11396 int len;
11397
11398 len = exp_end - exp_start;
11399 while (len > 0 && isspace (exp_start[len - 1]))
11400 len--;
11401 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11402 }
11403
11404 exp_valid_block = innermost_block;
11405 mark = value_mark ();
11406 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11407
11408 if (val != NULL && just_location)
11409 {
11410 saved_bitpos = value_bitpos (val);
11411 saved_bitsize = value_bitsize (val);
11412 }
11413
11414 if (just_location)
11415 {
11416 int ret;
11417
11418 exp_valid_block = NULL;
11419 val = value_addr (result);
11420 release_value (val);
11421 value_free_to_mark (mark);
11422
11423 if (use_mask)
11424 {
11425 ret = target_masked_watch_num_registers (value_as_address (val),
11426 mask);
11427 if (ret == -1)
11428 error (_("This target does not support masked watchpoints."));
11429 else if (ret == -2)
11430 error (_("Invalid mask or memory region."));
11431 }
11432 }
11433 else if (val != NULL)
11434 release_value (val);
11435
11436 tok = skip_spaces_const (arg);
11437 end_tok = skip_to_space_const (tok);
11438
11439 toklen = end_tok - tok;
11440 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11441 {
11442 struct expression *cond;
11443
11444 innermost_block = NULL;
11445 tok = cond_start = end_tok + 1;
11446 cond = parse_exp_1 (&tok, 0, 0, 0);
11447
11448 /* The watchpoint expression may not be local, but the condition
11449 may still be. E.g.: `watch global if local > 0'. */
11450 cond_exp_valid_block = innermost_block;
11451
11452 xfree (cond);
11453 cond_end = tok;
11454 }
11455 if (*tok)
11456 error (_("Junk at end of command."));
11457
11458 frame = block_innermost_frame (exp_valid_block);
11459
11460 /* If the expression is "local", then set up a "watchpoint scope"
11461 breakpoint at the point where we've left the scope of the watchpoint
11462 expression. Create the scope breakpoint before the watchpoint, so
11463 that we will encounter it first in bpstat_stop_status. */
11464 if (exp_valid_block && frame)
11465 {
11466 if (frame_id_p (frame_unwind_caller_id (frame)))
11467 {
11468 scope_breakpoint
11469 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11470 frame_unwind_caller_pc (frame),
11471 bp_watchpoint_scope,
11472 &momentary_breakpoint_ops);
11473
11474 scope_breakpoint->enable_state = bp_enabled;
11475
11476 /* Automatically delete the breakpoint when it hits. */
11477 scope_breakpoint->disposition = disp_del;
11478
11479 /* Only break in the proper frame (help with recursion). */
11480 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11481
11482 /* Set the address at which we will stop. */
11483 scope_breakpoint->loc->gdbarch
11484 = frame_unwind_caller_arch (frame);
11485 scope_breakpoint->loc->requested_address
11486 = frame_unwind_caller_pc (frame);
11487 scope_breakpoint->loc->address
11488 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11489 scope_breakpoint->loc->requested_address,
11490 scope_breakpoint->type);
11491 }
11492 }
11493
11494 /* Now set up the breakpoint. We create all watchpoints as hardware
11495 watchpoints here even if hardware watchpoints are turned off, a call
11496 to update_watchpoint later in this function will cause the type to
11497 drop back to bp_watchpoint (software watchpoint) if required. */
11498
11499 if (accessflag == hw_read)
11500 bp_type = bp_read_watchpoint;
11501 else if (accessflag == hw_access)
11502 bp_type = bp_access_watchpoint;
11503 else
11504 bp_type = bp_hardware_watchpoint;
11505
11506 w = XCNEW (struct watchpoint);
11507 b = &w->base;
11508 if (use_mask)
11509 init_raw_breakpoint_without_location (b, NULL, bp_type,
11510 &masked_watchpoint_breakpoint_ops);
11511 else
11512 init_raw_breakpoint_without_location (b, NULL, bp_type,
11513 &watchpoint_breakpoint_ops);
11514 b->thread = thread;
11515 b->disposition = disp_donttouch;
11516 b->pspace = current_program_space;
11517 w->exp = exp;
11518 w->exp_valid_block = exp_valid_block;
11519 w->cond_exp_valid_block = cond_exp_valid_block;
11520 if (just_location)
11521 {
11522 struct type *t = value_type (val);
11523 CORE_ADDR addr = value_as_address (val);
11524 char *name;
11525
11526 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11527 name = type_to_string (t);
11528
11529 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11530 core_addr_to_string (addr));
11531 xfree (name);
11532
11533 w->exp_string = xstrprintf ("-location %.*s",
11534 (int) (exp_end - exp_start), exp_start);
11535
11536 /* The above expression is in C. */
11537 b->language = language_c;
11538 }
11539 else
11540 w->exp_string = savestring (exp_start, exp_end - exp_start);
11541
11542 if (use_mask)
11543 {
11544 w->hw_wp_mask = mask;
11545 }
11546 else
11547 {
11548 w->val = val;
11549 w->val_bitpos = saved_bitpos;
11550 w->val_bitsize = saved_bitsize;
11551 w->val_valid = 1;
11552 }
11553
11554 if (cond_start)
11555 b->cond_string = savestring (cond_start, cond_end - cond_start);
11556 else
11557 b->cond_string = 0;
11558
11559 if (frame)
11560 {
11561 w->watchpoint_frame = get_frame_id (frame);
11562 w->watchpoint_thread = inferior_ptid;
11563 }
11564 else
11565 {
11566 w->watchpoint_frame = null_frame_id;
11567 w->watchpoint_thread = null_ptid;
11568 }
11569
11570 if (scope_breakpoint != NULL)
11571 {
11572 /* The scope breakpoint is related to the watchpoint. We will
11573 need to act on them together. */
11574 b->related_breakpoint = scope_breakpoint;
11575 scope_breakpoint->related_breakpoint = b;
11576 }
11577
11578 if (!just_location)
11579 value_free_to_mark (mark);
11580
11581 TRY_CATCH (e, RETURN_MASK_ALL)
11582 {
11583 /* Finally update the new watchpoint. This creates the locations
11584 that should be inserted. */
11585 update_watchpoint (w, 1);
11586 }
11587 if (e.reason < 0)
11588 {
11589 delete_breakpoint (b);
11590 throw_exception (e);
11591 }
11592
11593 install_breakpoint (internal, b, 1);
11594 do_cleanups (back_to);
11595}
11596
11597/* Return count of debug registers needed to watch the given expression.
11598 If the watchpoint cannot be handled in hardware return zero. */
11599
11600static int
11601can_use_hardware_watchpoint (struct value *v)
11602{
11603 int found_memory_cnt = 0;
11604 struct value *head = v;
11605
11606 /* Did the user specifically forbid us to use hardware watchpoints? */
11607 if (!can_use_hw_watchpoints)
11608 return 0;
11609
11610 /* Make sure that the value of the expression depends only upon
11611 memory contents, and values computed from them within GDB. If we
11612 find any register references or function calls, we can't use a
11613 hardware watchpoint.
11614
11615 The idea here is that evaluating an expression generates a series
11616 of values, one holding the value of every subexpression. (The
11617 expression a*b+c has five subexpressions: a, b, a*b, c, and
11618 a*b+c.) GDB's values hold almost enough information to establish
11619 the criteria given above --- they identify memory lvalues,
11620 register lvalues, computed values, etcetera. So we can evaluate
11621 the expression, and then scan the chain of values that leaves
11622 behind to decide whether we can detect any possible change to the
11623 expression's final value using only hardware watchpoints.
11624
11625 However, I don't think that the values returned by inferior
11626 function calls are special in any way. So this function may not
11627 notice that an expression involving an inferior function call
11628 can't be watched with hardware watchpoints. FIXME. */
11629 for (; v; v = value_next (v))
11630 {
11631 if (VALUE_LVAL (v) == lval_memory)
11632 {
11633 if (v != head && value_lazy (v))
11634 /* A lazy memory lvalue in the chain is one that GDB never
11635 needed to fetch; we either just used its address (e.g.,
11636 `a' in `a.b') or we never needed it at all (e.g., `a'
11637 in `a,b'). This doesn't apply to HEAD; if that is
11638 lazy then it was not readable, but watch it anyway. */
11639 ;
11640 else
11641 {
11642 /* Ahh, memory we actually used! Check if we can cover
11643 it with hardware watchpoints. */
11644 struct type *vtype = check_typedef (value_type (v));
11645
11646 /* We only watch structs and arrays if user asked for it
11647 explicitly, never if they just happen to appear in a
11648 middle of some value chain. */
11649 if (v == head
11650 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11651 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11652 {
11653 CORE_ADDR vaddr = value_address (v);
11654 int len;
11655 int num_regs;
11656
11657 len = (target_exact_watchpoints
11658 && is_scalar_type_recursive (vtype))?
11659 1 : TYPE_LENGTH (value_type (v));
11660
11661 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11662 if (!num_regs)
11663 return 0;
11664 else
11665 found_memory_cnt += num_regs;
11666 }
11667 }
11668 }
11669 else if (VALUE_LVAL (v) != not_lval
11670 && deprecated_value_modifiable (v) == 0)
11671 return 0; /* These are values from the history (e.g., $1). */
11672 else if (VALUE_LVAL (v) == lval_register)
11673 return 0; /* Cannot watch a register with a HW watchpoint. */
11674 }
11675
11676 /* The expression itself looks suitable for using a hardware
11677 watchpoint, but give the target machine a chance to reject it. */
11678 return found_memory_cnt;
11679}
11680
11681void
11682watch_command_wrapper (char *arg, int from_tty, int internal)
11683{
11684 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11685}
11686
11687/* A helper function that looks for the "-location" argument and then
11688 calls watch_command_1. */
11689
11690static void
11691watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11692{
11693 int just_location = 0;
11694
11695 if (arg
11696 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11697 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11698 {
11699 arg = skip_spaces (arg);
11700 just_location = 1;
11701 }
11702
11703 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11704}
11705
11706static void
11707watch_command (char *arg, int from_tty)
11708{
11709 watch_maybe_just_location (arg, hw_write, from_tty);
11710}
11711
11712void
11713rwatch_command_wrapper (char *arg, int from_tty, int internal)
11714{
11715 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11716}
11717
11718static void
11719rwatch_command (char *arg, int from_tty)
11720{
11721 watch_maybe_just_location (arg, hw_read, from_tty);
11722}
11723
11724void
11725awatch_command_wrapper (char *arg, int from_tty, int internal)
11726{
11727 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11728}
11729
11730static void
11731awatch_command (char *arg, int from_tty)
11732{
11733 watch_maybe_just_location (arg, hw_access, from_tty);
11734}
11735\f
11736
11737/* Helper routines for the until_command routine in infcmd.c. Here
11738 because it uses the mechanisms of breakpoints. */
11739
11740struct until_break_command_continuation_args
11741{
11742 struct breakpoint *breakpoint;
11743 struct breakpoint *breakpoint2;
11744 int thread_num;
11745};
11746
11747/* This function is called by fetch_inferior_event via the
11748 cmd_continuation pointer, to complete the until command. It takes
11749 care of cleaning up the temporary breakpoints set up by the until
11750 command. */
11751static void
11752until_break_command_continuation (void *arg, int err)
11753{
11754 struct until_break_command_continuation_args *a = arg;
11755
11756 delete_breakpoint (a->breakpoint);
11757 if (a->breakpoint2)
11758 delete_breakpoint (a->breakpoint2);
11759 delete_longjmp_breakpoint (a->thread_num);
11760}
11761
11762void
11763until_break_command (char *arg, int from_tty, int anywhere)
11764{
11765 struct symtabs_and_lines sals;
11766 struct symtab_and_line sal;
11767 struct frame_info *frame;
11768 struct gdbarch *frame_gdbarch;
11769 struct frame_id stack_frame_id;
11770 struct frame_id caller_frame_id;
11771 struct breakpoint *breakpoint;
11772 struct breakpoint *breakpoint2 = NULL;
11773 struct cleanup *old_chain;
11774 int thread;
11775 struct thread_info *tp;
11776
11777 clear_proceed_status (0);
11778
11779 /* Set a breakpoint where the user wants it and at return from
11780 this function. */
11781
11782 if (last_displayed_sal_is_valid ())
11783 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11784 get_last_displayed_symtab (),
11785 get_last_displayed_line ());
11786 else
11787 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11788 (struct symtab *) NULL, 0);
11789
11790 if (sals.nelts != 1)
11791 error (_("Couldn't get information on specified line."));
11792
11793 sal = sals.sals[0];
11794 xfree (sals.sals); /* malloc'd, so freed. */
11795
11796 if (*arg)
11797 error (_("Junk at end of arguments."));
11798
11799 resolve_sal_pc (&sal);
11800
11801 tp = inferior_thread ();
11802 thread = tp->num;
11803
11804 old_chain = make_cleanup (null_cleanup, NULL);
11805
11806 /* Note linespec handling above invalidates the frame chain.
11807 Installing a breakpoint also invalidates the frame chain (as it
11808 may need to switch threads), so do any frame handling before
11809 that. */
11810
11811 frame = get_selected_frame (NULL);
11812 frame_gdbarch = get_frame_arch (frame);
11813 stack_frame_id = get_stack_frame_id (frame);
11814 caller_frame_id = frame_unwind_caller_id (frame);
11815
11816 /* Keep within the current frame, or in frames called by the current
11817 one. */
11818
11819 if (frame_id_p (caller_frame_id))
11820 {
11821 struct symtab_and_line sal2;
11822
11823 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11824 sal2.pc = frame_unwind_caller_pc (frame);
11825 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11826 sal2,
11827 caller_frame_id,
11828 bp_until);
11829 make_cleanup_delete_breakpoint (breakpoint2);
11830
11831 set_longjmp_breakpoint (tp, caller_frame_id);
11832 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11833 }
11834
11835 /* set_momentary_breakpoint could invalidate FRAME. */
11836 frame = NULL;
11837
11838 if (anywhere)
11839 /* If the user told us to continue until a specified location,
11840 we don't specify a frame at which we need to stop. */
11841 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11842 null_frame_id, bp_until);
11843 else
11844 /* Otherwise, specify the selected frame, because we want to stop
11845 only at the very same frame. */
11846 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11847 stack_frame_id, bp_until);
11848 make_cleanup_delete_breakpoint (breakpoint);
11849
11850 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11851
11852 /* If we are running asynchronously, and proceed call above has
11853 actually managed to start the target, arrange for breakpoints to
11854 be deleted when the target stops. Otherwise, we're already
11855 stopped and delete breakpoints via cleanup chain. */
11856
11857 if (target_can_async_p () && is_running (inferior_ptid))
11858 {
11859 struct until_break_command_continuation_args *args;
11860 args = xmalloc (sizeof (*args));
11861
11862 args->breakpoint = breakpoint;
11863 args->breakpoint2 = breakpoint2;
11864 args->thread_num = thread;
11865
11866 discard_cleanups (old_chain);
11867 add_continuation (inferior_thread (),
11868 until_break_command_continuation, args,
11869 xfree);
11870 }
11871 else
11872 do_cleanups (old_chain);
11873}
11874
11875/* This function attempts to parse an optional "if <cond>" clause
11876 from the arg string. If one is not found, it returns NULL.
11877
11878 Else, it returns a pointer to the condition string. (It does not
11879 attempt to evaluate the string against a particular block.) And,
11880 it updates arg to point to the first character following the parsed
11881 if clause in the arg string. */
11882
11883char *
11884ep_parse_optional_if_clause (char **arg)
11885{
11886 char *cond_string;
11887
11888 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11889 return NULL;
11890
11891 /* Skip the "if" keyword. */
11892 (*arg) += 2;
11893
11894 /* Skip any extra leading whitespace, and record the start of the
11895 condition string. */
11896 *arg = skip_spaces (*arg);
11897 cond_string = *arg;
11898
11899 /* Assume that the condition occupies the remainder of the arg
11900 string. */
11901 (*arg) += strlen (cond_string);
11902
11903 return cond_string;
11904}
11905
11906/* Commands to deal with catching events, such as signals, exceptions,
11907 process start/exit, etc. */
11908
11909typedef enum
11910{
11911 catch_fork_temporary, catch_vfork_temporary,
11912 catch_fork_permanent, catch_vfork_permanent
11913}
11914catch_fork_kind;
11915
11916static void
11917catch_fork_command_1 (char *arg, int from_tty,
11918 struct cmd_list_element *command)
11919{
11920 struct gdbarch *gdbarch = get_current_arch ();
11921 char *cond_string = NULL;
11922 catch_fork_kind fork_kind;
11923 int tempflag;
11924
11925 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11926 tempflag = (fork_kind == catch_fork_temporary
11927 || fork_kind == catch_vfork_temporary);
11928
11929 if (!arg)
11930 arg = "";
11931 arg = skip_spaces (arg);
11932
11933 /* The allowed syntax is:
11934 catch [v]fork
11935 catch [v]fork if <cond>
11936
11937 First, check if there's an if clause. */
11938 cond_string = ep_parse_optional_if_clause (&arg);
11939
11940 if ((*arg != '\0') && !isspace (*arg))
11941 error (_("Junk at end of arguments."));
11942
11943 /* If this target supports it, create a fork or vfork catchpoint
11944 and enable reporting of such events. */
11945 switch (fork_kind)
11946 {
11947 case catch_fork_temporary:
11948 case catch_fork_permanent:
11949 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11950 &catch_fork_breakpoint_ops);
11951 break;
11952 case catch_vfork_temporary:
11953 case catch_vfork_permanent:
11954 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11955 &catch_vfork_breakpoint_ops);
11956 break;
11957 default:
11958 error (_("unsupported or unknown fork kind; cannot catch it"));
11959 break;
11960 }
11961}
11962
11963static void
11964catch_exec_command_1 (char *arg, int from_tty,
11965 struct cmd_list_element *command)
11966{
11967 struct exec_catchpoint *c;
11968 struct gdbarch *gdbarch = get_current_arch ();
11969 int tempflag;
11970 char *cond_string = NULL;
11971
11972 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11973
11974 if (!arg)
11975 arg = "";
11976 arg = skip_spaces (arg);
11977
11978 /* The allowed syntax is:
11979 catch exec
11980 catch exec if <cond>
11981
11982 First, check if there's an if clause. */
11983 cond_string = ep_parse_optional_if_clause (&arg);
11984
11985 if ((*arg != '\0') && !isspace (*arg))
11986 error (_("Junk at end of arguments."));
11987
11988 c = XNEW (struct exec_catchpoint);
11989 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11990 &catch_exec_breakpoint_ops);
11991 c->exec_pathname = NULL;
11992
11993 install_breakpoint (0, &c->base, 1);
11994}
11995
11996void
11997init_ada_exception_breakpoint (struct breakpoint *b,
11998 struct gdbarch *gdbarch,
11999 struct symtab_and_line sal,
12000 char *addr_string,
12001 const struct breakpoint_ops *ops,
12002 int tempflag,
12003 int enabled,
12004 int from_tty)
12005{
12006 if (from_tty)
12007 {
12008 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12009 if (!loc_gdbarch)
12010 loc_gdbarch = gdbarch;
12011
12012 describe_other_breakpoints (loc_gdbarch,
12013 sal.pspace, sal.pc, sal.section, -1);
12014 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12015 version for exception catchpoints, because two catchpoints
12016 used for different exception names will use the same address.
12017 In this case, a "breakpoint ... also set at..." warning is
12018 unproductive. Besides, the warning phrasing is also a bit
12019 inappropriate, we should use the word catchpoint, and tell
12020 the user what type of catchpoint it is. The above is good
12021 enough for now, though. */
12022 }
12023
12024 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
12025
12026 b->enable_state = enabled ? bp_enabled : bp_disabled;
12027 b->disposition = tempflag ? disp_del : disp_donttouch;
12028 b->addr_string = addr_string;
12029 b->language = language_ada;
12030}
12031
12032/* Splits the argument using space as delimiter. Returns an xmalloc'd
12033 filter list, or NULL if no filtering is required. */
12034static VEC(int) *
12035catch_syscall_split_args (char *arg)
12036{
12037 VEC(int) *result = NULL;
12038 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
12039
12040 while (*arg != '\0')
12041 {
12042 int i, syscall_number;
12043 char *endptr;
12044 char cur_name[128];
12045 struct syscall s;
12046
12047 /* Skip whitespace. */
12048 arg = skip_spaces (arg);
12049
12050 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12051 cur_name[i] = arg[i];
12052 cur_name[i] = '\0';
12053 arg += i;
12054
12055 /* Check if the user provided a syscall name or a number. */
12056 syscall_number = (int) strtol (cur_name, &endptr, 0);
12057 if (*endptr == '\0')
12058 get_syscall_by_number (syscall_number, &s);
12059 else
12060 {
12061 /* We have a name. Let's check if it's valid and convert it
12062 to a number. */
12063 get_syscall_by_name (cur_name, &s);
12064
12065 if (s.number == UNKNOWN_SYSCALL)
12066 /* Here we have to issue an error instead of a warning,
12067 because GDB cannot do anything useful if there's no
12068 syscall number to be caught. */
12069 error (_("Unknown syscall name '%s'."), cur_name);
12070 }
12071
12072 /* Ok, it's valid. */
12073 VEC_safe_push (int, result, s.number);
12074 }
12075
12076 discard_cleanups (cleanup);
12077 return result;
12078}
12079
12080/* Implement the "catch syscall" command. */
12081
12082static void
12083catch_syscall_command_1 (char *arg, int from_tty,
12084 struct cmd_list_element *command)
12085{
12086 int tempflag;
12087 VEC(int) *filter;
12088 struct syscall s;
12089 struct gdbarch *gdbarch = get_current_arch ();
12090
12091 /* Checking if the feature if supported. */
12092 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12093 error (_("The feature 'catch syscall' is not supported on \
12094this architecture yet."));
12095
12096 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12097
12098 arg = skip_spaces (arg);
12099
12100 /* We need to do this first "dummy" translation in order
12101 to get the syscall XML file loaded or, most important,
12102 to display a warning to the user if there's no XML file
12103 for his/her architecture. */
12104 get_syscall_by_number (0, &s);
12105
12106 /* The allowed syntax is:
12107 catch syscall
12108 catch syscall <name | number> [<name | number> ... <name | number>]
12109
12110 Let's check if there's a syscall name. */
12111
12112 if (arg != NULL)
12113 filter = catch_syscall_split_args (arg);
12114 else
12115 filter = NULL;
12116
12117 create_syscall_event_catchpoint (tempflag, filter,
12118 &catch_syscall_breakpoint_ops);
12119}
12120
12121static void
12122catch_command (char *arg, int from_tty)
12123{
12124 error (_("Catch requires an event name."));
12125}
12126\f
12127
12128static void
12129tcatch_command (char *arg, int from_tty)
12130{
12131 error (_("Catch requires an event name."));
12132}
12133
12134/* A qsort comparison function that sorts breakpoints in order. */
12135
12136static int
12137compare_breakpoints (const void *a, const void *b)
12138{
12139 const breakpoint_p *ba = a;
12140 uintptr_t ua = (uintptr_t) *ba;
12141 const breakpoint_p *bb = b;
12142 uintptr_t ub = (uintptr_t) *bb;
12143
12144 if ((*ba)->number < (*bb)->number)
12145 return -1;
12146 else if ((*ba)->number > (*bb)->number)
12147 return 1;
12148
12149 /* Now sort by address, in case we see, e..g, two breakpoints with
12150 the number 0. */
12151 if (ua < ub)
12152 return -1;
12153 return ua > ub ? 1 : 0;
12154}
12155
12156/* Delete breakpoints by address or line. */
12157
12158static void
12159clear_command (char *arg, int from_tty)
12160{
12161 struct breakpoint *b, *prev;
12162 VEC(breakpoint_p) *found = 0;
12163 int ix;
12164 int default_match;
12165 struct symtabs_and_lines sals;
12166 struct symtab_and_line sal;
12167 int i;
12168 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12169
12170 if (arg)
12171 {
12172 sals = decode_line_with_current_source (arg,
12173 (DECODE_LINE_FUNFIRSTLINE
12174 | DECODE_LINE_LIST_MODE));
12175 make_cleanup (xfree, sals.sals);
12176 default_match = 0;
12177 }
12178 else
12179 {
12180 sals.sals = (struct symtab_and_line *)
12181 xmalloc (sizeof (struct symtab_and_line));
12182 make_cleanup (xfree, sals.sals);
12183 init_sal (&sal); /* Initialize to zeroes. */
12184
12185 /* Set sal's line, symtab, pc, and pspace to the values
12186 corresponding to the last call to print_frame_info. If the
12187 codepoint is not valid, this will set all the fields to 0. */
12188 get_last_displayed_sal (&sal);
12189 if (sal.symtab == 0)
12190 error (_("No source file specified."));
12191
12192 sals.sals[0] = sal;
12193 sals.nelts = 1;
12194
12195 default_match = 1;
12196 }
12197
12198 /* We don't call resolve_sal_pc here. That's not as bad as it
12199 seems, because all existing breakpoints typically have both
12200 file/line and pc set. So, if clear is given file/line, we can
12201 match this to existing breakpoint without obtaining pc at all.
12202
12203 We only support clearing given the address explicitly
12204 present in breakpoint table. Say, we've set breakpoint
12205 at file:line. There were several PC values for that file:line,
12206 due to optimization, all in one block.
12207
12208 We've picked one PC value. If "clear" is issued with another
12209 PC corresponding to the same file:line, the breakpoint won't
12210 be cleared. We probably can still clear the breakpoint, but
12211 since the other PC value is never presented to user, user
12212 can only find it by guessing, and it does not seem important
12213 to support that. */
12214
12215 /* For each line spec given, delete bps which correspond to it. Do
12216 it in two passes, solely to preserve the current behavior that
12217 from_tty is forced true if we delete more than one
12218 breakpoint. */
12219
12220 found = NULL;
12221 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12222 for (i = 0; i < sals.nelts; i++)
12223 {
12224 const char *sal_fullname;
12225
12226 /* If exact pc given, clear bpts at that pc.
12227 If line given (pc == 0), clear all bpts on specified line.
12228 If defaulting, clear all bpts on default line
12229 or at default pc.
12230
12231 defaulting sal.pc != 0 tests to do
12232
12233 0 1 pc
12234 1 1 pc _and_ line
12235 0 0 line
12236 1 0 <can't happen> */
12237
12238 sal = sals.sals[i];
12239 sal_fullname = (sal.symtab == NULL
12240 ? NULL : symtab_to_fullname (sal.symtab));
12241
12242 /* Find all matching breakpoints and add them to 'found'. */
12243 ALL_BREAKPOINTS (b)
12244 {
12245 int match = 0;
12246 /* Are we going to delete b? */
12247 if (b->type != bp_none && !is_watchpoint (b))
12248 {
12249 struct bp_location *loc = b->loc;
12250 for (; loc; loc = loc->next)
12251 {
12252 /* If the user specified file:line, don't allow a PC
12253 match. This matches historical gdb behavior. */
12254 int pc_match = (!sal.explicit_line
12255 && sal.pc
12256 && (loc->pspace == sal.pspace)
12257 && (loc->address == sal.pc)
12258 && (!section_is_overlay (loc->section)
12259 || loc->section == sal.section));
12260 int line_match = 0;
12261
12262 if ((default_match || sal.explicit_line)
12263 && loc->symtab != NULL
12264 && sal_fullname != NULL
12265 && sal.pspace == loc->pspace
12266 && loc->line_number == sal.line
12267 && filename_cmp (symtab_to_fullname (loc->symtab),
12268 sal_fullname) == 0)
12269 line_match = 1;
12270
12271 if (pc_match || line_match)
12272 {
12273 match = 1;
12274 break;
12275 }
12276 }
12277 }
12278
12279 if (match)
12280 VEC_safe_push(breakpoint_p, found, b);
12281 }
12282 }
12283
12284 /* Now go thru the 'found' chain and delete them. */
12285 if (VEC_empty(breakpoint_p, found))
12286 {
12287 if (arg)
12288 error (_("No breakpoint at %s."), arg);
12289 else
12290 error (_("No breakpoint at this line."));
12291 }
12292
12293 /* Remove duplicates from the vec. */
12294 qsort (VEC_address (breakpoint_p, found),
12295 VEC_length (breakpoint_p, found),
12296 sizeof (breakpoint_p),
12297 compare_breakpoints);
12298 prev = VEC_index (breakpoint_p, found, 0);
12299 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12300 {
12301 if (b == prev)
12302 {
12303 VEC_ordered_remove (breakpoint_p, found, ix);
12304 --ix;
12305 }
12306 }
12307
12308 if (VEC_length(breakpoint_p, found) > 1)
12309 from_tty = 1; /* Always report if deleted more than one. */
12310 if (from_tty)
12311 {
12312 if (VEC_length(breakpoint_p, found) == 1)
12313 printf_unfiltered (_("Deleted breakpoint "));
12314 else
12315 printf_unfiltered (_("Deleted breakpoints "));
12316 }
12317
12318 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12319 {
12320 if (from_tty)
12321 printf_unfiltered ("%d ", b->number);
12322 delete_breakpoint (b);
12323 }
12324 if (from_tty)
12325 putchar_unfiltered ('\n');
12326
12327 do_cleanups (cleanups);
12328}
12329\f
12330/* Delete breakpoint in BS if they are `delete' breakpoints and
12331 all breakpoints that are marked for deletion, whether hit or not.
12332 This is called after any breakpoint is hit, or after errors. */
12333
12334void
12335breakpoint_auto_delete (bpstat bs)
12336{
12337 struct breakpoint *b, *b_tmp;
12338
12339 for (; bs; bs = bs->next)
12340 if (bs->breakpoint_at
12341 && bs->breakpoint_at->disposition == disp_del
12342 && bs->stop)
12343 delete_breakpoint (bs->breakpoint_at);
12344
12345 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12346 {
12347 if (b->disposition == disp_del_at_next_stop)
12348 delete_breakpoint (b);
12349 }
12350}
12351
12352/* A comparison function for bp_location AP and BP being interfaced to
12353 qsort. Sort elements primarily by their ADDRESS (no matter what
12354 does breakpoint_address_is_meaningful say for its OWNER),
12355 secondarily by ordering first bp_permanent OWNERed elements and
12356 terciarily just ensuring the array is sorted stable way despite
12357 qsort being an unstable algorithm. */
12358
12359static int
12360bp_location_compare (const void *ap, const void *bp)
12361{
12362 struct bp_location *a = *(void **) ap;
12363 struct bp_location *b = *(void **) bp;
12364 /* A and B come from existing breakpoints having non-NULL OWNER. */
12365 int a_perm = a->owner->enable_state == bp_permanent;
12366 int b_perm = b->owner->enable_state == bp_permanent;
12367
12368 if (a->address != b->address)
12369 return (a->address > b->address) - (a->address < b->address);
12370
12371 /* Sort locations at the same address by their pspace number, keeping
12372 locations of the same inferior (in a multi-inferior environment)
12373 grouped. */
12374
12375 if (a->pspace->num != b->pspace->num)
12376 return ((a->pspace->num > b->pspace->num)
12377 - (a->pspace->num < b->pspace->num));
12378
12379 /* Sort permanent breakpoints first. */
12380 if (a_perm != b_perm)
12381 return (a_perm < b_perm) - (a_perm > b_perm);
12382
12383 /* Make the internal GDB representation stable across GDB runs
12384 where A and B memory inside GDB can differ. Breakpoint locations of
12385 the same type at the same address can be sorted in arbitrary order. */
12386
12387 if (a->owner->number != b->owner->number)
12388 return ((a->owner->number > b->owner->number)
12389 - (a->owner->number < b->owner->number));
12390
12391 return (a > b) - (a < b);
12392}
12393
12394/* Set bp_location_placed_address_before_address_max and
12395 bp_location_shadow_len_after_address_max according to the current
12396 content of the bp_location array. */
12397
12398static void
12399bp_location_target_extensions_update (void)
12400{
12401 struct bp_location *bl, **blp_tmp;
12402
12403 bp_location_placed_address_before_address_max = 0;
12404 bp_location_shadow_len_after_address_max = 0;
12405
12406 ALL_BP_LOCATIONS (bl, blp_tmp)
12407 {
12408 CORE_ADDR start, end, addr;
12409
12410 if (!bp_location_has_shadow (bl))
12411 continue;
12412
12413 start = bl->target_info.placed_address;
12414 end = start + bl->target_info.shadow_len;
12415
12416 gdb_assert (bl->address >= start);
12417 addr = bl->address - start;
12418 if (addr > bp_location_placed_address_before_address_max)
12419 bp_location_placed_address_before_address_max = addr;
12420
12421 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12422
12423 gdb_assert (bl->address < end);
12424 addr = end - bl->address;
12425 if (addr > bp_location_shadow_len_after_address_max)
12426 bp_location_shadow_len_after_address_max = addr;
12427 }
12428}
12429
12430/* Download tracepoint locations if they haven't been. */
12431
12432static void
12433download_tracepoint_locations (void)
12434{
12435 struct breakpoint *b;
12436 struct cleanup *old_chain;
12437
12438 if (!target_can_download_tracepoint ())
12439 return;
12440
12441 old_chain = save_current_space_and_thread ();
12442
12443 ALL_TRACEPOINTS (b)
12444 {
12445 struct bp_location *bl;
12446 struct tracepoint *t;
12447 int bp_location_downloaded = 0;
12448
12449 if ((b->type == bp_fast_tracepoint
12450 ? !may_insert_fast_tracepoints
12451 : !may_insert_tracepoints))
12452 continue;
12453
12454 for (bl = b->loc; bl; bl = bl->next)
12455 {
12456 /* In tracepoint, locations are _never_ duplicated, so
12457 should_be_inserted is equivalent to
12458 unduplicated_should_be_inserted. */
12459 if (!should_be_inserted (bl) || bl->inserted)
12460 continue;
12461
12462 switch_to_program_space_and_thread (bl->pspace);
12463
12464 target_download_tracepoint (bl);
12465
12466 bl->inserted = 1;
12467 bp_location_downloaded = 1;
12468 }
12469 t = (struct tracepoint *) b;
12470 t->number_on_target = b->number;
12471 if (bp_location_downloaded)
12472 observer_notify_breakpoint_modified (b);
12473 }
12474
12475 do_cleanups (old_chain);
12476}
12477
12478/* Swap the insertion/duplication state between two locations. */
12479
12480static void
12481swap_insertion (struct bp_location *left, struct bp_location *right)
12482{
12483 const int left_inserted = left->inserted;
12484 const int left_duplicate = left->duplicate;
12485 const int left_needs_update = left->needs_update;
12486 const struct bp_target_info left_target_info = left->target_info;
12487
12488 /* Locations of tracepoints can never be duplicated. */
12489 if (is_tracepoint (left->owner))
12490 gdb_assert (!left->duplicate);
12491 if (is_tracepoint (right->owner))
12492 gdb_assert (!right->duplicate);
12493
12494 left->inserted = right->inserted;
12495 left->duplicate = right->duplicate;
12496 left->needs_update = right->needs_update;
12497 left->target_info = right->target_info;
12498 right->inserted = left_inserted;
12499 right->duplicate = left_duplicate;
12500 right->needs_update = left_needs_update;
12501 right->target_info = left_target_info;
12502}
12503
12504/* Force the re-insertion of the locations at ADDRESS. This is called
12505 once a new/deleted/modified duplicate location is found and we are evaluating
12506 conditions on the target's side. Such conditions need to be updated on
12507 the target. */
12508
12509static void
12510force_breakpoint_reinsertion (struct bp_location *bl)
12511{
12512 struct bp_location **locp = NULL, **loc2p;
12513 struct bp_location *loc;
12514 CORE_ADDR address = 0;
12515 int pspace_num;
12516
12517 address = bl->address;
12518 pspace_num = bl->pspace->num;
12519
12520 /* This is only meaningful if the target is
12521 evaluating conditions and if the user has
12522 opted for condition evaluation on the target's
12523 side. */
12524 if (gdb_evaluates_breakpoint_condition_p ()
12525 || !target_supports_evaluation_of_breakpoint_conditions ())
12526 return;
12527
12528 /* Flag all breakpoint locations with this address and
12529 the same program space as the location
12530 as "its condition has changed". We need to
12531 update the conditions on the target's side. */
12532 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12533 {
12534 loc = *loc2p;
12535
12536 if (!is_breakpoint (loc->owner)
12537 || pspace_num != loc->pspace->num)
12538 continue;
12539
12540 /* Flag the location appropriately. We use a different state to
12541 let everyone know that we already updated the set of locations
12542 with addr bl->address and program space bl->pspace. This is so
12543 we don't have to keep calling these functions just to mark locations
12544 that have already been marked. */
12545 loc->condition_changed = condition_updated;
12546
12547 /* Free the agent expression bytecode as well. We will compute
12548 it later on. */
12549 if (loc->cond_bytecode)
12550 {
12551 free_agent_expr (loc->cond_bytecode);
12552 loc->cond_bytecode = NULL;
12553 }
12554 }
12555}
12556/* Called whether new breakpoints are created, or existing breakpoints
12557 deleted, to update the global location list and recompute which
12558 locations are duplicate of which.
12559
12560 The INSERT_MODE flag determines whether locations may not, may, or
12561 shall be inserted now. See 'enum ugll_insert_mode' for more
12562 info. */
12563
12564static void
12565update_global_location_list (enum ugll_insert_mode insert_mode)
12566{
12567 struct breakpoint *b;
12568 struct bp_location **locp, *loc;
12569 struct cleanup *cleanups;
12570 /* Last breakpoint location address that was marked for update. */
12571 CORE_ADDR last_addr = 0;
12572 /* Last breakpoint location program space that was marked for update. */
12573 int last_pspace_num = -1;
12574
12575 /* Used in the duplicates detection below. When iterating over all
12576 bp_locations, points to the first bp_location of a given address.
12577 Breakpoints and watchpoints of different types are never
12578 duplicates of each other. Keep one pointer for each type of
12579 breakpoint/watchpoint, so we only need to loop over all locations
12580 once. */
12581 struct bp_location *bp_loc_first; /* breakpoint */
12582 struct bp_location *wp_loc_first; /* hardware watchpoint */
12583 struct bp_location *awp_loc_first; /* access watchpoint */
12584 struct bp_location *rwp_loc_first; /* read watchpoint */
12585
12586 /* Saved former bp_location array which we compare against the newly
12587 built bp_location from the current state of ALL_BREAKPOINTS. */
12588 struct bp_location **old_location, **old_locp;
12589 unsigned old_location_count;
12590
12591 old_location = bp_location;
12592 old_location_count = bp_location_count;
12593 bp_location = NULL;
12594 bp_location_count = 0;
12595 cleanups = make_cleanup (xfree, old_location);
12596
12597 ALL_BREAKPOINTS (b)
12598 for (loc = b->loc; loc; loc = loc->next)
12599 bp_location_count++;
12600
12601 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12602 locp = bp_location;
12603 ALL_BREAKPOINTS (b)
12604 for (loc = b->loc; loc; loc = loc->next)
12605 *locp++ = loc;
12606 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12607 bp_location_compare);
12608
12609 bp_location_target_extensions_update ();
12610
12611 /* Identify bp_location instances that are no longer present in the
12612 new list, and therefore should be freed. Note that it's not
12613 necessary that those locations should be removed from inferior --
12614 if there's another location at the same address (previously
12615 marked as duplicate), we don't need to remove/insert the
12616 location.
12617
12618 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12619 and former bp_location array state respectively. */
12620
12621 locp = bp_location;
12622 for (old_locp = old_location; old_locp < old_location + old_location_count;
12623 old_locp++)
12624 {
12625 struct bp_location *old_loc = *old_locp;
12626 struct bp_location **loc2p;
12627
12628 /* Tells if 'old_loc' is found among the new locations. If
12629 not, we have to free it. */
12630 int found_object = 0;
12631 /* Tells if the location should remain inserted in the target. */
12632 int keep_in_target = 0;
12633 int removed = 0;
12634
12635 /* Skip LOCP entries which will definitely never be needed.
12636 Stop either at or being the one matching OLD_LOC. */
12637 while (locp < bp_location + bp_location_count
12638 && (*locp)->address < old_loc->address)
12639 locp++;
12640
12641 for (loc2p = locp;
12642 (loc2p < bp_location + bp_location_count
12643 && (*loc2p)->address == old_loc->address);
12644 loc2p++)
12645 {
12646 /* Check if this is a new/duplicated location or a duplicated
12647 location that had its condition modified. If so, we want to send
12648 its condition to the target if evaluation of conditions is taking
12649 place there. */
12650 if ((*loc2p)->condition_changed == condition_modified
12651 && (last_addr != old_loc->address
12652 || last_pspace_num != old_loc->pspace->num))
12653 {
12654 force_breakpoint_reinsertion (*loc2p);
12655 last_pspace_num = old_loc->pspace->num;
12656 }
12657
12658 if (*loc2p == old_loc)
12659 found_object = 1;
12660 }
12661
12662 /* We have already handled this address, update it so that we don't
12663 have to go through updates again. */
12664 last_addr = old_loc->address;
12665
12666 /* Target-side condition evaluation: Handle deleted locations. */
12667 if (!found_object)
12668 force_breakpoint_reinsertion (old_loc);
12669
12670 /* If this location is no longer present, and inserted, look if
12671 there's maybe a new location at the same address. If so,
12672 mark that one inserted, and don't remove this one. This is
12673 needed so that we don't have a time window where a breakpoint
12674 at certain location is not inserted. */
12675
12676 if (old_loc->inserted)
12677 {
12678 /* If the location is inserted now, we might have to remove
12679 it. */
12680
12681 if (found_object && should_be_inserted (old_loc))
12682 {
12683 /* The location is still present in the location list,
12684 and still should be inserted. Don't do anything. */
12685 keep_in_target = 1;
12686 }
12687 else
12688 {
12689 /* This location still exists, but it won't be kept in the
12690 target since it may have been disabled. We proceed to
12691 remove its target-side condition. */
12692
12693 /* The location is either no longer present, or got
12694 disabled. See if there's another location at the
12695 same address, in which case we don't need to remove
12696 this one from the target. */
12697
12698 /* OLD_LOC comes from existing struct breakpoint. */
12699 if (breakpoint_address_is_meaningful (old_loc->owner))
12700 {
12701 for (loc2p = locp;
12702 (loc2p < bp_location + bp_location_count
12703 && (*loc2p)->address == old_loc->address);
12704 loc2p++)
12705 {
12706 struct bp_location *loc2 = *loc2p;
12707
12708 if (breakpoint_locations_match (loc2, old_loc))
12709 {
12710 /* Read watchpoint locations are switched to
12711 access watchpoints, if the former are not
12712 supported, but the latter are. */
12713 if (is_hardware_watchpoint (old_loc->owner))
12714 {
12715 gdb_assert (is_hardware_watchpoint (loc2->owner));
12716 loc2->watchpoint_type = old_loc->watchpoint_type;
12717 }
12718
12719 /* loc2 is a duplicated location. We need to check
12720 if it should be inserted in case it will be
12721 unduplicated. */
12722 if (loc2 != old_loc
12723 && unduplicated_should_be_inserted (loc2))
12724 {
12725 swap_insertion (old_loc, loc2);
12726 keep_in_target = 1;
12727 break;
12728 }
12729 }
12730 }
12731 }
12732 }
12733
12734 if (!keep_in_target)
12735 {
12736 if (remove_breakpoint (old_loc, mark_uninserted))
12737 {
12738 /* This is just about all we can do. We could keep
12739 this location on the global list, and try to
12740 remove it next time, but there's no particular
12741 reason why we will succeed next time.
12742
12743 Note that at this point, old_loc->owner is still
12744 valid, as delete_breakpoint frees the breakpoint
12745 only after calling us. */
12746 printf_filtered (_("warning: Error removing "
12747 "breakpoint %d\n"),
12748 old_loc->owner->number);
12749 }
12750 removed = 1;
12751 }
12752 }
12753
12754 if (!found_object)
12755 {
12756 if (removed && non_stop
12757 && breakpoint_address_is_meaningful (old_loc->owner)
12758 && !is_hardware_watchpoint (old_loc->owner))
12759 {
12760 /* This location was removed from the target. In
12761 non-stop mode, a race condition is possible where
12762 we've removed a breakpoint, but stop events for that
12763 breakpoint are already queued and will arrive later.
12764 We apply an heuristic to be able to distinguish such
12765 SIGTRAPs from other random SIGTRAPs: we keep this
12766 breakpoint location for a bit, and will retire it
12767 after we see some number of events. The theory here
12768 is that reporting of events should, "on the average",
12769 be fair, so after a while we'll see events from all
12770 threads that have anything of interest, and no longer
12771 need to keep this breakpoint location around. We
12772 don't hold locations forever so to reduce chances of
12773 mistaking a non-breakpoint SIGTRAP for a breakpoint
12774 SIGTRAP.
12775
12776 The heuristic failing can be disastrous on
12777 decr_pc_after_break targets.
12778
12779 On decr_pc_after_break targets, like e.g., x86-linux,
12780 if we fail to recognize a late breakpoint SIGTRAP,
12781 because events_till_retirement has reached 0 too
12782 soon, we'll fail to do the PC adjustment, and report
12783 a random SIGTRAP to the user. When the user resumes
12784 the inferior, it will most likely immediately crash
12785 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12786 corrupted, because of being resumed e.g., in the
12787 middle of a multi-byte instruction, or skipped a
12788 one-byte instruction. This was actually seen happen
12789 on native x86-linux, and should be less rare on
12790 targets that do not support new thread events, like
12791 remote, due to the heuristic depending on
12792 thread_count.
12793
12794 Mistaking a random SIGTRAP for a breakpoint trap
12795 causes similar symptoms (PC adjustment applied when
12796 it shouldn't), but then again, playing with SIGTRAPs
12797 behind the debugger's back is asking for trouble.
12798
12799 Since hardware watchpoint traps are always
12800 distinguishable from other traps, so we don't need to
12801 apply keep hardware watchpoint moribund locations
12802 around. We simply always ignore hardware watchpoint
12803 traps we can no longer explain. */
12804
12805 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12806 old_loc->owner = NULL;
12807
12808 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12809 }
12810 else
12811 {
12812 old_loc->owner = NULL;
12813 decref_bp_location (&old_loc);
12814 }
12815 }
12816 }
12817
12818 /* Rescan breakpoints at the same address and section, marking the
12819 first one as "first" and any others as "duplicates". This is so
12820 that the bpt instruction is only inserted once. If we have a
12821 permanent breakpoint at the same place as BPT, make that one the
12822 official one, and the rest as duplicates. Permanent breakpoints
12823 are sorted first for the same address.
12824
12825 Do the same for hardware watchpoints, but also considering the
12826 watchpoint's type (regular/access/read) and length. */
12827
12828 bp_loc_first = NULL;
12829 wp_loc_first = NULL;
12830 awp_loc_first = NULL;
12831 rwp_loc_first = NULL;
12832 ALL_BP_LOCATIONS (loc, locp)
12833 {
12834 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12835 non-NULL. */
12836 struct bp_location **loc_first_p;
12837 b = loc->owner;
12838
12839 if (!unduplicated_should_be_inserted (loc)
12840 || !breakpoint_address_is_meaningful (b)
12841 /* Don't detect duplicate for tracepoint locations because they are
12842 never duplicated. See the comments in field `duplicate' of
12843 `struct bp_location'. */
12844 || is_tracepoint (b))
12845 {
12846 /* Clear the condition modification flag. */
12847 loc->condition_changed = condition_unchanged;
12848 continue;
12849 }
12850
12851 /* Permanent breakpoint should always be inserted. */
12852 if (b->enable_state == bp_permanent && ! loc->inserted)
12853 internal_error (__FILE__, __LINE__,
12854 _("allegedly permanent breakpoint is not "
12855 "actually inserted"));
12856
12857 if (b->type == bp_hardware_watchpoint)
12858 loc_first_p = &wp_loc_first;
12859 else if (b->type == bp_read_watchpoint)
12860 loc_first_p = &rwp_loc_first;
12861 else if (b->type == bp_access_watchpoint)
12862 loc_first_p = &awp_loc_first;
12863 else
12864 loc_first_p = &bp_loc_first;
12865
12866 if (*loc_first_p == NULL
12867 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12868 || !breakpoint_locations_match (loc, *loc_first_p))
12869 {
12870 *loc_first_p = loc;
12871 loc->duplicate = 0;
12872
12873 if (is_breakpoint (loc->owner) && loc->condition_changed)
12874 {
12875 loc->needs_update = 1;
12876 /* Clear the condition modification flag. */
12877 loc->condition_changed = condition_unchanged;
12878 }
12879 continue;
12880 }
12881
12882
12883 /* This and the above ensure the invariant that the first location
12884 is not duplicated, and is the inserted one.
12885 All following are marked as duplicated, and are not inserted. */
12886 if (loc->inserted)
12887 swap_insertion (loc, *loc_first_p);
12888 loc->duplicate = 1;
12889
12890 /* Clear the condition modification flag. */
12891 loc->condition_changed = condition_unchanged;
12892
12893 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12894 && b->enable_state != bp_permanent)
12895 internal_error (__FILE__, __LINE__,
12896 _("another breakpoint was inserted on top of "
12897 "a permanent breakpoint"));
12898 }
12899
12900 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12901 {
12902 if (insert_mode != UGLL_DONT_INSERT)
12903 insert_breakpoint_locations ();
12904 else
12905 {
12906 /* Even though the caller told us to not insert new
12907 locations, we may still need to update conditions on the
12908 target's side of breakpoints that were already inserted
12909 if the target is evaluating breakpoint conditions. We
12910 only update conditions for locations that are marked
12911 "needs_update". */
12912 update_inserted_breakpoint_locations ();
12913 }
12914 }
12915
12916 if (insert_mode != UGLL_DONT_INSERT)
12917 download_tracepoint_locations ();
12918
12919 do_cleanups (cleanups);
12920}
12921
12922void
12923breakpoint_retire_moribund (void)
12924{
12925 struct bp_location *loc;
12926 int ix;
12927
12928 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12929 if (--(loc->events_till_retirement) == 0)
12930 {
12931 decref_bp_location (&loc);
12932 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12933 --ix;
12934 }
12935}
12936
12937static void
12938update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12939{
12940 volatile struct gdb_exception e;
12941
12942 TRY_CATCH (e, RETURN_MASK_ERROR)
12943 update_global_location_list (insert_mode);
12944}
12945
12946/* Clear BKP from a BPS. */
12947
12948static void
12949bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12950{
12951 bpstat bs;
12952
12953 for (bs = bps; bs; bs = bs->next)
12954 if (bs->breakpoint_at == bpt)
12955 {
12956 bs->breakpoint_at = NULL;
12957 bs->old_val = NULL;
12958 /* bs->commands will be freed later. */
12959 }
12960}
12961
12962/* Callback for iterate_over_threads. */
12963static int
12964bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12965{
12966 struct breakpoint *bpt = data;
12967
12968 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12969 return 0;
12970}
12971
12972/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12973 callbacks. */
12974
12975static void
12976say_where (struct breakpoint *b)
12977{
12978 struct value_print_options opts;
12979
12980 get_user_print_options (&opts);
12981
12982 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12983 single string. */
12984 if (b->loc == NULL)
12985 {
12986 printf_filtered (_(" (%s) pending."), b->addr_string);
12987 }
12988 else
12989 {
12990 if (opts.addressprint || b->loc->symtab == NULL)
12991 {
12992 printf_filtered (" at ");
12993 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12994 gdb_stdout);
12995 }
12996 if (b->loc->symtab != NULL)
12997 {
12998 /* If there is a single location, we can print the location
12999 more nicely. */
13000 if (b->loc->next == NULL)
13001 printf_filtered (": file %s, line %d.",
13002 symtab_to_filename_for_display (b->loc->symtab),
13003 b->loc->line_number);
13004 else
13005 /* This is not ideal, but each location may have a
13006 different file name, and this at least reflects the
13007 real situation somewhat. */
13008 printf_filtered (": %s.", b->addr_string);
13009 }
13010
13011 if (b->loc->next)
13012 {
13013 struct bp_location *loc = b->loc;
13014 int n = 0;
13015 for (; loc; loc = loc->next)
13016 ++n;
13017 printf_filtered (" (%d locations)", n);
13018 }
13019 }
13020}
13021
13022/* Default bp_location_ops methods. */
13023
13024static void
13025bp_location_dtor (struct bp_location *self)
13026{
13027 xfree (self->cond);
13028 if (self->cond_bytecode)
13029 free_agent_expr (self->cond_bytecode);
13030 xfree (self->function_name);
13031
13032 VEC_free (agent_expr_p, self->target_info.conditions);
13033 VEC_free (agent_expr_p, self->target_info.tcommands);
13034}
13035
13036static const struct bp_location_ops bp_location_ops =
13037{
13038 bp_location_dtor
13039};
13040
13041/* Default breakpoint_ops methods all breakpoint_ops ultimately
13042 inherit from. */
13043
13044static void
13045base_breakpoint_dtor (struct breakpoint *self)
13046{
13047 decref_counted_command_line (&self->commands);
13048 xfree (self->cond_string);
13049 xfree (self->extra_string);
13050 xfree (self->addr_string);
13051 xfree (self->filter);
13052 xfree (self->addr_string_range_end);
13053}
13054
13055static struct bp_location *
13056base_breakpoint_allocate_location (struct breakpoint *self)
13057{
13058 struct bp_location *loc;
13059
13060 loc = XNEW (struct bp_location);
13061 init_bp_location (loc, &bp_location_ops, self);
13062 return loc;
13063}
13064
13065static void
13066base_breakpoint_re_set (struct breakpoint *b)
13067{
13068 /* Nothing to re-set. */
13069}
13070
13071#define internal_error_pure_virtual_called() \
13072 gdb_assert_not_reached ("pure virtual function called")
13073
13074static int
13075base_breakpoint_insert_location (struct bp_location *bl)
13076{
13077 internal_error_pure_virtual_called ();
13078}
13079
13080static int
13081base_breakpoint_remove_location (struct bp_location *bl)
13082{
13083 internal_error_pure_virtual_called ();
13084}
13085
13086static int
13087base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13088 struct address_space *aspace,
13089 CORE_ADDR bp_addr,
13090 const struct target_waitstatus *ws)
13091{
13092 internal_error_pure_virtual_called ();
13093}
13094
13095static void
13096base_breakpoint_check_status (bpstat bs)
13097{
13098 /* Always stop. */
13099}
13100
13101/* A "works_in_software_mode" breakpoint_ops method that just internal
13102 errors. */
13103
13104static int
13105base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13106{
13107 internal_error_pure_virtual_called ();
13108}
13109
13110/* A "resources_needed" breakpoint_ops method that just internal
13111 errors. */
13112
13113static int
13114base_breakpoint_resources_needed (const struct bp_location *bl)
13115{
13116 internal_error_pure_virtual_called ();
13117}
13118
13119static enum print_stop_action
13120base_breakpoint_print_it (bpstat bs)
13121{
13122 internal_error_pure_virtual_called ();
13123}
13124
13125static void
13126base_breakpoint_print_one_detail (const struct breakpoint *self,
13127 struct ui_out *uiout)
13128{
13129 /* nothing */
13130}
13131
13132static void
13133base_breakpoint_print_mention (struct breakpoint *b)
13134{
13135 internal_error_pure_virtual_called ();
13136}
13137
13138static void
13139base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13140{
13141 internal_error_pure_virtual_called ();
13142}
13143
13144static void
13145base_breakpoint_create_sals_from_address (char **arg,
13146 struct linespec_result *canonical,
13147 enum bptype type_wanted,
13148 char *addr_start,
13149 char **copy_arg)
13150{
13151 internal_error_pure_virtual_called ();
13152}
13153
13154static void
13155base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13156 struct linespec_result *c,
13157 char *cond_string,
13158 char *extra_string,
13159 enum bptype type_wanted,
13160 enum bpdisp disposition,
13161 int thread,
13162 int task, int ignore_count,
13163 const struct breakpoint_ops *o,
13164 int from_tty, int enabled,
13165 int internal, unsigned flags)
13166{
13167 internal_error_pure_virtual_called ();
13168}
13169
13170static void
13171base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13172 struct symtabs_and_lines *sals)
13173{
13174 internal_error_pure_virtual_called ();
13175}
13176
13177/* The default 'explains_signal' method. */
13178
13179static int
13180base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13181{
13182 return 1;
13183}
13184
13185/* The default "after_condition_true" method. */
13186
13187static void
13188base_breakpoint_after_condition_true (struct bpstats *bs)
13189{
13190 /* Nothing to do. */
13191}
13192
13193struct breakpoint_ops base_breakpoint_ops =
13194{
13195 base_breakpoint_dtor,
13196 base_breakpoint_allocate_location,
13197 base_breakpoint_re_set,
13198 base_breakpoint_insert_location,
13199 base_breakpoint_remove_location,
13200 base_breakpoint_breakpoint_hit,
13201 base_breakpoint_check_status,
13202 base_breakpoint_resources_needed,
13203 base_breakpoint_works_in_software_mode,
13204 base_breakpoint_print_it,
13205 NULL,
13206 base_breakpoint_print_one_detail,
13207 base_breakpoint_print_mention,
13208 base_breakpoint_print_recreate,
13209 base_breakpoint_create_sals_from_address,
13210 base_breakpoint_create_breakpoints_sal,
13211 base_breakpoint_decode_linespec,
13212 base_breakpoint_explains_signal,
13213 base_breakpoint_after_condition_true,
13214};
13215
13216/* Default breakpoint_ops methods. */
13217
13218static void
13219bkpt_re_set (struct breakpoint *b)
13220{
13221 /* FIXME: is this still reachable? */
13222 if (b->addr_string == NULL)
13223 {
13224 /* Anything without a string can't be re-set. */
13225 delete_breakpoint (b);
13226 return;
13227 }
13228
13229 breakpoint_re_set_default (b);
13230}
13231
13232static int
13233bkpt_insert_location (struct bp_location *bl)
13234{
13235 if (bl->loc_type == bp_loc_hardware_breakpoint)
13236 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13237 else
13238 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13239}
13240
13241static int
13242bkpt_remove_location (struct bp_location *bl)
13243{
13244 if (bl->loc_type == bp_loc_hardware_breakpoint)
13245 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13246 else
13247 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13248}
13249
13250static int
13251bkpt_breakpoint_hit (const struct bp_location *bl,
13252 struct address_space *aspace, CORE_ADDR bp_addr,
13253 const struct target_waitstatus *ws)
13254{
13255 if (ws->kind != TARGET_WAITKIND_STOPPED
13256 || ws->value.sig != GDB_SIGNAL_TRAP)
13257 return 0;
13258
13259 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13260 aspace, bp_addr))
13261 return 0;
13262
13263 if (overlay_debugging /* unmapped overlay section */
13264 && section_is_overlay (bl->section)
13265 && !section_is_mapped (bl->section))
13266 return 0;
13267
13268 return 1;
13269}
13270
13271static int
13272dprintf_breakpoint_hit (const struct bp_location *bl,
13273 struct address_space *aspace, CORE_ADDR bp_addr,
13274 const struct target_waitstatus *ws)
13275{
13276 if (dprintf_style == dprintf_style_agent
13277 && target_can_run_breakpoint_commands ())
13278 {
13279 /* An agent-style dprintf never causes a stop. If we see a trap
13280 for this address it must be for a breakpoint that happens to
13281 be set at the same address. */
13282 return 0;
13283 }
13284
13285 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13286}
13287
13288static int
13289bkpt_resources_needed (const struct bp_location *bl)
13290{
13291 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13292
13293 return 1;
13294}
13295
13296static enum print_stop_action
13297bkpt_print_it (bpstat bs)
13298{
13299 struct breakpoint *b;
13300 const struct bp_location *bl;
13301 int bp_temp;
13302 struct ui_out *uiout = current_uiout;
13303
13304 gdb_assert (bs->bp_location_at != NULL);
13305
13306 bl = bs->bp_location_at;
13307 b = bs->breakpoint_at;
13308
13309 bp_temp = b->disposition == disp_del;
13310 if (bl->address != bl->requested_address)
13311 breakpoint_adjustment_warning (bl->requested_address,
13312 bl->address,
13313 b->number, 1);
13314 annotate_breakpoint (b->number);
13315 if (bp_temp)
13316 ui_out_text (uiout, "\nTemporary breakpoint ");
13317 else
13318 ui_out_text (uiout, "\nBreakpoint ");
13319 if (ui_out_is_mi_like_p (uiout))
13320 {
13321 ui_out_field_string (uiout, "reason",
13322 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13323 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13324 }
13325 ui_out_field_int (uiout, "bkptno", b->number);
13326 ui_out_text (uiout, ", ");
13327
13328 return PRINT_SRC_AND_LOC;
13329}
13330
13331static void
13332bkpt_print_mention (struct breakpoint *b)
13333{
13334 if (ui_out_is_mi_like_p (current_uiout))
13335 return;
13336
13337 switch (b->type)
13338 {
13339 case bp_breakpoint:
13340 case bp_gnu_ifunc_resolver:
13341 if (b->disposition == disp_del)
13342 printf_filtered (_("Temporary breakpoint"));
13343 else
13344 printf_filtered (_("Breakpoint"));
13345 printf_filtered (_(" %d"), b->number);
13346 if (b->type == bp_gnu_ifunc_resolver)
13347 printf_filtered (_(" at gnu-indirect-function resolver"));
13348 break;
13349 case bp_hardware_breakpoint:
13350 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13351 break;
13352 case bp_dprintf:
13353 printf_filtered (_("Dprintf %d"), b->number);
13354 break;
13355 }
13356
13357 say_where (b);
13358}
13359
13360static void
13361bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13362{
13363 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13364 fprintf_unfiltered (fp, "tbreak");
13365 else if (tp->type == bp_breakpoint)
13366 fprintf_unfiltered (fp, "break");
13367 else if (tp->type == bp_hardware_breakpoint
13368 && tp->disposition == disp_del)
13369 fprintf_unfiltered (fp, "thbreak");
13370 else if (tp->type == bp_hardware_breakpoint)
13371 fprintf_unfiltered (fp, "hbreak");
13372 else
13373 internal_error (__FILE__, __LINE__,
13374 _("unhandled breakpoint type %d"), (int) tp->type);
13375
13376 fprintf_unfiltered (fp, " %s", tp->addr_string);
13377 print_recreate_thread (tp, fp);
13378}
13379
13380static void
13381bkpt_create_sals_from_address (char **arg,
13382 struct linespec_result *canonical,
13383 enum bptype type_wanted,
13384 char *addr_start, char **copy_arg)
13385{
13386 create_sals_from_address_default (arg, canonical, type_wanted,
13387 addr_start, copy_arg);
13388}
13389
13390static void
13391bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13392 struct linespec_result *canonical,
13393 char *cond_string,
13394 char *extra_string,
13395 enum bptype type_wanted,
13396 enum bpdisp disposition,
13397 int thread,
13398 int task, int ignore_count,
13399 const struct breakpoint_ops *ops,
13400 int from_tty, int enabled,
13401 int internal, unsigned flags)
13402{
13403 create_breakpoints_sal_default (gdbarch, canonical,
13404 cond_string, extra_string,
13405 type_wanted,
13406 disposition, thread, task,
13407 ignore_count, ops, from_tty,
13408 enabled, internal, flags);
13409}
13410
13411static void
13412bkpt_decode_linespec (struct breakpoint *b, char **s,
13413 struct symtabs_and_lines *sals)
13414{
13415 decode_linespec_default (b, s, sals);
13416}
13417
13418/* Virtual table for internal breakpoints. */
13419
13420static void
13421internal_bkpt_re_set (struct breakpoint *b)
13422{
13423 switch (b->type)
13424 {
13425 /* Delete overlay event and longjmp master breakpoints; they
13426 will be reset later by breakpoint_re_set. */
13427 case bp_overlay_event:
13428 case bp_longjmp_master:
13429 case bp_std_terminate_master:
13430 case bp_exception_master:
13431 delete_breakpoint (b);
13432 break;
13433
13434 /* This breakpoint is special, it's set up when the inferior
13435 starts and we really don't want to touch it. */
13436 case bp_shlib_event:
13437
13438 /* Like bp_shlib_event, this breakpoint type is special. Once
13439 it is set up, we do not want to touch it. */
13440 case bp_thread_event:
13441 break;
13442 }
13443}
13444
13445static void
13446internal_bkpt_check_status (bpstat bs)
13447{
13448 if (bs->breakpoint_at->type == bp_shlib_event)
13449 {
13450 /* If requested, stop when the dynamic linker notifies GDB of
13451 events. This allows the user to get control and place
13452 breakpoints in initializer routines for dynamically loaded
13453 objects (among other things). */
13454 bs->stop = stop_on_solib_events;
13455 bs->print = stop_on_solib_events;
13456 }
13457 else
13458 bs->stop = 0;
13459}
13460
13461static enum print_stop_action
13462internal_bkpt_print_it (bpstat bs)
13463{
13464 struct breakpoint *b;
13465
13466 b = bs->breakpoint_at;
13467
13468 switch (b->type)
13469 {
13470 case bp_shlib_event:
13471 /* Did we stop because the user set the stop_on_solib_events
13472 variable? (If so, we report this as a generic, "Stopped due
13473 to shlib event" message.) */
13474 print_solib_event (0);
13475 break;
13476
13477 case bp_thread_event:
13478 /* Not sure how we will get here.
13479 GDB should not stop for these breakpoints. */
13480 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13481 break;
13482
13483 case bp_overlay_event:
13484 /* By analogy with the thread event, GDB should not stop for these. */
13485 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13486 break;
13487
13488 case bp_longjmp_master:
13489 /* These should never be enabled. */
13490 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13491 break;
13492
13493 case bp_std_terminate_master:
13494 /* These should never be enabled. */
13495 printf_filtered (_("std::terminate Master Breakpoint: "
13496 "gdb should not stop!\n"));
13497 break;
13498
13499 case bp_exception_master:
13500 /* These should never be enabled. */
13501 printf_filtered (_("Exception Master Breakpoint: "
13502 "gdb should not stop!\n"));
13503 break;
13504 }
13505
13506 return PRINT_NOTHING;
13507}
13508
13509static void
13510internal_bkpt_print_mention (struct breakpoint *b)
13511{
13512 /* Nothing to mention. These breakpoints are internal. */
13513}
13514
13515/* Virtual table for momentary breakpoints */
13516
13517static void
13518momentary_bkpt_re_set (struct breakpoint *b)
13519{
13520 /* Keep temporary breakpoints, which can be encountered when we step
13521 over a dlopen call and solib_add is resetting the breakpoints.
13522 Otherwise these should have been blown away via the cleanup chain
13523 or by breakpoint_init_inferior when we rerun the executable. */
13524}
13525
13526static void
13527momentary_bkpt_check_status (bpstat bs)
13528{
13529 /* Nothing. The point of these breakpoints is causing a stop. */
13530}
13531
13532static enum print_stop_action
13533momentary_bkpt_print_it (bpstat bs)
13534{
13535 struct ui_out *uiout = current_uiout;
13536
13537 if (ui_out_is_mi_like_p (uiout))
13538 {
13539 struct breakpoint *b = bs->breakpoint_at;
13540
13541 switch (b->type)
13542 {
13543 case bp_finish:
13544 ui_out_field_string
13545 (uiout, "reason",
13546 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13547 break;
13548
13549 case bp_until:
13550 ui_out_field_string
13551 (uiout, "reason",
13552 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13553 break;
13554 }
13555 }
13556
13557 return PRINT_UNKNOWN;
13558}
13559
13560static void
13561momentary_bkpt_print_mention (struct breakpoint *b)
13562{
13563 /* Nothing to mention. These breakpoints are internal. */
13564}
13565
13566/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13567
13568 It gets cleared already on the removal of the first one of such placed
13569 breakpoints. This is OK as they get all removed altogether. */
13570
13571static void
13572longjmp_bkpt_dtor (struct breakpoint *self)
13573{
13574 struct thread_info *tp = find_thread_id (self->thread);
13575
13576 if (tp)
13577 tp->initiating_frame = null_frame_id;
13578
13579 momentary_breakpoint_ops.dtor (self);
13580}
13581
13582/* Specific methods for probe breakpoints. */
13583
13584static int
13585bkpt_probe_insert_location (struct bp_location *bl)
13586{
13587 int v = bkpt_insert_location (bl);
13588
13589 if (v == 0)
13590 {
13591 /* The insertion was successful, now let's set the probe's semaphore
13592 if needed. */
13593 if (bl->probe.probe->pops->set_semaphore != NULL)
13594 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13595 bl->probe.objfile,
13596 bl->gdbarch);
13597 }
13598
13599 return v;
13600}
13601
13602static int
13603bkpt_probe_remove_location (struct bp_location *bl)
13604{
13605 /* Let's clear the semaphore before removing the location. */
13606 if (bl->probe.probe->pops->clear_semaphore != NULL)
13607 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13608 bl->probe.objfile,
13609 bl->gdbarch);
13610
13611 return bkpt_remove_location (bl);
13612}
13613
13614static void
13615bkpt_probe_create_sals_from_address (char **arg,
13616 struct linespec_result *canonical,
13617 enum bptype type_wanted,
13618 char *addr_start, char **copy_arg)
13619{
13620 struct linespec_sals lsal;
13621
13622 lsal.sals = parse_probes (arg, canonical);
13623
13624 *copy_arg = xstrdup (canonical->addr_string);
13625 lsal.canonical = xstrdup (*copy_arg);
13626
13627 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13628}
13629
13630static void
13631bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13632 struct symtabs_and_lines *sals)
13633{
13634 *sals = parse_probes (s, NULL);
13635 if (!sals->sals)
13636 error (_("probe not found"));
13637}
13638
13639/* The breakpoint_ops structure to be used in tracepoints. */
13640
13641static void
13642tracepoint_re_set (struct breakpoint *b)
13643{
13644 breakpoint_re_set_default (b);
13645}
13646
13647static int
13648tracepoint_breakpoint_hit (const struct bp_location *bl,
13649 struct address_space *aspace, CORE_ADDR bp_addr,
13650 const struct target_waitstatus *ws)
13651{
13652 /* By definition, the inferior does not report stops at
13653 tracepoints. */
13654 return 0;
13655}
13656
13657static void
13658tracepoint_print_one_detail (const struct breakpoint *self,
13659 struct ui_out *uiout)
13660{
13661 struct tracepoint *tp = (struct tracepoint *) self;
13662 if (tp->static_trace_marker_id)
13663 {
13664 gdb_assert (self->type == bp_static_tracepoint);
13665
13666 ui_out_text (uiout, "\tmarker id is ");
13667 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13668 tp->static_trace_marker_id);
13669 ui_out_text (uiout, "\n");
13670 }
13671}
13672
13673static void
13674tracepoint_print_mention (struct breakpoint *b)
13675{
13676 if (ui_out_is_mi_like_p (current_uiout))
13677 return;
13678
13679 switch (b->type)
13680 {
13681 case bp_tracepoint:
13682 printf_filtered (_("Tracepoint"));
13683 printf_filtered (_(" %d"), b->number);
13684 break;
13685 case bp_fast_tracepoint:
13686 printf_filtered (_("Fast tracepoint"));
13687 printf_filtered (_(" %d"), b->number);
13688 break;
13689 case bp_static_tracepoint:
13690 printf_filtered (_("Static tracepoint"));
13691 printf_filtered (_(" %d"), b->number);
13692 break;
13693 default:
13694 internal_error (__FILE__, __LINE__,
13695 _("unhandled tracepoint type %d"), (int) b->type);
13696 }
13697
13698 say_where (b);
13699}
13700
13701static void
13702tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13703{
13704 struct tracepoint *tp = (struct tracepoint *) self;
13705
13706 if (self->type == bp_fast_tracepoint)
13707 fprintf_unfiltered (fp, "ftrace");
13708 if (self->type == bp_static_tracepoint)
13709 fprintf_unfiltered (fp, "strace");
13710 else if (self->type == bp_tracepoint)
13711 fprintf_unfiltered (fp, "trace");
13712 else
13713 internal_error (__FILE__, __LINE__,
13714 _("unhandled tracepoint type %d"), (int) self->type);
13715
13716 fprintf_unfiltered (fp, " %s", self->addr_string);
13717 print_recreate_thread (self, fp);
13718
13719 if (tp->pass_count)
13720 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13721}
13722
13723static void
13724tracepoint_create_sals_from_address (char **arg,
13725 struct linespec_result *canonical,
13726 enum bptype type_wanted,
13727 char *addr_start, char **copy_arg)
13728{
13729 create_sals_from_address_default (arg, canonical, type_wanted,
13730 addr_start, copy_arg);
13731}
13732
13733static void
13734tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13735 struct linespec_result *canonical,
13736 char *cond_string,
13737 char *extra_string,
13738 enum bptype type_wanted,
13739 enum bpdisp disposition,
13740 int thread,
13741 int task, int ignore_count,
13742 const struct breakpoint_ops *ops,
13743 int from_tty, int enabled,
13744 int internal, unsigned flags)
13745{
13746 create_breakpoints_sal_default (gdbarch, canonical,
13747 cond_string, extra_string,
13748 type_wanted,
13749 disposition, thread, task,
13750 ignore_count, ops, from_tty,
13751 enabled, internal, flags);
13752}
13753
13754static void
13755tracepoint_decode_linespec (struct breakpoint *b, char **s,
13756 struct symtabs_and_lines *sals)
13757{
13758 decode_linespec_default (b, s, sals);
13759}
13760
13761struct breakpoint_ops tracepoint_breakpoint_ops;
13762
13763/* The breakpoint_ops structure to be use on tracepoints placed in a
13764 static probe. */
13765
13766static void
13767tracepoint_probe_create_sals_from_address (char **arg,
13768 struct linespec_result *canonical,
13769 enum bptype type_wanted,
13770 char *addr_start, char **copy_arg)
13771{
13772 /* We use the same method for breakpoint on probes. */
13773 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13774 addr_start, copy_arg);
13775}
13776
13777static void
13778tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13779 struct symtabs_and_lines *sals)
13780{
13781 /* We use the same method for breakpoint on probes. */
13782 bkpt_probe_decode_linespec (b, s, sals);
13783}
13784
13785static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13786
13787/* Dprintf breakpoint_ops methods. */
13788
13789static void
13790dprintf_re_set (struct breakpoint *b)
13791{
13792 breakpoint_re_set_default (b);
13793
13794 /* This breakpoint could have been pending, and be resolved now, and
13795 if so, we should now have the extra string. If we don't, the
13796 dprintf was malformed when created, but we couldn't tell because
13797 we can't extract the extra string until the location is
13798 resolved. */
13799 if (b->loc != NULL && b->extra_string == NULL)
13800 error (_("Format string required"));
13801
13802 /* 1 - connect to target 1, that can run breakpoint commands.
13803 2 - create a dprintf, which resolves fine.
13804 3 - disconnect from target 1
13805 4 - connect to target 2, that can NOT run breakpoint commands.
13806
13807 After steps #3/#4, you'll want the dprintf command list to
13808 be updated, because target 1 and 2 may well return different
13809 answers for target_can_run_breakpoint_commands().
13810 Given absence of finer grained resetting, we get to do
13811 it all the time. */
13812 if (b->extra_string != NULL)
13813 update_dprintf_command_list (b);
13814}
13815
13816/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13817
13818static void
13819dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13820{
13821 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13822 tp->extra_string);
13823 print_recreate_thread (tp, fp);
13824}
13825
13826/* Implement the "after_condition_true" breakpoint_ops method for
13827 dprintf.
13828
13829 dprintf's are implemented with regular commands in their command
13830 list, but we run the commands here instead of before presenting the
13831 stop to the user, as dprintf's don't actually cause a stop. This
13832 also makes it so that the commands of multiple dprintfs at the same
13833 address are all handled. */
13834
13835static void
13836dprintf_after_condition_true (struct bpstats *bs)
13837{
13838 struct cleanup *old_chain;
13839 struct bpstats tmp_bs = { NULL };
13840 struct bpstats *tmp_bs_p = &tmp_bs;
13841
13842 /* dprintf's never cause a stop. This wasn't set in the
13843 check_status hook instead because that would make the dprintf's
13844 condition not be evaluated. */
13845 bs->stop = 0;
13846
13847 /* Run the command list here. Take ownership of it instead of
13848 copying. We never want these commands to run later in
13849 bpstat_do_actions, if a breakpoint that causes a stop happens to
13850 be set at same address as this dprintf, or even if running the
13851 commands here throws. */
13852 tmp_bs.commands = bs->commands;
13853 bs->commands = NULL;
13854 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13855
13856 bpstat_do_actions_1 (&tmp_bs_p);
13857
13858 /* 'tmp_bs.commands' will usually be NULL by now, but
13859 bpstat_do_actions_1 may return early without processing the whole
13860 list. */
13861 do_cleanups (old_chain);
13862}
13863
13864/* The breakpoint_ops structure to be used on static tracepoints with
13865 markers (`-m'). */
13866
13867static void
13868strace_marker_create_sals_from_address (char **arg,
13869 struct linespec_result *canonical,
13870 enum bptype type_wanted,
13871 char *addr_start, char **copy_arg)
13872{
13873 struct linespec_sals lsal;
13874
13875 lsal.sals = decode_static_tracepoint_spec (arg);
13876
13877 *copy_arg = savestring (addr_start, *arg - addr_start);
13878
13879 canonical->addr_string = xstrdup (*copy_arg);
13880 lsal.canonical = xstrdup (*copy_arg);
13881 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13882}
13883
13884static void
13885strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13886 struct linespec_result *canonical,
13887 char *cond_string,
13888 char *extra_string,
13889 enum bptype type_wanted,
13890 enum bpdisp disposition,
13891 int thread,
13892 int task, int ignore_count,
13893 const struct breakpoint_ops *ops,
13894 int from_tty, int enabled,
13895 int internal, unsigned flags)
13896{
13897 int i;
13898 struct linespec_sals *lsal = VEC_index (linespec_sals,
13899 canonical->sals, 0);
13900
13901 /* If the user is creating a static tracepoint by marker id
13902 (strace -m MARKER_ID), then store the sals index, so that
13903 breakpoint_re_set can try to match up which of the newly
13904 found markers corresponds to this one, and, don't try to
13905 expand multiple locations for each sal, given than SALS
13906 already should contain all sals for MARKER_ID. */
13907
13908 for (i = 0; i < lsal->sals.nelts; ++i)
13909 {
13910 struct symtabs_and_lines expanded;
13911 struct tracepoint *tp;
13912 struct cleanup *old_chain;
13913 char *addr_string;
13914
13915 expanded.nelts = 1;
13916 expanded.sals = &lsal->sals.sals[i];
13917
13918 addr_string = xstrdup (canonical->addr_string);
13919 old_chain = make_cleanup (xfree, addr_string);
13920
13921 tp = XCNEW (struct tracepoint);
13922 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13923 addr_string, NULL,
13924 cond_string, extra_string,
13925 type_wanted, disposition,
13926 thread, task, ignore_count, ops,
13927 from_tty, enabled, internal, flags,
13928 canonical->special_display);
13929 /* Given that its possible to have multiple markers with
13930 the same string id, if the user is creating a static
13931 tracepoint by marker id ("strace -m MARKER_ID"), then
13932 store the sals index, so that breakpoint_re_set can
13933 try to match up which of the newly found markers
13934 corresponds to this one */
13935 tp->static_trace_marker_id_idx = i;
13936
13937 install_breakpoint (internal, &tp->base, 0);
13938
13939 discard_cleanups (old_chain);
13940 }
13941}
13942
13943static void
13944strace_marker_decode_linespec (struct breakpoint *b, char **s,
13945 struct symtabs_and_lines *sals)
13946{
13947 struct tracepoint *tp = (struct tracepoint *) b;
13948
13949 *sals = decode_static_tracepoint_spec (s);
13950 if (sals->nelts > tp->static_trace_marker_id_idx)
13951 {
13952 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13953 sals->nelts = 1;
13954 }
13955 else
13956 error (_("marker %s not found"), tp->static_trace_marker_id);
13957}
13958
13959static struct breakpoint_ops strace_marker_breakpoint_ops;
13960
13961static int
13962strace_marker_p (struct breakpoint *b)
13963{
13964 return b->ops == &strace_marker_breakpoint_ops;
13965}
13966
13967/* Delete a breakpoint and clean up all traces of it in the data
13968 structures. */
13969
13970void
13971delete_breakpoint (struct breakpoint *bpt)
13972{
13973 struct breakpoint *b;
13974
13975 gdb_assert (bpt != NULL);
13976
13977 /* Has this bp already been deleted? This can happen because
13978 multiple lists can hold pointers to bp's. bpstat lists are
13979 especial culprits.
13980
13981 One example of this happening is a watchpoint's scope bp. When
13982 the scope bp triggers, we notice that the watchpoint is out of
13983 scope, and delete it. We also delete its scope bp. But the
13984 scope bp is marked "auto-deleting", and is already on a bpstat.
13985 That bpstat is then checked for auto-deleting bp's, which are
13986 deleted.
13987
13988 A real solution to this problem might involve reference counts in
13989 bp's, and/or giving them pointers back to their referencing
13990 bpstat's, and teaching delete_breakpoint to only free a bp's
13991 storage when no more references were extent. A cheaper bandaid
13992 was chosen. */
13993 if (bpt->type == bp_none)
13994 return;
13995
13996 /* At least avoid this stale reference until the reference counting
13997 of breakpoints gets resolved. */
13998 if (bpt->related_breakpoint != bpt)
13999 {
14000 struct breakpoint *related;
14001 struct watchpoint *w;
14002
14003 if (bpt->type == bp_watchpoint_scope)
14004 w = (struct watchpoint *) bpt->related_breakpoint;
14005 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
14006 w = (struct watchpoint *) bpt;
14007 else
14008 w = NULL;
14009 if (w != NULL)
14010 watchpoint_del_at_next_stop (w);
14011
14012 /* Unlink bpt from the bpt->related_breakpoint ring. */
14013 for (related = bpt; related->related_breakpoint != bpt;
14014 related = related->related_breakpoint);
14015 related->related_breakpoint = bpt->related_breakpoint;
14016 bpt->related_breakpoint = bpt;
14017 }
14018
14019 /* watch_command_1 creates a watchpoint but only sets its number if
14020 update_watchpoint succeeds in creating its bp_locations. If there's
14021 a problem in that process, we'll be asked to delete the half-created
14022 watchpoint. In that case, don't announce the deletion. */
14023 if (bpt->number)
14024 observer_notify_breakpoint_deleted (bpt);
14025
14026 if (breakpoint_chain == bpt)
14027 breakpoint_chain = bpt->next;
14028
14029 ALL_BREAKPOINTS (b)
14030 if (b->next == bpt)
14031 {
14032 b->next = bpt->next;
14033 break;
14034 }
14035
14036 /* Be sure no bpstat's are pointing at the breakpoint after it's
14037 been freed. */
14038 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
14039 in all threads for now. Note that we cannot just remove bpstats
14040 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14041 commands are associated with the bpstat; if we remove it here,
14042 then the later call to bpstat_do_actions (&stop_bpstat); in
14043 event-top.c won't do anything, and temporary breakpoints with
14044 commands won't work. */
14045
14046 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14047
14048 /* Now that breakpoint is removed from breakpoint list, update the
14049 global location list. This will remove locations that used to
14050 belong to this breakpoint. Do this before freeing the breakpoint
14051 itself, since remove_breakpoint looks at location's owner. It
14052 might be better design to have location completely
14053 self-contained, but it's not the case now. */
14054 update_global_location_list (UGLL_DONT_INSERT);
14055
14056 bpt->ops->dtor (bpt);
14057 /* On the chance that someone will soon try again to delete this
14058 same bp, we mark it as deleted before freeing its storage. */
14059 bpt->type = bp_none;
14060 xfree (bpt);
14061}
14062
14063static void
14064do_delete_breakpoint_cleanup (void *b)
14065{
14066 delete_breakpoint (b);
14067}
14068
14069struct cleanup *
14070make_cleanup_delete_breakpoint (struct breakpoint *b)
14071{
14072 return make_cleanup (do_delete_breakpoint_cleanup, b);
14073}
14074
14075/* Iterator function to call a user-provided callback function once
14076 for each of B and its related breakpoints. */
14077
14078static void
14079iterate_over_related_breakpoints (struct breakpoint *b,
14080 void (*function) (struct breakpoint *,
14081 void *),
14082 void *data)
14083{
14084 struct breakpoint *related;
14085
14086 related = b;
14087 do
14088 {
14089 struct breakpoint *next;
14090
14091 /* FUNCTION may delete RELATED. */
14092 next = related->related_breakpoint;
14093
14094 if (next == related)
14095 {
14096 /* RELATED is the last ring entry. */
14097 function (related, data);
14098
14099 /* FUNCTION may have deleted it, so we'd never reach back to
14100 B. There's nothing left to do anyway, so just break
14101 out. */
14102 break;
14103 }
14104 else
14105 function (related, data);
14106
14107 related = next;
14108 }
14109 while (related != b);
14110}
14111
14112static void
14113do_delete_breakpoint (struct breakpoint *b, void *ignore)
14114{
14115 delete_breakpoint (b);
14116}
14117
14118/* A callback for map_breakpoint_numbers that calls
14119 delete_breakpoint. */
14120
14121static void
14122do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14123{
14124 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14125}
14126
14127void
14128delete_command (char *arg, int from_tty)
14129{
14130 struct breakpoint *b, *b_tmp;
14131
14132 dont_repeat ();
14133
14134 if (arg == 0)
14135 {
14136 int breaks_to_delete = 0;
14137
14138 /* Delete all breakpoints if no argument. Do not delete
14139 internal breakpoints, these have to be deleted with an
14140 explicit breakpoint number argument. */
14141 ALL_BREAKPOINTS (b)
14142 if (user_breakpoint_p (b))
14143 {
14144 breaks_to_delete = 1;
14145 break;
14146 }
14147
14148 /* Ask user only if there are some breakpoints to delete. */
14149 if (!from_tty
14150 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14151 {
14152 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14153 if (user_breakpoint_p (b))
14154 delete_breakpoint (b);
14155 }
14156 }
14157 else
14158 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14159}
14160
14161static int
14162all_locations_are_pending (struct bp_location *loc)
14163{
14164 for (; loc; loc = loc->next)
14165 if (!loc->shlib_disabled
14166 && !loc->pspace->executing_startup)
14167 return 0;
14168 return 1;
14169}
14170
14171/* Subroutine of update_breakpoint_locations to simplify it.
14172 Return non-zero if multiple fns in list LOC have the same name.
14173 Null names are ignored. */
14174
14175static int
14176ambiguous_names_p (struct bp_location *loc)
14177{
14178 struct bp_location *l;
14179 htab_t htab = htab_create_alloc (13, htab_hash_string,
14180 (int (*) (const void *,
14181 const void *)) streq,
14182 NULL, xcalloc, xfree);
14183
14184 for (l = loc; l != NULL; l = l->next)
14185 {
14186 const char **slot;
14187 const char *name = l->function_name;
14188
14189 /* Allow for some names to be NULL, ignore them. */
14190 if (name == NULL)
14191 continue;
14192
14193 slot = (const char **) htab_find_slot (htab, (const void *) name,
14194 INSERT);
14195 /* NOTE: We can assume slot != NULL here because xcalloc never
14196 returns NULL. */
14197 if (*slot != NULL)
14198 {
14199 htab_delete (htab);
14200 return 1;
14201 }
14202 *slot = name;
14203 }
14204
14205 htab_delete (htab);
14206 return 0;
14207}
14208
14209/* When symbols change, it probably means the sources changed as well,
14210 and it might mean the static tracepoint markers are no longer at
14211 the same address or line numbers they used to be at last we
14212 checked. Losing your static tracepoints whenever you rebuild is
14213 undesirable. This function tries to resync/rematch gdb static
14214 tracepoints with the markers on the target, for static tracepoints
14215 that have not been set by marker id. Static tracepoint that have
14216 been set by marker id are reset by marker id in breakpoint_re_set.
14217 The heuristic is:
14218
14219 1) For a tracepoint set at a specific address, look for a marker at
14220 the old PC. If one is found there, assume to be the same marker.
14221 If the name / string id of the marker found is different from the
14222 previous known name, assume that means the user renamed the marker
14223 in the sources, and output a warning.
14224
14225 2) For a tracepoint set at a given line number, look for a marker
14226 at the new address of the old line number. If one is found there,
14227 assume to be the same marker. If the name / string id of the
14228 marker found is different from the previous known name, assume that
14229 means the user renamed the marker in the sources, and output a
14230 warning.
14231
14232 3) If a marker is no longer found at the same address or line, it
14233 may mean the marker no longer exists. But it may also just mean
14234 the code changed a bit. Maybe the user added a few lines of code
14235 that made the marker move up or down (in line number terms). Ask
14236 the target for info about the marker with the string id as we knew
14237 it. If found, update line number and address in the matching
14238 static tracepoint. This will get confused if there's more than one
14239 marker with the same ID (possible in UST, although unadvised
14240 precisely because it confuses tools). */
14241
14242static struct symtab_and_line
14243update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14244{
14245 struct tracepoint *tp = (struct tracepoint *) b;
14246 struct static_tracepoint_marker marker;
14247 CORE_ADDR pc;
14248
14249 pc = sal.pc;
14250 if (sal.line)
14251 find_line_pc (sal.symtab, sal.line, &pc);
14252
14253 if (target_static_tracepoint_marker_at (pc, &marker))
14254 {
14255 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14256 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14257 b->number,
14258 tp->static_trace_marker_id, marker.str_id);
14259
14260 xfree (tp->static_trace_marker_id);
14261 tp->static_trace_marker_id = xstrdup (marker.str_id);
14262 release_static_tracepoint_marker (&marker);
14263
14264 return sal;
14265 }
14266
14267 /* Old marker wasn't found on target at lineno. Try looking it up
14268 by string ID. */
14269 if (!sal.explicit_pc
14270 && sal.line != 0
14271 && sal.symtab != NULL
14272 && tp->static_trace_marker_id != NULL)
14273 {
14274 VEC(static_tracepoint_marker_p) *markers;
14275
14276 markers
14277 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14278
14279 if (!VEC_empty(static_tracepoint_marker_p, markers))
14280 {
14281 struct symtab_and_line sal2;
14282 struct symbol *sym;
14283 struct static_tracepoint_marker *tpmarker;
14284 struct ui_out *uiout = current_uiout;
14285
14286 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14287
14288 xfree (tp->static_trace_marker_id);
14289 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14290
14291 warning (_("marker for static tracepoint %d (%s) not "
14292 "found at previous line number"),
14293 b->number, tp->static_trace_marker_id);
14294
14295 init_sal (&sal2);
14296
14297 sal2.pc = tpmarker->address;
14298
14299 sal2 = find_pc_line (tpmarker->address, 0);
14300 sym = find_pc_sect_function (tpmarker->address, NULL);
14301 ui_out_text (uiout, "Now in ");
14302 if (sym)
14303 {
14304 ui_out_field_string (uiout, "func",
14305 SYMBOL_PRINT_NAME (sym));
14306 ui_out_text (uiout, " at ");
14307 }
14308 ui_out_field_string (uiout, "file",
14309 symtab_to_filename_for_display (sal2.symtab));
14310 ui_out_text (uiout, ":");
14311
14312 if (ui_out_is_mi_like_p (uiout))
14313 {
14314 const char *fullname = symtab_to_fullname (sal2.symtab);
14315
14316 ui_out_field_string (uiout, "fullname", fullname);
14317 }
14318
14319 ui_out_field_int (uiout, "line", sal2.line);
14320 ui_out_text (uiout, "\n");
14321
14322 b->loc->line_number = sal2.line;
14323 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14324
14325 xfree (b->addr_string);
14326 b->addr_string = xstrprintf ("%s:%d",
14327 symtab_to_filename_for_display (sal2.symtab),
14328 b->loc->line_number);
14329
14330 /* Might be nice to check if function changed, and warn if
14331 so. */
14332
14333 release_static_tracepoint_marker (tpmarker);
14334 }
14335 }
14336 return sal;
14337}
14338
14339/* Returns 1 iff locations A and B are sufficiently same that
14340 we don't need to report breakpoint as changed. */
14341
14342static int
14343locations_are_equal (struct bp_location *a, struct bp_location *b)
14344{
14345 while (a && b)
14346 {
14347 if (a->address != b->address)
14348 return 0;
14349
14350 if (a->shlib_disabled != b->shlib_disabled)
14351 return 0;
14352
14353 if (a->enabled != b->enabled)
14354 return 0;
14355
14356 a = a->next;
14357 b = b->next;
14358 }
14359
14360 if ((a == NULL) != (b == NULL))
14361 return 0;
14362
14363 return 1;
14364}
14365
14366/* Create new breakpoint locations for B (a hardware or software breakpoint)
14367 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14368 a ranged breakpoint. */
14369
14370void
14371update_breakpoint_locations (struct breakpoint *b,
14372 struct symtabs_and_lines sals,
14373 struct symtabs_and_lines sals_end)
14374{
14375 int i;
14376 struct bp_location *existing_locations = b->loc;
14377
14378 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14379 {
14380 /* Ranged breakpoints have only one start location and one end
14381 location. */
14382 b->enable_state = bp_disabled;
14383 update_global_location_list (UGLL_MAY_INSERT);
14384 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14385 "multiple locations found\n"),
14386 b->number);
14387 return;
14388 }
14389
14390 /* If there's no new locations, and all existing locations are
14391 pending, don't do anything. This optimizes the common case where
14392 all locations are in the same shared library, that was unloaded.
14393 We'd like to retain the location, so that when the library is
14394 loaded again, we don't loose the enabled/disabled status of the
14395 individual locations. */
14396 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14397 return;
14398
14399 b->loc = NULL;
14400
14401 for (i = 0; i < sals.nelts; ++i)
14402 {
14403 struct bp_location *new_loc;
14404
14405 switch_to_program_space_and_thread (sals.sals[i].pspace);
14406
14407 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14408
14409 /* Reparse conditions, they might contain references to the
14410 old symtab. */
14411 if (b->cond_string != NULL)
14412 {
14413 const char *s;
14414 volatile struct gdb_exception e;
14415
14416 s = b->cond_string;
14417 TRY_CATCH (e, RETURN_MASK_ERROR)
14418 {
14419 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14420 block_for_pc (sals.sals[i].pc),
14421 0);
14422 }
14423 if (e.reason < 0)
14424 {
14425 warning (_("failed to reevaluate condition "
14426 "for breakpoint %d: %s"),
14427 b->number, e.message);
14428 new_loc->enabled = 0;
14429 }
14430 }
14431
14432 if (sals_end.nelts)
14433 {
14434 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14435
14436 new_loc->length = end - sals.sals[0].pc + 1;
14437 }
14438 }
14439
14440 /* Update locations of permanent breakpoints. */
14441 if (b->enable_state == bp_permanent)
14442 make_breakpoint_permanent (b);
14443
14444 /* If possible, carry over 'disable' status from existing
14445 breakpoints. */
14446 {
14447 struct bp_location *e = existing_locations;
14448 /* If there are multiple breakpoints with the same function name,
14449 e.g. for inline functions, comparing function names won't work.
14450 Instead compare pc addresses; this is just a heuristic as things
14451 may have moved, but in practice it gives the correct answer
14452 often enough until a better solution is found. */
14453 int have_ambiguous_names = ambiguous_names_p (b->loc);
14454
14455 for (; e; e = e->next)
14456 {
14457 if (!e->enabled && e->function_name)
14458 {
14459 struct bp_location *l = b->loc;
14460 if (have_ambiguous_names)
14461 {
14462 for (; l; l = l->next)
14463 if (breakpoint_locations_match (e, l))
14464 {
14465 l->enabled = 0;
14466 break;
14467 }
14468 }
14469 else
14470 {
14471 for (; l; l = l->next)
14472 if (l->function_name
14473 && strcmp (e->function_name, l->function_name) == 0)
14474 {
14475 l->enabled = 0;
14476 break;
14477 }
14478 }
14479 }
14480 }
14481 }
14482
14483 if (!locations_are_equal (existing_locations, b->loc))
14484 observer_notify_breakpoint_modified (b);
14485
14486 update_global_location_list (UGLL_MAY_INSERT);
14487}
14488
14489/* Find the SaL locations corresponding to the given ADDR_STRING.
14490 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14491
14492static struct symtabs_and_lines
14493addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14494{
14495 char *s;
14496 struct symtabs_and_lines sals = {0};
14497 volatile struct gdb_exception e;
14498
14499 gdb_assert (b->ops != NULL);
14500 s = addr_string;
14501
14502 TRY_CATCH (e, RETURN_MASK_ERROR)
14503 {
14504 b->ops->decode_linespec (b, &s, &sals);
14505 }
14506 if (e.reason < 0)
14507 {
14508 int not_found_and_ok = 0;
14509 /* For pending breakpoints, it's expected that parsing will
14510 fail until the right shared library is loaded. User has
14511 already told to create pending breakpoints and don't need
14512 extra messages. If breakpoint is in bp_shlib_disabled
14513 state, then user already saw the message about that
14514 breakpoint being disabled, and don't want to see more
14515 errors. */
14516 if (e.error == NOT_FOUND_ERROR
14517 && (b->condition_not_parsed
14518 || (b->loc && b->loc->shlib_disabled)
14519 || (b->loc && b->loc->pspace->executing_startup)
14520 || b->enable_state == bp_disabled))
14521 not_found_and_ok = 1;
14522
14523 if (!not_found_and_ok)
14524 {
14525 /* We surely don't want to warn about the same breakpoint
14526 10 times. One solution, implemented here, is disable
14527 the breakpoint on error. Another solution would be to
14528 have separate 'warning emitted' flag. Since this
14529 happens only when a binary has changed, I don't know
14530 which approach is better. */
14531 b->enable_state = bp_disabled;
14532 throw_exception (e);
14533 }
14534 }
14535
14536 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
14537 {
14538 int i;
14539
14540 for (i = 0; i < sals.nelts; ++i)
14541 resolve_sal_pc (&sals.sals[i]);
14542 if (b->condition_not_parsed && s && s[0])
14543 {
14544 char *cond_string, *extra_string;
14545 int thread, task;
14546
14547 find_condition_and_thread (s, sals.sals[0].pc,
14548 &cond_string, &thread, &task,
14549 &extra_string);
14550 if (cond_string)
14551 b->cond_string = cond_string;
14552 b->thread = thread;
14553 b->task = task;
14554 if (extra_string)
14555 b->extra_string = extra_string;
14556 b->condition_not_parsed = 0;
14557 }
14558
14559 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14560 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14561
14562 *found = 1;
14563 }
14564 else
14565 *found = 0;
14566
14567 return sals;
14568}
14569
14570/* The default re_set method, for typical hardware or software
14571 breakpoints. Reevaluate the breakpoint and recreate its
14572 locations. */
14573
14574static void
14575breakpoint_re_set_default (struct breakpoint *b)
14576{
14577 int found;
14578 struct symtabs_and_lines sals, sals_end;
14579 struct symtabs_and_lines expanded = {0};
14580 struct symtabs_and_lines expanded_end = {0};
14581
14582 sals = addr_string_to_sals (b, b->addr_string, &found);
14583 if (found)
14584 {
14585 make_cleanup (xfree, sals.sals);
14586 expanded = sals;
14587 }
14588
14589 if (b->addr_string_range_end)
14590 {
14591 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14592 if (found)
14593 {
14594 make_cleanup (xfree, sals_end.sals);
14595 expanded_end = sals_end;
14596 }
14597 }
14598
14599 update_breakpoint_locations (b, expanded, expanded_end);
14600}
14601
14602/* Default method for creating SALs from an address string. It basically
14603 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14604
14605static void
14606create_sals_from_address_default (char **arg,
14607 struct linespec_result *canonical,
14608 enum bptype type_wanted,
14609 char *addr_start, char **copy_arg)
14610{
14611 parse_breakpoint_sals (arg, canonical);
14612}
14613
14614/* Call create_breakpoints_sal for the given arguments. This is the default
14615 function for the `create_breakpoints_sal' method of
14616 breakpoint_ops. */
14617
14618static void
14619create_breakpoints_sal_default (struct gdbarch *gdbarch,
14620 struct linespec_result *canonical,
14621 char *cond_string,
14622 char *extra_string,
14623 enum bptype type_wanted,
14624 enum bpdisp disposition,
14625 int thread,
14626 int task, int ignore_count,
14627 const struct breakpoint_ops *ops,
14628 int from_tty, int enabled,
14629 int internal, unsigned flags)
14630{
14631 create_breakpoints_sal (gdbarch, canonical, cond_string,
14632 extra_string,
14633 type_wanted, disposition,
14634 thread, task, ignore_count, ops, from_tty,
14635 enabled, internal, flags);
14636}
14637
14638/* Decode the line represented by S by calling decode_line_full. This is the
14639 default function for the `decode_linespec' method of breakpoint_ops. */
14640
14641static void
14642decode_linespec_default (struct breakpoint *b, char **s,
14643 struct symtabs_and_lines *sals)
14644{
14645 struct linespec_result canonical;
14646
14647 init_linespec_result (&canonical);
14648 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14649 (struct symtab *) NULL, 0,
14650 &canonical, multiple_symbols_all,
14651 b->filter);
14652
14653 /* We should get 0 or 1 resulting SALs. */
14654 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14655
14656 if (VEC_length (linespec_sals, canonical.sals) > 0)
14657 {
14658 struct linespec_sals *lsal;
14659
14660 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14661 *sals = lsal->sals;
14662 /* Arrange it so the destructor does not free the
14663 contents. */
14664 lsal->sals.sals = NULL;
14665 }
14666
14667 destroy_linespec_result (&canonical);
14668}
14669
14670/* Prepare the global context for a re-set of breakpoint B. */
14671
14672static struct cleanup *
14673prepare_re_set_context (struct breakpoint *b)
14674{
14675 struct cleanup *cleanups;
14676
14677 input_radix = b->input_radix;
14678 cleanups = save_current_space_and_thread ();
14679 if (b->pspace != NULL)
14680 switch_to_program_space_and_thread (b->pspace);
14681 set_language (b->language);
14682
14683 return cleanups;
14684}
14685
14686/* Reset a breakpoint given it's struct breakpoint * BINT.
14687 The value we return ends up being the return value from catch_errors.
14688 Unused in this case. */
14689
14690static int
14691breakpoint_re_set_one (void *bint)
14692{
14693 /* Get past catch_errs. */
14694 struct breakpoint *b = (struct breakpoint *) bint;
14695 struct cleanup *cleanups;
14696
14697 cleanups = prepare_re_set_context (b);
14698 b->ops->re_set (b);
14699 do_cleanups (cleanups);
14700 return 0;
14701}
14702
14703/* Re-set all breakpoints after symbols have been re-loaded. */
14704void
14705breakpoint_re_set (void)
14706{
14707 struct breakpoint *b, *b_tmp;
14708 enum language save_language;
14709 int save_input_radix;
14710 struct cleanup *old_chain;
14711
14712 save_language = current_language->la_language;
14713 save_input_radix = input_radix;
14714 old_chain = save_current_program_space ();
14715
14716 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14717 {
14718 /* Format possible error msg. */
14719 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14720 b->number);
14721 struct cleanup *cleanups = make_cleanup (xfree, message);
14722 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14723 do_cleanups (cleanups);
14724 }
14725 set_language (save_language);
14726 input_radix = save_input_radix;
14727
14728 jit_breakpoint_re_set ();
14729
14730 do_cleanups (old_chain);
14731
14732 create_overlay_event_breakpoint ();
14733 create_longjmp_master_breakpoint ();
14734 create_std_terminate_master_breakpoint ();
14735 create_exception_master_breakpoint ();
14736}
14737\f
14738/* Reset the thread number of this breakpoint:
14739
14740 - If the breakpoint is for all threads, leave it as-is.
14741 - Else, reset it to the current thread for inferior_ptid. */
14742void
14743breakpoint_re_set_thread (struct breakpoint *b)
14744{
14745 if (b->thread != -1)
14746 {
14747 if (in_thread_list (inferior_ptid))
14748 b->thread = pid_to_thread_id (inferior_ptid);
14749
14750 /* We're being called after following a fork. The new fork is
14751 selected as current, and unless this was a vfork will have a
14752 different program space from the original thread. Reset that
14753 as well. */
14754 b->loc->pspace = current_program_space;
14755 }
14756}
14757
14758/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14759 If from_tty is nonzero, it prints a message to that effect,
14760 which ends with a period (no newline). */
14761
14762void
14763set_ignore_count (int bptnum, int count, int from_tty)
14764{
14765 struct breakpoint *b;
14766
14767 if (count < 0)
14768 count = 0;
14769
14770 ALL_BREAKPOINTS (b)
14771 if (b->number == bptnum)
14772 {
14773 if (is_tracepoint (b))
14774 {
14775 if (from_tty && count != 0)
14776 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14777 bptnum);
14778 return;
14779 }
14780
14781 b->ignore_count = count;
14782 if (from_tty)
14783 {
14784 if (count == 0)
14785 printf_filtered (_("Will stop next time "
14786 "breakpoint %d is reached."),
14787 bptnum);
14788 else if (count == 1)
14789 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14790 bptnum);
14791 else
14792 printf_filtered (_("Will ignore next %d "
14793 "crossings of breakpoint %d."),
14794 count, bptnum);
14795 }
14796 observer_notify_breakpoint_modified (b);
14797 return;
14798 }
14799
14800 error (_("No breakpoint number %d."), bptnum);
14801}
14802
14803/* Command to set ignore-count of breakpoint N to COUNT. */
14804
14805static void
14806ignore_command (char *args, int from_tty)
14807{
14808 char *p = args;
14809 int num;
14810
14811 if (p == 0)
14812 error_no_arg (_("a breakpoint number"));
14813
14814 num = get_number (&p);
14815 if (num == 0)
14816 error (_("bad breakpoint number: '%s'"), args);
14817 if (*p == 0)
14818 error (_("Second argument (specified ignore-count) is missing."));
14819
14820 set_ignore_count (num,
14821 longest_to_int (value_as_long (parse_and_eval (p))),
14822 from_tty);
14823 if (from_tty)
14824 printf_filtered ("\n");
14825}
14826\f
14827/* Call FUNCTION on each of the breakpoints
14828 whose numbers are given in ARGS. */
14829
14830static void
14831map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14832 void *),
14833 void *data)
14834{
14835 int num;
14836 struct breakpoint *b, *tmp;
14837 int match;
14838 struct get_number_or_range_state state;
14839
14840 if (args == 0)
14841 error_no_arg (_("one or more breakpoint numbers"));
14842
14843 init_number_or_range (&state, args);
14844
14845 while (!state.finished)
14846 {
14847 const char *p = state.string;
14848
14849 match = 0;
14850
14851 num = get_number_or_range (&state);
14852 if (num == 0)
14853 {
14854 warning (_("bad breakpoint number at or near '%s'"), p);
14855 }
14856 else
14857 {
14858 ALL_BREAKPOINTS_SAFE (b, tmp)
14859 if (b->number == num)
14860 {
14861 match = 1;
14862 function (b, data);
14863 break;
14864 }
14865 if (match == 0)
14866 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14867 }
14868 }
14869}
14870
14871static struct bp_location *
14872find_location_by_number (char *number)
14873{
14874 char *dot = strchr (number, '.');
14875 char *p1;
14876 int bp_num;
14877 int loc_num;
14878 struct breakpoint *b;
14879 struct bp_location *loc;
14880
14881 *dot = '\0';
14882
14883 p1 = number;
14884 bp_num = get_number (&p1);
14885 if (bp_num == 0)
14886 error (_("Bad breakpoint number '%s'"), number);
14887
14888 ALL_BREAKPOINTS (b)
14889 if (b->number == bp_num)
14890 {
14891 break;
14892 }
14893
14894 if (!b || b->number != bp_num)
14895 error (_("Bad breakpoint number '%s'"), number);
14896
14897 p1 = dot+1;
14898 loc_num = get_number (&p1);
14899 if (loc_num == 0)
14900 error (_("Bad breakpoint location number '%s'"), number);
14901
14902 --loc_num;
14903 loc = b->loc;
14904 for (;loc_num && loc; --loc_num, loc = loc->next)
14905 ;
14906 if (!loc)
14907 error (_("Bad breakpoint location number '%s'"), dot+1);
14908
14909 return loc;
14910}
14911
14912
14913/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14914 If from_tty is nonzero, it prints a message to that effect,
14915 which ends with a period (no newline). */
14916
14917void
14918disable_breakpoint (struct breakpoint *bpt)
14919{
14920 /* Never disable a watchpoint scope breakpoint; we want to
14921 hit them when we leave scope so we can delete both the
14922 watchpoint and its scope breakpoint at that time. */
14923 if (bpt->type == bp_watchpoint_scope)
14924 return;
14925
14926 /* You can't disable permanent breakpoints. */
14927 if (bpt->enable_state == bp_permanent)
14928 return;
14929
14930 bpt->enable_state = bp_disabled;
14931
14932 /* Mark breakpoint locations modified. */
14933 mark_breakpoint_modified (bpt);
14934
14935 if (target_supports_enable_disable_tracepoint ()
14936 && current_trace_status ()->running && is_tracepoint (bpt))
14937 {
14938 struct bp_location *location;
14939
14940 for (location = bpt->loc; location; location = location->next)
14941 target_disable_tracepoint (location);
14942 }
14943
14944 update_global_location_list (UGLL_DONT_INSERT);
14945
14946 observer_notify_breakpoint_modified (bpt);
14947}
14948
14949/* A callback for iterate_over_related_breakpoints. */
14950
14951static void
14952do_disable_breakpoint (struct breakpoint *b, void *ignore)
14953{
14954 disable_breakpoint (b);
14955}
14956
14957/* A callback for map_breakpoint_numbers that calls
14958 disable_breakpoint. */
14959
14960static void
14961do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14962{
14963 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14964}
14965
14966static void
14967disable_command (char *args, int from_tty)
14968{
14969 if (args == 0)
14970 {
14971 struct breakpoint *bpt;
14972
14973 ALL_BREAKPOINTS (bpt)
14974 if (user_breakpoint_p (bpt))
14975 disable_breakpoint (bpt);
14976 }
14977 else
14978 {
14979 char *num = extract_arg (&args);
14980
14981 while (num)
14982 {
14983 if (strchr (num, '.'))
14984 {
14985 struct bp_location *loc = find_location_by_number (num);
14986
14987 if (loc)
14988 {
14989 if (loc->enabled)
14990 {
14991 loc->enabled = 0;
14992 mark_breakpoint_location_modified (loc);
14993 }
14994 if (target_supports_enable_disable_tracepoint ()
14995 && current_trace_status ()->running && loc->owner
14996 && is_tracepoint (loc->owner))
14997 target_disable_tracepoint (loc);
14998 }
14999 update_global_location_list (UGLL_DONT_INSERT);
15000 }
15001 else
15002 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15003 num = extract_arg (&args);
15004 }
15005 }
15006}
15007
15008static void
15009enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15010 int count)
15011{
15012 int target_resources_ok;
15013
15014 if (bpt->type == bp_hardware_breakpoint)
15015 {
15016 int i;
15017 i = hw_breakpoint_used_count ();
15018 target_resources_ok =
15019 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
15020 i + 1, 0);
15021 if (target_resources_ok == 0)
15022 error (_("No hardware breakpoint support in the target."));
15023 else if (target_resources_ok < 0)
15024 error (_("Hardware breakpoints used exceeds limit."));
15025 }
15026
15027 if (is_watchpoint (bpt))
15028 {
15029 /* Initialize it just to avoid a GCC false warning. */
15030 enum enable_state orig_enable_state = 0;
15031 volatile struct gdb_exception e;
15032
15033 TRY_CATCH (e, RETURN_MASK_ALL)
15034 {
15035 struct watchpoint *w = (struct watchpoint *) bpt;
15036
15037 orig_enable_state = bpt->enable_state;
15038 bpt->enable_state = bp_enabled;
15039 update_watchpoint (w, 1 /* reparse */);
15040 }
15041 if (e.reason < 0)
15042 {
15043 bpt->enable_state = orig_enable_state;
15044 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15045 bpt->number);
15046 return;
15047 }
15048 }
15049
15050 if (bpt->enable_state != bp_permanent)
15051 bpt->enable_state = bp_enabled;
15052
15053 bpt->enable_state = bp_enabled;
15054
15055 /* Mark breakpoint locations modified. */
15056 mark_breakpoint_modified (bpt);
15057
15058 if (target_supports_enable_disable_tracepoint ()
15059 && current_trace_status ()->running && is_tracepoint (bpt))
15060 {
15061 struct bp_location *location;
15062
15063 for (location = bpt->loc; location; location = location->next)
15064 target_enable_tracepoint (location);
15065 }
15066
15067 bpt->disposition = disposition;
15068 bpt->enable_count = count;
15069 update_global_location_list (UGLL_MAY_INSERT);
15070
15071 observer_notify_breakpoint_modified (bpt);
15072}
15073
15074
15075void
15076enable_breakpoint (struct breakpoint *bpt)
15077{
15078 enable_breakpoint_disp (bpt, bpt->disposition, 0);
15079}
15080
15081static void
15082do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15083{
15084 enable_breakpoint (bpt);
15085}
15086
15087/* A callback for map_breakpoint_numbers that calls
15088 enable_breakpoint. */
15089
15090static void
15091do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15092{
15093 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15094}
15095
15096/* The enable command enables the specified breakpoints (or all defined
15097 breakpoints) so they once again become (or continue to be) effective
15098 in stopping the inferior. */
15099
15100static void
15101enable_command (char *args, int from_tty)
15102{
15103 if (args == 0)
15104 {
15105 struct breakpoint *bpt;
15106
15107 ALL_BREAKPOINTS (bpt)
15108 if (user_breakpoint_p (bpt))
15109 enable_breakpoint (bpt);
15110 }
15111 else
15112 {
15113 char *num = extract_arg (&args);
15114
15115 while (num)
15116 {
15117 if (strchr (num, '.'))
15118 {
15119 struct bp_location *loc = find_location_by_number (num);
15120
15121 if (loc)
15122 {
15123 if (!loc->enabled)
15124 {
15125 loc->enabled = 1;
15126 mark_breakpoint_location_modified (loc);
15127 }
15128 if (target_supports_enable_disable_tracepoint ()
15129 && current_trace_status ()->running && loc->owner
15130 && is_tracepoint (loc->owner))
15131 target_enable_tracepoint (loc);
15132 }
15133 update_global_location_list (UGLL_MAY_INSERT);
15134 }
15135 else
15136 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15137 num = extract_arg (&args);
15138 }
15139 }
15140}
15141
15142/* This struct packages up disposition data for application to multiple
15143 breakpoints. */
15144
15145struct disp_data
15146{
15147 enum bpdisp disp;
15148 int count;
15149};
15150
15151static void
15152do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15153{
15154 struct disp_data disp_data = *(struct disp_data *) arg;
15155
15156 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15157}
15158
15159static void
15160do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15161{
15162 struct disp_data disp = { disp_disable, 1 };
15163
15164 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15165}
15166
15167static void
15168enable_once_command (char *args, int from_tty)
15169{
15170 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15171}
15172
15173static void
15174do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15175{
15176 struct disp_data disp = { disp_disable, *(int *) countptr };
15177
15178 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15179}
15180
15181static void
15182enable_count_command (char *args, int from_tty)
15183{
15184 int count = get_number (&args);
15185
15186 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15187}
15188
15189static void
15190do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15191{
15192 struct disp_data disp = { disp_del, 1 };
15193
15194 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15195}
15196
15197static void
15198enable_delete_command (char *args, int from_tty)
15199{
15200 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15201}
15202\f
15203static void
15204set_breakpoint_cmd (char *args, int from_tty)
15205{
15206}
15207
15208static void
15209show_breakpoint_cmd (char *args, int from_tty)
15210{
15211}
15212
15213/* Invalidate last known value of any hardware watchpoint if
15214 the memory which that value represents has been written to by
15215 GDB itself. */
15216
15217static void
15218invalidate_bp_value_on_memory_change (struct inferior *inferior,
15219 CORE_ADDR addr, ssize_t len,
15220 const bfd_byte *data)
15221{
15222 struct breakpoint *bp;
15223
15224 ALL_BREAKPOINTS (bp)
15225 if (bp->enable_state == bp_enabled
15226 && bp->type == bp_hardware_watchpoint)
15227 {
15228 struct watchpoint *wp = (struct watchpoint *) bp;
15229
15230 if (wp->val_valid && wp->val)
15231 {
15232 struct bp_location *loc;
15233
15234 for (loc = bp->loc; loc != NULL; loc = loc->next)
15235 if (loc->loc_type == bp_loc_hardware_watchpoint
15236 && loc->address + loc->length > addr
15237 && addr + len > loc->address)
15238 {
15239 value_free (wp->val);
15240 wp->val = NULL;
15241 wp->val_valid = 0;
15242 }
15243 }
15244 }
15245}
15246
15247/* Create and insert a breakpoint for software single step. */
15248
15249void
15250insert_single_step_breakpoint (struct gdbarch *gdbarch,
15251 struct address_space *aspace,
15252 CORE_ADDR next_pc)
15253{
15254 struct thread_info *tp = inferior_thread ();
15255 struct symtab_and_line sal;
15256 CORE_ADDR pc = next_pc;
15257
15258 if (tp->control.single_step_breakpoints == NULL)
15259 {
15260 tp->control.single_step_breakpoints
15261 = new_single_step_breakpoint (tp->num, gdbarch);
15262 }
15263
15264 sal = find_pc_line (pc, 0);
15265 sal.pc = pc;
15266 sal.section = find_pc_overlay (pc);
15267 sal.explicit_pc = 1;
15268 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15269
15270 update_global_location_list (UGLL_INSERT);
15271}
15272
15273/* See breakpoint.h. */
15274
15275int
15276breakpoint_has_location_inserted_here (struct breakpoint *bp,
15277 struct address_space *aspace,
15278 CORE_ADDR pc)
15279{
15280 struct bp_location *loc;
15281
15282 for (loc = bp->loc; loc != NULL; loc = loc->next)
15283 if (loc->inserted
15284 && breakpoint_location_address_match (loc, aspace, pc))
15285 return 1;
15286
15287 return 0;
15288}
15289
15290/* Check whether a software single-step breakpoint is inserted at
15291 PC. */
15292
15293int
15294single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15295 CORE_ADDR pc)
15296{
15297 struct breakpoint *bpt;
15298
15299 ALL_BREAKPOINTS (bpt)
15300 {
15301 if (bpt->type == bp_single_step
15302 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15303 return 1;
15304 }
15305 return 0;
15306}
15307
15308/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15309 non-zero otherwise. */
15310static int
15311is_syscall_catchpoint_enabled (struct breakpoint *bp)
15312{
15313 if (syscall_catchpoint_p (bp)
15314 && bp->enable_state != bp_disabled
15315 && bp->enable_state != bp_call_disabled)
15316 return 1;
15317 else
15318 return 0;
15319}
15320
15321int
15322catch_syscall_enabled (void)
15323{
15324 struct catch_syscall_inferior_data *inf_data
15325 = get_catch_syscall_inferior_data (current_inferior ());
15326
15327 return inf_data->total_syscalls_count != 0;
15328}
15329
15330int
15331catching_syscall_number (int syscall_number)
15332{
15333 struct breakpoint *bp;
15334
15335 ALL_BREAKPOINTS (bp)
15336 if (is_syscall_catchpoint_enabled (bp))
15337 {
15338 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15339
15340 if (c->syscalls_to_be_caught)
15341 {
15342 int i, iter;
15343 for (i = 0;
15344 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15345 i++)
15346 if (syscall_number == iter)
15347 return 1;
15348 }
15349 else
15350 return 1;
15351 }
15352
15353 return 0;
15354}
15355
15356/* Complete syscall names. Used by "catch syscall". */
15357static VEC (char_ptr) *
15358catch_syscall_completer (struct cmd_list_element *cmd,
15359 const char *text, const char *word)
15360{
15361 const char **list = get_syscall_names ();
15362 VEC (char_ptr) *retlist
15363 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15364
15365 xfree (list);
15366 return retlist;
15367}
15368
15369/* Tracepoint-specific operations. */
15370
15371/* Set tracepoint count to NUM. */
15372static void
15373set_tracepoint_count (int num)
15374{
15375 tracepoint_count = num;
15376 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15377}
15378
15379static void
15380trace_command (char *arg, int from_tty)
15381{
15382 struct breakpoint_ops *ops;
15383 const char *arg_cp = arg;
15384
15385 if (arg && probe_linespec_to_ops (&arg_cp))
15386 ops = &tracepoint_probe_breakpoint_ops;
15387 else
15388 ops = &tracepoint_breakpoint_ops;
15389
15390 create_breakpoint (get_current_arch (),
15391 arg,
15392 NULL, 0, NULL, 1 /* parse arg */,
15393 0 /* tempflag */,
15394 bp_tracepoint /* type_wanted */,
15395 0 /* Ignore count */,
15396 pending_break_support,
15397 ops,
15398 from_tty,
15399 1 /* enabled */,
15400 0 /* internal */, 0);
15401}
15402
15403static void
15404ftrace_command (char *arg, int from_tty)
15405{
15406 create_breakpoint (get_current_arch (),
15407 arg,
15408 NULL, 0, NULL, 1 /* parse arg */,
15409 0 /* tempflag */,
15410 bp_fast_tracepoint /* type_wanted */,
15411 0 /* Ignore count */,
15412 pending_break_support,
15413 &tracepoint_breakpoint_ops,
15414 from_tty,
15415 1 /* enabled */,
15416 0 /* internal */, 0);
15417}
15418
15419/* strace command implementation. Creates a static tracepoint. */
15420
15421static void
15422strace_command (char *arg, int from_tty)
15423{
15424 struct breakpoint_ops *ops;
15425
15426 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15427 or with a normal static tracepoint. */
15428 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15429 ops = &strace_marker_breakpoint_ops;
15430 else
15431 ops = &tracepoint_breakpoint_ops;
15432
15433 create_breakpoint (get_current_arch (),
15434 arg,
15435 NULL, 0, NULL, 1 /* parse arg */,
15436 0 /* tempflag */,
15437 bp_static_tracepoint /* type_wanted */,
15438 0 /* Ignore count */,
15439 pending_break_support,
15440 ops,
15441 from_tty,
15442 1 /* enabled */,
15443 0 /* internal */, 0);
15444}
15445
15446/* Set up a fake reader function that gets command lines from a linked
15447 list that was acquired during tracepoint uploading. */
15448
15449static struct uploaded_tp *this_utp;
15450static int next_cmd;
15451
15452static char *
15453read_uploaded_action (void)
15454{
15455 char *rslt;
15456
15457 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15458
15459 next_cmd++;
15460
15461 return rslt;
15462}
15463
15464/* Given information about a tracepoint as recorded on a target (which
15465 can be either a live system or a trace file), attempt to create an
15466 equivalent GDB tracepoint. This is not a reliable process, since
15467 the target does not necessarily have all the information used when
15468 the tracepoint was originally defined. */
15469
15470struct tracepoint *
15471create_tracepoint_from_upload (struct uploaded_tp *utp)
15472{
15473 char *addr_str, small_buf[100];
15474 struct tracepoint *tp;
15475
15476 if (utp->at_string)
15477 addr_str = utp->at_string;
15478 else
15479 {
15480 /* In the absence of a source location, fall back to raw
15481 address. Since there is no way to confirm that the address
15482 means the same thing as when the trace was started, warn the
15483 user. */
15484 warning (_("Uploaded tracepoint %d has no "
15485 "source location, using raw address"),
15486 utp->number);
15487 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15488 addr_str = small_buf;
15489 }
15490
15491 /* There's not much we can do with a sequence of bytecodes. */
15492 if (utp->cond && !utp->cond_string)
15493 warning (_("Uploaded tracepoint %d condition "
15494 "has no source form, ignoring it"),
15495 utp->number);
15496
15497 if (!create_breakpoint (get_current_arch (),
15498 addr_str,
15499 utp->cond_string, -1, NULL,
15500 0 /* parse cond/thread */,
15501 0 /* tempflag */,
15502 utp->type /* type_wanted */,
15503 0 /* Ignore count */,
15504 pending_break_support,
15505 &tracepoint_breakpoint_ops,
15506 0 /* from_tty */,
15507 utp->enabled /* enabled */,
15508 0 /* internal */,
15509 CREATE_BREAKPOINT_FLAGS_INSERTED))
15510 return NULL;
15511
15512 /* Get the tracepoint we just created. */
15513 tp = get_tracepoint (tracepoint_count);
15514 gdb_assert (tp != NULL);
15515
15516 if (utp->pass > 0)
15517 {
15518 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15519 tp->base.number);
15520
15521 trace_pass_command (small_buf, 0);
15522 }
15523
15524 /* If we have uploaded versions of the original commands, set up a
15525 special-purpose "reader" function and call the usual command line
15526 reader, then pass the result to the breakpoint command-setting
15527 function. */
15528 if (!VEC_empty (char_ptr, utp->cmd_strings))
15529 {
15530 struct command_line *cmd_list;
15531
15532 this_utp = utp;
15533 next_cmd = 0;
15534
15535 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15536
15537 breakpoint_set_commands (&tp->base, cmd_list);
15538 }
15539 else if (!VEC_empty (char_ptr, utp->actions)
15540 || !VEC_empty (char_ptr, utp->step_actions))
15541 warning (_("Uploaded tracepoint %d actions "
15542 "have no source form, ignoring them"),
15543 utp->number);
15544
15545 /* Copy any status information that might be available. */
15546 tp->base.hit_count = utp->hit_count;
15547 tp->traceframe_usage = utp->traceframe_usage;
15548
15549 return tp;
15550}
15551
15552/* Print information on tracepoint number TPNUM_EXP, or all if
15553 omitted. */
15554
15555static void
15556tracepoints_info (char *args, int from_tty)
15557{
15558 struct ui_out *uiout = current_uiout;
15559 int num_printed;
15560
15561 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15562
15563 if (num_printed == 0)
15564 {
15565 if (args == NULL || *args == '\0')
15566 ui_out_message (uiout, 0, "No tracepoints.\n");
15567 else
15568 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15569 }
15570
15571 default_collect_info ();
15572}
15573
15574/* The 'enable trace' command enables tracepoints.
15575 Not supported by all targets. */
15576static void
15577enable_trace_command (char *args, int from_tty)
15578{
15579 enable_command (args, from_tty);
15580}
15581
15582/* The 'disable trace' command disables tracepoints.
15583 Not supported by all targets. */
15584static void
15585disable_trace_command (char *args, int from_tty)
15586{
15587 disable_command (args, from_tty);
15588}
15589
15590/* Remove a tracepoint (or all if no argument). */
15591static void
15592delete_trace_command (char *arg, int from_tty)
15593{
15594 struct breakpoint *b, *b_tmp;
15595
15596 dont_repeat ();
15597
15598 if (arg == 0)
15599 {
15600 int breaks_to_delete = 0;
15601
15602 /* Delete all breakpoints if no argument.
15603 Do not delete internal or call-dummy breakpoints, these
15604 have to be deleted with an explicit breakpoint number
15605 argument. */
15606 ALL_TRACEPOINTS (b)
15607 if (is_tracepoint (b) && user_breakpoint_p (b))
15608 {
15609 breaks_to_delete = 1;
15610 break;
15611 }
15612
15613 /* Ask user only if there are some breakpoints to delete. */
15614 if (!from_tty
15615 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15616 {
15617 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15618 if (is_tracepoint (b) && user_breakpoint_p (b))
15619 delete_breakpoint (b);
15620 }
15621 }
15622 else
15623 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15624}
15625
15626/* Helper function for trace_pass_command. */
15627
15628static void
15629trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15630{
15631 tp->pass_count = count;
15632 observer_notify_breakpoint_modified (&tp->base);
15633 if (from_tty)
15634 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15635 tp->base.number, count);
15636}
15637
15638/* Set passcount for tracepoint.
15639
15640 First command argument is passcount, second is tracepoint number.
15641 If tracepoint number omitted, apply to most recently defined.
15642 Also accepts special argument "all". */
15643
15644static void
15645trace_pass_command (char *args, int from_tty)
15646{
15647 struct tracepoint *t1;
15648 unsigned int count;
15649
15650 if (args == 0 || *args == 0)
15651 error (_("passcount command requires an "
15652 "argument (count + optional TP num)"));
15653
15654 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15655
15656 args = skip_spaces (args);
15657 if (*args && strncasecmp (args, "all", 3) == 0)
15658 {
15659 struct breakpoint *b;
15660
15661 args += 3; /* Skip special argument "all". */
15662 if (*args)
15663 error (_("Junk at end of arguments."));
15664
15665 ALL_TRACEPOINTS (b)
15666 {
15667 t1 = (struct tracepoint *) b;
15668 trace_pass_set_count (t1, count, from_tty);
15669 }
15670 }
15671 else if (*args == '\0')
15672 {
15673 t1 = get_tracepoint_by_number (&args, NULL);
15674 if (t1)
15675 trace_pass_set_count (t1, count, from_tty);
15676 }
15677 else
15678 {
15679 struct get_number_or_range_state state;
15680
15681 init_number_or_range (&state, args);
15682 while (!state.finished)
15683 {
15684 t1 = get_tracepoint_by_number (&args, &state);
15685 if (t1)
15686 trace_pass_set_count (t1, count, from_tty);
15687 }
15688 }
15689}
15690
15691struct tracepoint *
15692get_tracepoint (int num)
15693{
15694 struct breakpoint *t;
15695
15696 ALL_TRACEPOINTS (t)
15697 if (t->number == num)
15698 return (struct tracepoint *) t;
15699
15700 return NULL;
15701}
15702
15703/* Find the tracepoint with the given target-side number (which may be
15704 different from the tracepoint number after disconnecting and
15705 reconnecting). */
15706
15707struct tracepoint *
15708get_tracepoint_by_number_on_target (int num)
15709{
15710 struct breakpoint *b;
15711
15712 ALL_TRACEPOINTS (b)
15713 {
15714 struct tracepoint *t = (struct tracepoint *) b;
15715
15716 if (t->number_on_target == num)
15717 return t;
15718 }
15719
15720 return NULL;
15721}
15722
15723/* Utility: parse a tracepoint number and look it up in the list.
15724 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15725 If the argument is missing, the most recent tracepoint
15726 (tracepoint_count) is returned. */
15727
15728struct tracepoint *
15729get_tracepoint_by_number (char **arg,
15730 struct get_number_or_range_state *state)
15731{
15732 struct breakpoint *t;
15733 int tpnum;
15734 char *instring = arg == NULL ? NULL : *arg;
15735
15736 if (state)
15737 {
15738 gdb_assert (!state->finished);
15739 tpnum = get_number_or_range (state);
15740 }
15741 else if (arg == NULL || *arg == NULL || ! **arg)
15742 tpnum = tracepoint_count;
15743 else
15744 tpnum = get_number (arg);
15745
15746 if (tpnum <= 0)
15747 {
15748 if (instring && *instring)
15749 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15750 instring);
15751 else
15752 printf_filtered (_("No previous tracepoint\n"));
15753 return NULL;
15754 }
15755
15756 ALL_TRACEPOINTS (t)
15757 if (t->number == tpnum)
15758 {
15759 return (struct tracepoint *) t;
15760 }
15761
15762 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15763 return NULL;
15764}
15765
15766void
15767print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15768{
15769 if (b->thread != -1)
15770 fprintf_unfiltered (fp, " thread %d", b->thread);
15771
15772 if (b->task != 0)
15773 fprintf_unfiltered (fp, " task %d", b->task);
15774
15775 fprintf_unfiltered (fp, "\n");
15776}
15777
15778/* Save information on user settable breakpoints (watchpoints, etc) to
15779 a new script file named FILENAME. If FILTER is non-NULL, call it
15780 on each breakpoint and only include the ones for which it returns
15781 non-zero. */
15782
15783static void
15784save_breakpoints (char *filename, int from_tty,
15785 int (*filter) (const struct breakpoint *))
15786{
15787 struct breakpoint *tp;
15788 int any = 0;
15789 struct cleanup *cleanup;
15790 struct ui_file *fp;
15791 int extra_trace_bits = 0;
15792
15793 if (filename == 0 || *filename == 0)
15794 error (_("Argument required (file name in which to save)"));
15795
15796 /* See if we have anything to save. */
15797 ALL_BREAKPOINTS (tp)
15798 {
15799 /* Skip internal and momentary breakpoints. */
15800 if (!user_breakpoint_p (tp))
15801 continue;
15802
15803 /* If we have a filter, only save the breakpoints it accepts. */
15804 if (filter && !filter (tp))
15805 continue;
15806
15807 any = 1;
15808
15809 if (is_tracepoint (tp))
15810 {
15811 extra_trace_bits = 1;
15812
15813 /* We can stop searching. */
15814 break;
15815 }
15816 }
15817
15818 if (!any)
15819 {
15820 warning (_("Nothing to save."));
15821 return;
15822 }
15823
15824 filename = tilde_expand (filename);
15825 cleanup = make_cleanup (xfree, filename);
15826 fp = gdb_fopen (filename, "w");
15827 if (!fp)
15828 error (_("Unable to open file '%s' for saving (%s)"),
15829 filename, safe_strerror (errno));
15830 make_cleanup_ui_file_delete (fp);
15831
15832 if (extra_trace_bits)
15833 save_trace_state_variables (fp);
15834
15835 ALL_BREAKPOINTS (tp)
15836 {
15837 /* Skip internal and momentary breakpoints. */
15838 if (!user_breakpoint_p (tp))
15839 continue;
15840
15841 /* If we have a filter, only save the breakpoints it accepts. */
15842 if (filter && !filter (tp))
15843 continue;
15844
15845 tp->ops->print_recreate (tp, fp);
15846
15847 /* Note, we can't rely on tp->number for anything, as we can't
15848 assume the recreated breakpoint numbers will match. Use $bpnum
15849 instead. */
15850
15851 if (tp->cond_string)
15852 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15853
15854 if (tp->ignore_count)
15855 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15856
15857 if (tp->type != bp_dprintf && tp->commands)
15858 {
15859 volatile struct gdb_exception ex;
15860
15861 fprintf_unfiltered (fp, " commands\n");
15862
15863 ui_out_redirect (current_uiout, fp);
15864 TRY_CATCH (ex, RETURN_MASK_ALL)
15865 {
15866 print_command_lines (current_uiout, tp->commands->commands, 2);
15867 }
15868 ui_out_redirect (current_uiout, NULL);
15869
15870 if (ex.reason < 0)
15871 throw_exception (ex);
15872
15873 fprintf_unfiltered (fp, " end\n");
15874 }
15875
15876 if (tp->enable_state == bp_disabled)
15877 fprintf_unfiltered (fp, "disable $bpnum\n");
15878
15879 /* If this is a multi-location breakpoint, check if the locations
15880 should be individually disabled. Watchpoint locations are
15881 special, and not user visible. */
15882 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15883 {
15884 struct bp_location *loc;
15885 int n = 1;
15886
15887 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15888 if (!loc->enabled)
15889 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15890 }
15891 }
15892
15893 if (extra_trace_bits && *default_collect)
15894 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15895
15896 if (from_tty)
15897 printf_filtered (_("Saved to file '%s'.\n"), filename);
15898 do_cleanups (cleanup);
15899}
15900
15901/* The `save breakpoints' command. */
15902
15903static void
15904save_breakpoints_command (char *args, int from_tty)
15905{
15906 save_breakpoints (args, from_tty, NULL);
15907}
15908
15909/* The `save tracepoints' command. */
15910
15911static void
15912save_tracepoints_command (char *args, int from_tty)
15913{
15914 save_breakpoints (args, from_tty, is_tracepoint);
15915}
15916
15917/* Create a vector of all tracepoints. */
15918
15919VEC(breakpoint_p) *
15920all_tracepoints (void)
15921{
15922 VEC(breakpoint_p) *tp_vec = 0;
15923 struct breakpoint *tp;
15924
15925 ALL_TRACEPOINTS (tp)
15926 {
15927 VEC_safe_push (breakpoint_p, tp_vec, tp);
15928 }
15929
15930 return tp_vec;
15931}
15932
15933\f
15934/* This help string is used for the break, hbreak, tbreak and thbreak
15935 commands. It is defined as a macro to prevent duplication.
15936 COMMAND should be a string constant containing the name of the
15937 command. */
15938#define BREAK_ARGS_HELP(command) \
15939command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15940PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15941probe point. Accepted values are `-probe' (for a generic, automatically\n\
15942guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
15943LOCATION may be a line number, function name, or \"*\" and an address.\n\
15944If a line number is specified, break at start of code for that line.\n\
15945If a function is specified, break at start of code for that function.\n\
15946If an address is specified, break at that exact address.\n\
15947With no LOCATION, uses current execution address of the selected\n\
15948stack frame. This is useful for breaking on return to a stack frame.\n\
15949\n\
15950THREADNUM is the number from \"info threads\".\n\
15951CONDITION is a boolean expression.\n\
15952\n\
15953Multiple breakpoints at one place are permitted, and useful if their\n\
15954conditions are different.\n\
15955\n\
15956Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15957
15958/* List of subcommands for "catch". */
15959static struct cmd_list_element *catch_cmdlist;
15960
15961/* List of subcommands for "tcatch". */
15962static struct cmd_list_element *tcatch_cmdlist;
15963
15964void
15965add_catch_command (char *name, char *docstring,
15966 cmd_sfunc_ftype *sfunc,
15967 completer_ftype *completer,
15968 void *user_data_catch,
15969 void *user_data_tcatch)
15970{
15971 struct cmd_list_element *command;
15972
15973 command = add_cmd (name, class_breakpoint, NULL, docstring,
15974 &catch_cmdlist);
15975 set_cmd_sfunc (command, sfunc);
15976 set_cmd_context (command, user_data_catch);
15977 set_cmd_completer (command, completer);
15978
15979 command = add_cmd (name, class_breakpoint, NULL, docstring,
15980 &tcatch_cmdlist);
15981 set_cmd_sfunc (command, sfunc);
15982 set_cmd_context (command, user_data_tcatch);
15983 set_cmd_completer (command, completer);
15984}
15985
15986static void
15987clear_syscall_counts (struct inferior *inf)
15988{
15989 struct catch_syscall_inferior_data *inf_data
15990 = get_catch_syscall_inferior_data (inf);
15991
15992 inf_data->total_syscalls_count = 0;
15993 inf_data->any_syscall_count = 0;
15994 VEC_free (int, inf_data->syscalls_counts);
15995}
15996
15997static void
15998save_command (char *arg, int from_tty)
15999{
16000 printf_unfiltered (_("\"save\" must be followed by "
16001 "the name of a save subcommand.\n"));
16002 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
16003}
16004
16005struct breakpoint *
16006iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16007 void *data)
16008{
16009 struct breakpoint *b, *b_tmp;
16010
16011 ALL_BREAKPOINTS_SAFE (b, b_tmp)
16012 {
16013 if ((*callback) (b, data))
16014 return b;
16015 }
16016
16017 return NULL;
16018}
16019
16020/* Zero if any of the breakpoint's locations could be a location where
16021 functions have been inlined, nonzero otherwise. */
16022
16023static int
16024is_non_inline_function (struct breakpoint *b)
16025{
16026 /* The shared library event breakpoint is set on the address of a
16027 non-inline function. */
16028 if (b->type == bp_shlib_event)
16029 return 1;
16030
16031 return 0;
16032}
16033
16034/* Nonzero if the specified PC cannot be a location where functions
16035 have been inlined. */
16036
16037int
16038pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16039 const struct target_waitstatus *ws)
16040{
16041 struct breakpoint *b;
16042 struct bp_location *bl;
16043
16044 ALL_BREAKPOINTS (b)
16045 {
16046 if (!is_non_inline_function (b))
16047 continue;
16048
16049 for (bl = b->loc; bl != NULL; bl = bl->next)
16050 {
16051 if (!bl->shlib_disabled
16052 && bpstat_check_location (bl, aspace, pc, ws))
16053 return 1;
16054 }
16055 }
16056
16057 return 0;
16058}
16059
16060/* Remove any references to OBJFILE which is going to be freed. */
16061
16062void
16063breakpoint_free_objfile (struct objfile *objfile)
16064{
16065 struct bp_location **locp, *loc;
16066
16067 ALL_BP_LOCATIONS (loc, locp)
16068 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16069 loc->symtab = NULL;
16070}
16071
16072void
16073initialize_breakpoint_ops (void)
16074{
16075 static int initialized = 0;
16076
16077 struct breakpoint_ops *ops;
16078
16079 if (initialized)
16080 return;
16081 initialized = 1;
16082
16083 /* The breakpoint_ops structure to be inherit by all kinds of
16084 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16085 internal and momentary breakpoints, etc.). */
16086 ops = &bkpt_base_breakpoint_ops;
16087 *ops = base_breakpoint_ops;
16088 ops->re_set = bkpt_re_set;
16089 ops->insert_location = bkpt_insert_location;
16090 ops->remove_location = bkpt_remove_location;
16091 ops->breakpoint_hit = bkpt_breakpoint_hit;
16092 ops->create_sals_from_address = bkpt_create_sals_from_address;
16093 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16094 ops->decode_linespec = bkpt_decode_linespec;
16095
16096 /* The breakpoint_ops structure to be used in regular breakpoints. */
16097 ops = &bkpt_breakpoint_ops;
16098 *ops = bkpt_base_breakpoint_ops;
16099 ops->re_set = bkpt_re_set;
16100 ops->resources_needed = bkpt_resources_needed;
16101 ops->print_it = bkpt_print_it;
16102 ops->print_mention = bkpt_print_mention;
16103 ops->print_recreate = bkpt_print_recreate;
16104
16105 /* Ranged breakpoints. */
16106 ops = &ranged_breakpoint_ops;
16107 *ops = bkpt_breakpoint_ops;
16108 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16109 ops->resources_needed = resources_needed_ranged_breakpoint;
16110 ops->print_it = print_it_ranged_breakpoint;
16111 ops->print_one = print_one_ranged_breakpoint;
16112 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16113 ops->print_mention = print_mention_ranged_breakpoint;
16114 ops->print_recreate = print_recreate_ranged_breakpoint;
16115
16116 /* Internal breakpoints. */
16117 ops = &internal_breakpoint_ops;
16118 *ops = bkpt_base_breakpoint_ops;
16119 ops->re_set = internal_bkpt_re_set;
16120 ops->check_status = internal_bkpt_check_status;
16121 ops->print_it = internal_bkpt_print_it;
16122 ops->print_mention = internal_bkpt_print_mention;
16123
16124 /* Momentary breakpoints. */
16125 ops = &momentary_breakpoint_ops;
16126 *ops = bkpt_base_breakpoint_ops;
16127 ops->re_set = momentary_bkpt_re_set;
16128 ops->check_status = momentary_bkpt_check_status;
16129 ops->print_it = momentary_bkpt_print_it;
16130 ops->print_mention = momentary_bkpt_print_mention;
16131
16132 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16133 ops = &longjmp_breakpoint_ops;
16134 *ops = momentary_breakpoint_ops;
16135 ops->dtor = longjmp_bkpt_dtor;
16136
16137 /* Probe breakpoints. */
16138 ops = &bkpt_probe_breakpoint_ops;
16139 *ops = bkpt_breakpoint_ops;
16140 ops->insert_location = bkpt_probe_insert_location;
16141 ops->remove_location = bkpt_probe_remove_location;
16142 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16143 ops->decode_linespec = bkpt_probe_decode_linespec;
16144
16145 /* Watchpoints. */
16146 ops = &watchpoint_breakpoint_ops;
16147 *ops = base_breakpoint_ops;
16148 ops->dtor = dtor_watchpoint;
16149 ops->re_set = re_set_watchpoint;
16150 ops->insert_location = insert_watchpoint;
16151 ops->remove_location = remove_watchpoint;
16152 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16153 ops->check_status = check_status_watchpoint;
16154 ops->resources_needed = resources_needed_watchpoint;
16155 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16156 ops->print_it = print_it_watchpoint;
16157 ops->print_mention = print_mention_watchpoint;
16158 ops->print_recreate = print_recreate_watchpoint;
16159 ops->explains_signal = explains_signal_watchpoint;
16160
16161 /* Masked watchpoints. */
16162 ops = &masked_watchpoint_breakpoint_ops;
16163 *ops = watchpoint_breakpoint_ops;
16164 ops->insert_location = insert_masked_watchpoint;
16165 ops->remove_location = remove_masked_watchpoint;
16166 ops->resources_needed = resources_needed_masked_watchpoint;
16167 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16168 ops->print_it = print_it_masked_watchpoint;
16169 ops->print_one_detail = print_one_detail_masked_watchpoint;
16170 ops->print_mention = print_mention_masked_watchpoint;
16171 ops->print_recreate = print_recreate_masked_watchpoint;
16172
16173 /* Tracepoints. */
16174 ops = &tracepoint_breakpoint_ops;
16175 *ops = base_breakpoint_ops;
16176 ops->re_set = tracepoint_re_set;
16177 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16178 ops->print_one_detail = tracepoint_print_one_detail;
16179 ops->print_mention = tracepoint_print_mention;
16180 ops->print_recreate = tracepoint_print_recreate;
16181 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16182 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16183 ops->decode_linespec = tracepoint_decode_linespec;
16184
16185 /* Probe tracepoints. */
16186 ops = &tracepoint_probe_breakpoint_ops;
16187 *ops = tracepoint_breakpoint_ops;
16188 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16189 ops->decode_linespec = tracepoint_probe_decode_linespec;
16190
16191 /* Static tracepoints with marker (`-m'). */
16192 ops = &strace_marker_breakpoint_ops;
16193 *ops = tracepoint_breakpoint_ops;
16194 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16195 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16196 ops->decode_linespec = strace_marker_decode_linespec;
16197
16198 /* Fork catchpoints. */
16199 ops = &catch_fork_breakpoint_ops;
16200 *ops = base_breakpoint_ops;
16201 ops->insert_location = insert_catch_fork;
16202 ops->remove_location = remove_catch_fork;
16203 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16204 ops->print_it = print_it_catch_fork;
16205 ops->print_one = print_one_catch_fork;
16206 ops->print_mention = print_mention_catch_fork;
16207 ops->print_recreate = print_recreate_catch_fork;
16208
16209 /* Vfork catchpoints. */
16210 ops = &catch_vfork_breakpoint_ops;
16211 *ops = base_breakpoint_ops;
16212 ops->insert_location = insert_catch_vfork;
16213 ops->remove_location = remove_catch_vfork;
16214 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16215 ops->print_it = print_it_catch_vfork;
16216 ops->print_one = print_one_catch_vfork;
16217 ops->print_mention = print_mention_catch_vfork;
16218 ops->print_recreate = print_recreate_catch_vfork;
16219
16220 /* Exec catchpoints. */
16221 ops = &catch_exec_breakpoint_ops;
16222 *ops = base_breakpoint_ops;
16223 ops->dtor = dtor_catch_exec;
16224 ops->insert_location = insert_catch_exec;
16225 ops->remove_location = remove_catch_exec;
16226 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16227 ops->print_it = print_it_catch_exec;
16228 ops->print_one = print_one_catch_exec;
16229 ops->print_mention = print_mention_catch_exec;
16230 ops->print_recreate = print_recreate_catch_exec;
16231
16232 /* Syscall catchpoints. */
16233 ops = &catch_syscall_breakpoint_ops;
16234 *ops = base_breakpoint_ops;
16235 ops->dtor = dtor_catch_syscall;
16236 ops->insert_location = insert_catch_syscall;
16237 ops->remove_location = remove_catch_syscall;
16238 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16239 ops->print_it = print_it_catch_syscall;
16240 ops->print_one = print_one_catch_syscall;
16241 ops->print_mention = print_mention_catch_syscall;
16242 ops->print_recreate = print_recreate_catch_syscall;
16243
16244 /* Solib-related catchpoints. */
16245 ops = &catch_solib_breakpoint_ops;
16246 *ops = base_breakpoint_ops;
16247 ops->dtor = dtor_catch_solib;
16248 ops->insert_location = insert_catch_solib;
16249 ops->remove_location = remove_catch_solib;
16250 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16251 ops->check_status = check_status_catch_solib;
16252 ops->print_it = print_it_catch_solib;
16253 ops->print_one = print_one_catch_solib;
16254 ops->print_mention = print_mention_catch_solib;
16255 ops->print_recreate = print_recreate_catch_solib;
16256
16257 ops = &dprintf_breakpoint_ops;
16258 *ops = bkpt_base_breakpoint_ops;
16259 ops->re_set = dprintf_re_set;
16260 ops->resources_needed = bkpt_resources_needed;
16261 ops->print_it = bkpt_print_it;
16262 ops->print_mention = bkpt_print_mention;
16263 ops->print_recreate = dprintf_print_recreate;
16264 ops->after_condition_true = dprintf_after_condition_true;
16265 ops->breakpoint_hit = dprintf_breakpoint_hit;
16266}
16267
16268/* Chain containing all defined "enable breakpoint" subcommands. */
16269
16270static struct cmd_list_element *enablebreaklist = NULL;
16271
16272void
16273_initialize_breakpoint (void)
16274{
16275 struct cmd_list_element *c;
16276
16277 initialize_breakpoint_ops ();
16278
16279 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16280 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16281 observer_attach_inferior_exit (clear_syscall_counts);
16282 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16283
16284 breakpoint_objfile_key
16285 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16286
16287 catch_syscall_inferior_data
16288 = register_inferior_data_with_cleanup (NULL,
16289 catch_syscall_inferior_data_cleanup);
16290
16291 breakpoint_chain = 0;
16292 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16293 before a breakpoint is set. */
16294 breakpoint_count = 0;
16295
16296 tracepoint_count = 0;
16297
16298 add_com ("ignore", class_breakpoint, ignore_command, _("\
16299Set ignore-count of breakpoint number N to COUNT.\n\
16300Usage is `ignore N COUNT'."));
16301 if (xdb_commands)
16302 add_com_alias ("bc", "ignore", class_breakpoint, 1);
16303
16304 add_com ("commands", class_breakpoint, commands_command, _("\
16305Set commands to be executed when a breakpoint is hit.\n\
16306Give breakpoint number as argument after \"commands\".\n\
16307With no argument, the targeted breakpoint is the last one set.\n\
16308The commands themselves follow starting on the next line.\n\
16309Type a line containing \"end\" to indicate the end of them.\n\
16310Give \"silent\" as the first line to make the breakpoint silent;\n\
16311then no output is printed when it is hit, except what the commands print."));
16312
16313 c = add_com ("condition", class_breakpoint, condition_command, _("\
16314Specify breakpoint number N to break only if COND is true.\n\
16315Usage is `condition N COND', where N is an integer and COND is an\n\
16316expression to be evaluated whenever breakpoint N is reached."));
16317 set_cmd_completer (c, condition_completer);
16318
16319 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16320Set a temporary breakpoint.\n\
16321Like \"break\" except the breakpoint is only temporary,\n\
16322so it will be deleted when hit. Equivalent to \"break\" followed\n\
16323by using \"enable delete\" on the breakpoint number.\n\
16324\n"
16325BREAK_ARGS_HELP ("tbreak")));
16326 set_cmd_completer (c, location_completer);
16327
16328 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16329Set a hardware assisted breakpoint.\n\
16330Like \"break\" except the breakpoint requires hardware support,\n\
16331some target hardware may not have this support.\n\
16332\n"
16333BREAK_ARGS_HELP ("hbreak")));
16334 set_cmd_completer (c, location_completer);
16335
16336 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16337Set a temporary hardware assisted breakpoint.\n\
16338Like \"hbreak\" except the breakpoint is only temporary,\n\
16339so it will be deleted when hit.\n\
16340\n"
16341BREAK_ARGS_HELP ("thbreak")));
16342 set_cmd_completer (c, location_completer);
16343
16344 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16345Enable some breakpoints.\n\
16346Give breakpoint numbers (separated by spaces) as arguments.\n\
16347With no subcommand, breakpoints are enabled until you command otherwise.\n\
16348This is used to cancel the effect of the \"disable\" command.\n\
16349With a subcommand you can enable temporarily."),
16350 &enablelist, "enable ", 1, &cmdlist);
16351 if (xdb_commands)
16352 add_com ("ab", class_breakpoint, enable_command, _("\
16353Enable some breakpoints.\n\
16354Give breakpoint numbers (separated by spaces) as arguments.\n\
16355With no subcommand, breakpoints are enabled until you command otherwise.\n\
16356This is used to cancel the effect of the \"disable\" command.\n\
16357With a subcommand you can enable temporarily."));
16358
16359 add_com_alias ("en", "enable", class_breakpoint, 1);
16360
16361 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16362Enable some breakpoints.\n\
16363Give breakpoint numbers (separated by spaces) as arguments.\n\
16364This is used to cancel the effect of the \"disable\" command.\n\
16365May be abbreviated to simply \"enable\".\n"),
16366 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16367
16368 add_cmd ("once", no_class, enable_once_command, _("\
16369Enable breakpoints for one hit. Give breakpoint numbers.\n\
16370If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16371 &enablebreaklist);
16372
16373 add_cmd ("delete", no_class, enable_delete_command, _("\
16374Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16375If a breakpoint is hit while enabled in this fashion, it is deleted."),
16376 &enablebreaklist);
16377
16378 add_cmd ("count", no_class, enable_count_command, _("\
16379Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16380If a breakpoint is hit while enabled in this fashion,\n\
16381the count is decremented; when it reaches zero, the breakpoint is disabled."),
16382 &enablebreaklist);
16383
16384 add_cmd ("delete", no_class, enable_delete_command, _("\
16385Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16386If a breakpoint is hit while enabled in this fashion, it is deleted."),
16387 &enablelist);
16388
16389 add_cmd ("once", no_class, enable_once_command, _("\
16390Enable breakpoints for one hit. Give breakpoint numbers.\n\
16391If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16392 &enablelist);
16393
16394 add_cmd ("count", no_class, enable_count_command, _("\
16395Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16396If a breakpoint is hit while enabled in this fashion,\n\
16397the count is decremented; when it reaches zero, the breakpoint is disabled."),
16398 &enablelist);
16399
16400 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16401Disable some breakpoints.\n\
16402Arguments are breakpoint numbers with spaces in between.\n\
16403To disable all breakpoints, give no argument.\n\
16404A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16405 &disablelist, "disable ", 1, &cmdlist);
16406 add_com_alias ("dis", "disable", class_breakpoint, 1);
16407 add_com_alias ("disa", "disable", class_breakpoint, 1);
16408 if (xdb_commands)
16409 add_com ("sb", class_breakpoint, disable_command, _("\
16410Disable some breakpoints.\n\
16411Arguments are breakpoint numbers with spaces in between.\n\
16412To disable all breakpoints, give no argument.\n\
16413A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16414
16415 add_cmd ("breakpoints", class_alias, disable_command, _("\
16416Disable some breakpoints.\n\
16417Arguments are breakpoint numbers with spaces in between.\n\
16418To disable all breakpoints, give no argument.\n\
16419A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16420This command may be abbreviated \"disable\"."),
16421 &disablelist);
16422
16423 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16424Delete some breakpoints or auto-display expressions.\n\
16425Arguments are breakpoint numbers with spaces in between.\n\
16426To delete all breakpoints, give no argument.\n\
16427\n\
16428Also a prefix command for deletion of other GDB objects.\n\
16429The \"unset\" command is also an alias for \"delete\"."),
16430 &deletelist, "delete ", 1, &cmdlist);
16431 add_com_alias ("d", "delete", class_breakpoint, 1);
16432 add_com_alias ("del", "delete", class_breakpoint, 1);
16433 if (xdb_commands)
16434 add_com ("db", class_breakpoint, delete_command, _("\
16435Delete some breakpoints.\n\
16436Arguments are breakpoint numbers with spaces in between.\n\
16437To delete all breakpoints, give no argument.\n"));
16438
16439 add_cmd ("breakpoints", class_alias, delete_command, _("\
16440Delete some breakpoints or auto-display expressions.\n\
16441Arguments are breakpoint numbers with spaces in between.\n\
16442To delete all breakpoints, give no argument.\n\
16443This command may be abbreviated \"delete\"."),
16444 &deletelist);
16445
16446 add_com ("clear", class_breakpoint, clear_command, _("\
16447Clear breakpoint at specified line or function.\n\
16448Argument may be line number, function name, or \"*\" and an address.\n\
16449If line number is specified, all breakpoints in that line are cleared.\n\
16450If function is specified, breakpoints at beginning of function are cleared.\n\
16451If an address is specified, breakpoints at that address are cleared.\n\
16452\n\
16453With no argument, clears all breakpoints in the line that the selected frame\n\
16454is executing in.\n\
16455\n\
16456See also the \"delete\" command which clears breakpoints by number."));
16457 add_com_alias ("cl", "clear", class_breakpoint, 1);
16458
16459 c = add_com ("break", class_breakpoint, break_command, _("\
16460Set breakpoint at specified line or function.\n"
16461BREAK_ARGS_HELP ("break")));
16462 set_cmd_completer (c, location_completer);
16463
16464 add_com_alias ("b", "break", class_run, 1);
16465 add_com_alias ("br", "break", class_run, 1);
16466 add_com_alias ("bre", "break", class_run, 1);
16467 add_com_alias ("brea", "break", class_run, 1);
16468
16469 if (xdb_commands)
16470 add_com_alias ("ba", "break", class_breakpoint, 1);
16471
16472 if (dbx_commands)
16473 {
16474 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16475Break in function/address or break at a line in the current file."),
16476 &stoplist, "stop ", 1, &cmdlist);
16477 add_cmd ("in", class_breakpoint, stopin_command,
16478 _("Break in function or address."), &stoplist);
16479 add_cmd ("at", class_breakpoint, stopat_command,
16480 _("Break at a line in the current file."), &stoplist);
16481 add_com ("status", class_info, breakpoints_info, _("\
16482Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16483The \"Type\" column indicates one of:\n\
16484\tbreakpoint - normal breakpoint\n\
16485\twatchpoint - watchpoint\n\
16486The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16487the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16488breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16489address and file/line number respectively.\n\
16490\n\
16491Convenience variable \"$_\" and default examine address for \"x\"\n\
16492are set to the address of the last breakpoint listed unless the command\n\
16493is prefixed with \"server \".\n\n\
16494Convenience variable \"$bpnum\" contains the number of the last\n\
16495breakpoint set."));
16496 }
16497
16498 add_info ("breakpoints", breakpoints_info, _("\
16499Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16500The \"Type\" column indicates one of:\n\
16501\tbreakpoint - normal breakpoint\n\
16502\twatchpoint - watchpoint\n\
16503The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16504the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16505breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16506address and file/line number respectively.\n\
16507\n\
16508Convenience variable \"$_\" and default examine address for \"x\"\n\
16509are set to the address of the last breakpoint listed unless the command\n\
16510is prefixed with \"server \".\n\n\
16511Convenience variable \"$bpnum\" contains the number of the last\n\
16512breakpoint set."));
16513
16514 add_info_alias ("b", "breakpoints", 1);
16515
16516 if (xdb_commands)
16517 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16518Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16519The \"Type\" column indicates one of:\n\
16520\tbreakpoint - normal breakpoint\n\
16521\twatchpoint - watchpoint\n\
16522The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16523the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16524breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16525address and file/line number respectively.\n\
16526\n\
16527Convenience variable \"$_\" and default examine address for \"x\"\n\
16528are set to the address of the last breakpoint listed unless the command\n\
16529is prefixed with \"server \".\n\n\
16530Convenience variable \"$bpnum\" contains the number of the last\n\
16531breakpoint set."));
16532
16533 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16534Status of all breakpoints, or breakpoint number NUMBER.\n\
16535The \"Type\" column indicates one of:\n\
16536\tbreakpoint - normal breakpoint\n\
16537\twatchpoint - watchpoint\n\
16538\tlongjmp - internal breakpoint used to step through longjmp()\n\
16539\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16540\tuntil - internal breakpoint used by the \"until\" command\n\
16541\tfinish - internal breakpoint used by the \"finish\" command\n\
16542The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16543the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16544breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16545address and file/line number respectively.\n\
16546\n\
16547Convenience variable \"$_\" and default examine address for \"x\"\n\
16548are set to the address of the last breakpoint listed unless the command\n\
16549is prefixed with \"server \".\n\n\
16550Convenience variable \"$bpnum\" contains the number of the last\n\
16551breakpoint set."),
16552 &maintenanceinfolist);
16553
16554 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16555Set catchpoints to catch events."),
16556 &catch_cmdlist, "catch ",
16557 0/*allow-unknown*/, &cmdlist);
16558
16559 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16560Set temporary catchpoints to catch events."),
16561 &tcatch_cmdlist, "tcatch ",
16562 0/*allow-unknown*/, &cmdlist);
16563
16564 add_catch_command ("fork", _("Catch calls to fork."),
16565 catch_fork_command_1,
16566 NULL,
16567 (void *) (uintptr_t) catch_fork_permanent,
16568 (void *) (uintptr_t) catch_fork_temporary);
16569 add_catch_command ("vfork", _("Catch calls to vfork."),
16570 catch_fork_command_1,
16571 NULL,
16572 (void *) (uintptr_t) catch_vfork_permanent,
16573 (void *) (uintptr_t) catch_vfork_temporary);
16574 add_catch_command ("exec", _("Catch calls to exec."),
16575 catch_exec_command_1,
16576 NULL,
16577 CATCH_PERMANENT,
16578 CATCH_TEMPORARY);
16579 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16580Usage: catch load [REGEX]\n\
16581If REGEX is given, only stop for libraries matching the regular expression."),
16582 catch_load_command_1,
16583 NULL,
16584 CATCH_PERMANENT,
16585 CATCH_TEMPORARY);
16586 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16587Usage: catch unload [REGEX]\n\
16588If REGEX is given, only stop for libraries matching the regular expression."),
16589 catch_unload_command_1,
16590 NULL,
16591 CATCH_PERMANENT,
16592 CATCH_TEMPORARY);
16593 add_catch_command ("syscall", _("\
16594Catch system calls by their names and/or numbers.\n\
16595Arguments say which system calls to catch. If no arguments\n\
16596are given, every system call will be caught.\n\
16597Arguments, if given, should be one or more system call names\n\
16598(if your system supports that), or system call numbers."),
16599 catch_syscall_command_1,
16600 catch_syscall_completer,
16601 CATCH_PERMANENT,
16602 CATCH_TEMPORARY);
16603
16604 c = add_com ("watch", class_breakpoint, watch_command, _("\
16605Set a watchpoint for an expression.\n\
16606Usage: watch [-l|-location] EXPRESSION\n\
16607A watchpoint stops execution of your program whenever the value of\n\
16608an expression changes.\n\
16609If -l or -location is given, this evaluates EXPRESSION and watches\n\
16610the memory to which it refers."));
16611 set_cmd_completer (c, expression_completer);
16612
16613 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16614Set a read watchpoint for an expression.\n\
16615Usage: rwatch [-l|-location] EXPRESSION\n\
16616A watchpoint stops execution of your program whenever the value of\n\
16617an expression is read.\n\
16618If -l or -location is given, this evaluates EXPRESSION and watches\n\
16619the memory to which it refers."));
16620 set_cmd_completer (c, expression_completer);
16621
16622 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16623Set a watchpoint for an expression.\n\
16624Usage: awatch [-l|-location] EXPRESSION\n\
16625A watchpoint stops execution of your program whenever the value of\n\
16626an expression is either read or written.\n\
16627If -l or -location is given, this evaluates EXPRESSION and watches\n\
16628the memory to which it refers."));
16629 set_cmd_completer (c, expression_completer);
16630
16631 add_info ("watchpoints", watchpoints_info, _("\
16632Status of specified watchpoints (all watchpoints if no argument)."));
16633
16634 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16635 respond to changes - contrary to the description. */
16636 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16637 &can_use_hw_watchpoints, _("\
16638Set debugger's willingness to use watchpoint hardware."), _("\
16639Show debugger's willingness to use watchpoint hardware."), _("\
16640If zero, gdb will not use hardware for new watchpoints, even if\n\
16641such is available. (However, any hardware watchpoints that were\n\
16642created before setting this to nonzero, will continue to use watchpoint\n\
16643hardware.)"),
16644 NULL,
16645 show_can_use_hw_watchpoints,
16646 &setlist, &showlist);
16647
16648 can_use_hw_watchpoints = 1;
16649
16650 /* Tracepoint manipulation commands. */
16651
16652 c = add_com ("trace", class_breakpoint, trace_command, _("\
16653Set a tracepoint at specified line or function.\n\
16654\n"
16655BREAK_ARGS_HELP ("trace") "\n\
16656Do \"help tracepoints\" for info on other tracepoint commands."));
16657 set_cmd_completer (c, location_completer);
16658
16659 add_com_alias ("tp", "trace", class_alias, 0);
16660 add_com_alias ("tr", "trace", class_alias, 1);
16661 add_com_alias ("tra", "trace", class_alias, 1);
16662 add_com_alias ("trac", "trace", class_alias, 1);
16663
16664 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16665Set a fast tracepoint at specified line or function.\n\
16666\n"
16667BREAK_ARGS_HELP ("ftrace") "\n\
16668Do \"help tracepoints\" for info on other tracepoint commands."));
16669 set_cmd_completer (c, location_completer);
16670
16671 c = add_com ("strace", class_breakpoint, strace_command, _("\
16672Set a static tracepoint at specified line, function or marker.\n\
16673\n\
16674strace [LOCATION] [if CONDITION]\n\
16675LOCATION may be a line number, function name, \"*\" and an address,\n\
16676or -m MARKER_ID.\n\
16677If a line number is specified, probe the marker at start of code\n\
16678for that line. If a function is specified, probe the marker at start\n\
16679of code for that function. If an address is specified, probe the marker\n\
16680at that exact address. If a marker id is specified, probe the marker\n\
16681with that name. With no LOCATION, uses current execution address of\n\
16682the selected stack frame.\n\
16683Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16684This collects arbitrary user data passed in the probe point call to the\n\
16685tracing library. You can inspect it when analyzing the trace buffer,\n\
16686by printing the $_sdata variable like any other convenience variable.\n\
16687\n\
16688CONDITION is a boolean expression.\n\
16689\n\
16690Multiple tracepoints at one place are permitted, and useful if their\n\
16691conditions are different.\n\
16692\n\
16693Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16694Do \"help tracepoints\" for info on other tracepoint commands."));
16695 set_cmd_completer (c, location_completer);
16696
16697 add_info ("tracepoints", tracepoints_info, _("\
16698Status of specified tracepoints (all tracepoints if no argument).\n\
16699Convenience variable \"$tpnum\" contains the number of the\n\
16700last tracepoint set."));
16701
16702 add_info_alias ("tp", "tracepoints", 1);
16703
16704 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16705Delete specified tracepoints.\n\
16706Arguments are tracepoint numbers, separated by spaces.\n\
16707No argument means delete all tracepoints."),
16708 &deletelist);
16709 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16710
16711 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16712Disable specified tracepoints.\n\
16713Arguments are tracepoint numbers, separated by spaces.\n\
16714No argument means disable all tracepoints."),
16715 &disablelist);
16716 deprecate_cmd (c, "disable");
16717
16718 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16719Enable specified tracepoints.\n\
16720Arguments are tracepoint numbers, separated by spaces.\n\
16721No argument means enable all tracepoints."),
16722 &enablelist);
16723 deprecate_cmd (c, "enable");
16724
16725 add_com ("passcount", class_trace, trace_pass_command, _("\
16726Set the passcount for a tracepoint.\n\
16727The trace will end when the tracepoint has been passed 'count' times.\n\
16728Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16729if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16730
16731 add_prefix_cmd ("save", class_breakpoint, save_command,
16732 _("Save breakpoint definitions as a script."),
16733 &save_cmdlist, "save ",
16734 0/*allow-unknown*/, &cmdlist);
16735
16736 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16737Save current breakpoint definitions as a script.\n\
16738This includes all types of breakpoints (breakpoints, watchpoints,\n\
16739catchpoints, tracepoints). Use the 'source' command in another debug\n\
16740session to restore them."),
16741 &save_cmdlist);
16742 set_cmd_completer (c, filename_completer);
16743
16744 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16745Save current tracepoint definitions as a script.\n\
16746Use the 'source' command in another debug session to restore them."),
16747 &save_cmdlist);
16748 set_cmd_completer (c, filename_completer);
16749
16750 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16751 deprecate_cmd (c, "save tracepoints");
16752
16753 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16754Breakpoint specific settings\n\
16755Configure various breakpoint-specific variables such as\n\
16756pending breakpoint behavior"),
16757 &breakpoint_set_cmdlist, "set breakpoint ",
16758 0/*allow-unknown*/, &setlist);
16759 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16760Breakpoint specific settings\n\
16761Configure various breakpoint-specific variables such as\n\
16762pending breakpoint behavior"),
16763 &breakpoint_show_cmdlist, "show breakpoint ",
16764 0/*allow-unknown*/, &showlist);
16765
16766 add_setshow_auto_boolean_cmd ("pending", no_class,
16767 &pending_break_support, _("\
16768Set debugger's behavior regarding pending breakpoints."), _("\
16769Show debugger's behavior regarding pending breakpoints."), _("\
16770If on, an unrecognized breakpoint location will cause gdb to create a\n\
16771pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16772an error. If auto, an unrecognized breakpoint location results in a\n\
16773user-query to see if a pending breakpoint should be created."),
16774 NULL,
16775 show_pending_break_support,
16776 &breakpoint_set_cmdlist,
16777 &breakpoint_show_cmdlist);
16778
16779 pending_break_support = AUTO_BOOLEAN_AUTO;
16780
16781 add_setshow_boolean_cmd ("auto-hw", no_class,
16782 &automatic_hardware_breakpoints, _("\
16783Set automatic usage of hardware breakpoints."), _("\
16784Show automatic usage of hardware breakpoints."), _("\
16785If set, the debugger will automatically use hardware breakpoints for\n\
16786breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16787a warning will be emitted for such breakpoints."),
16788 NULL,
16789 show_automatic_hardware_breakpoints,
16790 &breakpoint_set_cmdlist,
16791 &breakpoint_show_cmdlist);
16792
16793 add_setshow_boolean_cmd ("always-inserted", class_support,
16794 &always_inserted_mode, _("\
16795Set mode for inserting breakpoints."), _("\
16796Show mode for inserting breakpoints."), _("\
16797When this mode is on, breakpoints are inserted immediately as soon as\n\
16798they're created, kept inserted even when execution stops, and removed\n\
16799only when the user deletes them. When this mode is off (the default),\n\
16800breakpoints are inserted only when execution continues, and removed\n\
16801when execution stops."),
16802 NULL,
16803 &show_always_inserted_mode,
16804 &breakpoint_set_cmdlist,
16805 &breakpoint_show_cmdlist);
16806
16807 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16808 condition_evaluation_enums,
16809 &condition_evaluation_mode_1, _("\
16810Set mode of breakpoint condition evaluation."), _("\
16811Show mode of breakpoint condition evaluation."), _("\
16812When this is set to \"host\", breakpoint conditions will be\n\
16813evaluated on the host's side by GDB. When it is set to \"target\",\n\
16814breakpoint conditions will be downloaded to the target (if the target\n\
16815supports such feature) and conditions will be evaluated on the target's side.\n\
16816If this is set to \"auto\" (default), this will be automatically set to\n\
16817\"target\" if it supports condition evaluation, otherwise it will\n\
16818be set to \"gdb\""),
16819 &set_condition_evaluation_mode,
16820 &show_condition_evaluation_mode,
16821 &breakpoint_set_cmdlist,
16822 &breakpoint_show_cmdlist);
16823
16824 add_com ("break-range", class_breakpoint, break_range_command, _("\
16825Set a breakpoint for an address range.\n\
16826break-range START-LOCATION, END-LOCATION\n\
16827where START-LOCATION and END-LOCATION can be one of the following:\n\
16828 LINENUM, for that line in the current file,\n\
16829 FILE:LINENUM, for that line in that file,\n\
16830 +OFFSET, for that number of lines after the current line\n\
16831 or the start of the range\n\
16832 FUNCTION, for the first line in that function,\n\
16833 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16834 *ADDRESS, for the instruction at that address.\n\
16835\n\
16836The breakpoint will stop execution of the inferior whenever it executes\n\
16837an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16838range (including START-LOCATION and END-LOCATION)."));
16839
16840 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16841Set a dynamic printf at specified line or function.\n\
16842dprintf location,format string,arg1,arg2,...\n\
16843location may be a line number, function name, or \"*\" and an address.\n\
16844If a line number is specified, break at start of code for that line.\n\
16845If a function is specified, break at start of code for that function."));
16846 set_cmd_completer (c, location_completer);
16847
16848 add_setshow_enum_cmd ("dprintf-style", class_support,
16849 dprintf_style_enums, &dprintf_style, _("\
16850Set the style of usage for dynamic printf."), _("\
16851Show the style of usage for dynamic printf."), _("\
16852This setting chooses how GDB will do a dynamic printf.\n\
16853If the value is \"gdb\", then the printing is done by GDB to its own\n\
16854console, as with the \"printf\" command.\n\
16855If the value is \"call\", the print is done by calling a function in your\n\
16856program; by default printf(), but you can choose a different function or\n\
16857output stream by setting dprintf-function and dprintf-channel."),
16858 update_dprintf_commands, NULL,
16859 &setlist, &showlist);
16860
16861 dprintf_function = xstrdup ("printf");
16862 add_setshow_string_cmd ("dprintf-function", class_support,
16863 &dprintf_function, _("\
16864Set the function to use for dynamic printf"), _("\
16865Show the function to use for dynamic printf"), NULL,
16866 update_dprintf_commands, NULL,
16867 &setlist, &showlist);
16868
16869 dprintf_channel = xstrdup ("");
16870 add_setshow_string_cmd ("dprintf-channel", class_support,
16871 &dprintf_channel, _("\
16872Set the channel to use for dynamic printf"), _("\
16873Show the channel to use for dynamic printf"), NULL,
16874 update_dprintf_commands, NULL,
16875 &setlist, &showlist);
16876
16877 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16878 &disconnected_dprintf, _("\
16879Set whether dprintf continues after GDB disconnects."), _("\
16880Show whether dprintf continues after GDB disconnects."), _("\
16881Use this to let dprintf commands continue to hit and produce output\n\
16882even if GDB disconnects or detaches from the target."),
16883 NULL,
16884 NULL,
16885 &setlist, &showlist);
16886
16887 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16888agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16889(target agent only) This is useful for formatted output in user-defined commands."));
16890
16891 automatic_hardware_breakpoints = 1;
16892
16893 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16894 observer_attach_thread_exit (remove_threaded_breakpoints);
16895}
This page took 0.079414 seconds and 4 git commands to generate.