DEFINE_NOTE puts the data of a note after the packed 20-byte header.
When the data is 8-byte aligned, as lwpstatus_t, pstatus_t, psinfo_t
and auxv_t are on x86_64, the compiler adds 4 bytes of padding in
between, and write_note wrote them to the file along with the header.
The contents of each of these notes were shifted by 4 bytes and the
last 4 bytes were cut off.  gdb then reads the same id for every thread
and fails with an internal error in get_thread_regcache.

Write the header and the data separately.  On i386 there is no padding,
so the output is the same there.

Also advance the offset after a partial write (the old loop wrote every
chunk at the same offset), and write nothing when the offset is already
past the core limit instead of computing a negative size.
---
c809367c says the core files still make gdb crash on x86_64; this is
why.  The FP state is a separate problem and is still missing (gdb says
.reg2 is too small).  With this patch gdb gets the pid, the signal and
all the threads, but it still cannot unwind past the first frame: its
amd64 GNU/Hurd gregset table has the Linux offsets instead of
gregset_t's.  I'm sending that fix to gdb-patches.

Tested on Guix System x86_64 in QEMU, Hurd 6290b4c plus this patch (its
exec/elfcore.c only differs from master in the initializer syntax of
the ELF header).  The test program calls abort, writes through NULL, or
aborts with a second thread running; static for all three, dynamic for
the first two.  Before, gdb 17.2 aborted on all five cores.  After,
with the gdb fix too, it shows full backtraces, also for a core written
by gcore.  On i586 the notes have the same sizes and the same bytes as
before, except for the pids, the start time, the CPU usage and ecx, and
gdb shows the same backtraces.  Not tested: core limits, and aarch64,
which should have the same padding.

 exec/elfcore.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/exec/elfcore.c b/exec/elfcore.c
index 4b2afdf1..1ae3df85 100644
--- a/exec/elfcore.c
+++ b/exec/elfcore.c
@@ -276,7 +276,7 @@ dump_core (task_t task, file_t file, off_t corelimit,
 #define DEFINE_NOTE(typename) struct { struct note_header hdr; typename data; }
 #define WRITE_NOTE(type, var) ({                                             \
   (var).hdr = NOTE_HEADER ((type), sizeof (var).data);                       \
-  write_note (&(var).hdr);                                                   \
+  write_note (&(var).hdr, &(var).data);                                        
      \
 })
   struct note_header
   {
@@ -285,26 +285,35 @@ dump_core (task_t task, file_t file, off_t corelimit,
   } __attribute__ ((packed));
 #define NOTE_HEADER(type, size) \
   ((struct note_header) { { sizeof "CORE", (size), (type) }, "CORE" })
-  inline error_t write_note (struct note_header *hdr)
+  /* Write SIZE bytes from DATA at OFFSET, within CORELIMIT, and advance
+     OFFSET.  */
+  inline error_t write_bytes (const void *data, size_t size)
     {
-      error_t err = 0;
-      char *data = (char *) hdr;
-      size_t size = sizeof *hdr + hdr->note.n_descsz;
+      const char *p = data;
       if (corelimit >= 0 && offset + size > corelimit)
-       size = corelimit - offset;
+       size = corelimit > offset ? corelimit - offset : 0;
       while (size > 0)
        {
-         err = io_write (file, data, size, offset, &wrote);
+         error_t err = io_write (file, p, size, offset, &wrote);
          if (err)
            return err;
          if (wrote > size)
            return EGRATUITOUS;
-         data += wrote;
+         p += wrote;
+         offset += wrote;
          size -= wrote;
        }
-      offset = (offset + wrote + 3) &~ 3; /* Pad it to word alignment.  */
       return 0;
     }
+  /* DEFINE_NOTE can have padding between the header and the data (4 bytes
+     on x86_64), which must not go into the file.  */
+  inline error_t write_note (struct note_header *hdr, const void *desc)
+    {
+      error_t err = write_bytes (hdr, sizeof *hdr)
+                   ?: write_bytes (desc, hdr->note.n_descsz);
+      offset = (offset + 3) &~ 3; /* Pad it to word alignment.  */
+      return err;
+    }
 
   struct vm_region_list
   {
-- 
2.56.0


Reply via email to