[Lldb-commits] [lldb] r274366 - Added support for thread local variables on all Apple OS variants.

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Fri Jul 1 10:17:24 PDT 2016


Author: gclayton
Date: Fri Jul  1 12:17:23 2016
New Revision: 274366

URL: http://llvm.org/viewvc/llvm-project?rev=274366&view=rev
Log:
Added support for thread local variables on all Apple OS variants.

We had support that assumed that thread local data for a variable could be determined solely from the module in which the variable exists. While this work for linux, it doesn't work for Apple OSs. The DWARF for thread local variables consists of location opcodes that do something like:

DW_OP_const8u (x)
DW_OP_form_tls_address

or 

DW_OP_const8u (x)
DW_OP_GNU_push_tls_address

The "x" is allowed to be anything that is needed to determine the location of the variable. For Linux "x" is the offset within the TLS data for a given executable (ModuleSP in LLDB). For Apple OS variants, it is the file address of the data structure that contains a pthread key that can be used with pthread_getspecific() and the offset needed. 

This fix passes the "x" along to the thread:

virtual lldb::addr_t
lldb_private::Thread::GetThreadLocalData(const lldb::ModuleSP module, lldb::addr_t tls_file_addr);

Then this is passed along to the DynamicLoader::GetThreadLocalData():

virtual lldb::addr_t
lldb_private::DynamicLoader::GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread, lldb::addr_t tls_file_addr);

This allows each DynamicLoader plug-in do the right thing for the current OS.

The DynamicLoaderMacOSXDYLD was modified to be able to grab the pthread key from the data structure that is in memory and call "void *pthread_getspecific(pthread_key_t key)" to get the value of the thread local storage and it caches it per thread since it never changes.

I had to update the test case to access the thread local data before trying to print it as on Apple OS variants, thread locals are not available unless they have been accessed at least one by the current thread.

I also added a new lldb::ValueType named "eValueTypeVariableThreadLocal" so that we can ask SBValue objects for their ValueType and be able to tell when we have a thread local variable.

<rdar://problem/23308080>


Modified:
    lldb/trunk/include/lldb/Expression/DWARFExpression.h
    lldb/trunk/include/lldb/Target/DynamicLoader.h
    lldb/trunk/include/lldb/Target/Thread.h
    lldb/trunk/include/lldb/lldb-enumerations.h
    lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/TestTlsGlobals.py
    lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/a.c
    lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/main.c
    lldb/trunk/source/API/SBBlock.cpp
    lldb/trunk/source/API/SBFrame.cpp
    lldb/trunk/source/API/SBValue.cpp
    lldb/trunk/source/Commands/CommandObjectFrame.cpp
    lldb/trunk/source/Commands/CommandObjectTarget.cpp
    lldb/trunk/source/Expression/DWARFExpression.cpp
    lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp
    lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.h
    lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
    lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h
    lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
    lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h
    lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
    lldb/trunk/source/Symbol/SymbolContext.cpp
    lldb/trunk/source/Symbol/Variable.cpp
    lldb/trunk/source/Target/Thread.cpp

Modified: lldb/trunk/include/lldb/Expression/DWARFExpression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/DWARFExpression.h?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/DWARFExpression.h (original)
+++ lldb/trunk/include/lldb/Expression/DWARFExpression.h Fri Jul  1 12:17:23 2016
@@ -10,11 +10,12 @@
 #ifndef liblldb_DWARFExpression_h_
 #define liblldb_DWARFExpression_h_
 
-#include "lldb/lldb-private.h"
 #include "lldb/Core/Address.h"
 #include "lldb/Core/DataExtractor.h"
 #include "lldb/Core/Error.h"
 #include "lldb/Core/Scalar.h"
+#include "lldb/lldb-private.h"
+#include <functional>
 
 class DWARFCompileUnit;
 
@@ -166,7 +167,14 @@ public:
 
     bool
     Update_DW_OP_addr (lldb::addr_t file_addr);
-    
+
+    bool
+    ContainsThreadLocalStorage() const;
+
+    bool
+    LinkThreadLocalStorage(lldb::ModuleSP new_module_sp,
+                           std::function<lldb::addr_t(lldb::addr_t file_addr)> const &link_address_callback);
+
     //------------------------------------------------------------------
     /// Make the expression parser read its location information from a
     /// given data source.  Does not change the offset and length

Modified: lldb/trunk/include/lldb/Target/DynamicLoader.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/DynamicLoader.h?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/DynamicLoader.h (original)
+++ lldb/trunk/include/lldb/Target/DynamicLoader.h Fri Jul  1 12:17:23 2016
@@ -238,7 +238,7 @@ public:
     ///     LLDB_INVALID_ADDRESS is returned.
     //------------------------------------------------------------------
     virtual lldb::addr_t
