From: Hannes Domani <ssbssa@yahoo.de>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v4 4/9] Windows gdb: Refactor getting pointer to register inside context
Date: Wed, 30 Sep 2026 17:11:38 +0200 [thread overview]
Message-ID: <20260930151403.774660-4-ssbssa@yahoo.de> (raw)
In-Reply-To: <20260930151403.774660-1-ssbssa@yahoo.de>
These get_context_reg_ptr helper functions will be extended for XState
registers later.
Approved-By: Tom Tromey <tom@tromey.com>
---
v3: no changes
v4: no changes
---
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 e0e147856fb..2fb2e3abf2e 100644
--- a/gdb/windows-nat.h
+++ b/gdb/windows-nat.h
@@ -393,12 +393,18 @@ int i386_windows_segment_register_p (int regnum);
/* context register offsets for x86. */
extern const int i386_mappings[];
+/* number of context register offsets 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 offsets 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.55.0
next prev parent reply other threads:[~2026-09-30 15:17 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260930151403.774660-1-ssbssa.ref@yahoo.de>
2026-09-30 15:11 ` [PATCH v4 1/9] gdb/testsuite: Add Windows replacement for aligned_alloc Hannes Domani
2026-09-30 15:11 ` [PATCH v4 2/9] Windows gdb: Use allocated buffer for CONTEXT Hannes Domani
2026-09-30 15:11 ` [PATCH v4 3/9] Windows gdb: Remove mappings member from windows_per_inferior Hannes Domani
2026-09-30 15:11 ` Hannes Domani [this message]
2026-09-30 15:11 ` [PATCH v4 5/9] Windows gdb: Prepare XState functions Hannes Domani
2026-09-30 15:11 ` [PATCH v4 6/9] Windows gdb: Get available XState features Hannes Domani
2026-09-30 15:11 ` [PATCH v4 7/9] Windows gdb: Implement AVX register support Hannes Domani
2026-10-05 14:09 ` Rohr, Stephan
2026-10-09 14:30 ` Hannes Domani
2026-10-14 13:04 ` Joos, Christina
2026-09-30 15:11 ` [PATCH v4 8/9] Windows gdb: Implement AVX-512 " Hannes Domani
2026-10-05 14:08 ` Rohr, Stephan
2026-10-09 14:36 ` Hannes Domani
2026-10-12 7:59 ` Joos, Christina
2026-10-12 18:33 ` Hannes Domani
2026-10-26 12:28 ` Joos, Christina
2026-10-14 13:06 ` Joos, Christina
2026-09-30 15:11 ` [PATCH v4 9/9] Windows gdbserver: Remove win32_target_ops "num_regs" method Hannes Domani
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260930151403.774660-4-ssbssa@yahoo.de \
--to=ssbssa@yahoo.de \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox