initial code repo
[stor4nfv.git] / src / ceph / src / common / BackTrace.h
diff --git a/src/ceph/src/common/BackTrace.h b/src/ceph/src/common/BackTrace.h
new file mode 100644 (file)
index 0000000..372788e
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef CEPH_BACKTRACE_H
+#define CEPH_BACKTRACE_H
+
+#include "acconfig.h"
+#include <iosfwd>
+#ifdef HAVE_EXECINFO_H
+#include <execinfo.h>
+#endif
+#include <stdlib.h>
+
+namespace ceph {
+
+struct BackTrace {
+  const static int max = 100;
+
+  int skip;
+  void *array[max]{};
+  size_t size;
+  char **strings;
+
+  explicit BackTrace(int s) : skip(s) {
+#ifdef HAVE_EXECINFO_H
+    size = backtrace(array, max);
+    strings = backtrace_symbols(array, size);
+#else
+    skip = 0;
+    size = 0;
+    strings = nullptr;
+#endif
+  }
+  ~BackTrace() {
+    free(strings);
+  }
+
+  BackTrace(const BackTrace& other);
+  const BackTrace& operator=(const BackTrace& other);
+
+  void print(std::ostream& out) const;
+};
+
+inline std::ostream& operator<<(std::ostream& out, const BackTrace& bt) {
+  bt.print(out);
+  return out;
+}
+
+}
+
+#endif