Files
allwpilib/wpilibc/wpilibC++Devices/src/Utility.cpp

272 lines
7.3 KiB
C++
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "Utility.h"
//#include "NetworkCommunication/FRCComm.h"
#include "HAL/HAL.hpp"
#include "Task.h"
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <execinfo.h>
#include <cxxabi.h>
#include "nivision.h"
static bool suspendOnAssertEnabled = false;
/**
* Enable suspend on assert.
* If enabled, the user task will be suspended whenever an assert fails. This
* will allow the user to attach to the task with the debugger and examine variables
* around the failure.
*/
void wpi_suspendOnAssertEnabled(bool enabled)
{
suspendOnAssertEnabled = enabled;
}
/**
* Assert implementation.
* 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.
*/
bool wpi_assert_impl(bool conditionValue,
const char *conditionText,
const char *message,
const char *fileName,
uint32_t lineNumber,
const char *funcName)
{
if(!conditionValue)
{
std::stringstream errorStream;
errorStream << "Assertion \"" << conditionText << "\" ";
errorStream << "on line " << lineNumber << " ";
errorStream << "of "<< basename(fileName) << " ";
if(message)
{
errorStream << "failed: " << message << std::endl;
}
else
{
errorStream << "failed." << std::endl;
}
errorStream << GetStackTrace(2);
std::string error = errorStream.str();
// Print the error and send it to the DriverStation
std::cout << error << std::endl;
HALSetErrorData(error.c_str(), error.size(), 100);
if (suspendOnAssertEnabled) suspendTask(0);
}
return conditionValue;
}
/**
* Common error routines for wpi_assertEqual_impl and wpi_assertNotEqual_impl
* This should not be called directly; it should only be used by wpi_assertEqual_impl
* and wpi_assertNotEqual_impl.
*/
void wpi_assertEqual_common_impl(const char *valueA,
const char *valueB,
const char *equalityType,
const char *message,
const char *fileName,
uint32_t lineNumber,
const char *funcName)
{
std::stringstream errorStream;
errorStream << "Assertion \"" << valueA << " " << equalityType << " " << valueB << "\" ";
errorStream << "on line " << lineNumber << " ";
errorStream << "of "<< basename(fileName) << " ";
if(message)
{
errorStream << "failed: " << message << std::endl;
}
else
{
errorStream << "failed." << std::endl;
}
errorStream << GetStackTrace(3);
std::string error = errorStream.str();
// Print the error and send it to the DriverStation
std::cout << error << std::endl;
HALSetErrorData(error.c_str(), error.size(), 100);
if (suspendOnAssertEnabled) suspendTask(0);
}
/**
* Assert equal implementation.
* 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.
*/
bool wpi_assertEqual_impl(int valueA,
int valueB,
const char *valueAString,
const char *valueBString,
const char *message,
const char *fileName,
uint32_t lineNumber,
const char *funcName)
{
if(!(valueA == valueB))
{
wpi_assertEqual_common_impl(valueAString, valueBString,
"==", message, fileName, lineNumber, funcName);
}
return valueA == valueB;
}
/**
* Assert not equal implementation.
* 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.
*/
bool wpi_assertNotEqual_impl(int valueA,
int valueB,
const char *valueAString,
const char *valueBString,
const char *message,
const char *fileName,
uint32_t lineNumber,
const char *funcName)
{
if(!(valueA != valueB))
{
wpi_assertEqual_common_impl(valueAString, valueBString,
"!=", message, fileName, lineNumber, funcName);
}
return valueA != valueB;
}
/**
* Return the FPGA Version number.
* For now, expect this to be competition year.
* @return FPGA Version number.
*/
uint16_t GetFPGAVersion()
{
int32_t status = 0;
uint16_t version = getFPGAVersion(&status);
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
return version;
}
/**
* Return the FPGA Revision number.
* 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.
* @return FPGA Revision number.
*/
uint32_t GetFPGARevision()
{
int32_t status = 0;
uint32_t revision = getFPGARevision(&status);
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
return revision;
}
/**
* Read the microsecond-resolution timer on the FPGA.
*
* @return The current time in microseconds according to the FPGA (since FPGA reset).
*/
uint32_t GetFPGATime()
{
int32_t status = 0;
uint32_t time = getFPGATime(&status);
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
return time;
}
/**
* Get the state of the "USER" button on the RoboRIO
* @return True if the button is currently pressed down
*/
bool GetUserButton()
{
int32_t status = 0;
bool value = getFPGAButton(&status);
wpi_setGlobalError(status);
return value;
}
/**
* Demangle a C++ symbol, used for printing stack traces.
*/
static std::string demangle(char const *mangledSymbol)
{
char buffer[256];
size_t length;
int status;
Pass errors to DS in C++ and Java Squashed commit of the following: commit f317b3522e312cf7e7bb9eb0494f2f96a7f6363c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 17:15:46 2014 -0400 Send unhandled exceptions back to the DS. Change-Id: I0e658fdb6d43593ee20457f20f71f4f4cd2d21c3 commit f834ef8c791945697ad483c27b4167eb917ac242 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 16:05:24 2014 -0400 Add StackTrace to Java errors Change-Id: I83b162afcc5f294703705770fbcd8623b0895539 commit 02e040b0c79067ce046ada29e26004e0460fceb0 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 15:07:44 2014 -0400 HAL Errors to DS in Java Change-Id: I5fb51e4066bbc26ea59ca513c03c5ec5ace98831 commit 03775ddc42b129c27fdf403f17f0796009311c3c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 13:38:18 2014 -0400 Update AnalogInput to report errors for getting and setting sample rate Change-Id: I00eb78f52fc5b17a60bc84456f0ec9842cc40ef7 commit 4c10cb79612ae81e3cbb6bd4d6da8cf3b8955821 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 11:46:03 2014 -0400 Define errors in HAL Change-Id: I96595472e42ba61f0f3d0da17caf01a748d0422a commit 56cb5dcd93e5e849a016f63ac9d0dc245a23eb2b Author: Kevin O'Connor <koconnor@usfirst.org> Date: Fri Oct 17 10:59:29 2014 -0400 Throttle errors (1 report per second per error code) and fix issue with GetTime conflicting with GetTime from Timer.h/Timer.cpp Change-Id: Ibe4dc2e400fc4671b240b876a46959256ea65ad7 commit 71c78826e548682ecd0c1548255f8a6552cece32 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Thu Oct 16 16:41:04 2014 -0400 Feed errors to DS from C++ Change-Id: I009a7798499fd93e9fdd976ff00aa74c0bd094ae commit 81030c6cee7f18a5ddf0e95c4e402a6cf7b5de6c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Thu Oct 16 16:40:50 2014 -0400 Don't try to de-mangle lines without any symbols in them Change-Id: Icea02494b68f2ec9116d6cbf20a35a3a132234f8 Change-Id: If7717025b03914183736ccd95da5c9d49819a6f3
2014-10-20 17:19:28 -04:00
if(sscanf(mangledSymbol, "%*[^(]%*[(]%255[^)+]", buffer))
{
char *symbol = abi::__cxa_demangle(buffer, NULL, &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;
}
/**
* Get a stack trace, ignoring the first "offset" symbols.
* @param offset The number of symbols at the top of the stack to ignore
*/
std::string GetStackTrace(uint32_t offset)
{
void *stackTrace[128];
int stackSize = backtrace(stackTrace, 128);
char **mangledSymbols = backtrace_symbols(stackTrace, stackSize);
std::stringstream trace;
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]) << std::endl;
}
}
free(mangledSymbols);
return trace.str();
}