c - ptrace(PTRACE_PEEKUSER) 和 ptrace(PTRACE_PEEKDATA) 之间的区别?

标签 c system-calls strace ptrace

ptrace 上发了很多问题之后(最近的 5 个问题是我的 :( )我终于在替换时得到了想要的输出

reg_val[1] = ptrace(PTRACE_PEEKDATA, child, 4 * EBX, NULL);

reg_val[1] = ptrace(PTRACE_PEEKUSER, child, 4 * EBX, NULL);

man page中提到的区别是这样的

  • PTRACE_PEEKTEXT 在 child 内存中的位置 addr 读取一个单词
  • PTRACE_PEEKUSER 读取 child USER 区偏移地址处的一个词

我无法仅从手册页中理解这种差异。任何人都可以在这方面对我进行更多教育吗??

最佳答案

PTRACE_PEEKDATA 用于读取子进程的数据/代码部分(一般过程——所谓的 tracee)。如您所知,调试器经常使用 ptrace。他们可以使用此调用来检查变量的值。例如,在 GDB/DBX 中,如果你说

print count

调试器将使用 PTRACE_PEEKDATA 在内部调用 ptrace 并找到它的值。

PTRACE_PEEKUSER 用于读取 child 的 USER 区域的内容,该区域包含寄存器内容和其他信息。 sys/user.h列出其他信息是什么。

例如USER区包含,

struct user_regs_struct
{
  long int ebx;
  long int ecx;
  long int edx;
  long int esi;
  long int edi;
  long int ebp;
  long int eax;
  long int xds;
  long int xes;
  long int xfs;
  long int xgs;
  long int orig_eax;
  long int eip;
  long int xcs;
  long int eflags;
  long int esp;
  long int xss;
};

简而言之:

  • PTRACE_PEEKDATA 用于程序数据(例如变量)和代码;
  • PTRACE_PEEKUSER 用于寄存器值和其他调试信息;

请注意 PTRACE_PEEKDATAPTRACE_PEEKTEXT 之间的等效性。来自 man ptrace :

Linux does not have separate text and data address spaces, so these two requests are currently equivalent.

关于c - ptrace(PTRACE_PEEKUSER) 和 ptrace(PTRACE_PEEKDATA) 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803908/

相关文章:

c - 从另一个应用程序访问(读/写)虚拟内存进程

logging - 使用 logger.exe 嗅探系统调用

c - 调试seg错误(无法修改源代码或makefile)

bash - 找出 bash/dash/sh 在启动过程中读取的所有文件

c - ‘tzp’ 的存储大小未知

c - strcat() 是否像 strcat_s() 一样返回错误?

CLion 出现 CMAKE 错误

c - system() 调用行为

r - 如何正确地从 R 内部转义系统调用

计算程序的系统调用并使用 strace 检查结果的有效性