Permalink
Browse files

backtrace: added function to print a back trace in asserts

  • Loading branch information...
1 parent f5eb106 commit ee644506eac5bc6eb40c4a5d508677d82d76796c Gaëtan Morin committed May 15, 2012
Showing with 19 additions and 0 deletions.
  1. +2 −0 kernel/CUtils/assert.h
  2. +16 −0 kernel/CUtils/backtrace.c
  3. +1 −0 kernel/CUtils/backtrace.h
@@ -11,10 +11,12 @@
#define _ASSERT_H_
#include "stdio.h"
+#include "backtrace.h"
#define assert(cond) do { \
if (!(cond)) { \
printf("%s (%s:%d): Assertion \"%s\" failed.", __FILE__, __func__, __LINE__, #cond); \
+ backtrace_print(); \
} \
} while (0)
@@ -0,0 +1,16 @@
+#include "backtrace.h"
+#include "stdio.h"
+
+#define BACKTRACE_SIZE 10
+static void *backtrace_buffer[BACKTRACE_SIZE];
+
+void backtrace_print(void)
+{
+ int i;
+
+ backtrace(backtrace_buffer, BACKTRACE_SIZE);
+ for (i = 0; i < BACKTRACE_SIZE; ++i) {
+ printf("%3d: 0x%08x\n", i backtrace_buffer[i]);
+ }
+}
+
@@ -17,6 +17,7 @@ extern "C" {
void backtrace(void **array, int size);
+void backtrace_print(void);
@ddejean
ddejean May 16, 2012

Comments ? Use javadoc or doxygen style.

@gaetmorin
gaetmorin May 16, 2012 Owner

Yes I'll fix that. Laziness is a bad habit... ;-)

@gaetmorin
gaetmorin May 16, 2012 Owner

Also, I wanted to discuss this API with you.

At this stage backtrace_print feels very patchy and tailored for assert only. It is not great to have it in a public header like this. I kind of had to do it because assert is a macro and every function it uses must be public.

Instead, what would you think about having a fail(char *message) API in assert.h that the macro would use ? fail would be implemented in its own file and have its own static backtrace buffer. This way we would have a function for failing without an assert (for example, in unreachable code).

@ddejean
ddejean May 16, 2012

I agree on backtrace_print issue, it must not be public. IMHO it might be part of assert stuff only. You're fail idea looks good to me, do you plan to introduce blocking code into ? like while(1) { hlt(); } ?

@gaetmorin
gaetmorin May 17, 2012 Owner

Great idea! I've implemented that and it works fine. I'm sending e new pull request in a moment.

2 comments on commit ee64450

@ddejean

Could you stash and do a "feature" commit ? This is a small changeset so one commit is enough and simpler to manage.

@gaetmorin
Owner

Yes you are right. I will rebase master this evening so don't pull anything now. I'll do proper topic branches in the future.

Please sign in to comment.