1

My goal is to record the number of processor instructions executed by a given binary program through the duration of its run. While it's easy to get the actual machine code from the source code (through gdb or any other disassembler), this does not take into account function calls and branches within the program that cause instructions to be executed more than once or skipped altogether.

Is there a straightforward solution to this?

4
  • What is this machine code? What other code does a machine run? Feb 8, 2011 at 17:45
  • 1
    While the code is running on a real machine - easy. To do this with static analysis - hard. You would end up buidling something very like QEMU Feb 8, 2011 at 18:03
  • This is on Ubuntu with an x86 processor. Feb 9, 2011 at 14:56
  • Also, yes, this is with the code running on the machine. Feb 9, 2011 at 15:43

3 Answers 3

1

This is very hardware specific, but most processors offer a facility that counts the exact number of machine instructions (and other events) that have flowed through them. That's how profilers work to capture things like cache misses: by querying these internal registers.

The PAPI library provides calls to query this data on a variety of major processors. If you're on Linux+x86, PerfSuite gives you some more high-level tools which may be easier to start with.

Intel has a monitor app you can use to watch the chip's internal counters in realtime, and their Performance Analysis Guide describes the various Performance Monitoring Units on the chip and how to read them.

1

If you're on Linux, you should be able to run your program through cachegrind to get instruction counts.

It may also be possible to use ollydbg's Run Trace function to obtain an instruction count, but that may be limited by memory.

Alternately, it is possible to write a small debugger that simply runs the program in single steps.

1

The raw tools for tracking system calls are platform specific.

  • Solaris: truss or dtrace
  • MacOS X: dtrace
  • Linux: strace
  • HP-UX: tusc
  • AIX: truss
  • Windows: ...

For example (Solaris):

truss -o ls.truss ls $HOME

This will capture all the system calls made by ls as it lists your home directory.

OTOH, this may not be what you're after...in which case it is of limited value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.