1 //===-- OProfileJITEventListener.cpp - Tell OProfile about JITted code ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a JITEventListener object that uses OProfileWrapper to tell
11 // oprofile about JITted functions, including source line information.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h"
16 #include "llvm/ExecutionEngine/JITEventListener.h"
18 #define DEBUG_TYPE "oprofile-jit-event-listener"
19 #include "llvm/DebugInfo.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/ExecutionEngine/ObjectImage.h"
24 #include "llvm/ExecutionEngine/OProfileWrapper.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/Errno.h"
29 #include "EventListenerCommon.h"
35 using namespace llvm::jitprofiling;
39 class OProfileJITEventListener : public JITEventListener {
40 OProfileWrapper& Wrapper;
45 OProfileJITEventListener(OProfileWrapper& LibraryWrapper)
46 : Wrapper(LibraryWrapper) {
50 ~OProfileJITEventListener();
52 virtual void NotifyFunctionEmitted(const Function &F,
53 void *FnStart, size_t FnSize,
54 const JITEvent_EmittedFunctionDetails &Details);
56 virtual void NotifyFreeingMachineCode(void *OldPtr);
58 virtual void NotifyObjectEmitted(const ObjectImage &Obj);
60 virtual void NotifyFreeingObject(const ObjectImage &Obj);
63 void OProfileJITEventListener::initialize() {
64 if (!Wrapper.op_open_agent()) {
65 const std::string err_str = sys::StrError();
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) {
75 const std::string err_str = sys::StrError();
76 DEBUG(dbgs() << "Failed to disconnect from OProfile agent: "
79 DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
84 static debug_line_info LineStartToOProfileFormat(
85 const MachineFunction &MF, FilenameCache &Filenames,
86 uintptr_t Address, DebugLoc Loc) {
87 debug_line_info Result;
89 Result.lineno = Loc.getLine();
90 Result.filename = Filenames.getFilename(
91 Loc.getScope(MF.getFunction()->getContext()));
92 DEBUG(dbgs() << "Mapping " << reinterpret_cast<void*>(Result.vma) << " to "
93 << Result.filename << ":" << Result.lineno << "\n");
97 // Adds the just-emitted function to the symbol table.
98 void OProfileJITEventListener::NotifyFunctionEmitted(
99 const Function &F, void *FnStart, size_t FnSize,
100 const JITEvent_EmittedFunctionDetails &Details) {
101 assert(F.hasName() && FnStart != 0 && "Bad symbol to add");
102 if (Wrapper.op_write_native_code(F.getName().data(),
103 reinterpret_cast<uint64_t>(FnStart),
104 FnStart, FnSize) == -1) {
105 DEBUG(dbgs() << "Failed to tell OProfile about native function "
106 << F.getName() << " at ["
107 << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n");
111 if (!Details.LineStarts.empty()) {
112 // Now we convert the line number information from the address/DebugLoc
113 // format in Details to the address/filename/lineno format that OProfile
114 // expects. Note that OProfile 0.9.4 has a bug that causes it to ignore
115 // line numbers for addresses above 4G.
116 FilenameCache Filenames;
117 std::vector<debug_line_info> LineInfo;
118 LineInfo.reserve(1 + Details.LineStarts.size());
120 DebugLoc FirstLoc = Details.LineStarts[0].Loc;
121 assert(!FirstLoc.isUnknown()
122 && "LineStarts should not contain unknown DebugLocs");
123 MDNode *FirstLocScope = FirstLoc.getScope(F.getContext());
124 DISubprogram FunctionDI = getDISubprogram(FirstLocScope);
125 if (FunctionDI.Verify()) {
126 // If we have debug info for the function itself, use that as the line
127 // number of the first several instructions. Otherwise, after filling
128 // LineInfo, we'll adjust the address of the first line number to point at
129 // the start of the function.
130 debug_line_info line_info;
131 line_info.vma = reinterpret_cast<uintptr_t>(FnStart);
132 line_info.lineno = FunctionDI.getLineNumber();
133 line_info.filename = Filenames.getFilename(FirstLocScope);
134 LineInfo.push_back(line_info);
137 for (std::vector<EmittedFunctionDetails::LineStart>::const_iterator
138 I = Details.LineStarts.begin(), E = Details.LineStarts.end();
140 LineInfo.push_back(LineStartToOProfileFormat(
141 *Details.MF, Filenames, I->Address, I->Loc));
144 // In case the function didn't have line info of its own, adjust the first
145 // line info's address to include the start of the function.
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 "
152 << F.getName() << " at ["
153 << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n");
158 // Removes the being-deleted function from the symbol table.
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()) {
173 // Use symbol info to iterate functions in the object.
175 for (object::symbol_iterator I = Obj.begin_symbols(),
176 E = Obj.end_symbols();
179 object::SymbolRef::Type SymType;
180 if (I->getType(SymType)) continue;
181 if (SymType == object::SymbolRef::ST_Function) {
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");
196 // TODO: support line number info (similar to IntelJITEventListener.cpp)
201 void OProfileJITEventListener::NotifyFreeingObject(const ObjectImage &Obj) {
202 if (!Wrapper.isAgentAvailable()) {
206 // Use symbol info to iterate functions in the object.
208 for (object::symbol_iterator I = Obj.begin_symbols(),
209 E = Obj.end_symbols();
212 object::SymbolRef::Type SymType;
213 if (I->getType(SymType)) continue;
214 if (SymType == object::SymbolRef::ST_Function) {
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");
228 } // anonymous namespace.
231 JITEventListener *JITEventListener::createOProfileJITEventListener() {
232 static OwningPtr<OProfileWrapper> JITProfilingWrapper(new OProfileWrapper);
233 return new OProfileJITEventListener(*JITProfilingWrapper);
237 JITEventListener *JITEventListener::createOProfileJITEventListener(
238 OProfileWrapper* TestImpl) {
239 return new OProfileJITEventListener(*TestImpl);