summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/trace/tr_dump.c
blob: 5269c4ddc849dfb4aba3d701db8223a089c4912d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**************************************************************************
 *
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/


/**
 * @file
 * Trace dumping functions.
 * 
 * For now we just use standard XML for dumping the trace calls, as this is
 * simple to write, parse, and visually inspect, but the actual representation 
 * is abstracted out of this file, so that we can switch to a binary 
 * representation if/when it becomes justified.
 * 
 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>   
 */


#include "pipe/p_compiler.h"
#include "util/u_string.h"

#include "tr_stream.h"
#include "tr_dump.h"


static INLINE void 
trace_dump_write(struct trace_stream *stream, const char *s)
{
   trace_stream_write(stream, s, strlen(s));
}


static INLINE void 
trace_dump_writef(struct trace_stream *stream, const char *format, ...)
{
   char buf[1024];
   va_list ap;
   va_start(ap, format);
   util_vsnprintf(buf, sizeof(buf), format, ap);
   va_end(ap);
   trace_dump_write(stream, buf);
}


static INLINE void 
trace_dump_escape(struct trace_stream *stream, const char *str) 
{
   const unsigned char *p = (const unsigned char *)str;
   unsigned char c;
   while((c = *p++) != 0) {
      if(c == '<')
         trace_dump_write(stream, "&lt;");
      else if(c == '>')
         trace_dump_write(stream, "&gt;");
      else if(c == '&')
         trace_dump_write(stream, "&amp;");
      else if(c == '\'')
         trace_dump_write(stream, "&apos;");
      else if(c == '\"')
         trace_dump_write(stream, "&quot;");
      else if(c >= 0x20 && c <= 0x7e)
         trace_dump_writef(stream, "%c", c);
      else
         trace_dump_writef(stream, "&#%u;", c);
   }
}


static INLINE void 
trace_dump_indent(struct trace_stream *stream, unsigned level)
{
   unsigned i;
   for(i = 0; i < level; ++i)
      trace_dump_write(stream, "\t");
}


static INLINE void 
trace_dump_newline(struct trace_stream *stream) 
{
   trace_dump_write(stream, "\n");
}


static INLINE void 
trace_dump_tag(struct trace_stream *stream, 
               const char *name)
{
   trace_dump_write(stream, "<");
   trace_dump_write(stream, name);
   trace_dump_write(stream, "/>");
}


static INLINE void 
trace_dump_tag_begin(struct trace_stream *stream, 
                     const char *name)
{
   trace_dump_write(stream, "<");
   trace_dump_write(stream, name);
   trace_dump_write(stream, ">");
}

static INLINE void 
trace_dump_tag_begin1(struct trace_stream *stream, 
                      const char *name, 
                      const char *attr1, const char *value1)
{
   trace_dump_write(stream, "<");
   trace_dump_write(stream, name);
   trace_dump_write(stream, " ");
   trace_dump_write(stream, attr1);
   trace_dump_write(stream, "='");
   trace_dump_escape(stream, value1);
   trace_dump_write(stream, "'>");
}


static INLINE void 
trace_dump_tag_begin2(struct trace_stream *stream, 
                      const char *name, 
                      const char *attr1, const char *value1,
                      const char *attr2, const char *value2)
{
   trace_dump_write(stream, "<");
   trace_dump_write(stream, name);
   trace_dump_write(stream, " ");
   trace_dump_write(stream, attr1);
   trace_dump_write(stream, "=\'");
   trace_dump_escape(stream, value1);
   trace_dump_write(stream, "\' ");
   trace_dump_write(stream, attr2);
   trace_dump_write(stream, "=\'");
   trace_dump_escape(stream, value2);
   trace_dump_write(stream, "\'>");
}


static INLINE void 
trace_dump_tag_begin3(struct trace_stream *stream, 
                      const char *name, 
                      const char *attr1, const char *value1,
                      const char *attr2, const char *value2,
                      const char *attr3, const char *value3)
{
   trace_dump_write(stream, "<");
   trace_dump_write(stream, name);
   trace_dump_write(stream, " ");
   trace_dump_write(stream, attr1);
   trace_dump_write(stream, "=\'");
   trace_dump_escape(stream, value1);
   trace_dump_write(stream, "\' ");
   trace_dump_write(stream, attr2);
   trace_dump_write(stream, "=\'");
   trace_dump_escape(stream, value2);
   trace_dump_write(stream, "\' ");
   trace_dump_write(stream, attr3);
   trace_dump_write(stream, "=\'");
   trace_dump_escape(stream, value3);
   trace_dump_write(stream, "\'>");
}


static INLINE void
trace_dump_tag_end(struct trace_stream *stream, 
                   const char *name)
{
   trace_dump_write(stream, "</");
   trace_dump_write(stream, name);
   trace_dump_write(stream, ">");
}


