Skip to content

Add "assert with stacktrace" macro #23

@dmcconachie

Description

@dmcconachie

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions