Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / auxiliary / util / p_debug.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include <stdarg.h>
30
31 #ifdef WIN32
32 #include <windows.h>
33 #include <winddi.h>
34 #else
35 #include <stdio.h>
36 #include <stdlib.h>
37 #endif
38
39 #include "pipe/p_compiler.h"
40 #include "pipe/p_util.h"
41 #include "pipe/p_debug.h"
42
43
44 #ifdef WIN32
45 static INLINE void
46 rpl_EngDebugPrint(const char *format, ...)
47 {
48 va_list ap;
49 va_start(ap, format);
50 EngDebugPrint("", (PCHAR)format, ap);
51 va_end(ap);
52 }
53
54 int rpl_vsnprintf(char *, size_t, const char *, va_list);
55 #endif
56
57
58 void debug_vprintf(const char *format, va_list ap)
59 {
60 #ifdef WIN32
61 #ifndef WINCE
62 /* EngDebugPrint does not handle float point arguments, so we need to use
63 * our own vsnprintf implementation */
64 char buf[512 + 1];
65 rpl_vsnprintf(buf, sizeof(buf), format, ap);
66 rpl_EngDebugPrint("%s", buf);
67 #else
68 /* TODO: Implement debug print for WINCE */
69 #endif
70 #else
71 vfprintf(stderr, format, ap);
72 #endif
73 }
74
75
76 void debug_printf(const char *format, ...)
77 {
78 va_list ap;
79 va_start(ap, format);
80 debug_vprintf(format, ap);
81 va_end(ap);
82 }
83
84
85 /* TODO: implement a debug_abort that calls EngBugCheckEx on WIN32 */
86
87
88 static INLINE void debug_break(void)
89 {
90 #if (defined(__i386__) || defined(__386__)) && defined(__GNUC__)
91 __asm("int3");
92 #elif (defined(__i386__) || defined(__386__)) && defined(__MSC__)
93 _asm {int 3};
94 #elif defined(WIN32) && !defined(WINCE)
95 EngDebugBreak();
96 #else
97 abort();
98 #endif
99 }
100
101 #if defined(WIN32)
102 ULONG_PTR debug_config_file = 0;
103 void *mapped_config_file = 0;
104
105 enum {
106 eAssertAbortEn = 0x1,
107 };
108
109 /* Check for aborts enabled. */
110 static unsigned abort_en()
111 {
112 if (!mapped_config_file)
113 {
114 /* Open an 8 byte file for configuration data. */
115 mapped_config_file = EngMapFile(L"\\??\\c:\\gaDebug.cfg", 8, &debug_config_file);
116 }
117
118 /* A value of "0" (ascii) in the configuration file will clear the
119 * first 8 bits in the test byte.
120 *
121 * A value of "1" (ascii) in the configuration file will set the
122 * first bit in the test byte.
123 *
124 * A value of "2" (ascii) in the configuration file will set the
125 * second bit in the test byte.
126 *
127 * Currently the only interesting values are 0 and 1, which clear
128 * and set abort-on-assert behaviour respectively.
129 */
130 return ((((char *)mapped_config_file)[0]) - 0x30) & eAssertAbortEn;
131 }
132 #else /* WIN32 */
133 static unsigned abort_en()
134 {
135 return !GETENV("GALLIUM_ABORT_ON_ASSERT");
136 }
137 #endif
138
139 void debug_assert_fail(const char *expr, const char *file, unsigned line)
140 {
141 debug_printf("%s:%i: Assertion `%s' failed.\n", file, line, expr);
142 if (abort_en())
143 {
144 debug_break();
145 } else
146 {
147 debug_printf("continuing...\n");
148 }
149 }
150
151
152 #define DEBUG_MASK_TABLE_SIZE 256
153
154
155 /**
156 * Mask hash table.
157 *
158 * For now we just take the lower bits of the key, and do no attempt to solve
159 * collisions. Use a proper hash table when we have dozens of drivers.
160 */
161 static uint32_t debug_mask_table[DEBUG_MASK_TABLE_SIZE];
162
163
164 void debug_mask_set(uint32_t uuid, uint32_t mask)
165 {
166 unsigned hash = uuid & (DEBUG_MASK_TABLE_SIZE - 1);
167 debug_mask_table[hash] = mask;
168 }
169
170
171 uint32_t debug_mask_get(uint32_t uuid)
172 {
173 unsigned hash = uuid & (DEBUG_MASK_TABLE_SIZE - 1);
174 return debug_mask_table[hash];
175 }
176
177
178 void debug_mask_vprintf(uint32_t uuid, uint32_t what, const char *format, va_list ap)
179 {
180 uint32_t mask = debug_mask_get(uuid);
181 if(mask & what)
182 debug_vprintf(format, ap);
183 }