Tizen 2.0 Release
[profile/ivi/osmesa.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 "os/os_misc.h"
43
44
45 #ifdef  __cplusplus
46 extern "C" {
47 #endif
48
49
50 #if defined(__GNUC__)
51 #define _util_printf_format(fmt, list) __attribute__ ((format (printf, fmt, list)))
52 #else
53 #define _util_printf_format(fmt, list)
54 #endif
55
56 void _debug_vprintf(const char *format, va_list ap);
57    
58
59 static INLINE void
60 _debug_printf(const char *format, ...)
61 {
62    va_list ap;
63    va_start(ap, format);
64    _debug_vprintf(format, ap);
65    va_end(ap);
66 }
67
68
69 /**
70  * Print debug messages.
71  *
72  * The actual channel used to output debug message is platform specific. To
73  * avoid misformating or truncation, follow these rules of thumb:
74  * - output whole lines
75  * - avoid outputing large strings (512 bytes is the current maximum length
76  * that is guaranteed to be printed in all platforms)
77  */
78 #if !defined(PIPE_OS_HAIKU)
79 static INLINE void
80 debug_printf(const char *format, ...) _util_printf_format(1,2);
81
82 static INLINE void
83 debug_printf(const char *format, ...)
84 {
85 #ifdef DEBUG
86    va_list ap;
87    va_start(ap, format);
88    _debug_vprintf(format, ap);
89    va_end(ap);
90 #else
91    (void) format; /* silence warning */
92 #endif
93 }
94
95 #endif /* !PIPE_OS_HAIKU */
96
97 /*
98  * ... isn't portable so we need to pass arguments in parentheses.
99  *
100  * usage:
101  *    debug_printf_once(("awnser: %i\n", 42));
102  */
103 #define debug_printf_once(args) \
104    do { \
105       static boolean once = TRUE; \
106       if (once) { \
107          once = FALSE; \
108          debug_printf args; \
109       } \
110    } while (0)
111
112
113 #ifdef DEBUG
114 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
115 #else
116 #define debug_vprintf(_format, _ap) ((void)0)
117 #endif
118
119
120 #ifdef DEBUG
121 /**
122  * Dump a blob in hex to the same place that debug_printf sends its
123  * messages.
124  */
125 void debug_print_blob( const char *name, const void *blob, unsigned size );
126
127 /* Print a message along with a prettified format string
128  */
129 void debug_print_format(const char *msg, unsigned fmt );
130 #else
131 #define debug_print_blob(_name, _blob, _size) ((void)0)
132 #define debug_print_format(_msg, _fmt) ((void)0)
133 #endif
134
135
136 /**
137  * Hard-coded breakpoint.
138  */
139 #ifdef DEBUG
140 #define debug_break() os_break()
141 #else /* !DEBUG */
142 #define debug_break() ((void)0)
143 #endif /* !DEBUG */
144
145
146 long
147 debug_get_num_option(const char *name, long dfault);
148
149 void _debug_assert_fail(const char *expr, 
150                         const char *file, 
151                         unsigned line, 
152                         const char *function);
153
154
155 /** 
156  * Assert macro
157  * 
158  * Do not expect that the assert call terminates -- errors must be handled 
159  * regardless of assert behavior.
160  *
161  * For non debug builds the assert macro will expand to a no-op, so do not
162  * call functions with side effects in the assert expression.
163  */
164 #ifdef DEBUG
165 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
166 #else
167 #define debug_assert(expr) do { } while (0 && (expr))
168 #endif
169
170
171 /** Override standard assert macro */
172 #ifdef assert
173 #undef assert
174 #endif
175 #define assert(expr) debug_assert(expr)
176
177
178 /**
179  * Output the current function name.
180  */
181 #ifdef DEBUG
182 #define debug_checkpoint() \
183    _debug_printf("%s\n", __FUNCTION__)
184 #else
185 #define debug_checkpoint() \
186    ((void)0) 
187 #endif
188
189
190 /**
191  * Output the full source code position.
192  */
193 #ifdef DEBUG
194 #define debug_checkpoint_full() \
195    _debug_printf("%s:%u:%s\n", __FILE__, __LINE__, __FUNCTION__)
196 #else
197 #define debug_checkpoint_full() \
198    ((void)0) 
199 #endif
200
201
202 /**
203  * Output a warning message. Muted on release version.
204  */
205 #ifdef DEBUG
206 #define debug_warning(__msg) \
207    _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
208 #else
209 #define debug_warning(__msg) \
210    ((void)0) 
211 #endif
212
213
214 /**
215  * Output an error message. Not muted on release version.
216  */
217 #ifdef DEBUG
218 #define debug_error(__msg) \
219    _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg) 
220 #else
221 #define debug_error(__msg) \
222    _debug_printf("error: %s\n", __msg)
223 #endif
224
225
226 /**
227  * Used by debug_dump_enum and debug_dump_flags to describe symbols.
228  */
229 struct debug_named_value
230 {
231    const char *name;
232    unsigned long value;
233    const char *desc;
234 };
235
236
237 /**
238  * Some C pre-processor magic to simplify creating named values.
239  * 
240  * Example:
241  * @code
242  * static const debug_named_value my_names[] = {
243  *    DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
244  *    DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
245  *    DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
246  *    DEBUG_NAMED_VALUE_END
247  * };
248  * 
249  *    ...
250  *    debug_printf("%s = %s\n", 
251  *                 name,
252  *                 debug_dump_enum(my_names, my_value));
253  *    ...
254  * @endcode
255  */
256 #define DEBUG_NAMED_VALUE(__symbol) DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, NULL)
257 #define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
258 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
259
260
261 /**
262  * Convert a enum value to a string.
263  */
264 const char *
265 debug_dump_enum(const struct debug_named_value *names, 
266                 unsigned long value);
267
268 const char *
269 debug_dump_enum_noprefix(const struct debug_named_value *names, 
270                          const char *prefix,
271                          unsigned long value);
272
273
274 /**
275  * Convert binary flags value to a string.
276  */
277 const char *
278 debug_dump_flags(const struct debug_named_value *names, 
279                  unsigned long value);
280
281
282 /**
283  * Function enter exit loggers
284  */
285 #ifdef DEBUG
286 int debug_funclog_enter(const char* f, const int line, const char* file);
287 void debug_funclog_exit(const char* f, const int line, const char* file);
288 void debug_funclog_enter_exit(const char* f, const int line, const char* file);
289
290 #define DEBUG_FUNCLOG_ENTER() \
291    int __debug_decleration_work_around = \
292       debug_funclog_enter(__FUNCTION__, __LINE__, __FILE__)
293 #define DEBUG_FUNCLOG_EXIT() \
294    do { \
295       (void)__debug_decleration_work_around; \
296       debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
297       return; \
298    } while(0)
299 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
300    do { \
301       (void)__debug_decleration_work_around; \
302       debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
303       return ret; \
304    } while(0)
305 #define DEBUG_FUNCLOG_ENTER_EXIT() \
306    debug_funclog_enter_exit(__FUNCTION__, __LINE__, __FILE__)
307
308 #else
309 #define DEBUG_FUNCLOG_ENTER() \
310    int __debug_decleration_work_around
311 #define DEBUG_FUNCLOG_EXIT() \
312    do { (void)__debug_decleration_work_around; return; } while(0)
313 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
314    do { (void)__debug_decleration_work_around; return ret; } while(0)
315 #define DEBUG_FUNCLOG_ENTER_EXIT()
316 #endif
317
318
319 /**
320  * Get option.
321  * 
322  * It is an alias for getenv on Linux. 
323  * 
324  * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line 
325  * endings with one option per line as
326  *  
327  *   NAME=value
328  * 
329  * This file must be terminated with an extra empty line.
330  */
331 const char *
332 debug_get_option(const char *name, const char *dfault);
333
334 boolean
335 debug_get_bool_option(const char *name, boolean dfault);
336
337 long
338 debug_get_num_option(const char *name, long dfault);
339
340 unsigned long
341 debug_get_flags_option(const char *name, 
342                        const struct debug_named_value *flags,
343                        unsigned long dfault);
344
345 #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \
346 static boolean \
347 debug_get_option_ ## sufix (void) \
348 { \
349    static boolean first = TRUE; \
350    static boolean value; \
351    if (first) { \
352       first = FALSE; \
353       value = debug_get_bool_option(name, dfault); \
354    } \
355    return value; \
356 }
357
358 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
359 static long \
360 debug_get_option_ ## sufix (void) \
361 { \
362    static boolean first = TRUE; \
363    static long value; \
364    if (first) { \
365       first = FALSE; \
366       value = debug_get_num_option(name, dfault); \
367    } \
368    return value; \
369 }
370
371 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
372 static unsigned long \
373 debug_get_option_ ## sufix (void) \
374 { \
375    static boolean first = TRUE; \
376    static unsigned long value; \
377    if (first) { \
378       first = FALSE; \
379       value = debug_get_flags_option(name, flags, dfault); \
380    } \
381    return value; \
382 }
383
384
385 unsigned long
386 debug_memory_begin(void);
387
388 void 
389 debug_memory_end(unsigned long beginning);
390
391
392 #ifdef DEBUG
393 struct pipe_context;
394 struct pipe_surface;
395 struct pipe_transfer;
396 struct pipe_resource;
397
398 void debug_dump_image(const char *prefix,
399                       unsigned format, unsigned cpp,
400                       unsigned width, unsigned height,
401                       unsigned stride,
402                       const void *data);
403 void debug_dump_surface(struct pipe_context *pipe,
404                         const char *prefix,
405                         struct pipe_surface *surface);   
406 void debug_dump_texture(struct pipe_context *pipe,
407                         const char *prefix,
408                         struct pipe_resource *texture);
409 void debug_dump_surface_bmp(struct pipe_context *pipe,
410                             const char *filename,
411                             struct pipe_surface *surface);
412 void debug_dump_transfer_bmp(struct pipe_context *pipe,
413                              const char *filename,
414                              struct pipe_transfer *transfer);
415 void debug_dump_float_rgba_bmp(const char *filename,
416                                unsigned width, unsigned height,
417                                float *rgba, unsigned stride);
418 #else
419 #define debug_dump_image(prefix, format, cpp, width, height, stride, data) ((void)0)
420 #define debug_dump_surface(pipe, prefix, surface) ((void)0)
421 #define debug_dump_surface_bmp(pipe, filename, surface) ((void)0)
422 #define debug_dump_transfer_bmp(filename, transfer) ((void)0)
423 #define debug_dump_float_rgba_bmp(filename, width, height, rgba, stride) ((void)0)
424 #endif
425
426
427 #ifdef  __cplusplus
428 }
429 #endif
430
431 #endif /* U_DEBUG_H_ */