27#elif defined( __APPLE__ )
32# define VCL_NAMESPACE Gaudi
44#if defined( __linux ) || defined( __APPLE__ )
46# include <sys/utsname.h>
58static const std::array<const char*, 2> SHLIB_SUFFIXES = {
".dylib",
".so" };
60static const std::array<const char*, 1> SHLIB_SUFFIXES = {
".so" };
66 void* mh = ::dlopen(
name.length() == 0 ?
nullptr : path, RTLD_LAZY | RTLD_GLOBAL );
68#elif defined( __APPLE__ )
69 void* mh = ::dlopen(
name.length() == 0 ?
nullptr : path, RTLD_LAZY | RTLD_GLOBAL );
72 shl_t mh = ::shl_load(
name.length() == 0 ? 0 : path, BIND_IMMEDIATE | BIND_VERBOSE, 0 );
73 HMODULE*
mod =
new HMODULE;
75 if ( 0 != ::shl_gethandle_r( mh, &
mod->dsc ) ) {
76 std::cout <<
"System::loadDynamicLib>" << ::strerror(
getLastError() ) << std::endl;
78 typedef void* ( *___all )();
79 ___all _alloc = (___all)malloc;
80 mod->numSym = ::shl_getsymbols(
mod->dsc.handle, TYPE_PROCEDURE, EXPORT_SYMBOLS, malloc, &
mod->sym );
89static unsigned long loadWithoutEnvironment(
const std::string& name,
System::ImageHandle* handle ) {
92 if ( name.length() == 0 ) {
return doLoad( name, handle ); }
96 std::string dllName =
name;
97 bool hasShlibSuffix =
false;
98 for (
const char* suffix : SHLIB_SUFFIXES ) {
99 const size_t len = strlen( suffix );
100 if ( dllName.compare( dllName.length() - len, len, suffix ) == 0 ) {
101 hasShlibSuffix =
true;
108 if ( !hasShlibSuffix ) { dllName += SHLIB_SUFFIXES[0]; }
111 return doLoad( dllName, handle );
116 unsigned long res = 0;
118 if ( name.length() == 0 ) {
119 res = loadWithoutEnvironment( name, handle );
124 if (
getEnv( name, imgName ) ) {
125 res = loadWithoutEnvironment( imgName, handle );
128 std::string dllName = name;
131#if defined( __linux ) || defined( __APPLE__ )
132 if ( ( dllName.find(
'/' ) == std::string::npos ) && ( dllName.compare( 0, 3,
"lib" ) != 0 ) ) {
133 dllName =
"lib" + dllName;
138 for (
const char* suffix : SHLIB_SUFFIXES ) {
140 std::string libName = dllName;
141 const size_t len = strlen( suffix );
142 if ( dllName.compare( dllName.length() - len, len, suffix ) != 0 ) { libName += suffix; }
144 res = loadWithoutEnvironment( libName, handle );
146 if ( res == 1 ) {
break; }
150#if defined( __linux ) || defined( __APPLE__ )
161#if defined( __linux ) || defined( __APPLE__ )
168 HMODULE* mod = (HMODULE*)handle;
169 if ( 0 == ::shl_unload( mod->dsc.handle ) ) {
182#if defined( __linux )
183 *pFunction =
reinterpret_cast<EntryPoint>( ::dlsym( handle, name.c_str() ) );
190#elif defined( __APPLE__ )
191 *pFunction = (
EntryPoint )::dlsym( handle, name.c_str() );
192 if ( !( *pFunction ) ) {
194 std::string sname =
"_" + name;
195 *pFunction = (
EntryPoint )::dlsym( handle, sname.c_str() );
197 if ( 0 == *pFunction ) {
205 HMODULE* mod = (HMODULE*)handle;
207 long ll1 = name.length();
208 for (
int i = 0; i < mod->numSym; i++ ) {
209 long ll2 = strlen( mod->sym[i].name );
210 if ( 0 != ::strncmp( mod->sym[i].name, name.c_str(), ( ll1 > ll2 ) ? ll1 : ll2 ) == 0 ) {
230 return static_cast<unsigned long>(
static_cast<unsigned int>( errno ) );
241 std::string errString =
"";
242 char* cerrString(
nullptr );
244 if ( error == 0xAFFEDEAD ) {
245 cerrString = ::dlerror();
246 if ( !cerrString ) cerrString = ::strerror( error );
248 cerrString = (
char*)
"Unknown error. No information found in strerror()!";
250 errString = std::string( cerrString );
254 cerrString = ::strerror( error );
255 errString = std::string( cerrString );
262const std::string
System::typeinfoName(
const char* class_name ) {
return Platform::typeinfoName( class_name ); }
266 static const std::string host = Platform::hostName();
272 static const std::string osname = Platform::osName();
278 static const std::string osver = Platform::osVersion();
284 static const std::string mach = Platform::machineType();
290 using namespace Gaudi;
299 static const std::string account = Platform::accountName();
311 static const std::vector<std::string> args = Platform::cmdLineArgs();
317 auto helperFunc = [](
const std::vector<std::string>& args ) -> std::vector<const char*> {
318 std::vector<const char*> result;
319 std::transform( args.begin(), args.end(), std::back_inserter( result ),
320 [](
const std::string& s ) { return s.c_str(); } );
323 static const std::vector<const char*> args = helperFunc(
cmdLineArgs() );
325 return (
char**)&( args[0] );
331 if ( ( env = getenv( var ) ) !=
nullptr ) {
341 if ( ( env = getenv( var ) ) !=
nullptr ) {
352#if defined( __APPLE__ )
354# include <crt_externs.h>
357#if defined( __APPLE__ )
358 static char** environ = *_NSGetEnviron();
360 std::vector<std::string> vars;
361 for (
int i = 0; environ[i] !=
nullptr; ++i ) { vars.push_back( environ[i] ); }
369# include <execinfo.h>
376 int count = backtrace( addresses, depth );
377 return count > 0 ? count : 0;
387 const size_t totalOffset = offset + 2;
388 const size_t totalDepth = depth + totalOffset;
390 std::string fnc, lib;
392 std::vector<void*> addresses( totalDepth,
nullptr );
394 for (
size_t i = totalOffset; i < count; ++i ) {
395 void* addr =
nullptr;
398 std::ostringstream ost;
399 ost <<
"#" << std::setw( 3 ) << std::setiosflags( std::ios::left ) << i - totalOffset + 1;
400 ost << std::hex << addr << std::dec <<
" " << fnc <<
" [" << lib <<
"]" << std::endl;
405 }
catch (
const std::bad_alloc& e ) {
return false; }
409 [[maybe_unused]] std::string& fnc, [[maybe_unused]] std::string& lib ) {
415 if ( dladdr( addresses, &info ) && info.dli_fname && info.dli_fname[0] !=
'\0' ) {
416 const char* symbol = info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname :
nullptr;
418 lib = info.dli_fname;
419 addr = info.dli_saddr;
424 std::unique_ptr<char, decltype( free )*>( abi::__cxa_demangle( symbol,
nullptr,
nullptr, &stat ), std::free );
425 fnc = ( stat == 0 ) ? dmg.get() : symbol;
440int System::setEnv(
const std::string& name,
const std::string& value,
int overwrite ) {
441 return value.empty() ?
443 ::unsetenv( name.c_str() ),
446 ::setenv( name.c_str(), value.c_str(), overwrite );
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
int instrset_detect(void)
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
GAUDI_API bool getStackLevel(void *addresses, void *&addr, std::string &fnc, std::string &lib)
GAUDI_API const std::string & osName()
OS name.
GAUDI_API int backTrace(void **addresses, const int depth)
void *(* Creator)()
Definition of the "generic" DLL entry point function.
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
unsigned long(* EntryPoint)(const unsigned long iid, void **ppvObject)
Definition of the "generic" DLL entry point function.
GAUDI_API const std::string & machineType()
Machine type.
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
GAUDI_API long numCmdLineArgs()
Number of arguments passed to the commandline.
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
GAUDI_API unsigned long unloadDynamicLib(ImageHandle handle)
unload dynamic link library
GAUDI_API unsigned long getProcedureByName(ImageHandle handle, const std::string &name, EntryPoint *pFunction)
Get a specific function defined in the DLL.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
void * ImageHandle
Definition of an image handle.
GAUDI_API std::vector< std::string > getEnv()
get all environment variables
GAUDI_API const std::string & hostName()
Host name.
GAUDI_API const std::string & accountName()
User login name.
GAUDI_API unsigned long getLastError()
Get last system known error.
GAUDI_API long argc()
Number of arguments passed to the commandline (==numCmdLineArgs()); just to match argv call....
GAUDI_API const std::string getErrorString(unsigned long error)
Retrieve error code as string for a given error.
GAUDI_API const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
GAUDI_API int setEnv(const std::string &name, const std::string &value, int overwrite=1)
Set an environment variables.
GAUDI_API char ** argv()
char** command line arguments including executable name as arg[0]; You may not modify them!
GAUDI_API const std::string & osVersion()
OS version.
GAUDI_API int instructionsetLevel()
Instruction Set "Level".