I want my program to print backtraces that will be usable by addr2line after program finishes. On ubuntu 14.04 this was achieved by following code:
void bt() {
constexpr int MAX_STACK = 30;
void *array[MAX_STACK];
auto size = backtrace(array, MAX_STACK);
std::ostringstream msg;
for (int i = 0; i < size; ++i) {
msg << array[i] << " ";
}
std::cout << msg.str() << std::endl;
}
// sample output: 0x55db7a9fdea3 0x55db7a9fdfd5 0x55db7a9fdfe1 0x55db7a9fe01f
When used now on ubuntu 18.04, addr2line gives a lot of ??:0
.
I am not interested in human-readable format given by backtrace_symbols(3)
linux function, I want concise format that can be decoded when necessary with separately distributed debug symbols (stripped to separate .dbg file).
I could compile with -no-pie
, but I don't want to. It seems that this is something that we don't want in production.
I learned that if for each pointer (array[i]
above) I remove value from /proc/<pid>/maps
and convert back to hex, I get address that is understandable for addr2line. So question is:
How do I get the offset in code, without looking at text file in /proc? Or is there a better method for achieving concise backtrace?