Skip to content

Commit

Permalink
Add support for printing backtraces on segfault
Browse files Browse the repository at this point in the history
Enabled in debug mode (make all-debug).
  • Loading branch information
jonas committed Aug 2, 2017
1 parent c3d183b commit 8c0a7c0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ m4_pattern_forbid([^AX_])

AC_PROG_CC

AC_CHECK_HEADERS([stdint.h stdlib.h string.h sys/time.h unistd.h wordexp.h])
AC_CHECK_HEADERS([execinfo.h stdint.h stdlib.h string.h sys/time.h unistd.h wordexp.h])
AC_CHECK_FUNCS([gettimeofday])
AC_CHECK_DECLS([environ])
AC_CHECK_DECLS([errno], [], [], [#include <errno.h>])
Expand Down
1 change: 1 addition & 0 deletions contrib/config.make-Darwin
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Work-around for Homebrew-based xmlto.
export XML_CATALOG_FILES=/usr/local/etc/xml/catalog

TIG_CFLAGS += -DHAVE_EXECINFO_H
TIG_LDLIBS = -liconv

NCURSES_DIR = $(wildcard /usr/local/opt/ncurses)
Expand Down
4 changes: 4 additions & 0 deletions include/tig/tig.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
#include <langinfo.h>
#include <iconv.h>

#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif

/* ncurses(3): Must be defined to have extended wide-character functions. */
#define _XOPEN_SOURCE_EXTENDED 1

Expand Down
2 changes: 2 additions & 0 deletions include/tig/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,7 @@ name(type **mem, size_t size, size_t increase) \
return tmp; \
}

void sigsegv_handler(int sig);

#endif
/* vim: set ts=8 sw=8 noexpandtab: */
42 changes: 42 additions & 0 deletions src/tig.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,43 @@ handle_mouse_event(void)
}
#endif

/*
* Error handling.
*
* Inspired by code from src/util.c in ELinks
* (f86be659718c0cd0a67f88b42f07044c23d0d028).
*/

#ifdef DEBUG
void
sigsegv_handler(int sig)
{
if (die_callback)
die_callback();

fputs("Tig crashed!\n\n"
"Please report this issue along with all info printed below to\n\n"
" https://github.com/jonas/tig/issues/new\n\n", stderr);

fputs("Tig version: ", stderr);
fputs(TIG_VERSION, stderr);
fputs("\n\n", stderr);

#ifdef HAVE_EXECINFO_H
{
/* glibc way of doing this */
void *stack[20];
size_t size = backtrace(stack, 20);

backtrace_symbols_fd(stack, size, STDERR_FILENO);
}
#endif

/* The fastest way OUT! */
abort();
}
#endif

struct key_combo {
enum request request;
struct keymap *keymap;
Expand Down Expand Up @@ -690,6 +727,11 @@ main(int argc, const char *argv[])
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
die("Failed to setup signal handler");

#ifdef DEBUG
if (signal(SIGSEGV, sigsegv_handler) == SIG_ERR)
die("Failed to setup signal handler");
#endif

if (setlocale(LC_ALL, "")) {
codeset = nl_langinfo(CODESET);
}
Expand Down

0 comments on commit 8c0a7c0

Please sign in to comment.