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