OpenTTD Source: src/os/unix/crashlog_unix.cpp Source File

OpenTTD Source 12.2

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../../stdafx.h"
11 #include "../../crashlog.h"
12 #include "../../string_func.h"
13 #include "../../gamelog.h"
14 #include "../../saveload/saveload.h"
15
16 #include <errno.h>
17 #include <signal.h>
18 #include <sys/utsname.h>
19
20 #if defined(__GLIBC__)
21 /* Execinfo (and thus making stacktraces) is a GNU extension */
22 # include <execinfo.h>
23 #elif defined(SUNOS)
24 # include <ucontext.h>
25 # include <dlfcn.h>
26 #endif
27
28 #if defined(__NetBSD__)
29 #include <unistd.h>
30 #endif
31
32 #include "../../safeguards.h"
33
37 class CrashLogUnix : public CrashLog {
39 int signum;
40
41 char *LogOSVersion(char *buffer, const char *last) const override
42 {
43 struct utsname name;
44 if (uname(&name) < 0) {
45 return buffer + seprintf(buffer, last, "Could not get OS version: %s\n", strerror(errno));
46 }
47
48 return buffer + seprintf(buffer, last,
49 "Operating system:\n"
50 " Name: %s\n"
51 " Release: %s\n"
52 " Version: %s\n"
53 " Machine: %s\n",
54 name.sysname,
55 name.release,
56 name.version,
57 name.machine
58 );
59 }
60
61 char *LogError(char *buffer, const char *last, const char *message) const override
62 {
63 return buffer + seprintf(buffer, last,
64 "Crash reason:\n"
65 " Signal: %s (%d)\n"
66 " Message: %s\n\n",
67 strsignal(this->signum),
68 this->signum,
69 message == nullptr ? "<none>" : message
70 );
71 }
72
73 #if defined(SUNOS)
74
75 struct StackWalkerParams {
76 char **bufptr;
77 const char *last;
78 int counter;
79 };
80
88 static int SunOSStackWalker(uintptr_t pc, int sig, void *params)
89 {
90 StackWalkerParams *wp = (StackWalkerParams *)params;
91
92 /* Resolve program counter to file and nearest symbol (if possible) */
93 Dl_info dli;
94 if (dladdr((void *)pc, &dli) != 0) {
95 *wp->bufptr += seprintf(*wp->bufptr, wp->last, " [%02i] %s(%s+0x%x) [0x%x]\n",
96 wp->counter, dli.dli_fname, dli.dli_sname, (int)((byte *)pc - (byte *)dli.dli_saddr), (uint)pc);
97 } else {
98 *wp->bufptr += seprintf(*wp->bufptr, wp->last, " [%02i] [0x%x]\n", wp->counter, (uint)pc);
99 }
100 wp->counter++;
101
102 return 0;
103 }
104 #endif
105
106 char *LogStacktrace(char *buffer, const char *last) const override
107 {
108 buffer += seprintf(buffer, last, "Stacktrace:\n");
109 #if defined(__GLIBC__)
110 void *trace[64];
111 int trace_size = backtrace(trace, lengthof(trace));
112
113 char **messages = backtrace_symbols(trace, trace_size);
114 for (int i = 0; i < trace_size; i++) {
115 buffer += seprintf(buffer, last, " [%02i] %s\n", i, messages[i]);
116 }
117 free(messages);
118 #elif defined(SUNOS)
119 ucontext_t uc;
120 if (getcontext(&uc) != 0) {
121 buffer += seprintf(buffer, last, " getcontext() failed\n\n");
122 return buffer;
123 }
124
125 StackWalkerParams wp = { &buffer, last, 0 };
126 walkcontext(&uc, &CrashLogUnix::SunOSStackWalker, &wp);
127 #else
128 buffer += seprintf(buffer, last, " Not supported.\n");
129 #endif
130 return buffer + seprintf(buffer, last, "\n");
131 }
132 public:
137 CrashLogUnix(int signum) :
138 signum(signum)
139 {
140 }
141 };
142
144 static const int _signals_to_handle[] = { SIGSEGV, SIGABRT, SIGFPE, SIGBUS, SIGILL };
145
151 static void CDECL HandleCrash(int signum)
152 {
153 /* Disable all handling of signals by us, so we don't go into infinite loops. */
154 for (const int *i = _signals_to_handle; i != endof(_signals_to_handle); i++) {
155 signal(*i, SIG_DFL);
156 }
157
158 if (GamelogTestEmergency()) {
159 printf("A serious fault condition occurred in the game. The game will shut down.\n");
160 printf("As you loaded an emergency savegame no crash information will be generated.\n");
161 abort();
162 }
163
164 if (SaveloadCrashWithMissingNewGRFs()) {
165 printf("A serious fault condition occurred in the game. The game will shut down.\n");
166 printf("As you loaded an savegame for which you do not have the required NewGRFs\n");
167 printf("no crash information will be generated.\n");
168 abort();
169 }
170
171 CrashLogUnix log(signum);
172 log.MakeCrashLog();
173
174 CrashLog::AfterCrashLogCleanup();
175 abort();
176 }
177
178 /* static */ void CrashLog::InitialiseCrashLog()
179 {
180 for (const int *i = _signals_to_handle; i != endof(_signals_to_handle); i++) {
181 signal(*i, HandleCrash);
182 }
183 }
184
185 /* static */ void CrashLog::InitThread()
186 {
187 }
CrashLog::MakeCrashLog
bool MakeCrashLog() const
Makes the crash log, writes it to a file and then subsequently tries to make a crash dump and crash s...
Definition: crashlog.cpp:458
CrashLog::AfterCrashLogCleanup
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:526
HandleCrash
static void CDECL HandleCrash(int signum)
Entry point for the crash handler.
Definition: crashlog_unix.cpp:151
CrashLogUnix::LogOSVersion
char * LogOSVersion(char *buffer, const char *last) const override
Writes OS' version to the buffer.
Definition: crashlog_unix.cpp:41
CrashLogUnix::LogError
char * LogError(char *buffer, const char *last, const char *message) const override
Writes actually encountered error to the buffer.
Definition: crashlog_unix.cpp:61
_signals_to_handle
static const int _signals_to_handle[]
The signals we want our crash handler to handle.
Definition: crashlog_unix.cpp:144
CrashLog::InitThread
static void InitThread()
Prepare crash log handler for a newly started thread.
Definition: crashlog_osx.cpp:261
GamelogTestEmergency
bool GamelogTestEmergency()
Finds out if current game is a loaded emergency savegame.
Definition: gamelog.cpp:420
SaveloadCrashWithMissingNewGRFs
bool SaveloadCrashWithMissingNewGRFs()
Did loading the savegame cause a crash? If so, were NewGRFs missing?
Definition: afterload.cpp:365
CrashLogUnix
Unix implementation for the crash logger.
Definition: crashlog_unix.cpp:37
CrashLog::message
static const char * message
Pointer to the error message.
Definition: crashlog.h:19
CrashLog
Helper class for creating crash logs.
Definition: crashlog.h:16
CrashLog::InitialiseCrashLog
static void InitialiseCrashLog()
Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler ins...
Definition: crashlog_osx.cpp:254
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
CrashLogUnix::signum
int signum
Signal that has been thrown.
Definition: crashlog_unix.cpp:39
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
CrashLogUnix::CrashLogUnix
CrashLogUnix(int signum)
A crash log is always generated by signal.
Definition: crashlog_unix.cpp:137
CrashLogUnix::LogStacktrace
char * LogStacktrace(char *buffer, const char *last) const override
Writes the stack trace to the buffer, if there is information about it available.
Definition: crashlog_unix.cpp:106