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