cc036dabf82fe8c0473637d0bc5a8daa0fd9c0b8
[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 int rpl_snprintf(char *str, size_t size, const char *format, ...);
56 #define vsnprintf rpl_vsnprintf
57 #define snprintf rpl_snprintf
58 #endif
59
60
61 void _debug_vprintf(const char *format, va_list ap)
62 {
63 #ifdef WIN32
64 #ifndef WINCE
65 /* EngDebugPrint does not handle float point arguments, so we need to use
66 * our own vsnprintf implementation */
67 char buf[512 + 1];
68 rpl_vsnprintf(buf, sizeof(buf), format, ap);
69 rpl_EngDebugPrint("%s", buf);
70 #else
71 /* TODO: Implement debug print for WINCE */
72 #endif
73 #else
74 vfprintf(stderr, format, ap);
75 #endif
76 }
77
78
79 #ifdef DEBUG
80 void debug_print_blob( const char *name,
81 const void *blob,
82 unsigned size )
83 {
84 const unsigned *ublob = (const unsigned *)blob;
85 unsigned i;
86
87 debug_printf("%s (%d dwords%s)\n", name, size/4,
88 size%4 ? "... plus a few bytes" : "");
89
90 for (i = 0; i < size/4; i++) {
91 debug_printf("%d:\t%08x\n", i, ublob[i]);
92 }
93 }
94 #endif
95
96
97 void _debug_break(void)
98 {
99 #if (defined(__i386__) || defined(__386__)) && defined(__GNUC__)
100 __asm("int3");
101 #elif (defined(__i386__) || defined(__386__)) && defined(__MSC__)
102 _asm {int 3};
103 #elif defined(WIN32) && !defined(WINCE)
104 EngDebugBreak();
105 #else
106 abort();
107 #endif
108 }
109
110
111 #ifdef WIN32
112 static const char *
113 find(const char *start, const char *end, char c)
114 {
115 const char *p;
116 for(p = start; !end || p != end; ++p) {
117 if(*p == c)
118 return p;
119 if(*p < 32)
120 break;
121 }
122 return NULL;
123 }
124
125 static int
126 compare(const char *start, const char *end, const char *s)
127 {
128 const char *p, *q;
129 for(p = start, q = s; p != end && *q != '\0'; ++p, ++q) {
130 if(*p != *q)
131 return 0;
132 }
133 return p == end && *q == '\0';
134 }
135
136 static void
137 copy(char *dst, const char *start, const char *end, size_t n)
138 {
139 const char *p;
140 char *q;
141 for(p = start, q = dst, n = n - 1; p != end && n; ++p, ++q, --n)
142 *q = *p;
143 *q = '\0';
144 }
145 #endif
146
147
148 const char *
149 debug_get_option(const char *name, const char *dfault)
150 {
151 const char *result;
152 #ifdef WIN32
153 ULONG_PTR iFile = 0;
154 const void *pMap = NULL;
155 const char *sol, *eol, *sep;
156 static char output[1024];
157
158 pMap = EngMapFile(L"\\??\\c:\\gallium.cfg", 0, &iFile);
159 if(!pMap)
160 result = dfault;
161 else {
162 sol = (const char *)pMap;
163 while(1) {
164 /* TODO: handle LF line endings */
165 eol = find(sol, NULL, '\r');
166 if(!eol || eol == sol)
167 break;
168 sep = find(sol, eol, '=');
169 if(!sep)
170 break;
171 if(compare(sol, sep, name)) {
172 copy(output, sep + 1, eol, sizeof(output));
173 result = output;
174 break;
175 }
176 sol = eol + 2;
177 }
178 EngUnmapFile(iFile);
179 }
180 #else
181
182 result = getenv(name);
183 if(!result)
184 result = dfault;
185 #endif
186
187 if(result)
188 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result);
189 else
190 debug_printf("%s: %s = (null)\n", __FUNCTION__, name);
191
192 return result;
193 }
194
195 boolean
196 debug_get_bool_option(const char *name, boolean dfault)
197 {
198 const char *str = debug_get_option(name, NULL);
199 boolean result;
200
201 if(str == NULL)
202 result = dfault;
203 else if(!strcmp(str, "no"))
204 result = FALSE;
205 else if(!strcmp(str, "0"))
206 result = FALSE;
207 else if(!strcmp(str, "f"))
208 result = FALSE;
209 else if(!strcmp(str, "false"))
210 result = FALSE;
211 else
212 result = TRUE;
213
214 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? "TRUE" : "FALSE");
215
216 return result;
217 }
218
219
220 long
221 debug_get_num_option(const char *name, long dfault)
222 {
223 /* FIXME */
224 return dfault;
225 }
226
227
228 unsigned long
229 debug_get_flags_option(const char *name,
230 const struct debug_named_value *flags,
231 unsigned long dfault)
232 {
233 unsigned long result;
234 const char *str;
235
236 str = debug_get_option(name, NULL);
237 if(!str)
238 result = dfault;
239 else {
240 result = 0;
241 while( flags->name ) {
242 if (!strcmp(str, "all") || strstr(str, flags->name ))
243 result |= flags->value;
244 ++flags;
245 }
246 }
247
248 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
249
250 return result;
251 }
252
253
254 #if defined(WIN32)
255 ULONG_PTR debug_config_file = 0;
256 void *mapped_config_file = 0;
257
258 enum {
259 eAssertAbortEn = 0x1,
260 };
261
262 /* Check for aborts enabled. */
263 static unsigned abort_en(void)
264 {
265 if (!mapped_config_file)
266 {
267 /* Open an 8 byte file for configuration data. */
268 mapped_config_file = EngMapFile(L"\\??\\c:\\gaDebug.cfg", 8, &debug_config_file);
269 }
270
271 /* A value of "0" (ascii) in the configuration file will clear the
272 * first 8 bits in the test byte.
273 *
274 * A value of "1" (ascii) in the configuration file will set the
275 * first bit in the test byte.
276 *
277 * A value of "2" (ascii) in the configuration file will set the
278 * second bit in the test byte.
279 *
280 * Currently the only interesting values are 0 and 1, which clear
281 * and set abort-on-assert behaviour respectively.
282 */
283 return ((((char *)mapped_config_file)[0]) - 0x30) & eAssertAbortEn;
284 }
285 #else /* WIN32 */
286 static unsigned abort_en(void)
287 {
288 return !GETENV("GALLIUM_ABORT_ON_ASSERT");
289 }
290 #endif
291
292 void _debug_assert_fail(const char *expr,
293 const char *file,
294 unsigned line,
295 const char *function)
296 {
297 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
298 if (abort_en())
299 {
300 debug_break();
301 } else
302 {
303 _debug_printf("continuing...\n");
304 }
305 }
306
307
308 const char *
309 debug_dump_enum(const struct debug_named_value *names,
310 unsigned long value)
311 {
312 static char rest[256];
313
314 while(names->name) {
315 if(names->value == value)
316 return names->name;
317 ++names;
318 }
319
320 snprintf(rest, sizeof(rest), "0x%08lx", value);
321 return rest;
322 }
323
324
325 const char *
326 debug_dump_flags(const struct debug_named_value *names,
327 unsigned long value)
328 {
329 static char output[4096];
330 static char rest[256];
331 int first = 1;
332
333 output[0] = '\0';
334
335 while(names->name) {
336 if((names->value & value) == names->value) {
337 if (!first)
338 strncat(output, "|", sizeof(output));
339 else
340 first = 0;
341 strncat(output, names->name, sizeof(output));
342 value &= ~names->value;
343 }
344 ++names;
345 }
346
347 if (value) {
348 if (!first)
349 strncat(output, "|", sizeof(output));
350 else
351 first = 0;
352
353 snprintf(rest, sizeof(rest), "0x%08lx", value);
354 strncat(output, rest, sizeof(output));
355 }
356
357 if(first)
358 return "0";
359
360 return output;
361 }
362
363