1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:27:35 +00:00

strace: Change implementation to use ptrace()

This commit is contained in:
Itamar 2020-03-28 11:57:46 +03:00 committed by Andreas Kling
parent 6b74d38aab
commit e5ebdb9bca
6 changed files with 177 additions and 38 deletions

View file

@ -25,18 +25,34 @@
*/
#include <AK/Assertions.h>
#include <AK/LogStream.h>
#include <AK/Types.h>
#include <Kernel/Syscall.h>
#include <LibC/sys/arch/i386/regs.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>
static int usage()
{
printf("usage: strace [-p PID] [command...]\n");
return 0;
printf("usage: strace [-p pid] [command...]\n");
return 1;
}
static int g_pid = -1;
static void handle_sigint(int)
{
if (g_pid == -1)
return;
if (ptrace(PT_DETACH, g_pid, 0, 0) == -1) {
perror("detach");
}
}
int main(int argc, char** argv)
@ -44,18 +60,17 @@ int main(int argc, char** argv)
if (argc == 1)
return usage();
pid_t pid = -1;
bool pid_is_child = false;
if (!strcmp(argv[1], "-p")) {
if (argc != 3)
return usage();
pid = atoi(argv[2]);
g_pid = atoi(argv[2]);
} else {
pid_is_child = true;
pid = fork();
int pid = fork();
if (!pid) {
kill(getpid(), SIGSTOP);
if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) {
perror("traceme");
return 1;
}
int rc = execvp(argv[1], &argv[1]);
if (rc < 0) {
perror("execvp");
@ -63,40 +78,84 @@ int main(int argc, char** argv)
}
ASSERT_NOT_REACHED();
}
if (waitpid(pid, nullptr, WSTOPPED) != pid) {
perror("waitpid");
return 1;
}
g_pid = pid;
}
int fd = systrace(pid);
if (fd < 0) {
perror("systrace");
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handle_sigint;
sigaction(SIGINT, &sa, nullptr);
if (ptrace(PT_ATTACH, g_pid, 0, 0) == -1) {
perror("attach");
return 1;
}
if (pid_is_child) {
int rc = kill(pid, SIGCONT);
if (rc < 0) {
perror("kill(pid, SIGCONT)");
return 1;
}
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
perror("waitpid");
return 1;
}
for (;;) {
u32 call[5];
int nread = read(fd, &call, sizeof(call));
if (nread == 0)
break;
if (nread < 0) {
perror("read");
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
if (errno == ESRCH)
return 0;
perror("syscall");
return 1;
}
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
perror("wait_pid");
return 1;
}
ASSERT(nread == sizeof(call));
fprintf(stderr, "%s(%#x, %#x, %#x) = %#x\n", Syscall::to_string((Syscall::Function)call[0]), call[1], call[2], call[3], call[4]);
}
int rc = close(fd);
if (rc < 0) {
perror("close");
return 1;
PtraceRegisters regs = {};
if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) {
perror("getregs");
return 1;
}
u32 syscall_index = regs.eax;
u32 arg1 = regs.edx;
u32 arg2 = regs.ecx;
u32 arg3 = regs.ebx;
// skip syscall exit
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
if (errno == ESRCH)
return 0;
perror("syscall");
return 1;
}
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
perror("wait_pid");
return 1;
}
if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) {
if (errno == ESRCH && syscall_index == SC_exit) {
regs.eax = 0;
} else {
perror("getregs");
return 1;
}
}
u32 res = regs.eax;
fprintf(stderr, "%s(0x%x, 0x%x, 0x%x)\t=%d\n",
Syscall::to_string(
(Syscall::Function)syscall_index),
arg1,
arg2,
arg3,
res);
}
return 0;
return 0;
}