我在intel 32位机器上使用Ubuntu 12.04和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
请帮忙。
答案 0 :(得分:16)
尝试包含sys / user.h和sys / reg.h ORIG_EAX在reg.h中定义
答案 1 :(得分:4)
那么让我们再深一点,看一下/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