VirtualBox

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

Last change on this file since 101537 was 99051, checked in by vboxsync, 21 months ago

VMM: More ARMv8 x86/amd64 separation work, VBoxVMMArm compiles and links now, bugref:10385

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.5 KB
Line 
1/* $Id: DBGFCoreWrite.cpp 99051 2023-03-19 16:40:06Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Guest Core Dump.
4 */
5
6/*
7 * Copyright (C) 2010-2023 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#if defined(VBOX_VMM_TARGET_ARMV8)
340 AssertReleaseFailed();
341 RT_NOREF(pVCpu, pDbgfCpu);
342#else
343 PVM pVM = pVCpu->CTX_SUFF(pVM);
344 PCCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
345 pDbgfCpu->rax = pCtx->rax;
346 pDbgfCpu->rbx = pCtx->rbx;
347 pDbgfCpu->rcx = pCtx->rcx;
348 pDbgfCpu->rdx = pCtx->rdx;
349 pDbgfCpu->rsi = pCtx->rsi;
350 pDbgfCpu->rdi = pCtx->rdi;
351 pDbgfCpu->r8 = pCtx->r8;
352 pDbgfCpu->r9 = pCtx->r9;
353 pDbgfCpu->r10 = pCtx->r10;
354 pDbgfCpu->r11 = pCtx->r11;
355 pDbgfCpu->r12 = pCtx->r12;
356 pDbgfCpu->r13 = pCtx->r13;
357 pDbgfCpu->r14 = pCtx->r14;
358 pDbgfCpu->r15 = pCtx->r15;
359 pDbgfCpu->rip = pCtx->rip;
360 pDbgfCpu->rsp = pCtx->rsp;
361 pDbgfCpu->rbp = pCtx->rbp;
362 pDbgfCpu->rflags = pCtx->rflags.u;
363 DBGFCOPYSEL(pDbgfCpu->cs, pCtx->cs);
364 DBGFCOPYSEL(pDbgfCpu->ds, pCtx->ds);
365 DBGFCOPYSEL(pDbgfCpu->es, pCtx->es);
366 DBGFCOPYSEL(pDbgfCpu->fs, pCtx->fs);
367 DBGFCOPYSEL(pDbgfCpu->gs, pCtx->gs);
368 DBGFCOPYSEL(pDbgfCpu->ss, pCtx->ss);
369 pDbgfCpu->cr0 = pCtx->cr0;
370 pDbgfCpu->cr2 = pCtx->cr2;
371 pDbgfCpu->cr3 = pCtx->cr3;
372 pDbgfCpu->cr4 = pCtx->cr4;
373 AssertCompile(RT_ELEMENTS(pDbgfCpu->dr) == RT_ELEMENTS(pCtx->dr));
374 for (unsigned i = 0; i < RT_ELEMENTS(pDbgfCpu->dr); i++)
375 pDbgfCpu->dr[i] = pCtx->dr[i];
376 pDbgfCpu->gdtr.uAddr = pCtx->gdtr.pGdt;
377 pDbgfCpu->gdtr.cb = pCtx->gdtr.cbGdt;
378 pDbgfCpu->idtr.uAddr = pCtx->idtr.pIdt;
379 pDbgfCpu->idtr.cb = pCtx->idtr.cbIdt;
380 DBGFCOPYSEL(pDbgfCpu->ldtr, pCtx->ldtr);
381 DBGFCOPYSEL(pDbgfCpu->tr, pCtx->tr);
382 pDbgfCpu->sysenter.cs = pCtx->SysEnter.cs;
383 pDbgfCpu->sysenter.eip = pCtx->SysEnter.eip;
384 pDbgfCpu->sysenter.esp = pCtx->SysEnter.esp;
385 pDbgfCpu->msrEFER = pCtx->msrEFER;
386 pDbgfCpu->msrSTAR = pCtx->msrSTAR;
387 pDbgfCpu->msrPAT = pCtx->msrPAT;
388 pDbgfCpu->msrLSTAR = pCtx->msrLSTAR;
389 pDbgfCpu->msrCSTAR = pCtx->msrCSTAR;
390 pDbgfCpu->msrSFMASK = pCtx->msrSFMASK;
391 pDbgfCpu->msrKernelGSBase = pCtx->msrKERNELGSBASE;
392 pDbgfCpu->msrApicBase = APICGetBaseMsrNoCheck(pVCpu);
393 pDbgfCpu->msrTscAux = CPUMGetGuestTscAux(pVCpu);
394 pDbgfCpu->aXcr[0] = pCtx->aXcr[0];
395 pDbgfCpu->aXcr[1] = pCtx->aXcr[1];
396 AssertCompile(sizeof(pDbgfCpu->ext) == sizeof(pCtx->XState));
397 pDbgfCpu->cbExt = pVM->cpum.ro.GuestFeatures.cbMaxExtendedState;
398 if (RT_LIKELY(pDbgfCpu->cbExt))
399 memcpy(&pDbgfCpu->ext, &pCtx->XState, pDbgfCpu->cbExt);
400#endif
401
402#undef DBGFCOPYSEL
403}
404
405
406/**
407 * Worker function for dbgfR3CoreWrite() which does the writing.
408 *
409 * @returns VBox status code
410 * @param pVM The cross context VM structure.
411 * @param hFile The file to write to. Caller closes this.
412 */
413static int dbgfR3CoreWriteWorker(PVM pVM, RTFILE hFile)
414{
415 /*
416 * Collect core information.
417 */
418 uint32_t const cu32MemRanges = dbgfR3GetRamRangeCount(pVM);
419 uint16_t const cMemRanges = cu32MemRanges < UINT16_MAX - 1 ? cu32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
420 uint16_t const cProgHdrs = cMemRanges + 1;
421
422 DBGFCOREDESCRIPTOR CoreDescriptor;
423 RT_ZERO(CoreDescriptor);
424 CoreDescriptor.u32Magic = DBGFCORE_MAGIC;
425 CoreDescriptor.u32FmtVersion = DBGFCORE_FMT_VERSION;
426 CoreDescriptor.cbSelf = sizeof(CoreDescriptor);
427 CoreDescriptor.u32VBoxVersion = VBOX_FULL_VERSION;
428 CoreDescriptor.u32VBoxRevision = VMMGetSvnRev();
429 CoreDescriptor.cCpus = pVM->cCpus;
430
431 Log((DBGFLOG_NAME ": CoreDescriptor Version=%u Revision=%u\n", CoreDescriptor.u32VBoxVersion, CoreDescriptor.u32VBoxRevision));
432
433 /*
434 * Compute the file layout (see pg_dbgf_vmcore).
435 */
436 uint64_t const offElfHdr = RTFileTell(hFile);
437 uint64_t const offNoteSection = offElfHdr + sizeof(Elf64_Ehdr);
438 uint64_t const offLoadSections = offNoteSection + sizeof(Elf64_Phdr);
439 uint64_t const cbLoadSections = cMemRanges * sizeof(Elf64_Phdr);
440 uint64_t const offCoreDescriptor = offLoadSections + cbLoadSections;
441 uint64_t const cbCoreDescriptor = Elf64NoteSectionSize(g_pcszCoreVBoxCore, sizeof(CoreDescriptor));
442 uint64_t const offCpuDumps = offCoreDescriptor + cbCoreDescriptor;
443 uint64_t const cbCpuDumps = pVM->cCpus * Elf64NoteSectionSize(g_pcszCoreVBoxCpu, sizeof(DBGFCORECPU));
444 uint64_t const offMemory = offCpuDumps + cbCpuDumps;
445
446 uint64_t const offNoteSectionData = offCoreDescriptor;
447 uint64_t const cbNoteSectionData = cbCoreDescriptor + cbCpuDumps;
448
449 /*
450 * Write ELF header.
451 */
452 int rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */);
453 if (RT_FAILURE(rc))
454 {
455 LogRel((DBGFLOG_NAME ": Elf64WriteElfHdr failed. rc=%Rrc\n", rc));
456 return rc;
457 }
458
459 /*
460 * Write PT_NOTE program header.
461 */
462 Assert(RTFileTell(hFile) == offNoteSection);
463 rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
464 offNoteSectionData, /* file offset to contents */
465 cbNoteSectionData, /* size in core file */
466 cbNoteSectionData, /* size in memory */
467 0); /* physical address */
468 if (RT_FAILURE(rc))
469 {
470 LogRel((DBGFLOG_NAME ": Elf64WritreProgHdr failed for PT_NOTE. rc=%Rrc\n", rc));
471 return rc;
472 }
473
474 /*
475 * Write PT_LOAD program header for each memory range.
476 */
477 Assert(RTFileTell(hFile) == offLoadSections);
478 uint64_t offMemRange = offMemory;
479 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
480 {
481 RTGCPHYS GCPhysStart;
482 RTGCPHYS GCPhysEnd;
483 bool fIsMmio;
484 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
485 if (RT_FAILURE(rc))
486 {
487 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
488 return rc;
489 }
490
491 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
492 uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
493
494 Log((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
495 iRange, GCPhysStart, GCPhysEnd, cbMemRange));
496
497 rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
498 offMemRange, /* file offset to contents */
499 cbFileRange, /* size in core file */
500 cbMemRange, /* size in memory */
501 GCPhysStart); /* physical address */
502 if (RT_FAILURE(rc))
503 {
504 LogRel((DBGFLOG_NAME ": Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n",
505 iRange, cbFileRange, cbMemRange, rc));
506 return rc;
507 }
508
509 offMemRange += cbFileRange;
510 }
511
512 /*
513 * Write the Core descriptor note header and data.
514 */
515 Assert(RTFileTell(hFile) == offCoreDescriptor);
516 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, g_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor));
517 if (RT_FAILURE(rc))
518 {
519 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for Note '%s' rc=%Rrc\n", g_pcszCoreVBoxCore, rc));
520 return rc;
521 }
522
523 /*
524 * Write the CPU context note headers and data.
525 * We allocate the DBGFCORECPU struct. rather than using the stack as it can be pretty large due to X86XSAVEAREA.
526 */
527 Assert(RTFileTell(hFile) == offCpuDumps);
528 PDBGFCORECPU pDbgfCoreCpu = (PDBGFCORECPU)RTMemAlloc(sizeof(*pDbgfCoreCpu));
529 if (RT_UNLIKELY(!pDbgfCoreCpu))
530 {
531 LogRel((DBGFLOG_NAME ": Failed to alloc %u bytes for DBGFCORECPU\n", sizeof(*pDbgfCoreCpu)));
532 return VERR_NO_MEMORY;
533 }
534
535 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
536 {
537 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
538 RT_BZERO(pDbgfCoreCpu, sizeof(*pDbgfCoreCpu));
539 dbgfR3GetCoreCpu(pVCpu, pDbgfCoreCpu);
540
541 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, g_pcszCoreVBoxCpu, pDbgfCoreCpu, sizeof(*pDbgfCoreCpu));
542 if (RT_FAILURE(rc))
543 {
544 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", idCpu, rc));
545 RTMemFree(pDbgfCoreCpu);
546 return rc;
547 }
548 }
549 RTMemFree(pDbgfCoreCpu);
550 pDbgfCoreCpu = NULL;
551
552 /*
553 * Write memory ranges.
554 */
555 Assert(RTFileTell(hFile) == offMemory);
556 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
557 {
558 RTGCPHYS GCPhysStart;
559 RTGCPHYS GCPhysEnd;
560 bool fIsMmio;
561 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
562 if (RT_FAILURE(rc))
563 {
564 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange(2) failed for iRange(%u) rc=%Rrc\n", iRange, rc));
565 return rc;
566 }
567
568 if (fIsMmio)
569 continue;
570
571 /*
572 * Write page-by-page of this memory range.
573 *
574 * The read function may fail on MMIO ranges, we write these as zero
575 * pages for now (would be nice to have the VGA bits there though).
576 */
577 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
578 uint64_t cPages = cbMemRange >> GUEST_PAGE_SHIFT;
579 for (uint64_t iPage = 0; iPage < cPages; iPage++)
580 {
581 uint8_t abPage[GUEST_PAGE_SIZE];
582 rc = PGMPhysSimpleReadGCPhys(pVM, abPage, GCPhysStart + (iPage << GUEST_PAGE_SHIFT), sizeof(abPage));
583 if (RT_FAILURE(rc))
584 {
585 if (rc != VERR_PGM_PHYS_PAGE_RESERVED)
586 LogRel((DBGFLOG_NAME ": PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange, iPage, rc));
587 RT_ZERO(abPage);
588 }
589
590 rc = RTFileWrite(hFile, abPage, sizeof(abPage), NULL /* all */);
591 if (RT_FAILURE(rc))
592 {
593 LogRel((DBGFLOG_NAME ": RTFileWrite failed. iRange=%u iPage=%u rc=%Rrc\n", iRange, iPage, rc));
594 return rc;
595 }
596 }
597 }
598
599 return rc;
600}
601
602
603/**
604 * EMT Rendezvous worker function for DBGFR3CoreWrite().
605 *
606 * @param pVM The cross context VM structure.
607 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
608 * @param pvData Opaque data.
609 *
610 * @return VBox status code.
611 */
612static DECLCALLBACK(VBOXSTRICTRC) dbgfR3CoreWriteRendezvous(PVM pVM, PVMCPU pVCpu, void *pvData)
613{
614 /*
615 * Validate input.
616 */
617 AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
618 AssertReturn(pVCpu, VERR_INVALID_VMCPU_HANDLE);
619 AssertReturn(pvData, VERR_INVALID_POINTER);
620
621 PDBGFCOREDATA pDbgfData = (PDBGFCOREDATA)pvData;
622
623 /*
624 * Create the core file.
625 */
626 uint32_t fFlags = (pDbgfData->fReplaceFile ? RTFILE_O_CREATE_REPLACE : RTFILE_O_CREATE)
627 | RTFILE_O_WRITE
628 | RTFILE_O_DENY_ALL
629 | (0600 << RTFILE_O_CREATE_MODE_SHIFT);
630 RTFILE hFile;
631 int rc = RTFileOpen(&hFile, pDbgfData->pszFilename, fFlags);
632 if (RT_SUCCESS(rc))
633 {
634 rc = dbgfR3CoreWriteWorker(pVM, hFile);
635 RTFileClose(hFile);
636 }
637 else
638 LogRel((DBGFLOG_NAME ": RTFileOpen failed for '%s' rc=%Rrc\n", pDbgfData->pszFilename, rc));
639 return rc;
640}
641
642
643/**
644 * Write core dump of the guest.
645 *
646 * @returns VBox status code.
647 * @param pUVM The user mode VM handle.
648 * @param pszFilename The name of the file to which the guest core
649 * dump should be written.
650 * @param fReplaceFile Whether to replace the file or not.
651 *
652 * @remarks The VM may need to be suspended before calling this function in
653 * order to truly stop all device threads and drivers. This function
654 * only synchronizes EMTs.
655 */
656VMMR3DECL(int) DBGFR3CoreWrite(PUVM pUVM, const char *pszFilename, bool fReplaceFile)
657{
658 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
659 PVM pVM = pUVM->pVM;
660 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
661 AssertReturn(pszFilename, VERR_INVALID_HANDLE);
662
663 /*
664 * Pass the core write request down to EMT rendezvous which makes sure
665 * other EMTs, if any, are not running. IO threads could still be running
666 * but we don't care about them.
667 */
668 DBGFCOREDATA CoreData;
669 RT_ZERO(CoreData);
670 CoreData.pszFilename = pszFilename;
671 CoreData.fReplaceFile = fReplaceFile;
672
673 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWriteRendezvous, &CoreData);
674 if (RT_SUCCESS(rc))
675 LogRel((DBGFLOG_NAME ": Successfully wrote guest core dump '%s'\n", pszFilename));
676 else
677 LogRel((DBGFLOG_NAME ": Failed to write guest core dump '%s'. rc=%Rrc\n", pszFilename, rc));
678 return rc;
679}
680
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette