Excellent!!! Thanks for adding this, Enrico! Can you also add something to the Python Tutorial describing this and why it's a good idea to do it this way?
Jim > On Oct 1, 2014, at 2:47 PM, Enrico Granata <[email protected]> wrote: > > Author: enrico > Date: Wed Oct 1 16:47:29 2014 > New Revision: 218834 > > URL: http://llvm.org/viewvc/llvm-project?rev=218834&view=rev > Log: > Allow Python commands to optionally take an SBExecutionContext argument in > case they need to handle 'where they want to act' separately from the notion > of 'currently-selected entity' that is associated to the debugger. Do this in > an (hopefully) non-breaking way by running an argcount check before passing > in the new argument. Update the test case to also check for this new feature. > www update to follow > > Modified: > lldb/trunk/include/lldb/API/SBExecutionContext.h > lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h > lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h > lldb/trunk/scripts/Python/python-swigsafecast.swig > lldb/trunk/scripts/Python/python-wrapper.swig > lldb/trunk/source/API/SBCommandInterpreter.cpp > lldb/trunk/source/API/SBExecutionContext.cpp > lldb/trunk/source/Commands/CommandObjectCommands.cpp > lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp > lldb/trunk/test/functionalities/command_script/TestCommandScript.py > lldb/trunk/test/functionalities/command_script/py_import > lldb/trunk/test/functionalities/command_script/welcome.py > > Modified: lldb/trunk/include/lldb/API/SBExecutionContext.h > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBExecutionContext.h?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/include/lldb/API/SBExecutionContext.h (original) > +++ lldb/trunk/include/lldb/API/SBExecutionContext.h Wed Oct 1 16:47:29 2014 > @@ -25,6 +25,8 @@ public: > > SBExecutionContext (const lldb::SBExecutionContext &rhs); > > + SBExecutionContext (lldb::ExecutionContextRefSP exe_ctx_ref_sp); > + > SBExecutionContext (const lldb::SBTarget &target); > > SBExecutionContext (const lldb::SBProcess &process); > > Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h (original) > +++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h Wed Oct 1 > 16:47:29 2014 > @@ -127,7 +127,8 @@ public: > const char > *session_dictionary_name, > > lldb::DebuggerSP& debugger, > const char* > args, > - > lldb_private::CommandReturnObject& cmd_retobj); > + > lldb_private::CommandReturnObject& cmd_retobj, > + > lldb::ExecutionContextRefSP exe_ctx_ref_sp); > > typedef bool (*SWIGPythonCallModuleInit) (const char > *python_module_name, > const char > *session_dictionary_name, > @@ -502,7 +503,8 @@ public: > const char* args, > ScriptedCommandSynchronicity synchronicity, > lldb_private::CommandReturnObject& cmd_retobj, > - Error& error) > + Error& error, > + const lldb_private::ExecutionContext& exe_ctx) > { > return false; > } > > Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h (original) > +++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h Wed Oct 1 > 16:47:29 2014 > @@ -146,7 +146,8 @@ public: > const char* args, > ScriptedCommandSynchronicity synchronicity, > lldb_private::CommandReturnObject& cmd_retobj, > - Error& error); > + Error& error, > + const lldb_private::ExecutionContext& exe_ctx); > > Error > GenerateFunction(const char *signature, const StringList &input); > > Modified: lldb/trunk/scripts/Python/python-swigsafecast.swig > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-swigsafecast.swig?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/scripts/Python/python-swigsafecast.swig (original) > +++ lldb/trunk/scripts/Python/python-swigsafecast.swig Wed Oct 1 16:47:29 > 2014 > @@ -126,3 +126,10 @@ SBTypeToSWIGWrapper (lldb::SBCommandRetu > { > return SWIG_NewPointerObj((void *) cmd_ret_obj_sb, > SWIGTYPE_p_lldb__SBCommandReturnObject, 0); > } > + > +template <> > +PyObject* > +SBTypeToSWIGWrapper (lldb::SBExecutionContext* ctx_sb) > +{ > + return SWIG_NewPointerObj((void *) ctx_sb, > SWIGTYPE_p_lldb__SBExecutionContext, 0); > +} > > Modified: lldb/trunk/scripts/Python/python-wrapper.swig > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-wrapper.swig?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/scripts/Python/python-wrapper.swig (original) > +++ lldb/trunk/scripts/Python/python-wrapper.swig Wed Oct 1 16:47:29 2014 > @@ -770,13 +770,15 @@ LLDBSwigPythonCallCommand > const char *session_dictionary_name, > lldb::DebuggerSP& debugger, > const char* args, > - lldb_private::CommandReturnObject& cmd_retobj > + 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; > > @@ -791,7 +793,12 @@ LLDBSwigPythonCallCommand > // 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, &cmd_retobj_sb, session_dict = > FindSessionDictionary(session_dictionary_name)); > + > + PyCallable::argc argc = pfunc.GetNumArguments(); > + if (argc.num_args == 5 || argc.varargs == true) > + pvalue = pfunc(debugger_sb, args, exe_ctx_sb, &cmd_retobj_sb, > session_dict = FindSessionDictionary(session_dictionary_name)); > + else > + pvalue = pfunc(debugger_sb, args, &cmd_retobj_sb, session_dict = > FindSessionDictionary(session_dictionary_name)); > > Py_XINCREF (session_dict); > Py_XDECREF (pvalue); > > Modified: lldb/trunk/source/API/SBCommandInterpreter.cpp > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBCommandInterpreter.cpp?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/source/API/SBCommandInterpreter.cpp (original) > +++ lldb/trunk/source/API/SBCommandInterpreter.cpp Wed Oct 1 16:47:29 2014 > @@ -479,7 +479,8 @@ LLDBSwigPythonCallCommand (const char *p > const char *session_dictionary_name, > lldb::DebuggerSP& debugger, > const char* args, > - lldb_private::CommandReturnObject &cmd_retobj); > + lldb_private::CommandReturnObject &cmd_retobj, > + lldb::ExecutionContextRefSP exe_ctx_ref_sp); > > extern "C" bool > LLDBSwigPythonCallModuleInit (const char *python_module_name, > > Modified: lldb/trunk/source/API/SBExecutionContext.cpp > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBExecutionContext.cpp?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/source/API/SBExecutionContext.cpp (original) > +++ lldb/trunk/source/API/SBExecutionContext.cpp Wed Oct 1 16:47:29 2014 > @@ -29,6 +29,11 @@ m_exe_ctx_sp(rhs.m_exe_ctx_sp) > { > } > > +SBExecutionContext::SBExecutionContext (lldb::ExecutionContextRefSP > exe_ctx_ref_sp) : > +m_exe_ctx_sp(exe_ctx_ref_sp) > +{ > +} > + > SBExecutionContext::SBExecutionContext (const lldb::SBTarget &target) : > m_exe_ctx_sp(new ExecutionContextRef()) > { > > Modified: lldb/trunk/source/Commands/CommandObjectCommands.cpp > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectCommands.cpp?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/source/Commands/CommandObjectCommands.cpp (original) > +++ lldb/trunk/source/Commands/CommandObjectCommands.cpp Wed Oct 1 16:47:29 > 2014 > @@ -1366,7 +1366,8 @@ protected: > raw_command_line, > m_synchro, > result, > - error) == false) > + error, > + m_exe_ctx) == false) > { > result.AppendError(error.AsCString()); > result.SetStatus(eReturnStatusFailed); > > Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original) > +++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Wed Oct 1 > 16:47:29 2014 > @@ -2510,7 +2510,8 @@ ScriptInterpreterPython::RunScriptBasedC > const char* args, > ScriptedCommandSynchronicity > synchronicity, > > lldb_private::CommandReturnObject& cmd_retobj, > - Error& error) > + Error& error, > + const > lldb_private::ExecutionContext& exe_ctx) > { > if (!impl_function) > { > @@ -2525,6 +2526,7 @@ ScriptInterpreterPython::RunScriptBasedC > } > > lldb::DebuggerSP debugger_sp = > m_interpreter.GetDebugger().shared_from_this(); > + lldb::ExecutionContextRefSP exe_ctx_ref_sp(new > ExecutionContextRef(exe_ctx)); > > if (!debugger_sp.get()) > { > @@ -2548,7 +2550,8 @@ ScriptInterpreterPython::RunScriptBasedC > m_dictionary_name.c_str(), > debugger_sp, > args, > - cmd_retobj); > + cmd_retobj, > + exe_ctx_ref_sp); > } > > if (!ret_val) > > Modified: lldb/trunk/test/functionalities/command_script/TestCommandScript.py > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/TestCommandScript.py?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/test/functionalities/command_script/TestCommandScript.py > (original) > +++ lldb/trunk/test/functionalities/command_script/TestCommandScript.py Wed > Oct 1 16:47:29 2014 > @@ -40,6 +40,7 @@ class CmdPythonTestCase(TestBase): > self.runCmd('command script delete tell_async', check=False) > self.runCmd('command script delete tell_curr', check=False) > self.runCmd('command script delete bug11569', check=False) > + self.runCmd('command script delete takes_exe_ctx', check=False) > > # Execute the cleanup function during test case tear down. > self.addTearDownHook(cleanup) > @@ -108,6 +109,9 @@ class CmdPythonTestCase(TestBase): > substrs = ['running async']) > self.expect("tell_curr", > substrs = ['I am running sync']) > + > +# check that the execution context is passed in to commands that ask for it > + self.expect("takes_exe_ctx", substrs = ["a.out"]) > > # Test that a python command can redefine itself > self.expect('command script add -f foobar welcome -h "just some > help"') > > Modified: lldb/trunk/test/functionalities/command_script/py_import > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/py_import?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/test/functionalities/command_script/py_import (original) > +++ lldb/trunk/test/functionalities/command_script/py_import Wed Oct 1 > 16:47:29 2014 > @@ -9,3 +9,4 @@ command script import mysto.py --allow-r > command script add tell_sync --function welcome.check_for_synchro > --synchronicity sync > command script add tell_async --function welcome.check_for_synchro > --synchronicity async > command script add tell_curr --function welcome.check_for_synchro > --synchronicity curr > +command script add takes_exe_ctx --function welcome.takes_exe_ctx > > Modified: lldb/trunk/test/functionalities/command_script/welcome.py > URL: > http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/welcome.py?rev=218834&r1=218833&r2=218834&view=diff > ============================================================================== > --- lldb/trunk/test/functionalities/command_script/welcome.py (original) > +++ lldb/trunk/test/functionalities/command_script/welcome.py Wed Oct 1 > 16:47:29 2014 > @@ -30,3 +30,6 @@ def check_for_synchro(debugger, args, re > if debugger.GetAsync() == False: > print >>result, ('I am running sync') > > +def takes_exe_ctx(debugger, args, exe_ctx, result, dict): > + print >>result, str(exe_ctx.GetTarget()) > + > > > _______________________________________________ > lldb-commits mailing list > [email protected] > http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits _______________________________________________ lldb-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits
