Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
39642 views
//===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H9#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H1011#include <optional>12#include <string>1314#include "lldb/Host/Config.h"1516#if LLDB_ENABLE_PYTHON1718// LLDB Python header must be included first19#include "lldb-python.h"2021#include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"22#include "lldb/lldb-forward.h"23#include "lldb/lldb-types.h"24#include "llvm/Support/Error.h"2526namespace lldb {27class SBEvent;28class SBCommandReturnObject;29class SBValue;30class SBStream;31class SBStructuredData;32class SBFileSpec;33class SBModuleSpec;34class SBStringList;35} // namespace lldb3637namespace lldb_private {38namespace python {3940typedef struct swig_type_info swig_type_info;4142python::PythonObject ToSWIGHelper(void *obj, swig_type_info *info);4344/// A class that automatically clears an SB object when it goes out of scope.45/// Use for cases where the SB object points to a temporary/unowned entity.46template <typename T> class ScopedPythonObject : PythonObject {47public:48ScopedPythonObject(T *sb, swig_type_info *info)49: PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}50~ScopedPythonObject() {51if (m_sb)52*m_sb = T();53}54ScopedPythonObject(ScopedPythonObject &&rhs)55: PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}56ScopedPythonObject(const ScopedPythonObject &) = delete;57ScopedPythonObject &operator=(const ScopedPythonObject &) = delete;58ScopedPythonObject &operator=(ScopedPythonObject &&) = delete;5960const PythonObject &obj() const { return *this; }6162private:63T *m_sb;64};6566// TODO: We may want to support other languages in the future w/ SWIG (we67// already support Lua right now, for example). We could create a generic68// SWIGBridge class and have this one specialize it, something like this:69//70// <typename T>71// class SWIGBridge {72// static T ToSWIGWrapper(...);73// };74//75// class SWIGPythonBridge : public SWIGBridge<PythonObject> {76// template<> static PythonObject ToSWIGWrapper(...);77// };78//79// And we should be able to more easily support things like Lua80class SWIGBridge {81public:82static PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb);83static PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp);84static PythonObject ToSWIGWrapper(lldb::TargetSP target_sp);85static PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp);86static PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp);87static PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp);88static PythonObject ToSWIGWrapper(const Status &status);89static PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl);90static PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp);91static PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp);92static PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp);93static PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp);94static PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp);95static PythonObject ToSWIGWrapper(lldb::TypeImplSP type_impl_sp);96static PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp);97static PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options);98static PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx);99static PythonObject ToSWIGWrapper(const Stream *stream);100static PythonObject ToSWIGWrapper(std::shared_ptr<lldb::SBStream> stream_sb);101static PythonObject ToSWIGWrapper(Event *event);102103static PythonObject ToSWIGWrapper(lldb::ProcessAttachInfoSP attach_info_sp);104static PythonObject ToSWIGWrapper(lldb::ProcessLaunchInfoSP launch_info_sp);105static PythonObject ToSWIGWrapper(lldb::DataExtractorSP data_extractor_sp);106107static PythonObject108ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb);109static PythonObject110ToSWIGWrapper(std::unique_ptr<lldb::SBFileSpec> file_spec_sb);111static PythonObject112ToSWIGWrapper(std::unique_ptr<lldb::SBModuleSpec> module_spec_sb);113114static python::ScopedPythonObject<lldb::SBCommandReturnObject>115ToSWIGWrapper(CommandReturnObject &cmd_retobj);116// These prototypes are the Pythonic implementations of the required117// callbacks. Although these are scripting-language specific, their definition118// depends on the public API.119120static llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(121const char *python_function_name, const char *session_dictionary_name,122const lldb::StackFrameSP &sb_frame,123const lldb::BreakpointLocationSP &sb_bp_loc,124const lldb_private::StructuredDataImpl &args_impl);125126static bool LLDBSwigPythonWatchpointCallbackFunction(127const char *python_function_name, const char *session_dictionary_name,128const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);129130static bool131LLDBSwigPythonFormatterCallbackFunction(const char *python_function_name,132const char *session_dictionary_name,133lldb::TypeImplSP type_impl_sp);134135static bool LLDBSwigPythonCallTypeScript(136const char *python_function_name, const void *session_dictionary,137const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,138const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval);139140static python::PythonObject141LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,142const char *session_dictionary_name,143const lldb::ValueObjectSP &valobj_sp);144145static python::PythonObject146LLDBSwigPythonCreateCommandObject(const char *python_class_name,147const char *session_dictionary_name,148lldb::DebuggerSP debugger_sp);149150static python::PythonObject LLDBSwigPythonCreateScriptedBreakpointResolver(151const char *python_class_name, const char *session_dictionary_name,152const StructuredDataImpl &args, const lldb::BreakpointSP &bkpt_sp);153154static unsigned int155LLDBSwigPythonCallBreakpointResolver(void *implementor,156const char *method_name,157lldb_private::SymbolContext *sym_ctx);158159static python::PythonObject LLDBSwigPythonCreateScriptedStopHook(160lldb::TargetSP target_sp, const char *python_class_name,161const char *session_dictionary_name, const StructuredDataImpl &args,162lldb_private::Status &error);163164static bool165LLDBSwigPythonStopHookCallHandleStop(void *implementor,166lldb::ExecutionContextRefSP exc_ctx,167lldb::StreamSP stream);168169static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor,170uint32_t max);171172static PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor,173uint32_t idx);174175static int LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor,176const char *child_name);177178static lldb::ValueObjectSP179LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data);180181static bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor);182183static bool184LLDBSwigPython_MightHaveChildrenSynthProviderInstance(PyObject *implementor);185186static PyObject *187LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor);188189static bool190LLDBSwigPythonCallCommand(const char *python_function_name,191const char *session_dictionary_name,192lldb::DebuggerSP debugger, const char *args,193lldb_private::CommandReturnObject &cmd_retobj,194lldb::ExecutionContextRefSP exe_ctx_ref_sp);195196static bool197LLDBSwigPythonCallCommandObject(PyObject *implementor,198lldb::DebuggerSP debugger, const char *args,199lldb_private::CommandReturnObject &cmd_retobj,200lldb::ExecutionContextRefSP exe_ctx_ref_sp);201static bool202LLDBSwigPythonCallParsedCommandObject(PyObject *implementor,203lldb::DebuggerSP debugger,204StructuredDataImpl &args_impl,205lldb_private::CommandReturnObject &cmd_retobj,206lldb::ExecutionContextRefSP exe_ctx_ref_sp);207208static std::optional<std::string>209LLDBSwigPythonGetRepeatCommandForScriptedCommand(PyObject *implementor,210std::string &command);211212static bool LLDBSwigPythonCallModuleInit(const char *python_module_name,213const char *session_dictionary_name,214lldb::DebuggerSP debugger);215216static python::PythonObject217LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,218const char *session_dictionary_name,219const lldb::ProcessSP &process_sp);220221static python::PythonObject222LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,223const char *session_dictionary_name);224225static PyObject *226LLDBSwigPython_GetRecognizedArguments(PyObject *implementor,227const lldb::StackFrameSP &frame_sp);228229static bool LLDBSWIGPythonRunScriptKeywordProcess(230const char *python_function_name, const char *session_dictionary_name,231const lldb::ProcessSP &process, std::string &output);232233static std::optional<std::string>234LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,235const char *session_dictionary_name,236lldb::ThreadSP thread);237238static bool LLDBSWIGPythonRunScriptKeywordTarget(239const char *python_function_name, const char *session_dictionary_name,240const lldb::TargetSP &target, std::string &output);241242static std::optional<std::string>243LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,244const char *session_dictionary_name,245lldb::StackFrameSP frame);246247static bool LLDBSWIGPythonRunScriptKeywordValue(248const char *python_function_name, const char *session_dictionary_name,249const lldb::ValueObjectSP &value, std::string &output);250251static void *252LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,253const lldb::TargetSP &target_sp);254};255256void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data);257void *LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject *data);258void *LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject *data);259void *LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject *data);260void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data);261void *LLDBSWIGPython_CastPyObjectToSBEvent(PyObject *data);262void *LLDBSWIGPython_CastPyObjectToSBStream(PyObject *data);263void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data);264void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data);265} // namespace python266267} // namespace lldb_private268269#endif // LLDB_ENABLE_PYTHON270#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H271272273