1 //===-- POSIXThread.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 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
16 // Other libraries and framework includes
18 #include "lldb/Breakpoint/Watchpoint.h"
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/Debugger.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Host/Host.h"
23 #include "lldb/Host/HostNativeThread.h"
24 #include "lldb/Host/HostInfo.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/StopInfo.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/ThreadSpec.h"
29 #include "llvm/ADT/SmallString.h"
30 #include "POSIXStopInfo.h"
31 #include "POSIXThread.h"
32 #include "ProcessPOSIX.h"
33 #include "ProcessPOSIXLog.h"
34 #include "Plugins/Process/Linux/ProcessMonitor.h"
35 #include "RegisterContextPOSIXProcessMonitor_arm64.h"
36 #include "RegisterContextPOSIXProcessMonitor_mips64.h"
37 #include "RegisterContextPOSIXProcessMonitor_powerpc.h"
38 #include "RegisterContextPOSIXProcessMonitor_x86.h"
39 #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h"
40 #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
41 #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
42 #include "Plugins/Process/Utility/RegisterContextFreeBSD_i386.h"
43 #include "Plugins/Process/Utility/RegisterContextFreeBSD_mips64.h"
44 #include "Plugins/Process/Utility/RegisterContextFreeBSD_powerpc.h"
45 #include "Plugins/Process/Utility/RegisterContextFreeBSD_x86_64.h"
46 #include "Plugins/Process/Utility/UnwindLLDB.h"
49 using namespace lldb_private;
52 POSIXThread::POSIXThread(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 ("POSIXThread::%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 POSIXThread::~POSIXThread()
94 POSIXThread::GetMonitor()
96 ProcessSP base = GetProcess();
97 ProcessPOSIX &process = static_cast<ProcessPOSIX&>(*base);
98 return process.GetMonitor();
101 // Overridden by FreeBSDThread; this is used only on Linux.
103 POSIXThread::RefreshStateAfterStop()
105 // Invalidate all registers in our register context. We don't set "force" to
106 // true because the stop reply packet might have had some register values
107 // that were expedited and these will already be copied into the register
108 // context by the time this function gets called. The KDPRegisterContext
109 // class has been made smart enough to detect when it needs to invalidate
110 // which registers are valid by putting hooks in the register read and
111 // register supply functions where they check the process stop ID and do
113 //if (StateIsStoppedState(GetState())
115 const bool force = false;
116 GetRegisterContext()->InvalidateIfNeeded (force);
118 // FIXME: This should probably happen somewhere else.
119 SetResumeState(eStateRunning, true);
120 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
122 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ") setting thread resume state to running", __FUNCTION__, GetID());
126 POSIXThread::GetInfo()
132 POSIXThread::SetName (const char *name)
134 m_thread_name_valid = (name && name[0]);
135 if (m_thread_name_valid)
136 m_thread_name.assign (name);
138 m_thread_name.clear();
142 POSIXThread::GetName ()
144 if (!m_thread_name_valid)
146 llvm::SmallString<32> thread_name;
147 HostNativeThread::GetName(GetID(), thread_name);
148 m_thread_name = thread_name.c_str();
149 m_thread_name_valid = true;
152 if (m_thread_name.empty())
154 return m_thread_name.c_str();
157 lldb::RegisterContextSP
158 POSIXThread::GetRegisterContext()
160 if (!m_reg_context_sp)
162 m_posix_thread = NULL;
164 RegisterInfoInterface *reg_interface = NULL;
165 const ArchSpec &target_arch = GetProcess()->GetTarget().GetArchitecture();
167 switch (target_arch.GetTriple().getOS())
169 case llvm::Triple::FreeBSD:
170 switch (target_arch.GetMachine())
172 case llvm::Triple::ppc:
173 #ifndef __powerpc64__
174 reg_interface = new RegisterContextFreeBSD_powerpc32(target_arch);
177 case llvm::Triple::ppc64:
178 reg_interface = new RegisterContextFreeBSD_powerpc64(target_arch);
180 case llvm::Triple::mips64:
181 reg_interface = new RegisterContextFreeBSD_mips64(target_arch);
183 case llvm::Triple::x86:
184 reg_interface = new RegisterContextFreeBSD_i386(target_arch);
186 case llvm::Triple::x86_64:
187 reg_interface = new RegisterContextFreeBSD_x86_64(target_arch);
194 case llvm::Triple::Linux:
195 switch (target_arch.GetMachine())
197 case llvm::Triple::aarch64:
198 assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host");
199 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch));
201 case llvm::Triple::x86:
202 case llvm::Triple::x86_64:
203 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
205 // 32-bit hosts run with a RegisterContextLinux_i386 context.
206 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
210 assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
211 "Register setting path assumes this is a 64-bit host");
212 // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
213 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_x86_64(target_arch));
224 assert(reg_interface && "OS or CPU not supported!");
226 switch (target_arch.GetMachine())
228 case llvm::Triple::aarch64:
230 RegisterContextPOSIXProcessMonitor_arm64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_arm64(*this, 0, reg_interface);
231 m_posix_thread = reg_ctx;
232 m_reg_context_sp.reset(reg_ctx);
235 case llvm::Triple::mips64:
237 RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
238 m_posix_thread = reg_ctx;
239 m_reg_context_sp.reset(reg_ctx);
242 case llvm::Triple::ppc:
243 case llvm::Triple::ppc64:
245 RegisterContextPOSIXProcessMonitor_powerpc *reg_ctx = new RegisterContextPOSIXProcessMonitor_powerpc(*this, 0, reg_interface);
246 m_posix_thread = reg_ctx;
247 m_reg_context_sp.reset(reg_ctx);
250 case llvm::Triple::x86:
251 case llvm::Triple::x86_64:
253 RegisterContextPOSIXProcessMonitor_x86_64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_x86_64(*this, 0, reg_interface);
254 m_posix_thread = reg_ctx;
255 m_reg_context_sp.reset(reg_ctx);
262 return m_reg_context_sp;
265 lldb::RegisterContextSP
266 POSIXThread::CreateRegisterContextForFrame(lldb_private::StackFrame *frame)
268 lldb::RegisterContextSP reg_ctx_sp;
269 uint32_t concrete_frame_idx = 0;
271 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
272 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
273 log->Printf ("POSIXThread::%s ()", __FUNCTION__);
276 concrete_frame_idx = frame->GetConcreteFrameIndex();
278 if (concrete_frame_idx == 0)
279 reg_ctx_sp = GetRegisterContext();
282 assert(GetUnwinder());
283 reg_ctx_sp = GetUnwinder()->CreateRegisterContextForFrame(frame);
290 POSIXThread::GetThreadPointer ()
292 ProcessMonitor &monitor = GetMonitor();
294 if (monitor.ReadThreadPointer (GetID(), addr))
297 return LLDB_INVALID_ADDRESS;
301 POSIXThread::CalculateStopInfo()
303 SetStopInfo (m_stop_info_sp);
308 POSIXThread::GetUnwinder()
310 if (m_unwinder_ap.get() == NULL)
311 m_unwinder_ap.reset(new UnwindLLDB(*this));
313 return m_unwinder_ap.get();
316 // Overridden by FreeBSDThread; this is used only on Linux.
318 POSIXThread::WillResume(lldb::StateType resume_state)
320 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
322 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ") setting thread resume state to %s", __FUNCTION__, GetID(), StateAsCString(resume_state));
323 // TODO: the line below shouldn't really be done, but
324 // the POSIXThread might rely on this so I will leave this in for now
325 SetResumeState(resume_state);
329 POSIXThread::DidStop()
331 // Don't set the thread state to stopped unless we really stopped.
335 POSIXThread::Resume()
337 lldb::StateType resume_state = GetResumeState();
338 ProcessMonitor &monitor = GetMonitor();
341 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
343 log->Printf ("POSIXThread::%s (), resume_state = %s", __FUNCTION__,
344 StateAsCString(resume_state));
346 switch (resume_state)
349 assert(false && "Unexpected state for resume!");
353 case lldb::eStateRunning:
354 SetState(resume_state);
355 status = monitor.Resume(GetID(), GetResumeSignal());
358 case lldb::eStateStepping:
359 SetState(resume_state);
360 status = monitor.SingleStep(GetID(), GetResumeSignal());
362 case lldb::eStateStopped:
363 case lldb::eStateSuspended:
372 POSIXThread::Notify(const ProcessMessage &message)
374 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
376 log->Printf ("POSIXThread::%s () message kind = '%s' for tid %" PRIu64,
377 __FUNCTION__, message.PrintKind(), GetID());
379 switch (message.GetKind())
382 assert(false && "Unexpected message kind!");
385 case ProcessMessage::eExitMessage:
386 // Nothing to be done.
389 case ProcessMessage::eLimboMessage:
390 LimboNotify(message);
393 case ProcessMessage::eSignalMessage:
394 SignalNotify(message);
397 case ProcessMessage::eSignalDeliveredMessage:
398 SignalDeliveredNotify(message);
401 case ProcessMessage::eTraceMessage:
402 TraceNotify(message);
405 case ProcessMessage::eBreakpointMessage:
406 BreakNotify(message);
409 case ProcessMessage::eWatchpointMessage:
410 WatchNotify(message);
413 case ProcessMessage::eCrashMessage:
414 CrashNotify(message);
417 case ProcessMessage::eNewThreadMessage:
418 ThreadNotify(message);
421 case ProcessMessage::eExecMessage:
428 POSIXThread::EnableHardwareWatchpoint(Watchpoint *wp)
433 addr_t wp_addr = wp->GetLoadAddress();
434 size_t wp_size = wp->GetByteSize();
435 bool wp_read = wp->WatchpointRead();
436 bool wp_write = wp->WatchpointWrite();
437 uint32_t wp_hw_index = wp->GetHardwareIndex();
438 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
440 wp_set = reg_ctx->SetHardwareWatchpointWithIndex(wp_addr, wp_size,
448 POSIXThread::DisableHardwareWatchpoint(Watchpoint *wp)
453 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
454 if (reg_ctx_sp.get())
455 result = reg_ctx_sp->ClearHardwareWatchpoint(wp->GetHardwareIndex());
461 POSIXThread::NumSupportedHardwareWatchpoints()
463 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
464 if (reg_ctx_sp.get())
465 return reg_ctx_sp->NumSupportedHardwareWatchpoints();
470 POSIXThread::FindVacantWatchpointIndex()
472 uint32_t hw_index = LLDB_INVALID_INDEX32;
473 uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
475 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
478 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
480 if (reg_ctx->IsWatchpointVacant(wp_idx))
491 POSIXThread::BreakNotify(const ProcessMessage &message)
494 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
496 assert(GetRegisterContext());
497 status = GetPOSIXBreakpointProtocol()->UpdateAfterBreakpoint();
498 assert(status && "Breakpoint update failed!");
500 // With our register state restored, resolve the breakpoint object
501 // corresponding to our current PC.
502 assert(GetRegisterContext());
503 lldb::addr_t pc = GetRegisterContext()->GetPC();
505 log->Printf ("POSIXThread::%s () PC=0x%8.8" PRIx64, __FUNCTION__, pc);
506 lldb::BreakpointSiteSP bp_site(GetProcess()->GetBreakpointSiteList().FindByAddress(pc));
508 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
509 // we create a stop reason with should_stop=false. If there is no breakpoint location, then report
510 // an invalid stop reason. We don't need to worry about stepping over the breakpoint here, that will
511 // be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
514 lldb::break_id_t bp_id = bp_site->GetID();
515 if (bp_site->ValidForThisThread(this))
516 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id));
519 const bool should_stop = false;
520 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id, should_stop));
524 SetStopInfo(StopInfoSP());
528 POSIXThread::WatchNotify(const ProcessMessage &message)
530 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
532 lldb::addr_t halt_addr = message.GetHWAddress();
534 log->Printf ("POSIXThread::%s () Hardware Watchpoint Address = 0x%8.8"
535 PRIx64, __FUNCTION__, halt_addr);
537 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
540 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
542 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
544 if (reg_ctx->IsWatchpointHit(wp_idx))
546 // Clear the watchpoint hit here
547 reg_ctx->ClearWatchpointHits();
552 if (wp_idx == num_hw_wps)
555 Target &target = GetProcess()->GetTarget();
556 lldb::addr_t wp_monitor_addr = reg_ctx->GetWatchpointAddress(wp_idx);
557 const WatchpointList &wp_list = target.GetWatchpointList();
558 lldb::WatchpointSP wp_sp = wp_list.FindByAddress(wp_monitor_addr);
560 assert(wp_sp.get() && "No watchpoint found");
561 SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID(*this,
567 POSIXThread::TraceNotify(const ProcessMessage &message)
569 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
572 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
574 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
576 if (reg_ctx->IsWatchpointHit(wp_idx))
578 WatchNotify(message);
584 SetStopInfo (StopInfo::CreateStopReasonToTrace(*this));
588 POSIXThread::LimboNotify(const ProcessMessage &message)
590 SetStopInfo (lldb::StopInfoSP(new POSIXLimboStopInfo(*this)));
594 POSIXThread::SignalNotify(const ProcessMessage &message)
596 int signo = message.GetSignal();
597 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
601 POSIXThread::SignalDeliveredNotify(const ProcessMessage &message)
603 int signo = message.GetSignal();
604 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
608 POSIXThread::CrashNotify(const ProcessMessage &message)
610 // FIXME: Update stop reason as per bugzilla 14598
611 int signo = message.GetSignal();
613 assert(message.GetKind() == ProcessMessage::eCrashMessage);
615 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
617 log->Printf ("POSIXThread::%s () signo = %i, reason = '%s'",
618 __FUNCTION__, signo, message.PrintCrashReason());
620 SetStopInfo (lldb::StopInfoSP(new POSIXCrashStopInfo(*this, signo,
621 message.GetCrashReason(),
622 message.GetFaultAddress())));
626 POSIXThread::ThreadNotify(const ProcessMessage &message)
628 SetStopInfo (lldb::StopInfoSP(new POSIXNewThreadStopInfo(*this)));
632 POSIXThread::GetRegisterIndexFromOffset(unsigned offset)
634 unsigned reg = LLDB_INVALID_REGNUM;
635 ArchSpec arch = HostInfo::GetArchitecture();
637 switch (arch.GetMachine())
640 llvm_unreachable("CPU type not supported!");
643 case llvm::Triple::aarch64:
644 case llvm::Triple::mips64:
645 case llvm::Triple::ppc:
646 case llvm::Triple::ppc64:
647 case llvm::Triple::x86:
648 case llvm::Triple::x86_64:
650 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
651 reg = reg_ctx->GetRegisterIndexFromOffset(offset);
659 POSIXThread::ExecNotify(const ProcessMessage &message)
661 SetStopInfo (StopInfo::CreateStopReasonWithExec(*this));
665 POSIXThread::GetRegisterName(unsigned reg)
667 const char * name = nullptr;
668 ArchSpec arch = HostInfo::GetArchitecture();
670 switch (arch.GetMachine())
673 assert(false && "CPU type not supported!");
676 case llvm::Triple::aarch64:
677 case llvm::Triple::mips64:
678 case llvm::Triple::ppc:
679 case llvm::Triple::ppc64:
680 case llvm::Triple::x86:
681 case llvm::Triple::x86_64:
682 name = GetRegisterContext()->GetRegisterName(reg);
689 POSIXThread::GetRegisterNameFromOffset(unsigned offset)
691 return GetRegisterName(GetRegisterIndexFromOffset(offset));