]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/util/backtrace.hh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / include / seastar / util / backtrace.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright 2016 ScyllaDB
20 */
21
22 #pragma once
23
24 #include <execinfo.h>
25 #include <iosfwd>
26 #include <variant>
27 #include <boost/container/static_vector.hpp>
28
29 #include <seastar/core/sstring.hh>
30 #include <seastar/core/print.hh>
31 #include <seastar/core/scheduling.hh>
32 #include <seastar/core/shared_ptr.hh>
33
34 namespace seastar {
35
36 struct shared_object {
37 sstring name;
38 uintptr_t begin;
39 uintptr_t end; // C++-style, last addr + 1
40 };
41
42 struct frame {
43 const shared_object* so;
44 uintptr_t addr;
45 };
46
47 bool operator==(const frame& a, const frame& b);
48
49
50 // If addr doesn't seem to belong to any of the provided shared objects, it
51 // will be considered as part of the executable.
52 frame decorate(uintptr_t addr);
53
54 // Invokes func for each frame passing it as argument.
55 template<typename Func>
56 void backtrace(Func&& func) noexcept(noexcept(func(frame()))) {
57 constexpr size_t max_backtrace = 100;
58 void* buffer[max_backtrace];
59 int n = ::backtrace(buffer, max_backtrace);
60 for (int i = 0; i < n; ++i) {
61 auto ip = reinterpret_cast<uintptr_t>(buffer[i]);
62 func(decorate(ip - 1));
63 }
64 }
65
66 // Represents a call stack of a single thread.
67 class simple_backtrace {
68 public:
69 using vector_type = boost::container::static_vector<frame, 64>;
70 private:
71 vector_type _frames;
72 size_t _hash;
73 private:
74 size_t calculate_hash() const;
75 public:
76 simple_backtrace() = default;
77 simple_backtrace(vector_type f) : _frames(std::move(f)) {}
78 size_t hash() const { return _hash; }
79
80 friend std::ostream& operator<<(std::ostream& out, const simple_backtrace&);
81
82 bool operator==(const simple_backtrace& o) const {
83 return _hash == o._hash && _frames == o._frames;
84 }
85
86 bool operator!=(const simple_backtrace& o) const {
87 return !(*this == o);
88 }
89 };
90
91 using shared_backtrace = seastar::lw_shared_ptr<simple_backtrace>;
92
93 // Represents a task object inside a tasktrace.
94 class task_entry {
95 const std::type_info* _task_type;
96 public:
97 task_entry(const std::type_info& ti)
98 : _task_type(&ti)
99 { }
100
101 friend std::ostream& operator<<(std::ostream& out, const task_entry&);
102
103 bool operator==(const task_entry& o) const {
104 return *_task_type == *o._task_type;
105 }
106
107 bool operator!=(const task_entry& o) const {
108 return !(*this == o);
109 }
110
111 size_t hash() const { return _task_type->hash_code(); }
112 };
113
114 // Extended backtrace which consists of a backtrace of the currently running task
115 // and information about the chain of tasks waiting for the current operation to complete.
116 class tasktrace {
117 public:
118 using entry = std::variant<shared_backtrace, task_entry>;
119 using vector_type = boost::container::static_vector<entry, 16>;
120 private:
121 simple_backtrace _main;
122 vector_type _prev;
123 scheduling_group _sg;
124 size_t _hash;
125 public:
126 tasktrace() = default;
127 tasktrace(simple_backtrace main, vector_type prev, size_t prev_hash, scheduling_group sg);
128 ~tasktrace();
129
130 size_t hash() const { return _hash; }
131
132 friend std::ostream& operator<<(std::ostream& out, const tasktrace&);
133
134 bool operator==(const tasktrace& o) const;
135
136 bool operator!=(const tasktrace& o) const {
137 return !(*this == o);
138 }
139 };
140
141 }
142
143 namespace std {
144
145 template<>
146 struct hash<seastar::simple_backtrace> {
147 size_t operator()(const seastar::simple_backtrace& b) const {
148 return b.hash();
149 }
150 };
151
152 template<>
153 struct hash<seastar::tasktrace> {
154 size_t operator()(const seastar::tasktrace& b) const {
155 return b.hash();
156 }
157 };
158
159 }
160
161 namespace seastar {
162
163 using saved_backtrace = tasktrace;
164
165 saved_backtrace current_backtrace() noexcept;
166
167 tasktrace current_tasktrace() noexcept;
168
169 // Collects backtrace only within the currently executing task.
170 simple_backtrace current_backtrace_tasklocal() noexcept;
171
172 std::ostream& operator<<(std::ostream& out, const tasktrace& b);
173
174 namespace internal {
175
176 template<class Exc>
177 class backtraced : public Exc {
178 std::shared_ptr<sstring> _backtrace;
179 public:
180 template<typename... Args>
181 backtraced(Args&&... args)
182 : Exc(std::forward<Args>(args)...)
183 , _backtrace(std::make_shared<sstring>(format("{} Backtrace: {}", Exc::what(), current_backtrace()))) {}
184
185 /**
186 * Returns the original exception message with a backtrace appended to it
187 *
188 * @return original exception message followed by a backtrace
189 */
190 virtual const char* what() const noexcept override {
191 assert(_backtrace);
192 return _backtrace->c_str();
193 }
194 };
195
196 }
197
198
199 /// Create an exception pointer of unspecified type that is derived from Exc type
200 /// with a backtrace attached to its message.
201 ///
202 /// \tparam Exc exception type to be caught at the receiving side
203 /// \tparam Args types of arguments forwarded to the constructor of Exc
204 /// \param args arguments forwarded to the constructor of Exc
205 /// \return std::exception_ptr containing the exception with the backtrace.
206 template <class Exc, typename... Args>
207 std::exception_ptr make_backtraced_exception_ptr(Args&&... args) {
208 using exc_type = std::decay_t<Exc>;
209 static_assert(std::is_base_of<std::exception, exc_type>::value,
210 "throw_with_backtrace only works with exception types");
211 return std::make_exception_ptr<internal::backtraced<exc_type>>(Exc(std::forward<Args>(args)...));
212 }
213
214 /**
215 * Throws an exception of unspecified type that is derived from the Exc type
216 * with a backtrace attached to its message
217 *
218 * @tparam Exc exception type to be caught at the receiving side
219 * @tparam Args types of arguments forwarded to the constructor of Exc
220 * @param args arguments forwarded to the constructor of Exc
221 * @return never returns (throws an exception)
222 */
223 template <class Exc, typename... Args>
224 [[noreturn]]
225 void
226 throw_with_backtrace(Args&&... args) {
227 std::rethrow_exception(make_backtraced_exception_ptr<Exc>(std::forward<Args>(args)...));
228 };
229
230 }