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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/* $Id$
*
* Helper to obtain the current stack trace. Should not be used in the actual
* compiler (only there for debugging).
*
* Copyright (C) 2008-2009 FAUmachine Team <info@faumachine.org>.
* This program is free software. You can redistribute it and/or modify it
* under the terms of the GNU General Public License, either version 2 of
* the License, or (at your option) any later version. See COPYING.
*/
#include "frontend/misc/StackTrace.hpp"
#include <iostream>
#include <cassert>
extern "C" {
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <dlfcn.h>
#include <stdio.h>
static void *
__attribute__((always_inline))
getStackElement(
void *frame_pointer,
const char **entry
)
{
void **fp = reinterpret_cast<void **>(frame_pointer);
void *return_address;
void *old_fp;
int ret;
Dl_info info;
old_fp = *fp;
fp++;
return_address = *fp;
ret = dladdr(return_address, &info);
if (ret == 0) {
return NULL;
}
*entry = info.dli_sname;
return old_fp;
}
#if defined(__i386__)
static void *
__attribute__((always_inline))
getFramePointer(void)
{
void *fp;
asm (
"mov %%ebp, %0;"
: "=r"(fp) /* output */
: /* no input */
: /* nothing clobbered */
);
return fp;
}
/* __i386__ */
#elif defined(__x86_64__)
static void *
__attribute__((always_inline))
getFramePointer(void)
{
void *fp;
asm (
"mov %%rbp, %0;"
: "=r"(fp) /* output */
: /* no input */
: /* nothing clobbered */
);
return fp;
}
#else /* ! __x86_64__ */
#error Not ported yet.
#endif
const char *
demangle(const char *sym)
{
static char cmd[4096];
unsigned int ret;
FILE *fp;
char *s;
ret = snprintf(cmd, sizeof(cmd), "/usr/bin/c++filt \"%s\"", sym);
assert(ret < sizeof(cmd));
fp = popen(cmd, "r");
if (fp == NULL) {
return sym;
}
s = fgets(cmd, sizeof(cmd), fp);
if (s == NULL) {
pclose(fp);
return sym;
}
pclose(fp);
return cmd;
}
}; /* extern "C" */
namespace ast {
std::map<std::string, std::string> StackTrace::mangleMap =
std::map<std::string, std::string>();
StackTrace *
StackTrace::getStackTrace(void)
{
void *fp;
const char *symbol;
StackTrace *strace;
std::string demangled_sym;
strace = new StackTrace();
fp = getFramePointer();
while (fp != NULL) {
fp = getStackElement(fp, &symbol);
if (fp != NULL) {
demangled_sym = resolveSym(std::string(symbol));
strace->stackTrace.push_back(demangled_sym);
}
}
return strace;
}
void
StackTrace::put(std::ostream &stream) const
{
stream << "Stack trace:" << std::endl;
for (std::list<std::string>::const_iterator i =
this->stackTrace.begin(); i != this->stackTrace.end(); i++) {
stream << "\t" << *i;
}
}
std::string
StackTrace::resolveSym(std::string sym)
{
std::map<std::string, std::string>::const_iterator i =
StackTrace::mangleMap.find(sym);
if (i != StackTrace::mangleMap.end()) {
return i->second;
}
// resolve + insert into mangle map.
const char *res;
res = demangle(sym.c_str());
if (res != NULL) {
mangleMap[sym] = std::string(res);
return std::string(res);
}
// resolution not possible
mangleMap[sym] = sym;
return sym;
}
std::ostream&
operator <<(std::ostream &stream, const StackTrace &st)
{
st.put(stream);
return stream;
}
}; /* namespace ast */
|