gallium/util: move memory debug declarations into u_debug_gallium
[mesa.git] / src / gallium / auxiliary / util / u_debug.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright (c) 2008 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29
30 #include "pipe/p_config.h"
31
32 #include "pipe/p_compiler.h"
33 #include "util/u_debug.h"
34 #include "pipe/p_format.h"
35 #include "pipe/p_state.h"
36 #include "util/u_inlines.h"
37 #include "util/u_string.h"
38 #include "util/u_math.h"
39 #include <inttypes.h>
40
41 #include <stdio.h>
42 #include <limits.h> /* CHAR_BIT */
43 #include <ctype.h> /* isalnum */
44
45 #ifdef _WIN32
46 #include <windows.h>
47 #include <stdlib.h>
48 #endif
49
50
51 void
52 _debug_vprintf(const char *format, va_list ap)
53 {
54 static char buf[4096] = {'\0'};
55 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
56 /* We buffer until we find a newline. */
57 size_t len = strlen(buf);
58 int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
59 if (ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
60 os_log_message(buf);
61 buf[0] = '\0';
62 }
63 #else
64 util_vsnprintf(buf, sizeof(buf), format, ap);
65 os_log_message(buf);
66 #endif
67 }
68
69
70 void
71 _pipe_debug_message(struct pipe_debug_callback *cb,
72 unsigned *id,
73 enum pipe_debug_type type,
74 const char *fmt, ...)
75 {
76 va_list args;
77 va_start(args, fmt);
78 if (cb && cb->debug_message)
79 cb->debug_message(cb->data, id, type, fmt, args);
80 va_end(args);
81 }
82
83
84 void
85 debug_disable_error_message_boxes(void)
86 {
87 #ifdef _WIN32
88 /* When Windows' error message boxes are disabled for this process (as is
89 * typically the case when running tests in an automated fashion) we disable
90 * CRT message boxes too.
91 */
92 UINT uMode = SetErrorMode(0);
93 SetErrorMode(uMode);
94 if (uMode & SEM_FAILCRITICALERRORS) {
95 /* Disable assertion failure message box.
96 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
97 */
98 _set_error_mode(_OUT_TO_STDERR);
99 #ifdef _MSC_VER
100 /* Disable abort message box.
101 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
102 */
103 _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
104 #endif
105 }
106 #endif /* _WIN32 */
107 }
108
109
110 #ifdef DEBUG
111 void
112 debug_print_blob(const char *name, const void *blob, unsigned size)
113 {
114 const unsigned *ublob = (const unsigned *)blob;
115 unsigned i;
116
117 debug_printf("%s (%d dwords%s)\n", name, size/4,
118 size%4 ? "... plus a few bytes" : "");
119
120 for (i = 0; i < size/4; i++) {
121 debug_printf("%d:\t%08x\n", i, ublob[i]);
122 }
123 }
124 #endif
125
126
127 static boolean
128 debug_get_option_should_print(void)
129 {
130 static boolean first = TRUE;
131 static boolean value = FALSE;
132
133 if (!first)
134 return value;
135
136 /* Oh hey this will call into this function,
137 * but its cool since we set first to false
138 */
139 first = FALSE;
140 value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE);
141 /* XXX should we print this option? Currently it wont */
142 return value;
143 }
144
145
146 const char *
147 debug_get_option(const char *name, const char *dfault)
148 {
149 const char *result;
150
151 result = os_get_option(name);
152 if (!result)
153 result = dfault;
154
155 if (debug_get_option_should_print())
156 debug_printf("%s: %s = %s\n", __FUNCTION__, name,
157 result ? result : "(null)");
158
159 return result;
160 }
161
162
163 boolean
164 debug_get_bool_option(const char *name, boolean dfault)
165 {
166 const char *str = os_get_option(name);
167 boolean result;
168
169 if (str == NULL)
170 result = dfault;
171 else if (!util_strcmp(str, "n"))
172 result = FALSE;
173 else if (!util_strcmp(str, "no"))
174 result = FALSE;
175 else if (!util_strcmp(str, "0"))
176 result = FALSE;
177 else if (!util_strcmp(str, "f"))
178 result = FALSE;
179 else if (!util_strcmp(str, "F"))
180 result = FALSE;
181 else if (!util_strcmp(str, "false"))
182 result = FALSE;
183 else if (!util_strcmp(str, "FALSE"))
184 result = FALSE;
185 else
186 result = TRUE;
187
188 if (debug_get_option_should_print())
189 debug_printf("%s: %s = %s\n", __FUNCTION__, name,
190 result ? "TRUE" : "FALSE");
191
192 return result;
193 }
194
195
196 long
197 debug_get_num_option(const char *name, long dfault)
198 {
199 long result;
200 const char *str;
201
202 str = os_get_option(name);
203 if (!str) {
204 result = dfault;
205 } else {
206 char *endptr;
207
208 result = strtol(str, &endptr, 0);
209 if (str == endptr) {
210 /* Restore the default value when no digits were found. */
211 result = dfault;
212 }
213 }
214
215 if (debug_get_option_should_print())
216 debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
217
218 return result;
219 }
220
221
222 static boolean
223 str_has_option(const char *str, const char *name)
224 {
225 /* Empty string. */
226 if (!*str) {
227 return FALSE;
228 }
229
230 /* OPTION=all */
231 if (!util_strcmp(str, "all")) {
232 return TRUE;
233 }
234
235 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
236 {
237 const char *start = str;
238 unsigned name_len = strlen(name);
239
240 /* 'start' is the beginning of the currently-parsed word,
241 * we increment 'str' each iteration.
242 * if we find either the end of string or a non-alphanumeric character,
243 * we compare 'start' up to 'str-1' with 'name'. */
244
245 while (1) {
246 if (!*str || !(isalnum(*str) || *str == '_')) {
247 if (str-start == name_len &&
248 !memcmp(start, name, name_len)) {
249 return TRUE;
250 }
251
252 if (!*str) {
253 return FALSE;
254 }
255
256 start = str+1;
257 }
258
259 str++;
260 }
261 }
262
263 return FALSE;
264 }
265
266
267 uint64_t
268 debug_get_flags_option(const char *name,
269 const struct debug_named_value *flags,
270 uint64_t dfault)
271 {
272 uint64_t result;
273 const char *str;
274 const struct debug_named_value *orig = flags;
275 unsigned namealign = 0;
276
277 str = os_get_option(name);
278 if (!str)
279 result = dfault;
280 else if (!util_strcmp(str, "help")) {
281 result = dfault;
282 _debug_printf("%s: help for %s:\n", __FUNCTION__, name);
283 for (; flags->name; ++flags)
284 namealign = MAX2(namealign, strlen(flags->name));
285 for (flags = orig; flags->name; ++flags)
286 _debug_printf("| %*s [0x%0*"PRIx64"]%s%s\n", namealign, flags->name,
287 (int)sizeof(uint64_t)*CHAR_BIT/4, flags->value,
288 flags->desc ? " " : "", flags->desc ? flags->desc : "");
289 }
290 else {
291 result = 0;
292 while (flags->name) {
293 if (str_has_option(str, flags->name))
294 result |= flags->value;
295 ++flags;
296 }
297 }
298
299 if (debug_get_option_should_print()) {
300 if (str) {
301 debug_printf("%s: %s = 0x%"PRIx64" (%s)\n",
302 __FUNCTION__, name, result, str);
303 } else {
304 debug_printf("%s: %s = 0x%"PRIx64"\n", __FUNCTION__, name, result);
305 }
306 }
307
308 return result;
309 }
310
311
312 void
313 _debug_assert_fail(const char *expr, const char *file, unsigned line,
314 const char *function)
315 {
316 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
317 file, line, function, expr);
318 os_abort();
319 }
320
321
322 const char *
323 debug_dump_enum(const struct debug_named_value *names,
324 unsigned long value)
325 {
326 static char rest[64];
327
328 while (names->name) {
329 if (names->value == value)
330 return names->name;
331 ++names;
332 }
333
334 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
335 return rest;
336 }
337
338
339 const char *
340 debug_dump_enum_noprefix(const struct debug_named_value *names,
341 const char *prefix,
342 unsigned long value)
343 {
344 static char rest[64];
345
346 while (names->name) {
347 if (names->value == value) {
348 const char *name = names->name;
349 while (*name == *prefix) {
350 name++;
351 prefix++;
352 }
353 return name;
354 }
355 ++names;
356 }
357
358 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
359 return rest;
360 }
361
362
363 const char *
364 debug_dump_flags(const struct debug_named_value *names, unsigned long value)
365 {
366 static char output[4096];
367 static char rest[256];
368 int first = 1;
369
370 output[0] = '\0';
371
372 while (names->name) {
373 if ((names->value & value) == names->value) {
374 if (!first)
375 util_strncat(output, "|", sizeof(output) - strlen(output) - 1);
376 else
377 first = 0;
378 util_strncat(output, names->name, sizeof(output) - strlen(output) - 1);
379 output[sizeof(output) - 1] = '\0';
380 value &= ~names->value;
381 }
382 ++names;
383 }
384
385 if (value) {
386 if (!first)
387 util_strncat(output, "|", sizeof(output) - strlen(output) - 1);
388 else
389 first = 0;
390
391 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
392 util_strncat(output, rest, sizeof(output) - strlen(output) - 1);
393 output[sizeof(output) - 1] = '\0';
394 }
395
396 if (first)
397 return "0";
398
399 return output;
400 }
401
402
403
404 #ifdef DEBUG
405 int fl_indent = 0;
406 const char* fl_function[1024];
407
408 int
409 debug_funclog_enter(const char* f, UNUSED const int line,
410 UNUSED const char* file)
411 {
412 int i;
413
414 for (i = 0; i < fl_indent; i++)
415 debug_printf(" ");
416 debug_printf("%s\n", f);
417
418 assert(fl_indent < 1023);
419 fl_function[fl_indent++] = f;
420
421 return 0;
422 }
423
424 void
425 debug_funclog_exit(const char* f, UNUSED const int line,
426 UNUSED const char* file)
427 {
428 --fl_indent;
429 assert(fl_indent >= 0);
430 assert(fl_function[fl_indent] == f);
431 }
432
433 void
434 debug_funclog_enter_exit(const char* f, UNUSED const int line,
435 UNUSED const char* file)
436 {
437 int i;
438 for (i = 0; i < fl_indent; i++)
439 debug_printf(" ");
440 debug_printf("%s\n", f);
441 }
442 #endif