LLVM: lib/ExecutionEngine/OProfileJIT/OProfileWrapper.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

21#include

22#include <dirent.h>

23#include <fcntl.h>

24#include

25#include <stddef.h>

26#include <sys/stat.h>

27#include <unistd.h>

28

29#define DEBUG_TYPE "oprofile-wrapper"

30

31namespace {

32

33

35

36}

37

38namespace llvm {

39

41: Agent(0),

42 OpenAgentFunc(0),

43 CloseAgentFunc(0),

44 WriteNativeCodeFunc(0),

45 WriteDebugLineInfoFunc(0),

46 UnloadNativeCodeFunc(0),

47 MajorVersionFunc(0),

48 MinorVersionFunc(0),

49 IsOProfileRunningFunc(0),

50 Initialized(false) {

51}

52

53bool OProfileWrapper::initialize() {

54 using namespace llvm;

56

57 std::lock_guardsys::Mutex Guard(OProfileInitializationMutex);

58

59 if (Initialized)

60 return OpenAgentFunc != 0;

61

62 Initialized = true;

63

64

65 if (!isOProfileRunning()) {

66 LLVM_DEBUG(dbgs() << "OProfile daemon is not detected.\n");

67 return false;

68 }

69

70 std::string error;

74 << "OProfile connector library libopagent.so could not be loaded: "

75 << error << "\n");

76 }

77

78

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)

93

94

95 if (!OpenAgentFunc

96 || !CloseAgentFunc

97 || !WriteNativeCodeFunc

98 || !WriteDebugLineInfoFunc

99 || !UnloadNativeCodeFunc) {

100 OpenAgentFunc = 0;

101 CloseAgentFunc = 0;

102 WriteNativeCodeFunc = 0;

103 WriteDebugLineInfoFunc = 0;

104 UnloadNativeCodeFunc = 0;

105 return false;

106 }

107

108 return true;

109}

110

111bool OProfileWrapper::isOProfileRunning() {

112 if (IsOProfileRunningFunc != 0)

113 return IsOProfileRunningFunc();

114 return checkForOProfileProcEntry();

115}

116

117bool OProfileWrapper::checkForOProfileProcEntry() {

118 DIR* ProcDir;

119

120 ProcDir = opendir("/proc");

121 if (!ProcDir)

122 return false;

123

124

125 struct dirent* Entry;

126 while (0 != (Entry = readdir(ProcDir))) {

127 if (Entry->d_type == DT_DIR) {

128

129 SmallString<256> CmdLineFName;

130 raw_svector_ostream(CmdLineFName) << "/proc/" << Entry->d_name

131 << "/cmdline";

132

133

134 int CmdLineFD = open(CmdLineFName.c_str(), S_IRUSR);

135 if (CmdLineFD != -1) {

137 char* BaseName = 0;

138

139

140 ssize_t NumRead = read(CmdLineFD, ExeName, PATH_MAX+1);

141 close(CmdLineFD);

142 ssize_t Idx = 0;

143

144 if (ExeName[0] != '/') {

145 BaseName = ExeName;

146 }

147

148

149 while (Idx < NumRead-1 && ExeName[Idx] != 0) {

150 Idx++;

151 }

152

153

154 Idx--;

155

156

157 while (Idx > 0) {

158 if (ExeName[Idx] == '/') {

159 BaseName = ExeName + Idx + 1;

160 break;

161 }

162 Idx--;

163 }

164

165

166 if (BaseName != 0 && (!strcmp("oprofiled", BaseName) ||

167 !strcmp("operf", BaseName))) {

168

169 closedir(ProcDir);

170 return true;

171 }

172 }

173 }

174 }

175

176

177 closedir(ProcDir);

178 return false;

179}

180

182 if (!Initialized)

184

185 if (OpenAgentFunc != 0) {

186 Agent = OpenAgentFunc();

187 return Agent != 0;

188 }

189

190 return false;

191}

192

194 if (!Initialized)

196

197 int ret = -1;

198 if (Agent && CloseAgentFunc) {

199 ret = CloseAgentFunc(Agent);

200 if (ret == 0) {

201 Agent = 0;

202 }

203 }

204 return ret;

205}

206

208 return Agent != 0;

209}

210

213 void const* Code,

214 const unsigned int Size) {

215 if (!Initialized)

217

218 if (Agent && WriteNativeCodeFunc)

219 return WriteNativeCodeFunc(Agent, Name, Addr, Code, Size);

220

221 return -1;

222}

223

225 void const* Code,

226 size_t NumEntries,

227 struct debug_line_info const* Info) {

228 if (!Initialized)

230

231 if (Agent && WriteDebugLineInfoFunc)

232 return WriteDebugLineInfoFunc(Agent, Code, NumEntries, Info);

233

234 return -1;

235}

236

238 if (!Initialized)

240

241 if (Agent && MajorVersionFunc)

242 return MajorVersionFunc();

243

244 return -1;

245}

246

248 if (!Initialized)

250

251 if (Agent && MinorVersionFunc)

252 return MinorVersionFunc();

253

254 return -1;

255}

256

258 if (!Initialized)

260

261 if (Agent && UnloadNativeCodeFunc)

262 return UnloadNativeCodeFunc(Agent, Addr);

263

264 return -1;

265}

266

267}

Analysis containing CSE Info

This file defines the SmallString class.

static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, const llvm::StringTable &StandardNames, VectorLibrary VecLib)

Initialize the set of available library functions based on the specified target triple.

OProfileWrapper()

Definition OProfileWrapper.cpp:40

int op_write_native_code(const char *name, uint64_t addr, void const *code, const unsigned int size)

Definition OProfileWrapper.cpp:211

bool op_open_agent()

Definition OProfileWrapper.cpp:181

int op_unload_native_code(uint64_t addr)

Definition OProfileWrapper.cpp:257

bool isAgentAvailable()

Definition OProfileWrapper.cpp:207

int op_close_agent()

Definition OProfileWrapper.cpp:193

int op_write_debug_line_info(void const *code, size_t num_entries, struct debug_line_info const *info)

Definition OProfileWrapper.cpp:224

int op_minor_version()

Definition OProfileWrapper.cpp:247

int op_major_version()

Definition OProfileWrapper.cpp:237

static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)

This function permanently loads the dynamic library at the given path.

static LLVM_ABI void * SearchForAddressOfSymbol(const char *symbolName)

This function will search through all previously loaded dynamic libraries for the symbol symbolName.

value_type read(const void *memory, endianness endian)

Read a value of a particular endianness from memory.

SmartMutex< false > Mutex

Mutex - A standard, always enforced mutex.

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.