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/Symbol/ObjectFile.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/RegisterContext.h"
15 #include "lldb/Target/StopInfo.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/Unwind.h"
18 #include "lldb/Utility/ArchSpec.h"
19 #include "lldb/Utility/DataExtractor.h"
20 #include "lldb/Utility/State.h"
21 #include "lldb/Utility/StreamString.h"
22 
23 #include "ProcessMachCore.h"
24 //#include "RegisterContextKDP_arm.h"
25 //#include "RegisterContextKDP_i386.h"
26 //#include "RegisterContextKDP_x86_64.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 // Thread Registers
32 
33 ThreadMachCore::ThreadMachCore(Process &process, lldb::tid_t tid)
34     : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
35       m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS), m_thread_reg_ctx_sp() {}
36 
37 ThreadMachCore::~ThreadMachCore() { DestroyThread(); }
38 
39 const char *ThreadMachCore::GetName() {
40   if (m_thread_name.empty())
41     return nullptr;
42   return m_thread_name.c_str();
43 }
44 
45 void ThreadMachCore::RefreshStateAfterStop() {
46   // Invalidate all registers in our register context. We don't set "force" to
47   // true because the stop reply packet might have had some register values
48   // that were expedited and these will already be copied into the register
49   // context by the time this function gets called. The KDPRegisterContext
50   // class has been made smart enough to detect when it needs to invalidate
51   // which registers are valid by putting hooks in the register read and
52   // register supply functions where they check the process stop ID and do the
53   // right thing.
54   const bool force = false;
55   GetRegisterContext()->InvalidateIfNeeded(force);
56 }
57 
58 bool ThreadMachCore::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
59 
60 lldb::RegisterContextSP ThreadMachCore::GetRegisterContext() {
61   if (!m_reg_context_sp)
62     m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
63   return m_reg_context_sp;
64 }
65 
66 lldb::RegisterContextSP
67 ThreadMachCore::CreateRegisterContextForFrame(StackFrame *frame) {
68   lldb::RegisterContextSP reg_ctx_sp;
69   uint32_t concrete_frame_idx = 0;
70 
71   if (frame)
72     concrete_frame_idx = frame->GetConcreteFrameIndex();
73 
74   if (concrete_frame_idx == 0) {
75     if (!m_thread_reg_ctx_sp) {
76       ProcessSP process_sp(GetProcess());
77 
78       ObjectFile *core_objfile =
79           static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile();
80       if (core_objfile)
81         m_thread_reg_ctx_sp =
82             core_objfile->GetThreadContextAtIndex(GetID(), *this);
83     }
84     reg_ctx_sp = m_thread_reg_ctx_sp;
85   } else {
86     reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
87   }
88   return reg_ctx_sp;
89 }
90 
91 bool ThreadMachCore::CalculateStopInfo() {
92   ProcessSP process_sp(GetProcess());
93   if (process_sp) {
94     SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, SIGSTOP));
95     return true;
96   }
97   return false;
98 }
99