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