blob: 7a8b60189bc804660821bce2b485f131e89f6d87 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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//===----------------------------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00009
10#include "lldb/Target/ExecutionContext.h"
Greg Clayton9b5450f2012-07-30 22:05:39 +000011
12#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Target/ExecutionContextScope.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000014#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Thread.h"
18
19using namespace lldb_private;
20
21ExecutionContext::ExecutionContext() :
Greg Claytonc14ee322011-09-22 04:58:26 +000022 m_target_sp (),
23 m_process_sp (),
24 m_thread_sp (),
25 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026{
27}
28
Greg Claytonc14ee322011-09-22 04:58:26 +000029ExecutionContext::ExecutionContext (const ExecutionContext &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +000030 m_target_sp(rhs.m_target_sp),
Greg Claytonc14ee322011-09-22 04:58:26 +000031 m_process_sp(rhs.m_process_sp),
Greg Claytoncc4d0142012-02-17 07:49:44 +000032 m_thread_sp(rhs.m_thread_sp),
33 m_frame_sp(rhs.m_frame_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +000034{
35}
36
Greg Claytoncc4d0142012-02-17 07:49:44 +000037ExecutionContext::ExecutionContext (const lldb::TargetSP &target_sp, bool get_process) :
38 m_target_sp (),
39 m_process_sp (),
40 m_thread_sp (),
41 m_frame_sp ()
Greg Claytonc14ee322011-09-22 04:58:26 +000042{
Greg Claytoncc4d0142012-02-17 07:49:44 +000043 if (target_sp)
44 SetContext (target_sp, get_process);
45}
46
47ExecutionContext::ExecutionContext (const lldb::ProcessSP &process_sp) :
48 m_target_sp (),
49 m_process_sp (),
50 m_thread_sp (),
51 m_frame_sp ()
52{
53 if (process_sp)
54 SetContext (process_sp);
55}
56
57ExecutionContext::ExecutionContext (const lldb::ThreadSP &thread_sp) :
58 m_target_sp (),
59 m_process_sp (),
60 m_thread_sp (),
61 m_frame_sp ()
62{
63 if (thread_sp)
64 SetContext (thread_sp);
65}
66
Jason Molendab57e4a12013-11-04 09:33:30 +000067ExecutionContext::ExecutionContext (const lldb::StackFrameSP &frame_sp) :
Greg Claytoncc4d0142012-02-17 07:49:44 +000068 m_target_sp (),
69 m_process_sp (),
70 m_thread_sp (),
71 m_frame_sp ()
72{
73 if (frame_sp)
74 SetContext (frame_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +000075}
76
Greg Clayton1ac04c32012-02-21 00:09:25 +000077ExecutionContext::ExecutionContext (const lldb::TargetWP &target_wp, bool get_process) :
78 m_target_sp (),
79 m_process_sp (),
80 m_thread_sp (),
81 m_frame_sp ()
82{
83 lldb::TargetSP target_sp(target_wp.lock());
84 if (target_sp)
85 SetContext (target_sp, get_process);
86}
87
88ExecutionContext::ExecutionContext (const lldb::ProcessWP &process_wp) :
89 m_target_sp (),
90 m_process_sp (),
91 m_thread_sp (),
92 m_frame_sp ()
93{
94 lldb::ProcessSP process_sp(process_wp.lock());
95 if (process_sp)
96 SetContext (process_sp);
97}
98
99ExecutionContext::ExecutionContext (const lldb::ThreadWP &thread_wp) :
100 m_target_sp (),
101 m_process_sp (),
102 m_thread_sp (),
103 m_frame_sp ()
104{
105 lldb::ThreadSP thread_sp(thread_wp.lock());
106 if (thread_sp)
107 SetContext (thread_sp);
108}
109
Jason Molendab57e4a12013-11-04 09:33:30 +0000110ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
Greg Clayton1ac04c32012-02-21 00:09:25 +0000111 m_target_sp (),
112 m_process_sp (),
113 m_thread_sp (),
114 m_frame_sp ()
115{
Jason Molendab57e4a12013-11-04 09:33:30 +0000116 lldb::StackFrameSP frame_sp(frame_wp.lock());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000117 if (frame_sp)
118 SetContext (frame_sp);
119}
120
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
Greg Claytone1cd1be2012-01-29 20:56:30 +0000122 m_target_sp (t->shared_from_this()),
Greg Claytonc14ee322011-09-22 04:58:26 +0000123 m_process_sp (),
124 m_thread_sp (),
125 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126{
127 if (t && fill_current_process_thread_frame)
128 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000129 m_process_sp = t->GetProcessSP();
130 if (m_process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000132 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
133 if (m_thread_sp)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000134 m_frame_sp = m_thread_sp->GetSelectedFrame();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 }
136 }
137}
138
Jason Molendab57e4a12013-11-04 09:33:30 +0000139ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
Greg Claytone1cd1be2012-01-29 20:56:30 +0000140 m_target_sp (),
141 m_process_sp (process->shared_from_this()),
142 m_thread_sp (thread->shared_from_this()),
143 m_frame_sp (frame->shared_from_this())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000145 if (process)
146 m_target_sp = process->GetTarget().shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Greg Claytoncc4d0142012-02-17 07:49:44 +0000149ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref) :
150 m_target_sp (exe_ctx_ref.GetTargetSP()),
151 m_process_sp (exe_ctx_ref.GetProcessSP()),
152 m_thread_sp (exe_ctx_ref.GetThreadSP()),
153 m_frame_sp (exe_ctx_ref.GetFrameSP())
154{
155}
156
157ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr) :
158 m_target_sp (),
159 m_process_sp (),
160 m_thread_sp (),
161 m_frame_sp ()
162{
163 if (exe_ctx_ref_ptr)
164 {
165 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
166 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
167 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
168 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
169 }
170}
171
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000172ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr, Mutex::Locker &locker) :
173 m_target_sp (),
174 m_process_sp (),
175 m_thread_sp (),
176 m_frame_sp ()
177{
178 if (exe_ctx_ref_ptr)
179 {
180 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
181 if (m_target_sp)
182 {
183 locker.Lock(m_target_sp->GetAPIMutex());
184 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
185 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
186 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
187 }
188 }
189}
190
191ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref, Mutex::Locker &locker) :
192 m_target_sp (exe_ctx_ref.GetTargetSP()),
193 m_process_sp (),
194 m_thread_sp (),
195 m_frame_sp ()
196{
197 if (m_target_sp)
198 {
199 locker.Lock(m_target_sp->GetAPIMutex());
200 m_process_sp = exe_ctx_ref.GetProcessSP();
201 m_thread_sp = exe_ctx_ref.GetThreadSP();
202 m_frame_sp = exe_ctx_ref.GetFrameSP();
203 }
204}
205
Greg Claytoncc4d0142012-02-17 07:49:44 +0000206ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
207 m_target_sp (),
208 m_process_sp (),
209 m_thread_sp (),
210 m_frame_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211{
212 if (exe_scope_ptr)
Greg Clayton0603aa92010-10-04 01:05:56 +0000213 exe_scope_ptr->CalculateExecutionContext (*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214}
215
216ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
217{
Greg Clayton0603aa92010-10-04 01:05:56 +0000218 exe_scope_ref.CalculateExecutionContext (*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219}
220
221void
222ExecutionContext::Clear()
223{
Greg Claytonc14ee322011-09-22 04:58:26 +0000224 m_target_sp.reset();
225 m_process_sp.reset();
226 m_thread_sp.reset();
227 m_frame_sp.reset();
228}
229
230ExecutionContext::~ExecutionContext()
231{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232}
233
Greg Claytoncc4d0142012-02-17 07:49:44 +0000234uint32_t
235ExecutionContext::GetAddressByteSize() const
236{
237 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
238 m_target_sp->GetArchitecture().GetAddressByteSize();
239 if (m_process_sp)
240 m_process_sp->GetAddressByteSize();
241 return sizeof(void *);
242}
243
Enrico Granata347c2aa2013-10-08 21:49:02 +0000244lldb::ByteOrder
245ExecutionContext::GetByteOrder() const
246{
247 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
248 m_target_sp->GetArchitecture().GetByteOrder();
249 if (m_process_sp)
250 m_process_sp->GetByteOrder();
251 return lldb::endian::InlHostByteOrder();
252}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253
254RegisterContext *
255ExecutionContext::GetRegisterContext () const
256{
Greg Claytonc14ee322011-09-22 04:58:26 +0000257 if (m_frame_sp)
258 return m_frame_sp->GetRegisterContext().get();
259 else if (m_thread_sp)
260 return m_thread_sp->GetRegisterContext().get();
261 return NULL;
262}
263
264Target *
265ExecutionContext::GetTargetPtr () const
266{
267 if (m_target_sp)
268 return m_target_sp.get();
269 if (m_process_sp)
270 return &m_process_sp->GetTarget();
271 return NULL;
272}
273
274Process *
275ExecutionContext::GetProcessPtr () const
276{
277 if (m_process_sp)
278 return m_process_sp.get();
279 if (m_target_sp)
280 return m_target_sp->GetProcessSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 return NULL;
282}
283
284ExecutionContextScope *
285ExecutionContext::GetBestExecutionContextScope () const
286{
Greg Claytonc14ee322011-09-22 04:58:26 +0000287 if (m_frame_sp)
288 return m_frame_sp.get();
289 if (m_thread_sp)
290 return m_thread_sp.get();
291 if (m_process_sp)
292 return m_process_sp.get();
293 return m_target_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294}
Greg Clayton644247c2011-07-07 01:59:51 +0000295
Greg Claytonc14ee322011-09-22 04:58:26 +0000296Target &
297ExecutionContext::GetTargetRef () const
Greg Clayton644247c2011-07-07 01:59:51 +0000298{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000299#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000300 assert (m_target_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000301#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000302 return *m_target_sp;
Greg Clayton644247c2011-07-07 01:59:51 +0000303}
Greg Claytonc14ee322011-09-22 04:58:26 +0000304
305Process &
306ExecutionContext::GetProcessRef () const
307{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000308#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000309 assert (m_process_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000310#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000311 return *m_process_sp;
312}
313
314Thread &
315ExecutionContext::GetThreadRef () const
316{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000317#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000318 assert (m_thread_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000319#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000320 return *m_thread_sp;
321}
322
Jason Molendab57e4a12013-11-04 09:33:30 +0000323StackFrame &
Greg Claytonc14ee322011-09-22 04:58:26 +0000324ExecutionContext::GetFrameRef () const
325{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000326#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Claytonc14ee322011-09-22 04:58:26 +0000327 assert (m_frame_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000328#endif
Greg Claytonc14ee322011-09-22 04:58:26 +0000329 return *m_frame_sp;
330}
331
332void
333ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
334{
335 m_target_sp = target_sp;
336}
337
338void
339ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
340{
341 m_process_sp = process_sp;
342}
343
344void
345ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
346{
347 m_thread_sp = thread_sp;
348}
349
350void
Jason Molendab57e4a12013-11-04 09:33:30 +0000351ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytonc14ee322011-09-22 04:58:26 +0000352{
353 m_frame_sp = frame_sp;
354}
355
356void
357ExecutionContext::SetTargetPtr (Target* target)
358{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000359 if (target)
360 m_target_sp = target->shared_from_this();
361 else
362 m_target_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000363}
364
365void
366ExecutionContext::SetProcessPtr (Process *process)
367{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000368 if (process)
369 m_process_sp = process->shared_from_this();
370 else
371 m_process_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000372}
373
374void
375ExecutionContext::SetThreadPtr (Thread *thread)
376{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000377 if (thread)
378 m_thread_sp = thread->shared_from_this();
379 else
380 m_thread_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000381}
382
383void
Jason Molendab57e4a12013-11-04 09:33:30 +0000384ExecutionContext::SetFramePtr (StackFrame *frame)
Greg Claytonc14ee322011-09-22 04:58:26 +0000385{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000386 if (frame)
387 m_frame_sp = frame->shared_from_this();
388 else
389 m_frame_sp.reset();
Greg Claytonc14ee322011-09-22 04:58:26 +0000390}
391
Greg Claytoncc4d0142012-02-17 07:49:44 +0000392void
393ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
394{
395 m_target_sp = target_sp;
396 if (get_process && target_sp)
397 m_process_sp = target_sp->GetProcessSP();
398 else
399 m_process_sp.reset();
400 m_thread_sp.reset();
401 m_frame_sp.reset();
402}
403
404void
405ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
406{
407 m_process_sp = process_sp;
408 if (process_sp)
409 m_target_sp = process_sp->GetTarget().shared_from_this();
410 else
411 m_target_sp.reset();
412 m_thread_sp.reset();
413 m_frame_sp.reset();
414}
415
416void
417ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
418{
419 m_frame_sp.reset();
420 m_thread_sp = thread_sp;
421 if (thread_sp)
422 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000423 m_process_sp = thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000424 if (m_process_sp)
425 m_target_sp = m_process_sp->GetTarget().shared_from_this();
426 else
427 m_target_sp.reset();
428 }
429 else
430 {
431 m_target_sp.reset();
432 m_process_sp.reset();
433 }
434}
435
436void
Jason Molendab57e4a12013-11-04 09:33:30 +0000437ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000438{
439 m_frame_sp = frame_sp;
440 if (frame_sp)
441 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000442 m_thread_sp = frame_sp->CalculateThread();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000443 if (m_thread_sp)
444 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000445 m_process_sp = m_thread_sp->GetProcess();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000446 if (m_process_sp)
447 m_target_sp = m_process_sp->GetTarget().shared_from_this();
448 else
449 m_target_sp.reset();
450 }
451 else
452 {
453 m_target_sp.reset();
454 m_process_sp.reset();
455 }
456 }
457 else
458 {
459 m_target_sp.reset();
460 m_process_sp.reset();
461 m_thread_sp.reset();
462 }
463}
464
465ExecutionContext &
466ExecutionContext::operator =(const ExecutionContext &rhs)
467{
468 if (this != &rhs)
469 {
470 m_target_sp = rhs.m_target_sp;
471 m_process_sp = rhs.m_process_sp;
472 m_thread_sp = rhs.m_thread_sp;
473 m_frame_sp = rhs.m_frame_sp;
474 }
475 return *this;
476}
477
478bool
479ExecutionContext::operator ==(const ExecutionContext &rhs) const
480{
481 // Check that the frame shared pointers match, or both are valid and their stack
482 // IDs match since sometimes we get new objects that represent the same
483 // frame within a thread.
484 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
485 {
486 // Check that the thread shared pointers match, or both are valid and
487 // their thread IDs match since sometimes we get new objects that
488 // represent the same thread within a process.
489 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
490 {
491 // Processes and targets don't change much
492 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
493 }
494 }
495 return false;
496}
497
498bool
499ExecutionContext::operator !=(const ExecutionContext &rhs) const
500{
501 return !(*this == rhs);
502}
503
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000504bool
505ExecutionContext::HasTargetScope () const
506{
507 return ((bool) m_target_sp
508 && m_target_sp->IsValid());
509}
510
511bool
512ExecutionContext::HasProcessScope () const
513{
514 return (HasTargetScope()
515 && ((bool) m_process_sp && m_process_sp->IsValid()));
516}
517
518bool
519ExecutionContext::HasThreadScope () const
520{
521 return (HasProcessScope()
522 && ((bool) m_thread_sp && m_thread_sp->IsValid()));
523}
524
525bool
526ExecutionContext::HasFrameScope () const
527{
528 return HasThreadScope() && m_frame_sp;
529}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000530
531ExecutionContextRef::ExecutionContextRef() :
532 m_target_wp (),
533 m_process_wp (),
534 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000535 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000536 m_stack_id ()
537{
538}
539
540ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
541 m_target_wp (),
542 m_process_wp (),
543 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000544 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000545 m_stack_id ()
546{
547 if (exe_ctx)
548 *this = *exe_ctx;
549}
550
551ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
552 m_target_wp (),
553 m_process_wp (),
554 m_thread_wp (),
Greg Clayton6482d232013-03-15 23:54:07 +0000555 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000556 m_stack_id ()
557{
558 *this = exe_ctx;
559}
560
561
562ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
563 m_target_wp(),
564 m_process_wp(),
565 m_thread_wp(),
Greg Clayton6482d232013-03-15 23:54:07 +0000566 m_tid(LLDB_INVALID_THREAD_ID),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000567 m_stack_id ()
568{
569 SetTargetPtr (target, adopt_selected);
570}
571
572
573
574
575ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
576 m_target_wp (rhs.m_target_wp),
577 m_process_wp(rhs.m_process_wp),
578 m_thread_wp (rhs.m_thread_wp),
Greg Claytoncc4d0142012-02-17 07:49:44 +0000579 m_tid (rhs.m_tid),
580 m_stack_id (rhs.m_stack_id)
581{
582}
583
584ExecutionContextRef &
585ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
586{
587 if (this != &rhs)
588 {
589 m_target_wp = rhs.m_target_wp;
590 m_process_wp = rhs.m_process_wp;
591 m_thread_wp = rhs.m_thread_wp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000592 m_tid = rhs.m_tid;
593 m_stack_id = rhs.m_stack_id;
594 }
595 return *this;
596}
597
598ExecutionContextRef &
599ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
600{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000601 m_target_wp = exe_ctx.GetTargetSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000602 m_process_wp = exe_ctx.GetProcessSP();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000603 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
604 m_thread_wp = thread_sp;
605 if (thread_sp)
606 m_tid = thread_sp->GetID();
607 else
608 m_tid = LLDB_INVALID_THREAD_ID;
Jason Molendab57e4a12013-11-04 09:33:30 +0000609 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000610 if (frame_sp)
611 m_stack_id = frame_sp->GetStackID();
612 else
613 m_stack_id.Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000614 return *this;
615}
616
617void
618ExecutionContextRef::Clear()
619{
620 m_target_wp.reset();
621 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000622 ClearThread();
623 ClearFrame();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000624}
625
626ExecutionContextRef::~ExecutionContextRef()
627{
628}
629
630void
631ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
632{
633 m_target_wp = target_sp;
634}
635
636void
637ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
638{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000639 if (process_sp)
640 {
641 m_process_wp = process_sp;
642 SetTargetSP (process_sp->GetTarget().shared_from_this());
643 }
644 else
645 {
646 m_process_wp.reset();
647 m_target_wp.reset();
648 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000649}
650
651void
652ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
653{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000654 if (thread_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000655 {
656 m_thread_wp = thread_sp;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000657 m_tid = thread_sp->GetID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000658 SetProcessSP (thread_sp->GetProcess());
659 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000660 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000661 {
662 ClearThread();
663 m_process_wp.reset();
664 m_target_wp.reset();
665 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000666}
667
668void
Jason Molendab57e4a12013-11-04 09:33:30 +0000669ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000670{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000671 if (frame_sp)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000672 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000673 m_stack_id = frame_sp->GetStackID();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000674 SetThreadSP (frame_sp->GetThread());
675 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000676 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000677 {
678 ClearFrame();
679 ClearThread();
680 m_process_wp.reset();
681 m_target_wp.reset();
682 }
683
Greg Claytoncc4d0142012-02-17 07:49:44 +0000684}
685
686void
687ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
688{
689 Clear();
690 if (target)
691 {
692 lldb::TargetSP target_sp (target->shared_from_this());
693 if (target_sp)
694 {
695 m_target_wp = target_sp;
696 if (adopt_selected)
697 {
698 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
699 if (process_sp)
700 {
701 m_process_wp = process_sp;
702 if (process_sp)
703 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000704 // Only fill in the thread and frame if our process is stopped
705 if (StateIsStoppedState (process_sp->GetState(), true))
Greg Claytoncc4d0142012-02-17 07:49:44 +0000706 {
Greg Clayton9b5450f2012-07-30 22:05:39 +0000707 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
708 if (!thread_sp)
709 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
710
711 if (thread_sp)
712 {
713 SetThreadSP (thread_sp);
Jason Molendab57e4a12013-11-04 09:33:30 +0000714 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
Greg Clayton9b5450f2012-07-30 22:05:39 +0000715 if (!frame_sp)
716 frame_sp = thread_sp->GetStackFrameAtIndex(0);
717 if (frame_sp)
718 SetFrameSP (frame_sp);
719 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000720 }
721 }
722 }
723 }
724 }
725 }
726}
727
728void
729ExecutionContextRef::SetProcessPtr (Process *process)
730{
731 if (process)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000732 {
733 SetProcessSP(process->shared_from_this());
734 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000735 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000736 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000737 m_process_wp.reset();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000738 m_target_wp.reset();
739 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000740}
741
742void
743ExecutionContextRef::SetThreadPtr (Thread *thread)
744{
745 if (thread)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000746 {
747 SetThreadSP (thread->shared_from_this());
748 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000749 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000750 {
751 ClearThread();
752 m_process_wp.reset();
753 m_target_wp.reset();
754 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000755}
756
757void
Jason Molendab57e4a12013-11-04 09:33:30 +0000758ExecutionContextRef::SetFramePtr (StackFrame *frame)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000759{
760 if (frame)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000761 SetFrameSP (frame->shared_from_this());
Greg Claytoncc4d0142012-02-17 07:49:44 +0000762 else
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000763 Clear();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000764}
765
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000766lldb::TargetSP
767ExecutionContextRef::GetTargetSP () const
768{
769 lldb::TargetSP target_sp(m_target_wp.lock());
770 if (target_sp && !target_sp->IsValid())
771 target_sp.reset();
772 return target_sp;
773}
774
775lldb::ProcessSP
776ExecutionContextRef::GetProcessSP () const
777{
778 lldb::ProcessSP process_sp(m_process_wp.lock());
779 if (process_sp && !process_sp->IsValid())
780 process_sp.reset();
781 return process_sp;
782}
Greg Claytoncc4d0142012-02-17 07:49:44 +0000783
784lldb::ThreadSP
785ExecutionContextRef::GetThreadSP () const
786{
787 lldb::ThreadSP thread_sp (m_thread_wp.lock());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000788
Greg Clayton0b88d812012-04-04 20:43:47 +0000789 if (m_tid != LLDB_INVALID_THREAD_ID)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000790 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000791 // We check if the thread has been destroyed in cases where clients
792 // might still have shared pointer to a thread, but the thread is
793 // not valid anymore (not part of the process)
794 if (!thread_sp || !thread_sp->IsValid())
Greg Claytoncc4d0142012-02-17 07:49:44 +0000795 {
Greg Clayton0b88d812012-04-04 20:43:47 +0000796 lldb::ProcessSP process_sp(GetProcessSP());
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000797 if (process_sp && process_sp->IsValid())
Greg Clayton0b88d812012-04-04 20:43:47 +0000798 {
799 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
800 m_thread_wp = thread_sp;
801 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000802 }
803 }
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000804
805 // Check that we aren't about to return an invalid thread sp. We might return a NULL thread_sp,
806 // but don't return an invalid one.
807
808 if (thread_sp && !thread_sp->IsValid())
809 thread_sp.reset();
810
Greg Claytoncc4d0142012-02-17 07:49:44 +0000811 return thread_sp;
812}
813
Jason Molendab57e4a12013-11-04 09:33:30 +0000814lldb::StackFrameSP
Greg Claytoncc4d0142012-02-17 07:49:44 +0000815ExecutionContextRef::GetFrameSP () const
816{
Greg Clayton7bcb93d2013-05-24 00:58:29 +0000817 if (m_stack_id.IsValid())
818 {
819 lldb::ThreadSP thread_sp (GetThreadSP());
820 if (thread_sp)
821 return thread_sp->GetFrameWithStackID (m_stack_id);
822 }
Jason Molendab57e4a12013-11-04 09:33:30 +0000823 return lldb::StackFrameSP();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000824}
825
826ExecutionContext
827ExecutionContextRef::Lock () const
828{
829 return ExecutionContext(this);
830}
831
832