请告诉我为什么下面的代码不能在64位linux上运行
父进程将通过ptrace更改tchild中的数据值。最初子进程正常执行,并通过信号挂起进程并更改tchild程序中的数据。

  #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);
   }

最佳答案

似乎您忘记在父进程中附加目标进程。您还需要等待跟踪程序在发送信号后停止。

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 );  )

关于c - ptrace无法在64位中工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44138020/

10-16 20:31