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/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"
33 using namespace llvm::jitprofiling;
34 using namespace llvm::object;
36 #define DEBUG_TYPE "oprofile-jit-event-listener"
40 class OProfileJITEventListener : public JITEventListener {
41 std::unique_ptr<OProfileWrapper> Wrapper;
44 std::map<const char*, OwningBinary<ObjectFile>> DebugObjects;
47 OProfileJITEventListener(std::unique_ptr<OProfileWrapper> LibraryWrapper)
48 : Wrapper(std::move(LibraryWrapper)) {
52 ~OProfileJITEventListener();
54 void NotifyObjectEmitted(const ObjectFile &Obj,
55 const RuntimeDyld::LoadedObjectInfo &L) override;
57 void NotifyFreeingObject(const ObjectFile &Obj) override;
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");
65 DEBUG(dbgs() << "Connected to OProfile agent.\n");
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: "
76 DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
81 void OProfileJITEventListener::NotifyObjectEmitted(
82 const ObjectFile &Obj,
83 const RuntimeDyld::LoadedObjectInfo &L) {
84 if (!Wrapper->isAgentAvailable()) {
88 OwningBinary<ObjectFile> DebugObjOwner = L.getObjectForDebug(Obj);
89 const ObjectFile &DebugObj = *DebugObjOwner.getBinary();
91 // Use symbol info to iterate functions in the object.
92 for (symbol_iterator I = DebugObj.symbol_begin(), E = DebugObj.symbol_end();
94 SymbolRef::Type SymType;
95 if (I->getType(SymType)) continue;
96 if (SymType == SymbolRef::ST_Function) {
100 if (I->getName(Name)) continue;
101 if (I->getAddress(Addr)) continue;
102 if (I->getSize(Size)) continue;
104 if (Wrapper->op_write_native_code(Name.data(), Addr, (void*)Addr, Size)
106 DEBUG(dbgs() << "Failed to tell OProfile about native function "
108 << (void*)Addr << "-" << ((char*)Addr + Size) << "]\n");
111 // TODO: support line number info (similar to IntelJITEventListener.cpp)
115 DebugObjects[Obj.getData().data()] = std::move(DebugObjOwner);
118 void OProfileJITEventListener::NotifyFreeingObject(const ObjectFile &Obj) {
119 if (Wrapper->isAgentAvailable()) {
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())
126 const ObjectFile &DebugObj = *DebugObjects[Obj.getData().data()].getBinary();
128 // Use symbol info to iterate functions in the object.
129 for (symbol_iterator I = DebugObj.symbol_begin(),
130 E = DebugObj.symbol_end();
132 SymbolRef::Type SymType;
133 if (I->getType(SymType)) continue;
134 if (SymType == SymbolRef::ST_Function) {
136 if (I->getAddress(Addr)) continue;
138 if (Wrapper->op_unload_native_code(Addr) == -1) {
140 << "Failed to tell OProfile about unload of native function at "
141 << (void*)Addr << "\n");
148 DebugObjects.erase(Obj.getData().data());
151 } // anonymous namespace.
154 JITEventListener *JITEventListener::createOProfileJITEventListener() {
155 return new OProfileJITEventListener(llvm::make_unique<OProfileWrapper>());