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/Support/raw_ostream.h"
26 #include "llvm/System/Errno.h"
27 #include "llvm/Config/config.h"
37 class OProfileJITEventListener : public JITEventListener {
40 OProfileJITEventListener();
41 ~OProfileJITEventListener();
43 virtual void NotifyFunctionEmitted(const Function &F,
44 void *FnStart, size_t FnSize,
45 const EmittedFunctionDetails &Details);
46 virtual void NotifyFreeingMachineCode(const Function &F, void *OldPtr);
49 OProfileJITEventListener::OProfileJITEventListener()
50 : Agent(op_open_agent()) {
52 const std::string err_str = sys::StrError();
53 DOUT << "Failed to connect to OProfile agent: " << err_str << "\n";
55 DOUT << "Connected to OProfile agent.\n";
59 OProfileJITEventListener::~OProfileJITEventListener() {
61 if (op_close_agent(Agent) == -1) {
62 const std::string err_str = sys::StrError();
63 DOUT << "Failed to disconnect from OProfile agent: " << err_str << "\n";
65 DOUT << "Disconnected from OProfile agent.\n";
71 // Holds the filename of each CompileUnit, so that we can pass the
72 // pointer into oprofile. These char*s are freed in the destructor.
73 DenseMap<GlobalVariable*, char*> Filenames;
74 // Used as the scratch space in DICompileUnit::getFilename().
75 std::string TempFilename;
78 const char* getFilename(GlobalVariable *CompileUnit) {
79 char *&Filename = Filenames[CompileUnit];
80 if (Filename == NULL) {
81 DICompileUnit CU(CompileUnit);
82 Filename = strdup(CU.getFilename(TempFilename).c_str());
87 for (DenseMap<GlobalVariable*, char*>::iterator
88 I = Filenames.begin(), E = Filenames.end(); I != E;++I) {
94 static debug_line_info LineStartToOProfileFormat(
95 const MachineFunction &MF, FilenameCache &Filenames,
96 uintptr_t Address, DebugLoc Loc) {
97 debug_line_info Result;
99 const DebugLocTuple& tuple = MF.getDebugLocTuple(Loc);
100 Result.lineno = tuple.Line;
101 Result.filename = Filenames.getFilename(tuple.CompileUnit);
102 DOUT << "Mapping " << reinterpret_cast<void*>(Result.vma) << " to "
103 << Result.filename << ":" << Result.lineno << "\n";
107 // Adds the just-emitted function to the symbol table.
108 void OProfileJITEventListener::NotifyFunctionEmitted(
109 const Function &F, void *FnStart, size_t FnSize,
110 const EmittedFunctionDetails &Details) {
111 assert(F.hasName() && FnStart != 0 && "Bad symbol to add");
112 if (op_write_native_code(Agent, F.getName().data(),
113 reinterpret_cast<uint64_t>(FnStart),
114 FnStart, FnSize) == -1) {
115 DEBUG(errs() << "Failed to tell OProfile about native function "
116 << F.getName() << " at ["
117 << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n");
121 // Now we convert the line number information from the address/DebugLoc format
122 // in Details to the address/filename/lineno format that OProfile expects.
123 // OProfile 0.9.4 (and maybe later versions) has a bug that causes it to
124 // ignore line numbers for addresses above 4G.
125 FilenameCache Filenames;
126 std::vector<debug_line_info> LineInfo;
127 LineInfo.reserve(1 + Details.LineStarts.size());
128 if (!Details.MF->getDefaultDebugLoc().isUnknown()) {
129 LineInfo.push_back(LineStartToOProfileFormat(
130 *Details.MF, Filenames,
131 reinterpret_cast<uintptr_t>(FnStart),
132 Details.MF->getDefaultDebugLoc()));
134 for (std::vector<EmittedFunctionDetails::LineStart>::const_iterator
135 I = Details.LineStarts.begin(), E = Details.LineStarts.end();
137 LineInfo.push_back(LineStartToOProfileFormat(
138 *Details.MF, Filenames, I->Address, I->Loc));
140 if (!LineInfo.empty()) {
141 if (op_write_debug_line_info(Agent, FnStart,
142 LineInfo.size(), &*LineInfo.begin()) == -1) {
144 << "Failed to tell OProfile about line numbers for native function "
145 << F.getName() << " at ["
146 << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n");
151 // Removes the to-be-deleted function from the symbol table.
152 void OProfileJITEventListener::NotifyFreeingMachineCode(
153 const Function &F, void *FnStart) {
154 assert(FnStart && "Invalid function pointer");
155 if (op_unload_native_code(Agent, reinterpret_cast<uint64_t>(FnStart)) == -1) {
156 DEBUG(errs() << "Failed to tell OProfile about unload of native function "
157 << F.getName() << " at " << FnStart << "\n");
161 } // anonymous namespace.
164 JITEventListener *createOProfileJITEventListener() {
165 return new OProfileJITEventListener;
169 #else // USE_OPROFILE
172 // By defining this to return NULL, we can let clients call it unconditionally,
173 // even if they haven't configured with the OProfile libraries.
174 JITEventListener *createOProfileJITEventListener() {
179 #endif // USE_OPROFILE