11a3d19ddSKamil Rytarowski //===-- NativeProcessNetBSD.cpp ------------------------------- -*- C++ -*-===// 21a3d19ddSKamil Rytarowski // 31a3d19ddSKamil Rytarowski // The LLVM Compiler Infrastructure 41a3d19ddSKamil Rytarowski // 51a3d19ddSKamil Rytarowski // This file is distributed under the University of Illinois Open Source 61a3d19ddSKamil Rytarowski // License. See LICENSE.TXT for details. 71a3d19ddSKamil Rytarowski // 81a3d19ddSKamil Rytarowski //===----------------------------------------------------------------------===// 91a3d19ddSKamil Rytarowski 101a3d19ddSKamil Rytarowski #include "NativeProcessNetBSD.h" 111a3d19ddSKamil Rytarowski 121a3d19ddSKamil Rytarowski // C Includes 131a3d19ddSKamil Rytarowski 141a3d19ddSKamil Rytarowski // C++ Includes 151a3d19ddSKamil Rytarowski 161a3d19ddSKamil Rytarowski // Other libraries and framework includes 171a3d19ddSKamil Rytarowski #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 18f07a9995SKamil Rytarowski #include "lldb/Core/State.h" 19f07a9995SKamil Rytarowski #include "lldb/Host/HostProcess.h" 20f07a9995SKamil Rytarowski #include "lldb/Host/common/NativeBreakpoint.h" 21f07a9995SKamil Rytarowski #include "lldb/Host/common/NativeRegisterContext.h" 22f07a9995SKamil Rytarowski #include "lldb/Host/posix/ProcessLauncherPosixFork.h" 23f07a9995SKamil Rytarowski #include "lldb/Target/Process.h" 24c1a6b128SPavel Labath #include "llvm/Support/Errno.h" 251a3d19ddSKamil Rytarowski 261a3d19ddSKamil Rytarowski // System includes - They have to be included after framework includes because 271a3d19ddSKamil Rytarowski // they define some 281a3d19ddSKamil Rytarowski // macros which collide with variable names in other modules 29f07a9995SKamil Rytarowski // clang-format off 30f07a9995SKamil Rytarowski #include <sys/types.h> 31f07a9995SKamil Rytarowski #include <sys/ptrace.h> 32f07a9995SKamil Rytarowski #include <sys/sysctl.h> 33f07a9995SKamil Rytarowski #include <sys/wait.h> 34f07a9995SKamil Rytarowski #include <uvm/uvm_prot.h> 35f07a9995SKamil Rytarowski #include <elf.h> 36f07a9995SKamil Rytarowski #include <util.h> 37f07a9995SKamil Rytarowski // clang-format on 381a3d19ddSKamil Rytarowski 391a3d19ddSKamil Rytarowski using namespace lldb; 401a3d19ddSKamil Rytarowski using namespace lldb_private; 411a3d19ddSKamil Rytarowski using namespace lldb_private::process_netbsd; 421a3d19ddSKamil Rytarowski using namespace llvm; 431a3d19ddSKamil Rytarowski 44f07a9995SKamil Rytarowski // Simple helper function to ensure flags are enabled on the given file 45f07a9995SKamil Rytarowski // descriptor. 4697206d57SZachary Turner static Status EnsureFDFlags(int fd, int flags) { 4797206d57SZachary Turner Status error; 48f07a9995SKamil Rytarowski 49f07a9995SKamil Rytarowski int status = fcntl(fd, F_GETFL); 50f07a9995SKamil Rytarowski if (status == -1) { 51f07a9995SKamil Rytarowski error.SetErrorToErrno(); 52f07a9995SKamil Rytarowski return error; 53f07a9995SKamil Rytarowski } 54f07a9995SKamil Rytarowski 55f07a9995SKamil Rytarowski if (fcntl(fd, F_SETFL, status | flags) == -1) { 56f07a9995SKamil Rytarowski error.SetErrorToErrno(); 57f07a9995SKamil Rytarowski return error; 58f07a9995SKamil Rytarowski } 59f07a9995SKamil Rytarowski 60f07a9995SKamil Rytarowski return error; 61f07a9995SKamil Rytarowski } 62f07a9995SKamil Rytarowski 631a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 641a3d19ddSKamil Rytarowski // Public Static Methods 651a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 661a3d19ddSKamil Rytarowski 67*82abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 6896e600fcSPavel Labath NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info, 6996e600fcSPavel Labath NativeDelegate &native_delegate, 7096e600fcSPavel Labath MainLoop &mainloop) const { 71f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 72f07a9995SKamil Rytarowski 7396e600fcSPavel Labath Status status; 7496e600fcSPavel Labath ::pid_t pid = ProcessLauncherPosixFork() 7596e600fcSPavel Labath .LaunchProcess(launch_info, status) 7696e600fcSPavel Labath .GetProcessId(); 7796e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 7896e600fcSPavel Labath if (status.Fail()) { 7996e600fcSPavel Labath LLDB_LOG(log, "failed to launch process: {0}", status); 8096e600fcSPavel Labath return status.ToError(); 81f07a9995SKamil Rytarowski } 82f07a9995SKamil Rytarowski 8396e600fcSPavel Labath // Wait for the child process to trap on its call to execve. 8496e600fcSPavel Labath int wstatus; 8596e600fcSPavel Labath ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); 8696e600fcSPavel Labath assert(wpid == pid); 8796e600fcSPavel Labath (void)wpid; 8896e600fcSPavel Labath if (!WIFSTOPPED(wstatus)) { 8996e600fcSPavel Labath LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}", 9096e600fcSPavel Labath WaitStatus::Decode(wstatus)); 9196e600fcSPavel Labath return llvm::make_error<StringError>("Could not sync with inferior process", 9296e600fcSPavel Labath llvm::inconvertibleErrorCode()); 9396e600fcSPavel Labath } 9496e600fcSPavel Labath LLDB_LOG(log, "inferior started, now in stopped state"); 95f07a9995SKamil Rytarowski 9696e600fcSPavel Labath ArchSpec arch; 9796e600fcSPavel Labath if ((status = ResolveProcessArchitecture(pid, arch)).Fail()) 9896e600fcSPavel Labath return status.ToError(); 9996e600fcSPavel Labath 10096e600fcSPavel Labath // Set the architecture to the exe architecture. 10196e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 10296e600fcSPavel Labath arch.GetArchitectureName()); 10396e600fcSPavel Labath 104*82abefa4SPavel Labath std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD( 10596e600fcSPavel Labath pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, 10696e600fcSPavel Labath arch, mainloop)); 10796e600fcSPavel Labath 108*82abefa4SPavel Labath status = process_up->ReinitializeThreads(); 10996e600fcSPavel Labath if (status.Fail()) 11096e600fcSPavel Labath return status.ToError(); 11196e600fcSPavel Labath 112*82abefa4SPavel Labath for (const auto &thread_sp : process_up->m_threads) { 11396e600fcSPavel Labath static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal( 11496e600fcSPavel Labath SIGSTOP); 11596e600fcSPavel Labath } 116*82abefa4SPavel Labath process_up->SetState(StateType::eStateStopped); 11796e600fcSPavel Labath 118*82abefa4SPavel Labath return std::move(process_up); 119f07a9995SKamil Rytarowski } 120f07a9995SKamil Rytarowski 121*82abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 122*82abefa4SPavel Labath NativeProcessNetBSD::Factory::Attach( 1231a3d19ddSKamil Rytarowski lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 12496e600fcSPavel Labath MainLoop &mainloop) const { 125f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 126f07a9995SKamil Rytarowski LLDB_LOG(log, "pid = {0:x}", pid); 127f07a9995SKamil Rytarowski 128f07a9995SKamil Rytarowski // Retrieve the architecture for the running process. 12996e600fcSPavel Labath ArchSpec arch; 13096e600fcSPavel Labath Status status = ResolveProcessArchitecture(pid, arch); 13196e600fcSPavel Labath if (!status.Success()) 13296e600fcSPavel Labath return status.ToError(); 133f07a9995SKamil Rytarowski 134*82abefa4SPavel Labath std::unique_ptr<NativeProcessNetBSD> process_up( 13596e600fcSPavel Labath new NativeProcessNetBSD(pid, -1, native_delegate, arch, mainloop)); 136f07a9995SKamil Rytarowski 137*82abefa4SPavel Labath status = process_up->Attach(); 13896e600fcSPavel Labath if (!status.Success()) 13996e600fcSPavel Labath return status.ToError(); 140f07a9995SKamil Rytarowski 141*82abefa4SPavel Labath return std::move(process_up); 1421a3d19ddSKamil Rytarowski } 1431a3d19ddSKamil Rytarowski 1441a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 1451a3d19ddSKamil Rytarowski // Public Instance Methods 1461a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 1471a3d19ddSKamil Rytarowski 14896e600fcSPavel Labath NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd, 14996e600fcSPavel Labath NativeDelegate &delegate, 15096e600fcSPavel Labath const ArchSpec &arch, 15196e600fcSPavel Labath MainLoop &mainloop) 15296e600fcSPavel Labath : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) { 15396e600fcSPavel Labath if (m_terminal_fd != -1) { 15496e600fcSPavel Labath Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 15596e600fcSPavel Labath assert(status.Success()); 15696e600fcSPavel Labath } 15796e600fcSPavel Labath 15896e600fcSPavel Labath Status status; 15996e600fcSPavel Labath m_sigchld_handle = mainloop.RegisterSignal( 16096e600fcSPavel Labath SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status); 16196e600fcSPavel Labath assert(m_sigchld_handle && status.Success()); 16296e600fcSPavel Labath } 163f07a9995SKamil Rytarowski 164f07a9995SKamil Rytarowski // Handles all waitpid events from the inferior process. 165f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) { 166f07a9995SKamil Rytarowski switch (signal) { 167f07a9995SKamil Rytarowski case SIGTRAP: 168f07a9995SKamil Rytarowski return MonitorSIGTRAP(pid); 169f07a9995SKamil Rytarowski case SIGSTOP: 170f07a9995SKamil Rytarowski return MonitorSIGSTOP(pid); 171f07a9995SKamil Rytarowski default: 172f07a9995SKamil Rytarowski return MonitorSignal(pid, signal); 173f07a9995SKamil Rytarowski } 174f07a9995SKamil Rytarowski } 175f07a9995SKamil Rytarowski 1763508fc8cSPavel Labath void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) { 177f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 178f07a9995SKamil Rytarowski 1793508fc8cSPavel Labath LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid); 180f07a9995SKamil Rytarowski 181f07a9995SKamil Rytarowski /* Stop Tracking All Threads attached to Process */ 182f07a9995SKamil Rytarowski m_threads.clear(); 183f07a9995SKamil Rytarowski 1843508fc8cSPavel Labath SetExitStatus(status, true); 185f07a9995SKamil Rytarowski 186f07a9995SKamil Rytarowski // Notify delegate that our process has exited. 187f07a9995SKamil Rytarowski SetState(StateType::eStateExited, true); 188f07a9995SKamil Rytarowski } 189f07a9995SKamil Rytarowski 190f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) { 191f07a9995SKamil Rytarowski ptrace_siginfo_t info; 192f07a9995SKamil Rytarowski 193f07a9995SKamil Rytarowski const auto siginfo_err = 194f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 195f07a9995SKamil Rytarowski 196f07a9995SKamil Rytarowski // Get details on the signal raised. 197f07a9995SKamil Rytarowski if (siginfo_err.Success()) { 198f07a9995SKamil Rytarowski // Handle SIGSTOP from LLGS (LLDB GDB Server) 199f07a9995SKamil Rytarowski if (info.psi_siginfo.si_code == SI_USER && 200f07a9995SKamil Rytarowski info.psi_siginfo.si_pid == ::getpid()) { 201f07a9995SKamil Rytarowski /* Stop Tracking All Threads attached to Process */ 202f07a9995SKamil Rytarowski for (const auto &thread_sp : m_threads) { 203f07a9995SKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal( 204f07a9995SKamil Rytarowski SIGSTOP, &info.psi_siginfo); 205f07a9995SKamil Rytarowski } 206f07a9995SKamil Rytarowski } 207f07a9995SKamil Rytarowski } 208f07a9995SKamil Rytarowski } 209f07a9995SKamil Rytarowski 210f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { 211f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 212f07a9995SKamil Rytarowski ptrace_siginfo_t info; 213f07a9995SKamil Rytarowski 214f07a9995SKamil Rytarowski const auto siginfo_err = 215f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 216f07a9995SKamil Rytarowski 217f07a9995SKamil Rytarowski // Get details on the signal raised. 21836e23ecaSKamil Rytarowski if (siginfo_err.Fail()) { 21936e23ecaSKamil Rytarowski return; 22036e23ecaSKamil Rytarowski } 22136e23ecaSKamil Rytarowski 222f07a9995SKamil Rytarowski switch (info.psi_siginfo.si_code) { 223f07a9995SKamil Rytarowski case TRAP_BRKPT: 224f07a9995SKamil Rytarowski for (const auto &thread_sp : m_threads) { 225f07a9995SKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp) 226f07a9995SKamil Rytarowski ->SetStoppedByBreakpoint(); 227f07a9995SKamil Rytarowski FixupBreakpointPCAsNeeded( 228f07a9995SKamil Rytarowski *static_pointer_cast<NativeThreadNetBSD>(thread_sp)); 229f07a9995SKamil Rytarowski } 230f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 231f07a9995SKamil Rytarowski break; 2323eef2b5eSKamil Rytarowski case TRAP_TRACE: 2333eef2b5eSKamil Rytarowski for (const auto &thread_sp : m_threads) { 2343eef2b5eSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedByTrace(); 2353eef2b5eSKamil Rytarowski } 2363eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2373eef2b5eSKamil Rytarowski break; 2383eef2b5eSKamil Rytarowski case TRAP_EXEC: { 23997206d57SZachary Turner Status error = ReinitializeThreads(); 2403eef2b5eSKamil Rytarowski if (error.Fail()) { 2413eef2b5eSKamil Rytarowski SetState(StateType::eStateInvalid); 2423eef2b5eSKamil Rytarowski return; 2433eef2b5eSKamil Rytarowski } 2443eef2b5eSKamil Rytarowski 2453eef2b5eSKamil Rytarowski // Let our delegate know we have just exec'd. 2463eef2b5eSKamil Rytarowski NotifyDidExec(); 2473eef2b5eSKamil Rytarowski 24836e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 24936e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedByExec(); 25036e23ecaSKamil Rytarowski } 2513eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2523eef2b5eSKamil Rytarowski } break; 25336e23ecaSKamil Rytarowski case TRAP_DBREG: { 25436e23ecaSKamil Rytarowski // If a watchpoint was hit, report it 25536e23ecaSKamil Rytarowski uint32_t wp_index; 25697206d57SZachary Turner Status error = 25736e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(m_threads[info.psi_lwpid]) 25836e23ecaSKamil Rytarowski ->GetRegisterContext() 25936e23ecaSKamil Rytarowski ->GetWatchpointHitIndex(wp_index, 26036e23ecaSKamil Rytarowski (uintptr_t)info.psi_siginfo.si_addr); 26136e23ecaSKamil Rytarowski if (error.Fail()) 26236e23ecaSKamil Rytarowski LLDB_LOG(log, 26336e23ecaSKamil Rytarowski "received error while checking for watchpoint hits, pid = " 26436e23ecaSKamil Rytarowski "{0}, LWP = {1}, error = {2}", 26536e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 26636e23ecaSKamil Rytarowski if (wp_index != LLDB_INVALID_INDEX32) { 26736e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 26836e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp) 26936e23ecaSKamil Rytarowski ->SetStoppedByWatchpoint(wp_index); 270f07a9995SKamil Rytarowski } 27136e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 27236e23ecaSKamil Rytarowski break; 27336e23ecaSKamil Rytarowski } 27436e23ecaSKamil Rytarowski 27536e23ecaSKamil Rytarowski // If a breakpoint was hit, report it 27636e23ecaSKamil Rytarowski uint32_t bp_index; 27736e23ecaSKamil Rytarowski error = static_pointer_cast<NativeThreadNetBSD>(m_threads[info.psi_lwpid]) 27836e23ecaSKamil Rytarowski ->GetRegisterContext() 27936e23ecaSKamil Rytarowski ->GetHardwareBreakHitIndex(bp_index, 28036e23ecaSKamil Rytarowski (uintptr_t)info.psi_siginfo.si_addr); 28136e23ecaSKamil Rytarowski if (error.Fail()) 28236e23ecaSKamil Rytarowski LLDB_LOG(log, 28336e23ecaSKamil Rytarowski "received error while checking for hardware " 28436e23ecaSKamil Rytarowski "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", 28536e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 28636e23ecaSKamil Rytarowski if (bp_index != LLDB_INVALID_INDEX32) { 28736e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 28836e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp) 28936e23ecaSKamil Rytarowski ->SetStoppedByBreakpoint(); 29036e23ecaSKamil Rytarowski } 29136e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 29236e23ecaSKamil Rytarowski break; 29336e23ecaSKamil Rytarowski } 29436e23ecaSKamil Rytarowski } break; 295f07a9995SKamil Rytarowski } 296f07a9995SKamil Rytarowski } 297f07a9995SKamil Rytarowski 298f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { 299f07a9995SKamil Rytarowski ptrace_siginfo_t info; 300f07a9995SKamil Rytarowski const auto siginfo_err = 301f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 302f07a9995SKamil Rytarowski 303f07a9995SKamil Rytarowski for (const auto &thread_sp : m_threads) { 304f07a9995SKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal( 305f07a9995SKamil Rytarowski info.psi_siginfo.si_signo, &info.psi_siginfo); 306f07a9995SKamil Rytarowski } 307f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 308f07a9995SKamil Rytarowski } 309f07a9995SKamil Rytarowski 31097206d57SZachary Turner Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 311f07a9995SKamil Rytarowski int data, int *result) { 312f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 31397206d57SZachary Turner Status error; 314f07a9995SKamil Rytarowski int ret; 315f07a9995SKamil Rytarowski 316f07a9995SKamil Rytarowski errno = 0; 317f07a9995SKamil Rytarowski ret = ptrace(req, static_cast<::pid_t>(pid), addr, data); 318f07a9995SKamil Rytarowski 319f07a9995SKamil Rytarowski if (ret == -1) 320f07a9995SKamil Rytarowski error.SetErrorToErrno(); 321f07a9995SKamil Rytarowski 322f07a9995SKamil Rytarowski if (result) 323f07a9995SKamil Rytarowski *result = ret; 324f07a9995SKamil Rytarowski 325f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret); 326f07a9995SKamil Rytarowski 327f07a9995SKamil Rytarowski if (error.Fail()) 328f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace() failed: {0}", error); 329f07a9995SKamil Rytarowski 330f07a9995SKamil Rytarowski return error; 331f07a9995SKamil Rytarowski } 332f07a9995SKamil Rytarowski 33397206d57SZachary Turner Status NativeProcessNetBSD::GetSoftwareBreakpointPCOffset( 334f07a9995SKamil Rytarowski uint32_t &actual_opcode_size) { 335f07a9995SKamil Rytarowski // FIXME put this behind a breakpoint protocol class that can be 336f07a9995SKamil Rytarowski // set per architecture. Need ARM, MIPS support here. 337f07a9995SKamil Rytarowski static const uint8_t g_i386_opcode[] = {0xCC}; 338f07a9995SKamil Rytarowski switch (m_arch.GetMachine()) { 339f07a9995SKamil Rytarowski case llvm::Triple::x86_64: 340f07a9995SKamil Rytarowski actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 34197206d57SZachary Turner return Status(); 342f07a9995SKamil Rytarowski default: 343f07a9995SKamil Rytarowski assert(false && "CPU type not supported!"); 34497206d57SZachary Turner return Status("CPU type not supported"); 345f07a9995SKamil Rytarowski } 346f07a9995SKamil Rytarowski } 347f07a9995SKamil Rytarowski 34897206d57SZachary Turner Status 34997206d57SZachary Turner NativeProcessNetBSD::FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread) { 350f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 35197206d57SZachary Turner Status error; 352f07a9995SKamil Rytarowski // Find out the size of a breakpoint (might depend on where we are in the 353f07a9995SKamil Rytarowski // code). 354f07a9995SKamil Rytarowski NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 355f07a9995SKamil Rytarowski if (!context_sp) { 356f07a9995SKamil Rytarowski error.SetErrorString("cannot get a NativeRegisterContext for the thread"); 357f07a9995SKamil Rytarowski LLDB_LOG(log, "failed: {0}", error); 358f07a9995SKamil Rytarowski return error; 359f07a9995SKamil Rytarowski } 360f07a9995SKamil Rytarowski uint32_t breakpoint_size = 0; 361f07a9995SKamil Rytarowski error = GetSoftwareBreakpointPCOffset(breakpoint_size); 362f07a9995SKamil Rytarowski if (error.Fail()) { 363f07a9995SKamil Rytarowski LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 364f07a9995SKamil Rytarowski return error; 365f07a9995SKamil Rytarowski } else 366f07a9995SKamil Rytarowski LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 36736e23ecaSKamil Rytarowski // First try probing for a breakpoint at a software breakpoint location: PC 36836e23ecaSKamil Rytarowski // - breakpoint size. 369f07a9995SKamil Rytarowski const lldb::addr_t initial_pc_addr = 370f07a9995SKamil Rytarowski context_sp->GetPCfromBreakpointLocation(); 371f07a9995SKamil Rytarowski lldb::addr_t breakpoint_addr = initial_pc_addr; 372f07a9995SKamil Rytarowski if (breakpoint_size > 0) { 373f07a9995SKamil Rytarowski // Do not allow breakpoint probe to wrap around. 374f07a9995SKamil Rytarowski if (breakpoint_addr >= breakpoint_size) 375f07a9995SKamil Rytarowski breakpoint_addr -= breakpoint_size; 376f07a9995SKamil Rytarowski } 377f07a9995SKamil Rytarowski // Check if we stopped because of a breakpoint. 378f07a9995SKamil Rytarowski NativeBreakpointSP breakpoint_sp; 379f07a9995SKamil Rytarowski error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 380f07a9995SKamil Rytarowski if (!error.Success() || !breakpoint_sp) { 381f07a9995SKamil Rytarowski // We didn't find one at a software probe location. Nothing to do. 382f07a9995SKamil Rytarowski LLDB_LOG(log, 383f07a9995SKamil Rytarowski "pid {0} no lldb breakpoint found at current pc with " 384f07a9995SKamil Rytarowski "adjustment: {1}", 385f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 38697206d57SZachary Turner return Status(); 387f07a9995SKamil Rytarowski } 388f07a9995SKamil Rytarowski // If the breakpoint is not a software breakpoint, nothing to do. 389f07a9995SKamil Rytarowski if (!breakpoint_sp->IsSoftwareBreakpoint()) { 390f07a9995SKamil Rytarowski LLDB_LOG( 391f07a9995SKamil Rytarowski log, 392f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 393f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 39497206d57SZachary Turner return Status(); 395f07a9995SKamil Rytarowski } 396f07a9995SKamil Rytarowski // 397f07a9995SKamil Rytarowski // We have a software breakpoint and need to adjust the PC. 398f07a9995SKamil Rytarowski // 399f07a9995SKamil Rytarowski // Sanity check. 400f07a9995SKamil Rytarowski if (breakpoint_size == 0) { 401f07a9995SKamil Rytarowski // Nothing to do! How did we get here? 402f07a9995SKamil Rytarowski LLDB_LOG(log, 403f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, it is software, but the " 404f07a9995SKamil Rytarowski "size is zero, nothing to do (unexpected)", 405f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 40697206d57SZachary Turner return Status(); 407f07a9995SKamil Rytarowski } 408f07a9995SKamil Rytarowski // 409f07a9995SKamil Rytarowski // We have a software breakpoint and need to adjust the PC. 410f07a9995SKamil Rytarowski // 411f07a9995SKamil Rytarowski // Sanity check. 412f07a9995SKamil Rytarowski if (breakpoint_size == 0) { 413f07a9995SKamil Rytarowski // Nothing to do! How did we get here? 414f07a9995SKamil Rytarowski LLDB_LOG(log, 415f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, it is software, but the " 416f07a9995SKamil Rytarowski "size is zero, nothing to do (unexpected)", 417f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 41897206d57SZachary Turner return Status(); 419f07a9995SKamil Rytarowski } 420f07a9995SKamil Rytarowski // Change the program counter. 421f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 422f07a9995SKamil Rytarowski thread.GetID(), initial_pc_addr, breakpoint_addr); 423f07a9995SKamil Rytarowski error = context_sp->SetPC(breakpoint_addr); 424f07a9995SKamil Rytarowski if (error.Fail()) { 425f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 426f07a9995SKamil Rytarowski thread.GetID(), error); 427f07a9995SKamil Rytarowski return error; 428f07a9995SKamil Rytarowski } 429f07a9995SKamil Rytarowski return error; 430f07a9995SKamil Rytarowski } 431f07a9995SKamil Rytarowski 43297206d57SZachary Turner Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { 433f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 434f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 435f07a9995SKamil Rytarowski 436f07a9995SKamil Rytarowski const auto &thread_sp = m_threads[0]; 437f07a9995SKamil Rytarowski const ResumeAction *const action = 438f07a9995SKamil Rytarowski resume_actions.GetActionForThread(thread_sp->GetID(), true); 439f07a9995SKamil Rytarowski 440f07a9995SKamil Rytarowski if (action == nullptr) { 441f07a9995SKamil Rytarowski LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 442f07a9995SKamil Rytarowski thread_sp->GetID()); 44397206d57SZachary Turner return Status(); 444f07a9995SKamil Rytarowski } 445f07a9995SKamil Rytarowski 44697206d57SZachary Turner Status error; 4473eef2b5eSKamil Rytarowski 448f07a9995SKamil Rytarowski switch (action->state) { 449f07a9995SKamil Rytarowski case eStateRunning: { 450f07a9995SKamil Rytarowski // Run the thread, possibly feeding it the signal. 4513eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, 4523eef2b5eSKamil Rytarowski action->signal); 453f07a9995SKamil Rytarowski if (!error.Success()) 454f07a9995SKamil Rytarowski return error; 455f07a9995SKamil Rytarowski for (const auto &thread_sp : m_threads) { 456f07a9995SKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetRunning(); 457f07a9995SKamil Rytarowski } 458f07a9995SKamil Rytarowski SetState(eStateRunning, true); 459f07a9995SKamil Rytarowski break; 460f07a9995SKamil Rytarowski } 461f07a9995SKamil Rytarowski case eStateStepping: 4623eef2b5eSKamil Rytarowski // Run the thread, possibly feeding it the signal. 4633eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, 4643eef2b5eSKamil Rytarowski action->signal); 4653eef2b5eSKamil Rytarowski if (!error.Success()) 4663eef2b5eSKamil Rytarowski return error; 4673eef2b5eSKamil Rytarowski for (const auto &thread_sp : m_threads) { 4683eef2b5eSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStepping(); 4693eef2b5eSKamil Rytarowski } 4703eef2b5eSKamil Rytarowski SetState(eStateStepping, true); 471f07a9995SKamil Rytarowski break; 472f07a9995SKamil Rytarowski 473f07a9995SKamil Rytarowski case eStateSuspended: 474f07a9995SKamil Rytarowski case eStateStopped: 475f07a9995SKamil Rytarowski llvm_unreachable("Unexpected state"); 476f07a9995SKamil Rytarowski 477f07a9995SKamil Rytarowski default: 47897206d57SZachary Turner return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " 479f07a9995SKamil Rytarowski "for pid %" PRIu64 ", tid %" PRIu64, 480f07a9995SKamil Rytarowski __FUNCTION__, StateAsCString(action->state), GetID(), 481f07a9995SKamil Rytarowski thread_sp->GetID()); 482f07a9995SKamil Rytarowski } 483f07a9995SKamil Rytarowski 48497206d57SZachary Turner return Status(); 485f07a9995SKamil Rytarowski } 486f07a9995SKamil Rytarowski 48797206d57SZachary Turner Status NativeProcessNetBSD::Halt() { 48897206d57SZachary Turner Status error; 489f07a9995SKamil Rytarowski 490f07a9995SKamil Rytarowski if (kill(GetID(), SIGSTOP) != 0) 491f07a9995SKamil Rytarowski error.SetErrorToErrno(); 492f07a9995SKamil Rytarowski 493f07a9995SKamil Rytarowski return error; 494f07a9995SKamil Rytarowski } 495f07a9995SKamil Rytarowski 49697206d57SZachary Turner Status NativeProcessNetBSD::Detach() { 49797206d57SZachary Turner Status error; 498f07a9995SKamil Rytarowski 499f07a9995SKamil Rytarowski // Stop monitoring the inferior. 500f07a9995SKamil Rytarowski m_sigchld_handle.reset(); 501f07a9995SKamil Rytarowski 502f07a9995SKamil Rytarowski // Tell ptrace to detach from the process. 503f07a9995SKamil Rytarowski if (GetID() == LLDB_INVALID_PROCESS_ID) 504f07a9995SKamil Rytarowski return error; 505f07a9995SKamil Rytarowski 506f07a9995SKamil Rytarowski return PtraceWrapper(PT_DETACH, GetID()); 507f07a9995SKamil Rytarowski } 508f07a9995SKamil Rytarowski 50997206d57SZachary Turner Status NativeProcessNetBSD::Signal(int signo) { 51097206d57SZachary Turner Status error; 511f07a9995SKamil Rytarowski 512f07a9995SKamil Rytarowski if (kill(GetID(), signo)) 513f07a9995SKamil Rytarowski error.SetErrorToErrno(); 514f07a9995SKamil Rytarowski 515f07a9995SKamil Rytarowski return error; 516f07a9995SKamil Rytarowski } 517f07a9995SKamil Rytarowski 51897206d57SZachary Turner Status NativeProcessNetBSD::Kill() { 519f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 520f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 521f07a9995SKamil Rytarowski 52297206d57SZachary Turner Status error; 523f07a9995SKamil Rytarowski 524f07a9995SKamil Rytarowski switch (m_state) { 525f07a9995SKamil Rytarowski case StateType::eStateInvalid: 526f07a9995SKamil Rytarowski case StateType::eStateExited: 527f07a9995SKamil Rytarowski case StateType::eStateCrashed: 528f07a9995SKamil Rytarowski case StateType::eStateDetached: 529f07a9995SKamil Rytarowski case StateType::eStateUnloaded: 530f07a9995SKamil Rytarowski // Nothing to do - the process is already dead. 531f07a9995SKamil Rytarowski LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 532f07a9995SKamil Rytarowski StateAsCString(m_state)); 533f07a9995SKamil Rytarowski return error; 534f07a9995SKamil Rytarowski 535f07a9995SKamil Rytarowski case StateType::eStateConnected: 536f07a9995SKamil Rytarowski case StateType::eStateAttaching: 537f07a9995SKamil Rytarowski case StateType::eStateLaunching: 538f07a9995SKamil Rytarowski case StateType::eStateStopped: 539f07a9995SKamil Rytarowski case StateType::eStateRunning: 540f07a9995SKamil Rytarowski case StateType::eStateStepping: 541f07a9995SKamil Rytarowski case StateType::eStateSuspended: 542f07a9995SKamil Rytarowski // We can try to kill a process in these states. 543f07a9995SKamil Rytarowski break; 544f07a9995SKamil Rytarowski } 545f07a9995SKamil Rytarowski 546f07a9995SKamil Rytarowski if (kill(GetID(), SIGKILL) != 0) { 547f07a9995SKamil Rytarowski error.SetErrorToErrno(); 548f07a9995SKamil Rytarowski return error; 549f07a9995SKamil Rytarowski } 550f07a9995SKamil Rytarowski 551f07a9995SKamil Rytarowski return error; 552f07a9995SKamil Rytarowski } 553f07a9995SKamil Rytarowski 55497206d57SZachary Turner Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr, 555f07a9995SKamil Rytarowski MemoryRegionInfo &range_info) { 556f07a9995SKamil Rytarowski 557f07a9995SKamil Rytarowski if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 558f07a9995SKamil Rytarowski // We're done. 55997206d57SZachary Turner return Status("unsupported"); 560f07a9995SKamil Rytarowski } 561f07a9995SKamil Rytarowski 56297206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 563f07a9995SKamil Rytarowski if (error.Fail()) { 564f07a9995SKamil Rytarowski return error; 565f07a9995SKamil Rytarowski } 566f07a9995SKamil Rytarowski 567f07a9995SKamil Rytarowski lldb::addr_t prev_base_address = 0; 568f07a9995SKamil Rytarowski // FIXME start by finding the last region that is <= target address using 569f07a9995SKamil Rytarowski // binary search. Data is sorted. 570f07a9995SKamil Rytarowski // There can be a ton of regions on pthreads apps with lots of threads. 571f07a9995SKamil Rytarowski for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 572f07a9995SKamil Rytarowski ++it) { 573f07a9995SKamil Rytarowski MemoryRegionInfo &proc_entry_info = it->first; 574f07a9995SKamil Rytarowski // Sanity check assumption that memory map entries are ascending. 575f07a9995SKamil Rytarowski assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 576f07a9995SKamil Rytarowski "descending memory map entries detected, unexpected"); 577f07a9995SKamil Rytarowski prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 578f07a9995SKamil Rytarowski UNUSED_IF_ASSERT_DISABLED(prev_base_address); 57936e23ecaSKamil Rytarowski // If the target address comes before this entry, indicate distance to 58036e23ecaSKamil Rytarowski // next region. 581f07a9995SKamil Rytarowski if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 582f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 583f07a9995SKamil Rytarowski range_info.GetRange().SetByteSize( 584f07a9995SKamil Rytarowski proc_entry_info.GetRange().GetRangeBase() - load_addr); 585f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 586f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 587f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 588f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 589f07a9995SKamil Rytarowski return error; 590f07a9995SKamil Rytarowski } else if (proc_entry_info.GetRange().Contains(load_addr)) { 591f07a9995SKamil Rytarowski // The target address is within the memory region we're processing here. 592f07a9995SKamil Rytarowski range_info = proc_entry_info; 593f07a9995SKamil Rytarowski return error; 594f07a9995SKamil Rytarowski } 595f07a9995SKamil Rytarowski // The target memory address comes somewhere after the region we just 596f07a9995SKamil Rytarowski // parsed. 597f07a9995SKamil Rytarowski } 598f07a9995SKamil Rytarowski // If we made it here, we didn't find an entry that contained the given 599f07a9995SKamil Rytarowski // address. Return the 60036e23ecaSKamil Rytarowski // load_addr as start and the amount of bytes betwwen load address and the 60136e23ecaSKamil Rytarowski // end of the memory as size. 602f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 603f07a9995SKamil Rytarowski range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 604f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 605f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 606f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 607f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 608f07a9995SKamil Rytarowski return error; 609f07a9995SKamil Rytarowski } 610f07a9995SKamil Rytarowski 61197206d57SZachary Turner Status NativeProcessNetBSD::PopulateMemoryRegionCache() { 612f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 613f07a9995SKamil Rytarowski // If our cache is empty, pull the latest. There should always be at least 614f07a9995SKamil Rytarowski // one memory region if memory region handling is supported. 615f07a9995SKamil Rytarowski if (!m_mem_region_cache.empty()) { 616f07a9995SKamil Rytarowski LLDB_LOG(log, "reusing {0} cached memory region entries", 617f07a9995SKamil Rytarowski m_mem_region_cache.size()); 61897206d57SZachary Turner return Status(); 619f07a9995SKamil Rytarowski } 620f07a9995SKamil Rytarowski 621f07a9995SKamil Rytarowski struct kinfo_vmentry *vm; 622f07a9995SKamil Rytarowski size_t count, i; 623f07a9995SKamil Rytarowski vm = kinfo_getvmmap(GetID(), &count); 624f07a9995SKamil Rytarowski if (vm == NULL) { 625f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 62697206d57SZachary Turner Status error; 627f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 628f07a9995SKamil Rytarowski return error; 629f07a9995SKamil Rytarowski } 630f07a9995SKamil Rytarowski for (i = 0; i < count; i++) { 631f07a9995SKamil Rytarowski MemoryRegionInfo info; 632f07a9995SKamil Rytarowski info.Clear(); 633f07a9995SKamil Rytarowski info.GetRange().SetRangeBase(vm[i].kve_start); 634f07a9995SKamil Rytarowski info.GetRange().SetRangeEnd(vm[i].kve_end); 635f07a9995SKamil Rytarowski info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 636f07a9995SKamil Rytarowski 637f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_READ) 638f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 639f07a9995SKamil Rytarowski else 640f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 641f07a9995SKamil Rytarowski 642f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_WRITE) 643f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 644f07a9995SKamil Rytarowski else 645f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 646f07a9995SKamil Rytarowski 647f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_EXECUTE) 648f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 649f07a9995SKamil Rytarowski else 650f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 651f07a9995SKamil Rytarowski 652f07a9995SKamil Rytarowski if (vm[i].kve_path[0]) 653f07a9995SKamil Rytarowski info.SetName(vm[i].kve_path); 654f07a9995SKamil Rytarowski 655f07a9995SKamil Rytarowski m_mem_region_cache.emplace_back( 656f07a9995SKamil Rytarowski info, FileSpec(info.GetName().GetCString(), true)); 657f07a9995SKamil Rytarowski } 658f07a9995SKamil Rytarowski free(vm); 659f07a9995SKamil Rytarowski 660f07a9995SKamil Rytarowski if (m_mem_region_cache.empty()) { 661f07a9995SKamil Rytarowski // No entries after attempting to read them. This shouldn't happen. 662f07a9995SKamil Rytarowski // Assume we don't support map entries. 663f07a9995SKamil Rytarowski LLDB_LOG(log, "failed to find any vmmap entries, assuming no support " 664f07a9995SKamil Rytarowski "for memory region metadata retrieval"); 665f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 66697206d57SZachary Turner Status error; 667f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 668f07a9995SKamil Rytarowski return error; 669f07a9995SKamil Rytarowski } 670f07a9995SKamil Rytarowski LLDB_LOG(log, "read {0} memory region entries from process {1}", 671f07a9995SKamil Rytarowski m_mem_region_cache.size(), GetID()); 672f07a9995SKamil Rytarowski // We support memory retrieval, remember that. 673f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolYes; 67497206d57SZachary Turner return Status(); 675f07a9995SKamil Rytarowski } 676f07a9995SKamil Rytarowski 67797206d57SZachary Turner Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions, 678f07a9995SKamil Rytarowski lldb::addr_t &addr) { 67997206d57SZachary Turner return Status("Unimplemented"); 680f07a9995SKamil Rytarowski } 681f07a9995SKamil Rytarowski 68297206d57SZachary Turner Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) { 68397206d57SZachary Turner return Status("Unimplemented"); 684f07a9995SKamil Rytarowski } 685f07a9995SKamil Rytarowski 686f07a9995SKamil Rytarowski lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() { 687f07a9995SKamil Rytarowski // punt on this for now 688f07a9995SKamil Rytarowski return LLDB_INVALID_ADDRESS; 689f07a9995SKamil Rytarowski } 690f07a9995SKamil Rytarowski 691f07a9995SKamil Rytarowski size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); } 692f07a9995SKamil Rytarowski 693f07a9995SKamil Rytarowski bool NativeProcessNetBSD::GetArchitecture(ArchSpec &arch) const { 694f07a9995SKamil Rytarowski arch = m_arch; 695f07a9995SKamil Rytarowski return true; 696f07a9995SKamil Rytarowski } 697f07a9995SKamil Rytarowski 69897206d57SZachary Turner Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size, 699f07a9995SKamil Rytarowski bool hardware) { 700f07a9995SKamil Rytarowski if (hardware) 70197206d57SZachary Turner return Status("NativeProcessNetBSD does not support hardware breakpoints"); 702f07a9995SKamil Rytarowski else 703f07a9995SKamil Rytarowski return SetSoftwareBreakpoint(addr, size); 704f07a9995SKamil Rytarowski } 705f07a9995SKamil Rytarowski 70697206d57SZachary Turner Status NativeProcessNetBSD::GetSoftwareBreakpointTrapOpcode( 707f07a9995SKamil Rytarowski size_t trap_opcode_size_hint, size_t &actual_opcode_size, 708f07a9995SKamil Rytarowski const uint8_t *&trap_opcode_bytes) { 709f07a9995SKamil Rytarowski static const uint8_t g_i386_opcode[] = {0xCC}; 710f07a9995SKamil Rytarowski 711f07a9995SKamil Rytarowski switch (m_arch.GetMachine()) { 712f07a9995SKamil Rytarowski case llvm::Triple::x86: 713f07a9995SKamil Rytarowski case llvm::Triple::x86_64: 714f07a9995SKamil Rytarowski trap_opcode_bytes = g_i386_opcode; 715f07a9995SKamil Rytarowski actual_opcode_size = sizeof(g_i386_opcode); 71697206d57SZachary Turner return Status(); 717f07a9995SKamil Rytarowski default: 718f07a9995SKamil Rytarowski assert(false && "CPU type not supported!"); 71997206d57SZachary Turner return Status("CPU type not supported"); 720f07a9995SKamil Rytarowski } 721f07a9995SKamil Rytarowski } 722f07a9995SKamil Rytarowski 72397206d57SZachary Turner Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path, 724f07a9995SKamil Rytarowski FileSpec &file_spec) { 72597206d57SZachary Turner return Status("Unimplemented"); 726f07a9995SKamil Rytarowski } 727f07a9995SKamil Rytarowski 72897206d57SZachary Turner Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name, 729f07a9995SKamil Rytarowski lldb::addr_t &load_addr) { 730f07a9995SKamil Rytarowski load_addr = LLDB_INVALID_ADDRESS; 73197206d57SZachary Turner return Status(); 732f07a9995SKamil Rytarowski } 733f07a9995SKamil Rytarowski 734f07a9995SKamil Rytarowski void NativeProcessNetBSD::SigchldHandler() { 735f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 736f07a9995SKamil Rytarowski // Process all pending waitpid notifications. 737f07a9995SKamil Rytarowski int status; 738c1a6b128SPavel Labath ::pid_t wait_pid = 739c1a6b128SPavel Labath llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG); 740f07a9995SKamil Rytarowski 741f07a9995SKamil Rytarowski if (wait_pid == 0) 742f07a9995SKamil Rytarowski return; // We are done. 743f07a9995SKamil Rytarowski 744f07a9995SKamil Rytarowski if (wait_pid == -1) { 74597206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 746f07a9995SKamil Rytarowski LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error); 747f07a9995SKamil Rytarowski } 748f07a9995SKamil Rytarowski 7493508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 7503508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 7513508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 7523508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 753f07a9995SKamil Rytarowski 754f07a9995SKamil Rytarowski LLDB_LOG(log, 7553508fc8cSPavel Labath "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}", 7563508fc8cSPavel Labath GetID(), wait_pid, status, exited); 757f07a9995SKamil Rytarowski 758f07a9995SKamil Rytarowski if (exited) 7593508fc8cSPavel Labath MonitorExited(wait_pid, wait_status); 7603508fc8cSPavel Labath else { 7614bb74415SKamil Rytarowski assert(wait_status.type == WaitStatus::Stop); 7623508fc8cSPavel Labath MonitorCallback(wait_pid, wait_status.status); 7633508fc8cSPavel Labath } 764f07a9995SKamil Rytarowski } 765f07a9995SKamil Rytarowski 766269eec03SKamil Rytarowski bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { 767269eec03SKamil Rytarowski for (auto thread_sp : m_threads) { 768269eec03SKamil Rytarowski assert(thread_sp && "thread list should not contain NULL threads"); 769269eec03SKamil Rytarowski if (thread_sp->GetID() == thread_id) { 770269eec03SKamil Rytarowski // We have this thread. 771269eec03SKamil Rytarowski return true; 772269eec03SKamil Rytarowski } 773269eec03SKamil Rytarowski } 774269eec03SKamil Rytarowski 775269eec03SKamil Rytarowski // We don't have this thread. 776269eec03SKamil Rytarowski return false; 777269eec03SKamil Rytarowski } 778269eec03SKamil Rytarowski 779f07a9995SKamil Rytarowski NativeThreadNetBSDSP NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { 780f07a9995SKamil Rytarowski 781f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 782f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 783f07a9995SKamil Rytarowski 784f07a9995SKamil Rytarowski assert(!HasThreadNoLock(thread_id) && 785f07a9995SKamil Rytarowski "attempted to add a thread by id that already exists"); 786f07a9995SKamil Rytarowski 787f07a9995SKamil Rytarowski // If this is the first thread, save it as the current thread 788f07a9995SKamil Rytarowski if (m_threads.empty()) 789f07a9995SKamil Rytarowski SetCurrentThreadID(thread_id); 790f07a9995SKamil Rytarowski 791*82abefa4SPavel Labath auto thread_sp = std::make_shared<NativeThreadNetBSD>(*this, thread_id); 792f07a9995SKamil Rytarowski m_threads.push_back(thread_sp); 793f07a9995SKamil Rytarowski return thread_sp; 794f07a9995SKamil Rytarowski } 795f07a9995SKamil Rytarowski 79696e600fcSPavel Labath Status NativeProcessNetBSD::Attach() { 797f07a9995SKamil Rytarowski // Attach to the requested process. 798f07a9995SKamil Rytarowski // An attach will cause the thread to stop with a SIGSTOP. 79996e600fcSPavel Labath Status status = PtraceWrapper(PT_ATTACH, m_pid); 80096e600fcSPavel Labath if (status.Fail()) 80196e600fcSPavel Labath return status; 802f07a9995SKamil Rytarowski 80396e600fcSPavel Labath int wstatus; 804f07a9995SKamil Rytarowski // Need to use WALLSIG otherwise we receive an error with errno=ECHLD 805f07a9995SKamil Rytarowski // At this point we should have a thread stopped if waitpid succeeds. 80696e600fcSPavel Labath if ((wstatus = waitpid(m_pid, NULL, WALLSIG)) < 0) 80796e600fcSPavel Labath return Status(errno, eErrorTypePOSIX); 808f07a9995SKamil Rytarowski 809f07a9995SKamil Rytarowski /* Initialize threads */ 81096e600fcSPavel Labath status = ReinitializeThreads(); 81196e600fcSPavel Labath if (status.Fail()) 81296e600fcSPavel Labath return status; 813f07a9995SKamil Rytarowski 81436e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 81536e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal( 81636e23ecaSKamil Rytarowski SIGSTOP); 81736e23ecaSKamil Rytarowski } 81836e23ecaSKamil Rytarowski 819f07a9995SKamil Rytarowski // Let our process instance know the thread has stopped. 820f07a9995SKamil Rytarowski SetState(StateType::eStateStopped); 82196e600fcSPavel Labath return Status(); 822f07a9995SKamil Rytarowski } 823f07a9995SKamil Rytarowski 82497206d57SZachary Turner Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf, 82597206d57SZachary Turner size_t size, size_t &bytes_read) { 826f07a9995SKamil Rytarowski unsigned char *dst = static_cast<unsigned char *>(buf); 827f07a9995SKamil Rytarowski struct ptrace_io_desc io; 828f07a9995SKamil Rytarowski 829f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 830f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 831f07a9995SKamil Rytarowski 832f07a9995SKamil Rytarowski bytes_read = 0; 833f07a9995SKamil Rytarowski io.piod_op = PIOD_READ_D; 834f07a9995SKamil Rytarowski io.piod_len = size; 835f07a9995SKamil Rytarowski 836f07a9995SKamil Rytarowski do { 837f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_read); 838f07a9995SKamil Rytarowski io.piod_addr = dst + bytes_read; 839f07a9995SKamil Rytarowski 84097206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 841f07a9995SKamil Rytarowski if (error.Fail()) 842f07a9995SKamil Rytarowski return error; 843f07a9995SKamil Rytarowski 844f07a9995SKamil Rytarowski bytes_read = io.piod_len; 845f07a9995SKamil Rytarowski io.piod_len = size - bytes_read; 846f07a9995SKamil Rytarowski } while (bytes_read < size); 847f07a9995SKamil Rytarowski 84897206d57SZachary Turner return Status(); 849f07a9995SKamil Rytarowski } 850f07a9995SKamil Rytarowski 85197206d57SZachary Turner Status NativeProcessNetBSD::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 852f07a9995SKamil Rytarowski size_t size, 853f07a9995SKamil Rytarowski size_t &bytes_read) { 85497206d57SZachary Turner Status error = ReadMemory(addr, buf, size, bytes_read); 855f07a9995SKamil Rytarowski if (error.Fail()) 856f07a9995SKamil Rytarowski return error; 857f07a9995SKamil Rytarowski return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 858f07a9995SKamil Rytarowski } 859f07a9995SKamil Rytarowski 86097206d57SZachary Turner Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf, 861f07a9995SKamil Rytarowski size_t size, size_t &bytes_written) { 862f07a9995SKamil Rytarowski const unsigned char *src = static_cast<const unsigned char *>(buf); 86397206d57SZachary Turner Status error; 864f07a9995SKamil Rytarowski struct ptrace_io_desc io; 865f07a9995SKamil Rytarowski 866f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 867f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 868f07a9995SKamil Rytarowski 869f07a9995SKamil Rytarowski bytes_written = 0; 870f07a9995SKamil Rytarowski io.piod_op = PIOD_WRITE_D; 871f07a9995SKamil Rytarowski io.piod_len = size; 872f07a9995SKamil Rytarowski 873f07a9995SKamil Rytarowski do { 874269eec03SKamil Rytarowski io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written)); 875f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_written); 876f07a9995SKamil Rytarowski 87797206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 878f07a9995SKamil Rytarowski if (error.Fail()) 879f07a9995SKamil Rytarowski return error; 880f07a9995SKamil Rytarowski 881f07a9995SKamil Rytarowski bytes_written = io.piod_len; 882f07a9995SKamil Rytarowski io.piod_len = size - bytes_written; 883f07a9995SKamil Rytarowski } while (bytes_written < size); 884f07a9995SKamil Rytarowski 885f07a9995SKamil Rytarowski return error; 886f07a9995SKamil Rytarowski } 887f07a9995SKamil Rytarowski 888f07a9995SKamil Rytarowski llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 889f07a9995SKamil Rytarowski NativeProcessNetBSD::GetAuxvData() const { 890f07a9995SKamil Rytarowski /* 891f07a9995SKamil Rytarowski * ELF_AUX_ENTRIES is currently restricted to kernel 892f07a9995SKamil Rytarowski * (<sys/exec_elf.h> r. 1.155 specifies 15) 893f07a9995SKamil Rytarowski * 894f07a9995SKamil Rytarowski * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this 895f07a9995SKamil Rytarowski * information isn't needed. 896f07a9995SKamil Rytarowski */ 897f07a9995SKamil Rytarowski size_t auxv_size = 100 * sizeof(AuxInfo); 898f07a9995SKamil Rytarowski 899f07a9995SKamil Rytarowski ErrorOr<std::unique_ptr<MemoryBuffer>> buf = 900f07a9995SKamil Rytarowski llvm::MemoryBuffer::getNewMemBuffer(auxv_size); 901f07a9995SKamil Rytarowski 902269eec03SKamil Rytarowski struct ptrace_io_desc io; 903269eec03SKamil Rytarowski io.piod_op = PIOD_READ_AUXV; 904269eec03SKamil Rytarowski io.piod_offs = 0; 905269eec03SKamil Rytarowski io.piod_addr = const_cast<void *>(static_cast<const void *>(buf.get()->getBufferStart())); 906269eec03SKamil Rytarowski io.piod_len = auxv_size; 907f07a9995SKamil Rytarowski 90897206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 909f07a9995SKamil Rytarowski 910f07a9995SKamil Rytarowski if (error.Fail()) 911f07a9995SKamil Rytarowski return std::error_code(error.GetError(), std::generic_category()); 912f07a9995SKamil Rytarowski 913f07a9995SKamil Rytarowski if (io.piod_len < 1) 914f07a9995SKamil Rytarowski return std::error_code(ECANCELED, std::generic_category()); 915f07a9995SKamil Rytarowski 916f07a9995SKamil Rytarowski return buf; 917f07a9995SKamil Rytarowski } 9183eef2b5eSKamil Rytarowski 91997206d57SZachary Turner Status NativeProcessNetBSD::ReinitializeThreads() { 9203eef2b5eSKamil Rytarowski // Clear old threads 9213eef2b5eSKamil Rytarowski m_threads.clear(); 9223eef2b5eSKamil Rytarowski 9233eef2b5eSKamil Rytarowski // Initialize new thread 9243eef2b5eSKamil Rytarowski struct ptrace_lwpinfo info = {}; 92597206d57SZachary Turner Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 9263eef2b5eSKamil Rytarowski if (error.Fail()) { 9273eef2b5eSKamil Rytarowski return error; 9283eef2b5eSKamil Rytarowski } 9293eef2b5eSKamil Rytarowski // Reinitialize from scratch threads and register them in process 9303eef2b5eSKamil Rytarowski while (info.pl_lwpid != 0) { 9313eef2b5eSKamil Rytarowski NativeThreadNetBSDSP thread_sp = AddThread(info.pl_lwpid); 9323eef2b5eSKamil Rytarowski error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 9333eef2b5eSKamil Rytarowski if (error.Fail()) { 9343eef2b5eSKamil Rytarowski return error; 9353eef2b5eSKamil Rytarowski } 9363eef2b5eSKamil Rytarowski } 9373eef2b5eSKamil Rytarowski 9383eef2b5eSKamil Rytarowski return error; 9393eef2b5eSKamil Rytarowski } 940