1 //===----- GDBRegistrationListener.cpp - Registers objects with GDB -------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm-c/ExecutionEngine.h"
10 #include "llvm/ADT/DenseMap.h"
11 #include "llvm/ExecutionEngine/JITEventListener.h"
12 #include "llvm/Object/ObjectFile.h"
13 #include "llvm/Support/Compiler.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/Mutex.h"
19 using namespace llvm::object
;
21 // This must be kept in sync with gdb/gdb/jit.h .
30 struct jit_code_entry
{
31 struct jit_code_entry
*next_entry
;
32 struct jit_code_entry
*prev_entry
;
33 const char *symfile_addr
;
34 uint64_t symfile_size
;
37 struct jit_descriptor
{
39 // This should be jit_actions_t, but we want to be specific about the
42 struct jit_code_entry
*relevant_entry
;
43 struct jit_code_entry
*first_entry
;
46 // We put information about the JITed function in this global, which the
47 // debugger reads. Make sure to specify the version statically, because the
48 // debugger checks the version before we can set it during runtime.
49 extern struct jit_descriptor __jit_debug_descriptor
;
51 // Debuggers puts a breakpoint in this function.
52 extern "C" void __jit_debug_register_code();
57 // FIXME: lli aims to provide both, RuntimeDyld and JITLink, as the dynamic
58 // loaders for its JIT implementations. And they both offer debugging via the
59 // GDB JIT interface, which builds on the two well-known symbol names below.
60 // As these symbols must be unique across the linked executable, we can only
61 // define them in one of the libraries and make the other depend on it.
62 // OrcTargetProcess is a minimal stub for embedding a JIT client in remote
63 // executors. For the moment it seems reasonable to have the definition there
64 // and let ExecutionEngine depend on it, until we find a better solution.
66 LLVM_ATTRIBUTE_USED
void requiredSymbolDefinitionsFromOrcTargetProcess() {
67 errs() << (void *)&__jit_debug_register_code
68 << (void *)&__jit_debug_descriptor
;
71 struct RegisteredObjectInfo
{
72 RegisteredObjectInfo() = default;
74 RegisteredObjectInfo(std::size_t Size
, jit_code_entry
*Entry
,
75 OwningBinary
<ObjectFile
> Obj
)
76 : Size(Size
), Entry(Entry
), Obj(std::move(Obj
)) {}
79 jit_code_entry
*Entry
;
80 OwningBinary
<ObjectFile
> Obj
;
83 // Buffer for an in-memory object file in executable memory
84 typedef llvm::DenseMap
<JITEventListener::ObjectKey
, RegisteredObjectInfo
>
85 RegisteredObjectBufferMap
;
87 /// Global access point for the JIT debugging interface designed for use with a
88 /// singleton toolbox. Handles thread-safe registration and deregistration of
89 /// object files that are in executable memory managed by the client of this
91 class GDBJITRegistrationListener
: public JITEventListener
{
92 /// Lock used to serialize all jit registration events, since they
93 /// modify global variables.
95 /// Only a single instance of GDBJITRegistrationListener is ever created,
96 /// and so the lock can be a member variable of that instance. This ensures
97 /// destructors are run in the correct order.
98 sys::Mutex JITDebugLock
;
100 /// A map of in-memory object files that have been registered with the
102 RegisteredObjectBufferMap ObjectBufferMap
;
104 /// Instantiates the JIT service.
105 GDBJITRegistrationListener() = default;
107 /// Unregisters each object that was previously registered and releases all
108 /// internal resources.
109 ~GDBJITRegistrationListener() override
;
112 static GDBJITRegistrationListener
&instance() {
113 static GDBJITRegistrationListener Instance
;
117 /// Creates an entry in the JIT registry for the buffer @p Object,
118 /// which must contain an object file in executable memory with any
119 /// debug information for the debugger.
120 void notifyObjectLoaded(ObjectKey K
, const ObjectFile
&Obj
,
121 const RuntimeDyld::LoadedObjectInfo
&L
) override
;
123 /// Removes the internal registration of @p Object, and
124 /// frees associated resources.
125 /// Returns true if @p Object was found in ObjectBufferMap.
126 void notifyFreeingObject(ObjectKey K
) override
;
129 /// Deregister the debug info for the given object file from the debugger
130 /// and delete any temporary copies. This private method does not remove
131 /// the function from Map so that it can be called while iterating over Map.
132 void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I
);
135 /// Do the registration.
136 void NotifyDebugger(jit_code_entry
* JITCodeEntry
) {
137 __jit_debug_descriptor
.action_flag
= JIT_REGISTER_FN
;
139 // Insert this entry at the head of the list.
140 JITCodeEntry
->prev_entry
= nullptr;
141 jit_code_entry
* NextEntry
= __jit_debug_descriptor
.first_entry
;
142 JITCodeEntry
->next_entry
= NextEntry
;
144 NextEntry
->prev_entry
= JITCodeEntry
;
146 __jit_debug_descriptor
.first_entry
= JITCodeEntry
;
147 __jit_debug_descriptor
.relevant_entry
= JITCodeEntry
;
148 __jit_debug_register_code();
151 GDBJITRegistrationListener::~GDBJITRegistrationListener() {
152 // Free all registered object files.
153 std::lock_guard
<llvm::sys::Mutex
> locked(JITDebugLock
);
154 for (RegisteredObjectBufferMap::iterator I
= ObjectBufferMap
.begin(),
155 E
= ObjectBufferMap
.end();
157 // Call the private method that doesn't update the map so our iterator
159 deregisterObjectInternal(I
);
161 ObjectBufferMap
.clear();
164 void GDBJITRegistrationListener::notifyObjectLoaded(
165 ObjectKey K
, const ObjectFile
&Obj
,
166 const RuntimeDyld::LoadedObjectInfo
&L
) {
168 OwningBinary
<ObjectFile
> DebugObj
= L
.getObjectForDebug(Obj
);
170 // Bail out if debug objects aren't supported.
171 if (!DebugObj
.getBinary())
174 const char *Buffer
= DebugObj
.getBinary()->getMemoryBufferRef().getBufferStart();
175 size_t Size
= DebugObj
.getBinary()->getMemoryBufferRef().getBufferSize();
177 std::lock_guard
<llvm::sys::Mutex
> locked(JITDebugLock
);
178 assert(!ObjectBufferMap
.contains(K
) &&
179 "Second attempt to perform debug registration.");
180 jit_code_entry
* JITCodeEntry
= new jit_code_entry();
183 llvm::report_fatal_error(
184 "Allocation failed when registering a JIT entry!\n");
186 JITCodeEntry
->symfile_addr
= Buffer
;
187 JITCodeEntry
->symfile_size
= Size
;
190 RegisteredObjectInfo(Size
, JITCodeEntry
, std::move(DebugObj
));
191 NotifyDebugger(JITCodeEntry
);
195 void GDBJITRegistrationListener::notifyFreeingObject(ObjectKey K
) {
196 std::lock_guard
<llvm::sys::Mutex
> locked(JITDebugLock
);
197 RegisteredObjectBufferMap::iterator I
= ObjectBufferMap
.find(K
);
199 if (I
!= ObjectBufferMap
.end()) {
200 deregisterObjectInternal(I
);
201 ObjectBufferMap
.erase(I
);
205 void GDBJITRegistrationListener::deregisterObjectInternal(
206 RegisteredObjectBufferMap::iterator I
) {
208 jit_code_entry
*& JITCodeEntry
= I
->second
.Entry
;
210 // Do the unregistration.
212 __jit_debug_descriptor
.action_flag
= JIT_UNREGISTER_FN
;
214 // Remove the jit_code_entry from the linked list.
215 jit_code_entry
* PrevEntry
= JITCodeEntry
->prev_entry
;
216 jit_code_entry
* NextEntry
= JITCodeEntry
->next_entry
;
219 NextEntry
->prev_entry
= PrevEntry
;
222 PrevEntry
->next_entry
= NextEntry
;
225 assert(__jit_debug_descriptor
.first_entry
== JITCodeEntry
);
226 __jit_debug_descriptor
.first_entry
= NextEntry
;
229 // Tell the debugger which entry we removed, and unregister the code.
230 __jit_debug_descriptor
.relevant_entry
= JITCodeEntry
;
231 __jit_debug_register_code();
235 JITCodeEntry
= nullptr;
242 JITEventListener
* JITEventListener::createGDBRegistrationListener() {
243 return &GDBJITRegistrationListener::instance();
248 LLVMJITEventListenerRef
LLVMCreateGDBRegistrationListener(void)
250 return wrap(JITEventListener::createGDBRegistrationListener());