From 3c49b1dbbef02af6058233408775e310766e769a Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sat, 24 Nov 2018 15:22:54 +0000 Subject: [PATCH] core: use dladdr() instead of parsing /proc/self/maps --- modules/core/src/utils/datafile.cpp | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/modules/core/src/utils/datafile.cpp b/modules/core/src/utils/datafile.cpp index 4973a55..09b412f 100644 --- a/modules/core/src/utils/datafile.cpp +++ b/modules/core/src/utils/datafile.cpp @@ -24,6 +24,8 @@ #undef min #undef max #undef abs +#elif defined(__linux__) +#include // requires -ldl #elif defined(__APPLE__) #include #if TARGET_OS_MAC @@ -123,27 +125,10 @@ static cv::String getModuleLocation(const void* addr) } } #elif defined(__linux__) - std::ifstream fs("/proc/self/maps"); - std::string line; - while (std::getline(fs, line, '\n')) + Dl_info info; + if (0 != dladdr(addr, &info)) { - long long int addr_begin = 0, addr_end = 0; - if (2 == sscanf(line.c_str(), "%llx-%llx", &addr_begin, &addr_end)) - { - if ((intptr_t)addr >= (intptr_t)addr_begin && (intptr_t)addr < (intptr_t)addr_end) - { - size_t pos = line.rfind(" "); // 2 spaces - if (pos == cv::String::npos) - pos = line.rfind(' '); // 1 spaces - else - pos++; - if (pos == cv::String::npos) - { - CV_LOG_DEBUG(NULL, "Can't parse module path: '" << line << '\''); - } - return line.substr(pos + 1); - } - } + return cv::String(info.dli_fname); } #elif defined(__APPLE__) # if TARGET_OS_MAC -- 2.7.4