所以,我已經閱讀了這篇文章Counting machine instructions of a process using PTRACE_SINGLESTEP,我明白動態鏈接一個測試程序到我的ptrace程序將返回一個指令計數,它也計算運行時庫的初始化。不過,我想獲得一個有效的計數爲我的測試程序,它是:爲什麼ptrace singlestep在靜態鏈接時會返回太大的指令數?
int main(){
return 0;
}
我ptrace的程序首先也回到90K +值,所以我改成了靜態鏈接的使用testprogram。櫃檯現在少了,但仍然超過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進行時序分析,但首先要做的第一件事就是嘗試計算執行指令的數量
謝謝!
您的項目是如何運作的?你有沒有發佈任何關於它? – 2015-09-17 11:34:26