1 //===-- ThreadKDP.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 "ThreadKDP.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/Target/Process.h"
20 #include "lldb/Target/RegisterContext.h"
21 #include "lldb/Target/StopInfo.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/Unwind.h"
24 #include "lldb/Breakpoint/Watchpoint.h"
25 
26 #include "ProcessKDP.h"
27 #include "ProcessKDPLog.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 ThreadKDP::ThreadKDP (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 {
45     ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::ThreadKDP (tid = 0x%4.4x)", this, GetID());
46 }
47 
48 ThreadKDP::~ThreadKDP ()
49 {
50     ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::~ThreadKDP (tid = 0x%4.4x)", this, GetID());
51     DestroyThread();
52 }
53 
54 const char *
55 ThreadKDP::GetName ()
56 {
57     if (m_thread_name.empty())
58         return NULL;
59     return m_thread_name.c_str();
60 }
61 
62 const char *
63 ThreadKDP::GetQueueName ()
64 {
65     return NULL;
66 }
67 
68 bool
69 ThreadKDP::WillResume (StateType resume_state)
70 {
71     ClearStackFrames();
72     // Call the Thread::WillResume first. If we stop at a signal, the stop info
73     // class for signal will set the resume signal that we need below. The signal
74     // stuff obeys the Process::UnixSignal defaults.
75     Thread::WillResume(resume_state);
76 
77     lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
78     if (log)
79         log->Printf ("Resuming thread: %4.4llx with state: %s.", GetID(), StateAsCString(resume_state));
80 
81 //    ProcessKDP &process = GetKDPProcess();
82 //    switch (resume_state)
83 //    {
84 //    case eStateSuspended:
85 //    case eStateStopped:
86 //        // Don't append anything for threads that should stay stopped.
87 //        break;
88 //
89 //    case eStateRunning:
90 //    case eStateStepping:
91 //        break;
92 //
93 //    default:
94 //        break;
95 //    }
96     return true;
97 }
98 
99 void
100 ThreadKDP::RefreshStateAfterStop()
101 {
102     // Invalidate all registers in our register context. We don't set "force" to
103     // true because the stop reply packet might have had some register values
104     // that were expedited and these will already be copied into the register
105     // context by the time this function gets called. The KDPRegisterContext
106     // class has been made smart enough to detect when it needs to invalidate
107     // which registers are valid by putting hooks in the register read and
108     // register supply functions where they check the process stop ID and do
109     // the right thing.
110     const bool force = false;
111     GetRegisterContext()->InvalidateIfNeeded (force);
112 }
113 
114 void
115 ThreadKDP::ClearStackFrames ()
116 {
117     Unwind *unwinder = GetUnwinder ();
118     if (unwinder)
119         unwinder->Clear();
120     Thread::ClearStackFrames();
121 }
122 
123 
124 bool
125 ThreadKDP::ThreadIDIsValid (lldb::tid_t thread)
126 {
127     return thread != 0;
128 }
129 
130 void
131 ThreadKDP::Dump(Log *log, uint32_t index)
132 {
133 }
134 
135 
136 bool
137 ThreadKDP::ShouldStop (bool &step_more)
138 {
139     return true;
140 }
141 lldb::RegisterContextSP
142 ThreadKDP::GetRegisterContext ()
143 {
144     if (m_reg_context_sp.get() == NULL)
145         m_reg_context_sp = CreateRegisterContextForFrame (NULL);
146     return m_reg_context_sp;
147 }
148 
149 lldb::RegisterContextSP
150 ThreadKDP::CreateRegisterContextForFrame (StackFrame *frame)
151 {
152     lldb::RegisterContextSP reg_ctx_sp;
153     uint32_t concrete_frame_idx = 0;
154 
155     if (frame)
156         concrete_frame_idx = frame->GetConcreteFrameIndex ();
157 
158     if (concrete_frame_idx == 0)
159     {
160         ProcessSP process_sp (CalculateProcess());
161         if (process_sp)
162         {
163             switch (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().GetCPUType())
164             {
165                 case llvm::MachO::CPUTypeARM:
166                     reg_ctx_sp.reset (new RegisterContextKDP_arm (*this, concrete_frame_idx));
167                     break;
168                 case llvm::MachO::CPUTypeI386:
169                     reg_ctx_sp.reset (new RegisterContextKDP_i386 (*this, concrete_frame_idx));
170                     break;
171                 case llvm::MachO::CPUTypeX86_64:
172                     reg_ctx_sp.reset (new RegisterContextKDP_x86_64 (*this, concrete_frame_idx));
173                     break;
174                 default:
175                     assert (!"Add CPU type support in KDP");
176                     break;
177             }
178         }
179     }
180     else if (m_unwinder_ap.get())
181         reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
182     return reg_ctx_sp;
183 }
184 
185 lldb::StopInfoSP
186 ThreadKDP::GetPrivateStopReason ()
187 {
188     ProcessSP process_sp (GetProcess());
189     if (process_sp)
190     {
191         const uint32_t process_stop_id = process_sp->GetStopID();
192         if (m_thread_stop_reason_stop_id != process_stop_id ||
193             (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
194         {
195             // TODO: can we query the initial state of the thread here?
196             // For now I am just going to pretend that a SIGSTOP happened.
197 
198             SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP));
199 
200             // If GetKDPProcess().SetThreadStopInfo() doesn't find a stop reason
201             // for this thread, then m_actual_stop_info_sp will not ever contain
202             // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
203             // check will never be able to tell us if we have the correct stop info
204             // for this thread and we will continually send qThreadStopInfo packets
205             // down to the remote KDP server, so we need to keep our own notion
206             // of the stop ID that m_actual_stop_info_sp is valid for (even if it
207             // contains nothing). We use m_thread_stop_reason_stop_id for this below.
208     //        m_thread_stop_reason_stop_id = process_stop_id;
209     //        m_actual_stop_info_sp.reset();
210 
211         }
212     }
213     return m_actual_stop_info_sp;
214 }
215 
216 
217