1 /**************************************************************************
3 * Copyright 2012 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
30 * u_debug_flush.c Debug flush and map-related issues:
31 * - Flush while synchronously mapped.
32 * - Command stream reference while synchronously mapped.
33 * - Synchronous map while referenced on command stream.
35 * - Unmap while not mapped.
37 * @author Thomas Hellstrom <thellstrom@vmware.com>
41 #include "pipe/p_compiler.h"
42 #include "util/u_debug_stack.h"
43 #include "util/u_debug.h"
44 #include "util/u_memory.h"
45 #include "util/u_debug_flush.h"
46 #include "util/u_hash_table.h"
47 #include "util/u_double_list.h"
48 #include "util/u_inlines.h"
49 #include "util/u_string.h"
50 #include "os/os_thread.h"
53 struct debug_flush_buf
{
55 struct pipe_reference reference
; /* Must be the first member. */
58 boolean supports_unsync
;
60 /* Protected by mutex */
63 struct debug_stack_frame
*map_frame
;
66 struct debug_flush_item
{
67 struct debug_flush_buf
*fbuf
;
69 struct debug_stack_frame
*ref_frame
;
72 struct debug_flush_ctx
{
73 /* Contexts are used by a single thread at a time */
75 boolean catch_map_of_referenced
;
76 struct util_hash_table
*ref_hash
;
77 struct list_head head
;
80 pipe_static_mutex(list_mutex
);
81 static struct list_head ctx_list
= {&ctx_list
, &ctx_list
};
83 static struct debug_stack_frame
*
84 debug_flush_capture_frame(int start
, int depth
)
86 struct debug_stack_frame
*frames
;
88 frames
= CALLOC(depth
, sizeof(*frames
));
92 debug_backtrace_capture(frames
, start
, depth
);
97 debug_flush_pointer_compare(void *key1
, void *key2
)
99 return (key1
== key2
) ? 0 : 1;
103 debug_flush_pointer_hash(void *key
)
105 return (unsigned) (unsigned long) key
;
108 struct debug_flush_buf
*
109 debug_flush_buf_create(boolean supports_unsync
, unsigned bt_depth
)
111 struct debug_flush_buf
*fbuf
= CALLOC_STRUCT(debug_flush_buf
);
116 fbuf
->supports_unsync
= supports_unsync
;
117 fbuf
->bt_depth
= bt_depth
;
118 pipe_reference_init(&fbuf
->reference
, 1);
119 pipe_mutex_init(fbuf
->mutex
);
123 debug_printf("Debug flush buffer creation failed.\n");
124 debug_printf("Debug flush checking for this buffer will be incomplete.\n");
129 debug_flush_buf_reference(struct debug_flush_buf
**dst
,
130 struct debug_flush_buf
*src
)
132 struct debug_flush_buf
*fbuf
= *dst
;
134 if (pipe_reference(&(*dst
)->reference
, &src
->reference
)) {
135 FREE(fbuf
->map_frame
);
144 debug_flush_item_destroy(struct debug_flush_item
*item
)
146 debug_flush_buf_reference(&item
->fbuf
, NULL
);
148 FREE(item
->ref_frame
);
153 struct debug_flush_ctx
*
154 debug_flush_ctx_create(boolean catch_reference_of_mapped
, unsigned bt_depth
)
156 struct debug_flush_ctx
*fctx
= CALLOC_STRUCT(debug_flush_ctx
);
161 fctx
->ref_hash
= util_hash_table_create(debug_flush_pointer_hash
,
162 debug_flush_pointer_compare
);
165 goto out_no_ref_hash
;
167 fctx
->bt_depth
= bt_depth
;
168 pipe_mutex_lock(list_mutex
);
169 list_addtail(&fctx
->head
, &ctx_list
);
170 pipe_mutex_unlock(list_mutex
);
177 debug_printf("Debug flush context creation failed.\n");
178 debug_printf("Debug flush checking for this context will be incomplete.\n");
183 debug_flush_alert(const char *s
, const char *op
,
184 unsigned start
, unsigned depth
,
187 const struct debug_stack_frame
*frame
)
190 frame
= debug_flush_capture_frame(start
, depth
);
193 debug_printf("%s ", s
);
195 debug_printf("%s backtrace follows:\n", op
);
196 debug_backtrace_dump(frame
, depth
);
198 debug_printf("No %s backtrace was captured.\n", op
);
201 debug_printf("**********************************\n");
203 debug_printf("*********END OF MESSAGE***********\n\n\n");
211 debug_flush_map(struct debug_flush_buf
*fbuf
, unsigned flags
)
213 boolean mapped_sync
= FALSE
;
218 pipe_mutex_lock(fbuf
->mutex
);
220 debug_flush_alert("Recursive map detected.", "Map",
221 2, fbuf
->bt_depth
, TRUE
, TRUE
, NULL
);
222 debug_flush_alert(NULL
, "Previous map", 0, fbuf
->bt_depth
, FALSE
,
223 FALSE
, fbuf
->map_frame
);
224 } else if (!(flags
& PIPE_TRANSFER_UNSYNCHRONIZED
) ||
225 !fbuf
->supports_unsync
) {
226 fbuf
->mapped_sync
= mapped_sync
= TRUE
;
228 fbuf
->map_frame
= debug_flush_capture_frame(1, fbuf
->bt_depth
);
230 pipe_mutex_unlock(fbuf
->mutex
);
233 struct debug_flush_ctx
*fctx
;
235 pipe_mutex_lock(list_mutex
);
236 LIST_FOR_EACH_ENTRY(fctx
, &ctx_list
, head
) {
237 struct debug_flush_item
*item
=
238 util_hash_table_get(fctx
->ref_hash
, fbuf
);
240 if (item
&& fctx
->catch_map_of_referenced
) {
241 debug_flush_alert("Already referenced map detected.",
242 "Map", 2, fbuf
->bt_depth
, TRUE
, TRUE
, NULL
);
243 debug_flush_alert(NULL
, "Reference", 0, item
->bt_depth
,
244 FALSE
, FALSE
, item
->ref_frame
);
247 pipe_mutex_unlock(list_mutex
);
252 debug_flush_unmap(struct debug_flush_buf
*fbuf
)
257 pipe_mutex_lock(fbuf
->mutex
);
259 debug_flush_alert("Unmap not previously mapped detected.", "Map",
260 2, fbuf
->bt_depth
, FALSE
, TRUE
, NULL
);
262 fbuf
->mapped_sync
= FALSE
;
263 fbuf
->mapped
= FALSE
;
264 FREE(fbuf
->map_frame
);
265 fbuf
->map_frame
= NULL
;
266 pipe_mutex_unlock(fbuf
->mutex
);
270 debug_flush_cb_reference(struct debug_flush_ctx
*fctx
,
271 struct debug_flush_buf
*fbuf
)
273 struct debug_flush_item
*item
;
278 item
= util_hash_table_get(fctx
->ref_hash
, fbuf
);
280 pipe_mutex_lock(fbuf
->mutex
);
281 if (fbuf
->mapped_sync
) {
282 debug_flush_alert("Reference of mapped buffer detected.", "Reference",
283 2, fctx
->bt_depth
, TRUE
, TRUE
, NULL
);
284 debug_flush_alert(NULL
, "Map", 0, fbuf
->bt_depth
, FALSE
,
285 FALSE
, fbuf
->map_frame
);
287 pipe_mutex_unlock(fbuf
->mutex
);
290 item
= CALLOC_STRUCT(debug_flush_item
);
292 debug_flush_buf_reference(&item
->fbuf
, fbuf
);
293 item
->bt_depth
= fctx
->bt_depth
;
294 item
->ref_frame
= debug_flush_capture_frame(2, item
->bt_depth
);
295 if (util_hash_table_set(fctx
->ref_hash
, fbuf
, item
) != PIPE_OK
) {
296 debug_flush_item_destroy(item
);
306 debug_printf("Debug flush command buffer reference creation failed.\n");
307 debug_printf("Debug flush checking will be incomplete "
308 "for this command batch.\n");
311 static enum pipe_error
312 debug_flush_might_flush_cb(void *key
, void *value
, void *data
)
314 struct debug_flush_item
*item
=
315 (struct debug_flush_item
*) value
;
316 struct debug_flush_buf
*fbuf
= item
->fbuf
;
317 const char *reason
= (const char *) data
;
320 util_snprintf(message
, sizeof(message
),
321 "%s referenced mapped buffer detected.", reason
);
323 pipe_mutex_lock(fbuf
->mutex
);
324 if (fbuf
->mapped_sync
) {
325 debug_flush_alert(message
, reason
, 3, item
->bt_depth
, TRUE
, TRUE
, NULL
);
326 debug_flush_alert(NULL
, "Map", 0, fbuf
->bt_depth
, TRUE
, FALSE
,
328 debug_flush_alert(NULL
, "First reference", 0, item
->bt_depth
, FALSE
,
329 FALSE
, item
->ref_frame
);
331 pipe_mutex_unlock(fbuf
->mutex
);
337 debug_flush_might_flush(struct debug_flush_ctx
*fctx
)
342 util_hash_table_foreach(fctx
->ref_hash
,
343 debug_flush_might_flush_cb
,
347 static enum pipe_error
348 debug_flush_flush_cb(void *key
, void *value
, void *data
)
350 struct debug_flush_item
*item
=
351 (struct debug_flush_item
*) value
;
353 debug_flush_item_destroy(item
);
360 debug_flush_flush(struct debug_flush_ctx
*fctx
)
365 util_hash_table_foreach(fctx
->ref_hash
,
366 debug_flush_might_flush_cb
,
368 util_hash_table_foreach(fctx
->ref_hash
,
369 debug_flush_flush_cb
,
371 util_hash_table_clear(fctx
->ref_hash
);
375 debug_flush_ctx_destroy(struct debug_flush_ctx
*fctx
)
380 list_del(&fctx
->head
);
381 util_hash_table_foreach(fctx
->ref_hash
,
382 debug_flush_flush_cb
,
384 util_hash_table_clear(fctx
->ref_hash
);
385 util_hash_table_destroy(fctx
->ref_hash
);