auxiliary: add reference count debugging code
[mesa.git] / src / gallium / auxiliary / util / u_debug_refcnt.c
1 #if defined(DEBUG) && (!defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_WINDOWS_USER))
2
3 /* see http://www.mozilla.org/performance/refcnt-balancer.html for what do with the output
4 * on Linux, use tools/addr2line.sh to postprocess it before anything else
5 **/
6 #include <util/u_debug.h>
7 #include <util/u_debug_refcnt.h>
8 #include <util/u_debug_stack.h>
9 #include <util/u_debug_symbol.h>
10 #include <util/u_string.h>
11 #include <util/u_hash_table.h>
12 #include <os/os_thread.h>
13 #include <os/os_stream.h>
14
15 int debug_refcnt_state;
16
17 struct os_stream* stream;
18
19 /* TODO: maybe move this serial machinery to a stand-alone module and expose it? */
20 static pipe_mutex serials_mutex;
21 static struct util_hash_table* serials_hash;
22 static unsigned serials_last;
23
24 static unsigned hash_ptr(void* p)
25 {
26 return (unsigned)(uintptr_t)p;
27 }
28
29 static int compare_ptr(void* a, void* b)
30 {
31 if(a == b)
32 return 0;
33 else if(a < b)
34 return -1;
35 else
36 return 1;
37 }
38
39 static boolean debug_serial(void* p, unsigned* pserial)
40 {
41 unsigned serial;
42 boolean found = TRUE;
43 pipe_mutex_lock(serials_mutex);
44 if(!serials_hash)
45 serials_hash = util_hash_table_create(hash_ptr, compare_ptr);
46 serial = (unsigned)(uintptr_t)util_hash_table_get(serials_hash, p);
47 if(!serial)
48 {
49 /* time to stop logging... (you'll have a 100 GB logfile at least at this point)
50 * TODO: avoid this
51 */
52 serial = ++serials_last;
53 if(!serial)
54 {
55 debug_error("More than 2^32 objects detected, aborting.\n");
56 os_abort();
57 }
58
59 util_hash_table_set(serials_hash, p, (void*)(uintptr_t)serial);
60 found = FALSE;
61 }
62 pipe_mutex_unlock(serials_mutex);
63 *pserial = serial;
64 return found;
65 }
66
67 static void debug_serial_delete(void* p)
68 {
69 pipe_mutex_lock(serials_mutex);
70 util_hash_table_remove(serials_hash, p);
71 pipe_mutex_unlock(serials_mutex);
72 }
73
74 #define STACK_LEN 64
75
76 static void dump_stack(const char* symbols[STACK_LEN])
77 {
78 unsigned i;
79 for(i = 0; i < STACK_LEN; ++i)
80 {
81 if(symbols[i])
82 os_stream_printf(stream, "%s\n", symbols[i]);
83 }
84 os_stream_write(stream, "\n", 1);
85 }
86
87 void debug_reference_slowpath(const struct pipe_reference* p, void* pget_desc, int change)
88 {
89 if(debug_refcnt_state < 0)
90 return;
91
92 if(!debug_refcnt_state)
93 {
94 const char* filename = debug_get_option("GALLIUM_REFCNT_LOG", NULL);
95 if(filename && filename[0])
96 stream = os_file_stream_create(filename);
97
98 if(stream)
99 debug_refcnt_state = 1;
100 else
101 debug_refcnt_state = -1;
102 }
103
104 if(debug_refcnt_state > 0)
105 {
106 struct debug_stack_frame frames[STACK_LEN];
107 const char* symbols[STACK_LEN];
108 char buf[1024];
109
110 void (*get_desc)(char*, const struct pipe_reference*) = pget_desc;
111 unsigned i;
112 unsigned refcnt = p->count;
113 unsigned serial;
114 boolean existing = debug_serial((void*)p, &serial);
115
116 debug_backtrace_capture(frames, 1, STACK_LEN);
117 for(i = 0; i < STACK_LEN; ++i)
118 {
119 if(frames[i].function)
120 symbols[i] = debug_symbol_name_cached(frames[i].function);
121 else
122 symbols[i] = 0;
123 }
124
125 get_desc(buf, p);
126
127 if(!existing)
128 {
129 os_stream_printf(stream, "<%s> %p %u Create\n", buf, p, serial);
130 dump_stack(symbols);
131
132 /* this is there to provide a gradual change even if we don't see the initialization */
133 for(i = 1; i <= refcnt - change; ++i)
134 {
135 os_stream_printf(stream, "<%s> %p %u AddRef %u\n", buf, p, serial, i);
136 dump_stack(symbols);
137 }
138 }
139
140 if(change)
141 {
142 os_stream_printf(stream, "<%s> %p %u %s %u\n", buf, p, serial, change > 0 ? "AddRef" : "Release", refcnt);
143 dump_stack(symbols);
144 }
145
146 if(!refcnt)
147 {
148 debug_serial_delete((void*)p);
149 os_stream_printf(stream, "<%s> %p %u Destroy\n", buf, p, serial);
150 dump_stack(symbols);
151 }
152
153 os_stream_flush(stream);
154 }
155 }
156 #endif