On x86_64 fpregset_t is a pointer, so pr_fpreg was only a pointer and
no FP state got into the cores.  gdb says that .reg2 is too small and
shows the x87 and SSE registers as unavailable.

Use glibc's struct _libc_fpstate for prfpregset_t on x86_64.  It is the
512-byte FXSAVE area, like Linux's elf_fpregset_t.  Fill it from
i386_XFLOAT_STATE, whose hw_state starts with that area.
i386_FLOAT_STATE has the old FSAVE layout, without the xmm registers.

lwpstatus_t grows from 216 to 720 bytes on x86_64, so gdb has to be
built against the new header to read these cores.
---
A gdb 17.2 built against the old header segfaults on the new cores,
and a rebuilt one segfaults on old cores.  gdb 18 has af7fe6fff9 for
that crash, but I haven't tried it.

With a program that loads known values into the x87 stack, MXCSR and
the xmm registers of two threads and then faults, a gdb 17.2 with my
gregset patch, rebuilt against the new header, shows each thread's own
values from the core.  Yelninei's reproducer from
https://codeberg.org/guix/guix/issues/1041, guile -c '(raise SIGABRT)',
also gives a core with both threads and their FP registers.  This was
on Guix's Hurd 6290b4c plus d0aa274b, under KVM on a Zen 3.  I haven't
built i386 or aarch64.

I used struct _libc_fpstate rather than adding an elf_fpregset_t type
like Linux has.  Would you prefer the new type?

 exec/elfcore.c       | 39 +++++++++++++++++++++++----------------
 include/sys/procfs.h |  6 ++++++
 2 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/exec/elfcore.c b/exec/elfcore.c
index 262842ce..4c5bda51 100644
--- a/exec/elfcore.c
+++ b/exec/elfcore.c
@@ -43,6 +43,9 @@
 
 #include <mach/thread_status.h>
 #include <mach/vm_param.h>
+#ifdef __x86_64__
+# include <mach/machine/mach_i386.h>
+#endif
 #include <assert-backtrace.h>
 
 #ifdef __x86_64__
@@ -94,29 +97,33 @@ static inline void
 fetch_thread_fpregset (thread_t thread, prfpregset_t *fpregs)
 {
   error_t err;
-  struct i386_float_state st;
-  mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
+  vm_size_t size;
+  struct i386_xfloat_state *st;
+  mach_msg_type_number_t count;
 
-  if (!*fpregs)
-    /* FIXME: having a pointer here is bogus actually, prfpregset_t should be a
-       struct, not a pointer, like on i386.  This needs to be fixed in glibc
-       etc.  */
-    return;
-  memset (*fpregs, 0, sizeof (**fpregs));
-
-  err = thread_get_state (thread, i386_FLOAT_STATE,
-                                  (thread_state_t) &st, &count);
+  err = i386_get_xstate_size (mach_host_self (), &size);
   if (err)
     {
-      error (0, err, "Failed to fetch thread state");
+      error (0, err, "Failed to get the FPU state size");
       return;
     }
-  if (st.initialized)
+
+  st = alloca (size);
+  count = size / sizeof (natural_t);
+  err = thread_get_state (thread, i386_XFLOAT_STATE,
+                         (thread_state_t) st, &count);
+  if (err)
     {
-      _Static_assert (sizeof (**fpregs) >= sizeof (st.hw_state),
-                      "Expected prfpregset_t to contain hw_state");
-      memcpy (*fpregs, &st.hw_state, sizeof (st.hw_state));
+      error (0, err, "Failed to fetch thread FP state");
+      return;
     }
+
+  /* The XSAVE area starts with the 512-byte FXSAVE area.  */
+  _Static_assert (sizeof (*fpregs) == 512,
+                 "Expected prfpregset_t to be the FXSAVE area");
+  if (st->initialized
+      && count * sizeof (natural_t) >= sizeof (*st) + sizeof (*fpregs))
+    memcpy (fpregs, st->hw_state, sizeof (*fpregs));
 }
 
 #elif defined (i386_THREAD_STATE)
diff --git a/include/sys/procfs.h b/include/sys/procfs.h
index 4acc3464..5487c86b 100644
--- a/include/sys/procfs.h
+++ b/include/sys/procfs.h
@@ -96,7 +96,13 @@ struct elf_siginfo
 };
 
 typedef gregset_t prgregset_t;
+#ifdef __x86_64__
+/* fpregset_t is a pointer on x86_64.  Like Linux's elf_fpregset_t, store
+   the FXSAVE area itself.  */
+typedef struct _libc_fpstate prfpregset_t;
+#else
 typedef fpregset_t prfpregset_t;
+#endif
 
 /* This structure describes the state of one thread.  GDB examines
    pr_cursig to see what killed the process, so those are set in the
-- 
2.56.0


Reply via email to