13 #define SYSTEM_SYSTEM_CPP 29 #define strcasecmp _stricmp 30 #define strncasecmp _strnicmp 31 #define getpid _getpid 38 static const char* SHLIB_SUFFIX =
".dll";
39 #else // UNIX...: first the EGCS stuff, then the OS dependent includes 40 static const char* SHLIB_SUFFIX =
".so";
43 #include "sys/times.h" 48 #if defined(__linux) || defined(__APPLE__) 50 #include <sys/utsname.h> 65 # if __GNUC__ < 3 || \ 66 (__GNUC__ == 3 && (__GNUC_MINOR__ < 4 )) 68 # define __attribute__(x) 72 # define __attribute__(x) 84 #if defined(__linux) || defined(__APPLE__) 85 void *mh = ::dlopen(name.
length() == 0 ?
nullptr :
path, RTLD_LAZY | RTLD_GLOBAL);
88 shl_t mh = ::shl_load(name.
length() == 0 ? 0 :
path, BIND_IMMEDIATE | BIND_VERBOSE, 0);
89 HMODULE*
mod =
new HMODULE;
91 if ( 0 != ::shl_gethandle_r(mh, &mod->dsc) ) {
95 typedef void* (*___all)();
96 ___all _alloc = (___all)malloc;
97 mod->numSym = ::shl_getsymbols(mod->dsc.handle, TYPE_PROCEDURE, EXPORT_SYMBOLS, malloc, &mod->sym);
112 long len = strlen(SHLIB_SUFFIX);
116 if ((dllName.
length() != 0) &&
117 ::strncasecmp(dllName.
data()+dllName.
length()-len, SHLIB_SUFFIX, len) != 0) {
118 dllName += SHLIB_SUFFIX;
122 return doLoad(dllName, handle);
130 res = loadWithoutEnvironment(name, handle);
135 if (
getEnv(name, imgName) ) {
136 res = loadWithoutEnvironment(imgName, handle);
143 if (dllName.
find(
'/') == std::string::npos) {
144 #if defined(__linux) || defined(__APPLE__) 145 if (dllName.
compare(0, 3,
"lib") != 0)
146 dllName =
"lib" + dllName;
148 if (dllName.
find(SHLIB_SUFFIX) == std::string::npos)
149 dllName += SHLIB_SUFFIX;
152 res = loadWithoutEnvironment(dllName, handle);
155 #if defined(__linux) || defined(__APPLE__) 167 if ( !::FreeLibrary((HINSTANCE)handle) ) {
168 #elif defined(__linux) || defined(__APPLE__) 177 HMODULE*
mod = (HMODULE*)handle;
178 if ( 0 == ::shl_unload( mod->dsc.handle ) ) {
193 *pFunction = (
EntryPoint)::GetProcAddress((HINSTANCE)handle, name.
data());
194 if ( 0 == *pFunction ) {
198 #elif defined(__linux) 202 *pFunction = FuncPtrCast<EntryPoint>(::dlsym(handle, name.
c_str()));
204 if ( ! *pFunction ) {
210 #elif defined(__APPLE__) 217 if ( 0 == *pFunction ) {
225 HMODULE* mod = (HMODULE*)handle;
228 for (
int i = 0; i < mod->numSym; i++ ) {
229 long ll2 = strlen(mod->sym[i].name);
230 if ( 0 != ::strncmp(mod->sym[i].name, name.
c_str(), (ll1>ll2) ? ll1 : ll2)==0 ) {
248 return ::GetLastError();
251 return static_cast<unsigned long>(
static_cast<unsigned int>(errno));
265 LPVOID lpMessageBuffer;
267 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
270 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
271 (LPTSTR) &lpMessageBuffer,
274 errString = (
const char*)lpMessageBuffer;
276 ::LocalFree( lpMessageBuffer );
278 char *cerrString(
nullptr);
280 if ( error == 0xAFFEDEAD ) {
281 cerrString = (
char*)::dlerror();
282 if ( !cerrString ) cerrString = ::strerror(error);
284 cerrString = (
char *)
"Unknown error. No information found in strerror()!";
292 cerrString = ::strerror(error);
307 if ( ::strncmp(class_name,
"class ", 6) == 0 ) {
311 if ( ::strncmp(class_name,
"struct ", 7) == 0 ) {
318 while( (loc = tmp.
find(
"class ")) > 0 ) {
322 while( (loc = tmp.
find(
"struct ")) > 0 ) {
331 while ( (off=result.
find(
" *")) != std::string::npos ) {
335 while ( (off=result.
find(
" &")) != std::string::npos ) {
339 #elif defined(__linux) || defined(__APPLE__) 342 if (!realname)
return class_name;
343 #if _GLIBCXX_USE_CXX11_ABI 344 static const std::regex cxx11_string{
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > ?"};
361 unsigned long len = buffer.
size();
362 ::GetComputerName(buffer.
data(), &len);
364 ::gethostname(buffer.
data(), buffer.
size());
366 return { buffer.
data() };
383 if (uname(&ut) == 0) {
398 ut.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
401 ver << ut.dwMajorVersion <<
'.' << ut.dwMinorVersion;
405 if (uname(&ut) == 0) {
419 ::GetSystemInfo(&ut);
421 arch << ut.wProcessorArchitecture;
425 if (uname(&ut) == 0) {
446 if ( account ==
"" ) {
449 unsigned long buflen =
sizeof(buffer);
450 ::GetUserName(buffer, &buflen);
453 const char* acct = ::getlogin();
454 if ( !acct ) acct = ::getenv(
"LOGNAME");
455 if ( !acct ) acct = ::getenv(
"USER");
456 account = (acct) ? acct :
"Unknown";
474 if ( s_argvChars.
size() == 0 ) {
479 #pragma warning(push) 480 #pragma warning(disable:4996) 485 char *next, *tmp1, *tmp2;
486 for(LPTSTR cmd = ::GetCommandLine(); *cmd; cmd=next) {
487 memset(exe,0,
sizeof(exe));
488 while ( *cmd ==
' ' ) cmd++;
489 next=::strchr(cmd,
' ');
490 if ( !next ) next = cmd + strlen(cmd);
491 if ( (tmp1=::strchr(cmd,
'\"')) > 0 && tmp1 < next ) {
492 tmp2 = ::strchr(++tmp1,
'\"');
495 if ( cmd < tmp1 ) strncpy(exe, cmd, tmp1-cmd-1);
496 strncpy(&exe[strlen(exe)], tmp1, tmp2-tmp1-1);
501 s_argvStrings.
erase(s_argvStrings.
begin(), s_argvStrings.
end());
502 return s_argvStrings;
506 strncpy(exe, cmd, next-cmd);
512 #elif defined(__linux) || defined(__APPLE__) 513 sprintf(exe,
"/proc/%d/cmdline", ::getpid());
514 FILE *cmdLine = ::fopen(exe,
"r");
517 long len = fread(cmd,
sizeof(
char),
sizeof(cmd), cmdLine);
520 for (
char* token = cmd; token-cmd < len; token += strlen(token)+1 ) {
525 s_argvChars[0] = s_argvStrings[0].c_str();
531 return s_argvStrings;
540 return (
char**)&s_argvChars[0];
546 #pragma warning(disable:4996) 552 if ( (env = getenv(var)) !=
nullptr ) {
562 if ( (env = getenv(var)) !=
nullptr ) {
571 return getenv(var) !=
nullptr;
575 #if defined(__APPLE__) 577 #include "crt_externs.h" 581 # define environ _environ 582 #elif defined(__APPLE__) 583 static char **environ = *_NSGetEnviron();
586 for (
int i=0; environ[i] !=
nullptr; ++i) {
596 #include <execinfo.h> 605 int count = backtrace( addresses, depth );
606 return count > 0 ? count : 0;
608 #else // windows and osx parts not implemented 618 const int totalOffset = offset + 2;
619 const int totalDepth = depth + totalOffset;
625 for (
int i = totalOffset; i < count; ++i) {
626 void *addr =
nullptr;
651 if ( dladdr( addresses, &info ) && info.dli_fname
652 && info.dli_fname[0] !=
'\0' ) {
653 const char* symbol = info.dli_sname
654 && info.dli_sname[0] !=
'\0' ? info.dli_sname :
nullptr;
656 lib = info.dli_fname;
657 addr = info.dli_saddr;
662 fnc = (stat == 0) ? dmg.
get() : symbol;
671 #else // not implemented for windows and osx 682 return value.
empty() ?
684 ::unsetenv(name.
c_str()) , 0 :
686 ::setenv(name.
c_str(),value.
c_str(), overwrite);
689 if ( value.
empty() ) {
691 return ::_putenv((name+
"=").c_str());
694 if ( !getenv(name.
c_str()) || overwrite ) {
696 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)
unsigned long(* EntryPoint)(const unsigned long iid, void **ppvObject)
Definition of the "generic" DLL entry point function.
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.
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.
void *(* Creator)()
Definition of the "generic" DLL entry point function.
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.