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 | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 24 | #if !defined(TRACE_OUTPUT_FILE) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 25 | # define TRACE_OUTPUT_FILE "debug.txt" |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 26 | #endif |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 27 | |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 28 | #if defined(__GNUC__) || defined(__clang__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 29 | # define CHECK_PRINTF_ARGS __attribute__((format(printf, 1, 2))) |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 30 | #else |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 31 | # define CHECK_PRINTF_ARGS |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 32 | #endif |
| 33 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 34 | namespace rr { |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 35 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 36 | // Outputs text to the debugging log |
| 37 | void trace(const char *format, ...) CHECK_PRINTF_ARGS; |
| 38 | inline void trace() {} |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 39 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 40 | // Outputs text to the debugging log and prints to stderr. |
| 41 | void warn(const char *format, ...) CHECK_PRINTF_ARGS; |
| 42 | inline void warn() {} |
| 43 | |
| 44 | // Outputs the message to the debugging log and stderr, and calls abort(). |
| 45 | void abort(const char *format, ...) CHECK_PRINTF_ARGS; |
| 46 | |
| 47 | } // namespace rr |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 48 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 49 | // A macro to output a trace of a function call and its arguments to the |
| 50 | // debugging log. Disabled if RR_DISABLE_TRACE is defined. |
| 51 | #if defined(RR_DISABLE_TRACE) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 52 | # define TRACE(message, ...) (void(0)) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 53 | #else |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 54 | # define TRACE(message, ...) rr::trace("%s:%d TRACE: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 55 | #endif |
| 56 | |
| 57 | // A macro to print a warning message to the debugging log and stderr to denote |
| 58 | // an issue that needs fixing. |
| 59 | #define FIXME(message, ...) rr::warn("%s:%d FIXME: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__); |
| 60 | |
| 61 | // A macro to print a warning message to the debugging log and stderr. |
| 62 | #define WARN(message, ...) rr::warn("%s:%d WARNING: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__); |
| 63 | |
| 64 | // A macro that prints the message to the debugging log and stderr and |
| 65 | // immediately aborts execution of the application. |
| 66 | // |
| 67 | // Note: This will terminate the application regardless of build flags! |
| 68 | // Use with extreme caution! |
| 69 | #undef ABORT |
| 70 | #define ABORT(message, ...) rr::abort("%s:%d ABORT: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
| 71 | |
| 72 | // A macro that delegates to: |
| 73 | // ABORT() in debug builds (!NDEBUG || DCHECK_ALWAYS_ON) |
| 74 | // or |
| 75 | // WARN() in release builds (NDEBUG && !DCHECK_ALWAYS_ON) |
| 76 | #undef DABORT |
| 77 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 78 | # define DABORT(message, ...) ABORT(message, ##__VA_ARGS__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 79 | #else |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 80 | # define DABORT(message, ...) WARN(message, ##__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 |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 86 | #define ASSERT_MSG(expression, format, ...) \ |
| 87 | do \ |
| 88 | { \ |
| 89 | if(!(expression)) \ |
| 90 | { \ |
| 91 | DABORT("ASSERT(%s): " format "\n", #expression, ##__VA_ARGS__); \ |
| 92 | } \ |
| 93 | } while(0) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 94 | |
| 95 | // A macro asserting a condition. |
| 96 | // If the condition fails, the condition is passed to DABORT(). |
| 97 | #undef ASSERT |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 98 | #define ASSERT(expression) \ |
| 99 | do \ |
| 100 | { \ |
| 101 | if(!(expression)) \ |
| 102 | { \ |
| 103 | DABORT("ASSERT(%s)\n", #expression); \ |
| 104 | } \ |
| 105 | } while(0) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 106 | |
| 107 | // A macro to indicate unimplemented functionality. |
| 108 | #undef UNIMPLEMENTED |
| 109 | #define UNIMPLEMENTED(format, ...) DABORT("UNIMPLEMENTED: " format, ##__VA_ARGS__) |
| 110 | |
| 111 | // A macro for code which is not expected to be reached under valid assumptions. |
| 112 | #undef UNREACHABLE |
| 113 | #define UNREACHABLE(format, ...) DABORT("UNREACHABLE: " format, ##__VA_ARGS__) |
| 114 | |
| 115 | // A macro asserting a condition and performing a return. |
| 116 | #undef ASSERT_OR_RETURN |
| 117 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 118 | # define ASSERT_OR_RETURN(expression) ASSERT(expression) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 119 | #else |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 120 | # define ASSERT_OR_RETURN(expression) \ |
| 121 | do \ |
| 122 | { \ |
| 123 | if(!(expression)) \ |
| 124 | { \ |
| 125 | return; \ |
| 126 | } \ |
| 127 | } while(0) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 128 | #endif |
| 129 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 130 | #endif // rr_DEBUG_H_ |