- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 ptrace 来跟踪子进程。当子进程正常退出时,它工作得很好。但是,如果它异常退出,尽管使用了宏 WIFSIGNALED(&status),程序仍会进入死循环。这是示例子进程:
try.c
int main()
{
int a=5/0;
}
这是跟踪程序
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/syscall.h> /* For SYS_write etc */
#include <sys/reg.h>
#include <signal.h>
int main()
{
pid_t child;
long orig_eax, eax;
int status,insyscall = 0;
child = fork();
if(child == 0)
{
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl("./try", "try", NULL);
}
else
{
siginfo_t sig;
memset(&sig,0,sizeof(siginfo_t));
while(1)
{
wait(&status);
if(WIFSIGNALED(status))
{
printf("Exiting due to signal\n");
exit(0);
}
if(WIFEXITED(status))
break;
orig_eax = ptrace(PTRACE_PEEKUSER,child, 4 * ORIG_EAX, NULL);
printf("system call number=%ld\n",orig_eax);
if(insyscall == 0)
{
/* Syscall entry */
insyscall = 1;
printf("In sys call\n");
}
else
{
/* Syscall exit */
eax = ptrace(PTRACE_PEEKUSER,child, 4 * EAX, NULL);
printf("System call returned with %ld\n", eax);
insyscall = 0;
}
ptrace(PTRACE_SYSCALL,child, NULL, NULL);
}
}
return 0;
}
为什么没有检测到信号,否则在不使用 ptrace 时可以正常工作?
最佳答案
当你 ptrace 一个进程时,wait 会返回任何状态变化。其中之一是进程即将接收信号时。您的等待将在信号传递给 child 之前返回。你需要使用 PTRACE_CONT 来允许将信号传递给 child ,如果这是你想要发生的。
为什么会这样?请记住,ptrace 的主要目的是用于实现调试器。如果您没有机会拦截诸如 SIGSEGV
之类的信号,调试器将无法停止并让您在进程被拆除之前检查段错误。
关于c - 为什么 WIFSIGNALED(status) 在使用 ptrace 跟踪进程时无法检测到信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462230/
有人添加到 Wikipedia "ptrace" article声称在 Linux 上,一个 ptraced 进程本身不能 ptrace 另一个进程。我正在尝试确定是否(如果是,为什么)是这种情况。下
我的 Linux 守护程序有问题。它以 root 权限开始,进行一些配置,然后通过切换到某个用户和组来永久删除权限并继续工作。切换到非特权用户是这样完成的: void switch_to_user_g
在 ptrace 上发了很多问题之后(最近的 5 个问题是我的 :( )我终于在替换时得到了想要的输出 reg_val[1] = ptrace(PTRACE_PEEKDATA, child, 4 *
来自手册页: PTRACE_GET_SYSCALL_INFO (since Linux 5.3) Retrieve information about the system call that cau
我的Linux守护程序有问题。它从root特权开始,进行一些配置,然后通过切换到某些用户和组永久放弃特权并继续工作。切换到非特权用户的操作如下: void switch_to_user_group(s
目前,对于一个项目,我需要使用 ptrace() 编写某种调试器。最后,它应该显示程序中进入/退出的每个函数/系统调用以进行跟踪。 现在,我被困住了。我制作了一个小程序,它应该尝试跟踪给定的程序,并根
考虑这个无限循环的简单程序: int main(void) { for(;;); } 使用 ptrace 向其中注入(inject)系统调用非常简单,如下所示: #include #
我有以下代码。它只是在进入无限循环之前调用 ptrace(PTRACE_TRACEME)。 我有两个问题: 执行二进制文件后,即使我是 root,我也无法附加 gdb。 使用 ptrace(PTRAC
我正在使用 Go 的系统调用包 Ptrace 接口(interface)来跟踪进程。问题是,如果被跟踪者长时间运行,跟踪似乎会挂起。我尝试用 C 实现复制这个问题,但一切似乎都运行良好。 这是重现该问
我只想跟踪 C 程序的一部分以进行系统调用。我使用 ptrace() 和 PTRACE_TRACEME 选项来开始跟踪。如何在几行代码后阻止此进程被跟踪。我正在尝试使用 PTRACE_DETACH 但
ptrace 可以在进入/退出系统调用时获取寄存器和内存数据。但是,如果 linux 系统调用处理程序更改了一些内存,包括堆栈中的某个位置,我如何才能知道哪个内存已被更改。 最佳答案 你不能;但例如
我想在 Linux 上跟踪 PIE,例如在给定的指令地址中断。从反汇编中,我得到了指令的相对地址 - 如何找出可执行文件的加载位置,以便获得绝对地址? GDB 能够跟踪 PIE - 它是如何处理的?
我想使用 ptrace() 调用从用户定义的函数中捕获信息。 但是函数地址不稳定(因为ASLR)。 如何以编程方式获取另一个程序的函数信息,例如gdb? #include #include #in
我在研究项目上遇到了问题。我正在尝试的是使用 ptrace 来观察目标进程的执行。在 ptrace 的帮助下,我将 mprotect 系统调用注入(inject)目标代码段(类似于断点)并将堆栈保护设
我正在尝试使用 ptrace 实现数据流异常检测。经过一些研究,我实现了一个类似于 strace 的程序。现在,我对此感到困惑,这就是 wiki 告诉我的关于系统调用的内容: "System call
我以 ./main & 运行我的进程 它给了我一个看起来像这样的地址:[1] 4257 然后在一个新的终端上我这样做:./tracer 4257 这行代码返回-1 ptrace(PTRACE_ATTA
我正在编写一个程序来监视系统调用(除其他外)。但是我在让 ptrace 识别我传递给它的进程 ID 时遇到了一些麻烦。执行程序后,我收到此错误消息: :No such process 但是,我已经在调
当我附加到另一个进程时,我无法跟踪 fork/exec 事件,从 waitpid 返回的 status 始终为零(在右移 16 次之后)。 我已经成功附加到 bash shell,但是无论我运行什么命
我想按以下方式(伪代码)使用 ptrace: child : foo(); now that foo is done parent should use ptrace to chang
我正在尝试使用 ptrace 在运行时更改另一个程序变量,在我的例子中是一个虚拟 shell。 int main(void) { char buf[MAXLINE]; fgets
我是一名优秀的程序员,十分优秀!