[Lldb-commits] [lldb] r132021 - in /lldb/trunk: include/lldb/Target/ABI.h source/Expression/IRForTarget.cpp source/Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h source/Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp source/Plugins/Process/Utility/UnwindLLDB.cpp source/Plugins/Process/Utility/UnwindLLDB.h

Greg Clayton gclayton at apple.com
Tue May 24 16:06:02 PDT 2011


Author: gclayton
Date: Tue May 24 18:06:02 2011
New Revision: 132021

URL: http://llvm.org/viewvc/llvm-project?rev=132021&view=rev
Log:
ABI plug-ins must implement the following pure virtual functions:

virtual bool
ABI::StackUsesFrames () = 0;

Should return true if your ABI uses frames when doing stack backtraces. This
means a frame pointer is used that points to the previous stack frame in some
way or another.

virtual bool
ABI::CallFrameAddressIsValid (lldb::addr_t cfa) = 0;

Should take a look at a call frame address (CFA) which is just the stack
pointer value upon entry to a function. ABIs usually impose alignment
restrictions (4, 8 or 16 byte aligned), and zero is usually not allowed.
This function should return true if "cfa" is valid call frame address for
the ABI, and false otherwise. This is used by the generic stack frame unwinding
code to help determine when a stack ends.

virtual bool
ABI::CodeAddressIsValid (lldb::addr_t pc) = 0;    

Validates a possible PC value and returns true if an opcode can be at "pc".
Some ABIs or architectures have fixed width instructions and must be aligned
to a 2 or 4 byte boundary. "pc" can be an opcode or a callable address which
means the load address might be decorated with extra bits (such as bit zero
to indicate a thumb function call for ARM targets), so take this into account
when returning true or false. The address should also be validated to ensure
it is a valid address for the address size of the inferior process. 32 bit
targets should make sure the address is less than UINT32_MAX.

Modified UnwindLLDB to use the new ABI functions to help it properly terminate
stacks.


Modified the mach-o function that extracts dependent files to not resolve the
path as the paths inside a binary might not match those on the current
host system.



Modified:
    lldb/trunk/include/lldb/Target/ABI.h
    lldb/trunk/source/Expression/IRForTarget.cpp
    lldb/trunk/source/Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h
    lldb/trunk/source/Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h
    lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h
    lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
    lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.cpp
    lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.h

Modified: lldb/trunk/include/lldb/Target/ABI.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/ABI.h?rev=132021&r1=132020&r2=132021&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/ABI.h (original)
+++ lldb/trunk/include/lldb/Target/ABI.h Tue May 24 18:06:02 2011
@@ -58,6 +58,15 @@
     virtual bool
     RegisterIsVolatile (const RegisterInfo *reg_info) = 0;
 
+    virtual bool
+    StackUsesFrames () = 0;
+
+    virtual bool
+    CallFrameAddressIsValid (lldb::addr_t cfa) = 0;
+
+    virtual bool
+    CodeAddressIsValid (lldb::addr_t pc) = 0;    
+
     static lldb::ABISP
     FindPlugin (const ArchSpec &arch);
     

Modified: lldb/trunk/source/Expression/IRForTarget.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/IRForTarget.cpp?rev=132021&r1=132020&r2=132021&view=diff
==============================================================================
--- lldb/trunk/source/Expression/IRForTarget.cpp (original)
+++ lldb/trunk/source/Expression/IRForTarget.cpp Tue May 24 18:06:02 2011
@@ -1735,10 +1735,7 @@
     
     ConstantIterator constant_iter;
     UserIterator user_iter;
