-
Notifications
You must be signed in to change notification settings - Fork 5
/
ngx_backtrace_module.c
372 lines (296 loc) · 10.3 KB
/
ngx_backtrace_module.c
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
/*
* Copyright (C) 2016 Jorge Pereira <jpereiran@gmail.com>
*/
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <memory.h>
#include <libunwind.h> /* from -llibuwind */
#if defined(REG_RIP)
# define SIGSEGV_STACK_IA64
# define REGFORMAT "%016lx"
#elif defined(REG_EIP)
# define SIGSEGV_STACK_X86
# define REGFORMAT "%08x"
#else
# define SIGSEGV_STACK_GENERIC
# define REGFORMAT "%x"
#endif
#define NGX_BACKTRACE_DEFAULT_STACK_MAX_SIZE 30
static char *ngx_backtrace_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void ngx_error_signal_handler(int signo, siginfo_t *info, void *secret);
static ngx_int_t ngx_backtrace_init_module(ngx_cycle_t *cycle);
static void *ngx_backtrace_create_conf(ngx_cycle_t *cycle);
#if defined(nginx_version) && nginx_version >= 1005002
static ngx_log_t *ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name);
#endif
typedef struct {
int signo;
char *signame;
char *name;
void (*handler)(int sig, siginfo_t *info, void *secret);
} ngx_signal_t;
typedef struct {
ngx_log_t *log;
ngx_int_t max_stack_size;
} ngx_backtrace_conf_t;
static ngx_signal_t ngx_backtrace_signals[] = {
{ SIGABRT, "SIGABRT", "", ngx_error_signal_handler },
#ifdef SIGBUS
{ SIGBUS, "SIGBUS", "", ngx_error_signal_handler },
#endif
{ SIGFPE, "SIGFPE", "", ngx_error_signal_handler },
{ SIGILL, "SIGILL", "", ngx_error_signal_handler },
{ SIGIOT, "SIGIOT", "", ngx_error_signal_handler },
{ SIGSEGV, "SIGSEGV", "", ngx_error_signal_handler },
{ 0, NULL, "", NULL }
};
typedef struct {
int signo;
int si_code;
const char *si_code_desc;
} sig_action_map_t;
static sig_action_map_t ngx_backtrace_si_codes[] = {
{ SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR (Address not mapped to object)" },
{ SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR (Invalid permissions for mapped object)" },
{ SIGSEGV, -1, "Unknown reason" }
};
const char *ngx_si_code2desc(int signo, int si_code) {
sig_action_map_t *p;
for (p = &ngx_backtrace_si_codes[0]; p->signo != -1; p++) {
if (p->signo == signo) {
return (p->si_code == si_code) ? p->si_code_desc : "Unknown reason";
}
}
return NULL;
}
static ngx_command_t ngx_backtrace_commands[] = {
{ ngx_string("backtrace_log"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_backtrace_files,
0,
0,
NULL },
{ ngx_string("backtrace_max_stack_size"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_backtrace_conf_t, max_stack_size),
NULL },
ngx_null_command
};
static ngx_core_module_t ngx_backtrace_module_ctx = {
ngx_string("backtrace"),
ngx_backtrace_create_conf,
NULL
};
ngx_module_t ngx_backtrace_module = {
NGX_MODULE_V1,
&ngx_backtrace_module_ctx, /* module context */
ngx_backtrace_commands, /* module directives */
NGX_CORE_MODULE, /* module type */
NULL, /* init master */
ngx_backtrace_init_module, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
#if defined(nginx_version) && nginx_version >= 1005002
static ngx_log_t *
ngx_log_create (ngx_cycle_t *cycle, ngx_str_t *name)
{
ngx_log_t *log;
log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
if (log == NULL) {
return NULL;
}
log->file = ngx_conf_open_file(cycle, name);
if (log->file == NULL) {
return NULL;
}
return log;
}
#endif
const char *ngx_backtrace_get_proc_exe (pid_t pid) {
char proc_pid[64];
static char proc_buf[64];
snprintf(proc_pid, sizeof(proc_buf), "/proc/%d/exe", pid);
if (readlink(proc_pid, proc_buf, sizeof(proc_buf)-1) < 1) {
perror("readlink");
return NULL;
}
return (const char *)&proc_buf[0];
}
static ngx_int_t
ngx_init_error_signals (ngx_log_t *log)
{
ngx_signal_t *sig;
struct sigaction sa;
for (sig = ngx_backtrace_signals; sig->signo != 0; sig++) {
ngx_memzero(&sa, sizeof(struct sigaction));
sa.sa_sigaction = sig->handler;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
if (sigaction(sig->signo, &sa, NULL) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
"ngx_backtrace_module: sigaction(%s) failed", sig->signame);
return NGX_ERROR;
}
}
return NGX_OK;
}
static void
ngx_error_signal_handler (int signo, siginfo_t *info, void *ptr) {
ngx_log_t *log;
ngx_signal_t *sig;
struct sigaction sa;
ngx_backtrace_conf_t *bcf;
int nptrs;
int ret;
int fd;
time_t crash_time;
const char *si_code_reason;
const char *proc_exe;
unw_cursor_t cursor;
unw_context_t uc;
bcf = (ngx_backtrace_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
ngx_backtrace_module);
log = bcf->log ? bcf->log : ngx_cycle->log;
for (sig = ngx_backtrace_signals; sig->signo != 0; sig++) {
if (sig->signo == signo) {
break;
}
}
if (sig == 0) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"ngx_backtrace_module: Wrong signal received from Kernel! Weird!!");
return;
}
ngx_log_error(NGX_LOG_ERR, log, 0,
"ngx_backtrace_module: Got signal %d (%s), Saving the stacktrace in %s",
signo, sig->signame, (char *)log->file->name.data
);
fd = log->file->fd;
if (fcntl(fd, F_GETFL) < 0) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"ngx_backtrace_module: We can't write into the file %s, exiting.",
(char *)log->file->name.data
);
goto bye;
}
dprintf(fd, "+-------------------------------------------------------+\n");
dprintf(fd, "| ngx_backtrace_module: Received signal %d (%s)\n", signo, sig->signame);
dprintf(fd, "+-------------------------------------------------------+\n");
crash_time = time(NULL);
dprintf(fd, " Date: %s", ctime(&crash_time));
dprintf(fd, " Faulty address: %p\n", info->si_addr);
dprintf(fd, " PID: %ld\n", (long)getpid());
dprintf(fd, " PPID: %ld\n", (long)getppid());
proc_exe = ngx_backtrace_get_proc_exe(getpid());
dprintf(fd, " Binary name: %s\n", proc_exe);
dprintf(fd, " Signal Code: %d\n", info->si_code);
si_code_reason = ngx_si_code2desc(signo, info->si_code);
dprintf(fd, " Signal Reason: %s\n", si_code_reason);
dprintf(fd, "+-------------------------------------------------------+\n");
ngx_memzero(&sa, sizeof(struct sigaction));
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
if (sigaction(signo, &sa, NULL) == -1) {
ngx_log_error(NGX_LOG_ERR, log, ngx_errno,
"ngx_backtrace_module: sigaction(%s) failed", sig->signame);
}
if (bcf->max_stack_size == NGX_CONF_UNSET) {
bcf->max_stack_size = NGX_BACKTRACE_DEFAULT_STACK_MAX_SIZE;
}
dprintf(fd, "Stack trace:\n");
ret = unw_getcontext(&uc);
if (ret != UNW_ESUCCESS) {
ngx_log_error(NGX_LOG_ERR, log, ngx_errno,
"ngx_backtrace_module: Problems with unw_getcontext() ret=%d", ret);
goto invalid;
}
ret = unw_init_local (&cursor, &uc);
if (ret != 0) {
dprintf(fd, "Problems with unw_init_local() failed: ret=%d\n", ret);
goto invalid;
}
for (nptrs = 0; unw_step(&cursor) > 0; nptrs++) {
char fname[128] = { '\0', };
unw_word_t ip, sp, offp;
unw_get_proc_name (&cursor, fname, sizeof(fname), &offp);
ret = unw_get_reg (&cursor, UNW_REG_IP, &ip);
if (ret != 0) {
dprintf(fd, "Problems with unw_get_reg(UNW_REG_IP) failed: ret=%d\n", ret);
goto invalid;
}
ret = unw_get_reg (&cursor, UNW_REG_SP, &sp);
if (ret != 0) {
dprintf(fd, "Problems with unw_get_reg(UNW_REG_SP) failed: ret=%d\n", ret);
goto invalid;
}
if (!strcmp(fname, "__restore_rt")) continue;
if (!strcmp(fname, "__libc_start_main")) break;
dprintf(fd, "\t#%02d: 0x"REGFORMAT" in %s(), sp = 0x"REGFORMAT"\n",
nptrs, (long) ip, fname[0] ? fname : "??", (long) sp);
}
dprintf(fd, "End of stack trace.\n\n");
fsync(fd);
bye:
return;
invalid:
kill(ngx_getpid(), signo);
}
static char *
ngx_backtrace_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t file, *value;
ngx_log_t *log;
ngx_backtrace_conf_t *bcf;
bcf = (ngx_backtrace_conf_t *) ngx_get_conf(cf->cycle->conf_ctx,
ngx_backtrace_module);
value = cf->args->elts;
file = value[1];
ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
"ngx_backtrace_module: Initializing the module saving in %s", file.data);
if (ngx_conf_full_name(cf->cycle, &file, 1) != NGX_OK) {
return NGX_CONF_ERROR;
}
log = ngx_log_create(cf->cycle, &file);
if (log == NULL) {
return NGX_CONF_ERROR;
}
bcf->log = log;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_backtrace_init_module(ngx_cycle_t *cycle)
{
ngx_backtrace_conf_t *bcf;
bcf = (ngx_backtrace_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_backtrace_module);
if (!bcf->log) {
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "ngx_backtrace_module: The module is not in use");
return NGX_OK;
}
if (ngx_init_error_signals(cycle->log) == NGX_ERROR) {
return NGX_ERROR;
}
return NGX_OK;
}
static void *
ngx_backtrace_create_conf(ngx_cycle_t *cycle)
{
ngx_backtrace_conf_t *bcf;
bcf = ngx_pcalloc(cycle->pool, sizeof(ngx_backtrace_conf_t));
if (bcf == NULL) {
return NULL;
}
bcf->max_stack_size = NGX_CONF_UNSET;
return bcf;
}