25#include <sys/utsname.h>
60unsigned long doLoad(
const string& name,
ImageHandle* handle) {
61 void* mh = ::dlopen(name.
length() == 0 ? 0 : name.
c_str(), RTLD_LAZY | RTLD_GLOBAL);
69unsigned long loadWithoutEnvironment(
const string& name,
ImageHandle* handle) {
71 string dll_name = name;
72 size_t dll_len = dll_name.
size();
76 if (dll_len >= suf_len && dll_name.
compare(dll_len - suf_len, suf_len,
SHLIB_SUFFIX) != 0) {
81 return doLoad(dll_name, handle);
92 res = loadWithoutEnvironment(name, handle);
97 if (
getEnv(name, imgName)) {
98 res = loadWithoutEnvironment(imgName, handle);
101 string dllName = name;
102 dllName =
"lib" + dllName;
105 res = loadWithoutEnvironment(dllName, handle);
108 errno =
static_cast<int>(0xAFFEDEAD);
125#if defined(__linux__)
126 *pFunction = FuncPtrCast<EntryPoint>(::dlsym(handle, name.
c_str()));
127 if (0 == *pFunction) {
128 errno =
static_cast<int>(0xAFFEDEAD);
132#elif defined(__APPLE__)
134 if (not *pFunction) {
136 string sname =
"_" + name;
139 if (0 == *pFunction) {
140 errno =
static_cast<int>(0xAFFEDEAD);
156 return static_cast<unsigned long>(
static_cast<unsigned int>(errno));
167 string errString =
"";
170 if (error == 0xAFFEDEAD) {
171 cerrString =
reinterpret_cast<char*
>(::dlerror());
172 if (0 == cerrString) {
175 if (0 == cerrString) {
176 cerrString =
const_cast<char*
>(
"Unknown error. No information found in strerror()!");
178 errString =
string(cerrString);
182 errString =
string(cerrString);
193 if (strnlen(class_name, 1024) == 1) {
196 switch (class_name[0]) {
210 result =
"signed char";
213 result =
"unsigned char";
219 result =
"unsigned short";
225 result =
"unsigned int";
231 result =
"unsigned long";
234 result =
"long long";
237 result =
"unsigned long long";
243 result =
"unsigned __int128";
252 result =
"long double";
255 result =
"__float128";
263 std::unique_ptr<char,
decltype(free)*> realname(abi::__cxa_demangle(class_name, 0, 0, &status), free);
264 if (realname ==
nullptr) {
267 result = realname.get();
269 string::size_type pos = result.
find(
", ");
270 while (string::npos != pos) {
271 result.
replace(pos,
static_cast<string::size_type
>(2),
",");
272 pos = result.
find(
", ");
280 static string host{};
284 host = buffer.
data();
291 static string osname =
"";
293 if (::uname(&ut) == 0) {
303 static string osver =
"UNKNOWN";
306 if (uname(&ut) == 0) {
315 static string mach =
"UNKNOWN";
318 if (uname(&ut) == 0) {
335bool getEnv(
const string& var,
string& value) {
339 char* env = ::getenv(var.
c_str());
340 if (env !=
nullptr) {
350 return getEnv(var, result);
354#if defined(__APPLE__)
356#include "crt_externs.h"
359#if defined(__APPLE__)
360 static char** environ = *_NSGetEnviron();
363 for (
int i = 0; environ[i] != 0; ++i) {
370int setEnv(
const string& name,
const string& value,
bool overwrite) {
377 return ::setenv(name.
c_str(), value.
c_str(), over);
381 return ::unsetenv(name.
c_str());
390 int count = ::backtrace(addresses.get(), depth);
402 const int total_depth = depth + total_offset;
408 if (addresses.get() !=
nullptr) {
410 int count =
backTrace(addresses, total_depth);
412 for (
int i = total_offset; i < count; ++i) {
420 trace.emplace_back(ost.
str());
433 if (::dladdr(addresses, &info) && info.dli_fname && info.dli_fname[0] !=
'\0') {
434 const char* symbol = info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname : 0;
436 lib = info.dli_fname;
437 addr = info.dli_saddr;
441 std::unique_ptr<char,
decltype(free)*> dmg(abi::__cxa_demangle(symbol, 0, 0, &stat), free);
442 fnc =
string((stat == 0) ? dmg.get() : symbol);
OS specific details to access at run-time the module configuration of the process.
defines a Small helper function that allows the cast from void * to function pointer
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 int backTrace(ELEMENTS_UNUSED std::shared_ptr< void * > addresses, ELEMENTS_UNUSED const int depth)
ELEMENTS_API std::vector< std::string > getEnv()
get all environment variables
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 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.