VirtualBox

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

最後變更 在這個檔案從34134是 34134,由 vboxsync 提交於 14 年 前

DBGFCoreWrite.cpp: Eliminated unnecessary gotos.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 18.9 KB
 
1/* $Id: DBGFCoreWrite.cpp 34134 2010-11-17 09:39:35Z 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.alldomusa.eu.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/** @page pg_dbgf_vmcore VMCore Format
19 *
20 * The VirtualBox VMCore Format:
21 * [ ELF 64 Header] -- Only 1
22 *
23 * [ PT_NOTE ] -- Only 1
24 * - Offset into CoreDescriptor followed by list of Notes (Note Hdr + data) of VBox CPUs.
25 * - (Any Additional custom Note sections).
26 *
27 * [ PT_LOAD ] -- One for each contiguous memory chunk
28 * - Memory offset (physical).
29 * - File offset.
30 *
31 * CoreDescriptor
32 * - Magic, VBox version.
33 * - Number of CPus.
34 *
35 * Per-CPU register dump
36 * - CPU 1 Note Hdr + Data.
37 * - CPU 2 Note Hdr + Data.
38 * ...
39 * (Additional custom notes Hdr+data)
40 * - VBox 1 Note Hdr + Data.
41 * - VBox 2 Note Hdr + Data.
42 * ...
43 * Memory dump
44 *
45 */
46
47/*******************************************************************************
48* Header Files *
49*******************************************************************************/
50#define LOG_GROUP LOG_GROUP_DBGF
51#include <iprt/param.h>
52#include <iprt/file.h>
53
54#include "DBGFInternal.h"
55
56#include <VBox/cpum.h>
57#include "CPUMInternal.h"
58#include <VBox/dbgf.h>
59#include <VBox/dbgfcorefmt.h>
60#include <VBox/vm.h>
61#include <VBox/pgm.h>
62#include <VBox/err.h>
63#include <VBox/log.h>
64#include <VBox/mm.h>
65#include <VBox/version.h>
66
67#include "../Runtime/include/internal/ldrELF64.h"
68
69/*******************************************************************************
70* Defined Constants And Macros *
71*******************************************************************************/
72#ifdef DEBUG_ramshankar
73# undef Log
74# define Log LogRel
75#endif
76#define DBGFLOG_NAME "DBGFCoreWrite"
77
78static const int s_NoteAlign = 8;
79static const int s_cbNoteName = 16;
80
81/* These strings *HAVE* to be 8-byte aligned */
82static const char *s_pcszCoreVBoxCore = "VBCORE";
83static const char *s_pcszCoreVBoxCpu = "VBCPU";
84
85
86/**
87 * DBGFCOREDATA: Core data.
88 */
89typedef struct
90{
91 const char *pszDumpPath; /* File path to dump the core into. */
92} DBGFCOREDATA, *PDBGFCOREDATA;
93
94/**
95 * ELF function to write 64-bit ELF header.
96 *
97 * @param hFile The file to write to.
98 * @param cProgHdrs Number of program headers.
99 * @param cSecHdrs Number of section headers.
100 * @param pcbElfHdr Where to store the size of written header to file,
101 * can be NULL.
102 *
103 * @return IPRT status code.
104 */
105static int Elf64WriteElfHdr(RTFILE hFile, uint16_t cProgHdrs, uint16_t cSecHdrs, uint64_t *pcbElfHdr)
106{
107 Elf64_Ehdr ElfHdr;
108 RT_ZERO(ElfHdr);
109 ElfHdr.e_ident[EI_MAG0] = ELFMAG0;
110 ElfHdr.e_ident[EI_MAG1] = ELFMAG1;
111 ElfHdr.e_ident[EI_MAG2] = ELFMAG2;
112 ElfHdr.e_ident[EI_MAG3] = ELFMAG3;
113 ElfHdr.e_ident[EI_DATA] = ELFDATA2LSB;
114 ElfHdr.e_type = ET_CORE;
115 ElfHdr.e_version = EV_CURRENT;
116 ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
117 /* 32-bit builds will produce cores with e_machine EM_386. */
118#ifdef RT_ARCH_AMD64
119 ElfHdr.e_machine = EM_X86_64;
120#else
121 ElfHdr.e_machine = EM_386;
122#endif
123 ElfHdr.e_phnum = cProgHdrs;
124 ElfHdr.e_shnum = cSecHdrs;
125 ElfHdr.e_ehsize = sizeof(ElfHdr);
126 ElfHdr.e_phoff = sizeof(ElfHdr);
127 ElfHdr.e_phentsize = sizeof(Elf64_Phdr);
128 ElfHdr.e_shentsize = sizeof(Elf64_Shdr);
129
130 int rc = RTFileWrite(hFile, &ElfHdr, sizeof(ElfHdr), NULL /* all */);
131 if (RT_SUCCESS(rc) && pcbElfHdr)
132 *pcbElfHdr = sizeof(ElfHdr);
133 return rc;
134}
135
136
137/**
138 * ELF function to write 64-bit program header.
139 *
140 * @param hFile The file to write to.
141 * @param Type Type of program header (PT_*).
142 * @param fFlags Flags (access permissions, PF_*).
143 * @param offFileData File offset of contents.
144 * @param cbFileData Size of contents in the file.
145 * @param cbMemData Size of contents in memory.
146 * @param Phys Physical address, pass zero if not applicable.
147 * @param pcbProgHdr Where to store the size of written header to file,
148 * can be NULL.
149 *
150 * @return IPRT status code.
151 */
152static int Elf64WriteProgHdr(RTFILE hFile, uint32_t Type, uint32_t fFlags, uint64_t offFileData, uint64_t cbFileData,
153 uint64_t cbMemData, RTGCPHYS Phys, uint64_t *pcbProgHdr)
154{
155 Elf64_Phdr ProgHdr;
156 RT_ZERO(ProgHdr);
157 ProgHdr.p_type = Type;
158 ProgHdr.p_flags = fFlags;
159 ProgHdr.p_offset = offFileData;
160 ProgHdr.p_filesz = cbFileData;
161 ProgHdr.p_memsz = cbMemData;
162 ProgHdr.p_paddr = Phys;
163
164 int rc = RTFileWrite(hFile, &ProgHdr, sizeof(ProgHdr), NULL /* all */);
165 if (RT_SUCCESS(rc) && pcbProgHdr)
166 *pcbProgHdr = sizeof(ProgHdr);
167 return rc;
168}
169
170
171/**
172 * Returns the size of the NOTE section given the name and size of the data.
173 *
174 * @param pszName Name of the note section.
175 * @param cb Size of the data portion of the note section.
176 *
177 * @return The size of the NOTE section as rounded to the file alignment.
178 */
179static inline uint64_t Elf64NoteSectionSize(const char *pszName, uint64_t cbData)
180{
181 uint64_t cbNote = sizeof(Elf64_Nhdr);
182
183 size_t cchName = strlen(pszName) + 1;
184 size_t cchNameAlign = RT_ALIGN_Z(cchName, s_NoteAlign);
185
186 cbNote += cchNameAlign;
187 cbNote += RT_ALIGN_64(cbData, s_NoteAlign);
188 return cbNote;
189}
190
191
192/**
193 * Elf function to write 64-bit note header.
194 *
195 * @param hFile The file to write to.
196 * @param Type Type of this section.
197 * @param pszName Name of this section.
198 * @param pcv Opaque pointer to the data, if NULL only computes size.
199 * @param cbData Size of the data.
200 * @param pcbNoteHdr Where to store the size of written header to file,
201 * can be NULL.
202 *
203 * @return IPRT status code.
204 */
205static int Elf64WriteNoteHdr(RTFILE hFile, uint16_t Type, const char *pszName, const void *pcvData, uint64_t cbData, uint64_t *pcbNoteHdr)
206{
207 AssertReturn(pcvData, VERR_INVALID_POINTER);
208 AssertReturn(cbData > 0, VERR_NO_DATA);
209
210 char szNoteName[s_cbNoteName];
211 RT_ZERO(szNoteName);
212 RTStrCopy(szNoteName, sizeof(szNoteName), pszName);
213
214 size_t cchName = strlen(szNoteName) + 1;
215 size_t cchNameAlign = RT_ALIGN_Z(cchName, s_NoteAlign);
216 uint64_t cbDataAlign = RT_ALIGN_64(cbData, s_NoteAlign);
217
218 /*
219 * Yell loudly and bail if we are going to be writing a core file that is not compatible with
220 * both Solaris and the 64-bit ELF spec. which dictates 8-byte alignment. See #5211 comment 3.
221 */
222 if (cchNameAlign - cchName > 3)
223 {
224 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr pszName=%s cchName=%u cchNameAlign=%u, cchName aligns to 4 not 8-bytes!\n", pszName, cchName,
225 cchNameAlign));
226 return VERR_INVALID_PARAMETER;
227 }
228
229 if (cbDataAlign - cbData > 3)
230 {
231 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr pszName=%s cbData=%u cbDataAlign=%u, cbData aligns to 4 not 8-bytes!\n", pszName, cbData,
232 cbDataAlign));
233 return VERR_INVALID_PARAMETER;
234 }
235
236 static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
237 AssertCompile(sizeof(s_achPad) >= s_NoteAlign - 1);
238
239 Elf64_Nhdr ElfNoteHdr;
240 RT_ZERO(ElfNoteHdr);
241 ElfNoteHdr.n_namesz = (Elf64_Word)cchName - 1; /* Again a discrepancy between ELF-64 and Solaris (#5211 comment 3), we will follow ELF-64 */
242 ElfNoteHdr.n_type = Type;
243 ElfNoteHdr.n_descsz = (Elf64_Word)cbDataAlign;
244
245 /*
246 * Write note header.
247 */
248 int rc = RTFileWrite(hFile, &ElfNoteHdr, sizeof(ElfNoteHdr), NULL /* all */);
249 if (RT_SUCCESS(rc))
250 {
251 /*
252 * Write note name.
253 */
254 rc = RTFileWrite(hFile, szNoteName, cchName, NULL /* all */);
255 if (RT_SUCCESS(rc))
256 {
257 /*
258 * Write note name padding if required.
259 */
260 if (cchNameAlign > cchName)
261 rc = RTFileWrite(hFile, s_achPad, cchNameAlign - cchName, NULL);
262
263 if (RT_SUCCESS(rc))
264 {
265 /*
266 * Write note data.
267 */
268 rc = RTFileWrite(hFile, pcvData, cbData, NULL /* all */);
269 if (RT_SUCCESS(rc))
270 {
271 /*
272 * Write note data padding if required.
273 */
274 if (cbDataAlign > cbData)
275 rc = RTFileWrite(hFile, s_achPad, cbDataAlign - cbData, NULL /* all*/);
276 }
277 }
278 }
279 }
280
281 if (RT_FAILURE(rc))
282 LogRel((DBGFLOG_NAME ": RTFileWrite failed. rc=%Rrc pszName=%s cchName=%u cchNameAlign=%u cbData=%u cbDataAlign=%u\n",
283 rc, pszName, cchName, cchNameAlign, cbData, cbDataAlign));
284
285 return rc;
286}
287
288
289/**
290 * Count the number of memory ranges that go into the core file.
291 *
292 * We cannot do a page-by-page dump of the entire guest memory as there will be
293 * way too many program header entries. Also we don't want to dump MMIO regions
294 * which means we cannot have a 1:1 mapping between core file offset and memory
295 * offset. Instead we dump the memory in ranges. A memory range is a contiguous
296 * memory area suitable for dumping to a core file.
297 *
298 * @param pVM The VM handle.
299 *
300 * @return Number of memory ranges
301 */
302static uint32_t dbgfR3GetRamRangeCount(PVM pVM)
303{
304 return PGMR3PhysGetRamRangeCount(pVM);
305}
306
307
308/**
309 * Worker function for dbgfR3CoreWrite which does the writing.
310 *
311 * @returns VBox status code
312 * @param pVM The VM handle.
313 * @param pDbgfData The core dump parameters.
314 * @param hFile The file to write to. Caller closes this.
315 */
316static int dbgfR3CoreWriteWorker(PVM pVM, PDBGFCOREDATA pDbgfData, RTFILE hFile)
317{
318 /*
319 * Collect core information.
320 */
321 uint32_t u32MemRanges = dbgfR3GetRamRangeCount(pVM);
322 uint16_t cMemRanges = u32MemRanges < UINT16_MAX - 1 ? u32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
323 uint16_t cProgHdrs = cMemRanges + 1;
324
325 DBGFCOREDESCRIPTOR CoreDescriptor;
326 RT_ZERO(CoreDescriptor);
327 CoreDescriptor.u32Magic = DBGFCORE_MAGIC;
328 CoreDescriptor.u32FmtVersion = DBGFCORE_FMT_VERSION;
329 CoreDescriptor.cbSelf = sizeof(CoreDescriptor);
330 CoreDescriptor.u32VBoxVersion = VBOX_FULL_VERSION;
331 CoreDescriptor.u32VBoxRevision = VMMGetSvnRev();
332 CoreDescriptor.cCpus = pVM->cCpus;
333
334 Log((DBGFLOG_NAME ": CoreDescriptor Version=%u Revision=%u\n", CoreDescriptor.u32VBoxVersion, CoreDescriptor.u32VBoxRevision));
335
336 /*
337 * Compute total size of the note section.
338 */
339 uint64_t cbNoteSection = Elf64NoteSectionSize(s_pcszCoreVBoxCore, sizeof(CoreDescriptor))
340 + pVM->cCpus * Elf64NoteSectionSize(s_pcszCoreVBoxCpu, sizeof(CPUMCTX));
341 uint64_t off = 0;
342
343 /*
344 * Write ELF header.
345 */
346 uint64_t cbElfHdr = 0;
347 uint64_t cbProgHdr = 0;
348 uint64_t offMemRange = 0;
349 int rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */, &cbElfHdr);
350 off += cbElfHdr;
351 if (RT_FAILURE(rc))
352 {
353 LogRel((DBGFLOG_NAME ": Elf64WriteElfHdr failed. rc=%Rrc\n", rc));
354 return rc;
355 }
356
357 /*
358 * Write PT_NOTE program header.
359 */
360 rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
361 cbElfHdr + cProgHdrs * sizeof(Elf64_Phdr), /* file offset to contents */
362 cbNoteSection, /* size in core file */
363 cbNoteSection, /* size in memory */
364 0, /* physical address */
365 &cbProgHdr);
366 Assert(cbProgHdr == sizeof(Elf64_Phdr));
367 off += cbProgHdr;
368
369 if (RT_FAILURE(rc))
370 {
371 LogRel((DBGFLOG_NAME ": Elf64WritreProgHdr failed for PT_NOTE. rc=%Rrc\n", rc));
372 return rc;
373 }
374
375 /*
376 * Write PT_LOAD program header for each memory range.
377 */
378 offMemRange = off + cbNoteSection; /** @todo this isn't taking the cmemRanges of prog hdrs into account. */
379 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
380 {
381 RTGCPHYS GCPhysStart;
382 RTGCPHYS GCPhysEnd;
383
384 bool fIsMmio;
385 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
386 if (RT_FAILURE(rc))
387 {
388 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
389 return rc;
390 }
391
392 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
393 uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
394
395 Log((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
396 iRange, GCPhysStart, GCPhysEnd, cbMemRange));
397
398 rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
399 offMemRange, /* file offset to contents */
400 cbFileRange, /* size in core file */
401 cbMemRange, /* size in memory */
402 GCPhysStart, /* physical address */
403 &cbProgHdr);
404 Assert(cbProgHdr == sizeof(Elf64_Phdr));
405 if (RT_FAILURE(rc))
406 {
407 LogRel((DBGFLOG_NAME ": Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n", iRange,
408 cbFileRange, cbMemRange, rc));
409 return rc;
410 }
411
412 offMemRange += cbFileRange;
413 }
414
415 /*
416 * Write the Core descriptor note header and data.
417 */
418 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, s_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor),
419 NULL /* pcbNoteHdr */);
420 if (RT_FAILURE(rc))
421 {
422 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for Note '%s' rc=%Rrc\n", s_pcszCoreVBoxCore, rc));
423 return rc;
424 }
425
426 /*
427 * Write the CPU context note headers and data.
428 */
429 for (uint32_t iCpu = 0; iCpu < pVM->cCpus; iCpu++)
430 {
431 PCPUMCTX pCpuCtx = &pVM->aCpus[iCpu].cpum.s.Guest;
432 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, s_pcszCoreVBoxCpu, pCpuCtx, sizeof(CPUMCTX), NULL /* pcbNoteHdr */);
433 if (RT_FAILURE(rc))
434 {
435 LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", iCpu, rc));
436 return rc;
437 }
438 }
439
440 /*
441 * Write memory ranges.
442 */
443 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
444 {
445 RTGCPHYS GCPhysStart;
446 RTGCPHYS GCPhysEnd;
447 bool fIsMmio;
448 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
449 if (RT_FAILURE(rc))
450 {
451 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange(2) failed for iRange(%u) rc=%Rrc\n", iRange, rc));
452 return rc;
453 }
454
455 if (fIsMmio)
456 continue;
457
458 /*
459 * Write page-by-page of this memory range.
460 */
461 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
462 uint64_t cPages = cbMemRange >> PAGE_SHIFT;
463 for (uint64_t iPage = 0; iPage < cPages; iPage++)
464 {
465 const int cbBuf = PAGE_SIZE;
466 void *pvBuf = MMR3HeapAlloc(pVM, MM_TAG_DBGF_CORE_WRITE, cbBuf);
467 if (RT_UNLIKELY(!pvBuf))
468 {
469 LogRel((DBGFLOG_NAME ": MMR3HeapAlloc failed. iRange=%u iPage=%u\n", iRange, iPage));
470 return rc;
471 }
472
473 rc = PGMPhysRead(pVM, GCPhysStart, pvBuf, cbBuf);
474 if (RT_FAILURE(rc))
475 {
476 /*
477 * For some reason this failed, write out a zero page instead.
478 */
479 LogRel((DBGFLOG_NAME ": PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange,
480 iPage, rc));
481 memset(pvBuf, 0, cbBuf);
482 }
483
484 rc = RTFileWrite(hFile, pvBuf, cbBuf, NULL /* all */);
485 if (RT_FAILURE(rc))
486 {
487 LogRel((DBGFLOG_NAME ": RTFileWrite failed. iRange=%u iPage=%u rc=%Rrc\n", iRange, iPage, rc));
488 MMR3HeapFree(pvBuf);
489 return rc;
490 }
491
492 MMR3HeapFree(pvBuf);
493 }
494 }
495
496 return rc;
497}
498
499
500/**
501 * EMT Rendezvous worker function for DBGFR3CoreWrite.
502 *
503 * @param pVM The VM handle.
504 * @param pVCpu The handle of the calling VCPU.
505 * @param pvData Opaque data.
506 *
507 * @return VBox status code.
508 */
509static DECLCALLBACK(VBOXSTRICTRC) dbgfR3CoreWrite(PVM pVM, PVMCPU pVCpu, void *pvData)
510{
511 /*
512 * Validate input.
513 */
514 AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
515 AssertReturn(pVCpu, VERR_INVALID_VMCPU_HANDLE);
516 AssertReturn(pvData, VERR_INVALID_POINTER);
517
518 PDBGFCOREDATA pDbgfData = (PDBGFCOREDATA)pvData;
519
520 /*
521 * Create the core file.
522 */
523 RTFILE hFile;
524 int rc = RTFileOpen(&hFile, pDbgfData->pszDumpPath, RTFILE_O_CREATE_REPLACE | RTFILE_O_READWRITE);
525 if (RT_SUCCESS(rc))
526 {
527 rc = dbgfR3CoreWriteWorker(pVM, pDbgfData, hFile);
528 RTFileClose(hFile);
529 }
530 else
531 LogRel((DBGFLOG_NAME ": RTFileOpen failed for '%s' rc=%Rrc\n", pDbgfData->pszDumpPath, rc));
532 return rc;
533}
534
535
536/**
537 * Write core dump of the guest.
538 *
539 * @return VBox status code.
540 * @param pVM The VM handle.
541 * @param pszDumpPath The path of the file to dump into, cannot be
542 * NULL.
543 *
544 * @remarks The VM must be suspended before calling this function.
545 */
546VMMR3DECL(int) DBGFR3CoreWrite(PVM pVM, const char *pszDumpPath)
547{
548 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
549 AssertReturn(pszDumpPath, VERR_INVALID_HANDLE);
550
551 /*
552 * Pass the core write request down to EMT rendezvous which makes sure
553 * other EMTs, if any, are not running. IO threads could still be running
554 * but we don't care about them.
555 */
556 DBGFCOREDATA CoreData;
557 RT_ZERO(CoreData);
558 CoreData.pszDumpPath = pszDumpPath;
559
560 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWrite, &CoreData);
561 if (RT_SUCCESS(rc))
562 LogRel((DBGFLOG_NAME ": Successfully wrote guest core dump %s\n", pszDumpPath));
563 else
564 LogRel((DBGFLOG_NAME ": Failed to write guest core dump %s. rc=%Rrc\n", pszDumpPath, rc));
565 return rc;
566}
567
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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