on a Linux machine, I am using ptrace with the PTRACE_SINGLESTEP parameter to count the number of machine instructions of a program. I followed this article: http://www.ncsu.edu/it/mirror/ldp/LDP/LGNET/81/sandeep.html.
However, the result seems odd to me. For a very simple program, over 95000 machine instructions are counted. The test program is
int main(void) { return 23; }
What's going on here? Is the code from the article wrong? (I can't see what's wrong with it.) If not, what causes such a simple program to require >95000 instructions?
The C program you're compiling is linked to C library. It contains the _start
symbol which the program execution starts from. At that point, C library initializes itself and eventually calls main
. After main
returns, the control flows back to _start
and there are a bunch of other instructions to execute and return the program return value. Note that using PTRACE_SINGLESTEP
successively doesn't count the number of compiled instructions. It counts the number of executed instructions. That means 95k instructions are executed before entering main
, when executing main
and after exiting main
.