Skip to content

Commit 609f085

Browse files
sawenzelclaude
andcommitted
Report why a simulation setup library cannot be loaded
This replaces the asserts in execFromPlugin by fatal messages with the reason. - A failed dlopen of libO2G4Setup and friends stopped at assert(libHandle), without the error from dlerror(). - The message now gives the dlopen error, e.g. a missing static TLS reserve for Geant4 built with the initial-exec TLS model. - Errors of the names that do not exist are listed only if no other error occurred. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a8a2a45 commit 609f085

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

‎Detectors/gconfig/src/SimSetup.cxx‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
#include <fairlogger/Logger.h>
1616
#include "SetCuts.h"
1717
#include <dlfcn.h>
18-
#ifdef NDEBUG
19-
#undef NDEBUG
20-
#endif
21-
#include <cassert>
22-
#include <sstream>
18+
#include <string>
2319

2420
namespace o2
2521
{
@@ -28,23 +24,36 @@ typedef void (*setup_fnc)();
2824

2925
void execFromPlugin(const char* libname, const char* funcname)
3026
{
31-
auto libHandle = dlopen(libname, RTLD_NOW);
32-
// try to make the library loading a bit more portable:
27+
// errors other than "not found" explain why an existing library could not be loaded
28+
std::string notFound, otherErrors;
29+
auto open = [&notFound, &otherErrors](const std::string& name) {
30+
auto handle = dlopen(name.c_str(), RTLD_NOW);
31+
if (!handle) {
32+
const char* error = dlerror();
33+
const std::string message = error ? error : "unknown error";
34+
(message.find("No such file or directory") != std::string::npos ? notFound : otherErrors) += "\n " + message;
35+
}
36+
return handle;
37+
};
38+
// try to make the library loading a bit more portable by also trying the *.so and *.dylib names
39+
auto libHandle = open(libname);
3340
if (!libHandle) {
34-
// try appending *.so
35-
std::stringstream stream;
36-
stream << libname << ".so";
37-
libHandle = dlopen(stream.str().c_str(), RTLD_NOW);
41+
libHandle = open(std::string(libname) + ".so");
3842
}
3943
if (!libHandle) {
40-
// try appending *.dylib
41-
std::stringstream stream;
42-
stream << libname << ".dylib";
43-
libHandle = dlopen(stream.str().c_str(), RTLD_NOW);
44+
libHandle = open(std::string(libname) + ".dylib");
4445
}
45-
assert(libHandle);
46+
if (!libHandle) {
47+
LOG(fatal) << "Cannot load " << libname << ":" << (otherErrors.empty() ? notFound : otherErrors);
48+
return;
49+
}
50+
dlerror(); // clear any pending error before dlsym
4651
auto func = (setup_fnc)dlsym(libHandle, funcname);
47-
assert(func);
52+
if (!func) {
53+
const char* error = dlerror();
54+
LOG(fatal) << "Cannot find " << funcname << " in " << libname << ": " << (error ? error : "the symbol is null");
55+
return;
56+
}
4857
func();
4958
}
5059

0 commit comments

Comments
 (0)