25#include <sys/utsname.h>
56unsigned long doLoad(
const string& name,
ImageHandle* handle) {
57 void* mh = ::dlopen(name.
length() == 0 ? 0 : name.
c_str(), RTLD_LAZY | RTLD_GLOBAL);
65unsigned long loadWithoutEnvironment(
const string& name,
ImageHandle* handle) {
67 string dll_name = name;
68 size_t dll_len = dll_name.
size();
72 if (dll_len >= suf_len && dll_name.
compare(dll_len - suf_len, suf_len,
SHLIB_SUFFIX) != 0) {
77 return doLoad(dll_name, handle);
88 res = loadWithoutEnvironment(name, handle);
93 if (
getEnv(name, imgName)) {
94 res = loadWithoutEnvironment(imgName, handle);
97 string dllName = name;
98 dllName =
"lib" + dllName;
101 res = loadWithoutEnvironment(dllName, handle);
104 errno =
static_cast<int>(0xAFFEDEAD);
118#if defined(__linux__)
120 if (0 == *pFunction) {
121 errno =
static_cast<int>(0xAFFEDEAD);
124#elif defined(__APPLE__)
126 if (not *pFunction) {
128 string sname =
"_" + name;
131 if (0 == *pFunction) {
132 errno =
static_cast<int>(0xAFFEDEAD);
148 return static_cast<unsigned long>(
static_cast<unsigned int>(errno));
162 if (error == 0xAFFEDEAD) {
163 cerrString =
reinterpret_cast<char*
>(::dlerror());
164 if (0 == cerrString) {
167 if (0 == cerrString) {
168 cerrString =
const_cast<char*
>(
"Unknown error. No information found in strerror()!");
170 errString =
string(cerrString);
174 errString =
string(cerrString);
185 if (strnlen(class_name, 1024) == 1) {
188 switch (class_name[0]) {
202 result =
"signed char";
205 result =
"unsigned char";
211 result =
"unsigned short";
217 result =
"unsigned int";
223 result =
"unsigned long";
226 result =
"long long";
229 result =
"unsigned long long";
235 result =
"unsigned __int128";
244 result =
"long double";
247 result =
"__float128";
255 std::unique_ptr<char,
decltype(free)*> realname(abi::__cxa_demangle(class_name, 0, 0, &status), free);
256 if (realname ==
nullptr) {
259 result = realname.get();
261 string::size_type pos = result.
find(
", ");
262 while (string::npos != pos) {
263 result.
replace(pos,
static_cast<string::size_type
>(2),
",");
264 pos = result.
find(
", ");
272 static string host{};
276 host = buffer.
data();
283 static string osname =
"";
285 if (::uname(&ut) == 0) {
295 static string osver =
"UNKNOWN";
298 if (uname(&ut) == 0) {
307 static string mach =
"UNKNOWN";
310 if (uname(&ut) == 0) {
327bool getEnv(
const string& variable_name,
string& variable_value) {
331 char* env = ::getenv(variable_name.
c_str());
332 if (env !=
nullptr) {
334 variable_value = env;
342 return getEnv(variable_name, result);
346#if defined(__APPLE__)
348#include "crt_externs.h"
351#if defined(__APPLE__)
352 static char** environ = *_NSGetEnviron();
355 for (
int i = 0; environ[i] != 0; ++i) {
362int setEnv(
const string& name,
const string& value,
bool overwrite) {
369 return ::setenv(name.
c_str(), value.
c_str(), over);
373 return ::unsetenv(name.
c_str());
382 int count = ::backtrace(addresses.get(), depth);
394 const int total_depth = depth + total_offset;
400 if (addresses.
get() !=
nullptr) {
402 int count =
backTrace(addresses, total_depth);
404 for (
int i = total_offset; i < count; ++i) {
425 if (::dladdr(addresses, &info) && info.dli_fname && info.dli_fname[0] !=
'\0') {
426 const char* symbol = info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname : 0;
428 lib = info.dli_fname;
429 addr = info.dli_saddr;
433 std::unique_ptr<char,
decltype(free)*> dmg(abi::__cxa_demangle(symbol, 0, 0, &stat), free);
434 fnc =
string((stat == 0) ? dmg.get() : symbol);
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)
constexpr DESTPTR FuncPtrCast(SRC *const src_p) noexcept
Cast from void * to function pointer.
unsigned long(*)(const unsigned long iid, void **ppvObject) EntryPoint
Definition of the "generic" DLL entry point function.
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
ELEMENTS_API const std::string getErrorString(unsigned long error)
Retrieve error code as string for a given error.
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
void * ImageHandle
Definition of an image handle.
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.