OSDN Git Service

am 6eeae0cf: Revert "Apply rL216114 from upstream LLVM."
[android-x86/external-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/ObjectImage.h"
22 #include "llvm/ExecutionEngine/OProfileWrapper.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
35 #define DEBUG_TYPE "oprofile-jit-event-listener"
36
37 namespace {
38
39 class OProfileJITEventListener : public JITEventListener {
40   OProfileWrapper& Wrapper;
41
42   void initialize();
43
44 public:
45   OProfileJITEventListener(OProfileWrapper& LibraryWrapper)
46   : Wrapper(LibraryWrapper) {
47     initialize();
48   }
49
50   ~OProfileJITEventListener();
51
52   virtual void NotifyObjectEmitted(const ObjectImage &Obj);
53
54   virtual void NotifyFreeingObject(const ObjectImage &Obj);
55 };
56
57 void OProfileJITEventListener::initialize() {
58   if (!Wrapper.op_open_agent()) {
59     const std::string err_str = sys::StrError();
60     DEBUG(dbgs() << "Failed to connect to OProfile agent: " << err_str << "\n");
61   } else {
62     DEBUG(dbgs() << "Connected to OProfile agent.\n");
63   }
64 }
65
66 OProfileJITEventListener::~OProfileJITEventListener() {
67   if (Wrapper.isAgentAvailable()) {
68     if (Wrapper.op_close_agent() == -1) {
69       const std::string err_str = sys::StrError();
70       DEBUG(dbgs() << "Failed to disconnect from OProfile agent: "
71                    << err_str << "\n");
72     } else {
73       DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
74     }
75   }
76 }
77
78 void OProfileJITEventListener::NotifyObjectEmitted(const ObjectImage &Obj) {
79   if (!Wrapper.isAgentAvailable()) {
80     return;
81   }
82
83   // Use symbol info to iterate functions in the object.
84   for (object::symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols();
85        I != E; ++I) {
86     object::SymbolRef::Type SymType;
87     if (I->getType(SymType)) continue;
88     if (SymType == object::SymbolRef::ST_Function) {
89       StringRef  Name;
90       uint64_t   Addr;
91       uint64_t   Size;
92       if (I->getName(Name)) continue;
93       if (I->getAddress(Addr)) continue;
94       if (I->getSize(Size)) continue;
95
96       if (Wrapper.op_write_native_code(Name.data(), Addr, (void*)Addr, Size)
97                         == -1) {
98         DEBUG(dbgs() << "Failed to tell OProfile about native function "
99           << Name << " at ["
100           << (void*)Addr << "-" << ((char*)Addr + Size) << "]\n");
101         continue;
102       }
103       // TODO: support line number info (similar to IntelJITEventListener.cpp)
104     }
105   }
106 }
107
108 void OProfileJITEventListener::NotifyFreeingObject(const ObjectImage &Obj) {
109   if (!Wrapper.isAgentAvailable()) {
110     return;
111   }
112
113   // Use symbol info to iterate functions in the object.
114   for (object::symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols();
115        I != E; ++I) {
116     object::SymbolRef::Type SymType;
117     if (I->getType(SymType)) continue;
118     if (SymType == object::SymbolRef::ST_Function) {
119       uint64_t   Addr;
120       if (I->getAddress(Addr)) continue;
121
122       if (Wrapper.op_unload_native_code(Addr) == -1) {
123         DEBUG(dbgs()
124           << "Failed to tell OProfile about unload of native function at "
125           << (void*)Addr << "\n");
126         continue;
127       }
128     }
129   }
130 }
131
132 }  // anonymous namespace.
133
134 namespace llvm {
135 JITEventListener *JITEventListener::createOProfileJITEventListener() {
136   static std::unique_ptr<OProfileWrapper> JITProfilingWrapper(
137       new OProfileWrapper);
138   return new OProfileJITEventListener(*JITProfilingWrapper);
139 }
140
141 // for testing
142 JITEventListener *JITEventListener::createOProfileJITEventListener(
143                                       OProfileWrapper* TestImpl) {
144   return new OProfileJITEventListener(*TestImpl);
145 }
146
147 } // namespace llvm
148