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