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:
Address = 0x60104c
And the error is:
Error at PEEKTEXT
No such process
ESRCH ERROR
***DATA retrieved is: -1
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
Use waitpid
with the following options:
waitpid(pid, &status, WUNTRACED | WCONTINUED)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With