1 /**************************************************************************
3 * Copyright 2010 Luca Barbieri
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:
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.
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.
25 **************************************************************************/
30 * If the GALLIUM_REFCNT_LOG env var is defined as a filename, gallium
31 * reference counting will be logged to the file.
33 * See http://www-archive.mozilla.org/performance/refcnt-balancer.html
34 * for what to do with the output on Linux, use tools/addr2line.sh to
35 * postprocess it before anything else.
40 #include "util/u_debug.h"
41 #include "util/u_debug_refcnt.h"
42 #include "util/u_debug_stack.h"
43 #include "util/u_debug_symbol.h"
44 #include "util/u_string.h"
45 #include "util/u_hash_table.h"
46 #include "os/os_thread.h"
48 int debug_refcnt_state
;
52 /* TODO: maybe move this serial machinery to a stand-alone module and
55 static mtx_t serials_mutex
= _MTX_INITIALIZER_NP
;
57 static struct util_hash_table
*serials_hash
;
58 static unsigned serials_last
;
64 return (unsigned) (uintptr_t) p
;
69 compare_ptr(void *a
, void *b
)
81 * Return a small integer serial number for the given pointer.
84 debug_serial(void *p
, unsigned *pserial
)
88 #ifdef PIPE_SUBSYSTEM_WINDOWS_USER
89 static boolean first
= TRUE
;
92 (void) mtx_init(&serials_mutex
, mtx_plain
);
97 mtx_lock(&serials_mutex
);
99 serials_hash
= util_hash_table_create(hash_ptr
, compare_ptr
);
101 serial
= (unsigned) (uintptr_t) util_hash_table_get(serials_hash
, p
);
103 /* time to stop logging... (you'll have a 100 GB logfile at least at
104 * this point) TODO: avoid this
106 serial
= ++serials_last
;
108 debug_error("More than 2^32 objects detected, aborting.\n");
112 util_hash_table_set(serials_hash
, p
, (void *) (uintptr_t) serial
);
115 mtx_unlock(&serials_mutex
);
124 * Free the serial number for the given pointer.
127 debug_serial_delete(void *p
)
129 mtx_lock(&serials_mutex
);
130 util_hash_table_remove(serials_hash
, p
);
131 mtx_unlock(&serials_mutex
);
138 * Log a reference count change to the log file (if enabled).
139 * This is called via the pipe_reference() and debug_reference() functions,
140 * basically whenever a reference count is initialized or changed.
142 * \param p the refcount being changed (the value is not changed here)
143 * \param get_desc a function which will be called to print an object's
144 * name/pointer into a string buffer during logging
145 * \param change the reference count change which must be +/-1 or 0 when
146 * creating the object and initializing the refcount.
149 debug_reference_slowpath(const struct pipe_reference
*p
,
150 debug_reference_descriptor get_desc
, int change
)
152 assert(change
>= -1);
155 if (debug_refcnt_state
< 0)
158 if (!debug_refcnt_state
) {
159 const char *filename
= debug_get_option("GALLIUM_REFCNT_LOG", NULL
);
160 if (filename
&& filename
[0])
161 stream
= fopen(filename
, "wt");
164 debug_refcnt_state
= 1;
166 debug_refcnt_state
= -1;
169 if (debug_refcnt_state
> 0) {
170 struct debug_stack_frame frames
[STACK_LEN
];
173 unsigned refcnt
= p
->count
;
175 boolean existing
= debug_serial((void *) p
, &serial
);
177 debug_backtrace_capture(frames
, 1, STACK_LEN
);
182 fprintf(stream
, "<%s> %p %u Create\n", buf
, (void *) p
, serial
);
183 debug_backtrace_print(stream
, frames
, STACK_LEN
);
185 /* this is here to provide a gradual change even if we don't see
188 for (i
= 1; i
<= refcnt
- change
; ++i
) {
189 fprintf(stream
, "<%s> %p %u AddRef %u\n", buf
, (void *) p
,
191 debug_backtrace_print(stream
, frames
, STACK_LEN
);
196 fprintf(stream
, "<%s> %p %u %s %u\n", buf
, (void *) p
, serial
,
197 change
> 0 ? "AddRef" : "Release", refcnt
);
198 debug_backtrace_print(stream
, frames
, STACK_LEN
);
202 debug_serial_delete((void *) p
);
203 fprintf(stream
, "<%s> %p %u Destroy\n", buf
, (void *) p
, serial
);
204 debug_backtrace_print(stream
, frames
, STACK_LEN
);