Spicy
backtrace.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <cxxabi.h>
6 
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 namespace hilti::rt {
12 
14 class Backtrace {
15 public:
16  Backtrace();
17  Backtrace(const Backtrace& other) = default;
18  Backtrace(Backtrace&& other) = default;
19  ~Backtrace() = default;
20 
21  // Returns pointer to save stack space.
22  std::unique_ptr<std::vector<std::string>> backtrace() const;
23 
24  friend bool operator==(const Backtrace& a, const Backtrace& b);
25  friend bool operator!=(const Backtrace& a, const Backtrace& b) { return ! (a == b); }
26 
27  Backtrace& operator=(const Backtrace& other) = default;
28  Backtrace& operator=(Backtrace&& other) = default;
29 
30 private:
31  using Callstack = std::array<void*, 32>;
32  std::shared_ptr<Callstack> _callstack = nullptr;
33  int _frames = -1;
34 };
35 
36 bool operator==(const Backtrace& a, const Backtrace& b);
37 
39 inline std::string demangle(const std::string& symbol) {
40  int status;
41  char* dname = abi::__cxa_demangle(symbol.c_str(), nullptr, nullptr, &status);
42  std::string x = (dname && status == 0) ? dname : symbol;
43  if ( dname )
44  free(dname); // NOLINT
45 
46  return x;
47 }
48 
49 } // namespace hilti::rt
Definition: any.h:7
std::string demangle(const std::string &symbol)
Definition: backtrace.h:39
Definition: backtrace.h:14