【问题标题】:Stack Walk on linux using ptrace使用 ptrace 在 Linux 上进行 Stack Walk
【发布时间】:2010-07-05 12:38:08
【问题描述】:

以下是我的要求。

当进程 A 运行时。

  1. 使用 PTRACE_ATTACH 连接 B 中的进程 A。
  2. 开始循环
  3. 停止进程 A
  4. 读取寄存器
  5. 恢复进程 A
  6. 睡眠(1)
  7. 结束循环
  8. 分离A

我正面临从循环中启动和恢复进程 A 的问题。我尝试了kill(pid,SIGSTOP),kill(pid,SIGCONT),PTRACE_CONT的组合。但没用。

请问还有其他解决方案吗?

提前致谢。 桑迪普

【问题讨论】:

标签: c linux ptrace


【解决方案1】:

以下代码对我有用,并且似乎满足您的要求 -

交流

#include<stdio.h>
int main()
{
   int i=0;
   printf("My PID is - %ld\n",getpid());
   while(i>=0)
   {
   }
   return 0;
}

B.c - 追踪流程

int main()
{
   int pid;
   int status;
   struct user_regs_struct regs;
   unsigned int eip;

   printf("Enter pid to trace : \n");
   scanf("%d",&pid);
   printf("PID to be traced - %ld\n",pid);

   ptrace(PTRACE_ATTACH,pid,0,0);
   if(errno)
   {
        perror("attach");
        return -1;
   }

   waitpid(pid,&status,WUNTRACED);

   printf("Process Stopped\n");
   while(1)
   {
      ptrace(PTRACE_GETREGS,pid,0,&regs);
      eip=ptrace(PTRACE_PEEKTEXT,pid,regs.eip,0);

      printf("EIP - 0x%08x, instruction executed - 0x%08x\n",regs.eip,eip);

      ptrace(PTRACE_CONT,pid,0,0);
      waitpid(pid,&status,WUNTRACED);
   }

   return 0;

}

信号通过 -

杀死 -STOP 17779 杀死 -STOP 17779

A 的输出 -

xxxxx!xxxxx:~/myPer/stack_overflow [135]$ ./A
My PID is - 17779

B 的输出 -

XXXXX!xxxxx:~/myPer/stack_overflow [121]$ ./B
Enter pid to trace :
17779
PID to be traced - 17779
Process Stopped
EIP - 0x080483e1, instruction executed - 0x00f87d83
EIP - 0x080483e5, instruction executed - 0x00b8fa79
EIP - 0x080483e5, instruction executed - 0x00b8fa79

我们看到 B 显示传递给客户端的每个信号的 EIP 值。基本上信号没有传递给 A,而是 B 唤醒并检查 EIP,然后继续循环。您可以根据需要修改代码以传递信号。

这是我从你的问题中理解的。如果我理解其他内容,请告诉我,我会相应地更新答案

【讨论】:

    【解决方案2】:

    听起来像是一个从头开始进行的非常具有挑战性的项目。您是否考虑过以任何方式利用GNU debugger?特别是有一个名为libgdb2 的长期运行的子项目可能适合您的目的,即使此时它还远未完成或稳定。

    【讨论】:

      【解决方案3】:

      您可以尝试使用与许多 IDE 相同的方式编写脚本/与 gdb 交互。另见http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/gdb/gdb-mi.html

      【讨论】:

        猜你喜欢
        • 2011-01-22
        • 2022-08-04
        • 1970-01-01
        • 2022-06-14
        • 2011-07-26
        • 2016-03-26
        • 1970-01-01
        • 2012-04-03
        • 2019-10-02
        相关资源
        最近更新 更多