blob: 31d62b2de9474f57e73480b409d181e6b5cde2cf [file] [log] [blame]
Alexis Hetud73b8712018-09-21 15:14:43 -04001// 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
Ben Clayton105fd0f2019-02-26 17:59:24 +000020#include <stdlib.h>
Alexis Hetud73b8712018-09-21 15:14:43 -040021#include <assert.h>
22#include <stdio.h>
23
24#if !defined(TRACE_OUTPUT_FILE)
25#define TRACE_OUTPUT_FILE "debug.txt"
26#endif
27
Ben Clayton46e28cb2019-04-25 11:18:06 +010028#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
Alexis Hetud73b8712018-09-21 15:14:43 -040034namespace vk
35{
Ben Clayton00424c12019-03-17 17:29:30 +000036 // Outputs text to the debugging log
Ben Clayton46e28cb2019-04-25 11:18:06 +010037 void trace(const char *format, ...) CHECK_PRINTF_ARGS;
Ben Clayton00424c12019-03-17 17:29:30 +000038 inline void trace() {}
39
40 // Outputs text to the debugging log and prints to stderr.
Ben Clayton46e28cb2019-04-25 11:18:06 +010041 void warn(const char *format, ...) CHECK_PRINTF_ARGS;
Ben Clayton00424c12019-03-17 17:29:30 +000042 inline void warn() {}
43
44 // Outputs the message to the debugging log and stderr, and calls abort().
Ben Clayton46e28cb2019-04-25 11:18:06 +010045 void abort(const char *format, ...) CHECK_PRINTF_ARGS;
Alexis Hetud73b8712018-09-21 15:14:43 -040046}
47
Ben Clayton00424c12019-03-17 17:29:30 +000048// 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.
Alexis Hetud73b8712018-09-21 15:14:43 -040050#if defined(SWIFTSHADER_DISABLE_TRACE)
51#define TRACE(message, ...) (void(0))
52#else
Ben Clayton00424c12019-03-17 17:29:30 +000053#define TRACE(message, ...) vk::trace("%s:%d TRACE: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__)
Alexis Hetud73b8712018-09-21 15:14:43 -040054#endif
55
Ben Clayton00424c12019-03-17 17:29:30 +000056// 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__);
Ben Clayton36411212019-03-05 11:57:31 +000059
Ben Clayton00424c12019-03-17 17:29:30 +000060// 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__);
Ben Clayton36411212019-03-05 11:57:31 +000062
Ben Clayton00424c12019-03-17 17:29:30 +000063// 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
Ben Clayton01c03fd2019-03-15 20:41:33 +000076#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
Ben Clayton00424c12019-03-17 17:29:30 +000077#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
Alexis Hetud73b8712018-09-21 15:14:43 -040093#define ASSERT(expression) do { \
94 if(!(expression)) { \
Ben Clayton00424c12019-03-17 17:29:30 +000095 DABORT("ASSERT(%s)\n", #expression); \
Alexis Hetud73b8712018-09-21 15:14:43 -040096 } } while(0)
Ben Clayton01c03fd2019-03-15 20:41:33 +000097
Ben Clayton92070e82019-04-25 09:45:05 +010098// A macro to indicate functionality currently unimplemented for a feature
99// advertised as supported. For unsupported features not advertised as supported
100// use UNSUPPORTED().
Ben Clayton01c03fd2019-03-15 20:41:33 +0000101#undef UNIMPLEMENTED
Ben Clayton00424c12019-03-17 17:29:30 +0000102#define UNIMPLEMENTED(format, ...) DABORT("UNIMPLEMENTED: " format, ##__VA_ARGS__)
Ben Clayton01c03fd2019-03-15 20:41:33 +0000103
Ben Clayton92070e82019-04-25 09:45:05 +0100104// 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.
Ben Clayton01c03fd2019-03-15 20:41:33 +0000114#undef UNREACHABLE
Ben Clayton00424c12019-03-17 17:29:30 +0000115#define UNREACHABLE(format, ...) DABORT("UNREACHABLE: " format, ##__VA_ARGS__)
Ben Clayton01c03fd2019-03-15 20:41:33 +0000116
Ben Clayton00424c12019-03-17 17:29:30 +0000117// A macro asserting a condition and performing a return.
Ben Clayton01c03fd2019-03-15 20:41:33 +0000118#undef ASSERT_OR_RETURN
Ben Clayton00424c12019-03-17 17:29:30 +0000119#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
120#define ASSERT_OR_RETURN(expression) ASSERT(expression)
121#else
Alexis Hetud73b8712018-09-21 15:14:43 -0400122#define ASSERT_OR_RETURN(expression) do { \
123 if(!(expression)) { \
Alexis Hetud73b8712018-09-21 15:14:43 -0400124 return; \
125 } } while(0)
Ben Clayton00424c12019-03-17 17:29:30 +0000126#endif
Alexis Hetud73b8712018-09-21 15:14:43 -0400127
128#endif // VK_DEBUG_H_