我正在尝试使用gaddr2line
打印更漂亮的回溯符号版本。我只是backtrace()
通过gaddr2line
使用popen
. 在 linux 下使用addr2line
可以正常工作,但由于某种原因,我gaddr2line
在 OS X 上使用时遇到奇怪的错误。在 Mac OS 上尝试这个时,我不断收到BFD: ... : unknown load command ...
一些帖子提到 GDB,所以我尝试用自制软件安装它,但那没有没有帮助。
这是我的代码:
// set to the maximum possible path size
char exe_path[4096];
// Used to check if an error occured while setting up command
bool error = false;
// Check if we are running on Mac OS or not, and select appropriate command
char* command;
#ifdef __APPLE__
// Check if 'gaddr2line' function is available, if not exit
if( !system( "which gaddr2line > /dev/null 2>&1" ) ) {
command = "gaddr2line -Cfspe";
pid_t pid = getpid();
int path_length = proc_pidpath( pid, exe_path, sizeof( exe_path ) );
if( path_length <= 0 ) {
writeLog( SIMPLOG_LOGGER, "Unable to get execution path. Defaulting to standard backtrace." );
error = true;
}
exe_path[path_length] = 0;
} else {
writeLog( SIMPLOG_LOGGER, "Function 'gaddr2line' unavailable. Defaulting to standard backtrace. Please install package 'binutils' for better stacktrace output." );
error = true;
}
#endif
这是输出:
gaddr2line -Cfspe /Users/nate/git/SimpleLogger/build/test 7408927
BFD: /Users/nate/git/SimpleLogger/build/test: unknown load command 0x2a
BFD: /Users/nate/git/SimpleLogger/build/test: unknown load command 0x28
BFD: /Users/nate/git/SimpleLogger/build/test: unknown load command 0x29
BFD: /Users/nate/git/SimpleLogger/build/test: unknown load command 0x2b