Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / gallium / auxiliary / util / u_debug_refcnt.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #if defined(DEBUG) && (!defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_WINDOWS_USER))
28
29 /* see http://www.mozilla.org/performance/refcnt-balancer.html for what do with the output
30 * on Linux, use tools/addr2line.sh to postprocess it before anything else
31 **/
32 #include <util/u_debug.h>
33 #include <util/u_debug_refcnt.h>
34 #include <util/u_debug_stack.h>
35 #include <util/u_debug_symbol.h>
36 #include <util/u_string.h>
37 #include <util/u_hash_table.h>
38 #include <os/os_thread.h>
39 #include <os/os_stream.h>
40
41 int debug_refcnt_state;
42
43 struct os_stream* stream;
44
45 /* TODO: maybe move this serial machinery to a stand-alone module and expose it? */
46 static pipe_mutex serials_mutex;
47 static struct util_hash_table* serials_hash;
48 static unsigned serials_last;
49
50 static unsigned hash_ptr(void* p)
51 {
52 return (unsigned)(uintptr_t)p;
53 }
54
55 static int compare_ptr(void* a, void* b)
56 {
57 if(a == b)
58 return 0;
59 else if(a < b)
60 return -1;
61 else
62 return 1;
63 }
64
65 static boolean debug_serial(void* p, unsigned* pserial)
66 {
67 unsigned serial;
68 boolean found = TRUE;
69 pipe_mutex_lock(serials_mutex);
70 if(!serials_hash)
71 serials_hash = util_hash_table_create(hash_ptr, compare_ptr);
72 serial = (unsigned)(uintptr_t)util_hash_table_get(serials_hash, p);
73 if(!serial)
74 {
75 /* time to stop logging... (you'll have a 100 GB logfile at least at this point)
76 * TODO: avoid this
77 */
78 serial = ++serials_last;
79 if(!serial)
80 {
81 debug_error("More than 2^32 objects detected, aborting.\n");
82 os_abort();
83 }
84
85 util_hash_table_set(serials_hash, p, (void*)(uintptr_t)serial);
86 found = FALSE;
87 }
88 pipe_mutex_unlock(serials_mutex);
89 *pserial = serial;
90 return found;
91 }
92
93 static void debug_serial_delete(void* p)
94 {
95 pipe_mutex_lock(serials_mutex);
96 util_hash_table_remove(serials_hash, p);
97 pipe_mutex_unlock(serials_mutex);
98 }
99
100 #define STACK_LEN 64
101
102 static void dump_stack(const char* symbols[STACK_LEN])
103 {
104 unsigned i;
105 for(i = 0; i < STACK_LEN; ++i)
106 {
107 if(symbols[i])
108 os_stream_printf(stream, "%s\n", symbols[i]);
109 }
110 os_stream_write(stream, "\n", 1);
111 }
112
113 void debug_reference_slowpath(const struct pipe_reference* p, debug_reference_descriptor get_desc, int change)
114 {
115 if(debug_refcnt_state < 0)
116 return;
117
118 if(!debug_refcnt_state)
119 {
120 const char* filename = debug_get_option("GALLIUM_REFCNT_LOG", NULL);
121 if(filename && filename[0])
122 stream = os_file_stream_create(filename);
123
124 if(stream)
125 debug_refcnt_state = 1;
126 else
127 debug_refcnt_state = -1;
128 }
129
130 if(debug_refcnt_state > 0)
131 {
132 struct debug_stack_frame frames[STACK_LEN];
133 const char* symbols[STACK_LEN];
134 char buf[1024];
135
136 unsigned i;
137 unsigned refcnt = p->count;
138 unsigned serial;
139 boolean existing = debug_serial((void*)p, &serial);
140
141 debug_backtrace_capture(frames, 1, STACK_LEN);
142 for(i = 0; i < STACK_LEN; ++i)
143 {
144 if(frames[i].function)
145 symbols[i] = debug_symbol_name_cached(frames[i].function);
146 else
147 symbols[i] = 0;
148 }
149
150 get_desc(buf, p);
151
152 if(!existing)
153 {
154 os_stream_printf(stream, "<%s> %p %u Create\n", buf, p, serial);
155 dump_stack(symbols);
156
157 /* this is there to provide a gradual change even if we don't see the initialization */
158 for(i = 1; i <= refcnt - change; ++i)
159 {
160 os_stream_printf(stream, "<%s> %p %u AddRef %u\n", buf, p, serial, i);
161 dump_stack(symbols);
162 }
163 }
164
165 if(change)
166 {
167 os_stream_printf(stream, "<%s> %p %u %s %u\n", buf, p, serial, change > 0 ? "AddRef" : "Release", refcnt);
168 dump_stack(symbols);
169 }
170
171 if(!refcnt)
172 {
173 debug_serial_delete((void*)p);
174 os_stream_printf(stream, "<%s> %p %u Destroy\n", buf, p, serial);
175 dump_stack(symbols);
176 }
177
178 os_stream_flush(stream);
179 }
180 }
181 #endif