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