[Lldb-commits] [lldb] r162513 - in /lldb/trunk: include/lldb/API/SBProcess.h include/lldb/Interpreter/ScriptInterpreter.h include/lldb/Interpreter/ScriptInterpreterPython.h scripts/Python/python-wrapper.swig source/Interpreter/ScriptInterpreterPython.cpp source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp source/Plugins/OperatingSystem/Python/OperatingSystemPython.h

Enrico Granata egranata at apple.com
Thu Aug 23 17:30:48 PDT 2012


Author: enrico
Date: Thu Aug 23 19:30:47 2012
New Revision: 162513

URL: http://llvm.org/viewvc/llvm-project?rev=162513&view=rev
Log:
Adding bindings to the Script Interpreter for some basic Python OS plugin functionality (still WIP)

Modified:
    lldb/trunk/include/lldb/API/SBProcess.h
    lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h
    lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
    lldb/trunk/scripts/Python/python-wrapper.swig
    lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
    lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp
    lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.h

Modified: lldb/trunk/include/lldb/API/SBProcess.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBProcess.h?rev=162513&r1=162512&r2=162513&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBProcess.h (original)
+++ lldb/trunk/include/lldb/API/SBProcess.h Thu Aug 23 19:30:47 2012
@@ -40,6 +40,8 @@
     const lldb::SBProcess&
     operator = (const lldb::SBProcess& rhs);
 
+    SBProcess (const lldb::ProcessSP &process_sp);
+    
     ~SBProcess();
 
     static const char *
@@ -217,8 +219,6 @@
     friend class SBThread;
     friend class SBValue;
 
-    SBProcess (const lldb::ProcessSP &process_sp);
-
     lldb::ProcessSP
     GetSP() const;
     

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h?rev=162513&r1=162512&r2=162513&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h Thu Aug 23 19:30:47 2012
@@ -82,6 +82,10 @@
     typedef void* (*SWIGPythonCreateSyntheticProvider) (const std::string python_class_name,
                                                         const char *session_dictionary_name,
                                                         const lldb::ValueObjectSP& valobj_sp);
+
+    typedef void* (*SWIGPythonCreateOSPlugin) (const std::string python_class_name,
+                                               const char *session_dictionary_name,
+                                               const lldb::ProcessSP& process_sp);
     
     typedef uint32_t       (*SWIGPythonCalculateNumChildren)        (void *implementor);
     typedef void*          (*SWIGPythonGetChildAtIndex)             (void *implementor, uint32_t idx);
@@ -195,6 +199,19 @@
         return lldb::ScriptInterpreterObjectSP();
     }
     
+    virtual lldb::ScriptInterpreterObjectSP
+    CreateOSPlugin (std::string class_name,
+                    lldb::ProcessSP process_sp)
+    {
+        return lldb::ScriptInterpreterObjectSP();
+    }
+    
+    virtual lldb::ScriptInterpreterObjectSP
+    OSPlugin_QueryForRegisterInfo (lldb::ScriptInterpreterObjectSP object)
+    {
+        return lldb::ScriptInterpreterObjectSP();
+    }
+    
     virtual bool
     GenerateFunction(const char *signature, const StringList &input)
     {

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h?rev=162513&r1=162512&r2=162513&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h Thu Aug 23 19:30:47 2012
@@ -76,6 +76,13 @@
     CreateSyntheticScriptedProvider (std::string class_name,
                                      lldb::ValueObjectSP valobj);
     
+    virtual lldb::ScriptInterpreterObjectSP
+    CreateOSPlugin (std::string class_name,
+                    lldb::ProcessSP process_sp);
+    
+    virtual lldb::ScriptInterpreterObjectSP
+    OSPlugin_QueryForRegisterInfo (lldb::ScriptInterpreterObjectSP object);
+    
     virtual uint32_t
     CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor);
     

Modified: lldb/trunk/scripts/Python/python-wrapper.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-wrapper.swig?rev=162513&r1=162512&r2=162513&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-wrapper.swig (original)
+++ lldb/trunk/scripts/Python/python-wrapper.swig Thu Aug 23 19:30:47 2012
@@ -742,6 +742,103 @@
     return retval;
 }
 
