coredumper-solaris.cpp revision 5241858827a9ef9bcad4f3e9bf3ade21ee9c6f97
/* $Id$ */
/** @file
* IPRT - Custom Core Dumper, Solaris.
*/
/*
* Copyright (C) 2010-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_DEFAULT
#include <iprt/coredumper.h>
#include "coredumper-solaris.h"
#ifdef RT_OS_SOLARIS
# include <syslog.h>
# include <signal.h>
# include <stdlib.h>
# include <unistd.h>
# include <errno.h>
# include <zone.h>
# include <sys/sysmacros.h>
# include <sys/systeminfo.h>
# include <fcntl.h>
# include <ucontext.h>
#endif /* RT_OS_SOLARIS */
#include "internal/ldrELF64.h"
/*******************************************************************************
* Globals *
*******************************************************************************/
static bool volatile g_fCoreDumpSignalSetup = false;
static uint32_t volatile g_fCoreDumpFlags = 0;
static char g_szCoreDumpDir[PATH_MAX] = { 0 };
static char g_szCoreDumpFile[PATH_MAX] = { 0 };
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define CORELOG_NAME "CoreDumper: "
#define CORELOGRELSYS(a) \
do { \
} while (0)
/**
* ELFNOTEHDR: ELF NOTE header.
*/
typedef struct ELFNOTEHDR
{
} ELFNOTEHDR;
typedef ELFNOTEHDR *PELFNOTEHDR;
/**
* Wrapper function to write IPRT format style string to the syslog.
*
* @param pszFormat Format string
*/
static void rtCoreDumperSysLogWrapper(const char *pszFormat, ...)
{
char szBuf[1024];
}
/**
* Determines endianness of the system. Just for completeness.
*
* @return Will return false if system is little endian, true otherwise.
*/
static bool IsBigEndian()
{
const int i = 1;
char *p = (char *)&i;
if (p[0] == 1)
return false;
return true;
}
/**
* Reads from a file making sure an interruption doesn't cause a failure.
*
* @param fd Handle to the file to read.
* @param pv Where to store the read data.
* @param cbToRead Size of data to read.
*
* @return IPRT status code.
*/
{
for (;;)
{
if (cbRead < 0)
{
continue;
return RTErrConvertFromErrno(errno);
}
return VINF_SUCCESS;
return VERR_INTERNAL_ERROR_3;
if (cbRead == 0)
return VERR_EOF;
}
}
/**
* Writes to a file making sure an interruption doesn't cause a failure.
*
* @param fd Handle to the file to write to.
* @param pv Pointer to what to write.
* @param cbToWrite Size of data to write.
*
* @return IPRT status code.
*/
{
for (;;)
{
if (cbWritten < 0)
{
continue;
return RTErrConvertFromErrno(errno);
}
return VINF_SUCCESS;
return VERR_INTERNAL_ERROR_2;
}
}
/**
* Read from a given offset in the process' address space.
*
* @param pSolProc Pointer to the solaris process.
* @param pv Where to read the data into.
* @param cb Size of the read buffer.
* @param off Offset to read from.
*
* @return VINF_SUCCESS, if all the given bytes was read in, otherwise VERR_READ_ERROR.
*/
static ssize_t ProcReadAddrSpace(PRTSOLCOREPROCESS pSolProc, RTFOFF off, void *pvBuf, size_t cbToRead)
{
for (;;)
{
if (cbRead < 0)
{
continue;
return RTErrConvertFromErrno(errno);
}
return VINF_SUCCESS;
return VERR_INTERNAL_ERROR_4;
if (cbRead == 0)
return VERR_EOF;
}
}
/**
* Determines if the current process' architecture is suitable for dumping core.
*
* @param pSolProc Pointer to the solaris process.
*
* @return true if the architecture matches the current one.
*/
{
}
/**
* Helper function to get the size_t compatible file size from a file
* descriptor.
*
* @return The file size (in bytes).
* @param fd The file descriptor.
*/
{
{
return 0;
}
CORELOGRELSYS((CORELOG_NAME "GetFileSizeByFd: fstat failed rc=%Rrc\n", RTErrConvertFromErrno(errno)));
return 0;
}
/**
* Helper function to get the size_t compatible size of a file given its path.
*
* @return The file size (in bytes).
* @param pszPath Pointer to the full path of the file.
*/
{
if (fd < 0)
{
CORELOGRELSYS((CORELOG_NAME "GetFileSizeByName: failed to open %s rc=%Rrc\n", pszPath, RTErrConvertFromErrno(errno)));
return 0;
}
return cb;
}
/**
* Pre-compute and pre-allocate sufficient memory for dumping core.
* This is meant to be called once, as a single-large anonymously
* mapped memory area which will be used during the core dumping routines.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code.
*/
{
static struct
{
const char *pszFilePath; /* Proc based path */
} const s_aPreAllocTable[] =
{
};
for (unsigned i = 0; i < RT_ELEMENTS(s_aPreAllocTable); i++)
{
RTStrPrintf(szPath, sizeof(szPath), s_aPreAllocTable[i].pszFilePath, (int)pSolCore->SolProc.Process);
if ( cbFile > 0
&& s_aPreAllocTable[i].cbEntry > 0)
{
}
}
/*
* Make room for our own mapping accountant entry which will also be included in the core.
*/
cb += sizeof(RTSOLCOREMAPINFO);
/*
* Allocate the required space, plus some extra room.
*/
void *pv = mmap(NULL, cb, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1 /* fd */, 0 /* offset */);
if (pv != MAP_FAILED)
{
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
/**
* Free memory area used by the core object.
*
* @param pSolCore Pointer to the core object.
*/
{
}
/**
* Get a chunk from the area of allocated memory.
*
* @param pSolCore Pointer to the core object.
* @param cb Size of requested chunk.
*
* @return Pointer to allocated memory, or NULL on failure.
*/
{
{
return pb;
}
return NULL;
}
/**
* Reads the proc file's content into a newly allocated buffer.
*
* @param pSolCore Pointer to the core object.
* @param pszFileFmt Only the name of the file to read from (/proc/<pid> will be prepended)
* @param ppv Where to store the allocated buffer.
* @param pcb Where to store size of the buffer.
*
* @return IPRT status code. If the proc file is 0 bytes, VINF_SUCCESS is
* returned with pointed to values of @c ppv, @c pcb set to NULL and 0
* respectively.
*/
static int ProcReadFileInto(PRTSOLCORE pSolCore, const char *pszProcFileName, void **ppv, size_t *pcb)
{
RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/%s", (int)pSolCore->SolProc.Process, pszProcFileName);
int rc = VINF_SUCCESS;
if (fd >= 0)
{
if (*pcb > 0)
{
if (*ppv)
else
rc = VERR_NO_MEMORY;
}
else
{
*pcb = 0;
rc = VINF_SUCCESS;
}
}
else
{
}
return rc;
}
/**
* Read process information (format psinfo_t) from /proc.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code.
*/
{
int rc = VINF_SUCCESS;
if (fd >= 0)
{
}
else
{
}
return rc;
}
/**
* Read process status (format pstatus_t) from /proc.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code.
*/
{
int rc = VINF_SUCCESS;
if (fd >= 0)
{
}
else
{
}
return rc;
}
/**
* Read process credential information (format prcred_t + array of guid_t)
*
* @param pSolCore Pointer to the core object.
*
* @remarks Should not be called before successful call to @see AllocMemoryArea()
* @return IPRT status code.
*/
{
}
/**
* Read process privilege information (format prpriv_t + array of priv_chunk_t)
*
* @param pSolCore Pointer to the core object.
*
* @remarks Should not be called before successful call to @see AllocMemoryArea()
* @return IPRT status code.
*/
{
if (RT_FAILURE(rc))
return rc;
if (!pSolProc->pcPrivImpl)
{
return VERR_INVALID_STATE;
}
return rc;
}
/**
* Read process LDT information (format array of struct ssd) from /proc.
*
* @param pSolProc Pointer to the core object.
*
* @remarks Should not be called before successful call to @see AllocMemoryArea()
* @return IPRT status code.
*/
{
}
/**
* Read process auxiliary vectors (format auxv_t) for the process.
*
* @param pSolCore Pointer to the core object.
*
* @remarks Should not be called before successful call to @see AllocMemoryArea()
* @return IPRT status code.
*/
{
int rc = VINF_SUCCESS;
if (fd < 0)
{
return rc;
}
{
{
if (RT_SUCCESS(rc))
{
/* Terminate list of vectors */
CORELOG((CORELOG_NAME "ProcReadAuxVecs: cbAuxFile=%u auxv_t size %d cAuxVecs=%u\n", cbAuxFile, sizeof(auxv_t),
{
return VINF_SUCCESS;
}
}
else
CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: ReadFileNoIntr failed. rc=%Rrc cbAuxFile=%u\n", rc, cbAuxFile));
}
else
{
CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: no memory for %u bytes\n", cbAuxFile + sizeof(auxv_t)));
rc = VERR_NO_MEMORY;
}
}
else
{
CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: aux file too small %u, expecting %u or more\n", cbAuxFile, sizeof(auxv_t)));
}
return rc;
}
/*
* Find an element in the process' auxiliary vector.
*/
{
{
{
}
}
return -1;
}
/**
* Read the process mappings (format prmap_t array).
*
* @param pSolCore Pointer to the core object.
*
* @remarks Should not be called before successful call to @see AllocMemoryArea()
* @return IPRT status code.
*/
{
int rc = VINF_SUCCESS;
if (fdMap < 0)
{
return rc;
}
{
/*
* Allocate and read all the prmap_t objects from proc.
*/
{
if (pMap)
{
if (RT_SUCCESS(rc))
{
{
/*
* Allocate for each prmap_t object, a corresponding RTSOLCOREMAPINFO object.
*/
pSolProc->pMapInfoHead = (PRTSOLCOREMAPINFO)GetMemoryChunk(pSolCore, pSolProc->cMappings * sizeof(RTSOLCOREMAPINFO));
if (pSolProc->pMapInfoHead)
{
/*
* Associate the prmap_t with the mapping info object.
*/
/*Assert(pSolProc->pMapInfoHead == NULL); - does not make sense */
{
if (pPrev)
/*
* Make sure we can read the mapping, otherwise mark them to be skipped.
*/
uint64_t k = 0;
{
if (RT_FAILURE(rc2))
{
/*
* Instead of storing the actual mapping data which we failed to read, the core
* will contain an errno in place. So we adjust the prmap_t's size field too
* so the program header offsets match.
*/
break;
}
k += cb;
}
}
if (pPrev)
CORELOG((CORELOG_NAME "ProcReadMappings: successfully read in %u mappings\n", pSolProc->cMappings));
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
{
}
}
else
CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: FileReadNoIntr failed. rc=%Rrc cbMapFile=%u\n", rc, cbMapFile));
}
else
{
rc = VERR_NO_MEMORY;
}
}
}
else
return rc;
}
/**
* Reads the thread information for all threads in the process.
*
* @param pSolCore Pointer to the core object.
*
* @remarks Should not be called before successful call to @see AllocMemoryArea()
* @return IPRT status code.
*/
{
/*
* Read the information for threads.
* Format: prheader_t + array of lwpsinfo_t's.
*/
if (RT_SUCCESS(rc))
{
/*
* Read the status of threads.
* Format: prheader_t + array of lwpstatus_t's.
*/
void *pvStatusHdr = NULL;
if (RT_SUCCESS(rc))
{
CORELOG((CORELOG_NAME "ProcReadThreads: read info(%u) status(%u), threads:cInfo=%u cStatus=%u\n", cbInfoHdrAndData,
/*
* Minor sanity size check (remember sizeof lwpstatus_t & lwpsinfo_t is <= size in file per entry).
*/
{
/*
* Make sure we have a matching lstatus entry for an lpsinfo entry unless
* it is a zombie thread, in which case we will not have a matching lstatus entry.
*/
{
{
if ( cStatus == 0
{
CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: cStatus = %u pStatuslwpid=%d infolwpid=%d\n", cStatus,
break;
}
cStatus--;
}
}
if (RT_SUCCESS(rc))
{
/*
* There can still be more lwpsinfo_t's than lwpstatus_t's, build the
* lists accordingly.
*/
if (pSolProc->pThreadInfoHead)
{
{
{
/*
* Adjust the context of the dumping thread to reflect the context
* when the core dump got initiated before whatever signal caused it.
*/
if ( pStatus /* noid droid */
{
memcpy(&pStatus->pr_fpreg, &pSolProc->pCurThreadCtx->uc_mcontext.fpregs, sizeof(pStatus->pr_fpreg));
CORELOG((CORELOG_NAME "ProcReadThreads: patched dumper thread context with pre-dump time context.\n"));
}
}
else
{
}
if (pPrev)
}
if (pPrev)
return VINF_SUCCESS;
}
else
{
CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: GetMemoryChunk failed for %u bytes\n", cbThreadInfo));
rc = VERR_NO_MEMORY;
}
}
else
CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: Invalid state information for threads. rc=%Rrc\n", rc));
}
else
{
CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: huh!? cbStatusHdrAndData=%u prheader_t=%u entsize=%u\n", cbStatusHdrAndData,
CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: huh!? cbInfoHdrAndData=%u entsize=%u\n", cbInfoHdrAndData,
pStatusHdr->pr_entsize));
}
}
else
CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: ReadFileNoIntr failed for \"lpsinfo\" rc=%Rrc\n", rc));
}
else
CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: ReadFileNoIntr failed for \"lstatus\" rc=%Rrc\n", rc));
return rc;
}
/**
* Reads miscellaneous information that is collected as part of a core file.
* This may include platform name, zone name and other OS-specific information.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code.
*/
{
#ifdef RT_OS_SOLARIS
/*
* Read the platform name, uname string and zone name.
*/
if (rc == -1)
{
return VERR_GENERAL_FAILURE;
}
if (rc == -1)
{
return VERR_GENERAL_FAILURE;
}
rc = getzonenamebyid(pSolProc->ProcInfo.pr_zoneid, pSolProc->szZoneName, sizeof(pSolProc->szZoneName));
if (rc < 0)
{
CORELOGRELSYS((CORELOG_NAME "ProcReadMiscInfo: getzonenamebyid failed. rc=%d errno=%d zoneid=%d\n", rc, errno,
return VERR_GENERAL_FAILURE;
}
rc = VINF_SUCCESS;
#else
#endif
return rc;
}
/**
* On Solaris use the old-style procfs interfaces but the core file still should have this
* info. for backward and GDB compatibility, hence the need for this ugly function.
*
* @param pSolCore Pointer to the core object.
* @param pInfo Pointer to the old prpsinfo_t structure to update.
*/
{
pInfo->pr_aslwpid = 0;
}
/**
* On Solaris use the old-style procfs interfaces but the core file still should have this
* info. for backward and GDB compatibility, hence the need for this ugly function.
*
* @param pSolCore Pointer to the core object.
* @param pInfo Pointer to the thread info.
* @param pStatus Pointer to the thread status.
* @param pDst Pointer to the old-style status structure to update.
*
*/
static void GetOldProcessStatus(PRTSOLCORE pSolCore, lwpsinfo_t *pInfo, lwpstatus_t *pStatus, prstatus_t *pDst)
{
/* PR_PTRACE is never set */
}
/**
* Callback for rtCoreDumperForEachThread to suspend a thread.
*
* @param pSolCore Pointer to the core object.
* @param pvThreadInfo Opaque pointer to thread information.
*
* @return IPRT status code.
*/
{
return VINF_SUCCESS;
}
/**
* Callback for rtCoreDumperForEachThread to resume a thread.
*
* @param pSolCore Pointer to the core object.
* @param pvThreadInfo Opaque pointer to thread information.
*
* @return IPRT status code.
*/
{
return VINF_SUCCESS;
}
/**
* Calls a thread worker function for all threads in the process as described by /proc
*
* @param pSolCore Pointer to the core object.
* @param pcThreads Number of threads read.
* @param pfnWorker Callback function for each thread.
*
* @return IPRT status code.
*/
static int rtCoreDumperForEachThread(PRTSOLCORE pSolCore, uint64_t *pcThreads, PFNRTSOLCORETHREADWORKER pfnWorker)
{
/*
* Read the information for threads.
* Format: prheader_t + array of lwpsinfo_t's.
*/
char szLpsInfoPath[PATH_MAX];
int rc = VINF_SUCCESS;
if (fd >= 0)
{
-1 /* fd */, 0 /* offset */);
if (pvInfoHdr != MAP_FAILED)
{
if (RT_SUCCESS(rc))
{
{
}
if (pcThreads)
}
}
else
rc = VERR_NO_MEMORY;
}
else
return rc;
}
/**
* Resume all threads of this process.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code..
*/
{
#if 1
#else
char szCurThread[128];
int32_t cRunningThreads = 0;
if (RT_SUCCESS(rc))
{
/*
* Loop through all our threads & resume them.
*/
{
continue;
continue;
}
}
else
{
}
return rc;
#endif
}
/**
* Stop all running threads of this process except the current one.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code.
*/
{
/*
* This function tries to ensures while we suspend threads, no newly spawned threads
* or a combination of spawning and terminating threads can cause any threads to be left running.
* The assumption here is that threads can only increase not decrease across iterations.
*/
#if 1
int rc = VERR_GENERAL_FAILURE;
{
if (RT_FAILURE(rc))
break;
}
if ( RT_SUCCESS(rc)
{
rc = VERR_TIMEOUT;
}
return rc;
#else
char szCurThread[128];
int rc = -1;
{
uint32_t cRunningThreads = 0;
if (RT_SUCCESS(rc))
{
/*
* Loop through all our threads & suspend them, multiple calls to _lwp_suspend() are okay.
*/
{
continue;
continue;
}
{
rc = VINF_SUCCESS;
break;
}
}
else
{
break;
}
}
if (RT_SUCCESS(rc))
CORELOG((CORELOG_NAME "SuspendThreads: Stopped %u threads successfully with %u tries\n", cThreads, cTries));
return rc;
#endif
}
/**
* Returns size of an ELF NOTE header given the size of data the NOTE section will contain.
*
* @param cb Size of the data.
*
* @return Size of data actually used for NOTE header and section.
*/
{
}
/**
* Write an ELF NOTE header into the core file.
*
* @param pSolCore Pointer to the core object.
* @param Type Type of this NOTE section.
* @param pcv Opaque pointer to the data, if NULL only computes size.
* @param cb Size of the data.
*
* @return IPRT status code.
*/
{
int rc = VERR_GENERAL_FAILURE;
#ifdef RT_OS_SOLARIS
/*
* This is a known violation of the 64-bit ELF spec., see xTracker #5211 comment#3
* for the historic reasons as to the padding and namesz anomalies.
*/
static const char s_achPad[3] = { 0, 0, 0 };
/*
* Write note header and description.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
}
if (RT_FAILURE(rc))
#else
#endif
return rc;
}
/**
* Computes the size of NOTE section for the given core type.
* Solaris has two types of program header information (new and old).
*
* @param pSolCore Pointer to the core object.
* @param enmType Type of core file information required.
*
* @return Size of NOTE section.
*/
{
switch (enmType)
{
case enmOldEra:
{
while (pThreadInfo)
{
if (pThreadInfo->pStatus)
{
}
}
break;
}
case enmNewEra:
{
if (pSolProc->pcPrivImpl)
while (pThreadInfo)
{
if (pThreadInfo->pStatus)
}
break;
}
default:
{
break;
}
}
return cb;
}
/**
* Write the note section for the given era into the core file.
* Solaris has two types of program header information (new and old).
*
* @param pSolCore Pointer to the core object.
* @param enmType Type of core file information required.
*
* @return IPRT status code.
*/
{
int rc = VERR_GENERAL_FAILURE;
#ifdef RT_OS_SOLARIS
typedef struct ELFWRITENOTE
{
const char *pszType;
const void *pcv;
} ELFWRITENOTE;
switch (enmType)
{
case enmOldEra:
{
{
};
for (unsigned i = 0; i < RT_ELEMENTS(aElfNotes); i++)
{
if (RT_FAILURE(rc))
{
CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader failed for %s. rc=%Rrc\n", aElfNotes[i].pszType, rc));
break;
}
}
/*
* Write old-style thread info., they contain nothing about zombies,
* so we just skip if there is no status information for them.
*/
{
if (!pThreadInfo->pStatus)
continue;
if (RT_SUCCESS(rc))
{
rc = ElfWriteNoteHeader(pSolCore, NT_PRFPREG, &pThreadInfo->pStatus->pr_fpreg, sizeof(prfpregset_t));
if (RT_FAILURE(rc))
{
break;
}
}
else
{
CORELOGRELSYS((CORELOG_NAME "ElfWriteSegment: ElfWriteNote failed for NT_PRSTATUS. rc=%Rrc\n", rc));
break;
}
}
break;
}
case enmNewEra:
{
{
{ "NT_PRPRIVINFO", NT_PRPRIVINFO, pSolProc->pcPrivImpl, PRIV_IMPL_INFO_SIZE(pSolProc->pcPrivImpl) },
};
for (unsigned i = 0; i < RT_ELEMENTS(aElfNotes); i++)
{
if (RT_FAILURE(rc))
{
CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader failed for %s. rc=%Rrc\n", aElfNotes[i].pszType, rc));
break;
}
}
/*
* Write new-style thread info., missing lwpstatus_t indicates it's a zombie thread
* we only dump the lwpsinfo_t in that case.
*/
{
if (RT_FAILURE(rc))
{
CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader for NT_LWPSINFO failed. rc=%Rrc\n", rc));
break;
}
if (pThreadInfo->pStatus)
{
if (RT_FAILURE(rc))
{
CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader for NT_LWPSTATUS failed. rc=%Rrc\n", rc));
break;
}
}
}
break;
}
default:
{
break;
}
}
#else
#endif
return rc;
}
/**
* Write mappings into the core file.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code.
*/
{
int rc = VERR_GENERAL_FAILURE;
while (pMapInfo)
{
{
uint64_t k = 0;
{
if (RT_FAILURE(rc2))
{
CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: Failed to read mapping, can't recover. Bye. rc=%Rrc\n", rc));
return VERR_INVALID_STATE;
}
if (RT_FAILURE(rc))
{
return rc;
}
k += cb;
}
}
else
{
if (RT_FAILURE(rc))
{
return rc;
}
}
}
return VINF_SUCCESS;
}
/**
* Write program headers for all mappings into the core file.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code.
*/
{
int rc = VERR_GENERAL_FAILURE;
while (pMapInfo)
{
ProgHdr.p_vaddr = pMapInfo->pMap.pr_vaddr; /* Virtual address of this mapping in the process address space */
if (RT_FAILURE(rc))
{
return rc;
}
}
return rc;
}
/**
* Inner worker for rtCoreDumperWriteCore, which purpose is to
* squash cleanup gotos.
*/
{
/*
* Write the ELF header.
*/
#ifdef RT_ARCH_AMD64
#else
#endif
else
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Setup program header.
*/
/*
* Write old-style NOTE program header.
*/
if (RT_FAILURE(rc))
{
CORELOGRELSYS((CORELOG_NAME "WriteCore: pfnWriter failed writing old-style ELF program Header. rc=%Rrc\n", rc));
return rc;
}
/*
* Write new-style NOTE program header.
*/
if (RT_FAILURE(rc))
{
CORELOGRELSYS((CORELOG_NAME "WriteCore: pfnWriter failed writing new-style ELF program header. rc=%Rrc\n", rc));
return rc;
}
/*
* Write program headers per mapping.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Write old-style note section.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Write new-style section.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Write all mappings.
*/
if (RT_FAILURE(rc))
{
return rc;
}
return rc;
}
/**
* Write a prepared core file using a user-passed in writer function, requires all threads
* to be in suspended state (i.e. called after CreateCore).
*
* @param pSolCore Pointer to the core object.
* @param pfnWriter Pointer to the writer function to override default writer (NULL uses default).
*
* @remarks Resumes all suspended threads, unless it's an invalid core. This
* function must be called only -after- rtCoreDumperCreateCore().
* @return IPRT status.
*/
{
return VERR_INVALID_STATE;
if (pfnWriter)
int rc;
/*
* Open the process address space file.
*/
if (fd >= 0)
{
/*
* Create the core file.
*/
if (fd >= 0)
{
/*
* Do the actual writing.
*/
}
else
{
}
}
else
{
}
return rc;
}
/**
* Takes a process snapshot into a passed-in core object. It has the side-effect of halting
* all threads which can lead to things like spurious wakeups of threads (if and when threads
* are ultimately resumed en-masse) already suspended while calling this function.
*
* @param pSolCore Pointer to a core object.
* @param pContext Pointer to the caller context thread.
* @param pszCoreFilePath Path to the core file. If NULL is passed, the global
* path specified in RTCoreDumperSetup() would be used.
*
* @remarks Halts all threads.
* @return IPRT status code.
*/
static int rtCoreDumperCreateCore(PRTSOLCORE pSolCore, ucontext_t *pContext, const char *pszCoreFilePath)
{
/*
* Initialize core structures.
*/
RTProcGetExecutablePath(pSolProc->szExecPath, sizeof(pSolProc->szExecPath)); /* this gets full path not just name */
/*
* If a path has been specified, use it. Otherwise use the global path.
*/
if (!pszCoreFilePath)
{
/*
* If no output directory is specified, use current directory.
*/
if (g_szCoreDumpDir[0] == '\0')
g_szCoreDumpDir[0] = '.';
if (g_szCoreDumpFile[0] == '\0')
{
/* We cannot call RTPathAbs*() as they call getcwd() which calls malloc. */
}
else
RTStrPrintf(pSolCore->szCorePath, sizeof(pSolCore->szCorePath), "%s/core.vb.%s", g_szCoreDumpDir, g_szCoreDumpFile);
}
else
CORELOG((CORELOG_NAME "CreateCore: Taking Core %s from Thread %d\n", pSolCore->szCorePath, (int)pSolProc->hCurThread));
/*
* Quiesce the process.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (IsProcessArchNative(pSolProc))
{
/*
* Read process status, information such as number of active LWPs will be invalid since we just quiesced the process.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
struct COREACCUMULATOR
{
const char *pszName;
bool fOptional;
} aAccumulators[] =
{
{ "ProcReadLdt", &ProcReadLdt, false },
{ "ProcReadCred", &ProcReadCred, false },
{ "ProcReadPriv", &ProcReadPriv, false },
{ "ProcReadAuxVecs", &ProcReadAuxVecs, false },
{ "ProcReadMappings", &ProcReadMappings, false },
{ "ProcReadThreads", &ProcReadThreads, false },
{ "ProcReadMiscInfo", &ProcReadMiscInfo, false }
};
for (unsigned i = 0; i < RT_ELEMENTS(aAccumulators); i++)
{
if (RT_FAILURE(rc))
{
if (!aAccumulators[i].fOptional)
break;
}
}
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
else
}
else
}
else
{
}
}
else
/*
* Resume threads on failure.
*/
}
else
return rc;
}
/**
* Destroy an existing core object.
*
* @param pSolCore Pointer to the core object.
*
* @return IPRT status code.
*/
{
return VERR_INVALID_STATE;
return VINF_SUCCESS;
}
/**
* Takes a core dump. This function has no other parameters than the context
* because it can be called from signal handlers.
*
* @param pContext The context of the caller.
* @param pszOutputFile Path of the core file. If NULL is passed, the
* global path passed in RTCoreDumperSetup will
* be used.
* @returns IPRT status code.
*/
{
if (!pContext)
{
return VERR_INVALID_POINTER;
}
/*
* Take a snapshot, then dump core to disk, all threads except this one are halted
* from before taking the snapshot until writing the core is completely finished.
* Any errors would resume all threads if they were halted.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
else
CORELOGRELSYS((CORELOG_NAME "TakeDump: WriteCore failed. szCorePath=%s rc=%Rrc\n", SolCore.szCorePath, rc));
}
else
return rc;
}
/**
* The signal handler that will be invoked to take core dumps.
*
* @param Sig The signal that invoked us.
* @param pSigInfo The signal information.
* @param pvArg Opaque pointer to the caller context structure,
* this cannot be NULL.
*/
{
int rc = VERR_GENERAL_FAILURE;
bool fCallSystemDump = false;
bool fRc;
if (fRc)
{
if (RT_FAILURE(rc))
}
{
/*
* Core dumping is already in progress and we've somehow ended up being
* signalled again.
*/
/*
* If our dumper has crashed. No point in waiting, trigger the system one.
* Wait only when the dumping thread is not the one generating this signal.
*/
if (hNativeDumperThread == RTThreadNativeSelf())
{
CORELOGRELSYS((CORELOG_NAME "SignalHandler: Core dumper (thread %u) crashed Sig=%d. Triggering system dump\n",
RTThreadSelf(), Sig));
fCallSystemDump = true;
}
else
{
/*
* Some other thread in the process is triggering a crash, wait a while
* to let our core dumper finish, on timeout trigger system dump.
*/
CORELOGRELSYS((CORELOG_NAME "SignalHandler: Core dump already in progress! Waiting a while for completion Sig=%d.\n", Sig));
for (;;)
{
break;
RTThreadSleep(200);
iTimeout -= 200;
if (iTimeout <= 0)
break;
}
if (iTimeout <= 0)
{
fCallSystemDump = true;
CORELOGRELSYS((CORELOG_NAME "SignalHandler: Core dumper seems to be stuck. Signalling new signal %d\n", Sig));
}
}
}
{
/*
* Reset signal handlers, we're not a live core we will be blown away
* one way or another.
*/
/*
* Hard terminate the process if this is not a live dump without invoking
* the system core dumping behaviour.
*/
if (RT_SUCCESS(rc))
/*
* Something went wrong, fall back to the system core dumper.
*/
if (fCallSystemDump)
abort();
}
}
{
if (!rc)
{
/*
* Block SIGSEGV and co. while we write the core.
*/
if (RT_FAILURE(rc))
if (!fLiveCore)
{
if (RT_SUCCESS(rc))
else
abort();
}
}
else
{
}
return rc;
}
{
/*
* Validate flags.
*/
/*
*/
if (pszOutputDir)
{
if (!RTDirExists(pszOutputDir))
return VERR_NOT_A_DIRECTORY;
}
/*
* Install core dump signal handler only if the flags changed or if it's the first time.
*/
if ( ASMAtomicReadBool(&g_fCoreDumpSignalSetup) == false
{
{
}
{
}
ASMAtomicWriteBool(&g_fCoreDumpSignalSetup, true);
}
return VINF_SUCCESS;
}
RTDECL(int) RTCoreDumperDisable(void)
{
/*
* Remove core dump signal handler & reset variables.
*/
if (ASMAtomicReadBool(&g_fCoreDumpSignalSetup) == true)
{
ASMAtomicWriteBool(&g_fCoreDumpSignalSetup, false);
}
return VINF_SUCCESS;
}