diff options
| author | Amlal <amlal.elmahrouss@icloud.com> | 2025-01-27 21:49:14 +0100 |
|---|---|---|
| committer | Amlal <amlal.elmahrouss@icloud.com> | 2025-01-27 21:49:14 +0100 |
| commit | 8aa2adf99fd4fcc4b47c534d2fed5ec7ed6ea334 (patch) | |
| tree | 7d7a2a587ac67ba25305220eeb49308dba7ee03a | |
| parent | f8baa212ab66a3114e4f6e581989861a18f5282c (diff) | |
ADD: LibDebugger C++ library and improved `dbg` tool.
ADD: New manual files `dbg` and `ld64`
Signed-off-by: Amlal <amlal.elmahrouss@icloud.com>
| -rw-r--r-- | dev/LibDebugger/Debugger.h | 102 | ||||
| -rw-r--r-- | dev/LibIDE/TerminalEmulator.h | 7 | ||||
| -rw-r--r-- | man/dbg.7 | 67 | ||||
| -rw-r--r-- | man/ld64.7 | 63 | ||||
| -rw-r--r-- | tools/dbg.cc | 116 | ||||
| -rwxr-xr-x | tools/dbg.sh | 2 |
6 files changed, 247 insertions, 110 deletions
diff --git a/dev/LibDebugger/Debugger.h b/dev/LibDebugger/Debugger.h new file mode 100644 index 0000000..af3217c --- /dev/null +++ b/dev/LibDebugger/Debugger.h @@ -0,0 +1,102 @@ +/*** + (C) 2025 Amlal El Mahrouss + */ + +#include <iostream> +#include <unordered_map> + +#include <sys/ptrace.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/user.h> +#include <unistd.h> +#include <stdint.h> + +namespace LibDebugger +{ + /// \brief Debug IDebugger class in C++ + /// \author Amlal El Mahrouss + class IDebugger final + { + public: + IDebugger() = default; + ~IDebugger() = default; + + IDebugger& operator=(const IDebugger&) = default; + IDebugger(const IDebugger&) = default; + + public: + void Attach(pid_t pid) + { + this->m_pid = pid; + + if (ptrace(PTRACE_ATTACH, this->m_pid, nullptr, nullptr) == -1) + { + perror("dbg: Attach"); + exit(1); + } + + waitpid(m_pid, nullptr, 0); + + std::cout << "[+] Attached to process: " << m_pid << std::endl; + } + + void SetBreakpoint(void* addr) + { + long original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, nullptr); + if (original_data == -1) + { + perror("dbg: Peek"); + exit(1); + } + + long data_with_int3 = (original_data & ~0xFF) | 0xCC; // Insert INT3 (0xCC) + if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1) + { + perror("dbg: Poke"); + exit(1); + } + + std::cout << "[+] Breakpoint set at: " << addr << std::endl; + + m_breakpoints[reinterpret_cast<uintptr_t>(addr)] = original_data; // Store original data + } + + void ContinueExecution() + { + if (ptrace(PTRACE_CONT, m_pid, nullptr, nullptr) == -1) + { + perror("dbg: Cont"); + exit(1); + } + + int status; + waitpid(m_pid, &status, 0); + + if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) + { + std::cout << "[!] Breakpoint hit." << std::endl; + } + } + + void Detach() + { + if (ptrace(PTRACE_DETACH, m_pid, nullptr, nullptr) == -1) + { + perror("dbg: Detach"); + exit(1); + } + + std::cout << "[-] Detached from process: " << m_pid << std::endl; + } + + std::unordered_map<uintptr_t, long>& Breakpoints() + { + return m_breakpoints; + } + + private: + pid_t m_pid; + std::unordered_map<uintptr_t, long> m_breakpoints; + }; +} // namespace LibDebugger diff --git a/dev/LibIDE/TerminalEmulator.h b/dev/LibIDE/TerminalEmulator.h index a155e53..fb8b901 100644 --- a/dev/LibIDE/TerminalEmulator.h +++ b/dev/LibIDE/TerminalEmulator.h @@ -6,5 +6,8 @@ #pragma once -class ITerminalEmulator; -class iTerminalEscapeCode; +namespace LibIDE +{ + class Terminal; + class BuildSystem; +} diff --git a/man/dbg.7 b/man/dbg.7 new file mode 100644 index 0000000..a198885 --- /dev/null +++ b/man/dbg.7 @@ -0,0 +1,67 @@ +.TH DBG 1 "NeKernel Internal Kit" "January 2025" "NeKernel Manual" +.SH NAME +.B dbg +\- NeKernel internal debugger + +.SH SYNOPSIS +.B dbg +[␌IOPTIONS␌R] [␌ICOMMAND␌R] + +.SH DESCRIPTION +.B dbg +is the internal debugging tool for NeKernel. It provides a low-level +interface for inspecting and manipulating running processes, +memory, and kernel state. + +.SH OPTIONS +.TP +.B -p +Attach to a process by its PID. +.TP +.B -s +Start a new debugging session. +.TP +.B -m +Enable memory inspection mode. +.TP +.B -r +Display register values. +.TP +.B -b +Set a breakpoint at a function or address. +.TP +.B -c +Continue execution after a breakpoint. +.TP +.B -h +Display help information. + +.SH USAGE EXAMPLES +.TP +.B Start a new debugging session: +.B dbg -s +.TP +.B Attach to a running process (PID 1234): +.B dbg -p 1234 +.TP +.B Inspect memory at address 0x1000: +.B dbg -m 0x1000 +.TP +.B Set a breakpoint at function "main": +.B dbg -b main +.TP +.B Display register values: +.B dbg -r + +.SH EXIT STATUS +.TP +0 Successful execution. +.TP +1 Error occurred during debugging. + +.SH SEE ALSO +.BR nekernel (7), gdb (1) + +.SH AUTHOR +NeKernel Development Team + diff --git a/man/ld64.7 b/man/ld64.7 new file mode 100644 index 0000000..388a887 --- /dev/null +++ b/man/ld64.7 @@ -0,0 +1,63 @@ +.TH LD64 1 "NeKernel Internal Kit" "January 2025" "NeKernel Manual" +.SH NAME +.B ld64 +\- PEF binary format linker for NeKernel + +.SH SYNOPSIS +.B ld64 +[␌IOPTIONS␌R] [␌IINPUT_FILES␌R] -o ␌IOUTPUT_FILE␌R + +.SH DESCRIPTION +.B ld64 +is the dedicated linker for the Preferred Executable Format (PEF) used by NeKernel. +It links object files into a PEF executable suitable for execution within the NeKernel environment. + +.SH OPTIONS +.TP +.B -o <file> +Specify the output file. +.TP +.B -L <path> +Add a library search path. +.TP +.B -l <lib> +Link against the specified library. +.TP +.B -T <script> +Specify a linker script. +.TP +.B -e <symbol> +Set the entry point symbol. +.TP +.B -M +Display the memory layout of the linked binary. +.TP +.B -v +Enable verbose output. +.TP +.B -h +Show help information. + +.SH USAGE EXAMPLES +.TP +.B Link object files into a PEF binary: +.B ld64 main.o utils.o -o app.exe +.TP +.B Link with a custom entry point: +.B ld64 -e _start main.o -o app.exe +.TP +.B Generate a memory layout report: +.B ld64 -M main.o -o app.exe + +.SH EXIT STATUS +.TP +0 Successful linking. +.TP +1 Error encountered during linking. + +.SH SEE ALSO +.BR nekernel (7), asm (1) + +.SH AUTHOR +NeKernel Development Team + diff --git a/tools/dbg.cc b/tools/dbg.cc index ded2dbd..a6f739a 100644 --- a/tools/dbg.cc +++ b/tools/dbg.cc @@ -2,118 +2,20 @@ (C) 2025 Amlal El Mahrouss */ -#include <iostream> -#include <unordered_map> - -#include <sys/ptrace.h> -#include <sys/types.h> -#include <sys/wait.h> -#include <sys/user.h> -#include <unistd.h> -#include <stdint.h> - -namespace LibDebugger -{ - /// \brief Debug IDebugger class in C++ - /// \author Amlal El Mahrouss - class IDebugger final - { - public: - IDebugger() = default; - ~IDebugger() = default; - - IDebugger& operator=(const IDebugger&) = default; - IDebugger(const IDebugger&) = default; - - public: - void Attach(pid_t pid) - { - this->m_pid = pid; - - if (ptrace(PTRACE_ATTACH, this->m_pid, nullptr, nullptr) == -1) - { - perror("dbg: Attach"); - exit(1); - } - - waitpid(m_pid, nullptr, 0); - - std::cout << "[+] Attached to process: " << m_pid << std::endl; - } - - void SetBreakpoint(void* addr) - { - long original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, nullptr); - if (original_data == -1) - { - perror("dbg: Peek"); - exit(1); - } - - long data_with_int3 = (original_data & ~0xFF) | 0xCC; // Insert INT3 (0xCC) - if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1) - { - perror("dbg: Poke"); - exit(1); - } - - std::cout << "[+] Breakpoint set at: " << addr << std::endl; - - m_breakpoints[reinterpret_cast<uintptr_t>(addr)] = original_data; // Store original data - } - - void ContinueExecution() - { - if (ptrace(PTRACE_CONT, m_pid, nullptr, nullptr) == -1) - { - perror("dbg: Cont"); - exit(1); - } - - int status; - waitpid(m_pid, &status, 0); - - if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) - { - std::cout << "[!] Breakpoint hit." << std::endl; - } - } - - void Detach() - { - if (ptrace(PTRACE_DETACH, m_pid, nullptr, nullptr) == -1) - { - perror("dbg: Detach"); - exit(1); - } - - std::cout << "[-] Detached from process: " << m_pid << std::endl; - } - - std::unordered_map<uintptr_t, long>& Breakpoints() - { - return m_breakpoints; - } - - private: - pid_t m_pid; - std::unordered_map<uintptr_t, long> m_breakpoints; - }; -} // namespace LibDebugger +#include <LibDebugger/Debugger.h> int main(int argc, char* argv[]) { - if (!argv[1]) + LibDebugger::IDebugger debugger; + pid_t pid = 0L; + + if (argc >= 3 && std::string(argv[1]) == "-p" && + argv[2] != nullptr) { - std::cout << "[?] Enter a PID to attach on.\n"; - return EXIT_FAILURE; + pid = std::stoi(argv[2]); + debugger.Attach(pid); } - pid_t pid = std::stoi(argv[1]); - - LibDebugger::IDebugger debugger; - debugger.Attach(pid); - while (true) { std::string cmd; @@ -133,7 +35,7 @@ int main(int argc, char* argv[]) std::getline(std::cin, cmd); pid = std::stoi(cmd.c_str()); - + debugger.Attach(pid); } diff --git a/tools/dbg.sh b/tools/dbg.sh index 8624b93..6689d13 100755 --- a/tools/dbg.sh +++ b/tools/dbg.sh @@ -1,2 +1,2 @@ #!/bin/sh -g++ dbg.cc -o dbg +g++ dbg.cc -std=c++20 -I../dev -o dbg |
