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),
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().get();
62         }
63     }
64 }
65 
66 ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
67     m_target_sp (process ? &process->GetTarget() : NULL),
68     m_process_sp (process),
69     m_thread_sp (thread),
70     m_frame_sp (frame)
71 {
72 }
73 
74 ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr)
75 {
76     if (exe_scope_ptr)
77         exe_scope_ptr->CalculateExecutionContext (*this);
78     else
79     {
80         m_target_sp.reset();
81         m_process_sp.reset();
82         m_thread_sp.reset();
83         m_frame_sp.reset();
84     }
85 }
86 
87 ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
88 {
89     exe_scope_ref.CalculateExecutionContext (*this);
90 }
91 
92 void
93 ExecutionContext::Clear()
94 {
95     m_target_sp.reset();
96     m_process_sp.reset();
97     m_thread_sp.reset();
98     m_frame_sp.reset();
99 }
100 
101 ExecutionContext::~ExecutionContext()
102 {
103 }
104 
105 
106 RegisterContext *
107 ExecutionContext::GetRegisterContext () const
108 {
109     if (m_frame_sp)
110         return m_frame_sp->GetRegisterContext().get();
111     else if (m_thread_sp)
112         return m_thread_sp->GetRegisterContext().get();
113     return NULL;
114 }
115 
116 Target *
117 ExecutionContext::GetTargetPtr () const
118 {
119     if (m_target_sp)
120         return m_target_sp.get();
121     if (m_process_sp)
122         return &m_process_sp->GetTarget();
123     return NULL;
124 }
125 
126 Process *
127 ExecutionContext::GetProcessPtr () const
128 {
129     if (m_process_sp)
130         return m_process_sp.get();
131     if (m_target_sp)
132         return m_target_sp->GetProcessSP().get();
133     return NULL;
134 }
135 
136 ExecutionContextScope *
137 ExecutionContext::GetBestExecutionContextScope () const
138 {
139     if (m_frame_sp)
140         return m_frame_sp.get();
141     if (m_thread_sp)
142         return m_thread_sp.get();
143     if (m_process_sp)
144         return m_process_sp.get();
145     return m_target_sp.get();
146 }
147 
148 Target &
149 ExecutionContext::GetTargetRef () const
150 {
151     assert (m_target_sp.get());
152     return *m_target_sp;
153 }
154 
155 Process &
156 ExecutionContext::GetProcessRef () const
157 {
158     assert (m_process_sp.get());
159     return *m_process_sp;
160 }
161 
162 Thread &
163 ExecutionContext::GetThreadRef () const
164 {
165     assert (m_thread_sp.get());
166     return *m_thread_sp;
167 }
168 
169 StackFrame &
170 ExecutionContext::GetFrameRef () const
171 {
172     assert (m_frame_sp.get());
173     return *m_frame_sp;
174 }
175 
176 void
177 ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
178 {
179     m_target_sp = target_sp;
180 }
181 
182 void
183 ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
184 {
185     m_process_sp = process_sp;
186 }
187 
188 void
189 ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
190 {
191     m_thread_sp = thread_sp;
192 }
193 
194 void
195 ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
196 {
197     m_frame_sp = frame_sp;
198 }
199 
200 void
201 ExecutionContext::SetTargetPtr (Target* target)
202 {
203     m_target_sp = target;
204 }
205 
206 void
207 ExecutionContext::SetProcessPtr (Process *process)
208 {
209     m_process_sp = process;
210 }
211 
212 void
213 ExecutionContext::SetThreadPtr (Thread *thread)
214 {
215     m_thread_sp = thread;
216 }
217 
218 void
219 ExecutionContext::SetFramePtr (StackFrame *frame)
220 {
221     m_frame_sp = frame;
222 }
223 
224