1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include "posixcrashhandler.h"
#ifndef _WIN32
#include <execinfo.h>
#include <errno.h>
#include <dlfcn.h>
#include <cxxabi.h>
#include <cstdio>
#include <cstdlib>
#include <signal.h>
#include <cstring>
#include <cstdint>
#include <sstream>
#include <memory>
#include "../defines.h"
#include "../common/common.h"
#define KILOBYTE 1024
#define DEMANGLE_MEMORY_SIZE (10*(KILOBYTE))
#define STACK_MEMORY_SIZE (90*(KILOBYTE))
namespace Debug {
struct FreeDeleter {
void operator()(void* ptr) const {
free(ptr);
}
};
char *fake_alloc(char **memory, size_t size) {
char *allocated = *memory;
char *last = allocated + size;
*last = '\0';
*memory += size + 1;
return allocated;
}
void chilltrace(const char * const stackEntry) {
if (stackEntry) {
fputs(stackEntry,stderr);
}
}
void posixSignalHandler( int signum, siginfo_t* si, void* ucontext ) {
(void)si;
(void)ucontext;
auto &handler = PosixCrashHandler::getInstance();
handler.handleCrash();
// If you caught one of the above signals, it is likely you just
// want to quit your program right now.
//exit( signum );
std::_Exit(CHILLOUT_EXIT_CODE);
}
PosixCrashHandler::PosixCrashHandler():
m_stackMemory(nullptr),
m_demangleMemory(nullptr)
{
m_stackMemory = (char*)malloc(STACK_MEMORY_SIZE);
memset(&m_stackMemory[0], 0, STACK_MEMORY_SIZE);
m_demangleMemory = (char*)malloc(DEMANGLE_MEMORY_SIZE);
memset(&m_demangleMemory[0], 0, DEMANGLE_MEMORY_SIZE);
m_backtraceCallback = chilltrace;
}
PosixCrashHandler::~PosixCrashHandler() {
free(m_stackMemory);
free(m_demangleMemory);
}
void PosixCrashHandler::setup(const std::string &appName, const std::string &crashDirPath) {
struct sigaction sa;
sa.sa_sigaction = posixSignalHandler;
sigemptyset( &sa.sa_mask );
#ifdef __APPLE__
/* for some reason we backtrace() doesn't work on osx
when we use an alternate stack */
sa.sa_flags = SA_SIGINFO;
#else
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
#endif
sigaction( SIGABRT, &sa, NULL );
sigaction( SIGSEGV, &sa, NULL );
sigaction( SIGBUS, &sa, NULL );
sigaction( SIGILL, &sa, NULL );
sigaction( SIGFPE, &sa, NULL );
sigaction( SIGPIPE, &sa, NULL );
sigaction( SIGTERM, &sa, NULL );
if (!crashDirPath.empty()) {
std::string path = crashDirPath;
while ((path.size() > 1) &&
(path[path.size() - 1] == '/')) {
path.erase(path.size() - 1);
}
std::stringstream s;
s << path << "/" << appName << "_";
formatDateTime(s, now(), CHILLOUT_DATETIME);
s << ".bktr";
m_backtraceFilePath = s.str();
}
}
void PosixCrashHandler::teardown() {
struct sigaction sa;
sigset_t mysigset;
sigemptyset(&mysigset);
sa.sa_handler = SIG_DFL;
sa.sa_mask = mysigset;
sa.sa_flags = 0;
sigaction( SIGABRT, &sa, NULL );
sigaction( SIGSEGV, &sa, NULL );
sigaction( SIGBUS, &sa, NULL );
sigaction( SIGILL, &sa, NULL );
sigaction( SIGFPE, &sa, NULL );
sigaction( SIGPIPE, &sa, NULL );
sigaction( SIGTERM, &sa, NULL );
}
void PosixCrashHandler::handleCrash() {
if (m_crashCallback) {
m_crashCallback();
}
}
void PosixCrashHandler::setCrashCallback(const std::function<void()> &callback) {
m_crashCallback = callback;
}
void PosixCrashHandler::setBacktraceCallback(const std::function<void(const char * const)> &callback) {
m_backtraceCallback = callback;
}
}
#endif
|