diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/.cproject b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/.cproject index c4d783aa48..d53ded629b 100644 --- a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/.cproject +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/.cproject @@ -58,6 +58,7 @@ diff --git a/hal/include/HAL/cpp/StackTrace.hpp b/hal/include/HAL/cpp/StackTrace.hpp deleted file mode 100644 index 6182e45856..0000000000 --- a/hal/include/HAL/cpp/StackTrace.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#ifdef __vxworks -#include -#else -#include -#endif - -extern "C" -{ - void printCurrentStackTrace(); -} diff --git a/hal/lib/Athena/cpp/StackTrace.cpp b/hal/lib/Athena/cpp/StackTrace.cpp deleted file mode 100644 index d20796ab1d..0000000000 --- a/hal/lib/Athena/cpp/StackTrace.cpp +++ /dev/null @@ -1,9 +0,0 @@ - -#include "HAL/cpp/StackTrace.hpp" - -#include - - -void printCurrentStackTrace() { - // TODO: Implement -} diff --git a/wpilibc/wpilibC++/include/Error.h b/wpilibc/wpilibC++/include/Error.h index 8d4ce5fdaa..18c5abc46d 100644 --- a/wpilibc/wpilibC++/include/Error.h +++ b/wpilibc/wpilibC++/include/Error.h @@ -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); }; diff --git a/wpilibc/wpilibC++/include/Utility.h b/wpilibc/wpilibC++/include/Utility.h index dfa7179172..07b4bc39aa 100644 --- a/wpilibc/wpilibC++/include/Utility.h +++ b/wpilibc/wpilibC++/include/Utility.h @@ -6,22 +6,22 @@ #pragma once #include "HAL/HAL.hpp" +#include #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); diff --git a/wpilibc/wpilibC++/lib/Error.cpp b/wpilibc/wpilibC++/lib/Error.cpp index b8c1e5da06..9da8924c6e 100644 --- a/wpilibc/wpilibC++/lib/Error.cpp +++ b/wpilibc/wpilibC++/lib/Error.cpp @@ -6,14 +6,15 @@ #include "Error.h" -#include "HAL/cpp/StackTrace.hpp" +#include +#include #include #include +#include //#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,"%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("---------------------------\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; } - diff --git a/wpilibc/wpilibC++/lib/ErrorBase.cpp b/wpilibc/wpilibC++/lib/ErrorBase.cpp index 9092d5bc6c..414c15b883 100644 --- a/wpilibc/wpilibC++/lib/ErrorBase.cpp +++ b/wpilibc/wpilibC++/lib/ErrorBase.cpp @@ -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" diff --git a/wpilibc/wpilibC++/lib/Utility.cpp b/wpilibc/wpilibC++/lib/Utility.cpp index 54b484fbde..52200248b3 100644 --- a/wpilibc/wpilibC++/lib/Utility.cpp +++ b/wpilibc/wpilibC++/lib/Utility.cpp @@ -8,23 +8,18 @@ //#include "NetworkCommunication/FRCComm.h" #include "HAL/HAL.hpp" -#include "HAL/cpp/StackTrace.hpp" #include "Task.h" -#include -#include +#include +#include +#include +#include +#include +#include +#include #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---------------------------\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(); +} diff --git a/wpilibc/wpilibC++IntegrationTests/src/CANJaguarTest.cpp b/wpilibc/wpilibC++IntegrationTests/src/CANJaguarTest.cpp index f59ec78a57..122328e68e 100644 --- a/wpilibc/wpilibC++IntegrationTests/src/CANJaguarTest.cpp +++ b/wpilibc/wpilibC++IntegrationTests/src/CANJaguarTest.cpp @@ -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);