Ptrace未能连接

Ptrace未能连接,c,ubuntu,ptrace,C,Ubuntu,Ptrace,我以/main& 它给了我一个类似这样的地址:[1]4257 然后在一个新的终端上我这样做:/tracer4257 这行代码返回-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); }

我以
/main&

它给了我一个类似这样的地址:
[1]4257

然后在一个新的终端上我这样做:
/tracer4257

这行代码返回-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;
}
intmain()
{
int i;
对于(i=0;i<10;i++)
{
printf(“Hello World\n”);
睡眠(5);
}
返回0;
}

示踪剂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, &regs);
   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;
}
#包括
#包括
#包括
#包括
#包含//用于用户\u注册表\u结构
int main(int argc,char*argv[])
{  
pid跟踪过程;
结构用户\u注册表\u结构注册表;
如果(argc!=2)
{
printf(“用法:%s\n”,argv[0],argv[1]);
出口(1);
}
跟踪_过程=atoi(argv[1]);
长t=0;
t=ptrace(ptrace\u-ATTACH,tracked\u-process,NULL,NULL);
if(t<0)
printf(“-1\n”);
等待(空);
ptrace(ptrace_GETREGS、tracked_process、NULL和regs);
long-ins=ptrace(ptrace_-peek-text,跟踪_-process,regs.eip,NULL);
如果(ins<0)
printf(“-1\n”);
printf(“执行的EIP:%lx指令:%lx\n”,regs.EIP,ins);
ptrace(ptrace_分离,跟踪_进程,NULL,NULL);
返回0;
}

如何解决这个问题?

如果没有直接的父子进程关系(或者您不是root用户),Ubuntu会限制其他程序通过ptrace连接的能力

看看


基本上,您需要允许跟踪,或者通过执行echo 0>/proc/sys/kernel/yama/ptrace_scope来禁用保护系统范围

我必须执行echo 0 | sudo tee/proc/sys/kernel/yama/ptrace_scope但现在它在这一行上返回-1
ptrace(ptrace_GETREGS,tracked_process,NULL,®s)对不起,我不知道这个问题:((但是ptrace命令设置了errno,所以你应该可以从那里学到更多的东西)我会设法解决的。谢谢你,你帮了我很多忙!