summaryrefslogtreecommitdiffhomepage
path: root/dev/LibDebugger
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-03-26 10:12:50 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-03-26 10:12:50 +0100
commit733edfd7f7ea2dee27a177127730d04821e313db (patch)
tree1ea95799223b3e99bb11cf5c2a95de79b35335a5 /dev/LibDebugger
parentb0d0fb97b2219f524dd8ad40664f2b358f006af9 (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')
-rw-r--r--dev/LibDebugger/POSIXMachContract.h (renamed from dev/LibDebugger/POSIXContract.h)64
-rw-r--r--dev/LibDebugger/src/POSIXMachContract.cc (renamed from dev/LibDebugger/src/POSIXContract.cc)39
2 files changed, 71 insertions, 32 deletions
diff --git a/dev/LibDebugger/POSIXContract.h b/dev/LibDebugger/POSIXMachContract.h
index 5a8ae48..6ff083f 100644
--- a/dev/LibDebugger/POSIXContract.h
+++ b/dev/LibDebugger/POSIXMachContract.h
@@ -5,9 +5,12 @@
#pragma once
#ifdef _WIN32
-#error Windows doesn't have a POSIX subsystem, please combine with windows instead.
+#error Windows doesn't have a POSIX/Mach subsystem, please combine with windows instead.
#endif
+/// @file POSIXMachContract.h
+/// @brief POSIX/Mach debugger.
+
#include <LibDebugger/DebuggerContract.h>
#include <sys/ptrace.h>
@@ -18,6 +21,9 @@
#include <stdint.h>
#ifdef __APPLE__
+#include <mach/mach.h>
+#include <mach/mach_error.h>
+
#define PTRACE_ATTACH PT_ATTACHEXC
#define PTRACE_DETACH PT_DETACH
#define PTRACE_POKETEXT PT_WRITE_I
@@ -27,21 +33,29 @@
namespace LibDebugger::POSIX
{
- /// \brief POSIXDebuggerContract engine interface class in C++
+ /// \brief POSIXMachContract engine interface class in C++
/// \author Amlal El Mahrouss
- class POSIXDebuggerContract final : public DebuggerContract
+ class POSIXMachContract final : public DebuggerContract
{
public:
- explicit POSIXDebuggerContract() = default;
- ~POSIXDebuggerContract() override = default;
+ explicit POSIXMachContract() = default;
+ ~POSIXMachContract() override = default;
public:
- POSIXDebuggerContract& operator=(const POSIXDebuggerContract&) = default;
- POSIXDebuggerContract(const POSIXDebuggerContract&) = default;
+ POSIXMachContract& operator=(const POSIXMachContract&) = default;
+ POSIXMachContract(const POSIXMachContract&) = default;
public:
bool Attach(ProcessID pid) noexcept override
{
+#ifdef __APPLE__
+ if (pid == 0)
+ return false;
+
+ this->m_pid = pid;
+ return true;
+#else
+
if (ptrace(PTRACE_ATTACH, pid, nullptr, 0) == -1)
{
return false;
@@ -52,10 +66,18 @@ namespace LibDebugger::POSIX
waitpid(m_pid, nullptr, 0);
return true;
+#endif
}
bool Break(CAddress addr) noexcept override
{
+#ifdef __APPLE__
+ task_read_t task;
+ task_for_pid(mach_task_self(), m_pid, &task);
+ kern_return_t ret = task_suspend(task);
+
+ return ret == KERN_SUCCESS;
+#else
uintptr_t original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0);
if (original_data == -1)
@@ -75,12 +97,21 @@ namespace LibDebugger::POSIX
m_breakpoints[reinterpret_cast<uintptr_t>(addr)] = original_data; // Store original data
return true;
+#endif
}
bool Continue() noexcept override
{
+#ifdef __APPLE__
+ task_read_t task;
+ task_for_pid(mach_task_self(), m_pid, &task);
+ kern_return_t ret = task_resume(task);
+
+ return ret == KERN_SUCCESS;
+#else
if (ptrace(PTRACE_CONT, m_pid, nullptr, 0) == -1)
{
+
return false;
}
@@ -88,19 +119,26 @@ namespace LibDebugger::POSIX
waitpid(m_pid, &status, 0);
return WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP;
+#endif
}
bool Detach() noexcept override
{
- if (ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1)
- {
- return false;
- }
+#ifdef __APPLE__
+ this->Continue();
- return true;
+ task_read_t task;
+ task_for_pid(mach_task_self(), m_pid, &task);
+
+ kern_return_t kr = mach_port_deallocate(mach_task_self(), task);
+
+ return kr = KERN_SUCCESS;
+#else
+ return ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1;
+#endif
}
private:
- pid_t m_pid;
+ ProcessID m_pid{0};
};
} // namespace LibDebugger::POSIX
diff --git a/dev/LibDebugger/src/POSIXContract.cc b/dev/LibDebugger/src/POSIXMachContract.cc
index 3caeabf..4e7212c 100644
--- a/dev/LibDebugger/src/POSIXContract.cc
+++ b/dev/LibDebugger/src/POSIXMachContract.cc
@@ -4,19 +4,20 @@
#include <LibCompiler/Defines.h>
#include <Vendor/Dialogs.h>
-#include <LibDebugger/POSIXContract.h>
+#include <LibDebugger/POSIXMachContract.h>
#include <cstdint>
#include <string>
#ifndef _WIN32
-static bool kKeepRunning = false;
-static LibDebugger::POSIX::POSIXDebuggerContract kDebugger;
-static pid_t kPID = 0L;
+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 dbgl_ctrlc_handler(std::int32_t _)
+static void dbgi_ctrlc_handler(std::int32_t _)
{
if (!kPID)
{
@@ -25,17 +26,9 @@ static void dbgl_ctrlc_handler(std::int32_t _)
auto list = kDebugger.Get();
- LibDebugger::CAddress addr = (LibDebugger::CAddress)list[list.size() - 1];
+ kDebugger.Break(kActiveAddress);
- if (!addr)
- {
- pfd::notify("Debugger Event", "Invalid breakpoint at: " + std::to_string(list[list.size() - 1]));
- return;
- }
-
- kDebugger.Break(addr);
-
- pfd::notify("Debugger Event", "Breakpoint at: " + std::to_string(list[list.size() - 1]));
+ pfd::notify("Debugger Event", "Breakpoint hit!");
kKeepRunning = false;
}
@@ -51,7 +44,7 @@ LIBCOMPILER_MODULE(DebuggerPOSIX)
kDebugger.Attach(kPID);
}
- ::signal(SIGINT, dbgl_ctrlc_handler);
+ ::signal(SIGINT, dbgi_ctrlc_handler);
while (YES)
{
@@ -64,7 +57,8 @@ LIBCOMPILER_MODULE(DebuggerPOSIX)
std::getline(std::cin, cmd);
if (cmd == "c" ||
- cmd == "cont")
+ cmd == "cont" ||
+ cmd == "continue")
{
kDebugger.Continue();
kKeepRunning = true;
@@ -77,7 +71,9 @@ LIBCOMPILER_MODULE(DebuggerPOSIX)
cmd == "detach")
kDebugger.Detach();
- if (cmd == "attach")
+ if (cmd == "attach" ||
+ cmd == "pid" ||
+ cmd == "a")
{
std::cout << "[?] Enter a PID to attach on: ";
@@ -97,6 +93,7 @@ LIBCOMPILER_MODULE(DebuggerPOSIX)
break;
}
+#ifndef __APPLE__
if (cmd == "break" ||
cmd == "b")
{
@@ -118,8 +115,12 @@ LIBCOMPILER_MODULE(DebuggerPOSIX)
LibDebugger::CAddress breakpoint_addr = reinterpret_cast<LibDebugger::CAddress>(addr);
if (breakpoint_addr)
- kDebugger.Break(breakpoint_addr);
+ {
+ kActiveAddress = breakpoint_addr;
+ kDebugger.Break(kActiveAddress);
+ }
}
+#endif // ifndef __APPLE__
}
return EXIT_SUCCESS;