LLDB  mainline
ThreadMachCore.cpp
Go to the documentation of this file.
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 
12 #include "lldb/Host/SafeMachO.h"
13 #include "lldb/Symbol/ObjectFile.h"
15 #include "lldb/Target/Process.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"
23 #include "lldb/Utility/State.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 
37  : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
38  m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS), m_thread_reg_ctx_sp() {}
39 
41 
42 const char *ThreadMachCore::GetName() {
43  if (m_thread_name.empty())
44  return nullptr;
45  return m_thread_name.c_str();
46 }
47 
49  // Invalidate all registers in our register context. We don't set "force" to
50  // true because the stop reply packet might have had some register values
51  // that were expedited and these will already be copied into the register
52  // context by the time this function gets called. The KDPRegisterContext
53  // class has been made smart enough to detect when it needs to invalidate
54  // which registers are valid by putting hooks in the register read and
55  // register supply functions where they check the process stop ID and do the
56  // right thing.
57  const bool force = false;
58  GetRegisterContext()->InvalidateIfNeeded(force);
59 }
60 
61 bool ThreadMachCore::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
62 
63 lldb::RegisterContextSP ThreadMachCore::GetRegisterContext() {
64  if (!m_reg_context_sp)
66  return m_reg_context_sp;
67 }
68 
69 lldb::RegisterContextSP
71  lldb::RegisterContextSP reg_ctx_sp;
72  uint32_t concrete_frame_idx = 0;
73 
74  if (frame)
75  concrete_frame_idx = frame->GetConcreteFrameIndex();
76 
77  if (concrete_frame_idx == 0) {
78  if (!m_thread_reg_ctx_sp) {
79  ProcessSP process_sp(GetProcess());
80 
81  ObjectFile *core_objfile =
82  static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile();
83  if (core_objfile)
85  core_objfile->GetThreadContextAtIndex(GetID(), *this);
86  }
87  reg_ctx_sp = m_thread_reg_ctx_sp;
88  } else {
89  reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
90  }
91  return reg_ctx_sp;
92 }
93 
95  switch (EC) {
96  case AppleArm64ExceptionClass::ESR_EC_UNCATEGORIZED:
97  case AppleArm64ExceptionClass::ESR_EC_SVC_32:
98  case AppleArm64ExceptionClass::ESR_EC_SVC_64:
99  // In the ARM exception model, a process takes an exception when asking the
100  // kernel to service a system call. Don't treat this like a crash.
101  return false;
102  default:
103  return true;
104  }
105 }
106 
108  ProcessSP process_sp(GetProcess());
109  if (process_sp) {
110  StopInfoSP stop_info;
111  RegisterContextSP reg_ctx_sp = GetRegisterContext();
112 
113  if (reg_ctx_sp) {
114  Target &target = process_sp->GetTarget();
115  const ArchSpec arch_spec = target.GetArchitecture();
116  const uint32_t cputype = arch_spec.GetMachOCPUType();
117 
118  if (cputype == llvm::MachO::CPU_TYPE_ARM64 ||
119  cputype == llvm::MachO::CPU_TYPE_ARM64_32) {
120  const RegisterInfo *esr_info = reg_ctx_sp->GetRegisterInfoByName("esr");
121  const RegisterInfo *far_info = reg_ctx_sp->GetRegisterInfoByName("far");
122  RegisterValue esr, far;
123  if (reg_ctx_sp->ReadRegister(esr_info, esr) &&
124  reg_ctx_sp->ReadRegister(far_info, far)) {
125  const uint32_t esr_val = esr.GetAsUInt32();
126  const AppleArm64ExceptionClass exception_class =
128  if (IsCrashExceptionClass(exception_class)) {
129  StreamString S;
130  S.Printf("%s (fault address: 0x%" PRIx64 ")",
131  toString(exception_class), far.GetAsUInt64());
132  stop_info =
133  StopInfo::CreateStopReasonWithException(*this, S.GetData());
134  }
135  }
136  }
137  }
138 
139  // Set a stop reason for crashing threads only so that they get selected
140  // preferentially.
141  if (stop_info)
142  SetStopInfo(stop_info);
143  return true;
144  }
145  return false;
146 }
lldb_private::toString
const char * toString(AppleArm64ExceptionClass EC)
Definition: AppleArm64ExceptionClass.h:38
lldb_private::ArchSpec
Definition: ArchSpec.h:33
ThreadMachCore::RefreshStateAfterStop
void RefreshStateAfterStop() override
Definition: ThreadMachCore.cpp:48
lldb_private::RegisterValue
Definition: RegisterValue.h:28
ThreadMachCore::GetRegisterContext
lldb::RegisterContextSP GetRegisterContext() override
Definition: ThreadMachCore.cpp:63
lldb_private::Thread::GetUnwinder
virtual Unwind & GetUnwinder()
Definition: Thread.cpp:1861
Unwind.h
lldb_private::Process
Definition: Process.h:340
RegisterValue.h
lldb_private::Target
Definition: Target.h:450
Process.h
lldb_private::RegisterValue::GetAsUInt64
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
Definition: RegisterValue.cpp:563
ThreadMachCore::ThreadMachCore
ThreadMachCore(lldb_private::Process &process, lldb::tid_t tid)
Definition: ThreadMachCore.cpp:36
SafeMachO.h
ProcessMachCore.h
ThreadMachCore::~ThreadMachCore
~ThreadMachCore() override
Definition: ThreadMachCore.cpp:40
Target.h
lldb_private::StackFrame::GetConcreteFrameIndex
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:407
lldb_private::Thread::GetProcess
lldb::ProcessSP GetProcess() const
Definition: Thread.h:152
lldb_private::Thread::DestroyThread
virtual void DestroyThread()
Definition: Thread.cpp:247
ProcessMachCore
Definition: ProcessMachCore.h:21
lldb_private::ArchSpec::GetMachOCPUType
uint32_t GetMachOCPUType() const
Definition: ArchSpec.cpp:635
Watchpoint.h
lldb_private::Thread
Definition: Thread.h:60
ThreadMachCore.h
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
ThreadMachCore::GetName
const char * GetName() override
Definition: ThreadMachCore.cpp:42
lldb_private::ObjectFile::GetThreadContextAtIndex
virtual lldb::RegisterContextSP GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread)
Definition: ObjectFile.h:538
StreamString.h
IsCrashExceptionClass
static bool IsCrashExceptionClass(AppleArm64ExceptionClass EC)
Definition: ThreadMachCore.cpp:94
ThreadMachCore::m_thread_name
std::string m_thread_name
Definition: ThreadMachCore.h:56
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::Thread::SetStopInfo
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition: Thread.cpp:433
ThreadMachCore::ThreadIDIsValid
static bool ThreadIDIsValid(lldb::tid_t thread)
Definition: ThreadMachCore.cpp:61
ThreadMachCore::m_thread_reg_ctx_sp
lldb::RegisterContextSP m_thread_reg_ctx_sp
Definition: ThreadMachCore.h:59
CPU_TYPE_ARM64_32
#define CPU_TYPE_ARM64_32
Definition: HostInfoMacOSX.mm:51
ObjectFile.h
lldb_private::UserID::GetID
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
lldb_private::Unwind::CreateRegisterContextForFrame
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:56
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:966
lldb_private::Thread::m_reg_context_sp
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread's current register state.
Definition: Thread.h:1247
uint32_t
lldb_private::getAppleArm64ExceptionClass
AppleArm64ExceptionClass getAppleArm64ExceptionClass(uint32_t esr)
Get the Apple ARM64 exception class encoded within esr.
Definition: AppleArm64ExceptionClass.h:22
ArchSpec.h
StopInfo.h
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
DataExtractor.h
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
ThreadMachCore::CalculateStopInfo
bool CalculateStopInfo() override
Definition: ThreadMachCore.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
State.h
ThreadMachCore::CreateRegisterContextForFrame
lldb::RegisterContextSP CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override
Definition: ThreadMachCore.cpp:70
AppleArm64ExceptionClass.h
lldb_private::AppleArm64ExceptionClass
AppleArm64ExceptionClass
Definition: AppleArm64ExceptionClass.h:16
lldb_private::StackFrame
Definition: StackFrame.h:40
lldb_private::RegisterValue::GetAsUInt32
uint32_t GetAsUInt32(uint32_t fail_value=UINT32_MAX, bool *success_ptr=nullptr) const
Definition: RegisterValue.cpp:533
lldb
Definition: SBAddress.h:15
RegisterContext.h
lldb_private::ObjectFile
Definition: ObjectFile.h:58
CPU_TYPE_ARM64
#define CPU_TYPE_ARM64
Definition: HostInfoMacOSX.mm:46
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86