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/Target/Process.h"
24 #include "lldb/Target/StopInfo.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/ThreadSpec.h"
27 #include "POSIXStopInfo.h"
28 #include "POSIXThread.h"
29 #include "ProcessPOSIX.h"
30 #include "ProcessPOSIXLog.h"
31 #include "ProcessMonitor.h"
32 #include "RegisterContextPOSIXProcessMonitor_mips64.h"
33 #include "RegisterContextPOSIXProcessMonitor_x86.h"
34 #include "RegisterContextLinux_i386.h"
35 #include "RegisterContextLinux_x86_64.h"
36 #include "RegisterContextFreeBSD_i386.h"
37 #include "RegisterContextFreeBSD_mips64.h"
38 #include "RegisterContextFreeBSD_x86_64.h"
40 #include "UnwindLLDB.h"
43 using namespace lldb_private;
46 POSIXThread::POSIXThread(Process &process, lldb::tid_t tid)
47 : Thread(process, tid),
50 m_thread_name_valid (false),
54 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
55 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
56 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ")", __FUNCTION__, tid);
58 // Set the current watchpoints for this thread.
59 Target &target = GetProcess()->GetTarget();
60 const WatchpointList &wp_list = target.GetWatchpointList();
61 size_t wp_size = wp_list.GetSize();
63 for (uint32_t wp_idx = 0; wp_idx < wp_size; wp_idx++)
65 lldb::WatchpointSP wp = wp_list.GetByIndex(wp_idx);
66 if (wp.get() && wp->IsEnabled())
68 // This watchpoint as been enabled; obviously this "new" thread
69 // has been created since that watchpoint was enabled. Since
70 // the POSIXBreakpointProtocol has yet to be initialized, its
71 // m_watchpoints_initialized member will be FALSE. Attempting to
72 // read the debug status register to determine if a watchpoint
73 // has been hit would result in the zeroing of that register.
74 // Since the active debug registers would have been cloned when
75 // this thread was created, simply force the m_watchpoints_initized
76 // member to TRUE and avoid resetting dr6 and dr7.
77 GetPOSIXBreakpointProtocol()->ForceWatchpointsInitialized();
82 POSIXThread::~POSIXThread()
88 POSIXThread::GetMonitor()
90 ProcessSP base = GetProcess();
91 ProcessPOSIX &process = static_cast<ProcessPOSIX&>(*base);
92 return process.GetMonitor();
95 // Overridden by FreeBSDThread; this is used only on Linux.
97 POSIXThread::RefreshStateAfterStop()
99 // Invalidate all registers in our register context. We don't set "force" to
100 // true because the stop reply packet might have had some register values
101 // that were expedited and these will already be copied into the register
102 // context by the time this function gets called. The KDPRegisterContext
103 // class has been made smart enough to detect when it needs to invalidate
104 // which registers are valid by putting hooks in the register read and
105 // register supply functions where they check the process stop ID and do
107 //if (StateIsStoppedState(GetState())
109 const bool force = false;
110 GetRegisterContext()->InvalidateIfNeeded (force);
112 // FIXME: This should probably happen somewhere else.
113 SetResumeState(eStateRunning);
114 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
116 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ") setting thread resume state to running", __FUNCTION__, GetID());
120 POSIXThread::GetInfo()
126 POSIXThread::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 POSIXThread::GetName ()
138 if (!m_thread_name_valid)
140 SetName(Host::GetThreadName(GetProcess()->GetID(), GetID()).c_str());
141 m_thread_name_valid = true;
144 if (m_thread_name.empty())
146 return m_thread_name.c_str();
149 lldb::RegisterContextSP
150 POSIXThread::GetRegisterContext()
152 if (!m_reg_context_sp)
154 m_posix_thread = NULL;
156 RegisterInfoInterface *reg_interface = NULL;
157 const ArchSpec &target_arch = GetProcess()->GetTarget().GetArchitecture();
159 switch (target_arch.GetCore())
161 case ArchSpec::eCore_mips64:
163 switch (target_arch.GetTriple().getOS())
165 case llvm::Triple::FreeBSD:
166 reg_interface = new RegisterContextFreeBSD_mips64(target_arch);
169 assert(false && "OS not supported");
175 RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
176 m_posix_thread = reg_ctx;
177 m_reg_context_sp.reset(reg_ctx);
182 case ArchSpec::eCore_x86_32_i386:
183 case ArchSpec::eCore_x86_32_i486:
184 case ArchSpec::eCore_x86_32_i486sx:
185 case ArchSpec::eCore_x86_64_x86_64:
187 switch (target_arch.GetTriple().getOS())
189 case llvm::Triple::FreeBSD:
190 reg_interface = new RegisterContextFreeBSD_x86_64(target_arch);
192 case llvm::Triple::Linux:
193 reg_interface = new RegisterContextLinux_x86_64(target_arch);
196 assert(false && "OS not supported");
202 RegisterContextPOSIXProcessMonitor_x86_64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_x86_64(*this, 0, reg_interface);
203 m_posix_thread = reg_ctx;
204 m_reg_context_sp.reset(reg_ctx);
210 assert(false && "CPU type not supported!");
214 return m_reg_context_sp;
217 lldb::RegisterContextSP
218 POSIXThread::CreateRegisterContextForFrame(lldb_private::StackFrame *frame)
220 lldb::RegisterContextSP reg_ctx_sp;
221 uint32_t concrete_frame_idx = 0;
223 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
224 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
225 log->Printf ("POSIXThread::%s ()", __FUNCTION__);
228 concrete_frame_idx = frame->GetConcreteFrameIndex();
230 if (concrete_frame_idx == 0)
231 reg_ctx_sp = GetRegisterContext();
234 assert(GetUnwinder());
235 reg_ctx_sp = GetUnwinder()->CreateRegisterContextForFrame(frame);
242 POSIXThread::GetThreadPointer ()
244 ProcessMonitor &monitor = GetMonitor();
246 if (monitor.ReadThreadPointer (GetID(), addr))
249 return LLDB_INVALID_ADDRESS;
253 POSIXThread::CalculateStopInfo()
255 SetStopInfo (m_stop_info_sp);
260 POSIXThread::GetUnwinder()
262 if (m_unwinder_ap.get() == NULL)
263 m_unwinder_ap.reset(new UnwindLLDB(*this));
265 return m_unwinder_ap.get();
268 // Overridden by FreeBSDThread; this is used only on Linux.
270 POSIXThread::WillResume(lldb::StateType resume_state)
272 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
274 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ") setting thread resume state to %s", __FUNCTION__, GetID(), StateAsCString(resume_state));
275 // TODO: the line below shouldn't really be done, but
276 // the POSIXThread might rely on this so I will leave this in for now
277 SetResumeState(resume_state);
281 POSIXThread::DidStop()
283 // Don't set the thread state to stopped unless we really stopped.
287 POSIXThread::Resume()
289 lldb::StateType resume_state = GetResumeState();
290 ProcessMonitor &monitor = GetMonitor();
293 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
295 log->Printf ("POSIXThread::%s (), resume_state = %s", __FUNCTION__,
296 StateAsCString(resume_state));
298 switch (resume_state)
301 assert(false && "Unexpected state for resume!");
305 case lldb::eStateRunning:
306 SetState(resume_state);
307 status = monitor.Resume(GetID(), GetResumeSignal());
310 case lldb::eStateStepping:
311 SetState(resume_state);
312 status = monitor.SingleStep(GetID(), GetResumeSignal());
314 case lldb::eStateStopped:
315 case lldb::eStateSuspended:
324 POSIXThread::Notify(const ProcessMessage &message)
326 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
328 log->Printf ("POSIXThread::%s () message kind = '%s' for tid %" PRIu64,
329 __FUNCTION__, message.PrintKind(), GetID());
331 switch (message.GetKind())
334 assert(false && "Unexpected message kind!");
337 case ProcessMessage::eExitMessage:
338 // Nothing to be done.
341 case ProcessMessage::eLimboMessage:
342 LimboNotify(message);
345 case ProcessMessage::eSignalMessage:
346 SignalNotify(message);
349 case ProcessMessage::eSignalDeliveredMessage:
350 SignalDeliveredNotify(message);
353 case ProcessMessage::eTraceMessage:
354 TraceNotify(message);
357 case ProcessMessage::eBreakpointMessage:
358 BreakNotify(message);
361 case ProcessMessage::eWatchpointMessage:
362 WatchNotify(message);
365 case ProcessMessage::eCrashMessage:
366 CrashNotify(message);
369 case ProcessMessage::eNewThreadMessage:
370 ThreadNotify(message);
373 case ProcessMessage::eExecMessage:
380 POSIXThread::EnableHardwareWatchpoint(Watchpoint *wp)
385 addr_t wp_addr = wp->GetLoadAddress();
386 size_t wp_size = wp->GetByteSize();
387 bool wp_read = wp->WatchpointRead();
388 bool wp_write = wp->WatchpointWrite();
389 uint32_t wp_hw_index = wp->GetHardwareIndex();
390 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
392 wp_set = reg_ctx->SetHardwareWatchpointWithIndex(wp_addr, wp_size,
400 POSIXThread::DisableHardwareWatchpoint(Watchpoint *wp)
405 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
406 if (reg_ctx_sp.get())
407 result = reg_ctx_sp->ClearHardwareWatchpoint(wp->GetHardwareIndex());
413 POSIXThread::NumSupportedHardwareWatchpoints()
415 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
416 if (reg_ctx_sp.get())
417 return reg_ctx_sp->NumSupportedHardwareWatchpoints();
422 POSIXThread::FindVacantWatchpointIndex()
424 uint32_t hw_index = LLDB_INVALID_INDEX32;
425 uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
427 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
430 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
432 if (reg_ctx->IsWatchpointVacant(wp_idx))
443 POSIXThread::BreakNotify(const ProcessMessage &message)
446 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
448 assert(GetRegisterContext());
449 status = GetPOSIXBreakpointProtocol()->UpdateAfterBreakpoint();
450 assert(status && "Breakpoint update failed!");
452 // With our register state restored, resolve the breakpoint object
453 // corresponding to our current PC.
454 assert(GetRegisterContext());
455 lldb::addr_t pc = GetRegisterContext()->GetPC();
457 log->Printf ("POSIXThread::%s () PC=0x%8.8" PRIx64, __FUNCTION__, pc);
458 lldb::BreakpointSiteSP bp_site(GetProcess()->GetBreakpointSiteList().FindByAddress(pc));
460 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
461 // we create a stop reason with should_stop=false. If there is no breakpoint location, then report
462 // an invalid stop reason. We don't need to worry about stepping over the breakpoint here, that will
463 // be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
466 lldb::break_id_t bp_id = bp_site->GetID();
467 if (bp_site->ValidForThisThread(this))
468 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id));
471 const bool should_stop = false;
472 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id, should_stop));
476 SetStopInfo(StopInfoSP());
480 POSIXThread::WatchNotify(const ProcessMessage &message)
482 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
484 lldb::addr_t halt_addr = message.GetHWAddress();
486 log->Printf ("POSIXThread::%s () Hardware Watchpoint Address = 0x%8.8"
487 PRIx64, __FUNCTION__, halt_addr);
489 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
492 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
494 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
496 if (reg_ctx->IsWatchpointHit(wp_idx))
498 // Clear the watchpoint hit here
499 reg_ctx->ClearWatchpointHits();
504 if (wp_idx == num_hw_wps)
507 Target &target = GetProcess()->GetTarget();
508 lldb::addr_t wp_monitor_addr = reg_ctx->GetWatchpointAddress(wp_idx);
509 const WatchpointList &wp_list = target.GetWatchpointList();
510 lldb::WatchpointSP wp_sp = wp_list.FindByAddress(wp_monitor_addr);
512 assert(wp_sp.get() && "No watchpoint found");
513 SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID(*this,
519 POSIXThread::TraceNotify(const ProcessMessage &message)
521 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
524 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
526 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
528 if (reg_ctx->IsWatchpointHit(wp_idx))
530 WatchNotify(message);
536 SetStopInfo (StopInfo::CreateStopReasonToTrace(*this));
540 POSIXThread::LimboNotify(const ProcessMessage &message)
542 SetStopInfo (lldb::StopInfoSP(new POSIXLimboStopInfo(*this)));
546 POSIXThread::SignalNotify(const ProcessMessage &message)
548 int signo = message.GetSignal();
550 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
551 SetResumeSignal(signo);
555 POSIXThread::SignalDeliveredNotify(const ProcessMessage &message)
557 int signo = message.GetSignal();
559 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
560 SetResumeSignal(signo);
564 POSIXThread::CrashNotify(const ProcessMessage &message)
566 // FIXME: Update stop reason as per bugzilla 14598
567 int signo = message.GetSignal();
569 assert(message.GetKind() == ProcessMessage::eCrashMessage);
571 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
573 log->Printf ("POSIXThread::%s () signo = %i, reason = '%s'",
574 __FUNCTION__, signo, message.PrintCrashReason());
576 SetStopInfo (lldb::StopInfoSP(new POSIXCrashStopInfo(*this, signo,
577 message.GetCrashReason(),
578 message.GetFaultAddress())));
579 SetResumeSignal(signo);
583 POSIXThread::ThreadNotify(const ProcessMessage &message)
585 SetStopInfo (lldb::StopInfoSP(new POSIXNewThreadStopInfo(*this)));
589 POSIXThread::GetRegisterIndexFromOffset(unsigned offset)
591 unsigned reg = LLDB_INVALID_REGNUM;
592 ArchSpec arch = Host::GetArchitecture();
594 switch (arch.GetCore())
597 llvm_unreachable("CPU type not supported!");
600 case ArchSpec::eCore_mips64:
601 case ArchSpec::eCore_x86_32_i386:
602 case ArchSpec::eCore_x86_32_i486:
603 case ArchSpec::eCore_x86_32_i486sx:
604 case ArchSpec::eCore_x86_64_x86_64:
606 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
607 reg = reg_ctx->GetRegisterIndexFromOffset(offset);
615 POSIXThread::ExecNotify(const ProcessMessage &message)
617 SetStopInfo (StopInfo::CreateStopReasonWithExec(*this));
621 POSIXThread::GetRegisterName(unsigned reg)
623 const char * name = nullptr;
624 ArchSpec arch = Host::GetArchitecture();
626 switch (arch.GetCore())
629 assert(false && "CPU type not supported!");
632 case ArchSpec::eCore_mips64:
633 case ArchSpec::eCore_x86_32_i386:
634 case ArchSpec::eCore_x86_32_i486:
635 case ArchSpec::eCore_x86_32_i486sx:
636 case ArchSpec::eCore_x86_64_x86_64:
637 name = GetRegisterContext()->GetRegisterName(reg);
644 POSIXThread::GetRegisterNameFromOffset(unsigned offset)
646 return GetRegisterName(GetRegisterIndexFromOffset(offset));