From 9db4cea23576d9735c906829d4c4d2feb7720bf0 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 26 Apr 2012 16:04:53 +0200 Subject: [PATCH] Produce the stack trace in an async safe way. --- src/debug.c | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/debug.c b/src/debug.c index 8d51e959..42a73883 100644 --- a/src/debug.c +++ b/src/debug.c @@ -7,6 +7,7 @@ #ifdef HAVE_BACKTRACE #include #include +#include #endif /* HAVE_BACKTRACE */ /* ================================= Debugging ============================== */ @@ -567,27 +568,30 @@ void logRegisters(ucontext_t *uc) { #endif } -/* Logs the stack trace using the backtrace() call. */ -sds getStackTrace(ucontext_t *uc) { +/* Logs the stack trace using the backtrace() call. This function is designed + * to be called from signal handlers safely. */ +void logStackTrace(ucontext_t *uc) { void *trace[100]; - int i, trace_size = 0; - char **messages = NULL; - sds st = sdsempty(); + int trace_size = 0, fd; + + /* Open the log file in append mode. */ + fd = server.logfile ? + open(server.logfile, O_APPEND|O_CREAT|O_WRONLY, 0644) : + STDOUT_FILENO; + if (fd == -1) return; /* Generate the stack trace */ trace_size = backtrace(trace, 100); /* overwrite sigaction with caller's address */ - if (getMcontextEip(uc) != NULL) { + if (getMcontextEip(uc) != NULL) trace[1] = getMcontextEip(uc); - } - messages = backtrace_symbols(trace, trace_size); - for (i=1; i