From 9fe00e52d3dc12e80428648d84542f800c362163 Mon Sep 17 00:00:00 2001 From: Enrico Granata Date: Fri, 13 Mar 2015 02:20:41 +0000 Subject: Bulk of the infrastructure work to allow script commands to be backed by object instances in addition to free functions This works by creating a command backed by a class whose interface should - at least - include def __init__(self, debugger, session_dict) def __call__(self, args, return_obj, exe_ctx) What works: - adding a command via command script add --class - calling a thusly created command What is missing: - support for custom help - test cases The missing parts will follow over the next couple of days This is an improvement over the existing system as: a) it provides an obvious location for commands to provide help strings (i.e. methods) b) it allows commands to store state in an obvious fashion c) it allows us to easily add features to script commands over time (option parsing and subcommands registration, I am looking at you :-) llvm-svn: 232136 --- lldb/scripts/Python/python-wrapper.swig | 78 +++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'lldb/scripts/Python') diff --git a/lldb/scripts/Python/python-wrapper.swig b/lldb/scripts/Python/python-wrapper.swig index 84375fd1547..57291bc15a8 100644 --- a/lldb/scripts/Python/python-wrapper.swig +++ b/lldb/scripts/Python/python-wrapper.swig @@ -452,6 +452,44 @@ LLDBSwigPythonCreateSyntheticProvider Py_RETURN_NONE; } +SWIGEXPORT void* +LLDBSwigPythonCreateCommandObject +( + const char *python_class_name, + const char *session_dictionary_name, + const lldb::DebuggerSP debugger_sp +) +{ + PyObject* retval = NULL; + + if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name) + Py_RETURN_NONE; + + lldb::SBDebugger debugger_sb(debugger_sp); + + { + PyErr_Cleaner py_err_cleaner(true); + + PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name); + + if (!pfunc) + return retval; + + PyObject* session_dict = NULL; + session_dict = FindSessionDictionary(session_dictionary_name); + retval = pfunc(debugger_sb, session_dict); + + Py_XINCREF (session_dict); + + Py_XINCREF(retval); + } + + if (retval) + return retval; + else + Py_RETURN_NONE; +} + SWIGEXPORT void* LLDBSwigPythonCreateScriptedThreadPlan ( @@ -845,6 +883,46 @@ LLDBSwigPythonCallCommand return retval; } +SWIGEXPORT bool +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); + SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb); + lldb::SBDebugger debugger_sb(debugger); + lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp); + + bool retval = false; + + { + PyErr_Cleaner py_err_cleaner(true); + + PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"__call__"); + + if (!pfunc) + return NULL; + + // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you + // see comment above for SBCommandReturnObjectReleaser for further details + PyObject* pvalue = NULL; + + pvalue = pfunc(debugger_sb, args, exe_ctx_sb, &cmd_retobj_sb); + + Py_XDECREF (pvalue); + + retval = true; + } + + return retval; +} + SWIGEXPORT void* LLDBSWIGPythonCreateOSPlugin ( -- cgit v1.2.3