59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
#include <sys/time.h>
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc < 2) {
|
|
printf("Usage: debug <command> [args...]\n");
|
|
return 1;
|
|
}
|
|
|
|
struct timeval start, end;
|
|
|
|
gettimeofday(&start, NULL);
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
|
perror("debug: fork failed");
|
|
return 1;
|
|
}
|
|
|
|
if (pid == 0) {
|
|
execvp(argv[1], &argv[1]);
|
|
|
|
perror("debug: exec failed");
|
|
exit(127);
|
|
}
|
|
else {
|
|
int status;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
gettimeofday(&end, NULL);
|
|
|
|
long seconds = end.tv_sec - start.tv_sec;
|
|
long micros = end.tv_usec - start.tv_usec;
|
|
if (micros < 0) {
|
|
seconds -= 1;
|
|
micros += 1000000;
|
|
}
|
|
double elapsed = seconds + micros / 1000000.0;
|
|
|
|
printf("\n\033[1;33m[Debug Report]\033[0m\n");
|
|
printf("Target : %s\n", argv[1]);
|
|
printf("PID : %d\n", pid);
|
|
printf("Time Elapsed : %.6f sec\n", elapsed);
|
|
|
|
if (WIFEXITED(status)) {
|
|
printf("Exit Code : %d\n", WEXITSTATUS(status));
|
|
} else if (WIFSIGNALED(status)) {
|
|
printf("Terminated by Signal : %d\n", WTERMSIG(status));
|
|
}
|
|
printf("------------------------------\n");
|
|
}
|
|
|
|
return 0;
|
|
} |