-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktrace.cpp
More file actions
104 lines (83 loc) · 2.35 KB
/
Copy pathstacktrace.cpp
File metadata and controls
104 lines (83 loc) · 2.35 KB
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
// Eggs.Stacktrace
//
// Copyright (c) 2026 Agustin Berge
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <eggs/stacktrace.hpp>
#include <cstdint>
#include <limits>
#include <sstream>
#include <string>
#include "detail/backend.hpp"
namespace eggs {
// -- stacktrace::current --------------------------------------------------------
stacktrace stacktrace::current() noexcept
{
stacktrace st;
detail::capture(st.frames_, 1, std::numeric_limits<size_type>::max());
return st;
}
stacktrace stacktrace::current(size_type skip) noexcept
{
stacktrace st;
detail::capture(
st.frames_, skip + 1, std::numeric_limits<size_type>::max()
);
return st;
}
stacktrace stacktrace::current(size_type skip, size_type max_depth) noexcept
{
stacktrace st;
detail::capture(st.frames_, skip + 1, max_depth);
return st;
}
// -- stacktrace_entry accessors ------------------------------------------------
std::string stacktrace_entry::description() const
{
return detail::symbolize_description(address_);
}
std::string stacktrace_entry::source_file() const
{
return detail::symbolize_source_file(address_);
}
std::uint_least32_t stacktrace_entry::source_line() const
{
return detail::symbolize_source_line(address_);
}
// -- to_string(stacktrace_entry) -----------------------------------------------
std::string to_string(stacktrace_entry entry)
{
if (!entry) return {};
std::ostringstream oss;
oss << entry.native_handle();
std::string result = oss.str();
std::string const desc = entry.description();
if (!desc.empty()) {
result += " in ";
result += desc;
}
std::string const file = entry.source_file();
if (!file.empty()) {
result += " at ";
result += file;
std::uint_least32_t const line = entry.source_line();
if (line != 0) {
result += ':';
result += std::to_string(line);
}
}
return result;
}
// -- to_string(stacktrace) -------------------------------------------------------
std::string to_string(stacktrace const& st)
{
std::string result;
for (std::size_t i = 0; i < st.size(); ++i) {
result += to_string(st[i]);
result += '\n';
}
return result;
}
} // namespace eggs