LLVM  3.7.0
GDBRegistrationListener.cpp
Go to the documentation of this file.
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/ADT/DenseMap.h"
12 #include "llvm/Object/ObjectFile.h"
13 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/Mutex.h"
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 {
29  } jit_actions_t;
30 
31  struct jit_code_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;
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  struct jit_descriptor __jit_debug_descriptor = { 1, 0, nullptr, nullptr };
51 
52  // Debuggers puts a breakpoint in this function.
54  // The noinline and the asm prevent calls to this function from being
55  // optimized out.
56 #if !defined(_MSC_VER)
57  asm volatile("":::"memory");
58 #endif
59  }
60 
61 }
62 
63 namespace {
64 
65 struct RegisteredObjectInfo {
66  RegisteredObjectInfo() {}
67 
68  RegisteredObjectInfo(std::size_t Size, jit_code_entry *Entry,
70  : Size(Size), Entry(Entry), Obj(std::move(Obj)) {}
71 
72  RegisteredObjectInfo(RegisteredObjectInfo &&Other)
73  : Size(Other.Size), Entry(Other.Entry), Obj(std::move(Other.Obj)) {}
74 
75  RegisteredObjectInfo& operator=(RegisteredObjectInfo &&Other) {
76  Size = Other.Size;
77  Entry = Other.Entry;
78  Obj = std::move(Other.Obj);
79  return *this;
80  }
81 
82  std::size_t Size;
83  jit_code_entry *Entry;
85 };
86 
87 // Buffer for an in-memory object file in executable memory
89  RegisteredObjectBufferMap;
90 
91 /// Global access point for the JIT debugging interface designed for use with a
92 /// singleton toolbox. Handles thread-safe registration and deregistration of
93 /// object files that are in executable memory managed by the client of this
94 /// class.
95 class GDBJITRegistrationListener : public JITEventListener {
96  /// A map of in-memory object files that have been registered with the
97  /// JIT interface.
98  RegisteredObjectBufferMap ObjectBufferMap;
99 
100 public:
101  /// Instantiates the JIT service.
102  GDBJITRegistrationListener() : ObjectBufferMap() {}
103 
104  /// Unregisters each object that was previously registered and releases all
105  /// internal resources.
106  ~GDBJITRegistrationListener() override;
107 
108  /// Creates an entry in the JIT registry for the buffer @p Object,
109  /// which must contain an object file in executable memory with any
110  /// debug information for the debugger.
111  void NotifyObjectEmitted(const ObjectFile &Object,
112  const RuntimeDyld::LoadedObjectInfo &L) override;
113 
114  /// Removes the internal registration of @p Object, and
115  /// frees associated resources.
116  /// Returns true if @p Object was found in ObjectBufferMap.
117  void NotifyFreeingObject(const ObjectFile &Object) override;
118 
119 private:
120  /// Deregister the debug info for the given object file from the debugger
121  /// and delete any temporary copies. This private method does not remove
122  /// the function from Map so that it can be called while iterating over Map.
123  void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I);
124 };
125 
126 /// Lock used to serialize all jit registration events, since they
127 /// modify global variables.
128 ManagedStatic<sys::Mutex> JITDebugLock;
129 
130 /// Do the registration.
131 void NotifyDebugger(jit_code_entry* JITCodeEntry) {
133 
134  // Insert this entry at the head of the list.
135  JITCodeEntry->prev_entry = nullptr;
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;
144 }
145 
146 GDBJITRegistrationListener::~GDBJITRegistrationListener() {
147  // Free all registered object files.
148  llvm::MutexGuard locked(*JITDebugLock);
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::NotifyObjectEmitted(
160  const ObjectFile &Object,
162 
163  OwningBinary<ObjectFile> DebugObj = L.getObjectForDebug(Object);
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  const char *Key = Object.getMemoryBufferRef().getBufferStart();
173 
174  assert(Key && "Attempt to register a null object with a debugger.");
175  llvm::MutexGuard locked(*JITDebugLock);
176  assert(ObjectBufferMap.find(Key) == ObjectBufferMap.end() &&
177  "Second attempt to perform debug registration.");
178  jit_code_entry* JITCodeEntry = new jit_code_entry();
179 
180  if (!JITCodeEntry) {
182  "Allocation failed when registering a JIT entry!\n");
183  } else {
184  JITCodeEntry->symfile_addr = Buffer;
185  JITCodeEntry->symfile_size = Size;
186 
187  ObjectBufferMap[Key] = RegisteredObjectInfo(Size, JITCodeEntry,
188  std::move(DebugObj));
189  NotifyDebugger(JITCodeEntry);
190  }
191 }
192 
193 void GDBJITRegistrationListener::NotifyFreeingObject(const ObjectFile& Object) {
194  const char *Key = Object.getMemoryBufferRef().getBufferStart();
195  llvm::MutexGuard locked(*JITDebugLock);
196  RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(Key);
197 
198  if (I != ObjectBufferMap.end()) {
199  deregisterObjectInternal(I);
200  ObjectBufferMap.erase(I);
201  }
202 }
203 
204 void GDBJITRegistrationListener::deregisterObjectInternal(
205  RegisteredObjectBufferMap::iterator I) {
206 
207  jit_code_entry*& JITCodeEntry = I->second.Entry;
208 
209  // Do the unregistration.
210  {
212 
213  // Remove the jit_code_entry from the linked list.
214  jit_code_entry* PrevEntry = JITCodeEntry->prev_entry;
215  jit_code_entry* NextEntry = JITCodeEntry->next_entry;
216 
217  if (NextEntry) {
218  NextEntry->prev_entry = PrevEntry;
219  }
220  if (PrevEntry) {
221  PrevEntry->next_entry = NextEntry;
222  }
223  else {
224  assert(__jit_debug_descriptor.first_entry == JITCodeEntry);
226  }
227 
228  // Tell the debugger which entry we removed, and unregister the code.
229  __jit_debug_descriptor.relevant_entry = JITCodeEntry;
231  }
232 
233  delete JITCodeEntry;
234  JITCodeEntry = nullptr;
235 }
236 
238 
239 } // end namespace
240 
241 namespace llvm {
242 
244  return &*GDBRegListener;
245 }
246 
247 } // namespace llvm
Information about the loaded object.
Definition: RuntimeDyld.h:59
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
static JITEventListener * createGDBRegistrationListener()
This class is the base class for all object file types.
Definition: ObjectFile.h:176
const char * getBufferStart() const
Definition: MemoryBuffer.h:161
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
const char * symfile_addr
struct jit_descriptor __jit_debug_descriptor
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:591
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:37
struct jit_code_entry * first_entry
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Definition: MutexGuard.h:27
LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code()
size_t getBufferSize() const
Definition: MemoryBuffer.h:163
virtual object::OwningBinary< object::ObjectFile > getObjectForDebug(const object::ObjectFile &Obj) const =0
#define LLVM_ATTRIBUTE_NOINLINE
LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, mark a method "not for inl...
Definition: Compiler.h:184
struct jit_code_entry * prev_entry
uint64_t symfile_size
struct jit_code_entry * next_entry
#define I(x, y, z)
Definition: MD5.cpp:54
struct jit_code_entry * relevant_entry
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61