97872be784de95ab15ce5fd8c72cc21abcd65c79
[mesa.git] / src / gallium / drivers / trace / tr_dump.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * @file
31 * Trace dumping functions.
32 *
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.
37 *
38 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
39 */
40
41 #include "pipe/p_config.h"
42
43 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
44 #include <stdlib.h>
45 #endif
46
47 #include "pipe/p_compiler.h"
48 #include "util/u_debug.h"
49 #include "util/u_memory.h"
50 #include "util/u_string.h"
51 #include "util/u_stream.h"
52
53 #include "tr_dump.h"
54 #include "tr_screen.h"
55 #include "tr_texture.h"
56 #include "tr_buffer.h"
57
58
59 static struct util_stream *stream = NULL;
60 static unsigned refcount = 0;
61
62
63 static INLINE void
64 trace_dump_write(const char *buf, size_t size)
65 {
66 if(stream)
67 util_stream_write(stream, buf, size);
68 }
69
70
71 static INLINE void
72 trace_dump_writes(const char *s)
73 {
74 trace_dump_write(s, strlen(s));
75 }
76
77
78 static INLINE void
79 trace_dump_writef(const char *format, ...)
80 {
81 static char buf[1024];
82 unsigned len;
83 va_list ap;
84 va_start(ap, format);
85 len = util_vsnprintf(buf, sizeof(buf), format, ap);
86 va_end(ap);
87 trace_dump_write(buf, len);
88 }
89
90
91 static INLINE void
92 trace_dump_escape(const char *str)
93 {
94 const unsigned char *p = (const unsigned char *)str;
95 unsigned char c;
96 while((c = *p++) != 0) {
97 if(c == '<')
98 trace_dump_writes("&lt;");
99 else if(c == '>')
100 trace_dump_writes("&gt;");
101 else if(c == '&')
102 trace_dump_writes("&amp;");
103 else if(c == '\'')
104 trace_dump_writes("&apos;");
105 else if(c == '\"')
106 trace_dump_writes("&quot;");
107 else if(c >= 0x20 && c <= 0x7e)
108 trace_dump_writef("%c", c);
109 else
110 trace_dump_writef("&#%u;", c);
111 }
112 }
113
114
115 static INLINE void
116 trace_dump_indent(unsigned level)
117 {
118 unsigned i;
119 for(i = 0; i < level; ++i)
120 trace_dump_writes("\t");
121 }
122
123
124 static INLINE void
125 trace_dump_newline(void)
126 {
127 trace_dump_writes("\n");
128 }
129
130
131 static INLINE void
132 trace_dump_tag(const char *name)
133 {
134 trace_dump_writes("<");
135 trace_dump_writes(name);
136 trace_dump_writes("/>");
137 }
138
139
140 static INLINE void
141 trace_dump_tag_begin(const char *name)
142 {
143 trace_dump_writes("<");
144 trace_dump_writes(name);
145 trace_dump_writes(">");
146 }
147
148 static INLINE void
149 trace_dump_tag_begin1(const char *name,
150 const char *attr1, const char *value1)
151 {
152 trace_dump_writes("<");
153 trace_dump_writes(name);
154 trace_dump_writes(" ");
155 trace_dump_writes(attr1);
156 trace_dump_writes("='");
157 trace_dump_escape(value1);
158 trace_dump_writes("'>");
159 }
160
161
162 static INLINE void
163 trace_dump_tag_begin2(const char *name,
164 const char *attr1, const char *value1,
165 const char *attr2, const char *value2)
166 {
167 trace_dump_writes("<");
168 trace_dump_writes(name);
169 trace_dump_writes(" ");
170 trace_dump_writes(attr1);
171 trace_dump_writes("=\'");
172 trace_dump_escape(value1);
173 trace_dump_writes("\' ");
174 trace_dump_writes(attr2);
175 trace_dump_writes("=\'");
176 trace_dump_escape(value2);
177 trace_dump_writes("\'>");
178 }
179
180
181 static INLINE void
182 trace_dump_tag_begin3(const char *name,
183 const char *attr1, const char *value1,
184 const char *attr2, const char *value2,
185 const char *attr3, const char *value3)
186 {
187 trace_dump_writes("<");
188 trace_dump_writes(name);
189 trace_dump_writes(" ");
190 trace_dump_writes(attr1);
191 trace_dump_writes("=\'");
192 trace_dump_escape(value1);
193 trace_dump_writes("\' ");
194 trace_dump_writes(attr2);
195 trace_dump_writes("=\'");
196 trace_dump_escape(value2);
197 trace_dump_writes("\' ");
198 trace_dump_writes(attr3);
199 trace_dump_writes("=\'");
200 trace_dump_escape(value3);
201 trace_dump_writes("\'>");
202 }
203
204
205 static INLINE void
206 trace_dump_tag_end(const char *name)
207 {
208 trace_dump_writes("</");
209 trace_dump_writes(name);
210 trace_dump_writes(">");
211 }
212
213 static void
214 trace_dump_trace_close(void)
215 {
216 if(stream) {
217 trace_dump_writes("</trace>\n");
218 util_stream_close(stream);
219 stream = NULL;
220 refcount = 0;
221 }
222 }
223
224 boolean trace_dump_trace_begin()
225 {
226 const char *filename;
227
228 filename = debug_get_option("GALLIUM_TRACE", NULL);
229 if(!filename)
230 return FALSE;
231
232 if(!stream) {
233
234 stream = util_stream_create(filename, 0);
235 if(!stream)
236 return FALSE;
237
238 trace_dump_writes("<?xml version='1.0' encoding='UTF-8'?>\n");
239 trace_dump_writes("<?xml-stylesheet type='text/xsl' href='trace.xsl'?>\n");
240 trace_dump_writes("<trace version='0.1'>\n");
241
242 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
243 /* Linux applications rarely cleanup GL / Gallium resources so catch
244 * application exit here */
245 atexit(trace_dump_trace_close);
246 #endif
247 }
248
249 ++refcount;
250
251 return TRUE;
252 }
253
254 boolean trace_dump_enabled(void)
255 {
256 return stream ? TRUE : FALSE;
257 }
258
259 void trace_dump_trace_end(void)
260 {
261 if(stream)
262 if(!--refcount)
263 trace_dump_trace_close();
264 }
265
266 void trace_dump_call_begin(const char *klass, const char *method)
267 {
268 static long unsigned no = 0;
269 ++no;
270 trace_dump_indent(1);
271 trace_dump_writes("<call no=\'");
272 trace_dump_writef("%lu", no);
273 trace_dump_writes("\' class =\'");
274 trace_dump_escape(klass);
275 trace_dump_writes("\' method=\'");
276 trace_dump_escape(method);
277 trace_dump_writes("\'>");
278 trace_dump_newline();
279 }
280
281 void trace_dump_call_end(void)
282 {
283 trace_dump_indent(1);
284 trace_dump_tag_end("call");
285 trace_dump_newline();
286 util_stream_flush(stream);
287 }
288
289 void trace_dump_arg_begin(const char *name)
290 {
291 trace_dump_indent(2);
292 trace_dump_tag_begin1("arg", "name", name);
293 }
294
295 void trace_dump_arg_end(void)
296 {
297 trace_dump_tag_end("arg");
298 trace_dump_newline();
299 }
300
301 void trace_dump_ret_begin(void)
302 {
303 trace_dump_indent(2);
304 trace_dump_tag_begin("ret");
305 }
306
307 void trace_dump_ret_end(void)
308 {
309 trace_dump_tag_end("ret");
310 trace_dump_newline();
311 }
312
313 void trace_dump_bool(int value)
314 {
315 trace_dump_writef("<bool>%c</bool>", value ? '1' : '0');
316 }
317
318 void trace_dump_int(long long int value)
319 {
320 trace_dump_writef("<int>%lli</int>", value);
321 }
322
323 void trace_dump_uint(long long unsigned value)
324 {
325 trace_dump_writef("<uint>%llu</uint>", value);
326 }
327
328 void trace_dump_float(double value)
329 {
330 trace_dump_writef("<float>%g</float>", value);
331 }
332
333 void trace_dump_bytes(const void *data,
334 long unsigned size)
335 {
336 static const char hex_table[16] = "0123456789ABCDEF";
337 const uint8_t *p = data;
338 long unsigned i;
339 trace_dump_writes("<bytes>");
340 for(i = 0; i < size; ++i) {
341 uint8_t byte = *p++;
342 char hex[2];
343 hex[0] = hex_table[byte >> 4];
344 hex[1] = hex_table[byte & 0xf];
345 trace_dump_write(hex, 2);
346 }
347 trace_dump_writes("</bytes>");
348 }
349
350 void trace_dump_string(const char *str)
351 {
352 trace_dump_writes("<string>");
353 trace_dump_escape(str);
354 trace_dump_writes("</string>");
355 }
356
357 void trace_dump_enum(const char *value)
358 {
359 trace_dump_writes("<enum>");
360 trace_dump_escape(value);
361 trace_dump_writes("</enum>");
362 }
363
364 void trace_dump_array_begin(void)
365 {
366 trace_dump_writes("<array>");
367 }
368
369 void trace_dump_array_end(void)
370 {
371 trace_dump_writes("</array>");
372 }
373
374 void trace_dump_elem_begin(void)
375 {
376 trace_dump_writes("<elem>");
377 }
378
379 void trace_dump_elem_end(void)
380 {
381 trace_dump_writes("</elem>");
382 }
383
384 void trace_dump_struct_begin(const char *name)
385 {
386 trace_dump_writef("<struct name='%s'>", name);
387 }
388
389 void trace_dump_struct_end(void)
390 {
391 trace_dump_writes("</struct>");
392 }
393
394 void trace_dump_member_begin(const char *name)
395 {
396 trace_dump_writef("<member name='%s'>", name);
397 }
398
399 void trace_dump_member_end(void)
400 {
401 trace_dump_writes("</member>");
402 }
403
404 void trace_dump_null(void)
405 {
406 trace_dump_writes("<null/>");
407 }
408
409 void trace_dump_ptr(const void *value)
410 {
411 if(value)
412 trace_dump_writef("<ptr>0x%08lx</ptr>", (unsigned long)(uintptr_t)value);
413 else
414 trace_dump_null();
415 }
416
417 void trace_dump_buffer_ptr(struct pipe_buffer *_buffer)
418 {
419 if (_buffer) {
420 struct trace_screen *tr_scr = trace_screen(_buffer->screen);
421 struct trace_buffer *tr_buf = trace_buffer(tr_scr, _buffer);
422 trace_dump_ptr(tr_buf->buffer);
423 } else {
424 trace_dump_null();
425 }
426 }
427
428 void trace_dump_texture_ptr(struct pipe_texture *_texture)
429 {
430 if (_texture) {
431 struct trace_screen *tr_scr = trace_screen(_texture->screen);
432 struct trace_texture *tr_tex = trace_texture(tr_scr, _texture);
433 trace_dump_ptr(tr_tex->texture);
434 } else {
435 trace_dump_null();
436 }
437 }
438
439 void trace_dump_surface_ptr(struct pipe_surface *_surface)
440 {
441 if (_surface) {
442 struct trace_screen *tr_scr = trace_screen(_surface->texture->screen);
443 struct trace_texture *tr_tex = trace_texture(tr_scr, _surface->texture);
444 struct trace_surface *tr_surf = trace_surface(tr_tex, _surface);
445 trace_dump_ptr(tr_surf->surface);
446 } else {
447 trace_dump_null();
448 }
449 }
450
451 void trace_dump_transfer_ptr(struct pipe_transfer *_transfer)
452 {
453 if (_transfer) {
454 struct trace_screen *tr_scr = trace_screen(_transfer->texture->screen);
455 struct trace_texture *tr_tex = trace_texture(tr_scr, _transfer->texture);
456 struct trace_transfer *tr_tran = trace_transfer(tr_tex, _transfer);
457 trace_dump_ptr(tr_tran->transfer);
458 } else {
459 trace_dump_null();
460 }
461 }