-    GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread)
+    GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread, lldb::addr_t tls_file_addr)
     {
         return LLDB_INVALID_ADDRESS;
     }

Modified: lldb/trunk/include/lldb/Target/Thread.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Thread.h?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Thread.h (original)
+++ lldb/trunk/include/lldb/Target/Thread.h Fri Jul  1 12:17:23 2016
@@ -708,13 +708,15 @@ public:
     /// @param[in] module
     ///     The module to query TLS data for.
     ///
+    /// @param[in] tls_file_addr
+    ///     The thread local address in module
     /// @return
     ///     If the thread has TLS data allocated for the
     ///     module, the address of the TLS block. Otherwise
     ///     LLDB_INVALID_ADDRESS is returned.
     //------------------------------------------------------------------
     virtual lldb::addr_t
-    GetThreadLocalData (const lldb::ModuleSP module);
+    GetThreadLocalData(const lldb::ModuleSP module, lldb::addr_t tls_file_addr);
 
     //------------------------------------------------------------------
     /// Check whether this thread is safe to run functions

Modified: lldb/trunk/include/lldb/lldb-enumerations.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/lldb-enumerations.h?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/include/lldb/lldb-enumerations.h (original)
+++ lldb/trunk/include/lldb/lldb-enumerations.h Fri Jul  1 12:17:23 2016
@@ -271,17 +271,17 @@ namespace lldb {
         eErrorTypeWin32         ///< Standard Win32 error codes.
     };
 
-
     enum ValueType
     {
-        eValueTypeInvalid           = 0,
-        eValueTypeVariableGlobal    = 1,    // globals variable
-        eValueTypeVariableStatic    = 2,    // static variable
-        eValueTypeVariableArgument  = 3,    // function argument variables
-        eValueTypeVariableLocal     = 4,    // function local variables
-        eValueTypeRegister          = 5,    // stack frame register value
-        eValueTypeRegisterSet       = 6,    // A collection of stack frame register values
-        eValueTypeConstResult       = 7     // constant result variables
+        eValueTypeInvalid = 0,
+        eValueTypeVariableGlobal = 1,     // globals variable
+        eValueTypeVariableStatic = 2,     // static variable
+        eValueTypeVariableArgument = 3,   // function argument variables
+        eValueTypeVariableLocal = 4,      // function local variables
+        eValueTypeRegister = 5,           // stack frame register value
+        eValueTypeRegisterSet = 6,        // A collection of stack frame register values
+        eValueTypeConstResult = 7,        // constant result variables
+        eValueTypeVariableThreadLocal = 8 // thread local storage variable
     };
 
     //----------------------------------------------------------------------

Modified: lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/TestTlsGlobals.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/TestTlsGlobals.py?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/TestTlsGlobals.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/TestTlsGlobals.py Fri Jul  1 12:17:23 2016
@@ -26,7 +26,6 @@ class TlsGlobalTestCase(TestBase):
                 self.runCmd("settings set target.env-vars " + self.dylibPath + "=" + os.getcwd())
             self.addTearDownHook(lambda: self.runCmd("settings remove target.env-vars " + self.dylibPath))
 
-    @unittest2.expectedFailure("rdar://7796742")
     @skipIfWindows # TLS works differently on Windows, this would need to be implemented separately.
     def test(self):
         """Test thread-local storage."""

