LLVM  8.0.1
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-c/ExecutionEngine.h"
11 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Mutex.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 {
30  } jit_actions_t;
31 
32  struct jit_code_entry {
35  const char *symfile_addr;
36  uint64_t symfile_size;
37  };
38 
39  struct jit_descriptor {
41  // This should be jit_actions_t, but we want to be specific about the
42  // bit-width.
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.
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,
71  : Size(Size), Entry(Entry), Obj(std::move(Obj)) {}
72 
73  std::size_t Size;
74  jit_code_entry *Entry;
76 };
77 
78 // Buffer for an in-memory object file in executable memory
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) {
124 
125  // Insert this entry at the head of the list.
126  JITCodeEntry->prev_entry = nullptr;
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;
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,
153 
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) {
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  {
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);
213  }
214 
215  // Tell the debugger which entry we removed, and unregister the code.
216  __jit_debug_descriptor.relevant_entry = JITCodeEntry;
218  }
219 
220  delete JITCodeEntry;
221  JITCodeEntry = nullptr;
222 }
223 
225 
226 } // end namespace
227 
228 namespace llvm {
229 
231  return &*GDBRegListener;
232 }
233 
234 } // namespace llvm
235 
237 {
239 }
Information about the loaded object.
Definition: RuntimeDyld.h:69
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:202
#define LLVM_ATTRIBUTE_NOINLINE
Definition: Compiler.h:70
const char * symfile_addr
struct jit_descriptor __jit_debug_descriptor
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
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
size_t getBufferSize() const
Definition: MemoryBuffer.h:279
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code()
virtual object::OwningBinary< object::ObjectFile > getObjectForDebug(const object::ObjectFile &Obj) const =0
struct jit_code_entry * prev_entry
uint64_t symfile_size
struct jit_code_entry * next_entry
LLVMJITEventListenerRef LLVMCreateGDBRegistrationListener(void)
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t Size
Definition: Profile.cpp:47
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const char * getBufferStart() const
Definition: MemoryBuffer.h:277
struct LLVMOpaqueJITEventListener * LLVMJITEventListenerRef
Definition: Types.h:164
struct jit_code_entry * relevant_entry
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61