2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-01-11 22:36:09 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-04-22 18:44:45 +02:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Types.h>
|
2020-05-20 14:23:31 +02:00
|
|
|
#include <LibC/sys/arch/i386/regs.h>
|
2020-07-04 04:14:24 +04:30
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-12-24 23:08:58 +11:00
|
|
|
#include <LibCore/File.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-03-28 11:57:46 +03:00
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/wait.h>
|
2021-02-05 12:16:30 +01:00
|
|
|
#include <syscall.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <unistd.h>
|
2019-04-22 18:44:45 +02:00
|
|
|
|
2020-03-28 11:57:46 +03:00
|
|
|
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");
|
|
|
|
}
|
2019-05-02 15:51:39 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 18:44:45 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2021-05-04 18:44:04 +03:00
|
|
|
if (pledge("stdio wpath cpath proc exec ptrace sigaction", nullptr) < 0) {
|
2021-01-11 22:36:09 +01:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-04 04:14:24 +04:30
|
|
|
Vector<const char*> child_argv;
|
2020-04-11 16:38:59 +03:00
|
|
|
|
2020-12-24 23:08:58 +11:00
|
|
|
const char* output_filename = nullptr;
|
2021-05-11 10:56:28 +03:00
|
|
|
auto trace_file = Core::File::standard_error();
|
2020-12-24 23:08:58 +11:00
|
|
|
|
2020-07-04 04:14:24 +04:30
|
|
|
Core::ArgsParser parser;
|
2021-06-07 21:56:24 +02:00
|
|
|
parser.set_stop_on_first_non_option(true);
|
2020-12-05 16:22:58 +01:00
|
|
|
parser.set_general_help(
|
|
|
|
"Trace all syscalls and their result.");
|
2020-07-04 04:14:24 +04:30
|
|
|
parser.add_option(g_pid, "Trace the given PID", "pid", 'p', "pid");
|
2020-12-24 23:08:58 +11:00
|
|
|
parser.add_option(output_filename, "Filename to write output to", "output", 'o', "output");
|
2020-07-04 04:14:24 +04:30
|
|
|
parser.add_positional_argument(child_argv, "Arguments to exec", "argument", Core::ArgsParser::Required::No);
|
|
|
|
|
|
|
|
parser.parse(argc, argv);
|
|
|
|
|
2020-12-24 23:08:58 +11:00
|
|
|
if (output_filename != nullptr) {
|
2021-05-12 13:56:43 +04:30
|
|
|
auto open_result = Core::File::open(output_filename, Core::OpenMode::WriteOnly);
|
2020-12-24 23:08:58 +11:00
|
|
|
if (open_result.is_error()) {
|
|
|
|
outln(stderr, "Failed to open output file: {}", open_result.error());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
trace_file = open_result.value();
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:44:04 +03:00
|
|
|
if (pledge("stdio proc exec ptrace sigaction", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:05:27 -07:00
|
|
|
int status;
|
2020-07-04 04:14:24 +04:30
|
|
|
if (g_pid == -1) {
|
|
|
|
if (child_argv.is_empty()) {
|
2021-06-03 22:49:37 +02:00
|
|
|
warnln("strace: Expected either a pid or some arguments");
|
2020-07-04 04:14:24 +04:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
child_argv.append(nullptr);
|
2020-03-28 11:57:46 +03:00
|
|
|
int pid = fork();
|
2020-07-04 04:14:24 +04:30
|
|
|
if (pid < 0) {
|
|
|
|
perror("fork");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-02 15:51:39 +02:00
|
|
|
if (!pid) {
|
2020-03-28 11:57:46 +03:00
|
|
|
if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) {
|
|
|
|
perror("traceme");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-04 04:14:24 +04:30
|
|
|
int rc = execvp(child_argv.first(), const_cast<char**>(child_argv.data()));
|
2019-05-02 15:51:39 +02:00
|
|
|
if (rc < 0) {
|
|
|
|
perror("execvp");
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-05-02 15:51:39 +02:00
|
|
|
}
|
2020-07-04 04:14:24 +04:30
|
|
|
|
|
|
|
g_pid = pid;
|
2020-11-29 16:05:27 -07:00
|
|
|
if (waitpid(pid, &status, WSTOPPED | WEXITED) != pid || !WIFSTOPPED(status)) {
|
2020-03-28 11:57:46 +03:00
|
|
|
perror("waitpid");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-02 15:51:39 +02:00
|
|
|
}
|
2019-04-22 18:44:45 +02:00
|
|
|
|
2020-03-28 11:57:46 +03:00
|
|
|
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");
|
2019-04-22 18:44:45 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
|
2020-03-28 11:57:46 +03:00
|
|
|
perror("waitpid");
|
|
|
|
return 1;
|
2019-05-02 15:51:39 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 18:44:45 +02:00
|
|
|
for (;;) {
|
2020-03-28 11:57:46 +03:00
|
|
|
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
|
|
|
|
perror("syscall");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
|
2020-03-28 11:57:46 +03:00
|
|
|
perror("wait_pid");
|
2019-04-22 18:44:45 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-28 11:57:46 +03:00
|
|
|
PtraceRegisters regs = {};
|
|
|
|
if (ptrace(PT_GETREGS, g_pid, ®s, 0) == -1) {
|
|
|
|
perror("getregs");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
u32 syscall_index = regs.eax;
|
|
|
|
u32 arg1 = regs.edx;
|
|
|
|
u32 arg2 = regs.ecx;
|
|
|
|
u32 arg3 = regs.ebx;
|
|
|
|
|
|
|
|
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
|
|
|
|
perror("syscall");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
|
2020-03-28 11:57:46 +03:00
|
|
|
perror("wait_pid");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptrace(PT_GETREGS, g_pid, ®s, 0) == -1) {
|
2020-11-29 16:05:27 -07:00
|
|
|
perror("getregs");
|
|
|
|
return 1;
|
2020-03-28 11:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 res = regs.eax;
|
|
|
|
|
2021-05-07 22:06:58 +02:00
|
|
|
auto string = String::formatted("{}({:#08x}, {:#08x}, {:#08x})\t={}\n",
|
|
|
|
Syscall::to_string((Syscall::Function)syscall_index),
|
2020-03-28 11:57:46 +03:00
|
|
|
arg1,
|
|
|
|
arg2,
|
|
|
|
arg3,
|
|
|
|
res);
|
2021-05-07 22:06:58 +02:00
|
|
|
|
|
|
|
if (!trace_file->write(string)) {
|
|
|
|
warnln("write: {}", trace_file->error_string());
|
|
|
|
return 1;
|
|
|
|
}
|
2019-04-22 18:44:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|