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
189 lines
5.8 KiB
C++
189 lines
5.8 KiB
C++
//===-- DynamicLoaderPOSIX.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_DynamicLoaderPOSIX_H_
|
|
#define liblldb_DynamicLoaderPOSIX_H_
|
|
|
|
// C Includes
|
|
// C++ Includes
|
|
// Other libraries and framework includes
|
|
#include "lldb/Breakpoint/StoppointCallbackContext.h"
|
|
#include "lldb/Target/DynamicLoader.h"
|
|
|
|
#include "DYLDRendezvous.h"
|
|
|
|
class AuxVector;
|
|
|
|
class DynamicLoaderPOSIXDYLD : public lldb_private::DynamicLoader
|
|
{
|
|
public:
|
|
|
|
static void
|
|
Initialize();
|
|
|
|
static void
|
|
Terminate();
|
|
|
|
static lldb_private::ConstString
|
|
GetPluginNameStatic();
|
|
|
|
static const char *
|
|
GetPluginDescriptionStatic();
|
|
|
|
static lldb_private::DynamicLoader *
|
|
CreateInstance(lldb_private::Process *process, bool force);
|
|
|
|
DynamicLoaderPOSIXDYLD(lldb_private::Process *process);
|
|
|
|
virtual
|
|
~DynamicLoaderPOSIXDYLD();
|
|
|
|
//------------------------------------------------------------------
|
|
// DynamicLoader protocol
|
|
//------------------------------------------------------------------
|
|
|
|
virtual void
|
|
DidAttach();
|
|
|
|
virtual void
|
|
DidLaunch();
|
|
|
|
virtual lldb::ThreadPlanSP
|
|
GetStepThroughTrampolinePlan(lldb_private::Thread &thread,
|
|
bool stop_others);
|
|
|
|
virtual lldb_private::Error
|
|
CanLoadImage();
|
|
|
|
virtual lldb::addr_t
|
|
GetThreadLocalData (const lldb::ModuleSP module, const lldb::ThreadSP thread);
|
|
|
|
//------------------------------------------------------------------
|
|
// PluginInterface protocol
|
|
//------------------------------------------------------------------
|
|
virtual lldb_private::ConstString
|
|
GetPluginName();
|
|
|
|
virtual uint32_t
|
|
GetPluginVersion();
|
|
|
|
virtual void
|
|
GetPluginCommandHelp(const char *command, lldb_private::Stream *strm);
|
|
|
|
virtual lldb_private::Error
|
|
ExecutePluginCommand(lldb_private::Args &command, lldb_private::Stream *strm);
|
|
|
|
virtual lldb_private::Log *
|
|
EnablePluginLogging(lldb_private::Stream *strm, lldb_private::Args &command);
|
|
|
|
protected:
|
|
/// Runtime linker rendezvous structure.
|
|
DYLDRendezvous m_rendezvous;
|
|
|
|
/// Virtual load address of the inferior process.
|
|
lldb::addr_t m_load_offset;
|
|
|
|
/// Virtual entry address of the inferior process.
|
|
lldb::addr_t m_entry_point;
|
|
|
|
/// Auxiliary vector of the inferior process.
|
|
std::unique_ptr<AuxVector> m_auxv;
|
|
|
|
/// Rendezvous breakpoint.
|
|
lldb::break_id_t m_dyld_bid;
|
|
|
|
/// Loaded module list. (link map for each module)
|
|
std::map<lldb::ModuleWP, lldb::addr_t, std::owner_less<lldb::ModuleWP>> m_loaded_modules;
|
|
|
|
/// Enables a breakpoint on a function called by the runtime
|
|
/// linker each time a module is loaded or unloaded.
|
|
void
|
|
SetRendezvousBreakpoint();
|
|
|
|
/// Callback routine which updates the current list of loaded modules based
|
|
/// on the information supplied by the runtime linker.
|
|
static bool
|
|
RendezvousBreakpointHit(void *baton,
|
|
lldb_private::StoppointCallbackContext *context,
|
|
lldb::user_id_t break_id,
|
|
lldb::user_id_t break_loc_id);
|
|
|
|
/// Helper method for RendezvousBreakpointHit. Updates LLDB's current set
|
|
/// of loaded modules.
|
|
void
|
|
RefreshModules();
|
|
|
|
/// Updates the load address of every allocatable section in @p module.
|
|
///
|
|
/// @param module The module to traverse.
|
|
///
|
|
/// @param link_map_addr The virtual address of the link map for the @p module.
|
|
///
|
|
/// @param base_addr The virtual base address @p module is loaded at.
|
|
void
|
|
UpdateLoadedSections(lldb::ModuleSP module,
|
|
lldb::addr_t link_map_addr,
|
|
lldb::addr_t base_addr);
|
|
|
|
/// Removes the loaded sections from the target in @p module.
|
|
///
|
|
/// @param module The module to traverse.
|
|
void
|
|
UnloadSections(const lldb::ModuleSP module);
|
|
|
|
/// Locates or creates a module given by @p file and updates/loads the
|
|
/// resulting module at the virtual base address @p base_addr.
|
|
lldb::ModuleSP
|
|
LoadModuleAtAddress(const lldb_private::FileSpec &file, lldb::addr_t link_map_addr, lldb::addr_t base_addr);
|
|
|
|
/// Resolves the entry point for the current inferior process and sets a
|
|
/// breakpoint at that address.
|
|
void
|
|
ProbeEntry();
|
|
|
|
/// Callback routine invoked when we hit the breakpoint on process entry.
|
|
///
|
|
/// This routine is responsible for resolving the load addresses of all
|
|
/// dependent modules required by the inferior and setting up the rendezvous
|
|
/// breakpoint.
|
|
static bool
|
|
EntryBreakpointHit(void *baton,
|
|
lldb_private::StoppointCallbackContext *context,
|
|
lldb::user_id_t break_id,
|
|
lldb::user_id_t break_loc_id);
|
|
|
|
/// Helper for the entry breakpoint callback. Resolves the load addresses
|
|
/// of all dependent modules.
|
|
void
|
|
LoadAllCurrentModules();
|
|
|
|
/// Computes a value for m_load_offset returning the computed address on
|
|
/// success and LLDB_INVALID_ADDRESS on failure.
|
|
lldb::addr_t
|
|
ComputeLoadOffset();
|
|
|
|
/// Computes a value for m_entry_point returning the computed address on
|
|
/// success and LLDB_INVALID_ADDRESS on failure.
|
|
lldb::addr_t
|
|
GetEntryPoint();
|
|
|
|
/// Checks to see if the target module has changed, updates the target
|
|
/// accordingly and returns the target executable module.
|
|
lldb::ModuleSP
|
|
GetTargetExecutable();
|
|
|
|
private:
|
|
DISALLOW_COPY_AND_ASSIGN(DynamicLoaderPOSIXDYLD);
|
|
|
|
const lldb_private::SectionList *
|
|
GetSectionListFromModule(const lldb::ModuleSP module) const;
|
|
};
|
|
|
|
#endif // liblldb_DynamicLoaderPOSIXDYLD_H_
|