/* Native-dependent code for the i386.
- Copyright (C) 2001, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
+ Copyright (C) 2001, 2004, 2005, 2007, 2008, 2009, 2010
+ Free Software Foundation, Inc.
This file is part of GDB.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#include "i386-nat.h"
#include "defs.h"
#include "breakpoint.h"
#include "command.h"
#include "gdbcmd.h"
#include "target.h"
+#include "gdb_assert.h"
/* Support for hardware watchpoints and breakpoints using the i386
debug registers.
more of the watchpoints triggered and at what address, checking
whether a given region can be watched, etc.
- A target which wants to use these functions should define several
- macros, such as `target_insert_watchpoint' and
- `target_stopped_data_address', listed in target.h, to call the
- appropriate functions below. It should also define
- I386_USE_GENERIC_WATCHPOINTS in its tm.h file.
-
- In addition, each target should provide several low-level macros
- that will be called to insert watchpoints and hardware breakpoints
- into the inferior, remove them, and check their status. These
- macros are:
-
- I386_DR_LOW_SET_CONTROL -- set the debug control (DR7)
- register to a given value
-
- I386_DR_LOW_SET_ADDR -- put an address into one debug
- register
-
- I386_DR_LOW_RESET_ADDR -- reset the address stored in
- one debug register
-
- I386_DR_LOW_GET_STATUS -- return the value of the debug
- status (DR6) register.
-
The functions below implement debug registers sharing by reference
counts, and allow to watch regions up to 16 bytes long. */
-#ifdef I386_USE_GENERIC_WATCHPOINTS
+struct i386_dr_low_type i386_dr_low;
+
/* Support for 8-byte wide hw watchpoints. */
-#ifndef TARGET_HAS_DR_LEN_8
-#define TARGET_HAS_DR_LEN_8 0
-#endif
+#define TARGET_HAS_DR_LEN_8 (i386_dr_low.debug_register_length == 8)
/* Debug registers' indices. */
#define DR_NADDR 4 /* The number of debug address registers. */
#define I386_DR_GET_RW_LEN(i) \
((dr_control_mirror >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)
+/* Mask that this I'th watchpoint has triggered. */
+#define I386_DR_WATCH_MASK(i) (1 << (i))
+
/* Did the watchpoint whose address is in the I'th register break? */
-#define I386_DR_WATCH_HIT(i) (dr_status_mirror & (1 << (i)))
+#define I386_DR_WATCH_HIT(i) (dr_status_mirror & I386_DR_WATCH_MASK (i))
/* A macro to loop over all debug registers. */
#define ALL_DEBUG_REGISTERS(i) for (i = 0; i < DR_NADDR; i++)
/* Mirror the inferior's DRi registers. We keep the status and
control registers separated because they don't hold addresses. */
static CORE_ADDR dr_mirror[DR_NADDR];
-static unsigned dr_status_mirror, dr_control_mirror;
+static unsigned long dr_status_mirror, dr_control_mirror;
/* Reference counts for each debug register. */
static int dr_ref_count[DR_NADDR];
dr_status_mirror = 0;
}
-/* Reset all debug registers at each new startup to avoid missing
- watchpoints after restart. */
-
-void
-child_post_startup_inferior (ptid_t ptid)
-{
- i386_cleanup_dregs ();
-}
-
/* Print the values of the mirrored debug registers. This is called
when maint_show_dr is non-zero. To set that up, type "maint
show-debug-regs" at GDB's prompt. */
i386_show_dr (const char *func, CORE_ADDR addr,
int len, enum target_hw_bp_type type)
{
+ int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
int i;
puts_unfiltered (func);
here. */
: "??unknown??"))));
puts_unfiltered (":\n");
- printf_unfiltered ("\tCONTROL (DR7): %08x STATUS (DR6): %08x\n",
- dr_control_mirror, dr_status_mirror);
+ printf_unfiltered ("\tCONTROL (DR7): %s STATUS (DR6): %s\n",
+ phex (dr_control_mirror, 8), phex (dr_status_mirror, 8));
ALL_DEBUG_REGISTERS(i)
{
printf_unfiltered ("\
\tDR%d: addr=0x%s, ref.count=%d DR%d: addr=0x%s, ref.count=%d\n",
- i, paddr(dr_mirror[i]), dr_ref_count[i],
- i+1, paddr(dr_mirror[i+1]), dr_ref_count[i+1]);
+ i, phex (dr_mirror[i], addr_size), dr_ref_count[i],
+ i+1, phex (dr_mirror[i+1], addr_size), dr_ref_count[i+1]);
i++;
}
}
rw = DR_RW_WRITE;
break;
case hw_read:
- /* The i386 doesn't support data-read watchpoints. */
+ internal_error (__FILE__, __LINE__,
+ _("The i386 doesn't support data-read watchpoints.\n"));
case hw_access:
rw = DR_RW_READ;
break;
{
int i;
+ if (!i386_dr_low.set_addr || !i386_dr_low.set_control)
+ return -1;
+
/* First, look for an occupied debug register with the same address
and the same RW and LEN definitions. If we find one, we can
reuse it for this watchpoint as well (and save a register). */
dr_control_mirror &= I386_DR_CONTROL_MASK;
/* Finally, actually pass the info to the inferior. */
- I386_DR_LOW_SET_ADDR (i, addr);
- I386_DR_LOW_SET_CONTROL (dr_control_mirror);
+ i386_dr_low.set_addr (i, addr);
+ i386_dr_low.set_control (dr_control_mirror);
+
+ /* Only a sanity check for leftover bits (set possibly only by inferior). */
+ if (i386_dr_low.unset_status)
+ i386_dr_low.unset_status (I386_DR_WATCH_MASK (i));
return 0;
}
dr_mirror[i] = 0;
I386_DR_DISABLE (i);
/* Reset it in the inferior. */
- I386_DR_LOW_SET_CONTROL (dr_control_mirror);
- I386_DR_LOW_RESET_ADDR (i);
+ i386_dr_low.set_control (dr_control_mirror);
+ if (i386_dr_low.reset_addr)
+ i386_dr_low.reset_addr (i);
}
retval = 0;
}
address ADDR and whose length is LEN bytes. Watch memory accesses
of the type TYPE. Return 0 on success, -1 on failure. */
-int
+static int
i386_insert_watchpoint (CORE_ADDR addr, int len, int type)
{
int retval;
+ if (type == hw_read)
+ return 1; /* unsupported */
+
if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8))
|| addr % len != 0)
retval = i386_handle_nonaligned_watchpoint (WP_INSERT, addr, len, type);
/* Remove a watchpoint that watched the memory region which starts at
address ADDR, whose length is LEN bytes, and for accesses of the
type TYPE. Return 0 on success, -1 on failure. */
-int
+static int
i386_remove_watchpoint (CORE_ADDR addr, int len, int type)
{
int retval;
/* Return non-zero if we can watch a memory region that starts at
address ADDR and whose length is LEN bytes. */
-int
+static int
i386_region_ok_for_watchpoint (CORE_ADDR addr, int len)
{
int nregs;
address associated with that watchpoint and return non-zero.
Otherwise, return zero. */
-int
+static int
i386_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
{
CORE_ADDR addr = 0;
int i;
int rc = 0;
- dr_status_mirror = I386_DR_LOW_GET_STATUS ();
+ dr_status_mirror = i386_dr_low.get_status ();
ALL_DEBUG_REGISTERS(i)
{
that GDB doesn't call the target_stopped_data_address
method except for data watchpoints. In other words, I'm
being paranoiac. */
- && I386_DR_GET_RW_LEN (i) != 0)
+ && I386_DR_GET_RW_LEN (i) != 0
+ /* This third condition makes sure DRi is not vacant, this
+ avoids false positives in windows-nat.c. */
+ && !I386_DR_VACANT (i))
{
addr = dr_mirror[i];
rc = 1;
return rc;
}
-int
+static int
i386_stopped_by_watchpoint (void)
{
CORE_ADDR addr = 0;
return i386_stopped_data_address (¤t_target, &addr);
}
-/* Return non-zero if the inferior has some break/watchpoint that
- triggered. */
-
-int
-i386_stopped_by_hwbp (void)
-{
- int i;
-
- dr_status_mirror = I386_DR_LOW_GET_STATUS ();
- if (maint_show_dr)
- i386_show_dr ("stopped_by_hwbp", 0, 0, hw_execute);
-
- ALL_DEBUG_REGISTERS(i)
- {
- if (I386_DR_WATCH_HIT (i))
- return 1;
- }
-
- return 0;
-}
-
/* Insert a hardware-assisted breakpoint at BP_TGT->placed_address.
Return 0 on success, EBUSY on failure. */
-int
-i386_insert_hw_breakpoint (struct bp_target_info *bp_tgt)
+static int
+i386_insert_hw_breakpoint (struct gdbarch *gdbarch,
+ struct bp_target_info *bp_tgt)
{
unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
CORE_ADDR addr = bp_tgt->placed_address;
/* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
Return 0 on success, -1 on failure. */
-int
-i386_remove_hw_breakpoint (struct bp_target_info *bp_tgt)
+static int
+i386_remove_hw_breakpoint (struct gdbarch *gdbarch,
+ struct bp_target_info *bp_tgt)
{
unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
CORE_ADDR addr = bp_tgt->placed_address;
return 1;
}
+static void
+add_show_debug_regs_command (void)
+{
+ /* A maintenance command to enable printing the internal DRi mirror
+ variables. */
+ add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
+ &maint_show_dr, _("\
+Set whether to show variables that mirror the x86 debug registers."), _("\
+Show whether to show variables that mirror the x86 debug registers."), _("\
+Use \"on\" to enable, \"off\" to disable.\n\
+If enabled, the debug registers values are shown when GDB inserts\n\
+or removes a hardware breakpoint or watchpoint, and when the inferior\n\
+triggers a breakpoint or watchpoint."),
+ NULL,
+ NULL,
+ &maintenance_set_cmdlist,
+ &maintenance_show_cmdlist);
+}
+
+/* There are only two global functions left. */
+
void
i386_use_watchpoints (struct target_ops *t)
{
t->to_remove_hw_breakpoint = i386_remove_hw_breakpoint;
}
-#endif /* I386_USE_GENERIC_WATCHPOINTS */
-\f
-
-/* Provide a prototype to silence -Wmissing-prototypes. */
-void _initialize_i386_nat (void);
-
void
-_initialize_i386_nat (void)
+i386_set_debug_register_length (int len)
{
-#ifdef I386_USE_GENERIC_WATCHPOINTS
- /* A maintenance command to enable printing the internal DRi mirror
- variables. */
- deprecated_add_set_cmd ("show-debug-regs", class_maintenance,
- var_boolean, (char *) &maint_show_dr, _("\
-Set whether to show variables that mirror the x86 debug registers.\n\
-Use \"on\" to enable, \"off\" to disable.\n\
-If enabled, the debug registers values are shown when GDB inserts\n\
-or removes a hardware breakpoint or watchpoint, and when the inferior\n\
-triggers a breakpoint or watchpoint."),
- &maintenancelist);
-#endif
+ /* This function should be called only once for each native target. */
+ gdb_assert (i386_dr_low.debug_register_length == 0);
+ gdb_assert (len == 4 || len == 8);
+ i386_dr_low.debug_register_length = len;
+ add_show_debug_regs_command ();
}