VirtualBox

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

Last change on this file since 32351 was 32341, checked in by vboxsync, 15 years ago

dbgfcore.h -> dbgfcorefmt.h; only the format, no prototypes or stuff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/* $Id: DBGFCoreWrite.cpp 32341 2010-09-09 11:56:06Z 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/*
78 * For now use Solaris-specific padding and namesz length (i.e. includes NULL terminator)
79 */
80static const int s_NoteAlign = 4; /* @todo see #5211 comment 3 */
81static const int s_cbNoteName = 16;
82static const char *s_pcszCoreVBoxCore = "VBOXCORE";
83static const char *s_pcszCoreVBoxCpu = "VBOXCPU";
84
85/**
86 * DBGFCOREDATA: Core data.
87 */
88typedef 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 */
104static 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 */
151static 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 */
178static inline uint64_t Elf64NoteSectionSize(const char *pszName, uint64_t cbData)
179{
180 uint64_t cbNote = sizeof(Elf64_Nhdr);
181
182 size_t cbName = strlen(pszName) + 1;
183 size_t cbNameAlign = RT_ALIGN_Z(cbName, s_NoteAlign);
184
185 cbNote += cbNameAlign;
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 */
204static 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 cbName = strlen(szNoteName) + 1;
214 size_t cbNameAlign = RT_ALIGN_Z(cbName, s_NoteAlign);
215 uint64_t cbDataAlign = RT_ALIGN_64(cbData, s_NoteAlign);
216
217 static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
218 AssertCompile(sizeof(s_achPad) >= s_NoteAlign - 1);
219
220 Elf64_Nhdr ElfNoteHdr;
221 RT_ZERO(ElfNoteHdr);
222 ElfNoteHdr.n_namesz = (Elf64_Word)cbName; /* @todo fix this later to NOT include NULL terminator */
223 ElfNoteHdr.n_type = Type;
224 ElfNoteHdr.n_descsz = (Elf64_Word)cbDataAlign;
225
226 /*
227 * Write note header.
228 */
229 int rc = RTFileWrite(hFile, &ElfNoteHdr, sizeof(ElfNoteHdr), NULL /* all */);
230 if (RT_SUCCESS(rc))
231 {
232 /*
233 * Write note name.
234 */
235 rc = RTFileWrite(hFile, szNoteName, cbName, NULL /* all */);
236 if (RT_SUCCESS(rc))
237 {
238 /*
239 * Write note name padding if required.
240 */
241 if (cbNameAlign > cbName)
242 rc = RTFileWrite(hFile, s_achPad, cbNameAlign - cbName, NULL);
243
244 if (RT_SUCCESS(rc))
245 {
246 /*
247 * Write note data.
248 */
249 rc = RTFileWrite(hFile, pcvData, cbData, NULL /* all */);
250 if (RT_SUCCESS(rc))
251 {
252 /*
253 * Write note data padding if required.
254 */
255 if (cbDataAlign > cbData)
256 rc = RTFileWrite(hFile, s_achPad, cbDataAlign - cbData, NULL /* all*/);
257 }
258 }
259 }
260 }
261
262 if (RT_FAILURE(rc))
263 LogRel((DBGFLOG_NAME ":RTFileWrite failed. rc=%Rrc pszName=%s cbData=%u cbDataAlign=%u\n", rc, pszName, cbData, cbDataAlign));
264
265 return rc;
266}
267
268
269/**
270 * Count the number of memory ranges that go into the core file.
271 *
272 * We cannot do a page-by-page dump of the entire guest memory as there will be
273 * way too many program header entries. Also we don't want to dump MMIO regions
274 * which means we cannot have a 1:1 mapping between core file offset and memory
275 * offset. Instead we dump the memory in ranges. A memory range is a contiguous
276 * memory area suitable for dumping to a core file.
277 *
278 * @param pVM The VM handle.
279 *
280 * @return Number of memory ranges
281 */
282static uint32_t dbgfR3GetRamRangeCount(PVM pVM)
283{
284 return PGMR3PhysGetRamRangeCount(pVM);
285}
286
287
288/**
289 * EMT Rendezvous worker function for DBGFR3CoreWrite.
290 *
291 * @param pVM The VM handle.
292 * @param pVCpu The handle of the calling VCPU.
293 * @param pvData Opaque data.
294 *
295 * @return VBox status code.
296 */
297static DECLCALLBACK(VBOXSTRICTRC) dbgfR3CoreWrite(PVM pVM, PVMCPU pVCpu, void *pvData)
298{
299 /*
300 * Validate input.
301 */
302 AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
303 AssertReturn(pVCpu, VERR_INVALID_VMCPU_HANDLE);
304 AssertReturn(pvData, VERR_INVALID_POINTER);
305
306 PDBGFCOREDATA pDbgfData = (PDBGFCOREDATA)pvData;
307
308 /*
309 * Collect core information.
310 */
311 uint32_t u32MemRanges = dbgfR3GetRamRangeCount(pVM);
312 uint16_t cMemRanges = u32MemRanges < UINT16_MAX - 1 ? u32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
313 uint16_t cProgHdrs = cMemRanges + 1;
314
315 DBGFCOREDESCRIPTOR CoreDescriptor;
316 RT_ZERO(CoreDescriptor);
317 CoreDescriptor.u32Magic = DBGFCORE_MAGIC;
318 CoreDescriptor.u32FmtVersion = DBGFCORE_FMT_VERSION;
319 CoreDescriptor.cbSelf = sizeof(CoreDescriptor);
320 CoreDescriptor.u32VBoxVersion = VBOX_FULL_VERSION;
321 CoreDescriptor.u32VBoxRevision = VMMGetSvnRev();
322 CoreDescriptor.cCpus = pVM->cCpus;
323
324 LogRel((DBGFLOG_NAME ":CoreDescriptor Version=%u Revision=%u\n", CoreDescriptor.u32VBoxVersion, CoreDescriptor.u32VBoxRevision));
325
326 /*
327 * Compute total size of the note section.
328 */
329 uint64_t cbNoteSection = Elf64NoteSectionSize(s_pcszCoreVBoxCore, sizeof(CoreDescriptor))
330 + pVM->cCpus * Elf64NoteSectionSize(s_pcszCoreVBoxCpu, sizeof(CPUMCTX));
331 uint64_t off = 0;
332
333 /*
334 * Create the core file.
335 */
336 RTFILE hFile = NIL_RTFILE;
337 int rc = RTFileOpen(&hFile, pDbgfData->pszDumpPath, RTFILE_O_CREATE_REPLACE | RTFILE_O_READWRITE);
338 if (RT_FAILURE(rc))
339 {
340 LogRel((DBGFLOG_NAME ":RTFileOpen failed for '%s' rc=%Rrc\n", pDbgfData->pszDumpPath, rc));
341 return rc;
342 }
343
344 /*
345 * Write ELF header.
346 */
347 uint64_t cbElfHdr = 0;
348 uint64_t cbProgHdr = 0;
349 uint64_t offMemRange = 0;
350 rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */, &cbElfHdr);
351 off += cbElfHdr;
352 if (RT_FAILURE(rc))
353 {
354 LogRel((DBGFLOG_NAME ":Elf64WriteElfHdr failed. rc=%Rrc\n", rc));
355 goto CoreWriteDone;
356 }
357
358 /*
359 * Write PT_NOTE program header.
360 */
361 rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
362 cbElfHdr + cProgHdrs * sizeof(Elf64_Phdr), /* file offset to contents */
363 cbNoteSection, /* size in core file */
364 cbNoteSection, /* size in memory */
365 0, /* physical address */
366 &cbProgHdr);
367 Assert(cbProgHdr == sizeof(Elf64_Phdr));
368 off += cbProgHdr;
369
370 if (RT_FAILURE(rc))
371 {
372 LogRel((DBGFLOG_NAME ":Elf64WritreProgHdr failed for PT_NOTE. rc=%Rrc\n", rc));
373 goto CoreWriteDone;
374 }
375
376 /*
377 * Write PT_LOAD program header for each memory range.
378 */
379 offMemRange = off + cbNoteSection;
380 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
381 {
382 RTGCPHYS GCPhysStart;
383 RTGCPHYS GCPhysEnd;
384
385 bool fIsMmio;
386 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
387 if (RT_FAILURE(rc))
388 {
389 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
390 goto CoreWriteDone;
391 }
392
393 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
394 uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
395
396 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
397 iRange, GCPhysStart, GCPhysEnd, cbMemRange));
398
399 rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
400 offMemRange, /* file offset to contents */
401 cbFileRange, /* size in core file */
402 cbMemRange, /* size in memory */
403 GCPhysStart, /* physical address */
404 &cbProgHdr);
405 Assert(cbProgHdr == sizeof(Elf64_Phdr));
406 if (RT_FAILURE(rc))
407 {
408 LogRel((DBGFLOG_NAME ":Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n", iRange,
409 cbFileRange, cbMemRange, rc));
410 goto CoreWriteDone;
411 }
412
413 offMemRange += cbFileRange;
414 }
415
416 /*
417 * Write the Core descriptor note header and data.
418 */
419 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, s_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor),
420 NULL /* pcbNoteHdr */);
421 if (RT_FAILURE(rc))
422 {
423 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr failed for Note '%s' rc=%Rrc\n", s_pcszCoreVBoxCore, rc));
424 goto CoreWriteDone;
425 }
426
427 /*
428 * Write the CPU context note headers and data.
429 */
430 for (uint32_t iCpu = 0; iCpu < pVM->cCpus; iCpu++)
431 {
432 PCPUMCTX pCpuCtx = &pVM->aCpus[iCpu].cpum.s.Guest;
433 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, s_pcszCoreVBoxCpu, pCpuCtx, sizeof(CPUMCTX), NULL /* pcbNoteHdr */);
434 if (RT_FAILURE(rc))
435 {
436 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", iCpu, rc));
437 goto CoreWriteDone;
438 }
439 }
440
441 /*
442 * Write memory ranges.
443 */
444 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
445 {
446 RTGCPHYS GCPhysStart;
447 RTGCPHYS GCPhysEnd;
448 bool fIsMmio;
449 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
450 if (RT_FAILURE(rc))
451 {
452 LogRel((DBGFLOG_NAME ":PGMR3PhysGetRange(2) failed for iRange(%u) rc=%Rrc\n", iRange, rc));
453 goto CoreWriteDone;
454 }
455
456 if (fIsMmio)
457 continue;
458
459 /*
460 * Write page-by-page of this memory range.
461 */
462 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
463 uint64_t cPages = cbMemRange >> PAGE_SHIFT;
464 for (uint64_t iPage = 0; iPage < cPages; iPage++)
465 {
466 const int cbBuf = PAGE_SIZE;
467 void *pvBuf = MMR3HeapAlloc(pVM, MM_TAG_DBGF_CORE_WRITE, cbBuf);
468 if (RT_UNLIKELY(!pvBuf))
469 {
470 LogRel((DBGFLOG_NAME ":MMR3HeapAlloc failed. iRange=%u iPage=%u\n", iRange, iPage));
471 goto CoreWriteDone;
472 }
473
474 rc = PGMPhysRead(pVM, GCPhysStart, pvBuf, cbBuf);
475 if (RT_FAILURE(rc))
476 {
477 /*
478 * For some reason this failed, write out a zero page instead.
479 */
480 LogRel((DBGFLOG_NAME ":PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange,
481 iPage, rc));
482 memset(pvBuf, 0, cbBuf);
483 }
484
485 rc = RTFileWrite(hFile, pvBuf, cbBuf, NULL /* all */);
486 if (RT_FAILURE(rc))
487 {
488 LogRel((DBGFLOG_NAME ":RTFileWrite failed. iRange=%u iPage=%u rc=%Rrc\n", iRange, iPage, rc));
489 MMR3HeapFree(pvBuf);
490 goto CoreWriteDone;
491 }
492
493 MMR3HeapFree(pvBuf);
494 }
495 }
496
497CoreWriteDone:
498 RTFileClose(hFile);
499
500 return rc;
501}
502
503
504/**
505 * Write core dump of the guest.
506 *
507 * @return VBox status code.
508 * @param pVM The VM handle.
509 * @param pszDumpPath The path of the file to dump into, cannot be
510 * NULL.
511 *
512 * @remarks The VM must be suspended before calling this function.
513 */
514VMMR3DECL(int) DBGFR3CoreWrite(PVM pVM, const char *pszDumpPath)
515{
516 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
517 AssertReturn(pszDumpPath, VERR_INVALID_HANDLE);
518
519 /*
520 * Pass the core write request down to EMT rendezvous which makes sure
521 * other EMTs, if any, are not running.
522 */
523 DBGFCOREDATA CoreData;
524 RT_ZERO(CoreData);
525 CoreData.pszDumpPath = pszDumpPath;
526
527 return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWrite, &CoreData);
528}
529
Note: See TracBrowser for help on using the repository browser.

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