|
| 1 | +#include "DFHackVersion.h" |
| 2 | +#include <csignal> |
| 3 | +#include <iomanip> |
| 4 | +#include <filesystem> |
| 5 | +#include <fstream> |
| 6 | +#include <thread> |
| 7 | + |
| 8 | +#include <sys/eventfd.h> |
| 9 | +#include <execinfo.h> |
| 10 | +#include <unistd.h> |
| 11 | + |
| 12 | +const int BT_ENTRY_MAX = 25; |
| 13 | +struct CrashInfo { |
| 14 | + int backtrace_entries = 0; |
| 15 | + void* backtrace[BT_ENTRY_MAX]; |
| 16 | + int signal = 0; |
| 17 | +}; |
| 18 | + |
| 19 | +CrashInfo crash_info; |
| 20 | + |
| 21 | +std::atomic<bool> crashed = false; |
| 22 | +std::atomic<bool> crashlog_ready = false; |
| 23 | +std::atomic<bool> shutdown = false; |
| 24 | + |
| 25 | +// Use eventfd for async-signal safe waiting |
| 26 | +int crashlog_complete = -1; |
| 27 | + |
| 28 | +void flag_set(std::atomic_bool &atom) { |
| 29 | + atom.store(true); |
| 30 | + atom.notify_all(); |
| 31 | +} |
| 32 | +void flag_wait(std::atomic_bool &atom) { |
| 33 | + atom.wait(false); |
| 34 | +} |
| 35 | + |
| 36 | +void signal_crashlog_complete() { |
| 37 | + if (crashlog_complete == -1) |
| 38 | + return; |
| 39 | + uint64_t v = 1; |
| 40 | + [[maybe_unused]] auto _ = write(crashlog_complete, &v, sizeof(v)); |
| 41 | +} |
| 42 | + |
| 43 | +std::thread crashlog_thread; |
| 44 | + |
| 45 | +extern "C" void dfhack_crashlog_handle_signal(int sig) { |
| 46 | + if (shutdown.load() || crashed.exchange(true) || crashlog_ready.load()) { |
| 47 | + // Ensure the signal handler doesn't try to write a crashlog |
| 48 | + // whilst the crashlog thread is unavailable. |
| 49 | + std::quick_exit(1); |
| 50 | + } |
| 51 | + crash_info.signal = sig; |
| 52 | + crash_info.backtrace_entries = backtrace(crash_info.backtrace, BT_ENTRY_MAX); |
| 53 | + |
| 54 | + // Signal saving of crashlog |
| 55 | + flag_set(crashlog_ready); |
| 56 | + // Wait for completion via eventfd read, if fd isn't valid, bail |
| 57 | + if (crashlog_complete != -1) { |
| 58 | + [[maybe_unused]] uint64_t v; |
| 59 | + [[maybe_unused]] auto _ = read(crashlog_complete, &v, sizeof(v)); |
| 60 | + } |
| 61 | + std::quick_exit(1); |
| 62 | +} |
| 63 | + |
| 64 | +void dfhack_crashlog_handle_terminate() { |
| 65 | + dfhack_crashlog_handle_signal(0); |
| 66 | +} |
| 67 | + |
| 68 | +std::string signal_name(int sig) { |
| 69 | + switch (sig) { |
| 70 | + case SIGINT: |
| 71 | + return "SIGINT"; |
| 72 | + case SIGILL: |
| 73 | + return "SIGILL"; |
| 74 | + case SIGABRT: |
| 75 | + return "SIGABRT"; |
| 76 | + case SIGFPE: |
| 77 | + return "SIGFPE"; |
| 78 | + case SIGSEGV: |
| 79 | + return "SIGSEGV"; |
| 80 | + case SIGTERM: |
| 81 | + return "SIGTERM"; |
| 82 | + } |
| 83 | + return ""; |
| 84 | +} |
| 85 | + |
| 86 | +std::filesystem::path get_crashlog_path() { |
| 87 | + std::time_t time = std::time(nullptr); |
| 88 | + std::tm* tm = std::localtime(&time); |
| 89 | + |
| 90 | + std::string timestamp = "unknown"; |
| 91 | + if (tm) { |
| 92 | + char stamp[64]; |
| 93 | + std::size_t out = strftime(&stamp[0], 63, "%Y-%m-%d-%H-%M-%S", tm); |
| 94 | + if (out != 0) |
| 95 | + timestamp = stamp; |
| 96 | + } |
| 97 | + |
| 98 | + std::filesystem::path dir = "crashlog"; |
| 99 | + std::error_code err; |
| 100 | + std::filesystem::create_directories(dir, err); |
| 101 | + |
| 102 | + std::filesystem::path log_path = dir / ("crash_" + timestamp + ".txt"); |
| 103 | + return log_path; |
| 104 | +} |
| 105 | + |
| 106 | +void dfhack_save_crashlog() { |
| 107 | + char** backtrace_strings = backtrace_symbols(crash_info.backtrace, crash_info.backtrace_entries); |
| 108 | + if (!backtrace_strings) { |
| 109 | + // Allocation failed, give up |
| 110 | + return; |
| 111 | + } |
| 112 | + try { |
| 113 | + std::filesystem::path crashlog_path = get_crashlog_path(); |
| 114 | + std::ofstream crashlog(crashlog_path); |
| 115 | + |
| 116 | + crashlog << "Dwarf Fortress Linux has crashed!" << "\n"; |
| 117 | + crashlog << "Dwarf Fortress Version " << DFHack::Version::df_version() << "\n"; |
| 118 | + crashlog << "DFHack Version " << DFHack::Version::dfhack_version() << "\n\n"; |
| 119 | + |
| 120 | + std::string signal = signal_name(crash_info.signal); |
| 121 | + if (!signal.empty()) { |
| 122 | + crashlog << "Signal " << signal << "\n"; |
| 123 | + } |
| 124 | + |
| 125 | + for (int i = 0; i < crash_info.backtrace_entries; i++) { |
| 126 | + crashlog << i << "> " << backtrace_strings[i] << "\n"; |
| 127 | + } |
| 128 | + } catch (...) {} |
| 129 | + |
| 130 | + free(backtrace_strings); |
| 131 | +} |
| 132 | + |
| 133 | +void dfhack_crashlog_thread() { |
| 134 | + // Wait for activation signal |
| 135 | + flag_wait(crashlog_ready); |
| 136 | + if (shutdown.load()) // Shutting down gracefully, end thread. |
| 137 | + return; |
| 138 | + |
| 139 | + dfhack_save_crashlog(); |
| 140 | + signal_crashlog_complete(); |
| 141 | + std::quick_exit(1); |
| 142 | +} |
| 143 | + |
| 144 | +std::terminate_handler term_handler = nullptr; |
| 145 | + |
| 146 | +const int desired_signals[3] = {SIGSEGV,SIGILL,SIGABRT}; |
| 147 | +namespace DFHack { |
| 148 | + void dfhack_crashlog_init() { |
| 149 | + // Initialize eventfd flag |
| 150 | + crashlog_complete = eventfd(0, EFD_CLOEXEC); |
| 151 | + |
| 152 | + crashlog_thread = std::thread(dfhack_crashlog_thread); |
| 153 | + |
| 154 | + for (int signal : desired_signals) { |
| 155 | + std::signal(signal, dfhack_crashlog_handle_signal); |
| 156 | + } |
| 157 | + term_handler = std::set_terminate(dfhack_crashlog_handle_terminate); |
| 158 | + |
| 159 | + // https://sourceware.org/glibc/manual/latest/html_mono/libc.html#index-backtrace-1 |
| 160 | + // backtrace is AsyncSignal-Unsafe due to dynamic loading of libgcc_s |
| 161 | + // Using it here ensures it is loaded before use in the signal handler. |
| 162 | + [[maybe_unused]] int _ = backtrace(crash_info.backtrace, 1); |
| 163 | + } |
| 164 | + |
| 165 | + void dfhack_crashlog_shutdown() { |
| 166 | + shutdown.exchange(true); |
| 167 | + for (int signal : desired_signals) { |
| 168 | + std::signal(signal, SIG_DFL); |
| 169 | + } |
| 170 | + std::set_terminate(term_handler); |
| 171 | + |
| 172 | + // Shutdown the crashlog thread. |
| 173 | + flag_set(crashlog_ready); |
| 174 | + crashlog_thread.join(); |
| 175 | + |
| 176 | + // If the signal handler is somehow running whilst here, let it terminate |
| 177 | + signal_crashlog_complete(); |
| 178 | + if (crashlog_complete != -1) |
| 179 | + close(crashlog_complete); // Close fd |
| 180 | + return; |
| 181 | + } |
| 182 | +} |
0 commit comments