1 //===-- ThreadMachCore.cpp ------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ThreadMachCore.h"
11 #include "lldb/Breakpoint/Watchpoint.h"
12 #include "lldb/Host/SafeMachO.h"
13 #include "lldb/Symbol/ObjectFile.h"
14 #include "lldb/Target/AppleArm64ExceptionClass.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/RegisterContext.h"
17 #include "lldb/Target/StopInfo.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/Unwind.h"
20 #include "lldb/Utility/ArchSpec.h"
21 #include "lldb/Utility/DataExtractor.h"
22 #include "lldb/Utility/RegisterValue.h"
23 #include "lldb/Utility/State.h"
24 #include "lldb/Utility/StreamString.h"
26 #include "ProcessMachCore.h"
27 //#include "RegisterContextKDP_arm.h"
28 //#include "RegisterContextKDP_i386.h"
29 //#include "RegisterContextKDP_x86_64.h"
32 using namespace lldb_private
;
36 ThreadMachCore::ThreadMachCore(Process
&process
, lldb::tid_t tid
,
37 uint32_t objfile_lc_thread_idx
)
38 : Thread(process
, tid
), m_thread_name(), m_dispatch_queue_name(),
39 m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS
), m_thread_reg_ctx_sp(),
40 m_objfile_lc_thread_idx(objfile_lc_thread_idx
) {}
42 ThreadMachCore::~ThreadMachCore() { DestroyThread(); }
44 const char *ThreadMachCore::GetName() {
45 if (m_thread_name
.empty())
47 return m_thread_name
.c_str();
50 void ThreadMachCore::RefreshStateAfterStop() {
51 // Invalidate all registers in our register context. We don't set "force" to
52 // true because the stop reply packet might have had some register values
53 // that were expedited and these will already be copied into the register
54 // context by the time this function gets called. The KDPRegisterContext
55 // class has been made smart enough to detect when it needs to invalidate
56 // which registers are valid by putting hooks in the register read and
57 // register supply functions where they check the process stop ID and do the
59 const bool force
= false;
60 GetRegisterContext()->InvalidateIfNeeded(force
);
63 bool ThreadMachCore::ThreadIDIsValid(lldb::tid_t thread
) { return thread
!= 0; }
65 lldb::RegisterContextSP
ThreadMachCore::GetRegisterContext() {
66 if (!m_reg_context_sp
)
67 m_reg_context_sp
= CreateRegisterContextForFrame(nullptr);
68 return m_reg_context_sp
;
71 lldb::RegisterContextSP
72 ThreadMachCore::CreateRegisterContextForFrame(StackFrame
*frame
) {
73 lldb::RegisterContextSP reg_ctx_sp
;
74 uint32_t concrete_frame_idx
= 0;
77 concrete_frame_idx
= frame
->GetConcreteFrameIndex();
79 if (concrete_frame_idx
== 0) {
80 if (!m_thread_reg_ctx_sp
) {
81 ProcessSP
process_sp(GetProcess());
83 ObjectFile
*core_objfile
=
84 static_cast<ProcessMachCore
*>(process_sp
.get())->GetCoreObjectFile();
86 m_thread_reg_ctx_sp
= core_objfile
->GetThreadContextAtIndex(
87 m_objfile_lc_thread_idx
, *this);
89 reg_ctx_sp
= m_thread_reg_ctx_sp
;
91 reg_ctx_sp
= GetUnwinder().CreateRegisterContextForFrame(frame
);
96 static bool IsCrashExceptionClass(AppleArm64ExceptionClass EC
) {
98 case AppleArm64ExceptionClass::ESR_EC_UNCATEGORIZED
:
99 case AppleArm64ExceptionClass::ESR_EC_SVC_32
:
100 case AppleArm64ExceptionClass::ESR_EC_SVC_64
:
101 // In the ARM exception model, a process takes an exception when asking the
102 // kernel to service a system call. Don't treat this like a crash.
109 bool ThreadMachCore::CalculateStopInfo() {
110 ProcessSP
process_sp(GetProcess());
112 StopInfoSP stop_info
;
113 RegisterContextSP reg_ctx_sp
= GetRegisterContext();
116 Target
&target
= process_sp
->GetTarget();
117 const ArchSpec arch_spec
= target
.GetArchitecture();
118 const uint32_t cputype
= arch_spec
.GetMachOCPUType();
120 if (cputype
== llvm::MachO::CPU_TYPE_ARM64
||
121 cputype
== llvm::MachO::CPU_TYPE_ARM64_32
) {
122 const RegisterInfo
*esr_info
= reg_ctx_sp
->GetRegisterInfoByName("esr");
123 const RegisterInfo
*far_info
= reg_ctx_sp
->GetRegisterInfoByName("far");
124 RegisterValue esr
, far
;
125 if (reg_ctx_sp
->ReadRegister(esr_info
, esr
) &&
126 reg_ctx_sp
->ReadRegister(far_info
, far
)) {
127 const uint32_t esr_val
= esr
.GetAsUInt32();
128 const AppleArm64ExceptionClass exception_class
=
129 getAppleArm64ExceptionClass(esr_val
);
130 if (IsCrashExceptionClass(exception_class
)) {
132 S
.Printf("%s (fault address: 0x%" PRIx64
")",
133 toString(exception_class
), far
.GetAsUInt64());
135 StopInfo::CreateStopReasonWithException(*this, S
.GetData());
141 // Set a stop reason for crashing threads only so that they get selected
144 SetStopInfo(stop_info
);