diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-03-26 10:12:50 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-03-26 10:12:50 +0100 |
| commit | 733edfd7f7ea2dee27a177127730d04821e313db (patch) | |
| tree | 1ea95799223b3e99bb11cf5c2a95de79b35335a5 /dev/LibDebugger/src/POSIXMachContract.cc | |
| parent | b0d0fb97b2219f524dd8ad40664f2b358f006af9 (diff) | |
feat(dbg): made it work on OS X, using mach API.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/LibDebugger/src/POSIXMachContract.cc')
| -rw-r--r-- | dev/LibDebugger/src/POSIXMachContract.cc | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/dev/LibDebugger/src/POSIXMachContract.cc b/dev/LibDebugger/src/POSIXMachContract.cc new file mode 100644 index 0000000..4e7212c --- /dev/null +++ b/dev/LibDebugger/src/POSIXMachContract.cc @@ -0,0 +1,129 @@ +/*** + (C) 2025 Amlal El Mahrouss + */ + +#include <LibCompiler/Defines.h> +#include <Vendor/Dialogs.h> +#include <LibDebugger/POSIXMachContract.h> +#include <cstdint> +#include <string> + +#ifndef _WIN32 + +static bool kKeepRunning = false; +static LibDebugger::POSIX::POSIXMachContract kDebugger; +static pid_t kPID = 0L; +static LibDebugger::CAddress kActiveAddress = nullptr; + +/// @internal +/// @brief Handles CTRL-C signal on debugger. +static void dbgi_ctrlc_handler(std::int32_t _) +{ + if (!kPID) + { + return; + } + + auto list = kDebugger.Get(); + + kDebugger.Break(kActiveAddress); + + pfd::notify("Debugger Event", "Breakpoint hit!"); + + kKeepRunning = false; +} + +LIBCOMPILER_MODULE(DebuggerPOSIX) +{ + pfd::notify("Debugger Event", "NeKernel Debugger\n(C) 2025 Amlal El Mahrouss, all rights reserved."); + + if (argc >= 3 && std::string(argv[1]) == "-p" && + argv[2] != nullptr) + { + kPID = std::stoi(argv[2]); + kDebugger.Attach(kPID); + } + + ::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") + { + kDebugger.Continue(); + kKeepRunning = true; + + std::cout << "[+] Continuing...\n"; + pfd::notify("Debugger Event", "Continuing..."); + } + + if (cmd == "d" || + cmd == "detach") + kDebugger.Detach(); + + if (cmd == "attach" || + cmd == "pid" || + cmd == "a") + { + std::cout << "[?] Enter a PID to attach on: "; + + std::getline(std::cin, cmd); + kPID = std::stoi(cmd.c_str()); + + pfd::notify("Debugger Event", "Attach process: " + std::to_string(kPID)); + + kDebugger.Attach(kPID); + } + + if (cmd == "exit") + { + if (kPID > 0) + kDebugger.Detach(); + + break; + } + +#ifndef __APPLE__ + if (cmd == "break" || + cmd == "b") + { + std::cout << "[?] Enter an address/symbol to add a break on: "; + + std::getline(std::cin, cmd); + + auto addr = std::stoul(cmd.c_str(), nullptr, 16); + + try + { + pfd::notify("Debugger Event", "Add Breakpoint at: " + std::to_string(addr)); + } + catch (...) + { + pfd::notify("Debugger Event", "Add Breakpoint at: " + cmd); + } + + LibDebugger::CAddress breakpoint_addr = reinterpret_cast<LibDebugger::CAddress>(addr); + + if (breakpoint_addr) + { + kActiveAddress = breakpoint_addr; + kDebugger.Break(kActiveAddress); + } + } +#endif // ifndef __APPLE__ + } + + return EXIT_SUCCESS; +} + +#endif
\ No newline at end of file |
