1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 <jfonseca@vmware.com>
36 */
37
38 #ifndef U_DEBUG_H_
39 #define U_DEBUG_H_
40
41 #include <stdarg.h>
42 #include <string.h>
43 #if !defined(_WIN32)
44 #include <sys/types.h>
45 #include <unistd.h>
46 #endif
47
48 #include "util/os_misc.h"
49 #include "util/detect_os.h"
50 #include "util/macros.h"
51
52 #if DETECT_OS_HAIKU
53 /* Haiku provides debug_printf in libroot with OS.h */
54 #include <OS.h>
55 #endif
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60
61
62 #define _util_printf_format(fmt, list) PRINTFLIKE(fmt, list)
63
64 void _debug_vprintf(const char *format, va_list ap);
65
66
67 static inline void
_debug_printf(const char * format,...)68 _debug_printf(const char *format, ...)
69 {
70 va_list ap;
71 va_start(ap, format);
72 _debug_vprintf(format, ap);
73 va_end(ap);
74 }
75
76
77 /**
78 * Print debug messages.
79 *
80 * The actual channel used to output debug message is platform specific. To
81 * avoid misformating or truncation, follow these rules of thumb:
82 * - output whole lines
83 * - avoid outputing large strings (512 bytes is the current maximum length
84 * that is guaranteed to be printed in all platforms)
85 */
86 #if !DETECT_OS_HAIKU
87 static inline void
88 debug_printf(const char *format, ...) _util_printf_format(1,2);
89
90 static inline void
debug_printf(const char * format,...)91 debug_printf(const char *format, ...)
92 {
93 #ifdef DEBUG
94 va_list ap;
95 va_start(ap, format);
96 _debug_vprintf(format, ap);
97 va_end(ap);
98 #else
99 (void) format; /* silence warning */
100 #endif
101 }
102 #endif
103
104
105 /*
106 * ... isn't portable so we need to pass arguments in parentheses.
107 *
108 * usage:
109 * debug_printf_once(("answer: %i\n", 42));
110 */
111 #define debug_printf_once(args) \
112 do { \
113 static bool 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 */
133 void debug_print_blob( const char *name, const void *blob, unsigned size );
134 #else
135 #define debug_print_blob(_name, _blob, _size) ((void)0)
136 #endif
137
138
139 /**
140 * Disable interactive error message boxes.
141 *
142 * Should be called as soon as possible for effectiveness.
143 */
144 void
145 debug_disable_error_message_boxes(void);
146
147
148 /**
149 * Hard-coded breakpoint.
150 */
151 #ifdef DEBUG
152 #define debug_break() os_break()
153 #else /* !DEBUG */
154 #define debug_break() ((void)0)
155 #endif /* !DEBUG */
156
157
158 long
159 debug_get_num_option(const char *name, long dfault);
160
161 void
162 debug_get_version_option(const char *name, unsigned *major, unsigned *minor);
163
164 #ifdef _MSC_VER
165 __declspec(noreturn)
166 #endif
167 void _debug_assert_fail(const char *expr,
168 const char *file,
169 unsigned line,
170 const char *function)
171 #if defined(__GNUC__) && !defined(DEBUG)
172 __attribute__((noreturn))
173 #endif
174 ;
175
176
177 /**
178 * Assert macro
179 *
180 * Do not expect that the assert call terminates -- errors must be handled
181 * regardless of assert behavior.
182 *
183 * For non debug builds the assert macro will expand to a no-op, so do not
184 * call functions with side effects in the assert expression.
185 */
186 #ifndef NDEBUG
187 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
188 #else
189 #define debug_assert(expr) (void)(0 && (expr))
190 #endif
191
192
193 /** Override standard assert macro */
194 #ifdef assert
195 #undef assert
196 #endif
197 #define assert(expr) debug_assert(expr)
198
199
200 /**
201 * Output the current function name.
202 */
203 #ifdef DEBUG
204 #define debug_checkpoint() \
205 _debug_printf("%s\n", __FUNCTION__)
206 #else
207 #define debug_checkpoint() \
208 ((void)0)
209 #endif
210
211
212 /**
213 * Output the full source code position.
214 */
215 #ifdef DEBUG
216 #define debug_checkpoint_full() \
217 _debug_printf("%s:%u:%s\n", __FILE__, __LINE__, __FUNCTION__)
218 #else
219 #define debug_checkpoint_full() \
220 ((void)0)
221 #endif
222
223
224 /**
225 * Output a warning message. Muted on release version.
226 */
227 #ifdef DEBUG
228 #define debug_warning(__msg) \
229 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
230 #else
231 #define debug_warning(__msg) \
232 ((void)0)
233 #endif
234
235
236 /**
237 * Emit a warning message, but only once.
238 */
239 #ifdef DEBUG
240 #define debug_warn_once(__msg) \
241 do { \
242 static bool warned = false; \
243 if (!warned) { \
244 _debug_printf("%s:%u:%s: one time warning: %s\n", \
245 __FILE__, __LINE__, __FUNCTION__, __msg); \
246 warned = true; \
247 } \
248 } while (0)
249 #else
250 #define debug_warn_once(__msg) \
251 ((void)0)
252 #endif
253
254
255 /**
256 * Output an error message. Not muted on release version.
257 */
258 #ifdef DEBUG
259 #define debug_error(__msg) \
260 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
261 #else
262 #define debug_error(__msg) \
263 _debug_printf("error: %s\n", __msg)
264 #endif
265
266 /**
267 * Output a debug log message to the debug info callback.
268 * (virglrenderer) Removed.
269 */
270
271
272 /**
273 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
274 */
275 struct debug_named_value
276 {
277 const char *name;
278 uint64_t value;
279 const char *desc;
280 };
281
282
283 /**
284 * Some C pre-processor magic to simplify creating named values.
285 *
286 * Example:
287 * @code
288 * static const debug_named_value my_names[] = {
289 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
290 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
291 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
292 * DEBUG_NAMED_VALUE_END
293 * };
294 *
295 * ...
296 * debug_printf("%s = %s\n",
297 * name,
298 * debug_dump_enum(my_names, my_value));
299 * ...
300 * @endcode
301 */
302 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol, NULL}
303 #define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
304 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
305
306
307 /**
308 * Convert a enum value to a string.
309 */
310 const char *
311 debug_dump_enum(const struct debug_named_value *names,
312 unsigned long value);
313
314 const char *
315 debug_dump_enum_noprefix(const struct debug_named_value *names,
316 const char *prefix,
317 unsigned long value);
318
319
320 /**
321 * Convert binary flags value to a string.
322 */
323 const char *
324 debug_dump_flags(const struct debug_named_value *names,
325 unsigned long value);
326
327
328 /**
329 * Function enter exit loggers
330 */
331 #ifdef DEBUG
332 int debug_funclog_enter(const char* f, const int line, const char* file);
333 void debug_funclog_exit(const char* f, const int line, const char* file);
334 void debug_funclog_enter_exit(const char* f, const int line, const char* file);
335
336 #define DEBUG_FUNCLOG_ENTER() \
337 int __debug_decleration_work_around = \
338 debug_funclog_enter(__FUNCTION__, __LINE__, __FILE__)
339 #define DEBUG_FUNCLOG_EXIT() \
340 do { \
341 (void)__debug_decleration_work_around; \
342 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
343 return; \
344 } while(0)
345 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
346 do { \
347 (void)__debug_decleration_work_around; \
348 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
349 return ret; \
350 } while(0)
351 #define DEBUG_FUNCLOG_ENTER_EXIT() \
352 debug_funclog_enter_exit(__FUNCTION__, __LINE__, __FILE__)
353
354 #else
355 #define DEBUG_FUNCLOG_ENTER() \
356 int __debug_decleration_work_around
357 #define DEBUG_FUNCLOG_EXIT() \
358 do { (void)__debug_decleration_work_around; return; } while(0)
359 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
360 do { (void)__debug_decleration_work_around; return ret; } while(0)
361 #define DEBUG_FUNCLOG_ENTER_EXIT()
362 #endif
363
364
365 /**
366 * Get option.
367 *
368 * It is an alias for getenv on Linux.
369 *
370 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
371 * endings with one option per line as
372 *
373 * NAME=value
374 *
375 * This file must be terminated with an extra empty line.
376 */
377 const char *
378 debug_get_option(const char *name, const char *dfault);
379
380 bool
381 debug_get_bool_option(const char *name, bool dfault);
382
383 long
384 debug_get_num_option(const char *name, long dfault);
385
386 uint64_t
387 debug_get_flags_option(const char *name,
388 const struct debug_named_value *flags,
389 uint64_t dfault);
390
391 #define DEBUG_GET_ONCE_OPTION(suffix, name, dfault) \
392 static const char * \
393 debug_get_option_ ## suffix (void) \
394 { \
395 static bool first = true; \
396 static const char * value; \
397 if (first) { \
398 first = false; \
399 value = debug_get_option(name, dfault); \
400 } \
401 return value; \
402 }
403
404 static inline bool
__check_suid(void)405 __check_suid(void)
406 {
407 #if !defined(_WIN32)
408 if (geteuid() != getuid())
409 return true;
410 #endif
411 return false;
412 }
413
414 /**
415 * Define a getter for a debug option which specifies a 'FILE *'
416 * to open, with additional checks for suid executables. Note
417 * that if the return is not NULL, the caller owns the 'FILE *'
418 * reference.
419 */
420 #define DEBUG_GET_ONCE_FILE_OPTION(suffix, name, dfault, mode) \
421 static FILE * \
422 debug_get_option_ ## suffix (void) \
423 { \
424 static bool first = true; \
425 static const char * value; \
426 if (__check_suid()) \
427 return NULL; \
428 if (first) { \
429 first = false; \
430 value = debug_get_option(name, dfault); \
431 } \
432 if (!value) \
433 return NULL; \
434 return fopen(value, mode); \
435 }
436
437 #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \
438 static bool \
439 debug_get_option_ ## sufix (void) \
440 { \
441 static bool first = true; \
442 static bool value; \
443 if (first) { \
444 first = false; \
445 value = debug_get_bool_option(name, dfault); \
446 } \
447 return value; \
448 }
449
450 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
451 static long \
452 debug_get_option_ ## sufix (void) \
453 { \
454 static bool first = true; \
455 static long value; \
456 if (first) { \
457 first = false; \
458 value = debug_get_num_option(name, dfault); \
459 } \
460 return value; \
461 }
462
463 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
464 static unsigned long \
465 debug_get_option_ ## sufix (void) \
466 { \
467 static bool first = true; \
468 static unsigned long value; \
469 if (first) { \
470 first = false; \
471 value = debug_get_flags_option(name, flags, dfault); \
472 } \
473 return value; \
474 }
475
476
477 #ifdef __cplusplus
478 }
479 #endif
480
481 #endif /* U_DEBUG_H_ */
482