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