LLVM  3.7.0
OProfileJITEventListener.cpp
Go to the documentation of this file.
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"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Object/SymbolSize.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Errno.h"
27 #include <dirent.h>
28 #include <fcntl.h>
29 
30 using namespace llvm;
31 using namespace llvm::object;
32 
33 #define DEBUG_TYPE "oprofile-jit-event-listener"
34 
35 namespace {
36 
37 class OProfileJITEventListener : public JITEventListener {
38  std::unique_ptr<OProfileWrapper> Wrapper;
39 
40  void initialize();
41  std::map<const char*, OwningBinary<ObjectFile>> DebugObjects;
42 
43 public:
44  OProfileJITEventListener(std::unique_ptr<OProfileWrapper> LibraryWrapper)
45  : Wrapper(std::move(LibraryWrapper)) {
46  initialize();
47  }
48 
49  ~OProfileJITEventListener();
50 
51  void NotifyObjectEmitted(const ObjectFile &Obj,
52  const RuntimeDyld::LoadedObjectInfo &L) override;
53 
54  void NotifyFreeingObject(const ObjectFile &Obj) override;
55 };
56 
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(
79  const ObjectFile &Obj,
81  if (!Wrapper->isAgentAvailable()) {
82  return;
83  }
84 
85  OwningBinary<ObjectFile> DebugObjOwner = L.getObjectForDebug(Obj);
86  const ObjectFile &DebugObj = *DebugObjOwner.getBinary();
87 
88  // Use symbol info to iterate functions in the object.
89  for (const std::pair<SymbolRef, uint64_t> &P : computeSymbolSizes(DebugObj)) {
90  SymbolRef Sym = P.first;
91  if (Sym.getType() != SymbolRef::ST_Function)
92  continue;
93 
94  ErrorOr<StringRef> NameOrErr = Sym.getName();
95  if (NameOrErr.getError())
96  continue;
97  StringRef Name = *NameOrErr;
98  ErrorOr<uint64_t> AddrOrErr = Sym.getAddress();
99  if (AddrOrErr.getError())
100  continue;
101  uint64_t Addr = *AddrOrErr;
102  uint64_t Size = P.second;
103 
104  if (Wrapper->op_write_native_code(Name.data(), Addr, (void *)Addr, Size) ==
105  -1) {
106  DEBUG(dbgs() << "Failed to tell OProfile about native function " << Name
107  << " at [" << (void *)Addr << "-" << ((char *)Addr + Size)
108  << "]\n");
109  continue;
110  }
111  // TODO: support line number info (similar to IntelJITEventListener.cpp)
112  }
113 
114  DebugObjects[Obj.getData().data()] = std::move(DebugObjOwner);
115 }
116 
117 void OProfileJITEventListener::NotifyFreeingObject(const ObjectFile &Obj) {
118  if (Wrapper->isAgentAvailable()) {
119 
120  // If there was no agent registered when the original object was loaded then
121  // we won't have created a debug object for it, so bail out.
122  if (DebugObjects.find(Obj.getData().data()) == DebugObjects.end())
123  return;
124 
125  const ObjectFile &DebugObj = *DebugObjects[Obj.getData().data()].getBinary();
126 
127  // Use symbol info to iterate functions in the object.
128  for (symbol_iterator I = DebugObj.symbol_begin(),
129  E = DebugObj.symbol_end();
130  I != E; ++I) {
131  if (I->getType() == SymbolRef::ST_Function) {
132  ErrorOr<uint64_t> AddrOrErr = I->getAddress();
133  if (AddrOrErr.getError())
134  continue;
135  uint64_t Addr = *AddrOrErr;
136 
137  if (Wrapper->op_unload_native_code(Addr) == -1) {
138  DEBUG(dbgs()
139  << "Failed to tell OProfile about unload of native function at "
140  << (void*)Addr << "\n");
141  continue;
142  }
143  }
144  }
145  }
146 
147  DebugObjects.erase(Obj.getData().data());
148 }
149 
150 } // anonymous namespace.
151 
152 namespace llvm {
154  return new OProfileJITEventListener(llvm::make_unique<OProfileWrapper>());
155 }
156 
157 } // namespace llvm
158 
Information about the loaded object.
Definition: RuntimeDyld.h:59
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
This class is the base class for all object file types.
Definition: ObjectFile.h:176
std::string StrError()
Returns a string representation of the errno value, using whatever thread-safe variant of strerror() ...
Definition: Errno.cpp:32
StringRef getData() const
Definition: Binary.cpp:33
SymbolRef::Type getType() const
Definition: ObjectFile.h:330
basic_symbol_iterator symbol_begin() const
Definition: SymbolicFile.h:136
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
#define P(N)
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, const char *const *StandardNames)
initialize - Initialize the set of available library functions based on the specified target triple...
basic_symbol_iterator symbol_end() const
Definition: SymbolicFile.h:139
virtual object::OwningBinary< object::ObjectFile > getObjectForDebug(const object::ObjectFile &Obj) const =0
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
std::vector< std::pair< SymbolRef, uint64_t > > computeSymbolSizes(const ObjectFile &O)
Definition: SymbolSize.cpp:47
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:114
ErrorOr< StringRef > getName() const
Definition: ObjectFile.h:306
#define I(x, y, z)
Definition: MD5.cpp:54
#define DEBUG(X)
Definition: Debug.h:92
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
ErrorOr< uint64_t > getAddress() const
Returns the symbol virtual address (i.e.
Definition: ObjectFile.h:310
static JITEventListener * createOProfileJITEventListener()