1 //===-- IntelJITEventListener.cpp - Tell Intel profiler about JITed code --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a JITEventListener object to tell Intel(R) VTune(TM)
11 // Amplifier XE 2011 about JITted functions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Config/config.h"
16 #include "IntelJITEventsWrapper.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/DebugInfo/DWARF/DIContext.h"
20 #include "llvm/ExecutionEngine/JITEventListener.h"
21 #include "llvm/IR/DebugInfo.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Errno.h"
28 #include "llvm/Support/raw_ostream.h"
29
30 using namespace llvm;
31 using namespace llvm::object;
32
33 #define DEBUG_TYPE "amplifier-jit-event-listener"
34
35 namespace {
36
37 class IntelJITEventListener : public JITEventListener {
38 typedef DenseMap<void*, unsigned int> MethodIDMap;
39
40 std::unique_ptr<IntelJITEventsWrapper> Wrapper;
41 MethodIDMap MethodIDs;
42
43 typedef SmallVector<const void *, 64> MethodAddressVector;
44 typedef DenseMap<const void *, MethodAddressVector> ObjectMap;
45
46 ObjectMap LoadedObjectMap;
47 std::map<const char*, OwningBinary<ObjectFile>> DebugObjects;
48
49 public:
IntelJITEventListener(IntelJITEventsWrapper * libraryWrapper)50 IntelJITEventListener(IntelJITEventsWrapper* libraryWrapper) {
51 Wrapper.reset(libraryWrapper);
52 }
53
~IntelJITEventListener()54 ~IntelJITEventListener() {
55 }
56
57 void NotifyObjectEmitted(const ObjectFile &Obj,
58 const RuntimeDyld::LoadedObjectInfo &L) override;
59
60 void NotifyFreeingObject(const ObjectFile &Obj) override;
61 };
62
DILineInfoToIntelJITFormat(uintptr_t StartAddress,uintptr_t Address,DILineInfo Line)63 static LineNumberInfo DILineInfoToIntelJITFormat(uintptr_t StartAddress,
64 uintptr_t Address,
65 DILineInfo Line) {
66 LineNumberInfo Result;
67
68 Result.Offset = Address - StartAddress;
69 Result.LineNumber = Line.Line;
70
71 return Result;
72 }
73
FunctionDescToIntelJITFormat(IntelJITEventsWrapper & Wrapper,const char * FnName,uintptr_t FnStart,size_t FnSize)74 static iJIT_Method_Load FunctionDescToIntelJITFormat(
75 IntelJITEventsWrapper& Wrapper,
76 const char* FnName,
77 uintptr_t FnStart,
78 size_t FnSize) {
79 iJIT_Method_Load Result;
80 memset(&Result, 0, sizeof(iJIT_Method_Load));
81
82 Result.method_id = Wrapper.iJIT_GetNewMethodID();
83 Result.method_name = const_cast<char*>(FnName);
84 Result.method_load_address = reinterpret_cast<void*>(FnStart);
85 Result.method_size = FnSize;
86
87 Result.class_id = 0;
88 Result.class_file_name = NULL;
89 Result.user_data = NULL;
90 Result.user_data_size = 0;
91 Result.env = iJDE_JittingAPI;
92
93 return Result;
94 }
95
NotifyObjectEmitted(const ObjectFile & Obj,const RuntimeDyld::LoadedObjectInfo & L)96 void IntelJITEventListener::NotifyObjectEmitted(
97 const ObjectFile &Obj,
98 const RuntimeDyld::LoadedObjectInfo &L) {
99
100 OwningBinary<ObjectFile> DebugObjOwner = L.getObjectForDebug(Obj);
101 const ObjectFile &DebugObj = *DebugObjOwner.getBinary();
102
103 // Get the address of the object image for use as a unique identifier
104 const void* ObjData = DebugObj.getData().data();
105 DIContext* Context = DIContext::getDWARFContext(DebugObj);
106 MethodAddressVector Functions;
107
108 // Use symbol info to iterate functions in the object.
109 for (symbol_iterator I = DebugObj.symbol_begin(),
110 E = DebugObj.symbol_end();
111 I != E;
112 ++I) {
113 std::vector<LineNumberInfo> LineInfo;
114 std::string SourceFileName;
115
116 SymbolRef::Type SymType;
117 if (I->getType(SymType)) continue;
118 if (SymType == SymbolRef::ST_Function) {
119 StringRef Name;
120 uint64_t Addr;
121 uint64_t Size;
122 if (I->getName(Name)) continue;
123 if (I->getAddress(Addr)) continue;
124 if (I->getSize(Size)) continue;
125
126 // Record this address in a local vector
127 Functions.push_back((void*)Addr);
128
129 // Build the function loaded notification message
130 iJIT_Method_Load FunctionMessage = FunctionDescToIntelJITFormat(*Wrapper,
131 Name.data(),
132 Addr,
133 Size);
134 if (Context) {
135 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
136 DILineInfoTable::iterator Begin = Lines.begin();
137 DILineInfoTable::iterator End = Lines.end();
138 for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
139 LineInfo.push_back(DILineInfoToIntelJITFormat((uintptr_t)Addr,
140 It->first,
141 It->second));
142 }
143 if (LineInfo.size() == 0) {
144 FunctionMessage.source_file_name = 0;
145 FunctionMessage.line_number_size = 0;
146 FunctionMessage.line_number_table = 0;
147 } else {
148 // Source line information for the address range is provided as
149 // a code offset for the start of the corresponding sub-range and
150 // a source line. JIT API treats offsets in LineNumberInfo structures
151 // as the end of the corresponding code region. The start of the code
152 // is taken from the previous element. Need to shift the elements.
153
154 LineNumberInfo last = LineInfo.back();
155 last.Offset = FunctionMessage.method_size;
156 LineInfo.push_back(last);
157 for (size_t i = LineInfo.size() - 2; i > 0; --i)
158 LineInfo[i].LineNumber = LineInfo[i - 1].LineNumber;
159
160 SourceFileName = Lines.front().second.FileName;
161 FunctionMessage.source_file_name = const_cast<char *>(SourceFileName.c_str());
162 FunctionMessage.line_number_size = LineInfo.size();
163 FunctionMessage.line_number_table = &*LineInfo.begin();
164 }
165 } else {
166 FunctionMessage.source_file_name = 0;
167 FunctionMessage.line_number_size = 0;
168 FunctionMessage.line_number_table = 0;
169 }
170
171 Wrapper->iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED,
172 &FunctionMessage);
173 MethodIDs[(void*)Addr] = FunctionMessage.method_id;
174 }
175 }
176
177 // To support object unload notification, we need to keep a list of
178 // registered function addresses for each loaded object. We will
179 // use the MethodIDs map to get the registered ID for each function.
180 LoadedObjectMap[ObjData] = Functions;
181 DebugObjects[Obj.getData().data()] = std::move(DebugObjOwner);
182 }
183
NotifyFreeingObject(const ObjectFile & Obj)184 void IntelJITEventListener::NotifyFreeingObject(const ObjectFile &Obj) {
185 // This object may not have been registered with the listener. If it wasn't,
186 // bail out.
187 if (DebugObjects.find(Obj.getData().data()) == DebugObjects.end())
188 return;
189
190 // Get the address of the object image for use as a unique identifier
191 const ObjectFile &DebugObj = *DebugObjects[Obj.getData().data()].getBinary();
192 const void* ObjData = DebugObj.getData().data();
193
194 // Get the object's function list from LoadedObjectMap
195 ObjectMap::iterator OI = LoadedObjectMap.find(ObjData);
196 if (OI == LoadedObjectMap.end())
197 return;
198 MethodAddressVector& Functions = OI->second;
199
200 // Walk the function list, unregistering each function
201 for (MethodAddressVector::iterator FI = Functions.begin(),
202 FE = Functions.end();
203 FI != FE;
204 ++FI) {
205 void* FnStart = const_cast<void*>(*FI);
206 MethodIDMap::iterator MI = MethodIDs.find(FnStart);
207 if (MI != MethodIDs.end()) {
208 Wrapper->iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_UNLOAD_START,
209 &MI->second);
210 MethodIDs.erase(MI);
211 }
212 }
213
214 // Erase the object from LoadedObjectMap
215 LoadedObjectMap.erase(OI);
216 DebugObjects.erase(Obj.getData().data());
217 }
218
219 } // anonymous namespace.
220
221 namespace llvm {
createIntelJITEventListener()222 JITEventListener *JITEventListener::createIntelJITEventListener() {
223 return new IntelJITEventListener(new IntelJITEventsWrapper);
224 }
225
226 // for testing
createIntelJITEventListener(IntelJITEventsWrapper * TestImpl)227 JITEventListener *JITEventListener::createIntelJITEventListener(
228 IntelJITEventsWrapper* TestImpl) {
229 return new IntelJITEventListener(TestImpl);
230 }
231
232 } // namespace llvm
233
234