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 **************************************************************************/
32 #include "trace_file.hpp"
33 #include "trace_parser.hpp"
36 #define TRACE_VERBOSE 0
56 bool Parser::open(const char *filename) {
58 file = File::createForRead(filename);
63 version = read_uint();
64 if (version > TRACE_VERSION) {
65 std::cerr << "error: unsupported trace format version " << version << "\n";
72 template <typename Iter>
74 deleteAll(Iter begin, Iter end)
76 while (begin != end) {
82 template <typename Container>
84 deleteAll(Container &c)
86 deleteAll(c.begin(), c.end());
90 void Parser::close(void) {
99 // Delete all signature data. Signatures are mere structures which don't
100 // own their own memory, so we need to destroy all data we created here.
102 for (FunctionMap::iterator it = functions.begin(); it != functions.end(); ++it) {
103 FunctionSigState *sig = *it;
106 for (unsigned arg = 0; arg < sig->num_args; ++arg) {
107 delete [] sig->arg_names[arg];
109 delete [] sig->arg_names;
115 for (StructMap::iterator it = structs.begin(); it != structs.end(); ++it) {
116 StructSigState *sig = *it;
119 for (unsigned member = 0; member < sig->num_members; ++member) {
120 delete [] sig->member_names[member];
122 delete [] sig->member_names;
128 for (EnumMap::iterator it = enums.begin(); it != enums.end(); ++it) {
129 EnumSigState *sig = *it;
131 for (unsigned value = 0; value < sig->num_values; ++value) {
132 delete [] sig->values[value].name;
134 delete [] sig->values;
140 for (BitmaskMap::iterator it = bitmasks.begin(); it != bitmasks.end(); ++it) {
141 BitmaskSigState *sig = *it;
143 for (unsigned flag = 0; flag < sig->num_flags; ++flag) {
144 delete [] sig->flags[flag].name;
146 delete [] sig->flags;
156 void Parser::getBookmark(ParseBookmark &bookmark) {
157 bookmark.offset = file->currentOffset();
158 bookmark.next_call_no = next_call_no;
162 void Parser::setBookmark(const ParseBookmark &bookmark) {
163 file->setCurrentOffset(bookmark.offset);
164 next_call_no = bookmark.next_call_no;
166 // Simply ignore all pending calls
171 Call *Parser::parse_call(Mode mode) {
176 case trace::EVENT_ENTER:
179 case trace::EVENT_LEAVE:
180 call = parse_leave(mode);
181 adjust_call_flags(call);
184 std::cerr << "error: unknown event " << c << "\n";
187 if (!calls.empty()) {
188 call = calls.front();
189 call->flags |= CALL_FLAG_INCOMPLETE;
191 adjust_call_flags(call);
201 * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit.
204 T *lookup(std::vector<T *> &map, size_t index) {
205 if (index >= map.size()) {
206 map.resize(index + 1);
214 Parser::FunctionSigFlags *
215 Parser::parse_function_sig(void) {
216 size_t id = read_uint();
218 FunctionSigState *sig = lookup(functions, id);
221 /* parse the signature */
222 sig = new FunctionSigState;
224 sig->name = read_string();
225 sig->num_args = read_uint();
226 const char **arg_names = new const char *[sig->num_args];
227 for (unsigned i = 0; i < sig->num_args; ++i) {
228 arg_names[i] = read_string();
230 sig->arg_names = arg_names;
231 sig->flags = lookupCallFlags(sig->name);
232 sig->offset = file->currentOffset();
236 * Note down the signature of special functions for future reference.
238 * NOTE: If the number of comparisons increases we should move this to a
239 * separate function and use bisection.
241 if (sig->num_args == 0 &&
242 strcmp(sig->name, "glGetError") == 0) {
246 } else if (file->currentOffset() < sig->offset) {
247 /* skip over the signature */
248 skip_string(); /* name */
249 unsigned num_args = read_uint();
250 for (unsigned i = 0; i < num_args; ++i) {
251 skip_string(); /*arg_name*/
260 StructSig *Parser::parse_struct_sig() {
261 size_t id = read_uint();
263 StructSigState *sig = lookup(structs, id);
266 /* parse the signature */
267 sig = new StructSigState;
269 sig->name = read_string();
270 sig->num_members = read_uint();
271 const char **member_names = new const char *[sig->num_members];
272 for (unsigned i = 0; i < sig->num_members; ++i) {
273 member_names[i] = read_string();
275 sig->member_names = member_names;
276 sig->offset = file->currentOffset();
278 } else if (file->currentOffset() < sig->offset) {
279 /* skip over the signature */
280 skip_string(); /* name */
281 unsigned num_members = read_uint();
282 for (unsigned i = 0; i < num_members; ++i) {
283 skip_string(); /* member_name */
293 * Old enum signatures would cover a single name/value only:
295 * enum_sig = id name value
298 EnumSig *Parser::parse_old_enum_sig() {
299 size_t id = read_uint();
301 EnumSigState *sig = lookup(enums, id);
304 /* parse the signature */
305 sig = new EnumSigState;
308 EnumValue *values = new EnumValue[sig->num_values];
309 values->name = read_string();
310 values->value = read_sint();
311 sig->values = values;
312 sig->offset = file->currentOffset();
314 } else if (file->currentOffset() < sig->offset) {
315 /* skip over the signature */
316 skip_string(); /*name*/
325 EnumSig *Parser::parse_enum_sig() {
326 size_t id = read_uint();
328 EnumSigState *sig = lookup(enums, id);
331 /* parse the signature */
332 sig = new EnumSigState;
334 sig->num_values = read_uint();
335 EnumValue *values = new EnumValue[sig->num_values];
336 for (EnumValue *it = values; it != values + sig->num_values; ++it) {
337 it->name = read_string();
338 it->value = read_sint();
340 sig->values = values;
341 sig->offset = file->currentOffset();
343 } else if (file->currentOffset() < sig->offset) {
344 /* skip over the signature */
345 int num_values = read_uint();
346 for (int i = 0; i < num_values; ++i) {
347 skip_string(); /*name */
348 skip_sint(); /* value */
357 BitmaskSig *Parser::parse_bitmask_sig() {
358 size_t id = read_uint();
360 BitmaskSigState *sig = lookup(bitmasks, id);
363 /* parse the signature */
364 sig = new BitmaskSigState;
366 sig->num_flags = read_uint();
367 BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
368 for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
369 it->name = read_string();
370 it->value = read_uint();
371 if (it->value == 0 && it != flags) {
372 std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
376 sig->offset = file->currentOffset();
378 } else if (file->currentOffset() < sig->offset) {
379 /* skip over the signature */
380 int num_flags = read_uint();
381 for (int i = 0; i < num_flags; ++i) {
382 skip_string(); /*name */
383 skip_uint(); /* value */
392 void Parser::parse_enter(Mode mode) {
393 FunctionSigFlags *sig = parse_function_sig();
395 Call *call = new Call(sig, sig->flags);
397 call->no = next_call_no++;
399 if (parse_call_details(call, mode)) {
400 calls.push_back(call);
407 Call *Parser::parse_leave(Mode mode) {
408 unsigned call_no = read_uint();
410 for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
411 if ((*it)->no == call_no) {
421 if (parse_call_details(call, mode)) {
430 bool Parser::parse_call_details(Call *call, Mode mode) {
434 case trace::CALL_END:
436 case trace::CALL_ARG:
437 parse_arg(call, mode);
439 case trace::CALL_RET:
440 call->ret = parse_value(mode);
443 std::cerr << "error: ("<<call->name()<< ") unknown call detail "
454 * Make adjustments to this particular call flags.
456 * NOTE: This is called per-call so no string comparisons should be done here.
457 * All name comparisons should be done when the signature is parsed instead.
459 void Parser::adjust_call_flags(Call *call) {
460 // Mark glGetError() = GL_NO_ERROR as verbose
461 if (call->sig == glGetErrorSig &&
463 call->ret->toSInt() == 0) {
464 call->flags |= CALL_FLAG_VERBOSE;
468 void Parser::parse_arg(Call *call, Mode mode) {
469 unsigned index = read_uint();
470 Value *value = parse_value(mode);
472 if (index >= call->args.size()) {
473 call->args.resize(index + 1);
475 call->args[index] = value;
480 Value *Parser::parse_value(void) {
485 case trace::TYPE_NULL:
488 case trace::TYPE_FALSE:
489 value = new Bool(false);
491 case trace::TYPE_TRUE:
492 value = new Bool(true);
494 case trace::TYPE_SINT:
495 value = parse_sint();
497 case trace::TYPE_UINT:
498 value = parse_uint();
500 case trace::TYPE_FLOAT:
501 value = parse_float();
503 case trace::TYPE_DOUBLE:
504 value = parse_double();
506 case trace::TYPE_STRING:
507 value = parse_string();
509 case trace::TYPE_ENUM:
510 value = parse_enum();
512 case trace::TYPE_BITMASK:
513 value = parse_bitmask();
515 case trace::TYPE_ARRAY:
516 value = parse_array();
518 case trace::TYPE_STRUCT:
519 value = parse_struct();
521 case trace::TYPE_BLOB:
522 value = parse_blob();
524 case trace::TYPE_OPAQUE:
525 value = parse_opaque();
528 std::cerr << "error: unknown type " << c << "\n";
536 std::cerr << "\tVALUE " << value << "\n";
543 void Parser::scan_value(void) {
546 case trace::TYPE_NULL:
547 case trace::TYPE_FALSE:
548 case trace::TYPE_TRUE:
550 case trace::TYPE_SINT:
553 case trace::TYPE_UINT:
556 case trace::TYPE_FLOAT:
559 case trace::TYPE_DOUBLE:
562 case trace::TYPE_STRING:
565 case trace::TYPE_ENUM:
568 case trace::TYPE_BITMASK:
571 case trace::TYPE_ARRAY:
574 case trace::TYPE_STRUCT:
577 case trace::TYPE_BLOB:
580 case trace::TYPE_OPAQUE:
584 std::cerr << "error: unknown type " << c << "\n";
592 Value *Parser::parse_sint() {
593 return new SInt(-(signed long long)read_uint());
597 void Parser::scan_sint() {
602 Value *Parser::parse_uint() {
603 return new UInt(read_uint());
607 void Parser::scan_uint() {
612 Value *Parser::parse_float() {
614 file->read(&value, sizeof value);
615 return new Float(value);
619 void Parser::scan_float() {
620 file->skip(sizeof(float));
624 Value *Parser::parse_double() {
626 file->read(&value, sizeof value);
627 return new Double(value);
631 void Parser::scan_double() {
632 file->skip(sizeof(double));
636 Value *Parser::parse_string() {
637 return new String(read_string());
641 void Parser::scan_string() {
646 Value *Parser::parse_enum() {
648 signed long long value;
650 sig = parse_enum_sig();
653 sig = parse_old_enum_sig();
654 assert(sig->num_values == 1);
655 value = sig->values->value;
657 return new Enum(sig, value);
661 void Parser::scan_enum() {
666 parse_old_enum_sig();
671 Value *Parser::parse_bitmask() {
672 BitmaskSig *sig = parse_bitmask_sig();
674 unsigned long long value = read_uint();
676 return new Bitmask(sig, value);
680 void Parser::scan_bitmask() {
682 skip_uint(); /* value */
686 Value *Parser::parse_array(void) {
687 size_t len = read_uint();
688 Array *array = new Array(len);
689 for (size_t i = 0; i < len; ++i) {
690 array->values[i] = parse_value();
696 void Parser::scan_array(void) {
697 size_t len = read_uint();
698 for (size_t i = 0; i < len; ++i) {
704 Value *Parser::parse_blob(void) {
705 size_t size = read_uint();
706 Blob *blob = new Blob(size);
708 file->read(blob->buf, (unsigned)size);
714 void Parser::scan_blob(void) {
715 size_t size = read_uint();
722 Value *Parser::parse_struct() {
723 StructSig *sig = parse_struct_sig();
724 Struct *value = new Struct(sig);
726 for (size_t i = 0; i < sig->num_members; ++i) {
727 value->members[i] = parse_value();
734 void Parser::scan_struct() {
735 StructSig *sig = parse_struct_sig();
736 for (size_t i = 0; i < sig->num_members; ++i) {
742 Value *Parser::parse_opaque() {
743 unsigned long long addr;
745 return new Pointer(addr);
749 void Parser::scan_opaque() {
754 const char * Parser::read_string(void) {
755 size_t len = read_uint();
756 char * value = new char[len + 1];
758 file->read(value, (unsigned)len);
762 std::cerr << "\tSTRING \"" << value << "\"\n";
768 void Parser::skip_string(void) {
769 size_t len = read_uint();
775 * For the time being, a signed int is encoded as any other value, but we here parse
776 * it without the extra baggage of the Value class.
779 Parser::read_sint(void) {
783 case trace::TYPE_SINT:
785 case trace::TYPE_UINT:
788 std::cerr << "error: unexpected type " << c << "\n";
796 Parser::skip_sint(void) {
801 unsigned long long Parser::read_uint(void) {
802 unsigned long long value = 0;
810 value |= (unsigned long long)(c & 0x7f) << shift;
814 std::cerr << "\tUINT " << value << "\n";
820 void Parser::skip_uint(void) {
831 inline int Parser::read_byte(void) {
832 int c = file->getc();
835 std::cerr << "\tEOF" << "\n";
837 std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
843 inline void Parser::skip_byte(void) {
848 } /* namespace trace */