blob: 9bddb6190cfd4c903e41e6ecf516b9b78486b69b [file] [log] [blame]
Chris Lattner24943d22010-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 Lattner24943d22010-06-08 16:52:24 +00009
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
17using namespace lldb_private;
18
19ExecutionContext::ExecutionContext() :
Greg Clayton567e7f32011-09-22 04:58:26 +000020 m_target_sp (),
21 m_process_sp (),
22 m_thread_sp (),
23 m_frame_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000024{
25}
26
Greg Clayton567e7f32011-09-22 04:58:26 +000027ExecutionContext::ExecutionContext (const ExecutionContext &rhs) :
Greg Claytonb4d7fc02012-02-17 07:49:44 +000028 m_target_sp(rhs.m_target_sp),
Greg Clayton567e7f32011-09-22 04:58:26 +000029 m_process_sp(rhs.m_process_sp),
Greg Claytonb4d7fc02012-02-17 07:49:44 +000030 m_thread_sp(rhs.m_thread_sp),
31 m_frame_sp(rhs.m_frame_sp)
Greg Clayton567e7f32011-09-22 04:58:26 +000032{
33}
34
Greg Claytonb4d7fc02012-02-17 07:49:44 +000035ExecutionContext::ExecutionContext (const lldb::TargetSP &target_sp, bool get_process) :
36 m_target_sp (),
37 m_process_sp (),
38 m_thread_sp (),
39 m_frame_sp ()
Greg Clayton567e7f32011-09-22 04:58:26 +000040{
Greg Claytonb4d7fc02012-02-17 07:49:44 +000041 if (target_sp)
42 SetContext (target_sp, get_process);
43}
44
45ExecutionContext::ExecutionContext (const lldb::ProcessSP &process_sp) :
46 m_target_sp (),
47 m_process_sp (),
48 m_thread_sp (),
49 m_frame_sp ()
50{
51 if (process_sp)
52 SetContext (process_sp);
53}
54
55ExecutionContext::ExecutionContext (const lldb::ThreadSP &thread_sp) :
56 m_target_sp (),
57 m_process_sp (),
58 m_thread_sp (),
59 m_frame_sp ()
60{
61 if (thread_sp)
62 SetContext (thread_sp);
63}
64
65ExecutionContext::ExecutionContext (const lldb::StackFrameSP &frame_sp) :
66 m_target_sp (),
67 m_process_sp (),
68 m_thread_sp (),
69 m_frame_sp ()
70{
71 if (frame_sp)
72 SetContext (frame_sp);
Greg Clayton567e7f32011-09-22 04:58:26 +000073}
74
Greg Claytonf4124de2012-02-21 00:09:25 +000075ExecutionContext::ExecutionContext (const lldb::TargetWP &target_wp, bool get_process) :
76 m_target_sp (),
77 m_process_sp (),
78 m_thread_sp (),
79 m_frame_sp ()
80{
81 lldb::TargetSP target_sp(target_wp.lock());
82 if (target_sp)
83 SetContext (target_sp, get_process);
84}
85
86ExecutionContext::ExecutionContext (const lldb::ProcessWP &process_wp) :
87 m_target_sp (),
88 m_process_sp (),
89 m_thread_sp (),
90 m_frame_sp ()
91{
92 lldb::ProcessSP process_sp(process_wp.lock());
93 if (process_sp)
94 SetContext (process_sp);
95}
96
97ExecutionContext::ExecutionContext (const lldb::ThreadWP &thread_wp) :
98 m_target_sp (),
99 m_process_sp (),
100 m_thread_sp (),
101 m_frame_sp ()
102{
103 lldb::ThreadSP thread_sp(thread_wp.lock());
104 if (thread_sp)
105 SetContext (thread_sp);
106}
107
108ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
109 m_target_sp (),
110 m_process_sp (),
111 m_thread_sp (),
112 m_frame_sp ()
113{
114 lldb::StackFrameSP frame_sp(frame_wp.lock());
115 if (frame_sp)
116 SetContext (frame_sp);
117}
118
Chris Lattner24943d22010-06-08 16:52:24 +0000119ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
Greg Clayton13d24fb2012-01-29 20:56:30 +0000120 m_target_sp (t->shared_from_this()),
Greg Clayton567e7f32011-09-22 04:58:26 +0000121 m_process_sp (),
122 m_thread_sp (),
123 m_frame_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +0000124{
125 if (t && fill_current_process_thread_frame)
126 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000127 m_process_sp = t->GetProcessSP();
128 if (m_process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000129 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000130 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
131 if (m_thread_sp)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000132 m_frame_sp = m_thread_sp->GetSelectedFrame();
Chris Lattner24943d22010-06-08 16:52:24 +0000133 }
134 }
135}
136
Greg Clayton567e7f32011-09-22 04:58:26 +0000137ExecutionContext::ExecutionContext(Process* process, Thread *thread, StackFrame *frame) :
Greg Clayton13d24fb2012-01-29 20:56:30 +0000138 m_target_sp (),
139 m_process_sp (process->shared_from_this()),
140 m_thread_sp (thread->shared_from_this()),
141 m_frame_sp (frame->shared_from_this())
Chris Lattner24943d22010-06-08 16:52:24 +0000142{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000143 if (process)
144 m_target_sp = process->GetTarget().shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000145}
146
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000147ExecutionContext::ExecutionContext (const ExecutionContextRef &exe_ctx_ref) :
148 m_target_sp (exe_ctx_ref.GetTargetSP()),
149 m_process_sp (exe_ctx_ref.GetProcessSP()),
150 m_thread_sp (exe_ctx_ref.GetThreadSP()),
151 m_frame_sp (exe_ctx_ref.GetFrameSP())
152{
153}
154
155ExecutionContext::ExecutionContext (const ExecutionContextRef *exe_ctx_ref_ptr) :
156 m_target_sp (),
157 m_process_sp (),
158 m_thread_sp (),
159 m_frame_sp ()
160{
161 if (exe_ctx_ref_ptr)
162 {
163 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
164 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
165 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
166 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
167 }
168}
169
170ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr) :
171 m_target_sp (),
172 m_process_sp (),
173 m_thread_sp (),
174 m_frame_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +0000175{
176 if (exe_scope_ptr)
Greg Claytona830adb2010-10-04 01:05:56 +0000177 exe_scope_ptr->CalculateExecutionContext (*this);
Chris Lattner24943d22010-06-08 16:52:24 +0000178}
179
180ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
181{
Greg Claytona830adb2010-10-04 01:05:56 +0000182 exe_scope_ref.CalculateExecutionContext (*this);
Chris Lattner24943d22010-06-08 16:52:24 +0000183}
184
185void
186ExecutionContext::Clear()
187{
Greg Clayton567e7f32011-09-22 04:58:26 +0000188 m_target_sp.reset();
189 m_process_sp.reset();
190 m_thread_sp.reset();
191 m_frame_sp.reset();
192}
193
194ExecutionContext::~ExecutionContext()
195{
Chris Lattner24943d22010-06-08 16:52:24 +0000196}
197
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000198uint32_t
199ExecutionContext::GetAddressByteSize() const
200{
201 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
202 m_target_sp->GetArchitecture().GetAddressByteSize();
203 if (m_process_sp)
204 m_process_sp->GetAddressByteSize();
205 return sizeof(void *);
206}
207
208
Chris Lattner24943d22010-06-08 16:52:24 +0000209
210RegisterContext *
211ExecutionContext::GetRegisterContext () const
212{
Greg Clayton567e7f32011-09-22 04:58:26 +0000213 if (m_frame_sp)
214 return m_frame_sp->GetRegisterContext().get();
215 else if (m_thread_sp)
216 return m_thread_sp->GetRegisterContext().get();
217 return NULL;
218}
219
220Target *
221ExecutionContext::GetTargetPtr () const
222{
223 if (m_target_sp)
224 return m_target_sp.get();
225 if (m_process_sp)
226 return &m_process_sp->GetTarget();
227 return NULL;
228}
229
230Process *
231ExecutionContext::GetProcessPtr () const
232{
233 if (m_process_sp)
234 return m_process_sp.get();
235 if (m_target_sp)
236 return m_target_sp->GetProcessSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000237 return NULL;
238}
239
240ExecutionContextScope *
241ExecutionContext::GetBestExecutionContextScope () const
242{
Greg Clayton567e7f32011-09-22 04:58:26 +0000243 if (m_frame_sp)
244 return m_frame_sp.get();
245 if (m_thread_sp)
246 return m_thread_sp.get();
247 if (m_process_sp)
248 return m_process_sp.get();
249 return m_target_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000250}
Greg Clayton801417e2011-07-07 01:59:51 +0000251
Greg Clayton567e7f32011-09-22 04:58:26 +0000252Target &
253ExecutionContext::GetTargetRef () const
Greg Clayton801417e2011-07-07 01:59:51 +0000254{
Greg Claytonf4124de2012-02-21 00:09:25 +0000255#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Clayton567e7f32011-09-22 04:58:26 +0000256 assert (m_target_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000257#endif
Greg Clayton567e7f32011-09-22 04:58:26 +0000258 return *m_target_sp;
Greg Clayton801417e2011-07-07 01:59:51 +0000259}
Greg Clayton567e7f32011-09-22 04:58:26 +0000260
261Process &
262ExecutionContext::GetProcessRef () const
263{
Greg Claytonf4124de2012-02-21 00:09:25 +0000264#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Clayton567e7f32011-09-22 04:58:26 +0000265 assert (m_process_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000266#endif
Greg Clayton567e7f32011-09-22 04:58:26 +0000267 return *m_process_sp;
268}
269
270Thread &
271ExecutionContext::GetThreadRef () const
272{
Greg Claytonf4124de2012-02-21 00:09:25 +0000273#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Clayton567e7f32011-09-22 04:58:26 +0000274 assert (m_thread_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000275#endif
Greg Clayton567e7f32011-09-22 04:58:26 +0000276 return *m_thread_sp;
277}
278
279StackFrame &
280ExecutionContext::GetFrameRef () const
281{
Greg Claytonf4124de2012-02-21 00:09:25 +0000282#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
Greg Clayton567e7f32011-09-22 04:58:26 +0000283 assert (m_frame_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000284#endif
Greg Clayton567e7f32011-09-22 04:58:26 +0000285 return *m_frame_sp;
286}
287
288void
289ExecutionContext::SetTargetSP (const lldb::TargetSP &target_sp)
290{
291 m_target_sp = target_sp;
292}
293
294void
295ExecutionContext::SetProcessSP (const lldb::ProcessSP &process_sp)
296{
297 m_process_sp = process_sp;
298}
299
300void
301ExecutionContext::SetThreadSP (const lldb::ThreadSP &thread_sp)
302{
303 m_thread_sp = thread_sp;
304}
305
306void
307ExecutionContext::SetFrameSP (const lldb::StackFrameSP &frame_sp)
308{
309 m_frame_sp = frame_sp;
310}
311
312void
313ExecutionContext::SetTargetPtr (Target* target)
314{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000315 if (target)
316 m_target_sp = target->shared_from_this();
317 else
318 m_target_sp.reset();
Greg Clayton567e7f32011-09-22 04:58:26 +0000319}
320
321void
322ExecutionContext::SetProcessPtr (Process *process)
323{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000324 if (process)
325 m_process_sp = process->shared_from_this();
326 else
327 m_process_sp.reset();
Greg Clayton567e7f32011-09-22 04:58:26 +0000328}
329
330void
331ExecutionContext::SetThreadPtr (Thread *thread)
332{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000333 if (thread)
334 m_thread_sp = thread->shared_from_this();
335 else
336 m_thread_sp.reset();
Greg Clayton567e7f32011-09-22 04:58:26 +0000337}
338
339void
340ExecutionContext::SetFramePtr (StackFrame *frame)
341{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000342 if (frame)
343 m_frame_sp = frame->shared_from_this();
344 else
345 m_frame_sp.reset();
Greg Clayton567e7f32011-09-22 04:58:26 +0000346}
347
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000348void
349ExecutionContext::SetContext (const lldb::TargetSP &target_sp, bool get_process)
350{
351 m_target_sp = target_sp;
352 if (get_process && target_sp)
353 m_process_sp = target_sp->GetProcessSP();
354 else
355 m_process_sp.reset();
356 m_thread_sp.reset();
357 m_frame_sp.reset();
358}
359
360void
361ExecutionContext::SetContext (const lldb::ProcessSP &process_sp)
362{
363 m_process_sp = process_sp;
364 if (process_sp)
365 m_target_sp = process_sp->GetTarget().shared_from_this();
366 else
367 m_target_sp.reset();
368 m_thread_sp.reset();
369 m_frame_sp.reset();
370}
371
372void
373ExecutionContext::SetContext (const lldb::ThreadSP &thread_sp)
374{
375 m_frame_sp.reset();
376 m_thread_sp = thread_sp;
377 if (thread_sp)
378 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000379 m_process_sp = thread_sp->GetProcess();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000380 if (m_process_sp)
381 m_target_sp = m_process_sp->GetTarget().shared_from_this();
382 else
383 m_target_sp.reset();
384 }
385 else
386 {
387 m_target_sp.reset();
388 m_process_sp.reset();
389 }
390}
391
392void
393ExecutionContext::SetContext (const lldb::StackFrameSP &frame_sp)
394{
395 m_frame_sp = frame_sp;
396 if (frame_sp)
397 {
Greg Clayton289afcb2012-02-18 05:35:26 +0000398 m_thread_sp = frame_sp->CalculateThread();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000399 if (m_thread_sp)
400 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000401 m_process_sp = m_thread_sp->GetProcess();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000402 if (m_process_sp)
403 m_target_sp = m_process_sp->GetTarget().shared_from_this();
404 else
405 m_target_sp.reset();
406 }
407 else
408 {
409 m_target_sp.reset();
410 m_process_sp.reset();
411 }
412 }
413 else
414 {
415 m_target_sp.reset();
416 m_process_sp.reset();
417 m_thread_sp.reset();
418 }
419}
420
421ExecutionContext &
422ExecutionContext::operator =(const ExecutionContext &rhs)
423{
424 if (this != &rhs)
425 {
426 m_target_sp = rhs.m_target_sp;
427 m_process_sp = rhs.m_process_sp;
428 m_thread_sp = rhs.m_thread_sp;
429 m_frame_sp = rhs.m_frame_sp;
430 }
431 return *this;
432}
433
434bool
435ExecutionContext::operator ==(const ExecutionContext &rhs) const
436{
437 // Check that the frame shared pointers match, or both are valid and their stack
438 // IDs match since sometimes we get new objects that represent the same
439 // frame within a thread.
440 if ((m_frame_sp == rhs.m_frame_sp) || (m_frame_sp && rhs.m_frame_sp && m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID()))
441 {
442 // Check that the thread shared pointers match, or both are valid and
443 // their thread IDs match since sometimes we get new objects that
444 // represent the same thread within a process.
445 if ((m_thread_sp == rhs.m_thread_sp) || (m_thread_sp && rhs.m_thread_sp && m_thread_sp->GetID() == rhs.m_thread_sp->GetID()))
446 {
447 // Processes and targets don't change much
448 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
449 }
450 }
451 return false;
452}
453
454bool
455ExecutionContext::operator !=(const ExecutionContext &rhs) const
456{
457 return !(*this == rhs);
458}
459
460
461ExecutionContextRef::ExecutionContextRef() :
462 m_target_wp (),
463 m_process_wp (),
464 m_thread_wp (),
465 m_frame_wp (),
466 m_tid(LLDB_INVALID_THREAD_ID),
467 m_stack_id ()
468{
469}
470
471ExecutionContextRef::ExecutionContextRef (const ExecutionContext *exe_ctx) :
472 m_target_wp (),
473 m_process_wp (),
474 m_thread_wp (),
475 m_frame_wp (),
476 m_tid(LLDB_INVALID_THREAD_ID),
477 m_stack_id ()
478{
479 if (exe_ctx)
480 *this = *exe_ctx;
481}
482
483ExecutionContextRef::ExecutionContextRef (const ExecutionContext &exe_ctx) :
484 m_target_wp (),
485 m_process_wp (),
486 m_thread_wp (),
487 m_frame_wp (),
488 m_tid(LLDB_INVALID_THREAD_ID),
489 m_stack_id ()
490{
491 *this = exe_ctx;
492}
493
494
495ExecutionContextRef::ExecutionContextRef (Target *target, bool adopt_selected) :
496 m_target_wp(),
497 m_process_wp(),
498 m_thread_wp(),
499 m_frame_wp(),
500 m_tid(LLDB_INVALID_THREAD_ID),
501 m_stack_id ()
502{
503 SetTargetPtr (target, adopt_selected);
504}
505
506
507
508
509ExecutionContextRef::ExecutionContextRef (const ExecutionContextRef &rhs) :
510 m_target_wp (rhs.m_target_wp),
511 m_process_wp(rhs.m_process_wp),
512 m_thread_wp (rhs.m_thread_wp),
513 m_frame_wp (rhs.m_frame_wp),
514 m_tid (rhs.m_tid),
515 m_stack_id (rhs.m_stack_id)
516{
517}
518
519ExecutionContextRef &
520ExecutionContextRef::operator =(const ExecutionContextRef &rhs)
521{
522 if (this != &rhs)
523 {
524 m_target_wp = rhs.m_target_wp;
525 m_process_wp = rhs.m_process_wp;
526 m_thread_wp = rhs.m_thread_wp;
527 m_frame_wp = rhs.m_frame_wp;
528 m_tid = rhs.m_tid;
529 m_stack_id = rhs.m_stack_id;
530 }
531 return *this;
532}
533
534ExecutionContextRef &
535ExecutionContextRef::operator =(const ExecutionContext &exe_ctx)
536{
Greg Claytona894fe72012-04-05 16:12:35 +0000537 m_target_wp = exe_ctx.GetTargetSP();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000538 m_process_wp = exe_ctx.GetProcessSP();
Greg Claytona894fe72012-04-05 16:12:35 +0000539 lldb::ThreadSP thread_sp (exe_ctx.GetThreadSP());
540 m_thread_wp = thread_sp;
541 if (thread_sp)
542 m_tid = thread_sp->GetID();
543 else
544 m_tid = LLDB_INVALID_THREAD_ID;
545 lldb::StackFrameSP frame_sp (exe_ctx.GetFrameSP());
546 m_frame_wp = frame_sp;
547 if (frame_sp)
548 m_stack_id = frame_sp->GetStackID();
549 else
550 m_stack_id.Clear();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000551 return *this;
552}
553
554void
555ExecutionContextRef::Clear()
556{
557 m_target_wp.reset();
558 m_process_wp.reset();
Greg Claytona894fe72012-04-05 16:12:35 +0000559 ClearThread();
560 ClearFrame();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000561}
562
563ExecutionContextRef::~ExecutionContextRef()
564{
565}
566
567void
568ExecutionContextRef::SetTargetSP (const lldb::TargetSP &target_sp)
569{
570 m_target_wp = target_sp;
571}
572
573void
574ExecutionContextRef::SetProcessSP (const lldb::ProcessSP &process_sp)
575{
Greg Claytona894fe72012-04-05 16:12:35 +0000576 if (process_sp)
577 {
578 m_process_wp = process_sp;
579 SetTargetSP (process_sp->GetTarget().shared_from_this());
580 }
581 else
582 {
583 m_process_wp.reset();
584 m_target_wp.reset();
585 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000586}
587
588void
589ExecutionContextRef::SetThreadSP (const lldb::ThreadSP &thread_sp)
590{
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000591 if (thread_sp)
Greg Claytona894fe72012-04-05 16:12:35 +0000592 {
593 m_thread_wp = thread_sp;
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000594 m_tid = thread_sp->GetID();
Greg Claytona894fe72012-04-05 16:12:35 +0000595 SetProcessSP (thread_sp->GetProcess());
596 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000597 else
Greg Claytona894fe72012-04-05 16:12:35 +0000598 {
599 ClearThread();
600 m_process_wp.reset();
601 m_target_wp.reset();
602 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000603}
604
605void
606ExecutionContextRef::SetFrameSP (const lldb::StackFrameSP &frame_sp)
607{
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000608 if (frame_sp)
Greg Claytona894fe72012-04-05 16:12:35 +0000609 {
610 m_frame_wp = frame_sp;
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000611 m_stack_id = frame_sp->GetStackID();
Greg Claytona894fe72012-04-05 16:12:35 +0000612 SetThreadSP (frame_sp->GetThread());
613 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000614 else
Greg Claytona894fe72012-04-05 16:12:35 +0000615 {
616 ClearFrame();
617 ClearThread();
618 m_process_wp.reset();
619 m_target_wp.reset();
620 }
621
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000622}
623
624void
625ExecutionContextRef::SetTargetPtr (Target* target, bool adopt_selected)
626{
627 Clear();
628 if (target)
629 {
630 lldb::TargetSP target_sp (target->shared_from_this());
631 if (target_sp)
632 {
633 m_target_wp = target_sp;
634 if (adopt_selected)
635 {
636 lldb::ProcessSP process_sp (target_sp->GetProcessSP());
637 if (process_sp)
638 {
639 m_process_wp = process_sp;
640 if (process_sp)
641 {
642 lldb::ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
643 if (!thread_sp)
644 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
645
646 if (thread_sp)
647 {
648 SetThreadSP (thread_sp);
649 lldb::StackFrameSP frame_sp (thread_sp->GetSelectedFrame());
650 if (!frame_sp)
651 frame_sp = thread_sp->GetStackFrameAtIndex(0);
652 if (frame_sp)
653 SetFrameSP (frame_sp);
654 }
655 }
656 }
657 }
658 }
659 }
660}
661
662void
663ExecutionContextRef::SetProcessPtr (Process *process)
664{
665 if (process)
Greg Claytona894fe72012-04-05 16:12:35 +0000666 {
667 SetProcessSP(process->shared_from_this());
668 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000669 else
Greg Claytona894fe72012-04-05 16:12:35 +0000670 {
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000671 m_process_wp.reset();
Greg Claytona894fe72012-04-05 16:12:35 +0000672 m_target_wp.reset();
673 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000674}
675
676void
677ExecutionContextRef::SetThreadPtr (Thread *thread)
678{
679 if (thread)
Greg Claytona894fe72012-04-05 16:12:35 +0000680 {
681 SetThreadSP (thread->shared_from_this());
682 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000683 else
Greg Claytona894fe72012-04-05 16:12:35 +0000684 {
685 ClearThread();
686 m_process_wp.reset();
687 m_target_wp.reset();
688 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000689}
690
691void
692ExecutionContextRef::SetFramePtr (StackFrame *frame)
693{
694 if (frame)
Greg Claytona894fe72012-04-05 16:12:35 +0000695 SetFrameSP (frame->shared_from_this());
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000696 else
Greg Claytona894fe72012-04-05 16:12:35 +0000697 Clear();
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000698}
699
700
701lldb::ThreadSP
702ExecutionContextRef::GetThreadSP () const
703{
704 lldb::ThreadSP thread_sp (m_thread_wp.lock());
Greg Clayton3feafab2012-04-04 20:43:47 +0000705 if (m_tid != LLDB_INVALID_THREAD_ID)
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000706 {
Greg Clayton3feafab2012-04-04 20:43:47 +0000707 // We check if the thread has been destroyed in cases where clients
708 // might still have shared pointer to a thread, but the thread is
709 // not valid anymore (not part of the process)
710 if (!thread_sp || !thread_sp->IsValid())
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000711 {
Greg Clayton3feafab2012-04-04 20:43:47 +0000712 lldb::ProcessSP process_sp(GetProcessSP());
713 if (process_sp)
714 {
715 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
716 m_thread_wp = thread_sp;
717 }
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000718 }
719 }
720 return thread_sp;
721}
722
723lldb::StackFrameSP
724ExecutionContextRef::GetFrameSP () const
725{
726 lldb::StackFrameSP frame_sp (m_frame_wp.lock());
727 if (!frame_sp && m_stack_id.IsValid())
728 {
729 lldb::ThreadSP thread_sp (GetThreadSP());
730 if (thread_sp)
731 {
732 frame_sp = thread_sp->GetFrameWithStackID (m_stack_id);
733 m_frame_wp = frame_sp;
734 }
735 }
736 return frame_sp;
737}
738
739ExecutionContext
740ExecutionContextRef::Lock () const
741{
742 return ExecutionContext(this);
743}
744
745