[英]Generating C++ BackTraces in OS/X (10.5.7)
I've been utilizing backtrace and backtrace_symbols to generate programmatic stack traces for the purposes of logging/diagnosis. 我一直在使用backtrace和backtrace_symbols来生成编程堆栈跟踪,以便进行日志记录/诊断。 It seems to roughly work, however, I'm getting a little bit of mangling and there are no accompanying file/line numbers associated with each function invocation (as I'd expect within a gdb bt call or something).
它似乎粗略地工作,但是,我得到一点点修改,并且没有与每个函数调用相关联的文件/行号(正如我在gdb bt调用中所期望的那样)。 Here's an example:
这是一个例子:
1 leonardo 0x00006989 _ZN9ExceptionC2E13ExceptionType + 111 1 leonardo 0x00006989 _ZN9ExceptionC2E13ExceptionType + 111
2 leonardo 0x00006a20 _ZN9ExceptionC1E13ExceptionType + 24 2 leonardo 0x00006a20 _ZN9ExceptionC1E13ExceptionType + 24
3 leonardo 0x0000ab64 _ZN5Rules11ApplyActionER16ApplicableActionR9GameState + 1060 3 leonardo 0x0000ab64 _ZN5Rules11ApplyActionER16ApplicableActionR9GameState + 1060
4 leonardo 0x0000ed15 _ZN9Simulator8SimulateEv + 2179 4 leonardo 0x0000ed15 _ZN9Simulator8SimulateEv + 2179
5 leonardo 0x0000eec9 _ZN9Simulator8SimulateEi + 37 5 leonardo 0x0000eec9 _ZN9Simulator8SimulateEi + 37
6 leonardo 0x00009729 main + 45 6 leonardo 0x00009729 main + 45
7 leonardo 0x000025c6 start + 54 7 leonardo 0x000025c6开始+ 54
Anything I'm missing something, doing something silly, or is this all I can expect out of backtrace on OS/X? 我有什么遗漏的东西,做些傻事,或者这是我在OS / X上的回溯吗?
Some other tidbits: 其他一些花絮:
rdynamic
link option for the g++ version (4.0.1) I'm using. rdynamic
链接选项。 -g/-g3
doesn't make any difference. -g/-g3
没有任何区别。 abi::__cxa__demangle
doesn't seem to do anything abi::__cxa__demangle
似乎什么也没做 The backtraces typically come back from backtrace_symbols in the following format: 回溯通常以backtrace_symbols以下列格式返回:
./MyApp(_ZN4test3fooEv+0x8) [0x821c874] ./MyApp(_ZN4test3fooEv+0x8)[0x821c874]
abi::__cxa_demangle expects only the function name. abi :: __ cxa_demangle只需要函数名称。 Thus, some parsing must first be done on the trace:
因此,必须首先对跟踪进行一些解析:
std::string trace(backtrace[idx]);
// attempt to demangle
{
std::string::size_type begin, end;
// find the beginning and the end of the useful part of the trace
begin = trace.find_first_of('(') + 1;
end = trace.find_last_of('+');
// if they were found, we'll go ahead and demangle
if (begin != std::string::npos && end != std::string::npos) {
trace = trace.substr(begin, end - begin);
size_t maxName = 1024;
int demangleStatus;
char* demangledName = (char*) malloc(maxName);
if ((demangledName = abi::__cxa_demangle(trace.c_str(), demangledName, &maxName,
&demangleStatus)) && demangleStatus == 0) {
trace = demangledName; // the demangled name is now in our trace string
}
free(demangledName);
}
}
I've tested this is on my own project, and it gives somewhat nicer backtraces with the following format: 我已经测试过这是我自己的项目,它使用以下格式提供了更好的回溯:
test::foo() 测试:: FOO()
Sure, there are no line numbers, but I'm not certain that is even possible. 当然,没有行号,但我不确定是否可行。
I don't know of any implementation of backtrace_symbols()
which gives any more than symbol+offset. 我不知道
backtrace_symbols()
的任何实现,它提供的不仅仅是symbol + offset。
With regards to abi:: __cxa__demangle
, you need to ensure that you only pass it the symbol name, without the + line
suffix, otherwise it won't recognise the symbol as valid. 关于
abi:: __cxa__demangle
,您需要确保只传递符号名称,不带+ line
后缀,否则它不会将符号识别为有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.