31#include <mach-o/dyld.h>
50#include <boost/filesystem/operations.hpp>
69 ::dladdr(FuncPtrCast<void*>(funct),
m_dlinfo.
get());
73 return ::basename(
const_cast<char*
>(
m_dlinfo->dli_fname));
77 return const_cast<char*
>(
m_dlinfo->dli_fname);
88ModuleInfo::operator
const Dl_info&()
const {
97 static string module{};
100 string mod = ::basename(
const_cast<char*
>((
reinterpret_cast<Dl_info*
>(
moduleHandle()))->dli_fname));
101 module = mod.
substr(
static_cast<string::size_type
>(0), mod.
find(
'.'));
109 static string module{};
110 if (module.empty()) {
114 const char* path = (
reinterpret_cast<Dl_info*
>(
moduleHandle())->dli_fname);
115 if (::realpath(path, name.data())) {
116 module = name.data();
131 }
else if (module[loc] ==
'e' or module[loc] ==
'E') {
133 }
else if (module[loc] ==
's' and module[loc + 1] ==
'o') {
145 static void* hP =
reinterpret_cast<void*
>(pid);
150 s_module_handle = handle;
154 if (
nullptr == s_module_handle) {
157 if (0 != ::dladdr(FuncPtrCast<void*>(
moduleHandle), &info)) {
162 return s_module_handle;
167 static Dl_info infoBuf;
168 static Dl_info* info;
170 if (
nullptr == info) {
171 void* handle = ::dlopen(
nullptr, RTLD_LAZY);
172 if (
nullptr != handle) {
173 void* func = ::dlsym(handle,
"main");
174 if (
nullptr != func) {
175 if (0 != ::dladdr(func, &infoBuf)) {
185 static string module{
""};
186 if (module.empty()) {
194 Path::Item self_proc{
"/proc/self"};
196 Path::Item exe = self_proc /
"exe";
198 if (not boost::filesystem::exists(exe)) {
200 self_str <<
"/proc/" << ::getpid();
201 self_proc = Path::Item(self_str.str());
223 if (not(iss >> address >> perms >> offset >> dev >> inode >> pathname)) {
226 if (perms ==
"r-xp" and boost::filesystem::exists(pathname)) {
233 return linked_modules;
238 if (s_linkedModules.size() == 0) {
244 return s_linkedModules;
251 char pathbuf[PATH_MAX + 1];
252 unsigned int bufsize =
sizeof(pathbuf);
253 _NSGetExecutablePath(pathbuf, &bufsize);
254 path self_exe = path(
string(pathbuf));
261 return boost::filesystem::canonical(self_exe);
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
provide functions to retrieve resources pointed by environment variables
const void * addresse() const
std::unique_ptr< Dl_info > m_dlinfo
const std::string name() const
const std::string libraryName() const
T emplace_back(T... args)
void * ImageHandle
Definition of an image handle.
ELEMENTS_API void setModuleHandle(ImageHandle handle)
Attach module handle.
ELEMENTS_API Path::Item getSelfProc()
Get the path to the /proc directory of the process.
ELEMENTS_API ProcessHandle processHandle()
Handle to running process.
ELEMENTS_API ImageHandle moduleHandle()
Handle to currently executed module.
ELEMENTS_API ImageHandle exeHandle()
Handle to the executable file running.
ELEMENTS_API const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
ELEMENTS_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.
ELEMENTS_API Path::Item getExecutablePath()
Get the full executable path.
ELEMENTS_API ModuleType moduleType()
Get type of the module.
ELEMENTS_API const std::vector< std::string > linkedModules()
Vector of names of linked modules.
ELEMENTS_API std::vector< Path::Item > linkedModulePaths()
ELEMENTS_API const std::string & exeName()
Name of the executable file running.