[PATCH v2 4/8] Windows gdb: Refactor getting pointer to register inside context
Hannes Domani <[email protected]> Mon, 27 Jul 2026 19:42:28 +0200
| Newsgroups | gmane.comp.gdb.patches |
|---|---|
| Message-ID | <[email protected]> |
These get_context_reg_ptr helper functions will be extended for XState
registers later.
---
gdb/amd64-windows-nat.c | 3 ++
gdb/i386-windows-nat.c | 3 ++
gdb/windows-nat.h | 6 +++
gdb/x86-windows-nat.c | 75 ++++++++++++++++++++++---------------
gdbserver/win32-i386-low.cc | 46 +++++++++++++++--------
5 files changed, 87 insertions(+), 46 deletions(-)
diff --git a/gdb/amd64-windows-nat.c b/gdb/amd64-windows-nat.c
index cca606b5200..9d83a5f8334 100644
--- a/gdb/amd64-windows-nat.c
+++ b/gdb/amd64-windows-nat.c
@@ -86,6 +86,9 @@ const int amd64_mappings[] =
};
#undef context_offset
+const int amd64_mappings_count
+ = sizeof (amd64_mappings) / sizeof (amd64_mappings[0]);
+
/* segment_register_p_ftype implementation for amd64. */
int
diff --git a/gdb/i386-windows-nat.c b/gdb/i386-windows-nat.c
index 991f301cad1..151b778036c 100644
--- a/gdb/i386-windows-nat.c
+++ b/gdb/i386-windows-nat.c
@@ -74,6 +74,9 @@ const int i386_mappings[] =
#undef context_offset
#undef CONTEXT
+const int i386_mappings_count
+ = sizeof (i386_mappings) / sizeof (i386_mappings[0]);
+
/* segment_register_p_ftype implementation for x86. */
int
diff --git a/gdb/windows-nat.h b/gdb/windows-nat.h
index d6977ff4c85..69be069e419 100644
--- a/gdb/windows-nat.h
+++ b/gdb/windows-nat.h
@@ -392,12 +392,18 @@ int i386_windows_segment_register_p (int regnum);
/* context register offsets for x86. */
extern const int i386_mappings[];
+/* number of context register offests for x86. */
+extern const int i386_mappings_count;
+
#ifdef __x86_64__
/* segment_register_p_ftype implementation for amd64. */
int amd64_windows_segment_register_p (int regnum);
/* context register offsets for amd64. */
extern const int amd64_mappings[];
+
+/* number of context register offests for amd64. */
+extern const int amd64_mappings_count;
#endif
/* Creates an iterator that works like all_matching_threads_iterator,
diff --git a/gdb/x86-windows-nat.c b/gdb/x86-windows-nat.c
index b3ad8594020..3af5ef4dae0 100644
--- a/gdb/x86-windows-nat.c
+++ b/gdb/x86-windows-nat.c
@@ -223,31 +223,53 @@ x86_windows_nat_target::thread_context_step (windows_thread_info *th,
});
}
-/* See windows-nat.h. */
+/* Get pointer to register R inside CONTEXT. */
-void
-x86_windows_nat_target::fetch_one_register (struct regcache *regcache,
- windows_thread_info *th, int r)
+template<typename Context>
+static char *
+get_context_reg_ptr (Context *context, int r)
{
- gdb_assert (r >= 0);
-
- char *context_ptr = x86_windows_process.with_context (th, [] (auto *context)
- {
- return (char *) context;
- });
-
const int *mappings;
+ int mappings_count;
#ifdef __x86_64__
if (!x86_windows_process.wow64_process)
- mappings = amd64_mappings;
+ {
+ mappings = amd64_mappings;
+ mappings_count = amd64_mappings_count;
+ }
else
#endif
- mappings = i386_mappings;
+ {
+ mappings = i386_mappings;
+ mappings_count = i386_mappings_count;
+ }
+
+ char *context_offset;
+ if (r < mappings_count)
+ context_offset = (char *) context + mappings[r];
+ else
+ gdb_assert_not_reached ("invalid register number %d", r);
+
+ return context_offset;
+}
+
+/* See windows-nat.h. */
+
+void
+x86_windows_nat_target::fetch_one_register (struct regcache *regcache,
+ windows_thread_info *th, int r)
+{
+ gdb_assert (r >= 0);
- char *context_offset = context_ptr + mappings[r];
struct gdbarch *gdbarch = regcache->arch ();
i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
+ char *context_offset
+ = x86_windows_process.with_context (th, [&] (auto *context)
+ {
+ return get_context_reg_ptr (context, r);
+ });
+
gdb_assert (!gdbarch_read_pc_p (gdbarch));
gdb_assert (gdbarch_pc_regnum (gdbarch) >= 0);
gdb_assert (!gdbarch_write_pc_p (gdbarch));
@@ -304,23 +326,16 @@ x86_windows_nat_target::store_one_register (const struct regcache *regcache,
{
gdb_assert (r >= 0);
- char *context_ptr = x86_windows_process.with_context (th, [] (auto *context)
+ struct gdbarch *gdbarch = regcache->arch ();
+ i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
+
+ char *context_offset
+ = x86_windows_process.with_context (th, [&] (auto *context)
{
gdb_assert (context->ContextFlags != 0);
- return (char *) context;
+ return get_context_reg_ptr (context, r);
});
- const int *mappings;
-#ifdef __x86_64__
- if (!x86_windows_process.wow64_process)
- mappings = amd64_mappings;
- else
-#endif
- mappings = i386_mappings;
-
- struct gdbarch *gdbarch = regcache->arch ();
- i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
-
/* GDB treats some registers as 32-bit, where they are in fact only
16 bits long. These cases must be handled specially to avoid
overwriting other registers in the context. */
@@ -329,7 +344,7 @@ x86_windows_nat_target::store_one_register (const struct regcache *regcache,
{
gdb_byte bytes[4];
regcache->raw_collect (r, bytes);
- memcpy (context_ptr + mappings[r], bytes, 2);
+ memcpy (context_offset, bytes, 2);
}
else if (r == I387_FOP_REGNUM (tdep))
{
@@ -338,10 +353,10 @@ x86_windows_nat_target::store_one_register (const struct regcache *regcache,
/* The value of FOP occupies the top two bytes in the context,
so write the two low-order bytes from the cache into the
appropriate spot. */
- memcpy (context_ptr + mappings[r] + 2, bytes, 2);
+ memcpy (context_offset + 2, bytes, 2);
}
else
- regcache->raw_collect (r, context_ptr + mappings[r]);
+ regcache->raw_collect (r, context_offset);
}
/* See windows-nat.h. */
diff --git a/gdbserver/win32-i386-low.cc b/gdbserver/win32-i386-low.cc
index 1aacd302074..b77f6adc6ed 100644
--- a/gdbserver/win32-i386-low.cc
+++ b/gdbserver/win32-i386-low.cc
@@ -473,22 +473,44 @@ is_segment_register (int r)
return r >= I386_CS_REGNUM && r <= I386_GS_REGNUM;
}
-/* Fetch register from gdbserver regcache data. */
-static void
-i386_fetch_inferior_register (struct regcache *regcache,
- windows_thread_info *th, int r)
+/* Get pointer to register R inside CONTEXT. */
+
+template<typename Context>
+static char *
+get_context_reg_ptr (Context *context, int r)
{
const int *mappings;
+ int mappings_count;
#ifdef __x86_64__
if (!windows_process.wow64_process)
- mappings = amd64_mappings;
+ {
+ mappings = amd64_mappings;
+ mappings_count = sizeof (amd64_mappings) / sizeof (amd64_mappings[0]);
+ }
else
#endif
- mappings = i386_mappings;
+ {
+ mappings = i386_mappings;
+ mappings_count = sizeof (i386_mappings) / sizeof (i386_mappings[0]);
+ }
+
+ char *context_offset;
+ if (r < mappings_count)
+ context_offset = (char *) context + mappings[r];
+ else
+ gdb_assert_not_reached ("invalid register number %d", r);
+
+ return context_offset;
+}
+/* Fetch register from gdbserver regcache data. */
+static void
+i386_fetch_inferior_register (struct regcache *regcache,
+ windows_thread_info *th, int r)
+{
char *context_offset = windows_process.with_context (th, [&] (auto *context)
{
- return (char *) context + mappings[r];
+ return get_context_reg_ptr (context, r);
});
/* GDB treats some registers as 32-bit, where they are in fact only
@@ -514,17 +536,9 @@ static void
i386_store_inferior_register (struct regcache *regcache,
windows_thread_info *th, int r)
{
- const int *mappings;
-#ifdef __x86_64__
- if (!windows_process.wow64_process)
- mappings = amd64_mappings;
- else
-#endif
- mappings = i386_mappings;
-
char *context_offset = windows_process.with_context (th, [&] (auto *context)
{
- return (char *) context + mappings[r];
+ return get_context_reg_ptr (context, r);
});
/* GDB treats some registers as 32-bit, where they are in fact only
--
2.54.0