xref: /llvm-project/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp (revision 738af7a6241c98164625b9cd1ba9f8af4e36f197)
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 extern "C" bool LLDBSwigPythonBreakpointCallbackFunction(
63     const char *python_function_name, const char *session_dictionary_name,
64     const lldb::StackFrameSP &sb_frame,
65     const lldb::BreakpointLocationSP &sb_bp_loc,
66     StructuredDataImpl *args_impl) {
67   return false;
68 }
69 
70 extern "C" bool LLDBSwigPythonWatchpointCallbackFunction(
71     const char *python_function_name, const char *session_dictionary_name,
72     const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp) {
73   return false;
74 }
75 
76 extern "C" bool LLDBSwigPythonCallTypeScript(
77     const char *python_function_name, void *session_dictionary,
78     const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
79     const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
80   return false;
81 }
82 
83 extern "C" void *
84 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
85                                       const char *session_dictionary_name,
86                                       const lldb::ValueObjectSP &valobj_sp) {
87   return nullptr;
88 }
89 
90 extern "C" void *
91 LLDBSwigPythonCreateCommandObject(const char *python_class_name,
92                                   const char *session_dictionary_name,
93                                   const lldb::DebuggerSP debugger_sp) {
94   return nullptr;
95 }
96 
97 extern "C" void *LLDBSwigPythonCreateScriptedThreadPlan(
98     const char *python_class_name, const char *session_dictionary_name,
99     StructuredDataImpl *args_data,
100     std::string &error_string,
101     const lldb::ThreadPlanSP &thread_plan_sp) {
102   return nullptr;
103 }
104 
105 extern "C" bool LLDBSWIGPythonCallThreadPlan(void *implementor,
106                                              const char *method_name,
107                                              Event *event_sp, bool &got_error) {
108   return false;
109 }
110 
111 extern "C" void *LLDBSwigPythonCreateScriptedBreakpointResolver(
112     const char *python_class_name, const char *session_dictionary_name,
113     lldb_private::StructuredDataImpl *args, lldb::BreakpointSP &bkpt_sp) {
114   return nullptr;
115 }
116 
117 extern "C" unsigned int
118 LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
119                                      lldb_private::SymbolContext *sym_ctx) {
120   return 0;
121 }
122 
123 extern "C" size_t LLDBSwigPython_CalculateNumChildren(void *implementor,
124                                                       uint32_t max) {
125   return 0;
126 }
127 
128 extern "C" void *LLDBSwigPython_GetChildAtIndex(void *implementor,
129                                                 uint32_t idx) {
130   return nullptr;
131 }
132 
133 extern "C" int LLDBSwigPython_GetIndexOfChildWithName(void *implementor,
134                                                       const char *child_name) {
135   return 0;
136 }
137 
138 extern "C" void *LLDBSWIGPython_CastPyObjectToSBValue(void *data) {
139   return nullptr;
140 }
141 
142 extern lldb::ValueObjectSP
143 LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data) {
144   return nullptr;
145 }
146 
147 extern "C" bool LLDBSwigPython_UpdateSynthProviderInstance(void *implementor) {
148   return false;
149 }
150 
151 extern "C" bool
152 LLDBSwigPython_MightHaveChildrenSynthProviderInstance(void *implementor) {
153   return false;
154 }
155 
156 extern "C" void *
157 LLDBSwigPython_GetValueSynthProviderInstance(void *implementor) {
158   return nullptr;
159 }
160 
161 extern "C" bool
162 LLDBSwigPythonCallCommand(const char *python_function_name,
163                           const char *session_dictionary_name,
164                           lldb::DebuggerSP &debugger, const char *args,
165                           lldb_private::CommandReturnObject &cmd_retobj,
166                           lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
167   return false;
168 }
169 
170 extern "C" bool
171 LLDBSwigPythonCallCommandObject(void *implementor, lldb::DebuggerSP &debugger,
172                                 const char *args,
173                                 lldb_private::CommandReturnObject &cmd_retobj,
174                                 lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
175   return false;
176 }
177 
178 extern "C" bool
179 LLDBSwigPythonCallModuleInit(const char *python_module_name,
180                              const char *session_dictionary_name,
181                              lldb::DebuggerSP &debugger) {
182   return false;
183 }
184 
185 extern "C" void *
186 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
187                              const char *session_dictionary_name,
188                              const lldb::ProcessSP &process_sp) {
189   return nullptr;
190 }
191 
192 extern "C" void *
193 LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
194                                      const char *session_dictionary_name) {
195   return nullptr;
196 }
197 
198 extern "C" void *
199 LLDBSwigPython_GetRecognizedArguments(void *implementor,
200                                       const lldb::StackFrameSP &frame_sp) {
201   return nullptr;
202 }
203 
204 extern "C" bool LLDBSWIGPythonRunScriptKeywordProcess(
205     const char *python_function_name, const char *session_dictionary_name,
206     lldb::ProcessSP &process, std::string &output) {
207   return false;
208 }
209 
210 extern "C" bool LLDBSWIGPythonRunScriptKeywordThread(
211     const char *python_function_name, const char *session_dictionary_name,
212     lldb::ThreadSP &thread, std::string &output) {
213   return false;
214 }
215 
216 extern "C" bool LLDBSWIGPythonRunScriptKeywordTarget(
217     const char *python_function_name, const char *session_dictionary_name,
218     lldb::TargetSP &target, std::string &output) {
219   return false;
220 }
221 
222 extern "C" bool LLDBSWIGPythonRunScriptKeywordFrame(
223     const char *python_function_name, const char *session_dictionary_name,
224     lldb::StackFrameSP &frame, std::string &output) {
225   return false;
226 }
227 
228 extern "C" bool LLDBSWIGPythonRunScriptKeywordValue(
229     const char *python_function_name, const char *session_dictionary_name,
230     lldb::ValueObjectSP &value, std::string &output) {
231   return false;
232 }
233 
234 extern "C" void *
235 LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
236                                  const lldb::TargetSP &target_sp) {
237   return nullptr;
238 }
239