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