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
|
/*
* File: crash-u.cc
* Summary: UNIX specific crash handling functions.
* Written by: Matthew Cline
*
* Modified for Crawl Reference by $Author$ on $Date$
*/
#include "AppHdr.h"
REVISION("$Rev$");
#ifdef USE_UNIX_SIGNALS
#include <signal.h>
#endif
#ifdef __GLIBC__
#include <execinfo.h>
#endif
#include "crash.h"
#include "externs.h"
#include "state.h"
#include "initfile.h"
/////////////////////////////////////////////////////////////////////////////
// Code for printing out debugging info on a crash.
////////////////////////////////////////////////////////////////////////////
static int _crash_signal = 0;
static int _recursion_depth = 0;
static void _crash_signal_handler(int sig_num)
{
if (crawl_state.game_crashed)
{
if (_recursion_depth > 0)
return;
_recursion_depth++;
fprintf(stderr, "Recursive crash." EOL);
std::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];
sprintf(name, "%scrash-recursive-%s-%d.txt", dir.c_str(),
you.your_name, (int) time(NULL));
FILE* file = fopen(name, "w");
if (file == NULL)
file = stderr;
write_stack_trace(file, 0);
if (file != stderr)
fclose(file);
return;
}
_crash_signal = sig_num;
crawl_state.game_crashed = true;
// In case the crash dumper is unable to open a file and has to dump
// to stderr.
#ifndef USE_TILE
unixcurses_shutdown();
#endif
do_crash_dump();
// Now crash for real.
signal(sig_num, SIG_DFL);
raise(sig_num);
}
void init_crash_handler()
{
#if defined(USE_UNIX_SIGNALS)
for (int i = 1; i <= 64; i++)
{
#ifdef SIGHUP_SAVE
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
if (i == SIGWINCH)
continue;
signal(i, _crash_signal_handler);
}
#endif // if defined(USE_UNIX_SIGNALS)
}
void dump_crash_info(FILE* file)
{
const char *name = strsignal(_crash_signal);
if (name == NULL)
name = "INVALID";
fprintf(file, "Crash caused by signal #%d: %s" EOL EOL, _crash_signal,
name);
}
#ifdef __GLIBC__
// NOTE: This should work on OS X, according to
// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/ManPages/man3/backtrace_symbols.3.html
void write_stack_trace(FILE* file, int ignore_count)
{
void* frames[50];
int num_frames = backtrace(frames, ARRAYSZ(frames));
char **symbols = backtrace_symbols(frames, num_frames);
if (symbols == NULL)
{
fprintf(stderr, "Out of memory." EOL);
fprintf(file, "Out of memory." EOL);
// 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;
}
for (int i = ignore_count; i < num_frames; i++)
{
fprintf(file, "%s" EOL, symbols[i]);
}
free(symbols);
}
#else // ifdef __GLIBC__
void write_stack_trace(FILE* file, int ignore_count)
{
const char* msg = "Unable to get stack trace on this platform." EOL;
fprintf(stderr, msg);
fprintf(file, msg);
}
#endif
|