+SWIGEXPORT void*
+LLDBSWIGPythonCreateOSPlugin
+(
+    const std::string python_class_name,
+    const char *session_dictionary_name,
+    const lldb::ProcessSP& process_sp
+)
+{
+    PyObject* retval = NULL;
+
+    if (python_class_name.empty() || !session_dictionary_name)
+        Py_RETURN_NONE;
+
+    // I do not want the SBValue to be deallocated when going out of scope because python
+    // has ownership of it and will manage memory for this object by itself
+    lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
+
+    PyObject *SBProc_PyObj = SWIG_NewPointerObj((void *)process_sb, SWIGTYPE_p_lldb__SBProcess, 0);
+
+    if (SBProc_PyObj == NULL)
+        Py_RETURN_NONE;
+
+    const char* python_function_name = python_class_name.c_str();
+
+    PyObject *session_dict, *pfunc;
+    PyObject *pvalue;
+    
+    session_dict = FindSessionDictionary (session_dictionary_name);
+    if (session_dict != NULL)
+    {
+        pfunc = ResolvePythonName (python_function_name, session_dict);
+        if (pfunc != NULL)
+        {
+            // Set up the arguments and call the function.
+                
+            if (PyCallable_Check (pfunc))
+            {
+                PyObject *argList = Py_BuildValue("S", SBProc_PyObj);
+
+                if (PyErr_Occurred ())
+                {
+                    PyErr_Print();
+                    PyErr_Clear();
+                    return retval;
+                }
+
+                if (argList == NULL)
+                {
+                    return retval;
+                }
+
+                Py_INCREF(SBProc_PyObj);
+
+                pvalue = PyObject_CallObject(pfunc, argList);
+
+                Py_DECREF(argList);
+
+                if (pvalue != NULL)
+                {
+                    if (pvalue != Py_None)
+                        retval = pvalue;
+                    else
+                    {
+                        retval = Py_None;
+                        Py_INCREF(retval);
+                    }
+                }
+                else if (PyErr_Occurred ())
+                {
+                    PyErr_Print();
+                    PyErr_Clear();
+                }
+                Py_INCREF (session_dict);
+            }
+            else if (PyErr_Occurred())
+            {
+                PyErr_Print();
+                PyErr_Clear();
+            }
+        }
+        else if (PyErr_Occurred())
+        {
+            PyErr_Print();
+            PyErr_Clear();
+        }
+    }
+    else if (PyErr_Occurred ())
+    {
+        PyErr_Print();
+        PyErr_Clear ();
+    }
+    if (retval)
+        return retval;
+    else
+        Py_RETURN_NONE;
+}
+
 SWIGEXPORT bool
 LLDBSwigPythonCallModuleInit 
 (

Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=162513&r1=162512&r2=162513&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Thu Aug 23 19:30:47 2012
@@ -55,6 +55,7 @@
 static ScriptInterpreter::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = NULL;
 static ScriptInterpreter::SWIGPythonCallCommand g_swig_call_command = NULL;
 static ScriptInterpreter::SWIGPythonCallModuleInit g_swig_call_module_init = NULL;
+static ScriptInterpreter::SWIGPythonCreateOSPlugin g_swig_create_os_plugin = NULL;
 
 // these are the Pythonic implementations of the required callbacks
 // these are scripting-language specific, which is why they belong here
@@ -121,6 +122,13 @@
  lldb::DebuggerSP& debugger
  );
 
+extern "C" void*        LLDBSWIGPythonCreateOSPlugin
+(
+ const std::string python_class_name,
+ const char *session_dictionary_name,
+ const lldb::ProcessSP& process_sp
+);
+
 static int
 _check_and_flush (FILE *stream)
 {
@@ -1697,6 +1705,85 @@
 }
 
 lldb::ScriptInterpreterObjectSP
+ScriptInterpreterPython::CreateOSPlugin (std::string class_name,
+                lldb::ProcessSP process_sp)
+{
+    if (class_name.empty())
+        return lldb::ScriptInterpreterObjectSP();
+    
+    if (!process_sp)
+        return lldb::ScriptInterpreterObjectSP();
+        
+    void* ret_val;
+    
+    {
+        Locker py_lock(this);
+        ret_val = g_swig_create_os_plugin    (class_name,
+                                              m_dictionary_name.c_str(),
+                                              process_sp);
+    }
+    
+    return MakeScriptObject(ret_val);
+}
+
+lldb::ScriptInterpreterObjectSP
+ScriptInterpreterPython::OSPlugin_QueryForRegisterInfo (lldb::ScriptInterpreterObjectSP object)
+{
+    static char callee_name[] = "get_register_info";
+    
+    if (!object)
+        return lldb::ScriptInterpreterObjectSP();
+    
+    PyObject* implementor = (PyObject*)object->GetObject();
+    
+    if (implementor == NULL || implementor == Py_None)
+        return lldb::ScriptInterpreterObjectSP();
+    
+    PyObject* pmeth  = PyObject_GetAttrString(implementor, callee_name);
+    
+    if (PyErr_Occurred())
+    {
+        PyErr_Clear();
+    }
+    
+    if (pmeth == NULL || pmeth == Py_None)
+    {
+        Py_XDECREF(pmeth);
+        return lldb::ScriptInterpreterObjectSP();
+    }
+    
+    if (PyCallable_Check(pmeth) == 0)
+    {
+        if (PyErr_Occurred())
+        {
+            PyErr_Clear();
+        }
+        
+        Py_XDECREF(pmeth);
+        return lldb::ScriptInterpreterObjectSP();
+    }
+    
+    if (PyErr_Occurred())
+    {
+        PyErr_Clear();
+    }
+    
+    Py_XDECREF(pmeth);
+    
+    // right now we know this function exists and is callable..
+    PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
+    
+    // if it fails, print the error but otherwise go on
+    if (PyErr_Occurred())
+    {
+        PyErr_Print();
+        PyErr_Clear();
+    }
+    
+    return MakeScriptObject(py_return);
+}
+
+lldb::ScriptInterpreterObjectSP
 ScriptInterpreterPython::CreateSyntheticScriptedProvider (std::string class_name,
                                                           lldb::ValueObjectSP valobj)
 {
@@ -2366,6 +2453,7 @@
     g_swig_update_provider = LLDBSwigPython_UpdateSynthProviderInstance;
     g_swig_call_command = LLDBSwigPythonCallCommand;
     g_swig_call_module_init = LLDBSwigPythonCallModuleInit;
+    g_swig_create_os_plugin = LLDBSWIGPythonCreateOSPlugin;
 }
 
 void

