13 #define SYSTEM_MODULEINFO_CPP
21 #include "GaudiKernel/ModuleInfo.h"
22 #include "GaudiKernel/System.h"
27 # define strcasecmp _stricmp
28 # define strncasecmp _strnicmp
32 static PsApiFunctions _psApi;
33 #define getpid _getpid
37 # define PATH_MAX 1024
39 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
42 #include "sys/times.h"
43 #include "sys/param.h"
51 static std::vector<std::string> s_linkedModules;
55 static std::string module(
"");
65 #elif defined(__linux) || defined(__APPLE__)
70 module = mod.substr(0, mod.rfind(
'.'));
78 static std::string module(
"");
81 char name[PATH_MAX] = {
"Unknown.module"};
90 # if defined(__linux) || defined(__APPLE__)
95 if (::realpath(path, name))
108 int loc = module.rfind(
'.')+1;
111 else if ( module[loc] ==
'e' || module[loc] ==
'E' )
114 else if ( module[loc] ==
'd' || module[loc] ==
'D' )
116 else if ( module[loc] ==
's' && module[loc+1] ==
'o' )
127 static long pid = ::getpid();
129 static HANDLE hP = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE,pid);
131 static void* hP = (
void*)pid;
137 ModuleHandle = handle;
141 if ( !ModuleHandle ) {
144 static HINSTANCE handle = 0;
146 if ( 0 == handle && _psApi ) {
147 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof(ModuleHandle), &cbNeeded) ) {
151 #elif defined(__linux) || defined(__APPLE__)
174 static HINSTANCE handle = 0;
176 if ( 0 == handle && _psApi ) {
177 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof(ModuleHandle), &cbNeeded) ) {
183 #elif defined(__linux) || defined(__APPLE__)
185 static Dl_info infoBuf, *info = &infoBuf;
187 void* handle = ::dlopen(
nullptr, RTLD_LAZY);
190 void*
func = ::dlsym(handle,
"main");
193 if ( 0 != ::dladdr(func, &infoBuf) ) {
208 static std::string module(
"");
209 if ( module.length() == 0 ) {
210 char name[PATH_MAX] = {
"Unknown.module"};
217 #elif defined(__linux) || defined(__APPLE__)
219 ::sprintf(cmd,
"/proc/%d/exe", ::getpid());
221 if (::readlink(cmd, name,
sizeof(name)) >= 0)
224 if (::realpath(((HMODULE*)
exeHandle())->dsc.filename, name))
232 if ( s_linkedModules.size() == 0 ) {
236 HINSTANCE handle[1024];
238 if ( _psApi.EnumProcessModules(
processHandle(),handle,
sizeof(handle),&cbNeeded) ) {
239 for (
size_t i = 0;
i < cbNeeded/
sizeof(HANDLE);
i++ ) {
240 if ( 0 < _psApi.GetModuleFileNameExA(
processHandle(), handle[
i], name,
sizeof(name)) ) {
241 s_linkedModules.push_back(name);
246 #elif defined(__linux) || defined(__APPLE__)
247 char ff[512], cmd[1024], fname[1024], buf1[64], buf2[64], buf3[64], buf4[64];
248 ::sprintf(ff,
"/proc/%d/maps", ::getpid());
249 FILE*
maps = ::fopen(ff,
"r");
250 while( ::fgets(cmd,
sizeof(cmd), maps) ) {
252 sscanf(cmd,
"%s %s %s %s %d %s", buf1, buf2, buf3, buf4, &len, fname);
253 if ( len > 0 && strncmp(buf2,
"r-xp",strlen(
"r-xp")) == 0 ) {
254 s_linkedModules.push_back(fname);
260 return s_linkedModules;
GAUDI_API ImageHandle moduleHandle()
Handle to currently executed module.
GAUDI_API void setModuleHandle(ImageHandle handle)
Attach module handle.
GAUDI_API ModuleType moduleType()
Get type of the module.
GAUDI_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.
void * ImageHandle
Definition of an image handle.
GAUDI_API const std::string & exeName()
Name of the executable file running.
GAUDI_API ProcessHandle processHandle()
Handle to running process.
GAUDI_API const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
GAUDI_API const std::vector< std::string > linkedModules()
Vector of names of linked modules.
GAUDI_API ImageHandle exeHandle()
Handle to the executable file running.