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);
131#elif defined(__APPLE__)
133 if (not *pFunction) {
135 string sname =
"_" + name;
138 if (0 == *pFunction) {
139 errno =
static_cast<int>(0xAFFEDEAD);
155 return static_cast<unsigned long>(
static_cast<unsigned int>(errno));
166 string errString =
"";
169 if (error == 0xAFFEDEAD) {
170 cerrString =
reinterpret_cast<char*
>(::dlerror());
171 if (0 == cerrString) {
174 if (0 == cerrString) {
175 cerrString =
const_cast<char*
>(
"Unknown error. No information found in strerror()!");
177 errString =
string(cerrString);
181 errString =
string(cerrString);
192 if (strnlen(class_name, 1024) == 1) {
195 switch (class_name[0]) {
209 result =
"signed char";
212 result =
"unsigned char";
218 result =
"unsigned short";
224 result =
"unsigned int";
230 result =
"unsigned long";
233 result =
"long long";
236 result =
"unsigned long long";
242 result =
"unsigned __int128";
251 result =
"long double";
254 result =
"__float128";
262 std::unique_ptr<char,
decltype(free)*> realname(abi::__cxa_demangle(class_name, 0, 0, &status), free);
263 if (realname ==
nullptr) {
266 result = realname.get();
268 string::size_type pos = result.
find(
", ");
269 while (string::npos != pos) {
270 result.
replace(pos,
static_cast<string::size_type
>(2),
",");
271 pos = result.
find(
", ");
279 static string host{};
283 host = buffer.
data();
290 static string osname =
"";
292 if (::uname(&ut) == 0) {
302 static string osver =
"UNKNOWN";
305 if (uname(&ut) == 0) {
314 static string mach =
"UNKNOWN";
317 if (uname(&ut) == 0) {
334bool getEnv(
const string& var,
string& value) {
338 char* env = ::getenv(var.
c_str());
339 if (env !=
nullptr) {
349 return getEnv(var, result);
353#if defined(__APPLE__)
355#include "crt_externs.h"
358#if defined(__APPLE__)
359 static char** environ = *_NSGetEnviron();
362 for (
int i = 0; environ[i] != 0; ++i) {
369int setEnv(
const string& name,
const string& value,
bool overwrite) {
376 return ::setenv(name.
c_str(), value.
c_str(), over);
380 return ::unsetenv(name.
c_str());
389 int count = ::backtrace(addresses.get(), depth);
401 const int total_depth = depth + total_offset;
407 if (addresses.get() !=
nullptr) {
409 int count =
backTrace(addresses, total_depth);
411 for (
int i = total_offset; i < count; ++i) {
419 trace.emplace_back(ost.
str());
432 if (::dladdr(addresses, &info) && info.dli_fname && info.dli_fname[0] !=
'\0') {
433 const char* symbol = info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname : 0;
435 lib = info.dli_fname;
436 addr = info.dli_saddr;
440 std::unique_ptr<char,
decltype(free)*> dmg(abi::__cxa_demangle(symbol, 0, 0, &stat), free);
441 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 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.