diff --git a/library/Crashlog.cpp b/library/Crashlog.cpp index c560666f99..3860e0106e 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) { @@ -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); @@ -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 (...) {}