Home | History | Annotate | Line # | Download | only in gdbserver
mem-break.cc revision 1.1
      1  1.1  christos /* Memory breakpoint operations for the remote server for GDB.
      2  1.1  christos    Copyright (C) 2002-2020 Free Software Foundation, Inc.
      3  1.1  christos 
      4  1.1  christos    Contributed by MontaVista Software.
      5  1.1  christos 
      6  1.1  christos    This file is part of GDB.
      7  1.1  christos 
      8  1.1  christos    This program is free software; you can redistribute it and/or modify
      9  1.1  christos    it under the terms of the GNU General Public License as published by
     10  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     11  1.1  christos    (at your option) any later version.
     12  1.1  christos 
     13  1.1  christos    This program is distributed in the hope that it will be useful,
     14  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  1.1  christos    GNU General Public License for more details.
     17  1.1  christos 
     18  1.1  christos    You should have received a copy of the GNU General Public License
     19  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20  1.1  christos 
     21  1.1  christos #include "server.h"
     22  1.1  christos #include "regcache.h"
     23  1.1  christos #include "ax.h"
     24  1.1  christos 
     25  1.1  christos #define MAX_BREAKPOINT_LEN 8
     26  1.1  christos 
     27  1.1  christos /* Helper macro used in loops that append multiple items to a singly-linked
     28  1.1  christos    list instead of inserting items at the head of the list, as, say, in the
     29  1.1  christos    breakpoint lists.  LISTPP is a pointer to the pointer that is the head of
     30  1.1  christos    the new list.  ITEMP is a pointer to the item to be added to the list.
     31  1.1  christos    TAILP must be defined to be the same type as ITEMP, and initialized to
     32  1.1  christos    NULL.  */
     33  1.1  christos 
     34  1.1  christos #define APPEND_TO_LIST(listpp, itemp, tailp) \
     35  1.1  christos 	  do \
     36  1.1  christos 	    { \
     37  1.1  christos 	      if ((tailp) == NULL) \
     38  1.1  christos 		*(listpp) = (itemp); \
     39  1.1  christos 	      else \
     40  1.1  christos 		(tailp)->next = (itemp); \
     41  1.1  christos 	      (tailp) = (itemp); \
     42  1.1  christos 	    } \
     43  1.1  christos 	  while (0)
     44  1.1  christos 
     45  1.1  christos /* GDB will never try to install multiple breakpoints at the same
     46  1.1  christos    address.  However, we can see GDB requesting to insert a breakpoint
     47  1.1  christos    at an address is had already inserted one previously in a few
     48  1.1  christos    situations.
     49  1.1  christos 
     50  1.1  christos    - The RSP documentation on Z packets says that to avoid potential
     51  1.1  christos    problems with duplicate packets, the operations should be
     52  1.1  christos    implemented in an idempotent way.
     53  1.1  christos 
     54  1.1  christos    - A breakpoint is set at ADDR, an address in a shared library.
     55  1.1  christos    Then the shared library is unloaded.  And then another, unrelated,
     56  1.1  christos    breakpoint at ADDR is set.  There is not breakpoint removal request
     57  1.1  christos    between the first and the second breakpoint.
     58  1.1  christos 
     59  1.1  christos    - When GDB wants to update the target-side breakpoint conditions or
     60  1.1  christos    commands, it re-inserts the breakpoint, with updated
     61  1.1  christos    conditions/commands associated.
     62  1.1  christos 
     63  1.1  christos    Also, we need to keep track of internal breakpoints too, so we do
     64  1.1  christos    need to be able to install multiple breakpoints at the same address
     65  1.1  christos    transparently.
     66  1.1  christos 
     67  1.1  christos    We keep track of two different, and closely related structures.  A
     68  1.1  christos    raw breakpoint, which manages the low level, close to the metal
     69  1.1  christos    aspect of a breakpoint.  It holds the breakpoint address, and for
     70  1.1  christos    software breakpoints, a buffer holding a copy of the instructions
     71  1.1  christos    that would be in memory had not been a breakpoint there (we call
     72  1.1  christos    that the shadow memory of the breakpoint).  We occasionally need to
     73  1.1  christos    temporarilly uninsert a breakpoint without the client knowing about
     74  1.1  christos    it (e.g., to step over an internal breakpoint), so we keep an
     75  1.1  christos    `inserted' state associated with this low level breakpoint
     76  1.1  christos    structure.  There can only be one such object for a given address.
     77  1.1  christos    Then, we have (a bit higher level) breakpoints.  This structure
     78  1.1  christos    holds a callback to be called whenever a breakpoint is hit, a
     79  1.1  christos    high-level type, and a link to a low level raw breakpoint.  There
     80  1.1  christos    can be many high-level breakpoints at the same address, and all of
     81  1.1  christos    them will point to the same raw breakpoint, which is reference
     82  1.1  christos    counted.  */
     83  1.1  christos 
     84  1.1  christos /* The low level, physical, raw breakpoint.  */
     85  1.1  christos struct raw_breakpoint
     86  1.1  christos {
     87  1.1  christos   struct raw_breakpoint *next;
     88  1.1  christos 
     89  1.1  christos   /* The low level type of the breakpoint (software breakpoint,
     90  1.1  christos      watchpoint, etc.)  */
     91  1.1  christos   enum raw_bkpt_type raw_type;
     92  1.1  christos 
     93  1.1  christos   /* A reference count.  Each high level breakpoint referencing this
     94  1.1  christos      raw breakpoint accounts for one reference.  */
     95  1.1  christos   int refcount;
     96  1.1  christos 
     97  1.1  christos   /* The breakpoint's insertion address.  There can only be one raw
     98  1.1  christos      breakpoint for a given PC.  */
     99  1.1  christos   CORE_ADDR pc;
    100  1.1  christos 
    101  1.1  christos   /* The breakpoint's kind.  This is target specific.  Most
    102  1.1  christos      architectures only use one specific instruction for breakpoints, while
    103  1.1  christos      others may use more than one.  E.g., on ARM, we need to use different
    104  1.1  christos      breakpoint instructions on Thumb, Thumb-2, and ARM code.  Likewise for
    105  1.1  christos      hardware breakpoints -- some architectures (including ARM) need to
    106  1.1  christos      setup debug registers differently depending on mode.  */
    107  1.1  christos   int kind;
    108  1.1  christos 
    109  1.1  christos   /* The breakpoint's shadow memory.  */
    110  1.1  christos   unsigned char old_data[MAX_BREAKPOINT_LEN];
    111  1.1  christos 
    112  1.1  christos   /* Positive if this breakpoint is currently inserted in the
    113  1.1  christos      inferior.  Negative if it was, but we've detected that it's now
    114  1.1  christos      gone.  Zero if not inserted.  */
    115  1.1  christos   int inserted;
    116  1.1  christos };
    117  1.1  christos 
    118  1.1  christos /* The type of a breakpoint.  */
    119  1.1  christos enum bkpt_type
    120  1.1  christos   {
    121  1.1  christos     /* A GDB breakpoint, requested with a Z0 packet.  */
    122  1.1  christos     gdb_breakpoint_Z0,
    123  1.1  christos 
    124  1.1  christos     /* A GDB hardware breakpoint, requested with a Z1 packet.  */
    125  1.1  christos     gdb_breakpoint_Z1,
    126  1.1  christos 
    127  1.1  christos     /* A GDB write watchpoint, requested with a Z2 packet.  */
    128  1.1  christos     gdb_breakpoint_Z2,
    129  1.1  christos 
    130  1.1  christos     /* A GDB read watchpoint, requested with a Z3 packet.  */
    131  1.1  christos     gdb_breakpoint_Z3,
    132  1.1  christos 
    133  1.1  christos     /* A GDB access watchpoint, requested with a Z4 packet.  */
    134  1.1  christos     gdb_breakpoint_Z4,
    135  1.1  christos 
    136  1.1  christos     /* A software single-step breakpoint.  */
    137  1.1  christos     single_step_breakpoint,
    138  1.1  christos 
    139  1.1  christos     /* Any other breakpoint type that doesn't require specific
    140  1.1  christos        treatment goes here.  E.g., an event breakpoint.  */
    141  1.1  christos     other_breakpoint,
    142  1.1  christos   };
    143  1.1  christos 
    144  1.1  christos struct point_cond_list
    145  1.1  christos {
    146  1.1  christos   /* Pointer to the agent expression that is the breakpoint's
    147  1.1  christos      conditional.  */
    148  1.1  christos   struct agent_expr *cond;
    149  1.1  christos 
    150  1.1  christos   /* Pointer to the next condition.  */
    151  1.1  christos   struct point_cond_list *next;
    152  1.1  christos };
    153  1.1  christos 
    154  1.1  christos struct point_command_list
    155  1.1  christos {
    156  1.1  christos   /* Pointer to the agent expression that is the breakpoint's
    157  1.1  christos      commands.  */
    158  1.1  christos   struct agent_expr *cmd;
    159  1.1  christos 
    160  1.1  christos   /* Flag that is true if this command should run even while GDB is
    161  1.1  christos      disconnected.  */
    162  1.1  christos   int persistence;
    163  1.1  christos 
    164  1.1  christos   /* Pointer to the next command.  */
    165  1.1  christos   struct point_command_list *next;
    166  1.1  christos };
    167  1.1  christos 
    168  1.1  christos /* A high level (in gdbserver's perspective) breakpoint.  */
    169  1.1  christos struct breakpoint
    170  1.1  christos {
    171  1.1  christos   struct breakpoint *next;
    172  1.1  christos 
    173  1.1  christos   /* The breakpoint's type.  */
    174  1.1  christos   enum bkpt_type type;
    175  1.1  christos 
    176  1.1  christos   /* Link to this breakpoint's raw breakpoint.  This is always
    177  1.1  christos      non-NULL.  */
    178  1.1  christos   struct raw_breakpoint *raw;
    179  1.1  christos };
    180  1.1  christos 
    181  1.1  christos /* Breakpoint requested by GDB.  */
    182  1.1  christos 
    183  1.1  christos struct gdb_breakpoint
    184  1.1  christos {
    185  1.1  christos   struct breakpoint base;
    186  1.1  christos 
    187  1.1  christos   /* Pointer to the condition list that should be evaluated on
    188  1.1  christos      the target or NULL if the breakpoint is unconditional or
    189  1.1  christos      if GDB doesn't want us to evaluate the conditionals on the
    190  1.1  christos      target's side.  */
    191  1.1  christos   struct point_cond_list *cond_list;
    192  1.1  christos 
    193  1.1  christos   /* Point to the list of commands to run when this is hit.  */
    194  1.1  christos   struct point_command_list *command_list;
    195  1.1  christos };
    196  1.1  christos 
    197  1.1  christos /* Breakpoint used by GDBserver.  */
    198  1.1  christos 
    199  1.1  christos struct other_breakpoint
    200  1.1  christos {
    201  1.1  christos   struct breakpoint base;
    202  1.1  christos 
    203  1.1  christos   /* Function to call when we hit this breakpoint.  If it returns 1,
    204  1.1  christos      the breakpoint shall be deleted; 0 or if this callback is NULL,
    205  1.1  christos      it will be left inserted.  */
    206  1.1  christos   int (*handler) (CORE_ADDR);
    207  1.1  christos };
    208  1.1  christos 
    209  1.1  christos /* Breakpoint for single step.  */
    210  1.1  christos 
    211  1.1  christos struct single_step_breakpoint
    212  1.1  christos {
    213  1.1  christos   struct breakpoint base;
    214  1.1  christos 
    215  1.1  christos   /* Thread the reinsert breakpoint belongs to.  */
    216  1.1  christos   ptid_t ptid;
    217  1.1  christos };
    218  1.1  christos 
    219  1.1  christos /* Return the breakpoint size from its kind.  */
    220  1.1  christos 
    221  1.1  christos static int
    222  1.1  christos bp_size (struct raw_breakpoint *bp)
    223  1.1  christos {
    224  1.1  christos   int size = 0;
    225  1.1  christos 
    226  1.1  christos   the_target->sw_breakpoint_from_kind (bp->kind, &size);
    227  1.1  christos   return size;
    228  1.1  christos }
    229  1.1  christos 
    230  1.1  christos /* Return the breakpoint opcode from its kind.  */
    231  1.1  christos 
    232  1.1  christos static const gdb_byte *
    233  1.1  christos bp_opcode (struct raw_breakpoint *bp)
    234  1.1  christos {
    235  1.1  christos   int size = 0;
    236  1.1  christos 
    237  1.1  christos   return the_target->sw_breakpoint_from_kind (bp->kind, &size);
    238  1.1  christos }
    239  1.1  christos 
    240  1.1  christos /* See mem-break.h.  */
    241  1.1  christos 
    242  1.1  christos enum target_hw_bp_type
    243  1.1  christos raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
    244  1.1  christos {
    245  1.1  christos   switch (raw_type)
    246  1.1  christos     {
    247  1.1  christos     case raw_bkpt_type_hw:
    248  1.1  christos       return hw_execute;
    249  1.1  christos     case raw_bkpt_type_write_wp:
    250  1.1  christos       return hw_write;
    251  1.1  christos     case raw_bkpt_type_read_wp:
    252  1.1  christos       return hw_read;
    253  1.1  christos     case raw_bkpt_type_access_wp:
    254  1.1  christos       return hw_access;
    255  1.1  christos     default:
    256  1.1  christos       internal_error (__FILE__, __LINE__,
    257  1.1  christos 		      "bad raw breakpoint type %d", (int) raw_type);
    258  1.1  christos     }
    259  1.1  christos }
    260  1.1  christos 
    261  1.1  christos /* See mem-break.h.  */
    262  1.1  christos 
    263  1.1  christos static enum bkpt_type
    264  1.1  christos Z_packet_to_bkpt_type (char z_type)
    265  1.1  christos {
    266  1.1  christos   gdb_assert ('0' <= z_type && z_type <= '4');
    267  1.1  christos 
    268  1.1  christos   return (enum bkpt_type) (gdb_breakpoint_Z0 + (z_type - '0'));
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos /* See mem-break.h.  */
    272  1.1  christos 
    273  1.1  christos enum raw_bkpt_type
    274  1.1  christos Z_packet_to_raw_bkpt_type (char z_type)
    275  1.1  christos {
    276  1.1  christos   switch (z_type)
    277  1.1  christos     {
    278  1.1  christos     case Z_PACKET_SW_BP:
    279  1.1  christos       return raw_bkpt_type_sw;
    280  1.1  christos     case Z_PACKET_HW_BP:
    281  1.1  christos       return raw_bkpt_type_hw;
    282  1.1  christos     case Z_PACKET_WRITE_WP:
    283  1.1  christos       return raw_bkpt_type_write_wp;
    284  1.1  christos     case Z_PACKET_READ_WP:
    285  1.1  christos       return raw_bkpt_type_read_wp;
    286  1.1  christos     case Z_PACKET_ACCESS_WP:
    287  1.1  christos       return raw_bkpt_type_access_wp;
    288  1.1  christos     default:
    289  1.1  christos       gdb_assert_not_reached ("unhandled Z packet type.");
    290  1.1  christos     }
    291  1.1  christos }
    292  1.1  christos 
    293  1.1  christos /* Return true if breakpoint TYPE is a GDB breakpoint.  */
    294  1.1  christos 
    295  1.1  christos static int
    296  1.1  christos is_gdb_breakpoint (enum bkpt_type type)
    297  1.1  christos {
    298  1.1  christos   return (type == gdb_breakpoint_Z0
    299  1.1  christos 	  || type == gdb_breakpoint_Z1
    300  1.1  christos 	  || type == gdb_breakpoint_Z2
    301  1.1  christos 	  || type == gdb_breakpoint_Z3
    302  1.1  christos 	  || type == gdb_breakpoint_Z4);
    303  1.1  christos }
    304  1.1  christos 
    305  1.1  christos bool
    306  1.1  christos any_persistent_commands (process_info *proc)
    307  1.1  christos {
    308  1.1  christos   struct breakpoint *bp;
    309  1.1  christos   struct point_command_list *cl;
    310  1.1  christos 
    311  1.1  christos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
    312  1.1  christos     {
    313  1.1  christos       if (is_gdb_breakpoint (bp->type))
    314  1.1  christos 	{
    315  1.1  christos 	  struct gdb_breakpoint *gdb_bp = (struct gdb_breakpoint *) bp;
    316  1.1  christos 
    317  1.1  christos 	  for (cl = gdb_bp->command_list; cl != NULL; cl = cl->next)
    318  1.1  christos 	    if (cl->persistence)
    319  1.1  christos 	      return true;
    320  1.1  christos 	}
    321  1.1  christos     }
    322  1.1  christos 
    323  1.1  christos   return false;
    324  1.1  christos }
    325  1.1  christos 
    326  1.1  christos /* Find low-level breakpoint of type TYPE at address ADDR that is not
    327  1.1  christos    insert-disabled.  Returns NULL if not found.  */
    328  1.1  christos 
    329  1.1  christos static struct raw_breakpoint *
    330  1.1  christos find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
    331  1.1  christos {
    332  1.1  christos   struct process_info *proc = current_process ();
    333  1.1  christos   struct raw_breakpoint *bp;
    334  1.1  christos 
    335  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
    336  1.1  christos     if (bp->pc == addr
    337  1.1  christos 	&& bp->raw_type == type
    338  1.1  christos 	&& bp->inserted >= 0)
    339  1.1  christos       return bp;
    340  1.1  christos 
    341  1.1  christos   return NULL;
    342  1.1  christos }
    343  1.1  christos 
    344  1.1  christos /* Find low-level breakpoint of type TYPE at address ADDR.  Returns
    345  1.1  christos    NULL if not found.  */
    346  1.1  christos 
    347  1.1  christos static struct raw_breakpoint *
    348  1.1  christos find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int kind)
    349  1.1  christos {
    350  1.1  christos   struct process_info *proc = current_process ();
    351  1.1  christos   struct raw_breakpoint *bp;
    352  1.1  christos 
    353  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
    354  1.1  christos     if (bp->pc == addr && bp->raw_type == type && bp->kind == kind)
    355  1.1  christos       return bp;
    356  1.1  christos 
    357  1.1  christos   return NULL;
    358  1.1  christos }
    359  1.1  christos 
    360  1.1  christos /* See mem-break.h.  */
    361  1.1  christos 
    362  1.1  christos int
    363  1.1  christos insert_memory_breakpoint (struct raw_breakpoint *bp)
    364  1.1  christos {
    365  1.1  christos   unsigned char buf[MAX_BREAKPOINT_LEN];
    366  1.1  christos   int err;
    367  1.1  christos 
    368  1.1  christos   /* Note that there can be fast tracepoint jumps installed in the
    369  1.1  christos      same memory range, so to get at the original memory, we need to
    370  1.1  christos      use read_inferior_memory, which masks those out.  */
    371  1.1  christos   err = read_inferior_memory (bp->pc, buf, bp_size (bp));
    372  1.1  christos   if (err != 0)
    373  1.1  christos     {
    374  1.1  christos       if (debug_threads)
    375  1.1  christos 	debug_printf ("Failed to read shadow memory of"
    376  1.1  christos 		      " breakpoint at 0x%s (%s).\n",
    377  1.1  christos 		      paddress (bp->pc), safe_strerror (err));
    378  1.1  christos     }
    379  1.1  christos   else
    380  1.1  christos     {
    381  1.1  christos       memcpy (bp->old_data, buf, bp_size (bp));
    382  1.1  christos 
    383  1.1  christos       err = the_target->write_memory (bp->pc, bp_opcode (bp),
    384  1.1  christos 				      bp_size (bp));
    385  1.1  christos       if (err != 0)
    386  1.1  christos 	{
    387  1.1  christos 	  if (debug_threads)
    388  1.1  christos 	    debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
    389  1.1  christos 			  paddress (bp->pc), safe_strerror (err));
    390  1.1  christos 	}
    391  1.1  christos     }
    392  1.1  christos   return err != 0 ? -1 : 0;
    393  1.1  christos }
    394  1.1  christos 
    395  1.1  christos /* See mem-break.h  */
    396  1.1  christos 
    397  1.1  christos int
    398  1.1  christos remove_memory_breakpoint (struct raw_breakpoint *bp)
    399  1.1  christos {
    400  1.1  christos   unsigned char buf[MAX_BREAKPOINT_LEN];
    401  1.1  christos   int err;
    402  1.1  christos 
    403  1.1  christos   /* Since there can be trap breakpoints inserted in the same address
    404  1.1  christos      range, we use `target_write_memory', which takes care of
    405  1.1  christos      layering breakpoints on top of fast tracepoints, and on top of
    406  1.1  christos      the buffer we pass it.  This works because the caller has already
    407  1.1  christos      either unlinked the breakpoint or marked it uninserted.  Also
    408  1.1  christos      note that we need to pass the current shadow contents, because
    409  1.1  christos      target_write_memory updates any shadow memory with what we pass
    410  1.1  christos      here, and we want that to be a nop.  */
    411  1.1  christos   memcpy (buf, bp->old_data, bp_size (bp));
    412  1.1  christos   err = target_write_memory (bp->pc, buf, bp_size (bp));
    413  1.1  christos   if (err != 0)
    414  1.1  christos     {
    415  1.1  christos       if (debug_threads)
    416  1.1  christos 	debug_printf ("Failed to uninsert raw breakpoint "
    417  1.1  christos 		      "at 0x%s (%s) while deleting it.\n",
    418  1.1  christos 		      paddress (bp->pc), safe_strerror (err));
    419  1.1  christos     }
    420  1.1  christos   return err != 0 ? -1 : 0;
    421  1.1  christos }
    422  1.1  christos 
    423  1.1  christos /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE.  On
    424  1.1  christos    success, a pointer to the new breakpoint is returned.  On failure,
    425  1.1  christos    returns NULL and writes the error code to *ERR.  */
    426  1.1  christos 
    427  1.1  christos static struct raw_breakpoint *
    428  1.1  christos set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int kind,
    429  1.1  christos 		       int *err)
    430  1.1  christos {
    431  1.1  christos   struct process_info *proc = current_process ();
    432  1.1  christos   struct raw_breakpoint *bp;
    433  1.1  christos 
    434  1.1  christos   if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
    435  1.1  christos     {
    436  1.1  christos       bp = find_enabled_raw_code_breakpoint_at (where, type);
    437  1.1  christos       if (bp != NULL && bp->kind != kind)
    438  1.1  christos 	{
    439  1.1  christos 	  /* A different kind than previously seen.  The previous
    440  1.1  christos 	     breakpoint must be gone then.  */
    441  1.1  christos 	  if (debug_threads)
    442  1.1  christos 	    debug_printf ("Inconsistent breakpoint kind?  Was %d, now %d.\n",
    443  1.1  christos 			  bp->kind, kind);
    444  1.1  christos 	  bp->inserted = -1;
    445  1.1  christos 	  bp = NULL;
    446  1.1  christos 	}
    447  1.1  christos     }
    448  1.1  christos   else
    449  1.1  christos     bp = find_raw_breakpoint_at (where, type, kind);
    450  1.1  christos 
    451  1.1  christos   gdb::unique_xmalloc_ptr<struct raw_breakpoint> bp_holder;
    452  1.1  christos   if (bp == NULL)
    453  1.1  christos     {
    454  1.1  christos       bp_holder.reset (XCNEW (struct raw_breakpoint));
    455  1.1  christos       bp = bp_holder.get ();
    456  1.1  christos       bp->pc = where;
    457  1.1  christos       bp->kind = kind;
    458  1.1  christos       bp->raw_type = type;
    459  1.1  christos     }
    460  1.1  christos 
    461  1.1  christos   if (!bp->inserted)
    462  1.1  christos     {
    463  1.1  christos       *err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
    464  1.1  christos       if (*err != 0)
    465  1.1  christos 	{
    466  1.1  christos 	  if (debug_threads)
    467  1.1  christos 	    debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
    468  1.1  christos 			  paddress (where), *err);
    469  1.1  christos 
    470  1.1  christos 	  return NULL;
    471  1.1  christos 	}
    472  1.1  christos 
    473  1.1  christos       bp->inserted = 1;
    474  1.1  christos     }
    475  1.1  christos 
    476  1.1  christos   /* If the breakpoint was allocated above, we know we want to keep it
    477  1.1  christos      now.  */
    478  1.1  christos   bp_holder.release ();
    479  1.1  christos 
    480  1.1  christos   /* Link the breakpoint in, if this is the first reference.  */
    481  1.1  christos   if (++bp->refcount == 1)
    482  1.1  christos     {
    483  1.1  christos       bp->next = proc->raw_breakpoints;
    484  1.1  christos       proc->raw_breakpoints = bp;
    485  1.1  christos     }
    486  1.1  christos   return bp;
    487  1.1  christos }
    488  1.1  christos 
    489  1.1  christos /* Notice that breakpoint traps are always installed on top of fast
    490  1.1  christos    tracepoint jumps.  This is even if the fast tracepoint is installed
    491  1.1  christos    at a later time compared to when the breakpoint was installed.
    492  1.1  christos    This means that a stopping breakpoint or tracepoint has higher
    493  1.1  christos    "priority".  In turn, this allows having fast and slow tracepoints
    494  1.1  christos    (and breakpoints) at the same address behave correctly.  */
    495  1.1  christos 
    496  1.1  christos 
    497  1.1  christos /* A fast tracepoint jump.  */
    498  1.1  christos 
    499  1.1  christos struct fast_tracepoint_jump
    500  1.1  christos {
    501  1.1  christos   struct fast_tracepoint_jump *next;
    502  1.1  christos 
    503  1.1  christos   /* A reference count.  GDB can install more than one fast tracepoint
    504  1.1  christos      at the same address (each with its own action list, for
    505  1.1  christos      example).  */
    506  1.1  christos   int refcount;
    507  1.1  christos 
    508  1.1  christos   /* The fast tracepoint's insertion address.  There can only be one
    509  1.1  christos      of these for a given PC.  */
    510  1.1  christos   CORE_ADDR pc;
    511  1.1  christos 
    512  1.1  christos   /* Non-zero if this fast tracepoint jump is currently inserted in
    513  1.1  christos      the inferior.  */
    514  1.1  christos   int inserted;
    515  1.1  christos 
    516  1.1  christos   /* The length of the jump instruction.  */
    517  1.1  christos   int length;
    518  1.1  christos 
    519  1.1  christos   /* A poor-man's flexible array member, holding both the jump
    520  1.1  christos      instruction to insert, and a copy of the instruction that would
    521  1.1  christos      be in memory had not been a jump there (the shadow memory of the
    522  1.1  christos      tracepoint jump).  */
    523  1.1  christos   unsigned char insn_and_shadow[0];
    524  1.1  christos };
    525  1.1  christos 
    526  1.1  christos /* Fast tracepoint FP's jump instruction to insert.  */
    527  1.1  christos #define fast_tracepoint_jump_insn(fp) \
    528  1.1  christos   ((fp)->insn_and_shadow + 0)
    529  1.1  christos 
    530  1.1  christos /* The shadow memory of fast tracepoint jump FP.  */
    531  1.1  christos #define fast_tracepoint_jump_shadow(fp) \
    532  1.1  christos   ((fp)->insn_and_shadow + (fp)->length)
    533  1.1  christos 
    534  1.1  christos 
    535  1.1  christos /* Return the fast tracepoint jump set at WHERE.  */
    536  1.1  christos 
    537  1.1  christos static struct fast_tracepoint_jump *
    538  1.1  christos find_fast_tracepoint_jump_at (CORE_ADDR where)
    539  1.1  christos {
    540  1.1  christos   struct process_info *proc = current_process ();
    541  1.1  christos   struct fast_tracepoint_jump *jp;
    542  1.1  christos 
    543  1.1  christos   for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
    544  1.1  christos     if (jp->pc == where)
    545  1.1  christos       return jp;
    546  1.1  christos 
    547  1.1  christos   return NULL;
    548  1.1  christos }
    549  1.1  christos 
    550  1.1  christos int
    551  1.1  christos fast_tracepoint_jump_here (CORE_ADDR where)
    552  1.1  christos {
    553  1.1  christos   struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
    554  1.1  christos 
    555  1.1  christos   return (jp != NULL);
    556  1.1  christos }
    557  1.1  christos 
    558  1.1  christos int
    559  1.1  christos delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
    560  1.1  christos {
    561  1.1  christos   struct fast_tracepoint_jump *bp, **bp_link;
    562  1.1  christos   int ret;
    563  1.1  christos   struct process_info *proc = current_process ();
    564  1.1  christos 
    565  1.1  christos   bp = proc->fast_tracepoint_jumps;
    566  1.1  christos   bp_link = &proc->fast_tracepoint_jumps;
    567  1.1  christos 
    568  1.1  christos   while (bp)
    569  1.1  christos     {
    570  1.1  christos       if (bp == todel)
    571  1.1  christos 	{
    572  1.1  christos 	  if (--bp->refcount == 0)
    573  1.1  christos 	    {
    574  1.1  christos 	      struct fast_tracepoint_jump *prev_bp_link = *bp_link;
    575  1.1  christos 	      unsigned char *buf;
    576  1.1  christos 
    577  1.1  christos 	      /* Unlink it.  */
    578  1.1  christos 	      *bp_link = bp->next;
    579  1.1  christos 
    580  1.1  christos 	      /* Since there can be breakpoints inserted in the same
    581  1.1  christos 		 address range, we use `target_write_memory', which
    582  1.1  christos 		 takes care of layering breakpoints on top of fast
    583  1.1  christos 		 tracepoints, and on top of the buffer we pass it.
    584  1.1  christos 		 This works because we've already unlinked the fast
    585  1.1  christos 		 tracepoint jump above.  Also note that we need to
    586  1.1  christos 		 pass the current shadow contents, because
    587  1.1  christos 		 target_write_memory updates any shadow memory with
    588  1.1  christos 		 what we pass here, and we want that to be a nop.  */
    589  1.1  christos 	      buf = (unsigned char *) alloca (bp->length);
    590  1.1  christos 	      memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
    591  1.1  christos 	      ret = target_write_memory (bp->pc, buf, bp->length);
    592  1.1  christos 	      if (ret != 0)
    593  1.1  christos 		{
    594  1.1  christos 		  /* Something went wrong, relink the jump.  */
    595  1.1  christos 		  *bp_link = prev_bp_link;
    596  1.1  christos 
    597  1.1  christos 		  if (debug_threads)
    598  1.1  christos 		    debug_printf ("Failed to uninsert fast tracepoint jump "
    599  1.1  christos 				  "at 0x%s (%s) while deleting it.\n",
    600  1.1  christos 				  paddress (bp->pc), safe_strerror (ret));
    601  1.1  christos 		  return ret;
    602  1.1  christos 		}
    603  1.1  christos 
    604  1.1  christos 	      free (bp);
    605  1.1  christos 	    }
    606  1.1  christos 
    607  1.1  christos 	  return 0;
    608  1.1  christos 	}
    609  1.1  christos       else
    610  1.1  christos 	{
    611  1.1  christos 	  bp_link = &bp->next;
    612  1.1  christos 	  bp = *bp_link;
    613  1.1  christos 	}
    614  1.1  christos     }
    615  1.1  christos 
    616  1.1  christos   warning ("Could not find fast tracepoint jump in list.");
    617  1.1  christos   return ENOENT;
    618  1.1  christos }
    619  1.1  christos 
    620  1.1  christos void
    621  1.1  christos inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
    622  1.1  christos {
    623  1.1  christos   jp->refcount++;
    624  1.1  christos }
    625  1.1  christos 
    626  1.1  christos struct fast_tracepoint_jump *
    627  1.1  christos set_fast_tracepoint_jump (CORE_ADDR where,
    628  1.1  christos 			  unsigned char *insn, ULONGEST length)
    629  1.1  christos {
    630  1.1  christos   struct process_info *proc = current_process ();
    631  1.1  christos   struct fast_tracepoint_jump *jp;
    632  1.1  christos   int err;
    633  1.1  christos   unsigned char *buf;
    634  1.1  christos 
    635  1.1  christos   /* We refcount fast tracepoint jumps.  Check if we already know
    636  1.1  christos      about a jump at this address.  */
    637  1.1  christos   jp = find_fast_tracepoint_jump_at (where);
    638  1.1  christos   if (jp != NULL)
    639  1.1  christos     {
    640  1.1  christos       jp->refcount++;
    641  1.1  christos       return jp;
    642  1.1  christos     }
    643  1.1  christos 
    644  1.1  christos   /* We don't, so create a new object.  Double the length, because the
    645  1.1  christos      flexible array member holds both the jump insn, and the
    646  1.1  christos      shadow.  */
    647  1.1  christos   jp = (struct fast_tracepoint_jump *) xcalloc (1, sizeof (*jp) + (length * 2));
    648  1.1  christos   jp->pc = where;
    649  1.1  christos   jp->length = length;
    650  1.1  christos   memcpy (fast_tracepoint_jump_insn (jp), insn, length);
    651  1.1  christos   jp->refcount = 1;
    652  1.1  christos   buf = (unsigned char *) alloca (length);
    653  1.1  christos 
    654  1.1  christos   /* Note that there can be trap breakpoints inserted in the same
    655  1.1  christos      address range.  To access the original memory contents, we use
    656  1.1  christos      `read_inferior_memory', which masks out breakpoints.  */
    657  1.1  christos   err = read_inferior_memory (where, buf, length);
    658  1.1  christos   if (err != 0)
    659  1.1  christos     {
    660  1.1  christos       if (debug_threads)
    661  1.1  christos 	debug_printf ("Failed to read shadow memory of"
    662  1.1  christos 		      " fast tracepoint at 0x%s (%s).\n",
    663  1.1  christos 		      paddress (where), safe_strerror (err));
    664  1.1  christos       free (jp);
    665  1.1  christos       return NULL;
    666  1.1  christos     }
    667  1.1  christos   memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
    668  1.1  christos 
    669  1.1  christos   /* Link the jump in.  */
    670  1.1  christos   jp->inserted = 1;
    671  1.1  christos   jp->next = proc->fast_tracepoint_jumps;
    672  1.1  christos   proc->fast_tracepoint_jumps = jp;
    673  1.1  christos 
    674  1.1  christos   /* Since there can be trap breakpoints inserted in the same address
    675  1.1  christos      range, we use use `target_write_memory', which takes care of
    676  1.1  christos      layering breakpoints on top of fast tracepoints, on top of the
    677  1.1  christos      buffer we pass it.  This works because we've already linked in
    678  1.1  christos      the fast tracepoint jump above.  Also note that we need to pass
    679  1.1  christos      the current shadow contents, because target_write_memory
    680  1.1  christos      updates any shadow memory with what we pass here, and we want
    681  1.1  christos      that to be a nop.  */
    682  1.1  christos   err = target_write_memory (where, buf, length);
    683  1.1  christos   if (err != 0)
    684  1.1  christos     {
    685  1.1  christos       if (debug_threads)
    686  1.1  christos 	debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
    687  1.1  christos 		      paddress (where), safe_strerror (err));
    688  1.1  christos 
    689  1.1  christos       /* Unlink it.  */
    690  1.1  christos       proc->fast_tracepoint_jumps = jp->next;
    691  1.1  christos       free (jp);
    692  1.1  christos 
    693  1.1  christos       return NULL;
    694  1.1  christos     }
    695  1.1  christos 
    696  1.1  christos   return jp;
    697  1.1  christos }
    698  1.1  christos 
    699  1.1  christos void
    700  1.1  christos uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
    701  1.1  christos {
    702  1.1  christos   struct fast_tracepoint_jump *jp;
    703  1.1  christos   int err;
    704  1.1  christos 
    705  1.1  christos   jp = find_fast_tracepoint_jump_at (pc);
    706  1.1  christos   if (jp == NULL)
    707  1.1  christos     {
    708  1.1  christos       /* This can happen when we remove all breakpoints while handling
    709  1.1  christos 	 a step-over.  */
    710  1.1  christos       if (debug_threads)
    711  1.1  christos 	debug_printf ("Could not find fast tracepoint jump at 0x%s "
    712  1.1  christos 		      "in list (uninserting).\n",
    713  1.1  christos 		      paddress (pc));
    714  1.1  christos       return;
    715  1.1  christos     }
    716  1.1  christos 
    717  1.1  christos   if (jp->inserted)
    718  1.1  christos     {
    719  1.1  christos       unsigned char *buf;
    720  1.1  christos 
    721  1.1  christos       jp->inserted = 0;
    722  1.1  christos 
    723  1.1  christos       /* Since there can be trap breakpoints inserted in the same
    724  1.1  christos 	 address range, we use use `target_write_memory', which
    725  1.1  christos 	 takes care of layering breakpoints on top of fast
    726  1.1  christos 	 tracepoints, and on top of the buffer we pass it.  This works
    727  1.1  christos 	 because we've already marked the fast tracepoint fast
    728  1.1  christos 	 tracepoint jump uninserted above.  Also note that we need to
    729  1.1  christos 	 pass the current shadow contents, because
    730  1.1  christos 	 target_write_memory updates any shadow memory with what we
    731  1.1  christos 	 pass here, and we want that to be a nop.  */
    732  1.1  christos       buf = (unsigned char *) alloca (jp->length);
    733  1.1  christos       memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
    734  1.1  christos       err = target_write_memory (jp->pc, buf, jp->length);
    735  1.1  christos       if (err != 0)
    736  1.1  christos 	{
    737  1.1  christos 	  jp->inserted = 1;
    738  1.1  christos 
    739  1.1  christos 	  if (debug_threads)
    740  1.1  christos 	    debug_printf ("Failed to uninsert fast tracepoint jump at"
    741  1.1  christos 			  " 0x%s (%s).\n",
    742  1.1  christos 			  paddress (pc), safe_strerror (err));
    743  1.1  christos 	}
    744  1.1  christos     }
    745  1.1  christos }
    746  1.1  christos 
    747  1.1  christos void
    748  1.1  christos reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
    749  1.1  christos {
    750  1.1  christos   struct fast_tracepoint_jump *jp;
    751  1.1  christos   int err;
    752  1.1  christos   unsigned char *buf;
    753  1.1  christos 
    754  1.1  christos   jp = find_fast_tracepoint_jump_at (where);
    755  1.1  christos   if (jp == NULL)
    756  1.1  christos     {
    757  1.1  christos       /* This can happen when we remove breakpoints when a tracepoint
    758  1.1  christos 	 hit causes a tracing stop, while handling a step-over.  */
    759  1.1  christos       if (debug_threads)
    760  1.1  christos 	debug_printf ("Could not find fast tracepoint jump at 0x%s "
    761  1.1  christos 		      "in list (reinserting).\n",
    762  1.1  christos 		      paddress (where));
    763  1.1  christos       return;
    764  1.1  christos     }
    765  1.1  christos 
    766  1.1  christos   if (jp->inserted)
    767  1.1  christos     error ("Jump already inserted at reinsert time.");
    768  1.1  christos 
    769  1.1  christos   jp->inserted = 1;
    770  1.1  christos 
    771  1.1  christos   /* Since there can be trap breakpoints inserted in the same address
    772  1.1  christos      range, we use `target_write_memory', which takes care of
    773  1.1  christos      layering breakpoints on top of fast tracepoints, and on top of
    774  1.1  christos      the buffer we pass it.  This works because we've already marked
    775  1.1  christos      the fast tracepoint jump inserted above.  Also note that we need
    776  1.1  christos      to pass the current shadow contents, because
    777  1.1  christos      target_write_memory updates any shadow memory with what we pass
    778  1.1  christos      here, and we want that to be a nop.  */
    779  1.1  christos   buf = (unsigned char *) alloca (jp->length);
    780  1.1  christos   memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
    781  1.1  christos   err = target_write_memory (where, buf, jp->length);
    782  1.1  christos   if (err != 0)
    783  1.1  christos     {
    784  1.1  christos       jp->inserted = 0;
    785  1.1  christos 
    786  1.1  christos       if (debug_threads)
    787  1.1  christos 	debug_printf ("Failed to reinsert fast tracepoint jump at"
    788  1.1  christos 		      " 0x%s (%s).\n",
    789  1.1  christos 		      paddress (where), safe_strerror (err));
    790  1.1  christos     }
    791  1.1  christos }
    792  1.1  christos 
    793  1.1  christos /* Set a high-level breakpoint of type TYPE, with low level type
    794  1.1  christos    RAW_TYPE and kind KIND, at WHERE.  On success, a pointer to the new
    795  1.1  christos    breakpoint is returned.  On failure, returns NULL and writes the
    796  1.1  christos    error code to *ERR.  HANDLER is called when the breakpoint is hit.
    797  1.1  christos    HANDLER should return 1 if the breakpoint should be deleted, 0
    798  1.1  christos    otherwise.  */
    799  1.1  christos 
    800  1.1  christos static struct breakpoint *
    801  1.1  christos set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
    802  1.1  christos 		CORE_ADDR where, int kind,
    803  1.1  christos 		int (*handler) (CORE_ADDR), int *err)
    804  1.1  christos {
    805  1.1  christos   struct process_info *proc = current_process ();
    806  1.1  christos   struct breakpoint *bp;
    807  1.1  christos   struct raw_breakpoint *raw;
    808  1.1  christos 
    809  1.1  christos   raw = set_raw_breakpoint_at (raw_type, where, kind, err);
    810  1.1  christos 
    811  1.1  christos   if (raw == NULL)
    812  1.1  christos     {
    813  1.1  christos       /* warn? */
    814  1.1  christos       return NULL;
    815  1.1  christos     }
    816  1.1  christos 
    817  1.1  christos   if (is_gdb_breakpoint (type))
    818  1.1  christos     {
    819  1.1  christos       struct gdb_breakpoint *gdb_bp = XCNEW (struct gdb_breakpoint);
    820  1.1  christos 
    821  1.1  christos       bp = (struct breakpoint *) gdb_bp;
    822  1.1  christos       gdb_assert (handler == NULL);
    823  1.1  christos     }
    824  1.1  christos   else if (type == other_breakpoint)
    825  1.1  christos     {
    826  1.1  christos       struct other_breakpoint *other_bp = XCNEW (struct other_breakpoint);
    827  1.1  christos 
    828  1.1  christos       other_bp->handler = handler;
    829  1.1  christos       bp = (struct breakpoint *) other_bp;
    830  1.1  christos     }
    831  1.1  christos   else if (type == single_step_breakpoint)
    832  1.1  christos     {
    833  1.1  christos       struct single_step_breakpoint *ss_bp
    834  1.1  christos 	= XCNEW (struct single_step_breakpoint);
    835  1.1  christos 
    836  1.1  christos       bp = (struct breakpoint *) ss_bp;
    837  1.1  christos     }
    838  1.1  christos   else
    839  1.1  christos     gdb_assert_not_reached ("unhandled breakpoint type");
    840  1.1  christos 
    841  1.1  christos   bp->type = type;
    842  1.1  christos   bp->raw = raw;
    843  1.1  christos 
    844  1.1  christos   bp->next = proc->breakpoints;
    845  1.1  christos   proc->breakpoints = bp;
    846  1.1  christos 
    847  1.1  christos   return bp;
    848  1.1  christos }
    849  1.1  christos 
    850  1.1  christos /* Set breakpoint of TYPE on address WHERE with handler HANDLER.  */
    851  1.1  christos 
    852  1.1  christos static struct breakpoint *
    853  1.1  christos set_breakpoint_type_at (enum bkpt_type type, CORE_ADDR where,
    854  1.1  christos 			int (*handler) (CORE_ADDR))
    855  1.1  christos {
    856  1.1  christos   int err_ignored;
    857  1.1  christos   CORE_ADDR placed_address = where;
    858  1.1  christos   int breakpoint_kind = target_breakpoint_kind_from_pc (&placed_address);
    859  1.1  christos 
    860  1.1  christos   return set_breakpoint (type, raw_bkpt_type_sw,
    861  1.1  christos 			 placed_address, breakpoint_kind, handler,
    862  1.1  christos 			 &err_ignored);
    863  1.1  christos }
    864  1.1  christos 
    865  1.1  christos /* See mem-break.h  */
    866  1.1  christos 
    867  1.1  christos struct breakpoint *
    868  1.1  christos set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
    869  1.1  christos {
    870  1.1  christos   return set_breakpoint_type_at (other_breakpoint, where, handler);
    871  1.1  christos }
    872  1.1  christos 
    873  1.1  christos 
    874  1.1  christos static int
    875  1.1  christos delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
    876  1.1  christos {
    877  1.1  christos   struct raw_breakpoint *bp, **bp_link;
    878  1.1  christos   int ret;
    879  1.1  christos 
    880  1.1  christos   bp = proc->raw_breakpoints;
    881  1.1  christos   bp_link = &proc->raw_breakpoints;
    882  1.1  christos 
    883  1.1  christos   while (bp)
    884  1.1  christos     {
    885  1.1  christos       if (bp == todel)
    886  1.1  christos 	{
    887  1.1  christos 	  if (bp->inserted > 0)
    888  1.1  christos 	    {
    889  1.1  christos 	      struct raw_breakpoint *prev_bp_link = *bp_link;
    890  1.1  christos 
    891  1.1  christos 	      *bp_link = bp->next;
    892  1.1  christos 
    893  1.1  christos 	      ret = the_target->remove_point (bp->raw_type, bp->pc,
    894  1.1  christos 					      bp->kind, bp);
    895  1.1  christos 	      if (ret != 0)
    896  1.1  christos 		{
    897  1.1  christos 		  /* Something went wrong, relink the breakpoint.  */
    898  1.1  christos 		  *bp_link = prev_bp_link;
    899  1.1  christos 
    900  1.1  christos 		  if (debug_threads)
    901  1.1  christos 		    debug_printf ("Failed to uninsert raw breakpoint "
    902  1.1  christos 				  "at 0x%s while deleting it.\n",
    903  1.1  christos 				  paddress (bp->pc));
    904  1.1  christos 		  return ret;
    905  1.1  christos 		}
    906  1.1  christos 	    }
    907  1.1  christos 	  else
    908  1.1  christos 	    *bp_link = bp->next;
    909  1.1  christos 
    910  1.1  christos 	  free (bp);
    911  1.1  christos 	  return 0;
    912  1.1  christos 	}
    913  1.1  christos       else
    914  1.1  christos 	{
    915  1.1  christos 	  bp_link = &bp->next;
    916  1.1  christos 	  bp = *bp_link;
    917  1.1  christos 	}
    918  1.1  christos     }
    919  1.1  christos 
    920  1.1  christos   warning ("Could not find raw breakpoint in list.");
    921  1.1  christos   return ENOENT;
    922  1.1  christos }
    923  1.1  christos 
    924  1.1  christos static int
    925  1.1  christos release_breakpoint (struct process_info *proc, struct breakpoint *bp)
    926  1.1  christos {
    927  1.1  christos   int newrefcount;
    928  1.1  christos   int ret;
    929  1.1  christos 
    930  1.1  christos   newrefcount = bp->raw->refcount - 1;
    931  1.1  christos   if (newrefcount == 0)
    932  1.1  christos     {
    933  1.1  christos       ret = delete_raw_breakpoint (proc, bp->raw);
    934  1.1  christos       if (ret != 0)
    935  1.1  christos 	return ret;
    936  1.1  christos     }
    937  1.1  christos   else
    938  1.1  christos     bp->raw->refcount = newrefcount;
    939  1.1  christos 
    940  1.1  christos   free (bp);
    941  1.1  christos 
    942  1.1  christos   return 0;
    943  1.1  christos }
    944  1.1  christos 
    945  1.1  christos static int
    946  1.1  christos delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
    947  1.1  christos {
    948  1.1  christos   struct breakpoint *bp, **bp_link;
    949  1.1  christos   int err;
    950  1.1  christos 
    951  1.1  christos   bp = proc->breakpoints;
    952  1.1  christos   bp_link = &proc->breakpoints;
    953  1.1  christos 
    954  1.1  christos   while (bp)
    955  1.1  christos     {
    956  1.1  christos       if (bp == todel)
    957  1.1  christos 	{
    958  1.1  christos 	  *bp_link = bp->next;
    959  1.1  christos 
    960  1.1  christos 	  err = release_breakpoint (proc, bp);
    961  1.1  christos 	  if (err != 0)
    962  1.1  christos 	    return err;
    963  1.1  christos 
    964  1.1  christos 	  bp = *bp_link;
    965  1.1  christos 	  return 0;
    966  1.1  christos 	}
    967  1.1  christos       else
    968  1.1  christos 	{
    969  1.1  christos 	  bp_link = &bp->next;
    970  1.1  christos 	  bp = *bp_link;
    971  1.1  christos 	}
    972  1.1  christos     }
    973  1.1  christos 
    974  1.1  christos   warning ("Could not find breakpoint in list.");
    975  1.1  christos   return ENOENT;
    976  1.1  christos }
    977  1.1  christos 
    978  1.1  christos int
    979  1.1  christos delete_breakpoint (struct breakpoint *todel)
    980  1.1  christos {
    981  1.1  christos   struct process_info *proc = current_process ();
    982  1.1  christos   return delete_breakpoint_1 (proc, todel);
    983  1.1  christos }
    984  1.1  christos 
    985  1.1  christos /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
    986  1.1  christos    address ADDR and return a pointer to its structure.  If KIND is -1,
    987  1.1  christos    the breakpoint's kind is ignored.  */
    988  1.1  christos 
    989  1.1  christos static struct gdb_breakpoint *
    990  1.1  christos find_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
    991  1.1  christos {
    992  1.1  christos   struct process_info *proc = current_process ();
    993  1.1  christos   struct breakpoint *bp;
    994  1.1  christos   enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
    995  1.1  christos 
    996  1.1  christos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
    997  1.1  christos     if (bp->type == type && bp->raw->pc == addr
    998  1.1  christos 	&& (kind == -1 || bp->raw->kind == kind))
    999  1.1  christos       return (struct gdb_breakpoint *) bp;
   1000  1.1  christos 
   1001  1.1  christos   return NULL;
   1002  1.1  christos }
   1003  1.1  christos 
   1004  1.1  christos static int
   1005  1.1  christos z_type_supported (char z_type)
   1006  1.1  christos {
   1007  1.1  christos   return (z_type >= '0' && z_type <= '4'
   1008  1.1  christos 	  && the_target->supports_z_point_type (z_type));
   1009  1.1  christos }
   1010  1.1  christos 
   1011  1.1  christos /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
   1012  1.1  christos    Returns a pointer to the newly created breakpoint on success.  On
   1013  1.1  christos    failure returns NULL and sets *ERR to either -1 for error, or 1 if
   1014  1.1  christos    Z_TYPE breakpoints are not supported on this target.  */
   1015  1.1  christos 
   1016  1.1  christos static struct gdb_breakpoint *
   1017  1.1  christos set_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int kind, int *err)
   1018  1.1  christos {
   1019  1.1  christos   struct gdb_breakpoint *bp;
   1020  1.1  christos   enum bkpt_type type;
   1021  1.1  christos   enum raw_bkpt_type raw_type;
   1022  1.1  christos 
   1023  1.1  christos   /* If we see GDB inserting a second code breakpoint at the same
   1024  1.1  christos      address, then either: GDB is updating the breakpoint's conditions
   1025  1.1  christos      or commands; or, the first breakpoint must have disappeared due
   1026  1.1  christos      to a shared library unload.  On targets where the shared
   1027  1.1  christos      libraries are handled by userspace, like SVR4, for example,
   1028  1.1  christos      GDBserver can't tell if a library was loaded or unloaded.  Since
   1029  1.1  christos      we refcount raw breakpoints, we must be careful to make sure GDB
   1030  1.1  christos      breakpoints never contribute more than one reference.  if we
   1031  1.1  christos      didn't do this, in case the previous breakpoint is gone due to a
   1032  1.1  christos      shared library unload, we'd just increase the refcount of the
   1033  1.1  christos      previous breakpoint at this address, but the trap was not planted
   1034  1.1  christos      in the inferior anymore, thus the breakpoint would never be hit.
   1035  1.1  christos      Note this must be careful to not create a window where
   1036  1.1  christos      breakpoints are removed from the target, for non-stop, in case
   1037  1.1  christos      the target can poke at memory while the program is running.  */
   1038  1.1  christos   if (z_type == Z_PACKET_SW_BP
   1039  1.1  christos       || z_type == Z_PACKET_HW_BP)
   1040  1.1  christos     {
   1041  1.1  christos       bp = find_gdb_breakpoint (z_type, addr, -1);
   1042  1.1  christos 
   1043  1.1  christos       if (bp != NULL)
   1044  1.1  christos 	{
   1045  1.1  christos 	  if (bp->base.raw->kind != kind)
   1046  1.1  christos 	    {
   1047  1.1  christos 	      /* A different kind than previously seen.  The previous
   1048  1.1  christos 		 breakpoint must be gone then.  */
   1049  1.1  christos 	      bp->base.raw->inserted = -1;
   1050  1.1  christos 	      delete_breakpoint ((struct breakpoint *) bp);
   1051  1.1  christos 	      bp = NULL;
   1052  1.1  christos 	    }
   1053  1.1  christos 	  else if (z_type == Z_PACKET_SW_BP)
   1054  1.1  christos 	    {
   1055  1.1  christos 	      /* Check if the breakpoint is actually gone from the
   1056  1.1  christos 		 target, due to an solib unload, for example.  Might
   1057  1.1  christos 		 as well validate _all_ breakpoints.  */
   1058  1.1  christos 	      validate_breakpoints ();
   1059  1.1  christos 
   1060  1.1  christos 	      /* Breakpoints that don't pass validation are
   1061  1.1  christos 		 deleted.  */
   1062  1.1  christos 	      bp = find_gdb_breakpoint (z_type, addr, -1);
   1063  1.1  christos 	    }
   1064  1.1  christos 	}
   1065  1.1  christos     }
   1066  1.1  christos   else
   1067  1.1  christos     {
   1068  1.1  christos       /* Data breakpoints for the same address but different kind are
   1069  1.1  christos 	 expected.  GDB doesn't merge these.  The backend gets to do
   1070  1.1  christos 	 that if it wants/can.  */
   1071  1.1  christos       bp = find_gdb_breakpoint (z_type, addr, kind);
   1072  1.1  christos     }
   1073  1.1  christos 
   1074  1.1  christos   if (bp != NULL)
   1075  1.1  christos     {
   1076  1.1  christos       /* We already know about this breakpoint, there's nothing else
   1077  1.1  christos 	 to do - GDB's reference is already accounted for.  Note that
   1078  1.1  christos 	 whether the breakpoint inserted is left as is - we may be
   1079  1.1  christos 	 stepping over it, for example, in which case we don't want to
   1080  1.1  christos 	 force-reinsert it.  */
   1081  1.1  christos       return bp;
   1082  1.1  christos     }
   1083  1.1  christos 
   1084  1.1  christos   raw_type = Z_packet_to_raw_bkpt_type (z_type);
   1085  1.1  christos   type = Z_packet_to_bkpt_type (z_type);
   1086  1.1  christos   return (struct gdb_breakpoint *) set_breakpoint (type, raw_type, addr,
   1087  1.1  christos 						   kind, NULL, err);
   1088  1.1  christos }
   1089  1.1  christos 
   1090  1.1  christos static int
   1091  1.1  christos check_gdb_bp_preconditions (char z_type, int *err)
   1092  1.1  christos {
   1093  1.1  christos   /* As software/memory breakpoints work by poking at memory, we need
   1094  1.1  christos      to prepare to access memory.  If that operation fails, we need to
   1095  1.1  christos      return error.  Seeing an error, if this is the first breakpoint
   1096  1.1  christos      of that type that GDB tries to insert, GDB would then assume the
   1097  1.1  christos      breakpoint type is supported, but it may actually not be.  So we
   1098  1.1  christos      need to check whether the type is supported at all before
   1099  1.1  christos      preparing to access memory.  */
   1100  1.1  christos   if (!z_type_supported (z_type))
   1101  1.1  christos     {
   1102  1.1  christos       *err = 1;
   1103  1.1  christos       return 0;
   1104  1.1  christos     }
   1105  1.1  christos 
   1106  1.1  christos   return 1;
   1107  1.1  christos }
   1108  1.1  christos 
   1109  1.1  christos /* See mem-break.h.  This is a wrapper for set_gdb_breakpoint_1 that
   1110  1.1  christos    knows to prepare to access memory for Z0 breakpoints.  */
   1111  1.1  christos 
   1112  1.1  christos struct gdb_breakpoint *
   1113  1.1  christos set_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind, int *err)
   1114  1.1  christos {
   1115  1.1  christos   struct gdb_breakpoint *bp;
   1116  1.1  christos 
   1117  1.1  christos   if (!check_gdb_bp_preconditions (z_type, err))
   1118  1.1  christos     return NULL;
   1119  1.1  christos 
   1120  1.1  christos   /* If inserting a software/memory breakpoint, need to prepare to
   1121  1.1  christos      access memory.  */
   1122  1.1  christos   if (z_type == Z_PACKET_SW_BP)
   1123  1.1  christos     {
   1124  1.1  christos       if (prepare_to_access_memory () != 0)
   1125  1.1  christos 	{
   1126  1.1  christos 	  *err = -1;
   1127  1.1  christos 	  return NULL;
   1128  1.1  christos 	}
   1129  1.1  christos     }
   1130  1.1  christos 
   1131  1.1  christos   bp = set_gdb_breakpoint_1 (z_type, addr, kind, err);
   1132  1.1  christos 
   1133  1.1  christos   if (z_type == Z_PACKET_SW_BP)
   1134  1.1  christos     done_accessing_memory ();
   1135  1.1  christos 
   1136  1.1  christos   return bp;
   1137  1.1  christos }
   1138  1.1  christos 
   1139  1.1  christos /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
   1140  1.1  christos    inserted at ADDR with set_gdb_breakpoint_at.  Returns 0 on success,
   1141  1.1  christos    -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
   1142  1.1  christos    target.  */
   1143  1.1  christos 
   1144  1.1  christos static int
   1145  1.1  christos delete_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int kind)
   1146  1.1  christos {
   1147  1.1  christos   struct gdb_breakpoint *bp;
   1148  1.1  christos   int err;
   1149  1.1  christos 
   1150  1.1  christos   bp = find_gdb_breakpoint (z_type, addr, kind);
   1151  1.1  christos   if (bp == NULL)
   1152  1.1  christos     return -1;
   1153  1.1  christos 
   1154  1.1  christos   /* Before deleting the breakpoint, make sure to free its condition
   1155  1.1  christos      and command lists.  */
   1156  1.1  christos   clear_breakpoint_conditions_and_commands (bp);
   1157  1.1  christos   err = delete_breakpoint ((struct breakpoint *) bp);
   1158  1.1  christos   if (err != 0)
   1159  1.1  christos     return -1;
   1160  1.1  christos 
   1161  1.1  christos   return 0;
   1162  1.1  christos }
   1163  1.1  christos 
   1164  1.1  christos /* See mem-break.h.  This is a wrapper for delete_gdb_breakpoint that
   1165  1.1  christos    knows to prepare to access memory for Z0 breakpoints.  */
   1166  1.1  christos 
   1167  1.1  christos int
   1168  1.1  christos delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
   1169  1.1  christos {
   1170  1.1  christos   int ret;
   1171  1.1  christos 
   1172  1.1  christos   if (!check_gdb_bp_preconditions (z_type, &ret))
   1173  1.1  christos     return ret;
   1174  1.1  christos 
   1175  1.1  christos   /* If inserting a software/memory breakpoint, need to prepare to
   1176  1.1  christos      access memory.  */
   1177  1.1  christos   if (z_type == Z_PACKET_SW_BP)
   1178  1.1  christos     {
   1179  1.1  christos       int err;
   1180  1.1  christos 
   1181  1.1  christos       err = prepare_to_access_memory ();
   1182  1.1  christos       if (err != 0)
   1183  1.1  christos 	return -1;
   1184  1.1  christos     }
   1185  1.1  christos 
   1186  1.1  christos   ret = delete_gdb_breakpoint_1 (z_type, addr, kind);
   1187  1.1  christos 
   1188  1.1  christos   if (z_type == Z_PACKET_SW_BP)
   1189  1.1  christos     done_accessing_memory ();
   1190  1.1  christos 
   1191  1.1  christos   return ret;
   1192  1.1  christos }
   1193  1.1  christos 
   1194  1.1  christos /* Clear all conditions associated with a breakpoint.  */
   1195  1.1  christos 
   1196  1.1  christos static void
   1197  1.1  christos clear_breakpoint_conditions (struct gdb_breakpoint *bp)
   1198  1.1  christos {
   1199  1.1  christos   struct point_cond_list *cond;
   1200  1.1  christos 
   1201  1.1  christos   if (bp->cond_list == NULL)
   1202  1.1  christos     return;
   1203  1.1  christos 
   1204  1.1  christos   cond = bp->cond_list;
   1205  1.1  christos 
   1206  1.1  christos   while (cond != NULL)
   1207  1.1  christos     {
   1208  1.1  christos       struct point_cond_list *cond_next;
   1209  1.1  christos 
   1210  1.1  christos       cond_next = cond->next;
   1211  1.1  christos       gdb_free_agent_expr (cond->cond);
   1212  1.1  christos       free (cond);
   1213  1.1  christos       cond = cond_next;
   1214  1.1  christos     }
   1215  1.1  christos 
   1216  1.1  christos   bp->cond_list = NULL;
   1217  1.1  christos }
   1218  1.1  christos 
   1219  1.1  christos /* Clear all commands associated with a breakpoint.  */
   1220  1.1  christos 
   1221  1.1  christos static void
   1222  1.1  christos clear_breakpoint_commands (struct gdb_breakpoint *bp)
   1223  1.1  christos {
   1224  1.1  christos   struct point_command_list *cmd;
   1225  1.1  christos 
   1226  1.1  christos   if (bp->command_list == NULL)
   1227  1.1  christos     return;
   1228  1.1  christos 
   1229  1.1  christos   cmd = bp->command_list;
   1230  1.1  christos 
   1231  1.1  christos   while (cmd != NULL)
   1232  1.1  christos     {
   1233  1.1  christos       struct point_command_list *cmd_next;
   1234  1.1  christos 
   1235  1.1  christos       cmd_next = cmd->next;
   1236  1.1  christos       gdb_free_agent_expr (cmd->cmd);
   1237  1.1  christos       free (cmd);
   1238  1.1  christos       cmd = cmd_next;
   1239  1.1  christos     }
   1240  1.1  christos 
   1241  1.1  christos   bp->command_list = NULL;
   1242  1.1  christos }
   1243  1.1  christos 
   1244  1.1  christos void
   1245  1.1  christos clear_breakpoint_conditions_and_commands (struct gdb_breakpoint *bp)
   1246  1.1  christos {
   1247  1.1  christos   clear_breakpoint_conditions (bp);
   1248  1.1  christos   clear_breakpoint_commands (bp);
   1249  1.1  christos }
   1250  1.1  christos 
   1251  1.1  christos /* Add condition CONDITION to GDBserver's breakpoint BP.  */
   1252  1.1  christos 
   1253  1.1  christos static void
   1254  1.1  christos add_condition_to_breakpoint (struct gdb_breakpoint *bp,
   1255  1.1  christos 			     struct agent_expr *condition)
   1256  1.1  christos {
   1257  1.1  christos   struct point_cond_list *new_cond;
   1258  1.1  christos 
   1259  1.1  christos   /* Create new condition.  */
   1260  1.1  christos   new_cond = XCNEW (struct point_cond_list);
   1261  1.1  christos   new_cond->cond = condition;
   1262  1.1  christos 
   1263  1.1  christos   /* Add condition to the list.  */
   1264  1.1  christos   new_cond->next = bp->cond_list;
   1265  1.1  christos   bp->cond_list = new_cond;
   1266  1.1  christos }
   1267  1.1  christos 
   1268  1.1  christos /* Add a target-side condition CONDITION to a breakpoint.  */
   1269  1.1  christos 
   1270  1.1  christos int
   1271  1.1  christos add_breakpoint_condition (struct gdb_breakpoint *bp, const char **condition)
   1272  1.1  christos {
   1273  1.1  christos   const char *actparm = *condition;
   1274  1.1  christos   struct agent_expr *cond;
   1275  1.1  christos 
   1276  1.1  christos   if (condition == NULL)
   1277  1.1  christos     return 1;
   1278  1.1  christos 
   1279  1.1  christos   if (bp == NULL)
   1280  1.1  christos     return 0;
   1281  1.1  christos 
   1282  1.1  christos   cond = gdb_parse_agent_expr (&actparm);
   1283  1.1  christos 
   1284  1.1  christos   if (cond == NULL)
   1285  1.1  christos     {
   1286  1.1  christos       warning ("Condition evaluation failed. Assuming unconditional.");
   1287  1.1  christos       return 0;
   1288  1.1  christos     }
   1289  1.1  christos 
   1290  1.1  christos   add_condition_to_breakpoint (bp, cond);
   1291  1.1  christos 
   1292  1.1  christos   *condition = actparm;
   1293  1.1  christos 
   1294  1.1  christos   return 1;
   1295  1.1  christos }
   1296  1.1  christos 
   1297  1.1  christos /* Evaluate condition (if any) at breakpoint BP.  Return 1 if
   1298  1.1  christos    true and 0 otherwise.  */
   1299  1.1  christos 
   1300  1.1  christos static int
   1301  1.1  christos gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
   1302  1.1  christos {
   1303  1.1  christos   /* Fetch registers for the current inferior.  */
   1304  1.1  christos   struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
   1305  1.1  christos   ULONGEST value = 0;
   1306  1.1  christos   struct point_cond_list *cl;
   1307  1.1  christos   int err = 0;
   1308  1.1  christos   struct eval_agent_expr_context ctx;
   1309  1.1  christos 
   1310  1.1  christos   if (bp == NULL)
   1311  1.1  christos     return 0;
   1312  1.1  christos 
   1313  1.1  christos   /* Check if the breakpoint is unconditional.  If it is,
   1314  1.1  christos      the condition always evaluates to TRUE.  */
   1315  1.1  christos   if (bp->cond_list == NULL)
   1316  1.1  christos     return 1;
   1317  1.1  christos 
   1318  1.1  christos   ctx.regcache = get_thread_regcache (current_thread, 1);
   1319  1.1  christos   ctx.tframe = NULL;
   1320  1.1  christos   ctx.tpoint = NULL;
   1321  1.1  christos 
   1322  1.1  christos   /* Evaluate each condition in the breakpoint's list of conditions.
   1323  1.1  christos      Return true if any of the conditions evaluates to TRUE.
   1324  1.1  christos 
   1325  1.1  christos      If we failed to evaluate the expression, TRUE is returned.  This
   1326  1.1  christos      forces GDB to reevaluate the conditions.  */
   1327  1.1  christos   for (cl = bp->cond_list;
   1328  1.1  christos        cl && !value && !err; cl = cl->next)
   1329  1.1  christos     {
   1330  1.1  christos       /* Evaluate the condition.  */
   1331  1.1  christos       err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
   1332  1.1  christos     }
   1333  1.1  christos 
   1334  1.1  christos   if (err)
   1335  1.1  christos     return 1;
   1336  1.1  christos 
   1337  1.1  christos   return (value != 0);
   1338  1.1  christos }
   1339  1.1  christos 
   1340  1.1  christos int
   1341  1.1  christos gdb_condition_true_at_breakpoint (CORE_ADDR where)
   1342  1.1  christos {
   1343  1.1  christos   /* Only check code (software or hardware) breakpoints.  */
   1344  1.1  christos   return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
   1345  1.1  christos 	  || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
   1346  1.1  christos }
   1347  1.1  christos 
   1348  1.1  christos /* Add commands COMMANDS to GDBserver's breakpoint BP.  */
   1349  1.1  christos 
   1350  1.1  christos static void
   1351  1.1  christos add_commands_to_breakpoint (struct gdb_breakpoint *bp,
   1352  1.1  christos 			    struct agent_expr *commands, int persist)
   1353  1.1  christos {
   1354  1.1  christos   struct point_command_list *new_cmd;
   1355  1.1  christos 
   1356  1.1  christos   /* Create new command.  */
   1357  1.1  christos   new_cmd = XCNEW (struct point_command_list);
   1358  1.1  christos   new_cmd->cmd = commands;
   1359  1.1  christos   new_cmd->persistence = persist;
   1360  1.1  christos 
   1361  1.1  christos   /* Add commands to the list.  */
   1362  1.1  christos   new_cmd->next = bp->command_list;
   1363  1.1  christos   bp->command_list = new_cmd;
   1364  1.1  christos }
   1365  1.1  christos 
   1366  1.1  christos /* Add a target-side command COMMAND to the breakpoint at ADDR.  */
   1367  1.1  christos 
   1368  1.1  christos int
   1369  1.1  christos add_breakpoint_commands (struct gdb_breakpoint *bp, const char **command,
   1370  1.1  christos 			 int persist)
   1371  1.1  christos {
   1372  1.1  christos   const char *actparm = *command;
   1373  1.1  christos   struct agent_expr *cmd;
   1374  1.1  christos 
   1375  1.1  christos   if (command == NULL)
   1376  1.1  christos     return 1;
   1377  1.1  christos 
   1378  1.1  christos   if (bp == NULL)
   1379  1.1  christos     return 0;
   1380  1.1  christos 
   1381  1.1  christos   cmd = gdb_parse_agent_expr (&actparm);
   1382  1.1  christos 
   1383  1.1  christos   if (cmd == NULL)
   1384  1.1  christos     {
   1385  1.1  christos       warning ("Command evaluation failed. Disabling.");
   1386  1.1  christos       return 0;
   1387  1.1  christos     }
   1388  1.1  christos 
   1389  1.1  christos   add_commands_to_breakpoint (bp, cmd, persist);
   1390  1.1  christos 
   1391  1.1  christos   *command = actparm;
   1392  1.1  christos 
   1393  1.1  christos   return 1;
   1394  1.1  christos }
   1395  1.1  christos 
   1396  1.1  christos /* Return true if there are no commands to run at this location,
   1397  1.1  christos    which likely means we want to report back to GDB.  */
   1398  1.1  christos 
   1399  1.1  christos static int
   1400  1.1  christos gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
   1401  1.1  christos {
   1402  1.1  christos   struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
   1403  1.1  christos 
   1404  1.1  christos   if (bp == NULL)
   1405  1.1  christos     return 1;
   1406  1.1  christos 
   1407  1.1  christos   if (debug_threads)
   1408  1.1  christos     debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
   1409  1.1  christos 		  paddress (addr), z_type,
   1410  1.1  christos 		  phex_nz ((uintptr_t) bp->command_list, 0));
   1411  1.1  christos   return (bp->command_list == NULL);
   1412  1.1  christos }
   1413  1.1  christos 
   1414  1.1  christos /* Return true if there are no commands to run at this location,
   1415  1.1  christos    which likely means we want to report back to GDB.  */
   1416  1.1  christos 
   1417  1.1  christos int
   1418  1.1  christos gdb_no_commands_at_breakpoint (CORE_ADDR where)
   1419  1.1  christos {
   1420  1.1  christos   /* Only check code (software or hardware) breakpoints.  */
   1421  1.1  christos   return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
   1422  1.1  christos 	  && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
   1423  1.1  christos }
   1424  1.1  christos 
   1425  1.1  christos /* Run a breakpoint's commands.  Returns 0 if there was a problem
   1426  1.1  christos    running any command, 1 otherwise.  */
   1427  1.1  christos 
   1428  1.1  christos static int
   1429  1.1  christos run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
   1430  1.1  christos {
   1431  1.1  christos   /* Fetch registers for the current inferior.  */
   1432  1.1  christos   struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
   1433  1.1  christos   ULONGEST value = 0;
   1434  1.1  christos   struct point_command_list *cl;
   1435  1.1  christos   int err = 0;
   1436  1.1  christos   struct eval_agent_expr_context ctx;
   1437  1.1  christos 
   1438  1.1  christos   if (bp == NULL)
   1439  1.1  christos     return 1;
   1440  1.1  christos 
   1441  1.1  christos   ctx.regcache = get_thread_regcache (current_thread, 1);
   1442  1.1  christos   ctx.tframe = NULL;
   1443  1.1  christos   ctx.tpoint = NULL;
   1444  1.1  christos 
   1445  1.1  christos   for (cl = bp->command_list;
   1446  1.1  christos        cl && !value && !err; cl = cl->next)
   1447  1.1  christos     {
   1448  1.1  christos       /* Run the command.  */
   1449  1.1  christos       err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
   1450  1.1  christos 
   1451  1.1  christos       /* If one command has a problem, stop digging the hole deeper.  */
   1452  1.1  christos       if (err)
   1453  1.1  christos 	return 0;
   1454  1.1  christos     }
   1455  1.1  christos 
   1456  1.1  christos   return 1;
   1457  1.1  christos }
   1458  1.1  christos 
   1459  1.1  christos void
   1460  1.1  christos run_breakpoint_commands (CORE_ADDR where)
   1461  1.1  christos {
   1462  1.1  christos   /* Only check code (software or hardware) breakpoints.  If one
   1463  1.1  christos      command has a problem, stop digging the hole deeper.  */
   1464  1.1  christos   if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
   1465  1.1  christos     run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
   1466  1.1  christos }
   1467  1.1  christos 
   1468  1.1  christos /* See mem-break.h.  */
   1469  1.1  christos 
   1470  1.1  christos int
   1471  1.1  christos gdb_breakpoint_here (CORE_ADDR where)
   1472  1.1  christos {
   1473  1.1  christos   /* Only check code (software or hardware) breakpoints.  */
   1474  1.1  christos   return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
   1475  1.1  christos 	  || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
   1476  1.1  christos }
   1477  1.1  christos 
   1478  1.1  christos void
   1479  1.1  christos set_single_step_breakpoint (CORE_ADDR stop_at, ptid_t ptid)
   1480  1.1  christos {
   1481  1.1  christos   struct single_step_breakpoint *bp;
   1482  1.1  christos 
   1483  1.1  christos   gdb_assert (current_ptid.pid () == ptid.pid ());
   1484  1.1  christos 
   1485  1.1  christos   bp = (struct single_step_breakpoint *) set_breakpoint_type_at (single_step_breakpoint,
   1486  1.1  christos 								stop_at, NULL);
   1487  1.1  christos   bp->ptid = ptid;
   1488  1.1  christos }
   1489  1.1  christos 
   1490  1.1  christos void
   1491  1.1  christos delete_single_step_breakpoints (struct thread_info *thread)
   1492  1.1  christos {
   1493  1.1  christos   struct process_info *proc = get_thread_process (thread);
   1494  1.1  christos   struct breakpoint *bp, **bp_link;
   1495  1.1  christos 
   1496  1.1  christos   bp = proc->breakpoints;
   1497  1.1  christos   bp_link = &proc->breakpoints;
   1498  1.1  christos 
   1499  1.1  christos   while (bp)
   1500  1.1  christos     {
   1501  1.1  christos       if (bp->type == single_step_breakpoint
   1502  1.1  christos 	  && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
   1503  1.1  christos 	{
   1504  1.1  christos 	  struct thread_info *saved_thread = current_thread;
   1505  1.1  christos 
   1506  1.1  christos 	  current_thread = thread;
   1507  1.1  christos 	  *bp_link = bp->next;
   1508  1.1  christos 	  release_breakpoint (proc, bp);
   1509  1.1  christos 	  bp = *bp_link;
   1510  1.1  christos 	  current_thread = saved_thread;
   1511  1.1  christos 	}
   1512  1.1  christos       else
   1513  1.1  christos 	{
   1514  1.1  christos 	  bp_link = &bp->next;
   1515  1.1  christos 	  bp = *bp_link;
   1516  1.1  christos 	}
   1517  1.1  christos     }
   1518  1.1  christos }
   1519  1.1  christos 
   1520  1.1  christos static void
   1521  1.1  christos uninsert_raw_breakpoint (struct raw_breakpoint *bp)
   1522  1.1  christos {
   1523  1.1  christos   if (bp->inserted < 0)
   1524  1.1  christos     {
   1525  1.1  christos       if (debug_threads)
   1526  1.1  christos 	debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
   1527  1.1  christos 		      paddress (bp->pc));
   1528  1.1  christos     }
   1529  1.1  christos   else if (bp->inserted > 0)
   1530  1.1  christos     {
   1531  1.1  christos       int err;
   1532  1.1  christos 
   1533  1.1  christos       bp->inserted = 0;
   1534  1.1  christos 
   1535  1.1  christos       err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
   1536  1.1  christos       if (err != 0)
   1537  1.1  christos 	{
   1538  1.1  christos 	  bp->inserted = 1;
   1539  1.1  christos 
   1540  1.1  christos 	  if (debug_threads)
   1541  1.1  christos 	    debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
   1542  1.1  christos 			  paddress (bp->pc));
   1543  1.1  christos 	}
   1544  1.1  christos     }
   1545  1.1  christos }
   1546  1.1  christos 
   1547  1.1  christos void
   1548  1.1  christos uninsert_breakpoints_at (CORE_ADDR pc)
   1549  1.1  christos {
   1550  1.1  christos   struct process_info *proc = current_process ();
   1551  1.1  christos   struct raw_breakpoint *bp;
   1552  1.1  christos   int found = 0;
   1553  1.1  christos 
   1554  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
   1555  1.1  christos     if ((bp->raw_type == raw_bkpt_type_sw
   1556  1.1  christos 	 || bp->raw_type == raw_bkpt_type_hw)
   1557  1.1  christos 	&& bp->pc == pc)
   1558  1.1  christos       {
   1559  1.1  christos 	found = 1;
   1560  1.1  christos 
   1561  1.1  christos 	if (bp->inserted)
   1562  1.1  christos 	  uninsert_raw_breakpoint (bp);
   1563  1.1  christos       }
   1564  1.1  christos 
   1565  1.1  christos   if (!found)
   1566  1.1  christos     {
   1567  1.1  christos       /* This can happen when we remove all breakpoints while handling
   1568  1.1  christos 	 a step-over.  */
   1569  1.1  christos       if (debug_threads)
   1570  1.1  christos 	debug_printf ("Could not find breakpoint at 0x%s "
   1571  1.1  christos 		      "in list (uninserting).\n",
   1572  1.1  christos 		      paddress (pc));
   1573  1.1  christos     }
   1574  1.1  christos }
   1575  1.1  christos 
   1576  1.1  christos void
   1577  1.1  christos uninsert_all_breakpoints (void)
   1578  1.1  christos {
   1579  1.1  christos   struct process_info *proc = current_process ();
   1580  1.1  christos   struct raw_breakpoint *bp;
   1581  1.1  christos 
   1582  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
   1583  1.1  christos     if ((bp->raw_type == raw_bkpt_type_sw
   1584  1.1  christos 	 || bp->raw_type == raw_bkpt_type_hw)
   1585  1.1  christos 	&& bp->inserted)
   1586  1.1  christos       uninsert_raw_breakpoint (bp);
   1587  1.1  christos }
   1588  1.1  christos 
   1589  1.1  christos void
   1590  1.1  christos uninsert_single_step_breakpoints (struct thread_info *thread)
   1591  1.1  christos {
   1592  1.1  christos   struct process_info *proc = get_thread_process (thread);
   1593  1.1  christos   struct breakpoint *bp;
   1594  1.1  christos 
   1595  1.1  christos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
   1596  1.1  christos     {
   1597  1.1  christos     if (bp->type == single_step_breakpoint
   1598  1.1  christos 	&& ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
   1599  1.1  christos       {
   1600  1.1  christos 	gdb_assert (bp->raw->inserted > 0);
   1601  1.1  christos 
   1602  1.1  christos 	/* Only uninsert the raw breakpoint if it only belongs to a
   1603  1.1  christos 	   reinsert breakpoint.  */
   1604  1.1  christos 	if (bp->raw->refcount == 1)
   1605  1.1  christos 	  {
   1606  1.1  christos 	    struct thread_info *saved_thread = current_thread;
   1607  1.1  christos 
   1608  1.1  christos 	    current_thread = thread;
   1609  1.1  christos 	    uninsert_raw_breakpoint (bp->raw);
   1610  1.1  christos 	    current_thread = saved_thread;
   1611  1.1  christos 	  }
   1612  1.1  christos       }
   1613  1.1  christos     }
   1614  1.1  christos }
   1615  1.1  christos 
   1616  1.1  christos static void
   1617  1.1  christos reinsert_raw_breakpoint (struct raw_breakpoint *bp)
   1618  1.1  christos {
   1619  1.1  christos   int err;
   1620  1.1  christos 
   1621  1.1  christos   if (bp->inserted)
   1622  1.1  christos     return;
   1623  1.1  christos 
   1624  1.1  christos   err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
   1625  1.1  christos   if (err == 0)
   1626  1.1  christos     bp->inserted = 1;
   1627  1.1  christos   else if (debug_threads)
   1628  1.1  christos     debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
   1629  1.1  christos 		  paddress (bp->pc), err);
   1630  1.1  christos }
   1631  1.1  christos 
   1632  1.1  christos void
   1633  1.1  christos reinsert_breakpoints_at (CORE_ADDR pc)
   1634  1.1  christos {
   1635  1.1  christos   struct process_info *proc = current_process ();
   1636  1.1  christos   struct raw_breakpoint *bp;
   1637  1.1  christos   int found = 0;
   1638  1.1  christos 
   1639  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
   1640  1.1  christos     if ((bp->raw_type == raw_bkpt_type_sw
   1641  1.1  christos 	 || bp->raw_type == raw_bkpt_type_hw)
   1642  1.1  christos 	&& bp->pc == pc)
   1643  1.1  christos       {
   1644  1.1  christos 	found = 1;
   1645  1.1  christos 
   1646  1.1  christos 	reinsert_raw_breakpoint (bp);
   1647  1.1  christos       }
   1648  1.1  christos 
   1649  1.1  christos   if (!found)
   1650  1.1  christos     {
   1651  1.1  christos       /* This can happen when we remove all breakpoints while handling
   1652  1.1  christos 	 a step-over.  */
   1653  1.1  christos       if (debug_threads)
   1654  1.1  christos 	debug_printf ("Could not find raw breakpoint at 0x%s "
   1655  1.1  christos 		      "in list (reinserting).\n",
   1656  1.1  christos 		      paddress (pc));
   1657  1.1  christos     }
   1658  1.1  christos }
   1659  1.1  christos 
   1660  1.1  christos int
   1661  1.1  christos has_single_step_breakpoints (struct thread_info *thread)
   1662  1.1  christos {
   1663  1.1  christos   struct process_info *proc = get_thread_process (thread);
   1664  1.1  christos   struct breakpoint *bp, **bp_link;
   1665  1.1  christos 
   1666  1.1  christos   bp = proc->breakpoints;
   1667  1.1  christos   bp_link = &proc->breakpoints;
   1668  1.1  christos 
   1669  1.1  christos   while (bp)
   1670  1.1  christos     {
   1671  1.1  christos       if (bp->type == single_step_breakpoint
   1672  1.1  christos 	  && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
   1673  1.1  christos 	return 1;
   1674  1.1  christos       else
   1675  1.1  christos 	{
   1676  1.1  christos 	  bp_link = &bp->next;
   1677  1.1  christos 	  bp = *bp_link;
   1678  1.1  christos 	}
   1679  1.1  christos     }
   1680  1.1  christos 
   1681  1.1  christos   return 0;
   1682  1.1  christos }
   1683  1.1  christos 
   1684  1.1  christos void
   1685  1.1  christos reinsert_all_breakpoints (void)
   1686  1.1  christos {
   1687  1.1  christos   struct process_info *proc = current_process ();
   1688  1.1  christos   struct raw_breakpoint *bp;
   1689  1.1  christos 
   1690  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
   1691  1.1  christos     if ((bp->raw_type == raw_bkpt_type_sw
   1692  1.1  christos 	 || bp->raw_type == raw_bkpt_type_hw)
   1693  1.1  christos 	&& !bp->inserted)
   1694  1.1  christos       reinsert_raw_breakpoint (bp);
   1695  1.1  christos }
   1696  1.1  christos 
   1697  1.1  christos void
   1698  1.1  christos reinsert_single_step_breakpoints (struct thread_info *thread)
   1699  1.1  christos {
   1700  1.1  christos   struct process_info *proc = get_thread_process (thread);
   1701  1.1  christos   struct breakpoint *bp;
   1702  1.1  christos 
   1703  1.1  christos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
   1704  1.1  christos     {
   1705  1.1  christos       if (bp->type == single_step_breakpoint
   1706  1.1  christos 	  && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
   1707  1.1  christos 	{
   1708  1.1  christos 	  gdb_assert (bp->raw->inserted > 0);
   1709  1.1  christos 
   1710  1.1  christos 	  if (bp->raw->refcount == 1)
   1711  1.1  christos 	    {
   1712  1.1  christos 	      struct thread_info *saved_thread = current_thread;
   1713  1.1  christos 
   1714  1.1  christos 	      current_thread = thread;
   1715  1.1  christos 	      reinsert_raw_breakpoint (bp->raw);
   1716  1.1  christos 	      current_thread = saved_thread;
   1717  1.1  christos 	    }
   1718  1.1  christos 	}
   1719  1.1  christos     }
   1720  1.1  christos }
   1721  1.1  christos 
   1722  1.1  christos void
   1723  1.1  christos check_breakpoints (CORE_ADDR stop_pc)
   1724  1.1  christos {
   1725  1.1  christos   struct process_info *proc = current_process ();
   1726  1.1  christos   struct breakpoint *bp, **bp_link;
   1727  1.1  christos 
   1728  1.1  christos   bp = proc->breakpoints;
   1729  1.1  christos   bp_link = &proc->breakpoints;
   1730  1.1  christos 
   1731  1.1  christos   while (bp)
   1732  1.1  christos     {
   1733  1.1  christos       struct raw_breakpoint *raw = bp->raw;
   1734  1.1  christos 
   1735  1.1  christos       if ((raw->raw_type == raw_bkpt_type_sw
   1736  1.1  christos 	   || raw->raw_type == raw_bkpt_type_hw)
   1737  1.1  christos 	  && raw->pc == stop_pc)
   1738  1.1  christos 	{
   1739  1.1  christos 	  if (!raw->inserted)
   1740  1.1  christos 	    {
   1741  1.1  christos 	      warning ("Hit a removed breakpoint?");
   1742  1.1  christos 	      return;
   1743  1.1  christos 	    }
   1744  1.1  christos 
   1745  1.1  christos 	  if (bp->type == other_breakpoint)
   1746  1.1  christos 	    {
   1747  1.1  christos 	      struct other_breakpoint *other_bp
   1748  1.1  christos 		= (struct other_breakpoint *) bp;
   1749  1.1  christos 
   1750  1.1  christos 	      if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
   1751  1.1  christos 		{
   1752  1.1  christos 		  *bp_link = bp->next;
   1753  1.1  christos 
   1754  1.1  christos 		  release_breakpoint (proc, bp);
   1755  1.1  christos 
   1756  1.1  christos 		  bp = *bp_link;
   1757  1.1  christos 		  continue;
   1758  1.1  christos 		}
   1759  1.1  christos 	    }
   1760  1.1  christos 	}
   1761  1.1  christos 
   1762  1.1  christos       bp_link = &bp->next;
   1763  1.1  christos       bp = *bp_link;
   1764  1.1  christos     }
   1765  1.1  christos }
   1766  1.1  christos 
   1767  1.1  christos int
   1768  1.1  christos breakpoint_here (CORE_ADDR addr)
   1769  1.1  christos {
   1770  1.1  christos   struct process_info *proc = current_process ();
   1771  1.1  christos   struct raw_breakpoint *bp;
   1772  1.1  christos 
   1773  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
   1774  1.1  christos     if ((bp->raw_type == raw_bkpt_type_sw
   1775  1.1  christos 	 || bp->raw_type == raw_bkpt_type_hw)
   1776  1.1  christos 	&& bp->pc == addr)
   1777  1.1  christos       return 1;
   1778  1.1  christos 
   1779  1.1  christos   return 0;
   1780  1.1  christos }
   1781  1.1  christos 
   1782  1.1  christos int
   1783  1.1  christos breakpoint_inserted_here (CORE_ADDR addr)
   1784  1.1  christos {
   1785  1.1  christos   struct process_info *proc = current_process ();
   1786  1.1  christos   struct raw_breakpoint *bp;
   1787  1.1  christos 
   1788  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
   1789  1.1  christos     if ((bp->raw_type == raw_bkpt_type_sw
   1790  1.1  christos 	 || bp->raw_type == raw_bkpt_type_hw)
   1791  1.1  christos 	&& bp->pc == addr
   1792  1.1  christos 	&& bp->inserted)
   1793  1.1  christos       return 1;
   1794  1.1  christos 
   1795  1.1  christos   return 0;
   1796  1.1  christos }
   1797  1.1  christos 
   1798  1.1  christos /* See mem-break.h.  */
   1799  1.1  christos 
   1800  1.1  christos int
   1801  1.1  christos software_breakpoint_inserted_here (CORE_ADDR addr)
   1802  1.1  christos {
   1803  1.1  christos   struct process_info *proc = current_process ();
   1804  1.1  christos   struct raw_breakpoint *bp;
   1805  1.1  christos 
   1806  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
   1807  1.1  christos     if (bp->raw_type == raw_bkpt_type_sw
   1808  1.1  christos 	&& bp->pc == addr
   1809  1.1  christos 	&& bp->inserted)
   1810  1.1  christos       return 1;
   1811  1.1  christos 
   1812  1.1  christos   return 0;
   1813  1.1  christos }
   1814  1.1  christos 
   1815  1.1  christos /* See mem-break.h.  */
   1816  1.1  christos 
   1817  1.1  christos int
   1818  1.1  christos hardware_breakpoint_inserted_here (CORE_ADDR addr)
   1819  1.1  christos {
   1820  1.1  christos   struct process_info *proc = current_process ();
   1821  1.1  christos   struct raw_breakpoint *bp;
   1822  1.1  christos 
   1823  1.1  christos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
   1824  1.1  christos     if (bp->raw_type == raw_bkpt_type_hw
   1825  1.1  christos 	&& bp->pc == addr
   1826  1.1  christos 	&& bp->inserted)
   1827  1.1  christos       return 1;
   1828  1.1  christos 
   1829  1.1  christos   return 0;
   1830  1.1  christos }
   1831  1.1  christos 
   1832  1.1  christos /* See mem-break.h.  */
   1833  1.1  christos 
   1834  1.1  christos int
   1835  1.1  christos single_step_breakpoint_inserted_here (CORE_ADDR addr)
   1836  1.1  christos {
   1837  1.1  christos   struct process_info *proc = current_process ();
   1838  1.1  christos   struct breakpoint *bp;
   1839  1.1  christos 
   1840  1.1  christos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
   1841  1.1  christos     if (bp->type == single_step_breakpoint
   1842  1.1  christos 	&& bp->raw->pc == addr
   1843  1.1  christos 	&& bp->raw->inserted)
   1844  1.1  christos       return 1;
   1845  1.1  christos 
   1846  1.1  christos   return 0;
   1847  1.1  christos }
   1848  1.1  christos 
   1849  1.1  christos static int
   1850  1.1  christos validate_inserted_breakpoint (struct raw_breakpoint *bp)
   1851  1.1  christos {
   1852  1.1  christos   unsigned char *buf;
   1853  1.1  christos   int err;
   1854  1.1  christos 
   1855  1.1  christos   gdb_assert (bp->inserted);
   1856  1.1  christos   gdb_assert (bp->raw_type == raw_bkpt_type_sw);
   1857  1.1  christos 
   1858  1.1  christos   buf = (unsigned char *) alloca (bp_size (bp));
   1859  1.1  christos   err = the_target->read_memory (bp->pc, buf, bp_size (bp));
   1860  1.1  christos   if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
   1861  1.1  christos     {
   1862  1.1  christos       /* Tag it as gone.  */
   1863  1.1  christos       bp->inserted = -1;
   1864  1.1  christos       return 0;
   1865  1.1  christos     }
   1866  1.1  christos 
   1867  1.1  christos   return 1;
   1868  1.1  christos }
   1869  1.1  christos 
   1870  1.1  christos static void
   1871  1.1  christos delete_disabled_breakpoints (void)
   1872  1.1  christos {
   1873  1.1  christos   struct process_info *proc = current_process ();
   1874  1.1  christos   struct breakpoint *bp, *next;
   1875  1.1  christos 
   1876  1.1  christos   for (bp = proc->breakpoints; bp != NULL; bp = next)
   1877  1.1  christos     {
   1878  1.1  christos       next = bp->next;
   1879  1.1  christos       if (bp->raw->inserted < 0)
   1880  1.1  christos 	{
   1881  1.1  christos 	  /* If single_step_breakpoints become disabled, that means the
   1882  1.1  christos 	     manipulations (insertion and removal) of them are wrong.  */
   1883  1.1  christos 	  gdb_assert (bp->type != single_step_breakpoint);
   1884  1.1  christos 	  delete_breakpoint_1 (proc, bp);
   1885  1.1  christos 	}
   1886  1.1  christos     }
   1887  1.1  christos }
   1888  1.1  christos 
   1889  1.1  christos /* Check if breakpoints we inserted still appear to be inserted.  They
   1890  1.1  christos    may disappear due to a shared library unload, and worse, a new
   1891  1.1  christos    shared library may be reloaded at the same address as the
   1892  1.1  christos    previously unloaded one.  If that happens, we should make sure that
   1893  1.1  christos    the shadow memory of the old breakpoints isn't used when reading or
   1894  1.1  christos    writing memory.  */
   1895  1.1  christos 
   1896  1.1  christos void
   1897  1.1  christos validate_breakpoints (void)
   1898  1.1  christos {
   1899  1.1  christos   struct process_info *proc = current_process ();
   1900  1.1  christos   struct breakpoint *bp;
   1901  1.1  christos 
   1902  1.1  christos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
   1903  1.1  christos     {
   1904  1.1  christos       struct raw_breakpoint *raw = bp->raw;
   1905  1.1  christos 
   1906  1.1  christos       if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
   1907  1.1  christos 	validate_inserted_breakpoint (raw);
   1908  1.1  christos     }
   1909  1.1  christos 
   1910  1.1  christos   delete_disabled_breakpoints ();
   1911  1.1  christos }
   1912  1.1  christos 
   1913  1.1  christos void
   1914  1.1  christos check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
   1915  1.1  christos {
   1916  1.1  christos   struct process_info *proc = current_process ();
   1917  1.1  christos   struct raw_breakpoint *bp = proc->raw_breakpoints;
   1918  1.1  christos   struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
   1919  1.1  christos   CORE_ADDR mem_end = mem_addr + mem_len;
   1920  1.1  christos   int disabled_one = 0;
   1921  1.1  christos 
   1922  1.1  christos   for (; jp != NULL; jp = jp->next)
   1923  1.1  christos     {
   1924  1.1  christos       CORE_ADDR bp_end = jp->pc + jp->length;
   1925  1.1  christos       CORE_ADDR start, end;
   1926  1.1  christos       int copy_offset, copy_len, buf_offset;
   1927  1.1  christos 
   1928  1.1  christos       gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
   1929  1.1  christos 		  || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
   1930  1.1  christos 
   1931  1.1  christos       if (mem_addr >= bp_end)
   1932  1.1  christos 	continue;
   1933  1.1  christos       if (jp->pc >= mem_end)
   1934  1.1  christos 	continue;
   1935  1.1  christos 
   1936  1.1  christos       start = jp->pc;
   1937  1.1  christos       if (mem_addr > start)
   1938  1.1  christos 	start = mem_addr;
   1939  1.1  christos 
   1940  1.1  christos       end = bp_end;
   1941  1.1  christos       if (end > mem_end)
   1942  1.1  christos 	end = mem_end;
   1943  1.1  christos 
   1944  1.1  christos       copy_len = end - start;
   1945  1.1  christos       copy_offset = start - jp->pc;
   1946  1.1  christos       buf_offset = start - mem_addr;
   1947  1.1  christos 
   1948  1.1  christos       if (jp->inserted)
   1949  1.1  christos 	memcpy (buf + buf_offset,
   1950  1.1  christos 		fast_tracepoint_jump_shadow (jp) + copy_offset,
   1951  1.1  christos 		copy_len);
   1952  1.1  christos     }
   1953  1.1  christos 
   1954  1.1  christos   for (; bp != NULL; bp = bp->next)
   1955  1.1  christos     {
   1956  1.1  christos       CORE_ADDR bp_end = bp->pc + bp_size (bp);
   1957  1.1  christos       CORE_ADDR start, end;
   1958  1.1  christos       int copy_offset, copy_len, buf_offset;
   1959  1.1  christos 
   1960  1.1  christos       if (bp->raw_type != raw_bkpt_type_sw)
   1961  1.1  christos 	continue;
   1962  1.1  christos 
   1963  1.1  christos       gdb_assert (bp->old_data >= buf + mem_len
   1964  1.1  christos 		  || buf >= &bp->old_data[sizeof (bp->old_data)]);
   1965  1.1  christos 
   1966  1.1  christos       if (mem_addr >= bp_end)
   1967  1.1  christos 	continue;
   1968  1.1  christos       if (bp->pc >= mem_end)
   1969  1.1  christos 	continue;
   1970  1.1  christos 
   1971  1.1  christos       start = bp->pc;
   1972  1.1  christos       if (mem_addr > start)
   1973  1.1  christos 	start = mem_addr;
   1974  1.1  christos 
   1975  1.1  christos       end = bp_end;
   1976  1.1  christos       if (end > mem_end)
   1977  1.1  christos 	end = mem_end;
   1978  1.1  christos 
   1979  1.1  christos       copy_len = end - start;
   1980  1.1  christos       copy_offset = start - bp->pc;
   1981  1.1  christos       buf_offset = start - mem_addr;
   1982  1.1  christos 
   1983  1.1  christos       if (bp->inserted > 0)
   1984  1.1  christos 	{
   1985  1.1  christos 	  if (validate_inserted_breakpoint (bp))
   1986  1.1  christos 	    memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
   1987  1.1  christos 	  else
   1988  1.1  christos 	    disabled_one = 1;
   1989  1.1  christos 	}
   1990  1.1  christos     }
   1991  1.1  christos 
   1992  1.1  christos   if (disabled_one)
   1993  1.1  christos     delete_disabled_breakpoints ();
   1994  1.1  christos }
   1995  1.1  christos 
   1996  1.1  christos void
   1997  1.1  christos check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
   1998  1.1  christos 		 const unsigned char *myaddr, int mem_len)
   1999  1.1  christos {
   2000  1.1  christos   struct process_info *proc = current_process ();
   2001  1.1  christos   struct raw_breakpoint *bp = proc->raw_breakpoints;
   2002  1.1  christos   struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
   2003  1.1  christos   CORE_ADDR mem_end = mem_addr + mem_len;
   2004  1.1  christos   int disabled_one = 0;
   2005  1.1  christos 
   2006  1.1  christos   /* First fast tracepoint jumps, then breakpoint traps on top.  */
   2007  1.1  christos 
   2008  1.1  christos   for (; jp != NULL; jp = jp->next)
   2009  1.1  christos     {
   2010  1.1  christos       CORE_ADDR jp_end = jp->pc + jp->length;
   2011  1.1  christos       CORE_ADDR start, end;
   2012  1.1  christos       int copy_offset, copy_len, buf_offset;
   2013  1.1  christos 
   2014  1.1  christos       gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
   2015  1.1  christos 		  || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
   2016  1.1  christos       gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
   2017  1.1  christos 		  || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
   2018  1.1  christos 
   2019  1.1  christos       if (mem_addr >= jp_end)
   2020  1.1  christos 	continue;
   2021  1.1  christos       if (jp->pc >= mem_end)
   2022  1.1  christos 	continue;
   2023  1.1  christos 
   2024  1.1  christos       start = jp->pc;
   2025  1.1  christos       if (mem_addr > start)
   2026  1.1  christos 	start = mem_addr;
   2027  1.1  christos 
   2028  1.1  christos       end = jp_end;
   2029  1.1  christos       if (end > mem_end)
   2030  1.1  christos 	end = mem_end;
   2031  1.1  christos 
   2032  1.1  christos       copy_len = end - start;
   2033  1.1  christos       copy_offset = start - jp->pc;
   2034  1.1  christos       buf_offset = start - mem_addr;
   2035  1.1  christos 
   2036  1.1  christos       memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
   2037  1.1  christos 	      myaddr + buf_offset, copy_len);
   2038  1.1  christos       if (jp->inserted)
   2039  1.1  christos 	memcpy (buf + buf_offset,
   2040  1.1  christos 		fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
   2041  1.1  christos     }
   2042  1.1  christos 
   2043  1.1  christos   for (; bp != NULL; bp = bp->next)
   2044  1.1  christos     {
   2045  1.1  christos       CORE_ADDR bp_end = bp->pc + bp_size (bp);
   2046  1.1  christos       CORE_ADDR start, end;
   2047  1.1  christos       int copy_offset, copy_len, buf_offset;
   2048  1.1  christos 
   2049  1.1  christos       if (bp->raw_type != raw_bkpt_type_sw)
   2050  1.1  christos 	continue;
   2051  1.1  christos 
   2052  1.1  christos       gdb_assert (bp->old_data >= myaddr + mem_len
   2053  1.1  christos 		  || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
   2054  1.1  christos 
   2055  1.1  christos       if (mem_addr >= bp_end)
   2056  1.1  christos 	continue;
   2057  1.1  christos       if (bp->pc >= mem_end)
   2058  1.1  christos 	continue;
   2059  1.1  christos 
   2060  1.1  christos       start = bp->pc;
   2061  1.1  christos       if (mem_addr > start)
   2062  1.1  christos 	start = mem_addr;
   2063  1.1  christos 
   2064  1.1  christos       end = bp_end;
   2065  1.1  christos       if (end > mem_end)
   2066  1.1  christos 	end = mem_end;
   2067  1.1  christos 
   2068  1.1  christos       copy_len = end - start;
   2069  1.1  christos       copy_offset = start - bp->pc;
   2070  1.1  christos       buf_offset = start - mem_addr;
   2071  1.1  christos 
   2072  1.1  christos       memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
   2073  1.1  christos       if (bp->inserted > 0)
   2074  1.1  christos 	{
   2075  1.1  christos 	  if (validate_inserted_breakpoint (bp))
   2076  1.1  christos 	    memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
   2077  1.1  christos 	  else
   2078  1.1  christos 	    disabled_one = 1;
   2079  1.1  christos 	}
   2080  1.1  christos     }
   2081  1.1  christos 
   2082  1.1  christos   if (disabled_one)
   2083  1.1  christos     delete_disabled_breakpoints ();
   2084  1.1  christos }
   2085  1.1  christos 
   2086  1.1  christos /* Delete all breakpoints, and un-insert them from the inferior.  */
   2087  1.1  christos 
   2088  1.1  christos void
   2089  1.1  christos delete_all_breakpoints (void)
   2090  1.1  christos {
   2091  1.1  christos   struct process_info *proc = current_process ();
   2092  1.1  christos 
   2093  1.1  christos   while (proc->breakpoints)
   2094  1.1  christos     delete_breakpoint_1 (proc, proc->breakpoints);
   2095  1.1  christos }
   2096  1.1  christos 
   2097  1.1  christos /* Clear the "inserted" flag in all breakpoints.  */
   2098  1.1  christos 
   2099  1.1  christos void
   2100  1.1  christos mark_breakpoints_out (struct process_info *proc)
   2101  1.1  christos {
   2102  1.1  christos   struct raw_breakpoint *raw_bp;
   2103  1.1  christos 
   2104  1.1  christos   for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
   2105  1.1  christos     raw_bp->inserted = 0;
   2106  1.1  christos }
   2107  1.1  christos 
   2108  1.1  christos /* Release all breakpoints, but do not try to un-insert them from the
   2109  1.1  christos    inferior.  */
   2110  1.1  christos 
   2111  1.1  christos void
   2112  1.1  christos free_all_breakpoints (struct process_info *proc)
   2113  1.1  christos {
   2114  1.1  christos   mark_breakpoints_out (proc);
   2115  1.1  christos 
   2116  1.1  christos   /* Note: use PROC explicitly instead of deferring to
   2117  1.1  christos      delete_all_breakpoints --- CURRENT_INFERIOR may already have been
   2118  1.1  christos      released when we get here.  There should be no call to
   2119  1.1  christos      current_process from here on.  */
   2120  1.1  christos   while (proc->breakpoints)
   2121  1.1  christos     delete_breakpoint_1 (proc, proc->breakpoints);
   2122  1.1  christos }
   2123  1.1  christos 
   2124  1.1  christos /* Clone an agent expression.  */
   2125  1.1  christos 
   2126  1.1  christos static struct agent_expr *
   2127  1.1  christos clone_agent_expr (const struct agent_expr *src_ax)
   2128  1.1  christos {
   2129  1.1  christos   struct agent_expr *ax;
   2130  1.1  christos 
   2131  1.1  christos   ax = XCNEW (struct agent_expr);
   2132  1.1  christos   ax->length = src_ax->length;
   2133  1.1  christos   ax->bytes = (unsigned char *) xcalloc (ax->length, 1);
   2134  1.1  christos   memcpy (ax->bytes, src_ax->bytes, ax->length);
   2135  1.1  christos   return ax;
   2136  1.1  christos }
   2137  1.1  christos 
   2138  1.1  christos /* Deep-copy the contents of one breakpoint to another.  */
   2139  1.1  christos 
   2140  1.1  christos static struct breakpoint *
   2141  1.1  christos clone_one_breakpoint (const struct breakpoint *src, ptid_t ptid)
   2142  1.1  christos {
   2143  1.1  christos   struct breakpoint *dest;
   2144  1.1  christos   struct raw_breakpoint *dest_raw;
   2145  1.1  christos 
   2146  1.1  christos   /* Clone the raw breakpoint.  */
   2147  1.1  christos   dest_raw = XCNEW (struct raw_breakpoint);
   2148  1.1  christos   dest_raw->raw_type = src->raw->raw_type;
   2149  1.1  christos   dest_raw->refcount = src->raw->refcount;
   2150  1.1  christos   dest_raw->pc = src->raw->pc;
   2151  1.1  christos   dest_raw->kind = src->raw->kind;
   2152  1.1  christos   memcpy (dest_raw->old_data, src->raw->old_data, MAX_BREAKPOINT_LEN);
   2153  1.1  christos   dest_raw->inserted = src->raw->inserted;
   2154  1.1  christos 
   2155  1.1  christos   /* Clone the high-level breakpoint.  */
   2156  1.1  christos   if (is_gdb_breakpoint (src->type))
   2157  1.1  christos     {
   2158  1.1  christos       struct gdb_breakpoint *gdb_dest = XCNEW (struct gdb_breakpoint);
   2159  1.1  christos       struct point_cond_list *current_cond;
   2160  1.1  christos       struct point_cond_list *new_cond;
   2161  1.1  christos       struct point_cond_list *cond_tail = NULL;
   2162  1.1  christos       struct point_command_list *current_cmd;
   2163  1.1  christos       struct point_command_list *new_cmd;
   2164  1.1  christos       struct point_command_list *cmd_tail = NULL;
   2165  1.1  christos 
   2166  1.1  christos       /* Clone the condition list.  */
   2167  1.1  christos       for (current_cond = ((struct gdb_breakpoint *) src)->cond_list;
   2168  1.1  christos 	   current_cond != NULL;
   2169  1.1  christos 	   current_cond = current_cond->next)
   2170  1.1  christos 	{
   2171  1.1  christos 	  new_cond = XCNEW (struct point_cond_list);
   2172  1.1  christos 	  new_cond->cond = clone_agent_expr (current_cond->cond);
   2173  1.1  christos 	  APPEND_TO_LIST (&gdb_dest->cond_list, new_cond, cond_tail);
   2174  1.1  christos 	}
   2175  1.1  christos 
   2176  1.1  christos       /* Clone the command list.  */
   2177  1.1  christos       for (current_cmd = ((struct gdb_breakpoint *) src)->command_list;
   2178  1.1  christos 	   current_cmd != NULL;
   2179  1.1  christos 	   current_cmd = current_cmd->next)
   2180  1.1  christos 	{
   2181  1.1  christos 	  new_cmd = XCNEW (struct point_command_list);
   2182  1.1  christos 	  new_cmd->cmd = clone_agent_expr (current_cmd->cmd);
   2183  1.1  christos 	  new_cmd->persistence = current_cmd->persistence;
   2184  1.1  christos 	  APPEND_TO_LIST (&gdb_dest->command_list, new_cmd, cmd_tail);
   2185  1.1  christos 	}
   2186  1.1  christos 
   2187  1.1  christos       dest = (struct breakpoint *) gdb_dest;
   2188  1.1  christos     }
   2189  1.1  christos   else if (src->type == other_breakpoint)
   2190  1.1  christos     {
   2191  1.1  christos       struct other_breakpoint *other_dest = XCNEW (struct other_breakpoint);
   2192  1.1  christos 
   2193  1.1  christos       other_dest->handler = ((struct other_breakpoint *) src)->handler;
   2194  1.1  christos       dest = (struct breakpoint *) other_dest;
   2195  1.1  christos     }
   2196  1.1  christos   else if (src->type == single_step_breakpoint)
   2197  1.1  christos     {
   2198  1.1  christos       struct single_step_breakpoint *ss_dest
   2199  1.1  christos 	= XCNEW (struct single_step_breakpoint);
   2200  1.1  christos 
   2201  1.1  christos       dest = (struct breakpoint *) ss_dest;
   2202  1.1  christos       /* Since single-step breakpoint is thread specific, don't copy
   2203  1.1  christos 	 thread id from SRC, use ID instead.  */
   2204  1.1  christos       ss_dest->ptid = ptid;
   2205  1.1  christos     }
   2206  1.1  christos   else
   2207  1.1  christos     gdb_assert_not_reached ("unhandled breakpoint type");
   2208  1.1  christos 
   2209  1.1  christos   dest->type = src->type;
   2210  1.1  christos   dest->raw = dest_raw;
   2211  1.1  christos 
   2212  1.1  christos   return dest;
   2213  1.1  christos }
   2214  1.1  christos 
   2215  1.1  christos /* See mem-break.h.  */
   2216  1.1  christos 
   2217  1.1  christos void
   2218  1.1  christos clone_all_breakpoints (struct thread_info *child_thread,
   2219  1.1  christos 		       const struct thread_info *parent_thread)
   2220  1.1  christos {
   2221  1.1  christos   const struct breakpoint *bp;
   2222  1.1  christos   struct breakpoint *new_bkpt;
   2223  1.1  christos   struct breakpoint *bkpt_tail = NULL;
   2224  1.1  christos   struct raw_breakpoint *raw_bkpt_tail = NULL;
   2225  1.1  christos   struct process_info *child_proc = get_thread_process (child_thread);
   2226  1.1  christos   struct process_info *parent_proc = get_thread_process (parent_thread);
   2227  1.1  christos   struct breakpoint **new_list = &child_proc->breakpoints;
   2228  1.1  christos   struct raw_breakpoint **new_raw_list = &child_proc->raw_breakpoints;
   2229  1.1  christos 
   2230  1.1  christos   for (bp = parent_proc->breakpoints; bp != NULL; bp = bp->next)
   2231  1.1  christos     {
   2232  1.1  christos       new_bkpt = clone_one_breakpoint (bp, ptid_of (child_thread));
   2233  1.1  christos       APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
   2234  1.1  christos       APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);
   2235  1.1  christos     }
   2236  1.1  christos }
   2237