92a67b9b2a6d4eea2f969413225f4693555df33f
[oota-llvm.git] / lib / ExecutionEngine / OProfileJIT / OProfileJITEventListener.cpp
1 //===-- OProfileJITEventListener.cpp - Tell OProfile about JITted 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 that uses OProfileWrapper to tell
11 // oprofile about JITted functions, including source line information.
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/CodeGen/MachineFunction.h"
21 #include "llvm/ExecutionEngine/OProfileWrapper.h"
22 #include "llvm/ExecutionEngine/RuntimeDyld.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Errno.h"
27 #include "EventListenerCommon.h"
28
29 #include <dirent.h>
30 #include <fcntl.h>
31
32 using namespace llvm;
33 using namespace llvm::jitprofiling;
34 using namespace llvm::object;
35
36 #define DEBUG_TYPE "oprofile-jit-event-listener"
37
38 namespace {
39
40 class OProfileJITEventListener : public JITEventListener {
41   std::unique_ptr<OProfileWrapper> Wrapper;
42
43   void initialize();
44   std::map<const char*, OwningBinary<ObjectFile>> DebugObjects;
45
46 public:
47   OProfileJITEventListener(std::unique_ptr<OProfileWrapper> LibraryWrapper)
48     : Wrapper(std::move(LibraryWrapper)) {
49     initialize();
50   }
51
52   ~OProfileJITEventListener();
53
54   void NotifyObjectEmitted(const ObjectFile &Obj,
55                            const RuntimeDyld::LoadedObjectInfo &L) override;
56
57   void NotifyFreeingObject(const ObjectFile &Obj) override;
58 };
59
60 void OProfileJITEventListener::initialize() {
61   if (!Wrapper->op_open_agent()) {
62     const std::string err_str = sys::StrError();
63     DEBUG(dbgs() << "Failed to connect to OProfile agent: " << err_str << "\n");
64   } else {
65     DEBUG(dbgs() << "Connected to OProfile agent.\n");
66   }
67 }
68
69 OProfileJITEventListener::~OProfileJITEventListener() {
70   if (Wrapper->isAgentAvailable()) {
71     if (Wrapper->op_close_agent() == -1) {
72       const std::string err_str = sys::StrError();
73       DEBUG(dbgs() << "Failed to disconnect from OProfile agent: "
74                    << err_str << "\n");
75     } else {
76       DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
77     }
78   }
79 }
80
81 void OProfileJITEventListener::NotifyObjectEmitted(
82                                        const ObjectFile &Obj,
83                                        const RuntimeDyld::LoadedObjectInfo &L) {
84   if (!Wrapper->isAgentAvailable()) {
85     return;
86   }
87
88   OwningBinary<ObjectFile> DebugObjOwner = L.getObjectForDebug(Obj);
89   const ObjectFile &DebugObj = *DebugObjOwner.getBinary();
90
91   // Use symbol info to iterate functions in the object.
92   for (symbol_iterator I = DebugObj.symbol_begin(), E = DebugObj.symbol_end();
93        I != E; ++I) {
94     SymbolRef::Type SymType;
95     if (I->getType(SymType)) continue;
96     if (SymType == SymbolRef::ST_Function) {
97       StringRef  Name;
98       uint64_t   Addr;
99       uint64_t   Size;
100       if (I->getName(Name)) continue;
101       if (I->getAddress(Addr)) continue;
102       if (I->getSize(Size)) continue;
103
104       if (Wrapper->op_write_native_code(Name.data(), Addr, (void*)Addr, Size)
105                         == -1) {
106         DEBUG(dbgs() << "Failed to tell OProfile about native function "
107           << Name << " at ["
108           << (void*)Addr << "-" << ((char*)Addr + Size) << "]\n");
109         continue;
110       }
111       // TODO: support line number info (similar to IntelJITEventListener.cpp)
112     }
113   }
114
115   DebugObjects[Obj.getData().data()] = std::move(DebugObjOwner);
116 }
117
118 void OProfileJITEventListener::NotifyFreeingObject(const ObjectFile &Obj) {
119   if (Wrapper->isAgentAvailable()) {
120
121     // If there was no agent registered when the original object was loaded then
122     // we won't have created a debug object for it, so bail out.
123     if (DebugObjects.find(Obj.getData().data()) == DebugObjects.end())
124       return;
125
126     const ObjectFile &DebugObj = *DebugObjects[Obj.getData().data()].getBinary();
127
128     // Use symbol info to iterate functions in the object.
129     for (symbol_iterator I = DebugObj.symbol_begin(),
130                          E = DebugObj.symbol_end();
131          I != E; ++I) {
132       SymbolRef::Type SymType;
133       if (I->getType(SymType)) continue;
134       if (SymType == SymbolRef::ST_Function) {
135         uint64_t   Addr;
136         if (I->getAddress(Addr)) continue;
137
138         if (Wrapper->op_unload_native_code(Addr) == -1) {
139           DEBUG(dbgs()
140                 << "Failed to tell OProfile about unload of native function at "
141                 << (void*)Addr << "\n");
142           continue;
143         }
144       }
145     }
146   }
147
148   DebugObjects.erase(Obj.getData().data());
149 }
150
151 }  // anonymous namespace.
152
153 namespace llvm {
154 JITEventListener *JITEventListener::createOProfileJITEventListener() {
155   return new OProfileJITEventListener(llvm::make_unique<OProfileWrapper>());
156 }
157
158 } // namespace llvm
159