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" 241a3d19ddSKamil Rytarowski 251a3d19ddSKamil Rytarowski // System includes - They have to be included after framework includes because 261a3d19ddSKamil Rytarowski // they define some 271a3d19ddSKamil Rytarowski // 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 static ExitType convert_pid_status_to_exit_type(int status) { 44f07a9995SKamil Rytarowski if (WIFEXITED(status)) 45f07a9995SKamil Rytarowski return ExitType::eExitTypeExit; 46f07a9995SKamil Rytarowski else if (WIFSIGNALED(status)) 47f07a9995SKamil Rytarowski return ExitType::eExitTypeSignal; 48f07a9995SKamil Rytarowski else if (WIFSTOPPED(status)) 49f07a9995SKamil Rytarowski return ExitType::eExitTypeStop; 50f07a9995SKamil Rytarowski else { 51f07a9995SKamil Rytarowski // We don't know what this is. 52f07a9995SKamil Rytarowski return ExitType::eExitTypeInvalid; 53f07a9995SKamil Rytarowski } 54f07a9995SKamil Rytarowski } 55f07a9995SKamil Rytarowski 56f07a9995SKamil Rytarowski static int convert_pid_status_to_return_code(int status) { 57f07a9995SKamil Rytarowski if (WIFEXITED(status)) 58f07a9995SKamil Rytarowski return WEXITSTATUS(status); 59f07a9995SKamil Rytarowski else if (WIFSIGNALED(status)) 60f07a9995SKamil Rytarowski return WTERMSIG(status); 61f07a9995SKamil Rytarowski else if (WIFSTOPPED(status)) 62f07a9995SKamil Rytarowski return WSTOPSIG(status); 63f07a9995SKamil Rytarowski else { 64f07a9995SKamil Rytarowski // We don't know what this is. 65f07a9995SKamil Rytarowski return ExitType::eExitTypeInvalid; 66f07a9995SKamil Rytarowski } 67f07a9995SKamil Rytarowski } 68f07a9995SKamil Rytarowski 69f07a9995SKamil Rytarowski // Simple helper function to ensure flags are enabled on the given file 70f07a9995SKamil Rytarowski // descriptor. 7197206d57SZachary Turner static Status EnsureFDFlags(int fd, int flags) { 7297206d57SZachary Turner Status error; 73f07a9995SKamil Rytarowski 74f07a9995SKamil Rytarowski int status = fcntl(fd, F_GETFL); 75f07a9995SKamil Rytarowski if (status == -1) { 76f07a9995SKamil Rytarowski error.SetErrorToErrno(); 77f07a9995SKamil Rytarowski return error; 78f07a9995SKamil Rytarowski } 79f07a9995SKamil Rytarowski 80f07a9995SKamil Rytarowski if (fcntl(fd, F_SETFL, status | flags) == -1) { 81f07a9995SKamil Rytarowski error.SetErrorToErrno(); 82f07a9995SKamil Rytarowski return error; 83f07a9995SKamil Rytarowski } 84f07a9995SKamil Rytarowski 85f07a9995SKamil Rytarowski return error; 86f07a9995SKamil Rytarowski } 87f07a9995SKamil Rytarowski 881a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 891a3d19ddSKamil Rytarowski // Public Static Methods 901a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 911a3d19ddSKamil Rytarowski 9297206d57SZachary Turner Status NativeProcessProtocol::Launch( 931a3d19ddSKamil Rytarowski ProcessLaunchInfo &launch_info, 941a3d19ddSKamil Rytarowski NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, 951a3d19ddSKamil Rytarowski NativeProcessProtocolSP &native_process_sp) { 96f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 97f07a9995SKamil Rytarowski 9897206d57SZachary Turner Status error; 99f07a9995SKamil Rytarowski 100f07a9995SKamil Rytarowski // Verify the working directory is valid if one was specified. 101f07a9995SKamil Rytarowski FileSpec working_dir{launch_info.GetWorkingDirectory()}; 102f07a9995SKamil Rytarowski if (working_dir && (!working_dir.ResolvePath() || 103f07a9995SKamil Rytarowski !llvm::sys::fs::is_directory(working_dir.GetPath()))) { 104f07a9995SKamil Rytarowski error.SetErrorStringWithFormat("No such file or directory: %s", 105f07a9995SKamil Rytarowski working_dir.GetCString()); 106f07a9995SKamil Rytarowski return error; 107f07a9995SKamil Rytarowski } 108f07a9995SKamil Rytarowski 109f07a9995SKamil Rytarowski // Create the NativeProcessNetBSD in launch mode. 110f07a9995SKamil Rytarowski native_process_sp.reset(new NativeProcessNetBSD()); 111f07a9995SKamil Rytarowski 112f07a9995SKamil Rytarowski if (!native_process_sp->RegisterNativeDelegate(native_delegate)) { 113f07a9995SKamil Rytarowski native_process_sp.reset(); 114f07a9995SKamil Rytarowski error.SetErrorStringWithFormat("failed to register the native delegate"); 115f07a9995SKamil Rytarowski return error; 116f07a9995SKamil Rytarowski } 117f07a9995SKamil Rytarowski 118f07a9995SKamil Rytarowski error = std::static_pointer_cast<NativeProcessNetBSD>(native_process_sp) 119f07a9995SKamil Rytarowski ->LaunchInferior(mainloop, launch_info); 120f07a9995SKamil Rytarowski 121f07a9995SKamil Rytarowski if (error.Fail()) { 122f07a9995SKamil Rytarowski native_process_sp.reset(); 123f07a9995SKamil Rytarowski LLDB_LOG(log, "failed to launch process: {0}", error); 124f07a9995SKamil Rytarowski return error; 125f07a9995SKamil Rytarowski } 126f07a9995SKamil Rytarowski 127f07a9995SKamil Rytarowski launch_info.SetProcessID(native_process_sp->GetID()); 128f07a9995SKamil Rytarowski 129f07a9995SKamil Rytarowski return error; 1301a3d19ddSKamil Rytarowski } 1311a3d19ddSKamil Rytarowski 13297206d57SZachary Turner Status NativeProcessProtocol::Attach( 1331a3d19ddSKamil Rytarowski lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 1341a3d19ddSKamil Rytarowski MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { 135f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 136f07a9995SKamil Rytarowski LLDB_LOG(log, "pid = {0:x}", pid); 137f07a9995SKamil Rytarowski 138f07a9995SKamil Rytarowski // Retrieve the architecture for the running process. 139f07a9995SKamil Rytarowski ArchSpec process_arch; 14097206d57SZachary Turner Status error = ResolveProcessArchitecture(pid, process_arch); 141f07a9995SKamil Rytarowski if (!error.Success()) 142f07a9995SKamil Rytarowski return error; 143f07a9995SKamil Rytarowski 144f07a9995SKamil Rytarowski std::shared_ptr<NativeProcessNetBSD> native_process_netbsd_sp( 145f07a9995SKamil Rytarowski new NativeProcessNetBSD()); 146f07a9995SKamil Rytarowski 147f07a9995SKamil Rytarowski if (!native_process_netbsd_sp->RegisterNativeDelegate(native_delegate)) { 148f07a9995SKamil Rytarowski error.SetErrorStringWithFormat("failed to register the native delegate"); 149f07a9995SKamil Rytarowski return error; 150f07a9995SKamil Rytarowski } 151f07a9995SKamil Rytarowski 152f07a9995SKamil Rytarowski native_process_netbsd_sp->AttachToInferior(mainloop, pid, error); 153f07a9995SKamil Rytarowski if (!error.Success()) 154f07a9995SKamil Rytarowski return error; 155f07a9995SKamil Rytarowski 156f07a9995SKamil Rytarowski native_process_sp = native_process_netbsd_sp; 157f07a9995SKamil Rytarowski return error; 1581a3d19ddSKamil Rytarowski } 1591a3d19ddSKamil Rytarowski 1601a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 1611a3d19ddSKamil Rytarowski // Public Instance Methods 1621a3d19ddSKamil Rytarowski // ----------------------------------------------------------------------------- 1631a3d19ddSKamil Rytarowski 1641a3d19ddSKamil Rytarowski NativeProcessNetBSD::NativeProcessNetBSD() 165f07a9995SKamil Rytarowski : NativeProcessProtocol(LLDB_INVALID_PROCESS_ID), m_arch(), 166f07a9995SKamil Rytarowski m_supports_mem_region(eLazyBoolCalculate), m_mem_region_cache() {} 167f07a9995SKamil Rytarowski 168f07a9995SKamil Rytarowski // Handles all waitpid events from the inferior process. 169f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) { 170f07a9995SKamil Rytarowski switch (signal) { 171f07a9995SKamil Rytarowski case SIGTRAP: 172f07a9995SKamil Rytarowski return MonitorSIGTRAP(pid); 173f07a9995SKamil Rytarowski case SIGSTOP: 174f07a9995SKamil Rytarowski return MonitorSIGSTOP(pid); 175f07a9995SKamil Rytarowski default: 176f07a9995SKamil Rytarowski return MonitorSignal(pid, signal); 177f07a9995SKamil Rytarowski } 178f07a9995SKamil Rytarowski } 179f07a9995SKamil Rytarowski 180f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, int signal, 181f07a9995SKamil Rytarowski int status) { 182f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 183f07a9995SKamil Rytarowski 184f07a9995SKamil Rytarowski LLDB_LOG(log, "got exit signal({0}) , pid = {1}", signal, pid); 185f07a9995SKamil Rytarowski 186f07a9995SKamil Rytarowski /* Stop Tracking All Threads attached to Process */ 187f07a9995SKamil Rytarowski m_threads.clear(); 188f07a9995SKamil Rytarowski 189f07a9995SKamil Rytarowski SetExitStatus(convert_pid_status_to_exit_type(status), 190f07a9995SKamil Rytarowski convert_pid_status_to_return_code(status), nullptr, true); 191f07a9995SKamil Rytarowski 192f07a9995SKamil Rytarowski // Notify delegate that our process has exited. 193f07a9995SKamil Rytarowski SetState(StateType::eStateExited, true); 194f07a9995SKamil Rytarowski } 195f07a9995SKamil Rytarowski 196f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) { 197f07a9995SKamil Rytarowski ptrace_siginfo_t info; 198f07a9995SKamil Rytarowski 199f07a9995SKamil Rytarowski const auto siginfo_err = 200f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 201f07a9995SKamil Rytarowski 202f07a9995SKamil Rytarowski // Get details on the signal raised. 203f07a9995SKamil Rytarowski if (siginfo_err.Success()) { 204f07a9995SKamil Rytarowski // Handle SIGSTOP from LLGS (LLDB GDB Server) 205f07a9995SKamil Rytarowski if (info.psi_siginfo.si_code == SI_USER && 206f07a9995SKamil Rytarowski info.psi_siginfo.si_pid == ::getpid()) { 207f07a9995SKamil Rytarowski /* Stop Tracking All Threads attached to Process */ 208f07a9995SKamil Rytarowski for (const auto &thread_sp : m_threads) { 209f07a9995SKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal( 210f07a9995SKamil Rytarowski SIGSTOP, &info.psi_siginfo); 211f07a9995SKamil Rytarowski } 212f07a9995SKamil Rytarowski } 213f07a9995SKamil Rytarowski } 214f07a9995SKamil Rytarowski } 215f07a9995SKamil Rytarowski 216f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { 217f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 218f07a9995SKamil Rytarowski ptrace_siginfo_t info; 219f07a9995SKamil Rytarowski 220f07a9995SKamil Rytarowski const auto siginfo_err = 221f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 222f07a9995SKamil Rytarowski 223f07a9995SKamil Rytarowski // Get details on the signal raised. 22436e23ecaSKamil Rytarowski if (siginfo_err.Fail()) { 22536e23ecaSKamil Rytarowski return; 22636e23ecaSKamil Rytarowski } 22736e23ecaSKamil Rytarowski 228f07a9995SKamil Rytarowski switch (info.psi_siginfo.si_code) { 229f07a9995SKamil Rytarowski case TRAP_BRKPT: 230f07a9995SKamil Rytarowski for (const auto &thread_sp : m_threads) { 231f07a9995SKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp) 232f07a9995SKamil Rytarowski ->SetStoppedByBreakpoint(); 233f07a9995SKamil Rytarowski FixupBreakpointPCAsNeeded( 234f07a9995SKamil Rytarowski *static_pointer_cast<NativeThreadNetBSD>(thread_sp)); 235f07a9995SKamil Rytarowski } 236f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 237f07a9995SKamil Rytarowski break; 2383eef2b5eSKamil Rytarowski case TRAP_TRACE: 2393eef2b5eSKamil Rytarowski for (const auto &thread_sp : m_threads) { 2403eef2b5eSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedByTrace(); 2413eef2b5eSKamil Rytarowski } 2423eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2433eef2b5eSKamil Rytarowski break; 2443eef2b5eSKamil Rytarowski case TRAP_EXEC: { 24597206d57SZachary Turner Status error = ReinitializeThreads(); 2463eef2b5eSKamil Rytarowski if (error.Fail()) { 2473eef2b5eSKamil Rytarowski SetState(StateType::eStateInvalid); 2483eef2b5eSKamil Rytarowski return; 2493eef2b5eSKamil Rytarowski } 2503eef2b5eSKamil Rytarowski 2513eef2b5eSKamil Rytarowski // Let our delegate know we have just exec'd. 2523eef2b5eSKamil Rytarowski NotifyDidExec(); 2533eef2b5eSKamil Rytarowski 25436e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 25536e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedByExec(); 25636e23ecaSKamil Rytarowski } 2573eef2b5eSKamil Rytarowski SetState(StateType::eStateStopped, true); 2583eef2b5eSKamil Rytarowski } break; 25936e23ecaSKamil Rytarowski case TRAP_DBREG: { 26036e23ecaSKamil Rytarowski // If a watchpoint was hit, report it 26136e23ecaSKamil Rytarowski uint32_t wp_index; 26297206d57SZachary Turner Status error = 26336e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(m_threads[info.psi_lwpid]) 26436e23ecaSKamil Rytarowski ->GetRegisterContext() 26536e23ecaSKamil Rytarowski ->GetWatchpointHitIndex(wp_index, 26636e23ecaSKamil Rytarowski (uintptr_t)info.psi_siginfo.si_addr); 26736e23ecaSKamil Rytarowski if (error.Fail()) 26836e23ecaSKamil Rytarowski LLDB_LOG(log, 26936e23ecaSKamil Rytarowski "received error while checking for watchpoint hits, pid = " 27036e23ecaSKamil Rytarowski "{0}, LWP = {1}, error = {2}", 27136e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 27236e23ecaSKamil Rytarowski if (wp_index != LLDB_INVALID_INDEX32) { 27336e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 27436e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp) 27536e23ecaSKamil Rytarowski ->SetStoppedByWatchpoint(wp_index); 276f07a9995SKamil Rytarowski } 27736e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 27836e23ecaSKamil Rytarowski break; 27936e23ecaSKamil Rytarowski } 28036e23ecaSKamil Rytarowski 28136e23ecaSKamil Rytarowski // If a breakpoint was hit, report it 28236e23ecaSKamil Rytarowski uint32_t bp_index; 28336e23ecaSKamil Rytarowski error = static_pointer_cast<NativeThreadNetBSD>(m_threads[info.psi_lwpid]) 28436e23ecaSKamil Rytarowski ->GetRegisterContext() 28536e23ecaSKamil Rytarowski ->GetHardwareBreakHitIndex(bp_index, 28636e23ecaSKamil Rytarowski (uintptr_t)info.psi_siginfo.si_addr); 28736e23ecaSKamil Rytarowski if (error.Fail()) 28836e23ecaSKamil Rytarowski LLDB_LOG(log, 28936e23ecaSKamil Rytarowski "received error while checking for hardware " 29036e23ecaSKamil Rytarowski "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", 29136e23ecaSKamil Rytarowski GetID(), info.psi_lwpid, error); 29236e23ecaSKamil Rytarowski if (bp_index != LLDB_INVALID_INDEX32) { 29336e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 29436e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp) 29536e23ecaSKamil Rytarowski ->SetStoppedByBreakpoint(); 29636e23ecaSKamil Rytarowski } 29736e23ecaSKamil Rytarowski SetState(StateType::eStateStopped, true); 29836e23ecaSKamil Rytarowski break; 29936e23ecaSKamil Rytarowski } 30036e23ecaSKamil Rytarowski } break; 301f07a9995SKamil Rytarowski } 302f07a9995SKamil Rytarowski } 303f07a9995SKamil Rytarowski 304f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { 305f07a9995SKamil Rytarowski ptrace_siginfo_t info; 306f07a9995SKamil Rytarowski const auto siginfo_err = 307f07a9995SKamil Rytarowski PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); 308f07a9995SKamil Rytarowski 309f07a9995SKamil Rytarowski for (const auto &thread_sp : m_threads) { 310f07a9995SKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal( 311f07a9995SKamil Rytarowski info.psi_siginfo.si_signo, &info.psi_siginfo); 312f07a9995SKamil Rytarowski } 313f07a9995SKamil Rytarowski SetState(StateType::eStateStopped, true); 314f07a9995SKamil Rytarowski } 315f07a9995SKamil Rytarowski 31697206d57SZachary Turner Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 317f07a9995SKamil Rytarowski int data, int *result) { 318f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 31997206d57SZachary Turner Status error; 320f07a9995SKamil Rytarowski int ret; 321f07a9995SKamil Rytarowski 322f07a9995SKamil Rytarowski errno = 0; 323f07a9995SKamil Rytarowski ret = ptrace(req, static_cast<::pid_t>(pid), addr, data); 324f07a9995SKamil Rytarowski 325f07a9995SKamil Rytarowski if (ret == -1) 326f07a9995SKamil Rytarowski error.SetErrorToErrno(); 327f07a9995SKamil Rytarowski 328f07a9995SKamil Rytarowski if (result) 329f07a9995SKamil Rytarowski *result = ret; 330f07a9995SKamil Rytarowski 331f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret); 332f07a9995SKamil Rytarowski 333f07a9995SKamil Rytarowski if (error.Fail()) 334f07a9995SKamil Rytarowski LLDB_LOG(log, "ptrace() failed: {0}", error); 335f07a9995SKamil Rytarowski 336f07a9995SKamil Rytarowski return error; 337f07a9995SKamil Rytarowski } 338f07a9995SKamil Rytarowski 33997206d57SZachary Turner Status NativeProcessNetBSD::GetSoftwareBreakpointPCOffset( 340f07a9995SKamil Rytarowski uint32_t &actual_opcode_size) { 341f07a9995SKamil Rytarowski // FIXME put this behind a breakpoint protocol class that can be 342f07a9995SKamil Rytarowski // set per architecture. Need ARM, MIPS support here. 343f07a9995SKamil Rytarowski static const uint8_t g_i386_opcode[] = {0xCC}; 344f07a9995SKamil Rytarowski switch (m_arch.GetMachine()) { 345f07a9995SKamil Rytarowski case llvm::Triple::x86_64: 346f07a9995SKamil Rytarowski actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 34797206d57SZachary Turner return Status(); 348f07a9995SKamil Rytarowski default: 349f07a9995SKamil Rytarowski assert(false && "CPU type not supported!"); 35097206d57SZachary Turner return Status("CPU type not supported"); 351f07a9995SKamil Rytarowski } 352f07a9995SKamil Rytarowski } 353f07a9995SKamil Rytarowski 35497206d57SZachary Turner Status 35597206d57SZachary Turner NativeProcessNetBSD::FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread) { 356f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 35797206d57SZachary Turner Status error; 358f07a9995SKamil Rytarowski // Find out the size of a breakpoint (might depend on where we are in the 359f07a9995SKamil Rytarowski // code). 360f07a9995SKamil Rytarowski NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 361f07a9995SKamil Rytarowski if (!context_sp) { 362f07a9995SKamil Rytarowski error.SetErrorString("cannot get a NativeRegisterContext for the thread"); 363f07a9995SKamil Rytarowski LLDB_LOG(log, "failed: {0}", error); 364f07a9995SKamil Rytarowski return error; 365f07a9995SKamil Rytarowski } 366f07a9995SKamil Rytarowski uint32_t breakpoint_size = 0; 367f07a9995SKamil Rytarowski error = GetSoftwareBreakpointPCOffset(breakpoint_size); 368f07a9995SKamil Rytarowski if (error.Fail()) { 369f07a9995SKamil Rytarowski LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 370f07a9995SKamil Rytarowski return error; 371f07a9995SKamil Rytarowski } else 372f07a9995SKamil Rytarowski LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 37336e23ecaSKamil Rytarowski // First try probing for a breakpoint at a software breakpoint location: PC 37436e23ecaSKamil Rytarowski // - breakpoint size. 375f07a9995SKamil Rytarowski const lldb::addr_t initial_pc_addr = 376f07a9995SKamil Rytarowski context_sp->GetPCfromBreakpointLocation(); 377f07a9995SKamil Rytarowski lldb::addr_t breakpoint_addr = initial_pc_addr; 378f07a9995SKamil Rytarowski if (breakpoint_size > 0) { 379f07a9995SKamil Rytarowski // Do not allow breakpoint probe to wrap around. 380f07a9995SKamil Rytarowski if (breakpoint_addr >= breakpoint_size) 381f07a9995SKamil Rytarowski breakpoint_addr -= breakpoint_size; 382f07a9995SKamil Rytarowski } 383f07a9995SKamil Rytarowski // Check if we stopped because of a breakpoint. 384f07a9995SKamil Rytarowski NativeBreakpointSP breakpoint_sp; 385f07a9995SKamil Rytarowski error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 386f07a9995SKamil Rytarowski if (!error.Success() || !breakpoint_sp) { 387f07a9995SKamil Rytarowski // We didn't find one at a software probe location. Nothing to do. 388f07a9995SKamil Rytarowski LLDB_LOG(log, 389f07a9995SKamil Rytarowski "pid {0} no lldb breakpoint found at current pc with " 390f07a9995SKamil Rytarowski "adjustment: {1}", 391f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 39297206d57SZachary Turner return Status(); 393f07a9995SKamil Rytarowski } 394f07a9995SKamil Rytarowski // If the breakpoint is not a software breakpoint, nothing to do. 395f07a9995SKamil Rytarowski if (!breakpoint_sp->IsSoftwareBreakpoint()) { 396f07a9995SKamil Rytarowski LLDB_LOG( 397f07a9995SKamil Rytarowski log, 398f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 399f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 40097206d57SZachary Turner return Status(); 401f07a9995SKamil Rytarowski } 402f07a9995SKamil Rytarowski // 403f07a9995SKamil Rytarowski // We have a software breakpoint and need to adjust the PC. 404f07a9995SKamil Rytarowski // 405f07a9995SKamil Rytarowski // Sanity check. 406f07a9995SKamil Rytarowski if (breakpoint_size == 0) { 407f07a9995SKamil Rytarowski // Nothing to do! How did we get here? 408f07a9995SKamil Rytarowski LLDB_LOG(log, 409f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, it is software, but the " 410f07a9995SKamil Rytarowski "size is zero, nothing to do (unexpected)", 411f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 41297206d57SZachary Turner return Status(); 413f07a9995SKamil Rytarowski } 414f07a9995SKamil Rytarowski // 415f07a9995SKamil Rytarowski // We have a software breakpoint and need to adjust the PC. 416f07a9995SKamil Rytarowski // 417f07a9995SKamil Rytarowski // Sanity check. 418f07a9995SKamil Rytarowski if (breakpoint_size == 0) { 419f07a9995SKamil Rytarowski // Nothing to do! How did we get here? 420f07a9995SKamil Rytarowski LLDB_LOG(log, 421f07a9995SKamil Rytarowski "pid {0} breakpoint found at {1:x}, it is software, but the " 422f07a9995SKamil Rytarowski "size is zero, nothing to do (unexpected)", 423f07a9995SKamil Rytarowski GetID(), breakpoint_addr); 42497206d57SZachary Turner return Status(); 425f07a9995SKamil Rytarowski } 426f07a9995SKamil Rytarowski // Change the program counter. 427f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 428f07a9995SKamil Rytarowski thread.GetID(), initial_pc_addr, breakpoint_addr); 429f07a9995SKamil Rytarowski error = context_sp->SetPC(breakpoint_addr); 430f07a9995SKamil Rytarowski if (error.Fail()) { 431f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 432f07a9995SKamil Rytarowski thread.GetID(), error); 433f07a9995SKamil Rytarowski return error; 434f07a9995SKamil Rytarowski } 435f07a9995SKamil Rytarowski return error; 436f07a9995SKamil Rytarowski } 437f07a9995SKamil Rytarowski 43897206d57SZachary Turner Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { 439f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 440f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 441f07a9995SKamil Rytarowski 442f07a9995SKamil Rytarowski const auto &thread_sp = m_threads[0]; 443f07a9995SKamil Rytarowski const ResumeAction *const action = 444f07a9995SKamil Rytarowski resume_actions.GetActionForThread(thread_sp->GetID(), true); 445f07a9995SKamil Rytarowski 446f07a9995SKamil Rytarowski if (action == nullptr) { 447f07a9995SKamil Rytarowski LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 448f07a9995SKamil Rytarowski thread_sp->GetID()); 44997206d57SZachary Turner return Status(); 450f07a9995SKamil Rytarowski } 451f07a9995SKamil Rytarowski 45297206d57SZachary Turner Status error; 4533eef2b5eSKamil Rytarowski 454f07a9995SKamil Rytarowski switch (action->state) { 455f07a9995SKamil Rytarowski case eStateRunning: { 456f07a9995SKamil Rytarowski // Run the thread, possibly feeding it the signal. 4573eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, 4583eef2b5eSKamil Rytarowski action->signal); 459f07a9995SKamil Rytarowski if (!error.Success()) 460f07a9995SKamil Rytarowski return error; 461f07a9995SKamil Rytarowski for (const auto &thread_sp : m_threads) { 462f07a9995SKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetRunning(); 463f07a9995SKamil Rytarowski } 464f07a9995SKamil Rytarowski SetState(eStateRunning, true); 465f07a9995SKamil Rytarowski break; 466f07a9995SKamil Rytarowski } 467f07a9995SKamil Rytarowski case eStateStepping: 4683eef2b5eSKamil Rytarowski // Run the thread, possibly feeding it the signal. 4693eef2b5eSKamil Rytarowski error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, 4703eef2b5eSKamil Rytarowski action->signal); 4713eef2b5eSKamil Rytarowski if (!error.Success()) 4723eef2b5eSKamil Rytarowski return error; 4733eef2b5eSKamil Rytarowski for (const auto &thread_sp : m_threads) { 4743eef2b5eSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStepping(); 4753eef2b5eSKamil Rytarowski } 4763eef2b5eSKamil Rytarowski SetState(eStateStepping, true); 477f07a9995SKamil Rytarowski break; 478f07a9995SKamil Rytarowski 479f07a9995SKamil Rytarowski case eStateSuspended: 480f07a9995SKamil Rytarowski case eStateStopped: 481f07a9995SKamil Rytarowski llvm_unreachable("Unexpected state"); 482f07a9995SKamil Rytarowski 483f07a9995SKamil Rytarowski default: 48497206d57SZachary Turner return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " 485f07a9995SKamil Rytarowski "for pid %" PRIu64 ", tid %" PRIu64, 486f07a9995SKamil Rytarowski __FUNCTION__, StateAsCString(action->state), GetID(), 487f07a9995SKamil Rytarowski thread_sp->GetID()); 488f07a9995SKamil Rytarowski } 489f07a9995SKamil Rytarowski 49097206d57SZachary Turner return Status(); 491f07a9995SKamil Rytarowski } 492f07a9995SKamil Rytarowski 49397206d57SZachary Turner Status NativeProcessNetBSD::Halt() { 49497206d57SZachary Turner Status error; 495f07a9995SKamil Rytarowski 496f07a9995SKamil Rytarowski if (kill(GetID(), SIGSTOP) != 0) 497f07a9995SKamil Rytarowski error.SetErrorToErrno(); 498f07a9995SKamil Rytarowski 499f07a9995SKamil Rytarowski return error; 500f07a9995SKamil Rytarowski } 501f07a9995SKamil Rytarowski 50297206d57SZachary Turner Status NativeProcessNetBSD::Detach() { 50397206d57SZachary Turner Status error; 504f07a9995SKamil Rytarowski 505f07a9995SKamil Rytarowski // Stop monitoring the inferior. 506f07a9995SKamil Rytarowski m_sigchld_handle.reset(); 507f07a9995SKamil Rytarowski 508f07a9995SKamil Rytarowski // Tell ptrace to detach from the process. 509f07a9995SKamil Rytarowski if (GetID() == LLDB_INVALID_PROCESS_ID) 510f07a9995SKamil Rytarowski return error; 511f07a9995SKamil Rytarowski 512f07a9995SKamil Rytarowski return PtraceWrapper(PT_DETACH, GetID()); 513f07a9995SKamil Rytarowski } 514f07a9995SKamil Rytarowski 51597206d57SZachary Turner Status NativeProcessNetBSD::Signal(int signo) { 51697206d57SZachary Turner Status error; 517f07a9995SKamil Rytarowski 518f07a9995SKamil Rytarowski if (kill(GetID(), signo)) 519f07a9995SKamil Rytarowski error.SetErrorToErrno(); 520f07a9995SKamil Rytarowski 521f07a9995SKamil Rytarowski return error; 522f07a9995SKamil Rytarowski } 523f07a9995SKamil Rytarowski 52497206d57SZachary Turner Status NativeProcessNetBSD::Kill() { 525f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 526f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0}", GetID()); 527f07a9995SKamil Rytarowski 52897206d57SZachary Turner Status error; 529f07a9995SKamil Rytarowski 530f07a9995SKamil Rytarowski switch (m_state) { 531f07a9995SKamil Rytarowski case StateType::eStateInvalid: 532f07a9995SKamil Rytarowski case StateType::eStateExited: 533f07a9995SKamil Rytarowski case StateType::eStateCrashed: 534f07a9995SKamil Rytarowski case StateType::eStateDetached: 535f07a9995SKamil Rytarowski case StateType::eStateUnloaded: 536f07a9995SKamil Rytarowski // Nothing to do - the process is already dead. 537f07a9995SKamil Rytarowski LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 538f07a9995SKamil Rytarowski StateAsCString(m_state)); 539f07a9995SKamil Rytarowski return error; 540f07a9995SKamil Rytarowski 541f07a9995SKamil Rytarowski case StateType::eStateConnected: 542f07a9995SKamil Rytarowski case StateType::eStateAttaching: 543f07a9995SKamil Rytarowski case StateType::eStateLaunching: 544f07a9995SKamil Rytarowski case StateType::eStateStopped: 545f07a9995SKamil Rytarowski case StateType::eStateRunning: 546f07a9995SKamil Rytarowski case StateType::eStateStepping: 547f07a9995SKamil Rytarowski case StateType::eStateSuspended: 548f07a9995SKamil Rytarowski // We can try to kill a process in these states. 549f07a9995SKamil Rytarowski break; 550f07a9995SKamil Rytarowski } 551f07a9995SKamil Rytarowski 552f07a9995SKamil Rytarowski if (kill(GetID(), SIGKILL) != 0) { 553f07a9995SKamil Rytarowski error.SetErrorToErrno(); 554f07a9995SKamil Rytarowski return error; 555f07a9995SKamil Rytarowski } 556f07a9995SKamil Rytarowski 557f07a9995SKamil Rytarowski return error; 558f07a9995SKamil Rytarowski } 559f07a9995SKamil Rytarowski 56097206d57SZachary Turner Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr, 561f07a9995SKamil Rytarowski MemoryRegionInfo &range_info) { 562f07a9995SKamil Rytarowski 563f07a9995SKamil Rytarowski if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 564f07a9995SKamil Rytarowski // We're done. 56597206d57SZachary Turner return Status("unsupported"); 566f07a9995SKamil Rytarowski } 567f07a9995SKamil Rytarowski 56897206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 569f07a9995SKamil Rytarowski if (error.Fail()) { 570f07a9995SKamil Rytarowski return error; 571f07a9995SKamil Rytarowski } 572f07a9995SKamil Rytarowski 573f07a9995SKamil Rytarowski lldb::addr_t prev_base_address = 0; 574f07a9995SKamil Rytarowski // FIXME start by finding the last region that is <= target address using 575f07a9995SKamil Rytarowski // binary search. Data is sorted. 576f07a9995SKamil Rytarowski // There can be a ton of regions on pthreads apps with lots of threads. 577f07a9995SKamil Rytarowski for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 578f07a9995SKamil Rytarowski ++it) { 579f07a9995SKamil Rytarowski MemoryRegionInfo &proc_entry_info = it->first; 580f07a9995SKamil Rytarowski // Sanity check assumption that memory map entries are ascending. 581f07a9995SKamil Rytarowski assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 582f07a9995SKamil Rytarowski "descending memory map entries detected, unexpected"); 583f07a9995SKamil Rytarowski prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 584f07a9995SKamil Rytarowski UNUSED_IF_ASSERT_DISABLED(prev_base_address); 58536e23ecaSKamil Rytarowski // If the target address comes before this entry, indicate distance to 58636e23ecaSKamil Rytarowski // next region. 587f07a9995SKamil Rytarowski if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 588f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 589f07a9995SKamil Rytarowski range_info.GetRange().SetByteSize( 590f07a9995SKamil Rytarowski proc_entry_info.GetRange().GetRangeBase() - load_addr); 591f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 592f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 593f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 594f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 595f07a9995SKamil Rytarowski return error; 596f07a9995SKamil Rytarowski } else if (proc_entry_info.GetRange().Contains(load_addr)) { 597f07a9995SKamil Rytarowski // The target address is within the memory region we're processing here. 598f07a9995SKamil Rytarowski range_info = proc_entry_info; 599f07a9995SKamil Rytarowski return error; 600f07a9995SKamil Rytarowski } 601f07a9995SKamil Rytarowski // The target memory address comes somewhere after the region we just 602f07a9995SKamil Rytarowski // parsed. 603f07a9995SKamil Rytarowski } 604f07a9995SKamil Rytarowski // If we made it here, we didn't find an entry that contained the given 605f07a9995SKamil Rytarowski // address. Return the 60636e23ecaSKamil Rytarowski // load_addr as start and the amount of bytes betwwen load address and the 60736e23ecaSKamil Rytarowski // end of the memory as size. 608f07a9995SKamil Rytarowski range_info.GetRange().SetRangeBase(load_addr); 609f07a9995SKamil Rytarowski range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 610f07a9995SKamil Rytarowski range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 611f07a9995SKamil Rytarowski range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 612f07a9995SKamil Rytarowski range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 613f07a9995SKamil Rytarowski range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 614f07a9995SKamil Rytarowski return error; 615f07a9995SKamil Rytarowski } 616f07a9995SKamil Rytarowski 61797206d57SZachary Turner Status NativeProcessNetBSD::PopulateMemoryRegionCache() { 618f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 619f07a9995SKamil Rytarowski // If our cache is empty, pull the latest. There should always be at least 620f07a9995SKamil Rytarowski // one memory region if memory region handling is supported. 621f07a9995SKamil Rytarowski if (!m_mem_region_cache.empty()) { 622f07a9995SKamil Rytarowski LLDB_LOG(log, "reusing {0} cached memory region entries", 623f07a9995SKamil Rytarowski m_mem_region_cache.size()); 62497206d57SZachary Turner return Status(); 625f07a9995SKamil Rytarowski } 626f07a9995SKamil Rytarowski 627f07a9995SKamil Rytarowski struct kinfo_vmentry *vm; 628f07a9995SKamil Rytarowski size_t count, i; 629f07a9995SKamil Rytarowski vm = kinfo_getvmmap(GetID(), &count); 630f07a9995SKamil Rytarowski if (vm == NULL) { 631f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 63297206d57SZachary Turner Status error; 633f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 634f07a9995SKamil Rytarowski return error; 635f07a9995SKamil Rytarowski } 636f07a9995SKamil Rytarowski for (i = 0; i < count; i++) { 637f07a9995SKamil Rytarowski MemoryRegionInfo info; 638f07a9995SKamil Rytarowski info.Clear(); 639f07a9995SKamil Rytarowski info.GetRange().SetRangeBase(vm[i].kve_start); 640f07a9995SKamil Rytarowski info.GetRange().SetRangeEnd(vm[i].kve_end); 641f07a9995SKamil Rytarowski info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 642f07a9995SKamil Rytarowski 643f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_READ) 644f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 645f07a9995SKamil Rytarowski else 646f07a9995SKamil Rytarowski info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 647f07a9995SKamil Rytarowski 648f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_WRITE) 649f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 650f07a9995SKamil Rytarowski else 651f07a9995SKamil Rytarowski info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 652f07a9995SKamil Rytarowski 653f07a9995SKamil Rytarowski if (vm[i].kve_protection & VM_PROT_EXECUTE) 654f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 655f07a9995SKamil Rytarowski else 656f07a9995SKamil Rytarowski info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 657f07a9995SKamil Rytarowski 658f07a9995SKamil Rytarowski if (vm[i].kve_path[0]) 659f07a9995SKamil Rytarowski info.SetName(vm[i].kve_path); 660f07a9995SKamil Rytarowski 661f07a9995SKamil Rytarowski m_mem_region_cache.emplace_back( 662f07a9995SKamil Rytarowski info, FileSpec(info.GetName().GetCString(), true)); 663f07a9995SKamil Rytarowski } 664f07a9995SKamil Rytarowski free(vm); 665f07a9995SKamil Rytarowski 666f07a9995SKamil Rytarowski if (m_mem_region_cache.empty()) { 667f07a9995SKamil Rytarowski // No entries after attempting to read them. This shouldn't happen. 668f07a9995SKamil Rytarowski // Assume we don't support map entries. 669f07a9995SKamil Rytarowski LLDB_LOG(log, "failed to find any vmmap entries, assuming no support " 670f07a9995SKamil Rytarowski "for memory region metadata retrieval"); 671f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolNo; 67297206d57SZachary Turner Status error; 673f07a9995SKamil Rytarowski error.SetErrorString("not supported"); 674f07a9995SKamil Rytarowski return error; 675f07a9995SKamil Rytarowski } 676f07a9995SKamil Rytarowski LLDB_LOG(log, "read {0} memory region entries from process {1}", 677f07a9995SKamil Rytarowski m_mem_region_cache.size(), GetID()); 678f07a9995SKamil Rytarowski // We support memory retrieval, remember that. 679f07a9995SKamil Rytarowski m_supports_mem_region = LazyBool::eLazyBoolYes; 68097206d57SZachary Turner return Status(); 681f07a9995SKamil Rytarowski } 682f07a9995SKamil Rytarowski 68397206d57SZachary Turner Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions, 684f07a9995SKamil Rytarowski lldb::addr_t &addr) { 68597206d57SZachary Turner return Status("Unimplemented"); 686f07a9995SKamil Rytarowski } 687f07a9995SKamil Rytarowski 68897206d57SZachary Turner Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) { 68997206d57SZachary Turner return Status("Unimplemented"); 690f07a9995SKamil Rytarowski } 691f07a9995SKamil Rytarowski 692f07a9995SKamil Rytarowski lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() { 693f07a9995SKamil Rytarowski // punt on this for now 694f07a9995SKamil Rytarowski return LLDB_INVALID_ADDRESS; 695f07a9995SKamil Rytarowski } 696f07a9995SKamil Rytarowski 697f07a9995SKamil Rytarowski size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); } 698f07a9995SKamil Rytarowski 699f07a9995SKamil Rytarowski bool NativeProcessNetBSD::GetArchitecture(ArchSpec &arch) const { 700f07a9995SKamil Rytarowski arch = m_arch; 701f07a9995SKamil Rytarowski return true; 702f07a9995SKamil Rytarowski } 703f07a9995SKamil Rytarowski 70497206d57SZachary Turner Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size, 705f07a9995SKamil Rytarowski bool hardware) { 706f07a9995SKamil Rytarowski if (hardware) 70797206d57SZachary Turner return Status("NativeProcessNetBSD does not support hardware breakpoints"); 708f07a9995SKamil Rytarowski else 709f07a9995SKamil Rytarowski return SetSoftwareBreakpoint(addr, size); 710f07a9995SKamil Rytarowski } 711f07a9995SKamil Rytarowski 71297206d57SZachary Turner Status NativeProcessNetBSD::GetSoftwareBreakpointTrapOpcode( 713f07a9995SKamil Rytarowski size_t trap_opcode_size_hint, size_t &actual_opcode_size, 714f07a9995SKamil Rytarowski const uint8_t *&trap_opcode_bytes) { 715f07a9995SKamil Rytarowski static const uint8_t g_i386_opcode[] = {0xCC}; 716f07a9995SKamil Rytarowski 717f07a9995SKamil Rytarowski switch (m_arch.GetMachine()) { 718f07a9995SKamil Rytarowski case llvm::Triple::x86: 719f07a9995SKamil Rytarowski case llvm::Triple::x86_64: 720f07a9995SKamil Rytarowski trap_opcode_bytes = g_i386_opcode; 721f07a9995SKamil Rytarowski actual_opcode_size = sizeof(g_i386_opcode); 72297206d57SZachary Turner return Status(); 723f07a9995SKamil Rytarowski default: 724f07a9995SKamil Rytarowski assert(false && "CPU type not supported!"); 72597206d57SZachary Turner return Status("CPU type not supported"); 726f07a9995SKamil Rytarowski } 727f07a9995SKamil Rytarowski } 728f07a9995SKamil Rytarowski 72997206d57SZachary Turner Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path, 730f07a9995SKamil Rytarowski FileSpec &file_spec) { 73197206d57SZachary Turner return Status("Unimplemented"); 732f07a9995SKamil Rytarowski } 733f07a9995SKamil Rytarowski 73497206d57SZachary Turner Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name, 735f07a9995SKamil Rytarowski lldb::addr_t &load_addr) { 736f07a9995SKamil Rytarowski load_addr = LLDB_INVALID_ADDRESS; 73797206d57SZachary Turner return Status(); 738f07a9995SKamil Rytarowski } 739f07a9995SKamil Rytarowski 74097206d57SZachary Turner Status NativeProcessNetBSD::LaunchInferior(MainLoop &mainloop, 741f07a9995SKamil Rytarowski ProcessLaunchInfo &launch_info) { 74297206d57SZachary Turner Status error; 743f07a9995SKamil Rytarowski m_sigchld_handle = mainloop.RegisterSignal( 744f07a9995SKamil Rytarowski SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 745f07a9995SKamil Rytarowski if (!m_sigchld_handle) 746f07a9995SKamil Rytarowski return error; 747f07a9995SKamil Rytarowski 748f07a9995SKamil Rytarowski SetState(eStateLaunching); 749f07a9995SKamil Rytarowski 750f07a9995SKamil Rytarowski ::pid_t pid = ProcessLauncherPosixFork() 751f07a9995SKamil Rytarowski .LaunchProcess(launch_info, error) 752f07a9995SKamil Rytarowski .GetProcessId(); 753f07a9995SKamil Rytarowski if (error.Fail()) 754f07a9995SKamil Rytarowski return error; 755f07a9995SKamil Rytarowski 756f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 757f07a9995SKamil Rytarowski 758f07a9995SKamil Rytarowski // Wait for the child process to trap on its call to execve. 759f07a9995SKamil Rytarowski ::pid_t wpid; 760f07a9995SKamil Rytarowski int status; 761f07a9995SKamil Rytarowski if ((wpid = waitpid(pid, &status, 0)) < 0) { 762f07a9995SKamil Rytarowski error.SetErrorToErrno(); 763f07a9995SKamil Rytarowski LLDB_LOG(log, "waitpid for inferior failed with %s", error); 764f07a9995SKamil Rytarowski 765f07a9995SKamil Rytarowski // Mark the inferior as invalid. 76636e23ecaSKamil Rytarowski // FIXME this could really use a new state - eStateLaunchFailure. For 76736e23ecaSKamil Rytarowski // now, using eStateInvalid. 768f07a9995SKamil Rytarowski SetState(StateType::eStateInvalid); 769f07a9995SKamil Rytarowski 770f07a9995SKamil Rytarowski return error; 771f07a9995SKamil Rytarowski } 772f07a9995SKamil Rytarowski assert(WIFSTOPPED(status) && (wpid == static_cast<::pid_t>(pid)) && 773f07a9995SKamil Rytarowski "Could not sync with inferior process."); 774f07a9995SKamil Rytarowski 775f07a9995SKamil Rytarowski LLDB_LOG(log, "inferior started, now in stopped state"); 776f07a9995SKamil Rytarowski 777f07a9995SKamil Rytarowski // Release the master terminal descriptor and pass it off to the 778f07a9995SKamil Rytarowski // NativeProcessNetBSD instance. Similarly stash the inferior pid. 779f07a9995SKamil Rytarowski m_terminal_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 780f07a9995SKamil Rytarowski m_pid = pid; 781f07a9995SKamil Rytarowski launch_info.SetProcessID(pid); 782f07a9995SKamil Rytarowski 783f07a9995SKamil Rytarowski if (m_terminal_fd != -1) { 784f07a9995SKamil Rytarowski error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 785f07a9995SKamil Rytarowski if (error.Fail()) { 786f07a9995SKamil Rytarowski LLDB_LOG(log, 787f07a9995SKamil Rytarowski "inferior EnsureFDFlags failed for ensuring terminal " 788f07a9995SKamil Rytarowski "O_NONBLOCK setting: {0}", 789f07a9995SKamil Rytarowski error); 790f07a9995SKamil Rytarowski 791f07a9995SKamil Rytarowski // Mark the inferior as invalid. 792f07a9995SKamil Rytarowski // FIXME this could really use a new state - eStateLaunchFailure. For 793f07a9995SKamil Rytarowski // now, using eStateInvalid. 794f07a9995SKamil Rytarowski SetState(StateType::eStateInvalid); 795f07a9995SKamil Rytarowski 796f07a9995SKamil Rytarowski return error; 797f07a9995SKamil Rytarowski } 798f07a9995SKamil Rytarowski } 799f07a9995SKamil Rytarowski 800f07a9995SKamil Rytarowski LLDB_LOG(log, "adding pid = {0}", pid); 801f07a9995SKamil Rytarowski 802f07a9995SKamil Rytarowski ResolveProcessArchitecture(m_pid, m_arch); 803f07a9995SKamil Rytarowski 8043eef2b5eSKamil Rytarowski error = ReinitializeThreads(); 805f07a9995SKamil Rytarowski if (error.Fail()) { 806f07a9995SKamil Rytarowski SetState(StateType::eStateInvalid); 807f07a9995SKamil Rytarowski return error; 808f07a9995SKamil Rytarowski } 809f07a9995SKamil Rytarowski 81036e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 81136e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal( 81236e23ecaSKamil Rytarowski SIGSTOP); 81336e23ecaSKamil Rytarowski } 81436e23ecaSKamil Rytarowski 815f07a9995SKamil Rytarowski /* Set process stopped */ 816f07a9995SKamil Rytarowski SetState(StateType::eStateStopped); 817f07a9995SKamil Rytarowski 818f07a9995SKamil Rytarowski if (error.Fail()) 819f07a9995SKamil Rytarowski LLDB_LOG(log, "inferior launching failed {0}", error); 820f07a9995SKamil Rytarowski return error; 821f07a9995SKamil Rytarowski } 822f07a9995SKamil Rytarowski 823f07a9995SKamil Rytarowski void NativeProcessNetBSD::AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, 82497206d57SZachary Turner Status &error) { 825f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 826f07a9995SKamil Rytarowski LLDB_LOG(log, "pid = {0:x}", pid); 827f07a9995SKamil Rytarowski 828f07a9995SKamil Rytarowski m_sigchld_handle = mainloop.RegisterSignal( 829f07a9995SKamil Rytarowski SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 830f07a9995SKamil Rytarowski if (!m_sigchld_handle) 831f07a9995SKamil Rytarowski return; 832f07a9995SKamil Rytarowski 833f07a9995SKamil Rytarowski error = ResolveProcessArchitecture(pid, m_arch); 834f07a9995SKamil Rytarowski if (!error.Success()) 835f07a9995SKamil Rytarowski return; 836f07a9995SKamil Rytarowski 837f07a9995SKamil Rytarowski // Set the architecture to the exe architecture. 838f07a9995SKamil Rytarowski LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 839f07a9995SKamil Rytarowski m_arch.GetArchitectureName()); 840f07a9995SKamil Rytarowski 841f07a9995SKamil Rytarowski m_pid = pid; 842f07a9995SKamil Rytarowski SetState(eStateAttaching); 843f07a9995SKamil Rytarowski 844f07a9995SKamil Rytarowski Attach(pid, error); 845f07a9995SKamil Rytarowski } 846f07a9995SKamil Rytarowski 847f07a9995SKamil Rytarowski void NativeProcessNetBSD::SigchldHandler() { 848f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 849f07a9995SKamil Rytarowski // Process all pending waitpid notifications. 850f07a9995SKamil Rytarowski int status; 851f07a9995SKamil Rytarowski ::pid_t wait_pid = waitpid(GetID(), &status, WALLSIG | WNOHANG); 852f07a9995SKamil Rytarowski 853f07a9995SKamil Rytarowski if (wait_pid == 0) 854f07a9995SKamil Rytarowski return; // We are done. 855f07a9995SKamil Rytarowski 856f07a9995SKamil Rytarowski if (wait_pid == -1) { 857f07a9995SKamil Rytarowski if (errno == EINTR) 858f07a9995SKamil Rytarowski return; 859f07a9995SKamil Rytarowski 86097206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 861f07a9995SKamil Rytarowski LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error); 862f07a9995SKamil Rytarowski } 863f07a9995SKamil Rytarowski 864f07a9995SKamil Rytarowski bool exited = false; 865f07a9995SKamil Rytarowski int signal = 0; 866f07a9995SKamil Rytarowski int exit_status = 0; 867f07a9995SKamil Rytarowski const char *status_cstr = nullptr; 868f07a9995SKamil Rytarowski if (WIFSTOPPED(status)) { 869f07a9995SKamil Rytarowski signal = WSTOPSIG(status); 870f07a9995SKamil Rytarowski status_cstr = "STOPPED"; 871f07a9995SKamil Rytarowski } else if (WIFEXITED(status)) { 872f07a9995SKamil Rytarowski exit_status = WEXITSTATUS(status); 873f07a9995SKamil Rytarowski status_cstr = "EXITED"; 874f07a9995SKamil Rytarowski exited = true; 875f07a9995SKamil Rytarowski } else if (WIFSIGNALED(status)) { 876f07a9995SKamil Rytarowski signal = WTERMSIG(status); 877f07a9995SKamil Rytarowski status_cstr = "SIGNALED"; 878f07a9995SKamil Rytarowski if (wait_pid == static_cast<::pid_t>(GetID())) { 879f07a9995SKamil Rytarowski exited = true; 880f07a9995SKamil Rytarowski exit_status = -1; 881f07a9995SKamil Rytarowski } 882f07a9995SKamil Rytarowski } else 883f07a9995SKamil Rytarowski status_cstr = "(\?\?\?)"; 884f07a9995SKamil Rytarowski 885f07a9995SKamil Rytarowski LLDB_LOG(log, 886f07a9995SKamil Rytarowski "waitpid ({0}, &status, _) => pid = {1}, status = {2:x} " 887f07a9995SKamil Rytarowski "({3}), signal = {4}, exit_state = {5}", 888f07a9995SKamil Rytarowski GetID(), wait_pid, status, status_cstr, signal, exit_status); 889f07a9995SKamil Rytarowski 890f07a9995SKamil Rytarowski if (exited) 891f07a9995SKamil Rytarowski MonitorExited(wait_pid, signal, exit_status); 892f07a9995SKamil Rytarowski else 893f07a9995SKamil Rytarowski MonitorCallback(wait_pid, signal); 894f07a9995SKamil Rytarowski } 895f07a9995SKamil Rytarowski 896*269eec03SKamil Rytarowski bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { 897*269eec03SKamil Rytarowski for (auto thread_sp : m_threads) { 898*269eec03SKamil Rytarowski assert(thread_sp && "thread list should not contain NULL threads"); 899*269eec03SKamil Rytarowski if (thread_sp->GetID() == thread_id) { 900*269eec03SKamil Rytarowski // We have this thread. 901*269eec03SKamil Rytarowski return true; 902*269eec03SKamil Rytarowski } 903*269eec03SKamil Rytarowski } 904*269eec03SKamil Rytarowski 905*269eec03SKamil Rytarowski // We don't have this thread. 906*269eec03SKamil Rytarowski return false; 907*269eec03SKamil Rytarowski } 908*269eec03SKamil Rytarowski 909f07a9995SKamil Rytarowski NativeThreadNetBSDSP NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { 910f07a9995SKamil Rytarowski 911f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 912f07a9995SKamil Rytarowski LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 913f07a9995SKamil Rytarowski 914f07a9995SKamil Rytarowski assert(!HasThreadNoLock(thread_id) && 915f07a9995SKamil Rytarowski "attempted to add a thread by id that already exists"); 916f07a9995SKamil Rytarowski 917f07a9995SKamil Rytarowski // If this is the first thread, save it as the current thread 918f07a9995SKamil Rytarowski if (m_threads.empty()) 919f07a9995SKamil Rytarowski SetCurrentThreadID(thread_id); 920f07a9995SKamil Rytarowski 921f07a9995SKamil Rytarowski auto thread_sp = std::make_shared<NativeThreadNetBSD>(this, thread_id); 922f07a9995SKamil Rytarowski m_threads.push_back(thread_sp); 923f07a9995SKamil Rytarowski return thread_sp; 924f07a9995SKamil Rytarowski } 925f07a9995SKamil Rytarowski 92697206d57SZachary Turner ::pid_t NativeProcessNetBSD::Attach(lldb::pid_t pid, Status &error) { 927f07a9995SKamil Rytarowski if (pid <= 1) { 928f07a9995SKamil Rytarowski error.SetErrorToGenericError(); 929f07a9995SKamil Rytarowski error.SetErrorString("Attaching to process 1 is not allowed."); 930f07a9995SKamil Rytarowski return -1; 931f07a9995SKamil Rytarowski } 932f07a9995SKamil Rytarowski 933f07a9995SKamil Rytarowski // Attach to the requested process. 934f07a9995SKamil Rytarowski // An attach will cause the thread to stop with a SIGSTOP. 935f07a9995SKamil Rytarowski error = PtraceWrapper(PT_ATTACH, pid); 936f07a9995SKamil Rytarowski if (error.Fail()) 937f07a9995SKamil Rytarowski return -1; 938f07a9995SKamil Rytarowski 939f07a9995SKamil Rytarowski int status; 940f07a9995SKamil Rytarowski // Need to use WALLSIG otherwise we receive an error with errno=ECHLD 941f07a9995SKamil Rytarowski // At this point we should have a thread stopped if waitpid succeeds. 942f07a9995SKamil Rytarowski if ((status = waitpid(pid, NULL, WALLSIG)) < 0) 943f07a9995SKamil Rytarowski return -1; 944f07a9995SKamil Rytarowski 945f07a9995SKamil Rytarowski m_pid = pid; 946f07a9995SKamil Rytarowski 947f07a9995SKamil Rytarowski /* Initialize threads */ 9483eef2b5eSKamil Rytarowski error = ReinitializeThreads(); 949f07a9995SKamil Rytarowski if (error.Fail()) { 950f07a9995SKamil Rytarowski SetState(StateType::eStateInvalid); 951f07a9995SKamil Rytarowski return -1; 952f07a9995SKamil Rytarowski } 953f07a9995SKamil Rytarowski 95436e23ecaSKamil Rytarowski for (const auto &thread_sp : m_threads) { 95536e23ecaSKamil Rytarowski static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal( 95636e23ecaSKamil Rytarowski SIGSTOP); 95736e23ecaSKamil Rytarowski } 95836e23ecaSKamil Rytarowski 959f07a9995SKamil Rytarowski // Let our process instance know the thread has stopped. 960f07a9995SKamil Rytarowski SetState(StateType::eStateStopped); 961f07a9995SKamil Rytarowski 962f07a9995SKamil Rytarowski return pid; 963f07a9995SKamil Rytarowski } 964f07a9995SKamil Rytarowski 96597206d57SZachary Turner Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf, 96697206d57SZachary Turner size_t size, size_t &bytes_read) { 967f07a9995SKamil Rytarowski unsigned char *dst = static_cast<unsigned char *>(buf); 968f07a9995SKamil Rytarowski struct ptrace_io_desc io; 969f07a9995SKamil Rytarowski 970f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 971f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 972f07a9995SKamil Rytarowski 973f07a9995SKamil Rytarowski bytes_read = 0; 974f07a9995SKamil Rytarowski io.piod_op = PIOD_READ_D; 975f07a9995SKamil Rytarowski io.piod_len = size; 976f07a9995SKamil Rytarowski 977f07a9995SKamil Rytarowski do { 978f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_read); 979f07a9995SKamil Rytarowski io.piod_addr = dst + bytes_read; 980f07a9995SKamil Rytarowski 98197206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 982f07a9995SKamil Rytarowski if (error.Fail()) 983f07a9995SKamil Rytarowski return error; 984f07a9995SKamil Rytarowski 985f07a9995SKamil Rytarowski bytes_read = io.piod_len; 986f07a9995SKamil Rytarowski io.piod_len = size - bytes_read; 987f07a9995SKamil Rytarowski } while (bytes_read < size); 988f07a9995SKamil Rytarowski 98997206d57SZachary Turner return Status(); 990f07a9995SKamil Rytarowski } 991f07a9995SKamil Rytarowski 99297206d57SZachary Turner Status NativeProcessNetBSD::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 993f07a9995SKamil Rytarowski size_t size, 994f07a9995SKamil Rytarowski size_t &bytes_read) { 99597206d57SZachary Turner Status error = ReadMemory(addr, buf, size, bytes_read); 996f07a9995SKamil Rytarowski if (error.Fail()) 997f07a9995SKamil Rytarowski return error; 998f07a9995SKamil Rytarowski return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 999f07a9995SKamil Rytarowski } 1000f07a9995SKamil Rytarowski 100197206d57SZachary Turner Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf, 1002f07a9995SKamil Rytarowski size_t size, size_t &bytes_written) { 1003f07a9995SKamil Rytarowski const unsigned char *src = static_cast<const unsigned char *>(buf); 100497206d57SZachary Turner Status error; 1005f07a9995SKamil Rytarowski struct ptrace_io_desc io; 1006f07a9995SKamil Rytarowski 1007f07a9995SKamil Rytarowski Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1008f07a9995SKamil Rytarowski LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 1009f07a9995SKamil Rytarowski 1010f07a9995SKamil Rytarowski bytes_written = 0; 1011f07a9995SKamil Rytarowski io.piod_op = PIOD_WRITE_D; 1012f07a9995SKamil Rytarowski io.piod_len = size; 1013f07a9995SKamil Rytarowski 1014f07a9995SKamil Rytarowski do { 1015*269eec03SKamil Rytarowski io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written)); 1016f07a9995SKamil Rytarowski io.piod_offs = (void *)(addr + bytes_written); 1017f07a9995SKamil Rytarowski 101897206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 1019f07a9995SKamil Rytarowski if (error.Fail()) 1020f07a9995SKamil Rytarowski return error; 1021f07a9995SKamil Rytarowski 1022f07a9995SKamil Rytarowski bytes_written = io.piod_len; 1023f07a9995SKamil Rytarowski io.piod_len = size - bytes_written; 1024f07a9995SKamil Rytarowski } while (bytes_written < size); 1025f07a9995SKamil Rytarowski 1026f07a9995SKamil Rytarowski return error; 1027f07a9995SKamil Rytarowski } 1028f07a9995SKamil Rytarowski 1029f07a9995SKamil Rytarowski llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 1030f07a9995SKamil Rytarowski NativeProcessNetBSD::GetAuxvData() const { 1031f07a9995SKamil Rytarowski /* 1032f07a9995SKamil Rytarowski * ELF_AUX_ENTRIES is currently restricted to kernel 1033f07a9995SKamil Rytarowski * (<sys/exec_elf.h> r. 1.155 specifies 15) 1034f07a9995SKamil Rytarowski * 1035f07a9995SKamil Rytarowski * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this 1036f07a9995SKamil Rytarowski * information isn't needed. 1037f07a9995SKamil Rytarowski */ 1038f07a9995SKamil Rytarowski size_t auxv_size = 100 * sizeof(AuxInfo); 1039f07a9995SKamil Rytarowski 1040f07a9995SKamil Rytarowski ErrorOr<std::unique_ptr<MemoryBuffer>> buf = 1041f07a9995SKamil Rytarowski llvm::MemoryBuffer::getNewMemBuffer(auxv_size); 1042f07a9995SKamil Rytarowski 1043*269eec03SKamil Rytarowski struct ptrace_io_desc io; 1044*269eec03SKamil Rytarowski io.piod_op = PIOD_READ_AUXV; 1045*269eec03SKamil Rytarowski io.piod_offs = 0; 1046*269eec03SKamil Rytarowski io.piod_addr = const_cast<void *>(static_cast<const void *>(buf.get()->getBufferStart())); 1047*269eec03SKamil Rytarowski io.piod_len = auxv_size; 1048f07a9995SKamil Rytarowski 104997206d57SZachary Turner Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); 1050f07a9995SKamil Rytarowski 1051f07a9995SKamil Rytarowski if (error.Fail()) 1052f07a9995SKamil Rytarowski return std::error_code(error.GetError(), std::generic_category()); 1053f07a9995SKamil Rytarowski 1054f07a9995SKamil Rytarowski if (io.piod_len < 1) 1055f07a9995SKamil Rytarowski return std::error_code(ECANCELED, std::generic_category()); 1056f07a9995SKamil Rytarowski 1057f07a9995SKamil Rytarowski return buf; 1058f07a9995SKamil Rytarowski } 10593eef2b5eSKamil Rytarowski 106097206d57SZachary Turner Status NativeProcessNetBSD::ReinitializeThreads() { 10613eef2b5eSKamil Rytarowski // Clear old threads 10623eef2b5eSKamil Rytarowski m_threads.clear(); 10633eef2b5eSKamil Rytarowski 10643eef2b5eSKamil Rytarowski // Initialize new thread 10653eef2b5eSKamil Rytarowski struct ptrace_lwpinfo info = {}; 106697206d57SZachary Turner Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 10673eef2b5eSKamil Rytarowski if (error.Fail()) { 10683eef2b5eSKamil Rytarowski return error; 10693eef2b5eSKamil Rytarowski } 10703eef2b5eSKamil Rytarowski // Reinitialize from scratch threads and register them in process 10713eef2b5eSKamil Rytarowski while (info.pl_lwpid != 0) { 10723eef2b5eSKamil Rytarowski NativeThreadNetBSDSP thread_sp = AddThread(info.pl_lwpid); 10733eef2b5eSKamil Rytarowski error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); 10743eef2b5eSKamil Rytarowski if (error.Fail()) { 10753eef2b5eSKamil Rytarowski return error; 10763eef2b5eSKamil Rytarowski } 10773eef2b5eSKamil Rytarowski } 10783eef2b5eSKamil Rytarowski 10793eef2b5eSKamil Rytarowski return error; 10803eef2b5eSKamil Rytarowski } 1081