2017-05-23 67 views
0

請任何人告訴我,爲什麼下面的代碼不能在64位linux上工作 父進程會改變tchild中的數據值,通過ptrace.initially子進程正常執行,並通過信號掛起進程並更改tchild程序中的數據。ptrace在64位不工作

#include <stdio.h> 
    #include <unistd.h> 
    #include <signal.h> 
    #include <sys/ptrace.h> 
    #include <sys/stat.h> 
    #include <sys/types.h> 
    #include <stdlib.h> 
    #include <wait.h> 
    #include <linux/user.h> 
    int main() 
    { 
     struct user_regs_struct regs; 
     int pid, status; /* process id & status */ 
     pid = fork(); /* create new process */ 
     int data; 

     if(pid == 0) { 
    ptrace(PTRACE_TRACEME, 0, 0, 0); 
     if(execl("/home/neeraj/neerajgit/ptrace/tchild", "tchild", 0) == -1)   
     { 
      fprintf(stderr, "exec err \n"); /* err msg */ 
      exit(EXIT_FAILURE); 
     } 
    } 
    else if(pid < 0) { 
     fprintf(stderr, "fork err\n"); 
    } 
    else { 
     wait(&status); 

     if(WIFSTOPPED(status)) { printf("child stopped \n"); } 

     printf("parent start\n"); 
     kill(pid, SIGSTOP); 
     data = ptrace(PTRACE_GETREGS, pid, 0,&regs); printf("%d\n", data); 
     data = 30; 
     ptrace(PTRACE_POKEDATA, pid, 201010 + 8 , &data); 




    ptrace(PTRACE_PEEKDATA, pid, 201010 + 8, NULL); printf("%d\n", data); 
    printf("child started\n"); 
    printf("%ld \n", regs.rbx); 
    ptrace(PTRACE_CONT, pid, 0, 0); 
    sleep(5); 
    } 

    this is the tchild program 

    #include <stdio.h> 
    #include <sys/ptrace.h> 
    int data; 
    data = 20; /* tchild main */ 
    int main() 
    {  printf("child started \n"); 
    while(data != 30) ; 
    printf("child stopped %d\n", data); 
    } 
+0

(1)您如何知道變量'data'在子進程中的地址爲'201010 + 8'? (2)編譯'tchild'時,編譯器有權假定'data'永遠不會改變。我敢打賭,如果你將程序集轉儲爲'tchild',你將會看到一個無條件的無限循環,甚至不會看'數據'。 – zwol

+0

(3)與'ptrace'有關的一切都是[黑色藝術](http://www.jargon.net/jargonfile/b/blackart.html)。你有沒有其他的方法來實現你的更大目標? – zwol

回答

1

看起來你忘了在父進程中附加目標進程。您還需要等待跟蹤的程序在發送信號後停止

ptrace(PTRACE_ATTACH, pid, 0, 0); 
wait(&status); 
printf("parent start\n"); 

if (WIFSTOPPED(status)) { printf("child stopped \n"); } 

data = ptrace(PTRACE_GETREGS, pid, 0,&regs); printf("%d\n", data); 
data = 30; 
ptrace(PTRACE_POKEDATA, pid, 201010 + 8 , &data); ) 
相關問題