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
26 #include <boost/container/static_vector.hpp>
28 #include <seastar/core/sstring.hh>
29 #include <seastar/core/print.hh>
33 struct shared_object {
36 uintptr_t end; // C++-style, last addr + 1
40 const shared_object* so;
44 bool operator==(const frame& a, const frame& b);
47 // If addr doesn't seem to belong to any of the provided shared objects, it
48 // will be considered as part of the executable.
49 frame decorate(uintptr_t addr);
51 // Invokes func for each frame passing it as argument.
52 template<typename Func>
53 void backtrace(Func&& func) noexcept(noexcept(func(frame()))) {
54 constexpr size_t max_backtrace = 100;
55 void* buffer[max_backtrace];
56 int n = ::backtrace(buffer, max_backtrace);
57 for (int i = 0; i < n; ++i) {
58 auto ip = reinterpret_cast<uintptr_t>(buffer[i]);
59 func(decorate(ip - 1));
63 class saved_backtrace {
65 using vector_type = boost::container::static_vector<frame, 64>;
69 saved_backtrace() = default;
70 saved_backtrace(vector_type f) : _frames(std::move(f)) {}
73 friend std::ostream& operator<<(std::ostream& out, const saved_backtrace&);
75 bool operator==(const saved_backtrace& o) const {
76 return _frames == o._frames;
79 bool operator!=(const saved_backtrace& o) const {
89 struct hash<seastar::saved_backtrace> {
90 size_t operator()(const seastar::saved_backtrace& b) const {
99 saved_backtrace current_backtrace() noexcept;
100 std::ostream& operator<<(std::ostream& out, const saved_backtrace& b);
105 class backtraced : public Exc {
106 std::shared_ptr<sstring> _backtrace;
108 template<typename... Args>
109 backtraced(Args&&... args)
110 : Exc(std::forward<Args>(args)...)
111 , _backtrace(std::make_shared<sstring>(format("{} Backtrace: {}", Exc::what(), current_backtrace()))) {}
114 * Returns the original exception message with a backtrace appended to it
116 * @return original exception message followed by a backtrace
118 virtual const char* what() const noexcept override {
120 return _backtrace->c_str();
127 * Throws an exception of unspecified type that is derived from the Exc type
128 * with a backtrace attached to its message
130 * @tparam Exc exception type to be caught at the receiving side
131 * @tparam Args types of arguments forwarded to the constructor of Exc
132 * @param args arguments forwarded to the constructor of Exc
133 * @return never returns (throws an exception)
135 template <class Exc, typename... Args>
138 throw_with_backtrace(Args&&... args) {
139 using exc_type = std::decay_t<Exc>;
140 static_assert(std::is_base_of<std::exception, exc_type>::value,
141 "throw_with_backtrace only works with exception types");
142 throw internal::backtraced<exc_type>(std::forward<Args>(args)...);