180814287SRaphael Isemann //===-- PythonTestSuite.cpp -----------------------------------------------===//
2c946d462SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c946d462SZachary Turner //
7c946d462SZachary Turner //===----------------------------------------------------------------------===//
8c946d462SZachary Turner 
9b9c1b51eSKate Stone #include "gtest/gtest.h"
10c946d462SZachary Turner 
11f4413134SJonas Devlieghere #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
12f4413134SJonas Devlieghere 
13c946d462SZachary Turner #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
146f8251fbSJonas Devlieghere #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h"
153925204cSMed Ismail Bennani 
1646376966SJonas Devlieghere #include "lldb/Host/FileSystem.h"
17b9c1b51eSKate Stone #include "lldb/Host/HostInfo.h"
18c946d462SZachary Turner 
19c946d462SZachary Turner #include "PythonTestSuite.h"
20c946d462SZachary Turner 
21c946d462SZachary Turner using namespace lldb_private;
226f8251fbSJonas Devlieghere class TestScriptInterpreterPython : public ScriptInterpreterPythonImpl {
23e3959de2SJonas Devlieghere public:
246f8251fbSJonas Devlieghere   using ScriptInterpreterPythonImpl::Initialize;
256f8251fbSJonas Devlieghere   using ScriptInterpreterPythonImpl::InitializePrivate;
26e3959de2SJonas Devlieghere };
27c946d462SZachary Turner 
28b9c1b51eSKate Stone void PythonTestSuite::SetUp() {
2946376966SJonas Devlieghere   FileSystem::Initialize();
30c946d462SZachary Turner   HostInfoBase::Initialize();
31c946d462SZachary Turner   // ScriptInterpreterPython::Initialize() depends on HostInfo being
32c946d462SZachary Turner   // initializedso it can compute the python directory etc.
33e3959de2SJonas Devlieghere   TestScriptInterpreterPython::Initialize();
34e3959de2SJonas Devlieghere   TestScriptInterpreterPython::InitializePrivate();
35c946d462SZachary Turner 
36c946d462SZachary Turner   // Although we don't care about concurrency for the purposes of running
37c946d462SZachary Turner   // this test suite, Python requires the GIL to be locked even for
38c946d462SZachary Turner   // deallocating memory, which can happen when you call Py_DECREF or
39c946d462SZachary Turner   // Py_INCREF.  So acquire the GIL for the entire duration of this
40c946d462SZachary Turner   // test suite.
41c946d462SZachary Turner   m_gil_state = PyGILState_Ensure();
42c946d462SZachary Turner }
43c946d462SZachary Turner 
44b9c1b51eSKate Stone void PythonTestSuite::TearDown() {
45c946d462SZachary Turner   PyGILState_Release(m_gil_state);
46c946d462SZachary Turner 
47e3959de2SJonas Devlieghere   TestScriptInterpreterPython::Terminate();
480bca15a3SJonas Devlieghere   HostInfoBase::Terminate();
490bca15a3SJonas Devlieghere   FileSystem::Terminate();
50c946d462SZachary Turner }
51282890d7SJonas Devlieghere 
52282890d7SJonas Devlieghere // The following functions are the Pythonic implementations of the required
53282890d7SJonas Devlieghere // callbacks. Because they're defined in libLLDB which we cannot link for the
54282890d7SJonas Devlieghere // unit test, we have a 'default' implementation here.
55282890d7SJonas Devlieghere 
56282890d7SJonas Devlieghere #if PY_MAJOR_VERSION >= 3
57282890d7SJonas Devlieghere extern "C" PyObject *PyInit__lldb(void) { return nullptr; }
58282890d7SJonas Devlieghere #define LLDBSwigPyInit PyInit__lldb
59282890d7SJonas Devlieghere #else
60282890d7SJonas Devlieghere extern "C" void init_lldb(void) {}
61282890d7SJonas Devlieghere #define LLDBSwigPyInit init_lldb
62282890d7SJonas Devlieghere #endif
63282890d7SJonas Devlieghere 
64fb01c01bSLawrence D'Anna #pragma clang diagnostic push
65fb01c01bSLawrence D'Anna #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
66fb01c01bSLawrence D'Anna 
671cc0ba4cSAlexandre Ganea // Disable warning C4190: 'LLDBSwigPythonBreakpointCallbackFunction' has
681cc0ba4cSAlexandre Ganea // C-linkage specified, but returns UDT 'llvm::Expected<bool>' which is
691cc0ba4cSAlexandre Ganea // incompatible with C
701cc0ba4cSAlexandre Ganea #if _MSC_VER
711cc0ba4cSAlexandre Ganea #pragma warning (push)
721cc0ba4cSAlexandre Ganea #pragma warning (disable : 4190)
731cc0ba4cSAlexandre Ganea #endif
741cc0ba4cSAlexandre Ganea 
75a69bbe02SLawrence D'Anna extern "C" llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
76282890d7SJonas Devlieghere     const char *python_function_name, const char *session_dictionary_name,
77282890d7SJonas Devlieghere     const lldb::StackFrameSP &sb_frame,
78738af7a6SJim Ingham     const lldb::BreakpointLocationSP &sb_bp_loc,
79738af7a6SJim Ingham     StructuredDataImpl *args_impl) {
80282890d7SJonas Devlieghere   return false;
81282890d7SJonas Devlieghere }
82282890d7SJonas Devlieghere 
831cc0ba4cSAlexandre Ganea #if _MSC_VER
841cc0ba4cSAlexandre Ganea #pragma warning (pop)
851cc0ba4cSAlexandre Ganea #endif
861cc0ba4cSAlexandre Ganea 
87fb01c01bSLawrence D'Anna #pragma clang diagnostic pop
88fb01c01bSLawrence D'Anna 
89282890d7SJonas Devlieghere extern "C" bool LLDBSwigPythonWatchpointCallbackFunction(
90282890d7SJonas Devlieghere     const char *python_function_name, const char *session_dictionary_name,
91282890d7SJonas Devlieghere     const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp) {
92282890d7SJonas Devlieghere   return false;
93282890d7SJonas Devlieghere }
94282890d7SJonas Devlieghere 
95282890d7SJonas Devlieghere extern "C" bool LLDBSwigPythonCallTypeScript(
96282890d7SJonas Devlieghere     const char *python_function_name, void *session_dictionary,
97282890d7SJonas Devlieghere     const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
98282890d7SJonas Devlieghere     const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
99282890d7SJonas Devlieghere   return false;
100282890d7SJonas Devlieghere }
101282890d7SJonas Devlieghere 
102282890d7SJonas Devlieghere extern "C" void *
103282890d7SJonas Devlieghere LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
104282890d7SJonas Devlieghere                                       const char *session_dictionary_name,
105282890d7SJonas Devlieghere                                       const lldb::ValueObjectSP &valobj_sp) {
106282890d7SJonas Devlieghere   return nullptr;
107282890d7SJonas Devlieghere }
108282890d7SJonas Devlieghere 
109282890d7SJonas Devlieghere extern "C" void *
110282890d7SJonas Devlieghere LLDBSwigPythonCreateCommandObject(const char *python_class_name,
111282890d7SJonas Devlieghere                                   const char *session_dictionary_name,
112282890d7SJonas Devlieghere                                   const lldb::DebuggerSP debugger_sp) {
113282890d7SJonas Devlieghere   return nullptr;
114282890d7SJonas Devlieghere }
115282890d7SJonas Devlieghere 
116282890d7SJonas Devlieghere extern "C" void *LLDBSwigPythonCreateScriptedThreadPlan(
117282890d7SJonas Devlieghere     const char *python_class_name, const char *session_dictionary_name,
11827a14f19SJim Ingham     StructuredDataImpl *args_data,
11993c98346SJim Ingham     std::string &error_string,
120282890d7SJonas Devlieghere     const lldb::ThreadPlanSP &thread_plan_sp) {
121282890d7SJonas Devlieghere   return nullptr;
122282890d7SJonas Devlieghere }
123282890d7SJonas Devlieghere 
124282890d7SJonas Devlieghere extern "C" bool LLDBSWIGPythonCallThreadPlan(void *implementor,
125282890d7SJonas Devlieghere                                              const char *method_name,
126282890d7SJonas Devlieghere                                              Event *event_sp, bool &got_error) {
127282890d7SJonas Devlieghere   return false;
128282890d7SJonas Devlieghere }
129282890d7SJonas Devlieghere 
130282890d7SJonas Devlieghere extern "C" void *LLDBSwigPythonCreateScriptedBreakpointResolver(
131282890d7SJonas Devlieghere     const char *python_class_name, const char *session_dictionary_name,
132*7f09ab08SPavel Labath     lldb_private::StructuredDataImpl *args, const lldb::BreakpointSP &bkpt_sp) {
133282890d7SJonas Devlieghere   return nullptr;
134282890d7SJonas Devlieghere }
135282890d7SJonas Devlieghere 
136282890d7SJonas Devlieghere extern "C" unsigned int
137282890d7SJonas Devlieghere LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
138282890d7SJonas Devlieghere                                      lldb_private::SymbolContext *sym_ctx) {
139282890d7SJonas Devlieghere   return 0;
140282890d7SJonas Devlieghere }
141282890d7SJonas Devlieghere 
142282890d7SJonas Devlieghere extern "C" size_t LLDBSwigPython_CalculateNumChildren(void *implementor,
143282890d7SJonas Devlieghere                                                       uint32_t max) {
144282890d7SJonas Devlieghere   return 0;
145282890d7SJonas Devlieghere }
146282890d7SJonas Devlieghere 
147282890d7SJonas Devlieghere extern "C" void *LLDBSwigPython_GetChildAtIndex(void *implementor,
148282890d7SJonas Devlieghere                                                 uint32_t idx) {
149282890d7SJonas Devlieghere   return nullptr;
150282890d7SJonas Devlieghere }
151282890d7SJonas Devlieghere 
152282890d7SJonas Devlieghere extern "C" int LLDBSwigPython_GetIndexOfChildWithName(void *implementor,
153282890d7SJonas Devlieghere                                                       const char *child_name) {
154282890d7SJonas Devlieghere   return 0;
155282890d7SJonas Devlieghere }
156282890d7SJonas Devlieghere 
1571f6a57c1SMed Ismail Bennani extern "C" void *LLDBSWIGPython_CastPyObjectToSBData(void *data) {
1581f6a57c1SMed Ismail Bennani   return nullptr;
1591f6a57c1SMed Ismail Bennani }
1601f6a57c1SMed Ismail Bennani 
1611f6a57c1SMed Ismail Bennani extern "C" void *LLDBSWIGPython_CastPyObjectToSBError(void *data) {
1621f6a57c1SMed Ismail Bennani   return nullptr;
1631f6a57c1SMed Ismail Bennani }
1641f6a57c1SMed Ismail Bennani 
165282890d7SJonas Devlieghere extern "C" void *LLDBSWIGPython_CastPyObjectToSBValue(void *data) {
166282890d7SJonas Devlieghere   return nullptr;
167282890d7SJonas Devlieghere }
168282890d7SJonas Devlieghere 
169a758c9f7SMed Ismail Bennani extern "C" void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(void *data) {
170a758c9f7SMed Ismail Bennani   return nullptr;
171a758c9f7SMed Ismail Bennani }
172a758c9f7SMed Ismail Bennani 
173282890d7SJonas Devlieghere extern lldb::ValueObjectSP
174282890d7SJonas Devlieghere LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data) {
175282890d7SJonas Devlieghere   return nullptr;
176282890d7SJonas Devlieghere }
177282890d7SJonas Devlieghere 
178282890d7SJonas Devlieghere extern "C" bool LLDBSwigPython_UpdateSynthProviderInstance(void *implementor) {
179282890d7SJonas Devlieghere   return false;
180282890d7SJonas Devlieghere }
181282890d7SJonas Devlieghere 
182282890d7SJonas Devlieghere extern "C" bool
183282890d7SJonas Devlieghere LLDBSwigPython_MightHaveChildrenSynthProviderInstance(void *implementor) {
184282890d7SJonas Devlieghere   return false;
185282890d7SJonas Devlieghere }
186282890d7SJonas Devlieghere 
187282890d7SJonas Devlieghere extern "C" void *
188282890d7SJonas Devlieghere LLDBSwigPython_GetValueSynthProviderInstance(void *implementor) {
189282890d7SJonas Devlieghere   return nullptr;
190282890d7SJonas Devlieghere }
191282890d7SJonas Devlieghere 
192282890d7SJonas Devlieghere extern "C" bool
193282890d7SJonas Devlieghere LLDBSwigPythonCallCommand(const char *python_function_name,
194282890d7SJonas Devlieghere                           const char *session_dictionary_name,
195282890d7SJonas Devlieghere                           lldb::DebuggerSP &debugger, const char *args,
196282890d7SJonas Devlieghere                           lldb_private::CommandReturnObject &cmd_retobj,
197282890d7SJonas Devlieghere                           lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
198282890d7SJonas Devlieghere   return false;
199282890d7SJonas Devlieghere }
200282890d7SJonas Devlieghere 
201282890d7SJonas Devlieghere extern "C" bool
202282890d7SJonas Devlieghere LLDBSwigPythonCallCommandObject(void *implementor, lldb::DebuggerSP &debugger,
203282890d7SJonas Devlieghere                                 const char *args,
204282890d7SJonas Devlieghere                                 lldb_private::CommandReturnObject &cmd_retobj,
205282890d7SJonas Devlieghere                                 lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
206282890d7SJonas Devlieghere   return false;
207282890d7SJonas Devlieghere }
208282890d7SJonas Devlieghere 
209282890d7SJonas Devlieghere extern "C" bool
210282890d7SJonas Devlieghere LLDBSwigPythonCallModuleInit(const char *python_module_name,
211282890d7SJonas Devlieghere                              const char *session_dictionary_name,
212282890d7SJonas Devlieghere                              lldb::DebuggerSP &debugger) {
213282890d7SJonas Devlieghere   return false;
214282890d7SJonas Devlieghere }
215282890d7SJonas Devlieghere 
216282890d7SJonas Devlieghere extern "C" void *
217282890d7SJonas Devlieghere LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
218282890d7SJonas Devlieghere                              const char *session_dictionary_name,
219282890d7SJonas Devlieghere                              const lldb::ProcessSP &process_sp) {
220282890d7SJonas Devlieghere   return nullptr;
221282890d7SJonas Devlieghere }
222282890d7SJonas Devlieghere 
2231f6a57c1SMed Ismail Bennani extern "C" void *LLDBSwigPythonCreateScriptedProcess(
2241f6a57c1SMed Ismail Bennani     const char *python_class_name, const char *session_dictionary_name,
2251f6a57c1SMed Ismail Bennani     const lldb::TargetSP &target_sp, StructuredDataImpl *args_impl,
2261f6a57c1SMed Ismail Bennani     std::string &error_string) {
2271f6a57c1SMed Ismail Bennani   return nullptr;
2281f6a57c1SMed Ismail Bennani }
2291f6a57c1SMed Ismail Bennani 
23059d8dd79SMed Ismail Bennani extern "C" void *LLDBSwigPythonCreateScriptedThread(
23159d8dd79SMed Ismail Bennani     const char *python_class_name, const char *session_dictionary_name,
232738621d0SMed Ismail Bennani     const lldb::ProcessSP &process_sp, StructuredDataImpl *args_impl,
233738621d0SMed Ismail Bennani     std::string &error_string) {
23459d8dd79SMed Ismail Bennani   return nullptr;
23559d8dd79SMed Ismail Bennani }
23659d8dd79SMed Ismail Bennani 
237282890d7SJonas Devlieghere extern "C" void *
238282890d7SJonas Devlieghere LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
239282890d7SJonas Devlieghere                                      const char *session_dictionary_name) {
240282890d7SJonas Devlieghere   return nullptr;
241282890d7SJonas Devlieghere }
242282890d7SJonas Devlieghere 
243282890d7SJonas Devlieghere extern "C" void *
244282890d7SJonas Devlieghere LLDBSwigPython_GetRecognizedArguments(void *implementor,
245282890d7SJonas Devlieghere                                       const lldb::StackFrameSP &frame_sp) {
246282890d7SJonas Devlieghere   return nullptr;
247282890d7SJonas Devlieghere }
248282890d7SJonas Devlieghere 
249282890d7SJonas Devlieghere extern "C" bool LLDBSWIGPythonRunScriptKeywordProcess(
250282890d7SJonas Devlieghere     const char *python_function_name, const char *session_dictionary_name,
251*7f09ab08SPavel Labath     const lldb::ProcessSP &process, std::string &output) {
252282890d7SJonas Devlieghere   return false;
253282890d7SJonas Devlieghere }
254282890d7SJonas Devlieghere 
255282890d7SJonas Devlieghere extern "C" bool LLDBSWIGPythonRunScriptKeywordThread(
256282890d7SJonas Devlieghere     const char *python_function_name, const char *session_dictionary_name,
257282890d7SJonas Devlieghere     lldb::ThreadSP &thread, std::string &output) {
258282890d7SJonas Devlieghere   return false;
259282890d7SJonas Devlieghere }
260282890d7SJonas Devlieghere 
261282890d7SJonas Devlieghere extern "C" bool LLDBSWIGPythonRunScriptKeywordTarget(
262282890d7SJonas Devlieghere     const char *python_function_name, const char *session_dictionary_name,
263*7f09ab08SPavel Labath     const lldb::TargetSP &target, std::string &output) {
264282890d7SJonas Devlieghere   return false;
265282890d7SJonas Devlieghere }
266282890d7SJonas Devlieghere 
267282890d7SJonas Devlieghere extern "C" bool LLDBSWIGPythonRunScriptKeywordFrame(
268282890d7SJonas Devlieghere     const char *python_function_name, const char *session_dictionary_name,
269282890d7SJonas Devlieghere     lldb::StackFrameSP &frame, std::string &output) {
270282890d7SJonas Devlieghere   return false;
271282890d7SJonas Devlieghere }
272282890d7SJonas Devlieghere 
273282890d7SJonas Devlieghere extern "C" bool LLDBSWIGPythonRunScriptKeywordValue(
274282890d7SJonas Devlieghere     const char *python_function_name, const char *session_dictionary_name,
275*7f09ab08SPavel Labath     const lldb::ValueObjectSP &value, std::string &output) {
276282890d7SJonas Devlieghere   return false;
277282890d7SJonas Devlieghere }
278282890d7SJonas Devlieghere 
279282890d7SJonas Devlieghere extern "C" void *
280282890d7SJonas Devlieghere LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
281282890d7SJonas Devlieghere                                  const lldb::TargetSP &target_sp) {
282282890d7SJonas Devlieghere   return nullptr;
283282890d7SJonas Devlieghere }
2841b1d9815SJim Ingham 
2851b1d9815SJim Ingham extern "C" void *LLDBSwigPythonCreateScriptedStopHook(
2861b1d9815SJim Ingham     lldb::TargetSP target_sp, const char *python_class_name,
2871b1d9815SJim Ingham     const char *session_dictionary_name,
2881b1d9815SJim Ingham     lldb_private::StructuredDataImpl *args_impl, Status &error) {
2891b1d9815SJim Ingham   return nullptr;
2901b1d9815SJim Ingham }
2911b1d9815SJim Ingham 
2921b1d9815SJim Ingham extern "C" bool
2931b1d9815SJim Ingham LLDBSwigPythonStopHookCallHandleStop(void *implementor,
2941b1d9815SJim Ingham                                      lldb::ExecutionContextRefSP exc_ctx_sp,
2951b1d9815SJim Ingham                                      lldb::StreamSP stream) {
2961b1d9815SJim Ingham   return false;
2971b1d9815SJim Ingham }
298