好的,这是一个简单的问题。当我使用 ptrace 系统调用时,android 支持 PTRACE_SINGLESTEP 吗?当我想ptrace一个android apk程序时,我发现我无法处理SINGLESTEP跟踪。但是当我使用PTRACE_SYSCALL时情况发生了变化,它可以完美工作。是android取消了这个功能还是arm缺乏硬件支持?任何帮助将不胜感激!谢谢。
这是我的核心程序:
int main(int argc, char *argv[])
{
if(argc != 2) {
__android_log_print(ANDROID_LOG_DEBUG,TAG,"please input the pid!");
return -1;
}
if(0 != ptrace(PTRACE_ATTACH, target_pid, NULL, NULL))
{
__android_log_print(ANDROID_LOG_DEBUG,TAG,"ptrace attach error");
return -1;
}
__android_log_print(ANDROID_LOG_DEBUG,TAG,"start monitor process :%d",target_pid);
while(1)
{
wait(&status);
if(WIFEXITED(status))
{
break;
}
if (ptrace(PTRACE_SINGLESTEP, target_pid, 0, 0) != 0)
__android_log_print(ANDROID_LOG_DEBUG,TAG,"PTRACE_SINGLESTEP attach error");
}
ptrace(PTRACE_DETACH, target_pid, NULL, NULL);
__android_log_print(ANDROID_LOG_DEBUG,TAG,"monitor finished");
return 0;
}
我在 shell 上运行这个程序。而且我可以获得root权限。 如果我将请求更改为 PTRACE_SYSCALL 程序将正常运行。 但如果请求是PTRACE_SINGLESTEP,程序就会出错!
最佳答案
自 2011 年以来,PTRACE_SINGLESTEP 已由 this commit 在 ARM Linux 上删除。 .
硬件不支持单步执行;以前的内核支持涉及解码指令以找出下一个(分支)并暂时用调试中断软件断点替换它。
引用有关同一提交的邮件列表消息,描述旧情况:http://lists.infradead.org/pipermail/linux-arm-kernel/2011-February/041324.html
PTRACE_SINGLESTEP is a ptrace request designed to offer single-stepping support to userspace when the underlying architecture has hardware support for this operation.
On ARM, we set
arch_has_single_step()
to 1 and attempt to emulate hardware single-stepping by disassembling the current instruction to determine the next pc and placing a software breakpoint on that location.Unfortunately this has the following problems:
- Only a subset of ARMv7 instructions are supported
- Thumb-2 is unsupported
- The code is not SMP safe
We could try to fix this code, but it turns out that because of the above issues it is rarely used in practice. GDB, for example, uses PTRACE_POKETEXT and PTRACE_PEEKTEXT to manage breakpoints itself and does not require any kernel assistance.
This patch removes the single-step emulation code from ptrace meaning that the PTRACE_SINGLESTEP request will return -EIO on ARM. Portable code must check the return value from a ptrace call and handle the failure gracefully.
Signed-off-by: Will Deacon <will.deacon at arm.com>
---The comments I received about v1 suggest that:
- If emulation is required, it is plausible to do it from userspace
- ltrace uses the SINGLESTEP call (conditionally at compile-time since other architectures, such as mips, do not support this request) but does not check the return value from ptrace. This is a bug in ltrace.
- strace does not use SINGLESTEP
关于android - android 支持 PTRACE_SINGLESTEP 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23058003/