1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-04-01 15:06:41 +00:00

Support lldb in crash catcher

This commit is contained in:
elsid 2024-01-19 15:19:09 +01:00
parent 0095cb604f
commit b96e32144c
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625

View file

@ -129,86 +129,140 @@ namespace
const auto it = std::find_if(info.begin(), info.end(), [&](const SignalInfo& v) { return v.mCode == code; }); const auto it = std::find_if(info.begin(), info.end(), [&](const SignalInfo& v) { return v.mCode == code; });
return it == info.end() ? "" : it->mDescription; return it == info.end() ? "" : it->mDescription;
} }
}
static void printGdbInfo(pid_t pid) struct Close
{ {
/* void operator()(const int* value) { close(*value); }
* 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. struct CloseFile
* So CoverityScan warning is valid only for ancient versions of stdlib. {
*/ void operator()(FILE* value) { fclose(value); }
char respfile[64] = "/tmp/gdb-respfile-XXXXXX"; };
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);
#ifdef __COVERITY__ #ifdef __COVERITY__
umask(0600); umask(0600);
#endif #endif
const int fd = mkstemp(respfile); const int fd = mkstemp(scriptPath);
if (fd == -1) if (fd == -1)
{ {
printf("Failed to call mkstemp: %s\n", std::generic_category().message(errno).c_str()); printf("Failed to call mkstemp: %s\n", std::generic_category().message(errno).c_str());
return; return false;
} }
std::unique_ptr<const char, Remove> tempFile(scriptPath);
std::unique_ptr<const int, Close> scopedFd(&fd);
FILE* const f = fdopen(fd, "w"); FILE* const file = fdopen(fd, "w");
if (f == nullptr) if (file == nullptr)
{ {
printf("Failed to open file for gdb output \"%s\": %s\n", respfile, printf("Failed to open file for %s output \"%s\": %s\n", T::sName, scriptPath,
std::generic_category().message(errno).c_str()); std::generic_category().message(errno).c_str());
} return false;
else }
{ std::unique_ptr<FILE, CloseFile> scopedFile(file);
fprintf(f,
"attach %d\n"
"shell echo \"\"\n"
"shell echo \"* Loaded Libraries\"\n"
"info sharedlibrary\n"
"shell echo \"\"\n"
"shell echo \"* Threads\"\n"
"info threads\n"
"shell echo \"\"\n"
"shell echo \"* FPU Status\"\n"
"info float\n"
"shell echo \"\"\n"
"shell echo \"* Registers\"\n"
"info registers\n"
"shell echo \"\"\n"
"shell echo \"* Backtrace\"\n"
"thread apply all backtrace full 1000\n"
"detach\n"
"quit\n",
pid);
fclose(f);
/* Run gdb and print process info. */ if (fprintf(file, "%s", T::sScript) < 0)
char cmd_buf[128]; {
snprintf(cmd_buf, sizeof(cmd_buf), "gdb --quiet --batch --command=%s", respfile); printf("Failed to write debugger script to file \"%s\": %s\n", scriptPath,
printf("Executing: %s\n", cmd_buf); std::generic_category().message(errno).c_str());
return false;
}
scopedFile = nullptr;
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);
const bool result = (ret == 0);
if (ret == -1) if (ret == -1)
printf( printf(
"\nFailed to create a crash report: %s.\n" "\nFailed to create a crash report: %s.\n"
"Please make sure that 'gdb' is installed and present in PATH then crash again.\n" "Please make sure that '%s' is installed and present in PATH then crash again.\n"
"Current PATH: %s\n", "Current PATH: %s\n",
std::generic_category().message(errno).c_str(), getenv("PATH")); T::sName, std::generic_category().message(errno).c_str(), getenv("PATH"));
else if (ret != 0) else if (ret != 0)
printf( printf(
"\nFailed to create a crash report.\n" "\nFailed to create a crash report.\n"
"Please make sure that 'gdb' is installed and present in PATH then crash again.\n" "Please make sure that '%s' is installed and present in PATH then crash again.\n"
"Current PATH: %s\n", "Current PATH: %s\n",
getenv("PATH")); T::sName, getenv("PATH"));
fflush(stdout); fflush(stdout);
return result;
} }
close(fd); struct Gdb
remove(respfile); {
fflush(stdout); static constexpr char sName[] = "gdb";
static constexpr char sScript[] = R"(shell echo ""
shell echo "* Loaded Libraries"
info sharedlibrary
shell echo ""
shell echo "* Threads"
info threads
shell echo ""
shell echo "* FPU Status"
info float
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;
}
} }
static void printSystemInfo(void) static void printSystemInfo(void)
@ -369,7 +423,7 @@ static void crash_catcher(int signum, siginfo_t* siginfo, void* /*context*/)
if (crash_info.pid > 0) if (crash_info.pid > 0)
{ {
printGdbInfo(crash_info.pid); printProcessInfo(crash_info.pid);
kill(crash_info.pid, SIGKILL); kill(crash_info.pid, SIGKILL);
} }