Merge branch 'crash_catcher' into 'master'

Cleanup crash catcher code and support lldb

See merge request OpenMW/openmw!3792
ini_importer_tests
psi29a 11 months ago
commit 9fc71bb7df

@ -1,23 +1,27 @@
#include <algorithm>
#include <cstring> #include <cstring>
#include <errno.h>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <optional>
#include <span>
#include <errno.h>
#include <limits.h> #include <limits.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/ptrace.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/ptrace.h>
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/files/conversion.hpp>
#include <SDL_messagebox.h> #include <SDL_messagebox.h>
@ -59,149 +63,214 @@ static struct
{ {
int signum; int signum;
pid_t pid; pid_t pid;
int has_siginfo; std::optional<siginfo_t> siginfo;
siginfo_t siginfo;
char buf[1024];
} crash_info; } crash_info;
static const struct namespace
{ {
const char* name; constexpr char crash_switch[] = "--cc-handle-crash";
int signum;
} signals[] = { { "Segmentation fault", SIGSEGV }, { "Illegal instruction", SIGILL }, { "FPU exception", SIGFPE },
{ "System BUS error", SIGBUS }, { nullptr, 0 } };
static const struct struct SignalInfo
{ {
int code; int mCode;
const char* name; const char* mDescription;
} sigill_codes[] = { const char* mName = "";
};
constexpr SignalInfo signals[] = {
{ SIGSEGV, "Segmentation fault", "SIGSEGV" },
{ SIGILL, "Illegal instruction", "SIGILL" },
{ SIGFPE, "FPU exception", "SIGFPE" },
{ SIGBUS, "System BUS error", "SIGBUS" },
{ SIGABRT, "Abnormal termination condition", "SIGABRT" },
};
constexpr SignalInfo sigIllCodes[] = {
#if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
{ ILL_ILLOPC, "Illegal opcode" }, { ILL_ILLOPN, "Illegal operand" }, { ILL_ILLADR, "Illegal addressing mode" }, { ILL_ILLOPC, "Illegal opcode" },
{ ILL_ILLTRP, "Illegal trap" }, { ILL_PRVOPC, "Privileged opcode" }, { ILL_PRVREG, "Privileged register" }, { ILL_ILLOPN, "Illegal operand" },
{ ILL_COPROC, "Coprocessor error" }, { ILL_BADSTK, "Internal stack error" }, { ILL_ILLADR, "Illegal addressing mode" },
{ ILL_ILLTRP, "Illegal trap" },
{ ILL_PRVOPC, "Privileged opcode" },
{ ILL_PRVREG, "Privileged register" },
{ ILL_COPROC, "Coprocessor error" },
{ ILL_BADSTK, "Internal stack error" },
#endif #endif
{ 0, nullptr } };
};
constexpr SignalInfo sigFpeCodes[] = {
static const struct { FPE_INTDIV, "Integer divide by zero" },
{ { FPE_INTOVF, "Integer overflow" },
int code; { FPE_FLTDIV, "Floating point divide by zero" },
const char* name; { FPE_FLTOVF, "Floating point overflow" },
} sigfpe_codes[] = { { FPE_INTDIV, "Integer divide by zero" }, { FPE_INTOVF, "Integer overflow" }, { FPE_FLTUND, "Floating point underflow" },
{ FPE_FLTDIV, "Floating point divide by zero" }, { FPE_FLTOVF, "Floating point overflow" }, { FPE_FLTRES, "Floating point inexact result" },
{ FPE_FLTUND, "Floating point underflow" }, { FPE_FLTRES, "Floating point inexact result" }, { FPE_FLTINV, "Floating point invalid operation" },
{ FPE_FLTINV, "Floating point invalid operation" }, { FPE_FLTSUB, "Subscript out of range" }, { 0, nullptr } }; { FPE_FLTSUB, "Subscript out of range" },
};
static const struct
{ constexpr SignalInfo sigSegvCodes[] = {
int code;
const char* name;
} sigsegv_codes[] = {
#ifndef __FreeBSD__ #ifndef __FreeBSD__
{ SEGV_MAPERR, "Address not mapped to object" }, { SEGV_ACCERR, "Invalid permissions for mapped object" }, { SEGV_MAPERR, "Address not mapped to object" },
{ SEGV_ACCERR, "Invalid permissions for mapped object" },
#endif #endif
{ 0, nullptr } };
};
static const struct constexpr SignalInfo sigBusCodes[] = {
{
int code;
const char* name;
} sigbus_codes[] = {
#ifndef __FreeBSD__ #ifndef __FreeBSD__
{ BUS_ADRALN, "Invalid address alignment" }, { BUS_ADRERR, "Non-existent physical address" }, { BUS_ADRALN, "Invalid address alignment" },
{ BUS_OBJERR, "Object specific hardware error" }, { BUS_ADRERR, "Non-existent physical address" },
{ BUS_OBJERR, "Object specific hardware error" },
#endif #endif
{ 0, nullptr } };
};
static int (*cc_user_info)(char*, char*); const char* findSignalDescription(std::span<const SignalInfo> info, int code)
{
const auto it = std::find_if(info.begin(), info.end(), [&](const SignalInfo& v) { return v.mCode == code; });
return it == info.end() ? "" : it->mDescription;
}
struct Close
{
void operator()(const int* value) { close(*value); }
};
struct CloseFile
{
void operator()(FILE* value) { fclose(value); }
};
struct Remove
{
void operator()(const char* value) { remove(value); }
};
template <class T>
bool printDebuggerInfo(pid_t pid)
{
// Create a temp file to put gdb commands into.
// Note: POSIX.1-2008 declares that the file should be already created with mode 0600 by default.
// Modern systems implement it and suggest to do not touch masks in multithreaded applications.
// So CoverityScan warning is valid only for ancient versions of stdlib.
char scriptPath[64];
std::snprintf(scriptPath, sizeof(scriptPath), "/tmp/%s-script-XXXXXX", T::sName);
static void gdb_info(pid_t pid)
{
char respfile[64];
FILE* f;
int fd;
/*
* Create a temp file to put gdb commands into.
* Note: POSIX.1-2008 declares that the file should be already created with mode 0600 by default.
* Modern systems implement it and suggest to do not touch masks in multithreaded applications.
* So CoverityScan warning is valid only for ancient versions of stdlib.
*/
strcpy(respfile, "/tmp/gdb-respfile-XXXXXX");
#ifdef __COVERITY__ #ifdef __COVERITY__
umask(0600); umask(0600);
#endif #endif
if ((fd = mkstemp(respfile)) >= 0 && (f = fdopen(fd, "w")) != nullptr)
{ const int fd = mkstemp(scriptPath);
fprintf(f, if (fd == -1)
"attach %d\n" {
"shell echo \"\"\n" printf("Failed to call mkstemp: %s\n", std::generic_category().message(errno).c_str());
"shell echo \"* Loaded Libraries\"\n" return false;
"info sharedlibrary\n" }
"shell echo \"\"\n" std::unique_ptr<const char, Remove> tempFile(scriptPath);
"shell echo \"* Threads\"\n" std::unique_ptr<const int, Close> scopedFd(&fd);
"info threads\n"
"shell echo \"\"\n" FILE* const file = fdopen(fd, "w");
"shell echo \"* FPU Status\"\n" if (file == nullptr)
"info float\n" {
"shell echo \"\"\n" printf("Failed to open file for %s output \"%s\": %s\n", T::sName, scriptPath,
"shell echo \"* Registers\"\n" std::generic_category().message(errno).c_str());
"info registers\n" return false;
"shell echo \"\"\n" }
"shell echo \"* Backtrace\"\n" std::unique_ptr<FILE, CloseFile> scopedFile(file);
"thread apply all backtrace full 1000\n"
"detach\n" if (fprintf(file, "%s", T::sScript) < 0)
"quit\n", {
pid); printf("Failed to write debugger script to file \"%s\": %s\n", scriptPath,
fclose(f); std::generic_category().message(errno).c_str());
return false;
/* Run gdb and print process info. */ }
char cmd_buf[128];
snprintf(cmd_buf, sizeof(cmd_buf), "gdb --quiet --batch --command=%s", respfile); scopedFile = nullptr;
printf("Executing: %s\n", cmd_buf); scopedFd = nullptr;
char command[128];
snprintf(command, sizeof(command), T::sCommandTemplate, pid, scriptPath);
printf("Executing: %s\n", command);
fflush(stdout); fflush(stdout);
int ret = system(cmd_buf); const int ret = system(command);
if (ret != 0) const bool result = (ret == 0);
if (ret == -1)
printf(
"\nFailed to create a crash report: %s.\n"
"Please make sure that '%s' is installed and present in PATH then crash again.\n"
"Current PATH: %s\n",
T::sName, std::generic_category().message(errno).c_str(), getenv("PATH"));
else if (ret != 0)
printf( printf(
"\nFailed to create a crash report. Please make sure that 'gdb' is installed and present in PATH then " "\nFailed to create a crash report.\n"
"crash again." "Please make sure that '%s' is installed and present in PATH then crash again.\n"
"\nCurrent PATH: %s\n", "Current PATH: %s\n",
getenv("PATH")); T::sName, getenv("PATH"));
fflush(stdout); fflush(stdout);
/* Clean up */ return result;
if (remove(respfile) != 0)
Log(Debug::Warning) << "Warning: can not remove file '" << respfile
<< "': " << std::generic_category().message(errno);
} }
else
struct Gdb
{ {
/* Error creating temp file */ static constexpr char sName[] = "gdb";
if (fd >= 0) static constexpr char sScript[] = R"(shell echo ""
{ shell echo "* Loaded Libraries"
if (close(fd) != 0) info sharedlibrary
Log(Debug::Warning) << "Warning: can not close file '" << respfile shell echo ""
<< "': " << std::generic_category().message(errno); shell echo "* Threads"
else if (remove(respfile) != 0) info threads
Log(Debug::Warning) << "Warning: can not remove file '" << respfile shell echo ""
<< "': " << std::generic_category().message(errno); shell echo "* FPU Status"
} info float
printf("!!! Could not create gdb command file\n"); shell echo ""
shell echo "* Registers"
info registers
shell echo ""
shell echo "* Backtrace"
thread apply all backtrace full 1000
detach
quit
)";
static constexpr char sCommandTemplate[] = "gdb --pid %d --quiet --batch --command %s";
};
struct Lldb
{
static constexpr char sName[] = "lldb";
static constexpr char sScript[] = R"(script print("\n* Loaded Libraries")
image list
script print('\n* Threads')
thread list
script print('\n* Registers')
register read --all
script print('\n* Backtrace')
script print(''.join(f'{t}\n' + ''.join(''.join([f' {f}\n', ''.join(f' {s}\n' for s in f.statics), ''.join(f' {v}\n' for v in f.variables)]) for f in t.frames) for t in lldb.process.threads))
detach
quit
)";
static constexpr char sCommandTemplate[] = "lldb --attach-pid %d --batch --source %s";
};
void printProcessInfo(pid_t pid)
{
if (printDebuggerInfo<Gdb>(pid))
return;
if (printDebuggerInfo<Lldb>(pid))
return;
} }
fflush(stdout);
} }
static void sys_info(void) static void printSystemInfo(void)
{ {
#ifdef __unix__ #ifdef __unix__
struct utsname info; struct utsname info;
if (uname(&info)) if (uname(&info) == -1)
printf("!!! Failed to get system information\n"); printf("Failed to get system information: %s\n", std::generic_category().message(errno).c_str());
else else
printf("System: %s %s %s %s %s\n", info.sysname, info.nodename, info.release, info.version, info.machine); printf("System: %s %s %s %s %s\n", info.sysname, info.nodename, info.release, info.version, info.machine);
@ -214,8 +283,8 @@ static size_t safe_write(int fd, const void* buf, size_t len)
size_t ret = 0; size_t ret = 0;
while (ret < len) while (ret < len)
{ {
ssize_t rem; const ssize_t rem = write(fd, (const char*)buf + ret, len - ret);
if ((rem = write(fd, (const char*)buf + ret, len - ret)) == -1) if (rem == -1)
{ {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
@ -226,12 +295,8 @@ static size_t safe_write(int fd, const void* buf, size_t len)
return ret; return ret;
} }
static void crash_catcher(int signum, siginfo_t* siginfo, void* context) static void crash_catcher(int signum, siginfo_t* siginfo, void* /*context*/)
{ {
// ucontext_t *ucontext = (ucontext_t*)context;
pid_t dbg_pid;
int fd[2];
/* Make sure the effective uid is the real uid */ /* Make sure the effective uid is the real uid */
if (getuid() != geteuid()) if (getuid() != geteuid())
{ {
@ -240,6 +305,7 @@ static void crash_catcher(int signum, siginfo_t* siginfo, void* context)
} }
safe_write(STDERR_FILENO, fatal_err, sizeof(fatal_err) - 1); safe_write(STDERR_FILENO, fatal_err, sizeof(fatal_err) - 1);
int fd[2];
if (pipe(fd) == -1) if (pipe(fd) == -1)
{ {
safe_write(STDERR_FILENO, pipe_err, sizeof(pipe_err) - 1); safe_write(STDERR_FILENO, pipe_err, sizeof(pipe_err) - 1);
@ -249,14 +315,14 @@ static void crash_catcher(int signum, siginfo_t* siginfo, void* context)
crash_info.signum = signum; crash_info.signum = signum;
crash_info.pid = getpid(); crash_info.pid = getpid();
crash_info.has_siginfo = !!siginfo; if (siginfo == nullptr)
if (siginfo) crash_info.siginfo = std::nullopt;
else
crash_info.siginfo = *siginfo; crash_info.siginfo = *siginfo;
if (cc_user_info)
cc_user_info(crash_info.buf, crash_info.buf + sizeof(crash_info.buf));
const pid_t dbg_pid = fork();
/* Fork off to start a crash handler */ /* Fork off to start a crash handler */
switch ((dbg_pid = fork())) switch (dbg_pid)
{ {
/* Error */ /* Error */
case -1: case -1:
@ -296,110 +362,68 @@ static void crash_catcher(int signum, siginfo_t* siginfo, void* context)
} }
} }
static void crash_handler(const char* logfile) [[noreturn]] static void handleCrash(const char* logfile)
{ {
const char* sigdesc = "";
int i;
if (fread(&crash_info, sizeof(crash_info), 1, stdin) != 1) if (fread(&crash_info, sizeof(crash_info), 1, stdin) != 1)
{ {
fprintf(stderr, "!!! Failed to retrieve info from crashed process\n"); fprintf(stderr, "Failed to retrieve info from crashed process: %s\n",
std::generic_category().message(errno).c_str());
exit(1); exit(1);
} }
/* Get the signal description */ const char* sigdesc = findSignalDescription(signals, crash_info.signum);
for (i = 0; signals[i].name; ++i)
{
if (signals[i].signum == crash_info.signum)
{
sigdesc = signals[i].name;
break;
}
}
if (crash_info.has_siginfo) if (crash_info.siginfo.has_value())
{ {
switch (crash_info.signum) switch (crash_info.signum)
{ {
case SIGSEGV: case SIGSEGV:
for (i = 0; sigsegv_codes[i].name; ++i) sigdesc = findSignalDescription(sigSegvCodes, crash_info.siginfo->si_code);
{
if (sigsegv_codes[i].code == crash_info.siginfo.si_code)
{
sigdesc = sigsegv_codes[i].name;
break;
}
}
break; break;
case SIGFPE: case SIGFPE:
for (i = 0; sigfpe_codes[i].name; ++i) sigdesc = findSignalDescription(sigFpeCodes, crash_info.siginfo->si_code);
{
if (sigfpe_codes[i].code == crash_info.siginfo.si_code)
{
sigdesc = sigfpe_codes[i].name;
break;
}
}
break; break;
case SIGILL: case SIGILL:
for (i = 0; sigill_codes[i].name; ++i) sigdesc = findSignalDescription(sigIllCodes, crash_info.siginfo->si_code);
{
if (sigill_codes[i].code == crash_info.siginfo.si_code)
{
sigdesc = sigill_codes[i].name;
break;
}
}
break; break;
case SIGBUS: case SIGBUS:
for (i = 0; sigbus_codes[i].name; ++i) sigdesc = findSignalDescription(sigBusCodes, crash_info.siginfo->si_code);
{
if (sigbus_codes[i].code == crash_info.siginfo.si_code)
{
sigdesc = sigbus_codes[i].name;
break;
}
}
break; break;
} }
} }
fprintf(stderr, "%s (signal %i)\n", sigdesc, crash_info.signum); fprintf(stderr, "%s (signal %i)\n", sigdesc, crash_info.signum);
if (crash_info.has_siginfo) if (crash_info.siginfo.has_value())
fprintf(stderr, "Address: %p\n", crash_info.siginfo.si_addr); fprintf(stderr, "Address: %p\n", crash_info.siginfo->si_addr);
fputc('\n', stderr); fputc('\n', stderr);
if (logfile) /* Create crash log file and redirect shell output to it */
if (freopen(logfile, "wa", stdout) != stdout)
{ {
/* Create crash log file and redirect shell output to it */ fprintf(stderr, "Could not create %s following signal: %s\n", logfile,
if (freopen(logfile, "wa", stdout) != stdout) std::generic_category().message(errno).c_str());
{ exit(1);
fprintf(stderr, "!!! Could not create %s following signal\n", logfile);
exit(1);
}
fprintf(stderr, "Generating %s and killing process %d, please wait... ", logfile, crash_info.pid);
printf(
"*** Fatal Error ***\n"
"%s (signal %i)\n",
sigdesc, crash_info.signum);
if (crash_info.has_siginfo)
printf("Address: %p\n", crash_info.siginfo.si_addr);
fputc('\n', stdout);
fflush(stdout);
} }
fprintf(stderr, "Generating %s and killing process %d, please wait... ", logfile, crash_info.pid);
printf(
"*** Fatal Error ***\n"
"%s (signal %i)\n",
sigdesc, crash_info.signum);
if (crash_info.siginfo.has_value())
printf("Address: %p\n", crash_info.siginfo->si_addr);
fputc('\n', stdout);
fflush(stdout);
sys_info(); printSystemInfo();
crash_info.buf[sizeof(crash_info.buf) - 1] = '\0';
printf("%s\n", crash_info.buf);
fflush(stdout); fflush(stdout);
if (crash_info.pid > 0) if (crash_info.pid > 0)
{ {
gdb_info(crash_info.pid); printProcessInfo(crash_info.pid);
kill(crash_info.pid, SIGKILL); kill(crash_info.pid, SIGKILL);
} }
@ -408,12 +432,9 @@ static void crash_handler(const char* logfile)
// even faulty applications shouldn't be able to freeze the X server. // even faulty applications shouldn't be able to freeze the X server.
usleep(100000); usleep(100000);
if (logfile) const std::string message = "OpenMW has encountered a fatal error.\nCrash log saved to '" + std::string(logfile)
{ + "'.\n Please report this to https://gitlab.com/OpenMW/openmw/issues !";
std::string message = "OpenMW has encountered a fatal error.\nCrash log saved to '" + std::string(logfile) SDL_ShowSimpleMessageBox(0, "Fatal Error", message.c_str(), nullptr);
+ "'.\n Please report this to https://gitlab.com/OpenMW/openmw/issues !";
SDL_ShowSimpleMessageBox(0, "Fatal Error", message.c_str(), nullptr);
}
exit(0); exit(0);
} }
@ -426,13 +447,16 @@ static void getExecPath(char** argv)
if (sysctl(mib, 4, argv0, &size, nullptr, 0) == 0) if (sysctl(mib, 4, argv0, &size, nullptr, 0) == 0)
return; return;
Log(Debug::Warning) << "Failed to call sysctl: " << std::generic_category().message(errno);
#endif #endif
#if defined(__APPLE__) #if defined(__APPLE__)
if (proc_pidpath(getpid(), argv0, sizeof(argv0)) > 0) if (proc_pidpath(getpid(), argv0, sizeof(argv0)) > 0)
return; return;
Log(Debug::Warning) << "Failed to call proc_pidpath: " << std::generic_category().message(errno);
#endif #endif
int cwdlen;
const char* statusPaths[] = { "/proc/self/exe", "/proc/self/file", "/proc/curproc/exe", "/proc/curproc/file" }; const char* statusPaths[] = { "/proc/self/exe", "/proc/self/file", "/proc/curproc/exe", "/proc/curproc/file" };
memset(argv0, 0, sizeof(argv0)); memset(argv0, 0, sizeof(argv0));
@ -440,60 +464,83 @@ static void getExecPath(char** argv)
{ {
if (readlink(path, argv0, sizeof(argv0)) != -1) if (readlink(path, argv0, sizeof(argv0)) != -1)
return; return;
Log(Debug::Warning) << "Failed to call readlink for \"" << path
<< "\": " << std::generic_category().message(errno);
} }
if (argv[0][0] == '/') if (argv[0][0] == '/')
{
snprintf(argv0, sizeof(argv0), "%s", argv[0]); snprintf(argv0, sizeof(argv0), "%s", argv[0]);
else if (getcwd(argv0, sizeof(argv0)) != nullptr) return;
}
if (getcwd(argv0, sizeof(argv0)) == nullptr)
{ {
cwdlen = strlen(argv0); Log(Debug::Error) << "Failed to call getcwd: " << std::generic_category().message(errno);
snprintf(argv0 + cwdlen, sizeof(argv0) - cwdlen, "/%s", argv[0]); return;
} }
const int cwdlen = strlen(argv0);
snprintf(argv0 + cwdlen, sizeof(argv0) - cwdlen, "/%s", argv[0]);
} }
int crashCatcherInstallHandlers( static bool crashCatcherInstallHandlers(char** argv)
int argc, char** argv, int num_signals, int* signals, const char* logfile, int (*user_info)(char*, char*))
{ {
struct sigaction sa;
stack_t altss;
int retval;
if (argc == 2 && strcmp(argv[1], crash_switch) == 0)
crash_handler(logfile);
cc_user_info = user_info;
getExecPath(argv); getExecPath(argv);
/* Set an alternate signal stack so SIGSEGVs caused by stack overflows /* Set an alternate signal stack so SIGSEGVs caused by stack overflows
* still run */ * still run */
static char* altstack = new char[SIGSTKSZ]; static char* altstack = new char[SIGSTKSZ];
stack_t altss;
altss.ss_sp = altstack; altss.ss_sp = altstack;
altss.ss_flags = 0; altss.ss_flags = 0;
altss.ss_size = SIGSTKSZ; altss.ss_size = SIGSTKSZ;
sigaltstack(&altss, nullptr); if (sigaltstack(&altss, nullptr) == -1)
{
Log(Debug::Error) << "Failed to call sigaltstack: " << std::generic_category().message(errno);
return false;
}
struct sigaction sa;
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = crash_catcher; sa.sa_sigaction = crash_catcher;
sa.sa_flags = SA_RESETHAND | SA_NODEFER | SA_SIGINFO | SA_ONSTACK; sa.sa_flags = SA_RESETHAND | SA_NODEFER | SA_SIGINFO | SA_ONSTACK;
sigemptyset(&sa.sa_mask); if (sigemptyset(&sa.sa_mask) == -1)
{
Log(Debug::Error) << "Failed to call sigemptyset: " << std::generic_category().message(errno);
return false;
}
retval = 0; for (const SignalInfo& signal : signals)
while (num_signals--)
{ {
if ((*signals != SIGSEGV && *signals != SIGILL && *signals != SIGFPE && *signals != SIGABRT if (sigaction(signal.mCode, &sa, nullptr) == -1)
&& *signals != SIGBUS)
|| sigaction(*signals, &sa, nullptr) == -1)
{ {
*signals = 0; Log(Debug::Error) << "Failed to call sigaction for signal " << signal.mName << " (" << signal.mCode
retval = -1; << "): " << std::generic_category().message(errno);
return false;
} }
++signals;
} }
return retval;
return true;
}
namespace
{
#if defined(__APPLE__)
bool isDebuggerPresent(const auto& info)
{
return (info.kp_proc.p_flag & P_TRACED) != 0;
}
#elif defined(__FreeBSD__)
bool isDebuggerPresent(const auto& info)
{
return (info.ki_flag & P_TRACED) != 0;
}
#endif
} }
static bool is_debugger_present() static bool isDebuggerPresent()
{ {
#if defined(__linux__) #if defined(__linux__)
std::filesystem::path procstatus = std::filesystem::path("/proc/self/status"); std::filesystem::path procstatus = std::filesystem::path("/proc/self/status");
@ -512,44 +559,23 @@ static bool is_debugger_present()
} }
} }
return false; return false;
#elif defined(__APPLE__) #elif defined(__APPLE__) || defined(__FreeBSD__)
int junk;
int mib[4];
struct kinfo_proc info; struct kinfo_proc info;
size_t size; std::memset(&info, 0, sizeof(info));
// Initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.
info.kp_proc.p_flag = 0;
// Initialize mib, which tells sysctl the info we want, in this case // Initialize mib, which tells sysctl the info we want, in this case
// we're looking for information about a specific process ID. // we're looking for information about a specific process ID.
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
// Call sysctl.
size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0);
assert(junk == 0);
// We're being debugged if the P_TRACED flag is set.
return (info.kp_proc.p_flag & P_TRACED) != 0;
#elif defined(__FreeBSD__)
struct kinfo_proc info;
size_t size = sizeof(info); size_t size = sizeof(info);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() }; if (sysctl(mib, std::size(mib), &info, &size, nullptr, 0) == -1)
{
Log(Debug::Warning) << "Failed to call sysctl, assuming no debugger: "
<< std::generic_category().message(errno);
return false;
}
if (sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) == 0) return isDebuggerPresent(info);
return (info.ki_flag & P_TRACED) != 0;
else
perror("Failed to retrieve process info");
return false;
#else #else
return false; return false;
#endif #endif
@ -557,14 +583,17 @@ static bool is_debugger_present()
void crashCatcherInstall(int argc, char** argv, const std::filesystem::path& crashLogPath) void crashCatcherInstall(int argc, char** argv, const std::filesystem::path& crashLogPath)
{ {
if ((argc == 2 && strcmp(argv[1], crash_switch) == 0) || !is_debugger_present()) #if (defined(__APPLE__) || (defined(__linux) && !defined(ANDROID)) || (defined(__unix) && !defined(ANDROID)) \
{ || defined(__posix))
int s[5] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGABRT }; if (argc == 2 && strcmp(argv[1], crash_switch) == 0)
if (crashCatcherInstallHandlers(argc, argv, 5, s, crashLogPath.c_str(), nullptr) == -1) handleCrash(Files::pathToUnicodeString(crashLogPath).c_str());
{
Log(Debug::Warning) << "Installing crash handler failed"; if (isDebuggerPresent())
} return;
else
Log(Debug::Info) << "Crash handler installed"; if (crashCatcherInstallHandlers(argv))
} Log(Debug::Info) << "Crash handler installed";
else
Log(Debug::Warning) << "Installing crash handler failed";
#endif
} }

