backtrace.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "crucible/process/backtrace.hpp" #include "crucible/core.hpp" #ifdef CRUCIBLE_UNIX #include "crucible/unix.hpp" #endif // CRUCIBLE_UNIX #ifdef CRUCIBLE_WINDOWS #include "crucible/windows.hpp" #endif // CRUCIBLE_WINDOWS #include <cstdlib> #include <exception> #include <iostream> namespace crucible::process::backtrace { namespace { [[noreturn]] auto abort_with_detailed_backtrace(std::vector<DetailedStackFrame> const &frames) -> void { std::cerr << "Full backtrace of the calling thread:\n" << "─────────────────────────────────────\n"; for (std::size_t i { 0 }; i < frames.size(); ++i) { auto const &frame { frames.at(i) }; std::cerr << "• #" << i << ": " << frame.describe() << "\n"; } std::cerr << "\n"; std::abort(); } auto print_failed_detailed_backtrace(process::errors::Error const &error) -> void { std::cerr << "Failed to get full backtrace of the calling thread:\n" << "───────────────────────────────────────────────────\n" << "• " << error.describe() << "\n" << "\n"; } [[noreturn]] auto abort_with_simple_backtrace(std::vector<SimpleStackFrame> const &frames) -> void { std::cerr << "Simple backtrace of the calling thread:\n" << "───────────────────────────────────────\n"; for (std::size_t i { 0 }; i < frames.size(); ++i) { auto const &frame { frames.at(i) }; std::cerr << "• #" << i << ": " << frame.describe() << "\n"; } std::cerr << "\n"; std::abort(); } auto print_failed_simple_backtrace(process::errors::Error const &error) -> void { std::cerr << "Failed to get simple backtrace of the calling thread:\n" << "─────────────────────────────────────────────────────\n" << "• " << error.describe() << "\n" << "\n"; } } DetailedStackFrame::DetailedStackFrame(std::string const &function, std::string const &file, std::size_t const line) : my_function { function }, my_file { file }, my_line { line } {} auto DetailedStackFrame::get_function() const -> std::string { return my_function; } auto DetailedStackFrame::get_file() const -> std::string { return my_file; } auto DetailedStackFrame::get_line() const -> std::size_t { return my_line; } auto DetailedStackFrame::describe() const -> std::string { static std::string const our_root { CRUCIBLE_ROOT }; if (my_file.empty() or my_line == 0) { return my_function + " at <unknown>"; } auto const root_index { my_file.find(our_root) }; auto const relative_file { root_index == 0 ? my_file.substr(our_root.size() + 1) : my_file }; return my_function + " at " + relative_file + ":" + std::to_string(my_line); } auto DetailedStackFrame::format() const -> std::string { return "crucible::process::backtrace::DetailedStackFrame { my_function: " + core::format::format_std_string(my_function) + ", my_file: " + core::format::format_std_string(my_file) + ", my_line: " + core::format::format_integral(my_line) + " }"; } SimpleStackFrame::SimpleStackFrame(const std::string &function, std::string const &details) : my_function { function }, my_details { details } {} auto SimpleStackFrame::get_function() const -> std::string { return my_function; } auto SimpleStackFrame::get_details() const -> std::string { return my_details; } auto SimpleStackFrame::describe() const -> std::string { return my_function + (my_details.empty() ? "" : " (" + my_details + ")"); } auto SimpleStackFrame::format() const -> std::string { return "crucible::process::backtrace::SimpleStackFrame { my_function: " + core::format::format_std_string(my_function) + ", my_details: " + core::format::format_std_string(my_details) + " }"; } #ifdef CRUCIBLE_UNIX auto take_detailed_snapshot() -> core::result::Result<process::errors::Error, std::vector<DetailedStackFrame>> { return core::result::make_failure<process::errors::Error>(process::errors::MissingDebugInformation {}); } auto take_simple_snapshot() -> core::result::Result<process::errors::Error, std::vector<SimpleStackFrame>> { auto const snapshot { unix::backtrace::snapshot() }; if (snapshot.empty()) { return core::result::make_failure<process::errors::Error>(process::errors::EmptyBacktrace {}); } std::vector<SimpleStackFrame> result {}; for (auto const &frame : snapshot) { result.emplace_back(frame.get_function(), frame.get_details()); } return core::result::make_success(result); } #endif // CRUCIBLE_UNIX #ifdef CRUCIBLE_WINDOWS auto take_detailed_snapshot() -> core::result::Result<process::errors::Error, std::vector<DetailedStackFrame>> { auto const backtrace { windows::backtrace::snapshot() }; if (backtrace.empty()) { return core::result::make_failure<process::errors::Error>(process::errors::EmptyBacktrace {}); } std::vector<DetailedStackFrame> result {}; for (auto const &frame : backtrace) { result.emplace_back(frame.get_function(), frame.get_file(), frame.get_line()); } return core::result::make_success(result); } auto take_simple_snapshot() -> core::result::Result<process::errors::Error, std::vector<SimpleStackFrame>> { auto const backtrace { windows::backtrace::snapshot() }; if (backtrace.empty()) { return core::result::make_failure<process::errors::Error>(process::errors::EmptyBacktrace {}); } std::vector<SimpleStackFrame> result {}; for (auto const &frame : backtrace) { result.emplace_back(frame.get_function(), ""); } return core::result::make_success(result); } #endif // CRUCIBLE_WINDOWS auto terminate_handler() -> void { std::cerr <<"\n" << "╭────────────────────────────────────────────────────────────╮\n" << "│ std::terminate() called (this is a crash!) │\n" << "│ │\n" << "│ Here's all the information I could find about the problem: │\n" << "╰────────────────────────────────────────────────────────────╯\n" << "\n"; auto const exception { std::current_exception() }; try { if (exception) { std::rethrow_exception(exception); } std::cerr << "No std::exception objects are currently being thrown in the calling thread\n" << "──────────────────────────────────────────────────────────────────────────\n" << "\n"; } catch (std::exception const &e) { std::cerr << "Uncaught std::exception object in the calling thread:\n" << "─────────────────────────────────────────────────────\n" << "• typeid(): " << typeid(e).name() << "\n" << "• what(): " << e.what() << "\n" << "\n"; } // `abort_with_detailed_backtrace()` will exit if we end up calling it auto const detailed_backtrace { take_detailed_snapshot() }; detailed_backtrace.match(print_failed_detailed_backtrace, abort_with_detailed_backtrace); // `abort_with_simple_backtrace()` will exit if we end up calling it auto const simple_backtrace { take_simple_snapshot() }; simple_backtrace.match(print_failed_simple_backtrace, abort_with_simple_backtrace); // If we failed both backtraces, just give up std::abort(); } }