1a63d08c9SGreg Clayton //===-- ThreadKDP.cpp -------------------------------------*- C++ -*-===// 2a63d08c9SGreg Clayton // 3a63d08c9SGreg Clayton // The LLVM Compiler Infrastructure 4a63d08c9SGreg Clayton // 5a63d08c9SGreg Clayton // This file is distributed under the University of Illinois Open Source 6a63d08c9SGreg Clayton // License. See LICENSE.TXT for details. 7a63d08c9SGreg Clayton // 8a63d08c9SGreg Clayton //===----------------------------------------------------------------------===// 9a63d08c9SGreg Clayton 10a63d08c9SGreg Clayton 11a63d08c9SGreg Clayton #include "ThreadKDP.h" 12a63d08c9SGreg Clayton 13a63d08c9SGreg Clayton #include "llvm/Support/MachO.h" 14a63d08c9SGreg Clayton 15a63d08c9SGreg Clayton #include "lldb/Core/ArchSpec.h" 16a63d08c9SGreg Clayton #include "lldb/Core/DataExtractor.h" 17a63d08c9SGreg Clayton #include "lldb/Core/StreamString.h" 18a63d08c9SGreg Clayton #include "lldb/Core/State.h" 19a63d08c9SGreg Clayton #include "lldb/Target/Process.h" 20a63d08c9SGreg Clayton #include "lldb/Target/RegisterContext.h" 21a63d08c9SGreg Clayton #include "lldb/Target/StopInfo.h" 22a63d08c9SGreg Clayton #include "lldb/Target/Target.h" 23a63d08c9SGreg Clayton #include "lldb/Target/Unwind.h" 2401a67860SJohnny Chen #include "lldb/Breakpoint/Watchpoint.h" 25a63d08c9SGreg Clayton 26a63d08c9SGreg Clayton #include "ProcessKDP.h" 27a63d08c9SGreg Clayton #include "ProcessKDPLog.h" 28a63d08c9SGreg Clayton #include "RegisterContextKDP_arm.h" 29a63d08c9SGreg Clayton #include "RegisterContextKDP_i386.h" 30a63d08c9SGreg Clayton #include "RegisterContextKDP_x86_64.h" 31a63d08c9SGreg Clayton 32a63d08c9SGreg Clayton using namespace lldb; 33a63d08c9SGreg Clayton using namespace lldb_private; 34a63d08c9SGreg Clayton 35a63d08c9SGreg Clayton //---------------------------------------------------------------------- 36a63d08c9SGreg Clayton // Thread Registers 37a63d08c9SGreg Clayton //---------------------------------------------------------------------- 38a63d08c9SGreg Clayton 39a63d08c9SGreg Clayton ThreadKDP::ThreadKDP (ProcessKDP &process, lldb::tid_t tid) : 40a63d08c9SGreg Clayton Thread(process, tid), 41a63d08c9SGreg Clayton m_thread_name (), 42a63d08c9SGreg Clayton m_dispatch_queue_name (), 43a63d08c9SGreg Clayton m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS) 44a63d08c9SGreg Clayton { 45a63d08c9SGreg Clayton ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::ThreadKDP (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID()); 46a63d08c9SGreg Clayton } 47a63d08c9SGreg Clayton 48a63d08c9SGreg Clayton ThreadKDP::~ThreadKDP () 49a63d08c9SGreg Clayton { 50a63d08c9SGreg Clayton ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::~ThreadKDP (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID()); 51a63d08c9SGreg Clayton DestroyThread(); 52a63d08c9SGreg Clayton } 53a63d08c9SGreg Clayton 54a63d08c9SGreg Clayton const char * 55a63d08c9SGreg Clayton ThreadKDP::GetName () 56a63d08c9SGreg Clayton { 57a63d08c9SGreg Clayton if (m_thread_name.empty()) 58a63d08c9SGreg Clayton return NULL; 59a63d08c9SGreg Clayton return m_thread_name.c_str(); 60a63d08c9SGreg Clayton } 61a63d08c9SGreg Clayton 62a63d08c9SGreg Clayton const char * 63a63d08c9SGreg Clayton ThreadKDP::GetQueueName () 64a63d08c9SGreg Clayton { 65a63d08c9SGreg Clayton return NULL; 66a63d08c9SGreg Clayton } 67a63d08c9SGreg Clayton 68a63d08c9SGreg Clayton bool 69a63d08c9SGreg Clayton ThreadKDP::WillResume (StateType resume_state) 70a63d08c9SGreg Clayton { 71a63d08c9SGreg Clayton ClearStackFrames(); 72a63d08c9SGreg Clayton // Call the Thread::WillResume first. If we stop at a signal, the stop info 73a63d08c9SGreg Clayton // class for signal will set the resume signal that we need below. The signal 74a63d08c9SGreg Clayton // stuff obeys the Process::UnixSignal defaults. 75a63d08c9SGreg Clayton Thread::WillResume(resume_state); 76a63d08c9SGreg Clayton 77a63d08c9SGreg Clayton lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP)); 78a63d08c9SGreg Clayton if (log) 79*81c22f61SGreg Clayton log->Printf ("Resuming thread: %4.4llx with state: %s.", GetID(), StateAsCString(resume_state)); 80a63d08c9SGreg Clayton 81a63d08c9SGreg Clayton // ProcessKDP &process = GetKDPProcess(); 82a63d08c9SGreg Clayton // switch (resume_state) 83a63d08c9SGreg Clayton // { 84a63d08c9SGreg Clayton // case eStateSuspended: 85a63d08c9SGreg Clayton // case eStateStopped: 86a63d08c9SGreg Clayton // // Don't append anything for threads that should stay stopped. 87a63d08c9SGreg Clayton // break; 88a63d08c9SGreg Clayton // 89a63d08c9SGreg Clayton // case eStateRunning: 90a63d08c9SGreg Clayton // case eStateStepping: 91a63d08c9SGreg Clayton // break; 92a63d08c9SGreg Clayton // 93a63d08c9SGreg Clayton // default: 94a63d08c9SGreg Clayton // break; 95a63d08c9SGreg Clayton // } 96a63d08c9SGreg Clayton return true; 97a63d08c9SGreg Clayton } 98a63d08c9SGreg Clayton 99a63d08c9SGreg Clayton void 100a63d08c9SGreg Clayton ThreadKDP::RefreshStateAfterStop() 101a63d08c9SGreg Clayton { 102a63d08c9SGreg Clayton // Invalidate all registers in our register context. We don't set "force" to 103a63d08c9SGreg Clayton // true because the stop reply packet might have had some register values 104a63d08c9SGreg Clayton // that were expedited and these will already be copied into the register 105a63d08c9SGreg Clayton // context by the time this function gets called. The KDPRegisterContext 106a63d08c9SGreg Clayton // class has been made smart enough to detect when it needs to invalidate 107a63d08c9SGreg Clayton // which registers are valid by putting hooks in the register read and 108a63d08c9SGreg Clayton // register supply functions where they check the process stop ID and do 109a63d08c9SGreg Clayton // the right thing. 110a63d08c9SGreg Clayton const bool force = false; 111a63d08c9SGreg Clayton GetRegisterContext()->InvalidateIfNeeded (force); 112a63d08c9SGreg Clayton } 113a63d08c9SGreg Clayton 114a63d08c9SGreg Clayton void 115a63d08c9SGreg Clayton ThreadKDP::ClearStackFrames () 116a63d08c9SGreg Clayton { 117a63d08c9SGreg Clayton Unwind *unwinder = GetUnwinder (); 118a63d08c9SGreg Clayton if (unwinder) 119a63d08c9SGreg Clayton unwinder->Clear(); 120a63d08c9SGreg Clayton Thread::ClearStackFrames(); 121a63d08c9SGreg Clayton } 122a63d08c9SGreg Clayton 123a63d08c9SGreg Clayton 124a63d08c9SGreg Clayton bool 125a63d08c9SGreg Clayton ThreadKDP::ThreadIDIsValid (lldb::tid_t thread) 126a63d08c9SGreg Clayton { 127a63d08c9SGreg Clayton return thread != 0; 128a63d08c9SGreg Clayton } 129a63d08c9SGreg Clayton 130a63d08c9SGreg Clayton void 131a63d08c9SGreg Clayton ThreadKDP::Dump(Log *log, uint32_t index) 132a63d08c9SGreg Clayton { 133a63d08c9SGreg Clayton } 134a63d08c9SGreg Clayton 135a63d08c9SGreg Clayton 136a63d08c9SGreg Clayton bool 137a63d08c9SGreg Clayton ThreadKDP::ShouldStop (bool &step_more) 138a63d08c9SGreg Clayton { 139a63d08c9SGreg Clayton return true; 140a63d08c9SGreg Clayton } 141a63d08c9SGreg Clayton lldb::RegisterContextSP 142a63d08c9SGreg Clayton ThreadKDP::GetRegisterContext () 143a63d08c9SGreg Clayton { 144a63d08c9SGreg Clayton if (m_reg_context_sp.get() == NULL) 145a63d08c9SGreg Clayton m_reg_context_sp = CreateRegisterContextForFrame (NULL); 146a63d08c9SGreg Clayton return m_reg_context_sp; 147a63d08c9SGreg Clayton } 148a63d08c9SGreg Clayton 149a63d08c9SGreg Clayton lldb::RegisterContextSP 150a63d08c9SGreg Clayton ThreadKDP::CreateRegisterContextForFrame (StackFrame *frame) 151a63d08c9SGreg Clayton { 152a63d08c9SGreg Clayton lldb::RegisterContextSP reg_ctx_sp; 153a63d08c9SGreg Clayton uint32_t concrete_frame_idx = 0; 154a63d08c9SGreg Clayton 155a63d08c9SGreg Clayton if (frame) 156a63d08c9SGreg Clayton concrete_frame_idx = frame->GetConcreteFrameIndex (); 157a63d08c9SGreg Clayton 158a63d08c9SGreg Clayton if (concrete_frame_idx == 0) 159a63d08c9SGreg Clayton { 160a63d08c9SGreg Clayton switch (GetKDPProcess().GetCommunication().GetCPUType()) 161a63d08c9SGreg Clayton { 162a63d08c9SGreg Clayton case llvm::MachO::CPUTypeARM: 163a63d08c9SGreg Clayton reg_ctx_sp.reset (new RegisterContextKDP_arm (*this, concrete_frame_idx)); 164a63d08c9SGreg Clayton break; 165a63d08c9SGreg Clayton case llvm::MachO::CPUTypeI386: 166a63d08c9SGreg Clayton reg_ctx_sp.reset (new RegisterContextKDP_i386 (*this, concrete_frame_idx)); 167a63d08c9SGreg Clayton break; 168a63d08c9SGreg Clayton case llvm::MachO::CPUTypeX86_64: 169a63d08c9SGreg Clayton reg_ctx_sp.reset (new RegisterContextKDP_x86_64 (*this, concrete_frame_idx)); 170a63d08c9SGreg Clayton break; 171a63d08c9SGreg Clayton default: 172a63d08c9SGreg Clayton assert (!"Add CPU type support in KDP"); 173a63d08c9SGreg Clayton break; 174a63d08c9SGreg Clayton } 175a63d08c9SGreg Clayton } 176a63d08c9SGreg Clayton else if (m_unwinder_ap.get()) 177a63d08c9SGreg Clayton reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame); 178a63d08c9SGreg Clayton return reg_ctx_sp; 179a63d08c9SGreg Clayton } 180a63d08c9SGreg Clayton 181a63d08c9SGreg Clayton lldb::StopInfoSP 182a63d08c9SGreg Clayton ThreadKDP::GetPrivateStopReason () 183a63d08c9SGreg Clayton { 184a63d08c9SGreg Clayton const uint32_t process_stop_id = GetProcess().GetStopID(); 185a63d08c9SGreg Clayton if (m_thread_stop_reason_stop_id != process_stop_id || 186a63d08c9SGreg Clayton (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid())) 187a63d08c9SGreg Clayton { 188a63d08c9SGreg Clayton // TODO: can we query the initial state of the thread here? 189a63d08c9SGreg Clayton // For now I am just going to pretend that a SIGSTOP happened. 190a63d08c9SGreg Clayton 191a63d08c9SGreg Clayton SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP)); 192a63d08c9SGreg Clayton 193a63d08c9SGreg Clayton // If GetKDPProcess().SetThreadStopInfo() doesn't find a stop reason 194a63d08c9SGreg Clayton // for this thread, then m_actual_stop_info_sp will not ever contain 195a63d08c9SGreg Clayton // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false" 196a63d08c9SGreg Clayton // check will never be able to tell us if we have the correct stop info 197a63d08c9SGreg Clayton // for this thread and we will continually send qThreadStopInfo packets 198a63d08c9SGreg Clayton // down to the remote KDP server, so we need to keep our own notion 199a63d08c9SGreg Clayton // of the stop ID that m_actual_stop_info_sp is valid for (even if it 200a63d08c9SGreg Clayton // contains nothing). We use m_thread_stop_reason_stop_id for this below. 201a63d08c9SGreg Clayton // m_thread_stop_reason_stop_id = process_stop_id; 202a63d08c9SGreg Clayton // m_actual_stop_info_sp.reset(); 203a63d08c9SGreg Clayton 204a63d08c9SGreg Clayton } 205a63d08c9SGreg Clayton return m_actual_stop_info_sp; 206a63d08c9SGreg Clayton } 207a63d08c9SGreg Clayton 208a63d08c9SGreg Clayton 209