VirtualBox

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

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

VMM/DBGFCoreWrite: build fix.

  • 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 32254 2010-09-06 15:19:50Z 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 uint64_t offMemRange = 0;
343 rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */, &cbElfHdr);
344 off += cbElfHdr;
345 if (RT_FAILURE(rc))
346 {
347 LogRel((DBGFLOG_NAME ":Elf64WriteElfHdr failed. rc=%Rrc\n", rc));
348 goto CoreWriteDone;
349 }
350
351 /*
352 * Write PT_NOTE program header.
353 */
354 rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
355 cbElfHdr + cProgHdrs * sizeof(Elf64_Phdr), /* file offset to contents */
356 cbNoteSection, /* size in core file */
357 cbNoteSection, /* size in memory */
358 0, /* physical address */
359 &cbProgHdr);
360 Assert(cbProgHdr == sizeof(Elf64_Phdr));
361 off += cbProgHdr;
362
363 if (RT_FAILURE(rc))
364 {
365 LogRel((DBGFLOG_NAME ":Elf64WritreProgHdr failed for PT_NOTE. rc=%Rrc\n", rc));
366 goto CoreWriteDone;
367 }
368
369 /*
370 * Write PT_LOAD program header for each memory range.
371 */
372 offMemRange = off + cbNoteSection;
373 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
374 {
375 RTGCPHYS GCPhysStart;
376 RTGCPHYS GCPhysEnd;
377
378 bool fIsMmio;
379 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
380 if (RT_FAILURE(rc))
381 {
382 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
383 goto CoreWriteDone;
384 }
385
386 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
387 uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
388
389 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
390 iRange, GCPhysStart, GCPhysEnd, cbMemRange));
391
392 rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
393 offMemRange, /* file offset to contents */
394 cbFileRange, /* size in core file */
395 cbMemRange, /* size in memory */
396 GCPhysStart, /* physical address */
397 &cbProgHdr);
398 Assert(cbProgHdr == sizeof(Elf64_Phdr));
399 if (RT_FAILURE(rc))
400 {
401 LogRel((DBGFLOG_NAME ":Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n", iRange,
402 cbFileRange, cbMemRange, rc));
403 goto CoreWriteDone;
404 }
405
406 offMemRange += cbFileRange;
407 }
408
409 /*
410 * Write the Core descriptor note header and data.
411 */
412 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, s_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor),
413 NULL /* pcbNoteHdr */);
414 if (RT_FAILURE(rc))
415 {
416 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr failed for Note '%s' rc=%Rrc\n", s_pcszCoreVBoxCore, rc));
417 goto CoreWriteDone;
418 }
419
420 /*
421 * Write the CPU context note headers and data.
422 */
423 for (uint32_t iCpu = 0; iCpu < pVM->cCpus; iCpu++)
424 {
425 PCPUMCTX pCpuCtx = &pVM->aCpus[iCpu].cpum.s.Guest;
426 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, s_pcszCoreVBoxCpu, pCpuCtx, sizeof(CPUMCTX), NULL /* pcbNoteHdr */);
427 if (RT_FAILURE(rc))
428 {
429 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", iCpu, rc));
430 goto CoreWriteDone;
431 }
432 }
433
434 /*
435 * Write memory ranges.
436 */
437 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
438 {
439 RTGCPHYS GCPhysStart;
440 RTGCPHYS GCPhysEnd;
441 bool fIsMmio;
442 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
443 if (RT_FAILURE(rc))
444 {
445 LogRel((DBGFLOG_NAME ":PGMR3PhysGetRange(2) failed for iRange(%u) rc=%Rrc\n", iRange, rc));
446 goto CoreWriteDone;
447 }
448
449 if (fIsMmio)
450 continue;
451
452 /*
453 * Write page-by-page of this memory range.
454 */
455 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
456 uint64_t cPages = cbMemRange >> PAGE_SHIFT;
457 for (uint64_t iPage = 0; iPage < cPages; iPage++)
458 {
459 const int cbBuf = PAGE_SIZE;
460 void *pvBuf = MMR3HeapAlloc(pVM, MM_TAG_DBGF_CORE_WRITE, cbBuf);
461 if (RT_UNLIKELY(!pvBuf))
462 {
463 LogRel((DBGFLOG_NAME ":MMR3HeapAlloc failed. iRange=%u iPage=%u\n", iRange, iPage));
464 goto CoreWriteDone;
465 }
466
467 rc = PGMPhysRead(pVM, GCPhysStart, pvBuf, cbBuf);
468 if (RT_FAILURE(rc))
469 {
470 /*
471 * For some reason this failed, write out a zero page instead.
472 */
473 LogRel((DBGFLOG_NAME ":PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange,
474 iPage, rc));
475 memset(pvBuf, 0, cbBuf);
476 }
477
478 rc = RTFileWrite(hFile, pvBuf, cbBuf, NULL /* all */);
479 if (RT_FAILURE(rc))
480 {
481 LogRel((DBGFLOG_NAME ":RTFileWrite failed. iRange=%u iPage=%u rc=%Rrc\n", iRange, iPage, rc));
482 MMR3HeapFree(pvBuf);
483 goto CoreWriteDone;
484 }
485
486 MMR3HeapFree(pvBuf);
487 }
488 }
489
490CoreWriteDone:
491 RTFileClose(hFile);
492
493 return rc;
494}
495
496
497/**
498 * Write core dump of the guest.
499 *
500 * @return VBox status code.
501 * @param pVM The VM handle.
502 * @param idCpu The target CPU ID.
503 * @param pszDumpPath The path of the file to dump into, cannot be
504 * NULL.
505 *
506 * @remarks The VM must be suspended before calling this function.
507 */
508VMMR3DECL(int) DBGFR3CoreWrite(PVM pVM, VMCPUID idCpu, const char *pszDumpPath)
509{
510 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
511 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
512 AssertReturn(pszDumpPath, VERR_INVALID_HANDLE);
513
514 /*
515 * Pass the core write request down to EMT rendezvous which makes sure
516 * other EMTs, if any, are not running.
517 */
518 DBGFCOREDATA CoreData;
519 RT_ZERO(CoreData);
520 CoreData.pszDumpPath = pszDumpPath;
521
522 return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWrite, &CoreData);
523}
524
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