-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Backtraces do not work for binaries loaded through ld.so #101913
Comments
(Thanks @jryans for pointing me at this issue.) If I've understood, the main problem is getting the file path associated with a particular region in the program's virtual address space. A secondary problem is that the link map leaves out the executable's filename (it is recorded as the empty string), and the target of The only reliable way I know to solve the main problem is to use For example, a backtrace routine could look for the I've grappled with this sort of thing in my liballocs and librunt projects. E.g. there is a gotcha about reading the maps file: if your program might map memory while reading it, the file might change under your feet. |
Would that be enough to get the debug info though? even if the file name wouldn't resolve, at least the symbols would. |
If you don't want to call out to the filesystem, then only the dynamic linking symbols are available (.dynsym but not .symtab). Exactly which symbols that includes depends on how the binary was linked... in executables, you can get away without exporting much, so sticking with this limitation will not reliably give good backtraces. But 'debugging information' usually doesn't refer to either of these sections, but rather to the more detailed information that is generated for a debugger's benefit (.debug_info and friends, if using DWARF). If you want reliably good backtraces then it's worth looking at that, as even .symtab isn't enough to give you a symbolic name for all code. You need to go to the filesystem to get either of those, anyway. |
I've started looking at this approach. It looks like it may be reasonably straight-foward to implement (something on the order of 100 lines of code in a standalone prototype), and does seem to resolve the I'm going to see if I can make the corresponding change in https://github.com/rust-lang/backtrace-rs now |
(This might be fixed, as I believe that the change from rust-lang/backtrace-rs#488 has been pulled into the current stable rust, and may just need a test. But I haven't checked carefully yet.) |
WG-prioritization assigning priority (Zulip discussion). @rustbot label -I-prioritize +P-low |
Weird: Things seem to work here when I use the nightly build, but not when I use the 1.70 build:
So I must need to do something deeper here than what I had expected. Looking. |
@rustbot label -P-low +P-medium |
Curiouser and cusiouser:
I.e. it works in the beta (and nightly) channels, but not in the stable channel. Why?
|
Hmm. My change to backtrace-rs only just "recently" made it into a release of that crate, in backtrace-rs 0.3.67, back in December 2022. and the rust project's Cargo.lock file only "recently" was bumped to use that version of backtrace-rs, in #106704 (see 7c8c9cf#diff-13ee4b2252c9e516a0547f2891aa2105c3ca71c6d7a1e682c69be97998dfc87eR215 ) Now, I had thought that the rust project itself was using backtrace-rs as a submodule, and that would cause us to pull in the development version of backtrace that is referenced by the git metadata when it comes to how the stdlib computes backtraces. But the discovery in my earlier comment that the beta branch "works", while the stable branch does not, is leading me to wonder if I've misunderstood how backtrace-rs is linked in (and thus misunderstood which version of backtrace-rs would be linked in...) This is all "fine", in the sense that I'll be very happy if this has all just been a big misunderstanding on my part of how long it would take for my fix to make it into Rust itself (in part because I wasn't aggressively pushing for changes to version of backtrace-rs in the Cargo.lock file, and that was because, as mentioned above, I didn't realize they would be necessary...) |
Ah, okay, brilliant. Thanks to the awesome https://github.com/kennytm/rustup-toolchain-install-master I was able to just compare the behavior of the compiler immediately before and after PR #106704, where before is 9c51cf7 and after is 39c6804
So that's enough to tell me that this was indeed fixed by #106704, and that we should see the effects of that fix flow into Rust 1.71. (I had erroneously thought that 1.67 would be the first release that had the fixed behavior.) |
(at this point I think the only remaining task here is to write a test. It will probably need to be a run-make test, due to the very specialized nature of how the test will need to invoke the binary and then post-process the output...) |
When a Rust binary that is executed through ld.so — the Linux dynamic linker — triggers a panic, the backtraces are useless. I've included a discussion of why executing through ld.so is valuable below, but first, to reproduce, build a simple binary that panics on execution:
Backtraces work fine when executing the binary directly:
But when executing through
ld.so
, the debug symbols vanish:I suspect this happens because backtraces attempt to load debug symbols through
/proc/self/exe
, but/proc/self/exe
points told.so
in this case, not the real binary. I don't know of any good solutions to this, but one solution is to preferargv[0]
if/proc/self/exe
isld.so
. It won't be perfect (callers need to useexec -a
among other things), but it'll be better than what happens today. I suspect this happens with other execution wrappers than justld.so
(does Valgrind do this for certain kinds of execution for example?), but I don't have any concrete examples.Why ld.so?
Where I work, we execute binaries through the dynamic linker so that we can explicitly set the shared library search path to ensure that the same shared libraries used to build a given binary are used when it is executed. It has a similar effect as setting
LD_LIBRARY_PATH
, but with the added benefit that it does not also set the shared library search path of any programs executed down the line. There is a lot of underlying complexity here that is probably not worth digging too much into, but the idea is that a binary should ship "with its environment" (think Nix-style binaries), and so if the built Rust binary and some binary it in turn invokes are built separately, they should use different shared library search paths, namely the ones that map to exactly what they were built with. Executing through the dynamic linker achieves that,LD_LIBRARY_PATH
does not. (This is also done by things like go-appimage).Cargo has a similar issue, though its solution is going to be a little different since it can also be used a library: rust-lang/cargo#10119.
rust-analyzer also run into issues with ld.so, though from what I can tell there are fixes in the pipeline there already.
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: