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 //===----------------------------------------------------------------------===//
14 // Other libraries and framework includes
16 #include "lldb/Breakpoint/Watchpoint.h"
17 #include "lldb/Breakpoint/BreakpointLocation.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/State.h"
20 #include "lldb/Host/Host.h"
21 #include "lldb/Host/HostNativeThread.h"
22 #include "lldb/Host/HostInfo.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 "llvm/ADT/SmallString.h"
28 #include "POSIXStopInfo.h"
29 #include "POSIXThread.h"
30 #include "ProcessPOSIX.h"
31 #include "ProcessPOSIXLog.h"
32 #include "ProcessMonitor.h"
33 #include "RegisterContextPOSIXProcessMonitor_arm.h"
34 #include "RegisterContextPOSIXProcessMonitor_arm64.h"
35 #include "RegisterContextPOSIXProcessMonitor_mips64.h"
36 #include "RegisterContextPOSIXProcessMonitor_powerpc.h"
37 #include "RegisterContextPOSIXProcessMonitor_x86.h"
38 #include "Plugins/Process/Utility/RegisterContextFreeBSD_arm.h"
39 #include "Plugins/Process/Utility/RegisterContextFreeBSD_arm64.h"
40 #include "Plugins/Process/Utility/RegisterContextFreeBSD_i386.h"
41 #include "Plugins/Process/Utility/RegisterContextFreeBSD_mips64.h"
42 #include "Plugins/Process/Utility/RegisterContextFreeBSD_powerpc.h"
43 #include "Plugins/Process/Utility/RegisterContextFreeBSD_x86_64.h"
44 #include "Plugins/Process/Utility/UnwindLLDB.h"
47 using namespace lldb_private;
50 POSIXThread::POSIXThread(Process &process, lldb::tid_t tid)
51 : Thread(process, tid),
54 m_thread_name_valid (false),
58 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
59 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
60 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ")", __FUNCTION__, tid);
62 // Set the current watchpoints for this thread.
63 Target &target = GetProcess()->GetTarget();
64 const WatchpointList &wp_list = target.GetWatchpointList();
65 size_t wp_size = wp_list.GetSize();
67 for (uint32_t wp_idx = 0; wp_idx < wp_size; wp_idx++)
69 lldb::WatchpointSP wp = wp_list.GetByIndex(wp_idx);
70 if (wp.get() && wp->IsEnabled())
72 // This watchpoint as been enabled; obviously this "new" thread
73 // has been created since that watchpoint was enabled. Since
74 // the POSIXBreakpointProtocol has yet to be initialized, its
75 // m_watchpoints_initialized member will be FALSE. Attempting to
76 // read the debug status register to determine if a watchpoint
77 // has been hit would result in the zeroing of that register.
78 // Since the active debug registers would have been cloned when
79 // this thread was created, simply force the m_watchpoints_initized
80 // member to TRUE and avoid resetting dr6 and dr7.
81 GetPOSIXBreakpointProtocol()->ForceWatchpointsInitialized();
86 POSIXThread::~POSIXThread()
92 POSIXThread::GetMonitor()
94 ProcessSP base = GetProcess();
95 ProcessPOSIX &process = static_cast<ProcessPOSIX&>(*base);
96 return process.GetMonitor();
100 POSIXThread::RefreshStateAfterStop()
102 // Invalidate all registers in our register context. We don't set "force" to
103 // true because the stop reply packet might have had some register values
104 // that were expedited and these will already be copied into the register
105 // context by the time this function gets called. The KDPRegisterContext
106 // class has been made smart enough to detect when it needs to invalidate
107 // which registers are valid by putting hooks in the register read and
108 // register supply functions where they check the process stop ID and do
110 //if (StateIsStoppedState(GetState())
112 const bool force = false;
113 GetRegisterContext()->InvalidateIfNeeded (force);
118 POSIXThread::GetInfo()
124 POSIXThread::SetName (const char *name)
126 m_thread_name_valid = (name && name[0]);
127 if (m_thread_name_valid)
128 m_thread_name.assign (name);
130 m_thread_name.clear();
134 POSIXThread::GetName ()
136 if (!m_thread_name_valid)
138 llvm::SmallString<32> thread_name;
139 HostNativeThread::GetName(GetID(), thread_name);
140 m_thread_name = thread_name.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.GetTriple().getOS())
161 case 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);
196 assert(reg_interface && "OS or CPU not supported!");
198 switch (target_arch.GetMachine())
200 case llvm::Triple::aarch64:
202 RegisterContextPOSIXProcessMonitor_arm64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_arm64(*this, 0, reg_interface);
203 m_posix_thread = reg_ctx;
204 m_reg_context_sp.reset(reg_ctx);
207 case llvm::Triple::arm:
209 RegisterContextPOSIXProcessMonitor_arm *reg_ctx = new RegisterContextPOSIXProcessMonitor_arm(*this, 0, reg_interface);
210 m_posix_thread = reg_ctx;
211 m_reg_context_sp.reset(reg_ctx);
214 case llvm::Triple::mips64:
216 RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
217 m_posix_thread = reg_ctx;
218 m_reg_context_sp.reset(reg_ctx);
221 case llvm::Triple::ppc:
222 case llvm::Triple::ppc64:
224 RegisterContextPOSIXProcessMonitor_powerpc *reg_ctx = new RegisterContextPOSIXProcessMonitor_powerpc(*this, 0, reg_interface);
225 m_posix_thread = reg_ctx;
226 m_reg_context_sp.reset(reg_ctx);
229 case llvm::Triple::x86:
230 case llvm::Triple::x86_64:
232 RegisterContextPOSIXProcessMonitor_x86_64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_x86_64(*this, 0, reg_interface);
233 m_posix_thread = reg_ctx;
234 m_reg_context_sp.reset(reg_ctx);
241 return m_reg_context_sp;
244 lldb::RegisterContextSP
245 POSIXThread::CreateRegisterContextForFrame(lldb_private::StackFrame *frame)
247 lldb::RegisterContextSP reg_ctx_sp;
248 uint32_t concrete_frame_idx = 0;
250 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
251 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
252 log->Printf ("POSIXThread::%s ()", __FUNCTION__);
255 concrete_frame_idx = frame->GetConcreteFrameIndex();
257 if (concrete_frame_idx == 0)
258 reg_ctx_sp = GetRegisterContext();
261 assert(GetUnwinder());
262 reg_ctx_sp = GetUnwinder()->CreateRegisterContextForFrame(frame);
269 POSIXThread::GetThreadPointer ()
271 ProcessMonitor &monitor = GetMonitor();
273 if (monitor.ReadThreadPointer (GetID(), addr))
276 return LLDB_INVALID_ADDRESS;
280 POSIXThread::CalculateStopInfo()
282 SetStopInfo (m_stop_info_sp);
287 POSIXThread::GetUnwinder()
289 if (m_unwinder_ap.get() == NULL)
290 m_unwinder_ap.reset(new UnwindLLDB(*this));
292 return m_unwinder_ap.get();
296 POSIXThread::DidStop()
298 // Don't set the thread state to stopped unless we really stopped.
302 POSIXThread::Resume()
304 lldb::StateType resume_state = GetResumeState();
305 ProcessMonitor &monitor = GetMonitor();
308 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
310 log->Printf ("POSIXThread::%s (), resume_state = %s", __FUNCTION__,
311 StateAsCString(resume_state));
313 switch (resume_state)
316 assert(false && "Unexpected state for resume!");
320 case lldb::eStateRunning:
321 SetState(resume_state);
322 status = monitor.Resume(GetID(), GetResumeSignal());
325 case lldb::eStateStepping:
326 SetState(resume_state);
327 status = monitor.SingleStep(GetID(), GetResumeSignal());
329 case lldb::eStateStopped:
330 case lldb::eStateSuspended:
339 POSIXThread::Notify(const ProcessMessage &message)
341 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
343 log->Printf ("POSIXThread::%s () message kind = '%s' for tid %" PRIu64,
344 __FUNCTION__, message.PrintKind(), GetID());
346 switch (message.GetKind())
349 assert(false && "Unexpected message kind!");
352 case ProcessMessage::eExitMessage:
353 // Nothing to be done.
356 case ProcessMessage::eLimboMessage:
357 LimboNotify(message);
360 case ProcessMessage::eSignalMessage:
361 SignalNotify(message);
364 case ProcessMessage::eSignalDeliveredMessage:
365 SignalDeliveredNotify(message);
368 case ProcessMessage::eTraceMessage:
369 TraceNotify(message);
372 case ProcessMessage::eBreakpointMessage:
373 BreakNotify(message);
376 case ProcessMessage::eWatchpointMessage:
377 WatchNotify(message);
380 case ProcessMessage::eCrashMessage:
381 CrashNotify(message);
384 case ProcessMessage::eNewThreadMessage:
385 ThreadNotify(message);
388 case ProcessMessage::eExecMessage:
395 POSIXThread::EnableHardwareWatchpoint(Watchpoint *wp)
400 addr_t wp_addr = wp->GetLoadAddress();
401 size_t wp_size = wp->GetByteSize();
402 bool wp_read = wp->WatchpointRead();
403 bool wp_write = wp->WatchpointWrite();
404 uint32_t wp_hw_index = wp->GetHardwareIndex();
405 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
407 wp_set = reg_ctx->SetHardwareWatchpointWithIndex(wp_addr, wp_size,
415 POSIXThread::DisableHardwareWatchpoint(Watchpoint *wp)
420 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
421 if (reg_ctx_sp.get())
422 result = reg_ctx_sp->ClearHardwareWatchpoint(wp->GetHardwareIndex());
428 POSIXThread::NumSupportedHardwareWatchpoints()
430 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
431 if (reg_ctx_sp.get())
432 return reg_ctx_sp->NumSupportedHardwareWatchpoints();
437 POSIXThread::FindVacantWatchpointIndex()
439 uint32_t hw_index = LLDB_INVALID_INDEX32;
440 uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
442 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
445 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
447 if (reg_ctx->IsWatchpointVacant(wp_idx))
458 POSIXThread::BreakNotify(const ProcessMessage &message)
461 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
463 assert(GetRegisterContext());
464 status = GetPOSIXBreakpointProtocol()->UpdateAfterBreakpoint();
465 assert(status && "Breakpoint update failed!");
467 // With our register state restored, resolve the breakpoint object
468 // corresponding to our current PC.
469 assert(GetRegisterContext());
470 lldb::addr_t pc = GetRegisterContext()->GetPC();
472 log->Printf ("POSIXThread::%s () PC=0x%8.8" PRIx64, __FUNCTION__, pc);
473 lldb::BreakpointSiteSP bp_site(GetProcess()->GetBreakpointSiteList().FindByAddress(pc));
475 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
476 // we create a stop reason with should_stop=false. If there is no breakpoint location, then report
477 // an invalid stop reason. We don't need to worry about stepping over the breakpoint here, that will
478 // be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
481 lldb::break_id_t bp_id = bp_site->GetID();
482 // If we have an operating system plug-in, we might have set a thread specific breakpoint using the
483 // operating system thread ID, so we can't make any assumptions about the thread ID so we must always
484 // report the breakpoint regardless of the thread.
485 if (bp_site->ValidForThisThread(this) || GetProcess()->GetOperatingSystem () != NULL)
486 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id));
489 const bool should_stop = false;
490 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id, should_stop));
494 SetStopInfo(StopInfoSP());
498 POSIXThread::WatchNotify(const ProcessMessage &message)
500 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
502 lldb::addr_t halt_addr = message.GetHWAddress();
504 log->Printf ("POSIXThread::%s () Hardware Watchpoint Address = 0x%8.8"
505 PRIx64, __FUNCTION__, halt_addr);
507 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
510 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
512 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
514 if (reg_ctx->IsWatchpointHit(wp_idx))
516 // Clear the watchpoint hit here
517 reg_ctx->ClearWatchpointHits();
522 if (wp_idx == num_hw_wps)
525 Target &target = GetProcess()->GetTarget();
526 lldb::addr_t wp_monitor_addr = reg_ctx->GetWatchpointAddress(wp_idx);
527 const WatchpointList &wp_list = target.GetWatchpointList();
528 lldb::WatchpointSP wp_sp = wp_list.FindByAddress(wp_monitor_addr);
530 assert(wp_sp.get() && "No watchpoint found");
531 SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID(*this,
537 POSIXThread::TraceNotify(const ProcessMessage &message)
539 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
542 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
544 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
546 if (reg_ctx->IsWatchpointHit(wp_idx))
548 WatchNotify(message);
554 SetStopInfo (StopInfo::CreateStopReasonToTrace(*this));
558 POSIXThread::LimboNotify(const ProcessMessage &message)
560 SetStopInfo (lldb::StopInfoSP(new POSIXLimboStopInfo(*this)));
564 POSIXThread::SignalNotify(const ProcessMessage &message)
566 int signo = message.GetSignal();
567 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
571 POSIXThread::SignalDeliveredNotify(const ProcessMessage &message)
573 int signo = message.GetSignal();
574 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
578 POSIXThread::CrashNotify(const ProcessMessage &message)
580 // FIXME: Update stop reason as per bugzilla 14598
581 int signo = message.GetSignal();
583 assert(message.GetKind() == ProcessMessage::eCrashMessage);
585 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
587 log->Printf ("POSIXThread::%s () signo = %i, reason = '%s'",
588 __FUNCTION__, signo, message.PrintCrashReason());
590 SetStopInfo (lldb::StopInfoSP(new POSIXCrashStopInfo(*this, signo,
591 message.GetCrashReason(),
592 message.GetFaultAddress())));
596 POSIXThread::ThreadNotify(const ProcessMessage &message)
598 SetStopInfo (lldb::StopInfoSP(new POSIXNewThreadStopInfo(*this)));
602 POSIXThread::GetRegisterIndexFromOffset(unsigned offset)
604 unsigned reg = LLDB_INVALID_REGNUM;
605 ArchSpec arch = HostInfo::GetArchitecture();
607 switch (arch.GetMachine())
610 llvm_unreachable("CPU type not supported!");
613 case llvm::Triple::aarch64:
614 case llvm::Triple::arm:
615 case llvm::Triple::mips64:
616 case llvm::Triple::ppc:
617 case llvm::Triple::ppc64:
618 case llvm::Triple::x86:
619 case llvm::Triple::x86_64:
621 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
622 reg = reg_ctx->GetRegisterIndexFromOffset(offset);
630 POSIXThread::ExecNotify(const ProcessMessage &message)
632 SetStopInfo (StopInfo::CreateStopReasonWithExec(*this));
636 POSIXThread::GetRegisterName(unsigned reg)
638 const char * name = nullptr;
639 ArchSpec arch = HostInfo::GetArchitecture();
641 switch (arch.GetMachine())
644 assert(false && "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:
654 name = GetRegisterContext()->GetRegisterName(reg);
661 POSIXThread::GetRegisterNameFromOffset(unsigned offset)
663 return GetRegisterName(GetRegisterIndexFromOffset(offset));