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/CodeGen/MachineFunction.h"
17 #include "llvm/ExecutionEngine/JITEventListener.h"
18 #include "llvm/ExecutionEngine/OProfileWrapper.h"
19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/Errno.h"
25 #include "llvm/Support/raw_ostream.h"
30 using namespace llvm::object;
32 #define DEBUG_TYPE "oprofile-jit-event-listener"
36 class OProfileJITEventListener : public JITEventListener {
37 std::unique_ptr<OProfileWrapper> Wrapper;
40 std::map<const char*, OwningBinary<ObjectFile>> DebugObjects;
43 OProfileJITEventListener(std::unique_ptr<OProfileWrapper> LibraryWrapper)
44 : Wrapper(std::move(LibraryWrapper)) {
48 ~OProfileJITEventListener();
50 void NotifyObjectEmitted(const ObjectFile &Obj,
51 const RuntimeDyld::LoadedObjectInfo &L) override;
53 void NotifyFreeingObject(const ObjectFile &Obj) override;
56 void OProfileJITEventListener::initialize() {
57 if (!Wrapper->op_open_agent()) {
58 const std::string err_str = sys::StrError();
59 DEBUG(dbgs() << "Failed to connect to OProfile agent: " << err_str << "\n");
61 DEBUG(dbgs() << "Connected to OProfile agent.\n");
65 OProfileJITEventListener::~OProfileJITEventListener() {
66 if (Wrapper->isAgentAvailable()) {
67 if (Wrapper->op_close_agent() == -1) {
68 const std::string err_str = sys::StrError();
69 DEBUG(dbgs() << "Failed to disconnect from OProfile agent: "
72 DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
77 void OProfileJITEventListener::NotifyObjectEmitted(
78 const ObjectFile &Obj,
79 const RuntimeDyld::LoadedObjectInfo &L) {
80 if (!Wrapper->isAgentAvailable()) {
84 OwningBinary<ObjectFile> DebugObjOwner = L.getObjectForDebug(Obj);
85 const ObjectFile &DebugObj = *DebugObjOwner.getBinary();
87 // Use symbol info to iterate functions in the object.
88 for (symbol_iterator I = DebugObj.symbol_begin(), E = DebugObj.symbol_end();
90 SymbolRef::Type SymType;
91 if (I->getType(SymType)) continue;
92 if (SymType == SymbolRef::ST_Function) {
96 if (I->getName(Name)) continue;
97 if (I->getAddress(Addr)) continue;
98 if (I->getSize(Size)) continue;
100 if (Wrapper->op_write_native_code(Name.data(), Addr, (void*)Addr, Size)
102 DEBUG(dbgs() << "Failed to tell OProfile about native function "
104 << (void*)Addr << "-" << ((char*)Addr + Size) << "]\n");
107 // TODO: support line number info (similar to IntelJITEventListener.cpp)
111 DebugObjects[Obj.getData().data()] = std::move(DebugObjOwner);
114 void OProfileJITEventListener::NotifyFreeingObject(const ObjectFile &Obj) {
115 if (Wrapper->isAgentAvailable()) {
117 // If there was no agent registered when the original object was loaded then
118 // we won't have created a debug object for it, so bail out.
119 if (DebugObjects.find(Obj.getData().data()) == DebugObjects.end())
122 const ObjectFile &DebugObj = *DebugObjects[Obj.getData().data()].getBinary();
124 // Use symbol info to iterate functions in the object.
125 for (symbol_iterator I = DebugObj.symbol_begin(),
126 E = DebugObj.symbol_end();
128 SymbolRef::Type SymType;
129 if (I->getType(SymType)) continue;
130 if (SymType == 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 DebugObjects.erase(Obj.getData().data());
147 } // anonymous namespace.
150 JITEventListener *JITEventListener::createOProfileJITEventListener() {
151 return new OProfileJITEventListener(llvm::make_unique<OProfileWrapper>());