forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktrace.hh
159 lines (131 loc) · 4.09 KB
/
backtrace.hh
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
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright 2016 ScyllaDB
*/
#pragma once
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <iosfwd>
#include <boost/container/static_vector.hpp>
#include "core/sstring.hh"
#include "core/print.hh"
namespace seastar {
struct shared_object {
sstring name;
uintptr_t begin;
uintptr_t end; // C++-style, last addr + 1
};
struct frame {
const shared_object* so;
uintptr_t addr;
};
bool operator==(const frame& a, const frame& b);
// If addr doesn't seem to belong to any of the provided shared objects, it
// will be considered as part of the executable.
frame decorate(uintptr_t addr);
// Invokes func for each frame passing it as argument.
template<typename Func>
void backtrace(Func&& func) noexcept(noexcept(func(frame()))) {
unw_context_t context;
if (unw_getcontext(&context) < 0) {
return;
}
unw_cursor_t cursor;
if (unw_init_local(&cursor, &context) < 0) {
return;
}
while (unw_step(&cursor) > 0) {
unw_word_t ip;
if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) {
break;
}
if (!ip) {
break;
}
func(decorate(ip - 1));
}
}
class saved_backtrace {
public:
using vector_type = boost::container::static_vector<frame, 64>;
private:
vector_type _frames;
public:
saved_backtrace() = default;
saved_backtrace(vector_type f) : _frames(std::move(f)) {}
size_t hash() const;
friend std::ostream& operator<<(std::ostream& out, const saved_backtrace&);
bool operator==(const saved_backtrace& o) const {
return _frames == o._frames;
}
bool operator!=(const saved_backtrace& o) const {
return !(*this == o);
}
};
}
namespace std {
template<>
struct hash<seastar::saved_backtrace> {
size_t operator()(const seastar::saved_backtrace& b) const {
return b.hash();
}
};
}
namespace seastar {
saved_backtrace current_backtrace() noexcept;
std::ostream& operator<<(std::ostream& out, const saved_backtrace& b);
namespace internal {
template<class Exc>
class backtraced : public Exc {
std::shared_ptr<sstring> _backtrace;
public:
template<typename... Args>
backtraced(Args&&... args)
: Exc(std::forward<Args>(args)...)
, _backtrace(std::make_shared<sstring>(sprint("%s Backtrace: %s", Exc::what(), current_backtrace()))) {}
/**
* Returns the original exception message with a backtrace appended to it
*
* @return original exception message followed by a backtrace
*/
virtual const char* what() const noexcept override {
assert(_backtrace);
return _backtrace->c_str();
}
};
}
/**
* Throws an exception of unspecified type that is derived from the Exc type
* with a backtrace attached to its message
*
* @tparam Exc exception type to be caught at the receiving side
* @tparam Args types of arguments forwarded to the constructor of Exc
* @param args arguments forwarded to the constructor of Exc
* @return never returns (throws an exception)
*/
template <class Exc, typename... Args>
[[noreturn]]
void
throw_with_backtrace(Args&&... args) {
using exc_type = std::decay_t<Exc>;
static_assert(std::is_base_of<std::exception, exc_type>::value,
"throw_with_backtrace only works with exception types");
throw internal::backtraced<exc_type>(std::forward<Args>(args)...);
};
}