Skip to contentSkip to conversation

elf2dmp: identify faulting CPU in Windows crash dumps

PATCH Superseded Zhengrong Li 3 participants 6 messages Aug 6

lore
Share & tools
lore
#0 lore raw

v3 cover elf2dmp: identify faulting CPU in Windows crash dumps

When a Windows guest crashes and QEMU generates a memory dump via pvpanic, elf2dmp currently always sets CPU 0's context in the dump header. This makes WinDbg default to CPU 0 when opening the dump, even if the actual crash happened on a different CPU.

This series adds crash_occurred tracking from the runstate layer through to the QEMUCPUState in the ELF dump, and teaches elf2dmp to identify and use the faulting CPU's context.

Changes since v2:
- Remove unused fault_cpu variable in fill_context()
- Use per-CPU size check (s->size >= offsetof + sizeof) instead of global has_is_crash_occurred_cpu flag, fixing heterogeneous dumps where CPU notes have different sizes
- Remove has_is_crash_occurred_cpu from QEMU_Elf struct and init_states() since per-CPU check makes it unnecessary

Changes since v1:
- Set header context before best-effort guest memory operations to avoid losing faulting CPU context on partial dump failures
- Use LIVE_SYSTEM_DUMP when BugcheckCode is zero
- Use PRIx32 for BugcheckCode format specifier
- Use linux.alibaba.com identity

Zhengrong Li (3):
runstate: set crash_occurred on guest crashloaded
dump: add crash_occurred flag to QEMUCPUState
elf2dmp: fill ContextBuffer with faulting CPU context

 contrib/elf2dmp/kdbg.h     |  9 +++++++
 contrib/elf2dmp/main.c     | 50 ++++++++++++++++++++++++++++++++++++--
 contrib/elf2dmp/qemu_elf.h |  2 ++
 system/runstate.c          |  5 ++++
 target/i386/arch_dump.c    |  6 +++++
 5 files changed, 70 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/3 runstate: set crash_occurred on guest crashloaded

Windows pvpanic driver writes PVPANIC_CRASH_LOADED (bit 1) on bugcheck, not PVPANIC_PANICKED (bit 0). As a result, qemu_system_guest_panicked() is never called and crash_occurred stays false for Windows guests.

Set crash_occurred in qemu_system_guest_crashloaded() as well, so the faulting CPU can be identified in the guest dump.

---
 system/runstate.c | 5 +++++
 1 file changed, 5 insertions(+)
system/runstate.c +5 -0
index 08acf801b0..c7961cc1df 100644
@@ -724,6 +724,11 @@ void qemu_system_guest_panicked(GuestPanicInformation *info)
 void qemu_system_guest_crashloaded(GuestPanicInformation *info)
 {
     qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
+
+    if (current_cpu) {
+        current_cpu->crash_occurred = true;
+    }
+
     qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info);
     qapi_free_GuestPanicInformation(info);
 }
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/3 dump: add crash_occurred flag to QEMUCPUState

In reply to Zhengrong Li

Add is_crash_occurred_cpu field to QEMUCPUState so that dump-guest-memory can record which CPU triggered the guest panic. Bump QEMUCPUSTATE_VERSION to 2.

The new field is appended after kernel_gs_base, so existing tools that check the 'size' field can safely ignore it when reading older dumps.

---
 contrib/elf2dmp/qemu_elf.h | 2 ++
 target/i386/arch_dump.c    | 6 ++++++
 2 files changed, 8 insertions(+)
contrib/elf2dmp/qemu_elf.h +2 -0
index adc50238b4..3b28332e67 100644
@@ -27,6 +27,8 @@ typedef struct QEMUCPUState {
     QEMUCPUSegment ldt, tr, gdt, idt;
     uint64_t cr[5];
     uint64_t kernel_gs_base;
+    uint8_t  is_crash_occurred_cpu;
+    uint8_t  pad[7];
 } QEMUCPUState;
 
 int is_system(QEMUCPUState *s);
target/i386/arch_dump.c +6 -0
index 16e47c4747..8ce2c7e9bb 100644
@@ -264,6 +264,8 @@ struct QEMUCPUState {
      * by checking 'size' field.
      */
     uint64_t kernel_gs_base;
+    uint8_t  is_crash_occurred_cpu;
+    uint8_t  pad[7];
 };
 
 typedef struct QEMUCPUState QEMUCPUState;
@@ -279,6 +281,7 @@ static void copy_segment(QEMUCPUSegment *d, SegmentCache *s)
 
 static void qemu_get_cpustate(QEMUCPUState *s, CPUX86State *env)
 {
+    CPUState *cs = env_cpu(env);
     memset(s, 0, sizeof(QEMUCPUState));
 
     s->version = QEMUCPUSTATE_VERSION;
@@ -325,6 +328,9 @@ static void qemu_get_cpustate(QEMUCPUState *s, CPUX86State *env)
 #ifdef TARGET_X86_64
     s->kernel_gs_base = env->kernelgsbase;
 #endif
+    if (cs->crash_occurred) {
+        s->is_crash_occurred_cpu = 1;
+    }
 }
 
 static inline int cpu_write_qemu_note(WriteCoreDumpFunction f,
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v3 2/3] dump: add crash_occurred flag to QEMUCPUState

