diff options
author | Sahan Fernando <sahan.h.fernando@gmail.com> | 2020-12-24 23:08:58 +1100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-24 20:48:54 +0100 |
commit | 3eeb00b003a8ee4413b09d58fd2e8de9a08a2f9c (patch) | |
tree | ec236f4e043725a0dbec2e8141e2d69f5fbfd134 | |
parent | b71edba06d284083bb0e0ab5c7ae1973f149b1f0 (diff) | |
download | serenity-3eeb00b003a8ee4413b09d58fd2e8de9a08a2f9c.zip |
Userland: Add strace parameter for output log file
-rw-r--r-- | Userland/strace.cpp | 18 |
1 files 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 <Kernel/API/Syscall.h> #include <LibC/sys/arch/i386/regs.h> #include <LibCore/ArgsParser.h> +#include <LibCore/File.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> @@ -54,18 +55,31 @@ int main(int argc, char** argv) { Vector<const char*> 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, |