1 //===-- ExecutionContext.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 "lldb/Target/ExecutionContext.h"
11 #include "lldb/Target/ExecutionContextScope.h"
12 #include "lldb/Target/StackFrame.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Target/Thread.h"
16 
17 using namespace lldb_private;
18 
19 ExecutionContext::ExecutionContext() :
20     m_target_sp (),
21     m_process_sp (),
22     m_thread_sp (),
23     m_frame_sp ()
24 {
25 }
26 
27 ExecutionContext::ExecutionContext (const ExecutionContext &rhs) :
28     m_target_sp (rhs.m_target_sp),
29     m_process_sp(rhs.m_process_sp),
30     m_thread_sp (rhs.m_thread_sp),
31     m_frame_sp  (rhs.m_frame_sp)
32 {
33 }
34 
35 ExecutionContext &
36 ExecutionContext::operator =(const ExecutionContext &rhs)
37 {
38     if (this != &rhs)
39     {
40         m_target_sp  = rhs.m_target_sp;
41         m_process_sp = rhs.m_process_sp;
42         m_thread_sp  = rhs.m_thread_sp;
43         m_frame_sp   = rhs.m_frame_sp;
44     }
45     return *this;
46 }
47 
48 ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
49     m_target_sp (t->shared_from_this()),
50     m_process_sp (),
51     m_thread_sp (),
52     m_frame_sp ()
53 {
54     if (t && fill_current_process_thread_frame)
55     {
56         m_process_sp = t->GetProcessSP();
57         if (m_process_sp)
58         {
59             m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
60             if (m_thread_sp)
61                 m_frame_sp = m_thread_sp->GetSelectedFrame();
62         }
63     }
64 }
65 
66 ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
67     m_target_sp (),
68     m_process_sp (process->shared_from_this()),
69     m_thread_sp (thread->shared_from_this()),
70     m_frame_sp (frame->shared_from_this())
71 {
72     if (process)
73         m_target_sp = process->GetTarget().shared_from_this();
74 }
75 
76 ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr)
77 {
78     if (exe_scope_ptr)
79         exe_scope_ptr->CalculateExecutionContext (*this);
80     else
81     {
82         m_target_sp.reset();
83         m_process_sp.reset();
84         m_thread_sp.reset();
85         m_frame_sp.reset();
86     }
87 }
88 
89 ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
90 {
91     exe_scope_ref.CalculateExecutionContext (*this);
92 }
93 
94 void
95 ExecutionContext::Clear()
96 {
97     m_target_sp.reset();
98     m_process_sp.reset();
99     m_thread_sp.reset();
100     m_frame_sp.reset();
101 }
102 
103 ExecutionContext::~ExecutionContext()
104 {
105 }
106 
107 
108 RegisterContext *
109 ExecutionContext::GetRegisterContext () const
110 {
111     if (m_frame_sp)
112         return m_frame_sp->GetRegisterContext().get();
113     else if (m_thread_sp)
114         return m_thread_sp->GetRegisterContext().get();
115     return NULL;
116 }
117 
118 Target *
119 ExecutionContext::GetTargetPtr () const
120 {
121     if (m_target_sp)
122         return m_target_sp.get();
123     if (m_process_sp)
124         return &m_process_sp->GetTarget();
125     return NULL;
126 }
127 
128 Process *
129 ExecutionContext::GetProcessPtr () const
130 {
131     if (m_process_sp)
132         return m_process_sp.get();
133     if (m_target_sp)
134         return m_target_sp->GetProcessSP().get();
135     return NULL;
136 }
137 
138 ExecutionContextScope *
139 ExecutionContext::GetBestExecutionContextScope () const
140 {
141     if (m_frame_sp)
142         return m_frame_sp.get();
143     if (m_thread_sp)
144         return m_thread_sp.get();
145     if (m_process_sp)
146         return m_process_sp.get();
147     return m_target_sp.get();
148 }
149 
150 Target &
151 ExecutionContext::GetTargetRef () const
152 {
153     assert (m_target_sp.get());
154     return *m_target_sp;
155 }
156 
157 Process &
158 ExecutionContext::GetProcessRef () const
159 {
160     assert (m_process_sp.get());
161     return *m_process_sp;
162 }
163 
164 Thread &
165 ExecutionContext::GetThreadRef () const
166 {
167     assert (m_thread_sp.get());
168     return *m_thread_sp;
169 }
170 
171 StackFrame &
172 ExecutionContext::GetFrameRef () const
173 {
174     assert (m_frame_sp.get());
175     return *m_frame_sp;
176 }
177 
178 void
179 ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
180 {
181     m_target_sp = target_sp;
182 }
183 
184 void
185 ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
186 {
187     m_process_sp = process_sp;
188 }
189 
190 void
191 ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
192 {
193     m_thread_sp = thread_sp;
194 }
195 
196 void
197 ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
198 {
199     m_frame_sp = frame_sp;
200 }
201 
202 void
203 ExecutionContext::SetTargetPtr (Target* target)
204 {
205     if (target)
206         m_target_sp = target->shared_from_this();
207     else
208         m_target_sp.reset();
209 }
210 
211 void
212 ExecutionContext::SetProcessPtr (Process *process)
213 {
214     if (process)
215         m_process_sp = process->shared_from_this();
216     else
217         m_process_sp.reset();
218 }
219 
220 void
221 ExecutionContext::SetThreadPtr (Thread *thread)
222 {
223     if (thread)
224         m_thread_sp = thread->shared_from_this();
225     else
226         m_thread_sp.reset();
227 }
228 
229 void
230 ExecutionContext::SetFramePtr (StackFrame *frame)
231 {
232     if (frame)
233         m_frame_sp = frame->shared_from_this();
234     else
235         m_frame_sp.reset();
236 }
237 
238