Home | History | Annotate | Line # | Download | only in gdb
      1   1.1  christos /* Everything about breakpoints, for GDB.
      2   1.1  christos 
      3  1.11  christos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
      4   1.1  christos 
      5   1.1  christos    This file is part of GDB.
      6   1.1  christos 
      7   1.1  christos    This program is free software; you can redistribute it and/or modify
      8   1.1  christos    it under the terms of the GNU General Public License as published by
      9   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10   1.1  christos    (at your option) any later version.
     11   1.1  christos 
     12   1.1  christos    This program is distributed in the hope that it will be useful,
     13   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1  christos    GNU General Public License for more details.
     16   1.1  christos 
     17   1.1  christos    You should have received a copy of the GNU General Public License
     18   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19   1.1  christos 
     20   1.1  christos #include "arch-utils.h"
     21   1.1  christos #include <ctype.h>
     22  1.11  christos #include "event-top.h"
     23   1.1  christos #include "hashtab.h"
     24   1.1  christos #include "symtab.h"
     25   1.1  christos #include "frame.h"
     26   1.1  christos #include "breakpoint.h"
     27   1.1  christos #include "tracepoint.h"
     28   1.1  christos #include "gdbtypes.h"
     29   1.1  christos #include "expression.h"
     30   1.1  christos #include "gdbcore.h"
     31  1.11  christos #include "cli/cli-cmds.h"
     32   1.1  christos #include "value.h"
     33   1.1  christos #include "command.h"
     34   1.1  christos #include "inferior.h"
     35   1.3  christos #include "infrun.h"
     36   1.1  christos #include "gdbthread.h"
     37   1.1  christos #include "target.h"
     38   1.1  christos #include "language.h"
     39   1.1  christos #include "gdb-demangle.h"
     40   1.1  christos #include "filenames.h"
     41   1.1  christos #include "annotate.h"
     42   1.1  christos #include "symfile.h"
     43   1.1  christos #include "objfiles.h"
     44   1.1  christos #include "source.h"
     45   1.1  christos #include "linespec.h"
     46   1.1  christos #include "completer.h"
     47   1.1  christos #include "ui-out.h"
     48   1.1  christos #include "cli/cli-script.h"
     49   1.1  christos #include "block.h"
     50   1.1  christos #include "solib.h"
     51   1.1  christos #include "solist.h"
     52   1.8  christos #include "observable.h"
     53   1.1  christos #include "memattr.h"
     54   1.1  christos #include "ada-lang.h"
     55   1.1  christos #include "top.h"
     56  1.11  christos #include "ui.h"
     57   1.1  christos #include "valprint.h"
     58   1.1  christos #include "jit.h"
     59   1.1  christos #include "parser-defs.h"
     60  1.10  christos #include "gdbsupport/gdb_regex.h"
     61   1.1  christos #include "probe.h"
     62   1.1  christos #include "cli/cli-utils.h"
     63   1.1  christos #include "stack.h"
     64   1.1  christos #include "ax-gdb.h"
     65   1.1  christos #include "dummy-frame.h"
     66   1.3  christos #include "interps.h"
     67   1.9  christos #include "gdbsupport/format.h"
     68   1.6  christos #include "thread-fsm.h"
     69   1.6  christos #include "tid-parse.h"
     70   1.8  christos #include "cli/cli-style.h"
     71  1.10  christos #include "cli/cli-decode.h"
     72  1.11  christos #include <unordered_set>
     73   1.1  christos 
     74   1.1  christos /* readline include files */
     75   1.9  christos #include "readline/tilde.h"
     76   1.1  christos 
     77   1.1  christos /* readline defines this.  */
     78   1.1  christos #undef savestring
     79   1.1  christos 
     80   1.1  christos #include "mi/mi-common.h"
     81   1.3  christos #include "extension.h"
     82   1.7  christos #include <algorithm>
     83   1.8  christos #include "progspace-and-thread.h"
     84   1.9  christos #include "gdbsupport/array-view.h"
     85  1.11  christos #include <optional>
     86  1.11  christos #include "gdbsupport/common-utils.h"
     87   1.1  christos 
     88   1.1  christos /* Prototypes for local functions.  */
     89   1.1  christos 
     90   1.7  christos static void map_breakpoint_numbers (const char *,
     91   1.8  christos 				    gdb::function_view<void (breakpoint *)>);
     92   1.1  christos 
     93  1.10  christos static void
     94  1.10  christos   create_sals_from_location_spec_default (location_spec *locspec,
     95  1.10  christos 					  linespec_result *canonical);
     96   1.1  christos 
     97  1.10  christos static void create_breakpoints_sal (struct gdbarch *,
     98  1.10  christos 				    struct linespec_result *,
     99  1.10  christos 				    gdb::unique_xmalloc_ptr<char>,
    100  1.10  christos 				    gdb::unique_xmalloc_ptr<char>,
    101  1.10  christos 				    enum bptype,
    102  1.11  christos 				    enum bpdisp, int, int, int,
    103  1.10  christos 				    int,
    104  1.10  christos 				    int, int, int, unsigned);
    105   1.1  christos 
    106   1.8  christos static int can_use_hardware_watchpoint
    107   1.8  christos     (const std::vector<value_ref_ptr> &vals);
    108   1.1  christos 
    109  1.10  christos static void mention (const breakpoint *);
    110   1.1  christos 
    111  1.10  christos static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
    112  1.10  christos 
    113  1.10  christos static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
    114   1.1  christos 
    115   1.1  christos static struct breakpoint *
    116   1.1  christos   momentary_breakpoint_from_master (struct breakpoint *orig,
    117   1.1  christos 				    enum bptype type,
    118  1.10  christos 				    int loc_enabled, int thread);
    119   1.1  christos 
    120  1.10  christos static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
    121   1.1  christos 
    122   1.1  christos static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
    123   1.1  christos 					    CORE_ADDR bpaddr,
    124  1.10  christos 					    enum bptype bptype,
    125  1.10  christos 					    struct program_space *pspace);
    126  1.10  christos 
    127  1.10  christos static bool watchpoint_locations_match (const struct bp_location *loc1,
    128  1.10  christos 					const struct bp_location *loc2);
    129   1.1  christos 
    130  1.10  christos static bool breakpoint_locations_match (const struct bp_location *loc1,
    131  1.10  christos 					const struct bp_location *loc2,
    132  1.10  christos 					bool sw_hw_bps_match = false);
    133  1.10  christos 
    134  1.10  christos static bool breakpoint_location_address_match (struct bp_location *bl,
    135  1.10  christos 					       const struct address_space *aspace,
    136  1.10  christos 					       CORE_ADDR addr);
    137  1.10  christos 
    138  1.10  christos static bool breakpoint_location_address_range_overlap (struct bp_location *,
    139  1.10  christos 						       const address_space *,
    140  1.10  christos 						       CORE_ADDR, int);
    141   1.6  christos 
    142   1.6  christos static int remove_breakpoint (struct bp_location *);
    143   1.6  christos static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
    144   1.1  christos 
    145  1.10  christos static enum print_stop_action print_bp_stop_message (bpstat *bs);
    146   1.1  christos 
    147   1.1  christos static int hw_breakpoint_used_count (void);
    148   1.1  christos 
    149   1.1  christos static int hw_watchpoint_use_count (struct breakpoint *);
    150   1.1  christos 
    151   1.1  christos static int hw_watchpoint_used_count_others (struct breakpoint *except,
    152   1.1  christos 					    enum bptype type,
    153   1.1  christos 					    int *other_type_used);
    154   1.1  christos 
    155   1.1  christos static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
    156   1.1  christos 				    int count);
    157   1.1  christos 
    158   1.1  christos static void decref_bp_location (struct bp_location **loc);
    159   1.1  christos 
    160  1.10  christos static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
    161  1.10  christos      (struct breakpoint *b,
    162  1.10  christos       location_spec *locspec,
    163  1.10  christos       struct program_space *search_pspace);
    164  1.10  christos 
    165  1.10  christos static bool bl_address_is_meaningful (bp_location *loc);
    166  1.10  christos 
    167  1.10  christos static int find_loc_num_by_location (const bp_location *loc);
    168   1.1  christos 
    169   1.3  christos /* update_global_location_list's modes of operation wrt to whether to
    170   1.3  christos    insert locations now.  */
    171   1.3  christos enum ugll_insert_mode
    172   1.3  christos {
    173   1.3  christos   /* Don't insert any breakpoint locations into the inferior, only
    174   1.3  christos      remove already-inserted locations that no longer should be
    175   1.3  christos      inserted.  Functions that delete a breakpoint or breakpoints
    176   1.3  christos      should specify this mode, so that deleting a breakpoint doesn't
    177   1.3  christos      have the side effect of inserting the locations of other
    178   1.3  christos      breakpoints that are marked not-inserted, but should_be_inserted
    179   1.3  christos      returns true on them.
    180   1.3  christos 
    181   1.3  christos      This behavior is useful is situations close to tear-down -- e.g.,
    182   1.3  christos      after an exec, while the target still has execution, but
    183   1.3  christos      breakpoint shadows of the previous executable image should *NOT*
    184   1.3  christos      be restored to the new image; or before detaching, where the
    185   1.3  christos      target still has execution and wants to delete breakpoints from
    186   1.3  christos      GDB's lists, and all breakpoints had already been removed from
    187   1.3  christos      the inferior.  */
    188   1.3  christos   UGLL_DONT_INSERT,
    189   1.3  christos 
    190   1.3  christos   /* May insert breakpoints iff breakpoints_should_be_inserted_now
    191   1.3  christos      claims breakpoints should be inserted now.  */
    192   1.3  christos   UGLL_MAY_INSERT,
    193   1.3  christos 
    194   1.3  christos   /* Insert locations now, irrespective of
    195   1.3  christos      breakpoints_should_be_inserted_now.  E.g., say all threads are
    196   1.3  christos      stopped right now, and the user did "continue".  We need to
    197   1.3  christos      insert breakpoints _before_ resuming the target, but
    198   1.3  christos      UGLL_MAY_INSERT wouldn't insert them, because
    199   1.3  christos      breakpoints_should_be_inserted_now returns false at that point,
    200   1.3  christos      as no thread is running yet.  */
    201   1.3  christos   UGLL_INSERT
    202   1.3  christos };
    203   1.3  christos 
    204  1.11  christos /* Return a textual version of INSERT_MODE.  */
    205  1.11  christos 
    206  1.11  christos static const char *
    207  1.11  christos ugll_insert_mode_text (ugll_insert_mode insert_mode)
    208  1.11  christos {
    209  1.11  christos /* Make sure the compiler warns if a new ugll_insert_mode enumerator is added
    210  1.11  christos    but not handled here.  */
    211  1.11  christos DIAGNOSTIC_PUSH
    212  1.11  christos DIAGNOSTIC_ERROR_SWITCH
    213  1.11  christos   switch (insert_mode)
    214  1.11  christos     {
    215  1.11  christos     case UGLL_DONT_INSERT:
    216  1.11  christos       return "UGLL_DONT_INSERT";
    217  1.11  christos     case UGLL_MAY_INSERT:
    218  1.11  christos       return "UGLL_MAY_INSERT";
    219  1.11  christos     case UGLL_INSERT:
    220  1.11  christos       return "UGLL_INSERT";
    221  1.11  christos     }
    222  1.11  christos DIAGNOSTIC_POP
    223  1.11  christos 
    224  1.11  christos   gdb_assert_not_reached ("must handle all enum values");
    225  1.11  christos }
    226  1.11  christos 
    227  1.11  christos /* Return a textual version of REASON.  */
    228  1.11  christos 
    229  1.11  christos static const char *
    230  1.11  christos remove_bp_reason_str (remove_bp_reason reason)
    231  1.11  christos {
    232  1.11  christos /* Make sure the compiler warns if a new remove_bp_reason enumerator is added
    233  1.11  christos    but not handled here.  */
    234  1.11  christos DIAGNOSTIC_PUSH
    235  1.11  christos DIAGNOSTIC_ERROR_SWITCH
    236  1.11  christos   switch (reason)
    237  1.11  christos     {
    238  1.11  christos     case REMOVE_BREAKPOINT:
    239  1.11  christos       return "regular remove";
    240  1.11  christos     case DETACH_BREAKPOINT:
    241  1.11  christos       return "detach";
    242  1.11  christos     }
    243  1.11  christos DIAGNOSTIC_POP
    244  1.11  christos 
    245  1.11  christos   gdb_assert_not_reached ("must handle all enum values");
    246  1.11  christos }
    247  1.11  christos 
    248  1.11  christos /* Return a textual version of breakpoint location BL describing number,
    249  1.11  christos    location and address.  */
    250  1.11  christos 
    251  1.11  christos static std::string
    252  1.11  christos breakpoint_location_address_str (const bp_location *bl)
    253  1.11  christos {
    254  1.11  christos   std::string str = string_printf ("Breakpoint %d (%s) at address %s",
    255  1.11  christos 				   bl->owner->number,
    256  1.11  christos 				   host_address_to_string (bl),
    257  1.11  christos 				   paddress (bl->gdbarch, bl->address));
    258  1.11  christos 
    259  1.11  christos   std::string loc_string = bl->to_string ();
    260  1.11  christos   if (!loc_string.empty ())
    261  1.11  christos     str += string_printf (" %s", loc_string.c_str ());
    262  1.11  christos 
    263  1.11  christos   return str;
    264  1.11  christos }
    265  1.11  christos 
    266   1.3  christos static void update_global_location_list (enum ugll_insert_mode);
    267   1.1  christos 
    268   1.3  christos static void update_global_location_list_nothrow (enum ugll_insert_mode);
    269   1.1  christos 
    270   1.1  christos static void insert_breakpoint_locations (void);
    271   1.1  christos 
    272   1.8  christos static void trace_pass_command (const char *, int);
    273   1.1  christos 
    274   1.1  christos static void set_tracepoint_count (int num);
    275   1.1  christos 
    276   1.9  christos static bool is_masked_watchpoint (const struct breakpoint *b);
    277   1.1  christos 
    278  1.10  christos /* Return true if B refers to a static tracepoint set by marker ("-m"),
    279  1.10  christos    zero otherwise.  */
    280  1.10  christos 
    281  1.10  christos static bool strace_marker_p (struct breakpoint *b);
    282  1.10  christos 
    283  1.10  christos static void bkpt_probe_create_sals_from_location_spec
    284  1.10  christos      (location_spec *locspec,
    285  1.10  christos       struct linespec_result *canonical);
    286  1.10  christos static void tracepoint_probe_create_sals_from_location_spec
    287  1.10  christos      (location_spec *locspec,
    288  1.10  christos       struct linespec_result *canonical);
    289  1.10  christos 
    290  1.10  christos const struct breakpoint_ops code_breakpoint_ops =
    291  1.10  christos {
    292  1.10  christos   create_sals_from_location_spec_default,
    293  1.10  christos   create_breakpoints_sal,
    294  1.10  christos };
    295  1.10  christos 
    296  1.10  christos /* Breakpoints set on probes.  */
    297  1.10  christos static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
    298  1.10  christos {
    299  1.10  christos   bkpt_probe_create_sals_from_location_spec,
    300  1.10  christos   create_breakpoints_sal,
    301  1.10  christos };
    302  1.10  christos 
    303  1.10  christos /* Tracepoints set on probes.  */
    304  1.10  christos static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
    305  1.10  christos {
    306  1.10  christos   tracepoint_probe_create_sals_from_location_spec,
    307  1.10  christos   create_breakpoints_sal,
    308  1.10  christos };
    309  1.10  christos 
    310  1.10  christos /* Implementation of abstract dtors.  These must exist to satisfy the
    311  1.10  christos    linker.  */
    312  1.10  christos 
    313  1.10  christos breakpoint::~breakpoint ()
    314  1.10  christos {
    315  1.10  christos }
    316  1.10  christos 
    317  1.10  christos code_breakpoint::~code_breakpoint ()
    318  1.10  christos {
    319  1.10  christos }
    320  1.10  christos 
    321  1.10  christos catchpoint::~catchpoint ()
    322  1.10  christos {
    323  1.10  christos }
    324  1.10  christos 
    325  1.10  christos /* The structure to be used in regular breakpoints.  */
    326  1.10  christos struct ordinary_breakpoint : public code_breakpoint
    327  1.10  christos {
    328  1.10  christos   using code_breakpoint::code_breakpoint;
    329  1.10  christos 
    330  1.10  christos   int resources_needed (const struct bp_location *) override;
    331  1.10  christos   enum print_stop_action print_it (const bpstat *bs) const override;
    332  1.10  christos   void print_mention () const override;
    333  1.10  christos   void print_recreate (struct ui_file *fp) const override;
    334  1.10  christos };
    335  1.10  christos 
    336  1.10  christos /* Internal breakpoints.  These typically have a lifetime the same as
    337  1.10  christos    the program, and they end up installed on the breakpoint chain with
    338  1.10  christos    a negative breakpoint number.  They're visible in "maint info
    339  1.10  christos    breakpoints", but not "info breakpoints".  */
    340  1.10  christos struct internal_breakpoint : public code_breakpoint
    341  1.10  christos {
    342  1.10  christos   internal_breakpoint (struct gdbarch *gdbarch,
    343  1.10  christos 		       enum bptype type, CORE_ADDR address)
    344  1.10  christos     : code_breakpoint (gdbarch, type)
    345  1.10  christos   {
    346  1.10  christos     symtab_and_line sal;
    347  1.10  christos     sal.pc = address;
    348  1.10  christos     sal.section = find_pc_overlay (sal.pc);
    349  1.10  christos     sal.pspace = current_program_space;
    350  1.10  christos     add_location (sal);
    351  1.10  christos 
    352  1.10  christos     pspace = current_program_space;
    353  1.10  christos     disposition = disp_donttouch;
    354  1.10  christos   }
    355  1.10  christos 
    356  1.10  christos   void re_set () override;
    357  1.10  christos   void check_status (struct bpstat *bs) override;
    358  1.10  christos   enum print_stop_action print_it (const bpstat *bs) const override;
    359  1.10  christos   void print_mention () const override;
    360  1.10  christos };
    361  1.10  christos 
    362  1.10  christos /* Momentary breakpoints.  These typically have a lifetime of some run
    363  1.10  christos    control command only, are always thread-specific, and have 0 for
    364  1.10  christos    breakpoint number.  I.e., there can be many momentary breakpoints
    365  1.10  christos    on the breakpoint chain and they all same the same number (zero).
    366  1.10  christos    They're visible in "maint info breakpoints", but not "info
    367  1.10  christos    breakpoints".  */
    368  1.10  christos struct momentary_breakpoint : public code_breakpoint
    369  1.10  christos {
    370  1.10  christos   momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
    371  1.10  christos 			program_space *pspace_,
    372  1.10  christos 			const struct frame_id &frame_id_,
    373  1.10  christos 			int thread_)
    374  1.10  christos     : code_breakpoint (gdbarch_, bptype)
    375  1.10  christos   {
    376  1.10  christos     /* If FRAME_ID is valid, it should be a real frame, not an inlined
    377  1.10  christos        or tail-called one.  */
    378  1.10  christos     gdb_assert (!frame_id_artificial_p (frame_id));
    379  1.10  christos 
    380  1.10  christos     /* Momentary breakpoints are always thread-specific.  */
    381  1.10  christos     gdb_assert (thread_ > 0);
    382  1.10  christos 
    383  1.10  christos     pspace = pspace_;
    384  1.10  christos     enable_state = bp_enabled;
    385  1.10  christos     disposition = disp_donttouch;
    386  1.10  christos     frame_id = frame_id_;
    387  1.10  christos     thread = thread_;
    388  1.11  christos 
    389  1.11  christos     /* The inferior should have been set by the parent constructor.  */
    390  1.11  christos     gdb_assert (inferior == -1);
    391  1.10  christos   }
    392   1.1  christos 
    393  1.10  christos   void re_set () override;
    394  1.10  christos   void check_status (struct bpstat *bs) override;
    395  1.10  christos   enum print_stop_action print_it (const bpstat *bs) const override;
    396  1.10  christos   void print_mention () const override;
    397  1.10  christos };
    398   1.1  christos 
    399  1.10  christos /* DPrintf breakpoints.  */
    400  1.10  christos struct dprintf_breakpoint : public ordinary_breakpoint
    401  1.10  christos {
    402  1.10  christos   using ordinary_breakpoint::ordinary_breakpoint;
    403   1.1  christos 
    404  1.10  christos   void re_set () override;
    405  1.10  christos   int breakpoint_hit (const struct bp_location *bl,
    406  1.10  christos 		      const address_space *aspace,
    407  1.10  christos 		      CORE_ADDR bp_addr,
    408  1.10  christos 		      const target_waitstatus &ws) override;
    409  1.10  christos   void print_recreate (struct ui_file *fp) const override;
    410  1.10  christos   void after_condition_true (struct bpstat *bs) override;
    411  1.10  christos };
    412   1.1  christos 
    413  1.10  christos /* Ranged breakpoints.  */
    414  1.10  christos struct ranged_breakpoint : public ordinary_breakpoint
    415  1.10  christos {
    416  1.10  christos   explicit ranged_breakpoint (struct gdbarch *gdbarch,
    417  1.10  christos 			      const symtab_and_line &sal_start,
    418  1.10  christos 			      int length,
    419  1.10  christos 			      location_spec_up start_locspec,
    420  1.10  christos 			      location_spec_up end_locspec)
    421  1.10  christos     : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
    422  1.10  christos   {
    423  1.10  christos     bp_location *bl = add_location (sal_start);
    424  1.10  christos     bl->length = length;
    425   1.1  christos 
    426  1.10  christos     disposition = disp_donttouch;
    427   1.1  christos 
    428  1.10  christos     locspec = std::move (start_locspec);
    429  1.10  christos     locspec_range_end = std::move (end_locspec);
    430  1.10  christos   }
    431   1.1  christos 
    432  1.10  christos   int breakpoint_hit (const struct bp_location *bl,
    433  1.10  christos 		      const address_space *aspace,
    434  1.10  christos 		      CORE_ADDR bp_addr,
    435  1.10  christos 		      const target_waitstatus &ws) override;
    436  1.10  christos   int resources_needed (const struct bp_location *) override;
    437  1.10  christos   enum print_stop_action print_it (const bpstat *bs) const override;
    438  1.11  christos   bool print_one (const bp_location **) const override;
    439  1.10  christos   void print_one_detail (struct ui_out *) const override;
    440  1.10  christos   void print_mention () const override;
    441  1.10  christos   void print_recreate (struct ui_file *fp) const override;
    442  1.10  christos };
    443   1.1  christos 
    444  1.10  christos /* Static tracepoints with marker (`-m').  */
    445  1.10  christos struct static_marker_tracepoint : public tracepoint
    446  1.10  christos {
    447  1.10  christos   using tracepoint::tracepoint;
    448   1.9  christos 
    449  1.10  christos   std::vector<symtab_and_line> decode_location_spec
    450  1.10  christos        (struct location_spec *locspec,
    451  1.10  christos 	struct program_space *search_pspace) override;
    452  1.10  christos };
    453   1.1  christos 
    454   1.1  christos /* The style in which to perform a dynamic printf.  This is a user
    455   1.1  christos    option because different output options have different tradeoffs;
    456   1.1  christos    if GDB does the printing, there is better error handling if there
    457   1.1  christos    is a problem with any of the arguments, but using an inferior
    458   1.1  christos    function lets you have special-purpose printers and sending of
    459   1.1  christos    output to the same place as compiled-in print functions.  */
    460   1.1  christos 
    461   1.1  christos static const char dprintf_style_gdb[] = "gdb";
    462   1.1  christos static const char dprintf_style_call[] = "call";
    463   1.1  christos static const char dprintf_style_agent[] = "agent";
    464   1.1  christos static const char *const dprintf_style_enums[] = {
    465   1.1  christos   dprintf_style_gdb,
    466   1.1  christos   dprintf_style_call,
    467   1.1  christos   dprintf_style_agent,
    468   1.1  christos   NULL
    469   1.1  christos };
    470   1.1  christos static const char *dprintf_style = dprintf_style_gdb;
    471   1.1  christos 
    472   1.1  christos /* The function to use for dynamic printf if the preferred style is to
    473   1.1  christos    call into the inferior.  The value is simply a string that is
    474   1.1  christos    copied into the command, so it can be anything that GDB can
    475   1.1  christos    evaluate to a callable address, not necessarily a function name.  */
    476   1.1  christos 
    477  1.10  christos static std::string dprintf_function = "printf";
    478   1.1  christos 
    479   1.1  christos /* The channel to use for dynamic printf if the preferred style is to
    480   1.1  christos    call into the inferior; if a nonempty string, it will be passed to
    481   1.1  christos    the call as the first argument, with the format string as the
    482   1.1  christos    second.  As with the dprintf function, this can be anything that
    483   1.1  christos    GDB knows how to evaluate, so in addition to common choices like
    484   1.1  christos    "stderr", this could be an app-specific expression like
    485   1.1  christos    "mystreams[curlogger]".  */
    486   1.1  christos 
    487  1.10  christos static std::string dprintf_channel;
    488   1.1  christos 
    489   1.1  christos /* True if dprintf commands should continue to operate even if GDB
    490   1.1  christos    has disconnected.  */
    491   1.9  christos static bool disconnected_dprintf = true;
    492   1.1  christos 
    493   1.1  christos struct command_line *
    494   1.1  christos breakpoint_commands (struct breakpoint *b)
    495   1.1  christos {
    496   1.8  christos   return b->commands ? b->commands.get () : NULL;
    497   1.1  christos }
    498   1.1  christos 
    499   1.1  christos /* Flag indicating that a command has proceeded the inferior past the
    500   1.1  christos    current breakpoint.  */
    501   1.1  christos 
    502   1.9  christos static bool breakpoint_proceeded;
    503   1.1  christos 
    504   1.1  christos const char *
    505   1.1  christos bpdisp_text (enum bpdisp disp)
    506   1.1  christos {
    507   1.1  christos   /* NOTE: the following values are a part of MI protocol and
    508   1.1  christos      represent values of 'disp' field returned when inferior stops at
    509   1.1  christos      a breakpoint.  */
    510   1.1  christos   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
    511   1.1  christos 
    512   1.1  christos   return bpdisps[(int) disp];
    513   1.1  christos }
    514   1.1  christos 
    515   1.1  christos /* Prototypes for exported functions.  */
    516   1.1  christos /* If FALSE, gdb will not use hardware support for watchpoints, even
    517   1.1  christos    if such is available.  */
    518   1.1  christos static int can_use_hw_watchpoints;
    519   1.1  christos 
    520   1.1  christos static void
    521   1.1  christos show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
    522   1.1  christos 			     struct cmd_list_element *c,
    523   1.1  christos 			     const char *value)
    524   1.1  christos {
    525  1.10  christos   gdb_printf (file,
    526  1.10  christos 	      _("Debugger's willingness to use "
    527  1.10  christos 		"watchpoint hardware is %s.\n"),
    528  1.10  christos 	      value);
    529   1.1  christos }
    530   1.1  christos 
    531   1.1  christos /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
    532   1.1  christos    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
    533   1.1  christos    for unrecognized breakpoint locations.
    534   1.1  christos    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
    535   1.1  christos static enum auto_boolean pending_break_support;
    536   1.1  christos static void
    537   1.1  christos show_pending_break_support (struct ui_file *file, int from_tty,
    538   1.1  christos 			    struct cmd_list_element *c,
    539   1.1  christos 			    const char *value)
    540   1.1  christos {
    541  1.10  christos   gdb_printf (file,
    542  1.10  christos 	      _("Debugger's behavior regarding "
    543  1.10  christos 		"pending breakpoints is %s.\n"),
    544  1.10  christos 	      value);
    545   1.1  christos }
    546   1.1  christos 
    547   1.9  christos /* If true, gdb will automatically use hardware breakpoints for breakpoints
    548   1.1  christos    set with "break" but falling in read-only memory.
    549   1.9  christos    If false, gdb will warn about such breakpoints, but won't automatically
    550   1.1  christos    use hardware breakpoints.  */
    551   1.9  christos static bool automatic_hardware_breakpoints;
    552   1.1  christos static void
    553   1.1  christos show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
    554   1.1  christos 				     struct cmd_list_element *c,
    555   1.1  christos 				     const char *value)
    556   1.1  christos {
    557  1.10  christos   gdb_printf (file,
    558  1.10  christos 	      _("Automatic usage of hardware breakpoints is %s.\n"),
    559  1.10  christos 	      value);
    560   1.1  christos }
    561   1.1  christos 
    562   1.3  christos /* If on, GDB keeps breakpoints inserted even if the inferior is
    563   1.3  christos    stopped, and immediately inserts any new breakpoints as soon as
    564   1.3  christos    they're created.  If off (default), GDB keeps breakpoints off of
    565   1.3  christos    the target as long as possible.  That is, it delays inserting
    566   1.3  christos    breakpoints until the next resume, and removes them again when the
    567   1.3  christos    target fully stops.  This is a bit safer in case GDB crashes while
    568   1.3  christos    processing user input.  */
    569   1.9  christos static bool always_inserted_mode = false;
    570   1.1  christos 
    571   1.1  christos static void
    572   1.1  christos show_always_inserted_mode (struct ui_file *file, int from_tty,
    573   1.1  christos 		     struct cmd_list_element *c, const char *value)
    574   1.1  christos {
    575  1.10  christos   gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
    576  1.10  christos 	      value);
    577   1.1  christos }
    578   1.1  christos 
    579  1.11  christos /* True if breakpoint debug output is enabled.  */
    580  1.11  christos static bool debug_breakpoint = false;
    581  1.11  christos 
    582  1.11  christos /* Print a "breakpoint" debug statement.  */
    583  1.11  christos #define breakpoint_debug_printf(fmt, ...) \
    584  1.11  christos   debug_prefixed_printf_cond (debug_breakpoint, "breakpoint", fmt, \
    585  1.11  christos 			      ##__VA_ARGS__)
    586  1.11  christos 
    587  1.11  christos /* "show debug breakpoint" implementation.  */
    588  1.11  christos static void
    589  1.11  christos show_debug_breakpoint (struct ui_file *file, int from_tty,
    590  1.11  christos 		       struct cmd_list_element *c, const char *value)
    591  1.11  christos {
    592  1.11  christos   gdb_printf (file, _("Breakpoint location debugging is %s.\n"), value);
    593  1.11  christos }
    594  1.11  christos 
    595   1.3  christos /* See breakpoint.h.  */
    596   1.3  christos 
    597   1.1  christos int
    598   1.3  christos breakpoints_should_be_inserted_now (void)
    599   1.1  christos {
    600  1.11  christos   if (gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
    601   1.3  christos     {
    602   1.3  christos       /* If breakpoints are global, they should be inserted even if no
    603   1.3  christos 	 thread under gdb's control is running, or even if there are
    604   1.3  christos 	 no threads under GDB's control yet.  */
    605   1.3  christos       return 1;
    606   1.3  christos     }
    607   1.9  christos   else
    608   1.3  christos     {
    609   1.3  christos       if (always_inserted_mode)
    610   1.3  christos 	{
    611   1.3  christos 	  /* The user wants breakpoints inserted even if all threads
    612   1.3  christos 	     are stopped.  */
    613   1.3  christos 	  return 1;
    614   1.3  christos 	}
    615   1.3  christos 
    616   1.9  christos       for (inferior *inf : all_inferiors ())
    617   1.9  christos 	if (inf->has_execution ()
    618   1.9  christos 	    && threads_are_executing (inf->process_target ()))
    619   1.9  christos 	  return 1;
    620   1.6  christos 
    621   1.6  christos       /* Don't remove breakpoints yet if, even though all threads are
    622   1.6  christos 	 stopped, we still have events to process.  */
    623   1.8  christos       for (thread_info *tp : all_non_exited_threads ())
    624  1.10  christos 	if (tp->resumed () && tp->has_pending_waitstatus ())
    625   1.6  christos 	  return 1;
    626   1.3  christos     }
    627   1.3  christos   return 0;
    628   1.1  christos }
    629   1.1  christos 
    630   1.1  christos static const char condition_evaluation_both[] = "host or target";
    631   1.1  christos 
    632   1.1  christos /* Modes for breakpoint condition evaluation.  */
    633   1.1  christos static const char condition_evaluation_auto[] = "auto";
    634   1.1  christos static const char condition_evaluation_host[] = "host";
    635   1.1  christos static const char condition_evaluation_target[] = "target";
    636   1.1  christos static const char *const condition_evaluation_enums[] = {
    637   1.1  christos   condition_evaluation_auto,
    638   1.1  christos   condition_evaluation_host,
    639   1.1  christos   condition_evaluation_target,
    640   1.1  christos   NULL
    641   1.1  christos };
    642   1.1  christos 
    643   1.1  christos /* Global that holds the current mode for breakpoint condition evaluation.  */
    644   1.1  christos static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
    645   1.1  christos 
    646   1.1  christos /* Global that we use to display information to the user (gets its value from
    647   1.1  christos    condition_evaluation_mode_1.  */
    648   1.1  christos static const char *condition_evaluation_mode = condition_evaluation_auto;
    649   1.1  christos 
    650   1.1  christos /* Translate a condition evaluation mode MODE into either "host"
    651   1.1  christos    or "target".  This is used mostly to translate from "auto" to the
    652   1.1  christos    real setting that is being used.  It returns the translated
    653   1.1  christos    evaluation mode.  */
    654   1.1  christos 
    655   1.1  christos static const char *
    656   1.1  christos translate_condition_evaluation_mode (const char *mode)
    657   1.1  christos {
    658   1.1  christos   if (mode == condition_evaluation_auto)
    659   1.1  christos     {
    660   1.1  christos       if (target_supports_evaluation_of_breakpoint_conditions ())
    661   1.1  christos 	return condition_evaluation_target;
    662   1.1  christos       else
    663   1.1  christos 	return condition_evaluation_host;
    664   1.1  christos     }
    665   1.1  christos   else
    666   1.1  christos     return mode;
    667   1.1  christos }
    668   1.1  christos 
    669   1.1  christos /* Discovers what condition_evaluation_auto translates to.  */
    670   1.1  christos 
    671   1.1  christos static const char *
    672   1.1  christos breakpoint_condition_evaluation_mode (void)
    673   1.1  christos {
    674   1.1  christos   return translate_condition_evaluation_mode (condition_evaluation_mode);
    675   1.1  christos }
    676   1.1  christos 
    677   1.1  christos /* Return true if GDB should evaluate breakpoint conditions or false
    678   1.1  christos    otherwise.  */
    679   1.1  christos 
    680  1.10  christos static bool
    681   1.1  christos gdb_evaluates_breakpoint_condition_p (void)
    682   1.1  christos {
    683   1.1  christos   const char *mode = breakpoint_condition_evaluation_mode ();
    684   1.1  christos 
    685   1.1  christos   return (mode == condition_evaluation_host);
    686   1.1  christos }
    687   1.1  christos 
    688   1.1  christos /* Are we executing breakpoint commands?  */
    689   1.1  christos static int executing_breakpoint_commands;
    690   1.1  christos 
    691   1.1  christos /* Are overlay event breakpoints enabled? */
    692   1.1  christos static int overlay_events_enabled;
    693   1.1  christos 
    694   1.1  christos /* See description in breakpoint.h. */
    695   1.9  christos bool target_exact_watchpoints = false;
    696   1.1  christos 
    697   1.1  christos /* Chains of all breakpoints defined.  */
    698   1.1  christos 
    699  1.11  christos static intrusive_list<breakpoint> breakpoint_chain;
    700   1.1  christos 
    701  1.10  christos /* See breakpoint.h.  */
    702  1.10  christos 
    703  1.10  christos breakpoint_range
    704  1.10  christos all_breakpoints ()
    705  1.10  christos {
    706  1.11  christos   return breakpoint_range (breakpoint_chain.begin (), breakpoint_chain.end ());
    707  1.10  christos }
    708  1.10  christos 
    709  1.10  christos /* See breakpoint.h.  */
    710  1.10  christos 
    711  1.10  christos breakpoint_safe_range
    712  1.10  christos all_breakpoints_safe ()
    713  1.10  christos {
    714  1.10  christos   return breakpoint_safe_range (all_breakpoints ());
    715  1.10  christos }
    716  1.10  christos 
    717  1.10  christos /* See breakpoint.h.  */
    718  1.10  christos 
    719  1.10  christos tracepoint_range
    720  1.10  christos all_tracepoints ()
    721  1.10  christos {
    722  1.11  christos   return tracepoint_range (tracepoint_iterator (breakpoint_chain.begin ()),
    723  1.11  christos 			   tracepoint_iterator (breakpoint_chain.end ()));
    724  1.10  christos }
    725  1.10  christos 
    726   1.9  christos /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS.  */
    727   1.1  christos 
    728  1.10  christos static std::vector<bp_location *> bp_locations;
    729  1.10  christos 
    730  1.10  christos /* See breakpoint.h.  */
    731  1.10  christos 
    732  1.10  christos const std::vector<bp_location *> &
    733  1.10  christos all_bp_locations ()
    734  1.10  christos {
    735  1.10  christos   return bp_locations;
    736  1.10  christos }
    737  1.10  christos 
    738  1.10  christos /* Range to iterate over breakpoint locations at a given address.  */
    739  1.10  christos 
    740  1.10  christos struct bp_locations_at_addr_range
    741  1.10  christos {
    742  1.10  christos   using iterator = std::vector<bp_location *>::iterator;
    743  1.10  christos 
    744  1.10  christos   bp_locations_at_addr_range (CORE_ADDR addr)
    745  1.10  christos   {
    746  1.10  christos     struct compare
    747  1.10  christos     {
    748  1.10  christos       bool operator() (const bp_location *loc, CORE_ADDR addr_) const
    749  1.10  christos       { return loc->address < addr_; }
    750  1.10  christos 
    751  1.10  christos       bool operator() (CORE_ADDR addr_, const bp_location *loc) const
    752  1.10  christos       { return addr_ < loc->address; }
    753  1.10  christos     };
    754  1.10  christos 
    755  1.10  christos     auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
    756  1.10  christos 				     addr, compare ());
    757  1.10  christos 
    758  1.10  christos     m_begin = it_pair.first;
    759  1.10  christos     m_end = it_pair.second;
    760  1.10  christos   }
    761  1.10  christos 
    762  1.10  christos   iterator begin () const
    763  1.10  christos   { return m_begin; }
    764  1.10  christos 
    765  1.10  christos   iterator end () const
    766  1.10  christos   { return m_end; }
    767  1.10  christos 
    768  1.10  christos private:
    769  1.10  christos   iterator m_begin;
    770  1.10  christos   iterator m_end;
    771  1.10  christos };
    772  1.10  christos 
    773  1.10  christos /* Return a range to iterate over all breakpoint locations exactly at address
    774  1.10  christos    ADDR.
    775   1.1  christos 
    776  1.10  christos    If it's needed to iterate multiple times on the same range, it's possible
    777  1.10  christos    to save the range in a local variable and use it multiple times:
    778   1.1  christos 
    779  1.10  christos      auto range = all_bp_locations_at_addr (addr);
    780  1.10  christos 
    781  1.10  christos      for (bp_location *loc : range)
    782  1.10  christos        // use loc
    783  1.10  christos 
    784  1.10  christos      for (bp_location *loc : range)
    785  1.10  christos        // use loc
    786  1.10  christos 
    787  1.10  christos    This saves a bit of time, as it avoids re-doing the binary searches to find
    788  1.10  christos    the range's boundaries.  Just remember not to change the bp_locations vector
    789  1.10  christos    in the mean time, as it could make the range's iterators stale.  */
    790  1.10  christos 
    791  1.10  christos static bp_locations_at_addr_range
    792  1.10  christos all_bp_locations_at_addr (CORE_ADDR addr)
    793  1.10  christos {
    794  1.10  christos   return bp_locations_at_addr_range (addr);
    795  1.10  christos }
    796   1.1  christos 
    797   1.1  christos /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
    798   1.7  christos    ADDRESS for the current elements of BP_LOCATIONS which get a valid
    799   1.1  christos    result from bp_location_has_shadow.  You can use it for roughly
    800   1.7  christos    limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
    801   1.1  christos    an address you need to read.  */
    802   1.1  christos 
    803   1.7  christos static CORE_ADDR bp_locations_placed_address_before_address_max;
    804   1.1  christos 
    805   1.1  christos /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
    806   1.1  christos    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
    807   1.7  christos    BP_LOCATIONS which get a valid result from bp_location_has_shadow.
    808   1.7  christos    You can use it for roughly limiting the subrange of BP_LOCATIONS to
    809   1.1  christos    scan for shadow bytes for an address you need to read.  */
    810   1.1  christos 
    811   1.7  christos static CORE_ADDR bp_locations_shadow_len_after_address_max;
    812   1.1  christos 
    813   1.1  christos /* The locations that no longer correspond to any breakpoint, unlinked
    814   1.7  christos    from the bp_locations array, but for which a hit may still be
    815   1.7  christos    reported by a target.  */
    816   1.8  christos static std::vector<bp_location *> moribund_locations;
    817   1.1  christos 
    818   1.1  christos /* Number of last breakpoint made.  */
    819   1.1  christos 
    820   1.1  christos static int breakpoint_count;
    821   1.1  christos 
    822   1.1  christos /* The value of `breakpoint_count' before the last command that
    823   1.1  christos    created breakpoints.  If the last (break-like) command created more
    824   1.1  christos    than one breakpoint, then the difference between BREAKPOINT_COUNT
    825   1.1  christos    and PREV_BREAKPOINT_COUNT is more than one.  */
    826   1.1  christos static int prev_breakpoint_count;
    827   1.1  christos 
    828   1.1  christos /* Number of last tracepoint made.  */
    829   1.1  christos 
    830   1.1  christos static int tracepoint_count;
    831   1.1  christos 
    832   1.1  christos static struct cmd_list_element *breakpoint_set_cmdlist;
    833   1.1  christos static struct cmd_list_element *breakpoint_show_cmdlist;
    834   1.1  christos struct cmd_list_element *save_cmdlist;
    835   1.1  christos 
    836   1.1  christos /* Return whether a breakpoint is an active enabled breakpoint.  */
    837  1.10  christos static bool
    838   1.1  christos breakpoint_enabled (struct breakpoint *b)
    839   1.1  christos {
    840   1.1  christos   return (b->enable_state == bp_enabled);
    841   1.1  christos }
    842   1.1  christos 
    843   1.1  christos /* Set breakpoint count to NUM.  */
    844   1.1  christos 
    845   1.1  christos static void
    846   1.1  christos set_breakpoint_count (int num)
    847   1.1  christos {
    848   1.1  christos   prev_breakpoint_count = breakpoint_count;
    849   1.1  christos   breakpoint_count = num;
    850   1.1  christos   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
    851   1.1  christos }
    852   1.1  christos 
    853   1.1  christos /* Used by `start_rbreak_breakpoints' below, to record the current
    854   1.1  christos    breakpoint count before "rbreak" creates any breakpoint.  */
    855   1.1  christos static int rbreak_start_breakpoint_count;
    856   1.1  christos 
    857   1.1  christos /* Called at the start an "rbreak" command to record the first
    858   1.1  christos    breakpoint made.  */
    859   1.1  christos 
    860   1.8  christos scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
    861   1.1  christos {
    862   1.1  christos   rbreak_start_breakpoint_count = breakpoint_count;
    863   1.1  christos }
    864   1.1  christos 
    865   1.1  christos /* Called at the end of an "rbreak" command to record the last
    866   1.1  christos    breakpoint made.  */
    867   1.1  christos 
    868   1.8  christos scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
    869   1.1  christos {
    870   1.1  christos   prev_breakpoint_count = rbreak_start_breakpoint_count;
    871   1.1  christos }
    872   1.1  christos 
    873   1.1  christos /* Used in run_command to zero the hit count when a new run starts.  */
    874   1.1  christos 
    875   1.1  christos void
    876   1.1  christos clear_breakpoint_hit_counts (void)
    877   1.1  christos {
    878  1.11  christos   for (breakpoint &b : all_breakpoints ())
    879  1.11  christos     b.hit_count = 0;
    880   1.1  christos }
    881   1.1  christos 
    882   1.1  christos 
    883   1.1  christos /* Return the breakpoint with the specified number, or NULL
    885   1.1  christos    if the number does not refer to an existing breakpoint.  */
    886   1.1  christos 
    887   1.1  christos struct breakpoint *
    888   1.1  christos get_breakpoint (int num)
    889  1.11  christos {
    890  1.11  christos   for (breakpoint &b : all_breakpoints ())
    891  1.11  christos     if (b.number == num)
    892   1.1  christos       return &b;
    893  1.10  christos 
    894  1.10  christos   return nullptr;
    895  1.10  christos }
    896  1.10  christos 
    897  1.10  christos /* Return TRUE if NUM refer to an existing breakpoint that has
    898  1.10  christos    multiple code locations.  */
    899  1.10  christos 
    900  1.10  christos static bool
    901  1.10  christos has_multiple_locations (int num)
    902  1.11  christos {
    903  1.11  christos   for (breakpoint &b : all_breakpoints ())
    904  1.11  christos     if (b.number == num)
    905  1.10  christos       return b.has_multiple_locations ();
    906  1.10  christos 
    907   1.1  christos   return false;
    908   1.1  christos }
    909   1.1  christos 
    910   1.1  christos 
    911   1.1  christos 
    913   1.1  christos /* Mark locations as "conditions have changed" in case the target supports
    914   1.1  christos    evaluating conditions on its side.  */
    915   1.1  christos 
    916   1.1  christos static void
    917   1.1  christos mark_breakpoint_modified (struct breakpoint *b)
    918   1.1  christos {
    919   1.1  christos   /* This is only meaningful if the target is
    920   1.1  christos      evaluating conditions and if the user has
    921   1.1  christos      opted for condition evaluation on the target's
    922   1.1  christos      side.  */
    923   1.1  christos   if (gdb_evaluates_breakpoint_condition_p ()
    924   1.1  christos       || !target_supports_evaluation_of_breakpoint_conditions ())
    925   1.1  christos     return;
    926   1.1  christos 
    927   1.1  christos   if (!is_breakpoint (b))
    928  1.11  christos     return;
    929  1.11  christos 
    930   1.1  christos   for (bp_location &loc : b->locations ())
    931   1.1  christos     loc.condition_changed = condition_modified;
    932   1.1  christos }
    933   1.1  christos 
    934   1.1  christos /* Mark location as "conditions have changed" in case the target supports
    935   1.1  christos    evaluating conditions on its side.  */
    936   1.1  christos 
    937   1.1  christos static void
    938   1.1  christos mark_breakpoint_location_modified (struct bp_location *loc)
    939   1.1  christos {
    940   1.1  christos   /* This is only meaningful if the target is
    941   1.1  christos      evaluating conditions and if the user has
    942   1.1  christos      opted for condition evaluation on the target's
    943   1.1  christos      side.  */
    944   1.1  christos   if (gdb_evaluates_breakpoint_condition_p ()
    945   1.1  christos       || !target_supports_evaluation_of_breakpoint_conditions ())
    946   1.1  christos 
    947   1.1  christos     return;
    948   1.1  christos 
    949   1.1  christos   if (!is_breakpoint (loc->owner))
    950   1.1  christos     return;
    951   1.1  christos 
    952   1.1  christos   loc->condition_changed = condition_modified;
    953   1.1  christos }
    954   1.1  christos 
    955   1.1  christos /* Sets the condition-evaluation mode using the static global
    956   1.1  christos    condition_evaluation_mode.  */
    957   1.8  christos 
    958   1.1  christos static void
    959   1.1  christos set_condition_evaluation_mode (const char *args, int from_tty,
    960   1.1  christos 			       struct cmd_list_element *c)
    961   1.1  christos {
    962   1.1  christos   const char *old_mode, *new_mode;
    963   1.1  christos 
    964   1.1  christos   if ((condition_evaluation_mode_1 == condition_evaluation_target)
    965   1.1  christos       && !target_supports_evaluation_of_breakpoint_conditions ())
    966   1.1  christos     {
    967   1.1  christos       condition_evaluation_mode_1 = condition_evaluation_mode;
    968   1.1  christos       warning (_("Target does not support breakpoint condition evaluation.\n"
    969   1.1  christos 		 "Using host evaluation mode instead."));
    970   1.1  christos       return;
    971   1.1  christos     }
    972   1.1  christos 
    973   1.1  christos   new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
    974   1.1  christos   old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
    975   1.1  christos 
    976   1.1  christos   /* Flip the switch.  Flip it even if OLD_MODE == NEW_MODE as one of the
    977   1.1  christos      settings was "auto".  */
    978   1.1  christos   condition_evaluation_mode = condition_evaluation_mode_1;
    979   1.1  christos 
    980   1.1  christos   /* Only update the mode if the user picked a different one.  */
    981   1.1  christos   if (new_mode != old_mode)
    982   1.1  christos     {
    983   1.1  christos       /* If the user switched to a different evaluation mode, we
    984   1.1  christos 	 need to synch the changes with the target as follows:
    985   1.1  christos 
    986   1.1  christos 	 "host" -> "target": Send all (valid) conditions to the target.
    987   1.1  christos 	 "target" -> "host": Remove all the conditions from the target.
    988   1.1  christos       */
    989   1.1  christos 
    990   1.1  christos       if (new_mode == condition_evaluation_target)
    991   1.1  christos 	{
    992  1.10  christos 	  /* Mark everything modified and synch conditions with the
    993   1.1  christos 	     target.  */
    994  1.10  christos 	  for (bp_location *loc : all_bp_locations ())
    995   1.1  christos 	    mark_breakpoint_location_modified (loc);
    996   1.1  christos 	}
    997   1.1  christos       else
    998   1.1  christos 	{
    999   1.1  christos 	  /* Manually mark non-duplicate locations to synch conditions
   1000  1.10  christos 	     with the target.  We do this to remove all the conditions the
   1001   1.1  christos 	     target knows about.  */
   1002   1.1  christos 	  for (bp_location *loc : all_bp_locations ())
   1003   1.1  christos 	    if (is_breakpoint (loc->owner) && loc->inserted)
   1004   1.1  christos 	      loc->needs_update = 1;
   1005   1.1  christos 	}
   1006   1.3  christos 
   1007   1.1  christos       /* Do the update.  */
   1008   1.1  christos       update_global_location_list (UGLL_MAY_INSERT);
   1009   1.1  christos     }
   1010   1.1  christos 
   1011   1.1  christos   return;
   1012   1.1  christos }
   1013   1.1  christos 
   1014   1.1  christos /* Shows the current mode of breakpoint condition evaluation.  Explicitly shows
   1015   1.1  christos    what "auto" is translating to.  */
   1016   1.1  christos 
   1017   1.1  christos static void
   1018   1.1  christos show_condition_evaluation_mode (struct ui_file *file, int from_tty,
   1019   1.1  christos 				struct cmd_list_element *c, const char *value)
   1020  1.10  christos {
   1021  1.10  christos   if (condition_evaluation_mode == condition_evaluation_auto)
   1022  1.10  christos     gdb_printf (file,
   1023  1.10  christos 		_("Breakpoint condition evaluation "
   1024  1.10  christos 		  "mode is %s (currently %s).\n"),
   1025   1.1  christos 		value,
   1026  1.10  christos 		breakpoint_condition_evaluation_mode ());
   1027  1.10  christos   else
   1028   1.1  christos     gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
   1029   1.1  christos 		value);
   1030  1.10  christos }
   1031  1.10  christos 
   1032  1.10  christos /* Parse COND_STRING in the context of LOC and set as the condition
   1033  1.10  christos    expression of LOC.  BP_NUM is the number of LOC's owner, LOC_NUM is
   1034   1.1  christos    the number of LOC within its owner.  In case of parsing error, mark
   1035  1.10  christos    LOC as DISABLED_BY_COND.  In case of success, unset DISABLED_BY_COND.  */
   1036  1.10  christos 
   1037  1.10  christos static void
   1038   1.1  christos set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
   1039  1.10  christos 				   int bp_num, int loc_num)
   1040  1.10  christos {
   1041  1.10  christos   bool has_junk = false;
   1042  1.10  christos   try
   1043  1.10  christos     {
   1044  1.10  christos       expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
   1045  1.10  christos 					   block_for_pc (loc->address), 0);
   1046  1.10  christos       if (*cond_string != 0)
   1047  1.10  christos 	has_junk = true;
   1048  1.10  christos       else
   1049  1.10  christos 	{
   1050  1.10  christos 	  loc->cond = std::move (new_exp);
   1051  1.10  christos 	  if (loc->disabled_by_cond && loc->enabled)
   1052  1.10  christos 	    gdb_printf (_("Breakpoint %d's condition is now valid at "
   1053   1.1  christos 			  "location %d, enabling.\n"),
   1054  1.10  christos 			bp_num, loc_num);
   1055  1.10  christos 
   1056  1.10  christos 	  loc->disabled_by_cond = false;
   1057  1.10  christos 	}
   1058  1.10  christos     }
   1059  1.10  christos   catch (const gdb_exception_error &e)
   1060  1.10  christos     {
   1061  1.10  christos       if (loc->enabled)
   1062  1.10  christos 	{
   1063  1.10  christos 	  /* Warn if a user-enabled location is now becoming disabled-by-cond.
   1064  1.10  christos 	     BP_NUM is 0 if the breakpoint is being defined for the first
   1065  1.10  christos 	     time using the "break ... if ..." command, and non-zero if
   1066  1.10  christos 	     already defined.  */
   1067  1.10  christos 	  if (bp_num != 0)
   1068  1.10  christos 	    warning (_("failed to validate condition at location %d.%d, "
   1069  1.10  christos 		       "disabling:\n  %s"), bp_num, loc_num, e.what ());
   1070  1.10  christos 	  else
   1071  1.10  christos 	    warning (_("failed to validate condition at location %d, "
   1072   1.1  christos 		       "disabling:\n  %s"), loc_num, e.what ());
   1073  1.10  christos 	}
   1074  1.10  christos 
   1075   1.1  christos       loc->disabled_by_cond = true;
   1076  1.10  christos     }
   1077  1.10  christos 
   1078   1.1  christos   if (has_junk)
   1079   1.1  christos     error (_("Garbage '%s' follows condition"), cond_string);
   1080  1.11  christos }
   1081  1.11  christos 
   1082  1.11  christos /* See breakpoint.h.  */
   1083  1.11  christos 
   1084  1.11  christos void
   1085  1.11  christos notify_breakpoint_modified (breakpoint *b)
   1086  1.11  christos {
   1087  1.11  christos   interps_notify_breakpoint_modified (b);
   1088  1.11  christos   gdb::observers::breakpoint_modified.notify (b);
   1089   1.1  christos }
   1090   1.5  christos 
   1091  1.10  christos void
   1092   1.1  christos set_breakpoint_condition (struct breakpoint *b, const char *exp,
   1093   1.9  christos 			  int from_tty, bool force)
   1094   1.1  christos {
   1095  1.10  christos   if (*exp == 0)
   1096   1.1  christos     {
   1097   1.9  christos       b->cond_string.reset ();
   1098  1.10  christos 
   1099   1.9  christos       if (is_watchpoint (b))
   1100   1.1  christos 	gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
   1101  1.10  christos       else
   1102  1.11  christos 	{
   1103   1.9  christos 	  int loc_num = 1;
   1104  1.11  christos 	  for (bp_location &loc : b->locations ())
   1105  1.11  christos 	    {
   1106  1.10  christos 	      loc.cond.reset ();
   1107  1.10  christos 	      if (loc.disabled_by_cond && loc.enabled)
   1108  1.10  christos 		gdb_printf (_("Breakpoint %d's condition is now valid at "
   1109  1.11  christos 			      "location %d, enabling.\n"),
   1110  1.10  christos 			    b->number, loc_num);
   1111   1.1  christos 	      loc.disabled_by_cond = false;
   1112   1.9  christos 	      loc_num++;
   1113   1.9  christos 
   1114   1.9  christos 	      /* No need to free the condition agent expression
   1115   1.9  christos 		 bytecode (if we have one).  We will handle this
   1116   1.1  christos 		 when we go through update_global_location_list.  */
   1117   1.1  christos 	    }
   1118   1.1  christos 	}
   1119  1.10  christos 
   1120   1.1  christos       if (from_tty)
   1121   1.1  christos 	gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
   1122   1.1  christos     }
   1123   1.1  christos   else
   1124   1.1  christos     {
   1125   1.9  christos       if (is_watchpoint (b))
   1126   1.9  christos 	{
   1127   1.9  christos 	  innermost_block_tracker tracker;
   1128   1.9  christos 	  const char *arg = exp;
   1129   1.1  christos 	  expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
   1130  1.10  christos 	  if (*arg != 0)
   1131   1.9  christos 	    error (_("Junk at end of expression"));
   1132   1.9  christos 	  watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
   1133   1.1  christos 	  w->cond_exp = std::move (new_exp);
   1134   1.1  christos 	  w->cond_exp_valid_block = tracker.block ();
   1135   1.1  christos 	}
   1136   1.9  christos       else
   1137   1.9  christos 	{
   1138  1.10  christos 	  /* Parse and set condition expressions.  We make two passes.
   1139  1.10  christos 	     In the first, we parse the condition string to see if it
   1140  1.10  christos 	     is valid in at least one location.  If so, the condition
   1141   1.9  christos 	     would be accepted.  So we go ahead and set the locations'
   1142   1.9  christos 	     conditions.  In case no valid case is found, we throw
   1143   1.9  christos 	     the error and the condition string will be rejected.
   1144  1.11  christos 	     This two-pass approach is taken to avoid setting the
   1145  1.10  christos 	     state of locations in case of a reject.  */
   1146  1.10  christos 	  for (const bp_location &loc : b->locations ())
   1147  1.10  christos 	    {
   1148  1.10  christos 	      try
   1149  1.11  christos 		{
   1150  1.11  christos 		  const char *arg = exp;
   1151  1.10  christos 		  parse_exp_1 (&arg, loc.address,
   1152  1.10  christos 			       block_for_pc (loc.address), 0);
   1153  1.10  christos 		  if (*arg != 0)
   1154  1.10  christos 		    error (_("Junk at end of expression"));
   1155  1.10  christos 		  break;
   1156  1.10  christos 		}
   1157  1.10  christos 	      catch (const gdb_exception_error &e)
   1158  1.10  christos 		{
   1159  1.10  christos 		  /* Condition string is invalid.  If this happens to
   1160  1.11  christos 		     be the last loc, abandon (if not forced) or continue
   1161  1.10  christos 		     (if forced).  */
   1162  1.10  christos 		  if (&loc == &b->last_loc () && !force)
   1163  1.10  christos 		    throw;
   1164  1.10  christos 		}
   1165  1.10  christos 	    }
   1166  1.10  christos 
   1167  1.11  christos 	  /* If we reach here, the condition is valid at some locations.  */
   1168   1.9  christos 	  int loc_num = 1;
   1169  1.11  christos 	  for (bp_location &loc : b->locations ())
   1170  1.10  christos 	    {
   1171   1.1  christos 	      set_breakpoint_location_condition (exp, &loc, b->number, loc_num);
   1172   1.1  christos 	      loc_num++;
   1173   1.9  christos 	    }
   1174   1.9  christos 	}
   1175   1.9  christos 
   1176  1.10  christos       /* We know that the new condition parsed successfully.  The
   1177   1.9  christos 	 condition string of the breakpoint can be safely updated.  */
   1178   1.1  christos       b->cond_string = make_unique_xstrdup (exp);
   1179   1.1  christos       b->condition_not_parsed = 0;
   1180   1.1  christos     }
   1181  1.11  christos   mark_breakpoint_modified (b);
   1182   1.1  christos 
   1183   1.1  christos   notify_breakpoint_modified (b);
   1184  1.10  christos }
   1185  1.10  christos 
   1186  1.10  christos /* See breakpoint.h.  */
   1187  1.10  christos 
   1188  1.10  christos void
   1189  1.10  christos set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
   1190  1.11  christos 			  bool force)
   1191  1.11  christos {
   1192  1.10  christos   for (breakpoint &b : all_breakpoints ())
   1193  1.10  christos     if (b.number == bpnum)
   1194  1.10  christos       {
   1195  1.10  christos 	/* Check if this breakpoint has a "stop" method implemented in an
   1196  1.10  christos 	   extension language.  This method and conditions entered into GDB
   1197  1.11  christos 	   from the CLI are mutually exclusive.  */
   1198  1.10  christos 	const struct extension_language_defn *extlang
   1199  1.10  christos 	  = get_breakpoint_cond_ext_lang (&b, EXT_LANG_NONE);
   1200  1.10  christos 
   1201  1.10  christos 	if (extlang != NULL)
   1202  1.10  christos 	  {
   1203  1.10  christos 	    error (_("Only one stop condition allowed.  There is currently"
   1204  1.10  christos 		     " a %s stop condition defined for this breakpoint."),
   1205  1.11  christos 		   ext_lang_capitalized_name (extlang));
   1206  1.10  christos 	  }
   1207  1.11  christos 	set_breakpoint_condition (&b, exp, from_tty, force);
   1208  1.10  christos 
   1209  1.10  christos 	if (is_breakpoint (&b))
   1210  1.10  christos 	  update_global_location_list (UGLL_MAY_INSERT);
   1211  1.10  christos 
   1212  1.10  christos 	return;
   1213  1.10  christos       }
   1214  1.10  christos 
   1215  1.10  christos   error (_("No breakpoint number %d."), bpnum);
   1216  1.10  christos }
   1217  1.10  christos 
   1218  1.10  christos /* The options for the "condition" command.  */
   1219  1.10  christos 
   1220  1.10  christos struct condition_command_opts
   1221  1.10  christos {
   1222  1.10  christos   /* For "-force".  */
   1223  1.10  christos   bool force_condition = false;
   1224  1.10  christos };
   1225  1.10  christos 
   1226  1.10  christos static const gdb::option::option_def condition_command_option_defs[] = {
   1227  1.10  christos 
   1228  1.10  christos   gdb::option::flag_option_def<condition_command_opts> {
   1229  1.10  christos     "force",
   1230  1.10  christos     [] (condition_command_opts *opts) { return &opts->force_condition; },
   1231  1.10  christos     N_("Set the condition even if it is invalid for all current locations."),
   1232  1.10  christos   },
   1233  1.10  christos 
   1234  1.10  christos };
   1235  1.10  christos 
   1236  1.10  christos /* Create an option_def_group for the "condition" options, with
   1237  1.10  christos    CC_OPTS as context.  */
   1238  1.10  christos 
   1239  1.10  christos static inline gdb::option::option_def_group
   1240  1.10  christos make_condition_command_options_def_group (condition_command_opts *cc_opts)
   1241  1.10  christos {
   1242  1.10  christos   return {{condition_command_option_defs}, cc_opts};
   1243   1.1  christos }
   1244   1.1  christos 
   1245   1.8  christos /* Completion for the "condition" command.  */
   1246   1.1  christos 
   1247   1.8  christos static void
   1248  1.10  christos condition_completer (struct cmd_list_element *cmd,
   1249   1.1  christos 		     completion_tracker &tracker,
   1250  1.10  christos 		     const char *text, const char * /*word*/)
   1251  1.10  christos {
   1252  1.10  christos   bool has_no_arguments = (*text == '\0');
   1253  1.10  christos   condition_command_opts cc_opts;
   1254  1.10  christos   const auto group = make_condition_command_options_def_group (&cc_opts);
   1255  1.10  christos   if (gdb::option::complete_options
   1256   1.1  christos       (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
   1257   1.8  christos     return;
   1258  1.10  christos 
   1259   1.1  christos   text = skip_spaces (text);
   1260   1.1  christos   const char *space = skip_to_space (text);
   1261   1.1  christos   if (*space == '\0')
   1262   1.1  christos     {
   1263   1.1  christos       int len;
   1264   1.1  christos 
   1265  1.10  christos       if (text[0] == '$')
   1266   1.1  christos 	{
   1267  1.11  christos 	  tracker.advance_custom_word_point_by (1);
   1268   1.8  christos 	  /* We don't support completion of history indices.  */
   1269   1.8  christos 	  if (!isdigit ((unsigned char)text[1]))
   1270   1.1  christos 	    complete_internalvar (tracker, &text[1]);
   1271   1.1  christos 	  return;
   1272  1.10  christos 	}
   1273  1.10  christos 
   1274  1.10  christos       /* Suggest the "-force" flag if no arguments are given.  If
   1275  1.10  christos 	 arguments were passed, they either already include the flag,
   1276  1.10  christos 	 or we are beyond the point of suggesting it because it's
   1277  1.10  christos 	 positionally the first argument.  */
   1278  1.10  christos       if (has_no_arguments)
   1279   1.1  christos 	gdb::option::complete_on_all_options (tracker, group);
   1280   1.1  christos 
   1281   1.1  christos       /* We're completing the breakpoint number.  */
   1282  1.11  christos       len = strlen (text);
   1283   1.1  christos 
   1284   1.1  christos       for (breakpoint &b : all_breakpoints ())
   1285   1.1  christos 	{
   1286  1.11  christos 	  char number[50];
   1287   1.1  christos 
   1288   1.1  christos 	  xsnprintf (number, sizeof (number), "%d", b.number);
   1289   1.9  christos 
   1290   1.1  christos 	  if (strncmp (number, text, len) == 0)
   1291   1.1  christos 	    tracker.add_completion (make_unique_xstrdup (number));
   1292   1.8  christos 	}
   1293   1.1  christos 
   1294   1.1  christos       return;
   1295  1.10  christos     }
   1296  1.10  christos 
   1297  1.10  christos   /* We're completing the expression part.  Skip the breakpoint num.  */
   1298  1.10  christos   const char *exp_start = skip_spaces (space);
   1299  1.10  christos   tracker.advance_custom_word_point_by (exp_start - text);
   1300   1.8  christos   text = exp_start;
   1301   1.1  christos   const char *word = advance_to_expression_complete_word_point (tracker, text);
   1302   1.1  christos   expression_completer (cmd, tracker, text, word);
   1303   1.1  christos }
   1304   1.1  christos 
   1305   1.1  christos /* condition N EXP -- set break condition of breakpoint N to EXP.  */
   1306   1.8  christos 
   1307   1.1  christos static void
   1308   1.8  christos condition_command (const char *arg, int from_tty)
   1309   1.1  christos {
   1310   1.1  christos   const char *p;
   1311   1.1  christos   int bnum;
   1312   1.1  christos 
   1313   1.1  christos   if (arg == 0)
   1314   1.1  christos     error_no_arg (_("breakpoint number"));
   1315  1.10  christos 
   1316  1.10  christos   p = arg;
   1317  1.10  christos 
   1318  1.10  christos   /* Check if the "-force" flag was passed.  */
   1319  1.10  christos   condition_command_opts cc_opts;
   1320  1.10  christos   const auto group = make_condition_command_options_def_group (&cc_opts);
   1321  1.10  christos   gdb::option::process_options
   1322   1.1  christos     (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
   1323   1.1  christos 
   1324   1.1  christos   bnum = get_number (&p);
   1325   1.1  christos   if (bnum == 0)
   1326  1.10  christos     error (_("Bad breakpoint argument: '%s'"), arg);
   1327   1.1  christos 
   1328   1.1  christos   set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
   1329   1.1  christos }
   1330   1.1  christos 
   1331   1.1  christos /* Check that COMMAND do not contain commands that are suitable
   1332   1.1  christos    only for tracepoints and not suitable for ordinary breakpoints.
   1333   1.1  christos    Throw if any such commands is found.  */
   1334   1.1  christos 
   1335   1.1  christos static void
   1336   1.1  christos check_no_tracepoint_commands (struct command_line *commands)
   1337   1.1  christos {
   1338   1.1  christos   struct command_line *c;
   1339   1.1  christos 
   1340   1.1  christos   for (c = commands; c; c = c->next)
   1341   1.1  christos     {
   1342   1.1  christos       if (c->control_type == while_stepping_control)
   1343   1.1  christos 	error (_("The 'while-stepping' command can "
   1344   1.8  christos 		 "only be used for tracepoints"));
   1345   1.8  christos 
   1346   1.1  christos       check_no_tracepoint_commands (c->body_list_0.get ());
   1347   1.1  christos       check_no_tracepoint_commands (c->body_list_1.get ());
   1348   1.1  christos 
   1349   1.1  christos       /* Not that command parsing removes leading whitespace and comment
   1350   1.1  christos 	 lines and also empty lines.  So, we only need to check for
   1351   1.1  christos 	 command directly.  */
   1352   1.1  christos       if (strstr (c->line, "collect ") == c->line)
   1353   1.1  christos 	error (_("The 'collect' command can only be used for tracepoints"));
   1354   1.1  christos 
   1355   1.1  christos       if (strstr (c->line, "teval ") == c->line)
   1356   1.1  christos 	error (_("The 'teval' command can only be used for tracepoints"));
   1357   1.1  christos     }
   1358  1.10  christos }
   1359   1.8  christos 
   1360  1.10  christos struct longjmp_breakpoint : public momentary_breakpoint
   1361  1.10  christos {
   1362   1.8  christos   using momentary_breakpoint::momentary_breakpoint;
   1363   1.8  christos 
   1364   1.8  christos   ~longjmp_breakpoint () override;
   1365   1.1  christos };
   1366   1.1  christos 
   1367   1.8  christos /* Encapsulate tests for different types of tracepoints.  */
   1368   1.8  christos 
   1369   1.1  christos static bool
   1370   1.1  christos is_tracepoint_type (bptype type)
   1371   1.1  christos {
   1372  1.10  christos   return (type == bp_tracepoint
   1373  1.10  christos 	  || type == bp_fast_tracepoint
   1374   1.8  christos 	  || type == bp_static_tracepoint
   1375   1.8  christos 	  || type == bp_static_marker_tracepoint);
   1376   1.9  christos }
   1377   1.9  christos 
   1378   1.9  christos /* See breakpoint.h.  */
   1379   1.1  christos 
   1380   1.1  christos bool
   1381   1.1  christos is_tracepoint (const struct breakpoint *b)
   1382   1.1  christos {
   1383   1.1  christos   return is_tracepoint_type (b->type);
   1384   1.8  christos }
   1385   1.8  christos 
   1386   1.8  christos /* Factory function to create an appropriate instance of breakpoint given
   1387  1.10  christos    TYPE.  */
   1388  1.10  christos 
   1389  1.10  christos template<typename... Arg>
   1390  1.10  christos static std::unique_ptr<code_breakpoint>
   1391   1.8  christos new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
   1392  1.10  christos 			  Arg&&... args)
   1393  1.10  christos {
   1394  1.10  christos   code_breakpoint *b;
   1395  1.10  christos 
   1396  1.10  christos   switch (type)
   1397  1.10  christos     {
   1398  1.10  christos     case bp_breakpoint:
   1399  1.10  christos     case bp_hardware_breakpoint:
   1400  1.10  christos       b = new ordinary_breakpoint (gdbarch, type,
   1401  1.10  christos 				   std::forward<Arg> (args)...);
   1402  1.10  christos       break;
   1403  1.10  christos 
   1404  1.10  christos     case bp_fast_tracepoint:
   1405  1.10  christos     case bp_static_tracepoint:
   1406  1.10  christos     case bp_tracepoint:
   1407  1.10  christos       b = new tracepoint (gdbarch, type,
   1408  1.10  christos 			  std::forward<Arg> (args)...);
   1409  1.10  christos       break;
   1410  1.10  christos 
   1411  1.10  christos     case bp_static_marker_tracepoint:
   1412  1.10  christos       b = new static_marker_tracepoint (gdbarch, type,
   1413  1.10  christos 					std::forward<Arg> (args)...);
   1414  1.10  christos       break;
   1415  1.10  christos 
   1416  1.10  christos     case bp_dprintf:
   1417  1.10  christos       b = new dprintf_breakpoint (gdbarch, type,
   1418   1.8  christos 				  std::forward<Arg> (args)...);
   1419  1.10  christos       break;
   1420  1.10  christos 
   1421  1.10  christos     default:
   1422   1.8  christos       gdb_assert_not_reached ("invalid type");
   1423  1.10  christos     }
   1424   1.8  christos 
   1425   1.8  christos   return std::unique_ptr<code_breakpoint> (b);
   1426   1.1  christos }
   1427   1.1  christos 
   1428   1.1  christos /* A helper function that validates that COMMANDS are valid for a
   1429   1.1  christos    breakpoint.  This function will throw an exception if a problem is
   1430   1.1  christos    found.  */
   1431   1.1  christos 
   1432   1.1  christos static void
   1433   1.1  christos validate_commands_for_breakpoint (struct breakpoint *b,
   1434   1.1  christos 				  struct command_line *commands)
   1435   1.1  christos {
   1436  1.11  christos   if (is_tracepoint (b))
   1437   1.1  christos     {
   1438   1.1  christos       tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
   1439   1.1  christos       struct command_line *c;
   1440   1.1  christos       struct command_line *while_stepping = 0;
   1441  1.10  christos 
   1442  1.10  christos       /* Reset the while-stepping step count.  The previous commands
   1443   1.1  christos 	 might have included a while-stepping action, while the new
   1444   1.1  christos 	 ones might not.  */
   1445   1.1  christos       t->step_count = 0;
   1446   1.1  christos 
   1447   1.1  christos       /* We need to verify that each top-level element of commands is
   1448   1.1  christos 	 valid for tracepoints, that there's at most one
   1449   1.1  christos 	 while-stepping element, and that the while-stepping's body
   1450   1.1  christos 	 has valid tracing commands excluding nested while-stepping.
   1451   1.1  christos 	 We also need to validate the tracepoint action line in the
   1452   1.1  christos 	 context of the tracepoint --- validate_actionline actually
   1453   1.1  christos 	 has side effects, like setting the tracepoint's
   1454   1.1  christos 	 while-stepping STEP_COUNT, in addition to checking if the
   1455   1.1  christos 	 collect/teval actions parse and make sense in the
   1456   1.1  christos 	 tracepoint's context.  */
   1457   1.1  christos       for (c = commands; c; c = c->next)
   1458   1.1  christos 	{
   1459   1.1  christos 	  if (c->control_type == while_stepping_control)
   1460   1.1  christos 	    {
   1461   1.1  christos 	      if (b->type == bp_fast_tracepoint)
   1462  1.10  christos 		error (_("The 'while-stepping' command "
   1463  1.10  christos 			 "cannot be used for fast tracepoint"));
   1464   1.1  christos 	      else if (b->type == bp_static_tracepoint
   1465   1.1  christos 		       || b->type == bp_static_marker_tracepoint)
   1466   1.1  christos 		error (_("The 'while-stepping' command "
   1467   1.1  christos 			 "cannot be used for static tracepoint"));
   1468   1.1  christos 
   1469   1.1  christos 	      if (while_stepping)
   1470   1.1  christos 		error (_("The 'while-stepping' command "
   1471   1.1  christos 			 "can be used only once"));
   1472   1.1  christos 	      else
   1473   1.1  christos 		while_stepping = c;
   1474  1.11  christos 	    }
   1475   1.1  christos 
   1476   1.1  christos 	  validate_actionline (c->line, t);
   1477   1.1  christos 	}
   1478   1.1  christos       if (while_stepping)
   1479   1.1  christos 	{
   1480   1.8  christos 	  struct command_line *c2;
   1481   1.8  christos 
   1482   1.1  christos 	  gdb_assert (while_stepping->body_list_1 == nullptr);
   1483   1.1  christos 	  c2 = while_stepping->body_list_0.get ();
   1484   1.1  christos 	  for (; c2; c2 = c2->next)
   1485   1.1  christos 	    {
   1486   1.1  christos 	      if (c2->control_type == while_stepping_control)
   1487   1.1  christos 		error (_("The 'while-stepping' command cannot be nested"));
   1488   1.1  christos 	    }
   1489   1.1  christos 	}
   1490   1.1  christos     }
   1491   1.1  christos   else
   1492   1.1  christos     {
   1493   1.1  christos       check_no_tracepoint_commands (commands);
   1494   1.1  christos     }
   1495   1.1  christos }
   1496   1.1  christos 
   1497   1.1  christos /* Return a vector of all the static tracepoints set at ADDR.  The
   1498   1.8  christos    caller is responsible for releasing the vector.  */
   1499   1.1  christos 
   1500   1.1  christos std::vector<breakpoint *>
   1501   1.8  christos static_tracepoints_here (CORE_ADDR addr)
   1502   1.1  christos {
   1503  1.11  christos   std::vector<breakpoint *> found;
   1504  1.11  christos 
   1505  1.11  christos   for (breakpoint &b : all_breakpoints ())
   1506   1.1  christos     if (b.type == bp_static_tracepoint
   1507  1.11  christos 	|| b.type == bp_static_marker_tracepoint)
   1508  1.11  christos       {
   1509  1.11  christos 	for (bp_location &loc : b.locations ())
   1510   1.1  christos 	  if (loc.address == addr)
   1511   1.1  christos 	    found.push_back (&b);
   1512   1.1  christos       }
   1513   1.1  christos 
   1514   1.1  christos   return found;
   1515   1.1  christos }
   1516   1.1  christos 
   1517   1.1  christos /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
   1518   1.1  christos    validate that only allowed commands are included.  */
   1519   1.1  christos 
   1520   1.8  christos void
   1521   1.1  christos breakpoint_set_commands (struct breakpoint *b,
   1522   1.7  christos 			 counted_command_line &&commands)
   1523   1.1  christos {
   1524   1.8  christos   validate_commands_for_breakpoint (b, commands.get ());
   1525  1.11  christos 
   1526   1.1  christos   b->commands = std::move (commands);
   1527   1.1  christos   notify_breakpoint_modified (b);
   1528   1.1  christos }
   1529   1.1  christos 
   1530   1.1  christos /* Set the internal `silent' flag on the breakpoint.  Note that this
   1531   1.1  christos    is not the same as the "silent" that may appear in the breakpoint's
   1532   1.1  christos    commands.  */
   1533   1.1  christos 
   1534   1.1  christos void
   1535   1.1  christos breakpoint_set_silent (struct breakpoint *b, int silent)
   1536   1.1  christos {
   1537   1.1  christos   int old_silent = b->silent;
   1538   1.1  christos 
   1539  1.11  christos   b->silent = silent;
   1540   1.1  christos   if (old_silent != silent)
   1541   1.1  christos     notify_breakpoint_modified (b);
   1542  1.11  christos }
   1543   1.1  christos 
   1544   1.1  christos /* See breakpoint.h.  */
   1545   1.1  christos 
   1546   1.1  christos void
   1547  1.11  christos breakpoint_set_thread (struct breakpoint *b, int thread)
   1548  1.11  christos {
   1549  1.11  christos   /* THREAD should be -1, meaning no thread restriction, or it should be a
   1550  1.11  christos      valid global thread-id, which are greater than zero.  */
   1551  1.11  christos   gdb_assert (thread == -1 || thread > 0);
   1552  1.11  christos 
   1553  1.11  christos   /* It is not valid to set a thread restriction for a breakpoint that
   1554  1.11  christos      already has task or inferior restriction.  */
   1555   1.1  christos   gdb_assert (thread == -1 || (b->task == -1 && b->inferior == -1));
   1556   1.1  christos 
   1557   1.1  christos   int old_thread = b->thread;
   1558  1.11  christos   b->thread = thread;
   1559  1.11  christos   if (old_thread != thread)
   1560  1.11  christos     notify_breakpoint_modified (b);
   1561  1.11  christos }
   1562  1.11  christos 
   1563  1.11  christos /* See breakpoint.h.  */
   1564  1.11  christos 
   1565  1.11  christos void
   1566  1.11  christos breakpoint_set_inferior (struct breakpoint *b, int inferior)
   1567  1.11  christos {
   1568  1.11  christos   /* INFERIOR should be -1, meaning no inferior restriction, or it should
   1569  1.11  christos      be a valid inferior number, which are greater than zero.  */
   1570  1.11  christos   gdb_assert (inferior == -1 || inferior > 0);
   1571  1.11  christos 
   1572  1.11  christos   /* It is not valid to set an inferior restriction for a breakpoint that
   1573  1.11  christos      already has a task or thread restriction.  */
   1574  1.11  christos   gdb_assert (inferior == -1 || (b->task == -1 && b->thread == -1));
   1575  1.11  christos 
   1576  1.11  christos   int old_inferior = b->inferior;
   1577  1.11  christos   b->inferior = inferior;
   1578   1.1  christos   if (old_inferior != inferior)
   1579   1.1  christos     notify_breakpoint_modified (b);
   1580  1.11  christos }
   1581   1.1  christos 
   1582   1.1  christos /* See breakpoint.h.  */
   1583   1.1  christos 
   1584   1.1  christos void
   1585  1.11  christos breakpoint_set_task (struct breakpoint *b, int task)
   1586  1.11  christos {
   1587  1.11  christos   /* TASK should be -1, meaning no task restriction, or it should be a
   1588  1.11  christos      valid task-id, which are greater than zero.  */
   1589  1.11  christos   gdb_assert (task == -1 || task > 0);
   1590  1.11  christos 
   1591  1.11  christos   /* It is not valid to set a task restriction for a breakpoint that
   1592  1.11  christos      already has a thread or inferior restriction.  */
   1593   1.1  christos   gdb_assert (task == -1 || (b->thread == -1 && b->inferior == -1));
   1594   1.1  christos 
   1595   1.1  christos   int old_task = b->task;
   1596  1.11  christos   b->task = task;
   1597   1.1  christos   if (old_task != task)
   1598   1.1  christos     notify_breakpoint_modified (b);
   1599   1.1  christos }
   1600   1.7  christos 
   1601   1.1  christos static void
   1602   1.1  christos commands_command_1 (const char *arg, int from_tty,
   1603   1.8  christos 		    struct command_line *control)
   1604   1.8  christos {
   1605   1.8  christos   counted_command_line cmd;
   1606   1.8  christos   /* cmd_read will be true once we have read cmd.  Note that cmd might still be
   1607   1.8  christos      NULL after the call to read_command_lines if the user provides an empty
   1608   1.1  christos      list of command by just typing "end".  */
   1609   1.7  christos   bool cmd_read = false;
   1610   1.7  christos 
   1611   1.1  christos   std::string new_arg;
   1612   1.1  christos 
   1613  1.10  christos   if (arg == NULL || !*arg)
   1614   1.1  christos     {
   1615   1.7  christos       /* Argument not explicitly given.  Synthesize it.  */
   1616   1.7  christos       if (breakpoint_count - prev_breakpoint_count > 1)
   1617   1.1  christos 	new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
   1618   1.7  christos 				 breakpoint_count);
   1619   1.1  christos       else if (breakpoint_count > 0)
   1620  1.10  christos 	new_arg = string_printf ("%d", breakpoint_count);
   1621  1.10  christos     }
   1622  1.10  christos   else
   1623  1.10  christos     {
   1624  1.10  christos       /* Create a copy of ARG.  This is needed because the "commands"
   1625  1.10  christos 	 command may be coming from a script.  In that case, the read
   1626  1.10  christos 	 line buffer is going to be overwritten in the lambda of
   1627  1.10  christos 	 'map_breakpoint_numbers' below when reading the next line
   1628  1.10  christos 	 before we are are done parsing the breakpoint numbers.  */
   1629  1.10  christos       new_arg = arg;
   1630   1.1  christos     }
   1631   1.8  christos   arg = new_arg.c_str ();
   1632   1.8  christos 
   1633   1.8  christos   map_breakpoint_numbers
   1634   1.8  christos     (arg, [&] (breakpoint *b)
   1635   1.8  christos      {
   1636   1.8  christos        if (!cmd_read)
   1637   1.8  christos 	 {
   1638   1.8  christos 	   gdb_assert (cmd == NULL);
   1639   1.8  christos 	   if (control != NULL)
   1640   1.8  christos 	     cmd = control->body_list_0;
   1641   1.8  christos 	   else
   1642   1.8  christos 	     {
   1643   1.8  christos 	       std::string str
   1644   1.8  christos 		 = string_printf (_("Type commands for breakpoint(s) "
   1645   1.8  christos 				    "%s, one per line."),
   1646   1.8  christos 				  arg);
   1647   1.8  christos 
   1648  1.11  christos 	       auto do_validate = [=] (const char *line)
   1649  1.11  christos 				  {
   1650  1.11  christos 				    tracepoint *t
   1651   1.8  christos 				      = gdb::checked_static_cast<tracepoint *> (b);
   1652   1.8  christos 				    validate_actionline (line, t);
   1653   1.8  christos 				  };
   1654   1.8  christos 	       gdb::function_view<void (const char *)> validator;
   1655   1.8  christos 	       if (is_tracepoint (b))
   1656   1.8  christos 		 validator = do_validate;
   1657   1.8  christos 
   1658   1.8  christos 	       cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
   1659   1.8  christos 	     }
   1660   1.8  christos 	   cmd_read = true;
   1661   1.8  christos 	 }
   1662   1.8  christos 
   1663   1.8  christos        /* If a breakpoint was on the list more than once, we don't need to
   1664   1.8  christos 	  do anything.  */
   1665   1.8  christos        if (b->commands != cmd)
   1666   1.8  christos 	 {
   1667  1.11  christos 	   validate_commands_for_breakpoint (b, cmd.get ());
   1668   1.8  christos 	   b->commands = cmd;
   1669   1.8  christos 	   notify_breakpoint_modified (b);
   1670   1.1  christos 	 }
   1671   1.1  christos      });
   1672   1.1  christos }
   1673   1.8  christos 
   1674   1.1  christos static void
   1675   1.1  christos commands_command (const char *arg, int from_tty)
   1676   1.1  christos {
   1677   1.1  christos   commands_command_1 (arg, from_tty, NULL);
   1678   1.1  christos }
   1679   1.1  christos 
   1680   1.1  christos /* Like commands_command, but instead of reading the commands from
   1681   1.1  christos    input stream, takes them from an already parsed command structure.
   1682   1.1  christos 
   1683   1.1  christos    This is used by cli-script.c to DTRT with breakpoint commands
   1684   1.7  christos    that are part of if and while bodies.  */
   1685   1.1  christos enum command_control_type
   1686   1.1  christos commands_from_control_command (const char *arg, struct command_line *cmd)
   1687   1.1  christos {
   1688   1.1  christos   commands_command_1 (arg, 0, cmd);
   1689   1.1  christos   return simple_control;
   1690  1.10  christos }
   1691   1.1  christos 
   1692  1.10  christos /* Return true if BL->TARGET_INFO contains valid information.  */
   1693   1.1  christos 
   1694   1.1  christos static bool
   1695   1.1  christos bp_location_has_shadow (struct bp_location *bl)
   1696  1.10  christos {
   1697   1.1  christos   if (bl->loc_type != bp_loc_software_breakpoint)
   1698  1.10  christos     return false;
   1699   1.1  christos   if (!bl->inserted)
   1700   1.1  christos     return false;
   1701  1.10  christos   if (bl->target_info.shadow_len == 0)
   1702  1.10  christos     /* BL isn't valid, or doesn't shadow memory.  */
   1703   1.1  christos     return false;
   1704   1.1  christos   return true;
   1705   1.3  christos }
   1706   1.3  christos 
   1707   1.3  christos /* Update BUF, which is LEN bytes read from the target address
   1708   1.3  christos    MEMADDR, by replacing a memory breakpoint with its shadowed
   1709   1.3  christos    contents.
   1710   1.3  christos 
   1711   1.3  christos    If READBUF is not NULL, this buffer must not overlap with the of
   1712   1.3  christos    the breakpoint location's shadow_contents buffer.  Otherwise, a
   1713   1.3  christos    failed assertion internal error will be raised.  */
   1714   1.3  christos 
   1715   1.3  christos static void
   1716   1.3  christos one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
   1717   1.3  christos 			    const gdb_byte *writebuf_org,
   1718   1.3  christos 			    ULONGEST memaddr, LONGEST len,
   1719   1.3  christos 			    struct bp_target_info *target_info,
   1720   1.3  christos 			    struct gdbarch *gdbarch)
   1721   1.3  christos {
   1722   1.3  christos   /* Now do full processing of the found relevant range of elements.  */
   1723   1.3  christos   CORE_ADDR bp_addr = 0;
   1724   1.3  christos   int bp_size = 0;
   1725   1.3  christos   int bptoffset = 0;
   1726  1.11  christos 
   1727   1.3  christos   if (!breakpoint_address_match (target_info->placed_address_space, 0,
   1728   1.3  christos 				 current_program_space->aspace.get (), 0))
   1729   1.3  christos     {
   1730   1.3  christos       /* The breakpoint is inserted in a different address space.  */
   1731   1.3  christos       return;
   1732   1.3  christos     }
   1733   1.3  christos 
   1734   1.3  christos   /* Addresses and length of the part of the breakpoint that
   1735   1.3  christos      we need to copy.  */
   1736   1.3  christos   bp_addr = target_info->placed_address;
   1737   1.3  christos   bp_size = target_info->shadow_len;
   1738   1.3  christos 
   1739   1.3  christos   if (bp_addr + bp_size <= memaddr)
   1740   1.3  christos     {
   1741   1.3  christos       /* The breakpoint is entirely before the chunk of memory we are
   1742   1.3  christos 	 reading.  */
   1743   1.3  christos       return;
   1744   1.3  christos     }
   1745   1.3  christos 
   1746   1.3  christos   if (bp_addr >= memaddr + len)
   1747   1.3  christos     {
   1748   1.3  christos       /* The breakpoint is entirely after the chunk of memory we are
   1749   1.3  christos 	 reading.  */
   1750   1.3  christos       return;
   1751   1.3  christos     }
   1752   1.3  christos 
   1753   1.3  christos   /* Offset within shadow_contents.  */
   1754   1.3  christos   if (bp_addr < memaddr)
   1755   1.3  christos     {
   1756   1.3  christos       /* Only copy the second part of the breakpoint.  */
   1757   1.3  christos       bp_size -= memaddr - bp_addr;
   1758   1.3  christos       bptoffset = memaddr - bp_addr;
   1759   1.3  christos       bp_addr = memaddr;
   1760   1.3  christos     }
   1761   1.3  christos 
   1762   1.3  christos   if (bp_addr + bp_size > memaddr + len)
   1763   1.3  christos     {
   1764   1.3  christos       /* Only copy the first part of the breakpoint.  */
   1765   1.3  christos       bp_size -= (bp_addr + bp_size) - (memaddr + len);
   1766   1.3  christos     }
   1767   1.3  christos 
   1768   1.3  christos   if (readbuf != NULL)
   1769   1.3  christos     {
   1770   1.3  christos       /* Verify that the readbuf buffer does not overlap with the
   1771   1.3  christos 	 shadow_contents buffer.  */
   1772   1.3  christos       gdb_assert (target_info->shadow_contents >= readbuf + len
   1773   1.3  christos 		  || readbuf >= (target_info->shadow_contents
   1774   1.3  christos 				 + target_info->shadow_len));
   1775   1.3  christos 
   1776   1.3  christos       /* Update the read buffer with this inserted breakpoint's
   1777   1.3  christos 	 shadow.  */
   1778   1.3  christos       memcpy (readbuf + bp_addr - memaddr,
   1779   1.3  christos 	      target_info->shadow_contents + bptoffset, bp_size);
   1780   1.3  christos     }
   1781   1.3  christos   else
   1782   1.3  christos     {
   1783   1.3  christos       const unsigned char *bp;
   1784   1.3  christos       CORE_ADDR addr = target_info->reqstd_address;
   1785   1.3  christos       int placed_size;
   1786   1.3  christos 
   1787   1.3  christos       /* Update the shadow with what we want to write to memory.  */
   1788   1.3  christos       memcpy (target_info->shadow_contents + bptoffset,
   1789   1.3  christos 	      writebuf_org + bp_addr - memaddr, bp_size);
   1790   1.3  christos 
   1791   1.3  christos       /* Determine appropriate breakpoint contents and size for this
   1792   1.3  christos 	 address.  */
   1793   1.3  christos       bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
   1794   1.3  christos 
   1795   1.3  christos       /* Update the final write buffer with this inserted
   1796   1.3  christos 	 breakpoint's INSN.  */
   1797   1.3  christos       memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
   1798   1.3  christos     }
   1799   1.1  christos }
   1800   1.1  christos 
   1801   1.1  christos /* Update BUF, which is LEN bytes read from the target address MEMADDR,
   1802   1.1  christos    by replacing any memory breakpoints with their shadowed contents.
   1803   1.1  christos 
   1804   1.1  christos    If READBUF is not NULL, this buffer must not overlap with any of
   1805   1.1  christos    the breakpoint location's shadow_contents buffers.  Otherwise,
   1806   1.1  christos    a failed assertion internal error will be raised.
   1807   1.7  christos 
   1808   1.7  christos    The range of shadowed area by each bp_location is:
   1809   1.1  christos      bl->address - bp_locations_placed_address_before_address_max
   1810   1.1  christos      up to bl->address + bp_locations_shadow_len_after_address_max
   1811   1.1  christos    The range we were requested to resolve shadows for is:
   1812   1.1  christos      memaddr ... memaddr + len
   1813   1.7  christos    Thus the safe cutoff boundaries for performance optimization are
   1814   1.1  christos      memaddr + len <= (bl->address
   1815   1.7  christos 		       - bp_locations_placed_address_before_address_max)
   1816   1.1  christos    and:
   1817   1.1  christos      bl->address + bp_locations_shadow_len_after_address_max <= memaddr  */
   1818   1.1  christos 
   1819   1.1  christos void
   1820   1.1  christos breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
   1821   1.1  christos 			const gdb_byte *writebuf_org,
   1822   1.1  christos 			ULONGEST memaddr, LONGEST len)
   1823   1.1  christos {
   1824   1.1  christos   /* Left boundary, right boundary and median element of our binary
   1825   1.1  christos      search.  */
   1826   1.1  christos   unsigned bc_l, bc_r, bc;
   1827   1.1  christos 
   1828   1.1  christos   /* Find BC_L which is a leftmost element which may affect BUF
   1829   1.1  christos      content.  It is safe to report lower value but a failure to
   1830   1.1  christos      report higher one.  */
   1831  1.10  christos 
   1832   1.1  christos   bc_l = 0;
   1833   1.1  christos   bc_r = bp_locations.size ();
   1834   1.1  christos   while (bc_l + 1 < bc_r)
   1835   1.1  christos     {
   1836   1.1  christos       struct bp_location *bl;
   1837   1.7  christos 
   1838   1.1  christos       bc = (bc_l + bc_r) / 2;
   1839   1.1  christos       bl = bp_locations[bc];
   1840   1.1  christos 
   1841   1.1  christos       /* Check first BL->ADDRESS will not overflow due to the added
   1842   1.1  christos 	 constant.  Then advance the left boundary only if we are sure
   1843   1.1  christos 	 the BC element can in no way affect the BUF content (MEMADDR
   1844   1.7  christos 	 to MEMADDR + LEN range).
   1845   1.1  christos 
   1846   1.1  christos 	 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
   1847   1.1  christos 	 offset so that we cannot miss a breakpoint with its shadow
   1848   1.7  christos 	 range tail still reaching MEMADDR.  */
   1849   1.1  christos 
   1850   1.7  christos       if ((bl->address + bp_locations_shadow_len_after_address_max
   1851   1.1  christos 	   >= bl->address)
   1852   1.1  christos 	  && (bl->address + bp_locations_shadow_len_after_address_max
   1853   1.1  christos 	      <= memaddr))
   1854   1.1  christos 	bc_l = bc;
   1855   1.1  christos       else
   1856   1.1  christos 	bc_r = bc;
   1857   1.1  christos     }
   1858   1.1  christos 
   1859   1.1  christos   /* Due to the binary search above, we need to make sure we pick the
   1860   1.1  christos      first location that's at BC_L's address.  E.g., if there are
   1861   1.1  christos      multiple locations at the same address, BC_L may end up pointing
   1862   1.1  christos      at a duplicate location, and miss the "master"/"inserted"
   1863   1.1  christos      location.  Say, given locations L1, L2 and L3 at addresses A and
   1864   1.1  christos      B:
   1865   1.1  christos 
   1866   1.1  christos       L1@A, L2@A, L3@B, ...
   1867   1.1  christos 
   1868   1.1  christos      BC_L could end up pointing at location L2, while the "master"
   1869   1.1  christos      location could be L1.  Since the `loc->inserted' flag is only set
   1870   1.1  christos      on "master" locations, we'd forget to restore the shadow of L1
   1871   1.7  christos      and L2.  */
   1872   1.1  christos   while (bc_l > 0
   1873   1.1  christos 	 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
   1874   1.1  christos     bc_l--;
   1875   1.1  christos 
   1876  1.10  christos   /* Now do full processing of the found relevant range of elements.  */
   1877  1.10  christos 
   1878  1.10  christos   for (bc = bc_l; bc < bp_locations.size (); bc++)
   1879   1.1  christos     {
   1880  1.10  christos       struct bp_location *bl = bp_locations[bc];
   1881  1.10  christos 
   1882  1.10  christos       /* bp_location array has BL->OWNER always non-NULL.  */
   1883  1.10  christos       if (bl->owner->type == bp_none)
   1884   1.1  christos 	warning (_("reading through apparently deleted breakpoint #%d?"),
   1885  1.10  christos 		 bl->owner->number);
   1886  1.10  christos 
   1887   1.1  christos       /* Performance optimization: any further element can no longer affect BUF
   1888  1.10  christos 	 content.  */
   1889  1.10  christos 
   1890  1.10  christos       if (bl->address >= bp_locations_placed_address_before_address_max
   1891  1.10  christos 	  && (memaddr + len
   1892  1.10  christos 	      <= (bl->address
   1893   1.1  christos 		  - bp_locations_placed_address_before_address_max)))
   1894  1.10  christos 	break;
   1895  1.10  christos 
   1896   1.1  christos       if (!bp_location_has_shadow (bl))
   1897  1.10  christos 	continue;
   1898  1.10  christos 
   1899  1.10  christos       one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
   1900   1.1  christos 				  memaddr, len, &bl->target_info, bl->gdbarch);
   1901   1.3  christos     }
   1902   1.9  christos }
   1903   1.1  christos 
   1904   1.9  christos /* See breakpoint.h.  */
   1905   1.1  christos 
   1906   1.1  christos bool
   1907   1.1  christos is_breakpoint (const struct breakpoint *bpt)
   1908   1.1  christos {
   1909   1.1  christos   return (bpt->type == bp_breakpoint
   1910   1.1  christos 	  || bpt->type == bp_hardware_breakpoint
   1911   1.1  christos 	  || bpt->type == bp_dprintf);
   1912   1.1  christos }
   1913   1.1  christos 
   1914   1.9  christos /* Return true if BPT is of any hardware watchpoint kind.  */
   1915   1.1  christos 
   1916   1.1  christos static bool
   1917   1.1  christos is_hardware_watchpoint (const struct breakpoint *bpt)
   1918   1.1  christos {
   1919   1.1  christos   return (bpt->type == bp_hardware_watchpoint
   1920   1.1  christos 	  || bpt->type == bp_read_watchpoint
   1921   1.1  christos 	  || bpt->type == bp_access_watchpoint);
   1922   1.9  christos }
   1923   1.1  christos 
   1924   1.9  christos /* See breakpoint.h.  */
   1925   1.1  christos 
   1926   1.1  christos bool
   1927   1.1  christos is_watchpoint (const struct breakpoint *bpt)
   1928   1.1  christos {
   1929   1.1  christos   return (is_hardware_watchpoint (bpt)
   1930   1.1  christos 	  || bpt->type == bp_watchpoint);
   1931   1.1  christos }
   1932   1.1  christos 
   1933   1.1  christos /* Returns true if the current thread and its running state are safe
   1934   1.1  christos    to evaluate or update watchpoint B.  Watchpoints on local
   1935   1.1  christos    expressions need to be evaluated in the context of the thread that
   1936   1.1  christos    was current when the watchpoint was created, and, that thread needs
   1937   1.1  christos    to be stopped to be able to select the correct frame context.
   1938   1.1  christos    Watchpoints on global expressions can be evaluated on any thread,
   1939   1.1  christos    and in any state.  It is presently left to the target allowing
   1940  1.10  christos    memory accesses when threads are running.  */
   1941   1.1  christos 
   1942   1.1  christos static bool
   1943   1.8  christos watchpoint_in_thread_scope (struct watchpoint *b)
   1944   1.8  christos {
   1945   1.8  christos   return (b->pspace == current_program_space
   1946  1.10  christos 	  && (b->watchpoint_thread == null_ptid
   1947   1.1  christos 	      || (inferior_ptid == b->watchpoint_thread
   1948   1.1  christos 		  && !inferior_thread ()->executing ())));
   1949   1.1  christos }
   1950   1.1  christos 
   1951   1.1  christos /* Set watchpoint B to disp_del_at_next_stop, even including its possible
   1952   1.1  christos    associated bp_watchpoint_scope breakpoint.  */
   1953   1.1  christos 
   1954   1.1  christos static void
   1955   1.8  christos watchpoint_del_at_next_stop (struct watchpoint *w)
   1956   1.1  christos {
   1957   1.8  christos   if (w->related_breakpoint != w)
   1958   1.8  christos     {
   1959   1.8  christos       gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
   1960   1.8  christos       gdb_assert (w->related_breakpoint->related_breakpoint == w);
   1961   1.8  christos       w->related_breakpoint->disposition = disp_del_at_next_stop;
   1962   1.1  christos       w->related_breakpoint->related_breakpoint = w->related_breakpoint;
   1963   1.8  christos       w->related_breakpoint = w;
   1964  1.11  christos     }
   1965   1.1  christos   w->disposition = disp_del_at_next_stop;
   1966   1.1  christos   disable_breakpoint (w);
   1967   1.3  christos }
   1968   1.3  christos 
   1969   1.3  christos /* Extract a bitfield value from value VAL using the bit parameters contained in
   1970   1.3  christos    watchpoint W.  */
   1971   1.3  christos 
   1972   1.3  christos static struct value *
   1973   1.3  christos extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
   1974   1.3  christos {
   1975   1.3  christos   struct value *bit_val;
   1976   1.3  christos 
   1977   1.3  christos   if (val == NULL)
   1978  1.11  christos     return NULL;
   1979   1.3  christos 
   1980  1.11  christos   bit_val = value::allocate (val->type ());
   1981  1.11  christos 
   1982  1.11  christos   val->unpack_bitfield (bit_val,
   1983  1.11  christos 			w->val_bitpos,
   1984  1.11  christos 			w->val_bitsize,
   1985   1.3  christos 			val->contents_for_printing ().data (),
   1986   1.3  christos 			val->offset ());
   1987   1.3  christos 
   1988   1.3  christos   return bit_val;
   1989  1.10  christos }
   1990  1.10  christos 
   1991  1.10  christos /* Allocate a dummy location and add it to B.  This is required
   1992   1.6  christos    because bpstat_stop_status requires a location to be able to report
   1993   1.6  christos    stops.  */
   1994  1.10  christos 
   1995  1.10  christos static void
   1996   1.6  christos add_dummy_location (struct breakpoint *b,
   1997  1.11  christos 		    struct program_space *pspace)
   1998   1.6  christos {
   1999  1.11  christos   gdb_assert (!b->has_locations ());
   2000  1.11  christos 
   2001  1.11  christos   bp_location *loc = new bp_location (b, bp_loc_other);
   2002   1.6  christos   loc->pspace = pspace;
   2003   1.6  christos   b->add_location (*loc);
   2004   1.1  christos }
   2005  1.10  christos 
   2006   1.1  christos /* Assuming that B is a watchpoint:
   2007   1.1  christos    - Reparse watchpoint expression, if REPARSE is true
   2008   1.1  christos    - Evaluate expression and store the result in B->val
   2009   1.1  christos    - Evaluate the condition if there is one, and store the result
   2010   1.1  christos      in b->loc->cond.
   2011   1.1  christos    - Update the list of values that must be watched in B->loc.
   2012   1.1  christos 
   2013   1.1  christos    If the watchpoint disposition is disp_del_at_next_stop, then do
   2014   1.1  christos    nothing.  If this is local watchpoint that is out of scope, delete
   2015   1.1  christos    it.
   2016   1.1  christos 
   2017   1.1  christos    Even with `set breakpoint always-inserted on' the watchpoints are
   2018   1.1  christos    removed + inserted on each stop here.  Normal breakpoints must
   2019   1.1  christos    never be removed because they might be missed by a running thread
   2020   1.1  christos    when debugging in non-stop mode.  On the other hand, hardware
   2021   1.1  christos    watchpoints (is_hardware_watchpoint; processed here) are specific
   2022   1.1  christos    to each LWP since they are stored in each LWP's hardware debug
   2023   1.1  christos    registers.  Therefore, such LWP must be stopped first in order to
   2024   1.1  christos    be able to modify its hardware watchpoints.
   2025   1.1  christos 
   2026   1.1  christos    Hardware watchpoints must be reset exactly once after being
   2027   1.1  christos    presented to the user.  It cannot be done sooner, because it would
   2028   1.1  christos    reset the data used to present the watchpoint hit to the user.  And
   2029   1.1  christos    it must not be done later because it could display the same single
   2030   1.1  christos    watchpoint hit during multiple GDB stops.  Note that the latter is
   2031   1.1  christos    relevant only to the hardware watchpoint types bp_read_watchpoint
   2032   1.1  christos    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
   2033   1.1  christos    not user-visible - its hit is suppressed if the memory content has
   2034   1.1  christos    not changed.
   2035   1.1  christos 
   2036   1.1  christos    The following constraints influence the location where we can reset
   2037   1.1  christos    hardware watchpoints:
   2038   1.1  christos 
   2039   1.1  christos    * target_stopped_by_watchpoint and target_stopped_data_address are
   2040   1.1  christos      called several times when GDB stops.
   2041   1.1  christos 
   2042   1.1  christos    [linux]
   2043   1.1  christos    * Multiple hardware watchpoints can be hit at the same time,
   2044   1.1  christos      causing GDB to stop.  GDB only presents one hardware watchpoint
   2045   1.1  christos      hit at a time as the reason for stopping, and all the other hits
   2046   1.1  christos      are presented later, one after the other, each time the user
   2047   1.1  christos      requests the execution to be resumed.  Execution is not resumed
   2048   1.1  christos      for the threads still having pending hit event stored in
   2049   1.1  christos      LWP_INFO->STATUS.  While the watchpoint is already removed from
   2050   1.1  christos      the inferior on the first stop the thread hit event is kept being
   2051   1.1  christos      reported from its cached value by linux_nat_stopped_data_address
   2052   1.1  christos      until the real thread resume happens after the watchpoint gets
   2053   1.1  christos      presented and thus its LWP_INFO->STATUS gets reset.
   2054   1.1  christos 
   2055   1.1  christos    Therefore the hardware watchpoint hit can get safely reset on the
   2056   1.1  christos    watchpoint removal from inferior.  */
   2057  1.10  christos 
   2058   1.1  christos static void
   2059  1.10  christos update_watchpoint (struct watchpoint *b, bool reparse)
   2060   1.1  christos {
   2061   1.1  christos   bool within_current_scope;
   2062   1.1  christos 
   2063   1.1  christos   /* If this is a local watchpoint, we only want to check if the
   2064   1.1  christos      watchpoint frame is in scope if the current thread is the thread
   2065   1.1  christos      that was used to create the watchpoint.  */
   2066   1.1  christos   if (!watchpoint_in_thread_scope (b))
   2067   1.8  christos     return;
   2068   1.1  christos 
   2069  1.10  christos   if (b->disposition == disp_del_at_next_stop)
   2070  1.11  christos     return;
   2071   1.1  christos 
   2072   1.1  christos   std::optional<scoped_restore_selected_frame> restore_frame;
   2073   1.1  christos 
   2074  1.10  christos   /* Determine if the watchpoint is within scope.  */
   2075   1.1  christos   if (b->exp_valid_block == NULL)
   2076   1.1  christos     within_current_scope = true;
   2077  1.10  christos   else
   2078   1.1  christos     {
   2079   1.1  christos       frame_info_ptr fi = get_current_frame ();
   2080   1.1  christos       struct gdbarch *frame_arch = get_frame_arch (fi);
   2081   1.5  christos       CORE_ADDR frame_pc = get_frame_pc (fi);
   2082   1.5  christos 
   2083   1.5  christos       /* If we're at a point where the stack has been destroyed
   2084   1.1  christos 	 (e.g. in a function epilogue), unwinding may not work
   2085   1.5  christos 	 properly. Do not attempt to recreate locations at this
   2086   1.1  christos 	 point.  See similar comments in watchpoint_check.  */
   2087   1.1  christos       if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
   2088   1.1  christos 	return;
   2089  1.10  christos 
   2090   1.1  christos       /* Save the current frame's ID so we can restore it after
   2091  1.10  christos 	 evaluating the watchpoint expression on its own frame.  */
   2092  1.10  christos       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
   2093  1.10  christos 	 took a frame parameter, so that we didn't have to change the
   2094   1.1  christos 	 selected frame.  */
   2095   1.1  christos       restore_frame.emplace ();
   2096   1.1  christos 
   2097   1.1  christos       fi = frame_find_by_id (b->watchpoint_frame);
   2098   1.1  christos       within_current_scope = (fi != NULL);
   2099   1.1  christos       if (within_current_scope)
   2100   1.1  christos 	select_frame (fi);
   2101   1.1  christos     }
   2102   1.1  christos 
   2103   1.1  christos   /* We don't free locations.  They are stored in the bp_location array
   2104  1.11  christos      and update_global_location_list will eventually delete them and
   2105   1.1  christos      remove breakpoints if needed.  */
   2106   1.1  christos   b->clear_locations ();
   2107   1.1  christos 
   2108   1.1  christos   if (within_current_scope && reparse)
   2109   1.1  christos     {
   2110   1.7  christos       const char *s;
   2111  1.10  christos 
   2112  1.10  christos       b->exp.reset ();
   2113  1.10  christos       s = (b->exp_string_reparse
   2114   1.1  christos 	   ? b->exp_string_reparse.get ()
   2115   1.1  christos 	   : b->exp_string.get ());
   2116   1.1  christos       b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
   2117   1.1  christos       /* If the meaning of expression itself changed, the old value is
   2118   1.1  christos 	 no longer relevant.  We don't want to report a watchpoint hit
   2119   1.1  christos 	 to the user when the old value and the new value may actually
   2120   1.9  christos 	 be completely different objects.  */
   2121   1.1  christos       b->val = NULL;
   2122   1.1  christos       b->val_valid = false;
   2123   1.1  christos 
   2124   1.1  christos       /* Note that unlike with breakpoints, the watchpoint's condition
   2125   1.8  christos 	 expression is stored in the breakpoint object, not in the
   2126   1.1  christos 	 locations (re)created below.  */
   2127   1.7  christos       if (b->cond_string != NULL)
   2128   1.1  christos 	{
   2129  1.10  christos 	  b->cond_exp.reset ();
   2130   1.1  christos 
   2131   1.1  christos 	  s = b->cond_string.get ();
   2132   1.1  christos 	  b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
   2133   1.1  christos 	}
   2134   1.1  christos     }
   2135   1.1  christos 
   2136   1.1  christos   /* If we failed to parse the expression, for example because
   2137   1.1  christos      it refers to a global variable in a not-yet-loaded shared library,
   2138   1.1  christos      don't try to insert watchpoint.  We don't automatically delete
   2139  1.10  christos      such watchpoint, though, since failure to parse expression
   2140   1.1  christos      is different from out-of-scope watchpoint.  */
   2141   1.1  christos   if (!target_has_execution ())
   2142   1.1  christos     {
   2143   1.1  christos       /* Without execution, memory can't change.  No use to try and
   2144   1.1  christos 	 set watchpoint locations.  The watchpoint will be reset when
   2145   1.1  christos 	 the target gains execution, through breakpoint_re_set.  */
   2146  1.10  christos       if (!can_use_hw_watchpoints)
   2147   1.8  christos 	{
   2148   1.1  christos 	  if (b->works_in_software_mode ())
   2149   1.1  christos 	    b->type = bp_watchpoint;
   2150   1.1  christos 	  else
   2151   1.1  christos 	    error (_("Can't set read/access watchpoint when "
   2152   1.1  christos 		     "hardware watchpoints are disabled."));
   2153   1.1  christos 	}
   2154   1.1  christos     }
   2155   1.8  christos   else if (within_current_scope && b->exp)
   2156   1.8  christos     {
   2157  1.11  christos       std::vector<value_ref_ptr> val_chain;
   2158   1.1  christos       struct value *v, *result;
   2159  1.10  christos       struct program_space *wp_pspace;
   2160  1.10  christos 
   2161   1.1  christos       fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
   2162   1.1  christos 			  &val_chain, false);
   2163   1.1  christos 
   2164   1.1  christos       /* Avoid setting b->val if it's already set.  The meaning of
   2165   1.1  christos 	 b->val is 'the last value' user saw, and we should update
   2166   1.1  christos 	 it only if we reported that last value to user.  As it
   2167   1.1  christos 	 happens, the code that reports it updates b->val directly.
   2168   1.8  christos 	 We don't keep track of the memory value for masked
   2169   1.1  christos 	 watchpoints.  */
   2170   1.3  christos       if (!b->val_valid && !is_masked_watchpoint (b))
   2171   1.8  christos 	{
   2172   1.8  christos 	  if (b->val_bitsize != 0)
   2173   1.9  christos 	    v = extract_bitfield_from_watchpoint_value (b, v);
   2174   1.1  christos 	  b->val = release_value (v);
   2175   1.1  christos 	  b->val_valid = true;
   2176  1.11  christos 	}
   2177  1.11  christos 
   2178  1.11  christos       if (b->exp_valid_block == nullptr)
   2179  1.11  christos 	wp_pspace = current_program_space;
   2180   1.1  christos       else
   2181   1.1  christos 	wp_pspace = get_frame_program_space (get_selected_frame (NULL));
   2182   1.8  christos 
   2183   1.8  christos       /* Look at each value on the value chain.  */
   2184   1.1  christos       gdb_assert (!val_chain.empty ());
   2185   1.8  christos       for (const value_ref_ptr &iter : val_chain)
   2186   1.8  christos 	{
   2187   1.1  christos 	  v = iter.get ();
   2188   1.1  christos 
   2189   1.1  christos 	  /* If it's a memory location, and GDB actually needed
   2190   1.1  christos 	     its contents to evaluate the expression, then we
   2191   1.1  christos 	     must watch it.  If the first value returned is
   2192  1.11  christos 	     still lazy, that means an error occurred reading it;
   2193  1.11  christos 	     watch it anyway in case it becomes readable.  */
   2194   1.1  christos 	  if (v->lval () == lval_memory
   2195  1.11  christos 	      && (v == val_chain[0] || ! v->lazy ()))
   2196   1.1  christos 	    {
   2197   1.1  christos 	      struct type *vtype = check_typedef (v->type ());
   2198   1.1  christos 
   2199   1.1  christos 	      /* We only watch structs and arrays if user asked
   2200   1.1  christos 		 for it explicitly, never if they just happen to
   2201   1.9  christos 		 appear in the middle of some value chain.  */
   2202   1.9  christos 	      if (v == result
   2203   1.1  christos 		  || (vtype->code () != TYPE_CODE_STRUCT
   2204   1.1  christos 		      && vtype->code () != TYPE_CODE_ARRAY))
   2205   1.6  christos 		{
   2206   1.3  christos 		  CORE_ADDR addr;
   2207   1.3  christos 		  enum target_hw_bp_type type;
   2208  1.11  christos 		  int bitpos = 0, bitsize = 0;
   2209   1.3  christos 
   2210   1.3  christos 		  if (v->bitsize () != 0)
   2211   1.3  christos 		    {
   2212  1.11  christos 		      /* Extract the bit parameters out from the bitfield
   2213  1.11  christos 			 sub-expression.  */
   2214   1.3  christos 		      bitpos = v->bitpos ();
   2215   1.3  christos 		      bitsize = v->bitsize ();
   2216   1.3  christos 		    }
   2217   1.3  christos 		  else if (v == result && b->val_bitsize != 0)
   2218   1.3  christos 		    {
   2219   1.3  christos 		     /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
   2220   1.3  christos 			lvalue whose bit parameters are saved in the fields
   2221   1.3  christos 			VAL_BITPOS and VAL_BITSIZE.  */
   2222   1.3  christos 		      bitpos = b->val_bitpos;
   2223   1.1  christos 		      bitsize = b->val_bitsize;
   2224  1.11  christos 		    }
   2225   1.3  christos 
   2226   1.3  christos 		  addr = v->address ();
   2227   1.3  christos 		  if (bitsize != 0)
   2228   1.3  christos 		    {
   2229   1.3  christos 		      /* Skip the bytes that don't contain the bitfield.  */
   2230   1.3  christos 		      addr += bitpos / 8;
   2231   1.1  christos 		    }
   2232   1.8  christos 
   2233   1.1  christos 		  type = hw_write;
   2234   1.8  christos 		  if (b->type == bp_read_watchpoint)
   2235   1.1  christos 		    type = hw_read;
   2236   1.1  christos 		  else if (b->type == bp_access_watchpoint)
   2237  1.11  christos 		    type = hw_access;
   2238  1.11  christos 
   2239  1.11  christos 		  bp_location *loc = b->allocate_location ();
   2240  1.10  christos 		  loc->gdbarch = v->type ()->arch ();
   2241  1.10  christos 		  loc->pspace = wp_pspace;
   2242  1.11  christos 		  loc->address
   2243   1.3  christos 		    = gdbarch_remove_non_address_bits (loc->gdbarch, addr);
   2244   1.3  christos 		  b->add_location (*loc);
   2245   1.3  christos 
   2246   1.3  christos 		  if (bitsize != 0)
   2247   1.3  christos 		    {
   2248   1.3  christos 		      /* Just cover the bytes that make up the bitfield.  */
   2249   1.3  christos 		      loc->length = ((bitpos % 8) + bitsize + 7) / 8;
   2250  1.11  christos 		    }
   2251   1.3  christos 		  else
   2252   1.1  christos 		    loc->length = v->type ()->length ();
   2253   1.1  christos 
   2254   1.1  christos 		  loc->watchpoint_type = type;
   2255   1.1  christos 		}
   2256   1.1  christos 	    }
   2257  1.11  christos 	}
   2258  1.11  christos 
   2259  1.11  christos       /* Helper function to bundle possibly emitting a warning along with
   2260  1.11  christos 	 changing the type of B to bp_watchpoint.  */
   2261  1.11  christos       auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
   2262  1.11  christos       {
   2263  1.11  christos 	/* Only warn for breakpoints that have been assigned a +ve number,
   2264  1.11  christos 	   anything else is either an internal watchpoint (which we don't
   2265  1.11  christos 	   currently create) or has not yet been finalized, in which case
   2266  1.11  christos 	   this change of type will be occurring before the user is told
   2267  1.11  christos 	   the type of this watchpoint.  */
   2268  1.11  christos 	if (bp->type == bp_hardware_watchpoint && bp->number > 0)
   2269  1.11  christos 	  warning (_("watchpoint %d downgraded to software watchpoint"),
   2270  1.11  christos 		   bp->number);
   2271  1.11  christos 	bp->type = bp_watchpoint;
   2272   1.1  christos       };
   2273  1.10  christos 
   2274  1.10  christos       /* Change the type of breakpoint between hardware assisted or
   2275  1.10  christos 	 an ordinary watchpoint depending on the hardware support and
   2276   1.1  christos 	 free hardware slots.  Recheck the number of free hardware slots
   2277   1.1  christos 	 as the value chain may have changed.  */
   2278   1.1  christos 	{
   2279   1.1  christos 	  int reg_cnt;
   2280   1.1  christos 	  enum bp_loc_type loc_type;
   2281   1.1  christos 
   2282   1.1  christos 	  reg_cnt = can_use_hardware_watchpoint (val_chain);
   2283   1.1  christos 
   2284   1.1  christos 	  if (reg_cnt)
   2285   1.1  christos 	    {
   2286   1.1  christos 	      int i, target_resources_ok, other_type_used;
   2287   1.1  christos 	      enum bptype type;
   2288   1.1  christos 
   2289   1.1  christos 	      /* Use an exact watchpoint when there's only one memory region to be
   2290   1.1  christos 		 watched, and only one debug register is needed to watch it.  */
   2291   1.1  christos 	      b->exact = target_exact_watchpoints && reg_cnt == 1;
   2292   1.1  christos 
   2293   1.1  christos 	      /* We need to determine how many resources are already
   2294   1.1  christos 		 used for all other hardware watchpoints plus this one
   2295   1.1  christos 		 to see if we still have enough resources to also fit
   2296   1.1  christos 		 this watchpoint in as well.  */
   2297   1.1  christos 
   2298   1.1  christos 	      /* If this is a software watchpoint, we try to turn it
   2299   1.8  christos 		 to a hardware one -- count resources as if B was of
   2300   1.1  christos 		 hardware watchpoint type.  */
   2301   1.1  christos 	      type = b->type;
   2302   1.1  christos 	      if (type == bp_watchpoint)
   2303   1.1  christos 		type = bp_hardware_watchpoint;
   2304   1.1  christos 
   2305   1.1  christos 	      /* This watchpoint may or may not have been placed on
   2306   1.1  christos 		 the list yet at this point (it won't be in the list
   2307   1.1  christos 		 if we're trying to create it for the first time,
   2308   1.1  christos 		 through watch_command), so always account for it
   2309   1.1  christos 		 manually.  */
   2310   1.8  christos 
   2311   1.1  christos 	      /* Count resources used by all watchpoints except B.  */
   2312   1.1  christos 	      i = hw_watchpoint_used_count_others (b, type, &other_type_used);
   2313   1.8  christos 
   2314   1.1  christos 	      /* Add in the resources needed for B.  */
   2315   1.1  christos 	      i += hw_watchpoint_use_count (b);
   2316   1.1  christos 
   2317   1.1  christos 	      target_resources_ok
   2318   1.1  christos 		= target_can_use_hardware_watchpoint (type, i, other_type_used);
   2319  1.10  christos 	      if (target_resources_ok <= 0)
   2320   1.1  christos 		{
   2321   1.1  christos 		  bool sw_mode = b->works_in_software_mode ();
   2322   1.1  christos 
   2323   1.1  christos 		  if (target_resources_ok == 0 && !sw_mode)
   2324   1.1  christos 		    error (_("Target does not support this type of "
   2325   1.1  christos 			     "hardware watchpoint."));
   2326   1.1  christos 		  else if (target_resources_ok < 0 && !sw_mode)
   2327   1.1  christos 		    error (_("There are not enough available hardware "
   2328   1.1  christos 			     "resources for this watchpoint."));
   2329  1.11  christos 
   2330   1.1  christos 		  /* Downgrade to software watchpoint.  */
   2331   1.1  christos 		  change_type_to_bp_watchpoint (b);
   2332   1.1  christos 		}
   2333   1.1  christos 	      else
   2334   1.1  christos 		{
   2335   1.1  christos 		  /* If this was a software watchpoint, we've just
   2336   1.1  christos 		     found we have enough resources to turn it to a
   2337   1.8  christos 		     hardware watchpoint.  Otherwise, this is a
   2338   1.1  christos 		     nop.  */
   2339   1.1  christos 		  b->type = type;
   2340  1.10  christos 		}
   2341   1.1  christos 	    }
   2342   1.1  christos 	  else if (!b->works_in_software_mode ())
   2343   1.1  christos 	    {
   2344   1.1  christos 	      if (!can_use_hw_watchpoints)
   2345   1.1  christos 		error (_("Can't set read/access watchpoint when "
   2346   1.1  christos 			 "hardware watchpoints are disabled."));
   2347   1.1  christos 	      else
   2348   1.1  christos 		error (_("Expression cannot be implemented with "
   2349   1.1  christos 			 "read/access watchpoint."));
   2350  1.11  christos 	    }
   2351   1.1  christos 	  else
   2352  1.10  christos 	    change_type_to_bp_watchpoint (b);
   2353   1.1  christos 
   2354  1.11  christos 	  loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
   2355  1.11  christos 		      : bp_loc_hardware_watchpoint);
   2356  1.11  christos 
   2357   1.1  christos 	  for (bp_location &bl : b->locations ())
   2358   1.1  christos 	    bl.loc_type = loc_type;
   2359   1.1  christos 	}
   2360   1.1  christos 
   2361   1.1  christos       /* If a software watchpoint is not watching any memory, then the
   2362   1.1  christos 	 above left it without any location set up.  But,
   2363  1.11  christos 	 bpstat_stop_status requires a location to be able to report
   2364  1.11  christos 	 stops, so make sure there's at least a dummy one.  */
   2365   1.1  christos       if (b->type == bp_watchpoint && !b->has_locations ())
   2366   1.1  christos 	add_dummy_location (b, wp_pspace);
   2367   1.1  christos     }
   2368  1.10  christos   else if (!within_current_scope)
   2369   1.1  christos     {
   2370   1.1  christos       gdb_printf (_("\
   2371  1.10  christos Watchpoint %d deleted because the program has left the block\n\
   2372   1.1  christos in which its expression is valid.\n"),
   2373   1.1  christos 		  b->number);
   2374   1.1  christos       watchpoint_del_at_next_stop (b);
   2375   1.1  christos     }
   2376  1.10  christos }
   2377   1.1  christos 
   2378   1.1  christos /* Returns true iff breakpoint location should be
   2379   1.1  christos    inserted in the inferior.  We don't differentiate the type of BL's owner
   2380   1.1  christos    (breakpoint vs. tracepoint), although insert_location in tracepoint's
   2381  1.10  christos    breakpoint_ops is not defined, because in insert_bp_location,
   2382  1.10  christos    tracepoint's insert_location will not be called.  */
   2383   1.1  christos 
   2384   1.1  christos static bool
   2385   1.1  christos should_be_inserted (struct bp_location *bl)
   2386  1.10  christos {
   2387   1.1  christos   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
   2388   1.1  christos     return false;
   2389  1.10  christos 
   2390   1.1  christos   if (bl->owner->disposition == disp_del_at_next_stop)
   2391  1.10  christos     return false;
   2392  1.10  christos 
   2393  1.10  christos   if (!bl->enabled || bl->disabled_by_cond
   2394   1.1  christos       || bl->shlib_disabled || bl->duplicate)
   2395   1.1  christos     return false;
   2396  1.10  christos 
   2397   1.1  christos   if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
   2398   1.1  christos     return false;
   2399   1.1  christos 
   2400   1.1  christos   /* This is set for example, when we're attached to the parent of a
   2401   1.1  christos      vfork, and have detached from the child.  The child is running
   2402   1.1  christos      free, and we expect it to do an exec or exit, at which point the
   2403   1.1  christos      OS makes the parent schedulable again (and the target reports
   2404   1.1  christos      that the vfork is done).  Until the child is done with the shared
   2405   1.1  christos      memory region, do not insert breakpoints in the parent, otherwise
   2406   1.1  christos      the child could still trip on the parent's breakpoints.  Since
   2407  1.10  christos      the parent is blocked anyway, it won't miss any breakpoint.  */
   2408   1.1  christos   if (bl->pspace->breakpoints_not_allowed)
   2409   1.3  christos     return false;
   2410   1.6  christos 
   2411   1.6  christos   /* Don't insert a breakpoint if we're trying to step past its
   2412   1.6  christos      location, except if the breakpoint is a single-step breakpoint,
   2413   1.3  christos      and the breakpoint's thread is the thread which is stepping past
   2414   1.3  christos      a breakpoint.  */
   2415  1.11  christos   if ((bl->loc_type == bp_loc_software_breakpoint
   2416   1.6  christos        || bl->loc_type == bp_loc_hardware_breakpoint)
   2417   1.6  christos       && stepping_past_instruction_at (bl->pspace->aspace.get (),
   2418   1.6  christos 				       bl->address)
   2419   1.6  christos       /* The single-step breakpoint may be inserted at the location
   2420   1.6  christos 	 we're trying to step if the instruction branches to itself.
   2421   1.6  christos 	 However, the instruction won't be executed at all and it may
   2422   1.6  christos 	 break the semantics of the instruction, for example, the
   2423   1.6  christos 	 instruction is a conditional branch or updates some flags.
   2424   1.6  christos 	 We can't fix it unless GDB is able to emulate the instruction
   2425   1.6  christos 	 or switch to displaced stepping.  */
   2426   1.3  christos       && !(bl->owner->type == bp_single_step
   2427   1.9  christos 	   && thread_is_stepping_over_breakpoint (bl->owner->thread)))
   2428   1.9  christos     {
   2429  1.10  christos       infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
   2430   1.3  christos 			   paddress (bl->gdbarch, bl->address));
   2431   1.3  christos       return false;
   2432   1.3  christos     }
   2433   1.3  christos 
   2434   1.3  christos   /* Don't insert watchpoints if we're trying to step past the
   2435   1.3  christos      instruction that triggered one.  */
   2436   1.3  christos   if ((bl->loc_type == bp_loc_hardware_watchpoint)
   2437   1.9  christos       && stepping_past_nonsteppable_watchpoint ())
   2438  1.10  christos     {
   2439   1.9  christos       infrun_debug_printf ("stepping past non-steppable watchpoint. "
   2440  1.10  christos 			   "skipping watchpoint at %s:%d",
   2441   1.3  christos 			   paddress (bl->gdbarch, bl->address), bl->length);
   2442   1.3  christos       return false;
   2443  1.10  christos     }
   2444   1.1  christos 
   2445   1.1  christos   return true;
   2446   1.1  christos }
   2447   1.1  christos 
   2448   1.1  christos /* Same as should_be_inserted but does the check assuming
   2449  1.10  christos    that the location is not duplicated.  */
   2450   1.1  christos 
   2451   1.1  christos static bool
   2452  1.10  christos unduplicated_should_be_inserted (struct bp_location *bl)
   2453  1.10  christos {
   2454   1.1  christos   scoped_restore restore_bl_duplicate
   2455  1.10  christos     = make_scoped_restore (&bl->duplicate, 0);
   2456   1.1  christos 
   2457   1.1  christos   return should_be_inserted (bl);
   2458   1.1  christos }
   2459   1.1  christos 
   2460   1.1  christos /* Parses a conditional described by an expression COND into an
   2461   1.1  christos    agent expression bytecode suitable for evaluation
   2462   1.1  christos    by the bytecode interpreter.  Return NULL if there was
   2463   1.7  christos    any error during parsing.  */
   2464   1.1  christos 
   2465   1.1  christos static agent_expr_up
   2466   1.7  christos parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
   2467   1.7  christos {
   2468   1.1  christos   if (cond == NULL)
   2469   1.7  christos     return NULL;
   2470   1.1  christos 
   2471   1.1  christos   agent_expr_up aexpr;
   2472   1.1  christos 
   2473   1.9  christos   /* We don't want to stop processing, so catch any errors
   2474   1.1  christos      that may show up.  */
   2475   1.1  christos   try
   2476   1.1  christos     {
   2477   1.1  christos       aexpr = gen_eval_for_expr (scope, cond);
   2478   1.9  christos     }
   2479   1.1  christos 
   2480   1.1  christos   catch (const gdb_exception_error &ex)
   2481   1.1  christos     {
   2482   1.1  christos       /* If we got here, it means the condition could not be parsed to a valid
   2483   1.1  christos 	 bytecode expression and thus can't be evaluated on the target's side.
   2484   1.1  christos 	 It's no use iterating through the conditions.  */
   2485   1.1  christos     }
   2486   1.1  christos 
   2487   1.1  christos   /* We have a valid agent expression.  */
   2488   1.1  christos   return aexpr;
   2489   1.1  christos }
   2490   1.1  christos 
   2491   1.1  christos /* Based on location BL, create a list of breakpoint conditions to be
   2492   1.1  christos    passed on to the target.  If we have duplicated locations with different
   2493   1.1  christos    conditions, we will add such conditions to the list.  The idea is that the
   2494   1.1  christos    target will evaluate the list of conditions and will only notify GDB when
   2495   1.1  christos    one of them is true.  */
   2496   1.1  christos 
   2497   1.1  christos static void
   2498  1.10  christos build_target_condition_list (struct bp_location *bl)
   2499   1.1  christos {
   2500   1.1  christos   bool null_condition_or_parse_error = false;
   2501   1.1  christos   int modified = bl->needs_update;
   2502   1.7  christos 
   2503   1.1  christos   /* Release conditions left over from a previous insert.  */
   2504   1.1  christos   bl->target_info.conditions.clear ();
   2505   1.1  christos 
   2506   1.1  christos   /* This is only meaningful if the target is
   2507   1.1  christos      evaluating conditions and if the user has
   2508   1.1  christos      opted for condition evaluation on the target's
   2509   1.1  christos      side.  */
   2510   1.1  christos   if (gdb_evaluates_breakpoint_condition_p ()
   2511   1.1  christos       || !target_supports_evaluation_of_breakpoint_conditions ())
   2512  1.10  christos     return;
   2513  1.10  christos 
   2514   1.1  christos   auto loc_range = all_bp_locations_at_addr (bl->address);
   2515   1.9  christos 
   2516   1.9  christos   /* Do a first pass to check for locations with no assigned
   2517   1.9  christos      conditions or conditions that fail to parse to a valid agent
   2518   1.9  christos      expression bytecode.  If any of these happen, then it's no use to
   2519   1.9  christos      send conditions to the target since this location will always
   2520   1.9  christos      trigger and generate a response back to GDB.  Note we consider
   2521   1.9  christos      all locations at the same address irrespective of type, i.e.,
   2522   1.9  christos      even if the locations aren't considered duplicates (e.g.,
   2523  1.10  christos      software breakpoint and hardware breakpoint at the same
   2524   1.1  christos      address).  */
   2525   1.1  christos   for (bp_location *loc : loc_range)
   2526   1.1  christos     {
   2527   1.1  christos       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
   2528   1.1  christos 	{
   2529   1.1  christos 	  if (modified)
   2530   1.1  christos 	    {
   2531   1.1  christos 	      /* Re-parse the conditions since something changed.  In that
   2532   1.1  christos 		 case we already freed the condition bytecodes (see
   2533   1.7  christos 		 force_breakpoint_reinsertion).  We just
   2534   1.7  christos 		 need to parse the condition to bytecodes again.  */
   2535   1.1  christos 	      loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
   2536   1.1  christos 							loc->cond.get ());
   2537   1.1  christos 	    }
   2538   1.1  christos 
   2539   1.1  christos 	  /* If we have a NULL bytecode expression, it means something
   2540   1.1  christos 	     went wrong or we have a null condition expression.  */
   2541  1.10  christos 	  if (!loc->cond_bytecode)
   2542   1.1  christos 	    {
   2543   1.1  christos 	      null_condition_or_parse_error = true;
   2544   1.1  christos 	      break;
   2545   1.1  christos 	    }
   2546   1.1  christos 	}
   2547   1.1  christos     }
   2548   1.1  christos 
   2549   1.1  christos   /* If any of these happened, it means we will have to evaluate the conditions
   2550   1.1  christos      for the location's address on gdb's side.  It is no use keeping bytecodes
   2551   1.1  christos      for all the other duplicate locations, thus we free all of them here.
   2552   1.1  christos 
   2553   1.1  christos      This is so we have a finer control over which locations' conditions are
   2554   1.1  christos      being evaluated by GDB or the remote stub.  */
   2555  1.10  christos   if (null_condition_or_parse_error)
   2556   1.1  christos     {
   2557   1.1  christos       for (bp_location *loc : loc_range)
   2558   1.1  christos 	{
   2559   1.1  christos 	  if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
   2560   1.1  christos 	    {
   2561   1.1  christos 	      /* Only go as far as the first NULL bytecode is
   2562   1.1  christos 		 located.  */
   2563   1.1  christos 	      if (!loc->cond_bytecode)
   2564   1.7  christos 		return;
   2565   1.1  christos 
   2566   1.1  christos 	      loc->cond_bytecode.reset ();
   2567   1.1  christos 	    }
   2568   1.1  christos 	}
   2569   1.9  christos     }
   2570   1.9  christos 
   2571   1.9  christos   /* No NULL conditions or failed bytecode generation.  Build a
   2572  1.11  christos      condition list for this location's address.  If we have software
   2573   1.9  christos      and hardware locations at the same address, they aren't
   2574   1.9  christos      considered duplicates, but we still merge all the conditions
   2575  1.10  christos      anyway, as it's simpler, and doesn't really make a practical
   2576  1.10  christos      difference.  */
   2577  1.10  christos   for (bp_location *loc : loc_range)
   2578  1.10  christos     if (loc->cond
   2579  1.10  christos 	&& is_breakpoint (loc->owner)
   2580  1.10  christos 	&& loc->pspace->num == bl->pspace->num
   2581  1.10  christos 	&& loc->owner->enable_state == bp_enabled
   2582  1.10  christos 	&& loc->enabled
   2583  1.10  christos 	&& !loc->disabled_by_cond)
   2584  1.10  christos       {
   2585  1.10  christos 	/* Add the condition to the vector.  This will be used later
   2586  1.10  christos 	   to send the conditions to the target.  */
   2587   1.1  christos 	bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
   2588   1.1  christos       }
   2589   1.1  christos 
   2590   1.1  christos   return;
   2591   1.1  christos }
   2592   1.1  christos 
   2593   1.1  christos /* Parses a command described by string CMD into an agent expression
   2594   1.1  christos    bytecode suitable for evaluation by the bytecode interpreter.
   2595   1.7  christos    Return NULL if there was any error during parsing.  */
   2596   1.1  christos 
   2597   1.1  christos static agent_expr_up
   2598   1.1  christos parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
   2599   1.1  christos {
   2600   1.1  christos   const char *cmdrest;
   2601   1.1  christos   const char *format_start, *format_end;
   2602   1.7  christos   struct gdbarch *gdbarch = get_current_arch ();
   2603   1.1  christos 
   2604   1.1  christos   if (cmd == NULL)
   2605   1.1  christos     return NULL;
   2606   1.1  christos 
   2607   1.1  christos   cmdrest = cmd;
   2608   1.1  christos 
   2609   1.8  christos   if (*cmdrest == ',')
   2610   1.1  christos     ++cmdrest;
   2611   1.1  christos   cmdrest = skip_spaces (cmdrest);
   2612   1.1  christos 
   2613   1.1  christos   if (*cmdrest++ != '"')
   2614   1.1  christos     error (_("No format string following the location"));
   2615   1.1  christos 
   2616   1.8  christos   format_start = cmdrest;
   2617   1.1  christos 
   2618   1.1  christos   format_pieces fpieces (&cmdrest);
   2619   1.1  christos 
   2620   1.1  christos   format_end = cmdrest;
   2621   1.1  christos 
   2622   1.1  christos   if (*cmdrest++ != '"')
   2623   1.8  christos     error (_("Bad format string, non-terminated '\"'."));
   2624   1.1  christos 
   2625   1.1  christos   cmdrest = skip_spaces (cmdrest);
   2626   1.1  christos 
   2627   1.1  christos   if (!(*cmdrest == ',' || *cmdrest == '\0'))
   2628   1.1  christos     error (_("Invalid argument syntax"));
   2629   1.1  christos 
   2630   1.8  christos   if (*cmdrest == ',')
   2631   1.1  christos     cmdrest++;
   2632   1.1  christos   cmdrest = skip_spaces (cmdrest);
   2633   1.1  christos 
   2634   1.8  christos   /* For each argument, make an expression.  */
   2635   1.1  christos 
   2636   1.1  christos   std::vector<struct expression *> argvec;
   2637   1.1  christos   while (*cmdrest != '\0')
   2638   1.1  christos     {
   2639   1.1  christos       const char *cmd1;
   2640  1.11  christos 
   2641  1.11  christos       cmd1 = cmdrest;
   2642   1.8  christos       expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope),
   2643   1.1  christos 					PARSER_COMMA_TERMINATES);
   2644   1.1  christos       argvec.push_back (expr.release ());
   2645   1.1  christos       cmdrest = cmd1;
   2646   1.1  christos       if (*cmdrest == ',')
   2647   1.1  christos 	++cmdrest;
   2648   1.7  christos     }
   2649   1.7  christos 
   2650   1.1  christos   agent_expr_up aexpr;
   2651   1.1  christos 
   2652   1.9  christos   /* We don't want to stop processing, so catch any errors
   2653   1.1  christos      that may show up.  */
   2654   1.1  christos   try
   2655   1.1  christos     {
   2656   1.8  christos       aexpr = gen_printf (scope, gdbarch, 0, 0,
   2657   1.1  christos 			  format_start, format_end - format_start,
   2658   1.9  christos 			  argvec.size (), argvec.data ());
   2659   1.1  christos     }
   2660   1.1  christos   catch (const gdb_exception_error &ex)
   2661   1.1  christos     {
   2662   1.1  christos       /* If we got here, it means the command could not be parsed to a valid
   2663   1.1  christos 	 bytecode expression and thus can't be evaluated on the target's side.
   2664   1.5  christos 	 It's no use iterating through the other commands.  */
   2665   1.1  christos     }
   2666   1.1  christos 
   2667   1.1  christos   /* We have a valid agent expression, return it.  */
   2668   1.1  christos   return aexpr;
   2669   1.1  christos }
   2670   1.1  christos 
   2671   1.1  christos /* Based on location BL, create a list of breakpoint commands to be
   2672   1.1  christos    passed on to the target.  If we have duplicated locations with
   2673   1.1  christos    different commands, we will add any such to the list.  */
   2674   1.1  christos 
   2675   1.1  christos static void
   2676  1.10  christos build_target_command_list (struct bp_location *bl)
   2677   1.1  christos {
   2678   1.1  christos   bool null_command_or_parse_error = false;
   2679   1.7  christos   int modified = bl->needs_update;
   2680   1.7  christos 
   2681   1.1  christos   /* Clear commands left over from a previous insert.  */
   2682   1.3  christos   bl->target_info.tcommands.clear ();
   2683   1.3  christos 
   2684   1.3  christos   if (!target_can_run_breakpoint_commands ())
   2685   1.1  christos     return;
   2686   1.3  christos 
   2687   1.1  christos   /* For now, limit to agent-style dprintf breakpoints.  */
   2688   1.1  christos   if (dprintf_style != dprintf_style_agent)
   2689  1.10  christos     return;
   2690  1.10  christos 
   2691   1.9  christos   auto loc_range = all_bp_locations_at_addr (bl->address);
   2692   1.9  christos 
   2693   1.9  christos   /* For now, if we have any location at the same address that isn't a
   2694   1.3  christos      dprintf, don't install the target-side commands, as that would
   2695  1.10  christos      make the breakpoint not be reported to the core, and we'd lose
   2696  1.10  christos      control.  */
   2697  1.10  christos   for (bp_location *loc : loc_range)
   2698  1.10  christos     if (is_breakpoint (loc->owner)
   2699  1.10  christos 	&& loc->pspace->num == bl->pspace->num
   2700   1.1  christos 	&& loc->owner->type != bp_dprintf)
   2701   1.1  christos       return;
   2702   1.1  christos 
   2703   1.1  christos   /* Do a first pass to check for locations with no assigned
   2704   1.1  christos      conditions or conditions that fail to parse to a valid agent expression
   2705   1.1  christos      bytecode.  If any of these happen, then it's no use to send conditions
   2706  1.10  christos      to the target since this location will always trigger and generate a
   2707   1.1  christos      response back to GDB.  */
   2708   1.1  christos   for (bp_location *loc : loc_range)
   2709   1.1  christos     {
   2710   1.1  christos       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
   2711   1.1  christos 	{
   2712   1.1  christos 	  if (modified)
   2713   1.1  christos 	    {
   2714   1.1  christos 	      /* Re-parse the commands since something changed.  In that
   2715   1.1  christos 		 case we already freed the command bytecodes (see
   2716   1.7  christos 		 force_breakpoint_reinsertion).  We just
   2717   1.7  christos 		 need to parse the command to bytecodes again.  */
   2718  1.10  christos 	      loc->cmd_bytecode
   2719   1.1  christos 		= parse_cmd_to_aexpr (bl->address,
   2720   1.1  christos 				      loc->owner->extra_string.get ());
   2721   1.1  christos 	    }
   2722   1.1  christos 
   2723   1.1  christos 	  /* If we have a NULL bytecode expression, it means something
   2724   1.1  christos 	     went wrong or we have a null command expression.  */
   2725  1.10  christos 	  if (!loc->cmd_bytecode)
   2726   1.1  christos 	    {
   2727   1.1  christos 	      null_command_or_parse_error = true;
   2728   1.1  christos 	      break;
   2729   1.1  christos 	    }
   2730   1.1  christos 	}
   2731   1.1  christos     }
   2732   1.1  christos 
   2733   1.1  christos   /* If anything failed, then we're not doing target-side commands,
   2734   1.1  christos      and so clean up.  */
   2735  1.10  christos   if (null_command_or_parse_error)
   2736  1.10  christos     {
   2737  1.10  christos       for (bp_location *loc : loc_range)
   2738  1.10  christos 	if (is_breakpoint (loc->owner)
   2739  1.10  christos 	    && loc->pspace->num == bl->pspace->num)
   2740  1.10  christos 	  {
   2741  1.10  christos 	    /* Only go as far as the first NULL bytecode is
   2742  1.10  christos 	       located.  */
   2743   1.1  christos 	    if (loc->cmd_bytecode == NULL)
   2744  1.10  christos 	      return;
   2745  1.10  christos 
   2746   1.1  christos 	    loc->cmd_bytecode.reset ();
   2747   1.1  christos 	  }
   2748   1.9  christos     }
   2749   1.9  christos 
   2750   1.9  christos   /* No NULL commands or failed bytecode generation.  Build a command
   2751   1.9  christos      list for all duplicate locations at this location's address.
   2752   1.9  christos      Note that here we must care for whether the breakpoint location
   2753   1.9  christos      types are considered duplicates, otherwise, say, if we have a
   2754   1.9  christos      software and hardware location at the same address, the target
   2755   1.9  christos      could end up running the commands twice.  For the moment, we only
   2756  1.10  christos      support targets-side commands with dprintf, but it doesn't hurt
   2757  1.10  christos      to be pedantically correct in case that changes.  */
   2758  1.10  christos   for (bp_location *loc : loc_range)
   2759  1.10  christos     if (breakpoint_locations_match (bl, loc)
   2760  1.10  christos 	&& loc->owner->extra_string
   2761  1.10  christos 	&& is_breakpoint (loc->owner)
   2762  1.10  christos 	&& loc->pspace->num == bl->pspace->num
   2763  1.10  christos 	&& loc->owner->enable_state == bp_enabled
   2764  1.10  christos 	&& loc->enabled
   2765  1.10  christos 	&& !loc->disabled_by_cond)
   2766  1.10  christos       {
   2767  1.10  christos 	/* Add the command to the vector.  This will be used later
   2768  1.10  christos 	   to send the commands to the target.  */
   2769   1.1  christos 	bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
   2770   1.1  christos       }
   2771   1.1  christos 
   2772   1.1  christos   bl->target_info.persist = 0;
   2773   1.1  christos   /* Maybe flag this location as persistent.  */
   2774   1.1  christos   if (bl->owner->type == bp_dprintf && disconnected_dprintf)
   2775   1.1  christos     bl->target_info.persist = 1;
   2776   1.7  christos }
   2777   1.7  christos 
   2778   1.7  christos /* Return the kind of breakpoint on address *ADDR.  Get the kind
   2779   1.7  christos    of breakpoint according to ADDR except single-step breakpoint.
   2780   1.7  christos    Get the kind of single-step breakpoint according to the current
   2781   1.7  christos    registers state.  */
   2782  1.10  christos 
   2783   1.7  christos static int
   2784   1.7  christos breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
   2785   1.7  christos {
   2786   1.7  christos   if (bl->owner->type == bp_single_step)
   2787   1.7  christos     {
   2788   1.7  christos       struct thread_info *thr = find_thread_global_id (bl->owner->thread);
   2789   1.8  christos       struct regcache *regcache;
   2790   1.7  christos 
   2791   1.7  christos       regcache = get_thread_regcache (thr);
   2792   1.7  christos 
   2793   1.7  christos       return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
   2794   1.7  christos 							 regcache, addr);
   2795   1.7  christos     }
   2796   1.7  christos   else
   2797   1.7  christos     return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
   2798  1.10  christos }
   2799  1.10  christos 
   2800  1.10  christos /* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
   2801  1.10  christos    E is either the currently handled exception, or a copy, or a sliced copy,
   2802  1.10  christos    so we can't rethrow that one, but we can use it to inspect the properties
   2803  1.10  christos    of the currently handled exception.  */
   2804  1.10  christos 
   2805  1.10  christos static void
   2806  1.10  christos rethrow_on_target_close_error (const gdb_exception &e)
   2807  1.10  christos {
   2808  1.10  christos   if (e.reason == 0)
   2809  1.10  christos     return;
   2810  1.10  christos   /* Can't set the breakpoint.  */
   2811  1.10  christos 
   2812  1.10  christos   if (e.error != TARGET_CLOSE_ERROR)
   2813  1.10  christos     return;
   2814  1.10  christos 
   2815  1.10  christos   /* If the target has closed then it will have deleted any breakpoints
   2816  1.10  christos      inserted within the target inferior, as a result any further attempts
   2817  1.10  christos      to interact with the breakpoint objects is not possible.  Just rethrow
   2818  1.10  christos      the error.  Don't use e to rethrow, to prevent object slicing of the
   2819  1.10  christos      exception.  */
   2820  1.10  christos   throw;
   2821   1.1  christos }
   2822   1.1  christos 
   2823   1.1  christos /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
   2824   1.1  christos    location.  Any error messages are printed to TMP_ERROR_STREAM; and
   2825   1.1  christos    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
   2826   1.1  christos    Returns 0 for success, 1 if the bp_location type is not supported or
   2827   1.1  christos    -1 for failure.
   2828   1.1  christos 
   2829   1.1  christos    NOTE drow/2003-09-09: This routine could be broken down to an
   2830   1.1  christos    object-style method for each breakpoint or catchpoint type.  */
   2831   1.1  christos static int
   2832   1.1  christos insert_bp_location (struct bp_location *bl,
   2833   1.1  christos 		    struct ui_file *tmp_error_stream,
   2834   1.1  christos 		    int *disabled_breaks,
   2835   1.1  christos 		    int *hw_breakpoint_error,
   2836   1.9  christos 		    int *hw_bp_error_explained_already)
   2837   1.1  christos {
   2838   1.1  christos   gdb_exception bp_excpt;
   2839   1.1  christos 
   2840   1.1  christos   if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
   2841  1.11  christos     return 0;
   2842  1.11  christos 
   2843   1.1  christos   breakpoint_debug_printf ("%s", breakpoint_location_address_str (bl).c_str ());
   2844   1.1  christos 
   2845   1.1  christos   /* Note we don't initialize bl->target_info, as that wipes out
   2846   1.1  christos      the breakpoint location's shadow_contents if the breakpoint
   2847   1.1  christos      is still inserted at that location.  This in turn breaks
   2848   1.1  christos      target_read_memory which depends on these buffers when
   2849   1.1  christos      a memory read is requested at the breakpoint location:
   2850   1.1  christos      Once the target_info has been wiped, we fail to see that
   2851   1.1  christos      we have a breakpoint inserted at that address and thus
   2852   1.3  christos      read the breakpoint instead of returning the data saved in
   2853  1.11  christos      the breakpoint location's shadow contents.  */
   2854   1.1  christos   bl->target_info.reqstd_address = bl->address;
   2855   1.1  christos   bl->target_info.placed_address_space = bl->pspace->aspace.get ();
   2856   1.1  christos   bl->target_info.length = bl->length;
   2857   1.1  christos 
   2858   1.1  christos   /* When working with target-side conditions, we must pass all the conditions
   2859   1.1  christos      for the same breakpoint address down to the target since GDB will not
   2860   1.1  christos      insert those locations.  With a list of breakpoint conditions, the target
   2861   1.1  christos      can decide when to stop and notify GDB.  */
   2862   1.1  christos 
   2863   1.1  christos   if (is_breakpoint (bl->owner))
   2864   1.1  christos     {
   2865   1.1  christos       build_target_condition_list (bl);
   2866   1.1  christos       build_target_command_list (bl);
   2867   1.1  christos       /* Reset the modification marker.  */
   2868   1.1  christos       bl->needs_update = 0;
   2869   1.9  christos     }
   2870   1.9  christos 
   2871   1.9  christos   /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
   2872   1.9  christos      set at a read-only address, then a breakpoint location will have
   2873   1.9  christos      been changed to hardware breakpoint before we get here.  If it is
   2874   1.1  christos      "off" however, error out before actually trying to insert the
   2875   1.9  christos      breakpoint, with a nicer error message.  */
   2876   1.1  christos   if (bl->loc_type == bp_loc_software_breakpoint
   2877   1.9  christos       && !automatic_hardware_breakpoints)
   2878   1.9  christos     {
   2879   1.9  christos       mem_region *mr = lookup_mem_region (bl->address);
   2880   1.1  christos 
   2881  1.10  christos       if (mr != nullptr && mr->attrib.mode != MEM_RW)
   2882  1.10  christos 	{
   2883  1.10  christos 	  gdb_printf (tmp_error_stream,
   2884  1.10  christos 		      _("Cannot insert breakpoint %d.\n"
   2885  1.10  christos 			"Cannot set software breakpoint "
   2886  1.10  christos 			"at read-only address %s\n"),
   2887   1.9  christos 		      bl->owner->number,
   2888   1.9  christos 		      paddress (bl->gdbarch, bl->address));
   2889   1.9  christos 	  return 1;
   2890   1.1  christos 	}
   2891   1.9  christos     }
   2892   1.9  christos 
   2893   1.9  christos   if (bl->loc_type == bp_loc_software_breakpoint
   2894   1.1  christos       || bl->loc_type == bp_loc_hardware_breakpoint)
   2895   1.1  christos     {
   2896   1.1  christos       /* First check to see if we have to handle an overlay.  */
   2897   1.1  christos       if (overlay_debugging == ovly_off
   2898   1.1  christos 	  || bl->section == NULL
   2899   1.1  christos 	  || !(section_is_overlay (bl->section)))
   2900   1.9  christos 	{
   2901   1.1  christos 	  /* No overlay handling: just set the breakpoint.  */
   2902   1.3  christos 	  try
   2903   1.3  christos 	    {
   2904  1.10  christos 	      int val;
   2905   1.3  christos 
   2906   1.8  christos 	      val = bl->owner->insert_location (bl);
   2907   1.1  christos 	      if (val)
   2908   1.9  christos 		bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
   2909   1.1  christos 	    }
   2910  1.10  christos 	  catch (gdb_exception &e)
   2911   1.9  christos 	    {
   2912   1.1  christos 	      rethrow_on_target_close_error (e);
   2913   1.1  christos 	      bp_excpt = std::move (e);
   2914   1.1  christos 	    }
   2915   1.1  christos 	}
   2916   1.1  christos       else
   2917   1.1  christos 	{
   2918   1.1  christos 	  /* This breakpoint is in an overlay section.
   2919   1.1  christos 	     Shall we set a breakpoint at the LMA?  */
   2920   1.1  christos 	  if (!overlay_events_enabled)
   2921   1.1  christos 	    {
   2922   1.1  christos 	      /* Yes -- overlay event support is not active,
   2923   1.1  christos 		 so we must try to set a breakpoint at the LMA.
   2924   1.1  christos 		 This will not work for a hardware breakpoint.  */
   2925   1.1  christos 	      if (bl->loc_type == bp_loc_hardware_breakpoint)
   2926   1.1  christos 		warning (_("hardware breakpoint %d not supported in overlay!"),
   2927   1.1  christos 			 bl->owner->number);
   2928   1.1  christos 	      else
   2929   1.1  christos 		{
   2930   1.1  christos 		  CORE_ADDR addr = overlay_unmapped_address (bl->address,
   2931   1.1  christos 							     bl->section);
   2932   1.3  christos 		  /* Set a software (trap) breakpoint at the LMA.  */
   2933   1.3  christos 		  bl->overlay_target_info = bl->target_info;
   2934   1.3  christos 		  bl->overlay_target_info.reqstd_address = addr;
   2935   1.9  christos 
   2936   1.3  christos 		  /* No overlay handling: just set the breakpoint.  */
   2937   1.3  christos 		  try
   2938   1.3  christos 		    {
   2939   1.7  christos 		      int val;
   2940   1.7  christos 
   2941   1.7  christos 		      bl->overlay_target_info.kind
   2942   1.3  christos 			= breakpoint_kind (bl, &addr);
   2943   1.3  christos 		      bl->overlay_target_info.placed_address = addr;
   2944   1.3  christos 		      val = target_insert_breakpoint (bl->gdbarch,
   2945   1.8  christos 						      &bl->overlay_target_info);
   2946   1.8  christos 		      if (val)
   2947   1.3  christos 			bp_excpt
   2948   1.9  christos 			  = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
   2949   1.3  christos 		    }
   2950  1.10  christos 		  catch (gdb_exception &e)
   2951   1.9  christos 		    {
   2952   1.3  christos 		      rethrow_on_target_close_error (e);
   2953   1.3  christos 		      bp_excpt = std::move (e);
   2954   1.8  christos 		    }
   2955  1.10  christos 
   2956  1.10  christos 		  if (bp_excpt.reason != 0)
   2957  1.10  christos 		    gdb_printf (tmp_error_stream,
   2958  1.10  christos 				"Overlay breakpoint %d "
   2959   1.1  christos 				"failed: in ROM?\n",
   2960   1.1  christos 				bl->owner->number);
   2961   1.1  christos 		}
   2962   1.1  christos 	    }
   2963   1.1  christos 	  /* Shall we set a breakpoint at the VMA? */
   2964   1.1  christos 	  if (section_is_mapped (bl->section))
   2965   1.9  christos 	    {
   2966  1.10  christos 	      /* Yes.  This overlay section is mapped into memory.  */
   2967   1.3  christos 	      try
   2968   1.3  christos 		{
   2969  1.10  christos 		  int val;
   2970   1.3  christos 
   2971   1.8  christos 		  val = bl->owner->insert_location (bl);
   2972  1.10  christos 		  if (val)
   2973  1.11  christos 		    bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
   2974  1.10  christos 		}
   2975  1.10  christos 	      catch (gdb_exception_error &e)
   2976   1.9  christos 		{
   2977  1.10  christos 		  rethrow_on_target_close_error (e);
   2978   1.1  christos 		  bp_excpt = std::move (e);
   2979   1.1  christos 		}
   2980   1.1  christos 	    }
   2981   1.1  christos 	  else
   2982   1.1  christos 	    {
   2983   1.1  christos 	      /* No.  This breakpoint will not be inserted.
   2984   1.1  christos 		 No error, but do not mark the bp as 'inserted'.  */
   2985   1.1  christos 	      return 0;
   2986   1.1  christos 	    }
   2987   1.8  christos 	}
   2988   1.1  christos 
   2989   1.1  christos       if (bp_excpt.reason != 0)
   2990  1.10  christos 	{
   2991   1.3  christos 	  /* Can't set the breakpoint.  */
   2992   1.3  christos 	  gdb_assert (bl->owner != nullptr);
   2993   1.3  christos 
   2994   1.3  christos 	  /* In some cases, we might not be able to insert a
   2995   1.3  christos 	     breakpoint in a shared library that has already been
   2996   1.3  christos 	     removed, but we have not yet processed the shlib unload
   2997   1.3  christos 	     event.  Unfortunately, some targets that implement
   2998   1.3  christos 	     breakpoint insertion themselves can't tell why the
   2999   1.3  christos 	     breakpoint insertion failed (e.g., the remote target
   3000   1.8  christos 	     doesn't define error codes), so we must treat generic
   3001   1.8  christos 	     errors as memory errors.  */
   3002   1.8  christos 	  if (bp_excpt.reason == RETURN_ERROR
   3003   1.3  christos 	      && (bp_excpt.error == GENERIC_ERROR
   3004   1.3  christos 		  || bp_excpt.error == MEMORY_ERROR)
   3005   1.3  christos 	      && bl->loc_type == bp_loc_software_breakpoint
   3006   1.3  christos 	      && (solib_name_from_address (bl->pspace, bl->address)
   3007   1.1  christos 		  || shared_objfile_contains_address_p (bl->pspace,
   3008   1.1  christos 							bl->address)))
   3009   1.1  christos 	    {
   3010  1.11  christos 	      /* See also: disable_breakpoints_in_shlibs.  */
   3011   1.1  christos 	      bl->shlib_disabled = 1;
   3012   1.1  christos 	      notify_breakpoint_modified (bl->owner);
   3013  1.10  christos 	      if (!*disabled_breaks)
   3014  1.10  christos 		{
   3015  1.10  christos 		  gdb_printf (tmp_error_stream,
   3016  1.10  christos 			      "Cannot insert breakpoint %d.\n",
   3017  1.10  christos 			      bl->owner->number);
   3018  1.10  christos 		  gdb_printf (tmp_error_stream,
   3019   1.1  christos 			      "Temporarily disabling shared "
   3020   1.1  christos 			      "library breakpoints:\n");
   3021  1.10  christos 		}
   3022  1.10  christos 	      *disabled_breaks = 1;
   3023   1.3  christos 	      gdb_printf (tmp_error_stream,
   3024   1.1  christos 			  "breakpoint #%d\n", bl->owner->number);
   3025   1.1  christos 	      return 0;
   3026   1.1  christos 	    }
   3027   1.1  christos 	  else
   3028   1.1  christos 	    {
   3029   1.3  christos 	      if (bl->loc_type == bp_loc_hardware_breakpoint)
   3030   1.8  christos 		{
   3031  1.10  christos 		  *hw_breakpoint_error = 1;
   3032  1.10  christos 		  *hw_bp_error_explained_already = bp_excpt.message != NULL;
   3033  1.10  christos 		  gdb_printf (tmp_error_stream,
   3034  1.10  christos 			      "Cannot insert hardware breakpoint %d%s",
   3035  1.10  christos 			      bl->owner->number,
   3036  1.10  christos 			      bp_excpt.message ? ":" : ".\n");
   3037  1.10  christos 		  if (bp_excpt.message != NULL)
   3038   1.1  christos 		    gdb_printf (tmp_error_stream, "%s.\n",
   3039   1.1  christos 				bp_excpt.what ());
   3040   1.1  christos 		}
   3041   1.8  christos 	      else
   3042   1.3  christos 		{
   3043   1.8  christos 		  if (bp_excpt.message == NULL)
   3044   1.3  christos 		    {
   3045   1.3  christos 		      std::string message
   3046   1.3  christos 			= memory_error_message (TARGET_XFER_E_IO,
   3047  1.10  christos 						bl->gdbarch, bl->address);
   3048  1.10  christos 
   3049  1.10  christos 		      gdb_printf (tmp_error_stream,
   3050  1.10  christos 				  "Cannot insert breakpoint %d.\n"
   3051   1.3  christos 				  "%s\n",
   3052   1.3  christos 				  bl->owner->number, message.c_str ());
   3053   1.3  christos 		    }
   3054  1.10  christos 		  else
   3055  1.10  christos 		    {
   3056  1.10  christos 		      gdb_printf (tmp_error_stream,
   3057  1.10  christos 				  "Cannot insert breakpoint %d: %s\n",
   3058   1.3  christos 				  bl->owner->number,
   3059   1.1  christos 				  bp_excpt.what ());
   3060   1.3  christos 		    }
   3061   1.1  christos 		}
   3062   1.1  christos 	      return 1;
   3063   1.1  christos 
   3064   1.1  christos 	    }
   3065   1.1  christos 	}
   3066   1.1  christos       else
   3067   1.3  christos 	bl->inserted = 1;
   3068   1.1  christos 
   3069   1.1  christos       return 0;
   3070   1.1  christos     }
   3071   1.1  christos 
   3072   1.1  christos   else if (bl->loc_type == bp_loc_hardware_watchpoint
   3073   1.3  christos 	   && bl->owner->disposition != disp_del_at_next_stop)
   3074   1.3  christos     {
   3075  1.10  christos       int val;
   3076   1.1  christos 
   3077   1.1  christos       val = bl->owner->insert_location (bl);
   3078   1.1  christos 
   3079   1.1  christos       /* If trying to set a read-watchpoint, and it turns out it's not
   3080   1.1  christos 	 supported, try emulating one with an access watchpoint.  */
   3081   1.1  christos       if (val == 1 && bl->watchpoint_type == hw_read)
   3082   1.1  christos 	{
   3083   1.1  christos 	  /* But don't try to insert it, if there's already another
   3084  1.10  christos 	     hw_access location that would be considered a duplicate
   3085   1.1  christos 	     of this one.  */
   3086   1.1  christos 	  for (bp_location *loc : all_bp_locations ())
   3087   1.1  christos 	    if (loc != bl
   3088   1.1  christos 		&& loc->watchpoint_type == hw_access
   3089   1.1  christos 		&& watchpoint_locations_match (bl, loc))
   3090   1.1  christos 	      {
   3091   1.1  christos 		bl->duplicate = 1;
   3092   1.1  christos 		bl->inserted = 1;
   3093   1.1  christos 		bl->target_info = loc->target_info;
   3094   1.1  christos 		bl->watchpoint_type = hw_access;
   3095   1.1  christos 		val = 0;
   3096   1.1  christos 		break;
   3097   1.1  christos 	      }
   3098   1.1  christos 
   3099   1.1  christos 	  if (val == 1)
   3100  1.10  christos 	    {
   3101   1.1  christos 	      bl->watchpoint_type = hw_access;
   3102   1.1  christos 	      val = bl->owner->insert_location (bl);
   3103   1.1  christos 
   3104   1.1  christos 	      if (val)
   3105   1.1  christos 		/* Back to the original value.  */
   3106   1.1  christos 		bl->watchpoint_type = hw_read;
   3107   1.1  christos 	    }
   3108   1.1  christos 	}
   3109   1.1  christos 
   3110   1.1  christos       bl->inserted = (val == 0);
   3111   1.1  christos     }
   3112   1.1  christos 
   3113   1.3  christos   else if (bl->owner->type == bp_catchpoint)
   3114   1.3  christos     {
   3115  1.10  christos       int val;
   3116   1.1  christos 
   3117   1.1  christos       val = bl->owner->insert_location (bl);
   3118   1.1  christos       if (val)
   3119   1.1  christos 	{
   3120   1.1  christos 	  bl->owner->enable_state = bp_disabled;
   3121   1.1  christos 
   3122   1.1  christos 	  if (val == 1)
   3123   1.1  christos 	    warning (_("\
   3124   1.1  christos Error inserting catchpoint %d: Your system does not support this type\n\
   3125   1.1  christos of catchpoint."), bl->owner->number);
   3126   1.1  christos 	  else
   3127   1.1  christos 	    warning (_("Error inserting catchpoint %d."), bl->owner->number);
   3128   1.1  christos 	}
   3129   1.1  christos 
   3130   1.1  christos       bl->inserted = (val == 0);
   3131   1.1  christos 
   3132   1.1  christos       /* We've already printed an error message if there was a problem
   3133   1.1  christos 	 inserting this catchpoint, and we've disabled the catchpoint,
   3134   1.1  christos 	 so just return success.  */
   3135   1.1  christos       return 0;
   3136   1.1  christos     }
   3137   1.1  christos 
   3138   1.1  christos   return 0;
   3139   1.1  christos }
   3140   1.1  christos 
   3141   1.1  christos /* This function is called when program space PSPACE is about to be
   3142   1.1  christos    deleted.  It takes care of updating breakpoints to not reference
   3143   1.1  christos    PSPACE anymore.  */
   3144   1.1  christos 
   3145   1.1  christos void
   3146   1.1  christos breakpoint_program_space_exit (struct program_space *pspace)
   3147  1.11  christos {
   3148  1.11  christos   /* Remove any breakpoint that was set through this program space.  */
   3149  1.11  christos   for (breakpoint &b : all_breakpoints_safe ())
   3150   1.1  christos     if (b.pspace == pspace)
   3151   1.1  christos       delete_breakpoint (&b);
   3152   1.1  christos 
   3153  1.10  christos   /* Breakpoints set through other program spaces could have locations
   3154  1.11  christos      bound to PSPACE as well.  Remove those.  */
   3155  1.11  christos   for (bp_location *loc : all_bp_locations ())
   3156  1.11  christos     if (loc->pspace == pspace)
   3157  1.11  christos       {
   3158  1.11  christos 	/* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
   3159   1.1  christos 	loc->owner->unadd_location (*loc);
   3160   1.1  christos       }
   3161   1.1  christos 
   3162   1.3  christos   /* Now update the global location list to permanently delete the
   3163   1.1  christos      removed locations above.  */
   3164   1.1  christos   update_global_location_list (UGLL_DONT_INSERT);
   3165   1.1  christos }
   3166   1.1  christos 
   3167   1.1  christos /* Make sure all breakpoints are inserted in inferior.
   3168   1.1  christos    Throws exception on any error.
   3169   1.1  christos    A breakpoint that is already inserted won't be inserted
   3170   1.1  christos    again, so calling this function twice is safe.  */
   3171   1.1  christos void
   3172  1.11  christos insert_breakpoints (void)
   3173  1.11  christos {
   3174   1.1  christos   for (breakpoint &bpt : all_breakpoints ())
   3175  1.11  christos     if (is_hardware_watchpoint (&bpt))
   3176   1.1  christos       {
   3177  1.11  christos 	watchpoint &w = gdb::checked_static_cast<watchpoint &> (bpt);
   3178   1.1  christos 
   3179   1.1  christos 	update_watchpoint (&w, false /* don't reparse.  */);
   3180   1.3  christos       }
   3181   1.3  christos 
   3182   1.9  christos   /* Updating watchpoints creates new locations, so update the global
   3183   1.9  christos      location list.  Explicitly tell ugll to insert locations and
   3184   1.9  christos      ignore breakpoints_always_inserted_mode.  Also,
   3185   1.9  christos      update_global_location_list tries to "upgrade" software
   3186   1.9  christos      breakpoints to hardware breakpoints to handle "set breakpoint
   3187   1.3  christos      auto-hw", so we need to call it even if we don't have new
   3188   1.1  christos      locations.  */
   3189   1.1  christos   update_global_location_list (UGLL_INSERT);
   3190   1.1  christos }
   3191   1.1  christos 
   3192   1.1  christos /* This is used when we need to synch breakpoint conditions between GDB and the
   3193   1.1  christos    target.  It is the case with deleting and disabling of breakpoints when using
   3194   1.1  christos    always-inserted mode.  */
   3195   1.1  christos 
   3196   1.1  christos static void
   3197   1.1  christos update_inserted_breakpoint_locations (void)
   3198   1.1  christos {
   3199   1.1  christos   int error_flag = 0;
   3200   1.1  christos   int val = 0;
   3201   1.1  christos   int disabled_breaks = 0;
   3202   1.1  christos   int hw_breakpoint_error = 0;
   3203   1.7  christos   int hw_bp_details_reported = 0;
   3204   1.1  christos 
   3205   1.1  christos   string_file tmp_error_stream;
   3206   1.1  christos 
   3207   1.7  christos   /* Explicitly mark the warning -- this will only be printed if
   3208   1.1  christos      there was an error.  */
   3209   1.8  christos   tmp_error_stream.puts ("Warning:\n");
   3210   1.1  christos 
   3211  1.10  christos   scoped_restore_current_pspace_and_thread restore_pspace_thread;
   3212   1.1  christos 
   3213   1.1  christos   for (bp_location *bl : all_bp_locations ())
   3214   1.1  christos     {
   3215   1.1  christos       /* We only want to update software breakpoints and hardware
   3216   1.1  christos 	 breakpoints.  */
   3217   1.1  christos       if (!is_breakpoint (bl->owner))
   3218   1.1  christos 	continue;
   3219   1.1  christos 
   3220   1.1  christos       /* We only want to update locations that are already inserted
   3221   1.8  christos 	 and need updating.  This is to avoid unwanted insertion during
   3222   1.1  christos 	 deletion of breakpoints.  */
   3223   1.1  christos       if (!bl->inserted || !bl->needs_update)
   3224   1.1  christos 	continue;
   3225   1.1  christos 
   3226   1.1  christos       switch_to_program_space_and_thread (bl->pspace);
   3227   1.1  christos 
   3228   1.1  christos       /* For targets that support global breakpoints, there's no need
   3229   1.1  christos 	 to select an inferior to insert breakpoint to.  In fact, even
   3230  1.11  christos 	 if we aren't attached to any process yet, we should still
   3231  1.10  christos 	 insert breakpoints.  */
   3232   1.1  christos       if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
   3233   1.1  christos 	  && (inferior_ptid == null_ptid || !target_has_execution ()))
   3234   1.7  christos 	continue;
   3235   1.1  christos 
   3236   1.1  christos       val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
   3237   1.1  christos 				    &hw_breakpoint_error, &hw_bp_details_reported);
   3238   1.1  christos       if (val)
   3239   1.1  christos 	error_flag = val;
   3240   1.1  christos     }
   3241   1.1  christos 
   3242   1.8  christos   if (error_flag)
   3243  1.11  christos     {
   3244   1.1  christos       target_terminal::ours_for_output ();
   3245   1.1  christos       error (("%s"), tmp_error_stream.c_str ());
   3246   1.1  christos     }
   3247   1.1  christos }
   3248   1.1  christos 
   3249   1.1  christos /* Used when starting or continuing the program.  */
   3250   1.1  christos 
   3251   1.1  christos static void
   3252   1.1  christos insert_breakpoint_locations (void)
   3253   1.1  christos {
   3254   1.1  christos   int error_flag = 0;
   3255   1.1  christos   int val = 0;
   3256   1.1  christos   int disabled_breaks = 0;
   3257   1.1  christos   int hw_breakpoint_error = 0;
   3258   1.7  christos   int hw_bp_error_explained_already = 0;
   3259   1.7  christos 
   3260   1.1  christos   string_file tmp_error_stream;
   3261   1.1  christos 
   3262   1.7  christos   /* Explicitly mark the warning -- this will only be printed if
   3263   1.1  christos      there was an error.  */
   3264   1.8  christos   tmp_error_stream.puts ("Warning:\n");
   3265   1.1  christos 
   3266  1.10  christos   scoped_restore_current_pspace_and_thread restore_pspace_thread;
   3267   1.1  christos 
   3268   1.1  christos   for (bp_location *bl : all_bp_locations ())
   3269   1.1  christos     {
   3270   1.1  christos       if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
   3271   1.1  christos 	continue;
   3272   1.1  christos 
   3273   1.1  christos       /* There is no point inserting thread-specific breakpoints if
   3274   1.1  christos 	 the thread no longer exists.  ALL_BP_LOCATIONS bp_location
   3275   1.6  christos 	 has BL->OWNER always non-NULL.  */
   3276   1.1  christos       if (bl->owner->thread != -1
   3277   1.1  christos 	  && !valid_global_thread_id (bl->owner->thread))
   3278  1.11  christos 	continue;
   3279  1.11  christos 
   3280  1.11  christos       /* Or inferior specific breakpoints if the inferior no longer
   3281  1.11  christos 	 exists.  */
   3282  1.11  christos       if (bl->owner->inferior != -1
   3283  1.11  christos 	  && !valid_global_inferior_id (bl->owner->inferior))
   3284   1.1  christos 	continue;
   3285   1.1  christos 
   3286   1.1  christos       switch_to_program_space_and_thread (bl->pspace);
   3287   1.1  christos 
   3288   1.1  christos       /* For targets that support global breakpoints, there's no need
   3289   1.1  christos 	 to select an inferior to insert breakpoint to.  In fact, even
   3290  1.11  christos 	 if we aren't attached to any process yet, we should still
   3291  1.10  christos 	 insert breakpoints.  */
   3292   1.1  christos       if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
   3293   1.1  christos 	  && (inferior_ptid == null_ptid || !target_has_execution ()))
   3294   1.7  christos 	continue;
   3295   1.1  christos 
   3296   1.1  christos       val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
   3297   1.1  christos 				    &hw_breakpoint_error, &hw_bp_error_explained_already);
   3298   1.1  christos       if (val)
   3299   1.1  christos 	error_flag = val;
   3300   1.1  christos     }
   3301   1.1  christos 
   3302  1.11  christos   /* If we failed to insert all locations of a watchpoint, remove
   3303   1.1  christos      them, as half-inserted watchpoint is of limited use.  */
   3304  1.10  christos   for (breakpoint &bpt : all_breakpoints ())
   3305   1.1  christos     {
   3306  1.11  christos       bool some_failed = false;
   3307   1.1  christos 
   3308   1.1  christos       if (!is_hardware_watchpoint (&bpt))
   3309  1.11  christos 	continue;
   3310   1.1  christos 
   3311   1.1  christos       if (!breakpoint_enabled (&bpt))
   3312  1.11  christos 	continue;
   3313   1.1  christos 
   3314  1.11  christos       if (bpt.disposition == disp_del_at_next_stop)
   3315  1.11  christos 	continue;
   3316  1.11  christos 
   3317   1.1  christos       for (bp_location &loc : bpt.locations ())
   3318  1.10  christos 	if (!loc.inserted && should_be_inserted (&loc))
   3319   1.1  christos 	  {
   3320   1.1  christos 	    some_failed = true;
   3321  1.10  christos 	    break;
   3322   1.1  christos 	  }
   3323   1.1  christos 
   3324  1.11  christos       if (some_failed)
   3325  1.11  christos 	{
   3326  1.11  christos 	  for (bp_location &loc : bpt.locations ())
   3327   1.1  christos 	    if (loc.inserted)
   3328   1.1  christos 	      remove_breakpoint (&loc);
   3329   1.7  christos 
   3330   1.7  christos 	  hw_breakpoint_error = 1;
   3331  1.11  christos 	  tmp_error_stream.printf ("Could not insert "
   3332   1.1  christos 				   "hardware watchpoint %d.\n",
   3333   1.1  christos 				   bpt.number);
   3334   1.1  christos 	  error_flag = -1;
   3335   1.1  christos 	}
   3336   1.1  christos     }
   3337   1.1  christos 
   3338   1.1  christos   if (error_flag)
   3339  1.10  christos     {
   3340   1.1  christos       /* If a hardware breakpoint or watchpoint was inserted, add a
   3341   1.1  christos 	 message about possibly exhausted resources.  */
   3342   1.7  christos       if (hw_breakpoint_error && !hw_bp_error_explained_already)
   3343   1.1  christos 	{
   3344   1.1  christos 	  tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
   3345   1.8  christos You may have requested too many hardware breakpoints/watchpoints.\n");
   3346  1.11  christos 	}
   3347   1.1  christos       target_terminal::ours_for_output ();
   3348   1.1  christos       error (("%s"), tmp_error_stream.c_str ());
   3349   1.1  christos     }
   3350   1.1  christos }
   3351   1.1  christos 
   3352   1.1  christos /* Used when the program stops.
   3353   1.1  christos    Returns zero if successful, or non-zero if there was a problem
   3354   1.1  christos    removing a breakpoint location.  */
   3355   1.1  christos 
   3356   1.1  christos int
   3357   1.1  christos remove_breakpoints (void)
   3358   1.1  christos {
   3359  1.10  christos   int val = 0;
   3360   1.1  christos 
   3361   1.6  christos   for (bp_location *bl : all_bp_locations ())
   3362  1.10  christos     if (bl->inserted && !is_tracepoint (bl->owner))
   3363   1.1  christos       val |= remove_breakpoint (bl);
   3364   1.1  christos 
   3365   1.1  christos   return val;
   3366   1.1  christos }
   3367   1.1  christos 
   3368   1.1  christos /* When a thread exits, remove breakpoints that are related to
   3369   1.1  christos    that thread.  */
   3370  1.11  christos 
   3371  1.11  christos static void
   3372  1.11  christos remove_threaded_breakpoints (thread_info *tp,
   3373   1.1  christos 			     std::optional<ULONGEST> /* exit_code */,
   3374  1.11  christos 			     int /* silent */)
   3375   1.1  christos {
   3376  1.11  christos   for (breakpoint &b : all_breakpoints_safe ())
   3377   1.1  christos     {
   3378  1.10  christos       if (b.thread == tp->global_num && user_breakpoint_p (&b))
   3379   1.6  christos 	{
   3380  1.11  christos 	  gdb_printf (_("\
   3381  1.11  christos Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
   3382  1.11  christos 		      b.number, print_thread_id (tp));
   3383  1.11  christos 	  delete_breakpoint (&b);
   3384  1.11  christos        }
   3385  1.11  christos     }
   3386  1.11  christos }
   3387  1.11  christos 
   3388   1.1  christos /* Called when inferior INF has been removed from GDB.  Remove associated
   3389  1.11  christos    per-inferior breakpoints.  */
   3390  1.11  christos 
   3391  1.11  christos static void
   3392  1.11  christos remove_inferior_breakpoints (struct inferior *inf)
   3393  1.11  christos {
   3394  1.11  christos   for (breakpoint &b : all_breakpoints_safe ())
   3395  1.11  christos     {
   3396  1.11  christos       if (b.inferior == inf->num && user_breakpoint_p (&b))
   3397  1.11  christos 	{
   3398  1.11  christos 	  /* Tell the user the breakpoint has been deleted.  But only for
   3399  1.11  christos 	     breakpoints that would not normally have been deleted at the
   3400  1.11  christos 	     next stop anyway.  */
   3401  1.11  christos 	  if (b.disposition != disp_del
   3402  1.11  christos 	      && b.disposition != disp_del_at_next_stop)
   3403  1.11  christos 	    gdb_printf (_("\
   3404  1.11  christos Inferior-specific breakpoint %d deleted - inferior %d has been removed.\n"),
   3405   1.1  christos 			b.number, inf->num);
   3406   1.1  christos 	  delete_breakpoint (&b);
   3407   1.1  christos        }
   3408   1.1  christos     }
   3409   1.9  christos }
   3410   1.1  christos 
   3411   1.9  christos /* See breakpoint.h.  */
   3412   1.8  christos 
   3413   1.1  christos void
   3414   1.1  christos remove_breakpoints_inf (inferior *inf)
   3415   1.1  christos {
   3416  1.11  christos   int val;
   3417  1.11  christos 
   3418  1.10  christos   breakpoint_debug_printf ("inf->num = %d", inf->num);
   3419  1.10  christos 
   3420  1.10  christos   for (bp_location *bl : all_bp_locations ())
   3421  1.10  christos     {
   3422   1.1  christos       if (bl->pspace != inf->pspace)
   3423  1.10  christos 	continue;
   3424  1.10  christos 
   3425  1.10  christos       if (bl->inserted && !bl->target_info.persist)
   3426  1.10  christos 	{
   3427  1.10  christos 	  val = remove_breakpoint (bl);
   3428  1.10  christos 	  if (val != 0)
   3429  1.10  christos 	    return;
   3430   1.1  christos 	}
   3431   1.1  christos     }
   3432   1.1  christos }
   3433   1.1  christos 
   3434   1.1  christos static int internal_breakpoint_number = -1;
   3435   1.1  christos 
   3436   1.1  christos /* Set the breakpoint number of B, depending on the value of INTERNAL.
   3437   1.1  christos    If INTERNAL is non-zero, the breakpoint number will be populated
   3438   1.1  christos    from internal_breakpoint_number and that variable decremented.
   3439   1.1  christos    Otherwise the breakpoint number will be populated from
   3440   1.1  christos    breakpoint_count and that value incremented.  Internal breakpoints
   3441   1.1  christos    do not set the internal var bpnum.  */
   3442   1.1  christos static void
   3443   1.1  christos set_breakpoint_number (int internal, struct breakpoint *b)
   3444   1.1  christos {
   3445   1.1  christos   if (internal)
   3446   1.1  christos     b->number = internal_breakpoint_number--;
   3447   1.1  christos   else
   3448   1.1  christos     {
   3449   1.1  christos       set_breakpoint_count (breakpoint_count + 1);
   3450   1.1  christos       b->number = breakpoint_count;
   3451   1.1  christos     }
   3452  1.10  christos }
   3453  1.10  christos 
   3454   1.1  christos /* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH.  */
   3455   1.1  christos 
   3456  1.10  christos static struct breakpoint *
   3457   1.1  christos create_internal_breakpoint (struct gdbarch *gdbarch,
   3458  1.10  christos 			    CORE_ADDR address, enum bptype type)
   3459  1.10  christos {
   3460   1.1  christos   std::unique_ptr<internal_breakpoint> b
   3461   1.1  christos     (new internal_breakpoint (gdbarch, type, address));
   3462   1.1  christos 
   3463  1.10  christos   b->number = internal_breakpoint_number--;
   3464  1.10  christos 
   3465  1.10  christos   return add_to_breakpoint_chain (std::move (b));
   3466  1.10  christos }
   3467  1.10  christos 
   3468  1.10  christos /* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
   3469  1.10  christos    GDBARCH.  */
   3470  1.10  christos 
   3471  1.10  christos static struct breakpoint *
   3472  1.10  christos create_internal_breakpoint (struct gdbarch *gdbarch,
   3473  1.10  christos 			    struct bound_minimal_symbol &msym, enum bptype type)
   3474  1.10  christos {
   3475  1.10  christos   CORE_ADDR address;
   3476  1.10  christos 
   3477  1.10  christos   address = msym.value_address ();
   3478  1.10  christos 
   3479  1.10  christos   address = gdbarch_convert_from_func_ptr_addr
   3480  1.10  christos     (gdbarch, address, current_inferior ()->top_target ());
   3481  1.10  christos 
   3482  1.10  christos   /* Note that we're not using gdbarch_addr_bits_remove here, because that's
   3483  1.10  christos      related to addresses in $pc.  We're getting the address from the
   3484  1.10  christos      minimal symbol table.  */
   3485  1.10  christos 
   3486  1.10  christos   /* Is gdbarch_deprecated_function_start_offset needed here?  Or is that dealt
   3487  1.10  christos      with elsewhere?  Needs testing on vax.  */
   3488  1.10  christos 
   3489  1.10  christos   if (gdbarch_skip_entrypoint_p (gdbarch))
   3490  1.10  christos     address = gdbarch_skip_entrypoint (gdbarch, address);
   3491   1.1  christos 
   3492   1.1  christos   return create_internal_breakpoint (gdbarch, address, type);
   3493   1.1  christos }
   3494   1.1  christos 
   3495   1.1  christos static const char *const longjmp_names[] =
   3496   1.1  christos   {
   3497   1.1  christos     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
   3498   1.1  christos   };
   3499   1.1  christos #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
   3500   1.1  christos 
   3501   1.1  christos /* Per-objfile data private to breakpoint.c.  */
   3502   1.1  christos struct breakpoint_objfile_data
   3503  1.10  christos {
   3504   1.1  christos   /* Minimal symbol for "_ovly_debug_event" (if any).  */
   3505   1.1  christos   struct bound_minimal_symbol overlay_msym;
   3506  1.10  christos 
   3507   1.1  christos   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
   3508   1.1  christos   struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
   3509   1.8  christos 
   3510   1.1  christos   /* True if we have looked for longjmp probes.  */
   3511   1.8  christos   int longjmp_searched = 0;
   3512   1.8  christos 
   3513   1.8  christos   /* SystemTap probe points for longjmp (if any).  These are non-owning
   3514   1.1  christos      references.  */
   3515   1.1  christos   std::vector<probe *> longjmp_probes;
   3516  1.10  christos 
   3517   1.1  christos   /* Minimal symbol for "std::terminate()" (if any).  */
   3518   1.1  christos   struct bound_minimal_symbol terminate_msym;
   3519  1.10  christos 
   3520   1.1  christos   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
   3521   1.1  christos   struct bound_minimal_symbol exception_msym;
   3522   1.8  christos 
   3523   1.1  christos   /* True if we have looked for exception probes.  */
   3524   1.8  christos   int exception_searched = 0;
   3525   1.8  christos 
   3526   1.8  christos   /* SystemTap probe points for unwinding (if any).  These are non-owning
   3527   1.1  christos      references.  */
   3528   1.1  christos   std::vector<probe *> exception_probes;
   3529  1.10  christos };
   3530   1.9  christos 
   3531   1.1  christos static const registry<objfile>::key<breakpoint_objfile_data>
   3532   1.1  christos   breakpoint_objfile_key;
   3533   1.1  christos 
   3534   1.1  christos /* Minimal symbol not found sentinel.  */
   3535   1.1  christos static struct minimal_symbol msym_not_found;
   3536   1.1  christos 
   3537  1.10  christos /* Returns TRUE if MSYM point to the "not found" sentinel.  */
   3538   1.1  christos 
   3539   1.1  christos static bool
   3540   1.1  christos msym_not_found_p (const struct minimal_symbol *msym)
   3541   1.1  christos {
   3542   1.1  christos   return msym == &msym_not_found;
   3543   1.1  christos }
   3544   1.1  christos 
   3545   1.1  christos /* Return per-objfile data needed by breakpoint.c.
   3546   1.1  christos    Allocate the data if necessary.  */
   3547   1.1  christos 
   3548   1.1  christos static struct breakpoint_objfile_data *
   3549   1.1  christos get_breakpoint_objfile_data (struct objfile *objfile)
   3550   1.1  christos {
   3551   1.9  christos   struct breakpoint_objfile_data *bp_objfile_data;
   3552   1.1  christos 
   3553   1.9  christos   bp_objfile_data = breakpoint_objfile_key.get (objfile);
   3554   1.1  christos   if (bp_objfile_data == NULL)
   3555   1.1  christos     bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
   3556   1.1  christos   return bp_objfile_data;
   3557   1.1  christos }
   3558   1.1  christos 
   3559   1.1  christos static void
   3560   1.1  christos create_overlay_event_breakpoint (void)
   3561   1.1  christos {
   3562   1.8  christos   const char *const func_name = "_ovly_debug_event";
   3563   1.1  christos 
   3564   1.1  christos   for (objfile *objfile : current_program_space->objfiles ())
   3565   1.1  christos     {
   3566   1.1  christos       struct breakpoint *b;
   3567   1.1  christos       struct breakpoint_objfile_data *bp_objfile_data;
   3568   1.1  christos       CORE_ADDR addr;
   3569   1.1  christos 
   3570   1.3  christos       bp_objfile_data = get_breakpoint_objfile_data (objfile);
   3571   1.1  christos 
   3572   1.1  christos       if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
   3573   1.3  christos 	continue;
   3574   1.1  christos 
   3575   1.3  christos       if (bp_objfile_data->overlay_msym.minsym == NULL)
   3576   1.1  christos 	{
   3577   1.1  christos 	  struct bound_minimal_symbol m;
   3578   1.3  christos 
   3579   1.1  christos 	  m = lookup_minimal_symbol_text (func_name, objfile);
   3580   1.1  christos 	  if (m.minsym == NULL)
   3581   1.3  christos 	    {
   3582   1.1  christos 	      /* Avoid future lookups in this objfile.  */
   3583   1.1  christos 	      bp_objfile_data->overlay_msym.minsym = &msym_not_found;
   3584   1.1  christos 	      continue;
   3585   1.1  christos 	    }
   3586   1.1  christos 	  bp_objfile_data->overlay_msym = m;
   3587  1.10  christos 	}
   3588   1.9  christos 
   3589  1.10  christos       addr = bp_objfile_data->overlay_msym.value_address ();
   3590  1.10  christos       b = create_internal_breakpoint (objfile->arch (), addr,
   3591   1.1  christos 				      bp_overlay_event);
   3592   1.1  christos       b->locspec = new_explicit_location_spec_function (func_name);
   3593  1.10  christos 
   3594  1.10  christos       if (overlay_debugging == ovly_auto)
   3595  1.10  christos 	{
   3596  1.10  christos 	  b->enable_state = bp_enabled;
   3597   1.1  christos 	  overlay_events_enabled = 1;
   3598   1.1  christos 	}
   3599  1.10  christos       else
   3600  1.10  christos        {
   3601   1.1  christos 	 b->enable_state = bp_disabled;
   3602   1.1  christos 	 overlay_events_enabled = 0;
   3603   1.1  christos        }
   3604   1.1  christos     }
   3605  1.10  christos }
   3606  1.10  christos 
   3607  1.10  christos /* Install a master longjmp breakpoint for OBJFILE using a probe.  Return
   3608  1.10  christos    true if a breakpoint was installed.  */
   3609  1.10  christos 
   3610   1.1  christos static bool
   3611  1.10  christos create_longjmp_master_breakpoint_probe (objfile *objfile)
   3612  1.10  christos {
   3613  1.10  christos   struct gdbarch *gdbarch = objfile->arch ();
   3614   1.1  christos   struct breakpoint_objfile_data *bp_objfile_data
   3615  1.10  christos     = get_breakpoint_objfile_data (objfile);
   3616   1.9  christos 
   3617  1.10  christos   if (!bp_objfile_data->longjmp_searched)
   3618  1.10  christos     {
   3619   1.1  christos       std::vector<probe *> ret
   3620  1.10  christos 	= find_probes_in_objfile (objfile, "libc", "longjmp");
   3621   1.9  christos 
   3622  1.10  christos       if (!ret.empty ())
   3623  1.10  christos 	{
   3624  1.10  christos 	  /* We are only interested in checking one element.  */
   3625  1.10  christos 	  probe *p = ret[0];
   3626  1.10  christos 
   3627  1.10  christos 	  if (!p->can_evaluate_arguments ())
   3628  1.10  christos 	    {
   3629  1.10  christos 	      /* We cannot use the probe interface here,
   3630  1.10  christos 		 because it does not know how to evaluate
   3631  1.10  christos 		 arguments.  */
   3632  1.10  christos 	      ret.clear ();
   3633  1.10  christos 	    }
   3634  1.10  christos 	}
   3635  1.10  christos       bp_objfile_data->longjmp_probes = ret;
   3636  1.10  christos       bp_objfile_data->longjmp_searched = 1;
   3637  1.10  christos     }
   3638  1.10  christos 
   3639  1.10  christos   if (bp_objfile_data->longjmp_probes.empty ())
   3640  1.10  christos     return false;
   3641  1.10  christos 
   3642  1.10  christos   for (probe *p : bp_objfile_data->longjmp_probes)
   3643  1.10  christos     {
   3644  1.10  christos       struct breakpoint *b;
   3645  1.10  christos 
   3646  1.10  christos       b = create_internal_breakpoint (gdbarch,
   3647  1.10  christos 				      p->get_relocated_address (objfile),
   3648  1.10  christos 				      bp_longjmp_master);
   3649  1.10  christos       b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
   3650   1.1  christos       b->enable_state = bp_disabled;
   3651  1.10  christos     }
   3652  1.10  christos 
   3653   1.1  christos   return true;
   3654  1.10  christos }
   3655  1.10  christos 
   3656   1.1  christos /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
   3657  1.10  christos    Return true if at least one breakpoint was installed.  */
   3658  1.10  christos 
   3659  1.10  christos static bool
   3660  1.10  christos create_longjmp_master_breakpoint_names (objfile *objfile)
   3661  1.10  christos {
   3662  1.10  christos   struct gdbarch *gdbarch = objfile->arch ();
   3663   1.1  christos   if (!gdbarch_get_longjmp_target_p (gdbarch))
   3664  1.10  christos     return false;
   3665  1.10  christos 
   3666  1.10  christos   struct breakpoint_objfile_data *bp_objfile_data
   3667   1.1  christos     = get_breakpoint_objfile_data (objfile);
   3668  1.10  christos   unsigned int installed_bp = 0;
   3669  1.10  christos 
   3670  1.10  christos   for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
   3671  1.10  christos     {
   3672  1.10  christos       struct breakpoint *b;
   3673   1.1  christos       const char *func_name;
   3674  1.10  christos       CORE_ADDR addr;
   3675  1.10  christos 
   3676   1.1  christos       if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
   3677  1.10  christos 	continue;
   3678  1.10  christos 
   3679  1.10  christos       func_name = longjmp_names[i];
   3680  1.10  christos       if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
   3681   1.1  christos 	{
   3682  1.10  christos 	  struct bound_minimal_symbol m;
   3683  1.10  christos 
   3684  1.10  christos 	  m = lookup_minimal_symbol_text (func_name, objfile);
   3685  1.10  christos 	  if (m.minsym == NULL)
   3686  1.10  christos 	    {
   3687   1.9  christos 	      /* Prevent future lookups in this objfile.  */
   3688   1.9  christos 	      bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
   3689  1.10  christos 	      continue;
   3690  1.10  christos 	    }
   3691   1.1  christos 	  bp_objfile_data->longjmp_msym[i] = m;
   3692  1.10  christos 	}
   3693  1.10  christos 
   3694  1.10  christos       addr = bp_objfile_data->longjmp_msym[i].value_address ();
   3695  1.10  christos       b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
   3696  1.10  christos       b->locspec = new_explicit_location_spec_function (func_name);
   3697  1.10  christos       b->enable_state = bp_disabled;
   3698  1.10  christos       installed_bp++;
   3699  1.10  christos     }
   3700  1.10  christos 
   3701  1.10  christos   return installed_bp > 0;
   3702  1.10  christos }
   3703   1.1  christos 
   3704  1.10  christos /* Create a master longjmp breakpoint.  */
   3705  1.10  christos 
   3706  1.10  christos static void
   3707  1.10  christos create_longjmp_master_breakpoint (void)
   3708   1.1  christos {
   3709  1.10  christos   scoped_restore_current_program_space restore_pspace;
   3710  1.10  christos 
   3711  1.10  christos   for (struct program_space *pspace : program_spaces)
   3712   1.1  christos     {
   3713  1.10  christos       set_current_program_space (pspace);
   3714  1.10  christos 
   3715  1.10  christos       for (objfile *obj : current_program_space->objfiles ())
   3716  1.10  christos 	{
   3717  1.10  christos 	  /* Skip separate debug object, it's handled in the loop below.  */
   3718   1.1  christos 	  if (obj->separate_debug_objfile_backlink != nullptr)
   3719  1.10  christos 	    continue;
   3720  1.10  christos 
   3721  1.10  christos 	  /* Try a probe kind breakpoint on main objfile.  */
   3722   1.1  christos 	  if (create_longjmp_master_breakpoint_probe (obj))
   3723  1.10  christos 	    continue;
   3724  1.10  christos 
   3725  1.10  christos 	  /* Try longjmp_names kind breakpoints on main and separate_debug
   3726  1.10  christos 	     objfiles.  */
   3727  1.10  christos 	  for (objfile *debug_objfile : obj->separate_debug_objfiles ())
   3728   1.9  christos 	    if (create_longjmp_master_breakpoint_names (debug_objfile))
   3729   1.9  christos 	      break;
   3730   1.1  christos 	}
   3731   1.1  christos     }
   3732   1.1  christos }
   3733   1.1  christos 
   3734   1.1  christos /* Create a master std::terminate breakpoint.  */
   3735   1.1  christos static void
   3736   1.1  christos create_std_terminate_master_breakpoint (void)
   3737   1.1  christos {
   3738   1.8  christos   const char *const func_name = "std::terminate()";
   3739  1.11  christos 
   3740  1.11  christos   scoped_restore_current_program_space restore_pspace;
   3741   1.1  christos   scoped_restore_current_language save_language;
   3742   1.9  christos   set_language (language_cplus);
   3743   1.9  christos 
   3744   1.9  christos   for (struct program_space *pspace : program_spaces)
   3745   1.1  christos     {
   3746   1.9  christos       set_current_program_space (pspace);
   3747   1.9  christos 
   3748   1.9  christos       for (objfile *objfile : current_program_space->objfiles ())
   3749   1.9  christos 	{
   3750   1.1  christos 	  struct breakpoint *b;
   3751   1.9  christos 	  struct breakpoint_objfile_data *bp_objfile_data;
   3752   1.1  christos 
   3753   1.9  christos 	  bp_objfile_data = get_breakpoint_objfile_data (objfile);
   3754   1.9  christos 
   3755   1.1  christos 	  if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
   3756   1.9  christos 	    continue;
   3757   1.9  christos 
   3758   1.9  christos 	  if (bp_objfile_data->terminate_msym.minsym == NULL)
   3759   1.1  christos 	    {
   3760   1.9  christos 	      struct bound_minimal_symbol m;
   3761  1.10  christos 
   3762  1.10  christos 	      m = lookup_minimal_symbol (func_name, NULL, objfile);
   3763   1.9  christos 	      if (m.minsym == NULL || (m.minsym->type () != mst_text
   3764   1.9  christos 				       && m.minsym->type () != mst_file_text))
   3765   1.9  christos 		{
   3766   1.9  christos 		  /* Prevent future lookups in this objfile.  */
   3767   1.9  christos 		  bp_objfile_data->terminate_msym.minsym = &msym_not_found;
   3768   1.9  christos 		  continue;
   3769   1.9  christos 		}
   3770   1.1  christos 	      bp_objfile_data->terminate_msym = m;
   3771  1.10  christos 	    }
   3772  1.10  christos 
   3773  1.10  christos 	  b = create_internal_breakpoint (objfile->arch (),
   3774  1.10  christos 					  bp_objfile_data->terminate_msym,
   3775   1.9  christos 					  bp_std_terminate_master);
   3776   1.9  christos 	  b->locspec = new_explicit_location_spec_function (func_name);
   3777   1.9  christos 	  b->enable_state = bp_disabled;
   3778   1.1  christos 	}
   3779   1.1  christos     }
   3780  1.10  christos }
   3781  1.10  christos 
   3782   1.1  christos /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
   3783  1.10  christos    probe.  Return true if a breakpoint was installed.  */
   3784  1.10  christos 
   3785   1.1  christos static bool
   3786  1.10  christos create_exception_master_breakpoint_probe (objfile *objfile)
   3787  1.10  christos {
   3788  1.10  christos   struct breakpoint *b;
   3789  1.10  christos   struct gdbarch *gdbarch;
   3790  1.10  christos   struct breakpoint_objfile_data *bp_objfile_data;
   3791   1.1  christos 
   3792  1.10  christos   bp_objfile_data = get_breakpoint_objfile_data (objfile);
   3793  1.10  christos 
   3794   1.1  christos   /* We prefer the SystemTap probe point if it exists.  */
   3795  1.10  christos   if (!bp_objfile_data->exception_searched)
   3796  1.10  christos     {
   3797   1.1  christos       std::vector<probe *> ret
   3798  1.10  christos 	= find_probes_in_objfile (objfile, "libgcc", "unwind");
   3799   1.1  christos 
   3800  1.10  christos       if (!ret.empty ())
   3801  1.10  christos 	{
   3802   1.1  christos 	  /* We are only interested in checking one element.  */
   3803  1.10  christos 	  probe *p = ret[0];
   3804   1.1  christos 
   3805  1.10  christos 	  if (!p->can_evaluate_arguments ())
   3806  1.10  christos 	    {
   3807  1.10  christos 	      /* We cannot use the probe interface here, because it does
   3808   1.1  christos 		 not know how to evaluate arguments.  */
   3809   1.1  christos 	      ret.clear ();
   3810  1.10  christos 	    }
   3811  1.10  christos 	}
   3812  1.10  christos       bp_objfile_data->exception_probes = ret;
   3813   1.1  christos       bp_objfile_data->exception_searched = 1;
   3814  1.10  christos     }
   3815  1.10  christos 
   3816   1.8  christos   if (bp_objfile_data->exception_probes.empty ())
   3817  1.10  christos     return false;
   3818   1.1  christos 
   3819  1.10  christos   gdbarch = objfile->arch ();
   3820  1.10  christos 
   3821  1.10  christos   for (probe *p : bp_objfile_data->exception_probes)
   3822  1.10  christos     {
   3823  1.10  christos       b = create_internal_breakpoint (gdbarch,
   3824  1.10  christos 				      p->get_relocated_address (objfile),
   3825  1.10  christos 				      bp_exception_master);
   3826  1.10  christos       b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
   3827   1.1  christos       b->enable_state = bp_disabled;
   3828  1.10  christos     }
   3829  1.10  christos 
   3830   1.1  christos   return true;
   3831  1.10  christos }
   3832  1.10  christos 
   3833   1.1  christos /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
   3834  1.10  christos    _Unwind_DebugHook.  Return true if a breakpoint was installed.  */
   3835  1.10  christos 
   3836  1.10  christos static bool
   3837  1.10  christos create_exception_master_breakpoint_hook (objfile *objfile)
   3838  1.10  christos {
   3839  1.10  christos   const char *const func_name = "_Unwind_DebugHook";
   3840  1.10  christos   struct breakpoint *b;
   3841   1.1  christos   struct gdbarch *gdbarch;
   3842  1.10  christos   struct breakpoint_objfile_data *bp_objfile_data;
   3843   1.1  christos 
   3844  1.10  christos   bp_objfile_data = get_breakpoint_objfile_data (objfile);
   3845  1.10  christos 
   3846  1.10  christos   if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
   3847  1.10  christos     return false;
   3848  1.10  christos 
   3849  1.10  christos   gdbarch = objfile->arch ();
   3850  1.10  christos 
   3851  1.10  christos   if (bp_objfile_data->exception_msym.minsym == NULL)
   3852   1.1  christos     {
   3853  1.11  christos       struct bound_minimal_symbol debug_hook;
   3854  1.10  christos 
   3855  1.10  christos       debug_hook = lookup_minimal_symbol_text (func_name, objfile);
   3856  1.10  christos       if (debug_hook.minsym == NULL)
   3857  1.10  christos 	{
   3858   1.1  christos 	  bp_objfile_data->exception_msym.minsym = &msym_not_found;
   3859   1.1  christos 	  return false;
   3860  1.10  christos 	}
   3861  1.10  christos 
   3862  1.10  christos       bp_objfile_data->exception_msym = debug_hook;
   3863  1.10  christos     }
   3864  1.10  christos 
   3865  1.10  christos   b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
   3866  1.10  christos 				  bp_exception_master);
   3867  1.10  christos   b->locspec = new_explicit_location_spec_function (func_name);
   3868  1.10  christos   b->enable_state = bp_disabled;
   3869  1.10  christos 
   3870  1.10  christos   return true;
   3871  1.10  christos }
   3872  1.10  christos 
   3873  1.10  christos /* Install a master breakpoint on the unwinder's debug hook.  */
   3874  1.10  christos 
   3875  1.10  christos static void
   3876  1.10  christos create_exception_master_breakpoint (void)
   3877  1.10  christos {
   3878  1.10  christos   for (objfile *obj : current_program_space->objfiles ())
   3879  1.10  christos     {
   3880  1.10  christos       /* Skip separate debug object.  */
   3881  1.10  christos       if (obj->separate_debug_objfile_backlink)
   3882  1.10  christos 	continue;
   3883  1.10  christos 
   3884  1.10  christos       /* Try a probe kind breakpoint.  */
   3885  1.10  christos       if (create_exception_master_breakpoint_probe (obj))
   3886  1.10  christos 	continue;
   3887  1.10  christos 
   3888  1.10  christos       /* Iterate over main and separate debug objects and try an
   3889  1.10  christos 	 _Unwind_DebugHook kind breakpoint.  */
   3890  1.10  christos       for (objfile *debug_objfile : obj->separate_debug_objfiles ())
   3891   1.1  christos 	if (create_exception_master_breakpoint_hook (debug_objfile))
   3892   1.6  christos 	  break;
   3893   1.6  christos     }
   3894   1.6  christos }
   3895   1.1  christos 
   3896  1.10  christos /* Does B have a location spec?  */
   3897  1.10  christos 
   3898   1.6  christos static bool
   3899  1.10  christos breakpoint_location_spec_empty_p (const struct breakpoint *b)
   3900   1.1  christos {
   3901   1.1  christos   return (b->locspec != nullptr && b->locspec->empty_p ());
   3902   1.1  christos }
   3903   1.1  christos 
   3904   1.1  christos void
   3905   1.1  christos update_breakpoints_after_exec (void)
   3906   1.1  christos {
   3907   1.1  christos   /* We're about to delete breakpoints from GDB's lists.  If the
   3908   1.1  christos      INSERTED flag is true, GDB will try to lift the breakpoints by
   3909   1.1  christos      writing the breakpoints' "shadow contents" back into memory.  The
   3910   1.1  christos      "shadow contents" are NOT valid after an exec, so GDB should not
   3911   1.1  christos      do that.  Instead, the target is responsible from marking
   3912   1.1  christos      breakpoints out as soon as it detects an exec.  We don't do that
   3913  1.10  christos      here instead, because there may be other attempts to delete
   3914   1.1  christos      breakpoints after detecting an exec and before reaching here.  */
   3915   1.1  christos   for (bp_location *bploc : all_bp_locations ())
   3916   1.1  christos     if (bploc->pspace == current_program_space)
   3917  1.11  christos       gdb_assert (!bploc->inserted);
   3918  1.10  christos 
   3919  1.11  christos   for (breakpoint &b : all_breakpoints_safe ())
   3920  1.10  christos     {
   3921   1.1  christos       if (b.pspace != current_program_space)
   3922  1.10  christos 	continue;
   3923  1.11  christos 
   3924  1.10  christos       /* Solib breakpoints must be explicitly reset after an exec().  */
   3925  1.11  christos       if (b.type == bp_shlib_event)
   3926  1.10  christos 	{
   3927  1.10  christos 	  delete_breakpoint (&b);
   3928   1.1  christos 	  continue;
   3929  1.10  christos 	}
   3930  1.11  christos 
   3931  1.10  christos       /* JIT breakpoints must be explicitly reset after an exec().  */
   3932  1.11  christos       if (b.type == bp_jit_event)
   3933  1.10  christos 	{
   3934  1.10  christos 	  delete_breakpoint (&b);
   3935   1.1  christos 	  continue;
   3936  1.10  christos 	}
   3937  1.10  christos 
   3938  1.11  christos       /* Thread event breakpoints must be set anew after an exec(),
   3939  1.11  christos 	 as must overlay event and longjmp master breakpoints.  */
   3940  1.11  christos       if (b.type == bp_thread_event || b.type == bp_overlay_event
   3941  1.10  christos 	  || b.type == bp_longjmp_master || b.type == bp_std_terminate_master
   3942  1.11  christos 	  || b.type == bp_exception_master)
   3943  1.10  christos 	{
   3944  1.10  christos 	  delete_breakpoint (&b);
   3945   1.1  christos 	  continue;
   3946  1.10  christos 	}
   3947  1.11  christos 
   3948  1.10  christos       /* Step-resume breakpoints are meaningless after an exec().  */
   3949  1.11  christos       if (b.type == bp_step_resume || b.type == bp_hp_step_resume)
   3950  1.10  christos 	{
   3951  1.10  christos 	  delete_breakpoint (&b);
   3952   1.1  christos 	  continue;
   3953  1.10  christos 	}
   3954  1.11  christos 
   3955  1.10  christos       /* Just like single-step breakpoints.  */
   3956  1.11  christos       if (b.type == bp_single_step)
   3957  1.10  christos 	{
   3958  1.10  christos 	  delete_breakpoint (&b);
   3959   1.3  christos 	  continue;
   3960  1.10  christos 	}
   3961  1.10  christos 
   3962  1.11  christos       /* Longjmp and longjmp-resume breakpoints are also meaningless
   3963  1.11  christos 	 after an exec.  */
   3964  1.11  christos       if (b.type == bp_longjmp || b.type == bp_longjmp_resume
   3965  1.10  christos 	  || b.type == bp_longjmp_call_dummy
   3966  1.11  christos 	  || b.type == bp_exception || b.type == bp_exception_resume)
   3967  1.10  christos 	{
   3968  1.10  christos 	  delete_breakpoint (&b);
   3969   1.1  christos 	  continue;
   3970  1.11  christos 	}
   3971  1.10  christos 
   3972  1.10  christos       if (b.type == bp_catchpoint)
   3973  1.10  christos 	{
   3974  1.10  christos 	  /* For now, none of the bp_catchpoint breakpoints need to
   3975  1.10  christos 	     do anything at this point.  In the future, if some of
   3976  1.10  christos 	     the catchpoints need to something, we will need to add
   3977  1.10  christos 	     a new method, and call this method from here.  */
   3978   1.1  christos 	  continue;
   3979  1.10  christos 	}
   3980  1.10  christos 
   3981  1.10  christos       /* bp_finish is a special case.  The only way we ought to be able
   3982  1.10  christos 	 to see one of these when an exec() has happened, is if the user
   3983  1.10  christos 	 caught a vfork, and then said "finish".  Ordinarily a finish just
   3984  1.10  christos 	 carries them to the call-site of the current callee, by setting
   3985  1.10  christos 	 a temporary bp there and resuming.  But in this case, the finish
   3986  1.10  christos 	 will carry them entirely through the vfork & exec.
   3987  1.10  christos 
   3988  1.10  christos 	 We don't want to allow a bp_finish to remain inserted now.  But
   3989  1.10  christos 	 we can't safely delete it, 'cause finish_command has a handle to
   3990  1.10  christos 	 the bp on a bpstat, and will later want to delete it.  There's a
   3991  1.10  christos 	 chance (and I've seen it happen) that if we delete the bp_finish
   3992  1.10  christos 	 here, that its storage will get reused by the time finish_command
   3993  1.10  christos 	 gets 'round to deleting the "use to be a bp_finish" breakpoint.
   3994  1.10  christos 	 We really must allow finish_command to delete a bp_finish.
   3995  1.10  christos 
   3996  1.10  christos 	 In the absence of a general solution for the "how do we know
   3997  1.10  christos 	 it's safe to delete something others may have handles to?"
   3998  1.10  christos 	 problem, what we'll do here is just uninsert the bp_finish, and
   3999  1.10  christos 	 let finish_command delete it.
   4000  1.10  christos 
   4001  1.10  christos 	 (We know the bp_finish is "doomed" in the sense that it's
   4002  1.10  christos 	 momentary, and will be deleted as soon as finish_command sees
   4003  1.10  christos 	 the inferior stopped.  So it doesn't matter that the bp's
   4004   1.1  christos 	 address is probably bogus in the new a.out, unlike e.g., the
   4005  1.11  christos 	 solib breakpoints.)  */
   4006  1.10  christos 
   4007  1.10  christos       if (b.type == bp_finish)
   4008  1.10  christos 	{
   4009   1.1  christos 	  continue;
   4010  1.10  christos 	}
   4011  1.10  christos 
   4012  1.10  christos       /* Without a symbolic address, we have little hope of the
   4013  1.11  christos 	 pre-exec() address meaning the same thing in the post-exec()
   4014  1.10  christos 	 a.out.  */
   4015  1.11  christos       if (breakpoint_location_spec_empty_p (&b))
   4016  1.10  christos 	{
   4017  1.10  christos 	  delete_breakpoint (&b);
   4018  1.10  christos 	  continue;
   4019   1.1  christos 	}
   4020   1.1  christos     }
   4021   1.1  christos }
   4022   1.1  christos 
   4023   1.1  christos int
   4024   1.1  christos detach_breakpoints (ptid_t ptid)
   4025   1.8  christos {
   4026   1.1  christos   int val = 0;
   4027   1.1  christos   scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
   4028   1.8  christos   struct inferior *inf = current_inferior ();
   4029   1.1  christos 
   4030   1.1  christos   if (ptid.pid () == inferior_ptid.pid ())
   4031   1.1  christos     error (_("Cannot detach breakpoints of inferior_ptid"));
   4032   1.1  christos 
   4033  1.10  christos   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
   4034  1.10  christos   inferior_ptid = ptid;
   4035  1.10  christos   for (bp_location *bl : all_bp_locations ())
   4036  1.10  christos     {
   4037   1.1  christos       if (bl->pspace != inf->pspace)
   4038  1.10  christos 	continue;
   4039  1.10  christos 
   4040  1.10  christos       /* This function must physically remove breakpoints locations
   4041  1.10  christos 	 from the specified ptid, without modifying the breakpoint
   4042  1.10  christos 	 package's state.  Locations of type bp_loc_other and
   4043  1.10  christos 	 bp_loc_software_watchpoint are only maintained at GDB side,
   4044  1.10  christos 	 so there is no need to remove them.  Moreover, removing these
   4045  1.10  christos 	 would modify the breakpoint package's state.  */
   4046  1.10  christos       if (bl->loc_type == bp_loc_other
   4047   1.1  christos 	  || bl->loc_type == bp_loc_software_watchpoint)
   4048  1.10  christos 	continue;
   4049  1.10  christos 
   4050  1.10  christos       if (bl->inserted)
   4051   1.1  christos 	val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
   4052   1.1  christos     }
   4053   1.1  christos 
   4054   1.1  christos   return val;
   4055   1.1  christos }
   4056   1.1  christos 
   4057   1.1  christos /* Remove the breakpoint location BL from the current address space.
   4058   1.1  christos    Note that this is used to detach breakpoints from a child fork.
   4059   1.1  christos    When we get here, the child isn't in the inferior list, and neither
   4060   1.1  christos    do we have objects to represent its address space --- we should
   4061   1.1  christos    *not* look at bl->pspace->aspace here.  */
   4062   1.6  christos 
   4063   1.1  christos static int
   4064  1.11  christos remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
   4065  1.11  christos {
   4066  1.11  christos   breakpoint_debug_printf ("%s due to %s",
   4067  1.11  christos 			   breakpoint_location_address_str (bl).c_str (),
   4068   1.1  christos 			   remove_bp_reason_str (reason));
   4069   1.1  christos 
   4070   1.1  christos   int val;
   4071   1.1  christos 
   4072   1.1  christos   /* BL is never in moribund_locations by our callers.  */
   4073   1.1  christos   gdb_assert (bl->owner != NULL);
   4074   1.1  christos 
   4075   1.1  christos   /* The type of none suggests that owner is actually deleted.
   4076   1.1  christos      This should not ever happen.  */
   4077   1.1  christos   gdb_assert (bl->owner->type != bp_none);
   4078   1.1  christos 
   4079   1.1  christos   if (bl->loc_type == bp_loc_software_breakpoint
   4080   1.1  christos       || bl->loc_type == bp_loc_hardware_breakpoint)
   4081   1.1  christos     {
   4082   1.1  christos       /* "Normal" instruction breakpoint: either the standard
   4083   1.1  christos 	 trap-instruction bp (bp_breakpoint), or a
   4084   1.1  christos 	 bp_hardware_breakpoint.  */
   4085   1.1  christos 
   4086   1.1  christos       /* First check to see if we have to handle an overlay.  */
   4087   1.1  christos       if (overlay_debugging == ovly_off
   4088   1.1  christos 	  || bl->section == NULL
   4089   1.1  christos 	  || !(section_is_overlay (bl->section)))
   4090   1.3  christos 	{
   4091   1.3  christos 	  /* No overlay handling: just remove the breakpoint.  */
   4092   1.3  christos 
   4093   1.3  christos 	  /* If we're trying to uninsert a memory breakpoint that we
   4094   1.3  christos 	     know is set in a dynamic object that is marked
   4095   1.3  christos 	     shlib_disabled, then either the dynamic object was
   4096   1.3  christos 	     removed with "remove-symbol-file" or with
   4097   1.3  christos 	     "nosharedlibrary".  In the former case, we don't know
   4098   1.3  christos 	     whether another dynamic object might have loaded over the
   4099   1.3  christos 	     breakpoint's address -- the user might well let us know
   4100   1.3  christos 	     about it next with add-symbol-file (the whole point of
   4101   1.3  christos 	     add-symbol-file is letting the user manually maintain a
   4102   1.3  christos 	     list of dynamically loaded objects).  If we have the
   4103   1.3  christos 	     breakpoint's shadow memory, that is, this is a software
   4104   1.3  christos 	     breakpoint managed by GDB, check whether the breakpoint
   4105   1.3  christos 	     is still inserted in memory, to avoid overwriting wrong
   4106   1.3  christos 	     code with stale saved shadow contents.  Note that HW
   4107   1.3  christos 	     breakpoints don't have shadow memory, as they're
   4108   1.3  christos 	     implemented using a mechanism that is not dependent on
   4109   1.3  christos 	     being able to modify the target's memory, and as such
   4110   1.3  christos 	     they should always be removed.  */
   4111   1.3  christos 	  if (bl->shlib_disabled
   4112   1.3  christos 	      && bl->target_info.shadow_len != 0
   4113   1.3  christos 	      && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
   4114  1.10  christos 	    val = 0;
   4115   1.1  christos 	  else
   4116   1.1  christos 	    val = bl->owner->remove_location (bl, reason);
   4117   1.1  christos 	}
   4118   1.1  christos       else
   4119   1.1  christos 	{
   4120   1.1  christos 	  /* This breakpoint is in an overlay section.
   4121   1.1  christos 	     Did we set a breakpoint at the LMA?  */
   4122   1.1  christos 	  if (!overlay_events_enabled)
   4123   1.1  christos 	      {
   4124   1.1  christos 		/* Yes -- overlay event support is not active, so we
   4125   1.1  christos 		   should have set a breakpoint at the LMA.  Remove it.
   4126   1.1  christos 		*/
   4127   1.1  christos 		/* Ignore any failures: if the LMA is in ROM, we will
   4128   1.1  christos 		   have already warned when we failed to insert it.  */
   4129   1.1  christos 		if (bl->loc_type == bp_loc_hardware_breakpoint)
   4130   1.1  christos 		  target_remove_hw_breakpoint (bl->gdbarch,
   4131   1.1  christos 					       &bl->overlay_target_info);
   4132   1.6  christos 		else
   4133   1.6  christos 		  target_remove_breakpoint (bl->gdbarch,
   4134   1.1  christos 					    &bl->overlay_target_info,
   4135   1.1  christos 					    reason);
   4136   1.1  christos 	      }
   4137   1.1  christos 	  /* Did we set a breakpoint at the VMA?
   4138   1.1  christos 	     If so, we will have marked the breakpoint 'inserted'.  */
   4139   1.1  christos 	  if (bl->inserted)
   4140   1.1  christos 	    {
   4141   1.1  christos 	      /* Yes -- remove it.  Previously we did not bother to
   4142   1.1  christos 		 remove the breakpoint if the section had been
   4143   1.1  christos 		 unmapped, but let's not rely on that being safe.  We
   4144   1.1  christos 		 don't know what the overlay manager might do.  */
   4145   1.1  christos 
   4146   1.1  christos 	      /* However, we should remove *software* breakpoints only
   4147   1.1  christos 		 if the section is still mapped, or else we overwrite
   4148   1.1  christos 		 wrong code with the saved shadow contents.  */
   4149  1.10  christos 	      if (bl->loc_type == bp_loc_hardware_breakpoint
   4150   1.1  christos 		  || section_is_mapped (bl->section))
   4151   1.1  christos 		val = bl->owner->remove_location (bl, reason);
   4152   1.1  christos 	      else
   4153   1.1  christos 		val = 0;
   4154   1.1  christos 	    }
   4155   1.1  christos 	  else
   4156   1.1  christos 	    {
   4157   1.1  christos 	      /* No -- not inserted, so no need to remove.  No error.  */
   4158   1.1  christos 	      val = 0;
   4159   1.1  christos 	    }
   4160   1.3  christos 	}
   4161   1.3  christos 
   4162   1.3  christos       /* In some cases, we might not be able to remove a breakpoint in
   4163   1.3  christos 	 a shared library that has already been removed, but we have
   4164   1.3  christos 	 not yet processed the shlib unload event.  Similarly for an
   4165   1.3  christos 	 unloaded add-symbol-file object - the user might not yet have
   4166   1.3  christos 	 had the chance to remove-symbol-file it.  shlib_disabled will
   4167   1.3  christos 	 be set if the library/object has already been removed, but
   4168   1.3  christos 	 the breakpoint hasn't been uninserted yet, e.g., after
   4169   1.3  christos 	 "nosharedlibrary" or "remove-symbol-file" with breakpoints
   4170   1.3  christos 	 always-inserted mode.  */
   4171   1.3  christos       if (val
   4172   1.3  christos 	  && (bl->loc_type == bp_loc_software_breakpoint
   4173   1.3  christos 	      && (bl->shlib_disabled
   4174   1.3  christos 		  || solib_name_from_address (bl->pspace, bl->address)
   4175   1.1  christos 		  || shared_objfile_contains_address_p (bl->pspace,
   4176   1.1  christos 							bl->address))))
   4177   1.1  christos 	val = 0;
   4178   1.1  christos 
   4179   1.6  christos       if (val)
   4180   1.1  christos 	return val;
   4181   1.1  christos       bl->inserted = (reason == DETACH_BREAKPOINT);
   4182   1.1  christos     }
   4183   1.6  christos   else if (bl->loc_type == bp_loc_hardware_watchpoint)
   4184  1.10  christos     {
   4185   1.1  christos       bl->inserted = (reason == DETACH_BREAKPOINT);
   4186   1.1  christos       bl->owner->remove_location (bl, reason);
   4187   1.6  christos 
   4188   1.1  christos       /* Failure to remove any of the hardware watchpoints comes here.  */
   4189   1.1  christos       if (reason == REMOVE_BREAKPOINT && bl->inserted)
   4190   1.1  christos 	warning (_("Could not remove hardware watchpoint %d."),
   4191   1.1  christos 		 bl->owner->number);
   4192  1.10  christos     }
   4193  1.10  christos   else if (bl->owner->type == bp_catchpoint
   4194   1.1  christos 	   && breakpoint_enabled (bl->owner)
   4195  1.10  christos 	   && !bl->duplicate)
   4196   1.1  christos     {
   4197   1.1  christos       val = bl->owner->remove_location (bl, reason);
   4198   1.1  christos       if (val)
   4199   1.6  christos 	return val;
   4200   1.1  christos 
   4201   1.1  christos       bl->inserted = (reason == DETACH_BREAKPOINT);
   4202   1.1  christos     }
   4203   1.1  christos 
   4204   1.1  christos   return 0;
   4205   1.1  christos }
   4206   1.6  christos 
   4207   1.1  christos static int
   4208   1.1  christos remove_breakpoint (struct bp_location *bl)
   4209   1.1  christos {
   4210   1.1  christos   /* BL is never in moribund_locations by our callers.  */
   4211   1.1  christos   gdb_assert (bl->owner != NULL);
   4212   1.1  christos 
   4213   1.1  christos   /* The type of none suggests that owner is actually deleted.
   4214   1.1  christos      This should not ever happen.  */
   4215   1.8  christos   gdb_assert (bl->owner->type != bp_none);
   4216   1.1  christos 
   4217   1.1  christos   scoped_restore_current_pspace_and_thread restore_pspace_thread;
   4218   1.1  christos 
   4219   1.8  christos   switch_to_program_space_and_thread (bl->pspace);
   4220   1.1  christos 
   4221   1.1  christos   return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
   4222  1.11  christos }
   4223   1.1  christos 
   4224   1.1  christos /* See breakpoint.h.  */
   4225  1.11  christos 
   4226   1.1  christos void
   4227  1.10  christos mark_breakpoints_out (program_space *pspace)
   4228  1.11  christos {
   4229   1.1  christos   for (bp_location *bl : all_bp_locations ())
   4230   1.1  christos     if (bl->pspace == pspace)
   4231   1.1  christos       bl->inserted = 0;
   4232  1.11  christos }
   4233   1.1  christos 
   4234   1.1  christos /* See breakpoint.h.  */
   4235  1.11  christos 
   4236   1.1  christos void
   4237   1.1  christos breakpoint_init_inferior (inferior *inf, inf_context context)
   4238   1.1  christos {
   4239  1.11  christos   /* If breakpoint locations are shared across processes, then there's
   4240   1.1  christos      nothing to do.  */
   4241   1.1  christos   if (gdbarch_has_global_breakpoints (inf->arch ()))
   4242  1.11  christos     return;
   4243   1.1  christos 
   4244  1.11  christos   mark_breakpoints_out (inf->pspace);
   4245  1.10  christos 
   4246  1.11  christos   for (breakpoint &b : all_breakpoints_safe ())
   4247  1.10  christos     {
   4248   1.1  christos       if (b.has_locations () && b.first_loc ().pspace != inf->pspace)
   4249  1.11  christos 	continue;
   4250  1.10  christos 
   4251  1.10  christos       switch (b.type)
   4252  1.10  christos 	{
   4253   1.1  christos 	case bp_call_dummy:
   4254  1.10  christos 	case bp_longjmp_call_dummy:
   4255  1.10  christos 
   4256  1.10  christos 	  /* If the call dummy breakpoint is at the entry point it will
   4257   1.1  christos 	     cause problems when the inferior is rerun, so we better get
   4258  1.10  christos 	     rid of it.  */
   4259   1.1  christos 
   4260  1.10  christos 	case bp_watchpoint_scope:
   4261   1.1  christos 
   4262  1.10  christos 	  /* Also get rid of scope breakpoints.  */
   4263   1.1  christos 
   4264  1.10  christos 	case bp_shlib_event:
   4265  1.10  christos 
   4266  1.10  christos 	  /* Also remove solib event breakpoints.  Their addresses may
   4267  1.10  christos 	     have changed since the last time we ran the program.
   4268  1.10  christos 	     Actually we may now be debugging against different target;
   4269  1.10  christos 	     and so the solib backend that installed this breakpoint may
   4270  1.10  christos 	     not be used in by the target.  E.g.,
   4271  1.10  christos 
   4272  1.10  christos 	     (gdb) file prog-linux
   4273  1.10  christos 	     (gdb) run               # native linux target
   4274  1.10  christos 	     ...
   4275  1.10  christos 	     (gdb) kill
   4276  1.10  christos 	     (gdb) file prog-win.exe
   4277   1.1  christos 	     (gdb) tar rem :9999     # remote Windows gdbserver.
   4278  1.10  christos 	  */
   4279   1.1  christos 
   4280  1.10  christos 	case bp_step_resume:
   4281   1.1  christos 
   4282  1.10  christos 	  /* Also remove step-resume breakpoints.  */
   4283   1.1  christos 
   4284  1.10  christos 	case bp_single_step:
   4285   1.3  christos 
   4286  1.11  christos 	  /* Also remove single-step breakpoints.  */
   4287  1.10  christos 
   4288   1.3  christos 	  delete_breakpoint (&b);
   4289  1.10  christos 	  break;
   4290  1.10  christos 
   4291  1.10  christos 	case bp_watchpoint:
   4292  1.10  christos 	case bp_hardware_watchpoint:
   4293  1.10  christos 	case bp_read_watchpoint:
   4294  1.11  christos 	case bp_access_watchpoint:
   4295   1.1  christos 	  {
   4296  1.10  christos 	    watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
   4297  1.11  christos 
   4298  1.11  christos 	    /* Likewise for watchpoints on local expressions.  */
   4299  1.10  christos 	    if (w.exp_valid_block != NULL)
   4300  1.10  christos 	      delete_breakpoint (&b);
   4301  1.10  christos 	    else
   4302  1.10  christos 	      {
   4303  1.10  christos 		/* Get rid of existing locations, which are no longer
   4304  1.10  christos 		   valid.  New ones will be created in
   4305  1.10  christos 		   update_watchpoint, when the inferior is restarted.
   4306  1.11  christos 		   The next update_global_location_list call will
   4307   1.1  christos 		   garbage collect them.  */
   4308  1.10  christos 		b.clear_locations ();
   4309  1.10  christos 
   4310  1.10  christos 		if (context == inf_starting)
   4311  1.10  christos 		  {
   4312  1.11  christos 		    /* Reset val field to force reread of starting value in
   4313  1.11  christos 		       insert_breakpoints.  */
   4314  1.10  christos 		    w.val.reset (nullptr);
   4315  1.10  christos 		    w.val_valid = false;
   4316  1.10  christos 		  }
   4317  1.10  christos 	      }
   4318  1.10  christos 	  }
   4319  1.10  christos 	  break;
   4320   1.1  christos 	default:
   4321  1.10  christos 	  break;
   4322   1.1  christos 	}
   4323   1.1  christos     }
   4324   1.8  christos 
   4325   1.1  christos   /* Get rid of the moribund locations.  */
   4326   1.8  christos   for (bp_location *bl : moribund_locations)
   4327   1.1  christos     decref_bp_location (&bl);
   4328   1.1  christos   moribund_locations.clear ();
   4329   1.1  christos }
   4330   1.1  christos 
   4331   1.1  christos /* These functions concern about actual breakpoints inserted in the
   4332   1.1  christos    target --- to e.g. check if we need to do decr_pc adjustment or if
   4333   1.1  christos    we need to hop over the bkpt --- so we check for address space
   4334   1.1  christos    match, not program space.  */
   4335   1.1  christos 
   4336   1.1  christos /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
   4337   1.1  christos    exists at PC.  It returns ordinary_breakpoint_here if it's an
   4338   1.1  christos    ordinary breakpoint, or permanent_breakpoint_here if it's a
   4339   1.1  christos    permanent breakpoint.
   4340   1.1  christos    - When continuing from a location with an ordinary breakpoint, we
   4341   1.1  christos      actually single step once before calling insert_breakpoints.
   4342   1.1  christos    - When continuing from a location with a permanent breakpoint, we
   4343   1.1  christos      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
   4344   1.1  christos      the target, to advance the PC past the breakpoint.  */
   4345   1.8  christos 
   4346   1.1  christos enum breakpoint_here
   4347  1.10  christos breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
   4348   1.1  christos {
   4349  1.10  christos   bool any_breakpoint_here = false;
   4350   1.1  christos 
   4351   1.1  christos   for (bp_location *bl : all_bp_locations ())
   4352   1.1  christos     {
   4353   1.1  christos       if (bl->loc_type != bp_loc_software_breakpoint
   4354   1.1  christos 	  && bl->loc_type != bp_loc_hardware_breakpoint)
   4355   1.1  christos 	continue;
   4356   1.1  christos 
   4357   1.3  christos       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
   4358   1.1  christos       if ((breakpoint_enabled (bl->owner)
   4359   1.1  christos 	   || bl->permanent)
   4360   1.1  christos 	  && breakpoint_location_address_match (bl, aspace, pc))
   4361   1.1  christos 	{
   4362   1.1  christos 	  if (overlay_debugging
   4363   1.1  christos 	      && section_is_overlay (bl->section)
   4364   1.3  christos 	      && !section_is_mapped (bl->section))
   4365   1.1  christos 	    continue;		/* unmapped overlay -- can't be a match */
   4366   1.1  christos 	  else if (bl->permanent)
   4367  1.10  christos 	    return permanent_breakpoint_here;
   4368   1.1  christos 	  else
   4369   1.1  christos 	    any_breakpoint_here = true;
   4370   1.1  christos 	}
   4371   1.6  christos     }
   4372   1.6  christos 
   4373   1.6  christos   return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
   4374   1.6  christos }
   4375   1.6  christos 
   4376   1.6  christos /* See breakpoint.h.  */
   4377   1.8  christos 
   4378   1.6  christos int
   4379   1.6  christos breakpoint_in_range_p (const address_space *aspace,
   4380  1.10  christos 		       CORE_ADDR addr, ULONGEST len)
   4381   1.6  christos {
   4382   1.6  christos   for (bp_location *bl : all_bp_locations ())
   4383   1.6  christos     {
   4384   1.6  christos       if (bl->loc_type != bp_loc_software_breakpoint
   4385   1.6  christos 	  && bl->loc_type != bp_loc_hardware_breakpoint)
   4386   1.6  christos 	continue;
   4387   1.6  christos 
   4388   1.6  christos       if ((breakpoint_enabled (bl->owner)
   4389   1.6  christos 	   || bl->permanent)
   4390   1.6  christos 	  && breakpoint_location_address_range_overlap (bl, aspace,
   4391   1.6  christos 							addr, len))
   4392   1.6  christos 	{
   4393   1.6  christos 	  if (overlay_debugging
   4394   1.6  christos 	      && section_is_overlay (bl->section)
   4395   1.6  christos 	      && !section_is_mapped (bl->section))
   4396   1.6  christos 	    {
   4397   1.6  christos 	      /* Unmapped overlay -- can't be a match.  */
   4398   1.6  christos 	      continue;
   4399   1.6  christos 	    }
   4400   1.6  christos 
   4401   1.6  christos 	  return 1;
   4402   1.6  christos 	}
   4403   1.6  christos     }
   4404   1.1  christos 
   4405   1.1  christos   return 0;
   4406   1.1  christos }
   4407   1.1  christos 
   4408   1.1  christos /* Return true if there's a moribund breakpoint at PC.  */
   4409   1.8  christos 
   4410   1.1  christos int
   4411   1.8  christos moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
   4412   1.1  christos {
   4413   1.1  christos   for (bp_location *loc : moribund_locations)
   4414   1.1  christos     if (breakpoint_location_address_match (loc, aspace, pc))
   4415   1.1  christos       return 1;
   4416   1.1  christos 
   4417   1.1  christos   return 0;
   4418  1.10  christos }
   4419   1.3  christos 
   4420  1.10  christos /* Returns true iff BL is inserted at PC, in address space ASPACE.  */
   4421  1.10  christos 
   4422   1.8  christos static bool
   4423   1.3  christos bp_location_inserted_here_p (const struct bp_location *bl,
   4424   1.3  christos 			     const address_space *aspace, CORE_ADDR pc)
   4425  1.11  christos {
   4426   1.3  christos   if (bl->inserted
   4427   1.3  christos       && breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
   4428  1.10  christos 				   aspace, pc))
   4429  1.10  christos     {
   4430  1.10  christos       /* An unmapped overlay can't be a match.  */
   4431  1.10  christos       return !(overlay_debugging
   4432   1.3  christos 	       && section_is_overlay (bl->section)
   4433  1.10  christos 	       && !section_is_mapped (bl->section));
   4434   1.3  christos     }
   4435   1.3  christos   return false;
   4436   1.3  christos }
   4437   1.1  christos 
   4438   1.1  christos /* Returns non-zero iff there's a breakpoint inserted at PC.  */
   4439   1.8  christos 
   4440   1.1  christos int
   4441  1.10  christos breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
   4442   1.1  christos {
   4443   1.1  christos   for (bp_location *bl : all_bp_locations_at_addr (pc))
   4444   1.1  christos     {
   4445   1.1  christos       if (bl->loc_type != bp_loc_software_breakpoint
   4446   1.1  christos 	  && bl->loc_type != bp_loc_hardware_breakpoint)
   4447   1.3  christos 	continue;
   4448   1.3  christos 
   4449   1.1  christos       if (bp_location_inserted_here_p (bl, aspace, pc))
   4450   1.1  christos 	return 1;
   4451   1.1  christos     }
   4452   1.1  christos   return 0;
   4453   1.3  christos }
   4454   1.3  christos 
   4455   1.1  christos /* This function returns non-zero iff there is a software breakpoint
   4456   1.1  christos    inserted at PC.  */
   4457   1.8  christos 
   4458   1.3  christos int
   4459   1.1  christos software_breakpoint_inserted_here_p (const address_space *aspace,
   4460  1.10  christos 				     CORE_ADDR pc)
   4461   1.3  christos {
   4462   1.3  christos   for (bp_location *bl : all_bp_locations_at_addr (pc))
   4463   1.3  christos     {
   4464   1.1  christos       if (bl->loc_type != bp_loc_software_breakpoint)
   4465   1.3  christos 	continue;
   4466   1.3  christos 
   4467   1.3  christos       if (bp_location_inserted_here_p (bl, aspace, pc))
   4468   1.1  christos 	return 1;
   4469   1.1  christos     }
   4470   1.1  christos 
   4471   1.1  christos   return 0;
   4472   1.3  christos }
   4473   1.1  christos 
   4474   1.1  christos /* See breakpoint.h.  */
   4475   1.8  christos 
   4476   1.1  christos int
   4477   1.1  christos hardware_breakpoint_inserted_here_p (const address_space *aspace,
   4478  1.10  christos 				     CORE_ADDR pc)
   4479   1.1  christos {
   4480   1.3  christos   for (bp_location *bl : all_bp_locations_at_addr (pc))
   4481   1.1  christos     {
   4482   1.1  christos       if (bl->loc_type != bp_loc_hardware_breakpoint)
   4483   1.3  christos 	continue;
   4484   1.3  christos 
   4485   1.1  christos       if (bp_location_inserted_here_p (bl, aspace, pc))
   4486   1.1  christos 	return 1;
   4487   1.1  christos     }
   4488   1.1  christos 
   4489   1.1  christos   return 0;
   4490   1.1  christos }
   4491   1.8  christos 
   4492   1.1  christos int
   4493   1.1  christos hardware_watchpoint_inserted_in_range (const address_space *aspace,
   4494  1.11  christos 				       CORE_ADDR addr, ULONGEST len)
   4495   1.1  christos {
   4496  1.11  christos   for (breakpoint &bpt : all_breakpoints ())
   4497  1.11  christos     {
   4498   1.1  christos       if (bpt.type != bp_hardware_watchpoint
   4499   1.1  christos 	  && bpt.type != bp_access_watchpoint)
   4500  1.11  christos 	continue;
   4501   1.1  christos 
   4502   1.1  christos       if (!breakpoint_enabled (&bpt))
   4503  1.11  christos 	continue;
   4504  1.11  christos 
   4505   1.1  christos       for (bp_location &loc : bpt.locations ())
   4506   1.1  christos 	if (loc.pspace->aspace.get () == aspace && loc.inserted)
   4507   1.1  christos 	  {
   4508   1.1  christos 	    CORE_ADDR l, h;
   4509  1.11  christos 
   4510  1.11  christos 	    /* Check for intersection.  */
   4511   1.1  christos 	    l = std::max<CORE_ADDR> (loc.address, addr);
   4512   1.1  christos 	    h = std::min<CORE_ADDR> (loc.address + loc.length, addr + len);
   4513   1.1  christos 	    if (l < h)
   4514   1.1  christos 	      return 1;
   4515   1.1  christos 	  }
   4516   1.1  christos     }
   4517   1.1  christos   return 0;
   4518   1.9  christos }
   4519   1.1  christos 
   4520   1.9  christos /* See breakpoint.h.  */
   4521   1.9  christos 
   4522   1.1  christos bool
   4523   1.9  christos is_catchpoint (struct breakpoint *b)
   4524   1.1  christos {
   4525   1.1  christos   return (b->type == bp_catchpoint);
   4526   1.1  christos }
   4527   1.1  christos 
   4528   1.1  christos /* Clear a bpstat so that it says we are not at any breakpoint.
   4529   1.1  christos    Also free any storage that is part of a bpstat.  */
   4530  1.10  christos 
   4531   1.1  christos void
   4532  1.10  christos bpstat_clear (bpstat **bsp)
   4533  1.10  christos {
   4534   1.1  christos   bpstat *p;
   4535   1.1  christos   bpstat *q;
   4536   1.1  christos 
   4537   1.1  christos   if (bsp == 0)
   4538   1.1  christos     return;
   4539   1.1  christos   p = *bsp;
   4540   1.1  christos   while (p != NULL)
   4541   1.8  christos     {
   4542   1.1  christos       q = p->next;
   4543   1.1  christos       delete p;
   4544   1.1  christos       p = q;
   4545   1.1  christos     }
   4546   1.1  christos   *bsp = NULL;
   4547  1.10  christos }
   4548   1.8  christos 
   4549   1.8  christos bpstat::bpstat (const bpstat &other)
   4550   1.8  christos   : next (NULL),
   4551   1.8  christos     bp_location_at (other.bp_location_at),
   4552   1.8  christos     breakpoint_at (other.breakpoint_at),
   4553   1.8  christos     commands (other.commands),
   4554   1.8  christos     print (other.print),
   4555   1.8  christos     stop (other.stop),
   4556   1.8  christos     print_it (other.print_it)
   4557  1.11  christos {
   4558   1.8  christos   if (other.old_val != NULL)
   4559   1.8  christos     old_val = release_value (other.old_val->copy ());
   4560   1.1  christos }
   4561   1.1  christos 
   4562   1.1  christos /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
   4563  1.10  christos    is part of the bpstat is copied as well.  */
   4564  1.10  christos 
   4565   1.1  christos bpstat *
   4566  1.10  christos bpstat_copy (bpstat *bs)
   4567  1.10  christos {
   4568  1.10  christos   bpstat *p = nullptr;
   4569   1.1  christos   bpstat *tmp;
   4570   1.1  christos   bpstat *retval = nullptr;
   4571   1.1  christos 
   4572   1.1  christos   if (bs == NULL)
   4573   1.1  christos     return bs;
   4574   1.1  christos 
   4575  1.10  christos   for (; bs != NULL; bs = bs->next)
   4576   1.1  christos     {
   4577   1.1  christos       tmp = new bpstat (*bs);
   4578   1.1  christos 
   4579   1.1  christos       if (p == NULL)
   4580   1.1  christos 	/* This is the first thing in the chain.  */
   4581   1.1  christos 	retval = tmp;
   4582   1.1  christos       else
   4583   1.1  christos 	p->next = tmp;
   4584   1.1  christos       p = tmp;
   4585   1.1  christos     }
   4586   1.1  christos   p->next = NULL;
   4587   1.1  christos   return retval;
   4588   1.1  christos }
   4589   1.1  christos 
   4590  1.10  christos /* Find the bpstat associated with this breakpoint.  */
   4591  1.10  christos 
   4592   1.1  christos bpstat *
   4593   1.1  christos bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
   4594   1.1  christos {
   4595   1.1  christos   if (bsp == NULL)
   4596   1.1  christos     return NULL;
   4597   1.1  christos 
   4598   1.1  christos   for (; bsp != NULL; bsp = bsp->next)
   4599   1.1  christos     {
   4600   1.1  christos       if (bsp->breakpoint_at == breakpoint)
   4601   1.1  christos 	return bsp;
   4602   1.1  christos     }
   4603   1.1  christos   return NULL;
   4604   1.1  christos }
   4605   1.1  christos 
   4606   1.9  christos /* See breakpoint.h.  */
   4607  1.10  christos 
   4608   1.1  christos bool
   4609   1.1  christos bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
   4610   1.1  christos {
   4611   1.1  christos   for (; bsp != NULL; bsp = bsp->next)
   4612   1.1  christos     {
   4613   1.1  christos       if (bsp->breakpoint_at == NULL)
   4614   1.1  christos 	{
   4615   1.1  christos 	  /* A moribund location can never explain a signal other than
   4616   1.9  christos 	     GDB_SIGNAL_TRAP.  */
   4617   1.1  christos 	  if (sig == GDB_SIGNAL_TRAP)
   4618   1.1  christos 	    return true;
   4619   1.1  christos 	}
   4620  1.10  christos       else
   4621   1.9  christos 	{
   4622   1.1  christos 	  if (bsp->breakpoint_at->explains_signal (sig))
   4623   1.1  christos 	    return true;
   4624   1.1  christos 	}
   4625   1.9  christos     }
   4626   1.1  christos 
   4627   1.1  christos   return false;
   4628  1.10  christos }
   4629   1.1  christos 
   4630   1.1  christos /* See breakpoint.h.  */
   4631  1.10  christos 
   4632   1.1  christos int
   4633   1.1  christos bpstat_num (bpstat **bsp, int *num)
   4634   1.1  christos {
   4635   1.1  christos   struct breakpoint *b;
   4636   1.1  christos 
   4637   1.1  christos   if ((*bsp) == NULL)
   4638   1.1  christos     return 0;			/* No more breakpoint values */
   4639   1.1  christos 
   4640   1.1  christos   /* We assume we'll never have several bpstats that correspond to a
   4641   1.1  christos      single breakpoint -- otherwise, this function might return the
   4642   1.1  christos      same number more than once and this will look ugly.  */
   4643   1.1  christos   b = (*bsp)->breakpoint_at;
   4644   1.1  christos   *bsp = (*bsp)->next;
   4645   1.1  christos   if (b == NULL)
   4646   1.1  christos     return -1;			/* breakpoint that's been deleted since */
   4647   1.1  christos 
   4648   1.1  christos   *num = b->number;		/* We have its number */
   4649   1.1  christos   return 1;
   4650  1.10  christos }
   4651  1.10  christos 
   4652  1.10  christos /* See breakpoint.h  */
   4653  1.10  christos 
   4654  1.10  christos int
   4655  1.10  christos bpstat_locno (const bpstat *bs)
   4656  1.10  christos {
   4657  1.10  christos   const struct breakpoint *b = bs->breakpoint_at;
   4658  1.11  christos   const struct bp_location *bl = bs->bp_location_at.get ();
   4659  1.10  christos 
   4660  1.11  christos   if (b != nullptr && b->has_multiple_locations ())
   4661  1.10  christos     {
   4662  1.11  christos       int locno = 1;
   4663  1.11  christos 
   4664  1.11  christos       for (bp_location &loc : b->locations ())
   4665  1.11  christos 	{
   4666  1.10  christos 	  if (bl == &loc)
   4667  1.11  christos 	    return locno;
   4668  1.10  christos 
   4669  1.11  christos 	  ++locno;
   4670  1.11  christos 	}
   4671  1.11  christos 
   4672  1.10  christos       warning (_("location number not found for breakpoint %d address %s."),
   4673  1.10  christos 	       b->number, paddress (bl->gdbarch, bl->address));
   4674  1.11  christos     }
   4675  1.10  christos 
   4676  1.10  christos   return 0;
   4677  1.10  christos }
   4678  1.10  christos 
   4679  1.10  christos /* See breakpoint.h.  */
   4680  1.10  christos 
   4681  1.10  christos void
   4682  1.10  christos print_num_locno (const bpstat *bs, struct ui_out *uiout)
   4683  1.10  christos {
   4684  1.10  christos   struct breakpoint *b = bs->breakpoint_at;
   4685  1.10  christos 
   4686  1.10  christos   if (b == nullptr)
   4687  1.10  christos     uiout->text (_("deleted breakpoint"));
   4688  1.10  christos   else
   4689  1.10  christos     {
   4690  1.10  christos       uiout->field_signed ("bkptno", b->number);
   4691  1.10  christos 
   4692  1.10  christos       int locno = bpstat_locno (bs);
   4693  1.10  christos       if (locno != 0)
   4694  1.10  christos 	uiout->message (".%pF", signed_field ("locno", locno));
   4695  1.10  christos     }
   4696   1.1  christos }
   4697   1.1  christos 
   4698   1.1  christos /* See breakpoint.h.  */
   4699   1.1  christos 
   4700   1.1  christos void
   4701  1.10  christos bpstat_clear_actions (void)
   4702   1.1  christos {
   4703   1.8  christos   bpstat *bs;
   4704   1.1  christos 
   4705   1.1  christos   if (inferior_ptid == null_ptid)
   4706   1.8  christos     return;
   4707   1.1  christos 
   4708   1.1  christos   thread_info *tp = inferior_thread ();
   4709   1.8  christos   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
   4710   1.8  christos     {
   4711   1.1  christos       bs->commands = NULL;
   4712   1.1  christos       bs->old_val.reset (nullptr);
   4713   1.1  christos     }
   4714   1.1  christos }
   4715   1.1  christos 
   4716   1.1  christos /* Called when a command is about to proceed the inferior.  */
   4717   1.1  christos 
   4718   1.1  christos static void
   4719   1.8  christos breakpoint_about_to_proceed (void)
   4720   1.1  christos {
   4721   1.1  christos   if (inferior_ptid != null_ptid)
   4722   1.1  christos     {
   4723   1.1  christos       struct thread_info *tp = inferior_thread ();
   4724   1.1  christos 
   4725   1.1  christos       /* Allow inferior function calls in breakpoint commands to not
   4726   1.1  christos 	 interrupt the command list.  When the call finishes
   4727   1.1  christos 	 successfully, the inferior will be standing at the same
   4728   1.1  christos 	 breakpoint as if nothing happened.  */
   4729   1.1  christos       if (tp->control.in_infcall)
   4730   1.1  christos 	return;
   4731   1.1  christos     }
   4732   1.1  christos 
   4733   1.1  christos   breakpoint_proceeded = 1;
   4734  1.10  christos }
   4735   1.1  christos 
   4736   1.1  christos /* Return true iff CMD as the first line of a command sequence is `silent'
   4737  1.10  christos    or its equivalent.  */
   4738   1.1  christos 
   4739   1.1  christos static bool
   4740   1.5  christos command_line_is_silent (struct command_line *cmd)
   4741   1.1  christos {
   4742   1.1  christos   return cmd && (strcmp ("silent", cmd->line) == 0);
   4743  1.10  christos }
   4744  1.10  christos 
   4745  1.10  christos /* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
   4746  1.10  christos    A locno 0 is changed to 1 to e.g. let the user do
   4747  1.10  christos      (gdb) disable $_hit_bpnum.$_hit_locno
   4748  1.10  christos    for a single location breakpoint.  */
   4749  1.10  christos 
   4750  1.10  christos static void
   4751  1.10  christos set_hit_convenience_vars (int bpnum, int locno)
   4752  1.10  christos {
   4753  1.10  christos   set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
   4754  1.10  christos   set_internalvar_integer (lookup_internalvar ("_hit_locno"),
   4755  1.10  christos 			   (locno > 0 ? locno : 1));
   4756   1.1  christos }
   4757   1.1  christos 
   4758   1.1  christos /* Execute all the commands associated with all the breakpoints at
   4759   1.1  christos    this location.  Any of these commands could cause the process to
   4760   1.1  christos    proceed beyond this point, etc.  We look out for such changes by
   4761   1.1  christos    checking the global "breakpoint_proceeded" after each command.
   4762   1.1  christos 
   4763   1.1  christos    Returns true if a breakpoint command resumed the inferior.  In that
   4764   1.1  christos    case, it is the caller's responsibility to recall it again with the
   4765  1.10  christos    bpstat of the current thread.  */
   4766  1.10  christos 
   4767   1.1  christos static bool
   4768  1.10  christos bpstat_do_actions_1 (bpstat **bsp)
   4769  1.10  christos {
   4770   1.1  christos   bpstat *bs;
   4771   1.1  christos   bool again = false;
   4772   1.1  christos 
   4773   1.1  christos   /* Avoid endless recursion if a `source' command is contained
   4774  1.10  christos      in bs->commands.  */
   4775   1.1  christos   if (executing_breakpoint_commands)
   4776   1.8  christos     return false;
   4777   1.8  christos 
   4778   1.1  christos   scoped_restore save_executing
   4779   1.7  christos     = make_scoped_restore (&executing_breakpoint_commands, 1);
   4780   1.1  christos 
   4781   1.1  christos   scoped_restore preventer = prevent_dont_repeat ();
   4782   1.1  christos 
   4783   1.1  christos   /* This pointer will iterate over the list of bpstat's.  */
   4784  1.10  christos   bs = *bsp;
   4785  1.10  christos 
   4786  1.10  christos   /* The $_hit_* convenience variables are set before running the
   4787  1.10  christos      commands of BS.  In case we have several bs, after the loop,
   4788  1.10  christos      we set again the variables to the first printed bpnum and locno.
   4789  1.10  christos      For multiple breakpoints, this ensures the variables are set to the
   4790  1.10  christos      breakpoint printed for the user. */
   4791  1.10  christos   int printed_hit_bpnum = -1;
   4792   1.1  christos   int printed_hit_locno = -1;
   4793   1.1  christos 
   4794   1.1  christos   breakpoint_proceeded = 0;
   4795   1.8  christos   for (; bs != NULL; bs = bs->next)
   4796   1.1  christos     {
   4797  1.10  christos       struct command_line *cmd = NULL;
   4798  1.10  christos 
   4799  1.10  christos       /* Set the _hit_* convenience variables before running BS's commands.  */
   4800  1.10  christos       {
   4801  1.10  christos 	const struct breakpoint *b = bs->breakpoint_at;
   4802  1.10  christos 	if (b != nullptr)
   4803  1.10  christos 	  {
   4804  1.10  christos 	    int locno = bpstat_locno (bs);
   4805  1.10  christos 
   4806  1.10  christos 	    set_hit_convenience_vars (b->number, locno);
   4807  1.10  christos 	    if (printed_hit_locno == -1 && bs->print)
   4808  1.10  christos 	      {
   4809  1.10  christos 		printed_hit_bpnum = b->number;
   4810  1.10  christos 		printed_hit_locno = locno;
   4811  1.10  christos 	      }
   4812  1.10  christos 	  }
   4813   1.1  christos       }
   4814   1.1  christos 
   4815  1.10  christos       /* Take ownership of the BSP's command tree, if it has one.
   4816  1.10  christos 
   4817  1.10  christos 	 The command tree could legitimately contain commands like
   4818  1.10  christos 	 'step' and 'next', which call clear_proceed_status, which
   4819  1.10  christos 	 frees the bpstat BS and its command tree.  To make sure this doesn't
   4820  1.10  christos 	 free the tree we're executing out from under us, we need to
   4821  1.10  christos 	 take ownership of the tree ourselves.  Since a given bpstat's
   4822  1.10  christos 	 commands are only executed once, we don't need to copy it; we
   4823   1.8  christos 	 can clear the pointer in the bpstat, and make sure we free
   4824   1.1  christos 	 the tree when we're done.  */
   4825   1.8  christos       counted_command_line ccmd = bs->commands;
   4826   1.8  christos       bs->commands = NULL;
   4827   1.1  christos       if (ccmd != NULL)
   4828   1.1  christos 	cmd = ccmd.get ();
   4829   1.1  christos       if (command_line_is_silent (cmd))
   4830   1.1  christos 	{
   4831   1.1  christos 	  /* The action has been already done by bpstat_stop_status.  */
   4832   1.1  christos 	  cmd = cmd->next;
   4833   1.1  christos 	}
   4834   1.1  christos 
   4835   1.1  christos       while (cmd != NULL)
   4836  1.10  christos 	{
   4837  1.10  christos 	  execute_control_command (cmd);
   4838   1.1  christos 	  /* After execute_control_command, if breakpoint_proceeded is true,
   4839   1.1  christos 	     BS has been freed and cannot be accessed anymore.  */
   4840   1.1  christos 
   4841   1.1  christos 	  if (breakpoint_proceeded)
   4842   1.1  christos 	    break;
   4843   1.1  christos 	  else
   4844   1.1  christos 	    cmd = cmd->next;
   4845   1.1  christos 	}
   4846   1.1  christos 
   4847   1.6  christos       if (breakpoint_proceeded)
   4848   1.1  christos 	{
   4849   1.1  christos 	  if (current_ui->async)
   4850   1.1  christos 	    /* If we are in async mode, then the target might be still
   4851   1.1  christos 	       running, not stopped at any breakpoint, so nothing for
   4852   1.1  christos 	       us to do here -- just return to the event loop.  */
   4853   1.1  christos 	    ;
   4854   1.1  christos 	  else
   4855   1.1  christos 	    /* In sync mode, when execute_control_command returns
   4856   1.1  christos 	       we're already standing on the next breakpoint.
   4857   1.1  christos 	       Breakpoint commands for that stop were not run, since
   4858   1.1  christos 	       execute_command does not run breakpoint commands --
   4859   1.1  christos 	       only command_line_handler does, but that one is not
   4860   1.1  christos 	       involved in execution of breakpoint commands.  So, we
   4861   1.1  christos 	       can now execute breakpoint commands.  It should be
   4862   1.1  christos 	       noted that making execute_command do bpstat actions is
   4863   1.1  christos 	       not an option -- in this case we'll have recursive
   4864   1.1  christos 	       invocation of bpstat for each breakpoint with a
   4865   1.1  christos 	       command, and can easily blow up GDB stack.  Instead, we
   4866  1.10  christos 	       return true, which will trigger the caller to recall us
   4867   1.1  christos 	       with the new stop_bpstat.  */
   4868   1.1  christos 	    again = true;
   4869   1.1  christos 	  break;
   4870  1.10  christos 	}
   4871  1.10  christos     }
   4872  1.10  christos 
   4873  1.10  christos   /* Now that we have executed the commands of all bs, set the _hit_*
   4874  1.10  christos      convenience variables to the printed values.  */
   4875  1.10  christos   if (printed_hit_locno != -1)
   4876   1.1  christos     set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
   4877   1.1  christos 
   4878   1.1  christos   return again;
   4879   1.8  christos }
   4880   1.8  christos 
   4881   1.8  christos /* Helper for bpstat_do_actions.  Get the current thread, if there's
   4882   1.8  christos    one, is alive and has execution.  Return NULL otherwise.  */
   4883   1.8  christos 
   4884   1.8  christos static thread_info *
   4885  1.10  christos get_bpstat_thread ()
   4886   1.8  christos {
   4887   1.8  christos   if (inferior_ptid == null_ptid || !target_has_execution ())
   4888   1.8  christos     return NULL;
   4889  1.10  christos 
   4890   1.8  christos   thread_info *tp = inferior_thread ();
   4891   1.8  christos   if (tp->state == THREAD_EXITED || tp->executing ())
   4892   1.8  christos     return NULL;
   4893   1.8  christos   return tp;
   4894   1.1  christos }
   4895   1.1  christos 
   4896   1.1  christos void
   4897   1.8  christos bpstat_do_actions (void)
   4898   1.8  christos {
   4899   1.1  christos   auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
   4900   1.1  christos   thread_info *tp;
   4901   1.8  christos 
   4902   1.8  christos   /* Do any commands attached to breakpoint we are stopped at.  */
   4903   1.8  christos   while ((tp = get_bpstat_thread ()) != NULL)
   4904   1.8  christos     {
   4905   1.8  christos       /* Since in sync mode, bpstat_do_actions may resume the
   4906   1.8  christos 	 inferior, and only return when it is stopped at the next
   4907   1.8  christos 	 breakpoint, we keep doing breakpoint actions until it returns
   4908   1.8  christos 	 false to indicate the inferior was not resumed.  */
   4909   1.8  christos       if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
   4910   1.1  christos 	break;
   4911   1.8  christos     }
   4912   1.1  christos 
   4913   1.1  christos   cleanup_if_error.release ();
   4914   1.1  christos }
   4915   1.1  christos 
   4916   1.1  christos /* Print out the (old or new) value associated with a watchpoint.  */
   4917   1.1  christos 
   4918   1.1  christos static void
   4919   1.1  christos watchpoint_value_print (struct value *val, struct ui_file *stream)
   4920   1.9  christos {
   4921   1.1  christos   if (val == NULL)
   4922   1.1  christos     fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
   4923   1.1  christos   else
   4924   1.1  christos     {
   4925   1.1  christos       struct value_print_options opts;
   4926   1.1  christos       get_user_print_options (&opts);
   4927   1.1  christos       value_print (val, stream, &opts);
   4928   1.1  christos     }
   4929   1.6  christos }
   4930   1.6  christos 
   4931   1.6  christos /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
   4932   1.6  christos    debugging multiple threads.  */
   4933   1.6  christos 
   4934   1.6  christos void
   4935   1.7  christos maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
   4936   1.6  christos {
   4937   1.6  christos   if (uiout->is_mi_like_p ())
   4938   1.7  christos     return;
   4939   1.6  christos 
   4940   1.6  christos   uiout->text ("\n");
   4941   1.6  christos 
   4942   1.6  christos   if (show_thread_that_caused_stop ())
   4943   1.6  christos     {
   4944   1.7  christos       struct thread_info *thr = inferior_thread ();
   4945   1.9  christos 
   4946   1.6  christos       uiout->text ("Thread ");
   4947  1.10  christos       uiout->field_string ("thread-id", print_thread_id (thr));
   4948   1.6  christos 
   4949   1.6  christos       const char *name = thread_name (thr);
   4950   1.7  christos       if (name != NULL)
   4951   1.9  christos 	{
   4952   1.7  christos 	  uiout->text (" \"");
   4953   1.6  christos 	  uiout->field_string ("name", name);
   4954   1.6  christos 	  uiout->text ("\"");
   4955   1.7  christos 	}
   4956   1.6  christos 
   4957   1.6  christos       uiout->text (" hit ");
   4958   1.6  christos     }
   4959   1.1  christos }
   4960   1.1  christos 
   4961   1.1  christos /* Generic routine for printing messages indicating why we
   4962   1.1  christos    stopped.  The behavior of this function depends on the value
   4963   1.1  christos    'print_it' in the bpstat structure.  Under some circumstances we
   4964   1.1  christos    may decide not to print anything here and delegate the task to
   4965   1.1  christos    normal_stop().  */
   4966  1.10  christos 
   4967   1.1  christos static enum print_stop_action
   4968   1.1  christos print_bp_stop_message (bpstat *bs)
   4969   1.1  christos {
   4970   1.1  christos   switch (bs->print_it)
   4971   1.1  christos     {
   4972   1.1  christos     case print_it_noop:
   4973   1.1  christos       /* Nothing should be printed for this bpstat entry.  */
   4974   1.1  christos       return PRINT_UNKNOWN;
   4975   1.1  christos 
   4976  1.10  christos     case print_it_done:
   4977   1.1  christos       /* We still want to print the frame, but we already printed the
   4978   1.1  christos 	 relevant messages.  */
   4979   1.1  christos       return PRINT_SRC_AND_LOC;
   4980   1.1  christos 
   4981   1.1  christos     case print_it_normal:
   4982   1.1  christos       {
   4983   1.1  christos 	struct breakpoint *b = bs->breakpoint_at;
   4984   1.1  christos 
   4985   1.1  christos 	/* bs->breakpoint_at can be NULL if it was a momentary breakpoint
   4986   1.1  christos 	   which has since been deleted.  */
   4987   1.1  christos 	if (b == NULL)
   4988   1.1  christos 	  return PRINT_UNKNOWN;
   4989  1.10  christos 
   4990   1.1  christos 	/* Normal case.  Call the breakpoint's print_it method.  */
   4991   1.1  christos 	return b->print_it (bs);
   4992   1.1  christos       }
   4993  1.10  christos 
   4994   1.1  christos     default:
   4995   1.1  christos       internal_error (_("print_bp_stop_message: unrecognized enum value"));
   4996   1.1  christos     }
   4997  1.10  christos }
   4998   1.1  christos 
   4999  1.10  christos /* See breakpoint.h.  */
   5000  1.10  christos 
   5001   1.1  christos void
   5002   1.8  christos print_solib_event (bool is_catchpoint)
   5003   1.8  christos {
   5004   1.1  christos   bool any_deleted = !current_program_space->deleted_solibs.empty ();
   5005   1.1  christos   bool any_added = !current_program_space->added_solibs.empty ();
   5006   1.1  christos 
   5007   1.1  christos   if (!is_catchpoint)
   5008   1.7  christos     {
   5009   1.1  christos       if (any_added || any_deleted)
   5010   1.7  christos 	current_uiout->text (_("Stopped due to shared library event:\n"));
   5011   1.7  christos       else
   5012   1.1  christos 	current_uiout->text (_("Stopped due to shared library event (no "
   5013   1.1  christos 			       "libraries added or removed)\n"));
   5014   1.7  christos     }
   5015   1.7  christos 
   5016   1.7  christos   if (current_uiout->is_mi_like_p ())
   5017   1.1  christos     current_uiout->field_string ("reason",
   5018   1.1  christos 				 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
   5019   1.1  christos 
   5020   1.7  christos   if (any_deleted)
   5021   1.8  christos     {
   5022   1.8  christos       current_uiout->text (_("  Inferior unloaded "));
   5023   1.1  christos       ui_out_emit_list list_emitter (current_uiout, "removed");
   5024   1.8  christos       for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
   5025   1.8  christos 	{
   5026   1.1  christos 	  const std::string &name = current_program_space->deleted_solibs[ix];
   5027   1.7  christos 
   5028   1.7  christos 	  if (ix > 0)
   5029   1.7  christos 	    current_uiout->text ("    ");
   5030   1.1  christos 	  current_uiout->field_string ("library", name);
   5031   1.1  christos 	  current_uiout->text ("\n");
   5032   1.1  christos 	}
   5033   1.1  christos     }
   5034   1.1  christos 
   5035   1.7  christos   if (any_added)
   5036   1.8  christos     {
   5037   1.8  christos       current_uiout->text (_("  Inferior loaded "));
   5038  1.11  christos       ui_out_emit_list list_emitter (current_uiout, "added");
   5039   1.1  christos       bool first = true;
   5040   1.8  christos       for (solib *iter : current_program_space->added_solibs)
   5041   1.7  christos 	{
   5042   1.8  christos 	  if (!first)
   5043   1.7  christos 	    current_uiout->text ("    ");
   5044   1.7  christos 	  first = false;
   5045   1.1  christos 	  current_uiout->field_string ("library", iter->so_name);
   5046   1.1  christos 	  current_uiout->text ("\n");
   5047   1.1  christos 	}
   5048   1.1  christos     }
   5049   1.1  christos }
   5050   1.1  christos 
   5051   1.1  christos /* Print a message indicating what happened.  This is called from
   5052   1.1  christos    normal_stop().  The input to this routine is the head of the bpstat
   5053   1.1  christos    list - a list of the eventpoints that caused this stop.  KIND is
   5054   1.1  christos    the target_waitkind for the stopping event.  This
   5055   1.1  christos    routine calls the generic print routine for printing a message
   5056   1.1  christos    about reasons for stopping.  This will print (for example) the
   5057   1.1  christos    "Breakpoint n," part of the output.  The return value of this
   5058   1.1  christos    routine is one of:
   5059   1.1  christos 
   5060   1.1  christos    PRINT_UNKNOWN: Means we printed nothing.
   5061   1.1  christos    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
   5062   1.1  christos    code to print the location.  An example is
   5063   1.1  christos    "Breakpoint 1, " which should be followed by
   5064   1.1  christos    the location.
   5065   1.1  christos    PRINT_SRC_ONLY: Means we printed something, but there is no need
   5066   1.1  christos    to also print the location part of the message.
   5067   1.1  christos    An example is the catch/throw messages, which
   5068   1.1  christos    don't require a location appended to the end.
   5069   1.1  christos    PRINT_NOTHING: We have done some printing and we don't need any
   5070   1.1  christos    further info to be printed.  */
   5071  1.10  christos 
   5072   1.1  christos enum print_stop_action
   5073   1.6  christos bpstat_print (bpstat *bs, target_waitkind kind)
   5074   1.1  christos {
   5075   1.1  christos   enum print_stop_action val;
   5076   1.1  christos 
   5077   1.1  christos   /* Maybe another breakpoint in the chain caused us to stop.
   5078   1.1  christos      (Currently all watchpoints go on the bpstat whether hit or not.
   5079   1.1  christos      That probably could (should) be changed, provided care is taken
   5080   1.1  christos      with respect to bpstat_explains_signal).  */
   5081   1.1  christos   for (; bs; bs = bs->next)
   5082   1.1  christos     {
   5083  1.10  christos       val = print_bp_stop_message (bs);
   5084   1.1  christos       if (val == PRINT_SRC_ONLY
   5085   1.1  christos 	  || val == PRINT_SRC_AND_LOC
   5086   1.1  christos 	  || val == PRINT_NOTHING)
   5087   1.1  christos 	return val;
   5088   1.1  christos     }
   5089   1.1  christos 
   5090   1.1  christos   /* If we had hit a shared library event breakpoint,
   5091   1.1  christos      print_bp_stop_message would print out this message.  If we hit an
   5092   1.1  christos      OS-level shared library event, do the same thing.  */
   5093  1.10  christos   if (kind == TARGET_WAITKIND_LOADED)
   5094   1.1  christos     {
   5095   1.1  christos       print_solib_event (false);
   5096   1.1  christos       return PRINT_NOTHING;
   5097   1.1  christos     }
   5098   1.1  christos 
   5099   1.1  christos   /* We reached the end of the chain, or we got a null BS to start
   5100   1.1  christos      with and nothing was printed.  */
   5101   1.1  christos   return PRINT_UNKNOWN;
   5102   1.8  christos }
   5103   1.1  christos 
   5104   1.8  christos /* Evaluate the boolean expression EXP and return the result.  */
   5105   1.8  christos 
   5106   1.1  christos static bool
   5107  1.10  christos breakpoint_cond_eval (expression *exp)
   5108  1.11  christos {
   5109   1.1  christos   scoped_value_mark mark;
   5110   1.1  christos   return value_true (exp->evaluate ());
   5111   1.1  christos }
   5112   1.1  christos 
   5113  1.10  christos /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
   5114   1.8  christos 
   5115  1.10  christos bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
   5116   1.8  christos   : next (NULL),
   5117   1.8  christos     bp_location_at (bp_location_ref_ptr::new_reference (bl)),
   5118   1.8  christos     breakpoint_at (bl->owner),
   5119   1.8  christos     commands (NULL),
   5120   1.8  christos     print (0),
   5121   1.1  christos     stop (0),
   5122   1.8  christos     print_it (print_it_normal)
   5123   1.8  christos {
   5124   1.8  christos   **bs_link_pointer = this;
   5125   1.1  christos   *bs_link_pointer = &next;
   5126  1.10  christos }
   5127   1.8  christos 
   5128   1.8  christos bpstat::bpstat ()
   5129   1.8  christos   : next (NULL),
   5130   1.8  christos     breakpoint_at (NULL),
   5131   1.8  christos     commands (NULL),
   5132   1.8  christos     print (0),
   5133   1.8  christos     stop (0),
   5134   1.1  christos     print_it (print_it_normal)
   5135   1.1  christos {
   5136   1.1  christos }
   5137   1.1  christos 
   5138   1.1  christos /* The target has stopped with waitstatus WS.  Check if any hardware
   5140  1.10  christos    watchpoints have triggered, according to the target.  */
   5141   1.1  christos 
   5142   1.8  christos int
   5143   1.1  christos watchpoints_triggered (const target_waitstatus &ws)
   5144   1.1  christos {
   5145   1.1  christos   bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
   5146   1.1  christos   CORE_ADDR addr;
   5147   1.1  christos 
   5148   1.1  christos   if (!stopped_by_watchpoint)
   5149  1.11  christos     {
   5150  1.11  christos       /* We were not stopped by a watchpoint.  Mark all watchpoints
   5151   1.1  christos 	 as not triggered.  */
   5152  1.11  christos       for (breakpoint &b : all_breakpoints ())
   5153   1.1  christos 	if (is_hardware_watchpoint (&b))
   5154  1.11  christos 	  {
   5155   1.1  christos 	    watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
   5156   1.1  christos 
   5157   1.1  christos 	    w.watchpoint_triggered = watch_triggered_no;
   5158   1.1  christos 	  }
   5159   1.1  christos 
   5160  1.10  christos       return 0;
   5161   1.1  christos     }
   5162   1.1  christos 
   5163   1.1  christos   if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
   5164  1.11  christos     {
   5165  1.11  christos       /* We were stopped by a watchpoint, but we don't know where.
   5166   1.1  christos 	 Mark all watchpoints as unknown.  */
   5167  1.11  christos       for (breakpoint &b : all_breakpoints ())
   5168   1.1  christos 	if (is_hardware_watchpoint (&b))
   5169  1.11  christos 	  {
   5170   1.1  christos 	    watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
   5171   1.1  christos 
   5172   1.1  christos 	    w.watchpoint_triggered = watch_triggered_unknown;
   5173   1.1  christos 	  }
   5174   1.1  christos 
   5175   1.1  christos       return 1;
   5176   1.1  christos     }
   5177   1.1  christos 
   5178   1.1  christos   /* The target could report the data address.  Mark watchpoints
   5179  1.11  christos      affected by this data address as triggered, and all others as not
   5180  1.11  christos      triggered.  */
   5181   1.1  christos 
   5182  1.11  christos   for (breakpoint &b : all_breakpoints ())
   5183   1.1  christos     if (is_hardware_watchpoint (&b))
   5184  1.11  christos       {
   5185  1.11  christos 	watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
   5186   1.1  christos 
   5187  1.11  christos 	w.watchpoint_triggered = watch_triggered_no;
   5188   1.1  christos 	for (bp_location &loc : b.locations ())
   5189  1.11  christos 	  {
   5190  1.11  christos 	    if (is_masked_watchpoint (&b))
   5191   1.1  christos 	      {
   5192   1.1  christos 		CORE_ADDR newaddr = addr & w.hw_wp_mask;
   5193   1.1  christos 		CORE_ADDR start = loc.address & w.hw_wp_mask;
   5194  1.11  christos 
   5195   1.1  christos 		if (newaddr == start)
   5196   1.1  christos 		  {
   5197   1.1  christos 		    w.watchpoint_triggered = watch_triggered_yes;
   5198   1.1  christos 		    break;
   5199  1.10  christos 		  }
   5200  1.11  christos 	      }
   5201  1.11  christos 	    /* Exact match not required.  Within range is sufficient.  */
   5202   1.1  christos 	    else if (target_watchpoint_addr_within_range
   5203  1.11  christos 		       (current_inferior ()->top_target (), addr, loc.address,
   5204   1.1  christos 			loc.length))
   5205   1.1  christos 	      {
   5206   1.1  christos 		w.watchpoint_triggered = watch_triggered_yes;
   5207   1.1  christos 		break;
   5208   1.1  christos 	      }
   5209   1.1  christos 	  }
   5210   1.1  christos       }
   5211   1.1  christos 
   5212   1.8  christos   return 1;
   5213   1.8  christos }
   5214   1.8  christos 
   5215   1.8  christos /* Possible return values for watchpoint_check.  */
   5216   1.8  christos enum wp_check_result
   5217   1.8  christos   {
   5218   1.8  christos     /* The watchpoint has been deleted.  */
   5219   1.8  christos     WP_DELETED = 1,
   5220   1.8  christos 
   5221   1.8  christos     /* The value has changed.  */
   5222   1.8  christos     WP_VALUE_CHANGED = 2,
   5223   1.8  christos 
   5224   1.8  christos     /* The value has not changed.  */
   5225   1.8  christos     WP_VALUE_NOT_CHANGED = 3,
   5226   1.8  christos 
   5227   1.1  christos     /* Ignore this watchpoint, no matter if the value changed or not.  */
   5228   1.1  christos     WP_IGNORE = 4,
   5229   1.1  christos   };
   5230   1.1  christos 
   5231   1.1  christos #define BP_TEMPFLAG 1
   5232   1.8  christos #define BP_HARDWAREFLAG 2
   5233   1.1  christos 
   5234   1.8  christos /* Evaluate watchpoint condition expression and check if its value
   5235  1.10  christos    changed.  */
   5236   1.1  christos 
   5237  1.10  christos static wp_check_result
   5238  1.10  christos watchpoint_check (bpstat *bs)
   5239   1.1  christos {
   5240   1.1  christos   frame_info_ptr fr;
   5241   1.1  christos   bool within_current_scope;
   5242  1.11  christos 
   5243   1.1  christos   /* BS is built from an existing struct breakpoint.  */
   5244   1.1  christos   gdb_assert (bs->breakpoint_at != NULL);
   5245   1.1  christos   watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
   5246   1.1  christos 
   5247   1.1  christos   /* If this is a local watchpoint, we only want to check if the
   5248   1.1  christos      watchpoint frame is in scope if the current thread is the thread
   5249   1.1  christos      that was used to create the watchpoint.  */
   5250   1.1  christos   if (!watchpoint_in_thread_scope (b))
   5251  1.10  christos     return WP_IGNORE;
   5252   1.1  christos 
   5253   1.1  christos   if (b->exp_valid_block == NULL)
   5254  1.10  christos     within_current_scope = true;
   5255   1.1  christos   else
   5256   1.1  christos     {
   5257   1.1  christos       frame_info_ptr frame = get_current_frame ();
   5258   1.5  christos       struct gdbarch *frame_arch = get_frame_arch (frame);
   5259   1.1  christos       CORE_ADDR frame_pc = get_frame_pc (frame);
   5260   1.1  christos 
   5261   1.1  christos       /* stack_frame_destroyed_p() returns a non-zero value if we're
   5262   1.1  christos 	 still in the function but the stack frame has already been
   5263   1.1  christos 	 invalidated.  Since we can't rely on the values of local
   5264   1.1  christos 	 variables after the stack has been destroyed, we are treating
   5265   1.1  christos 	 the watchpoint in that state as `not changed' without further
   5266   1.1  christos 	 checking.  Don't mark watchpoints as changed if the current
   5267   1.5  christos 	 frame is in an epilogue - even if they are in some other
   5268   1.1  christos 	 frame, our view of the stack is likely to be wrong and
   5269   1.1  christos 	 frame_find_by_id could error out.  */
   5270   1.1  christos       if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
   5271   1.1  christos 	return WP_IGNORE;
   5272   1.1  christos 
   5273   1.1  christos       fr = frame_find_by_id (b->watchpoint_frame);
   5274   1.1  christos       within_current_scope = (fr != NULL);
   5275   1.1  christos 
   5276   1.1  christos       /* If we've gotten confused in the unwinder, we might have
   5277   1.1  christos 	 returned a frame that can't describe this variable.  */
   5278   1.1  christos       if (within_current_scope)
   5279   1.1  christos 	{
   5280   1.1  christos 	  struct symbol *function;
   5281  1.11  christos 
   5282  1.10  christos 	  function = get_frame_function (fr);
   5283   1.1  christos 	  if (function == NULL
   5284   1.1  christos 	      || !function->value_block ()->contains (b->exp_valid_block))
   5285   1.1  christos 	    within_current_scope = false;
   5286   1.1  christos 	}
   5287   1.1  christos 
   5288   1.1  christos       if (within_current_scope)
   5289   1.1  christos 	/* If we end up stopping, the current frame will get selected
   5290   1.1  christos 	   in normal_stop.  So this call to select_frame won't affect
   5291   1.1  christos 	   the user.  */
   5292   1.1  christos 	select_frame (fr);
   5293   1.1  christos     }
   5294   1.1  christos 
   5295  1.10  christos   if (within_current_scope)
   5296  1.10  christos     {
   5297  1.10  christos       /* We use value_{,free_to_}mark because it could be a *long*
   5298   1.1  christos 	 time before we return to the command level and call
   5299   1.1  christos 	 free_all_values.  We can't call free_all_values because we
   5300   1.1  christos 	 might be in the middle of evaluating a function call.  */
   5301   1.1  christos 
   5302   1.8  christos       struct value *mark;
   5303   1.1  christos       struct value *new_val;
   5304   1.1  christos 
   5305   1.1  christos       if (is_masked_watchpoint (b))
   5306   1.1  christos 	/* Since we don't know the exact trigger address (from
   5307   1.1  christos 	   stopped_data_address), just tell the user we've triggered
   5308   1.1  christos 	   a mask watchpoint.  */
   5309  1.10  christos 	return WP_VALUE_CHANGED;
   5310  1.10  christos 
   5311   1.1  christos       mark = value_mark ();
   5312   1.3  christos       fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
   5313   1.3  christos 			  NULL, NULL, false);
   5314   1.3  christos 
   5315   1.1  christos       if (b->val_bitsize != 0)
   5316   1.1  christos 	new_val = extract_bitfield_from_watchpoint_value (b, new_val);
   5317   1.1  christos 
   5318   1.1  christos       /* We use value_equal_contents instead of value_equal because
   5319   1.1  christos 	 the latter coerces an array to a pointer, thus comparing just
   5320   1.8  christos 	 the address of the array instead of its contents.  This is
   5321   1.8  christos 	 not what we want.  */
   5322   1.1  christos       if ((b->val != NULL) != (new_val != NULL)
   5323   1.1  christos 	  || (b->val != NULL && !value_equal_contents (b->val.get (),
   5324   1.8  christos 						       new_val)))
   5325   1.9  christos 	{
   5326   1.8  christos 	  bs->old_val = b->val;
   5327   1.8  christos 	  b->val = release_value (new_val);
   5328   1.1  christos 	  b->val_valid = true;
   5329   1.1  christos 	  if (new_val != NULL)
   5330   1.1  christos 	    value_free_to_mark (mark);
   5331   1.1  christos 	  return WP_VALUE_CHANGED;
   5332   1.1  christos 	}
   5333   1.1  christos       else
   5334   1.1  christos 	{
   5335   1.1  christos 	  /* Nothing changed.  */
   5336   1.1  christos 	  value_free_to_mark (mark);
   5337   1.1  christos 	  return WP_VALUE_NOT_CHANGED;
   5338   1.1  christos 	}
   5339   1.1  christos     }
   5340  1.10  christos   else
   5341  1.10  christos     {
   5342  1.10  christos       /* This seems like the only logical thing to do because
   5343  1.10  christos 	 if we temporarily ignored the watchpoint, then when
   5344  1.10  christos 	 we reenter the block in which it is valid it contains
   5345  1.10  christos 	 garbage (in the case of a function, it may have two
   5346  1.10  christos 	 garbage values, one before and one after the prologue).
   5347   1.1  christos 	 So we can't even detect the first assignment to it and
   5348  1.10  christos 	 watch after that (since the garbage may or may not equal
   5349  1.10  christos 	 the first value assigned).  */
   5350   1.1  christos       /* We print all the stop information in
   5351   1.1  christos 	 breakpointprint_it, but in this case, by the time we
   5352   1.6  christos 	 call breakpoint->print_it this bp will be deleted
   5353   1.7  christos 	 already.  So we have no choice but print the information
   5354  1.10  christos 	 here.  */
   5355   1.6  christos 
   5356   1.6  christos       SWITCH_THRU_ALL_UIS ()
   5357   1.7  christos 	{
   5358   1.7  christos 	  struct ui_out *uiout = current_uiout;
   5359   1.7  christos 
   5360   1.9  christos 	  if (uiout->is_mi_like_p ())
   5361   1.9  christos 	    uiout->field_string
   5362   1.9  christos 	      ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
   5363   1.9  christos 	  uiout->message ("\nWatchpoint %pF deleted because the program has "
   5364   1.6  christos 			  "left the block in\n"
   5365   1.1  christos 			  "which its expression is valid.\n",
   5366   1.1  christos 			  signed_field ("wpnum", b->number));
   5367   1.8  christos 	}
   5368   1.1  christos 
   5369   1.1  christos       /* Make sure the watchpoint's commands aren't executed.  */
   5370   1.1  christos       b->commands = NULL;
   5371   1.1  christos       watchpoint_del_at_next_stop (b);
   5372   1.1  christos 
   5373   1.1  christos       return WP_DELETED;
   5374   1.1  christos     }
   5375   1.1  christos }
   5376   1.1  christos 
   5377   1.1  christos /* Return true if it looks like target has stopped due to hitting
   5378  1.10  christos    breakpoint location BL.  This function does not check if we should
   5379   1.1  christos    stop, only if BL explains the stop.  */
   5380   1.8  christos 
   5381  1.10  christos static bool
   5382   1.1  christos bpstat_check_location (const struct bp_location *bl,
   5383   1.1  christos 		       const address_space *aspace, CORE_ADDR bp_addr,
   5384   1.1  christos 		       const target_waitstatus &ws)
   5385   1.1  christos {
   5386   1.1  christos   struct breakpoint *b = bl->owner;
   5387   1.1  christos 
   5388  1.10  christos   /* BL is from an existing breakpoint.  */
   5389   1.1  christos   gdb_assert (b != NULL);
   5390   1.1  christos 
   5391   1.1  christos   return b->breakpoint_hit (bl, aspace, bp_addr, ws);
   5392  1.11  christos }
   5393   1.1  christos 
   5394   1.1  christos /* Determine if the watched values have actually changed, and we
   5395  1.10  christos    should stop.  If not, set BS->stop to false.  */
   5396   1.1  christos 
   5397   1.1  christos static void
   5398   1.1  christos bpstat_check_watchpoint (bpstat *bs)
   5399   1.1  christos {
   5400  1.10  christos   const struct bp_location *bl;
   5401   1.1  christos 
   5402  1.11  christos   /* BS is built for existing struct breakpoint.  */
   5403  1.11  christos   bl = bs->bp_location_at.get ();
   5404  1.11  christos   gdb_assert (bl != NULL);
   5405  1.11  christos   watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
   5406  1.11  christos 
   5407  1.11  christos   bool must_check_value = false;
   5408  1.11  christos 
   5409  1.11  christos   if (b->type == bp_watchpoint)
   5410  1.11  christos     /* For a software watchpoint, we must always check the
   5411  1.11  christos        watched value.  */
   5412  1.11  christos     must_check_value = true;
   5413  1.11  christos   else if (b->watchpoint_triggered == watch_triggered_yes)
   5414  1.11  christos     /* We have a hardware watchpoint (read, write, or access)
   5415  1.11  christos        and the target earlier reported an address watched by
   5416  1.11  christos        this watchpoint.  */
   5417  1.11  christos     must_check_value = true;
   5418  1.11  christos   else if (b->watchpoint_triggered == watch_triggered_unknown
   5419  1.11  christos 	   && b->type == bp_hardware_watchpoint)
   5420  1.11  christos     /* We were stopped by a hardware watchpoint, but the target could
   5421  1.11  christos        not report the data address.  We must check the watchpoint's
   5422   1.1  christos        value.  Access and read watchpoints are out of luck; without
   5423  1.11  christos        a data address, we can't figure it out.  */
   5424   1.1  christos     must_check_value = true;
   5425  1.11  christos 
   5426  1.10  christos   if (must_check_value)
   5427  1.11  christos     {
   5428  1.11  christos       wp_check_result e;
   5429  1.11  christos 
   5430  1.11  christos       try
   5431  1.11  christos 	{
   5432   1.1  christos 	  e = watchpoint_check (bs);
   5433  1.11  christos 	}
   5434  1.11  christos       catch (const gdb_exception_error &ex)
   5435  1.11  christos 	{
   5436  1.11  christos 	  exception_fprintf (gdb_stderr, ex,
   5437   1.8  christos 			     "Error evaluating expression "
   5438  1.11  christos 			     "for watchpoint %d\n",
   5439   1.8  christos 			     b->number);
   5440  1.11  christos 
   5441  1.11  christos 	  SWITCH_THRU_ALL_UIS ()
   5442   1.8  christos 	    {
   5443  1.11  christos 	      gdb_printf (_("Watchpoint %d deleted.\n"),
   5444  1.11  christos 			  b->number);
   5445  1.11  christos 	    }
   5446  1.11  christos 	  watchpoint_del_at_next_stop (b);
   5447  1.11  christos 	  e = WP_DELETED;
   5448  1.11  christos 	}
   5449  1.11  christos 
   5450  1.11  christos       switch (e)
   5451  1.11  christos 	{
   5452  1.11  christos 	case WP_DELETED:
   5453  1.11  christos 	  /* We've already printed what needs to be printed.  */
   5454  1.11  christos 	  bs->print_it = print_it_done;
   5455  1.11  christos 	  /* Stop.  */
   5456  1.11  christos 	  break;
   5457  1.11  christos 	case WP_IGNORE:
   5458  1.11  christos 	  bs->print_it = print_it_noop;
   5459  1.11  christos 	  bs->stop = false;
   5460   1.8  christos 	  break;
   5461  1.11  christos 	case WP_VALUE_CHANGED:
   5462   1.8  christos 	  if (b->type == bp_read_watchpoint)
   5463  1.11  christos 	    {
   5464  1.11  christos 	      /* There are two cases to consider here:
   5465  1.11  christos 
   5466  1.11  christos 		 1. We're watching the triggered memory for reads.
   5467  1.11  christos 		 In that case, trust the target, and always report
   5468  1.11  christos 		 the watchpoint hit to the user.  Even though
   5469  1.11  christos 		 reads don't cause value changes, the value may
   5470  1.11  christos 		 have changed since the last time it was read, and
   5471  1.11  christos 		 since we're not trapping writes, we will not see
   5472  1.11  christos 		 those, and as such we should ignore our notion of
   5473  1.11  christos 		 old value.
   5474  1.11  christos 
   5475  1.11  christos 		 2. We're watching the triggered memory for both
   5476  1.11  christos 		 reads and writes.  There are two ways this may
   5477  1.11  christos 		 happen:
   5478  1.11  christos 
   5479  1.11  christos 		 2.1. This is a target that can't break on data
   5480  1.11  christos 		 reads only, but can break on accesses (reads or
   5481  1.11  christos 		 writes), such as e.g., x86.  We detect this case
   5482  1.11  christos 		 at the time we try to insert read watchpoints.
   5483  1.11  christos 
   5484  1.11  christos 		 2.2. Otherwise, the target supports read
   5485  1.11  christos 		 watchpoints, but, the user set an access or write
   5486  1.11  christos 		 watchpoint watching the same memory as this read
   5487  1.11  christos 		 watchpoint.
   5488  1.11  christos 
   5489  1.11  christos 		 If we're watching memory writes as well as reads,
   5490  1.11  christos 		 ignore watchpoint hits when we find that the
   5491  1.11  christos 		 value hasn't changed, as reads don't cause
   5492  1.11  christos 		 changes.  This still gives false positives when
   5493  1.11  christos 		 the program writes the same value to memory as
   5494  1.11  christos 		 what there was already in memory (we will confuse
   5495  1.11  christos 		 it for a read), but it's much better than
   5496   1.8  christos 		 nothing.  */
   5497  1.11  christos 
   5498   1.1  christos 	      int other_write_watchpoint = 0;
   5499  1.11  christos 
   5500  1.11  christos 	      if (bl->watchpoint_type == hw_read)
   5501  1.11  christos 		{
   5502  1.11  christos 		  for (breakpoint &other_b : all_breakpoints ())
   5503  1.11  christos 		    if (other_b.type == bp_hardware_watchpoint
   5504  1.11  christos 			|| other_b.type == bp_access_watchpoint)
   5505   1.1  christos 		      {
   5506  1.11  christos 			watchpoint &other_w =
   5507  1.11  christos 			  gdb::checked_static_cast<watchpoint &> (other_b);
   5508   1.1  christos 
   5509  1.11  christos 			if (other_w.watchpoint_triggered
   5510  1.11  christos 			    == watch_triggered_yes)
   5511   1.1  christos 			  {
   5512  1.11  christos 			    other_write_watchpoint = 1;
   5513  1.11  christos 			    break;
   5514   1.1  christos 			  }
   5515  1.11  christos 		      }
   5516  1.11  christos 		}
   5517   1.1  christos 
   5518  1.11  christos 	      if (other_write_watchpoint
   5519  1.11  christos 		  || bl->watchpoint_type == hw_access)
   5520  1.11  christos 		{
   5521  1.11  christos 		  /* We're watching the same memory for writes,
   5522   1.1  christos 		     and the value changed since the last time we
   5523  1.11  christos 		     updated it, so this trap must be for a write.
   5524   1.1  christos 		     Ignore it.  */
   5525   1.1  christos 		  bs->print_it = print_it_noop;
   5526  1.11  christos 		  bs->stop = false;
   5527  1.11  christos 		}
   5528  1.11  christos 	    }
   5529  1.11  christos 	  break;
   5530  1.11  christos 	case WP_VALUE_NOT_CHANGED:
   5531  1.11  christos 	  if (b->type == bp_hardware_watchpoint
   5532  1.11  christos 	      || b->type == bp_watchpoint)
   5533  1.11  christos 	    {
   5534  1.11  christos 	      /* Don't stop: write watchpoints shouldn't fire if
   5535  1.11  christos 		 the value hasn't changed.  */
   5536  1.11  christos 	      bs->print_it = print_it_noop;
   5537  1.11  christos 	      bs->stop = false;
   5538  1.11  christos 	    }
   5539  1.11  christos 	  /* Stop.  */
   5540  1.11  christos 	  break;
   5541   1.1  christos 	default:
   5542  1.11  christos 	  /* Can't happen.  */
   5543  1.11  christos 	  break;
   5544  1.11  christos 	}
   5545  1.11  christos     }
   5546  1.11  christos   else	/* !must_check_value */
   5547  1.11  christos     {
   5548  1.11  christos       /* This is a case where some watchpoint(s) triggered, but
   5549  1.11  christos 	 not at the address of this watchpoint, or else no
   5550  1.11  christos 	 watchpoint triggered after all.  So don't print
   5551   1.1  christos 	 anything for this watchpoint.  */
   5552   1.1  christos       bs->print_it = print_it_noop;
   5553   1.1  christos       bs->stop = false;
   5554   1.1  christos     }
   5555   1.1  christos }
   5556   1.1  christos 
   5557   1.1  christos /* For breakpoints that are currently marked as telling gdb to stop,
   5558   1.1  christos    check conditions (condition proper, frame, thread and ignore count)
   5559   1.1  christos    of breakpoint referred to by BS.  If we should not stop for this
   5560  1.10  christos    breakpoint, set BS->stop to 0.  */
   5561   1.1  christos 
   5562  1.10  christos static void
   5563  1.10  christos bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
   5564   1.1  christos {
   5565   1.1  christos   INFRUN_SCOPED_DEBUG_ENTER_EXIT;
   5566   1.8  christos 
   5567   1.8  christos   const struct bp_location *bl;
   5568   1.1  christos   struct breakpoint *b;
   5569   1.1  christos   /* Assume stop.  */
   5570   1.1  christos   bool condition_result = true;
   5571   1.1  christos   struct expression *cond;
   5572   1.1  christos 
   5573  1.10  christos   gdb_assert (bs->stop);
   5574   1.1  christos 
   5575   1.1  christos   /* BS is built for existing struct breakpoint.  */
   5576   1.1  christos   bl = bs->bp_location_at.get ();
   5577   1.1  christos   gdb_assert (bl != NULL);
   5578  1.10  christos   b = bs->breakpoint_at;
   5579  1.10  christos   gdb_assert (b != NULL);
   5580  1.10  christos 
   5581  1.10  christos   infrun_debug_printf ("thread = %s, breakpoint %d.%d",
   5582   1.1  christos 		       thread->ptid.to_string ().c_str (),
   5583   1.1  christos 		       b->number, find_loc_num_by_location (bl));
   5584   1.1  christos 
   5585   1.1  christos   /* Even if the target evaluated the condition on its end and notified GDB, we
   5586   1.1  christos      need to do so again since GDB does not know if we stopped due to a
   5587  1.10  christos      breakpoint or a single step breakpoint.  */
   5588   1.1  christos 
   5589  1.10  christos   if (frame_id_p (b->frame_id)
   5590  1.10  christos       && b->frame_id != get_stack_frame_id (get_current_frame ()))
   5591  1.10  christos     {
   5592  1.11  christos       infrun_debug_printf ("incorrect frame %s not %s, not stopping",
   5593   1.1  christos 			   get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
   5594   1.1  christos 			   b->frame_id.to_string ().c_str ());
   5595   1.1  christos       bs->stop = false;
   5596   1.3  christos       return;
   5597   1.3  christos     }
   5598   1.3  christos 
   5599   1.8  christos   /* If this is a thread/task-specific breakpoint, don't waste cpu
   5600  1.11  christos      evaluating the condition if this isn't the specified
   5601  1.11  christos      thread/task.  */
   5602   1.1  christos   if ((b->thread != -1 && b->thread != thread->global_num)
   5603  1.10  christos       || (b->inferior != -1 && b->inferior != thread->inf->num)
   5604  1.11  christos       || (b->task != -1 && b->task != ada_get_task_number (thread)))
   5605   1.1  christos     {
   5606   1.1  christos       infrun_debug_printf ("incorrect thread or task, not stopping");
   5607   1.1  christos       bs->stop = false;
   5608   1.3  christos       return;
   5609   1.3  christos     }
   5610   1.3  christos 
   5611   1.1  christos   /* Evaluate extension language breakpoints that have a "stop" method
   5612   1.1  christos      implemented.  */
   5613   1.1  christos   bs->stop = breakpoint_ext_lang_cond_says_stop (b);
   5614  1.11  christos 
   5615   1.1  christos   if (is_watchpoint (b))
   5616   1.7  christos     {
   5617   1.1  christos       watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
   5618   1.1  christos 
   5619   1.7  christos       cond = w->cond_exp.get ();
   5620   1.1  christos     }
   5621  1.10  christos   else
   5622   1.1  christos     cond = bl->cond.get ();
   5623  1.10  christos 
   5624   1.1  christos   if (cond != nullptr && b->disposition != disp_del_at_next_stop)
   5625  1.10  christos     {
   5626  1.10  christos       bool within_current_scope = true;
   5627  1.10  christos 
   5628  1.10  christos       /* We use scoped_value_mark because it could be a long time
   5629  1.10  christos 	 before we return to the command level and call
   5630   1.1  christos 	 free_all_values.  We can't call free_all_values because we
   5631  1.11  christos 	 might be in the middle of evaluating a function call.  */
   5632   1.1  christos       scoped_value_mark mark;
   5633  1.11  christos 
   5634   1.1  christos       watchpoint *w = nullptr;
   5635   1.1  christos       if (is_watchpoint (b))
   5636   1.1  christos 	w = gdb::checked_static_cast<watchpoint *> (b);
   5637   1.1  christos 
   5638   1.1  christos       /* Need to select the frame, with all that implies so that
   5639   1.1  christos 	 the conditions will have the right context.  Because we
   5640   1.1  christos 	 use the frame, we will not see an inlined function's
   5641   1.1  christos 	 variables when we arrive at a breakpoint at the start
   5642   1.1  christos 	 of the inlined function; the current frame will be the
   5643   1.1  christos 	 call site.  */
   5644   1.1  christos       if (w == NULL || w->cond_exp_valid_block == NULL)
   5645  1.10  christos 	select_frame (get_current_frame ());
   5646   1.1  christos       else
   5647   1.1  christos 	{
   5648   1.1  christos 	  frame_info_ptr frame;
   5649   1.1  christos 
   5650   1.1  christos 	  /* For local watchpoint expressions, which particular
   5651   1.1  christos 	     instance of a local is being watched matters, so we
   5652   1.1  christos 	     keep track of the frame to evaluate the expression
   5653   1.1  christos 	     in.  To evaluate the condition however, it doesn't
   5654   1.1  christos 	     really matter which instantiation of the function
   5655   1.1  christos 	     where the condition makes sense triggers the
   5656   1.1  christos 	     watchpoint.  This allows an expression like "watch
   5657   1.1  christos 	     global if q > 10" set in `func', catch writes to
   5658   1.1  christos 	     global on all threads that call `func', or catch
   5659   1.1  christos 	     writes on all recursive calls of `func' by a single
   5660   1.1  christos 	     thread.  We simply always evaluate the condition in
   5661   1.1  christos 	     the innermost frame that's executing where it makes
   5662   1.1  christos 	     sense to evaluate the condition.  It seems
   5663   1.1  christos 	     intuitive.  */
   5664   1.1  christos 	  frame = block_innermost_frame (w->cond_exp_valid_block);
   5665  1.10  christos 	  if (frame != NULL)
   5666   1.1  christos 	    select_frame (frame);
   5667   1.1  christos 	  else
   5668   1.8  christos 	    within_current_scope = false;
   5669   1.9  christos 	}
   5670   1.8  christos       if (within_current_scope)
   5671  1.11  christos 	{
   5672  1.11  christos 	  try
   5673   1.8  christos 	    {
   5674   1.8  christos 	      scoped_restore reset_in_cond_eval
   5675  1.11  christos 		= make_scoped_restore (&thread->control.in_cond_eval, true);
   5676   1.8  christos 	      condition_result = breakpoint_cond_eval (cond);
   5677  1.11  christos 	    }
   5678  1.11  christos 	  catch (const gdb_exception_error &ex)
   5679  1.11  christos 	    {
   5680  1.11  christos 	      int locno = bpstat_locno (bs);
   5681  1.11  christos 	      if (locno != 0)
   5682  1.11  christos 		exception_fprintf
   5683  1.11  christos 		  (gdb_stderr, ex,
   5684  1.11  christos 		   "Error in testing condition for breakpoint %d.%d:\n",
   5685  1.11  christos 		   b->number, locno);
   5686  1.11  christos 	      else
   5687  1.11  christos 		exception_fprintf
   5688   1.8  christos 		  (gdb_stderr, ex,
   5689   1.8  christos 		   "Error in testing condition for breakpoint %d:\n",
   5690   1.1  christos 		   b->number);
   5691   1.1  christos 	    }
   5692   1.1  christos 	}
   5693   1.1  christos       else
   5694   1.1  christos 	{
   5695   1.1  christos 	  warning (_("Watchpoint condition cannot be tested "
   5696   1.1  christos 		     "in the current scope"));
   5697   1.1  christos 	  /* If we failed to set the right context for this
   5698   1.1  christos 	     watchpoint, unconditionally report it.  */
   5699   1.1  christos 	}
   5700  1.10  christos       /* FIXME-someday, should give breakpoint #.  */
   5701   1.1  christos     }
   5702  1.10  christos 
   5703  1.11  christos   if (cond != nullptr && !condition_result)
   5704  1.10  christos     {
   5705   1.1  christos       infrun_debug_printf ("condition_result = false, not stopping");
   5706   1.1  christos       bs->stop = false;
   5707   1.1  christos       return;
   5708  1.10  christos     }
   5709  1.10  christos   else if (b->ignore_count > 0)
   5710   1.1  christos     {
   5711  1.11  christos       infrun_debug_printf ("ignore count %d, not stopping",
   5712   1.1  christos 			   b->ignore_count);
   5713   1.1  christos       b->ignore_count--;
   5714  1.11  christos       bs->stop = false;
   5715  1.10  christos       /* Increase the hit count even though we don't stop.  */
   5716  1.10  christos       ++(b->hit_count);
   5717  1.10  christos       notify_breakpoint_modified (b);
   5718  1.10  christos       return;
   5719  1.10  christos     }
   5720  1.10  christos 
   5721  1.10  christos   if (bs->stop)
   5722   1.1  christos     infrun_debug_printf ("stopping at this breakpoint");
   5723   1.1  christos   else
   5724   1.5  christos     infrun_debug_printf ("not stopping at this breakpoint");
   5725   1.5  christos }
   5726   1.5  christos 
   5727  1.10  christos /* Returns true if we need to track moribund locations of LOC's type
   5728  1.10  christos    on the current target.  */
   5729   1.5  christos 
   5730   1.5  christos static bool
   5731   1.5  christos need_moribund_for_location_type (const struct bp_location *loc)
   5732   1.5  christos {
   5733   1.5  christos   return ((loc->loc_type == bp_loc_software_breakpoint
   5734   1.5  christos 	   && !target_supports_stopped_by_sw_breakpoint ())
   5735   1.5  christos 	  || (loc->loc_type == bp_loc_hardware_breakpoint
   5736   1.8  christos 	      && !target_supports_stopped_by_hw_breakpoint ()));
   5737   1.1  christos }
   5738  1.10  christos 
   5739   1.8  christos /* See breakpoint.h.  */
   5740  1.10  christos 
   5741   1.1  christos bpstat *
   5742  1.10  christos build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
   5743   1.1  christos 		    const target_waitstatus &ws)
   5744  1.11  christos {
   5745   1.1  christos   bpstat *bs_head = nullptr, **bs_link = &bs_head;
   5746  1.11  christos 
   5747   1.1  christos   for (breakpoint &b : all_breakpoints ())
   5748   1.1  christos     {
   5749  1.11  christos       if (!breakpoint_enabled (&b))
   5750   1.1  christos 	continue;
   5751   1.1  christos 
   5752   1.1  christos       for (bp_location &bl : b.locations ())
   5753   1.1  christos 	{
   5754   1.1  christos 	  /* For hardware watchpoints, we look only at the first
   5755   1.1  christos 	     location.  The watchpoint_check function will work on the
   5756  1.11  christos 	     entire expression, not the individual locations.  For
   5757   1.1  christos 	     read watchpoints, the watchpoints_triggered function has
   5758   1.1  christos 	     checked all locations already.  */
   5759  1.11  christos 	  if (b.type == bp_hardware_watchpoint && &bl != &b.first_loc ())
   5760   1.1  christos 	    break;
   5761   1.1  christos 
   5762  1.11  christos 	  if (!bl.enabled || bl.disabled_by_cond || bl.shlib_disabled)
   5763   1.1  christos 	    continue;
   5764   1.1  christos 
   5765   1.1  christos 	  if (!bpstat_check_location (&bl, aspace, bp_addr, ws))
   5766   1.1  christos 	    continue;
   5767   1.1  christos 
   5768  1.11  christos 	  /* Come here if it's a watchpoint, or if the break address
   5769   1.8  christos 	     matches.  */
   5770   1.1  christos 
   5771   1.1  christos 	  bpstat *bs = new bpstat (&bl, &bs_link);	/* Alloc a bpstat to
   5772   1.1  christos 							   explain stop.  */
   5773   1.1  christos 
   5774  1.11  christos 	  /* Assume we stop.  Should we find a watchpoint that is not
   5775  1.11  christos 	     actually triggered, or if the condition of the breakpoint
   5776   1.1  christos 	     evaluates as false, we'll reset 'stop' to 0.  */
   5777   1.1  christos 	  bs->stop = true;
   5778   1.1  christos 	  bs->print = true;
   5779   1.1  christos 
   5780   1.1  christos 	  /* If this is a scope breakpoint, mark the associated
   5781  1.11  christos 	     watchpoint as triggered so that we will handle the
   5782   1.1  christos 	     out-of-scope event.  We'll get to the watchpoint next
   5783  1.11  christos 	     iteration.  */
   5784  1.11  christos 	  if (b.type == bp_watchpoint_scope && b.related_breakpoint != &b)
   5785   1.1  christos 	    {
   5786   1.1  christos 	      watchpoint *w
   5787   1.1  christos 		= gdb::checked_static_cast<watchpoint *> (b.related_breakpoint);
   5788   1.1  christos 
   5789   1.1  christos 	      w->watchpoint_triggered = watch_triggered_yes;
   5790   1.1  christos 	    }
   5791   1.3  christos 	}
   5792   1.5  christos     }
   5793   1.5  christos 
   5794   1.1  christos   /* Check if a moribund breakpoint explains the stop.  */
   5795   1.8  christos   if (!target_supports_stopped_by_sw_breakpoint ()
   5796   1.1  christos       || !target_supports_stopped_by_hw_breakpoint ())
   5797   1.5  christos     {
   5798   1.5  christos       for (bp_location *loc : moribund_locations)
   5799   1.5  christos 	{
   5800  1.10  christos 	  if (breakpoint_location_address_match (loc, aspace, bp_addr)
   5801   1.5  christos 	      && need_moribund_for_location_type (loc))
   5802  1.11  christos 	    {
   5803  1.11  christos 	      bpstat *bs = new bpstat (loc, &bs_link);
   5804   1.5  christos 	      /* For hits of moribund locations, we should just proceed.  */
   5805   1.5  christos 	      bs->stop = false;
   5806   1.1  christos 	      bs->print = false;
   5807   1.1  christos 	      bs->print_it = print_it_noop;
   5808   1.1  christos 	    }
   5809   1.8  christos 	}
   5810   1.8  christos     }
   5811   1.8  christos 
   5812   1.8  christos   return bs_head;
   5813   1.8  christos }
   5814  1.10  christos 
   5815   1.8  christos /* See breakpoint.h.  */
   5816   1.8  christos 
   5817  1.10  christos bpstat *
   5818  1.10  christos bpstat_stop_status (const address_space *aspace,
   5819   1.8  christos 		    CORE_ADDR bp_addr, thread_info *thread,
   5820   1.8  christos 		    const target_waitstatus &ws,
   5821   1.8  christos 		    bpstat *stop_chain)
   5822  1.10  christos {
   5823  1.10  christos   struct breakpoint *b = NULL;
   5824   1.8  christos   /* First item of allocated bpstat's.  */
   5825   1.8  christos   bpstat *bs_head = stop_chain;
   5826   1.8  christos   bpstat *bs;
   5827   1.8  christos   int need_remove_insert;
   5828   1.8  christos   int removed_any;
   5829   1.8  christos 
   5830   1.8  christos   /* First, build the bpstat chain with locations that explain a
   5831   1.8  christos      target stop, while being careful to not set the target running,
   5832   1.8  christos      as that may invalidate locations (in particular watchpoint
   5833   1.8  christos      locations are recreated).  Resuming will happen here with
   5834   1.8  christos      breakpoint conditions or watchpoint expressions that include
   5835   1.8  christos      inferior function calls.  */
   5836   1.1  christos   if (bs_head == NULL)
   5837   1.1  christos     bs_head = build_bpstat_chain (aspace, bp_addr, ws);
   5838   1.1  christos 
   5839   1.1  christos   /* A bit of special processing for shlib breakpoints.  We need to
   5840   1.1  christos      process solib loading here, so that the lists of loaded and
   5841   1.1  christos      unloaded libraries are correct before we handle "catch load" and
   5842   1.1  christos      "catch unload".  */
   5843   1.1  christos   for (bs = bs_head; bs != NULL; bs = bs->next)
   5844   1.1  christos     {
   5845   1.1  christos       if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
   5846   1.1  christos 	{
   5847   1.1  christos 	  handle_solib_event ();
   5848   1.1  christos 	  break;
   5849   1.1  christos 	}
   5850   1.1  christos     }
   5851   1.1  christos 
   5852   1.1  christos   /* Now go through the locations that caused the target to stop, and
   5853   1.1  christos      check whether we're interested in reporting this stop to higher
   5854   1.1  christos      layers, or whether we should resume the target transparently.  */
   5855   1.1  christos 
   5856   1.1  christos   removed_any = 0;
   5857   1.1  christos 
   5858   1.1  christos   for (bs = bs_head; bs != NULL; bs = bs->next)
   5859   1.1  christos     {
   5860   1.1  christos       if (!bs->stop)
   5861  1.10  christos 	continue;
   5862   1.1  christos 
   5863   1.1  christos       b = bs->breakpoint_at;
   5864   1.8  christos       b->check_status (bs);
   5865   1.1  christos       if (bs->stop)
   5866   1.1  christos 	{
   5867   1.1  christos 	  bpstat_check_breakpoint_conditions (bs, thread);
   5868   1.1  christos 
   5869   1.1  christos 	  if (bs->stop)
   5870   1.1  christos 	    {
   5871   1.1  christos 	      ++(b->hit_count);
   5872   1.1  christos 
   5873   1.1  christos 	      /* We will stop here.  */
   5874   1.3  christos 	      if (b->disposition == disp_disable)
   5875   1.1  christos 		{
   5876   1.1  christos 		  --(b->enable_count);
   5877   1.1  christos 		  if (b->enable_count <= 0)
   5878  1.11  christos 		    b->enable_state = bp_disabled;
   5879   1.1  christos 		  removed_any = 1;
   5880  1.11  christos 		}
   5881   1.1  christos 	      notify_breakpoint_modified (b);
   5882   1.1  christos 	      if (b->silent)
   5883   1.8  christos 		bs->print = false;
   5884  1.11  christos 	      bs->commands = b->commands;
   5885   1.1  christos 	      if (command_line_is_silent (bs->commands
   5886  1.10  christos 					  ? bs->commands.get () : NULL))
   5887   1.1  christos 		bs->print = false;
   5888   1.1  christos 
   5889   1.1  christos 	      b->after_condition_true (bs);
   5890   1.1  christos 	    }
   5891   1.1  christos 
   5892   1.1  christos 	}
   5893   1.1  christos 
   5894   1.1  christos       /* Print nothing for this entry if we don't stop or don't
   5895   1.1  christos 	 print.  */
   5896   1.1  christos       if (!bs->stop || !bs->print)
   5897   1.1  christos 	bs->print_it = print_it_noop;
   5898   1.1  christos     }
   5899   1.1  christos 
   5900   1.1  christos   /* If we aren't stopping, the value of some hardware watchpoint may
   5901   1.1  christos      not have changed, but the intermediate memory locations we are
   5902   1.1  christos      watching may have.  Don't bother if we're stopping; this will get
   5903   1.1  christos      done later.  */
   5904   1.1  christos   need_remove_insert = 0;
   5905   1.1  christos   if (! bpstat_causes_stop (bs_head))
   5906   1.1  christos     for (bs = bs_head; bs != NULL; bs = bs->next)
   5907   1.1  christos       if (!bs->stop
   5908  1.11  christos 	  && bs->breakpoint_at
   5909  1.11  christos 	  && is_hardware_watchpoint (bs->breakpoint_at))
   5910   1.1  christos 	{
   5911  1.10  christos 	  watchpoint *w
   5912   1.1  christos 	    = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
   5913   1.1  christos 
   5914   1.1  christos 	  update_watchpoint (w, false /* don't reparse.  */);
   5915   1.1  christos 	  need_remove_insert = 1;
   5916   1.3  christos 	}
   5917   1.1  christos 
   5918   1.3  christos   if (need_remove_insert)
   5919   1.1  christos     update_global_location_list (UGLL_MAY_INSERT);
   5920   1.1  christos   else if (removed_any)
   5921   1.1  christos     update_global_location_list (UGLL_DONT_INSERT);
   5922   1.1  christos 
   5923  1.10  christos   return bs_head;
   5924  1.10  christos }
   5925  1.10  christos 
   5926  1.10  christos /* See breakpoint.h.  */
   5927  1.10  christos 
   5928  1.10  christos bpstat *
   5929  1.10  christos bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
   5930  1.10  christos 			    thread_info *thread, const target_waitstatus &ws)
   5931  1.10  christos {
   5932  1.10  christos   gdb_assert (!target_stopped_by_watchpoint ());
   5933  1.10  christos 
   5934  1.10  christos   /* Clear all watchpoints' 'watchpoint_triggered' value from a
   5935  1.10  christos      previous stop to avoid confusing bpstat_stop_status.  */
   5936  1.10  christos   watchpoints_triggered (ws);
   5937  1.10  christos 
   5938   1.1  christos   return bpstat_stop_status (aspace, bp_addr, thread, ws);
   5939  1.10  christos }
   5940   1.1  christos 
   5941   1.1  christos static void
   5942   1.1  christos handle_jit_event (CORE_ADDR address)
   5943   1.9  christos {
   5944   1.6  christos   struct gdbarch *gdbarch;
   5945   1.1  christos 
   5946   1.1  christos   infrun_debug_printf ("handling bp_jit_event");
   5947   1.8  christos 
   5948   1.1  christos   /* Switch terminal for any messages produced by
   5949  1.10  christos      breakpoint_re_set.  */
   5950  1.10  christos   target_terminal::ours_for_output ();
   5951  1.10  christos 
   5952  1.10  christos   gdbarch = get_frame_arch (get_current_frame ());
   5953  1.10  christos   /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
   5954  1.10  christos      thus it is expected that its objectfile can be found through
   5955  1.10  christos      minimal symbol lookup.  If it doesn't work (and assert fails), it
   5956  1.10  christos      most likely means that `jit_breakpoint_re_set` was changes and this
   5957  1.10  christos      function needs to be updated too.  */
   5958  1.10  christos   bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
   5959  1.10  christos   gdb_assert (jit_bp_sym.objfile != nullptr);
   5960  1.10  christos   objfile *objfile = jit_bp_sym.objfile;
   5961   1.1  christos   if (objfile->separate_debug_objfile_backlink)
   5962   1.8  christos     objfile = objfile->separate_debug_objfile_backlink;
   5963   1.1  christos   jit_event_handler (gdbarch, objfile);
   5964   1.1  christos 
   5965   1.1  christos   target_terminal::inferior ();
   5966   1.1  christos }
   5967   1.1  christos 
   5968   1.1  christos /* Prepare WHAT final decision for infrun.  */
   5969   1.1  christos 
   5970  1.10  christos /* Decide what infrun needs to do with this bpstat.  */
   5971   1.1  christos 
   5972   1.1  christos struct bpstat_what
   5973  1.10  christos bpstat_what (bpstat *bs_head)
   5974   1.1  christos {
   5975   1.1  christos   struct bpstat_what retval;
   5976   1.1  christos   bpstat *bs;
   5977   1.9  christos 
   5978   1.1  christos   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
   5979   1.1  christos   retval.call_dummy = STOP_NONE;
   5980   1.1  christos   retval.is_longjmp = false;
   5981   1.1  christos 
   5982   1.1  christos   for (bs = bs_head; bs != NULL; bs = bs->next)
   5983   1.1  christos     {
   5984   1.1  christos       /* Extract this BS's action.  After processing each BS, we check
   5985   1.1  christos 	 if its action overrides all we've seem so far.  */
   5986   1.1  christos       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
   5987   1.1  christos       enum bptype bptype;
   5988   1.1  christos 
   5989   1.1  christos       if (bs->breakpoint_at == NULL)
   5990   1.1  christos 	{
   5991   1.1  christos 	  /* I suspect this can happen if it was a momentary
   5992   1.1  christos 	     breakpoint which has since been deleted.  */
   5993   1.1  christos 	  bptype = bp_none;
   5994   1.1  christos 	}
   5995   1.1  christos       else
   5996   1.1  christos 	bptype = bs->breakpoint_at->type;
   5997   1.1  christos 
   5998   1.1  christos       switch (bptype)
   5999   1.1  christos 	{
   6000   1.1  christos 	case bp_none:
   6001   1.3  christos 	  break;
   6002   1.1  christos 	case bp_breakpoint:
   6003   1.1  christos 	case bp_hardware_breakpoint:
   6004   1.1  christos 	case bp_single_step:
   6005   1.1  christos 	case bp_until:
   6006   1.1  christos 	case bp_finish:
   6007   1.1  christos 	case bp_shlib_event:
   6008   1.1  christos 	  if (bs->stop)
   6009   1.1  christos 	    {
   6010   1.1  christos 	      if (bs->print)
   6011   1.1  christos 		this_action = BPSTAT_WHAT_STOP_NOISY;
   6012   1.1  christos 	      else
   6013   1.1  christos 		this_action = BPSTAT_WHAT_STOP_SILENT;
   6014   1.1  christos 	    }
   6015   1.1  christos 	  else
   6016   1.1  christos 	    this_action = BPSTAT_WHAT_SINGLE;
   6017   1.1  christos 	  break;
   6018   1.1  christos 	case bp_watchpoint:
   6019   1.1  christos 	case bp_hardware_watchpoint:
   6020   1.1  christos 	case bp_read_watchpoint:
   6021   1.1  christos 	case bp_access_watchpoint:
   6022   1.1  christos 	  if (bs->stop)
   6023   1.1  christos 	    {
   6024   1.1  christos 	      if (bs->print)
   6025   1.1  christos 		this_action = BPSTAT_WHAT_STOP_NOISY;
   6026   1.1  christos 	      else
   6027   1.1  christos 		this_action = BPSTAT_WHAT_STOP_SILENT;
   6028   1.1  christos 	    }
   6029   1.1  christos 	  else
   6030   1.1  christos 	    {
   6031   1.1  christos 	      /* There was a watchpoint, but we're not stopping.
   6032   1.1  christos 		 This requires no further action.  */
   6033   1.1  christos 	    }
   6034   1.1  christos 	  break;
   6035   1.5  christos 	case bp_longjmp:
   6036   1.5  christos 	case bp_longjmp_call_dummy:
   6037   1.5  christos 	case bp_exception:
   6038   1.5  christos 	  if (bs->stop)
   6039   1.5  christos 	    {
   6040   1.5  christos 	      this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
   6041   1.5  christos 	      retval.is_longjmp = bptype != bp_exception;
   6042   1.1  christos 	    }
   6043   1.1  christos 	  else
   6044   1.1  christos 	    this_action = BPSTAT_WHAT_SINGLE;
   6045   1.5  christos 	  break;
   6046   1.5  christos 	case bp_longjmp_resume:
   6047   1.5  christos 	case bp_exception_resume:
   6048   1.5  christos 	  if (bs->stop)
   6049   1.5  christos 	    {
   6050   1.5  christos 	      this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
   6051   1.5  christos 	      retval.is_longjmp = bptype == bp_longjmp_resume;
   6052   1.1  christos 	    }
   6053   1.1  christos 	  else
   6054   1.1  christos 	    this_action = BPSTAT_WHAT_SINGLE;
   6055   1.1  christos 	  break;
   6056   1.1  christos 	case bp_step_resume:
   6057   1.1  christos 	  if (bs->stop)
   6058   1.1  christos 	    this_action = BPSTAT_WHAT_STEP_RESUME;
   6059   1.1  christos 	  else
   6060   1.1  christos 	    {
   6061   1.1  christos 	      /* It is for the wrong frame.  */
   6062   1.1  christos 	      this_action = BPSTAT_WHAT_SINGLE;
   6063   1.1  christos 	    }
   6064   1.1  christos 	  break;
   6065   1.1  christos 	case bp_hp_step_resume:
   6066   1.1  christos 	  if (bs->stop)
   6067   1.1  christos 	    this_action = BPSTAT_WHAT_HP_STEP_RESUME;
   6068   1.1  christos 	  else
   6069   1.1  christos 	    {
   6070   1.1  christos 	      /* It is for the wrong frame.  */
   6071   1.1  christos 	      this_action = BPSTAT_WHAT_SINGLE;
   6072   1.1  christos 	    }
   6073   1.1  christos 	  break;
   6074   1.1  christos 	case bp_watchpoint_scope:
   6075   1.1  christos 	case bp_thread_event:
   6076   1.1  christos 	case bp_overlay_event:
   6077   1.1  christos 	case bp_longjmp_master:
   6078   1.1  christos 	case bp_std_terminate_master:
   6079   1.1  christos 	case bp_exception_master:
   6080   1.1  christos 	  this_action = BPSTAT_WHAT_SINGLE;
   6081   1.1  christos 	  break;
   6082   1.1  christos 	case bp_catchpoint:
   6083   1.1  christos 	  if (bs->stop)
   6084   1.1  christos 	    {
   6085   1.1  christos 	      if (bs->print)
   6086   1.1  christos 		this_action = BPSTAT_WHAT_STOP_NOISY;
   6087   1.1  christos 	      else
   6088   1.1  christos 		this_action = BPSTAT_WHAT_STOP_SILENT;
   6089   1.9  christos 	    }
   6090   1.9  christos 	  else
   6091  1.10  christos 	    {
   6092  1.10  christos 	      /* Some catchpoints are implemented with breakpoints.
   6093   1.9  christos 		 For those, we need to step over the breakpoint.  */
   6094   1.1  christos 	      if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
   6095   1.1  christos 		  || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
   6096   1.1  christos 		this_action = BPSTAT_WHAT_SINGLE;
   6097   1.1  christos 	    }
   6098   1.1  christos 	  break;
   6099   1.1  christos 	case bp_jit_event:
   6100   1.1  christos 	  this_action = BPSTAT_WHAT_SINGLE;
   6101   1.1  christos 	  break;
   6102   1.1  christos 	case bp_call_dummy:
   6103   1.1  christos 	  /* Make sure the action is stop (silent or noisy),
   6104   1.1  christos 	     so infrun.c pops the dummy frame.  */
   6105   1.1  christos 	  retval.call_dummy = STOP_STACK_DUMMY;
   6106   1.1  christos 	  this_action = BPSTAT_WHAT_STOP_SILENT;
   6107   1.1  christos 	  break;
   6108   1.1  christos 	case bp_std_terminate:
   6109   1.1  christos 	  /* Make sure the action is stop (silent or noisy),
   6110   1.1  christos 	     so infrun.c pops the dummy frame.  */
   6111   1.1  christos 	  retval.call_dummy = STOP_STD_TERMINATE;
   6112   1.1  christos 	  this_action = BPSTAT_WHAT_STOP_SILENT;
   6113   1.1  christos 	  break;
   6114  1.10  christos 	case bp_tracepoint:
   6115   1.1  christos 	case bp_fast_tracepoint:
   6116   1.1  christos 	case bp_static_tracepoint:
   6117   1.1  christos 	case bp_static_marker_tracepoint:
   6118  1.10  christos 	  /* Tracepoint hits should not be reported back to GDB, and
   6119   1.1  christos 	     if one got through somehow, it should have been filtered
   6120   1.1  christos 	     out already.  */
   6121   1.1  christos 	  internal_error (_("bpstat_what: tracepoint encountered"));
   6122   1.1  christos 	  break;
   6123   1.1  christos 	case bp_gnu_ifunc_resolver:
   6124   1.1  christos 	  /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
   6125   1.1  christos 	  this_action = BPSTAT_WHAT_SINGLE;
   6126   1.1  christos 	  break;
   6127   1.1  christos 	case bp_gnu_ifunc_resolver_return:
   6128   1.1  christos 	  /* The breakpoint will be removed, execution will restart from the
   6129   1.1  christos 	     PC of the former breakpoint.  */
   6130   1.1  christos 	  this_action = BPSTAT_WHAT_KEEP_CHECKING;
   6131   1.1  christos 	  break;
   6132   1.1  christos 
   6133   1.1  christos 	case bp_dprintf:
   6134   1.1  christos 	  if (bs->stop)
   6135   1.1  christos 	    this_action = BPSTAT_WHAT_STOP_SILENT;
   6136   1.1  christos 	  else
   6137   1.1  christos 	    this_action = BPSTAT_WHAT_SINGLE;
   6138  1.10  christos 	  break;
   6139   1.1  christos 
   6140   1.1  christos 	default:
   6141   1.7  christos 	  internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
   6142   1.1  christos 	}
   6143   1.1  christos 
   6144   1.6  christos       retval.main_action = std::max (retval.main_action, this_action);
   6145   1.6  christos     }
   6146   1.1  christos 
   6147   1.6  christos   return retval;
   6148  1.10  christos }
   6149   1.6  christos 
   6150  1.10  christos void
   6151   1.1  christos bpstat_run_callbacks (bpstat *bs_head)
   6152   1.1  christos {
   6153   1.1  christos   bpstat *bs;
   6154   1.1  christos 
   6155   1.1  christos   for (bs = bs_head; bs != NULL; bs = bs->next)
   6156   1.1  christos     {
   6157   1.1  christos       struct breakpoint *b = bs->breakpoint_at;
   6158   1.1  christos 
   6159   1.1  christos       if (b == NULL)
   6160   1.6  christos 	continue;
   6161  1.10  christos       switch (b->type)
   6162   1.6  christos 	{
   6163   1.1  christos 	case bp_jit_event:
   6164  1.11  christos 	  handle_jit_event (bs->bp_location_at->address);
   6165  1.11  christos 	  break;
   6166   1.1  christos 	case bp_gnu_ifunc_resolver:
   6167   1.1  christos 	  gnu_ifunc_resolver_stop
   6168  1.11  christos 	    (gdb::checked_static_cast<code_breakpoint *> (b));
   6169  1.11  christos 	  break;
   6170   1.1  christos 	case bp_gnu_ifunc_resolver_return:
   6171   1.1  christos 	  gnu_ifunc_resolver_return_stop
   6172   1.1  christos 	    (gdb::checked_static_cast<code_breakpoint *> (b));
   6173   1.1  christos 	  break;
   6174   1.1  christos 	}
   6175   1.9  christos     }
   6176   1.1  christos }
   6177   1.9  christos 
   6178   1.9  christos /* See breakpoint.h.  */
   6179   1.1  christos 
   6180  1.11  christos bool
   6181  1.11  christos bpstat_should_step ()
   6182  1.11  christos {
   6183  1.11  christos   for (breakpoint &b : all_breakpoints ())
   6184   1.9  christos     if (breakpoint_enabled (&b)
   6185  1.10  christos 	&& b.type == bp_watchpoint
   6186   1.9  christos 	&& b.has_locations ())
   6187   1.1  christos       return true;
   6188   1.1  christos 
   6189   1.9  christos   return false;
   6190   1.9  christos }
   6191   1.9  christos 
   6192  1.10  christos /* See breakpoint.h.  */
   6193   1.1  christos 
   6194   1.1  christos bool
   6195   1.1  christos bpstat_causes_stop (bpstat *bs)
   6196   1.9  christos {
   6197   1.1  christos   for (; bs != NULL; bs = bs->next)
   6198   1.9  christos     if (bs->stop)
   6199   1.1  christos       return true;
   6200   1.1  christos 
   6201   1.1  christos   return false;
   6202   1.1  christos }
   6203  1.10  christos 
   6204   1.1  christos 
   6205   1.1  christos 
   6207  1.10  christos /* Compute a number of spaces suitable to indent the next line
   6208   1.1  christos    so it starts at the position corresponding to the table column
   6209   1.1  christos    named COL_NAME in the currently active table of UIOUT.  */
   6210   1.1  christos 
   6211   1.7  christos static int
   6212   1.1  christos wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
   6213   1.1  christos {
   6214   1.7  christos   int i, total_width, width, align;
   6215   1.1  christos   const char *text;
   6216   1.1  christos 
   6217  1.10  christos   total_width = 0;
   6218   1.1  christos   for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
   6219   1.1  christos     {
   6220   1.1  christos       if (strcmp (text, col_name) == 0)
   6221   1.1  christos 	return total_width;
   6222  1.10  christos 
   6223   1.1  christos       total_width += width + 1;
   6224   1.1  christos     }
   6225   1.1  christos 
   6226   1.1  christos   return 0;
   6227   1.1  christos }
   6228   1.1  christos 
   6229   1.1  christos /* Determine if the locations of this breakpoint will have their conditions
   6230   1.1  christos    evaluated by the target, host or a mix of both.  Returns the following:
   6231   1.1  christos 
   6232   1.1  christos     "host": Host evals condition.
   6233   1.1  christos     "host or target": Host or Target evals condition.
   6234  1.10  christos     "target": Target evals condition.
   6235   1.1  christos */
   6236   1.1  christos 
   6237   1.1  christos static const char *
   6238   1.1  christos bp_condition_evaluator (const breakpoint *b)
   6239   1.1  christos {
   6240   1.1  christos   char host_evals = 0;
   6241   1.1  christos   char target_evals = 0;
   6242   1.1  christos 
   6243   1.1  christos   if (!b)
   6244   1.1  christos     return NULL;
   6245   1.1  christos 
   6246   1.1  christos   if (!is_breakpoint (b))
   6247   1.1  christos     return NULL;
   6248   1.1  christos 
   6249  1.11  christos   if (gdb_evaluates_breakpoint_condition_p ()
   6250   1.1  christos       || !target_supports_evaluation_of_breakpoint_conditions ())
   6251  1.11  christos     return condition_evaluation_host;
   6252   1.1  christos 
   6253   1.1  christos   for (bp_location &bl : b->locations ())
   6254   1.1  christos     {
   6255   1.1  christos       if (bl.cond_bytecode)
   6256   1.1  christos 	target_evals++;
   6257   1.1  christos       else
   6258   1.1  christos 	host_evals++;
   6259   1.1  christos     }
   6260   1.1  christos 
   6261   1.1  christos   if (host_evals && target_evals)
   6262   1.1  christos     return condition_evaluation_both;
   6263   1.1  christos   else if (target_evals)
   6264   1.1  christos     return condition_evaluation_target;
   6265   1.1  christos   else
   6266   1.1  christos     return condition_evaluation_host;
   6267   1.1  christos }
   6268   1.1  christos 
   6269  1.10  christos /* Determine the breakpoint location's condition evaluator.  This is
   6270   1.1  christos    similar to bp_condition_evaluator, but for locations.  */
   6271   1.1  christos 
   6272   1.1  christos static const char *
   6273   1.1  christos bp_location_condition_evaluator (const struct bp_location *bl)
   6274   1.1  christos {
   6275   1.1  christos   if (bl && !is_breakpoint (bl->owner))
   6276   1.1  christos     return NULL;
   6277   1.1  christos 
   6278   1.1  christos   if (gdb_evaluates_breakpoint_condition_p ()
   6279   1.1  christos       || !target_supports_evaluation_of_breakpoint_conditions ())
   6280   1.1  christos     return condition_evaluation_host;
   6281   1.1  christos 
   6282   1.1  christos   if (bl && bl->cond_bytecode)
   6283   1.1  christos     return condition_evaluation_target;
   6284   1.1  christos   else
   6285   1.1  christos     return condition_evaluation_host;
   6286   1.1  christos }
   6287  1.11  christos 
   6288   1.1  christos /* Print the LOC location out of the list of B->LOC locations.  */
   6289   1.1  christos 
   6290   1.8  christos static void
   6291   1.8  christos print_breakpoint_location (const breakpoint *b, const bp_location *loc)
   6292   1.1  christos {
   6293   1.1  christos   struct ui_out *uiout = current_uiout;
   6294   1.1  christos 
   6295   1.1  christos   scoped_restore_current_program_space restore_pspace;
   6296   1.1  christos 
   6297   1.1  christos   if (loc != NULL && loc->shlib_disabled)
   6298   1.1  christos     loc = NULL;
   6299   1.1  christos 
   6300  1.10  christos   if (loc != NULL)
   6301   1.1  christos     set_current_program_space (loc->pspace);
   6302   1.1  christos 
   6303   1.8  christos   if (b->display_canonical)
   6304   1.8  christos     uiout->field_string ("what", b->locspec->to_string ());
   6305   1.1  christos   else if (loc && loc->symtab)
   6306   1.1  christos     {
   6307   1.7  christos       const struct symbol *sym = loc->symbol;
   6308   1.9  christos 
   6309   1.9  christos       if (sym)
   6310   1.7  christos 	{
   6311   1.7  christos 	  uiout->text ("in ");
   6312   1.7  christos 	  uiout->field_string ("func", sym->print_name (),
   6313   1.1  christos 			       function_name_style.style ());
   6314   1.7  christos 	  uiout->text (" ");
   6315   1.8  christos 	  uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
   6316   1.9  christos 	  uiout->text ("at ");
   6317   1.7  christos 	}
   6318   1.1  christos       uiout->field_string ("file",
   6319   1.7  christos 			   symtab_to_filename_for_display (loc->symtab),
   6320   1.7  christos 			   file_name_style.style ());
   6321   1.1  christos       uiout->text (":");
   6322   1.9  christos 
   6323   1.1  christos       if (uiout->is_mi_like_p ())
   6324   1.1  christos 	uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
   6325   1.1  christos 
   6326   1.7  christos       uiout->field_signed ("line", loc->line_number);
   6327   1.1  christos     }
   6328   1.7  christos   else if (loc)
   6329   1.1  christos     {
   6330   1.7  christos       string_file stb;
   6331   1.1  christos 
   6332   1.1  christos       print_address_symbolic (loc->gdbarch, loc->address, &stb,
   6333   1.6  christos 			      demangle, "");
   6334  1.10  christos       uiout->field_stream ("at", stb);
   6335   1.6  christos     }
   6336   1.6  christos   else
   6337   1.6  christos     {
   6338   1.7  christos       uiout->field_string ("pending", b->locspec->to_string ());
   6339   1.6  christos       /* If extra_string is available, it could be holding a condition
   6340   1.6  christos 	 or dprintf arguments.  In either case, make sure it is printed,
   6341   1.7  christos 	 too, but only for non-MI streams.  */
   6342   1.6  christos       if (!uiout->is_mi_like_p () && b->extra_string != NULL)
   6343   1.7  christos 	{
   6344  1.10  christos 	  if (b->type == bp_dprintf)
   6345   1.6  christos 	    uiout->text (",");
   6346   1.6  christos 	  else
   6347   1.1  christos 	    uiout->text (" ");
   6348   1.1  christos 	  uiout->text (b->extra_string.get ());
   6349   1.1  christos 	}
   6350   1.1  christos     }
   6351   1.1  christos 
   6352   1.7  christos   if (loc && is_breakpoint (b)
   6353   1.7  christos       && breakpoint_condition_evaluation_mode () == condition_evaluation_target
   6354   1.1  christos       && bp_condition_evaluator (b) == condition_evaluation_both)
   6355   1.7  christos     {
   6356   1.1  christos       uiout->text (" (");
   6357   1.1  christos       uiout->field_string ("evaluated-by",
   6358   1.1  christos 			   bp_location_condition_evaluator (loc));
   6359   1.1  christos       uiout->text (")");
   6360   1.1  christos     }
   6361   1.1  christos }
   6362   1.1  christos 
   6363   1.1  christos static const char *
   6364   1.1  christos bptype_string (enum bptype type)
   6365   1.7  christos {
   6366   1.1  christos   struct ep_type_description
   6367   1.1  christos     {
   6368   1.1  christos       enum bptype type;
   6369   1.1  christos       const char *description;
   6370   1.1  christos     };
   6371   1.1  christos   static struct ep_type_description bptypes[] =
   6372   1.3  christos   {
   6373   1.1  christos     {bp_none, "?deleted?"},
   6374   1.1  christos     {bp_breakpoint, "breakpoint"},
   6375   1.1  christos     {bp_hardware_breakpoint, "hw breakpoint"},
   6376   1.1  christos     {bp_single_step, "sw single-step"},
   6377   1.1  christos     {bp_until, "until"},
   6378   1.1  christos     {bp_finish, "finish"},
   6379   1.1  christos     {bp_watchpoint, "watchpoint"},
   6380   1.1  christos     {bp_hardware_watchpoint, "hw watchpoint"},
   6381   1.1  christos     {bp_read_watchpoint, "read watchpoint"},
   6382   1.1  christos     {bp_access_watchpoint, "acc watchpoint"},
   6383   1.1  christos     {bp_longjmp, "longjmp"},
   6384   1.1  christos     {bp_longjmp_resume, "longjmp resume"},
   6385   1.1  christos     {bp_longjmp_call_dummy, "longjmp for call dummy"},
   6386   1.1  christos     {bp_exception, "exception"},
   6387   1.1  christos     {bp_exception_resume, "exception resume"},
   6388   1.1  christos     {bp_step_resume, "step resume"},
   6389   1.1  christos     {bp_hp_step_resume, "high-priority step resume"},
   6390   1.1  christos     {bp_watchpoint_scope, "watchpoint scope"},
   6391   1.1  christos     {bp_call_dummy, "call dummy"},
   6392   1.1  christos     {bp_std_terminate, "std::terminate"},
   6393   1.1  christos     {bp_shlib_event, "shlib events"},
   6394   1.1  christos     {bp_thread_event, "thread events"},
   6395   1.1  christos     {bp_overlay_event, "overlay events"},
   6396   1.1  christos     {bp_longjmp_master, "longjmp master"},
   6397   1.1  christos     {bp_std_terminate_master, "std::terminate master"},
   6398   1.1  christos     {bp_exception_master, "exception master"},
   6399  1.10  christos     {bp_catchpoint, "catchpoint"},
   6400   1.1  christos     {bp_tracepoint, "tracepoint"},
   6401   1.1  christos     {bp_fast_tracepoint, "fast tracepoint"},
   6402   1.1  christos     {bp_static_tracepoint, "static tracepoint"},
   6403   1.1  christos     {bp_static_marker_tracepoint, "static marker tracepoint"},
   6404   1.1  christos     {bp_dprintf, "dprintf"},
   6405   1.1  christos     {bp_jit_event, "jit events"},
   6406   1.1  christos     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
   6407   1.1  christos     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
   6408  1.10  christos   };
   6409   1.1  christos 
   6410   1.1  christos   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
   6411   1.1  christos       || ((int) type != bptypes[(int) type].type))
   6412   1.1  christos     internal_error (_("bptypes table does not describe type #%d."),
   6413   1.1  christos 		    (int) type);
   6414   1.1  christos 
   6415   1.1  christos   return bptypes[(int) type].description;
   6416   1.1  christos }
   6417   1.1  christos 
   6418   1.1  christos /* For MI, output a field named 'thread-groups' with a list as the value.
   6419   1.1  christos    For CLI, prefix the list with the string 'inf'. */
   6420   1.8  christos 
   6421   1.1  christos static void
   6422   1.1  christos output_thread_groups (struct ui_out *uiout,
   6423   1.7  christos 		      const char *field_name,
   6424   1.1  christos 		      const std::vector<int> &inf_nums,
   6425   1.1  christos 		      int mi_only)
   6426   1.1  christos {
   6427   1.1  christos   int is_mi = uiout->is_mi_like_p ();
   6428   1.1  christos 
   6429   1.1  christos   /* For backward compatibility, don't display inferiors in CLI unless
   6430   1.8  christos      there are several.  Always display them for MI. */
   6431   1.1  christos   if (!is_mi && mi_only)
   6432   1.8  christos     return;
   6433   1.1  christos 
   6434   1.1  christos   ui_out_emit_list list_emitter (uiout, field_name);
   6435   1.1  christos 
   6436   1.1  christos   for (size_t i = 0; i < inf_nums.size (); i++)
   6437   1.1  christos     {
   6438   1.8  christos       if (is_mi)
   6439   1.7  christos 	{
   6440   1.1  christos 	  char mi_group[10];
   6441   1.1  christos 
   6442   1.1  christos 	  xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
   6443   1.1  christos 	  uiout->field_string (NULL, mi_group);
   6444   1.7  christos 	}
   6445   1.1  christos       else
   6446   1.7  christos 	{
   6447   1.1  christos 	  if (i == 0)
   6448   1.8  christos 	    uiout->text (" inf ");
   6449   1.1  christos 	  else
   6450   1.1  christos 	    uiout->text (", ");
   6451   1.1  christos 
   6452   1.1  christos 	  uiout->text (plongest (inf_nums[i]));
   6453  1.10  christos 	}
   6454  1.10  christos     }
   6455  1.10  christos }
   6456  1.10  christos 
   6457   1.9  christos /* See breakpoint.h. */
   6458   1.9  christos 
   6459   1.9  christos bool fix_breakpoint_script_output_globally = false;
   6460   1.9  christos 
   6461  1.10  christos /* Print B to gdb_stdout.  If RAW_LOC, print raw breakpoint locations
   6462  1.10  christos    instead of going via breakpoint_ops::print_one.  This makes "maint
   6463   1.1  christos    info breakpoints" show the software breakpoint locations of
   6464  1.10  christos    catchpoints, which are considered internal implementation
   6465   1.1  christos    detail.  Returns true if RAW_LOC is false and if the breakpoint's
   6466   1.1  christos    print_one method did something; false otherwise.  */
   6467   1.1  christos 
   6468  1.11  christos static bool
   6469   1.9  christos print_one_breakpoint_location (struct breakpoint *b,
   6470   1.1  christos 			       struct bp_location *loc,
   6471   1.1  christos 			       int loc_number,
   6472   1.1  christos 			       const bp_location **last_loc,
   6473   1.1  christos 			       int allflag, bool raw_loc)
   6474   1.1  christos {
   6475  1.10  christos   struct command_line *l;
   6476  1.10  christos   static char bpenables[] = "nynny";
   6477   1.1  christos 
   6478   1.1  christos   struct ui_out *uiout = current_uiout;
   6479   1.1  christos   bool header_of_multiple = false;
   6480   1.1  christos   bool part_of_multiple = (loc != NULL);
   6481   1.1  christos   struct value_print_options opts;
   6482   1.1  christos 
   6483   1.1  christos   get_user_print_options (&opts);
   6484   1.1  christos 
   6485  1.11  christos   gdb_assert (!loc || loc_number != 0);
   6486  1.11  christos   /* See comment in print_one_breakpoint concerning treatment of
   6487  1.11  christos      breakpoints with single disabled location.  */
   6488  1.10  christos   if (loc == NULL
   6489  1.11  christos       && (b->has_locations ()
   6490  1.11  christos 	  && (b->has_multiple_locations ()
   6491  1.11  christos 	      || !b->first_loc ().enabled || b->first_loc ().disabled_by_cond)))
   6492   1.1  christos     header_of_multiple = true;
   6493   1.1  christos 
   6494   1.1  christos   if (loc == NULL && b->has_locations ())
   6495   1.1  christos     loc = &b->first_loc ();
   6496   1.1  christos 
   6497   1.1  christos   annotate_record ();
   6498   1.8  christos 
   6499   1.1  christos   /* 1 */
   6500   1.9  christos   annotate_field (0);
   6501   1.1  christos   if (part_of_multiple)
   6502   1.1  christos     uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
   6503   1.1  christos   else
   6504   1.1  christos     uiout->field_signed ("number", b->number);
   6505   1.7  christos 
   6506   1.1  christos   /* 2 */
   6507   1.7  christos   annotate_field (1);
   6508   1.1  christos   if (part_of_multiple)
   6509   1.1  christos     uiout->field_skip ("type");
   6510   1.1  christos   else
   6511   1.1  christos     uiout->field_string ("type", bptype_string (b->type));
   6512   1.7  christos 
   6513   1.1  christos   /* 3 */
   6514   1.7  christos   annotate_field (2);
   6515   1.1  christos   if (part_of_multiple)
   6516   1.1  christos     uiout->field_skip ("disp");
   6517   1.1  christos   else
   6518   1.1  christos     uiout->field_string ("disp", bpdisp_text (b->disposition));
   6519  1.10  christos 
   6520  1.10  christos   /* 4 */
   6521  1.10  christos   annotate_field (3);
   6522  1.10  christos   if (part_of_multiple)
   6523  1.10  christos     {
   6524  1.10  christos       /* For locations that are disabled because of an invalid
   6525  1.10  christos 	 condition, display "N*" on the CLI, where "*" refers to a
   6526  1.11  christos 	 footnote below the table.  For MI, simply display a "N"
   6527  1.10  christos 	 without a footnote.  On the CLI, for enabled locations whose
   6528  1.10  christos 	 breakpoint is disabled, display "y-".  */
   6529  1.10  christos       auto get_enable_state = [uiout, loc] () -> const char *
   6530  1.10  christos 	{
   6531  1.10  christos 	  if (uiout->is_mi_like_p ())
   6532  1.10  christos 	    {
   6533  1.10  christos 	      if (loc->disabled_by_cond)
   6534  1.10  christos 		return "N";
   6535  1.10  christos 	      else if (!loc->enabled)
   6536  1.10  christos 		return "n";
   6537  1.10  christos 	      else
   6538  1.10  christos 		return "y";
   6539  1.10  christos 	    }
   6540  1.10  christos 	  else
   6541  1.10  christos 	    {
   6542  1.10  christos 	      if (loc->disabled_by_cond)
   6543  1.10  christos 		return "N*";
   6544  1.10  christos 	      else if (!loc->enabled)
   6545  1.10  christos 		return "n";
   6546  1.10  christos 	      else if (!breakpoint_enabled (loc->owner))
   6547  1.10  christos 		return "y-";
   6548  1.10  christos 	      else
   6549  1.10  christos 		return "y";
   6550   1.1  christos 	    }
   6551   1.7  christos 	};
   6552   1.1  christos       uiout->field_string ("enabled", get_enable_state ());
   6553   1.1  christos     }
   6554  1.10  christos   else
   6555  1.10  christos     uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
   6556  1.10  christos 
   6557   1.9  christos   /* 5 and 6 */
   6558   1.1  christos   bool result = false;
   6559   1.9  christos   if (!raw_loc && b->print_one (last_loc))
   6560   1.1  christos     result = true;
   6561  1.11  christos   else
   6562   1.1  christos     {
   6563   1.1  christos       if (is_watchpoint (b))
   6564   1.1  christos 	{
   6565   1.1  christos 	  watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
   6566   1.1  christos 
   6567   1.7  christos 	  /* Field 4, the address, is omitted (which makes the columns
   6568   1.1  christos 	     not line up too nicely with the headers, but the effect
   6569  1.10  christos 	     is relatively readable).  */
   6570   1.1  christos 	  if (opts.addressprint)
   6571   1.9  christos 	    uiout->field_skip ("addr");
   6572   1.9  christos 	  annotate_field (5);
   6573   1.9  christos 	  uiout->field_string ("what", w->exp_string.get ());
   6574   1.9  christos 	}
   6575   1.9  christos       else if (!is_catchpoint (b) || is_exception_catchpoint (b)
   6576   1.9  christos 	       || is_ada_exception_catchpoint (b))
   6577   1.9  christos 	{
   6578   1.9  christos 	  if (opts.addressprint)
   6579   1.9  christos 	    {
   6580  1.11  christos 	      annotate_field (4);
   6581   1.9  christos 	      if (header_of_multiple)
   6582   1.9  christos 		uiout->field_string ("addr", "<MULTIPLE>",
   6583   1.9  christos 				     metadata_style.style ());
   6584   1.9  christos 	      else if (!b->has_locations () || loc->shlib_disabled)
   6585   1.9  christos 		uiout->field_string ("addr", "<PENDING>",
   6586   1.9  christos 				     metadata_style.style ());
   6587   1.9  christos 	      else
   6588   1.9  christos 		uiout->field_core_addr ("addr",
   6589   1.9  christos 					loc->gdbarch, loc->address);
   6590  1.11  christos 	    }
   6591  1.11  christos 	  annotate_field (5);
   6592   1.9  christos 	  if (!header_of_multiple)
   6593   1.9  christos 	    print_breakpoint_location (b, loc);
   6594   1.1  christos 	  if (b->has_locations ())
   6595   1.1  christos 	    *last_loc = &b->first_loc ();
   6596   1.1  christos 	}
   6597   1.8  christos     }
   6598   1.1  christos 
   6599   1.1  christos   if (loc != NULL && !header_of_multiple)
   6600   1.8  christos     {
   6601   1.1  christos       std::vector<int> inf_nums;
   6602   1.1  christos       int mi_only = 1;
   6603   1.8  christos 
   6604   1.1  christos       for (inferior *inf : all_inferiors ())
   6605   1.1  christos 	{
   6606  1.11  christos 	  if (inf->pspace == loc->pspace)
   6607  1.11  christos 	    inf_nums.push_back (inf->num);
   6608  1.11  christos 	}
   6609  1.11  christos 
   6610  1.11  christos       /* For backward compatibility, don't display inferiors in CLI unless
   6611  1.11  christos 	 there are several.  Always display for MI. */
   6612  1.11  christos       if (allflag
   6613  1.11  christos 	  || (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
   6614  1.11  christos 	      && (program_spaces.size () > 1
   6615   1.1  christos 		  || number_of_inferiors () > 1)
   6616   1.8  christos 	      /* LOC is for existing B, it cannot be in
   6617   1.1  christos 		 moribund_locations and thus having NULL OWNER.  */
   6618   1.1  christos 	      && loc->owner->type != bp_catchpoint))
   6619  1.11  christos 	mi_only = 0;
   6620  1.11  christos       output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
   6621  1.11  christos     }
   6622  1.11  christos 
   6623  1.11  christos   /* In the MI output, each location of a thread or task specific
   6624  1.11  christos      breakpoint includes the relevant thread or task ID.  This is done for
   6625  1.11  christos      backwards compatibility reasons.
   6626  1.11  christos 
   6627   1.1  christos      For the CLI output, the thread/task information is printed on a
   6628   1.1  christos      separate line, see the 'stop only in thread' and 'stop only in task'
   6629  1.11  christos      output below.  */
   6630  1.11  christos   if (part_of_multiple && uiout->is_mi_like_p ())
   6631  1.11  christos     {
   6632  1.11  christos       if (b->thread != -1)
   6633  1.11  christos 	uiout->field_signed ("thread", b->thread);
   6634   1.1  christos       else if (b->task != -1)
   6635   1.1  christos 	uiout->field_signed ("task", b->task);
   6636   1.7  christos       else if (b->inferior != -1)
   6637   1.1  christos 	uiout->field_signed ("inferior", b->inferior);
   6638   1.1  christos     }
   6639  1.10  christos 
   6640   1.1  christos   uiout->text ("\n");
   6641   1.1  christos 
   6642   1.1  christos   if (!part_of_multiple)
   6643   1.1  christos     b->print_one_detail (uiout);
   6644   1.7  christos 
   6645   1.1  christos   if (part_of_multiple && frame_id_p (b->frame_id))
   6646  1.10  christos     {
   6647   1.7  christos       annotate_field (6);
   6648   1.1  christos       uiout->text ("\tstop only in stack frame at ");
   6649   1.7  christos       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
   6650   1.1  christos 	 the frame ID.  */
   6651   1.1  christos       uiout->field_core_addr ("frame",
   6652   1.1  christos 			      b->gdbarch, b->frame_id.stack_addr);
   6653   1.1  christos       uiout->text ("\n");
   6654   1.1  christos     }
   6655   1.1  christos 
   6656   1.7  christos   if (!part_of_multiple && b->cond_string)
   6657   1.1  christos     {
   6658   1.7  christos       annotate_field (7);
   6659  1.10  christos       if (is_tracepoint (b))
   6660   1.1  christos 	uiout->text ("\ttrace only if ");
   6661   1.1  christos       else
   6662   1.1  christos 	uiout->text ("\tstop only if ");
   6663   1.1  christos       uiout->field_string ("cond", b->cond_string.get ());
   6664   1.1  christos 
   6665   1.1  christos       /* Print whether the target is doing the breakpoint's condition
   6666   1.1  christos 	 evaluation.  If GDB is doing the evaluation, don't print anything.  */
   6667   1.9  christos       if (is_breakpoint (b)
   6668   1.9  christos 	  && breakpoint_condition_evaluation_mode ()
   6669   1.9  christos 	  == condition_evaluation_target)
   6670   1.1  christos 	{
   6671   1.7  christos 	  uiout->message (" (%pF evals)",
   6672   1.1  christos 			  string_field ("evaluated-by",
   6673   1.1  christos 					bp_condition_evaluator (b)));
   6674   1.1  christos 	}
   6675   1.1  christos       uiout->text ("\n");
   6676   1.1  christos     }
   6677   1.7  christos 
   6678   1.7  christos   if (!part_of_multiple && b->thread != -1)
   6679   1.9  christos     {
   6680   1.6  christos       /* FIXME should make an annotation for this.  */
   6681   1.6  christos       uiout->text ("\tstop only in thread ");
   6682   1.6  christos       if (uiout->is_mi_like_p ())
   6683   1.6  christos 	uiout->field_signed ("thread", b->thread);
   6684   1.7  christos       else
   6685   1.6  christos 	{
   6686   1.7  christos 	  struct thread_info *thr = find_thread_global_id (b->thread);
   6687   1.1  christos 
   6688  1.11  christos 	  uiout->field_string ("thread", print_thread_id (thr));
   6689  1.11  christos 	}
   6690  1.11  christos       uiout->text ("\n");
   6691  1.11  christos     }
   6692  1.11  christos 
   6693  1.11  christos   if (!part_of_multiple && b->task != -1)
   6694  1.11  christos     {
   6695  1.11  christos       uiout->text ("\tstop only in task ");
   6696  1.11  christos       uiout->field_signed ("task", b->task);
   6697  1.11  christos       uiout->text ("\n");
   6698  1.11  christos     }
   6699  1.11  christos 
   6700  1.11  christos   if (!part_of_multiple && b->inferior != -1)
   6701  1.11  christos     {
   6702  1.11  christos       uiout->text ("\tstop only in inferior ");
   6703   1.1  christos       uiout->field_signed ("inferior", b->inferior);
   6704   1.1  christos       uiout->text ("\n");
   6705   1.1  christos     }
   6706   1.1  christos 
   6707   1.1  christos   if (!part_of_multiple)
   6708   1.1  christos     {
   6709   1.7  christos       if (b->hit_count)
   6710   1.1  christos 	{
   6711   1.7  christos 	  /* FIXME should make an annotation for this.  */
   6712   1.1  christos 	  if (is_catchpoint (b))
   6713   1.7  christos 	    uiout->text ("\tcatchpoint");
   6714   1.7  christos 	  else if (is_tracepoint (b))
   6715   1.9  christos 	    uiout->text ("\ttracepoint");
   6716   1.1  christos 	  else
   6717   1.7  christos 	    uiout->text ("\tbreakpoint");
   6718   1.1  christos 	  uiout->text (" already hit ");
   6719   1.7  christos 	  uiout->field_signed ("times", b->hit_count);
   6720   1.1  christos 	  if (b->hit_count == 1)
   6721   1.1  christos 	    uiout->text (" time\n");
   6722   1.1  christos 	  else
   6723   1.1  christos 	    uiout->text (" times\n");
   6724   1.7  christos 	}
   6725   1.9  christos       else
   6726   1.1  christos 	{
   6727   1.1  christos 	  /* Output the count also if it is zero, but only if this is mi.  */
   6728   1.1  christos 	  if (uiout->is_mi_like_p ())
   6729   1.1  christos 	    uiout->field_signed ("times", b->hit_count);
   6730   1.1  christos 	}
   6731   1.1  christos     }
   6732   1.9  christos 
   6733   1.9  christos   if (!part_of_multiple && b->ignore_count)
   6734   1.1  christos     {
   6735   1.1  christos       annotate_field (8);
   6736   1.1  christos       uiout->message ("\tignore next %pF hits\n",
   6737   1.1  christos 		      signed_field ("ignore", b->ignore_count));
   6738   1.1  christos     }
   6739   1.1  christos 
   6740   1.1  christos   /* Note that an enable count of 1 corresponds to "enable once"
   6741   1.1  christos      behavior, which is reported by the combination of enablement and
   6742   1.7  christos      disposition, so we don't need to mention it here.  */
   6743   1.1  christos   if (!part_of_multiple && b->enable_count > 1)
   6744   1.1  christos     {
   6745   1.1  christos       annotate_field (8);
   6746   1.7  christos       uiout->text ("\tdisable after ");
   6747   1.1  christos       /* Tweak the wording to clarify that ignore and enable counts
   6748   1.7  christos 	 are distinct, and have additive effect.  */
   6749   1.9  christos       if (b->ignore_count)
   6750   1.7  christos 	uiout->text ("additional ");
   6751   1.1  christos       else
   6752   1.1  christos 	uiout->text ("next ");
   6753   1.1  christos       uiout->field_signed ("enable", b->enable_count);
   6754   1.1  christos       uiout->text (" hits\n");
   6755  1.11  christos     }
   6756   1.1  christos 
   6757   1.1  christos   if (!part_of_multiple && is_tracepoint (b))
   6758   1.1  christos     {
   6759   1.7  christos       tracepoint *tp = gdb::checked_static_cast<tracepoint *> (b);
   6760   1.9  christos 
   6761   1.7  christos       if (tp->traceframe_usage)
   6762   1.1  christos 	{
   6763   1.1  christos 	  uiout->text ("\ttrace buffer usage ");
   6764   1.1  christos 	  uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
   6765   1.8  christos 	  uiout->text (" bytes\n");
   6766   1.1  christos 	}
   6767   1.1  christos     }
   6768   1.1  christos 
   6769  1.10  christos   l = b->commands ? b->commands.get () : NULL;
   6770  1.10  christos   if (!part_of_multiple && l)
   6771  1.10  christos     {
   6772  1.11  christos       annotate_field (9);
   6773  1.10  christos 
   6774  1.11  christos       bool use_fixed_output =
   6775  1.11  christos 	(uiout->test_flags (fix_breakpoint_script_output)
   6776  1.10  christos 	 || fix_breakpoint_script_output_globally);
   6777  1.10  christos 
   6778  1.10  christos       std::optional<ui_out_emit_tuple> tuple_emitter;
   6779  1.10  christos       std::optional<ui_out_emit_list> list_emitter;
   6780  1.10  christos 
   6781  1.10  christos       if (use_fixed_output)
   6782   1.1  christos 	list_emitter.emplace (uiout, "script");
   6783   1.1  christos       else
   6784   1.1  christos 	tuple_emitter.emplace (uiout, "script");
   6785   1.1  christos 
   6786   1.1  christos       print_command_lines (uiout, l, 4);
   6787  1.11  christos     }
   6788   1.1  christos 
   6789   1.1  christos   if (is_tracepoint (b))
   6790   1.1  christos     {
   6791   1.1  christos       tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
   6792   1.7  christos 
   6793   1.9  christos       if (!part_of_multiple && t->pass_count)
   6794   1.7  christos 	{
   6795   1.1  christos 	  annotate_field (10);
   6796   1.1  christos 	  uiout->text ("\tpass count ");
   6797   1.1  christos 	  uiout->field_signed ("pass", t->pass_count);
   6798   1.1  christos 	  uiout->text (" \n");
   6799   1.1  christos 	}
   6800   1.1  christos 
   6801   1.1  christos       /* Don't display it when tracepoint or tracepoint location is
   6802   1.1  christos 	 pending.   */
   6803   1.7  christos       if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
   6804   1.7  christos 	{
   6805   1.1  christos 	  annotate_field (11);
   6806   1.1  christos 
   6807   1.1  christos 	  if (uiout->is_mi_like_p ())
   6808   1.1  christos 	    uiout->field_string ("installed",
   6809   1.7  christos 				 loc->inserted ? "y" : "n");
   6810   1.1  christos 	  else
   6811   1.7  christos 	    {
   6812   1.7  christos 	      if (loc->inserted)
   6813   1.1  christos 		uiout->text ("\t");
   6814   1.1  christos 	      else
   6815   1.1  christos 		uiout->text ("\tnot ");
   6816   1.1  christos 	      uiout->text ("installed on target\n");
   6817   1.7  christos 	    }
   6818   1.1  christos 	}
   6819   1.1  christos     }
   6820   1.1  christos 
   6821  1.11  christos   if (uiout->is_mi_like_p () && !part_of_multiple)
   6822   1.1  christos     {
   6823  1.10  christos       if (is_watchpoint (b))
   6824  1.10  christos 	{
   6825  1.10  christos 	  watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
   6826  1.10  christos 
   6827  1.10  christos 	  uiout->field_string ("original-location", w->exp_string.get ());
   6828  1.10  christos 	}
   6829  1.10  christos       else if (b->locspec != nullptr)
   6830   1.1  christos 	{
   6831   1.1  christos 	  const char *str = b->locspec->to_string ();
   6832  1.10  christos 	  if (str != nullptr)
   6833  1.10  christos 	    uiout->field_string ("original-location", str);
   6834   1.1  christos 	}
   6835   1.1  christos     }
   6836   1.9  christos 
   6837   1.9  christos   return result;
   6838   1.9  christos }
   6839   1.9  christos 
   6840   1.1  christos /* See breakpoint.h. */
   6841  1.11  christos 
   6842   1.1  christos bool fix_multi_location_breakpoint_output_globally = false;
   6843   1.1  christos 
   6844   1.9  christos static void
   6845   1.9  christos print_one_breakpoint (breakpoint *b, const bp_location **last_loc, int allflag)
   6846   1.9  christos {
   6847   1.9  christos   struct ui_out *uiout = current_uiout;
   6848  1.11  christos   bool use_fixed_output
   6849  1.11  christos     = (uiout->test_flags (fix_multi_location_breakpoint_output)
   6850  1.10  christos        || fix_multi_location_breakpoint_output_globally);
   6851  1.10  christos 
   6852   1.9  christos   std::optional<ui_out_emit_tuple> bkpt_tuple_emitter (std::in_place, uiout,
   6853   1.9  christos 						       "bkpt");
   6854   1.9  christos   bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
   6855   1.9  christos 						allflag, false);
   6856   1.9  christos 
   6857   1.1  christos   /* The mi2 broken format: the main breakpoint tuple ends here, the locations
   6858   1.1  christos      are outside.  */
   6859   1.1  christos   if (!use_fixed_output)
   6860   1.1  christos     bkpt_tuple_emitter.reset ();
   6861  1.10  christos 
   6862   1.1  christos   /* If this breakpoint has custom print function,
   6863   1.1  christos      it's already printed.  Otherwise, print individual
   6864   1.1  christos      locations, if any.  */
   6865   1.1  christos   if (!printed || allflag)
   6866   1.1  christos     {
   6867   1.1  christos       /* If breakpoint has a single location that is disabled, we
   6868   1.1  christos 	 print it as if it had several locations, since otherwise it's
   6869   1.9  christos 	 hard to represent "breakpoint enabled, location disabled"
   6870   1.9  christos 	 situation.
   6871   1.9  christos 
   6872   1.9  christos 	 Note that while hardware watchpoints have several locations
   6873   1.9  christos 	 internally, that's not a property exposed to users.
   6874   1.9  christos 
   6875   1.9  christos 	 Likewise, while catchpoints may be implemented with
   6876   1.9  christos 	 breakpoints (e.g., catch throw), that's not a property
   6877   1.9  christos 	 exposed to users.  We do however display the internal
   6878   1.9  christos 	 breakpoint locations with "maint info breakpoints".  */
   6879  1.11  christos       if (!is_hardware_watchpoint (b)
   6880  1.11  christos 	  && (!is_catchpoint (b) || is_exception_catchpoint (b)
   6881  1.11  christos 	      || is_ada_exception_catchpoint (b))
   6882  1.11  christos 	  && (allflag
   6883   1.9  christos 	      || (b->has_locations ()
   6884  1.11  christos 		  && (b->has_multiple_locations ()
   6885   1.9  christos 		      || !b->first_loc ().enabled
   6886   1.9  christos 		      || b->first_loc ().disabled_by_cond))))
   6887   1.9  christos 	{
   6888   1.9  christos 	  std::optional<ui_out_emit_list> locations_list;
   6889   1.9  christos 
   6890   1.9  christos 	  /* For MI version <= 2, keep the behavior where GDB outputs an invalid
   6891   1.9  christos 	     MI record.  For later versions, place breakpoint locations in a
   6892   1.1  christos 	     list.  */
   6893  1.11  christos 	  if (uiout->is_mi_like_p () && use_fixed_output)
   6894   1.1  christos 	    locations_list.emplace (uiout, "locations");
   6895   1.9  christos 
   6896  1.11  christos 	  int n = 1;
   6897   1.9  christos 	  for (bp_location &loc : b->locations ())
   6898  1.10  christos 	    {
   6899   1.1  christos 	      ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
   6900   1.1  christos 	      print_one_breakpoint_location (b, &loc, n, last_loc,
   6901   1.1  christos 					     allflag, allflag);
   6902   1.1  christos 	      n++;
   6903   1.1  christos 	    }
   6904   1.1  christos 	}
   6905   1.1  christos     }
   6906   1.1  christos }
   6907   1.1  christos 
   6908   1.1  christos static int
   6909  1.11  christos breakpoint_address_bits (struct breakpoint *b)
   6910   1.1  christos {
   6911  1.11  christos   int print_address_bits = 0;
   6912  1.10  christos 
   6913   1.1  christos   for (bp_location &loc : b->locations ())
   6914  1.11  christos     {
   6915   1.1  christos       if (!bl_address_is_meaningful (&loc))
   6916   1.1  christos 	continue;
   6917   1.1  christos 
   6918   1.1  christos       int addr_bit = gdbarch_addr_bit (loc.gdbarch);
   6919   1.1  christos       if (addr_bit > print_address_bits)
   6920   1.1  christos 	print_address_bits = addr_bit;
   6921   1.1  christos     }
   6922   1.8  christos 
   6923   1.1  christos   return print_address_bits;
   6924   1.8  christos }
   6925   1.8  christos 
   6926   1.1  christos /* See breakpoint.h.  */
   6927  1.11  christos 
   6928   1.8  christos void
   6929   1.1  christos print_breakpoint (breakpoint *b)
   6930   1.1  christos {
   6931   1.1  christos   const bp_location *dummy_loc = nullptr;
   6932   1.1  christos   print_one_breakpoint (b, &dummy_loc, 0);
   6933   1.1  christos }
   6934   1.1  christos 
   6935   1.1  christos /* Return true if this breakpoint was set by the user, false if it is
   6936   1.1  christos    internal or momentary.  */
   6937   1.1  christos 
   6938   1.1  christos int
   6939   1.1  christos user_breakpoint_p (struct breakpoint *b)
   6940   1.6  christos {
   6941   1.6  christos   return b->number > 0;
   6942   1.6  christos }
   6943   1.6  christos 
   6944   1.6  christos /* See breakpoint.h.  */
   6945  1.11  christos 
   6946   1.6  christos int
   6947   1.6  christos pending_breakpoint_p (struct breakpoint *b)
   6948   1.9  christos {
   6949   1.9  christos   return !b->has_locations ();
   6950   1.9  christos }
   6951   1.9  christos 
   6952   1.9  christos /* Print information on breakpoints (including watchpoints and tracepoints).
   6953   1.9  christos 
   6954   1.9  christos    If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
   6955   1.9  christos    understood by number_or_range_parser.  Only breakpoints included in this
   6956   1.9  christos    list are then printed.
   6957   1.9  christos 
   6958   1.9  christos    If SHOW_INTERNAL is true, print internal breakpoints.
   6959   1.9  christos 
   6960   1.1  christos    If FILTER is non-NULL, call it on each breakpoint and only include the
   6961   1.1  christos    ones for which it returns true.
   6962   1.9  christos 
   6963   1.9  christos    Return the total number of breakpoints listed.  */
   6964   1.1  christos 
   6965  1.11  christos static int
   6966   1.1  christos breakpoint_1 (const char *bp_num_list, bool show_internal,
   6967   1.1  christos 	      bool (*filter) (const struct breakpoint *))
   6968   1.1  christos {
   6969   1.1  christos   const bp_location *last_loc = nullptr;
   6970   1.1  christos   int nr_printable_breakpoints;
   6971  1.10  christos   struct value_print_options opts;
   6972   1.1  christos   int print_address_bits = 0;
   6973   1.1  christos   int print_type_col_width = 14;
   6974   1.1  christos   struct ui_out *uiout = current_uiout;
   6975   1.1  christos   bool has_disabled_by_cond_location = false;
   6976   1.1  christos 
   6977   1.1  christos   get_user_print_options (&opts);
   6978  1.11  christos 
   6979   1.1  christos   /* Compute the number of rows in the table, as well as the size
   6980   1.1  christos      required for address fields.  */
   6981  1.11  christos   nr_printable_breakpoints = 0;
   6982   1.1  christos   for (breakpoint &b : all_breakpoints ())
   6983   1.1  christos     {
   6984   1.9  christos       /* If we have a filter, only list the breakpoints it accepts.  */
   6985   1.1  christos       if (filter && !filter (&b))
   6986   1.9  christos 	continue;
   6987   1.1  christos 
   6988  1.11  christos       /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
   6989   1.1  christos 	 accept.  Skip the others.  */
   6990  1.11  christos       if (bp_num_list != NULL && *bp_num_list != '\0')
   6991   1.1  christos 	{
   6992   1.1  christos 	  if (show_internal && parse_and_eval_long (bp_num_list) != b.number)
   6993   1.1  christos 	    continue;
   6994  1.11  christos 	  if (!show_internal && !number_is_in_list (bp_num_list, b.number))
   6995   1.1  christos 	    continue;
   6996   1.1  christos 	}
   6997   1.1  christos 
   6998  1.11  christos       if (show_internal || user_breakpoint_p (&b))
   6999   1.1  christos 	{
   7000   1.1  christos 	  int addr_bit, type_len;
   7001   1.1  christos 
   7002  1.11  christos 	  addr_bit = breakpoint_address_bits (&b);
   7003   1.1  christos 	  if (addr_bit > print_address_bits)
   7004   1.1  christos 	    print_address_bits = addr_bit;
   7005   1.1  christos 
   7006   1.1  christos 	  type_len = strlen (bptype_string (b.type));
   7007   1.1  christos 	  if (type_len > print_type_col_width)
   7008   1.1  christos 	    print_type_col_width = type_len;
   7009   1.1  christos 
   7010   1.8  christos 	  nr_printable_breakpoints++;
   7011   1.8  christos 	}
   7012   1.8  christos     }
   7013   1.8  christos 
   7014   1.8  christos   {
   7015   1.8  christos     ui_out_emit_table table_emitter (uiout,
   7016   1.8  christos 				     opts.addressprint ? 6 : 5,
   7017   1.8  christos 				     nr_printable_breakpoints,
   7018   1.8  christos 				     "BreakpointTable");
   7019   1.8  christos 
   7020   1.8  christos     if (nr_printable_breakpoints > 0)
   7021   1.8  christos       annotate_breakpoints_headers ();
   7022   1.8  christos     if (nr_printable_breakpoints > 0)
   7023   1.8  christos       annotate_field (0);
   7024   1.8  christos     uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
   7025   1.8  christos     if (nr_printable_breakpoints > 0)
   7026   1.8  christos       annotate_field (1);
   7027   1.8  christos     uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
   7028   1.8  christos     if (nr_printable_breakpoints > 0)
   7029   1.8  christos       annotate_field (2);
   7030   1.8  christos     uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
   7031   1.8  christos     if (nr_printable_breakpoints > 0)
   7032   1.8  christos       annotate_field (3);
   7033   1.8  christos     uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
   7034   1.8  christos     if (opts.addressprint)
   7035   1.8  christos       {
   7036   1.8  christos 	if (nr_printable_breakpoints > 0)
   7037   1.8  christos 	  annotate_field (4);
   7038   1.8  christos 	if (print_address_bits <= 32)
   7039   1.8  christos 	  uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
   7040   1.8  christos 	else
   7041   1.8  christos 	  uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
   7042   1.8  christos       }
   7043   1.8  christos     if (nr_printable_breakpoints > 0)
   7044   1.8  christos       annotate_field (5);
   7045   1.1  christos     uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
   7046  1.11  christos     uiout->table_body ();
   7047   1.8  christos     if (nr_printable_breakpoints > 0)
   7048   1.8  christos       annotate_breakpoints_table ();
   7049   1.8  christos 
   7050  1.11  christos     for (breakpoint &b : all_breakpoints ())
   7051   1.8  christos       {
   7052   1.1  christos 	QUIT;
   7053   1.9  christos 	/* If we have a filter, only list the breakpoints it accepts.  */
   7054   1.8  christos 	if (filter && !filter (&b))
   7055   1.1  christos 	  continue;
   7056   1.9  christos 
   7057   1.8  christos 	/* If we have a BP_NUM_LIST string, it is a list of breakpoints to
   7058   1.9  christos 	   accept.  Skip the others.  */
   7059   1.8  christos 
   7060  1.11  christos 	if (bp_num_list != NULL && *bp_num_list != '\0')
   7061   1.8  christos 	  {
   7062   1.8  christos 	    if (show_internal)	/* maintenance info breakpoint */
   7063   1.8  christos 	      {
   7064   1.8  christos 		if (parse_and_eval_long (bp_num_list) != b.number)
   7065  1.11  christos 		  continue;
   7066   1.8  christos 	      }
   7067   1.8  christos 	    else		/* all others */
   7068   1.8  christos 	      {
   7069   1.8  christos 		if (!number_is_in_list (bp_num_list, b.number))
   7070   1.9  christos 		  continue;
   7071  1.11  christos 	      }
   7072  1.10  christos 	  }
   7073  1.11  christos 	/* We only print out user settable breakpoints unless the
   7074  1.11  christos 	   show_internal is set.  */
   7075  1.11  christos 	if (show_internal || user_breakpoint_p (&b))
   7076  1.10  christos 	  {
   7077  1.10  christos 	    print_one_breakpoint (&b, &last_loc, show_internal);
   7078   1.8  christos 	    for (bp_location &loc : b.locations ())
   7079   1.8  christos 	      if (loc.disabled_by_cond)
   7080   1.1  christos 		has_disabled_by_cond_location = true;
   7081   1.1  christos 	  }
   7082   1.1  christos       }
   7083   1.1  christos   }
   7084   1.1  christos 
   7085   1.1  christos   if (nr_printable_breakpoints == 0)
   7086   1.1  christos     {
   7087   1.9  christos       /* If there's a filter, let the caller decide how to report
   7088  1.11  christos 	 empty list.  */
   7089  1.11  christos       if (!filter)
   7090   1.1  christos 	{
   7091  1.11  christos 	  if (bp_num_list == NULL || *bp_num_list == '\0')
   7092  1.11  christos 	    uiout->message ("No breakpoints, watchpoints, tracepoints, "
   7093   1.1  christos 			    "or catchpoints.\n");
   7094   1.1  christos 	  else
   7095   1.1  christos 	    uiout->message ("No breakpoint, watchpoint, tracepoint, "
   7096   1.1  christos 			    "or catchpoint matching '%s'.\n", bp_num_list);
   7097   1.1  christos 	}
   7098   1.1  christos     }
   7099  1.10  christos   else
   7100  1.10  christos     {
   7101  1.10  christos       if (last_loc && !server_command)
   7102  1.10  christos 	set_next_address (last_loc->gdbarch, last_loc->address);
   7103   1.1  christos 
   7104   1.1  christos       if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
   7105   1.1  christos 	uiout->message (_("(*): Breakpoint condition is invalid at this "
   7106   1.1  christos 			  "location.\n"));
   7107   1.1  christos     }
   7108   1.1  christos 
   7109   1.1  christos   /* FIXME?  Should this be moved up so that it is only called when
   7110   1.1  christos      there have been breakpoints? */
   7111   1.1  christos   annotate_breakpoints_table_end ();
   7112   1.1  christos 
   7113   1.1  christos   return nr_printable_breakpoints;
   7114   1.1  christos }
   7115   1.1  christos 
   7116   1.1  christos /* Display the value of default-collect in a way that is generally
   7117   1.1  christos    compatible with the breakpoint list.  */
   7118   1.1  christos 
   7119   1.1  christos static void
   7120   1.1  christos default_collect_info (void)
   7121   1.1  christos {
   7122   1.1  christos   struct ui_out *uiout = current_uiout;
   7123  1.10  christos 
   7124   1.1  christos   /* If it has no value (which is frequently the case), say nothing; a
   7125   1.1  christos      message like "No default-collect." gets in user's face when it's
   7126   1.1  christos      not wanted.  */
   7127   1.1  christos   if (default_collect.empty ())
   7128   1.7  christos     return;
   7129   1.7  christos 
   7130   1.7  christos   /* The following phrase lines up nicely with per-tracepoint collect
   7131   1.1  christos      actions.  */
   7132   1.1  christos   uiout->text ("default collect ");
   7133   1.1  christos   uiout->field_string ("default-collect", default_collect);
   7134   1.8  christos   uiout->text (" \n");
   7135   1.1  christos }
   7136   1.9  christos 
   7137   1.1  christos static void
   7138   1.1  christos info_breakpoints_command (const char *args, int from_tty)
   7139   1.1  christos {
   7140   1.1  christos   breakpoint_1 (args, false, NULL);
   7141   1.1  christos 
   7142   1.8  christos   default_collect_info ();
   7143   1.1  christos }
   7144   1.9  christos 
   7145   1.1  christos static void
   7146   1.1  christos info_watchpoints_command (const char *args, int from_tty)
   7147   1.1  christos {
   7148   1.1  christos   int num_printed = breakpoint_1 (args, false, is_watchpoint);
   7149   1.1  christos   struct ui_out *uiout = current_uiout;
   7150   1.7  christos 
   7151   1.1  christos   if (num_printed == 0)
   7152   1.7  christos     {
   7153   1.1  christos       if (args == NULL || *args == '\0')
   7154   1.1  christos 	uiout->message ("No watchpoints.\n");
   7155   1.1  christos       else
   7156   1.1  christos 	uiout->message ("No watchpoint matching '%s'.\n", args);
   7157   1.8  christos     }
   7158   1.1  christos }
   7159   1.9  christos 
   7160   1.1  christos static void
   7161   1.1  christos maintenance_info_breakpoints (const char *args, int from_tty)
   7162   1.1  christos {
   7163   1.1  christos   breakpoint_1 (args, true, NULL);
   7164  1.10  christos 
   7165   1.1  christos   default_collect_info ();
   7166   1.1  christos }
   7167   1.1  christos 
   7168   1.1  christos static bool
   7169  1.11  christos breakpoint_has_pc (struct breakpoint *b,
   7170   1.1  christos 		   struct program_space *pspace,
   7171  1.11  christos 		   CORE_ADDR pc, struct obj_section *section)
   7172  1.11  christos {
   7173  1.11  christos   for (bp_location &bl : b->locations ())
   7174  1.10  christos     {
   7175   1.1  christos       if (bl.pspace == pspace
   7176  1.10  christos 	  && bl.address == pc
   7177   1.1  christos 	  && (!overlay_debugging || bl.section == section))
   7178   1.1  christos 	return true;
   7179  1.10  christos     }
   7180   1.1  christos   return false;
   7181  1.10  christos }
   7182   1.1  christos 
   7183   1.1  christos /* See breakpoint.h.  */
   7184   1.1  christos 
   7185   1.1  christos void
   7186   1.1  christos describe_other_breakpoints (struct gdbarch *gdbarch,
   7187   1.1  christos 			    struct program_space *pspace, CORE_ADDR pc,
   7188  1.11  christos 			    struct obj_section *section, int thread)
   7189  1.11  christos {
   7190  1.11  christos   int others = 0;
   7191  1.10  christos 
   7192   1.1  christos   for (breakpoint &b : all_breakpoints ())
   7193   1.1  christos     others += (user_breakpoint_p (&b)
   7194   1.1  christos 	       && breakpoint_has_pc (&b, pspace, pc, section));
   7195  1.10  christos 
   7196   1.1  christos   if (others > 0)
   7197  1.10  christos     {
   7198  1.11  christos       if (others == 1)
   7199  1.11  christos 	gdb_printf (_("Note: breakpoint "));
   7200  1.11  christos       else /* if (others == ???) */
   7201   1.1  christos 	gdb_printf (_("Note: breakpoints "));
   7202   1.1  christos       for (breakpoint &b : all_breakpoints ())
   7203  1.11  christos 	if (user_breakpoint_p (&b)
   7204  1.11  christos 	    && breakpoint_has_pc (&b, pspace, pc, section))
   7205  1.10  christos 	  {
   7206  1.11  christos 	    others--;
   7207  1.11  christos 	    gdb_printf ("%d", b.number);
   7208  1.11  christos 	    if (b.thread == -1 && thread != -1)
   7209  1.11  christos 	      gdb_printf (" (all threads)");
   7210  1.11  christos 	    else if (b.thread != -1)
   7211  1.11  christos 	      {
   7212  1.11  christos 		struct thread_info *thr = find_thread_global_id (b.thread);
   7213  1.10  christos 		gdb_printf (" (thread %s)", print_thread_id (thr));
   7214  1.11  christos 	      }
   7215  1.11  christos 	    else if (b.task != -1)
   7216  1.10  christos 	      gdb_printf (" (task %d)", b.task);
   7217  1.10  christos 	    gdb_printf ("%s%s ",
   7218  1.10  christos 			((b.enable_state == bp_disabled
   7219  1.10  christos 			  || b.enable_state == bp_call_disabled)
   7220   1.1  christos 			 ? " (disabled)"
   7221   1.9  christos 			 : ""),
   7222   1.9  christos 			(others > 1) ? ","
   7223   1.9  christos 			: ((others == 1) ? " and" : ""));
   7224   1.1  christos 	  }
   7225   1.1  christos       current_uiout->message (_("also set at pc %ps.\n"),
   7226   1.1  christos 			      styled_string (address_style.style (),
   7227   1.1  christos 					     paddress (gdbarch, pc)));
   7228   1.9  christos     }
   7229   1.9  christos }
   7230   1.9  christos 
   7231   1.9  christos 
   7233   1.9  christos /* Return true iff it is meaningful to use the address member of LOC.
   7234   1.9  christos    For some breakpoint types, the locations' address members are
   7235   1.9  christos    irrelevant and it makes no sense to attempt to compare them to
   7236   1.9  christos    other addresses (or use them for any other purpose either).
   7237   1.9  christos 
   7238   1.1  christos    More specifically, software watchpoints and catchpoints that are
   7239   1.9  christos    not backed by breakpoints always have a zero valued location
   7240   1.9  christos    address and we don't want to mark breakpoints of any of these types
   7241   1.1  christos    to be a duplicate of an actual breakpoint location at address
   7242   1.9  christos    zero.  */
   7243   1.1  christos 
   7244   1.1  christos static bool
   7245   1.1  christos bl_address_is_meaningful (bp_location *loc)
   7246   1.1  christos {
   7247   1.1  christos   return loc->loc_type != bp_loc_other;
   7248  1.10  christos }
   7249  1.10  christos 
   7250  1.10  christos /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
   7251   1.1  christos    true if LOC1 and LOC2 represent the same watchpoint location.  */
   7252  1.11  christos 
   7253  1.11  christos static bool
   7254   1.1  christos watchpoint_locations_match (const struct bp_location *loc1,
   7255   1.1  christos 			    const struct bp_location *loc2)
   7256   1.1  christos {
   7257   1.1  christos   watchpoint *w1 = gdb::checked_static_cast<watchpoint *> (loc1->owner);
   7258   1.1  christos   watchpoint *w2 = gdb::checked_static_cast<watchpoint *> (loc2->owner);
   7259   1.1  christos 
   7260   1.1  christos   /* Both of them must exist.  */
   7261   1.1  christos   gdb_assert (w1 != NULL);
   7262   1.1  christos   gdb_assert (w2 != NULL);
   7263   1.1  christos 
   7264   1.1  christos   /* If the target can evaluate the condition expression in hardware,
   7265   1.1  christos      then we we need to insert both watchpoints even if they are at
   7266   1.1  christos      the same place.  Otherwise the watchpoint will only trigger when
   7267   1.1  christos      the condition of whichever watchpoint was inserted evaluates to
   7268   1.1  christos      true, not giving a chance for GDB to check the condition of the
   7269   1.7  christos      other watchpoint.  */
   7270   1.1  christos   if ((w1->cond_exp
   7271   1.1  christos        && target_can_accel_watchpoint_condition (loc1->address,
   7272   1.1  christos 						 loc1->length,
   7273   1.1  christos 						 loc1->watchpoint_type,
   7274   1.7  christos 						 w1->cond_exp.get ()))
   7275  1.10  christos       || (w2->cond_exp
   7276   1.1  christos 	  && target_can_accel_watchpoint_condition (loc2->address,
   7277   1.1  christos 						    loc2->length,
   7278   1.1  christos 						    loc2->watchpoint_type,
   7279   1.1  christos 						    w2->cond_exp.get ())))
   7280   1.1  christos     return false;
   7281   1.1  christos 
   7282   1.1  christos   /* Note that this checks the owner's type, not the location's.  In
   7283   1.1  christos      case the target does not support read watchpoints, but does
   7284   1.1  christos      support access watchpoints, we'll have bp_read_watchpoint
   7285   1.1  christos      watchpoints with hw_access locations.  Those should be considered
   7286   1.1  christos      duplicates of hw_read locations.  The hw_read locations will
   7287   1.1  christos      become hw_access locations later.  */
   7288   1.1  christos   return (loc1->owner->type == loc2->owner->type
   7289   1.3  christos 	  && loc1->pspace->aspace == loc2->pspace->aspace
   7290   1.1  christos 	  && loc1->address == loc2->address
   7291   1.3  christos 	  && loc1->length == loc2->length);
   7292   1.8  christos }
   7293   1.8  christos 
   7294   1.1  christos /* See breakpoint.h.  */
   7295  1.11  christos 
   7296   1.1  christos int
   7297   1.1  christos breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
   7298   1.1  christos 			  const address_space *aspace2, CORE_ADDR addr2)
   7299   1.1  christos {
   7300   1.1  christos   return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
   7301   1.1  christos 	   || aspace1 == aspace2)
   7302   1.1  christos 	  && addr1 == addr2);
   7303   1.1  christos }
   7304   1.1  christos 
   7305  1.10  christos /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
   7306   1.8  christos    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
   7307   1.8  christos    matches ASPACE2.  On targets that have global breakpoints, the address
   7308   1.8  christos    space doesn't really matter.  */
   7309   1.1  christos 
   7310   1.1  christos static bool
   7311  1.11  christos breakpoint_address_match_range (const address_space *aspace1,
   7312   1.1  christos 				CORE_ADDR addr1,
   7313   1.1  christos 				int len1, const address_space *aspace2,
   7314   1.1  christos 				CORE_ADDR addr2)
   7315   1.1  christos {
   7316   1.1  christos   return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
   7317   1.1  christos 	   || aspace1 == aspace2)
   7318   1.1  christos 	  && addr2 >= addr1 && addr2 < addr1 + len1);
   7319   1.1  christos }
   7320   1.1  christos 
   7321  1.10  christos /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
   7322   1.1  christos    a ranged breakpoint.  In most targets, a match happens only if ASPACE
   7323   1.8  christos    matches the breakpoint's address space.  On targets that have global
   7324   1.1  christos    breakpoints, the address space doesn't really matter.  */
   7325   1.1  christos 
   7326  1.11  christos static bool
   7327   1.1  christos breakpoint_location_address_match (struct bp_location *bl,
   7328   1.1  christos 				   const address_space *aspace,
   7329  1.11  christos 				   CORE_ADDR addr)
   7330   1.1  christos {
   7331   1.1  christos   return (breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
   7332   1.1  christos 				    aspace, addr)
   7333   1.1  christos 	  || (bl->length
   7334   1.6  christos 	      && breakpoint_address_match_range (bl->pspace->aspace.get (),
   7335   1.6  christos 						 bl->address, bl->length,
   7336   1.6  christos 						 aspace, addr)));
   7337   1.6  christos }
   7338   1.6  christos 
   7339   1.6  christos /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
   7340  1.10  christos    breakpoint BL.  BL may be a ranged breakpoint.  In most targets, a
   7341   1.6  christos    match happens only if ASPACE matches the breakpoint's address
   7342   1.8  christos    space.  On targets that have global breakpoints, the address space
   7343   1.6  christos    doesn't really matter.  */
   7344   1.6  christos 
   7345  1.11  christos static bool
   7346  1.11  christos breakpoint_location_address_range_overlap (struct bp_location *bl,
   7347   1.6  christos 					   const address_space *aspace,
   7348   1.6  christos 					   CORE_ADDR addr, int len)
   7349   1.6  christos {
   7350   1.6  christos   if (gdbarch_has_global_breakpoints (current_inferior ()->arch ())
   7351   1.6  christos       || bl->pspace->aspace.get () == aspace)
   7352   1.6  christos     {
   7353   1.6  christos       int bl_len = bl->length != 0 ? bl->length : 1;
   7354   1.6  christos 
   7355   1.6  christos       if (mem_ranges_overlap (addr, len, bl->address, bl_len))
   7356   1.1  christos 	return 1;
   7357   1.1  christos     }
   7358   1.1  christos   return 0;
   7359   1.1  christos }
   7360  1.10  christos 
   7361  1.10  christos /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
   7362  1.10  christos    Then, if LOC1 and LOC2 represent the same tracepoint location, returns
   7363   1.1  christos    true, otherwise returns false.  */
   7364   1.1  christos 
   7365   1.1  christos static bool
   7366   1.1  christos tracepoint_locations_match (const struct bp_location *loc1,
   7367   1.1  christos 			    const struct bp_location *loc2)
   7368   1.1  christos {
   7369   1.1  christos   if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
   7370  1.10  christos     /* Since tracepoint locations are never duplicated with others', tracepoint
   7371   1.1  christos        locations at the same address of different tracepoints are regarded as
   7372   1.1  christos        different locations.  */
   7373   1.1  christos     return (loc1->address == loc2->address && loc1->owner == loc2->owner);
   7374   1.9  christos   else
   7375   1.9  christos     return false;
   7376   1.9  christos }
   7377   1.9  christos 
   7378   1.1  christos /* Assuming LOC1 and LOC2's types' have meaningful target addresses
   7379  1.10  christos    (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
   7380  1.10  christos    the same location.  If SW_HW_BPS_MATCH is true, then software
   7381  1.10  christos    breakpoint locations and hardware breakpoint locations match,
   7382   1.9  christos    otherwise they don't.  */
   7383   1.1  christos 
   7384   1.1  christos static bool
   7385   1.1  christos breakpoint_locations_match (const struct bp_location *loc1,
   7386   1.1  christos 			    const struct bp_location *loc2,
   7387   1.1  christos 			    bool sw_hw_bps_match)
   7388   1.1  christos {
   7389   1.1  christos   int hw_point1, hw_point2;
   7390   1.1  christos 
   7391   1.1  christos   /* Both of them must not be in moribund_locations.  */
   7392   1.1  christos   gdb_assert (loc1->owner != NULL);
   7393   1.1  christos   gdb_assert (loc2->owner != NULL);
   7394  1.10  christos 
   7395   1.1  christos   hw_point1 = is_hardware_watchpoint (loc1->owner);
   7396   1.1  christos   hw_point2 = is_hardware_watchpoint (loc2->owner);
   7397   1.1  christos 
   7398   1.1  christos   if (hw_point1 != hw_point2)
   7399   1.1  christos     return false;
   7400   1.9  christos   else if (hw_point1)
   7401   1.9  christos     return watchpoint_locations_match (loc1, loc2);
   7402   1.9  christos   else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
   7403  1.11  christos     return tracepoint_locations_match (loc1, loc2);
   7404  1.11  christos   else
   7405  1.11  christos     /* We compare bp_location.length in order to cover ranged
   7406  1.11  christos        breakpoints.  Keep this in sync with
   7407   1.9  christos        bp_location_is_less_than.  */
   7408   1.1  christos     return (breakpoint_address_match (loc1->pspace->aspace.get (),
   7409   1.1  christos 				      loc1->address,
   7410   1.1  christos 				      loc2->pspace->aspace.get (),
   7411   1.1  christos 				      loc2->address)
   7412   1.1  christos 	    && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
   7413  1.10  christos 	    && loc1->length == loc2->length);
   7414   1.1  christos }
   7415   1.1  christos 
   7416   1.1  christos static void
   7417   1.1  christos breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
   7418   1.1  christos 			       int bnum, bool have_bnum)
   7419   1.1  christos {
   7420   1.1  christos   /* The longest string possibly returned by hex_string_custom
   7421   1.1  christos      is 50 chars.  These must be at least that big for safety.  */
   7422   1.1  christos   char astr1[64];
   7423   1.1  christos   char astr2[64];
   7424  1.10  christos 
   7425   1.1  christos   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
   7426   1.1  christos   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
   7427   1.1  christos   if (have_bnum)
   7428   1.1  christos     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
   7429   1.1  christos 	     bnum, astr1, astr2);
   7430   1.1  christos   else
   7431   1.1  christos     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
   7432   1.1  christos }
   7433   1.1  christos 
   7434   1.1  christos /* Adjust a breakpoint's address to account for architectural
   7435   1.1  christos    constraints on breakpoint placement.  Return the adjusted address.
   7436  1.10  christos    Note: Very few targets require this kind of adjustment.  For most
   7437  1.10  christos    targets, this function is simply the identity function.  */
   7438   1.1  christos 
   7439  1.10  christos static CORE_ADDR
   7440  1.10  christos adjust_breakpoint_address (struct gdbarch *gdbarch,
   7441   1.8  christos 			   CORE_ADDR bpaddr, enum bptype bptype,
   7442   1.8  christos 			   struct program_space *pspace)
   7443   1.8  christos {
   7444   1.8  christos   gdb_assert (pspace != nullptr);
   7445   1.8  christos 
   7446   1.1  christos   if (bptype == bp_watchpoint
   7447   1.1  christos       || bptype == bp_hardware_watchpoint
   7448  1.10  christos       || bptype == bp_read_watchpoint
   7449   1.1  christos       || bptype == bp_access_watchpoint
   7450   1.1  christos       || bptype == bp_catchpoint)
   7451   1.3  christos     {
   7452   1.3  christos       /* Watchpoints and the various bp_catch_* eventpoints should not
   7453   1.3  christos 	 have their addresses modified.  */
   7454   1.3  christos       return bpaddr;
   7455   1.3  christos     }
   7456   1.3  christos   else if (bptype == bp_single_step)
   7457   1.3  christos     {
   7458   1.3  christos       /* Single-step breakpoints should not have their addresses
   7459   1.3  christos 	 modified.  If there's any architectural constrain that
   7460   1.3  christos 	 applies to this address, then it should have already been
   7461   1.1  christos 	 taken into account when the breakpoint was created in the
   7462   1.1  christos 	 first place.  If we didn't do this, stepping through e.g.,
   7463   1.8  christos 	 Thumb-2 IT blocks would break.  */
   7464   1.8  christos       return bpaddr;
   7465  1.10  christos     }
   7466  1.10  christos   else
   7467   1.8  christos     {
   7468   1.8  christos       CORE_ADDR adjusted_bpaddr = bpaddr;
   7469  1.10  christos 
   7470  1.10  christos       /* Some targets have architectural constraints on the placement
   7471  1.10  christos 	 of breakpoint instructions.  Obtain the adjusted address.  */
   7472  1.10  christos       if (gdbarch_adjust_breakpoint_address_p (gdbarch))
   7473  1.10  christos 	{
   7474  1.10  christos 	  /* Targets that implement this adjustment function will likely
   7475  1.10  christos 	     inspect either the symbol table, target memory at BPADDR, or
   7476  1.10  christos 	     even state registers, so ensure a suitable thread (and its
   7477   1.8  christos 	     associated program space) are currently selected.  */
   7478   1.1  christos 	  scoped_restore_current_pspace_and_thread restore_pspace_thread;
   7479  1.10  christos 	  switch_to_program_space_and_thread (pspace);
   7480  1.10  christos 	  adjusted_bpaddr
   7481   1.1  christos 	    = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
   7482   1.1  christos 	}
   7483  1.10  christos 
   7484   1.1  christos       adjusted_bpaddr
   7485   1.1  christos 	= gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
   7486  1.10  christos 
   7487   1.1  christos       /* An adjusted breakpoint address can significantly alter
   7488   1.1  christos 	 a user's expectations.  Print a warning if an adjustment
   7489   1.1  christos 	 is required.  */
   7490   1.1  christos       if (adjusted_bpaddr != bpaddr)
   7491   1.1  christos 	breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
   7492   1.9  christos 
   7493   1.9  christos       return adjusted_bpaddr;
   7494   1.1  christos     }
   7495   1.9  christos }
   7496   1.1  christos 
   7497   1.1  christos static bp_loc_type
   7498   1.3  christos bp_location_from_bp_type (bptype type)
   7499   1.1  christos {
   7500   1.1  christos   switch (type)
   7501   1.1  christos     {
   7502   1.1  christos     case bp_breakpoint:
   7503   1.1  christos     case bp_single_step:
   7504   1.1  christos     case bp_until:
   7505   1.1  christos     case bp_finish:
   7506   1.1  christos     case bp_longjmp:
   7507   1.1  christos     case bp_longjmp_resume:
   7508   1.1  christos     case bp_longjmp_call_dummy:
   7509   1.1  christos     case bp_exception:
   7510   1.1  christos     case bp_exception_resume:
   7511   1.1  christos     case bp_step_resume:
   7512   1.1  christos     case bp_hp_step_resume:
   7513   1.1  christos     case bp_watchpoint_scope:
   7514   1.1  christos     case bp_call_dummy:
   7515   1.1  christos     case bp_std_terminate:
   7516   1.1  christos     case bp_shlib_event:
   7517   1.1  christos     case bp_thread_event:
   7518   1.1  christos     case bp_overlay_event:
   7519   1.1  christos     case bp_jit_event:
   7520   1.1  christos     case bp_longjmp_master:
   7521   1.9  christos     case bp_std_terminate_master:
   7522  1.11  christos     case bp_exception_master:
   7523   1.1  christos     case bp_gnu_ifunc_resolver:
   7524   1.9  christos     case bp_gnu_ifunc_resolver_return:
   7525  1.11  christos     case bp_dprintf:
   7526   1.1  christos       return bp_loc_software_breakpoint;
   7527   1.1  christos 
   7528   1.1  christos     case bp_hardware_breakpoint:
   7529   1.9  christos       return bp_loc_hardware_breakpoint;
   7530  1.11  christos 
   7531   1.1  christos     case bp_hardware_watchpoint:
   7532  1.10  christos     case bp_read_watchpoint:
   7533  1.11  christos     case bp_access_watchpoint:
   7534   1.1  christos       return bp_loc_hardware_watchpoint;
   7535   1.1  christos 
   7536   1.1  christos     case bp_watchpoint:
   7537  1.10  christos       return bp_loc_software_watchpoint;
   7538  1.11  christos 
   7539  1.11  christos     case bp_tracepoint:
   7540  1.11  christos     case bp_fast_tracepoint:
   7541   1.9  christos     case bp_static_tracepoint:
   7542  1.11  christos     case bp_static_marker_tracepoint:
   7543   1.1  christos       return bp_loc_tracepoint;
   7544  1.10  christos 
   7545   1.1  christos     case bp_catchpoint:
   7546   1.9  christos       return bp_loc_other;
   7547   1.1  christos 
   7548   1.9  christos     default:
   7549   1.9  christos       internal_error (_("unknown breakpoint type"));
   7550   1.9  christos     }
   7551   1.9  christos }
   7552   1.9  christos 
   7553   1.9  christos bp_location::bp_location (breakpoint *owner, bp_loc_type type)
   7554  1.10  christos {
   7555   1.9  christos   this->owner = owner;
   7556   1.9  christos   this->cond_bytecode = NULL;
   7557   1.9  christos   this->shlib_disabled = 0;
   7558   1.9  christos   this->enabled = 1;
   7559   1.9  christos   this->disabled_by_cond = false;
   7560   1.9  christos 
   7561   1.9  christos   this->loc_type = type;
   7562  1.10  christos 
   7563   1.9  christos   if (this->loc_type == bp_loc_software_breakpoint
   7564   1.9  christos       || this->loc_type == bp_loc_hardware_breakpoint)
   7565   1.9  christos     mark_breakpoint_location_modified (this);
   7566   1.9  christos 
   7567   1.9  christos   incref ();
   7568   1.9  christos }
   7569   1.1  christos 
   7570   1.1  christos bp_location::bp_location (breakpoint *owner)
   7571  1.11  christos   : bp_location::bp_location (owner,
   7572  1.11  christos 			      bp_location_from_bp_type (owner->type))
   7573  1.11  christos {
   7574  1.11  christos }
   7575  1.11  christos 
   7576  1.11  christos /* See breakpoint.h.  */
   7577  1.11  christos 
   7578  1.11  christos std::string
   7579  1.11  christos bp_location::to_string () const
   7580  1.11  christos {
   7581  1.11  christos   string_file stb;
   7582   1.1  christos   ui_out_redirect_pop redir (current_uiout, &stb);
   7583   1.1  christos   print_breakpoint_location (this->owner, this);
   7584   1.1  christos   return stb.release ();
   7585   1.1  christos }
   7586   1.1  christos 
   7587   1.1  christos /* Decrement reference count.  If the reference count reaches 0,
   7588  1.10  christos    destroy the bp_location.  Sets *BLP to NULL.  */
   7589   1.1  christos 
   7590   1.1  christos static void
   7591   1.1  christos decref_bp_location (struct bp_location **blp)
   7592   1.1  christos {
   7593   1.1  christos   bp_location_ref_policy::decref (*blp);
   7594   1.8  christos   *blp = NULL;
   7595   1.8  christos }
   7596   1.1  christos 
   7597   1.1  christos /* Add breakpoint B at the end of the global breakpoint chain.  */
   7598   1.1  christos 
   7599   1.1  christos static breakpoint *
   7600  1.11  christos add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
   7601   1.8  christos {
   7602  1.11  christos   /* Add this breakpoint to the end of the chain so that a list of
   7603   1.1  christos      breakpoints will come out in order of increasing numbers.  */
   7604   1.1  christos 
   7605   1.9  christos   breakpoint_chain.push_back (*b.release ());
   7606   1.1  christos 
   7607   1.1  christos   return &breakpoint_chain.back ();
   7608   1.9  christos }
   7609   1.1  christos 
   7610   1.1  christos /* Initialize loc->function_name.  */
   7611   1.1  christos 
   7612   1.1  christos static void
   7613   1.1  christos set_breakpoint_location_function (struct bp_location *loc)
   7614   1.1  christos {
   7615   1.1  christos   gdb_assert (loc->owner != NULL);
   7616   1.1  christos 
   7617   1.1  christos   if (loc->owner->type == bp_breakpoint
   7618   1.8  christos       || loc->owner->type == bp_hardware_breakpoint
   7619  1.10  christos       || is_tracepoint (loc->owner))
   7620  1.10  christos     {
   7621   1.1  christos       const char *function_name;
   7622   1.1  christos 
   7623   1.1  christos       if (loc->msymbol != NULL
   7624   1.9  christos 	  && (loc->msymbol->type () == mst_text_gnu_ifunc
   7625   1.8  christos 	      || loc->msymbol->type () == mst_data_gnu_ifunc))
   7626  1.11  christos 	{
   7627  1.11  christos 	  struct breakpoint *b = loc->owner;
   7628  1.11  christos 
   7629   1.1  christos 	  function_name = loc->msymbol->linkage_name ();
   7630   1.1  christos 
   7631   1.1  christos 	  if (b->type == bp_breakpoint
   7632   1.1  christos 	      && b->has_single_location ()
   7633   1.1  christos 	      && b->related_breakpoint == b)
   7634  1.10  christos 	    {
   7635   1.8  christos 	      /* Create only the whole new breakpoint of this type but do not
   7636   1.1  christos 		 mess more complicated breakpoints with multiple locations.  */
   7637   1.1  christos 	      b->type = bp_gnu_ifunc_resolver;
   7638   1.8  christos 	      /* Remember the resolver's address for use by the return
   7639   1.8  christos 		 breakpoint.  */
   7640   1.1  christos 	      loc->related_address = loc->address;
   7641   1.1  christos 	    }
   7642  1.10  christos 	}
   7643   1.1  christos       else
   7644   1.1  christos 	find_pc_partial_function (loc->address, &function_name, NULL, NULL);
   7645   1.1  christos 
   7646   1.1  christos       if (function_name)
   7647   1.1  christos 	loc->function_name = make_unique_xstrdup (function_name);
   7648   1.1  christos     }
   7649   1.1  christos }
   7650   1.1  christos 
   7651   1.9  christos /* Attempt to determine architecture of location identified by SAL.  */
   7652   1.1  christos struct gdbarch *
   7653  1.10  christos get_sal_arch (struct symtab_and_line sal)
   7654   1.1  christos {
   7655   1.1  christos   if (sal.section)
   7656   1.1  christos     return sal.section->objfile->arch ();
   7657   1.1  christos   if (sal.symtab)
   7658   1.1  christos     return sal.symtab->compunit ()->objfile ()->arch ();
   7659   1.1  christos 
   7660   1.1  christos   return NULL;
   7661   1.1  christos }
   7662   1.1  christos 
   7663   1.1  christos /* Call this routine when stepping and nexting to enable a breakpoint
   7664   1.1  christos    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
   7665   1.6  christos    initiated the operation.  */
   7666   1.1  christos 
   7667   1.1  christos void
   7668   1.1  christos set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
   7669   1.1  christos {
   7670   1.1  christos   int thread = tp->global_num;
   7671  1.11  christos 
   7672  1.11  christos   /* To avoid having to rescan all objfile symbols at every step,
   7673  1.11  christos      we maintain a list of continually-inserted but always disabled
   7674  1.11  christos      longjmp "master" breakpoints.  Here, we simply create momentary
   7675   1.1  christos      clones of those and enable them for the requested thread.  */
   7676  1.11  christos   for (breakpoint &b : all_breakpoints_safe ())
   7677   1.1  christos     if (b.pspace == current_program_space
   7678   1.1  christos 	&& (b.type == bp_longjmp_master
   7679  1.11  christos 	    || b.type == bp_exception_master))
   7680   1.1  christos       {
   7681   1.1  christos 	bptype type = b.type == bp_longjmp_master ? bp_longjmp : bp_exception;
   7682   1.1  christos 	/* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
   7683   1.1  christos 	   after their removal.  */
   7684   1.1  christos 	momentary_breakpoint_from_master (&b, type, 1, thread);
   7685   1.1  christos       }
   7686   1.1  christos 
   7687   1.1  christos   tp->initiating_frame = frame;
   7688   1.1  christos }
   7689  1.11  christos 
   7690  1.11  christos /* Delete all longjmp breakpoints from THREAD.  */
   7691   1.1  christos void
   7692  1.11  christos delete_longjmp_breakpoint (int thread)
   7693  1.11  christos {
   7694  1.11  christos   for (breakpoint &b : all_breakpoints_safe ())
   7695  1.11  christos     if (b.type == bp_longjmp || b.type == bp_exception)
   7696  1.11  christos       {
   7697   1.1  christos 	if (b.thread == thread)
   7698   1.1  christos 	  {
   7699   1.1  christos 	    gdb_assert (b.inferior == -1);
   7700   1.1  christos 	    delete_breakpoint (&b);
   7701   1.1  christos 	  }
   7702   1.1  christos       }
   7703  1.11  christos }
   7704  1.11  christos 
   7705   1.1  christos void
   7706  1.11  christos delete_longjmp_breakpoint_at_next_stop (int thread)
   7707  1.11  christos {
   7708  1.11  christos   for (breakpoint &b : all_breakpoints_safe ())
   7709  1.11  christos     if (b.type == bp_longjmp || b.type == bp_exception)
   7710  1.11  christos       {
   7711   1.1  christos 	if (b.thread == thread)
   7712   1.1  christos 	  {
   7713   1.1  christos 	    gdb_assert (b.inferior == -1);
   7714   1.1  christos 	    b.disposition = disp_del_at_next_stop;
   7715   1.1  christos 	  }
   7716   1.1  christos       }
   7717   1.1  christos }
   7718   1.1  christos 
   7719   1.1  christos /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
   7720   1.1  christos    INFERIOR_PTID thread.  Chain them all by RELATED_BREAKPOINT and return
   7721   1.1  christos    pointer to any of them.  Return NULL if this system cannot place longjmp
   7722  1.10  christos    breakpoints.  */
   7723   1.1  christos 
   7724  1.11  christos struct breakpoint *
   7725  1.11  christos set_longjmp_breakpoint_for_call_dummy (void)
   7726   1.1  christos {
   7727  1.10  christos   breakpoint *retval = nullptr;
   7728  1.10  christos 
   7729  1.11  christos   for (breakpoint &b : all_breakpoints ())
   7730  1.10  christos     if (b.pspace == current_program_space && b.type == bp_longjmp_master)
   7731   1.1  christos       {
   7732   1.1  christos 	int thread = inferior_thread ()->global_num;
   7733   1.1  christos 	breakpoint *new_b
   7734   1.1  christos 	  = momentary_breakpoint_from_master (&b, bp_longjmp_call_dummy,
   7735   1.1  christos 					      1, thread);
   7736   1.1  christos 
   7737   1.1  christos 	/* Link NEW_B into the chain of RETVAL breakpoints.  */
   7738   1.1  christos 
   7739   1.1  christos 	gdb_assert (new_b->related_breakpoint == new_b);
   7740   1.1  christos 	if (retval == NULL)
   7741   1.1  christos 	  retval = new_b;
   7742   1.1  christos 	new_b->related_breakpoint = retval;
   7743   1.1  christos 	while (retval->related_breakpoint != new_b->related_breakpoint)
   7744   1.1  christos 	  retval = retval->related_breakpoint;
   7745   1.1  christos 	retval->related_breakpoint = new_b;
   7746   1.1  christos       }
   7747   1.3  christos 
   7748   1.1  christos   return retval;
   7749   1.1  christos }
   7750  1.10  christos 
   7751  1.10  christos /* Verify all existing dummy frames and their associated breakpoints for
   7752  1.10  christos    TP.  Remove those which can no longer be found in the current frame
   7753  1.10  christos    stack.
   7754   1.1  christos 
   7755   1.1  christos    If the unwind fails then there is not sufficient information to discard
   7756   1.3  christos    dummy frames.  In this case, elide the clean up and the dummy frames will
   7757   1.1  christos    be cleaned up next time this function is called from a location where
   7758  1.11  christos    unwinding is possible.  */
   7759  1.11  christos 
   7760  1.11  christos void
   7761  1.11  christos check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
   7762  1.11  christos {
   7763  1.11  christos   /* We would need to delete breakpoints other than the current one while
   7764  1.11  christos      iterating, so all_breakpoints_safe is not sufficient to make that safe.
   7765  1.11  christos      Save all breakpoints to delete in that set and delete them at the end.  */
   7766  1.11  christos   std::unordered_set<breakpoint *> to_delete;
   7767  1.11  christos 
   7768  1.11  christos   for (struct breakpoint &b : all_breakpoints ())
   7769  1.11  christos     {
   7770  1.11  christos       if (b.type == bp_longjmp_call_dummy && b.thread == tp->global_num)
   7771  1.11  christos 	{
   7772  1.11  christos 	  gdb_assert (b.inferior == -1);
   7773  1.11  christos 	  struct breakpoint *dummy_b = b.related_breakpoint;
   7774  1.11  christos 
   7775  1.11  christos 	  /* Find the bp_call_dummy breakpoint in the list of breakpoints
   7776  1.11  christos 	     chained off b->related_breakpoint.  */
   7777  1.11  christos 	  while (dummy_b != &b && dummy_b->type != bp_call_dummy)
   7778  1.11  christos 	    dummy_b = dummy_b->related_breakpoint;
   7779  1.11  christos 
   7780  1.11  christos 	  /* If there was no bp_call_dummy breakpoint then there's nothing
   7781  1.11  christos 	     more to do.  Or, if the dummy frame associated with the
   7782   1.1  christos 	     bp_call_dummy is still on the stack then we need to leave this
   7783  1.11  christos 	     bp_call_dummy in place.  */
   7784  1.11  christos 	  if (dummy_b->type != bp_call_dummy
   7785  1.11  christos 	      || frame_find_by_id (dummy_b->frame_id) != NULL)
   7786  1.11  christos 	    continue;
   7787  1.11  christos 
   7788  1.11  christos 	  /* We didn't find the dummy frame on the stack, this could be
   7789  1.11  christos 	     because we have longjmp'd to a stack frame that is previous to
   7790  1.11  christos 	     the dummy frame, or it could be because the stack unwind is
   7791  1.11  christos 	     broken at some point between the longjmp frame and the dummy
   7792  1.11  christos 	     frame.
   7793  1.11  christos 
   7794  1.11  christos 	     Next we figure out why the stack unwind stopped.  If it looks
   7795  1.11  christos 	     like the unwind is complete then we assume the dummy frame has
   7796  1.11  christos 	     been jumped over, however, if the unwind stopped for an
   7797  1.11  christos 	     unexpected reason then we assume the stack unwind is currently
   7798  1.11  christos 	     broken, and that we will (eventually) return to the dummy
   7799  1.11  christos 	     frame.
   7800  1.11  christos 
   7801  1.11  christos 	     It might be tempting to consider using frame_id_inner here, but
   7802  1.11  christos 	     that is not safe.   There is no guarantee that the stack frames
   7803  1.11  christos 	     we are looking at here are even on the same stack as the
   7804  1.11  christos 	     original dummy frame, hence frame_id_inner can't be used.  See
   7805  1.11  christos 	     the comments on frame_id_inner for more details.  */
   7806  1.11  christos 	  bool unwind_finished_unexpectedly = false;
   7807  1.11  christos 	  for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
   7808  1.11  christos 	    {
   7809  1.11  christos 	      frame_info_ptr prev = get_prev_frame (fi);
   7810  1.11  christos 	      if (prev == nullptr)
   7811  1.11  christos 		{
   7812  1.11  christos 		  /* FI is the last stack frame.  Why did this frame not
   7813  1.11  christos 		     unwind further?  */
   7814  1.11  christos 		  auto stop_reason = get_frame_unwind_stop_reason (fi);
   7815  1.11  christos 		  if (stop_reason != UNWIND_NO_REASON
   7816  1.11  christos 		      && stop_reason != UNWIND_OUTERMOST)
   7817  1.11  christos 		    unwind_finished_unexpectedly = true;
   7818   1.1  christos 		}
   7819  1.11  christos 	      fi = prev;
   7820  1.10  christos 	    }
   7821  1.11  christos 	  if (unwind_finished_unexpectedly)
   7822  1.11  christos 	    continue;
   7823  1.11  christos 
   7824  1.11  christos 	  dummy_frame_discard (dummy_b->frame_id, tp);
   7825  1.10  christos 
   7826  1.11  christos 	  for (breakpoint *related_breakpoint = b.related_breakpoint;
   7827  1.11  christos 	       related_breakpoint != &b;
   7828  1.11  christos 	       related_breakpoint = related_breakpoint->related_breakpoint)
   7829   1.1  christos 	    to_delete.insert (b.related_breakpoint);
   7830  1.11  christos 
   7831  1.11  christos 	  to_delete.insert (&b);
   7832   1.1  christos 	}
   7833   1.1  christos     }
   7834   1.1  christos 
   7835   1.1  christos   for (breakpoint *b : to_delete)
   7836   1.1  christos     delete_breakpoint (b);
   7837  1.11  christos }
   7838  1.11  christos 
   7839  1.10  christos void
   7840  1.11  christos enable_overlay_breakpoints (void)
   7841  1.10  christos {
   7842  1.10  christos   for (breakpoint &b : all_breakpoints ())
   7843  1.10  christos     if (b.type == bp_overlay_event)
   7844   1.1  christos       {
   7845   1.1  christos 	b.enable_state = bp_enabled;
   7846   1.1  christos 	update_global_location_list (UGLL_MAY_INSERT);
   7847   1.1  christos 	overlay_events_enabled = 1;
   7848   1.1  christos       }
   7849  1.11  christos }
   7850  1.11  christos 
   7851  1.10  christos void
   7852  1.11  christos disable_overlay_breakpoints (void)
   7853  1.10  christos {
   7854  1.10  christos   for (breakpoint &b : all_breakpoints ())
   7855  1.10  christos     if (b.type == bp_overlay_event)
   7856   1.1  christos       {
   7857   1.1  christos 	b.enable_state = bp_disabled;
   7858   1.1  christos 	update_global_location_list (UGLL_DONT_INSERT);
   7859   1.1  christos 	overlay_events_enabled = 0;
   7860   1.1  christos       }
   7861   1.1  christos }
   7862   1.1  christos 
   7863  1.11  christos /* Set an active std::terminate breakpoint for each std::terminate
   7864  1.11  christos    master breakpoint.  */
   7865  1.11  christos void
   7866   1.1  christos set_std_terminate_breakpoint (void)
   7867  1.11  christos {
   7868  1.10  christos   for (breakpoint &b : all_breakpoints_safe ())
   7869   1.1  christos     if (b.pspace == current_program_space
   7870   1.1  christos 	&& b.type == bp_std_terminate_master)
   7871   1.1  christos       {
   7872   1.1  christos 	momentary_breakpoint_from_master (&b, bp_std_terminate, 1,
   7873   1.1  christos 					  inferior_thread ()->global_num);
   7874   1.1  christos       }
   7875   1.1  christos }
   7876  1.11  christos 
   7877  1.11  christos /* Delete all the std::terminate breakpoints.  */
   7878  1.11  christos void
   7879   1.1  christos delete_std_terminate_breakpoint (void)
   7880   1.1  christos {
   7881   1.1  christos   for (breakpoint &b : all_breakpoints_safe ())
   7882   1.1  christos     if (b.type == bp_std_terminate)
   7883   1.1  christos       delete_breakpoint (&b);
   7884   1.1  christos }
   7885   1.1  christos 
   7886  1.10  christos struct breakpoint *
   7887   1.1  christos create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
   7888   1.1  christos {
   7889  1.10  christos   struct breakpoint *b;
   7890  1.11  christos 
   7891   1.1  christos   b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
   7892   1.3  christos 
   7893   1.1  christos   b->enable_state = bp_enabled;
   7894   1.1  christos   /* locspec has to be used or breakpoint_re_set will delete me.  */
   7895   1.1  christos   b->locspec = new_address_location_spec (b->first_loc ().address, NULL, 0);
   7896   1.1  christos 
   7897   1.1  christos   update_global_location_list_nothrow (UGLL_MAY_INSERT);
   7898   1.1  christos 
   7899   1.1  christos   return b;
   7900   1.1  christos }
   7901   1.1  christos 
   7902   1.1  christos struct lang_and_radix
   7903   1.1  christos   {
   7904   1.1  christos     enum language lang;
   7905   1.1  christos     int radix;
   7906   1.1  christos   };
   7907   1.1  christos 
   7908  1.10  christos /* Create a breakpoint for JIT code registration and unregistration.  */
   7909   1.1  christos 
   7910   1.1  christos struct breakpoint *
   7911   1.1  christos create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
   7912   1.1  christos {
   7913   1.1  christos   return create_internal_breakpoint (gdbarch, address, bp_jit_event);
   7914   1.1  christos }
   7915   1.1  christos 
   7916  1.11  christos /* Remove JIT code registration and unregistration breakpoint(s).  */
   7917  1.11  christos 
   7918  1.11  christos void
   7919  1.11  christos remove_jit_event_breakpoints (void)
   7920   1.1  christos {
   7921   1.1  christos   for (breakpoint &b : all_breakpoints_safe ())
   7922   1.1  christos     if (b.type == bp_jit_event
   7923   1.1  christos 	&& b.first_loc ().pspace == current_program_space)
   7924   1.1  christos       delete_breakpoint (&b);
   7925  1.11  christos }
   7926  1.11  christos 
   7927  1.11  christos void
   7928  1.11  christos remove_solib_event_breakpoints (void)
   7929   1.1  christos {
   7930   1.1  christos   for (breakpoint &b : all_breakpoints_safe ())
   7931   1.3  christos     if (b.type == bp_shlib_event
   7932   1.3  christos 	&& b.first_loc ().pspace == current_program_space)
   7933   1.3  christos       delete_breakpoint (&b);
   7934   1.3  christos }
   7935   1.3  christos 
   7936  1.11  christos /* See breakpoint.h.  */
   7937  1.11  christos 
   7938  1.11  christos void
   7939  1.11  christos remove_solib_event_breakpoints_at_next_stop (void)
   7940   1.3  christos {
   7941   1.3  christos   for (breakpoint &b : all_breakpoints_safe ())
   7942   1.3  christos     if (b.type == bp_shlib_event
   7943   1.3  christos 	&& b.first_loc ().pspace == current_program_space)
   7944   1.3  christos       b.disposition = disp_del_at_next_stop;
   7945   1.3  christos }
   7946   1.3  christos 
   7947   1.3  christos /* Helper for create_solib_event_breakpoint /
   7948   1.3  christos    create_and_insert_solib_event_breakpoint.  Allows specifying which
   7949   1.3  christos    INSERT_MODE to pass through to update_global_location_list.  */
   7950   1.3  christos 
   7951   1.3  christos static struct breakpoint *
   7952  1.10  christos create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
   7953   1.3  christos 				 enum ugll_insert_mode insert_mode)
   7954   1.3  christos {
   7955   1.3  christos   struct breakpoint *b;
   7956   1.3  christos 
   7957   1.1  christos   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
   7958   1.1  christos   update_global_location_list_nothrow (insert_mode);
   7959   1.1  christos   return b;
   7960   1.3  christos }
   7961   1.3  christos 
   7962   1.3  christos struct breakpoint *
   7963   1.3  christos create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
   7964   1.3  christos {
   7965   1.3  christos   return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
   7966   1.3  christos }
   7967   1.3  christos 
   7968   1.1  christos /* See breakpoint.h.  */
   7969   1.1  christos 
   7970   1.3  christos struct breakpoint *
   7971   1.3  christos create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
   7972   1.3  christos {
   7973  1.11  christos   struct breakpoint *b;
   7974   1.3  christos 
   7975   1.3  christos   /* Explicitly tell update_global_location_list to insert
   7976   1.3  christos      locations.  */
   7977   1.3  christos   b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
   7978   1.1  christos   if (!b->first_loc ().inserted)
   7979   1.1  christos     {
   7980   1.1  christos       delete_breakpoint (b);
   7981  1.11  christos       return NULL;
   7982   1.1  christos     }
   7983   1.1  christos   return b;
   7984  1.11  christos }
   7985   1.1  christos 
   7986  1.10  christos /* See breakpoint.h.  */
   7987  1.10  christos 
   7988  1.10  christos void
   7989  1.10  christos disable_breakpoints_in_shlibs (program_space *pspace)
   7990   1.1  christos {
   7991  1.10  christos   for (bp_location *loc : all_bp_locations ())
   7992  1.10  christos     {
   7993  1.10  christos       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
   7994  1.10  christos       struct breakpoint *b = loc->owner;
   7995  1.10  christos 
   7996  1.10  christos       /* We apply the check to all breakpoints, including disabled for
   7997  1.10  christos 	 those with loc->duplicate set.  This is so that when breakpoint
   7998  1.10  christos 	 becomes enabled, or the duplicate is removed, gdb will try to
   7999  1.10  christos 	 insert all breakpoints.  If we don't set shlib_disabled here,
   8000  1.11  christos 	 we'll try to insert those breakpoints and fail.  */
   8001  1.10  christos       if (((b->type == bp_breakpoint)
   8002  1.10  christos 	   || (b->type == bp_jit_event)
   8003  1.10  christos 	   || (b->type == bp_hardware_breakpoint)
   8004  1.10  christos 	   || (is_tracepoint (b)))
   8005  1.10  christos 	  && loc->pspace == pspace
   8006  1.10  christos 	  && !loc->shlib_disabled
   8007  1.10  christos 	  && solib_name_from_address (loc->pspace, loc->address)
   8008   1.1  christos 	  )
   8009   1.1  christos 	{
   8010   1.1  christos 	  loc->shlib_disabled = 1;
   8011   1.1  christos 	}
   8012   1.1  christos     }
   8013   1.1  christos }
   8014   1.1  christos 
   8015  1.11  christos /* Disable any breakpoints and tracepoints that are in SOLIB upon
   8016   1.1  christos    notification of unloaded_shlib.  Only apply to enabled breakpoints,
   8017  1.10  christos    disabled ones can just stay disabled.  */
   8018   1.1  christos 
   8019  1.10  christos static void
   8020  1.10  christos disable_breakpoints_in_unloaded_shlib (program_space *pspace, const solib &solib)
   8021  1.10  christos {
   8022  1.10  christos   bool disabled_shlib_breaks = false;
   8023   1.1  christos 
   8024  1.11  christos   for (bp_location *loc : all_bp_locations ())
   8025  1.10  christos     {
   8026  1.10  christos       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
   8027  1.10  christos       struct breakpoint *b = loc->owner;
   8028  1.10  christos 
   8029  1.10  christos       if (pspace == loc->pspace
   8030  1.10  christos 	  && !loc->shlib_disabled
   8031  1.10  christos 	  && (((b->type == bp_breakpoint
   8032  1.10  christos 		|| b->type == bp_jit_event
   8033  1.10  christos 		|| b->type == bp_hardware_breakpoint)
   8034  1.10  christos 	       && (loc->loc_type == bp_loc_hardware_breakpoint
   8035  1.10  christos 		   || loc->loc_type == bp_loc_software_breakpoint))
   8036  1.10  christos 	      || is_tracepoint (b))
   8037  1.10  christos 	  && solib_contains_address_p (solib, loc->address))
   8038  1.10  christos 	{
   8039  1.10  christos 	  loc->shlib_disabled = 1;
   8040  1.10  christos 	  /* At this point, we cannot rely on remove_breakpoint
   8041  1.11  christos 	     succeeding so we must mark the breakpoint as not inserted
   8042  1.10  christos 	     to prevent future errors occurring in remove_breakpoints.  */
   8043  1.10  christos 	  loc->inserted = 0;
   8044  1.10  christos 
   8045  1.10  christos 	  /* This may cause duplicate notifications for the same breakpoint.  */
   8046  1.10  christos 	  notify_breakpoint_modified (b);
   8047  1.10  christos 
   8048  1.11  christos 	  if (!disabled_shlib_breaks)
   8049  1.10  christos 	    {
   8050  1.10  christos 	      target_terminal::ours_for_output ();
   8051  1.10  christos 	      warning (_("Temporarily disabling breakpoints "
   8052  1.10  christos 			 "for unloaded shared library \"%s\""),
   8053   1.1  christos 		       solib.so_name.c_str ());
   8054   1.1  christos 	    }
   8055   1.1  christos 	  disabled_shlib_breaks = true;
   8056   1.1  christos 	}
   8057   1.1  christos     }
   8058   1.1  christos }
   8059   1.1  christos 
   8060   1.1  christos /* Disable any breakpoints and tracepoints in OBJFILE upon
   8061   1.1  christos    notification of free_objfile.  Only apply to enabled breakpoints,
   8062   1.3  christos    disabled ones can just stay disabled.  */
   8063   1.3  christos 
   8064   1.3  christos static void
   8065   1.3  christos disable_breakpoints_in_freed_objfile (struct objfile *objfile)
   8066   1.3  christos {
   8067   1.3  christos   /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
   8068   1.3  christos      managed by the user with add-symbol-file/remove-symbol-file.
   8069   1.3  christos      Similarly to how breakpoints in shared libraries are handled in
   8070   1.3  christos      response to "nosharedlibrary", mark breakpoints in such modules
   8071   1.3  christos      shlib_disabled so they end up uninserted on the next global
   8072   1.3  christos      location list update.  Shared libraries not loaded by the user
   8073   1.3  christos      aren't handled here -- they're already handled in
   8074   1.3  christos      disable_breakpoints_in_unloaded_shlib, called by solib.c's
   8075   1.1  christos      solib_unloaded observer.  We skip objfiles that are not
   8076   1.1  christos      OBJF_SHARED as those aren't considered dynamic objects (e.g. the
   8077  1.11  christos      main objfile).  */
   8078   1.1  christos   if ((objfile->flags & OBJF_SHARED) == 0
   8079  1.10  christos       || (objfile->flags & OBJF_USERLOADED) == 0)
   8080   1.1  christos     return;
   8081  1.11  christos 
   8082   1.1  christos   for (breakpoint &b : all_breakpoints ())
   8083   1.1  christos     {
   8084  1.11  christos       bool bp_modified = false;
   8085   1.1  christos 
   8086  1.11  christos       if (!is_breakpoint (&b) && !is_tracepoint (&b))
   8087   1.1  christos 	continue;
   8088  1.11  christos 
   8089  1.11  christos       for (bp_location &loc : b.locations ())
   8090   1.1  christos 	{
   8091   1.1  christos 	  CORE_ADDR loc_addr = loc.address;
   8092  1.11  christos 
   8093   1.1  christos 	  if (loc.loc_type != bp_loc_hardware_breakpoint
   8094   1.1  christos 	      && loc.loc_type != bp_loc_software_breakpoint)
   8095  1.11  christos 	    continue;
   8096   1.1  christos 
   8097   1.1  christos 	  if (loc.shlib_disabled != 0)
   8098  1.11  christos 	    continue;
   8099  1.11  christos 
   8100   1.1  christos 	  if (objfile->pspace != loc.pspace)
   8101   1.1  christos 	    continue;
   8102   1.1  christos 
   8103   1.1  christos 	  if (loc.loc_type != bp_loc_hardware_breakpoint
   8104  1.11  christos 	      && loc.loc_type != bp_loc_software_breakpoint)
   8105   1.3  christos 	    continue;
   8106   1.3  christos 
   8107   1.3  christos 	  if (is_addr_in_objfile (loc_addr, objfile))
   8108   1.3  christos 	    {
   8109   1.3  christos 	      loc.shlib_disabled = 1;
   8110   1.1  christos 	      /* At this point, we don't know whether the object was
   8111  1.11  christos 		 unmapped from the inferior or not, so leave the
   8112   1.1  christos 		 inserted flag alone.  We'll handle failure to
   8113  1.10  christos 		 uninsert quietly, in case the object was indeed
   8114   1.1  christos 		 unmapped.  */
   8115   1.1  christos 
   8116   1.1  christos 	      mark_breakpoint_location_modified (&loc);
   8117   1.1  christos 
   8118  1.11  christos 	      bp_modified = true;
   8119   1.1  christos 	    }
   8120   1.1  christos 	}
   8121   1.1  christos 
   8122  1.10  christos       if (bp_modified)
   8123   1.1  christos 	notify_breakpoint_modified (&b);
   8124  1.10  christos     }
   8125  1.10  christos }
   8126  1.10  christos 
   8127  1.10  christos /* See breakpoint.h.  */
   8128  1.10  christos 
   8129  1.10  christos breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
   8130  1.10  christos 			bool temp, const char *cond_string_)
   8131  1.10  christos   : type (bptype),
   8132  1.10  christos     disposition (temp ? disp_del : disp_donttouch),
   8133  1.10  christos     gdbarch (gdbarch_),
   8134  1.10  christos     language (current_language->la_language),
   8135   1.1  christos     input_radix (::input_radix),
   8136   1.1  christos     cond_string (cond_string_ != nullptr
   8137   1.1  christos 		 ? make_unique_xstrdup (cond_string_)
   8138  1.10  christos 		 : nullptr),
   8139   1.1  christos     related_breakpoint (this)
   8140  1.10  christos {
   8141  1.10  christos }
   8142  1.10  christos 
   8143   1.1  christos /* See breakpoint.h.  */
   8144  1.10  christos 
   8145  1.10  christos catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
   8146  1.10  christos 			const char *cond_string)
   8147   1.1  christos   : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
   8148   1.1  christos {
   8149  1.11  christos   add_dummy_location (this, current_program_space);
   8150  1.11  christos 
   8151  1.11  christos   pspace = current_program_space;
   8152  1.11  christos }
   8153  1.11  christos 
   8154  1.11  christos /* Notify interpreters and observers that breakpoint B was created.  */
   8155  1.11  christos 
   8156  1.11  christos static void
   8157  1.11  christos notify_breakpoint_created (breakpoint *b)
   8158  1.11  christos {
   8159  1.10  christos   interps_notify_breakpoint_created (b);
   8160   1.1  christos   gdb::observers::breakpoint_created.notify (b);
   8161  1.10  christos }
   8162  1.10  christos 
   8163  1.10  christos breakpoint *
   8164  1.10  christos install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
   8165  1.10  christos {
   8166  1.10  christos   breakpoint *b = add_to_breakpoint_chain (std::move (arg));
   8167  1.11  christos   set_breakpoint_number (internal, b);
   8168  1.11  christos   if (is_tracepoint (b))
   8169   1.1  christos     set_tracepoint_count (breakpoint_count);
   8170  1.10  christos   if (!internal)
   8171  1.10  christos     mention (b);
   8172  1.11  christos 
   8173  1.11  christos   notify_breakpoint_created (b);
   8174   1.1  christos 
   8175   1.1  christos   if (update_gll)
   8176  1.10  christos     update_global_location_list (UGLL_MAY_INSERT);
   8177  1.10  christos 
   8178  1.10  christos   return b;
   8179  1.10  christos }
   8180   1.1  christos 
   8181  1.11  christos static int
   8182  1.11  christos hw_breakpoint_used_count (void)
   8183  1.11  christos {
   8184  1.10  christos   int i = 0;
   8185  1.10  christos 
   8186  1.10  christos   for (breakpoint &b : all_breakpoints ())
   8187  1.11  christos     if (b.type == bp_hardware_breakpoint && breakpoint_enabled (&b))
   8188  1.10  christos       for (bp_location &bl : b.locations ())
   8189   1.1  christos 	{
   8190  1.10  christos 	  /* Special types of hardware breakpoints may use more than
   8191   1.1  christos 	     one register.  */
   8192   1.1  christos 	  i += b.resources_needed (&bl);
   8193  1.10  christos 	}
   8194  1.10  christos 
   8195   1.1  christos   return i;
   8196  1.10  christos }
   8197  1.10  christos 
   8198   1.1  christos /* Returns the resources B would use if it were a hardware
   8199  1.10  christos    watchpoint.  */
   8200   1.1  christos 
   8201  1.10  christos static int
   8202  1.10  christos hw_watchpoint_use_count (struct breakpoint *b)
   8203   1.1  christos {
   8204  1.11  christos   int i = 0;
   8205   1.1  christos 
   8206  1.10  christos   if (!breakpoint_enabled (b))
   8207  1.10  christos     return 0;
   8208  1.11  christos 
   8209   1.1  christos   for (bp_location &bl : b->locations ())
   8210   1.1  christos     {
   8211  1.10  christos       /* Special types of hardware watchpoints may use more than
   8212   1.1  christos 	 one register.  */
   8213   1.1  christos       i += b->resources_needed (&bl);
   8214  1.10  christos     }
   8215  1.10  christos 
   8216  1.10  christos   return i;
   8217  1.10  christos }
   8218   1.1  christos 
   8219  1.10  christos /* Returns the sum the used resources of all hardware watchpoints of
   8220   1.1  christos    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
   8221   1.1  christos    the sum of the used resources of all hardware watchpoints of other
   8222   1.1  christos    types _not_ TYPE.  */
   8223   1.1  christos 
   8224   1.1  christos static int
   8225   1.1  christos hw_watchpoint_used_count_others (struct breakpoint *except,
   8226  1.11  christos 				 enum bptype type, int *other_type_used)
   8227   1.1  christos {
   8228  1.11  christos   int i = 0;
   8229   1.1  christos 
   8230  1.11  christos   *other_type_used = 0;
   8231   1.1  christos   for (breakpoint &b : all_breakpoints ())
   8232   1.1  christos     {
   8233  1.11  christos       if (&b == except)
   8234  1.11  christos 	continue;
   8235  1.11  christos       if (!breakpoint_enabled (&b))
   8236   1.1  christos 	continue;
   8237   1.1  christos 
   8238   1.1  christos       if (b.type == type)
   8239   1.1  christos 	i += hw_watchpoint_use_count (&b);
   8240   1.1  christos       else if (is_hardware_watchpoint (&b))
   8241   1.1  christos 	*other_type_used = 1;
   8242   1.1  christos     }
   8243   1.1  christos 
   8244   1.1  christos   return i;
   8245  1.11  christos }
   8246  1.11  christos 
   8247   1.1  christos void
   8248  1.11  christos disable_watchpoints_before_interactive_call_start (void)
   8249   1.3  christos {
   8250   1.1  christos   for (breakpoint &b : all_breakpoints ())
   8251   1.1  christos     if (is_watchpoint (&b) && breakpoint_enabled (&b))
   8252   1.1  christos       {
   8253   1.1  christos 	b.enable_state = bp_call_disabled;
   8254   1.1  christos 	update_global_location_list (UGLL_DONT_INSERT);
   8255   1.1  christos       }
   8256  1.11  christos }
   8257  1.11  christos 
   8258   1.1  christos void
   8259  1.11  christos enable_watchpoints_after_interactive_call_stop (void)
   8260   1.3  christos {
   8261   1.1  christos   for (breakpoint &b : all_breakpoints ())
   8262   1.1  christos     if (is_watchpoint (&b) && b.enable_state == bp_call_disabled)
   8263   1.1  christos       {
   8264   1.1  christos 	b.enable_state = bp_enabled;
   8265   1.1  christos 	update_global_location_list (UGLL_MAY_INSERT);
   8266   1.1  christos       }
   8267   1.1  christos }
   8268   1.3  christos 
   8269   1.1  christos void
   8270   1.1  christos disable_breakpoints_before_startup (void)
   8271   1.1  christos {
   8272   1.1  christos   current_program_space->executing_startup = 1;
   8273   1.1  christos   update_global_location_list (UGLL_DONT_INSERT);
   8274   1.1  christos }
   8275   1.1  christos 
   8276   1.1  christos void
   8277   1.1  christos enable_breakpoints_after_startup (void)
   8278  1.10  christos {
   8279   1.3  christos   current_program_space->executing_startup = 0;
   8280  1.10  christos   breakpoint_re_set ();
   8281  1.10  christos }
   8282  1.10  christos 
   8283  1.10  christos /* Allocate a new momentary breakpoint.  */
   8284  1.10  christos 
   8285  1.10  christos template<typename... Arg>
   8286  1.10  christos static momentary_breakpoint *
   8287  1.10  christos new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
   8288  1.10  christos 			  Arg&&... args)
   8289  1.10  christos {
   8290  1.10  christos   if (type == bp_longjmp || type == bp_exception)
   8291   1.3  christos     return new longjmp_breakpoint (gdbarch, type,
   8292   1.3  christos 				   std::forward<Arg> (args)...);
   8293   1.3  christos   else
   8294   1.3  christos     return new momentary_breakpoint (gdbarch, type,
   8295   1.3  christos 				     std::forward<Arg> (args)...);
   8296   1.1  christos }
   8297   1.8  christos 
   8298   1.1  christos /* Set a momentary breakpoint of type TYPE at address specified by
   8299   1.1  christos    SAL.  If FRAME_ID is valid, the breakpoint is restricted to that
   8300   1.1  christos    frame.  */
   8301   1.1  christos 
   8302   1.1  christos breakpoint_up
   8303   1.1  christos set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
   8304   1.1  christos 			  struct frame_id frame_id, enum bptype type)
   8305  1.10  christos {
   8306  1.10  christos   /* If FRAME_ID is valid, it should be a real frame, not an inlined or
   8307  1.10  christos      tail-called one.  */
   8308  1.10  christos   gdb_assert (!frame_id_artificial_p (frame_id));
   8309  1.10  christos 
   8310   1.1  christos   std::unique_ptr<momentary_breakpoint> b
   8311  1.10  christos     (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
   8312   1.1  christos 			       inferior_thread ()->global_num));
   8313   1.3  christos 
   8314   1.1  christos   b->add_location (sal);
   8315  1.10  christos 
   8316   1.1  christos   breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
   8317   1.1  christos 
   8318   1.1  christos   update_global_location_list_nothrow (UGLL_MAY_INSERT);
   8319   1.3  christos 
   8320   1.3  christos   return bp;
   8321   1.1  christos }
   8322   1.1  christos 
   8323   1.1  christos /* Make a momentary breakpoint based on the master breakpoint ORIG.
   8324   1.1  christos    The new breakpoint will have type TYPE, use OPS as its
   8325  1.10  christos    breakpoint_ops, and will set enabled to LOC_ENABLED.  */
   8326  1.10  christos 
   8327   1.1  christos static struct breakpoint *
   8328  1.10  christos momentary_breakpoint_from_master (struct breakpoint *orig,
   8329  1.10  christos 				  enum bptype type,
   8330  1.10  christos 				  int loc_enabled,
   8331  1.11  christos 				  int thread)
   8332  1.11  christos {
   8333  1.11  christos   std::unique_ptr<breakpoint> copy
   8334  1.11  christos     (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
   8335  1.11  christos 			       orig->frame_id, thread));
   8336  1.11  christos   const bp_location &orig_loc = orig->first_loc ();
   8337  1.11  christos   bp_location *copy_loc = copy->allocate_location ();
   8338  1.11  christos   copy->add_location (*copy_loc);
   8339  1.11  christos   set_breakpoint_location_function (copy_loc);
   8340  1.11  christos 
   8341  1.11  christos   copy_loc->gdbarch = orig_loc.gdbarch;
   8342  1.11  christos   copy_loc->requested_address = orig_loc.requested_address;
   8343  1.11  christos   copy_loc->address = orig_loc.address;
   8344  1.11  christos   copy_loc->section = orig_loc.section;
   8345   1.1  christos   copy_loc->pspace = orig_loc.pspace;
   8346  1.10  christos   copy_loc->probe = orig_loc.probe;
   8347   1.3  christos   copy_loc->line_number = orig_loc.line_number;
   8348  1.10  christos   copy_loc->symtab = orig_loc.symtab;
   8349   1.1  christos   copy_loc->enabled = loc_enabled;
   8350   1.1  christos 
   8351   1.1  christos   breakpoint *b = add_to_breakpoint_chain (std::move (copy));
   8352   1.1  christos   update_global_location_list_nothrow (UGLL_DONT_INSERT);
   8353   1.1  christos   return b;
   8354   1.1  christos }
   8355   1.1  christos 
   8356   1.1  christos /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
   8357   1.1  christos    ORIG is NULL.  */
   8358   1.1  christos 
   8359   1.1  christos struct breakpoint *
   8360   1.1  christos clone_momentary_breakpoint (struct breakpoint *orig)
   8361  1.10  christos {
   8362  1.10  christos   /* If there's nothing to clone, then return nothing.  */
   8363   1.1  christos   if (orig == NULL)
   8364   1.1  christos     return NULL;
   8365   1.8  christos 
   8366   1.1  christos   return momentary_breakpoint_from_master (orig, orig->type, 0,
   8367   1.1  christos 					   orig->thread);
   8368   1.1  christos }
   8369   1.1  christos 
   8370   1.1  christos breakpoint_up
   8371   1.1  christos set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
   8372   1.1  christos 				enum bptype type)
   8373   1.1  christos {
   8374   1.1  christos   struct symtab_and_line sal;
   8375   1.1  christos 
   8376   1.1  christos   sal = find_pc_line (pc, 0);
   8377   1.1  christos   sal.pc = pc;
   8378   1.1  christos   sal.section = find_pc_overlay (pc);
   8379   1.1  christos   sal.explicit_pc = 1;
   8380   1.1  christos 
   8381   1.1  christos   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
   8382   1.1  christos }
   8383  1.10  christos 
   8384   1.1  christos 
   8386   1.8  christos /* Tell the user we have just set a breakpoint B.  */
   8387   1.1  christos 
   8388   1.1  christos static void
   8389   1.1  christos mention (const breakpoint *b)
   8390   1.9  christos {
   8391   1.9  christos   b->print_mention ();
   8392   1.9  christos   current_uiout->text ("\n");
   8393   1.9  christos }
   8394   1.9  christos 
   8395   1.9  christos 
   8397   1.9  christos static bool bp_loc_is_permanent (struct bp_location *loc);
   8398   1.9  christos 
   8399   1.9  christos /* Handle "set breakpoint auto-hw on".
   8400   1.9  christos 
   8401   1.9  christos    If the explicitly specified breakpoint type is not hardware
   8402   1.9  christos    breakpoint, check the memory map to see whether the breakpoint
   8403   1.9  christos    address is in read-only memory.
   8404   1.9  christos 
   8405   1.9  christos    - location type is not hardware breakpoint, memory is read-only.
   8406   1.9  christos    We change the type of the location to hardware breakpoint.
   8407   1.9  christos 
   8408   1.9  christos    - location type is hardware breakpoint, memory is read-write.  This
   8409   1.9  christos    means we've previously made the location hardware one, but then the
   8410   1.9  christos    memory map changed, so we undo.
   8411   1.9  christos */
   8412   1.9  christos 
   8413   1.9  christos static void
   8414   1.9  christos handle_automatic_hardware_breakpoints (bp_location *bl)
   8415   1.9  christos {
   8416   1.9  christos   if (automatic_hardware_breakpoints
   8417   1.9  christos       && bl->owner->type != bp_hardware_breakpoint
   8418   1.9  christos       && (bl->loc_type == bp_loc_software_breakpoint
   8419   1.9  christos 	  || bl->loc_type == bp_loc_hardware_breakpoint))
   8420   1.9  christos     {
   8421   1.9  christos       /* When breakpoints are removed, remove_breakpoints will use
   8422   1.9  christos 	 location types we've just set here, the only possible problem
   8423   1.9  christos 	 is that memory map has changed during running program, but
   8424   1.9  christos 	 it's not going to work anyway with current gdb.  */
   8425   1.9  christos       mem_region *mr = lookup_mem_region (bl->address);
   8426   1.9  christos 
   8427   1.9  christos       if (mr != nullptr)
   8428   1.9  christos 	{
   8429   1.9  christos 	  enum bp_loc_type new_type;
   8430   1.9  christos 
   8431   1.9  christos 	  if (mr->attrib.mode != MEM_RW)
   8432   1.9  christos 	    new_type = bp_loc_hardware_breakpoint;
   8433   1.9  christos 	  else
   8434   1.9  christos 	    new_type = bp_loc_software_breakpoint;
   8435   1.9  christos 
   8436  1.10  christos 	  if (new_type != bl->loc_type)
   8437  1.10  christos 	    {
   8438  1.10  christos 	      static bool said = false;
   8439   1.9  christos 
   8440   1.9  christos 	      bl->loc_type = new_type;
   8441   1.9  christos 	      if (!said)
   8442   1.9  christos 		{
   8443   1.9  christos 		  gdb_printf (_("Note: automatically using "
   8444   1.9  christos 				"hardware breakpoints for "
   8445   1.3  christos 				"read-only addresses.\n"));
   8446  1.10  christos 		  said = true;
   8447  1.10  christos 		}
   8448   1.1  christos 	    }
   8449   1.1  christos 	}
   8450  1.10  christos     }
   8451   1.1  christos }
   8452   1.1  christos 
   8453  1.10  christos bp_location *
   8454   1.1  christos code_breakpoint::add_location (const symtab_and_line &sal)
   8455   1.1  christos {
   8456  1.10  christos   CORE_ADDR adjusted_address;
   8457   1.1  christos   struct gdbarch *loc_gdbarch = get_sal_arch (sal);
   8458   1.1  christos 
   8459   1.1  christos   if (loc_gdbarch == NULL)
   8460   1.1  christos     loc_gdbarch = gdbarch;
   8461   1.1  christos 
   8462  1.10  christos   /* Adjust the breakpoint's address prior to allocating a location.
   8463  1.10  christos      Once we call allocate_location(), that mostly uninitialized
   8464   1.1  christos      location will be placed on the location chain.  Adjustment of the
   8465   1.1  christos      breakpoint may cause target_read_memory() to be called and we do
   8466  1.11  christos      not want its scan of the location chain to find a breakpoint and
   8467   1.1  christos      location that's only been partially initialized.  */
   8468  1.10  christos   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
   8469  1.10  christos 						sal.pc, type,
   8470  1.10  christos 						sal.pspace);
   8471  1.10  christos 
   8472  1.10  christos   /* Sort the locations by their ADDRESS.  */
   8473  1.10  christos   bp_location *new_loc = this->allocate_location ();
   8474  1.10  christos 
   8475  1.10  christos   new_loc->requested_address = sal.pc;
   8476  1.10  christos   new_loc->address = adjusted_address;
   8477  1.10  christos   new_loc->pspace = sal.pspace;
   8478  1.10  christos   new_loc->probe.prob = sal.prob;
   8479  1.10  christos   new_loc->probe.objfile = sal.objfile;
   8480  1.10  christos   gdb_assert (new_loc->pspace != NULL);
   8481   1.1  christos   new_loc->section = sal.section;
   8482  1.11  christos   new_loc->gdbarch = loc_gdbarch;
   8483  1.11  christos   new_loc->line_number = sal.line;
   8484  1.10  christos   new_loc->symtab = sal.symtab;
   8485   1.3  christos   new_loc->symbol = sal.symbol;
   8486   1.5  christos   new_loc->msymbol = sal.msymbol;
   8487   1.5  christos   new_loc->objfile = sal.objfile;
   8488   1.5  christos 
   8489   1.5  christos   breakpoint::add_location (*new_loc);
   8490   1.5  christos 
   8491   1.5  christos   set_breakpoint_location_function (new_loc);
   8492   1.5  christos 
   8493   1.5  christos   /* While by definition, permanent breakpoints are already present in the
   8494   1.5  christos      code, we don't mark the location as inserted.  Normally one would expect
   8495   1.5  christos      that GDB could rely on that breakpoint instruction to stop the program,
   8496   1.5  christos      thus removing the need to insert its own breakpoint, except that executing
   8497   1.5  christos      the breakpoint instruction can kill the target instead of reporting a
   8498   1.5  christos      SIGTRAP.  E.g., on SPARC, when interrupts are disabled, executing the
   8499  1.10  christos      instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
   8500  1.10  christos      with "Trap 0x02 while interrupts disabled, Error state".  Letting the
   8501   1.3  christos      breakpoint be inserted normally results in QEMU knowing about the GDB
   8502  1.10  christos      breakpoint, and thus trap before the breakpoint instruction is executed.
   8503   1.1  christos      (If GDB later needs to continue execution past the permanent breakpoint,
   8504   1.1  christos      it manually increments the PC, thus avoiding executing the breakpoint
   8505   1.1  christos      instruction.)  */
   8506   1.9  christos   if (bp_loc_is_permanent (new_loc))
   8507   1.9  christos     new_loc->permanent = 1;
   8508   1.1  christos 
   8509   1.9  christos   return new_loc;
   8510   1.5  christos }
   8511   1.5  christos 
   8512   1.5  christos 
   8514   1.9  christos /* Return true if LOC is pointing to a permanent breakpoint,
   8515   1.9  christos    return false otherwise.  */
   8516   1.9  christos 
   8517   1.9  christos static bool
   8518   1.5  christos bp_loc_is_permanent (struct bp_location *loc)
   8519   1.9  christos {
   8520   1.9  christos   gdb_assert (loc != NULL);
   8521   1.5  christos 
   8522   1.8  christos   /* If we have a non-breakpoint-backed catchpoint or a software
   8523   1.1  christos      watchpoint, just return 0.  We should not attempt to read from
   8524   1.9  christos      the addresses the locations of these breakpoint types point to.
   8525   1.1  christos      gdbarch_program_breakpoint_here_p, below, will attempt to read
   8526   1.1  christos      memory.  */
   8527   1.1  christos   if (!bl_address_is_meaningful (loc))
   8528   1.1  christos     return false;
   8529   1.1  christos 
   8530   1.1  christos   scoped_restore_current_pspace_and_thread restore_pspace_thread;
   8531   1.1  christos   switch_to_program_space_and_thread (loc->pspace);
   8532   1.1  christos   return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
   8533  1.11  christos }
   8534  1.11  christos 
   8535  1.11  christos /* Build a command list for the dprintf corresponding to the current
   8536  1.10  christos    settings of the dprintf style options.  */
   8537  1.10  christos 
   8538   1.1  christos static void
   8539  1.11  christos update_dprintf_command_list (struct breakpoint *b)
   8540  1.11  christos {
   8541  1.11  christos   gdb_assert (b->type == bp_dprintf);
   8542   1.1  christos   gdb_assert (b->extra_string != nullptr);
   8543   1.1  christos 
   8544   1.1  christos   const char *dprintf_args = b->extra_string.get ();
   8545   1.1  christos   gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
   8546   1.1  christos 
   8547   1.1  christos   /* Trying to create a dprintf breakpoint without a format and args
   8548   1.1  christos      string should be detected at creation time.  */
   8549   1.1  christos   gdb_assert (dprintf_args != nullptr);
   8550   1.1  christos 
   8551   1.1  christos   dprintf_args = skip_spaces (dprintf_args);
   8552   1.1  christos 
   8553   1.1  christos   /* Allow a comma, as it may have terminated a location, but don't
   8554   1.1  christos      insist on it.  */
   8555   1.1  christos   if (*dprintf_args == ',')
   8556   1.1  christos     ++dprintf_args;
   8557   1.1  christos   dprintf_args = skip_spaces (dprintf_args);
   8558  1.10  christos 
   8559   1.1  christos   if (*dprintf_args != '"')
   8560   1.1  christos     error (_("Bad format string, missing '\"'."));
   8561  1.10  christos 
   8562   1.1  christos   if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
   8563  1.10  christos     printf_line = xstrprintf ("printf %s", dprintf_args);
   8564  1.10  christos   else if (strcmp (dprintf_style, dprintf_style_call) == 0)
   8565   1.1  christos     {
   8566   1.1  christos       if (dprintf_function.empty ())
   8567   1.1  christos 	error (_("No function supplied for dprintf call"));
   8568  1.10  christos 
   8569   1.1  christos       if (!dprintf_channel.empty ())
   8570   1.1  christos 	printf_line = xstrprintf ("call (void) %s (%s,%s)",
   8571   1.1  christos 				  dprintf_function.c_str (),
   8572   1.1  christos 				  dprintf_channel.c_str (),
   8573   1.1  christos 				  dprintf_args);
   8574   1.1  christos       else
   8575   1.1  christos 	printf_line = xstrprintf ("call (void) %s (%s)",
   8576   1.1  christos 				  dprintf_function.c_str (),
   8577   1.1  christos 				  dprintf_args);
   8578   1.1  christos     }
   8579   1.1  christos   else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
   8580   1.1  christos     {
   8581   1.1  christos       if (target_can_run_breakpoint_commands ())
   8582  1.10  christos 	printf_line = xstrprintf ("agent-printf %s", dprintf_args);
   8583   1.1  christos       else
   8584   1.1  christos 	{
   8585   1.8  christos 	  warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
   8586   1.1  christos 	  printf_line = xstrprintf ("printf %s", dprintf_args);
   8587   1.8  christos 	}
   8588  1.10  christos     }
   8589   1.8  christos   else
   8590   1.8  christos     internal_error (_("Invalid dprintf style."));
   8591   1.1  christos 
   8592   1.1  christos   gdb_assert (printf_line != NULL);
   8593   1.1  christos 
   8594   1.1  christos   /* Manufacture a printf sequence.  */
   8595   1.1  christos   struct command_line *printf_cmd_line
   8596   1.1  christos     = new struct command_line (simple_control, printf_line.release ());
   8597   1.8  christos   breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
   8598   1.1  christos 						    command_lines_deleter ()));
   8599   1.1  christos }
   8600  1.11  christos 
   8601  1.11  christos /* Update all dprintf commands, making their command lists reflect
   8602  1.11  christos    current style settings.  */
   8603   1.1  christos 
   8604   1.1  christos static void
   8605  1.10  christos update_dprintf_commands (const char *args, int from_tty,
   8606  1.10  christos 			 struct cmd_list_element *c)
   8607  1.10  christos {
   8608  1.10  christos   for (breakpoint &b : all_breakpoints ())
   8609  1.10  christos     if (b.type == bp_dprintf)
   8610  1.10  christos 	update_dprintf_command_list (&b);
   8611  1.10  christos }
   8612  1.10  christos 
   8613  1.11  christos code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
   8614  1.11  christos 				  enum bptype type_,
   8615  1.10  christos 				  gdb::array_view<const symtab_and_line> sals,
   8616  1.10  christos 				  location_spec_up &&locspec_,
   8617  1.10  christos 				  gdb::unique_xmalloc_ptr<char> filter_,
   8618  1.10  christos 				  gdb::unique_xmalloc_ptr<char> cond_string_,
   8619   1.1  christos 				  gdb::unique_xmalloc_ptr<char> extra_string_,
   8620   1.1  christos 				  enum bpdisp disposition_,
   8621   1.1  christos 				  int thread_, int task_, int inferior_,
   8622   1.1  christos 				  int ignore_count_,
   8623   1.1  christos 				  int from_tty,
   8624   1.1  christos 				  int enabled_, unsigned flags,
   8625   1.1  christos 				  int display_canonical_)
   8626   1.1  christos   : breakpoint (gdbarch_, type_)
   8627   1.1  christos {
   8628   1.1  christos   int i;
   8629   1.1  christos 
   8630   1.1  christos   if (type == bp_hardware_breakpoint)
   8631   1.1  christos     {
   8632   1.1  christos       int target_resources_ok;
   8633   1.1  christos 
   8634   1.1  christos       i = hw_breakpoint_used_count ();
   8635   1.1  christos       target_resources_ok =
   8636   1.8  christos 	target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
   8637   1.1  christos 					    i + 1, 0);
   8638  1.11  christos       if (target_resources_ok == 0)
   8639  1.11  christos 	error (_("No hardware breakpoint support in the target."));
   8640  1.11  christos       else if (target_resources_ok < 0)
   8641  1.11  christos 	error (_("Hardware breakpoints used exceeds limit."));
   8642  1.11  christos     }
   8643  1.10  christos 
   8644  1.10  christos   gdb_assert (!sals.empty ());
   8645  1.11  christos 
   8646   1.1  christos   /* At most one of thread, task, or inferior can be set on any breakpoint.  */
   8647  1.10  christos   gdb_assert (((thread == -1 ? 0 : 1)
   8648  1.10  christos 	       + (task == -1 ? 0 : 1)
   8649  1.10  christos 	       + (inferior == -1 ? 0 : 1)) <= 1);
   8650  1.10  christos 
   8651  1.10  christos   thread = thread_;
   8652   1.1  christos   task = task_;
   8653  1.10  christos   inferior = inferior_;
   8654  1.10  christos 
   8655  1.10  christos   cond_string = std::move (cond_string_);
   8656  1.11  christos   extra_string = std::move (extra_string_);
   8657  1.10  christos   ignore_count = ignore_count_;
   8658   1.1  christos   enable_state = enabled_ ? bp_enabled : bp_disabled;
   8659  1.10  christos   disposition = disposition_;
   8660   1.1  christos 
   8661  1.10  christos   if (type == bp_static_tracepoint
   8662  1.10  christos       || type == bp_static_marker_tracepoint)
   8663  1.10  christos     {
   8664  1.10  christos       auto *t = gdb::checked_static_cast<tracepoint *> (this);
   8665   1.1  christos       struct static_tracepoint_marker marker;
   8666  1.10  christos 
   8667   1.1  christos       if (strace_marker_p (this))
   8668  1.10  christos 	{
   8669   1.1  christos 	  /* We already know the marker exists, otherwise, we wouldn't
   8670  1.10  christos 	     see a sal for it.  */
   8671   1.1  christos 	  const char *p = &locspec_->to_string ()[3];
   8672  1.10  christos 	  const char *endp;
   8673  1.10  christos 
   8674  1.10  christos 	  p = skip_spaces (p);
   8675  1.10  christos 
   8676  1.10  christos 	  endp = skip_to_space (p);
   8677  1.10  christos 
   8678   1.1  christos 	  t->static_trace_marker_id.assign (p, endp - p);
   8679  1.10  christos 
   8680  1.10  christos 	  gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
   8681   1.1  christos 		      t->static_trace_marker_id.c_str ());
   8682   1.1  christos 	}
   8683  1.10  christos       else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
   8684  1.10  christos 	{
   8685   1.1  christos 	  t->static_trace_marker_id = std::move (marker.str_id);
   8686  1.10  christos 
   8687  1.10  christos 	  gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
   8688  1.10  christos 		      t->static_trace_marker_id.c_str ());
   8689   1.1  christos 	}
   8690  1.10  christos       else
   8691  1.10  christos 	warning (_("Couldn't determine the static tracepoint marker to probe"));
   8692  1.10  christos     }
   8693   1.1  christos 
   8694  1.10  christos   for (const auto &sal : sals)
   8695  1.10  christos     {
   8696   1.1  christos       if (from_tty)
   8697   1.1  christos 	{
   8698  1.10  christos 	  struct gdbarch *loc_gdbarch = get_sal_arch (sal);
   8699  1.10  christos 	  if (loc_gdbarch == nullptr)
   8700  1.10  christos 	    loc_gdbarch = gdbarch;
   8701  1.10  christos 
   8702  1.10  christos 	  describe_other_breakpoints (loc_gdbarch,
   8703  1.10  christos 				      sal.pspace, sal.pc, sal.section, thread);
   8704  1.10  christos 	}
   8705  1.11  christos 
   8706  1.10  christos       bp_location *new_loc = add_location (sal);
   8707  1.11  christos       if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
   8708  1.11  christos 	new_loc->inserted = 1;
   8709  1.11  christos 
   8710  1.11  christos       /* Do not set breakpoint locations conditions yet.  As locations
   8711  1.11  christos 	 are inserted, they get sorted based on their addresses.  Let
   8712  1.11  christos 	 the list stabilize to have reliable location numbers.  */
   8713   1.1  christos     }
   8714  1.10  christos 
   8715  1.10  christos   /* Dynamic printf requires and uses additional arguments on the
   8716  1.10  christos      command line, otherwise it's an error.  */
   8717  1.11  christos   if (type == bp_dprintf)
   8718  1.10  christos     update_dprintf_command_list (this);
   8719  1.10  christos   else if (extra_string != nullptr)
   8720  1.11  christos     error (_("Garbage '%s' at end of command"), extra_string.get ());
   8721  1.10  christos 
   8722  1.10  christos   /* The order of the locations is now stable.  Set the location
   8723  1.10  christos      condition using the location's number.  */
   8724  1.10  christos   int loc_num = 1;
   8725  1.10  christos   for (bp_location &bl : locations ())
   8726  1.10  christos     {
   8727  1.10  christos       if (cond_string != nullptr)
   8728  1.10  christos 	set_breakpoint_location_condition (cond_string.get (), &bl,
   8729   1.1  christos 					   number, loc_num);
   8730  1.11  christos 
   8731  1.10  christos       ++loc_num;
   8732   1.1  christos     }
   8733   1.1  christos 
   8734   1.1  christos   display_canonical = display_canonical_;
   8735   1.1  christos   if (locspec_ != nullptr)
   8736   1.8  christos     locspec = std::move (locspec_);
   8737  1.10  christos   else
   8738   1.8  christos     locspec = new_address_location_spec (this->first_loc ().address, NULL, 0);
   8739   1.8  christos   filter = std::move (filter_);
   8740   1.8  christos }
   8741   1.1  christos 
   8742  1.11  christos static void
   8743  1.10  christos create_breakpoint_sal (struct gdbarch *gdbarch,
   8744   1.1  christos 		       gdb::array_view<const symtab_and_line> sals,
   8745   1.1  christos 		       location_spec_up &&locspec,
   8746   1.1  christos 		       gdb::unique_xmalloc_ptr<char> filter,
   8747  1.10  christos 		       gdb::unique_xmalloc_ptr<char> cond_string,
   8748  1.10  christos 		       gdb::unique_xmalloc_ptr<char> extra_string,
   8749  1.10  christos 		       enum bptype type, enum bpdisp disposition,
   8750  1.10  christos 		       int thread, int task, int inferior, int ignore_count,
   8751  1.10  christos 		       int from_tty,
   8752  1.10  christos 		       int enabled, int internal, unsigned flags,
   8753  1.10  christos 		       int display_canonical)
   8754  1.10  christos {
   8755  1.10  christos   std::unique_ptr<code_breakpoint> b
   8756  1.11  christos     = new_breakpoint_from_type (gdbarch,
   8757  1.10  christos 				type,
   8758  1.10  christos 				sals,
   8759  1.10  christos 				std::move (locspec),
   8760   1.1  christos 				std::move (filter),
   8761   1.8  christos 				std::move (cond_string),
   8762   1.1  christos 				std::move (extra_string),
   8763   1.1  christos 				disposition,
   8764   1.1  christos 				thread, task, inferior, ignore_count,
   8765   1.1  christos 				from_tty,
   8766   1.1  christos 				enabled, flags,
   8767   1.1  christos 				display_canonical);
   8768   1.1  christos 
   8769   1.1  christos   install_breakpoint (internal, std::move (b), 0);
   8770   1.1  christos }
   8771   1.1  christos 
   8772   1.1  christos /* Add SALS.nelts breakpoints to the breakpoint table.  For each
   8773   1.1  christos    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
   8774   1.1  christos    value.  COND_STRING, if not NULL, specified the condition to be
   8775   1.1  christos    used for all breakpoints.  Essentially the only case where
   8776   1.1  christos    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
   8777   1.1  christos    function.  In that case, it's still not possible to specify
   8778   1.1  christos    separate conditions for different overloaded functions, so
   8779   1.1  christos    we take just a single condition string.
   8780   1.1  christos 
   8781   1.1  christos    NOTE: If the function succeeds, the caller is expected to cleanup
   8782   1.8  christos    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
   8783   1.8  christos    array contents).  If the function fails (error() is called), the
   8784   1.1  christos    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
   8785  1.11  christos    COND and SALS arrays and each of those arrays contents.  */
   8786  1.11  christos 
   8787  1.10  christos static void
   8788   1.1  christos create_breakpoints_sal (struct gdbarch *gdbarch,
   8789   1.1  christos 			struct linespec_result *canonical,
   8790   1.1  christos 			gdb::unique_xmalloc_ptr<char> cond_string,
   8791   1.8  christos 			gdb::unique_xmalloc_ptr<char> extra_string,
   8792   1.1  christos 			enum bptype type, enum bpdisp disposition,
   8793   1.8  christos 			int thread, int task, int inferior,
   8794   1.1  christos 			int ignore_count,
   8795   1.6  christos 			int from_tty,
   8796   1.1  christos 			int enabled, int internal, unsigned flags)
   8797  1.10  christos {
   8798  1.10  christos   if (canonical->pre_expanded)
   8799  1.10  christos     gdb_assert (canonical->lsals.size () == 1);
   8800  1.10  christos 
   8801   1.8  christos   for (const auto &lsal : canonical->lsals)
   8802   1.8  christos     {
   8803   1.1  christos       /* Note that 'location' can be NULL in the case of a plain
   8804   1.8  christos 	 'break', without arguments.  */
   8805  1.10  christos       location_spec_up locspec
   8806   1.8  christos 	= (canonical->locspec != nullptr
   8807   1.8  christos 	   ? canonical->locspec->clone ()
   8808   1.8  christos 	   : nullptr);
   8809   1.1  christos       gdb::unique_xmalloc_ptr<char> filter_string
   8810  1.11  christos 	(lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
   8811   1.1  christos 
   8812   1.1  christos       create_breakpoint_sal (gdbarch, lsal.sals,
   8813   1.1  christos 			     std::move (locspec),
   8814   1.1  christos 			     std::move (filter_string),
   8815   1.1  christos 			     std::move (cond_string),
   8816  1.10  christos 			     std::move (extra_string),
   8817   1.1  christos 			     type, disposition,
   8818  1.10  christos 			     thread, task, inferior, ignore_count,
   8819  1.10  christos 			     from_tty, enabled, internal, flags,
   8820   1.1  christos 			     canonical->special_display);
   8821   1.1  christos     }
   8822   1.1  christos }
   8823   1.1  christos 
   8824   1.1  christos /* Parse LOCSPEC which is assumed to be a SAL specification possibly
   8825  1.10  christos    followed by conditionals.  On return, SALS contains an array of SAL
   8826   1.1  christos    addresses found.  LOCSPEC points to the end of the SAL (for
   8827   1.1  christos    linespec locspecs).
   8828   1.6  christos 
   8829   1.6  christos    The array and the line spec strings are allocated on the heap, it is
   8830  1.10  christos    the caller's responsibility to free them.  */
   8831   1.1  christos 
   8832  1.11  christos static void
   8833  1.11  christos parse_breakpoint_sals (location_spec *locspec,
   8834   1.6  christos 		       struct linespec_result *canonical)
   8835   1.8  christos {
   8836   1.6  christos   struct symtab_and_line cursal;
   8837   1.6  christos 
   8838   1.6  christos   if (locspec->type () == LINESPEC_LOCATION_SPEC)
   8839   1.6  christos     {
   8840   1.6  christos       const char *spec
   8841   1.6  christos 	= as_linespec_location_spec (locspec)->spec_string.get ();
   8842   1.6  christos 
   8843   1.6  christos       if (spec == NULL)
   8844   1.6  christos 	{
   8845   1.6  christos 	  /* The last displayed codepoint, if it's valid, is our default
   8846   1.8  christos 	     breakpoint address.  */
   8847   1.8  christos 	  if (last_displayed_sal_is_valid ())
   8848   1.8  christos 	    {
   8849   1.6  christos 	      /* Set sal's pspace, pc, symtab, and line to the values
   8850   1.6  christos 		 corresponding to the last call to print_frame_info.
   8851   1.6  christos 		 Be sure to reinitialize LINE with NOTCURRENT == 0
   8852   1.6  christos 		 as the breakpoint line number is inappropriate otherwise.
   8853   1.6  christos 		 find_pc_line would adjust PC, re-set it back.  */
   8854   1.6  christos 	      symtab_and_line sal = get_last_displayed_sal ();
   8855   1.6  christos 	      CORE_ADDR pc = sal.pc;
   8856   1.6  christos 
   8857   1.6  christos 	      sal = find_pc_line (pc, 0);
   8858   1.6  christos 
   8859   1.8  christos 	      /* "break" without arguments is equivalent to "break *PC"
   8860   1.8  christos 		 where PC is the last displayed codepoint's address.  So
   8861   1.6  christos 		 make sure to set sal.explicit_pc to prevent GDB from
   8862   1.6  christos 		 trying to expand the list of sals to include all other
   8863   1.8  christos 		 instances with the same symtab and line.  */
   8864   1.6  christos 	      sal.pc = pc;
   8865   1.6  christos 	      sal.explicit_pc = 1;
   8866   1.6  christos 
   8867   1.6  christos 	      struct linespec_sals lsal;
   8868   1.6  christos 	      lsal.sals = {sal};
   8869   1.1  christos 	      lsal.canonical = NULL;
   8870   1.6  christos 
   8871   1.6  christos 	      canonical->lsals.push_back (std::move (lsal));
   8872   1.6  christos 	      return;
   8873   1.6  christos 	    }
   8874   1.6  christos 	  else
   8875   1.6  christos 	    error (_("No default breakpoint address now."));
   8876   1.6  christos 	}
   8877   1.6  christos     }
   8878   1.6  christos 
   8879   1.6  christos   /* Force almost all breakpoints to be in terms of the
   8880   1.1  christos      current_source_symtab (which is decode_line_1's default).
   8881   1.8  christos      This should produce the results we want almost all of the
   8882   1.6  christos      time while leaving default_breakpoint_* alone.
   8883  1.10  christos 
   8884  1.11  christos      ObjC: However, don't match an Objective-C method name which
   8885   1.6  christos      may have a '+' or '-' succeeded by a '['.  */
   8886   1.6  christos   cursal = get_current_source_symtab_and_line ();
   8887   1.8  christos   if (last_displayed_sal_is_valid ())
   8888   1.8  christos     {
   8889   1.8  christos       const char *spec = NULL;
   8890   1.6  christos 
   8891  1.10  christos       if (locspec->type () == LINESPEC_LOCATION_SPEC)
   8892   1.6  christos 	spec = as_linespec_location_spec (locspec)->spec_string.get ();
   8893   1.6  christos 
   8894   1.6  christos       if (!cursal.symtab
   8895   1.6  christos 	  || (spec != NULL
   8896   1.6  christos 	      && strchr ("+-", spec[0]) != NULL
   8897   1.1  christos 	      && spec[1] != '['))
   8898   1.6  christos 	{
   8899  1.10  christos 	  decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
   8900   1.6  christos 			    get_last_displayed_symtab (),
   8901   1.1  christos 			    get_last_displayed_line (),
   8902   1.1  christos 			    canonical, NULL, NULL);
   8903   1.1  christos 	  return;
   8904   1.1  christos 	}
   8905   1.1  christos     }
   8906   1.1  christos 
   8907   1.1  christos   decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
   8908   1.8  christos 		    cursal.symtab, cursal.line, canonical, NULL, NULL);
   8909   1.1  christos }
   8910   1.8  christos 
   8911   1.8  christos 
   8912   1.1  christos /* Convert each SAL into a real PC.  Verify that the PC can be
   8913   1.1  christos    inserted as a breakpoint.  If it can't throw an error.  */
   8914   1.1  christos 
   8915   1.1  christos static void
   8916   1.1  christos breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
   8917   1.1  christos {
   8918   1.1  christos   for (auto &sal : sals)
   8919   1.1  christos     resolve_sal_pc (&sal);
   8920   1.1  christos }
   8921   1.1  christos 
   8922   1.1  christos /* Fast tracepoints may have restrictions on valid locations.  For
   8923   1.8  christos    instance, a fast tracepoint using a jump instead of a trap will
   8924   1.1  christos    likely have to overwrite more bytes than a trap would, and so can
   8925   1.8  christos    only be placed where the instruction is longer than the jump, or a
   8926   1.1  christos    multi-instruction sequence does not have a jump into the middle of
   8927   1.1  christos    it, etc.  */
   8928   1.1  christos 
   8929   1.8  christos static void
   8930   1.1  christos check_fast_tracepoint_sals (struct gdbarch *gdbarch,
   8931   1.1  christos 			    gdb::array_view<const symtab_and_line> sals)
   8932   1.1  christos {
   8933   1.1  christos   for (const auto &sal : sals)
   8934   1.8  christos     {
   8935   1.8  christos       struct gdbarch *sarch;
   8936   1.7  christos 
   8937   1.8  christos       sarch = get_sal_arch (sal);
   8938   1.1  christos       /* We fall back to GDBARCH if there is no architecture
   8939   1.1  christos 	 associated with SAL.  */
   8940   1.1  christos       if (sarch == NULL)
   8941  1.11  christos 	sarch = gdbarch;
   8942  1.11  christos       std::string msg;
   8943  1.11  christos       if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
   8944  1.11  christos 	error (_("May not have a fast tracepoint at %s%s"),
   8945  1.11  christos 	       paddress (sarch, sal.pc), msg.c_str ());
   8946  1.11  christos     }
   8947  1.11  christos }
   8948  1.11  christos 
   8949  1.11  christos /* Given TOK, a string specification of condition and thread, as accepted
   8950  1.11  christos    by the 'break' command, extract the condition string into *COND_STRING.
   8951   1.1  christos    If no condition string is found then *COND_STRING is set to nullptr.
   8952   1.1  christos 
   8953   1.1  christos    If the breakpoint specification has an associated thread, task, or
   8954  1.10  christos    inferior, these are extracted into *THREAD, *TASK, and *INFERIOR
   8955  1.11  christos    respectively, otherwise these arguments are set to -1 (for THREAD and
   8956  1.10  christos    INFERIOR) or 0 (for TASK).
   8957   1.1  christos 
   8958  1.10  christos    PC identifies the context at which the condition should be parsed.  */
   8959   1.1  christos 
   8960  1.11  christos static void
   8961  1.11  christos find_condition_and_thread (const char *tok, CORE_ADDR pc,
   8962  1.10  christos 			   gdb::unique_xmalloc_ptr<char> *cond_string,
   8963  1.10  christos 			   int *thread, int *inferior, int *task,
   8964   1.1  christos 			   gdb::unique_xmalloc_ptr<char> *rest)
   8965   1.1  christos {
   8966   1.1  christos   cond_string->reset ();
   8967   1.1  christos   *thread = -1;
   8968   1.1  christos   *inferior = -1;
   8969   1.1  christos   *task = -1;
   8970   1.1  christos   rest->reset ();
   8971   1.1  christos   bool force = false;
   8972   1.8  christos 
   8973   1.1  christos   while (tok && *tok)
   8974   1.1  christos     {
   8975   1.1  christos       const char *end_tok;
   8976  1.10  christos       int toklen;
   8977  1.11  christos       const char *cond_start = NULL;
   8978   1.1  christos       const char *cond_end = NULL;
   8979   1.1  christos 
   8980   1.8  christos       tok = skip_spaces (tok);
   8981   1.1  christos 
   8982   1.1  christos       if ((*tok == '"' || *tok == ',') && rest)
   8983   1.1  christos 	{
   8984   1.1  christos 	  rest->reset (savestring (tok, strlen (tok)));
   8985   1.1  christos 	  break;
   8986   1.1  christos 	}
   8987  1.10  christos 
   8988  1.10  christos       end_tok = skip_to_space (tok);
   8989  1.10  christos 
   8990  1.10  christos       toklen = end_tok - tok;
   8991  1.10  christos 
   8992  1.10  christos       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
   8993  1.10  christos 	{
   8994  1.10  christos 	  tok = cond_start = end_tok + 1;
   8995  1.10  christos 	  try
   8996  1.10  christos 	    {
   8997  1.10  christos 	      parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
   8998   1.1  christos 	    }
   8999  1.10  christos 	  catch (const gdb_exception_error &)
   9000  1.10  christos 	    {
   9001  1.10  christos 	      if (!force)
   9002  1.10  christos 		throw;
   9003  1.10  christos 	      else
   9004  1.10  christos 		tok = tok + strlen (tok);
   9005   1.1  christos 	    }
   9006   1.1  christos 	  cond_end = tok;
   9007   1.1  christos 	  cond_string->reset (savestring (cond_start, cond_end - cond_start));
   9008   1.6  christos 	}
   9009   1.6  christos       else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
   9010   1.1  christos 	{
   9011  1.11  christos 	  tok = tok + toklen;
   9012  1.11  christos 	  force = true;
   9013  1.11  christos 	}
   9014  1.11  christos       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
   9015  1.11  christos 	{
   9016  1.11  christos 	  const char *tmptok;
   9017  1.11  christos 	  struct thread_info *thr;
   9018  1.11  christos 
   9019  1.11  christos 	  if (*thread != -1)
   9020   1.1  christos 	    error(_("You can specify only one thread."));
   9021   1.6  christos 
   9022   1.1  christos 	  if (*task != -1)
   9023   1.1  christos 	    error (_("You can specify only one of thread or task."));
   9024   1.6  christos 
   9025   1.1  christos 	  if (*inferior != -1)
   9026   1.1  christos 	    error (_("You can specify only one of inferior or thread."));
   9027  1.11  christos 
   9028  1.11  christos 	  tok = end_tok + 1;
   9029  1.11  christos 	  thr = parse_thread_id (tok, &tmptok);
   9030  1.11  christos 	  if (tok == tmptok)
   9031  1.11  christos 	    error (_("Junk after thread keyword."));
   9032  1.11  christos 	  *thread = thr->global_num;
   9033  1.11  christos 	  tok = tmptok;
   9034  1.11  christos 	}
   9035  1.11  christos       else if (toklen >= 1 && strncmp (tok, "inferior", toklen) == 0)
   9036  1.11  christos 	{
   9037  1.11  christos 	  if (*inferior != -1)
   9038  1.11  christos 	    error(_("You can specify only one inferior."));
   9039  1.11  christos 
   9040  1.11  christos 	  if (*task != -1)
   9041  1.11  christos 	    error (_("You can specify only one of inferior or task."));
   9042  1.11  christos 
   9043  1.11  christos 	  if (*thread != -1)
   9044  1.11  christos 	    error (_("You can specify only one of inferior or thread."));
   9045  1.11  christos 
   9046  1.11  christos 	  char *tmptok;
   9047   1.1  christos 	  tok = end_tok + 1;
   9048   1.1  christos 	  *inferior = strtol (tok, &tmptok, 0);
   9049   1.1  christos 	  if (tok == tmptok)
   9050   1.1  christos 	    error (_("Junk after inferior keyword."));
   9051  1.11  christos 	  if (!valid_global_inferior_id (*inferior))
   9052  1.11  christos 	    error (_("Unknown inferior number %d."), *inferior);
   9053  1.11  christos 	  tok = tmptok;
   9054  1.11  christos 	}
   9055  1.11  christos       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
   9056  1.11  christos 	{
   9057  1.11  christos 	  char *tmptok;
   9058  1.11  christos 
   9059  1.11  christos 	  if (*task != -1)
   9060   1.1  christos 	    error(_("You can specify only one task."));
   9061   1.1  christos 
   9062   1.1  christos 	  if (*thread != -1)
   9063   1.1  christos 	    error (_("You can specify only one of thread or task."));
   9064   1.1  christos 
   9065   1.1  christos 	  if (*inferior != -1)
   9066  1.10  christos 	    error (_("You can specify only one of inferior or task."));
   9067  1.10  christos 
   9068  1.10  christos 	  tok = end_tok + 1;
   9069  1.10  christos 	  *task = strtol (tok, &tmptok, 0);
   9070  1.10  christos 	  if (tok == tmptok)
   9071  1.11  christos 	    error (_("Junk after task keyword."));
   9072  1.10  christos 	  if (!valid_task_id (*task))
   9073  1.10  christos 	    error (_("Unknown task %d."), *task);
   9074  1.10  christos 	  tok = tmptok;
   9075  1.10  christos 	}
   9076  1.10  christos       else if (rest)
   9077  1.10  christos 	{
   9078  1.10  christos 	  rest->reset (savestring (tok, strlen (tok)));
   9079  1.10  christos 	  break;
   9080  1.10  christos 	}
   9081  1.10  christos       else
   9082  1.10  christos 	error (_("Junk at end of arguments."));
   9083  1.10  christos     }
   9084  1.10  christos }
   9085  1.10  christos 
   9086  1.10  christos /* Call 'find_condition_and_thread' for each sal in SALS until a parse
   9087  1.11  christos    succeeds.  The parsed values are written to COND_STRING, THREAD,
   9088  1.10  christos    TASK, and REST.  See the comment of 'find_condition_and_thread'
   9089  1.10  christos    for the description of these parameters and INPUT.  */
   9090  1.10  christos 
   9091  1.10  christos static void
   9092  1.10  christos find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
   9093  1.10  christos 				    const char *input,
   9094  1.11  christos 				    gdb::unique_xmalloc_ptr<char> *cond_string,
   9095  1.11  christos 				    int *thread, int *inferior, int *task,
   9096  1.11  christos 				    gdb::unique_xmalloc_ptr<char> *rest)
   9097  1.10  christos {
   9098  1.10  christos   int num_failures = 0;
   9099  1.10  christos   for (auto &sal : sals)
   9100  1.10  christos     {
   9101  1.10  christos       gdb::unique_xmalloc_ptr<char> cond;
   9102  1.10  christos       int thread_id = -1;
   9103  1.10  christos       int inferior_id = -1;
   9104  1.10  christos       int task_id = -1;
   9105  1.10  christos       gdb::unique_xmalloc_ptr<char> remaining;
   9106  1.10  christos 
   9107  1.10  christos       /* Here we want to parse 'arg' to separate condition from thread
   9108  1.11  christos 	 number.  But because parsing happens in a context and the
   9109  1.10  christos 	 contexts of sals might be different, try each until there is
   9110  1.11  christos 	 success.  Finding one successful parse is sufficient for our
   9111  1.11  christos 	 goal.  When setting the breakpoint we'll re-parse the
   9112  1.11  christos 	 condition in the context of each sal.  */
   9113  1.11  christos       try
   9114  1.11  christos 	{
   9115  1.11  christos 	  find_condition_and_thread (input, sal.pc, &cond, &thread_id,
   9116  1.10  christos 				     &inferior_id, &task_id, &remaining);
   9117  1.11  christos 	  *cond_string = std::move (cond);
   9118  1.10  christos 	  /* A value of -1 indicates that these fields are unset.  At most
   9119  1.10  christos 	     one of these fields should be set (to a value other than -1)
   9120  1.10  christos 	     at this point.  */
   9121   1.1  christos 	  gdb_assert (((thread_id == -1 ? 1 : 0)
   9122  1.10  christos 		       + (task_id == -1 ? 1 : 0)
   9123   1.1  christos 		       + (inferior_id == -1 ? 1 : 0)) >= 2);
   9124  1.10  christos 	  *thread = thread_id;
   9125  1.10  christos 	  *inferior = inferior_id;
   9126  1.10  christos 	  *task = task_id;
   9127  1.10  christos 	  *rest = std::move (remaining);
   9128   1.1  christos 	  break;
   9129   1.1  christos 	}
   9130   1.1  christos       catch (const gdb_exception_error &e)
   9131   1.1  christos 	{
   9132   1.1  christos 	  num_failures++;
   9133   1.1  christos 	  /* If no sal remains, do not continue.  */
   9134   1.8  christos 	  if (num_failures == sals.size ())
   9135   1.6  christos 	    throw;
   9136   1.1  christos 	}
   9137   1.6  christos     }
   9138   1.6  christos }
   9139   1.1  christos 
   9140   1.8  christos /* Decode a static tracepoint marker spec.  */
   9141   1.1  christos 
   9142   1.8  christos static std::vector<symtab_and_line>
   9143   1.1  christos decode_static_tracepoint_spec (const char **arg_p)
   9144   1.8  christos {
   9145   1.1  christos   const char *p = &(*arg_p)[3];
   9146   1.8  christos   const char *endp;
   9147   1.8  christos 
   9148   1.8  christos   p = skip_spaces (p);
   9149   1.8  christos 
   9150   1.8  christos   endp = skip_to_space (p);
   9151   1.1  christos 
   9152   1.8  christos   std::string marker_str (p, endp - p);
   9153   1.8  christos 
   9154   1.1  christos   std::vector<static_tracepoint_marker> markers
   9155   1.8  christos     = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
   9156   1.1  christos   if (markers.empty ())
   9157   1.8  christos     error (_("No known static tracepoint marker named %s"),
   9158   1.8  christos 	   marker_str.c_str ());
   9159   1.8  christos 
   9160   1.8  christos   std::vector<symtab_and_line> sals;
   9161   1.1  christos   sals.reserve (markers.size ());
   9162   1.1  christos 
   9163   1.1  christos   for (const static_tracepoint_marker &marker : markers)
   9164   1.1  christos     {
   9165   1.1  christos       symtab_and_line sal = find_pc_line (marker.address, 0);
   9166   1.9  christos       sal.pc = marker.address;
   9167   1.9  christos       sals.push_back (sal);
   9168   1.9  christos    }
   9169   1.9  christos 
   9170  1.10  christos   *arg_p = endp;
   9171  1.10  christos   return sals;
   9172   1.9  christos }
   9173   1.9  christos 
   9174   1.9  christos /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
   9175  1.10  christos    according to IS_TRACEPOINT.  */
   9176   1.9  christos 
   9177   1.9  christos static const struct breakpoint_ops *
   9178  1.10  christos breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
   9179   1.9  christos 				       bool is_tracepoint)
   9180   1.9  christos {
   9181   1.9  christos   if (is_tracepoint)
   9182  1.10  christos     {
   9183   1.9  christos       if (locspec_type == PROBE_LOCATION_SPEC)
   9184   1.9  christos 	return &tracepoint_probe_breakpoint_ops;
   9185  1.10  christos       else
   9186   1.9  christos 	return &code_breakpoint_ops;
   9187   1.9  christos     }
   9188   1.9  christos   else
   9189   1.9  christos     {
   9190   1.9  christos       if (locspec_type == PROBE_LOCATION_SPEC)
   9191   1.9  christos 	return &bkpt_probe_breakpoint_ops;
   9192  1.10  christos       else
   9193  1.10  christos 	return &code_breakpoint_ops;
   9194   1.9  christos     }
   9195  1.10  christos }
   9196  1.10  christos 
   9197  1.10  christos /* See breakpoint.h.  */
   9198  1.10  christos 
   9199   1.9  christos const struct breakpoint_ops *
   9200   1.9  christos breakpoint_ops_for_location_spec (const location_spec *locspec,
   9201   1.6  christos 				  bool is_tracepoint)
   9202   1.1  christos {
   9203   1.1  christos   if (locspec != nullptr)
   9204   1.1  christos     return (breakpoint_ops_for_location_spec_type
   9205  1.10  christos 	    (locspec->type (), is_tracepoint));
   9206   1.8  christos   return &code_breakpoint_ops;
   9207  1.11  christos }
   9208  1.11  christos 
   9209  1.10  christos /* See breakpoint.h.  */
   9210   1.1  christos 
   9211   1.1  christos int
   9212   1.1  christos create_breakpoint (struct gdbarch *gdbarch,
   9213   1.1  christos 		   location_spec *locspec,
   9214   1.1  christos 		   const char *cond_string,
   9215   1.1  christos 		   int thread, int inferior,
   9216   1.1  christos 		   const char *extra_string,
   9217   1.1  christos 		   bool force_condition, int parse_extra,
   9218  1.10  christos 		   int tempflag, enum bptype type_wanted,
   9219  1.11  christos 		   int ignore_count,
   9220   1.1  christos 		   enum auto_boolean pending_break_support,
   9221   1.1  christos 		   const struct breakpoint_ops *ops,
   9222  1.11  christos 		   int from_tty, int enabled, int internal,
   9223  1.11  christos 		   unsigned flags)
   9224  1.11  christos {
   9225  1.11  christos   struct linespec_result canonical;
   9226  1.11  christos   bool pending = false;
   9227  1.11  christos   int task = -1;
   9228  1.11  christos   int prev_bkpt_count = breakpoint_count;
   9229  1.11  christos 
   9230  1.11  christos   gdb_assert (thread == -1 || thread > 0);
   9231  1.11  christos   gdb_assert (inferior == -1 || inferior > 0);
   9232   1.1  christos   gdb_assert (thread == -1 || inferior == -1);
   9233   1.1  christos 
   9234   1.6  christos   /* If PARSE_EXTRA is true then the thread and inferior details will be
   9235   1.6  christos      parsed from the EXTRA_STRING, the THREAD and INFERIOR arguments
   9236   1.6  christos      should be -1.  */
   9237   1.6  christos   gdb_assert (!parse_extra || thread == -1);
   9238  1.11  christos   gdb_assert (!parse_extra || inferior == -1);
   9239  1.11  christos 
   9240  1.11  christos   gdb_assert (ops != NULL);
   9241  1.11  christos 
   9242  1.11  christos   /* If extra_string isn't useful, set it to NULL.  */
   9243  1.11  christos   if (extra_string != NULL && *extra_string == '\0')
   9244  1.11  christos     extra_string = NULL;
   9245  1.11  christos 
   9246  1.11  christos   /* A bp_dprintf must always have an accompanying EXTRA_STRING containing
   9247  1.11  christos      the dprintf format and arguments -- PARSE_EXTRA should always be false
   9248   1.9  christos      in this case.
   9249   1.1  christos 
   9250  1.10  christos      For all other breakpoint types, EXTRA_STRING should be nullptr unless
   9251   1.1  christos      PARSE_EXTRA is true.  */
   9252   1.9  christos   gdb_assert ((type_wanted == bp_dprintf)
   9253   1.1  christos 	      ? (extra_string != nullptr && !parse_extra)
   9254   1.5  christos 	      : (extra_string == nullptr || parse_extra));
   9255   1.5  christos 
   9256   1.5  christos   try
   9257   1.1  christos     {
   9258   1.1  christos       ops->create_sals_from_location_spec (locspec, &canonical);
   9259   1.1  christos     }
   9260   1.1  christos   catch (const gdb_exception_error &e)
   9261   1.1  christos     {
   9262   1.9  christos       /* If caller is interested in rc value from parse, set
   9263   1.1  christos 	 value.  */
   9264   1.1  christos       if (e.error == NOT_FOUND_ERROR)
   9265   1.1  christos 	{
   9266  1.10  christos 	  /* If pending breakpoint support is turned off, throw
   9267   1.1  christos 	     error.  */
   9268   1.1  christos 
   9269   1.1  christos 	  if (pending_break_support == AUTO_BOOLEAN_FALSE)
   9270   1.1  christos 	    throw;
   9271   1.1  christos 
   9272   1.1  christos 	  exception_print (gdb_stderr, e);
   9273   1.1  christos 
   9274   1.1  christos 	  /* If pending breakpoint support is auto query and the user
   9275   1.1  christos 	     selects no, then simply return the error code.  */
   9276   1.1  christos 	  if (pending_break_support == AUTO_BOOLEAN_AUTO
   9277  1.10  christos 	      && !nquery (_("Make %s pending on future shared library load? "),
   9278   1.1  christos 			  bptype_string (type_wanted)))
   9279   1.5  christos 	    return 0;
   9280   1.9  christos 
   9281   1.1  christos 	  /* At this point, either the user was queried about setting
   9282   1.5  christos 	     a pending breakpoint and selected yes, or pending
   9283   1.8  christos 	     breakpoint behavior is on and thus a pending breakpoint
   9284   1.5  christos 	     is defaulted on behalf of the user.  */
   9285   1.1  christos 	  pending = true;
   9286   1.1  christos 	}
   9287   1.1  christos       else
   9288   1.1  christos 	throw;
   9289   1.1  christos     }
   9290   1.8  christos 
   9291   1.8  christos   if (!pending && canonical.lsals.empty ())
   9292   1.1  christos     return 0;
   9293   1.1  christos 
   9294   1.1  christos   /* Resolve all line numbers to PC's and verify that the addresses
   9295   1.1  christos      are ok for the target.  */
   9296   1.1  christos   if (!pending)
   9297   1.8  christos     {
   9298   1.8  christos       for (auto &lsal : canonical.lsals)
   9299   1.1  christos 	breakpoint_sals_to_pc (lsal.sals);
   9300   1.1  christos     }
   9301   1.1  christos 
   9302   1.1  christos   /* Fast tracepoints may have additional restrictions on location.  */
   9303   1.1  christos   if (!pending && type_wanted == bp_fast_tracepoint)
   9304   1.1  christos     {
   9305   1.1  christos       for (const auto &lsal : canonical.lsals)
   9306   1.8  christos 	check_fast_tracepoint_sals (gdbarch, lsal.sals);
   9307   1.8  christos     }
   9308   1.8  christos 
   9309   1.6  christos   /* Verify that condition can be parsed, before setting any
   9310  1.10  christos      breakpoints.  Allocate a separate condition expression for each
   9311  1.11  christos      breakpoint.  */
   9312  1.11  christos   if (!pending)
   9313  1.10  christos     {
   9314  1.10  christos       gdb::unique_xmalloc_ptr<char> cond_string_copy;
   9315   1.1  christos       gdb::unique_xmalloc_ptr<char> extra_string_copy;
   9316   1.8  christos 
   9317   1.1  christos       if (parse_extra)
   9318  1.10  christos 	{
   9319  1.11  christos 	  gdb_assert (type_wanted != bp_dprintf);
   9320  1.11  christos 
   9321  1.11  christos 	  gdb::unique_xmalloc_ptr<char> rest;
   9322  1.11  christos 	  gdb::unique_xmalloc_ptr<char> cond;
   9323  1.11  christos 
   9324  1.11  christos 	  const linespec_sals &lsal = canonical.lsals[0];
   9325  1.10  christos 
   9326  1.10  christos 	  find_condition_and_thread_for_sals (lsal.sals, extra_string,
   9327  1.10  christos 					      &cond, &thread, &inferior,
   9328   1.1  christos 					      &task, &rest);
   9329  1.10  christos 
   9330  1.10  christos 	  if (rest.get () != nullptr && *(rest.get ()) != '\0')
   9331  1.10  christos 	    error (_("Garbage '%s' at end of command"), rest.get ());
   9332  1.10  christos 
   9333  1.10  christos 	  cond_string_copy = std::move (cond);
   9334  1.10  christos 	  extra_string_copy = std::move (rest);
   9335  1.10  christos 	}
   9336  1.10  christos       else
   9337  1.10  christos 	{
   9338  1.10  christos 	  /* Check the validity of the condition.  We should error out
   9339  1.10  christos 	     if the condition is invalid at all of the locations and
   9340  1.10  christos 	     if it is not forced.  In the PARSE_EXTRA case above, this
   9341  1.10  christos 	     check is done when parsing the EXTRA_STRING.  */
   9342  1.10  christos 	  if (cond_string != nullptr && !force_condition)
   9343  1.10  christos 	    {
   9344  1.10  christos 	      int num_failures = 0;
   9345  1.10  christos 	      const linespec_sals &lsal = canonical.lsals[0];
   9346  1.10  christos 	      for (const auto &sal : lsal.sals)
   9347  1.10  christos 		{
   9348  1.10  christos 		  const char *cond = cond_string;
   9349  1.10  christos 		  try
   9350  1.10  christos 		    {
   9351  1.10  christos 		      parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
   9352  1.10  christos 		      /* One success is sufficient to keep going.  */
   9353  1.10  christos 		      break;
   9354  1.10  christos 		    }
   9355  1.10  christos 		  catch (const gdb_exception_error &)
   9356  1.10  christos 		    {
   9357   1.1  christos 		      num_failures++;
   9358   1.1  christos 		      /* If this is the last sal, error out.  */
   9359   1.8  christos 		      if (num_failures == lsal.sals.size ())
   9360   1.1  christos 			throw;
   9361   1.1  christos 		    }
   9362   1.8  christos 		}
   9363  1.10  christos 	    }
   9364   1.1  christos 
   9365   1.1  christos 	  /* Create a private copy of condition string.  */
   9366   1.8  christos 	  if (cond_string)
   9367   1.8  christos 	    cond_string_copy.reset (xstrdup (cond_string));
   9368   1.8  christos 	  /* Create a private copy of any extra string.  */
   9369   1.1  christos 	  if (extra_string)
   9370  1.11  christos 	    extra_string_copy.reset (xstrdup (extra_string));
   9371   1.1  christos 	}
   9372   1.1  christos 
   9373   1.1  christos       ops->create_breakpoints_sal (gdbarch, &canonical,
   9374   1.1  christos 				   std::move (cond_string_copy),
   9375  1.10  christos 				   std::move (extra_string_copy),
   9376  1.10  christos 				   type_wanted,
   9377  1.10  christos 				   tempflag ? disp_del : disp_donttouch,
   9378   1.1  christos 				   thread, task, inferior, ignore_count,
   9379   1.6  christos 				   from_tty, enabled, internal, flags);
   9380   1.6  christos     }
   9381   1.1  christos   else
   9382   1.1  christos     {
   9383   1.1  christos       std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
   9384  1.10  christos 								 type_wanted);
   9385  1.10  christos       b->locspec = locspec->clone ();
   9386  1.10  christos 
   9387   1.5  christos       if (parse_extra)
   9388   1.1  christos 	b->cond_string = NULL;
   9389   1.6  christos       else
   9390   1.6  christos 	{
   9391  1.10  christos 	  /* Create a private copy of condition string.  */
   9392  1.10  christos 	  b->cond_string.reset (cond_string != NULL
   9393  1.10  christos 				? xstrdup (cond_string)
   9394   1.1  christos 				: NULL);
   9395   1.1  christos 	  b->thread = thread;
   9396   1.1  christos 	}
   9397   1.1  christos 
   9398   1.1  christos       /* Create a private copy of any extra string.  */
   9399  1.10  christos       b->extra_string.reset (extra_string != NULL
   9400   1.1  christos 			     ? xstrdup (extra_string)
   9401   1.1  christos 			     : NULL);
   9402   1.8  christos       b->ignore_count = ignore_count;
   9403   1.1  christos       b->disposition = tempflag ? disp_del : disp_donttouch;
   9404   1.1  christos       b->condition_not_parsed = 1;
   9405   1.8  christos       b->enable_state = enabled ? bp_enabled : bp_disabled;
   9406   1.1  christos       if ((type_wanted != bp_breakpoint
   9407   1.1  christos 	   && type_wanted != bp_hardware_breakpoint) || thread != -1)
   9408   1.1  christos 	b->pspace = current_program_space;
   9409   1.1  christos 
   9410   1.1  christos       install_breakpoint (internal, std::move (b), 0);
   9411   1.1  christos     }
   9412   1.3  christos 
   9413   1.1  christos   if (canonical.lsals.size () > 1)
   9414   1.1  christos     {
   9415   1.1  christos       warning (_("Multiple breakpoints were set.\nUse the "
   9416   1.1  christos 		 "\"delete\" command to delete unwanted breakpoints."));
   9417   1.1  christos       prev_breakpoint_count = prev_bkpt_count;
   9418   1.1  christos     }
   9419   1.1  christos 
   9420   1.1  christos   update_global_location_list (UGLL_MAY_INSERT);
   9421   1.1  christos 
   9422   1.1  christos   return 1;
   9423   1.1  christos }
   9424   1.1  christos 
   9425   1.8  christos /* Set a breakpoint.
   9426   1.1  christos    ARG is a string describing breakpoint address,
   9427   1.1  christos    condition, and thread.
   9428   1.1  christos    FLAG specifies if a breakpoint is hardware on,
   9429   1.1  christos    and if breakpoint is temporary, using BP_HARDWARE_FLAG
   9430   1.1  christos    and BP_TEMPFLAG.  */
   9431   1.6  christos 
   9432  1.10  christos static void
   9433  1.10  christos break_command_1 (const char *arg, int flag, int from_tty)
   9434  1.10  christos {
   9435  1.10  christos   int tempflag = flag & BP_TEMPFLAG;
   9436   1.1  christos   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
   9437   1.1  christos 			     ? bp_hardware_breakpoint
   9438  1.10  christos 			     : bp_breakpoint);
   9439  1.11  christos 
   9440  1.11  christos   location_spec_up locspec = string_to_location_spec (&arg, current_language);
   9441  1.11  christos   const struct breakpoint_ops *ops
   9442   1.1  christos     = breakpoint_ops_for_location_spec (locspec.get (),
   9443   1.1  christos 					false /* is_tracepoint */);
   9444   1.1  christos 
   9445   1.1  christos   create_breakpoint (get_current_arch (),
   9446   1.1  christos 		     locspec.get (),
   9447   1.1  christos 		     NULL,
   9448   1.1  christos 		     -1 /* thread */, -1 /* inferior */,
   9449   1.1  christos 		     arg, false, 1 /* parse arg */,
   9450   1.1  christos 		     tempflag, type_wanted,
   9451   1.1  christos 		     0 /* Ignore count */,
   9452   1.1  christos 		     pending_break_support,
   9453   1.1  christos 		     ops,
   9454   1.1  christos 		     from_tty,
   9455   1.1  christos 		     1 /* enabled */,
   9456   1.1  christos 		     0 /* internal */,
   9457   1.1  christos 		     0);
   9458   1.1  christos }
   9459   1.1  christos 
   9460   1.1  christos /* Helper function for break_command_1 and disassemble_command.  */
   9461   1.1  christos 
   9462   1.1  christos void
   9463   1.1  christos resolve_sal_pc (struct symtab_and_line *sal)
   9464   1.1  christos {
   9465   1.1  christos   CORE_ADDR pc;
   9466   1.1  christos 
   9467  1.10  christos   if (sal->pc == 0 && sal->symtab != NULL)
   9468   1.1  christos     {
   9469   1.1  christos       if (!find_line_pc (sal->symtab, sal->line, &pc))
   9470   1.1  christos 	error (_("No line %d in file \"%s\"."),
   9471   1.1  christos 	       sal->line, symtab_to_filename_for_display (sal->symtab));
   9472   1.1  christos       sal->pc = pc;
   9473   1.1  christos 
   9474   1.3  christos       /* If this SAL corresponds to a breakpoint inserted using a line
   9475   1.3  christos 	 number, then skip the function prologue if necessary.  */
   9476   1.1  christos       if (sal->explicit_line)
   9477   1.1  christos 	skip_prologue_sal (sal);
   9478   1.3  christos     }
   9479  1.10  christos 
   9480   1.1  christos   if (sal->section == 0 && sal->symtab != NULL)
   9481   1.1  christos     {
   9482  1.11  christos       const struct blockvector *bv;
   9483   1.1  christos       const struct block *b;
   9484  1.11  christos       struct symbol *sym;
   9485  1.11  christos 
   9486   1.1  christos       bv = blockvector_for_pc_sect (sal->pc, 0, &b,
   9487   1.1  christos 				    sal->symtab->compunit ());
   9488   1.1  christos       if (bv != NULL)
   9489  1.10  christos 	{
   9490  1.10  christos 	  sym = b->linkage_function ();
   9491  1.10  christos 	  if (sym != NULL)
   9492   1.1  christos 	    sal->section
   9493   1.8  christos 	      = sym->obj_section (sal->symtab->compunit ()->objfile ());
   9494   1.1  christos 	  else
   9495   1.1  christos 	    {
   9496   1.8  christos 	      /* It really is worthwhile to have the section, so we'll
   9497   1.1  christos 		 just have to look harder. This case can be executed
   9498  1.10  christos 		 if we have line numbers but no functions (as can
   9499   1.1  christos 		 happen in assembly source).  */
   9500   1.1  christos 
   9501   1.1  christos 	      scoped_restore_current_pspace_and_thread restore_pspace_thread;
   9502   1.1  christos 	      switch_to_program_space_and_thread (sal->pspace);
   9503   1.1  christos 
   9504   1.1  christos 	      bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
   9505   1.8  christos 	      if (msym.minsym)
   9506   1.1  christos 		sal->section = msym.obj_section ();
   9507   1.1  christos 	    }
   9508   1.1  christos 	}
   9509   1.1  christos     }
   9510   1.1  christos }
   9511   1.8  christos 
   9512   1.1  christos void
   9513   1.1  christos break_command (const char *arg, int from_tty)
   9514   1.1  christos {
   9515   1.1  christos   break_command_1 (arg, 0, from_tty);
   9516   1.1  christos }
   9517   1.8  christos 
   9518   1.1  christos void
   9519   1.1  christos tbreak_command (const char *arg, int from_tty)
   9520   1.1  christos {
   9521   1.1  christos   break_command_1 (arg, BP_TEMPFLAG, from_tty);
   9522   1.1  christos }
   9523   1.8  christos 
   9524   1.1  christos static void
   9525   1.1  christos hbreak_command (const char *arg, int from_tty)
   9526   1.1  christos {
   9527   1.1  christos   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
   9528   1.1  christos }
   9529   1.1  christos 
   9530   1.1  christos static void
   9531   1.1  christos thbreak_command (const char *arg, int from_tty)
   9532   1.1  christos {
   9533   1.1  christos   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
   9534   1.8  christos }
   9535   1.1  christos 
   9536  1.10  christos /* The dynamic printf command is mostly like a regular breakpoint, but
   9537   1.6  christos    with a prewired command list consisting of a single output command,
   9538   1.6  christos    built from extra arguments supplied on the dprintf command
   9539   1.6  christos    line.  */
   9540  1.11  christos 
   9541  1.11  christos static void
   9542  1.11  christos dprintf_command (const char *arg, int from_tty)
   9543   1.6  christos {
   9544  1.11  christos   location_spec_up locspec = string_to_location_spec (&arg, current_language);
   9545  1.11  christos 
   9546   1.6  christos   /* If non-NULL, ARG should have been advanced past the location;
   9547   1.6  christos      the next character must be ','.  */
   9548   1.1  christos   if (arg == nullptr || arg[0] != ',' || arg[1] == '\0')
   9549  1.10  christos     error (_("Format string required"));
   9550  1.11  christos   else
   9551  1.11  christos     {
   9552   1.1  christos       /* Skip the comma.  */
   9553   1.1  christos       ++arg;
   9554   1.1  christos     }
   9555  1.10  christos 
   9556   1.1  christos   create_breakpoint (get_current_arch (),
   9557   1.1  christos 		     locspec.get (),
   9558   1.1  christos 		     NULL, -1, -1,
   9559   1.1  christos 		     arg, false, 0 /* parse arg */,
   9560   1.1  christos 		     0, bp_dprintf,
   9561   1.1  christos 		     0 /* Ignore count */,
   9562   1.1  christos 		     pending_break_support,
   9563   1.8  christos 		     &code_breakpoint_ops,
   9564   1.1  christos 		     from_tty,
   9565   1.1  christos 		     1 /* enabled */,
   9566   1.1  christos 		     0 /* internal */,
   9567   1.1  christos 		     0);
   9568  1.10  christos }
   9569   1.1  christos 
   9570  1.10  christos static void
   9571  1.10  christos agent_printf_command (const char *arg, int from_tty)
   9572  1.10  christos {
   9573  1.10  christos   error (_("May only run agent-printf on the target"));
   9574  1.10  christos }
   9575   1.1  christos 
   9576  1.10  christos /* Implement the "breakpoint_hit" method for ranged breakpoints.  */
   9577  1.10  christos 
   9578   1.1  christos int
   9579   1.1  christos ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
   9580  1.11  christos 				   const address_space *aspace,
   9581  1.11  christos 				   CORE_ADDR bp_addr,
   9582  1.11  christos 				   const target_waitstatus &ws)
   9583   1.1  christos {
   9584   1.1  christos   if (ws.kind () != TARGET_WAITKIND_STOPPED
   9585  1.10  christos       || ws.sig () != GDB_SIGNAL_TRAP)
   9586   1.1  christos     return 0;
   9587  1.10  christos 
   9588  1.10  christos   return breakpoint_address_match_range (bl->pspace->aspace.get (),
   9589   1.1  christos 					 bl->address, bl->length, aspace,
   9590   1.1  christos 					 bp_addr);
   9591   1.1  christos }
   9592   1.1  christos 
   9593  1.10  christos /* Implement the "resources_needed" method for ranged breakpoints.  */
   9594   1.1  christos 
   9595  1.10  christos int
   9596  1.10  christos ranged_breakpoint::resources_needed (const struct bp_location *bl)
   9597   1.1  christos {
   9598   1.1  christos   return target_ranged_break_num_registers ();
   9599   1.1  christos }
   9600  1.10  christos 
   9601   1.1  christos /* Implement the "print_it" method for ranged breakpoints.  */
   9602   1.1  christos 
   9603  1.11  christos enum print_stop_action
   9604   1.1  christos ranged_breakpoint::print_it (const bpstat *bs) const
   9605  1.10  christos {
   9606   1.6  christos   struct ui_out *uiout = current_uiout;
   9607   1.6  christos 
   9608   1.6  christos   gdb_assert (type == bp_hardware_breakpoint);
   9609  1.10  christos 
   9610   1.7  christos   /* Ranged breakpoints have only one location.  */
   9611   1.1  christos   gdb_assert (this->has_single_location ());
   9612   1.7  christos 
   9613   1.7  christos   annotate_breakpoint (number);
   9614   1.1  christos 
   9615   1.7  christos   maybe_print_thread_hit_breakpoint (uiout);
   9616   1.1  christos 
   9617  1.10  christos   if (disposition == disp_del)
   9618   1.1  christos     uiout->text ("Temporary ranged breakpoint ");
   9619  1.10  christos   else
   9620   1.7  christos     uiout->text ("Ranged breakpoint ");
   9621   1.1  christos   if (uiout->is_mi_like_p ())
   9622   1.1  christos     {
   9623   1.1  christos       uiout->field_string ("reason",
   9624   1.1  christos 		      async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
   9625  1.10  christos       uiout->field_string ("disp", bpdisp_text (disposition));
   9626   1.1  christos     }
   9627  1.10  christos   print_num_locno (bs, uiout);
   9628  1.11  christos   uiout->text (", ");
   9629   1.1  christos 
   9630   1.1  christos   return PRINT_SRC_AND_LOC;
   9631   1.1  christos }
   9632   1.1  christos 
   9633   1.1  christos /* Implement the "print_one" method for ranged breakpoints.  */
   9634  1.11  christos 
   9635   1.1  christos bool
   9636   1.1  christos ranged_breakpoint::print_one (const bp_location **last_loc) const
   9637   1.1  christos {
   9638   1.1  christos   struct value_print_options opts;
   9639   1.1  christos   struct ui_out *uiout = current_uiout;
   9640  1.10  christos 
   9641   1.7  christos   /* Ranged breakpoints have only one location.  */
   9642   1.1  christos   gdb_assert (this->has_single_location ());
   9643  1.11  christos 
   9644  1.11  christos   get_user_print_options (&opts);
   9645  1.10  christos 
   9646  1.10  christos   if (opts.addressprint)
   9647   1.1  christos     /* We don't print the address range here, it will be printed later
   9648   1.1  christos        by ranged_breakpoint::print_one_detail.  */
   9649  1.10  christos     uiout->field_skip ("addr");
   9650   1.1  christos   annotate_field (5);
   9651  1.10  christos   print_breakpoint_location (this, &this->first_loc ());
   9652  1.10  christos   *last_loc = &this->first_loc ();
   9653   1.1  christos 
   9654   1.1  christos   return true;
   9655  1.11  christos }
   9656   1.7  christos 
   9657   1.1  christos /* Implement the "print_one_detail" method for ranged breakpoints.  */
   9658  1.11  christos 
   9659  1.11  christos void
   9660   1.1  christos ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
   9661   1.7  christos {
   9662   1.7  christos   CORE_ADDR address_start, address_end;
   9663  1.11  christos   const bp_location &bl = this->first_loc ();
   9664  1.11  christos   string_file stb;
   9665   1.7  christos 
   9666   1.7  christos   address_start = bl.address;
   9667   1.1  christos   address_end = address_start + bl.length - 1;
   9668   1.1  christos 
   9669  1.10  christos   uiout->text ("\taddress range: ");
   9670   1.1  christos   stb.printf ("[%s, %s]",
   9671  1.10  christos 	      print_core_address (bl.gdbarch, address_start),
   9672  1.10  christos 	      print_core_address (bl.gdbarch, address_end));
   9673   1.1  christos   uiout->field_stream ("addr", stb);
   9674  1.11  christos   uiout->text ("\n");
   9675   1.1  christos }
   9676   1.1  christos 
   9677  1.10  christos /* Implement the "print_mention" method for ranged breakpoints.  */
   9678   1.1  christos 
   9679   1.8  christos void
   9680  1.11  christos ranged_breakpoint::print_mention () const
   9681  1.11  christos {
   9682   1.1  christos   const bp_location &bl = this->first_loc ();
   9683   1.1  christos   struct ui_out *uiout = current_uiout;
   9684  1.10  christos 
   9685   1.1  christos   gdb_assert (type == bp_hardware_breakpoint);
   9686  1.10  christos 
   9687  1.10  christos   uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
   9688   1.1  christos 		  number, paddress (bl.gdbarch, bl.address),
   9689  1.10  christos 		  paddress (bl.gdbarch, bl.address + bl.length - 1));
   9690  1.10  christos }
   9691  1.10  christos 
   9692  1.10  christos /* Implement the "print_recreate" method for ranged breakpoints.  */
   9693   1.1  christos 
   9694   1.1  christos void
   9695   1.1  christos ranged_breakpoint::print_recreate (struct ui_file *fp) const
   9696   1.1  christos {
   9697   1.1  christos   gdb_printf (fp, "break-range %s, %s",
   9698   1.1  christos 	      locspec->to_string (),
   9699   1.1  christos 	      locspec_range_end->to_string ());
   9700   1.1  christos   print_recreate_thread (fp);
   9701   1.1  christos }
   9702   1.1  christos 
   9703   1.1  christos /* Find the address where the end of the breakpoint range should be
   9704   1.1  christos    placed, given the SAL of the end of the range.  This is so that if
   9705   1.1  christos    the user provides a line number, the end of the range is set to the
   9706   1.1  christos    last instruction of the given line.  */
   9707   1.1  christos 
   9708   1.1  christos static CORE_ADDR
   9709   1.1  christos find_breakpoint_range_end (struct symtab_and_line sal)
   9710   1.1  christos {
   9711   1.1  christos   CORE_ADDR end;
   9712   1.1  christos 
   9713   1.1  christos   /* If the user provided a PC value, use it.  Otherwise,
   9714   1.1  christos      find the address of the end of the given location.  */
   9715   1.1  christos   if (sal.explicit_pc)
   9716   1.1  christos     end = sal.pc;
   9717   1.1  christos   else
   9718   1.1  christos     {
   9719   1.1  christos       int ret;
   9720   1.1  christos       CORE_ADDR start;
   9721   1.1  christos 
   9722   1.1  christos       ret = find_line_pc_range (sal, &start, &end);
   9723   1.1  christos       if (!ret)
   9724   1.1  christos 	error (_("Could not find location of the end of the range."));
   9725   1.1  christos 
   9726   1.1  christos       /* find_line_pc_range returns the start of the next line.  */
   9727   1.1  christos       end--;
   9728   1.8  christos     }
   9729   1.1  christos 
   9730   1.8  christos   return end;
   9731   1.1  christos }
   9732   1.1  christos 
   9733   1.1  christos /* Implement the "break-range" CLI command.  */
   9734   1.1  christos 
   9735   1.1  christos static void
   9736   1.1  christos break_range_command (const char *arg, int from_tty)
   9737   1.1  christos {
   9738   1.1  christos   const char *arg_start;
   9739   1.1  christos   struct linespec_result canonical_start, canonical_end;
   9740   1.1  christos   int bp_count, can_use_bp, length;
   9741   1.1  christos   CORE_ADDR end;
   9742   1.1  christos 
   9743   1.1  christos   /* We don't support software ranged breakpoints.  */
   9744   1.1  christos   if (target_ranged_break_num_registers () < 0)
   9745   1.1  christos     error (_("This target does not support hardware ranged breakpoints."));
   9746   1.1  christos 
   9747   1.1  christos   bp_count = hw_breakpoint_used_count ();
   9748   1.1  christos   bp_count += target_ranged_break_num_registers ();
   9749   1.1  christos   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
   9750   1.1  christos 						   bp_count, 0);
   9751  1.10  christos   if (can_use_bp < 0)
   9752  1.10  christos     error (_("Hardware breakpoints used exceeds limit."));
   9753  1.10  christos 
   9754   1.1  christos   arg = skip_spaces (arg);
   9755   1.1  christos   if (arg == NULL || arg[0] == '\0')
   9756   1.1  christos     error(_("No address range specified."));
   9757   1.8  christos 
   9758   1.1  christos   arg_start = arg;
   9759   1.1  christos   location_spec_up start_locspec
   9760   1.8  christos     = string_to_location_spec (&arg, current_language);
   9761   1.1  christos   parse_breakpoint_sals (start_locspec.get (), &canonical_start);
   9762   1.8  christos 
   9763   1.8  christos   if (arg[0] != ',')
   9764   1.1  christos     error (_("Too few arguments."));
   9765   1.1  christos   else if (canonical_start.lsals.empty ())
   9766   1.8  christos     error (_("Could not find location of the beginning of the range."));
   9767   1.8  christos 
   9768   1.1  christos   const linespec_sals &lsal_start = canonical_start.lsals[0];
   9769   1.1  christos 
   9770   1.1  christos   if (canonical_start.lsals.size () > 1
   9771   1.1  christos       || lsal_start.sals.size () != 1)
   9772  1.10  christos     error (_("Cannot create a ranged breakpoint with multiple locations."));
   9773   1.1  christos 
   9774   1.1  christos   const symtab_and_line &sal_start = lsal_start.sals[0];
   9775   1.1  christos   std::string addr_string_start (arg_start, arg - arg_start);
   9776   1.1  christos 
   9777  1.10  christos   arg++;	/* Skip the comma.  */
   9778  1.10  christos   arg = skip_spaces (arg);
   9779  1.10  christos 
   9780  1.10  christos   /* Parse the end location specification.  */
   9781  1.10  christos 
   9782  1.10  christos   arg_start = arg;
   9783  1.10  christos 
   9784  1.10  christos   /* We call decode_line_full directly here instead of using
   9785   1.1  christos      parse_breakpoint_sals because we need to specify the start
   9786   1.1  christos      location spec's symtab and line as the default symtab and line
   9787   1.1  christos      for the end of the range.  This makes it possible to have ranges
   9788   1.8  christos      like "foo.c:27, +14", where +14 means 14 lines from the start
   9789   1.1  christos      location spec.  */
   9790   1.1  christos   location_spec_up end_locspec
   9791   1.8  christos     = string_to_location_spec (&arg, current_language);
   9792   1.8  christos   decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
   9793   1.8  christos 		    sal_start.symtab, sal_start.line,
   9794   1.1  christos 		    &canonical_end, NULL, NULL);
   9795   1.1  christos 
   9796   1.8  christos   if (canonical_end.lsals.empty ())
   9797   1.1  christos     error (_("Could not find location of the end of the range."));
   9798   1.1  christos 
   9799   1.1  christos   const linespec_sals &lsal_end = canonical_end.lsals[0];
   9800   1.1  christos   if (canonical_end.lsals.size () > 1
   9801   1.1  christos       || lsal_end.sals.size () != 1)
   9802   1.1  christos     error (_("Cannot create a ranged breakpoint with multiple locations."));
   9803   1.1  christos 
   9804   1.1  christos   const symtab_and_line &sal_end = lsal_end.sals[0];
   9805   1.1  christos 
   9806   1.1  christos   end = find_breakpoint_range_end (sal_end);
   9807   1.1  christos   if (sal_start.pc > end)
   9808   1.1  christos     error (_("Invalid address range, end precedes start."));
   9809   1.1  christos 
   9810   1.8  christos   length = end - sal_start.pc + 1;
   9811   1.1  christos   if (length < 0)
   9812   1.1  christos     /* Length overflowed.  */
   9813   1.1  christos     error (_("Address range too large."));
   9814   1.1  christos   else if (length == 1)
   9815  1.10  christos     {
   9816  1.10  christos       /* This range is simple enough to be handled by
   9817  1.10  christos 	 the `hbreak' command.  */
   9818  1.10  christos       hbreak_command (&addr_string_start[0], 1);
   9819  1.10  christos 
   9820  1.10  christos       return;
   9821  1.10  christos     }
   9822   1.1  christos 
   9823  1.10  christos   /* Now set up the breakpoint and install it.  */
   9824   1.1  christos 
   9825   1.1  christos   std::unique_ptr<breakpoint> br
   9826  1.11  christos     (new ranged_breakpoint (get_current_arch (),
   9827  1.11  christos 			    sal_start, length,
   9828  1.11  christos 			    std::move (start_locspec),
   9829  1.11  christos 			    std::move (end_locspec)));
   9830  1.11  christos 
   9831  1.11  christos   install_breakpoint (false, std::move (br), true);
   9832  1.11  christos }
   9833  1.11  christos 
   9834  1.11  christos /* See breakpoint.h.  */
   9835  1.11  christos 
   9836  1.11  christos watchpoint::~watchpoint ()
   9837  1.11  christos {
   9838  1.11  christos   /* Make sure to unlink the destroyed watchpoint from the related
   9839  1.11  christos      breakpoint ring.  */
   9840   1.1  christos 
   9841   1.1  christos   breakpoint *bpt = this;
   9842   1.1  christos   while (bpt->related_breakpoint != this)
   9843   1.1  christos     bpt = bpt->related_breakpoint;
   9844   1.1  christos 
   9845  1.10  christos   bpt->related_breakpoint = this->related_breakpoint;
   9846   1.1  christos }
   9847   1.1  christos 
   9848  1.10  christos /*  Return non-zero if EXP is verified as constant.  Returned zero
   9849   1.1  christos     means EXP is variable.  Also the constant detection may fail for
   9850   1.1  christos     some constant expressions and in such case still falsely return
   9851  1.10  christos     zero.  */
   9852   1.1  christos 
   9853  1.10  christos static bool
   9854  1.10  christos watchpoint_exp_is_const (const struct expression *exp)
   9855   1.1  christos {
   9856   1.1  christos   return exp->op->constant_p ();
   9857   1.1  christos }
   9858   1.1  christos 
   9859   1.1  christos /* Implement the "re_set" method for watchpoints.  */
   9860   1.1  christos 
   9861   1.1  christos void
   9862   1.1  christos watchpoint::re_set ()
   9863   1.1  christos {
   9864   1.1  christos   /* Watchpoint can be either on expression using entirely global
   9865   1.1  christos      variables, or it can be on local variables.
   9866   1.1  christos 
   9867   1.1  christos      Watchpoints of the first kind are never auto-deleted, and even
   9868   1.1  christos      persist across program restarts.  Since they can use variables
   9869   1.1  christos      from shared libraries, we need to reparse expression as libraries
   9870   1.1  christos      are loaded and unloaded.
   9871   1.1  christos 
   9872   1.1  christos      Watchpoints on local variables can also change meaning as result
   9873   1.1  christos      of solib event.  For example, if a watchpoint uses both a local
   9874   1.1  christos      and a global variables in expression, it's a local watchpoint,
   9875   1.1  christos      but unloading of a shared library will make the expression
   9876   1.1  christos      invalid.  This is not a very common use case, but we still
   9877  1.10  christos      re-evaluate expression, to avoid surprises to the user.
   9878   1.1  christos 
   9879   1.1  christos      Note that for local watchpoints, we re-evaluate it only if
   9880   1.1  christos      watchpoints frame id is still valid.  If it's not, it means the
   9881  1.10  christos      watchpoint is out of scope and will be deleted soon.  In fact,
   9882   1.1  christos      I'm not sure we'll ever be called in this case.
   9883   1.1  christos 
   9884  1.10  christos      If a local watchpoint's frame id is still valid, then
   9885   1.1  christos      exp_valid_block is likewise valid, and we can safely use it.
   9886  1.10  christos 
   9887  1.10  christos      Don't do anything about disabled watchpoints, since they will be
   9888   1.1  christos      reevaluated again when enabled.  */
   9889  1.10  christos   update_watchpoint (this, true /* reparse */);
   9890   1.1  christos }
   9891   1.1  christos 
   9892  1.10  christos /* Implement the "insert" method for hardware watchpoints.  */
   9893   1.1  christos 
   9894   1.1  christos int
   9895  1.10  christos watchpoint::insert_location (struct bp_location *bl)
   9896   1.1  christos {
   9897  1.10  christos   int length = exact ? 1 : bl->length;
   9898  1.10  christos 
   9899  1.10  christos   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
   9900   1.1  christos 				   cond_exp.get ());
   9901  1.10  christos }
   9902   1.1  christos 
   9903   1.1  christos /* Implement the "remove" method for hardware watchpoints.  */
   9904  1.10  christos 
   9905   1.1  christos int
   9906   1.1  christos watchpoint::remove_location (struct bp_location *bl,
   9907  1.10  christos 			     enum remove_bp_reason reason)
   9908  1.10  christos {
   9909  1.10  christos   int length = exact ? 1 : bl->length;
   9910  1.10  christos 
   9911   1.1  christos   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
   9912   1.1  christos 				   cond_exp.get ());
   9913   1.1  christos }
   9914   1.1  christos 
   9915   1.1  christos int
   9916   1.1  christos watchpoint::breakpoint_hit (const struct bp_location *bl,
   9917   1.1  christos 			    const address_space *aspace, CORE_ADDR bp_addr,
   9918   1.1  christos 			    const target_waitstatus &ws)
   9919   1.1  christos {
   9920   1.1  christos   struct breakpoint *b = bl->owner;
   9921  1.10  christos 
   9922   1.1  christos   /* Continuable hardware watchpoints are treated as non-existent if the
   9923   1.1  christos      reason we stopped wasn't a hardware watchpoint (we didn't stop on
   9924   1.1  christos      some data address).  Otherwise gdb won't stop on a break instruction
   9925   1.1  christos      in the code (not from a breakpoint) when a hardware watchpoint has
   9926   1.1  christos      been defined.  Also skip watchpoints which we know did not trigger
   9927  1.10  christos      (did not match the data address).  */
   9928  1.10  christos   if (is_hardware_watchpoint (b)
   9929   1.1  christos       && watchpoint_triggered == watch_triggered_no)
   9930   1.1  christos     return 0;
   9931   1.1  christos 
   9932   1.1  christos   return 1;
   9933  1.10  christos }
   9934  1.10  christos 
   9935   1.1  christos void
   9936  1.10  christos watchpoint::check_status (bpstat *bs)
   9937  1.10  christos {
   9938   1.1  christos   bpstat_check_watchpoint (bs);
   9939  1.10  christos }
   9940   1.1  christos 
   9941   1.1  christos /* Implement the "resources_needed" method for hardware
   9942   1.1  christos    watchpoints.  */
   9943   1.1  christos 
   9944  1.10  christos int
   9945  1.10  christos watchpoint::resources_needed (const struct bp_location *bl)
   9946   1.1  christos {
   9947  1.10  christos   int length = exact? 1 : bl->length;
   9948  1.10  christos 
   9949   1.1  christos   return target_region_ok_for_hw_watchpoint (bl->address, length);
   9950   1.1  christos }
   9951  1.10  christos 
   9952   1.1  christos /* Implement the "works_in_software_mode" method for hardware
   9953   1.1  christos    watchpoints.  */
   9954  1.10  christos 
   9955  1.10  christos bool
   9956   1.1  christos watchpoint::works_in_software_mode () const
   9957   1.1  christos {
   9958   1.1  christos   /* Read and access watchpoints only work with hardware support.  */
   9959   1.1  christos   return type == bp_watchpoint || type == bp_hardware_watchpoint;
   9960   1.1  christos }
   9961   1.1  christos 
   9962  1.11  christos enum print_stop_action
   9963   1.6  christos watchpoint::print_it (const bpstat *bs) const
   9964   1.6  christos {
   9965   1.7  christos   enum print_stop_action result;
   9966   1.7  christos   struct ui_out *uiout = current_uiout;
   9967  1.11  christos 
   9968  1.11  christos   gdb_assert (bs->bp_location_at != NULL);
   9969   1.1  christos 
   9970   1.1  christos   annotate_watchpoint (this->number);
   9971   1.1  christos   maybe_print_thread_hit_breakpoint (uiout);
   9972   1.7  christos 
   9973   1.7  christos   string_file stb;
   9974   1.7  christos 
   9975  1.11  christos   std::optional<ui_out_emit_tuple> tuple_emitter;
   9976   1.8  christos   switch (this->type)
   9977   1.7  christos     {
   9978   1.8  christos     case bp_watchpoint:
   9979   1.7  christos     case bp_hardware_watchpoint:
   9980   1.7  christos       if (uiout->is_mi_like_p ())
   9981  1.10  christos 	uiout->field_string
   9982   1.7  christos 	  ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
   9983   1.7  christos       mention (this);
   9984   1.1  christos       tuple_emitter.emplace (uiout, "value");
   9985   1.1  christos       uiout->text ("\nOld value = ");
   9986   1.1  christos       watchpoint_value_print (bs->old_val.get (), &stb);
   9987   1.1  christos       uiout->field_stream ("old", stb);
   9988   1.1  christos       uiout->text ("\nNew value = ");
   9989   1.7  christos       watchpoint_value_print (val.get (), &stb);
   9990   1.7  christos       uiout->field_stream ("new", stb);
   9991   1.7  christos       uiout->text ("\n");
   9992  1.11  christos       /* More than one watchpoint may have been triggered.  */
   9993   1.8  christos       result = PRINT_UNKNOWN;
   9994   1.7  christos       break;
   9995  1.10  christos 
   9996   1.7  christos     case bp_read_watchpoint:
   9997   1.7  christos       if (uiout->is_mi_like_p ())
   9998   1.1  christos 	uiout->field_string
   9999   1.1  christos 	  ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
   10000   1.1  christos       mention (this);
   10001   1.1  christos       tuple_emitter.emplace (uiout, "value");
   10002   1.1  christos       uiout->text ("\nValue = ");
   10003   1.1  christos       watchpoint_value_print (val.get (), &stb);
   10004   1.7  christos       uiout->field_stream ("value", stb);
   10005   1.7  christos       uiout->text ("\n");
   10006   1.7  christos       result = PRINT_UNKNOWN;
   10007   1.1  christos       break;
   10008  1.11  christos 
   10009   1.8  christos     case bp_access_watchpoint:
   10010   1.7  christos       if (bs->old_val != NULL)
   10011   1.8  christos 	{
   10012   1.7  christos 	  if (uiout->is_mi_like_p ())
   10013   1.7  christos 	    uiout->field_string
   10014   1.1  christos 	      ("reason",
   10015   1.1  christos 	       async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
   10016   1.1  christos 	  mention (this);
   10017  1.11  christos 	  tuple_emitter.emplace (uiout, "value");
   10018   1.7  christos 	  uiout->text ("\nOld value = ");
   10019   1.7  christos 	  watchpoint_value_print (bs->old_val.get (), &stb);
   10020   1.7  christos 	  uiout->field_stream ("old", stb);
   10021   1.1  christos 	  uiout->text ("\nNew value = ");
   10022   1.8  christos 	}
   10023   1.7  christos       else
   10024   1.1  christos 	{
   10025  1.10  christos 	  mention (this);
   10026   1.7  christos 	  if (uiout->is_mi_like_p ())
   10027   1.7  christos 	    uiout->field_string
   10028   1.1  christos 	      ("reason",
   10029   1.1  christos 	       async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
   10030   1.1  christos 	  tuple_emitter.emplace (uiout, "value");
   10031   1.1  christos 	  uiout->text ("\nValue = ");
   10032   1.1  christos 	}
   10033   1.1  christos       watchpoint_value_print (val.get (), &stb);
   10034   1.1  christos       uiout->field_stream ("new", stb);
   10035   1.1  christos       uiout->text ("\n");
   10036   1.1  christos       result = PRINT_UNKNOWN;
   10037  1.10  christos       break;
   10038   1.1  christos     default:
   10039  1.10  christos       result = PRINT_UNKNOWN;
   10040  1.10  christos     }
   10041   1.1  christos 
   10042   1.1  christos   return result;
   10043   1.8  christos }
   10044   1.1  christos 
   10045  1.10  christos /* Implement the "print_mention" method for hardware watchpoints.  */
   10046   1.1  christos 
   10047   1.1  christos void
   10048   1.7  christos watchpoint::print_mention () const
   10049   1.8  christos {
   10050   1.1  christos   struct ui_out *uiout = current_uiout;
   10051   1.1  christos   const char *tuple_name;
   10052   1.7  christos 
   10053   1.8  christos   switch (type)
   10054   1.1  christos     {
   10055   1.1  christos     case bp_watchpoint:
   10056   1.7  christos       uiout->text ("Watchpoint ");
   10057   1.8  christos       tuple_name = "wpt";
   10058   1.1  christos       break;
   10059   1.1  christos     case bp_hardware_watchpoint:
   10060   1.7  christos       uiout->text ("Hardware watchpoint ");
   10061   1.8  christos       tuple_name = "wpt";
   10062   1.1  christos       break;
   10063   1.1  christos     case bp_read_watchpoint:
   10064  1.10  christos       uiout->text ("Hardware read watchpoint ");
   10065   1.1  christos       tuple_name = "hw-rwpt";
   10066   1.1  christos       break;
   10067   1.8  christos     case bp_access_watchpoint:
   10068  1.10  christos       uiout->text ("Hardware access (read/write) watchpoint ");
   10069   1.7  christos       tuple_name = "hw-awpt";
   10070  1.10  christos       break;
   10071   1.1  christos     default:
   10072   1.1  christos       internal_error (_("Invalid hardware watchpoint type."));
   10073  1.10  christos     }
   10074   1.1  christos 
   10075  1.10  christos   ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
   10076  1.10  christos   uiout->field_signed ("number", number);
   10077   1.1  christos   uiout->text (": ");
   10078  1.10  christos   uiout->field_string ("exp", exp_string.get ());
   10079   1.1  christos }
   10080   1.1  christos 
   10081   1.1  christos /* Implement the "print_recreate" method for watchpoints.  */
   10082  1.10  christos 
   10083   1.1  christos void
   10084   1.1  christos watchpoint::print_recreate (struct ui_file *fp) const
   10085  1.10  christos {
   10086   1.1  christos   switch (type)
   10087   1.1  christos     {
   10088  1.10  christos     case bp_watchpoint:
   10089   1.1  christos     case bp_hardware_watchpoint:
   10090   1.1  christos       gdb_printf (fp, "watch");
   10091  1.10  christos       break;
   10092   1.1  christos     case bp_read_watchpoint:
   10093   1.1  christos       gdb_printf (fp, "rwatch");
   10094  1.10  christos       break;
   10095  1.10  christos     case bp_access_watchpoint:
   10096   1.1  christos       gdb_printf (fp, "awatch");
   10097   1.1  christos       break;
   10098  1.10  christos     default:
   10099   1.1  christos       internal_error (_("Invalid watchpoint type."));
   10100  1.10  christos     }
   10101  1.10  christos 
   10102   1.1  christos   gdb_printf (fp, " %s", exp_string.get ());
   10103   1.1  christos   print_recreate_thread (fp);
   10104   1.1  christos }
   10105  1.10  christos 
   10106  1.10  christos /* Implement the "explains_signal" method for watchpoints.  */
   10107   1.1  christos 
   10108  1.10  christos bool
   10109   1.1  christos watchpoint::explains_signal (enum gdb_signal sig)
   10110   1.1  christos {
   10111  1.10  christos   /* A software watchpoint cannot cause a signal other than
   10112  1.10  christos      GDB_SIGNAL_TRAP.  */
   10113  1.10  christos   if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
   10114   1.1  christos     return false;
   10115  1.10  christos 
   10116  1.10  christos   return true;
   10117  1.10  christos }
   10118  1.10  christos 
   10119  1.10  christos struct masked_watchpoint : public watchpoint
   10120  1.10  christos {
   10121  1.10  christos   using watchpoint::watchpoint;
   10122  1.10  christos 
   10123  1.10  christos   int insert_location (struct bp_location *) override;
   10124  1.10  christos   int remove_location (struct bp_location *,
   10125   1.1  christos 		       enum remove_bp_reason reason) override;
   10126  1.10  christos   int resources_needed (const struct bp_location *) override;
   10127   1.1  christos   bool works_in_software_mode () const override;
   10128  1.10  christos   enum print_stop_action print_it (const bpstat *bs) const override;
   10129  1.10  christos   void print_one_detail (struct ui_out *) const override;
   10130   1.1  christos   void print_mention () const override;
   10131  1.10  christos   void print_recreate (struct ui_file *fp) const override;
   10132   1.1  christos };
   10133   1.1  christos 
   10134   1.1  christos /* Implement the "insert" method for masked hardware watchpoints.  */
   10135  1.10  christos 
   10136   1.1  christos int
   10137  1.10  christos masked_watchpoint::insert_location (struct bp_location *bl)
   10138  1.10  christos {
   10139  1.10  christos   return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
   10140   1.1  christos 					bl->watchpoint_type);
   10141  1.10  christos }
   10142  1.10  christos 
   10143   1.1  christos /* Implement the "remove" method for masked hardware watchpoints.  */
   10144   1.1  christos 
   10145  1.10  christos int
   10146  1.10  christos masked_watchpoint::remove_location (struct bp_location *bl,
   10147   1.1  christos 				    enum remove_bp_reason reason)
   10148  1.10  christos {
   10149  1.10  christos   return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
   10150   1.1  christos 					bl->watchpoint_type);
   10151  1.10  christos }
   10152   1.1  christos 
   10153   1.1  christos /* Implement the "resources_needed" method for masked hardware
   10154  1.10  christos    watchpoints.  */
   10155  1.10  christos 
   10156   1.1  christos int
   10157  1.10  christos masked_watchpoint::resources_needed (const struct bp_location *bl)
   10158  1.10  christos {
   10159   1.1  christos   return target_masked_watch_num_registers (bl->address, hw_wp_mask);
   10160  1.10  christos }
   10161   1.1  christos 
   10162   1.1  christos /* Implement the "works_in_software_mode" method for masked hardware
   10163  1.10  christos    watchpoints.  */
   10164  1.10  christos 
   10165   1.1  christos bool
   10166  1.10  christos masked_watchpoint::works_in_software_mode () const
   10167  1.10  christos {
   10168   1.1  christos   return false;
   10169   1.1  christos }
   10170   1.1  christos 
   10171   1.1  christos /* Implement the "print_it" method for masked hardware
   10172  1.11  christos    watchpoints.  */
   10173   1.1  christos 
   10174  1.11  christos enum print_stop_action
   10175   1.6  christos masked_watchpoint::print_it (const bpstat *bs) const
   10176   1.6  christos {
   10177  1.11  christos   struct ui_out *uiout = current_uiout;
   10178   1.1  christos 
   10179   1.1  christos   /* Masked watchpoints have only one location.  */
   10180   1.7  christos   gdb_assert (this->has_single_location ());
   10181   1.7  christos 
   10182   1.7  christos   annotate_watchpoint (this->number);
   10183   1.1  christos   maybe_print_thread_hit_breakpoint (uiout);
   10184   1.1  christos 
   10185   1.1  christos   switch (this->type)
   10186   1.7  christos     {
   10187   1.7  christos     case bp_hardware_watchpoint:
   10188   1.7  christos       if (uiout->is_mi_like_p ())
   10189   1.1  christos 	uiout->field_string
   10190   1.1  christos 	  ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
   10191   1.1  christos       break;
   10192   1.7  christos 
   10193   1.7  christos     case bp_read_watchpoint:
   10194   1.7  christos       if (uiout->is_mi_like_p ())
   10195   1.1  christos 	uiout->field_string
   10196   1.1  christos 	  ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
   10197   1.1  christos       break;
   10198  1.10  christos 
   10199   1.1  christos     case bp_access_watchpoint:
   10200   1.1  christos       if (uiout->is_mi_like_p ())
   10201  1.11  christos 	uiout->field_string
   10202   1.7  christos 	  ("reason",
   10203   1.1  christos 	   async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
   10204   1.1  christos       break;
   10205   1.7  christos     default:
   10206   1.1  christos       internal_error (_("Invalid hardware watchpoint type."));
   10207   1.1  christos     }
   10208   1.1  christos 
   10209   1.1  christos   mention (this);
   10210   1.1  christos   uiout->text (_("\n\
   10211  1.10  christos Check the underlying instruction at PC for the memory\n\
   10212  1.10  christos address and value which triggered this watchpoint.\n"));
   10213   1.1  christos   uiout->text ("\n");
   10214  1.10  christos 
   10215  1.10  christos   /* More than one watchpoint may have been triggered.  */
   10216   1.1  christos   return PRINT_UNKNOWN;
   10217   1.1  christos }
   10218  1.11  christos 
   10219   1.1  christos /* Implement the "print_one_detail" method for masked hardware
   10220   1.7  christos    watchpoints.  */
   10221  1.11  christos 
   10222   1.7  christos void
   10223   1.1  christos masked_watchpoint::print_one_detail (struct ui_out *uiout) const
   10224   1.1  christos {
   10225  1.10  christos   /* Masked watchpoints have only one location.  */
   10226  1.10  christos   gdb_assert (this->has_single_location ());
   10227   1.1  christos 
   10228  1.10  christos   uiout->text ("\tmask ");
   10229  1.10  christos   uiout->field_core_addr ("mask", this->first_loc ().gdbarch, hw_wp_mask);
   10230   1.1  christos   uiout->text ("\n");
   10231   1.1  christos }
   10232   1.8  christos 
   10233   1.1  christos /* Implement the "print_mention" method for masked hardware
   10234  1.10  christos    watchpoints.  */
   10235   1.1  christos 
   10236   1.1  christos void
   10237   1.7  christos masked_watchpoint::print_mention () const
   10238   1.8  christos {
   10239   1.1  christos   struct ui_out *uiout = current_uiout;
   10240   1.1  christos   const char *tuple_name;
   10241   1.7  christos 
   10242   1.8  christos   switch (type)
   10243   1.1  christos     {
   10244   1.1  christos     case bp_hardware_watchpoint:
   10245   1.7  christos       uiout->text ("Masked hardware watchpoint ");
   10246   1.8  christos       tuple_name = "wpt";
   10247   1.1  christos       break;
   10248   1.1  christos     case bp_read_watchpoint:
   10249  1.10  christos       uiout->text ("Masked hardware read watchpoint ");
   10250   1.1  christos       tuple_name = "hw-rwpt";
   10251   1.1  christos       break;
   10252   1.8  christos     case bp_access_watchpoint:
   10253  1.10  christos       uiout->text ("Masked hardware access (read/write) watchpoint ");
   10254   1.7  christos       tuple_name = "hw-awpt";
   10255  1.10  christos       break;
   10256   1.1  christos     default:
   10257   1.1  christos       internal_error (_("Invalid hardware watchpoint type."));
   10258  1.10  christos     }
   10259  1.10  christos 
   10260   1.1  christos   ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
   10261  1.10  christos   uiout->field_signed ("number", number);
   10262  1.10  christos   uiout->text (": ");
   10263   1.1  christos   uiout->field_string ("exp", exp_string.get ());
   10264  1.10  christos }
   10265   1.1  christos 
   10266   1.1  christos /* Implement the "print_recreate" method for masked hardware
   10267  1.10  christos    watchpoints.  */
   10268   1.1  christos 
   10269   1.1  christos void
   10270  1.10  christos masked_watchpoint::print_recreate (struct ui_file *fp) const
   10271   1.1  christos {
   10272   1.1  christos   switch (type)
   10273  1.10  christos     {
   10274   1.1  christos     case bp_hardware_watchpoint:
   10275   1.1  christos       gdb_printf (fp, "watch");
   10276  1.10  christos       break;
   10277   1.1  christos     case bp_read_watchpoint:
   10278   1.1  christos       gdb_printf (fp, "rwatch");
   10279  1.10  christos       break;
   10280  1.10  christos     case bp_access_watchpoint:
   10281  1.10  christos       gdb_printf (fp, "awatch");
   10282   1.1  christos       break;
   10283   1.1  christos     default:
   10284   1.1  christos       internal_error (_("Invalid hardware watchpoint type."));
   10285   1.1  christos     }
   10286   1.9  christos 
   10287   1.1  christos   gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
   10288   1.1  christos 	      phex (hw_wp_mask, sizeof (CORE_ADDR)));
   10289  1.10  christos   print_recreate_thread (fp);
   10290   1.1  christos }
   10291   1.1  christos 
   10292   1.1  christos /* Tell whether the given watchpoint is a masked hardware watchpoint.  */
   10293  1.10  christos 
   10294   1.1  christos static bool
   10295   1.1  christos is_masked_watchpoint (const struct breakpoint *b)
   10296   1.1  christos {
   10297  1.10  christos   return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
   10298   1.1  christos }
   10299   1.8  christos 
   10300   1.1  christos /* accessflag:  hw_write:  watch write,
   10301   1.8  christos 		hw_read:   watch read,
   10302   1.3  christos 		hw_access: watch access (read or write) */
   10303   1.1  christos static void
   10304   1.1  christos watch_command_1 (const char *arg, int accessflag, int from_tty,
   10305   1.1  christos 		 bool just_location, bool internal)
   10306   1.1  christos {
   10307   1.1  christos   struct breakpoint *scope_breakpoint = NULL;
   10308   1.1  christos   const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
   10309   1.1  christos   struct value *result;
   10310   1.1  christos   int saved_bitpos = 0, saved_bitsize = 0;
   10311  1.11  christos   const char *exp_start = NULL;
   10312   1.1  christos   const char *exp_end = NULL;
   10313   1.1  christos   const char *tok, *end_tok;
   10314  1.10  christos   int toklen = -1;
   10315   1.1  christos   const char *cond_start = NULL;
   10316  1.11  christos   const char *cond_end = NULL;
   10317   1.1  christos   enum bptype bp_type;
   10318   1.1  christos   int thread = -1;
   10319   1.1  christos   int inferior = -1;
   10320   1.1  christos   /* Flag to indicate whether we are going to use masks for
   10321   1.1  christos      the hardware watchpoint.  */
   10322   1.1  christos   bool use_mask = false;
   10323   1.1  christos   CORE_ADDR mask = 0;
   10324   1.1  christos   int task = -1;
   10325   1.1  christos 
   10326   1.1  christos   /* Make sure that we actually have parameters to parse.  */
   10327   1.1  christos   if (arg != NULL && arg[0] != '\0')
   10328   1.1  christos     {
   10329   1.1  christos       const char *value_start;
   10330   1.1  christos 
   10331   1.1  christos       exp_end = arg + strlen (arg);
   10332   1.1  christos 
   10333   1.1  christos       /* Look for "parameter value" pairs at the end
   10334   1.1  christos 	 of the arguments string.  */
   10335   1.1  christos       for (tok = exp_end - 1; tok > arg; tok--)
   10336   1.1  christos 	{
   10337   1.1  christos 	  /* Skip whitespace at the end of the argument list.  */
   10338   1.1  christos 	  while (tok > arg && (*tok == ' ' || *tok == '\t'))
   10339   1.1  christos 	    tok--;
   10340   1.1  christos 
   10341   1.1  christos 	  /* Find the beginning of the last token.
   10342   1.1  christos 	     This is the value of the parameter.  */
   10343   1.1  christos 	  while (tok > arg && (*tok != ' ' && *tok != '\t'))
   10344   1.1  christos 	    tok--;
   10345   1.1  christos 	  value_start = tok + 1;
   10346   1.1  christos 
   10347   1.1  christos 	  /* Skip whitespace.  */
   10348   1.1  christos 	  while (tok > arg && (*tok == ' ' || *tok == '\t'))
   10349   1.1  christos 	    tok--;
   10350   1.1  christos 
   10351   1.1  christos 	  end_tok = tok;
   10352   1.5  christos 
   10353   1.1  christos 	  /* Find the beginning of the second to last token.
   10354   1.6  christos 	     This is the parameter itself.  */
   10355   1.1  christos 	  while (tok > arg && (*tok != ' ' && *tok != '\t'))
   10356   1.1  christos 	    tok--;
   10357   1.1  christos 	  tok++;
   10358   1.6  christos 	  toklen = end_tok - tok + 1;
   10359   1.1  christos 
   10360   1.1  christos 	  if (toklen == 6 && startswith (tok, "thread"))
   10361   1.1  christos 	    {
   10362   1.1  christos 	      struct thread_info *thr;
   10363  1.11  christos 	      /* At this point we've found a "thread" token, which means
   10364  1.11  christos 		 the user is trying to set a watchpoint that triggers
   10365  1.11  christos 		 only in a specific thread.  */
   10366  1.11  christos 	      const char *endp;
   10367  1.11  christos 
   10368  1.11  christos 	      if (thread != -1)
   10369   1.1  christos 		error(_("You can specify only one thread."));
   10370   1.6  christos 
   10371  1.11  christos 	      if (task != -1)
   10372  1.11  christos 		error (_("You can specify only one of thread or task."));
   10373   1.1  christos 
   10374   1.6  christos 	      if (inferior != -1)
   10375   1.1  christos 		error (_("You can specify only one of inferior or thread."));
   10376  1.10  christos 
   10377  1.10  christos 	      /* Extract the thread ID from the next token.  */
   10378  1.10  christos 	      thr = parse_thread_id (value_start, &endp);
   10379  1.10  christos 	      if (value_start == endp)
   10380  1.11  christos 		error (_("Junk after thread keyword."));
   10381  1.11  christos 
   10382  1.11  christos 	      thread = thr->global_num;
   10383  1.11  christos 	    }
   10384  1.11  christos 	  else if (toklen == 4 && startswith (tok, "task"))
   10385  1.11  christos 	    {
   10386  1.11  christos 	      char *tmp;
   10387  1.11  christos 
   10388  1.11  christos 	      if (task != -1)
   10389  1.10  christos 		error(_("You can specify only one task."));
   10390  1.10  christos 
   10391  1.10  christos 	      if (thread != -1)
   10392  1.10  christos 		error (_("You can specify only one of thread or task."));
   10393  1.10  christos 
   10394  1.10  christos 	      if (inferior != -1)
   10395  1.11  christos 		error (_("You can specify only one of inferior or task."));
   10396  1.11  christos 
   10397  1.11  christos 	      task = strtol (value_start, &tmp, 0);
   10398  1.11  christos 	      if (tmp == value_start)
   10399  1.11  christos 		error (_("Junk after task keyword."));
   10400   1.5  christos 	      if (!valid_task_id (task))
   10401   1.1  christos 		error (_("Unknown task %d."), task);
   10402   1.1  christos 	    }
   10403   1.1  christos 	  else if (toklen == 8 && startswith (tok, "inferior"))
   10404   1.1  christos 	    {
   10405  1.10  christos 	      /* Support for watchpoints will be added in a later commit.  */
   10406   1.1  christos 	      error (_("Cannot use 'inferior' keyword with watchpoints"));
   10407   1.1  christos 	    }
   10408   1.1  christos 	  else if (toklen == 4 && startswith (tok, "mask"))
   10409   1.1  christos 	    {
   10410  1.10  christos 	      /* We've found a "mask" token, which means the user wants to
   10411   1.1  christos 		 create a hardware watchpoint that is going to have the mask
   10412  1.10  christos 		 facility.  */
   10413   1.1  christos 	      struct value *mask_value;
   10414   1.1  christos 
   10415   1.1  christos 	      if (use_mask)
   10416   1.1  christos 		error(_("You can specify only one mask."));
   10417   1.1  christos 
   10418   1.1  christos 	      use_mask = just_location = true;
   10419   1.1  christos 
   10420   1.1  christos 	      scoped_value_mark mark;
   10421   1.1  christos 	      mask_value = parse_to_comma_and_eval (&value_start);
   10422   1.1  christos 	      mask = value_as_address (mask_value);
   10423   1.1  christos 	    }
   10424   1.1  christos 	  else
   10425   1.1  christos 	    /* We didn't recognize what we found.  We should stop here.  */
   10426   1.1  christos 	    break;
   10427   1.1  christos 
   10428   1.1  christos 	  /* Truncate the string and get rid of the "parameter value" pair before
   10429   1.1  christos 	     the arguments string is parsed by the parse_exp_1 function.  */
   10430   1.1  christos 	  exp_end = tok;
   10431   1.8  christos 	}
   10432   1.8  christos     }
   10433   1.9  christos   else
   10434   1.9  christos     exp_end = arg;
   10435   1.1  christos 
   10436   1.1  christos   /* Parse the rest of the arguments.  From here on out, everything
   10437   1.1  christos      is in terms of a newly allocated string instead of the original
   10438   1.1  christos      ARG.  */
   10439   1.1  christos   std::string expression (arg, exp_end - arg);
   10440   1.1  christos   exp_start = arg = expression.c_str ();
   10441   1.1  christos   innermost_block_tracker tracker;
   10442   1.1  christos   expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
   10443   1.7  christos   exp_end = arg;
   10444   1.1  christos   /* Remove trailing whitespace from the expression before saving it.
   10445   1.1  christos      This makes the eventual display of the expression string a bit
   10446   1.1  christos      prettier.  */
   10447   1.1  christos   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
   10448  1.11  christos     --exp_end;
   10449   1.1  christos 
   10450   1.1  christos   /* Checking if the expression is not constant.  */
   10451   1.1  christos   if (watchpoint_exp_is_const (exp.get ()))
   10452   1.1  christos     {
   10453   1.9  christos       int len;
   10454   1.8  christos 
   10455   1.8  christos       len = exp_end - exp_start;
   10456  1.10  christos       while (len > 0 && isspace ((unsigned char)exp_start[len - 1]))
   10457   1.8  christos 	len--;
   10458   1.1  christos       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
   10459   1.8  christos     }
   10460   1.3  christos 
   10461  1.11  christos   exp_valid_block = tracker.block ();
   10462  1.11  christos   struct value *mark = value_mark ();
   10463   1.3  christos   struct value *val_as_value = nullptr;
   10464   1.3  christos   fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
   10465   1.8  christos 		      just_location);
   10466   1.1  christos 
   10467   1.1  christos   if (val_as_value != NULL && just_location)
   10468   1.1  christos     {
   10469   1.1  christos       saved_bitpos = val_as_value->bitpos ();
   10470   1.1  christos       saved_bitsize = val_as_value->bitsize ();
   10471   1.8  christos     }
   10472   1.1  christos 
   10473   1.1  christos   value_ref_ptr val;
   10474   1.1  christos   if (just_location)
   10475   1.1  christos     {
   10476   1.8  christos       int ret;
   10477   1.1  christos 
   10478   1.1  christos       exp_valid_block = NULL;
   10479   1.1  christos       val = release_value (value_addr (result));
   10480   1.1  christos       value_free_to_mark (mark);
   10481   1.1  christos 
   10482   1.1  christos       if (use_mask)
   10483   1.1  christos 	{
   10484   1.8  christos 	  ret = target_masked_watch_num_registers (value_as_address (val.get ()),
   10485   1.8  christos 						   mask);
   10486   1.1  christos 	  if (ret == -1)
   10487   1.8  christos 	    error (_("This target does not support masked watchpoints."));
   10488   1.8  christos 	  else if (ret == -2)
   10489   1.1  christos 	    error (_("Invalid mask or memory region."));
   10490   1.1  christos 	}
   10491   1.1  christos     }
   10492   1.1  christos   else if (val_as_value != NULL)
   10493   1.1  christos     val = release_value (val_as_value);
   10494   1.9  christos 
   10495   1.9  christos   tok = skip_spaces (arg);
   10496   1.1  christos   end_tok = skip_to_space (tok);
   10497   1.1  christos 
   10498   1.1  christos   toklen = end_tok - tok;
   10499   1.9  christos   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
   10500   1.1  christos     {
   10501   1.1  christos       tok = cond_start = end_tok + 1;
   10502   1.1  christos       innermost_block_tracker if_tracker;
   10503   1.1  christos       parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
   10504   1.1  christos 
   10505   1.1  christos       /* The watchpoint expression may not be local, but the condition
   10506  1.10  christos 	 may still be.  E.g.: `watch global if local > 0'.  */
   10507   1.7  christos       cond_exp_valid_block = if_tracker.block ();
   10508   1.7  christos 
   10509   1.7  christos       cond_end = tok;
   10510   1.7  christos     }
   10511   1.1  christos   if (*tok)
   10512   1.1  christos     error (_("Junk at end of command."));
   10513   1.1  christos 
   10514   1.1  christos   frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
   10515   1.1  christos 
   10516   1.7  christos   /* Save this because create_internal_breakpoint below invalidates
   10517   1.1  christos      'wp_frame'.  */
   10518   1.7  christos   frame_id watchpoint_frame = get_frame_id (wp_frame);
   10519   1.7  christos 
   10520   1.7  christos   /* If the expression is "local", then set up a "watchpoint scope"
   10521   1.1  christos      breakpoint at the point where we've left the scope of the watchpoint
   10522   1.7  christos      expression.  Create the scope breakpoint before the watchpoint, so
   10523   1.7  christos      that we will encounter it first in bpstat_stop_status.  */
   10524   1.7  christos   if (exp_valid_block != NULL && wp_frame != NULL)
   10525  1.10  christos     {
   10526   1.7  christos       frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
   10527  1.10  christos 
   10528   1.1  christos       if (frame_id_p (caller_frame_id))
   10529   1.7  christos 	{
   10530   1.7  christos 	  gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
   10531   1.7  christos 	  CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
   10532   1.1  christos 
   10533   1.1  christos 	  scope_breakpoint
   10534   1.1  christos 	    = create_internal_breakpoint (caller_arch, caller_pc,
   10535   1.1  christos 					  bp_watchpoint_scope);
   10536   1.1  christos 
   10537   1.1  christos 	  /* create_internal_breakpoint could invalidate WP_FRAME.  */
   10538   1.7  christos 	  wp_frame = NULL;
   10539   1.1  christos 
   10540   1.1  christos 	  scope_breakpoint->enable_state = bp_enabled;
   10541  1.11  christos 
   10542  1.11  christos 	  /* Automatically delete the breakpoint when it hits.  */
   10543  1.11  christos 	  scope_breakpoint->disposition = disp_del;
   10544  1.11  christos 
   10545  1.11  christos 	  /* Only break in the proper frame (help with recursion).  */
   10546  1.10  christos 	  scope_breakpoint->frame_id = caller_frame_id;
   10547  1.10  christos 
   10548   1.1  christos 	  /* Set the address at which we will stop.  */
   10549   1.1  christos 	  bp_location &loc = scope_breakpoint->first_loc ();
   10550   1.1  christos 	  loc.gdbarch = caller_arch;
   10551   1.1  christos 	  loc.requested_address = caller_pc;
   10552   1.1  christos 	  loc.address
   10553   1.1  christos 	    = adjust_breakpoint_address (loc.gdbarch, loc.requested_address,
   10554   1.1  christos 					 scope_breakpoint->type,
   10555   1.1  christos 					 current_program_space);
   10556   1.1  christos 	}
   10557   1.1  christos     }
   10558   1.1  christos 
   10559   1.1  christos   /* Now set up the breakpoint.  We create all watchpoints as hardware
   10560   1.1  christos      watchpoints here even if hardware watchpoints are turned off, a call
   10561   1.1  christos      to update_watchpoint later in this function will cause the type to
   10562   1.1  christos      drop back to bp_watchpoint (software watchpoint) if required.  */
   10563  1.10  christos 
   10564   1.1  christos   if (accessflag == hw_read)
   10565  1.10  christos     bp_type = bp_read_watchpoint;
   10566   1.1  christos   else if (accessflag == hw_access)
   10567  1.10  christos     bp_type = bp_access_watchpoint;
   10568  1.10  christos   else
   10569  1.11  christos     bp_type = bp_hardware_watchpoint;
   10570  1.11  christos 
   10571   1.8  christos   std::unique_ptr<watchpoint> w;
   10572  1.11  christos   if (use_mask)
   10573  1.10  christos     w.reset (new masked_watchpoint (nullptr, bp_type));
   10574   1.8  christos   else
   10575   1.8  christos     w.reset (new watchpoint (nullptr, bp_type));
   10576   1.7  christos 
   10577   1.1  christos   /* At most one of thread or task can be set on a watchpoint.  */
   10578   1.1  christos   gdb_assert (thread == -1 || task == -1);
   10579   1.1  christos   w->thread = thread;
   10580   1.1  christos   w->inferior = inferior;
   10581  1.11  christos   w->task = task;
   10582   1.8  christos   w->disposition = disp_donttouch;
   10583   1.1  christos   w->pspace = current_program_space;
   10584   1.8  christos   w->exp = std::move (exp);
   10585  1.10  christos   w->exp_valid_block = exp_valid_block;
   10586   1.1  christos   w->cond_exp_valid_block = cond_exp_valid_block;
   10587   1.1  christos   if (just_location)
   10588   1.1  christos     {
   10589   1.1  christos       struct type *t = val->type ();
   10590   1.1  christos       CORE_ADDR addr = value_as_address (val.get ());
   10591  1.10  christos 
   10592   1.1  christos       w->exp_string_reparse
   10593   1.1  christos 	= current_language->watch_location_expression (t, addr);
   10594   1.1  christos 
   10595   1.1  christos       w->exp_string = xstrprintf ("-location %.*s",
   10596   1.1  christos 				  (int) (exp_end - exp_start), exp_start);
   10597   1.1  christos     }
   10598   1.1  christos   else
   10599   1.1  christos     w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
   10600   1.3  christos 
   10601   1.3  christos   if (use_mask)
   10602   1.9  christos     {
   10603   1.1  christos       w->hw_wp_mask = mask;
   10604   1.1  christos     }
   10605   1.1  christos   else
   10606  1.10  christos     {
   10607   1.1  christos       w->val = val;
   10608   1.8  christos       w->val_bitpos = saved_bitpos;
   10609   1.1  christos       w->val_bitsize = saved_bitsize;
   10610   1.7  christos       w->val_valid = true;
   10611   1.1  christos     }
   10612   1.7  christos 
   10613   1.1  christos   if (cond_start)
   10614   1.1  christos     w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
   10615   1.1  christos   else
   10616   1.1  christos     w->cond_string = 0;
   10617   1.1  christos 
   10618   1.1  christos   if (frame_id_p (watchpoint_frame))
   10619   1.1  christos     {
   10620   1.1  christos       w->watchpoint_frame = watchpoint_frame;
   10621   1.1  christos       w->watchpoint_thread = inferior_ptid;
   10622   1.1  christos     }
   10623   1.1  christos   else
   10624   1.1  christos     {
   10625   1.8  christos       w->watchpoint_frame = null_frame_id;
   10626   1.8  christos       w->watchpoint_thread = null_ptid;
   10627   1.1  christos     }
   10628   1.1  christos 
   10629   1.1  christos   if (scope_breakpoint != NULL)
   10630   1.1  christos     {
   10631   1.1  christos       /* The scope breakpoint is related to the watchpoint.  We will
   10632   1.8  christos 	 need to act on them together.  */
   10633   1.8  christos       w->related_breakpoint = scope_breakpoint;
   10634  1.10  christos       scope_breakpoint->related_breakpoint = w.get ();
   10635   1.1  christos     }
   10636   1.8  christos 
   10637   1.1  christos   if (!just_location)
   10638   1.1  christos     value_free_to_mark (mark);
   10639   1.1  christos 
   10640   1.1  christos   /* Finally update the new watchpoint.  This creates the locations
   10641   1.1  christos      that should be inserted.  */
   10642   1.1  christos   update_watchpoint (w.get (), true /* reparse */);
   10643   1.8  christos 
   10644   1.1  christos   install_breakpoint (internal, std::move (w), 1);
   10645   1.1  christos }
   10646   1.1  christos 
   10647   1.1  christos /* Return count of debug registers needed to watch the given expression.
   10648   1.1  christos    If the watchpoint cannot be handled in hardware return zero.  */
   10649   1.1  christos 
   10650   1.1  christos static int
   10651   1.8  christos can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
   10652   1.8  christos {
   10653   1.8  christos   int found_memory_cnt = 0;
   10654   1.1  christos 
   10655   1.1  christos   /* Did the user specifically forbid us to use hardware watchpoints? */
   10656   1.1  christos   if (!can_use_hw_watchpoints)
   10657   1.1  christos     return 0;
   10658   1.1  christos 
   10659   1.1  christos   gdb_assert (!vals.empty ());
   10660   1.1  christos   struct value *head = vals[0].get ();
   10661   1.1  christos 
   10662   1.1  christos   /* Make sure that the value of the expression depends only upon
   10663   1.1  christos      memory contents, and values computed from them within GDB.  If we
   10664   1.1  christos      find any register references or function calls, we can't use a
   10665   1.1  christos      hardware watchpoint.
   10666   1.1  christos 
   10667   1.1  christos      The idea here is that evaluating an expression generates a series
   10668   1.1  christos      of values, one holding the value of every subexpression.  (The
   10669   1.1  christos      expression a*b+c has five subexpressions: a, b, a*b, c, and
   10670   1.1  christos      a*b+c.)  GDB's values hold almost enough information to establish
   10671   1.1  christos      the criteria given above --- they identify memory lvalues,
   10672   1.1  christos      register lvalues, computed values, etcetera.  So we can evaluate
   10673   1.8  christos      the expression, and then scan the chain of values that leaves
   10674   1.1  christos      behind to decide whether we can detect any possible change to the
   10675   1.8  christos      expression's final value using only hardware watchpoints.
   10676   1.8  christos 
   10677  1.11  christos      However, I don't think that the values returned by inferior
   10678   1.1  christos      function calls are special in any way.  So this function may not
   10679  1.11  christos      notice that an expression involving an inferior function call
   10680   1.1  christos      can't be watched with hardware watchpoints.  FIXME.  */
   10681   1.1  christos   for (const value_ref_ptr &iter : vals)
   10682   1.1  christos     {
   10683   1.1  christos       struct value *v = iter.get ();
   10684   1.1  christos 
   10685   1.1  christos       if (v->lval () == lval_memory)
   10686   1.1  christos 	{
   10687   1.1  christos 	  if (v != head && v->lazy ())
   10688   1.1  christos 	    /* A lazy memory lvalue in the chain is one that GDB never
   10689  1.10  christos 	       needed to fetch; we either just used its address (e.g.,
   10690  1.11  christos 	       `a' in `a.b') or we never needed it at all (e.g., `a'
   10691   1.1  christos 	       in `a,b').  This doesn't apply to HEAD; if that is
   10692   1.1  christos 	       lazy then it was not readable, but watch it anyway.  */
   10693   1.1  christos 	    ;
   10694   1.1  christos 	  else
   10695   1.1  christos 	    {
   10696   1.9  christos 	      /* Ahh, memory we actually used!  Check if we can cover
   10697   1.9  christos 		 it with hardware watchpoints.  */
   10698   1.1  christos 	      struct type *vtype = check_typedef (v->type ());
   10699  1.11  christos 
   10700   1.1  christos 	      /* We only watch structs and arrays if user asked for it
   10701   1.1  christos 		 explicitly, never if they just happen to appear in a
   10702   1.1  christos 		 middle of some value chain.  */
   10703   1.1  christos 	      if (v == head
   10704   1.1  christos 		  || (vtype->code () != TYPE_CODE_STRUCT
   10705  1.11  christos 		      && vtype->code () != TYPE_CODE_ARRAY))
   10706   1.1  christos 		{
   10707   1.1  christos 		  CORE_ADDR vaddr = v->address ();
   10708   1.1  christos 		  int len;
   10709   1.1  christos 		  int num_regs;
   10710   1.1  christos 
   10711   1.1  christos 		  len = (target_exact_watchpoints
   10712   1.1  christos 			 && is_scalar_type_recursive (vtype))?
   10713   1.1  christos 		    1 : v->type ()->length ();
   10714   1.1  christos 
   10715  1.11  christos 		  num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
   10716   1.1  christos 		  if (!num_regs)
   10717  1.11  christos 		    return 0;
   10718   1.1  christos 		  else
   10719   1.1  christos 		    found_memory_cnt += num_regs;
   10720   1.1  christos 		}
   10721   1.1  christos 	    }
   10722   1.1  christos 	}
   10723   1.1  christos       else if (v->lval () != not_lval && !v->deprecated_modifiable ())
   10724   1.1  christos 	return 0;	/* These are values from the history (e.g., $1).  */
   10725   1.1  christos       else if (v->lval () == lval_register)
   10726   1.1  christos 	return 0;	/* Cannot watch a register with a HW watchpoint.  */
   10727  1.10  christos     }
   10728   1.1  christos 
   10729   1.1  christos   /* The expression itself looks suitable for using a hardware
   10730   1.1  christos      watchpoint, but give the target machine a chance to reject it.  */
   10731   1.1  christos   return found_memory_cnt;
   10732  1.10  christos }
   10733  1.10  christos 
   10734  1.10  christos void
   10735  1.10  christos watch_command_wrapper (const char *arg, int from_tty, bool internal)
   10736  1.10  christos {
   10737  1.10  christos   watch_command_1 (arg, hw_write, from_tty, 0, internal);
   10738  1.10  christos }
   10739  1.10  christos 
   10740  1.10  christos /* Options for the watch, awatch, and rwatch commands.  */
   10741  1.10  christos 
   10742  1.10  christos struct watch_options
   10743  1.10  christos {
   10744  1.10  christos   /* For -location.  */
   10745  1.10  christos   bool location = false;
   10746  1.10  christos };
   10747  1.10  christos 
   10748  1.10  christos /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
   10749  1.10  christos 
   10750  1.10  christos    Historically GDB always accepted both '-location' and '-l' flags for
   10751  1.10  christos    these commands (both flags being synonyms).  When converting to the
   10752  1.10  christos    newer option scheme only '-location' is added here.  That's fine (for
   10753  1.10  christos    backward compatibility) as any non-ambiguous prefix of a flag will be
   10754  1.10  christos    accepted, so '-l', '-loc', are now all accepted.
   10755  1.10  christos 
   10756  1.10  christos    What this means is that, if in the future, we add any new flag here
   10757  1.10  christos    that starts with '-l' then this will break backward compatibility, so
   10758  1.10  christos    please, don't do that!  */
   10759  1.10  christos 
   10760  1.10  christos static const gdb::option::option_def watch_option_defs[] = {
   10761  1.10  christos   gdb::option::flag_option_def<watch_options> {
   10762  1.10  christos     "location",
   10763  1.10  christos     [] (watch_options *opt) { return &opt->location; },
   10764  1.10  christos     N_("\
   10765  1.10  christos This evaluates EXPRESSION and watches the memory to which is refers.\n\
   10766  1.10  christos -l can be used as a short form of -location."),
   10767  1.10  christos   },
   10768  1.10  christos };
   10769  1.10  christos 
   10770  1.10  christos /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
   10771   1.1  christos    commands.  */
   10772   1.1  christos 
   10773   1.1  christos static gdb::option::option_def_group
   10774   1.1  christos make_watch_options_def_group (watch_options *opts)
   10775   1.8  christos {
   10776   1.1  christos   return {{watch_option_defs}, opts};
   10777  1.10  christos }
   10778  1.10  christos 
   10779  1.10  christos /* A helper function that looks for the "-location" argument and then
   10780  1.10  christos    calls watch_command_1.  */
   10781  1.10  christos 
   10782  1.10  christos static void
   10783   1.1  christos watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
   10784  1.10  christos {
   10785  1.10  christos   watch_options opts;
   10786  1.10  christos   auto grp = make_watch_options_def_group (&opts);
   10787  1.10  christos   gdb::option::process_options
   10788  1.10  christos     (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
   10789  1.10  christos   if (arg != nullptr && *arg == '\0')
   10790  1.10  christos     arg = nullptr;
   10791  1.10  christos 
   10792  1.10  christos   watch_command_1 (arg, accessflag, from_tty, opts.location, false);
   10793  1.10  christos }
   10794  1.10  christos 
   10795  1.10  christos /* Command completion for 'watch', 'awatch', and 'rwatch' commands.   */
   10796  1.10  christos static void
   10797   1.1  christos watch_command_completer (struct cmd_list_element *ignore,
   10798  1.10  christos 			 completion_tracker &tracker,
   10799  1.10  christos 			 const char *text, const char * /*word*/)
   10800   1.1  christos {
   10801   1.1  christos   const auto group = make_watch_options_def_group (nullptr);
   10802   1.1  christos   if (gdb::option::complete_options
   10803   1.8  christos       (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
   10804   1.1  christos     return;
   10805   1.1  christos 
   10806   1.1  christos   const char *word = advance_to_expression_complete_word_point (tracker, text);
   10807   1.1  christos   expression_completer (ignore, tracker, text, word);
   10808   1.1  christos }
   10809  1.10  christos 
   10810   1.1  christos static void
   10811   1.1  christos watch_command (const char *arg, int from_tty)
   10812   1.1  christos {
   10813   1.1  christos   watch_maybe_just_location (arg, hw_write, from_tty);
   10814   1.1  christos }
   10815   1.8  christos 
   10816   1.1  christos void
   10817   1.1  christos rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
   10818   1.1  christos {
   10819   1.1  christos   watch_command_1 (arg, hw_read, from_tty, 0, internal);
   10820   1.1  christos }
   10821  1.10  christos 
   10822   1.1  christos static void
   10823   1.1  christos rwatch_command (const char *arg, int from_tty)
   10824   1.1  christos {
   10825   1.1  christos   watch_maybe_just_location (arg, hw_read, from_tty);
   10826   1.1  christos }
   10827   1.8  christos 
   10828   1.1  christos void
   10829   1.1  christos awatch_command_wrapper (const char *arg, int from_tty, bool internal)
   10830   1.1  christos {
   10831   1.1  christos   watch_command_1 (arg, hw_access, from_tty, 0, internal);
   10832   1.1  christos }
   10833   1.6  christos 
   10834   1.6  christos static void
   10835   1.6  christos awatch_command (const char *arg, int from_tty)
   10836   1.1  christos {
   10837   1.8  christos   watch_maybe_just_location (arg, hw_access, from_tty);
   10838   1.1  christos }
   10839   1.8  christos 
   10840   1.6  christos 
   10842   1.9  christos /* Data for the FSM that manages the until(location)/advance commands
   10843   1.9  christos    in infcmd.c.  Here because it uses the mechanisms of
   10844   1.9  christos    breakpoints.  */
   10845   1.8  christos 
   10846   1.8  christos struct until_break_fsm : public thread_fsm
   10847   1.9  christos {
   10848   1.8  christos   /* The thread that was current when the command was executed.  */
   10849   1.8  christos   int thread;
   10850   1.9  christos 
   10851   1.8  christos   /* The breakpoint set at the return address in the caller frame,
   10852   1.8  christos      plus breakpoints at all the destination locations.  */
   10853   1.6  christos   std::vector<breakpoint_up> breakpoints;
   10854   1.8  christos 
   10855   1.8  christos   until_break_fsm (struct interp *cmd_interp, int thread,
   10856   1.8  christos 		   std::vector<breakpoint_up> &&breakpoints)
   10857   1.1  christos     : thread_fsm (cmd_interp),
   10858   1.1  christos       thread (thread),
   10859   1.6  christos       breakpoints (std::move (breakpoints))
   10860   1.6  christos   {
   10861   1.6  christos   }
   10862   1.8  christos 
   10863   1.8  christos   void clean_up (struct thread_info *thread) override;
   10864   1.6  christos   bool should_stop (struct thread_info *thread) override;
   10865   1.9  christos   enum async_reply_reason do_async_reply_reason () override;
   10866   1.9  christos };
   10867   1.9  christos 
   10868   1.9  christos /* Implementation of the 'should_stop' FSM method for the
   10869   1.9  christos    until(location)/advance commands.  */
   10870   1.9  christos 
   10871   1.9  christos bool
   10872   1.6  christos until_break_fsm::should_stop (struct thread_info *tp)
   10873   1.8  christos {
   10874   1.6  christos   for (const breakpoint_up &bp : breakpoints)
   10875   1.6  christos     if (bpstat_find_breakpoint (tp->control.stop_bpstat,
   10876   1.6  christos 				bp.get ()) != NULL)
   10877   1.6  christos       {
   10878   1.6  christos 	set_finished ();
   10879   1.8  christos 	break;
   10880   1.8  christos       }
   10881   1.1  christos 
   10882   1.6  christos   return true;
   10883   1.9  christos }
   10884   1.8  christos 
   10885   1.6  christos /* Implementation of the 'clean_up' FSM method for the
   10886   1.6  christos    until(location)/advance commands.  */
   10887   1.6  christos 
   10888   1.6  christos void
   10889   1.1  christos until_break_fsm::clean_up (struct thread_info *)
   10890   1.8  christos {
   10891   1.8  christos   /* Clean up our temporary breakpoints.  */
   10892   1.6  christos   breakpoints.clear ();
   10893   1.6  christos   delete_longjmp_breakpoint (thread);
   10894   1.1  christos }
   10895   1.1  christos 
   10896   1.1  christos /* Implementation of the 'async_reply_reason' FSM method for the
   10897   1.8  christos    until(location)/advance commands.  */
   10898   1.1  christos 
   10899  1.10  christos enum async_reply_reason
   10900   1.1  christos until_break_fsm::do_async_reply_reason ()
   10901   1.1  christos {
   10902   1.1  christos   return EXEC_ASYNC_LOCATION_REACHED;
   10903   1.1  christos }
   10904   1.1  christos 
   10905   1.1  christos void
   10906   1.3  christos until_break_command (const char *arg, int from_tty, int anywhere)
   10907   1.1  christos {
   10908   1.1  christos   frame_info_ptr frame;
   10909   1.1  christos   struct gdbarch *frame_gdbarch;
   10910   1.1  christos   struct frame_id stack_frame_id;
   10911  1.10  christos   struct frame_id caller_frame_id;
   10912   1.6  christos   int thread;
   10913   1.8  christos   struct thread_info *tp;
   10914   1.8  christos 
   10915  1.10  christos   clear_proceed_status (0);
   10916   1.8  christos 
   10917   1.8  christos   /* Set a breakpoint where the user wants it and at return from
   10918  1.10  christos      this function.  */
   10919   1.9  christos 
   10920   1.1  christos   location_spec_up locspec = string_to_location_spec (&arg, current_language);
   10921   1.9  christos 
   10922   1.1  christos   std::vector<symtab_and_line> sals
   10923   1.1  christos     = (last_displayed_sal_is_valid ()
   10924   1.1  christos        ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
   10925   1.1  christos 			get_last_displayed_symtab (),
   10926   1.1  christos 			get_last_displayed_line ())
   10927   1.1  christos        : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
   10928   1.6  christos 			NULL, NULL, 0));
   10929   1.1  christos 
   10930   1.1  christos   if (sals.empty ())
   10931   1.1  christos     error (_("Couldn't get information on specified line."));
   10932   1.1  christos 
   10933   1.1  christos   if (*arg)
   10934   1.1  christos     error (_("Junk at end of arguments."));
   10935   1.1  christos 
   10936   1.1  christos   tp = inferior_thread ();
   10937   1.1  christos   thread = tp->global_num;
   10938   1.1  christos 
   10939   1.1  christos   /* Note linespec handling above invalidates the frame chain.
   10940   1.1  christos      Installing a breakpoint also invalidates the frame chain (as it
   10941   1.1  christos      may need to switch threads), so do any frame handling before
   10942   1.1  christos      that.  */
   10943   1.9  christos 
   10944   1.8  christos   frame = get_selected_frame (NULL);
   10945  1.11  christos   frame_gdbarch = get_frame_arch (frame);
   10946   1.8  christos   stack_frame_id = get_stack_frame_id (frame);
   10947   1.1  christos   caller_frame_id = frame_unwind_caller_id (frame);
   10948   1.1  christos 
   10949   1.1  christos   /* Keep within the current frame, or in frames called by the current
   10950   1.6  christos      one.  */
   10951   1.1  christos 
   10952   1.1  christos   std::vector<breakpoint_up> breakpoints;
   10953   1.1  christos 
   10954   1.6  christos   std::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
   10955   1.9  christos 
   10956   1.9  christos   if (frame_id_p (caller_frame_id))
   10957   1.9  christos     {
   10958   1.9  christos       struct symtab_and_line sal2;
   10959   1.9  christos       struct gdbarch *caller_gdbarch;
   10960   1.1  christos 
   10961  1.10  christos       sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
   10962   1.8  christos       sal2.pc = frame_unwind_caller_pc (frame);
   10963   1.1  christos       caller_gdbarch = frame_unwind_caller_arch (frame);
   10964   1.1  christos 
   10965   1.9  christos       breakpoint_up caller_breakpoint
   10966   1.9  christos 	= set_momentary_breakpoint (caller_gdbarch, sal2,
   10967   1.9  christos 				    caller_frame_id, bp_until);
   10968   1.9  christos       breakpoints.emplace_back (std::move (caller_breakpoint));
   10969   1.9  christos 
   10970   1.9  christos       set_longjmp_breakpoint (tp, stack_frame_id);
   10971   1.9  christos       lj_deleter.emplace (thread);
   10972   1.9  christos     }
   10973   1.9  christos 
   10974   1.9  christos   /* If the user told us to continue until a specified location, we
   10975  1.10  christos      don't specify a frame at which we need to stop.  Otherwise,
   10976  1.10  christos      specify the selected frame, because we want to stop only at the
   10977  1.10  christos      very same frame.  */
   10978  1.10  christos   frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
   10979  1.10  christos 
   10980   1.1  christos   for (symtab_and_line &sal : sals)
   10981  1.10  christos     {
   10982  1.10  christos       resolve_sal_pc (&sal);
   10983  1.10  christos 
   10984  1.10  christos       breakpoint_up location_breakpoint
   10985   1.1  christos 	= set_momentary_breakpoint (frame_gdbarch, sal,
   10986  1.10  christos 				    stop_frame_id, bp_until);
   10987  1.10  christos       breakpoints.emplace_back (std::move (location_breakpoint));
   10988   1.1  christos     }
   10989  1.10  christos 
   10990   1.1  christos   tp->set_thread_fsm
   10991   1.1  christos     (std::unique_ptr<thread_fsm>
   10992   1.1  christos      (new until_break_fsm (command_interp (), tp->global_num,
   10993   1.1  christos 			   std::move (breakpoints))));
   10994   1.8  christos 
   10995   1.1  christos   if (lj_deleter)
   10996   1.1  christos     lj_deleter->release ();
   10997   1.8  christos 
   10998   1.1  christos   proceed (-1, GDB_SIGNAL_DEFAULT);
   10999   1.8  christos }
   11000   1.8  christos 
   11001   1.1  christos 
   11002   1.8  christos 
   11004   1.8  christos /* Compare two breakpoints and return a strcmp-like result.  */
   11005   1.1  christos 
   11006   1.1  christos static int
   11007   1.1  christos compare_breakpoints (const breakpoint *a, const breakpoint *b)
   11008   1.1  christos {
   11009   1.1  christos   uintptr_t ua = (uintptr_t) a;
   11010   1.1  christos   uintptr_t ub = (uintptr_t) b;
   11011   1.1  christos 
   11012   1.1  christos   if (a->number < b->number)
   11013   1.1  christos     return -1;
   11014   1.1  christos   else if (a->number > b->number)
   11015   1.1  christos     return 1;
   11016   1.1  christos 
   11017   1.8  christos   /* Now sort by address, in case we see, e..g, two breakpoints with
   11018   1.1  christos      the number 0.  */
   11019   1.1  christos   if (ua < ub)
   11020   1.1  christos     return -1;
   11021   1.8  christos   return ua > ub ? 1 : 0;
   11022   1.8  christos }
   11023   1.8  christos 
   11024   1.1  christos /* Delete breakpoints by address or line.  */
   11025   1.1  christos 
   11026   1.8  christos static void
   11027   1.8  christos clear_command (const char *arg, int from_tty)
   11028   1.8  christos {
   11029   1.8  christos   int default_match;
   11030   1.1  christos 
   11031   1.8  christos   std::vector<symtab_and_line> decoded_sals;
   11032   1.1  christos   symtab_and_line last_sal;
   11033   1.1  christos   gdb::array_view<symtab_and_line> sals;
   11034   1.1  christos   if (arg)
   11035   1.1  christos     {
   11036   1.1  christos       decoded_sals
   11037   1.1  christos 	= decode_line_with_current_source (arg,
   11038   1.8  christos 					   (DECODE_LINE_FUNFIRSTLINE
   11039   1.8  christos 					    | DECODE_LINE_LIST_MODE));
   11040   1.1  christos       default_match = 0;
   11041   1.1  christos       sals = decoded_sals;
   11042   1.1  christos     }
   11043   1.8  christos   else
   11044   1.1  christos     {
   11045   1.1  christos       /* Set sal's line, symtab, pc, and pspace to the values
   11046   1.1  christos 	 corresponding to the last call to print_frame_info.  If the
   11047   1.1  christos 	 codepoint is not valid, this will set all the fields to 0.  */
   11048   1.1  christos       last_sal = get_last_displayed_sal ();
   11049   1.1  christos       if (last_sal.symtab == 0)
   11050   1.1  christos 	error (_("No source file specified."));
   11051   1.1  christos 
   11052   1.1  christos       default_match = 1;
   11053   1.1  christos       sals = last_sal;
   11054   1.1  christos     }
   11055   1.1  christos 
   11056   1.1  christos   /* We don't call resolve_sal_pc here.  That's not as bad as it
   11057   1.1  christos      seems, because all existing breakpoints typically have both
   11058   1.1  christos      file/line and pc set.  So, if clear is given file/line, we can
   11059   1.1  christos      match this to existing breakpoint without obtaining pc at all.
   11060   1.1  christos 
   11061   1.1  christos      We only support clearing given the address explicitly
   11062   1.1  christos      present in breakpoint table.  Say, we've set breakpoint
   11063   1.1  christos      at file:line.  There were several PC values for that file:line,
   11064   1.1  christos      due to optimization, all in one block.
   11065   1.1  christos 
   11066   1.1  christos      We've picked one PC value.  If "clear" is issued with another
   11067   1.1  christos      PC corresponding to the same file:line, the breakpoint won't
   11068   1.8  christos      be cleared.  We probably can still clear the breakpoint, but
   11069   1.8  christos      since the other PC value is never presented to user, user
   11070   1.1  christos      can only find it by guessing, and it does not seem important
   11071   1.1  christos      to support that.  */
   11072   1.1  christos 
   11073   1.1  christos   /* For each line spec given, delete bps which correspond to it.  Do
   11074  1.10  christos      it in two passes, solely to preserve the current behavior that
   11075  1.10  christos      from_tty is forced true if we delete more than one
   11076  1.10  christos      breakpoint.  */
   11077  1.10  christos 
   11078  1.10  christos   std::vector<struct breakpoint *> found;
   11079  1.10  christos   for (const auto &sal : sals)
   11080  1.10  christos     {
   11081  1.10  christos       const char *sal_fullname;
   11082  1.10  christos 
   11083  1.10  christos       /* If exact pc given, clear bpts at that pc.
   11084   1.1  christos 	 If line given (pc == 0), clear all bpts on specified line.
   11085   1.1  christos 	 If defaulting, clear all bpts on default line
   11086   1.1  christos 	 or at default pc.
   11087   1.1  christos 
   11088   1.1  christos 	 defaulting    sal.pc != 0    tests to do
   11089  1.11  christos 
   11090   1.1  christos 	 0              1             pc
   11091   1.1  christos 	 1              1             pc _and_ line
   11092   1.1  christos 	 0              0             line
   11093  1.11  christos 	 1              0             <can't happen> */
   11094  1.11  christos 
   11095   1.1  christos       sal_fullname = (sal.symtab == NULL
   11096  1.11  christos 		      ? NULL : symtab_to_fullname (sal.symtab));
   11097   1.1  christos 
   11098   1.1  christos       /* Find all matching breakpoints and add them to 'found'.  */
   11099   1.1  christos       for (breakpoint &b : all_breakpoints ())
   11100   1.1  christos 	{
   11101   1.1  christos 	  int match = 0;
   11102  1.11  christos 	  /* Are we going to delete b?  */
   11103  1.11  christos 	  if (b.type != bp_none && !is_watchpoint (&b)
   11104  1.11  christos 	      && user_breakpoint_p (&b))
   11105  1.11  christos 	    {
   11106   1.1  christos 	      for (bp_location &loc : b.locations ())
   11107   1.1  christos 		{
   11108   1.1  christos 		  /* If the user specified file:line, don't allow a PC
   11109  1.11  christos 		     match.  This matches historical gdb behavior.  */
   11110   1.1  christos 		  int pc_match = (!sal.explicit_line
   11111  1.11  christos 				  && sal.pc
   11112  1.11  christos 				  && (loc.pspace == sal.pspace)
   11113  1.11  christos 				  && (loc.address == sal.pc)
   11114   1.1  christos 				  && (!section_is_overlay (loc.section)
   11115   1.1  christos 				      || loc.section == sal.section));
   11116   1.1  christos 		  int line_match = 0;
   11117   1.1  christos 
   11118   1.1  christos 		  if ((default_match || sal.explicit_line)
   11119   1.1  christos 		      && loc.symtab != NULL
   11120   1.1  christos 		      && sal_fullname != NULL
   11121   1.1  christos 		      && sal.pspace == loc.pspace
   11122   1.1  christos 		      && loc.line_number == sal.line
   11123   1.1  christos 		      && filename_cmp (symtab_to_fullname (loc.symtab),
   11124   1.1  christos 				       sal_fullname) == 0)
   11125   1.1  christos 		    line_match = 1;
   11126  1.11  christos 
   11127   1.1  christos 		  if (pc_match || line_match)
   11128   1.1  christos 		    {
   11129   1.1  christos 		      match = 1;
   11130   1.1  christos 		      break;
   11131   1.8  christos 		    }
   11132   1.1  christos 		}
   11133   1.1  christos 	    }
   11134   1.1  christos 
   11135   1.1  christos 	  if (match)
   11136   1.1  christos 	    found.push_back (&b);
   11137   1.1  christos 	}
   11138   1.1  christos     }
   11139   1.1  christos 
   11140   1.8  christos   /* Now go thru the 'found' chain and delete them.  */
   11141   1.8  christos   if (found.empty ())
   11142   1.8  christos     {
   11143   1.8  christos       if (arg)
   11144   1.8  christos 	error (_("No breakpoint at %s."), arg);
   11145   1.8  christos       else
   11146   1.8  christos 	error (_("No breakpoint at this line."));
   11147   1.8  christos     }
   11148   1.8  christos 
   11149   1.8  christos   /* Remove duplicates from the vec.  */
   11150   1.8  christos   std::sort (found.begin (), found.end (),
   11151   1.1  christos 	     [] (const breakpoint *bp_a, const breakpoint *bp_b)
   11152   1.8  christos 	     {
   11153   1.1  christos 	       return compare_breakpoints (bp_a, bp_b) < 0;
   11154   1.1  christos 	     });
   11155   1.1  christos   found.erase (std::unique (found.begin (), found.end (),
   11156   1.8  christos 			    [] (const breakpoint *bp_a, const breakpoint *bp_b)
   11157  1.10  christos 			    {
   11158   1.1  christos 			      return compare_breakpoints (bp_a, bp_b) == 0;
   11159  1.10  christos 			    }),
   11160   1.1  christos 	       found.end ());
   11161   1.1  christos 
   11162   1.8  christos   if (found.size () > 1)
   11163   1.1  christos     from_tty = 1;	/* Always report if deleted more than one.  */
   11164   1.1  christos   if (from_tty)
   11165  1.10  christos     {
   11166   1.8  christos       if (found.size () == 1)
   11167   1.1  christos 	gdb_printf (_("Deleted breakpoint "));
   11168   1.1  christos       else
   11169  1.10  christos 	gdb_printf (_("Deleted breakpoints "));
   11170   1.1  christos     }
   11171   1.1  christos 
   11172   1.1  christos   for (breakpoint *iter : found)
   11173   1.1  christos     {
   11174   1.1  christos       if (from_tty)
   11175   1.1  christos 	gdb_printf ("%d ", iter->number);
   11176   1.1  christos       delete_breakpoint (iter);
   11177  1.10  christos     }
   11178   1.1  christos   if (from_tty)
   11179   1.1  christos     gdb_putc ('\n');
   11180   1.1  christos }
   11181   1.1  christos 
   11182   1.1  christos /* Delete breakpoint in BS if they are `delete' breakpoints and
   11184   1.1  christos    all breakpoints that are marked for deletion, whether hit or not.
   11185  1.11  christos    This is called after any breakpoint is hit, or after errors.  */
   11186  1.11  christos 
   11187  1.11  christos void
   11188   1.1  christos breakpoint_auto_delete (bpstat *bs)
   11189   1.1  christos {
   11190   1.1  christos   for (; bs; bs = bs->next)
   11191   1.9  christos     if (bs->breakpoint_at
   11192   1.9  christos 	&& bs->breakpoint_at->disposition == disp_del
   11193  1.11  christos 	&& bs->stop)
   11194   1.9  christos       delete_breakpoint (bs->breakpoint_at);
   11195   1.1  christos 
   11196   1.1  christos   for (breakpoint &b : all_breakpoints_safe ())
   11197   1.9  christos     if (b.disposition == disp_del_at_next_stop)
   11198   1.1  christos       delete_breakpoint (&b);
   11199   1.1  christos }
   11200   1.9  christos 
   11201   1.1  christos /* A comparison function for bp_location AP and BP being interfaced to
   11202   1.1  christos    std::sort.  Sort elements primarily by their ADDRESS (no matter what
   11203   1.1  christos    bl_address_is_meaningful says), secondarily by ordering first
   11204   1.1  christos    permanent elements and tertiarily just ensuring the array is sorted
   11205   1.1  christos    stable way despite std::sort being an unstable algorithm.  */
   11206   1.1  christos 
   11207   1.9  christos static int
   11208   1.1  christos bp_location_is_less_than (const bp_location *a, const bp_location *b)
   11209   1.1  christos {
   11210   1.3  christos   if (a->address != b->address)
   11211   1.9  christos     return a->address < b->address;
   11212   1.9  christos 
   11213   1.9  christos   /* Sort locations at the same address by their pspace number, keeping
   11214   1.9  christos      locations of the same inferior (in a multi-inferior environment)
   11215   1.9  christos      grouped.  */
   11216   1.9  christos 
   11217   1.9  christos   if (a->pspace->num != b->pspace->num)
   11218   1.9  christos     return a->pspace->num < b->pspace->num;
   11219   1.9  christos 
   11220   1.9  christos   /* Sort permanent breakpoints first.  */
   11221   1.9  christos   if (a->permanent != b->permanent)
   11222   1.9  christos     return a->permanent > b->permanent;
   11223   1.9  christos 
   11224   1.1  christos   /* Sort by type in order to make duplicate determination easier.
   11225   1.1  christos      See update_global_location_list.  This is kept in sync with
   11226   1.1  christos      breakpoint_locations_match.  */
   11227   1.1  christos   if (a->loc_type < b->loc_type)
   11228   1.1  christos     return true;
   11229   1.1  christos 
   11230   1.9  christos   /* Likewise, for range-breakpoints, sort by length.  */
   11231   1.1  christos   if (a->loc_type == bp_loc_hardware_breakpoint
   11232   1.9  christos       && b->loc_type == bp_loc_hardware_breakpoint
   11233   1.1  christos       && a->length < b->length)
   11234   1.1  christos     return true;
   11235   1.7  christos 
   11236   1.7  christos   /* Make the internal GDB representation stable across GDB runs
   11237   1.7  christos      where A and B memory inside GDB can differ.  Breakpoint locations of
   11238   1.1  christos      the same type at the same address can be sorted in arbitrary order.  */
   11239   1.1  christos 
   11240   1.7  christos   if (a->owner->number != b->owner->number)
   11241   1.1  christos     return a->owner->number < b->owner->number;
   11242   1.7  christos 
   11243   1.7  christos   return a < b;
   11244   1.1  christos }
   11245  1.10  christos 
   11246   1.1  christos /* Set bp_locations_placed_address_before_address_max and
   11247   1.1  christos    bp_locations_shadow_len_after_address_max according to the current
   11248   1.1  christos    content of the bp_locations array.  */
   11249   1.1  christos 
   11250   1.1  christos static void
   11251   1.1  christos bp_locations_target_extensions_update (void)
   11252   1.1  christos {
   11253   1.1  christos   bp_locations_placed_address_before_address_max = 0;
   11254   1.1  christos   bp_locations_shadow_len_after_address_max = 0;
   11255   1.1  christos 
   11256   1.1  christos   for (bp_location *bl : all_bp_locations ())
   11257   1.7  christos     {
   11258   1.7  christos       CORE_ADDR start, end, addr;
   11259   1.1  christos 
   11260   1.1  christos       if (!bp_location_has_shadow (bl))
   11261   1.1  christos 	continue;
   11262   1.1  christos 
   11263   1.1  christos       start = bl->target_info.placed_address;
   11264   1.7  christos       end = start + bl->target_info.shadow_len;
   11265   1.7  christos 
   11266   1.1  christos       gdb_assert (bl->address >= start);
   11267   1.1  christos       addr = bl->address - start;
   11268   1.1  christos       if (addr > bp_locations_placed_address_before_address_max)
   11269   1.1  christos 	bp_locations_placed_address_before_address_max = addr;
   11270   1.1  christos 
   11271   1.1  christos       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
   11272   1.1  christos 
   11273   1.1  christos       gdb_assert (bl->address < end);
   11274   1.6  christos       addr = end - bl->address;
   11275   1.1  christos       if (addr > bp_locations_shadow_len_after_address_max)
   11276   1.8  christos 	bp_locations_shadow_len_after_address_max = addr;
   11277   1.1  christos     }
   11278  1.11  christos }
   11279   1.1  christos 
   11280  1.10  christos /* Download tracepoint locations if they haven't been.  */
   11281   1.1  christos 
   11282  1.11  christos static void
   11283   1.1  christos download_tracepoint_locations (void)
   11284   1.1  christos {
   11285   1.1  christos   enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
   11286   1.1  christos 
   11287   1.6  christos   scoped_restore_current_pspace_and_thread restore_pspace_thread;
   11288   1.6  christos 
   11289   1.6  christos   for (breakpoint &b : all_tracepoints ())
   11290   1.6  christos     {
   11291   1.6  christos       bool bp_location_downloaded = false;
   11292   1.6  christos 
   11293   1.6  christos       if ((b.type == bp_fast_tracepoint
   11294   1.6  christos 	   ? !may_insert_fast_tracepoints
   11295   1.6  christos 	   : !may_insert_tracepoints))
   11296   1.6  christos 	continue;
   11297   1.6  christos 
   11298  1.11  christos       if (can_download_tracepoint == TRIBOOL_UNKNOWN)
   11299   1.1  christos 	{
   11300   1.1  christos 	  if (target_can_download_tracepoint ())
   11301   1.1  christos 	    can_download_tracepoint = TRIBOOL_TRUE;
   11302   1.1  christos 	  else
   11303  1.11  christos 	    can_download_tracepoint = TRIBOOL_FALSE;
   11304   1.1  christos 	}
   11305   1.1  christos 
   11306  1.11  christos       if (can_download_tracepoint == TRIBOOL_FALSE)
   11307   1.1  christos 	break;
   11308  1.11  christos 
   11309   1.1  christos       for (bp_location &bl : b.locations ())
   11310  1.11  christos 	{
   11311  1.10  christos 	  /* In tracepoint, locations are _never_ duplicated, so
   11312   1.1  christos 	     should_be_inserted is equivalent to
   11313  1.11  christos 	     unduplicated_should_be_inserted.  */
   11314  1.11  christos 	  if (!should_be_inserted (&bl) || bl.inserted)
   11315  1.11  christos 	    continue;
   11316   1.1  christos 
   11317  1.11  christos 	  switch_to_program_space_and_thread (bl.pspace);
   11318   1.1  christos 
   11319   1.1  christos 	  target_download_tracepoint (&bl);
   11320   1.1  christos 
   11321   1.1  christos 	  bl.inserted = 1;
   11322   1.1  christos 	  bp_location_downloaded = true;
   11323   1.1  christos 	}
   11324   1.1  christos 
   11325   1.1  christos       tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
   11326   1.1  christos       t.number_on_target = b.number;
   11327   1.1  christos       if (bp_location_downloaded)
   11328   1.1  christos 	notify_breakpoint_modified (&b);
   11329   1.1  christos     }
   11330   1.1  christos }
   11331   1.1  christos 
   11332   1.1  christos /* Swap the insertion/duplication state between two locations.  */
   11333   1.1  christos 
   11334   1.1  christos static void
   11335   1.1  christos swap_insertion (struct bp_location *left, struct bp_location *right)
   11336   1.1  christos {
   11337   1.1  christos   const int left_inserted = left->inserted;
   11338   1.1  christos   const int left_duplicate = left->duplicate;
   11339   1.1  christos   const int left_needs_update = left->needs_update;
   11340   1.1  christos   const struct bp_target_info left_target_info = left->target_info;
   11341   1.1  christos 
   11342   1.1  christos   /* Locations of tracepoints can never be duplicated.  */
   11343   1.1  christos   if (is_tracepoint (left->owner))
   11344   1.1  christos     gdb_assert (!left->duplicate);
   11345   1.1  christos   if (is_tracepoint (right->owner))
   11346   1.1  christos     gdb_assert (!right->duplicate);
   11347   1.1  christos 
   11348   1.1  christos   left->inserted = right->inserted;
   11349   1.1  christos   left->duplicate = right->duplicate;
   11350   1.1  christos   left->needs_update = right->needs_update;
   11351   1.1  christos   left->target_info = right->target_info;
   11352   1.1  christos   right->inserted = left_inserted;
   11353   1.1  christos   right->duplicate = left_duplicate;
   11354   1.1  christos   right->needs_update = left_needs_update;
   11355   1.1  christos   right->target_info = left_target_info;
   11356   1.1  christos }
   11357   1.1  christos 
   11358   1.1  christos /* Force the re-insertion of the locations at ADDRESS.  This is called
   11359   1.1  christos    once a new/deleted/modified duplicate location is found and we are evaluating
   11360   1.1  christos    conditions on the target's side.  Such conditions need to be updated on
   11361   1.1  christos    the target.  */
   11362   1.1  christos 
   11363   1.1  christos static void
   11364   1.1  christos force_breakpoint_reinsertion (struct bp_location *bl)
   11365   1.1  christos {
   11366   1.1  christos   CORE_ADDR address = 0;
   11367   1.1  christos   int pspace_num;
   11368   1.1  christos 
   11369   1.1  christos   address = bl->address;
   11370   1.1  christos   pspace_num = bl->pspace->num;
   11371   1.1  christos 
   11372   1.1  christos   /* This is only meaningful if the target is
   11373  1.10  christos      evaluating conditions and if the user has
   11374   1.1  christos      opted for condition evaluation on the target's
   11375   1.1  christos      side.  */
   11376   1.1  christos   if (gdb_evaluates_breakpoint_condition_p ()
   11377   1.1  christos       || !target_supports_evaluation_of_breakpoint_conditions ())
   11378   1.1  christos     return;
   11379   1.1  christos 
   11380   1.1  christos   /* Flag all breakpoint locations with this address and
   11381   1.1  christos      the same program space as the location
   11382   1.1  christos      as "its condition has changed".  We need to
   11383   1.1  christos      update the conditions on the target's side.  */
   11384   1.1  christos   for (bp_location *loc : all_bp_locations_at_addr (address))
   11385   1.1  christos     {
   11386   1.1  christos       if (!is_breakpoint (loc->owner)
   11387   1.1  christos 	  || pspace_num != loc->pspace->num)
   11388   1.7  christos 	continue;
   11389   1.1  christos 
   11390   1.1  christos       /* Flag the location appropriately.  We use a different state to
   11391   1.9  christos 	 let everyone know that we already updated the set of locations
   11392   1.3  christos 	 with addr bl->address and program space bl->pspace.  This is so
   11393   1.3  christos 	 we don't have to keep calling these functions just to mark locations
   11394   1.3  christos 	 that have already been marked.  */
   11395   1.1  christos       loc->condition_changed = condition_updated;
   11396   1.3  christos 
   11397   1.3  christos       /* Free the agent expression bytecode as well.  We will compute
   11398   1.3  christos 	 it later on.  */
   11399   1.1  christos       loc->cond_bytecode.reset ();
   11400   1.1  christos     }
   11401   1.3  christos }
   11402   1.1  christos 
   11403   1.1  christos /* Called whether new breakpoints are created, or existing breakpoints
   11404   1.1  christos    deleted, to update the global location list and recompute which
   11405   1.1  christos    locations are duplicate of which.
   11406   1.1  christos 
   11407   1.1  christos    The INSERT_MODE flag determines whether locations may not, may, or
   11408  1.11  christos    shall be inserted now.  See 'enum ugll_insert_mode' for more
   11409  1.11  christos    info.  */
   11410  1.11  christos 
   11411   1.1  christos static void
   11412   1.1  christos update_global_location_list (enum ugll_insert_mode insert_mode)
   11413   1.1  christos {
   11414   1.1  christos   /* Last breakpoint location address that was marked for update.  */
   11415   1.1  christos   CORE_ADDR last_addr = 0;
   11416   1.1  christos   /* Last breakpoint location program space that was marked for update.  */
   11417   1.1  christos   int last_pspace_num = -1;
   11418   1.1  christos 
   11419   1.1  christos   breakpoint_debug_printf ("insert_mode = %s",
   11420   1.1  christos 			   ugll_insert_mode_text (insert_mode));
   11421   1.1  christos 
   11422   1.7  christos   /* Used in the duplicates detection below.  When iterating over all
   11423   1.7  christos      bp_locations, points to the first bp_location of a given address.
   11424  1.10  christos      Breakpoints and watchpoints of different types are never
   11425  1.10  christos      duplicates of each other.  Keep one pointer for each type of
   11426  1.10  christos      breakpoint/watchpoint, so we only need to loop over all locations
   11427  1.11  christos      once.  */
   11428  1.11  christos   struct bp_location *bp_loc_first;  /* breakpoint */
   11429  1.11  christos   struct bp_location *wp_loc_first;  /* hardware watchpoint */
   11430   1.9  christos   struct bp_location *awp_loc_first; /* access watchpoint */
   11431   1.9  christos   struct bp_location *rwp_loc_first; /* read watchpoint */
   11432   1.9  christos 
   11433   1.9  christos   /* Saved former bp_locations array which we compare against the newly
   11434   1.9  christos      built bp_locations from the current state of ALL_BREAKPOINTS.  */
   11435  1.10  christos   std::vector<bp_location *> old_locations = std::move (bp_locations);
   11436  1.10  christos   bp_locations.clear ();
   11437   1.9  christos 
   11438   1.9  christos   for (breakpoint &b : all_breakpoints ())
   11439  1.10  christos     for (bp_location &loc : b.locations ())
   11440   1.9  christos       bp_locations.push_back (&loc);
   11441   1.1  christos 
   11442   1.7  christos   /* See if we need to "upgrade" a software breakpoint to a hardware
   11443   1.1  christos      breakpoint.  Do this before deciding whether locations are
   11444   1.1  christos      duplicates.  Also do this before sorting because sorting order
   11445   1.1  christos      depends on location type.  */
   11446   1.1  christos   for (bp_location *loc : bp_locations)
   11447   1.1  christos     if (!loc->inserted && should_be_inserted (loc))
   11448   1.1  christos 	handle_automatic_hardware_breakpoints (loc);
   11449   1.1  christos 
   11450   1.1  christos   std::sort (bp_locations.begin (), bp_locations.end (),
   11451   1.1  christos 	     bp_location_is_less_than);
   11452   1.1  christos 
   11453   1.1  christos   bp_locations_target_extensions_update ();
   11454  1.10  christos 
   11455  1.10  christos   /* Identify bp_location instances that are no longer present in the
   11456   1.1  christos      new list, and therefore should be freed.  Note that it's not
   11457   1.1  christos      necessary that those locations should be removed from inferior --
   11458   1.1  christos      if there's another location at the same address (previously
   11459  1.10  christos      marked as duplicate), we don't need to remove/insert the
   11460   1.1  christos      location.
   11461  1.10  christos 
   11462  1.10  christos      LOCP is kept in sync with OLD_LOCP, each pointing to the current
   11463   1.1  christos      and former bp_location array state respectively.  */
   11464   1.1  christos 
   11465   1.1  christos   size_t loc_i = 0;
   11466  1.10  christos   for (bp_location *old_loc : old_locations)
   11467  1.10  christos     {
   11468  1.10  christos       /* Tells if 'old_loc' is found among the new locations.  If
   11469  1.10  christos 	 not, we have to free it.  */
   11470  1.10  christos       bool found_object = false;
   11471  1.10  christos       /* Tells if the location should remain inserted in the target.  */
   11472  1.10  christos       bool keep_in_target = false;
   11473  1.10  christos       bool removed = false;
   11474   1.1  christos 
   11475   1.1  christos       /* Skip LOCP entries which will definitely never be needed.
   11476   1.1  christos 	 Stop either at or being the one matching OLD_LOC.  */
   11477   1.1  christos       while (loc_i < bp_locations.size ()
   11478   1.1  christos 	     && bp_locations[loc_i]->address < old_loc->address)
   11479  1.10  christos 	loc_i++;
   11480   1.1  christos 
   11481   1.1  christos       for (size_t loc2_i = loc_i;
   11482   1.1  christos 	   (loc2_i < bp_locations.size ()
   11483  1.10  christos 	    && bp_locations[loc2_i]->address == old_loc->address);
   11484   1.1  christos 	   loc2_i++)
   11485   1.1  christos 	{
   11486   1.1  christos 	  /* Check if this is a new/duplicated location or a duplicated
   11487  1.10  christos 	     location that had its condition modified.  If so, we want to send
   11488  1.10  christos 	     its condition to the target if evaluation of conditions is taking
   11489   1.1  christos 	     place there.  */
   11490   1.1  christos 	  if (bp_locations[loc2_i]->condition_changed == condition_modified
   11491   1.1  christos 	      && (last_addr != old_loc->address
   11492   1.1  christos 		  || last_pspace_num != old_loc->pspace->num))
   11493   1.1  christos 	    {
   11494   1.1  christos 	      force_breakpoint_reinsertion (bp_locations[loc2_i]);
   11495   1.1  christos 	      last_pspace_num = old_loc->pspace->num;
   11496   1.1  christos 	    }
   11497   1.1  christos 
   11498   1.1  christos 	  if (bp_locations[loc2_i] == old_loc)
   11499   1.1  christos 	    found_object = true;
   11500   1.1  christos 	}
   11501   1.1  christos 
   11502   1.1  christos       /* We have already handled this address, update it so that we don't
   11503   1.1  christos 	 have to go through updates again.  */
   11504   1.1  christos       last_addr = old_loc->address;
   11505   1.1  christos 
   11506   1.1  christos       /* Target-side condition evaluation: Handle deleted locations.  */
   11507   1.1  christos       if (!found_object)
   11508   1.1  christos 	force_breakpoint_reinsertion (old_loc);
   11509   1.1  christos 
   11510   1.1  christos       /* If this location is no longer present, and inserted, look if
   11511   1.1  christos 	 there's maybe a new location at the same address.  If so,
   11512   1.1  christos 	 mark that one inserted, and don't remove this one.  This is
   11513   1.1  christos 	 needed so that we don't have a time window where a breakpoint
   11514  1.10  christos 	 at certain location is not inserted.  */
   11515   1.1  christos 
   11516   1.1  christos       if (old_loc->inserted)
   11517   1.1  christos 	{
   11518   1.1  christos 	  /* If the location is inserted now, we might have to remove
   11519   1.1  christos 	     it.  */
   11520   1.1  christos 
   11521   1.1  christos 	  if (found_object && should_be_inserted (old_loc))
   11522   1.1  christos 	    {
   11523   1.1  christos 	      /* The location is still present in the location list,
   11524   1.1  christos 		 and still should be inserted.  Don't do anything.  */
   11525   1.1  christos 	      keep_in_target = true;
   11526   1.1  christos 	    }
   11527   1.1  christos 	  else
   11528   1.9  christos 	    {
   11529   1.1  christos 	      /* This location still exists, but it won't be kept in the
   11530  1.10  christos 		 target since it may have been disabled.  We proceed to
   11531  1.10  christos 		 remove its target-side condition.  */
   11532  1.10  christos 
   11533  1.10  christos 	      /* The location is either no longer present, or got
   11534   1.1  christos 		 disabled.  See if there's another location at the
   11535  1.10  christos 		 same address, in which case we don't need to remove
   11536   1.1  christos 		 this one from the target.  */
   11537   1.9  christos 
   11538   1.9  christos 	      /* OLD_LOC comes from existing struct breakpoint.  */
   11539   1.9  christos 	      if (bl_address_is_meaningful (old_loc))
   11540   1.1  christos 		{
   11541   1.1  christos 		  for (size_t loc2_i = loc_i;
   11542   1.1  christos 		       (loc2_i < bp_locations.size ()
   11543   1.1  christos 			&& bp_locations[loc2_i]->address == old_loc->address);
   11544   1.1  christos 		       loc2_i++)
   11545   1.1  christos 		    {
   11546   1.1  christos 		      bp_location *loc2 = bp_locations[loc2_i];
   11547   1.1  christos 
   11548   1.1  christos 		      if (loc2 == old_loc)
   11549   1.1  christos 			continue;
   11550   1.1  christos 
   11551   1.1  christos 		      if (breakpoint_locations_match (loc2, old_loc))
   11552   1.1  christos 			{
   11553   1.1  christos 			  /* Read watchpoint locations are switched to
   11554   1.9  christos 			     access watchpoints, if the former are not
   11555   1.1  christos 			     supported, but the latter are.  */
   11556   1.1  christos 			  if (is_hardware_watchpoint (old_loc->owner))
   11557  1.10  christos 			    {
   11558   1.1  christos 			      gdb_assert (is_hardware_watchpoint (loc2->owner));
   11559   1.1  christos 			      loc2->watchpoint_type = old_loc->watchpoint_type;
   11560   1.1  christos 			    }
   11561   1.1  christos 
   11562   1.1  christos 			  /* loc2 is a duplicated location. We need to check
   11563   1.1  christos 			     if it should be inserted in case it will be
   11564   1.1  christos 			     unduplicated.  */
   11565   1.1  christos 			  if (unduplicated_should_be_inserted (loc2))
   11566   1.1  christos 			    {
   11567   1.6  christos 			      swap_insertion (old_loc, loc2);
   11568   1.1  christos 			      keep_in_target = true;
   11569   1.1  christos 			      break;
   11570   1.1  christos 			    }
   11571   1.1  christos 			}
   11572   1.1  christos 		    }
   11573   1.1  christos 		}
   11574   1.1  christos 	    }
   11575   1.1  christos 
   11576   1.1  christos 	  if (!keep_in_target)
   11577  1.11  christos 	    {
   11578  1.11  christos 	      if (remove_breakpoint (old_loc))
   11579  1.11  christos 		{
   11580   1.1  christos 		  /* This is just about all we can do.  We could keep
   11581  1.10  christos 		     this location on the global list, and try to
   11582   1.1  christos 		     remove it next time, but there's no particular
   11583   1.1  christos 		     reason why we will succeed next time.
   11584   1.1  christos 
   11585   1.1  christos 		     Note that at this point, old_loc->owner is still
   11586   1.1  christos 		     valid, as delete_breakpoint frees the breakpoint
   11587   1.6  christos 		     only after calling us.  */
   11588   1.5  christos 		  warning (_("error removing breakpoint %d at %s"),
   11589   1.1  christos 			   old_loc->owner->number,
   11590   1.1  christos 			   paddress (old_loc->gdbarch, old_loc->address));
   11591   1.1  christos 		}
   11592   1.1  christos 	      removed = true;
   11593   1.1  christos 	    }
   11594   1.1  christos 	}
   11595   1.1  christos 
   11596   1.1  christos       if (!found_object)
   11597   1.1  christos 	{
   11598   1.1  christos 	  if (removed && target_is_non_stop_p ()
   11599   1.1  christos 	      && need_moribund_for_location_type (old_loc))
   11600   1.1  christos 	    {
   11601   1.1  christos 	      /* This location was removed from the target.  In
   11602   1.1  christos 		 non-stop mode, a race condition is possible where
   11603   1.1  christos 		 we've removed a breakpoint, but stop events for that
   11604   1.1  christos 		 breakpoint are already queued and will arrive later.
   11605   1.1  christos 		 We apply an heuristic to be able to distinguish such
   11606   1.1  christos 		 SIGTRAPs from other random SIGTRAPs: we keep this
   11607   1.1  christos 		 breakpoint location for a bit, and will retire it
   11608   1.1  christos 		 after we see some number of events.  The theory here
   11609   1.1  christos 		 is that reporting of events should, "on the average",
   11610   1.1  christos 		 be fair, so after a while we'll see events from all
   11611   1.1  christos 		 threads that have anything of interest, and no longer
   11612   1.1  christos 		 need to keep this breakpoint location around.  We
   11613   1.1  christos 		 don't hold locations forever so to reduce chances of
   11614   1.1  christos 		 mistaking a non-breakpoint SIGTRAP for a breakpoint
   11615   1.1  christos 		 SIGTRAP.
   11616   1.1  christos 
   11617   1.1  christos 		 The heuristic failing can be disastrous on
   11618   1.1  christos 		 decr_pc_after_break targets.
   11619   1.1  christos 
   11620   1.1  christos 		 On decr_pc_after_break targets, like e.g., x86-linux,
   11621   1.1  christos 		 if we fail to recognize a late breakpoint SIGTRAP,
   11622   1.1  christos 		 because events_till_retirement has reached 0 too
   11623   1.1  christos 		 soon, we'll fail to do the PC adjustment, and report
   11624   1.1  christos 		 a random SIGTRAP to the user.  When the user resumes
   11625   1.1  christos 		 the inferior, it will most likely immediately crash
   11626   1.1  christos 		 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
   11627   1.1  christos 		 corrupted, because of being resumed e.g., in the
   11628   1.1  christos 		 middle of a multi-byte instruction, or skipped a
   11629   1.1  christos 		 one-byte instruction.  This was actually seen happen
   11630   1.1  christos 		 on native x86-linux, and should be less rare on
   11631   1.1  christos 		 targets that do not support new thread events, like
   11632   1.1  christos 		 remote, due to the heuristic depending on
   11633   1.1  christos 		 thread_count.
   11634   1.1  christos 
   11635   1.9  christos 		 Mistaking a random SIGTRAP for a breakpoint trap
   11636   1.9  christos 		 causes similar symptoms (PC adjustment applied when
   11637   1.9  christos 		 it shouldn't), but then again, playing with SIGTRAPs
   11638   1.9  christos 		 behind the debugger's back is asking for trouble.
   11639   1.9  christos 
   11640   1.9  christos 		 Since hardware watchpoint traps are always
   11641   1.9  christos 		 distinguishable from other traps, so we don't need to
   11642   1.9  christos 		 apply keep hardware watchpoint moribund locations
   11643   1.9  christos 		 around.  We simply always ignore hardware watchpoint
   11644   1.9  christos 		 traps we can no longer explain.  */
   11645   1.9  christos 
   11646   1.9  christos 	      process_stratum_target *proc_target = nullptr;
   11647   1.1  christos 	      for (inferior *inf : all_inferiors ())
   11648   1.1  christos 		if (inf->pspace == old_loc->pspace)
   11649   1.8  christos 		  {
   11650   1.1  christos 		    proc_target = inf->process_target ();
   11651   1.1  christos 		    break;
   11652   1.1  christos 		  }
   11653   1.1  christos 	      if (proc_target != nullptr)
   11654   1.1  christos 		old_loc->events_till_retirement
   11655   1.1  christos 		  = 3 * (thread_count (proc_target) + 1);
   11656   1.1  christos 	      else
   11657   1.1  christos 		old_loc->events_till_retirement = 1;
   11658   1.1  christos 	      old_loc->owner = NULL;
   11659   1.1  christos 
   11660   1.1  christos 	      moribund_locations.push_back (old_loc);
   11661   1.1  christos 	    }
   11662   1.1  christos 	  else
   11663   1.1  christos 	    {
   11664   1.1  christos 	      old_loc->owner = NULL;
   11665   1.1  christos 	      decref_bp_location (&old_loc);
   11666   1.1  christos 	    }
   11667   1.1  christos 	}
   11668   1.1  christos     }
   11669   1.1  christos 
   11670   1.1  christos   /* Rescan breakpoints at the same address and section, marking the
   11671   1.1  christos      first one as "first" and any others as "duplicates".  This is so
   11672   1.1  christos      that the bpt instruction is only inserted once.  If we have a
   11673  1.10  christos      permanent breakpoint at the same place as BPT, make that one the
   11674  1.10  christos      official one, and the rest as duplicates.  Permanent breakpoints
   11675   1.1  christos      are sorted first for the same address.
   11676   1.1  christos 
   11677   1.1  christos      Do the same for hardware watchpoints, but also considering the
   11678   1.1  christos      watchpoint's type (regular/access/read) and length.  */
   11679  1.10  christos 
   11680   1.1  christos   bp_loc_first = NULL;
   11681   1.1  christos   wp_loc_first = NULL;
   11682   1.9  christos   awp_loc_first = NULL;
   11683   1.1  christos   rwp_loc_first = NULL;
   11684   1.1  christos 
   11685   1.1  christos   for (bp_location *loc : all_bp_locations ())
   11686   1.1  christos     {
   11687   1.1  christos       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
   11688   1.1  christos 	 non-NULL.  */
   11689   1.1  christos       struct bp_location **loc_first_p;
   11690   1.1  christos       breakpoint *b = loc->owner;
   11691   1.1  christos 
   11692   1.1  christos       if (!unduplicated_should_be_inserted (loc)
   11693   1.1  christos 	  || !bl_address_is_meaningful (loc)
   11694   1.1  christos 	  /* Don't detect duplicate for tracepoint locations because they are
   11695   1.1  christos 	   never duplicated.  See the comments in field `duplicate' of
   11696   1.1  christos 	   `struct bp_location'.  */
   11697   1.1  christos 	  || is_tracepoint (b))
   11698   1.1  christos 	{
   11699   1.1  christos 	  /* Clear the condition modification flag.  */
   11700   1.1  christos 	  loc->condition_changed = condition_unchanged;
   11701   1.1  christos 	  continue;
   11702   1.1  christos 	}
   11703   1.1  christos 
   11704   1.1  christos       if (b->type == bp_hardware_watchpoint)
   11705   1.1  christos 	loc_first_p = &wp_loc_first;
   11706   1.1  christos       else if (b->type == bp_read_watchpoint)
   11707   1.1  christos 	loc_first_p = &rwp_loc_first;
   11708   1.1  christos       else if (b->type == bp_access_watchpoint)
   11709   1.1  christos 	loc_first_p = &awp_loc_first;
   11710   1.1  christos       else
   11711   1.1  christos 	loc_first_p = &bp_loc_first;
   11712   1.1  christos 
   11713   1.1  christos       if (*loc_first_p == NULL
   11714   1.1  christos 	  || (overlay_debugging && loc->section != (*loc_first_p)->section)
   11715   1.1  christos 	  || !breakpoint_locations_match (loc, *loc_first_p))
   11716   1.1  christos 	{
   11717   1.1  christos 	  *loc_first_p = loc;
   11718   1.1  christos 	  loc->duplicate = 0;
   11719   1.1  christos 
   11720   1.1  christos 	  if (is_breakpoint (loc->owner) && loc->condition_changed)
   11721   1.1  christos 	    {
   11722   1.1  christos 	      loc->needs_update = 1;
   11723   1.1  christos 	      /* Clear the condition modification flag.  */
   11724   1.1  christos 	      loc->condition_changed = condition_unchanged;
   11725   1.1  christos 	    }
   11726   1.1  christos 	  continue;
   11727   1.1  christos 	}
   11728   1.1  christos 
   11729   1.1  christos 
   11730   1.3  christos       /* This and the above ensure the invariant that the first location
   11731   1.1  christos 	 is not duplicated, and is the inserted one.
   11732   1.3  christos 	 All following are marked as duplicated, and are not inserted.  */
   11733   1.1  christos       if (loc->inserted)
   11734   1.1  christos 	swap_insertion (loc, *loc_first_p);
   11735   1.1  christos       loc->duplicate = 1;
   11736   1.3  christos 
   11737   1.3  christos       /* Clear the condition modification flag.  */
   11738   1.3  christos       loc->condition_changed = condition_unchanged;
   11739   1.3  christos     }
   11740   1.1  christos 
   11741   1.1  christos   if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
   11742   1.1  christos     {
   11743   1.1  christos       if (insert_mode != UGLL_DONT_INSERT)
   11744   1.1  christos 	insert_breakpoint_locations ();
   11745   1.1  christos       else
   11746   1.3  christos 	{
   11747   1.1  christos 	  /* Even though the caller told us to not insert new
   11748   1.1  christos 	     locations, we may still need to update conditions on the
   11749   1.1  christos 	     target's side of breakpoints that were already inserted
   11750   1.1  christos 	     if the target is evaluating breakpoint conditions.  We
   11751   1.1  christos 	     only update conditions for locations that are marked
   11752   1.1  christos 	     "needs_update".  */
   11753   1.8  christos 	  update_inserted_breakpoint_locations ();
   11754   1.8  christos 	}
   11755   1.8  christos     }
   11756   1.8  christos 
   11757   1.8  christos   if (insert_mode != UGLL_DONT_INSERT)
   11758   1.8  christos     download_tracepoint_locations ();
   11759   1.8  christos }
   11760   1.8  christos 
   11761   1.8  christos void
   11762   1.8  christos breakpoint_retire_moribund (void)
   11763   1.1  christos {
   11764   1.1  christos   for (int ix = 0; ix < moribund_locations.size (); ++ix)
   11765   1.1  christos     {
   11766   1.3  christos       struct bp_location *loc = moribund_locations[ix];
   11767   1.1  christos       if (--(loc->events_till_retirement) == 0)
   11768   1.1  christos 	{
   11769   1.9  christos 	  decref_bp_location (&loc);
   11770   1.5  christos 	  unordered_remove (moribund_locations, ix);
   11771   1.5  christos 	  --ix;
   11772   1.5  christos 	}
   11773   1.9  christos     }
   11774   1.5  christos }
   11775   1.5  christos 
   11776   1.1  christos static void
   11777   1.1  christos update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
   11778   1.1  christos {
   11779   1.1  christos 
   11780   1.1  christos   try
   11781  1.10  christos     {
   11782   1.1  christos       update_global_location_list (insert_mode);
   11783  1.10  christos     }
   11784   1.1  christos   catch (const gdb_exception_error &e)
   11785   1.1  christos     {
   11786   1.1  christos     }
   11787   1.1  christos }
   11788   1.1  christos 
   11789   1.1  christos /* Clear BKP from a BPS.  */
   11790   1.1  christos 
   11791   1.1  christos static void
   11792   1.1  christos bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
   11793   1.1  christos {
   11794   1.1  christos   bpstat *bs;
   11795   1.1  christos 
   11796   1.1  christos   for (bs = bps; bs; bs = bs->next)
   11797   1.1  christos     if (bs->breakpoint_at == bpt)
   11798   1.6  christos       {
   11799   1.1  christos 	bs->breakpoint_at = NULL;
   11800   1.1  christos 	bs->old_val = NULL;
   11801   1.1  christos 	/* bs->commands will be freed later.  */
   11802   1.1  christos       }
   11803   1.1  christos }
   11804  1.11  christos 
   11805   1.1  christos /* Callback for iterate_over_threads.  */
   11806  1.11  christos static int
   11807  1.11  christos bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
   11808   1.1  christos {
   11809   1.1  christos   struct breakpoint *bpt = (struct breakpoint *) data;
   11810   1.1  christos 
   11811   1.1  christos   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
   11812   1.1  christos   return 0;
   11813   1.1  christos }
   11814   1.1  christos 
   11815  1.11  christos /* See breakpoint.h.  */
   11816   1.1  christos 
   11817   1.6  christos void
   11818  1.11  christos code_breakpoint::say_where () const
   11819   1.6  christos {
   11820  1.11  christos   struct value_print_options opts;
   11821   1.6  christos 
   11822  1.11  christos   get_user_print_options (&opts);
   11823   1.6  christos 
   11824  1.11  christos   /* i18n: cagney/2005-02-11: Below needs to be merged into a
   11825   1.6  christos      single string.  */
   11826  1.10  christos   if (!this->has_locations ())
   11827  1.11  christos     {
   11828  1.11  christos       /* For pending locations, the output differs slightly based
   11829   1.6  christos 	 on extra_string.  If this is non-NULL, it contains either
   11830   1.6  christos 	 a condition or dprintf arguments.  */
   11831   1.6  christos       if (extra_string == NULL)
   11832  1.10  christos 	{
   11833  1.11  christos 	  gdb_printf (_(" (%s) pending."), locspec->to_string ());
   11834  1.11  christos 	}
   11835   1.6  christos       else if (type == bp_dprintf)
   11836   1.1  christos 	{
   11837   1.1  christos 	  gdb_printf (_(" (%s,%s) pending."),
   11838   1.1  christos 		      locspec->to_string (),
   11839  1.11  christos 		      extra_string.get ());
   11840  1.11  christos 	}
   11841  1.10  christos       else
   11842  1.10  christos 	{
   11843  1.11  christos 	  gdb_printf (_(" (%s %s) pending."),
   11844  1.11  christos 		      locspec->to_string (),
   11845  1.11  christos 		      extra_string.get ());
   11846   1.1  christos 	}
   11847   1.1  christos     }
   11848   1.1  christos   else
   11849  1.11  christos     {
   11850   1.8  christos       const bp_location &bl = this->first_loc ();
   11851   1.9  christos       if (opts.addressprint || bl.symtab == nullptr)
   11852  1.11  christos 	gdb_printf (" at %ps",
   11853  1.10  christos 		    styled_string (address_style.style (),
   11854  1.10  christos 				   paddress (bl.gdbarch,
   11855  1.10  christos 					     bl.address)));
   11856  1.11  christos       if (bl.symtab != NULL)
   11857   1.8  christos 	{
   11858   1.1  christos 	  /* If there is a single location, we can print the location
   11859   1.1  christos 	     more nicely.  */
   11860   1.1  christos 	  if (!this->has_multiple_locations ())
   11861   1.1  christos 	    {
   11862  1.11  christos 	      const char *filename
   11863   1.1  christos 		= symtab_to_filename_for_display (bl.symtab);
   11864   1.1  christos 	      gdb_printf (": file %ps, line %d.",
   11865  1.11  christos 			  styled_string (file_name_style.style (),
   11866   1.1  christos 					 filename),
   11867  1.11  christos 			  bl.line_number);
   11868  1.10  christos 	    }
   11869   1.1  christos 	  else
   11870   1.1  christos 	    /* This is not ideal, but each location may have a
   11871   1.1  christos 	       different file name, and this at least reflects the
   11872   1.1  christos 	       real situation somewhat.  */
   11873  1.10  christos 	    gdb_printf (": %s.", locspec->to_string ());
   11874   1.1  christos 	}
   11875  1.10  christos 
   11876   1.1  christos       if (this->has_multiple_locations ())
   11877  1.11  christos 	{
   11878   1.1  christos 	  int n = std::distance (m_locations.begin (), m_locations.end ());
   11879   1.1  christos 	  gdb_printf (" (%d locations)", n);
   11880  1.10  christos 	}
   11881  1.10  christos     }
   11882   1.1  christos }
   11883  1.10  christos 
   11884   1.1  christos /* See breakpoint.h.  */
   11885   1.1  christos 
   11886  1.11  christos bp_location_range breakpoint::locations () const
   11887  1.11  christos {
   11888  1.11  christos   return bp_location_range (m_locations.begin (), m_locations.end ());
   11889  1.11  christos }
   11890  1.11  christos 
   11891  1.11  christos struct bp_location *
   11892  1.11  christos breakpoint::allocate_location ()
   11893  1.11  christos {
   11894  1.11  christos   return new bp_location (this);
   11895  1.11  christos }
   11896  1.11  christos 
   11897  1.11  christos /* See breakpoint.h.  */
   11898  1.11  christos 
   11899  1.11  christos void
   11900  1.11  christos breakpoint::add_location (bp_location &loc)
   11901  1.11  christos {
   11902  1.11  christos   gdb_assert (loc.owner == this);
   11903  1.11  christos   gdb_assert (!loc.is_linked ());
   11904  1.11  christos 
   11905  1.11  christos   auto ub = std::upper_bound (m_locations.begin (), m_locations.end (),
   11906  1.11  christos 			      loc,
   11907  1.11  christos 			      [] (const bp_location &left,
   11908  1.11  christos 				  const bp_location &right)
   11909  1.11  christos 				{ return left.address < right.address; });
   11910  1.11  christos   m_locations.insert (ub, loc);
   11911  1.11  christos }
   11912  1.11  christos 
   11913   1.1  christos /* See breakpoint.h.  */
   11914   1.1  christos 
   11915   1.1  christos void
   11916  1.10  christos breakpoint::unadd_location (bp_location &loc)
   11917  1.10  christos {
   11918   1.1  christos   gdb_assert (loc.owner == this);
   11919   1.1  christos   gdb_assert (loc.is_linked ());
   11920   1.1  christos 
   11921   1.1  christos   m_locations.erase (m_locations.iterator_to (loc));
   11922  1.10  christos }
   11923  1.10  christos 
   11924  1.10  christos #define internal_error_pure_virtual_called() \
   11925   1.1  christos   gdb_assert_not_reached ("pure virtual function called")
   11926   1.1  christos 
   11927   1.1  christos int
   11928   1.1  christos breakpoint::insert_location (struct bp_location *bl)
   11929  1.10  christos {
   11930  1.10  christos   internal_error_pure_virtual_called ();
   11931  1.10  christos }
   11932  1.10  christos 
   11933  1.10  christos int
   11934   1.1  christos breakpoint::remove_location (struct bp_location *bl,
   11935   1.1  christos 			     enum remove_bp_reason reason)
   11936   1.1  christos {
   11937   1.1  christos   internal_error_pure_virtual_called ();
   11938  1.10  christos }
   11939  1.10  christos 
   11940   1.1  christos int
   11941   1.1  christos breakpoint::breakpoint_hit (const struct bp_location *bl,
   11942   1.1  christos 			    const address_space *aspace,
   11943   1.1  christos 			    CORE_ADDR bp_addr,
   11944  1.10  christos 			    const target_waitstatus &ws)
   11945  1.10  christos {
   11946   1.1  christos   internal_error_pure_virtual_called ();
   11947   1.1  christos }
   11948   1.1  christos 
   11949   1.1  christos int
   11950  1.10  christos breakpoint::resources_needed (const struct bp_location *bl)
   11951  1.10  christos {
   11952   1.1  christos   internal_error_pure_virtual_called ();
   11953   1.1  christos }
   11954   1.1  christos 
   11955   1.1  christos enum print_stop_action
   11956  1.10  christos breakpoint::print_it (const bpstat *bs) const
   11957  1.10  christos {
   11958   1.1  christos   internal_error_pure_virtual_called ();
   11959   1.1  christos }
   11960   1.1  christos 
   11961   1.1  christos void
   11962   1.1  christos breakpoint::print_mention () const
   11963   1.1  christos {
   11964  1.10  christos   internal_error_pure_virtual_called ();
   11965  1.10  christos }
   11966   1.1  christos 
   11967   1.1  christos void
   11968  1.10  christos breakpoint::print_recreate (struct ui_file *fp) const
   11969   1.1  christos {
   11970   1.6  christos   internal_error_pure_virtual_called ();
   11971  1.10  christos }
   11972   1.1  christos 
   11973   1.1  christos /* Default breakpoint_ops methods.  */
   11974   1.1  christos 
   11975  1.10  christos void
   11976   1.1  christos code_breakpoint::re_set ()
   11977   1.1  christos {
   11978  1.10  christos   /* FIXME: is this still reachable?  */
   11979  1.10  christos   if (breakpoint_location_spec_empty_p (this))
   11980   1.1  christos     {
   11981   1.7  christos       /* Anything without a location can't be re-set.  */
   11982   1.7  christos       delete_breakpoint (this);
   11983   1.7  christos       return;
   11984   1.7  christos     }
   11985   1.7  christos 
   11986  1.10  christos   re_set_default ();
   11987   1.1  christos }
   11988  1.10  christos 
   11989   1.1  christos int
   11990  1.10  christos code_breakpoint::insert_location (struct bp_location *bl)
   11991  1.10  christos {
   11992  1.10  christos   CORE_ADDR addr = bl->target_info.reqstd_address;
   11993  1.10  christos 
   11994  1.10  christos   bl->target_info.kind = breakpoint_kind (bl, &addr);
   11995  1.10  christos   bl->target_info.placed_address = addr;
   11996  1.10  christos 
   11997  1.10  christos   int result;
   11998  1.10  christos   if (bl->loc_type == bp_loc_hardware_breakpoint)
   11999  1.10  christos     result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
   12000   1.1  christos   else
   12001   1.1  christos     result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
   12002  1.10  christos 
   12003  1.10  christos   if (result == 0 && bl->probe.prob != nullptr)
   12004  1.10  christos     {
   12005   1.1  christos       /* The insertion was successful, now let's set the probe's semaphore
   12006  1.10  christos 	 if needed.  */
   12007  1.10  christos       bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
   12008  1.10  christos     }
   12009  1.10  christos 
   12010  1.10  christos   return result;
   12011  1.10  christos }
   12012   1.1  christos 
   12013   1.1  christos int
   12014   1.1  christos code_breakpoint::remove_location (struct bp_location *bl,
   12015   1.6  christos 				  enum remove_bp_reason reason)
   12016   1.1  christos {
   12017   1.1  christos   if (bl->probe.prob != nullptr)
   12018  1.10  christos     {
   12019  1.10  christos       /* Let's clear the semaphore before removing the location.  */
   12020  1.10  christos       bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
   12021  1.10  christos     }
   12022  1.10  christos 
   12023   1.1  christos   if (bl->loc_type == bp_loc_hardware_breakpoint)
   12024  1.10  christos     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
   12025  1.10  christos   else
   12026   1.1  christos     return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
   12027   1.1  christos }
   12028  1.11  christos 
   12029   1.1  christos int
   12030   1.1  christos code_breakpoint::breakpoint_hit (const struct bp_location *bl,
   12031   1.1  christos 				 const address_space *aspace,
   12032   1.1  christos 				 CORE_ADDR bp_addr,
   12033   1.1  christos 				 const target_waitstatus &ws)
   12034   1.1  christos {
   12035   1.1  christos   if (ws.kind () != TARGET_WAITKIND_STOPPED
   12036   1.1  christos       || ws.sig () != GDB_SIGNAL_TRAP)
   12037   1.1  christos     return 0;
   12038   1.1  christos 
   12039   1.1  christos   if (!breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
   12040  1.10  christos 				 aspace, bp_addr))
   12041  1.10  christos     return 0;
   12042  1.10  christos 
   12043  1.10  christos   if (overlay_debugging		/* unmapped overlay section */
   12044  1.10  christos       && section_is_overlay (bl->section)
   12045   1.3  christos       && !section_is_mapped (bl->section))
   12046   1.3  christos     return 0;
   12047   1.3  christos 
   12048   1.3  christos   return 1;
   12049   1.3  christos }
   12050   1.3  christos 
   12051   1.3  christos int
   12052   1.3  christos dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
   12053   1.3  christos 				    const address_space *aspace,
   12054   1.3  christos 				    CORE_ADDR bp_addr,
   12055  1.10  christos 				    const target_waitstatus &ws)
   12056   1.3  christos {
   12057   1.3  christos   if (dprintf_style == dprintf_style_agent
   12058  1.10  christos       && target_can_run_breakpoint_commands ())
   12059  1.10  christos     {
   12060   1.1  christos       /* An agent-style dprintf never causes a stop.  If we see a trap
   12061  1.10  christos 	 for this address it must be for a breakpoint that happens to
   12062   1.1  christos 	 be set at the same address.  */
   12063   1.1  christos       return 0;
   12064   1.1  christos     }
   12065   1.1  christos 
   12066  1.10  christos   return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
   12067  1.10  christos }
   12068   1.1  christos 
   12069   1.1  christos int
   12070   1.1  christos ordinary_breakpoint::resources_needed (const struct bp_location *bl)
   12071   1.1  christos {
   12072   1.1  christos   gdb_assert (type == bp_hardware_breakpoint);
   12073  1.10  christos 
   12074   1.1  christos   return 1;
   12075  1.10  christos }
   12076   1.1  christos 
   12077   1.1  christos enum print_stop_action
   12078   1.1  christos ordinary_breakpoint::print_it (const bpstat *bs) const
   12079  1.10  christos {
   12080  1.10  christos   const struct bp_location *bl;
   12081   1.6  christos   int bp_temp;
   12082   1.6  christos   struct ui_out *uiout = current_uiout;
   12083   1.7  christos 
   12084   1.1  christos   bl = bs->bp_location_at.get ();
   12085   1.7  christos 
   12086   1.1  christos   bp_temp = disposition == disp_del;
   12087  1.10  christos   if (bl->address != bl->requested_address)
   12088   1.1  christos     breakpoint_adjustment_warning (bl->requested_address,
   12089  1.10  christos 				   bl->address,
   12090   1.9  christos 				   number, true);
   12091  1.10  christos   annotate_breakpoint (number);
   12092   1.9  christos   maybe_print_thread_hit_breakpoint (uiout);
   12093  1.10  christos 
   12094  1.10  christos   if (uiout->is_mi_like_p ())
   12095  1.10  christos     {
   12096   1.1  christos       uiout->field_string ("reason",
   12097   1.1  christos 			   async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
   12098   1.1  christos       uiout->field_string ("disp", bpdisp_text (disposition));
   12099   1.1  christos     }
   12100  1.10  christos 
   12101  1.10  christos   if (bp_temp)
   12102   1.1  christos     uiout->text ("Temporary breakpoint ");
   12103   1.7  christos   else
   12104   1.1  christos     uiout->text ("Breakpoint ");
   12105   1.1  christos   print_num_locno (bs, uiout);
   12106  1.10  christos   uiout->text (", ");
   12107   1.1  christos 
   12108   1.1  christos   return PRINT_SRC_AND_LOC;
   12109   1.1  christos }
   12110  1.10  christos 
   12111  1.10  christos void
   12112   1.1  christos ordinary_breakpoint::print_mention () const
   12113  1.10  christos {
   12114  1.10  christos   if (current_uiout->is_mi_like_p ())
   12115  1.10  christos     return;
   12116  1.10  christos 
   12117   1.1  christos   switch (type)
   12118   1.1  christos     {
   12119  1.10  christos     case bp_breakpoint:
   12120   1.1  christos     case bp_gnu_ifunc_resolver:
   12121   1.1  christos       if (disposition == disp_del)
   12122  1.10  christos 	gdb_printf (_("Temporary breakpoint"));
   12123   1.1  christos       else
   12124   1.1  christos 	gdb_printf (_("Breakpoint"));
   12125   1.1  christos       gdb_printf (_(" %d"), number);
   12126  1.11  christos       if (type == bp_gnu_ifunc_resolver)
   12127   1.1  christos 	gdb_printf (_(" at gnu-indirect-function resolver"));
   12128   1.1  christos       break;
   12129  1.10  christos     case bp_hardware_breakpoint:
   12130  1.10  christos       gdb_printf (_("Hardware assisted breakpoint %d"), number);
   12131   1.1  christos       break;
   12132  1.10  christos     case bp_dprintf:
   12133  1.10  christos       gdb_printf (_("Dprintf %d"), number);
   12134  1.10  christos       break;
   12135  1.10  christos     }
   12136  1.10  christos 
   12137  1.10  christos   say_where ();
   12138  1.10  christos }
   12139  1.10  christos 
   12140  1.10  christos void
   12141   1.1  christos ordinary_breakpoint::print_recreate (struct ui_file *fp) const
   12142  1.10  christos {
   12143   1.1  christos   if (type == bp_breakpoint && disposition == disp_del)
   12144  1.10  christos     gdb_printf (fp, "tbreak");
   12145   1.6  christos   else if (type == bp_breakpoint)
   12146   1.6  christos     gdb_printf (fp, "break");
   12147   1.6  christos   else if (type == bp_hardware_breakpoint
   12148  1.11  christos 	   && disposition == disp_del)
   12149  1.10  christos     gdb_printf (fp, "thbreak");
   12150   1.6  christos   else if (type == bp_hardware_breakpoint)
   12151  1.10  christos     gdb_printf (fp, "hbreak");
   12152   1.1  christos   else
   12153   1.1  christos     internal_error (_("unhandled breakpoint type %d"), (int) type);
   12154  1.10  christos 
   12155  1.10  christos   gdb_printf (fp, " %s", locspec->to_string ());
   12156  1.10  christos 
   12157   1.1  christos   /* Print out extra_string if this breakpoint is pending.  It might
   12158  1.10  christos      contain, for example, conditions that were set by the user.  */
   12159  1.10  christos   if (!this->has_locations () && extra_string != NULL)
   12160  1.10  christos     gdb_printf (fp, " %s", extra_string.get ());
   12161  1.10  christos 
   12162  1.10  christos   print_recreate_thread (fp);
   12163  1.10  christos }
   12164  1.10  christos 
   12165  1.10  christos std::vector<symtab_and_line>
   12166   1.1  christos code_breakpoint::decode_location_spec (location_spec *locspec,
   12167  1.10  christos 				       program_space *search_pspace)
   12168  1.10  christos {
   12169   1.1  christos   if (locspec->type () == PROBE_LOCATION_SPEC)
   12170  1.10  christos     return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
   12171  1.10  christos 
   12172  1.10  christos   struct linespec_result canonical;
   12173  1.10  christos 
   12174  1.10  christos   decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
   12175  1.10  christos 		    NULL, 0, &canonical, multiple_symbols_all,
   12176   1.1  christos 		    filter.get ());
   12177   1.1  christos 
   12178   1.1  christos   /* We should get 0 or 1 resulting SALs.  */
   12179   1.1  christos   gdb_assert (canonical.lsals.size () < 2);
   12180  1.10  christos 
   12181  1.10  christos   if (!canonical.lsals.empty ())
   12182   1.1  christos     {
   12183  1.10  christos       const linespec_sals &lsal = canonical.lsals[0];
   12184   1.1  christos       return std::move (lsal.sals);
   12185   1.1  christos     }
   12186   1.1  christos   return {};
   12187   1.1  christos }
   12188   1.1  christos 
   12189   1.1  christos /* Virtual table for internal breakpoints.  */
   12190   1.1  christos 
   12191  1.10  christos void
   12192   1.1  christos internal_breakpoint::re_set ()
   12193   1.1  christos {
   12194   1.1  christos   switch (type)
   12195  1.10  christos     {
   12196   1.1  christos       /* Delete overlay event and longjmp master breakpoints; they
   12197   1.1  christos 	 will be reset later by breakpoint_re_set.  */
   12198   1.1  christos     case bp_overlay_event:
   12199   1.1  christos     case bp_longjmp_master:
   12200   1.1  christos     case bp_std_terminate_master:
   12201   1.1  christos     case bp_exception_master:
   12202   1.1  christos       delete_breakpoint (this);
   12203   1.1  christos       break;
   12204   1.1  christos 
   12205  1.10  christos       /* This breakpoint is special, it's set up when the inferior
   12206  1.10  christos 	 starts and we really don't want to touch it.  */
   12207   1.1  christos     case bp_shlib_event:
   12208  1.10  christos 
   12209   1.1  christos       /* Like bp_shlib_event, this breakpoint type is special.  Once
   12210   1.1  christos 	 it is set up, we do not want to touch it.  */
   12211   1.1  christos     case bp_thread_event:
   12212   1.1  christos       break;
   12213   1.1  christos     }
   12214  1.11  christos }
   12215  1.11  christos 
   12216   1.1  christos void
   12217   1.1  christos internal_breakpoint::check_status (bpstat *bs)
   12218  1.11  christos {
   12219   1.1  christos   if (type == bp_shlib_event)
   12220   1.1  christos     {
   12221  1.10  christos       /* If requested, stop when the dynamic linker notifies GDB of
   12222  1.10  christos 	 events.  This allows the user to get control and place
   12223   1.1  christos 	 breakpoints in initializer routines for dynamically loaded
   12224  1.10  christos 	 objects (among other things).  */
   12225   1.1  christos       bs->stop = stop_on_solib_events != 0;
   12226   1.1  christos       bs->print = stop_on_solib_events != 0;
   12227   1.1  christos     }
   12228   1.1  christos   else
   12229   1.1  christos     bs->stop = false;
   12230  1.10  christos }
   12231   1.1  christos 
   12232   1.1  christos enum print_stop_action
   12233   1.1  christos internal_breakpoint::print_it (const bpstat *bs) const
   12234   1.1  christos {
   12235   1.1  christos   switch (type)
   12236  1.10  christos     {
   12237   1.1  christos     case bp_shlib_event:
   12238   1.1  christos       /* Did we stop because the user set the stop_on_solib_events
   12239   1.1  christos 	 variable?  (If so, we report this as a generic, "Stopped due
   12240   1.1  christos 	 to shlib event" message.) */
   12241  1.10  christos       print_solib_event (false);
   12242   1.1  christos       break;
   12243   1.1  christos 
   12244   1.1  christos     case bp_thread_event:
   12245   1.1  christos       /* Not sure how we will get here.
   12246  1.10  christos 	 GDB should not stop for these breakpoints.  */
   12247   1.1  christos       gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
   12248   1.1  christos       break;
   12249   1.1  christos 
   12250   1.1  christos     case bp_overlay_event:
   12251  1.10  christos       /* By analogy with the thread event, GDB should not stop for these.  */
   12252  1.10  christos       gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
   12253   1.1  christos       break;
   12254   1.1  christos 
   12255   1.1  christos     case bp_longjmp_master:
   12256   1.1  christos       /* These should never be enabled.  */
   12257  1.10  christos       gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
   12258  1.10  christos       break;
   12259   1.1  christos 
   12260   1.1  christos     case bp_std_terminate_master:
   12261   1.1  christos       /* These should never be enabled.  */
   12262   1.1  christos       gdb_printf (_("std::terminate Master Breakpoint: "
   12263   1.1  christos 		    "gdb should not stop!\n"));
   12264   1.1  christos       break;
   12265  1.10  christos 
   12266  1.10  christos     case bp_exception_master:
   12267   1.1  christos       /* These should never be enabled.  */
   12268   1.1  christos       gdb_printf (_("Exception Master Breakpoint: "
   12269   1.1  christos 		    "gdb should not stop!\n"));
   12270   1.1  christos       break;
   12271   1.1  christos     }
   12272   1.1  christos 
   12273  1.10  christos   return PRINT_NOTHING;
   12274  1.10  christos }
   12275   1.1  christos 
   12276   1.1  christos void
   12277   1.1  christos internal_breakpoint::print_mention () const
   12278   1.1  christos {
   12279   1.1  christos   /* Nothing to mention.  These breakpoints are internal.  */
   12280   1.1  christos }
   12281   1.1  christos 
   12282  1.10  christos /* Virtual table for momentary breakpoints  */
   12283  1.10  christos 
   12284   1.1  christos void
   12285   1.1  christos momentary_breakpoint::re_set ()
   12286   1.1  christos {
   12287   1.1  christos   /* Keep temporary breakpoints, which can be encountered when we step
   12288  1.10  christos      over a dlopen call and solib_add is resetting the breakpoints.
   12289  1.10  christos      Otherwise these should have been blown away via the cleanup chain
   12290   1.1  christos      or by breakpoint_init_inferior when we rerun the executable.  */
   12291   1.1  christos }
   12292   1.1  christos 
   12293   1.1  christos void
   12294  1.10  christos momentary_breakpoint::check_status (bpstat *bs)
   12295  1.10  christos {
   12296   1.1  christos   /* Nothing.  The point of these breakpoints is causing a stop.  */
   12297   1.1  christos }
   12298   1.1  christos 
   12299   1.1  christos enum print_stop_action
   12300   1.1  christos momentary_breakpoint::print_it (const bpstat *bs) const
   12301   1.1  christos {
   12302   1.1  christos   return PRINT_UNKNOWN;
   12303   1.1  christos }
   12304   1.1  christos 
   12305   1.8  christos void
   12306   1.1  christos momentary_breakpoint::print_mention () const
   12307   1.8  christos {
   12308   1.1  christos   /* Nothing to mention.  These breakpoints are internal.  */
   12309   1.8  christos }
   12310   1.1  christos 
   12311   1.1  christos /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
   12312   1.1  christos 
   12313  1.10  christos    It gets cleared already on the removal of the first one of such placed
   12314  1.10  christos    breakpoints.  This is OK as they get all removed altogether.  */
   12315  1.10  christos 
   12316   1.1  christos longjmp_breakpoint::~longjmp_breakpoint ()
   12317   1.1  christos {
   12318   1.1  christos   thread_info *tp = find_thread_global_id (this->thread);
   12319   1.1  christos 
   12320  1.10  christos   if (tp != NULL)
   12321  1.10  christos     tp->initiating_frame = null_frame_id;
   12322   1.8  christos }
   12323   1.1  christos 
   12324   1.1  christos static void
   12325   1.8  christos bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
   12326  1.10  christos 					   struct linespec_result *canonical)
   12327  1.10  christos 
   12328  1.10  christos {
   12329   1.1  christos   struct linespec_sals lsal;
   12330  1.10  christos 
   12331  1.10  christos   lsal.sals = parse_probes (locspec, NULL, canonical);
   12332   1.8  christos   lsal.canonical = xstrdup (canonical->locspec->to_string ());
   12333   1.1  christos   canonical->lsals.push_back (std::move (lsal));
   12334   1.8  christos }
   12335   1.1  christos 
   12336   1.1  christos static std::vector<symtab_and_line>
   12337  1.10  christos bkpt_probe_decode_location_spec (struct breakpoint *b,
   12338  1.10  christos 				 location_spec *locspec,
   12339  1.10  christos 				 program_space *search_pspace)
   12340  1.10  christos {
   12341   1.1  christos   std::vector<symtab_and_line> sals
   12342   1.1  christos     = parse_probes (locspec, search_pspace, NULL);
   12343   1.1  christos   if (sals.empty ())
   12344   1.1  christos     error (_("probe not found"));
   12345   1.1  christos   return sals;
   12346   1.1  christos }
   12347  1.10  christos 
   12348  1.10  christos int
   12349   1.1  christos tracepoint::breakpoint_hit (const struct bp_location *bl,
   12350  1.10  christos 			    const address_space *aspace, CORE_ADDR bp_addr,
   12351   1.1  christos 			    const target_waitstatus &ws)
   12352  1.10  christos {
   12353  1.10  christos   /* By definition, the inferior does not report stops at
   12354   1.1  christos      tracepoints.  */
   12355   1.9  christos   return 0;
   12356   1.9  christos }
   12357  1.10  christos 
   12358   1.1  christos void
   12359   1.1  christos tracepoint::print_one_detail (struct ui_out *uiout) const
   12360   1.1  christos {
   12361  1.10  christos   if (!static_trace_marker_id.empty ())
   12362  1.10  christos     {
   12363   1.1  christos       gdb_assert (type == bp_static_tracepoint
   12364   1.7  christos 		  || type == bp_static_marker_tracepoint);
   12365   1.1  christos 
   12366   1.1  christos       uiout->message ("\tmarker id is %pF\n",
   12367  1.10  christos 		      string_field ("static-tracepoint-marker-string-id",
   12368   1.1  christos 				    static_trace_marker_id.c_str ()));
   12369   1.1  christos     }
   12370  1.10  christos }
   12371  1.10  christos 
   12372   1.1  christos void
   12373   1.1  christos tracepoint::print_mention () const
   12374  1.10  christos {
   12375  1.10  christos   if (current_uiout->is_mi_like_p ())
   12376   1.1  christos     return;
   12377   1.1  christos 
   12378  1.10  christos   switch (type)
   12379  1.10  christos     {
   12380  1.10  christos     case bp_tracepoint:
   12381   1.1  christos       gdb_printf (_("Tracepoint"));
   12382   1.1  christos       gdb_printf (_(" %d"), number);
   12383  1.10  christos       break;
   12384   1.1  christos     case bp_fast_tracepoint:
   12385   1.1  christos       gdb_printf (_("Fast tracepoint"));
   12386  1.11  christos       gdb_printf (_(" %d"), number);
   12387   1.1  christos       break;
   12388   1.1  christos     case bp_static_tracepoint:
   12389  1.10  christos     case bp_static_marker_tracepoint:
   12390  1.10  christos       gdb_printf (_("Static tracepoint"));
   12391   1.1  christos       gdb_printf (_(" %d"), number);
   12392  1.10  christos       break;
   12393  1.10  christos     default:
   12394  1.10  christos       internal_error (_("unhandled tracepoint type %d"), (int) type);
   12395  1.10  christos     }
   12396  1.10  christos 
   12397  1.10  christos   say_where ();
   12398  1.10  christos }
   12399   1.1  christos 
   12400  1.10  christos void
   12401   1.1  christos tracepoint::print_recreate (struct ui_file *fp) const
   12402  1.10  christos {
   12403  1.10  christos   if (type == bp_fast_tracepoint)
   12404   1.1  christos     gdb_printf (fp, "ftrace");
   12405  1.10  christos   else if (type == bp_static_tracepoint
   12406  1.10  christos 	   || type == bp_static_marker_tracepoint)
   12407   1.1  christos     gdb_printf (fp, "strace");
   12408   1.1  christos   else if (type == bp_tracepoint)
   12409   1.9  christos     gdb_printf (fp, "trace");
   12410   1.1  christos   else
   12411   1.1  christos     internal_error (_("unhandled tracepoint type %d"), (int) type);
   12412  1.10  christos 
   12413  1.10  christos   gdb_printf (fp, " %s", locspec->to_string ());
   12414  1.10  christos   print_recreate_thread (fp);
   12415   1.1  christos 
   12416   1.1  christos   if (pass_count)
   12417  1.10  christos     gdb_printf (fp, "  passcount %d\n", pass_count);
   12418   1.1  christos }
   12419   1.1  christos 
   12420  1.10  christos /* Virtual table for tracepoints on static probes.  */
   12421  1.10  christos 
   12422   1.1  christos static void
   12423  1.10  christos tracepoint_probe_create_sals_from_location_spec
   12424   1.1  christos   (location_spec *locspec,
   12425   1.1  christos    struct linespec_result *canonical)
   12426   1.1  christos {
   12427   1.1  christos   /* We use the same method for breakpoint on probes.  */
   12428   1.1  christos   bkpt_probe_create_sals_from_location_spec (locspec, canonical);
   12429   1.1  christos }
   12430   1.1  christos 
   12431   1.1  christos void
   12432   1.1  christos dprintf_breakpoint::re_set ()
   12433   1.1  christos {
   12434   1.1  christos   re_set_default ();
   12435  1.11  christos 
   12436   1.1  christos   /* 1 - connect to target 1, that can run breakpoint commands.
   12437   1.1  christos      2 - create a dprintf, which resolves fine.
   12438  1.10  christos      3 - disconnect from target 1
   12439   1.1  christos      4 - connect to target 2, that can NOT run breakpoint commands.
   12440  1.10  christos 
   12441  1.10  christos      After steps #3/#4, you'll want the dprintf command list to
   12442   1.1  christos      be updated, because target 1 and 2 may well return different
   12443  1.10  christos      answers for target_can_run_breakpoint_commands().
   12444  1.10  christos      Given absence of finer grained resetting, we get to do
   12445   1.1  christos      it all the time.  */
   12446   1.1  christos   update_dprintf_command_list (this);
   12447  1.10  christos }
   12448   1.1  christos 
   12449   1.1  christos /* Implement the "print_recreate" method for dprintf.  */
   12450   1.1  christos 
   12451   1.1  christos void
   12452   1.1  christos dprintf_breakpoint::print_recreate (struct ui_file *fp) const
   12453   1.1  christos {
   12454   1.1  christos   gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
   12455  1.10  christos   print_recreate_thread (fp);
   12456  1.10  christos }
   12457   1.1  christos 
   12458   1.1  christos /* Implement the "after_condition_true" method for dprintf.
   12459   1.1  christos 
   12460   1.1  christos    dprintf's are implemented with regular commands in their command
   12461  1.11  christos    list, but we run the commands here instead of before presenting the
   12462   1.1  christos    stop to the user, as dprintf's don't actually cause a stop.  This
   12463   1.1  christos    also makes it so that the commands of multiple dprintfs at the same
   12464   1.1  christos    address are all handled.  */
   12465   1.1  christos 
   12466   1.1  christos void
   12467   1.1  christos dprintf_breakpoint::after_condition_true (struct bpstat *bs)
   12468  1.10  christos {
   12469  1.10  christos   /* dprintf's never cause a stop.  This wasn't set in the
   12470  1.10  christos      check_status hook instead because that would make the dprintf's
   12471   1.1  christos      condition not be evaluated.  */
   12472   1.1  christos   bs->stop = false;
   12473   1.1  christos 
   12474   1.1  christos   /* Run the command list here.  Take ownership of it instead of
   12475   1.1  christos      copying.  We never want these commands to run later in
   12476   1.1  christos      bpstat_do_actions, if a breakpoint that causes a stop happens to
   12477  1.10  christos      be set at same address as this dprintf, or even if running the
   12478  1.10  christos      commands here throws.  */
   12479   1.1  christos   counted_command_line cmds = std::move (bs->commands);
   12480   1.1  christos   gdb_assert (cmds != nullptr);
   12481   1.6  christos   execute_control_commands (cmds.get (), 0);
   12482   1.1  christos }
   12483  1.11  christos 
   12484   1.6  christos /* The breakpoint_ops structure to be used on static tracepoints with
   12485   1.1  christos    markers (`-m').  */
   12486   1.8  christos 
   12487   1.8  christos static void
   12488  1.10  christos strace_marker_create_sals_from_location_spec (location_spec *locspec,
   12489  1.10  christos 					      struct linespec_result *canonical)
   12490   1.1  christos {
   12491  1.10  christos   struct linespec_sals lsal;
   12492   1.8  christos   const char *arg_start, *arg;
   12493   1.1  christos 
   12494   1.1  christos   arg = arg_start = as_linespec_location_spec (locspec)->spec_string.get ();
   12495   1.1  christos   lsal.sals = decode_static_tracepoint_spec (&arg);
   12496   1.1  christos 
   12497   1.1  christos   std::string str (arg_start, arg - arg_start);
   12498   1.8  christos   const char *ptr = str.c_str ();
   12499   1.8  christos   canonical->locspec
   12500   1.1  christos     = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
   12501   1.1  christos 
   12502   1.1  christos   lsal.canonical = xstrdup (canonical->locspec->to_string ());
   12503  1.11  christos   canonical->lsals.push_back (std::move (lsal));
   12504  1.11  christos }
   12505   1.1  christos 
   12506   1.1  christos static void
   12507   1.1  christos strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
   12508   1.8  christos 				      struct linespec_result *canonical,
   12509   1.1  christos 				      gdb::unique_xmalloc_ptr<char> cond_string,
   12510   1.1  christos 				      gdb::unique_xmalloc_ptr<char> extra_string,
   12511   1.1  christos 				      enum bptype type_wanted,
   12512   1.1  christos 				      enum bpdisp disposition,
   12513   1.1  christos 				      int thread,
   12514   1.1  christos 				      int task, int inferior,
   12515   1.1  christos 				      int ignore_count,
   12516   1.1  christos 				      int from_tty, int enabled,
   12517   1.8  christos 				      int internal, unsigned flags)
   12518   1.1  christos {
   12519  1.10  christos   const linespec_sals &lsal = canonical->lsals[0];
   12520  1.10  christos 
   12521  1.10  christos   /* If the user is creating a static tracepoint by marker id
   12522  1.10  christos      (strace -m MARKER_ID), then store the sals index, so that
   12523  1.10  christos      breakpoint_re_set can try to match up which of the newly
   12524  1.10  christos      found markers corresponds to this one, and, don't try to
   12525  1.10  christos      expand multiple locations for each sal, given than SALS
   12526  1.10  christos      already should contain all sals for MARKER_ID.  */
   12527  1.10  christos 
   12528  1.10  christos   for (size_t i = 0; i < lsal.sals.size (); i++)
   12529  1.10  christos     {
   12530  1.11  christos       location_spec_up locspec = canonical->locspec->clone ();
   12531  1.10  christos 
   12532  1.10  christos       std::unique_ptr<tracepoint> tp
   12533   1.1  christos 	(new tracepoint (gdbarch,
   12534   1.1  christos 			 type_wanted,
   12535   1.1  christos 			 lsal.sals[i],
   12536   1.1  christos 			 std::move (locspec),
   12537   1.1  christos 			 NULL,
   12538   1.1  christos 			 std::move (cond_string),
   12539   1.1  christos 			 std::move (extra_string),
   12540   1.1  christos 			 disposition,
   12541   1.1  christos 			 thread, task, inferior, ignore_count,
   12542   1.8  christos 			 from_tty, enabled, flags,
   12543   1.1  christos 			 canonical->special_display));
   12544   1.1  christos 
   12545   1.1  christos       /* Given that its possible to have multiple markers with
   12546  1.10  christos 	 the same string id, if the user is creating a static
   12547  1.10  christos 	 tracepoint by marker id ("strace -m MARKER_ID"), then
   12548  1.10  christos 	 store the sals index, so that breakpoint_re_set can
   12549   1.1  christos 	 try to match up which of the newly found markers
   12550  1.11  christos 	 corresponds to this one  */
   12551   1.1  christos       tp->static_trace_marker_id_idx = i;
   12552   1.8  christos 
   12553  1.10  christos       install_breakpoint (internal, std::move (tp), 0);
   12554   1.1  christos     }
   12555  1.10  christos }
   12556   1.8  christos 
   12557   1.8  christos std::vector<symtab_and_line>
   12558   1.1  christos static_marker_tracepoint::decode_location_spec (location_spec *locspec,
   12559   1.1  christos 						program_space *search_pspace)
   12560  1.10  christos {
   12561   1.1  christos   const char *s = as_linespec_location_spec (locspec)->spec_string.get ();
   12562   1.1  christos 
   12563  1.10  christos   std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
   12564  1.10  christos   if (sals.size () > static_trace_marker_id_idx)
   12565  1.10  christos     {
   12566  1.10  christos       sals[0] = sals[static_trace_marker_id_idx];
   12567  1.10  christos       sals.resize (1);
   12568  1.10  christos       return sals;
   12569   1.1  christos     }
   12570  1.10  christos   else
   12571   1.1  christos     error (_("marker %s not found"), static_trace_marker_id.c_str ());
   12572   1.1  christos }
   12573  1.10  christos 
   12574   1.1  christos /* Static tracepoints with marker (`-m').  */
   12575   1.1  christos static struct breakpoint_ops strace_marker_breakpoint_ops =
   12576  1.11  christos {
   12577  1.11  christos   strace_marker_create_sals_from_location_spec,
   12578  1.11  christos   strace_marker_create_breakpoints_sal,
   12579  1.11  christos };
   12580  1.11  christos 
   12581  1.11  christos static bool
   12582  1.11  christos strace_marker_p (struct breakpoint *b)
   12583  1.11  christos {
   12584  1.11  christos   return b->type == bp_static_marker_tracepoint;
   12585   1.1  christos }
   12586   1.1  christos 
   12587   1.1  christos /* Notify interpreters and observers that breakpoint B was deleted.  */
   12588   1.1  christos 
   12589   1.1  christos static void
   12590   1.1  christos notify_breakpoint_deleted (breakpoint *b)
   12591   1.1  christos {
   12592   1.1  christos   interps_notify_breakpoint_deleted (b);
   12593   1.1  christos   gdb::observers::breakpoint_deleted.notify (b);
   12594   1.1  christos }
   12595   1.1  christos 
   12596   1.1  christos /* Delete a breakpoint and clean up all traces of it in the data
   12597   1.1  christos    structures.  */
   12598   1.1  christos 
   12599   1.1  christos void
   12600   1.1  christos delete_breakpoint (struct breakpoint *bpt)
   12601   1.1  christos {
   12602   1.1  christos   gdb_assert (bpt != NULL);
   12603   1.1  christos 
   12604   1.1  christos   /* Has this bp already been deleted?  This can happen because
   12605   1.1  christos      multiple lists can hold pointers to bp's.  bpstat lists are
   12606   1.1  christos      especial culprits.
   12607   1.1  christos 
   12608   1.1  christos      One example of this happening is a watchpoint's scope bp.  When
   12609   1.1  christos      the scope bp triggers, we notice that the watchpoint is out of
   12610   1.1  christos      scope, and delete it.  We also delete its scope bp.  But the
   12611   1.1  christos      scope bp is marked "auto-deleting", and is already on a bpstat.
   12612   1.1  christos      That bpstat is then checked for auto-deleting bp's, which are
   12613   1.1  christos      deleted.
   12614   1.1  christos 
   12615   1.1  christos      A real solution to this problem might involve reference counts in
   12616   1.1  christos      bp's, and/or giving them pointers back to their referencing
   12617   1.1  christos      bpstat's, and teaching delete_breakpoint to only free a bp's
   12618   1.1  christos      storage when no more references were extent.  A cheaper bandaid
   12619   1.1  christos      was chosen.  */
   12620  1.11  christos   if (bpt->type == bp_none)
   12621   1.1  christos     return;
   12622  1.11  christos 
   12623   1.1  christos   /* At least avoid this stale reference until the reference counting
   12624   1.1  christos      of breakpoints gets resolved.  */
   12625   1.1  christos   if (bpt->related_breakpoint != bpt)
   12626   1.1  christos     {
   12627   1.1  christos       struct breakpoint *related;
   12628   1.1  christos       struct watchpoint *w;
   12629   1.1  christos 
   12630   1.1  christos       if (bpt->type == bp_watchpoint_scope)
   12631   1.1  christos 	w = gdb::checked_static_cast<watchpoint *> (bpt->related_breakpoint);
   12632   1.1  christos       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
   12633   1.1  christos 	w = gdb::checked_static_cast<watchpoint *> (bpt);
   12634   1.1  christos       else
   12635   1.1  christos 	w = NULL;
   12636   1.1  christos       if (w != NULL)
   12637   1.1  christos 	watchpoint_del_at_next_stop (w);
   12638   1.1  christos 
   12639   1.1  christos       /* Unlink bpt from the bpt->related_breakpoint ring.  */
   12640  1.11  christos       for (related = bpt; related->related_breakpoint != bpt;
   12641   1.1  christos 	   related = related->related_breakpoint);
   12642  1.11  christos       related->related_breakpoint = bpt->related_breakpoint;
   12643   1.1  christos       bpt->related_breakpoint = bpt;
   12644   1.1  christos     }
   12645   1.1  christos 
   12646   1.1  christos   /* watch_command_1 creates a watchpoint but only sets its number if
   12647   1.1  christos      update_watchpoint succeeds in creating its bp_locations.  If there's
   12648   1.1  christos      a problem in that process, we'll be asked to delete the half-created
   12649   1.1  christos      watchpoint.  In that case, don't announce the deletion.  */
   12650   1.1  christos   if (bpt->number)
   12651   1.1  christos     notify_breakpoint_deleted (bpt);
   12652   1.1  christos 
   12653   1.1  christos   breakpoint_chain.erase (breakpoint_chain.iterator_to (*bpt));
   12654   1.1  christos 
   12655   1.1  christos   /* Be sure no bpstat's are pointing at the breakpoint after it's
   12656   1.1  christos      been freed.  */
   12657   1.1  christos   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
   12658   1.1  christos      in all threads for now.  Note that we cannot just remove bpstats
   12659   1.1  christos      pointing at bpt from the stop_bpstat list entirely, as breakpoint
   12660   1.1  christos      commands are associated with the bpstat; if we remove it here,
   12661  1.11  christos      then the later call to bpstat_do_actions (&stop_bpstat); in
   12662  1.11  christos      event-top.c won't do anything, and temporary breakpoints with
   12663  1.11  christos      commands won't work.  */
   12664  1.11  christos 
   12665  1.11  christos   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
   12666   1.3  christos 
   12667   1.1  christos   /* Now that breakpoint is removed from breakpoint list, update the
   12668   1.1  christos      global location list.  This will remove locations that used to
   12669   1.1  christos      belong to this breakpoint.  Do this before freeing the breakpoint
   12670   1.1  christos      itself, since remove_breakpoint looks at location's owner.  It
   12671   1.7  christos      might be better design to have location completely
   12672   1.1  christos      self-contained, but it's not the case now.
   12673   1.1  christos 
   12674   1.1  christos      Clear the location linked list first, otherwise, the intrusive_list
   12675   1.1  christos      destructor accesses the locations after they are freed.  */
   12676   1.1  christos   bpt->clear_locations ();
   12677   1.1  christos   update_global_location_list (UGLL_DONT_INSERT);
   12678   1.1  christos 
   12679   1.8  christos   /* On the chance that someone will soon try again to delete this
   12680   1.1  christos      same bp, we mark it as deleted before freeing its storage.  */
   12681   1.1  christos   bpt->type = bp_none;
   12682   1.1  christos   delete bpt;
   12683   1.1  christos }
   12684   1.1  christos 
   12685   1.1  christos /* Iterator function to call a user-provided callback function once
   12686   1.1  christos    for each of B and its related breakpoints.  */
   12687   1.1  christos 
   12688   1.1  christos static void
   12689   1.1  christos iterate_over_related_breakpoints (struct breakpoint *b,
   12690   1.1  christos 				  gdb::function_view<void (breakpoint *)> function)
   12691   1.1  christos {
   12692   1.1  christos   struct breakpoint *related;
   12693   1.1  christos 
   12694   1.8  christos   related = b;
   12695   1.1  christos   do
   12696   1.1  christos     {
   12697   1.1  christos       struct breakpoint *next;
   12698   1.1  christos 
   12699   1.1  christos       /* FUNCTION may delete RELATED.  */
   12700   1.1  christos       next = related->related_breakpoint;
   12701   1.1  christos 
   12702   1.8  christos       if (next == related)
   12703   1.1  christos 	{
   12704   1.1  christos 	  /* RELATED is the last ring entry.  */
   12705   1.1  christos 	  function (related);
   12706   1.1  christos 
   12707   1.1  christos 	  /* FUNCTION may have deleted it, so we'd never reach back to
   12708   1.1  christos 	     B.  There's nothing left to do anyway, so just break
   12709   1.1  christos 	     out.  */
   12710   1.8  christos 	  break;
   12711   1.1  christos 	}
   12712   1.1  christos       else
   12713   1.1  christos 	function (related);
   12714   1.1  christos 
   12715   1.1  christos       related = next;
   12716   1.1  christos     }
   12717   1.1  christos   while (related != b);
   12718  1.11  christos }
   12719  1.11  christos 
   12720  1.11  christos static void
   12721  1.11  christos delete_command (const char *arg, int from_tty)
   12722  1.11  christos {
   12723   1.1  christos   dont_repeat ();
   12724   1.1  christos 
   12725   1.1  christos   if (arg == 0)
   12726   1.1  christos     {
   12727   1.1  christos       int breaks_to_delete = 0;
   12728   1.1  christos 
   12729   1.1  christos       /* Delete all breakpoints, watchpoints, tracepoints, and catchpoints
   12730  1.11  christos 	 if no argument.  Do not delete internal breakpoints, these have to
   12731  1.11  christos 	 be deleted with an explicit breakpoint number argument.  */
   12732  1.11  christos       for (breakpoint &b : all_breakpoints ())
   12733  1.11  christos 	if (user_breakpoint_p (&b))
   12734  1.11  christos 	  {
   12735  1.11  christos 	    breaks_to_delete = 1;
   12736   1.1  christos 	    break;
   12737   1.1  christos 	  }
   12738   1.8  christos 
   12739   1.8  christos       /* Ask user only if there are some breakpoints to delete.  */
   12740   1.8  christos       if (!from_tty
   12741   1.8  christos 	  || (breaks_to_delete
   12742   1.8  christos 	      && query (_("Delete all breakpoints, watchpoints, tracepoints, "
   12743   1.1  christos 			  "and catchpoints? "))))
   12744   1.1  christos 	for (breakpoint &b : all_breakpoints_safe ())
   12745   1.6  christos 	  if (user_breakpoint_p (&b))
   12746   1.6  christos 	    delete_breakpoint (&b);
   12747   1.6  christos     }
   12748   1.6  christos   else
   12749  1.10  christos     map_breakpoint_numbers
   12750   1.6  christos       (arg, [&] (breakpoint *br)
   12751   1.1  christos        {
   12752  1.11  christos 	 iterate_over_related_breakpoints (br, delete_breakpoint);
   12753   1.6  christos        });
   12754  1.11  christos }
   12755  1.11  christos 
   12756  1.11  christos /* Return true if all locations of B bound to PSPACE are pending.  If
   12757  1.10  christos    PSPACE is NULL, all locations of all program spaces are
   12758  1.10  christos    considered.  */
   12759   1.1  christos 
   12760   1.1  christos static bool
   12761   1.1  christos all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
   12762  1.11  christos {
   12763   1.1  christos   for (bp_location &loc : b->locations ())
   12764   1.1  christos     if ((pspace == NULL
   12765  1.10  christos 	 || loc.pspace == pspace)
   12766  1.11  christos 	&& !loc.shlib_disabled
   12767   1.1  christos 	&& !loc.pspace->executing_startup)
   12768  1.10  christos       return false;
   12769  1.10  christos   return true;
   12770   1.1  christos }
   12771  1.11  christos 
   12772   1.1  christos /* Subroutine of update_breakpoint_locations to simplify it.
   12773   1.1  christos    Return true if multiple fns in list LOCS have the same name.
   12774  1.11  christos    Null names are ignored.  */
   12775   1.1  christos 
   12776   1.1  christos static bool
   12777   1.1  christos ambiguous_names_p (const bp_location_range &locs)
   12778   1.1  christos {
   12779   1.1  christos   htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
   12780  1.10  christos 				   xcalloc, xfree));
   12781   1.1  christos 
   12782   1.1  christos   for (const bp_location &l : locs)
   12783   1.1  christos     {
   12784   1.1  christos       const char **slot;
   12785  1.10  christos       const char *name = l.function_name.get ();
   12786   1.1  christos 
   12787   1.1  christos       /* Allow for some names to be NULL, ignore them.  */
   12788   1.1  christos       if (name == NULL)
   12789  1.10  christos 	continue;
   12790   1.1  christos 
   12791   1.1  christos       slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
   12792   1.1  christos 					     INSERT);
   12793   1.1  christos       /* NOTE: We can assume slot != NULL here because xcalloc never
   12794   1.1  christos 	 returns NULL.  */
   12795   1.1  christos       if (*slot != NULL)
   12796   1.1  christos 	return true;
   12797   1.1  christos       *slot = name;
   12798   1.1  christos     }
   12799   1.1  christos 
   12800   1.1  christos   return false;
   12801   1.1  christos }
   12802   1.1  christos 
   12803   1.1  christos /* When symbols change, it probably means the sources changed as well,
   12804   1.1  christos    and it might mean the static tracepoint markers are no longer at
   12805   1.1  christos    the same address or line numbers they used to be at last we
   12806   1.1  christos    checked.  Losing your static tracepoints whenever you rebuild is
   12807   1.1  christos    undesirable.  This function tries to resync/rematch gdb static
   12808   1.1  christos    tracepoints with the markers on the target, for static tracepoints
   12809   1.1  christos    that have not been set by marker id.  Static tracepoint that have
   12810   1.1  christos    been set by marker id are reset by marker id in breakpoint_re_set.
   12811   1.1  christos    The heuristic is:
   12812   1.1  christos 
   12813   1.1  christos    1) For a tracepoint set at a specific address, look for a marker at
   12814   1.1  christos    the old PC.  If one is found there, assume to be the same marker.
   12815   1.1  christos    If the name / string id of the marker found is different from the
   12816   1.1  christos    previous known name, assume that means the user renamed the marker
   12817   1.1  christos    in the sources, and output a warning.
   12818   1.1  christos 
   12819   1.1  christos    2) For a tracepoint set at a given line number, look for a marker
   12820   1.1  christos    at the new address of the old line number.  If one is found there,
   12821   1.1  christos    assume to be the same marker.  If the name / string id of the
   12822   1.1  christos    marker found is different from the previous known name, assume that
   12823   1.1  christos    means the user renamed the marker in the sources, and output a
   12824   1.1  christos    warning.
   12825   1.1  christos 
   12826  1.11  christos    3) If a marker is no longer found at the same address or line, it
   12827   1.1  christos    may mean the marker no longer exists.  But it may also just mean
   12828   1.1  christos    the code changed a bit.  Maybe the user added a few lines of code
   12829   1.1  christos    that made the marker move up or down (in line number terms).  Ask
   12830   1.1  christos    the target for info about the marker with the string id as we knew
   12831   1.1  christos    it.  If found, update line number and address in the matching
   12832   1.1  christos    static tracepoint.  This will get confused if there's more than one
   12833   1.1  christos    marker with the same ID (possible in UST, although unadvised
   12834   1.1  christos    precisely because it confuses tools).  */
   12835   1.1  christos 
   12836   1.1  christos static struct symtab_and_line
   12837   1.8  christos update_static_tracepoint (tracepoint *tp, struct symtab_and_line sal)
   12838   1.1  christos {
   12839  1.11  christos   struct static_tracepoint_marker marker;
   12840   1.8  christos   CORE_ADDR pc;
   12841   1.1  christos 
   12842   1.8  christos   pc = sal.pc;
   12843   1.1  christos   if (sal.line)
   12844   1.1  christos     find_line_pc (sal.symtab, sal.line, &pc);
   12845   1.1  christos 
   12846   1.1  christos   if (target_static_tracepoint_marker_at (pc, &marker))
   12847   1.1  christos     {
   12848   1.1  christos       if (tp->static_trace_marker_id != marker.str_id)
   12849   1.1  christos 	warning (_("static tracepoint %d changed probed marker from %s to %s"),
   12850   1.1  christos 		 tp->number, tp->static_trace_marker_id.c_str (),
   12851   1.1  christos 		 marker.str_id.c_str ());
   12852   1.8  christos 
   12853   1.1  christos       tp->static_trace_marker_id = std::move (marker.str_id);
   12854   1.8  christos 
   12855   1.8  christos       return sal;
   12856   1.8  christos     }
   12857   1.1  christos 
   12858   1.8  christos   /* Old marker wasn't found on target at lineno.  Try looking it up
   12859   1.1  christos      by string ID.  */
   12860   1.1  christos   if (!sal.explicit_pc
   12861   1.1  christos       && sal.line != 0
   12862   1.1  christos       && sal.symtab != NULL
   12863   1.1  christos       && !tp->static_trace_marker_id.empty ())
   12864   1.8  christos     {
   12865   1.1  christos       std::vector<static_tracepoint_marker> markers
   12866   1.8  christos 	= target_static_tracepoint_markers_by_strid
   12867   1.1  christos 	    (tp->static_trace_marker_id.c_str ());
   12868   1.1  christos 
   12869   1.1  christos       if (!markers.empty ())
   12870  1.11  christos 	{
   12871   1.1  christos 	  struct symbol *sym;
   12872   1.8  christos 	  struct static_tracepoint_marker *tpmarker;
   12873   1.1  christos 	  struct ui_out *uiout = current_uiout;
   12874   1.7  christos 
   12875   1.1  christos 	  tpmarker = &markers[0];
   12876   1.1  christos 
   12877   1.9  christos 	  tp->static_trace_marker_id = std::move (tpmarker->str_id);
   12878   1.9  christos 
   12879   1.7  christos 	  warning (_("marker for static tracepoint %d (%s) not "
   12880   1.1  christos 		     "found at previous line number"),
   12881   1.7  christos 		   tp->number, tp->static_trace_marker_id.c_str ());
   12882   1.8  christos 
   12883   1.9  christos 	  symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
   12884   1.7  christos 	  sym = find_pc_sect_function (tpmarker->address, NULL);
   12885   1.1  christos 	  uiout->text ("Now in ");
   12886   1.7  christos 	  if (sym)
   12887   1.1  christos 	    {
   12888   1.1  christos 	      uiout->field_string ("func", sym->print_name (),
   12889   1.1  christos 				   function_name_style.style ());
   12890   1.7  christos 	      uiout->text (" at ");
   12891   1.1  christos 	    }
   12892   1.1  christos 	  uiout->field_string ("file",
   12893   1.9  christos 			       symtab_to_filename_for_display (sal2.symtab),
   12894   1.7  christos 			       file_name_style.style ());
   12895   1.1  christos 	  uiout->text (":");
   12896  1.11  christos 
   12897  1.11  christos 	  if (uiout->is_mi_like_p ())
   12898   1.1  christos 	    {
   12899  1.10  christos 	      const char *fullname = symtab_to_fullname (sal2.symtab);
   12900  1.11  christos 
   12901  1.11  christos 	      uiout->field_string ("fullname", fullname);
   12902  1.11  christos 	    }
   12903  1.10  christos 
   12904  1.10  christos 	  uiout->field_signed ("line", sal2.line);
   12905  1.11  christos 	  uiout->text ("\n");
   12906   1.1  christos 
   12907   1.1  christos 	  tp->first_loc ().line_number = sal2.line;
   12908   1.1  christos 	  tp->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL;
   12909   1.1  christos 
   12910   1.1  christos 	  std::unique_ptr<explicit_location_spec> els
   12911   1.1  christos 	    (new explicit_location_spec
   12912   1.1  christos 	     (symtab_to_filename_for_display (sal2.symtab)));
   12913   1.1  christos 	  els->line_offset.offset = tp->first_loc ().line_number;
   12914  1.11  christos 	  els->line_offset.sign = LINE_OFFSET_NONE;
   12915   1.1  christos 
   12916   1.1  christos 	  tp->locspec = std::move (els);
   12917  1.10  christos 
   12918  1.11  christos 	  /* Might be nice to check if function changed, and warn if
   12919   1.1  christos 	     so.  */
   12920  1.11  christos 	}
   12921  1.11  christos     }
   12922  1.11  christos   return sal;
   12923  1.11  christos }
   12924   1.1  christos 
   12925  1.11  christos /* Returns true iff location lists A and B are sufficiently same that
   12926  1.10  christos    we don't need to report breakpoint as changed.  */
   12927   1.1  christos 
   12928  1.11  christos static bool
   12929  1.10  christos locations_are_equal (const bp_location_list &a, const bp_location_range &b)
   12930   1.1  christos {
   12931  1.11  christos   auto a_iter = a.begin ();
   12932  1.10  christos   auto b_iter = b.begin ();
   12933  1.10  christos 
   12934  1.11  christos   for (; a_iter != a.end () && b_iter != b.end (); ++a_iter, ++b_iter)
   12935  1.10  christos     {
   12936   1.1  christos       if (a_iter->address != b_iter->address)
   12937   1.1  christos 	return false;
   12938  1.11  christos 
   12939   1.1  christos       if (a_iter->shlib_disabled != b_iter->shlib_disabled)
   12940   1.1  christos 	return false;
   12941  1.11  christos 
   12942   1.6  christos       if (a_iter->enabled != b_iter->enabled)
   12943  1.11  christos 	return false;
   12944  1.11  christos 
   12945   1.6  christos       if (a_iter->disabled_by_cond != b_iter->disabled_by_cond)
   12946   1.6  christos 	return false;
   12947  1.11  christos     }
   12948   1.6  christos 
   12949  1.11  christos   return (a_iter == a.end ()) == (b_iter == b.end ());
   12950   1.6  christos }
   12951  1.11  christos 
   12952   1.6  christos /* See breakpoint.h.  */
   12953  1.11  christos 
   12954   1.6  christos bp_location_list
   12955  1.11  christos breakpoint::steal_locations (program_space *pspace)
   12956  1.11  christos {
   12957  1.11  christos   if (pspace == NULL)
   12958   1.6  christos     return std::move (m_locations);
   12959   1.6  christos 
   12960  1.11  christos   bp_location_list ret;
   12961   1.6  christos 
   12962   1.6  christos   for (auto it = m_locations.begin (); it != m_locations.end (); )
   12963  1.11  christos     {
   12964   1.6  christos       if (it->pspace == pspace)
   12965   1.6  christos 	{
   12966   1.6  christos 	  bp_location &loc = *it;
   12967   1.6  christos 	  it = m_locations.erase (it);
   12968   1.6  christos 	  ret.push_back (loc);
   12969   1.6  christos 	}
   12970   1.6  christos       else
   12971   1.1  christos 	++it;
   12972   1.1  christos     }
   12973  1.10  christos 
   12974   1.6  christos   return ret;
   12975   1.8  christos }
   12976   1.8  christos 
   12977   1.1  christos /* Create new breakpoint locations for B (a hardware or software
   12978   1.8  christos    breakpoint) based on SALS and SALS_END.  If SALS_END.NELTS is not
   12979   1.1  christos    zero, then B is a ranged breakpoint.  Only recreates locations for
   12980   1.1  christos    FILTER_PSPACE.  Locations of other program spaces are left
   12981   1.1  christos    untouched.  */
   12982   1.1  christos 
   12983  1.10  christos void
   12984  1.10  christos update_breakpoint_locations (code_breakpoint *b,
   12985  1.10  christos 			     struct program_space *filter_pspace,
   12986  1.10  christos 			     gdb::array_view<const symtab_and_line> sals,
   12987   1.1  christos 			     gdb::array_view<const symtab_and_line> sals_end)
   12988   1.1  christos {
   12989   1.1  christos   if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
   12990   1.1  christos     {
   12991   1.1  christos       /* Ranged breakpoints have only one start location and one end
   12992   1.1  christos 	 location.  */
   12993   1.1  christos       b->enable_state = bp_disabled;
   12994   1.1  christos       gdb_printf (gdb_stderr,
   12995   1.1  christos 		  _("Could not reset ranged breakpoint %d: "
   12996   1.8  christos 		    "multiple locations found\n"),
   12997   1.1  christos 		  b->number);
   12998   1.1  christos       return;
   12999  1.11  christos     }
   13000   1.1  christos 
   13001   1.8  christos   /* If there's no new locations, and all existing locations are
   13002   1.1  christos      pending, don't do anything.  This optimizes the common case where
   13003   1.1  christos      all locations are in the same shared library, that was unloaded.
   13004   1.1  christos      We'd like to retain the location, so that when the library is
   13005   1.8  christos      loaded again, we don't loose the enabled/disabled status of the
   13006   1.1  christos      individual locations.  */
   13007  1.10  christos   if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
   13008   1.1  christos     return;
   13009   1.1  christos 
   13010   1.1  christos   bp_location_list existing_locations = b->steal_locations (filter_pspace);
   13011   1.1  christos 
   13012   1.1  christos   for (const auto &sal : sals)
   13013   1.1  christos     {
   13014   1.1  christos       struct bp_location *new_loc;
   13015  1.10  christos 
   13016   1.9  christos       switch_to_program_space_and_thread (sal.pspace);
   13017   1.1  christos 
   13018   1.8  christos       new_loc = b->add_location (sal);
   13019   1.8  christos 
   13020   1.1  christos       /* Reparse conditions, they might contain references to the
   13021   1.1  christos 	 old symtab.  */
   13022   1.9  christos       if (b->cond_string != NULL)
   13023   1.1  christos 	{
   13024  1.10  christos 	  const char *s;
   13025   1.1  christos 
   13026   1.1  christos 	  s = b->cond_string.get ();
   13027   1.1  christos 	  try
   13028   1.8  christos 	    {
   13029   1.1  christos 	      new_loc->cond = parse_exp_1 (&s, sal.pc,
   13030   1.8  christos 					   block_for_pc (sal.pc),
   13031   1.1  christos 					   0);
   13032   1.8  christos 	    }
   13033   1.1  christos 	  catch (const gdb_exception_error &e)
   13034   1.1  christos 	    {
   13035   1.1  christos 	      new_loc->disabled_by_cond = true;
   13036   1.1  christos 	    }
   13037   1.1  christos 	}
   13038   1.1  christos 
   13039   1.1  christos       if (!sals_end.empty ())
   13040   1.1  christos 	{
   13041   1.1  christos 	  CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
   13042   1.1  christos 
   13043   1.1  christos 	  new_loc->length = end - sals[0].pc + 1;
   13044  1.11  christos 	}
   13045   1.1  christos     }
   13046  1.11  christos 
   13047   1.1  christos   /* If possible, carry over 'disable' status from existing
   13048  1.11  christos      breakpoints.  */
   13049   1.1  christos   {
   13050   1.1  christos     /* If there are multiple breakpoints with the same function name,
   13051   1.1  christos        e.g. for inline functions, comparing function names won't work.
   13052  1.11  christos        Instead compare pc addresses; this is just a heuristic as things
   13053   1.9  christos        may have moved, but in practice it gives the correct answer
   13054   1.9  christos        often enough until a better solution is found.  */
   13055   1.9  christos     int have_ambiguous_names = ambiguous_names_p (b->locations ());
   13056   1.9  christos 
   13057   1.9  christos     for (const bp_location &e : existing_locations)
   13058   1.9  christos       {
   13059   1.9  christos 	if ((!e.enabled || e.disabled_by_cond) && e.function_name)
   13060   1.9  christos 	  {
   13061  1.11  christos 	    if (have_ambiguous_names)
   13062   1.9  christos 	      {
   13063  1.11  christos 		for (bp_location &l : b->locations ())
   13064  1.11  christos 		  {
   13065   1.9  christos 		    /* Ignore software vs hardware location type at
   13066   1.9  christos 		       this point, because with "set breakpoint
   13067   1.9  christos 		       auto-hw", after a re-set, locations that were
   13068   1.1  christos 		       hardware can end up as software, or vice versa.
   13069   1.1  christos 		       As mentioned above, this is an heuristic and in
   13070   1.1  christos 		       practice should give the correct answer often
   13071  1.11  christos 		       enough.  */
   13072  1.11  christos 		    if (breakpoint_locations_match (&e, &l, true))
   13073  1.11  christos 		      {
   13074  1.11  christos 			l.enabled = e.enabled;
   13075   1.1  christos 			l.disabled_by_cond = e.disabled_by_cond;
   13076  1.11  christos 			break;
   13077  1.11  christos 		      }
   13078   1.1  christos 		  }
   13079   1.1  christos 	      }
   13080   1.1  christos 	    else
   13081   1.1  christos 	      {
   13082   1.1  christos 		for (bp_location &l : b->locations ())
   13083   1.1  christos 		  if (l.function_name
   13084   1.1  christos 		      && strcmp (e.function_name.get (),
   13085  1.11  christos 				 l.function_name.get ()) == 0)
   13086  1.11  christos 		    {
   13087   1.1  christos 		      l.enabled = e.enabled;
   13088   1.1  christos 		      l.disabled_by_cond = e.disabled_by_cond;
   13089  1.10  christos 		      break;
   13090   1.1  christos 		    }
   13091   1.1  christos 	      }
   13092  1.10  christos 	  }
   13093  1.10  christos       }
   13094  1.10  christos   }
   13095  1.10  christos 
   13096   1.1  christos   if (!locations_are_equal (existing_locations, b->locations ()))
   13097   1.9  christos     notify_breakpoint_modified (b);
   13098   1.1  christos }
   13099   1.8  christos 
   13100   1.8  christos /* Find the SaL locations corresponding to the given LOCSPEC.
   13101   1.9  christos    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
   13102   1.1  christos 
   13103  1.10  christos std::vector<symtab_and_line>
   13104   1.1  christos code_breakpoint::location_spec_to_sals (location_spec *locspec,
   13105   1.9  christos 					struct program_space *search_pspace,
   13106   1.1  christos 					int *found)
   13107  1.10  christos {
   13108   1.5  christos   struct gdb_exception exception;
   13109   1.1  christos 
   13110   1.1  christos   std::vector<symtab_and_line> sals;
   13111   1.1  christos 
   13112   1.1  christos   try
   13113   1.1  christos     {
   13114   1.1  christos       sals = decode_location_spec (locspec, search_pspace);
   13115   1.1  christos     }
   13116   1.1  christos   catch (gdb_exception_error &e)
   13117  1.10  christos     {
   13118  1.11  christos       int not_found_and_ok = false;
   13119   1.6  christos 
   13120  1.11  christos       /* For pending breakpoints, it's expected that parsing will
   13121  1.11  christos 	 fail until the right shared library is loaded.  User has
   13122  1.11  christos 	 already told to create pending breakpoints and don't need
   13123  1.11  christos 	 extra messages.  If breakpoint is in bp_shlib_disabled
   13124  1.10  christos 	 state, then user already saw the message about that
   13125  1.10  christos 	 breakpoint being disabled, and don't want to see more
   13126   1.1  christos 	 errors.  */
   13127   1.1  christos       if (e.error == NOT_FOUND_ERROR
   13128   1.1  christos 	  && (condition_not_parsed
   13129   1.1  christos 	      || (this->has_locations ()
   13130   1.1  christos 		  && search_pspace != NULL
   13131   1.1  christos 		  && this->first_loc ().pspace != search_pspace)
   13132   1.1  christos 	      || (this->has_locations () && this->first_loc ().shlib_disabled)
   13133   1.1  christos 	      || (this->has_locations ()
   13134   1.1  christos 		  && this->first_loc ().pspace->executing_startup)
   13135  1.10  christos 	      || enable_state == bp_disabled))
   13136   1.9  christos 	not_found_and_ok = true;
   13137   1.1  christos 
   13138   1.9  christos       if (!not_found_and_ok)
   13139   1.9  christos 	{
   13140   1.1  christos 	  /* We surely don't want to warn about the same breakpoint
   13141   1.1  christos 	     10 times.  One solution, implemented here, is disable
   13142   1.5  christos 	     the breakpoint on error.  Another solution would be to
   13143   1.1  christos 	     have separate 'warning emitted' flag.  Since this
   13144   1.8  christos 	     happens only when a binary has changed, I don't know
   13145   1.8  christos 	     which approach is better.  */
   13146  1.10  christos 	  enable_state = bp_disabled;
   13147   1.1  christos 	  throw;
   13148  1.10  christos 	}
   13149  1.11  christos 
   13150   1.1  christos       exception = std::move (e);
   13151  1.10  christos     }
   13152  1.10  christos 
   13153  1.11  christos   if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
   13154  1.10  christos     {
   13155  1.10  christos       for (auto &sal : sals)
   13156  1.10  christos 	resolve_sal_pc (&sal);
   13157  1.10  christos       if (condition_not_parsed && extra_string != NULL)
   13158  1.10  christos 	{
   13159  1.10  christos 	  gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
   13160  1.10  christos 	  int local_thread, local_task, local_inferior;
   13161  1.10  christos 
   13162  1.10  christos 	  find_condition_and_thread_for_sals (sals, extra_string.get (),
   13163   1.1  christos 					      &local_cond, &local_thread,
   13164   1.1  christos 					      &local_inferior,
   13165  1.10  christos 					      &local_task, &local_extra);
   13166  1.11  christos 	  gdb_assert (cond_string == nullptr);
   13167  1.11  christos 	  if (local_cond != nullptr)
   13168  1.11  christos 	    cond_string = std::move (local_cond);
   13169  1.11  christos 	  thread = local_thread;
   13170   1.1  christos 	  task = local_task;
   13171   1.1  christos 	  if (local_extra != nullptr)
   13172   1.1  christos 	    extra_string = std::move (local_extra);
   13173   1.1  christos 	  condition_not_parsed = 0;
   13174   1.1  christos 	}
   13175   1.1  christos 
   13176   1.1  christos       if (type == bp_static_tracepoint)
   13177   1.1  christos 	{
   13178   1.1  christos 	  tracepoint *t = gdb::checked_static_cast<tracepoint *> (this);
   13179   1.1  christos 	  sals[0] = update_static_tracepoint (t, sals[0]);
   13180   1.1  christos 	}
   13181   1.1  christos 
   13182   1.1  christos       *found = 1;
   13183  1.10  christos     }
   13184  1.10  christos   else
   13185   1.1  christos     *found = 0;
   13186   1.6  christos 
   13187   1.8  christos   return sals;
   13188   1.1  christos }
   13189   1.8  christos 
   13190  1.10  christos /* The default re_set method, for typical hardware or software
   13191  1.10  christos    breakpoints.  Reevaluate the breakpoint and recreate its
   13192  1.10  christos    locations.  */
   13193   1.1  christos 
   13194   1.8  christos void
   13195   1.1  christos code_breakpoint::re_set_default ()
   13196  1.10  christos {
   13197   1.1  christos   struct program_space *filter_pspace = current_program_space;
   13198   1.8  christos   std::vector<symtab_and_line> expanded, expanded_end;
   13199  1.10  christos 
   13200  1.10  christos   int found;
   13201   1.1  christos   std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
   13202   1.8  christos 							     filter_pspace,
   13203   1.1  christos 							     &found);
   13204   1.1  christos   if (found)
   13205  1.10  christos     expanded = std::move (sals);
   13206   1.1  christos 
   13207   1.1  christos   if (locspec_range_end != nullptr)
   13208   1.1  christos     {
   13209   1.1  christos       std::vector<symtab_and_line> sals_end
   13210   1.1  christos 	= location_spec_to_sals (locspec_range_end.get (),
   13211   1.1  christos 				 filter_pspace, &found);
   13212  1.10  christos       if (found)
   13213  1.10  christos 	expanded_end = std::move (sals_end);
   13214   1.1  christos     }
   13215  1.10  christos 
   13216   1.1  christos   update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
   13217   1.1  christos }
   13218   1.8  christos 
   13219   1.1  christos /* Default method for creating SALs from an address string.  It basically
   13220   1.8  christos    calls parse_breakpoint_sals.  Return 1 for success, zero for failure.  */
   13221   1.8  christos 
   13222   1.1  christos static void
   13223   1.1  christos create_sals_from_location_spec_default (location_spec *locspec,
   13224   1.1  christos 					struct linespec_result *canonical)
   13225   1.1  christos {
   13226  1.10  christos   parse_breakpoint_sals (locspec, canonical);
   13227   1.1  christos }
   13228   1.1  christos 
   13229   1.6  christos /* Reset a breakpoint.  */
   13230   1.6  christos 
   13231   1.6  christos static void
   13232   1.1  christos breakpoint_re_set_one (breakpoint *b)
   13233   1.1  christos {
   13234   1.1  christos   input_radix = b->input_radix;
   13235   1.1  christos   set_language (b->language);
   13236   1.8  christos 
   13237   1.8  christos   b->re_set ();
   13238   1.8  christos }
   13239   1.8  christos 
   13240   1.8  christos /* Re-set breakpoint locations for the current program space.
   13241   1.8  christos    Locations bound to other program spaces are left untouched.  */
   13242   1.8  christos 
   13243   1.8  christos void
   13244   1.8  christos breakpoint_re_set (void)
   13245   1.8  christos {
   13246   1.8  christos   {
   13247   1.8  christos     scoped_restore_current_language save_language;
   13248   1.8  christos     scoped_restore save_input_radix = make_scoped_restore (&input_radix);
   13249   1.8  christos     scoped_restore_current_pspace_and_thread restore_pspace_thread;
   13250   1.8  christos 
   13251   1.8  christos     /* breakpoint_re_set_one sets the current_language to the language
   13252   1.8  christos        of the breakpoint it is resetting (see prepare_re_set_context)
   13253   1.8  christos        before re-evaluating the breakpoint's location.  This change can
   13254   1.8  christos        unfortunately get undone by accident if the language_mode is set
   13255   1.8  christos        to auto, and we either switch frames, or more likely in this context,
   13256   1.8  christos        we select the current frame.
   13257   1.1  christos 
   13258  1.11  christos        We prevent this by temporarily turning the language_mode to
   13259   1.8  christos        language_mode_manual.  We restore it once all breakpoints
   13260   1.9  christos        have been reset.  */
   13261   1.8  christos     scoped_restore save_language_mode = make_scoped_restore (&language_mode);
   13262  1.11  christos     language_mode = language_mode_manual;
   13263   1.8  christos 
   13264   1.9  christos     /* Note: we must not try to insert locations until after all
   13265   1.8  christos        breakpoints have been re-set.  Otherwise, e.g., when re-setting
   13266   1.8  christos        breakpoint 1, we'd insert the locations of breakpoint 2, which
   13267   1.8  christos        hadn't been re-set yet, and thus may have stale locations.  */
   13268  1.11  christos 
   13269   1.8  christos     for (breakpoint &b : all_breakpoints_safe ())
   13270   1.8  christos       {
   13271   1.1  christos 	try
   13272   1.8  christos 	  {
   13273   1.8  christos 	    breakpoint_re_set_one (&b);
   13274   1.1  christos 	  }
   13275   1.1  christos 	catch (const gdb_exception &ex)
   13276   1.1  christos 	  {
   13277   1.1  christos 	    exception_fprintf (gdb_stderr, ex,
   13278   1.1  christos 			       "Error in re-setting breakpoint %d: ",
   13279   1.6  christos 			       b.number);
   13280   1.6  christos 	  }
   13281   1.6  christos       }
   13282   1.1  christos 
   13283   1.1  christos     jit_breakpoint_re_set ();
   13284   1.1  christos   }
   13285   1.1  christos 
   13286   1.1  christos   create_overlay_event_breakpoint ();
   13287   1.1  christos   create_longjmp_master_breakpoint ();
   13288   1.1  christos   create_std_terminate_master_breakpoint ();
   13289   1.1  christos   create_exception_master_breakpoint ();
   13290   1.1  christos 
   13291   1.1  christos   /* Now we can insert.  */
   13292   1.1  christos   update_global_location_list (UGLL_MAY_INSERT);
   13293   1.8  christos }
   13294   1.1  christos 
   13295   1.1  christos /* Reset the thread number of this breakpoint:
   13297   1.1  christos 
   13298   1.1  christos    - If the breakpoint is for all threads, leave it as-is.
   13299  1.11  christos    - Else, reset it to the current thread for inferior_ptid.  */
   13300   1.1  christos void
   13301   1.1  christos breakpoint_re_set_thread (struct breakpoint *b)
   13302   1.1  christos {
   13303   1.1  christos   if (b->thread != -1)
   13304   1.1  christos     {
   13305   1.1  christos       b->thread = inferior_thread ()->global_num;
   13306   1.1  christos 
   13307   1.1  christos       /* We're being called after following a fork.  The new fork is
   13308   1.1  christos 	 selected as current, and unless this was a vfork will have a
   13309   1.1  christos 	 different program space from the original thread.  Reset that
   13310   1.1  christos 	 as well.  */
   13311   1.1  christos       b->first_loc ().pspace = current_program_space;
   13312   1.1  christos     }
   13313  1.11  christos }
   13314  1.11  christos 
   13315  1.10  christos /* Set ignore-count of breakpoint number BPTNUM to COUNT.
   13316  1.11  christos    If from_tty is nonzero, it prints a message to that effect,
   13317  1.10  christos    which ends with a period (no newline).  */
   13318  1.10  christos 
   13319  1.10  christos void
   13320  1.10  christos set_ignore_count (int bptnum, int count, int from_tty)
   13321  1.10  christos {
   13322  1.10  christos   if (count < 0)
   13323  1.10  christos     count = 0;
   13324  1.11  christos 
   13325  1.10  christos   for (breakpoint &b : all_breakpoints ())
   13326  1.10  christos     if (b.number == bptnum)
   13327  1.10  christos       {
   13328  1.10  christos 	if (is_tracepoint (&b))
   13329  1.10  christos 	  {
   13330  1.10  christos 	    if (from_tty && count != 0)
   13331  1.10  christos 	      gdb_printf (_("Ignore count ignored for tracepoint %d."),
   13332  1.10  christos 			  bptnum);
   13333  1.10  christos 	    return;
   13334  1.10  christos 	  }
   13335  1.10  christos 
   13336  1.10  christos 	b.ignore_count = count;
   13337  1.10  christos 	if (from_tty)
   13338  1.10  christos 	  {
   13339  1.11  christos 	    if (count == 0)
   13340  1.10  christos 	      gdb_printf (_("Will stop next time "
   13341  1.10  christos 			    "breakpoint %d is reached."),
   13342   1.1  christos 			  bptnum);
   13343   1.1  christos 	    else if (count == 1)
   13344   1.1  christos 	      gdb_printf (_("Will ignore next crossing of breakpoint %d."),
   13345   1.1  christos 			  bptnum);
   13346   1.1  christos 	    else
   13347   1.1  christos 	      gdb_printf (_("Will ignore next %d "
   13348   1.1  christos 			    "crossings of breakpoint %d."),
   13349   1.8  christos 			  count, bptnum);
   13350   1.1  christos 	  }
   13351   1.8  christos 	notify_breakpoint_modified (&b);
   13352   1.1  christos 	return;
   13353   1.1  christos       }
   13354   1.1  christos 
   13355   1.1  christos   error (_("No breakpoint number %d."), bptnum);
   13356   1.1  christos }
   13357   1.1  christos 
   13358   1.1  christos /* Command to set ignore-count of breakpoint N to COUNT.  */
   13359   1.1  christos 
   13360   1.1  christos static void
   13361   1.1  christos ignore_command (const char *args, int from_tty)
   13362   1.1  christos {
   13363   1.1  christos   const char *p = args;
   13364   1.1  christos   int num;
   13365   1.1  christos 
   13366   1.1  christos   if (p == 0)
   13367  1.10  christos     error_no_arg (_("a breakpoint number"));
   13368   1.1  christos 
   13369   1.1  christos   num = get_number (&p);
   13370   1.8  christos   if (num == 0)
   13371   1.8  christos     error (_("bad breakpoint number: '%s'"), args);
   13372   1.8  christos   if (*p == 0)
   13373   1.1  christos     error (_("Second argument (specified ignore-count) is missing."));
   13374   1.1  christos 
   13375   1.8  christos   set_ignore_count (num,
   13376   1.8  christos 		    longest_to_int (value_as_long (parse_and_eval (p))),
   13377   1.1  christos 		    from_tty);
   13378   1.8  christos   if (from_tty)
   13379   1.8  christos     gdb_printf ("\n");
   13380   1.8  christos }
   13381   1.8  christos 
   13382   1.8  christos 
   13384   1.1  christos /* Call FUNCTION on each of the breakpoints with numbers in the range
   13385   1.8  christos    defined by BP_NUM_RANGE (an inclusive range).  */
   13386   1.1  christos 
   13387   1.8  christos static void
   13388   1.8  christos map_breakpoint_number_range (std::pair<int, int> bp_num_range,
   13389  1.11  christos 			     gdb::function_view<void (breakpoint *)> function)
   13390  1.11  christos {
   13391   1.1  christos   if (bp_num_range.first == 0)
   13392   1.7  christos     {
   13393  1.11  christos       warning (_("bad breakpoint number at or near '%d'"),
   13394   1.1  christos 	       bp_num_range.first);
   13395   1.1  christos     }
   13396   1.7  christos   else
   13397  1.10  christos     {
   13398   1.1  christos       for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
   13399   1.1  christos 	{
   13400   1.1  christos 	  bool match = false;
   13401   1.1  christos 
   13402   1.8  christos 	  for (breakpoint &b : all_breakpoints_safe ())
   13403   1.8  christos 	    if (b.number == i)
   13404   1.8  christos 	      {
   13405   1.8  christos 		match = true;
   13406   1.8  christos 		function (&b);
   13407   1.8  christos 		break;
   13408   1.8  christos 	      }
   13409   1.8  christos 	  if (!match)
   13410   1.8  christos 	    gdb_printf (_("No breakpoint number %d.\n"), i);
   13411   1.8  christos 	}
   13412   1.8  christos     }
   13413   1.8  christos }
   13414   1.8  christos 
   13415   1.8  christos /* Call FUNCTION on each of the breakpoints whose numbers are given in
   13416   1.8  christos    ARGS.  */
   13417   1.8  christos 
   13418   1.8  christos static void
   13419   1.8  christos map_breakpoint_numbers (const char *args,
   13420   1.8  christos 			gdb::function_view<void (breakpoint *)> function)
   13421   1.8  christos {
   13422   1.8  christos   if (args == NULL || *args == '\0')
   13423   1.8  christos     error_no_arg (_("one or more breakpoint numbers"));
   13424   1.1  christos 
   13425   1.8  christos   number_or_range_parser parser (args);
   13426   1.1  christos 
   13427  1.10  christos   while (!parser.finished ())
   13428   1.1  christos     {
   13429   1.1  christos       int num = parser.get_number ();
   13430   1.8  christos       map_breakpoint_number_range (std::make_pair (num, num), function);
   13431   1.1  christos     }
   13432   1.1  christos }
   13433   1.8  christos 
   13434   1.8  christos /* Return the breakpoint location structure corresponding to the
   13435   1.8  christos    BP_NUM and LOC_NUM values.  */
   13436  1.11  christos 
   13437   1.8  christos static struct bp_location *
   13438  1.11  christos find_location_by_number (int bp_num, int loc_num)
   13439   1.8  christos {
   13440   1.8  christos   breakpoint *b = get_breakpoint (bp_num);
   13441   1.8  christos 
   13442   1.8  christos   if (!b || b->number != bp_num)
   13443   1.8  christos     error (_("Bad breakpoint number '%d'"), bp_num);
   13444   1.8  christos 
   13445   1.8  christos   if (loc_num == 0)
   13446   1.8  christos     error (_("Bad breakpoint location number '%d'"), loc_num);
   13447   1.8  christos 
   13448   1.8  christos   int n = 0;
   13449   1.8  christos   for (bp_location &loc : b->locations ())
   13450   1.8  christos     if (++n == loc_num)
   13451   1.8  christos       return &loc;
   13452   1.8  christos 
   13453   1.8  christos   error (_("Bad breakpoint location number '%d'"), loc_num);
   13454   1.8  christos }
   13455   1.8  christos 
   13456   1.8  christos /* Modes of operation for extract_bp_num.  */
   13457   1.8  christos enum class extract_bp_kind
   13458   1.8  christos {
   13459   1.8  christos   /* Extracting a breakpoint number.  */
   13460   1.8  christos   bp,
   13461   1.8  christos 
   13462   1.8  christos   /* Extracting a location number.  */
   13463   1.8  christos   loc,
   13464   1.8  christos };
   13465   1.8  christos 
   13466   1.8  christos /* Extract a breakpoint or location number (as determined by KIND)
   13467   1.8  christos    from the string starting at START.  TRAILER is a character which
   13468   1.8  christos    can be found after the number.  If you don't want a trailer, use
   13469   1.8  christos    '\0'.  If END_OUT is not NULL, it is set to point after the parsed
   13470   1.8  christos    string.  This always returns a positive integer.  */
   13471   1.8  christos 
   13472   1.8  christos static int
   13473   1.8  christos extract_bp_num (extract_bp_kind kind, const char *start,
   13474   1.8  christos 		int trailer, const char **end_out = NULL)
   13475   1.8  christos {
   13476   1.8  christos   const char *end = start;
   13477   1.8  christos   int num = get_number_trailer (&end, trailer);
   13478   1.8  christos   if (num < 0)
   13479   1.8  christos     error (kind == extract_bp_kind::bp
   13480   1.8  christos 	   ? _("Negative breakpoint number '%.*s'")
   13481   1.8  christos 	   : _("Negative breakpoint location number '%.*s'"),
   13482   1.8  christos 	   int (end - start), start);
   13483   1.8  christos   if (num == 0)
   13484   1.8  christos     error (kind == extract_bp_kind::bp
   13485   1.8  christos 	   ? _("Bad breakpoint number '%.*s'")
   13486   1.8  christos 	   : _("Bad breakpoint location number '%.*s'"),
   13487   1.8  christos 	   int (end - start), start);
   13488   1.8  christos 
   13489   1.8  christos   if (end_out != NULL)
   13490   1.8  christos     *end_out = end;
   13491   1.8  christos   return num;
   13492   1.8  christos }
   13493   1.8  christos 
   13494   1.8  christos /* Extract a breakpoint or location range (as determined by KIND) in
   13495   1.8  christos    the form NUM1-NUM2 stored at &ARG[arg_offset].  Returns a std::pair
   13496   1.8  christos    representing the (inclusive) range.  The returned pair's elements
   13497   1.8  christos    are always positive integers.  */
   13498   1.8  christos 
   13499   1.8  christos static std::pair<int, int>
   13500   1.8  christos extract_bp_or_bp_range (extract_bp_kind kind,
   13501   1.8  christos 			const std::string &arg,
   13502   1.8  christos 			std::string::size_type arg_offset)
   13503   1.8  christos {
   13504   1.8  christos   std::pair<int, int> range;
   13505   1.8  christos   const char *bp_loc = &arg[arg_offset];
   13506   1.8  christos   std::string::size_type dash = arg.find ('-', arg_offset);
   13507   1.8  christos   if (dash != std::string::npos)
   13508   1.8  christos     {
   13509   1.8  christos       /* bp_loc is a range (x-z).  */
   13510   1.8  christos       if (arg.length () == dash + 1)
   13511   1.8  christos 	error (kind == extract_bp_kind::bp
   13512   1.8  christos 	       ? _("Bad breakpoint number at or near: '%s'")
   13513   1.8  christos 	       : _("Bad breakpoint location number at or near: '%s'"),
   13514   1.8  christos 	       bp_loc);
   13515   1.8  christos 
   13516   1.8  christos       const char *end;
   13517   1.8  christos       const char *start_first = bp_loc;
   13518   1.8  christos       const char *start_second = &arg[dash + 1];
   13519   1.8  christos       range.first = extract_bp_num (kind, start_first, '-');
   13520   1.8  christos       range.second = extract_bp_num (kind, start_second, '\0', &end);
   13521   1.8  christos 
   13522   1.8  christos       if (range.first > range.second)
   13523   1.8  christos 	error (kind == extract_bp_kind::bp
   13524   1.8  christos 	       ? _("Inverted breakpoint range at '%.*s'")
   13525   1.8  christos 	       : _("Inverted breakpoint location range at '%.*s'"),
   13526   1.8  christos 	       int (end - start_first), start_first);
   13527   1.8  christos     }
   13528   1.8  christos   else
   13529   1.8  christos     {
   13530   1.8  christos       /* bp_loc is a single value.  */
   13531   1.8  christos       range.first = extract_bp_num (kind, bp_loc, '\0');
   13532   1.8  christos       range.second = range.first;
   13533   1.8  christos     }
   13534   1.8  christos   return range;
   13535   1.8  christos }
   13536   1.8  christos 
   13537   1.8  christos /* Extract the breakpoint/location range specified by ARG.  Returns
   13538   1.8  christos    the breakpoint range in BP_NUM_RANGE, and the location range in
   13539   1.8  christos    BP_LOC_RANGE.
   13540   1.8  christos 
   13541   1.8  christos    ARG may be in any of the following forms:
   13542   1.8  christos 
   13543   1.8  christos    x     where 'x' is a breakpoint number.
   13544   1.8  christos    x-y   where 'x' and 'y' specify a breakpoint numbers range.
   13545   1.8  christos    x.y   where 'x' is a breakpoint number and 'y' a location number.
   13546   1.8  christos    x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
   13547   1.8  christos 	 location number range.
   13548   1.8  christos */
   13549   1.8  christos 
   13550   1.8  christos static void
   13551   1.8  christos extract_bp_number_and_location (const std::string &arg,
   13552   1.8  christos 				std::pair<int, int> &bp_num_range,
   13553   1.8  christos 				std::pair<int, int> &bp_loc_range)
   13554   1.8  christos {
   13555   1.8  christos   std::string::size_type dot = arg.find ('.');
   13556   1.8  christos 
   13557   1.8  christos   if (dot != std::string::npos)
   13558   1.8  christos     {
   13559   1.8  christos       /* Handle 'x.y' and 'x.y-z' cases.  */
   13560   1.8  christos 
   13561   1.8  christos       if (arg.length () == dot + 1 || dot == 0)
   13562   1.8  christos 	error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
   13563   1.8  christos 
   13564   1.8  christos       bp_num_range.first
   13565   1.8  christos 	= extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
   13566   1.8  christos       bp_num_range.second = bp_num_range.first;
   13567   1.1  christos 
   13568   1.8  christos       bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
   13569   1.8  christos 					     arg, dot + 1);
   13570   1.8  christos     }
   13571   1.8  christos   else
   13572   1.8  christos     {
   13573   1.8  christos       /* Handle x and x-y cases.  */
   13574   1.8  christos 
   13575   1.8  christos       bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
   13576   1.8  christos       bp_loc_range.first = 0;
   13577  1.10  christos       bp_loc_range.second = 0;
   13578  1.10  christos     }
   13579  1.10  christos }
   13580  1.10  christos 
   13581   1.8  christos /* Enable or disable a breakpoint location BP_NUM.LOC_NUM.  ENABLE
   13582   1.8  christos    specifies whether to enable or disable.  */
   13583   1.8  christos 
   13584   1.8  christos static void
   13585   1.8  christos enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
   13586   1.8  christos {
   13587   1.8  christos   struct bp_location *loc = find_location_by_number (bp_num, loc_num);
   13588   1.8  christos   if (loc != NULL)
   13589   1.8  christos     {
   13590   1.8  christos       if (loc->disabled_by_cond && enable)
   13591   1.8  christos 	error (_("Breakpoint %d's condition is invalid at location %d, "
   13592   1.8  christos 		 "cannot enable."), bp_num, loc_num);
   13593  1.11  christos 
   13594   1.1  christos       if (loc->enabled != enable)
   13595   1.1  christos 	{
   13596  1.10  christos 	  loc->enabled = enable;
   13597  1.10  christos 	  mark_breakpoint_location_modified (loc);
   13598  1.10  christos 	}
   13599  1.10  christos       if (target_supports_enable_disable_tracepoint ()
   13600  1.10  christos 	  && current_trace_status ()->running && loc->owner
   13601  1.10  christos 	  && is_tracepoint (loc->owner))
   13602  1.10  christos 	target_disable_tracepoint (loc);
   13603  1.10  christos     }
   13604  1.10  christos   update_global_location_list (UGLL_DONT_INSERT);
   13605  1.10  christos 
   13606  1.11  christos   notify_breakpoint_modified (loc->owner);
   13607  1.10  christos }
   13608  1.11  christos 
   13609  1.10  christos /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
   13610  1.10  christos    owner.  1-based indexing.  -1 signals NOT FOUND.  */
   13611  1.10  christos 
   13612  1.10  christos static int
   13613  1.10  christos find_loc_num_by_location (const bp_location *loc)
   13614  1.10  christos {
   13615  1.10  christos   if (loc != nullptr && loc->owner != nullptr)
   13616  1.10  christos     {
   13617  1.10  christos       /* Locations use 1-based indexing.  */
   13618  1.10  christos       int loc_num = 1;
   13619  1.10  christos       for (bp_location &it : loc->owner->locations ())
   13620  1.10  christos 	{
   13621  1.10  christos 	  if (&it == loc)
   13622  1.10  christos 	    return loc_num;
   13623  1.10  christos 	  loc_num++;
   13624  1.10  christos 	}
   13625  1.10  christos     }
   13626  1.10  christos   return -1;
   13627  1.10  christos }
   13628  1.10  christos 
   13629  1.10  christos /* Enable or disable a breakpoint location LOC.  ENABLE
   13630  1.10  christos    specifies whether to enable or disable.  */
   13631  1.10  christos 
   13632  1.10  christos void
   13633  1.10  christos enable_disable_bp_location (bp_location *loc, bool enable)
   13634  1.10  christos {
   13635  1.10  christos   if (loc == nullptr)
   13636  1.10  christos     error (_("Breakpoint location is invalid."));
   13637  1.10  christos 
   13638  1.10  christos   if (loc->owner == nullptr)
   13639  1.10  christos     error (_("Breakpoint location does not have an owner breakpoint."));
   13640  1.10  christos 
   13641  1.10  christos   if (loc->disabled_by_cond && enable)
   13642  1.10  christos     {
   13643  1.10  christos       int loc_num = find_loc_num_by_location (loc);
   13644  1.10  christos       if (loc_num == -1)
   13645  1.10  christos 	error (_("Breakpoint location LOC_NUM could not be found."));
   13646  1.10  christos       else
   13647  1.10  christos 	error (_("Breakpoint %d's condition is invalid at location %d, "
   13648  1.10  christos 		 "cannot enable."), loc->owner->number, loc_num);
   13649  1.10  christos     }
   13650  1.11  christos 
   13651  1.10  christos   if (loc->enabled != enable)
   13652  1.10  christos     {
   13653   1.8  christos       loc->enabled = enable;
   13654   1.8  christos       mark_breakpoint_location_modified (loc);
   13655   1.8  christos     }
   13656   1.8  christos 
   13657   1.8  christos   if (target_supports_enable_disable_tracepoint ()
   13658   1.8  christos       && current_trace_status ()->running && loc->owner
   13659   1.8  christos       && is_tracepoint (loc->owner))
   13660   1.8  christos     target_disable_tracepoint (loc);
   13661   1.8  christos 
   13662   1.8  christos   update_global_location_list (UGLL_DONT_INSERT);
   13663   1.8  christos   notify_breakpoint_modified (loc->owner);
   13664   1.8  christos }
   13665   1.8  christos 
   13666   1.8  christos /* Enable or disable a range of breakpoint locations.  BP_NUM is the
   13667   1.1  christos    number of the breakpoint, and BP_LOC_RANGE specifies the
   13668   1.1  christos    (inclusive) range of location numbers of that breakpoint to
   13669   1.1  christos    enable/disable.  ENABLE specifies whether to enable or disable the
   13670   1.1  christos    location.  */
   13671   1.1  christos 
   13672   1.1  christos static void
   13673   1.1  christos enable_disable_breakpoint_location_range (int bp_num,
   13674   1.1  christos 					  std::pair<int, int> &bp_loc_range,
   13675   1.1  christos 					  bool enable)
   13676   1.1  christos {
   13677   1.1  christos   for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
   13678   1.1  christos     enable_disable_bp_num_loc (bp_num, i, enable);
   13679   1.1  christos }
   13680   1.1  christos 
   13681   1.1  christos /* Set ignore-count of breakpoint number BPTNUM to COUNT.
   13682   1.1  christos    If from_tty is nonzero, it prints a message to that effect,
   13683   1.1  christos    which ends with a period (no newline).  */
   13684   1.1  christos 
   13685   1.1  christos void
   13686   1.1  christos disable_breakpoint (struct breakpoint *bpt)
   13687   1.1  christos {
   13688   1.1  christos   /* Never disable a watchpoint scope breakpoint; we want to
   13689  1.11  christos      hit them when we leave scope so we can delete both the
   13690  1.11  christos      watchpoint and its scope breakpoint at that time.  */
   13691   1.1  christos   if (bpt->type == bp_watchpoint_scope)
   13692   1.1  christos     return;
   13693   1.3  christos 
   13694   1.1  christos   bpt->enable_state = bp_disabled;
   13695  1.11  christos 
   13696   1.1  christos   /* Mark breakpoint locations modified.  */
   13697   1.1  christos   mark_breakpoint_modified (bpt);
   13698   1.8  christos 
   13699   1.8  christos   if (target_supports_enable_disable_tracepoint ()
   13700   1.8  christos       && current_trace_status ()->running && is_tracepoint (bpt))
   13701   1.8  christos     {
   13702   1.1  christos       for (bp_location &location : bpt->locations ())
   13703   1.1  christos 	target_disable_tracepoint (&location);
   13704   1.8  christos     }
   13705   1.1  christos 
   13706   1.1  christos   update_global_location_list (UGLL_DONT_INSERT);
   13707   1.1  christos 
   13708  1.11  christos   notify_breakpoint_modified (bpt);
   13709  1.11  christos }
   13710   1.8  christos 
   13711   1.8  christos /* Enable or disable the breakpoint(s) or breakpoint location(s)
   13712  1.11  christos    specified in ARGS.  ARGS may be in any of the formats handled by
   13713   1.8  christos    extract_bp_number_and_location.  ENABLE specifies whether to enable
   13714  1.11  christos    or disable the breakpoints/locations.  */
   13715   1.8  christos 
   13716   1.1  christos static void
   13717   1.1  christos enable_disable_command (const char *args, int from_tty, bool enable)
   13718   1.1  christos {
   13719   1.8  christos   if (args == 0)
   13720   1.1  christos     {
   13721   1.8  christos       for (breakpoint &bpt : all_breakpoints ())
   13722   1.1  christos 	if (user_breakpoint_p (&bpt))
   13723   1.8  christos 	  {
   13724   1.8  christos 	    if (enable)
   13725   1.8  christos 	      enable_breakpoint (&bpt);
   13726   1.8  christos 	    else
   13727   1.8  christos 	      disable_breakpoint (&bpt);
   13728  1.10  christos 	  }
   13729  1.10  christos     }
   13730  1.10  christos   else
   13731  1.10  christos     {
   13732   1.1  christos       std::string num = extract_arg (&args);
   13733  1.10  christos 
   13734  1.10  christos       while (!num.empty ())
   13735   1.8  christos 	{
   13736   1.8  christos 	  std::pair<int, int> bp_num_range, bp_loc_range;
   13737   1.8  christos 
   13738   1.8  christos 	  extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
   13739   1.1  christos 
   13740   1.1  christos 	  if (bp_loc_range.first == bp_loc_range.second
   13741   1.8  christos 	      && (bp_loc_range.first == 0
   13742   1.8  christos 		  || (bp_loc_range.first == 1
   13743   1.8  christos 		      && bp_num_range.first == bp_num_range.second
   13744   1.8  christos 		      && !has_multiple_locations (bp_num_range.first))))
   13745   1.8  christos 	    {
   13746   1.8  christos 	      /* Handle breakpoint ids with formats 'x' or 'x-z'
   13747   1.1  christos 		 or 'y.1' where y has only one code location.  */
   13748   1.1  christos 	      map_breakpoint_number_range (bp_num_range,
   13749   1.1  christos 					   enable
   13750   1.1  christos 					   ? enable_breakpoint
   13751   1.1  christos 					   : disable_breakpoint);
   13752   1.8  christos 	    }
   13753   1.8  christos 	  else
   13754   1.8  christos 	    {
   13755   1.8  christos 	      /* Handle breakpoint ids with formats 'x.y' or
   13756   1.8  christos 		 'x.y-z'.  */
   13757   1.8  christos 	      enable_disable_breakpoint_location_range
   13758   1.8  christos 		(bp_num_range.first, bp_loc_range, enable);
   13759   1.8  christos 	    }
   13760   1.8  christos 	  num = extract_arg (&args);
   13761   1.8  christos 	}
   13762   1.8  christos     }
   13763   1.1  christos }
   13764   1.1  christos 
   13765   1.1  christos /* The disable command disables the specified breakpoints/locations
   13766   1.1  christos    (or all defined breakpoints) so they're no longer effective in
   13767   1.1  christos    stopping the inferior.  ARGS may be in any of the forms defined in
   13768   1.1  christos    extract_bp_number_and_location.  */
   13769   1.1  christos 
   13770   1.1  christos static void
   13771   1.1  christos disable_command (const char *args, int from_tty)
   13772   1.1  christos {
   13773   1.1  christos   enable_disable_command (args, from_tty, false);
   13774   1.1  christos }
   13775   1.1  christos 
   13776   1.1  christos static void
   13777   1.1  christos enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
   13778   1.1  christos 			int count)
   13779   1.1  christos {
   13780   1.1  christos   int target_resources_ok;
   13781   1.1  christos 
   13782   1.1  christos   if (bpt->type == bp_hardware_breakpoint)
   13783   1.1  christos     {
   13784   1.1  christos       int i;
   13785   1.6  christos       i = hw_breakpoint_used_count ();
   13786   1.1  christos       target_resources_ok =
   13787   1.9  christos 	target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
   13788   1.1  christos 					    i + 1, 0);
   13789  1.11  christos       if (target_resources_ok == 0)
   13790   1.1  christos 	error (_("No hardware breakpoint support in the target."));
   13791   1.1  christos       else if (target_resources_ok < 0)
   13792   1.1  christos 	error (_("Hardware breakpoints used exceeds limit."));
   13793  1.10  christos     }
   13794   1.1  christos 
   13795  1.11  christos   if (is_watchpoint (bpt))
   13796   1.1  christos     {
   13797   1.1  christos       /* Initialize it just to avoid a GCC false warning.  */
   13798   1.1  christos       enum enable_state orig_enable_state = bp_disabled;
   13799   1.1  christos 
   13800   1.1  christos       try
   13801   1.1  christos 	{
   13802   1.1  christos 	  watchpoint *w = gdb::checked_static_cast<watchpoint *> (bpt);
   13803   1.1  christos 
   13804   1.1  christos 	  orig_enable_state = bpt->enable_state;
   13805   1.1  christos 	  bpt->enable_state = bp_enabled;
   13806   1.1  christos 	  update_watchpoint (w, true /* reparse */);
   13807   1.1  christos 	}
   13808   1.1  christos       catch (const gdb_exception_error &e)
   13809   1.1  christos 	{
   13810   1.1  christos 	  bpt->enable_state = orig_enable_state;
   13811   1.1  christos 	  exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
   13812  1.11  christos 			     bpt->number);
   13813  1.11  christos 	  return;
   13814   1.1  christos 	}
   13815   1.1  christos     }
   13816   1.1  christos 
   13817   1.1  christos   bpt->enable_state = bp_enabled;
   13818   1.3  christos 
   13819   1.1  christos   /* Mark breakpoint locations modified.  */
   13820  1.11  christos   mark_breakpoint_modified (bpt);
   13821   1.1  christos 
   13822   1.1  christos   if (target_supports_enable_disable_tracepoint ()
   13823   1.1  christos       && current_trace_status ()->running && is_tracepoint (bpt))
   13824   1.1  christos     {
   13825   1.1  christos       for (bp_location &location : bpt->locations ())
   13826   1.1  christos 	target_enable_tracepoint (&location);
   13827   1.1  christos     }
   13828   1.1  christos 
   13829   1.1  christos   bpt->disposition = disposition;
   13830   1.8  christos   bpt->enable_count = count;
   13831   1.8  christos   update_global_location_list (UGLL_MAY_INSERT);
   13832   1.8  christos 
   13833   1.8  christos   notify_breakpoint_modified (bpt);
   13834   1.1  christos }
   13835   1.1  christos 
   13836   1.8  christos 
   13837   1.1  christos void
   13838   1.8  christos enable_breakpoint (struct breakpoint *bpt)
   13839   1.1  christos {
   13840   1.1  christos   enable_breakpoint_disp (bpt, bpt->disposition, 0);
   13841   1.1  christos }
   13842   1.8  christos 
   13843   1.1  christos /* The enable command enables the specified breakpoints/locations (or
   13844   1.8  christos    all defined breakpoints) so they once again become (or continue to
   13845   1.8  christos    be) effective in stopping the inferior.  ARGS may be in any of the
   13846   1.8  christos    forms defined in extract_bp_number_and_location.  */
   13847   1.8  christos 
   13848   1.8  christos static void
   13849   1.8  christos enable_command (const char *args, int from_tty)
   13850   1.8  christos {
   13851   1.8  christos   enable_disable_command (args, from_tty, true);
   13852   1.8  christos }
   13853   1.1  christos 
   13854   1.1  christos static void
   13855   1.1  christos enable_once_command (const char *args, int from_tty)
   13856   1.8  christos {
   13857   1.1  christos   map_breakpoint_numbers
   13858   1.5  christos     (args, [&] (breakpoint *b)
   13859   1.5  christos      {
   13860   1.5  christos        iterate_over_related_breakpoints
   13861   1.5  christos 	 (b, [&] (breakpoint *bpt)
   13862   1.5  christos 	  {
   13863   1.5  christos 	    enable_breakpoint_disp (bpt, disp_disable, 1);
   13864   1.1  christos 	  });
   13865   1.8  christos      });
   13866   1.8  christos }
   13867   1.8  christos 
   13868   1.8  christos static void
   13869   1.8  christos enable_count_command (const char *args, int from_tty)
   13870   1.8  christos {
   13871   1.8  christos   int count;
   13872   1.8  christos 
   13873   1.8  christos   if (args == NULL)
   13874   1.1  christos     error_no_arg (_("hit count"));
   13875   1.1  christos 
   13876   1.1  christos   count = get_number (&args);
   13877   1.8  christos 
   13878   1.1  christos   map_breakpoint_numbers
   13879   1.8  christos     (args, [&] (breakpoint *b)
   13880   1.8  christos      {
   13881   1.8  christos        iterate_over_related_breakpoints
   13882   1.8  christos 	 (b, [&] (breakpoint *bpt)
   13883   1.8  christos 	  {
   13884   1.8  christos 	    enable_breakpoint_disp (bpt, disp_disable, count);
   13885   1.8  christos 	  });
   13886   1.8  christos      });
   13887   1.8  christos }
   13888   1.1  christos 
   13889   1.1  christos static void
   13890   1.1  christos enable_delete_command (const char *args, int from_tty)
   13891   1.1  christos {
   13892   1.1  christos   map_breakpoint_numbers
   13893   1.1  christos     (args, [&] (breakpoint *b)
   13894   1.1  christos      {
   13895   1.1  christos        iterate_over_related_breakpoints
   13896   1.1  christos 	 (b, [&] (breakpoint *bpt)
   13897   1.1  christos 	  {
   13898   1.1  christos 	    enable_breakpoint_disp (bpt, disp_del, 1);
   13899  1.11  christos 	  });
   13900  1.11  christos      });
   13901  1.11  christos }
   13902   1.1  christos 
   13903  1.11  christos /* Invalidate last known value of any hardware watchpoint if
   13905  1.11  christos    the memory which that value represents has been written to by
   13906   1.1  christos    GDB itself.  */
   13907  1.11  christos 
   13908  1.11  christos static void
   13909  1.11  christos invalidate_bp_value_on_memory_change (struct inferior *inferior,
   13910  1.11  christos 				      CORE_ADDR addr, ssize_t len,
   13911   1.1  christos 				      const bfd_byte *data)
   13912  1.11  christos {
   13913  1.11  christos   for (breakpoint &bp : all_breakpoints ())
   13914   1.1  christos     if (bp.enable_state == bp_enabled
   13915   1.1  christos 	&& bp.type == bp_hardware_watchpoint)
   13916   1.1  christos       {
   13917   1.1  christos 	watchpoint &wp = gdb::checked_static_cast<watchpoint &> (bp);
   13918   1.1  christos 
   13919   1.1  christos 	if (wp.val_valid && wp.val != nullptr)
   13920   1.1  christos 	  {
   13921   1.1  christos 	    for (bp_location &loc : bp.locations ())
   13922   1.1  christos 	      if (loc.loc_type == bp_loc_hardware_watchpoint
   13923   1.8  christos 		  && loc.address + loc.length > addr
   13924   1.1  christos 		  && addr + len > loc.address)
   13925   1.1  christos 		{
   13926   1.3  christos 		  wp.val = NULL;
   13927   1.3  christos 		  wp.val_valid = false;
   13928   1.3  christos 		}
   13929   1.1  christos 	  }
   13930   1.3  christos       }
   13931   1.1  christos }
   13932  1.10  christos 
   13933  1.10  christos /* Create and insert a breakpoint for software single step.  */
   13934  1.10  christos 
   13935  1.10  christos void
   13936  1.10  christos insert_single_step_breakpoint (struct gdbarch *gdbarch,
   13937  1.10  christos 			       const address_space *aspace,
   13938   1.3  christos 			       CORE_ADDR next_pc)
   13939  1.10  christos {
   13940   1.1  christos   struct thread_info *tp = inferior_thread ();
   13941   1.1  christos   struct symtab_and_line sal;
   13942   1.3  christos   CORE_ADDR pc = next_pc;
   13943   1.3  christos 
   13944   1.3  christos   if (tp->control.single_step_breakpoints == NULL)
   13945   1.3  christos     {
   13946  1.10  christos       std::unique_ptr<breakpoint> b
   13947  1.10  christos 	(new momentary_breakpoint (gdbarch, bp_single_step,
   13948  1.10  christos 				   current_program_space,
   13949  1.10  christos 				   null_frame_id,
   13950  1.10  christos 				   tp->global_num));
   13951   1.1  christos 
   13952   1.3  christos       tp->control.single_step_breakpoints
   13953   1.1  christos 	= add_to_breakpoint_chain (std::move (b));
   13954   1.1  christos     }
   13955   1.7  christos 
   13956   1.7  christos   sal = find_pc_line (pc, 0);
   13957   1.7  christos   sal.pc = pc;
   13958   1.7  christos   sal.section = find_pc_overlay (pc);
   13959   1.7  christos   sal.explicit_pc = 1;
   13960  1.11  christos 
   13961   1.8  christos   auto *ss_bp
   13962   1.7  christos     = (gdb::checked_static_cast<momentary_breakpoint *>
   13963   1.7  christos        (tp->control.single_step_breakpoints));
   13964   1.7  christos   ss_bp->add_location (sal);
   13965   1.8  christos 
   13966   1.7  christos   update_global_location_list (UGLL_INSERT);
   13967  1.10  christos }
   13968   1.8  christos 
   13969   1.7  christos /* Insert single step breakpoints according to the current state.  */
   13970   1.8  christos 
   13971   1.7  christos int
   13972   1.7  christos insert_single_step_breakpoints (struct gdbarch *gdbarch)
   13973   1.7  christos {
   13974   1.7  christos   regcache *regcache = get_thread_regcache (inferior_thread ());
   13975   1.7  christos   std::vector<CORE_ADDR> next_pcs;
   13976   1.7  christos 
   13977   1.7  christos   next_pcs = gdbarch_software_single_step (gdbarch, regcache);
   13978   1.7  christos 
   13979   1.3  christos   if (!next_pcs.empty ())
   13980   1.1  christos     {
   13981   1.1  christos       frame_info_ptr frame = get_current_frame ();
   13982   1.3  christos       const address_space *aspace = get_frame_address_space (frame);
   13983   1.8  christos 
   13984   1.3  christos       for (CORE_ADDR pc : next_pcs)
   13985   1.1  christos 	insert_single_step_breakpoint (gdbarch, aspace, pc);
   13986  1.11  christos 
   13987  1.11  christos       return 1;
   13988  1.11  christos     }
   13989   1.3  christos   else
   13990   1.1  christos     return 0;
   13991   1.3  christos }
   13992   1.1  christos 
   13993   1.1  christos /* See breakpoint.h.  */
   13994   1.1  christos 
   13995   1.1  christos int
   13996   1.1  christos breakpoint_has_location_inserted_here (struct breakpoint *bp,
   13997   1.3  christos 				       const address_space *aspace,
   13998   1.8  christos 				       CORE_ADDR pc)
   13999   1.1  christos {
   14000   1.1  christos   for (bp_location &loc : bp->locations ())
   14001  1.11  christos     if (loc.inserted
   14002   1.1  christos 	&& breakpoint_location_address_match (&loc, aspace, pc))
   14003  1.11  christos       return 1;
   14004  1.11  christos 
   14005   1.1  christos   return 0;
   14006   1.1  christos }
   14007   1.1  christos 
   14008   1.1  christos /* Check whether a software single-step breakpoint is inserted at
   14009   1.1  christos    PC.  */
   14010   1.1  christos 
   14011   1.1  christos int
   14012   1.1  christos single_step_breakpoint_inserted_here_p (const address_space *aspace,
   14013   1.1  christos 					CORE_ADDR pc)
   14014   1.1  christos {
   14015   1.1  christos   for (breakpoint &bpt : all_breakpoints ())
   14016   1.1  christos     {
   14017   1.1  christos       if (bpt.type == bp_single_step
   14018   1.1  christos 	  && breakpoint_has_location_inserted_here (&bpt, aspace, pc))
   14019   1.1  christos 	return 1;
   14020   1.1  christos     }
   14021   1.8  christos   return 0;
   14022   1.1  christos }
   14023  1.10  christos 
   14024  1.10  christos /* Tracepoint-specific operations.  */
   14025  1.10  christos 
   14026  1.10  christos /* Set tracepoint count to NUM.  */
   14027   1.1  christos static void
   14028   1.1  christos set_tracepoint_count (int num)
   14029  1.10  christos {
   14030  1.11  christos   tracepoint_count = num;
   14031   1.1  christos   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
   14032   1.1  christos }
   14033   1.1  christos 
   14034   1.1  christos static void
   14035   1.1  christos trace_command (const char *arg, int from_tty)
   14036   1.1  christos {
   14037   1.1  christos   location_spec_up locspec = string_to_location_spec (&arg,
   14038   1.1  christos 						      current_language);
   14039   1.1  christos   const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
   14040   1.1  christos     (locspec.get (), true /* is_tracepoint */);
   14041   1.1  christos 
   14042   1.8  christos   create_breakpoint (get_current_arch (),
   14043   1.1  christos 		     locspec.get (),
   14044  1.10  christos 		     NULL, -1, -1, arg, false, 1 /* parse arg */,
   14045  1.10  christos 		     0 /* tempflag */,
   14046   1.1  christos 		     bp_tracepoint /* type_wanted */,
   14047  1.10  christos 		     0 /* Ignore count */,
   14048  1.11  christos 		     pending_break_support,
   14049   1.1  christos 		     ops,
   14050   1.1  christos 		     from_tty,
   14051   1.1  christos 		     1 /* enabled */,
   14052   1.1  christos 		     0 /* internal */, 0);
   14053  1.10  christos }
   14054   1.1  christos 
   14055   1.1  christos static void
   14056   1.1  christos ftrace_command (const char *arg, int from_tty)
   14057   1.1  christos {
   14058   1.1  christos   location_spec_up locspec = string_to_location_spec (&arg,
   14059   1.1  christos 						      current_language);
   14060   1.1  christos   create_breakpoint (get_current_arch (),
   14061   1.1  christos 		     locspec.get (),
   14062   1.8  christos 		     NULL, -1, -1, arg, false, 1 /* parse arg */,
   14063   1.1  christos 		     0 /* tempflag */,
   14064  1.10  christos 		     bp_fast_tracepoint /* type_wanted */,
   14065  1.10  christos 		     0 /* Ignore count */,
   14066  1.10  christos 		     pending_break_support,
   14067   1.1  christos 		     &code_breakpoint_ops,
   14068   1.1  christos 		     from_tty,
   14069   1.1  christos 		     1 /* enabled */,
   14070  1.11  christos 		     0 /* internal */, 0);
   14071   1.6  christos }
   14072   1.6  christos 
   14073  1.10  christos /* strace command implementation.  Creates a static tracepoint.  */
   14074  1.10  christos 
   14075  1.10  christos static void
   14076   1.6  christos strace_command (const char *arg, int from_tty)
   14077   1.1  christos {
   14078   1.6  christos   const struct breakpoint_ops *ops;
   14079  1.10  christos   location_spec_up locspec;
   14080  1.10  christos   enum bptype type;
   14081  1.10  christos 
   14082   1.6  christos   /* Decide if we are dealing with a static tracepoint marker (`-m'),
   14083   1.1  christos      or with a normal static tracepoint.  */
   14084   1.1  christos   if (arg && startswith (arg, "-m") && isspace ((unsigned char)arg[2]))
   14085  1.10  christos     {
   14086  1.11  christos       ops = &strace_marker_breakpoint_ops;
   14087   1.1  christos       locspec = new_linespec_location_spec (&arg,
   14088  1.10  christos 					    symbol_name_match_type::FULL);
   14089   1.1  christos       type = bp_static_marker_tracepoint;
   14090   1.1  christos     }
   14091   1.1  christos   else
   14092   1.1  christos     {
   14093   1.1  christos       ops = &code_breakpoint_ops;
   14094   1.1  christos       locspec = string_to_location_spec (&arg, current_language);
   14095   1.1  christos       type = bp_static_tracepoint;
   14096   1.1  christos     }
   14097   1.1  christos 
   14098   1.1  christos   create_breakpoint (get_current_arch (),
   14099   1.1  christos 		     locspec.get (),
   14100   1.1  christos 		     NULL, -1, -1, arg, false, 1 /* parse arg */,
   14101   1.1  christos 		     0 /* tempflag */,
   14102   1.1  christos 		     type /* type_wanted */,
   14103  1.10  christos 		     0 /* Ignore count */,
   14104  1.10  christos 		     pending_break_support,
   14105   1.1  christos 		     ops,
   14106   1.8  christos 		     from_tty,
   14107   1.1  christos 		     1 /* enabled */,
   14108   1.8  christos 		     0 /* internal */, 0);
   14109   1.8  christos }
   14110   1.8  christos 
   14111   1.8  christos /* Set up a fake reader function that gets command lines from a linked
   14112   1.8  christos    list that was acquired during tracepoint uploading.  */
   14113   1.1  christos 
   14114   1.1  christos static struct uploaded_tp *this_utp;
   14115   1.1  christos static int next_cmd;
   14116   1.1  christos 
   14117   1.1  christos static const char *
   14118   1.1  christos read_uploaded_action (std::string &buffer)
   14119   1.1  christos {
   14120   1.1  christos   char *rslt = nullptr;
   14121   1.1  christos 
   14122   1.1  christos   if (next_cmd < this_utp->cmd_strings.size ())
   14123   1.1  christos     {
   14124   1.1  christos       rslt = this_utp->cmd_strings[next_cmd].get ();
   14125   1.1  christos       next_cmd++;
   14126   1.8  christos     }
   14127   1.8  christos 
   14128   1.1  christos   return rslt;
   14129   1.1  christos }
   14130   1.1  christos 
   14131   1.8  christos /* Given information about a tracepoint as recorded on a target (which
   14132   1.1  christos    can be either a live system or a trace file), attempt to create an
   14133   1.1  christos    equivalent GDB tracepoint.  This is not a reliable process, since
   14134   1.1  christos    the target does not necessarily have all the information used when
   14135   1.1  christos    the tracepoint was originally defined.  */
   14136   1.1  christos 
   14137   1.1  christos struct tracepoint *
   14138   1.1  christos create_tracepoint_from_upload (struct uploaded_tp *utp)
   14139   1.1  christos {
   14140   1.1  christos   const char *addr_str;
   14141   1.1  christos   char small_buf[100];
   14142   1.1  christos   struct tracepoint *tp;
   14143   1.1  christos 
   14144   1.1  christos   if (utp->at_string)
   14145   1.1  christos     addr_str = utp->at_string.get ();
   14146   1.1  christos   else
   14147   1.1  christos     {
   14148   1.1  christos       /* In the absence of a source location, fall back to raw
   14149   1.1  christos 	 address.  Since there is no way to confirm that the address
   14150   1.1  christos 	 means the same thing as when the trace was started, warn the
   14151  1.10  christos 	 user.  */
   14152  1.10  christos       warning (_("Uploaded tracepoint %d has no "
   14153  1.11  christos 		 "source location, using raw address"),
   14154  1.11  christos 	       utp->number);
   14155  1.11  christos       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
   14156  1.11  christos       addr_str = small_buf;
   14157  1.11  christos     }
   14158  1.11  christos 
   14159   1.1  christos   /* There's not much we can do with a sequence of bytecodes.  */
   14160  1.10  christos   if (utp->cond && !utp->cond_string)
   14161  1.11  christos     warning (_("Uploaded tracepoint %d condition "
   14162  1.10  christos 	       "has no source form, ignoring it"),
   14163   1.1  christos 	     utp->number);
   14164   1.1  christos 
   14165   1.1  christos   location_spec_up locspec = string_to_location_spec (&addr_str,
   14166   1.1  christos 						      current_language);
   14167   1.1  christos 
   14168  1.10  christos 
   14169   1.1  christos   gdb_assert (addr_str != nullptr);
   14170   1.1  christos   if (*addr_str != '\0')
   14171   1.1  christos     error (_("Garbage '%s' at end of location"), addr_str);
   14172   1.1  christos 
   14173   1.7  christos   if (!create_breakpoint (get_current_arch (),
   14174   1.1  christos 			  locspec.get (),
   14175   1.1  christos 			  utp->cond_string.get (), -1, -1, addr_str,
   14176   1.1  christos 			  false /* force_condition */,
   14177   1.1  christos 			  0 /* parse cond/thread */,
   14178   1.1  christos 			  0 /* tempflag */,
   14179   1.1  christos 			  utp->type /* type_wanted */,
   14180   1.1  christos 			  0 /* Ignore count */,
   14181   1.1  christos 			  pending_break_support,
   14182   1.8  christos 			  &code_breakpoint_ops,
   14183   1.1  christos 			  0 /* from_tty */,
   14184   1.1  christos 			  utp->enabled /* enabled */,
   14185   1.1  christos 			  0 /* internal */,
   14186   1.1  christos 			  CREATE_BREAKPOINT_FLAGS_INSERTED))
   14187   1.1  christos     return NULL;
   14188   1.1  christos 
   14189   1.1  christos   /* Get the tracepoint we just created.  */
   14190   1.1  christos   tp = get_tracepoint (tracepoint_count);
   14191   1.8  christos   gdb_assert (tp != NULL);
   14192   1.1  christos 
   14193   1.8  christos   if (utp->pass > 0)
   14194   1.1  christos     {
   14195   1.1  christos       xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
   14196   1.1  christos 		 tp->number);
   14197   1.1  christos 
   14198   1.8  christos       trace_pass_command (small_buf, 0);
   14199   1.1  christos     }
   14200   1.8  christos 
   14201   1.1  christos   /* If we have uploaded versions of the original commands, set up a
   14202   1.8  christos      special-purpose "reader" function and call the usual command line
   14203   1.8  christos      reader, then pass the result to the breakpoint command-setting
   14204   1.1  christos      function.  */
   14205   1.1  christos   if (!utp->cmd_strings.empty ())
   14206   1.1  christos     {
   14207   1.1  christos       counted_command_line cmd_list;
   14208   1.1  christos 
   14209   1.8  christos       this_utp = utp;
   14210   1.1  christos       next_cmd = 0;
   14211   1.1  christos 
   14212   1.1  christos       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
   14213   1.1  christos 
   14214   1.1  christos       breakpoint_set_commands (tp, std::move (cmd_list));
   14215   1.1  christos     }
   14216   1.1  christos   else if (!utp->actions.empty ()
   14217   1.1  christos 	   || !utp->step_actions.empty ())
   14218   1.1  christos     warning (_("Uploaded tracepoint %d actions "
   14219   1.8  christos 	       "have no source form, ignoring them"),
   14220   1.1  christos 	     utp->number);
   14221   1.1  christos 
   14222   1.1  christos   /* Copy any status information that might be available.  */
   14223   1.1  christos   tp->hit_count = utp->hit_count;
   14224   1.9  christos   tp->traceframe_usage = utp->traceframe_usage;
   14225   1.1  christos 
   14226   1.1  christos   return tp;
   14227   1.1  christos }
   14228   1.1  christos 
   14229   1.7  christos /* Print information on tracepoint number TPNUM_EXP, or all if
   14230   1.1  christos    omitted.  */
   14231   1.7  christos 
   14232   1.1  christos static void
   14233   1.1  christos info_tracepoints_command (const char *args, int from_tty)
   14234   1.1  christos {
   14235   1.1  christos   struct ui_out *uiout = current_uiout;
   14236   1.1  christos   int num_printed;
   14237   1.1  christos 
   14238   1.1  christos   num_printed = breakpoint_1 (args, false, is_tracepoint);
   14239   1.1  christos 
   14240   1.8  christos   if (num_printed == 0)
   14241   1.1  christos     {
   14242   1.1  christos       if (args == NULL || *args == '\0')
   14243   1.1  christos 	uiout->message ("No tracepoints.\n");
   14244   1.1  christos       else
   14245   1.1  christos 	uiout->message ("No tracepoint matching '%s'.\n", args);
   14246   1.1  christos     }
   14247   1.1  christos 
   14248   1.8  christos   default_collect_info ();
   14249   1.1  christos }
   14250   1.1  christos 
   14251   1.1  christos /* The 'enable trace' command enables tracepoints.
   14252   1.1  christos    Not supported by all targets.  */
   14253   1.1  christos static void
   14254   1.1  christos enable_trace_command (const char *args, int from_tty)
   14255   1.8  christos {
   14256   1.1  christos   enable_command (args, from_tty);
   14257   1.1  christos }
   14258   1.1  christos 
   14259   1.1  christos /* The 'disable trace' command disables tracepoints.
   14260   1.1  christos    Not supported by all targets.  */
   14261   1.1  christos static void
   14262   1.1  christos disable_trace_command (const char *args, int from_tty)
   14263  1.11  christos {
   14264  1.10  christos   disable_command (args, from_tty);
   14265  1.10  christos }
   14266   1.1  christos 
   14267  1.11  christos /* Remove a tracepoint (or all if no argument).  */
   14268  1.11  christos static void
   14269   1.1  christos delete_trace_command (const char *arg, int from_tty)
   14270   1.1  christos {
   14271   1.1  christos   dont_repeat ();
   14272   1.1  christos 
   14273   1.1  christos   if (arg == 0)
   14274   1.1  christos     {
   14275   1.1  christos       int breaks_to_delete = 0;
   14276   1.1  christos 
   14277   1.1  christos       /* Delete all tracepoints if no argument.
   14278  1.11  christos 	 Do not delete internal or call-dummy breakpoints, these
   14279  1.11  christos 	 have to be deleted with an explicit breakpoint number
   14280  1.11  christos 	 argument.  */
   14281   1.1  christos       for (breakpoint &tp : all_tracepoints ())
   14282   1.1  christos 	if (is_tracepoint (&tp) && user_breakpoint_p (&tp))
   14283   1.1  christos 	  {
   14284   1.8  christos 	    breaks_to_delete = 1;
   14285   1.8  christos 	    break;
   14286   1.8  christos 	  }
   14287   1.8  christos 
   14288   1.8  christos       /* Ask user only if there are some breakpoints to delete.  */
   14289   1.1  christos       if (!from_tty
   14290   1.1  christos 	  || (breaks_to_delete && query (_("Delete all tracepoints? "))))
   14291   1.1  christos 	{
   14292   1.1  christos 	  for (breakpoint &b : all_breakpoints_safe ())
   14293   1.1  christos 	    if (is_tracepoint (&b) && user_breakpoint_p (&b))
   14294   1.1  christos 	      delete_breakpoint (&b);
   14295   1.1  christos 	}
   14296   1.1  christos     }
   14297  1.11  christos   else
   14298   1.1  christos     map_breakpoint_numbers
   14299  1.10  christos       (arg, [&] (breakpoint *br)
   14300  1.10  christos        {
   14301   1.1  christos 	 iterate_over_related_breakpoints (br, delete_breakpoint);
   14302   1.1  christos        });
   14303   1.1  christos }
   14304   1.1  christos 
   14305   1.1  christos /* Helper function for trace_pass_command.  */
   14306   1.1  christos 
   14307   1.1  christos static void
   14308   1.1  christos trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
   14309   1.1  christos {
   14310   1.8  christos   tp->pass_count = count;
   14311   1.1  christos   notify_breakpoint_modified (tp);
   14312   1.8  christos   if (from_tty)
   14313   1.1  christos     gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
   14314   1.1  christos 		tp->number, count);
   14315   1.1  christos }
   14316   1.1  christos 
   14317   1.1  christos /* Set passcount for tracepoint.
   14318   1.8  christos 
   14319   1.1  christos    First command argument is passcount, second is tracepoint number.
   14320   1.1  christos    If tracepoint number omitted, apply to most recently defined.
   14321   1.1  christos    Also accepts special argument "all".  */
   14322   1.1  christos 
   14323   1.1  christos static void
   14324   1.1  christos trace_pass_command (const char *args, int from_tty)
   14325   1.1  christos {
   14326   1.1  christos   ULONGEST count;
   14327  1.11  christos 
   14328  1.10  christos   if (args == 0 || *args == 0)
   14329  1.11  christos     error (_("passcount command requires an "
   14330  1.11  christos 	     "argument (count + optional TP num)"));
   14331  1.10  christos 
   14332   1.1  christos   count = strtoulst (args, &args, 10);	/* Count comes first, then TP num.  */
   14333   1.1  christos 
   14334   1.1  christos   args = skip_spaces (args);
   14335  1.11  christos   if (*args && strncasecmp (args, "all", 3) == 0)
   14336   1.1  christos     {
   14337   1.1  christos       args += 3;			/* Skip special argument "all".  */
   14338   1.1  christos       if (*args)
   14339   1.1  christos 	error (_("Junk at end of arguments."));
   14340   1.1  christos 
   14341   1.7  christos       for (breakpoint &b : all_tracepoints ())
   14342   1.7  christos 	{
   14343   1.1  christos 	  tracepoint &t1 = gdb::checked_static_cast<tracepoint &> (b);
   14344  1.11  christos 	  trace_pass_set_count (&t1, count, from_tty);
   14345   1.1  christos 	}
   14346   1.1  christos     }
   14347   1.1  christos   else if (*args == '\0')
   14348   1.1  christos     {
   14349   1.1  christos       tracepoint *t1 = get_tracepoint_by_number (&args, NULL);
   14350   1.1  christos       if (t1)
   14351   1.1  christos 	trace_pass_set_count (t1, count, from_tty);
   14352   1.1  christos     }
   14353   1.1  christos   else
   14354  1.11  christos     {
   14355  1.11  christos       number_or_range_parser parser (args);
   14356  1.11  christos       while (!parser.finished ())
   14357   1.1  christos 	{
   14358   1.1  christos 	  tracepoint *t1 = get_tracepoint_by_number (&args, &parser);
   14359   1.1  christos 	  if (t1)
   14360   1.1  christos 	    trace_pass_set_count (t1, count, from_tty);
   14361   1.1  christos 	}
   14362   1.1  christos     }
   14363   1.1  christos }
   14364   1.1  christos 
   14365   1.1  christos struct tracepoint *
   14366   1.1  christos get_tracepoint (int num)
   14367   1.1  christos {
   14368  1.11  christos   for (breakpoint &t : all_tracepoints ())
   14369   1.1  christos     if (t.number == num)
   14370  1.11  christos       return gdb::checked_static_cast<tracepoint *> (&t);
   14371   1.1  christos 
   14372  1.11  christos   return NULL;
   14373  1.11  christos }
   14374   1.1  christos 
   14375   1.1  christos /* Find the tracepoint with the given target-side number (which may be
   14376   1.1  christos    different from the tracepoint number after disconnecting and
   14377   1.1  christos    reconnecting).  */
   14378   1.1  christos 
   14379   1.1  christos struct tracepoint *
   14380   1.1  christos get_tracepoint_by_number_on_target (int num)
   14381   1.3  christos {
   14382   1.3  christos   for (breakpoint &b : all_tracepoints ())
   14383   1.3  christos     {
   14384   1.1  christos       tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
   14385   1.8  christos 
   14386   1.7  christos       if (t.number_on_target == num)
   14387   1.1  christos 	return &t;
   14388   1.1  christos     }
   14389   1.8  christos 
   14390   1.1  christos   return NULL;
   14391   1.7  christos }
   14392   1.1  christos 
   14393   1.7  christos /* Utility: parse a tracepoint number and look it up in the list.
   14394   1.7  christos    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
   14395   1.1  christos    If the argument is missing, the most recent tracepoint
   14396   1.1  christos    (tracepoint_count) is returned.  */
   14397   1.3  christos 
   14398   1.1  christos struct tracepoint *
   14399   1.1  christos get_tracepoint_by_number (const char **arg,
   14400   1.1  christos 			  number_or_range_parser *parser)
   14401   1.1  christos {
   14402   1.1  christos   int tpnum;
   14403   1.1  christos   const char *instring = arg == NULL ? NULL : *arg;
   14404  1.10  christos 
   14405  1.10  christos   if (parser != NULL)
   14406   1.1  christos     {
   14407  1.10  christos       gdb_assert (!parser->finished ());
   14408   1.1  christos       tpnum = parser->get_number ();
   14409   1.1  christos     }
   14410   1.1  christos   else if (arg == NULL || *arg == NULL || ! **arg)
   14411  1.11  christos     tpnum = tracepoint_count;
   14412  1.11  christos   else
   14413  1.11  christos     tpnum = get_number (arg);
   14414   1.1  christos 
   14415  1.10  christos   if (tpnum <= 0)
   14416   1.1  christos     {
   14417   1.1  christos       if (instring && *instring)
   14418   1.1  christos 	gdb_printf (_("bad tracepoint number at or near '%s'\n"),
   14419   1.1  christos 		    instring);
   14420  1.10  christos       else
   14421   1.1  christos 	gdb_printf (_("No previous tracepoint\n"));
   14422  1.10  christos       return NULL;
   14423  1.11  christos     }
   14424  1.11  christos 
   14425  1.11  christos   for (breakpoint &t : all_tracepoints ())
   14426  1.11  christos     if (t.number == tpnum)
   14427   1.1  christos       return gdb::checked_static_cast<tracepoint *> (&t);
   14428  1.11  christos 
   14429  1.10  christos   gdb_printf ("No tracepoint number %d.\n", tpnum);
   14430   1.1  christos   return NULL;
   14431  1.10  christos }
   14432   1.1  christos 
   14433   1.1  christos void
   14434   1.1  christos breakpoint::print_recreate_thread (struct ui_file *fp) const
   14435   1.1  christos {
   14436   1.1  christos   if (thread != -1)
   14437   1.9  christos     {
   14438   1.1  christos       struct thread_info *thr = find_thread_global_id (thread);
   14439   1.1  christos       gdb_printf (fp, " thread %s", print_full_thread_id (thr));
   14440   1.8  christos     }
   14441   1.9  christos 
   14442   1.1  christos   if (task != -1)
   14443  1.10  christos     gdb_printf (fp, " task %d", task);
   14444   1.1  christos 
   14445   1.1  christos   gdb_printf (fp, "\n");
   14446   1.1  christos }
   14447   1.1  christos 
   14448   1.1  christos /* Save information on user settable breakpoints (watchpoints, etc) to
   14449   1.1  christos    a new script file named FILENAME.  If FILTER is non-NULL, call it
   14450  1.11  christos    on each breakpoint and only include the ones for which it returns
   14451  1.10  christos    true.  */
   14452  1.10  christos 
   14453  1.11  christos static void
   14454  1.10  christos save_breakpoints (const char *filename, int from_tty,
   14455   1.1  christos 		  bool (*filter) (const struct breakpoint *))
   14456  1.10  christos {
   14457  1.11  christos   bool any = false;
   14458  1.10  christos   int extra_trace_bits = 0;
   14459   1.1  christos 
   14460  1.10  christos   if (filename == 0 || *filename == 0)
   14461   1.1  christos     error (_("Argument required (file name in which to save)"));
   14462  1.11  christos 
   14463  1.10  christos   /* See if we have anything to save.  */
   14464  1.10  christos   for (breakpoint &tp : all_breakpoints ())
   14465   1.1  christos     {
   14466  1.10  christos       /* Skip internal and momentary breakpoints.  */
   14467  1.10  christos       if (!user_breakpoint_p (&tp))
   14468  1.10  christos 	continue;
   14469  1.10  christos 
   14470   1.1  christos       /* If we have a filter, only save the breakpoints it accepts.  */
   14471   1.1  christos       if (filter && !filter (&tp))
   14472   1.1  christos 	continue;
   14473   1.1  christos 
   14474   1.1  christos       any = true;
   14475   1.1  christos 
   14476   1.1  christos       if (is_tracepoint (&tp))
   14477   1.8  christos 	{
   14478   1.7  christos 	  extra_trace_bits = 1;
   14479   1.7  christos 
   14480   1.7  christos 	  /* We can stop searching.  */
   14481   1.8  christos 	  break;
   14482   1.1  christos 	}
   14483   1.8  christos     }
   14484   1.1  christos 
   14485   1.1  christos   if (!any)
   14486   1.7  christos     {
   14487   1.1  christos       warning (_("Nothing to save."));
   14488  1.11  christos       return;
   14489  1.10  christos     }
   14490  1.10  christos 
   14491  1.11  christos   gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
   14492  1.10  christos 
   14493  1.10  christos   stdio_file fp;
   14494  1.10  christos 
   14495  1.11  christos   if (!fp.open (expanded_filename.get (), "w"))
   14496  1.10  christos     error (_("Unable to open file '%s' for saving (%s)"),
   14497  1.10  christos 	   expanded_filename.get (), safe_strerror (errno));
   14498  1.11  christos 
   14499   1.1  christos   if (extra_trace_bits)
   14500  1.10  christos     save_trace_state_variables (&fp);
   14501  1.10  christos 
   14502  1.10  christos   for (breakpoint &tp : all_breakpoints ())
   14503   1.1  christos     {
   14504  1.11  christos       /* Skip internal and momentary breakpoints.  */
   14505  1.11  christos       if (!user_breakpoint_p (&tp))
   14506   1.1  christos 	continue;
   14507  1.11  christos 
   14508  1.11  christos       /* If we have a filter, only save the breakpoints it accepts.  */
   14509   1.1  christos       if (filter && !filter (&tp))
   14510  1.11  christos 	continue;
   14511  1.10  christos 
   14512  1.10  christos       tp.print_recreate (&fp);
   14513   1.1  christos 
   14514  1.10  christos       /* Note, we can't rely on tp->number for anything, as we can't
   14515  1.11  christos 	 assume the recreated breakpoint numbers will match.  Use $bpnum
   14516   1.1  christos 	 instead.  */
   14517  1.10  christos 
   14518  1.10  christos       if (tp.cond_string)
   14519   1.5  christos 	fp.printf ("  condition $bpnum %s\n", tp.cond_string.get ());
   14520  1.11  christos 
   14521  1.10  christos       if (tp.ignore_count)
   14522   1.1  christos 	fp.printf ("  ignore $bpnum %d\n", tp.ignore_count);
   14523  1.10  christos 
   14524  1.10  christos       if (tp.type != bp_dprintf && tp.commands)
   14525  1.10  christos 	{
   14526  1.11  christos 	  fp.puts ("  commands\n");
   14527  1.10  christos 
   14528  1.10  christos 	  ui_out_redirect_pop redir (current_uiout, &fp);
   14529   1.1  christos 	  print_command_lines (current_uiout, tp.commands.get (), 2);
   14530  1.11  christos 
   14531  1.10  christos 	  fp.puts ("  end\n");
   14532  1.11  christos 	}
   14533  1.10  christos 
   14534   1.1  christos       if (tp.enable_state == bp_disabled)
   14535  1.10  christos 	fp.puts ("disable $bpnum\n");
   14536  1.10  christos 
   14537  1.10  christos       /* If this is a multi-location breakpoint, check if the locations
   14538  1.10  christos 	 should be individually disabled.  Watchpoint locations are
   14539   1.1  christos 	 special, and not user visible.  */
   14540  1.10  christos       if (!is_watchpoint (&tp) && tp.has_multiple_locations ())
   14541  1.10  christos 	{
   14542   1.1  christos 	  int n = 1;
   14543   1.1  christos 
   14544  1.10  christos 	  for (bp_location &loc : tp.locations ())
   14545   1.1  christos 	    {
   14546   1.1  christos 	      if (!loc.enabled)
   14547   1.1  christos 		fp.printf ("disable $bpnum.%d\n", n);
   14548   1.1  christos 
   14549   1.1  christos 	      n++;
   14550   1.8  christos 	    }
   14551   1.1  christos 	}
   14552   1.1  christos     }
   14553   1.1  christos 
   14554   1.1  christos   if (extra_trace_bits && !default_collect.empty ())
   14555   1.1  christos     fp.printf ("set default-collect %s\n", default_collect.c_str ());
   14556   1.1  christos 
   14557   1.1  christos   if (from_tty)
   14558   1.8  christos     gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
   14559   1.1  christos }
   14560   1.1  christos 
   14561   1.1  christos /* The `save breakpoints' command.  */
   14562   1.1  christos 
   14563   1.1  christos static void
   14564   1.6  christos save_breakpoints_command (const char *args, int from_tty)
   14565   1.6  christos {
   14566   1.6  christos   save_breakpoints (args, from_tty, NULL);
   14567  1.10  christos }
   14568   1.6  christos 
   14569   1.6  christos /* The `save tracepoints' command.  */
   14570   1.6  christos 
   14571   1.6  christos static void
   14572   1.6  christos save_tracepoints_command (const char *args, int from_tty)
   14573   1.6  christos {
   14574   1.6  christos   save_breakpoints (args, from_tty, is_tracepoint);
   14575   1.6  christos }
   14576   1.6  christos 
   14577   1.6  christos 
   14578   1.6  christos /* This help string is used to consolidate all the help string for specifying
   14580   1.6  christos    locations used by several commands.  */
   14581   1.6  christos 
   14582   1.8  christos #define LOCATION_SPEC_HELP_STRING \
   14583   1.8  christos "Linespecs are colon-separated lists of location parameters, such as\n\
   14584   1.8  christos source filename, function name, label name, and line number.\n\
   14585   1.8  christos Example: To specify the start of a label named \"the_top\" in the\n\
   14586   1.8  christos function \"fact\" in the file \"factorial.c\", use\n\
   14587   1.8  christos \"factorial.c:fact:the_top\".\n\
   14588   1.8  christos \n\
   14589   1.9  christos Address locations begin with \"*\" and specify an exact address in the\n\
   14590   1.6  christos program.  Example: To specify the fourth byte past the start function\n\
   14591   1.1  christos \"main\", use \"*main + 4\".\n\
   14592   1.1  christos \n\
   14593   1.1  christos Explicit locations are similar to linespecs but use an option/argument\n\
   14594   1.1  christos syntax to specify location parameters.\n\
   14595   1.6  christos Example: To specify the start of the label named \"the_top\" in the\n\
   14596   1.1  christos function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
   14597  1.10  christos -function fact -label the_top\".\n\
   14598  1.10  christos \n\
   14599   1.1  christos By default, a specified function is matched against the program's\n\
   14600   1.1  christos functions in all scopes.  For C++, this means in all namespaces and\n\
   14601  1.11  christos classes.  For Ada, this means in all packages.  E.g., in C++,\n\
   14602   1.5  christos \"func()\" matches \"A::func()\", \"A::B::func()\", etc.  The\n\
   14603   1.6  christos \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
   14604   1.6  christos specified name as a complete fully-qualified name instead."
   14605   1.6  christos 
   14606   1.1  christos /* This help string is used for the break, hbreak, tbreak and thbreak
   14607   1.1  christos    commands.  It is defined as a macro to prevent duplication.
   14608   1.1  christos    COMMAND should be a string constant containing the name of the
   14609   1.1  christos    command.  */
   14610   1.1  christos 
   14611  1.10  christos #define BREAK_ARGS_HELP(command) \
   14612  1.10  christos command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
   14613  1.10  christos \t[-force-condition] [if CONDITION]\n\
   14614  1.10  christos PROBE_MODIFIER shall be present if the command is to be placed in a\n\
   14615   1.1  christos probe point.  Accepted values are `-probe' (for a generic, automatically\n\
   14616   1.1  christos guessed probe type), `-probe-stap' (for a SystemTap probe) or\n\
   14617   1.1  christos `-probe-dtrace' (for a DTrace probe).\n\
   14618   1.1  christos LOCATION may be a linespec, address, or explicit location as described\n\
   14619   1.1  christos below.\n\
   14620   1.1  christos \n\
   14621   1.1  christos With no LOCATION, uses current execution address of the selected\n\
   14622   1.1  christos stack frame.  This is useful for breaking on return to a stack frame.\n\
   14623   1.1  christos \n\
   14624   1.1  christos THREADNUM is the number from \"info threads\".\n\
   14625   1.1  christos CONDITION is a boolean expression.\n\
   14626   1.1  christos \n\
   14627   1.7  christos With the \"-force-condition\" flag, the condition is defined even when\n\
   14628  1.10  christos it is invalid for all current locations.\n\
   14629   1.1  christos \n" LOCATION_SPEC_HELP_STRING "\n\n\
   14630   1.1  christos Multiple breakpoints at one place are permitted, and useful if their\n\
   14631   1.1  christos conditions are different.\n\
   14632   1.1  christos \n\
   14633   1.1  christos Do \"help breakpoints\" for info on other commands dealing with breakpoints."
   14634   1.1  christos 
   14635   1.8  christos /* List of subcommands for "catch".  */
   14636   1.1  christos static struct cmd_list_element *catch_cmdlist;
   14637  1.10  christos 
   14638  1.10  christos /* List of subcommands for "tcatch".  */
   14639   1.1  christos static struct cmd_list_element *tcatch_cmdlist;
   14640   1.1  christos 
   14641   1.8  christos void
   14642   1.1  christos add_catch_command (const char *name, const char *docstring,
   14643  1.10  christos 		   cmd_func_ftype *func,
   14644  1.10  christos 		   completer_ftype *completer,
   14645   1.1  christos 		   void *user_data_catch,
   14646   1.1  christos 		   void *user_data_tcatch)
   14647   1.1  christos {
   14648  1.10  christos   struct cmd_list_element *command;
   14649  1.10  christos 
   14650   1.1  christos   command = add_cmd (name, class_breakpoint, docstring,
   14651  1.10  christos 		     &catch_cmdlist);
   14652   1.1  christos   command->func = func;
   14653   1.1  christos   command->set_context (user_data_catch);
   14654   1.1  christos   set_cmd_completer (command, completer);
   14655   1.1  christos 
   14656  1.10  christos   command = add_cmd (name, class_breakpoint, docstring,
   14657   1.1  christos 		     &tcatch_cmdlist);
   14658   1.1  christos   command->func = func;
   14659   1.1  christos   command->set_context (user_data_tcatch);
   14660   1.1  christos   set_cmd_completer (command, completer);
   14661   1.1  christos }
   14662   1.1  christos 
   14663   1.8  christos /* False if any of the breakpoint's locations could be a location where
   14664  1.10  christos    functions have been inlined, true otherwise.  */
   14665   1.1  christos 
   14666  1.11  christos static bool
   14667   1.1  christos is_non_inline_function (struct breakpoint *b)
   14668  1.11  christos {
   14669   1.1  christos   /* The shared library event breakpoint is set on the address of a
   14670   1.1  christos      non-inline function.  */
   14671  1.11  christos   return (b->type == bp_shlib_event);
   14672   1.1  christos }
   14673  1.11  christos 
   14674  1.11  christos /* Nonzero if the specified PC cannot be a location where functions
   14675   1.1  christos    have been inlined.  */
   14676   1.1  christos 
   14677   1.1  christos int
   14678   1.1  christos pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
   14679   1.1  christos 			   const target_waitstatus &ws)
   14680   1.1  christos {
   14681   1.1  christos   for (breakpoint &b : all_breakpoints ())
   14682   1.1  christos     {
   14683   1.1  christos       if (!is_non_inline_function (&b))
   14684   1.1  christos 	continue;
   14685   1.1  christos 
   14686   1.1  christos       for (bp_location &bl : b.locations ())
   14687  1.10  christos 	{
   14688  1.10  christos 	  if (!bl.shlib_disabled
   14689   1.1  christos 	      && bpstat_check_location (&bl, aspace, pc, ws))
   14690   1.1  christos 	    return 1;
   14691   1.1  christos 	}
   14692   1.1  christos     }
   14693   1.1  christos 
   14694   1.1  christos   return 0;
   14695   1.1  christos }
   14696   1.8  christos 
   14697   1.8  christos /* Remove any references to OBJFILE which is going to be freed.  */
   14698   1.8  christos 
   14699   1.8  christos void
   14700   1.9  christos breakpoint_free_objfile (struct objfile *objfile)
   14701   1.1  christos {
   14702   1.9  christos   for (bp_location *loc : all_bp_locations ())
   14703   1.1  christos     if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
   14704   1.1  christos       loc->symtab = NULL;
   14705   1.1  christos }
   14706  1.10  christos 
   14707  1.10  christos /* Chain containing all defined "enable breakpoint" subcommands.  */
   14708  1.10  christos 
   14709  1.10  christos static struct cmd_list_element *enablebreaklist = NULL;
   14710  1.10  christos 
   14711  1.10  christos /* See breakpoint.h.  */
   14712   1.1  christos 
   14713   1.1  christos cmd_list_element *commands_cmd_element = nullptr;
   14714   1.1  christos 
   14715   1.1  christos void _initialize_breakpoint ();
   14716   1.1  christos void
   14717   1.1  christos _initialize_breakpoint ()
   14718   1.1  christos {
   14719   1.1  christos   struct cmd_list_element *c;
   14720   1.1  christos 
   14721   1.1  christos   gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
   14722   1.1  christos 					 "breakpoint");
   14723   1.8  christos   gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
   14724   1.8  christos 				       "breakpoint");
   14725   1.7  christos   gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
   14726   1.7  christos 					 "breakpoint");
   14727   1.7  christos 
   14728   1.7  christos   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
   14729   1.1  christos      before a breakpoint is set.  */
   14730   1.1  christos   breakpoint_count = 0;
   14731   1.1  christos 
   14732   1.1  christos   tracepoint_count = 0;
   14733   1.1  christos 
   14734   1.1  christos   add_com ("ignore", class_breakpoint, ignore_command, _("\
   14735  1.10  christos Set ignore-count of breakpoint number N to COUNT.\n\
   14736  1.10  christos Usage is `ignore N COUNT'."));
   14737  1.10  christos 
   14738   1.1  christos   commands_cmd_element = add_com ("commands", class_breakpoint,
   14739  1.10  christos 				  commands_command, _("\
   14740  1.10  christos Set commands to be executed when the given breakpoints are hit.\n\
   14741  1.10  christos Give a space-separated breakpoint list as argument after \"commands\".\n\
   14742  1.10  christos A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
   14743  1.10  christos (e.g. `5-7').\n\
   14744  1.10  christos With no argument, the targeted breakpoint is the last one set.\n\
   14745  1.10  christos The commands themselves follow starting on the next line.\n\
   14746  1.10  christos Type a line containing \"end\" to indicate the end of them.\n\
   14747  1.10  christos Give \"silent\" as the first line to make the breakpoint silent;\n\
   14748   1.1  christos then no output is printed when it is hit, except what the commands print."));
   14749   1.1  christos 
   14750   1.1  christos   const auto cc_opts = make_condition_command_options_def_group (nullptr);
   14751   1.1  christos   static std::string condition_command_help
   14752   1.1  christos     = gdb::option::build_help (_("\
   14753   1.1  christos Specify breakpoint number N to break only if COND is true.\n\
   14754   1.1  christos Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
   14755   1.1  christos is an expression to be evaluated whenever breakpoint N is reached.\n\
   14756   1.1  christos \n\
   14757   1.1  christos Options:\n\
   14758   1.1  christos %OPTIONS%"), cc_opts);
   14759   1.1  christos 
   14760   1.1  christos   c = add_com ("condition", class_breakpoint, condition_command,
   14761   1.1  christos 	       condition_command_help.c_str ());
   14762   1.1  christos   set_cmd_completer_handle_brkchars (c, condition_completer);
   14763   1.1  christos 
   14764   1.1  christos   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
   14765   1.1  christos Set a temporary breakpoint.\n\
   14766   1.1  christos Like \"break\" except the breakpoint is only temporary,\n\
   14767   1.1  christos so it will be deleted when hit.  Equivalent to \"break\" followed\n\
   14768   1.1  christos by using \"enable delete\" on the breakpoint number.\n\
   14769   1.1  christos \n"
   14770   1.1  christos BREAK_ARGS_HELP ("tbreak")));
   14771   1.1  christos   set_cmd_completer (c, location_completer);
   14772   1.1  christos 
   14773   1.1  christos   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
   14774  1.10  christos Set a hardware assisted breakpoint.\n\
   14775  1.10  christos Like \"break\" except the breakpoint requires hardware support,\n\
   14776   1.9  christos some target hardware may not have this support.\n\
   14777   1.9  christos \n"
   14778   1.1  christos BREAK_ARGS_HELP ("hbreak")));
   14779   1.1  christos   set_cmd_completer (c, location_completer);
   14780   1.1  christos 
   14781   1.1  christos   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
   14782  1.10  christos Set a temporary hardware assisted breakpoint.\n\
   14783   1.1  christos Like \"hbreak\" except the breakpoint is only temporary,\n\
   14784  1.10  christos so it will be deleted when hit.\n\
   14785   1.1  christos \n"
   14786   1.1  christos BREAK_ARGS_HELP ("thbreak")));
   14787   1.9  christos   set_cmd_completer (c, location_completer);
   14788   1.9  christos 
   14789   1.1  christos   cmd_list_element *enable_cmd
   14790   1.1  christos     = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
   14791   1.9  christos Enable all or some breakpoints.\n\
   14792  1.10  christos Usage: enable [BREAKPOINTNUM]...\n\
   14793   1.1  christos Give breakpoint numbers (separated by spaces) as arguments.\n\
   14794   1.1  christos With no subcommand, breakpoints are enabled until you command otherwise.\n\
   14795   1.9  christos This is used to cancel the effect of the \"disable\" command.\n\
   14796   1.9  christos With a subcommand you can enable temporarily."),
   14797   1.1  christos 		      &enablelist, 1, &cmdlist);
   14798   1.1  christos 
   14799   1.1  christos   add_com_alias ("en", enable_cmd, class_breakpoint, 1);
   14800   1.1  christos 
   14801   1.9  christos   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
   14802   1.9  christos Enable all or some breakpoints.\n\
   14803   1.1  christos Usage: enable breakpoints [BREAKPOINTNUM]...\n\
   14804   1.1  christos Give breakpoint numbers (separated by spaces) as arguments.\n\
   14805   1.1  christos This is used to cancel the effect of the \"disable\" command.\n\
   14806   1.1  christos May be abbreviated to simply \"enable\"."),
   14807   1.9  christos 		   &enablebreaklist, 1, &enablelist);
   14808   1.9  christos 
   14809   1.1  christos   add_cmd ("once", no_class, enable_once_command, _("\
   14810   1.1  christos Enable some breakpoints for one hit.\n\
   14811   1.1  christos Usage: enable breakpoints once BREAKPOINTNUM...\n\
   14812   1.1  christos If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
   14813   1.1  christos 	   &enablebreaklist);
   14814   1.9  christos 
   14815   1.9  christos   add_cmd ("delete", no_class, enable_delete_command, _("\
   14816   1.1  christos Enable some breakpoints and delete when hit.\n\
   14817   1.1  christos Usage: enable breakpoints delete BREAKPOINTNUM...\n\
   14818   1.1  christos If a breakpoint is hit while enabled in this fashion, it is deleted."),
   14819   1.1  christos 	   &enablebreaklist);
   14820   1.9  christos 
   14821   1.9  christos   add_cmd ("count", no_class, enable_count_command, _("\
   14822   1.1  christos Enable some breakpoints for COUNT hits.\n\
   14823   1.1  christos Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
   14824   1.1  christos If a breakpoint is hit while enabled in this fashion,\n\
   14825   1.1  christos the count is decremented; when it reaches zero, the breakpoint is disabled."),
   14826   1.9  christos 	   &enablebreaklist);
   14827   1.9  christos 
   14828   1.1  christos   add_cmd ("delete", no_class, enable_delete_command, _("\
   14829   1.1  christos Enable some breakpoints and delete when hit.\n\
   14830   1.1  christos Usage: enable delete BREAKPOINTNUM...\n\
   14831   1.1  christos If a breakpoint is hit while enabled in this fashion, it is deleted."),
   14832  1.10  christos 	   &enablelist);
   14833  1.10  christos 
   14834   1.9  christos   add_cmd ("once", no_class, enable_once_command, _("\
   14835   1.9  christos Enable some breakpoints for one hit.\n\
   14836   1.1  christos Usage: enable once BREAKPOINTNUM...\n\
   14837   1.1  christos If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
   14838   1.1  christos 	   &enablelist);
   14839  1.10  christos 
   14840  1.10  christos   add_cmd ("count", no_class, enable_count_command, _("\
   14841  1.10  christos Enable some breakpoints for COUNT hits.\n\
   14842   1.1  christos Usage: enable count COUNT BREAKPOINTNUM...\n\
   14843   1.9  christos If a breakpoint is hit while enabled in this fashion,\n\
   14844   1.9  christos the count is decremented; when it reaches zero, the breakpoint is disabled."),
   14845   1.9  christos 	   &enablelist);
   14846   1.1  christos 
   14847   1.1  christos   cmd_list_element *disable_cmd
   14848   1.1  christos     = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
   14849   1.1  christos Disable all or some breakpoints.\n\
   14850   1.1  christos Usage: disable [BREAKPOINTNUM]...\n\
   14851   1.1  christos Arguments are breakpoint numbers with spaces in between.\n\
   14852  1.10  christos To disable all breakpoints, give no argument.\n\
   14853  1.10  christos A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
   14854   1.9  christos 		      &disablelist, 1, &cmdlist);
   14855   1.9  christos   add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
   14856   1.1  christos   add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
   14857  1.11  christos 
   14858  1.11  christos   add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
   14859   1.1  christos Disable all or some breakpoints.\n\
   14860   1.9  christos Usage: disable breakpoints [BREAKPOINTNUM]...\n\
   14861  1.10  christos Arguments are breakpoint numbers with spaces in between.\n\
   14862  1.10  christos To disable all breakpoints, give no argument.\n\
   14863  1.10  christos A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
   14864   1.1  christos This command may be abbreviated \"disable\"."),
   14865   1.9  christos 	   &disablelist);
   14866   1.9  christos 
   14867   1.9  christos   cmd_list_element *delete_cmd
   14868   1.1  christos     = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
   14869  1.11  christos Delete all or some breakpoints.\n\
   14870  1.11  christos Usage: delete [BREAKPOINTNUM]...\n\
   14871   1.1  christos Arguments are breakpoint numbers with spaces in between.\n\
   14872   1.1  christos To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
   14873   1.1  christos give no argument.\n\
   14874  1.10  christos \n\
   14875  1.10  christos Also a prefix command for deletion of other GDB objects."),
   14876   1.6  christos 		      &deletelist, 1, &cmdlist);
   14877   1.6  christos   add_com_alias ("d", delete_cmd, class_breakpoint, 1);
   14878   1.1  christos   add_com_alias ("del", delete_cmd, class_breakpoint, 1);
   14879   1.1  christos 
   14880   1.6  christos   add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
   14881  1.10  christos Delete all or some breakpoints or auto-display expressions.\n\
   14882   1.1  christos Usage: delete breakpoints [BREAKPOINTNUM]...\n\
   14883  1.10  christos Arguments are breakpoint numbers with spaces in between.\n\
   14884   1.1  christos To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
   14885  1.10  christos give no argument.\n\
   14886  1.10  christos This command may be abbreviated \"delete\"."),
   14887   1.6  christos 	   &deletelist);
   14888   1.1  christos 
   14889  1.10  christos   cmd_list_element *clear_cmd
   14890   1.1  christos    = add_com ("clear", class_breakpoint, clear_command, _("\
   14891  1.10  christos Clear breakpoint at specified location.\n\
   14892  1.10  christos Argument may be a linespec, explicit, or address location as described below.\n\
   14893  1.10  christos \n\
   14894  1.10  christos With no argument, clears all breakpoints in the line that the selected frame\n\
   14895   1.1  christos is executing in.\n"
   14896  1.10  christos "\n" LOCATION_SPEC_HELP_STRING "\n\n\
   14897  1.10  christos See also the \"delete\" command which clears breakpoints by number."));
   14898   1.1  christos   add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
   14899   1.1  christos 
   14900   1.1  christos   cmd_list_element *break_cmd
   14901   1.1  christos     = add_com ("break", class_breakpoint, break_command, _("\
   14902   1.1  christos Set breakpoint at specified location.\n"
   14903   1.1  christos BREAK_ARGS_HELP ("break")));
   14904   1.1  christos   set_cmd_completer (break_cmd, location_completer);
   14905   1.1  christos 
   14906   1.1  christos   add_com_alias ("b", break_cmd, class_run, 1);
   14907   1.1  christos   add_com_alias ("br", break_cmd, class_run, 1);
   14908   1.1  christos   add_com_alias ("bre", break_cmd, class_run, 1);
   14909   1.1  christos   add_com_alias ("brea", break_cmd, class_run, 1);
   14910   1.1  christos 
   14911   1.1  christos   cmd_list_element *info_breakpoints_cmd
   14912   1.1  christos     = add_info ("breakpoints", info_breakpoints_command, _("\
   14913  1.10  christos Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
   14914   1.1  christos The \"Type\" column indicates one of:\n\
   14915   1.1  christos \tbreakpoint     - normal breakpoint\n\
   14916   1.1  christos \twatchpoint     - watchpoint\n\
   14917   1.1  christos The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
   14918   1.1  christos the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
   14919   1.1  christos breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
   14920   1.1  christos address and file/line number respectively.\n\
   14921   1.1  christos \n\
   14922   1.1  christos Convenience variable \"$_\" and default examine address for \"x\"\n\
   14923   1.1  christos are set to the address of the last breakpoint listed unless the command\n\
   14924   1.1  christos is prefixed with \"server \".\n\n\
   14925   1.1  christos Convenience variable \"$bpnum\" contains the number of the last\n\
   14926   1.1  christos breakpoint set."));
   14927   1.1  christos 
   14928   1.1  christos   add_info_alias ("b", info_breakpoints_cmd, 1);
   14929   1.1  christos 
   14930   1.1  christos   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
   14931   1.1  christos Status of all breakpoints, or breakpoint number NUMBER.\n\
   14932   1.1  christos The \"Type\" column indicates one of:\n\
   14933   1.1  christos \tbreakpoint     - normal breakpoint\n\
   14934   1.1  christos \twatchpoint     - watchpoint\n\
   14935   1.1  christos \tlongjmp        - internal breakpoint used to step through longjmp()\n\
   14936   1.9  christos \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
   14937   1.1  christos \tuntil          - internal breakpoint used by the \"until\" command\n\
   14938  1.10  christos \tfinish         - internal breakpoint used by the \"finish\" command\n\
   14939   1.9  christos The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
   14940   1.1  christos the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
   14941   1.9  christos breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
   14942   1.1  christos address and file/line number respectively.\n\
   14943  1.10  christos \n\
   14944   1.9  christos Convenience variable \"$_\" and default examine address for \"x\"\n\
   14945   1.1  christos are set to the address of the last breakpoint listed unless the command\n\
   14946  1.10  christos is prefixed with \"server \".\n\n\
   14947  1.10  christos Convenience variable \"$bpnum\" contains the number of the last\n\
   14948  1.10  christos breakpoint set."),
   14949  1.10  christos 	   &maintenanceinfolist);
   14950  1.10  christos 
   14951  1.10  christos   add_basic_prefix_cmd ("catch", class_breakpoint, _("\
   14952  1.10  christos Set catchpoints to catch events."),
   14953  1.10  christos 			&catch_cmdlist,
   14954  1.10  christos 			0/*allow-unknown*/, &cmdlist);
   14955   1.1  christos 
   14956  1.10  christos   add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
   14957  1.10  christos Set temporary catchpoints to catch events."),
   14958  1.10  christos 			&tcatch_cmdlist,
   14959  1.10  christos 			0/*allow-unknown*/, &cmdlist);
   14960  1.10  christos 
   14961  1.10  christos   const auto opts = make_watch_options_def_group (nullptr);
   14962  1.10  christos 
   14963  1.10  christos   static const std::string watch_help = gdb::option::build_help (_("\
   14964  1.10  christos Set a watchpoint for EXPRESSION.\n\
   14965  1.10  christos Usage: watch [-location] EXPRESSION\n\
   14966  1.10  christos \n\
   14967  1.10  christos Options:\n\
   14968  1.10  christos %OPTIONS%\n\
   14969  1.10  christos \n\
   14970  1.10  christos A watchpoint stops execution of your program whenever the value of\n\
   14971  1.10  christos an expression changes."), opts);
   14972  1.10  christos   c = add_com ("watch", class_breakpoint, watch_command,
   14973  1.10  christos 	       watch_help.c_str ());
   14974  1.10  christos   set_cmd_completer_handle_brkchars (c, watch_command_completer);
   14975  1.10  christos 
   14976  1.10  christos   static const std::string rwatch_help = gdb::option::build_help (_("\
   14977  1.10  christos Set a read watchpoint for EXPRESSION.\n\
   14978  1.10  christos Usage: rwatch [-location] EXPRESSION\n\
   14979  1.10  christos \n\
   14980  1.10  christos Options:\n\
   14981  1.10  christos %OPTIONS%\n\
   14982  1.10  christos \n\
   14983  1.10  christos A read watchpoint stops execution of your program whenever the value of\n\
   14984  1.10  christos an expression is read."), opts);
   14985  1.10  christos   c = add_com ("rwatch", class_breakpoint, rwatch_command,
   14986   1.1  christos 	       rwatch_help.c_str ());
   14987   1.8  christos   set_cmd_completer_handle_brkchars (c, watch_command_completer);
   14988   1.1  christos 
   14989   1.1  christos   static const std::string awatch_help = gdb::option::build_help (_("\
   14990   1.1  christos Set an access watchpoint for EXPRESSION.\n\
   14991   1.1  christos Usage: awatch [-location] EXPRESSION\n\
   14992   1.1  christos \n\
   14993   1.1  christos Options:\n\
   14994   1.1  christos %OPTIONS%\n\
   14995   1.1  christos \n\
   14996   1.1  christos An access watchpoint stops execution of your program whenever the value\n\
   14997   1.1  christos of an expression is either read or written."), opts);
   14998   1.1  christos   c = add_com ("awatch", class_breakpoint, awatch_command,
   14999   1.1  christos 	       awatch_help.c_str ());
   15000   1.1  christos   set_cmd_completer_handle_brkchars (c, watch_command_completer);
   15001   1.1  christos 
   15002   1.1  christos   add_info ("watchpoints", info_watchpoints_command, _("\
   15003   1.1  christos Status of specified watchpoints (all watchpoints if no argument)."));
   15004   1.1  christos 
   15005   1.1  christos   /* XXX: cagney/2005-02-23: This should be a boolean, and should
   15006   1.1  christos      respond to changes - contrary to the description.  */
   15007   1.1  christos   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
   15008  1.10  christos 			    &can_use_hw_watchpoints, _("\
   15009  1.10  christos Set debugger's willingness to use watchpoint hardware."), _("\
   15010   1.6  christos Show debugger's willingness to use watchpoint hardware."), _("\
   15011   1.1  christos If zero, gdb will not use hardware for new watchpoints, even if\n\
   15012   1.1  christos such is available.  (However, any hardware watchpoints that were\n\
   15013   1.1  christos created before setting this to nonzero, will continue to use watchpoint\n\
   15014  1.10  christos hardware.)"),
   15015   1.1  christos 			    NULL,
   15016  1.10  christos 			    show_can_use_hw_watchpoints,
   15017  1.10  christos 			    &setlist, &showlist);
   15018  1.10  christos 
   15019  1.10  christos   can_use_hw_watchpoints = 1;
   15020   1.1  christos 
   15021   1.1  christos   /* Tracepoint manipulation commands.  */
   15022   1.6  christos 
   15023   1.1  christos   cmd_list_element *trace_cmd
   15024   1.1  christos     = add_com ("trace", class_breakpoint, trace_command, _("\
   15025   1.1  christos Set a tracepoint at specified location.\n\
   15026   1.1  christos \n"
   15027   1.1  christos BREAK_ARGS_HELP ("trace") "\n\
   15028   1.1  christos Do \"help tracepoints\" for info on other tracepoint commands."));
   15029   1.6  christos   set_cmd_completer (trace_cmd, location_completer);
   15030   1.1  christos 
   15031   1.1  christos   add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
   15032  1.11  christos   add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
   15033   1.6  christos   add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
   15034   1.6  christos   add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
   15035   1.6  christos 
   15036   1.1  christos   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
   15037   1.1  christos Set a fast tracepoint at specified location.\n\
   15038   1.1  christos \n"
   15039   1.1  christos BREAK_ARGS_HELP ("ftrace") "\n\
   15040   1.1  christos Do \"help tracepoints\" for info on other tracepoint commands."));
   15041   1.1  christos   set_cmd_completer (c, location_completer);
   15042  1.10  christos 
   15043   1.1  christos   c = add_com ("strace", class_breakpoint, strace_command, _("\
   15044   1.1  christos Set a static tracepoint at location or marker.\n\
   15045   1.1  christos \n\
   15046   1.1  christos strace [LOCATION] [if CONDITION]\n\
   15047   1.1  christos LOCATION may be a linespec, explicit, or address location (described below)\n\
   15048   1.1  christos or -m MARKER_ID.\n\n\
   15049   1.1  christos If a marker id is specified, probe the marker with that name.  With\n\
   15050  1.10  christos no LOCATION, uses current execution address of the selected stack frame.\n\
   15051  1.10  christos Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
   15052   1.1  christos This collects arbitrary user data passed in the probe point call to the\n\
   15053   1.1  christos tracing library.  You can inspect it when analyzing the trace buffer,\n\
   15054   1.1  christos by printing the $_sdata variable like any other convenience variable.\n\
   15055   1.1  christos \n\
   15056  1.10  christos CONDITION is a boolean expression.\n\
   15057   1.1  christos \n" LOCATION_SPEC_HELP_STRING "\n\n\
   15058  1.10  christos Multiple tracepoints at one place are permitted, and useful if their\n\
   15059  1.10  christos conditions are different.\n\
   15060   1.1  christos \n\
   15061   1.1  christos Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
   15062   1.1  christos Do \"help tracepoints\" for info on other tracepoint commands."));
   15063   1.1  christos   set_cmd_completer (c, location_completer);
   15064  1.10  christos 
   15065   1.1  christos   cmd_list_element *info_tracepoints_cmd
   15066   1.1  christos     = add_info ("tracepoints", info_tracepoints_command, _("\
   15067   1.1  christos Status of specified tracepoints (all tracepoints if no argument).\n\
   15068   1.1  christos Convenience variable \"$tpnum\" contains the number of the\n\
   15069   1.1  christos last tracepoint set."));
   15070   1.1  christos 
   15071   1.1  christos   add_info_alias ("tp", info_tracepoints_cmd, 1);
   15072   1.1  christos 
   15073   1.1  christos   cmd_list_element *delete_tracepoints_cmd
   15074   1.1  christos     = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
   15075   1.1  christos Delete specified tracepoints.\n\
   15076   1.1  christos Arguments are tracepoint numbers, separated by spaces.\n\
   15077   1.1  christos No argument means delete all tracepoints."),
   15078   1.1  christos 	   &deletelist);
   15079   1.1  christos   add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
   15080   1.1  christos 
   15081   1.1  christos   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
   15082   1.1  christos Disable specified tracepoints.\n\
   15083   1.1  christos Arguments are tracepoint numbers, separated by spaces.\n\
   15084   1.1  christos No argument means disable all tracepoints."),
   15085   1.1  christos 	   &disablelist);
   15086   1.9  christos   deprecate_cmd (c, "disable");
   15087   1.9  christos 
   15088  1.10  christos   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
   15089   1.9  christos Enable specified tracepoints.\n\
   15090   1.1  christos Arguments are tracepoint numbers, separated by spaces.\n\
   15091   1.1  christos No argument means enable all tracepoints."),
   15092   1.1  christos 	   &enablelist);
   15093   1.1  christos   deprecate_cmd (c, "enable");
   15094   1.1  christos 
   15095   1.1  christos   add_com ("passcount", class_trace, trace_pass_command, _("\
   15096   1.1  christos Set the passcount for a tracepoint.\n\
   15097   1.1  christos The trace will end when the tracepoint has been passed 'count' times.\n\
   15098   1.1  christos Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
   15099  1.10  christos if TPNUM is omitted, passcount refers to the last tracepoint defined."));
   15100  1.10  christos 
   15101   1.1  christos   add_basic_prefix_cmd ("save", class_breakpoint,
   15102   1.1  christos 			_("Save breakpoint definitions as a script."),
   15103   1.1  christos 			&save_cmdlist,
   15104  1.10  christos 			0/*allow-unknown*/, &cmdlist);
   15105   1.1  christos 
   15106  1.10  christos   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
   15107   1.1  christos Save current breakpoint definitions as a script.\n\
   15108   1.1  christos This includes all types of breakpoints (breakpoints, watchpoints,\n\
   15109  1.10  christos catchpoints, tracepoints).  Use the 'source' command in another debug\n\
   15110  1.10  christos session to restore them."),
   15111   1.9  christos 	       &save_cmdlist);
   15112   1.1  christos   set_cmd_completer (c, filename_completer);
   15113   1.9  christos 
   15114  1.10  christos   cmd_list_element *save_tracepoints_cmd
   15115   1.9  christos     = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
   15116   1.1  christos Save current tracepoint definitions as a script.\n\
   15117   1.9  christos Use the 'source' command in another debug session to restore them."),
   15118  1.10  christos 	       &save_cmdlist);
   15119  1.10  christos   set_cmd_completer (save_tracepoints_cmd, filename_completer);
   15120   1.1  christos 
   15121   1.1  christos   c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
   15122   1.1  christos   deprecate_cmd (c, "save tracepoints");
   15123   1.1  christos 
   15124   1.1  christos   add_setshow_prefix_cmd ("breakpoint", class_maintenance,
   15125   1.1  christos 			  _("\
   15126   1.1  christos Breakpoint specific settings.\n\
   15127   1.1  christos Configure various breakpoint-specific variables such as\n\
   15128   1.1  christos pending breakpoint behavior."),
   15129   1.1  christos 			  _("\
   15130   1.1  christos Breakpoint specific settings.\n\
   15131   1.1  christos Configure various breakpoint-specific variables such as\n\
   15132   1.1  christos pending breakpoint behavior."),
   15133   1.1  christos 			  &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
   15134   1.1  christos 			  &setlist, &showlist);
   15135   1.1  christos 
   15136   1.1  christos   add_setshow_auto_boolean_cmd ("pending", no_class,
   15137   1.1  christos 				&pending_break_support, _("\
   15138   1.1  christos Set debugger's behavior regarding pending breakpoints."), _("\
   15139   1.1  christos Show debugger's behavior regarding pending breakpoints."), _("\
   15140   1.1  christos If on, an unrecognized breakpoint location will cause gdb to create a\n\
   15141   1.1  christos pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
   15142   1.1  christos an error.  If auto, an unrecognized breakpoint location results in a\n\
   15143   1.1  christos user-query to see if a pending breakpoint should be created."),
   15144   1.1  christos 				NULL,
   15145   1.1  christos 				show_pending_break_support,
   15146   1.1  christos 				&breakpoint_set_cmdlist,
   15147   1.1  christos 				&breakpoint_show_cmdlist);
   15148   1.3  christos 
   15149   1.3  christos   pending_break_support = AUTO_BOOLEAN_AUTO;
   15150   1.1  christos 
   15151   1.1  christos   add_setshow_boolean_cmd ("auto-hw", no_class,
   15152   1.3  christos 			   &automatic_hardware_breakpoints, _("\
   15153   1.3  christos Set automatic usage of hardware breakpoints."), _("\
   15154   1.3  christos Show automatic usage of hardware breakpoints."), _("\
   15155   1.3  christos If set, the debugger will automatically use hardware breakpoints for\n\
   15156   1.3  christos breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
   15157   1.1  christos a warning will be emitted for such breakpoints."),
   15158   1.1  christos 			   NULL,
   15159   1.1  christos 			   show_automatic_hardware_breakpoints,
   15160   1.1  christos 			   &breakpoint_set_cmdlist,
   15161   1.1  christos 			   &breakpoint_show_cmdlist);
   15162  1.11  christos 
   15163  1.11  christos   add_setshow_boolean_cmd ("always-inserted", class_support,
   15164  1.11  christos 			   &always_inserted_mode, _("\
   15165  1.11  christos Set mode for inserting breakpoints."), _("\
   15166  1.11  christos Show mode for inserting breakpoints."), _("\
   15167  1.11  christos When this mode is on, breakpoints are inserted immediately as soon as\n\
   15168  1.11  christos they're created, kept inserted even when execution stops, and removed\n\
   15169  1.11  christos only when the user deletes them.  When this mode is off (the default),\n\
   15170  1.11  christos breakpoints are inserted only when execution continues, and removed\n\
   15171   1.1  christos when execution stops."),
   15172   1.1  christos 				NULL,
   15173   1.1  christos 				&show_always_inserted_mode,
   15174   1.1  christos 				&breakpoint_set_cmdlist,
   15175   1.1  christos 				&breakpoint_show_cmdlist);
   15176   1.1  christos 
   15177   1.1  christos   add_setshow_boolean_cmd ("breakpoint", class_maintenance,
   15178   1.1  christos 			   &debug_breakpoint, _("\
   15179   1.1  christos Set breakpoint location debugging."), _("\
   15180   1.1  christos Show breakpoint location debugging."), _("\
   15181   1.1  christos When on, breakpoint location specific debugging is enabled."),
   15182   1.9  christos 			   NULL,
   15183   1.1  christos 			   show_debug_breakpoint,
   15184   1.1  christos 			   &setdebuglist, &showdebuglist);
   15185   1.1  christos 
   15186   1.1  christos   add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
   15187   1.1  christos 			condition_evaluation_enums,
   15188   1.1  christos 			&condition_evaluation_mode_1, _("\
   15189   1.1  christos Set mode of breakpoint condition evaluation."), _("\
   15190   1.1  christos Show mode of breakpoint condition evaluation."), _("\
   15191   1.1  christos When this is set to \"host\", breakpoint conditions will be\n\
   15192   1.1  christos evaluated on the host's side by GDB.  When it is set to \"target\",\n\
   15193   1.1  christos breakpoint conditions will be downloaded to the target (if the target\n\
   15194   1.1  christos supports such feature) and conditions will be evaluated on the target's side.\n\
   15195  1.10  christos If this is set to \"auto\" (default), this will be automatically set to\n\
   15196   1.1  christos \"target\" if it supports condition evaluation, otherwise it will\n\
   15197   1.1  christos be set to \"host\"."),
   15198   1.1  christos 			   &set_condition_evaluation_mode,
   15199   1.1  christos 			   &show_condition_evaluation_mode,
   15200   1.1  christos 			   &breakpoint_set_cmdlist,
   15201   1.1  christos 			   &breakpoint_show_cmdlist);
   15202   1.1  christos 
   15203   1.1  christos   add_com ("break-range", class_breakpoint, break_range_command, _("\
   15204   1.1  christos Set a breakpoint for an address range.\n\
   15205   1.6  christos break-range START-LOCATION, END-LOCATION\n\
   15206   1.1  christos where START-LOCATION and END-LOCATION can be one of the following:\n\
   15207   1.6  christos   LINENUM, for that line in the current file,\n\
   15208  1.10  christos   FILE:LINENUM, for that line in that file,\n\
   15209   1.1  christos   +OFFSET, for that number of lines after the current line\n\
   15210   1.1  christos 	   or the start of the range\n\
   15211   1.1  christos   FUNCTION, for the first line in that function,\n\
   15212   1.1  christos   FILE:FUNCTION, to distinguish among like-named static functions.\n\
   15213   1.1  christos   *ADDRESS, for the instruction at that address.\n\
   15214   1.1  christos \n\
   15215   1.1  christos The breakpoint will stop execution of the inferior whenever it executes\n\
   15216   1.1  christos an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
   15217   1.1  christos range (including START-LOCATION and END-LOCATION)."));
   15218   1.1  christos 
   15219   1.1  christos   c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
   15220   1.1  christos Set a dynamic printf at specified location.\n\
   15221   1.1  christos dprintf location,format string,arg1,arg2,...\n\
   15222   1.1  christos location may be a linespec, explicit, or address location.\n"
   15223   1.1  christos "\n" LOCATION_SPEC_HELP_STRING));
   15224   1.1  christos   set_cmd_completer (c, location_completer);
   15225   1.1  christos 
   15226   1.9  christos   add_setshow_enum_cmd ("dprintf-style", class_support,
   15227   1.9  christos 			dprintf_style_enums, &dprintf_style, _("\
   15228   1.1  christos Set the style of usage for dynamic printf."), _("\
   15229   1.1  christos Show the style of usage for dynamic printf."), _("\
   15230   1.1  christos This setting chooses how GDB will do a dynamic printf.\n\
   15231   1.1  christos If the value is \"gdb\", then the printing is done by GDB to its own\n\
   15232   1.1  christos console, as with the \"printf\" command.\n\
   15233   1.9  christos If the value is \"call\", the print is done by calling a function in your\n\
   15234   1.9  christos program; by default printf(), but you can choose a different function or\n\
   15235   1.1  christos output stream by setting dprintf-function and dprintf-channel."),
   15236   1.1  christos 			update_dprintf_commands, NULL,
   15237   1.1  christos 			&setlist, &showlist);
   15238   1.1  christos 
   15239   1.1  christos   add_setshow_string_cmd ("dprintf-function", class_support,
   15240   1.1  christos 			  &dprintf_function, _("\
   15241   1.1  christos Set the function to use for dynamic printf."), _("\
   15242   1.1  christos Show the function to use for dynamic printf."), NULL,
   15243   1.1  christos 			  update_dprintf_commands, NULL,
   15244   1.1  christos 			  &setlist, &showlist);
   15245   1.1  christos 
   15246   1.1  christos   add_setshow_string_cmd ("dprintf-channel", class_support,
   15247   1.1  christos 			  &dprintf_channel, _("\
   15248   1.1  christos Set the channel to use for dynamic printf."), _("\
   15249   1.9  christos Show the channel to use for dynamic printf."), NULL,
   15250   1.9  christos 			  update_dprintf_commands, NULL,
   15251   1.9  christos 			  &setlist, &showlist);
   15252   1.9  christos 
   15253   1.1  christos   add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
   15254   1.9  christos 			   &disconnected_dprintf, _("\
   15255   1.1  christos Set whether dprintf continues after GDB disconnects."), _("\
   15256  1.10  christos Show whether dprintf continues after GDB disconnects."), _("\
   15257  1.10  christos Use this to let dprintf commands continue to hit and produce output\n\
   15258  1.10  christos even if GDB disconnects or detaches from the target."),
   15259  1.10  christos 			   NULL,
   15260  1.11  christos 			   NULL,
   15261  1.11  christos 			   &setlist, &showlist);
   15262   1.1  christos 
   15263                   add_com ("agent-printf", class_vars, agent_printf_command, _("\
   15264                 Target agent only formatted printing, like the C \"printf\" function.\n\
   15265                 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
   15266                 This supports most C printf format specifications, like %s, %d, etc.\n\
   15267                 This is useful for formatted output in user-defined commands."));
   15268                 
   15269                   automatic_hardware_breakpoints = true;
   15270                 
   15271                   gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
   15272                 					   "breakpoint");
   15273                   gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
   15274                 				      "breakpoint");
   15275                   gdb::observers::inferior_removed.attach (remove_inferior_breakpoints,
   15276                 					   "breakpoint");
   15277                 }
   15278