daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // debug.h: Debugging utilities. |
| 8 | |
| 9 | #ifndef COMMON_DEBUG_H_ |
| 10 | #define COMMON_DEBUG_H_ |
| 11 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <assert.h> |
| 14 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 15 | #include "common/angleutils.h" |
| 16 | |
| 17 | #if !defined(TRACE_OUTPUT_FILE) |
| 18 | #define TRACE_OUTPUT_FILE "debug.txt" |
| 19 | #endif |
| 20 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | namespace gl |
| 22 | { |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 23 | enum MessageType |
| 24 | { |
| 25 | MESSAGE_TRACE, |
| 26 | MESSAGE_FIXME, |
| 27 | MESSAGE_ERR, |
| 28 | MESSAGE_EVENT, |
| 29 | }; |
| 30 | |
Austin Kinross | fe14d45 | 2014-10-20 14:36:18 -0700 | [diff] [blame] | 31 | // Outputs text to the debugging log, or the debugging window |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 32 | void trace(bool traceInDebugOnly, MessageType messageType, const char *format, ...); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 33 | |
| 34 | // Returns whether D3DPERF is active. |
| 35 | bool perfActive(); |
| 36 | |
| 37 | // Pairs a D3D begin event with an end event. |
| 38 | class ScopedPerfEventHelper |
| 39 | { |
| 40 | public: |
| 41 | ScopedPerfEventHelper(const char* format, ...); |
| 42 | ~ScopedPerfEventHelper(); |
| 43 | |
| 44 | private: |
| 45 | DISALLOW_COPY_AND_ASSIGN(ScopedPerfEventHelper); |
| 46 | }; |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 47 | |
| 48 | void InitializeDebugAnnotations(); |
| 49 | void UninitializeDebugAnnotations(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // A macro to output a trace of a function call and its arguments to the debugging log |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 53 | #if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 54 | #define TRACE(message, ...) gl::trace(true, gl::MESSAGE_TRACE, "trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 55 | #else |
| 56 | #define TRACE(message, ...) (void(0)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 59 | // A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing. |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 60 | #if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 61 | #define FIXME(message, ...) gl::trace(false, gl::MESSAGE_FIXME, "fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 62 | #else |
| 63 | #define FIXME(message, ...) (void(0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 64 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 65 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 66 | // A macro to output a function call and its arguments to the debugging log, in case of error. |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 67 | #if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 68 | #define ERR(message, ...) gl::trace(false, gl::MESSAGE_ERR, "err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 69 | #else |
| 70 | #define ERR(message, ...) (void(0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | // A macro to log a performance event around a scope. |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 74 | #if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 75 | #if defined(_MSC_VER) |
Ehsan Akhgari | c1cc4c4 | 2014-07-02 13:09:07 -0400 | [diff] [blame] | 76 | #define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper ## __LINE__("%s" message "\n", __FUNCTION__, __VA_ARGS__); |
daniel@transgaming.com | 1825d8e | 2012-08-27 16:25:29 +0000 | [diff] [blame] | 77 | #else |
| 78 | #define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper(message "\n", ##__VA_ARGS__); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 79 | #endif // _MSC_VER |
| 80 | #else |
| 81 | #define EVENT(message, ...) (void(0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 82 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 83 | |
| 84 | // A macro asserting a condition and outputting failures to the debug log |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 85 | #if !defined(NDEBUG) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 86 | #define ASSERT(expression) do { \ |
| 87 | if(!(expression)) \ |
| 88 | ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \ |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 89 | assert(expression); \ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 90 | } while(0) |
Geoff Lang | 9cd1915 | 2014-05-28 15:54:34 -0400 | [diff] [blame] | 91 | #define UNUSED_ASSERTION_VARIABLE(variable) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 92 | #else |
| 93 | #define ASSERT(expression) (void(0)) |
Geoff Lang | 9cd1915 | 2014-05-28 15:54:34 -0400 | [diff] [blame] | 94 | #define UNUSED_ASSERTION_VARIABLE(variable) ((void)variable) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 95 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 96 | |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 97 | #ifndef ANGLE_ENABLE_DEBUG_TRACE |
Jamie Madill | bae30a0 | 2014-06-17 10:45:02 -0400 | [diff] [blame] | 98 | #define UNUSED_TRACE_VARIABLE(variable) ((void)variable) |
| 99 | #else |
| 100 | #define UNUSED_TRACE_VARIABLE(variable) |
| 101 | #endif |
| 102 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 103 | // A macro to indicate unimplemented functionality |
shannonwoods@chromium.org | eafb069 | 2013-05-30 00:20:44 +0000 | [diff] [blame] | 104 | |
Shannon Woods | 950cb60 | 2014-09-18 10:01:56 -0400 | [diff] [blame] | 105 | #if defined (ANGLE_TEST_CONFIG) |
| 106 | #define NOASSERT_UNIMPLEMENTED 1 |
| 107 | #endif |
| 108 | |
shannonwoods@chromium.org | eafb069 | 2013-05-30 00:20:44 +0000 | [diff] [blame] | 109 | // Define NOASSERT_UNIMPLEMENTED to non zero to skip the assert fail in the unimplemented checks |
| 110 | // This will allow us to test with some automated test suites (eg dEQP) without crashing |
| 111 | #ifndef NOASSERT_UNIMPLEMENTED |
| 112 | #define NOASSERT_UNIMPLEMENTED 0 |
| 113 | #endif |
| 114 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 115 | #if !defined(NDEBUG) |
| 116 | #define UNIMPLEMENTED() do { \ |
| 117 | FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); \ |
shannonwoods@chromium.org | eafb069 | 2013-05-30 00:20:44 +0000 | [diff] [blame] | 118 | assert(NOASSERT_UNIMPLEMENTED); \ |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 119 | } while(0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 120 | #else |
| 121 | #define UNIMPLEMENTED() FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__) |
| 122 | #endif |
| 123 | |
| 124 | // A macro for code which is not expected to be reached under valid assumptions |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 125 | #if !defined(NDEBUG) |
| 126 | #define UNREACHABLE() do { \ |
| 127 | ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__); \ |
| 128 | assert(false); \ |
| 129 | } while(0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 130 | #else |
| 131 | #define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__) |
| 132 | #endif |
| 133 | |
apatrick@chromium.org | 8b400b1 | 2013-01-30 21:53:40 +0000 | [diff] [blame] | 134 | // A macro that determines whether an object has a given runtime type. |
Geoff Lang | 8690da8 | 2013-10-08 10:57:09 -0400 | [diff] [blame] | 135 | #if !defined(NDEBUG) && (!defined(_MSC_VER) || defined(_CPPRTTI)) && (!defined(__GNUC__) || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || defined(__GXX_RTTI)) |
apatrick@chromium.org | 8b400b1 | 2013-01-30 21:53:40 +0000 | [diff] [blame] | 136 | #define HAS_DYNAMIC_TYPE(type, obj) (dynamic_cast<type >(obj) != NULL) |
| 137 | #else |
| 138 | #define HAS_DYNAMIC_TYPE(type, obj) true |
| 139 | #endif |
| 140 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 141 | // A macro functioning as a compile-time assert to validate constant conditions |
Jamie Madill | fd67b1b | 2015-03-10 16:13:24 -0400 | [diff] [blame] | 142 | |
| 143 | #if (defined(_MSC_VER) && _MSC_VER >= 1600) |
| 144 | #define ANGLE_HAS_STATIC_ASSERT |
| 145 | #elif (defined(__GNUC__) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)) |
| 146 | #define ANGLE_HAS_STATIC_ASSERT |
| 147 | #elif defined(__clang__) && __has_feature(cxx_static_assert) |
| 148 | #define ANGLE_HAS_STATIC_ASSERT |
| 149 | #endif |
| 150 | |
| 151 | #if defined(ANGLE_HAS_STATIC_ASSERT) |
Geoff Lang | d0f8e82 | 2013-09-30 15:21:53 -0400 | [diff] [blame] | 152 | #define META_ASSERT_MSG(condition, msg) static_assert(condition, msg) |
| 153 | #else |
Jamie Madill | 4f26786 | 2014-04-17 15:53:37 -0400 | [diff] [blame] | 154 | #define META_ASSERT_CONCAT(a, b) a ## b |
| 155 | #define META_ASSERT_CONCAT2(a, b) META_ASSERT_CONCAT(a, b) |
| 156 | #define META_ASSERT_MSG(condition, msg) typedef int META_ASSERT_CONCAT2(COMPILE_TIME_ASSERT_, __LINE__)[static_cast<bool>(condition)?1:-1] |
Geoff Lang | d0f8e82 | 2013-09-30 15:21:53 -0400 | [diff] [blame] | 157 | #endif |
| 158 | #define META_ASSERT(condition) META_ASSERT_MSG(condition, "compile time assertion failed.") |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 159 | |
| 160 | #endif // COMMON_DEBUG_H_ |