xref: /llvm-project/lldb/source/Plugins/Process/mach-core/ThreadMachCore.cpp (revision 2cab996192cf143d10e3381fcefa75e270cc7ddb)
1 //===-- ThreadMachCore.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 "ThreadMachCore.h"
10 
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"
25 
26 #include "ProcessMachCore.h"
27 //#include "RegisterContextKDP_arm.h"
28 //#include "RegisterContextKDP_i386.h"
29 //#include "RegisterContextKDP_x86_64.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 // Thread Registers
35 
ThreadMachCore(Process & process,lldb::tid_t tid,uint32_t objfile_lc_thread_idx)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) {}
41 
~ThreadMachCore()42 ThreadMachCore::~ThreadMachCore() { DestroyThread(); }
43 
GetName()44 const char *ThreadMachCore::GetName() {
45   if (m_thread_name.empty())
46     return nullptr;
47   return m_thread_name.c_str();
48 }
49 
RefreshStateAfterStop()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
58   // right thing.
59   const bool force = false;
60   GetRegisterContext()->InvalidateIfNeeded(force);
61 }
62 
ThreadIDIsValid(lldb::tid_t thread)63 bool ThreadMachCore::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
64 
GetRegisterContext()65 lldb::RegisterContextSP ThreadMachCore::GetRegisterContext() {
66   if (!m_reg_context_sp)
67     m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
68   return m_reg_context_sp;
69 }
70 
71 lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)72 ThreadMachCore::CreateRegisterContextForFrame(StackFrame *frame) {
73   lldb::RegisterContextSP reg_ctx_sp;
74   uint32_t concrete_frame_idx = 0;
75 
76   if (frame)
77     concrete_frame_idx = frame->GetConcreteFrameIndex();
78 
79   if (concrete_frame_idx == 0) {
80     if (!m_thread_reg_ctx_sp) {
81       ProcessSP process_sp(GetProcess());
82 
83       ObjectFile *core_objfile =
84           static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile();
85       if (core_objfile)
86         m_thread_reg_ctx_sp = core_objfile->GetThreadContextAtIndex(
87             m_objfile_lc_thread_idx, *this);
88     }
89     reg_ctx_sp = m_thread_reg_ctx_sp;
90   } else {
91     reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
92   }
93   return reg_ctx_sp;
94 }
95 
IsCrashExceptionClass(AppleArm64ExceptionClass EC)96 static bool IsCrashExceptionClass(AppleArm64ExceptionClass EC) {
97   switch (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.
103     return false;
104   default:
105     return true;
106   }
107 }
108 
CalculateStopInfo()109 bool ThreadMachCore::CalculateStopInfo() {
110   ProcessSP process_sp(GetProcess());
111   if (process_sp) {
112     StopInfoSP stop_info;
113     RegisterContextSP reg_ctx_sp = GetRegisterContext();
114 
115     if (reg_ctx_sp) {
116       Target &target = process_sp->GetTarget();
117       const ArchSpec arch_spec = target.GetArchitecture();
118       const uint32_t cputype = arch_spec.GetMachOCPUType();
119 
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)) {
131             StreamString S;
132             S.Printf("%s (fault address: 0x%" PRIx64 ")",
133                      toString(exception_class), far.GetAsUInt64());
134             stop_info =
135                 StopInfo::CreateStopReasonWithException(*this, S.GetData());
136           }
137         }
138       }
139     }
140 
141     // Set a stop reason for crashing threads only so that they get selected
142     // preferentially.
143     if (stop_info)
144       SetStopInfo(stop_info);
145     return true;
146   }
147   return false;
148 }
149