OpenTTD Source 20261112-master-gbcbec5e63f
crashlog_unix.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../../stdafx.h"
11#include "../../core/format.hpp"
12#include "../../crashlog.h"
13#include "../../fileio_func.h"
14#include "../../string_func.h"
15#include "../../gamelog.h"
17
18#include <setjmp.h>
19#include <signal.h>
20#include <sys/utsname.h>
21
22#if defined(__GLIBC__)
23/* Execinfo (and thus making stacktraces) is a GNU extension */
24# include <execinfo.h>
25#endif
26
27#ifdef WITH_UNOFFICIAL_BREAKPAD
28# include <client/linux/handler/exception_handler.h>
29#endif
30
31#include "../../safeguards.h"
32
34static constexpr int _signals_to_handle[] = { SIGSEGV, SIGABRT, SIGFPE, SIGBUS, SIGILL, SIGQUIT };
35
39class CrashLogUnix : public CrashLog {
41 int signum;
42
43 void SurveyCrash(nlohmann::json &survey) const override
44 {
45 survey["id"] = signum;
46 survey["reason"] = strsignal(signum);
47 }
48
49 void SurveyStacktrace([[maybe_unused]] nlohmann::json &survey) const override
50 {
51#if defined(__GLIBC__)
52 void *trace[64];
53 int trace_size = backtrace(trace, lengthof(trace));
54
55 survey = nlohmann::json::array();
56
57 char **messages = backtrace_symbols(trace, trace_size);
58 for (int i = 0; i < trace_size; i++) {
59 survey.push_back(messages[i]);
60 }
61 free(messages);
62#endif
63 }
64
65#ifdef WITH_UNOFFICIAL_BREAKPAD
66 static bool MinidumpCallback(const google_breakpad::MinidumpDescriptor &descriptor, void *context, bool succeeded)
67 {
68 CrashLogUnix *crashlog = reinterpret_cast<CrashLogUnix *>(context);
69
70 crashlog->crashdump_filename = crashlog->CreateFileName(".dmp");
71 std::rename(descriptor.path(), crashlog->crashdump_filename.c_str());
72 return succeeded;
73 }
74
75 bool WriteCrashDump() override
76 {
77 return google_breakpad::ExceptionHandler::WriteMinidump(_personal_dir, MinidumpCallback, this);
78 }
79#endif
80
81 bool TryExecute(std::string_view section_name, std::function<bool()> &&func) override
82 {
83 this->try_execute_active = true;
84
85 /* Setup a longjump in case a crash happens. */
86 if (setjmp(this->internal_fault_jmp_buf) != 0) {
87 fmt::print("Something went wrong when attempting to fill {} section of the crash log.\n", section_name);
88
89 /* Reset the signals and continue on. The handler is responsible for dealing with the crash. */
90 sigset_t sigs;
91 sigemptyset(&sigs);
92 for (int signum : _signals_to_handle) {
93 sigaddset(&sigs, signum);
94 }
95 sigprocmask(SIG_UNBLOCK, &sigs, nullptr);
96
97 this->try_execute_active = false;
98 return false;
99 }
100
101 bool res = func();
102 this->try_execute_active = false;
103 return res;
104 }
105
106public:
113 {
114 }
115
118
120 bool try_execute_active = false;
121
124};
125
126/* static */ CrashLogUnix *CrashLogUnix::current = nullptr;
127
134static sigset_t SetSignals(void(*handler)(int))
135{
136 sigset_t sigs;
137 sigemptyset(&sigs);
138 for (int signum : _signals_to_handle) {
139 sigaddset(&sigs, signum);
140 }
141
142 struct sigaction sa{};
143 sa.sa_flags = SA_RESTART;
144
145 sigemptyset(&sa.sa_mask);
146 sa.sa_handler = handler;
147 sa.sa_mask = sigs;
148
149 for (int signum : _signals_to_handle) {
150 sigaction(signum, &sa, nullptr);
151 }
152
153 return sigs;
154}
155
161static void CDECL HandleInternalCrash([[maybe_unused]] int signum)
162{
163 if (CrashLogUnix::current == nullptr || !CrashLogUnix::current->try_execute_active) {
164 fmt::print("Something went seriously wrong when creating the crash log. Aborting.\n");
165 std::_Exit(1);
166 }
167
168 longjmp(CrashLogUnix::current->internal_fault_jmp_buf, 1);
169}
170
176static void CDECL HandleCrash(int signum)
177{
178 if (CrashLogUnix::current != nullptr) {
180 std::_Exit(2);
181 }
182
183 /* Capture crashing during the handling of a crash. */
184 sigset_t sigs = SetSignals(HandleInternalCrash);
185 sigset_t old_sigset;
186 sigprocmask(SIG_UNBLOCK, &sigs, &old_sigset);
187
188 if (_gamelog.TestEmergency()) {
189 fmt::print("A serious fault condition occurred in the game. The game will shut down.\n");
190 fmt::print("As you loaded an emergency savegame no crash information will be generated.\n");
191 std::_Exit(3);
192 }
193
195 fmt::print("A serious fault condition occurred in the game. The game will shut down.\n");
196 fmt::print("As you loaded an savegame for which you do not have the required NewGRFs\n");
197 fmt::print("no crash information will be generated.\n");
198 std::_Exit(3);
199 }
200
201 CrashLogUnix *log = new CrashLogUnix(signum);
203 log->MakeCrashLog();
204
206 std::_Exit(2);
207}
208
209/* static */ void CrashLog::InitialiseCrashLog()
210{
212}
213
214/* static */ void CrashLog::InitThread()
215{
216}
bool SaveloadCrashWithMissingNewGRFs()
Did loading the savegame cause a crash?
Unix implementation for the crash logger.
CrashLogUnix(int signum)
A crash log is always generated by signal.
void SurveyCrash(nlohmann::json &survey) const override
Convert system crash reason to JSON.
bool try_execute_active
Whether we are in a TryExecute block.
jmp_buf internal_fault_jmp_buf
Buffer to track the long jump set setup.
static CrashLogUnix * current
Points to the current crash log.
bool TryExecute(std::string_view section_name, std::function< bool()> &&func) override
Execute the func() and return its value.
void SurveyStacktrace(nlohmann::json &survey) const override
Convert stacktrace to JSON.
int signum
Signal that has been thrown.
Helper class for creating crash logs.
Definition crashlog.h:18
void MakeCrashLog()
Makes the crash log, writes it to a file and then subsequently tries to make a crash dump and crash s...
Definition crashlog.cpp:272
virtual bool WriteCrashDump()
Write the (crash) dump to a file.
Definition crashlog.cpp:212
static void InitThread()
Prepare crash log handler for a newly started thread.
static void AfterCrashLogCleanup()
Try to close the sound/video stuff so it doesn't keep lingering around incorrect video states or so,...
Definition crashlog.cpp:338
std::string CreateFileName(std::string_view ext, bool with_dir=true) const
Create a timestamped filename.
Definition crashlog.cpp:76
static void InitialiseCrashLog()
Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler ins...
Functions to be called to log a crash.
static void HandleInternalCrash(int)
Entry point for a crash that happened during the handling of a crash.
static void HandleCrash(int signum)
Entry point for the crash handler.
static constexpr int _signals_to_handle[]
The signals we want our crash handler to handle.
static sigset_t SetSignals(void(*handler)(int))
Set a signal handler for all signals we want to capture.
static void HandleCrash(int signum)
Entry point for the crash handler.
static constexpr int _signals_to_handle[]
The signals we want our crash handler to handle.
static void HandleInternalCrash(int signum)
Entry point for a crash that happened during the handling of a crash.
static sigset_t SetSignals(void(*handler)(int))
Set a signal handler for all signals we want to capture.
std::string _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition fileio.cpp:923
Functions for standard in/out file operations.
String formatting functions and helpers.
Gamelog _gamelog
Gamelog instance.
Definition gamelog.cpp:30
Functions to be called to log fundamental changes to the game.
A number of safeguards to prevent using unsafe methods.
Functions related to saving and loading games.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:261
Functions related to low-level strings.