Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -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 | |
| 15 | // debug.h: Debugging utilities. |
| 16 | |
| 17 | #ifndef VK_DEBUG_H_ |
| 18 | #define VK_DEBUG_H_ |
| 19 | |
Ben Clayton | 105fd0f | 2019-02-26 17:59:24 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 21 | #include <assert.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #if !defined(TRACE_OUTPUT_FILE) |
| 25 | #define TRACE_OUTPUT_FILE "debug.txt" |
| 26 | #endif |
| 27 | |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 28 | #if defined(__GNUC__) || defined(__clang__) |
| 29 | #define CHECK_PRINTF_ARGS __attribute__((format(printf, 1, 2))) |
| 30 | #else |
| 31 | #define CHECK_PRINTF_ARGS |
| 32 | #endif |
| 33 | |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 34 | namespace vk |
| 35 | { |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 36 | // Outputs text to the debugging log |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 37 | void trace(const char *format, ...) CHECK_PRINTF_ARGS; |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 38 | inline void trace() {} |
| 39 | |
| 40 | // Outputs text to the debugging log and prints to stderr. |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 41 | void warn(const char *format, ...) CHECK_PRINTF_ARGS; |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 42 | inline void warn() {} |
| 43 | |
| 44 | // Outputs the message to the debugging log and stderr, and calls abort(). |
Ben Clayton | 46e28cb | 2019-04-25 11:18:06 +0100 | [diff] [blame] | 45 | void abort(const char *format, ...) CHECK_PRINTF_ARGS; |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 48 | // A macro to output a trace of a function call and its arguments to the |
| 49 | // debugging log. Disabled if SWIFTSHADER_DISABLE_TRACE is defined. |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 50 | #if defined(SWIFTSHADER_DISABLE_TRACE) |
| 51 | #define TRACE(message, ...) (void(0)) |
| 52 | #else |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 53 | #define TRACE(message, ...) vk::trace("%s:%d TRACE: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 54 | #endif |
| 55 | |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 56 | // A macro to print a warning message to the debugging log and stderr to denote |
| 57 | // an issue that needs fixing. |
| 58 | #define FIXME(message, ...) vk::warn("%s:%d FIXME: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__); |
Ben Clayton | 3641121 | 2019-03-05 11:57:31 +0000 | [diff] [blame] | 59 | |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 60 | // A macro to print a warning message to the debugging log and stderr. |
| 61 | #define WARN(message, ...) vk::warn("%s:%d WARNING: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__); |
Ben Clayton | 3641121 | 2019-03-05 11:57:31 +0000 | [diff] [blame] | 62 | |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 63 | // A macro that prints the message to the debugging log and stderr and |
| 64 | // immediately aborts execution of the application. |
| 65 | // |
| 66 | // Note: This will terminate the application regardless of build flags! |
| 67 | // Use with extreme caution! |
| 68 | #undef ABORT |
| 69 | #define ABORT(message, ...) vk::abort("%s:%d ABORT: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
| 70 | |
| 71 | // A macro that delegates to: |
| 72 | // ABORT() in debug builds (!NDEBUG || DCHECK_ALWAYS_ON) |
| 73 | // or |
| 74 | // WARN() in release builds (NDEBUG && !DCHECK_ALWAYS_ON) |
| 75 | #undef DABORT |
Ben Clayton | 01c03fd | 2019-03-15 20:41:33 +0000 | [diff] [blame] | 76 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 77 | #define DABORT(message, ...) ABORT(message, ##__VA_ARGS__) |
| 78 | #else |
| 79 | #define DABORT(message, ...) WARN(message, ##__VA_ARGS__) |
| 80 | #endif |
| 81 | |
| 82 | // A macro asserting a condition. |
| 83 | // If the condition fails, the condition and message is passed to DABORT(). |
| 84 | #undef ASSERT_MSG |
| 85 | #define ASSERT_MSG(expression, format, ...) do { \ |
| 86 | if(!(expression)) { \ |
| 87 | DABORT("ASSERT(%s): " format "\n", #expression, ##__VA_ARGS__); \ |
| 88 | } } while(0) |
| 89 | |
| 90 | // A macro asserting a condition. |
| 91 | // If the condition fails, the condition is passed to DABORT(). |
| 92 | #undef ASSERT |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 93 | #define ASSERT(expression) do { \ |
| 94 | if(!(expression)) { \ |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 95 | DABORT("ASSERT(%s)\n", #expression); \ |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 96 | } } while(0) |
Ben Clayton | 01c03fd | 2019-03-15 20:41:33 +0000 | [diff] [blame] | 97 | |
Ben Clayton | 92070e8 | 2019-04-25 09:45:05 +0100 | [diff] [blame] | 98 | // A macro to indicate functionality currently unimplemented for a feature |
| 99 | // advertised as supported. For unsupported features not advertised as supported |
| 100 | // use UNSUPPORTED(). |
Ben Clayton | 01c03fd | 2019-03-15 20:41:33 +0000 | [diff] [blame] | 101 | #undef UNIMPLEMENTED |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 102 | #define UNIMPLEMENTED(format, ...) DABORT("UNIMPLEMENTED: " format, ##__VA_ARGS__) |
Ben Clayton | 01c03fd | 2019-03-15 20:41:33 +0000 | [diff] [blame] | 103 | |
Ben Clayton | 92070e8 | 2019-04-25 09:45:05 +0100 | [diff] [blame] | 104 | // A macro to indicate unsupported functionality. |
| 105 | // This should be called when a Vulkan / SPIR-V feature is attempted to be used, |
| 106 | // but is not currently implemented by SwiftShader. |
| 107 | // Note that in a well-behaved application these should not be reached as the |
| 108 | // application should be respecting the advertised features / limits. |
| 109 | #undef UNSUPPORTED |
| 110 | #define UNSUPPORTED(format, ...) DABORT("UNSUPPORTED: " format, ##__VA_ARGS__) |
| 111 | |
| 112 | // A macro for code which should never be reached, even with misbehaving |
| 113 | // applications. |
Ben Clayton | 01c03fd | 2019-03-15 20:41:33 +0000 | [diff] [blame] | 114 | #undef UNREACHABLE |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 115 | #define UNREACHABLE(format, ...) DABORT("UNREACHABLE: " format, ##__VA_ARGS__) |
Ben Clayton | 01c03fd | 2019-03-15 20:41:33 +0000 | [diff] [blame] | 116 | |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 117 | // A macro asserting a condition and performing a return. |
Ben Clayton | 01c03fd | 2019-03-15 20:41:33 +0000 | [diff] [blame] | 118 | #undef ASSERT_OR_RETURN |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 119 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 120 | #define ASSERT_OR_RETURN(expression) ASSERT(expression) |
| 121 | #else |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 122 | #define ASSERT_OR_RETURN(expression) do { \ |
| 123 | if(!(expression)) { \ |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 124 | return; \ |
| 125 | } } while(0) |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 126 | #endif |
Alexis Hetu | d73b871 | 2018-09-21 15:14:43 -0400 | [diff] [blame] | 127 | |
| 128 | #endif // VK_DEBUG_H_ |