diff options
| author | Amlal <amlal@nekernel.org> | 2025-04-25 13:14:01 +0200 |
|---|---|---|
| committer | Amlal <amlal@nekernel.org> | 2025-04-25 13:14:01 +0200 |
| commit | 20042235d1f53ae428aa154e64afdbae5d8d91ad (patch) | |
| tree | 6ea42d1b30505a57301f8ff2916c78ce94ff6eaf /dev/LibDebugger | |
| parent | 0561a8d0a6ae7588309a6e3513bbfeeef5f0aa15 (diff) | |
meta: update .clang-format, format codebase.
Signed-off-by: Amlal <amlal@nekernel.org>
Diffstat (limited to 'dev/LibDebugger')
| -rw-r--r-- | dev/LibDebugger/DebuggerContract.h | 77 | ||||
| -rw-r--r-- | dev/LibDebugger/NeKernelContract.h | 86 | ||||
| -rw-r--r-- | dev/LibDebugger/POSIXMachContract.h | 203 | ||||
| -rw-r--r-- | dev/LibDebugger/src/POSIXMachContract.cc | 168 |
4 files changed, 247 insertions, 287 deletions
diff --git a/dev/LibDebugger/DebuggerContract.h b/dev/LibDebugger/DebuggerContract.h index 0ef88a2..0f4bc10 100644 --- a/dev/LibDebugger/DebuggerContract.h +++ b/dev/LibDebugger/DebuggerContract.h @@ -1,49 +1,44 @@ /*** - (C) 2025 Amlal El Mahrouss + (C) 2025 Amlal El Mahrouss */ #pragma once #include <cstdint> -#include <unordered_map> #include <string> +#include <unordered_map> + +namespace LibDebugger { +class DebuggerContract; + +/// \brief Process ID +typedef uint64_t ProcessID; + +/// \brief Address type, a la BSD. +typedef char* CAddress; + +/// \brief Debugger contract class in C++, as per the design states. +/// \author Amlal El Mahrouss +class DebuggerContract { + public: + explicit DebuggerContract() = default; + virtual ~DebuggerContract() = default; + + public: + DebuggerContract& operator=(const DebuggerContract&) = default; + DebuggerContract(const DebuggerContract&) = default; + + public: + virtual bool Attach(std::string path, std::string argv, ProcessID& pid) noexcept = 0; + virtual bool Breakpoint(std::string symbol) noexcept = 0; + virtual bool Break() noexcept = 0; + virtual bool Continue() noexcept = 0; + virtual bool Detach() noexcept = 0; + + virtual std::unordered_map<uintptr_t, uintptr_t>& Get() { return m_breakpoints; } -namespace LibDebugger -{ - class DebuggerContract; - - /// \brief Process ID - typedef uint64_t ProcessID; - - /// \brief Address type, a la BSD. - typedef char* CAddress; - - /// \brief Debugger contract class in C++, as per the design states. - /// \author Amlal El Mahrouss - class DebuggerContract - { - public: - explicit DebuggerContract() = default; - virtual ~DebuggerContract() = default; - - public: - DebuggerContract& operator=(const DebuggerContract&) = default; - DebuggerContract(const DebuggerContract&) = default; - - public: - virtual bool Attach(std::string path, std::string argv, ProcessID& pid) noexcept = 0; - virtual bool Breakpoint(std::string symbol) noexcept = 0; - virtual bool Break() noexcept = 0; - virtual bool Continue() noexcept = 0; - virtual bool Detach() noexcept = 0; - - virtual std::unordered_map<uintptr_t, uintptr_t>& Get() - { - return m_breakpoints; - } - - protected: - ProcessID m_pid; - std::unordered_map<uintptr_t, uintptr_t> m_breakpoints; - }; -} // namespace LibDebugger + protected: + ProcessID m_pid; + std::unordered_map<uintptr_t, uintptr_t> m_breakpoints; +}; +} // namespace LibDebugger diff --git a/dev/LibDebugger/NeKernelContract.h b/dev/LibDebugger/NeKernelContract.h index e6b40d0..8e54ab2 100644 --- a/dev/LibDebugger/NeKernelContract.h +++ b/dev/LibDebugger/NeKernelContract.h @@ -1,6 +1,6 @@ /*** - (C) 2025 Amlal El Mahrouss + (C) 2025 Amlal El Mahrouss */ #ifndef LD_NEKERNEL_CONTRACT_H @@ -21,47 +21,43 @@ #define kDebugMag3 'G' #define kDebugSourceFile 23 -#define kDebugLine 33 -#define kDebugTeam 43 -#define kDebugEOP 49 - -namespace LibDebugger::NeKernel -{ - class NeKernelContract; - - namespace Detail - { - class NeKernelPortHeader; - - inline constexpr size_t kDebugTypeLen = 256U; - - typedef char rt_debug_type[kDebugTypeLen]; - - class NeKernelPortHeader final - { - public: - int16_t fPort; - int16_t fPortBsy; - }; - } // namespace Detail - - class NeKernelContract : public DebuggerContract - { - public: - NeKernelContract(); - virtual ~NeKernelContract(); - - // Override additional methods from DebuggerContract - virtual bool Attach(std::string path, std::string argv, ProcessID& pid) noexcept override; - virtual bool Breakpoint(std::string symbol) noexcept override; - virtual bool Break() noexcept override; - virtual bool Continue() noexcept override; - virtual bool Detach() noexcept override; - - private: - std::string m_ip_address; - std::string m_port; - }; -} // namespace LibDebugger::NeKernel - -#endif // LD_NEKERNEL_CONTRACT_H
\ No newline at end of file +#define kDebugLine 33 +#define kDebugTeam 43 +#define kDebugEOP 49 + +namespace LibDebugger::NeKernel { +class NeKernelContract; + +namespace Detail { + class NeKernelPortHeader; + + inline constexpr size_t kDebugTypeLen = 256U; + + typedef char rt_debug_type[kDebugTypeLen]; + + class NeKernelPortHeader final { + public: + int16_t fPort; + int16_t fPortBsy; + }; +} // namespace Detail + +class NeKernelContract : public DebuggerContract { + public: + NeKernelContract(); + virtual ~NeKernelContract(); + + // Override additional methods from DebuggerContract + virtual bool Attach(std::string path, std::string argv, ProcessID& pid) noexcept override; + virtual bool Breakpoint(std::string symbol) noexcept override; + virtual bool Break() noexcept override; + virtual bool Continue() noexcept override; + virtual bool Detach() noexcept override; + + private: + std::string m_ip_address; + std::string m_port; +}; +} // namespace LibDebugger::NeKernel + +#endif // LD_NEKERNEL_CONTRACT_H
\ No newline at end of file diff --git a/dev/LibDebugger/POSIXMachContract.h b/dev/LibDebugger/POSIXMachContract.h index fbfe928..5904cd2 100644 --- a/dev/LibDebugger/POSIXMachContract.h +++ b/dev/LibDebugger/POSIXMachContract.h @@ -1,5 +1,5 @@ /*** - (C) 2025 Amlal El Mahrouss + (C) 2025 Amlal El Mahrouss */ #pragma once @@ -9,163 +9,150 @@ /// @file POSIXMachContract.h /// @brief POSIX Mach debugger. -#include <LibDebugger/DebuggerContract.h> #include <LibCompiler/Defines.h> +#include <LibDebugger/DebuggerContract.h> +#include <stdint.h> #include <sys/ptrace.h> #include <sys/types.h> -#include <sys/wait.h> #include <sys/user.h> +#include <sys/wait.h> #include <unistd.h> -#include <stdint.h> #include <filesystem> #include <iostream> +#include <dlfcn.h> +#include <mach-o/dyld.h> #include <mach/mach.h> #include <mach/mach_error.h> #include <signal.h> -#include <mach-o/dyld.h> -#include <dlfcn.h> -LC_IMPORT_C kern_return_t mach_vm_write( - vm_map_t target_task, - mach_vm_address_t address, - vm_offset_t data, - mach_msg_type_number_t dataCnt); +LC_IMPORT_C kern_return_t mach_vm_write(vm_map_t target_task, mach_vm_address_t address, + vm_offset_t data, mach_msg_type_number_t dataCnt); -LC_IMPORT_C kern_return_t mach_vm_protect(vm_map_t target_task, mach_vm_address_t address, mach_vm_size_t size, boolean_t set_maximum, vm_prot_t new_protection); +LC_IMPORT_C kern_return_t mach_vm_protect(vm_map_t target_task, mach_vm_address_t address, + mach_vm_size_t size, boolean_t set_maximum, + vm_prot_t new_protection); -#define PTRACE_ATTACH PT_ATTACHEXC -#define PTRACE_DETACH PT_DETACH +#define PTRACE_ATTACH PT_ATTACHEXC +#define PTRACE_DETACH PT_DETACH #define PTRACE_POKETEXT PT_WRITE_I -#define PTRACE_CONT PT_CONTINUE +#define PTRACE_CONT PT_CONTINUE #define PTRACE_PEEKTEXT PT_READ_I -namespace LibDebugger::POSIX -{ - /// \brief POSIXMachContract engine interface class in C++ - /// \author Amlal El Mahrouss - class POSIXMachContract : public DebuggerContract - { - public: - explicit POSIXMachContract() = default; - ~POSIXMachContract() override = default; +namespace LibDebugger::POSIX { +/// \brief POSIXMachContract engine interface class in C++ +/// \author Amlal El Mahrouss +class POSIXMachContract : public DebuggerContract { + public: + explicit POSIXMachContract() = default; + ~POSIXMachContract() override = default; - public: - POSIXMachContract& operator=(const POSIXMachContract&) = default; - POSIXMachContract(const POSIXMachContract&) = default; + public: + POSIXMachContract& operator=(const POSIXMachContract&) = default; + POSIXMachContract(const POSIXMachContract&) = default; - public: - BOOL Attach(std::string path, std::string argv, ProcessID& pid) noexcept override - { - pid = fork(); + public: + BOOL Attach(std::string path, std::string argv, ProcessID& pid) noexcept override { + pid = fork(); - if (pid == 0) - { - if (argv.empty()) - { - ptrace(PT_TRACE_ME, 0, nullptr, 0); - kill(getpid(), SIGSTOP); - } + if (pid == 0) { + if (argv.empty()) { + ptrace(PT_TRACE_ME, 0, nullptr, 0); + kill(getpid(), SIGSTOP); + } - std::vector<char*> argv_arr; + std::vector<char*> argv_arr; - argv_arr.push_back(const_cast<char*>(path.c_str())); - argv_arr.push_back(const_cast<char*>(argv.c_str())); - argv_arr.push_back(nullptr); + argv_arr.push_back(const_cast<char*>(path.c_str())); + argv_arr.push_back(const_cast<char*>(argv.c_str())); + argv_arr.push_back(nullptr); - execv(path.c_str(), argv_arr.data()); + execv(path.c_str(), argv_arr.data()); - _exit(1); - } + _exit(1); + } - m_path = path; - m_pid = pid; + m_path = path; + m_pid = pid; - pid = this->m_pid; + pid = this->m_pid; - return true; - } + return true; + } - void SetPath(std::string path) noexcept - { - if (path.empty()) - { - return; - } + void SetPath(std::string path) noexcept { + if (path.empty()) { + return; + } - m_path = path; - } + m_path = path; + } - BOOL Breakpoint(std::string symbol) noexcept override - { - if (!m_path.empty() && std::filesystem::exists(m_path) && std::filesystem::is_regular_file(m_path)) - { - auto handle = dlopen(m_path.c_str(), RTLD_LAZY); + BOOL Breakpoint(std::string symbol) noexcept override { + if (!m_path.empty() && std::filesystem::exists(m_path) && + std::filesystem::is_regular_file(m_path)) { + auto handle = dlopen(m_path.c_str(), RTLD_LAZY); - if (handle == nullptr) - { - return false; - } + if (handle == nullptr) { + return false; + } - auto addr = dlsym(handle, symbol.c_str()); + auto addr = dlsym(handle, symbol.c_str()); - if (addr == nullptr) - { - return false; - } + if (addr == nullptr) { + return false; + } - task_read_t task; - task_for_pid(mach_task_self(), m_pid, &task); + task_read_t task; + task_for_pid(mach_task_self(), m_pid, &task); - uint32_t brk_inst = 0xD43E0000; + uint32_t brk_inst = 0xD43E0000; - mach_vm_protect(task, (mach_vm_address_t)addr, sizeof(uint32_t), false, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE); - mach_vm_write(task, (mach_vm_address_t)addr, (vm_offset_t)&brk_inst, sizeof(addr)); + mach_vm_protect(task, (mach_vm_address_t) addr, sizeof(uint32_t), false, + VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE); + mach_vm_write(task, (mach_vm_address_t) addr, (vm_offset_t) &brk_inst, sizeof(addr)); - return true; - } + return true; + } - return false; - } + return false; + } - BOOL Break() noexcept override - { - task_read_t task; - task_for_pid(mach_task_self(), m_pid, &task); + BOOL Break() noexcept override { + task_read_t task; + task_for_pid(mach_task_self(), m_pid, &task); - kern_return_t ret = task_suspend(task); + kern_return_t ret = task_suspend(task); - return ret == KERN_SUCCESS; - } + return ret == KERN_SUCCESS; + } - BOOL Continue() noexcept override - { - task_read_t task; - task_for_pid(mach_task_self(), m_pid, &task); + BOOL Continue() noexcept override { + task_read_t task; + task_for_pid(mach_task_self(), m_pid, &task); - kern_return_t ret = task_resume(task); + kern_return_t ret = task_resume(task); - return ret == KERN_SUCCESS; - } + return ret == KERN_SUCCESS; + } - BOOL Detach() noexcept override - { - this->Continue(); + BOOL Detach() noexcept override { + this->Continue(); - task_read_t task; - task_for_pid(mach_task_self(), m_pid, &task); + task_read_t task; + task_for_pid(mach_task_self(), m_pid, &task); - kern_return_t kr = mach_port_deallocate(mach_task_self(), task); + kern_return_t kr = mach_port_deallocate(mach_task_self(), task); - return kr = KERN_SUCCESS; - } + return kr = KERN_SUCCESS; + } - private: - ProcessID m_pid{0}; - std::string m_path; - }; -} // namespace LibDebugger::POSIX + private: + ProcessID m_pid{0}; + std::string m_path; +}; +} // namespace LibDebugger::POSIX #endif diff --git a/dev/LibDebugger/src/POSIXMachContract.cc b/dev/LibDebugger/src/POSIXMachContract.cc index 2f30f09..2753e9a 100644 --- a/dev/LibDebugger/src/POSIXMachContract.cc +++ b/dev/LibDebugger/src/POSIXMachContract.cc @@ -1,120 +1,102 @@ /*** - (C) 2025 Amlal El Mahrouss + (C) 2025 Amlal El Mahrouss */ - #ifdef __APPLE__ +#ifdef __APPLE__ -#include <iostream> #include <LibCompiler/Defines.h> -#include <Vendor/Dialogs.h> #include <LibDebugger/POSIXMachContract.h> +#include <Vendor/Dialogs.h> #include <cstdint> +#include <iostream> #include <string> -static BOOL kKeepRunning = false; +static BOOL kKeepRunning = false; static LibDebugger::POSIX::POSIXMachContract kDebugger; -static LibDebugger::ProcessID kPID = 0L; -static LibDebugger::CAddress kActiveAddress = nullptr; -static std::string kPath = ""; +static LibDebugger::ProcessID kPID = 0L; +static LibDebugger::CAddress kActiveAddress = nullptr; +static std::string kPath = ""; #define kBlank "\e[0;30m" -#define kRed "\e[0;31m" +#define kRed "\e[0;31m" #define kWhite "\e[0;97m" #define kStdOut (std::cout << kRed << "dbg: " << kWhite) /// @internal /// @brief Handles CTRL-C signal on debugger. -static void dbgi_ctrlc_handler(std::int32_t _) -{ - if (!kPID) - { - return; - } +static void dbgi_ctrlc_handler(std::int32_t _) { + if (!kPID) { + return; + } - auto list = kDebugger.Get(); + auto list = kDebugger.Get(); - kDebugger.Break(); + kDebugger.Break(); - pfd::notify("Debugger Event", "Breakpoint hit!"); + pfd::notify("Debugger Event", "Breakpoint hit!"); - kKeepRunning = false; + kKeepRunning = false; } -LIBCOMPILER_MODULE(DebuggerMachPOSIX) -{ - pfd::notify("Debugger Event", "Userland Debugger\n(C) 2025 Amlal El Mahrouss, all rights reserved."); - - if (argc >= 3 && std::string(argv[1]) == "-p" && - argv[2] != nullptr) - { - kPath = argv[2]; - kDebugger.SetPath(kPath); - - kStdOut << "[+] Path set to: " << kPath << "\n"; - } - - ::signal(SIGINT, dbgi_ctrlc_handler); - - while (YES) - { - if (kKeepRunning) - { - continue; - } - - std::string cmd; - std::getline(std::cin, cmd); - - if (cmd == "c" || - cmd == "cont" || - cmd == "continue") - { - if (kDebugger.Continue()) - { - kKeepRunning = true; - - kStdOut << "[+] Continuing...\n"; - - pfd::notify("Debugger Event", "Continuing..."); - } - } - - if (cmd == "d" || - cmd == "detach") - kDebugger.Detach(); - - if (cmd == "start") - { - kStdOut << "[?] Enter a argument to use: "; - std::getline(std::cin, cmd); - - kDebugger.Attach(kPath, cmd, kPID); - } - - if (cmd == "exit") - { - if (kPID > 0) - kDebugger.Detach(); - - break; - } - - if (cmd == "break" || - cmd == "b") - { - kStdOut << "[?] Enter a symbol to break on: "; - - std::getline(std::cin, cmd); - - if (kDebugger.Breakpoint(cmd)) - { - pfd::notify("Debugger Event", "Add Breakpoint at: " + cmd); - } - } - } - - return EXIT_SUCCESS; +LIBCOMPILER_MODULE(DebuggerMachPOSIX) { + pfd::notify("Debugger Event", + "Userland Debugger\n(C) 2025 Amlal El Mahrouss, all rights reserved."); + + if (argc >= 3 && std::string(argv[1]) == "-p" && argv[2] != nullptr) { + kPath = argv[2]; + kDebugger.SetPath(kPath); + + kStdOut << "[+] Path set to: " << kPath << "\n"; + } + + ::signal(SIGINT, dbgi_ctrlc_handler); + + while (YES) { + if (kKeepRunning) { + continue; + } + + std::string cmd; + std::getline(std::cin, cmd); + + if (cmd == "c" || cmd == "cont" || cmd == "continue") { + if (kDebugger.Continue()) { + kKeepRunning = true; + + kStdOut << "[+] Continuing...\n"; + + pfd::notify("Debugger Event", "Continuing..."); + } + } + + if (cmd == "d" || cmd == "detach") kDebugger.Detach(); + + if (cmd == "start") { + kStdOut << "[?] Enter a argument to use: "; + std::getline(std::cin, cmd); + + kDebugger.Attach(kPath, cmd, kPID); + } + + if (cmd == "exit") { + if (kPID > 0) kDebugger.Detach(); + + break; + } + + if (cmd == "break" || cmd == "b") { + kStdOut << "[?] Enter a symbol to break on: "; + + std::getline(std::cin, cmd); + + if (kDebugger.Breakpoint(cmd)) { + pfd::notify("Debugger Event", "Add Breakpoint at: " + cmd); + } + } + } + + return EXIT_SUCCESS; } #endif
\ No newline at end of file |
