Add #ifdefs needed to compile Gallium on Solaris with gcc or Sun cc
[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 trace_dump_indent(1);
269 trace_dump_tag_begin2("call", "class", klass, "method", method);
270 trace_dump_newline();
271 }
272
273 void trace_dump_call_end(void)
274 {
275 trace_dump_indent(1);
276 trace_dump_tag_end("call");
277 trace_dump_newline();
278 util_stream_flush(stream);
279 }
280
281 void trace_dump_arg_begin(const char *name)
282 {
283 trace_dump_indent(2);
284 trace_dump_tag_begin1("arg", "name", name);
285 }
286
287 void trace_dump_arg_end(void)
288 {
289 trace_dump_tag_end("arg");
290 trace_dump_newline();
291 }
292
293 void trace_dump_ret_begin(void)
294 {
295 trace_dump_indent(2);
296 trace_dump_tag_begin("ret");
297 }
298
299 void trace_dump_ret_end(void)
300 {
301 trace_dump_tag_end("ret");
302 trace_dump_newline();
303 }
304
305 void trace_dump_bool(int value)
306 {
307 trace_dump_writef("<bool>%c</bool>", value ? '1' : '0');
308 }
309
310 void trace_dump_int(long long int value)
311 {
312 trace_dump_writef("<int>%lli</int>", value);
313 }
314
315 void trace_dump_uint(long long unsigned value)
316 {
317 trace_dump_writef("<uint>%llu</uint>", value);
318 }
319
320 void trace_dump_float(double value)
321 {
322 trace_dump_writef("<float>%g</float>", value);
323 }
324
325 void trace_dump_bytes(const void *data,
326 long unsigned size)
327 {
328 static const char hex_table[16] = "0123456789ABCDEF";
329 const uint8_t *p = data;
330 long unsigned i;
331 trace_dump_writes("<bytes>");
332 for(i = 0; i < size; ++i) {
333 uint8_t byte = *p++;
334 char hex[2];
335 hex[0] = hex_table[byte >> 4];
336 hex[1] = hex_table[byte & 0xf];
337 trace_dump_write(hex, 2);
338 }
339 trace_dump_writes("</bytes>");
340 }
341
342 void trace_dump_string(const char *str)
343 {
344 trace_dump_writes("<string>");
345 trace_dump_escape(str);
346 trace_dump_writes("</string>");
347 }
348
349 void trace_dump_enum(const char *value)
350 {
351 trace_dump_writes("<enum>");
352 trace_dump_escape(value);
353 trace_dump_writes("</enum>");
354 }
355
356 void trace_dump_array_begin(void)
357 {
358 trace_dump_writes("<array>");
359 }
360
361 void trace_dump_array_end(void)
362 {
363 trace_dump_writes("</array>");
364 }
365
366 void trace_dump_elem_begin(void)
367 {
368 trace_dump_writes("<elem>");
369 }
370
371 void trace_dump_elem_end(void)
372 {
373 trace_dump_writes("</elem>");
374 }
375
376 void trace_dump_struct_begin(const char *name)
377 {
378 trace_dump_writef("<struct name='%s'>", name);
379 }
380
381 void trace_dump_struct_end(void)
382 {
383 trace_dump_writes("</struct>");
384 }
385
386 void trace_dump_member_begin(const char *name)
387 {
388 trace_dump_writef("<member name='%s'>", name);
389 }
390
391 void trace_dump_member_end(void)
392 {
393 trace_dump_writes("</member>");
394 }
395
396 void trace_dump_null(void)
397 {
398 trace_dump_writes("<null/>");
399 }
400
401 void trace_dump_ptr(const void *value)
402 {
403 if(value)
404 trace_dump_writef("<ptr>0x%08lx</ptr>", (unsigned long)(uintptr_t)value);
405 else
406 trace_dump_null();
407 }
408
409 void trace_dump_buffer_ptr(struct pipe_buffer *_buffer)
410 {
411 if (_buffer) {
412 struct trace_screen *tr_scr = trace_screen(_buffer->screen);
413 struct trace_buffer *tr_buf = trace_buffer(tr_scr, _buffer);
414 trace_dump_ptr(tr_buf->buffer);
415 } else {
416 trace_dump_null();
417 }
418 }
419
420 void trace_dump_texture_ptr(struct pipe_texture *_texture)
421 {
422 if (_texture) {
423 struct trace_screen *tr_scr = trace_screen(_texture->screen);
424 struct trace_texture *tr_tex = trace_texture(tr_scr, _texture);
425 trace_dump_ptr(tr_tex->texture);
426 } else {
427 trace_dump_null();
428 }
429 }
430
431 void trace_dump_surface_ptr(struct pipe_surface *_surface)
432 {
433 if (_surface) {
434 struct trace_screen *tr_scr = trace_screen(_surface->texture->screen);
435 struct trace_texture *tr_tex = trace_texture(tr_scr, _surface->texture);
436 struct trace_surface *tr_surf = trace_surface(tr_tex, _surface);
437 trace_dump_ptr(tr_surf->surface);
438 } else {
439 trace_dump_null();
440 }
441 }
442
443 void trace_dump_transfer_ptr(struct pipe_transfer *_transfer)
444 {
445 if (_transfer) {
446 struct trace_screen *tr_scr = trace_screen(_transfer->texture->screen);
447 struct trace_texture *tr_tex = trace_texture(tr_scr, _transfer->texture);
448 struct trace_transfer *tr_tran = trace_transfer(tr_tex, _transfer);
449 trace_dump_ptr(tr_tran->transfer);
450 } else {
451 trace_dump_null();
452 }
453 }