使用多线程应用程序中的ptrace

pet*_*ohn 5 c c++ linux multithreading ptrace

我想用来ptrace检查我的程序生成的程序所调用的系统.我从本教程开始,因为它是在我之前的问题的答案中解释的.我修改了代码,使其适应我正在使用的平台(SLES 11 64位),并将以下测试代码放在一起,打印出生成的进程所做的每个系统调用:

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/reg.h>
#include <sys/syscall.h>   /* For SYS_write etc */

pid_t child;

void run()
{
    long orig_eax;
    int status;

     while(1) {
          int pid = wait(&status);
          if (pid == -1) {
              perror("wait");
              kill(child, SIGKILL);
              return;
          }
          printf("Got event from %d.\n", pid);
          if(WIFEXITED(status))
              break;
          orig_eax = ptrace(PTRACE_PEEKUSER,
                     pid, 8 * ORIG_RAX, NULL);
          if (orig_eax == -1) {
              perror("ptrace");
              kill(child, SIGKILL);
              return;
          } else {
              printf("Syscall %ld called.\n", orig_eax);
          }
            ptrace(PTRACE_SYSCALL,
                   pid, NULL, NULL);
    }
}

int main(int /*argc*/, char* argv[])
{

    child = fork();
    if(child == 0) {
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);
        execl(argv[1], argv[1], NULL);
    }
    else {
        printf("Child process id = %d.\n", child);
        run();

    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它工作得很好:它打印程序发出的系统调用的id(实际上它打印两次,一次在入口处,一次用于退出,但现在无关紧要).但是,我的程序除了检查系统调用之外还需要做其他事情,所以我决定将检查移到一个单独的线程(我对C++比C更舒服,所以我用C++方式做到了,但是我不要认为那很重要).当然在这个最好的程序中,我只启动线程然后加入它.

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/reg.h>
#include <sys/syscall.h>   /* For SYS_write etc */

#include <boost/thread.hpp>


pid_t child;

void run()
{
    long orig_eax;
    int status;

     while(1) {
          int pid = wait(&status);
          if (pid == -1) {
              perror("wait");
              kill(child, SIGKILL);
              return;
          }
          printf("Got event from %d.\n", pid);
          if(WIFEXITED(status))
              break;
          orig_eax = ptrace(PTRACE_PEEKUSER,
                     pid, 8 * ORIG_RAX, NULL);
          if (orig_eax == -1) {
              perror("ptrace");
              kill(child, SIGKILL);
              return;
          } else {
              printf("Syscall %ld called.\n", orig_eax);
          }
            ptrace(PTRACE_SYSCALL,
                   pid, NULL, NULL);
    }
}

int main(int /*argc*/, char* argv[])
{

    child = fork();
    if(child == 0) {
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);
        execl(argv[1], argv[1], NULL);
    }
    else {
        printf("Child process id = %d.\n", child);
        boost::thread t(run);
        t.join();
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这次我收到一条错误消息:

Child process id = 24682.
Got event from 24682.
ptrace: No such process
Run Code Online (Sandbox Code Playgroud)

为什么是这样?我试着寻找答案但却没有发现这样的事情.我发现ptrace不会跟踪子进程启动的线程,但这是另一件需要稍后处理的事情.甚至可以从不同的therad检查子进程吗?

另一个奇怪的事情是,在我的实际应用程序中,我基本上做了同样的事情(但是从一个更复杂的上下文:类,互斥体等),我得到了一种不同的错误.而不是ptrace返回错误,wait甚至不返回子进程上的系统调用(并且子进程甚至不会停止).另一方面,wait当子进程退出时,按预期工作.

小智 1

据我所知,ptrace每个进程只允许一个跟踪器。这意味着,如果您尝试附加(您可以尝试使用 强制附加)PTRACE_ATTACH,您将收到一条错误,告知您ptrace无法附加到指定的进程。

因此,出现错误是因为您的线程未附加到子进程,这样,当您尝试这样做时ptrace,它会失败,发送一个-ESRCH代码。

此外,您可以在这里查看这篇文章,它可能会回答您除了这个问题之外的其他一些问题。