14 #define SYSTEM_SYSTEM_CPP
26 #define strcasecmp _stricmp
27 #define strncasecmp _strnicmp
28 #define getpid _getpid
35 static const char* SHLIB_SUFFIX =
".dll";
36 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
37 static const char* SHLIB_SUFFIX =
".so";
40 #include "sys/times.h"
45 #if defined(__linux) || defined(__APPLE__)
47 #include <sys/utsname.h>
62 # if __GNUC__ < 3 || \
63 (__GNUC__ == 3 && (__GNUC_MINOR__ < 4 ))
65 # define __attribute__(x)
69 # define __attribute__(x)
72 static std::vector<std::string> s_argvStrings;
73 static std::vector<const char*> s_argvChars;
77 void* mh = ::LoadLibrary( name.length() == 0 ?
System::exeName().c_str() : name.c_str());
80 const char*
path = name.c_str();
81 #if defined(__linux) || defined(__APPLE__)
82 void *mh = ::dlopen(name.length() == 0 ? 0 :
path, RTLD_LAZY | RTLD_GLOBAL);
85 shl_t mh = ::shl_load(name.length() == 0 ? 0 :
path, BIND_IMMEDIATE | BIND_VERBOSE, 0);
86 HMODULE*
mod =
new HMODULE;
88 if ( 0 != ::shl_gethandle_r(mh, &mod->dsc) ) {
89 std::cout <<
"System::loadDynamicLib>" << ::strerror(
getLastError()) << std::endl;
92 typedef void* (*___all)();
93 ___all _alloc = (___all)malloc;
94 mod->numSym = ::shl_getsymbols(mod->dsc.handle, TYPE_PROCEDURE, EXPORT_SYMBOLS, malloc, &mod->sym);
100 if ( 0 == *handle ) {
106 static unsigned long loadWithoutEnvironment(
const std::string& name,
System::ImageHandle* handle) {
108 std::string dllName = name;
109 long len = strlen(SHLIB_SUFFIX);
113 if ((dllName.length() != 0) &&
114 ::strncasecmp(dllName.data()+dllName.length()-len, SHLIB_SUFFIX, len) != 0) {
115 dllName += SHLIB_SUFFIX;
119 return doLoad(dllName, handle);
126 if (name.length() == 0) {
127 res = loadWithoutEnvironment(name, handle);
132 if (
getEnv(name, imgName) ) {
133 res = loadWithoutEnvironment(imgName, handle);
136 std::string dllName = name;
140 if (dllName.find(
'/') == std::string::npos) {
141 #if defined(__linux) || defined(__APPLE__)
142 if (dllName.substr(0, 3) !=
"lib")
143 dllName =
"lib" + dllName;
145 if (dllName.find(SHLIB_SUFFIX) == std::string::npos)
146 dllName += SHLIB_SUFFIX;
149 res = loadWithoutEnvironment(dllName, handle);
152 #if defined(__linux) || defined(__APPLE__)
164 if ( !::FreeLibrary((HINSTANCE)handle) ) {
165 #elif defined(__linux) || defined(__APPLE__)
174 HMODULE*
mod = (HMODULE*)handle;
175 if ( 0 == ::shl_unload( mod->dsc.handle ) ) {
190 *pFunction = (
EntryPoint)::GetProcAddress((HINSTANCE)handle, name.data());
191 if ( 0 == *pFunction ) {
195 #elif defined(__linux)
197 *pFunction = (
EntryPoint)::dlsym(handle, name.c_str());
199 *pFunction = FuncPtrCast<EntryPoint>(::dlsym(handle, name.c_str()));
201 if ( 0 == *pFunction ) {
207 #elif defined(__APPLE__)
208 *pFunction = (
EntryPoint)::dlsym(handle, name.c_str());
211 std::string
sname =
"_" + name;
212 *pFunction = (
EntryPoint)::dlsym(handle, sname.c_str());
214 if ( 0 == *pFunction ) {
222 HMODULE* mod = (HMODULE*)handle;
224 long ll1 = name.length();
225 for (
int i = 0;
i < mod->numSym;
i++ ) {
226 long ll2 = strlen(mod->sym[
i].name);
227 if ( 0 != ::strncmp(mod->sym[
i].name, name.c_str(), (ll1>ll2) ? ll1 : ll2)==0 ) {
245 return ::GetLastError();
248 return static_cast<unsigned long>(
static_cast<unsigned int>(errno));
260 std::string errString =
"";
262 LPVOID lpMessageBuffer;
264 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
267 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
268 (LPTSTR) &lpMessageBuffer,
271 errString = (
const char*)lpMessageBuffer;
273 ::LocalFree( lpMessageBuffer );
277 if ( error == 0xAFFEDEAD ) {
278 cerrString = (
char*)::dlerror();
279 if ( 0 == cerrString ) {
280 cerrString = ::strerror(error);
282 if ( 0 == cerrString ) {
283 cerrString = (
char *)
"Unknown error. No information found in strerror()!";
286 errString = std::string(cerrString);
291 cerrString = ::strerror(error);
292 errString = std::string(cerrString);
306 if ( ::strncmp(class_name,
"class ", 6) == 0 ) {
310 if ( ::strncmp(class_name,
"struct ", 7) == 0 ) {
315 std::string tmp = class_name + off;
317 while( (loc = tmp.find(
"class ")) > 0 ) {
321 while( (loc = tmp.find(
"struct ")) > 0 ) {
330 while ( (off=result.find(
" *")) != std::string::npos ) {
331 result.replace(off, 2,
"*");
334 while ( (off=result.find(
" &")) != std::string::npos ) {
335 result.replace(off, 2,
"&");
338 #elif defined(__linux) || defined(__APPLE__)
339 if ( ::strlen(class_name) == 1 ) {
342 switch(class_name[0]) {
356 result =
"signed char";
359 result =
"unsigned char";
365 result =
"unsigned short";
371 result =
"unsigned int";
377 result =
"unsigned long";
380 result =
"long long";
383 result =
"unsigned long long";
389 result =
"unsigned __int128";
398 result =
"long double";
401 result =
"__float128";
411 realname = abi::__cxa_demangle(class_name, 0, 0, &status);
412 if (realname == 0)
return class_name;
416 std::string::size_type pos = result.find(
", ");
417 while( std::string::npos != pos ) {
418 result.replace( pos , 2 ,
"," ) ;
419 pos = result.find(
", ");
428 static std::string host =
"";
431 memset(buffer,0,
sizeof(buffer));
433 unsigned long len =
sizeof(buffer);
434 ::GetComputerName(buffer, &len);
436 ::gethostname(buffer,
sizeof(buffer));
445 static std::string osname =
"";
450 if (uname(&ut) == 0) {
462 static std::string osver =
"";
465 ut.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
467 std::ostringstream ver;
468 ver << ut.dwMajorVersion <<
'.' << ut.dwMinorVersion;
472 if (uname(&ut) == 0) {
483 static std::string mach =
"";
486 ::GetSystemInfo(&ut);
487 std::ostringstream arch;
488 arch << ut.wProcessorArchitecture;
492 if (uname(&ut) == 0) {
503 static std::string account =
"";
504 if ( account ==
"" ) {
507 unsigned long buflen =
sizeof(buffer);
508 ::GetUserName(buffer, &buflen);
511 const char* acct = ::getlogin();
512 if ( 0 == acct ) acct = ::getenv(
"LOGNAME");
513 if ( 0 == acct ) acct = ::getenv(
"USER");
514 account = (acct) ? acct :
"Unknown";
532 if ( s_argvChars.size() == 0 ) {
537 #pragma warning(push)
538 #pragma warning(disable:4996)
543 char *next, *tmp1, *tmp2;
544 for(LPTSTR cmd = ::GetCommandLine(); *cmd; cmd=next) {
545 memset(exe,0,
sizeof(exe));
546 while ( *cmd ==
' ' ) cmd++;
547 next=::strchr(cmd,
' ');
548 if ( !next ) next = cmd + strlen(cmd);
549 if ( (tmp1=::strchr(cmd,
'\"')) > 0 && tmp1 < next ) {
550 tmp2 = ::strchr(++tmp1,
'\"');
553 if ( cmd < tmp1 ) strncpy(exe, cmd, tmp1-cmd-1);
554 strncpy(&exe[strlen(exe)], tmp1, tmp2-tmp1-1);
557 std::cout <<
"Mismatched \" in command line arguments" << std::endl;
558 s_argvChars.erase(s_argvChars.begin(), s_argvChars.end());
559 s_argvStrings.erase(s_argvStrings.begin(), s_argvStrings.end());
560 return s_argvStrings;
564 strncpy(exe, cmd, next-cmd);
566 s_argvStrings.push_back(exe);
567 s_argvChars.push_back( s_argvStrings.back().c_str());
570 #elif defined(__linux) || defined(__APPLE__)
571 sprintf(exe,
"/proc/%d/cmdline", ::getpid());
572 FILE *cmdLine = ::fopen(exe,
"r");
575 long len = fread(cmd,
sizeof(
char),
sizeof(cmd), cmdLine);
578 for (
char* token = cmd; token-cmd < len; token += strlen(token)+1 ) {
579 s_argvStrings.push_back(token);
580 s_argvChars.push_back( s_argvStrings.back().c_str());
583 s_argvChars[0] = s_argvStrings[0].c_str();
589 return s_argvStrings;
598 return (
char**)&s_argvChars[0];
604 #pragma warning(disable:4996)
610 if ( (env = getenv(var)) != 0 ) {
620 if ( (env = getenv(var)) != 0 ) {
629 return getenv(var) != 0;
633 #if defined(__APPLE__)
635 #include "crt_externs.h"
639 # define environ _environ
640 #elif defined(__APPLE__)
641 static char **environ = *_NSGetEnviron();
643 std::vector<std::string> vars;
644 for (
int i=0; environ[
i] != 0; ++
i) {
645 vars.push_back(environ[
i]);
654 #include <execinfo.h>
663 int count = backtrace( addresses, depth );
670 #else // windows and osx parts not implemented
679 const int totalOffset = offset + 2;
680 const int totalDepth = depth + totalOffset;
682 std::string fnc, lib;
684 void** addresses = (
void**) malloc(totalDepth*
sizeof(
void *));
685 if ( addresses != 0 ){
687 for (
int i = totalOffset;
i < count; ++
i) {
691 std::ostringstream ost;
692 ost <<
"#" << std::setw(3) << std::setiosflags( std::ios::left ) << i-totalOffset+1;
693 ost << std::hex << addr << std::dec <<
" " << fnc <<
" [" << lib <<
"]" << std::endl;
717 if ( dladdr( addresses, &info ) && info.dli_fname
718 && info.dli_fname[0] !=
'\0' ) {
719 const char* symbol = info.dli_sname
720 && info.dli_sname[0] !=
'\0' ? info.dli_sname : 0;
722 lib = info.dli_fname;
723 addr = info.dli_saddr;
728 dmg = abi::__cxa_demangle(symbol,0,0,&stat);
729 fnc = (stat == 0) ? dmg : symbol;
739 #else // not implemented for windows and osx
750 return value.empty() ?
752 ::unsetenv(name.c_str()) , 0 :
754 ::setenv(name.c_str(),value.c_str(), overwrite);
757 if ( value.empty() ) {
759 return ::_putenv((name+
"=").c_str());
762 if ( !getenv(name.c_str()) || overwrite ) {
764 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 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.
GAUDI_API const std::string & osName()
OS name.
void *(* Creator)()
Definition of the "generic" DLL entry point function.
GAUDI_API unsigned long unloadDynamicLib(ImageHandle handle)
unload dynamic link library
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
GAUDI_API const std::string & hostName()
Host name.
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.
unsigned long(* EntryPoint)(const unsigned long iid, void **ppvObject)
Definition of the "generic" DLL entry point function.
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
GAUDI_API const std::string & machineType()
Machine type.
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.