Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting to OSX #94

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added NativeCore/.DS_Store
Binary file not shown.
Binary file added NativeCore/Dependencies/.DS_Store
Binary file not shown.
Binary file added NativeCore/Dependencies/distorm/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions NativeCore/ReClassNET_Plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#ifdef __linux__
#define RC_CallConv
#elif __APPLE__
#define RC_CallConv
#elif _WIN32
#define RC_CallConv __stdcall
#else
Expand Down
Binary file added NativeCore/Unix/.DS_Store
Binary file not shown.
10 changes: 8 additions & 2 deletions NativeCore/Unix/ControlRemoteProcess.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//#include <sys/types.h>
#include <csignal>

#if __APPLE__
#include <sys/proc_info.h>
#include <libproc.h>
#include <mach/mach_init.h>
#include <mach/mach_vm.h>
#endif
#include "NativeCore.hpp"

extern "C" void RC_CallConv ControlRemoteProcess(RC_Pointer handle, ControlRemoteProcessAction action)
Expand All @@ -14,6 +20,6 @@ extern "C" void RC_CallConv ControlRemoteProcess(RC_Pointer handle, ControlRemot
{
signal = SIGCONT;
}

kill(static_cast<pid_t>(reinterpret_cast<intptr_t>(handle)), signal);
kill(static_cast<pid_t>(reinterpret_cast<intptr_t>(handle)), signal);

}
29 changes: 23 additions & 6 deletions NativeCore/Unix/Debugger.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#include <sys/ptrace.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/user.h>
#ifdef __linux__
#include <sys/ptrace.h>
#include <experimental/filesystem>
#include <cstddef>
#endif


#include "NativeCore.hpp"

#ifdef __linux__
namespace fs = std::experimental::filesystem;
#endif

int ualarm(unsigned int milliseconds)
{
Expand All @@ -24,6 +29,7 @@ int ualarm(unsigned int milliseconds)

pid_t waitpid_timeout(pid_t pid, int* status, int options, int timeoutInMilliseconds, bool& timedOut)
{
#ifdef __linux__
struct sigaction sig = {};
sig.sa_flags = 0;
sig.sa_handler = [](int) {};
Expand All @@ -44,6 +50,9 @@ pid_t waitpid_timeout(pid_t pid, int* status, int options, int timeoutInMillisec
timedOut = false;
}
return res;
#else
return 0;
#endif
}

pid_t waitpid_timeout(int* status, int timeoutInMilliseconds, bool& timedOut)
Expand All @@ -54,25 +63,27 @@ pid_t waitpid_timeout(int* status, int timeoutInMilliseconds, bool& timedOut)
extern "C" bool RC_CallConv AttachDebuggerToProcess(RC_Pointer id)
{
//TODO: Attach to all threads.

#ifdef __linux__
ptrace(PTRACE_ATTACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);

waitpid(-1, nullptr, 0);

ptrace(PTRACE_CONT, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);

#endif
return false;
}

extern "C" void RC_CallConv DetachDebuggerFromProcess(RC_Pointer id)
{
//TODO: Detach to all threads.

#ifdef __linux__
ptrace(PTRACE_DETACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);
#endif
}

extern "C" bool RC_CallConv AwaitDebugEvent(DebugEvent* evt, int timeoutInMilliseconds)
{
#ifdef __linux__
int status;
bool timedOut;

Expand Down Expand Up @@ -167,10 +178,14 @@ extern "C" bool RC_CallConv AwaitDebugEvent(DebugEvent* evt, int timeoutInMillis
}

return result;
#else
return false;
#endif
}

extern "C" void RC_CallConv HandleDebugEvent(DebugEvent* evt)
{
#ifdef __linux__
auto tid = static_cast<pid_t>(reinterpret_cast<intptr_t>(evt->ThreadId));

siginfo_t si;
Expand All @@ -194,10 +209,12 @@ extern "C" void RC_CallConv HandleDebugEvent(DebugEvent* evt)

ptrace(PTRACE_CONT, tid, nullptr, signal);
}
#endif
}

