summaryrefslogtreecommitdiffhomepage
path: root/dev
diff options
context:
space:
mode:
Diffstat (limited to 'dev')
-rw-r--r--dev/LibDebugger/DebuggerContract.h42
-rw-r--r--dev/LibDebugger/POSIXContract.h (renamed from dev/LibDebugger/POSIX.h)42
-rw-r--r--dev/LibDebugger/src/POSIXContract.cc (renamed from dev/LibDebugger/src/POSIX.cc)26
3 files changed, 68 insertions, 42 deletions
diff --git a/dev/LibDebugger/DebuggerContract.h b/dev/LibDebugger/DebuggerContract.h
new file mode 100644
index 0000000..5d4f728
--- /dev/null
+++ b/dev/LibDebugger/DebuggerContract.h
@@ -0,0 +1,42 @@
+/***
+ (C) 2025 Amlal El Mahrouss
+ */
+
+#pragma once
+
+#include <iostream>
+#include <unordered_map>
+
+namespace LibDebugger
+{
+ typedef uint64_t ProcessID;
+ 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(ProcessID pid) noexcept = 0;
+ virtual bool Break(CAddress addr) 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:
+ pid_t m_pid;
+ std::unordered_map<uintptr_t, uintptr_t> m_breakpoints;
+ };
+} // namespace LibDebugger::POSIX
diff --git a/dev/LibDebugger/POSIX.h b/dev/LibDebugger/POSIXContract.h
index b863f58..5a8ae48 100644
--- a/dev/LibDebugger/POSIX.h
+++ b/dev/LibDebugger/POSIXContract.h
@@ -8,8 +8,7 @@
#error Windows doesn't have a POSIX subsystem, please combine with windows instead.
#endif
-#include <iostream>
-#include <unordered_map>
+#include <LibDebugger/DebuggerContract.h>
#include <sys/ptrace.h>
#include <sys/types.h>
@@ -28,26 +27,20 @@
namespace LibDebugger::POSIX
{
-#ifdef __APPLE__
- typedef caddr_t CAddr;
-#else
- typedef char* CAddr;
-#endif
-
- /// \brief LocalDebuggerPOSIX engine interface class in C++
+ /// \brief POSIXDebuggerContract engine interface class in C++
/// \author Amlal El Mahrouss
- class LocalDebuggerPOSIX final
+ class POSIXDebuggerContract final : public DebuggerContract
{
public:
- explicit LocalDebuggerPOSIX() = default;
- ~LocalDebuggerPOSIX() = default;
+ explicit POSIXDebuggerContract() = default;
+ ~POSIXDebuggerContract() override = default;
public:
- LocalDebuggerPOSIX& operator=(const LocalDebuggerPOSIX&) = default;
- LocalDebuggerPOSIX(const LocalDebuggerPOSIX&) = default;
+ POSIXDebuggerContract& operator=(const POSIXDebuggerContract&) = default;
+ POSIXDebuggerContract(const POSIXDebuggerContract&) = default;
public:
- bool Attach(pid_t pid)
+ bool Attach(ProcessID pid) noexcept override
{
if (ptrace(PTRACE_ATTACH, pid, nullptr, 0) == -1)
{
@@ -61,7 +54,7 @@ namespace LibDebugger::POSIX
return true;
}
- bool Break(CAddr addr)
+ bool Break(CAddress addr) noexcept override
{
uintptr_t original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0);
@@ -84,7 +77,7 @@ namespace LibDebugger::POSIX
return true;
}
- bool Continue()
+ bool Continue() noexcept override
{
if (ptrace(PTRACE_CONT, m_pid, nullptr, 0) == -1)
{
@@ -94,15 +87,10 @@ namespace LibDebugger::POSIX
int status;
waitpid(m_pid, &status, 0);
- if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
- {
- std::cout << "[!] Breakpoint has been hit!" << std::endl;
- }
-
- return true;
+ return WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP;
}
- bool Detach()
+ bool Detach() noexcept override
{
if (ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1)
{
@@ -112,13 +100,7 @@ namespace LibDebugger::POSIX
return true;
}
- std::unordered_map<uintptr_t, uintptr_t>& Get()
- {
- return m_breakpoints;
- }
-
private:
pid_t m_pid;
- std::unordered_map<uintptr_t, uintptr_t> m_breakpoints;
};
} // namespace LibDebugger::POSIX
diff --git a/dev/LibDebugger/src/POSIX.cc b/dev/LibDebugger/src/POSIXContract.cc
index b8c92ae..3caeabf 100644
--- a/dev/LibDebugger/src/POSIX.cc
+++ b/dev/LibDebugger/src/POSIXContract.cc
@@ -4,17 +4,19 @@
#include <LibCompiler/Defines.h>
#include <Vendor/Dialogs.h>
-#include <LibDebugger/POSIX.h>
+#include <LibDebugger/POSIXContract.h>
#include <cstdint>
#include <string>
#ifndef _WIN32
-static bool kKeepRunning = false;
-static LibDebugger::POSIX::LocalDebuggerPOSIX kDebugger;
-static pid_t kPID = 0L;
+static bool kKeepRunning = false;
+static LibDebugger::POSIX::POSIXDebuggerContract kDebugger;
+static pid_t kPID = 0L;
-static void DebuggerCtrlCHandler(std::int32_t _)
+/// @internal
+/// @brief Handles CTRL-C signal on debugger.
+static void dbgl_ctrlc_handler(std::int32_t _)
{
if (!kPID)
{
@@ -23,7 +25,7 @@ static void DebuggerCtrlCHandler(std::int32_t _)
auto list = kDebugger.Get();
- LibDebugger::POSIX::CAddr addr = (LibDebugger::POSIX::CAddr)list[list.size() - 1];
+ LibDebugger::CAddress addr = (LibDebugger::CAddress)list[list.size() - 1];
if (!addr)
{
@@ -40,10 +42,7 @@ static void DebuggerCtrlCHandler(std::int32_t _)
LIBCOMPILER_MODULE(DebuggerPOSIX)
{
- if (argc < 1)
- {
- return EXIT_FAILURE;
- }
+ 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)
@@ -52,7 +51,7 @@ LIBCOMPILER_MODULE(DebuggerPOSIX)
kDebugger.Attach(kPID);
}
- ::signal(SIGINT, DebuggerCtrlCHandler);
+ ::signal(SIGINT, dbgl_ctrlc_handler);
while (YES)
{
@@ -69,6 +68,9 @@ LIBCOMPILER_MODULE(DebuggerPOSIX)
{
kDebugger.Continue();
kKeepRunning = true;
+
+ std::cout << "[+] Continuing...\n";
+ pfd::notify("Debugger Event", "Continuing...");
}
if (cmd == "d" ||
@@ -113,7 +115,7 @@ LIBCOMPILER_MODULE(DebuggerPOSIX)
pfd::notify("Debugger Event", "Add Breakpoint at: " + cmd);
}
- LibDebugger::POSIX::CAddr breakpoint_addr = reinterpret_cast<LibDebugger::POSIX::CAddr>(addr);
+ LibDebugger::CAddress breakpoint_addr = reinterpret_cast<LibDebugger::CAddress>(addr);
if (breakpoint_addr)
kDebugger.Break(breakpoint_addr);