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);
127 #if defined(__linux__)
128 *pFunction = FuncPtrCast<EntryPoint>(::dlsym(handle, name.
c_str()));
129 if (0 == *pFunction) {
134 #elif defined(__APPLE__)
136 if (not *pFunction) {
138 string sname =
"_" + name;
141 if ( 0 == *pFunction ) {
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 =
"";
311 if (uname(&ut) == 0) {
321 static string mach =
"";
323 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);
408 bool backTrace(
string& btrace,
const int depth,
const int offset) {
412 const int total_depth = depth + total_offset;
418 if (addresses !=
nullptr) {
419 int count =
backTrace(addresses, total_depth);
420 for (
int i = total_offset; i < count; ++i) {
441 const int total_depth = depth + total_offset;
446 if (addresses !=
nullptr) {
448 int count =
backTrace(addresses, total_depth);
450 for (
int i=total_offset; i < count; ++i) {
466 string& fnc ELEMENTS_UNUSED,
string& lib ELEMENTS_UNUSED) {
471 if (::dladdr(addresses, &info) && info.dli_fname
472 && info.dli_fname[0] !=
'\0') {
474 info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname : 0;
476 lib = info.dli_fname;
477 addr = info.dli_saddr;
482 fnc =
string((stat == 0) ? dmg.
get() : symbol);
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 getLastError()
Get last system known error.
Macro to silence unused variables warnings from the compiler.
ELEMENTS_API unsigned long unloadDynamicLib(ImageHandle handle)
unload dynamic link library
ELEMENTS_API const std::string getLastErrorString()
Get last system error as string.
void *(*)( Creator)
Definition of the "generic" DLL entry point function.
ELEMENTS_API int setEnv(const std::string &name, const std::string &value, bool overwrite=true)
set an environment variables.
defines a Small helper function that allows the cast from void * to function pointer ...
ELEMENTS_API bool isEnvSet(const std::string &var)
Check if an environment variable is set or not.
void * ImageHandle
Definition of an image handle.
ELEMENTS_API std::string getEnv(const std::string &var)
get a particular environment variable
ELEMENTS_API const std::string & osVersion()
OS version.
This file is intended to iron out all the differences between systems (currently Linux and MacOSX) ...
ELEMENTS_API int unSetEnv(const std::string &name)
Simple wrap around unsetenv for strings.
ELEMENTS_API const std::string getErrorString(unsigned long error)
Retrieve error code as string for a given error.
ELEMENTS_API int backTrace(ELEMENTS_UNUSED std::shared_ptr< void * > addresses, ELEMENTS_UNUSED const int depth)
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 typeinfoName(const std::type_info &)
Get platform independent information about the class type.
OS specific details to access at run-time the module configuration of the process.
ELEMENTS_API const std::string & hostName()
Host name.
ELEMENTS_API const std::string & osName()
OS name.
unsigned long(*)(const unsigned long iid, void **ppvObject) EntryPoint
Definition of the "generic" DLL entry point function.
ELEMENTS_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
const std::string SHLIB_SUFFIX
alias for LIB_SUFFIX
ELEMENTS_API const std::string & machineType()
Machine type.