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 
11 #include "ThreadMachCore.h"
12 
13 #include "llvm/Support/MachO.h"
14 
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/DataExtractor.h"
17 #include "lldb/Core/StreamString.h"
18 #include "lldb/Core/State.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 #include "lldb/Breakpoint/Watchpoint.h"
26 
27 #include "ProcessMachCore.h"
28 //#include "RegisterContextKDP_arm.h"
29 //#include "RegisterContextKDP_i386.h"
30 //#include "RegisterContextKDP_x86_64.h"
31 
32 using namespace lldb;
33 using namespace lldb_private;
34 
35 //----------------------------------------------------------------------
36 // Thread Registers
37 //----------------------------------------------------------------------
38 
39 ThreadMachCore::ThreadMachCore (const lldb::ProcessSP &process_sp, lldb::tid_t tid) :
40     Thread(process_sp, tid),
41     m_thread_name (),
42     m_dispatch_queue_name (),
43     m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS),
44     m_thread_reg_ctx_sp ()
45 {
46 }
47 
48 ThreadMachCore::~ThreadMachCore ()
49 {
50     DestroyThread();
51 }
52 
53 const char *
54 ThreadMachCore::GetName ()
55 {
56     if (m_thread_name.empty())
57         return NULL;
58     return m_thread_name.c_str();
59 }
60 
61 void
62 ThreadMachCore::RefreshStateAfterStop()
63 {
64     // Invalidate all registers in our register context. We don't set "force" to
65     // true because the stop reply packet might have had some register values
66     // that were expedited and these will already be copied into the register
67     // context by the time this function gets called. The KDPRegisterContext
68     // class has been made smart enough to detect when it needs to invalidate
69     // which registers are valid by putting hooks in the register read and
70     // register supply functions where they check the process stop ID and do
71     // the right thing.
72     const bool force = false;
73     GetRegisterContext()->InvalidateIfNeeded (force);
74 }
75 
76 void
77 ThreadMachCore::ClearStackFrames ()
78 {
79     Unwind *unwinder = GetUnwinder ();
80     if (unwinder)
81         unwinder->Clear();
82     Thread::ClearStackFrames();
83 }
84 
85 
86 bool
87 ThreadMachCore::ThreadIDIsValid (lldb::tid_t thread)
88 {
89     return thread != 0;
90 }
91 
92 lldb::RegisterContextSP
93 ThreadMachCore::GetRegisterContext ()
94 {
95     if (m_reg_context_sp.get() == NULL)
96         m_reg_context_sp = CreateRegisterContextForFrame (NULL);
97     return m_reg_context_sp;
98 }
99 
100 lldb::RegisterContextSP
101 ThreadMachCore::CreateRegisterContextForFrame (StackFrame *frame)
102 {
103     lldb::RegisterContextSP reg_ctx_sp;
104     uint32_t concrete_frame_idx = 0;
105 
106     if (frame)
107         concrete_frame_idx = frame->GetConcreteFrameIndex ();
108 
109     if (concrete_frame_idx == 0)
110     {
111         if (!m_thread_reg_ctx_sp)
112         {
113             ProcessSP process_sp (GetProcess());
114 
115             ObjectFile *core_objfile = static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile ();
116             if (core_objfile)
117                 m_thread_reg_ctx_sp = core_objfile->GetThreadContextAtIndex (GetID(), *this);
118         }
119         reg_ctx_sp = m_thread_reg_ctx_sp;
120     }
121     else if (m_unwinder_ap.get())
122     {
123         reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
124     }
125     return reg_ctx_sp;
126 }
127 
128 lldb::StopInfoSP
129 ThreadMachCore::GetPrivateStopReason ()
130 {
131     ProcessSP process_sp (GetProcess());
132 
133     if (process_sp)
134     {
135         const uint32_t process_stop_id = process_sp->GetStopID();
136         if (m_thread_stop_reason_stop_id != process_stop_id ||
137             (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
138         {
139             // TODO: can we query the initial state of the thread here?
140             // For now I am just going to pretend that a SIGSTOP happened.
141 
142             SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP));
143 
144             // If GetKDPProcess().SetThreadStopInfo() doesn't find a stop reason
145             // for this thread, then m_actual_stop_info_sp will not ever contain
146             // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
147             // check will never be able to tell us if we have the correct stop info
148             // for this thread and we will continually send qThreadStopInfo packets
149             // down to the remote KDP server, so we need to keep our own notion
150             // of the stop ID that m_actual_stop_info_sp is valid for (even if it
151             // contains nothing). We use m_thread_stop_reason_stop_id for this below.
152     //        m_thread_stop_reason_stop_id = process_stop_id;
153     //        m_actual_stop_info_sp.reset();
154 
155         }
156     }
157     return m_actual_stop_info_sp;
158 }
159 
160 
161