diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index c5c76dddd..26c4c4df9 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -211,7 +211,7 @@ int main(int argc, char *argv[]) newdir = handle_opts(ld, argc, argv); /* Activate crash log now we're in the right place. */ - crashlog_activate(ld->log); + crashlog_activate(argv[0], ld->log); /* Ignore SIGPIPE: we look at our write return values*/ signal(SIGPIPE, SIG_IGN); diff --git a/lightningd/log.c b/lightningd/log.c index 7585b0f7d..0b42ed3fc 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -1,4 +1,5 @@ #include "log.h" +#include #include #include #include @@ -17,6 +18,8 @@ #include #include +static struct backtrace_state *backtrace_state; + struct log_entry { struct list_node list; struct timeabs time; @@ -411,6 +414,15 @@ void opt_register_logging(struct log *log) "log to file instead of stdout"); } +static int log_backtrace(void *log, uintptr_t pc, + const char *filename, int lineno, + const char *function) +{ + log_broken(log, "backtrace: %s:%u (%s) %p", + filename, lineno, function, (void *)pc); + return 0; +} + static struct log *crashlog; /* FIXME: Dump peer logs! */ @@ -419,8 +431,9 @@ static void log_crash(int sig) const char *logfile = NULL; if (sig) { - /* FIXME: Backtrace! */ log_broken(crashlog, "FATAL SIGNAL %i RECEIVED", sig); + backtrace_full(backtrace_state, 0, log_backtrace, NULL, + crashlog); } if (crashlog->lr->print == log_default_print) { @@ -442,18 +455,21 @@ static void log_crash(int sig) logfile = NULL; } - if (sig) + if (sig) { fprintf(stderr, "Fatal signal %u. ", sig); + backtrace_print(backtrace_state, 0, stderr); + } if (logfile) fprintf(stderr, "Log dumped in %s", logfile); fprintf(stderr, "\n"); } -void crashlog_activate(struct log *log) +void crashlog_activate(const char *argv0, struct log *log) { struct sigaction sa; crashlog = log; + backtrace_state = backtrace_create_state(argv0, 0, NULL, NULL); sa.sa_handler = log_crash; sigemptyset(&sa.sa_mask); /* We want to fall through to default handler */ diff --git a/lightningd/log.h b/lightningd/log.h index 21eb95fac..a825b3aad 100644 --- a/lightningd/log.h +++ b/lightningd/log.h @@ -87,7 +87,7 @@ void log_each_line_(const struct log_book *lr, void log_dump_to_file(int fd, const struct log_book *lr); void opt_register_logging(struct log *log); -void crashlog_activate(struct log *log); +void crashlog_activate(const char *argv0, struct log *log); /* Convenience parent for temporary allocations (eg. type_to_string) * during log calls; freed after every log_*() */