Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Wozniski <[email protected]>
  • Loading branch information
pablogsal and godlygeek authored Sep 4, 2024
1 parent a81955c commit 2c297ce
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/memray/_memray/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,13 @@ dlopen(const char* filename, int flag) noexcept
if (dladdr(callerAddr, &info)) {
const char* dlname = info.dli_fname;
{
struct stat dlstat, exestat;
// Check fi we are being called from the main executable
// Check if we are being called from the main executable
Dl_info main_info;
void* main_sym = NULL;
void* self_handle = dlopen(nullptr, RTLD_LAZY | RTLD_NOLOAD);
void* self_handle = MEMRAY_ORIG(dlopen)(nullptr, RTLD_LAZY | RTLD_NOLOAD);
if (self_handle) {
main_sym = dlsym(self_handle, "main");
dlclose(self_handle);
MEMRAY_ORIG(dlclose)(self_handle);
}
if (main_sym && dladdr(main_sym, &main_info)
&& strcmp(main_info.dli_fname, info.dli_fname) == 0)
Expand All @@ -341,32 +340,35 @@ dlopen(const char* filename, int flag) noexcept
if (caller != nullptr) {
Dl_serinfo size;
if (dlinfo(caller, RTLD_DI_SERINFOSIZE, &size) == 0) {
auto* paths = reinterpret_cast<Dl_serinfo*>(new char[size.dls_size]);
std::vector<char> paths_buf;
paths_buf.resize(size.dls_size);
auto paths = reinterpret_cast<Dl_serinfo*>(&paths_buf[0]);
*paths = size;
if (dlinfo(caller, RTLD_DI_SERINFO, paths) == 0) {
for (unsigned i = 0; i != paths->dls_cnt; ++i) {
for (unsigned int i = 0; i != paths->dls_cnt; ++i) {
const char* name = paths->dls_serpath[i].dls_name;
std::string path;
if (name == nullptr || name[0] == '\0') {
// In the dynamic linking search path, an
// empty entry typically represents the
// current working directory ($PWD).
path = filename;
}
path = name;
if (path.back() != '/') {
path += '/';
path = "./";
} else {
path = name;
if (path.back() != '/') {
path += '/';
}
}

path += filename;
ret = MEMRAY_ORIG(dlopen)(path.c_str(), flag);
if (ret) {
break;
}
}
}
delete[] reinterpret_cast<char*>(paths);
}
dlclose(caller);
MEMRAY_ORIG(dlclose)(caller);
}
}
}
Expand Down

0 comments on commit 2c297ce

Please sign in to comment.