GCC Code Coverage Report


Directory: ./
File: src/plugin.cc
Date: 2024-08-10 11:29:48
Exec Total Coverage
Lines: 14 29 48.3%
Branches: 5 72 6.9%

Line Branch Exec Source
1 // Copyright (c) 2019, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // 1. Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 // DAMAGE.
28
29 #include <dlfcn.h>
30
31 #include <boost/filesystem/operations.hpp>
32 #include <boost/filesystem/path.hpp>
33 #include <hpp/core/plugin.hh>
34 #include <hpp/util/debug.hh>
35 #include <hpp/util/exception-factory.hh>
36 #include <pinocchio/utils/file-explorer.hpp>
37
38 namespace fs = boost::filesystem;
39
40 namespace hpp {
41 namespace core {
42 namespace plugin {
43 std::string findPluginLibrary(const std::string& name) {
44 if (fs::path(name).is_absolute()) return name;
45 std::vector<std::string> ldpaths =
46 ::pinocchio::extractPathFromEnvVar("LD_LIBRARY_PATH", ":");
47 for (std::size_t i = 0; i < ldpaths.size(); ++i) {
48 fs::path lib(ldpaths[i]);
49 lib /= "hppPlugins";
50 lib /= name;
51 if (fs::is_regular_file(lib)) return lib.native();
52 }
53 HPP_THROW(std::invalid_argument, "Could not find plugin "
54 << name
55 << ". Check your LD_LIBRARY_PATH.");
56 }
57
58 1 bool loadPlugin(const std::string& lib, ProblemSolverPtr_t ps) {
59 typedef ::hpp::core::ProblemSolverPlugin* (*PluginFunction_t)();
60
61 // Clear old errors
62 1 const char* error = dlerror();
63 // void* library = dlopen(lib.c_str(), RTLD_NOW|RTLD_GLOBAL);
64 1 void* library = dlopen(lib.c_str(), RTLD_NOW);
65 1 error = dlerror();
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (error != NULL) {
67 throw std::runtime_error("Error loading library " + lib + ": " + error);
68 return false;
69 }
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (library == NULL) {
71 // uncaught error ?
72 return false;
73 }
74
75 PluginFunction_t function = reinterpret_cast<PluginFunction_t>(
76 1 dlsym(library, "createProblemSolverPlugin"));
77 1 error = dlerror();
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (error != NULL) {
79 throw std::runtime_error("Error loading library " + lib + ": " + error);
80 return false;
81 }
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (function == NULL) {
83 throw std::runtime_error(
84 "Symbol createProblemSolverPlugin of "
85 "(correctly loaded) library " +
86 lib + " is NULL.");
87 return false;
88 }
89
90 1 ProblemSolverPlugin* plugin = function();
91 1 bool success = plugin->initialize(ps);
92
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete plugin;
93
94 // I don't think we should do that because the symbols should not be
95 // removed... dlclose (library);
96
97 1 return success;
98 }
99 } // namespace plugin
100 } // namespace core
101 } // namespace hpp
102