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 **************************************************************************/
33 #include "trace_parser.hpp"
36 #define TRACE_VERBOSE 0
54 bool Parser::open(const char *filename) {
55 file = gzopen(filename, "rb");
60 version = read_uint();
61 if (version > TRACE_VERSION) {
62 std::cerr << "error: unsupported trace format version " << version << "\n";
69 template <typename Iter>
71 deleteAll(Iter begin, Iter end)
73 while (begin != end) {
79 template <typename Container>
81 deleteAll(const Container &c)
83 deleteAll(c.begin(), c.end());
86 void Parser::close(void) {
100 Call *Parser::parse_call(void) {
104 case Trace::EVENT_ENTER:
107 case Trace::EVENT_LEAVE:
108 return parse_leave();
110 std::cerr << "error: unknown event " << c << "\n";
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";
124 * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit.
127 T *lookup(std::vector<T *> &map, size_t index) {
128 if (index >= map.size()) {
129 map.resize(index + 1);
137 void Parser::parse_enter(void) {
138 size_t id = read_uint();
140 FunctionSig *sig = lookup(functions, id);
142 sig = new FunctionSig;
144 sig->name = read_string();
145 sig->num_args = read_uint();
146 const char **arg_names = new const char *[sig->num_args];
147 for (unsigned i = 0; i < sig->num_args; ++i) {
148 arg_names[i] = read_string();
150 sig->arg_names = arg_names;
155 Call *call = new Call(sig);
156 call->no = next_call_no++;
158 if (parse_call_details(call)) {
159 calls.push_back(call);
166 Call *Parser::parse_leave(void) {
167 unsigned call_no = read_uint();
169 for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
170 if ((*it)->no == call_no) {
180 if (parse_call_details(call)) {
189 bool Parser::parse_call_details(Call *call) {
193 case Trace::CALL_END:
195 case Trace::CALL_ARG:
198 case Trace::CALL_RET:
199 call->ret = parse_value();
202 std::cerr << "error: unknown call detail " << c << "\n";
211 void Parser::parse_arg(Call *call) {
212 unsigned index = read_uint();
213 Value *value = parse_value();
214 if (index >= call->args.size()) {
215 call->args.resize(index + 1);
217 call->args[index] = value;
221 Value *Parser::parse_value(void) {
226 case Trace::TYPE_NULL:
229 case Trace::TYPE_FALSE:
230 value = new Bool(false);
232 case Trace::TYPE_TRUE:
233 value = new Bool(true);
235 case Trace::TYPE_SINT:
236 value = parse_sint();
238 case Trace::TYPE_UINT:
239 value = parse_uint();
241 case Trace::TYPE_FLOAT:
242 value = parse_float();
244 case Trace::TYPE_DOUBLE:
245 value = parse_double();
247 case Trace::TYPE_STRING:
248 value = parse_string();
250 case Trace::TYPE_ENUM:
251 value = parse_enum();
253 case Trace::TYPE_BITMASK:
254 value = parse_bitmask();
256 case Trace::TYPE_ARRAY:
257 value = parse_array();
259 case Trace::TYPE_STRUCT:
260 value = parse_struct();
262 case Trace::TYPE_BLOB:
263 value = parse_blob();
265 case Trace::TYPE_OPAQUE:
266 value = parse_opaque();
269 std::cerr << "error: unknown type " << c << "\n";
277 std::cerr << "\tVALUE " << value << "\n";
284 Value *Parser::parse_sint() {
285 return new SInt(-(signed long long)read_uint());
289 Value *Parser::parse_uint() {
290 return new UInt(read_uint());
294 Value *Parser::parse_float() {
296 gzread(file, &value, sizeof value);
297 return new Float(value);
301 Value *Parser::parse_double() {
303 gzread(file, &value, sizeof value);
304 return new Float(value);
308 Value *Parser::parse_string() {
309 return new String(read_string());
313 Value *Parser::parse_enum() {
314 size_t id = read_uint();
315 EnumSig *sig = lookup(enums, id);
319 sig->name = read_string();
320 Value *value = parse_value();
321 sig->value = value->toSInt();
326 return new Enum(sig);
330 Value *Parser::parse_bitmask() {
331 size_t id = read_uint();
332 BitmaskSig *sig = lookup(bitmasks, id);
334 sig = new BitmaskSig;
336 sig->num_flags = read_uint();
337 BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
338 for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
339 it->name = read_string();
340 it->value = read_uint();
341 if (it->value == 0 && it != flags) {
342 std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
350 unsigned long long value = read_uint();
352 return new Bitmask(sig, value);
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();
366 Value *Parser::parse_blob(void) {
367 size_t size = read_uint();
368 Blob *blob = new Blob(size);
370 gzread(file, blob->buf, (unsigned)size);
376 Value *Parser::parse_struct() {
377 size_t id = read_uint();
379 StructSig *sig = lookup(structs, 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();
389 sig->member_names = member_names;
394 Struct *value = new Struct(sig);
396 for (size_t i = 0; i < sig->num_members; ++i) {
397 value->members[i] = parse_value();
404 Value *Parser::parse_opaque() {
405 unsigned long long addr;
407 return new Pointer(addr);
411 const char * Parser::read_string(void) {
412 size_t len = read_uint();
413 char * value = new char[len + 1];
415 gzread(file, value, (unsigned)len);
419 std::cerr << "\tSTRING \"" << value << "\"\n";
425 unsigned long long Parser::read_uint(void) {
426 unsigned long long value = 0;
434 value |= (unsigned long long)(c & 0x7f) << shift;
438 std::cerr << "\tUINT " << value << "\n";
444 inline int Parser::read_byte(void) {
445 int c = gzgetc(file);
448 std::cerr << "\tEOF" << "\n";
450 std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
456 } /* namespace Trace */