Skip to content

Commit

Permalink
Added support for thread local variables on all Apple OS variants.
Browse files Browse the repository at this point in the history
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>

llvm-svn: 274366
  • Loading branch information
Greg Clayton committed Jul 1, 2016
1 parent 4e3656d commit 63a27af
Show file tree
Hide file tree
Showing 23 changed files with 416 additions and 80 deletions.
12 changes: 10 additions & 2 deletions lldb/include/lldb/Expression/DWARFExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -166,7 +167,14 @@ class DWARFExpression

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
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Target/DynamicLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class DynamicLoader :
/// 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;
}
Expand Down
4 changes: 3 additions & 1 deletion lldb/include/lldb/Target/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,15 @@ class Thread :
/// @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
Expand Down
18 changes: 9 additions & 9 deletions lldb/include/lldb/lldb-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

//----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def setUp(self):
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."""
Expand Down
6 changes: 6 additions & 0 deletions lldb/packages/Python/lldbsuite/test/lang/c/tls_globals/a.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

__thread int var_shared = 33;

int
touch_shared()
{
return var_shared;
}

void shared_check()
{
var_shared *= 2;
Expand Down
14 changes: 10 additions & 4 deletions lldb/packages/Python/lldbsuite/test/lang/c/tls_globals/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
2 changes: 2 additions & 0 deletions lldb/source/API/SBBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ SBBlock::GetVariables (lldb::SBFrame& frame,
{
case eValueTypeVariableGlobal:
case eValueTypeVariableStatic:
case eValueTypeVariableThreadLocal:
add_variable = statics;
break;

Expand Down Expand Up @@ -356,6 +357,7 @@ SBBlock::GetVariables (lldb::SBTarget& target,
{
case eValueTypeVariableGlobal:
case eValueTypeVariableStatic:
case eValueTypeVariableThreadLocal:
add_variable = statics;
break;

Expand Down
51 changes: 25 additions & 26 deletions lldb/source/API/SBFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,33 +901,31 @@ SBFrame::FindValue (const char *name, ValueType value_type, lldb::DynamicValueTy
case eValueTypeVariableStatic: // static variable
case eValueTypeVariableArgument: // function argument variables
case eValueTypeVariableLocal: // function local variables
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)
{
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 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;

Expand Down Expand Up @@ -1201,6 +1199,7 @@ SBFrame::GetVariables (const lldb::SBVariablesOptions& options)
{
case eValueTypeVariableGlobal:
case eValueTypeVariableStatic:
case eValueTypeVariableThreadLocal:
add_variable = statics;
break;

Expand Down
4 changes: 4 additions & 0 deletions lldb/source/API/SBValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Commands/CommandObjectFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ class CommandObjectFrameVariable : public CommandObjectParsed
scope_string = " LOCAL: ";
break;

case eValueTypeVariableThreadLocal:
if (dump_variable && m_option_variable.show_scope)
scope_string = "THREAD: ";
break;
default:
break;
}
Expand Down
5 changes: 5 additions & 0 deletions lldb/source/Commands/CommandObjectTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,11 @@ class CommandObjectTargetVariable : public CommandObjectParsed
s.PutCString(" LOCAL: ");
break;

case eValueTypeVariableThreadLocal:
if (m_option_variable.show_scope)
s.PutCString("THREAD: ");
break;

default:
break;
}
Expand Down
132 changes: 126 additions & 6 deletions lldb/source/Expression/DWARFExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,128 @@ DWARFExpression::Update_DW_OP_addr (lldb::addr_t file_addr)
}

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;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 63a27af

Please sign in to comment.