From d6eaea10e438026bdc72a9b1ead7cc3b0bc58621 Mon Sep 17 00:00:00 2001 From: Nicholas McDaniel Date: Sat, 29 Nov 2025 18:41:45 -0500 Subject: [PATCH 1/2] Ensure the dfhack signal handler doesn't appear in crashlogs --- library/Crashlog.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/library/Crashlog.cpp b/library/Crashlog.cpp index c560666f99..681e4fd4c5 100644 --- a/library/Crashlog.cpp +++ b/library/Crashlog.cpp @@ -1,4 +1,5 @@ #include "DFHackVersion.h" + #include #include #include @@ -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. @@ -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) { @@ -122,8 +128,9 @@ void dfhack_save_crashlog() { crashlog << "Signal " << signal << "\n"; } - for (int i = 0; i < crash_info.backtrace_entries; i++) { - crashlog << i << "> " << backtrace_strings[i] << "\n"; + // 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"; } } catch (...) {} From 5687f5c0f0ee465d31a6a9b82e1920919b911422 Mon Sep 17 00:00:00 2001 From: Nicholas McDaniel Date: Thu, 4 Dec 2025 15:01:47 -0500 Subject: [PATCH 2/2] Make a failure to obtain relevant crash backtrace obvious --- library/Crashlog.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/library/Crashlog.cpp b/library/Crashlog.cpp index 681e4fd4c5..3860e0106e 100644 --- a/library/Crashlog.cpp +++ b/library/Crashlog.cpp @@ -111,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); @@ -128,9 +124,14 @@ void dfhack_save_crashlog() { crashlog << "Signal " << signal << "\n"; } - // 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"; + 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 (...) {}