1 | /* $Id: DBGFCoreWrite.cpp 32220 2010-09-02 18:27:25Z 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 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 | * Per-CPU register dump
|
---|
31 | * - CPU 1 Note Hdr + Data
|
---|
32 | * - CPU 2 Note Hdr + Data
|
---|
33 | * ...
|
---|
34 | * (Additional custom notes Hdr+data)
|
---|
35 | * - VBox 1 Note Hdr + Data
|
---|
36 | * - VBox 2 Note Hdr + Data
|
---|
37 | * ...
|
---|
38 | * Memory dump
|
---|
39 | *
|
---|
40 | */
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Header Files *
|
---|
44 | *******************************************************************************/
|
---|
45 | #define LOG_GROUP LOG_GROUP_DBGF
|
---|
46 | #include <iprt/param.h>
|
---|
47 | #include <iprt/file.h>
|
---|
48 |
|
---|
49 | #include "DBGFInternal.h"
|
---|
50 |
|
---|
51 | #include <VBox/cpum.h>
|
---|
52 | #include "CPUMInternal.h"
|
---|
53 | #include <VBox/dbgf.h>
|
---|
54 | #include <VBox/vm.h>
|
---|
55 | #include <VBox/pgm.h>
|
---|
56 | #include <VBox/err.h>
|
---|
57 | #include <VBox/log.h>
|
---|
58 | #include <VBox/mm.h>
|
---|
59 |
|
---|
60 | #include "../Runtime/include/internal/ldrELF64.h"
|
---|
61 |
|
---|
62 | /*******************************************************************************
|
---|
63 | * Defined Constants And Macros *
|
---|
64 | *******************************************************************************/
|
---|
65 | #ifdef DEBUG_ramshankar
|
---|
66 | # undef Log
|
---|
67 | # define Log LogRel
|
---|
68 | #endif
|
---|
69 | #define DBGFLOG_NAME "DGBFCoreWrite"
|
---|
70 |
|
---|
71 | static const int s_NoteAlign = 4;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * DBGFCOREDATA: Core data.
|
---|
75 | */
|
---|
76 | typedef struct
|
---|
77 | {
|
---|
78 | const char *pszDumpPath; /* File path to dump the core into. */
|
---|
79 | } DBGFCOREDATA, *PDBGFCOREDATA;
|
---|
80 |
|
---|
81 | typedef struct
|
---|
82 | {
|
---|
83 | Elf64_Nhdr Hdr; /* 64-bit NOTE Header */
|
---|
84 | char achName[8]; /* Name of NOTE section */
|
---|
85 | } ELFNOTEHDR;
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * ELF function to write 64-bit ELF header.
|
---|
90 | *
|
---|
91 | * @param hFile The file to write to.
|
---|
92 | * @param cProgHdrs Number of program headers.
|
---|
93 | * @param cSecHdrs Number of section headers.
|
---|
94 | * @param pcbElfHdr Where to store the size of written header to file,
|
---|
95 | * can be NULL.
|
---|
96 | *
|
---|
97 | * @return IPRT status code.
|
---|
98 | */
|
---|
99 | static int Elf64WriteElfHdr(RTFILE hFile, uint16_t cProgHdrs, uint16_t cSecHdrs, uint64_t *pcbElfHdr)
|
---|
100 | {
|
---|
101 | Elf64_Ehdr ElfHdr;
|
---|
102 | RT_ZERO(ElfHdr);
|
---|
103 | ElfHdr.e_ident[EI_MAG0] = ELFMAG0;
|
---|
104 | ElfHdr.e_ident[EI_MAG1] = ELFMAG1;
|
---|
105 | ElfHdr.e_ident[EI_MAG2] = ELFMAG2;
|
---|
106 | ElfHdr.e_ident[EI_MAG3] = ELFMAG3;
|
---|
107 | ElfHdr.e_ident[EI_DATA] = ELFDATA2LSB;
|
---|
108 | ElfHdr.e_type = ET_CORE;
|
---|
109 | ElfHdr.e_version = EV_CURRENT;
|
---|
110 | ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
|
---|
111 | /* 32-bit VMs will produce cores with e_machine EM_386. */
|
---|
112 | #ifdef RT_ARCH_AMD64
|
---|
113 | ElfHdr.e_machine = EM_X86_64;
|
---|
114 | #else
|
---|
115 | ElfHdr.e_machine = EM_386;
|
---|
116 | #endif
|
---|
117 | ElfHdr.e_phnum = cProgHdrs;
|
---|
118 | ElfHdr.e_shnum = cSecHdrs;
|
---|
119 | ElfHdr.e_ehsize = sizeof(ElfHdr);
|
---|
120 | ElfHdr.e_phoff = sizeof(ElfHdr);
|
---|
121 | ElfHdr.e_phentsize = sizeof(Elf64_Phdr);
|
---|
122 | ElfHdr.e_shentsize = sizeof(Elf64_Shdr);
|
---|
123 |
|
---|
124 | int rc = RTFileWrite(hFile, &ElfHdr, sizeof(ElfHdr), NULL /* full write */);
|
---|
125 | if (RT_SUCCESS(rc) && pcbElfHdr)
|
---|
126 | *pcbElfHdr = sizeof(ElfHdr);
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * ELF function to write 64-bit program header.
|
---|
133 | *
|
---|
134 | * @param hFile The file to write to.
|
---|
135 | * @param Type Type of program header (PT_*).
|
---|
136 | * @param fFlags Flags (access permissions, PF_*).
|
---|
137 | * @param offFileData File offset of contents.
|
---|
138 | * @param cbFileData Size of contents in the file.
|
---|
139 | * @param cbMemData Size of contents in memory.
|
---|
140 | * @param Phys Physical address, pass zero if not applicable.
|
---|
141 | * @param pcbProgHdr Where to store the size of written header to file,
|
---|
142 | * can be NULL.
|
---|
143 | *
|
---|
144 | * @return IPRT status code.
|
---|
145 | */
|
---|
146 | static int Elf64WriteProgHdr(RTFILE hFile, uint32_t Type, uint32_t fFlags, uint64_t offFileData, uint64_t cbFileData, uint64_t cbMemData,
|
---|
147 | RTGCPHYS Phys, uint64_t *pcbProgHdr)
|
---|
148 | {
|
---|
149 | Elf64_Phdr ProgHdr;
|
---|
150 | RT_ZERO(ProgHdr);
|
---|
151 | ProgHdr.p_type = Type;
|
---|
152 | ProgHdr.p_flags = fFlags;
|
---|
153 | ProgHdr.p_offset = offFileData;
|
---|
154 | ProgHdr.p_filesz = cbFileData;
|
---|
155 | ProgHdr.p_memsz = cbMemData;
|
---|
156 | ProgHdr.p_paddr = Phys;
|
---|
157 |
|
---|
158 | int rc = RTFileWrite(hFile, &ProgHdr, sizeof(ProgHdr), NULL /* full write */);
|
---|
159 | if (RT_SUCCESS(rc) && pcbProgHdr)
|
---|
160 | *pcbProgHdr = sizeof(ProgHdr);
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Returns the size of the NOTE section given the size of the data.
|
---|
167 | *
|
---|
168 | * @param cb Size of the data part of the NOTE.
|
---|
169 | *
|
---|
170 | * @return The size of the NOTE section as rounded to the file alignment.
|
---|
171 | */
|
---|
172 | static inline int Elf64NoteSectionSize(uint64_t cb)
|
---|
173 | {
|
---|
174 | return sizeof(ELFNOTEHDR) + RT_ALIGN_64(cb, s_NoteAlign);
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Elf function to write 64-bit note header.
|
---|
180 | *
|
---|
181 | * @param hFile The file to write to.
|
---|
182 | * @param Type Type of this section.
|
---|
183 | * @param pszName Name of this section, will be limited to 16 bytes.
|
---|
184 | * @param pcv Opaque pointer to the data, if NULL only computes size.
|
---|
185 | * @param cb Size of the data.
|
---|
186 | * @param pcbNoteHdr Where to store the size of written header to file,
|
---|
187 | * can be NULL.
|
---|
188 | *
|
---|
189 | * @return IPRT status code.
|
---|
190 | */
|
---|
191 | static int Elf64WriteNoteHdr(RTFILE hFile, uint16_t Type, const char *pszName, const void *pcv, uint64_t cb, uint64_t *pcbNoteHdr)
|
---|
192 | {
|
---|
193 | AssertReturn(pcv, VERR_INVALID_POINTER);
|
---|
194 | AssertReturn(cb > 0, VERR_NO_DATA);
|
---|
195 |
|
---|
196 | ELFNOTEHDR ElfNoteHdr;
|
---|
197 | RT_ZERO(ElfNoteHdr);
|
---|
198 | RTStrCopy(ElfNoteHdr.achName, sizeof(ElfNoteHdr.achName), pszName);
|
---|
199 | ElfNoteHdr.Hdr.n_namesz = (Elf64_Word)strlen(ElfNoteHdr.achName);
|
---|
200 | ElfNoteHdr.Hdr.n_type = Type;
|
---|
201 | size_t cbNameAlign = RT_ALIGN_Z(ElfNoteHdr.Hdr.n_namesz, 8);
|
---|
202 |
|
---|
203 | static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
|
---|
204 | uint64_t cbAlign = RT_ALIGN_64(cb, s_NoteAlign);
|
---|
205 | ElfNoteHdr.Hdr.n_descsz = (Elf64_Word)cbAlign;
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Write note header and description.
|
---|
209 | */
|
---|
210 | int rc = RTFileWrite(hFile, &ElfNoteHdr, sizeof(ElfNoteHdr), NULL /* full write */);
|
---|
211 | if (RT_SUCCESS(rc))
|
---|
212 | {
|
---|
213 | if (cbNameAlign > ElfNoteHdr.Hdr.n_namesz)
|
---|
214 | rc = RTFileWrite(hFile, s_achPad, cbNameAlign - ElfNoteHdr.Hdr.n_namesz, NULL);
|
---|
215 |
|
---|
216 | rc = RTFileWrite(hFile, pcv, cb, NULL /* full write */);
|
---|
217 | if (RT_SUCCESS(rc))
|
---|
218 | {
|
---|
219 | if (cbAlign > cb)
|
---|
220 | rc = RTFileWrite(hFile, s_achPad, cbAlign - cb, NULL /* full write*/);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (RT_FAILURE(rc))
|
---|
225 | LogRel((DBGFLOG_NAME ":RTFileWrite failed. rc=%Rrc pszName=%s cb=%u cbAlign=%u\n", rc, pszName, cb, cbAlign));
|
---|
226 |
|
---|
227 | return rc;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Count the number of memory ranges that go into the core file.
|
---|
233 | *
|
---|
234 | * We cannot do a page-by-page dump of the entire guest memory as there will be
|
---|
235 | * way too many program header entries. Also we don't want to dump MMIO regions
|
---|
236 | * which means we cannot have a 1:1 mapping between core file offset and memory
|
---|
237 | * offset. Instead we dump the memory in ranges. A memory range is a contiguous
|
---|
238 | * memory area suitable for dumping to a core file.
|
---|
239 | *
|
---|
240 | * @param pVM The VM handle.
|
---|
241 | *
|
---|
242 | * @return Number of memory ranges
|
---|
243 | */
|
---|
244 | static uint32_t dbgfR3GetRamRangeCount(PVM pVM)
|
---|
245 | {
|
---|
246 | return PGMR3PhysGetRamRangeCount(pVM);
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * EMT Rendezvous worker function for DBGFR3CoreWrite.
|
---|
252 | *
|
---|
253 | * @param pVM The VM handle.
|
---|
254 | * @param pVCpu The handle of the calling VCPU.
|
---|
255 | * @param pvData Opaque data.
|
---|
256 | *
|
---|
257 | * @return VBox status code.
|
---|
258 | */
|
---|
259 | static DECLCALLBACK(VBOXSTRICTRC) dbgfR3CoreWrite(PVM pVM, PVMCPU pVCpu, void *pvData)
|
---|
260 | {
|
---|
261 | /*
|
---|
262 | * Validate input.
|
---|
263 | */
|
---|
264 | AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
|
---|
265 | AssertReturn(pVCpu, VERR_INVALID_VMCPU_HANDLE);
|
---|
266 | AssertReturn(pvData, VERR_INVALID_POINTER);
|
---|
267 |
|
---|
268 | PDBGFCOREDATA pDbgfData = (PDBGFCOREDATA)pvData;
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Collect core information.
|
---|
272 | */
|
---|
273 | uint32_t u32MemRanges = dbgfR3GetRamRangeCount(pVM);
|
---|
274 | uint16_t cMemRanges = u32MemRanges < UINT16_MAX - 1 ? u32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
|
---|
275 | uint16_t cProgHdrs = cMemRanges + 1;
|
---|
276 |
|
---|
277 | /*
|
---|
278 | * Compute size of the note section.
|
---|
279 | */
|
---|
280 | uint64_t cbNoteSection = Elf64NoteSectionSize(pVM->cCpus * sizeof(CPUMCTX));
|
---|
281 | uint64_t off = 0;
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Create the core file.
|
---|
285 | */
|
---|
286 | RTFILE hFile = NIL_RTFILE;
|
---|
287 | int rc = RTFileOpen(&hFile, pDbgfData->pszDumpPath, RTFILE_O_CREATE_REPLACE | RTFILE_O_READWRITE);
|
---|
288 | if (RT_SUCCESS(rc))
|
---|
289 | {
|
---|
290 | /*
|
---|
291 | * Write ELF header.
|
---|
292 | */
|
---|
293 | uint64_t cbElfHdr = 0;
|
---|
294 | rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */, &cbElfHdr);
|
---|
295 | off += cbElfHdr;
|
---|
296 | if (RT_SUCCESS(rc))
|
---|
297 | {
|
---|
298 | /*
|
---|
299 | * Write PT_NOTE program header.
|
---|
300 | */
|
---|
301 | uint64_t cbProgHdr = 0;
|
---|
302 | rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
|
---|
303 | cbElfHdr + cProgHdrs * sizeof(Elf64_Phdr), /* file offset to contents */
|
---|
304 | cbNoteSection, /* size in core file */
|
---|
305 | cbNoteSection, /* size in memory */
|
---|
306 | 0, /* physical address */
|
---|
307 | &cbProgHdr);
|
---|
308 | Assert(cbProgHdr == sizeof(Elf64_Phdr));
|
---|
309 | off += cbProgHdr;
|
---|
310 | if (RT_SUCCESS(rc))
|
---|
311 | {
|
---|
312 | /*
|
---|
313 | * Write PT_LOAD program header for each memory range.
|
---|
314 | */
|
---|
315 | uint64_t offMemRange = off + cbNoteSection;
|
---|
316 | for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
|
---|
317 | {
|
---|
318 | RTGCPHYS GCPhysStart;
|
---|
319 | RTGCPHYS GCPhysEnd;
|
---|
320 |
|
---|
321 | bool fIsMmio;
|
---|
322 | rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
|
---|
323 | if (RT_FAILURE(rc))
|
---|
324 | {
|
---|
325 | LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
|
---|
326 | break;
|
---|
327 | }
|
---|
328 |
|
---|
329 | uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
|
---|
330 | uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
|
---|
331 |
|
---|
332 | LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
|
---|
333 | iRange, GCPhysStart, GCPhysEnd, cbMemRange));
|
---|
334 |
|
---|
335 | rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
|
---|
336 | offMemRange, /* file offset to contents */
|
---|
337 | cbFileRange, /* size in core file */
|
---|
338 | cbMemRange, /* size in memory */
|
---|
339 | GCPhysStart, /* physical address */
|
---|
340 | &cbProgHdr);
|
---|
341 | Assert(cbProgHdr == sizeof(Elf64_Phdr));
|
---|
342 | if (RT_FAILURE(rc))
|
---|
343 | {
|
---|
344 | LogRel((DBGFLOG_NAME ":Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n", iRange,
|
---|
345 | cbFileRange, cbMemRange, rc));
|
---|
346 | break;
|
---|
347 | }
|
---|
348 |
|
---|
349 | offMemRange += cbFileRange;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Write the CPU context note headers and data.
|
---|
354 | */
|
---|
355 | if (RT_SUCCESS(rc))
|
---|
356 | {
|
---|
357 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
358 | {
|
---|
359 | PCPUMCTX pCpuCtx = &pVM->aCpus[i].cpum.s.Guest;
|
---|
360 | rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, "VBOXCPU", pCpuCtx, sizeof(CPUMCTX), NULL /* pcbNoteHdr */);
|
---|
361 | if (RT_FAILURE(rc))
|
---|
362 | {
|
---|
363 | LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", i, rc));
|
---|
364 | break;
|
---|
365 | }
|
---|
366 | }
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | }
|
---|
371 |
|
---|
372 | RTFileClose(hFile);
|
---|
373 | }
|
---|
374 |
|
---|
375 | return rc;
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Write core dump of the guest.
|
---|
381 | *
|
---|
382 | * @return VBox status code.
|
---|
383 | * @param pVM The VM handle.
|
---|
384 | * @param idCpu The target CPU ID.
|
---|
385 | * @param pszDumpPath The path of the file to dump into, cannot be
|
---|
386 | * NULL.
|
---|
387 | *
|
---|
388 | * @remarks The VM must be suspended before calling this function.
|
---|
389 | */
|
---|
390 | VMMR3DECL(int) DBGFR3CoreWrite(PVM pVM, VMCPUID idCpu, const char *pszDumpPath)
|
---|
391 | {
|
---|
392 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
393 | AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
394 | AssertReturn(pszDumpPath, VERR_INVALID_HANDLE);
|
---|
395 |
|
---|
396 | /*
|
---|
397 | * Pass the core write request down to EMT rendezvous which makes sure
|
---|
398 | * other EMTs, if any, are not running.
|
---|
399 | */
|
---|
400 | DBGFCOREDATA CoreData;
|
---|
401 | RT_ZERO(CoreData);
|
---|
402 | CoreData.pszDumpPath = pszDumpPath;
|
---|
403 |
|
---|
404 | return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWrite, &CoreData);
|
---|
405 | }
|
---|
406 |
|
---|