1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:37:36 +00:00

strace: Port to LibMain :^)

This commit is contained in:
Andreas Kling 2021-12-25 11:07:48 +01:00
parent 2bd1a62ce1
commit e923cf6624
2 changed files with 22 additions and 54 deletions

View file

@ -132,6 +132,7 @@ target_link_libraries(run-tests LibRegex)
target_link_libraries(shot LibGUI) target_link_libraries(shot LibGUI)
target_link_libraries(sql LibLine LibSQL LibIPC) target_link_libraries(sql LibLine LibSQL LibIPC)
target_link_libraries(stat LibMain) target_link_libraries(stat LibMain)
target_link_libraries(strace LibMain)
target_link_libraries(su LibCrypt LibMain) target_link_libraries(su LibCrypt LibMain)
target_link_libraries(tar LibArchive LibCompress) target_link_libraries(tar LibArchive LibCompress)
target_link_libraries(telws LibProtocol LibLine) target_link_libraries(telws LibProtocol LibLine)

View file

@ -13,6 +13,7 @@
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <LibMain/Main.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -796,12 +797,9 @@ static void format_syscall(FormattedSyscallBuilder& builder, Syscall::Function s
} }
} }
int main(int argc, char** argv) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
if (pledge("stdio wpath cpath proc exec ptrace sigaction", nullptr) < 0) { TRY(Core::System::pledge("stdio wpath cpath proc exec ptrace sigaction"));
perror("pledge");
return 1;
}
Vector<const char*> child_argv; Vector<const char*> child_argv;
@ -822,16 +820,11 @@ int main(int argc, char** argv)
parser.add_option(include_syscalls_option, "Comma-delimited syscalls to include", "include", 'i', "include"); parser.add_option(include_syscalls_option, "Comma-delimited syscalls to include", "include", 'i', "include");
parser.add_positional_argument(child_argv, "Arguments to exec", "argument", Core::ArgsParser::Required::No); parser.add_positional_argument(child_argv, "Arguments to exec", "argument", Core::ArgsParser::Required::No);
parser.parse(argc, argv); parser.parse(arguments);
if (output_filename != nullptr)
trace_file = TRY(Core::File::open(output_filename, Core::OpenMode::WriteOnly));
if (output_filename != nullptr) {
auto open_result = Core::File::open(output_filename, Core::OpenMode::WriteOnly);
if (open_result.is_error()) {
outln(stderr, "Failed to open output file: {}", open_result.error());
return 1;
}
trace_file = open_result.value();
}
auto parse_syscalls = [](const char* option, auto& hash_table) { auto parse_syscalls = [](const char* option, auto& hash_table) {
if (option != nullptr) { if (option != nullptr) {
for (auto syscall : StringView(option).split_view(',')) for (auto syscall : StringView(option).split_view(','))
@ -841,30 +834,19 @@ int main(int argc, char** argv)
parse_syscalls(exclude_syscalls_option, exclude_syscalls); parse_syscalls(exclude_syscalls_option, exclude_syscalls);
parse_syscalls(include_syscalls_option, include_syscalls); parse_syscalls(include_syscalls_option, include_syscalls);
if (pledge("stdio proc exec ptrace sigaction", nullptr) < 0) { TRY(Core::System::pledge("stdio proc exec ptrace sigaction"));
perror("pledge");
return 1;
}
int status; int status;
if (g_pid == -1) { if (g_pid == -1) {
if (child_argv.is_empty()) { if (child_argv.is_empty())
warnln("strace: Expected either a pid or some arguments"); return Error::from_string_literal("Expected either a pid or some arguments"sv);
return 1;
}
child_argv.append(nullptr); child_argv.append(nullptr);
int pid = fork(); auto pid = TRY(Core::System::fork());
if (pid < 0) {
perror("fork");
return 1;
}
if (!pid) { if (!pid) {
if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) { TRY(Core::System::ptrace(PT_TRACE_ME, 0, 0, 0));
perror("traceme");
return 1;
}
int rc = execvp(child_argv.first(), const_cast<char**>(child_argv.data())); int rc = execvp(child_argv.first(), const_cast<char**>(child_argv.data()));
if (rc < 0) { if (rc < 0) {
perror("execvp"); perror("execvp");
@ -880,34 +862,25 @@ int main(int argc, char** argv)
} }
} }
struct sigaction sa; struct sigaction sa = {};
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handle_sigint; sa.sa_handler = handle_sigint;
sigaction(SIGINT, &sa, nullptr); TRY(Core::System::sigaction(SIGINT, &sa, nullptr));
if (ptrace(PT_ATTACH, g_pid, 0, 0) == -1) { TRY(Core::System::ptrace(PT_ATTACH, g_pid, 0, 0));
perror("attach");
return 1;
}
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) { if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
perror("waitpid"); perror("waitpid");
return 1; return 1;
} }
for (;;) { for (;;) {
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) { TRY(Core::System::ptrace(PT_SYSCALL, g_pid, 0, 0));
perror("syscall");
return 1;
}
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) { if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
perror("wait_pid"); perror("wait_pid");
return 1; return 1;
} }
PtraceRegisters regs = {}; PtraceRegisters regs = {};
if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) { TRY(Core::System::ptrace(PT_GETREGS, g_pid, &regs, 0));
perror("getregs");
return 1;
}
#if ARCH(I386) #if ARCH(I386)
syscall_arg_t syscall_index = regs.eax; syscall_arg_t syscall_index = regs.eax;
syscall_arg_t arg1 = regs.edx; syscall_arg_t arg1 = regs.edx;
@ -920,19 +893,13 @@ int main(int argc, char** argv)
syscall_arg_t arg3 = regs.rbx; syscall_arg_t arg3 = regs.rbx;
#endif #endif
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) { TRY(Core::System::ptrace(PT_SYSCALL, g_pid, 0, 0));
perror("syscall");
return 1;
}
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) { if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
perror("wait_pid"); perror("wait_pid");
return 1; return 1;
} }
if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) { TRY(Core::System::ptrace(PT_GETREGS, g_pid, &regs, 0));
perror("getregs");
return 1;
}
#if ARCH(I386) #if ARCH(I386)
u32 res = regs.eax; u32 res = regs.eax;