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 <array>
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 namespace hilti::rt {
13 
15 class Backtrace {
16 public:
17  Backtrace();
18  Backtrace(const Backtrace& other) = default;
19  Backtrace(Backtrace&& other) = default;
20  ~Backtrace() = default;
21 
22  // Returns pointer to save stack space.
23  std::unique_ptr<std::vector<std::string>> backtrace() const;
24 
25  friend bool operator==(const Backtrace& a, const Backtrace& b);
26  friend bool operator!=(const Backtrace& a, const Backtrace& b) { return ! (a == b); }
27 
28  Backtrace& operator=(const Backtrace& other) = default;
29  Backtrace& operator=(Backtrace&& other) = default;
30 
31 private:
32  using Callstack = std::array<void*, 32>;
33  std::shared_ptr<Callstack> _callstack = nullptr;
34  int _frames = -1;
35 };
36 
37 bool operator==(const Backtrace& a, const Backtrace& b);
38 
40 inline std::string demangle(const std::string& symbol) {
41  int status;
42  char* dname = abi::__cxa_demangle(symbol.c_str(), nullptr, nullptr, &status);
43  std::string x = (dname && status == 0) ? dname : symbol;
44  if ( dname )
45  free(dname); // NOLINT
46 
47  return x;
48 }
49 
50 } // namespace hilti::rt
Definition: any.h:7
std::string demangle(const std::string &symbol)
Definition: backtrace.h:40
Definition: backtrace.h:15