From 6a951aa9f705cf587370000c3b1c8e3e041112ba Mon Sep 17 00:00:00 2001 From: Nicolas Viennot Date: Wed, 6 Nov 2019 16:43:24 -0500 Subject: [PATCH] Improve debugging traces --- configure.ac | 2 ++ tmate-debug.c | 46 ++++++++++++++++++++++++++++++++++++++-------- tmate-main.c | 1 + tmate.h | 1 + 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index 856c588b..5262d670 100644 --- a/configure.ac +++ b/configure.ac @@ -100,6 +100,7 @@ AC_CHECK_HEADERS( inttypes.h \ libutil.h \ ncurses.h \ + execinfo.h \ ndir.h \ paths.h \ pty.h \ @@ -121,6 +122,7 @@ AC_CHECK_FUNCS( [ \ dirfd \ flock \ + backtrace \ setproctitle \ sysconf \ cfmakeraw \ diff --git a/tmate-debug.c b/tmate-debug.c index 6bf74b0a..2a2b660d 100644 --- a/tmate-debug.c +++ b/tmate-debug.c @@ -1,9 +1,20 @@ +#ifdef HAVE_EXECINFO_H #include +#endif #include #include #include +#include #include "tmate.h" +#ifndef HAVE_BACKTRACE + +void tmate_print_stack_trace(void) {} +void tmate_catch_sigsegv(void) {} +void tmate_preload_trace_lib(void) {} + +#else + #if DEBUG static int print_resolved_stack_frame(const char *frame) @@ -47,7 +58,7 @@ static int print_resolved_stack_frame(const char *frame) return -1; line[strlen(line)-1] = 0; /* remove \n */ - tmate_crit("%s(%s) [%s]", file, line, address); + tmate_debug("%s(%s) [%s]", file, line, address); return 0; } #endif @@ -59,23 +70,42 @@ void tmate_print_stack_trace(void) char **strings; size_t i; - size = backtrace(array, 20); - strings = backtrace_symbols(array, size); + size = backtrace (array, 20); + strings = backtrace_symbols (array, size); - tmate_crit("============ %zd stack frames ============", size); + tmate_info ("============ %zd stack frames ============", size); for (i = 1; i < size; i++) { #if DEBUG if (print_resolved_stack_frame(strings[i]) < 0) #endif - tmate_crit("%s", strings[i]); + tmate_info("%s", strings[i]); } - free(strings); + free (strings); +} + +static void handle_crash(__unused int sig) +{ + /* + * XXX we are in a signal handler, we are not allowed to use malloc + * and friends, which is what we do + */ + tmate_info("CRASH, printing stack trace"); + tmate_print_stack_trace(); + tmate_fatal("CRASHED"); +} + +void tmate_catch_sigsegv(void) +{ + signal(SIGSEGV, handle_crash); + signal(SIGABRT, handle_crash); } void tmate_preload_trace_lib(void) { - void *array[1]; - backtrace(array, 1); + void *array[1]; + backtrace(array, 1); } + +#endif diff --git a/tmate-main.c b/tmate-main.c index 55ab7319..eca4a6db 100644 --- a/tmate-main.c +++ b/tmate-main.c @@ -213,6 +213,7 @@ int main(int argc, char **argv, char **envp) cmdline_end = *envp; tmate_preload_trace_lib(); + tmate_catch_sigsegv(); if ((dev_urandom_fd = open("/dev/urandom", O_RDONLY)) < 0) tmate_fatal("Cannot open /dev/urandom"); diff --git a/tmate.h b/tmate.h index 5fbd65da..fbcc9ebe 100644 --- a/tmate.h +++ b/tmate.h @@ -282,6 +282,7 @@ static inline bool tmate_has_websocket(void) extern void tmate_preload_trace_lib(void); extern void tmate_print_stack_trace(void); +extern void tmate_catch_sigsegv(void); /* tmux.c */