bd58e0e3a8b12c1e10feb21e012d5b635d0a5604
[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 #if defined(WIN32)
111 ULONG_PTR debug_config_file = 0;
112 void *mapped_config_file = 0;
113
114 enum {
115 eAssertAbortEn = 0x1,
116 };
117
118 /* Check for aborts enabled. */
119 static unsigned abort_en()
120 {
121 if (!mapped_config_file)
122 {
123 /* Open an 8 byte file for configuration data. */
124 mapped_config_file = EngMapFile(L"\\??\\c:\\gaDebug.cfg", 8, &debug_config_file);
125 }
126
127 /* A value of "0" (ascii) in the configuration file will clear the
128 * first 8 bits in the test byte.
129 *
130 * A value of "1" (ascii) in the configuration file will set the
131 * first bit in the test byte.
132 *
133 * A value of "2" (ascii) in the configuration file will set the
134 * second bit in the test byte.
135 *
136 * Currently the only interesting values are 0 and 1, which clear
137 * and set abort-on-assert behaviour respectively.
138 */
139 return ((((char *)mapped_config_file)[0]) - 0x30) & eAssertAbortEn;
140 }
141 #else /* WIN32 */
142 static unsigned abort_en()
143 {
144 return !GETENV("GALLIUM_ABORT_ON_ASSERT");
145 }
146 #endif
147
148 void _debug_assert_fail(const char *expr,
149 const char *file,
150 unsigned line,
151 const char *function)
152 {
153 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
154 if (abort_en())
155 {
156 debug_break();
157 } else
158 {
159 _debug_printf("continuing...\n");
160 }
161 }
162
163
164 const char *
165 debug_dump_enum(const struct debug_named_value *names,
166 unsigned long value)
167 {
168 static char rest[256];
169
170 while(names->name) {
171 if(names->value == value)
172 return names->name;
173 ++names;
174 }
175
176 snprintf(rest, sizeof(rest), "0x%08lx", value);
177 return rest;
178 }
179
180
181 const char *
182 debug_dump_flags(const struct debug_named_value *names,
183 unsigned long value)
184 {
185 static char output[4096];
186 static char rest[256];
187 int first = 1;
188
189 output[0] = '\0';
190
191 while(names->name) {
192 if((names->value & value) == names->value) {
193 if (!first)
194 strncat(output, "|", sizeof(output));
195 else
196 first = 0;
197 strncat(output, names->name, sizeof(output));
198 value &= ~names->value;
199 }
200 ++names;
201 }
202
203 if (value) {
204 if (!first)
205 strncat(output, "|", sizeof(output));
206 else
207 first = 0;
208
209 snprintf(rest, sizeof(rest), "0x%08lx", value);
210 strncat(output, rest, sizeof(output));
211 }
212
213 if(first)
214 return "0";
215
216 return output;
217 }
218
219