Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [patch] Move common macros to i386-common.h
@ 2011-02-13  9:58 Yao Qi
  2011-02-13 13:40 ` Mark Kettenis
  2011-03-11  6:39 ` [try 2nd, patch] Move common macros to i386-dbg-reg.h Yao Qi
  0 siblings, 2 replies; 19+ messages in thread
From: Yao Qi @ 2011-02-13  9:58 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 229 bytes --]

Some macros are duplicated in gdb/i386-nat and gdb/gdbserver/i386-low. 
This patch is to move common macros to gdb/common/i386-common.h.

There are also some duplicated functions, which should be moved in next 
step.

-- 
Yao Qi

[-- Attachment #2: i386-common-h-0213.patch --]
[-- Type: text/x-patch, Size: 37237 bytes --]

gdb/

	* i386-nat.h: Include i386-common.h.
	Move macros to i386-common.h.
	* i386-nat.c: Move macros and structs to i386-common.h.
	New variable struct i386_debug_reg_state state to replace
	other variables dr_mirror, dr_ref_count, dr_control_mirror,
	and dr_status_mirror.
	(i386_insert_aligned_watchpoint): Add one formal parameter
	struct i386_debug_reg_state *STATE.
	Update code using these variables.	
	(i386_remove_aligned_watchpoint, i386_show_dr): Likewise.
	(i386_cleanup_dregs): Likewise.
	(i386_handle_nonaligned_watchpoint): Likewise.

gdb/common/

	* i386-common.h: New.  Common macros and structs.

gdb/gdbserver/

	* i386-low.h: Include i386-common.h.
	Move macros to i386-common.h.
	* i386-low.c (i386_set_debug_register_length): New.
	(i386_low_init_dregs): Call i386_set_debug_register_length.

diff --git a/gdb/common/i386-common.h b/gdb/common/i386-common.h
new file mode 100644
index 0000000..5d87159
--- /dev/null
+++ b/gdb/common/i386-common.h
@@ -0,0 +1,230 @@
+/* Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef I386_COMMON_H
+#define I386_COMMON_H 1
+
+/* DR7 Debug Control register fields.  */
+
+/* How many bits to skip in DR7 to get to R/W and LEN fields.  */
+#define DR_CONTROL_SHIFT	16
+/* How many bits in DR7 per R/W and LEN field for each watchpoint.  */
+#define DR_CONTROL_SIZE		4
+
+/* Watchpoint/breakpoint read/write fields in DR7.  */
+#define DR_RW_EXECUTE	(0x0)	/* Break on instruction execution.  */
+#define DR_RW_WRITE	(0x1)	/* Break on data writes.  */
+#define DR_RW_READ	(0x3)	/* Break on data reads or writes.  */
+/* This is here for completeness.  No platform supports this
+   functionality yet (as of March 2001).  Note that the DE flag in the
+   CR4 register needs to be set to support this.  */
+#ifndef DR_RW_IORW
+#define DR_RW_IORW	(0x2)	/* Break on I/O reads or writes.  */
+#endif
+
+/* Debug registers' indices.  */
+#define DR_NADDR	4	/* The number of debug address registers.  */
+#define DR_STATUS	6	/* Index of debug status register (DR6).  */
+#define DR_CONTROL	7	/* Index of debug control register (DR7). */
+
+/* Watchpoint/breakpoint length fields in DR7.  The 2-bit left shift
+   is so we could OR this with the read/write field defined above.  */
+#define DR_LEN_1	(0x0 << 2) /* 1-byte region watch or breakpoint.  */
+#define DR_LEN_2	(0x1 << 2) /* 2-byte region watch.  */
+#define DR_LEN_4	(0x3 << 2) /* 4-byte region watch.  */
+#define DR_LEN_8	(0x2 << 2) /* 8-byte region watch (AMD64).  */
+
+/* Local and Global Enable flags in DR7.
+
+   When the Local Enable flag is set, the breakpoint/watchpoint is
+   enabled only for the current task; the processor automatically
+   clears this flag on every task switch.  When the Global Enable flag
+   is set, the breakpoint/watchpoint is enabled for all tasks; the
+   processor never clears this flag.
+
+   Currently, all watchpoint are locally enabled.  If you need to
+   enable them globally, read the comment which pertains to this in
+   i386_insert_aligned_watchpoint below.  */
+#define DR_LOCAL_ENABLE_SHIFT	0 /* Extra shift to the local enable bit.  */
+#define DR_GLOBAL_ENABLE_SHIFT	1 /* Extra shift to the global enable bit.  */
+#define DR_ENABLE_SIZE		2 /* Two enable bits per debug register.  */
+
+/* Local and global exact breakpoint enable flags (a.k.a. slowdown
+   flags).  These are only required on i386, to allow detection of the
+   exact instruction which caused a watchpoint to break; i486 and
+   later processors do that automatically.  We set these flags for
+   backwards compatibility.  */
+#define DR_LOCAL_SLOWDOWN	(0x100)
+#define DR_GLOBAL_SLOWDOWN	(0x200)
+
+/* Fields reserved by Intel.  This includes the GD (General Detect
+   Enable) flag, which causes a debug exception to be generated when a
+   MOV instruction accesses one of the debug registers.
+
+   FIXME: My Intel manual says we should use 0xF800, not 0xFC00.  */
+#define DR_CONTROL_RESERVED	(0xFC00)
+
+/* Auxiliary helper macros.  */
+
+/* A value that masks all fields in DR7 that are reserved by Intel.  */
+#define I386_DR_CONTROL_MASK	(~DR_CONTROL_RESERVED)
+
+#ifndef DR_FIRSTADDR
+#define DR_FIRSTADDR 0
+#endif
+
+#ifndef DR_LASTADDR
+#define DR_LASTADDR 3
+#endif
+
+/* Types of operations supported by i386_handle_nonaligned_watchpoint.  */
+typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t;
+
+/* Return the value of a 4-bit field for DR7 suitable for watching a
+   region of LEN bytes for accesses of type TYPE.  LEN is assumed to
+   have the value of 1, 2, or 4.  */
+/*
+static unsigned i386_length_and_rw_bits (int len, enum target_hw_bp_type type);
+*/
+
+/* Global state needed to track h/w watchpoints.  */
+
+struct i386_debug_reg_state
+{
+  /* Mirror the inferior's DRi registers.  We keep the status and
+     control registers separated because they don't hold addresses.
+     Note that since we can change these mirrors while threads are
+     running, we never trust them to explain a cause of a trap.
+     For that, we need to peek directly in the inferior registers.  */
+  CORE_ADDR dr_mirror[DR_NADDR];
+  unsigned dr_status_mirror, dr_control_mirror;
+
+  /* Reference counts for each debug register.  */
+  int dr_ref_count[DR_NADDR];
+};
+
+/* Support for hardware watchpoints and breakpoints using the i386
+   debug registers.
+
+   This provides several functions for inserting and removing
+   hardware-assisted breakpoints and watchpoints, testing if one or
+   more of the watchpoints triggered and at what address, checking
+   whether a given region can be watched, etc.
+
+   The functions below implement debug registers sharing by reference
+   counts, and allow to watch regions up to 16 bytes long.  */
+
+/* Support for hardware watchpoints and breakpoints using the i386
+   debug registers.
+
+   This provides several functions for inserting and removing
+   hardware-assisted breakpoints and watchpoints, testing if one or
+   more of the watchpoints triggered and at what address, checking
+   whether a given region can be watched, etc.
+
+   In addition, each target should provide several low-level functions
+   regrouped into i386_dr_low_type struct below. These functions
+   that will be called to insert watchpoints and hardware breakpoints
+   into the inferior, remove them, and check their status.  These
+   functions are:
+
+      set_control              -- set the debug control (DR7)
+				  register to a given value for all LWPs
+
+      set_addr                 -- put an address into one debug
+				  register for all LWPs
+
+      reset_addr               -- reset the address stored in
+				  one debug register for all LWPs
+
+      get_status               -- return the value of the debug
+				  status (DR6) register for current LWP
+
+      unset_status             -- unset the specified bits of the debug
+				  status (DR6) register for all LWPs
+
+   Additionally, the native file should set the debug_register_length
+   field to 4 or 8 depending on the number of bytes used for
+   deubg registers.  */
+
+struct i386_dr_low_type
+  {
+    void (*set_control) (unsigned long);
+    void (*set_addr) (int, CORE_ADDR);
+    void (*reset_addr) (int);
+    unsigned long (*get_status) (void);
+    void (*unset_status) (unsigned long);
+    int debug_register_length;
+  };
+
+extern struct i386_dr_low_type i386_dr_low;
+
+/* Support for 8-byte wide hw watchpoints.  */
+#define TARGET_HAS_DR_LEN_8 (i386_dr_low.debug_register_length == 8)
+
+/* Auxiliary helper macros.  */
+
+/* A value that masks all fields in DR7 that are reserved by Intel.  */
+#define I386_DR_CONTROL_MASK	(~DR_CONTROL_RESERVED)
+
+/* The I'th debug register is vacant if its Local and Global Enable
+   bits are reset in the Debug Control register.  */
+#define I386_DR_VACANT(state, i) \
+  (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
+
+/* Locally enable the break/watchpoint in the I'th debug register.  */
+#define I386_DR_LOCAL_ENABLE(state, i) \
+  do { \
+    (state)->dr_control_mirror |= \
+      (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
+  } while (0)
+
+/* Globally enable the break/watchpoint in the I'th debug register.  */
+#define I386_DR_GLOBAL_ENABLE(state, i) \
+  do { \
+    (state)->dr_control_mirror |= \
+      (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
+  } while (0)
+
+/* Disable the break/watchpoint in the I'th debug register.  */
+#define I386_DR_DISABLE(state, i) \
+  do { \
+    (state)->dr_control_mirror &= \
+      ~(3 << (DR_ENABLE_SIZE * (i))); \
+  } while (0)
+
+/* Set in DR7 the RW and LEN fields for the I'th debug register.  */
+#define I386_DR_SET_RW_LEN(state, i,rwlen) \
+  do { \
+    (state)->dr_control_mirror &= \
+      ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
+    (state)->dr_control_mirror |= \
+      ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
+  } while (0)
+
+/* Get from DR7 the RW and LEN fields for the I'th debug register.  */
+#define I386_DR_GET_RW_LEN(dr7, i) \
+  (((dr7) \
+    >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)
+
+/* Did the watchpoint whose address is in the I'th register break?  */
+#define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i)))
+
+/* A macro to loop over all debug registers.  */
+#define ALL_DEBUG_REGISTERS(i)	for (i = 0; i < DR_NADDR; i++)
+
+#endif
diff --git a/gdb/gdbserver/i386-low.c b/gdb/gdbserver/i386-low.c
index 47268e7..a438ff5 100644
--- a/gdb/gdbserver/i386-low.c
+++ b/gdb/gdbserver/i386-low.c
@@ -21,11 +21,7 @@
 #include "target.h"
 #include "i386-low.h"
 
-/* Support for 8-byte wide hw watchpoints.  */
-#ifndef TARGET_HAS_DR_LEN_8
-/* NOTE: sizeof (long) == 4 on win64.  */
-#define TARGET_HAS_DR_LEN_8 (sizeof (void *) == 8)
-#endif
+struct i386_dr_low_type i386_dr_low;
 
 enum target_hw_bp_type
   {
@@ -35,120 +31,18 @@ enum target_hw_bp_type
     hw_execute = 3	/* Execute HW breakpoint */
   };
 
-/* DR7 Debug Control register fields.  */
-
-/* How many bits to skip in DR7 to get to R/W and LEN fields.  */
-#define DR_CONTROL_SHIFT	16
-/* How many bits in DR7 per R/W and LEN field for each watchpoint.  */
-#define DR_CONTROL_SIZE		4
-
-/* Watchpoint/breakpoint read/write fields in DR7.  */
-#define DR_RW_EXECUTE	(0x0)	/* Break on instruction execution.  */
-#define DR_RW_WRITE	(0x1)	/* Break on data writes.  */
-#define DR_RW_READ	(0x3)	/* Break on data reads or writes.  */
-
-/* This is here for completeness.  No platform supports this
-   functionality yet (as of March 2001).  Note that the DE flag in the
-   CR4 register needs to be set to support this.  */
-#ifndef DR_RW_IORW
-#define DR_RW_IORW	(0x2)	/* Break on I/O reads or writes.  */
-#endif
+static void
+i386_set_debug_register_length (int len)
+{
+  gdb_assert (i386_dr_low.debug_register_length == 0);
+  gdb_assert (len == 4 || len == 8);
+  i386_dr_low.debug_register_length = len;
+}
 
-/* Watchpoint/breakpoint length fields in DR7.  The 2-bit left shift
-   is so we could OR this with the read/write field defined above.  */
-#define DR_LEN_1	(0x0 << 2) /* 1-byte region watch or breakpoint.  */
-#define DR_LEN_2	(0x1 << 2) /* 2-byte region watch.  */
-#define DR_LEN_4	(0x3 << 2) /* 4-byte region watch.  */
-#define DR_LEN_8	(0x2 << 2) /* 8-byte region watch (AMD64).  */
-
-/* Local and Global Enable flags in DR7.
-
-   When the Local Enable flag is set, the breakpoint/watchpoint is
-   enabled only for the current task; the processor automatically
-   clears this flag on every task switch.  When the Global Enable flag
-   is set, the breakpoint/watchpoint is enabled for all tasks; the
-   processor never clears this flag.
-
-   Currently, all watchpoint are locally enabled.  If you need to
-   enable them globally, read the comment which pertains to this in
-   i386_insert_aligned_watchpoint below.  */
-#define DR_LOCAL_ENABLE_SHIFT	0 /* Extra shift to the local enable bit.  */
-#define DR_GLOBAL_ENABLE_SHIFT	1 /* Extra shift to the global enable bit.  */
-#define DR_ENABLE_SIZE		2 /* Two enable bits per debug register.  */
-
-/* Local and global exact breakpoint enable flags (a.k.a. slowdown
-   flags).  These are only required on i386, to allow detection of the
-   exact instruction which caused a watchpoint to break; i486 and
-   later processors do that automatically.  We set these flags for
-   backwards compatibility.  */
-#define DR_LOCAL_SLOWDOWN	(0x100)
-#define DR_GLOBAL_SLOWDOWN	(0x200)
-
-/* Fields reserved by Intel.  This includes the GD (General Detect
-   Enable) flag, which causes a debug exception to be generated when a
-   MOV instruction accesses one of the debug registers.
-
-   FIXME: My Intel manual says we should use 0xF800, not 0xFC00.  */
-#define DR_CONTROL_RESERVED	(0xFC00)
-
-/* Auxiliary helper macros.  */
-
-/* A value that masks all fields in DR7 that are reserved by Intel.  */
-#define I386_DR_CONTROL_MASK	(~DR_CONTROL_RESERVED)
-
-/* The I'th debug register is vacant if its Local and Global Enable
-   bits are reset in the Debug Control register.  */
-#define I386_DR_VACANT(state, i) \
-  (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
-
-/* Locally enable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_LOCAL_ENABLE(state, i) \
-  do { \
-    (state)->dr_control_mirror |= \
-      (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
-  } while (0)
-
-/* Globally enable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_GLOBAL_ENABLE(state, i) \
-  do { \
-    (state)->dr_control_mirror |= \
-      (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
-  } while (0)
-
-/* Disable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_DISABLE(state, i) \
-  do { \
-    (state)->dr_control_mirror &= \
-      ~(3 << (DR_ENABLE_SIZE * (i))); \
-  } while (0)
-
-/* Set in DR7 the RW and LEN fields for the I'th debug register.  */
-#define I386_DR_SET_RW_LEN(state, i,rwlen) \
-  do { \
-    (state)->dr_control_mirror &= \
-      ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
-    (state)->dr_control_mirror |= \
-      ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
-  } while (0)
-
-/* Get from DR7 the RW and LEN fields for the I'th debug register.  */
-#define I386_DR_GET_RW_LEN(dr7, i) \
-  (((dr7) \
-    >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)
-
-/* Did the watchpoint whose address is in the I'th register break?  */
-#define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i)))
-
-/* A macro to loop over all debug registers.  */
-#define ALL_DEBUG_REGISTERS(i)	for (i = 0; i < DR_NADDR; i++)
-
-/* Types of operations supported by i386_handle_nonaligned_watchpoint.  */
-typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t;
-\f
 /* Implementation.  */
 
 /* Clear the reference counts and forget everything we knew about the
-   debug registers.  */
+   debug registers.  Also set the length of debug register.  */
 
 void
 i386_low_init_dregs (struct i386_debug_reg_state *state)
@@ -162,6 +56,8 @@ i386_low_init_dregs (struct i386_debug_reg_state *state)
     }
   state->dr_control_mirror = 0;
   state->dr_status_mirror  = 0;
+
+  i386_set_debug_register_length (sizeof (void *));
 }
 
 /* Print the values of the mirrored debug registers.  This is enabled via
diff --git a/gdb/gdbserver/i386-low.h b/gdb/gdbserver/i386-low.h
index bbfbc10..d7c0ae5 100644
--- a/gdb/gdbserver/i386-low.h
+++ b/gdb/gdbserver/i386-low.h
@@ -29,29 +29,12 @@
    counts, and allow to watch regions up to 16 bytes long
    (32 bytes on 64 bit hosts).  */
 
+#include "i386-common.h"
 
 /* Debug registers' indices.  */
 #define DR_FIRSTADDR 0
 #define DR_LASTADDR  3
-#define DR_NADDR     4 /* The number of debug address registers.  */
-#define DR_STATUS    6
-#define DR_CONTROL   7
-
-/* Global state needed to track h/w watchpoints.  */
-
-struct i386_debug_reg_state
-{
-  /* Mirror the inferior's DRi registers.  We keep the status and
-     control registers separated because they don't hold addresses.
-     Note that since we can change these mirrors while threads are
-     running, we never trust them to explain a cause of a trap.
-     For that, we need to peek directly in the inferior registers.  */
-  CORE_ADDR dr_mirror[DR_NADDR];
-  unsigned dr_status_mirror, dr_control_mirror;
-
-  /* Reference counts for each debug register.  */
-  int dr_ref_count[DR_NADDR];
-};
+
 
 /* Initialize STATE.  */
 extern void i386_low_init_dregs (struct i386_debug_reg_state *state);
diff --git a/gdb/i386-nat.c b/gdb/i386-nat.c
index 0606ce1..df3ae5f 100644
--- a/gdb/i386-nat.c
+++ b/gdb/i386-nat.c
@@ -39,127 +39,13 @@
 
 struct i386_dr_low_type i386_dr_low;
 
-
-/* Support for 8-byte wide hw watchpoints.  */
-#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 DR_STATUS	6	/* Index of debug status register (DR6).  */
-#define DR_CONTROL	7	/* Index of debug control register (DR7).  */
-
-/* DR7 Debug Control register fields.  */
-
-/* How many bits to skip in DR7 to get to R/W and LEN fields.  */
-#define DR_CONTROL_SHIFT	16
-/* How many bits in DR7 per R/W and LEN field for each watchpoint.  */
-#define DR_CONTROL_SIZE		4
-
-/* Watchpoint/breakpoint read/write fields in DR7.  */
-#define DR_RW_EXECUTE	(0x0)	/* Break on instruction execution.  */
-#define DR_RW_WRITE	(0x1)	/* Break on data writes.  */
-#define DR_RW_READ	(0x3)	/* Break on data reads or writes.  */
-
-/* This is here for completeness.  No platform supports this
-   functionality yet (as of March 2001).  Note that the DE flag in the
-   CR4 register needs to be set to support this.  */
-#ifndef DR_RW_IORW
-#define DR_RW_IORW	(0x2)	/* Break on I/O reads or writes.  */
-#endif
-
-/* Watchpoint/breakpoint length fields in DR7.  The 2-bit left shift
-   is so we could OR this with the read/write field defined above.  */
-#define DR_LEN_1	(0x0 << 2) /* 1-byte region watch or breakpoint.  */
-#define DR_LEN_2	(0x1 << 2) /* 2-byte region watch.  */
-#define DR_LEN_4	(0x3 << 2) /* 4-byte region watch.  */
-#define DR_LEN_8	(0x2 << 2) /* 8-byte region watch (AMD64).  */
-
-/* Local and Global Enable flags in DR7.
-
-   When the Local Enable flag is set, the breakpoint/watchpoint is
-   enabled only for the current task; the processor automatically
-   clears this flag on every task switch.  When the Global Enable flag
-   is set, the breakpoint/watchpoint is enabled for all tasks; the
-   processor never clears this flag.
-
-   Currently, all watchpoint are locally enabled.  If you need to
-   enable them globally, read the comment which pertains to this in
-   i386_insert_aligned_watchpoint below.  */
-#define DR_LOCAL_ENABLE_SHIFT	0 /* Extra shift to the local enable bit.  */
-#define DR_GLOBAL_ENABLE_SHIFT	1 /* Extra shift to the global enable bit.  */
-#define DR_ENABLE_SIZE		2 /* Two enable bits per debug register.  */
-
-/* Local and global exact breakpoint enable flags (a.k.a. slowdown
-   flags).  These are only required on i386, to allow detection of the
-   exact instruction which caused a watchpoint to break; i486 and
-   later processors do that automatically.  We set these flags for
-   backwards compatibility.  */
-#define DR_LOCAL_SLOWDOWN	(0x100)
-#define DR_GLOBAL_SLOWDOWN     	(0x200)
-
-/* Fields reserved by Intel.  This includes the GD (General Detect
-   Enable) flag, which causes a debug exception to be generated when a
-   MOV instruction accesses one of the debug registers.
-
-   FIXME: My Intel manual says we should use 0xF800, not 0xFC00.  */
-#define DR_CONTROL_RESERVED	(0xFC00)
-
-/* Auxiliary helper macros.  */
-
-/* A value that masks all fields in DR7 that are reserved by Intel.  */
-#define I386_DR_CONTROL_MASK	(~DR_CONTROL_RESERVED)
-
-/* The I'th debug register is vacant if its Local and Global Enable
-   bits are reset in the Debug Control register.  */
-#define I386_DR_VACANT(i) \
-  ((dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
-
-/* Locally enable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_LOCAL_ENABLE(i) \
-  dr_control_mirror |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))
-
-/* Globally enable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_GLOBAL_ENABLE(i) \
-  dr_control_mirror |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))
-
-/* Disable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_DISABLE(i) \
-  dr_control_mirror &= ~(3 << (DR_ENABLE_SIZE * (i)))
-
-/* Set in DR7 the RW and LEN fields for the I'th debug register.  */
-#define I386_DR_SET_RW_LEN(i,rwlen) \
-  do { \
-    dr_control_mirror &= ~(0x0f << (DR_CONTROL_SHIFT+DR_CONTROL_SIZE*(i)));   \
-    dr_control_mirror |= ((rwlen) << (DR_CONTROL_SHIFT+DR_CONTROL_SIZE*(i))); \
-  } while (0)
-
-/* Get from DR7 the RW and LEN fields for the I'th debug register.  */
-#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 & 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 long dr_status_mirror, dr_control_mirror;
-
-/* Reference counts for each debug register.  */
-static int dr_ref_count[DR_NADDR];
+static struct i386_debug_reg_state state;
 
 /* Whether or not to print the mirrored debug registers.  */
 static int maint_show_dr;
 
-/* Types of operations supported by i386_handle_nonaligned_watchpoint.  */
-typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t;
-
 /* Internal functions.  */
 
 /* Return the value of a 4-bit field for DR7 suitable for watching a
@@ -172,7 +58,8 @@ static unsigned i386_length_and_rw_bits (int len, enum target_hw_bp_type type);
    value of the bit-field from DR7 which describes the length and
    access type of the region to be watched by this watchpoint.  Return
    0 on success, -1 on failure.  */
-static int i386_insert_aligned_watchpoint (CORE_ADDR addr,
+static int i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state,
+					   CORE_ADDR addr,
 					   unsigned len_rw_bits);
 
 /* Remove a watchpoint at address ADDR, which is assumed to be aligned
@@ -180,7 +67,8 @@ static int i386_insert_aligned_watchpoint (CORE_ADDR addr,
    value of the bits from DR7 which describes the length and access
    type of the region watched by this watchpoint.  Return 0 on
    success, -1 on failure.  */
-static int i386_remove_aligned_watchpoint (CORE_ADDR addr,
+static int i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state,
+					   CORE_ADDR addr,
 					   unsigned len_rw_bits);
 
 /* Insert or remove a (possibly non-aligned) watchpoint, or count the
@@ -189,7 +77,8 @@ static int i386_remove_aligned_watchpoint (CORE_ADDR addr,
    successful insertion or removal, a positive number when queried
    about the number of registers, or -1 on failure.  If WHAT is not a
    valid value, bombs through internal_error.  */
-static int i386_handle_nonaligned_watchpoint (i386_wp_op_t what,
+static int i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state,
+					      i386_wp_op_t what,
 					      CORE_ADDR addr, int len,
 					      enum target_hw_bp_type type);
 
@@ -205,11 +94,11 @@ i386_cleanup_dregs (void)
 
   ALL_DEBUG_REGISTERS(i)
     {
-      dr_mirror[i] = 0;
-      dr_ref_count[i] = 0;
+      state.dr_mirror[i] = 0;
+      state.dr_ref_count[i] = 0;
     }
-  dr_control_mirror = 0;
-  dr_status_mirror  = 0;
+  state.dr_control_mirror = 0;
+  state.dr_status_mirror  = 0;
 }
 
 /* Print the values of the mirrored debug registers.  This is called
@@ -217,8 +106,8 @@ i386_cleanup_dregs (void)
    show-debug-regs" at GDB's prompt.  */
 
 static void
-i386_show_dr (const char *func, CORE_ADDR addr,
-	      int len, enum target_hw_bp_type type)
+i386_show_dr (struct i386_debug_reg_state *state, 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;
@@ -239,13 +128,16 @@ i386_show_dr (const char *func, CORE_ADDR addr,
 				: "??unknown??"))));
   puts_unfiltered (":\n");
   printf_unfiltered ("\tCONTROL (DR7): %s          STATUS (DR6): %s\n",
-		     phex (dr_control_mirror, 8), phex (dr_status_mirror, 8));
+		     phex (state->dr_control_mirror, 8),
+		     phex (state->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, 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, phex (state->dr_mirror[i], addr_size),
+			 state->dr_ref_count[i], i+1,
+			 phex (state->dr_mirror[i+1], addr_size),
+			 state->dr_ref_count[i+1]);
       i++;
     }
 }
@@ -310,7 +202,8 @@ Invalid hardware breakpoint length %d in i386_length_and_rw_bits.\n"), len);
    success, -1 on failure.  */
 
 static int
-i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
+i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state,
+				CORE_ADDR addr, unsigned len_rw_bits)
 {
   int i;
 
@@ -322,11 +215,11 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
      reuse it for this watchpoint as well (and save a register).  */
   ALL_DEBUG_REGISTERS(i)
     {
-      if (!I386_DR_VACANT (i)
-	  && dr_mirror[i] == addr
-	  && I386_DR_GET_RW_LEN (i) == len_rw_bits)
+      if (!I386_DR_VACANT (state, i)
+	  && state->dr_mirror[i] == addr
+	  && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
 	{
-	  dr_ref_count[i]++;
+	  state->dr_ref_count[i]++;
 	  return 0;
 	}
     }
@@ -334,7 +227,7 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
   /* Next, look for a vacant debug register.  */
   ALL_DEBUG_REGISTERS(i)
     {
-      if (I386_DR_VACANT (i))
+      if (I386_DR_VACANT (state, i))
 	break;
     }
 
@@ -345,9 +238,9 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
   /* Now set up the register I to watch our region.  */
 
   /* Record the info in our local mirrored array.  */
-  dr_mirror[i] = addr;
-  dr_ref_count[i] = 1;
-  I386_DR_SET_RW_LEN (i, len_rw_bits);
+  state->dr_mirror[i] = addr;
+  state->dr_ref_count[i] = 1;
+  I386_DR_SET_RW_LEN (state, i, len_rw_bits);
   /* Note: we only enable the watchpoint locally, i.e. in the current
      task.  Currently, no i386 target allows or supports global
      watchpoints; however, if any target would want that in the
@@ -355,17 +248,17 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
      to enable watchpoints globally or locally, and the code below
      should use global or local enable and slow-down flags as
      appropriate.  */
-  I386_DR_LOCAL_ENABLE (i);
-  dr_control_mirror |= DR_LOCAL_SLOWDOWN;
-  dr_control_mirror &= I386_DR_CONTROL_MASK;
+  I386_DR_LOCAL_ENABLE (state, i);
+  state->dr_control_mirror |= DR_LOCAL_SLOWDOWN;
+  state->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_control (state->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));
+    i386_dr_low.unset_status ((1<<i));
 
   return 0;
 }
@@ -377,23 +270,24 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
    success, -1 on failure.  */
 
 static int
-i386_remove_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
+i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state,
+				CORE_ADDR addr, unsigned len_rw_bits)
 {
   int i, retval = -1;
 
   ALL_DEBUG_REGISTERS(i)
     {
-      if (!I386_DR_VACANT (i)
-	  && dr_mirror[i] == addr
-	  && I386_DR_GET_RW_LEN (i) == len_rw_bits)
+      if (!I386_DR_VACANT (state, i)
+	  && state->dr_mirror[i] == addr
+	  && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
 	{
-	  if (--dr_ref_count[i] == 0) /* no longer in use?  */
+	  if (--state->dr_ref_count[i] == 0) /* no longer in use?  */
 	    {
 	      /* Reset our mirror.  */
-	      dr_mirror[i] = 0;
-	      I386_DR_DISABLE (i);
+	      state->dr_mirror[i] = 0;
+	      I386_DR_DISABLE (state, i);
 	      /* Reset it in the inferior.  */
-	      i386_dr_low.set_control (dr_control_mirror);
+	      i386_dr_low.set_control (state->dr_control_mirror);
 	      if (i386_dr_low.reset_addr)
 		i386_dr_low.reset_addr (i);
 	    }
@@ -412,7 +306,8 @@ i386_remove_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
    valid value, bombs through internal_error.  */
 
 static int
-i386_handle_nonaligned_watchpoint (i386_wp_op_t what, CORE_ADDR addr, int len,
+i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state,
+				   i386_wp_op_t what, CORE_ADDR addr, int len,
 				   enum target_hw_bp_type type)
 {
   int retval = 0, status = 0;
@@ -453,9 +348,9 @@ i386_handle_nonaligned_watchpoint (i386_wp_op_t what, CORE_ADDR addr, int len,
 	  unsigned len_rw = i386_length_and_rw_bits (size, type);
 
 	  if (what == WP_INSERT)
-	    status = i386_insert_aligned_watchpoint (addr, len_rw);
+	    status = i386_insert_aligned_watchpoint (state, addr, len_rw);
 	  else if (what == WP_REMOVE)
-	    status = i386_remove_aligned_watchpoint (addr, len_rw);
+	    status = i386_remove_aligned_watchpoint (state, addr, len_rw);
 	  else
 	    internal_error (__FILE__, __LINE__, _("\
 Invalid value %d of operation in i386_handle_nonaligned_watchpoint.\n"),
@@ -495,16 +390,17 @@ i386_insert_watchpoint (CORE_ADDR addr, int len, int type,
 
   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);
+    retval = i386_handle_nonaligned_watchpoint (&state, WP_INSERT, addr, len,
+						type);
   else
     {
       unsigned len_rw = i386_length_and_rw_bits (len, type);
 
-      retval = i386_insert_aligned_watchpoint (addr, len_rw);
+      retval = i386_insert_aligned_watchpoint (&state, addr, len_rw);
     }
 
   if (maint_show_dr)
-    i386_show_dr ("insert_watchpoint", addr, len, type);
+    i386_show_dr (&state, "insert_watchpoint", addr, len, type);
 
   return retval;
 }
@@ -520,16 +416,17 @@ i386_remove_watchpoint (CORE_ADDR addr, int len, int type,
 
   if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8))
       || addr % len != 0)
-    retval = i386_handle_nonaligned_watchpoint (WP_REMOVE, addr, len, type);
+    retval = i386_handle_nonaligned_watchpoint (&state, WP_REMOVE, addr, len,
+						type);
   else
     {
       unsigned len_rw = i386_length_and_rw_bits (len, type);
 
-      retval = i386_remove_aligned_watchpoint (addr, len_rw);
+      retval = i386_remove_aligned_watchpoint (&state, addr, len_rw);
     }
 
   if (maint_show_dr)
-    i386_show_dr ("remove_watchpoint", addr, len, type);
+    i386_show_dr (&state, "remove_watchpoint", addr, len, type);
 
   return retval;
 }
@@ -544,7 +441,8 @@ i386_region_ok_for_watchpoint (CORE_ADDR addr, int len)
 
   /* Compute how many aligned watchpoints we would need to cover this
      region.  */
-  nregs = i386_handle_nonaligned_watchpoint (WP_COUNT, addr, len, hw_write);
+  nregs = i386_handle_nonaligned_watchpoint (&state, WP_COUNT, addr, len,
+					     hw_write);
   return nregs <= DR_NADDR ? 1 : 0;
 }
 
@@ -559,29 +457,29 @@ i386_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
   int i;
   int rc = 0;
 
-  dr_status_mirror = i386_dr_low.get_status ();
+  state.dr_status_mirror = i386_dr_low.get_status ();
 
   ALL_DEBUG_REGISTERS(i)
     {
-      if (I386_DR_WATCH_HIT (i)
+      if (I386_DR_WATCH_HIT (state.dr_status_mirror, i)
 	  /* This second condition makes sure DRi is set up for a data
 	     watchpoint, not a hardware breakpoint.  The reason is
 	     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 (state.dr_control_mirror, i) != 0
 	  /* This third condition makes sure DRi is not vacant, this
 	     avoids false positives in windows-nat.c.  */
-	  && !I386_DR_VACANT (i))
+	  && !I386_DR_VACANT (&state, i))
 	{
-	  addr = dr_mirror[i];
+	  addr = state.dr_mirror[i];
 	  rc = 1;
 	  if (maint_show_dr)
-	    i386_show_dr ("watchpoint_hit", addr, -1, hw_write);
+	    i386_show_dr (&state, "watchpoint_hit", addr, -1, hw_write);
 	}
     }
   if (maint_show_dr && addr == 0)
-    i386_show_dr ("stopped_data_addr", 0, 0, hw_write);
+    i386_show_dr (&state, "stopped_data_addr", 0, 0, hw_write);
 
   if (rc)
     *addr_p = addr;
@@ -603,10 +501,11 @@ i386_insert_hw_breakpoint (struct gdbarch *gdbarch,
 {
   unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
   CORE_ADDR addr = bp_tgt->placed_address;
-  int retval = i386_insert_aligned_watchpoint (addr, len_rw) ? EBUSY : 0;
+  int retval = i386_insert_aligned_watchpoint (&state, addr, len_rw)
+    ? EBUSY : 0;
 
   if (maint_show_dr)
-    i386_show_dr ("insert_hwbp", addr, 1, hw_execute);
+    i386_show_dr (&state, "insert_hwbp", addr, 1, hw_execute);
 
   return retval;
 }
@@ -620,10 +519,10 @@ i386_remove_hw_breakpoint (struct gdbarch *gdbarch,
 {
   unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
   CORE_ADDR addr = bp_tgt->placed_address;
-  int retval = i386_remove_aligned_watchpoint (addr, len_rw);
+  int retval = i386_remove_aligned_watchpoint (&state, addr, len_rw);
 
   if (maint_show_dr)
-    i386_show_dr ("remove_hwbp", addr, 1, hw_execute);
+    i386_show_dr (&state, "remove_hwbp", addr, 1, hw_execute);
 
   return retval;
 }
diff --git a/gdb/i386-nat.h b/gdb/i386-nat.h
index 819c6b8..2579ac6 100644
--- a/gdb/i386-nat.h
+++ b/gdb/i386-nat.h
@@ -21,6 +21,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "i386-common.h"
 
 #ifndef I386_NAT_H
 #define I386_NAT_H 1
@@ -33,50 +34,6 @@
 struct target_ops;
 extern void i386_use_watchpoints (struct target_ops *);
 
-/* Support for hardware watchpoints and breakpoints using the i386
-   debug registers.
-
-   This provides several functions for inserting and removing
-   hardware-assisted breakpoints and watchpoints, testing if one or
-   more of the watchpoints triggered and at what address, checking
-   whether a given region can be watched, etc.
-
-   In addition, each target should provide several low-level functions
-   regrouped into i386_dr_low_type struct below.  These functions
-   that will be called to insert watchpoints and hardware breakpoints
-   into the inferior, remove them, and check their status.  These
-   functions are:
-
-      set_control              -- set the debug control (DR7)
-				  register to a given value for all LWPs
-
-      set_addr                 -- put an address into one debug
-				  register for all LWPs
-
-      reset_addr               -- reset the address stored in
-				  one debug register for all LWPs
-
-      get_status               -- return the value of the debug
-				  status (DR6) register for current LWP
-
-      unset_status             -- unset the specified bits of the debug
-				  status (DR6) register for all LWPs
-
-   Additionally, the native file should set the debug_register_length
-   field to 4 or 8 depending on the number of bytes used for
-   deubg registers.  */
-
-struct i386_dr_low_type 
-  {
-    void (*set_control) (unsigned long);
-    void (*set_addr) (int, CORE_ADDR);
-    void (*reset_addr) (int);
-    unsigned long (*get_status) (void);
-    void (*unset_status) (unsigned long);
-    int debug_register_length;
-  };
-
-extern struct i386_dr_low_type i386_dr_low;
 
 /* Use this function to set i386_dr_low debug_register_length field
    rather than setting it directly to check that the length is only

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-13  9:58 [patch] Move common macros to i386-common.h Yao Qi
@ 2011-02-13 13:40 ` Mark Kettenis
  2011-02-17  6:36   ` Yao Qi
  2011-02-23  5:36   ` Yao Qi
  2011-03-11  6:39 ` [try 2nd, patch] Move common macros to i386-dbg-reg.h Yao Qi
  1 sibling, 2 replies; 19+ messages in thread
From: Mark Kettenis @ 2011-02-13 13:40 UTC (permalink / raw)
  To: Yao Qi; +Cc: gdb-patches

>  Some macros are duplicated in gdb/i386-nat and gdb/gdbserver/i386-low.
>  This patch is to move common macros to gdb/common/i386-common.h.
>
>  There are also some duplicated functions, which should be moved in next
>  step.

I'm just back from a 3+-week holiday.  I'm not buying into the "share more
stuff between gdb and gdbserver" mantra yet.  Please hold off on this
until
I've gotten caught up with my mail and had a chance to think about all this.

Thanks,

Mark



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-13 13:40 ` Mark Kettenis
@ 2011-02-17  6:36   ` Yao Qi
  2011-02-17  6:41     ` Joel Brobecker
  2011-02-23  5:36   ` Yao Qi
  1 sibling, 1 reply; 19+ messages in thread
From: Yao Qi @ 2011-02-17  6:36 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: gdb-patches

On 02/13/2011 09:40 PM, Mark Kettenis wrote:
> I'm just back from a 3+-week holiday.  I'm not buying into the "share more
> stuff between gdb and gdbserver" mantra yet.  Please hold off on this
> until
> I've gotten caught up with my mail and had a chance to think about all this.
> 
OK, no problem.  IMO, it is beneficial to source code maintenance.

w.r.t this specific patch, its "potential benefit" is that, some
duplicated functions (i386_length_and_rw_bits, and
i386_{insert|remove}_aligned_watchpoint) in i386-nat.c and
gdbserver/i386-low.c can be merged together.  Please take this into account.

-- 
Yao (齐尧)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-17  6:36   ` Yao Qi
@ 2011-02-17  6:41     ` Joel Brobecker
  2011-02-17 18:15       ` Jan Kratochvil
  0 siblings, 1 reply; 19+ messages in thread
From: Joel Brobecker @ 2011-02-17  6:41 UTC (permalink / raw)
  To: Yao Qi; +Cc: Mark Kettenis, gdb-patches

> w.r.t this specific patch, its "potential benefit" is that, some
> duplicated functions (i386_length_and_rw_bits, and
> i386_{insert|remove}_aligned_watchpoint) in i386-nat.c and
> gdbserver/i386-low.c can be merged together.  Please take this into account.

Thinking beyond that, I think that GDB should conceptualy contain
a internal GDBserver. That way, when we port GDB to an architecture,
we're pretty much porting GDBserver as well, for free. And when
someone only ported GDBserver at one point, we don't need to redo
that work when porting GDB itself.

-- 
Joel


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-17  6:41     ` Joel Brobecker
@ 2011-02-17 18:15       ` Jan Kratochvil
  0 siblings, 0 replies; 19+ messages in thread
From: Jan Kratochvil @ 2011-02-17 18:15 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: Yao Qi, Mark Kettenis, gdb-patches

On Thu, 17 Feb 2011 07:36:41 +0100, Joel Brobecker wrote:
> Thinking beyond that, I think that GDB should conceptualy contain
> a internal GDBserver. That way, when we port GDB to an architecture,
> we're pretty much porting GDBserver as well, for free. And when
> someone only ported GDBserver at one point, we don't need to redo
> that work when porting GDB itself.

It was discussed before, an external gdbserver is IMO fine, the problem is
some important features are now present only in linux-nat (such as
`set follow-fork-mode') (and other features are only in gdbserver now).
After the features are on-par in gdbserver the switch will happen ASAP.


Thanks,
Jan


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-13 13:40 ` Mark Kettenis
  2011-02-17  6:36   ` Yao Qi
@ 2011-02-23  5:36   ` Yao Qi
  2011-02-23 21:35     ` Mark Kettenis
  1 sibling, 1 reply; 19+ messages in thread
From: Yao Qi @ 2011-02-23  5:36 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: gdb-patches

On 02/13/2011 09:40 PM, Mark Kettenis wrote:
> I'm just back from a 3+-week holiday.  I'm not buying into the "share more
> stuff between gdb and gdbserver" mantra yet.  Please hold off on this
> until
> I've gotten caught up with my mail and had a chance to think about all this.

Mark,
Do you ever have a change to look at this patch?  I am inclined to
rename i386-common.h to i386-dbg-reg.h, since it is really about debug
registers.

-- 
Yao (齐尧)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-23  5:36   ` Yao Qi
@ 2011-02-23 21:35     ` Mark Kettenis
  2011-02-24  4:32       ` Yao Qi
                         ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Mark Kettenis @ 2011-02-23 21:35 UTC (permalink / raw)
  To: yao; +Cc: gdb-patches

> Date: Wed, 23 Feb 2011 13:26:33 +0800
> From: Yao Qi <yao@codesourcery.com>
> 
> On 02/13/2011 09:40 PM, Mark Kettenis wrote:
> > I'm just back from a 3+-week holiday.  I'm not buying into the "share more
> > stuff between gdb and gdbserver" mantra yet.  Please hold off on this
> > until
> > I've gotten caught up with my mail and had a chance to think about all this.
> 
> Mark,
> Do you ever have a change to look at this patch?  I am inclined to
> rename i386-common.h to i386-dbg-reg.h, since it is really about debug
> registers.

OK, here we go.  I don't have an issue with this diff per-se, but more
with the direction this is going.  A long time ago, gdb and gdbserver
shared a lot of code.  The gdbserver code could only be built on a
limited set of platforms though.  As a result gdbserver builds ended
up being broken often because of changes made to only gdb.  That's one
of the reasons why we split the two codebases.  Now, years later,
people are moving in the other direction again, and I'm worried it
will bring back the problems we had in the past.

I'm particularly worried about changes I'll make myself.  My primary
development platform is OpenBSD which isn't supported by gdbserver.
So I won't be building the gdbserver code, so I won't notice any
problems my diffs (and other people's diffs) will introduce in
gdbserver.

Sharing architecture-specific #define's is probably fine.  Sharing
some simple basec support functions may also be ok.  But I don't think
sharing more complicated code (such as the code manipulating the i386
debug registers) is a good idea.

Mark


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-23 21:35     ` Mark Kettenis
@ 2011-02-24  4:32       ` Yao Qi
  2011-02-24  5:11       ` Joel Brobecker
  2011-02-28 18:12       ` Tom Tromey
  2 siblings, 0 replies; 19+ messages in thread
From: Yao Qi @ 2011-02-24  4:32 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: gdb-patches

On 02/24/2011 05:17 AM, Mark Kettenis wrote:
> I'm particularly worried about changes I'll make myself.  My primary
> development platform is OpenBSD which isn't supported by gdbserver.
> So I won't be building the gdbserver code, so I won't notice any
> problems my diffs (and other people's diffs) will introduce in
> gdbserver.
> 

Everyone here can only test his/her diff on a limited number of
combination of arch and os.  If diff breaks gdb or gdbserver, it should
be fixed.

> Sharing architecture-specific #define's is probably fine.  Sharing
> some simple basec support functions may also be ok.  But I don't think
> sharing more complicated code (such as the code manipulating the i386
> debug registers) is a good idea.

To my patch, it is like an equivalent transformation.  Code moved in gdb
and gdbserver are exactly 100% same.  AFAICS, i386 debug register
manipulation code is the same on gdb and gdbserver.  Why do you think it
is not a good idea to move them?  Am I missing something?

-- 
Yao (齐尧)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-23 21:35     ` Mark Kettenis
  2011-02-24  4:32       ` Yao Qi
@ 2011-02-24  5:11       ` Joel Brobecker
  2011-02-28 18:12       ` Tom Tromey
  2 siblings, 0 replies; 19+ messages in thread
From: Joel Brobecker @ 2011-02-24  5:11 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: yao, gdb-patches

> I'm particularly worried about changes I'll make myself.
[...]
> Sharing architecture-specific #define's is probably fine.  Sharing
> some simple basec support functions may also be ok.  But I don't think
> sharing more complicated code (such as the code manipulating the i386
> debug registers) is a good idea.

I think (hope!) that this would be a transitionary problem, and I am
willing to take on any breakage inadvertandly introduced - We do
gdbserver builds nightly and I update our sources every week or so.

But I think we should share the complicated code, simply because
it is getting increasingly complex, thanks to new features such as
non-stop, tracepoints, or older features such as threading support
which evolve as the kernel capabilities evolve. And while we're at it,
why would we want to continue writing the same code twice?

Right now, we have some bugs in GDB but not in GDBserver, some bugs in
GDBserver but not GDB, and some bugs in both.  Everytime we have a bug
in one of them, but not the other, we just look at how we deal with the
situation in the other, and back-port the idea. Or, when the bug is in
both, we have to fix the problem twice, except that the API is different,
and thus we do the work twice.

My view of the direction we should take is that we should have the
same API for doing all the target stuff: start inferior, next/step/cont,
read/write register/memory, get shared libraries, tracepoints,
watchpoints, etc. Once both use this API, then the remaining part
of GDBserver should be the common code that implements the stub-side
of the remote protocol, which should pretty much build out-of-the-box
on any OS. The consequence of that should be that, once you port GDB,
you pretty much get GDBserver for free.  And the other consequence is
if you decided to port GDBserver only as a first step, you wouldn't
have to do the work again when you decide to port GDB.

-- 
Joel


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [patch] Move common macros to i386-common.h
  2011-02-23 21:35     ` Mark Kettenis
  2011-02-24  4:32       ` Yao Qi
  2011-02-24  5:11       ` Joel Brobecker
@ 2011-02-28 18:12       ` Tom Tromey
  2 siblings, 0 replies; 19+ messages in thread
From: Tom Tromey @ 2011-02-28 18:12 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: yao, gdb-patches

>>>>> "Mark" == Mark Kettenis <mark.kettenis@xs4all.nl> writes:

Mark> I'm particularly worried about changes I'll make myself.  My primary
Mark> development platform is OpenBSD which isn't supported by gdbserver.
Mark> So I won't be building the gdbserver code, so I won't notice any
Mark> problems my diffs (and other people's diffs) will introduce in
Mark> gdbserver.

I agree this is a potential problem, but it does not worry me very much.

First, I think that we already just make a best effort not to break GDB
on hosts that do not have active GDB developers.

Second, for active hosts, breakage is noticed and fixed quickly.

Third, we're looking at setting up a buildbot for GDB.  See:

    http://sourceware.org/gdb/wiki/BuildBot

We're still discussing the various options, but one idea is to host it
on the GCC Compile Farm, plus let people host their own buildslaves.

Tom


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-02-13  9:58 [patch] Move common macros to i386-common.h Yao Qi
  2011-02-13 13:40 ` Mark Kettenis
@ 2011-03-11  6:39 ` Yao Qi
  2011-03-29  7:54   ` Yao Qi
  1 sibling, 1 reply; 19+ messages in thread
From: Yao Qi @ 2011-03-11  6:39 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 486 bytes --]

On 02/13/2011 05:57 PM, Yao Qi wrote:
> Some macros are duplicated in gdb/i386-nat and gdb/gdbserver/i386-low.
> This patch is to move common macros to gdb/common/i386-common.h.
> 
> There are also some duplicated functions, which should be moved in next
> step.
> 

Compared with last version, two changes are in this new patch,
 1) rename i386-common.h to i386-dbg-reg.h,
 2) add dependency tracking in gdbserver.

OK for mainline after 7.3 branch is created?

-- 
Yao (齐尧)

[-- Attachment #2: i386-dbg-reg-h-0311.patch --]
[-- Type: text/x-patch, Size: 37815 bytes --]

gdb/

	* i386-nat.h: Include i386-dbg-reg.h.
	Move macros to i386-dbg-reg.h.
	* i386-nat.c: Move macros and structs to i386-dbg-reg.h.
	New variable struct i386_debug_reg_state state to replace
	other variables dr_mirror, dr_ref_count, dr_control_mirror,
	and dr_status_mirror.
	(i386_insert_aligned_watchpoint): Add one formal parameter
	struct i386_debug_reg_state *STATE.
	Update code using these variables.	
	(i386_remove_aligned_watchpoint, i386_show_dr): Likewise.
	(i386_cleanup_dregs): Likewise.
	(i386_handle_nonaligned_watchpoint): Likewise.
	* common/i386-dbg-reg.h: New.  Common macros and structs.

gdb/gdbserver/

	* i386-low.h: Include i386-dbg-reg.h.
	Move macros to i386-dbg-reg.h.
	* i386-low.c (i386_set_debug_register_length): New.
	(i386_low_init_dregs): Call i386_set_debug_register_length.
	* Makefile.in (i386_low_h) Depends on i386-dbg-reg.h.

diff --git a/gdb/common/i386-dbg-reg.h b/gdb/common/i386-dbg-reg.h
new file mode 100644
index 0000000..0870b76
--- /dev/null
+++ b/gdb/common/i386-dbg-reg.h
@@ -0,0 +1,230 @@
+/* Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef I386_DBG_REG_H
+#define I386_DBG_REG_H 1
+
+/* DR7 Debug Control register fields.  */
+
+/* How many bits to skip in DR7 to get to R/W and LEN fields.  */
+#define DR_CONTROL_SHIFT	16
+/* How many bits in DR7 per R/W and LEN field for each watchpoint.  */
+#define DR_CONTROL_SIZE		4
+
+/* Watchpoint/breakpoint read/write fields in DR7.  */
+#define DR_RW_EXECUTE	(0x0)	/* Break on instruction execution.  */
+#define DR_RW_WRITE	(0x1)	/* Break on data writes.  */
+#define DR_RW_READ	(0x3)	/* Break on data reads or writes.  */
+/* This is here for completeness.  No platform supports this
+   functionality yet (as of March 2001).  Note that the DE flag in the
+   CR4 register needs to be set to support this.  */
+#ifndef DR_RW_IORW
+#define DR_RW_IORW	(0x2)	/* Break on I/O reads or writes.  */
+#endif
+
+/* Debug registers' indices.  */
+#define DR_NADDR	4	/* The number of debug address registers.  */
+#define DR_STATUS	6	/* Index of debug status register (DR6).  */
+#define DR_CONTROL	7	/* Index of debug control register (DR7). */
+
+/* Watchpoint/breakpoint length fields in DR7.  The 2-bit left shift
+   is so we could OR this with the read/write field defined above.  */
+#define DR_LEN_1	(0x0 << 2) /* 1-byte region watch or breakpoint.  */
+#define DR_LEN_2	(0x1 << 2) /* 2-byte region watch.  */
+#define DR_LEN_4	(0x3 << 2) /* 4-byte region watch.  */
+#define DR_LEN_8	(0x2 << 2) /* 8-byte region watch (AMD64).  */
+
+/* Local and Global Enable flags in DR7.
+
+   When the Local Enable flag is set, the breakpoint/watchpoint is
+   enabled only for the current task; the processor automatically
+   clears this flag on every task switch.  When the Global Enable flag
+   is set, the breakpoint/watchpoint is enabled for all tasks; the
+   processor never clears this flag.
+
+   Currently, all watchpoint are locally enabled.  If you need to
+   enable them globally, read the comment which pertains to this in
+   i386_insert_aligned_watchpoint below.  */
+#define DR_LOCAL_ENABLE_SHIFT	0 /* Extra shift to the local enable bit.  */
+#define DR_GLOBAL_ENABLE_SHIFT	1 /* Extra shift to the global enable bit.  */
+#define DR_ENABLE_SIZE		2 /* Two enable bits per debug register.  */
+
+/* Local and global exact breakpoint enable flags (a.k.a. slowdown
+   flags).  These are only required on i386, to allow detection of the
+   exact instruction which caused a watchpoint to break; i486 and
+   later processors do that automatically.  We set these flags for
+   backwards compatibility.  */
+#define DR_LOCAL_SLOWDOWN	(0x100)
+#define DR_GLOBAL_SLOWDOWN	(0x200)
+
+/* Fields reserved by Intel.  This includes the GD (General Detect
+   Enable) flag, which causes a debug exception to be generated when a
+   MOV instruction accesses one of the debug registers.
+
+   FIXME: My Intel manual says we should use 0xF800, not 0xFC00.  */
+#define DR_CONTROL_RESERVED	(0xFC00)
+
+/* Auxiliary helper macros.  */
+
+/* A value that masks all fields in DR7 that are reserved by Intel.  */
+#define I386_DR_CONTROL_MASK	(~DR_CONTROL_RESERVED)
+
+#ifndef DR_FIRSTADDR
+#define DR_FIRSTADDR 0
+#endif
+
+#ifndef DR_LASTADDR
+#define DR_LASTADDR 3
+#endif
+
+/* Types of operations supported by i386_handle_nonaligned_watchpoint.  */
+typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t;
+
+/* Return the value of a 4-bit field for DR7 suitable for watching a
+   region of LEN bytes for accesses of type TYPE.  LEN is assumed to
+   have the value of 1, 2, or 4.  */
+/*
+static unsigned i386_length_and_rw_bits (int len, enum target_hw_bp_type type);
+*/
+
+/* Global state needed to track h/w watchpoints.  */
+
+struct i386_debug_reg_state
+{
+  /* Mirror the inferior's DRi registers.  We keep the status and
+     control registers separated because they don't hold addresses.
+     Note that since we can change these mirrors while threads are
+     running, we never trust them to explain a cause of a trap.
+     For that, we need to peek directly in the inferior registers.  */
+  CORE_ADDR dr_mirror[DR_NADDR];
+  unsigned dr_status_mirror, dr_control_mirror;
+
+  /* Reference counts for each debug register.  */
+  int dr_ref_count[DR_NADDR];
+};
+
+/* Support for hardware watchpoints and breakpoints using the i386
+   debug registers.
+
+   This provides several functions for inserting and removing
+   hardware-assisted breakpoints and watchpoints, testing if one or
+   more of the watchpoints triggered and at what address, checking
+   whether a given region can be watched, etc.
+
+   The functions below implement debug registers sharing by reference
+   counts, and allow to watch regions up to 16 bytes long.  */
+
+/* Support for hardware watchpoints and breakpoints using the i386
+   debug registers.
+
+   This provides several functions for inserting and removing
+   hardware-assisted breakpoints and watchpoints, testing if one or
+   more of the watchpoints triggered and at what address, checking
+   whether a given region can be watched, etc.
+
+   In addition, each target should provide several low-level functions
+   regrouped into i386_dr_low_type struct below. These functions
+   that will be called to insert watchpoints and hardware breakpoints
+   into the inferior, remove them, and check their status.  These
+   functions are:
+
+      set_control              -- set the debug control (DR7)
+				  register to a given value for all LWPs
+
+      set_addr                 -- put an address into one debug
+				  register for all LWPs
+
+      reset_addr               -- reset the address stored in
+				  one debug register for all LWPs
+
+      get_status               -- return the value of the debug
+				  status (DR6) register for current LWP
+
+      unset_status             -- unset the specified bits of the debug
+				  status (DR6) register for all LWPs
+
+   Additionally, the native file should set the debug_register_length
+   field to 4 or 8 depending on the number of bytes used for
+   deubg registers.  */
+
+struct i386_dr_low_type
+  {
+    void (*set_control) (unsigned long);
+    void (*set_addr) (int, CORE_ADDR);
+    void (*reset_addr) (int);
+    unsigned long (*get_status) (void);
+    void (*unset_status) (unsigned long);
+    int debug_register_length;
+  };
+
+extern struct i386_dr_low_type i386_dr_low;
+
+/* Support for 8-byte wide hw watchpoints.  */
+#define TARGET_HAS_DR_LEN_8 (i386_dr_low.debug_register_length == 8)
+
+/* Auxiliary helper macros.  */
+
+/* A value that masks all fields in DR7 that are reserved by Intel.  */
+#define I386_DR_CONTROL_MASK	(~DR_CONTROL_RESERVED)
+
+/* The I'th debug register is vacant if its Local and Global Enable
+   bits are reset in the Debug Control register.  */
+#define I386_DR_VACANT(state, i) \
+  (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
+
+/* Locally enable the break/watchpoint in the I'th debug register.  */
+#define I386_DR_LOCAL_ENABLE(state, i) \
+  do { \
+    (state)->dr_control_mirror |= \
+      (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
+  } while (0)
+
+/* Globally enable the break/watchpoint in the I'th debug register.  */
+#define I386_DR_GLOBAL_ENABLE(state, i) \
+  do { \
+    (state)->dr_control_mirror |= \
+      (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
+  } while (0)
+
+/* Disable the break/watchpoint in the I'th debug register.  */
+#define I386_DR_DISABLE(state, i) \
+  do { \
+    (state)->dr_control_mirror &= \
+      ~(3 << (DR_ENABLE_SIZE * (i))); \
+  } while (0)
+
+/* Set in DR7 the RW and LEN fields for the I'th debug register.  */
+#define I386_DR_SET_RW_LEN(state, i,rwlen) \
+  do { \
+    (state)->dr_control_mirror &= \
+      ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
+    (state)->dr_control_mirror |= \
+      ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
+  } while (0)
+
+/* Get from DR7 the RW and LEN fields for the I'th debug register.  */
+#define I386_DR_GET_RW_LEN(dr7, i) \
+  (((dr7) \
+    >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)
+
+/* Did the watchpoint whose address is in the I'th register break?  */
+#define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i)))
+
+/* A macro to loop over all debug registers.  */
+#define ALL_DEBUG_REGISTERS(i)	for (i = 0; i < DR_NADDR; i++)
+
+#endif
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index 1fabe59..e70c8b5 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -402,7 +402,7 @@ vasprintf.o: $(srcdir)/../../libiberty/vasprintf.c
 vsnprintf.o: $(srcdir)/../../libiberty/vsnprintf.c
 	$(CC) -o vsnprintf.o -c $(CPPFLAGS) $(INTERNAL_CFLAGS) $<
 
-i386_low_h = $(srcdir)/i386-low.h
+i386_low_h = $(srcdir)/i386-low.h $(srcdir)/../common/i386-dbg-reg.h
 
 i386-low.o: i386-low.c $(i386_low_h) $(server_h) $(target_h)
 
diff --git a/gdb/gdbserver/i386-low.c b/gdb/gdbserver/i386-low.c
index 1baa23d..ca157b7 100644
--- a/gdb/gdbserver/i386-low.c
+++ b/gdb/gdbserver/i386-low.c
@@ -21,11 +21,7 @@
 #include "target.h"
 #include "i386-low.h"
 
-/* Support for 8-byte wide hw watchpoints.  */
-#ifndef TARGET_HAS_DR_LEN_8
-/* NOTE: sizeof (long) == 4 on win64.  */
-#define TARGET_HAS_DR_LEN_8 (sizeof (void *) == 8)
-#endif
+struct i386_dr_low_type i386_dr_low;
 
 enum target_hw_bp_type
   {
@@ -35,120 +31,18 @@ enum target_hw_bp_type
     hw_execute = 3	/* Execute HW breakpoint */
   };
 
-/* DR7 Debug Control register fields.  */
-
-/* How many bits to skip in DR7 to get to R/W and LEN fields.  */
-#define DR_CONTROL_SHIFT	16
-/* How many bits in DR7 per R/W and LEN field for each watchpoint.  */
-#define DR_CONTROL_SIZE		4
-
-/* Watchpoint/breakpoint read/write fields in DR7.  */
-#define DR_RW_EXECUTE	(0x0)	/* Break on instruction execution.  */
-#define DR_RW_WRITE	(0x1)	/* Break on data writes.  */
-#define DR_RW_READ	(0x3)	/* Break on data reads or writes.  */
-
-/* This is here for completeness.  No platform supports this
-   functionality yet (as of March 2001).  Note that the DE flag in the
-   CR4 register needs to be set to support this.  */
-#ifndef DR_RW_IORW
-#define DR_RW_IORW	(0x2)	/* Break on I/O reads or writes.  */
-#endif
+static void
+i386_set_debug_register_length (int len)
+{
+  gdb_assert (i386_dr_low.debug_register_length == 0);
+  gdb_assert (len == 4 || len == 8);
+  i386_dr_low.debug_register_length = len;
+}
 
-/* Watchpoint/breakpoint length fields in DR7.  The 2-bit left shift
-   is so we could OR this with the read/write field defined above.  */
-#define DR_LEN_1	(0x0 << 2) /* 1-byte region watch or breakpoint.  */
-#define DR_LEN_2	(0x1 << 2) /* 2-byte region watch.  */
-#define DR_LEN_4	(0x3 << 2) /* 4-byte region watch.  */
-#define DR_LEN_8	(0x2 << 2) /* 8-byte region watch (AMD64).  */
-
-/* Local and Global Enable flags in DR7.
-
-   When the Local Enable flag is set, the breakpoint/watchpoint is
-   enabled only for the current task; the processor automatically
-   clears this flag on every task switch.  When the Global Enable flag
-   is set, the breakpoint/watchpoint is enabled for all tasks; the
-   processor never clears this flag.
-
-   Currently, all watchpoint are locally enabled.  If you need to
-   enable them globally, read the comment which pertains to this in
-   i386_insert_aligned_watchpoint below.  */
-#define DR_LOCAL_ENABLE_SHIFT	0 /* Extra shift to the local enable bit.  */
-#define DR_GLOBAL_ENABLE_SHIFT	1 /* Extra shift to the global enable bit.  */
-#define DR_ENABLE_SIZE		2 /* Two enable bits per debug register.  */
-
-/* Local and global exact breakpoint enable flags (a.k.a. slowdown
-   flags).  These are only required on i386, to allow detection of the
-   exact instruction which caused a watchpoint to break; i486 and
-   later processors do that automatically.  We set these flags for
-   backwards compatibility.  */
-#define DR_LOCAL_SLOWDOWN	(0x100)
-#define DR_GLOBAL_SLOWDOWN	(0x200)
-
-/* Fields reserved by Intel.  This includes the GD (General Detect
-   Enable) flag, which causes a debug exception to be generated when a
-   MOV instruction accesses one of the debug registers.
-
-   FIXME: My Intel manual says we should use 0xF800, not 0xFC00.  */
-#define DR_CONTROL_RESERVED	(0xFC00)
-
-/* Auxiliary helper macros.  */
-
-/* A value that masks all fields in DR7 that are reserved by Intel.  */
-#define I386_DR_CONTROL_MASK	(~DR_CONTROL_RESERVED)
-
-/* The I'th debug register is vacant if its Local and Global Enable
-   bits are reset in the Debug Control register.  */
-#define I386_DR_VACANT(state, i) \
-  (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
-
-/* Locally enable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_LOCAL_ENABLE(state, i) \
-  do { \
-    (state)->dr_control_mirror |= \
-      (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
-  } while (0)
-
-/* Globally enable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_GLOBAL_ENABLE(state, i) \
-  do { \
-    (state)->dr_control_mirror |= \
-      (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
-  } while (0)
-
-/* Disable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_DISABLE(state, i) \
-  do { \
-    (state)->dr_control_mirror &= \
-      ~(3 << (DR_ENABLE_SIZE * (i))); \
-  } while (0)
-
-/* Set in DR7 the RW and LEN fields for the I'th debug register.  */
-#define I386_DR_SET_RW_LEN(state, i,rwlen) \
-  do { \
-    (state)->dr_control_mirror &= \
-      ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
-    (state)->dr_control_mirror |= \
-      ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
-  } while (0)
-
-/* Get from DR7 the RW and LEN fields for the I'th debug register.  */
-#define I386_DR_GET_RW_LEN(dr7, i) \
-  (((dr7) \
-    >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)
-
-/* Did the watchpoint whose address is in the I'th register break?  */
-#define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i)))
-
-/* A macro to loop over all debug registers.  */
-#define ALL_DEBUG_REGISTERS(i)	for (i = 0; i < DR_NADDR; i++)
-
-/* Types of operations supported by i386_handle_nonaligned_watchpoint.  */
-typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t;
-\f
 /* Implementation.  */
 
 /* Clear the reference counts and forget everything we knew about the
-   debug registers.  */
+   debug registers.  Also set the length of debug register.  */
 
 void
 i386_low_init_dregs (struct i386_debug_reg_state *state)
@@ -162,6 +56,8 @@ i386_low_init_dregs (struct i386_debug_reg_state *state)
     }
   state->dr_control_mirror = 0;
   state->dr_status_mirror  = 0;
+
+  i386_set_debug_register_length (sizeof (void *));
 }
 
 /* Print the values of the mirrored debug registers.  This is enabled via
diff --git a/gdb/gdbserver/i386-low.h b/gdb/gdbserver/i386-low.h
index bbfbc10..97b3464 100644
--- a/gdb/gdbserver/i386-low.h
+++ b/gdb/gdbserver/i386-low.h
@@ -29,29 +29,12 @@
    counts, and allow to watch regions up to 16 bytes long
    (32 bytes on 64 bit hosts).  */
 
+#include "i386-dbg-reg.h"
 
 /* Debug registers' indices.  */
 #define DR_FIRSTADDR 0
 #define DR_LASTADDR  3
-#define DR_NADDR     4 /* The number of debug address registers.  */
-#define DR_STATUS    6
-#define DR_CONTROL   7
-
-/* Global state needed to track h/w watchpoints.  */
-
-struct i386_debug_reg_state
-{
-  /* Mirror the inferior's DRi registers.  We keep the status and
-     control registers separated because they don't hold addresses.
-     Note that since we can change these mirrors while threads are
-     running, we never trust them to explain a cause of a trap.
-     For that, we need to peek directly in the inferior registers.  */
-  CORE_ADDR dr_mirror[DR_NADDR];
-  unsigned dr_status_mirror, dr_control_mirror;
-
-  /* Reference counts for each debug register.  */
-  int dr_ref_count[DR_NADDR];
-};
+
 
 /* Initialize STATE.  */
 extern void i386_low_init_dregs (struct i386_debug_reg_state *state);
diff --git a/gdb/i386-nat.c b/gdb/i386-nat.c
index 7e6814e..50b9ee6 100644
--- a/gdb/i386-nat.c
+++ b/gdb/i386-nat.c
@@ -39,127 +39,13 @@
 
 struct i386_dr_low_type i386_dr_low;
 
-
-/* Support for 8-byte wide hw watchpoints.  */
-#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 DR_STATUS	6	/* Index of debug status register (DR6).  */
-#define DR_CONTROL	7	/* Index of debug control register (DR7).  */
-
-/* DR7 Debug Control register fields.  */
-
-/* How many bits to skip in DR7 to get to R/W and LEN fields.  */
-#define DR_CONTROL_SHIFT	16
-/* How many bits in DR7 per R/W and LEN field for each watchpoint.  */
-#define DR_CONTROL_SIZE		4
-
-/* Watchpoint/breakpoint read/write fields in DR7.  */
-#define DR_RW_EXECUTE	(0x0)	/* Break on instruction execution.  */
-#define DR_RW_WRITE	(0x1)	/* Break on data writes.  */
-#define DR_RW_READ	(0x3)	/* Break on data reads or writes.  */
-
-/* This is here for completeness.  No platform supports this
-   functionality yet (as of March 2001).  Note that the DE flag in the
-   CR4 register needs to be set to support this.  */
-#ifndef DR_RW_IORW
-#define DR_RW_IORW	(0x2)	/* Break on I/O reads or writes.  */
-#endif
-
-/* Watchpoint/breakpoint length fields in DR7.  The 2-bit left shift
-   is so we could OR this with the read/write field defined above.  */
-#define DR_LEN_1	(0x0 << 2) /* 1-byte region watch or breakpoint.  */
-#define DR_LEN_2	(0x1 << 2) /* 2-byte region watch.  */
-#define DR_LEN_4	(0x3 << 2) /* 4-byte region watch.  */
-#define DR_LEN_8	(0x2 << 2) /* 8-byte region watch (AMD64).  */
-
-/* Local and Global Enable flags in DR7.
-
-   When the Local Enable flag is set, the breakpoint/watchpoint is
-   enabled only for the current task; the processor automatically
-   clears this flag on every task switch.  When the Global Enable flag
-   is set, the breakpoint/watchpoint is enabled for all tasks; the
-   processor never clears this flag.
-
-   Currently, all watchpoint are locally enabled.  If you need to
-   enable them globally, read the comment which pertains to this in
-   i386_insert_aligned_watchpoint below.  */
-#define DR_LOCAL_ENABLE_SHIFT	0 /* Extra shift to the local enable bit.  */
-#define DR_GLOBAL_ENABLE_SHIFT	1 /* Extra shift to the global enable bit.  */
-#define DR_ENABLE_SIZE		2 /* Two enable bits per debug register.  */
-
-/* Local and global exact breakpoint enable flags (a.k.a. slowdown
-   flags).  These are only required on i386, to allow detection of the
-   exact instruction which caused a watchpoint to break; i486 and
-   later processors do that automatically.  We set these flags for
-   backwards compatibility.  */
-#define DR_LOCAL_SLOWDOWN	(0x100)
-#define DR_GLOBAL_SLOWDOWN     	(0x200)
-
-/* Fields reserved by Intel.  This includes the GD (General Detect
-   Enable) flag, which causes a debug exception to be generated when a
-   MOV instruction accesses one of the debug registers.
-
-   FIXME: My Intel manual says we should use 0xF800, not 0xFC00.  */
-#define DR_CONTROL_RESERVED	(0xFC00)
-
-/* Auxiliary helper macros.  */
-
-/* A value that masks all fields in DR7 that are reserved by Intel.  */
-#define I386_DR_CONTROL_MASK	(~DR_CONTROL_RESERVED)
-
-/* The I'th debug register is vacant if its Local and Global Enable
-   bits are reset in the Debug Control register.  */
-#define I386_DR_VACANT(i) \
-  ((dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
-
-/* Locally enable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_LOCAL_ENABLE(i) \
-  dr_control_mirror |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))
-
-/* Globally enable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_GLOBAL_ENABLE(i) \
-  dr_control_mirror |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))
-
-/* Disable the break/watchpoint in the I'th debug register.  */
-#define I386_DR_DISABLE(i) \
-  dr_control_mirror &= ~(3 << (DR_ENABLE_SIZE * (i)))
-
-/* Set in DR7 the RW and LEN fields for the I'th debug register.  */
-#define I386_DR_SET_RW_LEN(i,rwlen) \
-  do { \
-    dr_control_mirror &= ~(0x0f << (DR_CONTROL_SHIFT+DR_CONTROL_SIZE*(i)));   \
-    dr_control_mirror |= ((rwlen) << (DR_CONTROL_SHIFT+DR_CONTROL_SIZE*(i))); \
-  } while (0)
-
-/* Get from DR7 the RW and LEN fields for the I'th debug register.  */
-#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 & 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 long dr_status_mirror, dr_control_mirror;
-
-/* Reference counts for each debug register.  */
-static int dr_ref_count[DR_NADDR];
+static struct i386_debug_reg_state state;
 
 /* Whether or not to print the mirrored debug registers.  */
 static int maint_show_dr;
 
-/* Types of operations supported by i386_handle_nonaligned_watchpoint.  */
-typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t;
-
 /* Internal functions.  */
 
 /* Return the value of a 4-bit field for DR7 suitable for watching a
@@ -172,7 +58,8 @@ static unsigned i386_length_and_rw_bits (int len, enum target_hw_bp_type type);
    value of the bit-field from DR7 which describes the length and
    access type of the region to be watched by this watchpoint.  Return
    0 on success, -1 on failure.  */
-static int i386_insert_aligned_watchpoint (CORE_ADDR addr,
+static int i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state,
+					   CORE_ADDR addr,
 					   unsigned len_rw_bits);
 
 /* Remove a watchpoint at address ADDR, which is assumed to be aligned
@@ -180,7 +67,8 @@ static int i386_insert_aligned_watchpoint (CORE_ADDR addr,
    value of the bits from DR7 which describes the length and access
    type of the region watched by this watchpoint.  Return 0 on
    success, -1 on failure.  */
-static int i386_remove_aligned_watchpoint (CORE_ADDR addr,
+static int i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state,
+					   CORE_ADDR addr,
 					   unsigned len_rw_bits);
 
 /* Insert or remove a (possibly non-aligned) watchpoint, or count the
@@ -189,7 +77,8 @@ static int i386_remove_aligned_watchpoint (CORE_ADDR addr,
    successful insertion or removal, a positive number when queried
    about the number of registers, or -1 on failure.  If WHAT is not a
    valid value, bombs through internal_error.  */
-static int i386_handle_nonaligned_watchpoint (i386_wp_op_t what,
+static int i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state,
+					      i386_wp_op_t what,
 					      CORE_ADDR addr, int len,
 					      enum target_hw_bp_type type);
 
@@ -205,11 +94,11 @@ i386_cleanup_dregs (void)
 
   ALL_DEBUG_REGISTERS(i)
     {
-      dr_mirror[i] = 0;
-      dr_ref_count[i] = 0;
+      state.dr_mirror[i] = 0;
+      state.dr_ref_count[i] = 0;
     }
-  dr_control_mirror = 0;
-  dr_status_mirror  = 0;
+  state.dr_control_mirror = 0;
+  state.dr_status_mirror  = 0;
 }
 
 /* Print the values of the mirrored debug registers.  This is called
@@ -217,8 +106,8 @@ i386_cleanup_dregs (void)
    show-debug-regs" at GDB's prompt.  */
 
 static void
-i386_show_dr (const char *func, CORE_ADDR addr,
-	      int len, enum target_hw_bp_type type)
+i386_show_dr (struct i386_debug_reg_state *state, 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;
@@ -239,13 +128,16 @@ i386_show_dr (const char *func, CORE_ADDR addr,
 				: "??unknown??"))));
   puts_unfiltered (":\n");
   printf_unfiltered ("\tCONTROL (DR7): %s          STATUS (DR6): %s\n",
-		     phex (dr_control_mirror, 8), phex (dr_status_mirror, 8));
+		     phex (state->dr_control_mirror, 8),
+		     phex (state->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, 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, phex (state->dr_mirror[i], addr_size),
+			 state->dr_ref_count[i], i+1,
+			 phex (state->dr_mirror[i+1], addr_size),
+			 state->dr_ref_count[i+1]);
       i++;
     }
 }
@@ -311,7 +203,8 @@ Invalid hardware breakpoint length %d in i386_length_and_rw_bits.\n"), len);
    success, -1 on failure.  */
 
 static int
-i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
+i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state,
+				CORE_ADDR addr, unsigned len_rw_bits)
 {
   int i;
 
@@ -323,11 +216,11 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
      reuse it for this watchpoint as well (and save a register).  */
   ALL_DEBUG_REGISTERS(i)
     {
-      if (!I386_DR_VACANT (i)
-	  && dr_mirror[i] == addr
-	  && I386_DR_GET_RW_LEN (i) == len_rw_bits)
+      if (!I386_DR_VACANT (state, i)
+	  && state->dr_mirror[i] == addr
+	  && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
 	{
-	  dr_ref_count[i]++;
+	  state->dr_ref_count[i]++;
 	  return 0;
 	}
     }
@@ -335,7 +228,7 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
   /* Next, look for a vacant debug register.  */
   ALL_DEBUG_REGISTERS(i)
     {
-      if (I386_DR_VACANT (i))
+      if (I386_DR_VACANT (state, i))
 	break;
     }
 
@@ -346,9 +239,9 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
   /* Now set up the register I to watch our region.  */
 
   /* Record the info in our local mirrored array.  */
-  dr_mirror[i] = addr;
-  dr_ref_count[i] = 1;
-  I386_DR_SET_RW_LEN (i, len_rw_bits);
+  state->dr_mirror[i] = addr;
+  state->dr_ref_count[i] = 1;
+  I386_DR_SET_RW_LEN (state, i, len_rw_bits);
   /* Note: we only enable the watchpoint locally, i.e. in the current
      task.  Currently, no i386 target allows or supports global
      watchpoints; however, if any target would want that in the
@@ -356,17 +249,17 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
      to enable watchpoints globally or locally, and the code below
      should use global or local enable and slow-down flags as
      appropriate.  */
-  I386_DR_LOCAL_ENABLE (i);
-  dr_control_mirror |= DR_LOCAL_SLOWDOWN;
-  dr_control_mirror &= I386_DR_CONTROL_MASK;
+  I386_DR_LOCAL_ENABLE (state, i);
+  state->dr_control_mirror |= DR_LOCAL_SLOWDOWN;
+  state->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_control (state->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));
+    i386_dr_low.unset_status ((1<<i));
 
   return 0;
 }
@@ -378,23 +271,24 @@ i386_insert_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
    success, -1 on failure.  */
 
 static int
-i386_remove_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
+i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state,
+				CORE_ADDR addr, unsigned len_rw_bits)
 {
   int i, retval = -1;
 
   ALL_DEBUG_REGISTERS(i)
     {
-      if (!I386_DR_VACANT (i)
-	  && dr_mirror[i] == addr
-	  && I386_DR_GET_RW_LEN (i) == len_rw_bits)
+      if (!I386_DR_VACANT (state, i)
+	  && state->dr_mirror[i] == addr
+	  && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
 	{
-	  if (--dr_ref_count[i] == 0) /* no longer in use?  */
+	  if (--state->dr_ref_count[i] == 0) /* no longer in use?  */
 	    {
 	      /* Reset our mirror.  */
-	      dr_mirror[i] = 0;
-	      I386_DR_DISABLE (i);
+	      state->dr_mirror[i] = 0;
+	      I386_DR_DISABLE (state, i);
 	      /* Reset it in the inferior.  */
-	      i386_dr_low.set_control (dr_control_mirror);
+	      i386_dr_low.set_control (state->dr_control_mirror);
 	      if (i386_dr_low.reset_addr)
 		i386_dr_low.reset_addr (i);
 	    }
@@ -413,7 +307,8 @@ i386_remove_aligned_watchpoint (CORE_ADDR addr, unsigned len_rw_bits)
    valid value, bombs through internal_error.  */
 
 static int
-i386_handle_nonaligned_watchpoint (i386_wp_op_t what, CORE_ADDR addr, int len,
+i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state,
+				   i386_wp_op_t what, CORE_ADDR addr, int len,
 				   enum target_hw_bp_type type)
 {
   int retval = 0, status = 0;
@@ -454,9 +349,9 @@ i386_handle_nonaligned_watchpoint (i386_wp_op_t what, CORE_ADDR addr, int len,
 	  unsigned len_rw = i386_length_and_rw_bits (size, type);
 
 	  if (what == WP_INSERT)
-	    status = i386_insert_aligned_watchpoint (addr, len_rw);
+	    status = i386_insert_aligned_watchpoint (state, addr, len_rw);
 	  else if (what == WP_REMOVE)
-	    status = i386_remove_aligned_watchpoint (addr, len_rw);
+	    status = i386_remove_aligned_watchpoint (state, addr, len_rw);
 	  else
 	    internal_error (__FILE__, __LINE__, _("\
 Invalid value %d of operation in i386_handle_nonaligned_watchpoint.\n"),
@@ -496,16 +391,17 @@ i386_insert_watchpoint (CORE_ADDR addr, int len, int type,
 
   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);
+    retval = i386_handle_nonaligned_watchpoint (&state, WP_INSERT, addr, len,
+						type);
   else
     {
       unsigned len_rw = i386_length_and_rw_bits (len, type);
 
-      retval = i386_insert_aligned_watchpoint (addr, len_rw);
+      retval = i386_insert_aligned_watchpoint (&state, addr, len_rw);
     }
 
   if (maint_show_dr)
-    i386_show_dr ("insert_watchpoint", addr, len, type);
+    i386_show_dr (&state, "insert_watchpoint", addr, len, type);
 
   return retval;
 }
@@ -521,16 +417,17 @@ i386_remove_watchpoint (CORE_ADDR addr, int len, int type,
 
   if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8))
       || addr % len != 0)
-    retval = i386_handle_nonaligned_watchpoint (WP_REMOVE, addr, len, type);
+    retval = i386_handle_nonaligned_watchpoint (&state, WP_REMOVE, addr, len,
+						type);
   else
     {
       unsigned len_rw = i386_length_and_rw_bits (len, type);
 
-      retval = i386_remove_aligned_watchpoint (addr, len_rw);
+      retval = i386_remove_aligned_watchpoint (&state, addr, len_rw);
     }
 
   if (maint_show_dr)
-    i386_show_dr ("remove_watchpoint", addr, len, type);
+    i386_show_dr (&state, "remove_watchpoint", addr, len, type);
 
   return retval;
 }
@@ -545,7 +442,8 @@ i386_region_ok_for_watchpoint (CORE_ADDR addr, int len)
 
   /* Compute how many aligned watchpoints we would need to cover this
      region.  */
-  nregs = i386_handle_nonaligned_watchpoint (WP_COUNT, addr, len, hw_write);
+  nregs = i386_handle_nonaligned_watchpoint (&state, WP_COUNT, addr, len,
+					     hw_write);
   return nregs <= DR_NADDR ? 1 : 0;
 }
 
@@ -560,29 +458,29 @@ i386_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
   int i;
   int rc = 0;
 
-  dr_status_mirror = i386_dr_low.get_status ();
+  state.dr_status_mirror = i386_dr_low.get_status ();
 
   ALL_DEBUG_REGISTERS(i)
     {
-      if (I386_DR_WATCH_HIT (i)
+      if (I386_DR_WATCH_HIT (state.dr_status_mirror, i)
 	  /* This second condition makes sure DRi is set up for a data
 	     watchpoint, not a hardware breakpoint.  The reason is
 	     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 (state.dr_control_mirror, i) != 0
 	  /* This third condition makes sure DRi is not vacant, this
 	     avoids false positives in windows-nat.c.  */
-	  && !I386_DR_VACANT (i))
+	  && !I386_DR_VACANT (&state, i))
 	{
-	  addr = dr_mirror[i];
+	  addr = state.dr_mirror[i];
 	  rc = 1;
 	  if (maint_show_dr)
-	    i386_show_dr ("watchpoint_hit", addr, -1, hw_write);
+	    i386_show_dr (&state, "watchpoint_hit", addr, -1, hw_write);
 	}
     }
   if (maint_show_dr && addr == 0)
-    i386_show_dr ("stopped_data_addr", 0, 0, hw_write);
+    i386_show_dr (&state, "stopped_data_addr", 0, 0, hw_write);
 
   if (rc)
     *addr_p = addr;
@@ -604,10 +502,11 @@ i386_insert_hw_breakpoint (struct gdbarch *gdbarch,
 {
   unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
   CORE_ADDR addr = bp_tgt->placed_address;
-  int retval = i386_insert_aligned_watchpoint (addr, len_rw) ? EBUSY : 0;
+  int retval = i386_insert_aligned_watchpoint (&state, addr, len_rw)
+    ? EBUSY : 0;
 
   if (maint_show_dr)
-    i386_show_dr ("insert_hwbp", addr, 1, hw_execute);
+    i386_show_dr (&state, "insert_hwbp", addr, 1, hw_execute);
 
   return retval;
 }
@@ -621,10 +520,10 @@ i386_remove_hw_breakpoint (struct gdbarch *gdbarch,
 {
   unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
   CORE_ADDR addr = bp_tgt->placed_address;
-  int retval = i386_remove_aligned_watchpoint (addr, len_rw);
+  int retval = i386_remove_aligned_watchpoint (&state, addr, len_rw);
 
   if (maint_show_dr)
-    i386_show_dr ("remove_hwbp", addr, 1, hw_execute);
+    i386_show_dr (&state, "remove_hwbp", addr, 1, hw_execute);
 
   return retval;
 }
diff --git a/gdb/i386-nat.h b/gdb/i386-nat.h
index 819c6b8..c990229 100644
--- a/gdb/i386-nat.h
+++ b/gdb/i386-nat.h
@@ -21,6 +21,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "i386-dbg-reg.h"
 
 #ifndef I386_NAT_H
 #define I386_NAT_H 1
@@ -33,50 +34,6 @@
 struct target_ops;
 extern void i386_use_watchpoints (struct target_ops *);
 
-/* Support for hardware watchpoints and breakpoints using the i386
-   debug registers.
-
-   This provides several functions for inserting and removing
-   hardware-assisted breakpoints and watchpoints, testing if one or
-   more of the watchpoints triggered and at what address, checking
-   whether a given region can be watched, etc.
-
-   In addition, each target should provide several low-level functions
-   regrouped into i386_dr_low_type struct below.  These functions
-   that will be called to insert watchpoints and hardware breakpoints
-   into the inferior, remove them, and check their status.  These
-   functions are:
-
-      set_control              -- set the debug control (DR7)
-				  register to a given value for all LWPs
-
-      set_addr                 -- put an address into one debug
-				  register for all LWPs
-
-      reset_addr               -- reset the address stored in
-				  one debug register for all LWPs
-
-      get_status               -- return the value of the debug
-				  status (DR6) register for current LWP
-
-      unset_status             -- unset the specified bits of the debug
-				  status (DR6) register for all LWPs
-
-   Additionally, the native file should set the debug_register_length
-   field to 4 or 8 depending on the number of bytes used for
-   deubg registers.  */
-
-struct i386_dr_low_type 
-  {
-    void (*set_control) (unsigned long);
-    void (*set_addr) (int, CORE_ADDR);
-    void (*reset_addr) (int);
-    unsigned long (*get_status) (void);
-    void (*unset_status) (unsigned long);
-    int debug_register_length;
-  };
-
-extern struct i386_dr_low_type i386_dr_low;
 
 /* Use this function to set i386_dr_low debug_register_length field
    rather than setting it directly to check that the length is only

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-03-11  6:39 ` [try 2nd, patch] Move common macros to i386-dbg-reg.h Yao Qi
@ 2011-03-29  7:54   ` Yao Qi
  2011-04-07 14:07     ` Yao Qi
  0 siblings, 1 reply; 19+ messages in thread
From: Yao Qi @ 2011-03-29  7:54 UTC (permalink / raw)
  To: gdb-patches

On 03/11/2011 01:05 PM, Yao Qi wrote:
> Compared with last version, two changes are in this new patch,
>  1) rename i386-common.h to i386-dbg-reg.h,
>  2) add dependency tracking in gdbserver.
> 
> OK for mainline after 7.3 branch is created?
> 
> gdb/
> 
> 	* i386-nat.h: Include i386-dbg-reg.h.
> 	Move macros to i386-dbg-reg.h.
> 	* i386-nat.c: Move macros and structs to i386-dbg-reg.h.
> 	New variable struct i386_debug_reg_state state to replace
> 	other variables dr_mirror, dr_ref_count, dr_control_mirror,
> 	and dr_status_mirror.
> 	(i386_insert_aligned_watchpoint): Add one formal parameter
> 	struct i386_debug_reg_state *STATE.
> 	Update code using these variables.	
> 	(i386_remove_aligned_watchpoint, i386_show_dr): Likewise.
> 	(i386_cleanup_dregs): Likewise.
> 	(i386_handle_nonaligned_watchpoint): Likewise.
> 	* common/i386-dbg-reg.h: New.  Common macros and structs.
> 
> gdb/gdbserver/
> 
> 	* i386-low.h: Include i386-dbg-reg.h.
> 	Move macros to i386-dbg-reg.h.
> 	* i386-low.c (i386_set_debug_register_length): New.
> 	(i386_low_init_dregs): Call i386_set_debug_register_length.
> 	* Makefile.in (i386_low_h) Depends on i386-dbg-reg.h.

Ping.

-- 
Yao (齐尧)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-03-29  7:54   ` Yao Qi
@ 2011-04-07 14:07     ` Yao Qi
  2011-04-07 15:54       ` Mark Kettenis
  0 siblings, 1 reply; 19+ messages in thread
From: Yao Qi @ 2011-04-07 14:07 UTC (permalink / raw)
  To: gdb-patches

On 03/29/2011 01:02 PM, Yao Qi wrote:
> On 03/11/2011 01:05 PM, Yao Qi wrote:
>> Compared with last version, two changes are in this new patch,
>>  1) rename i386-common.h to i386-dbg-reg.h,
>>  2) add dependency tracking in gdbserver.
>>
>> OK for mainline after 7.3 branch is created?
>>
>> gdb/
>>
>> 	* i386-nat.h: Include i386-dbg-reg.h.
>> 	Move macros to i386-dbg-reg.h.
>> 	* i386-nat.c: Move macros and structs to i386-dbg-reg.h.
>> 	New variable struct i386_debug_reg_state state to replace
>> 	other variables dr_mirror, dr_ref_count, dr_control_mirror,
>> 	and dr_status_mirror.
>> 	(i386_insert_aligned_watchpoint): Add one formal parameter
>> 	struct i386_debug_reg_state *STATE.
>> 	Update code using these variables.	
>> 	(i386_remove_aligned_watchpoint, i386_show_dr): Likewise.
>> 	(i386_cleanup_dregs): Likewise.
>> 	(i386_handle_nonaligned_watchpoint): Likewise.
>> 	* common/i386-dbg-reg.h: New.  Common macros and structs.
>>
>> gdb/gdbserver/
>>
>> 	* i386-low.h: Include i386-dbg-reg.h.
>> 	Move macros to i386-dbg-reg.h.
>> 	* i386-low.c (i386_set_debug_register_length): New.
>> 	(i386_low_init_dregs): Call i386_set_debug_register_length.
>> 	* Makefile.in (i386_low_h) Depends on i386-dbg-reg.h.
> 
> Ping.
> 

Ping?

-- 
Yao (齐尧)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-04-07 14:07     ` Yao Qi
@ 2011-04-07 15:54       ` Mark Kettenis
  2011-04-11  2:01         ` Yao Qi
  0 siblings, 1 reply; 19+ messages in thread
From: Mark Kettenis @ 2011-04-07 15:54 UTC (permalink / raw)
  To: yao; +Cc: gdb-patches

> Date: Thu, 07 Apr 2011 22:07:15 +0800
> From: Yao Qi <yao@codesourcery.com>
> 
> On 03/29/2011 01:02 PM, Yao Qi wrote:
> > On 03/11/2011 01:05 PM, Yao Qi wrote:
> >> Compared with last version, two changes are in this new patch,
> >>  1) rename i386-common.h to i386-dbg-reg.h,
> >>  2) add dependency tracking in gdbserver.
> >>
> >> OK for mainline after 7.3 branch is created?
> >>
> >> gdb/
> >>
> >> 	* i386-nat.h: Include i386-dbg-reg.h.
> >> 	Move macros to i386-dbg-reg.h.
> >> 	* i386-nat.c: Move macros and structs to i386-dbg-reg.h.
> >> 	New variable struct i386_debug_reg_state state to replace
> >> 	other variables dr_mirror, dr_ref_count, dr_control_mirror,
> >> 	and dr_status_mirror.
> >> 	(i386_insert_aligned_watchpoint): Add one formal parameter
> >> 	struct i386_debug_reg_state *STATE.
> >> 	Update code using these variables.	
> >> 	(i386_remove_aligned_watchpoint, i386_show_dr): Likewise.
> >> 	(i386_cleanup_dregs): Likewise.
> >> 	(i386_handle_nonaligned_watchpoint): Likewise.
> >> 	* common/i386-dbg-reg.h: New.  Common macros and structs.
> >>
> >> gdb/gdbserver/
> >>
> >> 	* i386-low.h: Include i386-dbg-reg.h.
> >> 	Move macros to i386-dbg-reg.h.
> >> 	* i386-low.c (i386_set_debug_register_length): New.
> >> 	(i386_low_init_dregs): Call i386_set_debug_register_length.
> >> 	* Makefile.in (i386_low_h) Depends on i386-dbg-reg.h.
> > 
> > Ping.
> > 
> 
> Ping?

Sorry, but as long as there is no clear vision of what gdbsever should
be (a fully featured native GDB replacement or a lightweight remote
protocol stub) and how to handle the configure mess, I'm hesitant to
review diffs.

Also, posting pings without a pointer to the actual diff isn't helping
either.


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-04-07 15:54       ` Mark Kettenis
@ 2011-04-11  2:01         ` Yao Qi
  2011-04-13 17:05           ` Joel Brobecker
  0 siblings, 1 reply; 19+ messages in thread
From: Yao Qi @ 2011-04-11  2:01 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: gdb-patches

On 04/07/2011 11:53 PM, Mark Kettenis wrote:
>> Date: Thu, 07 Apr 2011 22:07:15 +0800
>> From: Yao Qi <yao@codesourcery.com>
>>
>> On 03/29/2011 01:02 PM, Yao Qi wrote:
>>> On 03/11/2011 01:05 PM, Yao Qi wrote:
>>>> Compared with last version, two changes are in this new patch,
>>>>  1) rename i386-common.h to i386-dbg-reg.h,
>>>>  2) add dependency tracking in gdbserver.
>>>>
>>>> OK for mainline after 7.3 branch is created?
>>>>
>>>> gdb/
>>>>
>>>> 	* i386-nat.h: Include i386-dbg-reg.h.
>>>> 	Move macros to i386-dbg-reg.h.
>>>> 	* i386-nat.c: Move macros and structs to i386-dbg-reg.h.
>>>> 	New variable struct i386_debug_reg_state state to replace
>>>> 	other variables dr_mirror, dr_ref_count, dr_control_mirror,
>>>> 	and dr_status_mirror.
>>>> 	(i386_insert_aligned_watchpoint): Add one formal parameter
>>>> 	struct i386_debug_reg_state *STATE.
>>>> 	Update code using these variables.	
>>>> 	(i386_remove_aligned_watchpoint, i386_show_dr): Likewise.
>>>> 	(i386_cleanup_dregs): Likewise.
>>>> 	(i386_handle_nonaligned_watchpoint): Likewise.
>>>> 	* common/i386-dbg-reg.h: New.  Common macros and structs.
>>>>
>>>> gdb/gdbserver/
>>>>
>>>> 	* i386-low.h: Include i386-dbg-reg.h.
>>>> 	Move macros to i386-dbg-reg.h.
>>>> 	* i386-low.c (i386_set_debug_register_length): New.
>>>> 	(i386_low_init_dregs): Call i386_set_debug_register_length.
>>>> 	* Makefile.in (i386_low_h) Depends on i386-dbg-reg.h.
>>>
>>> Ping.
>>>
>>
>> Ping?
> 
> Sorry, but as long as there is no clear vision of what gdbsever should
> be (a fully featured native GDB replacement or a lightweight remote
> protocol stub) and how to handle the configure mess, I'm hesitant to
> review diffs.

Mark,
"a fully featured native GDB replacement or a lightweight remote
protocol stub" is *not* related to this patch at all.  I am unable to do
such choice.  This patch (and other patches of mine in this area) is to
reduce source code duplication as much as possible.  No matter what
model we choose for gdbserver, this patch still makes sense, IMO.

configure problem has been fixed for some weeks by reverting my
configure/make stuff in common/ dir.

[revert patch] Remove configure/make in common/
http://sourceware.org/ml/gdb-patches/2011-03/msg00521.html

> 
> Also, posting pings without a pointer to the actual diff isn't helping
> either.

http://sourceware.org/ml/gdb-patches/2011-03/msg00648.html

-- 
Yao (齐尧)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-04-11  2:01         ` Yao Qi
@ 2011-04-13 17:05           ` Joel Brobecker
  2011-04-13 18:48             ` Jan Kratochvil
                               ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Joel Brobecker @ 2011-04-13 17:05 UTC (permalink / raw)
  To: Yao Qi; +Cc: Mark Kettenis, gdb-patches

> "a fully featured native GDB replacement or a lightweight remote
> protocol stub" is *not* related to this patch at all.  I am unable to do
> such choice.  This patch (and other patches of mine in this area) is to
> reduce source code duplication as much as possible.  No matter what
> model we choose for gdbserver, this patch still makes sense, IMO.

It does, but before we do so, I think it's important to know how
we are going to reduce this duplication. I haven't looked at the patch,
so I can't comment on it, but I think we just need a plan of what and
how we're going to avoid that.

-- 
Joel


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-04-13 17:05           ` Joel Brobecker
@ 2011-04-13 18:48             ` Jan Kratochvil
  2011-04-14  8:05             ` Yao Qi
  2011-04-25 11:03             ` Yao Qi
  2 siblings, 0 replies; 19+ messages in thread
From: Jan Kratochvil @ 2011-04-13 18:48 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: Yao Qi, Mark Kettenis, gdb-patches

On Wed, 13 Apr 2011 19:05:00 +0200, Joel Brobecker wrote:
> It does, but before we do so, I think it's important to know how
> we are going to reduce this duplication. I haven't looked at the patch,
> so I can't comment on it, but I think we just need a plan of what and
> how we're going to avoid that.

So far I try (not too actively as I have to maintain primarily linux-nat) and
at least prefer the plan of Pedro as he posted it in:
	Re: [0/9]#2 Fix lost siginfo_t
	http://sourceware.org/ml/gdb-patches/2010-08/msg00544.html

Therefore so far the plan seems clear to me, unless anyone disagrees.

The Yao's patches seem to support this general direction.



Thanks,
Jan


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-04-13 17:05           ` Joel Brobecker
  2011-04-13 18:48             ` Jan Kratochvil
@ 2011-04-14  8:05             ` Yao Qi
  2011-04-25 11:03             ` Yao Qi
  2 siblings, 0 replies; 19+ messages in thread
From: Yao Qi @ 2011-04-14  8:05 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: Mark Kettenis, gdb-patches

On 04/14/2011 01:05 AM, Joel Brobecker wrote:
>> "a fully featured native GDB replacement or a lightweight remote
>> protocol stub" is *not* related to this patch at all.  I am unable to do
>> such choice.  This patch (and other patches of mine in this area) is to
>> reduce source code duplication as much as possible.  No matter what
>> model we choose for gdbserver, this patch still makes sense, IMO.
> 
> It does, but before we do so, I think it's important to know how
> we are going to reduce this duplication. I haven't looked at the patch,

In general, the "how" is reading the source code, and identifying the
duplication between gdb and gdbserver.  Once duplication is found, try
to move them to common/ dir.  Removing duplication in *-nat.c and
*-low.c is a low-hang-fruit at that moment, and much duplications are
from this.

There are also some duplications in proc_service, and we can fix them
without much effort.

Of course, regcache and event-loop may have some duplications, but my
first impression is that it is not easy to reduce duplications there.
That is the same case to tracepoint.c as well.  I am not familiar with
them, so have no idea.

> so I can't comment on it, but I think we just need a plan of what and
> how we're going to avoid that.
> 

I guess your last "that" means "duplication".  I was keeping my eyes on
how to _reduce_ duplication, instead of how to avoid.  Think of it
shortly, something comes up in my mind,
1.  When adding a new port, common code, such as macros, register name,
and accessing method can be moved to common/, and gdb/gdbserver code use
it respectively.
2.  When adding a new functionality/feature, evaluate whether this new
feature can be done in both gdb and gdbserver.  If yes, move common part
to common/ dir, otherwise, implement this new feature in either gdb or
gdbserver.

These two rules apply to most of development works, and should be
helpful on avoiding duplications to some extent.

Finally, I don't think duplication can be avoided completely, so we need
change/refactor code periodically or continuously, even sometimes
changes are painful. :)

-- 
Yao (齐尧)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [try 2nd, patch] Move common macros to i386-dbg-reg.h
  2011-04-13 17:05           ` Joel Brobecker
  2011-04-13 18:48             ` Jan Kratochvil
  2011-04-14  8:05             ` Yao Qi
@ 2011-04-25 11:03             ` Yao Qi
  2 siblings, 0 replies; 19+ messages in thread
From: Yao Qi @ 2011-04-25 11:03 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: Mark Kettenis, gdb-patches

On 04/14/2011 01:05 AM, Joel Brobecker wrote:
>> "a fully featured native GDB replacement or a lightweight remote
>> protocol stub" is *not* related to this patch at all.  I am unable to do
>> such choice.  This patch (and other patches of mine in this area) is to
>> reduce source code duplication as much as possible.  No matter what
>> model we choose for gdbserver, this patch still makes sense, IMO.
> 
> It does, but before we do so, I think it's important to know how
> we are going to reduce this duplication. I haven't looked at the patch,
> so I can't comment on it, but I think we just need a plan of what and
> how we're going to avoid that.
> 

How do you think of a "plan" I posted
http://sourceware.org/ml/gdb-patches/2011-04/msg00202.html

How do think of this patch?  I copy the link here for you reference,
http://sourceware.org/ml/gdb-patches/2011-03/msg00648.html

-- 
Yao (齐尧)


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2011-04-25 11:03 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-13  9:58 [patch] Move common macros to i386-common.h Yao Qi
2011-02-13 13:40 ` Mark Kettenis
2011-02-17  6:36   ` Yao Qi
2011-02-17  6:41     ` Joel Brobecker
2011-02-17 18:15       ` Jan Kratochvil
2011-02-23  5:36   ` Yao Qi
2011-02-23 21:35     ` Mark Kettenis
2011-02-24  4:32       ` Yao Qi
2011-02-24  5:11       ` Joel Brobecker
2011-02-28 18:12       ` Tom Tromey
2011-03-11  6:39 ` [try 2nd, patch] Move common macros to i386-dbg-reg.h Yao Qi
2011-03-29  7:54   ` Yao Qi
2011-04-07 14:07     ` Yao Qi
2011-04-07 15:54       ` Mark Kettenis
2011-04-11  2:01         ` Yao Qi
2011-04-13 17:05           ` Joel Brobecker
2011-04-13 18:48             ` Jan Kratochvil
2011-04-14  8:05             ` Yao Qi
2011-04-25 11:03             ` Yao Qi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox