Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / gallium / auxiliary / util / u_debug.h
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 * @file
30 * Cross-platform debugging helpers.
31 *
32 * For now it just has assert and printf replacements, but it might be extended
33 * with stack trace reports and more advanced logging in the near future.
34 *
35 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
36 */
37
38 #ifndef U_DEBUG_H_
39 #define U_DEBUG_H_
40
41
42 #include <stdarg.h>
43
44 #include "pipe/p_compiler.h"
45
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51
52 #if defined(DBG) || defined(DEBUG)
53 #ifndef DEBUG
54 #define DEBUG 1
55 #endif
56 #else
57 #ifndef NDEBUG
58 #define NDEBUG 1
59 #endif
60 #endif
61
62
63 /* MSVC bebore VC7 does not have the __FUNCTION__ macro */
64 #if defined(_MSC_VER) && _MSC_VER < 1300
65 #define __FUNCTION__ "???"
66 #endif
67
68
69 void _debug_vprintf(const char *format, va_list ap);
70
71
72 static INLINE void
73 _debug_printf(const char *format, ...)
74 {
75 va_list ap;
76 va_start(ap, format);
77 _debug_vprintf(format, ap);
78 va_end(ap);
79 }
80
81
82 /**
83 * Print debug messages.
84 *
85 * The actual channel used to output debug message is platform specific. To
86 * avoid misformating or truncation, follow these rules of thumb:
87 * - output whole lines
88 * - avoid outputing large strings (512 bytes is the current maximum length
89 * that is guaranteed to be printed in all platforms)
90 */
91 #if !defined(PIPE_OS_HAIKU)
92 static INLINE void
93 debug_printf(const char *format, ...)
94 {
95 #ifdef DEBUG
96 va_list ap;
97 va_start(ap, format);
98 _debug_vprintf(format, ap);
99 va_end(ap);
100 #else
101 (void) format; /* silence warning */
102 #endif
103 }
104
105 #endif /* !PIPE_OS_HAIKU */
106
107 /*
108 * ... isn't portable so we need to pass arguments in parentheses.
109 *
110 * usage:
111 * debug_printf_once(("awnser: %i\n", 42));
112 */
113 #define debug_printf_once(args) \
114 do { \
115 static boolean once = TRUE; \
116 if (once) { \
117 once = FALSE; \
118 debug_printf args; \
119 } \
120 } while (0)
121
122
123 #ifdef DEBUG
124 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
125 #else
126 #define debug_vprintf(_format, _ap) ((void)0)
127 #endif
128
129
130 #ifdef DEBUG
131 /**
132 * Dump a blob in hex to the same place that debug_printf sends its
133 * messages.
134 */
135 void debug_print_blob( const char *name, const void *blob, unsigned size );
136
137 /* Print a message along with a prettified format string
138 */
139 void debug_print_format(const char *msg, unsigned fmt );
140 #else
141 #define debug_print_blob(_name, _blob, _size) ((void)0)
142 #define debug_print_format(_msg, _fmt) ((void)0)
143 #endif
144
145
146 /**
147 * Hard-coded breakpoint.
148 */
149 #ifdef DEBUG
150 #if (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)) && defined(PIPE_CC_GCC)
151 #define debug_break() __asm("int3")
152 #elif defined(PIPE_CC_MSVC)
153 #define debug_break() __debugbreak()
154 #else
155 void debug_break(void);
156 #endif
157 #else /* !DEBUG */
158 #define debug_break() ((void)0)
159 #endif /* !DEBUG */
160
161
162 long
163 debug_get_num_option(const char *name, long dfault);
164
165 void _debug_assert_fail(const char *expr,
166 const char *file,
167 unsigned line,
168 const char *function);
169
170
171 /**
172 * Assert macro
173 *
174 * Do not expect that the assert call terminates -- errors must be handled
175 * regardless of assert behavior.
176 */
177 #ifdef DEBUG
178 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
179 #else
180 #define debug_assert(expr) ((void)(expr))
181 #endif
182
183
184 /** Override standard assert macro */
185 #ifdef assert
186 #undef assert
187 #endif
188 #define assert(expr) debug_assert(expr)
189
190
191 /**
192 * Output the current function name.
193 */
194 #ifdef DEBUG
195 #define debug_checkpoint() \
196 _debug_printf("%s\n", __FUNCTION__)
197 #else
198 #define debug_checkpoint() \
199 ((void)0)
200 #endif
201
202
203 /**
204 * Output the full source code position.
205 */
206 #ifdef DEBUG
207 #define debug_checkpoint_full() \
208 _debug_printf("%s:%u:%s", __FILE__, __LINE__, __FUNCTION__)
209 #else
210 #define debug_checkpoint_full() \
211 ((void)0)
212 #endif
213
214
215 /**
216 * Output a warning message. Muted on release version.
217 */
218 #ifdef DEBUG
219 #define debug_warning(__msg) \
220 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
221 #else
222 #define debug_warning(__msg) \
223 ((void)0)
224 #endif
225
226
227 /**
228 * Output an error message. Not muted on release version.
229 */
230 #ifdef DEBUG
231 #define debug_error(__msg) \
232 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
233 #else
234 #define debug_error(__msg) \
235 _debug_printf("error: %s\n", __msg)
236 #endif
237
238
239 /**
240 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
241 */
242 struct debug_named_value
243 {
244 const char *name;
245 unsigned long value;
246 };
247
248
249 /**
250 * Some C pre-processor magic to simplify creating named values.
251 *
252 * Example:
253 * @code
254 * static const debug_named_value my_names[] = {
255 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
256 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
257 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
258 * DEBUG_NAMED_VALUE_END
259 * };
260 *
261 * ...
262 * debug_printf("%s = %s\n",
263 * name,
264 * debug_dump_enum(my_names, my_value));
265 * ...
266 * @endcode
267 */
268 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol}
269 #define DEBUG_NAMED_VALUE_END {NULL, 0}
270
271
272 /**
273 * Convert a enum value to a string.
274 */
275 const char *
276 debug_dump_enum(const struct debug_named_value *names,
277 unsigned long value);
278
279 const char *
280 debug_dump_enum_noprefix(const struct debug_named_value *names,
281 const char *prefix,
282 unsigned long value);
283
284
285 /**
286 * Convert binary flags value to a string.
287 */
288 const char *
289 debug_dump_flags(const struct debug_named_value *names,
290 unsigned long value);
291
292
293 /**
294 * Get option.
295 *
296 * It is an alias for getenv on Linux.
297 *
298 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
299 * endings with one option per line as
300 *
301 * NAME=value
302 *
303 * This file must be terminated with an extra empty line.
304 */
305 const char *
306 debug_get_option(const char *name, const char *dfault);
307
308 boolean
309 debug_get_bool_option(const char *name, boolean dfault);
310
311 long
312 debug_get_num_option(const char *name, long dfault);
313
314 unsigned long
315 debug_get_flags_option(const char *name,
316 const struct debug_named_value *flags,
317 unsigned long dfault);
318
319
320 void *
321 debug_malloc(const char *file, unsigned line, const char *function,
322 size_t size);
323
324 void
325 debug_free(const char *file, unsigned line, const char *function,
326 void *ptr);
327
328 void *
329 debug_calloc(const char *file, unsigned line, const char *function,
330 size_t count, size_t size );
331
332 void *
333 debug_realloc(const char *file, unsigned line, const char *function,
334 void *old_ptr, size_t old_size, size_t new_size );
335
336 unsigned long
337 debug_memory_begin(void);
338
339 void
340 debug_memory_end(unsigned long beginning);
341
342
343 #if defined(PROFILE) && defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
344
345 void
346 debug_profile_start(void);
347
348 void
349 debug_profile_stop(void);
350
351 #endif
352
353
354 #ifdef DEBUG
355 struct pipe_surface;
356 struct pipe_transfer;
357 void debug_dump_image(const char *prefix,
358 unsigned format, unsigned cpp,
359 unsigned width, unsigned height,
360 unsigned stride,
361 const void *data);
362 void debug_dump_surface(const char *prefix,
363 struct pipe_surface *surface);
364 void debug_dump_surface_bmp(const char *filename,
365 struct pipe_surface *surface);
366 void debug_dump_transfer_bmp(const char *filename,
367 struct pipe_transfer *transfer);
368 void debug_dump_float_rgba_bmp(const char *filename,
369 unsigned width, unsigned height,
370 float *rgba, unsigned stride);
371 #else
372 #define debug_dump_image(prefix, format, cpp, width, height, stride, data) ((void)0)
373 #define debug_dump_surface(prefix, surface) ((void)0)
374 #define debug_dump_surface_bmp(filename, surface) ((void)0)
375 #define debug_dump_transfer_bmp(filename, transfer) ((void)0)
376 #define debug_dump_rgba_float_bmp(filename, width, height, rgba, stride) ((void)0)
377 #endif
378
379
380 #ifdef __cplusplus
381 }
382 #endif
383
384 #endif /* U_DEBUG_H_ */