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 "RegisterContext_i386.h"
33 #include "RegisterContext_x86_64.h"
34 #include "RegisterContextPOSIX.h"
35 #include "RegisterContextLinux_x86_64.h"
36 #include "RegisterContextFreeBSD_x86_64.h"
38 #include "UnwindLLDB.h"
41 using namespace lldb_private;
44 POSIXThread::POSIXThread(Process &process, lldb::tid_t tid)
45 : Thread(process, tid),
48 m_thread_name_valid (false),
51 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
52 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
53 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ")", __FUNCTION__, tid);
55 // Set the current watchpoints for this thread.
56 Target &target = GetProcess()->GetTarget();
57 const WatchpointList &wp_list = target.GetWatchpointList();
58 size_t wp_size = wp_list.GetSize();
60 for (uint32_t wp_idx = 0; wp_idx < wp_size; wp_idx++)
62 lldb::WatchpointSP wp = wp_list.GetByIndex(wp_idx);
63 if (wp.get() && wp->IsEnabled())
65 assert(EnableHardwareWatchpoint(wp.get()));
70 POSIXThread::~POSIXThread()
76 POSIXThread::GetMonitor()
78 ProcessSP base = GetProcess();
79 ProcessPOSIX &process = static_cast<ProcessPOSIX&>(*base);
80 return process.GetMonitor();
84 POSIXThread::RefreshStateAfterStop()
86 // Invalidate all registers in our register context. We don't set "force" to
87 // true because the stop reply packet might have had some register values
88 // that were expedited and these will already be copied into the register
89 // context by the time this function gets called. The KDPRegisterContext
90 // class has been made smart enough to detect when it needs to invalidate
91 // which registers are valid by putting hooks in the register read and
92 // register supply functions where they check the process stop ID and do
94 //if (StateIsStoppedState(GetState())
96 const bool force = false;
97 GetRegisterContext()->InvalidateIfNeeded (force);
99 // FIXME: This should probably happen somewhere else.
100 SetResumeState(eStateRunning);
101 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
103 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ") setting thread resume state to running", __FUNCTION__, GetID());
107 POSIXThread::GetInfo()
113 POSIXThread::SetName (const char *name)
115 m_thread_name_valid = (name && name[0]);
116 if (m_thread_name_valid)
117 m_thread_name.assign (name);
119 m_thread_name.clear();
123 POSIXThread::GetName ()
125 if (!m_thread_name_valid)
127 SetName(Host::GetThreadName(GetProcess()->GetID(), GetID()).c_str());
128 m_thread_name_valid = true;
131 if (m_thread_name.empty())
133 return m_thread_name.c_str();
136 lldb::RegisterContextSP
137 POSIXThread::GetRegisterContext()
139 if (!m_reg_context_sp)
141 ArchSpec arch = Host::GetArchitecture();
143 switch (arch.GetCore())
146 assert(false && "CPU type not supported!");
149 case ArchSpec::eCore_x86_32_i386:
150 case ArchSpec::eCore_x86_32_i486:
151 case ArchSpec::eCore_x86_32_i486sx:
152 m_reg_context_sp.reset(new RegisterContext_i386(*this, 0));
155 case ArchSpec::eCore_x86_64_x86_64:
156 switch (arch.GetTriple().getOS())
158 case llvm::Triple::FreeBSD:
159 m_reg_context_sp.reset(new RegisterContextFreeBSD_x86_64(*this, 0));
161 case llvm::Triple::Linux:
162 m_reg_context_sp.reset(new RegisterContextLinux_x86_64(*this, 0));
165 assert(false && "OS not supported");
171 return m_reg_context_sp;
174 lldb::RegisterContextSP
175 POSIXThread::CreateRegisterContextForFrame(lldb_private::StackFrame *frame)
177 lldb::RegisterContextSP reg_ctx_sp;
178 uint32_t concrete_frame_idx = 0;
180 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
181 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
182 log->Printf ("POSIXThread::%s ()", __FUNCTION__);
185 concrete_frame_idx = frame->GetConcreteFrameIndex();
187 if (concrete_frame_idx == 0)
188 reg_ctx_sp = GetRegisterContext();
191 assert(GetUnwinder());
192 reg_ctx_sp = GetUnwinder()->CreateRegisterContextForFrame(frame);
199 POSIXThread::CalculateStopInfo()
201 SetStopInfo (m_stop_info_sp);
206 POSIXThread::GetUnwinder()
208 if (m_unwinder_ap.get() == NULL)
209 m_unwinder_ap.reset(new UnwindLLDB(*this));
211 return m_unwinder_ap.get();
215 POSIXThread::WillResume(lldb::StateType resume_state)
217 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
219 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ") setting thread resume state to %s", __FUNCTION__, GetID(), StateAsCString(resume_state));
220 // TODO: the line below shouldn't really be done, but
221 // the POSIXThread might rely on this so I will leave this in for now
222 SetResumeState(resume_state);
226 POSIXThread::DidStop()
228 // Don't set the thread state to stopped unless we really stopped.
232 POSIXThread::Resume()
234 lldb::StateType resume_state = GetResumeState();
235 ProcessMonitor &monitor = GetMonitor();
238 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
240 log->Printf ("POSIXThread::%s (), resume_state = %s", __FUNCTION__,
241 StateAsCString(resume_state));
243 switch (resume_state)
246 assert(false && "Unexpected state for resume!");
250 case lldb::eStateRunning:
251 SetState(resume_state);
252 status = monitor.Resume(GetID(), GetResumeSignal());
255 case lldb::eStateStepping:
256 SetState(resume_state);
257 status = monitor.SingleStep(GetID(), GetResumeSignal());
259 case lldb::eStateStopped:
260 case lldb::eStateSuspended:
269 POSIXThread::Notify(const ProcessMessage &message)
271 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
273 log->Printf ("POSIXThread::%s () message kind = '%s' for tid %" PRIu64,
274 __FUNCTION__, message.PrintKind(), GetID());
276 switch (message.GetKind())
279 assert(false && "Unexpected message kind!");
282 case ProcessMessage::eExitMessage:
283 // Nothing to be done.
286 case ProcessMessage::eLimboMessage:
287 LimboNotify(message);
290 case ProcessMessage::eSignalMessage:
291 SignalNotify(message);
294 case ProcessMessage::eSignalDeliveredMessage:
295 SignalDeliveredNotify(message);
298 case ProcessMessage::eTraceMessage:
299 TraceNotify(message);
302 case ProcessMessage::eBreakpointMessage:
303 BreakNotify(message);
306 case ProcessMessage::eWatchpointMessage:
307 WatchNotify(message);
310 case ProcessMessage::eCrashMessage:
311 CrashNotify(message);
314 case ProcessMessage::eNewThreadMessage:
315 ThreadNotify(message);
321 POSIXThread::EnableHardwareWatchpoint(Watchpoint *wp)
326 addr_t wp_addr = wp->GetLoadAddress();
327 size_t wp_size = wp->GetByteSize();
328 bool wp_read = wp->WatchpointRead();
329 bool wp_write = wp->WatchpointWrite();
330 uint32_t wp_hw_index = wp->GetHardwareIndex();
331 RegisterContextPOSIX* reg_ctx = GetRegisterContextPOSIX();
333 wp_set = reg_ctx->SetHardwareWatchpointWithIndex(wp_addr, wp_size,
341 POSIXThread::DisableHardwareWatchpoint(Watchpoint *wp)
346 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
347 if (reg_ctx_sp.get())
348 result = reg_ctx_sp->ClearHardwareWatchpoint(wp->GetHardwareIndex());
354 POSIXThread::NumSupportedHardwareWatchpoints()
356 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
357 if (reg_ctx_sp.get())
358 return reg_ctx_sp->NumSupportedHardwareWatchpoints();
363 POSIXThread::FindVacantWatchpointIndex()
365 uint32_t hw_index = LLDB_INVALID_INDEX32;
366 uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
368 RegisterContextPOSIX* reg_ctx = GetRegisterContextPOSIX();
371 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
373 if (reg_ctx->IsWatchpointVacant(wp_idx))
384 POSIXThread::BreakNotify(const ProcessMessage &message)
387 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
389 assert(GetRegisterContext());
390 status = GetRegisterContextPOSIX()->UpdateAfterBreakpoint();
391 assert(status && "Breakpoint update failed!");
393 // With our register state restored, resolve the breakpoint object
394 // corresponding to our current PC.
395 assert(GetRegisterContext());
396 lldb::addr_t pc = GetRegisterContext()->GetPC();
398 log->Printf ("POSIXThread::%s () PC=0x%8.8" PRIx64, __FUNCTION__, pc);
399 lldb::BreakpointSiteSP bp_site(GetProcess()->GetBreakpointSiteList().FindByAddress(pc));
401 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
402 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
403 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
404 if (bp_site && bp_site->ValidForThisThread(this))
406 lldb::break_id_t bp_id = bp_site->GetID();
407 if (GetProcess()->GetThreadList().SetSelectedThreadByID(GetID()))
408 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id));
410 assert(false && "Invalid thread ID during BreakNotify.");
414 const ThreadSpec *spec = bp_site ?
415 bp_site->GetOwnerAtIndex(0)->GetOptionsNoCreate()->GetThreadSpecNoCreate() : 0;
417 if (spec && spec->TIDMatches(*this))
418 assert(false && "BreakpointSite is invalid for the current ThreadSpec.");
421 if (!m_stop_info_sp) {
422 StopInfoSP invalid_stop_info_sp;
423 SetStopInfo (invalid_stop_info_sp);
430 POSIXThread::WatchNotify(const ProcessMessage &message)
432 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
434 lldb::addr_t halt_addr = message.GetHWAddress();
436 log->Printf ("POSIXThread::%s () Hardware Watchpoint Address = 0x%8.8"
437 PRIx64, __FUNCTION__, halt_addr);
439 RegisterContextPOSIX* reg_ctx = GetRegisterContextPOSIX();
442 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
444 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
446 if (reg_ctx->IsWatchpointHit(wp_idx))
448 // Clear the watchpoint hit here
449 reg_ctx->ClearWatchpointHits();
454 if (wp_idx == num_hw_wps)
457 Target &target = GetProcess()->GetTarget();
458 lldb::addr_t wp_monitor_addr = reg_ctx->GetWatchpointAddress(wp_idx);
459 const WatchpointList &wp_list = target.GetWatchpointList();
460 lldb::WatchpointSP wp_sp = wp_list.FindByAddress(wp_monitor_addr);
462 assert(wp_sp.get() && "No watchpoint found");
463 SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID(*this,
469 POSIXThread::TraceNotify(const ProcessMessage &message)
471 SetStopInfo (StopInfo::CreateStopReasonToTrace(*this));
475 POSIXThread::LimboNotify(const ProcessMessage &message)
477 SetStopInfo (lldb::StopInfoSP(new POSIXLimboStopInfo(*this)));
481 POSIXThread::SignalNotify(const ProcessMessage &message)
483 int signo = message.GetSignal();
485 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
486 SetResumeSignal(signo);
490 POSIXThread::SignalDeliveredNotify(const ProcessMessage &message)
492 int signo = message.GetSignal();
494 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
495 SetResumeSignal(signo);
499 POSIXThread::CrashNotify(const ProcessMessage &message)
501 // FIXME: Update stop reason as per bugzilla 14598
502 int signo = message.GetSignal();
504 assert(message.GetKind() == ProcessMessage::eCrashMessage);
506 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
508 log->Printf ("POSIXThread::%s () signo = %i, reason = '%s'",
509 __FUNCTION__, signo, message.PrintCrashReason());
511 SetStopInfo (lldb::StopInfoSP(new POSIXCrashStopInfo(*this, signo,
512 message.GetCrashReason(),
513 message.GetFaultAddress())));
514 SetResumeSignal(signo);
518 POSIXThread::ThreadNotify(const ProcessMessage &message)
520 SetStopInfo (lldb::StopInfoSP(new POSIXNewThreadStopInfo(*this)));
524 POSIXThread::GetRegisterIndexFromOffset(unsigned offset)
526 unsigned reg = LLDB_INVALID_REGNUM;
527 ArchSpec arch = Host::GetArchitecture();
529 switch (arch.GetCore())
532 llvm_unreachable("CPU type not supported!");
535 case ArchSpec::eCore_x86_32_i386:
536 case ArchSpec::eCore_x86_32_i486:
537 case ArchSpec::eCore_x86_32_i486sx:
538 case ArchSpec::eCore_x86_64_x86_64:
540 RegisterContextSP base = GetRegisterContext();
542 RegisterContextPOSIX &context = static_cast<RegisterContextPOSIX &>(*base);
543 reg = context.GetRegisterIndexFromOffset(offset);
552 POSIXThread::GetRegisterName(unsigned reg)
554 const char * name = nullptr;
555 ArchSpec arch = Host::GetArchitecture();
557 switch (arch.GetCore())
560 assert(false && "CPU type not supported!");
563 case ArchSpec::eCore_x86_32_i386:
564 case ArchSpec::eCore_x86_32_i486:
565 case ArchSpec::eCore_x86_32_i486sx:
566 case ArchSpec::eCore_x86_64_x86_64:
567 name = GetRegisterContext()->GetRegisterName(reg);
574 POSIXThread::GetRegisterNameFromOffset(unsigned offset)
576 return GetRegisterName(GetRegisterIndexFromOffset(offset));