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 6782abefa4SPavel 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 9636e82208SPavel Labath ProcessInstanceInfo Info; 9736e82208SPavel Labath if (!Host::GetProcessInfo(pid, Info)) { 9836e82208SPavel Labath return llvm::make_error<StringError>("Cannot get process architecture", 9936e82208SPavel Labath llvm::inconvertibleErrorCode()); 10036e82208SPavel Labath } 10196e600fcSPavel Labath 10296e600fcSPavel Labath // Set the architecture to the exe architecture. 10396e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 10436e82208SPavel Labath Info.GetArchitecture().GetArchitectureName()); 10596e600fcSPavel Labath 10682abefa4SPavel Labath std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD( 10796e600fcSPavel Labath pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, 10836e82208SPavel Labath Info.GetArchitecture(), mainloop)); 10996e600fcSPavel Labath 11082abefa4SPavel Labath status = process_up->ReinitializeThreads(); 11196e600fcSPavel Labath if (status.Fail()) 11296e600fcSPavel Labath return status.ToError(); 11396e600fcSPavel Labath 114a5be48b3SPavel Labath for (const auto &thread : process_up->m_threads) 115a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); 116*8a4bf06bSKamil Rytarowski process_up->SetState(StateType::eStateStopped, false); 11796e600fcSPavel Labath 11882abefa4SPavel Labath return std::move(process_up); 119f07a9995SKamil Rytarowski } 120f07a9995SKamil Rytarowski 12182abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 12282abefa4SPavel 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. 12936e82208SPavel Labath ProcessInstanceInfo Info; 13036e82208SPavel Labath if (!Host::GetProcessInfo(pid, Info)) { 13136e82208SPavel Labath return llvm::make_error<StringError>("Cannot get process architecture", 13236e82208SPavel Labath llvm::inconvertibleErrorCode()); 13336e82208SPavel Labath } 134f07a9995SKamil Rytarowski 13536e82208SPavel Labath std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD( 13636e82208SPavel Labath pid, -1, native_delegate, Info.GetArchitecture(), mainloop)); 137f07a9995SKamil Rytarowski 13802d4e50eSPavel Labath Status status = process_up->Attach(); 13996e600fcSPavel Labath if (!status.Success()) 14096e600fcSPavel Labath return status.ToError(); 141f07a9995SKamil Rytarowski 14282abefa4SPavel Labath return std::move(process_up); 1431a3d19ddSKamil Rytarowski } 1441a3d19ddSKamil Rytarowski 1451a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 1461a3d19ddSKamil Rytarowski // Public Instance Methods 1471a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 1481a3d19ddSKamil Rytarowski 14996e600fcSPavel Labath NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd, 15096e600fcSPavel Labath NativeDelegate &delegate, 15196e600fcSPavel Labath const ArchSpec &arch, 15296e600fcSPavel Labath MainLoop &mainloop) 15396e600fcSPavel Labath : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) { 15496e600fcSPavel Labath if (m_terminal_fd != -1) { 15596e600fcSPavel Labath Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 15696e600fcSPavel Labath assert(status.Success()); 15796e600fcSPavel Labath } 15896e600fcSPavel Labath 15996e600fcSPavel Labath Status status; 16096e600fcSPavel Labath m_sigchld_handle = mainloop.RegisterSignal( 16196e600fcSPavel Labath SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status); 16296e600fcSPavel Labath assert(m_sigchld_handle && status.Success()); 16396e600fcSPavel Labath } 164f07a9995SKamil Rytarowski 165f07a9995SKamil Rytarowski // Handles all waitpid events from the inferior process. 166f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) { 167f07a9995SKamil Rytarowski switch (signal) { 168f07a9995SKamil Rytarowski case SIGTRAP: 169f07a9995SKamil Rytarowski return MonitorSIGTRAP(pid); 170f07a9995SKamil Rytarowski case SIGSTOP: 171f07a9995SKamil Rytarowski return MonitorSIGSTOP(pid); 172f07a9995SKamil Rytarowski default: 173f07a9995SKamil Rytarowski return MonitorSignal(pid, signal); 174f07a9995SKamil Rytarowski } 175f07a9995SKamil Rytarowski } 176f07a9995SKamil Rytarowski 1773508fc8cSPavel Labath void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) { 178f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 179f07a9995SKamil Rytarowski 1803508fc8cSPavel Labath LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid); 181f07a9995SKamil Rytarowski 182f07a9995SKamil Rytarowski /* Stop Tracking All Threads attached to Process */ 183f07a9995SKamil Rytarowski m_threads.clear(); 184f07a9995SKamil Rytarowski 1853508fc8cSPavel Labath SetExitStatus(status, true); 186f07a9995SKamil Rytarowski 187f07a9995SKamil Rytarowski // Notify delegate that our process has exited. 188f07a9995SKamil Rytarowski SetState(StateType::eStateExited, true); 189f07a9995SKamil Rytarowski } 190f07a9995SKamil Rytarowski 191f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) { 192f07a9995SKamil Rytarowski ptrace_siginfo_t info; 193f07a9995SKamil Rytarowski 194f07a9995SKamil Rytarowski const auto siginfo_err = 195f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 196f07a9995SKamil Rytarowski 197f07a9995SKamil Rytarowski // Get details on the signal raised. 198f07a9995SKamil Rytarowski if (siginfo_err.Success()) { 199f07a9995SKamil Rytarowski // Handle SIGSTOP from LLGS (LLDB GDB Server) 200f07a9995SKamil Rytarowski if (info.psi_siginfo.si_code == SI_USER && 201f07a9995SKamil Rytarowski info.psi_siginfo.si_pid == ::getpid()) { 202a5be48b3SPavel Labath /* Stop Tracking all Threads attached to Process */ 203a5be48b3SPavel Labath for (const auto &thread : m_threads) { 204a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( 205f07a9995SKamil Rytarowski SIGSTOP, &info.psi_siginfo); 206f07a9995SKamil Rytarowski } 207f07a9995SKamil Rytarowski } 208f07a9995SKamil Rytarowski } 209f07a9995SKamil Rytarowski } 210f07a9995SKamil Rytarowski 211f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { 212f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 213f07a9995SKamil Rytarowski ptrace_siginfo_t info; 214f07a9995SKamil Rytarowski 215f07a9995SKamil Rytarowski const auto siginfo_err = 216f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 217f07a9995SKamil Rytarowski 218f07a9995SKamil Rytarowski // Get details on the signal raised. 21936e23ecaSKamil Rytarowski if (siginfo_err.Fail()) { 22036e23ecaSKamil Rytarowski return; 22136e23ecaSKamil Rytarowski } 22236e23ecaSKamil Rytarowski 223f07a9995SKamil Rytarowski switch (info.psi_siginfo.si_code) { 224f07a9995SKamil Rytarowski case TRAP_BRKPT: 225a5be48b3SPavel Labath for (const auto &thread : m_threads) { 226a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); 227a5be48b3SPavel Labath FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread)); 228f07a9995SKamil Rytarowski } 229f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 230f07a9995SKamil Rytarowski break; 2313eef2b5eSKamil Rytarowski case TRAP_TRACE: 232a5be48b3SPavel Labath for (const auto &thread : m_threads) 233a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace(); 2343eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2353eef2b5eSKamil Rytarowski break; 2363eef2b5eSKamil Rytarowski case TRAP_EXEC: { 23797206d57SZachary Turner Status error = ReinitializeThreads(); 2383eef2b5eSKamil Rytarowski if (error.Fail()) { 2393eef2b5eSKamil Rytarowski SetState(StateType::eStateInvalid); 2403eef2b5eSKamil Rytarowski return; 2413eef2b5eSKamil Rytarowski } 2423eef2b5eSKamil Rytarowski 2433eef2b5eSKamil Rytarowski // Let our delegate know we have just exec'd. 2443eef2b5eSKamil Rytarowski NotifyDidExec(); 2453eef2b5eSKamil Rytarowski 246a5be48b3SPavel Labath for (const auto &thread : m_threads) 247a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec(); 2483eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2493eef2b5eSKamil Rytarowski } break; 25036e23ecaSKamil Rytarowski case TRAP_DBREG: { 25136e23ecaSKamil Rytarowski // If a watchpoint was hit, report it 25236e23ecaSKamil Rytarowski uint32_t wp_index; 253a5be48b3SPavel Labath Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid]) 254a5be48b3SPavel Labath .GetRegisterContext() 255d37349f3SPavel Labath .GetWatchpointHitIndex( 256a5be48b3SPavel Labath wp_index, (uintptr_t)info.psi_siginfo.si_addr); 25736e23ecaSKamil Rytarowski if (error.Fail()) 25836e23ecaSKamil Rytarowski LLDB_LOG(log, 25936e23ecaSKamil Rytarowski "received error while checking for watchpoint hits, pid = " 26036e23ecaSKamil Rytarowski "{0}, LWP = {1}, error = {2}", 26136e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 26236e23ecaSKamil Rytarowski if (wp_index != LLDB_INVALID_INDEX32) { 263a5be48b3SPavel Labath for (const auto &thread : m_threads) 264a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint( 265a5be48b3SPavel Labath wp_index); 26636e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 26736e23ecaSKamil Rytarowski break; 26836e23ecaSKamil Rytarowski } 26936e23ecaSKamil Rytarowski 27036e23ecaSKamil Rytarowski // If a breakpoint was hit, report it 27136e23ecaSKamil Rytarowski uint32_t bp_index; 272a5be48b3SPavel Labath error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid]) 273a5be48b3SPavel Labath .GetRegisterContext() 274d37349f3SPavel Labath .GetHardwareBreakHitIndex(bp_index, 27536e23ecaSKamil Rytarowski (uintptr_t)info.psi_siginfo.si_addr); 27636e23ecaSKamil Rytarowski if (error.Fail()) 27736e23ecaSKamil Rytarowski LLDB_LOG(log, 27836e23ecaSKamil Rytarowski "received error while checking for hardware " 27936e23ecaSKamil Rytarowski "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", 28036e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 28136e23ecaSKamil Rytarowski if (bp_index != LLDB_INVALID_INDEX32) { 282a5be48b3SPavel Labath for (const auto &thread : m_threads) 283a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); 28436e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 28536e23ecaSKamil Rytarowski break; 28636e23ecaSKamil Rytarowski } 28736e23ecaSKamil Rytarowski } break; 288f07a9995SKamil Rytarowski } 289f07a9995SKamil Rytarowski } 290f07a9995SKamil Rytarowski 291f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { 292f07a9995SKamil Rytarowski ptrace_siginfo_t info; 293f07a9995SKamil Rytarowski const auto siginfo_err = 294f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 295f07a9995SKamil Rytarowski 296a5be48b3SPavel Labath for (const auto &thread : m_threads) { 297a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( 298f07a9995SKamil Rytarowski info.psi_siginfo.si_signo, &info.psi_siginfo); 299f07a9995SKamil Rytarowski } 300f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 301f07a9995SKamil Rytarowski } 302f07a9995SKamil Rytarowski 30397206d57SZachary Turner Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 304f07a9995SKamil Rytarowski int data, int *result) { 305f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 30697206d57SZachary Turner Status error; 307f07a9995SKamil Rytarowski int ret; 308f07a9995SKamil Rytarowski 309f07a9995SKamil Rytarowski errno = 0; 310f07a9995SKamil Rytarowski ret = ptrace(req, static_cast<::pid_t>(pid), addr, data); 311f07a9995SKamil Rytarowski 312f07a9995SKamil Rytarowski if (ret == -1) 313f07a9995SKamil Rytarowski error.SetErrorToErrno(); 314f07a9995SKamil Rytarowski 315f07a9995SKamil Rytarowski if (result) 316f07a9995SKamil Rytarowski *result = ret; 317f07a9995SKamil Rytarowski 318f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret); 319f07a9995SKamil Rytarowski 320f07a9995SKamil Rytarowski if (error.Fail()) 321f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace() failed: {0}", error); 322f07a9995SKamil Rytarowski 323f07a9995SKamil Rytarowski return error; 324f07a9995SKamil Rytarowski } 325f07a9995SKamil Rytarowski 32697206d57SZachary Turner Status NativeProcessNetBSD::GetSoftwareBreakpointPCOffset( 327f07a9995SKamil Rytarowski uint32_t &actual_opcode_size) { 328f07a9995SKamil Rytarowski // FIXME put this behind a breakpoint protocol class that can be 329f07a9995SKamil Rytarowski // set per architecture. Need ARM, MIPS support here. 330f07a9995SKamil Rytarowski static const uint8_t g_i386_opcode[] = {0xCC}; 331f07a9995SKamil Rytarowski switch (m_arch.GetMachine()) { 332f07a9995SKamil Rytarowski case llvm::Triple::x86_64: 333f07a9995SKamil Rytarowski actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 33497206d57SZachary Turner return Status(); 335f07a9995SKamil Rytarowski default: 336f07a9995SKamil Rytarowski assert(false && "CPU type not supported!"); 33797206d57SZachary Turner return Status("CPU type not supported"); 338f07a9995SKamil Rytarowski } 339f07a9995SKamil Rytarowski } 340f07a9995SKamil Rytarowski 34197206d57SZachary Turner Status 34297206d57SZachary Turner NativeProcessNetBSD::FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread) { 343f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 34497206d57SZachary Turner Status error; 345f07a9995SKamil Rytarowski // Find out the size of a breakpoint (might depend on where we are in the 346f07a9995SKamil Rytarowski // code). 347d37349f3SPavel Labath NativeRegisterContext& context = thread.GetRegisterContext(); 348f07a9995SKamil Rytarowski uint32_t breakpoint_size = 0; 349f07a9995SKamil Rytarowski error = GetSoftwareBreakpointPCOffset(breakpoint_size); 350f07a9995SKamil Rytarowski if (error.Fail()) { 351f07a9995SKamil Rytarowski LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 352f07a9995SKamil Rytarowski return error; 353f07a9995SKamil Rytarowski } else 354f07a9995SKamil Rytarowski LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 35536e23ecaSKamil Rytarowski // First try probing for a breakpoint at a software breakpoint location: PC 35636e23ecaSKamil Rytarowski // - breakpoint size. 357f07a9995SKamil Rytarowski const lldb::addr_t initial_pc_addr = 358d37349f3SPavel Labath context.GetPCfromBreakpointLocation(); 359f07a9995SKamil Rytarowski lldb::addr_t breakpoint_addr = initial_pc_addr; 360f07a9995SKamil Rytarowski if (breakpoint_size > 0) { 361f07a9995SKamil Rytarowski // Do not allow breakpoint probe to wrap around. 362f07a9995SKamil Rytarowski if (breakpoint_addr >= breakpoint_size) 363f07a9995SKamil Rytarowski breakpoint_addr -= breakpoint_size; 364f07a9995SKamil Rytarowski } 365f07a9995SKamil Rytarowski // Check if we stopped because of a breakpoint. 366f07a9995SKamil Rytarowski NativeBreakpointSP breakpoint_sp; 367f07a9995SKamil Rytarowski error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 368f07a9995SKamil Rytarowski if (!error.Success() || !breakpoint_sp) { 369f07a9995SKamil Rytarowski // We didn't find one at a software probe location. Nothing to do. 370f07a9995SKamil Rytarowski LLDB_LOG(log, 371f07a9995SKamil Rytarowski "pid {0} no lldb breakpoint found at current pc with " 372f07a9995SKamil Rytarowski "adjustment: {1}", 373f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 37497206d57SZachary Turner return Status(); 375f07a9995SKamil Rytarowski } 376f07a9995SKamil Rytarowski // If the breakpoint is not a software breakpoint, nothing to do. 377f07a9995SKamil Rytarowski if (!breakpoint_sp->IsSoftwareBreakpoint()) { 378f07a9995SKamil Rytarowski LLDB_LOG( 379f07a9995SKamil Rytarowski log, 380f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 381f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 38297206d57SZachary Turner return Status(); 383f07a9995SKamil Rytarowski } 384f07a9995SKamil Rytarowski // 385f07a9995SKamil Rytarowski // We have a software breakpoint and need to adjust the PC. 386f07a9995SKamil Rytarowski // 387f07a9995SKamil Rytarowski // Sanity check. 388f07a9995SKamil Rytarowski if (breakpoint_size == 0) { 389f07a9995SKamil Rytarowski // Nothing to do! How did we get here? 390f07a9995SKamil Rytarowski LLDB_LOG(log, 391f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, it is software, but the " 392f07a9995SKamil Rytarowski "size is zero, nothing to do (unexpected)", 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 // Change the program counter. 409f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 410f07a9995SKamil Rytarowski thread.GetID(), initial_pc_addr, breakpoint_addr); 411d37349f3SPavel Labath error = context.SetPC(breakpoint_addr); 412f07a9995SKamil Rytarowski if (error.Fail()) { 413f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 414f07a9995SKamil Rytarowski thread.GetID(), error); 415f07a9995SKamil Rytarowski return error; 416f07a9995SKamil Rytarowski } 417f07a9995SKamil Rytarowski return error; 418f07a9995SKamil Rytarowski } 419f07a9995SKamil Rytarowski 42097206d57SZachary Turner Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { 421f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 422f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 423f07a9995SKamil Rytarowski 424a5be48b3SPavel Labath const auto &thread = m_threads[0]; 425f07a9995SKamil Rytarowski const ResumeAction *const action = 426a5be48b3SPavel Labath resume_actions.GetActionForThread(thread->GetID(), true); 427f07a9995SKamil Rytarowski 428f07a9995SKamil Rytarowski if (action == nullptr) { 429f07a9995SKamil Rytarowski LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 430a5be48b3SPavel Labath thread->GetID()); 43197206d57SZachary Turner return Status(); 432f07a9995SKamil Rytarowski } 433f07a9995SKamil Rytarowski 43497206d57SZachary Turner Status error; 4353eef2b5eSKamil Rytarowski 436f07a9995SKamil Rytarowski switch (action->state) { 437f07a9995SKamil Rytarowski case eStateRunning: { 438f07a9995SKamil Rytarowski // Run the thread, possibly feeding it the signal. 4393eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, 4403eef2b5eSKamil Rytarowski action->signal); 441f07a9995SKamil Rytarowski if (!error.Success()) 442f07a9995SKamil Rytarowski return error; 443a5be48b3SPavel Labath for (const auto &thread : m_threads) 444a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetRunning(); 445f07a9995SKamil Rytarowski SetState(eStateRunning, true); 446f07a9995SKamil Rytarowski break; 447f07a9995SKamil Rytarowski } 448f07a9995SKamil Rytarowski case eStateStepping: 4493eef2b5eSKamil Rytarowski // Run the thread, possibly feeding it the signal. 4503eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, 4513eef2b5eSKamil Rytarowski action->signal); 4523eef2b5eSKamil Rytarowski if (!error.Success()) 4533eef2b5eSKamil Rytarowski return error; 454a5be48b3SPavel Labath for (const auto &thread : m_threads) 455a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStepping(); 4563eef2b5eSKamil Rytarowski SetState(eStateStepping, true); 457f07a9995SKamil Rytarowski break; 458f07a9995SKamil Rytarowski 459f07a9995SKamil Rytarowski case eStateSuspended: 460f07a9995SKamil Rytarowski case eStateStopped: 461f07a9995SKamil Rytarowski llvm_unreachable("Unexpected state"); 462f07a9995SKamil Rytarowski 463f07a9995SKamil Rytarowski default: 46497206d57SZachary Turner return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " 465f07a9995SKamil Rytarowski "for pid %" PRIu64 ", tid %" PRIu64, 466f07a9995SKamil Rytarowski __FUNCTION__, StateAsCString(action->state), GetID(), 467a5be48b3SPavel Labath thread->GetID()); 468f07a9995SKamil Rytarowski } 469f07a9995SKamil Rytarowski 47097206d57SZachary Turner return Status(); 471f07a9995SKamil Rytarowski } 472f07a9995SKamil Rytarowski 47397206d57SZachary Turner Status NativeProcessNetBSD::Halt() { 47497206d57SZachary Turner Status error; 475f07a9995SKamil Rytarowski 476f07a9995SKamil Rytarowski if (kill(GetID(), SIGSTOP) != 0) 477f07a9995SKamil Rytarowski error.SetErrorToErrno(); 478f07a9995SKamil Rytarowski 479f07a9995SKamil Rytarowski return error; 480f07a9995SKamil Rytarowski } 481f07a9995SKamil Rytarowski 48297206d57SZachary Turner Status NativeProcessNetBSD::Detach() { 48397206d57SZachary Turner Status error; 484f07a9995SKamil Rytarowski 485f07a9995SKamil Rytarowski // Stop monitoring the inferior. 486f07a9995SKamil Rytarowski m_sigchld_handle.reset(); 487f07a9995SKamil Rytarowski 488f07a9995SKamil Rytarowski // Tell ptrace to detach from the process. 489f07a9995SKamil Rytarowski if (GetID() == LLDB_INVALID_PROCESS_ID) 490f07a9995SKamil Rytarowski return error; 491f07a9995SKamil Rytarowski 492f07a9995SKamil Rytarowski return PtraceWrapper(PT_DETACH, GetID()); 493f07a9995SKamil Rytarowski } 494f07a9995SKamil Rytarowski 49597206d57SZachary Turner Status NativeProcessNetBSD::Signal(int signo) { 49697206d57SZachary Turner Status error; 497f07a9995SKamil Rytarowski 498f07a9995SKamil Rytarowski if (kill(GetID(), signo)) 499f07a9995SKamil Rytarowski error.SetErrorToErrno(); 500f07a9995SKamil Rytarowski 501f07a9995SKamil Rytarowski return error; 502f07a9995SKamil Rytarowski } 503f07a9995SKamil Rytarowski 50497206d57SZachary Turner Status NativeProcessNetBSD::Kill() { 505f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 506f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 507f07a9995SKamil Rytarowski 50897206d57SZachary Turner Status error; 509f07a9995SKamil Rytarowski 510f07a9995SKamil Rytarowski switch (m_state) { 511f07a9995SKamil Rytarowski case StateType::eStateInvalid: 512f07a9995SKamil Rytarowski case StateType::eStateExited: 513f07a9995SKamil Rytarowski case StateType::eStateCrashed: 514f07a9995SKamil Rytarowski case StateType::eStateDetached: 515f07a9995SKamil Rytarowski case StateType::eStateUnloaded: 516f07a9995SKamil Rytarowski // Nothing to do - the process is already dead. 517f07a9995SKamil Rytarowski LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 518f07a9995SKamil Rytarowski StateAsCString(m_state)); 519f07a9995SKamil Rytarowski return error; 520f07a9995SKamil Rytarowski 521f07a9995SKamil Rytarowski case StateType::eStateConnected: 522f07a9995SKamil Rytarowski case StateType::eStateAttaching: 523f07a9995SKamil Rytarowski case StateType::eStateLaunching: 524f07a9995SKamil Rytarowski case StateType::eStateStopped: 525f07a9995SKamil Rytarowski case StateType::eStateRunning: 526f07a9995SKamil Rytarowski case StateType::eStateStepping: 527f07a9995SKamil Rytarowski case StateType::eStateSuspended: 528f07a9995SKamil Rytarowski // We can try to kill a process in these states. 529f07a9995SKamil Rytarowski break; 530f07a9995SKamil Rytarowski } 531f07a9995SKamil Rytarowski 532f07a9995SKamil Rytarowski if (kill(GetID(), SIGKILL) != 0) { 533f07a9995SKamil Rytarowski error.SetErrorToErrno(); 534f07a9995SKamil Rytarowski return error; 535f07a9995SKamil Rytarowski } 536f07a9995SKamil Rytarowski 537f07a9995SKamil Rytarowski return error; 538f07a9995SKamil Rytarowski } 539f07a9995SKamil Rytarowski 54097206d57SZachary Turner Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr, 541f07a9995SKamil Rytarowski MemoryRegionInfo &range_info) { 542f07a9995SKamil Rytarowski 543f07a9995SKamil Rytarowski if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 544f07a9995SKamil Rytarowski // We're done. 54597206d57SZachary Turner return Status("unsupported"); 546f07a9995SKamil Rytarowski } 547f07a9995SKamil Rytarowski 54897206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 549f07a9995SKamil Rytarowski if (error.Fail()) { 550f07a9995SKamil Rytarowski return error; 551f07a9995SKamil Rytarowski } 552f07a9995SKamil Rytarowski 553f07a9995SKamil Rytarowski lldb::addr_t prev_base_address = 0; 554f07a9995SKamil Rytarowski // FIXME start by finding the last region that is <= target address using 555f07a9995SKamil Rytarowski // binary search. Data is sorted. 556f07a9995SKamil Rytarowski // There can be a ton of regions on pthreads apps with lots of threads. 557f07a9995SKamil Rytarowski for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 558f07a9995SKamil Rytarowski ++it) { 559f07a9995SKamil Rytarowski MemoryRegionInfo &proc_entry_info = it->first; 560f07a9995SKamil Rytarowski // Sanity check assumption that memory map entries are ascending. 561f07a9995SKamil Rytarowski assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 562f07a9995SKamil Rytarowski "descending memory map entries detected, unexpected"); 563f07a9995SKamil Rytarowski prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 564f07a9995SKamil Rytarowski UNUSED_IF_ASSERT_DISABLED(prev_base_address); 56536e23ecaSKamil Rytarowski // If the target address comes before this entry, indicate distance to 56636e23ecaSKamil Rytarowski // next region. 567f07a9995SKamil Rytarowski if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 568f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 569f07a9995SKamil Rytarowski range_info.GetRange().SetByteSize( 570f07a9995SKamil Rytarowski proc_entry_info.GetRange().GetRangeBase() - load_addr); 571f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 572f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 573f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 574f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 575f07a9995SKamil Rytarowski return error; 576f07a9995SKamil Rytarowski } else if (proc_entry_info.GetRange().Contains(load_addr)) { 577f07a9995SKamil Rytarowski // The target address is within the memory region we're processing here. 578f07a9995SKamil Rytarowski range_info = proc_entry_info; 579f07a9995SKamil Rytarowski return error; 580f07a9995SKamil Rytarowski } 581f07a9995SKamil Rytarowski // The target memory address comes somewhere after the region we just 582f07a9995SKamil Rytarowski // parsed. 583f07a9995SKamil Rytarowski } 584f07a9995SKamil Rytarowski // If we made it here, we didn't find an entry that contained the given 585f07a9995SKamil Rytarowski // address. Return the 58636e23ecaSKamil Rytarowski // load_addr as start and the amount of bytes betwwen load address and the 58736e23ecaSKamil Rytarowski // end of the memory as size. 588f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 589f07a9995SKamil Rytarowski range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 590f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 591f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 592f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 593f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 594f07a9995SKamil Rytarowski return error; 595f07a9995SKamil Rytarowski } 596f07a9995SKamil Rytarowski 59797206d57SZachary Turner Status NativeProcessNetBSD::PopulateMemoryRegionCache() { 598f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 599f07a9995SKamil Rytarowski // If our cache is empty, pull the latest. There should always be at least 600f07a9995SKamil Rytarowski // one memory region if memory region handling is supported. 601f07a9995SKamil Rytarowski if (!m_mem_region_cache.empty()) { 602f07a9995SKamil Rytarowski LLDB_LOG(log, "reusing {0} cached memory region entries", 603f07a9995SKamil Rytarowski m_mem_region_cache.size()); 60497206d57SZachary Turner return Status(); 605f07a9995SKamil Rytarowski } 606f07a9995SKamil Rytarowski 607f07a9995SKamil Rytarowski struct kinfo_vmentry *vm; 608f07a9995SKamil Rytarowski size_t count, i; 609f07a9995SKamil Rytarowski vm = kinfo_getvmmap(GetID(), &count); 610f07a9995SKamil Rytarowski if (vm == NULL) { 611f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 61297206d57SZachary Turner Status error; 613f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 614f07a9995SKamil Rytarowski return error; 615f07a9995SKamil Rytarowski } 616f07a9995SKamil Rytarowski for (i = 0; i < count; i++) { 617f07a9995SKamil Rytarowski MemoryRegionInfo info; 618f07a9995SKamil Rytarowski info.Clear(); 619f07a9995SKamil Rytarowski info.GetRange().SetRangeBase(vm[i].kve_start); 620f07a9995SKamil Rytarowski info.GetRange().SetRangeEnd(vm[i].kve_end); 621f07a9995SKamil Rytarowski info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 622f07a9995SKamil Rytarowski 623f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_READ) 624f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 625f07a9995SKamil Rytarowski else 626f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 627f07a9995SKamil Rytarowski 628f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_WRITE) 629f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 630f07a9995SKamil Rytarowski else 631f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 632f07a9995SKamil Rytarowski 633f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_EXECUTE) 634f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 635f07a9995SKamil Rytarowski else 636f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 637f07a9995SKamil Rytarowski 638f07a9995SKamil Rytarowski if (vm[i].kve_path[0]) 639f07a9995SKamil Rytarowski info.SetName(vm[i].kve_path); 640f07a9995SKamil Rytarowski 641f07a9995SKamil Rytarowski m_mem_region_cache.emplace_back( 642f07a9995SKamil Rytarowski info, FileSpec(info.GetName().GetCString(), true)); 643f07a9995SKamil Rytarowski } 644f07a9995SKamil Rytarowski free(vm); 645f07a9995SKamil Rytarowski 646f07a9995SKamil Rytarowski if (m_mem_region_cache.empty()) { 647f07a9995SKamil Rytarowski // No entries after attempting to read them. This shouldn't happen. 648f07a9995SKamil Rytarowski // Assume we don't support map entries. 649f07a9995SKamil Rytarowski LLDB_LOG(log, "failed to find any vmmap entries, assuming no support " 650f07a9995SKamil Rytarowski "for memory region metadata retrieval"); 651f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 65297206d57SZachary Turner Status error; 653f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 654f07a9995SKamil Rytarowski return error; 655f07a9995SKamil Rytarowski } 656f07a9995SKamil Rytarowski LLDB_LOG(log, "read {0} memory region entries from process {1}", 657f07a9995SKamil Rytarowski m_mem_region_cache.size(), GetID()); 658f07a9995SKamil Rytarowski // We support memory retrieval, remember that. 659f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolYes; 66097206d57SZachary Turner return Status(); 661f07a9995SKamil Rytarowski } 662f07a9995SKamil Rytarowski 66397206d57SZachary Turner Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions, 664f07a9995SKamil Rytarowski lldb::addr_t &addr) { 66597206d57SZachary Turner return Status("Unimplemented"); 666f07a9995SKamil Rytarowski } 667f07a9995SKamil Rytarowski 66897206d57SZachary Turner Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) { 66997206d57SZachary Turner return Status("Unimplemented"); 670f07a9995SKamil Rytarowski } 671f07a9995SKamil Rytarowski 672f07a9995SKamil Rytarowski lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() { 673f07a9995SKamil Rytarowski // punt on this for now 674f07a9995SKamil Rytarowski return LLDB_INVALID_ADDRESS; 675f07a9995SKamil Rytarowski } 676f07a9995SKamil Rytarowski 677f07a9995SKamil Rytarowski size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); } 678f07a9995SKamil Rytarowski 67997206d57SZachary Turner Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size, 680f07a9995SKamil Rytarowski bool hardware) { 681f07a9995SKamil Rytarowski if (hardware) 68297206d57SZachary Turner return Status("NativeProcessNetBSD does not support hardware breakpoints"); 683f07a9995SKamil Rytarowski else 684f07a9995SKamil Rytarowski return SetSoftwareBreakpoint(addr, size); 685f07a9995SKamil Rytarowski } 686f07a9995SKamil Rytarowski 68797206d57SZachary Turner Status NativeProcessNetBSD::GetSoftwareBreakpointTrapOpcode( 688f07a9995SKamil Rytarowski size_t trap_opcode_size_hint, size_t &actual_opcode_size, 689f07a9995SKamil Rytarowski const uint8_t *&trap_opcode_bytes) { 690f07a9995SKamil Rytarowski static const uint8_t g_i386_opcode[] = {0xCC}; 691f07a9995SKamil Rytarowski 692f07a9995SKamil Rytarowski switch (m_arch.GetMachine()) { 693f07a9995SKamil Rytarowski case llvm::Triple::x86: 694f07a9995SKamil Rytarowski case llvm::Triple::x86_64: 695f07a9995SKamil Rytarowski trap_opcode_bytes = g_i386_opcode; 696f07a9995SKamil Rytarowski actual_opcode_size = sizeof(g_i386_opcode); 69797206d57SZachary Turner return Status(); 698f07a9995SKamil Rytarowski default: 699f07a9995SKamil Rytarowski assert(false && "CPU type not supported!"); 70097206d57SZachary Turner return Status("CPU type not supported"); 701f07a9995SKamil Rytarowski } 702f07a9995SKamil Rytarowski } 703f07a9995SKamil Rytarowski 70497206d57SZachary Turner Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path, 705f07a9995SKamil Rytarowski FileSpec &file_spec) { 70697206d57SZachary Turner return Status("Unimplemented"); 707f07a9995SKamil Rytarowski } 708f07a9995SKamil Rytarowski 70997206d57SZachary Turner Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name, 710f07a9995SKamil Rytarowski lldb::addr_t &load_addr) { 711f07a9995SKamil Rytarowski load_addr = LLDB_INVALID_ADDRESS; 71297206d57SZachary Turner return Status(); 713f07a9995SKamil Rytarowski } 714f07a9995SKamil Rytarowski 715f07a9995SKamil Rytarowski void NativeProcessNetBSD::SigchldHandler() { 716f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 717f07a9995SKamil Rytarowski // Process all pending waitpid notifications. 718f07a9995SKamil Rytarowski int status; 719c1a6b128SPavel Labath ::pid_t wait_pid = 720c1a6b128SPavel Labath llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG); 721f07a9995SKamil Rytarowski 722f07a9995SKamil Rytarowski if (wait_pid == 0) 723f07a9995SKamil Rytarowski return; // We are done. 724f07a9995SKamil Rytarowski 725f07a9995SKamil Rytarowski if (wait_pid == -1) { 72697206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 727f07a9995SKamil Rytarowski LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error); 728f07a9995SKamil Rytarowski } 729f07a9995SKamil Rytarowski 7303508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 7313508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 7323508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 7333508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 734f07a9995SKamil Rytarowski 735f07a9995SKamil Rytarowski LLDB_LOG(log, 7363508fc8cSPavel Labath "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}", 7373508fc8cSPavel Labath GetID(), wait_pid, status, exited); 738f07a9995SKamil Rytarowski 739f07a9995SKamil Rytarowski if (exited) 7403508fc8cSPavel Labath MonitorExited(wait_pid, wait_status); 7413508fc8cSPavel Labath else { 7424bb74415SKamil Rytarowski assert(wait_status.type == WaitStatus::Stop); 7433508fc8cSPavel Labath MonitorCallback(wait_pid, wait_status.status); 7443508fc8cSPavel Labath } 745f07a9995SKamil Rytarowski } 746f07a9995SKamil Rytarowski 747269eec03SKamil Rytarowski bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { 748a5be48b3SPavel Labath for (const auto &thread : m_threads) { 749a5be48b3SPavel Labath assert(thread && "thread list should not contain NULL threads"); 750a5be48b3SPavel Labath if (thread->GetID() == thread_id) { 751269eec03SKamil Rytarowski // We have this thread. 752269eec03SKamil Rytarowski return true; 753269eec03SKamil Rytarowski } 754269eec03SKamil Rytarowski } 755269eec03SKamil Rytarowski 756269eec03SKamil Rytarowski // We don't have this thread. 757269eec03SKamil Rytarowski return false; 758269eec03SKamil Rytarowski } 759269eec03SKamil Rytarowski 760a5be48b3SPavel Labath NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { 761f07a9995SKamil Rytarowski 762f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 763f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 764f07a9995SKamil Rytarowski 765f07a9995SKamil Rytarowski assert(!HasThreadNoLock(thread_id) && 766f07a9995SKamil Rytarowski "attempted to add a thread by id that already exists"); 767f07a9995SKamil Rytarowski 768f07a9995SKamil Rytarowski // If this is the first thread, save it as the current thread 769f07a9995SKamil Rytarowski if (m_threads.empty()) 770f07a9995SKamil Rytarowski SetCurrentThreadID(thread_id); 771f07a9995SKamil Rytarowski 772a5be48b3SPavel Labath m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id)); 773a5be48b3SPavel Labath return static_cast<NativeThreadNetBSD &>(*m_threads.back()); 774f07a9995SKamil Rytarowski } 775f07a9995SKamil Rytarowski 77696e600fcSPavel Labath Status NativeProcessNetBSD::Attach() { 777f07a9995SKamil Rytarowski // Attach to the requested process. 778f07a9995SKamil Rytarowski // An attach will cause the thread to stop with a SIGSTOP. 77996e600fcSPavel Labath Status status = PtraceWrapper(PT_ATTACH, m_pid); 78096e600fcSPavel Labath if (status.Fail()) 78196e600fcSPavel Labath return status; 782f07a9995SKamil Rytarowski 78396e600fcSPavel Labath int wstatus; 784f07a9995SKamil Rytarowski // Need to use WALLSIG otherwise we receive an error with errno=ECHLD 785f07a9995SKamil Rytarowski // At this point we should have a thread stopped if waitpid succeeds. 78696e600fcSPavel Labath if ((wstatus = waitpid(m_pid, NULL, WALLSIG)) < 0) 78796e600fcSPavel Labath return Status(errno, eErrorTypePOSIX); 788f07a9995SKamil Rytarowski 789f07a9995SKamil Rytarowski /* Initialize threads */ 79096e600fcSPavel Labath status = ReinitializeThreads(); 79196e600fcSPavel Labath if (status.Fail()) 79296e600fcSPavel Labath return status; 793f07a9995SKamil Rytarowski 794a5be48b3SPavel Labath for (const auto &thread : m_threads) 795a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); 79636e23ecaSKamil Rytarowski 797f07a9995SKamil Rytarowski // Let our process instance know the thread has stopped. 798f07a9995SKamil Rytarowski SetState(StateType::eStateStopped); 79996e600fcSPavel Labath return Status(); 800f07a9995SKamil Rytarowski } 801f07a9995SKamil Rytarowski 80297206d57SZachary Turner Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf, 80397206d57SZachary Turner size_t size, size_t &bytes_read) { 804f07a9995SKamil Rytarowski unsigned char *dst = static_cast<unsigned char *>(buf); 805f07a9995SKamil Rytarowski struct ptrace_io_desc io; 806f07a9995SKamil Rytarowski 807f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 808f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 809f07a9995SKamil Rytarowski 810f07a9995SKamil Rytarowski bytes_read = 0; 811f07a9995SKamil Rytarowski io.piod_op = PIOD_READ_D; 812f07a9995SKamil Rytarowski io.piod_len = size; 813f07a9995SKamil Rytarowski 814f07a9995SKamil Rytarowski do { 815f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_read); 816f07a9995SKamil Rytarowski io.piod_addr = dst + bytes_read; 817f07a9995SKamil Rytarowski 81897206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 819f07a9995SKamil Rytarowski if (error.Fail()) 820f07a9995SKamil Rytarowski return error; 821f07a9995SKamil Rytarowski 822f07a9995SKamil Rytarowski bytes_read = io.piod_len; 823f07a9995SKamil Rytarowski io.piod_len = size - bytes_read; 824f07a9995SKamil Rytarowski } while (bytes_read < size); 825f07a9995SKamil Rytarowski 82697206d57SZachary Turner return Status(); 827f07a9995SKamil Rytarowski } 828f07a9995SKamil Rytarowski 82997206d57SZachary Turner Status NativeProcessNetBSD::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 830f07a9995SKamil Rytarowski size_t size, 831f07a9995SKamil Rytarowski size_t &bytes_read) { 83297206d57SZachary Turner Status error = ReadMemory(addr, buf, size, bytes_read); 833f07a9995SKamil Rytarowski if (error.Fail()) 834f07a9995SKamil Rytarowski return error; 835f07a9995SKamil Rytarowski return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 836f07a9995SKamil Rytarowski } 837f07a9995SKamil Rytarowski 83897206d57SZachary Turner Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf, 839f07a9995SKamil Rytarowski size_t size, size_t &bytes_written) { 840f07a9995SKamil Rytarowski const unsigned char *src = static_cast<const unsigned char *>(buf); 84197206d57SZachary Turner Status error; 842f07a9995SKamil Rytarowski struct ptrace_io_desc io; 843f07a9995SKamil Rytarowski 844f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 845f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 846f07a9995SKamil Rytarowski 847f07a9995SKamil Rytarowski bytes_written = 0; 848f07a9995SKamil Rytarowski io.piod_op = PIOD_WRITE_D; 849f07a9995SKamil Rytarowski io.piod_len = size; 850f07a9995SKamil Rytarowski 851f07a9995SKamil Rytarowski do { 852269eec03SKamil Rytarowski io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written)); 853f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_written); 854f07a9995SKamil Rytarowski 85597206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 856f07a9995SKamil Rytarowski if (error.Fail()) 857f07a9995SKamil Rytarowski return error; 858f07a9995SKamil Rytarowski 859f07a9995SKamil Rytarowski bytes_written = io.piod_len; 860f07a9995SKamil Rytarowski io.piod_len = size - bytes_written; 861f07a9995SKamil Rytarowski } while (bytes_written < size); 862f07a9995SKamil Rytarowski 863f07a9995SKamil Rytarowski return error; 864f07a9995SKamil Rytarowski } 865f07a9995SKamil Rytarowski 866f07a9995SKamil Rytarowski llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 867f07a9995SKamil Rytarowski NativeProcessNetBSD::GetAuxvData() const { 868f07a9995SKamil Rytarowski /* 869f07a9995SKamil Rytarowski * ELF_AUX_ENTRIES is currently restricted to kernel 870f07a9995SKamil Rytarowski * (<sys/exec_elf.h> r. 1.155 specifies 15) 871f07a9995SKamil Rytarowski * 872f07a9995SKamil Rytarowski * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this 873f07a9995SKamil Rytarowski * information isn't needed. 874f07a9995SKamil Rytarowski */ 875f07a9995SKamil Rytarowski size_t auxv_size = 100 * sizeof(AuxInfo); 876f07a9995SKamil Rytarowski 877e831bb3cSPavel Labath ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf = 878dbda2851SPavel Labath llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size); 879f07a9995SKamil Rytarowski 880269eec03SKamil Rytarowski struct ptrace_io_desc io; 881269eec03SKamil Rytarowski io.piod_op = PIOD_READ_AUXV; 882269eec03SKamil Rytarowski io.piod_offs = 0; 883e831bb3cSPavel Labath io.piod_addr = static_cast<void *>(buf.get()->getBufferStart()); 884269eec03SKamil Rytarowski io.piod_len = auxv_size; 885f07a9995SKamil Rytarowski 88697206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 887f07a9995SKamil Rytarowski 888f07a9995SKamil Rytarowski if (error.Fail()) 889f07a9995SKamil Rytarowski return std::error_code(error.GetError(), std::generic_category()); 890f07a9995SKamil Rytarowski 891f07a9995SKamil Rytarowski if (io.piod_len < 1) 892f07a9995SKamil Rytarowski return std::error_code(ECANCELED, std::generic_category()); 893f07a9995SKamil Rytarowski 894e831bb3cSPavel Labath return std::move(buf); 895f07a9995SKamil Rytarowski } 8963eef2b5eSKamil Rytarowski 89797206d57SZachary Turner Status NativeProcessNetBSD::ReinitializeThreads() { 8983eef2b5eSKamil Rytarowski // Clear old threads 8993eef2b5eSKamil Rytarowski m_threads.clear(); 9003eef2b5eSKamil Rytarowski 9013eef2b5eSKamil Rytarowski // Initialize new thread 9023eef2b5eSKamil Rytarowski struct ptrace_lwpinfo info = {}; 90397206d57SZachary Turner Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 9043eef2b5eSKamil Rytarowski if (error.Fail()) { 9053eef2b5eSKamil Rytarowski return error; 9063eef2b5eSKamil Rytarowski } 9073eef2b5eSKamil Rytarowski // Reinitialize from scratch threads and register them in process 9083eef2b5eSKamil Rytarowski while (info.pl_lwpid != 0) { 909a5be48b3SPavel Labath AddThread(info.pl_lwpid); 9103eef2b5eSKamil Rytarowski error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 9113eef2b5eSKamil Rytarowski if (error.Fail()) { 9123eef2b5eSKamil Rytarowski return error; 9133eef2b5eSKamil Rytarowski } 9143eef2b5eSKamil Rytarowski } 9153eef2b5eSKamil Rytarowski 9163eef2b5eSKamil Rytarowski return error; 9173eef2b5eSKamil Rytarowski } 918