1 //===-- ThreadMemory.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 "Plugins/Process/Utility/ThreadMemory.h"
11 #include "lldb/Target/OperatingSystem.h"
12 #include "lldb/Target/RegisterContext.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/StopInfo.h"
15 #include "lldb/Target/Unwind.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 ThreadMemory::ThreadMemory (Process &process,
21                             tid_t tid,
22                             const ValueObjectSP &thread_info_valobj_sp) :
23     Thread (process, tid),
24     m_backing_thread_sp (),
25     m_thread_info_valobj_sp (thread_info_valobj_sp),
26     m_name(),
27     m_queue()
28 {
29 }
30 
31 
32 ThreadMemory::ThreadMemory (Process &process,
33                             lldb::tid_t tid,
34                             const char *name,
35                             const char *queue,
36                             lldb::addr_t register_data_addr) :
37     Thread (process, tid),
38     m_backing_thread_sp (),
39     m_thread_info_valobj_sp (),
40     m_name(),
41     m_queue(),
42     m_register_data_addr (register_data_addr)
43 {
44     if (name)
45         m_name = name;
46     if (queue)
47         m_queue = queue;
48 }
49 
50 
51 ThreadMemory::~ThreadMemory()
52 {
53     DestroyThread();
54 }
55 
56 bool
57 ThreadMemory::WillResume (StateType resume_state)
58 {
59     ClearStackFrames();
60     Thread::WillResume(resume_state);
61 
62     if (m_backing_thread_sp)
63         return m_backing_thread_sp->WillResume(resume_state);
64     return true;
65 }
66 
67 RegisterContextSP
68 ThreadMemory::GetRegisterContext ()
69 {
70     if (m_backing_thread_sp)
71         return m_backing_thread_sp->GetRegisterContext();
72 
73     if (!m_reg_context_sp)
74     {
75         ProcessSP process_sp (GetProcess());
76         if (process_sp)
77         {
78             OperatingSystem *os = process_sp->GetOperatingSystem ();
79             if (os)
80                 m_reg_context_sp = os->CreateRegisterContextForThread (this, m_register_data_addr);
81         }
82     }
83     return m_reg_context_sp;
84 }
85 
86 RegisterContextSP
87 ThreadMemory::CreateRegisterContextForFrame (StackFrame *frame)
88 {
89     if (m_backing_thread_sp)
90         return m_backing_thread_sp->CreateRegisterContextForFrame(frame);
91 
92     RegisterContextSP reg_ctx_sp;
93     uint32_t concrete_frame_idx = 0;
94 
95     if (frame)
96         concrete_frame_idx = frame->GetConcreteFrameIndex ();
97 
98     if (concrete_frame_idx == 0)
99     {
100         reg_ctx_sp = GetRegisterContext ();
101     }
102     else
103     {
104         Unwind *unwinder = GetUnwinder ();
105         if (unwinder)
106             reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
107     }
108     return reg_ctx_sp;
109 }
110 
111 lldb::StopInfoSP
112 ThreadMemory::GetPrivateStopReason ()
113 {
114     if (m_backing_thread_sp)
115         return m_backing_thread_sp->GetPrivateStopReason();
116 
117     ProcessSP process_sp (GetProcess());
118 
119     if (process_sp)
120     {
121         const uint32_t process_stop_id = process_sp->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             if (IsStillAtLastBreakpointHit())
126                 return m_actual_stop_info_sp;
127 
128             // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
129             // for this thread, then m_actual_stop_info_sp will not ever contain
130             // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
131             // check will never be able to tell us if we have the correct stop info
132             // for this thread and we will continually send qThreadStopInfo packets
133             // down to the remote GDB server, so we need to keep our own notion
134             // of the stop ID that m_actual_stop_info_sp is valid for (even if it
135             // contains nothing). We use m_thread_stop_reason_stop_id for this below.
136             m_thread_stop_reason_stop_id = process_stop_id;
137             m_actual_stop_info_sp.reset();
138 
139             OperatingSystem *os = process_sp->GetOperatingSystem ();
140             if (os)
141                 m_actual_stop_info_sp = os->CreateThreadStopReason (this);
142         }
143     }
144     return m_actual_stop_info_sp;
145 
146 }
147 
148 void
149 ThreadMemory::RefreshStateAfterStop()
150 {
151     if (m_backing_thread_sp)
152         return m_backing_thread_sp->RefreshStateAfterStop();
153 
154 
155     // Don't fetch the registers by calling Thread::GetRegisterContext() below.
156     // We might not have fetched any registers yet and we don't want to fetch
157     // the registers just to call invalidate on them...
158     RegisterContextSP reg_ctx_sp(m_reg_context_sp);
159     if (reg_ctx_sp)
160     {
161         const bool force = true;
162         reg_ctx_sp->InvalidateIfNeeded (force);
163     }
164 }
165