I'm currently researching this interesting question and haven't found much material on it: How can one best programmatically observe the execution of a program, much like single-stepping it in the debugger, but wholesale and automatically?
Rigging up a debugger was my first idea, as they do the same thing for human consumption, but that doesn't seem to be a normal use scenario, and hence no APIs. Alternatively one could extend some virtualization software to capture what the processor is doing. I would settle for source modification, which would work for just capturing function calls, but that brings in other complications. Finally there's the issue of languages: I was thinking of simple C programs, although it would probably be easier in Java or Lisp, but I'd like to stay general.
Have you ever solved this or what approach would you try?