1
0
mirror of https://github.com/tmate-io/tmate-ssh-server.git synced 2020-11-18 19:53:51 -08:00

Improve debugging traces

This commit is contained in:
Nicolas Viennot 2019-11-06 16:43:24 -05:00
parent 975ba3323b
commit 6a951aa9f7
4 changed files with 42 additions and 8 deletions

View File

@ -100,6 +100,7 @@ AC_CHECK_HEADERS(
inttypes.h \ inttypes.h \
libutil.h \ libutil.h \
ncurses.h \ ncurses.h \
execinfo.h \
ndir.h \ ndir.h \
paths.h \ paths.h \
pty.h \ pty.h \
@ -121,6 +122,7 @@ AC_CHECK_FUNCS(
[ \ [ \
dirfd \ dirfd \
flock \ flock \
backtrace \
setproctitle \ setproctitle \
sysconf \ sysconf \
cfmakeraw \ cfmakeraw \

View File

@ -1,9 +1,20 @@
#ifdef HAVE_EXECINFO_H
#include <execinfo.h> #include <execinfo.h>
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <regex.h> #include <regex.h>
#include <signal.h>
#include "tmate.h" #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 #if DEBUG
static int print_resolved_stack_frame(const char *frame) static int print_resolved_stack_frame(const char *frame)
@ -47,7 +58,7 @@ static int print_resolved_stack_frame(const char *frame)
return -1; return -1;
line[strlen(line)-1] = 0; /* remove \n */ 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; return 0;
} }
#endif #endif
@ -59,23 +70,42 @@ void tmate_print_stack_trace(void)
char **strings; char **strings;
size_t i; size_t i;
size = backtrace(array, 20); size = backtrace (array, 20);
strings = backtrace_symbols(array, size); strings = backtrace_symbols (array, size);
tmate_crit("============ %zd stack frames ============", size); tmate_info ("============ %zd stack frames ============", size);
for (i = 1; i < size; i++) { for (i = 1; i < size; i++) {
#if DEBUG #if DEBUG
if (print_resolved_stack_frame(strings[i]) < 0) if (print_resolved_stack_frame(strings[i]) < 0)
#endif #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 tmate_preload_trace_lib(void)
{ {
void *array[1]; void *array[1];
backtrace(array, 1); backtrace(array, 1);
} }
#endif

View File

@ -213,6 +213,7 @@ int main(int argc, char **argv, char **envp)
cmdline_end = *envp; cmdline_end = *envp;
tmate_preload_trace_lib(); tmate_preload_trace_lib();
tmate_catch_sigsegv();
if ((dev_urandom_fd = open("/dev/urandom", O_RDONLY)) < 0) if ((dev_urandom_fd = open("/dev/urandom", O_RDONLY)) < 0)
tmate_fatal("Cannot open /dev/urandom"); tmate_fatal("Cannot open /dev/urandom");

View File

@ -282,6 +282,7 @@ static inline bool tmate_has_websocket(void)
extern void tmate_preload_trace_lib(void); extern void tmate_preload_trace_lib(void);
extern void tmate_print_stack_trace(void); extern void tmate_print_stack_trace(void);
extern void tmate_catch_sigsegv(void);
/* tmux.c */ /* tmux.c */