]> git.cworth.org Git - apitrace/blob - trace_parser.cpp
Unify Struct::Signature into StructSig
[apitrace] / trace_parser.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * Copyright 2010 VMware, Inc.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27
28 #include <assert.h>
29 #include <stdlib.h>
30
31 #include <zlib.h>
32
33 #include "trace_parser.hpp"
34
35
36 #define TRACE_VERBOSE 0
37
38
39 namespace Trace {
40
41
42 Parser::Parser() {
43     file = NULL;
44     next_call_no = 0;
45     version = 0;
46 }
47
48
49 Parser::~Parser() {
50     close();
51 }
52
53
54 bool Parser::open(const char *filename) {
55     file = gzopen(filename, "rb");
56     if (!file) {
57         return false;
58     }
59
60     version = read_uint();
61     if (version > TRACE_VERSION) {
62         std::cerr << "error: unsupported trace format version " << version << "\n";
63         return false;
64     }
65
66     return true;
67 }
68
69 template <typename Iter>
70 inline void
71 deleteAll(Iter begin, Iter end)
72 {
73     while (begin != end) {
74         delete *begin;
75         ++begin;
76     }
77 }
78
79 template <typename Container>
80 inline void
81 deleteAll(const Container &c)
82 {
83     deleteAll(c.begin(), c.end());
84 }
85
86 void Parser::close(void) {
87     if (file) {
88         gzclose(file);
89         file = NULL;
90     }
91
92     deleteAll(calls);
93     deleteAll(functions);
94     deleteAll(structs);
95     deleteAll(enums);
96     deleteAll(bitmasks);
97 }
98
99
100 Call *Parser::parse_call(void) {
101     do {
102         int c = read_byte();
103         switch(c) {
104         case Trace::EVENT_ENTER:
105             parse_enter();
106             break;
107         case Trace::EVENT_LEAVE:
108             return parse_leave();
109         default:
110             std::cerr << "error: unknown event " << c << "\n";
111             exit(1);
112         case -1:
113             for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
114                 std::cerr << "warning: incomplete call " << (*it)->name() << "\n";
115                 std::cerr << **it << "\n";
116             }
117             return NULL;
118         }
119     } while(true);
120 }
121
122
123 /**
124  * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit.
125  */
126 template<class T>
127 T *lookup(std::vector<T *> &map, size_t index) {
128     if (index >= map.size()) {
129         map.resize(index + 1);
130         return NULL;
131     } else {
132         return map[index];
133     }
134 }
135
136
137 void Parser::parse_enter(void) {
138     size_t id = read_uint();
139
140     Call::Signature *sig = lookup(functions, id);
141     if (!sig) {
142         sig = new Call::Signature;
143         sig->name = read_string();
144         unsigned size = read_uint();
145         for (unsigned i = 0; i < size; ++i) {
146             sig->arg_names.push_back(read_string());
147         }
148         functions[id] = sig;
149     }
150     assert(sig);
151
152     Call *call = new Call(sig);
153     call->no = next_call_no++;
154
155     if (parse_call_details(call)) {
156         calls.push_back(call);
157     } else {
158         delete call;
159     }
160 }
161
162
163 Call *Parser::parse_leave(void) {
164     unsigned call_no = read_uint();
165     Call *call = NULL;
166     for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
167         if ((*it)->no == call_no) {
168             call = *it;
169             calls.erase(it);
170             break;
171         }
172     }
173     if (!call) {
174         return NULL;
175     }
176
177     if (parse_call_details(call)) {
178         return call;
179     } else {
180         delete call;
181         return NULL;
182     }
183 }
184
185
186 bool Parser::parse_call_details(Call *call) {
187     do {
188         int c = read_byte();
189         switch(c) {
190         case Trace::CALL_END:
191             return true;
192         case Trace::CALL_ARG:
193             parse_arg(call);
194             break;
195         case Trace::CALL_RET:
196             call->ret = parse_value();
197             break;
198         default:
199             std::cerr << "error: unknown call detail " << c << "\n";
200             exit(1);
201         case -1:
202             return false;
203         }
204     } while(true);
205 }
206
207
208 void Parser::parse_arg(Call *call) {
209     unsigned index = read_uint();
210     Value *value = parse_value();
211     if (index >= call->args.size()) {
212         call->args.resize(index + 1);
213     }
214     call->args[index] = value;
215 }
216
217
218 Value *Parser::parse_value(void) {
219     int c;
220     Value *value;
221     c = read_byte();
222     switch(c) {
223     case Trace::TYPE_NULL:
224         value = new Null;
225         break;
226     case Trace::TYPE_FALSE:
227         value = new Bool(false);
228         break;
229     case Trace::TYPE_TRUE:
230         value = new Bool(true);
231         break;
232     case Trace::TYPE_SINT:
233         value = parse_sint();
234         break;
235     case Trace::TYPE_UINT:
236         value = parse_uint();
237         break;
238     case Trace::TYPE_FLOAT:
239         value = parse_float();
240         break;
241     case Trace::TYPE_DOUBLE:
242         value = parse_double();
243         break;
244     case Trace::TYPE_STRING:
245         value = parse_string();
246         break;
247     case Trace::TYPE_ENUM:
248         value = parse_enum();
249         break;
250     case Trace::TYPE_BITMASK:
251         value = parse_bitmask();
252         break;
253     case Trace::TYPE_ARRAY:
254         value = parse_array();
255         break;
256     case Trace::TYPE_STRUCT:
257         value = parse_struct();
258         break;
259     case Trace::TYPE_BLOB:
260         value = parse_blob();
261         break;
262     case Trace::TYPE_OPAQUE:
263         value = parse_opaque();
264         break;
265     default:
266         std::cerr << "error: unknown type " << c << "\n";
267         exit(1);
268     case -1:
269         value = NULL;
270         break;
271     }
272 #if TRACE_VERBOSE
273     if (value) {
274         std::cerr << "\tVALUE " << value << "\n";
275     }
276 #endif
277     return value;
278 }
279
280
281 Value *Parser::parse_sint() {
282     return new SInt(-(signed long long)read_uint());
283 }
284
285
286 Value *Parser::parse_uint() {
287     return new UInt(read_uint());
288 }
289
290
291 Value *Parser::parse_float() {
292     float value;
293     gzread(file, &value, sizeof value);
294     return new Float(value);
295 }
296
297
298 Value *Parser::parse_double() {
299     double value;
300     gzread(file, &value, sizeof value);
301     return new Float(value);
302 }
303
304
305 Value *Parser::parse_string() {
306     return new String(read_string());
307 }
308
309
310 Value *Parser::parse_enum() {
311     size_t id = read_uint();
312     EnumSig *sig = lookup(enums, id);
313     if (!sig) {
314         sig = new EnumSig;
315         sig->id = id;
316         sig->name = read_string();
317         Value *value = parse_value();
318         sig->value = value->toSInt();
319         delete value;
320         enums[id] = sig;
321     }
322     assert(sig);
323     return new Enum(sig);
324 }
325
326
327 Value *Parser::parse_bitmask() {
328     size_t id = read_uint();
329     BitmaskSig *sig = lookup(bitmasks, id);
330     if (!sig) {
331         size_t count = read_uint();
332         BitmaskVal *values = new BitmaskVal[count];
333         for (BitmaskVal *it = values; it != values + count; ++it) {
334             it->name = read_string();
335             it->value = read_uint();
336             if (it->value == 0 && it != values) {
337                 std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
338             }
339         }
340         sig = new BitmaskSig;
341         sig->id = id;
342         sig->count = count;
343         sig->values = values;
344         bitmasks[id] = sig;
345     }
346     assert(sig);
347
348     unsigned long long value = read_uint();
349
350     return new Bitmask(sig, value);
351 }
352
353
354 Value *Parser::parse_array(void) {
355     size_t len = read_uint();
356     Array *array = new Array(len);
357     for (size_t i = 0; i < len; ++i) {
358         array->values[i] = parse_value();
359     }
360     return array;
361 }
362
363
364 Value *Parser::parse_blob(void) {
365     size_t size = read_uint();
366     Blob *blob = new Blob(size);
367     if (size) {
368         gzread(file, blob->buf, (unsigned)size);
369     }
370     return blob;
371 }
372
373
374 Value *Parser::parse_struct() {
375     size_t id = read_uint();
376
377     StructSig *sig = lookup(structs, id);
378     if (!sig) {
379         sig = new StructSig;
380         sig->id = id;
381         sig->name = read_string();
382         sig->num_members = read_uint();
383         const char **member_names = new const char *[sig->num_members];
384         for (unsigned i = 0; i < sig->num_members; ++i) {
385             member_names[i] = read_string();
386         }
387         sig->member_names = member_names;
388         structs[id] = sig;
389     }
390     assert(sig);
391
392     Struct *value = new Struct(sig);
393
394     for (size_t i = 0; i < sig->num_members; ++i) {
395         value->members[i] = parse_value();
396     }
397
398     return value;
399 }
400
401
402 Value *Parser::parse_opaque() {
403     unsigned long long addr;
404     addr = read_uint();
405     return new Pointer(addr);
406 }
407
408
409 const char * Parser::read_string(void) {
410     size_t len = read_uint();
411     char * value = new char[len + 1];
412     if (len) {
413         gzread(file, value, (unsigned)len);
414     }
415     value[len] = 0;
416 #if TRACE_VERBOSE
417     std::cerr << "\tSTRING \"" << value << "\"\n";
418 #endif
419     return value;
420 }
421
422
423 unsigned long long Parser::read_uint(void) {
424     unsigned long long value = 0;
425     int c;
426     unsigned shift = 0;
427     do {
428         c = gzgetc(file);
429         if (c == -1) {
430             break;
431         }
432         value |= (unsigned long long)(c & 0x7f) << shift;
433         shift += 7;
434     } while(c & 0x80);
435 #if TRACE_VERBOSE
436     std::cerr << "\tUINT " << value << "\n";
437 #endif
438     return value;
439 }
440
441
442 inline int Parser::read_byte(void) {
443     int c = gzgetc(file);
444 #if TRACE_VERBOSE
445     if (c < 0)
446         std::cerr << "\tEOF" << "\n";
447     else
448         std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
449 #endif
450     return c;
451 }
452
453
454 } /* namespace Trace */