Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | |
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 5 | /*----------------------------------------------------------------------------*/ |
| 6 | |
| 7 | #include "Utility.h" |
| 8 | |
| 9 | #include "Timer.h" |
| 10 | #include "simulation/simTime.h" |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <iostream> |
| 15 | #include <sstream> |
| 16 | #include <cstdio> |
| 17 | #include <cstdlib> |
| 18 | #include <cstring> |
| 19 | #if defined(UNIX) |
| 20 | #include <execinfo.h> |
| 21 | #include <cxxabi.h> |
| 22 | #endif |
| 23 | |
| 24 | static bool stackTraceEnabled = false; |
| 25 | static bool suspendOnAssertEnabled = false; |
| 26 | |
| 27 | /** |
| 28 | * Enable Stack trace after asserts. |
| 29 | */ |
| 30 | void wpi_stackTraceOnAssertEnable(bool enabled) |
| 31 | { |
| 32 | stackTraceEnabled = enabled; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Enable suspend on asssert. |
| 37 | * If enabled, the user task will be suspended whenever an assert fails. This |
| 38 | * will allow the user to attach to the task with the debugger and examine variables |
| 39 | * around the failure. |
| 40 | */ |
| 41 | void wpi_suspendOnAssertEnabled(bool enabled) |
| 42 | { |
| 43 | suspendOnAssertEnabled = enabled; |
| 44 | } |
| 45 | |
| 46 | static void wpi_handleTracing() |
| 47 | { |
| 48 | // if (stackTraceEnabled) |
| 49 | // { |
| 50 | // printf("\n-----------<Stack Trace>----------------\n"); |
| 51 | // printCurrentStackTrace(); |
| 52 | // } |
| 53 | printf("\n"); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Assert implementation. |
| 58 | * This allows breakpoints to be set on an assert. |
| 59 | * The users don't call this, but instead use the wpi_assert macros in Utility.h. |
| 60 | */ |
| 61 | bool wpi_assert_impl(bool conditionValue, const std::string &conditionText, |
| 62 | const std::string &message, const std::string &fileName, |
| 63 | uint32_t lineNumber, const std::string &funcName) { |
| 64 | if (!conditionValue) { |
| 65 | // Error string buffer |
| 66 | std::stringstream error; |
| 67 | |
| 68 | // If an error message was specified, include it |
| 69 | // Build error string |
| 70 | if (message.size()) { |
| 71 | error << "Assertion failed: \"" << message << "\", \"" << conditionText |
| 72 | << "\" failed in " << funcName + "() in " << fileName << " at line " |
| 73 | << lineNumber; |
| 74 | } else { |
| 75 | error << "Assertion failed: \"" << conditionText << "\" in " << funcName |
| 76 | << "() in " << fileName << " at line " << lineNumber; |
| 77 | } |
| 78 | |
| 79 | // Print to console and send to remote dashboard |
| 80 | std::cout << "\n\n>>>>" << error.str(); |
| 81 | |
| 82 | wpi_handleTracing(); |
| 83 | } |
| 84 | return conditionValue; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Common error routines for wpi_assertEqual_impl and wpi_assertNotEqual_impl |
| 89 | * This should not be called directly; it should only be used by wpi_assertEqual_impl |
| 90 | * and wpi_assertNotEqual_impl. |
| 91 | */ |
| 92 | void wpi_assertEqual_common_impl(int valueA, int valueB, |
| 93 | const std::string &equalityType, |
| 94 | const std::string &message, |
| 95 | const std::string &fileName, |
| 96 | uint32_t lineNumber, |
| 97 | const std::string &funcName) { |
| 98 | // Error string buffer |
| 99 | std::stringstream error; |
| 100 | |
| 101 | // If an error message was specified, include it |
| 102 | // Build error string |
| 103 | if (message.size() > 0) { |
| 104 | error << "Assertion failed: \"" << message << "\", \"" << valueA << "\" " |
| 105 | << equalityType << " \"" << valueB << "\" in " << funcName << "() in " |
| 106 | << fileName << " at line " << lineNumber << "\n"; |
| 107 | } else { |
| 108 | error << "Assertion failed: \"" << valueA << "\" " << equalityType << " \"" |
| 109 | << valueB << "\" in " << funcName << "() in " << fileName |
| 110 | << " at line " << lineNumber << "\n"; |
| 111 | } |
| 112 | |
| 113 | // Print to console and send to remote dashboard |
| 114 | std::cout << "\n\n>>>>" << error.str(); |
| 115 | |
| 116 | wpi_handleTracing(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Assert equal implementation. |
| 121 | * This determines whether the two given integers are equal. If not, |
| 122 | * the value of each is printed along with an optional message string. |
| 123 | * The users don't call this, but instead use the wpi_assertEqual macros in Utility.h. |
| 124 | */ |
| 125 | bool wpi_assertEqual_impl(int valueA, |
| 126 | int valueB, |
| 127 | const std::string &message, |
| 128 | const std::string &fileName, |
| 129 | uint32_t lineNumber, |
| 130 | const std::string &funcName) |
| 131 | { |
| 132 | if(!(valueA == valueB)) |
| 133 | { |
| 134 | wpi_assertEqual_common_impl(valueA, valueB, "!=", message, fileName, lineNumber, funcName); |
| 135 | } |
| 136 | return valueA == valueB; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Assert not equal implementation. |
| 141 | * This determines whether the two given integers are equal. If so, |
| 142 | * the value of each is printed along with an optional message string. |
| 143 | * The users don't call this, but instead use the wpi_assertNotEqual macros in Utility.h. |
| 144 | */ |
| 145 | bool wpi_assertNotEqual_impl(int valueA, |
| 146 | int valueB, |
| 147 | const std::string &message, |
| 148 | const std::string &fileName, |
| 149 | uint32_t lineNumber, |
| 150 | const std::string &funcName) |
| 151 | { |
| 152 | if(!(valueA != valueB)) |
| 153 | { |
| 154 | wpi_assertEqual_common_impl(valueA, valueB, "==", message, fileName, lineNumber, funcName); |
| 155 | } |
| 156 | return valueA != valueB; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Read the microsecond-resolution timer on the FPGA. |
| 161 | * |
| 162 | * @return The current time in microseconds according to the FPGA (since FPGA reset). |
| 163 | */ |
| 164 | uint32_t GetFPGATime() |
| 165 | { |
| 166 | return wpilib::internal::simTime * 1e6; |
| 167 | } |
| 168 | |
| 169 | //TODO: implement symbol demangling and backtrace on windows |
| 170 | #if defined(UNIX) |
| 171 | |
| 172 | /** |
| 173 | * Demangle a C++ symbol, used for printing stack traces. |
| 174 | */ |
| 175 | static std::string demangle(char const *mangledSymbol) |
| 176 | { |
| 177 | char buffer[256]; |
| 178 | size_t length; |
| 179 | int status; |
| 180 | |
| 181 | if(sscanf(mangledSymbol, "%*[^(]%*[^_]%255[^)+]", buffer)) |
| 182 | { |
| 183 | char *symbol = abi::__cxa_demangle(buffer, nullptr, &length, &status); |
| 184 | |
| 185 | if(status == 0) |
| 186 | { |
| 187 | return symbol; |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | // If the symbol couldn't be demangled, it's probably a C function, |
| 192 | // so just return it as-is. |
| 193 | return buffer; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | // If everything else failed, just return the mangled symbol |
| 199 | return mangledSymbol; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Get a stack trace, ignoring the first "offset" symbols. |
| 204 | */ |
| 205 | std::string GetStackTrace(uint32_t offset) |
| 206 | { |
| 207 | void *stackTrace[128]; |
| 208 | int stackSize = backtrace(stackTrace, 128); |
| 209 | char **mangledSymbols = backtrace_symbols(stackTrace, stackSize); |
| 210 | std::stringstream trace; |
| 211 | |
| 212 | for(int i = offset; i < stackSize; i++) |
| 213 | { |
| 214 | // Only print recursive functions once in a row. |
| 215 | if(i == 0 ||stackTrace[i] != stackTrace[i - 1]) |
| 216 | { |
| 217 | trace << "\tat " << demangle(mangledSymbols[i]) << std::endl; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | free(mangledSymbols); |
| 222 | |
| 223 | return trace.str(); |
| 224 | } |
| 225 | |
| 226 | #else |
| 227 | static std::string demangle(char const *mangledSymbol) |
| 228 | { |
| 229 | return "no demangling on windows"; |
| 230 | } |
| 231 | std::string GetStackTrace(uint32_t offset) |
| 232 | { |
| 233 | return "no stack trace on windows"; |
| 234 | } |
| 235 | #endif |