Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "assert with stacktrace" macro #23

Open
dmcconachie opened this issue Oct 4, 2017 · 0 comments
Open

Add "assert with stacktrace" macro #23

dmcconachie opened this issue Oct 4, 2017 · 0 comments

Comments

@dmcconachie
Copy link
Contributor

An "assert with stacktrace" macro could be useful for times when debugging with GDB just isn't practical.

Here is one such possibility (may need tweaking) adapted from https://stackoverflow.com/questions/37473/how-can-i-assert-without-using-abort

#include <execinfo.h>

void print_stack_trace(int fd)
{
    void *array[256];
    size_t size;

    size = backtrace (array, 256);
    backtrace_symbols_fd(array, size, fd);
}

#define ASSERT_STACKTRACE(expr)      do {                           \
 if (!(expr))                                                       \
 {                                                                  \
         fprintf(stderr,                                            \
                "file %s: line %d (%s): precondition `%s' failed.", \
                __FILE__,                                           \
                __LINE__,                                           \
                __PRETTY_FUNCTION__,                                \
                #expr);                                             \
         print_stack_trace(2);                                      \
         assert(expr);                                              \
 };               } while(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant