【发布时间】:2013-12-16 00:42:56
【问题描述】:
我以./main & 身份运行我的进程
它给了我一个如下所示的地址:[1] 4257
然后在一个新终端上我这样做:./tracer 4257
这行代码返回-1
ptrace(PTRACE_ATTACH, traced_process, NULL, NULL);
main.c
int main()
{
int i;
for(i = 0; i < 10; i++)
{
printf("Hello World\n");
sleep(5);
}
return 0;
}
tracer.c
#include <sys/ptrace.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;
if(argc != 2)
{
printf("Usage: %s <pid to be traced>\n", argv[0], argv[1]);
exit(1);
}
traced_process = atoi(argv[1]);
long t = 0;
t = ptrace(PTRACE_ATTACH, traced_process, NULL, NULL);
if(t < 0)
printf("-1\n");
wait(NULL);
ptrace(PTRACE_GETREGS, traced_process, NULL, ®s);
long ins = ptrace(PTRACE_PEEKTEXT, traced_process, regs.eip, NULL);
if(ins < 0)
printf("-1\n");
printf("EIP: %lx Instruction executed: %lx\n", regs.eip, ins);
ptrace(PTRACE_DETACH, traced_process, NULL, NULL);
return 0;
}
我该如何解决这个问题?
【问题讨论】: