src/test/backtrace.c
author Tero Marttila <terom@fixme.fi>
Wed, 27 May 2009 23:57:48 +0300
branchnew-lib-errors
changeset 217 7728d6ec3abf
parent 189 f351facab1f0
permissions -rw-r--r--
nexus.c compiles
189
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "backtrace.h"
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include "../log.h"
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <execinfo.h>
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
void test_backtrace (int skip)
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
{
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    void *stacktrace[TEST_BACKTRACE_MAX];
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
    char **lines;
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    int count, i;
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    // get the raw stack data
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    if ((count = backtrace(stacktrace, TEST_BACKTRACE_MAX)) >= TEST_BACKTRACE_MAX)
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
        log_warn("(backtrace truncated)");
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    // get the textual representation
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    if ((lines = backtrace_symbols(stacktrace, count)) == NULL)
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
        return log_warn("(unable to alloc backtrace)");
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    // then log_debug it out
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    for (i = skip; i < count; i++)
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        log_debug("%s", lines[i]);
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    // release resources
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    free(lines);
f351facab1f0 add a test/backtrace module and have ASSERT_FAIL dump out a backtrace, also, fix assert_str[n]cmp to handle should_be == NULL
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
}