1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
4 * Copyright 2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
31 #include "trace_file.hpp"
32 #include "trace_snappyfile.hpp"
33 #include "trace_parser.hpp"
36 #define TRACE_VERBOSE 0
54 bool Parser::open(const char *filename) {
56 if (File::isZLibCompressed(filename)) {
59 file = new SnappyFile;
62 if (!file->open(filename, File::Read)) {
66 version = read_uint();
67 if (version > TRACE_VERSION) {
68 std::cerr << "error: unsupported trace format version " << version << "\n";
75 template <typename Iter>
77 deleteAll(Iter begin, Iter end)
79 while (begin != end) {
85 template <typename Container>
87 deleteAll(Container &c)
89 deleteAll(c.begin(), c.end());
93 void Parser::close(void) {
101 deleteAll(functions);
108 Call *Parser::parse_call(void) {
112 case Trace::EVENT_ENTER:
115 case Trace::EVENT_LEAVE:
116 return parse_leave();
118 std::cerr << "error: unknown event " << c << "\n";
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";
132 * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit.
135 T *lookup(std::vector<T *> &map, size_t index) {
136 if (index >= map.size()) {
137 map.resize(index + 1);
145 void Parser::parse_enter(void) {
146 size_t id = read_uint();
148 FunctionSig *sig = lookup(functions, id);
149 const File::Offset offset = file->currentOffset();
150 bool callWithSig = callWithSignature(offset);
151 if (!sig || callWithSig) {
153 sig = new FunctionSig;
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();
161 sig->arg_names = arg_names;
163 m_callSigOffsets.insert(offset);
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*/
175 Call *call = new Call(sig);
178 call->no = next_call_no++;
180 if (parse_call_details(call)) {
181 calls.push_back(call);
188 Call *Parser::parse_leave(void) {
189 unsigned call_no = read_uint();
191 for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
192 if ((*it)->no == call_no) {
202 if (parse_call_details(call)) {
211 bool Parser::parse_call_details(Call *call) {
215 case Trace::CALL_END:
217 case Trace::CALL_ARG:
220 case Trace::CALL_RET:
221 call->ret = parse_value();
224 std::cerr << "error: ("<<call->name()<< ") unknown call detail "
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);
240 call->args[index] = value;
244 Value *Parser::parse_value(void) {
249 case Trace::TYPE_NULL:
252 case Trace::TYPE_FALSE:
253 value = new Bool(false);
255 case Trace::TYPE_TRUE:
256 value = new Bool(true);
258 case Trace::TYPE_SINT:
259 value = parse_sint();
261 case Trace::TYPE_UINT:
262 value = parse_uint();
264 case Trace::TYPE_FLOAT:
265 value = parse_float();
267 case Trace::TYPE_DOUBLE:
268 value = parse_double();
270 case Trace::TYPE_STRING:
271 value = parse_string();
273 case Trace::TYPE_ENUM:
274 value = parse_enum();
276 case Trace::TYPE_BITMASK:
277 value = parse_bitmask();
279 case Trace::TYPE_ARRAY:
280 value = parse_array();
282 case Trace::TYPE_STRUCT:
283 value = parse_struct();
285 case Trace::TYPE_BLOB:
286 value = parse_blob();
288 case Trace::TYPE_OPAQUE:
289 value = parse_opaque();
292 std::cerr << "error: unknown type " << c << "\n";
300 std::cerr << "\tVALUE " << value << "\n";
307 Value *Parser::parse_sint() {
308 return new SInt(-(signed long long)read_uint());
312 Value *Parser::parse_uint() {
313 return new UInt(read_uint());
317 Value *Parser::parse_float() {
319 file->read(&value, sizeof value);
320 return new Float(value);
324 Value *Parser::parse_double() {
326 file->read(&value, sizeof value);
327 return new Float(value);
331 Value *Parser::parse_string() {
332 return new String(read_string());
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) {
345 sig->name = read_string();
346 Value *value = parse_value();
347 sig->value = value->toSInt();
350 m_enumSigOffsets.insert(offset);
352 read_string(); /*name*/
353 Value *value = parse_value();
358 return new Enum(sig);
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) {
369 sig = new BitmaskSig;
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";
382 m_bitmaskSigOffsets.insert(offset);
384 int num_flags = read_uint();
385 for (int i = 0; i < num_flags; ++i) {
386 read_string(); /*name */
387 read_uint(); /* value */
393 unsigned long long value = read_uint();
395 return new Bitmask(sig, value);
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();
409 Value *Parser::parse_blob(void) {
410 size_t size = read_uint();
411 Blob *blob = new Blob(size);
413 file->read(blob->buf, (unsigned)size);
419 Value *Parser::parse_struct() {
420 size_t id = read_uint();
422 StructSig *sig = lookup(structs, id);
423 const File::Offset offset = file->currentOffset();
424 bool structWithSig = structWithSignature(offset);
425 if (!sig || structWithSig) {
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();
435 sig->member_names = member_names;
437 m_structSigOffsets.insert(offset);
439 read_string(); /* name */
440 unsigned num_members = read_uint();
441 for (unsigned i = 0; i < num_members; ++i) {
442 read_string(); /* member_name */
448 Struct *value = new Struct(sig);
450 for (size_t i = 0; i < sig->num_members; ++i) {
451 value->members[i] = parse_value();
458 Value *Parser::parse_opaque() {
459 unsigned long long addr;
461 return new Pointer(addr);
465 const char * Parser::read_string(void) {
466 size_t len = read_uint();
467 char * value = new char[len + 1];
469 file->read(value, (unsigned)len);
473 std::cerr << "\tSTRING \"" << value << "\"\n";
479 unsigned long long Parser::read_uint(void) {
480 unsigned long long value = 0;
488 value |= (unsigned long long)(c & 0x7f) << shift;
492 std::cerr << "\tUINT " << value << "\n";
498 inline int Parser::read_byte(void) {
499 int c = file->getc();
502 std::cerr << "\tEOF" << "\n";
504 std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
510 inline bool Parser::callWithSignature(const File::Offset &offset) const
512 return m_callSigOffsets.find(offset) != m_callSigOffsets.end();
515 inline bool Parser::structWithSignature(const File::Offset &offset) const
517 return m_structSigOffsets.find(offset) != m_structSigOffsets.end();
520 inline bool Parser::enumWithSignature(const File::Offset &offset) const
522 return m_enumSigOffsets.find(offset) != m_enumSigOffsets.end();
525 inline bool Parser::bitmaskWithSignature(const File::Offset &offset) const
527 return m_bitmaskSigOffsets.find(offset) != m_bitmaskSigOffsets.end();
530 } /* namespace Trace */