Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions library/Crashlog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "DFHackVersion.h"

#include <csignal>
#include <iomanip>
#include <filesystem>
Expand Down Expand Up @@ -42,7 +43,8 @@ void signal_crashlog_complete() {

std::thread crashlog_thread;

extern "C" void dfhack_crashlog_handle_signal(int sig) {
// Force this method to be inlined so that it doesn't create a stack frame
[[gnu::always_inline]] inline void handle_signal_internal(int sig) {
if (shutdown.load() || crashed.exchange(true) || crashlog_ready.load()) {
// Ensure the signal handler doesn't try to write a crashlog
// whilst the crashlog thread is unavailable.
Expand All @@ -61,8 +63,12 @@ extern "C" void dfhack_crashlog_handle_signal(int sig) {
std::quick_exit(1);
}

extern "C" void dfhack_crashlog_handle_signal(int sig) {
handle_signal_internal(sig);
}

void dfhack_crashlog_handle_terminate() {
dfhack_crashlog_handle_signal(0);
handle_signal_internal(0);
}

std::string signal_name(int sig) {
Expand Down Expand Up @@ -105,10 +111,6 @@ std::filesystem::path get_crashlog_path() {

void dfhack_save_crashlog() {
char** backtrace_strings = backtrace_symbols(crash_info.backtrace, crash_info.backtrace_entries);
if (!backtrace_strings) {
// Allocation failed, give up
return;
}
try {
std::filesystem::path crashlog_path = get_crashlog_path();
std::ofstream crashlog(crashlog_path);
Expand All @@ -122,8 +124,14 @@ void dfhack_save_crashlog() {
crashlog << "Signal " << signal << "\n";
}

for (int i = 0; i < crash_info.backtrace_entries; i++) {
crashlog << i << "> " << backtrace_strings[i] << "\n";
if (crash_info.backtrace_entries >= 2 && backtrace_strings != nullptr) {
// Skip the first backtrace entry as it will always be dfhack_crashlog_handle_(signal|terminate)
for (int i = 1; i < crash_info.backtrace_entries; i++) {
crashlog << i - 1 << "> " << backtrace_strings[i] << "\n";
}
} else {
// Make it clear if no relevant backtrace was able to be obtained
crashlog << "Failed to obtain relevant backtrace\n";
}
} catch (...) {}

Expand Down