Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 15 | // debug.h: Debugging utilities. |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 16 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 17 | #ifndef rr_DEBUG_H_ |
| 18 | #define rr_DEBUG_H_ |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 19 | |
| 20 | #include <assert.h> |
| 21 | #include <stdio.h> |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 23 | |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 24 | #include <cctype> |
| 25 | #include <string> |
| 26 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 27 | #if !defined(TRACE_OUTPUT_FILE) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 28 | # define TRACE_OUTPUT_FILE "debug.txt" |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 29 | #endif |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 30 | |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 31 | #if defined(__GNUC__) || defined(__clang__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 32 | # define CHECK_PRINTF_ARGS __attribute__((format(printf, 1, 2))) |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 33 | #else |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 34 | # define CHECK_PRINTF_ARGS |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 35 | #endif |
| 36 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 37 | namespace rr { |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 38 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 39 | // Outputs text to the debugging log |
| 40 | void trace(const char *format, ...) CHECK_PRINTF_ARGS; |
| 41 | inline void trace() {} |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 42 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 43 | // Outputs text to the debugging log and prints to stderr. |
| 44 | void warn(const char *format, ...) CHECK_PRINTF_ARGS; |
| 45 | inline void warn() {} |
| 46 | |
| 47 | // Outputs the message to the debugging log and stderr, and calls abort(). |
| 48 | void abort(const char *format, ...) CHECK_PRINTF_ARGS; |
| 49 | |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 50 | // Outputs text to the debugging log, and asserts once if a debugger is attached. |
| 51 | void trace_assert(const char *format, ...) CHECK_PRINTF_ARGS; |
| 52 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 53 | } // namespace rr |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 54 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 55 | // A macro to output a trace of a function call and its arguments to the |
| 56 | // debugging log. Disabled if RR_DISABLE_TRACE is defined. |
| 57 | #if defined(RR_DISABLE_TRACE) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 58 | # define TRACE(message, ...) (void(0)) |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 59 | # define TRACE_ASSERT(message, ...) (void(0)) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 60 | #else |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 61 | # define TRACE(message, ...) rr::trace("%s:%d TRACE: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 62 | # define TRACE_ASSERT(message, ...) rr::trace_assert("%s:%d %s TRACE_ASSERT: " message "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | // A macro to print a warning message to the debugging log and stderr to denote |
| 66 | // an issue that needs fixing. |
Nicolas Capens | 60aa34a | 2020-04-24 10:41:28 -0400 | [diff] [blame] | 67 | #define FIXME(message, ...) rr::warn("%s:%d FIXME: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 68 | |
| 69 | // A macro to print a warning message to the debugging log and stderr. |
Nicolas Capens | 60aa34a | 2020-04-24 10:41:28 -0400 | [diff] [blame] | 70 | #define WARN(message, ...) rr::warn("%s:%d WARNING: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 71 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 72 | // A macro that delegates to: |
Nicolas Capens | 2717702 | 2020-04-22 11:09:24 -0400 | [diff] [blame] | 73 | // abort() in debug builds (!NDEBUG || DCHECK_ALWAYS_ON) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 74 | // or |
Nicolas Capens | 2717702 | 2020-04-22 11:09:24 -0400 | [diff] [blame] | 75 | // warn() in release builds (NDEBUG && !DCHECK_ALWAYS_ON) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 76 | #undef DABORT |
| 77 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
Nicolas Capens | 2717702 | 2020-04-22 11:09:24 -0400 | [diff] [blame] | 78 | # define DABORT(message, ...) rr::abort("%s:%d ABORT: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 79 | #else |
Nicolas Capens | 2717702 | 2020-04-22 11:09:24 -0400 | [diff] [blame] | 80 | # define DABORT(message, ...) rr::warn("%s:%d WARNING: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | // A macro asserting a condition. |
| 84 | // If the condition fails, the condition and message is passed to DABORT(). |
| 85 | #undef ASSERT_MSG |
Nicolas Capens | 60aa34a | 2020-04-24 10:41:28 -0400 | [diff] [blame] | 86 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 87 | # define ASSERT_MSG(expression, format, ...) \ |
| 88 | do \ |
| 89 | { \ |
| 90 | if(!(expression)) \ |
| 91 | { \ |
| 92 | DABORT("ASSERT(%s): " format "\n", #expression, ##__VA_ARGS__); \ |
| 93 | } \ |
| 94 | } while(0) |
| 95 | #else |
| 96 | // Silence unused variable warnings without evaluating the expressions. |
| 97 | // TODO(b/154914395): Also ignore variadic arguments (similar to RR_WATCH expansion) |
| 98 | # define ASSERT_MSG(expression, format, ...) \ |
| 99 | do \ |
| 100 | { \ |
| 101 | (void)sizeof((int)(bool)(expression)); \ |
| 102 | (void)sizeof(format); \ |
| 103 | } while(0) |
| 104 | #endif |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 105 | |
| 106 | // A macro asserting a condition. |
| 107 | // If the condition fails, the condition is passed to DABORT(). |
| 108 | #undef ASSERT |
Nicolas Capens | 60aa34a | 2020-04-24 10:41:28 -0400 | [diff] [blame] | 109 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 110 | # define ASSERT(expression) \ |
| 111 | do \ |
| 112 | { \ |
| 113 | if(!(expression)) \ |
| 114 | { \ |
| 115 | DABORT("ASSERT(%s)\n", #expression); \ |
| 116 | } \ |
| 117 | } while(0) |
| 118 | #else |
| 119 | // Silence unused variable warnings without evaluating the expressions. |
| 120 | # define ASSERT(expression) \ |
| 121 | do \ |
| 122 | { \ |
| 123 | (void)sizeof((int)(bool)(expression)); \ |
| 124 | } while(0) |
| 125 | #endif |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 126 | |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 127 | // A macro to indicate functionality currently unimplemented, for a feature advertised |
| 128 | // as supported. This is similar to UNIMPLEMENTED() but does not check there's a bug |
| 129 | // number. |
| 130 | #undef UNIMPLEMENTED_NO_BUG |
| 131 | #define UNIMPLEMENTED_NO_BUG(format, ...) \ |
| 132 | DABORT("UNIMPLEMENTED: " format, ##__VA_ARGS__); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 133 | |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 134 | // A macro to indicate functionality currently unimplemented, for a feature advertised |
| 135 | // as supported. Since this is a bug, a bug ID must be provided, in b/### format. |
| 136 | // For unimplemented functionality not advertised as supported, use UNSUPPORTED() instead. |
| 137 | #undef UNIMPLEMENTED |
| 138 | #define UNIMPLEMENTED(format, ...) \ |
| 139 | static_assert(format[0] == 'b' && format[1] == '/' && format[2] >= '0' && format[2] <= '9', "explanation must start with bug reference in b/### format"); \ |
| 140 | UNIMPLEMENTED_NO_BUG(format, ##__VA_ARGS__) |
| 141 | |
| 142 | // A macro to indicate unsupported functionality. |
| 143 | // This should be called when a feature is attempted to be used, but is not |
| 144 | // currently implemented by Reactor. |
| 145 | // Note that in a well-behaved application these should not be reached as the |
| 146 | // application should be respecting the advertised features / limits. |
| 147 | #undef UNSUPPORTED |
| 148 | #define UNSUPPORTED(format, ...) DABORT("UNSUPPORTED: " format, ##__VA_ARGS__) |
| 149 | |
| 150 | // A macro for code which should never be reached, even with misbehaving |
| 151 | // applications. |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 152 | #undef UNREACHABLE |
| 153 | #define UNREACHABLE(format, ...) DABORT("UNREACHABLE: " format, ##__VA_ARGS__) |
| 154 | |
Nicolas Capens | 60aa34a | 2020-04-24 10:41:28 -0400 | [diff] [blame] | 155 | // A macro asserting a condition and returning if false. |
| 156 | // Note this macro always evaluates the expression and also returns in Release builds. |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 157 | #undef ASSERT_OR_RETURN |
| 158 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 159 | # define ASSERT_OR_RETURN(expression) ASSERT(expression) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 160 | #else |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 161 | # define ASSERT_OR_RETURN(expression) \ |
| 162 | do \ |
| 163 | { \ |
| 164 | if(!(expression)) \ |
| 165 | { \ |
| 166 | return; \ |
| 167 | } \ |
| 168 | } while(0) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 169 | #endif |
| 170 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 171 | #endif // rr_DEBUG_H_ |