cd106af9cda6413d7880be16bd61becd4e328c88
[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 #include <stdio.h>
44 #include <stdlib.h>
45
46 #include "pipe/p_compiler.h"
47 #include "os/os_thread.h"
48 #include "util/u_debug.h"
49 #include "util/u_memory.h"
50 #include "util/u_string.h"
51 #include "util/u_math.h"
52 #include "util/u_format.h"
53
54 #include "tr_dump.h"
55 #include "tr_screen.h"
56 #include "tr_texture.h"
57
58
59 static FILE *stream = NULL;
60 static unsigned refcount = 0;
61 pipe_static_mutex(call_mutex);
62 static long unsigned call_no = 0;
63 static boolean dumping = FALSE;
64
65
66 static INLINE void
67 trace_dump_write(const char *buf, size_t size)
68 {
69 if (stream) {
70 fwrite(buf, size, 1, stream);
71 }
72 }
73
74
75 static INLINE void
76 trace_dump_writes(const char *s)
77 {
78 trace_dump_write(s, strlen(s));
79 }
80
81
82 static INLINE void
83 trace_dump_writef(const char *format, ...)
84 {
85 static char buf[1024];
86 unsigned len;
87 va_list ap;
88 va_start(ap, format);
89 len = util_vsnprintf(buf, sizeof(buf), format, ap);
90 va_end(ap);
91 trace_dump_write(buf, len);
92 }
93
94
95 static INLINE void
96 trace_dump_escape(const char *str)
97 {
98 const unsigned char *p = (const unsigned char *)str;
99 unsigned char c;
100 while((c = *p++) != 0) {
101 if(c == '<')
102 trace_dump_writes("&lt;");
103 else if(c == '>')
104 trace_dump_writes("&gt;");
105 else if(c == '&')
106 trace_dump_writes("&amp;");
107 else if(c == '\'')
108 trace_dump_writes("&apos;");
109 else if(c == '\"')
110 trace_dump_writes("&quot;");
111 else if(c >= 0x20 && c <= 0x7e)
112 trace_dump_writef("%c", c);
113 else
114 trace_dump_writef("&#%u;", c);
115 }
116 }
117
118
119 static INLINE void
120 trace_dump_indent(unsigned level)
121 {
122 unsigned i;
123 for(i = 0; i < level; ++i)
124 trace_dump_writes("\t");
125 }
126
127
128 static INLINE void
129 trace_dump_newline(void)
130 {
131 trace_dump_writes("\n");
132 }
133
134
135 static INLINE void
136 trace_dump_tag(const char *name)
137 {
138 trace_dump_writes("<");
139 trace_dump_writes(name);
140 trace_dump_writes("/>");
141 }
142
143
144 static INLINE void
145 trace_dump_tag_begin(const char *name)
146 {
147 trace_dump_writes("<");
148 trace_dump_writes(name);
149 trace_dump_writes(">");
150 }
151
152 static INLINE void
153 trace_dump_tag_begin1(const char *name,
154 const char *attr1, const char *value1)
155 {
156 trace_dump_writes("<");
157 trace_dump_writes(name);
158 trace_dump_writes(" ");
159 trace_dump_writes(attr1);
160 trace_dump_writes("='");
161 trace_dump_escape(value1);
162 trace_dump_writes("'>");
163 }
164
165
166 static INLINE void
167 trace_dump_tag_begin2(const char *name,
168 const char *attr1, const char *value1,
169 const char *attr2, const char *value2)
170 {
171 trace_dump_writes("<");
172 trace_dump_writes(name);
173 trace_dump_writes(" ");
174 trace_dump_writes(attr1);
175 trace_dump_writes("=\'");
176 trace_dump_escape(value1);
177 trace_dump_writes("\' ");
178 trace_dump_writes(attr2);
179 trace_dump_writes("=\'");
180 trace_dump_escape(value2);
181 trace_dump_writes("\'>");
182 }
183
184
185 static INLINE void
186 trace_dump_tag_begin3(const char *name,
187 const char *attr1, const char *value1,
188 const char *attr2, const char *value2,
189 const char *attr3, const char *value3)
190 {
191 trace_dump_writes("<");
192 trace_dump_writes(name);
193 trace_dump_writes(" ");
194 trace_dump_writes(attr1);
195 trace_dump_writes("=\'");
196 trace_dump_escape(value1);
197 trace_dump_writes("\' ");
198 trace_dump_writes(attr2);
199 trace_dump_writes("=\'");
200 trace_dump_escape(value2);
201 trace_dump_writes("\' ");
202 trace_dump_writes(attr3);
203 trace_dump_writes("=\'");
204 trace_dump_escape(value3);
205 trace_dump_writes("\'>");
206 }
207
208
209 static INLINE void
210 trace_dump_tag_end(const char *name)
211 {
212 trace_dump_writes("</");
213 trace_dump_writes(name);
214 trace_dump_writes(">");
215 }
216
217 void
218 trace_dump_trace_flush(void)
219 {
220 if(stream) {
221 fflush(stream);
222 }
223 }
224
225 static void
226 trace_dump_trace_close(void)
227 {
228 if(stream) {
229 trace_dump_writes("</trace>\n");
230 fclose(stream);
231 stream = NULL;
232 refcount = 0;
233 call_no = 0;
234 }
235 }
236
237 boolean
238 trace_dump_trace_begin(void)
239 {
240 const char *filename;
241
242 filename = debug_get_option("GALLIUM_TRACE", NULL);
243 if(!filename)
244 return FALSE;
245
246 if(!stream) {
247
248 if (strcmp(filename, "stderr") == 0) {
249 stream = stderr;
250 }
251 else if (strcmp(filename, "stdout") == 0) {
252 stream = stdout;
253 }
254 else {
255 stream = fopen(filename, "wt");
256 if (!stream)
257 return FALSE;
258 }
259
260 trace_dump_writes("<?xml version='1.0' encoding='UTF-8'?>\n");
261 trace_dump_writes("<?xml-stylesheet type='text/xsl' href='trace.xsl'?>\n");
262 trace_dump_writes("<trace version='0.1'>\n");
263
264 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE)
265 /* Linux applications rarely cleanup GL / Gallium resources so catch
266 * application exit here */
267 atexit(trace_dump_trace_close);
268 #endif
269 }
270
271 ++refcount;
272
273 return TRUE;
274 }
275
276 boolean trace_dump_trace_enabled(void)
277 {
278 return stream ? TRUE : FALSE;
279 }
280
281 void trace_dump_trace_end(void)
282 {
283 if(stream)
284 if(!--refcount)
285 trace_dump_trace_close();
286 }
287
288 /*
289 * Call lock
290 */
291
292 void trace_dump_call_lock(void)
293 {
294 pipe_mutex_lock(call_mutex);
295 }
296
297 void trace_dump_call_unlock(void)
298 {
299 pipe_mutex_unlock(call_mutex);
300 }
301
302 /*
303 * Dumping control
304 */
305
306 void trace_dumping_start_locked(void)
307 {
308 dumping = TRUE;
309 }
310
311 void trace_dumping_stop_locked(void)
312 {
313 dumping = FALSE;
314 }
315
316 boolean trace_dumping_enabled_locked(void)
317 {
318 return dumping;
319 }
320
321 void trace_dumping_start(void)
322 {
323 pipe_mutex_lock(call_mutex);
324 trace_dumping_start_locked();
325 pipe_mutex_unlock(call_mutex);
326 }
327
328 void trace_dumping_stop(void)
329 {
330 pipe_mutex_lock(call_mutex);
331 trace_dumping_stop_locked();
332 pipe_mutex_unlock(call_mutex);
333 }
334
335 boolean trace_dumping_enabled(void)
336 {
337 boolean ret;
338 pipe_mutex_lock(call_mutex);
339 ret = trace_dumping_enabled_locked();
340 pipe_mutex_unlock(call_mutex);
341 return ret;
342 }
343
344 /*
345 * Dump functions
346 */
347
348 void trace_dump_call_begin_locked(const char *klass, const char *method)
349 {
350 if (!dumping)
351 return;
352
353 ++call_no;
354 trace_dump_indent(1);
355 trace_dump_writes("<call no=\'");
356 trace_dump_writef("%lu", call_no);
357 trace_dump_writes("\' class=\'");
358 trace_dump_escape(klass);
359 trace_dump_writes("\' method=\'");
360 trace_dump_escape(method);
361 trace_dump_writes("\'>");
362 trace_dump_newline();
363 }
364
365 void trace_dump_call_end_locked(void)
366 {
367 if (!dumping)
368 return;
369
370 trace_dump_indent(1);
371 trace_dump_tag_end("call");
372 trace_dump_newline();
373 fflush(stream);
374 }
375
376 void trace_dump_call_begin(const char *klass, const char *method)
377 {
378 pipe_mutex_lock(call_mutex);
379 trace_dump_call_begin_locked(klass, method);
380 }
381
382 void trace_dump_call_end(void)
383 {
384 trace_dump_call_end_locked();
385 pipe_mutex_unlock(call_mutex);
386 }
387
388 void trace_dump_arg_begin(const char *name)
389 {
390 if (!dumping)
391 return;
392
393 trace_dump_indent(2);
394 trace_dump_tag_begin1("arg", "name", name);
395 }
396
397 void trace_dump_arg_end(void)
398 {
399 if (!dumping)
400 return;
401
402 trace_dump_tag_end("arg");
403 trace_dump_newline();
404 }
405
406 void trace_dump_ret_begin(void)
407 {
408 if (!dumping)
409 return;
410
411 trace_dump_indent(2);
412 trace_dump_tag_begin("ret");
413 }
414
415 void trace_dump_ret_end(void)
416 {
417 if (!dumping)
418 return;
419
420 trace_dump_tag_end("ret");
421 trace_dump_newline();
422 }
423
424 void trace_dump_bool(int value)
425 {
426 if (!dumping)
427 return;
428
429 trace_dump_writef("<bool>%c</bool>", value ? '1' : '0');
430 }
431
432 void trace_dump_int(long long int value)
433 {
434 if (!dumping)
435 return;
436
437 trace_dump_writef("<int>%lli</int>", value);
438 }
439
440 void trace_dump_uint(long long unsigned value)
441 {
442 if (!dumping)
443 return;
444
445 trace_dump_writef("<uint>%llu</uint>", value);
446 }
447
448 void trace_dump_float(double value)
449 {
450 if (!dumping)
451 return;
452
453 trace_dump_writef("<float>%g</float>", value);
454 }
455
456 void trace_dump_bytes(const void *data,
457 size_t size)
458 {
459 static const char hex_table[16] = "0123456789ABCDEF";
460 const uint8_t *p = data;
461 size_t i;
462
463 if (!dumping)
464 return;
465
466 trace_dump_writes("<bytes>");
467 for(i = 0; i < size; ++i) {
468 uint8_t byte = *p++;
469 char hex[2];
470 hex[0] = hex_table[byte >> 4];
471 hex[1] = hex_table[byte & 0xf];
472 trace_dump_write(hex, 2);
473 }
474 trace_dump_writes("</bytes>");
475 }
476
477 void trace_dump_box_bytes(const void *data,
478 enum pipe_format format,
479 const struct pipe_box *box,
480 unsigned stride,
481 unsigned slice_stride)
482 {
483 size_t size;
484
485 if (slice_stride)
486 size = box->depth * slice_stride;
487 else if (stride)
488 size = util_format_get_nblocksy(format, box->height) * stride;
489 else {
490 size = util_format_get_nblocksx(format, box->width) * util_format_get_blocksize(format);
491 }
492
493 trace_dump_bytes(data, size);
494 }
495
496 void trace_dump_string(const char *str)
497 {
498 if (!dumping)
499 return;
500
501 trace_dump_writes("<string>");
502 trace_dump_escape(str);
503 trace_dump_writes("</string>");
504 }
505
506 void trace_dump_enum(const char *value)
507 {
508 if (!dumping)
509 return;
510
511 trace_dump_writes("<enum>");
512 trace_dump_escape(value);
513 trace_dump_writes("</enum>");
514 }
515
516 void trace_dump_array_begin(void)
517 {
518 if (!dumping)
519 return;
520
521 trace_dump_writes("<array>");
522 }
523
524 void trace_dump_array_end(void)
525 {
526 if (!dumping)
527 return;
528
529 trace_dump_writes("</array>");
530 }
531
532 void trace_dump_elem_begin(void)
533 {
534 if (!dumping)
535 return;
536
537 trace_dump_writes("<elem>");
538 }
539
540 void trace_dump_elem_end(void)
541 {
542 if (!dumping)
543 return;
544
545 trace_dump_writes("</elem>");
546 }
547
548 void trace_dump_struct_begin(const char *name)
549 {
550 if (!dumping)
551 return;
552
553 trace_dump_writef("<struct name='%s'>", name);
554 }
555
556 void trace_dump_struct_end(void)
557 {
558 if (!dumping)
559 return;
560
561 trace_dump_writes("</struct>");
562 }
563
564 void trace_dump_member_begin(const char *name)
565 {
566 if (!dumping)
567 return;
568
569 trace_dump_writef("<member name='%s'>", name);
570 }
571
572 void trace_dump_member_end(void)
573 {
574 if (!dumping)
575 return;
576
577 trace_dump_writes("</member>");
578 }
579
580 void trace_dump_null(void)
581 {
582 if (!dumping)
583 return;
584
585 trace_dump_writes("<null/>");
586 }
587
588 void trace_dump_ptr(const void *value)
589 {
590 if (!dumping)
591 return;
592
593 if(value)
594 trace_dump_writef("<ptr>0x%08lx</ptr>", (unsigned long)(uintptr_t)value);
595 else
596 trace_dump_null();
597 }
598
599
600 void trace_dump_resource_ptr(struct pipe_resource *_resource)
601 {
602 if (!dumping)
603 return;
604
605 if (_resource) {
606 struct trace_resource *tr_resource = trace_resource(_resource);
607 trace_dump_ptr(tr_resource->resource);
608 } else {
609 trace_dump_null();
610 }
611 }
612
613 void trace_dump_surface_ptr(struct pipe_surface *_surface)
614 {
615 if (!dumping)
616 return;
617
618 if (_surface) {
619 struct trace_surface *tr_surf = trace_surface(_surface);
620 trace_dump_ptr(tr_surf->surface);
621 } else {
622 trace_dump_null();
623 }
624 }
625
626 void trace_dump_transfer_ptr(struct pipe_transfer *_transfer)
627 {
628 if (!dumping)
629 return;
630
631 if (_transfer) {
632 struct trace_transfer *tr_tran = trace_transfer(_transfer);
633 trace_dump_ptr(tr_tran->transfer);
634 } else {
635 trace_dump_null();
636 }
637 }