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) )
可以很容易发现pthread_self获取的是pthread_t(占用8Byte),而其他两个函数返回的都是pid_t(占用4Byte),上面我
们讲了,pthread_create创建的线程实际上是一个子进程,而这个子进程的Handle的地址为pthread_t,子进程ID通过gettid
获取,该子进程的父进程ID通过getpid获取。
这个函数比较复杂,具体的一些介绍,大家可以看这个:
输出为(注意不要结束此进程,不然没办法跟踪):
下面是跟踪程序:
输出为:
即可查出pthread_create创建的线程的寄存器信息。
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创建一个子进程作为线程的。下面是这两个函数的声明:
- #include <sys/types.h>
- #include <unistd.h>
- pid_t getpid(void);
- #include <sys/types.h>
- pid_t gettid(void);
- #include <pthread.h>
- pthread_t pthread_self(void);
们讲了,pthread_create创建的线程实际上是一个子进程,而这个子进程的Handle的地址为pthread_t,子进程ID通过gettid
获取,该子进程的父进程ID通过getpid获取。
3.ptrace函数:
- #include <sys/ptrace.h>
- long ptrace(enum __ptrace_request request, pid_t pid, void *addr, void *data);
man ptrace
ptrace用法
4.通过ptrace获取指定pthread线程的寄存器信息:
这个是被跟踪的进程的程序:
- #include <stdio.h>
- #include <unistd.h> //for sleep
- #include <stdlib.h> //for exit
- #include <pthread.h>//for pthread
- #include <errno.h> //for errno
- #include <sys/syscall.h> //for gettid
- #define gettid() syscall(__NR_gettid)
- void *func(void *para)
- {
- printf("Hello world.\n");
- printf("child process tid: %u\n", gettid());
- sleep(-1); // 该进程一直sleep,等待
- return NULL;
- }
- int main()
- {
- pthread_t tid;
- int ret = pthread_create(&tid, NULL, func, NULL);
- if(ret != 0)
- {
- exit(errno);
- }
- printf("parent process pid: %u\n", getpid());
- pthread_join(tid, NULL);
- return 0;
- }
- [biz@localhost test]$ g++ main.cpp -lpthread
- [biz@localhost test]$ ./a.out
- parent process pid: 14396
- child process tid: 14397
- sleep ...
下面是跟踪程序:
- #include <sys/ptrace.h>
- #include <stdlib.h> // for atoi
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <sys/user.h> /* For user_regs_struct */
- int main(int argc, char *argv[])
- {
- pid_t traced_process;
- struct user_regs_struct regs;
- long ins;
- if(argc != 2) {
- printf("Usage: %s <pid to be traced> ",
- argv[0], argv[1]);
- exit(1);
- }
- traced_process = atoi(argv[1]);
- ptrace(PTRACE_ATTACH, traced_process,
- NULL, NULL);
- wait(NULL);
- ptrace(PTRACE_GETREGS, traced_process,
- NULL, ®s);
- ins = ptrace(PTRACE_PEEKTEXT, traced_process,
- regs.rip, NULL);
- printf("EIP: %lx Instruction executed: %lx \n",
- regs.rip, ins);
- ptrace(PTRACE_DETACH, traced_process,
- NULL, NULL);
- return 0;
- }
输出为:
- [biz@localhost test]$ g++ attach.cpp -o attach
- [biz@localhost test]$ ./attach 14397
- EIP: 33d98ab91d Instruction executed: e8c28948243c8b48
即可查出pthread_create创建的线程的寄存器信息。
0 0
- Linux中使用ptrace获取pthread线程的寄存器信息
- Linux中使用ptrace获取pthread线程的寄存器信息
- Linux中使用ptrace获取pthread线程的寄存器信息
- linux之通过ptrace获取指定pthread线程的寄存器信息
- Linux中pthread线程使用详解
- [Pthread] Linux上程序调试的基石(一)--ptrace
- [Pthread] Linux上程序调试的基石(一)--ptrace
- [Ptrace]Linux内存替换(二)信息获取
- Linux中基于ptrace的外挂程序设计
- Linux 中 pthread 线程库历史
- linux下使用pthread库函数创建线程
- 不要在pthread线程中使用printf()
- Linux下pthread的线程亲和性研究
- Linux下pthread的线程亲和性研究
- 获取线程中抛出的异常信息
- 获取java线程中信息的方法
- windows下pthread线程库的使用
- Linux线程pthread笔记
- 开发中的问题:adb.exe' and can be executed
- LightOJ 1037 - Agent 47 (状压dp)
- jquery幻灯片更新版
- 欢迎使用CSDN-markdown编辑器
- 贪心算法之背包问题
- Linux中使用ptrace获取pthread线程的寄存器信息
- 自己动手 CentOS-6.5 安装Oracle11g R2
- webshell定时器与CAS单点冲突的奇葩问题
- Ckeditor在线文本编辑器
- 我们为什么要现在就开始写博客
- juzhen
- shell 脚本查找、抽取指定字符串的方法
- FZU1753 Another Easy Problem【组合数】
- mysql 时间类型 及时间函数的使用