Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

backtrace: added function to print a back trace in asserts

  • Loading branch information...
commit ee644506eac5bc6eb40c4a5d508677d82d76796c 1 parent f5eb106
Gaëtan Morin authored
2  kernel/CUtils/assert.h
@@ -11,10 +11,12 @@
11 11 #define _ASSERT_H_
12 12
13 13 #include "stdio.h"
  14 +#include "backtrace.h"
14 15
15 16 #define assert(cond) do { \
16 17 if (!(cond)) { \
17 18 printf("%s (%s:%d): Assertion \"%s\" failed.", __FILE__, __func__, __LINE__, #cond); \
  19 + backtrace_print(); \
18 20 } \
19 21 } while (0)
20 22
16 kernel/CUtils/backtrace.c
... ... @@ -0,0 +1,16 @@
  1 +#include "backtrace.h"
  2 +#include "stdio.h"
  3 +
  4 +#define BACKTRACE_SIZE 10
  5 +static void *backtrace_buffer[BACKTRACE_SIZE];
  6 +
  7 +void backtrace_print(void)
  8 +{
  9 + int i;
  10 +
  11 + backtrace(backtrace_buffer, BACKTRACE_SIZE);
  12 + for (i = 0; i < BACKTRACE_SIZE; ++i) {
  13 + printf("%3d: 0x%08x\n", i backtrace_buffer[i]);
  14 + }
  15 +}
  16 +
1  kernel/CUtils/backtrace.h
@@ -17,6 +17,7 @@ extern "C" {
17 17
18 18
19 19 void backtrace(void **array, int size);
  20 +void backtrace_print(void);
20 21
21 22
22 23

2 comments on commit ee64450

Damien Dejean

Comments ? Use javadoc or doxygen style.

Damien Dejean

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

gmorin

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

gmorin

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).

gmorin
Owner
gmorin commented on ee64450

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.

Damien Dejean

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(); } ?

gmorin

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

Please sign in to comment.
Something went wrong with that request. Please try again.