[Lldb-commits] [lldb] r151397 - in /lldb/trunk: include/lldb/Core/Module.h source/Core/Module.cpp source/Target/Process.cpp

Greg Clayton gclayton at apple.com
Fri Feb 24 13:56:00 PST 2012


Author: gclayton
Date: Fri Feb 24 15:55:59 2012
New Revision: 151397

URL: http://llvm.org/viewvc/llvm-project?rev=151397&view=rev
Log:
Fixed a crasher that was happening after making ObjectFile objects have a
weak reference back to the Module. We were crashing when trying to make a
memory object file since it was trying to get the object in the Module 
constructor before the "Module *" had been put into a shared pointer, and the
module was trying to initialize a weak pointer back to it.


Modified:
    lldb/trunk/include/lldb/Core/Module.h
    lldb/trunk/source/Core/Module.cpp
    lldb/trunk/source/Target/Process.cpp

Modified: lldb/trunk/include/lldb/Core/Module.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Module.h?rev=151397&r1=151396&r2=151397&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/Module.h (original)
+++ lldb/trunk/include/lldb/Core/Module.h Fri Feb 24 15:55:59 2012
@@ -96,9 +96,6 @@
             const ConstString *object_name = NULL,
             off_t object_offset = 0);
 
-    Module (const FileSpec& file_spec, 
-            const lldb::ProcessSP &processSP,
-            lldb::addr_t header_addr);
     //------------------------------------------------------------------
     /// Destructor.
     //------------------------------------------------------------------
@@ -543,6 +540,11 @@
     ObjectFile *
     GetObjectFile ();
 
+    // Load an object file from memory.
+    ObjectFile *
+    GetMemoryObjectFile (const lldb::ProcessSP &process_sp, 
+                         lldb::addr_t header_addr,
+                         Error &error);
     //------------------------------------------------------------------
     /// Get the symbol vendor interface for the current architecture.
     ///

Modified: lldb/trunk/source/Core/Module.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Module.cpp?rev=151397&r1=151396&r2=151397&view=diff
==============================================================================
--- lldb/trunk/source/Core/Module.cpp (original)
+++ lldb/trunk/source/Core/Module.cpp Fri Feb 24 15:55:59 2012
@@ -114,63 +114,6 @@
 
 #endif
     
-
-Module::Module(const FileSpec& file_spec, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) :
-    m_mutex (Mutex::eMutexTypeRecursive),
-    m_mod_time (),
-    m_arch (),
-    m_uuid (),
-    m_file (file_spec),
-    m_platform_file(),
-    m_symfile_spec (),
-    m_object_name (),
-    m_object_offset (),
-    m_objfile_sp (),
-    m_symfile_ap (),
-    m_ast (),
-    m_did_load_objfile (false),
-    m_did_load_symbol_vendor (false),
-    m_did_parse_uuid (false),
-    m_did_init_ast (false),
-    m_is_dynamic_loader_module (false),
-    m_was_modified (false)
-{
-    // Scope for locker below...
-    {
-        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
-        GetModuleCollection().push_back(this);
-    }
-    StreamString s;
-    if (m_file.GetFilename())
-        s << m_file.GetFilename();
-    s.Printf("[0x%16.16llx]", header_addr);
-    m_file.GetFilename().SetCString (s.GetData());
-    Mutex::Locker locker (m_mutex);
-    DataBufferSP data_sp;
-    if (process_sp)
-    {
-        m_did_load_objfile = true;
-        std::auto_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
-        Error error;
-        const size_t bytes_read = process_sp->ReadMemory (header_addr, 
-                                                          data_ap->GetBytes(), 
-                                                          data_ap->GetByteSize(), 
-                                                          error);
-        if (bytes_read == 512)
-        {
-            data_sp.reset (data_ap.release());
-            m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
-            if (m_objfile_sp)
-            {
-                // Once we get the object file, update our module with the object file's 
-                // architecture since it might differ in vendor/os if some parts were
-                // unknown.
-                m_objfile_sp->GetArchitecture (m_arch);
-            }
-        }
-    }
-}
-
 Module::Module(const FileSpec& file_spec, 
                const ArchSpec& arch, 
                const ConstString *object_name, 
@@ -244,6 +187,59 @@
     m_objfile_sp.reset();
 }
 
+ObjectFile *
+Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error)
+{
+    if (m_objfile_sp)
+    {
+        error.SetErrorString ("object file already exists");
+    }
+    else
+    {
+        Mutex::Locker locker (m_mutex);
+        if (process_sp)
+        {
+            StreamString s;
+            if (m_file.GetFilename())
+                s << m_file.GetFilename();
+                s.Printf("[0x%16.16llx]", header_addr);
+                m_file.GetFilename().SetCString (s.GetData());
+            m_did_load_objfile = true;
+            std::auto_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
+            Error readmem_error;
+            const size_t bytes_read = process_sp->ReadMemory (header_addr, 
+                                                              data_ap->GetBytes(), 
+                                                              data_ap->GetByteSize(), 
+                                                              readmem_error);
+            if (bytes_read == 512)
+            {
+                DataBufferSP data_sp(data_ap.release());
+                m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
+                if (m_objfile_sp)
+                {
+                    // Once we get the object file, update our module with the object file's 
+                    // architecture since it might differ in vendor/os if some parts were
+                    // unknown.
+                    m_objfile_sp->GetArchitecture (m_arch);
+                }
+                else
+                {
+                    error.SetErrorString ("unable to find suitable object file plug-in");
+                }
+            }
+            else
+            {
+                error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
+            }
+        }
+        else
+        {
+            error.SetErrorString ("invalid process");
+        }
+    }
+    return m_objfile_sp.get();
+}
+
 
 const lldb_private::UUID&
 Module::GetUUID()

Modified: lldb/trunk/source/Target/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Process.cpp?rev=151397&r1=151396&r2=151397&view=diff
==============================================================================
--- lldb/trunk/source/Target/Process.cpp (original)
+++ lldb/trunk/source/Target/Process.cpp Fri Feb 24 15:55:59 2012
@@ -2222,20 +2222,26 @@
                                bool add_image_to_target,
                                bool load_sections_in_target)
 {
-    ModuleSP module_sp (new Module (file_spec, shared_from_this(), header_addr));
+    ModuleSP module_sp (new Module (file_spec, ArchSpec()));
     if (module_sp)
     {
-        if (add_image_to_target)
+        Error error;
+        ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error);
+        if (objfile)
         {
-            m_target.GetImages().Append(module_sp);
-            if (load_sections_in_target)
+            if (add_image_to_target)
             {
-                bool changed = false;
-                module_sp->SetLoadAddress (m_target, 0, changed);
+                m_target.GetImages().Append(module_sp);
+                if (load_sections_in_target)
+                {
+                    bool changed = false;
+                    module_sp->SetLoadAddress (m_target, 0, changed);
+                }
             }
+            return module_sp;
         }
     }
-    return module_sp;
+    return ModuleSP();
 }
 
 Error





More information about the lldb-commits mailing list