Find out which line of a Rust program is running from another program

Is it possible to find out which line of a Rust program is running from another program? Maybe using the debug info somehow?

Let's say we have a Rust executable called execA. Another Rust program called runner spawns execA. Is it possible to use the debug information to find out which file and line of execA is currently running?

Even better would be being able to control the execution of the program similar to how GDB can. I looked it up and GDB uses something called ptrace. Any idea how to use this to get the currently executing line in the program as it runs?

Thanks!

You'd use PTRACE_GETREGS to retrieve the register state of the process. The rip on x86_64 or eip on i686 register contains the address of the currently executing instruction. You can then turn that into a function name and line number using the process's debug info using something like https://crates.io/crates/addr2line.

7 Likes

ptrace can be used directly from the libc crate but you can also use nix which provides a safer wrapper around it. There's WIP patches for it as well that make some improvements in that area also.

3 Likes

This is awesome! Thank you both!