1 //===-- GDBRegistrar.cpp - Registers objects with GDB ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
19 // This must be kept in sync with gdb/gdb/jit.h .
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;
35 struct jit_descriptor {
37 // This should be jit_actions_t, but we want to be specific about the
40 struct jit_code_entry *relevant_entry;
41 struct jit_code_entry *first_entry;
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 };
49 // Debuggers puts a breakpoint in this function.
50 LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() {
51 // The noinline and the asm prevent calls to this function from being
53 #if !defined(_MSC_VER)
54 asm volatile("":::"memory");
62 // Buffer for an in-memory object file in executable memory
63 typedef llvm::DenseMap< const char*,
64 std::pair<std::size_t, jit_code_entry*> >
65 RegisteredObjectBufferMap;
67 /// Global access point for the JIT debugging interface designed for use with a
68 /// singleton toolbox. Handles thread-safe registration and deregistration of
69 /// object files that are in executable memory managed by the client of this
71 class GDBJITRegistrar : public JITRegistrar {
72 /// A map of in-memory object files that have been registered with the
74 RegisteredObjectBufferMap ObjectBufferMap;
77 /// Instantiates the JIT service.
78 GDBJITRegistrar() : ObjectBufferMap() {}
80 /// Unregisters each object that was previously registered and releases all
81 /// internal resources.
82 virtual ~GDBJITRegistrar();
84 /// Creates an entry in the JIT registry for the buffer @p Object,
85 /// which must contain an object file in executable memory with any
86 /// debug information for the debugger.
87 void registerObject(const ObjectBuffer &Object);
89 /// Removes the internal registration of @p Object, and
90 /// frees associated resources.
91 /// Returns true if @p Object was found in ObjectBufferMap.
92 bool deregisterObject(const ObjectBuffer &Object);
95 /// Deregister the debug info for the given object file from the debugger
96 /// and delete any temporary copies. This private method does not remove
97 /// the function from Map so that it can be called while iterating over Map.
98 void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I);
101 /// Lock used to serialize all jit registration events, since they
102 /// modify global variables.
103 llvm::sys::Mutex JITDebugLock;
105 /// Acquire the lock and do the registration.
106 void NotifyDebugger(jit_code_entry* JITCodeEntry) {
107 llvm::MutexGuard locked(JITDebugLock);
108 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
110 // Insert this entry at the head of the list.
111 JITCodeEntry->prev_entry = NULL;
112 jit_code_entry* NextEntry = __jit_debug_descriptor.first_entry;
113 JITCodeEntry->next_entry = NextEntry;
114 if (NextEntry != NULL) {
115 NextEntry->prev_entry = JITCodeEntry;
117 __jit_debug_descriptor.first_entry = JITCodeEntry;
118 __jit_debug_descriptor.relevant_entry = JITCodeEntry;
119 __jit_debug_register_code();
122 GDBJITRegistrar::~GDBJITRegistrar() {
123 // Free all registered object files.
124 for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(), E = ObjectBufferMap.end();
126 // Call the private method that doesn't update the map so our iterator
128 deregisterObjectInternal(I);
130 ObjectBufferMap.clear();
133 void GDBJITRegistrar::registerObject(const ObjectBuffer &Object) {
135 const char *Buffer = Object.getBufferStart();
136 size_t Size = Object.getBufferSize();
138 assert(Buffer && "Attempt to register a null object with a debugger.");
139 assert(ObjectBufferMap.find(Buffer) == ObjectBufferMap.end() &&
140 "Second attempt to perform debug registration.");
141 jit_code_entry* JITCodeEntry = new jit_code_entry();
143 if (JITCodeEntry == 0) {
144 llvm::report_fatal_error(
145 "Allocation failed when registering a JIT entry!\n");
148 JITCodeEntry->symfile_addr = Buffer;
149 JITCodeEntry->symfile_size = Size;
151 ObjectBufferMap[Buffer] = std::make_pair(Size, JITCodeEntry);
152 NotifyDebugger(JITCodeEntry);
156 bool GDBJITRegistrar::deregisterObject(const ObjectBuffer& Object) {
157 const char *Buffer = Object.getBufferStart();
158 RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(Buffer);
160 if (I != ObjectBufferMap.end()) {
161 deregisterObjectInternal(I);
162 ObjectBufferMap.erase(I);
168 void GDBJITRegistrar::deregisterObjectInternal(
169 RegisteredObjectBufferMap::iterator I) {
171 jit_code_entry*& JITCodeEntry = I->second.second;
173 // Acquire the lock and do the unregistration.
175 llvm::MutexGuard locked(JITDebugLock);
176 __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN;
178 // Remove the jit_code_entry from the linked list.
179 jit_code_entry* PrevEntry = JITCodeEntry->prev_entry;
180 jit_code_entry* NextEntry = JITCodeEntry->next_entry;
183 NextEntry->prev_entry = PrevEntry;
186 PrevEntry->next_entry = NextEntry;
189 assert(__jit_debug_descriptor.first_entry == JITCodeEntry);
190 __jit_debug_descriptor.first_entry = NextEntry;
193 // Tell the debugger which entry we removed, and unregister the code.
194 __jit_debug_descriptor.relevant_entry = JITCodeEntry;
195 __jit_debug_register_code();
206 JITRegistrar& JITRegistrar::getGDBRegistrar() {
207 static GDBJITRegistrar* sRegistrar = NULL;
208 if (sRegistrar == NULL) {
209 // The mutex is here so that it won't slow down access once the registrar
211 llvm::MutexGuard locked(JITDebugLock);
212 // Check again to be sure another thread didn't create this while we waited
213 if (sRegistrar == NULL) {
214 sRegistrar = new GDBJITRegistrar;