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