diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2020-01-17 20:45:01 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2020-01-17 20:45:01 +0000 |
| commit | 706b4fc47bbc608932d3b491ae19a3b9cde9497b (patch) | |
| tree | 4adf86a776049cbf7f69a1929c4babcbbef925eb /lldb/source/Plugins/Process/NetBSD | |
| parent | 7cc9cf2bf09f069cb2dd947ead05d0b54301fb71 (diff) | |
Notes
Diffstat (limited to 'lldb/source/Plugins/Process/NetBSD')
8 files changed, 479 insertions, 130 deletions
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp index 8b6f9fbc33c3..4313d27e11e9 100644 --- a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp +++ b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp @@ -8,8 +8,7 @@ #include "NativeProcessNetBSD.h" - - +#include "Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h" #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" #include "lldb/Host/HostProcess.h" #include "lldb/Host/common/NativeRegisterContext.h" @@ -99,6 +98,17 @@ NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info, pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, Info.GetArchitecture(), mainloop)); + // Enable event reporting + ptrace_event_t events; + status = PtraceWrapper(PT_GET_EVENT_MASK, pid, &events, sizeof(events)); + if (status.Fail()) + return status.ToError(); + // TODO: PTRACE_FORK | PTRACE_VFORK | PTRACE_POSIX_SPAWN? + events.pe_set_event |= PTRACE_LWP_CREATE | PTRACE_LWP_EXIT; + status = PtraceWrapper(PT_SET_EVENT_MASK, pid, &events, sizeof(events)); + if (status.Fail()) + return status.ToError(); + status = process_up->ReinitializeThreads(); if (status.Fail()) return status.ToError(); @@ -211,17 +221,32 @@ void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { return; } + NativeThreadNetBSD* thread = nullptr; + if (info.psi_lwpid > 0) { + for (const auto &t : m_threads) { + if (t->GetID() == static_cast<lldb::tid_t>(info.psi_lwpid)) { + thread = static_cast<NativeThreadNetBSD *>(t.get()); + break; + } + static_cast<NativeThreadNetBSD *>(t.get())->SetStoppedWithNoReason(); + } + if (!thread) + LLDB_LOG(log, + "thread not found in m_threads, pid = {0}, LWP = {1}", pid, + info.psi_lwpid); + } + switch (info.psi_siginfo.si_code) { case TRAP_BRKPT: - for (const auto &thread : m_threads) { - static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); - FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread)); + if (thread) { + thread->SetStoppedByBreakpoint(); + FixupBreakpointPCAsNeeded(*thread); } SetState(StateType::eStateStopped, true); break; case TRAP_TRACE: - for (const auto &thread : m_threads) - static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace(); + if (thread) + thread->SetStoppedByTrace(); SetState(StateType::eStateStopped, true); break; case TRAP_EXEC: { @@ -238,54 +263,66 @@ void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec(); SetState(StateType::eStateStopped, true); } break; - case TRAP_DBREG: { - // Find the thread. - NativeThreadNetBSD* thread = nullptr; - for (const auto &t : m_threads) { - if (t->GetID() == info.psi_lwpid) { - thread = static_cast<NativeThreadNetBSD *>(t.get()); + case TRAP_LWP: { + ptrace_state_t pst; + Status error = PtraceWrapper(PT_GET_PROCESS_STATE, pid, &pst, sizeof(pst)); + if (error.Fail()) { + SetState(StateType::eStateInvalid); + return; + } + + switch (pst.pe_report_event) { + case PTRACE_LWP_CREATE: { + LLDB_LOG(log, + "monitoring new thread, pid = {0}, LWP = {1}", pid, + pst.pe_lwp); + NativeThreadNetBSD& t = AddThread(pst.pe_lwp); + error = t.CopyWatchpointsFrom( + static_cast<NativeThreadNetBSD &>(*GetCurrentThread())); + if (error.Fail()) { + LLDB_LOG(log, + "failed to copy watchpoints to new thread {0}: {1}", + pst.pe_lwp, error); + SetState(StateType::eStateInvalid); + return; + } + } break; + case PTRACE_LWP_EXIT: + LLDB_LOG(log, + "removing exited thread, pid = {0}, LWP = {1}", pid, + pst.pe_lwp); + RemoveThread(pst.pe_lwp); break; - } } - if (!thread) { - LLDB_LOG(log, - "thread not found in m_threads, pid = {0}, LWP = {1}", - GetID(), info.psi_lwpid); - break; + + error = PtraceWrapper(PT_CONTINUE, pid, reinterpret_cast<void*>(1), 0); + if (error.Fail()) { + SetState(StateType::eStateInvalid); + return; } + } break; + case TRAP_DBREG: { + if (!thread) + break; - // If a watchpoint was hit, report it + auto ®ctx = static_cast<NativeRegisterContextNetBSD &>( + thread->GetRegisterContext()); uint32_t wp_index = LLDB_INVALID_INDEX32; - Status error = thread->GetRegisterContext().GetWatchpointHitIndex( - wp_index, (uintptr_t)info.psi_siginfo.si_addr); + Status error = regctx.GetWatchpointHitIndex(wp_index, + (uintptr_t)info.psi_siginfo.si_addr); if (error.Fail()) LLDB_LOG(log, "received error while checking for watchpoint hits, pid = " - "{0}, LWP = {1}, error = {2}", - GetID(), info.psi_lwpid, error); + "{0}, LWP = {1}, error = {2}", pid, info.psi_lwpid, error); if (wp_index != LLDB_INVALID_INDEX32) { - for (const auto &thread : m_threads) - static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint( - wp_index); + thread->SetStoppedByWatchpoint(wp_index); + regctx.ClearWatchpointHit(wp_index); SetState(StateType::eStateStopped, true); break; } - // If a breakpoint was hit, report it - uint32_t bp_index = LLDB_INVALID_INDEX32; - error = thread->GetRegisterContext().GetHardwareBreakHitIndex( - bp_index, (uintptr_t)info.psi_siginfo.si_addr); - if (error.Fail()) - LLDB_LOG(log, - "received error while checking for hardware " - "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", - GetID(), info.psi_lwpid, error); - if (bp_index != LLDB_INVALID_INDEX32) { - for (const auto &thread : m_threads) - static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); - SetState(StateType::eStateStopped, true); - break; - } + thread->SetStoppedByTrace(); + SetState(StateType::eStateStopped, true); } break; } } @@ -295,9 +332,14 @@ void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { const auto siginfo_err = PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); - for (const auto &thread : m_threads) { - static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( - info.psi_siginfo.si_signo, &info.psi_siginfo); + for (const auto &abs_thread : m_threads) { + NativeThreadNetBSD &thread = static_cast<NativeThreadNetBSD &>(*abs_thread); + assert(info.psi_lwpid >= 0); + if (info.psi_lwpid == 0 || + static_cast<lldb::tid_t>(info.psi_lwpid) == thread.GetID()) + thread.SetStoppedBySignal(info.psi_siginfo.si_signo, &info.psi_siginfo); + else + thread.SetStoppedWithNoReason(); } SetState(StateType::eStateStopped, true); } @@ -325,68 +367,137 @@ Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, return error; } +static llvm::Expected<ptrace_siginfo_t> ComputeSignalInfo( + const std::vector<std::unique_ptr<NativeThreadProtocol>> &threads, + const ResumeActionList &resume_actions) { + // We need to account for three possible scenarios: + // 1. no signal being sent. + // 2. a signal being sent to one thread. + // 3. a signal being sent to the whole process. + + // Count signaled threads. While at it, determine which signal is being sent + // and ensure there's only one. + size_t signaled_threads = 0; + int signal = LLDB_INVALID_SIGNAL_NUMBER; + lldb::tid_t signaled_lwp; + for (const auto &thread : threads) { + assert(thread && "thread list should not contain NULL threads"); + const ResumeAction *action = + resume_actions.GetActionForThread(thread->GetID(), true); + if (action) { + if (action->signal != LLDB_INVALID_SIGNAL_NUMBER) { + signaled_threads++; + if (action->signal != signal) { + if (signal != LLDB_INVALID_SIGNAL_NUMBER) + return Status("NetBSD does not support passing multiple signals " + "simultaneously") + .ToError(); + signal = action->signal; + signaled_lwp = thread->GetID(); + } + } + } + } + + if (signaled_threads == 0) { + ptrace_siginfo_t siginfo; + siginfo.psi_siginfo.si_signo = LLDB_INVALID_SIGNAL_NUMBER; + return siginfo; + } + + if (signaled_threads > 1 && signaled_threads < threads.size()) + return Status("NetBSD does not support passing signal to 1<i<all threads") + .ToError(); + + ptrace_siginfo_t siginfo; + siginfo.psi_siginfo.si_signo = signal; + siginfo.psi_siginfo.si_code = SI_USER; + siginfo.psi_siginfo.si_pid = getpid(); + siginfo.psi_siginfo.si_uid = getuid(); + if (signaled_threads == 1) + siginfo.psi_lwpid = signaled_lwp; + else // signal for the whole process + siginfo.psi_lwpid = 0; + return siginfo; +} + Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); LLDB_LOG(log, "pid {0}", GetID()); - const auto &thread = m_threads[0]; - const ResumeAction *const action = - resume_actions.GetActionForThread(thread->GetID(), true); + Status ret; - if (action == nullptr) { - LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), - thread->GetID()); - return Status(); - } + Expected<ptrace_siginfo_t> siginfo = + ComputeSignalInfo(m_threads, resume_actions); + if (!siginfo) + return Status(siginfo.takeError()); - Status error; - int signal = - action->signal != LLDB_INVALID_SIGNAL_NUMBER ? action->signal : 0; + for (const auto &abs_thread : m_threads) { + assert(abs_thread && "thread list should not contain NULL threads"); + NativeThreadNetBSD &thread = static_cast<NativeThreadNetBSD &>(*abs_thread); - switch (action->state) { - case eStateRunning: { - // Run the thread, possibly feeding it the signal. - error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, - signal); - if (!error.Success()) - return error; - for (const auto &thread : m_threads) - static_cast<NativeThreadNetBSD &>(*thread).SetRunning(); - SetState(eStateRunning, true); - break; - } - case eStateStepping: - // Run the thread, possibly feeding it the signal. - error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, - signal); - if (!error.Success()) - return error; - for (const auto &thread : m_threads) - static_cast<NativeThreadNetBSD &>(*thread).SetStepping(); - SetState(eStateStepping, true); - break; + const ResumeAction *action = + resume_actions.GetActionForThread(thread.GetID(), true); + // we need to explicit issue suspend requests, so it is simpler to map it + // into proper action + ResumeAction suspend_action{thread.GetID(), eStateSuspended, + LLDB_INVALID_SIGNAL_NUMBER}; - case eStateSuspended: - case eStateStopped: - llvm_unreachable("Unexpected state"); + if (action == nullptr) { + LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), + thread.GetID()); + action = &suspend_action; + } - default: - return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " - "for pid %" PRIu64 ", tid %" PRIu64, - __FUNCTION__, StateAsCString(action->state), GetID(), - thread->GetID()); + LLDB_LOG( + log, + "processing resume action state {0} signal {1} for pid {2} tid {3}", + action->state, action->signal, GetID(), thread.GetID()); + + switch (action->state) { + case eStateRunning: + ret = thread.Resume(); + break; + case eStateStepping: + ret = thread.SingleStep(); + break; + case eStateSuspended: + case eStateStopped: + if (action->signal != LLDB_INVALID_SIGNAL_NUMBER) + return Status("Passing signal to suspended thread unsupported"); + + ret = thread.Suspend(); + break; + + default: + return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " + "for pid %" PRIu64 ", tid %" PRIu64, + __FUNCTION__, StateAsCString(action->state), GetID(), + thread.GetID()); + } + + if (!ret.Success()) + return ret; } - return Status(); + int signal = 0; + if (siginfo->psi_siginfo.si_signo != LLDB_INVALID_SIGNAL_NUMBER) { + ret = PtraceWrapper(PT_SET_SIGINFO, GetID(), &siginfo.get(), + sizeof(*siginfo)); + if (!ret.Success()) + return ret; + signal = siginfo->psi_siginfo.si_signo; + } + + ret = PtraceWrapper(PT_CONTINUE, GetID(), reinterpret_cast<void *>(1), + signal); + if (ret.Success()) + SetState(eStateRunning, true); + return ret; } Status NativeProcessNetBSD::Halt() { - Status error; - - if (kill(GetID(), SIGSTOP) != 0) - error.SetErrorToErrno(); - - return error; + return PtraceWrapper(PT_STOP, GetID()); } Status NativeProcessNetBSD::Detach() { @@ -411,6 +522,10 @@ Status NativeProcessNetBSD::Signal(int signo) { return error; } +Status NativeProcessNetBSD::Interrupt() { + return PtraceWrapper(PT_STOP, GetID()); +} + Status NativeProcessNetBSD::Kill() { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); LLDB_LOG(log, "pid {0}", GetID()); @@ -650,10 +765,10 @@ bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { } NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { - Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); + assert(thread_id > 0); assert(!HasThreadNoLock(thread_id) && "attempted to add a thread by id that already exists"); @@ -665,6 +780,22 @@ NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { return static_cast<NativeThreadNetBSD &>(*m_threads.back()); } +void NativeProcessNetBSD::RemoveThread(lldb::tid_t thread_id) { + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); + LLDB_LOG(log, "pid {0} removing thread with tid {1}", GetID(), thread_id); + + assert(thread_id > 0); + assert(HasThreadNoLock(thread_id) && + "attempted to remove a thread that does not exist"); + + for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { + if ((*it)->GetID() == thread_id) { + m_threads.erase(it); + break; + } + } +} + Status NativeProcessNetBSD::Attach() { // Attach to the requested process. // An attach will cause the thread to stop with a SIGSTOP. @@ -783,15 +914,23 @@ Status NativeProcessNetBSD::ReinitializeThreads() { m_threads.clear(); // Initialize new thread +#ifdef PT_LWPSTATUS + struct ptrace_lwpstatus info = {}; + int op = PT_LWPNEXT; +#else struct ptrace_lwpinfo info = {}; - Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); + int op = PT_LWPINFO; +#endif + + Status error = PtraceWrapper(op, GetID(), &info, sizeof(info)); + if (error.Fail()) { return error; } // Reinitialize from scratch threads and register them in process while (info.pl_lwpid != 0) { AddThread(info.pl_lwpid); - error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); + error = PtraceWrapper(op, GetID(), &info, sizeof(info)); if (error.Fail()) { return error; } diff --git a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h index 4e7f0a1c13ab..6a06773f40a8 100644 --- a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h +++ b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h @@ -47,6 +47,8 @@ public: Status Signal(int signo) override; + Status Interrupt() override; + Status Kill() override; Status GetMemoryRegionInfo(lldb::addr_t load_addr, @@ -98,6 +100,7 @@ private: bool HasThreadNoLock(lldb::tid_t thread_id); NativeThreadNetBSD &AddThread(lldb::tid_t thread_id); + void RemoveThread(lldb::tid_t thread_id); void MonitorCallback(lldb::pid_t pid, int signal); void MonitorExited(lldb::pid_t pid, WaitStatus status); diff --git a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp index 3a9caaad74c8..a8afa0b20305 100644 --- a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp +++ b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp @@ -8,6 +8,8 @@ #include "NativeRegisterContextNetBSD.h" +#include "Plugins/Process/NetBSD/NativeProcessNetBSD.h" + #include "lldb/Host/common/NativeProcessProtocol.h" using namespace lldb_private; diff --git a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h index f5dd0c33b677..13e023d856d2 100644 --- a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h +++ b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h @@ -11,12 +11,13 @@ #include "lldb/Host/common/NativeThreadProtocol.h" -#include "Plugins/Process/NetBSD/NativeProcessNetBSD.h" #include "Plugins/Process/Utility/NativeRegisterContextRegisterInfo.h" namespace lldb_private { namespace process_netbsd { +class NativeProcessNetBSD; + class NativeRegisterContextNetBSD : public NativeRegisterContextRegisterInfo { public: NativeRegisterContextNetBSD(NativeThreadProtocol &native_thread, @@ -30,6 +31,10 @@ public: static NativeRegisterContextNetBSD * CreateHostNativeRegisterContextNetBSD(const ArchSpec &target_arch, NativeThreadProtocol &native_thread); + virtual Status + CopyHardwareWatchpointsFrom(NativeRegisterContextNetBSD &source) = 0; + + virtual Status ClearWatchpointHit(uint32_t wp_index) = 0; protected: Status DoRegisterSet(int req, void *buf); diff --git a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.cpp b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.cpp index 6cc2810fa235..05a35401da46 100644 --- a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.cpp +++ b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.cpp @@ -84,13 +84,71 @@ static_assert((sizeof(g_gpr_regnums_x86_64) / sizeof(g_gpr_regnums_x86_64[0])) - k_num_gpr_registers_x86_64, "g_gpr_regnums_x86_64 has wrong number of register infos"); +// x86 64-bit floating point registers. +static const uint32_t g_fpu_regnums_x86_64[] = { + lldb_fctrl_x86_64, lldb_fstat_x86_64, lldb_ftag_x86_64, + lldb_fop_x86_64, lldb_fiseg_x86_64, lldb_fioff_x86_64, + lldb_foseg_x86_64, lldb_fooff_x86_64, lldb_mxcsr_x86_64, + lldb_mxcsrmask_x86_64, lldb_st0_x86_64, lldb_st1_x86_64, + lldb_st2_x86_64, lldb_st3_x86_64, lldb_st4_x86_64, + lldb_st5_x86_64, lldb_st6_x86_64, lldb_st7_x86_64, + lldb_mm0_x86_64, lldb_mm1_x86_64, lldb_mm2_x86_64, + lldb_mm3_x86_64, lldb_mm4_x86_64, lldb_mm5_x86_64, + lldb_mm6_x86_64, lldb_mm7_x86_64, lldb_xmm0_x86_64, + lldb_xmm1_x86_64, lldb_xmm2_x86_64, lldb_xmm3_x86_64, + lldb_xmm4_x86_64, lldb_xmm5_x86_64, lldb_xmm6_x86_64, + lldb_xmm7_x86_64, lldb_xmm8_x86_64, lldb_xmm9_x86_64, + lldb_xmm10_x86_64, lldb_xmm11_x86_64, lldb_xmm12_x86_64, + lldb_xmm13_x86_64, lldb_xmm14_x86_64, lldb_xmm15_x86_64, + LLDB_INVALID_REGNUM // register sets need to end with this flag +}; +static_assert((sizeof(g_fpu_regnums_x86_64) / sizeof(g_fpu_regnums_x86_64[0])) - + 1 == + k_num_fpr_registers_x86_64, + "g_fpu_regnums_x86_64 has wrong number of register infos"); + +// x86 64-bit registers available via XState. +static const uint32_t g_xstate_regnums_x86_64[] = { + lldb_ymm0_x86_64, lldb_ymm1_x86_64, lldb_ymm2_x86_64, lldb_ymm3_x86_64, + lldb_ymm4_x86_64, lldb_ymm5_x86_64, lldb_ymm6_x86_64, lldb_ymm7_x86_64, + lldb_ymm8_x86_64, lldb_ymm9_x86_64, lldb_ymm10_x86_64, lldb_ymm11_x86_64, + lldb_ymm12_x86_64, lldb_ymm13_x86_64, lldb_ymm14_x86_64, lldb_ymm15_x86_64, + // Note: we currently do not provide them but this is needed to avoid + // unnamed groups in SBFrame::GetRegisterContext(). + lldb_bnd0_x86_64, lldb_bnd1_x86_64, lldb_bnd2_x86_64, + lldb_bnd3_x86_64, lldb_bndcfgu_x86_64, lldb_bndstatus_x86_64, + LLDB_INVALID_REGNUM // register sets need to end with this flag +}; +static_assert((sizeof(g_xstate_regnums_x86_64) / sizeof(g_xstate_regnums_x86_64[0])) - + 1 == + k_num_avx_registers_x86_64 + k_num_mpx_registers_x86_64, + "g_xstate_regnums_x86_64 has wrong number of register infos"); + +// x86 debug registers. +static const uint32_t g_dbr_regnums_x86_64[] = { + lldb_dr0_x86_64, lldb_dr1_x86_64, lldb_dr2_x86_64, lldb_dr3_x86_64, + lldb_dr4_x86_64, lldb_dr5_x86_64, lldb_dr6_x86_64, lldb_dr7_x86_64, + LLDB_INVALID_REGNUM // register sets need to end with this flag +}; +static_assert((sizeof(g_dbr_regnums_x86_64) / sizeof(g_dbr_regnums_x86_64[0])) - + 1 == + k_num_dbr_registers_x86_64, + "g_dbr_regnums_x86_64 has wrong number of register infos"); + // Number of register sets provided by this context. -enum { k_num_extended_register_sets = 2, k_num_register_sets = 4 }; +enum { k_num_register_sets = 4 }; // Register sets for x86 64-bit. static const RegisterSet g_reg_sets_x86_64[k_num_register_sets] = { {"General Purpose Registers", "gpr", k_num_gpr_registers_x86_64, g_gpr_regnums_x86_64}, + {"Floating Point Registers", "fpu", k_num_fpr_registers_x86_64, + g_fpu_regnums_x86_64}, + {"Extended State Registers", "xstate", + k_num_avx_registers_x86_64 + k_num_mpx_registers_x86_64, + g_xstate_regnums_x86_64}, + {"Debug Registers", "dbr", k_num_dbr_registers_x86_64, + g_dbr_regnums_x86_64}, }; #define REG_CONTEXT_SIZE (GetRegisterInfoInterface().GetGPRSize()) @@ -667,11 +725,6 @@ Status NativeRegisterContextNetBSD_x86_64::ReadAllRegisterValues( ::memcpy(dst, &m_gpr_x86_64, GetRegisterInfoInterface().GetGPRSize()); dst += GetRegisterInfoInterface().GetGPRSize(); - RegisterValue value((uint64_t)-1); - const RegisterInfo *reg_info = - GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_eax"); - if (reg_info == nullptr) - reg_info = GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_rax"); return error; } @@ -795,10 +848,10 @@ Status NativeRegisterContextNetBSD_x86_64::SetHardwareWatchpointWithIndex( if (!is_vacant) return Status("Watchpoint index not vacant"); - RegisterValue reg_value; const RegisterInfo *const reg_info_dr7 = GetRegisterInfoAtIndex(lldb_dr7_x86_64); - error = ReadRegister(reg_info_dr7, reg_value); + RegisterValue dr7_value; + error = ReadRegister(reg_info_dr7, dr7_value); if (error.Fail()) return error; @@ -816,16 +869,28 @@ Status NativeRegisterContextNetBSD_x86_64::SetHardwareWatchpointWithIndex( uint64_t bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index)); - uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; + uint64_t control_bits = dr7_value.GetAsUInt64() & ~bit_mask; control_bits |= enable_bit | rw_bits | size_bits; const RegisterInfo *const reg_info_drN = GetRegisterInfoAtIndex(lldb_dr0_x86_64 + wp_index); - error = WriteRegister(reg_info_drN, RegisterValue(addr)); + RegisterValue drN_value; + error = ReadRegister(reg_info_drN, drN_value); if (error.Fail()) return error; + // clear dr6 if address or bits changed (i.e. we're not reenabling the same + // watchpoint) + if (drN_value.GetAsUInt64() != addr || + (dr7_value.GetAsUInt64() & bit_mask) != (rw_bits | size_bits)) { + ClearWatchpointHit(wp_index); + + error = WriteRegister(reg_info_drN, RegisterValue(addr)); + if (error.Fail()) + return error; + } + error = WriteRegister(reg_info_dr7, RegisterValue(control_bits)); if (error.Fail()) return error; @@ -839,32 +904,36 @@ bool NativeRegisterContextNetBSD_x86_64::ClearHardwareWatchpoint( if (wp_index >= NumSupportedHardwareWatchpoints()) return false; + // for watchpoints 0, 1, 2, or 3, respectively, clear bits 0-1, 2-3, 4-5 + // or 6-7 of the debug control register (DR7) + const RegisterInfo *const reg_info_dr7 = + GetRegisterInfoAtIndex(lldb_dr7_x86_64); RegisterValue reg_value; + Status error = ReadRegister(reg_info_dr7, reg_value); + if (error.Fail()) + return false; + uint64_t bit_mask = 0x3 << (2 * wp_index); + uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; - // for watchpoints 0, 1, 2, or 3, respectively, clear bits 0, 1, 2, or 3 of + return WriteRegister(reg_info_dr7, RegisterValue(control_bits)).Success(); +} + +Status NativeRegisterContextNetBSD_x86_64::ClearWatchpointHit(uint32_t wp_index) { + if (wp_index >= NumSupportedHardwareWatchpoints()) + return Status("Watchpoint index out of range"); + + // for watchpoints 0, 1, 2, or 3, respectively, check bits 0, 1, 2, or 3 of // the debug status register (DR6) const RegisterInfo *const reg_info_dr6 = GetRegisterInfoAtIndex(lldb_dr6_x86_64); + RegisterValue reg_value; Status error = ReadRegister(reg_info_dr6, reg_value); if (error.Fail()) - return false; + return error; + uint64_t bit_mask = 1 << wp_index; uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask; - error = WriteRegister(reg_info_dr6, RegisterValue(status_bits)); - if (error.Fail()) - return false; - - // for watchpoints 0, 1, 2, or 3, respectively, clear bits {0-1,16-19}, - // {2-3,20-23}, {4-5,24-27}, or {6-7,28-31} of the debug control register - // (DR7) - const RegisterInfo *const reg_info_dr7 = - GetRegisterInfoAtIndex(lldb_dr7_x86_64); - error = ReadRegister(reg_info_dr7, reg_value); - if (error.Fail()) - return false; - bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index)); - uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; - return WriteRegister(reg_info_dr7, RegisterValue(control_bits)).Success(); + return WriteRegister(reg_info_dr6, RegisterValue(status_bits)); } Status NativeRegisterContextNetBSD_x86_64::ClearAllHardwareWatchpoints() { @@ -930,4 +999,19 @@ uint32_t NativeRegisterContextNetBSD_x86_64::NumSupportedHardwareWatchpoints() { return 4; } +Status NativeRegisterContextNetBSD_x86_64::CopyHardwareWatchpointsFrom( + NativeRegisterContextNetBSD &source) { + auto &r_source = static_cast<NativeRegisterContextNetBSD_x86_64&>(source); + Status res = r_source.ReadRegisterSet(DBRegSet); + if (!res.Fail()) { + // copy dbregs only if any watchpoints were set + if ((r_source.m_dbr_x86_64.dr[7] & 0xFF) == 0) + return res; + + m_dbr_x86_64 = r_source.m_dbr_x86_64; + res = WriteRegisterSet(DBRegSet); + } + return res; +} + #endif // defined(__x86_64__) diff --git a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.h b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.h index 0fed16542a95..54b8a806267f 100644 --- a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.h +++ b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.h @@ -58,6 +58,8 @@ public: bool ClearHardwareWatchpoint(uint32_t wp_index) override; + Status ClearWatchpointHit(uint32_t wp_index) override; + Status ClearAllHardwareWatchpoints() override; Status SetHardwareWatchpointWithIndex(lldb::addr_t addr, size_t size, @@ -71,9 +73,12 @@ public: uint32_t NumSupportedHardwareWatchpoints() override; + Status + CopyHardwareWatchpointsFrom(NativeRegisterContextNetBSD &source) override; + private: // Private member types. - enum { GPRegSet, FPRegSet, DBRegSet, XStateRegSet }; + enum { GPRegSet, FPRegSet, XStateRegSet, DBRegSet }; // Private member variables. struct reg m_gpr_x86_64; diff --git a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp index e25975c93446..dd2745d9330e 100644 --- a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp +++ b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp @@ -16,9 +16,20 @@ #include "lldb/Utility/LLDBAssert.h" #include "lldb/Utility/RegisterValue.h" #include "lldb/Utility/State.h" +#include "llvm/Support/Errno.h" + +// clang-format off +#include <sys/types.h> +#include <sys/ptrace.h> +// clang-format on #include <sstream> +// clang-format off +#include <sys/types.h> +#include <sys/sysctl.h> +// clang-format on + using namespace lldb; using namespace lldb_private; using namespace lldb_private::process_netbsd; @@ -30,6 +41,38 @@ NativeThreadNetBSD::NativeThreadNetBSD(NativeProcessNetBSD &process, NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(process.GetArchitecture(), *this) ), m_stop_description() {} +Status NativeThreadNetBSD::Resume() { + Status ret = NativeProcessNetBSD::PtraceWrapper(PT_RESUME, m_process.GetID(), + nullptr, GetID()); + if (!ret.Success()) + return ret; + ret = NativeProcessNetBSD::PtraceWrapper(PT_CLEARSTEP, m_process.GetID(), + nullptr, GetID()); + if (ret.Success()) + SetRunning(); + return ret; +} + +Status NativeThreadNetBSD::SingleStep() { + Status ret = NativeProcessNetBSD::PtraceWrapper(PT_RESUME, m_process.GetID(), + nullptr, GetID()); + if (!ret.Success()) + return ret; + ret = NativeProcessNetBSD::PtraceWrapper(PT_SETSTEP, m_process.GetID(), + nullptr, GetID()); + if (ret.Success()) + SetStepping(); + return ret; +} + +Status NativeThreadNetBSD::Suspend() { + Status ret = NativeProcessNetBSD::PtraceWrapper(PT_SUSPEND, m_process.GetID(), + nullptr, GetID()); + if (ret.Success()) + SetStopped(); + return ret; +} + void NativeThreadNetBSD::SetStoppedBySignal(uint32_t signo, const siginfo_t *info) { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); @@ -89,6 +132,13 @@ void NativeThreadNetBSD::SetStoppedByWatchpoint(uint32_t wp_index) { m_stop_info.details.signal.signo = SIGTRAP; } +void NativeThreadNetBSD::SetStoppedWithNoReason() { + SetStopped(); + + m_stop_info.reason = StopReason::eStopReasonNone; + m_stop_info.details.signal.signo = 0; +} + void NativeThreadNetBSD::SetStopped() { const StateType new_state = StateType::eStateStopped; m_state = new_state; @@ -105,7 +155,49 @@ void NativeThreadNetBSD::SetStepping() { m_stop_info.reason = StopReason::eStopReasonNone; } -std::string NativeThreadNetBSD::GetName() { return std::string(""); } +std::string NativeThreadNetBSD::GetName() { + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); + +#ifdef PT_LWPSTATUS + struct ptrace_lwpstatus info = {}; + info.pl_lwpid = m_tid; + Status error = NativeProcessNetBSD::PtraceWrapper( + PT_LWPSTATUS, static_cast<int>(m_process.GetID()), &info, sizeof(info)); + if (error.Fail()) { + return ""; + } + return info.pl_name; +#else + std::vector<struct kinfo_lwp> infos; + int mib[5] = {CTL_KERN, KERN_LWP, static_cast<int>(m_process.GetID()), + sizeof(struct kinfo_lwp), 0}; + size_t size; + + if (::sysctl(mib, 5, nullptr, &size, nullptr, 0) == -1 || size == 0) { + LLDB_LOG(log, "sysctl() for LWP info size failed: {0}", + llvm::sys::StrError()); + return ""; + } + + mib[4] = size / sizeof(size_t); + infos.resize(size / sizeof(struct kinfo_lwp)); + + if (sysctl(mib, 5, infos.data(), &size, NULL, 0) == -1 || size == 0) { + LLDB_LOG(log, "sysctl() for LWP info failed: {0}", llvm::sys::StrError()); + return ""; + } + + size_t nlwps = size / sizeof(struct kinfo_lwp); + for (size_t i = 0; i < nlwps; i++) { + if (static_cast<lldb::tid_t>(infos[i].l_lid) == m_tid) { + return infos[i].l_name; + } + } + + LLDB_LOG(log, "unable to find lwp {0} in LWP infos", m_tid); + return ""; +#endif +} lldb::StateType NativeThreadNetBSD::GetState() { return m_state; } @@ -140,9 +232,9 @@ bool NativeThreadNetBSD::GetStopReason(ThreadStopInfo &stop_info, llvm_unreachable("unhandled StateType!"); } -NativeRegisterContext& NativeThreadNetBSD::GetRegisterContext() { +NativeRegisterContextNetBSD &NativeThreadNetBSD::GetRegisterContext() { assert(m_reg_context_up); -return *m_reg_context_up; + return *m_reg_context_up; } Status NativeThreadNetBSD::SetWatchpoint(lldb::addr_t addr, size_t size, @@ -203,3 +295,13 @@ Status NativeThreadNetBSD::RemoveHardwareBreakpoint(lldb::addr_t addr) { return Status("Clearing hardware breakpoint failed."); } + +Status NativeThreadNetBSD::CopyWatchpointsFrom(NativeThreadNetBSD &source) { + Status s = GetRegisterContext().CopyHardwareWatchpointsFrom( + source.GetRegisterContext()); + if (!s.Fail()) { + m_watchpoint_index_map = source.m_watchpoint_index_map; + m_hw_break_index_map = source.m_hw_break_index_map; + } + return s; +} diff --git a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h index 015d8995db34..89b61ef86722 100644 --- a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h +++ b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h @@ -11,6 +11,8 @@ #include "lldb/Host/common/NativeThreadProtocol.h" +#include "Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h" + #include <csignal> #include <map> #include <string> @@ -34,7 +36,7 @@ public: bool GetStopReason(ThreadStopInfo &stop_info, std::string &description) override; - NativeRegisterContext& GetRegisterContext() override; + NativeRegisterContextNetBSD &GetRegisterContext() override; Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override; @@ -48,19 +50,26 @@ public: private: // Interface for friend classes + Status Resume(); + Status SingleStep(); + Status Suspend(); + void SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr); void SetStoppedByBreakpoint(); void SetStoppedByTrace(); void SetStoppedByExec(); void SetStoppedByWatchpoint(uint32_t wp_index); + void SetStoppedWithNoReason(); void SetStopped(); void SetRunning(); void SetStepping(); + Status CopyWatchpointsFrom(NativeThreadNetBSD& source); + // Member Variables lldb::StateType m_state; ThreadStopInfo m_stop_info; - std::unique_ptr<NativeRegisterContext> m_reg_context_up; + std::unique_ptr<NativeRegisterContextNetBSD> m_reg_context_up; std::string m_stop_description; using WatchpointIndexMap = std::map<lldb::addr_t, uint32_t>; WatchpointIndexMap m_watchpoint_index_map; |
