我正在关注教程 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

代码有什么问题?

最佳答案

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



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

关于c - 如何在 x86-64 上玩 ptrace?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7418315/

10-13 07:15