Skip to content

Commit

Permalink
Print a stack trace when we abort()
Browse files Browse the repository at this point in the history
Should make it a little easier to figure out where a crash came from.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
  • Loading branch information
Mark Fasheh committed Nov 15, 2014
1 parent 540ab9e commit 56018f5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ glib_CFLAGS=$(shell pkg-config --cflags glib-2.0)
glib_LIBS=$(shell pkg-config --libs glib-2.0)

override CFLAGS += -D_FILE_OFFSET_BITS=64 -DVERSTRING=\"$(RELEASE)\" \
$(crypt_CFLAGS) $(glib_CFLAGS)
$(crypt_CFLAGS) $(glib_CFLAGS) -rdynamic
LIBRARY_FLAGS += $(crypt_LIBS) $(glib_LIBS)

objects = duperemove.o rbtree.o hash-tree.o results-tree.o dedupe.o filerec.o util.o serialize.o btrfs-util.o $(hash_obj)
Expand Down
5 changes: 4 additions & 1 deletion debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ void print_mem_stats(void);

#define dprintf(args...) if (debug) printf(args)
#define vprintf(args...) if (verbose) printf(args)
void print_stack_trace(void);/* defined in util.c */
#define abort_lineno() do { \
printf("ERROR: %s:%d\n", __FILE__, __LINE__); \
print_stack_trace(); \
abort(); \
} while (0)

#define abort_on(condition) do { \
if (condition) { \
printf("ERROR: %s:%d\n", __FILE__, __LINE__);\
printf("ERROR: %s:%d\n", __FILE__, __LINE__); \
print_stack_trace(); \
abort(); \
} \
} while(0)
Expand Down
15 changes: 15 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdint.h>
#include <ctype.h>
#include <inttypes.h>
#include <execinfo.h>

#include "debug.h"
#include "util.h"
Expand Down Expand Up @@ -127,3 +128,17 @@ void print_mem_stats(void)
show_allocs_filerec();
show_allocs_filerec_token();
}

void print_stack_trace(void)
{
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;

trace_size = backtrace(trace, 16);
messages = backtrace_symbols(trace, trace_size);
printf("[stack trace follows]\n");
for (i=0; i < trace_size; i++)
printf("%s\n", messages[i]);
free(messages);
}

0 comments on commit 56018f5

Please sign in to comment.