extern "C" bool RC_CallConv SetHardwareBreakpoint(RC_Pointer id, RC_Pointer address, HardwareBreakpointRegister reg, HardwareBreakpointTrigger type, HardwareBreakpointSize size, bool set)
{
#ifdef __linux__
if (reg == HardwareBreakpointRegister::InvalidRegister)
{
return false;
Expand Down Expand Up @@ -295,6 +312,6 @@ extern "C" bool RC_CallConv SetHardwareBreakpoint(RC_Pointer id, RC_Pointer addr
}
}
}

#endif
return true;
}
126 changes: 91 additions & 35 deletions NativeCore/Unix/EnumerateProcesses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,57 @@
#include <string>
#include <sstream>
#include <fstream>
#ifdef __linux__
#include <experimental/filesystem>

#elif __APPLE__
#include <sys/proc_info.h>
#include <libproc.h>
#endif
#include "NativeCore.hpp"

#ifdef __linux__
namespace fs = std::experimental::filesystem;
#endif

// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
#define USE_CUSTOM_READ_SYMLINK

#ifdef USE_CUSTOM_READ_SYMLINK
#include <unistd.h>

fs::path my_read_symlink(const fs::path& p, std::error_code& ec)
{
fs::path symlink_path;

std::string temp(64, '\0');
for (;; temp.resize(temp.size() * 2))
{
ssize_t result;
if ((result = ::readlink(p.c_str(), /*temp.data()*/ &temp[0], temp.size())) == -1)
{
ec.assign(errno, std::system_category());
break;
}
else
{
if (result != static_cast<ssize_t>(temp.size()))
{
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));

ec.clear();

break;
}
}
}

return symlink_path;
}

// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
#ifdef __linux__
#define USE_CUSTOM_READ_SYMLINK

#ifdef USE_CUSTOM_READ_SYMLINK
#include <unistd.h>

fs::path my_read_symlink(const fs::path& p, std::error_code& ec)
{
fs::path symlink_path;

std::string temp(64, '\0');
for (;; temp.resize(temp.size() * 2))
{
ssize_t result;
if ((result = ::readlink(p.c_str(), /*temp.data()*/ &temp[0], temp.size())) == -1)
{
ec.assign(errno, std::system_category());
break;
}
else
{
if (result != static_cast<ssize_t>(temp.size()))
{
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));

ec.clear();

break;
}
}
}

return symlink_path;
}

#endif
#endif

enum class Platform
Expand Down Expand Up @@ -85,7 +95,7 @@ extern "C" void RC_CallConv EnumerateProcesses(EnumerateProcessCallback callback
{
return;
}

#ifdef __linux__
fs::path procPath("/proc");
if (fs::is_directory(procPath))
{
Expand Down Expand Up @@ -134,4 +144,50 @@ extern "C" void RC_CallConv EnumerateProcesses(EnumerateProcessCallback callback
}
}
}
#elif __APPLE__
int procCnt = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
pid_t pids[1024];
memset(pids, 0, sizeof pids);
proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));

for (int i = 0; i < procCnt; i++)
{
if (!pids[i]) continue;
char curPath[PROC_PIDPATHINFO_MAXSIZE];
char curName[PROC_PIDPATHINFO_MAXSIZE];
memset(curPath, 0, sizeof curPath);
proc_pidpath(pids[i], curPath, sizeof curPath);
int len = strlen(curPath);
if (len)
{
int pos = len;
while (pos && curPath[pos] != '/') --pos;
strcpy(curName, curPath + pos + 1);

struct proc_bsdinfo bsd_info;
int error = proc_pidinfo (pids[i], PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
if (error == 0)
continue;

auto platform = Platform::X86;

if (bsd_info.pbi_flags & PROC_FLAG_LP64)
platform = Platform::X64;

#ifdef RECLASSNET64
if (platform == Platform::X64)
#else
if (platform == Platform::X86)
#endif
{
EnumerateProcessData data = {};
data.Id = (size_t)pids[i];
MultiByteToUnicode(curPath, data.Path, PATH_MAXIMUM_LENGTH);
MultiByteToUnicode(curName, data.Name, PATH_MAXIMUM_LENGTH);
callbackProcess(&data);
}

}
}
#endif
}
Loading