VirtualBox

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

Last change on this file since 96860 was 96407, checked in by vboxsync, 22 months ago

scm copyright and license note update

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

© 2023 Oracle
ContactPrivacy policyTerms of Use