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