]> git.cworth.org Git - apitrace/blob - trace_parser.cpp
Unify Call::Signature into FunctionSig.
[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     FunctionSig *sig = lookup(functions, id);
141     if (!sig) {
142         sig = new FunctionSig;
143         sig->name = read_string();
144         sig->num_args = read_uint();
145         const char **arg_names = new const char *[sig->num_args];
146         for (unsigned i = 0; i < sig->num_args; ++i) {
147             arg_names[i] = read_string();
148         }
149         sig->arg_names = arg_names;
150         functions[id] = sig;
151     }
152     assert(sig);
153
154     Call *call = new Call(sig);
155     call->no = next_call_no++;
156
157     if (parse_call_details(call)) {
158         calls.push_back(call);
159     } else {
160         delete call;
161     }
162 }
163
164
165 Call *Parser::parse_leave(void) {
166     unsigned call_no = read_uint();
167     Call *call = NULL;
168     for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
169         if ((*it)->no == call_no) {
170             call = *it;
171             calls.erase(it);
172             break;
173         }
174     }
175     if (!call) {
176         return NULL;
177     }
178
179     if (parse_call_details(call)) {
180         return call;
181     } else {
182         delete call;
183         return NULL;
184     }
185 }
186
187
188 bool Parser::parse_call_details(Call *call) {
189     do {
190         int c = read_byte();
191         switch(c) {
192         case Trace::CALL_END:
193             return true;
194         case Trace::CALL_ARG:
195             parse_arg(call);
196             break;
197         case Trace::CALL_RET:
198             call->ret = parse_value();
199             break;
200         default:
201             std::cerr << "error: unknown call detail " << c << "\n";
202             exit(1);
203         case -1:
204             return false;
205         }
206     } while(true);
207 }
208
209
210 void Parser::parse_arg(Call *call) {
211     unsigned index = read_uint();
212     Value *value = parse_value();
213     if (index >= call->args.size()) {
214         call->args.resize(index + 1);
215     }
216     call->args[index] = value;
217 }
218
219
220 Value *Parser::parse_value(void) {
221     int c;
222     Value *value;
223     c = read_byte();
224     switch(c) {
225     case Trace::TYPE_NULL:
226         value = new Null;
227         break;
228     case Trace::TYPE_FALSE:
229         value = new Bool(false);
230         break;
231     case Trace::TYPE_TRUE:
232         value = new Bool(true);
233         break;
234     case Trace::TYPE_SINT:
235         value = parse_sint();
236         break;
237     case Trace::TYPE_UINT:
238         value = parse_uint();
239         break;
240     case Trace::TYPE_FLOAT:
241         value = parse_float();
242         break;
243     case Trace::TYPE_DOUBLE:
244         value = parse_double();
245         break;
246     case Trace::TYPE_STRING:
247         value = parse_string();
248         break;
249     case Trace::TYPE_ENUM:
250         value = parse_enum();
251         break;
252     case Trace::TYPE_BITMASK:
253         value = parse_bitmask();
254         break;
255     case Trace::TYPE_ARRAY:
256         value = parse_array();
257         break;
258     case Trace::TYPE_STRUCT:
259         value = parse_struct();
260         break;
261     case Trace::TYPE_BLOB:
262         value = parse_blob();
263         break;
264     case Trace::TYPE_OPAQUE:
265         value = parse_opaque();
266         break;
267     default:
268         std::cerr << "error: unknown type " << c << "\n";
269         exit(1);
270     case -1:
271         value = NULL;
272         break;
273     }
274 #if TRACE_VERBOSE
275     if (value) {
276         std::cerr << "\tVALUE " << value << "\n";
277     }
278 #endif
279     return value;
280 }
281
282
283 Value *Parser::parse_sint() {
284     return new SInt(-(signed long long)read_uint());
285 }
286
287
288 Value *Parser::parse_uint() {
289     return new UInt(read_uint());
290 }
291
292
293 Value *Parser::parse_float() {
294     float value;
295     gzread(file, &value, sizeof value);
296     return new Float(value);
297 }
298
299
300 Value *Parser::parse_double() {
301     double value;
302     gzread(file, &value, sizeof value);
303     return new Float(value);
304 }
305
306
307 Value *Parser::parse_string() {
308     return new String(read_string());
309 }
310
311
312 Value *Parser::parse_enum() {
313     size_t id = read_uint();
314     EnumSig *sig = lookup(enums, id);
315     if (!sig) {
316         sig = new EnumSig;
317         sig->id = id;
318         sig->name = read_string();
319         Value *value = parse_value();
320         sig->value = value->toSInt();
321         delete value;
322         enums[id] = sig;
323     }
324     assert(sig);
325     return new Enum(sig);
326 }
327
328
329 Value *Parser::parse_bitmask() {
330     size_t id = read_uint();
331     BitmaskSig *sig = lookup(bitmasks, id);
332     if (!sig) {
333         size_t count = read_uint();
334         BitmaskVal *values = new BitmaskVal[count];
335         for (BitmaskVal *it = values; it != values + count; ++it) {
336             it->name = read_string();
337             it->value = read_uint();
338             if (it->value == 0 && it != values) {
339                 std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
340             }
341         }
342         sig = new BitmaskSig;
343         sig->id = id;
344         sig->count = count;
345         sig->values = values;
346         bitmasks[id] = sig;
347     }
348     assert(sig);
349
350     unsigned long long value = read_uint();
351
352     return new Bitmask(sig, value);
353 }
354
355
356 Value *Parser::parse_array(void) {
357     size_t len = read_uint();
358     Array *array = new Array(len);
359     for (size_t i = 0; i < len; ++i) {
360         array->values[i] = parse_value();
361     }
362     return array;
363 }
364
365
366 Value *Parser::parse_blob(void) {
367     size_t size = read_uint();
368     Blob *blob = new Blob(size);
369     if (size) {
370         gzread(file, blob->buf, (unsigned)size);
371     }
372     return blob;
373 }
374
375
376 Value *Parser::parse_struct() {
377     size_t id = read_uint();
378
379     StructSig *sig = lookup(structs, id);
380     if (!sig) {
381         sig = new StructSig;
382         sig->id = id;
383         sig->name = read_string();
384         sig->num_members = read_uint();
385         const char **member_names = new const char *[sig->num_members];
386         for (unsigned i = 0; i < sig->num_members; ++i) {
387             member_names[i] = read_string();
388         }
389         sig->member_names = member_names;
390         structs[id] = sig;
391     }
392     assert(sig);
393
394     Struct *value = new Struct(sig);
395
396     for (size_t i = 0; i < sig->num_members; ++i) {
397         value->members[i] = parse_value();
398     }
399
400     return value;
401 }
402
403
404 Value *Parser::parse_opaque() {
405     unsigned long long addr;
406     addr = read_uint();
407     return new Pointer(addr);
408 }
409
410
411 const char * Parser::read_string(void) {
412     size_t len = read_uint();
413     char * value = new char[len + 1];
414     if (len) {
415         gzread(file, value, (unsigned)len);
416     }
417     value[len] = 0;
418 #if TRACE_VERBOSE
419     std::cerr << "\tSTRING \"" << value << "\"\n";
420 #endif
421     return value;
422 }
423
424
425 unsigned long long Parser::read_uint(void) {
426     unsigned long long value = 0;
427     int c;
428     unsigned shift = 0;
429     do {
430         c = gzgetc(file);
431         if (c == -1) {
432             break;
433         }
434         value |= (unsigned long long)(c & 0x7f) << shift;
435         shift += 7;
436     } while(c & 0x80);
437 #if TRACE_VERBOSE
438     std::cerr << "\tUINT " << value << "\n";
439 #endif
440     return value;
441 }
442
443
444 inline int Parser::read_byte(void) {
445     int c = gzgetc(file);
446 #if TRACE_VERBOSE
447     if (c < 0)
448         std::cerr << "\tEOF" << "\n";
449     else
450         std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
451 #endif
452     return c;
453 }
454
455
456 } /* namespace Trace */