Modified: lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp?rev=162513&r1=162512&r2=162513&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp (original)
+++ lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp Thu Aug 23 19:30:47 2012
@@ -16,10 +16,13 @@
 
 #include "lldb/Core/ArchSpec.h"
 #include "lldb/Core/DataBufferHeap.h"
+#include "lldb/Core/Debugger.h"
 #include "lldb/Core/Module.h"
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/RegisterValue.h"
 #include "lldb/Core/ValueObjectVariable.h"
+#include "lldb/Interpreter/CommandInterpreter.h"
+#include "lldb/Interpreter/PythonDataObjects.h"
 #include "lldb/Symbol/ClangNamespaceDecl.h"
 #include "lldb/Symbol/ObjectFile.h"
 #include "lldb/Symbol/VariableList.h"
@@ -53,7 +56,7 @@
 OperatingSystemPython::CreateInstance (Process *process, bool force)
 {
     // Python OperatingSystem plug-ins must be requested by name, so force must be true
-    if (force)
+    //if (force)
         return new OperatingSystemPython (process);
     return NULL;
 }
@@ -75,14 +78,23 @@
 OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process) :
     OperatingSystem (process),
     m_thread_list_valobj_sp (),
-    m_register_info_ap ()
-{
-    // TODO: python: create a new python class the implements the necessary
-    // python class that will cache a SBProcess that contains the "process"
-    // argument above and implements:
-    // dict get_thread_info()
-    // dict get_register_info()
-    // Bytes get_register_context_data(SBThread thread)
+    m_register_info_ap (),
+    m_interpreter(NULL),
+    m_python_object(NULL)
+{
+    if (!process)
+        return;
+    lldb::TargetSP target_sp = process->CalculateTarget();
+    if (!target_sp)
+        return;
+    m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
+    if (m_interpreter)
+    {
+        // TODO: hardcoded is not good
+        auto object_sp = m_interpreter->CreateOSPlugin("operating_system.PlugIn",process->CalculateProcess());
+        if (object_sp)
+            m_python_object = object_sp->GetObject();
+    }
 }
 
 OperatingSystemPython::~OperatingSystemPython ()
@@ -92,10 +104,17 @@
 DynamicRegisterInfo *
 OperatingSystemPython::GetDynamicRegisterInfo ()
 {
-    // TODO: python: call get_register_info() on the python object that
-    // represents our instance of the OperatingSystem plug-in
+    if (!m_interpreter || !m_python_object)
+        return NULL;
+    auto object_sp = m_interpreter->OSPlugin_QueryForRegisterInfo(m_interpreter->MakeScriptObject(m_python_object));
+    if (!object_sp)
+        return NULL;
+    PythonDataObject dictionary_data_obj((PyObject*)object_sp->GetObject());
+    PythonDataDictionary dictionary = dictionary_data_obj.GetDictionaryObject();
+    if(!dictionary)
+        return NULL;
     
-    // Example code below shows creating a new DynamicRegisterInfo()
+    // TODO: iterate over the dictionary
     if (m_register_info_ap.get() == NULL && m_thread_list_valobj_sp)
     {
 //        static ConstString g_gpr_member_name("gpr");

Modified: lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.h?rev=162513&r1=162512&r2=162513&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.h (original)
+++ lldb/trunk/source/Plugins/OperatingSystem/Python/OperatingSystemPython.h Thu Aug 23 19:30:47 2012
@@ -14,6 +14,7 @@
 // C Includes
 // C++ Includes
 // Other libraries and framework includes
+#include "lldb/Interpreter/ScriptInterpreter.h"
 #include "lldb/Target/OperatingSystem.h"
 
 class DynamicRegisterInfo;
@@ -82,6 +83,8 @@
 
     lldb::ValueObjectSP m_thread_list_valobj_sp;
     std::auto_ptr<DynamicRegisterInfo> m_register_info_ap;
+    lldb_private::ScriptInterpreter *m_interpreter;
+    void* m_python_object;
     
 };
 





More information about the lldb-commits mailing list