15 #include "llvm/Config/config.h"
18 #define DEBUG_TYPE "oprofile-jit-event-listener"
35 using namespace llvm::jitprofiling;
50 ~OProfileJITEventListener();
52 virtual void NotifyFunctionEmitted(
const Function &
F,
53 void *FnStart,
size_t FnSize,
56 virtual void NotifyFreeingMachineCode(
void *OldPtr);
58 virtual void NotifyObjectEmitted(
const ObjectImage &Obj);
60 virtual void NotifyFreeingObject(
const ObjectImage &Obj);
66 DEBUG(
dbgs() <<
"Failed to connect to OProfile agent: " << err_str <<
"\n");
68 DEBUG(
dbgs() <<
"Connected to OProfile agent.\n");
72 OProfileJITEventListener::~OProfileJITEventListener() {
73 if (
Wrapper.isAgentAvailable()) {
74 if (
Wrapper.op_close_agent() == -1) {
76 DEBUG(
dbgs() <<
"Failed to disconnect from OProfile agent: "
79 DEBUG(
dbgs() <<
"Disconnected from OProfile agent.\n");
84 static debug_line_info LineStartToOProfileFormat(
87 debug_line_info Result;
92 DEBUG(
dbgs() <<
"Mapping " << reinterpret_cast<void*>(Result.vma) <<
" to "
93 << Result.filename <<
":" << Result.lineno <<
"\n");
98 void OProfileJITEventListener::NotifyFunctionEmitted(
99 const Function &
F,
void *FnStart,
size_t FnSize,
101 assert(F.
hasName() && FnStart != 0 &&
"Bad symbol to add");
103 reinterpret_cast<uint64_t
>(FnStart),
104 FnStart, FnSize) == -1) {
105 DEBUG(
dbgs() <<
"Failed to tell OProfile about native function "
107 << FnStart <<
"-" << ((
char*)FnStart + FnSize) <<
"]\n");
117 std::vector<debug_line_info> LineInfo;
118 LineInfo.reserve(1 + Details.
LineStarts.size());
122 &&
"LineStarts should not contain unknown DebugLocs");
125 if (FunctionDI.
Verify()) {
130 debug_line_info line_info;
131 line_info.vma =
reinterpret_cast<uintptr_t
>(FnStart);
133 line_info.filename = Filenames.
getFilename(FirstLocScope);
134 LineInfo.push_back(line_info);
137 for (std::vector<EmittedFunctionDetails::LineStart>::const_iterator
140 LineInfo.push_back(LineStartToOProfileFormat(
141 *Details.
MF, Filenames,
I->Address,
I->Loc));
146 LineInfo[0].vma =
reinterpret_cast<uintptr_t
>(FnStart);
148 if (
Wrapper.op_write_debug_line_info(FnStart, LineInfo.size(),
149 &*LineInfo.begin()) == -1) {
151 <<
"Failed to tell OProfile about line numbers for native function "
153 << FnStart <<
"-" << ((
char*)FnStart + FnSize) <<
"]\n");
159 void OProfileJITEventListener::NotifyFreeingMachineCode(
void *FnStart) {
160 assert(FnStart &&
"Invalid function pointer");
161 if (
Wrapper.op_unload_native_code(reinterpret_cast<uint64_t>(FnStart)) == -1) {
163 <<
"Failed to tell OProfile about unload of native function at "
168 void OProfileJITEventListener::NotifyObjectEmitted(
const ObjectImage &Obj) {
169 if (!
Wrapper.isAgentAvailable()) {
180 if (
I->getType(SymType))
continue;
185 if (
I->getName(Name))
continue;
186 if (
I->getAddress(Addr))
continue;
187 if (
I->getSize(Size))
continue;
189 if (
Wrapper.op_write_native_code(Name.
data(), Addr, (
void*)Addr, Size)
191 DEBUG(
dbgs() <<
"Failed to tell OProfile about native function "
193 << (
void*)Addr <<
"-" << ((
char*)Addr + Size) <<
"]\n");
201 void OProfileJITEventListener::NotifyFreeingObject(
const ObjectImage &Obj) {
202 if (!
Wrapper.isAgentAvailable()) {
213 if (
I->getType(SymType))
continue;
216 if (
I->getAddress(Addr))
continue;
218 if (
Wrapper.op_unload_native_code(Addr) == -1) {
220 <<
"Failed to tell OProfile about unload of native function at "
221 << (
void*)Addr <<
"\n");
233 return new OProfileJITEventListener(*JITProfilingWrapper);
239 return new OProfileJITEventListener(*TestImpl);
LLVMContext & getContext() const
MDNode - a tuple of other values.
const Function * getFunction() const
const char * getFilename(MDNode *Scope)
StringRef getName() const
bool isUnknown() const
isUnknown - Return true if this is an unknown location.
DISubprogram - This is a wrapper for a subprogram (e.g. a function).
const char * data() const
virtual object::symbol_iterator end_symbols() const =0
virtual object::symbol_iterator begin_symbols() const =0
unsigned getLineNumber() const
DISubprogram getDISubprogram(const MDNode *Scope)
getDISubprogram - Find subprogram that is enclosing this scope.
static void initialize(TargetLibraryInfo &TLI, const Triple &T, const char **StandardNames)
MDNode * getScope(const LLVMContext &Ctx) const
std::vector< LineStart > LineStarts
The list of line boundary information, sorted by address.
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
const MachineFunction * MF
The machine function the struct contains information for.
bool Verify() const
Verify - Verify that a subprogram descriptor is well formed.
static JITEventListener * createOProfileJITEventListener()