demangle函数实现

来源:互联网 发布:苹果手机短信备份软件 编辑:程序博客网 时间:2024/06/08 13:11
#include <cxxabi.h>#include <cstdio>void Exception::fillStackTrace(){  const int len = 200;  void* buffer[len];  int nptrs = ::backtrace(buffer, len);  char** strings = ::backtrace_symbols(buffer, nptrs);  if (strings)  {    for (int i = 0; i < nptrs; ++i)    {      // TODO demangle funcion name with abi::__cxa_demangle      stack_.append(demangle(strings[i]));      stack_.push_back('\n');    }    free(strings);  }}string Exception::demangle(const char *symbol){    size_t size = 0;    int status = 0;    char temp[128] = {0};    char *demangled = NULL;    //first, try to demangle a c++ name    if (sscanf(symbol, "%*[^(]%*[^_]%127[^)+]", temp) == 1) {        if ((demangled = abi::__cxa_demangle(temp, NULL, &size,                        &status)) != NULL) {            string result(demangled);            free(demangled);            return result;        }    }    //if that didn't work, try to get a regular c symbol    if (sscanf(symbol, "%127s", temp) == 1) {        return temp;    }    //if all else fails, just return the symbol    return symbol;}

用g++编译时候加上-rdynamic选项,加入函数符号表才能正确显示
0 0
原创粉丝点击