summaryrefslogtreecommitdiffhomepage
path: root/dev/LibDebugger
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2025-02-09 09:15:30 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2025-02-09 09:18:33 +0100
commit106adcb98390d41ced28ecbbd0b661d3056023d2 (patch)
tree4c2af2b0b6b09c5d70eaf882df6990345f147ca2 /dev/LibDebugger
parentb4bcdc8482f8deae2d25018b4f91593570cf9cf5 (diff)
ADD: Add LibDebugger as a separate dylib, fix depreciation warning for
ptrace. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/LibDebugger')
-rw-r--r--dev/LibDebugger/IDebuggerEngine.h (renamed from dev/LibDebugger/IDebugger.h)34
-rw-r--r--dev/LibDebugger/src/DebuggerEnginePOSIX.cc70
2 files changed, 90 insertions, 14 deletions
diff --git a/dev/LibDebugger/IDebugger.h b/dev/LibDebugger/IDebuggerEngine.h
index 957cea9..aefe117 100644
--- a/dev/LibDebugger/IDebugger.h
+++ b/dev/LibDebugger/IDebuggerEngine.h
@@ -13,7 +13,7 @@
#include <stdint.h>
#ifdef __APPLE__
-#define PTRACE_ATTACH PT_ATTACH
+#define PTRACE_ATTACH PT_ATTACHEXC
#define PTRACE_DETACH PT_DETACH
#define PTRACE_POKETEXT PT_WRITE_I
#define PTRACE_CONT PT_CONTINUE
@@ -22,38 +22,43 @@
namespace LibDebugger
{
- typedef char* VmAddress;
+#ifdef __APPLE__
+ typedef caddr_t CAddr;
+#else
+ typedef char* CAddr;
+#endif
- /// \brief Debugger interface class in C++
+ /// \brief Debugger engine interface class in C++
/// \author Amlal El Mahrouss
- class IDebugger final
+ class IDebuggerEngine final
{
public:
- IDebugger() = default;
- ~IDebugger() = default;
+ explicit IDebuggerEngine() = default;
+ ~IDebuggerEngine() = default;
- IDebugger& operator=(const IDebugger&) = default;
- IDebugger(const IDebugger&) = default;
+ public:
+ IDebuggerEngine& operator=(const IDebuggerEngine&) = default;
+ IDebuggerEngine(const IDebuggerEngine&) = default;
public:
void Attach(pid_t pid)
{
- this->m_pid = pid;
-
- if (ptrace(PTRACE_ATTACH, this->m_pid, nullptr, 0) == -1)
+ if (ptrace(PTRACE_ATTACH, pid, nullptr, 0) == -1)
{
perror("dbg: Attach");
return;
}
+ this->m_pid = pid;
+
waitpid(m_pid, nullptr, 0);
std::cout << "[+] Attached to process: " << m_pid << std::endl;
}
- void SetBreakpoint(VmAddress addr)
+ void SetBreakpoint(CAddr addr)
{
- long original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0);
+ auto original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0);
if (original_data == -1)
{
@@ -61,7 +66,8 @@ namespace LibDebugger
return;
}
- long data_with_int3 = (original_data & ~0xFF) | 0xCC; // Insert INT3 (0xCC)
+ auto data_with_int3 = (original_data & ~0xFF) | 0xCC; // Insert INT3 (0xCC)
+
if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1)
{
perror("dbg: Poke");
diff --git a/dev/LibDebugger/src/DebuggerEnginePOSIX.cc b/dev/LibDebugger/src/DebuggerEnginePOSIX.cc
new file mode 100644
index 0000000..e8a394e
--- /dev/null
+++ b/dev/LibDebugger/src/DebuggerEnginePOSIX.cc
@@ -0,0 +1,70 @@
+/***
+ (C) 2025 Amlal El Mahrouss
+ */
+
+#include <LibCompiler/Defines.h>
+#include <LibDebugger/IDebuggerEngine.h>
+
+#ifndef _WIN32
+
+LIBCOMPILER_MODULE(DebuggerPOSIX)
+{
+ LibDebugger::IDebuggerEngine debugger;
+ pid_t pid = 0L;
+
+ if (argc >= 3 && std::string(argv[1]) == "-p" &&
+ argv[2] != nullptr)
+ {
+ pid = std::stoi(argv[2]);
+ debugger.Attach(pid);
+ }
+
+ while (YES)
+ {
+ std::string cmd;
+ std::getline(std::cin, cmd);
+
+ if (cmd == "c" ||
+ cmd == "cont")
+ debugger.ContinueExecution();
+
+ if (cmd == "d" ||
+ cmd == "detach")
+ debugger.Detach();
+
+ if (cmd == "attach")
+ {
+ std::cout << "[?] Enter a PID to attach on: ";
+
+ std::getline(std::cin, cmd);
+ pid = std::stoi(cmd.c_str());
+
+ debugger.Attach(pid);
+ }
+
+ if (cmd == "exit")
+ {
+ if (pid > 0)
+ debugger.Detach();
+
+ break;
+ }
+
+ if (cmd == "break" ||
+ cmd == "b")
+ {
+ std::cout << "[?] Enter an address to add a breakpoint on: ";
+
+ std::getline(std::cin, cmd);
+
+ LibDebugger::CAddr breakpoint_addr = reinterpret_cast<LibDebugger::CAddr>(std::stoul(cmd.c_str(), nullptr, 16));
+
+ if (breakpoint_addr)
+ debugger.SetBreakpoint(breakpoint_addr);
+ }
+ }
+
+ return 0;
+}
+
+#endif \ No newline at end of file