linux之通过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;
}

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h> /**//* For user_regs_struct 
                             etc. */
#include <stdio.h>
#include <stdlib.h>

//http://www.cnblogs.com/wangkangluo1/archive/2012/06/05/2535484.html
//http://blog.csdn.net/sealyao/article/details/6710772
//通过ptrace获取指定pthread线程的寄存器信息:
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,
		&regs);
	ins = ptrace(PTRACE_PEEKTEXT,
		traced_process, 
		regs.eip,
		NULL);
	printf("EIP: %lx Instruction executed: %lx \n", 
		regs.eip,
		ins);
	ptrace(PTRACE_DETACH,
		traced_process, NULL,NULL);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值