1 //===-- ThreadKDP.cpp -------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ThreadKDP.h" 10 11 #include "lldb/Host/SafeMachO.h" 12 13 #include "lldb/Breakpoint/Watchpoint.h" 14 #include "lldb/Target/Process.h" 15 #include "lldb/Target/RegisterContext.h" 16 #include "lldb/Target/StopInfo.h" 17 #include "lldb/Target/Target.h" 18 #include "lldb/Target/Unwind.h" 19 #include "lldb/Utility/ArchSpec.h" 20 #include "lldb/Utility/DataExtractor.h" 21 #include "lldb/Utility/State.h" 22 #include "lldb/Utility/StreamString.h" 23 24 #include "Plugins/Process/Utility/StopInfoMachException.h" 25 #include "ProcessKDP.h" 26 #include "ProcessKDPLog.h" 27 #include "RegisterContextKDP_arm.h" 28 #include "RegisterContextKDP_arm64.h" 29 #include "RegisterContextKDP_i386.h" 30 #include "RegisterContextKDP_x86_64.h" 31 32 #include <memory> 33 34 using namespace lldb; 35 using namespace lldb_private; 36 37 //---------------------------------------------------------------------- 38 // Thread Registers 39 //---------------------------------------------------------------------- 40 41 ThreadKDP::ThreadKDP(Process &process, lldb::tid_t tid) 42 : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(), 43 m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS) { 44 Log *log = ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_THREAD); 45 LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID()); 46 } 47 48 ThreadKDP::~ThreadKDP() { 49 Log *log = ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_THREAD); 50 LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID()); 51 DestroyThread(); 52 } 53 54 const char *ThreadKDP::GetName() { 55 if (m_thread_name.empty()) 56 return nullptr; 57 return m_thread_name.c_str(); 58 } 59 60 const char *ThreadKDP::GetQueueName() { return nullptr; } 61 62 void ThreadKDP::RefreshStateAfterStop() { 63 // Invalidate all registers in our register context. We don't set "force" to 64 // true because the stop reply packet might have had some register values 65 // that were expedited and these will already be copied into the register 66 // context by the time this function gets called. The KDPRegisterContext 67 // class has been made smart enough to detect when it needs to invalidate 68 // which registers are valid by putting hooks in the register read and 69 // register supply functions where they check the process stop ID and do the 70 // right thing. 71 const bool force = false; 72 lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext()); 73 if (reg_ctx_sp) 74 reg_ctx_sp->InvalidateIfNeeded(force); 75 } 76 77 bool ThreadKDP::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; } 78 79 void ThreadKDP::Dump(Log *log, uint32_t index) {} 80 81 bool ThreadKDP::ShouldStop(bool &step_more) { return true; } 82 lldb::RegisterContextSP ThreadKDP::GetRegisterContext() { 83 if (!m_reg_context_sp) 84 m_reg_context_sp = CreateRegisterContextForFrame(nullptr); 85 return m_reg_context_sp; 86 } 87 88 lldb::RegisterContextSP 89 ThreadKDP::CreateRegisterContextForFrame(StackFrame *frame) { 90 lldb::RegisterContextSP reg_ctx_sp; 91 uint32_t concrete_frame_idx = 0; 92 93 if (frame) 94 concrete_frame_idx = frame->GetConcreteFrameIndex(); 95 96 if (concrete_frame_idx == 0) { 97 ProcessSP process_sp(CalculateProcess()); 98 if (process_sp) { 99 switch (static_cast<ProcessKDP *>(process_sp.get()) 100 ->GetCommunication() 101 .GetCPUType()) { 102 case llvm::MachO::CPU_TYPE_ARM: 103 reg_ctx_sp = 104 std::make_shared<RegisterContextKDP_arm>(*this, concrete_frame_idx); 105 break; 106 case llvm::MachO::CPU_TYPE_ARM64: 107 reg_ctx_sp = std::make_shared<RegisterContextKDP_arm64>( 108 *this, concrete_frame_idx); 109 break; 110 case llvm::MachO::CPU_TYPE_I386: 111 reg_ctx_sp = std::make_shared<RegisterContextKDP_i386>( 112 *this, concrete_frame_idx); 113 break; 114 case llvm::MachO::CPU_TYPE_X86_64: 115 reg_ctx_sp = std::make_shared<RegisterContextKDP_x86_64>( 116 *this, concrete_frame_idx); 117 break; 118 default: 119 llvm_unreachable("Add CPU type support in KDP"); 120 } 121 } 122 } else { 123 Unwind *unwinder = GetUnwinder(); 124 if (unwinder != nullptr) 125 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame); 126 } 127 return reg_ctx_sp; 128 } 129 130 bool ThreadKDP::CalculateStopInfo() { 131 ProcessSP process_sp(GetProcess()); 132 if (process_sp) { 133 if (m_cached_stop_info_sp) { 134 SetStopInfo(m_cached_stop_info_sp); 135 } else { 136 SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, SIGSTOP)); 137 } 138 return true; 139 } 140 return false; 141 } 142 143 void ThreadKDP::SetStopInfoFrom_KDP_EXCEPTION( 144 const DataExtractor &exc_reply_packet) { 145 lldb::offset_t offset = 0; 146 uint8_t reply_command = exc_reply_packet.GetU8(&offset); 147 if (reply_command == CommunicationKDP::KDP_EXCEPTION) { 148 offset = 8; 149 const uint32_t count = exc_reply_packet.GetU32(&offset); 150 if (count >= 1) { 151 // const uint32_t cpu = exc_reply_packet.GetU32 (&offset); 152 offset += 4; // Skip the useless CPU field 153 const uint32_t exc_type = exc_reply_packet.GetU32(&offset); 154 const uint32_t exc_code = exc_reply_packet.GetU32(&offset); 155 const uint32_t exc_subcode = exc_reply_packet.GetU32(&offset); 156 // We have to make a copy of the stop info because the thread list will 157 // iterate through the threads and clear all stop infos.. 158 159 // Let the StopInfoMachException::CreateStopReasonWithMachException() 160 // function update the PC if needed as we might hit a software breakpoint 161 // and need to decrement the PC (i386 and x86_64 need this) and KDP 162 // doesn't do this for us. 163 const bool pc_already_adjusted = false; 164 const bool adjust_pc_if_needed = true; 165 166 m_cached_stop_info_sp = 167 StopInfoMachException::CreateStopReasonWithMachException( 168 *this, exc_type, 2, exc_code, exc_subcode, 0, pc_already_adjusted, 169 adjust_pc_if_needed); 170 } 171 } 172 } 173