From 3eeb00b003a8ee4413b09d58fd2e8de9a08a2f9c Mon Sep 17 00:00:00 2001 From: Sahan Fernando Date: Thu, 24 Dec 2020 23:08:58 +1100 Subject: [PATCH] Userland: Add strace parameter for output log file --- Userland/strace.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Userland/strace.cpp b/Userland/strace.cpp index c929da6699..3e7f809787 100644 --- a/Userland/strace.cpp +++ b/Userland/strace.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -54,18 +55,31 @@ int main(int argc, char** argv) { Vector child_argv; + const char* output_filename = nullptr; + auto trace_file = Core::File::standard_output(); + Core::ArgsParser parser; parser.set_general_help( "Trace all syscalls and their result."); parser.add_option(g_pid, "Trace the given PID", "pid", 'p', "pid"); + parser.add_option(output_filename, "Filename to write output to", "output", 'o', "output"); parser.add_positional_argument(child_argv, "Arguments to exec", "argument", Core::ArgsParser::Required::No); parser.parse(argc, argv); + if (output_filename != nullptr) { + auto open_result = Core::File::open(output_filename, Core::IODevice::OpenMode::WriteOnly); + if (open_result.is_error()) { + outln(stderr, "Failed to open output file: {}", open_result.error()); + return 1; + } + trace_file = open_result.value(); + } + int status; if (g_pid == -1) { if (child_argv.is_empty()) { - fprintf(stderr, "strace: Expected either a pid or some arguments\n"); + outln(stderr, "strace: Expected either a pid or some arguments\n"); return 1; } @@ -145,7 +159,7 @@ int main(int argc, char** argv) u32 res = regs.eax; - fprintf(stderr, "%s(0x%x, 0x%x, 0x%x)\t=%d\n", + trace_file->printf("%s(0x%x, 0x%x, 0x%x)\t=%d\n", Syscall::to_string( (Syscall::Function)syscall_index), arg1,