1fe6060f1SDimitry Andric //===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric 
9fe6060f1SDimitry Andric #ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
10fe6060f1SDimitry Andric #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
11fe6060f1SDimitry Andric 
12bdd1243dSDimitry Andric #include <optional>
13fe6060f1SDimitry Andric #include <string>
14fe6060f1SDimitry Andric 
15fe6060f1SDimitry Andric #include "lldb/Host/Config.h"
16fe6060f1SDimitry Andric 
17fe6060f1SDimitry Andric #if LLDB_ENABLE_PYTHON
18fe6060f1SDimitry Andric 
194824e7fdSDimitry Andric // LLDB Python header must be included first
204824e7fdSDimitry Andric #include "lldb-python.h"
214824e7fdSDimitry Andric 
2204eeddc0SDimitry Andric #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"
23fe6060f1SDimitry Andric #include "lldb/lldb-forward.h"
24fe6060f1SDimitry Andric #include "lldb/lldb-types.h"
254824e7fdSDimitry Andric #include "llvm/Support/Error.h"
26fe6060f1SDimitry Andric 
27bdd1243dSDimitry Andric namespace lldb {
28bdd1243dSDimitry Andric class SBEvent;
29bdd1243dSDimitry Andric class SBCommandReturnObject;
30bdd1243dSDimitry Andric class SBValue;
31bdd1243dSDimitry Andric class SBStream;
32bdd1243dSDimitry Andric class SBStructuredData;
33bdd1243dSDimitry Andric } // namespace lldb
34bdd1243dSDimitry Andric 
35fe6060f1SDimitry Andric namespace lldb_private {
36bdd1243dSDimitry Andric namespace python {
37fe6060f1SDimitry Andric 
38bdd1243dSDimitry Andric typedef struct swig_type_info swig_type_info;
39fe6060f1SDimitry Andric 
40bdd1243dSDimitry Andric python::PythonObject ToSWIGHelper(void *obj, swig_type_info *info);
41bdd1243dSDimitry Andric 
42bdd1243dSDimitry Andric /// A class that automatically clears an SB object when it goes out of scope.
43bdd1243dSDimitry Andric /// Use for cases where the SB object points to a temporary/unowned entity.
44bdd1243dSDimitry Andric template <typename T> class ScopedPythonObject : PythonObject {
45bdd1243dSDimitry Andric public:
46bdd1243dSDimitry Andric   ScopedPythonObject(T *sb, swig_type_info *info)
47bdd1243dSDimitry Andric       : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}
48bdd1243dSDimitry Andric   ~ScopedPythonObject() {
49bdd1243dSDimitry Andric     if (m_sb)
50bdd1243dSDimitry Andric       *m_sb = T();
51bdd1243dSDimitry Andric   }
52bdd1243dSDimitry Andric   ScopedPythonObject(ScopedPythonObject &&rhs)
53bdd1243dSDimitry Andric       : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}
54bdd1243dSDimitry Andric   ScopedPythonObject(const ScopedPythonObject &) = delete;
55bdd1243dSDimitry Andric   ScopedPythonObject &operator=(const ScopedPythonObject &) = delete;
56bdd1243dSDimitry Andric   ScopedPythonObject &operator=(ScopedPythonObject &&) = delete;
57bdd1243dSDimitry Andric 
58bdd1243dSDimitry Andric   const PythonObject &obj() const { return *this; }
59bdd1243dSDimitry Andric 
60bdd1243dSDimitry Andric private:
61bdd1243dSDimitry Andric   T *m_sb;
62bdd1243dSDimitry Andric };
63bdd1243dSDimitry Andric 
64bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp);
65bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::TargetSP target_sp);
66bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp);
67bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp);
68bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp);
69bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(const Status &status);
70bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl);
71bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp);
72bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp);
73bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp);
74bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp);
75bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp);
76bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp);
77bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options);
78bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx);
79bdd1243dSDimitry Andric 
80bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb);
81bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStream> stream_sb);
82bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb);
83bdd1243dSDimitry Andric 
84bdd1243dSDimitry Andric python::ScopedPythonObject<lldb::SBCommandReturnObject>
85bdd1243dSDimitry Andric ToSWIGWrapper(CommandReturnObject &cmd_retobj);
86bdd1243dSDimitry Andric python::ScopedPythonObject<lldb::SBEvent> ToSWIGWrapper(Event *event);
87bdd1243dSDimitry Andric 
88bdd1243dSDimitry Andric } // namespace python
89fe6060f1SDimitry Andric 
904824e7fdSDimitry Andric void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data);
914824e7fdSDimitry Andric void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data);
924824e7fdSDimitry Andric void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data);
934824e7fdSDimitry Andric void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data);
944824e7fdSDimitry Andric 
954824e7fdSDimitry Andric // These prototypes are the Pythonic implementations of the required callbacks.
964824e7fdSDimitry Andric // Although these are scripting-language specific, their definition depends on
974824e7fdSDimitry Andric // the public API.
984824e7fdSDimitry Andric 
99bdd1243dSDimitry Andric python::PythonObject LLDBSwigPythonCreateScriptedObject(
10004eeddc0SDimitry Andric     const char *python_class_name, const char *session_dictionary_name,
101bdd1243dSDimitry Andric     lldb::ExecutionContextRefSP exe_ctx_sp,
102bdd1243dSDimitry Andric     const lldb_private::StructuredDataImpl &args_impl,
103349cc55cSDimitry Andric     std::string &error_string);
104349cc55cSDimitry Andric 
1054824e7fdSDimitry Andric llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
1064824e7fdSDimitry Andric     const char *python_function_name, const char *session_dictionary_name,
1074824e7fdSDimitry Andric     const lldb::StackFrameSP &sb_frame,
1084824e7fdSDimitry Andric     const lldb::BreakpointLocationSP &sb_bp_loc,
1090eae32dcSDimitry Andric     const lldb_private::StructuredDataImpl &args_impl);
1104824e7fdSDimitry Andric 
1114824e7fdSDimitry Andric bool LLDBSwigPythonWatchpointCallbackFunction(
1124824e7fdSDimitry Andric     const char *python_function_name, const char *session_dictionary_name,
1134824e7fdSDimitry Andric     const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);
1144824e7fdSDimitry Andric 
115bdd1243dSDimitry Andric bool LLDBSwigPythonFormatterCallbackFunction(
116bdd1243dSDimitry Andric     const char *python_function_name, const char *session_dictionary_name,
117bdd1243dSDimitry Andric     lldb::TypeImplSP type_impl_sp);
118bdd1243dSDimitry Andric 
1194824e7fdSDimitry Andric bool LLDBSwigPythonCallTypeScript(const char *python_function_name,
1204824e7fdSDimitry Andric                                   const void *session_dictionary,
1214824e7fdSDimitry Andric                                   const lldb::ValueObjectSP &valobj_sp,
1224824e7fdSDimitry Andric                                   void **pyfunct_wrapper,
1234824e7fdSDimitry Andric                                   const lldb::TypeSummaryOptionsSP &options_sp,
1244824e7fdSDimitry Andric                                   std::string &retval);
1254824e7fdSDimitry Andric 
12604eeddc0SDimitry Andric python::PythonObject
1274824e7fdSDimitry Andric LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
1284824e7fdSDimitry Andric                                       const char *session_dictionary_name,
1294824e7fdSDimitry Andric                                       const lldb::ValueObjectSP &valobj_sp);
1304824e7fdSDimitry Andric 
13104eeddc0SDimitry Andric python::PythonObject
13204eeddc0SDimitry Andric LLDBSwigPythonCreateCommandObject(const char *python_class_name,
1334824e7fdSDimitry Andric                                   const char *session_dictionary_name,
1340eae32dcSDimitry Andric                                   lldb::DebuggerSP debugger_sp);
1354824e7fdSDimitry Andric 
13604eeddc0SDimitry Andric python::PythonObject LLDBSwigPythonCreateScriptedThreadPlan(
1374824e7fdSDimitry Andric     const char *python_class_name, const char *session_dictionary_name,
1380eae32dcSDimitry Andric     const StructuredDataImpl &args_data, std::string &error_string,
1394824e7fdSDimitry Andric     const lldb::ThreadPlanSP &thread_plan_sp);
1404824e7fdSDimitry Andric 
1414824e7fdSDimitry Andric bool LLDBSWIGPythonCallThreadPlan(void *implementor, const char *method_name,
1424824e7fdSDimitry Andric                                   lldb_private::Event *event_sp,
1434824e7fdSDimitry Andric                                   bool &got_error);
1444824e7fdSDimitry Andric 
14504eeddc0SDimitry Andric python::PythonObject LLDBSwigPythonCreateScriptedBreakpointResolver(
1464824e7fdSDimitry Andric     const char *python_class_name, const char *session_dictionary_name,
1470eae32dcSDimitry Andric     const StructuredDataImpl &args, const lldb::BreakpointSP &bkpt_sp);
1484824e7fdSDimitry Andric 
1494824e7fdSDimitry Andric unsigned int
1504824e7fdSDimitry Andric LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
1514824e7fdSDimitry Andric                                      lldb_private::SymbolContext *sym_ctx);
1524824e7fdSDimitry Andric 
15304eeddc0SDimitry Andric python::PythonObject LLDBSwigPythonCreateScriptedStopHook(
15404eeddc0SDimitry Andric     lldb::TargetSP target_sp, const char *python_class_name,
15504eeddc0SDimitry Andric     const char *session_dictionary_name, const StructuredDataImpl &args,
1564824e7fdSDimitry Andric     lldb_private::Status &error);
1574824e7fdSDimitry Andric 
1584824e7fdSDimitry Andric bool LLDBSwigPythonStopHookCallHandleStop(void *implementor,
1594824e7fdSDimitry Andric                                           lldb::ExecutionContextRefSP exc_ctx,
1604824e7fdSDimitry Andric                                           lldb::StreamSP stream);
1614824e7fdSDimitry Andric 
1624824e7fdSDimitry Andric size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor, uint32_t max);
1634824e7fdSDimitry Andric 
1644824e7fdSDimitry Andric PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor, uint32_t idx);
1654824e7fdSDimitry Andric 
1664824e7fdSDimitry Andric int LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor,
1674824e7fdSDimitry Andric                                            const char *child_name);
1684824e7fdSDimitry Andric 
1694824e7fdSDimitry Andric lldb::ValueObjectSP LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data);
1704824e7fdSDimitry Andric 
1714824e7fdSDimitry Andric bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor);
1724824e7fdSDimitry Andric 
1734824e7fdSDimitry Andric bool LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
1744824e7fdSDimitry Andric     PyObject *implementor);
1754824e7fdSDimitry Andric 
1764824e7fdSDimitry Andric PyObject *LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor);
1774824e7fdSDimitry Andric 
1784824e7fdSDimitry Andric bool LLDBSwigPythonCallCommand(const char *python_function_name,
1794824e7fdSDimitry Andric                                const char *session_dictionary_name,
1800eae32dcSDimitry Andric                                lldb::DebuggerSP debugger, const char *args,
1814824e7fdSDimitry Andric                                lldb_private::CommandReturnObject &cmd_retobj,
1824824e7fdSDimitry Andric                                lldb::ExecutionContextRefSP exe_ctx_ref_sp);
1834824e7fdSDimitry Andric 
1844824e7fdSDimitry Andric bool LLDBSwigPythonCallCommandObject(
1850eae32dcSDimitry Andric     PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
1864824e7fdSDimitry Andric     lldb_private::CommandReturnObject &cmd_retobj,
1874824e7fdSDimitry Andric     lldb::ExecutionContextRefSP exe_ctx_ref_sp);
1884824e7fdSDimitry Andric 
1894824e7fdSDimitry Andric bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
1904824e7fdSDimitry Andric                                   const char *session_dictionary_name,
1910eae32dcSDimitry Andric                                   lldb::DebuggerSP debugger);
1924824e7fdSDimitry Andric 
19304eeddc0SDimitry Andric python::PythonObject
19404eeddc0SDimitry Andric LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
1954824e7fdSDimitry Andric                              const char *session_dictionary_name,
1964824e7fdSDimitry Andric                              const lldb::ProcessSP &process_sp);
1974824e7fdSDimitry Andric 
19804eeddc0SDimitry Andric python::PythonObject
19904eeddc0SDimitry Andric LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
2004824e7fdSDimitry Andric                                      const char *session_dictionary_name);
2014824e7fdSDimitry Andric 
2024824e7fdSDimitry Andric PyObject *
2034824e7fdSDimitry Andric LLDBSwigPython_GetRecognizedArguments(PyObject *implementor,
2044824e7fdSDimitry Andric                                       const lldb::StackFrameSP &frame_sp);
2054824e7fdSDimitry Andric 
2064824e7fdSDimitry Andric bool LLDBSWIGPythonRunScriptKeywordProcess(const char *python_function_name,
2074824e7fdSDimitry Andric                                            const char *session_dictionary_name,
2084824e7fdSDimitry Andric                                            const lldb::ProcessSP &process,
2094824e7fdSDimitry Andric                                            std::string &output);
2104824e7fdSDimitry Andric 
211bdd1243dSDimitry Andric std::optional<std::string>
2120eae32dcSDimitry Andric LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
2134824e7fdSDimitry Andric                                      const char *session_dictionary_name,
2140eae32dcSDimitry Andric                                      lldb::ThreadSP thread);
2154824e7fdSDimitry Andric 
2164824e7fdSDimitry Andric bool LLDBSWIGPythonRunScriptKeywordTarget(const char *python_function_name,
2174824e7fdSDimitry Andric                                           const char *session_dictionary_name,
2184824e7fdSDimitry Andric                                           const lldb::TargetSP &target,
2194824e7fdSDimitry Andric                                           std::string &output);
2204824e7fdSDimitry Andric 
221bdd1243dSDimitry Andric std::optional<std::string>
2220eae32dcSDimitry Andric LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
2234824e7fdSDimitry Andric                                     const char *session_dictionary_name,
2240eae32dcSDimitry Andric                                     lldb::StackFrameSP frame);
2254824e7fdSDimitry Andric 
2264824e7fdSDimitry Andric bool LLDBSWIGPythonRunScriptKeywordValue(const char *python_function_name,
2274824e7fdSDimitry Andric                                          const char *session_dictionary_name,
2284824e7fdSDimitry Andric                                          const lldb::ValueObjectSP &value,
2294824e7fdSDimitry Andric                                          std::string &output);
2304824e7fdSDimitry Andric 
2314824e7fdSDimitry Andric void *LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
2324824e7fdSDimitry Andric                                        const lldb::TargetSP &target_sp);
233fe6060f1SDimitry Andric 
234fe6060f1SDimitry Andric } // namespace lldb_private
235fe6060f1SDimitry Andric 
236fe6060f1SDimitry Andric #endif // LLDB_ENABLE_PYTHON
237fe6060f1SDimitry Andric #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
238