Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No such process - ptrace

Problem statement: a program C is composed of a loop. The execution of this program must be controlled by another process that will periodically display the progress of the controlled process. After kill(pid, SIGSTOP), the function ptrace(PTRACE_PEEKTEXT,pid,...) does not find anymore the C process. From what I have read, ptrace(PTRACE_PEEKTEXT,pid,...) is supposed to work when the process identified by pid is stopped.

I do not know what exactly I am missing, so any help will be much appreciated. Below is what I have done so far:

There are two processes, P and C.

The first process (P) creates the second one (C) via fork() .

C's code is like this:

int i = 0;
int main() {
     ptrace(PTRACE_TRACEME, 0, NULL, NULL);
     printf("Memory address = %p", (void *)&i);
     while(1) { i++;}
}

P's code is below:

{...}
switch (pid = fork()) {
case 0: /* the child */           
       if (execl("C", "", (char *) NULL) == -1)  {
        perror("execl");
    }
    break;

case -1: /* Error */
    perror("fork");
    exit(EXIT_FAILURE);

default: /* the parent */  
        sleep(1);
        kill(pid, SIGSTOP);
        wait(&status);            
        if (WSTOPSIG(status)==SIGSTOP) {
                printf("%s","Child was interrupted. Insert memory address\n");

            scanf("%p",&address);
            printf("Address = %p", address);               

            data = ptrace(PTRACE_PEEKTEXT, pid, address,NULL);        
            if(data==-1){
               if(errno){
                printf("%s\n","Error at PEEKTEXT\n");    
                printf("%s\n",strerror(errno));    
               }
               if(errno ==     ESRCH){
                printf("%s\n","ESRCH error\n");    
               }
                if(errno == EIO){
                printf("%s\n","EIO error\n");    
               }
            }
            printf("***Data retrieved is: %ld\n",data);
            data = ptrace(PTRACE_CONT, pid, 0, 0);

        }    
        if(WIFEXITED(status)){
            printf("[Parent] - Child's exit status is: %d \n", WEXITSTATUS(status));
            break;
        }
    break;        
} 
{...}

The output is:

  1. From C: Memory Address = 0x60104c
  2. From P: Child was interrupted. Insert memory address (next I insert what C printed)

Address = 0x60104c

And the error is:

Error at PEEKTEXT

No such process

ESRCH ERROR

***DATA retrieved is: -1

like image 845
Mirel Vlad Avatar asked Oct 17 '14 19:10

Mirel Vlad


2 Answers

Make sure the address variable is of the correct type. I tried with void * address and it worked for me also. I got your error by using int for address variable

like image 190
Roxana Roman Avatar answered Sep 21 '22 07:09

Roxana Roman


Use waitpid with the following options:

waitpid(pid, &status, WUNTRACED | WCONTINUED)
like image 36
Nima Gougol Avatar answered Sep 24 '22 07:09

Nima Gougol