liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright Antony Polukhin, 2016-2020.
//
// 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_ADDR2LINE_IMPLS_HPP
#define BOOST_STACKTRACE_DETAIL_ADDR2LINE_IMPLS_HPP
 
#include <boost/config.hpp>
#ifdef BOOST_HAS_PRAGMA_ONCE
#   pragma once
#endif
 
#include <boost/stacktrace/detail/to_hex_array.hpp>
#include <boost/stacktrace/detail/to_dec_array.hpp>
#include <boost/stacktrace/detail/try_dec_convert.hpp>
#include <boost/core/demangle.hpp>
#include <cstdio>
 
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
 
 
namespace boost { namespace stacktrace { namespace detail {
 
 
#if defined(BOOST_STACKTRACE_ADDR2LINE_LOCATION) && !defined(BOOST_NO_CXX11_CONSTEXPR)
 
constexpr bool is_abs_path(const char* path) BOOST_NOEXCEPT {
    return *path != '\0' && (
        *path == ':' || *path == '/' || is_abs_path(path + 1)
    );
}
 
#endif
 
class addr2line_pipe {
    ::FILE* p;
    ::pid_t pid;
 
public:
    explicit addr2line_pipe(const char *flag, const char* exec_path, const char* addr) BOOST_NOEXCEPT
        : p(0)
        , pid(0)
    {
        int pdes[2];
        #ifdef BOOST_STACKTRACE_ADDR2LINE_LOCATION
        char prog_name[] = BOOST_STRINGIZE( BOOST_STACKTRACE_ADDR2LINE_LOCATION );
        #if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_STATIC_ASSERT)
        static_assert(
            boost::stacktrace::detail::is_abs_path( BOOST_STRINGIZE( BOOST_STACKTRACE_ADDR2LINE_LOCATION ) ),
            "BOOST_STACKTRACE_ADDR2LINE_LOCATION must be an absolute path"
        );
        #endif
 
        #else
        char prog_name[] = "/usr/bin/addr2line";
        #endif
 
        char* argp[] = {
            prog_name,
            const_cast<char*>(flag),
            const_cast<char*>(exec_path),
            const_cast<char*>(addr),
            0
        };
 
        if (::pipe(pdes) < 0) {
            return;
        }
 
        pid = ::fork();
        switch (pid) {
        case -1:
            // Failed...
            ::close(pdes[0]);
            ::close(pdes[1]);
            return;
 
        case 0:
            // We are the child.
            ::close(STDERR_FILENO);
            ::close(pdes[0]);
            if (pdes[1] != STDOUT_FILENO) {
                ::dup2(pdes[1], STDOUT_FILENO);
            }
 
            // Do not use `execlp()`, `execvp()`, and `execvpe()` here!
            // `exec*p*` functions are vulnerable to PATH variable evaluation attacks.
            ::execv(prog_name, argp);
            ::_exit(127);
        }
 
        p = ::fdopen(pdes[0], "r");
        ::close(pdes[1]);
    }
 
    operator ::FILE*() const BOOST_NOEXCEPT {
        return p;
    }
 
    ~addr2line_pipe() BOOST_NOEXCEPT {
        if (p) {
            ::fclose(p);
            int pstat = 0;
            ::kill(pid, SIGKILL);
            ::waitpid(pid, &pstat, 0);
        }
    }
};
 
inline std::string addr2line(const char* flag, const void* addr) {
    std::string res;
 
    boost::stacktrace::detail::location_from_symbol loc(addr);
    if (!loc.empty()) {
        res = loc.name();
    } else {
        res.resize(16);
        int rlin_size = ::readlink("/proc/self/exe", &res[0], res.size() - 1);
        while (rlin_size == static_cast<int>(res.size() - 1)) {
            res.resize(res.size() * 4);
            rlin_size = ::readlink("/proc/self/exe", &res[0], res.size() - 1);
        }
        if (rlin_size == -1) {
            res.clear();
            return res;
        }
        res.resize(rlin_size);
    }
 
    addr2line_pipe p(flag, res.c_str(), to_hex_array(addr).data());
    res.clear();
 
    if (!p) {
        return res;
    }
 
    char data[32];
    while (!::feof(p)) {
        if (::fgets(data, sizeof(data), p)) {
            res += data;
        } else {
            break;
        }
    }
 
    // Trimming
    while (!res.empty() && (res[res.size() - 1] == '\n' || res[res.size() - 1] == '\r')) {
        res.erase(res.size() - 1);
    }
 
    return res;
}
 
 
struct to_string_using_addr2line {
    std::string res;
    void prepare_function_name(const void* addr) {
        res = boost::stacktrace::frame(addr).name();
    }
 
    bool prepare_source_location(const void* addr) {
        //return addr2line("-Cfipe", addr); // Does not seem to work in all cases
        std::string source_line = boost::stacktrace::detail::addr2line("-Cpe", addr);
        if (!source_line.empty() && source_line[0] != '?') {
            res += " at ";
            res += source_line;
            return true;
        }
 
        return false;
    }
};
 
template <class Base> class to_string_impl_base;
typedef to_string_impl_base<to_string_using_addr2line> to_string_impl;
 
inline std::string name_impl(const void* addr) {
    std::string res = boost::stacktrace::detail::addr2line("-fe", addr);
    res = res.substr(0, res.find_last_of('\n'));
    res = boost::core::demangle(res.c_str());
 
    if (res == "??") {
        res.clear();
    }
 
    return res;
}
 
} // namespace detail
 
std::string frame::source_file() const {
    std::string res;
    res = boost::stacktrace::detail::addr2line("-e", addr_);
    res = res.substr(0, res.find_last_of(':'));
    if (res == "??") {
        res.clear();
    }
 
    return res;
}
 
 
std::size_t frame::source_line() const {
    std::size_t line_num = 0;
    std::string res = boost::stacktrace::detail::addr2line("-e", addr_);
    const std::size_t last = res.find_last_of(':');
    if (last == std::string::npos) {
        return 0;
    }
    res = res.substr(last + 1);
 
    if (!boost::stacktrace::detail::try_dec_convert(res.c_str(), line_num)) {
        return 0;
    }
 
    return line_num;
}
 
 
}} // namespace boost::stacktrace
 
#endif // BOOST_STACKTRACE_DETAIL_ADDR2LINE_IMPLS_HPP