aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/tests/stacktrace/backtrace.c
blob: 44b33ca45721ab2bc91b46caa975a1ae5a5d0a2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "backtrace.h"

#if defined(__i386__)
// use GLIBC version, hope it works
extern int backtrace(void **buffer, int size);
#define HAVE_BACKTRACE
#endif

#if defined(__x86_64__)

/**
   Written by Arne H. J. based on docs:

   http://www.kernel.org/pub/linux/devel/gcc/unwind/
   http://www.codesourcery.com/public/cxx-abi/abi-eh.html
   http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/libgcc-s-ddefs.html
**/

#include <unwind.h>

struct trace_context {
    void **array;
    int size;
    int index;
};

static _Unwind_Reason_Code
trace_fn(struct _Unwind_Context *ctxt, void *arg)
{
    struct trace_context *tp = (struct trace_context *)arg;
    void *ip = (void *)_Unwind_GetIP(ctxt);

    if (ip == 0) {
        return _URC_END_OF_STACK;
    }
    if (tp->index <= tp->size) {
        // there's no point filling in the address of the backtrace()
        // function itself, that doesn't provide any extra information,
        // so skip one level
        if (tp->index > 0) {
            tp->array[tp->index - 1] = ip;
        }
        tp->index++;
    } else {
        return _URC_NORMAL_STOP;
    }
    return _URC_NO_REASON; // "This is not the destination frame" -> try next frame
}

#define HAVE_BACKTRACE
int
backtrace (void **array, int size)
{
    struct trace_context t;
    t.array = array;
    t.size = size;
    t.index = 0;
    _Unwind_Backtrace(trace_fn, &t);
    return t.index - 1;
}
#endif // x86_64


#ifdef HAVE_BACKTRACE

int
FastOS_backtrace (void **array, int size)
{
    return backtrace(array, size);
}

#else

# warning "backtrace not supported on this CPU"
int
FastOS_backtrace (void **array, int size) 
{
    (void) array;
    (void) size;
    return 0;
}

#endif