PTrace:linux/user.h:找不到文件或目录

14

我正在使用 Ubuntu 12.04 操作系统,在一台32位的Intel机器上安装了linux-headers-3.2.0-60。我尝试编译这个简单的程序来理解PTrace,但在编译过程中出现错误。

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <linux/user.h>   /* For constants
                                   ORIG_EAX etc */
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_EAX,
                          NULL);
        printf("The child made a "
               "system call %ld\n", orig_eax);
        ptrace(PTRACE_CONT, child, NULL, NULL);
    }
    return 0;
}

我遇到了以下错误:

make all 
Building file: ../src/Test.cpp
Invoking: Cross G++ Compiler
g++ -I/usr/local/include/boost -O0 -g3 -Wall -c -fmessage-length=0  -pthread -MMD -MP -MF"src/Test.d" -MT"src/Test.d" -o "src/Test.o" "../src/Test.cpp"
../src/Test.cpp:6:51: fatal error: linux/user.h: No such file or directory
compilation terminated.
make: *** [src/Test.o] Error 1

我检查了/usr/include/linux文件夹,但是没有名为user.h的文件。我尝试使用<sys/user.h>,但是它产生了另一个错误。

../src/Test.cpp:18:38: error: ‘ORIG_EAX’ was not declared in this scope

请帮忙。

2个回答

23

尝试包含sys/user.h和sys/reg.h ORIG_EAX在reg.h中定义


即使我已经包含了这两个,我仍然无法获得常量ORIG_EAX..但是我能在sys/reg.h中看到这一行# define ORIG_EAX 11。你能帮忙吗? - Harish Kayarohanam
9
我找到了问题。我的系统是64位的,所以必须使用ORIG_RAX。 - Harish Kayarohanam
我也遇到了64位问题,不得不将所有的EAX、EBX、ECX、EDX替换为RAX、RBX、RCX、RDX。 - Mohit

8

让我们深入了解一下/usr/include/sys/reg.h中的reg.h,以下是代码;对于64位,使用ORIG_RAX,否则使用ORIG_EAX。我使用的是64位工作站。

此外,对于64位,代码变化如下,因为它是8字节长的数组。

    orig_rax = ptrace(PTRACE_PEEKUSER,
            child, 8 * ORIG_RAX,
            NULL);

结果将会是 59,这是针对于 execve 系统调用的 (/usr/include/asm/unistd_64.h)

The child made a system call 59

/usr/include/sys/reg.h

#if __WORDSIZE == 64
/* Index into an array of 8 byte longs returned from ptrace for
    location of the users' stored general purpose registers.  */

# define R15    0
# define R14    1
# define R13    2
# define R12    3
# define RBP    4
# define RBX    5
# define R11    6
# define R10    7
# define R9 8
# define R8 9
# define RAX    10
# define RCX    11
# define RDX    12
# define RSI    13
# define RDI    14
# define ORIG_RAX 15
# define RIP    16
# define CS 17
# define EFLAGS 18
# define RSP    19
# define SS 20
# define FS_BASE 21
# define GS_BASE 22
# define DS 23
# define ES 24
# define FS 25
# define GS 26
#else

/* Index into an array of 4 byte integers returned from ptrace for
 * location of the users' stored general purpose registers. */

# define EBX 0
# define ECX 1
# define EDX 2
# define ESI 3
# define EDI 4
# define EBP 5
# define EAX 6
# define DS 7
# define ES 8
# define FS 9
# define GS 10
# define ORIG_EAX 11
# define EIP 12
# define CS  13
# define EFL 14
# define UESP 15
# define SS   16
#endif

发现了一个相关的网站http://theantway.com/2013/01/notes-for-playing-with-ptrace-on-64-bits-ubuntu-12-10/,其中包含更多信息。 - jarvis1729

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接