Linux/Windows 中的调试器 运行 如何读取 ARM32 和 Aarch64 上的 PC 寄存器?

How would a debugger running in Linux/Windows read the PC register on ARM32 & Aarch64?

Linux/Windows 中的调试器 运行 如何读取 ARM32 和 Aarch64 上的 PC 寄存器? 如何访问指令寄存器值?

如何在Linux上使用ptrace获取子进程的PC寄存器? Windows?

如何使用 GetThreadContext 从子进程的上下文结构中检索 PC 寄存器

谢谢。

ptrace(2) 有一个 PTRACE_GETREGS 选项,可以将被跟踪对象的所有通用寄存器读取到 <sys/user.h> 中定义的 struct user_regs_struct 中。对于 AArch64,此结构有一个大小为 31 的数组用于寄存器 x0 到 x30,以及用于 sp、pc 和 pstate 的单独字段。所以你可以这样做(未经测试):

#include <sys/ptrace.h>
#include <sys/user.h>

struct user_regs_struct regs;
if (ptrace(PTRACE_GETREGS, pid, NULL, &regs) < 0)
    die();
printf("pc is %#llx\n", regs.pc);

对于 ARM32,它看起来像结构被称为 struct user_regs,它只是一个大小为 18 的数组。我猜它是 r0 到 r15(其中 r15 是 pc),然后可能 pstate 还有别的吗?您必须检查内核或 GDB 源以确认。所以你可以可能做(非常未经测试):

#include <sys/ptrace.h>
#include <sys/user.h>

struct user_regs regs;
if (ptrace(PTRACE_GETREGS, pid, NULL, &regs) < 0)
    die();
printf("pc is %#lx\n", regs.uregs[15]);