]> git.cworth.org Git - apitrace/blob - trace_parser.cpp
66d56a66bbbb2abf2d524665f9f4e88e1f894071
[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 "trace_file.hpp"
32 #include "trace_snappyfile.hpp"
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     assert(!file);
56     if (File::isZLibCompressed(filename)) {
57         file = new ZLibFile;
58     } else {
59         file = new SnappyFile;
60     }
61
62     if (!file->open(filename, File::Read)) {
63         return false;
64     }
65
66     version = read_uint();
67     if (version > TRACE_VERSION) {
68         std::cerr << "error: unsupported trace format version " << version << "\n";
69         return false;
70     }
71
72     return true;
73 }
74
75 template <typename Iter>
76 inline void
77 deleteAll(Iter begin, Iter end)
78 {
79     while (begin != end) {
80         delete *begin;
81         ++begin;
82     }
83 }
84
85 template <typename Container>
86 inline void
87 deleteAll(Container &c)
88 {
89     deleteAll(c.begin(), c.end());
90     c.clear();
91 }
92
93 void Parser::close(void) {
94     if (file) {
95         file->close();
96         delete file;
97         file = NULL;
98     }
99
100     deleteAll(calls);
101     deleteAll(functions);
102     deleteAll(structs);
103     deleteAll(enums);
104     deleteAll(bitmasks);
105 }
106
107
108 Call *Parser::parse_call(void) {
109     do {
110         int c = read_byte();
111         switch(c) {
112         case Trace::EVENT_ENTER:
113             parse_enter();
114             break;
115         case Trace::EVENT_LEAVE:
116             return parse_leave();
117         default:
118             std::cerr << "error: unknown event " << c << "\n";
119             exit(1);
120         case -1:
121             for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
122                 std::cerr << "warning: incomplete call " << (*it)->name() << "\n";
123                 std::cerr << **it << "\n";
124             }
125             return NULL;
126         }
127     } while(true);
128 }
129
130
131 /**
132  * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit.
133  */
134 template<class T>
135 T *lookup(std::vector<T *> &map, size_t index) {
136     if (index >= map.size()) {
137         map.resize(index + 1);
138         return NULL;
139     } else {
140         return map[index];
141     }
142 }
143
144
145 void Parser::parse_enter(void) {
146     size_t id = read_uint();
147
148     FunctionSig *sig = lookup(functions, id);
149     const File::Offset offset = file->currentOffset();
150     bool callWithSig = callWithSignature(offset);
151     if (!sig || callWithSig) {
152         if (!sig) {
153             sig = new FunctionSig;
154             sig->id = id;
155             sig->name = read_string();
156             sig->num_args = read_uint();
157             const char **arg_names = new const char *[sig->num_args];
158             for (unsigned i = 0; i < sig->num_args; ++i) {
159                 arg_names[i] = read_string();
160             }
161             sig->arg_names = arg_names;
162             functions[id] = sig;
163             m_callSigOffsets.insert(offset);
164         } else {
165             /* skip over the signature */
166             read_string(); /* name */
167             int num_args = read_uint();
168             for (unsigned i = 0; i < num_args; ++i) {
169                  read_string(); /*arg_name*/
170             }
171         }
172     }
173     assert(sig);
174
175     Call *call = new Call(sig);
176
177
178     call->no = next_call_no++;
179
180     if (parse_call_details(call)) {
181         calls.push_back(call);
182     } else {
183         delete call;
184     }
185 }
186
187
188 Call *Parser::parse_leave(void) {
189     unsigned call_no = read_uint();
190     Call *call = NULL;
191     for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
192         if ((*it)->no == call_no) {
193             call = *it;
194             calls.erase(it);
195             break;
196         }
197     }
198     if (!call) {
199         return NULL;
200     }
201
202     if (parse_call_details(call)) {
203         return call;
204     } else {
205         delete call;
206         return NULL;
207     }
208 }
209
210
211 bool Parser::parse_call_details(Call *call) {
212     do {
213         int c = read_byte();
214         switch(c) {
215         case Trace::CALL_END:
216             return true;
217         case Trace::CALL_ARG:
218             parse_arg(call);
219             break;
220         case Trace::CALL_RET:
221             call->ret = parse_value();
222             break;
223         default:
224             std::cerr << "error: ("<<call->name()<< ") unknown call detail "
225                       << c << "\n";
226             exit(1);
227         case -1:
228             return false;
229         }
230     } while(true);
231 }
232
233
234 void Parser::parse_arg(Call *call) {
235     unsigned index = read_uint();
236     Value *value = parse_value();
237     if (index >= call->args.size()) {
238         call->args.resize(index + 1);
239     }
240     call->args[index] = value;
241 }
242
243
244 Value *Parser::parse_value(void) {
245     int c;
246     Value *value;
247     c = read_byte();
248     switch(c) {
249     case Trace::TYPE_NULL:
250         value = new Null;
251         break;
252     case Trace::TYPE_FALSE:
253         value = new Bool(false);
254         break;
255     case Trace::TYPE_TRUE:
256         value = new Bool(true);
257         break;
258     case Trace::TYPE_SINT:
259         value = parse_sint();
260         break;
261     case Trace::TYPE_UINT:
262         value = parse_uint();
263         break;
264     case Trace::TYPE_FLOAT:
265         value = parse_float();
266         break;
267     case Trace::TYPE_DOUBLE:
268         value = parse_double();
269         break;
270     case Trace::TYPE_STRING:
271         value = parse_string();
272         break;
273     case Trace::TYPE_ENUM:
274         value = parse_enum();
275         break;
276     case Trace::TYPE_BITMASK:
277         value = parse_bitmask();
278         break;
279     case Trace::TYPE_ARRAY:
280         value = parse_array();
281         break;
282     case Trace::TYPE_STRUCT:
283         value = parse_struct();
284         break;
285     case Trace::TYPE_BLOB:
286         value = parse_blob();
287         break;
288     case Trace::TYPE_OPAQUE:
289         value = parse_opaque();
290         break;
291     default:
292         std::cerr << "error: unknown type " << c << "\n";
293         exit(1);
294     case -1:
295         value = NULL;
296         break;
297     }
298 #if TRACE_VERBOSE
299     if (value) {
300         std::cerr << "\tVALUE " << value << "\n";
301     }
302 #endif
303     return value;
304 }
305
306
307 Value *Parser::parse_sint() {
308     return new SInt(-(signed long long)read_uint());
309 }
310
311
312 Value *Parser::parse_uint() {
313     return new UInt(read_uint());
314 }
315
316
317 Value *Parser::parse_float() {
318     float value;
319     file->read(&value, sizeof value);
320     return new Float(value);
321 }
322
323
324 Value *Parser::parse_double() {
325     double value;
326     file->read(&value, sizeof value);
327     return new Float(value);
328 }
329
330
331 Value *Parser::parse_string() {
332     return new String(read_string());
333 }
334
335
336 Value *Parser::parse_enum() {
337     size_t id = read_uint();
338     EnumSig *sig = lookup(enums, id);
339     const File::Offset offset = file->currentOffset();
340     bool enumWithSig = enumWithSignature(offset);
341     if (!sig || enumWithSig) {
342         if (!sig) {
343             sig = new EnumSig;
344             sig->id = id;
345             sig->name = read_string();
346             Value *value = parse_value();
347             sig->value = value->toSInt();
348             delete value;
349             enums[id] = sig;
350             m_enumSigOffsets.insert(offset);
351         } else {
352             read_string(); /*name*/
353             Value *value = parse_value();
354             delete value;
355         }
356     }
357     assert(sig);
358     return new Enum(sig);
359 }
360
361
362 Value *Parser::parse_bitmask() {
363     size_t id = read_uint();
364     BitmaskSig *sig = lookup(bitmasks, id);
365     const File::Offset offset = file->currentOffset();
366     bool bitmaskWithSig = bitmaskWithSignature(offset);
367     if (!sig || bitmaskWithSig) {
368         if (!sig) {
369             sig = new BitmaskSig;
370             sig->id = id;
371             sig->num_flags = read_uint();
372             BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
373             for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
374                 it->name = read_string();
375                 it->value = read_uint();
376                 if (it->value == 0 && it != flags) {
377                     std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
378                 }
379             }
380             sig->flags = flags;
381             bitmasks[id] = sig;
382             m_bitmaskSigOffsets.insert(offset);
383         } else {
384             int num_flags = read_uint();
385             for (int i = 0; i < num_flags; ++i) {
386                 read_string(); /*name */
387                 read_uint(); /* value */
388             }
389         }
390     }
391     assert(sig);
392
393     unsigned long long value = read_uint();
394
395     return new Bitmask(sig, value);
396 }
397
398
399 Value *Parser::parse_array(void) {
400     size_t len = read_uint();
401     Array *array = new Array(len);
402     for (size_t i = 0; i < len; ++i) {
403         array->values[i] = parse_value();
404     }
405     return array;
406 }
407
408
409 Value *Parser::parse_blob(void) {
410     size_t size = read_uint();
411     Blob *blob = new Blob(size);
412     if (size) {
413         file->read(blob->buf, (unsigned)size);
414     }
415     return blob;
416 }
417
418
419 Value *Parser::parse_struct() {
420     size_t id = read_uint();
421
422     StructSig *sig = lookup(structs, id);
423     const File::Offset offset = file->currentOffset();
424     bool structWithSig = structWithSignature(offset);
425     if (!sig || structWithSig) {
426         if (!sig) {
427             sig = new StructSig;
428             sig->id = id;
429             sig->name = read_string();
430             sig->num_members = read_uint();
431             const char **member_names = new const char *[sig->num_members];
432             for (unsigned i = 0; i < sig->num_members; ++i) {
433                 member_names[i] = read_string();
434             }
435             sig->member_names = member_names;
436             structs[id] = sig;
437             m_structSigOffsets.insert(offset);
438         } else {
439             read_string(); /* name */
440             unsigned num_members = read_uint();
441             for (unsigned i = 0; i < num_members; ++i) {
442                 read_string(); /* member_name */
443             }
444         }
445     }
446     assert(sig);
447
448     Struct *value = new Struct(sig);
449
450     for (size_t i = 0; i < sig->num_members; ++i) {
451         value->members[i] = parse_value();
452     }
453
454     return value;
455 }
456
457
458 Value *Parser::parse_opaque() {
459     unsigned long long addr;
460     addr = read_uint();
461     return new Pointer(addr);
462 }
463
464
465 const char * Parser::read_string(void) {
466     size_t len = read_uint();
467     char * value = new char[len + 1];
468     if (len) {
469         file->read(value, (unsigned)len);
470     }
471     value[len] = 0;
472 #if TRACE_VERBOSE
473     std::cerr << "\tSTRING \"" << value << "\"\n";
474 #endif
475     return value;
476 }
477
478
479 unsigned long long Parser::read_uint(void) {
480     unsigned long long value = 0;
481     int c;
482     unsigned shift = 0;
483     do {
484         c = file->getc();
485         if (c == -1) {
486             break;
487         }
488         value |= (unsigned long long)(c & 0x7f) << shift;
489         shift += 7;
490     } while(c & 0x80);
491 #if TRACE_VERBOSE
492     std::cerr << "\tUINT " << value << "\n";
493 #endif
494     return value;
495 }
496
497
498 inline int Parser::read_byte(void) {
499     int c = file->getc();
500 #if TRACE_VERBOSE
501     if (c < 0)
502         std::cerr << "\tEOF" << "\n";
503     else
504         std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
505 #endif
506     return c;
507 }
508
509
510 inline bool Parser::callWithSignature(const File::Offset &offset) const
511 {
512     return m_callSigOffsets.find(offset) != m_callSigOffsets.end();
513 }
514
515 inline bool Parser::structWithSignature(const File::Offset &offset) const
516 {
517     return m_structSigOffsets.find(offset) != m_structSigOffsets.end();
518 }
519
520 inline bool Parser::enumWithSignature(const File::Offset &offset) const
521 {
522     return m_enumSigOffsets.find(offset) != m_enumSigOffsets.end();
523 }
524
525 inline bool Parser::bitmaskWithSignature(const File::Offset &offset) const
526 {
527     return m_bitmaskSigOffsets.find(offset) != m_bitmaskSigOffsets.end();
528 }
529
530 } /* namespace Trace */