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