gdb/nat/x86-dregs.c - gdb

Data types defined

Functions defined

Macros defined

Source code

  1. /* Debug register code for x86 (i386 and x86-64).

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

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

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

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

  14. #include "common-defs.h"
  15. #include "x86-dregs.h"
  16. #include "break-common.h"

  17. /* Support for hardware watchpoints and breakpoints using the x86
  18.    debug registers.

  19.    This provides several functions for inserting and removing
  20.    hardware-assisted breakpoints and watchpoints, testing if one or
  21.    more of the watchpoints triggered and at what address, checking
  22.    whether a given region can be watched, etc.

  23.    The functions below implement debug registers sharing by reference
  24.    counts, and allow to watch regions up to 16 bytes long.  */

  25. /* Accessor macros for low-level function vector.  */

  26. /* Can we update the inferior's debug registers?  */
  27. #define x86_dr_low_can_set_addr() (x86_dr_low.set_addr != NULL)

  28. /* Update the inferior's debug register REGNUM from STATE.  */
  29. #define x86_dr_low_set_addr(new_state, i) \
  30.   (x86_dr_low.set_addr ((i), (new_state)->dr_mirror[(i)]))

  31. /* Return the inferior's debug register REGNUM.  */
  32. #define x86_dr_low_get_addr(i) (x86_dr_low.get_addr ((i)))

  33. /* Can we update the inferior's DR7 control register?  */
  34. #define x86_dr_low_can_set_control() (x86_dr_low.set_control != NULL)

  35. /* Update the inferior's DR7 debug control register from STATE.  */
  36. #define x86_dr_low_set_control(new_state) \
  37.   (x86_dr_low.set_control ((new_state)->dr_control_mirror))

  38. /* Return the value of the inferior's DR7 debug control register.  */
  39. #define x86_dr_low_get_control() (x86_dr_low.get_control ())

  40. /* Return the value of the inferior's DR6 debug status register.  */
  41. #define x86_dr_low_get_status() (x86_dr_low.get_status ())

  42. /* Return the debug register size, in bytes.  */
  43. #define x86_get_debug_register_length() \
  44.   (x86_dr_low.debug_register_length)

  45. /* Support for 8-byte wide hw watchpoints.  */
  46. #define TARGET_HAS_DR_LEN_8 (x86_get_debug_register_length () == 8)

  47. /* DR7 Debug Control register fields.  */

  48. /* How many bits to skip in DR7 to get to R/W and LEN fields.  */
  49. #define DR_CONTROL_SHIFT        16
  50. /* How many bits in DR7 per R/W and LEN field for each watchpoint.  */
  51. #define DR_CONTROL_SIZE                4

  52. /* Watchpoint/breakpoint read/write fields in DR7.  */
  53. #define DR_RW_EXECUTE        (0x0)        /* Break on instruction execution.  */
  54. #define DR_RW_WRITE        (0x1)        /* Break on data writes.  */
  55. #define DR_RW_READ        (0x3)        /* Break on data reads or writes.  */

  56. /* This is here for completeness.  No platform supports this
  57.    functionality yet (as of March 2001).  Note that the DE flag in the
  58.    CR4 register needs to be set to support this.  */
  59. #ifndef DR_RW_IORW
  60. #define DR_RW_IORW        (0x2)        /* Break on I/O reads or writes.  */
  61. #endif

  62. /* Watchpoint/breakpoint length fields in DR7.  The 2-bit left shift
  63.    is so we could OR this with the read/write field defined above.  */
  64. #define DR_LEN_1        (0x0 << 2) /* 1-byte region watch or breakpoint.  */
  65. #define DR_LEN_2        (0x1 << 2) /* 2-byte region watch.  */
  66. #define DR_LEN_4        (0x3 << 2) /* 4-byte region watch.  */
  67. #define DR_LEN_8        (0x2 << 2) /* 8-byte region watch (AMD64).  */

  68. /* Local and Global Enable flags in DR7.

  69.    When the Local Enable flag is set, the breakpoint/watchpoint is
  70.    enabled only for the current task; the processor automatically
  71.    clears this flag on every task switch.  When the Global Enable flag
  72.    is set, the breakpoint/watchpoint is enabled for all tasks; the
  73.    processor never clears this flag.

  74.    Currently, all watchpoint are locally enabled.  If you need to
  75.    enable them globally, read the comment which pertains to this in
  76.    x86_insert_aligned_watchpoint below.  */
  77. #define DR_LOCAL_ENABLE_SHIFT        0 /* Extra shift to the local enable bit.  */
  78. #define DR_GLOBAL_ENABLE_SHIFT        1 /* Extra shift to the global enable bit.  */
  79. #define DR_ENABLE_SIZE                2 /* Two enable bits per debug register.  */

  80. /* Local and global exact breakpoint enable flags (a.k.a. slowdown
  81.    flags).  These are only required on i386, to allow detection of the
  82.    exact instruction which caused a watchpoint to break; i486 and
  83.    later processors do that automatically.  We set these flags for
  84.    backwards compatibility.  */
  85. #define DR_LOCAL_SLOWDOWN        (0x100)
  86. #define DR_GLOBAL_SLOWDOWN        (0x200)

  87. /* Fields reserved by Intel.  This includes the GD (General Detect
  88.    Enable) flag, which causes a debug exception to be generated when a
  89.    MOV instruction accesses one of the debug registers.

  90.    FIXME: My Intel manual says we should use 0xF800, not 0xFC00.  */
  91. #define DR_CONTROL_RESERVED        (0xFC00)

  92. /* Auxiliary helper macros.  */

  93. /* A value that masks all fields in DR7 that are reserved by Intel.  */
  94. #define X86_DR_CONTROL_MASK        (~DR_CONTROL_RESERVED)

  95. /* The I'th debug register is vacant if its Local and Global Enable
  96.    bits are reset in the Debug Control register.  */
  97. #define X86_DR_VACANT(state, i) \
  98.   (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)

  99. /* Locally enable the break/watchpoint in the I'th debug register.  */
  100. #define X86_DR_LOCAL_ENABLE(state, i) \
  101.   do { \
  102.     (state)->dr_control_mirror |= \
  103.       (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
  104.   } while (0)

  105. /* Globally enable the break/watchpoint in the I'th debug register.  */
  106. #define X86_DR_GLOBAL_ENABLE(state, i) \
  107.   do { \
  108.     (state)->dr_control_mirror |= \
  109.       (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
  110.   } while (0)

  111. /* Disable the break/watchpoint in the I'th debug register.  */
  112. #define X86_DR_DISABLE(state, i) \
  113.   do { \
  114.     (state)->dr_control_mirror &= \
  115.       ~(3 << (DR_ENABLE_SIZE * (i))); \
  116.   } while (0)

  117. /* Set in DR7 the RW and LEN fields for the I'th debug register.  */
  118. #define X86_DR_SET_RW_LEN(state, i, rwlen) \
  119.   do { \
  120.     (state)->dr_control_mirror &= \
  121.       ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
  122.     (state)->dr_control_mirror |= \
  123.       ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
  124.   } while (0)

  125. /* Get from DR7 the RW and LEN fields for the I'th debug register.  */
  126. #define X86_DR_GET_RW_LEN(dr7, i) \
  127.   (((dr7) \
  128.     >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)

  129. /* Did the watchpoint whose address is in the I'th register break?  */
  130. #define X86_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i)))

  131. /* Types of operations supported by x86_handle_nonaligned_watchpoint.  */
  132. typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } x86_wp_op_t;

  133. /* Print the values of the mirrored debug registers.  */

  134. static void
  135. x86_show_dr (struct x86_debug_reg_state *state,
  136.              const char *func, CORE_ADDR addr,
  137.              int len, enum target_hw_bp_type type)
  138. {
  139.   int i;

  140.   debug_printf ("%s", func);
  141.   if (addr || len)
  142.     debug_printf (" (addr=%s, len=%d, type=%s)",
  143.                   phex (addr, 8), len,
  144.                   type == hw_write ? "data-write"
  145.                   : (type == hw_read ? "data-read"
  146.                      : (type == hw_access ? "data-read/write"
  147.                         : (type == hw_execute ? "instruction-execute"
  148.                            /* FIXME: if/when I/O read/write
  149.                               watchpoints are supported, add them
  150.                               here.  */
  151.                            : "??unknown??"))));
  152.   debug_printf (":\n");
  153.   debug_printf ("\tCONTROL (DR7): %s          STATUS (DR6): %s\n",
  154.                 phex (state->dr_control_mirror, 8),
  155.                 phex (state->dr_status_mirror, 8));
  156.   ALL_DEBUG_ADDRESS_REGISTERS (i)
  157.     {
  158.       debug_printf ("\
  159. \tDR%d: addr=0x%s, ref.count=%d  DR%d: addr=0x%s, ref.count=%d\n",
  160.                     i, phex (state->dr_mirror[i],
  161.                              x86_get_debug_register_length ()),
  162.                     state->dr_ref_count[i],
  163.                     i + 1, phex (state->dr_mirror[i + 1],
  164.                                  x86_get_debug_register_length ()),
  165.                     state->dr_ref_count[i + 1]);
  166.       i++;
  167.     }
  168. }

  169. /* Return the value of a 4-bit field for DR7 suitable for watching a
  170.    region of LEN bytes for accesses of type TYPE.  LEN is assumed to
  171.    have the value of 1, 2, or 4.  */

  172. static unsigned
  173. x86_length_and_rw_bits (int len, enum target_hw_bp_type type)
  174. {
  175.   unsigned rw;

  176.   switch (type)
  177.     {
  178.       case hw_execute:
  179.         rw = DR_RW_EXECUTE;
  180.         break;
  181.       case hw_write:
  182.         rw = DR_RW_WRITE;
  183.         break;
  184.       case hw_read:
  185.         internal_error (__FILE__, __LINE__,
  186.                         _("The i386 doesn't support "
  187.                           "data-read watchpoints.\n"));
  188.       case hw_access:
  189.         rw = DR_RW_READ;
  190.         break;
  191. #if 0
  192.         /* Not yet supported.  */
  193.       case hw_io_access:
  194.         rw = DR_RW_IORW;
  195.         break;
  196. #endif
  197.       default:
  198.         internal_error (__FILE__, __LINE__, _("\
  199. Invalid hardware breakpoint type %d in x86_length_and_rw_bits.\n"),
  200.                         (int) type);
  201.     }

  202.   switch (len)
  203.     {
  204.       case 1:
  205.         return (DR_LEN_1 | rw);
  206.       case 2:
  207.         return (DR_LEN_2 | rw);
  208.       case 4:
  209.         return (DR_LEN_4 | rw);
  210.       case 8:
  211.         if (TARGET_HAS_DR_LEN_8)
  212.            return (DR_LEN_8 | rw);
  213.         /* ELSE FALL THROUGH */
  214.       default:
  215.         internal_error (__FILE__, __LINE__, _("\
  216. Invalid hardware breakpoint length %d in x86_length_and_rw_bits.\n"), len);
  217.     }
  218. }

  219. /* Insert a watchpoint at address ADDR, which is assumed to be aligned
  220.    according to the length of the region to watch.  LEN_RW_BITS is the
  221.    value of the bits from DR7 which describes the length and access
  222.    type of the region to be watched by this watchpoint.  Return 0 on
  223.    success, -1 on failure.  */

  224. static int
  225. x86_insert_aligned_watchpoint (struct x86_debug_reg_state *state,
  226.                                CORE_ADDR addr, unsigned len_rw_bits)
  227. {
  228.   int i;

  229.   if (!x86_dr_low_can_set_addr () || !x86_dr_low_can_set_control ())
  230.     return -1;

  231.   /* First, look for an occupied debug register with the same address
  232.      and the same RW and LEN definitions.  If we find one, we can
  233.      reuse it for this watchpoint as well (and save a register).  */
  234.   ALL_DEBUG_ADDRESS_REGISTERS (i)
  235.     {
  236.       if (!X86_DR_VACANT (state, i)
  237.           && state->dr_mirror[i] == addr
  238.           && X86_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
  239.         {
  240.           state->dr_ref_count[i]++;
  241.           return 0;
  242.         }
  243.     }

  244.   /* Next, look for a vacant debug register.  */
  245.   ALL_DEBUG_ADDRESS_REGISTERS (i)
  246.     {
  247.       if (X86_DR_VACANT (state, i))
  248.         break;
  249.     }

  250.   /* No more debug registers!  */
  251.   if (i >= DR_NADDR)
  252.     return -1;

  253.   /* Now set up the register I to watch our region.  */

  254.   /* Record the info in our local mirrored array.  */
  255.   state->dr_mirror[i] = addr;
  256.   state->dr_ref_count[i] = 1;
  257.   X86_DR_SET_RW_LEN (state, i, len_rw_bits);
  258.   /* Note: we only enable the watchpoint locally, i.e. in the current
  259.      task.  Currently, no x86 target allows or supports global
  260.      watchpoints; however, if any target would want that in the
  261.      future, GDB should probably provide a command to control whether
  262.      to enable watchpoints globally or locally, and the code below
  263.      should use global or local enable and slow-down flags as
  264.      appropriate.  */
  265.   X86_DR_LOCAL_ENABLE (state, i);
  266.   state->dr_control_mirror |= DR_LOCAL_SLOWDOWN;
  267.   state->dr_control_mirror &= X86_DR_CONTROL_MASK;

  268.   return 0;
  269. }

  270. /* Remove a watchpoint at address ADDR, which is assumed to be aligned
  271.    according to the length of the region to watch.  LEN_RW_BITS is the
  272.    value of the bits from DR7 which describes the length and access
  273.    type of the region watched by this watchpoint.  Return 0 on
  274.    success, -1 on failure.  */

  275. static int
  276. x86_remove_aligned_watchpoint (struct x86_debug_reg_state *state,
  277.                                CORE_ADDR addr, unsigned len_rw_bits)
  278. {
  279.   int i, retval = -1;
  280.   int all_vacant = 1;

  281.   ALL_DEBUG_ADDRESS_REGISTERS (i)
  282.     {
  283.       if (!X86_DR_VACANT (state, i)
  284.           && state->dr_mirror[i] == addr
  285.           && X86_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
  286.         {
  287.           if (--state->dr_ref_count[i] == 0) /* No longer in use?  */
  288.             {
  289.               /* Reset our mirror.  */
  290.               state->dr_mirror[i] = 0;
  291.               X86_DR_DISABLE (state, i);
  292.               /* Even though not strictly necessary, clear out all
  293.                  bits in DR_CONTROL related to this debug register.
  294.                  Debug output is clearer when we don't have stale bits
  295.                  in place.  This also allows the assertion below.  */
  296.               X86_DR_SET_RW_LEN (state, i, 0);
  297.             }
  298.           retval = 0;
  299.         }

  300.       if (!X86_DR_VACANT (state, i))
  301.         all_vacant = 0;
  302.     }

  303.   if (all_vacant)
  304.     {
  305.       /* Even though not strictly necessary, clear out all of
  306.          DR_CONTROL, so that when we have no debug registers in use,
  307.          we end up with DR_CONTROL == 0.  The Linux support relies on
  308.          this for an optimization.  Plus, it makes for clearer debug
  309.          output.  */
  310.       state->dr_control_mirror &= ~DR_LOCAL_SLOWDOWN;

  311.       gdb_assert (state->dr_control_mirror == 0);
  312.     }
  313.   return retval;
  314. }

  315. /* Insert or remove a (possibly non-aligned) watchpoint, or count the
  316.    number of debug registers required to watch a region at address
  317.    ADDR whose length is LEN for accesses of type TYPE.  Return 0 on
  318.    successful insertion or removal, a positive number when queried
  319.    about the number of registers, or -1 on failure.  If WHAT is not a
  320.    valid value, bombs through internal_error.  */

  321. static int
  322. x86_handle_nonaligned_watchpoint (struct x86_debug_reg_state *state,
  323.                                   x86_wp_op_t what, CORE_ADDR addr, int len,
  324.                                   enum target_hw_bp_type type)
  325. {
  326.   int retval = 0;
  327.   int max_wp_len = TARGET_HAS_DR_LEN_8 ? 8 : 4;

  328.   static const int size_try_array[8][8] =
  329.   {
  330.     {1, 1, 1, 1, 1, 1, 1, 1},        /* Trying size one.  */
  331.     {2, 1, 2, 1, 2, 1, 2, 1},        /* Trying size two.  */
  332.     {2, 1, 2, 1, 2, 1, 2, 1},        /* Trying size three.  */
  333.     {4, 1, 2, 1, 4, 1, 2, 1},        /* Trying size four.  */
  334.     {4, 1, 2, 1, 4, 1, 2, 1},        /* Trying size five.  */
  335.     {4, 1, 2, 1, 4, 1, 2, 1},        /* Trying size six.  */
  336.     {4, 1, 2, 1, 4, 1, 2, 1},        /* Trying size seven.  */
  337.     {8, 1, 2, 1, 4, 1, 2, 1},        /* Trying size eight.  */
  338.   };

  339.   while (len > 0)
  340.     {
  341.       int align = addr % max_wp_len;
  342.       /* Four (eight on AMD64) is the maximum length a debug register
  343.          can watch.  */
  344.       int try = (len > max_wp_len ? (max_wp_len - 1) : len - 1);
  345.       int size = size_try_array[try][align];

  346.       if (what == WP_COUNT)
  347.         {
  348.           /* size_try_array[] is defined such that each iteration
  349.              through the loop is guaranteed to produce an address and a
  350.              size that can be watched with a single debug register.
  351.              Thus, for counting the registers required to watch a
  352.              region, we simply need to increment the count on each
  353.              iteration.  */
  354.           retval++;
  355.         }
  356.       else
  357.         {
  358.           unsigned len_rw = x86_length_and_rw_bits (size, type);

  359.           if (what == WP_INSERT)
  360.             retval = x86_insert_aligned_watchpoint (state, addr, len_rw);
  361.           else if (what == WP_REMOVE)
  362.             retval = x86_remove_aligned_watchpoint (state, addr, len_rw);
  363.           else
  364.             internal_error (__FILE__, __LINE__, _("\
  365. Invalid value %d of operation in x86_handle_nonaligned_watchpoint.\n"),
  366.                             (int) what);
  367.           if (retval)
  368.             break;
  369.         }

  370.       addr += size;
  371.       len -= size;
  372.     }

  373.   return retval;
  374. }

  375. /* Update the inferior debug registers state, in STATE, with the
  376.    new debug registers state, in NEW_STATE.  */

  377. static void
  378. x86_update_inferior_debug_regs (struct x86_debug_reg_state *state,
  379.                                 struct x86_debug_reg_state *new_state)
  380. {
  381.   int i;

  382.   ALL_DEBUG_ADDRESS_REGISTERS (i)
  383.     {
  384.       if (X86_DR_VACANT (new_state, i) != X86_DR_VACANT (state, i))
  385.         x86_dr_low_set_addr (new_state, i);
  386.       else
  387.         gdb_assert (new_state->dr_mirror[i] == state->dr_mirror[i]);
  388.     }

  389.   if (new_state->dr_control_mirror != state->dr_control_mirror)
  390.     x86_dr_low_set_control (new_state);

  391.   *state = *new_state;
  392. }

  393. /* Insert a watchpoint to watch a memory region which starts at
  394.    address ADDR and whose length is LEN bytes.  Watch memory accesses
  395.    of the type TYPE.  Return 0 on success, -1 on failure.  */

  396. int
  397. x86_dr_insert_watchpoint (struct x86_debug_reg_state *state,
  398.                           enum target_hw_bp_type type,
  399.                           CORE_ADDR addr, int len)
  400. {
  401.   int retval;
  402.   /* Work on a local copy of the debug registers, and on success,
  403.      commit the change back to the inferior.  */
  404.   struct x86_debug_reg_state local_state = *state;

  405.   if (type == hw_read)
  406.     return 1; /* unsupported */

  407.   if (((len != 1 && len != 2 && len != 4)
  408.        && !(TARGET_HAS_DR_LEN_8 && len == 8))
  409.       || addr % len != 0)
  410.     {
  411.       retval = x86_handle_nonaligned_watchpoint (&local_state,
  412.                                                  WP_INSERT,
  413.                                                  addr, len, type);
  414.     }
  415.   else
  416.     {
  417.       unsigned len_rw = x86_length_and_rw_bits (len, type);

  418.       retval = x86_insert_aligned_watchpoint (&local_state,
  419.                                               addr, len_rw);
  420.     }

  421.   if (retval == 0)
  422.     x86_update_inferior_debug_regs (state, &local_state);

  423.   if (show_debug_regs)
  424.     x86_show_dr (state, "insert_watchpoint", addr, len, type);

  425.   return retval;
  426. }

  427. /* Remove a watchpoint that watched the memory region which starts at
  428.    address ADDR, whose length is LEN bytes, and for accesses of the
  429.    type TYPE.  Return 0 on success, -1 on failure.  */

  430. int
  431. x86_dr_remove_watchpoint (struct x86_debug_reg_state *state,
  432.                           enum target_hw_bp_type type,
  433.                           CORE_ADDR addr, int len)
  434. {
  435.   int retval;
  436.   /* Work on a local copy of the debug registers, and on success,
  437.      commit the change back to the inferior.  */
  438.   struct x86_debug_reg_state local_state = *state;

  439.   if (((len != 1 && len != 2 && len != 4)
  440.        && !(TARGET_HAS_DR_LEN_8 && len == 8))
  441.       || addr % len != 0)
  442.     {
  443.       retval = x86_handle_nonaligned_watchpoint (&local_state,
  444.                                                  WP_REMOVE,
  445.                                                  addr, len, type);
  446.     }
  447.   else
  448.     {
  449.       unsigned len_rw = x86_length_and_rw_bits (len, type);

  450.       retval = x86_remove_aligned_watchpoint (&local_state,
  451.                                               addr, len_rw);
  452.     }

  453.   if (retval == 0)
  454.     x86_update_inferior_debug_regs (state, &local_state);

  455.   if (show_debug_regs)
  456.     x86_show_dr (state, "remove_watchpoint", addr, len, type);

  457.   return retval;
  458. }

  459. /* Return non-zero if we can watch a memory region that starts at
  460.    address ADDR and whose length is LEN bytes.  */

  461. int
  462. x86_dr_region_ok_for_watchpoint (struct x86_debug_reg_state *state,
  463.                                  CORE_ADDR addr, int len)
  464. {
  465.   int nregs;

  466.   /* Compute how many aligned watchpoints we would need to cover this
  467.      region.  */
  468.   nregs = x86_handle_nonaligned_watchpoint (state, WP_COUNT,
  469.                                              addr, len, hw_write);
  470.   return nregs <= DR_NADDR ? 1 : 0;
  471. }

  472. /* If the inferior has some break/watchpoint that triggered, set the
  473.    address associated with that break/watchpoint and return non-zero.
  474.    Otherwise, return zero.  */

  475. int
  476. x86_dr_stopped_data_address (struct x86_debug_reg_state *state,
  477.                              CORE_ADDR *addr_p)
  478. {
  479.   CORE_ADDR addr = 0;
  480.   int i;
  481.   int rc = 0;
  482.   /* The current thread's DR_STATUS.  We always need to read this to
  483.      check whether some watchpoint caused the trap.  */
  484.   unsigned status;
  485.   /* We need DR_CONTROL as well, but only iff DR_STATUS indicates a
  486.      data breakpoint trap.  Only fetch it when necessary, to avoid an
  487.      unnecessary extra syscall when no watchpoint triggered.  */
  488.   int control_p = 0;
  489.   unsigned control = 0;

  490.   /* In non-stop/async, threads can be running while we change the
  491.      global dr_mirror (and friends).  Say, we set a watchpoint, and
  492.      let threads resume.  Now, say you delete the watchpoint, or
  493.      add/remove watchpoints such that dr_mirror changes while threads
  494.      are running.  On targets that support non-stop,
  495.      inserting/deleting watchpoints updates the global dr_mirror only.
  496.      It does not update the real thread's debug registers; that's only
  497.      done prior to resume.  Instead, if threads are running when the
  498.      mirror changes, a temporary and transparent stop on all threads
  499.      is forced so they can get their copy of the debug registers
  500.      updated on re-resume.  Now, say, a thread hit a watchpoint before
  501.      having been updated with the new dr_mirror contents, and we
  502.      haven't yet handled the corresponding SIGTRAP.  If we trusted
  503.      dr_mirror below, we'd mistake the real trapped address (from the
  504.      last time we had updated debug registers in the thread) with
  505.      whatever was currently in dr_mirror.  So to fix this, dr_mirror
  506.      always represents intention, what we _want_ threads to have in
  507.      debug registers.  To get at the address and cause of the trap, we
  508.      need to read the state the thread still has in its debug
  509.      registers.

  510.      In sum, always get the current debug register values the current
  511.      thread has, instead of trusting the global mirror.  If the thread
  512.      was running when we last changed watchpoints, the mirror no
  513.      longer represents what was set in this thread's debug
  514.      registers.  */
  515.   status = x86_dr_low_get_status ();

  516.   ALL_DEBUG_ADDRESS_REGISTERS (i)
  517.     {
  518.       if (!X86_DR_WATCH_HIT (status, i))
  519.         continue;

  520.       if (!control_p)
  521.         {
  522.           control = x86_dr_low_get_control ();
  523.           control_p = 1;
  524.         }

  525.       /* This second condition makes sure DRi is set up for a data
  526.          watchpoint, not a hardware breakpoint.  The reason is that
  527.          GDB doesn't call the target_stopped_data_address method
  528.          except for data watchpoints.  In other words, I'm being
  529.          paranoiac.  */
  530.       if (X86_DR_GET_RW_LEN (control, i) != 0)
  531.         {
  532.           addr = x86_dr_low_get_addr (i);
  533.           rc = 1;
  534.           if (show_debug_regs)
  535.             x86_show_dr (state, "watchpoint_hit", addr, -1, hw_write);
  536.         }
  537.     }

  538.   if (show_debug_regs && addr == 0)
  539.     x86_show_dr (state, "stopped_data_addr", 0, 0, hw_write);

  540.   if (rc)
  541.     *addr_p = addr;
  542.   return rc;
  543. }

  544. /* Return non-zero if the inferior has some watchpoint that triggered.
  545.    Otherwise return zero.  */

  546. int
  547. x86_dr_stopped_by_watchpoint (struct x86_debug_reg_state *state)
  548. {
  549.   CORE_ADDR addr = 0;
  550.   return x86_dr_stopped_data_address (state, &addr);
  551. }