我目前正在重新编码 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,NULL,NULL);
execvp(argv[1],argv + 1);
break;
default:
wait(&status);
while (status == 1407) {
ptrace(PTRACE_GETREGS,pid,®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,NULL);
wait(&status);
}
}
printf("Total Number of System Calls = %d\n",counter);
return 0;
}
这是使用我的程序的输出:
./strace ./my_program
SystemCall 59 called with 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 +++
如您所见,我的程序没有捕获写入系统调用。
我不明白为什么,你有什么想法吗?
感谢您的回答。