7

我在这里按照教程进行了一些修改x86-64(基本上将 eax 替换为 rax 等),以便它编译:

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <unistd.h>


int main()
{   pid_t child;
    long orig_eax;
    child = fork();
    if(child == 0) {
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);
        execl("/bin/ls", "ls", NULL);
    }
    else {
        wait(NULL);
        orig_eax = ptrace(PTRACE_PEEKUSER,
                          child, 4 * ORIG_RAX,
                          NULL);
        printf("The child made a "
               "system call %ld\n", orig_eax);
        ptrace(PTRACE_CONT, child, NULL, NULL);
    }
    return 0;
}

但它实际上并没有按预期工作,它总是说:

The child made a system call -1

代码有什么问题?

4

2 回答 2

6

ptrace 使用 errno EIO 返回 -1,因为您尝试读取的内容未正确对齐。取自 ptrace 联机帮助页:

   PTRACE_PEEKUSER
          Reads a word at offset addr in  the  child's  USER  area,  which
          holds the registers and other information about the process (see
          <sys/user.h>).  The word  is  returned  as  the  result  of  the
          ptrace()  call.   Typically  the  offset  must  be word-aligned,
          though this might vary by architecture.  See  NOTES.   (data  is
          ignored.)

在我的 64 位系统中,4 * ORIG_RAX 不是 8 字节对齐的。尝试使用 0 或 8 等值,它应该可以工作。

于 2011-09-14T15:34:35.347 回答
6

64 位 = 8 * ORIG_RAX

8 = 大小(长)

于 2013-05-11T14:00:13.870 回答