13 #define SYSTEM_SYSTEM_CPP 27 #define VCL_NAMESPACE Gaudi 32 #define strcasecmp _stricmp 33 #define strncasecmp _strnicmp 34 #define getpid _getpid 42 #else // UNIX...: first the EGCS stuff, then the OS dependent includes 44 #include "sys/times.h" 50 #if defined( __linux ) || defined( __APPLE__ ) 52 #include <sys/utsname.h> 61 #endif // HPUX or not... 69 #endif // Windows or Unix... 73 #if __GNUC__ < 3 || ( __GNUC__ == 3 && ( __GNUC_MINOR__ < 4 ) ) 75 #define __attribute__( x ) 79 #define __attribute__( x ) 92 #if defined( __linux ) || defined( __APPLE__ ) 93 void* mh = ::dlopen( name.
length() == 0 ?
nullptr :
path, RTLD_LAZY | RTLD_GLOBAL );
96 shl_t mh = ::shl_load( name.
length() == 0 ? 0 :
path, BIND_IMMEDIATE | BIND_VERBOSE, 0 );
97 HMODULE*
mod =
new HMODULE;
99 if ( 0 != ::shl_gethandle_r( mh, &mod->dsc ) ) {
102 typedef void* ( *___all )();
103 ___all _alloc = (___all)malloc;
104 mod->numSym = ::shl_getsymbols( mod->dsc.handle, TYPE_PROCEDURE, EXPORT_SYMBOLS, malloc, &mod->sym );
120 if ( name.
length() == 0 ) {
121 return doLoad( name, handle );
127 bool hasShlibSuffix =
false;
128 for (
const char* suffix : SHLIB_SUFFIXES ) {
129 const size_t len = strlen( suffix );
130 if ( dllName.
compare( dllName.
length() - len, len, suffix ) == 0 ) {
131 hasShlibSuffix =
true;
138 if ( !hasShlibSuffix ) {
139 dllName += SHLIB_SUFFIXES[0];
143 return doLoad( dllName, handle );
151 if ( name.
length() == 0 ) {
152 res = loadWithoutEnvironment( name, handle );
157 if (
getEnv( name, imgName ) ) {
158 res = loadWithoutEnvironment( imgName, handle );
164 #if defined( __linux ) || defined( __APPLE__ ) 165 if ( ( dllName.
find(
'/' ) == std::string::npos ) && ( dllName.
compare( 0, 3,
"lib" ) != 0 ) ) {
166 dllName =
"lib" + dllName;
171 for (
const char* suffix : SHLIB_SUFFIXES ) {
174 const size_t len = strlen( suffix );
175 if ( dllName.
compare( dllName.
length() - len, len, suffix ) != 0 ) {
179 res = loadWithoutEnvironment( libName, handle );
187 #if defined( __linux ) || defined( __APPLE__ ) 200 if ( !::FreeLibrary( (HINSTANCE)handle ) ) {
201 #elif defined( __linux ) || defined( __APPLE__ ) 210 HMODULE*
mod = (HMODULE*)handle;
211 if ( 0 == ::shl_unload( mod->dsc.handle ) ) {
226 *pFunction = (
EntryPoint )::GetProcAddress( (HINSTANCE)handle, name.
data() );
227 if ( 0 == *pFunction ) {
231 #elif defined( __linux ) 235 *pFunction = FuncPtrCast<EntryPoint>(::dlsym( handle, name.
c_str() ) );
243 #elif defined( __APPLE__ ) 245 if ( !( *pFunction ) ) {
250 if ( 0 == *pFunction ) {
258 HMODULE* mod = (HMODULE*)handle;
261 for (
int i = 0; i < mod->numSym; i++ ) {
262 long ll2 = strlen( mod->sym[i].name );
263 if ( 0 != ::strncmp( mod->sym[i].name, name.
c_str(), ( ll1 > ll2 ) ? ll1 : ll2 ) == 0 ) {
283 return ::GetLastError();
286 return static_cast<unsigned long>(
static_cast<unsigned int>( errno ) );
302 LPVOID lpMessageBuffer;
303 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
304 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
305 (LPTSTR)&lpMessageBuffer, 0, NULL );
306 errString = (
const char*)lpMessageBuffer;
308 ::LocalFree( lpMessageBuffer );
310 char* cerrString(
nullptr );
312 if ( error == 0xAFFEDEAD ) {
313 cerrString = (
char*)::dlerror();
314 if ( !cerrString ) cerrString = ::strerror( error );
316 cerrString = (
char*)
"Unknown error. No information found in strerror()!";
322 cerrString = ::strerror( error );
336 if (::strncmp( class_name,
"class ", 6 ) == 0 ) {
340 if (::strncmp( class_name,
"struct ", 7 ) == 0 ) {
347 while ( ( loc = tmp.
find(
"class " ) ) > 0 ) {
351 while ( ( loc = tmp.
find(
"struct " ) ) > 0 ) {
359 while ( ( off = result.
find(
" *" ) ) != std::string::npos ) {
363 while ( ( off = result.
find(
" &" ) ) != std::string::npos ) {
367 #elif defined( __linux ) || defined( __APPLE__ ) 370 abi::__cxa_demangle( class_name,
nullptr,
nullptr, &status ),
std::free );
371 if ( !realname )
return class_name;
372 #if _GLIBCXX_USE_CXX11_ABI 374 "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?"};
393 unsigned long len = buffer.
size();
394 ::GetComputerName( buffer.
data(), &len );
396 ::gethostname( buffer.
data(), buffer.
size() );
398 return {buffer.
data()};
417 if ( uname( &ut ) == 0 ) {
432 ut.dwOSVersionInfoSize =
sizeof( OSVERSIONINFO );
433 ::GetVersionEx( &ut );
435 ver << ut.dwMajorVersion <<
'.' << ut.dwMinorVersion;
439 if ( uname( &ut ) == 0 ) {
454 ::GetSystemInfo( &ut );
456 arch << ut.wProcessorArchitecture;
460 if ( uname( &ut ) == 0 ) {
471 using namespace Gaudi;
479 if ( account ==
"" ) {
482 unsigned long buflen =
sizeof( buffer );
483 ::GetUserName( buffer, &buflen );
486 const char* acct = ::getlogin();
487 if ( !acct ) acct = ::getenv(
"LOGNAME" );
488 if ( !acct ) acct = ::getenv(
"USER" );
489 account = ( acct ) ? acct :
"Unknown";
504 if ( s_argvChars.
size() == 0 ) {
509 #pragma warning( push ) 510 #pragma warning( disable : 4996 ) 515 char *next, *tmp1, *tmp2;
516 for ( LPTSTR cmd = ::GetCommandLine(); *cmd; cmd = next ) {
517 memset( exe, 0,
sizeof( exe ) );
518 while ( *cmd ==
' ' ) cmd++;
519 next = ::strchr( cmd,
' ' );
520 if ( !next ) next = cmd + strlen( cmd );
521 if ( ( tmp1 = ::strchr( cmd,
'\"' ) ) > 0 && tmp1 < next ) {
522 tmp2 = ::strchr( ++tmp1,
'\"' );
525 if ( cmd < tmp1 ) strncpy( exe, cmd, tmp1 - cmd - 1 );
526 strncpy( &exe[strlen( exe )], tmp1, tmp2 - tmp1 - 1 );
530 s_argvStrings.
erase( s_argvStrings.
begin(), s_argvStrings.
end() );
531 return s_argvStrings;
534 strncpy( exe, cmd, next - cmd );
539 #pragma warning( pop ) 540 #elif defined( __linux ) || defined( __APPLE__ ) 541 sprintf( exe,
"/proc/%d/cmdline", ::getpid() );
542 FILE* cmdLine = ::fopen( exe,
"r" );
545 long len = fread( cmd,
sizeof(
char ),
sizeof( cmd ), cmdLine );
548 for (
char* token = cmd; token - cmd < len; token += strlen( token ) + 1 ) {
553 s_argvChars[0] = s_argvStrings[0].c_str();
559 return s_argvStrings;
566 if ( s_argvChars.
empty() ) {
571 return (
char**)&s_argvChars[0];
577 #pragma warning( disable : 4996 ) 584 if ( ( env = getenv( var ) ) !=
nullptr ) {
595 if ( ( env = getenv( var ) ) !=
nullptr ) {
606 #if defined( __APPLE__ ) 608 #include "crt_externs.h" 612 #if defined( _WIN32 ) 613 #define environ _environ 614 #elif defined( __APPLE__ ) 615 static char** environ = *_NSGetEnviron();
618 for (
int i = 0; environ[i] !=
nullptr; ++i ) {
628 #include <execinfo.h> 636 int count = backtrace( addresses, depth );
637 return count > 0 ? count : 0;
639 #else // windows and osx parts not implemented 648 const int totalOffset = offset + 2;
649 const int totalDepth = depth + totalOffset;
655 for (
int i = totalOffset; i < count; ++i ) {
656 void* addr =
nullptr;
679 if ( dladdr( addresses, &info ) && info.dli_fname && info.dli_fname[0] !=
'\0' ) {
680 const char* symbol = info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname :
nullptr;
682 lib = info.dli_fname;
683 addr = info.dli_saddr;
689 fnc = ( stat == 0 ) ? dmg.
get() : symbol;
698 #else // not implemented for windows and osx 708 return value.
empty() ?
710 ::unsetenv( name.
c_str() ),
713 ::setenv( name.
c_str(), value.
c_str(), overwrite );
716 if ( value.
empty() ) {
718 return ::_putenv( ( name +
"=" ).c_str() );
720 if ( !getenv( name.
c_str() ) || overwrite ) {
722 return ::_putenv( ( name +
"=" + value ).c_str() );
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
GAUDI_API long argc()
Number of arguments passed to the commandline (==numCmdLineArgs()); just to match argv call...
GAUDI_API char ** argv()
char** command line arguments including executable name as arg[0]; You may not modify them! ...
GAUDI_API const std::string getErrorString(unsigned long error)
Retrieve error code as string for a given error.
GAUDI_API int setEnv(const std::string &name, const std::string &value, int overwrite=1)
Set an environment variables.
GAUDI_API unsigned long getLastError()
Get last system known error.
GAUDI_API unsigned long getProcedureByName(ImageHandle handle, const std::string &name, EntryPoint *pFunction)
Get a specific function defined in the DLL.
GAUDI_API bool getStackLevel(void *addresses, void *&addr, std::string &fnc, std::string &lib)
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
GAUDI_API int instructionsetLevel()
Instruction Set "Level".
GAUDI_API const std::string & accountName()
User login name.
void * ImageHandle
Definition of an image handle.
GAUDI_API int backTrace(void **addresses, const int depth)
GAUDI_API long numCmdLineArgs()
Number of arguments passed to the commandline.
GAUDI_API const std::string & exeName()
Name of the executable file running.
T regex_replace(T...args)
GAUDI_API const std::string & osName()
OS name.
int instrset_detect(void)
GAUDI_API unsigned long unloadDynamicLib(ImageHandle handle)
unload dynamic link library
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
GAUDI_API const std::string & hostName()
Host name.
void *(* Creator)()
Definition of the "generic" DLL entry point function.
GAUDI_API const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
GAUDI_API const std::string & osVersion()
OS version.
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
GAUDI_API const std::string & machineType()
Machine type.
Helper functions to set/get the application return code.
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
unsigned long(* EntryPoint)(const unsigned long iid, void **ppvObject)
Definition of the "generic" DLL entry point function.