tstRTCoreDump.cpp revision c74a793852e1fd8994bf9bd2495e1b731ed4ac09
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/* $Id$ */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/** @file
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * IPRT Testcase - Core Dumper.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Copyright (C) 2010 Oracle Corporation
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * available from http://www.virtualbox.org. This file is free software;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * General Public License (GPL) as published by the Free Software
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * The contents of this file may alternatively be used under the terms
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * of the Common Development and Distribution License Version 1.0
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * VirtualBox OSE distribution, in which case the provisions of the
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * CDDL are applicable instead of those of the GPL.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * You may elect to license modified versions of this file under the
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * terms and conditions of either the GPL or the CDDL or both.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/*******************************************************************************
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync* Header Files *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync*******************************************************************************/
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/types.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/file.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/err.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/dir.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/path.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/string.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/stream.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/initterm.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/thread.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/param.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include <iprt/asm.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#include "tstRTCoreDump.h"
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#ifdef RT_OS_SOLARIS
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# include <signal.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# include <unistd.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# include <errno.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# include <zone.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# include <sys/proc.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# include <sys/sysmacros.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# include <sys/systeminfo.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# include <sys/mman.h>
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#endif /* RT_OS_SOLARIS */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/*******************************************************************************
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync* Defined Constants And Macros *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync*******************************************************************************/
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#define CORELOG(a) RTPrintf a
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#define CORELOGREL(a) RTPrintf a
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * VBOXSOLCORETYPE: Whether this is an old or new style core.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsynctypedef enum VBOXSOLCORETYPE
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync enmOldEra = 0x01d, /* old */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync enmNewEra = 0x5c151 /* sci-fi */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync} VBOXSOLCORETYPE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncstatic unsigned volatile g_cErrors = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncvolatile bool g_fCoreDumpInProgress = false;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Reads a file in a given offset making sure an interruption doesn't cause a failure.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param hFile Handle to the file to read.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pv Where to store the read data.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param cb Size of the data to read.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param off Offset to read from.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return Number of bytes actually read.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncstatic ssize_t ReadFileAt(RTFILE hFile, void *pv, size_t cb, RTFOFF off)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (1)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ssize_t cbRead = pread(hFile, pv, cb, off);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( cbRead < 0
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && errno == EINTR)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync continue;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return cbRead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Determines endianness of the system. Just for completeness.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return Will return false if system is little endian, true otherwise.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncstatic bool IsBigEndian()
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync const int i = 1;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char *p = (char *)&i;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (p[0] == 1)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return false;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return true;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Reads from a file making sure an interruption doesn't cause a failure.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param hFile Handle to the file to read.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pv Where to store the read data.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param cbToRead Size of data to read.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncstatic int ReadFileNoIntr(RTFILE hFile, void *pv, size_t cbToRead)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = VERR_READ_ERROR;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (1)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = RTFileRead(hFile, pv, cbToRead, NULL /* Read all */);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (rc == VERR_INTERRUPTED)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync continue;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Writes to a file making sure an interruption doesn't cause a failure.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param hFile Handle to the file to write.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pv Pointer to what to write.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param cbToRead Size of data to write.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncstatic int WriteFileNoIntr(RTFILE hFile, const void *pcv, size_t cbToRead)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = VERR_READ_ERROR;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (1)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = RTFileWrite(hFile, pcv, cbToRead, NULL /* Write all */);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (rc == VERR_INTERRUPTED)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync continue;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read from a given offet in the process' address space.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxProc Pointer to the VBox process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pv Where to read the data into.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param cb Size of the read buffer.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param off Offset to read from.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VINF_SUCCESS, if all the given bytes was read in, otherwise VERR_READ_ERROR.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncstatic ssize_t ReadProcAddrSpace(PVBOXPROCESS pVBoxProc, void *pv, size_t cb, RTFOFF off)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ssize_t cbRead = ReadFileAt(pVBoxProc->hAs, pv, cb, off);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (cbRead == (ssize_t)cb)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_READ_ERROR;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Determines if the current process' architecture is suitable for dumping core.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxProc Pointer to the VBox process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return true if the architecture matches the current one.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncinline bool IsProcArchNative(PVBOXPROCESS pVBoxProc)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return pVBoxProc->ProcInfo.pr_dmodel == PR_MODEL_NATIVE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Helper function to get the size of a file given it's path.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pszPath Pointer to the full path of the file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return The size of the file in bytes.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncsize_t GetFileSize(const char *pszPath)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cb = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFILE hFile;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTFileOpen(&hFile, pszPath, RTFILE_O_OPEN | RTFILE_O_READ);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileGetSize(hFile, (uint64_t *)&cb);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(hFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("GetFileSize failed to open %s rc=%Rrc\n", pszPath, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return cb;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Pre-compute and pre-allocate sufficient memory for dumping core.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * This is meant to be called once, as a single-large anonymously
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * mapped memory area which will be used during the core dumping routines.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint AllocMemoryArea(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore->pvCore == NULL, VERR_ALREADY_EXISTS);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore->VBoxProc.Process != NIL_RTPROCESS, VERR_PROCESS_NOT_FOUND);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync struct VBOXSOLPREALLOCTABLE
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync const char *pszFilePath; /* Proc based path */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbHeader; /* Size of header */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbEntry; /* Size of each entry in file */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbAccounting; /* Size of each accounting entry per entry */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync } aPreAllocTable[] = {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "/proc/%d/map", 0, sizeof(prmap_t), sizeof(VBOXSOLMAPINFO) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "/proc/%d/auxv", 0, 0, 0 },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "/proc/%d/lpsinfo", sizeof(prheader_t), sizeof(lwpsinfo_t), sizeof(VBOXSOLTHREADINFO) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "/proc/%d/lstatus", 0, 0, 0 },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "/proc/%d/ldt", 0, 0, 0 },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "/proc/%d/cred", sizeof(prcred_t), sizeof(gid_t), 1 },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "/proc/%d/priv", sizeof(prpriv_t), sizeof(priv_chunk_t), 1 },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync };
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cb = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (int i = 0; i < (int)RT_ELEMENTS(aPreAllocTable); i++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), aPreAllocTable[i].pszFilePath, (int)pVBoxCore->VBoxProc.Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbFile = GetFileSize(szPath);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += cbFile;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( cbFile > 0
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && aPreAllocTable[i].cbEntry > 0
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && aPreAllocTable[i].cbAccounting > 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ((cbFile - aPreAllocTable[i].cbHeader) / aPreAllocTable[i].cbEntry) * aPreAllocTable[i].cbAccounting;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += aPreAllocTable[i].cbHeader;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Make Room for our own mapping accountant entry which will also be included in the core.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += sizeof(VBOXSOLMAPINFO);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Allocate the required space, plus some extra room.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += _128K;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync void *pv = mmap(NULL, cb, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1 /* fd */, 0 /* offset */);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pv)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("AllocMemoryArea: memory area of %u bytes allocated.\n", cb));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->pvCore = pv;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->pvFree = pv;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->cbCore = cb;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("AllocMemoryArea: failed cb=%u\n", cb));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_NO_MEMORY;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Free memory area used by the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncvoid FreeMemoryArea(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pVBoxCore->pvCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pVBoxCore->cbCore > 0);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync munmap(pVBoxCore->pvCore, pVBoxCore->cbCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("FreeMemoryArea: memory area of %u bytes freed.\n", pVBoxCore->cbCore));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->pvCore = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->pvFree= NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->cbCore = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Get a chunk from the area of allocated memory.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param cb Size of requested chunk.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return Pointer to allocated memory, or NULL on failure.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncvoid *GetMemoryChunk(PVBOXCORE pVBoxCore, size_t cb)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, NULL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore->pvCore, NULL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore->pvFree, NULL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbAllocated = (char *)pVBoxCore->pvFree - (char *)pVBoxCore->pvCore;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (cbAllocated < pVBoxCore->cbCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char *pb = (char *)pVBoxCore->pvFree;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->pvFree = pb + cb;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return pb;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Reads the proc file's content into a newly allocated buffer.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pszFileFmt Only the name of the file to read from (/proc/<pid> will be prepended)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param ppv Where to store the allocated buffer.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pcb Where to store size of the buffer.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ProcReadFileInto(PVBOXCORE pVBoxCore, const char *pszProcFileName, void **ppv, size_t *pcb)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/%s", (int)pVBoxCore->VBoxProc.Process, pszProcFileName);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFILE hFile;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileGetSize(hFile, (uint64_t *)pcb);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (*pcb > 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *ppv = GetMemoryChunk(pVBoxCore, *pcb);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (*ppv)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadFileNoIntr(hFile, *ppv, *pcb);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_NO_MEMORY;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pcb = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *ppv = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(hFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ProcReadFileInto: failed to open %s. rc=%Rrc\n", szPath, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Reads an ELF header from the given offset in a file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param hFile The file to read.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param off The offset where ELF header resides.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pHdr Where to store the read-in ELF header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pcSecHdrs Where to store the number of section headers.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pSecHdrIndex Where to store the section header index.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pcProgHdrs Where to store the number of program headers.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ElfReadHdr(RTFILE hFile, RTFOFF off, Ehdr *pHdr, uint32_t *pcSecHdrs, uint32_t *pSecHdrIndex, uint32_t *pcProgHdrs)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read ELF header from the given position.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = ReadFileAt(hFile, pHdr, sizeof(Ehdr), off) != sizeof(Ehdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadHdr: pfnRead failed rc=%d\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_READ_ERROR;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Validate ELF header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( pHdr->e_ident[EI_MAG0] != ELFMAG0
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || pHdr->e_ident[EI_MAG1] != ELFMAG1
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || pHdr->e_ident[EI_MAG2] != ELFMAG2
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || pHdr->e_ident[EI_MAG3] != ELFMAG3
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#ifdef BIG_ENDIAN
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || pHdr->e_ident[EI_DATA] != ELFDATA2MSB
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || pHdr->e_ident[EI_DATA] != ELFDATA2LSB
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#endif
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || pHdr->e_ident[EI_VERSION] != EV_CURRENT)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadHdr: Bad format(1)\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_BAD_EXE_FORMAT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#ifdef RT_ARCH_AMD64
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pHdr->e_ident[EI_CLASS] != ELFCLASS64
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || ( pHdr->e_machine != EM_AMD64
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && pHdr->e_machine != EM_X86_64)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pHdr->e_ident[EI_CLASS] != ELFCLASS32
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || ( pHdr->e_machine != EM_386
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && pHdr->e_machine != EM_486)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#endif
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync )
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadHdr: Bad format(2)\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_BAD_EXE_FORMAT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( pHdr->e_type != ET_EXEC
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && pHdr->e_type != ET_DYN)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadHdr: Bad format(3)\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_BAD_EXE_FORMAT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( pHdr->e_shoff < pHdr->e_ehsize
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && !(pHdr->e_shoff && pHdr->e_shnum))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadHdr: Section headers overlap ELF header!\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_BAD_EXE_FORMAT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint64_t cbFileSize = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = RTFileGetSize(hFile, &cbFileSize);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( RT_SUCCESS(rc)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && ( pHdr->e_shoff + pHdr->e_shnum * pHdr->e_shentsize > cbFileSize
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || pHdr->e_shoff + pHdr->e_shnum * pHdr->e_shentsize < pHdr->e_shoff))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadHdr: Section headers goes beyond file cbFileSize=%u shoff=%u\n", cbFileSize, pHdr->e_shoff));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_BAD_EXE_FORMAT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pcSecHdrs = pHdr->e_shnum;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pSecHdrIndex = pHdr->e_shstrndx;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pcProgHdrs = pHdr->e_phnum;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * If pcSecHdrs, pSecHdrIndex or pcProgHdrs is at it's max value, we need to
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * read the section header at index 0 for getting the real values.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( (*pcSecHdrs == 0 && pHdr->e_shoff != 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || *pSecHdrIndex == SHN_XINDEX
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || *pcProgHdrs == PN_XNUM)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync Shdr SecHdr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pHdr->e_shoff == 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadHdr: invalid section header offset %d\n", pHdr->e_shoff));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_ELF_EXE_NOT_SUPPORTED;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadFileAt(hFile, &SecHdr, sizeof(SecHdr), pHdr->e_shoff);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (*pcSecHdrs == 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pcSecHdrs = SecHdr.sh_size;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (*pSecHdrIndex == SHN_XINDEX)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pSecHdrIndex = SecHdr.sh_link;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (*pcProgHdrs == PN_XNUM && SecHdr.sh_info != 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pcProgHdrs = SecHdr.sh_info;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read a section header from an ELF file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param hFile The file to read.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param offElfStart Offset that represents the start of the ELF header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pHdr Pointer to the ELF header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param cSecHdrs Number of section headers.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param SecHdrIndex Index into the section header table.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param ppvSecHdr Where to store the pointer to the read-in section header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pcbSecHdr Where to store the size of the read-in section header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param ppszSecHdrStr Where to store the section header string table.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pcbSecHdrStr Where to store the size of the section header string table.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ElfReadSecHdr(PVBOXCORE pVBoxCore, RTFILE hFile, RTFOFF offElfStart, const Ehdr *pHdr, uint32_t cSecHdrs, uint32_t SecHdrIndex,
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync void **ppvSecHdr, size_t *pcbSecHdr, char **ppszSecHdrStr, size_t *pcbSecHdrStr)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Since we use e_shentsize to index into the array of section headers, make sure it's 8-byte aligned.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * We use all the members through sh_entsize (on both 32, 64 bit) so e_shentsize must be large enough.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync Shdr *pSecHdr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ssize_t cSize = RT_OFFSETOF(Shdr, sh_entsize) + sizeof(pSecHdr->sh_entsize);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( pHdr->e_shentsize < cSize
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || (pHdr->e_shentsize & 3) != 0
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || SecHdrIndex > cSecHdrs)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadSecHdr: invalid format\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_BAD_EXE_FORMAT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Allocate & read in the section header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pcbSecHdr = cSecHdrs * pHdr->e_shentsize;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *ppvSecHdr = GetMemoryChunk(pVBoxCore, *pcbSecHdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!*ppvSecHdr)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadFileAt(hFile, *ppvSecHdr, *pcbSecHdr, pHdr->e_shoff);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Allocate & read in the section string table.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pSecHdr = (Shdr *)((char *)*ppvSecHdr + SecHdrIndex * pHdr->e_shentsize);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pSecHdr->sh_size > 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pcbSecHdrStr = pSecHdr->sh_size;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *ppszSecHdrStr = (char *)GetMemoryChunk(pVBoxCore, *pcbSecHdrStr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (*ppszSecHdrStr)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadFileAt(hFile, *ppszSecHdrStr, *pcbSecHdrStr, pSecHdr->sh_offset);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync (*ppszSecHdrStr)[*pcbSecHdrStr - 1] = '\0';
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadSecHdr: failed to read section header string table at %u\n", pSecHdr->sh_offset));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_NO_MEMORY;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadSecHdr: invalid section header string table size\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_BAD_EXE_FORMAT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfReadSecHdr: failed to read section header at %u\n", pHdr->e_shoff));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *ppvSecHdr = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *pcbSecHdr = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_NO_MEMORY;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read process information (format psinfo_t) from /proc.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcInfo(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFILE hFile;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/psinfo", (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbProcInfo = sizeof(psinfo_t);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadFileNoIntr(hFile, &pVBoxProc->ProcInfo, cbProcInfo);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(hFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read process status (format pstatus_t) from /proc.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcStatus(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFILE hFile;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/status", (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbRead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbProcStatus = sizeof(pstatus_t);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertCompile(sizeof(pstatus_t) == sizeof(pVBoxProc->ProcStatus));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadFileNoIntr(hFile, &pVBoxProc->ProcStatus, cbProcStatus);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(hFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read process credential information (format prcred_t + array of guid_t)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @remarks Should not be called before successful call to @see AllocMemoryArea()
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcCred(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return ProcReadFileInto(pVBoxCore, "cred", &pVBoxProc->pvCred, &pVBoxProc->cbCred);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read process privilege information (format prpriv_t + array of priv_chunk_t)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @remarks Should not be called before successful call to @see AllocMemoryArea()
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcPriv(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = ProcReadFileInto(pVBoxCore, "priv", (void **)&pVBoxProc->pPriv, &pVBoxProc->cbPriv);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pcPrivImpl = getprivimplinfo();
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!pVBoxProc->pcPrivImpl)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcPriv: getprivimplinfo returned NULL.\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_INVALID_STATE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read process LDT information (format array of struct ssd) from /proc.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxProc Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @remarks Should not be called before successful call to @see AllocMemoryArea()
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcLdt(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return ProcReadFileInto(pVBoxCore, "ldt", &pVBoxProc->pvLdt, &pVBoxProc->cbLdt);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read process auxiliary vectors (format auxv_t) for the process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @remarks Should not be called before successful call to @see AllocMemoryArea()
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcAuxVecs(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFILE hFile = NIL_RTFILE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/auxv", (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcAuxVecs: RTFileOpen %s failed rc=%Rrc\n", szPath, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbAuxFile = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileGetSize(hFile, (uint64_t *)&cbAuxFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (cbAuxFile >= sizeof(auxv_t))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pAuxVecs = (auxv_t*)GetMemoryChunk(pVBoxCore, cbAuxFile + sizeof(auxv_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->pAuxVecs)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadFileNoIntr(hFile, pVBoxProc->pAuxVecs, cbAuxFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /* Terminate list of vectors */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->cAuxVecs = cbAuxFile / sizeof(auxv_t);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("ReadProcAuxVecs: cbAuxFile=%u auxv_t size %d cAuxVecs=%u\n", cbAuxFile, sizeof(auxv_t), pVBoxProc->cAuxVecs));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->cAuxVecs > 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pAuxVecs[pVBoxProc->cAuxVecs].a_type = AT_NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pAuxVecs[pVBoxProc->cAuxVecs].a_un.a_val = 0L;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(hFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcAuxVecs: Invalid vector count %u\n", pVBoxProc->cAuxVecs));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_READ_ERROR;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcAuxVecs: ReadFileNoIntr failed. rc=%Rrc cbAuxFile=%u\n", rc, cbAuxFile));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pAuxVecs = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->cAuxVecs = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcAuxVecs: no memory for %u bytes\n", cbAuxFile + sizeof(auxv_t)));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_NO_MEMORY;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcAuxVecs: aux file too small %u, expecting %u or more\n", cbAuxFile, sizeof(auxv_t)));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(hFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Find an element in the process' auxiliary vector.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsynclong GetAuxVal(PVBOXPROCESS pVBoxProc, int Type)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxProc, -1);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->pAuxVecs)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync auxv_t *pAuxVec = pVBoxProc->pAuxVecs;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (; pAuxVec->a_type != AT_NULL; pAuxVec++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pAuxVec->a_type == Type)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return pAuxVec->a_un.a_val;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return -1;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read the process mappings (format prmap_t array).
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @remarks Should not be called before successful call to @see AllocMemoryArea()
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcMappings(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFILE hFile = NIL_RTFILE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/map", (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/as", (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = RTFileOpen(&pVBoxProc->hAs, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Allocate and read all the prmap_t objects from proc.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbMapFile = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileGetSize(hFile, (uint64_t *)&cbMapFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (cbMapFile >= sizeof(prmap_t))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync prmap_t *pMap = (prmap_t*)GetMemoryChunk(pVBoxCore, cbMapFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pMap)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadFileNoIntr(hFile, pMap, cbMapFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->cMappings = cbMapFile / sizeof(prmap_t);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->cMappings > 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Allocate for each prmap_t object, a corresponding VBOXSOLMAPINFO object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pMapInfoHead = (PVBOXSOLMAPINFO)GetMemoryChunk(pVBoxCore, pVBoxProc->cMappings * sizeof(VBOXSOLMAPINFO));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->pMapInfoHead)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Associate the prmap_t with the mapping info object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync Assert(pVBoxProc->pMapInfoHead == NULL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLMAPINFO pCur = pVBoxProc->pMapInfoHead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLMAPINFO pPrev = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (uint64_t i = 0; i < pVBoxProc->cMappings; i++, pMap++, pCur++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pCur->pMap = *pMap;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pPrev)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pPrev->pNext = pCur;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pCur->fError = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Make sure we can read the mapping, otherwise mark them to be skipped.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char achBuf[PAGE_SIZE];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint64_t k = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (k < pCur->pMap.pr_size)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cb = RT_MIN(sizeof(achBuf), pCur->pMap.pr_size - k);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc2 = ReadProcAddrSpace(pVBoxProc, &achBuf, cb, pCur->pMap.pr_vaddr + k);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc2))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMappings: skipping mapping. vaddr=%#x rc=%Rrc\n", pCur->pMap.pr_vaddr, rc2));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Instead of storing the actual mapping data which we failed to read, the core
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * will contain an errno in place. So we adjust the prmap_t's size field too
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * so the program header offsets match.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pCur->pMap.pr_size = RT_ALIGN_Z(sizeof(int), 8);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pCur->fError = errno;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pCur->fError == 0) /* huh!? somehow errno got reset? fake one! EFAULT is nice. */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pCur->fError = EFAULT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync k += cb;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pPrev = pCur;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pPrev)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pPrev->pNext = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(hFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(pVBoxProc->hAs);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->hAs = NIL_RTFILE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("ReadProcMappings: successfully read in %u mappings\n", pVBoxProc->cMappings));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMappings: GetMemoryChunk failed %u\n", pVBoxProc->cMappings * sizeof(VBOXSOLMAPINFO)));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_NO_MEMORY;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMappings: Invalid mapping count %u\n", pVBoxProc->cMappings));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_READ_ERROR;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMappings: FileReadNoIntr failed. rc=%Rrc cbMapFile=%u\n", rc, cbMapFile));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMappings: GetMemoryChunk failed. cbMapFile=%u\n", cbMapFile));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_NO_MEMORY;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(pVBoxProc->hAs);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->hAs = NIL_RTFILE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMappings: failed to open %s. rc=%Rrc\n", szPath, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(hFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Reads the thread information for all threads in the process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @remarks Should not be called before successful call to @see AllocMemoryArea()
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcThreads(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxProc->pCurThreadCtx, VERR_NO_DATA);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read the information for threads.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Format: prheader_t + array of lwpsinfo_t's.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbInfoHdrAndData;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync void *pvInfoHdr = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = ProcReadFileInto(pVBoxCore, "lpsinfo", &pvInfoHdr, &cbInfoHdrAndData);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read the status of threads.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Format: prheader_t + array of lwpstatus_t's.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync void *pvStatusHdr = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbStatusHdrAndData;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ProcReadFileInto(pVBoxCore, "lstatus", &pvStatusHdr, &cbStatusHdrAndData);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync prheader_t *pInfoHdr = (prheader_t *)pvInfoHdr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync prheader_t *pStatusHdr = (prheader_t *)pvStatusHdr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync lwpstatus_t *pStatus = (lwpstatus_t *)((uintptr_t)pStatusHdr + sizeof(prheader_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync lwpsinfo_t *pInfo = (lwpsinfo_t *)((uintptr_t)pInfoHdr + sizeof(prheader_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint64_t cStatus = pStatusHdr->pr_nent;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint64_t cInfo = pInfoHdr->pr_nent;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("ReadProcThreads: read info(%u) status(%u), threads:cInfo=%u cStatus=%u\n", cbInfoHdrAndData, cbStatusHdrAndData, cInfo, cStatus));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Minor sanity size check (remember sizeof lwpstatus_t & lwpsinfo_t is <= size in file per entry).
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( (cbStatusHdrAndData - sizeof(prheader_t)) % pStatusHdr->pr_entsize == 0
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && (cbInfoHdrAndData - sizeof(prheader_t)) % pInfoHdr->pr_entsize == 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Make sure we have a matching lstatus entry for an lpsinfo entry unless
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * it is a zombie thread, in which case we will not have a matching lstatus entry.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (; cInfo != 0; cInfo--)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pInfo->pr_sname != 'Z') /* zombie */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( cStatus == 0
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || pStatus->pr_lwpid != pInfo->pr_lwpid)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcThreads: cStatus = %u pStatuslwpid=%d infolwpid=%d\n", cStatus, pStatus->pr_lwpid, pInfo->pr_lwpid));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_INVALID_STATE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pStatus = (lwpstatus_t *)((uintptr_t)pStatus + pStatusHdr->pr_entsize);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cStatus--;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo = (lwpsinfo_t *)((uintptr_t)pInfo + pInfoHdr->pr_entsize);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Threre can still be more lwpsinfo_t's than lwpstatus_t's, build the
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * lists accordingly.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pStatus = (lwpstatus_t *)((uintptr_t)pStatusHdr + sizeof(prheader_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo = (lwpsinfo_t *)((uintptr_t)pInfoHdr + sizeof(prheader_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cInfo = pInfoHdr->pr_nent;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cStatus = pInfoHdr->pr_nent;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cbThreadInfo = RT_MAX(cStatus, cInfo) * sizeof(VBOXSOLTHREADINFO);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pThreadInfoHead = (PVBOXSOLTHREADINFO)GetMemoryChunk(pVBoxCore, cbThreadInfo);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->pThreadInfoHead)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLTHREADINFO pCur = pVBoxProc->pThreadInfoHead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLTHREADINFO pPrev = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (uint64_t i = 0; i < cInfo; i++, pCur++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pCur->Info = *pInfo;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( pInfo->pr_sname != 'Z'
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && pInfo->pr_lwpid == pStatus->pr_lwpid)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Adjust the context of the dumping thread to reflect the context
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * when the core dump got initiated before whatever signal caused it.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( pStatus /* noid droid */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync && pStatus->pr_lwpid == (id_t)pVBoxProc->hCurThread)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertCompile(sizeof(pStatus->pr_reg) == sizeof(pVBoxProc->pCurThreadCtx->uc_mcontext.gregs));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertCompile(sizeof(pStatus->pr_fpreg) == sizeof(pVBoxProc->pCurThreadCtx->uc_mcontext.fpregs));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memcpy(&pStatus->pr_reg, &pVBoxProc->pCurThreadCtx->uc_mcontext.gregs, sizeof(pStatus->pr_reg));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memcpy(&pStatus->pr_fpreg, &pVBoxProc->pCurThreadCtx->uc_mcontext.fpregs, sizeof(pStatus->pr_fpreg));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertCompile(sizeof(pStatus->pr_lwphold) == sizeof(pVBoxProc->pCurThreadCtx->uc_sigmask));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memcpy(&pStatus->pr_lwphold, &pVBoxProc->pCurThreadCtx->uc_sigmask, sizeof(pStatus->pr_lwphold));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pStatus->pr_ustack = (uintptr_t)&pVBoxProc->pCurThreadCtx->uc_stack;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("ReadProcThreads: patched dumper thread context with pre-dump time context.\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pCur->pStatus = pStatus;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pStatus = (lwpstatus_t *)((uintptr_t)pStatus + pStatusHdr->pr_entsize);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcThreads: missing status for lwp %d\n", pInfo->pr_lwpid));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pCur->pStatus = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pPrev)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pPrev->pNext = pCur;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pPrev = pCur;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo = (lwpsinfo_t *)((uintptr_t)pInfo + pInfoHdr->pr_entsize);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pPrev)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pPrev->pNext = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("ReadProcThreads: successfully read %u threads.\n", cInfo));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->cThreads = cInfo;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcThreads: GetMemoryChunk failed for %u bytes\n", cbThreadInfo));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_NO_MEMORY;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcThreads: Invalid state information for threads.\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcThreads: huh!? cbStatusHdrAndData=%u prheader_t=%u entsize=%u\n", cbStatusHdrAndData,
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sizeof(prheader_t), pStatusHdr->pr_entsize));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcThreads: huh!? cbInfoHdrAndData=%u entsize=%u\n", cbInfoHdrAndData, pStatusHdr->pr_entsize));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_INVALID_STATE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcThreads: ReadFileNoIntr failed for \"lpsinfo\" rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcThreads: ReadFileNoIntr failed for \"lstatus\" rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Reads miscellaneous information that is collected as part of a core file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * This may include platform name, zone name and other OS-specific information.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ReadProcMiscInfo(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#ifdef RT_OS_SOLARIS
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read the platform name, uname string and zone name.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = sysinfo(SI_PLATFORM, pVBoxProc->szPlatform, sizeof(pVBoxProc->szPlatform));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (rc == -1)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMiscInfo: sysinfo failed. rc=%d errno=%d\n", rc, errno));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_GENERAL_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->szPlatform[sizeof(pVBoxProc->szPlatform) - 1] = '\0';
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = uname(&pVBoxProc->UtsName);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (rc == -1)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMiscInfo: uname failed. rc=%d errno=%d\n", rc, errno));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_GENERAL_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = getzonenamebyid(pVBoxProc->ProcInfo.pr_zoneid, pVBoxProc->szZoneName, sizeof(pVBoxProc->szZoneName));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (rc < 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ReadProcMiscInfo: getzonenamebyid failed. rc=%d errno=%d zoneid=%d\n", rc, errno, pVBoxProc->ProcInfo.pr_zoneid));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_GENERAL_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->szZoneName[sizeof(pVBoxProc->szZoneName) - 1] = '\0';
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# error Port Me!
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#endif
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * On Solaris use the old-style procfs interfaces but the core file still should have this
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * info. for backward and GDB compatibility, hence the need for this ugly function.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pInfo Pointer to the old prpsinfo_t structure to update.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncvoid GetOldProcessInfo(PVBOXCORE pVBoxCore, prpsinfo_t *pInfo)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pInfo);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync psinfo_t *pSrc = &pVBoxProc->ProcInfo;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memset(pInfo, 0, sizeof(prpsinfo_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_state = pSrc->pr_lwp.pr_state;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_zomb = (pInfo->pr_state == SZOMB);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrCopy(pInfo->pr_clname, sizeof(pInfo->pr_clname), pSrc->pr_lwp.pr_clname);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrCopy(pInfo->pr_fname, sizeof(pInfo->pr_fname), pSrc->pr_fname);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memcpy(&pInfo->pr_psargs, &pSrc->pr_psargs, sizeof(pInfo->pr_psargs));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_nice = pSrc->pr_lwp.pr_nice;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_flag = pSrc->pr_lwp.pr_flag;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_uid = pSrc->pr_uid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_gid = pSrc->pr_gid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_pid = pSrc->pr_pid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_ppid = pSrc->pr_ppid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_pgrp = pSrc->pr_pgid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_sid = pSrc->pr_sid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_addr = (caddr_t)pSrc->pr_addr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_size = pSrc->pr_size;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_rssize = pSrc->pr_rssize;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_wchan = (caddr_t)pSrc->pr_lwp.pr_wchan;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_start = pSrc->pr_start;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_time = pSrc->pr_time;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_pri = pSrc->pr_lwp.pr_pri;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_oldpri = pSrc->pr_lwp.pr_oldpri;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_cpu = pSrc->pr_lwp.pr_cpu;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_ottydev = cmpdev(pSrc->pr_ttydev);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_lttydev = pSrc->pr_ttydev;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_syscall = pSrc->pr_lwp.pr_syscall;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_ctime = pSrc->pr_ctime;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_bysize = pSrc->pr_size * PAGESIZE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_byrssize = pSrc->pr_rssize * PAGESIZE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_argc = pSrc->pr_argc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_argv = (char **)pSrc->pr_argv;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_envp = (char **)pSrc->pr_envp;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_wstat = pSrc->pr_wstat;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_pctcpu = pSrc->pr_pctcpu;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_pctmem = pSrc->pr_pctmem;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_euid = pSrc->pr_euid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_egid = pSrc->pr_egid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_aslwpid = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pInfo->pr_dmodel = pSrc->pr_dmodel;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * On Solaris use the old-style procfs interfaces but the core file still should have this
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * info. for backward and GDB compatibility, hence the need for this ugly function.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pInfo Pointer to the thread info.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pStatus Pointer to the thread status.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pDst Pointer to the old-style status structure to update.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncvoid GetOldProcessStatus(PVBOXCORE pVBoxCore, lwpsinfo_t *pInfo, lwpstatus_t *pStatus, prstatus_t *pDst)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pInfo);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pStatus);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturnVoid(pDst);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memset(pDst, 0, sizeof(prstatus_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_STOPPED)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0001;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_ISTOP)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0002;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_DSTOP)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0004;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_ASLEEP)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0008;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_FORK)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0010;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_RLC)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0020;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /* PR_PTRACE is never set */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_PCINVAL)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0080;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_ISSYS)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0100;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_STEP)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0200;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_KLC)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0400;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_ASYNC)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x0800;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_PTRACE)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x1000;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_MSACCT)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x2000;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_BPTADJ)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x4000;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pStatus->pr_flags & PR_ASLWP)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_flags = 0x8000;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_who = pStatus->pr_lwpid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_why = pStatus->pr_why;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_what = pStatus->pr_what;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_info = pStatus->pr_info;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_cursig = pStatus->pr_cursig;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_sighold = pStatus->pr_lwphold;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_altstack = pStatus->pr_altstack;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_action = pStatus->pr_action;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_syscall = pStatus->pr_syscall;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_nsysarg = pStatus->pr_nsysarg;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_lwppend = pStatus->pr_lwppend;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_oldcontext = (ucontext_t *)pStatus->pr_oldcontext;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memcpy(pDst->pr_reg, pStatus->pr_reg, sizeof(pDst->pr_reg));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memcpy(pDst->pr_sysarg, pStatus->pr_sysarg, sizeof(pDst->pr_sysarg));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrCopy(pDst->pr_clname, sizeof(pDst->pr_clname), pStatus->pr_clname);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_nlwp = pVBoxProc->ProcStatus.pr_nlwp;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_sigpend = pVBoxProc->ProcStatus.pr_sigpend;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_pid = pVBoxProc->ProcStatus.pr_pid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_ppid = pVBoxProc->ProcStatus.pr_ppid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_pgrp = pVBoxProc->ProcStatus.pr_pgid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_sid = pVBoxProc->ProcStatus.pr_sid;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_utime = pVBoxProc->ProcStatus.pr_utime;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_stime = pVBoxProc->ProcStatus.pr_stime;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_cutime = pVBoxProc->ProcStatus.pr_cutime;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_cstime = pVBoxProc->ProcStatus.pr_cstime;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_brkbase = (caddr_t)pVBoxProc->ProcStatus.pr_brkbase;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_brksize = pVBoxProc->ProcStatus.pr_brksize;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_stkbase = (caddr_t)pVBoxProc->ProcStatus.pr_stkbase;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_stksize = pVBoxProc->ProcStatus.pr_stksize;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_processor = (short)pInfo->pr_onpro;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_bind = (short)pInfo->pr_bindpro;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pDst->pr_instr = pStatus->pr_instr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Count the number of sections which will be dumped into the core file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return Number of sections for the core file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncuint32_t CountSections(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /* @todo sections */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync NOREF(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Resume all threads of this process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxProc Pointer to the VBox process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox error code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ResumeAllThreads(PVBOXPROCESS pVBoxProc)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxProc, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szCurThread[128];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PRTDIR pDir = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/lwp", (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szCurThread, sizeof(szCurThread), "%d", (int)pVBoxProc->hCurThread);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int32_t cRunningThreads = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTDirOpen(&pDir, szPath);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Loop through all our threads & resume them.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTDIRENTRY DirEntry;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( !strcmp(DirEntry.szName, ".")
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || !strcmp(DirEntry.szName, ".."))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync continue;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( !strcmp(DirEntry.szName, szCurThread))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync continue;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int32_t ThreadId = RTStrToInt32(DirEntry.szName);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync _lwp_continue((lwpid_t)ThreadId);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ++cRunningThreads;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("ResumeAllThreads: resumed %d threads\n", cRunningThreads));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTDirClose(pDir);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ResumeAllThreads: Failed to open %s\n", szPath));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_READ_ERROR;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Stop all running threads of this process. Before dumping any
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * core we need to make sure the process is quiesced.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxProc Pointer to the VBox process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox error code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint SuspendAllThreads(PVBOXPROCESS pVBoxProc)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szCurThread[128];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PRTDIR pDir = NULL;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/lwp", (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szCurThread, sizeof(szCurThread), "%d", (int)pVBoxProc->hCurThread);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = -1;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint32_t cThreads = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint16_t cTries = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (cTries = 0; cTries < 10; cTries++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint32_t cRunningThreads = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = RTDirOpen(&pDir, szPath);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Loop through all our threads & suspend them, multiple calls to _lwp_suspend() are okay.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTDIRENTRY DirEntry;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( !strcmp(DirEntry.szName, ".")
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync || !strcmp(DirEntry.szName, ".."))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync continue;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if ( !strcmp(DirEntry.szName, szCurThread))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync continue;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int32_t ThreadId = RTStrToInt32(DirEntry.szName);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync _lwp_suspend((lwpid_t)ThreadId);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ++cRunningThreads;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (cTries > 5 && cThreads == cRunningThreads)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cThreads = cRunningThreads;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTDirClose(pDir);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("SuspendAllThreads: Failed to open %s cTries=%d\n", szPath, cTries));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_READ_ERROR;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("Stopped %u threads successfully with %u tries\n", cThreads, cTries));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Returns size of an ELF NOTE header given the size of data the NOTE section will contain.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param cb Size of the data.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return Size of data actually used for NOTE header and section.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncinline size_t ElfNoteHeaderSize(size_t cb)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return sizeof(ELFNOTEHDR) + RT_ALIGN_Z(cb, 4);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write an ELF NOTE header into the core file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param Type Type of this NOTE section.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pcv Opaque pointer to the data, if NULL only computes size.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param cb Size of the data.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ElfWriteNoteHeader(PVBOXCORE pVBoxCore, uint_t Type, const void *pcv, size_t cb)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pcv, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(cb > 0, VERR_NO_DATA);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore->pfnWriter, VERR_WRITE_ERROR);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore->hCoreFile, VERR_INVALID_HANDLE);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = VERR_GENERAL_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#ifdef RT_OS_SOLARIS
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ELFNOTEHDR ElfNoteHdr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RT_ZERO(ElfNoteHdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfNoteHdr.achName[0] = 'C';
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfNoteHdr.achName[1] = 'O';
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfNoteHdr.achName[2] = 'R';
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfNoteHdr.achName[3] = 'E';
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfNoteHdr.Hdr.n_namesz = 5;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfNoteHdr.Hdr.n_type = Type;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfNoteHdr.Hdr.n_descsz = RT_ALIGN_Z(cb, 4);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write note header and description.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ElfNoteHdr, sizeof(ElfNoteHdr));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, pcv, ElfNoteHdr.Hdr.n_descsz);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteNote: pfnWriter failed. Type=%d rc=%Rrc\n", Type, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#error Port Me!
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#endif
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Computes the size of NOTE section for the given core type.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Solaris has two types of program header information (new and old).
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param enmType Type of core file information required.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return Size of NOTE section.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncsize_t ElfNoteSectionSize(PVBOXCORE pVBoxCore, VBOXSOLCORETYPE enmType)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cb = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync switch (enmType)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync case enmOldEra:
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(prpsinfo_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(pVBoxProc->cAuxVecs * sizeof(auxv_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(strlen(pVBoxProc->szPlatform));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLTHREADINFO pThreadInfo = pVBoxProc->pThreadInfoHead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (pThreadInfo)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pThreadInfo->pStatus)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(prstatus_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(prfpregset_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pThreadInfo = pThreadInfo->pNext;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync case enmNewEra:
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(psinfo_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(pstatus_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(pVBoxProc->cAuxVecs * sizeof(auxv_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(strlen(pVBoxProc->szPlatform) + 1);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(struct utsname));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(core_content_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(pVBoxProc->cbCred);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->pPriv)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(PRIV_PRPRIV_SIZE(pVBoxProc->pPriv)); /* Ought to be same as cbPriv!? */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->pcPrivImpl)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(PRIV_IMPL_INFO_SIZE(pVBoxProc->pcPrivImpl));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(strlen(pVBoxProc->szZoneName) + 1);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->cbLdt > 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(pVBoxProc->cbLdt);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLTHREADINFO pThreadInfo = pVBoxProc->pThreadInfoHead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (pThreadInfo)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(lwpsinfo_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pThreadInfo->pStatus)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync cb += ElfNoteHeaderSize(sizeof(lwpstatus_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pThreadInfo = pThreadInfo->pNext;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync default:
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfNoteSectionSize: Unknown segment era %d\n", enmType));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return cb;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write the note section for the given era into the core file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Solaris has two types of program header information (new and old).
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param enmType Type of core file information required.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ElfWriteNoteSection(PVBOXCORE pVBoxCore, VBOXSOLCORETYPE enmType)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = VERR_GENERAL_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#ifdef RT_OS_SOLARIS
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync typedef int (*PFNELFWRITENOTEHDR)(PVBOXCORE pVBoxCore, uint_t, const void *pcv, size_t cb);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync typedef struct ELFWRITENOTE
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync const char *pszType;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint_t Type;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync const void *pcv;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cb;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync } ELFWRITENOTE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync switch (enmType)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync case enmOldEra:
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ELFWRITENOTE aElfNotes[] =
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_PRPSINFO", NT_PRPSINFO, &pVBoxProc->ProcInfoOld, sizeof(prpsinfo_t) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_AUXV", NT_AUXV, pVBoxProc->pAuxVecs, pVBoxProc->cAuxVecs * sizeof(auxv_t) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_PLATFORM", NT_PLATFORM, pVBoxProc->szPlatform, strlen(pVBoxProc->szPlatform) + 1 }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync };
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (unsigned i = 0; i < RT_ELEMENTS(aElfNotes); i++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteNoteHeader(pVBoxCore, aElfNotes[i].Type, aElfNotes[i].pcv, aElfNotes[i].cb);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteNoteSection: ElfWriteNoteHeader failed for %s. rc=%Rrc\n", aElfNotes[i].pszType, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write old-style thread info., they contain nothing about zombies,
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * so we just skip if there is no status information for them.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLTHREADINFO pThreadInfo = pVBoxProc->pThreadInfoHead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (; pThreadInfo; pThreadInfo = pThreadInfo->pNext)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!pThreadInfo->pStatus)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync continue;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync prstatus_t OldProcessStatus;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync GetOldProcessStatus(pVBoxCore, &pThreadInfo->Info, pThreadInfo->pStatus, &OldProcessStatus);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteNoteHeader(pVBoxCore, NT_PRSTATUS, &OldProcessStatus, sizeof(prstatus_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteNoteHeader(pVBoxCore, NT_PRFPREG, &pThreadInfo->pStatus->pr_fpreg, sizeof(prfpregset_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteSegment: ElfWriteNote failed for NT_PRFPREF. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteSegment: ElfWriteNote failed for NT_PRSTATUS. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync case enmNewEra:
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ELFWRITENOTE aElfNotes[] =
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_PSINFO", NT_PSINFO, &pVBoxProc->ProcInfo, sizeof(psinfo_t) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_PSTATUS", NT_PSTATUS, &pVBoxProc->ProcStatus, sizeof(pstatus_t) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_AUXV", NT_AUXV, pVBoxProc->pAuxVecs, pVBoxProc->cAuxVecs * sizeof(auxv_t) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_PLATFORM", NT_PLATFORM, pVBoxProc->szPlatform, strlen(pVBoxProc->szPlatform) + 1 },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_UTSNAME", NT_UTSNAME, &pVBoxProc->UtsName, sizeof(struct utsname) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_CONTENT", NT_CONTENT, &pVBoxProc->CoreContent, sizeof(core_content_t) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_PRCRED", NT_PRCRED, pVBoxProc->pvCred, pVBoxProc->cbCred },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_PRPRIV", NT_PRPRIV, pVBoxProc->pPriv, PRIV_PRPRIV_SIZE(pVBoxProc->pPriv) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_PRPRIVINFO", NT_PRPRIVINFO, pVBoxProc->pcPrivImpl, PRIV_IMPL_INFO_SIZE(pVBoxProc->pcPrivImpl) },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "NT_ZONENAME", NT_ZONENAME, pVBoxProc->szZoneName, strlen(pVBoxProc->szZoneName) + 1 }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync };
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (unsigned i = 0; i < RT_ELEMENTS(aElfNotes); i++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteNoteHeader(pVBoxCore, aElfNotes[i].Type, aElfNotes[i].pcv, aElfNotes[i].cb);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteNoteSection: ElfWriteNoteHeader failed for %s. rc=%Rrc\n", aElfNotes[i].pszType, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write new-style thread info., missing lwpstatus_t indicates it's a zombie thread
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * we only dump the lwpsinfo_t in that case.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLTHREADINFO pThreadInfo = pVBoxProc->pThreadInfoHead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (; pThreadInfo; pThreadInfo = pThreadInfo->pNext)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteNoteHeader(pVBoxCore, NT_LWPSINFO, &pThreadInfo->Info, sizeof(lwpsinfo_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteNoteSection: ElfWriteNoteHeader for NT_LWPSINFO failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pThreadInfo->pStatus)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteNoteHeader(pVBoxCore, NT_LWPSTATUS, pThreadInfo->pStatus, sizeof(lwpstatus_t));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteNoteSection: ElfWriteNoteHeader for NT_LWPSTATUS failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync default:
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteNoteSection: Invalid type %d\n", enmType));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_GENERAL_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync# error Port Me!
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#endif
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write mappings into the core file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ElfWriteMappings(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = VERR_GENERAL_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLMAPINFO pMapInfo = pVBoxProc->pMapInfoHead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (pMapInfo)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!pMapInfo->fError)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint64_t k = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char achBuf[PAGE_SIZE];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (k < pMapInfo->pMap.pr_size)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync size_t cb = RT_MIN(sizeof(achBuf), pMapInfo->pMap.pr_size - k);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc2 = ReadProcAddrSpace(pVBoxProc, &achBuf, cb, pMapInfo->pMap.pr_vaddr + k);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc2))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteMappings: Failed to read mapping, can't recover. Bye. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_INVALID_STATE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, achBuf, sizeof(achBuf));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteMappings: pfnWriter failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync k += cb;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char achBuf[RT_ALIGN_Z(sizeof(int), 8)];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RT_ZERO(achBuf);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memcpy(achBuf, &pMapInfo->fError, sizeof(pMapInfo->fError));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (sizeof(achBuf) != pMapInfo->pMap.pr_size)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteMappings: Huh!? something is wrong!\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &achBuf, sizeof(achBuf));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteMappings: pfnWriter(2) failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pMapInfo = pMapInfo->pNext;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write program headers for all mappings into the core file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint ElfWriteMappingHeaders(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync Phdr ProgHdr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RT_ZERO(ProgHdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_type = PT_LOAD;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = VERR_GENERAL_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXSOLMAPINFO pMapInfo = pVBoxProc->pMapInfoHead;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (pMapInfo)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_vaddr = pMapInfo->pMap.pr_vaddr; /* Virtual address of this mapping in the process address space */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_offset = pVBoxCore->offWrite; /* Where this mapping is located in the core file */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_memsz = pMapInfo->pMap.pr_size; /* Size of the memory image of the mapping */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_filesz = pMapInfo->pMap.pr_size; /* Size of the file image of the mapping */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pMapInfo->pMap.pr_mflags & MA_READ)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_flags |= PF_R;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pMapInfo->pMap.pr_mflags & MA_WRITE)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_flags |= PF_W;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pMapInfo->pMap.pr_mflags & MA_EXEC)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_flags |= PF_X;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pMapInfo->fError)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_flags |= PF_SUNW_FAILURE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ProgHdr, sizeof(ProgHdr));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("ElfWriteMappingHeaders: pfnWriter failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->offWrite += ProgHdr.p_filesz;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pMapInfo = pMapInfo->pNext;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write a prepared core file using a user-passed in writer function, requires all threads
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * to be in suspended state (i.e. called after CreateCore).
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pfnWriter Pointer to the writer function to override default writer (NULL uses default).
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @remarks Resumes all suspended threads, unless it's an invalid core.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint WriteCore(PVBOXCORE pVBoxCore, PFNCOREWRITER pfnWriter)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!pVBoxCore->fIsValid)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_INVALID_STATE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pfnWriter)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->pfnWriter = pfnWriter;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync char szPath[PATH_MAX];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Open the process address space file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/as", (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTFileOpen(&pVBoxProc->hAs, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("WriteCore: Failed to open address space, %s. rc=%Rrc\n", szPath, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Create the core file.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(szPath, sizeof(szPath), "/export/home/ram/vbox/out/solaris.amd64/release/bin/%s", pVBoxCore->szCorePath, pVBoxCore->VBoxProc.Process); /* @todo fix this */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = RTFileOpen(&pVBoxCore->hCoreFile, szPath, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE | RTFILE_O_READWRITE | RTFILE_O_DENY_ALL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("WriteCore: failed to open %s. rc=%Rrc\n", szPath, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->offWrite = 0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint32_t cProgHdrs = pVBoxProc->cMappings + 2; /* two PT_NOTE program headers (old, new style) */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync uint32_t cSecHdrs = CountSections(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write the ELF header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync Ehdr ElfHdr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RT_ZERO(ElfHdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_ident[EI_MAG0] = ELFMAG0;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_ident[EI_MAG1] = ELFMAG1;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_ident[EI_MAG2] = ELFMAG2;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_ident[EI_MAG3] = ELFMAG3;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_ident[EI_DATA] = IsBigEndian() ? ELFDATA2MSB : ELFDATA2LSB;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_type = ET_CORE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_version = EV_CURRENT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#ifdef RT_ARCH_AMD64
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_machine = EM_AMD64;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_machine = EM_386;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_ident[EI_CLASS] = ELFCLASS32;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync#endif
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (cProgHdrs >= PN_XNUM)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_phnum = PN_XNUM;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_phnum = cProgHdrs;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_ehsize = sizeof(ElfHdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_phoff = sizeof(ElfHdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_phentsize = sizeof(Phdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ElfHdr.e_shentsize = sizeof(Shdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ElfHdr, sizeof(ElfHdr));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("WriteCore: pfnWriter failed writing ELF header. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Setup program header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync Phdr ProgHdr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RT_ZERO(ProgHdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_type = PT_NOTE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_flags = PF_R;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write old-style NOTE program header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->offWrite += sizeof(ElfHdr) + cProgHdrs * sizeof(ProgHdr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_offset = pVBoxCore->offWrite;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_filesz = ElfNoteSectionSize(pVBoxCore, enmOldEra);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ProgHdr, sizeof(ProgHdr));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("WriteCore: pfnWriter failed writing old-style ELF program Header. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write new-style NOTE program header.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->offWrite += ProgHdr.p_filesz;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_offset = pVBoxCore->offWrite;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ProgHdr.p_filesz = ElfNoteSectionSize(pVBoxCore, enmNewEra);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ProgHdr, sizeof(ProgHdr));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("WriteCore: pfnWriter failed writing new-style ELF program header. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write program headers per mapping.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->offWrite += ProgHdr.p_filesz;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteMappingHeaders(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("Write: ElfWriteMappings failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write old-style note section.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteNoteSection(pVBoxCore, enmOldEra);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("WriteCore: ElfWriteNoteSection old-style failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write new-style section.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteNoteSection(pVBoxCore, enmNewEra);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("WriteCore: ElfWriteNoteSection new-style failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Write all mappings.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ElfWriteMappings(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("WriteCore: ElfWriteMappings failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync goto WriteCoreDone;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncWriteCoreDone:
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxCore->hCoreFile != NIL_RTFILE)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(pVBoxCore->hCoreFile);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->hCoreFile = NIL_RTFILE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (pVBoxProc->hAs != NIL_RTFILE)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTFileClose(pVBoxProc->hAs);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->hAs = NIL_RTFILE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ResumeAllThreads(pVBoxProc);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Takes a process snapshot into a passed-in core object. It has the side-effect of halting
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * all threads which can lead to things like spurious wakeups of threads (if and when threads
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * are ultimately resumed en-masse) already suspended while calling this function.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to a core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pContext Pointer to the caller context thread.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @remarks Halts all threads.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint CreateCore(PVBOXCORE pVBoxCore, ucontext_t *pContext)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pContext, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Initialize core structures.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync memset(pVBoxCore, 0, sizeof(VBOXCORE));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->pfnReader = &ReadFileNoIntr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->pfnWriter = &WriteFileNoIntr;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->fIsValid = false;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->hCoreFile = NIL_RTFILE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->Process = RTProcSelf();
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->hCurThread = _lwp_self(); /* thr_self() */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->hAs = NIL_RTFILE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pCurThreadCtx = pContext;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->CoreContent = CC_CONTENT_DEFAULT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTProcGetExecutableName(pVBoxProc->szExecPath, sizeof(pVBoxProc->szExecPath)); /* this gets full path not just name */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxProc->pszExecName = RTPathFilename(pVBoxProc->szExecPath);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTStrPrintf(pVBoxCore->szCorePath, sizeof(pVBoxCore->szCorePath), "core.vb.%s.%d", pVBoxProc->pszExecName, (int)pVBoxProc->Process);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("tstRTCoreDump: Taking Core %s from Thread %d\n", pVBoxCore->szCorePath, (int)pVBoxProc->hCurThread));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Quiesce the process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = SuspendAllThreads(pVBoxProc);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadProcInfo(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync GetOldProcessInfo(pVBoxCore, &pVBoxProc->ProcInfoOld);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (IsProcArchNative(pVBoxProc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Read process status, information such as number of active LWPs will be invalid since we just quiesced the process.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = ReadProcStatus(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = AllocMemoryArea(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync struct COREACCUMULATORS
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync const char *pszName;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync PFNCOREACCUMULATOR pfnAcc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync bool fOptional;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync } aAccumulators[] =
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "ReadProcLdt", &ReadProcLdt, false },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "ReadProcCred", &ReadProcCred, false },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "ReadProcPriv", &ReadProcPriv, false },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "ReadProcAuxVecs", &ReadProcAuxVecs, false },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "ReadProcMappings", &ReadProcMappings, false },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "ReadProcThreads", &ReadProcThreads, false },
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync { "ReadProcMiscInfo", &ReadProcMiscInfo, false }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync };
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (unsigned i = 0; i < RT_ELEMENTS(aAccumulators); i++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = aAccumulators[i].pfnAcc(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("DumpCore: %s failed. rc=%Rrc\n", aAccumulators[i].pszName, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!aAccumulators[i].fOptional)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->fIsValid = true;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync FreeMemoryArea(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("DumpCore: AllocMemoryArea failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("DumpCore: ReadProcStatus failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("DumpCore: IsProcArchNative failed.\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = VERR_BAD_EXE_FORMAT;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("DumpCore: ReadProcInfo failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Resume threads on failure.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ResumeAllThreads(pVBoxProc);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("DumpCore: SuspendAllThreads failed. Thread bomb!?! rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return rc;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync/**
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Destroy an existing core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @param pVBoxCore Pointer to the core object.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync *
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * @return VBox status code.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint DestroyCore(PVBOXCORE pVBoxCore)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!pVBoxCore->fIsValid)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VERR_INVALID_STATE;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync FreeMemoryArea(pVBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync pVBoxCore->fIsValid = false;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncvoid CoreSigHandler(int Sig, siginfo_t *pSigInfo, void *pvArg)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("CoreSigHandler Sig=%d pvArg=%p\n", Sig, pvArg));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ucontext_t *pContext = (ucontext_t *)pvArg;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!pContext)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("CoreSigHandler: Missing context.\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!ASMAtomicUoReadBool(&g_fCoreDumpInProgress))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ASMAtomicWriteBool(&g_fCoreDumpInProgress, true);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Take a snapshot, then dump core to disk, all threads except this one are halted
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * from before taking the snapshot until writing the core is completely finished.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Any errors would resume all threads if they were halted.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync VBOXCORE VBoxCore;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RT_ZERO(VBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = CreateCore(&VBoxCore, pContext);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync rc = WriteCore(&VBoxCore, &WriteFileNoIntr);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_SUCCESS(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("CoreSigHandler: Successfully wrote core file to disk.\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("CoreSigHandler: WriteCore failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync DestroyCore(&VBoxCore);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("CoreSigHandler: CreateCore failed. rc=%Rrc\n", rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ASMAtomicWriteBool(&g_fCoreDumpInProgress, false);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("CoreSigHandler: Core dump already in progress! Waiting before signalling Sig=%d.\n", Sig));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int64_t iTimeout = 10000; /* timeout (ms) */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync while (!ASMAtomicUoReadBool(&g_fCoreDumpInProgress))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTThreadSleep(200);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync iTimeout -= 200;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (iTimeout <= 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (iTimeout <= 0)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOGREL(("CoreSigHandler: Core dump seems to be stuck. Signalling new signal %d\n", Sig));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync signal(Sig, SIG_DFL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync kill((int)getpid(), Sig);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncstatic DECLCALLBACK(int) SleepyThread(RTTHREAD Thread, void *pvUser)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync NOREF(pvUser);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sleep(10000);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return VINF_SUCCESS;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsyncint main()
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync{
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTR3Init();
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("tstRTCoreDump: TESTING pid=%d\n", getpid()));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Install core dump signal handler.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync struct sigaction sigAction;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sigAction.sa_sigaction = CoreSigHandler;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sigemptyset(&sigAction.sa_mask);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sigAction.sa_flags = SA_RESTART | SA_SIGINFO;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sigaction(SIGSEGV, &sigAction, NULL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sigaction(SIGBUS, &sigAction, NULL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sigaction(SIGUSR1, &sigAction, NULL);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Spawn a few threads.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync RTTHREAD ahThreads[5];
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync for (unsigned i = 0; i < RT_ELEMENTS(ahThreads); i++)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync int rc = RTThreadCreate(&ahThreads[i], SleepyThread, &ahThreads[i], 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "TEST1");
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (RT_FAILURE(rc))
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync {
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("tstRTCoreDump: FAILURE(%d) - %d RTThreadCreate failed, rc=%Rrc\n", __LINE__, i, rc));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync g_cErrors++;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync ahThreads[i] = NIL_RTTHREAD;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync break;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync }
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("Spawned %d threads\n", RT_ELEMENTS(ahThreads)));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Send signal to dump core.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync kill(getpid(), SIGUSR1);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync g_cErrors++;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync sleep(10);
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync /*
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync * Summary.
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync */
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync if (!g_cErrors)
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("tstRTCoreDump: SUCCESS\n"));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync else
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync CORELOG(("tstRTCoreDump: FAILURE - %d errors\n", g_cErrors));
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync return !!g_cErrors;
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync}
c74a793852e1fd8994bf9bd2495e1b731ed4ac09vboxsync