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