-    
-    const Type *intptr_ty = Type::getIntNTy(m_module->getContext(), 
-                                            (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
-    
+        
     for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
          constant_iter != static_constants.end();
          ++constant_iter, ++user_iter)

Modified: lldb/trunk/source/Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h?rev=132021&r1=132020&r2=132021&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h (original)
+++ lldb/trunk/source/Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h Tue May 24 18:06:02 2011
@@ -54,6 +54,31 @@
     virtual bool
     RegisterIsVolatile (const lldb_private::RegisterInfo *reg_info);
     
+    virtual bool
+    StackUsesFrames ()
+    {
+        return true;
+    }
+    
+    virtual bool
+    CallFrameAddressIsValid (lldb::addr_t cfa)
+    {
+        // Make sure the stack call frame addresses are are 4 byte aligned
+        if (cfa & (4ull - 1ull))
+            return false;   // Not 4 byte aligned
+        if (cfa == 0)
+            return false;   // Zero is not a valid stack address
+        return true;
+    }
+    
+    virtual bool
+    CodeAddressIsValid (lldb::addr_t pc)
+    {
+        // Just make sure the address is a valid 32 bit address. Bit zero
+        // might be set due to Thumb function calls, so don't enforce 2 byte
+        // alignment
+        return pc <= UINT32_MAX;
+    }
 
     //------------------------------------------------------------------
     // Static Functions

Modified: lldb/trunk/source/Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h?rev=132021&r1=132020&r2=132021&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h (original)
+++ lldb/trunk/source/Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h Tue May 24 18:06:02 2011
@@ -177,6 +177,30 @@
     virtual bool
     RegisterIsVolatile (const lldb_private::RegisterInfo *reg_info);
     
+    virtual bool
+    StackUsesFrames ()
+    {
+        return true;
+    }
+    
+    virtual bool
+    CallFrameAddressIsValid (lldb::addr_t cfa)
+    {
+        // Make sure the stack call frame addresses are are 8 byte aligned
+        if (cfa & (8ull - 1ull))
+            return false;   // Not 8 byte aligned
+        if (cfa == 0)
+            return false;   // Zero is not a valid stack address
+        return true;
+    }
+
+    virtual bool
+    CodeAddressIsValid (lldb::addr_t pc)
+    {
+        // Just make sure the address is a valid 32 bit address. 
+        return pc <= UINT32_MAX;
+    }
+    
 
     //------------------------------------------------------------------
     // Static Functions

Modified: lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h?rev=132021&r1=132020&r2=132021&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h (original)
+++ lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h Tue May 24 18:06:02 2011
@@ -196,6 +196,31 @@
     virtual bool
     RegisterIsVolatile (const lldb_private::RegisterInfo *reg_info);
     
+    virtual bool
+    StackUsesFrames ()
+    {
+        return true;
+    }
+    
+    virtual bool
+    CallFrameAddressIsValid (lldb::addr_t cfa)
+    {
+        // Make sure the stack call frame addresses are are 8 byte aligned
+        if (cfa & (8ull - 1ull))
+            return false;   // Not 8 byte aligned
+        if (cfa == 0)
+            return false;   // Zero is not a valid stack address
+        return true;
+    }
+    
+    virtual bool
+    CodeAddressIsValid (lldb::addr_t pc)
+    {
+        // We have a 64 bit address space, so anything is valid as opcodes
+        // aren't fixed width...
+        return true;
+    }
+    
     //------------------------------------------------------------------
     // Static Functions
     //------------------------------------------------------------------

Modified: lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp?rev=132021&r1=132020&r2=132021&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp (original)
+++ lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp Tue May 24 18:06:02 2011
@@ -1503,6 +1503,7 @@
     struct load_command load_cmd;
     uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
     uint32_t count = 0;
+    const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
     uint32_t i;
     for (i=0; i<m_header.ncmds; ++i)
     {
@@ -1526,7 +1527,7 @@
                 // @rpath/.../file
                 if (path && path[0] != '@')
                 {
-                    FileSpec file_spec(path, true);
+                    FileSpec file_spec(path, resolve_path);
                     if (files.AppendIfUnique(file_spec))
                         count++;
                 }

Modified: lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.cpp?rev=132021&r1=132020&r2=132021&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.cpp (original)
+++ lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.cpp Tue May 24 18:06:02 2011
@@ -41,7 +41,10 @@
 #endif
         if (!AddFirstFrame ())
             return 0;
-        while (AddOneMoreFrame ())
+
+        ABI *abi = m_thread.GetProcess().GetABI().get();
+
+        while (AddOneMoreFrame (abi))
         {
 #if DEBUG_FRAME_SPEED
             if ((m_frames.size() % FRAME_COUNT) == 0)
@@ -66,32 +69,32 @@
 {
     // First, set up the 0th (initial) frame
     CursorSP first_cursor_sp(new Cursor ());
-    std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB (m_thread, 
-                                                                                       RegisterContextLLDB::SharedPtr(), 
-                                                                                       first_cursor_sp->sctx, 
-                                                                                       0));
-    if (first_register_ctx_ap.get() == NULL)
+    RegisterContextLLDB::SharedPtr reg_ctx_sp (new RegisterContextLLDB (m_thread, 
+                                                                        RegisterContextLLDB::SharedPtr(), 
+                                                                        first_cursor_sp->sctx, 
+                                                                        0));
+    if (reg_ctx_sp.get() == NULL)
         return false;
     
-    if (!first_register_ctx_ap->IsValid())
+    if (!reg_ctx_sp->IsValid())
         return false;
 
-    if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa))
+    if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa))
         return false;
 
-    if (!first_register_ctx_ap->ReadPC (first_cursor_sp->start_pc))
+    if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc))
         return false;
 
     // Everything checks out, so release the auto pointer value and let the
     // cursor own it in its shared pointer
