From cdd9faaf39d711071d22777ed581590234781407 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Sun, 18 Apr 2021 21:02:50 -0700 Subject: [PATCH] profile: Add an option to wait for user input to disable profiling. Often you want to enable and then disable profiling after a period of time. To make this slightly more ergonomic, add an option to wait for user input after enabling profiling, this will disable profiling on exit after the key press. --- Userland/Utilities/profile.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Userland/Utilities/profile.cpp b/Userland/Utilities/profile.cpp index 41ea8b65e5..8e8168b045 100644 --- a/Userland/Utilities/profile.cpp +++ b/Userland/Utilities/profile.cpp @@ -36,6 +36,7 @@ int main(int argc, char** argv) const char* pid_argument = nullptr; const char* cmd_argument = nullptr; + bool wait = false; bool enable = false; bool disable = false; bool all_processes = false; @@ -44,6 +45,7 @@ int main(int argc, char** argv) args_parser.add_option(all_processes, "Profile all processes (super-user only)", nullptr, 'a'); args_parser.add_option(enable, "Enable", nullptr, 'e'); args_parser.add_option(disable, "Disable", nullptr, 'd'); + args_parser.add_option(wait, "Enable profiling and wait for user input to disable.", nullptr, 'w'); args_parser.add_option(cmd_argument, "Command", nullptr, 'c', "command"); args_parser.parse(argc, argv); @@ -54,25 +56,33 @@ int main(int argc, char** argv) } if (pid_argument || all_processes) { - if (!(enable ^ disable)) { - fprintf(stderr, "-p requires -e xor -d.\n"); + if (!(enable ^ disable ^ wait)) { + fprintf(stderr, "-p requires -e xor -d xor -w.\n"); return 1; } pid_t pid = all_processes ? -1 : atoi(pid_argument); - if (enable) { + if (wait || enable) { if (profiling_enable(pid) < 0) { perror("profiling_enable"); return 1; } - return 0; + + if (!wait) + return 0; + } + + if (wait) { + outln("Profiling enabled, waiting for user input to disable..."); + (void)getchar(); } if (profiling_disable(pid) < 0) { perror("profiling_disable"); return 1; } + outln("Profiling disabled."); return 0; }