2017-05-23 66 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); ) 
相关问题