Squashed commit of the following:
[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) || defined(PIPE_OS_APPLE)
44 #include <stdlib.h>
45 #endif
46
47 #include "pipe/p_compiler.h"
48 #include "os/os_thread.h"
49 #include "os/os_stream.h"
50 #include "util/u_debug.h"
51 #include "util/u_memory.h"
52 #include "util/u_string.h"
53
54 #include "tr_dump.h"
55 #include "tr_screen.h"
56 #include "tr_texture.h"
57
58
59 static struct os_stream *stream = NULL;
60 static unsigned refcount = 0;
61 static pipe_mutex call_mutex;
62 static long unsigned call_no = 0;
63 static boolean dumping = FALSE;
64 static boolean initialized = FALSE;
65
66
67 static INLINE void
68 trace_dump_write(const char *buf, size_t size)
69 {
70 if(stream)
71 os_stream_write(stream, buf, size);
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 static void
218 trace_dump_trace_close(void)
219 {
220 if(stream) {
221 trace_dump_writes("</trace>\n");
222 os_stream_close(stream);
223 stream = NULL;
224 refcount = 0;
225 call_no = 0;
226 pipe_mutex_destroy(call_mutex);
227 }
228 }
229
230 void trace_dump_init()
231 {
232 if (initialized)
233 return;
234
235 pipe_mutex_init(call_mutex);
236 dumping = FALSE;
237 initialized = TRUE;
238 }
239
240 boolean trace_dump_trace_begin()
241 {
242 const char *filename;
243
244 assert(initialized);
245
246 filename = debug_get_option("GALLIUM_TRACE", NULL);
247 if(!filename)
248 return FALSE;
249
250 if(!stream) {
251
252 stream = os_file_stream_create(filename);
253 if(!stream)
254 return FALSE;
255
256 trace_dump_writes("<?xml version='1.0' encoding='UTF-8'?>\n");
257 trace_dump_writes("<?xml-stylesheet type='text/xsl' href='trace.xsl'?>\n");
258 trace_dump_writes("<trace version='0.1'>\n");
259
260 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE)
261 /* Linux applications rarely cleanup GL / Gallium resources so catch
262 * application exit here */
263 atexit(trace_dump_trace_close);
264 #endif
265 }
266
267 ++refcount;
268
269 return TRUE;
270 }
271
272 boolean trace_dump_trace_enabled(void)
273 {
274 return stream ? TRUE : FALSE;
275 }
276
277 void trace_dump_trace_end(void)
278 {
279 if(stream)
280 if(!--refcount)
281 trace_dump_trace_close();
282 }
283
284 /*
285 * Call lock
286 */
287
288 void trace_dump_call_lock(void)
289 {
290 pipe_mutex_lock(call_mutex);
291 }
292
293 void trace_dump_call_unlock(void)
294 {
295 pipe_mutex_unlock(call_mutex);
296 }
297
298 /*
299 * Dumping control
300 */
301
302 void trace_dumping_start_locked(void)
303 {
304 dumping = TRUE;
305 }
306
307 void trace_dumping_stop_locked(void)
308 {
309 dumping = FALSE;
310 }
311
312 boolean trace_dumping_enabled_locked(void)
313 {
314 return dumping;
315 }
316
317 void trace_dumping_start(void)
318 {
319 pipe_mutex_lock(call_mutex);
320 trace_dumping_start_locked();
321 pipe_mutex_unlock(call_mutex);
322 }
323
324 void trace_dumping_stop(void)
325 {
326 pipe_mutex_lock(call_mutex);
327 trace_dumping_stop_locked();
328 pipe_mutex_unlock(call_mutex);
329 }
330
331 boolean trace_dumping_enabled(void)
332 {
333 boolean ret;
334 pipe_mutex_lock(call_mutex);
335 ret = trace_dumping_enabled_locked();
336 pipe_mutex_unlock(call_mutex);
337 return ret;
338 }
339
340 /*
341 * Dump functions
342 */
343
344 void trace_dump_call_begin_locked(const char *klass, const char *method)
345 {
346 if (!dumping)
347 return;
348
349 ++call_no;
350 trace_dump_indent(1);
351 trace_dump_writes("<call no=\'");
352 trace_dump_writef("%lu", call_no);
353 trace_dump_writes("\' class=\'");
354 trace_dump_escape(klass);
355 trace_dump_writes("\' method=\'");
356 trace_dump_escape(method);
357 trace_dump_writes("\'>");
358 trace_dump_newline();
359 }
360
361 void trace_dump_call_end_locked(void)
362 {
363 if (!dumping)
364 return;
365
366 trace_dump_indent(1);
367 trace_dump_tag_end("call");
368 trace_dump_newline();
369 os_stream_flush(stream);
370 }
371
372 void trace_dump_call_begin(const char *klass, const char *method)
373 {
374 pipe_mutex_lock(call_mutex);
375 trace_dump_call_begin_locked(klass, method);
376 }
377
378 void trace_dump_call_end(void)
379 {
380 trace_dump_call_end_locked();
381 pipe_mutex_unlock(call_mutex);
382 }
383
384 void trace_dump_arg_begin(const char *name)
385 {
386 if (!dumping)
387 return;
388
389 trace_dump_indent(2);
390 trace_dump_tag_begin1("arg", "name", name);
391 }
392
393 void trace_dump_arg_end(void)
394 {
395 if (!dumping)
396 return;
397
398 trace_dump_tag_end("arg");
399 trace_dump_newline();
400 }
401
402 void trace_dump_ret_begin(void)
403 {
404 if (!dumping)
405 return;
406
407 trace_dump_indent(2);
408 trace_dump_tag_begin("ret");
409 }
410
411 void trace_dump_ret_end(void)
412 {
413 if (!dumping)
414 return;
415
416 trace_dump_tag_end("ret");
417 trace_dump_newline();
418 }
419
420 void trace_dump_bool(int value)
421 {
422 if (!dumping)
423 return;
424
425 trace_dump_writef("<bool>%c</bool>", value ? '1' : '0');
426 }
427
428 void trace_dump_int(long long int value)
429 {
430 if (!dumping)
431 return;
432
433 trace_dump_writef("<int>%lli</int>", value);
434 }
435
436 void trace_dump_uint(long long unsigned value)
437 {
438 if (!dumping)
439 return;
440
441 trace_dump_writef("<uint>%llu</uint>", value);
442 }
443
444 void trace_dump_float(double value)
445 {
446 if (!dumping)
447 return;
448
449 trace_dump_writef("<float>%g</float>", value);
450 }
451
452 void trace_dump_bytes(const void *data,
453 size_t size)
454 {
455 static const char hex_table[16] = "0123456789ABCDEF";
456 const uint8_t *p = data;
457 size_t i;
458
459 if (!dumping)
460 return;
461
462 trace_dump_writes("<bytes>");
463 for(i = 0; i < size; ++i) {
464 uint8_t byte = *p++;
465 char hex[2];
466 hex[0] = hex_table[byte >> 4];
467 hex[1] = hex_table[byte & 0xf];
468 trace_dump_write(hex, 2);
469 }
470 trace_dump_writes("</bytes>");
471 }
472
473 void trace_dump_box_bytes(const void *data,
474 unsigned format,
475 const struct pipe_box *box,
476 unsigned stride,
477 unsigned slice_stride)
478 {
479 //size_t size = util_format_get_nblocksy(transfer->resource->format, transfer->box.height) * transfer->stride;
480
481 }
482
483 void trace_dump_string(const char *str)
484 {
485 if (!dumping)
486 return;
487
488 trace_dump_writes("<string>");
489 trace_dump_escape(str);
490 trace_dump_writes("</string>");
491 }
492
493 void trace_dump_enum(const char *value)
494 {
495 if (!dumping)
496 return;
497
498 trace_dump_writes("<enum>");
499 trace_dump_escape(value);
500 trace_dump_writes("</enum>");
501 }
502
503 void trace_dump_array_begin(void)
504 {
505 if (!dumping)
506 return;
507
508 trace_dump_writes("<array>");
509 }
510
511 void trace_dump_array_end(void)
512 {
513 if (!dumping)
514 return;
515
516 trace_dump_writes("</array>");
517 }
518
519 void trace_dump_elem_begin(void)
520 {
521 if (!dumping)
522 return;
523
524 trace_dump_writes("<elem>");
525 }
526
527 void trace_dump_elem_end(void)
528 {
529 if (!dumping)
530 return;
531
532 trace_dump_writes("</elem>");
533 }
534
535 void trace_dump_struct_begin(const char *name)
536 {
537 if (!dumping)
538 return;
539
540 trace_dump_writef("<struct name='%s'>", name);
541 }
542
543 void trace_dump_struct_end(void)
544 {
545 if (!dumping)
546 return;
547
548 trace_dump_writes("</struct>");
549 }
550
551 void trace_dump_member_begin(const char *name)
552 {
553 if (!dumping)
554 return;
555
556 trace_dump_writef("<member name='%s'>", name);
557 }
558
559 void trace_dump_member_end(void)
560 {
561 if (!dumping)
562 return;
563
564 trace_dump_writes("</member>");
565 }
566
567 void trace_dump_null(void)
568 {
569 if (!dumping)
570 return;
571
572 trace_dump_writes("<null/>");
573 }
574
575 void trace_dump_ptr(const void *value)
576 {
577 if (!dumping)
578 return;
579
580 if(value)
581 trace_dump_writef("<ptr>0x%08lx</ptr>", (unsigned long)(uintptr_t)value);
582 else
583 trace_dump_null();
584 }
585
586
587 void trace_dump_resource_ptr(struct pipe_resource *_resource)
588 {
589 if (!dumping)
590 return;
591
592 if (_resource) {
593 struct trace_resource *tr_resource = trace_resource(_resource);
594 trace_dump_ptr(tr_resource->resource);
595 } else {
596 trace_dump_null();
597 }
598 }
599
600 void trace_dump_surface_ptr(struct pipe_surface *_surface)
601 {
602 if (!dumping)
603 return;
604
605 if (_surface) {
606 struct trace_surface *tr_surf = trace_surface(_surface);
607 trace_dump_ptr(tr_surf->surface);
608 } else {
609 trace_dump_null();
610 }
611 }
612
613 void trace_dump_transfer_ptr(struct pipe_transfer *_transfer)
614 {
615 if (!dumping)
616 return;
617
618 if (_transfer) {
619 struct trace_transfer *tr_tran = trace_transfer(_transfer);
620 trace_dump_ptr(tr_tran->transfer);
621 } else {
622 trace_dump_null();
623 }
624 }