使用PTRACE

编程入门 行业动态 更新时间:2024-10-24 16:23:09
使用PTRACE_SINGLESTEP计算进程的机器指令(Counting machine instructions of a process using PTRACE_SINGLESTEP)

在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.

更多推荐

本文发布于:2023-08-04 07:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PTRACE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!