49349496ac6cbac3b2ba0b94659c71c1b0da7fe1
1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
31 * Trace dumping functions.
33 * For now we just use standard XML for dumping the trace calls, as this is
34 * simple to write, parse, and visually inspect, but the actual representation
35 * is abstracted out of this file, so that we can switch to a binary
36 * representation if/when it becomes justified.
38 * @author Jose Fonseca <jfonseca@vmware.com>
41 #include "pipe/p_config.h"
46 #include "pipe/p_compiler.h"
47 #include "os/os_thread.h"
48 #include "util/os_time.h"
49 #include "util/u_debug.h"
50 #include "util/u_memory.h"
51 #include "util/u_string.h"
52 #include "util/u_math.h"
53 #include "util/u_format.h"
56 #include "tr_screen.h"
57 #include "tr_texture.h"
60 static boolean close_stream
= FALSE
;
61 static FILE *stream
= NULL
;
62 static mtx_t call_mutex
= _MTX_INITIALIZER_NP
;
63 static long unsigned call_no
= 0;
64 static boolean dumping
= FALSE
;
68 trace_dump_write(const char *buf
, size_t size
)
71 fwrite(buf
, size
, 1, stream
);
77 trace_dump_writes(const char *s
)
79 trace_dump_write(s
, strlen(s
));
84 trace_dump_writef(const char *format
, ...)
86 static char buf
[1024];
90 len
= util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
92 trace_dump_write(buf
, len
);
97 trace_dump_escape(const char *str
)
99 const unsigned char *p
= (const unsigned char *)str
;
101 while((c
= *p
++) != 0) {
103 trace_dump_writes("<");
105 trace_dump_writes(">");
107 trace_dump_writes("&");
109 trace_dump_writes("'");
111 trace_dump_writes(""");
112 else if(c
>= 0x20 && c
<= 0x7e)
113 trace_dump_writef("%c", c
);
115 trace_dump_writef("&#%u;", c
);
121 trace_dump_indent(unsigned level
)
124 for(i
= 0; i
< level
; ++i
)
125 trace_dump_writes("\t");
130 trace_dump_newline(void)
132 trace_dump_writes("\n");
137 trace_dump_tag_begin(const char *name
)
139 trace_dump_writes("<");
140 trace_dump_writes(name
);
141 trace_dump_writes(">");
145 trace_dump_tag_begin1(const char *name
,
146 const char *attr1
, const char *value1
)
148 trace_dump_writes("<");
149 trace_dump_writes(name
);
150 trace_dump_writes(" ");
151 trace_dump_writes(attr1
);
152 trace_dump_writes("='");
153 trace_dump_escape(value1
);
154 trace_dump_writes("'>");
159 trace_dump_tag_end(const char *name
)
161 trace_dump_writes("</");
162 trace_dump_writes(name
);
163 trace_dump_writes(">");
167 trace_dump_trace_flush(void)
175 trace_dump_trace_close(void)
178 trace_dump_writes("</trace>\n");
181 close_stream
= FALSE
;
190 trace_dump_call_time(int64_t time
)
193 trace_dump_indent(2);
194 trace_dump_tag_begin("time");
195 trace_dump_int(time
);
196 trace_dump_tag_end("time");
197 trace_dump_newline();
203 trace_dump_trace_begin(void)
205 const char *filename
;
207 filename
= debug_get_option("GALLIUM_TRACE", NULL
);
213 if (strcmp(filename
, "stderr") == 0) {
214 close_stream
= FALSE
;
217 else if (strcmp(filename
, "stdout") == 0) {
218 close_stream
= FALSE
;
223 stream
= fopen(filename
, "wt");
228 trace_dump_writes("<?xml version='1.0' encoding='UTF-8'?>\n");
229 trace_dump_writes("<?xml-stylesheet type='text/xsl' href='trace.xsl'?>\n");
230 trace_dump_writes("<trace version='0.1'>\n");
232 /* Many applications don't exit cleanly, others may create and destroy a
233 * screen multiple times, so we only write </trace> tag and close at exit
236 atexit(trace_dump_trace_close
);
242 boolean
trace_dump_trace_enabled(void)
244 return stream
? TRUE
: FALSE
;
251 void trace_dump_call_lock(void)
253 mtx_lock(&call_mutex
);
256 void trace_dump_call_unlock(void)
258 mtx_unlock(&call_mutex
);
265 void trace_dumping_start_locked(void)
270 void trace_dumping_stop_locked(void)
275 boolean
trace_dumping_enabled_locked(void)
280 void trace_dumping_start(void)
282 mtx_lock(&call_mutex
);
283 trace_dumping_start_locked();
284 mtx_unlock(&call_mutex
);
287 void trace_dumping_stop(void)
289 mtx_lock(&call_mutex
);
290 trace_dumping_stop_locked();
291 mtx_unlock(&call_mutex
);
294 boolean
trace_dumping_enabled(void)
297 mtx_lock(&call_mutex
);
298 ret
= trace_dumping_enabled_locked();
299 mtx_unlock(&call_mutex
);
307 static int64_t call_start_time
= 0;
309 void trace_dump_call_begin_locked(const char *klass
, const char *method
)
315 trace_dump_indent(1);
316 trace_dump_writes("<call no=\'");
317 trace_dump_writef("%lu", call_no
);
318 trace_dump_writes("\' class=\'");
319 trace_dump_escape(klass
);
320 trace_dump_writes("\' method=\'");
321 trace_dump_escape(method
);
322 trace_dump_writes("\'>");
323 trace_dump_newline();
325 call_start_time
= os_time_get();
328 void trace_dump_call_end_locked(void)
330 int64_t call_end_time
;
335 call_end_time
= os_time_get();
337 trace_dump_call_time(call_end_time
- call_start_time
);
338 trace_dump_indent(1);
339 trace_dump_tag_end("call");
340 trace_dump_newline();
344 void trace_dump_call_begin(const char *klass
, const char *method
)
346 mtx_lock(&call_mutex
);
347 trace_dump_call_begin_locked(klass
, method
);
350 void trace_dump_call_end(void)
352 trace_dump_call_end_locked();
353 mtx_unlock(&call_mutex
);
356 void trace_dump_arg_begin(const char *name
)
361 trace_dump_indent(2);
362 trace_dump_tag_begin1("arg", "name", name
);
365 void trace_dump_arg_end(void)
370 trace_dump_tag_end("arg");
371 trace_dump_newline();
374 void trace_dump_ret_begin(void)
379 trace_dump_indent(2);
380 trace_dump_tag_begin("ret");
383 void trace_dump_ret_end(void)
388 trace_dump_tag_end("ret");
389 trace_dump_newline();
392 void trace_dump_bool(int value
)
397 trace_dump_writef("<bool>%c</bool>", value
? '1' : '0');
400 void trace_dump_int(long long int value
)
405 trace_dump_writef("<int>%lli</int>", value
);
408 void trace_dump_uint(long long unsigned value
)
413 trace_dump_writef("<uint>%llu</uint>", value
);
416 void trace_dump_float(double value
)
421 trace_dump_writef("<float>%g</float>", value
);
424 void trace_dump_bytes(const void *data
,
427 static const char hex_table
[16] = "0123456789ABCDEF";
428 const uint8_t *p
= data
;
434 trace_dump_writes("<bytes>");
435 for(i
= 0; i
< size
; ++i
) {
438 hex
[0] = hex_table
[byte
>> 4];
439 hex
[1] = hex_table
[byte
& 0xf];
440 trace_dump_write(hex
, 2);
442 trace_dump_writes("</bytes>");
445 void trace_dump_box_bytes(const void *data
,
446 struct pipe_resource
*resource
,
447 const struct pipe_box
*box
,
449 unsigned slice_stride
)
451 enum pipe_format format
= resource
->format
;
454 assert(box
->height
> 0);
455 assert(box
->depth
> 0);
457 size
= util_format_get_nblocksx(format
, box
->width
) * util_format_get_blocksize(format
)
458 + (util_format_get_nblocksy(format
, box
->height
) - 1) * stride
459 + (box
->depth
- 1) * slice_stride
;
462 * Only dump buffer transfers to avoid huge files.
463 * TODO: Make this run-time configurable
465 if (resource
->target
!= PIPE_BUFFER
) {
469 trace_dump_bytes(data
, size
);
472 void trace_dump_string(const char *str
)
477 trace_dump_writes("<string>");
478 trace_dump_escape(str
);
479 trace_dump_writes("</string>");
482 void trace_dump_enum(const char *value
)
487 trace_dump_writes("<enum>");
488 trace_dump_escape(value
);
489 trace_dump_writes("</enum>");
492 void trace_dump_array_begin(void)
497 trace_dump_writes("<array>");
500 void trace_dump_array_end(void)
505 trace_dump_writes("</array>");
508 void trace_dump_elem_begin(void)
513 trace_dump_writes("<elem>");
516 void trace_dump_elem_end(void)
521 trace_dump_writes("</elem>");
524 void trace_dump_struct_begin(const char *name
)
529 trace_dump_writef("<struct name='%s'>", name
);
532 void trace_dump_struct_end(void)
537 trace_dump_writes("</struct>");
540 void trace_dump_member_begin(const char *name
)
545 trace_dump_writef("<member name='%s'>", name
);
548 void trace_dump_member_end(void)
553 trace_dump_writes("</member>");
556 void trace_dump_null(void)
561 trace_dump_writes("<null/>");
564 void trace_dump_ptr(const void *value
)
570 trace_dump_writef("<ptr>0x%08lx</ptr>", (unsigned long)(uintptr_t)value
);
575 void trace_dump_surface_ptr(struct pipe_surface
*_surface
)
581 struct trace_surface
*tr_surf
= trace_surface(_surface
);
582 trace_dump_ptr(tr_surf
->surface
);
588 void trace_dump_transfer_ptr(struct pipe_transfer
*_transfer
)
594 struct trace_transfer
*tr_tran
= trace_transfer(_transfer
);
595 trace_dump_ptr(tr_tran
->transfer
);