xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 
12*bdd1243dSDimitry 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 
27*bdd1243dSDimitry Andric namespace lldb {
28*bdd1243dSDimitry Andric class SBEvent;
29*bdd1243dSDimitry Andric class SBCommandReturnObject;
30*bdd1243dSDimitry Andric class SBValue;
31*bdd1243dSDimitry Andric class SBStream;
32*bdd1243dSDimitry Andric class SBStructuredData;
33*bdd1243dSDimitry Andric } // namespace lldb
34*bdd1243dSDimitry Andric 
35fe6060f1SDimitry Andric namespace lldb_private {
36*bdd1243dSDimitry Andric namespace python {
37fe6060f1SDimitry Andric 
38*bdd1243dSDimitry Andric typedef struct swig_type_info swig_type_info;
39fe6060f1SDimitry Andric 
40*bdd1243dSDimitry Andric python::PythonObject ToSWIGHelper(void *obj, swig_type_info *info);
41*bdd1243dSDimitry Andric 
42*bdd1243dSDimitry Andric /// A class that automatically clears an SB object when it goes out of scope.
43*bdd1243dSDimitry Andric /// Use for cases where the SB object points to a temporary/unowned entity.
44*bdd1243dSDimitry Andric template <typename T> class ScopedPythonObject : PythonObject {
45*bdd1243dSDimitry Andric public:
46*bdd1243dSDimitry Andric   ScopedPythonObject(T *sb, swig_type_info *info)
47*bdd1243dSDimitry Andric       : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}
48*bdd1243dSDimitry Andric   ~ScopedPythonObject() {
49*bdd1243dSDimitry Andric     if (m_sb)
50*bdd1243dSDimitry Andric       *m_sb = T();
51*bdd1243dSDimitry Andric   }
52*bdd1243dSDimitry Andric   ScopedPythonObject(ScopedPythonObject &&rhs)
53*bdd1243dSDimitry Andric       : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}
54*bdd1243dSDimitry Andric   ScopedPythonObject(const ScopedPythonObject &) = delete;
55*bdd1243dSDimitry Andric   ScopedPythonObject &operator=(const ScopedPythonObject &) = delete;
56*bdd1243dSDimitry Andric   ScopedPythonObject &operator=(ScopedPythonObject &&) = delete;
57*bdd1243dSDimitry Andric 
58*bdd1243dSDimitry Andric   const PythonObject &obj() const { return *this; }
59*bdd1243dSDimitry Andric 
60*bdd1243dSDimitry Andric private:
61*bdd1243dSDimitry Andric   T *m_sb;
62*bdd1243dSDimitry Andric };
63*bdd1243dSDimitry Andric 
64*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp);
65*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::TargetSP target_sp);
66*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp);
67*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp);
68*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp);
69*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(const Status &status);
70*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl);
71*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp);
72*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp);
73*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp);
74*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp);
75*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp);
76*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp);
77*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options);
78*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx);
79*bdd1243dSDimitry Andric 
80*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb);
81*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStream> stream_sb);
82*bdd1243dSDimitry Andric PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb);
83*bdd1243dSDimitry Andric 
84*bdd1243dSDimitry Andric python::ScopedPythonObject<lldb::SBCommandReturnObject>
85*bdd1243dSDimitry Andric ToSWIGWrapper(CommandReturnObject &cmd_retobj);
86*bdd1243dSDimitry Andric python::ScopedPythonObject<lldb::SBEvent> ToSWIGWrapper(Event *event);
87*bdd1243dSDimitry Andric 
88*bdd1243dSDimitry 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 
99*bdd1243dSDimitry Andric python::PythonObject LLDBSwigPythonCreateScriptedObject(
10004eeddc0SDimitry Andric     const char *python_class_name, const char *session_dictionary_name,
101*bdd1243dSDimitry Andric     lldb::ExecutionContextRefSP exe_ctx_sp,
102*bdd1243dSDimitry 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 
115*bdd1243dSDimitry Andric bool LLDBSwigPythonFormatterCallbackFunction(
116*bdd1243dSDimitry Andric     const char *python_function_name, const char *session_dictionary_name,
117*bdd1243dSDimitry Andric     lldb::TypeImplSP type_impl_sp);
118*bdd1243dSDimitry 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 
211*bdd1243dSDimitry 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 
221*bdd1243dSDimitry 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