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