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 rr_DEBUG_H_ |
18 | #define rr_DEBUG_H_ |
19 | |
20 | #include <stdlib.h> |
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 | |
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 | |
34 | namespace rr |
35 | { |
36 | // Outputs text to the debugging log |
37 | void trace(const char *format, ...) CHECK_PRINTF_ARGS; |
38 | inline void trace() {} |
39 | |
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 | |
48 | // A macro to output a trace of a function call and its arguments to the |
49 | // debugging log. Disabled if RR_DISABLE_TRACE is defined. |
50 | #if defined(RR_DISABLE_TRACE) |
51 | #define TRACE(message, ...) (void(0)) |
52 | #else |
53 | #define TRACE(message, ...) rr::trace("%s:%d TRACE: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
54 | #endif |
55 | |
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, ...) rr::warn("%s:%d FIXME: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__); |
59 | |
60 | // A macro to print a warning message to the debugging log and stderr. |
61 | #define WARN(message, ...) rr::warn("%s:%d WARNING: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__); |
62 | |
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, ...) rr::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 |
76 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
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 |
93 | #define ASSERT(expression) do { \ |
94 | if(!(expression)) { \ |
95 | DABORT("ASSERT(%s)\n", #expression); \ |
96 | } } while(0) |
97 | |
98 | // A macro to indicate unimplemented functionality. |
99 | #undef UNIMPLEMENTED |
100 | #define UNIMPLEMENTED(format, ...) DABORT("UNIMPLEMENTED: " format, ##__VA_ARGS__) |
101 | |
102 | // A macro for code which is not expected to be reached under valid assumptions. |
103 | #undef UNREACHABLE |
104 | #define UNREACHABLE(format, ...) DABORT("UNREACHABLE: " format, ##__VA_ARGS__) |
105 | |
106 | // A macro asserting a condition and performing a return. |
107 | #undef ASSERT_OR_RETURN |
108 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
109 | #define ASSERT_OR_RETURN(expression) ASSERT(expression) |
110 | #else |
111 | #define ASSERT_OR_RETURN(expression) do { \ |
112 | if(!(expression)) { \ |
113 | return; \ |
114 | } } while(0) |
115 | #endif |
116 | |
117 | #endif // rr_DEBUG_H_ |
118 | |