diff options
author | Enrico Granata <granata.enrico@gmail.com> | 2011-10-17 21:45:27 +0000 |
---|---|---|
committer | Enrico Granata <granata.enrico@gmail.com> | 2011-10-17 21:45:27 +0000 |
commit | a9dbf4325e9b97ca4c68baf4d05fad55bd194332 (patch) | |
tree | fc59812edc526ec91c28e8fbddc6ca5c32e1833b /lldb/scripts/Python | |
parent | a5748e22e2c997ae5fbe4aa3b9170e7a12375e26 (diff) | |
download | bcm5719-llvm-a9dbf4325e9b97ca4c68baf4d05fad55bd194332.tar.gz bcm5719-llvm-a9dbf4325e9b97ca4c68baf4d05fad55bd194332.zip |
this patch introduces a new command script import command which takes as input a filename for a Python script and imports the module contained in that file. the containing directory is added to the Python path such that dependencies are honored. also, the module may contain an __lldb_init_module(debugger,dict) function, which gets called after importing, and which can somehow initialize the module's interaction with lldb
llvm-svn: 142283
Diffstat (limited to 'lldb/scripts/Python')
-rw-r--r-- | lldb/scripts/Python/python-wrapper.swig | 84 |
1 files changed, 83 insertions, 1 deletions
diff --git a/lldb/scripts/Python/python-wrapper.swig b/lldb/scripts/Python/python-wrapper.swig index 3417c71ab35..d68c1b9260c 100644 --- a/lldb/scripts/Python/python-wrapper.swig +++ b/lldb/scripts/Python/python-wrapper.swig @@ -676,7 +676,89 @@ LLDBSwigPythonCallCommand PyErr_Print(); PyErr_Clear (); } -return retval; + return retval; +} + +SWIGEXPORT bool +LLDBSwigPythonCallModuleInit +( + const std::string python_module_name, + const char *session_dictionary_name, + lldb::DebuggerSP& debugger +) +{ + + lldb::SBDebugger debugger_sb(debugger); + + bool retval = false; + + PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); + + if (DebuggerObj_PyObj == NULL) + return retval; + + if (!(python_module_name.length()) || !session_dictionary_name) + return retval; + + PyObject *session_dict, *pfunc; + PyObject *pargs, *pvalue; + + session_dict = FindSessionDictionary (session_dictionary_name); + + std::string python_function_name_string = python_module_name + (".__lldb_init_module"); + const char* python_function_name = python_function_name_string.c_str(); + + if (session_dict != NULL) + { + pfunc = ResolvePythonName (python_function_name, session_dict); + + if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that + { + PyErr_Clear(); + return true; + } + + if (pfunc == NULL) + return true; + else + { + // Set up the arguments and call the function. + + if (PyCallable_Check (pfunc)) + { + pargs = PyTuple_New (2); + if (pargs == NULL) + { + if (PyErr_Occurred()) + PyErr_Clear(); + return retval; + } + + PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj + PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict + pvalue = PyObject_CallObject (pfunc, pargs); + Py_DECREF (pargs); + + if (PyErr_Occurred ()) + { + PyErr_Print(); + PyErr_Clear(); + } + else + { + retval = true; + Py_XDECREF (pvalue); + } + Py_INCREF (session_dict); + } + else if (PyErr_Occurred()) + { + PyErr_Print(); + PyErr_Clear(); + } + } + } + return retval; } %} |