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"
15#include "lldb/Target/Process.h"
18#include "lldb/Target/Target.h"
19#include "lldb/Target/Unwind.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
31using namespace lldb;
32using namespace lldb_private;
33
34// Thread Registers
35
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
43
45 if (m_thread_name.empty())
46 return nullptr;
47 return m_thread_name.c_str();
48}
49
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
63bool ThreadMachCore::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
64
68 return m_reg_context_sp;
69}
70
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) {
81 ProcessSP process_sp(GetProcess());
82
83 ObjectFile *core_objfile =
84 static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile();
85 if (core_objfile)
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
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
110 ProcessSP process_sp(GetProcess());
111 if (process_sp) {
112 StopInfoSP stop_info;
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 =
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 =
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}
static bool IsCrashExceptionClass(AppleArm64ExceptionClass EC)
std::string m_thread_name
void RefreshStateAfterStop() override
ThreadMachCore(lldb_private::Process &process, lldb::tid_t tid, uint32_t objfile_lc_thread_idx)
uint32_t m_objfile_lc_thread_idx
static bool ThreadIDIsValid(lldb::tid_t thread)
~ThreadMachCore() override
lldb::RegisterContextSP CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override
const char * GetName() override
bool CalculateStopInfo() override
Ask the thread subclass to set its stop info.
lldb::RegisterContextSP GetRegisterContext() override
lldb::RegisterContextSP m_thread_reg_ctx_sp
An architecture specification class.
Definition: ArchSpec.h:31
uint32_t GetMachOCPUType() const
Definition: ArchSpec.cpp:669
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
virtual lldb::RegisterContextSP GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread)
Definition: ObjectFile.h:567
A plug-in interface definition class for debugging a process.
Definition: Process.h:343
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
uint32_t GetAsUInt32(uint32_t fail_value=UINT32_MAX, bool *success_ptr=nullptr) const
This base class provides an interface to stack frames.
Definition: StackFrame.h:44
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:448
static lldb::StopInfoSP CreateStopReasonWithException(Thread &thread, const char *description)
Definition: StopInfo.cpp:1489
const char * GetData() const
Definition: StreamString.h:45
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
const ArchSpec & GetArchitecture() const
Definition: Target.h:1039
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition: Thread.cpp:463
virtual void DestroyThread()
Definition: Thread.cpp:251
virtual Unwind & GetUnwinder()
Definition: Thread.cpp:1915
lldb::ProcessSP GetProcess() const
Definition: Thread.h:157
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread's current register state.
Definition: Thread.h:1319
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:56
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
AppleArm64ExceptionClass getAppleArm64ExceptionClass(uint32_t esr)
Get the Apple ARM64 exception class encoded within esr.
const char * toString(AppleArm64ExceptionClass EC)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
Definition: lldb-forward.h:431
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:394
uint64_t tid_t
Definition: lldb-types.h:84
Every register is described in detail including its name, alternate name (optional),...