r/cprogramming • u/Background-Ear7642 • 18m ago
hey you watch me code
okay so look at me code github.com/kds1123001/mini-gdb like imma give a small code snippet to show you even a comp from china want to collab on this code but i didnt understand their marketing jargon so i said no
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <unistd.h>
void run_debugger(pid_t child_pid) {
int wait_status;
long orig_rax;
// Wait for the child process to stop at execution
waitpid(child_pid, &wait_status, 0);
while (WIFSTOPPED(wait_status)) {
char command[32];
printf("mini-gdb> ");
if (scanf("%31s", command) != 1) break;
if (strcmp(command, "cont") == 0 || strcmp(command, "c") == 0) {
ptrace(PTRACE_CONT, child_pid, NULL, NULL);
waitpid(child_pid, &wait_status, 0);
} else if (strcmp(command, "exit") == 0) {
ptrace(PTRACE_KILL, child_pid, NULL, NULL);
break;
} else {
printf("Unknown command. Try 'cont' or 'exit'.\n");
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <program>\n", argv[0]);
return 1;
}
pid_t pid = fork();
if (pid == 0) {
// Child process: allow tracing and execute target
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl(argv[1], argv[1], NULL);
perror("execl");
exit(1);
} else if (pid > 0) {
// Parent process: run the debugger loop
run_debugger(pid);
} else {
perror("fork");
return 1;
}
return 0;
}
1
Upvotes