I am trying, as an exercise, to make some sort of custom profiler for binaries in C, using the ptrace
api. I assume all binaries to be traced have been statically linked, I have access to tools such as nm(1)
, objdump
and readelf
and use a Linux, x86, 32 bit system.
In the current phase I am trying to create a dynamic call tree/graph (relative calls only) of the traced process and include the total number of instructions executed in each function call. In order to do that, I tried to:
- Retrieve all user defined symbols in the ELF file using
nm(1)
as well as their addresses - Use ptrace to step through the code and identify call and ret restructions
- After each call, use the
rip
register to figure out the address of the current instruction and within which function this instruction is; thus deducing the corresponding symbol name.
My question is relative to this last point. I was wondering if there is a way, using the ptrace api, to identify the call instruction as well as the address of the function to which the execution will jump; or even better directly the symbol name which corresponds to this function.
I have tried reading the documentation for ptrace but it is, at least for me, far from clear. Is there a standard approach to what I am trying to do? Is my approach maybe completely wrong?