Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Added oprofile support for MCJIT #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions lib/ExecutionEngine/OProfileJIT/OProfileJITEventListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "llvm/ADT/OwningPtr.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/ExecutionEngine/OProfileWrapper.h"
#include "llvm/DebugInfo/DIContext.h"
#include "llvm/ExecutionEngine/ObjectImage.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Errno.h"
Expand Down Expand Up @@ -52,6 +55,10 @@ class OProfileJITEventListener : public JITEventListener {
const JITEvent_EmittedFunctionDetails &Details);

virtual void NotifyFreeingMachineCode(void *OldPtr);

virtual void NotifyObjectEmitted(const ObjectImage &Obj);

virtual void NotifyFreeingObject(const ObjectImage &Obj);
};

void OProfileJITEventListener::initialize() {
Expand Down Expand Up @@ -159,6 +166,65 @@ void OProfileJITEventListener::NotifyFreeingMachineCode(void *FnStart) {
}
}

void OProfileJITEventListener::NotifyObjectEmitted(const ObjectImage &Obj) {
if (!Wrapper.isAgentAvailable()) {
return;
}

// Use symbol info to iterate functions in the object.
error_code ec;
for (object::symbol_iterator I = Obj.begin_symbols(),
E = Obj.end_symbols();
I != E && !ec;
I.increment(ec)) {
object::SymbolRef::Type SymType;
if (I->getType(SymType)) continue;
if (SymType == object::SymbolRef::ST_Function) {
StringRef Name;
uint64_t Addr;
uint64_t Size;
if (I->getName(Name)) continue;
if (I->getAddress(Addr)) continue;
if (I->getSize(Size)) continue;

if (Wrapper.op_write_native_code(Name.data(), Addr, (void*)Addr, Size) == -1) {
DEBUG(dbgs() << "Failed to tell OProfile about native function "
<< Name << " at ["
<< (void*)Addr << "-" << ((char*)Addr + Size) << "]\n");
continue;
}
// TODO: support for line number info (similar to IntelJITEventListener.cpp)
}
}
}

void OProfileJITEventListener::NotifyFreeingObject(const ObjectImage &Obj) {
if (!Wrapper.isAgentAvailable()) {
return;
}

// Use symbol info to iterate functions in the object.
error_code ec;
for (object::symbol_iterator I = Obj.begin_symbols(),
E = Obj.end_symbols();
I != E && !ec;
I.increment(ec)) {
object::SymbolRef::Type SymType;
if (I->getType(SymType)) continue;
if (SymType == object::SymbolRef::ST_Function) {
uint64_t Addr;
if (I->getAddress(Addr)) continue;

if (Wrapper.op_unload_native_code(Addr) == -1) {
DEBUG(dbgs()
<< "Failed to tell OProfile about unload of native function at "
<< (void*)Addr << "\n");
continue;
}
}
}
}

} // anonymous namespace.

namespace llvm {
Expand Down
6 changes: 5 additions & 1 deletion lib/ExecutionEngine/OProfileJIT/OProfileWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ bool OProfileWrapper::checkForOProfileProcEntry() {
close(CmdLineFD);
ssize_t Idx = 0;

if (ExeName[0] != '/') {
BaseName = ExeName;
}

// Find the terminator for the first string
while (Idx < NumRead-1 && ExeName[Idx] != 0) {
Idx++;
Expand All @@ -159,7 +163,7 @@ bool OProfileWrapper::checkForOProfileProcEntry() {
}

// Test this to see if it is the oprofile daemon
if (BaseName != 0 && !strcmp("oprofiled", BaseName)) {
if (BaseName != 0 && (!strcmp("oprofiled", BaseName) || !strcmp("operf", BaseName))) {
// If it is, we're done
closedir(ProcDir);
return true;
Expand Down