[Openmp-commits] [openmp] [OpenMP] Improve dladdr error handling in ompd_init() (PR #201043)

Mark Zhuang via Openmp-commits openmp-commits at lists.llvm.org
Thu Jun 11 01:28:29 PDT 2026


https://github.com/zqb-all updated https://github.com/llvm/llvm-project/pull/201043

>From 8e90913e06881ed6227d66a924d01ad7a1b6c3fb Mon Sep 17 00:00:00 2001
From: Mark Zhuang <mark.zhuang at spacemit.com>
Date: Tue, 2 Jun 2026 10:48:36 +0800
Subject: [PATCH] [OpenMP] Improve dladdr error handling in ompd_init()

When dladdr() fails to locate libomp.so, print a helpful hint pointing
the user at LD_LIBRARY_PATH instead of a bare dlerror() string. The hint
is shown when debugging is requested (OMP_DEBUG=enabled) or when dlerror()
returns a message, and the dlerror() text is appended only when non-NULL
to avoid confusing "(null)" output. Also guard dli_fname against NULL on
the success path before calling strrchr.

Assisted-by: Claude Sonnet 4.6
---
 openmp/runtime/src/ompd-specific.cpp | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/openmp/runtime/src/ompd-specific.cpp b/openmp/runtime/src/ompd-specific.cpp
index dadfb26e0e125..82ffe652f3caa 100644
--- a/openmp/runtime/src/ompd-specific.cpp
+++ b/openmp/runtime/src/ompd-specific.cpp
@@ -81,14 +81,24 @@ void ompd_init() {
 
   char *libname = NULL;
 
+  const char *ompd_env_var = getenv("OMP_DEBUG");
+  int ompd_debug = ompd_env_var && !strcmp(ompd_env_var, "enabled");
+
 #if KMP_OS_UNIX
   // Find the location of libomp.so thru dladdr and replace the libomp with
   // libompd to get the full path of libompd
   Dl_info dl_info;
   int ret = dladdr((void *)ompd_init, &dl_info);
   if (!ret) {
-    fprintf(stderr, "%s\n", dlerror());
-  } else if (strrchr(dl_info.dli_fname, '/')) {
+    const char *err = dlerror();
+    if (ompd_debug || err)
+      fprintf(stderr,
+              "The OpenMP runtime could not determine the location of "
+              "libompd.so. If the debugger fails to load the library, make "
+              "sure to add the directory containing a compatible libompd.so "
+              "to your LD_LIBRARY_PATH%s%s\n",
+              err ? ": " : "", err ? err : "");
+  } else if (dl_info.dli_fname && strrchr(dl_info.dli_fname, '/')) {
     int lib_path_length = strrchr(dl_info.dli_fname, '/') - dl_info.dli_fname;
     libname =
         (char *)malloc(lib_path_length + 12 /*for '/libompd.so' and '\0'*/);
@@ -97,8 +107,7 @@ void ompd_init() {
   }
 #endif
 
-  const char *ompd_env_var = getenv("OMP_DEBUG");
-  if (ompd_env_var && !strcmp(ompd_env_var, "enabled")) {
+  if (ompd_debug) {
     fprintf(stderr, "OMP_OMPD active\n");
     ompt_enabled.enabled = 1;
     ompd_state |= OMPD_ENABLE_BP;



More information about the Openmp-commits mailing list