Skip to content

Commit

Permalink
Final changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpereira committed Nov 29, 2016
1 parent 1f1247e commit 28cc0a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
4 changes: 2 additions & 2 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ fi
ngx_addon_name=ngx_backtrace_module
HTTP_MODULES="$HTTP_MODULES ngx_backtrace_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_backtrace_module.c"
CORE_LIBS="$CORE_LIBS -lunwind -lunwind-$lib_arch"
CFLAGS="$CFLAGS -fno-omit-frame-pointer -DNGINX_MODULE_BACKTRACE"
CORE_LIBS="$CORE_LIBS -rdynamic -fno-omit-frame-pointer -lunwind -lunwind-$lib_arch"
CFLAGS="$CFLAGS -rdynamic -fno-omit-frame-pointer"
49 changes: 26 additions & 23 deletions ngx_backtrace_module.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
* Copyright (C) 2016 Jorge Pereira <[email protected]>
*/
Expand Down Expand Up @@ -28,11 +27,13 @@

#define NGX_BACKTRACE_DEFAULT_STACK_MAX_SIZE 30

static char *ngx_backtrace_files(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
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_worker(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;
Expand All @@ -46,10 +47,6 @@ typedef struct {
ngx_int_t max_stack_size;
} ngx_backtrace_conf_t;

#if defined(nginx_version) && nginx_version >= 1005002
static ngx_log_t *ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name);
#endif

static ngx_signal_t ngx_backtrace_signals[] = {
{ SIGABRT, "SIGABRT", "", ngx_error_signal_handler },
#ifdef SIGBUS
Expand All @@ -68,7 +65,7 @@ typedef struct {
const char *si_code_desc;
} sig_action_map_t;

static sig_action_map_t ngx_backtrace_si_codes[] = {
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" }
Expand Down Expand Up @@ -106,7 +103,6 @@ static ngx_command_t ngx_backtrace_commands[] = {
ngx_null_command
};


static ngx_core_module_t ngx_backtrace_module_ctx = {
ngx_string("backtrace"),
ngx_backtrace_create_conf,
Expand All @@ -130,7 +126,7 @@ ngx_module_t ngx_backtrace_module = {

#if defined(nginx_version) && nginx_version >= 1005002
static ngx_log_t *
ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name)
ngx_log_create (ngx_cycle_t *cycle, ngx_str_t *name)
{
ngx_log_t *log;

Expand Down Expand Up @@ -163,7 +159,7 @@ const char *ngx_backtrace_get_proc_exe (pid_t pid) {
}

static ngx_int_t
ngx_init_error_signals(ngx_log_t *log)
ngx_init_error_signals (ngx_log_t *log)
{
ngx_signal_t *sig;
struct sigaction sa;
Expand All @@ -185,13 +181,14 @@ ngx_init_error_signals(ngx_log_t *log)
}

static void
ngx_error_signal_handler(int signo, siginfo_t *info, void *ptr) {
ngx_error_signal_handler (int signo, siginfo_t *info, void *ptr) {
void *buffer;
ngx_log_t *log;
ngx_signal_t *sig;
struct sigaction sa;
ngx_backtrace_conf_t *bcf;
int nptrs;
int ret;
time_t crash_time;
const char *si_code_reason;
const char *proc_exe;
Expand All @@ -210,14 +207,15 @@ ngx_error_signal_handler(int signo, siginfo_t *info, void *ptr) {
}

if (sig == 0) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "ngx_backtrace_module: Wrong signal received from Kernel! Weird!!");
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
"ngx_backtrace_module: Wrong signal received from Kernel! Weird!!");
return;
}

si_code_reason = ngx_si_code2desc(signo, info->si_code);

ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "ngx_backtrace_module: Got signal %d (%s), Saving the stacktrace in %s",
signo, sig->signame, (char *)log->file->name.data);
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
"ngx_backtrace_module: Got signal %d (%s), Saving the stacktrace in %s",
signo, sig->signame, (char *)log->file->name.data
);

dprintf(log->file->fd, "+-------------------------------------------------------+\n");
dprintf(log->file->fd, "| ngx_backtrace_module: Received signal %d (%s)\n", signo, sig->signame);
Expand All @@ -232,6 +230,8 @@ ngx_error_signal_handler(int signo, siginfo_t *info, void *ptr) {
proc_exe = ngx_backtrace_get_proc_exe(getpid());
dprintf(log->file->fd, " Binary name: %s\n", proc_exe);
dprintf(log->file->fd, " Signal Code: %d\n", info->si_code);

si_code_reason = ngx_si_code2desc(signo, info->si_code);
dprintf(log->file->fd, " Signal Reason: %s\n", si_code_reason);
dprintf(log->file->fd, "+-------------------------------------------------------+\n");

Expand All @@ -255,14 +255,18 @@ ngx_error_signal_handler(int signo, siginfo_t *info, void *ptr) {
dprintf(log->file->fd, "Stack trace:\n");

unw_getcontext (&uc);
unw_init_local (&cursor, &uc);
ret = unw_init_local (&cursor, &uc);
if (ret != 0) {
dprintf(log->file->fd, "Problems with unw_init_local() failed: ret=%d\n", ret);
goto invalid;
}

for (nptrs = 0; unw_step(&cursor) > 0; nptrs++) {
char fname[256] = { '\0', };
char fname[128] = { '\0', };
unw_word_t ip, sp, offp;
int ret;

unw_get_proc_name (&cursor, fname, 256, &offp);
unw_get_proc_name (&cursor, fname, sizeof(fname), &offp);

ret = unw_get_reg (&cursor, UNW_REG_IP, &ip);
if (ret != 0) {
dprintf(log->file->fd, "Problems with unw_get_reg(UNW_REG_IP) failed: ret=%d\n", ret);
Expand Down Expand Up @@ -291,8 +295,7 @@ ngx_error_signal_handler(int signo, siginfo_t *info, void *ptr) {
}

static char *
ngx_backtrace_files(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
ngx_backtrace_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t file, *value;
ngx_log_t *log;
Expand Down

0 comments on commit 28cc0a9

Please sign in to comment.