Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -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 COMMON_DEBUG_H_ |
| 18 | #define COMMON_DEBUG_H_ |
| 19 | |
Stephen White | e6ab01f | 2019-04-04 14:31:25 -0400 | [diff] [blame^] | 20 | #if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD) && !defined(ANDROID_NDK_BUILD) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 21 | #include "../../Common/DebugAndroid.hpp" |
| 22 | #else |
| 23 | #include <stdio.h> |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #if !defined(TRACE_OUTPUT_FILE) |
| 27 | #define TRACE_OUTPUT_FILE "debug.txt" |
| 28 | #endif |
| 29 | |
| 30 | namespace es |
| 31 | { |
Nicolas Capens | a5dfd97 | 2018-09-28 15:27:08 -0400 | [diff] [blame] | 32 | // Outputs text to the debugging log |
| 33 | void trace(const char *format, ...); |
| 34 | inline void trace() {} |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // A macro to output a trace of a function call and its arguments to the debugging log |
| 38 | #if defined(ANGLE_DISABLE_TRACE) |
| 39 | #define TRACE(message, ...) (void(0)) |
| 40 | #else |
| 41 | #define TRACE(message, ...) es::trace("trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) |
| 42 | #endif |
| 43 | |
| 44 | // A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing. |
| 45 | #if defined(ANGLE_DISABLE_TRACE) |
| 46 | #define FIXME(message, ...) (void(0)) |
| 47 | #else |
| 48 | #define FIXME(message, ...) do {es::trace("fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false) |
| 49 | #endif |
| 50 | |
| 51 | // A macro to output a function call and its arguments to the debugging log, in case of error. |
| 52 | #if defined(ANGLE_DISABLE_TRACE) |
| 53 | #define ERR(message, ...) (void(0)) |
| 54 | #else |
| 55 | #define ERR(message, ...) do {es::trace("err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false) |
| 56 | #endif |
| 57 | |
| 58 | // A macro asserting a condition and outputting failures to the debug log |
| 59 | #undef ASSERT |
Alexis Hetu | 0c5035b | 2017-09-20 15:55:08 -0400 | [diff] [blame] | 60 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 61 | #define ASSERT(expression) do { \ |
Nicolas Capens | 9e8bfca | 2018-02-22 16:39:20 -0500 | [diff] [blame] | 62 | if(!(expression)) { \ |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 63 | ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \ |
| 64 | assert(expression); \ |
Nicolas Capens | 9e8bfca | 2018-02-22 16:39:20 -0500 | [diff] [blame] | 65 | } } while(0) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 66 | #else |
| 67 | #define ASSERT(expression) (void(0)) |
| 68 | #endif |
| 69 | |
| 70 | // A macro to indicate unimplemented functionality |
| 71 | #undef UNIMPLEMENTED |
Alexis Hetu | 0c5035b | 2017-09-20 15:55:08 -0400 | [diff] [blame] | 72 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
Nicolas Capens | a5dfd97 | 2018-09-28 15:27:08 -0400 | [diff] [blame] | 73 | #define UNIMPLEMENTED(...) do { \ |
| 74 | es::trace("\t! Unimplemented: %s(%d): ", __FUNCTION__, __LINE__); \ |
Nicolas Capens | 60be5c4 | 2018-10-02 10:49:22 -0400 | [diff] [blame] | 75 | es::trace(__VA_ARGS__); \ |
Nicolas Capens | a5dfd97 | 2018-09-28 15:27:08 -0400 | [diff] [blame] | 76 | es::trace("\n"); \ |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 77 | assert(false); \ |
| 78 | } while(0) |
| 79 | #else |
Nicolas Capens | a5dfd97 | 2018-09-28 15:27:08 -0400 | [diff] [blame] | 80 | #define UNIMPLEMENTED(...) FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | // A macro for code which is not expected to be reached under valid assumptions |
| 84 | #undef UNREACHABLE |
Alexis Hetu | 0c5035b | 2017-09-20 15:55:08 -0400 | [diff] [blame] | 85 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 86 | #define UNREACHABLE(value) do { \ |
| 87 | ERR("\t! Unreachable case reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value); \ |
| 88 | assert(false); \ |
| 89 | } while(0) |
| 90 | #else |
| 91 | #define UNREACHABLE(value) ERR("\t! Unreachable reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value) |
| 92 | #endif |
| 93 | |
Nicolas Capens | 9e8bfca | 2018-02-22 16:39:20 -0500 | [diff] [blame] | 94 | #endif // !__ANDROID__ |
| 95 | |
| 96 | // A macro asserting a condition and outputting failures to the debug log, or return when in release mode. |
| 97 | #undef ASSERT_OR_RETURN |
| 98 | #define ASSERT_OR_RETURN(expression) do { \ |
| 99 | if(!(expression)) { \ |
| 100 | ASSERT(expression); \ |
| 101 | return; \ |
| 102 | } } while(0) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 103 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 104 | #endif // COMMON_DEBUG_H_ |