Linux中使用ptrace获取pthread线程的寄存器信息

来源:互联网 发布:php获取访客唯一标识 编辑:程序博客网 时间:2024/06/05 12:44

 
内核CLinuxpthread多线程
1.与本文相关的有这几个重要的函数:
pthread_create:创建线程
pthread_self:获取当前运行的线程的线程ID (The pthread_self() function returns the ID of the calling thread.)
getpid:获取当前进程ID
gettid:获取当前运行的线程的线程ID(gettid() returns the caller’s thread ID (TID) )

ptrace:跟踪一个指定的进程


2.getpid、gettid和pthread_self的区别:
pthread_self属于POSIX线程库的函数,Linux下POSIX的pthread线程库是用进程来实现的,pthread_create是通过系统调用
clone创建一个子进程作为线程的。下面是这两个函数的声明:

[cpp] view plaincopyprint?
  1. #include <sys/types.h>  
  2. #include <unistd.h>  
  3. pid_t getpid(void);  
  4.   
  5. #include <sys/types.h>  
  6. pid_t gettid(void);  
  7.   
  8. #include <pthread.h>  
  9. pthread_t pthread_self(void);  
可以很容易发现pthread_self获取的是pthread_t(占用8Byte),而其他两个函数返回的都是pid_t(占用4Byte),上面我
们讲了,pthread_create创建的线程实际上是一个子进程,而这个子进程的Handle的地址为pthread_t,子进程ID通过gettid
获取,该子进程的父进程ID通过getpid获取。

3.ptrace函数:

[cpp] view plaincopyprint?
  1. #include <sys/ptrace.h>  
  2. long ptrace(enum __ptrace_request request, pid_t pid, void *addr, void *data);  
这个函数比较复杂,具体的一些介绍,大家可以看这个:

man ptrace

ptrace用法


4.通过ptrace获取指定pthread线程的寄存器信息:
这个是被跟踪的进程的程序:

[cpp] view plaincopyprint?
  1. #include <stdio.h>  
  2. #include <unistd.h> //for sleep  
  3. #include <stdlib.h> //for exit  
  4. #include <pthread.h>//for pthread  
  5. #include <errno.h>  //for errno  
  6. #include <sys/syscall.h> //for gettid  
  7. #define gettid() syscall(__NR_gettid)  
  8.   
  9. void *func(void *para)  
  10. {  
  11.     printf("Hello world.\n");  
  12.     printf("child process tid: %u\n", gettid());  
  13.     sleep(-1);  // 该进程一直sleep,等待  
  14.     return NULL;  
  15. }  
  16.   
  17. int main()  
  18. {   
  19.     pthread_t tid;  
  20.     int ret = pthread_create(&tid, NULL, func, NULL);  
  21.     if(ret != 0)  
  22.     {     
  23.         exit(errno);  
  24.     }     
  25.     printf("parent process pid: %u\n", getpid());  
  26.   
  27.     pthread_join(tid, NULL);  
  28.     return 0;  
  29. }  
输出为(注意不要结束此进程,不然没办法跟踪):
[cpp] view plaincopyprint?
  1. [biz@localhost test]$ g++ main.cpp -lpthread  
  2. [biz@localhost test]$ ./a.out   
  3. parent process pid: 14396  
  4. child process tid: 14397  
  5. sleep ...  

下面是跟踪程序:
[cpp] view plaincopyprint?
  1. #include <sys/ptrace.h>  
  2. #include <stdlib.h>     // for atoi  
  3. #include <stdio.h>  
  4. #include <sys/types.h>  
  5. #include <sys/wait.h>  
  6. #include <unistd.h>  
  7. #include <sys/user.h> /* For user_regs_struct */  
  8. int main(int argc, char *argv[])  
  9. {   
  10.     pid_t traced_process;  
  11.     struct user_regs_struct regs;  
  12.     long ins;  
  13.     if(argc != 2) {  
  14.         printf("Usage: %s <pid to be traced> ",  
  15.                argv[0], argv[1]);  
  16.         exit(1);  
  17.     }     
  18.     traced_process = atoi(argv[1]);  
  19.     ptrace(PTRACE_ATTACH, traced_process,   
  20.            NULL, NULL);  
  21.     wait(NULL);  
  22.     ptrace(PTRACE_GETREGS, traced_process,   
  23.            NULL, ®s);  
  24.     ins = ptrace(PTRACE_PEEKTEXT, traced_process,   
  25.                  regs.rip, NULL);  
  26.     printf("EIP: %lx Instruction executed: %lx \n",   
  27.            regs.rip, ins);  
  28.     ptrace(PTRACE_DETACH, traced_process,   
  29.            NULL, NULL);  
  30.     return 0;  
  31. }  

输出为:
[cpp] view plaincopyprint?
  1. [biz@localhost test]$ g++ attach.cpp -o attach  
  2. [biz@localhost test]$ ./attach 14397  
  3. EIP: 33d98ab91d Instruction executed: e8c28948243c8b48  

即可查出pthread_create创建的线程的寄存器信息。


0 0
原创粉丝点击