summaryrefslogtreecommitdiffstats
path: root/lldb/source/Plugins/Process
diff options
context:
space:
mode:
authorRichard Mitton <richard@codersnotes.com>2013-10-17 21:14:00 +0000
committerRichard Mitton <richard@codersnotes.com>2013-10-17 21:14:00 +0000
commit0a558357553641dd8b336bdb4bf5d128f90811cf (patch)
treef651786a348f43e130819304028830d938824100 /lldb/source/Plugins/Process
parentbe2a55f5ac9c02ac4b55112f27c3fb6bad6523cb (diff)
downloadbcm5719-llvm-0a558357553641dd8b336bdb4bf5d128f90811cf.tar.gz
bcm5719-llvm-0a558357553641dd8b336bdb4bf5d128f90811cf.zip
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
Diffstat (limited to 'lldb/source/Plugins/Process')
-rw-r--r--lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp91
-rw-r--r--lldb/source/Plugins/Process/Linux/ProcessMonitor.h4
-rw-r--r--lldb/source/Plugins/Process/POSIX/POSIXThread.cpp11
-rw-r--r--lldb/source/Plugins/Process/POSIX/POSIXThread.h3
-rw-r--r--lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp3
5 files changed, 111 insertions, 1 deletions
diff --git a/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp b/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp
index a0cda3485c6..bda0cb33097 100644
--- a/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp
+++ b/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp
@@ -19,6 +19,7 @@
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/types.h>
+#include <sys/user.h>
#include <sys/wait.h>
// C++ Includes
@@ -46,6 +47,19 @@
#ifndef PTRACE_SETREGSET
#define PTRACE_SETREGSET 0x4205
#endif
+#ifndef PTRACE_GET_THREAD_AREA
+ #define PTRACE_GET_THREAD_AREA 25
+#endif
+#ifndef PTRACE_ARCH_PRCTL
+ #define PTRACE_ARCH_PRCTL 30
+#endif
+#ifndef ARCH_GET_FS
+ #define ARCH_SET_GS 0x1001
+ #define ARCH_SET_FS 0x1002
+ #define ARCH_GET_FS 0x1003
+ #define ARCH_GET_GS 0x1004
+#endif
+
// Support hardware breakpoints in case it has not been defined
#ifndef TRAP_HWBKPT
@@ -703,6 +717,74 @@ WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
}
//------------------------------------------------------------------------------
+/// @class ReadThreadPointerOperation
+/// @brief Implements ProcessMonitor::ReadThreadPointer.
+class ReadThreadPointerOperation : public Operation
+{
+public:
+ ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
+ : m_tid(tid), m_addr(addr), m_result(result)
+ { }
+
+ void Execute(ProcessMonitor *monitor);
+
+private:
+ lldb::tid_t m_tid;
+ lldb::addr_t *m_addr;
+ bool &m_result;
+};
+
+void
+ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
+{
+ Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
+ if (log)
+ log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
+
+ // The process for getting the thread area on Linux is
+ // somewhat... obscure. There's several different ways depending on
+ // what arch you're on, and what kernel version you have.
+
+ const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
+ switch(arch.GetMachine())
+ {
+ case llvm::Triple::x86:
+ {
+ // Find the GS register location for our host architecture.
+ size_t gs_user_offset = offsetof(struct user, regs);
+#ifdef __x86_64__
+ gs_user_offset += offsetof(struct user_regs_struct, gs);
+#endif
+#ifdef __i386__
+ gs_user_offset += offsetof(struct user_regs_struct, xgs);
+#endif
+
+ // Read the GS register value to get the selector.
+ errno = 0;
+ long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
+ if (errno)
+ {
+ m_result = false;
+ break;
+ }
+
+ // Read the LDT base for that selector.
+ uint32_t tmp[4];
+ m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
+ *m_addr = tmp[1];
+ break;
+ }
+ case llvm::Triple::x86_64:
+ // Read the FS register base.
+ m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
+ break;
+ default:
+ m_result = false;
+ break;
+ }
+}
+
+//------------------------------------------------------------------------------
/// @class ResumeOperation
/// @brief Implements ProcessMonitor::Resume.
class ResumeOperation : public Operation
@@ -2106,6 +2188,15 @@ ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, un
}
bool
+ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
+{
+ bool result;
+ ReadThreadPointerOperation op(tid, &value, result);
+ DoOperation(&op);
+ return result;
+}
+
+bool
ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
{
bool result;
diff --git a/lldb/source/Plugins/Process/Linux/ProcessMonitor.h b/lldb/source/Plugins/Process/Linux/ProcessMonitor.h
index 3729e257feb..a087140f407 100644
--- a/lldb/source/Plugins/Process/Linux/ProcessMonitor.h
+++ b/lldb/source/Plugins/Process/Linux/ProcessMonitor.h
@@ -147,6 +147,10 @@ public:
bool
WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
+ /// Reads the value of the thread-specific pointer for a given thread ID.
+ bool
+ ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
+
/// Writes a siginfo_t structure corresponding to the given thread ID to the
/// memory region pointed to by @p siginfo.
bool
diff --git a/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp b/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp
index db68875723c..16399748c54 100644
--- a/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp
+++ b/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp
@@ -230,6 +230,17 @@ POSIXThread::CreateRegisterContextForFrame(lldb_private::StackFrame *frame)
return reg_ctx_sp;
}
+lldb::addr_t
+POSIXThread::GetThreadPointer ()
+{
+ ProcessMonitor &monitor = GetMonitor();
+ addr_t addr;
+ if (monitor.ReadThreadPointer (GetID(), addr))
+ return addr;
+ else
+ return LLDB_INVALID_ADDRESS;
+}
+
bool
POSIXThread::CalculateStopInfo()
{
diff --git a/lldb/source/Plugins/Process/POSIX/POSIXThread.h b/lldb/source/Plugins/Process/POSIX/POSIXThread.h
index 5490f1f1ea5..51d6645f209 100644
--- a/lldb/source/Plugins/Process/POSIX/POSIXThread.h
+++ b/lldb/source/Plugins/Process/POSIX/POSIXThread.h
@@ -59,6 +59,9 @@ public:
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
diff --git a/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp b/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp
index 2a01d6a92d9..c19aec3a02c 100644
--- a/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp
+++ b/lldb/source/Plugins/Process/Utility/RegisterContextLLDB.cpp
@@ -1196,7 +1196,8 @@ RegisterContextLLDB::SavedLocationForRegister (uint32_t lldb_regnum, lldb_privat
DataExtractor dwarfdata (unwindplan_regloc.GetDWARFExpressionBytes(),
unwindplan_regloc.GetDWARFExpressionLength(),
process->GetByteOrder(), process->GetAddressByteSize());
- DWARFExpression dwarfexpr (dwarfdata, 0, unwindplan_regloc.GetDWARFExpressionLength());
+ ModuleSP opcode_ctx;
+ DWARFExpression dwarfexpr (opcode_ctx, dwarfdata, 0, unwindplan_regloc.GetDWARFExpressionLength());
dwarfexpr.SetRegisterKind (unwindplan_registerkind);
Value result;
Error error;
OpenPOWER on IntegriCloud