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.
This commit is contained in:
Brian Gianforcaro 2021-04-18 21:02:50 -07:00 committed by Andreas Kling
parent 9de58a2d89
commit cdd9faaf39
Notes: sideshowbarker 2024-07-18 19:23:09 +09:00

View File

@ -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 <PID> requires -e xor -d.\n");
if (!(enable ^ disable ^ wait)) {
fprintf(stderr, "-p <PID> 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;
}