1 #if defined(DEBUG) && (!defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_WINDOWS_USER))
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
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>
15 int debug_refcnt_state
;
17 struct os_stream
* stream
;
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
;
24 static unsigned hash_ptr(void* p
)
26 return (unsigned)(uintptr_t)p
;
29 static int compare_ptr(void* a
, void* b
)
39 static boolean
debug_serial(void* p
, unsigned* pserial
)
43 pipe_mutex_lock(serials_mutex
);
45 serials_hash
= util_hash_table_create(hash_ptr
, compare_ptr
);
46 serial
= (unsigned)(uintptr_t)util_hash_table_get(serials_hash
, p
);
49 /* time to stop logging... (you'll have a 100 GB logfile at least at this point)
52 serial
= ++serials_last
;
55 debug_error("More than 2^32 objects detected, aborting.\n");
59 util_hash_table_set(serials_hash
, p
, (void*)(uintptr_t)serial
);
62 pipe_mutex_unlock(serials_mutex
);
67 static void debug_serial_delete(void* p
)
69 pipe_mutex_lock(serials_mutex
);
70 util_hash_table_remove(serials_hash
, p
);
71 pipe_mutex_unlock(serials_mutex
);
76 static void dump_stack(const char* symbols
[STACK_LEN
])
79 for(i
= 0; i
< STACK_LEN
; ++i
)
82 os_stream_printf(stream
, "%s\n", symbols
[i
]);
84 os_stream_write(stream
, "\n", 1);
87 void debug_reference_slowpath(const struct pipe_reference
* p
, void* pget_desc
, int change
)
89 if(debug_refcnt_state
< 0)
92 if(!debug_refcnt_state
)
94 const char* filename
= debug_get_option("GALLIUM_REFCNT_LOG", NULL
);
95 if(filename
&& filename
[0])
96 stream
= os_file_stream_create(filename
);
99 debug_refcnt_state
= 1;
101 debug_refcnt_state
= -1;
104 if(debug_refcnt_state
> 0)
106 struct debug_stack_frame frames
[STACK_LEN
];
107 const char* symbols
[STACK_LEN
];
110 void (*get_desc
)(char*, const struct pipe_reference
*) = pget_desc
;
112 unsigned refcnt
= p
->count
;
114 boolean existing
= debug_serial((void*)p
, &serial
);
116 debug_backtrace_capture(frames
, 1, STACK_LEN
);
117 for(i
= 0; i
< STACK_LEN
; ++i
)
119 if(frames
[i
].function
)
120 symbols
[i
] = debug_symbol_name_cached(frames
[i
].function
);
129 os_stream_printf(stream
, "<%s> %p %u Create\n", buf
, p
, serial
);
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
)
135 os_stream_printf(stream
, "<%s> %p %u AddRef %u\n", buf
, p
, serial
, i
);
142 os_stream_printf(stream
, "<%s> %p %u %s %u\n", buf
, p
, serial
, change
> 0 ? "AddRef" : "Release", refcnt
);
148 debug_serial_delete((void*)p
);
149 os_stream_printf(stream
, "<%s> %p %u Destroy\n", buf
, p
, serial
);
153 os_stream_flush(stream
);