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