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