【问题标题】:How to play with ptrace on x86-64?如何在 x86-64 上使用 ptrace?
【发布时间】:2011-11-17 02:43:34
【问题描述】:

我正在关注here 的教程,并对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

代码有什么问题?

【问题讨论】:

    标签: c linux x86-64 ptrace


    【解决方案1】:

    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 等值,它应该可以工作。

    【讨论】:

      【解决方案2】:

      64 位 = 8 * ORIG_RAX

      8 = sizeof(long)

      【讨论】:

        猜你喜欢
        • 2016-10-11
        • 2019-04-26
        • 2020-12-08
        • 2012-09-13
        • 2022-01-25
        • 2016-09-23
        • 2012-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多