diff --git a/lldb/bindings/lua/lua-swigsafecast.swig b/lldb/bindings/lua/lua-swigsafecast.swig --- a/lldb/bindings/lua/lua-swigsafecast.swig +++ b/lldb/bindings/lua/lua-swigsafecast.swig @@ -1,27 +1,20 @@ -template -void -PushSBClass (lua_State* L, SBClass* obj); +template void PushSBClass(lua_State *L, SBClass *obj); -void -PushSBClass (lua_State* L, lldb::SBFrame* frame_sb) -{ - SWIG_NewPointerObj(L, frame_sb, SWIGTYPE_p_lldb__SBFrame, 0); +void PushSBClass(lua_State *L, lldb::SBFrame *frame_sb) { + SWIG_NewPointerObj(L, frame_sb, SWIGTYPE_p_lldb__SBFrame, 0); } -void -PushSBClass (lua_State* L, lldb::SBBreakpointLocation* breakpoint_location_sb) -{ - SWIG_NewPointerObj(L, breakpoint_location_sb, SWIGTYPE_p_lldb__SBBreakpointLocation, 0); +void PushSBClass(lua_State *L, + lldb::SBBreakpointLocation *breakpoint_location_sb) { + SWIG_NewPointerObj(L, breakpoint_location_sb, + SWIGTYPE_p_lldb__SBBreakpointLocation, 0); } -void -PushSBClass (lua_State* L, lldb::SBWatchpoint* watchpoint_sb) -{ - SWIG_NewPointerObj(L, watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0); +void PushSBClass(lua_State *L, lldb::SBWatchpoint *watchpoint_sb) { + SWIG_NewPointerObj(L, watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0); } -void -PushSBClass (lua_State* L, lldb::SBStructuredData* structured_data_sb) -{ - SWIG_NewPointerObj(L, structured_data_sb, SWIGTYPE_p_lldb__SBStructuredData, 0); +void PushSBClass(lua_State *L, lldb::SBStructuredData *structured_data_sb) { + SWIG_NewPointerObj(L, structured_data_sb, SWIGTYPE_p_lldb__SBStructuredData, + 0); } diff --git a/lldb/bindings/lua/lua-typemaps.swig b/lldb/bindings/lua/lua-typemaps.swig --- a/lldb/bindings/lua/lua-typemaps.swig +++ b/lldb/bindings/lua/lua-typemaps.swig @@ -76,11 +76,11 @@ // typemap for a char buffer %typemap(in) (char *dst, size_t dst_len) { - $2 = luaL_checkinteger(L, $input); - if ($2 <= 0) { - return luaL_error(L, "Positive integer expected"); - } - $1 = (char *) malloc($2); + $2 = luaL_checkinteger(L, $input); + if ($2 <= 0) { + return luaL_error(L, "Positive integer expected"); + } + $1 = (char *)malloc($2); } // SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated @@ -92,14 +92,14 @@ // 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 - if ($result == 0) { - lua_pushliteral(L, ""); - } else { - lua_pushlstring(L, (const char *)$1, $result); - } - free($1); - // SWIG_arg was already incremented + lua_pop(L, 1); // Blow away the previous result + if ($result == 0) { + lua_pushliteral(L, ""); + } else { + lua_pushlstring(L, (const char *)$1, $result); + } + free($1); + // SWIG_arg was already incremented } // SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated @@ -114,18 +114,18 @@ // 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); + $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 + lua_pop(L, 1); // Blow away the previous result + lua_pushlstring(L, (const char *)$1, $result); + free($1); + // SWIG_arg was already incremented } //===----------------------------------------------------------------------===// @@ -133,22 +133,22 @@ // Typemap for handling SBModule::GetVersion %typemap(in) (uint32_t *versions, uint32_t num_versions) { - $2 = 99; - $1 = (uint32_t *)malloc(sizeof(uint32_t) * $2); + $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); + 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); } //===----------------------------------------------------------------------===// @@ -156,15 +156,15 @@ // Typemap for handling SBDebugger::SetLoggingCallback %typemap(in) (lldb::LogOutputCallback log_callback, void *baton) { - $1 = LLDBSwigLuaCallLuaLogOutputCallback; - $2 = (void *)L; + $1 = LLDBSwigLuaCallLuaLogOutputCallback; + $2 = (void *)L; - luaL_checktype(L, 2, LUA_TFUNCTION); - lua_settop(L, 2); + luaL_checktype(L, 2, LUA_TFUNCTION); + lua_settop(L, 2); - lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback); - lua_insert(L, 2); - lua_settable(L, LUA_REGISTRYINDEX); + lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback); + lua_insert(L, 2); + lua_settable(L, LUA_REGISTRYINDEX); } //===----------------------------------------------------------------------===// @@ -172,20 +172,20 @@ // 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); + $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); + $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); + $1 = (void *)luaL_checklstring(L, $input, &$2); } //===----------------------------------------------------------------------===// @@ -195,35 +195,35 @@ // 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); + 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] = 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"); - } + $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); + free((char *) $1); } %typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** { - $1 = (lua_istable(L, $input) || lua_isnil(L, $input)); + $1 = (lua_istable(L, $input) || lua_isnil(L, $input)); } //===----------------------------------------------------------------------===// @@ -231,30 +231,30 @@ // 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(p->f, false); - if (!file_sp->IsValid()) - return luaL_error(L, "Invalid file"); - $1 = file_sp; + luaL_Stream *p = (luaL_Stream *)luaL_checkudata(L, $input, LUA_FILEHANDLE); + lldb::FileSP file_sp; + file_sp = std::make_shared(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); + $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++; - } + 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++; + } } //===----------------------------------------------------------------------===// @@ -266,29 +266,29 @@ (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); + 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"); } - } 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."); - } + $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), @@ -296,7 +296,7 @@ (int64_t* array, size_t array_len), (int32_t* array, size_t array_len), (double* array, size_t array_len) { - free($1); + free($1); } //===----------------------------------------------------------------------===// @@ -304,13 +304,12 @@ // 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); - } + if (lua_isnil(L, $input)) { + $1 = NULL; + $2 = 0; + } else { + $1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2); + } } //===----------------------------------------------------------------------===// diff --git a/lldb/bindings/lua/lua-wrapper.swig b/lldb/bindings/lua/lua-wrapper.swig --- a/lldb/bindings/lua/lua-wrapper.swig +++ b/lldb/bindings/lua/lua-wrapper.swig @@ -1,101 +1,87 @@ %header %{ -template -void -PushSBClass(lua_State* L, T* obj); +template void PushSBClass(lua_State * L, T * obj); // This function is called from Lua::CallBreakpointCallback -llvm::Expected -lldb_private::LLDBSwigLuaBreakpointCallbackFunction -( - lua_State *L, - lldb::StackFrameSP stop_frame_sp, - lldb::BreakpointLocationSP bp_loc_sp, - const StructuredDataImpl &extra_args_impl -) -{ - lldb::SBFrame sb_frame(stop_frame_sp); - lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); - int nargs = 2; - - lldb::SBStructuredData extra_args(extra_args_impl); - - // Push the Lua wrappers - PushSBClass(L, &sb_frame); - PushSBClass(L, &sb_bp_loc); - - if (extra_args.IsValid()) { - PushSBClass(L, &extra_args); - nargs++; - } - - // Call into the Lua callback passing 'sb_frame' and 'sb_bp_loc'. - // Expects a boolean return. - if (lua_pcall(L, nargs, 1, 0) != LUA_OK) { - llvm::Error E = llvm::make_error( - llvm::formatv("{0}\n", lua_tostring(L, -1)), - llvm::inconvertibleErrorCode()); - // Pop error message from the stack. - lua_pop(L, 1); - return std::move(E); - } - - // Boolean return from the callback - bool stop = lua_toboolean(L, -1); - lua_pop(L, 1); - - return stop; +llvm::Expected lldb_private::LLDBSwigLuaBreakpointCallbackFunction( + lua_State * L, lldb::StackFrameSP stop_frame_sp, + lldb::BreakpointLocationSP bp_loc_sp, + const StructuredDataImpl &extra_args_impl) { + lldb::SBFrame sb_frame(stop_frame_sp); + lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); + int nargs = 2; + + lldb::SBStructuredData extra_args(extra_args_impl); + + // Push the Lua wrappers + PushSBClass(L, &sb_frame); + PushSBClass(L, &sb_bp_loc); + + if (extra_args.IsValid()) { + PushSBClass(L, &extra_args); + nargs++; + } + + // Call into the Lua callback passing 'sb_frame' and 'sb_bp_loc'. + // Expects a boolean return. + if (lua_pcall(L, nargs, 1, 0) != LUA_OK) { + llvm::Error E = llvm::make_error( + llvm::formatv("{0}\n", lua_tostring(L, -1)), + llvm::inconvertibleErrorCode()); + // Pop error message from the stack. + lua_pop(L, 1); + return std::move(E); + } + + // Boolean return from the callback + bool stop = lua_toboolean(L, -1); + lua_pop(L, 1); + + return stop; } // This function is called from Lua::CallWatchpointCallback -llvm::Expected -lldb_private::LLDBSwigLuaWatchpointCallbackFunction -( - lua_State *L, - lldb::StackFrameSP stop_frame_sp, - lldb::WatchpointSP wp_sp -) -{ - lldb::SBFrame sb_frame(stop_frame_sp); - lldb::SBWatchpoint sb_wp(wp_sp); - int nargs = 2; - - // Push the Lua wrappers - PushSBClass(L, &sb_frame); - PushSBClass(L, &sb_wp); - - // Call into the Lua callback passing 'sb_frame' and 'sb_wp'. - // Expects a boolean return. - if (lua_pcall(L, nargs, 1, 0) != LUA_OK) { - llvm::Error E = llvm::make_error( - llvm::formatv("{0}\n", lua_tostring(L, -1)), - llvm::inconvertibleErrorCode()); - // Pop error message from the stack. - lua_pop(L, 1); - return std::move(E); - } - - // Boolean return from the callback - bool stop = lua_toboolean(L, -1); - lua_pop(L, 1); - - return stop; +llvm::Expected lldb_private::LLDBSwigLuaWatchpointCallbackFunction( + lua_State * L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp) { + lldb::SBFrame sb_frame(stop_frame_sp); + lldb::SBWatchpoint sb_wp(wp_sp); + int nargs = 2; + + // Push the Lua wrappers + PushSBClass(L, &sb_frame); + PushSBClass(L, &sb_wp); + + // Call into the Lua callback passing 'sb_frame' and 'sb_wp'. + // Expects a boolean return. + if (lua_pcall(L, nargs, 1, 0) != LUA_OK) { + llvm::Error E = llvm::make_error( + llvm::formatv("{0}\n", lua_tostring(L, -1)), + llvm::inconvertibleErrorCode()); + // Pop error message from the stack. + lua_pop(L, 1); + return std::move(E); + } + + // Boolean return from the callback + bool stop = lua_toboolean(L, -1); + lua_pop(L, 1); + + return stop; } -static void -LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) { - lua_State *L = (lua_State *)baton; +static void LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) { + lua_State *L = (lua_State *)baton; - lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback); - lua_gettable(L, LUA_REGISTRYINDEX); + 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); + // FIXME: There's no way to report errors back to the user + lua_pushstring(L, str); + lua_pcall(L, 1, 0, 0); } -static int LLDBSwigLuaCloseFileHandle(lua_State *L) { - return luaL_error(L, "You cannot close a file handle used by lldb."); +static int LLDBSwigLuaCloseFileHandle(lua_State * L) { + return luaL_error(L, "You cannot close a file handle used by lldb."); } %} diff --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig --- a/lldb/bindings/python/python-typemaps.swig +++ b/lldb/bindings/python/python-typemaps.swig @@ -12,22 +12,22 @@ PythonList list(PyRefType::Borrowed, $input); int size = list.GetSize(); int i = 0; - $1 = (char**)malloc((size+1)*sizeof(char*)); + $1 = (char **)malloc((size + 1) * sizeof(char *)); for (i = 0; i < size; i++) { PythonString py_str = list.GetItemAtIndex(i).AsType(); if (!py_str.IsAllocated()) { - PyErr_SetString(PyExc_TypeError,"list must contain strings"); + PyErr_SetString(PyExc_TypeError, "list must contain strings"); free($1); return nullptr; } - $1[i] = const_cast(py_str.GetString().data()); + $1[i] = const_cast(py_str.GetString().data()); } $1[i] = 0; } else if ($input == Py_None) { - $1 = NULL; + $1 = NULL; } else { - PyErr_SetString(PyExc_TypeError,"not a list"); + PyErr_SetString(PyExc_TypeError, "not a list"); return NULL; } } @@ -41,12 +41,12 @@ int i = 0; for (i = 0; i < size; i++) { PythonString s = list.GetItemAtIndex(i).AsType(); - if (!s.IsAllocated()) { $1 = 0; } + if (!s.IsAllocated()) { + $1 = 0; + } } - } - else - { - $1 = ( ($input == Py_None) ? 1 : 0); + } else { + $1 = (($input == Py_None) ? 1 : 0); } } @@ -58,7 +58,8 @@ int len; int i; len = 0; - while ($1[len]) len++; + while ($1[len]) + len++; PythonList list(len); for (i = 0; i < len; i++) list.SetItemAtIndex(i, PythonString($1[i])); @@ -76,7 +77,7 @@ %typemap(in) lldb::StateType { PythonObject obj = Retain($input); unsigned long long state_type_value = - unwrapOrSetPythonException(As(obj)); + unwrapOrSetPythonException(As(obj)); if (PyErr_Occurred()) return nullptr; if (state_type_value > lldb::StateType::kLastStateType) { @@ -90,16 +91,16 @@ // typemap for a char buffer %typemap(in) (char *dst, size_t dst_len) { - if (!PyInt_Check($input)) { - PyErr_SetString(PyExc_ValueError, "Expecting an integer"); - return NULL; - } - $2 = PyInt_AsLong($input); - if ($2 <= 0) { - PyErr_SetString(PyExc_ValueError, "Positive integer expected"); - return NULL; - } - $1 = (char *) malloc($2); + if (!PyInt_Check($input)) { + PyErr_SetString(PyExc_ValueError, "Expecting an integer"); + return NULL; + } + $2 = PyInt_AsLong($input); + if ($2 <= 0) { + PyErr_SetString(PyExc_ValueError, "Positive integer expected"); + return NULL; + } + $1 = (char *)malloc($2); } // SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated // as char data instead of byte data. @@ -107,17 +108,17 @@ // Return the char buffer. Discarding any previous return result %typemap(argout) (char *dst, size_t dst_len) { - Py_XDECREF($result); /* Blow away any previous result */ - if (result == 0) { - PythonString string(""); - $result = string.release(); - Py_INCREF($result); - } else { - llvm::StringRef ref(static_cast($1), result); - PythonString string(ref); - $result = string.release(); - } - free($1); + Py_XDECREF($result); /* Blow away any previous result */ + if (result == 0) { + PythonString string(""); + $result = string.release(); + Py_INCREF($result); + } else { + llvm::StringRef ref(static_cast($1), result); + PythonString string(ref); + $result = string.release(); + } + free($1); } // SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated // as char data instead of byte data. @@ -126,23 +127,23 @@ // typemap for handling an snprintf-like API like SBThread::GetStopDescription. %typemap(in) (char *dst_or_null, size_t dst_len) { - if (!PyInt_Check($input)) { - PyErr_SetString(PyExc_ValueError, "Expecting an integer"); - return NULL; - } - $2 = PyInt_AsLong($input); - if ($2 <= 0) { - PyErr_SetString(PyExc_ValueError, "Positive integer expected"); - return NULL; - } - $1 = (char *) malloc($2); + if (!PyInt_Check($input)) { + PyErr_SetString(PyExc_ValueError, "Expecting an integer"); + return NULL; + } + $2 = PyInt_AsLong($input); + if ($2 <= 0) { + PyErr_SetString(PyExc_ValueError, "Positive integer expected"); + return NULL; + } + $1 = (char *)malloc($2); } %typemap(argout) (char *dst_or_null, size_t dst_len) { - Py_XDECREF($result); /* Blow away any previous result */ - llvm::StringRef ref($1); - PythonString string(ref); - $result = string.release(); - free($1); + Py_XDECREF($result); /* Blow away any previous result */ + llvm::StringRef ref($1); + PythonString string(ref); + $result = string.release(); + free($1); } @@ -151,80 +152,74 @@ // Ditto for SBProcess::PutSTDIN(const char *src, size_t src_len). %typemap(in) (const char *cstr, uint32_t cstr_len), (const char *src, size_t src_len) { - if (PythonString::Check($input)) { - PythonString str(PyRefType::Borrowed, $input); - $1 = (char*)str.GetString().data(); - $2 = str.GetSize(); - } - else if(PythonByteArray::Check($input)) { - PythonByteArray bytearray(PyRefType::Borrowed, $input); - $1 = (char*)bytearray.GetBytes().data(); - $2 = bytearray.GetSize(); - } - else if (PythonBytes::Check($input)) { - PythonBytes bytes(PyRefType::Borrowed, $input); - $1 = (char*)bytes.GetBytes().data(); - $2 = bytes.GetSize(); - } - else { - PyErr_SetString(PyExc_ValueError, "Expecting a string"); - return NULL; - } + if (PythonString::Check($input)) { + PythonString str(PyRefType::Borrowed, $input); + $1 = (char *)str.GetString().data(); + $2 = str.GetSize(); + } else if (PythonByteArray::Check($input)) { + PythonByteArray bytearray(PyRefType::Borrowed, $input); + $1 = (char *)bytearray.GetBytes().data(); + $2 = bytearray.GetSize(); + } else if (PythonBytes::Check($input)) { + PythonBytes bytes(PyRefType::Borrowed, $input); + $1 = (char *)bytes.GetBytes().data(); + $2 = bytes.GetSize(); + } else { + PyErr_SetString(PyExc_ValueError, "Expecting a string"); + return NULL; + } } // For SBProcess::WriteMemory, SBTarget::GetInstructions and SBDebugger::DispatchInput. %typemap(in) (const void *buf, size_t size), (const void *data, size_t data_len) { - if (PythonString::Check($input)) { - PythonString str(PyRefType::Borrowed, $input); - $1 = (void*)str.GetString().data(); - $2 = str.GetSize(); - } - else if(PythonByteArray::Check($input)) { - PythonByteArray bytearray(PyRefType::Borrowed, $input); - $1 = (void*)bytearray.GetBytes().data(); - $2 = bytearray.GetSize(); - } - else if (PythonBytes::Check($input)) { - PythonBytes bytes(PyRefType::Borrowed, $input); - $1 = (void*)bytes.GetBytes().data(); - $2 = bytes.GetSize(); - } - else { - PyErr_SetString(PyExc_ValueError, "Expecting a buffer"); - return NULL; - } + if (PythonString::Check($input)) { + PythonString str(PyRefType::Borrowed, $input); + $1 = (void *)str.GetString().data(); + $2 = str.GetSize(); + } else if (PythonByteArray::Check($input)) { + PythonByteArray bytearray(PyRefType::Borrowed, $input); + $1 = (void *)bytearray.GetBytes().data(); + $2 = bytearray.GetSize(); + } else if (PythonBytes::Check($input)) { + PythonBytes bytes(PyRefType::Borrowed, $input); + $1 = (void *)bytes.GetBytes().data(); + $2 = bytes.GetSize(); + } else { + PyErr_SetString(PyExc_ValueError, "Expecting a buffer"); + return NULL; + } } // typemap for an incoming buffer // See also SBProcess::ReadMemory. %typemap(in) (void *buf, size_t size) { - if (PyInt_Check($input)) { - $2 = PyInt_AsLong($input); - } else if (PyLong_Check($input)) { - $2 = PyLong_AsLong($input); - } else { - PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object"); - return NULL; - } - if ($2 <= 0) { - PyErr_SetString(PyExc_ValueError, "Positive integer expected"); - return NULL; - } - $1 = (void *) malloc($2); + if (PyInt_Check($input)) { + $2 = PyInt_AsLong($input); + } else if (PyLong_Check($input)) { + $2 = PyLong_AsLong($input); + } else { + PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object"); + return NULL; + } + if ($2 <= 0) { + PyErr_SetString(PyExc_ValueError, "Positive integer expected"); + return NULL; + } + $1 = (void *)malloc($2); } // Return the buffer. Discarding any previous return result // See also SBProcess::ReadMemory. %typemap(argout) (void *buf, size_t size) { - Py_XDECREF($result); /* Blow away any previous result */ - if (result == 0) { - $result = Py_None; - Py_INCREF($result); - } else { - PythonBytes bytes(static_cast($1), result); - $result = bytes.release(); - } - free($1); + Py_XDECREF($result); /* Blow away any previous result */ + if (result == 0) { + $result = Py_None; + Py_INCREF($result); + } else { + PythonBytes bytes(static_cast($1), result); + $result = bytes.release(); + } + free($1); } %{ @@ -250,19 +245,18 @@ return static_cast(PyLong_AsLong(obj)); } -template -bool SetNumberFromPyObject(T &number, PyObject *obj) { +template bool SetNumberFromPyObject(T &number, PyObject *obj) { if (PyInt_Check(obj)) number = static_cast(PyInt_AsLong(obj)); else if (PyLong_Check(obj)) number = PyLongAsT(obj); - else return false; + else + return false; return true; } -template <> -bool SetNumberFromPyObject(double &number, PyObject *obj) { +template <> bool SetNumberFromPyObject(double &number, PyObject *obj) { if (PyFloat_Check(obj)) { number = PyFloat_AsDouble(obj); return true; @@ -287,11 +281,11 @@ int size = PyList_Size($input); int i = 0; $2 = size; - $1 = ($1_type) malloc(size * sizeof($*1_type)); + $1 = ($1_type)malloc(size * sizeof($*1_type)); for (i = 0; i < size; i++) { - PyObject *o = PyList_GetItem($input,i); + PyObject *o = PyList_GetItem($input, i); if (!SetNumberFromPyObject($1[i], o)) { - PyErr_SetString(PyExc_TypeError,"list must contain numbers"); + PyErr_SetString(PyExc_TypeError, "list must contain numbers"); free($1); return NULL; } @@ -302,10 +296,10 @@ } } } else if ($input == Py_None) { - $1 = NULL; + $1 = NULL; $2 = 0; } else { - PyErr_SetString(PyExc_TypeError,"not a list"); + PyErr_SetString(PyExc_TypeError, "not a list"); return NULL; } } @@ -322,43 +316,42 @@ // to the more Pythonic style where a list is returned and no previous allocation // is necessary - this will break if more than 50 versions are ever returned %typemap(typecheck) (uint32_t *versions, uint32_t num_versions) { - $1 = ($input == Py_None ? 1 : 0); + $1 = ($input == Py_None ? 1 : 0); } %typemap(in, numinputs=0) (uint32_t *versions) { - $1 = (uint32_t*)malloc(sizeof(uint32_t) * 50); + $1 = (uint32_t *)malloc(sizeof(uint32_t) * 50); } %typemap(in, numinputs=0) (uint32_t num_versions) { - $1 = 50; + $1 = 50; } %typemap(argout) (uint32_t *versions, uint32_t num_versions) { - uint32_t count = result; - if (count >= $2) - count = $2; - PyObject* list = PyList_New(count); - for (uint32_t j = 0; j < count; j++) - { - PyObject* item = PyInt_FromLong($1[j]); - int ok = PyList_SetItem(list,j,item); - if (ok != 0) - { - $result = Py_None; - break; - } + uint32_t count = result; + if (count >= $2) + count = $2; + PyObject *list = PyList_New(count); + for (uint32_t j = 0; j < count; j++) { + PyObject *item = PyInt_FromLong($1[j]); + int ok = PyList_SetItem(list, j, item); + if (ok != 0) { + $result = Py_None; + break; } - $result = list; + } + $result = list; } %typemap(freearg) (uint32_t *versions) { - free($1); + free($1); } // For Log::LogOutputCallback %typemap(in) (lldb::LogOutputCallback log_callback, void *baton) { - if (!($input == Py_None || PyCallable_Check(reinterpret_cast($input)))) { + if (!($input == Py_None || + PyCallable_Check(reinterpret_cast($input)))) { PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); return NULL; } @@ -376,7 +369,7 @@ %typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) { $1 = $input == Py_None; - $1 = $1 || PyCallable_Check(reinterpret_cast($input)); + $1 = $1 || PyCallable_Check(reinterpret_cast($input)); } @@ -398,7 +391,8 @@ PyErr_SetString(PyExc_TypeError, "not a file"); return nullptr; } - auto sp = unwrapOrSetPythonException(py_file.ConvertToFileForcingUseOfScriptingIOMethods()); + auto sp = unwrapOrSetPythonException( + py_file.ConvertToFileForcingUseOfScriptingIOMethods()); if (!sp) return nullptr; $1 = sp; @@ -410,7 +404,8 @@ PyErr_SetString(PyExc_TypeError, "not a file"); return nullptr; } - auto sp = unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true)); + auto sp = + unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true)); if (!sp) return nullptr; $1 = sp; @@ -422,7 +417,8 @@ PyErr_SetString(PyExc_TypeError, "not a file"); return nullptr; } - auto sp = unwrapOrSetPythonException(py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true)); + auto sp = unwrapOrSetPythonException( + py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true)); if (!sp) return nullptr; $1 = sp; @@ -446,40 +442,34 @@ return nullptr; $result = pyfile.release(); } - if (!$result) - { - $result = Py_None; - Py_INCREF(Py_None); + if (!$result) { + $result = Py_None; + Py_INCREF(Py_None); } } %typemap(in) (const char* string, int len) { - if ($input == Py_None) - { - $1 = NULL; - $2 = 0; - } - else if (PythonString::Check($input)) - { - PythonString py_str(PyRefType::Borrowed, $input); - llvm::StringRef str = py_str.GetString(); - $1 = const_cast(str.data()); - $2 = str.size(); - // In Python 2, if $input is a PyUnicode object then this - // will trigger a Unicode -> String conversion, in which - // case the `PythonString` will now own the PyString. Thus - // if it goes out of scope, the data will be deleted. The - // only way to avoid this is to leak the Python object in - // that case. Note that if there was no conversion, then - // releasing the string will not leak anything, since we - // created this as a borrowed reference. - py_str.release(); - } - else - { - PyErr_SetString(PyExc_TypeError,"not a string-like object"); - return NULL; - } + if ($input == Py_None) { + $1 = NULL; + $2 = 0; + } else if (PythonString::Check($input)) { + PythonString py_str(PyRefType::Borrowed, $input); + llvm::StringRef str = py_str.GetString(); + $1 = const_cast(str.data()); + $2 = str.size(); + // In Python 2, if $input is a PyUnicode object then this + // will trigger a Unicode -> String conversion, in which + // case the `PythonString` will now own the PyString. Thus + // if it goes out of scope, the data will be deleted. The + // only way to avoid this is to leak the Python object in + // that case. Note that if there was no conversion, then + // releasing the string will not leak anything, since we + // created this as a borrowed reference. + py_str.release(); + } else { + PyErr_SetString(PyExc_TypeError, "not a string-like object"); + return NULL; + } } // These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i, @@ -491,7 +481,9 @@ %define %pybuffer_mutable_binary(TYPEMAP, SIZE) %typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) { - int res; Py_ssize_t size = 0; void *buf = 0; + int res; + Py_ssize_t size = 0; + void *buf = 0; res = PyObject_GetBuffer($input, &view.buffer, PyBUF_WRITABLE); if (res < 0) { PyErr_Clear(); @@ -499,14 +491,16 @@ } size = view.buffer.len; buf = view.buffer.buf; - $1 = ($1_ltype) buf; - $2 = ($2_ltype) (size/sizeof($*1_type)); + $1 = ($1_ltype)buf; + $2 = ($2_ltype)(size / sizeof($*1_type)); } %enddef %define %pybuffer_binary(TYPEMAP, SIZE) %typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) { - int res; Py_ssize_t size = 0; const void *buf = 0; + int res; + Py_ssize_t size = 0; + const void *buf = 0; res = PyObject_GetBuffer($input, &view.buffer, PyBUF_CONTIG_RO); if (res < 0) { PyErr_Clear(); @@ -514,8 +508,8 @@ } size = view.buffer.len; buf = view.buffer.buf; - $1 = ($1_ltype) buf; - $2 = ($2_ltype) (size / sizeof($*1_type)); + $1 = ($1_ltype)buf; + $2 = ($2_ltype)(size / sizeof($*1_type)); } %enddef diff --git a/lldb/bindings/python/python-wrapper.swig b/lldb/bindings/python/python-wrapper.swig --- a/lldb/bindings/python/python-wrapper.swig +++ b/lldb/bindings/python/python-wrapper.swig @@ -1,67 +1,58 @@ %header %{ -class PyErr_Cleaner -{ +class PyErr_Cleaner { public: - PyErr_Cleaner(bool print=false) : - m_print(print) - { - } + PyErr_Cleaner(bool print = false) : m_print(print) {} - ~PyErr_Cleaner() - { - if (PyErr_Occurred()) - { - if(m_print && !PyErr_ExceptionMatches(PyExc_SystemExit)) - PyErr_Print(); - PyErr_Clear(); - } + ~PyErr_Cleaner() { + if (PyErr_Occurred()) { + if (m_print && !PyErr_ExceptionMatches(PyExc_SystemExit)) + PyErr_Print(); + PyErr_Clear(); } + } private: - bool m_print; + bool m_print; }; -llvm::Expected -lldb_private::LLDBSwigPythonBreakpointCallbackFunction -( - const char *python_function_name, - const char *session_dictionary_name, - const lldb::StackFrameSP& frame_sp, - const lldb::BreakpointLocationSP& bp_loc_sp, - const lldb_private::StructuredDataImpl &args_impl -) -{ - using namespace llvm; - - lldb::SBFrame sb_frame (frame_sp); - lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); - - PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name, dict); - - unsigned max_positional_args; - if (auto arg_info = pfunc.GetArgInfo()) - max_positional_args = arg_info.get().max_positional_args; - else - return arg_info.takeError(); - - PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame)); - PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc)); - - auto result = [&] () -> Expected { - // If the called function doesn't take extra_args, drop them here: - if (max_positional_args < 4) - return pfunc.Call(frame_arg, bp_loc_arg, dict); - return pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict); - } (); - - if (!result) - return result.takeError(); - - // Only False counts as false! - return result.get().get() != Py_False; +llvm::Expected lldb_private::LLDBSwigPythonBreakpointCallbackFunction( + const char *python_function_name, const char *session_dictionary_name, + const lldb::StackFrameSP &frame_sp, + const lldb::BreakpointLocationSP &bp_loc_sp, + const lldb_private::StructuredDataImpl &args_impl) { + using namespace llvm; + + lldb::SBFrame sb_frame(frame_sp); + lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); + + PyErr_Cleaner py_err_cleaner(true); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); + + unsigned max_positional_args; + if (auto arg_info = pfunc.GetArgInfo()) + max_positional_args = arg_info.get().max_positional_args; + else + return arg_info.takeError(); + + PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame)); + PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc)); + + auto result = [&]() -> Expected { + // If the called function doesn't take extra_args, drop them here: + if (max_positional_args < 4) + return pfunc.Call(frame_arg, bp_loc_arg, dict); + return pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict); + }(); + + if (!result) + return result.takeError(); + + // Only False counts as false! + return result.get().get() != Py_False; } // resolve a dotted Python name in the form @@ -69,390 +60,355 @@ // if pmodule is NULL, the __main__ module will be used // as the starting point for the search +// This function is called by +// lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) and is +// used when a script command is attached to a breakpoint for execution. -// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) -// and is used when a script command is attached to a breakpoint for execution. - -// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) -// and is used when a script command is attached to a watchpoint for execution. +// This function is called by +// lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) and is +// used when a script command is attached to a watchpoint for execution. -bool -lldb_private::LLDBSwigPythonWatchpointCallbackFunction -( - const char *python_function_name, - const char *session_dictionary_name, - const lldb::StackFrameSP& frame_sp, - const lldb::WatchpointSP& wp_sp -) -{ - lldb::SBFrame sb_frame (frame_sp); - lldb::SBWatchpoint sb_wp(wp_sp); +bool lldb_private::LLDBSwigPythonWatchpointCallbackFunction( + const char *python_function_name, const char *session_dictionary_name, + const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) { + lldb::SBFrame sb_frame(frame_sp); + lldb::SBWatchpoint sb_wp(wp_sp); - bool stop_at_watchpoint = true; + bool stop_at_watchpoint = true; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name, dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); - if (!pfunc.IsAllocated()) - return stop_at_watchpoint; + if (!pfunc.IsAllocated()) + return stop_at_watchpoint; - PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame)); - PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp)); - PythonObject result = pfunc(frame_arg, wp_arg, dict); + PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame)); + PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp)); + PythonObject result = pfunc(frame_arg, wp_arg, dict); - if (result.get() == Py_False) - stop_at_watchpoint = false; + if (result.get() == Py_False) + stop_at_watchpoint = false; - return stop_at_watchpoint; + return stop_at_watchpoint; } -bool -lldb_private::LLDBSwigPythonCallTypeScript -( - const char *python_function_name, - const void *session_dictionary, - const lldb::ValueObjectSP& valobj_sp, - void** pyfunct_wrapper, - const lldb::TypeSummaryOptionsSP& options_sp, - std::string& retval -) -{ - lldb::SBTypeSummaryOptions sb_options(options_sp.get()); +bool lldb_private::LLDBSwigPythonCallTypeScript( + const char *python_function_name, const void *session_dictionary, + const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper, + const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) { + lldb::SBTypeSummaryOptions sb_options(options_sp.get()); - retval.clear(); + retval.clear(); - if (!python_function_name || !session_dictionary) - return false; + if (!python_function_name || !session_dictionary) + return false; - PyObject *pfunc_impl = nullptr; + PyObject *pfunc_impl = nullptr; - if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper)) - { - pfunc_impl = (PyObject*)(*pyfunct_wrapper); - if (pfunc_impl->ob_refcnt == 1) - { - Py_XDECREF(pfunc_impl); - pfunc_impl = NULL; - } + if (pyfunct_wrapper && *pyfunct_wrapper && + PyFunction_Check(*pyfunct_wrapper)) { + pfunc_impl = (PyObject *)(*pyfunct_wrapper); + if (pfunc_impl->ob_refcnt == 1) { + Py_XDECREF(pfunc_impl); + pfunc_impl = NULL; } + } - PyObject *py_dict = (PyObject*)session_dictionary; - if (!PythonDictionary::Check(py_dict)) - return true; + PyObject *py_dict = (PyObject *)session_dictionary; + if (!PythonDictionary::Check(py_dict)) + return true; - PythonDictionary dict(PyRefType::Borrowed, py_dict); + PythonDictionary dict(PyRefType::Borrowed, py_dict); - PyErr_Cleaner pyerr_cleanup(true); // show Python errors + PyErr_Cleaner pyerr_cleanup(true); // show Python errors - PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl); + PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl); + if (!pfunc.IsAllocated()) { + pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); if (!pfunc.IsAllocated()) - { - pfunc = PythonObject::ResolveNameWithDictionary(python_function_name, dict); - if (!pfunc.IsAllocated()) - return false; - - if (pyfunct_wrapper) - { - *pyfunct_wrapper = pfunc.get(); - Py_XINCREF(pfunc.get()); - } - } + return false; - PythonObject result; - auto argc = pfunc.GetArgInfo(); - if (!argc) { - llvm::consumeError(argc.takeError()); - return false; + if (pyfunct_wrapper) { + *pyfunct_wrapper = pfunc.get(); + Py_XINCREF(pfunc.get()); } + } - PythonObject value_arg = ToSWIGWrapper(valobj_sp); - PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options)); + PythonObject result; + auto argc = pfunc.GetArgInfo(); + if (!argc) { + llvm::consumeError(argc.takeError()); + return false; + } - if (argc.get().max_positional_args < 3) - result = pfunc(value_arg,dict); - else - result = pfunc(value_arg,dict,options_arg); + PythonObject value_arg = ToSWIGWrapper(valobj_sp); + PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options)); - retval = result.Str().GetString().str(); + if (argc.get().max_positional_args < 3) + result = pfunc(value_arg, dict); + else + result = pfunc(value_arg, dict, options_arg); - return true; + retval = result.Str().GetString().str(); + + return true; } -void* -lldb_private::LLDBSwigPythonCreateSyntheticProvider -( - const char *python_class_name, - const char *session_dictionary_name, - const lldb::ValueObjectSP& valobj_sp -) -{ - if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) - Py_RETURN_NONE; +void *lldb_private::LLDBSwigPythonCreateSyntheticProvider( + const char *python_class_name, const char *session_dictionary_name, + const lldb::ValueObjectSP &valobj_sp) { + if (python_class_name == NULL || python_class_name[0] == '\0' || + !session_dictionary_name) + Py_RETURN_NONE; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_class_name,dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); - if (!pfunc.IsAllocated()) - Py_RETURN_NONE; + if (!pfunc.IsAllocated()) + Py_RETURN_NONE; - auto sb_value = std::make_unique(valobj_sp); - sb_value->SetPreferSyntheticValue(false); + auto sb_value = std::make_unique(valobj_sp); + sb_value->SetPreferSyntheticValue(false); - PythonObject val_arg = ToSWIGWrapper(std::move(sb_value)); - if (!val_arg.IsAllocated()) - Py_RETURN_NONE; + PythonObject val_arg = ToSWIGWrapper(std::move(sb_value)); + if (!val_arg.IsAllocated()) + Py_RETURN_NONE; - PythonObject result = pfunc(val_arg, dict); + PythonObject result = pfunc(val_arg, dict); - if (result.IsAllocated()) - return result.release(); + if (result.IsAllocated()) + return result.release(); - Py_RETURN_NONE; + Py_RETURN_NONE; } -void* -lldb_private::LLDBSwigPythonCreateCommandObject -( - const char *python_class_name, - const char *session_dictionary_name, - const lldb::DebuggerSP debugger_sp -) -{ - if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) - Py_RETURN_NONE; +void *lldb_private::LLDBSwigPythonCreateCommandObject( + const char *python_class_name, const char *session_dictionary_name, + const lldb::DebuggerSP debugger_sp) { + 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(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_class_name, dict); + PyErr_Cleaner py_err_cleaner(true); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); - if (!pfunc.IsAllocated()) - return nullptr; + if (!pfunc.IsAllocated()) + return nullptr; - lldb::SBDebugger debugger_sb(debugger_sp); - PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); - PythonObject result = pfunc(debugger_arg, dict); + lldb::SBDebugger debugger_sb(debugger_sp); + PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); + PythonObject result = pfunc(debugger_arg, dict); - if (result.IsAllocated()) - return result.release(); + if (result.IsAllocated()) + return result.release(); - Py_RETURN_NONE; + Py_RETURN_NONE; } -void* -lldb_private::LLDBSwigPythonCreateScriptedProcess -( - const char *python_class_name, - const char *session_dictionary_name, - const lldb::TargetSP& target_sp, +void *lldb_private::LLDBSwigPythonCreateScriptedProcess( + const char *python_class_name, const char *session_dictionary_name, + const lldb::TargetSP &target_sp, const 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(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_class_name, dict); - - if (!pfunc.IsAllocated()) { - error_string.append("could not find script class: "); - error_string.append(python_class_name); - return nullptr; - } - - PythonObject target_arg = ToSWIGWrapper(target_sp); - - llvm::Expected 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) { - result = pfunc(target_arg, ToSWIGWrapper(args_impl)); - } else { - error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)"); - Py_RETURN_NONE; - } - - if (result.IsAllocated()) - return result.release(); + std::string &error_string) { + if (python_class_name == NULL || python_class_name[0] == '\0' || + !session_dictionary_name) Py_RETURN_NONE; -} - -void* -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) - Py_RETURN_NONE; - - PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_class_name, dict); - - if (!pfunc.IsAllocated()) { - error_string.append("could not find script class: "); - error_string.append(python_class_name); - return nullptr; - } + PyErr_Cleaner py_err_cleaner(true); + + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); + + if (!pfunc.IsAllocated()) { + error_string.append("could not find script class: "); + error_string.append(python_class_name); + return nullptr; + } + + PythonObject target_arg = ToSWIGWrapper(target_sp); + + llvm::Expected 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) { + result = pfunc(target_arg, ToSWIGWrapper(args_impl)); + } else { + error_string.assign("wrong number of arguments in __init__, should be 2 " + "(not including self)"); + Py_RETURN_NONE; + } - llvm::Expected 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; - } + if (result.IsAllocated()) + return result.release(); + Py_RETURN_NONE; +} - PythonObject result = {}; - if (arg_info.get().max_positional_args == 2) { - result = pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl)); - } else { - error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)"); - Py_RETURN_NONE; - } +void *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) + Py_RETURN_NONE; - if (result.IsAllocated()) - return result.release(); + PyErr_Cleaner py_err_cleaner(true); + + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); + + if (!pfunc.IsAllocated()) { + error_string.append("could not find script class: "); + error_string.append(python_class_name); + return nullptr; + } + + llvm::Expected 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) { + result = pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl)); + } else { + 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; } -void* -lldb_private::LLDBSwigPythonCreateScriptedThreadPlan -( - const char *python_class_name, - const char *session_dictionary_name, +void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan( + const char *python_class_name, const char *session_dictionary_name, const lldb_private::StructuredDataImpl &args_impl, - std::string &error_string, - const lldb::ThreadPlanSP& thread_plan_sp -) -{ - 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(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_class_name, dict); - - if (!pfunc.IsAllocated()) { - error_string.append("could not find script class: "); - error_string.append(python_class_name); - return nullptr; - } - - PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp); - - llvm::Expected 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; - } + std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) { + if (python_class_name == NULL || python_class_name[0] == '\0' || + !session_dictionary_name) + Py_RETURN_NONE; - PythonObject result = {}; - auto args_sb = std::make_unique(args_impl); - if (arg_info.get().max_positional_args == 2) { - if (args_sb->IsValid()) { - error_string.assign("args passed, but __init__ does not take an args dictionary"); - Py_RETURN_NONE; - } - result = pfunc(tp_arg, dict); - } else if (arg_info.get().max_positional_args >= 3) { - result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict); - } else { - error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)"); - Py_RETURN_NONE; + PyErr_Cleaner py_err_cleaner(true); + + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); + + if (!pfunc.IsAllocated()) { + error_string.append("could not find script class: "); + error_string.append(python_class_name); + return nullptr; + } + + PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp); + + llvm::Expected 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 = {}; + auto args_sb = std::make_unique(args_impl); + if (arg_info.get().max_positional_args == 2) { + if (args_sb->IsValid()) { + error_string.assign( + "args passed, but __init__ does not take an args dictionary"); + Py_RETURN_NONE; } + result = pfunc(tp_arg, dict); + } else if (arg_info.get().max_positional_args >= 3) { + result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict); + } else { + error_string.assign("wrong number of arguments in __init__, should be 2 or " + "3 (not including self)"); + Py_RETURN_NONE; + } - // FIXME: At this point we should check that the class we found supports all the methods - // that we need. + // FIXME: At this point we should check that the class we found supports all + // the methods that we need. - if (result.IsAllocated()) - return result.release(); - Py_RETURN_NONE; + if (result.IsAllocated()) + return result.release(); + Py_RETURN_NONE; } -bool -lldb_private::LLDBSWIGPythonCallThreadPlan -( - void *implementor, - const char *method_name, - lldb_private::Event *event, - bool &got_error -) -{ - got_error = false; +bool lldb_private::LLDBSWIGPythonCallThreadPlan( + void *implementor, const char *method_name, lldb_private::Event *event, + bool &got_error) { + got_error = false; - PyErr_Cleaner py_err_cleaner(false); - PythonObject self(PyRefType::Borrowed, static_cast(implementor)); - auto pfunc = self.ResolveName(method_name); + PyErr_Cleaner py_err_cleaner(false); + PythonObject self(PyRefType::Borrowed, static_cast(implementor)); + auto pfunc = self.ResolveName(method_name); - if (!pfunc.IsAllocated()) - return false; - - PythonObject result; - if (event != nullptr) - { - lldb::SBEvent sb_event(event); - PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event)); - result = pfunc(event_arg); - } - else - result = pfunc(); - - if (PyErr_Occurred()) - { - got_error = true; - printf ("Return value was neither false nor true for call to %s.\n", method_name); - PyErr_Print(); - return false; - } + if (!pfunc.IsAllocated()) + return false; - if (result.get() == Py_True) - return true; - else if (result.get() == Py_False) - return false; + PythonObject result; + if (event != nullptr) { + lldb::SBEvent sb_event(event); + PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event)); + result = pfunc(event_arg); + } else + result = pfunc(); - // Somebody returned the wrong thing... + if (PyErr_Occurred()) { got_error = true; - printf ("Wrong return value type for call to %s.\n", method_name); + printf("Return value was neither false nor true for call to %s.\n", + method_name); + PyErr_Print(); return false; + } + + if (result.get() == Py_True) + return true; + else if (result.get() == Py_False) + return false; + + // Somebody returned the wrong thing... + got_error = true; + printf("Wrong return value type for call to %s.\n", method_name); + return false; } void *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver( @@ -460,790 +416,714 @@ const StructuredDataImpl &args_impl, const lldb::BreakpointSP &breakpoint_sp) { - if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) - Py_RETURN_NONE; + if (python_class_name == NULL || python_class_name[0] == '\0' || + !session_dictionary_name) + Py_RETURN_NONE; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_class_name, dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); - if (!pfunc.IsAllocated()) - return nullptr; - - PythonObject result = - pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict); - // FIXME: At this point we should check that the class we found supports all the methods - // that we need. - - if (result.IsAllocated()) - { - // Check that __callback__ is defined: - auto callback_func = result.ResolveName("__callback__"); - if (callback_func.IsAllocated()) - return result.release(); - else - result.release(); - } - Py_RETURN_NONE; -} + if (!pfunc.IsAllocated()) + return nullptr; -unsigned int -lldb_private::LLDBSwigPythonCallBreakpointResolver -( - void *implementor, - const char *method_name, - lldb_private::SymbolContext *sym_ctx -) -{ - PyErr_Cleaner py_err_cleaner(false); - PythonObject self(PyRefType::Borrowed, static_cast(implementor)); - auto pfunc = self.ResolveName(method_name); + PythonObject result = + pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict); + // FIXME: At this point we should check that the class we found supports all + // the methods that we need. - if (!pfunc.IsAllocated()) - return 0; - - PythonObject result; - if (sym_ctx != nullptr) { - lldb::SBSymbolContext sb_sym_ctx(sym_ctx); - PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx)); - result = pfunc(sym_ctx_arg); - } else - result = pfunc(); - - if (PyErr_Occurred()) - { - PyErr_Print(); - PyErr_Clear(); - return 0; - } + if (result.IsAllocated()) { + // Check that __callback__ is defined: + auto callback_func = result.ResolveName("__callback__"); + if (callback_func.IsAllocated()) + return result.release(); + else + result.release(); + } + Py_RETURN_NONE; +} - // The callback will return a bool, but we're need to also return ints - // so we're squirrelling the bool through as an int... And if you return - // nothing, we'll continue. - if (strcmp(method_name, "__callback__") == 0) { - if (result.get() == Py_False) - return 0; - else - return 1; - } +unsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver( + void *implementor, const char *method_name, + lldb_private::SymbolContext *sym_ctx) { + PyErr_Cleaner py_err_cleaner(false); + PythonObject self(PyRefType::Borrowed, static_cast(implementor)); + auto pfunc = self.ResolveName(method_name); + + if (!pfunc.IsAllocated()) + return 0; + + PythonObject result; + if (sym_ctx != nullptr) { + lldb::SBSymbolContext sb_sym_ctx(sym_ctx); + PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx)); + result = pfunc(sym_ctx_arg); + } else + result = pfunc(); + + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + return 0; + } + + // The callback will return a bool, but we're need to also return ints + // so we're squirrelling the bool through as an int... And if you return + // nothing, we'll continue. + if (strcmp(method_name, "__callback__") == 0) { + if (result.get() == Py_False) + return 0; + else + return 1; + } - long long ret_val = unwrapOrSetPythonException(As(result)); + long long ret_val = unwrapOrSetPythonException(As(result)); - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - return 0; - } + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + return 0; + } - return ret_val; + return ret_val; } -void * -lldb_private::LLDBSwigPythonCreateScriptedStopHook -( - lldb::TargetSP target_sp, - const char *python_class_name, - const char *session_dictionary_name, - const StructuredDataImpl &args_impl, - Status &error -) -{ - if (python_class_name == NULL || python_class_name[0] == '\0') { - error.SetErrorString("Empty class name."); - Py_RETURN_NONE; - } - if (!session_dictionary_name) { - error.SetErrorString("No session dictionary"); - Py_RETURN_NONE; - } - - PyErr_Cleaner py_err_cleaner(true); - - auto dict = - PythonModule::MainModule().ResolveName( - session_dictionary_name); - auto pfunc = - PythonObject::ResolveNameWithDictionary( - python_class_name, dict); - - if (!pfunc.IsAllocated()) { - error.SetErrorStringWithFormat("Could not find class: %s.", - python_class_name); - return nullptr; - } - - PythonObject result = - pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict); - - if (result.IsAllocated()) - { - // Check that the handle_stop callback is defined: - auto callback_func = result.ResolveName("handle_stop"); - if (callback_func.IsAllocated()) { - if (auto args_info = callback_func.GetArgInfo()) { - size_t num_args = (*args_info).max_positional_args; - if (num_args != 2) { - error.SetErrorStringWithFormat("Wrong number of args for " +void *lldb_private::LLDBSwigPythonCreateScriptedStopHook( + lldb::TargetSP target_sp, const char *python_class_name, + const char *session_dictionary_name, const StructuredDataImpl &args_impl, + Status &error) { + if (python_class_name == NULL || python_class_name[0] == '\0') { + error.SetErrorString("Empty class name."); + Py_RETURN_NONE; + } + if (!session_dictionary_name) { + error.SetErrorString("No session dictionary"); + Py_RETURN_NONE; + } + + PyErr_Cleaner py_err_cleaner(true); + + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); + + if (!pfunc.IsAllocated()) { + error.SetErrorStringWithFormat("Could not find class: %s.", + python_class_name); + return nullptr; + } + + PythonObject result = + pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict); + + if (result.IsAllocated()) { + // Check that the handle_stop callback is defined: + auto callback_func = result.ResolveName("handle_stop"); + if (callback_func.IsAllocated()) { + if (auto args_info = callback_func.GetArgInfo()) { + size_t num_args = (*args_info).max_positional_args; + if (num_args != 2) { + error.SetErrorStringWithFormat( + "Wrong number of args for " "handle_stop callback, should be 2 (excluding self), got: %zu", num_args); - Py_RETURN_NONE; - } else - return result.release(); - } else { - error.SetErrorString("Couldn't get num arguments for handle_stop " - "callback."); - Py_RETURN_NONE; - } + Py_RETURN_NONE; + } else return result.release(); - } - else { - error.SetErrorStringWithFormat("Class \"%s\" is missing the required " - "handle_stop callback.", - python_class_name); - result.release(); - } + } else { + error.SetErrorString("Couldn't get num arguments for handle_stop " + "callback."); + Py_RETURN_NONE; + } + return result.release(); + } else { + error.SetErrorStringWithFormat("Class \"%s\" is missing the required " + "handle_stop callback.", + python_class_name); + result.release(); } - Py_RETURN_NONE; + } + Py_RETURN_NONE; } -bool -lldb_private::LLDBSwigPythonStopHookCallHandleStop -( - void *implementor, - lldb::ExecutionContextRefSP exc_ctx_sp, - lldb::StreamSP stream -) -{ - // handle_stop will return a bool with the meaning "should_stop"... - // If you return nothing we'll assume we are going to stop. - // Also any errors should return true, since we should stop on error. +bool lldb_private::LLDBSwigPythonStopHookCallHandleStop( + void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp, + lldb::StreamSP stream) { + // handle_stop will return a bool with the meaning "should_stop"... + // If you return nothing we'll assume we are going to stop. + // Also any errors should return true, since we should stop on error. - PyErr_Cleaner py_err_cleaner(false); - PythonObject self(PyRefType::Borrowed, static_cast(implementor)); - auto pfunc = self.ResolveName("handle_stop"); + PyErr_Cleaner py_err_cleaner(false); + PythonObject self(PyRefType::Borrowed, static_cast(implementor)); + auto pfunc = self.ResolveName("handle_stop"); - if (!pfunc.IsAllocated()) - return true; - - PythonObject result; - lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp); - PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx)); - lldb::SBStream sb_stream; - PythonObject sb_stream_arg(PyRefType::Owned, - SBTypeToSWIGWrapper(sb_stream)); - result = pfunc(exc_ctx_arg, sb_stream_arg); - - if (PyErr_Occurred()) - { - stream->PutCString("Python error occurred handling stop-hook."); - PyErr_Print(); - PyErr_Clear(); - return true; - } - - // Now add the result to the output stream. SBStream only - // makes an internally help StreamString which I can't interpose, so I - // have to copy it over here. - stream->PutCString(sb_stream.GetData()); - - if (result.get() == Py_False) - return false; - else - return true; -} - -// wrapper that calls an optional instance member of an object taking no arguments -static PyObject* -LLDBSwigPython_CallOptionalMember -( - PyObject* implementor, - char* callee_name, - PyObject* ret_if_not_found = Py_None, - bool* was_found = NULL -) -{ - PyErr_Cleaner py_err_cleaner(false); - - PythonObject self(PyRefType::Borrowed, static_cast(implementor)); - auto pfunc = self.ResolveName(callee_name); + if (!pfunc.IsAllocated()) + return true; - if (!pfunc.IsAllocated()) - { - if (was_found) - *was_found = false; - Py_XINCREF(ret_if_not_found); - return ret_if_not_found; - } + PythonObject result; + lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp); + PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx)); + lldb::SBStream sb_stream; + PythonObject sb_stream_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_stream)); + result = pfunc(exc_ctx_arg, sb_stream_arg); + + if (PyErr_Occurred()) { + stream->PutCString("Python error occurred handling stop-hook."); + PyErr_Print(); + PyErr_Clear(); + return true; + } - if (was_found) - *was_found = true; + // Now add the result to the output stream. SBStream only + // makes an internally help StreamString which I can't interpose, so I + // have to copy it over here. + stream->PutCString(sb_stream.GetData()); - PythonObject result = pfunc(); - return result.release(); + if (result.get() == Py_False) + return false; + else + return true; } -size_t -lldb_private::LLDBSwigPython_CalculateNumChildren -( - PyObject *implementor, - uint32_t max -) -{ - PythonObject self(PyRefType::Borrowed, implementor); - auto pfunc = self.ResolveName("num_children"); +// wrapper that calls an optional instance member of an object taking no +// arguments +static PyObject *LLDBSwigPython_CallOptionalMember( + PyObject * implementor, char *callee_name, + PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) { + PyErr_Cleaner py_err_cleaner(false); - if (!pfunc.IsAllocated()) - return 0; - - auto arg_info = pfunc.GetArgInfo(); - if (!arg_info) { - llvm::consumeError(arg_info.takeError()); - return 0; - } + PythonObject self(PyRefType::Borrowed, static_cast(implementor)); + auto pfunc = self.ResolveName(callee_name); - size_t ret_val; - if (arg_info.get().max_positional_args < 1) - ret_val = unwrapOrSetPythonException(As(pfunc.Call())); - else - ret_val = unwrapOrSetPythonException(As(pfunc.Call(PythonInteger(max)))); + if (!pfunc.IsAllocated()) { + if (was_found) + *was_found = false; + Py_XINCREF(ret_if_not_found); + return ret_if_not_found; + } - if (PyErr_Occurred()) - { - PyErr_Print(); - PyErr_Clear(); - return 0; - } + if (was_found) + *was_found = true; - if (arg_info.get().max_positional_args < 1) - ret_val = std::min(ret_val, static_cast(max)); + PythonObject result = pfunc(); + return result.release(); +} - return ret_val; +size_t lldb_private::LLDBSwigPython_CalculateNumChildren(PyObject * implementor, + uint32_t max) { + PythonObject self(PyRefType::Borrowed, implementor); + auto pfunc = self.ResolveName("num_children"); + + if (!pfunc.IsAllocated()) + return 0; + + auto arg_info = pfunc.GetArgInfo(); + if (!arg_info) { + llvm::consumeError(arg_info.takeError()); + return 0; + } + + size_t ret_val; + if (arg_info.get().max_positional_args < 1) + ret_val = unwrapOrSetPythonException(As(pfunc.Call())); + else + ret_val = unwrapOrSetPythonException( + As(pfunc.Call(PythonInteger(max)))); + + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + return 0; + } + + if (arg_info.get().max_positional_args < 1) + ret_val = std::min(ret_val, static_cast(max)); + + return ret_val; } -PyObject* -lldb_private::LLDBSwigPython_GetChildAtIndex -( - PyObject *implementor, - uint32_t idx -) -{ - PyErr_Cleaner py_err_cleaner(true); +PyObject *lldb_private::LLDBSwigPython_GetChildAtIndex(PyObject * implementor, + uint32_t idx) { + PyErr_Cleaner py_err_cleaner(true); - PythonObject self(PyRefType::Borrowed, implementor); - auto pfunc = self.ResolveName("get_child_at_index"); + PythonObject self(PyRefType::Borrowed, implementor); + auto pfunc = self.ResolveName("get_child_at_index"); - if (!pfunc.IsAllocated()) - return nullptr; + if (!pfunc.IsAllocated()) + return nullptr; - PythonObject result = pfunc(PythonInteger(idx)); + PythonObject result = pfunc(PythonInteger(idx)); - if (!result.IsAllocated()) - return nullptr; + if (!result.IsAllocated()) + return nullptr; - lldb::SBValue* sbvalue_ptr = nullptr; - if (SWIG_ConvertPtr(result.get(), (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) - return nullptr; + lldb::SBValue *sbvalue_ptr = nullptr; + if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr, + SWIGTYPE_p_lldb__SBValue, 0) == -1) + return nullptr; - if (sbvalue_ptr == nullptr) - return nullptr; + if (sbvalue_ptr == nullptr) + return nullptr; - return result.release(); + return result.release(); } -int -lldb_private::LLDBSwigPython_GetIndexOfChildWithName -( - PyObject *implementor, - const char* child_name -) -{ - PyErr_Cleaner py_err_cleaner(true); +int lldb_private::LLDBSwigPython_GetIndexOfChildWithName( + PyObject * implementor, const char *child_name) { + PyErr_Cleaner py_err_cleaner(true); - PythonObject self(PyRefType::Borrowed, implementor); - auto pfunc = self.ResolveName("get_child_index"); + PythonObject self(PyRefType::Borrowed, implementor); + auto pfunc = self.ResolveName("get_child_index"); - if (!pfunc.IsAllocated()) - return UINT32_MAX; + if (!pfunc.IsAllocated()) + return UINT32_MAX; - llvm::Expected result = pfunc.Call(PythonString(child_name)); + llvm::Expected result = pfunc.Call(PythonString(child_name)); - long long retval = unwrapOrSetPythonException(As(std::move(result))); + long long retval = + unwrapOrSetPythonException(As(std::move(result))); - if (PyErr_Occurred()) { - PyErr_Clear(); // FIXME print this? do something else - return UINT32_MAX; - } + if (PyErr_Occurred()) { + PyErr_Clear(); // FIXME print this? do something else + return UINT32_MAX; + } - if (retval >= 0) - return (uint32_t)retval; + if (retval >= 0) + return (uint32_t)retval; - return UINT32_MAX; + return UINT32_MAX; } -bool -lldb_private::LLDBSwigPython_UpdateSynthProviderInstance -( - PyObject *implementor -) -{ - bool ret_val = false; +bool lldb_private::LLDBSwigPython_UpdateSynthProviderInstance(PyObject * + implementor) { + bool ret_val = false; - static char callee_name[] = "update"; + static char callee_name[] = "update"; - PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name); + PyObject *py_return = + LLDBSwigPython_CallOptionalMember(implementor, callee_name); - if (py_return == Py_True) - ret_val = true; + if (py_return == Py_True) + ret_val = true; - Py_XDECREF(py_return); + Py_XDECREF(py_return); - return ret_val; + return ret_val; } -bool -lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance -( - PyObject *implementor -) -{ - bool ret_val = false; +bool lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance( + PyObject * implementor) { + bool ret_val = false; - static char callee_name[] = "has_children"; + static char callee_name[] = "has_children"; - PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True); + PyObject *py_return = + LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True); - if (py_return == Py_True) - ret_val = true; + if (py_return == Py_True) + ret_val = true; - Py_XDECREF(py_return); + Py_XDECREF(py_return); - return ret_val; + return ret_val; } -PyObject* -lldb_private::LLDBSwigPython_GetValueSynthProviderInstance -( - PyObject *implementor -) -{ - PyObject* ret_val = nullptr; +PyObject *lldb_private::LLDBSwigPython_GetValueSynthProviderInstance( + PyObject * implementor) { + PyObject *ret_val = nullptr; - static char callee_name[] = "get_value"; + static char callee_name[] = "get_value"; - PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None); + PyObject *py_return = + LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None); - if (py_return == Py_None || py_return == nullptr) - ret_val = nullptr; + if (py_return == Py_None || py_return == nullptr) + ret_val = nullptr; - lldb::SBValue* sbvalue_ptr = NULL; + lldb::SBValue *sbvalue_ptr = NULL; - if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) - ret_val = nullptr; - else if (sbvalue_ptr == NULL) - ret_val = nullptr; - else - ret_val = py_return; + if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr, + SWIGTYPE_p_lldb__SBValue, 0) == -1) + ret_val = nullptr; + else if (sbvalue_ptr == NULL) + ret_val = nullptr; + else + ret_val = py_return; - Py_XDECREF(py_return); - return ret_val; + Py_XDECREF(py_return); + return ret_val; } -void* -lldb_private::LLDBSWIGPython_CastPyObjectToSBData -( - PyObject* data -) -{ - lldb::SBData* sb_ptr = nullptr; +void *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) { + lldb::SBData *sb_ptr = nullptr; - int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0); + int valid_cast = + SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0); - if (valid_cast == -1) - return NULL; + if (valid_cast == -1) + return NULL; - return sb_ptr; + return sb_ptr; } +void *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) { + lldb::SBError *sb_ptr = nullptr; -void* -lldb_private::LLDBSWIGPython_CastPyObjectToSBError -( - PyObject* data -) -{ - lldb::SBError* sb_ptr = nullptr; - - int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0); + int valid_cast = + SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0); - if (valid_cast == -1) - return NULL; + if (valid_cast == -1) + return NULL; - return sb_ptr; + return sb_ptr; } +void *lldb_private::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) { + lldb::SBValue *sb_ptr = NULL; -void* -lldb_private::LLDBSWIGPython_CastPyObjectToSBValue -( - PyObject* data -) -{ - lldb::SBValue* sb_ptr = NULL; - - int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); + int valid_cast = + SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); - if (valid_cast == -1) - return NULL; + if (valid_cast == -1) + return NULL; - return sb_ptr; + return sb_ptr; } -void* -lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo -( - PyObject* data -) -{ - lldb::SBMemoryRegionInfo* sb_ptr = NULL; +void *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject * + data) { + lldb::SBMemoryRegionInfo *sb_ptr = NULL; - int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0); + int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr, + SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0); - if (valid_cast == -1) - return NULL; + if (valid_cast == -1) + return NULL; - return sb_ptr; + return sb_ptr; } -bool -lldb_private::LLDBSwigPythonCallCommand -( - const char *python_function_name, - const char *session_dictionary_name, - lldb::DebuggerSP& debugger, - const char* args, - lldb_private::CommandReturnObject& cmd_retobj, - lldb::ExecutionContextRefSP exe_ctx_ref_sp -) -{ - lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); - lldb::SBDebugger debugger_sb(debugger); - lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); - - PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name, dict); - - if (!pfunc.IsAllocated()) - return false; - - auto argc = pfunc.GetArgInfo(); - if (!argc) { - llvm::consumeError(argc.takeError()); - return false; - } - 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)); - - if (argc.get().max_positional_args < 5u) - pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict); - else - pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict); +bool lldb_private::LLDBSwigPythonCallCommand( + const char *python_function_name, const char *session_dictionary_name, + lldb::DebuggerSP &debugger, const char *args, + lldb_private::CommandReturnObject &cmd_retobj, + lldb::ExecutionContextRefSP exe_ctx_ref_sp) { + lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); + lldb::SBDebugger debugger_sb(debugger); + lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); + + PyErr_Cleaner py_err_cleaner(true); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); + + if (!pfunc.IsAllocated()) + return false; - return true; + auto argc = pfunc.GetArgInfo(); + if (!argc) { + llvm::consumeError(argc.takeError()); + return false; + } + 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)); + + if (argc.get().max_positional_args < 5u) + pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict); + else + pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict); + + return true; } -bool -lldb_private::LLDBSwigPythonCallCommandObject -( - PyObject *implementor, - lldb::DebuggerSP& debugger, - const char* args, - lldb_private::CommandReturnObject& cmd_retobj, - lldb::ExecutionContextRefSP exe_ctx_ref_sp -) -{ - lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); - lldb::SBDebugger debugger_sb(debugger); - lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); +bool lldb_private::LLDBSwigPythonCallCommandObject( + PyObject * implementor, lldb::DebuggerSP & debugger, const char *args, + lldb_private::CommandReturnObject &cmd_retobj, + lldb::ExecutionContextRefSP exe_ctx_ref_sp) { + lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj); + lldb::SBDebugger debugger_sb(debugger); + lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - PythonObject self(PyRefType::Borrowed, implementor); - auto pfunc = self.ResolveName("__call__"); + PythonObject self(PyRefType::Borrowed, implementor); + auto pfunc = self.ResolveName("__call__"); - if (!pfunc.IsAllocated()) - return false; + if (!pfunc.IsAllocated()) + return false; - 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 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)); - pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg); + pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg); - return true; + return true; } -void* -lldb_private::LLDBSWIGPythonCreateOSPlugin -( - const char *python_class_name, - const char *session_dictionary_name, - const lldb::ProcessSP& process_sp -) -{ - if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) - Py_RETURN_NONE; +void *lldb_private::LLDBSWIGPythonCreateOSPlugin( + const char *python_class_name, const char *session_dictionary_name, + const lldb::ProcessSP &process_sp) { + if (python_class_name == NULL || python_class_name[0] == '\0' || + !session_dictionary_name) + Py_RETURN_NONE; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_class_name, dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); - if (!pfunc.IsAllocated()) - Py_RETURN_NONE; + if (!pfunc.IsAllocated()) + Py_RETURN_NONE; - auto result = pfunc(ToSWIGWrapper(process_sp)); + auto result = pfunc(ToSWIGWrapper(process_sp)); - if (result.IsAllocated()) - return result.release(); + if (result.IsAllocated()) + return result.release(); - Py_RETURN_NONE; + Py_RETURN_NONE; } -void* -lldb_private::LLDBSWIGPython_CreateFrameRecognizer -( - const char *python_class_name, - const char *session_dictionary_name -) -{ - if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) - Py_RETURN_NONE; +void *lldb_private::LLDBSWIGPython_CreateFrameRecognizer( + const char *python_class_name, const char *session_dictionary_name) { + if (python_class_name == NULL || python_class_name[0] == '\0' || + !session_dictionary_name) + Py_RETURN_NONE; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_class_name, dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_class_name, dict); - if (!pfunc.IsAllocated()) - Py_RETURN_NONE; + if (!pfunc.IsAllocated()) + Py_RETURN_NONE; - auto result = pfunc(); + auto result = pfunc(); - if (result.IsAllocated()) - return result.release(); + if (result.IsAllocated()) + return result.release(); - Py_RETURN_NONE; + Py_RETURN_NONE; } -PyObject* -lldb_private::LLDBSwigPython_GetRecognizedArguments -( - PyObject *implementor, - const lldb::StackFrameSP& frame_sp -) -{ - static char callee_name[] = "get_recognized_arguments"; +PyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments( + PyObject * implementor, const lldb::StackFrameSP &frame_sp) { + static char callee_name[] = "get_recognized_arguments"; - lldb::SBFrame frame_sb(frame_sp); - PyObject *arg = SBTypeToSWIGWrapper(frame_sb); + lldb::SBFrame frame_sb(frame_sp); + PyObject *arg = SBTypeToSWIGWrapper(frame_sb); - PythonString str(callee_name); - PyObject* result = PyObject_CallMethodObjArgs(implementor, str.get(), arg, - NULL); - return result; + PythonString str(callee_name); + PyObject *result = + PyObject_CallMethodObjArgs(implementor, str.get(), arg, NULL); + return result; } -void* -lldb_private::LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp) -{ - if (!module || !setting) - Py_RETURN_NONE; +void *lldb_private::LLDBSWIGPython_GetDynamicSetting( + void *module, const char *setting, const lldb::TargetSP &target_sp) { + if (!module || !setting) + Py_RETURN_NONE; - PyErr_Cleaner py_err_cleaner(true); - PythonObject py_module(PyRefType::Borrowed, (PyObject *)module); - auto pfunc = py_module.ResolveName("get_dynamic_setting"); + PyErr_Cleaner py_err_cleaner(true); + PythonObject py_module(PyRefType::Borrowed, (PyObject *)module); + auto pfunc = py_module.ResolveName("get_dynamic_setting"); - if (!pfunc.IsAllocated()) - Py_RETURN_NONE; + if (!pfunc.IsAllocated()) + Py_RETURN_NONE; - auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting)); + auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting)); - return result.release(); + return result.release(); } bool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess( const char *python_function_name, const char *session_dictionary_name, const lldb::ProcessSP &process, std::string &output) { - if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) - return false; + if (python_function_name == NULL || python_function_name[0] == '\0' || + !session_dictionary_name) + return false; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name, dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); - if (!pfunc.IsAllocated()) - return false; + if (!pfunc.IsAllocated()) + return false; - auto result = pfunc(ToSWIGWrapper(process), dict); + auto result = pfunc(ToSWIGWrapper(process), dict); - output = result.Str().GetString().str(); + output = result.Str().GetString().str(); - return true; + return true; } -bool -lldb_private::LLDBSWIGPythonRunScriptKeywordThread -(const char* python_function_name, -const char* session_dictionary_name, -lldb::ThreadSP& thread, -std::string& output) +bool lldb_private::LLDBSWIGPythonRunScriptKeywordThread( + const char *python_function_name, const char *session_dictionary_name, + lldb::ThreadSP &thread, std::string &output) { - if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) - return false; + if (python_function_name == NULL || python_function_name[0] == '\0' || + !session_dictionary_name) + return false; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name, dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); - if (!pfunc.IsAllocated()) - return false; + if (!pfunc.IsAllocated()) + return false; - lldb::SBThread thread_sb(thread); - PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb)); - auto result = pfunc(thread_arg, dict); + lldb::SBThread thread_sb(thread); + PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb)); + auto result = pfunc(thread_arg, dict); - output = result.Str().GetString().str(); + output = result.Str().GetString().str(); - return true; + return true; } bool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget( const char *python_function_name, const char *session_dictionary_name, const lldb::TargetSP &target, std::string &output) { - if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) - return false; + if (python_function_name == NULL || python_function_name[0] == '\0' || + !session_dictionary_name) + return false; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name,dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); - if (!pfunc.IsAllocated()) - return false; + if (!pfunc.IsAllocated()) + return false; - auto result = pfunc(ToSWIGWrapper(target), dict); + auto result = pfunc(ToSWIGWrapper(target), dict); - output = result.Str().GetString().str(); + output = result.Str().GetString().str(); - return true; + return true; } -bool -lldb_private::LLDBSWIGPythonRunScriptKeywordFrame -(const char* python_function_name, -const char* session_dictionary_name, -lldb::StackFrameSP& frame, -std::string& output) +bool lldb_private::LLDBSWIGPythonRunScriptKeywordFrame( + const char *python_function_name, const char *session_dictionary_name, + lldb::StackFrameSP &frame, std::string &output) { - if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) - return false; + if (python_function_name == NULL || python_function_name[0] == '\0' || + !session_dictionary_name) + return false; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name,dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); - if (!pfunc.IsAllocated()) - return false; + if (!pfunc.IsAllocated()) + return false; - lldb::SBFrame frame_sb(frame); - PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb)); - auto result = pfunc(frame_arg, dict); + lldb::SBFrame frame_sb(frame); + PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb)); + auto result = pfunc(frame_arg, dict); - output = result.Str().GetString().str(); + output = result.Str().GetString().str(); - return true; + return true; } bool lldb_private::LLDBSWIGPythonRunScriptKeywordValue( const char *python_function_name, const char *session_dictionary_name, const lldb::ValueObjectSP &value, std::string &output) { - if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name) - return false; + if (python_function_name == NULL || python_function_name[0] == '\0' || + !session_dictionary_name) + return false; - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name, dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); - if (!pfunc.IsAllocated()) - return false; + if (!pfunc.IsAllocated()) + return false; - auto result = pfunc(ToSWIGWrapper(value), dict); + auto result = pfunc(ToSWIGWrapper(value), dict); - output = result.Str().GetString().str(); + output = result.Str().GetString().str(); - return true; + return true; } -bool -lldb_private::LLDBSwigPythonCallModuleInit -( - const char *python_module_name, - const char *session_dictionary_name, - lldb::DebuggerSP& debugger -) -{ - std::string python_function_name_string = python_module_name; - python_function_name_string += ".__lldb_init_module"; - const char* python_function_name = python_function_name_string.c_str(); +bool lldb_private::LLDBSwigPythonCallModuleInit( + const char *python_module_name, const char *session_dictionary_name, + lldb::DebuggerSP &debugger) { + std::string python_function_name_string = python_module_name; + python_function_name_string += ".__lldb_init_module"; + const char *python_function_name = python_function_name_string.c_str(); - PyErr_Cleaner py_err_cleaner(true); + PyErr_Cleaner py_err_cleaner(true); - auto dict = PythonModule::MainModule().ResolveName(session_dictionary_name); - auto pfunc = PythonObject::ResolveNameWithDictionary(python_function_name, dict); + auto dict = PythonModule::MainModule().ResolveName( + session_dictionary_name); + auto pfunc = PythonObject::ResolveNameWithDictionary( + python_function_name, dict); - // This method is optional and need not exist. So if we don't find it, - // it's actually a success, not a failure. - if (!pfunc.IsAllocated()) - return true; + // This method is optional and need not exist. So if we don't find it, + // it's actually a success, not a failure. + if (!pfunc.IsAllocated()) + return true; - lldb::SBDebugger debugger_sb(debugger); - PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); - pfunc(debugger_arg, dict); + lldb::SBDebugger debugger_sb(debugger); + PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb)); + pfunc(debugger_arg, dict); - return true; + return true; } -lldb::ValueObjectSP -lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data) -{ - lldb::ValueObjectSP valobj_sp; - if (data) - { - lldb::SBValue* sb_ptr = (lldb::SBValue *)data; - valobj_sp = sb_ptr->GetSP(); - } - return valobj_sp; +lldb::ValueObjectSP lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue( + void *data) { + lldb::ValueObjectSP valobj_sp; + if (data) { + lldb::SBValue *sb_ptr = (lldb::SBValue *)data; + valobj_sp = sb_ptr->GetSP(); + } + return valobj_sp; } // For the LogOutputCallback functions -static void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) { - if (baton != Py_None) { - SWIG_PYTHON_THREAD_BEGIN_BLOCK; - PyObject *result = PyObject_CallFunction(reinterpret_cast(baton), const_cast("s"), str); - Py_XDECREF(result); - SWIG_PYTHON_THREAD_END_BLOCK; - } +static void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, + void *baton) { + if (baton != Py_None) { + SWIG_PYTHON_THREAD_BEGIN_BLOCK; + PyObject *result = PyObject_CallFunction( + reinterpret_cast(baton), const_cast("s"), str); + Py_XDECREF(result); + SWIG_PYTHON_THREAD_END_BLOCK; + } } %}