26 #include <sys/utsname.h>
59 unsigned long doLoad(
const string& name,
ImageHandle* handle) {
60 void *mh = ::dlopen(name.
length() == 0 ? 0 : name.
c_str(), RTLD_LAZY | RTLD_GLOBAL);
68 unsigned long loadWithoutEnvironment(
const string& name,
71 string dll_name = name;
72 size_t dll_len = dll_name.
size();
76 if (dll_len >= suf_len &&
82 return doLoad(dll_name, handle);
93 res = loadWithoutEnvironment(name, handle);
98 if (
getEnv(name, imgName)) {
99 res = loadWithoutEnvironment(imgName, handle);
102 string dllName = name;
103 dllName =
"lib" + dllName;
106 res = loadWithoutEnvironment(dllName, handle);
109 errno =
static_cast<int>(0xAFFEDEAD);
127 #if defined(__linux__)
128 *pFunction = FuncPtrCast<EntryPoint>(::dlsym(handle, name.
c_str()));
129 if (0 == *pFunction) {
130 errno =
static_cast<int>(0xAFFEDEAD);
134 #elif defined(__APPLE__)
136 if (not *pFunction) {
138 string sname =
"_" + name;
141 if ( 0 == *pFunction ) {
142 errno =
static_cast<int>(0xAFFEDEAD);
159 return static_cast<unsigned long>(
static_cast<unsigned int>(errno));
170 string errString =
"";
173 if (error == 0xAFFEDEAD) {
174 cerrString =
reinterpret_cast<char*
>(::dlerror());
175 if (0 == cerrString) {
178 if (0 == cerrString) {
180 const_cast<char *
> (
"Unknown error. No information found in strerror()!");
182 errString =
string(cerrString);
186 errString =
string(cerrString);
197 if (strnlen(class_name, 1024) == 1) {
200 switch (class_name[0]) {
214 result =
"signed char";
217 result =
"unsigned char";
223 result =
"unsigned short";
229 result =
"unsigned int";
235 result =
"unsigned long";
238 result =
"long long";
241 result =
"unsigned long long";
247 result =
"unsigned __int128";
256 result =
"long double";
259 result =
"__float128";
268 realname = abi::__cxa_demangle(class_name, 0, 0, &status);
275 string::size_type pos = result.
find(
", ");
276 while (string::npos != pos) {
277 result.
replace(pos,
static_cast<string::size_type
>(2),
",");
278 pos = result.
find(
", ");
286 static string host {};
297 static string osname =
"";
299 if (::uname(&ut) == 0) {
309 static string osver =
"UNKNOWN";
312 if (uname(&ut) == 0) {
321 static string mach =
"UNKNOWN";
324 if (uname(&ut) == 0) {
341 bool getEnv(
const string& var,
string& value) {
345 char* env = ::getenv(var.
c_str());
357 return getEnv(var, result);
361 #if defined(__APPLE__)
363 #include "crt_externs.h"
366 #if defined(__APPLE__)
367 static char **environ = *_NSGetEnviron();
370 for (
int i = 0; environ[i] != 0; ++i) {
377 int setEnv(
const string& name,
const string& value,
bool overwrite) {
384 return ::setenv(name.
c_str(), value.
c_str(), over);
389 return ::unsetenv(name.
c_str());
399 int count = ::backtrace(addresses.get(), depth);
412 const int total_depth = depth + total_offset;
417 if (addresses !=
nullptr) {
419 int count =
backTrace(addresses, total_depth);
421 for (
int i=total_offset; i < count; ++i) {
428 trace.emplace_back(ost.
str());
442 if (::dladdr(addresses, &info) && info.dli_fname
443 && info.dli_fname[0] !=
'\0') {
445 info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname : 0;
447 lib = info.dli_fname;
448 addr = info.dli_saddr;
452 std::unique_ptr<char, decltype(free)*> dmg(abi::__cxa_demangle(symbol, 0, 0, &stat), free);
453 fnc =
string((stat == 0) ? dmg.
get() : symbol);
defines a Small helper function that allows the cast from void * to function pointer
OS specific details to access at run-time the module configuration of the process.
This file is intended to iron out all the differences between systems (currently Linux and MacOSX)
Macro to silence unused variables warnings from the compiler.
T emplace_back(T... args)
ELEMENTS_API bool isEnvSet(const std::string &var)
Check if an environment variable is set or not.
const std::string SHLIB_SUFFIX
alias for LIB_SUFFIX
unsigned long(*)(const unsigned long iid, void **ppvObject) EntryPoint
Definition of the "generic" DLL entry point function.
ELEMENTS_API const std::string getErrorString(unsigned long error)
Retrieve error code as string for a given error.
void * ImageHandle
Definition of an image handle.
ELEMENTS_API int setEnv(const std::string &name, const std::string &value, bool overwrite=true)
set an environment variables.
ELEMENTS_API bool getStackLevel(ELEMENTS_UNUSED void *addresses, ELEMENTS_UNUSED void *&addr, ELEMENTS_UNUSED std::string &fnc, ELEMENTS_UNUSED std::string &lib)
ELEMENTS_API unsigned long unloadDynamicLib(ImageHandle handle)
unload dynamic link library
ELEMENTS_API unsigned long getLastError()
Get last system known error.
ELEMENTS_API const std::string & osName()
OS name.
ELEMENTS_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
ELEMENTS_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
ELEMENTS_API int unSetEnv(const std::string &name)
Simple wrap around unsetenv for strings.
ELEMENTS_API unsigned long getProcedureByName(ImageHandle handle, const std::string &name, EntryPoint *pFunction)
Get a specific function defined in the DLL.
ELEMENTS_API std::string getEnv(const std::string &var)
get a particular environment variable
ELEMENTS_API const std::string & osVersion()
OS version.
ELEMENTS_API const std::string getLastErrorString()
Get last system error as string.
ELEMENTS_API const std::string & machineType()
Machine type.
void *(*)() Creator
Definition of the "generic" DLL entry point function.
ELEMENTS_API const std::string & hostName()
Host name.
ELEMENTS_API int backTrace(ELEMENTS_UNUSED std::shared_ptr< void * > addresses, ELEMENTS_UNUSED const int depth)