commit 7bae3bcc578a3b4ae304c20ad4595db8503f4d72 Author: YANNIS Date: Fri May 23 00:57:27 2025 +0200 first commit diff --git a/Debugger.cpp b/Debugger.cpp new file mode 100644 index 0000000..39d629e --- /dev/null +++ b/Debugger.cpp @@ -0,0 +1,38 @@ +// Debugger.cpp +#include "Debugger.hpp" +#include +#include +#include +#include +#include + +Debugger::Debugger(const std::string& prog_name) : program_name(prog_name) {} + +void Debugger::run() { + child_pid = fork(); + if (child_pid == 0) { + run_target(); + } else if (child_pid > 0) { + run_debugger(); + } else { + std::cerr << "[!] fork() failed.\n"; + } +} + +void Debugger::run_target() { + std::cout << "[+] Child process started.\n"; + ptrace(PTRACE_TRACEME, 0, nullptr, nullptr); + execl(program_name.c_str(), program_name.c_str(), nullptr); +} + +void Debugger::run_debugger() { + int status; + waitpid(child_pid, &status, 0); + std::cout << "[+] Debugger attached to PID: " << child_pid << "\n"; + + ptrace(PTRACE_CONT, child_pid, nullptr, nullptr); + waitpid(child_pid, &status, 0); + + std::cout << "[+] Child process exited.\n"; +} + diff --git a/Debugger.hpp b/Debugger.hpp new file mode 100644 index 0000000..99e9a05 --- /dev/null +++ b/Debugger.hpp @@ -0,0 +1,18 @@ +// Debugger.hpp +#pragma once +#include +#include + +class Debugger { +public: + Debugger(const std::string& prog_name); + void run(); + +private: + void run_target(); + void run_debugger(); + + std::string program_name; + pid_t child_pid; +}; + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b283e8b --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +# Makefile +CXX = g++ +CXXFLAGS = -Wall -Wextra -Werror + +SRC = main.cpp Debugger.cpp +OBJ = $(SRC:.cpp=.o) +TARGET = nullDBG + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(CXX) $(CXXFLAGS) -o $@ $^ + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(TARGET) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..57bc45e --- /dev/null +++ b/main.cpp @@ -0,0 +1,15 @@ +// main.cpp +#include "Debugger.hpp" +#include + +int main(int argc, char* argv[]) { + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " \n"; + return -1; + } + + Debugger dbg(argv[1]); + dbg.run(); + return 0; +} +