]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/util/backtrace.hh
update sources to ceph Nautilus 14.2.1
[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 <boost/container/static_vector.hpp>
27
28 #include <seastar/core/sstring.hh>
29 #include <seastar/core/print.hh>
30
31 namespace seastar {
32
33 struct shared_object {
34 sstring name;
35 uintptr_t begin;
36 uintptr_t end; // C++-style, last addr + 1
37 };
38
39 struct frame {
40 const shared_object* so;
41 uintptr_t addr;
42 };
43
44 bool operator==(const frame& a, const frame& b);
45
46
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);
50
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));
60 }
61 }
62
63 class saved_backtrace {
64 public:
65 using vector_type = boost::container::static_vector<frame, 64>;
66 private:
67 vector_type _frames;
68 public:
69 saved_backtrace() = default;
70 saved_backtrace(vector_type f) : _frames(std::move(f)) {}
71 size_t hash() const;
72
73 friend std::ostream& operator<<(std::ostream& out, const saved_backtrace&);
74
75 bool operator==(const saved_backtrace& o) const {
76 return _frames == o._frames;
77 }
78
79 bool operator!=(const saved_backtrace& o) const {
80 return !(*this == o);
81 }
82 };
83
84 }
85
86 namespace std {
87
88 template<>
89 struct hash<seastar::saved_backtrace> {
90 size_t operator()(const seastar::saved_backtrace& b) const {
91 return b.hash();
92 }
93 };
94
95 }
96
97 namespace seastar {
98
99 saved_backtrace current_backtrace() noexcept;
100 std::ostream& operator<<(std::ostream& out, const saved_backtrace& b);
101
102 namespace internal {
103
104 template<class Exc>
105 class backtraced : public Exc {
106 std::shared_ptr<sstring> _backtrace;
107 public:
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()))) {}
112
113 /**
114 * Returns the original exception message with a backtrace appended to it
115 *
116 * @return original exception message followed by a backtrace
117 */
118 virtual const char* what() const noexcept override {
119 assert(_backtrace);
120 return _backtrace->c_str();
121 }
122 };
123
124 }
125
126 /**
127 * Throws an exception of unspecified type that is derived from the Exc type
128 * with a backtrace attached to its message
129 *
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)
134 */
135 template <class Exc, typename... Args>
136 [[noreturn]]
137 void
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)...);
143 };
144
145 }