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 pipe_static_mutex(call_mutex);
64 static long unsigned call_no = 0;
65 static boolean dumping = FALSE;
69 trace_dump_write(const char *buf, size_t size)
72 os_stream_write(stream, buf, size);
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(const char *name)
139 trace_dump_writes("<");
140 trace_dump_writes(name);
141 trace_dump_writes("/>");
146 trace_dump_tag_begin(const char *name)
148 trace_dump_writes("<");
149 trace_dump_writes(name);
150 trace_dump_writes(">");
154 trace_dump_tag_begin1(const char *name,
155 const char *attr1, const char *value1)
157 trace_dump_writes("<");
158 trace_dump_writes(name);
159 trace_dump_writes(" ");
160 trace_dump_writes(attr1);
161 trace_dump_writes("='");
162 trace_dump_escape(value1);
163 trace_dump_writes("'>");
168 trace_dump_tag_begin2(const char *name,
169 const char *attr1, const char *value1,
170 const char *attr2, const char *value2)
172 trace_dump_writes("<");
173 trace_dump_writes(name);
174 trace_dump_writes(" ");
175 trace_dump_writes(attr1);
176 trace_dump_writes("=\'");
177 trace_dump_escape(value1);
178 trace_dump_writes("\' ");
179 trace_dump_writes(attr2);
180 trace_dump_writes("=\'");
181 trace_dump_escape(value2);
182 trace_dump_writes("\'>");
187 trace_dump_tag_begin3(const char *name,
188 const char *attr1, const char *value1,
189 const char *attr2, const char *value2,
190 const char *attr3, const char *value3)
192 trace_dump_writes("<");
193 trace_dump_writes(name);
194 trace_dump_writes(" ");
195 trace_dump_writes(attr1);
196 trace_dump_writes("=\'");
197 trace_dump_escape(value1);
198 trace_dump_writes("\' ");
199 trace_dump_writes(attr2);
200 trace_dump_writes("=\'");
201 trace_dump_escape(value2);
202 trace_dump_writes("\' ");
203 trace_dump_writes(attr3);
204 trace_dump_writes("=\'");
205 trace_dump_escape(value3);
206 trace_dump_writes("\'>");
211 trace_dump_tag_end(const char *name)
213 trace_dump_writes("</");
214 trace_dump_writes(name);
215 trace_dump_writes(">");
219 trace_dump_trace_close(void)
222 trace_dump_writes("</trace>\n");
223 os_stream_close(stream);
230 boolean trace_dump_trace_begin()
232 const char *filename;
234 filename = debug_get_option("GALLIUM_TRACE", NULL);
240 stream = os_file_stream_create(filename);
244 trace_dump_writes("<?xml version='1.0' encoding='UTF-8'?>\n");
245 trace_dump_writes("<?xml-stylesheet type='text/xsl' href='trace.xsl'?>\n");
246 trace_dump_writes("<trace version='0.1'>\n");
248 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE)
249 /* Linux applications rarely cleanup GL / Gallium resources so catch
250 * application exit here */
251 atexit(trace_dump_trace_close);
260 boolean trace_dump_trace_enabled(void)
262 return stream ? TRUE : FALSE;
265 void trace_dump_trace_end(void)
269 trace_dump_trace_close();
276 void trace_dump_call_lock(void)
278 pipe_mutex_lock(call_mutex);
281 void trace_dump_call_unlock(void)
283 pipe_mutex_unlock(call_mutex);
290 void trace_dumping_start_locked(void)
295 void trace_dumping_stop_locked(void)
300 boolean trace_dumping_enabled_locked(void)
305 void trace_dumping_start(void)
307 pipe_mutex_lock(call_mutex);
308 trace_dumping_start_locked();
309 pipe_mutex_unlock(call_mutex);
312 void trace_dumping_stop(void)
314 pipe_mutex_lock(call_mutex);
315 trace_dumping_stop_locked();
316 pipe_mutex_unlock(call_mutex);
319 boolean trace_dumping_enabled(void)
322 pipe_mutex_lock(call_mutex);
323 ret = trace_dumping_enabled_locked();
324 pipe_mutex_unlock(call_mutex);
332 void trace_dump_call_begin_locked(const char *klass, const char *method)
338 trace_dump_indent(1);
339 trace_dump_writes("<call no=\'");
340 trace_dump_writef("%lu", call_no);
341 trace_dump_writes("\' class=\'");
342 trace_dump_escape(klass);
343 trace_dump_writes("\' method=\'");
344 trace_dump_escape(method);
345 trace_dump_writes("\'>");
346 trace_dump_newline();
349 void trace_dump_call_end_locked(void)
354 trace_dump_indent(1);
355 trace_dump_tag_end("call");
356 trace_dump_newline();
357 os_stream_flush(stream);
360 void trace_dump_call_begin(const char *klass, const char *method)
362 pipe_mutex_lock(call_mutex);
363 trace_dump_call_begin_locked(klass, method);
366 void trace_dump_call_end(void)
368 trace_dump_call_end_locked();
369 pipe_mutex_unlock(call_mutex);
372 void trace_dump_arg_begin(const char *name)
377 trace_dump_indent(2);
378 trace_dump_tag_begin1("arg", "name", name);
381 void trace_dump_arg_end(void)
386 trace_dump_tag_end("arg");
387 trace_dump_newline();
390 void trace_dump_ret_begin(void)
395 trace_dump_indent(2);
396 trace_dump_tag_begin("ret");
399 void trace_dump_ret_end(void)
404 trace_dump_tag_end("ret");
405 trace_dump_newline();
408 void trace_dump_bool(int value)
413 trace_dump_writef("<bool>%c</bool>", value ? '1' : '0');
416 void trace_dump_int(long long int value)
421 trace_dump_writef("<int>%lli</int>", value);
424 void trace_dump_uint(long long unsigned value)
429 trace_dump_writef("<uint>%llu</uint>", value);
432 void trace_dump_float(double value)
437 trace_dump_writef("<float>%g</float>", value);
440 void trace_dump_bytes(const void *data,
443 static const char hex_table[16] = "0123456789ABCDEF";
444 const uint8_t *p = data;
450 trace_dump_writes("<bytes>");
451 for(i = 0; i < size; ++i) {
454 hex[0] = hex_table[byte >> 4];
455 hex[1] = hex_table[byte & 0xf];
456 trace_dump_write(hex, 2);
458 trace_dump_writes("</bytes>");
461 void trace_dump_box_bytes(const void *data,
462 enum pipe_format format,
463 const struct pipe_box *box,
465 unsigned slice_stride)
470 size = box->depth * slice_stride;
472 size = util_format_get_nblocksy(format, box->height) * stride;
474 size = util_format_get_nblocksx(format, box->width) * util_format_get_blocksize(format);
477 trace_dump_bytes(data, size);
480 void trace_dump_string(const char *str)
485 trace_dump_writes("<string>");
486 trace_dump_escape(str);
487 trace_dump_writes("</string>");
490 void trace_dump_enum(const char *value)
495 trace_dump_writes("<enum>");
496 trace_dump_escape(value);
497 trace_dump_writes("</enum>");
500 void trace_dump_array_begin(void)
505 trace_dump_writes("<array>");
508 void trace_dump_array_end(void)
513 trace_dump_writes("</array>");
516 void trace_dump_elem_begin(void)
521 trace_dump_writes("<elem>");
524 void trace_dump_elem_end(void)
529 trace_dump_writes("</elem>");
532 void trace_dump_struct_begin(const char *name)
537 trace_dump_writef("<struct name='%s'>", name);
540 void trace_dump_struct_end(void)
545 trace_dump_writes("</struct>");
548 void trace_dump_member_begin(const char *name)
553 trace_dump_writef("<member name='%s'>", name);
556 void trace_dump_member_end(void)
561 trace_dump_writes("</member>");
564 void trace_dump_null(void)
569 trace_dump_writes("<null/>");
572 void trace_dump_ptr(const void *value)
578 trace_dump_writef("<ptr>0x%08lx</ptr>", (unsigned long)(uintptr_t)value);
584 void trace_dump_resource_ptr(struct pipe_resource *_resource)
590 struct trace_resource *tr_resource = trace_resource(_resource);
591 trace_dump_ptr(tr_resource->resource);
597 void trace_dump_surface_ptr(struct pipe_surface *_surface)
603 struct trace_surface *tr_surf = trace_surface(_surface);
604 trace_dump_ptr(tr_surf->surface);
610 void trace_dump_transfer_ptr(struct pipe_transfer *_transfer)
616 struct trace_transfer *tr_tran = trace_transfer(_transfer);
617 trace_dump_ptr(tr_tran->transfer);