VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGFCoreWrite.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.0 KB
RevLine 
[32006]1/* $Id: DBGFCoreWrite.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Guest Core Dump.
4 */
5
6/*
[76553]7 * Copyright (C) 2010-2019 Oracle Corporation
[32006]8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
[34134]18/** @page pg_dbgf_vmcore VMCore Format
19 *
20 * The VirtualBox VMCore Format:
[32219]21 * [ ELF 64 Header] -- Only 1
22 *
23 * [ PT_NOTE ] -- Only 1
[32313]24 * - Offset into CoreDescriptor followed by list of Notes (Note Hdr + data) of VBox CPUs.
[34134]25 * - (Any Additional custom Note sections).
[32219]26 *
27 * [ PT_LOAD ] -- One for each contiguous memory chunk
[34134]28 * - Memory offset (physical).
29 * - File offset.
[32219]30 *
[32313]31 * CoreDescriptor
[34134]32 * - Magic, VBox version.
33 * - Number of CPus.
[32313]34 *
[32219]35 * Per-CPU register dump
[34134]36 * - CPU 1 Note Hdr + Data.
37 * - CPU 2 Note Hdr + Data.
[32219]38 * ...
39 * (Additional custom notes Hdr+data)
[34134]40 * - VBox 1 Note Hdr + Data.
41 * - VBox 2 Note Hdr + Data.
[32219]42 * ...
43 * Memory dump
44 *
45 */
[32006]46
[57358]47
48/*********************************************************************************************************************************
49* Header Files *
50*********************************************************************************************************************************/
[32006]51#define LOG_GROUP LOG_GROUP_DBGF
52#include <iprt/param.h>
[32050]53#include <iprt/file.h>
[56514]54#include <iprt/mem.h>
[69440]55#include <iprt/formats/elf64.h>
[32219]56
57#include "DBGFInternal.h"
58
[35346]59#include <VBox/vmm/cpum.h>
[44399]60#include <VBox/vmm/pgm.h>
[64663]61#include <VBox/vmm/apic.h>
[35346]62#include <VBox/vmm/dbgf.h>
63#include <VBox/vmm/dbgfcorefmt.h>
[44399]64#include <VBox/vmm/mm.h>
[35346]65#include <VBox/vmm/vm.h>
[44399]66#include <VBox/vmm/uvm.h>
67
[32006]68#include <VBox/err.h>
69#include <VBox/log.h>
[32253]70#include <VBox/version.h>
[32006]71
72
[57358]73/*********************************************************************************************************************************
74* Defined Constants And Macros *
75*********************************************************************************************************************************/
[32253]76#define DBGFLOG_NAME "DBGFCoreWrite"
[32214]77
[34902]78
[57358]79/*********************************************************************************************************************************
80* Global Variables *
81*********************************************************************************************************************************/
[56302]82static const int g_NoteAlign = 8;
83static const int g_cbNoteName = 16;
[32219]84
[56302]85/* The size of these strings (incl. NULL terminator) must align to 8 bytes (g_NoteAlign) and -not- 4 bytes. */
86static const char *g_pcszCoreVBoxCore = "VBCORE";
87static const char *g_pcszCoreVBoxCpu = "VBCPU";
[32450]88
89
[57358]90/*********************************************************************************************************************************
91* Structures and Typedefs *
92*********************************************************************************************************************************/
[32214]93/**
[34902]94 * Guest core writer data.
95 *
[56302]96 * Used to pass parameters from DBGFR3CoreWrite to dbgfR3CoreWriteRendezvous().
[32214]97 */
[34902]98typedef struct DBGFCOREDATA
[32214]99{
[34902]100 /** The name of the file to write the file to. */
101 const char *pszFilename;
102 /** Whether to replace (/overwrite) any existing file. */
103 bool fReplaceFile;
104} DBGFCOREDATA;
105/** Pointer to the guest core writer data. */
106typedef DBGFCOREDATA *PDBGFCOREDATA;
[32214]107
[34902]108
109
[32006]110/**
[32050]111 * ELF function to write 64-bit ELF header.
112 *
[58122]113 * @param hFile The file to write to.
114 * @param cProgHdrs Number of program headers.
115 * @param cSecHdrs Number of section headers.
[32050]116 *
117 * @return IPRT status code.
118 */
[34135]119static int Elf64WriteElfHdr(RTFILE hFile, uint16_t cProgHdrs, uint16_t cSecHdrs)
[32050]120{
121 Elf64_Ehdr ElfHdr;
122 RT_ZERO(ElfHdr);
123 ElfHdr.e_ident[EI_MAG0] = ELFMAG0;
124 ElfHdr.e_ident[EI_MAG1] = ELFMAG1;
125 ElfHdr.e_ident[EI_MAG2] = ELFMAG2;
126 ElfHdr.e_ident[EI_MAG3] = ELFMAG3;
127 ElfHdr.e_ident[EI_DATA] = ELFDATA2LSB;
128 ElfHdr.e_type = ET_CORE;
129 ElfHdr.e_version = EV_CURRENT;
130 ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
[34134]131 /* 32-bit builds will produce cores with e_machine EM_386. */
[32050]132#ifdef RT_ARCH_AMD64
133 ElfHdr.e_machine = EM_X86_64;
134#else
135 ElfHdr.e_machine = EM_386;
136#endif
137 ElfHdr.e_phnum = cProgHdrs;
138 ElfHdr.e_shnum = cSecHdrs;
139 ElfHdr.e_ehsize = sizeof(ElfHdr);
140 ElfHdr.e_phoff = sizeof(ElfHdr);
141 ElfHdr.e_phentsize = sizeof(Elf64_Phdr);
142 ElfHdr.e_shentsize = sizeof(Elf64_Shdr);
143
[34135]144 return RTFileWrite(hFile, &ElfHdr, sizeof(ElfHdr), NULL /* all */);
[32050]145}
146
147
148/**
149 * ELF function to write 64-bit program header.
150 *
[58122]151 * @param hFile The file to write to.
152 * @param Type Type of program header (PT_*).
153 * @param fFlags Flags (access permissions, PF_*).
154 * @param offFileData File offset of contents.
155 * @param cbFileData Size of contents in the file.
156 * @param cbMemData Size of contents in memory.
157 * @param Phys Physical address, pass zero if not applicable.
[32050]158 *
159 * @return IPRT status code.
160 */
[34134]161static int Elf64WriteProgHdr(RTFILE hFile, uint32_t Type, uint32_t fFlags, uint64_t offFileData, uint64_t cbFileData,
[34135]162 uint64_t cbMemData, RTGCPHYS Phys)
[32050]163{
164 Elf64_Phdr ProgHdr;
165 RT_ZERO(ProgHdr);
[56302]166 ProgHdr.p_type = Type;
167 ProgHdr.p_flags = fFlags;
168 ProgHdr.p_offset = offFileData;
169 ProgHdr.p_filesz = cbFileData;
170 ProgHdr.p_memsz = cbMemData;
171 ProgHdr.p_paddr = Phys;
[32050]172
[34135]173 return RTFileWrite(hFile, &ProgHdr, sizeof(ProgHdr), NULL /* all */);
[32050]174}
175
176
177/**
[32236]178 * Returns the size of the NOTE section given the name and size of the data.
[32219]179 *
[58122]180 * @param pszName Name of the note section.
181 * @param cbData Size of the data portion of the note section.
[32219]182 *
183 * @return The size of the NOTE section as rounded to the file alignment.
184 */
[34135]185static uint64_t Elf64NoteSectionSize(const char *pszName, uint64_t cbData)
[32219]186{
[32236]187 uint64_t cbNote = sizeof(Elf64_Nhdr);
188
[56302]189 size_t cbName = strlen(pszName) + 1;
190 size_t cbNameAlign = RT_ALIGN_Z(cbName, g_NoteAlign);
[32236]191
[56302]192 cbNote += cbNameAlign;
193 cbNote += RT_ALIGN_64(cbData, g_NoteAlign);
[32236]194 return cbNote;
[32219]195}
196
197
198/**
[32214]199 * Elf function to write 64-bit note header.
[32050]200 *
[58122]201 * @param hFile The file to write to.
202 * @param Type Type of this section.
203 * @param pszName Name of this section.
204 * @param pvData Opaque pointer to the data, if NULL only computes size.
205 * @param cbData Size of the data.
[32214]206 *
[58122]207 * @returns IPRT status code.
[32214]208 */
[58126]209static int Elf64WriteNoteHdr(RTFILE hFile, uint16_t Type, const char *pszName, const void *pvData, uint64_t cbData)
[32214]210{
[58126]211 AssertReturn(pvData, VERR_INVALID_POINTER);
[32236]212 AssertReturn(cbData > 0, VERR_NO_DATA);
[32214]213
[56302]214 char szNoteName[g_cbNoteName];
[32236]215 RT_ZERO(szNoteName);
216 RTStrCopy(szNoteName, sizeof(szNoteName), pszName);
[32214]217
[56302]218 size_t cbName = strlen(szNoteName) + 1;
219 size_t cbNameAlign = RT_ALIGN_Z(cbName, g_NoteAlign);
220 uint64_t cbDataAlign = RT_ALIGN_64(cbData, g_NoteAlign);
[32236]221
[32450]222 /*
223 * Yell loudly and bail if we are going to be writing a core file that is not compatible with
[57237]224 * both Solaris and the 64-bit ELF spec. which dictates 8-byte alignment. See @bugref{5211#c3}.
[32450]225 */
[56302]226 if (cbNameAlign - cbName > 3)
[32450]227 {
[56302]228 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr pszName=%s cbName=%u cbNameAlign=%u, cbName aligns to 4 not 8-bytes!\n",
229 pszName, cbName, cbNameAlign));
[32450]230 return VERR_INVALID_PARAMETER;
231 }
232
[32558]233 if (cbDataAlign - cbData > 3)
[32450]234 {
[56302]235 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr pszName=%s cbData=%u cbDataAlign=%u, cbData aligns to 4 not 8-bytes!\n",
236 pszName, cbData, cbDataAlign));
[32450]237 return VERR_INVALID_PARAMETER;
238 }
239
[32219]240 static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
[56302]241 AssertCompile(sizeof(s_achPad) >= g_NoteAlign - 1);
[32214]242
[32236]243 Elf64_Nhdr ElfNoteHdr;
244 RT_ZERO(ElfNoteHdr);
[56302]245 ElfNoteHdr.n_namesz = (Elf64_Word)cbName - 1; /* Again, a discrepancy between ELF-64 and Solaris,
[57237]246 we will follow ELF-64, see @bugref{5211#c3}. */
[32236]247 ElfNoteHdr.n_type = Type;
248 ElfNoteHdr.n_descsz = (Elf64_Word)cbDataAlign;
249
[32214]250 /*
[32236]251 * Write note header.
[32214]252 */
[32239]253 int rc = RTFileWrite(hFile, &ElfNoteHdr, sizeof(ElfNoteHdr), NULL /* all */);
[32214]254 if (RT_SUCCESS(rc))
255 {
[32236]256 /*
257 * Write note name.
258 */
[56302]259 rc = RTFileWrite(hFile, szNoteName, cbName, NULL /* all */);
[32214]260 if (RT_SUCCESS(rc))
261 {
[32236]262 /*
263 * Write note name padding if required.
264 */
[56302]265 if (cbNameAlign > cbName)
266 rc = RTFileWrite(hFile, s_achPad, cbNameAlign - cbName, NULL);
[32236]267
268 if (RT_SUCCESS(rc))
269 {
270 /*
271 * Write note data.
272 */
[58126]273 rc = RTFileWrite(hFile, pvData, cbData, NULL /* all */);
[32236]274 if (RT_SUCCESS(rc))
275 {
276 /*
277 * Write note data padding if required.
278 */
279 if (cbDataAlign > cbData)
[32239]280 rc = RTFileWrite(hFile, s_achPad, cbDataAlign - cbData, NULL /* all*/);
[32236]281 }
282 }
[32214]283 }
284 }
285
286 if (RT_FAILURE(rc))
[56302]287 LogRel((DBGFLOG_NAME ": RTFileWrite failed. rc=%Rrc pszName=%s cbName=%u cbNameAlign=%u cbData=%u cbDataAlign=%u\n",
288 rc, pszName, cbName, cbNameAlign, cbData, cbDataAlign));
[32214]289
290 return rc;
291}
292
293
294/**
295 * Count the number of memory ranges that go into the core file.
296 *
[32050]297 * We cannot do a page-by-page dump of the entire guest memory as there will be
[32214]298 * way too many program header entries. Also we don't want to dump MMIO regions
299 * which means we cannot have a 1:1 mapping between core file offset and memory
300 * offset. Instead we dump the memory in ranges. A memory range is a contiguous
301 * memory area suitable for dumping to a core file.
[32050]302 *
[58122]303 * @param pVM The cross context VM structure.
[32050]304 *
[32214]305 * @return Number of memory ranges
[32050]306 */
[32214]307static uint32_t dbgfR3GetRamRangeCount(PVM pVM)
[32050]308{
[32214]309 return PGMR3PhysGetRamRangeCount(pVM);
[32050]310}
311
312
313/**
[56514]314 * Gets the guest-CPU context suitable for dumping into the core file.
315 *
[64663]316 * @param pVCpu The cross context virtual CPU structure.
[56514]317 * @param pDbgfCpu Where to dump the guest-CPU data.
318 */
[64663]319static void dbgfR3GetCoreCpu(PVMCPU pVCpu, PDBGFCORECPU pDbgfCpu)
[56514]320{
321#define DBGFCOPYSEL(a_dbgfsel, a_cpumselreg) \
322 do { \
323 (a_dbgfsel).uBase = (a_cpumselreg).u64Base; \
324 (a_dbgfsel).uLimit = (a_cpumselreg).u32Limit; \
325 (a_dbgfsel).uAttr = (a_cpumselreg).Attr.u; \
326 (a_dbgfsel).uSel = (a_cpumselreg).Sel; \
327 } while (0)
328
[64663]329 PVM pVM = pVCpu->CTX_SUFF(pVM);
330 PCCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
[56514]331 pDbgfCpu->rax = pCtx->rax;
332 pDbgfCpu->rbx = pCtx->rbx;
333 pDbgfCpu->rcx = pCtx->rcx;
334 pDbgfCpu->rdx = pCtx->rdx;
335 pDbgfCpu->rsi = pCtx->rsi;
336 pDbgfCpu->rdi = pCtx->rdi;
337 pDbgfCpu->r8 = pCtx->r8;
338 pDbgfCpu->r9 = pCtx->r9;
339 pDbgfCpu->r10 = pCtx->r10;
340 pDbgfCpu->r11 = pCtx->r11;
341 pDbgfCpu->r12 = pCtx->r12;
342 pDbgfCpu->r13 = pCtx->r13;
343 pDbgfCpu->r14 = pCtx->r14;
344 pDbgfCpu->r15 = pCtx->r15;
345 pDbgfCpu->rip = pCtx->rip;
346 pDbgfCpu->rsp = pCtx->rsp;
347 pDbgfCpu->rbp = pCtx->rbp;
[61532]348 pDbgfCpu->rflags = pCtx->rflags.u;
[56514]349 DBGFCOPYSEL(pDbgfCpu->cs, pCtx->cs);
350 DBGFCOPYSEL(pDbgfCpu->ds, pCtx->ds);
351 DBGFCOPYSEL(pDbgfCpu->es, pCtx->es);
352 DBGFCOPYSEL(pDbgfCpu->fs, pCtx->fs);
353 DBGFCOPYSEL(pDbgfCpu->gs, pCtx->gs);
354 DBGFCOPYSEL(pDbgfCpu->ss, pCtx->ss);
355 pDbgfCpu->cr0 = pCtx->cr0;
356 pDbgfCpu->cr2 = pCtx->cr2;
357 pDbgfCpu->cr3 = pCtx->cr3;
358 pDbgfCpu->cr4 = pCtx->cr4;
359 AssertCompile(RT_ELEMENTS(pDbgfCpu->dr) == RT_ELEMENTS(pCtx->dr));
360 for (unsigned i = 0; i < RT_ELEMENTS(pDbgfCpu->dr); i++)
361 pDbgfCpu->dr[i] = pCtx->dr[i];
362 pDbgfCpu->gdtr.uAddr = pCtx->gdtr.pGdt;
363 pDbgfCpu->gdtr.cb = pCtx->gdtr.cbGdt;
364 pDbgfCpu->idtr.uAddr = pCtx->idtr.pIdt;
365 pDbgfCpu->idtr.cb = pCtx->idtr.cbIdt;
366 DBGFCOPYSEL(pDbgfCpu->ldtr, pCtx->ldtr);
367 DBGFCOPYSEL(pDbgfCpu->tr, pCtx->tr);
368 pDbgfCpu->sysenter.cs = pCtx->SysEnter.cs;
369 pDbgfCpu->sysenter.eip = pCtx->SysEnter.eip;
370 pDbgfCpu->sysenter.esp = pCtx->SysEnter.esp;
371 pDbgfCpu->msrEFER = pCtx->msrEFER;
372 pDbgfCpu->msrSTAR = pCtx->msrSTAR;
373 pDbgfCpu->msrPAT = pCtx->msrPAT;
374 pDbgfCpu->msrLSTAR = pCtx->msrLSTAR;
375 pDbgfCpu->msrCSTAR = pCtx->msrCSTAR;
376 pDbgfCpu->msrSFMASK = pCtx->msrSFMASK;
377 pDbgfCpu->msrKernelGSBase = pCtx->msrKERNELGSBASE;
[64663]378 pDbgfCpu->msrApicBase = APICGetBaseMsrNoCheck(pVCpu);
[56514]379 pDbgfCpu->aXcr[0] = pCtx->aXcr[0];
380 pDbgfCpu->aXcr[1] = pCtx->aXcr[1];
381 AssertCompile(sizeof(pDbgfCpu->ext) == sizeof(*pCtx->pXStateR3));
[57172]382 pDbgfCpu->cbExt = pVM->cpum.ro.GuestFeatures.cbMaxExtendedState;
[57167]383 if (RT_LIKELY(pDbgfCpu->cbExt))
[57171]384 memcpy(&pDbgfCpu->ext, pCtx->pXStateR3, pDbgfCpu->cbExt);
[56514]385
386#undef DBGFCOPYSEL
387}
388
389
390/**
[56302]391 * Worker function for dbgfR3CoreWrite() which does the writing.
[32006]392 *
[34134]393 * @returns VBox status code
[58122]394 * @param pVM The cross context VM structure.
[34134]395 * @param hFile The file to write to. Caller closes this.
[32006]396 */
[34902]397static int dbgfR3CoreWriteWorker(PVM pVM, RTFILE hFile)
[32006]398{
399 /*
[32214]400 * Collect core information.
[32050]401 */
[34135]402 uint32_t const cu32MemRanges = dbgfR3GetRamRangeCount(pVM);
403 uint16_t const cMemRanges = cu32MemRanges < UINT16_MAX - 1 ? cu32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
404 uint16_t const cProgHdrs = cMemRanges + 1;
[32006]405
[32253]406 DBGFCOREDESCRIPTOR CoreDescriptor;
407 RT_ZERO(CoreDescriptor);
[56302]408 CoreDescriptor.u32Magic = DBGFCORE_MAGIC;
409 CoreDescriptor.u32FmtVersion = DBGFCORE_FMT_VERSION;
410 CoreDescriptor.cbSelf = sizeof(CoreDescriptor);
411 CoreDescriptor.u32VBoxVersion = VBOX_FULL_VERSION;
412 CoreDescriptor.u32VBoxRevision = VMMGetSvnRev();
413 CoreDescriptor.cCpus = pVM->cCpus;
[32253]414
[32595]415 Log((DBGFLOG_NAME ": CoreDescriptor Version=%u Revision=%u\n", CoreDescriptor.u32VBoxVersion, CoreDescriptor.u32VBoxRevision));
[32253]416
[32050]417 /*
[34135]418 * Compute the file layout (see pg_dbgf_vmcore).
[32050]419 */
[56302]420 uint64_t const offElfHdr = RTFileTell(hFile);
421 uint64_t const offNoteSection = offElfHdr + sizeof(Elf64_Ehdr);
422 uint64_t const offLoadSections = offNoteSection + sizeof(Elf64_Phdr);
423 uint64_t const cbLoadSections = cMemRanges * sizeof(Elf64_Phdr);
424 uint64_t const offCoreDescriptor = offLoadSections + cbLoadSections;
425 uint64_t const cbCoreDescriptor = Elf64NoteSectionSize(g_pcszCoreVBoxCore, sizeof(CoreDescriptor));
426 uint64_t const offCpuDumps = offCoreDescriptor + cbCoreDescriptor;
[56514]427 uint64_t const cbCpuDumps = pVM->cCpus * Elf64NoteSectionSize(g_pcszCoreVBoxCpu, sizeof(DBGFCORECPU));
[56302]428 uint64_t const offMemory = offCpuDumps + cbCpuDumps;
[32050]429
[34135]430 uint64_t const offNoteSectionData = offCoreDescriptor;
431 uint64_t const cbNoteSectionData = cbCoreDescriptor + cbCpuDumps;
432
[32050]433 /*
[32253]434 * Write ELF header.
435 */
[34135]436 int rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */);
[32253]437 if (RT_FAILURE(rc))
438 {
[32595]439 LogRel((DBGFLOG_NAME ": Elf64WriteElfHdr failed. rc=%Rrc\n", rc));
[34134]440 return rc;
[32253]441 }
[32214]442
[32253]443 /*
444 * Write PT_NOTE program header.
445 */
[34135]446 Assert(RTFileTell(hFile) == offNoteSection);
[32253]447 rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
[34135]448 offNoteSectionData, /* file offset to contents */
449 cbNoteSectionData, /* size in core file */
450 cbNoteSectionData, /* size in memory */
451 0); /* physical address */
[32253]452 if (RT_FAILURE(rc))
453 {
[32595]454 LogRel((DBGFLOG_NAME ": Elf64WritreProgHdr failed for PT_NOTE. rc=%Rrc\n", rc));
[34134]455 return rc;
[32253]456 }
[32214]457
[32253]458 /*
459 * Write PT_LOAD program header for each memory range.
460 */
[34135]461 Assert(RTFileTell(hFile) == offLoadSections);
462 uint64_t offMemRange = offMemory;
[32253]463 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
464 {
[34135]465 RTGCPHYS GCPhysStart;
466 RTGCPHYS GCPhysEnd;
467 bool fIsMmio;
[32253]468 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
469 if (RT_FAILURE(rc))
470 {
471 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
[34134]472 return rc;
[32253]473 }
[32214]474
[32253]475 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
476 uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
[32239]477
[32450]478 Log((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
[34134]479 iRange, GCPhysStart, GCPhysEnd, cbMemRange));
[32239]480
[32253]481 rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
482 offMemRange, /* file offset to contents */
483 cbFileRange, /* size in core file */
484 cbMemRange, /* size in memory */
[34135]485 GCPhysStart); /* physical address */
[32253]486 if (RT_FAILURE(rc))
487 {
[34135]488 LogRel((DBGFLOG_NAME ": Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n",
489 iRange, cbFileRange, cbMemRange, rc));
[34134]490 return rc;
[32253]491 }
[32239]492
[32253]493 offMemRange += cbFileRange;
494 }
[32239]495
[32253]496 /*
497 * Write the Core descriptor note header and data.
498 */
[34135]499 Assert(RTFileTell(hFile) == offCoreDescriptor);
[56302]500 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, g_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor));
[32253]501 if (RT_FAILURE(rc))
502 {
[56302]503 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for Note '%s' rc=%Rrc\n", g_pcszCoreVBoxCore, rc));
[34134]504 return rc;
[32253]505 }
[32239]506
[32253]507 /*
508 * Write the CPU context note headers and data.
[64663]509 * We allocate the DBGFCORECPU struct. rather than using the stack as it can be pretty large due to X86XSAVEAREA.
[32253]510 */
[34135]511 Assert(RTFileTell(hFile) == offCpuDumps);
[56514]512 PDBGFCORECPU pDbgfCoreCpu = (PDBGFCORECPU)RTMemAlloc(sizeof(*pDbgfCoreCpu));
513 if (RT_UNLIKELY(!pDbgfCoreCpu))
514 {
[57211]515 LogRel((DBGFLOG_NAME ": Failed to alloc %u bytes for DBGFCORECPU\n", sizeof(*pDbgfCoreCpu)));
[56514]516 return VERR_NO_MEMORY;
517 }
518
[32253]519 for (uint32_t iCpu = 0; iCpu < pVM->cCpus; iCpu++)
520 {
[64663]521 PVMCPU pVCpu = &pVM->aCpus[iCpu];
522 RT_BZERO(pDbgfCoreCpu, sizeof(*pDbgfCoreCpu));
523 dbgfR3GetCoreCpu(pVCpu, pDbgfCoreCpu);
[56514]524
525 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, g_pcszCoreVBoxCpu, pDbgfCoreCpu, sizeof(*pDbgfCoreCpu));
[32253]526 if (RT_FAILURE(rc))
527 {
[32595]528 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", iCpu, rc));
[56514]529 RTMemFree(pDbgfCoreCpu);
[34134]530 return rc;
[32253]531 }
532 }
[56514]533 RTMemFree(pDbgfCoreCpu);
534 pDbgfCoreCpu = NULL;
[32239]535
[32253]536 /*
537 * Write memory ranges.
538 */
[34135]539 Assert(RTFileTell(hFile) == offMemory);
[32253]540 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
541 {
542 RTGCPHYS GCPhysStart;
543 RTGCPHYS GCPhysEnd;
[34135]544 bool fIsMmio;
[32253]545 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
546 if (RT_FAILURE(rc))
547 {
[32595]548 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange(2) failed for iRange(%u) rc=%Rrc\n", iRange, rc));
[34134]549 return rc;
[32253]550 }
[32239]551
[32253]552 if (fIsMmio)
553 continue;
554
555 /*
556 * Write page-by-page of this memory range.
[34197]557 *
558 * The read function may fail on MMIO ranges, we write these as zero
559 * pages for now (would be nice to have the VGA bits there though).
[32253]560 */
561 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
[34135]562 uint64_t cPages = cbMemRange >> PAGE_SHIFT;
[32253]563 for (uint64_t iPage = 0; iPage < cPages; iPage++)
564 {
[34135]565 uint8_t abPage[PAGE_SIZE];
[34197]566 rc = PGMPhysSimpleReadGCPhys(pVM, abPage, GCPhysStart + (iPage << PAGE_SHIFT), sizeof(abPage));
[32253]567 if (RT_FAILURE(rc))
568 {
[34197]569 if (rc != VERR_PGM_PHYS_PAGE_RESERVED)
570 LogRel((DBGFLOG_NAME ": PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange, iPage, rc));
[34135]571 RT_ZERO(abPage);
[32253]572 }
573
[34135]574 rc = RTFileWrite(hFile, abPage, sizeof(abPage), NULL /* all */);
[32253]575 if (RT_FAILURE(rc))
576 {
[32595]577 LogRel((DBGFLOG_NAME ": RTFileWrite failed. iRange=%u iPage=%u rc=%Rrc\n", iRange, iPage, rc));
[34134]578 return rc;
[32253]579 }
[32050]580 }
581 }
582
[34134]583 return rc;
584}
[32253]585
[34134]586
587/**
[56302]588 * EMT Rendezvous worker function for DBGFR3CoreWrite().
[34134]589 *
[58122]590 * @param pVM The cross context VM structure.
[58123]591 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
[34134]592 * @param pvData Opaque data.
593 *
594 * @return VBox status code.
595 */
[34902]596static DECLCALLBACK(VBOXSTRICTRC) dbgfR3CoreWriteRendezvous(PVM pVM, PVMCPU pVCpu, void *pvData)
[34134]597{
598 /*
599 * Validate input.
600 */
601 AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
602 AssertReturn(pVCpu, VERR_INVALID_VMCPU_HANDLE);
603 AssertReturn(pvData, VERR_INVALID_POINTER);
604
605 PDBGFCOREDATA pDbgfData = (PDBGFCOREDATA)pvData;
606
607 /*
608 * Create the core file.
609 */
[34902]610 uint32_t fFlags = (pDbgfData->fReplaceFile ? RTFILE_O_CREATE_REPLACE : RTFILE_O_CREATE)
611 | RTFILE_O_WRITE
612 | RTFILE_O_DENY_ALL
613 | (0600 << RTFILE_O_CREATE_MODE_SHIFT);
614 RTFILE hFile;
615 int rc = RTFileOpen(&hFile, pDbgfData->pszFilename, fFlags);
[34134]616 if (RT_SUCCESS(rc))
617 {
[34902]618 rc = dbgfR3CoreWriteWorker(pVM, hFile);
[34134]619 RTFileClose(hFile);
620 }
621 else
[34902]622 LogRel((DBGFLOG_NAME ": RTFileOpen failed for '%s' rc=%Rrc\n", pDbgfData->pszFilename, rc));
[32050]623 return rc;
[32006]624}
625
626
627/**
628 * Write core dump of the guest.
629 *
[34902]630 * @returns VBox status code.
[44399]631 * @param pUVM The user mode VM handle.
[34902]632 * @param pszFilename The name of the file to which the guest core
633 * dump should be written.
634 * @param fReplaceFile Whether to replace the file or not.
[32214]635 *
[45930]636 * @remarks The VM may need to be suspended before calling this function in
637 * order to truly stop all device threads and drivers. This function
638 * only synchronizes EMTs.
[32006]639 */
[44399]640VMMR3DECL(int) DBGFR3CoreWrite(PUVM pUVM, const char *pszFilename, bool fReplaceFile)
[32006]641{
[44399]642 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
643 PVM pVM = pUVM->pVM;
[32006]644 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[34902]645 AssertReturn(pszFilename, VERR_INVALID_HANDLE);
[32006]646
647 /*
[32214]648 * Pass the core write request down to EMT rendezvous which makes sure
[32534]649 * other EMTs, if any, are not running. IO threads could still be running
650 * but we don't care about them.
[32006]651 */
[32214]652 DBGFCOREDATA CoreData;
653 RT_ZERO(CoreData);
[34902]654 CoreData.pszFilename = pszFilename;
655 CoreData.fReplaceFile = fReplaceFile;
[32214]656
[34902]657 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWriteRendezvous, &CoreData);
[32534]658 if (RT_SUCCESS(rc))
[34902]659 LogRel((DBGFLOG_NAME ": Successfully wrote guest core dump '%s'\n", pszFilename));
[32534]660 else
[34902]661 LogRel((DBGFLOG_NAME ": Failed to write guest core dump '%s'. rc=%Rrc\n", pszFilename, rc));
[32534]662 return rc;
[32006]663}
664
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use