forked from crawl/crawl
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcrash.cc
450 lines (386 loc) · 11.5 KB
/
crash.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
* @file
* @brief UNIX-style crash handling functions (also used on Windows).
**/
#include "AppHdr.h"
#include "crash.h"
#ifdef USE_UNIX_SIGNALS
#include <csignal>
#include <sys/time.h>
#endif
#ifndef TARGET_OS_WINDOWS
# include <cerrno>
# include <sys/wait.h>
#endif
#if defined(UNIX)
#include <unistd.h>
#define BACKTRACE_SUPPORTED
#endif
#ifdef BACKTRACE_SUPPORTED
#if defined(TARGET_CPU_MIPS) || \
defined(TARGET_OS_FREEBSD) || \
defined(TARGET_OS_NETBSD) || \
defined(TARGET_OS_OPENBSD) || \
defined(TARGET_COMPILER_CYGWIN) || \
defined(__ANDROID__)
#undef BACKTRACE_SUPPORTED
#endif
#endif
#ifdef BACKTRACE_SUPPORTED
#include <cxxabi.h>
#if !defined(TARGET_OS_MACOSX) && \
!defined(TARGET_OS_WINDOWS) && \
!defined(TARGET_COMPILER_CYGWIN)
#include <execinfo.h>
#endif
#ifdef TARGET_OS_MACOSX
#include <dlfcn.h>
typedef int (*backtrace_t)(void * *, int);
typedef char **(*backtrace_symbols_t)(void * const *, int);
// Used to convert from void* to function pointer (without a
// compiler warning).
template <typename TO, typename FROM> TO nasty_cast(FROM f)
{
union
{
FROM f;
TO t;
} u;
u.f = f;
return u.t;
}
#endif // TARGET_OS_MACOSX
#endif // BACKTRACE_SUPPORTED
// Support Yama LSM ptrace restrictions
#ifdef TARGET_OS_LINUX
# include <sys/prctl.h>
# ifndef PR_SET_PTRACER
# define PR_SET_PTRACER 0x59616d61
# endif
# ifndef PR_SET_PTRACER_ANY
# define PR_SET_PTRACER_ANY ((unsigned long)-1)
# endif
#endif
#include "files.h"
#include "initfile.h"
#include "options.h"
#include "state.h"
#include "stringutil.h"
#include "syscalls.h"
#include "threads.h"
/////////////////////////////////////////////////////////////////////////////
// Code for printing out debugging info on a crash.
////////////////////////////////////////////////////////////////////////////
#ifdef USE_UNIX_SIGNALS
static int _crash_signal = 0;
static int _recursion_depth = 0;
static mutex_t crash_mutex;
// Make this non-static so stack traces are easier to follow
void crash_signal_handler(int sig_num);
void crash_signal_handler(int sig_num)
{
// We rely on mutexes ignoring locks held by the same thread.
// On some platforms, this must be explicitly enabled (which we do).
// This mutex is never unlocked again -- the first thread to crash will
// do a dump then terminate the process while everyone else waits here
// forever.
// XXX: This is a bit dangerous: if we catch a signal while any
// non-asynch-signal-safe function is executing, and then call
// pthread_mutex_lock() (which is also not asynch-signal-safe),
// the behaviour is undefined.
mutex_lock(crash_mutex);
if (crawl_state.game_crashed)
{
if (_recursion_depth > 0)
return;
_recursion_depth++;
fprintf(stderr, "Recursive crash.\n");
string dir = (!Options.morgue_dir.empty() ? Options.morgue_dir :
!SysEnv.crawl_dir.empty() ? SysEnv.crawl_dir
: "");
if (!dir.empty() && dir[dir.length() - 1] != FILE_SEPARATOR)
dir += FILE_SEPARATOR;
char name[180];
snprintf(name, sizeof(name), "%scrash-recursive-%s-%s.txt", dir.c_str(),
you.your_name.c_str(), make_file_time(time(nullptr)).c_str());
FILE* file = fopen_replace(name);
if (file == nullptr)
file = stderr;
write_stack_trace(file, 0);
if (file != stderr)
fclose(file);
return;
}
_crash_signal = sig_num;
crawl_state.game_crashed = true;
// During a crash, we may be in an inconsistent state (duh). Doing a number
// of things can cause a lock up, especially calling non-reentrant functions
// like malloc() and friends, used by C++ basics like std::string
// internally.
// There's no reliable way to ensure such things won't happen. A pragmatic
// solution is to abort the crash dump.
alarm(120);
// In case the crash dumper is unable to open a file and has to dump
// to stderr.
#ifndef USE_TILE_LOCAL
if (crawl_state.io_inited)
console_shutdown();
#endif
#ifdef USE_TILE_WEB
tiles.shutdown();
#endif
#ifdef WATCHDOG
/* Infinite loop protection.
Not tickling the watchdog for 60 seconds of user CPU time (not wall
time!) means something is terribly wrong. Even worst hogs like
pre-0.6 god renouncement or current Time Step in the Abyss don't take
more than several seconds.
DGL only -- local players will notice the game is stuck and be able
to kill it.
It's likely to die horribly -- it's one of signals that is often
received while in non-signal safe functions, especially malloc()
which _will_ fuck the process up (remember, C++ can't blink without
malloc()ing something). In such cases, alarm() above will kill us.
That's nasty and random, but at least should give us backtraces most
of the time, and avoid dragging down the servers. And even if for
some odd reason SIGALRM won't kill us, the worst that can happen is
wasting 100% CPU which is precisely what happens right now.
*/
if (sig_num == SIGVTALRM)
die_noline("Stuck game with 100%% CPU use\n");
#endif
do_crash_dump();
// Now crash for real.
signal(sig_num, SIG_DFL);
raise(sig_num);
}
#endif
void init_crash_handler()
{
#if defined(USE_UNIX_SIGNALS)
mutex_init(crash_mutex);
for (int i = 1; i <= 64; i++)
{
#ifdef SIGALRM
if (i == SIGALRM)
continue;
#endif
#ifdef SIGHUP
if (i == SIGHUP)
continue;
#endif
#ifdef SIGQUIT
if (i == SIGQUIT)
continue;
#endif
#ifdef SIGINT
if (i == SIGINT)
continue;
#endif
#ifdef SIGCHLD
if (i == SIGCHLD)
continue;
#endif
#ifdef SIGTSTP
if (i == SIGTSTP)
continue;
#endif
#ifdef SIGCONT
if (i == SIGCONT)
continue;
#endif
#ifdef SIGIO
if (i == SIGIO)
continue;
#endif
#ifdef SIGPROF
if (i == SIGPROF)
continue;
#endif
#ifdef SIGTTOU
if (i == SIGTTOU)
continue;
#endif
#ifdef SIGTTIN
if (i == SIGTTIN)
continue;
#endif
#ifdef SIGKILL
if (i == SIGKILL)
continue;
#endif
#ifdef SIGSTOP
if (i == SIGSTOP)
continue;
#endif
#ifdef SIGWINCH
if (i == SIGWINCH)
continue;
#endif
signal(i, crash_signal_handler);
}
#endif // if defined(USE_UNIX_SIGNALS)
}
void dump_crash_info(FILE* file)
{
#if defined(UNIX)
const char *name = strsignal(_crash_signal);
if (name == nullptr)
name = "INVALID";
fprintf(file, "Crash caused by signal #%d: %s\n\n", _crash_signal,
name);
#endif
}
#if defined(BACKTRACE_SUPPORTED)
void write_stack_trace(FILE* file, int ignore_count)
{
void* frames[50];
#if defined(TARGET_OS_MACOSX)
backtrace_t backtrace;
backtrace_symbols_t backtrace_symbols;
backtrace = nasty_cast<backtrace_t, void*>(dlsym(RTLD_DEFAULT, "backtrace"));
backtrace_symbols = nasty_cast<backtrace_symbols_t, void*>(dlsym(RTLD_DEFAULT, "backtrace_symbols"));
if (!backtrace || !backtrace_symbols)
{
fprintf(stderr, "Couldn't get a stack trace.\n");
fprintf(file, "Couldn't get a stack trace.\n");
return;
}
#endif
int num_frames = backtrace(frames, ARRAYSZ(frames));
char **symbols = backtrace_symbols(frames, num_frames);
#if !defined(TARGET_OS_MACOSX)
if (symbols == nullptr)
{
fprintf(stderr, "Out of memory.\n");
fprintf(file, "Out of memory.\n");
// backtrace_symbols_fd() can print out the stack trace even if
// malloc() can't find any free memory.
backtrace_symbols_fd(frames, num_frames, fileno(file));
return;
}
#endif
fprintf(file, "Obtained %d stack frames.\n", num_frames);
// Now we prettify the printout to even show demangled C++ function names.
string bt = "";
for (int i = 0; i < num_frames; i++)
{
#if defined(TARGET_OS_MACOSX)
char *addr = ::strstr(symbols[i], "0x");
char *mangled = ::strchr(addr, ' ') + 1;
char *offset = ::strchr(addr, '+');
char *postmangle = ::strchr(mangled, ' ');
if (mangled)
*(mangled - 1) = 0;
bt += addr;
int status;
bt += ": ";
if (addr && mangled)
{
if (postmangle)
*postmangle = '\0';
char *realname = abi::__cxa_demangle(mangled, 0, 0, &status);
if (realname)
bt += realname;
else
bt += mangled;
bt += " ";
bt += offset;
free(realname);
}
#else // TARGET_OS_MACOSX
bt += symbols[i];
int status;
// Extract the identifier from symbols[i]. It's inside of parens.
char *firstparen = ::strchr(symbols[i], '(');
char *lastparen = ::strchr(symbols[i], '+');
if (firstparen != 0 && lastparen != 0 && firstparen < lastparen)
{
bt += ": ";
*lastparen = '\0';
char *realname = abi::__cxa_demangle(firstparen + 1, 0, 0, &status);
if (realname != nullptr)
bt += realname;
free(realname);
}
#endif
bt += "\n";
}
fprintf(file, "%s", bt.c_str());
free(symbols);
}
#else // BACKTRACE_SUPPORTED
void write_stack_trace(FILE* file, int ignore_count)
{
const char* msg = "Unable to get stack trace on this platform.\n";
fprintf(stderr, "%s", msg);
fprintf(file, "%s", msg);
}
#endif
void call_gdb(FILE *file)
{
#ifndef TARGET_OS_WINDOWS
if (crawl_state.no_gdb)
return (void)fprintf(file, "%s\n", crawl_state.no_gdb);
fprintf(file, "Trying to run gdb.\n");
fflush(file); // so we can use fileno()
char attach_cmd[20] = {};
snprintf(attach_cmd, sizeof(attach_cmd), "attach %d", getpid());
#ifdef TARGET_OS_LINUX
prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
#endif
switch (int gdb = fork())
{
case -1:
return (void)fprintf(file, "Couldn't fork: %s\n", strerror(errno));
case 0:
{
int fd = fileno(file);
dup2(fd, 1);
dup2(fd, 2);
close(fd);
const char* argv[] =
{
"gdb",
"-batch",
"-ex", "show version", // Too bad -iex needs gdb >=7.5 (jessie)
"-ex", attach_cmd,
"-ex", "bt full",
0
};
execv("/usr/bin/gdb", (char* const*)argv);
printf("Failed to start gdb: %s\n", strerror(errno));
fflush(stdout);
_exit(0);
}
return;
default:
waitpid(gdb, 0, 0);
}
#endif
}
void disable_other_crashes()
{
// If one thread calls end() without going through a crash (a handled
// fatal error), no one else should be allowed to crash. We're already
// going down so blocking the other thread is ok.
#ifdef USE_UNIX_SIGNALS
mutex_lock(crash_mutex);
#endif
}
void watchdog()
{
#ifdef UNIX
struct itimerval t;
t.it_interval.tv_sec = 0;
t.it_interval.tv_usec = 0;
t.it_value.tv_sec = 60;
t.it_value.tv_usec = 0;
setitimer(ITIMER_VIRTUAL, &t, 0);
#else
// Real time rather than CPU time.
// This will break DGL, but it makes no sense on Windows anyway.
// Mapstat is cool with this.
alarm(60);
#endif
}