about summary refs log tree commit diff
path: root/src/rt/rust_abi.cpp
blob: 8de42bb666617305b20973dd87809c81be309c91 (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
// ABI-specific routines.

#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <stdint.h>
#include "rust_abi.h"

#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
#define HAVE_DLFCN_H
#include <dlfcn.h>
#elif defined(_WIN32)
// FIXME #2889
#endif

#define END_OF_STACK_RA     (void (*)())0xdeadbeef

weak_symbol<uint32_t> abi_version("rust_abi_version");

uint32_t get_abi_version() {
    return (*abi_version == NULL) ? 0 : **abi_version;
}

namespace stack_walk {

#ifdef HAVE_DLFCN_H
std::string
frame::symbol() const {
    std::stringstream ss;

    Dl_info info;
    if (!dladdr((void *)ra, &info))
        ss << "??";
    else
        ss << info.dli_sname;

    ss << " @ " << std::hex << (uintptr_t)ra;
    return ss.str();
}
#else
std::string
frame::symbol() const {
    std::stringstream ss;
    ss << std::hex << (uintptr_t)ra;
    return ss.str();
}
#endif

std::vector<frame>
backtrace() {
    std::vector<frame> frames;

    // Ideally we would use the current value of EIP here, but there's no
    // portable way to get that and there are never any GC roots in our C++
    // frames anyhow.
    frame f(__builtin_frame_address(0), (void (*)())NULL);

    while (f.ra != END_OF_STACK_RA) {
        frames.push_back(f);
        f.next();
    }
    return frames;
}

std::string
symbolicate(const std::vector<frame> &frames) {
    std::stringstream ss;
    std::vector<frame>::const_iterator begin(frames.begin()),
                                       end(frames.end());
    while (begin != end) {
        ss << begin->symbol() << std::endl;
        ++begin;
    }
    return ss.str();
}

}   // end namespace stack_walk