void  trace_dump_trace_begin(struct trace_stream *stream,
                             unsigned version)
{
   trace_dump_write(stream, "<?xml version='1.0' encoding='UTF-8'?>\n");
   trace_dump_write(stream, "<?xml-stylesheet type='text/xsl' href='trace.xsl'?>\n");
   trace_dump_writef(stream, "<trace version='%u'>\n", version);
}


void trace_dump_trace_end(struct trace_stream *stream)
{
   trace_dump_write(stream, "</trace>\n");
}

void trace_dump_call_begin(struct trace_stream *stream,
                           const char *klass, const char *method)
{
   trace_dump_indent(stream, 1);
   trace_dump_tag_begin2(stream, "call", "class", klass, "method", method);
   trace_dump_newline(stream);
}

void trace_dump_call_end(struct trace_stream *stream)
{
   trace_dump_indent(stream, 1);
   trace_dump_tag_end(stream, "call");
   trace_dump_newline(stream);
}

void trace_dump_arg_begin(struct trace_stream *stream,
                          const char *name)
{
   trace_dump_indent(stream, 2);
   trace_dump_tag_begin1(stream, "arg", "name", name);
}

void trace_dump_arg_end(struct trace_stream *stream)
{
   trace_dump_tag_end(stream, "arg");
   trace_dump_newline(stream);
}

void trace_dump_ret_begin(struct trace_stream *stream)
{
   trace_dump_indent(stream, 2);
   trace_dump_tag_begin(stream, "ret");
}

void trace_dump_ret_end(struct trace_stream *stream)
{
   trace_dump_tag_end(stream, "ret");
   trace_dump_newline(stream);
}

void trace_dump_bool(struct trace_stream *stream, 
                     int value)
{
   trace_dump_writef(stream, "<bool>%c</bool>", value ? '1' : '0');
}

void trace_dump_int(struct trace_stream *stream, 
                    long int value)
{
   trace_dump_writef(stream, "<int>%li</int>", value);
}

void trace_dump_uint(struct trace_stream *stream, 
                     long unsigned value)
{
   trace_dump_writef(stream, "<uint>%lu</uint>", value);
}

void trace_dump_float(struct trace_stream *stream, 
                      double value)
{
   trace_dump_writef(stream, "<float>%g</float>", value);
}

void trace_dump_bytes(struct trace_stream *stream, 
                      const void *data,
                      long unsigned size)
{
   static const char hex_table[16] = "0123456789ABCDEF";
   const uint8_t *p = data;
   long unsigned i;
   trace_dump_write(stream, "<bytes>");
   for(i = 0; i < size; ++i) {
      uint8_t byte = *p++;
      char hex[2];
      hex[0] = hex_table[byte >> 4];
      hex[1] = hex_table[byte & 0xf];
      trace_stream_write(stream, hex, 2);
   }
   trace_dump_write(stream, "</bytes>");
}

void trace_dump_string(struct trace_stream *stream, 
                       const char *str)
{
   trace_dump_write(stream, "<string>");
   trace_dump_escape(stream, str);
   trace_dump_write(stream, "</string>");
}

void trace_dump_enum(struct trace_stream *stream, 
                     const char *value)
{
   trace_dump_write(stream, "<enum>");
   trace_dump_escape(stream, value);
   trace_dump_write(stream, "</enum>");
}

void trace_dump_array_begin(struct trace_stream *stream)
{
   trace_dump_write(stream, "<array>");
}

void trace_dump_array_end(struct trace_stream *stream)
{
   trace_dump_write(stream, "</array>");
}

void trace_dump_elem_begin(struct trace_stream *stream)
{
   trace_dump_write(stream, "<elem>");
}

void trace_dump_elem_end(struct trace_stream *stream)
{
   trace_dump_write(stream, "</elem>");
}

void trace_dump_struct_begin(struct trace_stream *stream, 
                             const char *name)
{
   trace_dump_writef(stream, "<struct name='%s'>", name);
}

void trace_dump_struct_end(struct trace_stream *stream)
{
   trace_dump_write(stream, "</struct>");
}

void trace_dump_member_begin(struct trace_stream *stream, 
                             const char *name)
{
   trace_dump_writef(stream, "<member name='%s'>", name);
}

void trace_dump_member_end(struct trace_stream *stream)
{
   trace_dump_write(stream, "</member>");
}

void trace_dump_null(struct trace_stream *stream)
{
   trace_dump_write(stream, "<null/>");
}

void trace_dump_ptr(struct trace_stream *stream, 
                    const void *value)
{
   if(value)
      trace_dump_writef(stream, "<ptr>%p</ptr>", value);
   else
      trace_dump_null(stream);
}