initial
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
bin/
|
17
Makefile
Normal file
17
Makefile
Normal file
@@ -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)
|
5
README.md
Normal file
5
README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# d
|
||||
|
||||
Yet another WIP.
|
||||
|
||||
A dead simple debugger. Not very pretty.
|
12
main.c
Normal file
12
main.c
Normal file
@@ -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;
|
||||
}
|
42
src/debugger.c
Normal file
42
src/debugger.c
Normal file
@@ -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.");
|
||||
}
|
17
src/debugger.h
Normal file
17
src/debugger.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <editline/readline.h>
|
||||
#include <sys/ptrace.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
#define PTRACE_TRACEME PT_TRACE_ME
|
||||
#define PTRACE_CONT PT_CONTINUE
|
||||
#define DATA 0
|
||||
#else
|
||||
#define DATA NULL
|
||||
#endif
|
||||
|
||||
void debug(char*);
|
Reference in New Issue
Block a user