2012-11-21 20:38:26 +00:00
|
|
|
//===-- llvm-jitlistener.cpp - Utility for testing MCJIT event listener ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This program is a used by lit tests to verify the MCJIT JITEventListener
|
|
|
|
// interface. It registers a mock JIT event listener, generates a module from
|
|
|
|
// an input IR file and dumps the reported event information to stdout.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "../../lib/ExecutionEngine/IntelJITEvents/IntelJITEventsWrapper.h"
|
2012-11-21 20:38:26 +00:00
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/ExecutionEngine/JITEventListener.h"
|
|
|
|
#include "llvm/ExecutionEngine/JITMemoryManager.h"
|
|
|
|
#include "llvm/ExecutionEngine/MCJIT.h"
|
|
|
|
#include "llvm/ExecutionEngine/ObjectImage.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 02:25:37 +00:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2012-11-21 20:38:26 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
typedef std::vector<std::pair<std::string, unsigned int> > SourceLocations;
|
|
|
|
typedef std::map<uint64_t, SourceLocations> NativeCodeMap;
|
|
|
|
|
|
|
|
NativeCodeMap ReportedDebugFuncs;
|
|
|
|
|
|
|
|
int NotifyEvent(iJIT_JVM_EVENT EventType, void *EventSpecificData) {
|
|
|
|
switch (EventType) {
|
|
|
|
case iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED: {
|
|
|
|
if (!EventSpecificData) {
|
|
|
|
errs() <<
|
|
|
|
"Error: The JIT event listener did not provide a event data.";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
iJIT_Method_Load* msg = static_cast<iJIT_Method_Load*>(EventSpecificData);
|
|
|
|
|
|
|
|
ReportedDebugFuncs[msg->method_id];
|
|
|
|
|
|
|
|
outs() << "Method load [" << msg->method_id << "]: " << msg->method_name
|
|
|
|
<< ", Size = " << msg->method_size << "\n";
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < msg->line_number_size; ++i) {
|
|
|
|
if (!msg->line_number_table) {
|
|
|
|
errs() << "A function with a non-zero line count had no line table.";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
std::pair<std::string, unsigned int> loc(
|
|
|
|
std::string(msg->source_file_name),
|
|
|
|
msg->line_number_table[i].LineNumber);
|
|
|
|
ReportedDebugFuncs[msg->method_id].push_back(loc);
|
|
|
|
outs() << " Line info @ " << msg->line_number_table[i].Offset
|
|
|
|
<< ": " << msg->source_file_name
|
|
|
|
<< ", line " << msg->line_number_table[i].LineNumber << "\n";
|
|
|
|
}
|
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case iJVM_EVENT_TYPE_METHOD_UNLOAD_START: {
|
|
|
|
if (!EventSpecificData) {
|
|
|
|
errs() <<
|
|
|
|
"Error: The JIT event listener did not provide a event data.";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
unsigned int UnloadId
|
|
|
|
= *reinterpret_cast<unsigned int*>(EventSpecificData);
|
|
|
|
assert(1 == ReportedDebugFuncs.erase(UnloadId));
|
|
|
|
outs() << "Method unload [" << UnloadId << "]\n";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
iJIT_IsProfilingActiveFlags IsProfilingActive(void) {
|
|
|
|
// for testing, pretend we have an Intel Parallel Amplifier XE 2011
|
|
|
|
// instance attached
|
|
|
|
return iJIT_SAMPLING_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int GetNewMethodID(void) {
|
|
|
|
static unsigned int id = 0;
|
|
|
|
return ++id;
|
|
|
|
}
|
|
|
|
|
|
|
|
class JitEventListenerTest {
|
|
|
|
protected:
|
|
|
|
void InitEE(const std::string &IRFile) {
|
|
|
|
LLVMContext &Context = getGlobalContext();
|
|
|
|
|
|
|
|
// If we have a native target, initialize it to ensure it is linked in and
|
|
|
|
// usable by the JIT.
|
|
|
|
InitializeNativeTarget();
|
|
|
|
InitializeNativeTargetAsmPrinter();
|
|
|
|
|
|
|
|
// Parse the bitcode...
|
|
|
|
SMDiagnostic Err;
|
|
|
|
TheModule = ParseIRFile(IRFile, Err, Context);
|
|
|
|
if (!TheModule) {
|
|
|
|
errs() << Err.getMessage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This is using the default legacy JITMemoryManager because it
|
|
|
|
// supports poison memory. At some point, we'll need to update this to
|
|
|
|
// use an MCJIT-specific memory manager. It might be nice to have the
|
|
|
|
// poison memory option there too.
|
|
|
|
JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
|
|
|
|
if (!MemMgr) {
|
|
|
|
errs() << "Unable to create memory manager.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the memory manager to poison freed memory so that accessing freed
|
|
|
|
// memory is more easily tested.
|
|
|
|
MemMgr->setPoisonMemory(true);
|
|
|
|
|
|
|
|
// Override the triple to generate ELF on Windows since that's supported
|
|
|
|
Triple Tuple(TheModule->getTargetTriple());
|
|
|
|
if (Tuple.getTriple().empty())
|
Introduce llvm::sys::getProcessTriple() function.
In r143502, we renamed getHostTriple() to getDefaultTargetTriple()
as part of work to allow the user to supply a different default
target triple at configure time. This change also affected the JIT.
However, it is inappropriate to use the default target triple in the
JIT in most circumstances because this will not necessarily match
the current architecture used by the process, leading to illegal
instruction and other such errors at run time.
Introduce the getProcessTriple() function for use in the JIT and
its clients, and cause the JIT to use it. On architectures with a
single bitness, the host and process triples are identical. On other
architectures, the host triple represents the architecture of the
host CPU, while the process triple represents the architecture used
by the host CPU to interpret machine code within the current process.
For example, when executing 32-bit code on a 64-bit Linux machine,
the host triple may be 'x86_64-unknown-linux-gnu', while the process
triple may be 'i386-unknown-linux-gnu'.
This fixes JIT for the 32-on-64-bit (and vice versa) build on non-Apple
platforms.
Differential Revision: http://llvm-reviews.chandlerc.com/D254
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172627 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-16 17:27:22 +00:00
|
|
|
Tuple.setTriple(sys::getProcessTriple());
|
2012-11-21 20:38:26 +00:00
|
|
|
|
2014-03-06 20:47:11 +00:00
|
|
|
if (Tuple.isOSWindows() && !Tuple.isOSBinFormatELF()) {
|
|
|
|
Tuple.setObjectFormat(Triple::ELF);
|
2012-11-21 20:38:26 +00:00
|
|
|
TheModule->setTargetTriple(Tuple.getTriple());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the IR
|
|
|
|
std::string Error;
|
|
|
|
TheJIT.reset(EngineBuilder(TheModule)
|
|
|
|
.setEngineKind(EngineKind::JIT)
|
|
|
|
.setErrorStr(&Error)
|
|
|
|
.setJITMemoryManager(MemMgr)
|
|
|
|
.setUseMCJIT(true)
|
|
|
|
.create());
|
|
|
|
if (Error.empty() == false)
|
|
|
|
errs() << Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyEE() {
|
|
|
|
TheJIT.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMContext Context; // Global ownership
|
|
|
|
Module *TheModule; // Owned by ExecutionEngine.
|
|
|
|
JITMemoryManager *JMM; // Owned by ExecutionEngine.
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<ExecutionEngine> TheJIT;
|
2012-11-21 20:38:26 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void ProcessInput(const std::string &Filename) {
|
|
|
|
InitEE(Filename);
|
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<llvm::JITEventListener> Listener(
|
|
|
|
JITEventListener::createIntelJITEventListener(new IntelJITEventsWrapper(
|
|
|
|
NotifyEvent, 0, IsProfilingActive, 0, 0, GetNewMethodID)));
|
2012-11-21 20:38:26 +00:00
|
|
|
|
|
|
|
TheJIT->RegisterJITEventListener(Listener.get());
|
|
|
|
|
|
|
|
TheJIT->finalizeObject();
|
|
|
|
|
|
|
|
// Destroy the JIT engine instead of unregistering to get unload events.
|
|
|
|
DestroyEE();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input IR file>"),
|
|
|
|
cl::Required);
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm jit event listener test utility\n");
|
|
|
|
|
|
|
|
JitEventListenerTest Test;
|
|
|
|
|
|
|
|
Test.ProcessInput(InputFilename);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|