blob: 8c9c6aed41f7e20e5159a4e219658ad83fa7bb67 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
// file : libbutl/backtrace.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#ifndef __cpp_modules_ts
#include <libbutl/backtrace.mxx>
#endif
// We only enable backtrace during bootstrap if we can do it without any
// complications of the build scripts/makefiles.
//
// With glibc linking with -rdynamic gives (non-static) function names.
// FreeBSD/NetBSD requires explicitly linking -lexecinfo.
//
// Note that some libc implementation on Linux (most notably, musl), don't
// support this, at least not out of the box.
//
#ifndef BUILD2_BOOTSTRAP
# if defined(__GLIBC__) || \
defined(__APPLE__) || \
defined(__FreeBSD__) || \
defined(__NetBSD__)
# define LIBBUTL_BACKTRACE
# endif
#else
# if defined(__GLIBC__) || \
defined(__APPLE__)
# define LIBBUTL_BACKTRACE
# endif
#endif
#ifdef LIBBUTL_BACKTRACE
# include <stdlib.h> // free()
# include <execinfo.h>
#endif
#include <cassert>
#ifndef __cpp_lib_modules_ts
#include <string>
#ifdef LIBBUTL_BACKTRACE
# include <memory> // unique_ptr
# include <cstddef> // size_t
#endif
#include <exception>
#endif
// Other includes.
#ifdef __cpp_modules_ts
module butl.backtrace;
// Only imports additional to interface.
#ifdef __clang__
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
#endif
#endif
using namespace std;
namespace butl
{
string
backtrace () noexcept
try
{
string r;
#ifdef LIBBUTL_BACKTRACE
// Note: backtrace() returns int on Linux and MacOS and size_t on *BSD.
//
void* buf[1024];
auto n (::backtrace (buf, 1024));
assert (n >= 0);
char** fs (backtrace_symbols (buf, n)); // Note: returns NULL on error.
if (fs != nullptr)
{
unique_ptr<char*, void (*)(char**)> deleter (
fs, [] (char** s) {::free (s);});
for (size_t i (0); i != static_cast<size_t> (n); ++i)
{
r += fs[i];
r += '\n';
}
}
#endif
return r;
}
catch (const std::exception&)
{
return string ();
}
}
|