LLVM  13.0.0git
GDBRegistrationListener.cpp
Go to the documentation of this file.
1 //===----- GDBRegistrationListener.cpp - Registers objects with GDB -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/Object/ObjectFile.h"
13 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/Mutex.h"
17 #include <mutex>
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 {
40  // This should be jit_actions_t, but we want to be specific about the
41  // bit-width.
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.
51 
52  // Debuggers puts a breakpoint in this function.
53  extern "C" void __jit_debug_register_code();
54 }
55 
56 namespace {
57 
58 // FIXME: lli aims to provide both, RuntimeDyld and JITLink, as the dynamic
59 // loaders for it's JIT implementations. And they both offer debugging via the
60 // GDB JIT interface, which builds on the two well-known symbol names below.
61 // As these symbols must be unique accross the linked executable, we can only
62 // define them in one of the libraries and make the other depend on it.
63 // OrcTargetProcess is a minimal stub for embedding a JIT client in remote
64 // executors. For the moment it seems reasonable to have the definition there
65 // and let ExecutionEngine depend on it, until we find a better solution.
66 //
67 LLVM_ATTRIBUTE_USED void requiredSymbolDefinitionsFromOrcTargetProcess() {
68  errs() << (void *)&__jit_debug_register_code
69  << (void *)&__jit_debug_descriptor;
70 }
71 
72 struct RegisteredObjectInfo {
73  RegisteredObjectInfo() {}
74 
75  RegisteredObjectInfo(std::size_t Size, jit_code_entry *Entry,
77  : Size(Size), Entry(Entry), Obj(std::move(Obj)) {}
78 
79  std::size_t Size;
80  jit_code_entry *Entry;
82 };
83 
84 // Buffer for an in-memory object file in executable memory
86  RegisteredObjectBufferMap;
87 
88 /// Global access point for the JIT debugging interface designed for use with a
89 /// singleton toolbox. Handles thread-safe registration and deregistration of
90 /// object files that are in executable memory managed by the client of this
91 /// class.
92 class GDBJITRegistrationListener : public JITEventListener {
93  /// A map of in-memory object files that have been registered with the
94  /// JIT interface.
95  RegisteredObjectBufferMap ObjectBufferMap;
96 
97 public:
98  /// Instantiates the JIT service.
99  GDBJITRegistrationListener() : ObjectBufferMap() {}
100 
101  /// Unregisters each object that was previously registered and releases all
102  /// internal resources.
103  ~GDBJITRegistrationListener() override;
104 
105  /// Creates an entry in the JIT registry for the buffer @p Object,
106  /// which must contain an object file in executable memory with any
107  /// debug information for the debugger.
108  void notifyObjectLoaded(ObjectKey K, const ObjectFile &Obj,
109  const RuntimeDyld::LoadedObjectInfo &L) override;
110 
111  /// Removes the internal registration of @p Object, and
112  /// frees associated resources.
113  /// Returns true if @p Object was found in ObjectBufferMap.
114  void notifyFreeingObject(ObjectKey K) override;
115 
116 private:
117  /// Deregister the debug info for the given object file from the debugger
118  /// and delete any temporary copies. This private method does not remove
119  /// the function from Map so that it can be called while iterating over Map.
120  void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I);
121 };
122 
123 /// Lock used to serialize all jit registration events, since they
124 /// modify global variables.
126 
127 /// Do the registration.
128 void NotifyDebugger(jit_code_entry* JITCodeEntry) {
130 
131  // Insert this entry at the head of the list.
132  JITCodeEntry->prev_entry = nullptr;
134  JITCodeEntry->next_entry = NextEntry;
135  if (NextEntry) {
136  NextEntry->prev_entry = JITCodeEntry;
137  }
138  __jit_debug_descriptor.first_entry = JITCodeEntry;
139  __jit_debug_descriptor.relevant_entry = JITCodeEntry;
141 }
142 
143 GDBJITRegistrationListener::~GDBJITRegistrationListener() {
144  // Free all registered object files.
145  std::lock_guard<llvm::sys::Mutex> locked(*JITDebugLock);
146  for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(),
147  E = ObjectBufferMap.end();
148  I != E; ++I) {
149  // Call the private method that doesn't update the map so our iterator
150  // doesn't break.
151  deregisterObjectInternal(I);
152  }
153  ObjectBufferMap.clear();
154 }
155 
156 void GDBJITRegistrationListener::notifyObjectLoaded(
157  ObjectKey K, const ObjectFile &Obj,
159 
161 
162  // Bail out if debug objects aren't supported.
163  if (!DebugObj.getBinary())
164  return;
165 
166  const char *Buffer = DebugObj.getBinary()->getMemoryBufferRef().getBufferStart();
167  size_t Size = DebugObj.getBinary()->getMemoryBufferRef().getBufferSize();
168 
169  std::lock_guard<llvm::sys::Mutex> locked(*JITDebugLock);
170  assert(ObjectBufferMap.find(K) == ObjectBufferMap.end() &&
171  "Second attempt to perform debug registration.");
172  jit_code_entry* JITCodeEntry = new jit_code_entry();
173 
174  if (!JITCodeEntry) {
176  "Allocation failed when registering a JIT entry!\n");
177  } else {
178  JITCodeEntry->symfile_addr = Buffer;
179  JITCodeEntry->symfile_size = Size;
180 
181  ObjectBufferMap[K] =
182  RegisteredObjectInfo(Size, JITCodeEntry, std::move(DebugObj));
183  NotifyDebugger(JITCodeEntry);
184  }
185 }
186 
187 void GDBJITRegistrationListener::notifyFreeingObject(ObjectKey K) {
188  std::lock_guard<llvm::sys::Mutex> locked(*JITDebugLock);
189  RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(K);
190 
191  if (I != ObjectBufferMap.end()) {
192  deregisterObjectInternal(I);
193  ObjectBufferMap.erase(I);
194  }
195 }
196 
197 void GDBJITRegistrationListener::deregisterObjectInternal(
198  RegisteredObjectBufferMap::iterator I) {
199 
200  jit_code_entry*& JITCodeEntry = I->second.Entry;
201 
202  // Do the unregistration.
203  {
205 
206  // Remove the jit_code_entry from the linked list.
207  jit_code_entry* PrevEntry = JITCodeEntry->prev_entry;
208  jit_code_entry* NextEntry = JITCodeEntry->next_entry;
209 
210  if (NextEntry) {
211  NextEntry->prev_entry = PrevEntry;
212  }
213  if (PrevEntry) {
214  PrevEntry->next_entry = NextEntry;
215  }
216  else {
217  assert(__jit_debug_descriptor.first_entry == JITCodeEntry);
219  }
220 
221  // Tell the debugger which entry we removed, and unregister the code.
222  __jit_debug_descriptor.relevant_entry = JITCodeEntry;
224  }
225 
226  delete JITCodeEntry;
227  JITCodeEntry = nullptr;
228 }
229 
231 
232 } // end namespace
233 
234 namespace llvm {
235 
237  return &*GDBRegListener;
238 }
239 
240 } // namespace llvm
241 
243 {
245 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
jit_descriptor::relevant_entry
struct jit_code_entry * relevant_entry
Definition: GDBRegistrationListener.cpp:43
LLVM_ATTRIBUTE_USED
#define LLVM_ATTRIBUTE_USED
Definition: Compiler.h:143
llvm::MemoryBufferRef::getBufferStart
const char * getBufferStart() const
Definition: MemoryBufferRef.h:35
llvm
This class represents lattice values for constants.
Definition: AllocatorList.h:23
llvm::RuntimeDyld::LoadedObjectInfo::getObjectForDebug
virtual object::OwningBinary< object::ObjectFile > getObjectForDebug(const object::ObjectFile &Obj) const =0
JITDebugLock
ManagedStatic< std::mutex > JITDebugLock
Definition: JITLoaderGDB.cpp:69
ErrorHandling.h
ManagedStatic.h
llvm::object::OwningBinary::getBinary
T * getBinary()
Definition: Binary.h:226
jit_code_entry
Definition: GDBRegistrationListener.cpp:31
jit_descriptor::version
uint32_t version
Definition: GDBRegistrationListener.cpp:39
DenseMap.h
__jit_debug_descriptor
struct jit_descriptor __jit_debug_descriptor
Definition: JITLoaderGDB.cpp:52
llvm::MemoryBufferRef::getBufferSize
size_t getBufferSize() const
Definition: MemoryBufferRef.h:37
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:894
JIT_NOACTION
@ JIT_NOACTION
Definition: GDBRegistrationListener.cpp:26
llvm::object
Definition: ObjectFileTransformer.h:18
llvm::object::Binary::getMemoryBufferRef
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:44
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
llvm::report_fatal_error
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
jit_code_entry::symfile_addr
const char * symfile_addr
Definition: GDBRegistrationListener.cpp:34
jit_actions_t
jit_actions_t
Definition: GDBRegistrationListener.cpp:25
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:714
I
#define I(x, y, z)
Definition: MD5.cpp:59
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1556
jit_code_entry::next_entry
struct jit_code_entry * next_entry
Definition: GDBRegistrationListener.cpp:32
ObjectFile.h
llvm::JITEventListener
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
Definition: JITEventListener.h:41
jit_descriptor::action_flag
uint32_t action_flag
Definition: GDBRegistrationListener.cpp:42
llvm::wrap
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:224
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:225
LLVMCreateGDBRegistrationListener
LLVMJITEventListenerRef LLVMCreateGDBRegistrationListener(void)
Definition: GDBRegistrationListener.cpp:242
uint32_t
Compiler.h
Mutex.h
jit_descriptor::first_entry
struct jit_code_entry * first_entry
Definition: GDBRegistrationListener.cpp:44
std
Definition: BitVector.h:941
jit_code_entry::symfile_size
uint64_t symfile_size
Definition: GDBRegistrationListener.cpp:35
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::RuntimeDyld::LoadedObjectInfo
Information about the loaded object.
Definition: RuntimeDyld.h:70
ExecutionEngine.h
JIT_REGISTER_FN
@ JIT_REGISTER_FN
Definition: GDBRegistrationListener.cpp:27
jit_code_entry::prev_entry
struct jit_code_entry * prev_entry
Definition: GDBRegistrationListener.cpp:33
JIT_UNREGISTER_FN
@ JIT_UNREGISTER_FN
Definition: GDBRegistrationListener.cpp:28
jit_descriptor
Definition: GDBRegistrationListener.cpp:38
llvm::JITEventListener::createGDBRegistrationListener
static JITEventListener * createGDBRegistrationListener()
Definition: GDBRegistrationListener.cpp:236
LLVMJITEventListenerRef
struct LLVMOpaqueJITEventListener * LLVMJITEventListenerRef
Definition: Types.h:163
__jit_debug_register_code
void __jit_debug_register_code()
Definition: JITLoaderGDB.cpp:57
JITEventListener.h