gdb/gdbserver/mem-break.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Memory breakpoint operations for the remote server for GDB.
  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.

  3.    Contributed by MontaVista Software.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "server.h"
  16. #include "regcache.h"
  17. #include "ax.h"
  18. #include <stdint.h>

  19. const unsigned char *breakpoint_data;
  20. int breakpoint_len;

  21. #define MAX_BREAKPOINT_LEN 8

  22. /* GDB will never try to install multiple breakpoints at the same
  23.    address.  However, we can see GDB requesting to insert a breakpoint
  24.    at an address is had already inserted one previously in a few
  25.    situations.

  26.    - The RSP documentation on Z packets says that to avoid potential
  27.    problems with duplicate packets, the operations should be
  28.    implemented in an idempotent way.

  29.    - A breakpoint is set at ADDR, an address in a shared library.
  30.    Then the shared library is unloaded.  And then another, unrelated,
  31.    breakpoint at ADDR is set.  There is not breakpoint removal request
  32.    between the first and the second breakpoint.

  33.    - When GDB wants to update the target-side breakpoint conditions or
  34.    commands, it re-inserts the breakpoint, with updated
  35.    conditions/commands associated.

  36.    Also, we need to keep track of internal breakpoints too, so we do
  37.    need to be able to install multiple breakpoints at the same address
  38.    transparently.

  39.    We keep track of two different, and closely related structures.  A
  40.    raw breakpoint, which manages the low level, close to the metal
  41.    aspect of a breakpoint.  It holds the breakpoint address, and for
  42.    software breakpoints, a buffer holding a copy of the instructions
  43.    that would be in memory had not been a breakpoint there (we call
  44.    that the shadow memory of the breakpoint).  We occasionally need to
  45.    temporarilly uninsert a breakpoint without the client knowing about
  46.    it (e.g., to step over an internal breakpoint), so we keep an
  47.    `inserted' state associated with this low level breakpoint
  48.    structure.  There can only be one such object for a given address.
  49.    Then, we have (a bit higher level) breakpoints.  This structure
  50.    holds a callback to be called whenever a breakpoint is hit, a
  51.    high-level type, and a link to a low level raw breakpoint.  There
  52.    can be many high-level breakpoints at the same address, and all of
  53.    them will point to the same raw breakpoint, which is reference
  54.    counted.  */

  55. /* The low level, physical, raw breakpoint.  */
  56. struct raw_breakpoint
  57. {
  58.   struct raw_breakpoint *next;

  59.   /* The low level type of the breakpoint (software breakpoint,
  60.      watchpoint, etc.)  */
  61.   enum raw_bkpt_type raw_type;

  62.   /* A reference count.  Each high level breakpoint referencing this
  63.      raw breakpoint accounts for one reference.  */
  64.   int refcount;

  65.   /* The breakpoint's insertion address.  There can only be one raw
  66.      breakpoint for a given PC.  */
  67.   CORE_ADDR pc;

  68.   /* The breakpoint's size.  */
  69.   int size;

  70.   /* The breakpoint's shadow memory.  */
  71.   unsigned char old_data[MAX_BREAKPOINT_LEN];

  72.   /* Positive if this breakpoint is currently inserted in the
  73.      inferior.  Negative if it was, but we've detected that it's now
  74.      gone.  Zero if not inserted.  */
  75.   int inserted;
  76. };

  77. /* The type of a breakpoint.  */
  78. enum bkpt_type
  79.   {
  80.     /* A GDB breakpoint, requested with a Z0 packet.  */
  81.     gdb_breakpoint_Z0,

  82.     /* A GDB hardware breakpoint, requested with a Z1 packet.  */
  83.     gdb_breakpoint_Z1,

  84.     /* A GDB write watchpoint, requested with a Z2 packet.  */
  85.     gdb_breakpoint_Z2,

  86.     /* A GDB read watchpoint, requested with a Z3 packet.  */
  87.     gdb_breakpoint_Z3,

  88.     /* A GDB access watchpoint, requested with a Z4 packet.  */
  89.     gdb_breakpoint_Z4,

  90.     /* A basic-software-single-step breakpoint.  */
  91.     reinsert_breakpoint,

  92.     /* Any other breakpoint type that doesn't require specific
  93.        treatment goes here.  E.g., an event breakpoint.  */
  94.     other_breakpoint,
  95.   };

  96. struct point_cond_list
  97. {
  98.   /* Pointer to the agent expression that is the breakpoint's
  99.      conditional.  */
  100.   struct agent_expr *cond;

  101.   /* Pointer to the next condition.  */
  102.   struct point_cond_list *next;
  103. };

  104. struct point_command_list
  105. {
  106.   /* Pointer to the agent expression that is the breakpoint's
  107.      commands.  */
  108.   struct agent_expr *cmd;

  109.   /* Flag that is true if this command should run even while GDB is
  110.      disconnected.  */
  111.   int persistence;

  112.   /* Pointer to the next command.  */
  113.   struct point_command_list *next;
  114. };

  115. /* A high level (in gdbserver's perspective) breakpoint.  */
  116. struct breakpoint
  117. {
  118.   struct breakpoint *next;

  119.   /* The breakpoint's type.  */
  120.   enum bkpt_type type;

  121.   /* Pointer to the condition list that should be evaluated on
  122.      the target or NULL if the breakpoint is unconditional or
  123.      if GDB doesn't want us to evaluate the conditionals on the
  124.      target's side.  */
  125.   struct point_cond_list *cond_list;

  126.   /* Point to the list of commands to run when this is hit.  */
  127.   struct point_command_list *command_list;

  128.   /* Link to this breakpoint's raw breakpoint.  This is always
  129.      non-NULL.  */
  130.   struct raw_breakpoint *raw;

  131.   /* Function to call when we hit this breakpoint.  If it returns 1,
  132.      the breakpoint shall be deleted; 0 or if this callback is NULL,
  133.      it will be left inserted.  */
  134.   int (*handler) (CORE_ADDR);
  135. };

  136. /* See mem-break.h.  */

  137. enum target_hw_bp_type
  138. raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
  139. {
  140.   switch (raw_type)
  141.     {
  142.     case raw_bkpt_type_hw:
  143.       return hw_execute;
  144.     case raw_bkpt_type_write_wp:
  145.       return hw_write;
  146.     case raw_bkpt_type_read_wp:
  147.       return hw_read;
  148.     case raw_bkpt_type_access_wp:
  149.       return hw_access;
  150.     default:
  151.       internal_error (__FILE__, __LINE__,
  152.                       "bad raw breakpoint type %d", (int) raw_type);
  153.     }
  154. }

  155. /* See mem-break.h.  */

  156. static enum bkpt_type
  157. Z_packet_to_bkpt_type (char z_type)
  158. {
  159.   gdb_assert ('0' <= z_type && z_type <= '4');

  160.   return gdb_breakpoint_Z0 + (z_type - '0');
  161. }

  162. /* See mem-break.h.  */

  163. enum raw_bkpt_type
  164. Z_packet_to_raw_bkpt_type (char z_type)
  165. {
  166.   switch (z_type)
  167.     {
  168.     case Z_PACKET_SW_BP:
  169.       return raw_bkpt_type_sw;
  170.     case Z_PACKET_HW_BP:
  171.       return raw_bkpt_type_hw;
  172.     case Z_PACKET_WRITE_WP:
  173.       return raw_bkpt_type_write_wp;
  174.     case Z_PACKET_READ_WP:
  175.       return raw_bkpt_type_read_wp;
  176.     case Z_PACKET_ACCESS_WP:
  177.       return raw_bkpt_type_access_wp;
  178.     default:
  179.       gdb_assert_not_reached ("unhandled Z packet type.");
  180.     }
  181. }

  182. int
  183. any_persistent_commands ()
  184. {
  185.   struct process_info *proc = current_process ();
  186.   struct breakpoint *bp;
  187.   struct point_command_list *cl;

  188.   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  189.     {
  190.       for (cl = bp->command_list; cl != NULL; cl = cl->next)
  191.         if (cl->persistence)
  192.           return 1;
  193.     }

  194.   return 0;
  195. }

  196. /* Find low-level breakpoint of type TYPE at address ADDR that is not
  197.    insert-disabled.  Returns NULL if not found.  */

  198. static struct raw_breakpoint *
  199. find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
  200. {
  201.   struct process_info *proc = current_process ();
  202.   struct raw_breakpoint *bp;

  203.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  204.     if (bp->pc == addr
  205.         && bp->raw_type == type
  206.         && bp->inserted >= 0)
  207.       return bp;

  208.   return NULL;
  209. }

  210. /* Find low-level breakpoint of type TYPE at address ADDR.  Returns
  211.    NULL if not found.  */

  212. static struct raw_breakpoint *
  213. find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int size)
  214. {
  215.   struct process_info *proc = current_process ();
  216.   struct raw_breakpoint *bp;

  217.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  218.     if (bp->pc == addr && bp->raw_type == type && bp->size == size)
  219.       return bp;

  220.   return NULL;
  221. }

  222. /* See mem-break.h.  */

  223. int
  224. insert_memory_breakpoint (struct raw_breakpoint *bp)
  225. {
  226.   unsigned char buf[MAX_BREAKPOINT_LEN];
  227.   int err;

  228.   if (breakpoint_data == NULL)
  229.     return 1;

  230.   /* If the architecture treats the size field of Z packets as a
  231.      'kind' field, then we'll need to be able to know which is the
  232.      breakpoint instruction too.  */
  233.   if (bp->size != breakpoint_len)
  234.     {
  235.       if (debug_threads)
  236.         debug_printf ("Don't know how to insert breakpoints of size %d.\n",
  237.                       bp->size);
  238.       return -1;
  239.     }

  240.   /* Note that there can be fast tracepoint jumps installed in the
  241.      same memory range, so to get at the original memory, we need to
  242.      use read_inferior_memory, which masks those out.  */
  243.   err = read_inferior_memory (bp->pc, buf, breakpoint_len);
  244.   if (err != 0)
  245.     {
  246.       if (debug_threads)
  247.         debug_printf ("Failed to read shadow memory of"
  248.                       " breakpoint at 0x%s (%s).\n",
  249.                       paddress (bp->pc), strerror (err));
  250.     }
  251.   else
  252.     {
  253.       memcpy (bp->old_data, buf, breakpoint_len);

  254.       err = (*the_target->write_memory) (bp->pc, breakpoint_data,
  255.                                          breakpoint_len);
  256.       if (err != 0)
  257.         {
  258.           if (debug_threads)
  259.             debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
  260.                           paddress (bp->pc), strerror (err));
  261.         }
  262.     }
  263.   return err != 0 ? -1 : 0;
  264. }

  265. /* See mem-break.h  */

  266. int
  267. remove_memory_breakpoint (struct raw_breakpoint *bp)
  268. {
  269.   unsigned char buf[MAX_BREAKPOINT_LEN];
  270.   int err;

  271.   /* Since there can be trap breakpoints inserted in the same address
  272.      range, we use `write_inferior_memory', which takes care of
  273.      layering breakpoints on top of fast tracepoints, and on top of
  274.      the buffer we pass it.  This works because the caller has already
  275.      either unlinked the breakpoint or marked it uninserted.  Also
  276.      note that we need to pass the current shadow contents, because
  277.      write_inferior_memory updates any shadow memory with what we pass
  278.      here, and we want that to be a nop.  */
  279.   memcpy (buf, bp->old_data, breakpoint_len);
  280.   err = write_inferior_memory (bp->pc, buf, breakpoint_len);
  281.   if (err != 0)
  282.     {
  283.       if (debug_threads)
  284.         debug_printf ("Failed to uninsert raw breakpoint "
  285.                       "at 0x%s (%s) while deleting it.\n",
  286.                       paddress (bp->pc), strerror (err));
  287.     }
  288.   return err != 0 ? -1 : 0;
  289. }

  290. /* Set a RAW breakpoint of type TYPE and size SIZE at WHERE.  On
  291.    success, a pointer to the new breakpoint is returned.  On failure,
  292.    returns NULL and writes the error code to *ERR.  */

  293. static struct raw_breakpoint *
  294. set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int size,
  295.                        int *err)
  296. {
  297.   struct process_info *proc = current_process ();
  298.   struct raw_breakpoint *bp;

  299.   if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
  300.     {
  301.       bp = find_enabled_raw_code_breakpoint_at (where, type);
  302.       if (bp != NULL && bp->size != size)
  303.         {
  304.           /* A different size than previously seen.  The previous
  305.              breakpoint must be gone then.  */
  306.           if (debug_threads)
  307.             debug_printf ("Inconsistent breakpoint size?  Was %d, now %d.\n",
  308.                           bp->size, size);
  309.           bp->inserted = -1;
  310.           bp = NULL;
  311.         }
  312.     }
  313.   else
  314.     bp = find_raw_breakpoint_at (where, type, size);

  315.   if (bp != NULL)
  316.     {
  317.       bp->refcount++;
  318.       return bp;
  319.     }

  320.   bp = xcalloc (1, sizeof (*bp));
  321.   bp->pc = where;
  322.   bp->size = size;
  323.   bp->refcount = 1;
  324.   bp->raw_type = type;

  325.   *err = the_target->insert_point (bp->raw_type, bp->pc, bp->size, bp);
  326.   if (*err != 0)
  327.     {
  328.       if (debug_threads)
  329.         debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
  330.                       paddress (where), *err);
  331.       free (bp);
  332.       return NULL;
  333.     }

  334.   bp->inserted = 1;
  335.   /* Link the breakpoint in.  */
  336.   bp->next = proc->raw_breakpoints;
  337.   proc->raw_breakpoints = bp;
  338.   return bp;
  339. }

  340. /* Notice that breakpoint traps are always installed on top of fast
  341.    tracepoint jumps.  This is even if the fast tracepoint is installed
  342.    at a later time compared to when the breakpoint was installed.
  343.    This means that a stopping breakpoint or tracepoint has higher
  344.    "priority".  In turn, this allows having fast and slow tracepoints
  345.    (and breakpoints) at the same address behave correctly.  */


  346. /* A fast tracepoint jump.  */

  347. struct fast_tracepoint_jump
  348. {
  349.   struct fast_tracepoint_jump *next;

  350.   /* A reference count.  GDB can install more than one fast tracepoint
  351.      at the same address (each with its own action list, for
  352.      example).  */
  353.   int refcount;

  354.   /* The fast tracepoint's insertion address.  There can only be one
  355.      of these for a given PC.  */
  356.   CORE_ADDR pc;

  357.   /* Non-zero if this fast tracepoint jump is currently inserted in
  358.      the inferior.  */
  359.   int inserted;

  360.   /* The length of the jump instruction.  */
  361.   int length;

  362.   /* A poor-man's flexible array member, holding both the jump
  363.      instruction to insert, and a copy of the instruction that would
  364.      be in memory had not been a jump there (the shadow memory of the
  365.      tracepoint jump).  */
  366.   unsigned char insn_and_shadow[0];
  367. };

  368. /* Fast tracepoint FP's jump instruction to insert.  */
  369. #define fast_tracepoint_jump_insn(fp) \
  370.   ((fp)->insn_and_shadow + 0)

  371. /* The shadow memory of fast tracepoint jump FP.  */
  372. #define fast_tracepoint_jump_shadow(fp) \
  373.   ((fp)->insn_and_shadow + (fp)->length)


  374. /* Return the fast tracepoint jump set at WHERE.  */

  375. static struct fast_tracepoint_jump *
  376. find_fast_tracepoint_jump_at (CORE_ADDR where)
  377. {
  378.   struct process_info *proc = current_process ();
  379.   struct fast_tracepoint_jump *jp;

  380.   for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
  381.     if (jp->pc == where)
  382.       return jp;

  383.   return NULL;
  384. }

  385. int
  386. fast_tracepoint_jump_here (CORE_ADDR where)
  387. {
  388.   struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);

  389.   return (jp != NULL);
  390. }

  391. int
  392. delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
  393. {
  394.   struct fast_tracepoint_jump *bp, **bp_link;
  395.   int ret;
  396.   struct process_info *proc = current_process ();

  397.   bp = proc->fast_tracepoint_jumps;
  398.   bp_link = &proc->fast_tracepoint_jumps;

  399.   while (bp)
  400.     {
  401.       if (bp == todel)
  402.         {
  403.           if (--bp->refcount == 0)
  404.             {
  405.               struct fast_tracepoint_jump *prev_bp_link = *bp_link;
  406.               unsigned char *buf;

  407.               /* Unlink it.  */
  408.               *bp_link = bp->next;

  409.               /* Since there can be breakpoints inserted in the same
  410.                  address range, we use `write_inferior_memory', which
  411.                  takes care of layering breakpoints on top of fast
  412.                  tracepoints, and on top of the buffer we pass it.
  413.                  This works because we've already unlinked the fast
  414.                  tracepoint jump above.  Also note that we need to
  415.                  pass the current shadow contents, because
  416.                  write_inferior_memory updates any shadow memory with
  417.                  what we pass here, and we want that to be a nop.  */
  418.               buf = alloca (bp->length);
  419.               memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
  420.               ret = write_inferior_memory (bp->pc, buf, bp->length);
  421.               if (ret != 0)
  422.                 {
  423.                   /* Something went wrong, relink the jump.  */
  424.                   *bp_link = prev_bp_link;

  425.                   if (debug_threads)
  426.                     debug_printf ("Failed to uninsert fast tracepoint jump "
  427.                                   "at 0x%s (%s) while deleting it.\n",
  428.                                   paddress (bp->pc), strerror (ret));
  429.                   return ret;
  430.                 }

  431.               free (bp);
  432.             }

  433.           return 0;
  434.         }
  435.       else
  436.         {
  437.           bp_link = &bp->next;
  438.           bp = *bp_link;
  439.         }
  440.     }

  441.   warning ("Could not find fast tracepoint jump in list.");
  442.   return ENOENT;
  443. }

  444. void
  445. inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
  446. {
  447.   jp->refcount++;
  448. }

  449. struct fast_tracepoint_jump *
  450. set_fast_tracepoint_jump (CORE_ADDR where,
  451.                           unsigned char *insn, ULONGEST length)
  452. {
  453.   struct process_info *proc = current_process ();
  454.   struct fast_tracepoint_jump *jp;
  455.   int err;
  456.   unsigned char *buf;

  457.   /* We refcount fast tracepoint jumps.  Check if we already know
  458.      about a jump at this address.  */
  459.   jp = find_fast_tracepoint_jump_at (where);
  460.   if (jp != NULL)
  461.     {
  462.       jp->refcount++;
  463.       return jp;
  464.     }

  465.   /* We don't, so create a new object.  Double the length, because the
  466.      flexible array member holds both the jump insn, and the
  467.      shadow.  */
  468.   jp = xcalloc (1, sizeof (*jp) + (length * 2));
  469.   jp->pc = where;
  470.   jp->length = length;
  471.   memcpy (fast_tracepoint_jump_insn (jp), insn, length);
  472.   jp->refcount = 1;
  473.   buf = alloca (length);

  474.   /* Note that there can be trap breakpoints inserted in the same
  475.      address range.  To access the original memory contents, we use
  476.      `read_inferior_memory', which masks out breakpoints.  */
  477.   err = read_inferior_memory (where, buf, length);
  478.   if (err != 0)
  479.     {
  480.       if (debug_threads)
  481.         debug_printf ("Failed to read shadow memory of"
  482.                       " fast tracepoint at 0x%s (%s).\n",
  483.                       paddress (where), strerror (err));
  484.       free (jp);
  485.       return NULL;
  486.     }
  487.   memcpy (fast_tracepoint_jump_shadow (jp), buf, length);

  488.   /* Link the jump in.  */
  489.   jp->inserted = 1;
  490.   jp->next = proc->fast_tracepoint_jumps;
  491.   proc->fast_tracepoint_jumps = jp;

  492.   /* Since there can be trap breakpoints inserted in the same address
  493.      range, we use use `write_inferior_memory', which takes care of
  494.      layering breakpoints on top of fast tracepoints, on top of the
  495.      buffer we pass it.  This works because we've already linked in
  496.      the fast tracepoint jump above.  Also note that we need to pass
  497.      the current shadow contents, because write_inferior_memory
  498.      updates any shadow memory with what we pass here, and we want
  499.      that to be a nop.  */
  500.   err = write_inferior_memory (where, buf, length);
  501.   if (err != 0)
  502.     {
  503.       if (debug_threads)
  504.         debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
  505.                       paddress (where), strerror (err));

  506.       /* Unlink it.  */
  507.       proc->fast_tracepoint_jumps = jp->next;
  508.       free (jp);

  509.       return NULL;
  510.     }

  511.   return jp;
  512. }

  513. void
  514. uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
  515. {
  516.   struct fast_tracepoint_jump *jp;
  517.   int err;

  518.   jp = find_fast_tracepoint_jump_at (pc);
  519.   if (jp == NULL)
  520.     {
  521.       /* This can happen when we remove all breakpoints while handling
  522.          a step-over.  */
  523.       if (debug_threads)
  524.         debug_printf ("Could not find fast tracepoint jump at 0x%s "
  525.                       "in list (uninserting).\n",
  526.                       paddress (pc));
  527.       return;
  528.     }

  529.   if (jp->inserted)
  530.     {
  531.       unsigned char *buf;

  532.       jp->inserted = 0;

  533.       /* Since there can be trap breakpoints inserted in the same
  534.          address range, we use use `write_inferior_memory', which
  535.          takes care of layering breakpoints on top of fast
  536.          tracepoints, and on top of the buffer we pass it.  This works
  537.          because we've already marked the fast tracepoint fast
  538.          tracepoint jump uninserted above.  Also note that we need to
  539.          pass the current shadow contents, because
  540.          write_inferior_memory updates any shadow memory with what we
  541.          pass here, and we want that to be a nop.  */
  542.       buf = alloca (jp->length);
  543.       memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
  544.       err = write_inferior_memory (jp->pc, buf, jp->length);
  545.       if (err != 0)
  546.         {
  547.           jp->inserted = 1;

  548.           if (debug_threads)
  549.             debug_printf ("Failed to uninsert fast tracepoint jump at"
  550.                           " 0x%s (%s).\n",
  551.                           paddress (pc), strerror (err));
  552.         }
  553.     }
  554. }

  555. void
  556. reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
  557. {
  558.   struct fast_tracepoint_jump *jp;
  559.   int err;
  560.   unsigned char *buf;

  561.   jp = find_fast_tracepoint_jump_at (where);
  562.   if (jp == NULL)
  563.     {
  564.       /* This can happen when we remove breakpoints when a tracepoint
  565.          hit causes a tracing stop, while handling a step-over.  */
  566.       if (debug_threads)
  567.         debug_printf ("Could not find fast tracepoint jump at 0x%s "
  568.                       "in list (reinserting).\n",
  569.                       paddress (where));
  570.       return;
  571.     }

  572.   if (jp->inserted)
  573.     error ("Jump already inserted at reinsert time.");

  574.   jp->inserted = 1;

  575.   /* Since there can be trap breakpoints inserted in the same address
  576.      range, we use `write_inferior_memory', which takes care of
  577.      layering breakpoints on top of fast tracepoints, and on top of
  578.      the buffer we pass it.  This works because we've already marked
  579.      the fast tracepoint jump inserted above.  Also note that we need
  580.      to pass the current shadow contents, because
  581.      write_inferior_memory updates any shadow memory with what we pass
  582.      here, and we want that to be a nop.  */
  583.   buf = alloca (jp->length);
  584.   memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
  585.   err = write_inferior_memory (where, buf, jp->length);
  586.   if (err != 0)
  587.     {
  588.       jp->inserted = 0;

  589.       if (debug_threads)
  590.         debug_printf ("Failed to reinsert fast tracepoint jump at"
  591.                       " 0x%s (%s).\n",
  592.                       paddress (where), strerror (err));
  593.     }
  594. }

  595. /* Set a high-level breakpoint of type TYPE, with low level type
  596.    RAW_TYPE and size SIZE, at WHERE.  On success, a pointer to the new
  597.    breakpoint is returned.  On failure, returns NULL and writes the
  598.    error code to *ERR.  HANDLER is called when the breakpoint is hit.
  599.    HANDLER should return 1 if the breakpoint should be deleted, 0
  600.    otherwise.  */

  601. static struct breakpoint *
  602. set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
  603.                 CORE_ADDR where, int size,
  604.                 int (*handler) (CORE_ADDR), int *err)
  605. {
  606.   struct process_info *proc = current_process ();
  607.   struct breakpoint *bp;
  608.   struct raw_breakpoint *raw;

  609.   raw = set_raw_breakpoint_at (raw_type, where, size, err);

  610.   if (raw == NULL)
  611.     {
  612.       /* warn? */
  613.       return NULL;
  614.     }

  615.   bp = xcalloc (1, sizeof (struct breakpoint));
  616.   bp->type = type;

  617.   bp->raw = raw;
  618.   bp->handler = handler;

  619.   bp->next = proc->breakpoints;
  620.   proc->breakpoints = bp;

  621.   return bp;
  622. }

  623. /* See mem-break.h  */

  624. struct breakpoint *
  625. set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
  626. {
  627.   int err_ignored;

  628.   return set_breakpoint (other_breakpoint, raw_bkpt_type_sw,
  629.                          where, breakpoint_len, handler,
  630.                          &err_ignored);
  631. }


  632. static int
  633. delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
  634. {
  635.   struct raw_breakpoint *bp, **bp_link;
  636.   int ret;

  637.   bp = proc->raw_breakpoints;
  638.   bp_link = &proc->raw_breakpoints;

  639.   while (bp)
  640.     {
  641.       if (bp == todel)
  642.         {
  643.           if (bp->inserted > 0)
  644.             {
  645.               struct raw_breakpoint *prev_bp_link = *bp_link;

  646.               *bp_link = bp->next;

  647.               ret = the_target->remove_point (bp->raw_type, bp->pc, bp->size,
  648.                                               bp);
  649.               if (ret != 0)
  650.                 {
  651.                   /* Something went wrong, relink the breakpoint.  */
  652.                   *bp_link = prev_bp_link;

  653.                   if (debug_threads)
  654.                     debug_printf ("Failed to uninsert raw breakpoint "
  655.                                   "at 0x%s while deleting it.\n",
  656.                                   paddress (bp->pc));
  657.                   return ret;
  658.                 }
  659.             }
  660.           else
  661.             *bp_link = bp->next;

  662.           free (bp);
  663.           return 0;
  664.         }
  665.       else
  666.         {
  667.           bp_link = &bp->next;
  668.           bp = *bp_link;
  669.         }
  670.     }

  671.   warning ("Could not find raw breakpoint in list.");
  672.   return ENOENT;
  673. }

  674. static int
  675. release_breakpoint (struct process_info *proc, struct breakpoint *bp)
  676. {
  677.   int newrefcount;
  678.   int ret;

  679.   newrefcount = bp->raw->refcount - 1;
  680.   if (newrefcount == 0)
  681.     {
  682.       ret = delete_raw_breakpoint (proc, bp->raw);
  683.       if (ret != 0)
  684.         return ret;
  685.     }
  686.   else
  687.     bp->raw->refcount = newrefcount;

  688.   free (bp);

  689.   return 0;
  690. }

  691. static int
  692. delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
  693. {
  694.   struct breakpoint *bp, **bp_link;
  695.   int err;

  696.   bp = proc->breakpoints;
  697.   bp_link = &proc->breakpoints;

  698.   while (bp)
  699.     {
  700.       if (bp == todel)
  701.         {
  702.           *bp_link = bp->next;

  703.           err = release_breakpoint (proc, bp);
  704.           if (err != 0)
  705.             return err;

  706.           bp = *bp_link;
  707.           return 0;
  708.         }
  709.       else
  710.         {
  711.           bp_link = &bp->next;
  712.           bp = *bp_link;
  713.         }
  714.     }

  715.   warning ("Could not find breakpoint in list.");
  716.   return ENOENT;
  717. }

  718. int
  719. delete_breakpoint (struct breakpoint *todel)
  720. {
  721.   struct process_info *proc = current_process ();
  722.   return delete_breakpoint_1 (proc, todel);
  723. }

  724. /* Locate a GDB breakpoint of type Z_TYPE and size SIZE placed at
  725.    address ADDR and return a pointer to its structure.  If SIZE is -1,
  726.    the breakpoints' sizes are ignored.  */

  727. static struct breakpoint *
  728. find_gdb_breakpoint (char z_type, CORE_ADDR addr, int size)
  729. {
  730.   struct process_info *proc = current_process ();
  731.   struct breakpoint *bp;
  732.   enum bkpt_type type = Z_packet_to_bkpt_type (z_type);

  733.   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  734.     if (bp->type == type && bp->raw->pc == addr
  735.         && (size == -1 || bp->raw->size == size))
  736.       return bp;

  737.   return NULL;
  738. }

  739. static int
  740. z_type_supported (char z_type)
  741. {
  742.   return (z_type >= '0' && z_type <= '4'
  743.           && the_target->supports_z_point_type != NULL
  744.           && the_target->supports_z_point_type (z_type));
  745. }

  746. /* Create a new GDB breakpoint of type Z_TYPE at ADDR with size SIZE.
  747.    Returns a pointer to the newly created breakpoint on success.  On
  748.    failure returns NULL and sets *ERR to either -1 for error, or 1 if
  749.    Z_TYPE breakpoints are not supported on this target.  */

  750. static struct breakpoint *
  751. set_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int size, int *err)
  752. {
  753.   struct breakpoint *bp;
  754.   enum bkpt_type type;
  755.   enum raw_bkpt_type raw_type;

  756.   /* If we see GDB inserting a second code breakpoint at the same
  757.      address, then either: GDB is updating the breakpoint's conditions
  758.      or commands; or, the first breakpoint must have disappeared due
  759.      to a shared library unload.  On targets where the shared
  760.      libraries are handled by userspace, like SVR4, for example,
  761.      GDBserver can't tell if a library was loaded or unloaded.  Since
  762.      we refcount raw breakpoints, we must be careful to make sure GDB
  763.      breakpoints never contribute more than one reference.  if we
  764.      didn't do this, in case the previous breakpoint is gone due to a
  765.      shared library unload, we'd just increase the refcount of the
  766.      previous breakpoint at this address, but the trap was not planted
  767.      in the inferior anymore, thus the breakpoint would never be hit.
  768.      Note this must be careful to not create a window where
  769.      breakpoints are removed from the target, for non-stop, in case
  770.      the target can poke at memory while the program is running.  */
  771.   if (z_type == Z_PACKET_SW_BP
  772.       || z_type == Z_PACKET_HW_BP)
  773.     {
  774.       bp = find_gdb_breakpoint (z_type, addr, -1);

  775.       if (bp != NULL)
  776.         {
  777.           if (bp->raw->size != size)
  778.             {
  779.               /* A different size than previously seen.  The previous
  780.                  breakpoint must be gone then.  */
  781.               bp->raw->inserted = -1;
  782.               delete_breakpoint (bp);
  783.               bp = NULL;
  784.             }
  785.           else if (z_type == Z_PACKET_SW_BP)
  786.             {
  787.               /* Check if the breakpoint is actually gone from the
  788.                  target, due to an solib unload, for example.  Might
  789.                  as well validate _all_ breakpoints.  */
  790.               validate_breakpoints ();

  791.               /* Breakpoints that don't pass validation are
  792.                  deleted.  */
  793.               bp = find_gdb_breakpoint (z_type, addr, -1);
  794.             }
  795.         }
  796.     }
  797.   else
  798.     {
  799.       /* Data breakpoints for the same address but different size are
  800.          expected.  GDB doesn't merge these.  The backend gets to do
  801.          that if it wants/can.  */
  802.       bp = find_gdb_breakpoint (z_type, addr, size);
  803.     }

  804.   if (bp != NULL)
  805.     {
  806.       /* We already know about this breakpoint, there's nothing else
  807.          to do - GDB's reference is already accounted for.  Note that
  808.          whether the breakpoint inserted is left as is - we may be
  809.          stepping over it, for example, in which case we don't want to
  810.          force-reinsert it.  */
  811.       return bp;
  812.     }

  813.   raw_type = Z_packet_to_raw_bkpt_type (z_type);
  814.   type = Z_packet_to_bkpt_type (z_type);
  815.   return set_breakpoint (type, raw_type, addr, size, NULL, err);
  816. }

  817. static int
  818. check_gdb_bp_preconditions (char z_type, int *err)
  819. {
  820.   /* As software/memory breakpoints work by poking at memory, we need
  821.      to prepare to access memory.  If that operation fails, we need to
  822.      return error.  Seeing an error, if this is the first breakpoint
  823.      of that type that GDB tries to insert, GDB would then assume the
  824.      breakpoint type is supported, but it may actually not be.  So we
  825.      need to check whether the type is supported at all before
  826.      preparing to access memory.  */
  827.   if (!z_type_supported (z_type))
  828.     {
  829.       *err = 1;
  830.       return 0;
  831.     }
  832.   else if (current_thread == NULL)
  833.     {
  834.       *err = -1;
  835.       return 0;
  836.     }
  837.   else
  838.     return 1;
  839. }

  840. /* See mem-break.h.  This is a wrapper for set_gdb_breakpoint_1 that
  841.    knows to prepare to access memory for Z0 breakpoints.  */

  842. struct breakpoint *
  843. set_gdb_breakpoint (char z_type, CORE_ADDR addr, int size, int *err)
  844. {
  845.   struct breakpoint *bp;

  846.   if (!check_gdb_bp_preconditions (z_type, err))
  847.     return NULL;

  848.   /* If inserting a software/memory breakpoint, need to prepare to
  849.      access memory.  */
  850.   if (z_type == Z_PACKET_SW_BP)
  851.     {
  852.       *err = prepare_to_access_memory ();
  853.       if (*err != 0)
  854.         return NULL;
  855.     }

  856.   bp = set_gdb_breakpoint_1 (z_type, addr, size, err);

  857.   if (z_type == Z_PACKET_SW_BP)
  858.     done_accessing_memory ();

  859.   return bp;
  860. }

  861. /* Delete a GDB breakpoint of type Z_TYPE and size SIZE previously
  862.    inserted at ADDR with set_gdb_breakpoint_at.  Returns 0 on success,
  863.    -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
  864.    target.  */

  865. static int
  866. delete_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int size)
  867. {
  868.   struct breakpoint *bp;
  869.   int err;

  870.   bp = find_gdb_breakpoint (z_type, addr, size);
  871.   if (bp == NULL)
  872.     return -1;

  873.   /* Before deleting the breakpoint, make sure to free its condition
  874.      and command lists.  */
  875.   clear_breakpoint_conditions_and_commands (bp);
  876.   err = delete_breakpoint (bp);
  877.   if (err != 0)
  878.     return -1;

  879.   return 0;
  880. }

  881. /* See mem-break.h.  This is a wrapper for delete_gdb_breakpoint that
  882.    knows to prepare to access memory for Z0 breakpoints.  */

  883. int
  884. delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int size)
  885. {
  886.   int ret;

  887.   if (!check_gdb_bp_preconditions (z_type, &ret))
  888.     return ret;

  889.   /* If inserting a software/memory breakpoint, need to prepare to
  890.      access memory.  */
  891.   if (z_type == Z_PACKET_SW_BP)
  892.     {
  893.       int err;

  894.       err = prepare_to_access_memory ();
  895.       if (err != 0)
  896.         return -1;
  897.     }

  898.   ret = delete_gdb_breakpoint_1 (z_type, addr, size);

  899.   if (z_type == Z_PACKET_SW_BP)
  900.     done_accessing_memory ();

  901.   return ret;
  902. }

  903. /* Clear all conditions associated with a breakpoint.  */

  904. static void
  905. clear_breakpoint_conditions (struct breakpoint *bp)
  906. {
  907.   struct point_cond_list *cond;

  908.   if (bp->cond_list == NULL)
  909.     return;

  910.   cond = bp->cond_list;

  911.   while (cond != NULL)
  912.     {
  913.       struct point_cond_list *cond_next;

  914.       cond_next = cond->next;
  915.       gdb_free_agent_expr (cond->cond);
  916.       free (cond);
  917.       cond = cond_next;
  918.     }

  919.   bp->cond_list = NULL;
  920. }

  921. /* Clear all commands associated with a breakpoint.  */

  922. static void
  923. clear_breakpoint_commands (struct breakpoint *bp)
  924. {
  925.   struct point_command_list *cmd;

  926.   if (bp->command_list == NULL)
  927.     return;

  928.   cmd = bp->command_list;

  929.   while (cmd != NULL)
  930.     {
  931.       struct point_command_list *cmd_next;

  932.       cmd_next = cmd->next;
  933.       gdb_free_agent_expr (cmd->cmd);
  934.       free (cmd);
  935.       cmd = cmd_next;
  936.     }

  937.   bp->command_list = NULL;
  938. }

  939. void
  940. clear_breakpoint_conditions_and_commands (struct breakpoint *bp)
  941. {
  942.   clear_breakpoint_conditions (bp);
  943.   clear_breakpoint_commands (bp);
  944. }

  945. /* Add condition CONDITION to GDBserver's breakpoint BP.  */

  946. static void
  947. add_condition_to_breakpoint (struct breakpoint *bp,
  948.                              struct agent_expr *condition)
  949. {
  950.   struct point_cond_list *new_cond;

  951.   /* Create new condition.  */
  952.   new_cond = xcalloc (1, sizeof (*new_cond));
  953.   new_cond->cond = condition;

  954.   /* Add condition to the list.  */
  955.   new_cond->next = bp->cond_list;
  956.   bp->cond_list = new_cond;
  957. }

  958. /* Add a target-side condition CONDITION to a breakpoint.  */

  959. int
  960. add_breakpoint_condition (struct breakpoint *bp, char **condition)
  961. {
  962.   char *actparm = *condition;
  963.   struct agent_expr *cond;

  964.   if (condition == NULL)
  965.     return 1;

  966.   if (bp == NULL)
  967.     return 0;

  968.   cond = gdb_parse_agent_expr (&actparm);

  969.   if (cond == NULL)
  970.     {
  971.       fprintf (stderr, "Condition evaluation failed. "
  972.                "Assuming unconditional.\n");
  973.       return 0;
  974.     }

  975.   add_condition_to_breakpoint (bp, cond);

  976.   *condition = actparm;

  977.   return 1;
  978. }

  979. /* Evaluate condition (if any) at breakpoint BP.  Return 1 if
  980.    true and 0 otherwise.  */

  981. static int
  982. gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
  983. {
  984.   /* Fetch registers for the current inferior.  */
  985.   struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
  986.   ULONGEST value = 0;
  987.   struct point_cond_list *cl;
  988.   int err = 0;
  989.   struct eval_agent_expr_context ctx;

  990.   if (bp == NULL)
  991.     return 0;

  992.   /* Check if the breakpoint is unconditional.  If it is,
  993.      the condition always evaluates to TRUE.  */
  994.   if (bp->cond_list == NULL)
  995.     return 1;

  996.   ctx.regcache = get_thread_regcache (current_thread, 1);
  997.   ctx.tframe = NULL;
  998.   ctx.tpoint = NULL;

  999.   /* Evaluate each condition in the breakpoint's list of conditions.
  1000.      Return true if any of the conditions evaluates to TRUE.

  1001.      If we failed to evaluate the expression, TRUE is returned.  This
  1002.      forces GDB to reevaluate the conditions.  */
  1003.   for (cl = bp->cond_list;
  1004.        cl && !value && !err; cl = cl->next)
  1005.     {
  1006.       /* Evaluate the condition.  */
  1007.       err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
  1008.     }

  1009.   if (err)
  1010.     return 1;

  1011.   return (value != 0);
  1012. }

  1013. int
  1014. gdb_condition_true_at_breakpoint (CORE_ADDR where)
  1015. {
  1016.   /* Only check code (software or hardware) breakpoints.  */
  1017.   return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
  1018.           || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
  1019. }

  1020. /* Add commands COMMANDS to GDBserver's breakpoint BP.  */

  1021. void
  1022. add_commands_to_breakpoint (struct breakpoint *bp,
  1023.                             struct agent_expr *commands, int persist)
  1024. {
  1025.   struct point_command_list *new_cmd;

  1026.   /* Create new command.  */
  1027.   new_cmd = xcalloc (1, sizeof (*new_cmd));
  1028.   new_cmd->cmd = commands;
  1029.   new_cmd->persistence = persist;

  1030.   /* Add commands to the list.  */
  1031.   new_cmd->next = bp->command_list;
  1032.   bp->command_list = new_cmd;
  1033. }

  1034. /* Add a target-side command COMMAND to the breakpoint at ADDR.  */

  1035. int
  1036. add_breakpoint_commands (struct breakpoint *bp, char **command,
  1037.                          int persist)
  1038. {
  1039.   char *actparm = *command;
  1040.   struct agent_expr *cmd;

  1041.   if (command == NULL)
  1042.     return 1;

  1043.   if (bp == NULL)
  1044.     return 0;

  1045.   cmd = gdb_parse_agent_expr (&actparm);

  1046.   if (cmd == NULL)
  1047.     {
  1048.       fprintf (stderr, "Command evaluation failed. "
  1049.                "Disabling.\n");
  1050.       return 0;
  1051.     }

  1052.   add_commands_to_breakpoint (bp, cmd, persist);

  1053.   *command = actparm;

  1054.   return 1;
  1055. }

  1056. /* Return true if there are no commands to run at this location,
  1057.    which likely means we want to report back to GDB.  */

  1058. static int
  1059. gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
  1060. {
  1061.   struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);

  1062.   if (bp == NULL)
  1063.     return 1;

  1064.   if (debug_threads)
  1065.     debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
  1066.                   paddress (addr), z_type,
  1067.                   phex_nz ((uintptr_t) bp->command_list, 0));
  1068.   return (bp->command_list == NULL);
  1069. }

  1070. /* Return true if there are no commands to run at this location,
  1071.    which likely means we want to report back to GDB.  */

  1072. int
  1073. gdb_no_commands_at_breakpoint (CORE_ADDR where)
  1074. {
  1075.   /* Only check code (software or hardware) breakpoints.  */
  1076.   return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
  1077.           && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
  1078. }

  1079. /* Run a breakpoint's commands.  Returns 0 if there was a problem
  1080.    running any command, 1 otherwise.  */

  1081. static int
  1082. run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
  1083. {
  1084.   /* Fetch registers for the current inferior.  */
  1085.   struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
  1086.   ULONGEST value = 0;
  1087.   struct point_command_list *cl;
  1088.   int err = 0;
  1089.   struct eval_agent_expr_context ctx;

  1090.   if (bp == NULL)
  1091.     return 1;

  1092.   ctx.regcache = get_thread_regcache (current_thread, 1);
  1093.   ctx.tframe = NULL;
  1094.   ctx.tpoint = NULL;

  1095.   for (cl = bp->command_list;
  1096.        cl && !value && !err; cl = cl->next)
  1097.     {
  1098.       /* Run the command.  */
  1099.       err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);

  1100.       /* If one command has a problem, stop digging the hole deeper.  */
  1101.       if (err)
  1102.         return 0;
  1103.     }

  1104.   return 1;
  1105. }

  1106. void
  1107. run_breakpoint_commands (CORE_ADDR where)
  1108. {
  1109.   /* Only check code (software or hardware) breakpoints.  If one
  1110.      command has a problem, stop digging the hole deeper.  */
  1111.   if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
  1112.     run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
  1113. }

  1114. /* See mem-break.h.  */

  1115. int
  1116. gdb_breakpoint_here (CORE_ADDR where)
  1117. {
  1118.   /* Only check code (software or hardware) breakpoints.  */
  1119.   return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
  1120.           || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
  1121. }

  1122. void
  1123. set_reinsert_breakpoint (CORE_ADDR stop_at)
  1124. {
  1125.   struct breakpoint *bp;

  1126.   bp = set_breakpoint_at (stop_at, NULL);
  1127.   bp->type = reinsert_breakpoint;
  1128. }

  1129. void
  1130. delete_reinsert_breakpoints (void)
  1131. {
  1132.   struct process_info *proc = current_process ();
  1133.   struct breakpoint *bp, **bp_link;

  1134.   bp = proc->breakpoints;
  1135.   bp_link = &proc->breakpoints;

  1136.   while (bp)
  1137.     {
  1138.       if (bp->type == reinsert_breakpoint)
  1139.         {
  1140.           *bp_link = bp->next;
  1141.           release_breakpoint (proc, bp);
  1142.           bp = *bp_link;
  1143.         }
  1144.       else
  1145.         {
  1146.           bp_link = &bp->next;
  1147.           bp = *bp_link;
  1148.         }
  1149.     }
  1150. }

  1151. static void
  1152. uninsert_raw_breakpoint (struct raw_breakpoint *bp)
  1153. {
  1154.   if (bp->inserted < 0)
  1155.     {
  1156.       if (debug_threads)
  1157.         debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
  1158.                       paddress (bp->pc));
  1159.     }
  1160.   else if (bp->inserted > 0)
  1161.     {
  1162.       int err;

  1163.       bp->inserted = 0;

  1164.       err = the_target->remove_point (bp->raw_type, bp->pc, bp->size, bp);
  1165.       if (err != 0)
  1166.         {
  1167.           bp->inserted = 1;

  1168.           if (debug_threads)
  1169.             debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
  1170.                           paddress (bp->pc));
  1171.         }
  1172.     }
  1173. }

  1174. void
  1175. uninsert_breakpoints_at (CORE_ADDR pc)
  1176. {
  1177.   struct process_info *proc = current_process ();
  1178.   struct raw_breakpoint *bp;
  1179.   int found = 0;

  1180.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1181.     if ((bp->raw_type == raw_bkpt_type_sw
  1182.          || bp->raw_type == raw_bkpt_type_hw)
  1183.         && bp->pc == pc)
  1184.       {
  1185.         found = 1;

  1186.         if (bp->inserted)
  1187.           uninsert_raw_breakpoint (bp);
  1188.       }

  1189.   if (!found)
  1190.     {
  1191.       /* This can happen when we remove all breakpoints while handling
  1192.          a step-over.  */
  1193.       if (debug_threads)
  1194.         debug_printf ("Could not find breakpoint at 0x%s "
  1195.                       "in list (uninserting).\n",
  1196.                       paddress (pc));
  1197.     }
  1198. }

  1199. void
  1200. uninsert_all_breakpoints (void)
  1201. {
  1202.   struct process_info *proc = current_process ();
  1203.   struct raw_breakpoint *bp;

  1204.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1205.     if ((bp->raw_type == raw_bkpt_type_sw
  1206.          || bp->raw_type == raw_bkpt_type_hw)
  1207.         && bp->inserted)
  1208.       uninsert_raw_breakpoint (bp);
  1209. }

  1210. static void
  1211. reinsert_raw_breakpoint (struct raw_breakpoint *bp)
  1212. {
  1213.   int err;

  1214.   if (bp->inserted)
  1215.     error ("Breakpoint already inserted at reinsert time.");

  1216.   err = the_target->insert_point (bp->raw_type, bp->pc, bp->size, bp);
  1217.   if (err == 0)
  1218.     bp->inserted = 1;
  1219.   else if (debug_threads)
  1220.     debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
  1221.                   paddress (bp->pc), err);
  1222. }

  1223. void
  1224. reinsert_breakpoints_at (CORE_ADDR pc)
  1225. {
  1226.   struct process_info *proc = current_process ();
  1227.   struct raw_breakpoint *bp;
  1228.   int found = 0;

  1229.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1230.     if ((bp->raw_type == raw_bkpt_type_sw
  1231.          || bp->raw_type == raw_bkpt_type_hw)
  1232.         && bp->pc == pc)
  1233.       {
  1234.         found = 1;

  1235.         reinsert_raw_breakpoint (bp);
  1236.       }

  1237.   if (!found)
  1238.     {
  1239.       /* This can happen when we remove all breakpoints while handling
  1240.          a step-over.  */
  1241.       if (debug_threads)
  1242.         debug_printf ("Could not find raw breakpoint at 0x%s "
  1243.                       "in list (reinserting).\n",
  1244.                       paddress (pc));
  1245.     }
  1246. }

  1247. void
  1248. reinsert_all_breakpoints (void)
  1249. {
  1250.   struct process_info *proc = current_process ();
  1251.   struct raw_breakpoint *bp;

  1252.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1253.     if ((bp->raw_type == raw_bkpt_type_sw
  1254.          || bp->raw_type == raw_bkpt_type_hw)
  1255.         && !bp->inserted)
  1256.       reinsert_raw_breakpoint (bp);
  1257. }

  1258. void
  1259. check_breakpoints (CORE_ADDR stop_pc)
  1260. {
  1261.   struct process_info *proc = current_process ();
  1262.   struct breakpoint *bp, **bp_link;

  1263.   bp = proc->breakpoints;
  1264.   bp_link = &proc->breakpoints;

  1265.   while (bp)
  1266.     {
  1267.       struct raw_breakpoint *raw = bp->raw;

  1268.       if ((raw->raw_type == raw_bkpt_type_sw
  1269.            || raw->raw_type == raw_bkpt_type_hw)
  1270.           && raw->pc == stop_pc)
  1271.         {
  1272.           if (!raw->inserted)
  1273.             {
  1274.               warning ("Hit a removed breakpoint?");
  1275.               return;
  1276.             }

  1277.           if (bp->handler != NULL && (*bp->handler) (stop_pc))
  1278.             {
  1279.               *bp_link = bp->next;

  1280.               release_breakpoint (proc, bp);

  1281.               bp = *bp_link;
  1282.               continue;
  1283.             }
  1284.         }

  1285.       bp_link = &bp->next;
  1286.       bp = *bp_link;
  1287.     }
  1288. }

  1289. void
  1290. set_breakpoint_data (const unsigned char *bp_data, int bp_len)
  1291. {
  1292.   breakpoint_data = bp_data;
  1293.   breakpoint_len = bp_len;
  1294. }

  1295. int
  1296. breakpoint_here (CORE_ADDR addr)
  1297. {
  1298.   struct process_info *proc = current_process ();
  1299.   struct raw_breakpoint *bp;

  1300.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1301.     if ((bp->raw_type == raw_bkpt_type_sw
  1302.          || bp->raw_type == raw_bkpt_type_hw)
  1303.         && bp->pc == addr)
  1304.       return 1;

  1305.   return 0;
  1306. }

  1307. int
  1308. breakpoint_inserted_here (CORE_ADDR addr)
  1309. {
  1310.   struct process_info *proc = current_process ();
  1311.   struct raw_breakpoint *bp;

  1312.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1313.     if ((bp->raw_type == raw_bkpt_type_sw
  1314.          || bp->raw_type == raw_bkpt_type_hw)
  1315.         && bp->pc == addr
  1316.         && bp->inserted)
  1317.       return 1;

  1318.   return 0;
  1319. }

  1320. /* See mem-break.h.  */

  1321. int
  1322. software_breakpoint_inserted_here (CORE_ADDR addr)
  1323. {
  1324.   struct process_info *proc = current_process ();
  1325.   struct raw_breakpoint *bp;

  1326.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1327.     if (bp->raw_type == raw_bkpt_type_sw
  1328.         && bp->pc == addr
  1329.         && bp->inserted)
  1330.       return 1;

  1331.   return 0;
  1332. }

  1333. /* See mem-break.h.  */

  1334. int
  1335. hardware_breakpoint_inserted_here (CORE_ADDR addr)
  1336. {
  1337.   struct process_info *proc = current_process ();
  1338.   struct raw_breakpoint *bp;

  1339.   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1340.     if (bp->raw_type == raw_bkpt_type_hw
  1341.         && bp->pc == addr
  1342.         && bp->inserted)
  1343.       return 1;

  1344.   return 0;
  1345. }

  1346. static int
  1347. validate_inserted_breakpoint (struct raw_breakpoint *bp)
  1348. {
  1349.   unsigned char *buf;
  1350.   int err;

  1351.   gdb_assert (bp->inserted);
  1352.   gdb_assert (bp->raw_type == raw_bkpt_type_sw);

  1353.   buf = alloca (breakpoint_len);
  1354.   err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len);
  1355.   if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0)
  1356.     {
  1357.       /* Tag it as gone.  */
  1358.       bp->inserted = -1;
  1359.       return 0;
  1360.     }

  1361.   return 1;
  1362. }

  1363. static void
  1364. delete_disabled_breakpoints (void)
  1365. {
  1366.   struct process_info *proc = current_process ();
  1367.   struct breakpoint *bp, *next;

  1368.   for (bp = proc->breakpoints; bp != NULL; bp = next)
  1369.     {
  1370.       next = bp->next;
  1371.       if (bp->raw->inserted < 0)
  1372.         delete_breakpoint_1 (proc, bp);
  1373.     }
  1374. }

  1375. /* Check if breakpoints we inserted still appear to be inserted.  They
  1376.    may disappear due to a shared library unload, and worse, a new
  1377.    shared library may be reloaded at the same address as the
  1378.    previously unloaded one.  If that happens, we should make sure that
  1379.    the shadow memory of the old breakpoints isn't used when reading or
  1380.    writing memory.  */

  1381. void
  1382. validate_breakpoints (void)
  1383. {
  1384.   struct process_info *proc = current_process ();
  1385.   struct breakpoint *bp;

  1386.   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  1387.     {
  1388.       struct raw_breakpoint *raw = bp->raw;

  1389.       if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
  1390.         validate_inserted_breakpoint (raw);
  1391.     }

  1392.   delete_disabled_breakpoints ();
  1393. }

  1394. void
  1395. check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
  1396. {
  1397.   struct process_info *proc = current_process ();
  1398.   struct raw_breakpoint *bp = proc->raw_breakpoints;
  1399.   struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
  1400.   CORE_ADDR mem_end = mem_addr + mem_len;
  1401.   int disabled_one = 0;

  1402.   for (; jp != NULL; jp = jp->next)
  1403.     {
  1404.       CORE_ADDR bp_end = jp->pc + jp->length;
  1405.       CORE_ADDR start, end;
  1406.       int copy_offset, copy_len, buf_offset;

  1407.       gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
  1408.                   || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);

  1409.       if (mem_addr >= bp_end)
  1410.         continue;
  1411.       if (jp->pc >= mem_end)
  1412.         continue;

  1413.       start = jp->pc;
  1414.       if (mem_addr > start)
  1415.         start = mem_addr;

  1416.       end = bp_end;
  1417.       if (end > mem_end)
  1418.         end = mem_end;

  1419.       copy_len = end - start;
  1420.       copy_offset = start - jp->pc;
  1421.       buf_offset = start - mem_addr;

  1422.       if (jp->inserted)
  1423.         memcpy (buf + buf_offset,
  1424.                 fast_tracepoint_jump_shadow (jp) + copy_offset,
  1425.                 copy_len);
  1426.     }

  1427.   for (; bp != NULL; bp = bp->next)
  1428.     {
  1429.       CORE_ADDR bp_end = bp->pc + breakpoint_len;
  1430.       CORE_ADDR start, end;
  1431.       int copy_offset, copy_len, buf_offset;

  1432.       if (bp->raw_type != raw_bkpt_type_sw)
  1433.         continue;

  1434.       gdb_assert (bp->old_data >= buf + mem_len
  1435.                   || buf >= &bp->old_data[sizeof (bp->old_data)]);

  1436.       if (mem_addr >= bp_end)
  1437.         continue;
  1438.       if (bp->pc >= mem_end)
  1439.         continue;

  1440.       start = bp->pc;
  1441.       if (mem_addr > start)
  1442.         start = mem_addr;

  1443.       end = bp_end;
  1444.       if (end > mem_end)
  1445.         end = mem_end;

  1446.       copy_len = end - start;
  1447.       copy_offset = start - bp->pc;
  1448.       buf_offset = start - mem_addr;

  1449.       if (bp->inserted > 0)
  1450.         {
  1451.           if (validate_inserted_breakpoint (bp))
  1452.             memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
  1453.           else
  1454.             disabled_one = 1;
  1455.         }
  1456.     }

  1457.   if (disabled_one)
  1458.     delete_disabled_breakpoints ();
  1459. }

  1460. void
  1461. check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
  1462.                  const unsigned char *myaddr, int mem_len)
  1463. {
  1464.   struct process_info *proc = current_process ();
  1465.   struct raw_breakpoint *bp = proc->raw_breakpoints;
  1466.   struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
  1467.   CORE_ADDR mem_end = mem_addr + mem_len;
  1468.   int disabled_one = 0;

  1469.   /* First fast tracepoint jumps, then breakpoint traps on top.  */

  1470.   for (; jp != NULL; jp = jp->next)
  1471.     {
  1472.       CORE_ADDR jp_end = jp->pc + jp->length;
  1473.       CORE_ADDR start, end;
  1474.       int copy_offset, copy_len, buf_offset;

  1475.       gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
  1476.                   || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
  1477.       gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
  1478.                   || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);

  1479.       if (mem_addr >= jp_end)
  1480.         continue;
  1481.       if (jp->pc >= mem_end)
  1482.         continue;

  1483.       start = jp->pc;
  1484.       if (mem_addr > start)
  1485.         start = mem_addr;

  1486.       end = jp_end;
  1487.       if (end > mem_end)
  1488.         end = mem_end;

  1489.       copy_len = end - start;
  1490.       copy_offset = start - jp->pc;
  1491.       buf_offset = start - mem_addr;

  1492.       memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
  1493.               myaddr + buf_offset, copy_len);
  1494.       if (jp->inserted)
  1495.         memcpy (buf + buf_offset,
  1496.                 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
  1497.     }

  1498.   for (; bp != NULL; bp = bp->next)
  1499.     {
  1500.       CORE_ADDR bp_end = bp->pc + breakpoint_len;
  1501.       CORE_ADDR start, end;
  1502.       int copy_offset, copy_len, buf_offset;

  1503.       if (bp->raw_type != raw_bkpt_type_sw)
  1504.         continue;

  1505.       gdb_assert (bp->old_data >= myaddr + mem_len
  1506.                   || myaddr >= &bp->old_data[sizeof (bp->old_data)]);

  1507.       if (mem_addr >= bp_end)
  1508.         continue;
  1509.       if (bp->pc >= mem_end)
  1510.         continue;

  1511.       start = bp->pc;
  1512.       if (mem_addr > start)
  1513.         start = mem_addr;

  1514.       end = bp_end;
  1515.       if (end > mem_end)
  1516.         end = mem_end;

  1517.       copy_len = end - start;
  1518.       copy_offset = start - bp->pc;
  1519.       buf_offset = start - mem_addr;

  1520.       memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
  1521.       if (bp->inserted > 0)
  1522.         {
  1523.           if (validate_inserted_breakpoint (bp))
  1524.             memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len);
  1525.           else
  1526.             disabled_one = 1;
  1527.         }
  1528.     }

  1529.   if (disabled_one)
  1530.     delete_disabled_breakpoints ();
  1531. }

  1532. /* Delete all breakpoints, and un-insert them from the inferior.  */

  1533. void
  1534. delete_all_breakpoints (void)
  1535. {
  1536.   struct process_info *proc = current_process ();

  1537.   while (proc->breakpoints)
  1538.     delete_breakpoint_1 (proc, proc->breakpoints);
  1539. }

  1540. /* Clear the "inserted" flag in all breakpoints.  */

  1541. void
  1542. mark_breakpoints_out (struct process_info *proc)
  1543. {
  1544.   struct raw_breakpoint *raw_bp;

  1545.   for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
  1546.     raw_bp->inserted = 0;
  1547. }

  1548. /* Release all breakpoints, but do not try to un-insert them from the
  1549.    inferior.  */

  1550. void
  1551. free_all_breakpoints (struct process_info *proc)
  1552. {
  1553.   mark_breakpoints_out (proc);

  1554.   /* Note: use PROC explicitly instead of deferring to
  1555.      delete_all_breakpoints --- CURRENT_INFERIOR may already have been
  1556.      released when we get here.  There should be no call to
  1557.      current_process from here on.  */
  1558.   while (proc->breakpoints)
  1559.     delete_breakpoint_1 (proc, proc->breakpoints);
  1560. }