1a63d08c9SGreg Clayton //===-- ThreadKDP.cpp -------------------------------------*- C++ -*-===//
2a63d08c9SGreg Clayton //
3a63d08c9SGreg Clayton //                     The LLVM Compiler Infrastructure
4a63d08c9SGreg Clayton //
5a63d08c9SGreg Clayton // This file is distributed under the University of Illinois Open Source
6a63d08c9SGreg Clayton // License. See LICENSE.TXT for details.
7a63d08c9SGreg Clayton //
8a63d08c9SGreg Clayton //===----------------------------------------------------------------------===//
9a63d08c9SGreg Clayton 
10a63d08c9SGreg Clayton 
11a63d08c9SGreg Clayton #include "ThreadKDP.h"
12a63d08c9SGreg Clayton 
13a63d08c9SGreg Clayton #include "llvm/Support/MachO.h"
14a63d08c9SGreg Clayton 
15a63d08c9SGreg Clayton #include "lldb/Core/ArchSpec.h"
16a63d08c9SGreg Clayton #include "lldb/Core/DataExtractor.h"
17a63d08c9SGreg Clayton #include "lldb/Core/StreamString.h"
18a63d08c9SGreg Clayton #include "lldb/Core/State.h"
19a63d08c9SGreg Clayton #include "lldb/Target/Process.h"
20a63d08c9SGreg Clayton #include "lldb/Target/RegisterContext.h"
21a63d08c9SGreg Clayton #include "lldb/Target/StopInfo.h"
22a63d08c9SGreg Clayton #include "lldb/Target/Target.h"
23a63d08c9SGreg Clayton #include "lldb/Target/Unwind.h"
2401a67860SJohnny Chen #include "lldb/Breakpoint/Watchpoint.h"
25a63d08c9SGreg Clayton 
26a63d08c9SGreg Clayton #include "ProcessKDP.h"
27a63d08c9SGreg Clayton #include "ProcessKDPLog.h"
28a63d08c9SGreg Clayton #include "RegisterContextKDP_arm.h"
29a63d08c9SGreg Clayton #include "RegisterContextKDP_i386.h"
30a63d08c9SGreg Clayton #include "RegisterContextKDP_x86_64.h"
3197d5cf05SGreg Clayton #include "Plugins/Process/Utility/StopInfoMachException.h"
32a63d08c9SGreg Clayton 
33a63d08c9SGreg Clayton using namespace lldb;
34a63d08c9SGreg Clayton using namespace lldb_private;
35a63d08c9SGreg Clayton 
36a63d08c9SGreg Clayton //----------------------------------------------------------------------
37a63d08c9SGreg Clayton // Thread Registers
38a63d08c9SGreg Clayton //----------------------------------------------------------------------
39a63d08c9SGreg Clayton 
40*4f465cffSJim Ingham ThreadKDP::ThreadKDP (Process &process, lldb::tid_t tid) :
41*4f465cffSJim Ingham     Thread(process, tid),
42a63d08c9SGreg Clayton     m_thread_name (),
43a63d08c9SGreg Clayton     m_dispatch_queue_name (),
44a63d08c9SGreg Clayton     m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
45a63d08c9SGreg Clayton {
461ac04c30SGreg Clayton     ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::ThreadKDP (tid = 0x%4.4x)", this, GetID());
47a63d08c9SGreg Clayton }
48a63d08c9SGreg Clayton 
49a63d08c9SGreg Clayton ThreadKDP::~ThreadKDP ()
50a63d08c9SGreg Clayton {
511ac04c30SGreg Clayton     ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::~ThreadKDP (tid = 0x%4.4x)", this, GetID());
52a63d08c9SGreg Clayton     DestroyThread();
53a63d08c9SGreg Clayton }
54a63d08c9SGreg Clayton 
55a63d08c9SGreg Clayton const char *
56a63d08c9SGreg Clayton ThreadKDP::GetName ()
57a63d08c9SGreg Clayton {
58a63d08c9SGreg Clayton     if (m_thread_name.empty())
59a63d08c9SGreg Clayton         return NULL;
60a63d08c9SGreg Clayton     return m_thread_name.c_str();
61a63d08c9SGreg Clayton }
62a63d08c9SGreg Clayton 
63a63d08c9SGreg Clayton const char *
64a63d08c9SGreg Clayton ThreadKDP::GetQueueName ()
65a63d08c9SGreg Clayton {
66a63d08c9SGreg Clayton     return NULL;
67a63d08c9SGreg Clayton }
68a63d08c9SGreg Clayton 
69a63d08c9SGreg Clayton bool
70a63d08c9SGreg Clayton ThreadKDP::WillResume (StateType resume_state)
71a63d08c9SGreg Clayton {
72a63d08c9SGreg Clayton     // Call the Thread::WillResume first. If we stop at a signal, the stop info
73a63d08c9SGreg Clayton     // class for signal will set the resume signal that we need below. The signal
74a63d08c9SGreg Clayton     // stuff obeys the Process::UnixSignal defaults.
75a63d08c9SGreg Clayton     Thread::WillResume(resume_state);
76a63d08c9SGreg Clayton 
7797d5cf05SGreg Clayton     ClearStackFrames();
7897d5cf05SGreg Clayton 
79a63d08c9SGreg Clayton     lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
80a63d08c9SGreg Clayton     if (log)
8181c22f61SGreg Clayton         log->Printf ("Resuming thread: %4.4llx with state: %s.", GetID(), StateAsCString(resume_state));
82a63d08c9SGreg Clayton 
83a63d08c9SGreg Clayton     return true;
84a63d08c9SGreg Clayton }
85a63d08c9SGreg Clayton 
86a63d08c9SGreg Clayton void
87a63d08c9SGreg Clayton ThreadKDP::RefreshStateAfterStop()
88a63d08c9SGreg Clayton {
89a63d08c9SGreg Clayton     // Invalidate all registers in our register context. We don't set "force" to
90a63d08c9SGreg Clayton     // true because the stop reply packet might have had some register values
91a63d08c9SGreg Clayton     // that were expedited and these will already be copied into the register
92a63d08c9SGreg Clayton     // context by the time this function gets called. The KDPRegisterContext
93a63d08c9SGreg Clayton     // class has been made smart enough to detect when it needs to invalidate
94a63d08c9SGreg Clayton     // which registers are valid by putting hooks in the register read and
95a63d08c9SGreg Clayton     // register supply functions where they check the process stop ID and do
96a63d08c9SGreg Clayton     // the right thing.
97a63d08c9SGreg Clayton     const bool force = false;
98a63d08c9SGreg Clayton     GetRegisterContext()->InvalidateIfNeeded (force);
99a63d08c9SGreg Clayton }
100a63d08c9SGreg Clayton 
101a63d08c9SGreg Clayton void
102a63d08c9SGreg Clayton ThreadKDP::ClearStackFrames ()
103a63d08c9SGreg Clayton {
104a63d08c9SGreg Clayton     Unwind *unwinder = GetUnwinder ();
105a63d08c9SGreg Clayton     if (unwinder)
106a63d08c9SGreg Clayton         unwinder->Clear();
107a63d08c9SGreg Clayton     Thread::ClearStackFrames();
108a63d08c9SGreg Clayton }
109a63d08c9SGreg Clayton 
110a63d08c9SGreg Clayton 
111a63d08c9SGreg Clayton bool
112a63d08c9SGreg Clayton ThreadKDP::ThreadIDIsValid (lldb::tid_t thread)
113a63d08c9SGreg Clayton {
114a63d08c9SGreg Clayton     return thread != 0;
115a63d08c9SGreg Clayton }
116a63d08c9SGreg Clayton 
117a63d08c9SGreg Clayton void
118a63d08c9SGreg Clayton ThreadKDP::Dump(Log *log, uint32_t index)
119a63d08c9SGreg Clayton {
120a63d08c9SGreg Clayton }
121a63d08c9SGreg Clayton 
122a63d08c9SGreg Clayton 
123a63d08c9SGreg Clayton bool
124a63d08c9SGreg Clayton ThreadKDP::ShouldStop (bool &step_more)
125a63d08c9SGreg Clayton {
126a63d08c9SGreg Clayton     return true;
127a63d08c9SGreg Clayton }
128a63d08c9SGreg Clayton lldb::RegisterContextSP
129a63d08c9SGreg Clayton ThreadKDP::GetRegisterContext ()
130a63d08c9SGreg Clayton {
131a63d08c9SGreg Clayton     if (m_reg_context_sp.get() == NULL)
132a63d08c9SGreg Clayton         m_reg_context_sp = CreateRegisterContextForFrame (NULL);
133a63d08c9SGreg Clayton     return m_reg_context_sp;
134a63d08c9SGreg Clayton }
135a63d08c9SGreg Clayton 
136a63d08c9SGreg Clayton lldb::RegisterContextSP
137a63d08c9SGreg Clayton ThreadKDP::CreateRegisterContextForFrame (StackFrame *frame)
138a63d08c9SGreg Clayton {
139a63d08c9SGreg Clayton     lldb::RegisterContextSP reg_ctx_sp;
140a63d08c9SGreg Clayton     uint32_t concrete_frame_idx = 0;
141a63d08c9SGreg Clayton 
142a63d08c9SGreg Clayton     if (frame)
143a63d08c9SGreg Clayton         concrete_frame_idx = frame->GetConcreteFrameIndex ();
144a63d08c9SGreg Clayton 
145a63d08c9SGreg Clayton     if (concrete_frame_idx == 0)
146a63d08c9SGreg Clayton     {
1471ac04c30SGreg Clayton         ProcessSP process_sp (CalculateProcess());
1481ac04c30SGreg Clayton         if (process_sp)
1491ac04c30SGreg Clayton         {
1501ac04c30SGreg Clayton             switch (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().GetCPUType())
151a63d08c9SGreg Clayton             {
152a63d08c9SGreg Clayton                 case llvm::MachO::CPUTypeARM:
153a63d08c9SGreg Clayton                     reg_ctx_sp.reset (new RegisterContextKDP_arm (*this, concrete_frame_idx));
154a63d08c9SGreg Clayton                     break;
155a63d08c9SGreg Clayton                 case llvm::MachO::CPUTypeI386:
156a63d08c9SGreg Clayton                     reg_ctx_sp.reset (new RegisterContextKDP_i386 (*this, concrete_frame_idx));
157a63d08c9SGreg Clayton                     break;
158a63d08c9SGreg Clayton                 case llvm::MachO::CPUTypeX86_64:
159a63d08c9SGreg Clayton                     reg_ctx_sp.reset (new RegisterContextKDP_x86_64 (*this, concrete_frame_idx));
160a63d08c9SGreg Clayton                     break;
161a63d08c9SGreg Clayton                 default:
162a63d08c9SGreg Clayton                     assert (!"Add CPU type support in KDP");
163a63d08c9SGreg Clayton                     break;
164a63d08c9SGreg Clayton             }
165a63d08c9SGreg Clayton         }
1661ac04c30SGreg Clayton     }
167a63d08c9SGreg Clayton     else if (m_unwinder_ap.get())
168a63d08c9SGreg Clayton         reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
169a63d08c9SGreg Clayton     return reg_ctx_sp;
170a63d08c9SGreg Clayton }
171a63d08c9SGreg Clayton 
172a63d08c9SGreg Clayton lldb::StopInfoSP
173a63d08c9SGreg Clayton ThreadKDP::GetPrivateStopReason ()
174a63d08c9SGreg Clayton {
1751ac04c30SGreg Clayton     ProcessSP process_sp (GetProcess());
1761ac04c30SGreg Clayton     if (process_sp)
1771ac04c30SGreg Clayton     {
1781ac04c30SGreg Clayton         const uint32_t process_stop_id = process_sp->GetStopID();
179a63d08c9SGreg Clayton         if (m_thread_stop_reason_stop_id != process_stop_id ||
180a63d08c9SGreg Clayton             (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
181a63d08c9SGreg Clayton         {
18297d5cf05SGreg Clayton             if (m_cached_stop_info_sp)
18397d5cf05SGreg Clayton                 SetStopInfo (m_cached_stop_info_sp);
18497d5cf05SGreg Clayton             else
185a63d08c9SGreg Clayton                 SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP));
186a63d08c9SGreg Clayton         }
1871ac04c30SGreg Clayton     }
188a63d08c9SGreg Clayton     return m_actual_stop_info_sp;
189a63d08c9SGreg Clayton }
190a63d08c9SGreg Clayton 
19197d5cf05SGreg Clayton void
19297d5cf05SGreg Clayton ThreadKDP::SetStopInfoFrom_KDP_EXCEPTION (const DataExtractor &exc_reply_packet)
19397d5cf05SGreg Clayton {
19497d5cf05SGreg Clayton     uint32_t offset = 0;
19597d5cf05SGreg Clayton     uint8_t reply_command = exc_reply_packet.GetU8(&offset);
19697d5cf05SGreg Clayton     if (reply_command == CommunicationKDP::KDP_EXCEPTION)
19797d5cf05SGreg Clayton     {
19897d5cf05SGreg Clayton         offset = 8;
19997d5cf05SGreg Clayton         const uint32_t count = exc_reply_packet.GetU32 (&offset);
20097d5cf05SGreg Clayton         if (count >= 1)
20197d5cf05SGreg Clayton         {
20297d5cf05SGreg Clayton             //const uint32_t cpu = exc_reply_packet.GetU32 (&offset);
20397d5cf05SGreg Clayton             offset += 4; // Skip the useless CPU field
20497d5cf05SGreg Clayton             const uint32_t exc_type = exc_reply_packet.GetU32 (&offset);
20597d5cf05SGreg Clayton             const uint32_t exc_code = exc_reply_packet.GetU32 (&offset);
20697d5cf05SGreg Clayton             const uint32_t exc_subcode = exc_reply_packet.GetU32 (&offset);
20797d5cf05SGreg Clayton             // We have to make a copy of the stop info because the thread list
20897d5cf05SGreg Clayton             // will iterate through the threads and clear all stop infos..
20997d5cf05SGreg Clayton 
21097d5cf05SGreg Clayton             // Let the StopInfoMachException::CreateStopReasonWithMachException()
21197d5cf05SGreg Clayton             // function update the PC if needed as we might hit a software breakpoint
21297d5cf05SGreg Clayton             // and need to decrement the PC (i386 and x86_64 need this) and KDP
21397d5cf05SGreg Clayton             // doesn't do this for us.
21497d5cf05SGreg Clayton             const bool pc_already_adjusted = false;
21597d5cf05SGreg Clayton             const bool adjust_pc_if_needed = true;
21697d5cf05SGreg Clayton 
21797d5cf05SGreg Clayton             m_cached_stop_info_sp = StopInfoMachException::CreateStopReasonWithMachException (*this,
21897d5cf05SGreg Clayton                                                                                               exc_type,
21997d5cf05SGreg Clayton                                                                                               2,
22097d5cf05SGreg Clayton                                                                                               exc_code,
22197d5cf05SGreg Clayton                                                                                               exc_subcode,
22297d5cf05SGreg Clayton                                                                                               0,
22397d5cf05SGreg Clayton                                                                                               pc_already_adjusted,
22497d5cf05SGreg Clayton                                                                                               adjust_pc_if_needed);
22597d5cf05SGreg Clayton         }
22697d5cf05SGreg Clayton     }
22797d5cf05SGreg Clayton }
228a63d08c9SGreg Clayton 
229