Search code examples
c++windbgthread-local-storage

How can I inspect the contents of a static thread_local variable on a heap dump with windbg?


Given the following source code:

namespace EventThreadLocal {

  static thread_local std::unique_ptr<std::vector<EventInfo>> Stack;

  static const EventInfo* Top()
  {
    auto stack = Stack.get();
    if (!stack)
      return nullptr;
    if (stack->empty())
      return nullptr;

    return &(stack->back());
  }
}

How can I inspect the contents of the static thread_local variable Stack on a heap dump?

My understanding is that the command !tls displays the thread local storage slots, but how can I know the appropriate slot index used by this variable?


Solution

  • You don't need know the TLS slot associated the the static thread_local variable, the debugger already resolves that with x command. See below sample output that x resolves Stack to different addresses for different threads in WinDbg.

    0:000> x test_exe!Stack
    00000296`a5437410 test_exe!Stack = empty
    0:000>~1s
    0:001> x test_exe!stack
    00000296`a543e230 test_exe!Stack = empty