Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:52:31

0001 // Copyright Antony Polukhin, 2016-2023.
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See
0004 // accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_STACKTRACE_DETAIL_FRAME_UNWIND_IPP
0008 #define BOOST_STACKTRACE_DETAIL_FRAME_UNWIND_IPP
0009 
0010 #include <boost/config.hpp>
0011 #ifdef BOOST_HAS_PRAGMA_ONCE
0012 #   pragma once
0013 #endif
0014 
0015 #include <boost/stacktrace/frame.hpp>
0016 
0017 #include <boost/stacktrace/detail/to_hex_array.hpp>
0018 #include <boost/stacktrace/detail/location_from_symbol.hpp>
0019 #include <boost/stacktrace/detail/to_dec_array.hpp>
0020 #include <boost/core/demangle.hpp>
0021 
0022 #include <cstdio>
0023 
0024 #ifdef BOOST_STACKTRACE_USE_BACKTRACE
0025 #   include <boost/stacktrace/detail/libbacktrace_impls.hpp>
0026 #elif defined(BOOST_STACKTRACE_USE_ADDR2LINE)
0027 #   include <boost/stacktrace/detail/addr2line_impls.hpp>
0028 #else
0029 #   include <boost/stacktrace/detail/unwind_base_impls.hpp>
0030 #endif
0031 
0032 namespace boost { namespace stacktrace { namespace detail {
0033 
0034 template <class Base>
0035 class to_string_impl_base: private Base {
0036 public:
0037     std::string operator()(boost::stacktrace::detail::native_frame_ptr_t addr) {
0038         Base::res.clear();
0039         Base::prepare_function_name(addr);
0040         if (!Base::res.empty()) {
0041             Base::res = boost::core::demangle(Base::res.c_str());
0042         } else {
0043             Base::res = to_hex_array(addr).data();
0044         }
0045 
0046         if (Base::prepare_source_location(addr)) {
0047             return Base::res;
0048         }
0049 
0050         boost::stacktrace::detail::location_from_symbol loc(addr);
0051         if (!loc.empty()) {
0052             Base::res += " in ";
0053             Base::res += loc.name();
0054         }
0055 
0056         return Base::res;
0057     }
0058 };
0059 
0060 std::string to_string(const frame* frames, std::size_t size) {
0061     std::string res;
0062     if (size == 0) {
0063         return res;
0064     }
0065     res.reserve(64 * size);
0066 
0067     to_string_impl impl;
0068 
0069     for (std::size_t i = 0; i < size; ++i) {
0070         if (i < 10) {
0071             res += ' ';
0072         }
0073         res += boost::stacktrace::detail::to_dec_array(i).data();
0074         res += '#';
0075         res += ' ';
0076         res += impl(frames[i].address());
0077         res += '\n';
0078     }
0079 
0080     return res;
0081 }
0082 
0083 
0084 } // namespace detail
0085 
0086 
0087 std::string frame::name() const {
0088     if (!addr_) {
0089         return std::string();
0090     }
0091 
0092 #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
0093     ::Dl_info dli;
0094     const bool dl_ok = !!::dladdr(const_cast<void*>(addr_), &dli); // `dladdr` on Solaris accepts nonconst addresses
0095     if (dl_ok && dli.dli_sname) {
0096         return boost::core::demangle(dli.dli_sname);
0097     }
0098 #endif
0099     return boost::stacktrace::detail::name_impl(addr_);
0100 }
0101 
0102 std::string to_string(const frame& f) {
0103     if (!f) {
0104         return std::string();
0105     }
0106 
0107     boost::stacktrace::detail::to_string_impl impl;
0108     return impl(f.address());
0109 }
0110 
0111 
0112 }} // namespace boost::stacktrace
0113 
0114 #endif // BOOST_STACKTRACE_DETAIL_FRAME_UNWIND_IPP