2014-11-26 16:54:40 +00:00
|
|
|
//===----- GDBRegistrationListener.cpp - Registers objects with GDB -------===//
|
2012-04-16 22:26:48 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2014-11-26 16:54:40 +00:00
|
|
|
#include "llvm/ExecutionEngine/JITEventListener.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2012-04-16 22:26:48 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/Mutex.h"
|
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
2014-11-26 16:54:40 +00:00
|
|
|
using namespace llvm::object;
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
// This must be kept in sync with gdb/gdb/jit.h .
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
JIT_NOACTION = 0,
|
|
|
|
JIT_REGISTER_FN,
|
|
|
|
JIT_UNREGISTER_FN
|
|
|
|
} jit_actions_t;
|
|
|
|
|
|
|
|
struct jit_code_entry {
|
|
|
|
struct jit_code_entry *next_entry;
|
|
|
|
struct jit_code_entry *prev_entry;
|
|
|
|
const char *symfile_addr;
|
|
|
|
uint64_t symfile_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct jit_descriptor {
|
|
|
|
uint32_t version;
|
|
|
|
// This should be jit_actions_t, but we want to be specific about the
|
|
|
|
// bit-width.
|
|
|
|
uint32_t action_flag;
|
|
|
|
struct jit_code_entry *relevant_entry;
|
|
|
|
struct jit_code_entry *first_entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We put information about the JITed function in this global, which the
|
|
|
|
// debugger reads. Make sure to specify the version statically, because the
|
|
|
|
// debugger checks the version before we can set it during runtime.
|
2014-04-24 06:44:33 +00:00
|
|
|
struct jit_descriptor __jit_debug_descriptor = { 1, 0, nullptr, nullptr };
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
// Debuggers puts a breakpoint in this function.
|
2014-01-16 04:50:58 +00:00
|
|
|
LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() {
|
|
|
|
// The noinline and the asm prevent calls to this function from being
|
|
|
|
// optimized out.
|
2014-01-16 05:09:32 +00:00
|
|
|
#if !defined(_MSC_VER)
|
2014-01-16 04:50:58 +00:00
|
|
|
asm volatile("":::"memory");
|
2014-01-16 05:09:32 +00:00
|
|
|
#endif
|
2014-01-16 04:50:58 +00:00
|
|
|
}
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
struct RegisteredObjectInfo {
|
|
|
|
RegisteredObjectInfo() {}
|
|
|
|
|
|
|
|
RegisteredObjectInfo(std::size_t Size, jit_code_entry *Entry,
|
|
|
|
OwningBinary<ObjectFile> Obj)
|
|
|
|
: Size(Size), Entry(Entry), Obj(std::move(Obj)) {}
|
|
|
|
|
|
|
|
RegisteredObjectInfo(RegisteredObjectInfo &&Other)
|
|
|
|
: Size(Other.Size), Entry(Other.Entry), Obj(std::move(Other.Obj)) {}
|
|
|
|
|
|
|
|
RegisteredObjectInfo& operator=(RegisteredObjectInfo &&Other) {
|
|
|
|
Size = Other.Size;
|
|
|
|
Entry = Other.Entry;
|
|
|
|
Obj = std::move(Other.Obj);
|
2014-11-26 17:21:41 +00:00
|
|
|
return *this;
|
2014-11-26 16:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t Size;
|
|
|
|
jit_code_entry *Entry;
|
|
|
|
OwningBinary<ObjectFile> Obj;
|
|
|
|
};
|
|
|
|
|
2012-04-16 22:26:48 +00:00
|
|
|
// Buffer for an in-memory object file in executable memory
|
2014-11-26 16:54:40 +00:00
|
|
|
typedef llvm::DenseMap< const char*, RegisteredObjectInfo>
|
2012-04-16 22:26:48 +00:00
|
|
|
RegisteredObjectBufferMap;
|
|
|
|
|
|
|
|
/// Global access point for the JIT debugging interface designed for use with a
|
|
|
|
/// singleton toolbox. Handles thread-safe registration and deregistration of
|
|
|
|
/// object files that are in executable memory managed by the client of this
|
|
|
|
/// class.
|
2014-11-26 16:54:40 +00:00
|
|
|
class GDBJITRegistrationListener : public JITEventListener {
|
2012-04-16 22:26:48 +00:00
|
|
|
/// A map of in-memory object files that have been registered with the
|
|
|
|
/// JIT interface.
|
|
|
|
RegisteredObjectBufferMap ObjectBufferMap;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Instantiates the JIT service.
|
2014-11-26 16:54:40 +00:00
|
|
|
GDBJITRegistrationListener() : ObjectBufferMap() {}
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
/// Unregisters each object that was previously registered and releases all
|
|
|
|
/// internal resources.
|
2014-11-26 16:54:40 +00:00
|
|
|
virtual ~GDBJITRegistrationListener();
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
/// Creates an entry in the JIT registry for the buffer @p Object,
|
|
|
|
/// which must contain an object file in executable memory with any
|
|
|
|
/// debug information for the debugger.
|
2014-11-26 16:54:40 +00:00
|
|
|
void NotifyObjectEmitted(const ObjectFile &Object,
|
|
|
|
const RuntimeDyld::LoadedObjectInfo &L) override;
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
/// Removes the internal registration of @p Object, and
|
|
|
|
/// frees associated resources.
|
|
|
|
/// Returns true if @p Object was found in ObjectBufferMap.
|
2014-11-26 16:54:40 +00:00
|
|
|
void NotifyFreeingObject(const ObjectFile &Object) override;
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Deregister the debug info for the given object file from the debugger
|
|
|
|
/// and delete any temporary copies. This private method does not remove
|
|
|
|
/// the function from Map so that it can be called while iterating over Map.
|
|
|
|
void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Lock used to serialize all jit registration events, since they
|
|
|
|
/// modify global variables.
|
2014-09-19 21:38:20 +00:00
|
|
|
ManagedStatic<sys::Mutex> JITDebugLock;
|
2012-04-16 22:26:48 +00:00
|
|
|
|
2014-03-13 21:25:37 +00:00
|
|
|
/// Do the registration.
|
2012-04-16 22:26:48 +00:00
|
|
|
void NotifyDebugger(jit_code_entry* JITCodeEntry) {
|
|
|
|
__jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
|
|
|
|
|
|
|
|
// Insert this entry at the head of the list.
|
2014-04-24 06:44:33 +00:00
|
|
|
JITCodeEntry->prev_entry = nullptr;
|
2012-04-16 22:26:48 +00:00
|
|
|
jit_code_entry* NextEntry = __jit_debug_descriptor.first_entry;
|
|
|
|
JITCodeEntry->next_entry = NextEntry;
|
2014-04-24 06:44:33 +00:00
|
|
|
if (NextEntry) {
|
2012-04-16 22:26:48 +00:00
|
|
|
NextEntry->prev_entry = JITCodeEntry;
|
|
|
|
}
|
|
|
|
__jit_debug_descriptor.first_entry = JITCodeEntry;
|
|
|
|
__jit_debug_descriptor.relevant_entry = JITCodeEntry;
|
|
|
|
__jit_debug_register_code();
|
|
|
|
}
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
GDBJITRegistrationListener::~GDBJITRegistrationListener() {
|
2012-04-16 22:26:48 +00:00
|
|
|
// Free all registered object files.
|
2014-09-19 21:38:20 +00:00
|
|
|
llvm::MutexGuard locked(*JITDebugLock);
|
2014-11-26 16:54:40 +00:00
|
|
|
for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(),
|
|
|
|
E = ObjectBufferMap.end();
|
2012-04-16 22:26:48 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
// Call the private method that doesn't update the map so our iterator
|
|
|
|
// doesn't break.
|
|
|
|
deregisterObjectInternal(I);
|
|
|
|
}
|
|
|
|
ObjectBufferMap.clear();
|
|
|
|
}
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
void GDBJITRegistrationListener::NotifyObjectEmitted(
|
|
|
|
const ObjectFile &Object,
|
|
|
|
const RuntimeDyld::LoadedObjectInfo &L) {
|
|
|
|
|
|
|
|
OwningBinary<ObjectFile> DebugObj = L.getObjectForDebug(Object);
|
|
|
|
|
|
|
|
// Bail out if debug objects aren't supported.
|
|
|
|
if (!DebugObj.getBinary())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char *Buffer = DebugObj.getBinary()->getMemoryBufferRef().getBufferStart();
|
|
|
|
size_t Size = DebugObj.getBinary()->getMemoryBufferRef().getBufferSize();
|
2012-04-16 22:26:48 +00:00
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
const char *Key = Object.getMemoryBufferRef().getBufferStart();
|
2012-04-16 22:26:48 +00:00
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
assert(Key && "Attempt to register a null object with a debugger.");
|
2014-09-19 21:38:20 +00:00
|
|
|
llvm::MutexGuard locked(*JITDebugLock);
|
2014-11-26 16:54:40 +00:00
|
|
|
assert(ObjectBufferMap.find(Key) == ObjectBufferMap.end() &&
|
2012-04-16 22:26:48 +00:00
|
|
|
"Second attempt to perform debug registration.");
|
|
|
|
jit_code_entry* JITCodeEntry = new jit_code_entry();
|
|
|
|
|
2014-04-24 06:44:33 +00:00
|
|
|
if (!JITCodeEntry) {
|
2012-04-16 22:26:48 +00:00
|
|
|
llvm::report_fatal_error(
|
|
|
|
"Allocation failed when registering a JIT entry!\n");
|
2014-04-24 06:44:33 +00:00
|
|
|
} else {
|
2012-04-16 22:26:48 +00:00
|
|
|
JITCodeEntry->symfile_addr = Buffer;
|
|
|
|
JITCodeEntry->symfile_size = Size;
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
ObjectBufferMap[Key] = RegisteredObjectInfo(Size, JITCodeEntry,
|
|
|
|
std::move(DebugObj));
|
2012-04-16 22:26:48 +00:00
|
|
|
NotifyDebugger(JITCodeEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
void GDBJITRegistrationListener::NotifyFreeingObject(const ObjectFile& Object) {
|
|
|
|
const char *Key = Object.getMemoryBufferRef().getBufferStart();
|
2014-09-19 21:38:20 +00:00
|
|
|
llvm::MutexGuard locked(*JITDebugLock);
|
2014-11-26 16:54:40 +00:00
|
|
|
RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(Key);
|
2012-04-16 22:26:48 +00:00
|
|
|
|
|
|
|
if (I != ObjectBufferMap.end()) {
|
|
|
|
deregisterObjectInternal(I);
|
|
|
|
ObjectBufferMap.erase(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
void GDBJITRegistrationListener::deregisterObjectInternal(
|
2012-04-16 22:26:48 +00:00
|
|
|
RegisteredObjectBufferMap::iterator I) {
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
jit_code_entry*& JITCodeEntry = I->second.Entry;
|
2012-04-16 22:26:48 +00:00
|
|
|
|
2014-03-13 21:25:37 +00:00
|
|
|
// Do the unregistration.
|
2012-04-16 22:26:48 +00:00
|
|
|
{
|
|
|
|
__jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN;
|
|
|
|
|
|
|
|
// Remove the jit_code_entry from the linked list.
|
|
|
|
jit_code_entry* PrevEntry = JITCodeEntry->prev_entry;
|
|
|
|
jit_code_entry* NextEntry = JITCodeEntry->next_entry;
|
|
|
|
|
|
|
|
if (NextEntry) {
|
|
|
|
NextEntry->prev_entry = PrevEntry;
|
|
|
|
}
|
|
|
|
if (PrevEntry) {
|
|
|
|
PrevEntry->next_entry = NextEntry;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(__jit_debug_descriptor.first_entry == JITCodeEntry);
|
|
|
|
__jit_debug_descriptor.first_entry = NextEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the debugger which entry we removed, and unregister the code.
|
|
|
|
__jit_debug_descriptor.relevant_entry = JITCodeEntry;
|
|
|
|
__jit_debug_register_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete JITCodeEntry;
|
2014-04-24 06:44:33 +00:00
|
|
|
JITCodeEntry = nullptr;
|
2012-04-16 22:26:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
llvm::ManagedStatic<GDBJITRegistrationListener> GDBRegListener;
|
2014-03-13 21:25:37 +00:00
|
|
|
|
2012-04-16 22:26:48 +00:00
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2014-11-26 16:54:40 +00:00
|
|
|
JITEventListener* JITEventListener::createGDBRegistrationListener() {
|
|
|
|
return &*GDBRegListener;
|
2012-04-16 22:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|