summaryrefslogtreecommitdiffhomepage
path: root/src/DebuggerKit/POSIXMachContract.h
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-11-24 03:05:29 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-11-24 03:05:29 +0100
commitbbe2c77243c541ca7e0075149f5be3262eb89523 (patch)
treeae5d59d299344fd19584a2c3642bacd788e841d4 /src/DebuggerKit/POSIXMachContract.h
parentb5adf16a96b9cbb80c74cf30404ed5bcff03ac34 (diff)
feat! breaking changes on necti sources.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'src/DebuggerKit/POSIXMachContract.h')
-rw-r--r--src/DebuggerKit/POSIXMachContract.h147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/DebuggerKit/POSIXMachContract.h b/src/DebuggerKit/POSIXMachContract.h
new file mode 100644
index 0000000..5bd4ba8
--- /dev/null
+++ b/src/DebuggerKit/POSIXMachContract.h
@@ -0,0 +1,147 @@
+/* ========================================
+
+ Copyright (C) 2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license.
+
+======================================== */
+
+#pragma once
+
+#ifdef DK_MACH_DEBUGGER
+
+/// @file POSIXMachContract.h
+/// @brief POSIX Mach debugger.
+
+#include <DebuggerKit/DebuggerContract.h>
+
+CK_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);
+
+CK_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_POKETEXT PT_WRITE_I
+#define PTRACE_CONT PT_CONTINUE
+#define PTRACE_PEEKTEXT PT_READ_I
+
+namespace DebuggerKit::POSIX {
+/// =========================================================== ///
+/// \brief POSIXMachContract engine class in C++
+/// \author Amlal El Mahrouss
+/// =========================================================== ///
+class POSIXMachContract final DK_DEBUGGER_CONTRACT {
+ public:
+ explicit POSIXMachContract() = default;
+ ~POSIXMachContract() override = default;
+
+ public:
+ POSIXMachContract& operator=(const POSIXMachContract&) = default;
+ POSIXMachContract(const POSIXMachContract&) = default;
+
+ public:
+ Bool Attach(CompilerKit::STLString path, CompilerKit::STLString argv,
+ ProcessID& pid) noexcept override {
+ pid = fork();
+
+ if (pid == 0) {
+ if (argv.empty()) {
+ ptrace(PT_TRACE_ME, 0, nullptr, 0);
+ kill(getpid(), SIGSTOP);
+ }
+
+ 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);
+
+ execv(path.c_str(), argv_arr.data());
+
+ _exit(1);
+ }
+
+ m_path = path;
+ m_pid = pid;
+
+ pid = this->m_pid;
+
+ return true;
+ }
+
+ void SetPath(CompilerKit::STLString path) noexcept {
+ if (path.empty()) {
+ return;
+ }
+
+ m_path = path;
+ }
+
+ Bool BreakAt(CompilerKit::STLString 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;
+ }
+
+ auto addr = dlsym(handle, symbol.c_str());
+
+ if (addr == nullptr) {
+ return false;
+ }
+
+ task_read_t task;
+ task_for_pid(mach_task_self(), m_pid, &task);
+
+ 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));
+
+ return true;
+ }
+
+ return false;
+ }
+
+ Bool Break() noexcept override {
+ task_read_t task;
+ task_for_pid(mach_task_self(), m_pid, &task);
+
+ kern_return_t ret = task_suspend(task);
+
+ return ret == KERN_SUCCESS;
+ }
+
+ Bool Continue() noexcept override {
+ task_read_t task;
+ task_for_pid(mach_task_self(), m_pid, &task);
+
+ kern_return_t ret = task_resume(task);
+
+ return ret == KERN_SUCCESS;
+ }
+
+ Bool Detach() noexcept override {
+ this->Continue();
+
+ 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;
+ }
+
+ private:
+ ProcessID m_pid{0};
+ CompilerKit::STLString m_path;
+};
+} // namespace DebuggerKit::POSIX
+
+#endif