1 //===-- OProfileWrapper.cpp - OProfile JIT API Wrapper implementation -----===//
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 implements the interface in OProfileWrapper.h. It is responsible
11 // for loading the opagent dynamic library when the first call to an op_
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "oprofile-wrapper"
17 #include "llvm/ExecutionEngine/OProfileWrapper.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/DynamicLibrary.h"
21 #include "llvm/Support/Mutex.h"
22 #include "llvm/Support/MutexGuard.h"
23 #include "llvm/Support/raw_ostream.h"
34 // Global mutex to ensure a single thread initializes oprofile agent.
35 llvm::sys::Mutex OProfileInitializationMutex;
37 } // anonymous namespace
41 OProfileWrapper::OProfileWrapper()
45 WriteNativeCodeFunc(0),
46 WriteDebugLineInfoFunc(0),
47 UnloadNativeCodeFunc(0),
50 IsOProfileRunningFunc(0),
54 bool OProfileWrapper::initialize() {
56 using namespace llvm::sys;
58 MutexGuard Guard(OProfileInitializationMutex);
61 return OpenAgentFunc != 0;
65 // If the oprofile daemon is not running, don't load the opagent library
66 if (!isOProfileRunning()) {
67 DEBUG(dbgs() << "OProfile daemon is not detected.\n");
72 if(!DynamicLibrary::LoadLibraryPermanently("libopagent.so", &error)) {
74 << "OProfile connector library libopagent.so could not be loaded: "
78 // Get the addresses of the opagent functions
79 OpenAgentFunc = (op_open_agent_ptr_t)(intptr_t)
80 DynamicLibrary::SearchForAddressOfSymbol("op_open_agent");
81 CloseAgentFunc = (op_close_agent_ptr_t)(intptr_t)
82 DynamicLibrary::SearchForAddressOfSymbol("op_close_agent");
83 WriteNativeCodeFunc = (op_write_native_code_ptr_t)(intptr_t)
84 DynamicLibrary::SearchForAddressOfSymbol("op_write_native_code");
85 WriteDebugLineInfoFunc = (op_write_debug_line_info_ptr_t)(intptr_t)
86 DynamicLibrary::SearchForAddressOfSymbol("op_write_debug_line_info");
87 UnloadNativeCodeFunc = (op_unload_native_code_ptr_t)(intptr_t)
88 DynamicLibrary::SearchForAddressOfSymbol("op_unload_native_code");
89 MajorVersionFunc = (op_major_version_ptr_t)(intptr_t)
90 DynamicLibrary::SearchForAddressOfSymbol("op_major_version");
91 MinorVersionFunc = (op_major_version_ptr_t)(intptr_t)
92 DynamicLibrary::SearchForAddressOfSymbol("op_minor_version");
94 // With missing functions, we can do nothing
97 || !WriteNativeCodeFunc
98 || !WriteDebugLineInfoFunc
99 || !UnloadNativeCodeFunc) {
102 WriteNativeCodeFunc = 0;
103 WriteDebugLineInfoFunc = 0;
104 UnloadNativeCodeFunc = 0;
111 bool OProfileWrapper::isOProfileRunning() {
112 if (IsOProfileRunningFunc != 0)
113 return IsOProfileRunningFunc();
114 return checkForOProfileProcEntry();
117 bool OProfileWrapper::checkForOProfileProcEntry() {
120 ProcDir = opendir("/proc");
124 // Walk the /proc tree looking for the oprofile daemon
125 struct dirent* Entry;
126 while (0 != (Entry = readdir(ProcDir))) {
127 if (Entry->d_type == DT_DIR) {
128 // Build a path from the current entry name
129 SmallString<256> CmdLineFName;
130 raw_svector_ostream(CmdLineFName) << "/proc/" << Entry->d_name
133 // Open the cmdline file
134 int CmdLineFD = open(CmdLineFName.c_str(), S_IRUSR);
135 if (CmdLineFD != -1) {
136 char ExeName[PATH_MAX+1];
139 // Read the cmdline file
140 ssize_t NumRead = read(CmdLineFD, ExeName, PATH_MAX+1);
144 if (ExeName[0] != '/') {
148 // Find the terminator for the first string
149 while (Idx < NumRead-1 && ExeName[Idx] != 0) {
153 // Go back to the last non-null character
156 // Find the last path separator in the first string
158 if (ExeName[Idx] == '/') {
159 BaseName = ExeName + Idx + 1;
165 // Test this to see if it is the oprofile daemon
166 if (BaseName != 0 && (!strcmp("oprofiled", BaseName) ||
167 !strcmp("operf", BaseName))) {
168 // If it is, we're done
176 // We've looked through all the files and didn't find the daemon
181 bool OProfileWrapper::op_open_agent() {
185 if (OpenAgentFunc != 0) {
186 Agent = OpenAgentFunc();
193 int OProfileWrapper::op_close_agent() {
198 if (Agent && CloseAgentFunc) {
199 ret = CloseAgentFunc(Agent);
207 bool OProfileWrapper::isAgentAvailable() {
211 int OProfileWrapper::op_write_native_code(const char* Name,
214 const unsigned int Size) {
218 if (Agent && WriteNativeCodeFunc)
219 return WriteNativeCodeFunc(Agent, Name, Addr, Code, Size);
224 int OProfileWrapper::op_write_debug_line_info(
227 struct debug_line_info const* Info) {
231 if (Agent && WriteDebugLineInfoFunc)
232 return WriteDebugLineInfoFunc(Agent, Code, NumEntries, Info);
237 int OProfileWrapper::op_major_version() {
241 if (Agent && MajorVersionFunc)
242 return MajorVersionFunc();
247 int OProfileWrapper::op_minor_version() {
251 if (Agent && MinorVersionFunc)
252 return MinorVersionFunc();
257 int OProfileWrapper::op_unload_native_code(uint64_t Addr) {
261 if (Agent && UnloadNativeCodeFunc)
262 return UnloadNativeCodeFunc(Agent, Addr);