Reputation: 4167
I use following code to get backtrace
void print_backtrace(void) {
static const char start[] = "BACKTRACE ------------\n";
static const char end[] = "----------------------\n";
void *bt[1024];
int bt_size;
char **bt_syms;
int i;
bt_size = backtrace(bt, 1024);
bt_syms = backtrace_symbols(bt, bt_size);
for (i = 1; i < bt_size; i++) {
size_t len = strlen(bt_syms[i]);
std::cout << bt_syms[i] << std::endl;
}
free(bt_syms);
}
int main() {
print_backtrace();
return 0;
}
and the output is:
/home/roroco/Dropbox/c/ro-c/cmake-build-debug/ex/test_backtrace_with_line_number
/home/roroco/Dropbox/c/ro-c/cmake-build-debug/ex/test_backtrace_with_line_number(main+0x9) [0x400d0b]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7ff20985c830]
/home/roroco/Dropbox/c/ro-c/cmake-build-debug/ex/test_backtrace_with_line_number(_start+0x29) [0x400b29]
when i use addr2line to convert addr to line number with following, the return always "??:0", how to get real line number?
roroco@roroco ~/Dropbox/c/ro-c $ addr2line -e /home/roroco/Dropbox/c/ro-c/cmake-build-debug/CMakeFiles/3.12.0/CompilerIdCXX/a.out 0x400d0b
??:0
roroco@roroco ~/Dropbox/c/ro-c $ addr2line -e /home/roroco/Dropbox/c/ro-c/cmake-build-debug/CMakeFiles/3.12.0/CompilerIdCXX/a.out 0x7ff20985c830
??:0
roroco@roroco ~/Dropbox/c/ro-c $ addr2line -e /home/roroco/Dropbox/c/ro-c/cmake-build-debug/CMakeFiles/3.12.0/CompilerIdCXX/a.out 0x400b29
??:0
Upvotes: 2
Views: 1236
Reputation: 2070
It turns out that
/home/roroco/Dropbox/c/ro-c/cmake-build-debug/CMakeFiles/3.12.0/CompilerIdCXX/a.out
is not the executable you built (it corresponds to the compiler test done by CMake).
Instead you could try the following:
addr2line -e /home/roroco/Dropbox/c/ro-c/cmake-build-debug/ex/test_backtrace_with_line_number
where test_backtrace_with_line_number
corresponds to the executable you built. It should be the name associated with the add_executable
command in your CMakeLists.txt
Upvotes: 1