LLVM API Documentation

GDBRegistrar.cpp
Go to the documentation of this file.
00001 //===-- GDBRegistrar.cpp - Registers objects with GDB ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "JITRegistrar.h"
00011 #include "llvm/ADT/DenseMap.h"
00012 #include "llvm/Support/Compiler.h"
00013 #include "llvm/Support/ErrorHandling.h"
00014 #include "llvm/Support/Mutex.h"
00015 #include "llvm/Support/MutexGuard.h"
00016 #include "llvm/Support/ManagedStatic.h"
00017 
00018 using namespace llvm;
00019 
00020 // This must be kept in sync with gdb/gdb/jit.h .
00021 extern "C" {
00022 
00023   typedef enum {
00024     JIT_NOACTION = 0,
00025     JIT_REGISTER_FN,
00026     JIT_UNREGISTER_FN
00027   } jit_actions_t;
00028 
00029   struct jit_code_entry {
00030     struct jit_code_entry *next_entry;
00031     struct jit_code_entry *prev_entry;
00032     const char *symfile_addr;
00033     uint64_t symfile_size;
00034   };
00035 
00036   struct jit_descriptor {
00037     uint32_t version;
00038     // This should be jit_actions_t, but we want to be specific about the
00039     // bit-width.
00040     uint32_t action_flag;
00041     struct jit_code_entry *relevant_entry;
00042     struct jit_code_entry *first_entry;
00043   };
00044 
00045   // We put information about the JITed function in this global, which the
00046   // debugger reads.  Make sure to specify the version statically, because the
00047   // debugger checks the version before we can set it during runtime.
00048   struct jit_descriptor __jit_debug_descriptor = { 1, 0, nullptr, nullptr };
00049 
00050   // Debuggers puts a breakpoint in this function.
00051   LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() {
00052     // The noinline and the asm prevent calls to this function from being
00053     // optimized out.
00054 #if !defined(_MSC_VER)
00055     asm volatile("":::"memory");
00056 #endif
00057   }
00058 
00059 }
00060 
00061 namespace {
00062 
00063 // Buffer for an in-memory object file in executable memory
00064 typedef llvm::DenseMap< const char*,
00065                         std::pair<std::size_t, jit_code_entry*> >
00066   RegisteredObjectBufferMap;
00067 
00068 /// Global access point for the JIT debugging interface designed for use with a
00069 /// singleton toolbox. Handles thread-safe registration and deregistration of
00070 /// object files that are in executable memory managed by the client of this
00071 /// class.
00072 class GDBJITRegistrar : public JITRegistrar {
00073   /// A map of in-memory object files that have been registered with the
00074   /// JIT interface.
00075   RegisteredObjectBufferMap ObjectBufferMap;
00076 
00077 public:
00078   /// Instantiates the JIT service.
00079   GDBJITRegistrar() : ObjectBufferMap() {}
00080 
00081   /// Unregisters each object that was previously registered and releases all
00082   /// internal resources.
00083   virtual ~GDBJITRegistrar();
00084 
00085   /// Creates an entry in the JIT registry for the buffer @p Object,
00086   /// which must contain an object file in executable memory with any
00087   /// debug information for the debugger.
00088   void registerObject(const ObjectBuffer &Object) override;
00089 
00090   /// Removes the internal registration of @p Object, and
00091   /// frees associated resources.
00092   /// Returns true if @p Object was found in ObjectBufferMap.
00093   bool deregisterObject(const ObjectBuffer &Object) override;
00094 
00095 private:
00096   /// Deregister the debug info for the given object file from the debugger
00097   /// and delete any temporary copies.  This private method does not remove
00098   /// the function from Map so that it can be called while iterating over Map.
00099   void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I);
00100 };
00101 
00102 /// Lock used to serialize all jit registration events, since they
00103 /// modify global variables.
00104 llvm::sys::Mutex JITDebugLock;
00105 
00106 /// Do the registration.
00107 void NotifyDebugger(jit_code_entry* JITCodeEntry) {
00108   __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
00109 
00110   // Insert this entry at the head of the list.
00111   JITCodeEntry->prev_entry = nullptr;
00112   jit_code_entry* NextEntry = __jit_debug_descriptor.first_entry;
00113   JITCodeEntry->next_entry = NextEntry;
00114   if (NextEntry) {
00115     NextEntry->prev_entry = JITCodeEntry;
00116   }
00117   __jit_debug_descriptor.first_entry = JITCodeEntry;
00118   __jit_debug_descriptor.relevant_entry = JITCodeEntry;
00119   __jit_debug_register_code();
00120 }
00121 
00122 GDBJITRegistrar::~GDBJITRegistrar() {
00123   // Free all registered object files.
00124   llvm::MutexGuard locked(JITDebugLock);
00125   for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(), E = ObjectBufferMap.end();
00126        I != E; ++I) {
00127     // Call the private method that doesn't update the map so our iterator
00128     // doesn't break.
00129     deregisterObjectInternal(I);
00130   }
00131   ObjectBufferMap.clear();
00132 }
00133 
00134 void GDBJITRegistrar::registerObject(const ObjectBuffer &Object) {
00135 
00136   const char *Buffer = Object.getBufferStart();
00137   size_t      Size = Object.getBufferSize();
00138 
00139   assert(Buffer && "Attempt to register a null object with a debugger.");
00140   llvm::MutexGuard locked(JITDebugLock);
00141   assert(ObjectBufferMap.find(Buffer) == ObjectBufferMap.end() &&
00142          "Second attempt to perform debug registration.");
00143   jit_code_entry* JITCodeEntry = new jit_code_entry();
00144 
00145   if (!JITCodeEntry) {
00146     llvm::report_fatal_error(
00147       "Allocation failed when registering a JIT entry!\n");
00148   } else {
00149     JITCodeEntry->symfile_addr = Buffer;
00150     JITCodeEntry->symfile_size = Size;
00151 
00152     ObjectBufferMap[Buffer] = std::make_pair(Size, JITCodeEntry);
00153     NotifyDebugger(JITCodeEntry);
00154   }
00155 }
00156 
00157 bool GDBJITRegistrar::deregisterObject(const ObjectBuffer& Object) {
00158   const char *Buffer = Object.getBufferStart();
00159   llvm::MutexGuard locked(JITDebugLock);
00160   RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(Buffer);
00161 
00162   if (I != ObjectBufferMap.end()) {
00163     deregisterObjectInternal(I);
00164     ObjectBufferMap.erase(I);
00165     return true;
00166   }
00167   return false;
00168 }
00169 
00170 void GDBJITRegistrar::deregisterObjectInternal(
00171     RegisteredObjectBufferMap::iterator I) {
00172 
00173   jit_code_entry*& JITCodeEntry = I->second.second;
00174 
00175   // Do the unregistration.
00176   {
00177     __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN;
00178 
00179     // Remove the jit_code_entry from the linked list.
00180     jit_code_entry* PrevEntry = JITCodeEntry->prev_entry;
00181     jit_code_entry* NextEntry = JITCodeEntry->next_entry;
00182 
00183     if (NextEntry) {
00184       NextEntry->prev_entry = PrevEntry;
00185     }
00186     if (PrevEntry) {
00187       PrevEntry->next_entry = NextEntry;
00188     }
00189     else {
00190       assert(__jit_debug_descriptor.first_entry == JITCodeEntry);
00191       __jit_debug_descriptor.first_entry = NextEntry;
00192     }
00193 
00194     // Tell the debugger which entry we removed, and unregister the code.
00195     __jit_debug_descriptor.relevant_entry = JITCodeEntry;
00196     __jit_debug_register_code();
00197   }
00198 
00199   delete JITCodeEntry;
00200   JITCodeEntry = nullptr;
00201 }
00202 
00203 llvm::ManagedStatic<GDBJITRegistrar> TheRegistrar;
00204 
00205 } // end namespace
00206 
00207 namespace llvm {
00208 
00209 JITRegistrar& JITRegistrar::getGDBRegistrar() {
00210   return *TheRegistrar;
00211 }
00212 
00213 } // namespace llvm