diff options
Diffstat (limited to 'contrib/llvm-project/lldb/bindings')
11 files changed, 430 insertions, 172 deletions
diff --git a/contrib/llvm-project/lldb/bindings/interface/SBDebugger.i b/contrib/llvm-project/lldb/bindings/interface/SBDebugger.i index cf4411980cc3..aae72dd51394 100644 --- a/contrib/llvm-project/lldb/bindings/interface/SBDebugger.i +++ b/contrib/llvm-project/lldb/bindings/interface/SBDebugger.i @@ -479,6 +479,8 @@ public: lldb::SBTypeSynthetic GetSyntheticForType (lldb::SBTypeNameSpecifier); + SBStructuredData GetScriptInterpreterInfo(ScriptLanguage); + STRING_EXTENSION(SBDebugger) %feature("docstring", diff --git a/contrib/llvm-project/lldb/bindings/interface/SBMemoryRegionInfo.i b/contrib/llvm-project/lldb/bindings/interface/SBMemoryRegionInfo.i index 3460dc0d06e2..0316c5a5be1d 100644 --- a/contrib/llvm-project/lldb/bindings/interface/SBMemoryRegionInfo.i +++ b/contrib/llvm-project/lldb/bindings/interface/SBMemoryRegionInfo.i @@ -20,6 +20,9 @@ public: SBMemoryRegionInfo (const lldb::SBMemoryRegionInfo &rhs); + SBMemoryRegionInfo::SBMemoryRegionInfo(const char *name, lldb::addr_t begin, + lldb::addr_t end, uint32_t permissions, bool mapped, bool stack_memory); + ~SBMemoryRegionInfo (); void diff --git a/contrib/llvm-project/lldb/bindings/interface/SBMemoryRegionInfoList.i b/contrib/llvm-project/lldb/bindings/interface/SBMemoryRegionInfoList.i index c2e74f1cd0dc..009751277542 100644 --- a/contrib/llvm-project/lldb/bindings/interface/SBMemoryRegionInfoList.i +++ b/contrib/llvm-project/lldb/bindings/interface/SBMemoryRegionInfoList.i @@ -25,6 +25,9 @@ public: GetSize () const; bool + GetMemoryRegionContainingAddress (lldb::addr_t addr, SBMemoryRegionInfo ®ion_info); + + bool GetMemoryRegionAtIndex (uint32_t idx, SBMemoryRegionInfo ®ion_info); void diff --git a/contrib/llvm-project/lldb/bindings/interface/SBSymbolContextList.i b/contrib/llvm-project/lldb/bindings/interface/SBSymbolContextList.i index e9d4aa8d62db..14566b3e3720 100644 --- a/contrib/llvm-project/lldb/bindings/interface/SBSymbolContextList.i +++ b/contrib/llvm-project/lldb/bindings/interface/SBSymbolContextList.i @@ -14,7 +14,7 @@ namespace lldb { For example (from test/python_api/target/TestTargetAPI.py), :: def find_functions(self, exe_name): - '''Exercise SBTaget.FindFunctions() API.''' + '''Exercise SBTarget.FindFunctions() API.''' exe = os.path.join(os.getcwd(), exe_name) # Create a target by the debugger. diff --git a/contrib/llvm-project/lldb/bindings/interface/SBType.i b/contrib/llvm-project/lldb/bindings/interface/SBType.i index 500bc99ca8cd..d6e8db3ab428 100644 --- a/contrib/llvm-project/lldb/bindings/interface/SBType.i +++ b/contrib/llvm-project/lldb/bindings/interface/SBType.i @@ -837,6 +837,21 @@ public: lldb::SBTypeMemberFunction GetMemberFunctionAtIndex (uint32_t idx); + %feature("docstring", + "Returns true if the type is completely defined. + + Language-specific behaviour: + + * C: Returns false for struct types that were only forward declared in the + type's `SBTarget`/`SBModule`. Otherwise returns true. + * C++: Returns false for template/non-template struct/class types and + scoped enums that were only forward declared inside the type's + `SBTarget`/`SBModule`. Otherwise returns true. + * Objective-C: Follows the same behavior as C for struct types. Objective-C + classes are considered complete unless they were only forward declared via + ``@class ClassName`` in the type's `SBTarget`/`SBModule`. Otherwise + returns true. + ") IsTypeComplete; bool IsTypeComplete (); diff --git a/contrib/llvm-project/lldb/bindings/lua/lua-typemaps.swig b/contrib/llvm-project/lldb/bindings/lua/lua-typemaps.swig index d912137a5674..e3b3f5718d15 100644 --- a/contrib/llvm-project/lldb/bindings/lua/lua-typemaps.swig +++ b/contrib/llvm-project/lldb/bindings/lua/lua-typemaps.swig @@ -12,7 +12,7 @@ // Primitive integer mapping %typemap(in,checkfn="lua_isinteger") TYPE -%{ $1 = (TYPE)lua_tointeger(L, $input); %} +%{ $1 = ($type)lua_tointeger(L, $input); %} %typemap(in,checkfn="lua_isinteger") const TYPE&($basetype temp) %{ temp=($basetype)lua_tointeger(L,$input); $1=&temp;%} %typemap(out) TYPE @@ -54,6 +54,7 @@ LLDB_NUMBER_TYPEMAP(signed long); LLDB_NUMBER_TYPEMAP(long long); LLDB_NUMBER_TYPEMAP(unsigned long long); LLDB_NUMBER_TYPEMAP(signed long long); +LLDB_NUMBER_TYPEMAP(enum SWIGTYPE); %apply unsigned long { size_t }; %apply const unsigned long & { const size_t & }; @@ -77,7 +78,7 @@ LLDB_NUMBER_TYPEMAP(signed long long); %typemap(in) (char *dst, size_t dst_len) { $2 = luaL_checkinteger(L, $input); if ($2 <= 0) { - return luaL_error(L, "Positive integer expected"); + return luaL_error(L, "Positive integer expected"); } $1 = (char *) malloc($2); } @@ -86,6 +87,9 @@ LLDB_NUMBER_TYPEMAP(signed long long); // as char data instead of byte data. %typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len); +// Also SBProcess::ReadMemory. +%typemap(in) (void *buf, size_t size) = (char *dst, size_t dst_len); + // Return the char buffer. Discarding any previous return result %typemap(argout) (char *dst, size_t dst_len) { lua_pop(L, 1); // Blow away the previous result @@ -102,4 +106,211 @@ LLDB_NUMBER_TYPEMAP(signed long long); // as char data instead of byte data. %typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len); +// Also SBProcess::ReadMemory. +%typemap(argout) (void *buf, size_t size) = (char *dst, size_t dst_len); + +//===----------------------------------------------------------------------===// + +// Typemap for handling a snprintf-like API like SBThread::GetStopDescription. + +%typemap(in) (char *dst_or_null, size_t dst_len) { + $2 = luaL_checkinteger(L, $input); + if ($2 <= 0) { + return luaL_error(L, "Positive integer expected"); + } + $1 = (char *)malloc($2); +} + +%typemap(argout) (char *dst_or_null, size_t dst_len) { + lua_pop(L, 1); // Blow away the previous result + lua_pushlstring(L, (const char *)$1, $result); + free($1); + // SWIG_arg was already incremented +} + +//===----------------------------------------------------------------------===// + +// Typemap for handling SBModule::GetVersion + +%typemap(in) (uint32_t *versions, uint32_t num_versions) { + $2 = 99; + $1 = (uint32_t *)malloc(sizeof(uint32_t) * $2); +} + +%typemap(argout) (uint32_t *versions, uint32_t num_versions) { + uint32_t count = result; + if (count >= $2) + count = $2; + lua_newtable(L); + int i = 0; + while (i++ < count) { + lua_pushinteger(L, $1[i - 1]); + lua_seti(L, -2, i); + } + SWIG_arg++; + free($1); +} + +//===----------------------------------------------------------------------===// + +// Typemap for handling SBDebugger::SetLoggingCallback + +%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) { + $1 = LLDBSwigLuaCallLuaLogOutputCallback; + $2 = (void *)L; + + luaL_checktype(L, 2, LUA_TFUNCTION); + lua_settop(L, 2); + + lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback); + lua_insert(L, 2); + lua_settable(L, LUA_REGISTRYINDEX); +} + +//===----------------------------------------------------------------------===// + +// Typemap for handling SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len) + +%typemap(in) (const char *cstr, uint32_t cstr_len) { + $1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2); +} + +// Typemap for handling SBProcess::PutSTDIN + +%typemap(in) (const char *src, size_t src_len) { + $1 = (char *)luaL_checklstring(L, $input, &$2); +} + +// Typemap for handling SBProcess::WriteMemory, SBTarget::GetInstructions... + +%typemap(in) (const void *buf, size_t size), + (const void *data, size_t data_len) { + $1 = (void *)luaL_checklstring(L, $input, &$2); +} + +//===----------------------------------------------------------------------===// + +// Typemap for handling char ** in SBTarget::LaunchSimple, SBTarget::Launch... + +// It should accept a Lua table of strings, for stuff like "argv" and "envp". + +%typemap(in) char ** { + if (lua_istable(L, $input)) { + size_t size = lua_rawlen(L, $input); + $1 = (char **)malloc((size + 1) * sizeof(char *)); + int i = 0, j = 0; + while (i++ < size) { + lua_rawgeti(L, $input, i); + if (!lua_isstring(L, -1)) { + // if current element cannot be converted to string, raise an error + lua_pop(L, 1); + return luaL_error(L, "List should only contain strings"); + } + $1[j++] = (char *)lua_tostring(L, -1); + lua_pop(L, 1); + } + $1[j] = 0; + } else if (lua_isnil(L, $input)) { + // "nil" is also acceptable, equivalent as an empty table + $1 = NULL; + } else { + return luaL_error(L, "A list of strings expected"); + } +} + +%typemap(freearg) char ** { + free((char *) $1); +} + +%typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** { + $1 = (lua_istable(L, $input) || lua_isnil(L, $input)); +} + +//===----------------------------------------------------------------------===// + +// Typemap for file handles (e.g. used in SBDebugger::SetOutputFile) + +%typemap(in) lldb::FileSP { + luaL_Stream *p = (luaL_Stream *)luaL_checkudata(L, $input, LUA_FILEHANDLE); + lldb::FileSP file_sp; + file_sp = std::make_shared<lldb_private::NativeFile>(p->f, false); + if (!file_sp->IsValid()) + return luaL_error(L, "Invalid file"); + $1 = file_sp; +} + +%typecheck(SWIG_TYPECHECK_POINTER) lldb::FileSP { + $1 = (lua_isuserdata(L, $input)) && + (luaL_testudata(L, $input, LUA_FILEHANDLE) != nullptr); +} + +// Typemap for file handles (e.g. used in SBDebugger::GetOutputFileHandle) + +%typemap(out) lldb::FileSP { + lldb::FileSP &sp = $1; + if (sp && sp->IsValid()) { + luaL_Stream *p = (luaL_Stream *)lua_newuserdata(L, sizeof(luaL_Stream)); + p->closef = &LLDBSwigLuaCloseFileHandle; + p->f = sp->GetStream(); + luaL_setmetatable(L, LUA_FILEHANDLE); + SWIG_arg++; + } +} + +//===----------------------------------------------------------------------===// + +// Typemap for SBData::CreateDataFromUInt64Array, SBData::SetDataFromUInt64Array ... + +%typemap(in) (uint64_t* array, size_t array_len), + (uint32_t* array, size_t array_len), + (int64_t* array, size_t array_len), + (int32_t* array, size_t array_len), + (double* array, size_t array_len) { + if (lua_istable(L, $input)) { + // It should accept a table of numbers. + $2 = lua_rawlen(L, $input); + $1 = ($1_ltype)malloc(($2) * sizeof($*1_type)); + int i = 0, j = 0; + while (i++ < $2) { + lua_rawgeti(L, $input, i); + if (!lua_isnumber(L, -1)) { + // if current element cannot be converted to number, raise an error + lua_pop(L, 1); + return luaL_error(L, "List should only contain numbers"); + } + $1[j++] = ($*1_ltype)lua_tonumber(L, -1); + lua_pop(L, 1); + } + } else if (lua_isnil(L, $input)) { + // "nil" is also acceptable, equivalent as an empty table + $1 = NULL; + $2 = 0; + } else { + // else raise an error + return luaL_error(L, "A list of numbers expected."); + } +} + +%typemap(freearg) (uint64_t* array, size_t array_len), + (uint32_t* array, size_t array_len), + (int64_t* array, size_t array_len), + (int32_t* array, size_t array_len), + (double* array, size_t array_len) { + free($1); +} + +//===----------------------------------------------------------------------===// + +// Typemap for SBCommandReturnObject::PutCString + +%typemap(in) (const char *string, int len) { + if (lua_isnil(L, $input)) { + $1 = NULL; + $2 = 0; + } + else { + $1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2); + } +} + //===----------------------------------------------------------------------===// diff --git a/contrib/llvm-project/lldb/bindings/lua/lua-wrapper.swig b/contrib/llvm-project/lldb/bindings/lua/lua-wrapper.swig index e070bae23683..c51911bb6bf7 100644 --- a/contrib/llvm-project/lldb/bindings/lua/lua-wrapper.swig +++ b/contrib/llvm-project/lldb/bindings/lua/lua-wrapper.swig @@ -6,6 +6,19 @@ PushSBClass(lua_State* L, T* obj); %} +%runtime %{ +#ifdef __cplusplus +extern "C" { +#endif + +void LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton); +int LLDBSwigLuaCloseFileHandle(lua_State *L); + +#ifdef __cplusplus +} +#endif +%} + %wrapper %{ // This function is called from Lua::CallBreakpointCallback @@ -88,5 +101,20 @@ LLDBSwigLuaWatchpointCallbackFunction return stop; } +SWIGEXPORT void +LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) { + lua_State *L = (lua_State *)baton; + + lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback); + lua_gettable(L, LUA_REGISTRYINDEX); + + // FIXME: There's no way to report errors back to the user + lua_pushstring(L, str); + lua_pcall(L, 1, 0, 0); +} + +int LLDBSwigLuaCloseFileHandle(lua_State *L) { + return luaL_error(L, "You cannot close a file handle used by lldb."); +} %} diff --git a/contrib/llvm-project/lldb/bindings/lua/lua.swig b/contrib/llvm-project/lldb/bindings/lua/lua.swig index c702e4964081..21fa44c8b4d8 100644 --- a/contrib/llvm-project/lldb/bindings/lua/lua.swig +++ b/contrib/llvm-project/lldb/bindings/lua/lua.swig @@ -17,6 +17,10 @@ #include "llvm/Support/Error.h" #include "llvm/Support/FormatVariadic.h" #include "../bindings/lua/lua-swigsafecast.swig" + +// required headers for typemaps +#include "lldb/Host/File.h" + using namespace lldb_private; using namespace lldb; %} diff --git a/contrib/llvm-project/lldb/bindings/python/lldb-python b/contrib/llvm-project/lldb/bindings/python/lldb-python new file mode 100755 index 000000000000..3bb3b332d852 --- /dev/null +++ b/contrib/llvm-project/lldb/bindings/python/lldb-python @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +import subprocess +import os +import sys +import json + +lldb = os.path.join(os.path.dirname(__file__), 'lldb') + +info_json = subprocess.run([lldb, "-l", "python", "-print-script-interpreter-info"], + check=True, stdout=subprocess.PIPE, encoding='utf8').stdout +info = json.loads(info_json) + +os.environ["PYTHONPATH"] = ( + info["lldb-pythonpath"] + os.path.pathsep + os.environ.get("PYTHONPATH", "")) + +os.execl(info["executable"], info["executable"], *sys.argv[1:]) diff --git a/contrib/llvm-project/lldb/bindings/python/python-swigsafecast.swig b/contrib/llvm-project/lldb/bindings/python/python-swigsafecast.swig index 091fc29b1057..aa2bcfb8c8ae 100644 --- a/contrib/llvm-project/lldb/bindings/python/python-swigsafecast.swig +++ b/contrib/llvm-project/lldb/bindings/python/python-swigsafecast.swig @@ -1,161 +1,72 @@ -// leaving this undefined ensures we will get a linker error if we try to use SBTypeToSWIGWrapper() -// for a type for which we did not specialze this function -template <typename SBClass> -PyObject* -SBTypeToSWIGWrapper (SBClass* sb_object); - -template <typename SBClass> -PyObject* -SBTypeToSWIGWrapper (SBClass& sb_object) -{ - return SBTypeToSWIGWrapper(&sb_object); -} - -template <typename SBClass> -PyObject* -SBTypeToSWIGWrapper (const SBClass& sb_object) -{ - return SBTypeToSWIGWrapper(&sb_object); -} - -template <> -PyObject* -SBTypeToSWIGWrapper (PyObject* py_object) -{ - return py_object; -} - -template <> -PyObject* -SBTypeToSWIGWrapper (unsigned int* c_int) -{ - if (!c_int) - return NULL; - return PyInt_FromLong(*c_int); -} - -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBEvent* event_sb) -{ - return SWIG_NewPointerObj((void *) event_sb, SWIGTYPE_p_lldb__SBEvent, 0); -} - -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBProcess* process_sb) -{ - return SWIG_NewPointerObj((void *) process_sb, SWIGTYPE_p_lldb__SBProcess, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBEvent &event_sb) { + return SWIG_NewPointerObj(&event_sb, SWIGTYPE_p_lldb__SBEvent, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBThread* thread_sb) -{ - return SWIG_NewPointerObj((void *) thread_sb, SWIGTYPE_p_lldb__SBThread, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBProcess &process_sb) { + return SWIG_NewPointerObj(&process_sb, SWIGTYPE_p_lldb__SBProcess, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBThreadPlan* thread_plan_sb) -{ - return SWIG_NewPointerObj((void *) thread_plan_sb, SWIGTYPE_p_lldb__SBThreadPlan, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBThread &thread_sb) { + return SWIG_NewPointerObj(&thread_sb, SWIGTYPE_p_lldb__SBThread, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBTarget* target_sb) -{ - return SWIG_NewPointerObj((void *) target_sb, SWIGTYPE_p_lldb__SBTarget, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBThreadPlan &thread_plan_sb) { + return SWIG_NewPointerObj(&thread_plan_sb, SWIGTYPE_p_lldb__SBThreadPlan, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBFrame* frame_sb) -{ - return SWIG_NewPointerObj((void *) frame_sb, SWIGTYPE_p_lldb__SBFrame, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBTarget &target_sb) { + return SWIG_NewPointerObj(&target_sb, SWIGTYPE_p_lldb__SBTarget, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBDebugger* debugger_sb) -{ - return SWIG_NewPointerObj((void *) debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBFrame &frame_sb) { + return SWIG_NewPointerObj(&frame_sb, SWIGTYPE_p_lldb__SBFrame, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBBreakpoint* breakpoint_sb) -{ - return SWIG_NewPointerObj((void *) breakpoint_sb, SWIGTYPE_p_lldb__SBBreakpoint, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBDebugger &debugger_sb) { + return SWIG_NewPointerObj(&debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBWatchpoint* watchpoint_sb) -{ - return SWIG_NewPointerObj((void *) watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBBreakpoint &breakpoint_sb) { + return SWIG_NewPointerObj(&breakpoint_sb, SWIGTYPE_p_lldb__SBBreakpoint, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBBreakpointLocation* breakpoint_location_sb) -{ - return SWIG_NewPointerObj((void *) breakpoint_location_sb, SWIGTYPE_p_lldb__SBBreakpointLocation, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBWatchpoint &watchpoint_sb) { + return SWIG_NewPointerObj(&watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBBreakpointName* breakpoint_name_sb) -{ - return SWIG_NewPointerObj((void *) breakpoint_name_sb, SWIGTYPE_p_lldb__SBBreakpointName, 0); +PyObject * +SBTypeToSWIGWrapper(lldb::SBBreakpointLocation &breakpoint_location_sb) { + return SWIG_NewPointerObj(&breakpoint_location_sb, + SWIGTYPE_p_lldb__SBBreakpointLocation, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBValue* value_sb) -{ - return SWIG_NewPointerObj((void *) value_sb, SWIGTYPE_p_lldb__SBValue, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBValue &value_sb) { + return SWIG_NewPointerObj(&value_sb, SWIGTYPE_p_lldb__SBValue, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBCommandReturnObject* cmd_ret_obj_sb) -{ - return SWIG_NewPointerObj((void *) cmd_ret_obj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBCommandReturnObject &cmd_ret_obj_sb) { + return SWIG_NewPointerObj(&cmd_ret_obj_sb, + SWIGTYPE_p_lldb__SBCommandReturnObject, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBExecutionContext* ctx_sb) -{ - return SWIG_NewPointerObj((void *) ctx_sb, SWIGTYPE_p_lldb__SBExecutionContext, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBExecutionContext &ctx_sb) { + return SWIG_NewPointerObj(&ctx_sb, SWIGTYPE_p_lldb__SBExecutionContext, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBTypeSummaryOptions* summary_options_sb) -{ - return SWIG_NewPointerObj((void *) summary_options_sb, SWIGTYPE_p_lldb__SBTypeSummaryOptions, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBTypeSummaryOptions &summary_options_sb) { + return SWIG_NewPointerObj(&summary_options_sb, + SWIGTYPE_p_lldb__SBTypeSummaryOptions, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBStructuredData* structured_data_sb) -{ - return SWIG_NewPointerObj((void *) structured_data_sb, SWIGTYPE_p_lldb__SBStructuredData, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBStructuredData &structured_data_sb) { + return SWIG_NewPointerObj(&structured_data_sb, + SWIGTYPE_p_lldb__SBStructuredData, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBSymbolContext* sym_ctx_sb) -{ - return SWIG_NewPointerObj((void *) sym_ctx_sb, SWIGTYPE_p_lldb__SBSymbolContext, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBSymbolContext &sym_ctx_sb) { + return SWIG_NewPointerObj(&sym_ctx_sb, SWIGTYPE_p_lldb__SBSymbolContext, 0); } -template <> -PyObject* -SBTypeToSWIGWrapper (lldb::SBStream* stream_sb) -{ - return SWIG_NewPointerObj((void *) stream_sb, SWIGTYPE_p_lldb__SBStream, 0); +PyObject *SBTypeToSWIGWrapper(lldb::SBStream &stream_sb) { + return SWIG_NewPointerObj(&stream_sb, SWIGTYPE_p_lldb__SBStream, 0); } diff --git a/contrib/llvm-project/lldb/bindings/python/python-wrapper.swig b/contrib/llvm-project/lldb/bindings/python/python-wrapper.swig index 4c39e9c2c776..6dc8ca170390 100644 --- a/contrib/llvm-project/lldb/bindings/python/python-wrapper.swig +++ b/contrib/llvm-project/lldb/bindings/python/python-wrapper.swig @@ -1,9 +1,5 @@ %header %{ -template <typename T> -PyObject * -SBTypeToSWIGWrapper (T* item); - class PyErr_Cleaner { public: @@ -83,8 +79,9 @@ LLDBSwigPythonBreakpointCallbackFunction if (max_positional_args < 4) { return pfunc.Call(frame_arg, bp_loc_arg, dict); } else { + // FIXME: SBStructuredData leaked here lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); - PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value)); + PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*args_value)); return pfunc.Call(frame_arg, bp_loc_arg, args_arg, dict); } } (); @@ -230,12 +227,11 @@ LLDBSwigPythonCreateSyntheticProvider if (!pfunc.IsAllocated()) Py_RETURN_NONE; - // I do not want the SBValue to be deallocated when going out of scope because python - // has ownership of it and will manage memory for this object by itself + // FIXME: SBValue leaked here lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp); sb_value->SetPreferSyntheticValue(false); - PythonObject val_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value)); + PythonObject val_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*sb_value)); if (!val_arg.IsAllocated()) Py_RETURN_NONE; @@ -288,7 +284,6 @@ LLDBSwigPythonCreateScriptedProcess if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) Py_RETURN_NONE; - PyErr_Cleaner py_err_cleaner(true); auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name); @@ -300,10 +295,9 @@ LLDBSwigPythonCreateScriptedProcess return nullptr; } - // I do not want the SBTarget to be deallocated when going out of scope - // because python has ownership of it and will manage memory for this - // object by itself - PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBTarget(target_sp))); + // FIXME: SBTarget leaked here + PythonObject target_arg( + PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBTarget(target_sp))); if (!target_arg.IsAllocated()) Py_RETURN_NONE; @@ -323,16 +317,71 @@ LLDBSwigPythonCreateScriptedProcess PythonObject result = {}; if (arg_info.get().max_positional_args == 2) { - if (args_impl != nullptr) { - error_string.assign("args passed, but __init__ does not take an args dictionary"); - Py_RETURN_NONE; - } - result = pfunc(target_arg, dict); - } else if (arg_info.get().max_positional_args >= 3) { - PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl))); - result = pfunc(target_arg, args_arg, dict); + // FIXME: SBStructuredData leaked here + PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl))); + result = pfunc(target_arg, args_arg); } else { - error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)"); + error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)"); + Py_RETURN_NONE; + } + + if (result.IsAllocated()) + return result.release(); + Py_RETURN_NONE; +} + +SWIGEXPORT void* +LLDBSwigPythonCreateScriptedThread +( + const char *python_class_name, + const char *session_dictionary_name, + const lldb::ProcessSP& process_sp, + lldb_private::StructuredDataImpl *args_impl, + std::string &error_string +) +{ + if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) + Py_RETURN_NONE; + + 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 nullptr; + } + + // FIXME: This leaks the SBProcess object + PythonObject process_arg( + PyRefType::Owned, + SBTypeToSWIGWrapper(*new lldb::SBProcess(process_sp))); + + if (!process_arg.IsAllocated()) + Py_RETURN_NONE; + + 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()); + }); + Py_RETURN_NONE; + } + + PythonObject result = {}; + if (arg_info.get().max_positional_args == 2) { + // FIXME: SBStructuredData leaked here + PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl))); + result = pfunc(process_arg, args_arg); + } else { + error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)"); Py_RETURN_NONE; } @@ -366,10 +415,10 @@ LLDBSwigPythonCreateScriptedThreadPlan return nullptr; } - // I do not want the SBThreadPlan to be deallocated when going out of scope - // because python has ownership of it and will manage memory for this - // object by itself - PythonObject tp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBThreadPlan(thread_plan_sp))); + // FIXME: SBThreadPlan leaked here + PythonObject tp_arg( + PyRefType::Owned, + SBTypeToSWIGWrapper(*new lldb::SBThreadPlan(thread_plan_sp))); if (!tp_arg.IsAllocated()) Py_RETURN_NONE; @@ -395,7 +444,8 @@ LLDBSwigPythonCreateScriptedThreadPlan } result = pfunc(tp_arg, dict); } else if (arg_info.get().max_positional_args >= 3) { - PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl))); + // FIXME: SBStructuredData leaked here + PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*new lldb::SBStructuredData(args_impl))); result = pfunc(tp_arg, args_arg, dict); } else { error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)"); @@ -477,12 +527,14 @@ LLDBSwigPythonCreateScriptedBreakpointResolver if (!pfunc.IsAllocated()) return nullptr; + // FIXME: SBBreakpoint leaked here lldb::SBBreakpoint *bkpt_value = new lldb::SBBreakpoint(breakpoint_sp); - PythonObject bkpt_arg(PyRefType::Owned, SBTypeToSWIGWrapper(bkpt_value)); + PythonObject bkpt_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*bkpt_value)); + // FIXME: SBStructuredData leaked here lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); - PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value)); + PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*args_value)); PythonObject result = pfunc(bkpt_arg, args_arg, dict); // FIXME: At this point we should check that the class we found supports all the methods @@ -585,13 +637,14 @@ LLDBSwigPythonCreateScriptedStopHook return nullptr; } + // FIXME: SBTarget leaked here lldb::SBTarget *target_val = new lldb::SBTarget(target_sp); + PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*target_val)); - PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_val)); - + // FIXME: SBStructuredData leaked here lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl); - PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value)); + PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*args_value)); PythonObject result = pfunc(target_arg, args_arg, dict); @@ -918,6 +971,22 @@ LLDBSWIGPython_CastPyObjectToSBValue return sb_ptr; } +SWIGEXPORT void* +LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo +( + PyObject* data +) +{ + lldb::SBMemoryRegionInfo* sb_ptr = NULL; + + int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0); + + if (valid_cast == -1) + return NULL; + + return sb_ptr; +} + SWIGEXPORT bool LLDBSwigPythonCallCommand ( @@ -940,8 +1009,6 @@ LLDBSwigPythonCallCommand if (!pfunc.IsAllocated()) return false; - // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you - // see comment above for SBCommandReturnObjectReleaser for further details auto argc = pfunc.GetArgInfo(); if (!argc) { llvm::consumeError(argc.takeError()); @@ -949,7 +1016,7 @@ LLDBSwigPythonCallCommand } PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb)); - PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb)); + PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(cmd_retobj_sb)); if (argc.get().max_positional_args < 5u) pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict); @@ -981,11 +1048,9 @@ LLDBSwigPythonCallCommandObject if (!pfunc.IsAllocated()) return false; - // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you - // see comment above for SBCommandReturnObjectReleaser for further details PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb)); - PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb)); + PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(cmd_retobj_sb)); pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg); @@ -1011,10 +1076,9 @@ LLDBSWIGPythonCreateOSPlugin if (!pfunc.IsAllocated()) Py_RETURN_NONE; - // I do not want the SBProcess to be deallocated when going out of scope because python - // has ownership of it and will manage memory for this object by itself + // FIXME: This leaks the SBProcess object lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp); - PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb)); + PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(*process_sb)); if (!process_arg.IsAllocated()) Py_RETURN_NONE; |