【发布时间】:2021-04-06 17:00:33
【问题描述】:
我目前正在重新编码 Strace 命令。
我了解此命令的目标,并且可以从可执行文件中捕获一些系统调用。
我的问题是:为什么我没有捕捉到“写”系统调用?
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <wait.h>
int main(int argc, char* argv[]) {
int status;
pid_t pid;
struct user_regs_struct regs;
int counter = 0;
int in_call =0;
switch(pid = fork()) {
case -1:
perror("fork");
exit(1);
case 0:
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execvp(argv[1], argv + 1);
break;
default:
wait(&status);
while (status == 1407) {
ptrace(PTRACE_GETREGS, pid, NULL, ®s);
if(!in_call) {
printf("SystemCall %lld called with %lld, %lld, %lld\n",regs.orig_rax,
regs.rbx, regs.rcx, regs.rdx);
in_call=1;
counter ++;
}
else
in_call = 0;
ptrace(PTRACE_SYSEMU, pid, NULL, NULL);
wait(&status);
}
}
printf("Total Number of System Calls = %d\n", counter);
return 0;
}
这是使用我的程序的输出:
./strace ./my_program
SystemCall 59 called with 0, 0, 0
SystemCall 60 called with 0, 4198437, 5
Total Number of System Calls = 2
59 代表 execve 系统调用。 60 代表退出系统调用。 这是使用 real strace 的输出:
strace ./my_program
execve("./my_program", ["./bin_asm_write"], 0x7ffd2929ae70 /* 67 vars */) = 0
write(1, "Toto\n", 5Toto
) = 5
exit(0) = ?
+++ exited with 0 +++
如您所见,我的程序没有捕获 write syscall。
我不明白为什么,你知道吗?
感谢您的回答。
【问题讨论】:
-
您的 while 循环设置为切换
in_call(在 0 和 1 之间来回翻转),因此您只打印每隔一个系统调用。所以你正在捕捉 write 系统调用,但由于它是一个奇怪的调用,它不会被打印出来。
标签: c system-calls strace