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 #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"
33 using namespace llvm::jitprofiling;
35 #define DEBUG_TYPE "oprofile-jit-event-listener"
39 class OProfileJITEventListener : public JITEventListener {
40 OProfileWrapper& Wrapper;
45 OProfileJITEventListener(OProfileWrapper& LibraryWrapper)
46 : Wrapper(LibraryWrapper) {
50 ~OProfileJITEventListener();
52 virtual void NotifyObjectEmitted(const ObjectImage &Obj);
54 virtual void NotifyFreeingObject(const ObjectImage &Obj);
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");
62 DEBUG(dbgs() << "Connected to OProfile agent.\n");
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: "
73 DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
78 void OProfileJITEventListener::NotifyObjectEmitted(const ObjectImage &Obj) {
79 if (!Wrapper.isAgentAvailable()) {
83 // Use symbol info to iterate functions in the object.
84 for (object::symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols();
86 object::SymbolRef::Type SymType;
87 if (I->getType(SymType)) continue;
88 if (SymType == object::SymbolRef::ST_Function) {
92 if (I->getName(Name)) continue;
93 if (I->getAddress(Addr)) continue;
94 if (I->getSize(Size)) continue;
96 if (Wrapper.op_write_native_code(Name.data(), Addr, (void*)Addr, Size)
98 DEBUG(dbgs() << "Failed to tell OProfile about native function "
100 << (void*)Addr << "-" << ((char*)Addr + Size) << "]\n");
103 // TODO: support line number info (similar to IntelJITEventListener.cpp)
108 void OProfileJITEventListener::NotifyFreeingObject(const ObjectImage &Obj) {
109 if (!Wrapper.isAgentAvailable()) {
113 // Use symbol info to iterate functions in the object.
114 for (object::symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols();
116 object::SymbolRef::Type SymType;
117 if (I->getType(SymType)) continue;
118 if (SymType == object::SymbolRef::ST_Function) {
120 if (I->getAddress(Addr)) continue;
122 if (Wrapper.op_unload_native_code(Addr) == -1) {
124 << "Failed to tell OProfile about unload of native function at "
125 << (void*)Addr << "\n");
132 } // anonymous namespace.
135 JITEventListener *JITEventListener::createOProfileJITEventListener() {
136 static std::unique_ptr<OProfileWrapper> JITProfilingWrapper(
137 new OProfileWrapper);
138 return new OProfileJITEventListener(*JITProfilingWrapper);
142 JITEventListener *JITEventListener::createOProfileJITEventListener(
143 OProfileWrapper* TestImpl) {
144 return new OProfileJITEventListener(*TestImpl);