-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds an implementation based on libdwfl from elfutils. Implements #176
- Loading branch information
Showing
7 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Distributed under the Boost Software License, Version 1.0. (See | ||
// accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <elfutils/libdwfl.h> | ||
|
||
int main() { | ||
Dwfl_Callbacks callbacks{nullptr, nullptr, nullptr, nullptr}; | ||
Dwfl* dwfl_ = dwfl_begin(&callbacks); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Distributed under the Boost Software License, Version 1.0. (See | ||
// accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef BOOST_STACKTRACE_DETAIL_LIBDWFL_IMPLS_HPP | ||
#define BOOST_STACKTRACE_DETAIL_LIBDWFL_IMPLS_HPP | ||
|
||
#include <elfutils/libdwfl.h> | ||
|
||
#include <boost/stacktrace/detail/to_dec_array.hpp> | ||
#include <boost/stacktrace/frame.hpp> | ||
|
||
namespace boost { namespace stacktrace { namespace detail { | ||
|
||
class dwfl_handle { | ||
public: | ||
dwfl_handle() noexcept | ||
: dwfl_(dwfl_begin(&callbacks_)) | ||
{ | ||
if (dwfl_) { | ||
dwfl_linux_proc_report(dwfl_, getpid()); | ||
dwfl_report_end(dwfl_, nullptr, nullptr); | ||
} | ||
} | ||
|
||
~dwfl_handle() { | ||
if (dwfl_) { | ||
dwfl_end(dwfl_); | ||
} | ||
} | ||
|
||
const char* function(Dwarf_Addr addr) const noexcept { | ||
if (!dwfl_ || !addr) { | ||
return nullptr; | ||
} | ||
|
||
Dwfl_Module* dwfl_module = dwfl_addrmodule (dwfl_, addr); | ||
return dwfl_module ? dwfl_module_addrname(dwfl_module, addr) : nullptr; | ||
} | ||
|
||
std::pair<const char*, std::size_t> source(Dwarf_Addr addr) const noexcept { | ||
if (!dwfl_ || !addr) { | ||
return {nullptr, 0}; | ||
} | ||
|
||
Dwfl_Line* dwfl_line = dwfl_getsrc(dwfl_, addr); | ||
if (!dwfl_line) { | ||
return {nullptr, 0}; | ||
} | ||
|
||
int line{0}; | ||
const char* filename = dwfl_lineinfo(dwfl_line, nullptr, &line, nullptr, nullptr, nullptr); | ||
return {filename, static_cast<std::size_t>(line)}; | ||
} | ||
|
||
private: | ||
Dwfl_Callbacks callbacks_{ | ||
.find_elf = dwfl_linux_proc_find_elf, | ||
.find_debuginfo = dwfl_standard_find_debuginfo, | ||
.section_address = nullptr, | ||
.debuginfo_path = nullptr, | ||
}; | ||
Dwfl* dwfl_; | ||
}; | ||
|
||
struct to_string_using_dwfl { | ||
std::string res; | ||
dwfl_handle dwfl; | ||
|
||
void prepare_function_name(const void* addr) noexcept { | ||
const char* function = dwfl.function(reinterpret_cast<Dwarf_Addr>(addr)); | ||
if (function) { | ||
res = function; | ||
} | ||
} | ||
|
||
bool prepare_source_location(const void* addr) noexcept { | ||
auto [filename, line] = dwfl.source(reinterpret_cast<Dwarf_Addr>(addr)); | ||
if (!filename) { | ||
return false; | ||
} | ||
|
||
res += " at "; | ||
res += filename; | ||
res += ':'; | ||
res += boost::stacktrace::detail::to_dec_array(line).data(); | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
template <class Base> class to_string_impl_base; | ||
typedef to_string_impl_base<to_string_using_dwfl> to_string_impl; | ||
|
||
inline std::string name_impl(const void* addr) { | ||
dwfl_handle dwfl; | ||
const char* function = dwfl.function(reinterpret_cast<Dwarf_Addr>(addr)); | ||
return function ? std::string{function} : std::string{}; | ||
} | ||
|
||
} // namespace detail | ||
|
||
std::string frame::source_file() const { | ||
detail::dwfl_handle dwfl; | ||
auto [filename, _] = dwfl.source(reinterpret_cast<Dwarf_Addr>(addr_)); | ||
return filename ? std::string{filename} : std::string{}; | ||
} | ||
|
||
std::size_t frame::source_line() const { | ||
detail::dwfl_handle dwfl; | ||
return dwfl.source(reinterpret_cast<Dwarf_Addr>(addr_)).second; | ||
} | ||
|
||
}} // namespace boost::stacktrace | ||
|
||
#endif // BOOST_STACKTRACE_DETAIL_LIBDWFL_IMPLS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Distributed under the Boost Software License, Version 1.0. (See | ||
// accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#define BOOST_STACKTRACE_INTERNAL_BUILD_LIBS | ||
#define BOOST_STACKTRACE_USE_DWFL | ||
#define BOOST_STACKTRACE_LINK | ||
|
||
#ifndef _GNU_SOURCE | ||
# define _GNU_SOURCE | ||
#endif | ||
|
||
#include <boost/stacktrace/detail/frame_unwind.ipp> | ||
#include <boost/stacktrace/safe_dump_to.hpp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters