Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to trace a specific thread from another thread.(Both are created from same parent)
1) Is it possible?


I wrote following code here; But from "thread_func_tracer" I got error no : 1, that is insufficient permission; i.e ptrace(PTRACE_ATTACH...) is failing with error code 1.

Can any one help?

C++
  1 #include <stdio.h>
  2 #include <pthread.h>
  3 #include <sys/ptrace.h>
  4 #include <sys/types.h>
  5 #include <sys/wait.h>
  6 #include <unistd.h>
  7 #include <sys/signal.h>
  8 #include <syscall.h>
  9 #include <unistd.h>
 10 #include <sys/user.h>
 11 #include <stddef.h>
 12 #include <errno.h>
 13 
 14 void *thread_func_one(void *);
 15 void* thread_func_tracer(void *);
 16 
 17 int g_iVar_one = 0;
 18 
 19 typedef struct {
 20      pid_t childProcess;
 21      pid_t child_tid1;
 22 }parent_action_args;
 23 
 24 parent_action_args parent_arg_params = {0};
 25 
 26 int main(int argc, char* argv[])
 27 {
 28         pthread_t threadOne, thread_tracer;
 29         int iRet = -1, iRet2 = -1;
 30
31         iRet = pthread_create(&threadOne, NULL, thread_func_one, NULL);
 32         sleep(1);
 33         iRet2 = pthread_create(&thread_tracer, NULL, thread_func_tracer, NULL);
 34 
 35         pthread_join(threadOne, NULL);
 36         pthread_join(thread_tracer, NULL);
 37 
 38         return 0;
 39 }
 40 
 41 void *thread_func_one(void *pParam)
 42 {
 43     int iFlag = 0;
 44     parent_arg_params.childProcess = syscall (SYS_gettid);
 45     printf("<tid> = %d\n", parent_arg_params.childProcess);
 46     while(10 > iFlag)
 47     {
 48         g_iVar_one++;
 49         iFlag++;
 50         printf("g_iVar_one = %d\n", iFlag);
 51         sleep(1);
 52     }
 53 
 54 }
 55 
 56 void *thread_func_tracer(void *pParam)
 57 {
 58 
 59     printf("thread_func_tracer<tid> = %d\n", parent_arg_params.childProcess);
 60     if(ptrace(PTRACE_ATTACH, parent_arg_params.childProcess, NULL, NULL))
 61     {
 62          printf("MKH _ ATTACH-FAIL err =%d\n",errno);
 63     }
 64     wait(NULL);
 65 }
Posted
Updated 19-Sep-13 0:04am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900