From 9b5d30ea9feb311d74857946dd9bb22aefb2f64a Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Fri, 22 Apr 2011 15:24:04 +0300 Subject: fs-tests: integck: print backtrace in case of failure This patch teaches the 'CHECK()' macro to print the back-trace in case of failure which is sometimes very useful for debugging. This patch also adds a helper function for the 'CHECK()' macro because otherwise it becomes too large. Note, since all functions in the test are static, the stack trace does not really show symbols. Signed-off-by: Artem Bityutskiy --- tests/fs-tests/integrity/integck.c | 39 ++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/fs-tests/integrity/integck.c b/tests/fs-tests/integrity/integck.c index 522c6de..9822b3b 100644 --- a/tests/fs-tests/integrity/integck.c +++ b/tests/fs-tests/integrity/integck.c @@ -17,7 +17,6 @@ * * Author: Adrian Hunter */ - #include #include #include @@ -32,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -54,15 +54,9 @@ */ #define stringify1(x) #x #define stringify(x) stringify1(x) -#define CHECK(cond) do { \ - if (!(cond)) { \ - int _err = errno; \ - fflush(stdout); \ - errmsg("condition '%s' failed at %s:%d", \ - stringify(cond), __FILE__, __LINE__); \ - errmsg("error %d (%s)", _err, strerror(_err)); \ - exit(EXIT_FAILURE); \ - } \ +#define CHECK(cond) do { \ + if (!(cond)) \ + check_failed(stringify(cond), __func__, __FILE__, __LINE__); \ } while(0) #define pcv(fmt, ...) do { \ @@ -214,6 +208,31 @@ static unsigned int check_run_no; */ static char *random_name_buf; +/* + * This is a helper for the 'CHECK()' macro - prints a scary error message and + * terminates the program. + */ +static void check_failed(const char *cond, const char *func, const char *file, + int line) +{ + int error = errno, count; + void *addresses[128]; + + fflush(stdout); + errmsg("condition '%s' failed in %s() at %s:%d", + cond, func, __FILE__, __LINE__); + normsg("error %d (%s)", error, strerror(error)); + /* + * Note, to make this work well you need: + * 1. Make all functions non-static - add "#define static' + * 2. Compile with -rdynamic and -g gcc options + * 3. Preferrably compile with -O0 to avoid inlining + */ + count = backtrace(addresses, 128); + backtrace_symbols_fd(addresses, count, fileno(stdout)); + exit(EXIT_FAILURE); +} + /* * Is this 'struct write_info' actually holds information about a truncation? */ -- cgit v1.2.3