Reputation: 6115
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?
Upvotes: 4
Views: 1497
Reputation: 146043
It's due to something called "software bloat". You have to initialize and finalize stdio, and maybe even some threading code that bled into the standard C runtime. If you read a little further and profile it you may find out exactly what. Or you could just read the source.
Update: Actually, I realized later that you have probably been tracing through the operation of the dynamic linker, which has a lot of work to do. I see that someone left such a comment, so I upvoted the comment. If you didn't link the program statically, then both of our original answers were basically wrong.
Upvotes: 1
Reputation: 421988
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
.
Upvotes: 5