Modified: lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/a.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/a.c?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/a.c (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/a.c Fri Jul  1 12:17:23 2016
@@ -11,6 +11,12 @@
 
 __thread int var_shared = 33;
 
+int
+touch_shared()
+{
+    return var_shared;
+}
+
 void shared_check()
 {
 	var_shared *= 2;

Modified: lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/main.c?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/main.c (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/lang/c/tls_globals/main.c Fri Jul  1 12:17:23 2016
@@ -11,6 +11,10 @@
 #include <unistd.h>
 
 void shared_check();
+// On some OS's (darwin) you must actually access a thread local variable
+// before you can read it
+int
+touch_shared();
 
 // Create some TLS storage within the static executable.
 __thread int var_static = 44;
@@ -28,9 +32,11 @@ int main (int argc, char const *argv[])
 {
 	pthread_t handle;
 	pthread_create(&handle, NULL, &fn_static, NULL);
+        touch_shared();
+        for (; var_static;)
+        {
+            usleep(1); // main breakpoint
+        }
 
-	for(;;)
-		usleep(1); // main breakpoint
-
-	return 0;
+        return 0;
 }

Modified: lldb/trunk/source/API/SBBlock.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBBlock.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/API/SBBlock.cpp (original)
+++ lldb/trunk/source/API/SBBlock.cpp Fri Jul  1 12:17:23 2016
@@ -294,6 +294,7 @@ SBBlock::GetVariables (lldb::SBFrame& fr
                         {
                             case eValueTypeVariableGlobal:
                             case eValueTypeVariableStatic:
+                            case eValueTypeVariableThreadLocal:
                                 add_variable = statics;
                                 break;
                                 
@@ -356,6 +357,7 @@ SBBlock::GetVariables (lldb::SBTarget& t
                         {
                             case eValueTypeVariableGlobal:
                             case eValueTypeVariableStatic:
+                            case eValueTypeVariableThreadLocal:
                                 add_variable = statics;
                                 break;
                                 

Modified: lldb/trunk/source/API/SBFrame.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBFrame.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/API/SBFrame.cpp (original)
+++ lldb/trunk/source/API/SBFrame.cpp Fri Jul  1 12:17:23 2016
@@ -901,33 +901,31 @@ SBFrame::FindValue (const char *name, Va
                 case eValueTypeVariableStatic:      // static variable
                 case eValueTypeVariableArgument:    // function argument variables
                 case eValueTypeVariableLocal:       // function local variables
-                    {
-                        SymbolContext sc(frame->GetSymbolContext(eSymbolContextBlock));
+                case eValueTypeVariableThreadLocal: // thread local variables
+                {
+                    SymbolContext sc(frame->GetSymbolContext(eSymbolContextBlock));
 
-                        const bool can_create = true;
-                        const bool get_parent_variables = true;
-                        const bool stop_if_block_is_inlined_function = true;
-
-                        if (sc.block)
-                            sc.block->AppendVariables(can_create,
-                                                      get_parent_variables,
-                                                      stop_if_block_is_inlined_function,
-                                                      [frame](Variable* v) { return v->IsInScope(frame); },
-                                                      &variable_list);
-                        if (value_type == eValueTypeVariableGlobal)
-                        {
-                            const bool get_file_globals = true;
-                            VariableList *frame_vars = frame->GetVariableList(get_file_globals);
-                            if (frame_vars)
-                                frame_vars->AppendVariablesIfUnique(variable_list);
-                        }
-                        ConstString const_name(name);
-                        VariableSP variable_sp(variable_list.FindVariable(const_name, value_type));
-                        if (variable_sp)
-                        {
-                            value_sp = frame->GetValueObjectForFrameVariable(variable_sp, eNoDynamicValues);
-                            sb_value.SetSP(value_sp, use_dynamic);
-                        }
+                    const bool can_create = true;
+                    const bool get_parent_variables = true;
+                    const bool stop_if_block_is_inlined_function = true;
+
+                    if (sc.block)
+                        sc.block->AppendVariables(can_create, get_parent_variables, stop_if_block_is_inlined_function,
+                                                  [frame](Variable *v) { return v->IsInScope(frame); }, &variable_list);
+                    if (value_type == eValueTypeVariableGlobal)
+                    {
+                        const bool get_file_globals = true;
+                        VariableList *frame_vars = frame->GetVariableList(get_file_globals);
+                        if (frame_vars)
+                            frame_vars->AppendVariablesIfUnique(variable_list);
+                    }
+                    ConstString const_name(name);
+                    VariableSP variable_sp(variable_list.FindVariable(const_name, value_type));
+                    if (variable_sp)
+                    {
+                        value_sp = frame->GetValueObjectForFrameVariable(variable_sp, eNoDynamicValues);
+                        sb_value.SetSP(value_sp, use_dynamic);
+                    }
                     }
                     break;
 
@@ -1201,6 +1199,7 @@ SBFrame::GetVariables (const lldb::SBVar
                                 {
                                 case eValueTypeVariableGlobal:
                                 case eValueTypeVariableStatic:
+                                case eValueTypeVariableThreadLocal:
                                     add_variable = statics;
                                     break;
 

Modified: lldb/trunk/source/API/SBValue.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBValue.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/API/SBValue.cpp (original)
+++ lldb/trunk/source/API/SBValue.cpp Fri Jul  1 12:17:23 2016
@@ -528,6 +528,10 @@ SBValue::GetValueType ()
                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeConstResult",
                              static_cast<void*>(value_sp.get()));
                 break;
+            case eValueTypeVariableThreadLocal:
+                log->Printf("SBValue(%p)::GetValueType () => eValueTypeVariableThreadLocal",
+                            static_cast<void *>(value_sp.get()));
+                break;
         }
     }
     return result;

Modified: lldb/trunk/source/Commands/CommandObjectFrame.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectFrame.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectFrame.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectFrame.cpp Fri Jul  1 12:17:23 2016
@@ -539,6 +539,10 @@ protected:
                                     scope_string = " LOCAL: ";
                                 break;
 
+                            case eValueTypeVariableThreadLocal:
+                                if (dump_variable && m_option_variable.show_scope)
+                                    scope_string = "THREAD: ";
+                                break;
                             default:
                                 break;
                         }

Modified: lldb/trunk/source/Commands/CommandObjectTarget.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectTarget.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectTarget.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectTarget.cpp Fri Jul  1 12:17:23 2016
@@ -795,6 +795,11 @@ public:
                     s.PutCString(" LOCAL: ");
                 break;
 
+            case eValueTypeVariableThreadLocal:
+                if (m_option_variable.show_scope)
+                    s.PutCString("THREAD: ");
+                break;
+
             default:
                 break;
         }

Modified: lldb/trunk/source/Expression/DWARFExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/DWARFExpression.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Expression/DWARFExpression.cpp (original)
+++ lldb/trunk/source/Expression/DWARFExpression.cpp Fri Jul  1 12:17:23 2016
@@ -1003,7 +1003,128 @@ DWARFExpression::Update_DW_OP_addr (lldb
 }
 
 bool
-DWARFExpression::LocationListContainsAddress (lldb::addr_t loclist_base_addr, lldb::addr_t addr) const
+DWARFExpression::ContainsThreadLocalStorage() const
+{
+    // We are assuming for now that any thread local variable will not
+    // have a location list. This has been true for all thread local
+    // variables we have seen so far produced by any compiler.
+    if (IsLocationList())
+        return false;
+    lldb::offset_t offset = 0;
+    while (m_data.ValidOffset(offset))
+    {
+        const uint8_t op = m_data.GetU8(&offset);
+
+        if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
+            return true;
+        const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
+        if (op_arg_size == LLDB_INVALID_OFFSET)
+            return false;
+        else
+            offset += op_arg_size;
+    }
+    return false;
+}
+bool
+DWARFExpression::LinkThreadLocalStorage(
+    lldb::ModuleSP new_module_sp, std::function<lldb::addr_t(lldb::addr_t file_addr)> const &link_address_callback)
+{
+    // We are assuming for now that any thread local variable will not
+    // have a location list. This has been true for all thread local
+    // variables we have seen so far produced by any compiler.
+    if (IsLocationList())
+        return false;
+
+    const uint32_t addr_byte_size = m_data.GetAddressByteSize();
+    // We have to make a copy of the data as we don't know if this
+    // data is from a read only memory mapped buffer, so we duplicate
+    // all of the data first, then modify it, and if all goes well,
+    // we then replace the data for this expression
+
+    // So first we copy the data into a heap buffer
+    std::shared_ptr<DataBufferHeap> heap_data_sp(new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
+
+    // Make en encoder so we can write the address into the buffer using
+    // the correct byte order (endianness)
+    DataEncoder encoder(heap_data_sp->GetBytes(), heap_data_sp->GetByteSize(), m_data.GetByteOrder(), addr_byte_size);
+
+    lldb::offset_t offset = 0;
+    lldb::offset_t const_offset = 0;
+    lldb::addr_t const_value = 0;
+    size_t const_byte_size = 0;
+    while (m_data.ValidOffset(offset))
+    {
+        const uint8_t op = m_data.GetU8(&offset);
+
+        bool decoded_data = false;
+        switch (op)
+        {
+            case DW_OP_const4u:
+                // Remember the const offset in case we later have a DW_OP_form_tls_address
+                // or DW_OP_GNU_push_tls_address
+                const_offset = offset;
+                const_value = m_data.GetU32(&offset);
+                decoded_data = true;
+                const_byte_size = 4;
+                break;
+
+            case DW_OP_const8u:
+                // Remember the const offset in case we later have a DW_OP_form_tls_address
+                // or DW_OP_GNU_push_tls_address
+                const_offset = offset;
+                const_value = m_data.GetU64(&offset);
+                decoded_data = true;
+                const_byte_size = 8;
+                break;
+
+            case DW_OP_form_tls_address:
+            case DW_OP_GNU_push_tls_address:
+                // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded by
+                // a file address on the stack. We assume that DW_OP_const4u or DW_OP_const8u
+                // is used for these values, and we check that the last opcode we got before
+                // either of these was DW_OP_const4u or DW_OP_const8u. If so, then we can link
+                // the value accodingly. For Darwin, the value in the DW_OP_const4u or
+                // DW_OP_const8u is the file address of a structure that contains a function
+                // pointer, the pthread key and the offset into the data pointed to by the
+                // pthread key. So we must link this address and also set the module of this
+                // expression to the new_module_sp so we can resolve the file address correctly
+                if (const_byte_size > 0)
+                {
+                    lldb::addr_t linked_file_addr = link_address_callback(const_value);
+                    if (linked_file_addr == LLDB_INVALID_ADDRESS)
+                        return false;
+                    // Replace the address in the new buffer
+                    if (encoder.PutMaxU64(const_offset, const_byte_size, linked_file_addr) == UINT32_MAX)
+                        return false;
+                }
+                break;
+
+            default:
+                const_offset = 0;
+                const_value = 0;
+                const_byte_size = 0;
+                break;
+        }
+
+        if (!decoded_data)
+        {
+            const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
+            if (op_arg_size == LLDB_INVALID_OFFSET)
+                return false;
+            else
+                offset += op_arg_size;
+        }
+    }
+
+    // If we linked the TLS address correctly, update the module so that when the expression
+    // is evaluated it can resolve the file address to a load address and read the TLS data
+    m_module_wp = new_module_sp;
+    m_data.SetData(heap_data_sp);
+    return true;
+}
+
+bool
+DWARFExpression::LocationListContainsAddress(lldb::addr_t loclist_base_addr, lldb::addr_t addr) const
 {
     if (addr == LLDB_INVALID_ADDRESS)
         return false;
@@ -2845,18 +2966,17 @@ DWARFExpression::Evaluate
                 }
 
                 // Lookup the TLS block address for this thread and module.
-                addr_t tls_addr = thread->GetThreadLocalData (module_sp);
+                const addr_t tls_file_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
+                const addr_t tls_load_addr = thread->GetThreadLocalData(module_sp, tls_file_addr);
 
-                if (tls_addr == LLDB_INVALID_ADDRESS)
+                if (tls_load_addr == LLDB_INVALID_ADDRESS)
                 {
                     if (error_ptr)
                         error_ptr->SetErrorString ("No TLS data currently exists for this thread.");
                     return false;
                 }
 
-                // Convert the TLS offset into the absolute address.
-                Scalar tmp = stack.back().ResolveValue(exe_ctx);
-                stack.back() = tmp + tls_addr;
+                stack.back().GetScalar() = tls_load_addr;
                 stack.back().SetValueType (Value::eValueTypeLoadAddress);
             }
             break;

Modified: lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp Fri Jul  1 12:17:23 2016
@@ -646,7 +646,8 @@ static int ReadInt(Process *process, add
 }
 
 lldb::addr_t
-DynamicLoaderHexagonDYLD::GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread)
+DynamicLoaderHexagonDYLD::GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread,
+                                             lldb::addr_t tls_file_addr)
 {
     auto it = m_loaded_modules.find (module);
     if (it == m_loaded_modules.end())
@@ -687,5 +688,5 @@ DynamicLoaderHexagonDYLD::GetThreadLocal
                     "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 ", modid=%i, tls_block=0x%" PRIx64,
                     mod->GetObjectName().AsCString(""), link_map, tp, modid, tls_block);
 
-    return tls_block;
+    return tls_block + tls_file_addr;
 }

Modified: lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.h?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.h (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.h Fri Jul  1 12:17:23 2016
@@ -59,7 +59,7 @@ public:
     CanLoadImage() override;
 
     lldb::addr_t
-    GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread) override;
+    GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread, lldb::addr_t tls_file_addr) override;
 
     //------------------------------------------------------------------
     // PluginInterface protocol

Modified: lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp Fri Jul  1 12:17:23 2016
@@ -17,16 +17,18 @@
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/Section.h"
 #include "lldb/Core/State.h"
+#include "lldb/Expression/DiagnosticManager.h"
 #include "lldb/Symbol/ClangASTContext.h"
 #include "lldb/Symbol/Function.h"
 #include "lldb/Symbol/ObjectFile.h"
-#include "lldb/Target/ObjCLanguageRuntime.h"
 #include "lldb/Target/ABI.h"
+#include "lldb/Target/ObjCLanguageRuntime.h"
 #include "lldb/Target/RegisterContext.h"
+#include "lldb/Target/StackFrame.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Target/Thread.h"
+#include "lldb/Target/ThreadPlanCallFunction.h"
 #include "lldb/Target/ThreadPlanRunToAddress.h"
-#include "lldb/Target/StackFrame.h"
 
 #include "DynamicLoaderMacOSXDYLD.h"
 
@@ -142,8 +144,11 @@ DynamicLoaderMacOSXDYLD::DynamicLoaderMa
     : DynamicLoader(process),
       m_dyld(),
       m_dyld_module_wp(),
+      m_libpthread_module_wp(),
+      m_pthread_getspecific_addr(),
       m_dyld_all_image_infos_addr(LLDB_INVALID_ADDRESS),
       m_dyld_all_image_infos(),
+      m_tid_to_tls_map(),
       m_dyld_all_image_infos_stop_id(UINT32_MAX),
       m_break_id(LLDB_INVALID_BREAK_ID),
       m_dyld_image_infos(),
@@ -192,6 +197,7 @@ DynamicLoaderMacOSXDYLD::DidLaunch ()
 bool
 DynamicLoaderMacOSXDYLD::ProcessDidExec ()
 {
+    bool did_exec = false;
     if (m_process)
     {
         // If we are stopped after an exec, we will have only one thread...
@@ -205,35 +211,42 @@ DynamicLoaderMacOSXDYLD::ProcessDidExec
             {
                 // The image info address from the process is the 'dyld_all_image_infos'
                 // address and it has changed.
-                return true;
+                did_exec = true;
             }
-            
-            if (m_process_image_addr_is_all_images_infos == false && shlib_addr == m_dyld.address)
+            else if (m_process_image_addr_is_all_images_infos == false && shlib_addr == m_dyld.address)
             {
                 // The image info address from the process is the mach_header
                 // address for dyld and it has changed.
-                return true;
+                did_exec = true;
             }
-            
-            // ASLR might be disabled and dyld could have ended up in the same
-            // location. We should try and detect if we are stopped at '_dyld_start'
-            ThreadSP thread_sp (m_process->GetThreadList().GetThreadAtIndex(0));
-            if (thread_sp)
+            else
             {
-                lldb::StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex(0));
-                if (frame_sp)
+                // ASLR might be disabled and dyld could have ended up in the same
+                // location. We should try and detect if we are stopped at '_dyld_start'
+                ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
+                if (thread_sp)
                 {
-                    const Symbol *symbol = frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
-                    if (symbol)
+                    lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
+                    if (frame_sp)
                     {
-                        if (symbol->GetName() == ConstString("_dyld_start"))
-                            return true;
+                        const Symbol *symbol = frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
+                        if (symbol)
+                        {
+                            if (symbol->GetName() == ConstString("_dyld_start"))
+                                did_exec = true;
+                        }
                     }
                 }
             }
+
+            if (did_exec)
+            {
+                m_libpthread_module_wp.reset();
+                m_pthread_getspecific_addr.Clear();
+            }
         }
     }
-    return false;
+    return did_exec;
 }
 
 
