linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Btrfs-progs: make BUG*() be more verbose
@ 2014-09-25 20:07 Josef Bacik
  2014-09-30  9:43 ` David Sterba
  0 siblings, 1 reply; 2+ messages in thread
From: Josef Bacik @ 2014-09-25 20:07 UTC (permalink / raw)
  To: linux-btrfs

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>
---
 Makefile     |  2 +-
 kerncompat.h | 40 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index 18eb944..7cc7783 100644
--- a/Makefile
+++ b/Makefile
@@ -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 \
diff --git a/kerncompat.h b/kerncompat.h
index bb03194..669dd8d 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,37 @@
 #define ULONG_MAX       (~0UL)
 #endif
 
-#define BUG() assert(0)
+static inline void print_trace(void)
+{
+	void *array[10];
+	size_t size;
+	char **strings;
+	size_t i;
+
+	size = backtrace(array, 10);
+	strings = backtrace_symbols(array, size);
+	for (i = 0; i < size; i++)
+		fprintf(stderr, "\t%s\n", strings[i]);
+	free(strings);
+}
+
+static inline void btr_assert(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() btr_assert(NULL, __FILE__, __func__, __LINE__, 0)
+
 #ifdef __CHECKER__
 #define __force    __attribute__((force))
 #define __bitwise__ __attribute__((bitwise))
@@ -237,9 +268,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) btr_assert(#c, __FILE__, __func__, __LINE__, !(c))
+
+#define WARN_ON(c) BUG_ON(c)
+#define	ASSERT(c) btr_assert(#c, __FILE__, __func__, __LINE__, (c))
 
 #define container_of(ptr, type, member) ({                      \
         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Btrfs-progs: make BUG*() be more verbose
  2014-09-25 20:07 [PATCH] Btrfs-progs: make BUG*() be more verbose Josef Bacik
@ 2014-09-30  9:43 ` David Sterba
  0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2014-09-30  9:43 UTC (permalink / raw)
  To: Josef Bacik; +Cc: linux-btrfs, naota

On Thu, Sep 25, 2014 at 04:07:10PM -0400, Josef Bacik wrote:
> 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,

https://patchwork.kernel.org/patch/4757041/

There are differences and I find Josef's implementation better in some
respects and Naota's in others. Below is what I'm going to apply with
sign-offs from both of you.

Notable change is the use of backtrace_symbols_fd, it does not
malloc/free the memory, though the output is slighly different from
backtrace_symbols, still readable.

--- a/kerncompat.h
+++ b/kerncompat.h
@@ -55,21 +55,17 @@
 #define ULONG_MAX       (~0UL)
 #endif

+#define MAX_BACKTRACE  16
 static inline void print_trace(void)
 {
-       void *array[10];
+       void *array[MAX_BACKTRACE];
        size_t size;
-       char **strings;
-       size_t i;
-
-       size = backtrace(array, 10);
-       strings = backtrace_symbols(array, size);
-       for (i = 0; i < size; i++)
-               fprintf(stderr, "\t%s\n", strings[i]);
-       free(strings);
+
+       size = backtrace(array, MAX_BACKTRACE);
+       backtrace_symbols_fd(array, size, 2);
 }

-static inline void btr_assert(const char *assertion, const char *filename,
+static inline void assert_trace(const char *assertion, const char *filename,
                              const char *func, unsigned line, int val)
 {
        if (val)
@@ -84,7 +80,7 @@ static inline void btr_assert(const char *assertion, const char *filename,
        exit(1);
 }

-#define BUG() btr_assert(NULL, __FILE__, __func__, __LINE__, 0)
+#define BUG() assert_trace(NULL, __FILE__, __func__, __LINE__, 0)

 #ifdef __CHECKER__
 #define __force    __attribute__((force))
@@ -268,10 +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) btr_assert(#c, __FILE__, __func__, __LINE__, !(c))
+#define BUG_ON(c) assert_trace(#c, __FILE__, __func__, __LINE__, !(c))

 #define WARN_ON(c) BUG_ON(c)
-#define        ASSERT(c) btr_assert(#c, __FILE__, __func__, __LINE__, (c))
+#define        ASSERT(c) assert_trace(#c, __FILE__, __func__, __LINE__, (c))

 #define container_of(ptr, type, member) ({                      \
         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-09-30  9:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-25 20:07 [PATCH] Btrfs-progs: make BUG*() be more verbose Josef Bacik
2014-09-30  9:43 ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).