1 //===-- SBExecutionContext.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBExecutionContext.h"
10 #include "SBReproducerPrivate.h"
11 
12 #include "lldb/API/SBFrame.h"
13 #include "lldb/API/SBProcess.h"
14 #include "lldb/API/SBTarget.h"
15 #include "lldb/API/SBThread.h"
16 
17 #include "lldb/Target/ExecutionContext.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
SBExecutionContext()22 SBExecutionContext::SBExecutionContext() : m_exe_ctx_sp() {
23   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBExecutionContext);
24 }
25 
SBExecutionContext(const lldb::SBExecutionContext & rhs)26 SBExecutionContext::SBExecutionContext(const lldb::SBExecutionContext &rhs)
27     : m_exe_ctx_sp(rhs.m_exe_ctx_sp) {
28   LLDB_RECORD_CONSTRUCTOR(SBExecutionContext,
29                           (const lldb::SBExecutionContext &), rhs);
30 }
31 
SBExecutionContext(lldb::ExecutionContextRefSP exe_ctx_ref_sp)32 SBExecutionContext::SBExecutionContext(
33     lldb::ExecutionContextRefSP exe_ctx_ref_sp)
34     : m_exe_ctx_sp(exe_ctx_ref_sp) {
35   LLDB_RECORD_CONSTRUCTOR(SBExecutionContext, (lldb::ExecutionContextRefSP),
36                           exe_ctx_ref_sp);
37 }
38 
SBExecutionContext(const lldb::SBTarget & target)39 SBExecutionContext::SBExecutionContext(const lldb::SBTarget &target)
40     : m_exe_ctx_sp(new ExecutionContextRef()) {
41   LLDB_RECORD_CONSTRUCTOR(SBExecutionContext, (const lldb::SBTarget &), target);
42 
43   m_exe_ctx_sp->SetTargetSP(target.GetSP());
44 }
45 
SBExecutionContext(const lldb::SBProcess & process)46 SBExecutionContext::SBExecutionContext(const lldb::SBProcess &process)
47     : m_exe_ctx_sp(new ExecutionContextRef()) {
48   LLDB_RECORD_CONSTRUCTOR(SBExecutionContext, (const lldb::SBProcess &),
49                           process);
50 
51   m_exe_ctx_sp->SetProcessSP(process.GetSP());
52 }
53 
SBExecutionContext(lldb::SBThread thread)54 SBExecutionContext::SBExecutionContext(lldb::SBThread thread)
55     : m_exe_ctx_sp(new ExecutionContextRef()) {
56   LLDB_RECORD_CONSTRUCTOR(SBExecutionContext, (lldb::SBThread), thread);
57 
58   m_exe_ctx_sp->SetThreadPtr(thread.get());
59 }
60 
SBExecutionContext(const lldb::SBFrame & frame)61 SBExecutionContext::SBExecutionContext(const lldb::SBFrame &frame)
62     : m_exe_ctx_sp(new ExecutionContextRef()) {
63   LLDB_RECORD_CONSTRUCTOR(SBExecutionContext, (const lldb::SBFrame &), frame);
64 
65   m_exe_ctx_sp->SetFrameSP(frame.GetFrameSP());
66 }
67 
68 SBExecutionContext::~SBExecutionContext() = default;
69 
70 const SBExecutionContext &SBExecutionContext::
operator =(const lldb::SBExecutionContext & rhs)71 operator=(const lldb::SBExecutionContext &rhs) {
72   LLDB_RECORD_METHOD(
73       const lldb::SBExecutionContext &,
74       SBExecutionContext, operator=,(const lldb::SBExecutionContext &), rhs);
75 
76   m_exe_ctx_sp = rhs.m_exe_ctx_sp;
77   return LLDB_RECORD_RESULT(*this);
78 }
79 
get() const80 ExecutionContextRef *SBExecutionContext::get() const {
81   return m_exe_ctx_sp.get();
82 }
83 
GetTarget() const84 SBTarget SBExecutionContext::GetTarget() const {
85   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBTarget, SBExecutionContext,
86                                    GetTarget);
87 
88   SBTarget sb_target;
89   if (m_exe_ctx_sp) {
90     TargetSP target_sp(m_exe_ctx_sp->GetTargetSP());
91     if (target_sp)
92       sb_target.SetSP(target_sp);
93   }
94   return LLDB_RECORD_RESULT(sb_target);
95 }
96 
GetProcess() const97 SBProcess SBExecutionContext::GetProcess() const {
98   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBProcess, SBExecutionContext,
99                                    GetProcess);
100 
101   SBProcess sb_process;
102   if (m_exe_ctx_sp) {
103     ProcessSP process_sp(m_exe_ctx_sp->GetProcessSP());
104     if (process_sp)
105       sb_process.SetSP(process_sp);
106   }
107   return LLDB_RECORD_RESULT(sb_process);
108 }
109 
GetThread() const110 SBThread SBExecutionContext::GetThread() const {
111   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBThread, SBExecutionContext,
112                                    GetThread);
113 
114   SBThread sb_thread;
115   if (m_exe_ctx_sp) {
116     ThreadSP thread_sp(m_exe_ctx_sp->GetThreadSP());
117     if (thread_sp)
118       sb_thread.SetThread(thread_sp);
119   }
120   return LLDB_RECORD_RESULT(sb_thread);
121 }
122 
GetFrame() const123 SBFrame SBExecutionContext::GetFrame() const {
124   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFrame, SBExecutionContext, GetFrame);
125 
126   SBFrame sb_frame;
127   if (m_exe_ctx_sp) {
128     StackFrameSP frame_sp(m_exe_ctx_sp->GetFrameSP());
129     if (frame_sp)
130       sb_frame.SetFrameSP(frame_sp);
131   }
132   return LLDB_RECORD_RESULT(sb_frame);
133 }
134 
135 namespace lldb_private {
136 namespace repro {
137 
138 template <>
RegisterMethods(Registry & R)139 void RegisterMethods<SBExecutionContext>(Registry &R) {
140   LLDB_REGISTER_CONSTRUCTOR(SBExecutionContext, ());
141   LLDB_REGISTER_CONSTRUCTOR(SBExecutionContext,
142                             (const lldb::SBExecutionContext &));
143   LLDB_REGISTER_CONSTRUCTOR(SBExecutionContext,
144                             (lldb::ExecutionContextRefSP));
145   LLDB_REGISTER_CONSTRUCTOR(SBExecutionContext, (const lldb::SBTarget &));
146   LLDB_REGISTER_CONSTRUCTOR(SBExecutionContext, (const lldb::SBProcess &));
147   LLDB_REGISTER_CONSTRUCTOR(SBExecutionContext, (lldb::SBThread));
148   LLDB_REGISTER_CONSTRUCTOR(SBExecutionContext, (const lldb::SBFrame &));
149   LLDB_REGISTER_METHOD(
150       const lldb::SBExecutionContext &,
151       SBExecutionContext, operator=,(const lldb::SBExecutionContext &));
152   LLDB_REGISTER_METHOD_CONST(lldb::SBTarget, SBExecutionContext, GetTarget,
153                              ());
154   LLDB_REGISTER_METHOD_CONST(lldb::SBProcess, SBExecutionContext, GetProcess,
155                              ());
156   LLDB_REGISTER_METHOD_CONST(lldb::SBThread, SBExecutionContext, GetThread,
157                              ());
158   LLDB_REGISTER_METHOD_CONST(lldb::SBFrame, SBExecutionContext, GetFrame, ());
159 }
160 
161 }
162 }
163