-    first_cursor_sp->reg_ctx.reset(first_register_ctx_ap.release());
+    first_cursor_sp->reg_ctx = reg_ctx_sp;
     m_frames.push_back (first_cursor_sp);
     return true;
 }
 
 // For adding a non-zero stack frame to m_frames.
 bool
-UnwindLLDB::AddOneMoreFrame ()
+UnwindLLDB::AddOneMoreFrame (ABI *abi)
 {
     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
     CursorSP cursor_sp(new Cursor ());
@@ -101,14 +104,14 @@
         return false;
 
     uint32_t cur_idx = m_frames.size ();
-    std::auto_ptr<RegisterContextLLDB> register_ctx_ap(new RegisterContextLLDB (m_thread, 
-                                                                                m_frames[cur_idx - 1]->reg_ctx, 
-                                                                                cursor_sp->sctx, 
-                                                                                cur_idx));
-    if (register_ctx_ap.get() == NULL)
+    RegisterContextLLDB::SharedPtr reg_ctx_sp(new RegisterContextLLDB (m_thread, 
+                                                                       m_frames[cur_idx - 1]->reg_ctx, 
+                                                                       cursor_sp->sctx, 
+                                                                       cur_idx));
+    if (reg_ctx_sp.get() == NULL)
         return false;
 
-    if (!register_ctx_ap->IsValid())
+    if (!reg_ctx_sp->IsValid())
     {
         if (log)
         {
@@ -117,7 +120,7 @@
         }
         return false;
     }
-    if (!register_ctx_ap->GetCFA (cursor_sp->cfa))
+    if (!reg_ctx_sp->GetCFA (cursor_sp->cfa))
     {
         if (log)
         {
@@ -126,7 +129,7 @@
         }
         return false;
     }
-    if (cursor_sp->cfa == (addr_t) -1 || cursor_sp->cfa == 1 || cursor_sp->cfa == 0)
+    if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa))
     {
         if (log)
         {
@@ -135,7 +138,7 @@
         }
         return false;
     }
-    if (!register_ctx_ap->ReadPC (cursor_sp->start_pc))
+    if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc))
     {
         if (log)
         {
@@ -144,16 +147,28 @@
         }
         return false;
     }
+    if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc))
+    {
+        if (log)
+        {
+            log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk",
+                        cur_idx < 100 ? cur_idx : 100, "", cur_idx);
+        }
+        return false;
+    }
     if (!m_frames.empty())
     {
-        if ((m_frames.back()->start_pc == cursor_sp->start_pc) &&
-            (m_frames.back()->cfa      == cursor_sp->cfa))
+        if (m_frames.back()->start_pc == cursor_sp->start_pc)
         {
-            // Infinite loop where the current cursor is the same as the previous one...
-            return false;
+            if (m_frames.back()->cfa == cursor_sp->cfa)
+                return false; // Infinite loop where the current cursor is the same as the previous one...
+            else if (abi->StackUsesFrames())
+            {
+                if (reg_ctx_sp->GetFP() == 0)
+                    return false;
+            }
         }
     }
-    RegisterContextLLDB::SharedPtr reg_ctx_sp(register_ctx_ap.release());
     cursor_sp->reg_ctx = reg_ctx_sp;
     m_frames.push_back (cursor_sp);
     return true;
@@ -168,7 +183,9 @@
             return false;
     }
 
-    while (idx >= m_frames.size() && AddOneMoreFrame ())
+    ABI *abi = m_thread.GetProcess().GetABI().get();
+
+    while (idx >= m_frames.size() && AddOneMoreFrame (abi))
         ;
 
     if (idx < m_frames.size ())
@@ -197,7 +214,9 @@
             return reg_ctx_sp;
     }
 
-    while (idx >= m_frames.size() && AddOneMoreFrame ())
+    ABI *abi = m_thread.GetProcess().GetABI().get();
+
+    while (idx >= m_frames.size() && AddOneMoreFrame (abi))
         ;
 
     if (idx < m_frames.size ())

Modified: lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.h?rev=132021&r1=132020&r2=132021&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.h (original)
+++ lldb/trunk/source/Plugins/Process/Utility/UnwindLLDB.h Tue May 24 18:06:02 2011
@@ -63,7 +63,7 @@
     typedef lldb::SharedPtr<Cursor>::Type CursorSP;
     std::vector<CursorSP> m_frames;
 
-    bool AddOneMoreFrame ();
+    bool AddOneMoreFrame (ABI *abi);
     bool AddFirstFrame ();
 
     //------------------------------------------------------------------





More information about the lldb-commits mailing list