所以,我已经阅读了这篇文章Counting machine instructions of a process using PTRACE_SINGLESTEP,我明白将test程序动态链接到我的ptrace程序将返回一个指令计数,该计数也计算运行时库的初始化。但是,我正在尝试为我的测试程序获取有效计数,即:
int main(){
return 0;
}
我的ptrace程序首先还返回了90k +值,因此我将其更改为静态链接使用过的测试程序。现在柜台较少,但仍然超过12k。我用来计算指令的程序是:
#include <sys/ptrace.h>
#include <unistd.h>
#include <stdio.h>
int main() {
long long counter = 1; // machine instruction counter
int wait_val; // child's return value
int pid; // child's process id
int dat;
switch (pid = fork()) { // copy entire parent space in child
case -1: perror("fork");
break;
case 0: // child process starts
ptrace(PTRACE_TRACEME,0,NULL,NULL);
/*
must be called in order to allow the
control over the child process and trace me (child)
0 refers to the parent pid
*/
execl("./returntestprog","returntestprog",NULL);
/*
executes the testprogram and causes
the child to stop and send a signal
to the parent, the parent can now
switch to PTRACE_SINGLESTEP
*/
break;
// child process ends
default: // parent process starts
wait(&wait_val);
if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0)
perror("ptrace");
/*
switch to singlestep tracing and
release child
if unable call error.
*/
wait(&wait_val);
// parent waits for child to stop at next
// instruction (execl())
while (wait_val == 1407) {
counter++;
if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0)
perror("ptrace");
/*
switch to singlestep tracing and
release child
if unable call error.
*/
wait(&wait_val);
// wait for next instruction to complete */
}
/*
continue to stop, wait and release until
the child is finished; wait_val != 1407
Low=0177L and High=05 (SIGTRAP)
*/
}
printf("Number of machine instructions : %lld\n", counter);
return 0;
} // end of switch
任何帮助都会非常感激,因为我不太确定它是否正常工作,或者根本不工作。一旦我开始这个事情,我想用ptrace进行时序分析,但首先要先尝试计算执行指令的数量
谢谢!
答案 0 :(得分:0)
我现在有点想通了。因此,即使静态链接代码,您也会尝试阻止库动态链接到您的程序,因此也会包含在您的计数中。然而,另一方面是执行文件,或者基本上在操作系统下调用也需要大量的指令。所以基本上,只要指令计数在相同条件下是常数且相同,您就可以从原始程序中减去此计数(例如,当使用返回0程序时),以计算实际指令数。