elf2dmp: identify faulting CPU in Windows crash dumps
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
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
@@ -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
PATCH v3 2/3 dump: add crash_occurred flag to QEMUCPUState
In reply to Zhengrong LiAdd 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
@@ -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
PATCH v3 3/3 elf2dmp: fill ContextBuffer with faulting CPU context
In reply to Zhengrong LiWhen 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
@@ -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
@@ -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
@@ -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
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_occurredtracking 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 unusedfault_cpuvariable infill_context()
- Use per-CPU size check (s->size>= offsetof + sizeof) instead of globalhas_is_crash_occurred_cpuflag, fixing heterogeneous dumps where CPU notes have different sizes
- Removehas_is_crash_occurred_cpufromQEMU_Elfstruct andinit_states()since per-CPU check makes it unnecessary
- Move QEMUCPUState struct growth from patch 2 to patch 3 sosizeof()change andinit_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
- Clampstate->sizetoMIN(state->size, n_descsz)ininit_states()to prevent reading beyond note descriptor boundary
- Comparestate_sizeagainst offsetof +sizeof(kernel_gs_base)instead ofsizeof(*state)to avoid misclassifying valid old 440-byte notes as lackingkernel_gs_baseChanges since v1:
- Set header context before best-effort guest memory operations to avoid losing faulting CPU context on partial dump failures
- UseLIVE_SYSTEM_DUMPwhen BugcheckCode is zero
- Use PRIx32 for BugcheckCode format specifier
- Use linux.alibaba.com identityZhengrong Li (3):
runstate: setcrash_occurredon guest crashloaded
dump: addcrash_occurredflag 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
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
Hi all,
A gentle follow-up ping on this reviewed series.
Would it be possible to queue it for QEMU master?
Thanks,
Zhengrong