2012-04-15 5 views
1

システムコールの数をすべて取得しようとしていますが、ptraceを使用して与えられたプログラムによって呼び出されたシステムコールの名前を取得しようとしています。私は64ビットシステム上にあるので、ORIG_RAX * 8を使用してptraceを使用してシステムコールを検索しています。私は現在、最初のシステムコールしか取得できません。サンプル実行の出力は以下のとおりです。何か案は?Ptraceでシステムコールを取得し、最初の呼び出しの後に停止する

ありがとうございます!

出力: griffinm @も$ G ++ mystrace.cc

~/cs153/assn2 
[email protected] $ a.out ls 
Please wait 
The child made a system call 59 
a.out mystrace.cc mystrace.cc~ 
Number of machine instructions : 252376 
~/cs153/assn2 



#include <stdio.h> 
#include <stdlib.h> 
#include <signal.h> 
#include <sys/ptrace.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <unistd.h> 
#include <errno.h> 
#define ORIG_RAX 120 

int main(int argc, char* argv[]) 
{ 
     long long counter = 0; /* machine instruction counter */ 
     int wait_val;   /* child's return value  */ 
     int pid; 
    long orig_eax;    /* child's process id   */ 

     puts("Please wait"); 

    switch (pid = fork()) { 
    case -1: 
      perror("fork"); 
      break; 
    case 0: 
      ptrace(PTRACE_TRACEME, 0, 0, 0); 

      execvp(argv[1], NULL); 

      break; 

    default: 
      wait(&wait_val); 

    orig_eax = ptrace(PTRACE_PEEKUSER, 
         pid, ORIG_RAX, 
         NULL); 
    printf("The child made a " 
      "system call %ld\n", orig_eax); 

      while (wait_val == 1407) { 
        counter++; 

        if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0) 
          perror("ptrace"); 

        wait(&wait_val); 

      } 

    } 
    printf("Number of machine instructions : %lld\n", counter); 
    return 0; 

}

Update Default Case: 
Default:  
      wait(&wait_val); 


      while (wait_val == 1407) { 
        counter++; 

        if (ptrace(PTRACE_SYSCALL, pid, 0, 0) != 0) 
          perror("ptrace"); 
     orig_eax = ptrace(PTRACE_PEEKUSER, 
         pid, 8*ORIG_RAX, 
         NULL); 
     cout<<orig_eax<<endl; 
        wait(&wait_val); 

      } 

    } 

編集:

Output: 

[email protected] $ a.out pwd 
Please wait 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
-1 
/home/csmajs/griffinm/cs153/assn2 
-1 
-1 
-1 
-1 
-1 
-1 

私は8 * Orig_RAXが問題だと思います、マシンは私が言ったように64ビットです。何か案は?

+0

['wait(2)'](http://linux.die.net/man/2/wait)の戻り値と比較するために番号1407をハードコーディングするべきではありません。これは実装の詳細です。代わりに 'while(WIFSTOPPED(wait_val)&& WSTOPSIG(wait_val)== SIGTRAP)'をテストする必要があります。 –

+0

ありがとう!私の教授はwhileループ条件のためのスケルトンコードをいくつか教えてくれました。私はそれをより一般的にする方法があると感じました。 –

答えて

0

PTRACE_SINGLESTEPの代わりにPTRACE_SYSCALLを使用して、1つの命令ではなく次のシステムコールまで子を実行したいと思うかもしれません。その後、PTRACE_PEEKUSERを再度使用して、それが何のシステムコールであるかを確認することができます。

+0

私はそれを試み、私の元の質問に更新された結果を掲載しました –

関連する問題