Home | History | Annotate | Download | only in RuntimeDyld
      1 //===-- GDBRegistrar.cpp - Registers objects with GDB ---------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "JITRegistrar.h"
     11 #include "llvm/ADT/DenseMap.h"
     12 #include "llvm/Support/Compiler.h"
     13 #include "llvm/Support/ErrorHandling.h"
     14 #include "llvm/Support/Mutex.h"
     15 #include "llvm/Support/MutexGuard.h"
     16 #include "llvm/Support/ManagedStatic.h"
     17 
     18 using namespace llvm;
     19 
     20 // This must be kept in sync with gdb/gdb/jit.h .
     21 extern "C" {
     22 
     23   typedef enum {
     24     JIT_NOACTION = 0,
     25     JIT_REGISTER_FN,
     26     JIT_UNREGISTER_FN
     27   } jit_actions_t;
     28 
     29   struct jit_code_entry {
     30     struct jit_code_entry *next_entry;
     31     struct jit_code_entry *prev_entry;
     32     const char *symfile_addr;
     33     uint64_t symfile_size;
     34   };
     35 
     36   struct jit_descriptor {
     37     uint32_t version;
     38     // This should be jit_actions_t, but we want to be specific about the
     39     // bit-width.
     40     uint32_t action_flag;
     41     struct jit_code_entry *relevant_entry;
     42     struct jit_code_entry *first_entry;
     43   };
     44 
     45   // We put information about the JITed function in this global, which the
     46   // debugger reads.  Make sure to specify the version statically, because the
     47   // debugger checks the version before we can set it during runtime.
     48   struct jit_descriptor __jit_debug_descriptor = { 1, 0, nullptr, nullptr };
     49 
     50   // Debuggers puts a breakpoint in this function.
     51   LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() {
     52     // The noinline and the asm prevent calls to this function from being
     53     // optimized out.
     54 #if !defined(_MSC_VER)
     55     asm volatile("":::"memory");
     56 #endif
     57   }
     58 
     59 }
     60 
     61 namespace {
     62 
     63 // Buffer for an in-memory object file in executable memory
     64 typedef llvm::DenseMap< const char*,
     65                         std::pair<std::size_t, jit_code_entry*> >
     66   RegisteredObjectBufferMap;
     67 
     68 /// Global access point for the JIT debugging interface designed for use with a
     69 /// singleton toolbox. Handles thread-safe registration and deregistration of
     70 /// object files that are in executable memory managed by the client of this
     71 /// class.
     72 class GDBJITRegistrar : public JITRegistrar {
     73   /// A map of in-memory object files that have been registered with the
     74   /// JIT interface.
     75   RegisteredObjectBufferMap ObjectBufferMap;
     76 
     77 public:
     78   /// Instantiates the JIT service.
     79   GDBJITRegistrar() : ObjectBufferMap() {}
     80 
     81   /// Unregisters each object that was previously registered and releases all
     82   /// internal resources.
     83   virtual ~GDBJITRegistrar();
     84 
     85   /// Creates an entry in the JIT registry for the buffer @p Object,
     86   /// which must contain an object file in executable memory with any
     87   /// debug information for the debugger.
     88   void registerObject(const ObjectBuffer &Object) override;
     89 
     90   /// Removes the internal registration of @p Object, and
     91   /// frees associated resources.
     92   /// Returns true if @p Object was found in ObjectBufferMap.
     93   bool deregisterObject(const ObjectBuffer &Object) override;
     94 
     95 private:
     96   /// Deregister the debug info for the given object file from the debugger
     97   /// and delete any temporary copies.  This private method does not remove
     98   /// the function from Map so that it can be called while iterating over Map.
     99   void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I);
    100 };
    101 
    102 /// Lock used to serialize all jit registration events, since they
    103 /// modify global variables.
    104 llvm::sys::Mutex JITDebugLock;
    105 
    106 /// Do the registration.
    107 void NotifyDebugger(jit_code_entry* JITCodeEntry) {
    108   __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
    109 
    110   // Insert this entry at the head of the list.
    111   JITCodeEntry->prev_entry = nullptr;
    112   jit_code_entry* NextEntry = __jit_debug_descriptor.first_entry;
    113   JITCodeEntry->next_entry = NextEntry;
    114   if (NextEntry) {
    115     NextEntry->prev_entry = JITCodeEntry;
    116   }
    117   __jit_debug_descriptor.first_entry = JITCodeEntry;
    118   __jit_debug_descriptor.relevant_entry = JITCodeEntry;
    119   __jit_debug_register_code();
    120 }
    121 
    122 GDBJITRegistrar::~GDBJITRegistrar() {
    123   // Free all registered object files.
    124   llvm::MutexGuard locked(JITDebugLock);
    125   for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(), E = ObjectBufferMap.end();
    126        I != E; ++I) {
    127     // Call the private method that doesn't update the map so our iterator
    128     // doesn't break.
    129     deregisterObjectInternal(I);
    130   }
    131   ObjectBufferMap.clear();
    132 }
    133 
    134 void GDBJITRegistrar::registerObject(const ObjectBuffer &Object) {
    135 
    136   const char *Buffer = Object.getBufferStart();
    137   size_t      Size = Object.getBufferSize();
    138 
    139   assert(Buffer && "Attempt to register a null object with a debugger.");
    140   llvm::MutexGuard locked(JITDebugLock);
    141   assert(ObjectBufferMap.find(Buffer) == ObjectBufferMap.end() &&
    142          "Second attempt to perform debug registration.");
    143   jit_code_entry* JITCodeEntry = new jit_code_entry();
    144 
    145   if (!JITCodeEntry) {
    146     llvm::report_fatal_error(
    147       "Allocation failed when registering a JIT entry!\n");
    148   } else {
    149     JITCodeEntry->symfile_addr = Buffer;
    150     JITCodeEntry->symfile_size = Size;
    151 
    152     ObjectBufferMap[Buffer] = std::make_pair(Size, JITCodeEntry);
    153     NotifyDebugger(JITCodeEntry);
    154   }
    155 }
    156 
    157 bool GDBJITRegistrar::deregisterObject(const ObjectBuffer& Object) {
    158   const char *Buffer = Object.getBufferStart();
    159   llvm::MutexGuard locked(JITDebugLock);
    160   RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(Buffer);
    161 
    162   if (I != ObjectBufferMap.end()) {
    163     deregisterObjectInternal(I);
    164     ObjectBufferMap.erase(I);
    165     return true;
    166   }
    167   return false;
    168 }
    169 
    170 void GDBJITRegistrar::deregisterObjectInternal(
    171     RegisteredObjectBufferMap::iterator I) {
    172 
    173   jit_code_entry*& JITCodeEntry = I->second.second;
    174 
    175   // Do the unregistration.
    176   {
    177     __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN;
    178 
    179     // Remove the jit_code_entry from the linked list.
    180     jit_code_entry* PrevEntry = JITCodeEntry->prev_entry;
    181     jit_code_entry* NextEntry = JITCodeEntry->next_entry;
    182 
    183     if (NextEntry) {
    184       NextEntry->prev_entry = PrevEntry;
    185     }
    186     if (PrevEntry) {
    187       PrevEntry->next_entry = NextEntry;
    188     }
    189     else {
    190       assert(__jit_debug_descriptor.first_entry == JITCodeEntry);
    191       __jit_debug_descriptor.first_entry = NextEntry;
    192     }
    193 
    194     // Tell the debugger which entry we removed, and unregister the code.
    195     __jit_debug_descriptor.relevant_entry = JITCodeEntry;
    196     __jit_debug_register_code();
    197   }
    198 
    199   delete JITCodeEntry;
    200   JITCodeEntry = nullptr;
    201 }
    202 
    203 llvm::ManagedStatic<GDBJITRegistrar> TheRegistrar;
    204 
    205 } // end namespace
    206 
    207 namespace llvm {
    208 
    209 JITRegistrar& JITRegistrar::getGDBRegistrar() {
    210   return *TheRegistrar;
    211 }
    212 
    213 } // namespace llvm
    214