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"
18 #define DEBUG_TYPE "oprofile-wrapper"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/DynamicLibrary.h"
22 #include "llvm/Support/Mutex.h"
23 #include "llvm/Support/MutexGuard.h"
24 #include "llvm/ADT/SmallString.h"
36 // Global mutex to ensure a single thread initializes oprofile agent.
37 llvm::sys::Mutex OProfileInitializationMutex
;
39 } // anonymous namespace
43 OProfileWrapper::OProfileWrapper()
47 WriteNativeCodeFunc(0),
48 WriteDebugLineInfoFunc(0),
49 UnloadNativeCodeFunc(0),
52 IsOProfileRunningFunc(0),
56 bool OProfileWrapper::initialize() {
58 using namespace llvm::sys
;
60 MutexGuard
Guard(OProfileInitializationMutex
);
63 return OpenAgentFunc
!= 0;
67 // If the oprofile daemon is not running, don't load the opagent library
68 if (!isOProfileRunning()) {
69 DEBUG(dbgs() << "OProfile daemon is not detected.\n");
74 if(!DynamicLibrary::LoadLibraryPermanently("libopagent.so", &error
)) {
76 << "OProfile connector library libopagent.so could not be loaded: "
80 // Get the addresses of the opagent functions
81 OpenAgentFunc
= (op_open_agent_ptr_t
)(intptr_t)
82 DynamicLibrary::SearchForAddressOfSymbol("op_open_agent");
83 CloseAgentFunc
= (op_close_agent_ptr_t
)(intptr_t)
84 DynamicLibrary::SearchForAddressOfSymbol("op_close_agent");
85 WriteNativeCodeFunc
= (op_write_native_code_ptr_t
)(intptr_t)
86 DynamicLibrary::SearchForAddressOfSymbol("op_write_native_code");
87 WriteDebugLineInfoFunc
= (op_write_debug_line_info_ptr_t
)(intptr_t)
88 DynamicLibrary::SearchForAddressOfSymbol("op_write_debug_line_info");
89 UnloadNativeCodeFunc
= (op_unload_native_code_ptr_t
)(intptr_t)
90 DynamicLibrary::SearchForAddressOfSymbol("op_unload_native_code");
91 MajorVersionFunc
= (op_major_version_ptr_t
)(intptr_t)
92 DynamicLibrary::SearchForAddressOfSymbol("op_major_version");
93 MinorVersionFunc
= (op_major_version_ptr_t
)(intptr_t)
94 DynamicLibrary::SearchForAddressOfSymbol("op_minor_version");
96 // With missing functions, we can do nothing
99 || !WriteNativeCodeFunc
100 || !WriteDebugLineInfoFunc
101 || !UnloadNativeCodeFunc
) {
104 WriteNativeCodeFunc
= 0;
105 WriteDebugLineInfoFunc
= 0;
106 UnloadNativeCodeFunc
= 0;
113 bool OProfileWrapper::isOProfileRunning() {
114 if (IsOProfileRunningFunc
!= 0)
115 return IsOProfileRunningFunc();
116 return checkForOProfileProcEntry();
119 bool OProfileWrapper::checkForOProfileProcEntry() {
122 ProcDir
= opendir("/proc");
126 // Walk the /proc tree looking for the oprofile daemon
127 struct dirent
* Entry
;
128 while (0 != (Entry
= readdir(ProcDir
))) {
129 if (Entry
->d_type
== DT_DIR
) {
130 // Build a path from the current entry name
131 SmallString
<256> CmdLineFName
;
132 raw_svector_ostream(CmdLineFName
) << "/proc/" << Entry
->d_name
135 // Open the cmdline file
136 int CmdLineFD
= open(CmdLineFName
.c_str(), S_IRUSR
);
137 if (CmdLineFD
!= -1) {
138 char ExeName
[PATH_MAX
+1];
141 // Read the cmdline file
142 ssize_t NumRead
= read(CmdLineFD
, ExeName
, PATH_MAX
+1);
146 // Find the terminator for the first string
147 while (Idx
< NumRead
-1 && ExeName
[Idx
] != 0) {
151 // Go back to the last non-null character
154 // Find the last path separator in the first string
156 if (ExeName
[Idx
] == '/') {
157 BaseName
= ExeName
+ Idx
+ 1;
163 // Test this to see if it is the oprofile daemon
164 if (BaseName
!= 0 && !strcmp("oprofiled", BaseName
)) {
165 // If it is, we're done
173 // We've looked through all the files and didn't find the daemon
178 bool OProfileWrapper::op_open_agent() {
182 if (OpenAgentFunc
!= 0) {
183 Agent
= OpenAgentFunc();
190 int OProfileWrapper::op_close_agent() {
195 if (Agent
&& CloseAgentFunc
) {
196 ret
= CloseAgentFunc(Agent
);
204 bool OProfileWrapper::isAgentAvailable() {
208 int OProfileWrapper::op_write_native_code(const char* Name
,
211 const unsigned int Size
) {
215 if (Agent
&& WriteNativeCodeFunc
)
216 return WriteNativeCodeFunc(Agent
, Name
, Addr
, Code
, Size
);
221 int OProfileWrapper::op_write_debug_line_info(
224 struct debug_line_info
const* Info
) {
228 if (Agent
&& WriteDebugLineInfoFunc
)
229 return WriteDebugLineInfoFunc(Agent
, Code
, NumEntries
, Info
);
234 int OProfileWrapper::op_major_version() {
238 if (Agent
&& MajorVersionFunc
)
239 return MajorVersionFunc();
244 int OProfileWrapper::op_minor_version() {
248 if (Agent
&& MinorVersionFunc
)
249 return MinorVersionFunc();
254 int OProfileWrapper::op_unload_native_code(uint64_t Addr
) {
258 if (Agent
&& UnloadNativeCodeFunc
)
259 return UnloadNativeCodeFunc(Agent
, Addr
);