|
4 | 4 |
|
5 | 5 | #include "wpi/StackTrace.h"
|
6 | 6 |
|
| 7 | +#ifdef __cpp_lib_stacktrace |
| 8 | +#include <stacktrace> |
| 9 | +#elif !defined(_WIN32) |
7 | 10 | #include <execinfo.h>
|
| 11 | +#endif |
8 | 12 |
|
9 | 13 | #include "wpi/Demangle.h"
|
10 | 14 | #include "wpi/SmallString.h"
|
|
14 | 18 | namespace wpi {
|
15 | 19 |
|
16 | 20 | std::string GetStackTraceDefault(int offset) {
|
| 21 | + wpi::SmallString<1024> buf; |
| 22 | + wpi::raw_svector_ostream trace(buf); |
| 23 | + |
| 24 | +#ifdef __cpp_lib_stacktrace |
| 25 | + auto stackTrace = std::stacktrace::current(); |
| 26 | + |
| 27 | + for (size_t i = offset; i < stackTrace.size(); ++i) { |
| 28 | + // Only print recursive functions once in a row |
| 29 | + if (i == 0 || stackTrace[i] != stackTrace[i - 1]) { |
| 30 | + trace << "\tat " << std::to_string(stackTrace[i]) << '\n'; |
| 31 | + } |
| 32 | + } |
| 33 | +#elif !defined(_WIN32) |
17 | 34 | void* stackTrace[128];
|
18 | 35 | int stackSize = backtrace(stackTrace, 128);
|
19 | 36 | char** mangledSymbols = backtrace_symbols(stackTrace, stackSize);
|
20 |
| - wpi::SmallString<1024> buf; |
21 |
| - wpi::raw_svector_ostream trace(buf); |
22 | 37 |
|
23 |
| - for (int i = offset; i < stackSize; i++) { |
24 |
| - // Only print recursive functions once in a row. |
| 38 | + for (int i = offset; i < stackSize; ++i) { |
| 39 | + // Only print recursive functions once in a row |
25 | 40 | if (i == 0 || stackTrace[i] != stackTrace[i - 1]) {
|
26 |
| - // extract just function name from "pathToExe(functionName+offset)" |
| 41 | + // Extract just function name from "pathToExe(functionName+offset)" |
27 | 42 | std::string_view sym = split(mangledSymbols[i], '(').second;
|
28 | 43 | std::string_view offset;
|
29 | 44 | std::tie(sym, offset) = split(sym, '+');
|
30 | 45 | std::string_view addr;
|
31 | 46 | std::tie(offset, addr) = split(offset, ')');
|
32 |
| - trace << "\tat " << Demangle(sym) << " + " << offset << addr << "\n"; |
| 47 | + |
| 48 | + trace << "\tat " << Demangle(sym) << " + " << offset << addr << '\n'; |
33 | 49 | }
|
34 | 50 | }
|
35 | 51 |
|
36 | 52 | std::free(mangledSymbols);
|
| 53 | +#endif |
37 | 54 |
|
38 | 55 | return std::string{trace.str()};
|
39 | 56 | }
|
|
0 commit comments