16 #define DEBUG_TYPE "oprofile-wrapper"
45 WriteNativeCodeFunc(0),
46 WriteDebugLineInfoFunc(0),
47 UnloadNativeCodeFunc(0),
50 IsOProfileRunningFunc(0),
54 bool OProfileWrapper::initialize() {
56 using namespace llvm::sys;
61 return OpenAgentFunc != 0;
66 if (!isOProfileRunning()) {
67 DEBUG(
dbgs() <<
"OProfile daemon is not detected.\n");
74 <<
"OProfile connector library libopagent.so could not be loaded: "
79 OpenAgentFunc = (op_open_agent_ptr_t)(
intptr_t)
81 CloseAgentFunc = (op_close_agent_ptr_t)(
intptr_t)
83 WriteNativeCodeFunc = (op_write_native_code_ptr_t)(
intptr_t)
85 WriteDebugLineInfoFunc = (op_write_debug_line_info_ptr_t)(
intptr_t)
87 UnloadNativeCodeFunc = (op_unload_native_code_ptr_t)(
intptr_t)
89 MajorVersionFunc = (op_major_version_ptr_t)(
intptr_t)
91 MinorVersionFunc = (op_major_version_ptr_t)(
intptr_t)
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() {
125 struct dirent* Entry;
126 while (0 != (Entry = readdir(ProcDir))) {
127 if (Entry->d_type == DT_DIR) {
134 int CmdLineFD =
open(CmdLineFName.
c_str(), S_IRUSR);
135 if (CmdLineFD != -1) {
136 char ExeName[PATH_MAX+1];
140 ssize_t NumRead =
read(CmdLineFD, ExeName, PATH_MAX+1);
144 if (ExeName[0] !=
'/') {
149 while (Idx < NumRead-1 && ExeName[Idx] != 0) {
158 if (ExeName[Idx] ==
'/') {
159 BaseName = ExeName + Idx + 1;
166 if (BaseName != 0 && (!
strcmp(
"oprofiled", BaseName) ||
167 !
strcmp(
"operf", BaseName))) {
185 if (OpenAgentFunc != 0) {
186 Agent = OpenAgentFunc();
198 if (Agent && CloseAgentFunc) {
199 ret = CloseAgentFunc(Agent);
214 const unsigned int Size) {
218 if (Agent && WriteNativeCodeFunc)
219 return WriteNativeCodeFunc(Agent, Name, Addr, Code, Size);
227 struct debug_line_info
const* Info) {
231 if (Agent && WriteDebugLineInfoFunc)
232 return WriteDebugLineInfoFunc(Agent, Code, NumEntries, Info);
241 if (Agent && MajorVersionFunc)
242 return MajorVersionFunc();
251 if (Agent && MinorVersionFunc)
252 return MinorVersionFunc();
261 if (Agent && UnloadNativeCodeFunc)
262 return UnloadNativeCodeFunc(Agent, Addr);
int strcmp(const char *s1, const char *s2);
static void * SearchForAddressOfSymbol(const char *symbolName)
Search through libraries for address of a symbol.
int open(const char *path, int oflag, ... );
DIR *opendir(const char *dirname);.
ssize_t read(int fildes, void *buf, size_t nbyte);
int op_write_debug_line_info(void const *code, size_t num_entries, struct debug_line_info const *info)
Guard a section of code with a Mutex.
static void initialize(TargetLibraryInfo &TLI, const Triple &T, const char **StandardNames)
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=0)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
int op_write_native_code(const char *name, uint64_t addr, void const *code, const unsigned int size)
int op_unload_native_code(uint64_t addr)