VirtualBox

source: vbox/trunk/src/VBox/VMM/DBGFCoreWrite.cpp@ 32248

Last change on this file since 32248 was 32248, checked in by vboxsync, 14 years ago

Unused var.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 KB
Line 
1/* $Id: DBGFCoreWrite.cpp 32248 2010-09-06 12:45:20Z 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/*
72 * For now use Solaris-specific padding and namesz length (i.e. includes NULL terminator)
73 */
74static const int s_NoteAlign = 4; /* @todo see #5211 comment 3 */
75static const int s_cbNoteName = 16;
76static const char *s_pcszCoreVBoxCpu = "VBOXCPU";
77
78/**
79 * DBGFCOREDATA: Core data.
80 */
81typedef struct
82{
83 const char *pszDumpPath; /* File path to dump the core into. */
84} DBGFCOREDATA, *PDBGFCOREDATA;
85
86
87/**
88 * ELF function to write 64-bit ELF header.
89 *
90 * @param hFile The file to write to.
91 * @param cProgHdrs Number of program headers.
92 * @param cSecHdrs Number of section headers.
93 * @param pcbElfHdr Where to store the size of written header to file,
94 * can be NULL.
95 *
96 * @return IPRT status code.
97 */
98static int Elf64WriteElfHdr(RTFILE hFile, uint16_t cProgHdrs, uint16_t cSecHdrs, uint64_t *pcbElfHdr)
99{
100 Elf64_Ehdr ElfHdr;
101 RT_ZERO(ElfHdr);
102 ElfHdr.e_ident[EI_MAG0] = ELFMAG0;
103 ElfHdr.e_ident[EI_MAG1] = ELFMAG1;
104 ElfHdr.e_ident[EI_MAG2] = ELFMAG2;
105 ElfHdr.e_ident[EI_MAG3] = ELFMAG3;
106 ElfHdr.e_ident[EI_DATA] = ELFDATA2LSB;
107 ElfHdr.e_type = ET_CORE;
108 ElfHdr.e_version = EV_CURRENT;
109 ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
110 /* 32-bit VMs will produce cores with e_machine EM_386. */
111#ifdef RT_ARCH_AMD64
112 ElfHdr.e_machine = EM_X86_64;
113#else
114 ElfHdr.e_machine = EM_386;
115#endif
116 ElfHdr.e_phnum = cProgHdrs;
117 ElfHdr.e_shnum = cSecHdrs;
118 ElfHdr.e_ehsize = sizeof(ElfHdr);
119 ElfHdr.e_phoff = sizeof(ElfHdr);
120 ElfHdr.e_phentsize = sizeof(Elf64_Phdr);
121 ElfHdr.e_shentsize = sizeof(Elf64_Shdr);
122
123 int rc = RTFileWrite(hFile, &ElfHdr, sizeof(ElfHdr), NULL /* all */);
124 if (RT_SUCCESS(rc) && pcbElfHdr)
125 *pcbElfHdr = sizeof(ElfHdr);
126 return rc;
127}
128
129
130/**
131 * ELF function to write 64-bit program header.
132 *
133 * @param hFile The file to write to.
134 * @param Type Type of program header (PT_*).
135 * @param fFlags Flags (access permissions, PF_*).
136 * @param offFileData File offset of contents.
137 * @param cbFileData Size of contents in the file.
138 * @param cbMemData Size of contents in memory.
139 * @param Phys Physical address, pass zero if not applicable.
140 * @param pcbProgHdr Where to store the size of written header to file,
141 * can be NULL.
142 *
143 * @return IPRT status code.
144 */
145static int Elf64WriteProgHdr(RTFILE hFile, uint32_t Type, uint32_t fFlags, uint64_t offFileData, uint64_t cbFileData, uint64_t cbMemData,
146 RTGCPHYS Phys, uint64_t *pcbProgHdr)
147{
148 Elf64_Phdr ProgHdr;
149 RT_ZERO(ProgHdr);
150 ProgHdr.p_type = Type;
151 ProgHdr.p_flags = fFlags;
152 ProgHdr.p_offset = offFileData;
153 ProgHdr.p_filesz = cbFileData;
154 ProgHdr.p_memsz = cbMemData;
155 ProgHdr.p_paddr = Phys;
156
157 int rc = RTFileWrite(hFile, &ProgHdr, sizeof(ProgHdr), NULL /* all */);
158 if (RT_SUCCESS(rc) && pcbProgHdr)
159 *pcbProgHdr = sizeof(ProgHdr);
160 return rc;
161}
162
163
164/**
165 * Returns the size of the NOTE section given the name and size of the data.
166 *
167 * @param pszName Name of the note section.
168 * @param cb Size of the data portion of the note section.
169 *
170 * @return The size of the NOTE section as rounded to the file alignment.
171 */
172static inline uint64_t Elf64NoteSectionSize(const char *pszName, uint64_t cbData)
173{
174 uint64_t cbNote = sizeof(Elf64_Nhdr);
175
176 size_t cbName = strlen(pszName) + 1;
177 size_t cbNameAlign = RT_ALIGN_Z(cbName, s_NoteAlign);
178
179 cbNote += cbNameAlign;
180 cbNote += RT_ALIGN_64(cbData, s_NoteAlign);
181 return cbNote;
182}
183
184
185/**
186 * Elf function to write 64-bit note header.
187 *
188 * @param hFile The file to write to.
189 * @param Type Type of this section.
190 * @param pszName Name of this section.
191 * @param pcv Opaque pointer to the data, if NULL only computes size.
192 * @param cbData Size of the data.
193 * @param pcbNoteHdr Where to store the size of written header to file,
194 * can be NULL.
195 *
196 * @return IPRT status code.
197 */
198static int Elf64WriteNoteHdr(RTFILE hFile, uint16_t Type, const char *pszName, const void *pcvData, uint64_t cbData, uint64_t *pcbNoteHdr)
199{
200 AssertReturn(pcvData, VERR_INVALID_POINTER);
201 AssertReturn(cbData > 0, VERR_NO_DATA);
202
203 char szNoteName[s_cbNoteName];
204 RT_ZERO(szNoteName);
205 RTStrCopy(szNoteName, sizeof(szNoteName), pszName);
206
207 size_t cbName = strlen(szNoteName) + 1;
208 size_t cbNameAlign = RT_ALIGN_Z(cbName, s_NoteAlign);
209 uint64_t cbDataAlign = RT_ALIGN_64(cbData, s_NoteAlign);
210
211 static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
212 AssertCompile(sizeof(s_achPad) >= s_NoteAlign - 1);
213
214 Elf64_Nhdr ElfNoteHdr;
215 RT_ZERO(ElfNoteHdr);
216 ElfNoteHdr.n_namesz = (Elf64_Word)cbName; /* @todo fix this later to NOT include NULL terminator */
217 ElfNoteHdr.n_type = Type;
218 ElfNoteHdr.n_descsz = (Elf64_Word)cbDataAlign;
219
220 /*
221 * Write note header.
222 */
223 int rc = RTFileWrite(hFile, &ElfNoteHdr, sizeof(ElfNoteHdr), NULL /* all */);
224 if (RT_SUCCESS(rc))
225 {
226 /*
227 * Write note name.
228 */
229 rc = RTFileWrite(hFile, szNoteName, cbName, NULL /* all */);
230 if (RT_SUCCESS(rc))
231 {
232 /*
233 * Write note name padding if required.
234 */
235 if (cbNameAlign > cbName)
236 rc = RTFileWrite(hFile, s_achPad, cbNameAlign - cbName, NULL);
237
238 if (RT_SUCCESS(rc))
239 {
240 /*
241 * Write note data.
242 */
243 rc = RTFileWrite(hFile, pcvData, cbData, NULL /* all */);
244 if (RT_SUCCESS(rc))
245 {
246 /*
247 * Write note data padding if required.
248 */
249 if (cbDataAlign > cbData)
250 rc = RTFileWrite(hFile, s_achPad, cbDataAlign - cbData, NULL /* all*/);
251 }
252 }
253 }
254 }
255
256 if (RT_FAILURE(rc))
257 LogRel((DBGFLOG_NAME ":RTFileWrite failed. rc=%Rrc pszName=%s cbData=%u cbDataAlign=%u\n", rc, pszName, cbData, cbDataAlign));
258
259 return rc;
260}
261
262
263/**
264 * Count the number of memory ranges that go into the core file.
265 *
266 * We cannot do a page-by-page dump of the entire guest memory as there will be
267 * way too many program header entries. Also we don't want to dump MMIO regions
268 * which means we cannot have a 1:1 mapping between core file offset and memory
269 * offset. Instead we dump the memory in ranges. A memory range is a contiguous
270 * memory area suitable for dumping to a core file.
271 *
272 * @param pVM The VM handle.
273 *
274 * @return Number of memory ranges
275 */
276static uint32_t dbgfR3GetRamRangeCount(PVM pVM)
277{
278 return PGMR3PhysGetRamRangeCount(pVM);
279}
280
281
282/**
283 * EMT Rendezvous worker function for DBGFR3CoreWrite.
284 *
285 * @param pVM The VM handle.
286 * @param pVCpu The handle of the calling VCPU.
287 * @param pvData Opaque data.
288 *
289 * @return VBox status code.
290 */
291static DECLCALLBACK(VBOXSTRICTRC) dbgfR3CoreWrite(PVM pVM, PVMCPU pVCpu, void *pvData)
292{
293 /*
294 * Validate input.
295 */
296 AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
297 AssertReturn(pVCpu, VERR_INVALID_VMCPU_HANDLE);
298 AssertReturn(pvData, VERR_INVALID_POINTER);
299
300 PDBGFCOREDATA pDbgfData = (PDBGFCOREDATA)pvData;
301
302 /*
303 * Collect core information.
304 */
305 uint32_t u32MemRanges = dbgfR3GetRamRangeCount(pVM);
306 uint16_t cMemRanges = u32MemRanges < UINT16_MAX - 1 ? u32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
307 uint16_t cProgHdrs = cMemRanges + 1;
308
309 /*
310 * Compute size of the note section.
311 */
312 uint64_t cbNoteSection = pVM->cCpus * Elf64NoteSectionSize(s_pcszCoreVBoxCpu, sizeof(CPUMCTX));
313 uint64_t off = 0;
314
315 /*
316 * Create the core file.
317 */
318 RTFILE hFile = NIL_RTFILE;
319 int rc = RTFileOpen(&hFile, pDbgfData->pszDumpPath, RTFILE_O_CREATE_REPLACE | RTFILE_O_READWRITE);
320 if (RT_SUCCESS(rc))
321 {
322 /*
323 * Write ELF header.
324 */
325 uint64_t cbElfHdr = 0;
326 rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */, &cbElfHdr);
327 off += cbElfHdr;
328 if (RT_SUCCESS(rc))
329 {
330 /*
331 * Write PT_NOTE program header.
332 */
333 uint64_t cbProgHdr = 0;
334 rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
335 cbElfHdr + cProgHdrs * sizeof(Elf64_Phdr), /* file offset to contents */
336 cbNoteSection, /* size in core file */
337 cbNoteSection, /* size in memory */
338 0, /* physical address */
339 &cbProgHdr);
340 Assert(cbProgHdr == sizeof(Elf64_Phdr));
341 off += cbProgHdr;
342 if (RT_SUCCESS(rc))
343 {
344 /*
345 * Write PT_LOAD program header for each memory range.
346 */
347 uint64_t offMemRange = off + cbNoteSection;
348 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
349 {
350 RTGCPHYS GCPhysStart;
351 RTGCPHYS GCPhysEnd;
352
353 bool fIsMmio;
354 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
355 if (RT_FAILURE(rc))
356 {
357 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
358 break;
359 }
360
361 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
362 uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
363
364 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
365 iRange, GCPhysStart, GCPhysEnd, cbMemRange));
366
367 rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
368 offMemRange, /* file offset to contents */
369 cbFileRange, /* size in core file */
370 cbMemRange, /* size in memory */
371 GCPhysStart, /* physical address */
372 &cbProgHdr);
373 Assert(cbProgHdr == sizeof(Elf64_Phdr));
374 if (RT_FAILURE(rc))
375 {
376 LogRel((DBGFLOG_NAME ":Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n", iRange,
377 cbFileRange, cbMemRange, rc));
378 break;
379 }
380
381 offMemRange += cbFileRange;
382 }
383
384 /*
385 * Write the CPU context note headers and data.
386 */
387 if (RT_SUCCESS(rc))
388 {
389 for (uint32_t iCpu = 0; iCpu < pVM->cCpus; iCpu++)
390 {
391 PCPUMCTX pCpuCtx = &pVM->aCpus[iCpu].cpum.s.Guest;
392 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, s_pcszCoreVBoxCpu, pCpuCtx, sizeof(CPUMCTX), NULL /* pcbNoteHdr */);
393 if (RT_FAILURE(rc))
394 {
395 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", iCpu, rc));
396 break;
397 }
398 }
399 }
400
401 /*
402 * Write memory ranges.
403 */
404 if (RT_SUCCESS(rc))
405 {
406 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
407 {
408 RTGCPHYS GCPhysStart;
409 RTGCPHYS GCPhysEnd;
410 bool fIsMmio;
411 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
412 if (RT_FAILURE(rc))
413 {
414 LogRel((DBGFLOG_NAME ":PGMR3PhysGetRange(2) failed for iRange(%u) rc=%Rrc\n", iRange, rc));
415 break;
416 }
417
418 if (fIsMmio)
419 continue;
420
421 /*
422 * Write page-by-page of this memory range.
423 */
424 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
425 uint64_t cPages = cbMemRange >> PAGE_SHIFT;
426 for (uint64_t iPage = 0; iPage < cPages; iPage++)
427 {
428 const int cbBuf = PAGE_SIZE;
429 void *pvBuf = MMR3HeapAlloc(pVM, MM_TAG_DBGF_CORE_WRITE, cbBuf);
430 if (RT_UNLIKELY(!pvBuf))
431 {
432 LogRel((DBGFLOG_NAME ":MMR3HeapAlloc failed. iRange=%u iPage=%u\n", iRange, iPage));
433 break;
434 }
435
436 rc = PGMPhysRead(pVM, GCPhysStart, pvBuf, cbBuf);
437 if (RT_FAILURE(rc))
438 {
439 /*
440 * For some reason this failed, write out a zero page instead.
441 */
442 LogRel((DBGFLOG_NAME ":PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange,
443 iPage, rc));
444 memset(pvBuf, 0, cbBuf);
445 }
446
447 rc = RTFileWrite(hFile, pvBuf, cbBuf, NULL /* all */);
448 if (RT_FAILURE(rc))
449 {
450 LogRel((DBGFLOG_NAME ":RTFileWrite failed. iRange=%u iPage=%u rc=%Rrc\n", iRange, iPage, rc));
451 MMR3HeapFree(pvBuf);
452 break;
453 }
454
455 MMR3HeapFree(pvBuf);
456 }
457
458 if (RT_FAILURE(rc))
459 break;
460 }
461 }
462 }
463
464 }
465
466 RTFileClose(hFile);
467 }
468
469 return rc;
470}
471
472
473/**
474 * Write core dump of the guest.
475 *
476 * @return VBox status code.
477 * @param pVM The VM handle.
478 * @param idCpu The target CPU ID.
479 * @param pszDumpPath The path of the file to dump into, cannot be
480 * NULL.
481 *
482 * @remarks The VM must be suspended before calling this function.
483 */
484VMMR3DECL(int) DBGFR3CoreWrite(PVM pVM, VMCPUID idCpu, const char *pszDumpPath)
485{
486 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
487 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
488 AssertReturn(pszDumpPath, VERR_INVALID_HANDLE);
489
490 /*
491 * Pass the core write request down to EMT rendezvous which makes sure
492 * other EMTs, if any, are not running.
493 */
494 DBGFCOREDATA CoreData;
495 RT_ZERO(CoreData);
496 CoreData.pszDumpPath = pszDumpPath;
497
498 return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWrite, &CoreData);
499}
500
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette