GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ReClass.NET/NativeCore/Unix/Debugger.cpp at master · ReClassNET/ReClass.NET Expand file tree/ Copy pathDebugger.cppMore file actions 300 lines (255 loc) · 7.73 KB / Copy pathDebugger.cppFile metadata and controls300 lines (255 loc) · 7.73 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 #include <sys/ptrace.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/time.h> #include <sys/user.h> #include <experimental/filesystem> #include <cstddef> #include "NativeCore.hpp" namespace fs = std::experimental::filesystem; int ualarm(unsigned int milliseconds) { struct itimerval nval = { 0 }; nval.it_value.tv_sec = milliseconds / 1000; nval.it_value.tv_usec = static_cast<long int>(milliseconds % 1000) * 1000; struct itimerval oval; if (setitimer(ITIMER_REAL, &nval, &oval) < 0) return 0; else return oval.it_value.tv_sec; } pid_t waitpid_timeout(pid_t pid, int* status, int options, int timeoutInMilliseconds, bool& timedOut) { struct sigaction sig = {}; sig.sa_flags = 0; sig.sa_handler = [](int) {}; sigfillset(&sig.sa_mask); sigaction(SIGALRM, &sig, nullptr); ualarm(timeoutInMilliseconds); auto res = waitpid(pid, status, options); if (res == -1 && errno == EINTR) { timedOut = true; } else { ualarm(0); // Cancel the alarm. timedOut = false; } return res; } pid_t waitpid_timeout(int* status, int timeoutInMilliseconds, bool& timedOut) { return waitpid_timeout(-1, status, 0, timeoutInMilliseconds, timedOut); } extern "C" bool RC_CallConv AttachDebuggerToProcess(RC_Pointer id) { //TODO: Attach to all threads. 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); return false; } extern "C" void RC_CallConv DetachDebuggerFromProcess(RC_Pointer id) { //TODO: Detach to all threads. ptrace(PTRACE_DETACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr); } extern "C" bool RC_CallConv AwaitDebugEvent(DebugEvent* evt, int timeoutInMilliseconds) { int status; bool timedOut; auto tid = waitpid_timeout(&status, timeoutInMilliseconds, timedOut); if (timedOut) { return false; } auto result = false; if (tid > 0) { evt->ThreadId = reinterpret_cast<RC_Pointer>(static_cast<intptr_t>(tid)); siginfo_t si; if (ptrace(PTRACE_GETSIGINFO, tid, nullptr, &si) == 0) { if (si.si_signo == SIGTRAP) { struct user_regs_struct regs; if (ptrace(PTRACE_GETREGS, tid, nullptr, ®s) == 0) { DebugRegister6 dr6; dr6.Value = ptrace(PTRACE_PEEKUSER, tid, offsetof(struct user, u_debugreg[6]), nullptr); // Check if breakpoint was a hardware breakpoint. if (dr6.DR0) { evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::Dr0; } else if (dr6.DR1) { evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::Dr1; } else if (dr6.DR2) { evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::Dr2; } else if (dr6.DR3) { evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::Dr3; } else { evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::InvalidRegister; } // Copy registers. auto& reg = evt->ExceptionInfo.Registers; #ifdef RECLASSNET64 reg.Rax = reinterpret_cast<RC_Pointer>(regs.rax); reg.Rbx = reinterpret_cast<RC_Pointer>(regs.rbx); reg.Rcx = reinterpret_cast<RC_Pointer>(regs.rcx); reg.Rdx = reinterpret_cast<RC_Pointer>(regs.rdx); reg.Rdi = reinterpret_cast<RC_Pointer>(regs.rdi); reg.Rsi = reinterpret_cast<RC_Pointer>(regs.rsi); reg.Rsp = reinterpret_cast<RC_Pointer>(regs.rsp); reg.Rbp = reinterpret_cast<RC_Pointer>(regs.rbp); reg.Rip = reinterpret_cast<RC_Pointer>(regs.rip); reg.R8 = reinterpret_cast<RC_Pointer>(regs.r8); reg.R9 = reinterpret_cast<RC_Pointer>(regs.r9); reg.R10 = reinterpret_cast<RC_Pointer>(regs.r10); reg.R11 = reinterpret_cast<RC_Pointer>(regs.r11); reg.R12 = reinterpret_cast<RC_Pointer>(regs.r12); reg.R13 = reinterpret_cast<RC_Pointer>(regs.r13); reg.R14 = reinterpret_cast<RC_Pointer>(regs.r14); reg.R15 = reinterpret_cast<RC_Pointer>(regs.r15); #else reg.Eax = reinterpret_cast<RC_Pointer>(regs.eax); reg.Ebx = reinterpret_cast<RC_Pointer>(regs.ebx); reg.Ecx = reinterpret_cast<RC_Pointer>(regs.ecx); reg.Edx = reinterpret_cast<RC_Pointer>(regs.edx); reg.Edi = reinterpret_cast<RC_Pointer>(regs.edi); reg.Esi = reinterpret_cast<RC_Pointer>(regs.esi); reg.Esp = reinterpret_cast<RC_Pointer>(regs.esp); reg.Ebp = reinterpret_cast<RC_Pointer>(regs.ebp); reg.Eip = reinterpret_cast<RC_Pointer>(regs.eip); #endif result = true; } } if (result == false) { ptrace(PTRACE_CONT, tid, nullptr, si.si_signo); } } } return result; } extern "C" void RC_CallConv HandleDebugEvent(DebugEvent* evt) { auto tid = static_cast<pid_t>(reinterpret_cast<intptr_t>(evt->ThreadId)); siginfo_t si; if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) == 0) { auto signal = 0; switch (evt->ContinueStatus) { case DebugContinueStatus::Handled: signal = 0; break; case DebugContinueStatus::NotHandled: signal = si.si_signo; break; } if (signal == SIGSTOP) { signal = 0; } ptrace(PTRACE_CONT, tid, nullptr, signal); } } extern "C" bool RC_CallConv SetHardwareBreakpoint(RC_Pointer id, RC_Pointer address, HardwareBreakpointRegister reg, HardwareBreakpointTrigger type, HardwareBreakpointSize size, bool set) { if (reg == HardwareBreakpointRegister::InvalidRegister) { return false; } intptr_t addressValue = 0; auto accessValue = 0; auto lengthValue = 0; if (set) { addressValue = reinterpret_cast<intptr_t>(address); if (type == HardwareBreakpointTrigger::Execute) accessValue = 0; else if (type == HardwareBreakpointTrigger::Access) accessValue = 3; else if (type == HardwareBreakpointTrigger::Write) accessValue = 1; if (size == HardwareBreakpointSize::Size1) lengthValue = 0; else if (size == HardwareBreakpointSize::Size2) lengthValue = 1; else if (size == HardwareBreakpointSize::Size4) lengthValue = 3; else if (size == HardwareBreakpointSize::Size8) lengthValue = 2; } auto tasksPath = fs::path("/proc") / std::to_string(reinterpret_cast<intptr_t>(id)) / "task"; if (fs::is_directory(tasksPath)) { for (auto& d : fs::directory_iterator(tasksPath)) { if (fs::is_directory(d)) { auto taskPath = d.path(); auto name = taskPath.filename().string(); if (is_number(name)) { auto tid = parse_type<size_t>(name); // Stop the thread. TODO: Check if the thread was already paused. for (int i = 0; i < 10; ++i) { kill(tid, SIGSTOP); bool timedOut; waitpid_timeout(tid, nullptr, 0, 100, timedOut); if (!timedOut) { break; } } DebugRegister7 dr7; dr7.Value = ptrace(PTRACE_PEEKUSER, tid, offsetof(struct user, u_debugreg[7]), nullptr); intptr_t registerAddress; switch (reg) { case HardwareBreakpointRegister::Dr0: registerAddress = offsetof(struct user, u_debugreg[0]); dr7.G0 = true; dr7.RW0 = accessValue; dr7.Len0 = lengthValue; break; case HardwareBreakpointRegister::Dr1: registerAddress = offsetof(struct user, u_debugreg[1]); dr7.G1 = true; dr7.RW1 = accessValue; dr7.Len1 = lengthValue; break; case HardwareBreakpointRegister::Dr2: registerAddress = offsetof(struct user, u_debugreg[2]); dr7.G2 = true; dr7.RW2 = accessValue; dr7.Len2 = lengthValue; break; case HardwareBreakpointRegister::Dr3: registerAddress = offsetof(struct user, u_debugreg[3]); dr7.G3 = true; dr7.RW3 = accessValue; dr7.Len3 = lengthValue; break; } ptrace(PTRACE_POKEUSER, tid, registerAddress, addressValue); ptrace(PTRACE_POKEUSER, tid, offsetof(struct user, u_debugreg[7]), dr7.Value); ptrace(PTRACE_CONT, tid, nullptr, nullptr); } } } } return true; } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |