commit 6f6b5b934e8d92d7ec94922fefebe8e3aa5a43ce Author: hellerve Date: Sun Sep 10 23:09:27 2017 +0200 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eabc434 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +TARGET=d +BUILDDIR=bin/ +PREFIX=/usr/local/bin/ +SOURCES=$(wildcard src/*.c) +MAIN=main.c +override CFLAGS+=-Werror -Wall -g -fPIC -O2 -DNDEBUG -ftrapv -Wfloat-equal -Wundef -Wwrite-strings -Wuninitialized -pedantic -std=c11 +override LDFLAGS+=-ledit + +all: main.c + mkdir -p $(BUILDDIR) + $(CC) $(MAIN) $(SOURCES) -o $(BUILDDIR)$(TARGET) $(CFLAGS) $(LDFLAGS) + +install: all + install $(BUILDDIR)$(TARGET) $(PREFIX)$(TARGET) + +uninstall: + rm -rf $(PREFIX)$(TARGET) diff --git a/README.md b/README.md new file mode 100644 index 0000000..f1ca6aa --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# d + +Yet another WIP. + +A dead simple debugger. Not very pretty. diff --git a/main.c b/main.c new file mode 100644 index 0000000..69b6744 --- /dev/null +++ b/main.c @@ -0,0 +1,12 @@ +#include "src/debugger.h" + +int main(int argc, char** argv) { + if (argc != 2) { + printf("usage: %s PROGRAM\n", argv[0]); + return 1; + } + + debug(argv[1]); + + return 0; +} diff --git a/src/debugger.c b/src/debugger.c new file mode 100644 index 0000000..025bc98 --- /dev/null +++ b/src/debugger.c @@ -0,0 +1,42 @@ +#include "debugger.h" + +void run_target(char* program) { + if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) perror("Error while creating child: ptrace failed"); + else execl(program, program, NULL); +} + +void debug_continue(pid_t pid) { + int status; + ptrace(PTRACE_CONT, pid, NULL, 0); + + waitpid(pid, &status, 0); +} + +void debug_loop(char* command, pid_t pid) { + if (!strcmp(command, "c")) { + debug_continue(pid); + } else { + printf("%s: Unknown command\n", command); + } +} + +void run_debugger(pid_t pid) { + int status; + char* line = NULL; + + waitpid(pid, &status, 0); + + while((line = readline("minidbg> ")) != NULL) { + debug_loop(line, pid); + add_history(line); + free(line); + } +} + +void debug(char* program) { + pid_t pid = fork(); + + if (!pid) run_target(program); + else if (pid > 0) run_debugger(pid); + else perror("Error while creating debugger: unable to fork."); +} diff --git a/src/debugger.h b/src/debugger.h new file mode 100644 index 0000000..23fecea --- /dev/null +++ b/src/debugger.h @@ -0,0 +1,17 @@ +#include +#include +#include + +#include +#include +#include + +#ifdef __APPLE__ +#define PTRACE_TRACEME PT_TRACE_ME +#define PTRACE_CONT PT_CONTINUE +#define DATA 0 +#else +#define DATA NULL +#endif + +void debug(char*);