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