1 //===-- FreeBSDThread.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 // Other libraries and framework includes
15 #include "lldb/Core/State.h"
16 #include "lldb/Target/UnixSignals.h"
19 #include "lldb/Breakpoint/Watchpoint.h"
20 #include "lldb/Breakpoint/BreakpointLocation.h"
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/Core/State.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Host/HostNativeThread.h"
25 #include "lldb/Host/HostInfo.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/StopInfo.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/ThreadSpec.h"
30 #include "llvm/ADT/SmallString.h"
31 #include "POSIXStopInfo.h"
32 #include "FreeBSDThread.h"
33 #include "ProcessFreeBSD.h"
34 #include "ProcessPOSIXLog.h"
35 #include "ProcessMonitor.h"
36 #include "RegisterContextPOSIXProcessMonitor_arm.h"
37 #include "RegisterContextPOSIXProcessMonitor_arm64.h"
38 #include "RegisterContextPOSIXProcessMonitor_mips64.h"
39 #include "RegisterContextPOSIXProcessMonitor_powerpc.h"
40 #include "RegisterContextPOSIXProcessMonitor_x86.h"
41 #include "Plugins/Process/Utility/RegisterContextFreeBSD_arm.h"
42 #include "Plugins/Process/Utility/RegisterContextFreeBSD_arm64.h"
43 #include "Plugins/Process/Utility/RegisterContextFreeBSD_i386.h"
44 #include "Plugins/Process/Utility/RegisterContextFreeBSD_mips64.h"
45 #include "Plugins/Process/Utility/RegisterContextFreeBSD_powerpc.h"
46 #include "Plugins/Process/Utility/RegisterContextFreeBSD_x86_64.h"
47 #include "Plugins/Process/Utility/UnwindLLDB.h"
50 using namespace lldb_private;
52 FreeBSDThread::FreeBSDThread(Process &process, lldb::tid_t tid)
53 : Thread(process, tid),
56 m_thread_name_valid (false),
60 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
61 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
62 log->Printf ("FreeBSDThread::%s (tid = %" PRIi64 ")", __FUNCTION__, tid);
64 // Set the current watchpoints for this thread.
65 Target &target = GetProcess()->GetTarget();
66 const WatchpointList &wp_list = target.GetWatchpointList();
67 size_t wp_size = wp_list.GetSize();
69 for (uint32_t wp_idx = 0; wp_idx < wp_size; wp_idx++)
71 lldb::WatchpointSP wp = wp_list.GetByIndex(wp_idx);
72 if (wp.get() && wp->IsEnabled())
74 // This watchpoint as been enabled; obviously this "new" thread
75 // has been created since that watchpoint was enabled. Since
76 // the POSIXBreakpointProtocol has yet to be initialized, its
77 // m_watchpoints_initialized member will be FALSE. Attempting to
78 // read the debug status register to determine if a watchpoint
79 // has been hit would result in the zeroing of that register.
80 // Since the active debug registers would have been cloned when
81 // this thread was created, simply force the m_watchpoints_initized
82 // member to TRUE and avoid resetting dr6 and dr7.
83 GetPOSIXBreakpointProtocol()->ForceWatchpointsInitialized();
88 FreeBSDThread::~FreeBSDThread()
94 FreeBSDThread::GetMonitor()
96 ProcessSP base = GetProcess();
97 ProcessFreeBSD &process = static_cast<ProcessFreeBSD&>(*base);
98 return process.GetMonitor();
102 FreeBSDThread::RefreshStateAfterStop()
104 // Invalidate all registers in our register context. We don't set "force" to
105 // true because the stop reply packet might have had some register values
106 // that were expedited and these will already be copied into the register
107 // context by the time this function gets called. The KDPRegisterContext
108 // class has been made smart enough to detect when it needs to invalidate
109 // which registers are valid by putting hooks in the register read and
110 // register supply functions where they check the process stop ID and do
112 //if (StateIsStoppedState(GetState())
114 const bool force = false;
115 GetRegisterContext()->InvalidateIfNeeded (force);
120 FreeBSDThread::GetInfo()
126 FreeBSDThread::SetName (const char *name)
128 m_thread_name_valid = (name && name[0]);
129 if (m_thread_name_valid)
130 m_thread_name.assign (name);
132 m_thread_name.clear();
136 FreeBSDThread::GetName ()
138 if (!m_thread_name_valid)
140 llvm::SmallString<32> thread_name;
141 HostNativeThread::GetName(GetID(), thread_name);
142 m_thread_name = thread_name.c_str();
143 m_thread_name_valid = true;
146 if (m_thread_name.empty())
148 return m_thread_name.c_str();
151 lldb::RegisterContextSP
152 FreeBSDThread::GetRegisterContext()
154 if (!m_reg_context_sp)
156 m_posix_thread = NULL;
158 RegisterInfoInterface *reg_interface = NULL;
159 const ArchSpec &target_arch = GetProcess()->GetTarget().GetArchitecture();
161 assert(target_arch.GetTriple().getOS() == llvm::Triple::FreeBSD);
162 switch (target_arch.GetMachine())
164 case llvm::Triple::aarch64:
165 reg_interface = new RegisterContextFreeBSD_arm64(target_arch);
167 case llvm::Triple::arm:
168 reg_interface = new RegisterContextFreeBSD_arm(target_arch);
170 case llvm::Triple::ppc:
171 #ifndef __powerpc64__
172 reg_interface = new RegisterContextFreeBSD_powerpc32(target_arch);
175 case llvm::Triple::ppc64:
176 reg_interface = new RegisterContextFreeBSD_powerpc64(target_arch);
178 case llvm::Triple::mips64:
179 reg_interface = new RegisterContextFreeBSD_mips64(target_arch);
181 case llvm::Triple::x86:
182 reg_interface = new RegisterContextFreeBSD_i386(target_arch);
184 case llvm::Triple::x86_64:
185 reg_interface = new RegisterContextFreeBSD_x86_64(target_arch);
188 llvm_unreachable("CPU not supported");
191 switch (target_arch.GetMachine())
193 case llvm::Triple::aarch64:
195 RegisterContextPOSIXProcessMonitor_arm64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_arm64(*this, 0, reg_interface);
196 m_posix_thread = reg_ctx;
197 m_reg_context_sp.reset(reg_ctx);
200 case llvm::Triple::arm:
202 RegisterContextPOSIXProcessMonitor_arm *reg_ctx = new RegisterContextPOSIXProcessMonitor_arm(*this, 0, reg_interface);
203 m_posix_thread = reg_ctx;
204 m_reg_context_sp.reset(reg_ctx);
207 case llvm::Triple::mips64:
209 RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
210 m_posix_thread = reg_ctx;
211 m_reg_context_sp.reset(reg_ctx);
214 case llvm::Triple::ppc:
215 case llvm::Triple::ppc64:
217 RegisterContextPOSIXProcessMonitor_powerpc *reg_ctx = new RegisterContextPOSIXProcessMonitor_powerpc(*this, 0, reg_interface);
218 m_posix_thread = reg_ctx;
219 m_reg_context_sp.reset(reg_ctx);
222 case llvm::Triple::x86:
223 case llvm::Triple::x86_64:
225 RegisterContextPOSIXProcessMonitor_x86_64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_x86_64(*this, 0, reg_interface);
226 m_posix_thread = reg_ctx;
227 m_reg_context_sp.reset(reg_ctx);
234 return m_reg_context_sp;
237 lldb::RegisterContextSP
238 FreeBSDThread::CreateRegisterContextForFrame(lldb_private::StackFrame *frame)
240 lldb::RegisterContextSP reg_ctx_sp;
241 uint32_t concrete_frame_idx = 0;
243 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
244 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
245 log->Printf ("FreeBSDThread::%s ()", __FUNCTION__);
248 concrete_frame_idx = frame->GetConcreteFrameIndex();
250 if (concrete_frame_idx == 0)
251 reg_ctx_sp = GetRegisterContext();
254 assert(GetUnwinder());
255 reg_ctx_sp = GetUnwinder()->CreateRegisterContextForFrame(frame);
262 FreeBSDThread::GetThreadPointer ()
264 ProcessMonitor &monitor = GetMonitor();
266 if (monitor.ReadThreadPointer (GetID(), addr))
269 return LLDB_INVALID_ADDRESS;
273 FreeBSDThread::CalculateStopInfo()
275 SetStopInfo (m_stop_info_sp);
280 FreeBSDThread::GetUnwinder()
282 if (m_unwinder_ap.get() == NULL)
283 m_unwinder_ap.reset(new UnwindLLDB(*this));
285 return m_unwinder_ap.get();
289 FreeBSDThread::DidStop()
291 // Don't set the thread state to stopped unless we really stopped.
295 FreeBSDThread::WillResume(lldb::StateType resume_state)
297 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
299 log->Printf("tid %lu resume_state = %s", GetID(),
300 lldb_private::StateAsCString(resume_state));
301 ProcessSP process_sp(GetProcess());
302 ProcessFreeBSD *process = static_cast<ProcessFreeBSD *>(process_sp.get());
303 int signo = GetResumeSignal();
304 bool signo_valid = process->GetUnixSignals()->SignalIsValid(signo);
306 switch (resume_state)
308 case eStateSuspended:
310 process->m_suspend_tids.push_back(GetID());
313 process->m_run_tids.push_back(GetID());
315 process->m_resume_signo = signo;
318 process->m_step_tids.push_back(GetID());
320 process->m_resume_signo = signo;
328 FreeBSDThread::Resume()
330 lldb::StateType resume_state = GetResumeState();
331 ProcessMonitor &monitor = GetMonitor();
334 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
336 log->Printf ("FreeBSDThread::%s (), resume_state = %s", __FUNCTION__,
337 StateAsCString(resume_state));
339 switch (resume_state)
342 assert(false && "Unexpected state for resume!");
346 case lldb::eStateRunning:
347 SetState(resume_state);
348 status = monitor.Resume(GetID(), GetResumeSignal());
351 case lldb::eStateStepping:
352 SetState(resume_state);
353 status = monitor.SingleStep(GetID(), GetResumeSignal());
355 case lldb::eStateStopped:
356 case lldb::eStateSuspended:
365 FreeBSDThread::Notify(const ProcessMessage &message)
367 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
369 log->Printf ("FreeBSDThread::%s () message kind = '%s' for tid %" PRIu64,
370 __FUNCTION__, message.PrintKind(), GetID());
372 switch (message.GetKind())
375 assert(false && "Unexpected message kind!");
378 case ProcessMessage::eExitMessage:
379 // Nothing to be done.
382 case ProcessMessage::eLimboMessage:
383 LimboNotify(message);
386 case ProcessMessage::eSignalMessage:
387 SignalNotify(message);
390 case ProcessMessage::eSignalDeliveredMessage:
391 SignalDeliveredNotify(message);
394 case ProcessMessage::eTraceMessage:
395 TraceNotify(message);
398 case ProcessMessage::eBreakpointMessage:
399 BreakNotify(message);
402 case ProcessMessage::eWatchpointMessage:
403 WatchNotify(message);
406 case ProcessMessage::eCrashMessage:
407 CrashNotify(message);
410 case ProcessMessage::eExecMessage:
417 FreeBSDThread::EnableHardwareWatchpoint(Watchpoint *wp)
422 addr_t wp_addr = wp->GetLoadAddress();
423 size_t wp_size = wp->GetByteSize();
424 bool wp_read = wp->WatchpointRead();
425 bool wp_write = wp->WatchpointWrite();
426 uint32_t wp_hw_index = wp->GetHardwareIndex();
427 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
429 wp_set = reg_ctx->SetHardwareWatchpointWithIndex(wp_addr, wp_size,
437 FreeBSDThread::DisableHardwareWatchpoint(Watchpoint *wp)
442 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
443 if (reg_ctx_sp.get())
444 result = reg_ctx_sp->ClearHardwareWatchpoint(wp->GetHardwareIndex());
450 FreeBSDThread::NumSupportedHardwareWatchpoints()
452 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
453 if (reg_ctx_sp.get())
454 return reg_ctx_sp->NumSupportedHardwareWatchpoints();
459 FreeBSDThread::FindVacantWatchpointIndex()
461 uint32_t hw_index = LLDB_INVALID_INDEX32;
462 uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
464 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
467 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
469 if (reg_ctx->IsWatchpointVacant(wp_idx))
480 FreeBSDThread::BreakNotify(const ProcessMessage &message)
483 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
485 assert(GetRegisterContext());
486 status = GetPOSIXBreakpointProtocol()->UpdateAfterBreakpoint();
487 assert(status && "Breakpoint update failed!");
489 // With our register state restored, resolve the breakpoint object
490 // corresponding to our current PC.
491 assert(GetRegisterContext());
492 lldb::addr_t pc = GetRegisterContext()->GetPC();
494 log->Printf ("FreeBSDThread::%s () PC=0x%8.8" PRIx64, __FUNCTION__, pc);
495 lldb::BreakpointSiteSP bp_site(GetProcess()->GetBreakpointSiteList().FindByAddress(pc));
497 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
498 // we create a stop reason with should_stop=false. If there is no breakpoint location, then report
499 // an invalid stop reason. We don't need to worry about stepping over the breakpoint here, that will
500 // be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
503 lldb::break_id_t bp_id = bp_site->GetID();
504 // If we have an operating system plug-in, we might have set a thread specific breakpoint using the
505 // operating system thread ID, so we can't make any assumptions about the thread ID so we must always
506 // report the breakpoint regardless of the thread.
507 if (bp_site->ValidForThisThread(this) || GetProcess()->GetOperatingSystem () != NULL)
508 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id));
511 const bool should_stop = false;
512 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id, should_stop));
516 SetStopInfo(StopInfoSP());
520 FreeBSDThread::WatchNotify(const ProcessMessage &message)
522 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
524 lldb::addr_t halt_addr = message.GetHWAddress();
526 log->Printf ("FreeBSDThread::%s () Hardware Watchpoint Address = 0x%8.8"
527 PRIx64, __FUNCTION__, halt_addr);
529 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
532 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
534 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
536 if (reg_ctx->IsWatchpointHit(wp_idx))
538 // Clear the watchpoint hit here
539 reg_ctx->ClearWatchpointHits();
544 if (wp_idx == num_hw_wps)
547 Target &target = GetProcess()->GetTarget();
548 lldb::addr_t wp_monitor_addr = reg_ctx->GetWatchpointAddress(wp_idx);
549 const WatchpointList &wp_list = target.GetWatchpointList();
550 lldb::WatchpointSP wp_sp = wp_list.FindByAddress(wp_monitor_addr);
552 assert(wp_sp.get() && "No watchpoint found");
553 SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID(*this,
559 FreeBSDThread::TraceNotify(const ProcessMessage &message)
561 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
563 // Try to resolve the breakpoint object corresponding to the current PC.
564 assert(GetRegisterContext());
565 lldb::addr_t pc = GetRegisterContext()->GetPC();
567 log->Printf ("FreeBSDThread::%s () PC=0x%8.8" PRIx64, __FUNCTION__, pc);
568 lldb::BreakpointSiteSP bp_site(GetProcess()->GetBreakpointSiteList().FindByAddress(pc));
570 // If the current pc is a breakpoint site then set the StopInfo to Breakpoint.
571 // Otherwise, set the StopInfo to Watchpoint or Trace.
572 // If we have an operating system plug-in, we might have set a thread specific breakpoint using the
573 // operating system thread ID, so we can't make any assumptions about the thread ID so we must always
574 // report the breakpoint regardless of the thread.
575 if (bp_site && (bp_site->ValidForThisThread(this) || GetProcess()->GetOperatingSystem () != NULL))
576 SetStopInfo(StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_site->GetID()));
579 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
582 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
584 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
586 if (reg_ctx->IsWatchpointHit(wp_idx))
588 WatchNotify(message);
593 SetStopInfo (StopInfo::CreateStopReasonToTrace(*this));
598 FreeBSDThread::LimboNotify(const ProcessMessage &message)
600 SetStopInfo (lldb::StopInfoSP(new POSIXLimboStopInfo(*this)));
604 FreeBSDThread::SignalNotify(const ProcessMessage &message)
606 int signo = message.GetSignal();
607 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
611 FreeBSDThread::SignalDeliveredNotify(const ProcessMessage &message)
613 int signo = message.GetSignal();
614 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
618 FreeBSDThread::CrashNotify(const ProcessMessage &message)
620 // FIXME: Update stop reason as per bugzilla 14598
621 int signo = message.GetSignal();
623 assert(message.GetKind() == ProcessMessage::eCrashMessage);
625 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
627 log->Printf ("FreeBSDThread::%s () signo = %i, reason = '%s'",
628 __FUNCTION__, signo, message.PrintCrashReason());
630 SetStopInfo (lldb::StopInfoSP(new POSIXCrashStopInfo(*this, signo,
631 message.GetCrashReason(),
632 message.GetFaultAddress())));
636 FreeBSDThread::GetRegisterIndexFromOffset(unsigned offset)
638 unsigned reg = LLDB_INVALID_REGNUM;
639 ArchSpec arch = HostInfo::GetArchitecture();
641 switch (arch.GetMachine())
644 llvm_unreachable("CPU type not supported!");
647 case llvm::Triple::aarch64:
648 case llvm::Triple::arm:
649 case llvm::Triple::mips64:
650 case llvm::Triple::ppc:
651 case llvm::Triple::ppc64:
652 case llvm::Triple::x86:
653 case llvm::Triple::x86_64:
655 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
656 reg = reg_ctx->GetRegisterIndexFromOffset(offset);
664 FreeBSDThread::ExecNotify(const ProcessMessage &message)
666 SetStopInfo (StopInfo::CreateStopReasonWithExec(*this));
670 FreeBSDThread::GetRegisterName(unsigned reg)
672 const char * name = nullptr;
673 ArchSpec arch = HostInfo::GetArchitecture();
675 switch (arch.GetMachine())
678 assert(false && "CPU type not supported!");
681 case llvm::Triple::aarch64:
682 case llvm::Triple::arm:
683 case llvm::Triple::mips64:
684 case llvm::Triple::ppc:
685 case llvm::Triple::ppc64:
686 case llvm::Triple::x86:
687 case llvm::Triple::x86_64:
688 name = GetRegisterContext()->GetRegisterName(reg);
695 FreeBSDThread::GetRegisterNameFromOffset(unsigned offset)
697 return GetRegisterName(GetRegisterIndexFromOffset(offset));