1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <jrfonseca@tungstengraphics.com>
41 #include "pipe/p_config.h"
43 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE)
47 #include "pipe/p_compiler.h"
48 #include "os/os_thread.h"
49 #include "os/os_stream.h"
50 #include "util/u_debug.h"
51 #include "util/u_memory.h"
52 #include "util/u_string.h"
53 #include "util/u_math.h"
54 #include "util/u_format.h"
57 #include "tr_screen.h"
58 #include "tr_texture.h"
61 static struct os_stream
*stream
= NULL
;
62 static unsigned refcount
= 0;
63 static pipe_mutex call_mutex
;
64 static long unsigned call_no
= 0;
65 static boolean dumping
= FALSE
;
66 static boolean initialized
= FALSE
;
70 trace_dump_write(const char *buf
, size_t size
)
73 os_stream_write(stream
, buf
, size
);
78 trace_dump_writes(const char *s
)
80 trace_dump_write(s
, strlen(s
));
85 trace_dump_writef(const char *format
, ...)
87 static char buf
[1024];
91 len
= util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
93 trace_dump_write(buf
, len
);
98 trace_dump_escape(const char *str
)
100 const unsigned char *p
= (const unsigned char *)str
;
102 while((c
= *p
++) != 0) {
104 trace_dump_writes("<");
106 trace_dump_writes(">");
108 trace_dump_writes("&");
110 trace_dump_writes("'");
112 trace_dump_writes(""");
113 else if(c
>= 0x20 && c
<= 0x7e)
114 trace_dump_writef("%c", c
);
116 trace_dump_writef("&#%u;", c
);
122 trace_dump_indent(unsigned level
)
125 for(i
= 0; i
< level
; ++i
)
126 trace_dump_writes("\t");
131 trace_dump_newline(void)
133 trace_dump_writes("\n");
138 trace_dump_tag(const char *name
)
140 trace_dump_writes("<");
141 trace_dump_writes(name
);
142 trace_dump_writes("/>");
147 trace_dump_tag_begin(const char *name
)
149 trace_dump_writes("<");
150 trace_dump_writes(name
);
151 trace_dump_writes(">");
155 trace_dump_tag_begin1(const char *name
,
156 const char *attr1
, const char *value1
)
158 trace_dump_writes("<");
159 trace_dump_writes(name
);
160 trace_dump_writes(" ");
161 trace_dump_writes(attr1
);
162 trace_dump_writes("='");
163 trace_dump_escape(value1
);
164 trace_dump_writes("'>");
169 trace_dump_tag_begin2(const char *name
,
170 const char *attr1
, const char *value1
,
171 const char *attr2
, const char *value2
)
173 trace_dump_writes("<");
174 trace_dump_writes(name
);
175 trace_dump_writes(" ");
176 trace_dump_writes(attr1
);
177 trace_dump_writes("=\'");
178 trace_dump_escape(value1
);
179 trace_dump_writes("\' ");
180 trace_dump_writes(attr2
);
181 trace_dump_writes("=\'");
182 trace_dump_escape(value2
);
183 trace_dump_writes("\'>");
188 trace_dump_tag_begin3(const char *name
,
189 const char *attr1
, const char *value1
,
190 const char *attr2
, const char *value2
,
191 const char *attr3
, const char *value3
)
193 trace_dump_writes("<");
194 trace_dump_writes(name
);
195 trace_dump_writes(" ");
196 trace_dump_writes(attr1
);
197 trace_dump_writes("=\'");
198 trace_dump_escape(value1
);
199 trace_dump_writes("\' ");
200 trace_dump_writes(attr2
);
201 trace_dump_writes("=\'");
202 trace_dump_escape(value2
);
203 trace_dump_writes("\' ");
204 trace_dump_writes(attr3
);
205 trace_dump_writes("=\'");
206 trace_dump_escape(value3
);
207 trace_dump_writes("\'>");
212 trace_dump_tag_end(const char *name
)
214 trace_dump_writes("</");
215 trace_dump_writes(name
);
216 trace_dump_writes(">");
220 trace_dump_trace_close(void)
223 trace_dump_writes("</trace>\n");
224 os_stream_close(stream
);
228 pipe_mutex_destroy(call_mutex
);
232 void trace_dump_init()
237 pipe_mutex_init(call_mutex
);
242 boolean
trace_dump_trace_begin()
244 const char *filename
;
248 filename
= debug_get_option("GALLIUM_TRACE", NULL
);
254 stream
= os_file_stream_create(filename
);
258 trace_dump_writes("<?xml version='1.0' encoding='UTF-8'?>\n");
259 trace_dump_writes("<?xml-stylesheet type='text/xsl' href='trace.xsl'?>\n");
260 trace_dump_writes("<trace version='0.1'>\n");
262 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE)
263 /* Linux applications rarely cleanup GL / Gallium resources so catch
264 * application exit here */
265 atexit(trace_dump_trace_close
);
274 boolean
trace_dump_trace_enabled(void)
276 return stream
? TRUE
: FALSE
;
279 void trace_dump_trace_end(void)
283 trace_dump_trace_close();
290 void trace_dump_call_lock(void)
292 pipe_mutex_lock(call_mutex
);
295 void trace_dump_call_unlock(void)
297 pipe_mutex_unlock(call_mutex
);
304 void trace_dumping_start_locked(void)
309 void trace_dumping_stop_locked(void)
314 boolean
trace_dumping_enabled_locked(void)
319 void trace_dumping_start(void)
321 pipe_mutex_lock(call_mutex
);
322 trace_dumping_start_locked();
323 pipe_mutex_unlock(call_mutex
);
326 void trace_dumping_stop(void)
328 pipe_mutex_lock(call_mutex
);
329 trace_dumping_stop_locked();
330 pipe_mutex_unlock(call_mutex
);
333 boolean
trace_dumping_enabled(void)
336 pipe_mutex_lock(call_mutex
);
337 ret
= trace_dumping_enabled_locked();
338 pipe_mutex_unlock(call_mutex
);
346 void trace_dump_call_begin_locked(const char *klass
, const char *method
)
352 trace_dump_indent(1);
353 trace_dump_writes("<call no=\'");
354 trace_dump_writef("%lu", call_no
);
355 trace_dump_writes("\' class=\'");
356 trace_dump_escape(klass
);
357 trace_dump_writes("\' method=\'");
358 trace_dump_escape(method
);
359 trace_dump_writes("\'>");
360 trace_dump_newline();
363 void trace_dump_call_end_locked(void)
368 trace_dump_indent(1);
369 trace_dump_tag_end("call");
370 trace_dump_newline();
371 os_stream_flush(stream
);
374 void trace_dump_call_begin(const char *klass
, const char *method
)
376 pipe_mutex_lock(call_mutex
);
377 trace_dump_call_begin_locked(klass
, method
);
380 void trace_dump_call_end(void)
382 trace_dump_call_end_locked();
383 pipe_mutex_unlock(call_mutex
);
386 void trace_dump_arg_begin(const char *name
)
391 trace_dump_indent(2);
392 trace_dump_tag_begin1("arg", "name", name
);
395 void trace_dump_arg_end(void)
400 trace_dump_tag_end("arg");
401 trace_dump_newline();
404 void trace_dump_ret_begin(void)
409 trace_dump_indent(2);
410 trace_dump_tag_begin("ret");
413 void trace_dump_ret_end(void)
418 trace_dump_tag_end("ret");
419 trace_dump_newline();
422 void trace_dump_bool(int value
)
427 trace_dump_writef("<bool>%c</bool>", value
? '1' : '0');
430 void trace_dump_int(long long int value
)
435 trace_dump_writef("<int>%lli</int>", value
);
438 void trace_dump_uint(long long unsigned value
)
443 trace_dump_writef("<uint>%llu</uint>", value
);
446 void trace_dump_float(double value
)
451 trace_dump_writef("<float>%g</float>", value
);
454 void trace_dump_bytes(const void *data
,
457 static const char hex_table
[16] = "0123456789ABCDEF";
458 const uint8_t *p
= data
;
464 trace_dump_writes("<bytes>");
465 for(i
= 0; i
< size
; ++i
) {
468 hex
[0] = hex_table
[byte
>> 4];
469 hex
[1] = hex_table
[byte
& 0xf];
470 trace_dump_write(hex
, 2);
472 trace_dump_writes("</bytes>");
475 void trace_dump_box_bytes(const void *data
,
476 enum pipe_format format
,
477 const struct pipe_box
*box
,
479 unsigned slice_stride
)
484 size
= box
->depth
* slice_stride
;
486 size
= util_format_get_nblocksy(format
, box
->height
) * stride
;
488 size
= util_format_get_nblocksx(format
, box
->width
) * util_format_get_blocksize(format
);
491 trace_dump_bytes(data
, size
);
494 void trace_dump_string(const char *str
)
499 trace_dump_writes("<string>");
500 trace_dump_escape(str
);
501 trace_dump_writes("</string>");
504 void trace_dump_enum(const char *value
)
509 trace_dump_writes("<enum>");
510 trace_dump_escape(value
);
511 trace_dump_writes("</enum>");
514 void trace_dump_array_begin(void)
519 trace_dump_writes("<array>");
522 void trace_dump_array_end(void)
527 trace_dump_writes("</array>");
530 void trace_dump_elem_begin(void)
535 trace_dump_writes("<elem>");
538 void trace_dump_elem_end(void)
543 trace_dump_writes("</elem>");
546 void trace_dump_struct_begin(const char *name
)
551 trace_dump_writef("<struct name='%s'>", name
);
554 void trace_dump_struct_end(void)
559 trace_dump_writes("</struct>");
562 void trace_dump_member_begin(const char *name
)
567 trace_dump_writef("<member name='%s'>", name
);
570 void trace_dump_member_end(void)
575 trace_dump_writes("</member>");
578 void trace_dump_null(void)
583 trace_dump_writes("<null/>");
586 void trace_dump_ptr(const void *value
)
592 trace_dump_writef("<ptr>0x%08lx</ptr>", (unsigned long)(uintptr_t)value
);
598 void trace_dump_resource_ptr(struct pipe_resource
*_resource
)
604 struct trace_resource
*tr_resource
= trace_resource(_resource
);
605 trace_dump_ptr(tr_resource
->resource
);
611 void trace_dump_surface_ptr(struct pipe_surface
*_surface
)
617 struct trace_surface
*tr_surf
= trace_surface(_surface
);
618 trace_dump_ptr(tr_surf
->surface
);
624 void trace_dump_transfer_ptr(struct pipe_transfer
*_transfer
)
630 struct trace_transfer
*tr_tran
= trace_transfer(_transfer
);
631 trace_dump_ptr(tr_tran
->transfer
);