@@ -1982,6 +1995,135 @@ DynamicLoaderMacOSXDYLD::CanLoadImage ()
     return error;
 }
 
+lldb::ModuleSP
+DynamicLoaderMacOSXDYLD::GetPThreadLibraryModule()
+{
+    ModuleSP module_sp = m_libpthread_module_wp.lock();
+    if (!module_sp)
+    {
+        SymbolContextList sc_list;
+        ModuleSpec module_spec;
+        module_spec.GetFileSpec().GetFilename().SetCString("libsystem_pthread.dylib");
+        ModuleList module_list;
+        if (m_process->GetTarget().GetImages().FindModules(module_spec, module_list))
+        {
+            if (module_list.GetSize() == 1)
+            {
+                module_sp = module_list.GetModuleAtIndex(0);
+                if (module_sp)
+                    m_libpthread_module_wp = module_sp;
+            }
+        }
+    }
+    return module_sp;
+}
+
+Address
+DynamicLoaderMacOSXDYLD::GetPthreadSetSpecificAddress()
+{
+    if (!m_pthread_getspecific_addr.IsValid())
+    {
+        ModuleSP module_sp = GetPThreadLibraryModule();
+        if (module_sp)
+        {
+            lldb_private::SymbolContextList sc_list;
+            module_sp->FindSymbolsWithNameAndType(ConstString("pthread_getspecific"), eSymbolTypeCode, sc_list);
+            SymbolContext sc;
+            if (sc_list.GetContextAtIndex(0, sc))
+            {
+                if (sc.symbol)
+                    m_pthread_getspecific_addr = sc.symbol->GetAddress();
+            }
+        }
+    }
+    return m_pthread_getspecific_addr;
+}
+
+lldb::addr_t
+DynamicLoaderMacOSXDYLD::GetThreadLocalData(const lldb::ModuleSP module_sp, const lldb::ThreadSP thread_sp,
+                                            lldb::addr_t tls_file_addr)
+{
+    if (!thread_sp || !module_sp)
+        return LLDB_INVALID_ADDRESS;
+
+    std::lock_guard<std::recursive_mutex> guard(m_mutex);
+
+    const uint32_t addr_size = m_process->GetAddressByteSize();
+    uint8_t buf[sizeof(lldb::addr_t) * 3];
+
+    lldb_private::Address tls_addr;
+    if (module_sp->ResolveFileAddress(tls_file_addr, tls_addr))
+    {
+        Error error;
+        const size_t tsl_data_size = addr_size * 3;
+        Target &target = m_process->GetTarget();
+        if (target.ReadMemory(tls_addr, false, buf, tsl_data_size, error) == tsl_data_size)
+        {
+            const ByteOrder byte_order = m_process->GetByteOrder();
+            DataExtractor data(buf, sizeof(buf), byte_order, addr_size);
+            lldb::offset_t offset = addr_size; // Skip the first pointer
+            const lldb::addr_t pthread_key = data.GetAddress(&offset);
+            const lldb::addr_t tls_offset = data.GetAddress(&offset);
+            if (pthread_key != 0)
+            {
+                // First check to see if we have already figured out the location
+                // of TLS data for the pthread_key on a specific thread yet. If we
+                // have we can re-use it since its location will not change unless
+                // the process execs.
+                const tid_t tid = thread_sp->GetID();
+                auto tid_pos = m_tid_to_tls_map.find(tid);
+                if (tid_pos != m_tid_to_tls_map.end())
+                {
+                    auto tls_pos = tid_pos->second.find(pthread_key);
+                    if (tls_pos != tid_pos->second.end())
+                    {
+                        return tls_pos->second + tls_offset;
+                    }
+                }
+                StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0);
+                if (frame_sp)
+                {
+                    ClangASTContext *clang_ast_context = target.GetScratchClangASTContext();
+
+                    if (!clang_ast_context)
+                        return LLDB_INVALID_ADDRESS;
+
+                    CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
+                    Address pthread_getspecific_addr = GetPthreadSetSpecificAddress();
+                    if (pthread_getspecific_addr.IsValid())
+                    {
+                        EvaluateExpressionOptions options;
+
+                        lldb::ThreadPlanSP thread_plan_sp(
+                            new ThreadPlanCallFunction(*thread_sp, pthread_getspecific_addr, clang_void_ptr_type,
+                                                       llvm::ArrayRef<lldb::addr_t>(pthread_key), options));
+
+                        DiagnosticManager execution_errors;
+                        ExecutionContext exe_ctx(thread_sp);
+                        lldb::ExpressionResults results =
+                            m_process->RunThreadPlan(exe_ctx, thread_plan_sp, options, execution_errors);
+
+                        if (results == lldb::eExpressionCompleted)
+                        {
+                            lldb::ValueObjectSP result_valobj_sp = thread_plan_sp->GetReturnValueObject();
+                            if (result_valobj_sp)
+                            {
+                                const lldb::addr_t pthread_key_data = result_valobj_sp->GetValueAsUnsigned(0);
+                                if (pthread_key_data)
+                                {
+                                    m_tid_to_tls_map[tid].insert(std::make_pair(pthread_key, pthread_key_data));
+                                    return pthread_key_data + tls_offset;
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+    return LLDB_INVALID_ADDRESS;
+}
+
 void
 DynamicLoaderMacOSXDYLD::Initialize()
 {

Modified: lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h Fri Jul  1 12:17:23 2016
@@ -12,6 +12,7 @@
 
 // C Includes
 // C++ Includes
+#include <map>
 #include <mutex>
 #include <vector>
 
@@ -76,6 +77,9 @@ public:
     lldb_private::Error
     CanLoadImage() override;
 
+    lldb::addr_t
+    GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread, lldb::addr_t tls_file_addr) override;
+
     //------------------------------------------------------------------
     // PluginInterface protocol
     //------------------------------------------------------------------
@@ -296,6 +300,8 @@ protected:
         }
     };
 
+    typedef std::map<uint64_t, lldb::addr_t> PthreadKeyToTLSMap;
+    typedef std::map<lldb::user_id_t, PthreadKeyToTLSMap> ThreadIDToTLSMap;
     void
     RegisterNotificationCallbacks();
 
@@ -365,11 +371,19 @@ protected:
                     uint32_t image_infos_count, 
                     DYLDImageInfo::collection &image_infos);
 
+    lldb::ModuleSP
+    GetPThreadLibraryModule();
+
+    lldb_private::Address
+    GetPthreadSetSpecificAddress();
 
     DYLDImageInfo m_dyld;               // Info about the current dyld being used
     lldb::ModuleWP m_dyld_module_wp;
+    lldb::ModuleWP m_libpthread_module_wp;
+    lldb_private::Address m_pthread_getspecific_addr;
     lldb::addr_t m_dyld_all_image_infos_addr;
     DYLDAllImageInfos m_dyld_all_image_infos;
+    ThreadIDToTLSMap m_tid_to_tls_map;
     uint32_t m_dyld_all_image_infos_stop_id;
     lldb::user_id_t m_break_id;
     DYLDImageInfo::collection m_dyld_image_infos;   // Current shared libraries information

Modified: lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp Fri Jul  1 12:17:23 2016
@@ -599,7 +599,8 @@ DynamicLoaderPOSIXDYLD::GetEntryPoint()
 }
 
 lldb::addr_t
-DynamicLoaderPOSIXDYLD::GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread)
+DynamicLoaderPOSIXDYLD::GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread,
+                                           lldb::addr_t tls_file_addr)
 {
     auto it = m_loaded_modules.find (module);
     if (it == m_loaded_modules.end())
@@ -641,7 +642,7 @@ DynamicLoaderPOSIXDYLD::GetThreadLocalDa
                     "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64 ", modid=%" PRId64 ", tls_block=0x%" PRIx64 "\n",
                     mod->GetObjectName().AsCString(""), link_map, tp, (int64_t)modid, tls_block);
 
-    return tls_block;
+    return tls_block + tls_file_addr;
 }
 
 void

