/** * 用于将backtrace_symbols函数所返回的字符串解析成对应的函数名,便于理解 * 头文件 cxxabi.h * 名字空间 abi * @param mangled_name A NUL-terminated character string containing the name to be demangled. * @param output_buffer A region of memory, allocated with malloc, of *length bytes, into which the demangled name is stored. If output_buffer is not long enough, it is expanded using realloc. * output_buffer may instead be NULL; in that case, the demangled name is placed in a region of memory allocated with malloc. * @param length If length is non-NULL, the length of the buffer containing the demangled name is placed in *length. * @param status *status is set to one of the following values: * 0: The demangling operation succeeded. * -1: A memory allocation failiure occurred. * -2: Mangled_name is not a valid name under the C++ ABI mangling rules. * -3: One of the arguments is invalid. */ char *__cxa_demangle (const char *mangled_name, char *output_buffer, size_t *length, int *status);
#define MAX_FRAMES 100
void GetStackTrace (std::string* stack)
{
void* addresses[MAX_FRAMES];
int size = backtrace (addresses, MAX_FRAMES);
std::unique_ptr<char*, void(*)(void*)> symbols {
backtrace_symbols (addresses, size),
std::free
};
for (int i = 0; i < size; ++i) {
stack->append (symbols.get()[i]);
stack->append ("n");
}
}
void TestFunc (std::string& stack, int value)
{
while (--value);
GetStackTrace (&stack);
}
int main(int argc, char* argv[])
{
std::string stack;
TestFunc (stack, 5);
std::cout << stack << std::endl;
return 0;
}
void DemangleSymbol (std::string* symbol)
{
size_t size = 0;
int status = -4;
char temp[256] = {' '};
//first, try to demangle a c++ name
if (1 == sscanf (symbol->c_str (), "%*[^(]%*[^_]%[^)+]", temp)) {
std::unique_ptr<char, void(*)(void*)> demangled {
abi::__cxa_demangle (temp, NULL, &size, &status),
std::free
};
if (demangled.get ()) {
symbol->clear ();
symbol->append (demangled.get ());
return;
}
}
//if that didn't work, try to get a regular c symbol
if (1 == sscanf(symbol->c_str (), "%255s", temp)) {
symbol->clear ();
symbol->append (temp);
}
}
void GetStackTrace (std::string* stack)
{
void* addresses[MAX_FRAMES];
int size = backtrace (addresses, MAX_FRAMES);
std::unique_ptr<char*, void(*)(void*)> symbols {
backtrace_symbols (addresses, size),
std::free
};
for (int i = 0; i < size; ++i) {
std::string demangled (symbols.get()[i]);
DemangleSymbol (&demangled);
stack->append (demangled);
stack->append ("n");
}
}
// The prefix used for mangled symbols, per the Itanium C++ ABI:
// http://www.codesourcery.com/cxx-abi/abi.html#mangling
const char kMangledSymbolPrefix[] = "_Z";
// Characters that can be used for symbols, generated by Ruby:
// (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join
const char kSymbolCharacters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
// Demangles C++ symbols in the given text. Example:
// "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]"
// =>
// "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]"
void DemangleSymbol (std::string* symbol)
{
std::string::size_type search_from = 0;
while (search_from < symbol->size ()) {
// Look for the start of a mangled symbol from search_from
std::string::size_type mangled_start = symbol->find (kMangledSymbolPrefix, search_from);
if (mangled_start == std::string::npos) {
break; // Mangled symbol not found
}
// Look for the end of the mangled symbol
std::string::size_type mangled_end = symbol->find_first_not_of (kSymbolCharacters, mangled_start);
if (mangled_end == std::string::npos) {
mangled_end = symbol->size ();
}
std::string mangled_symbol = std::move (symbol->substr (mangled_start, mangled_end - mangled_start));
// Try to demangle the mangled symbol candidate
int status = -4; // some arbitrary value to eliminate the compiler warning
std::unique_ptr<char, void(*)(void*)> demangled_symbol {
abi::__cxa_demangle (mangled_symbol.c_str (), nullptr, 0, &status),
std::free
};
// 0 Demangling is success
if (0 == status) {
// Remove the mangled symbol
symbol->erase (mangled_start, mangled_end - mangled_start);
// Insert the demangled symbol
symbol->insert (mangled_start, demangled_symbol.get ());
// Next time, we will start right after the demangled symbol
search_from = mangled_start + strlen (demangled_symbol.get ());
}
else {
// Failed to demangle. Retry after the "_Z" we just found
search_from = mangled_start + 2;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有