mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:27:45 +00:00
strace: Use ArgsParser for argument processing
This commit is contained in:
parent
ccdaa1bea9
commit
aa7148af89
1 changed files with 23 additions and 18 deletions
|
@ -29,6 +29,7 @@
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <Kernel/Syscall.h>
|
#include <Kernel/Syscall.h>
|
||||||
#include <LibC/sys/arch/i386/regs.h>
|
#include <LibC/sys/arch/i386/regs.h>
|
||||||
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -37,12 +38,6 @@
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static int usage()
|
|
||||||
{
|
|
||||||
printf("usage: strace [-p pid] [command...]\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int g_pid = -1;
|
static int g_pid = -1;
|
||||||
|
|
||||||
static void handle_sigint(int)
|
static void handle_sigint(int)
|
||||||
|
@ -57,35 +52,47 @@ static void handle_sigint(int)
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (argc == 1)
|
Vector<const char*> child_argv;
|
||||||
return usage();
|
|
||||||
|
|
||||||
bool spawned_new_process = false;
|
bool spawned_new_process = false;
|
||||||
|
|
||||||
if (!strcmp(argv[1], "-p")) {
|
Core::ArgsParser parser;
|
||||||
if (argc != 3)
|
parser.add_option(g_pid, "Trace the given PID", "pid", 'p', "pid");
|
||||||
return usage();
|
parser.add_positional_argument(child_argv, "Arguments to exec", "argument", Core::ArgsParser::Required::No);
|
||||||
g_pid = atoi(argv[2]);
|
|
||||||
} else {
|
parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if (g_pid == -1) {
|
||||||
|
if (child_argv.is_empty()) {
|
||||||
|
fprintf(stderr, "strace: Expected either a pid or some arguments\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
child_argv.append(nullptr);
|
||||||
spawned_new_process = true;
|
spawned_new_process = true;
|
||||||
int pid = fork();
|
int pid = fork();
|
||||||
|
if (pid < 0) {
|
||||||
|
perror("fork");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!pid) {
|
if (!pid) {
|
||||||
if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) {
|
if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) {
|
||||||
perror("traceme");
|
perror("traceme");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int rc = execvp(argv[1], &argv[1]);
|
int rc = execvp(child_argv.first(), const_cast<char**>(child_argv.data()));
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
perror("execvp");
|
perror("execvp");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g_pid = pid;
|
||||||
if (waitpid(pid, nullptr, WSTOPPED) != pid) {
|
if (waitpid(pid, nullptr, WSTOPPED) != pid) {
|
||||||
perror("waitpid");
|
perror("waitpid");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
g_pid = pid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
@ -171,6 +178,4 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue