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 NotifyFreeingMachineCode(void *OldPtr);
54 virtual void NotifyObjectEmitted(const ObjectImage &Obj);
56 virtual void NotifyFreeingObject(const ObjectImage &Obj);
59 void OProfileJITEventListener::initialize() {
60 if (!Wrapper.op_open_agent()) {
61 const std::string err_str = sys::StrError();
62 DEBUG(dbgs() << "Failed to connect to OProfile agent: " << err_str << "\n");
64 DEBUG(dbgs() << "Connected to OProfile agent.\n");
68 OProfileJITEventListener::~OProfileJITEventListener() {
69 if (Wrapper.isAgentAvailable()) {
70 if (Wrapper.op_close_agent() == -1) {
71 const std::string err_str = sys::StrError();
72 DEBUG(dbgs() << "Failed to disconnect from OProfile agent: "
75 DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
80 // Removes the being-deleted function from the symbol table.
81 void OProfileJITEventListener::NotifyFreeingMachineCode(void *FnStart) {
82 assert(FnStart && "Invalid function pointer");
83 if (Wrapper.op_unload_native_code(reinterpret_cast<uint64_t>(FnStart)) == -1) {
85 << "Failed to tell OProfile about unload of native function at "
90 void OProfileJITEventListener::NotifyObjectEmitted(const ObjectImage &Obj) {
91 if (!Wrapper.isAgentAvailable()) {
95 // Use symbol info to iterate functions in the object.
96 for (object::symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols();
98 object::SymbolRef::Type SymType;
99 if (I->getType(SymType)) continue;
100 if (SymType == object::SymbolRef::ST_Function) {
104 if (I->getName(Name)) continue;
105 if (I->getAddress(Addr)) continue;
106 if (I->getSize(Size)) continue;
108 if (Wrapper.op_write_native_code(Name.data(), Addr, (void*)Addr, Size)
110 DEBUG(dbgs() << "Failed to tell OProfile about native function "
112 << (void*)Addr << "-" << ((char*)Addr + Size) << "]\n");
115 // TODO: support line number info (similar to IntelJITEventListener.cpp)
120 void OProfileJITEventListener::NotifyFreeingObject(const ObjectImage &Obj) {
121 if (!Wrapper.isAgentAvailable()) {
125 // Use symbol info to iterate functions in the object.
126 for (object::symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols();
128 object::SymbolRef::Type SymType;
129 if (I->getType(SymType)) continue;
130 if (SymType == object::SymbolRef::ST_Function) {
132 if (I->getAddress(Addr)) continue;
134 if (Wrapper.op_unload_native_code(Addr) == -1) {
136 << "Failed to tell OProfile about unload of native function at "
137 << (void*)Addr << "\n");
144 } // anonymous namespace.
147 JITEventListener *JITEventListener::createOProfileJITEventListener() {
148 static std::unique_ptr<OProfileWrapper> JITProfilingWrapper(
149 new OProfileWrapper);
150 return new OProfileJITEventListener(*JITProfilingWrapper);
154 JITEventListener *JITEventListener::createOProfileJITEventListener(
155 OProfileWrapper* TestImpl) {
156 return new OProfileJITEventListener(*TestImpl);