mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
Merge "Added stack traces and better error reporting in C++"
This commit is contained in:
@@ -58,6 +58,7 @@
|
||||
</option>
|
||||
<option id="gnu.cpp.link.option.flags.1747959472" name="Linker flags" superClass="gnu.cpp.link.option.flags" value="-Wl,-rpath-link,$cpp-location/lib" valueType="string"/>
|
||||
<option id="gnu.cpp.link.option.other.1891020896" name="Other options (-Xlinker [option])" superClass="gnu.cpp.link.option.other" valueType="stringList">
|
||||
<listOptionValue builtIn="false" value="-export-dynamic"/>
|
||||
</option>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1757265359" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
|
||||
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __vxworks
|
||||
#include <vxWorks.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void printCurrentStackTrace();
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
|
||||
#include "HAL/cpp/StackTrace.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
void printCurrentStackTrace() {
|
||||
// TODO: Implement
|
||||
}
|
||||
@@ -33,10 +33,6 @@ public:
|
||||
void Clear();
|
||||
void Set(Code code, const char* contextMessage, const char* filename, const char *function,
|
||||
uint32_t lineNumber, const ErrorBase* originatingObject);
|
||||
static void EnableStackTrace(bool enable)
|
||||
{
|
||||
m_stackTraceEnabled = enable;
|
||||
}
|
||||
static void EnableSuspendOnError(bool enable)
|
||||
{
|
||||
m_suspendOnErrorEnabled = enable;
|
||||
@@ -53,6 +49,5 @@ private:
|
||||
const ErrorBase* m_originatingObject;
|
||||
double m_timestamp;
|
||||
|
||||
static bool m_stackTraceEnabled;
|
||||
static bool m_suspendOnErrorEnabled;DISALLOW_COPY_AND_ASSIGN(Error);
|
||||
};
|
||||
|
||||
@@ -6,22 +6,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "HAL/HAL.hpp"
|
||||
#include <string>
|
||||
|
||||
#define wpi_assert(condition) wpi_assert_impl(condition, #condition, NULL, __FILE__, __LINE__, __FUNCTION__)
|
||||
#define wpi_assertWithMessage(condition, message) wpi_assert_impl(condition, #condition, message, __FILE__, __LINE__, __FUNCTION__)
|
||||
|
||||
#define wpi_assertEqual(a, b) wpi_assertEqual_impl(a, b, NULL, __FILE__, __LINE__, __FUNCTION__)
|
||||
#define wpi_assertEqualWithMessage(a, b, message) wpi_assertEqual_impl(a, b, message, __FILE__, __LINE__, __FUNCTION__)
|
||||
#define wpi_assertEqual(a, b) wpi_assertEqual_impl(a, b, #a, #b, NULL, __FILE__, __LINE__, __FUNCTION__)
|
||||
#define wpi_assertEqualWithMessage(a, b, message) wpi_assertEqual_impl(a, b, #a, #b, message, __FILE__, __LINE__, __FUNCTION__)
|
||||
|
||||
#define wpi_assertNotEqual(a, b) wpi_assertNotEqual_impl(a, b, NULL, __FILE__, __LINE__, __FUNCTION__)
|
||||
#define wpi_assertNotEqualWithMessage(a, b, message) wpi_assertNotEqual_impl(a, b, message, __FILE__, __LINE__, __FUNCTION__)
|
||||
#define wpi_assertNotEqual(a, b) wpi_assertNotEqual_impl(a, b, #a, #b, NULL, __FILE__, __LINE__, __FUNCTION__)
|
||||
#define wpi_assertNotEqualWithMessage(a, b, message) wpi_assertNotEqual_impl(a, b, #a, #b, message, __FILE__, __LINE__, __FUNCTION__)
|
||||
|
||||
bool wpi_assert_impl(bool conditionValue, const char *conditionText, const char *message, const char *fileName, uint32_t lineNumber, const char *funcName);
|
||||
bool wpi_assertEqual_impl(int valueA, int valueB, const char *message, const char *fileName,uint32_t lineNumber, const char *funcName);
|
||||
bool wpi_assertNotEqual_impl(int valueA, int valueB, const char *message, const char *fileName,uint32_t lineNumber, const char *funcName);
|
||||
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);
|
||||
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);
|
||||
|
||||
void wpi_suspendOnAssertEnabled(bool enabled);
|
||||
void wpi_stackTraceOnAssertEnable(bool enabled);
|
||||
|
||||
uint16_t GetFPGAVersion();
|
||||
uint32_t GetFPGARevision();
|
||||
@@ -33,4 +33,4 @@ int32_t ToggleRIOUserLED();
|
||||
void SetRIO_FPGA_LED(uint32_t state);
|
||||
int32_t GetRIO_FPGA_LED();
|
||||
int32_t ToggleRIO_FPGA_LED();
|
||||
|
||||
std::string GetStackTrace(uint32_t offset);
|
||||
|
||||
@@ -6,14 +6,15 @@
|
||||
|
||||
#include "Error.h"
|
||||
|
||||
#include "HAL/cpp/StackTrace.hpp"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
//#include "NetworkCommunication/FRCComm.h"
|
||||
#include "Timer.h"
|
||||
#include "Utility.h"
|
||||
bool Error::m_stackTraceEnabled = false;
|
||||
bool Error::m_suspendOnErrorEnabled = false;
|
||||
|
||||
Error::Error()
|
||||
@@ -75,39 +76,18 @@ void Error::Set(Code code, const char* contextMessage, const char* filename, con
|
||||
|
||||
void Error::Report()
|
||||
{
|
||||
// Error string buffers
|
||||
char *error = new char[256];
|
||||
char *error_with_code = new char[256];
|
||||
std::stringstream errorStream;
|
||||
|
||||
// Build error strings
|
||||
if (m_code != -1)
|
||||
{
|
||||
snprintf(error, 256, "%s: status = %d (0x%08X) %s ...in %s() in %s at line %d\n",
|
||||
m_code < 0 ? "ERROR" : "WARNING", (int32_t)m_code, (uint32_t)m_code, m_message.c_str(),
|
||||
m_function.c_str(), m_filename.c_str(), m_lineNumber);
|
||||
sprintf(error_with_code,"<Code>%d %s", (int32_t)m_code, error);
|
||||
} else {
|
||||
snprintf(error, 256, "ERROR: %s ...in %s() in %s at line %d\n", m_message.c_str(),
|
||||
m_function.c_str(), m_filename.c_str(), m_lineNumber);
|
||||
strcpy(error_with_code, error);
|
||||
}
|
||||
// TODO: Add logging to disk
|
||||
errorStream << "Error on line " << m_lineNumber << " ";
|
||||
errorStream << "of "<< basename(m_filename.c_str()) << ": ";
|
||||
errorStream << m_message << std::endl;
|
||||
errorStream << GetStackTrace(4);
|
||||
|
||||
// Send to the DriverStation
|
||||
HALSetErrorData(error_with_code, strlen(error_with_code), 100);
|
||||
std::string error = errorStream.str();
|
||||
|
||||
delete [] error_with_code;
|
||||
|
||||
// Print to console
|
||||
printf("\n\n>>>>%s", error);
|
||||
|
||||
delete [] error;
|
||||
|
||||
if (m_stackTraceEnabled)
|
||||
{
|
||||
printf("-----------<Stack Trace>----------------\n");
|
||||
printCurrentStackTrace();
|
||||
}
|
||||
// Print the error and send it to the DriverStation
|
||||
std::cout << error << std::endl;
|
||||
HALSetErrorData(error.c_str(), error.size(), 100);
|
||||
}
|
||||
|
||||
void Error::Clear()
|
||||
@@ -120,4 +100,3 @@ void Error::Clear()
|
||||
m_originatingObject = NULL;
|
||||
m_timestamp = 0.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
#include "ErrorBase.h"
|
||||
#include "HAL/cpp/Synchronized.hpp"
|
||||
#include "HAL/cpp/StackTrace.hpp"
|
||||
#include "nivision.h"
|
||||
#define WPI_ERRORS_DEFINE_STRINGS
|
||||
#include "WPIErrors.h"
|
||||
|
||||
@@ -8,23 +8,18 @@
|
||||
|
||||
//#include "NetworkCommunication/FRCComm.h"
|
||||
#include "HAL/HAL.hpp"
|
||||
#include "HAL/cpp/StackTrace.hpp"
|
||||
#include "Task.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <execinfo.h>
|
||||
#include <cxxabi.h>
|
||||
#include "nivision.h"
|
||||
|
||||
static bool stackTraceEnabled = false;
|
||||
static bool suspendOnAssertEnabled = false;
|
||||
|
||||
/**
|
||||
* Enable Stack trace after asserts.
|
||||
*/
|
||||
void wpi_stackTraceOnAssertEnable(bool enabled)
|
||||
{
|
||||
stackTraceEnabled = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable suspend on asssert.
|
||||
* If enabled, the user task will be suspended whenever an assert fails. This
|
||||
@@ -36,50 +31,46 @@ void wpi_suspendOnAssertEnabled(bool enabled)
|
||||
suspendOnAssertEnabled = enabled;
|
||||
}
|
||||
|
||||
static void wpi_handleTracing()
|
||||
{
|
||||
if (stackTraceEnabled)
|
||||
{
|
||||
printf("\n-----------<Stack Trace>----------------\n");
|
||||
printCurrentStackTrace();
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
bool wpi_assert_impl(bool conditionValue,
|
||||
const char *conditionText,
|
||||
const char *message,
|
||||
const char *fileName,
|
||||
uint32_t lineNumber,
|
||||
const char *funcName)
|
||||
{
|
||||
if (!conditionValue)
|
||||
{
|
||||
// Error string buffer
|
||||
char error[256];
|
||||
|
||||
// If an error message was specified, include it
|
||||
// Build error string
|
||||
if(message != NULL) {
|
||||
sprintf(error, "Assertion failed: \"%s\", \"%s\" failed in %s() in %s at line %dd\n",
|
||||
message, conditionText, funcName, fileName, lineNumber);
|
||||
} else {
|
||||
sprintf(error, "Assertion failed: \"%s\" in %s() in %s at line %dd\n",
|
||||
conditionText, funcName, fileName, lineNumber);
|
||||
if(!conditionValue)
|
||||
{
|
||||
std::stringstream errorStream;
|
||||
|
||||
errorStream << "Assertion \"" << conditionText << "\" ";
|
||||
errorStream << "on line " << lineNumber << " ";
|
||||
errorStream << "of "<< basename(fileName) << " ";
|
||||
|
||||
if(message)
|
||||
{
|
||||
errorStream << "failed: " << message << std::endl;
|
||||
}
|
||||
|
||||
// Print to console and send to remote dashboard
|
||||
printf("\n\n>>>>%s", error);
|
||||
HALSetErrorData(error, strlen(error), 100);
|
||||
|
||||
wpi_handleTracing();
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -88,32 +79,37 @@ bool wpi_assert_impl(bool conditionValue,
|
||||
* This should not be called directly; it should only be used by wpi_assertEqual_impl
|
||||
* and wpi_assertNotEqual_impl.
|
||||
*/
|
||||
void wpi_assertEqual_common_impl(int valueA,
|
||||
int valueB,
|
||||
const char *equalityType,
|
||||
const char *message,
|
||||
const char *fileName,
|
||||
uint32_t lineNumber,
|
||||
const char *funcName)
|
||||
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)
|
||||
{
|
||||
// Error string buffer
|
||||
char error[256];
|
||||
|
||||
// If an error message was specified, include it
|
||||
// Build error string
|
||||
if(message != NULL) {
|
||||
sprintf(error, "Assertion failed: \"%s\", \"%d\" %s \"%d\" in %s() in %s at line %d\n",
|
||||
message, valueA, equalityType, valueB, funcName, fileName, lineNumber);
|
||||
} else {
|
||||
sprintf(error, "Assertion failed: \"%d\" %s \"%d\" in %s() in %s at line %d\n",
|
||||
valueA, equalityType, valueB, funcName, fileName, lineNumber);
|
||||
std::stringstream errorStream;
|
||||
|
||||
errorStream << "Assertion \"" << valueA << " " << equalityType << " " << valueB << "\" ";
|
||||
errorStream << "on line " << lineNumber << " ";
|
||||
errorStream << "of "<< basename(fileName) << " ";
|
||||
|
||||
if(message)
|
||||
{
|
||||
errorStream << "failed: " << message << std::endl;
|
||||
}
|
||||
|
||||
// Print to console and send to remote dashboard
|
||||
printf("\n\n>>>>%s", error);
|
||||
HALSetErrorData(error, strlen(error), 100);
|
||||
|
||||
wpi_handleTracing();
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -124,15 +120,18 @@ void wpi_assertEqual_common_impl(int valueA,
|
||||
* 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 *message,
|
||||
const char *fileName,
|
||||
uint32_t lineNumber,
|
||||
const char *funcName)
|
||||
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(valueA, valueB, "!=", message, fileName, lineNumber, funcName);
|
||||
wpi_assertEqual_common_impl(valueAString, valueBString,
|
||||
"==", message, fileName, lineNumber, funcName);
|
||||
}
|
||||
return valueA == valueB;
|
||||
}
|
||||
@@ -144,15 +143,18 @@ bool wpi_assertEqual_impl(int valueA,
|
||||
* 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 *message,
|
||||
const char *fileName,
|
||||
uint32_t lineNumber,
|
||||
const char *funcName)
|
||||
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(valueA, valueB, "==", message, fileName, lineNumber, funcName);
|
||||
wpi_assertEqual_common_impl(valueAString, valueBString,
|
||||
"!=", message, fileName, lineNumber, funcName);
|
||||
}
|
||||
return valueA != valueB;
|
||||
}
|
||||
@@ -189,7 +191,7 @@ uint32_t GetFPGARevision()
|
||||
|
||||
/**
|
||||
* Read the microsecond-resolution timer on the FPGA.
|
||||
*
|
||||
*
|
||||
* @return The current time in microseconds according to the FPGA (since FPGA reset).
|
||||
*/
|
||||
uint32_t GetFPGATime()
|
||||
@@ -279,4 +281,55 @@ int32_t ToggleRIO_FPGA_LED()
|
||||
return ledState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Demangle a C++ symbol, used for printing stack traces.
|
||||
*/
|
||||
static std::string demangle(char const *mangledSymbol)
|
||||
{
|
||||
char buffer[256];
|
||||
size_t length;
|
||||
int status;
|
||||
|
||||
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.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ protected:
|
||||
* causes a ResourceAlreadyAllocated error.
|
||||
*/
|
||||
TEST_F(CANJaguarTest, AlreadyAllocatedError) {
|
||||
std::cout << "The following errors are expected.";
|
||||
std::cout << "The following errors are expected." << std::endl << std::endl;
|
||||
|
||||
CANJaguar jaguar(TestBench::kCANJaguarID);
|
||||
EXPECT_EQ(wpi_error_value_ResourceAlreadyAllocated, jaguar.GetError().GetCode())
|
||||
@@ -92,7 +92,7 @@ TEST_F(CANJaguarTest, AlreadyAllocatedError) {
|
||||
* out-of-range error.
|
||||
*/
|
||||
TEST_F(CANJaguarTest, 64OutOfRangeError) {
|
||||
std::cout << "The following errors are expected.";
|
||||
std::cout << "The following errors are expected." << std::endl << std::endl;
|
||||
|
||||
CANJaguar jaguar(64);
|
||||
EXPECT_EQ(wpi_error_value_ChannelIndexOutOfRange, jaguar.GetError().GetCode())
|
||||
@@ -104,7 +104,7 @@ TEST_F(CANJaguarTest, 64OutOfRangeError) {
|
||||
* error.
|
||||
*/
|
||||
TEST_F(CANJaguarTest, 0OutOfRangeError) {
|
||||
std::cout << "The following errors are expected.";
|
||||
std::cout << "The following errors are expected." << std::endl << std::endl;
|
||||
|
||||
CANJaguar jaguar(0);
|
||||
EXPECT_EQ(wpi_error_value_ChannelIndexOutOfRange, jaguar.GetError().GetCode())
|
||||
@@ -143,7 +143,7 @@ TEST_F(CANJaguarTest, Disable) {
|
||||
m_jaguar->DisableControl();
|
||||
|
||||
double initialPosition = m_jaguar->GetPosition();
|
||||
|
||||
|
||||
SetJaguar(kMotorTime, 1.0f);
|
||||
m_jaguar->Set(0.0f);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user