blob: c0f32329b6da5d9c46605de6cbb15081fd4834e4 [file] [log] [blame]
Nicolas Capensc07dc4b2018-08-06 14:20:45 -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
Ben Claytoneb50d252019-04-15 13:50:01 -040015// debug.h: Debugging utilities.
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040016
Ben Claytoneb50d252019-04-15 13:50:01 -040017#ifndef rr_DEBUG_H_
18#define rr_DEBUG_H_
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040019
20#include <assert.h>
21#include <stdio.h>
Ben Clayton713b8d32019-12-17 20:37:56 +000022#include <stdlib.h>
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040023
Ben Claytoneb50d252019-04-15 13:50:01 -040024#if !defined(TRACE_OUTPUT_FILE)
Ben Clayton713b8d32019-12-17 20:37:56 +000025# define TRACE_OUTPUT_FILE "debug.txt"
Ben Claytoneb50d252019-04-15 13:50:01 -040026#endif
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040027
Ben Clayton46e28cb2019-04-25 11:18:06 +010028#if defined(__GNUC__) || defined(__clang__)
Ben Clayton713b8d32019-12-17 20:37:56 +000029# define CHECK_PRINTF_ARGS __attribute__((format(printf, 1, 2)))
Ben Clayton46e28cb2019-04-25 11:18:06 +010030#else
Ben Clayton713b8d32019-12-17 20:37:56 +000031# define CHECK_PRINTF_ARGS
Ben Clayton46e28cb2019-04-25 11:18:06 +010032#endif
33
Nicolas Capens157ba262019-12-10 17:49:14 -050034namespace rr {
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040035
Nicolas Capens157ba262019-12-10 17:49:14 -050036// Outputs text to the debugging log
37void trace(const char *format, ...) CHECK_PRINTF_ARGS;
38inline void trace() {}
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040039
Nicolas Capens157ba262019-12-10 17:49:14 -050040// Outputs text to the debugging log and prints to stderr.
41void warn(const char *format, ...) CHECK_PRINTF_ARGS;
42inline void warn() {}
43
44// Outputs the message to the debugging log and stderr, and calls abort().
45void abort(const char *format, ...) CHECK_PRINTF_ARGS;
46
47} // namespace rr
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040048
Ben Claytoneb50d252019-04-15 13:50:01 -040049// A macro to output a trace of a function call and its arguments to the
50// debugging log. Disabled if RR_DISABLE_TRACE is defined.
51#if defined(RR_DISABLE_TRACE)
Ben Clayton713b8d32019-12-17 20:37:56 +000052# define TRACE(message, ...) (void(0))
Ben Claytoneb50d252019-04-15 13:50:01 -040053#else
Ben Clayton713b8d32019-12-17 20:37:56 +000054# define TRACE(message, ...) rr::trace("%s:%d TRACE: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__)
Ben Claytoneb50d252019-04-15 13:50:01 -040055#endif
56
57// A macro to print a warning message to the debugging log and stderr to denote
58// an issue that needs fixing.
59#define FIXME(message, ...) rr::warn("%s:%d FIXME: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__);
60
61// A macro to print a warning message to the debugging log and stderr.
62#define WARN(message, ...) rr::warn("%s:%d WARNING: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__);
63
64// A macro that prints the message to the debugging log and stderr and
65// immediately aborts execution of the application.
66//
67// Note: This will terminate the application regardless of build flags!
68// Use with extreme caution!
69#undef ABORT
70#define ABORT(message, ...) rr::abort("%s:%d ABORT: " message "\n", __FILE__, __LINE__, ##__VA_ARGS__)
71
72// A macro that delegates to:
73// ABORT() in debug builds (!NDEBUG || DCHECK_ALWAYS_ON)
74// or
75// WARN() in release builds (NDEBUG && !DCHECK_ALWAYS_ON)
76#undef DABORT
77#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
Ben Clayton713b8d32019-12-17 20:37:56 +000078# define DABORT(message, ...) ABORT(message, ##__VA_ARGS__)
Ben Claytoneb50d252019-04-15 13:50:01 -040079#else
Ben Clayton713b8d32019-12-17 20:37:56 +000080# define DABORT(message, ...) WARN(message, ##__VA_ARGS__)
Ben Claytoneb50d252019-04-15 13:50:01 -040081#endif
82
83// A macro asserting a condition.
84// If the condition fails, the condition and message is passed to DABORT().
85#undef ASSERT_MSG
Ben Clayton713b8d32019-12-17 20:37:56 +000086#define ASSERT_MSG(expression, format, ...) \
87 do \
88 { \
89 if(!(expression)) \
90 { \
91 DABORT("ASSERT(%s): " format "\n", #expression, ##__VA_ARGS__); \
92 } \
93 } while(0)
Ben Claytoneb50d252019-04-15 13:50:01 -040094
95// A macro asserting a condition.
96// If the condition fails, the condition is passed to DABORT().
97#undef ASSERT
Ben Clayton713b8d32019-12-17 20:37:56 +000098#define ASSERT(expression) \
99 do \
100 { \
101 if(!(expression)) \
102 { \
103 DABORT("ASSERT(%s)\n", #expression); \
104 } \
105 } while(0)
Ben Claytoneb50d252019-04-15 13:50:01 -0400106
107// A macro to indicate unimplemented functionality.
108#undef UNIMPLEMENTED
109#define UNIMPLEMENTED(format, ...) DABORT("UNIMPLEMENTED: " format, ##__VA_ARGS__)
110
111// A macro for code which is not expected to be reached under valid assumptions.
112#undef UNREACHABLE
113#define UNREACHABLE(format, ...) DABORT("UNREACHABLE: " format, ##__VA_ARGS__)
114
115// A macro asserting a condition and performing a return.
116#undef ASSERT_OR_RETURN
117#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
Ben Clayton713b8d32019-12-17 20:37:56 +0000118# define ASSERT_OR_RETURN(expression) ASSERT(expression)
Ben Claytoneb50d252019-04-15 13:50:01 -0400119#else
Ben Clayton713b8d32019-12-17 20:37:56 +0000120# define ASSERT_OR_RETURN(expression) \
121 do \
122 { \
123 if(!(expression)) \
124 { \
125 return; \
126 } \
127 } while(0)
Ben Claytoneb50d252019-04-15 13:50:01 -0400128#endif
129
Ben Clayton713b8d32019-12-17 20:37:56 +0000130#endif // rr_DEBUG_H_