diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2023-02-11 12:38:04 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2023-02-11 12:38:11 +0000 |
| commit | e3b557809604d036af6e00c60f012c2025b59a5e (patch) | |
| tree | 8a11ba2269a3b669601e2fd41145b174008f4da8 /lldb/bindings/python | |
| parent | 08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 (diff) | |
Diffstat (limited to 'lldb/bindings/python')
| -rw-r--r-- | lldb/bindings/python/python-swigsafecast.swig | 30 | ||||
| -rw-r--r-- | lldb/bindings/python/python-typemaps.h | 2 | ||||
| -rw-r--r-- | lldb/bindings/python/python-typemaps.swig | 56 | ||||
| -rw-r--r-- | lldb/bindings/python/python-wrapper.swig | 86 | ||||
| -rw-r--r-- | lldb/bindings/python/python.swig | 2 |
5 files changed, 73 insertions, 103 deletions
diff --git a/lldb/bindings/python/python-swigsafecast.swig b/lldb/bindings/python/python-swigsafecast.swig index eb684133abef..a1e883b3b19d 100644 --- a/lldb/bindings/python/python-swigsafecast.swig +++ b/lldb/bindings/python/python-swigsafecast.swig @@ -5,28 +5,6 @@ PythonObject ToSWIGHelper(void *obj, swig_type_info *info) { return {PyRefType::Owned, SWIG_NewPointerObj(obj, info, SWIG_POINTER_OWN)}; } -/// A class that automatically clears an SB object when it goes out of scope. -/// Use for cases where the SB object points to a temporary/unowned entity. -template <typename T> class ScopedPythonObject : PythonObject { -public: - ScopedPythonObject(T *sb, swig_type_info *info) - : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {} - ~ScopedPythonObject() { - if (m_sb) - *m_sb = T(); - } - ScopedPythonObject(ScopedPythonObject &&rhs) - : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {} - ScopedPythonObject(const ScopedPythonObject &) = delete; - ScopedPythonObject &operator=(const ScopedPythonObject &) = delete; - ScopedPythonObject &operator=(ScopedPythonObject &&) = delete; - - const PythonObject &obj() const { return *this; } - -private: - T *m_sb; -}; - PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb) { return ToSWIGHelper(value_sb.release(), SWIGTYPE_p_lldb__SBValue); } @@ -55,6 +33,10 @@ PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp) { SWIGTYPE_p_lldb__SBBreakpoint); } +PythonObject ToSWIGWrapper(const Status& status) { + return ToSWIGHelper(new lldb::SBError(status), SWIGTYPE_p_lldb__SBError); +} + PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStream> stream_sb) { return ToSWIGHelper(stream_sb.release(), SWIGTYPE_p_lldb__SBStream); } @@ -97,6 +79,10 @@ PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp) { SWIGTYPE_p_lldb__SBExecutionContext); } +PythonObject ToSWIGWrapper(lldb::TypeImplSP type_impl_sp) { + return ToSWIGHelper(new lldb::SBType(type_impl_sp), SWIGTYPE_p_lldb__SBType); +} + PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options) { return ToSWIGHelper(new lldb::SBTypeSummaryOptions(summary_options), SWIGTYPE_p_lldb__SBTypeSummaryOptions); diff --git a/lldb/bindings/python/python-typemaps.h b/lldb/bindings/python/python-typemaps.h index b45352ad6295..8a533e822988 100644 --- a/lldb/bindings/python/python-typemaps.h +++ b/lldb/bindings/python/python-typemaps.h @@ -1,6 +1,8 @@ #ifndef LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H #define LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H +#include <Python.h> + // Defined here instead of a .swig file because SWIG 2 doesn't support // explicit deleted functions. struct Py_buffer_RAII { diff --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig index bf3de66b91bf..11f68d59ae7b 100644 --- a/lldb/bindings/python/python-typemaps.swig +++ b/lldb/bindings/python/python-typemaps.swig @@ -18,7 +18,7 @@ if (!py_str.IsAllocated()) { PyErr_SetString(PyExc_TypeError, "list must contain strings"); free($1); - return nullptr; + SWIG_fail; } $1[i] = const_cast<char *>(py_str.GetString().data()); @@ -28,7 +28,7 @@ $1 = NULL; } else { PyErr_SetString(PyExc_TypeError, "not a list"); - return NULL; + SWIG_fail; } } @@ -70,7 +70,7 @@ PythonObject obj = Retain<PythonObject>($input); lldb::tid_t value = unwrapOrSetPythonException(As<unsigned long long>(obj)); if (PyErr_Occurred()) - return nullptr; + SWIG_fail; $1 = value; } @@ -79,10 +79,10 @@ unsigned long long state_type_value = unwrapOrSetPythonException(As<unsigned long long>(obj)); if (PyErr_Occurred()) - return nullptr; + SWIG_fail; if (state_type_value > lldb::StateType::kLastStateType) { PyErr_SetString(PyExc_ValueError, "Not a valid StateType value"); - return nullptr; + SWIG_fail; } $1 = static_cast<lldb::StateType>(state_type_value); } @@ -93,12 +93,12 @@ %typemap(in) (char *dst, size_t dst_len) { if (!PyInt_Check($input)) { PyErr_SetString(PyExc_ValueError, "Expecting an integer"); - return NULL; + SWIG_fail; } $2 = PyInt_AsLong($input); if ($2 <= 0) { PyErr_SetString(PyExc_ValueError, "Positive integer expected"); - return NULL; + SWIG_fail; } $1 = (char *)malloc($2); } @@ -129,12 +129,12 @@ %typemap(in) (char *dst_or_null, size_t dst_len) { if (!PyInt_Check($input)) { PyErr_SetString(PyExc_ValueError, "Expecting an integer"); - return NULL; + SWIG_fail; } $2 = PyInt_AsLong($input); if ($2 <= 0) { PyErr_SetString(PyExc_ValueError, "Positive integer expected"); - return NULL; + SWIG_fail; } $1 = (char *)malloc($2); } @@ -166,7 +166,7 @@ $2 = bytes.GetSize(); } else { PyErr_SetString(PyExc_ValueError, "Expecting a string"); - return NULL; + SWIG_fail; } } // For SBProcess::WriteMemory, SBTarget::GetInstructions and SBDebugger::DispatchInput. @@ -186,7 +186,7 @@ $2 = bytes.GetSize(); } else { PyErr_SetString(PyExc_ValueError, "Expecting a buffer"); - return NULL; + SWIG_fail; } } @@ -199,11 +199,11 @@ $2 = PyLong_AsLong($input); } else { PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object"); - return NULL; + SWIG_fail; } if ($2 <= 0) { PyErr_SetString(PyExc_ValueError, "Positive integer expected"); - return NULL; + SWIG_fail; } $1 = (void *)malloc($2); } @@ -287,12 +287,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { if (!SetNumberFromPyObject($1[i], o)) { PyErr_SetString(PyExc_TypeError, "list must contain numbers"); free($1); - return NULL; + SWIG_fail; } if (PyErr_Occurred()) { free($1); - return NULL; + SWIG_fail; } } } else if ($input == Py_None) { @@ -300,7 +300,7 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { $2 = 0; } else { PyErr_SetString(PyExc_TypeError, "not a list"); - return NULL; + SWIG_fail; } } @@ -353,7 +353,7 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { if (!($input == Py_None || PyCallable_Check(reinterpret_cast<PyObject *>($input)))) { PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); - return NULL; + SWIG_fail; } // FIXME (filcab): We can't currently check if our callback is already @@ -377,11 +377,11 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { PythonFile py_file(PyRefType::Borrowed, $input); if (!py_file) { PyErr_SetString(PyExc_TypeError, "not a file"); - return nullptr; + SWIG_fail; } auto sp = unwrapOrSetPythonException(py_file.ConvertToFile()); if (!sp) - return nullptr; + SWIG_fail; $1 = sp; } @@ -389,12 +389,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { PythonFile py_file(PyRefType::Borrowed, $input); if (!py_file) { PyErr_SetString(PyExc_TypeError, "not a file"); - return nullptr; + SWIG_fail; } auto sp = unwrapOrSetPythonException( py_file.ConvertToFileForcingUseOfScriptingIOMethods()); if (!sp) - return nullptr; + SWIG_fail; $1 = sp; } @@ -402,12 +402,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { PythonFile py_file(PyRefType::Borrowed, $input); if (!py_file) { PyErr_SetString(PyExc_TypeError, "not a file"); - return nullptr; + SWIG_fail; } auto sp = unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true)); if (!sp) - return nullptr; + SWIG_fail; $1 = sp; } @@ -415,12 +415,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { PythonFile py_file(PyRefType::Borrowed, $input); if (!py_file) { PyErr_SetString(PyExc_TypeError, "not a file"); - return nullptr; + SWIG_fail; } auto sp = unwrapOrSetPythonException( py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true)); if (!sp) - return nullptr; + SWIG_fail; $1 = sp; } @@ -435,11 +435,11 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { %typemap(out) lldb::FileSP { $result = nullptr; - lldb::FileSP &sp = $1; + const lldb::FileSP &sp = $1; if (sp) { PythonFile pyfile = unwrapOrSetPythonException(PythonFile::FromFile(*sp)); if (!pyfile.IsValid()) - return nullptr; + SWIG_fail; $result = pyfile.release(); } if (!$result) { @@ -468,7 +468,7 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { py_str.release(); } else { PyErr_SetString(PyExc_TypeError, "not a string-like object"); - return NULL; + SWIG_fail; } } diff --git a/lldb/bindings/python/python-wrapper.swig b/lldb/bindings/python/python-wrapper.swig index 626fc47bebb9..9a08c3000b79 100644 --- a/lldb/bindings/python/python-wrapper.swig +++ b/lldb/bindings/python/python-wrapper.swig @@ -90,6 +90,32 @@ bool lldb_private::LLDBSwigPythonWatchpointCallbackFunction( return stop_at_watchpoint; } +// This function is called by +// ScriptInterpreterPython::FormatterMatchingCallbackFunction and it's used when +// a data formatter provides the name of a callback to inspect a candidate type +// before considering a match. +bool lldb_private::LLDBSwigPythonFormatterCallbackFunction( + const char *python_function_name, const char *session_dictionary_name, + lldb::TypeImplSP type_impl_sp) { + + PyErr_Cleaner py_err_cleaner(true); + + auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( + python_function_name, dict); + + if (!pfunc.IsAllocated()) + return false; + + PythonObject result = + pfunc(ToSWIGWrapper(type_impl_sp), dict); + + // Only if everything goes okay and the function returns True we'll consider + // it a match. + return result.get() == Py_True; +} + bool lldb_private::LLDBSwigPythonCallTypeScript( const char *python_function_name, const void *session_dictionary, const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper, @@ -203,9 +229,9 @@ PythonObject lldb_private::LLDBSwigPythonCreateCommandObject( return pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict); } -PythonObject lldb_private::LLDBSwigPythonCreateScriptedProcess( +PythonObject lldb_private::LLDBSwigPythonCreateScriptedObject( const char *python_class_name, const char *session_dictionary_name, - const lldb::TargetSP &target_sp, + lldb::ExecutionContextRefSP exe_ctx_sp, const lldb_private::StructuredDataImpl &args_impl, std::string &error_string) { if (python_class_name == NULL || python_class_name[0] == '\0' || @@ -225,8 +251,6 @@ PythonObject lldb_private::LLDBSwigPythonCreateScriptedProcess( return PythonObject(); } - PythonObject target_arg = ToSWIGWrapper(target_sp); - llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); if (!arg_info) { llvm::handleAllErrors( @@ -240,7 +264,7 @@ PythonObject lldb_private::LLDBSwigPythonCreateScriptedProcess( PythonObject result = {}; if (arg_info.get().max_positional_args == 2) { - result = pfunc(target_arg, ToSWIGWrapper(args_impl)); + result = pfunc(ToSWIGWrapper(exe_ctx_sp), ToSWIGWrapper(args_impl)); } else { error_string.assign("wrong number of arguments in __init__, should be 2 " "(not including self)"); @@ -248,46 +272,6 @@ PythonObject lldb_private::LLDBSwigPythonCreateScriptedProcess( return result; } -PythonObject lldb_private::LLDBSwigPythonCreateScriptedThread( - const char *python_class_name, const char *session_dictionary_name, - const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl, - std::string &error_string) { - if (python_class_name == NULL || python_class_name[0] == '\0' || - !session_dictionary_name) - return PythonObject(); - - PyErr_Cleaner py_err_cleaner(true); - - auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( - session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( - python_class_name, dict); - - if (!pfunc.IsAllocated()) { - error_string.append("could not find script class: "); - error_string.append(python_class_name); - return PythonObject(); - } - - llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); - if (!arg_info) { - llvm::handleAllErrors( - arg_info.takeError(), - [&](PythonException &E) { error_string.append(E.ReadBacktrace()); }, - [&](const llvm::ErrorInfoBase &E) { - error_string.append(E.message()); - }); - return PythonObject(); - } - - if (arg_info.get().max_positional_args == 2) - return pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl)); - - error_string.assign("wrong number of arguments in __init__, should be 2 " - "(not including self)"); - return PythonObject(); -} - PythonObject lldb_private::LLDBSwigPythonCreateScriptedThreadPlan( const char *python_class_name, const char *session_dictionary_name, const lldb_private::StructuredDataImpl &args_impl, @@ -915,12 +899,12 @@ bool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess( return true; } -llvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread( +std::optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread( const char *python_function_name, const char *session_dictionary_name, lldb::ThreadSP thread) { if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) - return llvm::None; + return std::nullopt; PyErr_Cleaner py_err_cleaner(true); @@ -930,7 +914,7 @@ llvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread( python_function_name, dict); if (!pfunc.IsAllocated()) - return llvm::None; + return std::nullopt; auto result = pfunc(ToSWIGWrapper(std::move(thread)), dict); @@ -962,12 +946,12 @@ bool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget( return true; } -llvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame( +std::optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame( const char *python_function_name, const char *session_dictionary_name, lldb::StackFrameSP frame) { if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) - return llvm::None; + return std::nullopt; PyErr_Cleaner py_err_cleaner(true); @@ -977,7 +961,7 @@ llvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame( python_function_name, dict); if (!pfunc.IsAllocated()) - return llvm::None; + return std::nullopt; auto result = pfunc(ToSWIGWrapper(std::move(frame)), dict); diff --git a/lldb/bindings/python/python.swig b/lldb/bindings/python/python.swig index cb80e1be61a8..b1f6c4b9301d 100644 --- a/lldb/bindings/python/python.swig +++ b/lldb/bindings/python/python.swig @@ -84,8 +84,6 @@ void name ## _set(type *t, int index, type val) { import uuid import re import os - -import six %} // Include the version of swig that was used to generate this interface. |
