core: use dladdr() instead of parsing /proc/self/maps
authorAlexander Alekhin <alexander.a.alekhin@gmail.com>
Sat, 24 Nov 2018 15:22:54 +0000 (15:22 +0000)
committerAlexander Alekhin <alexander.a.alekhin@gmail.com>
Sat, 24 Nov 2018 15:22:54 +0000 (15:22 +0000)
modules/core/src/utils/datafile.cpp

index 4973a55..09b412f 100644 (file)
@@ -24,6 +24,8 @@
 #undef min
 #undef max
 #undef abs
+#elif defined(__linux__)
+#include <dlfcn.h>  // requires -ldl
 #elif defined(__APPLE__)
 #include <TargetConditionals.h>
 #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