Richard Mitton 0a55835755 Added support for reading thread-local storage variables, as defined using the __thread modifier.
To make this work this patch extends LLDB to:

- Explicitly track the link_map address for each module. This is effectively the module handle, not sure why it wasn't already being stored off anywhere. As an extension later, it would be nice if someone were to add support for printing this as part of the modules list.

- Allow reading the per-thread data pointer via ptrace. I have added support for Linux here. I'll be happy to add support for FreeBSD once this is reviewed. OS X does not appear to have __thread variables, so maybe we don't need it there. Windows support should eventually be workable along the same lines.

- Make DWARF expressions track which module they originated from.

- Add support for the DW_OP_GNU_push_tls_address DWARF opcode, as generated by gcc and recent versions of clang. Earlier versions of clang (such as 3.2, which is default on Ubuntu right now) do not generate TLS debug info correctly so can not be supported here.

- Understand the format of the pthread DTV block. This is where it gets tricky. We have three basic options here:

  1) Call "dlinfo" or "__tls_get_addr" on the inferior and ask it directly. However this won't work on core dumps, and generally speaking it's not a good idea for the debugger to call functions itself, as it has the potential to not work depending on the state of the target.

  2) Use libthread_db. This is what GDB does. However this option requires having a version of libthread_db on the host cross-compiled for each potential target. This places a large burden on the user, and would make it very hard to cross-debug from Windows to Linux, for example. Trying to build a library intended exclusively for one OS on a different one is not pleasant. GDB sidesteps the problem and asks the user to figure it out.

  3) Parse the DTV structure ourselves. On initial inspection this seems to be a bad option, as the DTV structure (the format used by the runtime to manage TLS data) is not in fact a kernel data structure, it is implemented entirely in useerland in libc. Therefore the layout of it's fields are version and OS dependent, and are not standardized.

  However, it turns out not to be such a problem. All OSes use basically the same algorithm (a per-module lookup table) as detailed in Ulrich Drepper's TLS ELF ABI document, so we can easily write code to decode it ourselves. The only question therefore is the exact field layouts required. Happily, the implementors of libpthread expose the structure of the DTV via metadata exported as symbols from the .so itself, designed exactly for this kind of thing. So this patch simply reads that metadata in, and re-implements libthread_db's algorithm itself. We thereby get cross-platform TLS lookup without either requiring third-party libraries, while still being independent of the version of libpthread being used.

Test case included.

llvm-svn: 192922
2013-10-17 21:14:00 +00:00

136 lines
3.5 KiB
C++

//===-- POSIXThread.h -------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_POSIXThread_H_
#define liblldb_POSIXThread_H_
// C Includes
// C++ Includes
#include <memory>
#include <string>
// Other libraries and framework includes
#include "lldb/Target/Thread.h"
#include "RegisterContextPOSIX.h"
class ProcessMessage;
class ProcessMonitor;
class POSIXBreakpointProtocol;
//------------------------------------------------------------------------------
// @class POSIXThread
// @brief Abstraction of a POSIX thread.
class POSIXThread
: public lldb_private::Thread
{
public:
POSIXThread(lldb_private::Process &process, lldb::tid_t tid);
virtual ~POSIXThread();
void
RefreshStateAfterStop();
virtual void
WillResume(lldb::StateType resume_state);
// This notifies the thread when a private stop occurs.
virtual void
DidStop ();
const char *
GetInfo();
void
SetName (const char *name);
const char *
GetName ();
virtual lldb::RegisterContextSP
GetRegisterContext();
virtual lldb::RegisterContextSP
CreateRegisterContextForFrame (lldb_private::StackFrame *frame);
virtual lldb::addr_t
GetThreadPointer ();
//--------------------------------------------------------------------------
// These functions provide a mapping from the register offset
// back to the register index or name for use in debugging or log
// output.
unsigned
GetRegisterIndexFromOffset(unsigned offset);
const char *
GetRegisterName(unsigned reg);
const char *
GetRegisterNameFromOffset(unsigned offset);
//--------------------------------------------------------------------------
// These methods form a specialized interface to POSIX threads.
//
bool Resume();
void Notify(const ProcessMessage &message);
//--------------------------------------------------------------------------
// These methods provide an interface to watchpoints
//
bool EnableHardwareWatchpoint(lldb_private::Watchpoint *wp);
bool DisableHardwareWatchpoint(lldb_private::Watchpoint *wp);
uint32_t NumSupportedHardwareWatchpoints();
uint32_t FindVacantWatchpointIndex();
protected:
POSIXBreakpointProtocol *
GetPOSIXBreakpointProtocol ()
{
if (!m_reg_context_sp)
m_reg_context_sp = GetRegisterContext();
return m_posix_thread;
}
std::unique_ptr<lldb_private::StackFrame> m_frame_ap;
lldb::BreakpointSiteSP m_breakpoint;
bool m_thread_name_valid;
std::string m_thread_name;
POSIXBreakpointProtocol *m_posix_thread;
ProcessMonitor &
GetMonitor();
virtual bool
CalculateStopInfo();
void BreakNotify(const ProcessMessage &message);
void WatchNotify(const ProcessMessage &message);
virtual void TraceNotify(const ProcessMessage &message);
void LimboNotify(const ProcessMessage &message);
void SignalNotify(const ProcessMessage &message);
void SignalDeliveredNotify(const ProcessMessage &message);
void CrashNotify(const ProcessMessage &message);
void ThreadNotify(const ProcessMessage &message);
void ExitNotify(const ProcessMessage &message);
void ExecNotify(const ProcessMessage &message);
lldb_private::Unwind *
GetUnwinder();
};
#endif // #ifndef liblldb_POSIXThread_H_