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 : 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
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
61bool ThreadMachCore::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
62
63lldb::RegisterContextSP ThreadMachCore::GetRegisterContext() {
66 return m_reg_context_sp;
67}
68
69lldb::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) {
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 =
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}
static bool IsCrashExceptionClass(AppleArm64ExceptionClass EC)
std::string m_thread_name
ThreadMachCore(lldb_private::Process &process, lldb::tid_t tid)
void RefreshStateAfterStop() override
static bool ThreadIDIsValid(lldb::tid_t thread)
~ThreadMachCore() override
lldb::RegisterContextSP CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override
const char * GetName() override
bool CalculateStopInfo() override
lldb::RegisterContextSP GetRegisterContext() override
lldb::RegisterContextSP m_thread_reg_ctx_sp
An architecture specification class.
Definition: ArchSpec.h:32
uint32_t GetMachOCPUType() const
Definition: ArchSpec.cpp:646
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:62
virtual lldb::RegisterContextSP GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread)
Definition: ObjectFile.h:560
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:41
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:412
static lldb::StopInfoSP CreateStopReasonWithException(Thread &thread, const char *description)
Definition: StopInfo.cpp:1399
const char * GetData() const
Definition: StreamString.h:43
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
const ArchSpec & GetArchitecture() const
Definition: Target.h:990
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition: Thread.cpp:446
virtual void DestroyThread()
Definition: Thread.cpp:251
virtual Unwind & GetUnwinder()
Definition: Thread.cpp:1882
lldb::ProcessSP GetProcess() const
Definition: Thread.h:153
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread's current register state.
Definition: Thread.h:1271
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:56
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
AppleArm64ExceptionClass getAppleArm64ExceptionClass(uint32_t esr)
Get the Apple ARM64 exception class encoded within esr.
const char * toString(AppleArm64ExceptionClass EC)
Definition: SBAddress.h:15
uint64_t tid_t
Definition: lldb-types.h:82
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47