1 //===----- GDBRegistrationListener.cpp - Registers objects with GDB -------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm-c/ExecutionEngine.h" 10 #include "llvm/ADT/DenseMap.h" 11 #include "llvm/ExecutionEngine/JITEventListener.h" 12 #include "llvm/Object/ObjectFile.h" 13 #include "llvm/Support/Compiler.h" 14 #include "llvm/Support/ErrorHandling.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "llvm/Support/Mutex.h" 17 #include <mutex> 18 19 using namespace llvm; 20 using namespace llvm::object; 21 22 // This must be kept in sync with gdb/gdb/jit.h . 23 extern "C" { 24 25 typedef enum { 26 JIT_NOACTION = 0, 27 JIT_REGISTER_FN, 28 JIT_UNREGISTER_FN 29 } jit_actions_t; 30 31 struct jit_code_entry { 32 struct jit_code_entry *next_entry; 33 struct jit_code_entry *prev_entry; 34 const char *symfile_addr; 35 uint64_t symfile_size; 36 }; 37 38 struct jit_descriptor { 39 uint32_t version; 40 // This should be jit_actions_t, but we want to be specific about the 41 // bit-width. 42 uint32_t action_flag; 43 struct jit_code_entry *relevant_entry; 44 struct jit_code_entry *first_entry; 45 }; 46 47 // We put information about the JITed function in this global, which the 48 // debugger reads. Make sure to specify the version statically, because the 49 // debugger checks the version before we can set it during runtime. 50 extern struct jit_descriptor __jit_debug_descriptor; 51 52 // Debuggers puts a breakpoint in this function. 53 extern "C" void __jit_debug_register_code(); 54 } 55 56 namespace { 57 58 // FIXME: lli aims to provide both, RuntimeDyld and JITLink, as the dynamic 59 // loaders for it's JIT implementations. And they both offer debugging via the 60 // GDB JIT interface, which builds on the two well-known symbol names below. 61 // As these symbols must be unique accross the linked executable, we can only 62 // define them in one of the libraries and make the other depend on it. 63 // OrcTargetProcess is a minimal stub for embedding a JIT client in remote 64 // executors. For the moment it seems reasonable to have the definition there 65 // and let ExecutionEngine depend on it, until we find a better solution. 66 // 67 LLVM_ATTRIBUTE_USED void requiredSymbolDefinitionsFromOrcTargetProcess() { 68 errs() << (void *)&__jit_debug_register_code 69 << (void *)&__jit_debug_descriptor; 70 } 71 72 struct RegisteredObjectInfo { 73 RegisteredObjectInfo() = default; 74 75 RegisteredObjectInfo(std::size_t Size, jit_code_entry *Entry, 76 OwningBinary<ObjectFile> Obj) 77 : Size(Size), Entry(Entry), Obj(std::move(Obj)) {} 78 79 std::size_t Size; 80 jit_code_entry *Entry; 81 OwningBinary<ObjectFile> Obj; 82 }; 83 84 // Buffer for an in-memory object file in executable memory 85 typedef llvm::DenseMap<JITEventListener::ObjectKey, RegisteredObjectInfo> 86 RegisteredObjectBufferMap; 87 88 /// Global access point for the JIT debugging interface designed for use with a 89 /// singleton toolbox. Handles thread-safe registration and deregistration of 90 /// object files that are in executable memory managed by the client of this 91 /// class. 92 class GDBJITRegistrationListener : public JITEventListener { 93 /// A map of in-memory object files that have been registered with the 94 /// JIT interface. 95 RegisteredObjectBufferMap ObjectBufferMap; 96 97 public: 98 /// Instantiates the JIT service. 99 GDBJITRegistrationListener() = default; 100 101 /// Unregisters each object that was previously registered and releases all 102 /// internal resources. 103 ~GDBJITRegistrationListener() override; 104 105 /// Creates an entry in the JIT registry for the buffer @p Object, 106 /// which must contain an object file in executable memory with any 107 /// debug information for the debugger. 108 void notifyObjectLoaded(ObjectKey K, const ObjectFile &Obj, 109 const RuntimeDyld::LoadedObjectInfo &L) override; 110 111 /// Removes the internal registration of @p Object, and 112 /// frees associated resources. 113 /// Returns true if @p Object was found in ObjectBufferMap. 114 void notifyFreeingObject(ObjectKey K) override; 115 116 private: 117 /// Deregister the debug info for the given object file from the debugger 118 /// and delete any temporary copies. This private method does not remove 119 /// the function from Map so that it can be called while iterating over Map. 120 void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I); 121 }; 122 123 /// Lock used to serialize all jit registration events, since they 124 /// modify global variables. 125 sys::Mutex &getJITDebugLock() { 126 static sys::Mutex JITDebugLock; 127 return JITDebugLock; 128 } 129 130 /// Do the registration. 131 void NotifyDebugger(jit_code_entry* JITCodeEntry) { 132 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN; 133 134 // Insert this entry at the head of the list. 135 JITCodeEntry->prev_entry = nullptr; 136 jit_code_entry* NextEntry = __jit_debug_descriptor.first_entry; 137 JITCodeEntry->next_entry = NextEntry; 138 if (NextEntry) { 139 NextEntry->prev_entry = JITCodeEntry; 140 } 141 __jit_debug_descriptor.first_entry = JITCodeEntry; 142 __jit_debug_descriptor.relevant_entry = JITCodeEntry; 143 __jit_debug_register_code(); 144 } 145 146 GDBJITRegistrationListener::~GDBJITRegistrationListener() { 147 // Free all registered object files. 148 std::lock_guard<llvm::sys::Mutex> locked(getJITDebugLock()); 149 for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(), 150 E = ObjectBufferMap.end(); 151 I != E; ++I) { 152 // Call the private method that doesn't update the map so our iterator 153 // doesn't break. 154 deregisterObjectInternal(I); 155 } 156 ObjectBufferMap.clear(); 157 } 158 159 void GDBJITRegistrationListener::notifyObjectLoaded( 160 ObjectKey K, const ObjectFile &Obj, 161 const RuntimeDyld::LoadedObjectInfo &L) { 162 163 OwningBinary<ObjectFile> DebugObj = L.getObjectForDebug(Obj); 164 165 // Bail out if debug objects aren't supported. 166 if (!DebugObj.getBinary()) 167 return; 168 169 const char *Buffer = DebugObj.getBinary()->getMemoryBufferRef().getBufferStart(); 170 size_t Size = DebugObj.getBinary()->getMemoryBufferRef().getBufferSize(); 171 172 std::lock_guard<llvm::sys::Mutex> locked(getJITDebugLock()); 173 assert(ObjectBufferMap.find(K) == ObjectBufferMap.end() && 174 "Second attempt to perform debug registration."); 175 jit_code_entry* JITCodeEntry = new jit_code_entry(); 176 177 if (!JITCodeEntry) { 178 llvm::report_fatal_error( 179 "Allocation failed when registering a JIT entry!\n"); 180 } else { 181 JITCodeEntry->symfile_addr = Buffer; 182 JITCodeEntry->symfile_size = Size; 183 184 ObjectBufferMap[K] = 185 RegisteredObjectInfo(Size, JITCodeEntry, std::move(DebugObj)); 186 NotifyDebugger(JITCodeEntry); 187 } 188 } 189 190 void GDBJITRegistrationListener::notifyFreeingObject(ObjectKey K) { 191 std::lock_guard<llvm::sys::Mutex> locked(getJITDebugLock()); 192 RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(K); 193 194 if (I != ObjectBufferMap.end()) { 195 deregisterObjectInternal(I); 196 ObjectBufferMap.erase(I); 197 } 198 } 199 200 void GDBJITRegistrationListener::deregisterObjectInternal( 201 RegisteredObjectBufferMap::iterator I) { 202 203 jit_code_entry*& JITCodeEntry = I->second.Entry; 204 205 // Do the unregistration. 206 { 207 __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN; 208 209 // Remove the jit_code_entry from the linked list. 210 jit_code_entry* PrevEntry = JITCodeEntry->prev_entry; 211 jit_code_entry* NextEntry = JITCodeEntry->next_entry; 212 213 if (NextEntry) { 214 NextEntry->prev_entry = PrevEntry; 215 } 216 if (PrevEntry) { 217 PrevEntry->next_entry = NextEntry; 218 } 219 else { 220 assert(__jit_debug_descriptor.first_entry == JITCodeEntry); 221 __jit_debug_descriptor.first_entry = NextEntry; 222 } 223 224 // Tell the debugger which entry we removed, and unregister the code. 225 __jit_debug_descriptor.relevant_entry = JITCodeEntry; 226 __jit_debug_register_code(); 227 } 228 229 delete JITCodeEntry; 230 JITCodeEntry = nullptr; 231 } 232 233 } // end namespace 234 235 namespace llvm { 236 237 JITEventListener* JITEventListener::createGDBRegistrationListener() { 238 static GDBJITRegistrationListener GDBRegListener; 239 return &GDBRegListener; 240 } 241 242 } // namespace llvm 243 244 LLVMJITEventListenerRef LLVMCreateGDBRegistrationListener(void) 245 { 246 return wrap(JITEventListener::createGDBRegistrationListener()); 247 } 248