path: root/kerncompat.h
diff options
authorJosef Bacik <jbacik@fb.com>2014-09-25 16:07:10 -0400
committerDavid Sterba <dsterba@suse.cz>2014-10-01 16:34:29 +0200
commit3ae0209d94776faaa139fcebfafa22fd7840b9f4 (patch)
treeaff0995e2e6bb3614f1a40e4bf1648ede9c341b0 /kerncompat.h
parentafc850dd13e672db63f6ba1faab9d4da6182f5b8 (diff)
Btrfs-progs: make BUG*() be more verbose
Currently these macros just tie to assert(), which gives us line number and such but no backtrace so no actual context. This patch adds support for spitting out a backtrace so we can see how we got to the given assert. Thanks, Signed-off-by: Josef Bacik <jbacik@fb.com> [backtrace_symbols_fd] Signed-off-by: Naohiro Aota <naota@elisp.net> [minor fixups] Signed-off-by: David Sterba <dsterba@suse.cz>
Diffstat (limited to 'kerncompat.h')
1 files changed, 32 insertions, 4 deletions
diff --git a/kerncompat.h b/kerncompat.h
index bb03194..19c7fa5 100644
--- a/kerncompat.h
+++ b/kerncompat.h
@@ -29,6 +29,7 @@
#include <stddef.h>
#include <linux/types.h>
#include <stdint.h>
+#include <execinfo.h>
#define ptr_to_u64(x) ((u64)(uintptr_t)x)
#define u64_to_ptr(x) ((void *)(uintptr_t)x)
@@ -54,7 +55,33 @@
#define ULONG_MAX (~0UL)
-#define BUG() assert(0)
+#define MAX_BACKTRACE 16
+static inline void print_trace(void)
+ void *array[MAX_BACKTRACE];
+ size_t size;
+ size = backtrace(array, MAX_BACKTRACE);
+ backtrace_symbols_fd(array, size, 2);
+static inline void assert_trace(const char *assertion, const char *filename,
+ const char *func, unsigned line, int val)
+ if (val)
+ return;
+ if (assertion)
+ fprintf(stderr, "%s:%d: %s: Assertion `%s` failed.\n",
+ filename, line, func, assertion);
+ else
+ fprintf(stderr, "%s:%d: %s: Assertion failed.\n", filename,
+ line, func);
+ print_trace();
+ exit(1);
+#define BUG() assert_trace(NULL, __FILE__, __func__, __LINE__, 0)
#ifdef __CHECKER__
#define __force __attribute__((force))
#define __bitwise__ __attribute__((bitwise))
@@ -237,9 +264,10 @@ static inline long IS_ERR(const void *ptr)
#define kstrdup(x, y) strdup(x)
#define kfree(x) free(x)
-#define BUG_ON(c) assert(!(c))
-#define WARN_ON(c) assert(!(c))
-#define ASSERT(c) assert(c)
+#define BUG_ON(c) assert_trace(#c, __FILE__, __func__, __LINE__, !(c))
+#define WARN_ON(c) BUG_ON(c)
+#define ASSERT(c) assert_trace(#c, __FILE__, __func__, __LINE__, (c))
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \