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) 143*535f39ceSMichal Gorny : NativeProcessProtocol(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 } 198f07a9995SKamil Rytarowski } 199f07a9995SKamil Rytarowski } 200f07a9995SKamil Rytarowski 201f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { 202f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 203f07a9995SKamil Rytarowski ptrace_siginfo_t info; 204f07a9995SKamil Rytarowski 205f07a9995SKamil Rytarowski const auto siginfo_err = 206f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 207f07a9995SKamil Rytarowski 208f07a9995SKamil Rytarowski // Get details on the signal raised. 20936e23ecaSKamil Rytarowski if (siginfo_err.Fail()) { 21036e23ecaSKamil Rytarowski return; 21136e23ecaSKamil Rytarowski } 21236e23ecaSKamil Rytarowski 213f07a9995SKamil Rytarowski switch (info.psi_siginfo.si_code) { 214f07a9995SKamil Rytarowski case TRAP_BRKPT: 215a5be48b3SPavel Labath for (const auto &thread : m_threads) { 216a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); 217a5be48b3SPavel Labath FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread)); 218f07a9995SKamil Rytarowski } 219f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 220f07a9995SKamil Rytarowski break; 2213eef2b5eSKamil Rytarowski case TRAP_TRACE: 222a5be48b3SPavel Labath for (const auto &thread : m_threads) 223a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace(); 2243eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2253eef2b5eSKamil Rytarowski break; 2263eef2b5eSKamil Rytarowski case TRAP_EXEC: { 22797206d57SZachary Turner Status error = ReinitializeThreads(); 2283eef2b5eSKamil Rytarowski if (error.Fail()) { 2293eef2b5eSKamil Rytarowski SetState(StateType::eStateInvalid); 2303eef2b5eSKamil Rytarowski return; 2313eef2b5eSKamil Rytarowski } 2323eef2b5eSKamil Rytarowski 2333eef2b5eSKamil Rytarowski // Let our delegate know we have just exec'd. 2343eef2b5eSKamil Rytarowski NotifyDidExec(); 2353eef2b5eSKamil Rytarowski 236a5be48b3SPavel Labath for (const auto &thread : m_threads) 237a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec(); 2383eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2393eef2b5eSKamil Rytarowski } break; 24036e23ecaSKamil Rytarowski case TRAP_DBREG: { 24136e23ecaSKamil Rytarowski // If a watchpoint was hit, report it 24236e23ecaSKamil Rytarowski uint32_t wp_index; 243a5be48b3SPavel Labath Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid]) 244a5be48b3SPavel Labath .GetRegisterContext() 245d37349f3SPavel Labath .GetWatchpointHitIndex( 246a5be48b3SPavel Labath wp_index, (uintptr_t)info.psi_siginfo.si_addr); 24736e23ecaSKamil Rytarowski if (error.Fail()) 24836e23ecaSKamil Rytarowski LLDB_LOG(log, 24936e23ecaSKamil Rytarowski "received error while checking for watchpoint hits, pid = " 25036e23ecaSKamil Rytarowski "{0}, LWP = {1}, error = {2}", 25136e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 25236e23ecaSKamil Rytarowski if (wp_index != LLDB_INVALID_INDEX32) { 253a5be48b3SPavel Labath for (const auto &thread : m_threads) 254a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint( 255a5be48b3SPavel Labath wp_index); 25636e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 25736e23ecaSKamil Rytarowski break; 25836e23ecaSKamil Rytarowski } 25936e23ecaSKamil Rytarowski 26036e23ecaSKamil Rytarowski // If a breakpoint was hit, report it 26136e23ecaSKamil Rytarowski uint32_t bp_index; 262a5be48b3SPavel Labath error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid]) 263a5be48b3SPavel Labath .GetRegisterContext() 264d37349f3SPavel Labath .GetHardwareBreakHitIndex(bp_index, 26536e23ecaSKamil Rytarowski (uintptr_t)info.psi_siginfo.si_addr); 26636e23ecaSKamil Rytarowski if (error.Fail()) 26736e23ecaSKamil Rytarowski LLDB_LOG(log, 26836e23ecaSKamil Rytarowski "received error while checking for hardware " 26936e23ecaSKamil Rytarowski "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", 27036e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 27136e23ecaSKamil Rytarowski if (bp_index != LLDB_INVALID_INDEX32) { 272a5be48b3SPavel Labath for (const auto &thread : m_threads) 273a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); 27436e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 27536e23ecaSKamil Rytarowski break; 27636e23ecaSKamil Rytarowski } 27736e23ecaSKamil Rytarowski } break; 278f07a9995SKamil Rytarowski } 279f07a9995SKamil Rytarowski } 280f07a9995SKamil Rytarowski 281f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { 282f07a9995SKamil Rytarowski ptrace_siginfo_t info; 283f07a9995SKamil Rytarowski const auto siginfo_err = 284f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 285f07a9995SKamil Rytarowski 286a5be48b3SPavel Labath for (const auto &thread : m_threads) { 287a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( 288f07a9995SKamil Rytarowski info.psi_siginfo.si_signo, &info.psi_siginfo); 289f07a9995SKamil Rytarowski } 290f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 291f07a9995SKamil Rytarowski } 292f07a9995SKamil Rytarowski 29397206d57SZachary Turner Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 294f07a9995SKamil Rytarowski int data, int *result) { 295f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 29697206d57SZachary Turner Status error; 297f07a9995SKamil Rytarowski int ret; 298f07a9995SKamil Rytarowski 299f07a9995SKamil Rytarowski errno = 0; 300f07a9995SKamil Rytarowski ret = ptrace(req, static_cast<::pid_t>(pid), addr, data); 301f07a9995SKamil Rytarowski 302f07a9995SKamil Rytarowski if (ret == -1) 303f07a9995SKamil Rytarowski error.SetErrorToErrno(); 304f07a9995SKamil Rytarowski 305f07a9995SKamil Rytarowski if (result) 306f07a9995SKamil Rytarowski *result = ret; 307f07a9995SKamil Rytarowski 308f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret); 309f07a9995SKamil Rytarowski 310f07a9995SKamil Rytarowski if (error.Fail()) 311f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace() failed: {0}", error); 312f07a9995SKamil Rytarowski 313f07a9995SKamil Rytarowski return error; 314f07a9995SKamil Rytarowski } 315f07a9995SKamil Rytarowski 31697206d57SZachary Turner Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { 317f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 318f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 319f07a9995SKamil Rytarowski 320a5be48b3SPavel Labath const auto &thread = m_threads[0]; 321f07a9995SKamil Rytarowski const ResumeAction *const action = 322a5be48b3SPavel Labath resume_actions.GetActionForThread(thread->GetID(), true); 323f07a9995SKamil Rytarowski 324f07a9995SKamil Rytarowski if (action == nullptr) { 325f07a9995SKamil Rytarowski LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 326a5be48b3SPavel Labath thread->GetID()); 32797206d57SZachary Turner return Status(); 328f07a9995SKamil Rytarowski } 329f07a9995SKamil Rytarowski 33097206d57SZachary Turner Status error; 3313eef2b5eSKamil Rytarowski 332f07a9995SKamil Rytarowski switch (action->state) { 333f07a9995SKamil Rytarowski case eStateRunning: { 334f07a9995SKamil Rytarowski // Run the thread, possibly feeding it the signal. 3353eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, 3363eef2b5eSKamil Rytarowski action->signal); 337f07a9995SKamil Rytarowski if (!error.Success()) 338f07a9995SKamil Rytarowski return error; 339a5be48b3SPavel Labath for (const auto &thread : m_threads) 340a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetRunning(); 341f07a9995SKamil Rytarowski SetState(eStateRunning, true); 342f07a9995SKamil Rytarowski break; 343f07a9995SKamil Rytarowski } 344f07a9995SKamil Rytarowski case eStateStepping: 3453eef2b5eSKamil Rytarowski // Run the thread, possibly feeding it the signal. 3463eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, 3473eef2b5eSKamil Rytarowski action->signal); 3483eef2b5eSKamil Rytarowski if (!error.Success()) 3493eef2b5eSKamil Rytarowski return error; 350a5be48b3SPavel Labath for (const auto &thread : m_threads) 351a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStepping(); 3523eef2b5eSKamil Rytarowski SetState(eStateStepping, true); 353f07a9995SKamil Rytarowski break; 354f07a9995SKamil Rytarowski 355f07a9995SKamil Rytarowski case eStateSuspended: 356f07a9995SKamil Rytarowski case eStateStopped: 357f07a9995SKamil Rytarowski llvm_unreachable("Unexpected state"); 358f07a9995SKamil Rytarowski 359f07a9995SKamil Rytarowski default: 36097206d57SZachary Turner return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " 361f07a9995SKamil Rytarowski "for pid %" PRIu64 ", tid %" PRIu64, 362f07a9995SKamil Rytarowski __FUNCTION__, StateAsCString(action->state), GetID(), 363a5be48b3SPavel Labath thread->GetID()); 364f07a9995SKamil Rytarowski } 365f07a9995SKamil Rytarowski 36697206d57SZachary Turner return Status(); 367f07a9995SKamil Rytarowski } 368f07a9995SKamil Rytarowski 36997206d57SZachary Turner Status NativeProcessNetBSD::Halt() { 37097206d57SZachary Turner Status error; 371f07a9995SKamil Rytarowski 372f07a9995SKamil Rytarowski if (kill(GetID(), SIGSTOP) != 0) 373f07a9995SKamil Rytarowski error.SetErrorToErrno(); 374f07a9995SKamil Rytarowski 375f07a9995SKamil Rytarowski return error; 376f07a9995SKamil Rytarowski } 377f07a9995SKamil Rytarowski 37897206d57SZachary Turner Status NativeProcessNetBSD::Detach() { 37997206d57SZachary Turner Status error; 380f07a9995SKamil Rytarowski 381f07a9995SKamil Rytarowski // Stop monitoring the inferior. 382f07a9995SKamil Rytarowski m_sigchld_handle.reset(); 383f07a9995SKamil Rytarowski 384f07a9995SKamil Rytarowski // Tell ptrace to detach from the process. 385f07a9995SKamil Rytarowski if (GetID() == LLDB_INVALID_PROCESS_ID) 386f07a9995SKamil Rytarowski return error; 387f07a9995SKamil Rytarowski 388f07a9995SKamil Rytarowski return PtraceWrapper(PT_DETACH, GetID()); 389f07a9995SKamil Rytarowski } 390f07a9995SKamil Rytarowski 39197206d57SZachary Turner Status NativeProcessNetBSD::Signal(int signo) { 39297206d57SZachary Turner Status error; 393f07a9995SKamil Rytarowski 394f07a9995SKamil Rytarowski if (kill(GetID(), signo)) 395f07a9995SKamil Rytarowski error.SetErrorToErrno(); 396f07a9995SKamil Rytarowski 397f07a9995SKamil Rytarowski return error; 398f07a9995SKamil Rytarowski } 399f07a9995SKamil Rytarowski 40097206d57SZachary Turner Status NativeProcessNetBSD::Kill() { 401f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 402f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 403f07a9995SKamil Rytarowski 40497206d57SZachary Turner Status error; 405f07a9995SKamil Rytarowski 406f07a9995SKamil Rytarowski switch (m_state) { 407f07a9995SKamil Rytarowski case StateType::eStateInvalid: 408f07a9995SKamil Rytarowski case StateType::eStateExited: 409f07a9995SKamil Rytarowski case StateType::eStateCrashed: 410f07a9995SKamil Rytarowski case StateType::eStateDetached: 411f07a9995SKamil Rytarowski case StateType::eStateUnloaded: 412f07a9995SKamil Rytarowski // Nothing to do - the process is already dead. 413f07a9995SKamil Rytarowski LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 414f07a9995SKamil Rytarowski StateAsCString(m_state)); 415f07a9995SKamil Rytarowski return error; 416f07a9995SKamil Rytarowski 417f07a9995SKamil Rytarowski case StateType::eStateConnected: 418f07a9995SKamil Rytarowski case StateType::eStateAttaching: 419f07a9995SKamil Rytarowski case StateType::eStateLaunching: 420f07a9995SKamil Rytarowski case StateType::eStateStopped: 421f07a9995SKamil Rytarowski case StateType::eStateRunning: 422f07a9995SKamil Rytarowski case StateType::eStateStepping: 423f07a9995SKamil Rytarowski case StateType::eStateSuspended: 424f07a9995SKamil Rytarowski // We can try to kill a process in these states. 425f07a9995SKamil Rytarowski break; 426f07a9995SKamil Rytarowski } 427f07a9995SKamil Rytarowski 428f07a9995SKamil Rytarowski if (kill(GetID(), SIGKILL) != 0) { 429f07a9995SKamil Rytarowski error.SetErrorToErrno(); 430f07a9995SKamil Rytarowski return error; 431f07a9995SKamil Rytarowski } 432f07a9995SKamil Rytarowski 433f07a9995SKamil Rytarowski return error; 434f07a9995SKamil Rytarowski } 435f07a9995SKamil Rytarowski 43697206d57SZachary Turner Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr, 437f07a9995SKamil Rytarowski MemoryRegionInfo &range_info) { 438f07a9995SKamil Rytarowski 439f07a9995SKamil Rytarowski if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 440f07a9995SKamil Rytarowski // We're done. 44197206d57SZachary Turner return Status("unsupported"); 442f07a9995SKamil Rytarowski } 443f07a9995SKamil Rytarowski 44497206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 445f07a9995SKamil Rytarowski if (error.Fail()) { 446f07a9995SKamil Rytarowski return error; 447f07a9995SKamil Rytarowski } 448f07a9995SKamil Rytarowski 449f07a9995SKamil Rytarowski lldb::addr_t prev_base_address = 0; 450f07a9995SKamil Rytarowski // FIXME start by finding the last region that is <= target address using 451f07a9995SKamil Rytarowski // binary search. Data is sorted. 452f07a9995SKamil Rytarowski // There can be a ton of regions on pthreads apps with lots of threads. 453f07a9995SKamil Rytarowski for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 454f07a9995SKamil Rytarowski ++it) { 455f07a9995SKamil Rytarowski MemoryRegionInfo &proc_entry_info = it->first; 456f07a9995SKamil Rytarowski // Sanity check assumption that memory map entries are ascending. 457f07a9995SKamil Rytarowski assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 458f07a9995SKamil Rytarowski "descending memory map entries detected, unexpected"); 459f07a9995SKamil Rytarowski prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 460f07a9995SKamil Rytarowski UNUSED_IF_ASSERT_DISABLED(prev_base_address); 46105097246SAdrian Prantl // If the target address comes before this entry, indicate distance to next 46205097246SAdrian Prantl // region. 463f07a9995SKamil Rytarowski if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 464f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 465f07a9995SKamil Rytarowski range_info.GetRange().SetByteSize( 466f07a9995SKamil Rytarowski proc_entry_info.GetRange().GetRangeBase() - load_addr); 467f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 468f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 469f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 470f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 471f07a9995SKamil Rytarowski return error; 472f07a9995SKamil Rytarowski } else if (proc_entry_info.GetRange().Contains(load_addr)) { 473f07a9995SKamil Rytarowski // The target address is within the memory region we're processing here. 474f07a9995SKamil Rytarowski range_info = proc_entry_info; 475f07a9995SKamil Rytarowski return error; 476f07a9995SKamil Rytarowski } 477f07a9995SKamil Rytarowski // The target memory address comes somewhere after the region we just 478f07a9995SKamil Rytarowski // parsed. 479f07a9995SKamil Rytarowski } 480f07a9995SKamil Rytarowski // If we made it here, we didn't find an entry that contained the given 48105097246SAdrian Prantl // address. Return the load_addr as start and the amount of bytes betwwen 48205097246SAdrian Prantl // load address and the end of the memory as size. 483f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 484f07a9995SKamil Rytarowski range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 485f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 486f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 487f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 488f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 489f07a9995SKamil Rytarowski return error; 490f07a9995SKamil Rytarowski } 491f07a9995SKamil Rytarowski 49297206d57SZachary Turner Status NativeProcessNetBSD::PopulateMemoryRegionCache() { 493f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 494f07a9995SKamil Rytarowski // If our cache is empty, pull the latest. There should always be at least 495f07a9995SKamil Rytarowski // one memory region if memory region handling is supported. 496f07a9995SKamil Rytarowski if (!m_mem_region_cache.empty()) { 497f07a9995SKamil Rytarowski LLDB_LOG(log, "reusing {0} cached memory region entries", 498f07a9995SKamil Rytarowski m_mem_region_cache.size()); 49997206d57SZachary Turner return Status(); 500f07a9995SKamil Rytarowski } 501f07a9995SKamil Rytarowski 502f07a9995SKamil Rytarowski struct kinfo_vmentry *vm; 503f07a9995SKamil Rytarowski size_t count, i; 504f07a9995SKamil Rytarowski vm = kinfo_getvmmap(GetID(), &count); 505f07a9995SKamil Rytarowski if (vm == NULL) { 506f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 50797206d57SZachary Turner Status error; 508f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 509f07a9995SKamil Rytarowski return error; 510f07a9995SKamil Rytarowski } 511f07a9995SKamil Rytarowski for (i = 0; i < count; i++) { 512f07a9995SKamil Rytarowski MemoryRegionInfo info; 513f07a9995SKamil Rytarowski info.Clear(); 514f07a9995SKamil Rytarowski info.GetRange().SetRangeBase(vm[i].kve_start); 515f07a9995SKamil Rytarowski info.GetRange().SetRangeEnd(vm[i].kve_end); 516f07a9995SKamil Rytarowski info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 517f07a9995SKamil Rytarowski 518f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_READ) 519f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 520f07a9995SKamil Rytarowski else 521f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 522f07a9995SKamil Rytarowski 523f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_WRITE) 524f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 525f07a9995SKamil Rytarowski else 526f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 527f07a9995SKamil Rytarowski 528f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_EXECUTE) 529f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 530f07a9995SKamil Rytarowski else 531f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 532f07a9995SKamil Rytarowski 533f07a9995SKamil Rytarowski if (vm[i].kve_path[0]) 534f07a9995SKamil Rytarowski info.SetName(vm[i].kve_path); 535f07a9995SKamil Rytarowski 536f07a9995SKamil Rytarowski m_mem_region_cache.emplace_back( 537a0a44e9cSKamil Rytarowski info, FileSpec(info.GetName().GetCString())); 538f07a9995SKamil Rytarowski } 539f07a9995SKamil Rytarowski free(vm); 540f07a9995SKamil Rytarowski 541f07a9995SKamil Rytarowski if (m_mem_region_cache.empty()) { 54205097246SAdrian Prantl // No entries after attempting to read them. This shouldn't happen. Assume 54305097246SAdrian Prantl // we don't support map entries. 544f07a9995SKamil Rytarowski LLDB_LOG(log, "failed to find any vmmap entries, assuming no support " 545f07a9995SKamil Rytarowski "for memory region metadata retrieval"); 546f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 54797206d57SZachary Turner Status error; 548f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 549f07a9995SKamil Rytarowski return error; 550f07a9995SKamil Rytarowski } 551f07a9995SKamil Rytarowski LLDB_LOG(log, "read {0} memory region entries from process {1}", 552f07a9995SKamil Rytarowski m_mem_region_cache.size(), GetID()); 553f07a9995SKamil Rytarowski // We support memory retrieval, remember that. 554f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolYes; 55597206d57SZachary Turner return Status(); 556f07a9995SKamil Rytarowski } 557f07a9995SKamil Rytarowski 55897206d57SZachary Turner Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions, 559f07a9995SKamil Rytarowski lldb::addr_t &addr) { 56097206d57SZachary Turner return Status("Unimplemented"); 561f07a9995SKamil Rytarowski } 562f07a9995SKamil Rytarowski 56397206d57SZachary Turner Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) { 56497206d57SZachary Turner return Status("Unimplemented"); 565f07a9995SKamil Rytarowski } 566f07a9995SKamil Rytarowski 567f07a9995SKamil Rytarowski lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() { 568f07a9995SKamil Rytarowski // punt on this for now 569f07a9995SKamil Rytarowski return LLDB_INVALID_ADDRESS; 570f07a9995SKamil Rytarowski } 571f07a9995SKamil Rytarowski 572f07a9995SKamil Rytarowski size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); } 573f07a9995SKamil Rytarowski 57497206d57SZachary Turner Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size, 575f07a9995SKamil Rytarowski bool hardware) { 576f07a9995SKamil Rytarowski if (hardware) 57797206d57SZachary Turner return Status("NativeProcessNetBSD does not support hardware breakpoints"); 578f07a9995SKamil Rytarowski else 579f07a9995SKamil Rytarowski return SetSoftwareBreakpoint(addr, size); 580f07a9995SKamil Rytarowski } 581f07a9995SKamil Rytarowski 58297206d57SZachary Turner Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path, 583f07a9995SKamil Rytarowski FileSpec &file_spec) { 58497206d57SZachary Turner return Status("Unimplemented"); 585f07a9995SKamil Rytarowski } 586f07a9995SKamil Rytarowski 58797206d57SZachary Turner Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name, 588f07a9995SKamil Rytarowski lldb::addr_t &load_addr) { 589f07a9995SKamil Rytarowski load_addr = LLDB_INVALID_ADDRESS; 59097206d57SZachary Turner return Status(); 591f07a9995SKamil Rytarowski } 592f07a9995SKamil Rytarowski 593f07a9995SKamil Rytarowski void NativeProcessNetBSD::SigchldHandler() { 594f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 595f07a9995SKamil Rytarowski // Process all pending waitpid notifications. 596f07a9995SKamil Rytarowski int status; 597c1a6b128SPavel Labath ::pid_t wait_pid = 598c1a6b128SPavel Labath llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG); 599f07a9995SKamil Rytarowski 600f07a9995SKamil Rytarowski if (wait_pid == 0) 601f07a9995SKamil Rytarowski return; // We are done. 602f07a9995SKamil Rytarowski 603f07a9995SKamil Rytarowski if (wait_pid == -1) { 60497206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 605f07a9995SKamil Rytarowski LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error); 606f07a9995SKamil Rytarowski } 607f07a9995SKamil Rytarowski 6083508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 6093508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 6103508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 6113508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 612f07a9995SKamil Rytarowski 613f07a9995SKamil Rytarowski LLDB_LOG(log, 6143508fc8cSPavel Labath "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}", 6153508fc8cSPavel Labath GetID(), wait_pid, status, exited); 616f07a9995SKamil Rytarowski 617f07a9995SKamil Rytarowski if (exited) 6183508fc8cSPavel Labath MonitorExited(wait_pid, wait_status); 6193508fc8cSPavel Labath else { 6204bb74415SKamil Rytarowski assert(wait_status.type == WaitStatus::Stop); 6213508fc8cSPavel Labath MonitorCallback(wait_pid, wait_status.status); 6223508fc8cSPavel Labath } 623f07a9995SKamil Rytarowski } 624f07a9995SKamil Rytarowski 625269eec03SKamil Rytarowski bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { 626a5be48b3SPavel Labath for (const auto &thread : m_threads) { 627a5be48b3SPavel Labath assert(thread && "thread list should not contain NULL threads"); 628a5be48b3SPavel Labath if (thread->GetID() == thread_id) { 629269eec03SKamil Rytarowski // We have this thread. 630269eec03SKamil Rytarowski return true; 631269eec03SKamil Rytarowski } 632269eec03SKamil Rytarowski } 633269eec03SKamil Rytarowski 634269eec03SKamil Rytarowski // We don't have this thread. 635269eec03SKamil Rytarowski return false; 636269eec03SKamil Rytarowski } 637269eec03SKamil Rytarowski 638a5be48b3SPavel Labath NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { 639f07a9995SKamil Rytarowski 640f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 641f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 642f07a9995SKamil Rytarowski 643f07a9995SKamil Rytarowski assert(!HasThreadNoLock(thread_id) && 644f07a9995SKamil Rytarowski "attempted to add a thread by id that already exists"); 645f07a9995SKamil Rytarowski 646f07a9995SKamil Rytarowski // If this is the first thread, save it as the current thread 647f07a9995SKamil Rytarowski if (m_threads.empty()) 648f07a9995SKamil Rytarowski SetCurrentThreadID(thread_id); 649f07a9995SKamil Rytarowski 650a5be48b3SPavel Labath m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id)); 651a5be48b3SPavel Labath return static_cast<NativeThreadNetBSD &>(*m_threads.back()); 652f07a9995SKamil Rytarowski } 653f07a9995SKamil Rytarowski 65496e600fcSPavel Labath Status NativeProcessNetBSD::Attach() { 655f07a9995SKamil Rytarowski // Attach to the requested process. 656f07a9995SKamil Rytarowski // An attach will cause the thread to stop with a SIGSTOP. 65796e600fcSPavel Labath Status status = PtraceWrapper(PT_ATTACH, m_pid); 65896e600fcSPavel Labath if (status.Fail()) 65996e600fcSPavel Labath return status; 660f07a9995SKamil Rytarowski 66196e600fcSPavel Labath int wstatus; 66205097246SAdrian Prantl // Need to use WALLSIG otherwise we receive an error with errno=ECHLD At this 66305097246SAdrian Prantl // point we should have a thread stopped if waitpid succeeds. 6642819136fSMichal Gorny if ((wstatus = llvm::sys::RetryAfterSignal(-1, waitpid, 665c5d7bc86SMichal Gorny m_pid, nullptr, WALLSIG)) < 0) 66696e600fcSPavel Labath return Status(errno, eErrorTypePOSIX); 667f07a9995SKamil Rytarowski 668f07a9995SKamil Rytarowski /* Initialize threads */ 66996e600fcSPavel Labath status = ReinitializeThreads(); 67096e600fcSPavel Labath if (status.Fail()) 67196e600fcSPavel Labath return status; 672f07a9995SKamil Rytarowski 673a5be48b3SPavel Labath for (const auto &thread : m_threads) 674a5be48b3SPavel Labath static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); 67536e23ecaSKamil Rytarowski 676f07a9995SKamil Rytarowski // Let our process instance know the thread has stopped. 677f07a9995SKamil Rytarowski SetState(StateType::eStateStopped); 67896e600fcSPavel Labath return Status(); 679f07a9995SKamil Rytarowski } 680f07a9995SKamil Rytarowski 68197206d57SZachary Turner Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf, 68297206d57SZachary Turner size_t size, size_t &bytes_read) { 683f07a9995SKamil Rytarowski unsigned char *dst = static_cast<unsigned char *>(buf); 684f07a9995SKamil Rytarowski struct ptrace_io_desc io; 685f07a9995SKamil Rytarowski 686f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 687f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 688f07a9995SKamil Rytarowski 689f07a9995SKamil Rytarowski bytes_read = 0; 690f07a9995SKamil Rytarowski io.piod_op = PIOD_READ_D; 691f07a9995SKamil Rytarowski io.piod_len = size; 692f07a9995SKamil Rytarowski 693f07a9995SKamil Rytarowski do { 694f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_read); 695f07a9995SKamil Rytarowski io.piod_addr = dst + bytes_read; 696f07a9995SKamil Rytarowski 69797206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 698d14a0de9SMichal Gorny if (error.Fail() || io.piod_len == 0) 699f07a9995SKamil Rytarowski return error; 700f07a9995SKamil Rytarowski 701d14a0de9SMichal Gorny bytes_read += io.piod_len; 702f07a9995SKamil Rytarowski io.piod_len = size - bytes_read; 703f07a9995SKamil Rytarowski } while (bytes_read < size); 704f07a9995SKamil Rytarowski 70597206d57SZachary Turner return Status(); 706f07a9995SKamil Rytarowski } 707f07a9995SKamil Rytarowski 70897206d57SZachary Turner Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf, 709f07a9995SKamil Rytarowski size_t size, size_t &bytes_written) { 710f07a9995SKamil Rytarowski const unsigned char *src = static_cast<const unsigned char *>(buf); 71197206d57SZachary Turner Status error; 712f07a9995SKamil Rytarowski struct ptrace_io_desc io; 713f07a9995SKamil Rytarowski 714f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 715f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 716f07a9995SKamil Rytarowski 717f07a9995SKamil Rytarowski bytes_written = 0; 718f07a9995SKamil Rytarowski io.piod_op = PIOD_WRITE_D; 719f07a9995SKamil Rytarowski io.piod_len = size; 720f07a9995SKamil Rytarowski 721f07a9995SKamil Rytarowski do { 722269eec03SKamil Rytarowski io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written)); 723f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_written); 724f07a9995SKamil Rytarowski 72597206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 726d14a0de9SMichal Gorny if (error.Fail() || io.piod_len == 0) 727f07a9995SKamil Rytarowski return error; 728f07a9995SKamil Rytarowski 729d14a0de9SMichal Gorny bytes_written += io.piod_len; 730f07a9995SKamil Rytarowski io.piod_len = size - bytes_written; 731f07a9995SKamil Rytarowski } while (bytes_written < size); 732f07a9995SKamil Rytarowski 733f07a9995SKamil Rytarowski return error; 734f07a9995SKamil Rytarowski } 735f07a9995SKamil Rytarowski 736f07a9995SKamil Rytarowski llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 737f07a9995SKamil Rytarowski NativeProcessNetBSD::GetAuxvData() const { 738f07a9995SKamil Rytarowski /* 739f07a9995SKamil Rytarowski * ELF_AUX_ENTRIES is currently restricted to kernel 740f07a9995SKamil Rytarowski * (<sys/exec_elf.h> r. 1.155 specifies 15) 741f07a9995SKamil Rytarowski * 742f07a9995SKamil Rytarowski * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this 743f07a9995SKamil Rytarowski * information isn't needed. 744f07a9995SKamil Rytarowski */ 745f07a9995SKamil Rytarowski size_t auxv_size = 100 * sizeof(AuxInfo); 746f07a9995SKamil Rytarowski 747e831bb3cSPavel Labath ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf = 748dbda2851SPavel Labath llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size); 749f07a9995SKamil Rytarowski 750269eec03SKamil Rytarowski struct ptrace_io_desc io; 751269eec03SKamil Rytarowski io.piod_op = PIOD_READ_AUXV; 752269eec03SKamil Rytarowski io.piod_offs = 0; 753e831bb3cSPavel Labath io.piod_addr = static_cast<void *>(buf.get()->getBufferStart()); 754269eec03SKamil Rytarowski io.piod_len = auxv_size; 755f07a9995SKamil Rytarowski 75697206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 757f07a9995SKamil Rytarowski 758f07a9995SKamil Rytarowski if (error.Fail()) 759f07a9995SKamil Rytarowski return std::error_code(error.GetError(), std::generic_category()); 760f07a9995SKamil Rytarowski 761f07a9995SKamil Rytarowski if (io.piod_len < 1) 762f07a9995SKamil Rytarowski return std::error_code(ECANCELED, std::generic_category()); 763f07a9995SKamil Rytarowski 764e831bb3cSPavel Labath return std::move(buf); 765f07a9995SKamil Rytarowski } 7663eef2b5eSKamil Rytarowski 76797206d57SZachary Turner Status NativeProcessNetBSD::ReinitializeThreads() { 7683eef2b5eSKamil Rytarowski // Clear old threads 7693eef2b5eSKamil Rytarowski m_threads.clear(); 7703eef2b5eSKamil Rytarowski 7713eef2b5eSKamil Rytarowski // Initialize new thread 7723eef2b5eSKamil Rytarowski struct ptrace_lwpinfo info = {}; 77397206d57SZachary Turner Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 7743eef2b5eSKamil Rytarowski if (error.Fail()) { 7753eef2b5eSKamil Rytarowski return error; 7763eef2b5eSKamil Rytarowski } 7773eef2b5eSKamil Rytarowski // Reinitialize from scratch threads and register them in process 7783eef2b5eSKamil Rytarowski while (info.pl_lwpid != 0) { 779a5be48b3SPavel Labath AddThread(info.pl_lwpid); 7803eef2b5eSKamil Rytarowski error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 7813eef2b5eSKamil Rytarowski if (error.Fail()) { 7823eef2b5eSKamil Rytarowski return error; 7833eef2b5eSKamil Rytarowski } 7843eef2b5eSKamil Rytarowski } 7853eef2b5eSKamil Rytarowski 7863eef2b5eSKamil Rytarowski return error; 7873eef2b5eSKamil Rytarowski } 788