DBGFCoreWrite.cpp revision 0cbe0837a380478633429793c8ef21328d957d16
/* $Id$ */
/** @file
* DBGF - Debugger Facility, Guest Core Dump.
*/
/*
* Copyright (C) 2010-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/** @page pg_dbgf_vmcore VMCore Format
*
* The VirtualBox VMCore Format:
* [ ELF 64 Header] -- Only 1
*
* [ PT_NOTE ] -- Only 1
* - Offset into CoreDescriptor followed by list of Notes (Note Hdr + data) of VBox CPUs.
* - (Any Additional custom Note sections).
*
* [ PT_LOAD ] -- One for each contiguous memory chunk
* - Memory offset (physical).
* - File offset.
*
* CoreDescriptor
* - Magic, VBox version.
* - Number of CPus.
*
* Per-CPU register dump
* - CPU 1 Note Hdr + Data.
* - CPU 2 Note Hdr + Data.
* ...
* (Additional custom notes Hdr+data)
* - VBox 1 Note Hdr + Data.
* - VBox 2 Note Hdr + Data.
* ...
* Memory dump
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DBGF
#include "DBGFInternal.h"
#include "../../Runtime/include/internal/ldrELF64.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define DBGFLOG_NAME "DBGFCoreWrite"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static const int s_NoteAlign = 8;
static const int s_cbNoteName = 16;
/* These strings *HAVE* to be 8-byte aligned */
static const char *s_pcszCoreVBoxCore = "VBCORE";
static const char *s_pcszCoreVBoxCpu = "VBCPU";
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Guest core writer data.
*
* Used to pass parameters from DBGFR3CoreWrite to dbgfR3CoreWriteRendezvous.
*/
typedef struct DBGFCOREDATA
{
/** The name of the file to write the file to. */
const char *pszFilename;
/** Whether to replace (/overwrite) any existing file. */
bool fReplaceFile;
} DBGFCOREDATA;
/** Pointer to the guest core writer data. */
typedef DBGFCOREDATA *PDBGFCOREDATA;
/**
* ELF function to write 64-bit ELF header.
*
* @param hFile The file to write to.
* @param cProgHdrs Number of program headers.
* @param cSecHdrs Number of section headers.
*
* @return IPRT status code.
*/
{
/* 32-bit builds will produce cores with e_machine EM_386. */
#ifdef RT_ARCH_AMD64
#else
#endif
}
/**
* ELF function to write 64-bit program header.
*
* @param hFile The file to write to.
* @param Type Type of program header (PT_*).
* @param fFlags Flags (access permissions, PF_*).
* @param offFileData File offset of contents.
* @param cbFileData Size of contents in the file.
* @param cbMemData Size of contents in memory.
* @param Phys Physical address, pass zero if not applicable.
*
* @return IPRT status code.
*/
static int Elf64WriteProgHdr(RTFILE hFile, uint32_t Type, uint32_t fFlags, uint64_t offFileData, uint64_t cbFileData,
{
}
/**
* Returns the size of the NOTE section given the name and size of the data.
*
* @param pszName Name of the note section.
* @param cb Size of the data portion of the note section.
*
* @return The size of the NOTE section as rounded to the file alignment.
*/
{
cbNote += cchNameAlign;
return cbNote;
}
/**
* Elf function to write 64-bit note header.
*
* @param hFile The file to write to.
* @param Type Type of this section.
* @param pszName Name of this section.
* @param pcv Opaque pointer to the data, if NULL only computes size.
* @param cbData Size of the data.
*
* @return IPRT status code.
*/
static int Elf64WriteNoteHdr(RTFILE hFile, uint16_t Type, const char *pszName, const void *pcvData, uint64_t cbData)
{
char szNoteName[s_cbNoteName];
/*
* Yell loudly and bail if we are going to be writing a core file that is not compatible with
* both Solaris and the 64-bit ELF spec. which dictates 8-byte alignment. See @bugref{5211} comment 3.
*/
{
LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr pszName=%s cchName=%u cchNameAlign=%u, cchName aligns to 4 not 8-bytes!\n", pszName, cchName,
cchNameAlign));
return VERR_INVALID_PARAMETER;
}
{
LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr pszName=%s cbData=%u cbDataAlign=%u, cbData aligns to 4 not 8-bytes!\n", pszName, cbData,
cbDataAlign));
return VERR_INVALID_PARAMETER;
}
static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
ElfNoteHdr.n_namesz = (Elf64_Word)cchName - 1; /* Again a discrepancy between ELF-64 and Solaris (@bugref{5211} comment 3), we will follow ELF-64 */
/*
* Write note header.
*/
if (RT_SUCCESS(rc))
{
/*
* Write note name.
*/
if (RT_SUCCESS(rc))
{
/*
* Write note name padding if required.
*/
if (cchNameAlign > cchName)
if (RT_SUCCESS(rc))
{
/*
* Write note data.
*/
if (RT_SUCCESS(rc))
{
/*
* Write note data padding if required.
*/
if (cbDataAlign > cbData)
}
}
}
}
if (RT_FAILURE(rc))
LogRel((DBGFLOG_NAME ": RTFileWrite failed. rc=%Rrc pszName=%s cchName=%u cchNameAlign=%u cbData=%u cbDataAlign=%u\n",
return rc;
}
/**
* Count the number of memory ranges that go into the core file.
*
* We cannot do a page-by-page dump of the entire guest memory as there will be
* way too many program header entries. Also we don't want to dump MMIO regions
* which means we cannot have a 1:1 mapping between core file offset and memory
* offset. Instead we dump the memory in ranges. A memory range is a contiguous
* memory area suitable for dumping to a core file.
*
* @param pVM Pointer to the VM.
*
* @return Number of memory ranges
*/
{
return PGMR3PhysGetRamRangeCount(pVM);
}
/**
* Worker function for dbgfR3CoreWrite which does the writing.
*
* @returns VBox status code
* @param pVM Pointer to the VM.
* @param hFile The file to write to. Caller closes this.
*/
{
/*
* Collect core information.
*/
uint16_t const cMemRanges = cu32MemRanges < UINT16_MAX - 1 ? cu32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
Log((DBGFLOG_NAME ": CoreDescriptor Version=%u Revision=%u\n", CoreDescriptor.u32VBoxVersion, CoreDescriptor.u32VBoxRevision));
/*
* Compute the file layout (see pg_dbgf_vmcore).
*/
/*
* Write ELF header.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Write PT_NOTE program header.
*/
offNoteSectionData, /* file offset to contents */
cbNoteSectionData, /* size in core file */
cbNoteSectionData, /* size in memory */
0); /* physical address */
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Write PT_LOAD program header for each memory range.
*/
{
bool fIsMmio;
if (RT_FAILURE(rc))
{
return rc;
}
offMemRange, /* file offset to contents */
cbFileRange, /* size in core file */
cbMemRange, /* size in memory */
GCPhysStart); /* physical address */
if (RT_FAILURE(rc))
{
LogRel((DBGFLOG_NAME ": Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n",
return rc;
}
}
/*
* Write the Core descriptor note header and data.
*/
rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, s_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor));
if (RT_FAILURE(rc))
{
LogRel((DBGFLOG_NAME ": Elf64WriteNoteHdr failed for Note '%s' rc=%Rrc\n", s_pcszCoreVBoxCore, rc));
return rc;
}
/*
* Write the CPU context note headers and data.
*/
/** @todo r=ramshankar: Dump a more standardized CPU structure rather than
* dumping CPUMCTX and bump the core file version number. */
{
if (RT_FAILURE(rc))
{
return rc;
}
}
/*
* Write memory ranges.
*/
{
bool fIsMmio;
if (RT_FAILURE(rc))
{
return rc;
}
if (fIsMmio)
continue;
/*
* Write page-by-page of this memory range.
*
* The read function may fail on MMIO ranges, we write these as zero
* pages for now (would be nice to have the VGA bits there though).
*/
{
if (RT_FAILURE(rc))
{
if (rc != VERR_PGM_PHYS_PAGE_RESERVED)
LogRel((DBGFLOG_NAME ": PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange, iPage, rc));
}
if (RT_FAILURE(rc))
{
return rc;
}
}
}
return rc;
}
/**
* EMT Rendezvous worker function for DBGFR3CoreWrite.
*
* @param pVM Pointer to the VM.
* @param pVCpu The handle of the calling VCPU.
* @param pvData Opaque data.
*
* @return VBox status code.
*/
{
/*
* Validate input.
*/
/*
* Create the core file.
*/
| (0600 << RTFILE_O_CREATE_MODE_SHIFT);
if (RT_SUCCESS(rc))
{
}
else
return rc;
}
/**
* Write core dump of the guest.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param pszFilename The name of the file to which the guest core
* dump should be written.
* @param fReplaceFile Whether to replace the file or not.
*
* @remarks The VM may need to be suspended before calling this function in
* order to truly stop all device threads and drivers. This function
* only synchronizes EMTs.
*/
{
/*
* Pass the core write request down to EMT rendezvous which makes sure
* other EMTs, if any, are not running. IO threads could still be running
* but we don't care about them.
*/
int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWriteRendezvous, &CoreData);
if (RT_SUCCESS(rc))
else
return rc;
}