Fixed some bugs.
[llvm/zpu.git] / lib / ExecutionEngine / JIT / OProfileJITEventListener.cpp
blob1ca084b5808b2b69cfc4b9106cf563d873fe6d2f
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.
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/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/System/Errno.h"
30 #include "llvm/Config/config.h"
31 #include <stddef.h>
32 using namespace llvm;
34 #if USE_OPROFILE
36 #include <opagent.h>
38 namespace {
40 class OProfileJITEventListener : public JITEventListener {
41 op_agent_t Agent;
42 public:
43 OProfileJITEventListener();
44 ~OProfileJITEventListener();
46 virtual void NotifyFunctionEmitted(const Function &F,
47 void *FnStart, size_t FnSize,
48 const EmittedFunctionDetails &Details);
49 virtual void NotifyFreeingMachineCode(void *OldPtr);
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");
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");
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;
80 public:
81 const char *getFilename(MDNode *Scope) {
82 std::string &Filename = Filenames[Scope];
83 if (Filename.empty()) {
84 Filename = DIScope(Scope).getFilename();
86 return Filename.c_str();
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;
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;
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());
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);
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));
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);
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");
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");
174 } // anonymous namespace.
176 namespace llvm {
177 JITEventListener *createOProfileJITEventListener() {
178 return new OProfileJITEventListener;
182 #else // USE_OPROFILE
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;
190 } // namespace llvm
192 #endif // USE_OPROFILE