How to control the thread of child process

165 views Asked by At

I'm trying to implement the following function in a debugger: I want to use the debugger to control a thread in the process which is being debugged.
The debugger is the parent process and uses the ptrace() function to debug the child process, but I don't know how to control a thread (other than the main) of the child process from the parent process. I want to make the target thread halt or continue. Is there any way to do this? just like the code below

#include<pthread.h>
#include<stdio.h>

void *runner(void *param);
int main(int argc,char *argv[])
{
    int pid;
    pthread_t tid;
    pthread_attr_t attr;
    pid=fork();
    if(pid==0){
        pthread_attr_init(&attr);
        pthread_create(&tid,&attr,runner,NULL);
        pthread_join(tid,NULL);
        printf("CHILD VALUE=%d",value);
    }
    else if(pid>0){
        wait(NULL);
        printf("PARENT VALUE=%d",value);
    }
}


void *runner(void *param){
    int count = 0;
    while(1)
    { 
         printf("%d\n",count);
         count++;
         sleep(1);
    }
    pthread_exit(0);
}

In the code, the child process creates a thread to print numbers in a loop. Can I use some function like ptrace() in the parent process to control the thread? Make it suspend or continue?

0

There are 0 answers