1 //===-- ScriptInterpreterPython.h -------------------------------*- 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 #ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
10 #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
11 
12 #include <string>
13 
14 #include "lldb/Host/Config.h"
15 
16 #if LLDB_ENABLE_PYTHON
17 
18 // LLDB Python header must be included first
19 #include "lldb-python.h"
20 
21 #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"
22 #include "lldb/lldb-forward.h"
23 #include "lldb/lldb-types.h"
24 #include "llvm/Support/Error.h"
25 
26 namespace lldb_private {
27 
28 // GetPythonValueFormatString provides a system independent type safe way to
29 // convert a variable's type into a python value format. Python value formats
30 // are defined in terms of builtin C types and could change from system to as
31 // the underlying typedef for uint* types, size_t, off_t and other values
32 // change.
33 
34 template <typename T> const char *GetPythonValueFormatString(T t);
35 template <> const char *GetPythonValueFormatString(char *);
36 template <> const char *GetPythonValueFormatString(char);
37 template <> const char *GetPythonValueFormatString(unsigned char);
38 template <> const char *GetPythonValueFormatString(short);
39 template <> const char *GetPythonValueFormatString(unsigned short);
40 template <> const char *GetPythonValueFormatString(int);
41 template <> const char *GetPythonValueFormatString(unsigned int);
42 template <> const char *GetPythonValueFormatString(long);
43 template <> const char *GetPythonValueFormatString(unsigned long);
44 template <> const char *GetPythonValueFormatString(long long);
45 template <> const char *GetPythonValueFormatString(unsigned long long);
46 template <> const char *GetPythonValueFormatString(float t);
47 template <> const char *GetPythonValueFormatString(double t);
48 
49 void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data);
50 void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data);
51 void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data);
52 void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data);
53 
54 // These prototypes are the Pythonic implementations of the required callbacks.
55 // Although these are scripting-language specific, their definition depends on
56 // the public API.
57 
58 python::PythonObject LLDBSwigPythonCreateScriptedProcess(
59     const char *python_class_name, const char *session_dictionary_name,
60     const lldb::TargetSP &target_sp, const StructuredDataImpl &args_impl,
61     std::string &error_string);
62 
63 python::PythonObject LLDBSwigPythonCreateScriptedThread(
64     const char *python_class_name, const char *session_dictionary_name,
65     const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
66     std::string &error_string);
67 
68 llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
69     const char *python_function_name, const char *session_dictionary_name,
70     const lldb::StackFrameSP &sb_frame,
71     const lldb::BreakpointLocationSP &sb_bp_loc,
72     const lldb_private::StructuredDataImpl &args_impl);
73 
74 bool LLDBSwigPythonWatchpointCallbackFunction(
75     const char *python_function_name, const char *session_dictionary_name,
76     const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);
77 
78 bool LLDBSwigPythonCallTypeScript(const char *python_function_name,
79                                   const void *session_dictionary,
80                                   const lldb::ValueObjectSP &valobj_sp,
81                                   void **pyfunct_wrapper,
82                                   const lldb::TypeSummaryOptionsSP &options_sp,
83                                   std::string &retval);
84 
85 python::PythonObject
86 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
87                                       const char *session_dictionary_name,
88                                       const lldb::ValueObjectSP &valobj_sp);
89 
90 python::PythonObject
91 LLDBSwigPythonCreateCommandObject(const char *python_class_name,
92                                   const char *session_dictionary_name,
93                                   lldb::DebuggerSP debugger_sp);
94 
95 python::PythonObject LLDBSwigPythonCreateScriptedThreadPlan(
96     const char *python_class_name, const char *session_dictionary_name,
97     const StructuredDataImpl &args_data, std::string &error_string,
98     const lldb::ThreadPlanSP &thread_plan_sp);
99 
100 bool LLDBSWIGPythonCallThreadPlan(void *implementor, const char *method_name,
101                                   lldb_private::Event *event_sp,
102                                   bool &got_error);
103 
104 python::PythonObject LLDBSwigPythonCreateScriptedBreakpointResolver(
105     const char *python_class_name, const char *session_dictionary_name,
106     const StructuredDataImpl &args, const lldb::BreakpointSP &bkpt_sp);
107 
108 unsigned int
109 LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
110                                      lldb_private::SymbolContext *sym_ctx);
111 
112 python::PythonObject LLDBSwigPythonCreateScriptedStopHook(
113     lldb::TargetSP target_sp, const char *python_class_name,
114     const char *session_dictionary_name, const StructuredDataImpl &args,
115     lldb_private::Status &error);
116 
117 bool LLDBSwigPythonStopHookCallHandleStop(void *implementor,
118                                           lldb::ExecutionContextRefSP exc_ctx,
119                                           lldb::StreamSP stream);
120 
121 size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor, uint32_t max);
122 
123 PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor, uint32_t idx);
124 
125 int LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor,
126                                            const char *child_name);
127 
128 lldb::ValueObjectSP LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data);
129 
130 bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor);
131 
132 bool LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
133     PyObject *implementor);
134 
135 PyObject *LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor);
136 
137 bool LLDBSwigPythonCallCommand(const char *python_function_name,
138                                const char *session_dictionary_name,
139                                lldb::DebuggerSP debugger, const char *args,
140                                lldb_private::CommandReturnObject &cmd_retobj,
141                                lldb::ExecutionContextRefSP exe_ctx_ref_sp);
142 
143 bool LLDBSwigPythonCallCommandObject(
144     PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
145     lldb_private::CommandReturnObject &cmd_retobj,
146     lldb::ExecutionContextRefSP exe_ctx_ref_sp);
147 
148 bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
149                                   const char *session_dictionary_name,
150                                   lldb::DebuggerSP debugger);
151 
152 python::PythonObject
153 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
154                              const char *session_dictionary_name,
155                              const lldb::ProcessSP &process_sp);
156 
157 python::PythonObject
158 LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
159                                      const char *session_dictionary_name);
160 
161 PyObject *
162 LLDBSwigPython_GetRecognizedArguments(PyObject *implementor,
163                                       const lldb::StackFrameSP &frame_sp);
164 
165 bool LLDBSWIGPythonRunScriptKeywordProcess(const char *python_function_name,
166                                            const char *session_dictionary_name,
167                                            const lldb::ProcessSP &process,
168                                            std::string &output);
169 
170 llvm::Optional<std::string>
171 LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
172                                      const char *session_dictionary_name,
173                                      lldb::ThreadSP thread);
174 
175 bool LLDBSWIGPythonRunScriptKeywordTarget(const char *python_function_name,
176                                           const char *session_dictionary_name,
177                                           const lldb::TargetSP &target,
178                                           std::string &output);
179 
180 llvm::Optional<std::string>
181 LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
182                                     const char *session_dictionary_name,
183                                     lldb::StackFrameSP frame);
184 
185 bool LLDBSWIGPythonRunScriptKeywordValue(const char *python_function_name,
186                                          const char *session_dictionary_name,
187                                          const lldb::ValueObjectSP &value,
188                                          std::string &output);
189 
190 void *LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
191                                        const lldb::TargetSP &target_sp);
192 
193 } // namespace lldb_private
194 
195 #endif // LLDB_ENABLE_PYTHON
196 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
197