Skip to contentSkip to conversation

btrfs-progs: Show backtrace on BUGs

RFC PATCH In review Naohiro Aota 2 participants 2 messages Aug 21, 2014 – Sep 2, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 btrfs-progs: Show backtrace on BUGs

"btrfs check" is still under heavy development and so there are some BUGs beging hit. "btrfs check" can be run on limited environment which lacks gdb to debug the abort in detail. If we could see backtrace, it will be easier to find a root cause of the BUG.

Following is my "btrfs check" output with and without the patch.

without patch:

ref mismatch on [1437411180544 16384] extent item 3, found 2
btrfs: extent_io.c:612: free_extent_buffer: Assertion `!(eb->flags & 1)' failed. enabling repair mode
Checking filesystem on /dev/sdb2
UUID: a53121ee-679f-4241-bb44-ceb5a1a7beb7

with patch:

ref mismatch on [1437411180544 16384] extent item 3, found 2
btrfs check(free_extent_buffer+0xa3)[0x808ff89]
btrfs check[0x8090222]
btrfs check(alloc_extent_buffer+0xa7)[0x80903d0]
btrfs check(btrfs_find_create_tree_block+0x2e)[0x807edef]
btrfs check(btrfs_alloc_free_block+0x299)[0x808a3c4]
btrfs check(__btrfs_cow_block+0x1d4)[0x8078896]
btrfs check(btrfs_cow_block+0x150)[0x807934d]
btrfs check(btrfs_search_slot+0x136)[0x807c041]
btrfs check[0x8065a6b]
btrfs check[0x806bc0f]
btrfs check(cmd_check+0xc8f)[0x806d03a]
btrfs check(main+0x167)[0x804f5ec] /lib/libc.so.6(__libc_start_main+0xe6)[0xf754c4f6]
btrfs check[0x804f061]
btrfsck: extent_io.c:612: free_extent_buffer: Assertion `!(eb->flags & 1)' failed. enabling repair mode
Checking filesystem on /dev/sdb2
UUID: a53121ee-679f-4241-bb44-ceb5a1a7beb7

Now it's much clear that there's something wrong around alloc_extent_buffer.

---
 Makefile     |  2 +-
 kerncompat.h | 29 ++++++++++++++++++++++++++---
 2 files changed, 27 insertions(+), 4 deletions(-)
Makefile +1 -1
index 76565e8..9db5441 100644
@@ -5,7 +5,7 @@ CC = gcc
 LN = ln
 AR = ar
 AM_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES -fno-strict-aliasing -fPIC
-CFLAGS = -g -O1 -fno-strict-aliasing
+CFLAGS = -g -O1 -fno-strict-aliasing -rdynamic
 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
 	  root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
 	  extent-cache.o extent_io.o volumes.o utils.o repair.o \
kerncompat.h +26 -3
index f370cd8..94fadc8 100644
@@ -27,6 +27,7 @@
 #include <byteswap.h>
 #include <assert.h>
 #include <stddef.h>
+#include <execinfo.h>
 #include <linux/types.h>
 
 #ifndef READ
@@ -50,7 +51,18 @@
 #define ULONG_MAX       (~0UL)
 #endif
 
-#define BUG() assert(0)
+#define MAX_BACKTRACE 128
+#define show_trace()						\
+	do {							\
+		void *trace[MAX_BACKTRACE];			\
+		int n = backtrace(trace, MAX_BACKTRACE);	\
+		backtrace_symbols_fd(trace, n, 2);		\
+	} while(0)
+#define BUG()					\
+	do {					\
+		show_trace();			\
+		assert(0);			\
+	} while(0)
 #ifdef __CHECKER__
 #define __force    __attribute__((force))
 #define __bitwise__ __attribute__((bitwise))
@@ -233,8 +245,19 @@ 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 BUG_ON(c)				\
+	do {					\
+		if((c)) {			\
+			show_trace();		\
+			assert(!(c));		\
+		}				\
+	} while(0)
+#define WARN_ON(c) do {				\
+		if((c)) {			\
+			show_trace();		\
+			assert(!(c));		\
+		}				\
+	} while(0)
 
 
 #define container_of(ptr, type, member) ({                      \
Signature · 1 line
2.0.4
#1 lore raw
Quoted 4 lines · Naohiro Aota

"btrfs check" is still under heavy development and so there are some BUGs beging hit. "btrfs check" can be run on limited environment which lacks gdb to debug the abort in detail. If we could see backtrace, it will be easier to find a root cause of the BUG.

I like adding the backtraces, I'm only concerned about switching it on by default. Please add a compile-time option to use the backtrace enhanced macros or fallback to what's there right now. Also we'll probably need a makefile switch to enable it, possibly with more debugging options. The expected user of the backtraces are developers, the end users should not see the bug_ons at all, in the long term.