37#include <boost/filesystem/operations.hpp>
38#include <boost/filesystem/path.hpp>
45#include <mach-o/dyld.h>
66 return ::basename(
const_cast<char*
>(
m_dlinfo->dli_fname));
70 return const_cast<char*
>(
m_dlinfo->dli_fname);
81ModuleInfo::operator
const Dl_info&()
const {
90 static string module{};
93 string mod = ::basename(
const_cast<char*
>((
reinterpret_cast<Dl_info*
>(
moduleHandle()))->dli_fname));
94 module = mod.substr(static_cast<string::size_type>(0), mod.find('.'));
102 static string module{};
103 if (module.
empty()) {
107 const char* path = (
reinterpret_cast<Dl_info*
>(
moduleHandle())->dli_fname);
108 if (::realpath(path, name.
data())) {
109 module = name.data();
124 }
else if (module[loc] ==
'e' or module[loc] ==
'E') {
126 }
else if (module[loc] ==
's' and module[loc + 1] ==
'o') {
138 static void* hP =
reinterpret_cast<void*
>(pid);
143 s_module_handle = handle;
147 if (
nullptr == s_module_handle) {
155 return s_module_handle;
160 static Dl_info infoBuf;
161 static Dl_info* info;
163 if (
nullptr == info) {
164 void* handle = ::dlopen(
nullptr, RTLD_LAZY);
165 if (
nullptr != handle) {
166 void* func = ::dlsym(handle,
"main");
167 if (
nullptr != func) {
168 if (0 != ::dladdr(func, &infoBuf)) {
178 static string module{
""};
179 if (module.
empty()) {
180 module = getExecutablePath().string();
187 Path::Item self_proc{
"/proc/self"};
189 Path::Item exe = self_proc /
"exe";
191 if (not boost::filesystem::exists(exe)) {
193 self_str <<
"/proc/" << ::getpid();
194 self_proc = Path::Item(self_str.
str());
216 if (not(iss >> address >> perms >> offset >> dev >> inode >> pathname)) {
219 if (perms ==
"r-xp" and boost::filesystem::exists(pathname)) {
226 return linked_modules;
231 if (s_linkedModules.size() == 0) {
234 s_linkedModules.emplace_back(m.string());
237 return s_linkedModules;
244 char pathbuf[PATH_MAX + 1];
245 unsigned int bufsize =
sizeof(pathbuf);
246 _NSGetExecutablePath(pathbuf, &bufsize);
247 path self_exe = path(
string(pathbuf));
254 return boost::filesystem::canonical(self_exe);
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.
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)
constexpr DESTPTR FuncPtrCast(SRC *const src_p) noexcept
Cast from void * to function pointer.
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.
void * ImageHandle
Definition of an image handle.
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.