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