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/Analysis/DebugInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/ExecutionEngine/JITEventListener.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/System/Errno.h"
26 #include "llvm/Config/config.h"
36 class OProfileJITEventListener : public JITEventListener {
39 OProfileJITEventListener();
40 ~OProfileJITEventListener();
42 virtual void NotifyFunctionEmitted(const Function &F,
43 void *FnStart, size_t FnSize,
44 const EmittedFunctionDetails &Details);
45 virtual void NotifyFreeingMachineCode(const Function &F, void *OldPtr);
48 OProfileJITEventListener::OProfileJITEventListener()
49 : Agent(op_open_agent()) {
51 const std::string err_str = sys::StrError();
52 DOUT << "Failed to connect to OProfile agent: " << err_str << "\n";
54 DOUT << "Connected to OProfile agent.\n";
58 OProfileJITEventListener::~OProfileJITEventListener() {
60 if (op_close_agent(Agent) == -1) {
61 const std::string err_str = sys::StrError();
62 DOUT << "Failed to disconnect from OProfile agent: " << err_str << "\n";
64 DOUT << "Disconnected from OProfile agent.\n";
70 // Holds the filename of each CompileUnit, so that we can pass the
71 // pointer into oprofile. These char*s are freed in the destructor.
72 DenseMap<GlobalVariable*, char*> Filenames;
73 // Used as the scratch space in DICompileUnit::getFilename().
74 std::string TempFilename;
77 const char* getFilename(GlobalVariable *CompileUnit) {
78 char *&Filename = Filenames[CompileUnit];
79 if (Filename == NULL) {
80 DICompileUnit CU(CompileUnit);
81 Filename = strdup(CU.getFilename(TempFilename).c_str());
86 for (DenseMap<GlobalVariable*, char*>::iterator
87 I = Filenames.begin(), E = Filenames.end(); I != E;++I) {
93 static debug_line_info LineStartToOProfileFormat(
94 const MachineFunction &MF, FilenameCache &Filenames,
95 uintptr_t Address, DebugLoc Loc) {
96 debug_line_info Result;
98 const DebugLocTuple& tuple = MF.getDebugLocTuple(Loc);
99 Result.lineno = tuple.Line;
100 Result.filename = Filenames.getFilename(tuple.CompileUnit);
101 DOUT << "Mapping " << reinterpret_cast<void*>(Result.vma) << " to "
102 << Result.filename << ":" << Result.lineno << "\n";
106 // Adds the just-emitted function to the symbol table.
107 void OProfileJITEventListener::NotifyFunctionEmitted(
108 const Function &F, void *FnStart, size_t FnSize,
109 const EmittedFunctionDetails &Details) {
110 const char *const FnName = F.getNameStart();
111 assert(FnName != 0 && FnStart != 0 && "Bad symbol to add");
112 if (op_write_native_code(Agent, FnName,
113 reinterpret_cast<uint64_t>(FnStart),
114 FnStart, FnSize) == -1) {
115 DOUT << "Failed to tell OProfile about native function " << FnName
116 << " at [" << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n";
120 // Now we convert the line number information from the address/DebugLoc format
121 // in Details to the address/filename/lineno format that OProfile expects.
122 // OProfile 0.9.4 (and maybe later versions) has a bug that causes it to
123 // ignore line numbers for addresses above 4G.
124 FilenameCache Filenames;
125 std::vector<debug_line_info> LineInfo;
126 LineInfo.reserve(1 + Details.LineStarts.size());
127 if (!Details.MF->getDefaultDebugLoc().isUnknown()) {
128 LineInfo.push_back(LineStartToOProfileFormat(
129 *Details.MF, Filenames,
130 reinterpret_cast<uintptr_t>(FnStart),
131 Details.MF->getDefaultDebugLoc()));
133 for (std::vector<EmittedFunctionDetails::LineStart>::const_iterator
134 I = Details.LineStarts.begin(), E = Details.LineStarts.end();
136 LineInfo.push_back(LineStartToOProfileFormat(
137 *Details.MF, Filenames, I->Address, I->Loc));
139 if (!LineInfo.empty()) {
140 if (op_write_debug_line_info(Agent, FnStart,
141 LineInfo.size(), &*LineInfo.begin()) == -1) {
142 DOUT << "Failed to tell OProfile about line numbers for native function "
143 << FnName << " at [" << FnStart << "-" << ((char*)FnStart + FnSize)
149 // Removes the to-be-deleted function from the symbol table.
150 void OProfileJITEventListener::NotifyFreeingMachineCode(
151 const Function &F, void *FnStart) {
152 assert(FnStart && "Invalid function pointer");
153 if (op_unload_native_code(Agent, reinterpret_cast<uint64_t>(FnStart)) == -1) {
154 DOUT << "Failed to tell OProfile about unload of native function "
155 << F.getName() << " at " << FnStart << "\n";
159 } // anonymous namespace.
162 JITEventListener *createOProfileJITEventListener() {
163 return new OProfileJITEventListener;
167 #else // USE_OPROFILE
170 // By defining this to return NULL, we can let clients call it unconditionally,
171 // even if they haven't configured with the OProfile libraries.
172 JITEventListener *createOProfileJITEventListener() {
177 #endif // USE_OPROFILE