DBGFCoreWrite.cpp revision 31f081f300cba32a91281097d702b29efea261e8
/* $Id$ */
/** @file
* DBGF - Debugger Facility, Guest Core Dump.
*/
/*
* Copyright (C) 2010 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.
*/
/*
* VBox VMCore Format:
* [ ELF 64 Header] -- Only 1
*
* [ PT_NOTE ] -- Only 1
* - Offset into list of Notes (Note Hdr + data) of VBox CPUs.
* - (Any Additional custom Note sections)
*
* [ PT_LOAD ] -- One for each contiguous memory chunk
* - Memory offset
* - File offset
*
* 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 "CPUMInternal.h"
#include "../Runtime/include/internal/ldrELF64.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#ifdef DEBUG_ramshankar
#endif
#define DBGFLOG_NAME "DBGFCoreWrite"
/*
* For now use Solaris-specific padding and namesz length (i.e. includes NULL terminator)
*/
static const int s_cbNoteName = 16;
static const char *s_pcszCoreVBoxCore = "VBOXCORE";
static const char *s_pcszCoreVBoxCpu = "VBOXCPU";
/**
* DBGFCOREDATA: Core data.
*/
typedef struct
{
const char *pszDumpPath; /* File path to dump the core into. */
/**
* 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.
* @param pcbElfHdr Where to store the size of written header to file,
* can be NULL.
*
* @return IPRT status code.
*/
static int Elf64WriteElfHdr(RTFILE hFile, uint16_t cProgHdrs, uint16_t cSecHdrs, uint64_t *pcbElfHdr)
{
/* 32-bit VMs will produce cores with e_machine EM_386. */
#ifdef RT_ARCH_AMD64
#else
#endif
return rc;
}
/**
* 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.
* @param pcbProgHdr Where to store the size of written header to file,
* can be NULL.
*
* @return IPRT status code.
*/
static int Elf64WriteProgHdr(RTFILE hFile, uint32_t Type, uint32_t fFlags, uint64_t offFileData, uint64_t cbFileData, uint64_t cbMemData,
{
*pcbProgHdr = sizeof(ProgHdr);
return rc;
}
/**
* 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 += cbNameAlign;
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.
* @param pcbNoteHdr Where to store the size of written header to file,
* can be NULL.
*
* @return IPRT status code.
*/
static int Elf64WriteNoteHdr(RTFILE hFile, uint16_t Type, const char *pszName, const void *pcvData, uint64_t cbData, uint64_t *pcbNoteHdr)
{
char szNoteName[s_cbNoteName];
static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
/*
* Write note header.
*/
if (RT_SUCCESS(rc))
{
/*
* Write note name.
*/
if (RT_SUCCESS(rc))
{
/*
* Write note name padding if required.
*/
if (cbNameAlign > cbName)
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 cbData=%u cbDataAlign=%u\n", rc, pszName, cbData, cbDataAlign));
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 The VM handle.
*
* @return Number of memory ranges
*/
{
return PGMR3PhysGetRamRangeCount(pVM);
}
/**
* EMT Rendezvous worker function for DBGFR3CoreWrite.
*
* @param pVM The VM handle.
* @param pVCpu The handle of the calling VCPU.
* @param pvData Opaque data.
*
* @return VBox status code.
*/
{
/*
* Validate input.
*/
/*
* Collect core information.
*/
uint16_t cMemRanges = u32MemRanges < UINT16_MAX - 1 ? u32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
LogRel((DBGFLOG_NAME ":CoreDescriptor Version=%u Revision=%u\n", CoreDescriptor.VBoxVersion, CoreDescriptor.VBoxRevision));
/*
* Compute total size of the note section.
*/
/*
* Create the core file.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Write ELF header.
*/
if (RT_FAILURE(rc))
{
goto CoreWriteDone;
}
/*
* Write PT_NOTE program header.
*/
cbNoteSection, /* size in core file */
cbNoteSection, /* size in memory */
0, /* physical address */
&cbProgHdr);
if (RT_FAILURE(rc))
{
goto CoreWriteDone;
}
/*
* Write PT_LOAD program header for each memory range.
*/
{
bool fIsMmio;
if (RT_FAILURE(rc))
{
goto CoreWriteDone;
}
offMemRange, /* file offset to contents */
cbFileRange, /* size in core file */
cbMemRange, /* size in memory */
GCPhysStart, /* physical address */
&cbProgHdr);
if (RT_FAILURE(rc))
{
LogRel((DBGFLOG_NAME ":Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n", iRange,
goto CoreWriteDone;
}
}
/*
* Write the Core descriptor note header and data.
*/
rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, s_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor),
NULL /* pcbNoteHdr */);
if (RT_FAILURE(rc))
{
goto CoreWriteDone;
}
/*
* Write the CPU context note headers and data.
*/
{
rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, s_pcszCoreVBoxCpu, pCpuCtx, sizeof(CPUMCTX), NULL /* pcbNoteHdr */);
if (RT_FAILURE(rc))
{
goto CoreWriteDone;
}
}
/*
* Write memory ranges.
*/
{
bool fIsMmio;
if (RT_FAILURE(rc))
{
goto CoreWriteDone;
}
if (fIsMmio)
continue;
/*
* Write page-by-page of this memory range.
*/
{
if (RT_UNLIKELY(!pvBuf))
{
goto CoreWriteDone;
}
if (RT_FAILURE(rc))
{
/*
* For some reason this failed, write out a zero page instead.
*/
}
if (RT_FAILURE(rc))
{
goto CoreWriteDone;
}
}
}
return rc;
}
/**
* Write core dump of the guest.
*
* @return VBox status code.
* @param pVM The VM handle.
* @param idCpu The target CPU ID.
* @param pszDumpPath The path of the file to dump into, cannot be
* NULL.
*
* @remarks The VM must be suspended before calling this function.
*/
{
/*
* Pass the core write request down to EMT rendezvous which makes sure
* other EMTs, if any, are not running.
*/
}