@ -2,21 +2,7 @@
#define CRASHCATCHER_H #define CRASHCATCHER_H
#include <filesystem> #include <filesystem>
#include <string>
#if (defined(__APPLE__) || (defined(__linux) && !defined(ANDROID)) || (defined(__unix) && !defined(ANDROID)) \ void crashCatcherInstall(int argc, char** argv, const std::filesystem::path& crashLogPath);
|| defined(__posix))
#define USE_CRASH_CATCHER 1
#else
#define USE_CRASH_CATCHER 0
#endif
constexpr char crash_switch[] = "--cc-handle-crash";
#if USE_CRASH_CATCHER
extern void crashCatcherInstall(int argc, char** argv, const std::filesystem::path& crashLogPath);
#else
inline void crashCatcherInstall(int, char**, const std::string& crashLogPath) {}
#endif
#endif #endif

@ -352,8 +352,7 @@ int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, c
#else #else
const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.log"; const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.log";
// install the crash handler as soon as possible. // install the crash handler as soon as possible.
crashCatcherInstall( crashCatcherInstall(argc, argv, std::filesystem::temp_directory_path() / crashLogName);
argc, argv, Files::pathToUnicodeString(std::filesystem::temp_directory_path() / crashLogName));
#endif #endif
ret = innerApplication(argc, argv); ret = innerApplication(argc, argv);
} }

Loading…
Cancel
Save