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