1 // Copyright Antony Polukhin, 2016-2017.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_STACKTRACE_DETAIL_ADDR2LINE_IMPLS_HPP
8 #define BOOST_STACKTRACE_DETAIL_ADDR2LINE_IMPLS_HPP
10 #include <boost/config.hpp>
11 #ifdef BOOST_HAS_PRAGMA_ONCE
15 #include <boost/stacktrace/detail/to_hex_array.hpp>
16 #include <boost/core/demangle.hpp>
17 #include <boost/lexical_cast.hpp>
20 #include <sys/types.h>
25 namespace boost { namespace stacktrace { namespace detail {
28 #if defined(BOOST_STACKTRACE_ADDR2LINE_LOCATION) && !defined(BOOST_NO_CXX11_CONSTEXPR)
30 constexpr bool is_abs_path(const char* path) BOOST_NOEXCEPT {
31 return *path != '\0' && (
32 *path == ':' || *path == '/' || is_abs_path(path + 1)
38 class addr2line_pipe {
43 explicit addr2line_pipe(const char *flag, const char* exec_path, const char* addr) BOOST_NOEXCEPT
48 #ifdef BOOST_STACKTRACE_ADDR2LINE_LOCATION
49 char prog_name[] = BOOST_STRINGIZE( BOOST_STACKTRACE_ADDR2LINE_LOCATION );
50 #if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_STATIC_ASSERT)
52 boost::stacktrace::detail::is_abs_path( BOOST_STRINGIZE( BOOST_STACKTRACE_ADDR2LINE_LOCATION ) ),
53 "BOOST_STACKTRACE_ADDR2LINE_LOCATION must be an absolute path"
58 char prog_name[] = "/usr/bin/addr2line";
63 const_cast<char*>(flag),
64 const_cast<char*>(exec_path),
65 const_cast<char*>(addr),
69 if (::pipe(pdes) < 0) {
83 ::close(STDERR_FILENO);
85 if (pdes[1] != STDOUT_FILENO) {
86 ::dup2(pdes[1], STDOUT_FILENO);
89 // Do not use `execlp()`, `execvp()`, and `execvpe()` here!
90 // `exec*p*` functions are vulnerable to PATH variable evaluation attacks.
91 ::execv(prog_name, argp);
95 p = ::fdopen(pdes[0], "r");
99 operator ::FILE*() const BOOST_NOEXCEPT {
103 ~addr2line_pipe() BOOST_NOEXCEPT {
107 ::kill(pid, SIGKILL);
108 ::waitpid(pid, &pstat, 0);
113 inline std::string addr2line(const char* flag, const void* addr) {
116 boost::stacktrace::detail::location_from_symbol loc(addr);
121 int rlin_size = ::readlink("/proc/self/exe", &res[0], res.size() - 1);
122 while (rlin_size == static_cast<int>(res.size() - 1)) {
123 res.resize(res.size() * 4);
124 rlin_size = ::readlink("/proc/self/exe", &res[0], res.size() - 1);
126 if (rlin_size == -1) {
130 res.resize(rlin_size);
133 addr2line_pipe p(flag, res.c_str(), to_hex_array(addr).data());
142 if (::fgets(data, sizeof(data), p)) {
150 while (!res.empty() && (res[res.size() - 1] == '\n' || res[res.size() - 1] == '\r')) {
151 res.erase(res.size() - 1);
158 struct to_string_using_addr2line {
160 void prepare_function_name(const void* addr) {
161 res = boost::stacktrace::frame(addr).name();
164 bool prepare_source_location(const void* addr) {
165 //return addr2line("-Cfipe", addr); // Does not seem to work in all cases
166 std::string source_line = boost::stacktrace::detail::addr2line("-Cpe", addr);
167 if (!source_line.empty() && source_line[0] != '?') {
177 template <class Base> class to_string_impl_base;
178 typedef to_string_impl_base<to_string_using_addr2line> to_string_impl;
180 inline std::string name_impl(const void* addr) {
181 std::string res = boost::stacktrace::detail::addr2line("-fe", addr);
182 res = res.substr(0, res.find_last_of('\n'));
183 res = boost::core::demangle(res.c_str());
192 } // namespace detail
194 std::string frame::source_file() const {
196 res = boost::stacktrace::detail::addr2line("-e", addr_);
197 res = res.substr(0, res.find_last_of(':'));
206 std::size_t frame::source_line() const {
207 std::size_t line_num = 0;
208 std::string res = boost::stacktrace::detail::addr2line("-e", addr_);
209 const std::size_t last = res.find_last_of(':');
210 if (last == std::string::npos) {
213 res = res.substr(last + 1);
215 if (!boost::conversion::try_lexical_convert(res, line_num)) {
223 }} // namespace boost::stacktrace
225 #endif // BOOST_STACKTRACE_DETAIL_ADDR2LINE_IMPLS_HPP