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:
Peter Johnson
2019-08-12 23:45:45 -07:00
committed by GitHub
parent ef037457e5
commit f1d71da8a9
15 changed files with 1809 additions and 73 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -8,6 +8,7 @@
#include "frc/Error.h"
#include <wpi/Path.h>
#include <wpi/StackTrace.h>
#include "frc/DriverStation.h"
#include "frc/Timer.h"
@@ -84,7 +85,7 @@ void Error::Report() {
true, m_code, m_message,
m_function + wpi::Twine(" [") + wpi::sys::path::filename(m_filename) +
wpi::Twine(':') + wpi::Twine(m_lineNumber) + wpi::Twine(']'),
GetStackTrace(4));
wpi::GetStackTrace(4));
}
void Error::Clear() {

View File

@@ -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