]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/BackTrace.h
import ceph quincy 17.2.4
[ceph.git] / ceph / src / common / BackTrace.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_BACKTRACE_H
5 #define CEPH_BACKTRACE_H
6
7 #include "acconfig.h"
8 #include <iosfwd>
9 #ifdef HAVE_EXECINFO_H
10 #include <execinfo.h>
11 #endif
12 #include <stdlib.h>
13
14 #include <list>
15 #include <string>
16
17 namespace ceph {
18
19 class Formatter;
20
21 struct BackTrace {
22 virtual ~BackTrace() {}
23 virtual void print(std::ostream& out) const = 0;
24 virtual void dump(Formatter *f) const = 0;
25 };
26
27 inline std::ostream& operator<<(std::ostream& out, const BackTrace& bt) {
28 bt.print(out);
29 return out;
30 }
31
32
33 struct ClibBackTrace : public BackTrace {
34 const static int max = 32;
35
36 int skip;
37 void *array[max]{};
38 size_t size;
39 char **strings;
40
41 explicit ClibBackTrace(int s) {
42 #ifdef HAVE_EXECINFO_H
43 skip = s;
44 size = backtrace(array, max);
45 strings = backtrace_symbols(array, size);
46 #else
47 skip = 0;
48 size = 0;
49 strings = nullptr;
50 #endif
51 }
52 ~ClibBackTrace() {
53 free(strings);
54 }
55
56 ClibBackTrace(const ClibBackTrace& other);
57 const ClibBackTrace& operator=(const ClibBackTrace& other);
58
59 void print(std::ostream& out) const override;
60 void dump(Formatter *f) const override;
61
62 static std::string demangle(const char* name);
63 };
64
65
66 struct PyBackTrace : public BackTrace {
67 std::list<std::string> strings;
68
69 explicit PyBackTrace(std::list<std::string>& s) : strings(s) {}
70
71 void dump(Formatter *f) const override;
72 void print(std::ostream& out) const override;
73 };
74
75
76 }
77
78 #endif