strace: Port to LibMain :^)

This commit is contained in:
Andreas Kling 2021-12-25 11:07:48 +01:00
parent 2bd1a62ce1
commit e923cf6624
Notes: sideshowbarker 2024-07-17 22:11:42 +09:00
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(sql LibLine LibSQL LibIPC)
target_link_libraries(stat LibMain)
target_link_libraries(strace LibMain)
target_link_libraries(su LibCrypt LibMain)
target_link_libraries(tar LibArchive LibCompress)
target_link_libraries(telws LibProtocol LibLine)

View File

@ -13,6 +13,7 @@
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <errno.h>
#include <fcntl.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) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio wpath cpath proc exec ptrace sigaction"));
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_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) {
if (option != nullptr) {
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(include_syscalls_option, include_syscalls);
if (pledge("stdio proc exec ptrace sigaction", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio proc exec ptrace sigaction"));
int status;
if (g_pid == -1) {
if (child_argv.is_empty()) {
warnln("strace: Expected either a pid or some arguments");
return 1;
}
if (child_argv.is_empty())
return Error::from_string_literal("Expected either a pid or some arguments"sv);
child_argv.append(nullptr);
int pid = fork();
if (pid < 0) {
perror("fork");
return 1;
}
auto pid = TRY(Core::System::fork());
if (!pid) {
if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) {
perror("traceme");
return 1;
}
TRY(Core::System::ptrace(PT_TRACE_ME, 0, 0, 0));
int rc = execvp(child_argv.first(), const_cast<char**>(child_argv.data()));
if (rc < 0) {
perror("execvp");
@ -880,34 +862,25 @@ int main(int argc, char** argv)
}
}
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
struct sigaction sa = {};
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) {
perror("attach");
return 1;
}
TRY(Core::System::ptrace(PT_ATTACH, g_pid, 0, 0));
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
perror("waitpid");
return 1;
}
for (;;) {
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
perror("syscall");
return 1;
}
TRY(Core::System::ptrace(PT_SYSCALL, g_pid, 0, 0));
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
perror("wait_pid");
return 1;
}
PtraceRegisters regs = {};
if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) {
perror("getregs");
return 1;
}
TRY(Core::System::ptrace(PT_GETREGS, g_pid, &regs, 0));
#if ARCH(I386)
syscall_arg_t syscall_index = regs.eax;
syscall_arg_t arg1 = regs.edx;
@ -920,19 +893,13 @@ int main(int argc, char** argv)
syscall_arg_t arg3 = regs.rbx;
#endif
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
perror("syscall");
return 1;
}
TRY(Core::System::ptrace(PT_SYSCALL, g_pid, 0, 0));
if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
perror("wait_pid");
return 1;
}
if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) {
perror("getregs");
return 1;
}
TRY(Core::System::ptrace(PT_GETREGS, g_pid, &regs, 0));
#if ARCH(I386)
u32 res = regs.eax;