1 | /* $Id: DBGFCoreWrite.cpp 38736 2011-09-13 13:58:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGF - Debugger Facility, Guest Core Dump.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 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 |
|
---|
54 | #include "DBGFInternal.h"
|
---|
55 |
|
---|
56 | #include <VBox/vmm/cpum.h>
|
---|
57 | #include "CPUMInternal.h"
|
---|
58 | #include <VBox/vmm/dbgf.h>
|
---|
59 | #include <VBox/vmm/dbgfcorefmt.h>
|
---|
60 | #include <VBox/vmm/vm.h>
|
---|
61 | #include <VBox/vmm/pgm.h>
|
---|
62 | #include <VBox/err.h>
|
---|
63 | #include <VBox/log.h>
|
---|
64 | #include <VBox/vmm/mm.h>
|
---|
65 | #include <VBox/version.h>
|
---|
66 |
|
---|
67 | #include "../../Runtime/include/internal/ldrELF64.h"
|
---|
68 |
|
---|
69 |
|
---|
70 | /*******************************************************************************
|
---|
71 | * Defined Constants And Macros *
|
---|
72 | *******************************************************************************/
|
---|
73 | #define DBGFLOG_NAME "DBGFCoreWrite"
|
---|
74 |
|
---|
75 |
|
---|
76 | /*******************************************************************************
|
---|
77 | * Global Variables *
|
---|
78 | *******************************************************************************/
|
---|
79 | static const int s_NoteAlign = 8;
|
---|
80 | static const int s_cbNoteName = 16;
|
---|
81 |
|
---|
82 | /* These strings *HAVE* to be 8-byte aligned */
|
---|
83 | static const char *s_pcszCoreVBoxCore = "VBCORE";
|
---|
84 | static const char *s_pcszCoreVBoxCpu = "VBCPU";
|
---|
85 |
|
---|
86 |
|
---|
87 | /*******************************************************************************
|
---|
88 | * Structures and Typedefs *
|
---|
89 | *******************************************************************************/
|
---|
90 | /**
|
---|
91 | * Guest core writer data.
|
---|
92 | *
|
---|
93 | * Used to pass parameters from DBGFR3CoreWrite to dbgfR3CoreWriteRendezvous.
|
---|
94 | */
|
---|
95 | typedef struct DBGFCOREDATA
|
---|
96 | {
|
---|
97 | /** The name of the file to write the file to. */
|
---|
98 | const char *pszFilename;
|
---|
99 | /** Whether to replace (/overwrite) any existing file. */
|
---|
100 | bool fReplaceFile;
|
---|
101 | } DBGFCOREDATA;
|
---|
102 | /** Pointer to the guest core writer data. */
|
---|
103 | typedef DBGFCOREDATA *PDBGFCOREDATA;
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * ELF function to write 64-bit ELF header.
|
---|
109 | *
|
---|
110 | * @param hFile The file to write to.
|
---|
111 | * @param cProgHdrs Number of program headers.
|
---|
112 | * @param cSecHdrs Number of section headers.
|
---|
113 | *
|
---|
114 | * @return IPRT status code.
|
---|
115 | */
|
---|
116 | static int Elf64WriteElfHdr(RTFILE hFile, uint16_t cProgHdrs, uint16_t cSecHdrs)
|
---|
117 | {
|
---|
118 | Elf64_Ehdr ElfHdr;
|
---|
119 | RT_ZERO(ElfHdr);
|
---|
120 | ElfHdr.e_ident[EI_MAG0] = ELFMAG0;
|
---|
121 | ElfHdr.e_ident[EI_MAG1] = ELFMAG1;
|
---|
122 | ElfHdr.e_ident[EI_MAG2] = ELFMAG2;
|
---|
123 | ElfHdr.e_ident[EI_MAG3] = ELFMAG3;
|
---|
124 | ElfHdr.e_ident[EI_DATA] = ELFDATA2LSB;
|
---|
125 | ElfHdr.e_type = ET_CORE;
|
---|
126 | ElfHdr.e_version = EV_CURRENT;
|
---|
127 | ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
|
---|
128 | /* 32-bit builds will produce cores with e_machine EM_386. */
|
---|
129 | #ifdef RT_ARCH_AMD64
|
---|
130 | ElfHdr.e_machine = EM_X86_64;
|
---|
131 | #else
|
---|
132 | ElfHdr.e_machine = EM_386;
|
---|
133 | #endif
|
---|
134 | ElfHdr.e_phnum = cProgHdrs;
|
---|
135 | ElfHdr.e_shnum = cSecHdrs;
|
---|
136 | ElfHdr.e_ehsize = sizeof(ElfHdr);
|
---|
137 | ElfHdr.e_phoff = sizeof(ElfHdr);
|
---|
138 | ElfHdr.e_phentsize = sizeof(Elf64_Phdr);
|
---|
139 | ElfHdr.e_shentsize = sizeof(Elf64_Shdr);
|
---|
140 |
|
---|
141 | return RTFileWrite(hFile, &ElfHdr, sizeof(ElfHdr), NULL /* all */);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * ELF function to write 64-bit program header.
|
---|
147 | *
|
---|
148 | * @param hFile The file to write to.
|
---|
149 | * @param Type Type of program header (PT_*).
|
---|
150 | * @param fFlags Flags (access permissions, PF_*).
|
---|
151 | * @param offFileData File offset of contents.
|
---|
152 | * @param cbFileData Size of contents in the file.
|
---|
153 | * @param cbMemData Size of contents in memory.
|
---|
154 | * @param Phys Physical address, pass zero if not applicable.
|
---|
155 | *
|
---|
156 | * @return IPRT status code.
|
---|
157 | */
|
---|
158 | static int Elf64WriteProgHdr(RTFILE hFile, uint32_t Type, uint32_t fFlags, uint64_t offFileData, uint64_t cbFileData,
|
---|
159 | uint64_t cbMemData, RTGCPHYS Phys)
|
---|
160 | {
|
---|
161 | Elf64_Phdr ProgHdr;
|
---|
162 | RT_ZERO(ProgHdr);
|
---|
163 | ProgHdr.p_type = Type;
|
---|
164 | ProgHdr.p_flags = fFlags;
|
---|
165 | ProgHdr.p_offset = offFileData;
|
---|
166 | ProgHdr.p_filesz = cbFileData;
|
---|
167 | ProgHdr.p_memsz = cbMemData;
|
---|
168 | ProgHdr.p_paddr = Phys;
|
---|
169 |
|
---|
170 | return RTFileWrite(hFile, &ProgHdr, sizeof(ProgHdr), NULL /* all */);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Returns the size of the NOTE section given the name and size of the data.
|
---|
176 | *
|
---|
177 | * @param pszName Name of the note section.
|
---|
178 | * @param cb Size of the data portion of the note section.
|
---|
179 | *
|
---|
180 | * @return The size of the NOTE section as rounded to the file alignment.
|
---|
181 | */
|
---|
182 | static uint64_t Elf64NoteSectionSize(const char *pszName, uint64_t cbData)
|
---|
183 | {
|
---|
184 | uint64_t cbNote = sizeof(Elf64_Nhdr);
|
---|
185 |
|
---|
186 | size_t cchName = strlen(pszName) + 1;
|
---|
187 | size_t cchNameAlign = RT_ALIGN_Z(cchName, s_NoteAlign);
|
---|
188 |
|
---|
189 | cbNote += cchNameAlign;
|
---|
190 | cbNote += RT_ALIGN_64(cbData, s_NoteAlign);
|
---|
191 | return cbNote;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Elf function to write 64-bit note header.
|
---|
197 | *
|
---|
198 | * @param hFile The file to write to.
|
---|
199 | * @param Type Type of this section.
|
---|
200 | * @param pszName Name of this section.
|
---|
201 | * @param pcv Opaque pointer to the data, if NULL only computes size.
|
---|
202 | * @param cbData Size of the data.
|
---|
203 | *
|
---|
204 | * @return IPRT status code.
|
---|
205 | */
|
---|
206 | static int Elf64WriteNoteHdr(RTFILE hFile, uint16_t Type, const char *pszName, const void *pcvData, uint64_t cbData)
|
---|
207 | {
|
---|
208 | AssertReturn(pcvData, VERR_INVALID_POINTER);
|
---|
209 | AssertReturn(cbData > 0, VERR_NO_DATA);
|
---|
210 |
|
---|
211 | char szNoteName[s_cbNoteName];
|
---|
212 | RT_ZERO(szNoteName);
|
---|
213 | RTStrCopy(szNoteName, sizeof(szNoteName), pszName);
|
---|
214 |
|
---|
215 | size_t cchName = strlen(szNoteName) + 1;
|
---|
216 | size_t cchNameAlign = RT_ALIGN_Z(cchName, s_NoteAlign);
|
---|
217 | uint64_t cbDataAlign = RT_ALIGN_64(cbData, s_NoteAlign);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Yell loudly and bail if we are going to be writing a core file that is not compatible with
|
---|
221 | * both Solaris and the 64-bit ELF spec. which dictates 8-byte alignment. See #5211 comment 3.
|
---|
222 | */
|
---|
223 | if (cchNameAlign - cchName > 3)
|
---|
224 | {
|
---|
225 | LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr pszName=%s cchName=%u cchNameAlign=%u, cchName aligns to 4 not 8-bytes!\n", pszName, cchName,
|
---|
226 | cchNameAlign));
|
---|
227 | return VERR_INVALID_PARAMETER;
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (cbDataAlign - cbData > 3)
|
---|
231 | {
|
---|
232 | LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr pszName=%s cbData=%u cbDataAlign=%u, cbData aligns to 4 not 8-bytes!\n", pszName, cbData,
|
---|
233 | cbDataAlign));
|
---|
234 | return VERR_INVALID_PARAMETER;
|
---|
235 | }
|
---|
236 |
|
---|
237 | static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
|
---|
238 | AssertCompile(sizeof(s_achPad) >= s_NoteAlign - 1);
|
---|
239 |
|
---|
240 | Elf64_Nhdr ElfNoteHdr;
|
---|
241 | RT_ZERO(ElfNoteHdr);
|
---|
242 | ElfNoteHdr.n_namesz = (Elf64_Word)cchName - 1; /* Again a discrepancy between ELF-64 and Solaris (#5211 comment 3), we will follow ELF-64 */
|
---|
243 | ElfNoteHdr.n_type = Type;
|
---|
244 | ElfNoteHdr.n_descsz = (Elf64_Word)cbDataAlign;
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * Write note header.
|
---|
248 | */
|
---|
249 | int rc = RTFileWrite(hFile, &ElfNoteHdr, sizeof(ElfNoteHdr), NULL /* all */);
|
---|
250 | if (RT_SUCCESS(rc))
|
---|
251 | {
|
---|
252 | /*
|
---|
253 | * Write note name.
|
---|
254 | */
|
---|
255 | rc = RTFileWrite(hFile, szNoteName, cchName, NULL /* all */);
|
---|
256 | if (RT_SUCCESS(rc))
|
---|
257 | {
|
---|
258 | /*
|
---|
259 | * Write note name padding if required.
|
---|
260 | */
|
---|
261 | if (cchNameAlign > cchName)
|
---|
262 | rc = RTFileWrite(hFile, s_achPad, cchNameAlign - cchName, NULL);
|
---|
263 |
|
---|
264 | if (RT_SUCCESS(rc))
|
---|
265 | {
|
---|
266 | /*
|
---|
267 | * Write note data.
|
---|
268 | */
|
---|
269 | rc = RTFileWrite(hFile, pcvData, cbData, NULL /* all */);
|
---|
270 | if (RT_SUCCESS(rc))
|
---|
271 | {
|
---|
272 | /*
|
---|
273 | * Write note data padding if required.
|
---|
274 | */
|
---|
275 | if (cbDataAlign > cbData)
|
---|
276 | rc = RTFileWrite(hFile, s_achPad, cbDataAlign - cbData, NULL /* all*/);
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (RT_FAILURE(rc))
|
---|
283 | LogRel((DBGFLOG_NAME ": RTFileWrite failed. rc=%Rrc pszName=%s cchName=%u cchNameAlign=%u cbData=%u cbDataAlign=%u\n",
|
---|
284 | rc, pszName, cchName, cchNameAlign, cbData, cbDataAlign));
|
---|
285 |
|
---|
286 | return rc;
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Count the number of memory ranges that go into the core file.
|
---|
292 | *
|
---|
293 | * We cannot do a page-by-page dump of the entire guest memory as there will be
|
---|
294 | * way too many program header entries. Also we don't want to dump MMIO regions
|
---|
295 | * which means we cannot have a 1:1 mapping between core file offset and memory
|
---|
296 | * offset. Instead we dump the memory in ranges. A memory range is a contiguous
|
---|
297 | * memory area suitable for dumping to a core file.
|
---|
298 | *
|
---|
299 | * @param pVM The VM handle.
|
---|
300 | *
|
---|
301 | * @return Number of memory ranges
|
---|
302 | */
|
---|
303 | static uint32_t dbgfR3GetRamRangeCount(PVM pVM)
|
---|
304 | {
|
---|
305 | return PGMR3PhysGetRamRangeCount(pVM);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Worker function for dbgfR3CoreWrite which does the writing.
|
---|
311 | *
|
---|
312 | * @returns VBox status code
|
---|
313 | * @param pVM The VM handle.
|
---|
314 | * @param hFile The file to write to. Caller closes this.
|
---|
315 | */
|
---|
316 | static int dbgfR3CoreWriteWorker(PVM pVM, RTFILE hFile)
|
---|
317 | {
|
---|
318 | /*
|
---|
319 | * Collect core information.
|
---|
320 | */
|
---|
321 | uint32_t const cu32MemRanges = dbgfR3GetRamRangeCount(pVM);
|
---|
322 | uint16_t const cMemRanges = cu32MemRanges < UINT16_MAX - 1 ? cu32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
|
---|
323 | uint16_t const cProgHdrs = cMemRanges + 1;
|
---|
324 |
|
---|
325 | DBGFCOREDESCRIPTOR CoreDescriptor;
|
---|
326 | RT_ZERO(CoreDescriptor);
|
---|
327 | CoreDescriptor.u32Magic = DBGFCORE_MAGIC;
|
---|
328 | CoreDescriptor.u32FmtVersion = DBGFCORE_FMT_VERSION;
|
---|
329 | CoreDescriptor.cbSelf = sizeof(CoreDescriptor);
|
---|
330 | CoreDescriptor.u32VBoxVersion = VBOX_FULL_VERSION;
|
---|
331 | CoreDescriptor.u32VBoxRevision = VMMGetSvnRev();
|
---|
332 | CoreDescriptor.cCpus = pVM->cCpus;
|
---|
333 |
|
---|
334 | Log((DBGFLOG_NAME ": CoreDescriptor Version=%u Revision=%u\n", CoreDescriptor.u32VBoxVersion, CoreDescriptor.u32VBoxRevision));
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Compute the file layout (see pg_dbgf_vmcore).
|
---|
338 | */
|
---|
339 | uint64_t const offElfHdr = RTFileTell(hFile);
|
---|
340 | uint64_t const offNoteSection = offElfHdr + sizeof(Elf64_Ehdr);
|
---|
341 | uint64_t const offLoadSections = offNoteSection + sizeof(Elf64_Phdr);
|
---|
342 | uint64_t const cbLoadSections = cMemRanges * sizeof(Elf64_Phdr);
|
---|
343 | uint64_t const offCoreDescriptor= offLoadSections + cbLoadSections;
|
---|
344 | uint64_t const cbCoreDescriptor = Elf64NoteSectionSize(s_pcszCoreVBoxCore, sizeof(CoreDescriptor));
|
---|
345 | uint64_t const offCpuDumps = offCoreDescriptor + cbCoreDescriptor;
|
---|
346 | uint64_t const cbCpuDumps = pVM->cCpus * Elf64NoteSectionSize(s_pcszCoreVBoxCpu, sizeof(CPUMCTX));
|
---|
347 | uint64_t const offMemory = offCpuDumps + cbCpuDumps;
|
---|
348 |
|
---|
349 | uint64_t const offNoteSectionData = offCoreDescriptor;
|
---|
350 | uint64_t const cbNoteSectionData = cbCoreDescriptor + cbCpuDumps;
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Write ELF header.
|
---|
354 | */
|
---|
355 | int rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */);
|
---|
356 | if (RT_FAILURE(rc))
|
---|
357 | {
|
---|
358 | LogRel((DBGFLOG_NAME ": Elf64WriteElfHdr failed. rc=%Rrc\n", rc));
|
---|
359 | return rc;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * Write PT_NOTE program header.
|
---|
364 | */
|
---|
365 | Assert(RTFileTell(hFile) == offNoteSection);
|
---|
366 | rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
|
---|
367 | offNoteSectionData, /* file offset to contents */
|
---|
368 | cbNoteSectionData, /* size in core file */
|
---|
369 | cbNoteSectionData, /* size in memory */
|
---|
370 | 0); /* physical address */
|
---|
371 | if (RT_FAILURE(rc))
|
---|
372 | {
|
---|
373 | LogRel((DBGFLOG_NAME ": Elf64WritreProgHdr failed for PT_NOTE. rc=%Rrc\n", rc));
|
---|
374 | return rc;
|
---|
375 | }
|
---|
376 |
|
---|
377 | /*
|
---|
378 | * Write PT_LOAD program header for each memory range.
|
---|
379 | */
|
---|
380 | Assert(RTFileTell(hFile) == offLoadSections);
|
---|
381 | uint64_t offMemRange = offMemory;
|
---|
382 | for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
|
---|
383 | {
|
---|
384 | RTGCPHYS GCPhysStart;
|
---|
385 | RTGCPHYS GCPhysEnd;
|
---|
386 | bool fIsMmio;
|
---|
387 | rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
|
---|
388 | if (RT_FAILURE(rc))
|
---|
389 | {
|
---|
390 | LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
|
---|
391 | return rc;
|
---|
392 | }
|
---|
393 |
|
---|
394 | uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
|
---|
395 | uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
|
---|
396 |
|
---|
397 | Log((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
|
---|
398 | iRange, GCPhysStart, GCPhysEnd, cbMemRange));
|
---|
399 |
|
---|
400 | rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
|
---|
401 | offMemRange, /* file offset to contents */
|
---|
402 | cbFileRange, /* size in core file */
|
---|
403 | cbMemRange, /* size in memory */
|
---|
404 | GCPhysStart); /* physical address */
|
---|
405 | if (RT_FAILURE(rc))
|
---|
406 | {
|
---|
407 | LogRel((DBGFLOG_NAME ": Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n",
|
---|
408 | iRange, cbFileRange, cbMemRange, rc));
|
---|
409 | return rc;
|
---|
410 | }
|
---|
411 |
|
---|
412 | offMemRange += cbFileRange;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /*
|
---|
416 | * Write the Core descriptor note header and data.
|
---|
417 | */
|
---|
418 | Assert(RTFileTell(hFile) == offCoreDescriptor);
|
---|
419 | rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, s_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor));
|
---|
420 | if (RT_FAILURE(rc))
|
---|
421 | {
|
---|
422 | LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for Note '%s' rc=%Rrc\n", s_pcszCoreVBoxCore, rc));
|
---|
423 | return rc;
|
---|
424 | }
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * Write the CPU context note headers and data.
|
---|
428 | */
|
---|
429 | Assert(RTFileTell(hFile) == offCpuDumps);
|
---|
430 | for (uint32_t iCpu = 0; iCpu < pVM->cCpus; iCpu++)
|
---|
431 | {
|
---|
432 | PCPUMCTX pCpuCtx = &pVM->aCpus[iCpu].cpum.s.Guest;
|
---|
433 | rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, s_pcszCoreVBoxCpu, pCpuCtx, sizeof(CPUMCTX));
|
---|
434 | if (RT_FAILURE(rc))
|
---|
435 | {
|
---|
436 | LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", iCpu, rc));
|
---|
437 | return rc;
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * Write memory ranges.
|
---|
443 | */
|
---|
444 | Assert(RTFileTell(hFile) == offMemory);
|
---|
445 | for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
|
---|
446 | {
|
---|
447 | RTGCPHYS GCPhysStart;
|
---|
448 | RTGCPHYS GCPhysEnd;
|
---|
449 | bool fIsMmio;
|
---|
450 | rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
|
---|
451 | if (RT_FAILURE(rc))
|
---|
452 | {
|
---|
453 | LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange(2) failed for iRange(%u) rc=%Rrc\n", iRange, rc));
|
---|
454 | return rc;
|
---|
455 | }
|
---|
456 |
|
---|
457 | if (fIsMmio)
|
---|
458 | continue;
|
---|
459 |
|
---|
460 | /*
|
---|
461 | * Write page-by-page of this memory range.
|
---|
462 | *
|
---|
463 | * The read function may fail on MMIO ranges, we write these as zero
|
---|
464 | * pages for now (would be nice to have the VGA bits there though).
|
---|
465 | */
|
---|
466 | uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
|
---|
467 | uint64_t cPages = cbMemRange >> PAGE_SHIFT;
|
---|
468 | for (uint64_t iPage = 0; iPage < cPages; iPage++)
|
---|
469 | {
|
---|
470 | uint8_t abPage[PAGE_SIZE];
|
---|
471 | rc = PGMPhysSimpleReadGCPhys(pVM, abPage, GCPhysStart + (iPage << PAGE_SHIFT), sizeof(abPage));
|
---|
472 | if (RT_FAILURE(rc))
|
---|
473 | {
|
---|
474 | if (rc != VERR_PGM_PHYS_PAGE_RESERVED)
|
---|
475 | LogRel((DBGFLOG_NAME ": PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange, iPage, rc));
|
---|
476 | RT_ZERO(abPage);
|
---|
477 | }
|
---|
478 |
|
---|
479 | rc = RTFileWrite(hFile, abPage, sizeof(abPage), NULL /* all */);
|
---|
480 | if (RT_FAILURE(rc))
|
---|
481 | {
|
---|
482 | LogRel((DBGFLOG_NAME ": RTFileWrite failed. iRange=%u iPage=%u rc=%Rrc\n", iRange, iPage, rc));
|
---|
483 | return rc;
|
---|
484 | }
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | return rc;
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * EMT Rendezvous worker function for DBGFR3CoreWrite.
|
---|
494 | *
|
---|
495 | * @param pVM The VM handle.
|
---|
496 | * @param pVCpu The handle of the calling VCPU.
|
---|
497 | * @param pvData Opaque data.
|
---|
498 | *
|
---|
499 | * @return VBox status code.
|
---|
500 | */
|
---|
501 | static DECLCALLBACK(VBOXSTRICTRC) dbgfR3CoreWriteRendezvous(PVM pVM, PVMCPU pVCpu, void *pvData)
|
---|
502 | {
|
---|
503 | /*
|
---|
504 | * Validate input.
|
---|
505 | */
|
---|
506 | AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
|
---|
507 | AssertReturn(pVCpu, VERR_INVALID_VMCPU_HANDLE);
|
---|
508 | AssertReturn(pvData, VERR_INVALID_POINTER);
|
---|
509 |
|
---|
510 | PDBGFCOREDATA pDbgfData = (PDBGFCOREDATA)pvData;
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * Create the core file.
|
---|
514 | */
|
---|
515 | uint32_t fFlags = (pDbgfData->fReplaceFile ? RTFILE_O_CREATE_REPLACE : RTFILE_O_CREATE)
|
---|
516 | | RTFILE_O_WRITE
|
---|
517 | | RTFILE_O_DENY_ALL
|
---|
518 | | (0600 << RTFILE_O_CREATE_MODE_SHIFT);
|
---|
519 | RTFILE hFile;
|
---|
520 | int rc = RTFileOpen(&hFile, pDbgfData->pszFilename, fFlags);
|
---|
521 | if (RT_SUCCESS(rc))
|
---|
522 | {
|
---|
523 | rc = dbgfR3CoreWriteWorker(pVM, hFile);
|
---|
524 | RTFileClose(hFile);
|
---|
525 | }
|
---|
526 | else
|
---|
527 | LogRel((DBGFLOG_NAME ": RTFileOpen failed for '%s' rc=%Rrc\n", pDbgfData->pszFilename, rc));
|
---|
528 | return rc;
|
---|
529 | }
|
---|
530 |
|
---|
531 |
|
---|
532 | /**
|
---|
533 | * Write core dump of the guest.
|
---|
534 | *
|
---|
535 | * @returns VBox status code.
|
---|
536 | * @param pVM The VM handle.
|
---|
537 | * @param pszFilename The name of the file to which the guest core
|
---|
538 | * dump should be written.
|
---|
539 | * @param fReplaceFile Whether to replace the file or not.
|
---|
540 | *
|
---|
541 | * @remarks The VM should be suspended before calling this function or DMA may
|
---|
542 | * interfer with the state.
|
---|
543 | */
|
---|
544 | VMMR3DECL(int) DBGFR3CoreWrite(PVM pVM, const char *pszFilename, bool fReplaceFile)
|
---|
545 | {
|
---|
546 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
547 | AssertReturn(pszFilename, VERR_INVALID_HANDLE);
|
---|
548 |
|
---|
549 | /*
|
---|
550 | * Pass the core write request down to EMT rendezvous which makes sure
|
---|
551 | * other EMTs, if any, are not running. IO threads could still be running
|
---|
552 | * but we don't care about them.
|
---|
553 | */
|
---|
554 | DBGFCOREDATA CoreData;
|
---|
555 | RT_ZERO(CoreData);
|
---|
556 | CoreData.pszFilename = pszFilename;
|
---|
557 | CoreData.fReplaceFile = fReplaceFile;
|
---|
558 |
|
---|
559 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWriteRendezvous, &CoreData);
|
---|
560 | if (RT_SUCCESS(rc))
|
---|
561 | LogRel((DBGFLOG_NAME ": Successfully wrote guest core dump '%s'\n", pszFilename));
|
---|
562 | else
|
---|
563 | LogRel((DBGFLOG_NAME ": Failed to write guest core dump '%s'. rc=%Rrc\n", pszFilename, rc));
|
---|
564 | return rc;
|
---|
565 | }
|
---|
566 |
|
---|