2013-03-20 26 views
1

所以,我已經閱讀了這篇文章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進行時序分析,但首先要做的第一件事就是嘗試計算執行指令的數量

謝謝!

回答

0

我現在有點想通了。所以即使靜態鏈接你的代碼,你也要儘量避免庫被動態地鏈接到你的程序中,因此也包含在你的計數中。然而,它的另一方面是執行你的文件,或者基本上在操作系統下調用也需要大量的指令。因此,基本上,只要指令數在相同條件下保持不變並且相同,則可以從原始程序中減去此計數(例如,使用返回0程序),以計算實際的指令數。

+0

您的項目是如何運作的?你有沒有發佈任何關於它? – 2015-09-17 11:34:26

相關問題