2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2008-2017 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Utility.h"
|
|
|
|
|
|
2017-07-08 14:17:21 -07:00
|
|
|
#ifndef _WIN32
|
2016-05-20 17:30:37 -07:00
|
|
|
#include <cxxabi.h>
|
|
|
|
|
#include <execinfo.h>
|
2017-07-08 14:17:21 -07:00
|
|
|
#endif
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2017-07-08 14:17:21 -07:00
|
|
|
#include <cstdio>
|
2016-09-21 23:48:54 -07:00
|
|
|
#include <cstdlib>
|
2016-11-01 20:12:08 -07:00
|
|
|
#include <cstring>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-11-01 20:12:08 -07:00
|
|
|
#include "ErrorBase.h"
|
2016-12-21 21:55:31 -08:00
|
|
|
#include "HAL/DriverStation.h"
|
2016-05-22 21:41:22 -07:00
|
|
|
#include "HAL/HAL.h"
|
2016-12-20 22:08:24 -08:00
|
|
|
#include "llvm/SmallString.h"
|
2017-05-15 23:10:40 -07:00
|
|
|
#include "llvm/raw_ostream.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Assert implementation.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
|
|
|
|
* This allows breakpoints to be set on an assert. The users don't call this,
|
|
|
|
|
* but instead use the wpi_assert macros in Utility.h.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-12-20 22:08:24 -08:00
|
|
|
bool wpi_assert_impl(bool conditionValue, llvm::StringRef conditionText,
|
|
|
|
|
llvm::StringRef message, llvm::StringRef fileName,
|
|
|
|
|
int lineNumber, llvm::StringRef funcName) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!conditionValue) {
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<128> locBuf;
|
|
|
|
|
llvm::raw_svector_ostream locStream(locBuf);
|
2016-02-04 22:29:11 -08:00
|
|
|
locStream << funcName << " [";
|
2016-12-20 22:08:24 -08:00
|
|
|
llvm::SmallString<128> fileTemp;
|
2017-07-08 14:17:21 -07:00
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
char fname[60];
|
|
|
|
|
char ext[10];
|
|
|
|
|
_splitpath_s(fileName.c_str(fileTemp), nullptr, 0, nullptr, 0, fname, 60,
|
|
|
|
|
ext, 10);
|
|
|
|
|
locStream << fname << ":" << lineNumber << "]";
|
|
|
|
|
#elif __APPLE__
|
|
|
|
|
auto file = fileName.c_str(fileTemp);
|
|
|
|
|
int len = std::strlen(file) + 1;
|
|
|
|
|
char* basestr = new char[len + 1];
|
|
|
|
|
std::strncpy(basestr, file, len);
|
|
|
|
|
locStream << basestr << ":" << lineNumber << "]";
|
|
|
|
|
delete[] basestr;
|
|
|
|
|
#else
|
2016-12-20 22:08:24 -08:00
|
|
|
locStream << basename(fileName.c_str(fileTemp)) << ":" << lineNumber << "]";
|
2017-07-08 14:17:21 -07:00
|
|
|
#endif
|
2016-02-04 22:29:11 -08:00
|
|
|
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<128> errorBuf;
|
|
|
|
|
llvm::raw_svector_ostream errorStream(errorBuf);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
errorStream << "Assertion \"" << conditionText << "\" ";
|
|
|
|
|
|
2017-05-10 20:14:14 -07:00
|
|
|
if (!message.empty()) {
|
2017-05-15 23:10:40 -07:00
|
|
|
errorStream << "failed: " << message << "\n";
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
2017-05-15 23:10:40 -07:00
|
|
|
errorStream << "failed.\n";
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2016-02-04 22:29:11 -08:00
|
|
|
std::string stack = GetStackTrace(2);
|
|
|
|
|
std::string location = locStream.str();
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string error = errorStream.str();
|
|
|
|
|
|
|
|
|
|
// Print the error and send it to the DriverStation
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SendError(1, 1, 0, error.c_str(), location.c_str(), stack.c_str(), 1);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conditionValue;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-16 00:33:51 -08:00
|
|
|
* Common error routines for wpi_assertEqual_impl and wpi_assertNotEqual_impl.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* This should not be called directly; it should only be used by
|
2016-05-20 17:30:37 -07:00
|
|
|
* wpi_assertEqual_impl and wpi_assertNotEqual_impl.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-12-20 22:08:24 -08:00
|
|
|
void wpi_assertEqual_common_impl(llvm::StringRef valueA, llvm::StringRef valueB,
|
|
|
|
|
llvm::StringRef equalityType,
|
|
|
|
|
llvm::StringRef message,
|
|
|
|
|
llvm::StringRef fileName, int lineNumber,
|
|
|
|
|
llvm::StringRef funcName) {
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<128> locBuf;
|
|
|
|
|
llvm::raw_svector_ostream locStream(locBuf);
|
2016-02-04 22:29:11 -08:00
|
|
|
locStream << funcName << " [";
|
2016-12-20 22:08:24 -08:00
|
|
|
llvm::SmallString<128> fileTemp;
|
2017-07-08 14:17:21 -07:00
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
char fname[60];
|
|
|
|
|
char ext[10];
|
|
|
|
|
_splitpath_s(fileName.c_str(fileTemp), nullptr, 0, nullptr, 0, fname, 60, ext,
|
|
|
|
|
10);
|
|
|
|
|
locStream << fname << ":" << lineNumber << "]";
|
|
|
|
|
#elif __APPLE__
|
|
|
|
|
auto file = fileName.c_str(fileTemp);
|
|
|
|
|
int len = std::strlen(file) + 1;
|
|
|
|
|
char* basestr = new char[len + 1];
|
|
|
|
|
std::strncpy(basestr, file, len);
|
|
|
|
|
locStream << basestr << ":" << lineNumber << "]";
|
|
|
|
|
delete[] basestr;
|
|
|
|
|
#else
|
2016-12-20 22:08:24 -08:00
|
|
|
locStream << basename(fileName.c_str(fileTemp)) << ":" << lineNumber << "]";
|
2017-07-08 14:17:21 -07:00
|
|
|
#endif
|
2016-02-04 22:29:11 -08:00
|
|
|
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<128> errorBuf;
|
|
|
|
|
llvm::raw_svector_ostream errorStream(errorBuf);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
errorStream << "Assertion \"" << valueA << " " << equalityType << " "
|
|
|
|
|
<< valueB << "\" ";
|
|
|
|
|
|
2017-05-10 20:14:14 -07:00
|
|
|
if (!message.empty()) {
|
2017-05-15 23:10:40 -07:00
|
|
|
errorStream << "failed: " << message << "\n";
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
2017-05-15 23:10:40 -07:00
|
|
|
errorStream << "failed.\n";
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2016-02-04 22:29:11 -08:00
|
|
|
std::string trace = GetStackTrace(3);
|
|
|
|
|
std::string location = locStream.str();
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string error = errorStream.str();
|
|
|
|
|
|
|
|
|
|
// Print the error and send it to the DriverStation
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SendError(1, 1, 0, error.c_str(), location.c_str(), trace.c_str(), 1);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assert equal implementation.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
|
|
|
|
* This determines whether the two given integers are equal. If not, the value
|
|
|
|
|
* of each is printed along with an optional message string. The users don't
|
|
|
|
|
* call this, but instead use the wpi_assertEqual macros in Utility.h.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-12-20 22:08:24 -08:00
|
|
|
bool wpi_assertEqual_impl(int valueA, int valueB, llvm::StringRef valueAString,
|
|
|
|
|
llvm::StringRef valueBString, llvm::StringRef message,
|
|
|
|
|
llvm::StringRef fileName, int lineNumber,
|
|
|
|
|
llvm::StringRef funcName) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!(valueA == valueB)) {
|
|
|
|
|
wpi_assertEqual_common_impl(valueAString, valueBString, "==", message,
|
|
|
|
|
fileName, lineNumber, funcName);
|
|
|
|
|
}
|
|
|
|
|
return valueA == valueB;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assert not equal implementation.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
|
|
|
|
* This determines whether the two given integers are equal. If so, the value of
|
|
|
|
|
* each is printed along with an optional message string. The users don't call
|
|
|
|
|
* this, but instead use the wpi_assertNotEqual macros in Utility.h.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-12-20 22:08:24 -08:00
|
|
|
bool wpi_assertNotEqual_impl(int valueA, int valueB,
|
|
|
|
|
llvm::StringRef valueAString,
|
|
|
|
|
llvm::StringRef valueBString,
|
|
|
|
|
llvm::StringRef message, llvm::StringRef fileName,
|
|
|
|
|
int lineNumber, llvm::StringRef funcName) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!(valueA != valueB)) {
|
|
|
|
|
wpi_assertEqual_common_impl(valueAString, valueBString, "!=", message,
|
|
|
|
|
fileName, lineNumber, funcName);
|
|
|
|
|
}
|
|
|
|
|
return valueA != valueB;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Return the FPGA Version number.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* For now, expect this to be competition year.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return FPGA Version number.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetFPGAVersion() {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int version = HAL_GetFPGAVersion(&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setGlobalErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return version;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the FPGA Revision number.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
|
|
|
|
* The format of the revision is 3 numbers. The 12 most significant bits are the
|
|
|
|
|
* Major Revision. The next 8 bits are the Minor Revision. The 12 least
|
|
|
|
|
* significant bits are the Build Number.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return FPGA Revision number.
|
|
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
int64_t GetFPGARevision() {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-12 10:45:14 -07:00
|
|
|
int64_t revision = HAL_GetFPGARevision(&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setGlobalErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return revision;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the microsecond-resolution timer on the FPGA.
|
2014-07-28 16:25:37 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return The current time in microseconds according to the FPGA (since FPGA
|
2016-05-20 17:30:37 -07:00
|
|
|
* reset).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-12-30 19:06:47 -08:00
|
|
|
uint64_t GetFPGATime() {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
uint64_t time = HAL_GetFPGATime(&status);
|
|
|
|
|
wpi_setGlobalErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return time;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-29 09:19:43 -07:00
|
|
|
* Get the state of the "USER" button on the roboRIO.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return True if the button is currently pressed down
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool GetUserButton() {
|
|
|
|
|
int32_t status = 0;
|
2014-07-29 14:34:52 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
bool value = HAL_GetFPGAButton(&status);
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setGlobalError(status);
|
2014-07-29 14:34:52 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-07-08 14:17:21 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
2014-07-28 16:25:37 -04:00
|
|
|
/**
|
|
|
|
|
* Demangle a C++ symbol, used for printing stack traces.
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
static std::string demangle(char const* mangledSymbol) {
|
2015-06-25 15:07:55 -04:00
|
|
|
char buffer[256];
|
|
|
|
|
size_t length;
|
2016-09-06 00:01:45 -07:00
|
|
|
int32_t status;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-09-21 23:48:54 -07:00
|
|
|
if (std::sscanf(mangledSymbol, "%*[^(]%*[(]%255[^)+]", buffer)) {
|
2016-05-20 17:30:37 -07:00
|
|
|
char* symbol = abi::__cxa_demangle(buffer, nullptr, &length, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
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;
|
2014-07-28 16:25:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a stack trace, ignoring the first "offset" symbols.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param offset The number of symbols at the top of the stack to ignore
|
2014-07-28 16:25:37 -04:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
std::string GetStackTrace(int offset) {
|
2016-05-20 17:30:37 -07:00
|
|
|
void* stackTrace[128];
|
2015-06-25 15:07:55 -04:00
|
|
|
int stackSize = backtrace(stackTrace, 128);
|
2016-05-20 17:30:37 -07:00
|
|
|
char** mangledSymbols = backtrace_symbols(stackTrace, stackSize);
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<1024> buf;
|
|
|
|
|
llvm::raw_svector_ostream trace(buf);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
for (int i = offset; i < stackSize; i++) {
|
|
|
|
|
// Only print recursive functions once in a row.
|
|
|
|
|
if (i == 0 || stackTrace[i] != stackTrace[i - 1]) {
|
2017-05-15 23:10:40 -07:00
|
|
|
trace << "\tat " << demangle(mangledSymbols[i]) << "\n";
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 23:48:54 -07:00
|
|
|
std::free(mangledSymbols);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
return trace.str();
|
2014-07-28 16:25:37 -04:00
|
|
|
}
|
2016-11-01 22:33:12 -07:00
|
|
|
|
2017-07-08 14:17:21 -07:00
|
|
|
#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
|
|
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
} // namespace frc
|