Linux下打印调用堆栈
Posted startFromWeb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下打印调用堆栈相关的知识,希望对你有一定的参考价值。
废话不说直接写代码 不懂函数自己去man
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(strings[i]);
stack_.append(demangle(strings[i]));
stack_.push_back('\\n');
free(strings);
string Exception::demangle(const char* symbol)
size_t size;
int status;
char temp[128];
char* demangled;
//first, try to demangle a c++ name
if (1 == sscanf(symbol, "%*[^(]%*[^_]%127[^)+]", temp))
if (NULL != (demangled = abi::__cxa_demangle(temp, NULL, &size, &status)))
string result(demangled);
free(demangled);
return result;
//if that didn't work, try to get a regular c symbol
if (1 == sscanf(symbol, "%127s", temp))
return temp;
//if all else fails, just return the symbol
return symbol;
以上是关于Linux下打印调用堆栈的主要内容,如果未能解决你的问题,请参考以下文章