gallium: Change assert behavior on runtime (Mark Mueller).
[mesa.git] / src / gallium / auxiliary / util / p_debug.c
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 #include <stdarg.h>
30
31 #ifdef WIN32
32 #include <windows.h>
33 #include <winddi.h>
34 #else
35 #include <stdio.h>
36 #include <stdlib.h>
37 #endif
38
39 #include "pipe/p_compiler.h"
40 #include "pipe/p_util.h"
41 #include "pipe/p_debug.h"
42
43
44 #ifdef WIN32
45 static INLINE void
46 rpl_EngDebugPrint(const char *format, ...)
47 {
48 va_list ap;
49 va_start(ap, format);
50 EngDebugPrint("", (PCHAR)format, ap);
51 va_end(ap);
52 }
53
54 int rpl_vsnprintf(char *, size_t, const char *, va_list);
55 #endif
56
57
58 void debug_vprintf(const char *format, va_list ap)
59 {
60 #ifdef WIN32
61 #ifndef WINCE
62 /* EngDebugPrint does not handle float point arguments, so we need to use
63 * our own vsnprintf implementation */
64 char buf[512 + 1];
65 rpl_vsnprintf(buf, sizeof(buf), format, ap);
66 rpl_EngDebugPrint("%s", buf);
67 #else
68 /* TODO: Implement debug print for WINCE */
69 #endif
70 #else
71 vfprintf(stderr, format, ap);
72 #endif
73 }
74
75
76 void debug_printf(const char *format, ...)
77 {
78 va_list ap;
79 va_start(ap, format);
80 debug_vprintf(format, ap);
81 va_end(ap);
82 }
83
84
85 /* TODO: implement a debug_abort that calls EngBugCheckEx on WIN32 */
86
87
88 static INLINE void debug_break(void)
89 {
90 #if (defined(__i386__) || defined(__386__)) && defined(__GNUC__)
91 __asm("int3");
92 #elif (defined(__i386__) || defined(__386__)) && defined(__MSC__)
93 _asm {int 3};
94 #elif defined(WIN32) && !defined(WINCE)
95 EngDebugBreak();
96 #else
97 abort();
98 #endif
99 }
100
101 #if defined(WIN32)
102 ULONG_PTR debug_config_file = 0;
103 void *mapped_config_file = 0;
104
105 enum {
106 eAssertAbortEn = 0x1,
107 };
108
109 /* Check for aborts enabled. */
110 static unsigned abort_en()
111 {
112 if (!mapped_config_file)
113 {
114 /* Open an 8 byte file for configuration data. */
115 mapped_config_file = EngMapFile(L"\\??\\c:\\gaDebug.cfg", 8, &debug_config_file);
116 }
117 /* An value of "0" (ascii) in the configuration file will clear the first 8 bits in the test byte. */
118 /* An value of "1" (ascii) in the configuration file will set the first bit in the test byte. */
119 /* An value of "2" (ascii) in the configuration file will set the second bit in the test byte. */
120 return ((((char *)mapped_config_file)[0]) - 0x30) & eAssertAbortEn;
121 }
122 #else /* WIN32 */
123 static unsigned abort_en()
124 {
125 return !GETENV("GALLIUM_ABORT_ON_ASSERT");
126 }
127 #endif
128
129 void debug_assert_fail(const char *expr, const char *file, unsigned line)
130 {
131 debug_printf("%s:%i: Assertion `%s' failed.\n", file, line, expr);
132 if (abort_en())
133 {
134 debug_break();
135 } else
136 {
137 debug_printf("continuing...\n");
138 }
139 }
140
141
142 #define DEBUG_MASK_TABLE_SIZE 256
143
144
145 /**
146 * Mask hash table.
147 *
148 * For now we just take the lower bits of the key, and do no attempt to solve
149 * collisions. Use a proper hash table when we have dozens of drivers.
150 */
151 static uint32_t debug_mask_table[DEBUG_MASK_TABLE_SIZE];
152
153
154 void debug_mask_set(uint32_t uuid, uint32_t mask)
155 {
156 unsigned hash = uuid & (DEBUG_MASK_TABLE_SIZE - 1);
157 debug_mask_table[hash] = mask;
158 }
159
160
161 uint32_t debug_mask_get(uint32_t uuid)
162 {
163 unsigned hash = uuid & (DEBUG_MASK_TABLE_SIZE - 1);
164 return debug_mask_table[hash];
165 }
166
167
168 void debug_mask_vprintf(uint32_t uuid, uint32_t what, const char *format, va_list ap)
169 {
170 uint32_t mask = debug_mask_get(uuid);
171 if(mask & what)
172 debug_vprintf(format, ap);
173 }