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
20 #include <assert.h>
21 #include <stdio.h>
22
23 #if !defined(TRACE_OUTPUT_FILE)
24 #define TRACE_OUTPUT_FILE "debug.txt"
25 #endif
26
27 namespace vk
28 {
29 // Outputs text to the debugging log
30 void trace(const char *format, ...);
trace()31 inline void trace() {}
32 }
33
34 // A macro to output a trace of a function call and its arguments to the debugging log
35 #if defined(SWIFTSHADER_DISABLE_TRACE)
36 #define TRACE(message, ...) (void(0))
37 #else
38 #define TRACE(message, ...) vk::trace("trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
39 #endif
40
41 // A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing.
42 #if defined(SWIFTSHADER_DISABLE_TRACE)
43 #define FIXME(message, ...) (void(0))
44 #else
45 #define FIXME(message, ...) do {vk::trace("fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false)
46 #endif
47
48 // A macro to output a function call and its arguments to the debugging log, in case of error.
49 #if defined(SWIFTSHADER_DISABLE_TRACE)
50 #define ERR(message, ...) (void(0))
51 #else
52 #define ERR(message, ...) do {vk::trace("err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false)
53 #endif
54
55 // A macro asserting a condition and outputting failures to the debug log
56 #undef ASSERT
57 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
58 #define ASSERT(expression) do { \
59 if(!(expression)) { \
60 ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \
61 assert(expression); \
62 } } while(0)
63 #else
64 #define ASSERT(expression) (void(0))
65 #endif
66
67 // A macro to indicate unimplemented functionality
68 #undef UNIMPLEMENTED
69 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
70 #define UNIMPLEMENTED(...) do { \
71 vk::trace("\t! Unimplemented: %s(%d): ", __FUNCTION__, __LINE__); \
72 vk::trace(__VA_ARGS__); \
73 vk::trace("\n"); \
74 assert(false); \
75 } while(0)
76 #else
77 #define UNIMPLEMENTED(...) FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__)
78 #endif
79
80 // A macro for code which is not expected to be reached under valid assumptions
81 #undef UNREACHABLE
82 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
83 #define UNREACHABLE(value) do { \
84 ERR("\t! Unreachable case reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value); \
85 assert(false); \
86 } while(0)
87 #else
88 #define UNREACHABLE(value) ERR("\t! Unreachable reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value)
89 #endif
90
91 // A macro asserting a condition and outputting failures to the debug log, or return when in release mode.
92 #undef ASSERT_OR_RETURN
93 #define ASSERT_OR_RETURN(expression) do { \
94 if(!(expression)) { \
95 ASSERT(expression); \
96 return; \
97 } } while(0)
98
99 #endif // VK_DEBUG_H_
100