1 //===-- ThreadMachCore.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ThreadMachCore.h"
11 
12 #include "lldb/Breakpoint/Watchpoint.h"
13 #include "lldb/Symbol/ObjectFile.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/RegisterContext.h"
16 #include "lldb/Target/StopInfo.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/Unwind.h"
19 #include "lldb/Utility/ArchSpec.h"
20 #include "lldb/Utility/DataExtractor.h"
21 #include "lldb/Utility/State.h"
22 #include "lldb/Utility/StreamString.h"
23 
24 #include "ProcessMachCore.h"
25 //#include "RegisterContextKDP_arm.h"
26 //#include "RegisterContextKDP_i386.h"
27 //#include "RegisterContextKDP_x86_64.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 //----------------------------------------------------------------------
33 // Thread Registers
34 //----------------------------------------------------------------------
35 
36 ThreadMachCore::ThreadMachCore(Process &process, lldb::tid_t tid)
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 
40 ThreadMachCore::~ThreadMachCore() { DestroyThread(); }
41 
42 const char *ThreadMachCore::GetName() {
43   if (m_thread_name.empty())
44     return nullptr;
45   return m_thread_name.c_str();
46 }
47 
48 void ThreadMachCore::RefreshStateAfterStop() {
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 
61 bool ThreadMachCore::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
62 
63 lldb::RegisterContextSP ThreadMachCore::GetRegisterContext() {
64   if (!m_reg_context_sp)
65     m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
66   return m_reg_context_sp;
67 }
68 
69 lldb::RegisterContextSP
70 ThreadMachCore::CreateRegisterContextForFrame(StackFrame *frame) {
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) {
78     if (!m_thread_reg_ctx_sp) {
79       ProcessSP process_sp(GetProcess());
80 
81       ObjectFile *core_objfile =
82           static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile();
83       if (core_objfile)
84         m_thread_reg_ctx_sp =
85             core_objfile->GetThreadContextAtIndex(GetID(), *this);
86     }
87     reg_ctx_sp = m_thread_reg_ctx_sp;
88   } else {
89     Unwind *unwinder = GetUnwinder();
90     if (unwinder != nullptr)
91       reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
92   }
93   return reg_ctx_sp;
94 }
95 
96 bool ThreadMachCore::CalculateStopInfo() {
97   ProcessSP process_sp(GetProcess());
98   if (process_sp) {
99     SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, SIGSTOP));
100     return true;
101   }
102   return false;
103 }
104