LLDB mainline
ThreadKDP.cpp
Go to the documentation of this file.
1//===-- ThreadKDP.cpp -----------------------------------------------------===//
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"
17#include "lldb/Target/Target.h"
18#include "lldb/Target/Unwind.h"
21#include "lldb/Utility/State.h"
23
25#include "ProcessKDP.h"
26#include "ProcessKDPLog.h"
30
31#include <memory>
32
33using namespace lldb;
34using namespace lldb_private;
35
36// Thread Registers
37
39 : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
42 LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID());
43}
44
47 LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID());
49}
50
51const char *ThreadKDP::GetName() {
52 if (m_thread_name.empty())
53 return nullptr;
54 return m_thread_name.c_str();
55}
56
57const char *ThreadKDP::GetQueueName() { return nullptr; }
58
60 // Invalidate all registers in our register context. We don't set "force" to
61 // true because the stop reply packet might have had some register values
62 // that were expedited and these will already be copied into the register
63 // context by the time this function gets called. The KDPRegisterContext
64 // class has been made smart enough to detect when it needs to invalidate
65 // which registers are valid by putting hooks in the register read and
66 // register supply functions where they check the process stop ID and do the
67 // right thing.
68 const bool force = false;
70 if (reg_ctx_sp)
71 reg_ctx_sp->InvalidateIfNeeded(force);
72}
73
74bool ThreadKDP::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
75
76void ThreadKDP::Dump(Log *log, uint32_t index) {}
77
78bool ThreadKDP::ShouldStop(bool &step_more) { return true; }
84
87 lldb::RegisterContextSP reg_ctx_sp;
88 uint32_t concrete_frame_idx = 0;
89
90 if (frame)
91 concrete_frame_idx = frame->GetConcreteFrameIndex();
92
93 if (concrete_frame_idx == 0) {
94 ProcessSP process_sp(CalculateProcess());
95 if (process_sp) {
96 switch (static_cast<ProcessKDP *>(process_sp.get())
97 ->GetCommunication()
98 .GetCPUType()) {
99 case llvm::MachO::CPU_TYPE_ARM:
100 reg_ctx_sp =
101 std::make_shared<RegisterContextKDP_arm>(*this, concrete_frame_idx);
102 break;
103 case llvm::MachO::CPU_TYPE_ARM64:
104 reg_ctx_sp = std::make_shared<RegisterContextKDP_arm64>(
105 *this, concrete_frame_idx);
106 break;
107 case llvm::MachO::CPU_TYPE_X86_64:
108 reg_ctx_sp = std::make_shared<RegisterContextKDP_x86_64>(
109 *this, concrete_frame_idx);
110 break;
111 default:
112 llvm_unreachable("Add CPU type support in KDP");
113 }
114 }
115 } else {
116 reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
117 }
118 return reg_ctx_sp;
119}
120
122 ProcessSP process_sp(GetProcess());
123 if (process_sp) {
126 } else {
128 }
129 return true;
130 }
131 return false;
132}
133
135 const DataExtractor &exc_reply_packet) {
136 lldb::offset_t offset = 0;
137 uint8_t reply_command = exc_reply_packet.GetU8(&offset);
138 if (reply_command == CommunicationKDP::KDP_EXCEPTION) {
139 offset = 8;
140 const uint32_t count = exc_reply_packet.GetU32(&offset);
141 if (count >= 1) {
142 // const uint32_t cpu = exc_reply_packet.GetU32 (&offset);
143 offset += 4; // Skip the useless CPU field
144 const uint32_t exc_type = exc_reply_packet.GetU32(&offset);
145 const uint32_t exc_code = exc_reply_packet.GetU32(&offset);
146 const uint32_t exc_subcode = exc_reply_packet.GetU32(&offset);
147 // We have to make a copy of the stop info because the thread list will
148 // iterate through the threads and clear all stop infos..
149
150 // Let the StopInfoMachException::CreateStopReasonWithMachException()
151 // function update the PC if needed as we might hit a software breakpoint
152 // and need to decrement the PC (i386 and x86_64 need this) and KDP
153 // doesn't do this for us.
154 const bool pc_already_adjusted = false;
155 const bool adjust_pc_if_needed = true;
156
159 *this, exc_type, 2, exc_code, exc_subcode, 0, pc_already_adjusted,
160 adjust_pc_if_needed);
161 }
162 }
163}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
std::string m_dispatch_queue_name
Definition ThreadKDP.h:65
bool ShouldStop(bool &step_more)
Definition ThreadKDP.cpp:78
lldb::RegisterContextSP GetRegisterContext() override
Definition ThreadKDP.cpp:79
const char * GetQueueName() override
Retrieve the Queue name for the queue currently using this Thread.
Definition ThreadKDP.cpp:57
ThreadKDP(lldb_private::Process &process, lldb::tid_t tid)
Definition ThreadKDP.cpp:38
~ThreadKDP() override
Definition ThreadKDP.cpp:45
lldb::StopInfoSP m_cached_stop_info_sp
Definition ThreadKDP.h:67
friend class ProcessKDP
Definition ThreadKDP.h:61
void Dump(lldb_private::Log *log, uint32_t index)
Definition ThreadKDP.cpp:76
std::string m_thread_name
Definition ThreadKDP.h:64
void RefreshStateAfterStop() override
Definition ThreadKDP.cpp:59
lldb::addr_t m_thread_dispatch_qaddr
Definition ThreadKDP.h:66
lldb::RegisterContextSP CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override
Definition ThreadKDP.cpp:86
static bool ThreadIDIsValid(lldb::tid_t thread)
Definition ThreadKDP.cpp:74
void SetStopInfoFrom_KDP_EXCEPTION(const lldb_private::DataExtractor &exc_reply_packet)
const char * GetName() override
Definition ThreadKDP.cpp:51
bool CalculateStopInfo() override
Ask the thread subclass to set its stop info.
An data extractor class.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
A plug-in interface definition class for debugging a process.
Definition Process.h:357
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
Definition StackFrame.h:455
static lldb::StopInfoSP CreateStopReasonWithMachException(Thread &thread, uint32_t exc_type, uint32_t exc_data_count, uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code, bool pc_already_adjusted=true, bool adjust_pc_if_needed=false)
static lldb::StopInfoSP CreateStopReasonWithSignal(Thread &thread, int signo, const char *description=nullptr, std::optional< int > code=std::nullopt)
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition Thread.cpp:464
lldb::ProcessSP CalculateProcess() override
Definition Thread.cpp:1429
virtual void DestroyThread()
Definition Thread.cpp:252
virtual Unwind & GetUnwinder()
Definition Thread.cpp:1934
Thread(Process &process, lldb::tid_t tid, bool use_invalid_index_id=false)
Constructor.
Definition Thread.cpp:219
lldb::ProcessSP GetProcess() const
Definition Thread.h:158
friend class StackFrame
Definition Thread.h:1303
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread's current register state.
Definition Thread.h:1370
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition Unwind.h:56
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
uint64_t tid_t
Definition lldb-types.h:84
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition UserID.h:47
#define SIGSTOP