在SIGTRAP上是否有任何与arch无关的获取指令指针的方法?

时间:2017-08-06 13:30:28

标签: c linux signals posix ptrace

我正在寻找一种与arch {无关的方法来获取生成SIGTRAP的最后一条尝试指令(或一条过去的指令)的指令指针(AKA程序计数器),从{{1跟踪器。

依赖于拱形的方式是使用ptrace并选择例如i386上为PTRACE_GETREGS,x86_64为EIP,ARM上为RIP等。

我尝试使用PC以及来自siginfo.si_addr的{​​{1}} - 返回的结构,但这些值看起来完全错误(4个十六进制数字而不是8个,甚至不是类似于真实的地址),所以它们似乎不是我需要的。

在Linux中,我还尝试使用siginfo.si_ptr的第30个字段,内核使用PTRACE_GETSIGINFO填充/proc/<pid>/task/<tid>/stat(尽管名称以x86为中心,似乎为许多其他架构定义)。但由于某些原因,在我的ARMv6 Linux 4.9.28+(Raspbian 8)上,我得到了程序计数器和堆栈指针的零。

那么,是否有任何与arch无关的方法来确定POSIX定义的当前/下一个地址,或至少在Linux中可用?

1 个答案:

答案 0 :(得分:2)

您可以使用/proc/[pid]/syscall

mark@ubuntu:~$ gdb python
...
...
... 
Program received signal SIGINT, Interrupt.
0x00007ffff78ed573 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:84
84  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) 

mark@ubuntu:~/$ ps aux | grep python
mark      77858  0.2  0.7  90216 37780 pts/2    S+   15:13   0:00 gdb python
mark      77860  0.0  0.1  38416  6424 pts/2    t    15:13   0:00 /usr/bin/python

(请参阅跟踪77860 - t

mark@ubuntu:~/$ sudo cat /proc/77860/syscall
23 0x1 0x7fffffffd980 0x0 0x0 0x0 0x7ffff7fdb700 0x7fffffffd958 0x7ffff78ed573

0x7fffffffd958sp0x7ffff78ed573是程序计数器。

我找不到任何有助于此的ptrace电话。

http://man7.org/linux/man-pages/man5/proc.5.html

   /proc/[pid]/syscall (since Linux 2.6.27)
          This file exposes the system call number and argument regis‐
          ters for the system call currently being executed by the
          process, followed by the values of the stack pointer and pro‐
          gram counter registers.  The values of all six argument regis‐
          ters are exposed, although most system calls use fewer regis‐
          ters.

          If the process is blocked, but not in a system call, then the
          file displays -1 in place of the system call number, followed
          by just the values of the stack pointer and program counter.
          If process is not blocked, then the file contains just the
          string "running".