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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright 2016 ScyllaDB
27 #include <boost/container/static_vector.hpp>
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>
36 struct shared_object {
39 uintptr_t end; // C++-style, last addr + 1
43 const shared_object* so;
47 bool operator==(const frame& a, const frame& b);
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);
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));
66 // Represents a call stack of a single thread.
67 class simple_backtrace {
69 using vector_type = boost::container::static_vector<frame, 64>;
74 size_t calculate_hash() const;
76 simple_backtrace() = default;
77 simple_backtrace(vector_type f) : _frames(std::move(f)) {}
78 size_t hash() const { return _hash; }
80 friend std::ostream& operator<<(std::ostream& out, const simple_backtrace&);
82 bool operator==(const simple_backtrace& o) const {
83 return _hash == o._hash && _frames == o._frames;
86 bool operator!=(const simple_backtrace& o) const {
91 using shared_backtrace = seastar::lw_shared_ptr<simple_backtrace>;
93 // Represents a task object inside a tasktrace.
95 const std::type_info* _task_type;
97 task_entry(const std::type_info& ti)
101 friend std::ostream& operator<<(std::ostream& out, const task_entry&);
103 bool operator==(const task_entry& o) const {
104 return *_task_type == *o._task_type;
107 bool operator!=(const task_entry& o) const {
108 return !(*this == o);
111 size_t hash() const { return _task_type->hash_code(); }
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.
118 using entry = std::variant<shared_backtrace, task_entry>;
119 using vector_type = boost::container::static_vector<entry, 16>;
121 simple_backtrace _main;
123 scheduling_group _sg;
126 tasktrace() = default;
127 tasktrace(simple_backtrace main, vector_type prev, size_t prev_hash, scheduling_group sg);
130 size_t hash() const { return _hash; }
132 friend std::ostream& operator<<(std::ostream& out, const tasktrace&);
134 bool operator==(const tasktrace& o) const;
136 bool operator!=(const tasktrace& o) const {
137 return !(*this == o);
146 struct hash<seastar::simple_backtrace> {
147 size_t operator()(const seastar::simple_backtrace& b) const {
153 struct hash<seastar::tasktrace> {
154 size_t operator()(const seastar::tasktrace& b) const {
163 using saved_backtrace = tasktrace;
165 saved_backtrace current_backtrace() noexcept;
167 tasktrace current_tasktrace() noexcept;
169 // Collects backtrace only within the currently executing task.
170 simple_backtrace current_backtrace_tasklocal() noexcept;
172 std::ostream& operator<<(std::ostream& out, const tasktrace& b);
177 class backtraced : public Exc {
178 std::shared_ptr<sstring> _backtrace;
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()))) {}
186 * Returns the original exception message with a backtrace appended to it
188 * @return original exception message followed by a backtrace
190 virtual const char* what() const noexcept override {
192 return _backtrace->c_str();
199 /// Create an exception pointer of unspecified type that is derived from Exc type
200 /// with a backtrace attached to its message.
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)...));
215 * Throws an exception of unspecified type that is derived from the Exc type
216 * with a backtrace attached to its message
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)
223 template <class Exc, typename... Args>
226 throw_with_backtrace(Args&&... args) {
227 std::rethrow_exception(make_backtraced_exception_ptr<Exc>(std::forward<Args>(args)...));