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 27*05097246SAdrian Prantl // they define some macros which collide with variable names in other modules 28f07a9995SKamil Rytarowski // clang-format off 29f07a9995SKamil Rytarowski #include <sys/types.h> 30f07a9995SKamil Rytarowski #include <sys/ptrace.h> 31f07a9995SKamil Rytarowski #include <sys/sysctl.h> 32f07a9995SKamil Rytarowski #include <sys/wait.h> 33f07a9995SKamil Rytarowski #include <uvm/uvm_prot.h> 34f07a9995SKamil Rytarowski #include <elf.h> 35f07a9995SKamil Rytarowski #include <util.h> 36f07a9995SKamil Rytarowski // clang-format on 371a3d19ddSKamil Rytarowski 381a3d19ddSKamil Rytarowski using namespace lldb; 391a3d19ddSKamil Rytarowski using namespace lldb_private; 401a3d19ddSKamil Rytarowski using namespace lldb_private::process_netbsd; 411a3d19ddSKamil Rytarowski using namespace llvm; 421a3d19ddSKamil Rytarowski 43f07a9995SKamil Rytarowski // Simple helper function to ensure flags are enabled on the given file 44f07a9995SKamil Rytarowski // descriptor. 4597206d57SZachary Turner static Status EnsureFDFlags(int fd, int flags) { 4697206d57SZachary Turner Status error; 47f07a9995SKamil Rytarowski 48f07a9995SKamil Rytarowski int status = fcntl(fd, F_GETFL); 49f07a9995SKamil Rytarowski if (status == -1) { 50f07a9995SKamil Rytarowski error.SetErrorToErrno(); 51f07a9995SKamil Rytarowski return error; 52f07a9995SKamil Rytarowski } 53f07a9995SKamil Rytarowski 54f07a9995SKamil Rytarowski if (fcntl(fd, F_SETFL, status | flags) == -1) { 55f07a9995SKamil Rytarowski error.SetErrorToErrno(); 56f07a9995SKamil Rytarowski return error; 57f07a9995SKamil Rytarowski } 58f07a9995SKamil Rytarowski 59f07a9995SKamil Rytarowski return error; 60f07a9995SKamil Rytarowski } 61f07a9995SKamil Rytarowski 621a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 631a3d19ddSKamil Rytarowski // Public Static Methods 641a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 651a3d19ddSKamil Rytarowski 6682abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 6796e600fcSPavel Labath NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info, 6896e600fcSPavel Labath NativeDelegate &native_delegate, 6996e600fcSPavel Labath MainLoop &mainloop) const { 70f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 71f07a9995SKamil Rytarowski 7296e600fcSPavel Labath Status status; 7396e600fcSPavel Labath ::pid_t pid = ProcessLauncherPosixFork() 7496e600fcSPavel Labath .LaunchProcess(launch_info, status) 7596e600fcSPavel Labath .GetProcessId(); 7696e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 7796e600fcSPavel Labath if (status.Fail()) { 7896e600fcSPavel Labath LLDB_LOG(log, "failed to launch process: {0}", status); 7996e600fcSPavel Labath return status.ToError(); 80f07a9995SKamil Rytarowski } 81f07a9995SKamil Rytarowski 8296e600fcSPavel Labath // Wait for the child process to trap on its call to execve. 8396e600fcSPavel Labath int wstatus; 8496e600fcSPavel Labath ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); 8596e600fcSPavel Labath assert(wpid == pid); 8696e600fcSPavel Labath (void)wpid; 8796e600fcSPavel Labath if (!WIFSTOPPED(wstatus)) { 8896e600fcSPavel Labath LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}", 8996e600fcSPavel Labath WaitStatus::Decode(wstatus)); 9096e600fcSPavel Labath return llvm::make_error<StringError>("Could not sync with inferior process", 9196e600fcSPavel Labath llvm::inconvertibleErrorCode()); 9296e600fcSPavel Labath } 9396e600fcSPavel Labath LLDB_LOG(log, "inferior started, now in stopped state"); 94f07a9995SKamil Rytarowski 9536e82208SPavel Labath ProcessInstanceInfo Info; 9636e82208SPavel Labath if (!Host::GetProcessInfo(pid, Info)) { 9736e82208SPavel Labath return llvm::make_error<StringError>("Cannot get process architecture", 9836e82208SPavel Labath llvm::inconvertibleErrorCode()); 9936e82208SPavel Labath } 10096e600fcSPavel Labath 10196e600fcSPavel Labath // Set the architecture to the exe architecture. 10296e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 10336e82208SPavel Labath Info.GetArchitecture().GetArchitectureName()); 10496e600fcSPavel Labath 10582abefa4SPavel Labath std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD( 10696e600fcSPavel Labath pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, 10736e82208SPavel Labath Info.GetArchitecture(), mainloop)); 10896e600fcSPavel Labath 10982abefa4SPavel Labath status = process_up->ReinitializeThreads(); 11096e600fcSPavel Labath if (status.Fail()) 11196e600fcSPavel Labath return status.ToError(); 11296e600fcSPavel Labath 113a5be48b3SPavel Labath for (const auto &thread : process_up->m_threads) 114a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); 1158a4bf06bSKamil Rytarowski process_up->SetState(StateType::eStateStopped, false); 11696e600fcSPavel Labath 11782abefa4SPavel Labath return std::move(process_up); 118f07a9995SKamil Rytarowski } 119f07a9995SKamil Rytarowski 12082abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 12182abefa4SPavel Labath NativeProcessNetBSD::Factory::Attach( 1221a3d19ddSKamil Rytarowski lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 12396e600fcSPavel Labath MainLoop &mainloop) const { 124f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 125f07a9995SKamil Rytarowski LLDB_LOG(log, "pid = {0:x}", pid); 126f07a9995SKamil Rytarowski 127f07a9995SKamil Rytarowski // Retrieve the architecture for the running process. 12836e82208SPavel Labath ProcessInstanceInfo Info; 12936e82208SPavel Labath if (!Host::GetProcessInfo(pid, Info)) { 13036e82208SPavel Labath return llvm::make_error<StringError>("Cannot get process architecture", 13136e82208SPavel Labath llvm::inconvertibleErrorCode()); 13236e82208SPavel Labath } 133f07a9995SKamil Rytarowski 13436e82208SPavel Labath std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD( 13536e82208SPavel Labath pid, -1, native_delegate, Info.GetArchitecture(), mainloop)); 136f07a9995SKamil Rytarowski 13702d4e50eSPavel Labath Status status = process_up->Attach(); 13896e600fcSPavel Labath if (!status.Success()) 13996e600fcSPavel Labath return status.ToError(); 140f07a9995SKamil Rytarowski 14182abefa4SPavel 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()) { 201a5be48b3SPavel Labath /* Stop Tracking all Threads attached to Process */ 202a5be48b3SPavel Labath for (const auto &thread : m_threads) { 203a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).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: 224a5be48b3SPavel Labath for (const auto &thread : m_threads) { 225a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); 226a5be48b3SPavel Labath FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread)); 227f07a9995SKamil Rytarowski } 228f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 229f07a9995SKamil Rytarowski break; 2303eef2b5eSKamil Rytarowski case TRAP_TRACE: 231a5be48b3SPavel Labath for (const auto &thread : m_threads) 232a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace(); 2333eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2343eef2b5eSKamil Rytarowski break; 2353eef2b5eSKamil Rytarowski case TRAP_EXEC: { 23697206d57SZachary Turner Status error = ReinitializeThreads(); 2373eef2b5eSKamil Rytarowski if (error.Fail()) { 2383eef2b5eSKamil Rytarowski SetState(StateType::eStateInvalid); 2393eef2b5eSKamil Rytarowski return; 2403eef2b5eSKamil Rytarowski } 2413eef2b5eSKamil Rytarowski 2423eef2b5eSKamil Rytarowski // Let our delegate know we have just exec'd. 2433eef2b5eSKamil Rytarowski NotifyDidExec(); 2443eef2b5eSKamil Rytarowski 245a5be48b3SPavel Labath for (const auto &thread : m_threads) 246a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec(); 2473eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2483eef2b5eSKamil Rytarowski } break; 24936e23ecaSKamil Rytarowski case TRAP_DBREG: { 25036e23ecaSKamil Rytarowski // If a watchpoint was hit, report it 25136e23ecaSKamil Rytarowski uint32_t wp_index; 252a5be48b3SPavel Labath Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid]) 253a5be48b3SPavel Labath .GetRegisterContext() 254d37349f3SPavel Labath .GetWatchpointHitIndex( 255a5be48b3SPavel Labath wp_index, (uintptr_t)info.psi_siginfo.si_addr); 25636e23ecaSKamil Rytarowski if (error.Fail()) 25736e23ecaSKamil Rytarowski LLDB_LOG(log, 25836e23ecaSKamil Rytarowski "received error while checking for watchpoint hits, pid = " 25936e23ecaSKamil Rytarowski "{0}, LWP = {1}, error = {2}", 26036e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 26136e23ecaSKamil Rytarowski if (wp_index != LLDB_INVALID_INDEX32) { 262a5be48b3SPavel Labath for (const auto &thread : m_threads) 263a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint( 264a5be48b3SPavel Labath wp_index); 26536e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 26636e23ecaSKamil Rytarowski break; 26736e23ecaSKamil Rytarowski } 26836e23ecaSKamil Rytarowski 26936e23ecaSKamil Rytarowski // If a breakpoint was hit, report it 27036e23ecaSKamil Rytarowski uint32_t bp_index; 271a5be48b3SPavel Labath error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid]) 272a5be48b3SPavel Labath .GetRegisterContext() 273d37349f3SPavel Labath .GetHardwareBreakHitIndex(bp_index, 27436e23ecaSKamil Rytarowski (uintptr_t)info.psi_siginfo.si_addr); 27536e23ecaSKamil Rytarowski if (error.Fail()) 27636e23ecaSKamil Rytarowski LLDB_LOG(log, 27736e23ecaSKamil Rytarowski "received error while checking for hardware " 27836e23ecaSKamil Rytarowski "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", 27936e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 28036e23ecaSKamil Rytarowski if (bp_index != LLDB_INVALID_INDEX32) { 281a5be48b3SPavel Labath for (const auto &thread : m_threads) 282a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); 28336e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 28436e23ecaSKamil Rytarowski break; 28536e23ecaSKamil Rytarowski } 28636e23ecaSKamil Rytarowski } break; 287f07a9995SKamil Rytarowski } 288f07a9995SKamil Rytarowski } 289f07a9995SKamil Rytarowski 290f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { 291f07a9995SKamil Rytarowski ptrace_siginfo_t info; 292f07a9995SKamil Rytarowski const auto siginfo_err = 293f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 294f07a9995SKamil Rytarowski 295a5be48b3SPavel Labath for (const auto &thread : m_threads) { 296a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( 297f07a9995SKamil Rytarowski info.psi_siginfo.si_signo, &info.psi_siginfo); 298f07a9995SKamil Rytarowski } 299f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 300f07a9995SKamil Rytarowski } 301f07a9995SKamil Rytarowski 30297206d57SZachary Turner Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 303f07a9995SKamil Rytarowski int data, int *result) { 304f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 30597206d57SZachary Turner Status error; 306f07a9995SKamil Rytarowski int ret; 307f07a9995SKamil Rytarowski 308f07a9995SKamil Rytarowski errno = 0; 309f07a9995SKamil Rytarowski ret = ptrace(req, static_cast<::pid_t>(pid), addr, data); 310f07a9995SKamil Rytarowski 311f07a9995SKamil Rytarowski if (ret == -1) 312f07a9995SKamil Rytarowski error.SetErrorToErrno(); 313f07a9995SKamil Rytarowski 314f07a9995SKamil Rytarowski if (result) 315f07a9995SKamil Rytarowski *result = ret; 316f07a9995SKamil Rytarowski 317f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret); 318f07a9995SKamil Rytarowski 319f07a9995SKamil Rytarowski if (error.Fail()) 320f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace() failed: {0}", error); 321f07a9995SKamil Rytarowski 322f07a9995SKamil Rytarowski return error; 323f07a9995SKamil Rytarowski } 324f07a9995SKamil Rytarowski 32597206d57SZachary Turner Status NativeProcessNetBSD::GetSoftwareBreakpointPCOffset( 326f07a9995SKamil Rytarowski uint32_t &actual_opcode_size) { 327f07a9995SKamil Rytarowski // FIXME put this behind a breakpoint protocol class that can be 328f07a9995SKamil Rytarowski // set per architecture. Need ARM, MIPS support here. 329f07a9995SKamil Rytarowski static const uint8_t g_i386_opcode[] = {0xCC}; 330f07a9995SKamil Rytarowski switch (m_arch.GetMachine()) { 331f07a9995SKamil Rytarowski case llvm::Triple::x86_64: 332f07a9995SKamil Rytarowski actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 33397206d57SZachary Turner return Status(); 334f07a9995SKamil Rytarowski default: 335f07a9995SKamil Rytarowski assert(false && "CPU type not supported!"); 33697206d57SZachary Turner return Status("CPU type not supported"); 337f07a9995SKamil Rytarowski } 338f07a9995SKamil Rytarowski } 339f07a9995SKamil Rytarowski 34097206d57SZachary Turner Status 34197206d57SZachary Turner NativeProcessNetBSD::FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread) { 342f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 34397206d57SZachary Turner Status error; 344f07a9995SKamil Rytarowski // Find out the size of a breakpoint (might depend on where we are in the 345f07a9995SKamil Rytarowski // code). 346d37349f3SPavel Labath NativeRegisterContext& context = thread.GetRegisterContext(); 347f07a9995SKamil Rytarowski uint32_t breakpoint_size = 0; 348f07a9995SKamil Rytarowski error = GetSoftwareBreakpointPCOffset(breakpoint_size); 349f07a9995SKamil Rytarowski if (error.Fail()) { 350f07a9995SKamil Rytarowski LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 351f07a9995SKamil Rytarowski return error; 352f07a9995SKamil Rytarowski } else 353f07a9995SKamil Rytarowski LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 354*05097246SAdrian Prantl // First try probing for a breakpoint at a software breakpoint location: PC - 355*05097246SAdrian Prantl // breakpoint size. 356f07a9995SKamil Rytarowski const lldb::addr_t initial_pc_addr = 357d37349f3SPavel Labath context.GetPCfromBreakpointLocation(); 358f07a9995SKamil Rytarowski lldb::addr_t breakpoint_addr = initial_pc_addr; 359f07a9995SKamil Rytarowski if (breakpoint_size > 0) { 360f07a9995SKamil Rytarowski // Do not allow breakpoint probe to wrap around. 361f07a9995SKamil Rytarowski if (breakpoint_addr >= breakpoint_size) 362f07a9995SKamil Rytarowski breakpoint_addr -= breakpoint_size; 363f07a9995SKamil Rytarowski } 364f07a9995SKamil Rytarowski // Check if we stopped because of a breakpoint. 365f07a9995SKamil Rytarowski NativeBreakpointSP breakpoint_sp; 366f07a9995SKamil Rytarowski error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 367f07a9995SKamil Rytarowski if (!error.Success() || !breakpoint_sp) { 368f07a9995SKamil Rytarowski // We didn't find one at a software probe location. Nothing to do. 369f07a9995SKamil Rytarowski LLDB_LOG(log, 370f07a9995SKamil Rytarowski "pid {0} no lldb breakpoint found at current pc with " 371f07a9995SKamil Rytarowski "adjustment: {1}", 372f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 37397206d57SZachary Turner return Status(); 374f07a9995SKamil Rytarowski } 375f07a9995SKamil Rytarowski // If the breakpoint is not a software breakpoint, nothing to do. 376f07a9995SKamil Rytarowski if (!breakpoint_sp->IsSoftwareBreakpoint()) { 377f07a9995SKamil Rytarowski LLDB_LOG( 378f07a9995SKamil Rytarowski log, 379f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 380f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 38197206d57SZachary Turner return Status(); 382f07a9995SKamil Rytarowski } 383f07a9995SKamil Rytarowski // 384f07a9995SKamil Rytarowski // We have a software breakpoint and need to adjust the PC. 385f07a9995SKamil Rytarowski // 386f07a9995SKamil Rytarowski // Sanity check. 387f07a9995SKamil Rytarowski if (breakpoint_size == 0) { 388f07a9995SKamil Rytarowski // Nothing to do! How did we get here? 389f07a9995SKamil Rytarowski LLDB_LOG(log, 390f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, it is software, but the " 391f07a9995SKamil Rytarowski "size is zero, nothing to do (unexpected)", 392f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 39397206d57SZachary Turner return Status(); 394f07a9995SKamil Rytarowski } 395f07a9995SKamil Rytarowski // 396f07a9995SKamil Rytarowski // We have a software breakpoint and need to adjust the PC. 397f07a9995SKamil Rytarowski // 398f07a9995SKamil Rytarowski // Sanity check. 399f07a9995SKamil Rytarowski if (breakpoint_size == 0) { 400f07a9995SKamil Rytarowski // Nothing to do! How did we get here? 401f07a9995SKamil Rytarowski LLDB_LOG(log, 402f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, it is software, but the " 403f07a9995SKamil Rytarowski "size is zero, nothing to do (unexpected)", 404f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 40597206d57SZachary Turner return Status(); 406f07a9995SKamil Rytarowski } 407f07a9995SKamil Rytarowski // Change the program counter. 408f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 409f07a9995SKamil Rytarowski thread.GetID(), initial_pc_addr, breakpoint_addr); 410d37349f3SPavel Labath error = context.SetPC(breakpoint_addr); 411f07a9995SKamil Rytarowski if (error.Fail()) { 412f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 413f07a9995SKamil Rytarowski thread.GetID(), error); 414f07a9995SKamil Rytarowski return error; 415f07a9995SKamil Rytarowski } 416f07a9995SKamil Rytarowski return error; 417f07a9995SKamil Rytarowski } 418f07a9995SKamil Rytarowski 41997206d57SZachary Turner Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { 420f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 421f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 422f07a9995SKamil Rytarowski 423a5be48b3SPavel Labath const auto &thread = m_threads[0]; 424f07a9995SKamil Rytarowski const ResumeAction *const action = 425a5be48b3SPavel Labath resume_actions.GetActionForThread(thread->GetID(), true); 426f07a9995SKamil Rytarowski 427f07a9995SKamil Rytarowski if (action == nullptr) { 428f07a9995SKamil Rytarowski LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 429a5be48b3SPavel Labath thread->GetID()); 43097206d57SZachary Turner return Status(); 431f07a9995SKamil Rytarowski } 432f07a9995SKamil Rytarowski 43397206d57SZachary Turner Status error; 4343eef2b5eSKamil Rytarowski 435f07a9995SKamil Rytarowski switch (action->state) { 436f07a9995SKamil Rytarowski case eStateRunning: { 437f07a9995SKamil Rytarowski // Run the thread, possibly feeding it the signal. 4383eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, 4393eef2b5eSKamil Rytarowski action->signal); 440f07a9995SKamil Rytarowski if (!error.Success()) 441f07a9995SKamil Rytarowski return error; 442a5be48b3SPavel Labath for (const auto &thread : m_threads) 443a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetRunning(); 444f07a9995SKamil Rytarowski SetState(eStateRunning, true); 445f07a9995SKamil Rytarowski break; 446f07a9995SKamil Rytarowski } 447f07a9995SKamil Rytarowski case eStateStepping: 4483eef2b5eSKamil Rytarowski // Run the thread, possibly feeding it the signal. 4493eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, 4503eef2b5eSKamil Rytarowski action->signal); 4513eef2b5eSKamil Rytarowski if (!error.Success()) 4523eef2b5eSKamil Rytarowski return error; 453a5be48b3SPavel Labath for (const auto &thread : m_threads) 454a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStepping(); 4553eef2b5eSKamil Rytarowski SetState(eStateStepping, true); 456f07a9995SKamil Rytarowski break; 457f07a9995SKamil Rytarowski 458f07a9995SKamil Rytarowski case eStateSuspended: 459f07a9995SKamil Rytarowski case eStateStopped: 460f07a9995SKamil Rytarowski llvm_unreachable("Unexpected state"); 461f07a9995SKamil Rytarowski 462f07a9995SKamil Rytarowski default: 46397206d57SZachary Turner return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " 464f07a9995SKamil Rytarowski "for pid %" PRIu64 ", tid %" PRIu64, 465f07a9995SKamil Rytarowski __FUNCTION__, StateAsCString(action->state), GetID(), 466a5be48b3SPavel Labath thread->GetID()); 467f07a9995SKamil Rytarowski } 468f07a9995SKamil Rytarowski 46997206d57SZachary Turner return Status(); 470f07a9995SKamil Rytarowski } 471f07a9995SKamil Rytarowski 47297206d57SZachary Turner Status NativeProcessNetBSD::Halt() { 47397206d57SZachary Turner Status error; 474f07a9995SKamil Rytarowski 475f07a9995SKamil Rytarowski if (kill(GetID(), SIGSTOP) != 0) 476f07a9995SKamil Rytarowski error.SetErrorToErrno(); 477f07a9995SKamil Rytarowski 478f07a9995SKamil Rytarowski return error; 479f07a9995SKamil Rytarowski } 480f07a9995SKamil Rytarowski 48197206d57SZachary Turner Status NativeProcessNetBSD::Detach() { 48297206d57SZachary Turner Status error; 483f07a9995SKamil Rytarowski 484f07a9995SKamil Rytarowski // Stop monitoring the inferior. 485f07a9995SKamil Rytarowski m_sigchld_handle.reset(); 486f07a9995SKamil Rytarowski 487f07a9995SKamil Rytarowski // Tell ptrace to detach from the process. 488f07a9995SKamil Rytarowski if (GetID() == LLDB_INVALID_PROCESS_ID) 489f07a9995SKamil Rytarowski return error; 490f07a9995SKamil Rytarowski 491f07a9995SKamil Rytarowski return PtraceWrapper(PT_DETACH, GetID()); 492f07a9995SKamil Rytarowski } 493f07a9995SKamil Rytarowski 49497206d57SZachary Turner Status NativeProcessNetBSD::Signal(int signo) { 49597206d57SZachary Turner Status error; 496f07a9995SKamil Rytarowski 497f07a9995SKamil Rytarowski if (kill(GetID(), signo)) 498f07a9995SKamil Rytarowski error.SetErrorToErrno(); 499f07a9995SKamil Rytarowski 500f07a9995SKamil Rytarowski return error; 501f07a9995SKamil Rytarowski } 502f07a9995SKamil Rytarowski 50397206d57SZachary Turner Status NativeProcessNetBSD::Kill() { 504f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 505f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 506f07a9995SKamil Rytarowski 50797206d57SZachary Turner Status error; 508f07a9995SKamil Rytarowski 509f07a9995SKamil Rytarowski switch (m_state) { 510f07a9995SKamil Rytarowski case StateType::eStateInvalid: 511f07a9995SKamil Rytarowski case StateType::eStateExited: 512f07a9995SKamil Rytarowski case StateType::eStateCrashed: 513f07a9995SKamil Rytarowski case StateType::eStateDetached: 514f07a9995SKamil Rytarowski case StateType::eStateUnloaded: 515f07a9995SKamil Rytarowski // Nothing to do - the process is already dead. 516f07a9995SKamil Rytarowski LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 517f07a9995SKamil Rytarowski StateAsCString(m_state)); 518f07a9995SKamil Rytarowski return error; 519f07a9995SKamil Rytarowski 520f07a9995SKamil Rytarowski case StateType::eStateConnected: 521f07a9995SKamil Rytarowski case StateType::eStateAttaching: 522f07a9995SKamil Rytarowski case StateType::eStateLaunching: 523f07a9995SKamil Rytarowski case StateType::eStateStopped: 524f07a9995SKamil Rytarowski case StateType::eStateRunning: 525f07a9995SKamil Rytarowski case StateType::eStateStepping: 526f07a9995SKamil Rytarowski case StateType::eStateSuspended: 527f07a9995SKamil Rytarowski // We can try to kill a process in these states. 528f07a9995SKamil Rytarowski break; 529f07a9995SKamil Rytarowski } 530f07a9995SKamil Rytarowski 531f07a9995SKamil Rytarowski if (kill(GetID(), SIGKILL) != 0) { 532f07a9995SKamil Rytarowski error.SetErrorToErrno(); 533f07a9995SKamil Rytarowski return error; 534f07a9995SKamil Rytarowski } 535f07a9995SKamil Rytarowski 536f07a9995SKamil Rytarowski return error; 537f07a9995SKamil Rytarowski } 538f07a9995SKamil Rytarowski 53997206d57SZachary Turner Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr, 540f07a9995SKamil Rytarowski MemoryRegionInfo &range_info) { 541f07a9995SKamil Rytarowski 542f07a9995SKamil Rytarowski if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 543f07a9995SKamil Rytarowski // We're done. 54497206d57SZachary Turner return Status("unsupported"); 545f07a9995SKamil Rytarowski } 546f07a9995SKamil Rytarowski 54797206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 548f07a9995SKamil Rytarowski if (error.Fail()) { 549f07a9995SKamil Rytarowski return error; 550f07a9995SKamil Rytarowski } 551f07a9995SKamil Rytarowski 552f07a9995SKamil Rytarowski lldb::addr_t prev_base_address = 0; 553f07a9995SKamil Rytarowski // FIXME start by finding the last region that is <= target address using 554f07a9995SKamil Rytarowski // binary search. Data is sorted. 555f07a9995SKamil Rytarowski // There can be a ton of regions on pthreads apps with lots of threads. 556f07a9995SKamil Rytarowski for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 557f07a9995SKamil Rytarowski ++it) { 558f07a9995SKamil Rytarowski MemoryRegionInfo &proc_entry_info = it->first; 559f07a9995SKamil Rytarowski // Sanity check assumption that memory map entries are ascending. 560f07a9995SKamil Rytarowski assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 561f07a9995SKamil Rytarowski "descending memory map entries detected, unexpected"); 562f07a9995SKamil Rytarowski prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 563f07a9995SKamil Rytarowski UNUSED_IF_ASSERT_DISABLED(prev_base_address); 564*05097246SAdrian Prantl // If the target address comes before this entry, indicate distance to next 565*05097246SAdrian Prantl // region. 566f07a9995SKamil Rytarowski if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 567f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 568f07a9995SKamil Rytarowski range_info.GetRange().SetByteSize( 569f07a9995SKamil Rytarowski proc_entry_info.GetRange().GetRangeBase() - load_addr); 570f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 571f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 572f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 573f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 574f07a9995SKamil Rytarowski return error; 575f07a9995SKamil Rytarowski } else if (proc_entry_info.GetRange().Contains(load_addr)) { 576f07a9995SKamil Rytarowski // The target address is within the memory region we're processing here. 577f07a9995SKamil Rytarowski range_info = proc_entry_info; 578f07a9995SKamil Rytarowski return error; 579f07a9995SKamil Rytarowski } 580f07a9995SKamil Rytarowski // The target memory address comes somewhere after the region we just 581f07a9995SKamil Rytarowski // parsed. 582f07a9995SKamil Rytarowski } 583f07a9995SKamil Rytarowski // If we made it here, we didn't find an entry that contained the given 584*05097246SAdrian Prantl // address. Return the load_addr as start and the amount of bytes betwwen 585*05097246SAdrian Prantl // load address and the end of the memory as size. 586f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 587f07a9995SKamil Rytarowski range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 588f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 589f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 590f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 591f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 592f07a9995SKamil Rytarowski return error; 593f07a9995SKamil Rytarowski } 594f07a9995SKamil Rytarowski 59597206d57SZachary Turner Status NativeProcessNetBSD::PopulateMemoryRegionCache() { 596f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 597f07a9995SKamil Rytarowski // If our cache is empty, pull the latest. There should always be at least 598f07a9995SKamil Rytarowski // one memory region if memory region handling is supported. 599f07a9995SKamil Rytarowski if (!m_mem_region_cache.empty()) { 600f07a9995SKamil Rytarowski LLDB_LOG(log, "reusing {0} cached memory region entries", 601f07a9995SKamil Rytarowski m_mem_region_cache.size()); 60297206d57SZachary Turner return Status(); 603f07a9995SKamil Rytarowski } 604f07a9995SKamil Rytarowski 605f07a9995SKamil Rytarowski struct kinfo_vmentry *vm; 606f07a9995SKamil Rytarowski size_t count, i; 607f07a9995SKamil Rytarowski vm = kinfo_getvmmap(GetID(), &count); 608f07a9995SKamil Rytarowski if (vm == NULL) { 609f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 61097206d57SZachary Turner Status error; 611f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 612f07a9995SKamil Rytarowski return error; 613f07a9995SKamil Rytarowski } 614f07a9995SKamil Rytarowski for (i = 0; i < count; i++) { 615f07a9995SKamil Rytarowski MemoryRegionInfo info; 616f07a9995SKamil Rytarowski info.Clear(); 617f07a9995SKamil Rytarowski info.GetRange().SetRangeBase(vm[i].kve_start); 618f07a9995SKamil Rytarowski info.GetRange().SetRangeEnd(vm[i].kve_end); 619f07a9995SKamil Rytarowski info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 620f07a9995SKamil Rytarowski 621f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_READ) 622f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 623f07a9995SKamil Rytarowski else 624f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 625f07a9995SKamil Rytarowski 626f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_WRITE) 627f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 628f07a9995SKamil Rytarowski else 629f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 630f07a9995SKamil Rytarowski 631f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_EXECUTE) 632f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 633f07a9995SKamil Rytarowski else 634f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 635f07a9995SKamil Rytarowski 636f07a9995SKamil Rytarowski if (vm[i].kve_path[0]) 637f07a9995SKamil Rytarowski info.SetName(vm[i].kve_path); 638f07a9995SKamil Rytarowski 639f07a9995SKamil Rytarowski m_mem_region_cache.emplace_back( 640f07a9995SKamil Rytarowski info, FileSpec(info.GetName().GetCString(), true)); 641f07a9995SKamil Rytarowski } 642f07a9995SKamil Rytarowski free(vm); 643f07a9995SKamil Rytarowski 644f07a9995SKamil Rytarowski if (m_mem_region_cache.empty()) { 645*05097246SAdrian Prantl // No entries after attempting to read them. This shouldn't happen. Assume 646*05097246SAdrian Prantl // we don't support map entries. 647f07a9995SKamil Rytarowski LLDB_LOG(log, "failed to find any vmmap entries, assuming no support " 648f07a9995SKamil Rytarowski "for memory region metadata retrieval"); 649f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 65097206d57SZachary Turner Status error; 651f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 652f07a9995SKamil Rytarowski return error; 653f07a9995SKamil Rytarowski } 654f07a9995SKamil Rytarowski LLDB_LOG(log, "read {0} memory region entries from process {1}", 655f07a9995SKamil Rytarowski m_mem_region_cache.size(), GetID()); 656f07a9995SKamil Rytarowski // We support memory retrieval, remember that. 657f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolYes; 65897206d57SZachary Turner return Status(); 659f07a9995SKamil Rytarowski } 660f07a9995SKamil Rytarowski 66197206d57SZachary Turner Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions, 662f07a9995SKamil Rytarowski lldb::addr_t &addr) { 66397206d57SZachary Turner return Status("Unimplemented"); 664f07a9995SKamil Rytarowski } 665f07a9995SKamil Rytarowski 66697206d57SZachary Turner Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) { 66797206d57SZachary Turner return Status("Unimplemented"); 668f07a9995SKamil Rytarowski } 669f07a9995SKamil Rytarowski 670f07a9995SKamil Rytarowski lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() { 671f07a9995SKamil Rytarowski // punt on this for now 672f07a9995SKamil Rytarowski return LLDB_INVALID_ADDRESS; 673f07a9995SKamil Rytarowski } 674f07a9995SKamil Rytarowski 675f07a9995SKamil Rytarowski size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); } 676f07a9995SKamil Rytarowski 67797206d57SZachary Turner Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size, 678f07a9995SKamil Rytarowski bool hardware) { 679f07a9995SKamil Rytarowski if (hardware) 68097206d57SZachary Turner return Status("NativeProcessNetBSD does not support hardware breakpoints"); 681f07a9995SKamil Rytarowski else 682f07a9995SKamil Rytarowski return SetSoftwareBreakpoint(addr, size); 683f07a9995SKamil Rytarowski } 684f07a9995SKamil Rytarowski 68597206d57SZachary Turner Status NativeProcessNetBSD::GetSoftwareBreakpointTrapOpcode( 686f07a9995SKamil Rytarowski size_t trap_opcode_size_hint, size_t &actual_opcode_size, 687f07a9995SKamil Rytarowski const uint8_t *&trap_opcode_bytes) { 688f07a9995SKamil Rytarowski static const uint8_t g_i386_opcode[] = {0xCC}; 689f07a9995SKamil Rytarowski 690f07a9995SKamil Rytarowski switch (m_arch.GetMachine()) { 691f07a9995SKamil Rytarowski case llvm::Triple::x86: 692f07a9995SKamil Rytarowski case llvm::Triple::x86_64: 693f07a9995SKamil Rytarowski trap_opcode_bytes = g_i386_opcode; 694f07a9995SKamil Rytarowski actual_opcode_size = sizeof(g_i386_opcode); 69597206d57SZachary Turner return Status(); 696f07a9995SKamil Rytarowski default: 697f07a9995SKamil Rytarowski assert(false && "CPU type not supported!"); 69897206d57SZachary Turner return Status("CPU type not supported"); 699f07a9995SKamil Rytarowski } 700f07a9995SKamil Rytarowski } 701f07a9995SKamil Rytarowski 70297206d57SZachary Turner Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path, 703f07a9995SKamil Rytarowski FileSpec &file_spec) { 70497206d57SZachary Turner return Status("Unimplemented"); 705f07a9995SKamil Rytarowski } 706f07a9995SKamil Rytarowski 70797206d57SZachary Turner Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name, 708f07a9995SKamil Rytarowski lldb::addr_t &load_addr) { 709f07a9995SKamil Rytarowski load_addr = LLDB_INVALID_ADDRESS; 71097206d57SZachary Turner return Status(); 711f07a9995SKamil Rytarowski } 712f07a9995SKamil Rytarowski 713f07a9995SKamil Rytarowski void NativeProcessNetBSD::SigchldHandler() { 714f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 715f07a9995SKamil Rytarowski // Process all pending waitpid notifications. 716f07a9995SKamil Rytarowski int status; 717c1a6b128SPavel Labath ::pid_t wait_pid = 718c1a6b128SPavel Labath llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG); 719f07a9995SKamil Rytarowski 720f07a9995SKamil Rytarowski if (wait_pid == 0) 721f07a9995SKamil Rytarowski return; // We are done. 722f07a9995SKamil Rytarowski 723f07a9995SKamil Rytarowski if (wait_pid == -1) { 72497206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 725f07a9995SKamil Rytarowski LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error); 726f07a9995SKamil Rytarowski } 727f07a9995SKamil Rytarowski 7283508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 7293508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 7303508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 7313508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 732f07a9995SKamil Rytarowski 733f07a9995SKamil Rytarowski LLDB_LOG(log, 7343508fc8cSPavel Labath "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}", 7353508fc8cSPavel Labath GetID(), wait_pid, status, exited); 736f07a9995SKamil Rytarowski 737f07a9995SKamil Rytarowski if (exited) 7383508fc8cSPavel Labath MonitorExited(wait_pid, wait_status); 7393508fc8cSPavel Labath else { 7404bb74415SKamil Rytarowski assert(wait_status.type == WaitStatus::Stop); 7413508fc8cSPavel Labath MonitorCallback(wait_pid, wait_status.status); 7423508fc8cSPavel Labath } 743f07a9995SKamil Rytarowski } 744f07a9995SKamil Rytarowski 745269eec03SKamil Rytarowski bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { 746a5be48b3SPavel Labath for (const auto &thread : m_threads) { 747a5be48b3SPavel Labath assert(thread && "thread list should not contain NULL threads"); 748a5be48b3SPavel Labath if (thread->GetID() == thread_id) { 749269eec03SKamil Rytarowski // We have this thread. 750269eec03SKamil Rytarowski return true; 751269eec03SKamil Rytarowski } 752269eec03SKamil Rytarowski } 753269eec03SKamil Rytarowski 754269eec03SKamil Rytarowski // We don't have this thread. 755269eec03SKamil Rytarowski return false; 756269eec03SKamil Rytarowski } 757269eec03SKamil Rytarowski 758a5be48b3SPavel Labath NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { 759f07a9995SKamil Rytarowski 760f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 761f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 762f07a9995SKamil Rytarowski 763f07a9995SKamil Rytarowski assert(!HasThreadNoLock(thread_id) && 764f07a9995SKamil Rytarowski "attempted to add a thread by id that already exists"); 765f07a9995SKamil Rytarowski 766f07a9995SKamil Rytarowski // If this is the first thread, save it as the current thread 767f07a9995SKamil Rytarowski if (m_threads.empty()) 768f07a9995SKamil Rytarowski SetCurrentThreadID(thread_id); 769f07a9995SKamil Rytarowski 770a5be48b3SPavel Labath m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id)); 771a5be48b3SPavel Labath return static_cast<NativeThreadNetBSD &>(*m_threads.back()); 772f07a9995SKamil Rytarowski } 773f07a9995SKamil Rytarowski 77496e600fcSPavel Labath Status NativeProcessNetBSD::Attach() { 775f07a9995SKamil Rytarowski // Attach to the requested process. 776f07a9995SKamil Rytarowski // An attach will cause the thread to stop with a SIGSTOP. 77796e600fcSPavel Labath Status status = PtraceWrapper(PT_ATTACH, m_pid); 77896e600fcSPavel Labath if (status.Fail()) 77996e600fcSPavel Labath return status; 780f07a9995SKamil Rytarowski 78196e600fcSPavel Labath int wstatus; 782*05097246SAdrian Prantl // Need to use WALLSIG otherwise we receive an error with errno=ECHLD At this 783*05097246SAdrian Prantl // point we should have a thread stopped if waitpid succeeds. 78496e600fcSPavel Labath if ((wstatus = waitpid(m_pid, NULL, WALLSIG)) < 0) 78596e600fcSPavel Labath return Status(errno, eErrorTypePOSIX); 786f07a9995SKamil Rytarowski 787f07a9995SKamil Rytarowski /* Initialize threads */ 78896e600fcSPavel Labath status = ReinitializeThreads(); 78996e600fcSPavel Labath if (status.Fail()) 79096e600fcSPavel Labath return status; 791f07a9995SKamil Rytarowski 792a5be48b3SPavel Labath for (const auto &thread : m_threads) 793a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); 79436e23ecaSKamil Rytarowski 795f07a9995SKamil Rytarowski // Let our process instance know the thread has stopped. 796f07a9995SKamil Rytarowski SetState(StateType::eStateStopped); 79796e600fcSPavel Labath return Status(); 798f07a9995SKamil Rytarowski } 799f07a9995SKamil Rytarowski 80097206d57SZachary Turner Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf, 80197206d57SZachary Turner size_t size, size_t &bytes_read) { 802f07a9995SKamil Rytarowski unsigned char *dst = static_cast<unsigned char *>(buf); 803f07a9995SKamil Rytarowski struct ptrace_io_desc io; 804f07a9995SKamil Rytarowski 805f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 806f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 807f07a9995SKamil Rytarowski 808f07a9995SKamil Rytarowski bytes_read = 0; 809f07a9995SKamil Rytarowski io.piod_op = PIOD_READ_D; 810f07a9995SKamil Rytarowski io.piod_len = size; 811f07a9995SKamil Rytarowski 812f07a9995SKamil Rytarowski do { 813f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_read); 814f07a9995SKamil Rytarowski io.piod_addr = dst + bytes_read; 815f07a9995SKamil Rytarowski 81697206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 817f07a9995SKamil Rytarowski if (error.Fail()) 818f07a9995SKamil Rytarowski return error; 819f07a9995SKamil Rytarowski 820f07a9995SKamil Rytarowski bytes_read = io.piod_len; 821f07a9995SKamil Rytarowski io.piod_len = size - bytes_read; 822f07a9995SKamil Rytarowski } while (bytes_read < size); 823f07a9995SKamil Rytarowski 82497206d57SZachary Turner return Status(); 825f07a9995SKamil Rytarowski } 826f07a9995SKamil Rytarowski 82797206d57SZachary Turner Status NativeProcessNetBSD::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 828f07a9995SKamil Rytarowski size_t size, 829f07a9995SKamil Rytarowski size_t &bytes_read) { 83097206d57SZachary Turner Status error = ReadMemory(addr, buf, size, bytes_read); 831f07a9995SKamil Rytarowski if (error.Fail()) 832f07a9995SKamil Rytarowski return error; 833f07a9995SKamil Rytarowski return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 834f07a9995SKamil Rytarowski } 835f07a9995SKamil Rytarowski 83697206d57SZachary Turner Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf, 837f07a9995SKamil Rytarowski size_t size, size_t &bytes_written) { 838f07a9995SKamil Rytarowski const unsigned char *src = static_cast<const unsigned char *>(buf); 83997206d57SZachary Turner Status error; 840f07a9995SKamil Rytarowski struct ptrace_io_desc io; 841f07a9995SKamil Rytarowski 842f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 843f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 844f07a9995SKamil Rytarowski 845f07a9995SKamil Rytarowski bytes_written = 0; 846f07a9995SKamil Rytarowski io.piod_op = PIOD_WRITE_D; 847f07a9995SKamil Rytarowski io.piod_len = size; 848f07a9995SKamil Rytarowski 849f07a9995SKamil Rytarowski do { 850269eec03SKamil Rytarowski io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written)); 851f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_written); 852f07a9995SKamil Rytarowski 85397206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 854f07a9995SKamil Rytarowski if (error.Fail()) 855f07a9995SKamil Rytarowski return error; 856f07a9995SKamil Rytarowski 857f07a9995SKamil Rytarowski bytes_written = io.piod_len; 858f07a9995SKamil Rytarowski io.piod_len = size - bytes_written; 859f07a9995SKamil Rytarowski } while (bytes_written < size); 860f07a9995SKamil Rytarowski 861f07a9995SKamil Rytarowski return error; 862f07a9995SKamil Rytarowski } 863f07a9995SKamil Rytarowski 864f07a9995SKamil Rytarowski llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 865f07a9995SKamil Rytarowski NativeProcessNetBSD::GetAuxvData() const { 866f07a9995SKamil Rytarowski /* 867f07a9995SKamil Rytarowski * ELF_AUX_ENTRIES is currently restricted to kernel 868f07a9995SKamil Rytarowski * (<sys/exec_elf.h> r. 1.155 specifies 15) 869f07a9995SKamil Rytarowski * 870f07a9995SKamil Rytarowski * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this 871f07a9995SKamil Rytarowski * information isn't needed. 872f07a9995SKamil Rytarowski */ 873f07a9995SKamil Rytarowski size_t auxv_size = 100 * sizeof(AuxInfo); 874f07a9995SKamil Rytarowski 875e831bb3cSPavel Labath ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf = 876dbda2851SPavel Labath llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size); 877f07a9995SKamil Rytarowski 878269eec03SKamil Rytarowski struct ptrace_io_desc io; 879269eec03SKamil Rytarowski io.piod_op = PIOD_READ_AUXV; 880269eec03SKamil Rytarowski io.piod_offs = 0; 881e831bb3cSPavel Labath io.piod_addr = static_cast<void *>(buf.get()->getBufferStart()); 882269eec03SKamil Rytarowski io.piod_len = auxv_size; 883f07a9995SKamil Rytarowski 88497206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 885f07a9995SKamil Rytarowski 886f07a9995SKamil Rytarowski if (error.Fail()) 887f07a9995SKamil Rytarowski return std::error_code(error.GetError(), std::generic_category()); 888f07a9995SKamil Rytarowski 889f07a9995SKamil Rytarowski if (io.piod_len < 1) 890f07a9995SKamil Rytarowski return std::error_code(ECANCELED, std::generic_category()); 891f07a9995SKamil Rytarowski 892e831bb3cSPavel Labath return std::move(buf); 893f07a9995SKamil Rytarowski } 8943eef2b5eSKamil Rytarowski 89597206d57SZachary Turner Status NativeProcessNetBSD::ReinitializeThreads() { 8963eef2b5eSKamil Rytarowski // Clear old threads 8973eef2b5eSKamil Rytarowski m_threads.clear(); 8983eef2b5eSKamil Rytarowski 8993eef2b5eSKamil Rytarowski // Initialize new thread 9003eef2b5eSKamil Rytarowski struct ptrace_lwpinfo info = {}; 90197206d57SZachary Turner Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 9023eef2b5eSKamil Rytarowski if (error.Fail()) { 9033eef2b5eSKamil Rytarowski return error; 9043eef2b5eSKamil Rytarowski } 9053eef2b5eSKamil Rytarowski // Reinitialize from scratch threads and register them in process 9063eef2b5eSKamil Rytarowski while (info.pl_lwpid != 0) { 907a5be48b3SPavel Labath AddThread(info.pl_lwpid); 9083eef2b5eSKamil Rytarowski error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 9093eef2b5eSKamil Rytarowski if (error.Fail()) { 9103eef2b5eSKamil Rytarowski return error; 9113eef2b5eSKamil Rytarowski } 9123eef2b5eSKamil Rytarowski } 9133eef2b5eSKamil Rytarowski 9143eef2b5eSKamil Rytarowski return error; 9153eef2b5eSKamil Rytarowski } 916