11a3d19ddSKamil Rytarowski //===-- NativeProcessNetBSD.cpp ------------------------------- -*- C++ -*-===// 21a3d19ddSKamil Rytarowski // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61a3d19ddSKamil Rytarowski // 71a3d19ddSKamil Rytarowski //===----------------------------------------------------------------------===// 81a3d19ddSKamil Rytarowski 91a3d19ddSKamil Rytarowski #include "NativeProcessNetBSD.h" 101a3d19ddSKamil Rytarowski 111a3d19ddSKamil Rytarowski 121a3d19ddSKamil Rytarowski 131a3d19ddSKamil Rytarowski #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 14f07a9995SKamil Rytarowski #include "lldb/Host/HostProcess.h" 15f07a9995SKamil Rytarowski #include "lldb/Host/common/NativeRegisterContext.h" 16f07a9995SKamil Rytarowski #include "lldb/Host/posix/ProcessLauncherPosixFork.h" 17f07a9995SKamil Rytarowski #include "lldb/Target/Process.h" 18d821c997SPavel Labath #include "lldb/Utility/State.h" 19c1a6b128SPavel Labath #include "llvm/Support/Errno.h" 201a3d19ddSKamil Rytarowski 211a3d19ddSKamil Rytarowski // System includes - They have to be included after framework includes because 2205097246SAdrian Prantl // they define some macros which collide with variable names in other modules 23f07a9995SKamil Rytarowski // clang-format off 24f07a9995SKamil Rytarowski #include <sys/types.h> 25f07a9995SKamil Rytarowski #include <sys/ptrace.h> 26f07a9995SKamil Rytarowski #include <sys/sysctl.h> 27f07a9995SKamil Rytarowski #include <sys/wait.h> 28f07a9995SKamil Rytarowski #include <uvm/uvm_prot.h> 29f07a9995SKamil Rytarowski #include <elf.h> 30f07a9995SKamil Rytarowski #include <util.h> 31f07a9995SKamil Rytarowski // clang-format on 321a3d19ddSKamil Rytarowski 331a3d19ddSKamil Rytarowski using namespace lldb; 341a3d19ddSKamil Rytarowski using namespace lldb_private; 351a3d19ddSKamil Rytarowski using namespace lldb_private::process_netbsd; 361a3d19ddSKamil Rytarowski using namespace llvm; 371a3d19ddSKamil Rytarowski 38f07a9995SKamil Rytarowski // Simple helper function to ensure flags are enabled on the given file 39f07a9995SKamil Rytarowski // descriptor. 4097206d57SZachary Turner static Status EnsureFDFlags(int fd, int flags) { 4197206d57SZachary Turner Status error; 42f07a9995SKamil Rytarowski 43f07a9995SKamil Rytarowski int status = fcntl(fd, F_GETFL); 44f07a9995SKamil Rytarowski if (status == -1) { 45f07a9995SKamil Rytarowski error.SetErrorToErrno(); 46f07a9995SKamil Rytarowski return error; 47f07a9995SKamil Rytarowski } 48f07a9995SKamil Rytarowski 49f07a9995SKamil Rytarowski if (fcntl(fd, F_SETFL, status | flags) == -1) { 50f07a9995SKamil Rytarowski error.SetErrorToErrno(); 51f07a9995SKamil Rytarowski return error; 52f07a9995SKamil Rytarowski } 53f07a9995SKamil Rytarowski 54f07a9995SKamil Rytarowski return error; 55f07a9995SKamil Rytarowski } 56f07a9995SKamil Rytarowski 571a3d19ddSKamil Rytarowski // Public Static Methods 581a3d19ddSKamil Rytarowski 5982abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 6096e600fcSPavel Labath NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info, 6196e600fcSPavel Labath NativeDelegate &native_delegate, 6296e600fcSPavel Labath MainLoop &mainloop) const { 63f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 64f07a9995SKamil Rytarowski 6596e600fcSPavel Labath Status status; 6696e600fcSPavel Labath ::pid_t pid = ProcessLauncherPosixFork() 6796e600fcSPavel Labath .LaunchProcess(launch_info, status) 6896e600fcSPavel Labath .GetProcessId(); 6996e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 7096e600fcSPavel Labath if (status.Fail()) { 7196e600fcSPavel Labath LLDB_LOG(log, "failed to launch process: {0}", status); 7296e600fcSPavel Labath return status.ToError(); 73f07a9995SKamil Rytarowski } 74f07a9995SKamil Rytarowski 7596e600fcSPavel Labath // Wait for the child process to trap on its call to execve. 7696e600fcSPavel Labath int wstatus; 7796e600fcSPavel Labath ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); 7896e600fcSPavel Labath assert(wpid == pid); 7996e600fcSPavel Labath (void)wpid; 8096e600fcSPavel Labath if (!WIFSTOPPED(wstatus)) { 8196e600fcSPavel Labath LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}", 8296e600fcSPavel Labath WaitStatus::Decode(wstatus)); 8396e600fcSPavel Labath return llvm::make_error<StringError>("Could not sync with inferior process", 8496e600fcSPavel Labath llvm::inconvertibleErrorCode()); 8596e600fcSPavel Labath } 8696e600fcSPavel Labath LLDB_LOG(log, "inferior started, now in stopped state"); 87f07a9995SKamil Rytarowski 8836e82208SPavel Labath ProcessInstanceInfo Info; 8936e82208SPavel Labath if (!Host::GetProcessInfo(pid, Info)) { 9036e82208SPavel Labath return llvm::make_error<StringError>("Cannot get process architecture", 9136e82208SPavel Labath llvm::inconvertibleErrorCode()); 9236e82208SPavel Labath } 9396e600fcSPavel Labath 9496e600fcSPavel Labath // Set the architecture to the exe architecture. 9596e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 9636e82208SPavel Labath Info.GetArchitecture().GetArchitectureName()); 9796e600fcSPavel Labath 9882abefa4SPavel Labath std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD( 9996e600fcSPavel Labath pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, 10036e82208SPavel Labath Info.GetArchitecture(), mainloop)); 10196e600fcSPavel Labath 10282abefa4SPavel Labath status = process_up->ReinitializeThreads(); 10396e600fcSPavel Labath if (status.Fail()) 10496e600fcSPavel Labath return status.ToError(); 10596e600fcSPavel Labath 106a5be48b3SPavel Labath for (const auto &thread : process_up->m_threads) 107a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); 1088a4bf06bSKamil Rytarowski process_up->SetState(StateType::eStateStopped, false); 10996e600fcSPavel Labath 11082abefa4SPavel Labath return std::move(process_up); 111f07a9995SKamil Rytarowski } 112f07a9995SKamil Rytarowski 11382abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 11482abefa4SPavel Labath NativeProcessNetBSD::Factory::Attach( 1151a3d19ddSKamil Rytarowski lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 11696e600fcSPavel Labath MainLoop &mainloop) const { 117f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 118f07a9995SKamil Rytarowski LLDB_LOG(log, "pid = {0:x}", pid); 119f07a9995SKamil Rytarowski 120f07a9995SKamil Rytarowski // Retrieve the architecture for the running process. 12136e82208SPavel Labath ProcessInstanceInfo Info; 12236e82208SPavel Labath if (!Host::GetProcessInfo(pid, Info)) { 12336e82208SPavel Labath return llvm::make_error<StringError>("Cannot get process architecture", 12436e82208SPavel Labath llvm::inconvertibleErrorCode()); 12536e82208SPavel Labath } 126f07a9995SKamil Rytarowski 12736e82208SPavel Labath std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD( 12836e82208SPavel Labath pid, -1, native_delegate, Info.GetArchitecture(), mainloop)); 129f07a9995SKamil Rytarowski 13002d4e50eSPavel Labath Status status = process_up->Attach(); 13196e600fcSPavel Labath if (!status.Success()) 13296e600fcSPavel Labath return status.ToError(); 133f07a9995SKamil Rytarowski 13482abefa4SPavel Labath return std::move(process_up); 1351a3d19ddSKamil Rytarowski } 1361a3d19ddSKamil Rytarowski 1371a3d19ddSKamil Rytarowski // Public Instance Methods 1381a3d19ddSKamil Rytarowski 13996e600fcSPavel Labath NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd, 14096e600fcSPavel Labath NativeDelegate &delegate, 14196e600fcSPavel Labath const ArchSpec &arch, 14296e600fcSPavel Labath MainLoop &mainloop) 143b09bc8a2SMichal Gorny : NativeProcessELF(pid, terminal_fd, delegate), m_arch(arch) { 14496e600fcSPavel Labath if (m_terminal_fd != -1) { 14596e600fcSPavel Labath Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 14696e600fcSPavel Labath assert(status.Success()); 14796e600fcSPavel Labath } 14896e600fcSPavel Labath 14996e600fcSPavel Labath Status status; 15096e600fcSPavel Labath m_sigchld_handle = mainloop.RegisterSignal( 15196e600fcSPavel Labath SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status); 15296e600fcSPavel Labath assert(m_sigchld_handle && status.Success()); 15396e600fcSPavel Labath } 154f07a9995SKamil Rytarowski 155f07a9995SKamil Rytarowski // Handles all waitpid events from the inferior process. 156f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) { 157f07a9995SKamil Rytarowski switch (signal) { 158f07a9995SKamil Rytarowski case SIGTRAP: 159f07a9995SKamil Rytarowski return MonitorSIGTRAP(pid); 160f07a9995SKamil Rytarowski case SIGSTOP: 161f07a9995SKamil Rytarowski return MonitorSIGSTOP(pid); 162f07a9995SKamil Rytarowski default: 163f07a9995SKamil Rytarowski return MonitorSignal(pid, signal); 164f07a9995SKamil Rytarowski } 165f07a9995SKamil Rytarowski } 166f07a9995SKamil Rytarowski 1673508fc8cSPavel Labath void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) { 168f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 169f07a9995SKamil Rytarowski 1703508fc8cSPavel Labath LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid); 171f07a9995SKamil Rytarowski 172f07a9995SKamil Rytarowski /* Stop Tracking All Threads attached to Process */ 173f07a9995SKamil Rytarowski m_threads.clear(); 174f07a9995SKamil Rytarowski 1753508fc8cSPavel Labath SetExitStatus(status, true); 176f07a9995SKamil Rytarowski 177f07a9995SKamil Rytarowski // Notify delegate that our process has exited. 178f07a9995SKamil Rytarowski SetState(StateType::eStateExited, true); 179f07a9995SKamil Rytarowski } 180f07a9995SKamil Rytarowski 181f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) { 182f07a9995SKamil Rytarowski ptrace_siginfo_t info; 183f07a9995SKamil Rytarowski 184f07a9995SKamil Rytarowski const auto siginfo_err = 185f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 186f07a9995SKamil Rytarowski 187f07a9995SKamil Rytarowski // Get details on the signal raised. 188f07a9995SKamil Rytarowski if (siginfo_err.Success()) { 189f07a9995SKamil Rytarowski // Handle SIGSTOP from LLGS (LLDB GDB Server) 190f07a9995SKamil Rytarowski if (info.psi_siginfo.si_code == SI_USER && 191f07a9995SKamil Rytarowski info.psi_siginfo.si_pid == ::getpid()) { 192a5be48b3SPavel Labath /* Stop Tracking all Threads attached to Process */ 193a5be48b3SPavel Labath for (const auto &thread : m_threads) { 194a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( 195f07a9995SKamil Rytarowski SIGSTOP, &info.psi_siginfo); 196f07a9995SKamil Rytarowski } 197f07a9995SKamil Rytarowski } 198e1c159e8SMichal Gorny SetState(StateType::eStateStopped, true); 199f07a9995SKamil Rytarowski } 200f07a9995SKamil Rytarowski } 201f07a9995SKamil Rytarowski 202f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { 203f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 204f07a9995SKamil Rytarowski ptrace_siginfo_t info; 205f07a9995SKamil Rytarowski 206f07a9995SKamil Rytarowski const auto siginfo_err = 207f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 208f07a9995SKamil Rytarowski 209f07a9995SKamil Rytarowski // Get details on the signal raised. 21036e23ecaSKamil Rytarowski if (siginfo_err.Fail()) { 21136e23ecaSKamil Rytarowski return; 21236e23ecaSKamil Rytarowski } 21336e23ecaSKamil Rytarowski 214f07a9995SKamil Rytarowski switch (info.psi_siginfo.si_code) { 215f07a9995SKamil Rytarowski case TRAP_BRKPT: 216a5be48b3SPavel Labath for (const auto &thread : m_threads) { 217a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); 218a5be48b3SPavel Labath FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread)); 219f07a9995SKamil Rytarowski } 220f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 221f07a9995SKamil Rytarowski break; 2223eef2b5eSKamil Rytarowski case TRAP_TRACE: 223a5be48b3SPavel Labath for (const auto &thread : m_threads) 224a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace(); 2253eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2263eef2b5eSKamil Rytarowski break; 2273eef2b5eSKamil Rytarowski case TRAP_EXEC: { 22897206d57SZachary Turner Status error = ReinitializeThreads(); 2293eef2b5eSKamil Rytarowski if (error.Fail()) { 2303eef2b5eSKamil Rytarowski SetState(StateType::eStateInvalid); 2313eef2b5eSKamil Rytarowski return; 2323eef2b5eSKamil Rytarowski } 2333eef2b5eSKamil Rytarowski 2343eef2b5eSKamil Rytarowski // Let our delegate know we have just exec'd. 2353eef2b5eSKamil Rytarowski NotifyDidExec(); 2363eef2b5eSKamil Rytarowski 237a5be48b3SPavel Labath for (const auto &thread : m_threads) 238a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec(); 2393eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2403eef2b5eSKamil Rytarowski } break; 24136e23ecaSKamil Rytarowski case TRAP_DBREG: { 242baf64b65SMichal Gorny // Find the thread. 243baf64b65SMichal Gorny NativeThreadNetBSD* thread = nullptr; 244baf64b65SMichal Gorny for (const auto &t : m_threads) { 245baf64b65SMichal Gorny if (t->GetID() == info.psi_lwpid) { 246baf64b65SMichal Gorny thread = static_cast<NativeThreadNetBSD *>(t.get()); 247baf64b65SMichal Gorny break; 248baf64b65SMichal Gorny } 249baf64b65SMichal Gorny } 250baf64b65SMichal Gorny if (!thread) { 251baf64b65SMichal Gorny LLDB_LOG(log, 252baf64b65SMichal Gorny "thread not found in m_threads, pid = {0}, LWP = {1}", 253baf64b65SMichal Gorny GetID(), info.psi_lwpid); 254baf64b65SMichal Gorny break; 255baf64b65SMichal Gorny } 256baf64b65SMichal Gorny 25736e23ecaSKamil Rytarowski // If a watchpoint was hit, report it 258baf64b65SMichal Gorny uint32_t wp_index = LLDB_INVALID_INDEX32; 259baf64b65SMichal Gorny Status error = thread->GetRegisterContext().GetWatchpointHitIndex( 260a5be48b3SPavel Labath wp_index, (uintptr_t)info.psi_siginfo.si_addr); 26136e23ecaSKamil Rytarowski if (error.Fail()) 26236e23ecaSKamil Rytarowski LLDB_LOG(log, 26336e23ecaSKamil Rytarowski "received error while checking for watchpoint hits, pid = " 26436e23ecaSKamil Rytarowski "{0}, LWP = {1}, error = {2}", 26536e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 26636e23ecaSKamil Rytarowski if (wp_index != LLDB_INVALID_INDEX32) { 267a5be48b3SPavel Labath for (const auto &thread : m_threads) 268a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint( 269a5be48b3SPavel Labath wp_index); 27036e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 27136e23ecaSKamil Rytarowski break; 27236e23ecaSKamil Rytarowski } 27336e23ecaSKamil Rytarowski 27436e23ecaSKamil Rytarowski // If a breakpoint was hit, report it 275baf64b65SMichal Gorny uint32_t bp_index = LLDB_INVALID_INDEX32; 276baf64b65SMichal Gorny error = thread->GetRegisterContext().GetHardwareBreakHitIndex( 277baf64b65SMichal Gorny bp_index, (uintptr_t)info.psi_siginfo.si_addr); 27836e23ecaSKamil Rytarowski if (error.Fail()) 27936e23ecaSKamil Rytarowski LLDB_LOG(log, 28036e23ecaSKamil Rytarowski "received error while checking for hardware " 28136e23ecaSKamil Rytarowski "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", 28236e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 28336e23ecaSKamil Rytarowski if (bp_index != LLDB_INVALID_INDEX32) { 284a5be48b3SPavel Labath for (const auto &thread : m_threads) 285a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); 28636e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 28736e23ecaSKamil Rytarowski break; 28836e23ecaSKamil Rytarowski } 28936e23ecaSKamil Rytarowski } break; 290f07a9995SKamil Rytarowski } 291f07a9995SKamil Rytarowski } 292f07a9995SKamil Rytarowski 293f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { 294f07a9995SKamil Rytarowski ptrace_siginfo_t info; 295f07a9995SKamil Rytarowski const auto siginfo_err = 296f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 297f07a9995SKamil Rytarowski 298a5be48b3SPavel Labath for (const auto &thread : m_threads) { 299a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( 300f07a9995SKamil Rytarowski info.psi_siginfo.si_signo, &info.psi_siginfo); 301f07a9995SKamil Rytarowski } 302f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 303f07a9995SKamil Rytarowski } 304f07a9995SKamil Rytarowski 30597206d57SZachary Turner Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 306f07a9995SKamil Rytarowski int data, int *result) { 307f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 30897206d57SZachary Turner Status error; 309f07a9995SKamil Rytarowski int ret; 310f07a9995SKamil Rytarowski 311f07a9995SKamil Rytarowski errno = 0; 312f07a9995SKamil Rytarowski ret = ptrace(req, static_cast<::pid_t>(pid), addr, data); 313f07a9995SKamil Rytarowski 314f07a9995SKamil Rytarowski if (ret == -1) 315f07a9995SKamil Rytarowski error.SetErrorToErrno(); 316f07a9995SKamil Rytarowski 317f07a9995SKamil Rytarowski if (result) 318f07a9995SKamil Rytarowski *result = ret; 319f07a9995SKamil Rytarowski 320f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret); 321f07a9995SKamil Rytarowski 322f07a9995SKamil Rytarowski if (error.Fail()) 323f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace() failed: {0}", error); 324f07a9995SKamil Rytarowski 325f07a9995SKamil Rytarowski return error; 326f07a9995SKamil Rytarowski } 327f07a9995SKamil Rytarowski 32897206d57SZachary Turner Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { 329f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 330f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 331f07a9995SKamil Rytarowski 332a5be48b3SPavel Labath const auto &thread = m_threads[0]; 333f07a9995SKamil Rytarowski const ResumeAction *const action = 334a5be48b3SPavel Labath resume_actions.GetActionForThread(thread->GetID(), true); 335f07a9995SKamil Rytarowski 336f07a9995SKamil Rytarowski if (action == nullptr) { 337f07a9995SKamil Rytarowski LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 338a5be48b3SPavel Labath thread->GetID()); 33997206d57SZachary Turner return Status(); 340f07a9995SKamil Rytarowski } 341f07a9995SKamil Rytarowski 34297206d57SZachary Turner Status error; 343a292a494SMichal Gorny int signal = 344a292a494SMichal Gorny action->signal != LLDB_INVALID_SIGNAL_NUMBER ? action->signal : 0; 3453eef2b5eSKamil Rytarowski 346f07a9995SKamil Rytarowski switch (action->state) { 347f07a9995SKamil Rytarowski case eStateRunning: { 348f07a9995SKamil Rytarowski // Run the thread, possibly feeding it the signal. 3493eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, 350a292a494SMichal Gorny signal); 351f07a9995SKamil Rytarowski if (!error.Success()) 352f07a9995SKamil Rytarowski return error; 353a5be48b3SPavel Labath for (const auto &thread : m_threads) 354a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetRunning(); 355f07a9995SKamil Rytarowski SetState(eStateRunning, true); 356f07a9995SKamil Rytarowski break; 357f07a9995SKamil Rytarowski } 358f07a9995SKamil Rytarowski case eStateStepping: 3593eef2b5eSKamil Rytarowski // Run the thread, possibly feeding it the signal. 3603eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, 361a292a494SMichal Gorny signal); 3623eef2b5eSKamil Rytarowski if (!error.Success()) 3633eef2b5eSKamil Rytarowski return error; 364a5be48b3SPavel Labath for (const auto &thread : m_threads) 365a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStepping(); 3663eef2b5eSKamil Rytarowski SetState(eStateStepping, true); 367f07a9995SKamil Rytarowski break; 368f07a9995SKamil Rytarowski 369f07a9995SKamil Rytarowski case eStateSuspended: 370f07a9995SKamil Rytarowski case eStateStopped: 371f07a9995SKamil Rytarowski llvm_unreachable("Unexpected state"); 372f07a9995SKamil Rytarowski 373f07a9995SKamil Rytarowski default: 37497206d57SZachary Turner return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " 375f07a9995SKamil Rytarowski "for pid %" PRIu64 ", tid %" PRIu64, 376f07a9995SKamil Rytarowski __FUNCTION__, StateAsCString(action->state), GetID(), 377a5be48b3SPavel Labath thread->GetID()); 378f07a9995SKamil Rytarowski } 379f07a9995SKamil Rytarowski 38097206d57SZachary Turner return Status(); 381f07a9995SKamil Rytarowski } 382f07a9995SKamil Rytarowski 38397206d57SZachary Turner Status NativeProcessNetBSD::Halt() { 384*77cc2464SMichał Górny return PtraceWrapper(PT_STOP, GetID()); 385f07a9995SKamil Rytarowski } 386f07a9995SKamil Rytarowski 38797206d57SZachary Turner Status NativeProcessNetBSD::Detach() { 38897206d57SZachary Turner Status error; 389f07a9995SKamil Rytarowski 390f07a9995SKamil Rytarowski // Stop monitoring the inferior. 391f07a9995SKamil Rytarowski m_sigchld_handle.reset(); 392f07a9995SKamil Rytarowski 393f07a9995SKamil Rytarowski // Tell ptrace to detach from the process. 394f07a9995SKamil Rytarowski if (GetID() == LLDB_INVALID_PROCESS_ID) 395f07a9995SKamil Rytarowski return error; 396f07a9995SKamil Rytarowski 397f07a9995SKamil Rytarowski return PtraceWrapper(PT_DETACH, GetID()); 398f07a9995SKamil Rytarowski } 399f07a9995SKamil Rytarowski 40097206d57SZachary Turner Status NativeProcessNetBSD::Signal(int signo) { 40197206d57SZachary Turner Status error; 402f07a9995SKamil Rytarowski 403f07a9995SKamil Rytarowski if (kill(GetID(), signo)) 404f07a9995SKamil Rytarowski error.SetErrorToErrno(); 405f07a9995SKamil Rytarowski 406f07a9995SKamil Rytarowski return error; 407f07a9995SKamil Rytarowski } 408f07a9995SKamil Rytarowski 409*77cc2464SMichał Górny Status NativeProcessNetBSD::Interrupt() { 410*77cc2464SMichał Górny return PtraceWrapper(PT_STOP, GetID()); 411*77cc2464SMichał Górny } 412*77cc2464SMichał Górny 41397206d57SZachary Turner Status NativeProcessNetBSD::Kill() { 414f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 415f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 416f07a9995SKamil Rytarowski 41797206d57SZachary Turner Status error; 418f07a9995SKamil Rytarowski 419f07a9995SKamil Rytarowski switch (m_state) { 420f07a9995SKamil Rytarowski case StateType::eStateInvalid: 421f07a9995SKamil Rytarowski case StateType::eStateExited: 422f07a9995SKamil Rytarowski case StateType::eStateCrashed: 423f07a9995SKamil Rytarowski case StateType::eStateDetached: 424f07a9995SKamil Rytarowski case StateType::eStateUnloaded: 425f07a9995SKamil Rytarowski // Nothing to do - the process is already dead. 426f07a9995SKamil Rytarowski LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 427f07a9995SKamil Rytarowski StateAsCString(m_state)); 428f07a9995SKamil Rytarowski return error; 429f07a9995SKamil Rytarowski 430f07a9995SKamil Rytarowski case StateType::eStateConnected: 431f07a9995SKamil Rytarowski case StateType::eStateAttaching: 432f07a9995SKamil Rytarowski case StateType::eStateLaunching: 433f07a9995SKamil Rytarowski case StateType::eStateStopped: 434f07a9995SKamil Rytarowski case StateType::eStateRunning: 435f07a9995SKamil Rytarowski case StateType::eStateStepping: 436f07a9995SKamil Rytarowski case StateType::eStateSuspended: 437f07a9995SKamil Rytarowski // We can try to kill a process in these states. 438f07a9995SKamil Rytarowski break; 439f07a9995SKamil Rytarowski } 440f07a9995SKamil Rytarowski 441f07a9995SKamil Rytarowski if (kill(GetID(), SIGKILL) != 0) { 442f07a9995SKamil Rytarowski error.SetErrorToErrno(); 443f07a9995SKamil Rytarowski return error; 444f07a9995SKamil Rytarowski } 445f07a9995SKamil Rytarowski 446f07a9995SKamil Rytarowski return error; 447f07a9995SKamil Rytarowski } 448f07a9995SKamil Rytarowski 44997206d57SZachary Turner Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr, 450f07a9995SKamil Rytarowski MemoryRegionInfo &range_info) { 451f07a9995SKamil Rytarowski 452f07a9995SKamil Rytarowski if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 453f07a9995SKamil Rytarowski // We're done. 45497206d57SZachary Turner return Status("unsupported"); 455f07a9995SKamil Rytarowski } 456f07a9995SKamil Rytarowski 45797206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 458f07a9995SKamil Rytarowski if (error.Fail()) { 459f07a9995SKamil Rytarowski return error; 460f07a9995SKamil Rytarowski } 461f07a9995SKamil Rytarowski 462f07a9995SKamil Rytarowski lldb::addr_t prev_base_address = 0; 463f07a9995SKamil Rytarowski // FIXME start by finding the last region that is <= target address using 464f07a9995SKamil Rytarowski // binary search. Data is sorted. 465f07a9995SKamil Rytarowski // There can be a ton of regions on pthreads apps with lots of threads. 466f07a9995SKamil Rytarowski for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 467f07a9995SKamil Rytarowski ++it) { 468f07a9995SKamil Rytarowski MemoryRegionInfo &proc_entry_info = it->first; 469f07a9995SKamil Rytarowski // Sanity check assumption that memory map entries are ascending. 470f07a9995SKamil Rytarowski assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 471f07a9995SKamil Rytarowski "descending memory map entries detected, unexpected"); 472f07a9995SKamil Rytarowski prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 473f07a9995SKamil Rytarowski UNUSED_IF_ASSERT_DISABLED(prev_base_address); 47405097246SAdrian Prantl // If the target address comes before this entry, indicate distance to next 47505097246SAdrian Prantl // region. 476f07a9995SKamil Rytarowski if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 477f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 478f07a9995SKamil Rytarowski range_info.GetRange().SetByteSize( 479f07a9995SKamil Rytarowski proc_entry_info.GetRange().GetRangeBase() - load_addr); 480f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 481f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 482f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 483f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 484f07a9995SKamil Rytarowski return error; 485f07a9995SKamil Rytarowski } else if (proc_entry_info.GetRange().Contains(load_addr)) { 486f07a9995SKamil Rytarowski // The target address is within the memory region we're processing here. 487f07a9995SKamil Rytarowski range_info = proc_entry_info; 488f07a9995SKamil Rytarowski return error; 489f07a9995SKamil Rytarowski } 490f07a9995SKamil Rytarowski // The target memory address comes somewhere after the region we just 491f07a9995SKamil Rytarowski // parsed. 492f07a9995SKamil Rytarowski } 493f07a9995SKamil Rytarowski // If we made it here, we didn't find an entry that contained the given 49405097246SAdrian Prantl // address. Return the load_addr as start and the amount of bytes betwwen 49505097246SAdrian Prantl // load address and the end of the memory as size. 496f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 497f07a9995SKamil Rytarowski range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 498f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 499f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 500f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 501f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 502f07a9995SKamil Rytarowski return error; 503f07a9995SKamil Rytarowski } 504f07a9995SKamil Rytarowski 50597206d57SZachary Turner Status NativeProcessNetBSD::PopulateMemoryRegionCache() { 506f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 507f07a9995SKamil Rytarowski // If our cache is empty, pull the latest. There should always be at least 508f07a9995SKamil Rytarowski // one memory region if memory region handling is supported. 509f07a9995SKamil Rytarowski if (!m_mem_region_cache.empty()) { 510f07a9995SKamil Rytarowski LLDB_LOG(log, "reusing {0} cached memory region entries", 511f07a9995SKamil Rytarowski m_mem_region_cache.size()); 51297206d57SZachary Turner return Status(); 513f07a9995SKamil Rytarowski } 514f07a9995SKamil Rytarowski 515f07a9995SKamil Rytarowski struct kinfo_vmentry *vm; 516f07a9995SKamil Rytarowski size_t count, i; 517f07a9995SKamil Rytarowski vm = kinfo_getvmmap(GetID(), &count); 518f07a9995SKamil Rytarowski if (vm == NULL) { 519f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 52097206d57SZachary Turner Status error; 521f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 522f07a9995SKamil Rytarowski return error; 523f07a9995SKamil Rytarowski } 524f07a9995SKamil Rytarowski for (i = 0; i < count; i++) { 525f07a9995SKamil Rytarowski MemoryRegionInfo info; 526f07a9995SKamil Rytarowski info.Clear(); 527f07a9995SKamil Rytarowski info.GetRange().SetRangeBase(vm[i].kve_start); 528f07a9995SKamil Rytarowski info.GetRange().SetRangeEnd(vm[i].kve_end); 529f07a9995SKamil Rytarowski info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 530f07a9995SKamil Rytarowski 531f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_READ) 532f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 533f07a9995SKamil Rytarowski else 534f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 535f07a9995SKamil Rytarowski 536f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_WRITE) 537f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 538f07a9995SKamil Rytarowski else 539f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 540f07a9995SKamil Rytarowski 541f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_EXECUTE) 542f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 543f07a9995SKamil Rytarowski else 544f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 545f07a9995SKamil Rytarowski 546f07a9995SKamil Rytarowski if (vm[i].kve_path[0]) 547f07a9995SKamil Rytarowski info.SetName(vm[i].kve_path); 548f07a9995SKamil Rytarowski 549f07a9995SKamil Rytarowski m_mem_region_cache.emplace_back( 550a0a44e9cSKamil Rytarowski info, FileSpec(info.GetName().GetCString())); 551f07a9995SKamil Rytarowski } 552f07a9995SKamil Rytarowski free(vm); 553f07a9995SKamil Rytarowski 554f07a9995SKamil Rytarowski if (m_mem_region_cache.empty()) { 55505097246SAdrian Prantl // No entries after attempting to read them. This shouldn't happen. Assume 55605097246SAdrian Prantl // we don't support map entries. 557f07a9995SKamil Rytarowski LLDB_LOG(log, "failed to find any vmmap entries, assuming no support " 558f07a9995SKamil Rytarowski "for memory region metadata retrieval"); 559f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 56097206d57SZachary Turner Status error; 561f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 562f07a9995SKamil Rytarowski return error; 563f07a9995SKamil Rytarowski } 564f07a9995SKamil Rytarowski LLDB_LOG(log, "read {0} memory region entries from process {1}", 565f07a9995SKamil Rytarowski m_mem_region_cache.size(), GetID()); 566f07a9995SKamil Rytarowski // We support memory retrieval, remember that. 567f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolYes; 56897206d57SZachary Turner return Status(); 569f07a9995SKamil Rytarowski } 570f07a9995SKamil Rytarowski 57197206d57SZachary Turner Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions, 572f07a9995SKamil Rytarowski lldb::addr_t &addr) { 57397206d57SZachary Turner return Status("Unimplemented"); 574f07a9995SKamil Rytarowski } 575f07a9995SKamil Rytarowski 57697206d57SZachary Turner Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) { 57797206d57SZachary Turner return Status("Unimplemented"); 578f07a9995SKamil Rytarowski } 579f07a9995SKamil Rytarowski 580f07a9995SKamil Rytarowski lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() { 581f07a9995SKamil Rytarowski // punt on this for now 582f07a9995SKamil Rytarowski return LLDB_INVALID_ADDRESS; 583f07a9995SKamil Rytarowski } 584f07a9995SKamil Rytarowski 585f07a9995SKamil Rytarowski size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); } 586f07a9995SKamil Rytarowski 58797206d57SZachary Turner Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size, 588f07a9995SKamil Rytarowski bool hardware) { 589f07a9995SKamil Rytarowski if (hardware) 59097206d57SZachary Turner return Status("NativeProcessNetBSD does not support hardware breakpoints"); 591f07a9995SKamil Rytarowski else 592f07a9995SKamil Rytarowski return SetSoftwareBreakpoint(addr, size); 593f07a9995SKamil Rytarowski } 594f07a9995SKamil Rytarowski 59597206d57SZachary Turner Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path, 596f07a9995SKamil Rytarowski FileSpec &file_spec) { 59797206d57SZachary Turner return Status("Unimplemented"); 598f07a9995SKamil Rytarowski } 599f07a9995SKamil Rytarowski 60097206d57SZachary Turner Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name, 601f07a9995SKamil Rytarowski lldb::addr_t &load_addr) { 602f07a9995SKamil Rytarowski load_addr = LLDB_INVALID_ADDRESS; 60397206d57SZachary Turner return Status(); 604f07a9995SKamil Rytarowski } 605f07a9995SKamil Rytarowski 606f07a9995SKamil Rytarowski void NativeProcessNetBSD::SigchldHandler() { 607f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 608f07a9995SKamil Rytarowski // Process all pending waitpid notifications. 609f07a9995SKamil Rytarowski int status; 610c1a6b128SPavel Labath ::pid_t wait_pid = 611c1a6b128SPavel Labath llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG); 612f07a9995SKamil Rytarowski 613f07a9995SKamil Rytarowski if (wait_pid == 0) 614f07a9995SKamil Rytarowski return; // We are done. 615f07a9995SKamil Rytarowski 616f07a9995SKamil Rytarowski if (wait_pid == -1) { 61797206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 618f07a9995SKamil Rytarowski LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error); 619f07a9995SKamil Rytarowski } 620f07a9995SKamil Rytarowski 6213508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 6223508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 6233508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 6243508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 625f07a9995SKamil Rytarowski 626f07a9995SKamil Rytarowski LLDB_LOG(log, 6273508fc8cSPavel Labath "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}", 6283508fc8cSPavel Labath GetID(), wait_pid, status, exited); 629f07a9995SKamil Rytarowski 630f07a9995SKamil Rytarowski if (exited) 6313508fc8cSPavel Labath MonitorExited(wait_pid, wait_status); 6323508fc8cSPavel Labath else { 6334bb74415SKamil Rytarowski assert(wait_status.type == WaitStatus::Stop); 6343508fc8cSPavel Labath MonitorCallback(wait_pid, wait_status.status); 6353508fc8cSPavel Labath } 636f07a9995SKamil Rytarowski } 637f07a9995SKamil Rytarowski 638269eec03SKamil Rytarowski bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { 639a5be48b3SPavel Labath for (const auto &thread : m_threads) { 640a5be48b3SPavel Labath assert(thread && "thread list should not contain NULL threads"); 641a5be48b3SPavel Labath if (thread->GetID() == thread_id) { 642269eec03SKamil Rytarowski // We have this thread. 643269eec03SKamil Rytarowski return true; 644269eec03SKamil Rytarowski } 645269eec03SKamil Rytarowski } 646269eec03SKamil Rytarowski 647269eec03SKamil Rytarowski // We don't have this thread. 648269eec03SKamil Rytarowski return false; 649269eec03SKamil Rytarowski } 650269eec03SKamil Rytarowski 651a5be48b3SPavel Labath NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { 652f07a9995SKamil Rytarowski 653f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 654f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 655f07a9995SKamil Rytarowski 656f07a9995SKamil Rytarowski assert(!HasThreadNoLock(thread_id) && 657f07a9995SKamil Rytarowski "attempted to add a thread by id that already exists"); 658f07a9995SKamil Rytarowski 659f07a9995SKamil Rytarowski // If this is the first thread, save it as the current thread 660f07a9995SKamil Rytarowski if (m_threads.empty()) 661f07a9995SKamil Rytarowski SetCurrentThreadID(thread_id); 662f07a9995SKamil Rytarowski 663a8f3ae7cSJonas Devlieghere m_threads.push_back(std::make_unique<NativeThreadNetBSD>(*this, thread_id)); 664a5be48b3SPavel Labath return static_cast<NativeThreadNetBSD &>(*m_threads.back()); 665f07a9995SKamil Rytarowski } 666f07a9995SKamil Rytarowski 66796e600fcSPavel Labath Status NativeProcessNetBSD::Attach() { 668f07a9995SKamil Rytarowski // Attach to the requested process. 669f07a9995SKamil Rytarowski // An attach will cause the thread to stop with a SIGSTOP. 67096e600fcSPavel Labath Status status = PtraceWrapper(PT_ATTACH, m_pid); 67196e600fcSPavel Labath if (status.Fail()) 67296e600fcSPavel Labath return status; 673f07a9995SKamil Rytarowski 67496e600fcSPavel Labath int wstatus; 67505097246SAdrian Prantl // Need to use WALLSIG otherwise we receive an error with errno=ECHLD At this 67605097246SAdrian Prantl // point we should have a thread stopped if waitpid succeeds. 6772819136fSMichal Gorny if ((wstatus = llvm::sys::RetryAfterSignal(-1, waitpid, 678c5d7bc86SMichal Gorny m_pid, nullptr, WALLSIG)) < 0) 67996e600fcSPavel Labath return Status(errno, eErrorTypePOSIX); 680f07a9995SKamil Rytarowski 681f07a9995SKamil Rytarowski /* Initialize threads */ 68296e600fcSPavel Labath status = ReinitializeThreads(); 68396e600fcSPavel Labath if (status.Fail()) 68496e600fcSPavel Labath return status; 685f07a9995SKamil Rytarowski 686a5be48b3SPavel Labath for (const auto &thread : m_threads) 687a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); 68836e23ecaSKamil Rytarowski 689f07a9995SKamil Rytarowski // Let our process instance know the thread has stopped. 690f07a9995SKamil Rytarowski SetState(StateType::eStateStopped); 69196e600fcSPavel Labath return Status(); 692f07a9995SKamil Rytarowski } 693f07a9995SKamil Rytarowski 69497206d57SZachary Turner Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf, 69597206d57SZachary Turner size_t size, size_t &bytes_read) { 696f07a9995SKamil Rytarowski unsigned char *dst = static_cast<unsigned char *>(buf); 697f07a9995SKamil Rytarowski struct ptrace_io_desc io; 698f07a9995SKamil Rytarowski 699f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 700f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 701f07a9995SKamil Rytarowski 702f07a9995SKamil Rytarowski bytes_read = 0; 703f07a9995SKamil Rytarowski io.piod_op = PIOD_READ_D; 704f07a9995SKamil Rytarowski io.piod_len = size; 705f07a9995SKamil Rytarowski 706f07a9995SKamil Rytarowski do { 707f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_read); 708f07a9995SKamil Rytarowski io.piod_addr = dst + bytes_read; 709f07a9995SKamil Rytarowski 71097206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 711d14a0de9SMichal Gorny if (error.Fail() || io.piod_len == 0) 712f07a9995SKamil Rytarowski return error; 713f07a9995SKamil Rytarowski 714d14a0de9SMichal Gorny bytes_read += io.piod_len; 715f07a9995SKamil Rytarowski io.piod_len = size - bytes_read; 716f07a9995SKamil Rytarowski } while (bytes_read < size); 717f07a9995SKamil Rytarowski 71897206d57SZachary Turner return Status(); 719f07a9995SKamil Rytarowski } 720f07a9995SKamil Rytarowski 72197206d57SZachary Turner Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf, 722f07a9995SKamil Rytarowski size_t size, size_t &bytes_written) { 723f07a9995SKamil Rytarowski const unsigned char *src = static_cast<const unsigned char *>(buf); 72497206d57SZachary Turner Status error; 725f07a9995SKamil Rytarowski struct ptrace_io_desc io; 726f07a9995SKamil Rytarowski 727f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 728f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 729f07a9995SKamil Rytarowski 730f07a9995SKamil Rytarowski bytes_written = 0; 731f07a9995SKamil Rytarowski io.piod_op = PIOD_WRITE_D; 732f07a9995SKamil Rytarowski io.piod_len = size; 733f07a9995SKamil Rytarowski 734f07a9995SKamil Rytarowski do { 735269eec03SKamil Rytarowski io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written)); 736f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_written); 737f07a9995SKamil Rytarowski 73897206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 739d14a0de9SMichal Gorny if (error.Fail() || io.piod_len == 0) 740f07a9995SKamil Rytarowski return error; 741f07a9995SKamil Rytarowski 742d14a0de9SMichal Gorny bytes_written += io.piod_len; 743f07a9995SKamil Rytarowski io.piod_len = size - bytes_written; 744f07a9995SKamil Rytarowski } while (bytes_written < size); 745f07a9995SKamil Rytarowski 746f07a9995SKamil Rytarowski return error; 747f07a9995SKamil Rytarowski } 748f07a9995SKamil Rytarowski 749f07a9995SKamil Rytarowski llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 750f07a9995SKamil Rytarowski NativeProcessNetBSD::GetAuxvData() const { 751f07a9995SKamil Rytarowski /* 752f07a9995SKamil Rytarowski * ELF_AUX_ENTRIES is currently restricted to kernel 753f07a9995SKamil Rytarowski * (<sys/exec_elf.h> r. 1.155 specifies 15) 754f07a9995SKamil Rytarowski * 755f07a9995SKamil Rytarowski * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this 756f07a9995SKamil Rytarowski * information isn't needed. 757f07a9995SKamil Rytarowski */ 758f07a9995SKamil Rytarowski size_t auxv_size = 100 * sizeof(AuxInfo); 759f07a9995SKamil Rytarowski 760e831bb3cSPavel Labath ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf = 761dbda2851SPavel Labath llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size); 762f07a9995SKamil Rytarowski 763269eec03SKamil Rytarowski struct ptrace_io_desc io; 764269eec03SKamil Rytarowski io.piod_op = PIOD_READ_AUXV; 765269eec03SKamil Rytarowski io.piod_offs = 0; 766e831bb3cSPavel Labath io.piod_addr = static_cast<void *>(buf.get()->getBufferStart()); 767269eec03SKamil Rytarowski io.piod_len = auxv_size; 768f07a9995SKamil Rytarowski 76997206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 770f07a9995SKamil Rytarowski 771f07a9995SKamil Rytarowski if (error.Fail()) 772f07a9995SKamil Rytarowski return std::error_code(error.GetError(), std::generic_category()); 773f07a9995SKamil Rytarowski 774f07a9995SKamil Rytarowski if (io.piod_len < 1) 775f07a9995SKamil Rytarowski return std::error_code(ECANCELED, std::generic_category()); 776f07a9995SKamil Rytarowski 777e831bb3cSPavel Labath return std::move(buf); 778f07a9995SKamil Rytarowski } 7793eef2b5eSKamil Rytarowski 78097206d57SZachary Turner Status NativeProcessNetBSD::ReinitializeThreads() { 7813eef2b5eSKamil Rytarowski // Clear old threads 7823eef2b5eSKamil Rytarowski m_threads.clear(); 7833eef2b5eSKamil Rytarowski 7843eef2b5eSKamil Rytarowski // Initialize new thread 7853eef2b5eSKamil Rytarowski struct ptrace_lwpinfo info = {}; 78697206d57SZachary Turner Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 7873eef2b5eSKamil Rytarowski if (error.Fail()) { 7883eef2b5eSKamil Rytarowski return error; 7893eef2b5eSKamil Rytarowski } 7903eef2b5eSKamil Rytarowski // Reinitialize from scratch threads and register them in process 7913eef2b5eSKamil Rytarowski while (info.pl_lwpid != 0) { 792a5be48b3SPavel Labath AddThread(info.pl_lwpid); 7933eef2b5eSKamil Rytarowski error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 7943eef2b5eSKamil Rytarowski if (error.Fail()) { 7953eef2b5eSKamil Rytarowski return error; 7963eef2b5eSKamil Rytarowski } 7973eef2b5eSKamil Rytarowski } 7983eef2b5eSKamil Rytarowski 7993eef2b5eSKamil Rytarowski return error; 8003eef2b5eSKamil Rytarowski } 801