0

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?

mg30rg
  • 1,311
  • 13
  • 24
wangxf
  • 160
  • 1
  • 11
  • You can use a Mutex for interprocess communication. Just pass your mutex to the child process, then wait for it in your child process background thread without locking it (Obtain and instantly release it.) This way your if your parent process locks (obtains) the mutex, the child process stops and if it releases the mutex, the child process resumes. – mg30rg Jun 10 '15 at 13:30
  • @mg30rg I can not do this, because I want implement this in a debugger, the parent process is a debugger, and the child process is being debug, so I can not modify the source. – wangxf Jun 10 '15 at 13:34
  • Then modify your question accordingly. That is a vital detail worth mentioning. – mg30rg Jun 10 '15 at 13:35
  • @mg30rg ok, I will add this detail – wangxf Jun 10 '15 at 13:43
  • There's some good information in the answers to this question: [How to use PTRACE to get a consistent view of multiple threads](http://stackoverflow.com/questions/18577956/how-to-use-ptrace-to-get-a-consistent-view-of-multiple-threads) – Mark Plotnick Jul 06 '15 at 01:05

0 Answers0