Add more output in case dlerror/dlsym/dlclose errors.
authorMikhail Kurinnoi <m.kurinnoi@samsung.com>
Mon, 28 Aug 2023 10:28:14 +0000 (13:28 +0300)
committerGleb Balykov/Advanced System SW Lab /SRR/Staff Engineer/Samsung Electronics <g.balykov@samsung.com>
Wed, 13 Sep 2023 11:49:30 +0000 (20:49 +0900)
src/utils/dynlibs_unix.cpp

index e7f745a700fc1d65add34105fd9061d3c062a1a8..29a3e81af4de88498f904720e7cf33b12ff0d8b2 100644 (file)
@@ -14,6 +14,7 @@
 #include <dlfcn.h>
 #include "utils/limits.h"
 #include "utils/dynlibs.h"
+#include "utils/logger.h"
 
 namespace netcoredbg
 {
@@ -23,7 +24,14 @@ namespace netcoredbg
 // In case of error function returns NULL.
 DLHandle DLOpen(const std::string &path)
 {
-    return reinterpret_cast<DLHandle>(::dlopen(path.c_str(), RTLD_GLOBAL | RTLD_NOW));
+    void *tmpPointer = ::dlopen(path.c_str(), RTLD_GLOBAL | RTLD_NOW);
+    if (tmpPointer == NULL)
+    {
+        char *err = ::dlerror();
+        fprintf(stderr, "dlopen() error: %s\n", err);
+        LOGE("dlopen() error: %s", err);
+    }
+    return reinterpret_cast<DLHandle>(tmpPointer);
 }
 
 // This function resolves symbol address within library specified by handle,
@@ -36,14 +44,34 @@ void* DLSym(DLHandle handle, string_view name)
 
     name.copy(str, name.size());
     str[name.size()] = 0;
-    return ::dlsym(handle, str);
+
+    ::dlerror(); // Clear any existing error
+
+    void *tmpPointer = ::dlsym(handle, str);
+
+    char *err = ::dlerror();
+    if (err != NULL)
+    {
+        fprintf(stderr, "dlsym() error: %s\n", err);
+        LOGE("dlsym() error: %s", err);
+    }
+
+    return tmpPointer;
 }
 
 /// This function unloads previously loadded library, specified by handle.
 /// In case of error this function returns `false'.
 bool DLClose(DLHandle handle)
 {
-    return ::dlclose(handle);
+    int ret = ::dlclose(handle);
+    if (ret != 0)
+    {
+        char *err = ::dlerror();
+        fprintf(stderr, "dlclose() error: %s\n", err);
+        LOGE("dlclose() error: %s", err);
+    }
+
+    return ret;
 }
 
 }  // ::netcoredbg