Modified: lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h (original)
+++ lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h Fri Jul  1 12:17:23 2016
@@ -61,7 +61,7 @@ public:
     CanLoadImage() override;
 
     lldb::addr_t
-    GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread) override;
+    GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread, lldb::addr_t tls_file_addr) override;
 
     //------------------------------------------------------------------
     // PluginInterface protocol

Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp (original)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp Fri Jul  1 12:17:23 2016
@@ -4414,6 +4414,7 @@ SymbolFileDWARF::ParseVariableDIE
                         GetObjectFile()->GetModule()->ReportError ("0x%8.8x: %s has an invalid location: %s", die.GetOffset(), die.GetTagAsCString(), strm.GetString().c_str());
                     }
                 }
+                SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
 
                 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
                 {
@@ -4422,9 +4423,6 @@ SymbolFileDWARF::ParseVariableDIE
                     else
                         scope = eValueTypeVariableStatic;
                     
-                    
-                    SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
-                    
                     if (debug_map_symfile)
                     {
                         // When leaving the DWARF in the .o files on darwin,
@@ -4502,7 +4500,22 @@ SymbolFileDWARF::ParseVariableDIE
                     if (location_is_const_value_data)
                         scope = eValueTypeVariableStatic;
                     else
+                    {
                         scope = eValueTypeVariableLocal;
+                        if (debug_map_symfile)
+                        {
+                            // We need to check for TLS addresses that we need to fixup
+                            if (location.ContainsThreadLocalStorage())
+                            {
+                                location.LinkThreadLocalStorage(
+                                    debug_map_symfile->GetObjectFile()->GetModule(),
+                                    [this, debug_map_symfile](lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
+                                        return debug_map_symfile->LinkOSOFileAddress(this, unlinked_file_addr);
+                                    });
+                                scope = eValueTypeVariableThreadLocal;
+                            }
+                        }
+                    }
                 }
             }
 

