1 /* 2 * Copyright (c) 2018 Virtuozzo International GmbH 3 * 4 * This work is licensed under the terms of the GNU GPL, version 2 or later. 5 * 6 */ 7 8 #include "qemu/osdep.h" 9 10 #include "err.h" 11 #include "addrspace.h" 12 #include "pe.h" 13 #include "pdb.h" 14 #include "kdbg.h" 15 #include "download.h" 16 #include "qemu/win_dump_defs.h" 17 18 #define SYM_URL_BASE "https://msdl.microsoft.com/download/symbols/" 19 #define PDB_NAME "ntkrnlmp.pdb" 20 #define PE_NAME "ntoskrnl.exe" 21 22 #define INITIAL_MXCSR 0x1f80 23 #define MAX_NUMBER_OF_RUNS 42 24 25 typedef struct idt_desc { 26 uint16_t offset1; /* offset bits 0..15 */ 27 uint16_t selector; 28 uint8_t ist; 29 uint8_t type_attr; 30 uint16_t offset2; /* offset bits 16..31 */ 31 uint32_t offset3; /* offset bits 32..63 */ 32 uint32_t rsrvd; 33 } __attribute__ ((packed)) idt_desc_t; 34 35 static uint64_t idt_desc_addr(idt_desc_t desc) 36 { 37 return (uint64_t)desc.offset1 | ((uint64_t)desc.offset2 << 16) | 38 ((uint64_t)desc.offset3 << 32); 39 } 40 41 static const uint64_t SharedUserData = 0xfffff78000000000; 42 43 #define KUSD_OFFSET_SUITE_MASK 0x2d0 44 #define KUSD_OFFSET_PRODUCT_TYPE 0x264 45 46 #define SYM_RESOLVE(base, r, s) ((s = pdb_resolve(base, r, #s)),\ 47 s ? printf(#s" = 0x%016"PRIx64"\n", s) :\ 48 eprintf("Failed to resolve "#s"\n"), s) 49 50 static uint64_t rol(uint64_t x, uint64_t y) 51 { 52 return (x << y) | (x >> (64 - y)); 53 } 54 55 /* 56 * Decoding algorithm can be found in Volatility project 57 */ 58 static void kdbg_decode(uint64_t *dst, uint64_t *src, size_t size, 59 uint64_t kwn, uint64_t kwa, uint64_t kdbe) 60 { 61 size_t i; 62 assert(size % sizeof(uint64_t) == 0); 63 for (i = 0; i < size / sizeof(uint64_t); i++) { 64 uint64_t block; 65 66 block = src[i]; 67 block = rol(block ^ kwn, (uint8_t)kwn); 68 block = __builtin_bswap64(block ^ kdbe) ^ kwa; 69 dst[i] = block; 70 } 71 } 72 73 static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, 74 struct va_space *vs, uint64_t KdDebuggerDataBlock) 75 { 76 const char OwnerTag[4] = "KDBG"; 77 KDDEBUGGER_DATA64 *kdbg = NULL; 78 DBGKD_DEBUG_DATA_HEADER64 kdbg_hdr; 79 bool decode = false; 80 uint64_t kwn, kwa, KdpDataBlockEncoded; 81 82 if (!va_space_rw(vs, 83 KdDebuggerDataBlock + offsetof(KDDEBUGGER_DATA64, Header), 84 &kdbg_hdr, sizeof(kdbg_hdr), 0)) { 85 eprintf("Failed to extract KDBG header\n"); 86 return NULL; 87 } 88 89 if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) { 90 uint64_t KiWaitNever, KiWaitAlways; 91 92 decode = true; 93 94 if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) || 95 !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) || 96 !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) { 97 return NULL; 98 } 99 100 if (!va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) || 101 !va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) { 102 return NULL; 103 } 104 105 printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn); 106 printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa); 107 108 /* 109 * If KDBG header can be decoded, KDBG size is available 110 * and entire KDBG can be decoded. 111 */ 112 printf("Decoding KDBG header...\n"); 113 kdbg_decode((uint64_t *)&kdbg_hdr, (uint64_t *)&kdbg_hdr, 114 sizeof(kdbg_hdr), kwn, kwa, KdpDataBlockEncoded); 115 116 printf("Owner tag is \'%.4s\'\n", (char *)&kdbg_hdr.OwnerTag); 117 if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) { 118 eprintf("Failed to decode KDBG header\n"); 119 return NULL; 120 } 121 } 122 123 kdbg = g_malloc(kdbg_hdr.Size); 124 125 if (!va_space_rw(vs, KdDebuggerDataBlock, kdbg, kdbg_hdr.Size, 0)) { 126 eprintf("Failed to extract entire KDBG\n"); 127 g_free(kdbg); 128 return NULL; 129 } 130 131 if (!decode) { 132 return kdbg; 133 } 134 135 printf("Decoding KdDebuggerDataBlock...\n"); 136 kdbg_decode((uint64_t *)kdbg, (uint64_t *)kdbg, kdbg_hdr.Size, 137 kwn, kwa, KdpDataBlockEncoded); 138 139 va_space_rw(vs, KdDebuggerDataBlock, kdbg, kdbg_hdr.Size, 1); 140 141 return kdbg; 142 } 143 144 static void win_context_init_from_qemu_cpu_state(WinContext64 *ctx, 145 QEMUCPUState *s) 146 { 147 WinContext64 win_ctx = (WinContext64){ 148 .ContextFlags = WIN_CTX_X64 | WIN_CTX_INT | WIN_CTX_SEG | WIN_CTX_CTL, 149 .MxCsr = INITIAL_MXCSR, 150 151 .SegCs = s->cs.selector, 152 .SegSs = s->ss.selector, 153 .SegDs = s->ds.selector, 154 .SegEs = s->es.selector, 155 .SegFs = s->fs.selector, 156 .SegGs = s->gs.selector, 157 .EFlags = (uint32_t)s->rflags, 158 159 .Rax = s->rax, 160 .Rbx = s->rbx, 161 .Rcx = s->rcx, 162 .Rdx = s->rdx, 163 .Rsp = s->rsp, 164 .Rbp = s->rbp, 165 .Rsi = s->rsi, 166 .Rdi = s->rdi, 167 .R8 = s->r8, 168 .R9 = s->r9, 169 .R10 = s->r10, 170 .R11 = s->r11, 171 .R12 = s->r12, 172 .R13 = s->r13, 173 .R14 = s->r14, 174 .R15 = s->r15, 175 176 .Rip = s->rip, 177 .FltSave = { 178 .MxCsr = INITIAL_MXCSR, 179 }, 180 }; 181 182 *ctx = win_ctx; 183 } 184 185 /* 186 * Finds paging-structure hierarchy base, 187 * if previously set doesn't give access to kernel structures 188 */ 189 static bool fix_dtb(struct va_space *vs, QEMU_Elf *qe) 190 { 191 /* 192 * Firstly, test previously set DTB. 193 */ 194 if (va_space_resolve(vs, SharedUserData)) { 195 return true; 196 } 197 198 /* 199 * Secondly, find CPU which run system task. 200 */ 201 size_t i; 202 for (i = 0; i < qe->state_nr; i++) { 203 QEMUCPUState *s = qe->state[i]; 204 205 if (is_system(s)) { 206 va_space_set_dtb(vs, s->cr[3]); 207 printf("DTB 0x%016"PRIx64" has been found from CPU #%zu" 208 " as system task CR3\n", vs->dtb, i); 209 return va_space_resolve(vs, SharedUserData); 210 } 211 } 212 213 /* 214 * Thirdly, use KERNEL_GS_BASE from CPU #0 as PRCB address and 215 * CR3 as [Prcb+0x7000] 216 */ 217 if (qe->has_kernel_gs_base) { 218 QEMUCPUState *s = qe->state[0]; 219 uint64_t Prcb = s->kernel_gs_base; 220 uint64_t *cr3 = va_space_resolve(vs, Prcb + 0x7000); 221 222 if (!cr3) { 223 return false; 224 } 225 226 va_space_set_dtb(vs, *cr3); 227 printf("DirectoryTableBase = 0x%016"PRIx64" has been found from CPU #0" 228 " as interrupt handling CR3\n", vs->dtb); 229 return va_space_resolve(vs, SharedUserData); 230 } 231 232 return true; 233 } 234 235 static void try_merge_runs(struct pa_space *ps, 236 WinDumpPhyMemDesc64 *PhysicalMemoryBlock) 237 { 238 unsigned int merge_cnt = 0, run_idx = 0; 239 240 PhysicalMemoryBlock->NumberOfRuns = 0; 241 242 for (size_t idx = 0; idx < ps->block_nr; idx++) { 243 struct pa_block *blk = ps->block + idx; 244 struct pa_block *next = blk + 1; 245 246 PhysicalMemoryBlock->NumberOfPages += blk->size / ELF2DMP_PAGE_SIZE; 247 248 if (idx + 1 != ps->block_nr && blk->paddr + blk->size == next->paddr) { 249 printf("Block #%zu 0x%"PRIx64"+:0x%"PRIx64" and %u previous will be" 250 " merged\n", idx, blk->paddr, blk->size, merge_cnt); 251 merge_cnt++; 252 } else { 253 struct pa_block *first_merged = blk - merge_cnt; 254 255 printf("Block #%zu 0x%"PRIx64"+:0x%"PRIx64" and %u previous will be" 256 " merged to 0x%"PRIx64"+:0x%"PRIx64" (run #%u)\n", 257 idx, blk->paddr, blk->size, merge_cnt, first_merged->paddr, 258 blk->paddr + blk->size - first_merged->paddr, run_idx); 259 PhysicalMemoryBlock->Run[run_idx] = (WinDumpPhyMemRun64) { 260 .BasePage = first_merged->paddr / ELF2DMP_PAGE_SIZE, 261 .PageCount = (blk->paddr + blk->size - first_merged->paddr) / 262 ELF2DMP_PAGE_SIZE, 263 }; 264 PhysicalMemoryBlock->NumberOfRuns++; 265 run_idx++; 266 merge_cnt = 0; 267 } 268 } 269 } 270 271 static bool fill_header(WinDumpHeader64 *hdr, struct pa_space *ps, 272 struct va_space *vs, uint64_t KdDebuggerDataBlock, 273 KDDEBUGGER_DATA64 *kdbg, uint64_t KdVersionBlock, 274 int nr_cpus) 275 { 276 uint32_t *suite_mask = va_space_resolve(vs, SharedUserData + 277 KUSD_OFFSET_SUITE_MASK); 278 int32_t *product_type = va_space_resolve(vs, SharedUserData + 279 KUSD_OFFSET_PRODUCT_TYPE); 280 DBGKD_GET_VERSION64 kvb; 281 WinDumpHeader64 h; 282 283 QEMU_BUILD_BUG_ON(KUSD_OFFSET_SUITE_MASK >= ELF2DMP_PAGE_SIZE); 284 QEMU_BUILD_BUG_ON(KUSD_OFFSET_PRODUCT_TYPE >= ELF2DMP_PAGE_SIZE); 285 286 if (!suite_mask || !product_type) { 287 return false; 288 } 289 290 if (!va_space_rw(vs, KdVersionBlock, &kvb, sizeof(kvb), 0)) { 291 eprintf("Failed to extract KdVersionBlock\n"); 292 return false; 293 } 294 295 h = (WinDumpHeader64) { 296 .Signature = "PAGE", 297 .ValidDump = "DU64", 298 .MajorVersion = kvb.MajorVersion, 299 .MinorVersion = kvb.MinorVersion, 300 .DirectoryTableBase = vs->dtb, 301 .PfnDatabase = kdbg->MmPfnDatabase, 302 .PsLoadedModuleList = kdbg->PsLoadedModuleList, 303 .PsActiveProcessHead = kdbg->PsActiveProcessHead, 304 .MachineImageType = kvb.MachineType, 305 .NumberProcessors = nr_cpus, 306 .BugcheckCode = LIVE_SYSTEM_DUMP, 307 .KdDebuggerDataBlock = KdDebuggerDataBlock, 308 .DumpType = 1, 309 .Comment = "Hello from elf2dmp!", 310 .SuiteMask = *suite_mask, 311 .ProductType = *product_type, 312 .SecondaryDataState = kvb.KdSecondaryVersion, 313 .PhysicalMemoryBlock = (WinDumpPhyMemDesc64) { 314 .NumberOfRuns = ps->block_nr, 315 }, 316 .RequiredDumpSpace = sizeof(h), 317 }; 318 319 if (h.PhysicalMemoryBlock.NumberOfRuns <= MAX_NUMBER_OF_RUNS) { 320 for (size_t idx = 0; idx < ps->block_nr; idx++) { 321 h.PhysicalMemoryBlock.NumberOfPages += 322 ps->block[idx].size / ELF2DMP_PAGE_SIZE; 323 h.PhysicalMemoryBlock.Run[idx] = (WinDumpPhyMemRun64) { 324 .BasePage = ps->block[idx].paddr / ELF2DMP_PAGE_SIZE, 325 .PageCount = ps->block[idx].size / ELF2DMP_PAGE_SIZE, 326 }; 327 } 328 } else { 329 try_merge_runs(ps, &h.PhysicalMemoryBlock); 330 } 331 332 h.RequiredDumpSpace += 333 h.PhysicalMemoryBlock.NumberOfPages << ELF2DMP_PAGE_BITS; 334 335 *hdr = h; 336 337 return true; 338 } 339 340 /* 341 * fill_context() continues even if it fails to fill contexts of some CPUs. 342 * A dump may still contain valuable information even if it lacks contexts of 343 * some CPUs due to dump corruption or a failure before starting CPUs. 344 */ 345 static void fill_context(KDDEBUGGER_DATA64 *kdbg, 346 struct va_space *vs, QEMU_Elf *qe) 347 { 348 int i; 349 350 for (i = 0; i < qe->state_nr; i++) { 351 uint64_t Prcb; 352 uint64_t Context; 353 WinContext64 ctx; 354 QEMUCPUState *s = qe->state[i]; 355 356 if (!va_space_rw(vs, kdbg->KiProcessorBlock + sizeof(Prcb) * i, 357 &Prcb, sizeof(Prcb), 0)) { 358 eprintf("Failed to read CPU #%d PRCB location\n", i); 359 continue; 360 } 361 362 if (!Prcb) { 363 eprintf("Context for CPU #%d is missing\n", i); 364 continue; 365 } 366 367 if (!va_space_rw(vs, Prcb + kdbg->OffsetPrcbContext, 368 &Context, sizeof(Context), 0)) { 369 eprintf("Failed to read CPU #%d ContextFrame location\n", i); 370 continue; 371 } 372 373 printf("Filling context for CPU #%d...\n", i); 374 win_context_init_from_qemu_cpu_state(&ctx, s); 375 376 if (!va_space_rw(vs, Context, &ctx, sizeof(ctx), 1)) { 377 eprintf("Failed to fill CPU #%d context\n", i); 378 continue; 379 } 380 } 381 } 382 383 static bool pe_get_data_dir_entry(uint64_t base, void *start_addr, int idx, 384 void *entry, size_t size, struct va_space *vs) 385 { 386 const char e_magic[2] = "MZ"; 387 const char Signature[4] = "PE\0\0"; 388 IMAGE_DOS_HEADER *dos_hdr = start_addr; 389 IMAGE_NT_HEADERS64 nt_hdrs; 390 IMAGE_FILE_HEADER *file_hdr = &nt_hdrs.FileHeader; 391 IMAGE_OPTIONAL_HEADER64 *opt_hdr = &nt_hdrs.OptionalHeader; 392 IMAGE_DATA_DIRECTORY *data_dir = nt_hdrs.OptionalHeader.DataDirectory; 393 394 QEMU_BUILD_BUG_ON(sizeof(*dos_hdr) >= ELF2DMP_PAGE_SIZE); 395 396 if (memcmp(&dos_hdr->e_magic, e_magic, sizeof(e_magic))) { 397 return false; 398 } 399 400 if (!va_space_rw(vs, base + dos_hdr->e_lfanew, 401 &nt_hdrs, sizeof(nt_hdrs), 0)) { 402 return false; 403 } 404 405 if (memcmp(&nt_hdrs.Signature, Signature, sizeof(Signature)) || 406 file_hdr->Machine != 0x8664 || opt_hdr->Magic != 0x020b) { 407 return false; 408 } 409 410 if (!va_space_rw(vs, base + data_dir[idx].VirtualAddress, entry, size, 0)) { 411 return false; 412 } 413 414 printf("Data directory entry #%d: RVA = 0x%08"PRIx32"\n", idx, 415 (uint32_t)data_dir[idx].VirtualAddress); 416 417 return true; 418 } 419 420 static bool write_dump(struct pa_space *ps, 421 WinDumpHeader64 *hdr, const char *name) 422 { 423 FILE *dmp_file = fopen(name, "wb"); 424 size_t i; 425 426 if (!dmp_file) { 427 eprintf("Failed to open output file \'%s\'\n", name); 428 return false; 429 } 430 431 printf("Writing header to file...\n"); 432 433 if (fwrite(hdr, sizeof(*hdr), 1, dmp_file) != 1) { 434 eprintf("Failed to write dump header\n"); 435 fclose(dmp_file); 436 return false; 437 } 438 439 for (i = 0; i < ps->block_nr; i++) { 440 struct pa_block *b = &ps->block[i]; 441 442 printf("Writing block #%zu/%zu of %"PRIu64" bytes to file...\n", i, 443 ps->block_nr, b->size); 444 if (fwrite(b->addr, b->size, 1, dmp_file) != 1) { 445 eprintf("Failed to write block\n"); 446 fclose(dmp_file); 447 return false; 448 } 449 } 450 451 return !fclose(dmp_file); 452 } 453 454 static bool pe_check_pdb_name(uint64_t base, void *start_addr, 455 struct va_space *vs, OMFSignatureRSDS *rsds) 456 { 457 const char sign_rsds[4] = "RSDS"; 458 IMAGE_DEBUG_DIRECTORY debug_dir; 459 char pdb_name[sizeof(PDB_NAME)]; 460 461 if (!pe_get_data_dir_entry(base, start_addr, IMAGE_FILE_DEBUG_DIRECTORY, 462 &debug_dir, sizeof(debug_dir), vs)) { 463 eprintf("Failed to get Debug Directory\n"); 464 return false; 465 } 466 467 if (debug_dir.Type != IMAGE_DEBUG_TYPE_CODEVIEW) { 468 eprintf("Debug Directory type is not CodeView\n"); 469 return false; 470 } 471 472 if (!va_space_rw(vs, base + debug_dir.AddressOfRawData, 473 rsds, sizeof(*rsds), 0)) { 474 eprintf("Failed to resolve OMFSignatureRSDS\n"); 475 return false; 476 } 477 478 if (memcmp(&rsds->Signature, sign_rsds, sizeof(sign_rsds))) { 479 eprintf("CodeView signature is \'%.4s\', \'%.4s\' expected\n", 480 rsds->Signature, sign_rsds); 481 return false; 482 } 483 484 if (debug_dir.SizeOfData - sizeof(*rsds) != sizeof(PDB_NAME)) { 485 eprintf("PDB name size doesn't match\n"); 486 return false; 487 } 488 489 if (!va_space_rw(vs, base + debug_dir.AddressOfRawData + 490 offsetof(OMFSignatureRSDS, name), 491 pdb_name, sizeof(PDB_NAME), 0)) { 492 eprintf("Failed to resolve PDB name\n"); 493 return false; 494 } 495 496 printf("PDB name is \'%s\', \'%s\' expected\n", pdb_name, PDB_NAME); 497 498 return !strcmp(pdb_name, PDB_NAME); 499 } 500 501 static void pe_get_pdb_symstore_hash(OMFSignatureRSDS *rsds, char *hash) 502 { 503 sprintf(hash, "%.08x%.04x%.04x%.02x%.02x", rsds->guid.a, rsds->guid.b, 504 rsds->guid.c, rsds->guid.d[0], rsds->guid.d[1]); 505 hash += 20; 506 for (unsigned int i = 0; i < 6; i++, hash += 2) { 507 sprintf(hash, "%.02x", rsds->guid.e[i]); 508 } 509 510 sprintf(hash, "%.01x", rsds->age); 511 } 512 513 int main(int argc, char *argv[]) 514 { 515 int err = 1; 516 QEMU_Elf qemu_elf; 517 struct pa_space ps; 518 struct va_space vs; 519 QEMUCPUState *state; 520 idt_desc_t first_idt_desc; 521 uint64_t KernBase; 522 void *nt_start_addr = NULL; 523 WinDumpHeader64 header; 524 char pdb_hash[34]; 525 char pdb_url[] = SYM_URL_BASE PDB_NAME 526 "/0123456789ABCDEF0123456789ABCDEFx/" PDB_NAME; 527 struct pdb_reader pdb; 528 uint64_t KdDebuggerDataBlock; 529 KDDEBUGGER_DATA64 *kdbg; 530 uint64_t KdVersionBlock; 531 bool kernel_found = false; 532 OMFSignatureRSDS rsds; 533 534 if (argc != 3) { 535 eprintf("usage:\n\t%s elf_file dmp_file\n", argv[0]); 536 return 1; 537 } 538 539 if (!QEMU_Elf_init(&qemu_elf, argv[1])) { 540 eprintf("Failed to initialize QEMU ELF dump\n"); 541 return 1; 542 } 543 544 pa_space_create(&ps, &qemu_elf); 545 546 state = qemu_elf.state[0]; 547 printf("CPU #0 CR3 is 0x%016"PRIx64"\n", state->cr[3]); 548 549 va_space_create(&vs, &ps, state->cr[3]); 550 if (!fix_dtb(&vs, &qemu_elf)) { 551 eprintf("Failed to find paging base\n"); 552 goto out_elf; 553 } 554 555 printf("CPU #0 IDT is at 0x%016"PRIx64"\n", state->idt.base); 556 557 if (!va_space_rw(&vs, state->idt.base, 558 &first_idt_desc, sizeof(first_idt_desc), 0)) { 559 eprintf("Failed to get CPU #0 IDT[0]\n"); 560 goto out_ps; 561 } 562 printf("CPU #0 IDT[0] -> 0x%016"PRIx64"\n", idt_desc_addr(first_idt_desc)); 563 564 KernBase = idt_desc_addr(first_idt_desc) & ~(ELF2DMP_PAGE_SIZE - 1); 565 printf("Searching kernel downwards from 0x%016"PRIx64"...\n", KernBase); 566 567 for (; KernBase >= 0xfffff78000000000; KernBase -= ELF2DMP_PAGE_SIZE) { 568 nt_start_addr = va_space_resolve(&vs, KernBase); 569 if (!nt_start_addr) { 570 continue; 571 } 572 573 if (*(uint16_t *)nt_start_addr == 0x5a4d) { /* MZ */ 574 printf("Checking candidate KernBase = 0x%016"PRIx64"\n", KernBase); 575 if (pe_check_pdb_name(KernBase, nt_start_addr, &vs, &rsds)) { 576 kernel_found = true; 577 break; 578 } 579 } 580 } 581 582 if (!kernel_found) { 583 eprintf("Failed to find NT kernel image\n"); 584 goto out_ps; 585 } 586 587 printf("KernBase = 0x%016"PRIx64", signature is \'%.2s\'\n", KernBase, 588 (char *)nt_start_addr); 589 590 pe_get_pdb_symstore_hash(&rsds, pdb_hash); 591 592 sprintf(pdb_url, "%s%s/%s/%s", SYM_URL_BASE, PDB_NAME, pdb_hash, PDB_NAME); 593 printf("PDB URL is %s\n", pdb_url); 594 595 if (!download_url(PDB_NAME, pdb_url)) { 596 eprintf("Failed to download PDB file\n"); 597 goto out_ps; 598 } 599 600 if (!pdb_init_from_file(PDB_NAME, &pdb)) { 601 eprintf("Failed to initialize PDB reader\n"); 602 goto out_pdb_file; 603 } 604 605 if (!SYM_RESOLVE(KernBase, &pdb, KdDebuggerDataBlock) || 606 !SYM_RESOLVE(KernBase, &pdb, KdVersionBlock)) { 607 goto out_pdb; 608 } 609 610 kdbg = get_kdbg(KernBase, &pdb, &vs, KdDebuggerDataBlock); 611 if (!kdbg) { 612 goto out_pdb; 613 } 614 615 if (!fill_header(&header, &ps, &vs, KdDebuggerDataBlock, kdbg, 616 KdVersionBlock, qemu_elf.state_nr)) { 617 goto out_kdbg; 618 } 619 620 fill_context(kdbg, &vs, &qemu_elf); 621 622 if (!write_dump(&ps, &header, argv[2])) { 623 eprintf("Failed to save dump\n"); 624 goto out_kdbg; 625 } 626 627 err = 0; 628 629 out_kdbg: 630 g_free(kdbg); 631 out_pdb: 632 pdb_exit(&pdb); 633 out_pdb_file: 634 unlink(PDB_NAME); 635 out_ps: 636 pa_space_destroy(&ps); 637 out_elf: 638 QEMU_Elf_exit(&qemu_elf); 639 640 return err; 641 } 642