19 # define strcasecmp _stricmp
20 # define strncasecmp _strnicmp
24 static PsApiFunctions _psApi;
25 # define getpid _getpid
29 # define PATH_MAX 1024
31 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
36 # include <sys/param.h>
37 # include <sys/times.h>
42 static std::vector<std::string> s_linkedModules;
46 static std::string module(
"" );
56 #elif defined( __linux ) || defined( __APPLE__ )
57 std::string
mod = ::basename( (
char*)( (Dl_info*)
moduleHandle() )->dli_fname );
59 std::string
mod = ::basename( ( (HMODULE*)
moduleHandle() )->dsc.filename );
61 module =
mod.substr( 0,
mod.rfind(
'.' ) );
69 static std::string module(
"" );
72 char name[PATH_MAX] = {
"Unknown.module" };
81 # if defined( __linux ) || defined( __APPLE__ )
98 size_t loc = module.rfind(
'.' ) + 1;
101 else if ( module[loc] ==
'e' || module[loc] ==
'E' )
104 else if ( module[loc] ==
'd' || module[loc] ==
'D' )
106 else if ( module[loc] ==
's' && module[loc + 1] ==
'o' )
117 static long pid = ::getpid();
119 static HANDLE hP = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid );
121 static void* hP = (
void*)pid;
129 if ( !ModuleHandle ) {
132 static HINSTANCE handle = 0;
134 if ( 0 == handle && _psApi ) {
135 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof( ModuleHandle ), &cbNeeded ) ) {}
138 #elif defined( __linux ) || defined( __APPLE__ )
152 static HINSTANCE handle = 0;
154 if ( 0 == handle && _psApi ) {
155 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof( ModuleHandle ), &cbNeeded ) ) {}
160 #elif defined( __linux ) || defined( __APPLE__ )
162 static Dl_info infoBuf, *info = &infoBuf;
164 void* handle = ::dlopen(
nullptr, RTLD_LAZY );
167 void*
func = ::dlsym( handle,
"main" );
170 if ( 0 != ::dladdr(
func, &infoBuf ) ) {
186 static std::string module(
"" );
187 if ( module.length() == 0 ) {
188 char name[PATH_MAX] = {
"Unknown.module" };
195 #elif defined( __linux ) || defined( __APPLE__ )
197 ::sprintf( cmd,
"/proc/%d/exe", ::getpid() );
199 if ( ::readlink( cmd,
name,
sizeof(
name ) ) >= 0 ) module =
name;
201 if ( ::realpath( ( (HMODULE*)
exeHandle() )->dsc.filename,
name ) ) module =
name;
208 if ( s_linkedModules.size() == 0 ) {
212 HINSTANCE handle[1024];
214 if ( _psApi.EnumProcessModules(
processHandle(), handle,
sizeof( handle ), &cbNeeded ) ) {
215 for (
size_t i = 0; i < cbNeeded /
sizeof( HANDLE ); i++ ) {
217 s_linkedModules.push_back(
name );
222 #elif defined( __linux ) || defined( __APPLE__ )
223 char ff[512], cmd[1024], fname[1024], buf1[64], buf2[64], buf3[64], buf4[64];
224 ::sprintf( ff,
"/proc/%d/maps", ::getpid() );
225 FILE* maps = ::fopen( ff,
"r" );
226 while ( ::fgets( cmd,
sizeof( cmd ), maps ) ) {
228 sscanf( cmd,
"%s %s %s %s %d %s", buf1, buf2, buf3, buf4, &len, fname );
229 if ( len > 0 && strncmp( buf2,
"r-xp", strlen(
"r-xp" ) ) == 0 ) { s_linkedModules.push_back( fname ); }
234 return s_linkedModules;