[lldb-dev] Access to TLS variables on GNU/Linux

Florian Weimer via lldb-dev lldb-dev at lists.llvm.org
Tue May 14 04:38:57 PDT 2019


I'm trying to access thread-local variables using the API on GNU/Linux.
Here's my test program:

#include <err.h>
#include <sys/wait.h>
#include <unistd.h>

#include <lldb/API/SBDebugger.h>
#include <lldb/API/SBProcess.h>
#include <lldb/API/SBTarget.h>

thread_local int global_tls_variable
  __attribute__ ((tls_model ("initial-exec")))= 17;

int
main(void)
{
  // Target process for the debugger.
  pid_t pid = fork();
  if (pid < 0)
    err(1, "fork");
  if (pid == 0)
    while (true)
      pause();

  lldb::SBDebugger::Initialize();
  {
    lldb::SBDebugger debugger{lldb::SBDebugger::Create()};
    if (!debugger.IsValid())
      errx(1, "SBDebugger::Create failed");

    lldb::SBTarget target{debugger.CreateTarget(nullptr)};
    if (!target.IsValid())
      errx(1, "SBDebugger::CreateTarget failed");

    lldb::SBAttachInfo attachinfo(pid);
    lldb::SBError error;
    lldb::SBProcess process{target.Attach(attachinfo, error)};
    if (!process.IsValid())
      errx(1, "SBTarget::Attach failed: %s", error.GetCString());

    lldb::SBValue value{target.FindFirstGlobalVariable("global_tls_variable")};
    if (!value.IsValid())
      errx(1, "SBTarget::FindFirstGlobalVariable: %s",
           value.GetError().GetCString());
    printf("global_tls_variable (LLDB): %d\n",
           (int) value.GetValueAsSigned());
    printf("value type: %d\n", (int) value.GetValueType());
  }
  lldb::SBDebugger::Terminate();

  if (kill(pid, SIGKILL) != 0)
    err(1, "kill");
  if (waitpid(pid, NULL, 0) < 0)
    err(1, "waitpid");

  return 0;
}

It prints:

global_tls_variable (LLDB): 0
value type: 4

The target process has loaded libpthread.so.0, so it's not the usual
problem of libthread_db not working without libpthread.

On the other hand, I realize now that the lldb command cannot access TLS
variables, either.  Is this expected to work at all?

I'm using lldb-7.0.1-1.fc29.x86_64 from Fedora 29 (which is built around
GCC 8 and glibc 2.28).

Thanks,
Florian


More information about the lldb-dev mailing list