xref: /llvm-project/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp (revision 1cc0ba4cbdc54200e1b3c65e83e51a5368a819ea)
1 //===-- PythonTestSuite.cpp -------------------------------------*- C++ -*-===//
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 "gtest/gtest.h"
10 
11 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
12 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h"
13 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Host/HostInfo.h"
16 
17 #include "PythonTestSuite.h"
18 
19 using namespace lldb_private;
20 class TestScriptInterpreterPython : public ScriptInterpreterPythonImpl {
21 public:
22   using ScriptInterpreterPythonImpl::Initialize;
23   using ScriptInterpreterPythonImpl::InitializePrivate;
24 };
25 
26 void PythonTestSuite::SetUp() {
27   FileSystem::Initialize();
28   HostInfoBase::Initialize();
29   // ScriptInterpreterPython::Initialize() depends on HostInfo being
30   // initializedso it can compute the python directory etc.
31   TestScriptInterpreterPython::Initialize();
32   TestScriptInterpreterPython::InitializePrivate();
33 
34   // Although we don't care about concurrency for the purposes of running
35   // this test suite, Python requires the GIL to be locked even for
36   // deallocating memory, which can happen when you call Py_DECREF or
37   // Py_INCREF.  So acquire the GIL for the entire duration of this
38   // test suite.
39   m_gil_state = PyGILState_Ensure();
40 }
41 
42 void PythonTestSuite::TearDown() {
43   PyGILState_Release(m_gil_state);
44 
45   TestScriptInterpreterPython::Terminate();
46   HostInfoBase::Terminate();
47   FileSystem::Terminate();
48 }
49 
50 // The following functions are the Pythonic implementations of the required
51 // callbacks. Because they're defined in libLLDB which we cannot link for the
52 // unit test, we have a 'default' implementation here.
53 
54 #if PY_MAJOR_VERSION >= 3
55 extern "C" PyObject *PyInit__lldb(void) { return nullptr; }
56 #define LLDBSwigPyInit PyInit__lldb
57 #else
58 extern "C" void init_lldb(void) {}
59 #define LLDBSwigPyInit init_lldb
60 #endif
61 
62 #pragma clang diagnostic push
63 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
64 
65 // Disable warning C4190: 'LLDBSwigPythonBreakpointCallbackFunction' has
66 // C-linkage specified, but returns UDT 'llvm::Expected<bool>' which is
67 // incompatible with C
68 #if _MSC_VER
69 #pragma warning (push)
70 #pragma warning (disable : 4190)
71 #endif
72 
73 extern "C" llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
74     const char *python_function_name, const char *session_dictionary_name,
75     const lldb::StackFrameSP &sb_frame,
76     const lldb::BreakpointLocationSP &sb_bp_loc,
77     StructuredDataImpl *args_impl) {
78   return false;
79 }
80 
81 #if _MSC_VER
82 #pragma warning (pop)
83 #endif
84 
85 #pragma clang diagnostic pop
86 
87 extern "C" bool LLDBSwigPythonWatchpointCallbackFunction(
88     const char *python_function_name, const char *session_dictionary_name,
89     const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp) {
90   return false;
91 }
92 
93 extern "C" bool LLDBSwigPythonCallTypeScript(
94     const char *python_function_name, void *session_dictionary,
95     const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
96     const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
97   return false;
98 }
99 
100 extern "C" void *
101 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
102                                       const char *session_dictionary_name,
103                                       const lldb::ValueObjectSP &valobj_sp) {
104   return nullptr;
105 }
106 
107 extern "C" void *
108 LLDBSwigPythonCreateCommandObject(const char *python_class_name,
109                                   const char *session_dictionary_name,
110                                   const lldb::DebuggerSP debugger_sp) {
111   return nullptr;
112 }
113 
114 extern "C" void *LLDBSwigPythonCreateScriptedThreadPlan(
115     const char *python_class_name, const char *session_dictionary_name,
116     StructuredDataImpl *args_data,
117     std::string &error_string,
118     const lldb::ThreadPlanSP &thread_plan_sp) {
119   return nullptr;
120 }
121 
122 extern "C" bool LLDBSWIGPythonCallThreadPlan(void *implementor,
123                                              const char *method_name,
124                                              Event *event_sp, bool &got_error) {
125   return false;
126 }
127 
128 extern "C" void *LLDBSwigPythonCreateScriptedBreakpointResolver(
129     const char *python_class_name, const char *session_dictionary_name,
130     lldb_private::StructuredDataImpl *args, lldb::BreakpointSP &bkpt_sp) {
131   return nullptr;
132 }
133 
134 extern "C" unsigned int
135 LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
136                                      lldb_private::SymbolContext *sym_ctx) {
137   return 0;
138 }
139 
140 extern "C" size_t LLDBSwigPython_CalculateNumChildren(void *implementor,
141                                                       uint32_t max) {
142   return 0;
143 }
144 
145 extern "C" void *LLDBSwigPython_GetChildAtIndex(void *implementor,
146                                                 uint32_t idx) {
147   return nullptr;
148 }
149 
150 extern "C" int LLDBSwigPython_GetIndexOfChildWithName(void *implementor,
151                                                       const char *child_name) {
152   return 0;
153 }
154 
155 extern "C" void *LLDBSWIGPython_CastPyObjectToSBValue(void *data) {
156   return nullptr;
157 }
158 
159 extern lldb::ValueObjectSP
160 LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data) {
161   return nullptr;
162 }
163 
164 extern "C" bool LLDBSwigPython_UpdateSynthProviderInstance(void *implementor) {
165   return false;
166 }
167 
168 extern "C" bool
169 LLDBSwigPython_MightHaveChildrenSynthProviderInstance(void *implementor) {
170   return false;
171 }
172 
173 extern "C" void *
174 LLDBSwigPython_GetValueSynthProviderInstance(void *implementor) {
175   return nullptr;
176 }
177 
178 extern "C" bool
179 LLDBSwigPythonCallCommand(const char *python_function_name,
180                           const char *session_dictionary_name,
181                           lldb::DebuggerSP &debugger, const char *args,
182                           lldb_private::CommandReturnObject &cmd_retobj,
183                           lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
184   return false;
185 }
186 
187 extern "C" bool
188 LLDBSwigPythonCallCommandObject(void *implementor, lldb::DebuggerSP &debugger,
189                                 const char *args,
190                                 lldb_private::CommandReturnObject &cmd_retobj,
191                                 lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
192   return false;
193 }
194 
195 extern "C" bool
196 LLDBSwigPythonCallModuleInit(const char *python_module_name,
197                              const char *session_dictionary_name,
198                              lldb::DebuggerSP &debugger) {
199   return false;
200 }
201 
202 extern "C" void *
203 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
204                              const char *session_dictionary_name,
205                              const lldb::ProcessSP &process_sp) {
206   return nullptr;
207 }
208 
209 extern "C" void *
210 LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
211                                      const char *session_dictionary_name) {
212   return nullptr;
213 }
214 
215 extern "C" void *
216 LLDBSwigPython_GetRecognizedArguments(void *implementor,
217                                       const lldb::StackFrameSP &frame_sp) {
218   return nullptr;
219 }
220 
221 extern "C" bool LLDBSWIGPythonRunScriptKeywordProcess(
222     const char *python_function_name, const char *session_dictionary_name,
223     lldb::ProcessSP &process, std::string &output) {
224   return false;
225 }
226 
227 extern "C" bool LLDBSWIGPythonRunScriptKeywordThread(
228     const char *python_function_name, const char *session_dictionary_name,
229     lldb::ThreadSP &thread, std::string &output) {
230   return false;
231 }
232 
233 extern "C" bool LLDBSWIGPythonRunScriptKeywordTarget(
234     const char *python_function_name, const char *session_dictionary_name,
235     lldb::TargetSP &target, std::string &output) {
236   return false;
237 }
238 
239 extern "C" bool LLDBSWIGPythonRunScriptKeywordFrame(
240     const char *python_function_name, const char *session_dictionary_name,
241     lldb::StackFrameSP &frame, std::string &output) {
242   return false;
243 }
244 
245 extern "C" bool LLDBSWIGPythonRunScriptKeywordValue(
246     const char *python_function_name, const char *session_dictionary_name,
247     lldb::ValueObjectSP &value, std::string &output) {
248   return false;
249 }
250 
251 extern "C" void *
252 LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
253                                  const lldb::TargetSP &target_sp) {
254   return nullptr;
255 }
256