LLDB  mainline
ThreadKDP.cpp
Go to the documentation of this file.
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 
14 #include "lldb/Target/Process.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"
21 #include "lldb/Utility/State.h"
23 
25 #include "ProcessKDP.h"
26 #include "ProcessKDPLog.h"
27 #include "RegisterContextKDP_arm.h"
31 
32 #include <memory>
33 
34 using namespace lldb;
35 using namespace lldb_private;
36 
37 // Thread Registers
38 
40  : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
41  m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS) {
43  LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID());
44 }
45 
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 
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)
83  return m_reg_context_sp;
84 }
85 
86 lldb::RegisterContextSP
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 =
102  std::make_shared<RegisterContextKDP_arm>(*this, concrete_frame_idx);
103  break;
105  reg_ctx_sp = std::make_shared<RegisterContextKDP_arm64>(
106  *this, concrete_frame_idx);
107  break;
108  case llvm::MachO::CPU_TYPE_I386:
109  reg_ctx_sp = std::make_shared<RegisterContextKDP_i386>(
110  *this, concrete_frame_idx);
111  break;
112  case llvm::MachO::CPU_TYPE_X86_64:
113  reg_ctx_sp = std::make_shared<RegisterContextKDP_x86_64>(
114  *this, concrete_frame_idx);
115  break;
116  default:
117  llvm_unreachable("Add CPU type support in KDP");
118  }
119  }
120  } else {
121  Unwind *unwinder = GetUnwinder();
122  if (unwinder != nullptr)
123  reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
124  }
125  return reg_ctx_sp;
126 }
127 
129  ProcessSP process_sp(GetProcess());
130  if (process_sp) {
131  if (m_cached_stop_info_sp) {
133  } else {
134  SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, SIGSTOP));
135  }
136  return true;
137  }
138  return false;
139 }
140 
142  const DataExtractor &exc_reply_packet) {
143  lldb::offset_t offset = 0;
144  uint8_t reply_command = exc_reply_packet.GetU8(&offset);
145  if (reply_command == CommunicationKDP::KDP_EXCEPTION) {
146  offset = 8;
147  const uint32_t count = exc_reply_packet.GetU32(&offset);
148  if (count >= 1) {
149  // const uint32_t cpu = exc_reply_packet.GetU32 (&offset);
150  offset += 4; // Skip the useless CPU field
151  const uint32_t exc_type = exc_reply_packet.GetU32(&offset);
152  const uint32_t exc_code = exc_reply_packet.GetU32(&offset);
153  const uint32_t exc_subcode = exc_reply_packet.GetU32(&offset);
154  // We have to make a copy of the stop info because the thread list will
155  // iterate through the threads and clear all stop infos..
156 
157  // Let the StopInfoMachException::CreateStopReasonWithMachException()
158  // function update the PC if needed as we might hit a software breakpoint
159  // and need to decrement the PC (i386 and x86_64 need this) and KDP
160  // doesn't do this for us.
161  const bool pc_already_adjusted = false;
162  const bool adjust_pc_if_needed = true;
163 
165  StopInfoMachException::CreateStopReasonWithMachException(
166  *this, exc_type, 2, exc_code, exc_subcode, 0, pc_already_adjusted,
167  adjust_pc_if_needed);
168  }
169  }
170 }
An data extractor class.
Definition: DataExtractor.h:47
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
lldb::StopInfoSP m_cached_stop_info_sp
Definition: ThreadKDP.h:67
virtual void RefreshStateAfterStop()
Definition: ThreadKDP.cpp:60
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:49
void SetStopInfoFrom_KDP_EXCEPTION(const lldb_private::DataExtractor &exc_reply_packet)
Definition: ThreadKDP.cpp:141
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread&#39;s current register state.
Definition: Thread.h:1220
#define LLDB_LOG(log,...)
Definition: Log.h:209
#define SIGSTOP
virtual lldb_private::Unwind * GetUnwinder()
Definition: Thread.cpp:2056
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:55
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
virtual const char * GetName()
Definition: ThreadKDP.cpp:52
uint64_t offset_t
Definition: lldb-types.h:87
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition: Thread.cpp:477
static bool ThreadIDIsValid(lldb::tid_t thread)
Definition: ThreadKDP.cpp:75
ThreadKDP(lldb_private::Process &process, lldb::tid_t tid)
Definition: ThreadKDP.cpp:39
uint64_t tid_t
Definition: lldb-types.h:86
std::string m_thread_name
Definition: ThreadKDP.h:64
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
virtual void DestroyThread()
Definition: Thread.cpp:278
lldb::ProcessSP CalculateProcess() override
Definition: Thread.cpp:1592
virtual lldb::RegisterContextSP CreateRegisterContextForFrame(lldb_private::StackFrame *frame)
Definition: ThreadKDP.cpp:87
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
#define KDP_LOG_THREAD
Definition: ProcessKDPLog.h:15
Definition: SBAddress.h:15
virtual ~ThreadKDP()
Definition: ThreadKDP.cpp:46
void Dump(lldb_private::Log *log, uint32_t index)
Definition: ThreadKDP.cpp:77
virtual lldb::RegisterContextSP GetRegisterContext()
Definition: ThreadKDP.cpp:80
#define CPU_TYPE_ARM64
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
bool ShouldStop(bool &step_more)
Definition: ThreadKDP.cpp:79
virtual bool CalculateStopInfo()
Definition: ThreadKDP.cpp:128
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread&#39;s StackFrameList, not counting inlined frame...
Definition: StackFrame.h:389
This base class provides an interface to stack frames.
Definition: StackFrame.h:40
virtual const char * GetQueueName()
Retrieve the Queue name for the queue currently using this Thread.
Definition: ThreadKDP.cpp:58