|
| 1 | +// Distributed under the Boost Software License, Version 1.0. (See |
| 2 | +// accompanying file LICENSE_1_0.txt or copy at |
| 3 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 4 | + |
| 5 | +#ifndef BOOST_STACKTRACE_DETAIL_LIBDWFL_IMPLS_HPP |
| 6 | +#define BOOST_STACKTRACE_DETAIL_LIBDWFL_IMPLS_HPP |
| 7 | + |
| 8 | +#include <elfutils/libdwfl.h> |
| 9 | + |
| 10 | +#include <boost/stacktrace/detail/to_dec_array.hpp> |
| 11 | +#include <boost/stacktrace/frame.hpp> |
| 12 | + |
| 13 | +namespace boost { namespace stacktrace { namespace detail { |
| 14 | + |
| 15 | +class dwfl_handle { |
| 16 | +public: |
| 17 | + dwfl_handle() noexcept |
| 18 | + : dwfl_(dwfl_begin(&callbacks_)) |
| 19 | + { |
| 20 | + if (dwfl_) { |
| 21 | + dwfl_linux_proc_report(dwfl_, getpid()); |
| 22 | + dwfl_report_end(dwfl_, nullptr, nullptr); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + ~dwfl_handle() { |
| 27 | + if (dwfl_) { |
| 28 | + dwfl_end(dwfl_); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + const char* function(Dwarf_Addr addr) const noexcept { |
| 33 | + if (!dwfl_ || !addr) { |
| 34 | + return nullptr; |
| 35 | + } |
| 36 | + |
| 37 | + Dwfl_Module* dwfl_module = dwfl_addrmodule (dwfl_, addr); |
| 38 | + return dwfl_module ? dwfl_module_addrname(dwfl_module, addr) : nullptr; |
| 39 | + } |
| 40 | + |
| 41 | + std::pair<const char*, std::size_t> source(Dwarf_Addr addr) const noexcept { |
| 42 | + if (!dwfl_ || !addr) { |
| 43 | + return {nullptr, 0}; |
| 44 | + } |
| 45 | + |
| 46 | + Dwfl_Line* dwfl_line = dwfl_getsrc(dwfl_, addr); |
| 47 | + if (!dwfl_line) { |
| 48 | + return {nullptr, 0}; |
| 49 | + } |
| 50 | + |
| 51 | + int line{0}; |
| 52 | + const char* filename = dwfl_lineinfo(dwfl_line, nullptr, &line, nullptr, nullptr, nullptr); |
| 53 | + return {filename, static_cast<std::size_t>(line)}; |
| 54 | + } |
| 55 | + |
| 56 | +private: |
| 57 | + Dwfl_Callbacks callbacks_{ |
| 58 | + .find_elf = dwfl_linux_proc_find_elf, |
| 59 | + .find_debuginfo = dwfl_build_id_find_debuginfo, |
| 60 | + .section_address = dwfl_offline_section_address, |
| 61 | + .debuginfo_path = nullptr, |
| 62 | + }; |
| 63 | + Dwfl* dwfl_; |
| 64 | +}; |
| 65 | + |
| 66 | +struct to_string_using_dwfl { |
| 67 | + std::string res; |
| 68 | + dwfl_handle dwfl; |
| 69 | + |
| 70 | + void prepare_function_name(const void* addr) noexcept { |
| 71 | + const char* function = dwfl.function(reinterpret_cast<Dwarf_Addr>(addr)); |
| 72 | + if (function) { |
| 73 | + res = function; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + bool prepare_source_location(const void* addr) noexcept { |
| 78 | + auto [filename, line] = dwfl.source(reinterpret_cast<Dwarf_Addr>(addr)); |
| 79 | + if (!filename) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + res += " at "; |
| 84 | + res += filename; |
| 85 | + res += ':'; |
| 86 | + res += boost::stacktrace::detail::to_dec_array(line).data(); |
| 87 | + |
| 88 | + return true; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +template <class Base> class to_string_impl_base; |
| 93 | +typedef to_string_impl_base<to_string_using_dwfl> to_string_impl; |
| 94 | + |
| 95 | +inline std::string name_impl(const void* addr) { |
| 96 | + dwfl_handle dwfl; |
| 97 | + const char* function = dwfl.function(reinterpret_cast<Dwarf_Addr>(addr)); |
| 98 | + return function ? std::string{function} : std::string{}; |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace detail |
| 102 | + |
| 103 | +std::string frame::source_file() const { |
| 104 | + detail::dwfl_handle dwfl; |
| 105 | + auto [filename, _] = dwfl.source(reinterpret_cast<Dwarf_Addr>(addr_)); |
| 106 | + return filename ? std::string{filename} : std::string{}; |
| 107 | +} |
| 108 | + |
| 109 | +std::size_t frame::source_line() const { |
| 110 | + detail::dwfl_handle dwfl; |
| 111 | + return dwfl.source(reinterpret_cast<Dwarf_Addr>(addr_)).second; |
| 112 | +} |
| 113 | + |
| 114 | +}} // namespace boost::stacktrace |
| 115 | + |
| 116 | +#endif // BOOST_STACKTRACE_DETAIL_LIBDWFL_IMPLS_HPP |
0 commit comments