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