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     target (NULL),
21     process (NULL),
22     thread (NULL),
23     frame (NULL)
24 {
25 }
26 
27 ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
28     target (t),
29     process (NULL),
30     thread (NULL),
31     frame (NULL)
32 {
33     if (t && fill_current_process_thread_frame)
34     {
35         process = t->GetProcessSP().get();
36         if (process)
37         {
38             thread = process->GetThreadList().GetSelectedThread().get();
39             if (thread)
40                 frame = thread->GetSelectedFrame().get();
41         }
42     }
43 }
44 
45 ExecutionContext::ExecutionContext(Process* p, Thread *t, StackFrame *f) :
46     target (p ? &p->GetTarget() : NULL),
47     process (p),
48     thread (t),
49     frame (f)
50 {
51 }
52 
53 ExecutionContext::ExecutionContext (ExecutionContextScope *exe_scope_ptr)
54 {
55     if (exe_scope_ptr)
56         exe_scope_ptr->CalculateExecutionContext (*this);
57     else
58     {
59         target  = NULL;
60         process = NULL;
61         thread  = NULL;
62         frame   = NULL;
63     }
64 }
65 
66 ExecutionContext::ExecutionContext (ExecutionContextScope &exe_scope_ref)
67 {
68     exe_scope_ref.CalculateExecutionContext (*this);
69 }
70 
71 void
72 ExecutionContext::Clear()
73 {
74     target  = NULL;
75     process = NULL;
76     thread  = NULL;
77     frame   = NULL;
78 }
79 
80 
81 RegisterContext *
82 ExecutionContext::GetRegisterContext () const
83 {
84     if (frame)
85         return frame->GetRegisterContext().get();
86     else if (thread)
87         return thread->GetRegisterContext().get();
88     return NULL;
89 }
90 
91 ExecutionContextScope *
92 ExecutionContext::GetBestExecutionContextScope () const
93 {
94     if (frame)
95         return frame;
96     if (thread)
97         return thread;
98     if (process)
99         return process;
100     return target;
101 }
102