Seastar
High performance C++ framework for concurrent servers
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 <seastar/core/sstring.hh>
25 #include <seastar/core/print.hh>
27 #include <seastar/core/shared_ptr.hh>
28 #include <seastar/util/modules.hh>
29 
30 #ifndef SEASTAR_MODULE
31 #if __has_include(<execinfo.h>)
32 #include <execinfo.h>
33 #define HAVE_EXECINFO
34 #endif
35 #include <iosfwd>
36 #include <memory>
37 #include <variant>
38 #include <boost/container/static_vector.hpp>
39 #endif
40 
41 namespace seastar {
42 
43 struct shared_object {
44  sstring name;
45  uintptr_t begin;
46  uintptr_t end; // C++-style, last addr + 1
47 };
48 
49 struct frame {
50  const shared_object* so;
51  uintptr_t addr;
52 };
53 
54 bool operator==(const frame& a, const frame& b) noexcept;
55 
56 
57 // If addr doesn't seem to belong to any of the provided shared objects, it
58 // will be considered as part of the executable.
59 frame decorate(uintptr_t addr) noexcept;
60 
61 // Invokes func for each frame passing it as argument.
62 SEASTAR_MODULE_EXPORT
63 template<typename Func>
64 void backtrace(Func&& func) noexcept(noexcept(func(frame()))) {
65 #ifdef HAVE_EXECINFO
66  constexpr size_t max_backtrace = 100;
67  void* buffer[max_backtrace];
68  int n = ::backtrace(buffer, max_backtrace);
69  for (int i = 0; i < n; ++i) {
70  auto ip = reinterpret_cast<uintptr_t>(buffer[i]);
71  func(decorate(ip - 1));
72  }
73 #else
74 // Not implemented yet
75 #define SEASTAR_BACKTRACE_UNIMPLEMENTED
76 #endif
77 }
78 
79 // Represents a call stack of a single thread.
80 SEASTAR_MODULE_EXPORT
82 public:
83  using vector_type = boost::container::static_vector<frame, 64>;
84 private:
85  vector_type _frames;
86  size_t _hash;
87  char _delimeter;
88 private:
89  size_t calculate_hash() const noexcept;
90 public:
91  simple_backtrace(char delimeter = ' ') noexcept : _delimeter(delimeter) {}
92  simple_backtrace(vector_type f, char delimeter = ' ') noexcept : _frames(std::move(f)), _delimeter(delimeter) {}
93 
94  size_t hash() const noexcept { return _hash; }
95  char delimeter() const noexcept { return _delimeter; }
96 
97  friend std::ostream& operator<<(std::ostream& out, const simple_backtrace&);
98 
99  bool operator==(const simple_backtrace& o) const noexcept {
100  return _hash == o._hash && _frames == o._frames;
101  }
102 
103  bool operator!=(const simple_backtrace& o) const noexcept {
104  return !(*this == o);
105  }
106 };
107 
109 
110 // Represents a task object inside a tasktrace.
111 class task_entry {
112  const std::type_info* _task_type;
113 public:
114  task_entry(const std::type_info& ti) noexcept
115  : _task_type(&ti)
116  { }
117 
118  friend std::ostream& operator<<(std::ostream& out, const task_entry&);
119 
120  bool operator==(const task_entry& o) const noexcept {
121  return *_task_type == *o._task_type;
122  }
123 
124  bool operator!=(const task_entry& o) const noexcept {
125  return !(*this == o);
126  }
127 
128  size_t hash() const noexcept { return _task_type->hash_code(); }
129 };
130 
131 // Extended backtrace which consists of a backtrace of the currently running task
132 // and information about the chain of tasks waiting for the current operation to complete.
133 SEASTAR_MODULE_EXPORT
134 class tasktrace {
135 public:
136  using entry = std::variant<shared_backtrace, task_entry>;
137  using vector_type = boost::container::static_vector<entry, 16>;
138 private:
139  simple_backtrace _main;
140  vector_type _prev;
141  scheduling_group _sg;
142  size_t _hash;
143 public:
144  tasktrace() = default;
145  tasktrace(simple_backtrace main, vector_type prev, size_t prev_hash, scheduling_group sg) noexcept;
146  tasktrace(const tasktrace&) = default;
147  tasktrace& operator=(const tasktrace&) = default;
148  ~tasktrace();
149 
150  size_t hash() const noexcept { return _hash; }
151  char delimeter() const noexcept { return _main.delimeter(); }
152 
153  friend std::ostream& operator<<(std::ostream& out, const tasktrace&);
154 
155  bool operator==(const tasktrace& o) const noexcept;
156 
157  bool operator!=(const tasktrace& o) const noexcept {
158  return !(*this == o);
159  }
160 };
161 
162 }
163 
164 namespace std {
165 
166 SEASTAR_MODULE_EXPORT
167 template<>
168 struct hash<seastar::simple_backtrace> {
169  size_t operator()(const seastar::simple_backtrace& b) const {
170  return b.hash();
171  }
172 };
173 
174 SEASTAR_MODULE_EXPORT
175 template<>
176 struct hash<seastar::tasktrace> {
177  size_t operator()(const seastar::tasktrace& b) const {
178  return b.hash();
179  }
180 };
181 
182 }
183 
184 #if FMT_VERSION >= 90000
185 template <> struct fmt::formatter<seastar::tasktrace> : fmt::ostream_formatter {};
186 template <> struct fmt::formatter<seastar::simple_backtrace> : fmt::ostream_formatter {};
187 #endif
188 
189 namespace seastar {
190 
191 using saved_backtrace = tasktrace;
192 
193 saved_backtrace current_backtrace() noexcept;
194 
195 tasktrace current_tasktrace() noexcept;
196 
197 // Collects backtrace only within the currently executing task.
198 simple_backtrace current_backtrace_tasklocal() noexcept;
199 
200 std::ostream& operator<<(std::ostream& out, const tasktrace& b);
201 
202 namespace internal {
203 
204 template<class Exc>
205 class backtraced : public Exc {
206  std::shared_ptr<sstring> _backtrace;
207 public:
208  template<typename... Args>
209  backtraced(Args&&... args)
210  : Exc(std::forward<Args>(args)...)
211  , _backtrace(std::make_shared<sstring>(format("{} Backtrace: {}", Exc::what(), current_backtrace()))) {}
212 
218  virtual const char* what() const noexcept override {
219  assert(_backtrace);
220  return _backtrace->c_str();
221  }
222 };
223 
224 }
225 
226 
234 SEASTAR_MODULE_EXPORT
235 template <class Exc, typename... Args>
236 std::exception_ptr make_backtraced_exception_ptr(Args&&... args) {
237  using exc_type = std::decay_t<Exc>;
238  static_assert(std::is_base_of_v<std::exception, exc_type>,
239  "throw_with_backtrace only works with exception types");
240  return std::make_exception_ptr<internal::backtraced<exc_type>>(Exc(std::forward<Args>(args)...));
241 }
242 
252 SEASTAR_MODULE_EXPORT
253 template <class Exc, typename... Args>
254 [[noreturn]]
255 void
256 throw_with_backtrace(Args&&... args) {
257  std::rethrow_exception(make_backtraced_exception_ptr<Exc>(std::forward<Args>(args)...));
258 };
259 
260 }
Definition: shared_ptr.hh:270
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:286
Definition: backtrace.hh:81
Definition: backtrace.hh:111
Definition: backtrace.hh:134
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
void throw_with_backtrace(Args &&... args)
Definition: backtrace.hh:256
sstring format(const char *fmt, A &&... a)
Definition: print.hh:142
std::exception_ptr make_backtraced_exception_ptr(Args &&... args)
Definition: backtrace.hh:236
Definition: backtrace.hh:49
Definition: backtrace.hh:43