mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Move GetStackTrace and Demangle to wpiutil, add Windows support (#1819)
For Windows, import the StackWalker source (https://github.com/JochenKalmbach/StackWalker) plus PR 35 in that repo, with a few simplifications to StackWalker.h.
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include <hal/HAL.h>
|
||||
#include <wpi/Path.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/StackTrace.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "frc/ErrorBase.h"
|
||||
@@ -47,7 +48,7 @@ bool wpi_assert_impl(bool conditionValue, const wpi::Twine& conditionText,
|
||||
errorStream << "failed: " << message << "\n";
|
||||
}
|
||||
|
||||
std::string stack = GetStackTrace(2);
|
||||
std::string stack = wpi::GetStackTrace(2);
|
||||
|
||||
// Print the error and send it to the DriverStation
|
||||
HAL_SendError(1, 1, 0, errorBuf.c_str(), locBuf.c_str(), stack.c_str(), 1);
|
||||
@@ -86,7 +87,7 @@ void wpi_assertEqual_common_impl(const wpi::Twine& valueA,
|
||||
errorStream << "failed: " << message << "\n";
|
||||
}
|
||||
|
||||
std::string trace = GetStackTrace(3);
|
||||
std::string trace = wpi::GetStackTrace(3);
|
||||
|
||||
// Print the error and send it to the DriverStation
|
||||
HAL_SendError(1, 1, 0, errorBuf.c_str(), locBuf.c_str(), trace.c_str(), 1);
|
||||
@@ -115,59 +116,3 @@ bool wpi_assertNotEqual_impl(int valueA, int valueB,
|
||||
}
|
||||
return valueA != valueB;
|
||||
}
|
||||
|
||||
namespace frc {
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
/**
|
||||
* Demangle a C++ symbol, used for printing stack traces.
|
||||
*/
|
||||
static std::string demangle(char const* mangledSymbol) {
|
||||
char buffer[256];
|
||||
size_t length;
|
||||
int32_t status;
|
||||
|
||||
if (std::sscanf(mangledSymbol, "%*[^(]%*[(]%255[^)+]", buffer)) {
|
||||
char* symbol = abi::__cxa_demangle(buffer, nullptr, &length, &status);
|
||||
if (status == 0) {
|
||||
return symbol;
|
||||
} else {
|
||||
// If the symbol couldn't be demangled, it's probably a C function,
|
||||
// so just return it as-is.
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
// If everything else failed, just return the mangled symbol
|
||||
return mangledSymbol;
|
||||
}
|
||||
|
||||
std::string GetStackTrace(int offset) {
|
||||
void* stackTrace[128];
|
||||
int stackSize = backtrace(stackTrace, 128);
|
||||
char** mangledSymbols = backtrace_symbols(stackTrace, stackSize);
|
||||
wpi::SmallString<1024> buf;
|
||||
wpi::raw_svector_ostream trace(buf);
|
||||
|
||||
for (int i = offset; i < stackSize; i++) {
|
||||
// Only print recursive functions once in a row.
|
||||
if (i == 0 || stackTrace[i] != stackTrace[i - 1]) {
|
||||
trace << "\tat " << demangle(mangledSymbols[i]) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
std::free(mangledSymbols);
|
||||
|
||||
return trace.str();
|
||||
}
|
||||
|
||||
#else
|
||||
static std::string demangle(char const* mangledSymbol) {
|
||||
return "no demangling on windows";
|
||||
}
|
||||
|
||||
std::string GetStackTrace(int offset) { return "no stack trace on windows"; }
|
||||
#endif
|
||||
|
||||
} // namespace frc
|
||||
|
||||
Reference in New Issue
Block a user