Adds backtraces to all assert messages, so developers receive the
context of the problem.  After all, finding the problem often requires
knowing how the code managed to reach the call that contains the
assertion that has failed.

Signed-off-by: Zachary T Welch <z...@superlucidity.net>
---
NOTE: The complete series is available in my mirror on repo.or.cz
in the 'stack' branch:

        git://repo.or.cz/openocd/ztw.git
---
 src/helper/stack.c  |   10 ++++++++++
 src/helper/stack.h  |   16 ++++++++++++++++
 src/helper/system.h |    3 +++
 3 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/src/helper/stack.c b/src/helper/stack.c
index 599b983..fc1e038 100644
--- a/src/helper/stack.c
+++ b/src/helper/stack.c
@@ -181,3 +181,13 @@ int stack_walk(unsigned depth, stack_walker_t walker, 
intptr_t data)
        };
        return stack_dump(depth, &stack_walk_dumper, (intptr_t)&walk);
 }
+
+void stack_assert(const char *file, unsigned line,
+               const char *function, const char *condition)
+{
+       LOG_ERROR("Assertion failed:\n\t%s\n\tFunc:\t%s\n"
+                       "\tLine:\t%u:\n\tFile:\t%s",
+                       condition, function, line, file);
+       stack_walk(100, &stack_log_walker, 0);
+       exit(1);
+}
diff --git a/src/helper/stack.h b/src/helper/stack.h
index e18be14..6155f34 100644
--- a/src/helper/stack.h
+++ b/src/helper/stack.h
@@ -105,4 +105,20 @@ int stack_log_walker(struct stack_walk_state *state, 
intptr_t cmd_ctx);
  */
 int stack_walk(unsigned depth, stack_walker_t walker, intptr_t data);
 
+#ifndef NDEBUG
+
+void stack_assert(const char *file, unsigned line,
+               const char *function, const char *condition);
+
+// remove assert macro from namespace
+#undef assert
+// replace assert with call to inline and original version (C9x-portable)
+#define assert(__x) \
+       ((__x) \
+        ? (void)(0) \
+        : stack_assert(__FILE__, __LINE__, __func__, stringify(__x)))
+
+#endif // NDEBUG
+
+
 #endif // HELPER_STACK_H
diff --git a/src/helper/system.h b/src/helper/system.h
index 169df1c..e15dbef 100644
--- a/src/helper/system.h
+++ b/src/helper/system.h
@@ -30,6 +30,9 @@
 #include <ctype.h>
 #include <errno.h>
 
+// for stack_assert()
+#include "stack.h"
+
 // +++ AC_HEADER_TIME +++
 #ifdef TIME_WITH_SYS_TIME
 # include <sys/time.h>
-- 
1.6.4.4

_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to