【发布时间】:2020-11-15 17:01:30
【问题描述】:
我编写了以下一段代码,它逐步遍历 /bin/ls 并计算其指令:
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <sys/syscall.h>
int main()
{
pid_t child;
child = fork(); //create child
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
char* child_argv[] = {"/bin/ls", NULL};
execv("/bin/ls", child_argv);
}
else {
int status;
long long ins_count = 0;
while(1)
{
//stop tracing if child terminated successfully
wait(&status);
if(WIFEXITED(status))
break;
ins_count++;
ptrace(PTRACE_SINGLESTEP, child, NULL, NULL);
}
printf("\n%lld Instructions executed.\n", ins_count);
}
return 0;
}
运行这段代码可以让我执行大约 500.000 条指令。据我所知,这些指令中的大部分应该来自动态链接器。当我使用带有 qemu-x86_64 -singlestep -D log -d in_asm /bin/ls 的 QEMU 跟踪 /bin/ls 时,我执行了大约 17.000 条指令。我必须调整什么才能在 QEMU 所做的相同点开始和停止计数? (又名。计算相同的指令)。
我用 QEMU 跟踪了一个“return null”程序,它产生了 7840 条指令,而我的代码给了我 109025,因此 QEMU 似乎跟踪的比 main 多,但比我的代码少。
我的目标是稍后比较这些指令,这就是为什么我想迭代像 QEMU 这样的相同指令。
【问题讨论】:
-
有点像 ptrace 计算内核中的周期,而 qemu 没有。无论如何,17.000 个周期对于完成所有内核工作来说有点少。
标签: c linux qemu instructions ptrace