summaryrefslogtreecommitdiff
path: root/libphobos/libdruntime/core/sys/freebsd/execinfo.d
blob: 125ef097d27e73dc26ed4aec0b6f9ed504151951 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * FreeBSD implementation of glibc's $(LINK2 http://www.gnu.org/software/libc/manual/html_node/Backtraces.html backtrace) facility.
 *
 * Copyright: Copyright Martin Nowak 2012.
 * License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
 * Authors:   Martin Nowak
 * Source:    $(DRUNTIMESRC core/sys/freebsd/_execinfo.d)
 */
module core.sys.freebsd.execinfo;

version (FreeBSD):
extern (C):
nothrow:

version (GNU)
    version = BacktraceExternal;

version (BacktraceExternal)
{
    size_t backtrace(void**, size_t);
    char** backtrace_symbols(const(void*)*, size_t);
    void backtrace_symbols_fd(const(void*)*, size_t, int);
    char** backtrace_symbols_fmt(const(void*)*, size_t, const char*);
    int backtrace_symbols_fd_fmt(const(void*)*, size_t, int, const char*);
}
else
{
    import core.sys.freebsd.dlfcn;

    // Use extern (D) so that these functions don't collide with libexecinfo.

    extern (D) int backtrace(void** buffer, int size)
    {
        import core.thread : thread_stackBottom;

        void** p, pend=cast(void**)thread_stackBottom();
        version (D_InlineAsm_X86)
            asm nothrow @trusted { mov p[EBP], EBP; }
        else version (D_InlineAsm_X86_64)
            asm nothrow @trusted { mov p[RBP], RBP; }
        else version (AArch64)
            asm nothrow @trusted { "str x29, %0" : "=m" (p); }
        else
            static assert(false, "Architecture not supported.");

        int i;
        for (; i < size && p < pend; ++i)
        {
            buffer[i] = *(p + 1);
            auto pnext = cast(void**)*p;
            if (pnext <= p) break;
            p = pnext;
        }
        return i;
    }


    extern (D) char** backtrace_symbols(const(void*)* buffer, int size)
    {
        static void* realloc(void* p, size_t len) nothrow
        {
            static import cstdlib=core.stdc.stdlib;
            auto res = cstdlib.realloc(p, len);
            if (res is null) cstdlib.free(p);
            return res;
        }

        if (size <= 0) return null;

        size_t pos = size * (char*).sizeof;
        char** p = cast(char**)realloc(null, pos);
        if (p is null) return null;

        Dl_info info;
        foreach (i, addr; buffer[0 .. size])
        {
            if (dladdr(addr, &info) == 0)
                (cast(ubyte*)&info)[0 .. info.sizeof] = 0;
            fixupDLInfo(addr, info);

            immutable len = formatStackFrame(null, 0, addr, info);
            assert(len > 0);

            p = cast(char**)realloc(p, pos + len);
            if (p is null) return null;

            formatStackFrame(cast(char*)p + pos, len, addr, info) == len || assert(0);

            p[i] = cast(char*)pos;
            pos += len;
        }
        foreach (i; 0 .. size)
        {
            pos = cast(size_t)p[i];
            p[i] = cast(char*)p + pos;
        }
        return p;
    }


    extern (D) void backtrace_symbols_fd(const(void*)* buffer, int size, int fd)
    {
        import core.sys.posix.unistd : write;
        import core.stdc.stdlib : alloca;

        if (size <= 0) return;

        Dl_info info;
        foreach (i, addr; buffer[0 .. size])
        {
            if (dladdr(addr, &info) == 0)
                (cast(ubyte*)&info)[0 .. info.sizeof] = 0;
            fixupDLInfo(addr, info);

            enum maxAlloca = 1024;
            enum min = (size_t a, size_t b) => a <= b ? a : b;
            immutable len = min(formatStackFrame(null, 0, addr, info), maxAlloca);
            assert(len > 0);

            auto p = cast(char*)alloca(len);
            if (p is null) return;

            formatStackFrame(p, len, addr, info) >= len || assert(0);
            p[len - 1] = '\n';
            write(fd, p, len);
        }
    }


    private void fixupDLInfo(const(void)* addr, ref Dl_info info)
    {
        if (info.dli_fname is null) info.dli_fname = "???";
        if (info.dli_fbase is null) info.dli_fbase = null;
        if (info.dli_sname is null) info.dli_sname = "???";
        if (info.dli_saddr is null) info.dli_saddr = cast(void*)addr;
    }


    private size_t formatStackFrame(char* p, size_t plen, const(void)* addr, const ref Dl_info info)
    {
        import core.stdc.stdio : snprintf;

        immutable off = addr - info.dli_saddr;
        immutable len = snprintf(p, plen, "%p <%s+%zd> at %s",
                                 addr, info.dli_sname, off, info.dli_fname);
        assert(len > 0);
        return cast(size_t)len + 1; // + '\0'
    }
}