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 #include "llvm/ExecutionEngine/OProfileWrapper.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/DynamicLibrary.h"
20 #include "llvm/Support/Mutex.h"
21 #include "llvm/Support/MutexGuard.h"
22 #include "llvm/Support/raw_ostream.h"
31 #define DEBUG_TYPE "oprofile-wrapper"
35 // Global mutex to ensure a single thread initializes oprofile agent.
36 llvm::sys::Mutex OProfileInitializationMutex;
38 } // anonymous namespace
42 OProfileWrapper::OProfileWrapper()
46 WriteNativeCodeFunc(0),
47 WriteDebugLineInfoFunc(0),
48 UnloadNativeCodeFunc(0),
51 IsOProfileRunningFunc(0),
55 bool OProfileWrapper::initialize() {
57 using namespace llvm::sys;
59 MutexGuard Guard(OProfileInitializationMutex);
62 return OpenAgentFunc != 0;
66 // If the oprofile daemon is not running, don't load the opagent library
67 if (!isOProfileRunning()) {
68 DEBUG(dbgs() << "OProfile daemon is not detected.\n");
73 if(!DynamicLibrary::LoadLibraryPermanently("libopagent.so", &error)) {
75 << "OProfile connector library libopagent.so could not be loaded: "
79 // Get the addresses of the opagent functions
80 OpenAgentFunc = (op_open_agent_ptr_t)(intptr_t)
81 DynamicLibrary::SearchForAddressOfSymbol("op_open_agent");
82 CloseAgentFunc = (op_close_agent_ptr_t)(intptr_t)
83 DynamicLibrary::SearchForAddressOfSymbol("op_close_agent");
84 WriteNativeCodeFunc = (op_write_native_code_ptr_t)(intptr_t)
85 DynamicLibrary::SearchForAddressOfSymbol("op_write_native_code");
86 WriteDebugLineInfoFunc = (op_write_debug_line_info_ptr_t)(intptr_t)
87 DynamicLibrary::SearchForAddressOfSymbol("op_write_debug_line_info");
88 UnloadNativeCodeFunc = (op_unload_native_code_ptr_t)(intptr_t)
89 DynamicLibrary::SearchForAddressOfSymbol("op_unload_native_code");
90 MajorVersionFunc = (op_major_version_ptr_t)(intptr_t)
91 DynamicLibrary::SearchForAddressOfSymbol("op_major_version");
92 MinorVersionFunc = (op_major_version_ptr_t)(intptr_t)
93 DynamicLibrary::SearchForAddressOfSymbol("op_minor_version");
95 // With missing functions, we can do nothing
98 || !WriteNativeCodeFunc
99 || !WriteDebugLineInfoFunc
100 || !UnloadNativeCodeFunc) {
103 WriteNativeCodeFunc = 0;
104 WriteDebugLineInfoFunc = 0;
105 UnloadNativeCodeFunc = 0;
112 bool OProfileWrapper::isOProfileRunning() {
113 if (IsOProfileRunningFunc != 0)
114 return IsOProfileRunningFunc();
115 return checkForOProfileProcEntry();
118 bool OProfileWrapper::checkForOProfileProcEntry() {
121 ProcDir = opendir("/proc");
125 // Walk the /proc tree looking for the oprofile daemon
126 struct dirent* Entry;
127 while (0 != (Entry = readdir(ProcDir))) {
128 if (Entry->d_type == DT_DIR) {
129 // Build a path from the current entry name
130 SmallString<256> CmdLineFName;
131 raw_svector_ostream(CmdLineFName) << "/proc/" << Entry->d_name
134 // Open the cmdline file
135 int CmdLineFD = open(CmdLineFName.c_str(), S_IRUSR);
136 if (CmdLineFD != -1) {
137 char ExeName[PATH_MAX+1];
140 // Read the cmdline file
141 ssize_t NumRead = read(CmdLineFD, ExeName, PATH_MAX+1);
145 if (ExeName[0] != '/') {
149 // Find the terminator for the first string
150 while (Idx < NumRead-1 && ExeName[Idx] != 0) {
154 // Go back to the last non-null character
157 // Find the last path separator in the first string
159 if (ExeName[Idx] == '/') {
160 BaseName = ExeName + Idx + 1;
166 // Test this to see if it is the oprofile daemon
167 if (BaseName != 0 && (!strcmp("oprofiled", BaseName) ||
168 !strcmp("operf", BaseName))) {
169 // If it is, we're done
177 // We've looked through all the files and didn't find the daemon
182 bool OProfileWrapper::op_open_agent() {
186 if (OpenAgentFunc != 0) {
187 Agent = OpenAgentFunc();
194 int OProfileWrapper::op_close_agent() {
199 if (Agent && CloseAgentFunc) {
200 ret = CloseAgentFunc(Agent);
208 bool OProfileWrapper::isAgentAvailable() {
212 int OProfileWrapper::op_write_native_code(const char* Name,
215 const unsigned int Size) {
219 if (Agent && WriteNativeCodeFunc)
220 return WriteNativeCodeFunc(Agent, Name, Addr, Code, Size);
225 int OProfileWrapper::op_write_debug_line_info(
228 struct debug_line_info const* Info) {
232 if (Agent && WriteDebugLineInfoFunc)
233 return WriteDebugLineInfoFunc(Agent, Code, NumEntries, Info);
238 int OProfileWrapper::op_major_version() {
242 if (Agent && MajorVersionFunc)
243 return MajorVersionFunc();
248 int OProfileWrapper::op_minor_version() {
252 if (Agent && MinorVersionFunc)
253 return MinorVersionFunc();
258 int OProfileWrapper::op_unload_native_code(uint64_t Addr) {
262 if (Agent && UnloadNativeCodeFunc)
263 return UnloadNativeCodeFunc(Agent, Addr);