summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-12-25 11:07:48 +0100
committerAndreas Kling <kling@serenityos.org>2021-12-25 11:12:03 +0100
commite923cf6624b4195479e7881d57d933a0d5275c70 (patch)
treece61e2252f38e4d607c11c5774fa68d6c876b4c3 /Userland
parent2bd1a62ce1b834492bfdac993b3ff841c729c2fc (diff)
downloadserenity-e923cf6624b4195479e7881d57d933a0d5275c70.zip
strace: Port to LibMain :^)
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Utilities/CMakeLists.txt1
-rw-r--r--Userland/Utilities/strace.cpp75
2 files changed, 22 insertions, 54 deletions
diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt
index af7e417d7f..6c095f1d3b 100644
--- a/Userland/Utilities/CMakeLists.txt
+++ b/Userland/Utilities/CMakeLists.txt
@@ -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)
diff --git a/Userland/Utilities/strace.cpp b/Userland/Utilities/strace.cpp
index f8e7c9cf8a..4851bd7d07 100644
--- a/Userland/Utilities/strace.cpp
+++ b/Userland/Utilities/strace.cpp
@@ -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;