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 calls into OProfile to tell
11 // it about JITted functions. For now, we only record function names and sizes,
12 // but eventually we'll also record line number information.
14 // See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the
15 // definition of the interface we're using.
17 //===----------------------------------------------------------------------===//
19 #define DEBUG_TYPE "oprofile-jit-event-listener"
20 #include "llvm/Function.h"
21 #include "llvm/ExecutionEngine/JITEventListener.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/System/Errno.h"
24 #include "llvm/Config/config.h"
28 #if defined(USE_OPROFILE)
34 class OProfileJITEventListener : public JITEventListener {
37 OProfileJITEventListener();
38 ~OProfileJITEventListener();
40 virtual void NotifyFunctionEmitted(const Function &F,
41 void *FnStart, size_t FnSize,
42 const EmittedFunctionDetails &Details);
43 virtual void NotifyFreeingMachineCode(const Function &F, void *OldPtr);
46 OProfileJITEventListener::OProfileJITEventListener()
47 : Agent(op_open_agent()) {
49 const std::string err_str = sys::StrError();
50 DOUT << "Failed to connect to OProfile agent: " << err_str << "\n";
52 DOUT << "Connected to OProfile agent.\n";
56 OProfileJITEventListener::~OProfileJITEventListener() {
58 if (op_close_agent(Agent) == -1) {
59 const std::string err_str = sys::StrError();
60 DOUT << "Failed to disconnect from OProfile agent: " << err_str << "\n";
62 DOUT << "Disconnected from OProfile agent.\n";
67 // Adds the just-emitted function to the symbol table.
68 void OProfileJITEventListener::NotifyFunctionEmitted(
69 const Function &F, void *FnStart, size_t FnSize,
70 const EmittedFunctionDetails &) {
71 const char *const FnName = F.getNameStart();
72 assert(FnName != 0 && FnStart != 0 && "Bad symbol to add");
73 if (op_write_native_code(Agent, FnName,
74 reinterpret_cast<uint64_t>(FnStart),
75 FnStart, FnSize) == -1) {
76 DOUT << "Failed to tell OProfile about native function " << FnName
77 << " at [" << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n";
81 // Removes the to-be-deleted function from the symbol table.
82 void OProfileJITEventListener::NotifyFreeingMachineCode(
83 const Function &F, void *FnStart) {
84 assert(FnStart && "Invalid function pointer");
85 if (op_unload_native_code(Agent, reinterpret_cast<uint64_t>(FnStart)) == -1) {
86 DOUT << "Failed to tell OProfile about unload of native function "
87 << F.getName() << " at " << FnStart << "\n";
91 } // anonymous namespace.
94 JITEventListener *createOProfileJITEventListener() {
95 return new OProfileJITEventListener;
99 #else // !defined(USE_OPROFILE)
102 // By defining this to return NULL, we can let clients call it unconditionally,
103 // even if they haven't configured with the OProfile libraries.
104 JITEventListener *createOProfileJITEventListener() {
109 #endif // defined(USE_OPROFILE)