boostorg / stacktrace

C++ library for storing and printing backtraces.
https://boost.org/libs/stacktrace
422 stars 69 forks source link

fix addr2line for pie binaries #138

Closed ja2142 closed 1 year ago

ja2142 commented 1 year ago

addr2line can't translate addresses from position independent binaries, it needs offsets from binary base. Here it's achieved by reading /proc/self/maps (to be honest, I don't know how portable exactly that is, but judging from the fact that /proc/self/exe is read, that is not a huge concern?).

Most likely doesn't work with C++03, I can try to fix it if needed.

I'm not sure, but it might fix #97 or #113.


There's a commented out line

//return addr2line("-Cfipe", addr); // Does not seem to work in all cases

It's likely that it'd work in more cases with my fix, if offsets from binary image base were passed to addr2line - in that case, I think it might be wise to just use it as it was before, and get both function name and file info from addr2line, as it seems to be more robust than dladdr (addr2line should work for all binaries with debug info, while dl functions probably only work for exported symbols (and non-pie binaries, for some reason); maybe dladdr should still be left as a fallback).

coveralls commented 1 year ago

Coverage Status

coverage: 91.376% (+0.4%) from 90.927% when pulling 988ee0f512d750b9c3a9808dc28f3a62fc6cdd18 on ja2142:addr2line_pie_fix into c6e771286801ca7f5f58b3d6a812b95b85e71bc0 on boostorg:develop.

raidenluikang commented 1 year ago

It still write unusable addresses for stripped pie shared libraries. If I'm not mistaken.

ja2142 commented 1 year ago

I just realized I've only fixed converting whole frame to string, source_line(), source_file() etc. probably still won't work for pie binaries. I'll try to get it fixed somewhen today.

ja2142 commented 1 year ago

@raidenluikang well, exported symbols (I guess those that are visible in objdump -T <binary>?) should probably be visible, doesn't matter if binary is stripped or not (and they are in my simple tests). Non-exported functions would require debug symbols to have names in a stacktrace.

apolukhin commented 1 year ago

Many thanks for the great work!