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 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. 13 // 14 // See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the 15 // definition of the interface we're using. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #define DEBUG_TYPE "oprofile-jit-event-listener" 20 #include "llvm/Function.h" 21 #include "llvm/Metadata.h" 22 #include "llvm/ADT/DenseMap.h" 23 #include "llvm/Analysis/DebugInfo.h" 24 #include "llvm/CodeGen/MachineFunction.h" 25 #include "llvm/ExecutionEngine/JITEventListener.h" 26 #include "llvm/Support/Debug.h" 27 #include "llvm/Support/ValueHandle.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include "llvm/Support/Errno.h" 30 #include "llvm/Config/config.h" 31 #include <stddef.h> 32 using namespace llvm; 33 34 #if USE_OPROFILE 35 36 #include <opagent.h> 37 38 namespace { 39 40 class OProfileJITEventListener : public JITEventListener { 41 op_agent_t Agent; 42 public: 43 OProfileJITEventListener(); 44 ~OProfileJITEventListener(); 45 46 virtual void NotifyFunctionEmitted(const Function &F, 47 void *FnStart, size_t FnSize, 48 const EmittedFunctionDetails &Details); 49 virtual void NotifyFreeingMachineCode(void *OldPtr); 50 }; 51 52 OProfileJITEventListener::OProfileJITEventListener() 53 : Agent(op_open_agent()) { 54 if (Agent == NULL) { 55 const std::string err_str = sys::StrError(); 56 DEBUG(dbgs() << "Failed to connect to OProfile agent: " << err_str << "\n"); 57 } else { 58 DEBUG(dbgs() << "Connected to OProfile agent.\n"); 59 } 60 } 61 62 OProfileJITEventListener::~OProfileJITEventListener() { 63 if (Agent != NULL) { 64 if (op_close_agent(Agent) == -1) { 65 const std::string err_str = sys::StrError(); 66 DEBUG(dbgs() << "Failed to disconnect from OProfile agent: " 67 << err_str << "\n"); 68 } else { 69 DEBUG(dbgs() << "Disconnected from OProfile agent.\n"); 70 } 71 } 72 } 73 74 class FilenameCache { 75 // Holds the filename of each Scope, so that we can pass a null-terminated 76 // string into oprofile. Use an AssertingVH rather than a ValueMap because we 77 // shouldn't be modifying any MDNodes while this map is alive. 78 DenseMap<AssertingVH<MDNode>, std::string> Filenames; 79 80 public: 81 const char *getFilename(MDNode *Scope) { 82 std::string &Filename = Filenames[Scope]; 83 if (Filename.empty()) { 84 Filename = DIScope(Scope).getFilename(); 85 } 86 return Filename.c_str(); 87 } 88 }; 89 90 static debug_line_info LineStartToOProfileFormat( 91 const MachineFunction &MF, FilenameCache &Filenames, 92 uintptr_t Address, DebugLoc Loc) { 93 debug_line_info Result; 94 Result.vma = Address; 95 Result.lineno = Loc.getLine(); 96 Result.filename = Filenames.getFilename( 97 Loc.getScope(MF.getFunction()->getContext())); 98 DEBUG(dbgs() << "Mapping " << reinterpret_cast<void*>(Result.vma) << " to " 99 << Result.filename << ":" << Result.lineno << "\n"); 100 return Result; 101 } 102 103 // Adds the just-emitted function to the symbol table. 104 void OProfileJITEventListener::NotifyFunctionEmitted( 105 const Function &F, void *FnStart, size_t FnSize, 106 const EmittedFunctionDetails &Details) { 107 assert(F.hasName() && FnStart != 0 && "Bad symbol to add"); 108 if (op_write_native_code(Agent, F.getName().data(), 109 reinterpret_cast<uint64_t>(FnStart), 110 FnStart, FnSize) == -1) { 111 DEBUG(dbgs() << "Failed to tell OProfile about native function " 112 << F.getName() << " at [" 113 << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n"); 114 return; 115 } 116 117 if (!Details.LineStarts.empty()) { 118 // Now we convert the line number information from the address/DebugLoc 119 // format in Details to the address/filename/lineno format that OProfile 120 // expects. Note that OProfile 0.9.4 has a bug that causes it to ignore 121 // line numbers for addresses above 4G. 122 FilenameCache Filenames; 123 std::vector<debug_line_info> LineInfo; 124 LineInfo.reserve(1 + Details.LineStarts.size()); 125 126 DebugLoc FirstLoc = Details.LineStarts[0].Loc; 127 assert(!FirstLoc.isUnknown() 128 && "LineStarts should not contain unknown DebugLocs"); 129 MDNode *FirstLocScope = FirstLoc.getScope(F.getContext()); 130 DISubprogram FunctionDI = getDISubprogram(FirstLocScope); 131 if (FunctionDI.Verify()) { 132 // If we have debug info for the function itself, use that as the line 133 // number of the first several instructions. Otherwise, after filling 134 // LineInfo, we'll adjust the address of the first line number to point at 135 // the start of the function. 136 debug_line_info line_info; 137 line_info.vma = reinterpret_cast<uintptr_t>(FnStart); 138 line_info.lineno = FunctionDI.getLineNumber(); 139 line_info.filename = Filenames.getFilename(FirstLocScope); 140 LineInfo.push_back(line_info); 141 } 142 143 for (std::vector<EmittedFunctionDetails::LineStart>::const_iterator 144 I = Details.LineStarts.begin(), E = Details.LineStarts.end(); 145 I != E; ++I) { 146 LineInfo.push_back(LineStartToOProfileFormat( 147 *Details.MF, Filenames, I->Address, I->Loc)); 148 } 149 150 // In case the function didn't have line info of its own, adjust the first 151 // line info's address to include the start of the function. 152 LineInfo[0].vma = reinterpret_cast<uintptr_t>(FnStart); 153 154 if (op_write_debug_line_info(Agent, FnStart, 155 LineInfo.size(), &*LineInfo.begin()) == -1) { 156 DEBUG(dbgs() 157 << "Failed to tell OProfile about line numbers for native function " 158 << F.getName() << " at [" 159 << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n"); 160 } 161 } 162 } 163 164 // Removes the being-deleted function from the symbol table. 165 void OProfileJITEventListener::NotifyFreeingMachineCode(void *FnStart) { 166 assert(FnStart && "Invalid function pointer"); 167 if (op_unload_native_code(Agent, reinterpret_cast<uint64_t>(FnStart)) == -1) { 168 DEBUG(dbgs() 169 << "Failed to tell OProfile about unload of native function at " 170 << FnStart << "\n"); 171 } 172 } 173 174 } // anonymous namespace. 175 176 namespace llvm { 177 JITEventListener *createOProfileJITEventListener() { 178 return new OProfileJITEventListener; 179 } 180 } 181 182 #else // USE_OPROFILE 183 184 namespace llvm { 185 // By defining this to return NULL, we can let clients call it unconditionally, 186 // even if they haven't configured with the OProfile libraries. 187 JITEventListener *createOProfileJITEventListener() { 188 return NULL; 189 } 190 } // namespace llvm 191 192 #endif // USE_OPROFILE 193