]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/dll/runtime_symbol_info.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / dll / runtime_symbol_info.hpp
1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015-2017 Antony Polukhin.
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifndef BOOST_DLL_RUNTIME_SYMBOL_INFO_HPP
9 #define BOOST_DLL_RUNTIME_SYMBOL_INFO_HPP
10
11 #include <boost/config.hpp>
12 #include <boost/predef/os.h>
13 #include <boost/predef/compiler/visualc.h>
14 #include <boost/dll/detail/aggressive_ptr_cast.hpp>
15 #if BOOST_OS_WINDOWS
16 # include <boost/winapi/dll.hpp>
17 # include <boost/dll/detail/windows/path_from_handle.hpp>
18 #else
19 # include <dlfcn.h>
20 # include <boost/dll/detail/posix/program_location_impl.hpp>
21 #endif
22
23 #ifdef BOOST_HAS_PRAGMA_ONCE
24 # pragma once
25 #endif
26
27 /// \file boost/dll/runtime_symbol_info.hpp
28 /// \brief Provides methods for getting acceptable by boost::dll::shared_library location of symbol, source line or program.
29 namespace boost { namespace dll {
30
31 #if BOOST_OS_WINDOWS
32 namespace detail {
33 inline boost::filesystem::path program_location_impl(boost::system::error_code& ec) {
34 return boost::dll::detail::path_from_handle(NULL, ec);
35 }
36 } // namespace detail
37 #endif
38
39 /*!
40 * On success returns full path and name to the binary object that holds symbol pointed by ptr_to_symbol.
41 *
42 * \param ptr_to_symbol Pointer to symbol which location is to be determined.
43 * \param ec Variable that will be set to the result of the operation.
44 * \return Path to the binary object that holds symbol or empty path in case error.
45 * \throws std::bad_alloc in case of insufficient memory. Overload that does not accept boost::system::error_code also throws boost::system::system_error.
46 *
47 * \b Examples:
48 * \code
49 * int main() {
50 * dll::symbol_location_ptr(std::set_terminate(0)); // returns "/some/path/libmy_terminate_handler.so"
51 * dll::symbol_location_ptr(::signal(SIGSEGV, SIG_DFL)); // returns "/some/path/libmy_symbol_handler.so"
52 * }
53 * \endcode
54 */
55 template <class T>
56 inline boost::filesystem::path symbol_location_ptr(T ptr_to_symbol, boost::system::error_code& ec) {
57 BOOST_STATIC_ASSERT_MSG(boost::is_pointer<T>::value, "boost::dll::symbol_location_ptr works only with pointers! `ptr_to_symbol` must be a pointer");
58 boost::filesystem::path ret;
59 if (!ptr_to_symbol) {
60 ec = boost::system::error_code(
61 boost::system::errc::bad_address,
62 boost::system::generic_category()
63 );
64
65 return ret;
66 }
67 ec.clear();
68
69 const void* ptr = boost::dll::detail::aggressive_ptr_cast<const void*>(ptr_to_symbol);
70
71 #if BOOST_OS_WINDOWS
72 boost::winapi::MEMORY_BASIC_INFORMATION_ mbi;
73 if (!boost::winapi::VirtualQuery(ptr, &mbi, sizeof(mbi))) {
74 ec = boost::dll::detail::last_error_code();
75 return ret;
76 }
77
78 return boost::dll::detail::path_from_handle(reinterpret_cast<boost::winapi::HMODULE_>(mbi.AllocationBase), ec);
79 #else
80 Dl_info info;
81
82 // Some of the libc headers miss `const` in `dladdr(const void*, Dl_info*)`
83 const int res = dladdr(const_cast<void*>(ptr), &info);
84
85 if (res) {
86 ret = info.dli_fname;
87 } else {
88 boost::dll::detail::reset_dlerror();
89 ec = boost::system::error_code(
90 boost::system::errc::bad_address,
91 boost::system::generic_category()
92 );
93 }
94
95 return ret;
96 #endif
97 }
98
99 //! \overload symbol_location_ptr(const void* ptr_to_symbol, boost::system::error_code& ec)
100 template <class T>
101 inline boost::filesystem::path symbol_location_ptr(T ptr_to_symbol) {
102 boost::filesystem::path ret;
103 boost::system::error_code ec;
104 ret = boost::dll::symbol_location_ptr(ptr_to_symbol, ec);
105
106 if (ec) {
107 boost::dll::detail::report_error(ec, "boost::dll::symbol_location_ptr(T ptr_to_symbol) failed");
108 }
109
110 return ret;
111 }
112
113 /*!
114 * On success returns full path and name of the binary object that holds symbol.
115 *
116 * \tparam T Type of the symbol, must not be explicitly specified.
117 * \param symbol Symbol which location is to be determined.
118 * \param ec Variable that will be set to the result of the operation.
119 * \return Path to the binary object that holds symbol or empty path in case error.
120 * \throws std::bad_alloc in case of insufficient memory. Overload that does not accept boost::system::error_code also throws boost::system::system_error.
121 *
122 * \b Examples:
123 * \code
124 * int var;
125 * void foo() {}
126 *
127 * int main() {
128 * dll::symbol_location(var); // returns program location
129 * dll::symbol_location(foo); // returns program location
130 * dll::symbol_location(std::cerr); // returns location of libstdc++: "/usr/lib/x86_64-linux-gnu/libstdc++.so.6"
131 * dll::symbol_location(std::placeholders::_1); // returns location of libstdc++: "/usr/lib/x86_64-linux-gnu/libstdc++.so.6"
132 * dll::symbol_location(std::puts); // returns location of libc: "/lib/x86_64-linux-gnu/libc.so.6"
133 * }
134 * \endcode
135 */
136 template <class T>
137 inline boost::filesystem::path symbol_location(const T& symbol, boost::system::error_code& ec) {
138 ec.clear();
139 return boost::dll::symbol_location_ptr(
140 boost::dll::detail::aggressive_ptr_cast<const void*>(boost::addressof(symbol)),
141 ec
142 );
143 }
144
145 #if BOOST_COMP_MSVC < BOOST_VERSION_NUMBER(14,0,0)
146 // Without this MSVC 7.1 fails with:
147 // ..\boost\dll\runtime_symbol_info.hpp(133) : error C2780: 'filesystem::path dll::symbol_location(const T &)' : expects 1 arguments - 2 provided
148 template <class T>
149 inline boost::filesystem::path symbol_location(const T& symbol, const char* /*workaround*/ = 0)
150 #else
151 //! \overload symbol_location(const T& symbol, boost::system::error_code& ec)
152 template <class T>
153 inline boost::filesystem::path symbol_location(const T& symbol)
154 #endif
155 {
156 boost::filesystem::path ret;
157 boost::system::error_code ec;
158 ret = boost::dll::symbol_location_ptr(
159 boost::dll::detail::aggressive_ptr_cast<const void*>(boost::addressof(symbol)),
160 ec
161 );
162
163 if (ec) {
164 boost::dll::detail::report_error(ec, "boost::dll::symbol_location(const T& symbol) failed");
165 }
166
167 return ret;
168 }
169
170 /// @cond
171 // We have anonymous namespace here to make sure that `this_line_location()` method is instantiated in
172 // current translation unit and is not shadowed by instantiations from other units.
173 namespace {
174 /// @endcond
175
176 /*!
177 * On success returns full path and name of the binary object that holds the current line of code
178 * (the line in which the `this_line_location()` method was called).
179 *
180 * \param ec Variable that will be set to the result of the operation.
181 * \throws std::bad_alloc in case of insufficient memory. Overload that does not accept boost::system::error_code also throws boost::system::system_error.
182 */
183 static inline boost::filesystem::path this_line_location(boost::system::error_code& ec) {
184 typedef boost::filesystem::path(func_t)(boost::system::error_code& );
185 func_t& f = this_line_location;
186 return boost::dll::symbol_location(f, ec);
187 }
188
189 //! \overload this_line_location(boost::system::error_code& ec)
190 static inline boost::filesystem::path this_line_location() {
191 boost::filesystem::path ret;
192 boost::system::error_code ec;
193 ret = this_line_location(ec);
194
195 if (ec) {
196 boost::dll::detail::report_error(ec, "boost::dll::this_line_location() failed");
197 }
198
199 return ret;
200 }
201
202 /// @cond
203 } // anonymous namespace
204 /// @endcond
205
206 /*!
207 * On success returns full path and name of the currently running program (the one which contains the `main()` function).
208 *
209 * Return value can be used as a parameter for shared_library. See Tutorial "Linking plugin into the executable"
210 * for usage example. Flag '-rdynamic' must be used when linking the plugin into the executable
211 * on Linux OS.
212 *
213 * \param ec Variable that will be set to the result of the operation.
214 * \throws std::bad_alloc in case of insufficient memory. Overload that does not accept boost::system::error_code also throws boost::system::system_error.
215 */
216 inline boost::filesystem::path program_location(boost::system::error_code& ec) {
217 ec.clear();
218 return boost::dll::detail::program_location_impl(ec);
219 }
220
221 //! \overload program_location(boost::system::error_code& ec) {
222 inline boost::filesystem::path program_location() {
223 boost::filesystem::path ret;
224 boost::system::error_code ec;
225 ret = boost::dll::detail::program_location_impl(ec);
226
227 if (ec) {
228 boost::dll::detail::report_error(ec, "boost::dll::program_location() failed");
229 }
230
231 return ret;
232 }
233
234 }} // namespace boost::dll
235
236 #endif // BOOST_DLL_RUNTIME_SYMBOL_INFO_HPP
237