For a University assignment for a course called Real-Time Systems we had to implement a prototype for timing analysis by tracing instructions.
The idea is that when executing a program, ptrace controls the execution and stops after each instruction. After this the actual instruction in the memory can be fetched. Knowing which instruction is executed you could associate this with a certain time, add all times together for each instruction and you could dynamically determine how long a program would run: profit!
I made a prototype that can be viewed on GitHub.