Skip to content

Commit 7f6f80c

Browse files
authored
Merge pull request #5672 from NicksWorld/crashlog/hide_signal_handler
Exclude signal handler from linux crashlogs
2 parents 0789f3f + 5687f5c commit 7f6f80c

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

library/Crashlog.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "DFHackVersion.h"
2+
23
#include <csignal>
34
#include <iomanip>
45
#include <filesystem>
@@ -42,7 +43,8 @@ void signal_crashlog_complete() {
4243

4344
std::thread crashlog_thread;
4445

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

66+
extern "C" void dfhack_crashlog_handle_signal(int sig) {
67+
handle_signal_internal(sig);
68+
}
69+
6470
void dfhack_crashlog_handle_terminate() {
65-
dfhack_crashlog_handle_signal(0);
71+
handle_signal_internal(0);
6672
}
6773

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

106112
void dfhack_save_crashlog() {
107113
char** backtrace_strings = backtrace_symbols(crash_info.backtrace, crash_info.backtrace_entries);
108-
if (!backtrace_strings) {
109-
// Allocation failed, give up
110-
return;
111-
}
112114
try {
113115
std::filesystem::path crashlog_path = get_crashlog_path();
114116
std::ofstream crashlog(crashlog_path);
@@ -122,8 +124,14 @@ void dfhack_save_crashlog() {
122124
crashlog << "Signal " << signal << "\n";
123125
}
124126

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

0 commit comments

Comments
 (0)