James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "wpi/StackTrace.h" |
| 9 | |
| 10 | #include <execinfo.h> |
| 11 | |
| 12 | #include "wpi/Demangle.h" |
| 13 | #include "wpi/SmallString.h" |
| 14 | #include "wpi/raw_ostream.h" |
| 15 | |
| 16 | namespace wpi { |
| 17 | |
| 18 | std::string GetStackTrace(int offset) { |
| 19 | void* stackTrace[128]; |
| 20 | int stackSize = backtrace(stackTrace, 128); |
| 21 | char** mangledSymbols = backtrace_symbols(stackTrace, stackSize); |
| 22 | wpi::SmallString<1024> buf; |
| 23 | wpi::raw_svector_ostream trace(buf); |
| 24 | |
| 25 | for (int i = offset; i < stackSize; i++) { |
| 26 | // Only print recursive functions once in a row. |
| 27 | if (i == 0 || stackTrace[i] != stackTrace[i - 1]) { |
| 28 | trace << "\tat " << Demangle(mangledSymbols[i]) << "\n"; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | std::free(mangledSymbols); |
| 33 | |
| 34 | return trace.str(); |
| 35 | } |
| 36 | |
| 37 | } // namespace wpi |