VirtualBox

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

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

VMM/DBGF: Added CoreDescriptor.

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