13 #define SYSTEM_MODULEINFO_CPP 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 41 # include "sys/param.h" 42 # include "sys/times.h" 65 #elif defined( __linux ) || defined( __APPLE__ ) 81 char name[PATH_MAX] = {
"Unknown.module"};
90 # if defined( __linux ) || defined( __APPLE__ ) 95 if ( ::realpath( path, name ) ) module = name;
107 int loc = module.
rfind(
'.' ) + 1;
110 else if ( module[loc] ==
'e' || module[loc] ==
'E' )
113 else if ( module[loc] ==
'd' || module[loc] ==
'D' )
115 else if ( module[loc] ==
's' && module[loc + 1] ==
'o' )
126 static long pid = ::getpid();
128 static HANDLE hP = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid );
130 static void* hP = (
void*)pid;
138 if ( !ModuleHandle ) {
141 static HINSTANCE handle = 0;
143 if ( 0 == handle && _psApi ) {
144 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof( ModuleHandle ), &cbNeeded ) ) {}
147 #elif defined( __linux ) || defined( __APPLE__ ) 161 static HINSTANCE handle = 0;
163 if ( 0 == handle && _psApi ) {
164 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof( ModuleHandle ), &cbNeeded ) ) {}
169 #elif defined( __linux ) || defined( __APPLE__ ) 171 static Dl_info infoBuf, *info = &infoBuf;
173 void* handle = ::dlopen(
nullptr, RTLD_LAZY );
176 void*
func = ::dlsym( handle,
"main" );
179 if ( 0 != ::dladdr( func, &infoBuf ) ) {
195 if ( module.
length() == 0 ) {
196 char name[PATH_MAX] = {
"Unknown.module"};
203 #elif defined( __linux ) || defined( __APPLE__ ) 205 ::sprintf( cmd,
"/proc/%d/exe", ::getpid() );
207 if ( ::readlink( cmd, name,
sizeof( name ) ) >= 0 ) module =
name;
209 if ( ::realpath( ( (HMODULE*)
exeHandle() )->dsc.filename, name ) ) module =
name;
216 if ( s_linkedModules.
size() == 0 ) {
220 HINSTANCE handle[1024];
222 if ( _psApi.EnumProcessModules(
processHandle(), handle,
sizeof( handle ), &cbNeeded ) ) {
223 for (
size_t i = 0; i < cbNeeded /
sizeof( HANDLE ); i++ ) {
224 if ( 0 < _psApi.GetModuleFileNameExA(
processHandle(), handle[i], name,
sizeof( name ) ) ) {
230 #elif defined( __linux ) || defined( __APPLE__ ) 231 char ff[512], cmd[1024], fname[1024], buf1[64], buf2[64], buf3[64], buf4[64];
232 ::sprintf( ff,
"/proc/%d/maps", ::getpid() );
233 FILE*
maps = ::fopen( ff,
"r" );
234 while ( ::fgets( cmd,
sizeof( cmd ), maps ) ) {
236 sscanf( cmd,
"%s %s %s %s %d %s", buf1, buf2, buf3, buf4, &len, fname );
237 if ( len > 0 && strncmp( buf2,
"r-xp", strlen(
"r-xp" ) ) == 0 ) { s_linkedModules.
push_back( fname ); }
242 return s_linkedModules;
GAUDI_API ModuleType moduleType()
Get type of the module.
GAUDI_API void setModuleHandle(ImageHandle handle)
Attach module handle.
GAUDI_API const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
GAUDI_API ProcessHandle processHandle()
Handle to running process.
void * ImageHandle
Definition of an image handle.
GAUDI_API const std::string & exeName()
Name of the executable file running.
GAUDI_API ImageHandle moduleHandle()
Handle to currently executed module.
GAUDI_API ImageHandle exeHandle()
Handle to the executable file running.
GAUDI_API const std::vector< std::string > linkedModules()
Vector of names of linked modules.
GAUDI_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.