blob: d6327250d6e4df62ce3c8e077504ff618c56c9b8 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// debug.h: Debugging utilities.
8
9#ifndef COMMON_DEBUG_H_
10#define COMMON_DEBUG_H_
11
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012#include <stdio.h>
13#include <assert.h>
14
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000015#include "common/angleutils.h"
16
17#if !defined(TRACE_OUTPUT_FILE)
18#define TRACE_OUTPUT_FILE "debug.txt"
19#endif
20
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021namespace gl
22{
Jamie Madill14aa40f2015-01-07 15:12:47 -050023 enum MessageType
24 {
25 MESSAGE_TRACE,
26 MESSAGE_FIXME,
27 MESSAGE_ERR,
28 MESSAGE_EVENT,
29 };
30
Austin Kinrossfe14d452014-10-20 14:36:18 -070031 // Outputs text to the debugging log, or the debugging window
Jamie Madill14aa40f2015-01-07 15:12:47 -050032 void trace(bool traceInDebugOnly, MessageType messageType, const char *format, ...);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000033
34 // Returns whether D3DPERF is active.
35 bool perfActive();
36
37 // Pairs a D3D begin event with an end event.
38 class ScopedPerfEventHelper
39 {
40 public:
41 ScopedPerfEventHelper(const char* format, ...);
42 ~ScopedPerfEventHelper();
43
44 private:
45 DISALLOW_COPY_AND_ASSIGN(ScopedPerfEventHelper);
46 };
Austin Kinross922a9fb2014-10-21 14:26:33 -070047
48 void InitializeDebugAnnotations();
49 void UninitializeDebugAnnotations();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050}
51
52// A macro to output a trace of a function call and its arguments to the debugging log
Austin Kinrossf0360c62014-10-20 14:26:13 -070053#if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
Jamie Madill14aa40f2015-01-07 15:12:47 -050054#define TRACE(message, ...) gl::trace(true, gl::MESSAGE_TRACE, "trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040055#else
56#define TRACE(message, ...) (void(0))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057#endif
58
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000059// A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing.
Austin Kinrossf0360c62014-10-20 14:26:13 -070060#if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
Jamie Madill14aa40f2015-01-07 15:12:47 -050061#define FIXME(message, ...) gl::trace(false, gl::MESSAGE_FIXME, "fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040062#else
63#define FIXME(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000064#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000066// A macro to output a function call and its arguments to the debugging log, in case of error.
Austin Kinrossf0360c62014-10-20 14:26:13 -070067#if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
Jamie Madill14aa40f2015-01-07 15:12:47 -050068#define ERR(message, ...) gl::trace(false, gl::MESSAGE_ERR, "err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040069#else
70#define ERR(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000071#endif
72
73// A macro to log a performance event around a scope.
Austin Kinrossf0360c62014-10-20 14:26:13 -070074#if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
Geoff Langf5713122013-10-07 17:06:30 -040075#if defined(_MSC_VER)
Ehsan Akhgaric1cc4c42014-07-02 13:09:07 -040076#define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper ## __LINE__("%s" message "\n", __FUNCTION__, __VA_ARGS__);
daniel@transgaming.com1825d8e2012-08-27 16:25:29 +000077#else
78#define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper(message "\n", ##__VA_ARGS__);
Geoff Langf5713122013-10-07 17:06:30 -040079#endif // _MSC_VER
80#else
81#define EVENT(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000082#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083
84// A macro asserting a condition and outputting failures to the debug log
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000085#if !defined(NDEBUG)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086#define ASSERT(expression) do { \
87 if(!(expression)) \
88 ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000089 assert(expression); \
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090 } while(0)
Geoff Lang9cd19152014-05-28 15:54:34 -040091#define UNUSED_ASSERTION_VARIABLE(variable)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000092#else
93#define ASSERT(expression) (void(0))
Geoff Lang9cd19152014-05-28 15:54:34 -040094#define UNUSED_ASSERTION_VARIABLE(variable) ((void)variable)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000095#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
Austin Kinrossf0360c62014-10-20 14:26:13 -070097#ifndef ANGLE_ENABLE_DEBUG_TRACE
Jamie Madillbae30a02014-06-17 10:45:02 -040098#define UNUSED_TRACE_VARIABLE(variable) ((void)variable)
99#else
100#define UNUSED_TRACE_VARIABLE(variable)
101#endif
102
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103// A macro to indicate unimplemented functionality
shannonwoods@chromium.orgeafb0692013-05-30 00:20:44 +0000104
Shannon Woods950cb602014-09-18 10:01:56 -0400105#if defined (ANGLE_TEST_CONFIG)
106#define NOASSERT_UNIMPLEMENTED 1
107#endif
108
shannonwoods@chromium.orgeafb0692013-05-30 00:20:44 +0000109// Define NOASSERT_UNIMPLEMENTED to non zero to skip the assert fail in the unimplemented checks
110// This will allow us to test with some automated test suites (eg dEQP) without crashing
111#ifndef NOASSERT_UNIMPLEMENTED
112#define NOASSERT_UNIMPLEMENTED 0
113#endif
114
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000115#if !defined(NDEBUG)
116#define UNIMPLEMENTED() do { \
117 FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); \
shannonwoods@chromium.orgeafb0692013-05-30 00:20:44 +0000118 assert(NOASSERT_UNIMPLEMENTED); \
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000119 } while(0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120#else
121 #define UNIMPLEMENTED() FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__)
122#endif
123
124// A macro for code which is not expected to be reached under valid assumptions
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000125#if !defined(NDEBUG)
126#define UNREACHABLE() do { \
127 ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__); \
128 assert(false); \
129 } while(0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130#else
131 #define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__)
132#endif
133
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000134// A macro that determines whether an object has a given runtime type.
Geoff Lang8690da82013-10-08 10:57:09 -0400135#if !defined(NDEBUG) && (!defined(_MSC_VER) || defined(_CPPRTTI)) && (!defined(__GNUC__) || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || defined(__GXX_RTTI))
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000136#define HAS_DYNAMIC_TYPE(type, obj) (dynamic_cast<type >(obj) != NULL)
137#else
138#define HAS_DYNAMIC_TYPE(type, obj) true
139#endif
140
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141// A macro functioning as a compile-time assert to validate constant conditions
Jacek Cabana5521de2014-10-01 17:23:46 +0200142#if (defined(_MSC_VER) && _MSC_VER >= 1600) || (defined(__GNUC__) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3))
Geoff Langd0f8e822013-09-30 15:21:53 -0400143#define META_ASSERT_MSG(condition, msg) static_assert(condition, msg)
144#else
Jamie Madill4f267862014-04-17 15:53:37 -0400145#define META_ASSERT_CONCAT(a, b) a ## b
146#define META_ASSERT_CONCAT2(a, b) META_ASSERT_CONCAT(a, b)
147#define META_ASSERT_MSG(condition, msg) typedef int META_ASSERT_CONCAT2(COMPILE_TIME_ASSERT_, __LINE__)[static_cast<bool>(condition)?1:-1]
Geoff Langd0f8e822013-09-30 15:21:53 -0400148#endif
149#define META_ASSERT(condition) META_ASSERT_MSG(condition, "compile time assertion failed.")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150
151#endif // COMMON_DEBUG_H_