Modified: lldb/trunk/source/Symbol/SymbolContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/SymbolContext.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/SymbolContext.cpp (original)
+++ lldb/trunk/source/Symbol/SymbolContext.cpp Fri Jul  1 12:17:23 2016
@@ -365,6 +365,10 @@ SymbolContext::GetDescription(Stream *s,
                 s->PutCString("kind = local, ");
                 break;
 
+            case eValueTypeVariableThreadLocal:
+                s->PutCString("kind = thread local, ");
+                break;
+
             default:
                 break;
         }

Modified: lldb/trunk/source/Symbol/Variable.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/Variable.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/Variable.cpp (original)
+++ lldb/trunk/source/Symbol/Variable.cpp Fri Jul  1 12:17:23 2016
@@ -157,8 +157,13 @@ Variable::Dump(Stream *s, bool show_cont
         switch (m_scope)
         {
         case eValueTypeVariableGlobal:       s->PutCString(m_external ? "global" : "static"); break;
-        case eValueTypeVariableArgument:    s->PutCString("parameter"); break;
+        case eValueTypeVariableArgument:
+            s->PutCString("parameter");
+            break;
         case eValueTypeVariableLocal:        s->PutCString("local"); break;
+        case eValueTypeVariableThreadLocal:
+            s->PutCString("thread local");
+            break;
         default:            *s << "??? (" << m_scope << ')';
         }
     }
