在Linux机器上,我使用带有PTRACE_SINGLESTEP参数的ptrace来计算程序的机器指令数。 我遵循了这篇文章: http : //www.ncsu.edu/it/mirror/ldp/LDP/LGNET/81/sandeep.html 。
然而,结果对我来说似乎很奇怪。 对于一个非常简单的程序,计算超过95000台机器指令。 测试程序是
int main(void) { return 23; }这里发生了什么? 该文章的代码是错误的吗? (我看不出它有什么问题。)如果没有,是什么导致这么简单的程序要求> 95000指令?
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?
最满意答案
您正在编译的C程序链接到C库。 它包含程序执行开始的_start符号。 此时,C库初始化自己并最终调用main 。 在main返回之后,控制流回到_start并且还有一堆其他指令要执行并返回程序返回值。 请注意,连续使用PTRACE_SINGLESTEP不会计算编译指令的数量。 它计算执行指令的数量。 这意味着在进入main之前,执行main和退出main之后执行95k指令。
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.
更多推荐
发布评论