Reputation: 7268
In a tracing tool I'm developing, I use the libC backtrace
function to get the list of return pointers in the stack (array of void*
) from some points in the execution. These lists of pointers are then stored in a dictionary and associated with a unique integer.
Problem: from one execution of the program to another, the code of the program may be located somewhere else in memory, so the array of pointers from a particular call to backtrace will change from an execution to another, although the program has executed the same things.
Partial solution: getting the address of a reference function (e.g., main
) and store the difference between the address of this function and the addresses in the backtrace, instead of the raw addresses.
New problem: if the program uses dynamically loaded libraries, the location in memory of the code of these libraries may differ from one execution to another, so the differences of address with respect to a reference function will change.
Any suggestion to solve this? (I thought of using the backtrace_symbols
function to get the names of the functions instead of return addresses, but this function will return names only if they are available, for instance if the program has been compiled with -g
).
Upvotes: 0
Views: 148
Reputation: 126408
On Linux, I would suggest also looking at /proc/self/maps (or /proc/pid/maps if you're looking at another process) to get the load map of all the dynamic libs, as well as the main executable. You can then map the void *'s in the backtrace to which object they are part of, and offsets from the start of the object.
Something similar may be available on other OSes (many UNIX variants have a /proc filesystem that may contain similar information)
Upvotes: 1