2011-09-23 14:48:55 -05:00
|
|
|
// ABI-specific routines.
|
|
|
|
|
2011-09-23 17:05:24 -05:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2011-09-23 14:48:55 -05:00
|
|
|
#include <vector>
|
2011-08-20 16:22:09 -05:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "rust_abi.h"
|
|
|
|
|
2011-12-30 02:18:55 -06:00
|
|
|
#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
|
2011-09-23 17:05:24 -05:00
|
|
|
#define HAVE_DLFCN_H
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#elif defined(_WIN32)
|
2012-07-12 20:20:39 -05:00
|
|
|
// FIXME #2889
|
2011-09-23 17:05:24 -05:00
|
|
|
#endif
|
|
|
|
|
2011-09-23 14:48:55 -05:00
|
|
|
#define END_OF_STACK_RA (void (*)())0xdeadbeef
|
|
|
|
|
2011-08-20 16:22:09 -05:00
|
|
|
weak_symbol<uint32_t> abi_version("rust_abi_version");
|
|
|
|
|
|
|
|
uint32_t get_abi_version() {
|
|
|
|
return (*abi_version == NULL) ? 0 : **abi_version;
|
|
|
|
}
|
|
|
|
|
2011-09-23 14:48:55 -05:00
|
|
|
namespace stack_walk {
|
|
|
|
|
2011-09-23 17:05:24 -05:00
|
|
|
#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
|
2011-09-23 17:08:37 -05:00
|
|
|
frame::symbol() const {
|
2011-09-23 17:05:24 -05:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::hex << (uintptr_t)ra;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-23 14:48:55 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-23 17:05:24 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2011-09-23 14:48:55 -05:00
|
|
|
} // end namespace stack_walk
|
|
|
|
|