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 <optional>
10#include <string>
11#include <vector>
12
14#include "ThreadMachCore.h"
15
17#include "lldb/Host/SafeMachO.h"
21#include "lldb/Target/Process.h"
24#include "lldb/Target/Target.h"
25#include "lldb/Target/Unwind.h"
29#include "lldb/Utility/State.h"
32
33#include "ProcessMachCore.h"
34//#include "RegisterContextKDP_arm.h"
35//#include "RegisterContextKDP_i386.h"
36//#include "RegisterContextKDP_x86_64.h"
37
38using namespace lldb;
39using namespace lldb_private;
40
41// Thread Registers
42
44 uint32_t objfile_lc_thread_idx)
45 : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
47 m_objfile_lc_thread_idx(objfile_lc_thread_idx) {}
48
50
52 if (m_thread_name.empty())
53 return nullptr;
54 return m_thread_name.c_str();
55}
56
58 // Invalidate all registers in our register context. We don't set "force" to
59 // true because the stop reply packet might have had some register values
60 // that were expedited and these will already be copied into the register
61 // context by the time this function gets called. The KDPRegisterContext
62 // class has been made smart enough to detect when it needs to invalidate
63 // which registers are valid by putting hooks in the register read and
64 // register supply functions where they check the process stop ID and do the
65 // right thing.
66 const bool force = false;
67 GetRegisterContext()->InvalidateIfNeeded(force);
68}
69
70bool ThreadMachCore::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
71
77
80 uint32_t concrete_frame_idx = 0;
81
82 if (frame)
83 concrete_frame_idx = frame->GetConcreteFrameIndex();
84 if (concrete_frame_idx > 0)
86
89
90 ProcessSP process_sp(GetProcess());
91 assert(process_sp);
92
93 ObjectFile *core_objfile =
94 static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile();
95 if (!core_objfile)
96 return {};
97
98 RegisterContextSP core_thread_regctx_sp =
100
101 if (!core_thread_regctx_sp)
102 return {};
103
104 StructuredData::ObjectSP process_md_sp =
105 core_objfile->GetCorefileProcessMetadata();
106
107 StructuredData::ObjectSP thread_md_sp;
108 if (process_md_sp && process_md_sp->GetAsDictionary() &&
109 process_md_sp->GetAsDictionary()->HasKey("threads")) {
110 StructuredData::Array *threads = process_md_sp->GetAsDictionary()
111 ->GetValueForKey("threads")
112 ->GetAsArray();
113 if (threads && threads->GetSize() == core_objfile->GetNumThreadContexts()) {
114 StructuredData::ObjectSP thread_sp =
116 if (thread_sp && thread_sp->GetAsDictionary())
117 thread_md_sp = thread_sp;
118 }
119 }
120 m_thread_reg_ctx_sp = std::make_shared<RegisterContextUnifiedCore>(
121 *this, concrete_frame_idx, core_thread_regctx_sp, thread_md_sp);
122
123 return m_thread_reg_ctx_sp;
124}
125
127 switch (EC) {
128 case AppleArm64ExceptionClass::ESR_EC_UNCATEGORIZED:
129 case AppleArm64ExceptionClass::ESR_EC_SVC_32:
130 case AppleArm64ExceptionClass::ESR_EC_SVC_64:
131 // In the ARM exception model, a process takes an exception when asking the
132 // kernel to service a system call. Don't treat this like a crash.
133 return false;
134 default:
135 return true;
136 }
137}
138
140 ProcessSP process_sp(GetProcess());
141 if (process_sp) {
142 StopInfoSP stop_info;
144
145 if (reg_ctx_sp) {
146 Target &target = process_sp->GetTarget();
147 const ArchSpec arch_spec = target.GetArchitecture();
148 const uint32_t cputype = arch_spec.GetMachOCPUType();
149
150 if (cputype == llvm::MachO::CPU_TYPE_ARM64 ||
151 cputype == llvm::MachO::CPU_TYPE_ARM64_32) {
152 const RegisterInfo *esr_info = reg_ctx_sp->GetRegisterInfoByName("esr");
153 const RegisterInfo *far_info = reg_ctx_sp->GetRegisterInfoByName("far");
154 RegisterValue esr, far;
155 if (reg_ctx_sp->ReadRegister(esr_info, esr) &&
156 reg_ctx_sp->ReadRegister(far_info, far)) {
157 const uint32_t esr_val = esr.GetAsUInt32();
158 const AppleArm64ExceptionClass exception_class =
160 if (IsCrashExceptionClass(exception_class)) {
161 StreamString S;
162 S.Printf("%s (fault address: 0x%" PRIx64 ")",
163 toString(exception_class), far.GetAsUInt64());
164 stop_info =
165 StopInfo::CreateStopReasonWithException(*this, S.GetData());
166 }
167 }
168 }
169 }
170
171 // Set a stop reason for crashing threads only so that they get selected
172 // preferentially.
173 if (stop_info)
174 SetStopInfo(stop_info);
175 return true;
176 }
177 return false;
178}
static bool IsCrashExceptionClass(AppleArm64ExceptionClass EC)
std::string m_thread_name
friend class ProcessMachCore
lldb::addr_t m_thread_dispatch_qaddr
void RefreshStateAfterStop() override
std::string m_dispatch_queue_name
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:649
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:45
virtual lldb::RegisterContextSP GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread)
Definition ObjectFile.h:585
virtual uint32_t GetNumThreadContexts()
Definition ObjectFile.h:490
virtual StructuredData::ObjectSP GetCorefileProcessMetadata()
Get process metadata from the corefile in a StructuredData dictionary.
Definition ObjectFile.h:582
A plug-in interface definition class for debugging a process.
Definition Process.h:357
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
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:455
static lldb::StopInfoSP CreateStopReasonWithException(Thread &thread, const char *description)
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
ObjectSP GetItemAtIndex(size_t idx) const
ObjectSP GetValueForKey(llvm::StringRef key) const
std::shared_ptr< Object > ObjectSP
const ArchSpec & GetArchitecture() const
Definition Target.h:1056
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition Thread.cpp:464
virtual void DestroyThread()
Definition Thread.cpp:252
virtual Unwind & GetUnwinder()
Definition Thread.cpp:1934
Thread(Process &process, lldb::tid_t tid, bool use_invalid_index_id=false)
Constructor.
Definition Thread.cpp:219
lldb::ProcessSP GetProcess() const
Definition Thread.h:158
friend class StackFrame
Definition Thread.h:1303
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread's current register state.
Definition Thread.h:1370
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition Unwind.h:56
#define LLDB_INVALID_ADDRESS
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)
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
uint64_t tid_t
Definition lldb-types.h:84
Every register is described in detail including its name, alternate name (optional),...