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