27 #elif defined( __APPLE__ )
32 # define VCL_NAMESPACE Gaudi
42 #include <sys/times.h>
44 #if defined( __linux ) || defined( __APPLE__ )
46 # include <sys/utsname.h>
55 #endif // HPUX or not...
58 static const std::array<const char*, 2> SHLIB_SUFFIXES = {
".dylib",
".so" };
60 static const std::array<const char*, 1> SHLIB_SUFFIXES = {
".so" };
65 #if defined( __linux )
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 );
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 );
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__ )
192 if ( !( *pFunction ) ) {
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 );
290 using namespace Gaudi;
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>
372 int System::backTrace( [[maybe_unused]]
void** addresses, [[maybe_unused]]
const int depth ) {
376 int count = backtrace( addresses, depth );
377 return count > 0 ? count : 0;
379 #else // osx parts not implemented
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;
434 #else // not implemented for osx
441 return value.empty() ?
443 ::unsetenv(
name.c_str() ),
446 ::setenv(
name.c_str(), value.c_str(), overwrite );