Fix OProfileJITEventListener build for new DebugLoc.
[oota-llvm.git] / lib / ExecutionEngine / JIT / OProfileJITEventListener.cpp
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/System/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   // Now we convert the line number information from the address/DebugLoc format
118   // in Details to the address/filename/lineno format that OProfile expects.
119   // OProfile 0.9.4 (and maybe later versions) has a bug that causes it to
120   // ignore line numbers for addresses above 4G.
121   FilenameCache Filenames;
122   std::vector<debug_line_info> LineInfo;
123   LineInfo.reserve(1 + Details.LineStarts.size());
124   if (!Details.MF->getDefaultDebugLoc().isUnknown()) {
125     LineInfo.push_back(LineStartToOProfileFormat(
126         *Details.MF, Filenames,
127         reinterpret_cast<uintptr_t>(FnStart),
128         Details.MF->getDefaultDebugLoc()));
129   }
130   for (std::vector<EmittedFunctionDetails::LineStart>::const_iterator
131            I = Details.LineStarts.begin(), E = Details.LineStarts.end();
132        I != E; ++I) {
133     LineInfo.push_back(LineStartToOProfileFormat(
134         *Details.MF, Filenames, I->Address, I->Loc));
135   }
136   if (!LineInfo.empty()) {
137     if (op_write_debug_line_info(Agent, FnStart,
138                                  LineInfo.size(), &*LineInfo.begin()) == -1) {
139       DEBUG(dbgs() 
140             << "Failed to tell OProfile about line numbers for native function "
141             << F.getName() << " at [" 
142             << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n");
143     }
144   }
145 }
146
147 // Removes the being-deleted function from the symbol table.
148 void OProfileJITEventListener::NotifyFreeingMachineCode(void *FnStart) {
149   assert(FnStart && "Invalid function pointer");
150   if (op_unload_native_code(Agent, reinterpret_cast<uint64_t>(FnStart)) == -1) {
151     DEBUG(dbgs()
152           << "Failed to tell OProfile about unload of native function at "
153           << FnStart << "\n");
154   }
155 }
156
157 }  // anonymous namespace.
158
159 namespace llvm {
160 JITEventListener *createOProfileJITEventListener() {
161   return new OProfileJITEventListener;
162 }
163 }
164
165 #else  // USE_OPROFILE
166
167 namespace llvm {
168 // By defining this to return NULL, we can let clients call it unconditionally,
169 // even if they haven't configured with the OProfile libraries.
170 JITEventListener *createOProfileJITEventListener() {
171   return NULL;
172 }
173 }  // namespace llvm
174
175 #endif  // USE_OPROFILE