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