【发布时间】:2012-07-01 09:31:49
【问题描述】:
我正在使用 ptrace(PTRACE_ATTACH...) 附加到一个处于系统调用中的进程(如 nanosleep())。我可以使用 PTRACE_GETREGS 来获取寄存器内容,并且 eip 位于预期位置(在 __kernel_vsyscall 中)。但是,eax 和 orig_eax 寄存器有意想不到的内容:eax 通常包含 -516,而 orig_eax 通常为 0。
这是我使用的测试程序(取自http://www.linuxjournal.com/article/6210,稍作修改):
#include <stdlib.h>
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
int main(int argc, char *argv[])
{
pid_t traced_process;
struct user_regs_struct regs;
long ins;
if(argc != 2) {
printf("Usage: %s <pid to be traced>\n",
argv[0]);
exit(1);
}
traced_process = atoi(argv[1]);
ptrace(PTRACE_ATTACH, traced_process,
NULL, NULL);
wait(NULL);
ptrace(PTRACE_GETREGS, traced_process,
NULL, ®s);
printf("eax: %lx (%d); orig_eax: %lx\n",
regs.eax, (int)regs.eax, regs.orig_eax);
ins = ptrace(PTRACE_PEEKTEXT, traced_process,
regs.eip, NULL);
printf("EIP: %lx Instruction executed: %lx\n",
regs.eip, ins);
ptrace(PTRACE_DETACH, traced_process,
NULL, NULL);
return 0;
}
附加到在另一个终端中运行的“sleep 10000”命令时的输出:
eax: fffffdfc (-516); orig_eax: 0
EIP: b7711424 Instruction executed: c3595a5d
eax 中的值是什么意思?为什么 orig_eax 不包含原始系统调用号(如 162)?在这种情况下,我如何实际获取系统调用号?
另外,为什么 gdb 正确显示“print $orig_eax”的“162”?
顺便说一句。这是在 Ubuntu 12.04 上,内核为 3.2.0:
- uname -a: "Linux edgebox 3.2.0-24-generic-pae #37-Ubuntu SMP Wed Apr 25 10:47:59 UTC 2012 i686 athlon i386 GNU/Linux"
- /proc/cpuinfo:“AMD Athlon(tm) II Neo K345 双核处理器”
- file
which sleep: "/bin/sleep: ELF 32 位 LSB 可执行文件,Intel 80386,版本 1 (SYSV),动态链接(使用共享库),适用于 GNU/Linux 2.6.24,BuildID[sha1] =0x0965431bde4d183eaa2fa3e3989098ce46b92129,剥离”。
所以它是一个 32 位 PAE 内核和 32 位 Ubuntu 安装在 64 位 CPU 上。
【问题讨论】:
-
可能与stackoverflow.com/questions/5503425/… 存在相同的问题,但这更好,因为它有代码。文章的第一部分说你应该改用
PTRACE_PEEKUSER+ORIG_EAX。另见:github.com/nelhage/ministrace/blob/for-blog/ministrace.c -
如何从 RIP 中找出内核所在的函数?
标签: linux debugging system-calls ptrace