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 <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
34namespace vk
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 SWIFTSHADER_DISABLE_TRACE is defined.
50#if defined(SWIFTSHADER_DISABLE_TRACE)
51#define TRACE(message, ...) (void(0))
52#else
53#define TRACE(message, ...) vk::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, ...) vk::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, ...) vk::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, ...) 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
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 functionality currently unimplemented for a feature
99// advertised as supported. For unsupported features not advertised as supported
100// use UNSUPPORTED().
101#undef UNIMPLEMENTED
102#define UNIMPLEMENTED(format, ...) DABORT("UNIMPLEMENTED: " format, ##__VA_ARGS__)
103
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.
114#undef UNREACHABLE
115#define UNREACHABLE(format, ...) DABORT("UNREACHABLE: " format, ##__VA_ARGS__)
116
117// A macro asserting a condition and performing a return.
118#undef ASSERT_OR_RETURN
119#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
120#define ASSERT_OR_RETURN(expression) ASSERT(expression)
121#else
122#define ASSERT_OR_RETURN(expression) do { \
123 if(!(expression)) { \
124 return; \
125 } } while(0)
126#endif
127
128#endif // VK_DEBUG_H_
129