23 #define SYSTEM_MODULEINFO_CPP
37 # define strcasecmp _stricmp
38 # define strncasecmp _strnicmp
42 static PsApiFunctions _psApi;
43 # define getpid _getpid
47 # define PATH_MAX 1024
49 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
55 # include <sys/param.h>
56 # include <sys/times.h>
75 #elif defined( __linux ) || defined( __APPLE__ )
80 module =
mod.substr( 0,
mod.rfind(
'.' ) );
91 char name[PATH_MAX] = {
"Unknown.module" };
100 # if defined( __linux ) || defined( __APPLE__ )
117 size_t loc = module.
rfind(
'.' ) + 1;
120 else if ( module[loc] ==
'e' || module[loc] ==
'E' )
123 else if ( module[loc] ==
'd' || module[loc] ==
'D' )
125 else if ( module[loc] ==
's' && module[loc + 1] ==
'o' )
136 static long pid = ::getpid();
138 static HANDLE hP = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid );
140 static void* hP = (
void*)pid;
148 if ( !ModuleHandle ) {
151 static HINSTANCE handle = 0;
153 if ( 0 == handle && _psApi ) {
154 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof( ModuleHandle ), &cbNeeded ) ) {}
157 #elif defined( __linux ) || defined( __APPLE__ )
171 static HINSTANCE handle = 0;
173 if ( 0 == handle && _psApi ) {
174 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof( ModuleHandle ), &cbNeeded ) ) {}
179 #elif defined( __linux ) || defined( __APPLE__ )
181 static Dl_info infoBuf, *info = &infoBuf;
183 void* handle = ::dlopen(
nullptr, RTLD_LAZY );
186 void*
func = ::dlsym( handle,
"main" );
189 if ( 0 != ::dladdr(
func, &infoBuf ) ) {
205 if ( module.
length() == 0 ) {
206 char name[PATH_MAX] = {
"Unknown.module" };
213 #elif defined( __linux ) || defined( __APPLE__ )
215 ::sprintf( cmd,
"/proc/%d/exe", ::getpid() );
217 if ( ::readlink( cmd,
name,
sizeof(
name ) ) >= 0 ) module =
name;
219 if ( ::realpath( ( (HMODULE*)
exeHandle() )->dsc.filename,
name ) ) module =
name;
226 if ( s_linkedModules.
size() == 0 ) {
230 HINSTANCE handle[1024];
232 if ( _psApi.EnumProcessModules(
processHandle(), handle,
sizeof( handle ), &cbNeeded ) ) {
233 for (
size_t i = 0; i < cbNeeded /
sizeof( HANDLE ); i++ ) {
240 #elif defined( __linux ) || defined( __APPLE__ )
241 char ff[512], cmd[1024], fname[1024], buf1[64], buf2[64], buf3[64], buf4[64];
242 ::sprintf( ff,
"/proc/%d/maps", ::getpid() );
243 FILE*
maps = ::fopen( ff,
"r" );
244 while ( ::fgets( cmd,
sizeof( cmd ),
maps ) ) {
246 sscanf( cmd,
"%s %s %s %s %d %s", buf1, buf2, buf3, buf4, &len, fname );
247 if ( len > 0 && strncmp( buf2,
"r-xp", strlen(
"r-xp" ) ) == 0 ) { s_linkedModules.
push_back( fname ); }
252 return s_linkedModules;