Quoted 3 lines · Zhengrong Li

Add is_crash_occurred_cpu field to QEMUCPUState so that dump-guest-memory can record which CPU triggered the guest panic. Bump QEMUCPUSTATE_VERSION to 2.

The patch message still says it bumps QEMUCPUSTATE_VERSION.

Quoted 16 lines

The new field is appended after kernel_gs_base, so existing tools that check the 'size' field can safely ignore it when reading older dumps.

---
  contrib/elf2dmp/qemu_elf.h | 2 ++
  target/i386/arch_dump.c    | 6 ++++++
  2 files changed, 8 insertions(+)
contrib/elf2dmp/qemu_elf.h +0 -0
index adc50238b4..3b28332e67 100644

contrib/elf2dmp/qemu_elf.h typedef struct QEMUCPUState { · line 31

Quoted 5 lines
contrib/elf2dmp/qemu_elf.h +2 -0
@@ -27,6 +27,8 @@ typedef struct QEMUCPUState {
      QEMUCPUSegment ldt, tr, gdt, idt;
      uint64_t cr[5];
      uint64_t kernel_gs_base;
+    uint8_t  is_crash_occurred_cpu;
+    uint8_t  pad[7];

Growing QEMUCPUState from 440 to 448 bytes confuses init_states() in contrib/elf2dmp/qemu_elf.c and makes valid version-1 notes and appear to lack kernel_gs_base, despite that field occupying bytes 432–439. Move this hunk to patch 2 which updates the function and other elf2dmp stuff.

Regards,
Akihiko Odaki

Quoted 34 lines
contrib/elf2dmp/qemu_elf.h +0 -0
  } QEMUCPUState;
 
  int is_system(QEMUCPUState *s);
target/i386/arch_dump.c +6 -0
index 16e47c4747..8ce2c7e9bb 100644
@@ -264,6 +264,8 @@ struct QEMUCPUState {
       * by checking 'size' field.
       */
      uint64_t kernel_gs_base;
+    uint8_t  is_crash_occurred_cpu;
+    uint8_t  pad[7];
  };
 
  typedef struct QEMUCPUState QEMUCPUState;
@@ -279,6 +281,7 @@ static void copy_segment(QEMUCPUSegment *d, SegmentCache *s)
 
  static void qemu_get_cpustate(QEMUCPUState *s, CPUX86State *env)
  {
+    CPUState *cs = env_cpu(env);
      memset(s, 0, sizeof(QEMUCPUState));
 
      s->version = QEMUCPUSTATE_VERSION;
@@ -325,6 +328,9 @@ static void qemu_get_cpustate(QEMUCPUState *s, CPUX86State *env)
  #ifdef TARGET_X86_64
      s->kernel_gs_base = env->kernelgsbase;
  #endif
+    if (cs->crash_occurred) {
+        s->is_crash_occurred_cpu = 1;
+    }
  }
 
  static inline int cpu_write_qemu_note(WriteCoreDumpFunction f,
#4 lore raw

PATCH v3 3/3 elf2dmp: fill ContextBuffer with faulting CPU context

In reply to Zhengrong Li

When is_crash_occurred_cpu is set in QEMUCPUState, copy that CPU's context into WinDumpHeader64 ContextBuffer so WinDbg opens on the faulting CPU instead of CPU 0. A per-CPU size check ensures the field is present before reading, supporting heterogeneous dumps.

Also extract KiBugCheckData from the guest kernel to populate BugcheckCode and BugcheckParameters in the dump header.

---
 contrib/elf2dmp/kdbg.h |  9 ++++++++
 contrib/elf2dmp/main.c | 50 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 57 insertions(+), 2 deletions(-)
contrib/elf2dmp/kdbg.h +9 -0
index 002e3d0cd5..15e4fb1140 100644
@@ -195,4 +195,13 @@ typedef struct KDDEBUGGER_DATA64 {
     uint16_t OffsetPrcbContext;
 } KDDEBUGGER_DATA64;
 
+typedef struct KIBUGCHECK_INFO {
+    uint32_t BugcheckCode;
+    uint32_t unused0;
+    uint64_t BugcheckParameter1;
+    uint64_t BugcheckParameter2;
+    uint64_t BugcheckParameter3;
+    uint64_t BugcheckParameter4;
+} KIBUGCHECK_INFO;
+
 #endif /* KDBG_H */
contrib/elf2dmp/main.c +48 -2

72 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v3 3/3] elf2dmp: fill ContextBuffer with faulting CPU context

Quoted 42 lines · Zhengrong Li

When is_crash_occurred_cpu is set in QEMUCPUState, copy that CPU's context into WinDumpHeader64 ContextBuffer so WinDbg opens on the faulting CPU instead of CPU 0. A per-CPU size check ensures the field is present before reading, supporting heterogeneous dumps.

Also extract KiBugCheckData from the guest kernel to populate BugcheckCode and BugcheckParameters in the dump header.

---
  contrib/elf2dmp/kdbg.h |  9 ++++++++
  contrib/elf2dmp/main.c | 50 ++++++++++++++++++++++++++++++++++++++++--
  2 files changed, 57 insertions(+), 2 deletions(-)
contrib/elf2dmp/kdbg.h +9 -0
index 002e3d0cd5..15e4fb1140 100644
@@ -195,4 +195,13 @@ typedef struct KDDEBUGGER_DATA64 {
      uint16_t OffsetPrcbContext;
  } KDDEBUGGER_DATA64;
 
+typedef struct KIBUGCHECK_INFO {
+    uint32_t BugcheckCode;
+    uint32_t unused0;
+    uint64_t BugcheckParameter1;
+    uint64_t BugcheckParameter2;
+    uint64_t BugcheckParameter3;
+    uint64_t BugcheckParameter4;
+} KIBUGCHECK_INFO;
+
  #endif /* KDBG_H */
contrib/elf2dmp/main.c +1 -1
index a62abadcc0..e25709b45d 100644
@@ -338,11 +338,26 @@ static bool fill_header(WinDumpHeader64 *hdr, struct pa_space *ps,
   * A dump may still contain valuable information even if it lacks contexts of
   * some CPUs due to dump corruption or a failure before starting CPUs.
   */
-static void fill_context(KDDEBUGGER_DATA64 *kdbg,
+static void fill_context(WinDumpHeader64 *hdr, KDDEBUGGER_DATA64 *kdbg,

contrib/elf2dmp/main.c static bool fill_header(WinDumpHeader64 *hdr, struct pa_space *ps, · line 351

Quoted 10 lines
contrib/elf2dmp/main.c +6 -0
                           struct va_space *vs, QEMU_Elf *qe)
  {
      int i;
 
+    /* First pass: identify faulting CPU and set header context early */
+    for (i = 0; i < qe->state_nr; i++) {
+        QEMUCPUState *s = qe->state[i];
+        if (s->size >= offsetof(QEMUCPUState, is_crash_occurred_cpu) +
+                        sizeof(s->is_crash_occurred_cpu) &&
+            s->is_crash_occurred_cpu) {

This trusts embedded s->size, while the validated MIN(s->size, n_descsz) at init_states() in contrib/elf2dmp/qemu_elf.c is discarded. A note claiming 448 bytes with a 440-byte descriptor reads the marker beyond that descriptor. Please preserve each note’s validated size.

Regards,
Akihiko Odaki

Quoted 59 lines
contrib/elf2dmp/main.c +41 -1
+            WinContext64 ctx;
+            win_context_init_from_qemu_cpu_state(&ctx, s);
+            memcpy(hdr->ContextBuffer, &ctx, sizeof(ctx));
+            printf("Faulting CPU identified: #%d\n", i);
+            break;
+        }
+    }
+
+    /* Second pass: fill context for all CPUs (best-effort) */
      for (i = 0; i < qe->state_nr; i++) {
          uint64_t Prcb;
          uint64_t Context;
@@ -512,6 +527,7 @@ int main(int argc, char *argv[])
      uint64_t KdVersionBlock;
      bool kernel_found = false;
      OMFSignatureRSDS rsds;
+    uint64_t KiBugCheckData;
 
      if (argc != 3) {
          eprintf("usage:\n\t%s elf_file dmp_file\n", argv[0]);
@@ -611,7 +627,37 @@ int main(int argc, char *argv[])
          goto out_kdbg;
      }
 
-    fill_context(kdbg, &vs, &qemu_elf);
+    if (!SYM_RESOLVE(KernBase, &pdb, KiBugCheckData)) {
+        eprintf("Failed to get KiBugCheckData.\n");
+    } else {
+        KIBUGCHECK_INFO data = { 0 };
+        if (va_space_rw(&vs, KiBugCheckData, &data, sizeof(data), 0)) {
+            header.BugcheckCode = data.BugcheckCode;
+            header.BugcheckParameter1 = data.BugcheckParameter1;
+            header.BugcheckParameter2 = data.BugcheckParameter2;
+            header.BugcheckParameter3 = data.BugcheckParameter3;
+            header.BugcheckParameter4 = data.BugcheckParameter4;
+
+            /*
+             * If BugcheckCode wasn't saved, we consider guest OS as alive.
+             */
+            if (!header.BugcheckCode) {
+                header.BugcheckCode = LIVE_SYSTEM_DUMP;
+            }
+
+            printf("KiBugCheckData: 0x%016" PRIx64
+                   ", BugcheckCode: 0x%08" PRIx32 ", Args:"
+                   " 0x%016" PRIx64 " 0x%016" PRIx64
+                   " 0x%016" PRIx64 " 0x%016" PRIx64 "\n",
+                    KiBugCheckData, header.BugcheckCode,
+                    data.BugcheckParameter1, data.BugcheckParameter2,
+                    data.BugcheckParameter3, data.BugcheckParameter4);
+        } else {
+            eprintf("Failed to va_space_rw KiBugCheckData.\n");
+        }
+    }
+
+    fill_context(&header, kdbg, &vs, &qemu_elf);
 
      if (!write_dump(&ps, &header, argv[2])) {
          eprintf("Failed to save dump\n");