xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- ThreadKDP.cpp -----------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "ThreadKDP.h"
10061da546Spatrick 
11061da546Spatrick #include "lldb/Host/SafeMachO.h"
12061da546Spatrick 
13061da546Spatrick #include "lldb/Breakpoint/Watchpoint.h"
14061da546Spatrick #include "lldb/Target/Process.h"
15061da546Spatrick #include "lldb/Target/RegisterContext.h"
16061da546Spatrick #include "lldb/Target/StopInfo.h"
17061da546Spatrick #include "lldb/Target/Target.h"
18061da546Spatrick #include "lldb/Target/Unwind.h"
19061da546Spatrick #include "lldb/Utility/ArchSpec.h"
20061da546Spatrick #include "lldb/Utility/DataExtractor.h"
21061da546Spatrick #include "lldb/Utility/State.h"
22061da546Spatrick #include "lldb/Utility/StreamString.h"
23061da546Spatrick 
24061da546Spatrick #include "Plugins/Process/Utility/StopInfoMachException.h"
25061da546Spatrick #include "ProcessKDP.h"
26061da546Spatrick #include "ProcessKDPLog.h"
27061da546Spatrick #include "RegisterContextKDP_arm.h"
28061da546Spatrick #include "RegisterContextKDP_arm64.h"
29061da546Spatrick #include "RegisterContextKDP_i386.h"
30061da546Spatrick #include "RegisterContextKDP_x86_64.h"
31061da546Spatrick 
32061da546Spatrick #include <memory>
33061da546Spatrick 
34061da546Spatrick using namespace lldb;
35061da546Spatrick using namespace lldb_private;
36061da546Spatrick 
37061da546Spatrick // Thread Registers
38061da546Spatrick 
ThreadKDP(Process & process,lldb::tid_t tid)39061da546Spatrick ThreadKDP::ThreadKDP(Process &process, lldb::tid_t tid)
40061da546Spatrick     : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
41061da546Spatrick       m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS) {
42*f6aab3d8Srobert   Log *log = GetLog(KDPLog::Thread);
43061da546Spatrick   LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID());
44061da546Spatrick }
45061da546Spatrick 
~ThreadKDP()46061da546Spatrick ThreadKDP::~ThreadKDP() {
47*f6aab3d8Srobert   Log *log = GetLog(KDPLog::Thread);
48061da546Spatrick   LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID());
49061da546Spatrick   DestroyThread();
50061da546Spatrick }
51061da546Spatrick 
GetName()52061da546Spatrick const char *ThreadKDP::GetName() {
53061da546Spatrick   if (m_thread_name.empty())
54061da546Spatrick     return nullptr;
55061da546Spatrick   return m_thread_name.c_str();
56061da546Spatrick }
57061da546Spatrick 
GetQueueName()58061da546Spatrick const char *ThreadKDP::GetQueueName() { return nullptr; }
59061da546Spatrick 
RefreshStateAfterStop()60061da546Spatrick void ThreadKDP::RefreshStateAfterStop() {
61061da546Spatrick   // Invalidate all registers in our register context. We don't set "force" to
62061da546Spatrick   // true because the stop reply packet might have had some register values
63061da546Spatrick   // that were expedited and these will already be copied into the register
64061da546Spatrick   // context by the time this function gets called. The KDPRegisterContext
65061da546Spatrick   // class has been made smart enough to detect when it needs to invalidate
66061da546Spatrick   // which registers are valid by putting hooks in the register read and
67061da546Spatrick   // register supply functions where they check the process stop ID and do the
68061da546Spatrick   // right thing.
69061da546Spatrick   const bool force = false;
70061da546Spatrick   lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
71061da546Spatrick   if (reg_ctx_sp)
72061da546Spatrick     reg_ctx_sp->InvalidateIfNeeded(force);
73061da546Spatrick }
74061da546Spatrick 
ThreadIDIsValid(lldb::tid_t thread)75061da546Spatrick bool ThreadKDP::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
76061da546Spatrick 
Dump(Log * log,uint32_t index)77061da546Spatrick void ThreadKDP::Dump(Log *log, uint32_t index) {}
78061da546Spatrick 
ShouldStop(bool & step_more)79061da546Spatrick bool ThreadKDP::ShouldStop(bool &step_more) { return true; }
GetRegisterContext()80061da546Spatrick lldb::RegisterContextSP ThreadKDP::GetRegisterContext() {
81061da546Spatrick   if (!m_reg_context_sp)
82061da546Spatrick     m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
83061da546Spatrick   return m_reg_context_sp;
84061da546Spatrick }
85061da546Spatrick 
86061da546Spatrick lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)87061da546Spatrick ThreadKDP::CreateRegisterContextForFrame(StackFrame *frame) {
88061da546Spatrick   lldb::RegisterContextSP reg_ctx_sp;
89061da546Spatrick   uint32_t concrete_frame_idx = 0;
90061da546Spatrick 
91061da546Spatrick   if (frame)
92061da546Spatrick     concrete_frame_idx = frame->GetConcreteFrameIndex();
93061da546Spatrick 
94061da546Spatrick   if (concrete_frame_idx == 0) {
95061da546Spatrick     ProcessSP process_sp(CalculateProcess());
96061da546Spatrick     if (process_sp) {
97061da546Spatrick       switch (static_cast<ProcessKDP *>(process_sp.get())
98061da546Spatrick                   ->GetCommunication()
99061da546Spatrick                   .GetCPUType()) {
100061da546Spatrick       case llvm::MachO::CPU_TYPE_ARM:
101061da546Spatrick         reg_ctx_sp =
102061da546Spatrick             std::make_shared<RegisterContextKDP_arm>(*this, concrete_frame_idx);
103061da546Spatrick         break;
104061da546Spatrick       case llvm::MachO::CPU_TYPE_ARM64:
105061da546Spatrick         reg_ctx_sp = std::make_shared<RegisterContextKDP_arm64>(
106061da546Spatrick             *this, concrete_frame_idx);
107061da546Spatrick         break;
108061da546Spatrick       case llvm::MachO::CPU_TYPE_I386:
109061da546Spatrick         reg_ctx_sp = std::make_shared<RegisterContextKDP_i386>(
110061da546Spatrick             *this, concrete_frame_idx);
111061da546Spatrick         break;
112061da546Spatrick       case llvm::MachO::CPU_TYPE_X86_64:
113061da546Spatrick         reg_ctx_sp = std::make_shared<RegisterContextKDP_x86_64>(
114061da546Spatrick             *this, concrete_frame_idx);
115061da546Spatrick         break;
116061da546Spatrick       default:
117061da546Spatrick         llvm_unreachable("Add CPU type support in KDP");
118061da546Spatrick       }
119061da546Spatrick     }
120061da546Spatrick   } else {
121dda28197Spatrick     reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
122061da546Spatrick   }
123061da546Spatrick   return reg_ctx_sp;
124061da546Spatrick }
125061da546Spatrick 
CalculateStopInfo()126061da546Spatrick bool ThreadKDP::CalculateStopInfo() {
127061da546Spatrick   ProcessSP process_sp(GetProcess());
128061da546Spatrick   if (process_sp) {
129061da546Spatrick     if (m_cached_stop_info_sp) {
130061da546Spatrick       SetStopInfo(m_cached_stop_info_sp);
131061da546Spatrick     } else {
132061da546Spatrick       SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, SIGSTOP));
133061da546Spatrick     }
134061da546Spatrick     return true;
135061da546Spatrick   }
136061da546Spatrick   return false;
137061da546Spatrick }
138061da546Spatrick 
SetStopInfoFrom_KDP_EXCEPTION(const DataExtractor & exc_reply_packet)139061da546Spatrick void ThreadKDP::SetStopInfoFrom_KDP_EXCEPTION(
140061da546Spatrick     const DataExtractor &exc_reply_packet) {
141061da546Spatrick   lldb::offset_t offset = 0;
142061da546Spatrick   uint8_t reply_command = exc_reply_packet.GetU8(&offset);
143061da546Spatrick   if (reply_command == CommunicationKDP::KDP_EXCEPTION) {
144061da546Spatrick     offset = 8;
145061da546Spatrick     const uint32_t count = exc_reply_packet.GetU32(&offset);
146061da546Spatrick     if (count >= 1) {
147061da546Spatrick       // const uint32_t cpu = exc_reply_packet.GetU32 (&offset);
148061da546Spatrick       offset += 4; // Skip the useless CPU field
149061da546Spatrick       const uint32_t exc_type = exc_reply_packet.GetU32(&offset);
150061da546Spatrick       const uint32_t exc_code = exc_reply_packet.GetU32(&offset);
151061da546Spatrick       const uint32_t exc_subcode = exc_reply_packet.GetU32(&offset);
152061da546Spatrick       // We have to make a copy of the stop info because the thread list will
153061da546Spatrick       // iterate through the threads and clear all stop infos..
154061da546Spatrick 
155061da546Spatrick       // Let the StopInfoMachException::CreateStopReasonWithMachException()
156061da546Spatrick       // function update the PC if needed as we might hit a software breakpoint
157061da546Spatrick       // and need to decrement the PC (i386 and x86_64 need this) and KDP
158061da546Spatrick       // doesn't do this for us.
159061da546Spatrick       const bool pc_already_adjusted = false;
160061da546Spatrick       const bool adjust_pc_if_needed = true;
161061da546Spatrick 
162061da546Spatrick       m_cached_stop_info_sp =
163061da546Spatrick           StopInfoMachException::CreateStopReasonWithMachException(
164061da546Spatrick               *this, exc_type, 2, exc_code, exc_subcode, 0, pc_already_adjusted,
165061da546Spatrick               adjust_pc_if_needed);
166061da546Spatrick     }
167061da546Spatrick   }
168061da546Spatrick }
169