@@ -344,6 +349,7 @@ Variable::IsInScope (StackFrame *frame)
     case eValueTypeConstResult:
     case eValueTypeVariableGlobal:
     case eValueTypeVariableStatic:
+    case eValueTypeVariableThreadLocal:
         return true;
 
     case eValueTypeVariableArgument:

Modified: lldb/trunk/source/Target/Thread.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Thread.cpp?rev=274366&r1=274365&r2=274366&view=diff
==============================================================================
--- lldb/trunk/source/Target/Thread.cpp (original)
+++ lldb/trunk/source/Target/Thread.cpp Fri Jul  1 12:17:23 2016
@@ -2090,13 +2090,13 @@ Thread::GetThreadPointer ()
 }
 
 addr_t
-Thread::GetThreadLocalData (const ModuleSP module)
+Thread::GetThreadLocalData(const ModuleSP module, lldb::addr_t tls_file_addr)
 {
     // The default implementation is to ask the dynamic loader for it.
     // This can be overridden for specific platforms.
     DynamicLoader *loader = GetProcess()->GetDynamicLoader();
     if (loader)
-        return loader->GetThreadLocalData (module, shared_from_this());
+        return loader->GetThreadLocalData(module, shared_from_this(), tls_file_addr);
     else
         return LLDB_INVALID_ADDRESS;
 }




More information about the lldb-commits mailing list