Skip to contentSkip to conversation

elf2dmp: identify faulting CPU in Windows crash dumps

PATCH In review Zhengrong Li 2 participants 7 messages Aug 6 – Sep 9

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
- Move QEMUCPUState struct growth from patch 2 to patch 3 so sizeof() change and init_states() update stay together (bisectability)
- Fix patch 1 commit message: soften "is never called" to "is not always called when crash dumping is enabled"
- Fix patch 2 commit message: clarify compatibility description for both old-tools/new-dumps and new-tools/old-dumps directions
- Clamp state->size to MIN(state->size, n_descsz) in init_states() to prevent reading beyond note descriptor boundary
- Compare state_size against offsetof + sizeof(kernel_gs_base) instead of sizeof(*state) to avoid misclassifying valid old 440-byte notes as lacking kernel_gs_base

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.c | 10 +++++++-
 contrib/elf2dmp/qemu_elf.h |  2 ++
 system/runstate.c          |  5 ++++
 target/i386/arch_dump.c    |  6 +++++
 6 files changed, 79 insertions(+), 3 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) when crash dumping is enabled, so qemu_system_guest_panicked() is not always called and crash_occurred remains false.

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 the local QEMUCPUState in arch_dump.c so that dump-guest-memory can record which CPU triggered the guest panic.

The new field is appended after kernel_gs_base, so older tools safely ignore the extra bytes in newer dumps, and newer tools detect its absence in older dumps by checking the 'size' field.

---
 target/i386/arch_dump.c | 6 ++++++
 1 file changed, 6 insertions(+)
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

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.

The embedded state->size is clamped to MIN(state->size, n_descsz) in init_states() to prevent reading beyond the note descriptor.

---
 contrib/elf2dmp/kdbg.h     |  9 +++++++
 contrib/elf2dmp/main.c     | 50 ++++++++++++++++++++++++++++++++++++--
 contrib/elf2dmp/qemu_elf.c | 10 +++++++-
 contrib/elf2dmp/qemu_elf.h |  2 ++
 4 files changed, 68 insertions(+), 3 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

contrib/elf2dmp/qemu_elf.c +9 -1
index c9bad6e82c..f69023554b 100644
@@ -103,7 +103,15 @@ static bool init_states(QEMU_Elf *qe)
             nhdr->n_descsz >= offsetof(QEMUCPUState, kernel_gs_base)) {
             state_size = MIN(state->size, nhdr->n_descsz);
 
-            if (state_size < sizeof(*state)) {
+            /*
+             * Clamp the embedded size to the actual note descriptor size
+             * so that downstream size checks (e.g. fill_context) never
+             * read beyond the descriptor boundary.
+             */
+            state->size = state_size;
+
+            if (state_size < offsetof(QEMUCPUState, kernel_gs_base) +
+                              sizeof(state->kernel_gs_base)) {
                 eprintf("CPU #%u: QEMU CPU state size %u doesn't match\n",
                         states->len, state_size);
                 /*
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);
Signature · 1 line
2.43.0
Reviewed-by on the v3 series
#4 lore raw
In reply to Zhengrong Li
Quoted 41 lines · Zhengrong Li

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
- Move QEMUCPUState struct growth from patch 2 to patch 3 so sizeof() change and init_states() update stay together (bisectability)
- Fix patch 1 commit message: soften "is never called" to "is not always called when crash dumping is enabled"
- Fix patch 2 commit message: clarify compatibility description for both old-tools/new-dumps and new-tools/old-dumps directions
- Clamp state->size to MIN(state->size, n_descsz) in init_states() to prevent reading beyond note descriptor boundary
- Compare state_size against offsetof + sizeof(kernel_gs_base) instead of sizeof(*state) to avoid misclassifying valid old 440-byte notes as lacking kernel_gs_base

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

Quoted 8 lines
  contrib/elf2dmp/kdbg.h     |  9 +++++++
  contrib/elf2dmp/main.c     | 50 ++++++++++++++++++++++++++++++++++++--
  contrib/elf2dmp/qemu_elf.c | 10 +++++++-
  contrib/elf2dmp/qemu_elf.h |  2 ++
  system/runstate.c          |  5 ++++
  target/i386/arch_dump.c    |  6 +++++
  6 files changed, 79 insertions(+), 3 deletions(-)

The whole series now looks good to me:

Regards,
Akihiko Odaki

author Reviewed-by Akihiko Odaki on the v3 series
#5 lore raw
In reply to Zhengrong Li

Hi all,

Just a gentle ping on this series. Akihiko Odaki has reviewed it and provided a Reviewed-by for the whole series:

Please let me know if there's anything else you'd like me to address, or if it could be picked up for QEMU master when you have a chance.

Thanks for your time.

Regards,
Zhengrong Li

#6 lore raw

Hi all,

A gentle follow-up ping on this reviewed series.
Would it be possible to queue it for QEMU master?

Thanks,
Zhengrong