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