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
57 bool Parser::open(const char *filename) {
59 file = File::createForRead(filename);
64 version = read_uint();
65 if (version > TRACE_VERSION) {
66 std::cerr << "error: unsupported trace format version " << version << "\n";
74 template <typename Iter>
76 deleteAll(Iter begin, Iter end)
78 while (begin != end) {
84 template <typename Container>
86 deleteAll(Container &c)
88 deleteAll(c.begin(), c.end());
92 void Parser::close(void) {
101 // Delete all signature data. Signatures are mere structures which don't
102 // own their own memory, so we need to destroy all data we created here.
104 for (FunctionMap::iterator it = functions.begin(); it != functions.end(); ++it) {
105 FunctionSigState *sig = *it;
108 for (unsigned arg = 0; arg < sig->num_args; ++arg) {
109 delete [] sig->arg_names[arg];
111 delete [] sig->arg_names;
117 for (StructMap::iterator it = structs.begin(); it != structs.end(); ++it) {
118 StructSigState *sig = *it;
121 for (unsigned member = 0; member < sig->num_members; ++member) {
122 delete [] sig->member_names[member];
124 delete [] sig->member_names;
130 for (EnumMap::iterator it = enums.begin(); it != enums.end(); ++it) {
131 EnumSigState *sig = *it;
133 for (unsigned value = 0; value < sig->num_values; ++value) {
134 delete [] sig->values[value].name;
136 delete [] sig->values;
142 for (BitmaskMap::iterator it = bitmasks.begin(); it != bitmasks.end(); ++it) {
143 BitmaskSigState *sig = *it;
145 for (unsigned flag = 0; flag < sig->num_flags; ++flag) {
146 delete [] sig->flags[flag].name;
148 delete [] sig->flags;
158 void Parser::getBookmark(ParseBookmark &bookmark) {
159 bookmark.offset = file->currentOffset();
160 bookmark.next_call_no = next_call_no;
164 void Parser::setBookmark(const ParseBookmark &bookmark) {
165 file->setCurrentOffset(bookmark.offset);
166 next_call_no = bookmark.next_call_no;
168 // Simply ignore all pending calls
173 Call *Parser::parse_call(Mode mode) {
178 case trace::EVENT_ENTER:
181 case trace::EVENT_LEAVE:
182 call = parse_leave(mode);
183 adjust_call_flags(call);
186 std::cerr << "error: unknown event " << c << "\n";
189 if (!calls.empty()) {
190 call = calls.front();
191 call->flags |= CALL_FLAG_INCOMPLETE;
193 adjust_call_flags(call);
203 * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit.
206 T *lookup(std::vector<T *> &map, size_t index) {
207 if (index >= map.size()) {
208 map.resize(index + 1);
216 Parser::FunctionSigFlags *
217 Parser::parse_function_sig(void) {
218 size_t id = read_uint();
220 FunctionSigState *sig = lookup(functions, id);
223 /* parse the signature */
224 sig = new FunctionSigState;
226 sig->name = read_string();
227 sig->num_args = read_uint();
228 const char **arg_names = new const char *[sig->num_args];
229 for (unsigned i = 0; i < sig->num_args; ++i) {
230 arg_names[i] = read_string();
232 sig->arg_names = arg_names;
233 sig->flags = lookupCallFlags(sig->name);
234 sig->offset = file->currentOffset();
238 * Try to autodetect the API.
240 * XXX: Ideally we would allow to mix multiple APIs in a single trace,
241 * but as it stands today, retrace is done separately for each API.
243 if (api == API_UNKNOWN) {
244 const char *n = sig->name;
245 if ((n[0] == 'g' && n[1] == 'l' && n[2] == 'X') || // glX*
246 (n[0] == 'w' && n[1] == 'g' && n[2] == 'l' && n[3] >= 'A' && n[3] <= 'Z') || // wgl[A-Z]*
247 (n[0] == 'C' && n[1] == 'G' && n[2] == 'L')) { // CGL*
249 } else if (n[0] == 'e' && n[1] == 'g' && n[2] == 'l' && n[3] >= 'A' && n[3] <= 'Z') { // egl[A-Z]*
250 api = trace::API_EGL;
251 } else if ((n[0] == 'D' &&
252 ((n[1] == 'i' && n[2] == 'r' && n[3] == 'e' && n[4] == 'c' && n[5] == 't') || // Direct*
253 (n[1] == '3' && n[2] == 'D'))) || // D3D*
254 (n[0] == 'C' && n[1] == 'r' && n[2] == 'e' && n[3] == 'a' && n[4] == 't' && n[5] == 'e')) { // Create*
262 * Note down the signature of special functions for future reference.
264 * NOTE: If the number of comparisons increases we should move this to a
265 * separate function and use bisection.
267 if (sig->num_args == 0 &&
268 strcmp(sig->name, "glGetError") == 0) {
272 } else if (file->currentOffset() < sig->offset) {
273 /* skip over the signature */
274 skip_string(); /* name */
275 unsigned num_args = read_uint();
276 for (unsigned i = 0; i < num_args; ++i) {
277 skip_string(); /*arg_name*/
286 StructSig *Parser::parse_struct_sig() {
287 size_t id = read_uint();
289 StructSigState *sig = lookup(structs, id);
292 /* parse the signature */
293 sig = new StructSigState;
295 sig->name = read_string();
296 sig->num_members = read_uint();
297 const char **member_names = new const char *[sig->num_members];
298 for (unsigned i = 0; i < sig->num_members; ++i) {
299 member_names[i] = read_string();
301 sig->member_names = member_names;
302 sig->offset = file->currentOffset();
304 } else if (file->currentOffset() < sig->offset) {
305 /* skip over the signature */
306 skip_string(); /* name */
307 unsigned num_members = read_uint();
308 for (unsigned i = 0; i < num_members; ++i) {
309 skip_string(); /* member_name */
319 * Old enum signatures would cover a single name/value only:
321 * enum_sig = id name value
324 EnumSig *Parser::parse_old_enum_sig() {
325 size_t id = read_uint();
327 EnumSigState *sig = lookup(enums, id);
330 /* parse the signature */
331 sig = new EnumSigState;
334 EnumValue *values = new EnumValue[sig->num_values];
335 values->name = read_string();
336 values->value = read_sint();
337 sig->values = values;
338 sig->offset = file->currentOffset();
340 } else if (file->currentOffset() < sig->offset) {
341 /* skip over the signature */
342 skip_string(); /*name*/
351 EnumSig *Parser::parse_enum_sig() {
352 size_t id = read_uint();
354 EnumSigState *sig = lookup(enums, id);
357 /* parse the signature */
358 sig = new EnumSigState;
360 sig->num_values = read_uint();
361 EnumValue *values = new EnumValue[sig->num_values];
362 for (EnumValue *it = values; it != values + sig->num_values; ++it) {
363 it->name = read_string();
364 it->value = read_sint();
366 sig->values = values;
367 sig->offset = file->currentOffset();
369 } else if (file->currentOffset() < sig->offset) {
370 /* skip over the signature */
371 int num_values = read_uint();
372 for (int i = 0; i < num_values; ++i) {
373 skip_string(); /*name */
374 skip_sint(); /* value */
383 BitmaskSig *Parser::parse_bitmask_sig() {
384 size_t id = read_uint();
386 BitmaskSigState *sig = lookup(bitmasks, id);
389 /* parse the signature */
390 sig = new BitmaskSigState;
392 sig->num_flags = read_uint();
393 BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
394 for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
395 it->name = read_string();
396 it->value = read_uint();
397 if (it->value == 0 && it != flags) {
398 std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
402 sig->offset = file->currentOffset();
404 } else if (file->currentOffset() < sig->offset) {
405 /* skip over the signature */
406 int num_flags = read_uint();
407 for (int i = 0; i < num_flags; ++i) {
408 skip_string(); /*name */
409 skip_uint(); /* value */
418 void Parser::parse_enter(Mode mode) {
422 thread_id = read_uint();
427 FunctionSigFlags *sig = parse_function_sig();
429 Call *call = new Call(sig, sig->flags, thread_id);
431 call->no = next_call_no++;
433 if (parse_call_details(call, mode)) {
434 calls.push_back(call);
441 Call *Parser::parse_leave(Mode mode) {
442 unsigned call_no = read_uint();
444 for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
445 if ((*it)->no == call_no) {
455 if (parse_call_details(call, mode)) {
464 bool Parser::parse_call_details(Call *call, Mode mode) {
468 case trace::CALL_END:
470 case trace::CALL_ARG:
471 parse_arg(call, mode);
473 case trace::CALL_RET:
474 call->ret = parse_value(mode);
477 std::cerr << "error: ("<<call->name()<< ") unknown call detail "
488 * Make adjustments to this particular call flags.
490 * NOTE: This is called per-call so no string comparisons should be done here.
491 * All name comparisons should be done when the signature is parsed instead.
493 void Parser::adjust_call_flags(Call *call) {
494 // Mark glGetError() = GL_NO_ERROR as verbose
495 if (call->sig == glGetErrorSig &&
497 call->ret->toSInt() == 0) {
498 call->flags |= CALL_FLAG_VERBOSE;
502 void Parser::parse_arg(Call *call, Mode mode) {
503 unsigned index = read_uint();
504 Value *value = parse_value(mode);
506 if (index >= call->args.size()) {
507 call->args.resize(index + 1);
509 call->args[index].value = value;
514 Value *Parser::parse_value(void) {
519 case trace::TYPE_NULL:
522 case trace::TYPE_FALSE:
523 value = new Bool(false);
525 case trace::TYPE_TRUE:
526 value = new Bool(true);
528 case trace::TYPE_SINT:
529 value = parse_sint();
531 case trace::TYPE_UINT:
532 value = parse_uint();
534 case trace::TYPE_FLOAT:
535 value = parse_float();
537 case trace::TYPE_DOUBLE:
538 value = parse_double();
540 case trace::TYPE_STRING:
541 value = parse_string();
543 case trace::TYPE_ENUM:
544 value = parse_enum();
546 case trace::TYPE_BITMASK:
547 value = parse_bitmask();
549 case trace::TYPE_ARRAY:
550 value = parse_array();
552 case trace::TYPE_STRUCT:
553 value = parse_struct();
555 case trace::TYPE_BLOB:
556 value = parse_blob();
558 case trace::TYPE_OPAQUE:
559 value = parse_opaque();
561 case trace::TYPE_REPR:
562 value = parse_repr();
565 std::cerr << "error: unknown type " << c << "\n";
573 std::cerr << "\tVALUE " << value << "\n";
580 void Parser::scan_value(void) {
583 case trace::TYPE_NULL:
584 case trace::TYPE_FALSE:
585 case trace::TYPE_TRUE:
587 case trace::TYPE_SINT:
590 case trace::TYPE_UINT:
593 case trace::TYPE_FLOAT:
596 case trace::TYPE_DOUBLE:
599 case trace::TYPE_STRING:
602 case trace::TYPE_ENUM:
605 case trace::TYPE_BITMASK:
608 case trace::TYPE_ARRAY:
611 case trace::TYPE_STRUCT:
614 case trace::TYPE_BLOB:
617 case trace::TYPE_OPAQUE:
620 case trace::TYPE_REPR:
624 std::cerr << "error: unknown type " << c << "\n";
632 Value *Parser::parse_sint() {
633 return new SInt(-(signed long long)read_uint());
637 void Parser::scan_sint() {
642 Value *Parser::parse_uint() {
643 return new UInt(read_uint());
647 void Parser::scan_uint() {
652 Value *Parser::parse_float() {
654 file->read(&value, sizeof value);
655 return new Float(value);
659 void Parser::scan_float() {
660 file->skip(sizeof(float));
664 Value *Parser::parse_double() {
666 file->read(&value, sizeof value);
667 return new Double(value);
671 void Parser::scan_double() {
672 file->skip(sizeof(double));
676 Value *Parser::parse_string() {
677 return new String(read_string());
681 void Parser::scan_string() {
686 Value *Parser::parse_enum() {
688 signed long long value;
690 sig = parse_enum_sig();
693 sig = parse_old_enum_sig();
694 assert(sig->num_values == 1);
695 value = sig->values->value;
697 return new Enum(sig, value);
701 void Parser::scan_enum() {
706 parse_old_enum_sig();
711 Value *Parser::parse_bitmask() {
712 BitmaskSig *sig = parse_bitmask_sig();
714 unsigned long long value = read_uint();
716 return new Bitmask(sig, value);
720 void Parser::scan_bitmask() {
722 skip_uint(); /* value */
726 Value *Parser::parse_array(void) {
727 size_t len = read_uint();
728 Array *array = new Array(len);
729 for (size_t i = 0; i < len; ++i) {
730 array->values[i] = parse_value();
736 void Parser::scan_array(void) {
737 size_t len = read_uint();
738 for (size_t i = 0; i < len; ++i) {
744 Value *Parser::parse_blob(void) {
745 size_t size = read_uint();
746 Blob *blob = new Blob(size);
748 file->read(blob->buf, size);
754 void Parser::scan_blob(void) {
755 size_t size = read_uint();
762 Value *Parser::parse_struct() {
763 StructSig *sig = parse_struct_sig();
764 Struct *value = new Struct(sig);
766 for (size_t i = 0; i < sig->num_members; ++i) {
767 value->members[i] = parse_value();
774 void Parser::scan_struct() {
775 StructSig *sig = parse_struct_sig();
776 for (size_t i = 0; i < sig->num_members; ++i) {
782 Value *Parser::parse_opaque() {
783 unsigned long long addr;
785 return new Pointer(addr);
789 void Parser::scan_opaque() {
794 Value *Parser::parse_repr() {
795 Value *humanValue = parse_value();
796 Value *machineValue = parse_value();
797 return new Repr(humanValue, machineValue);
801 void Parser::scan_repr() {
807 const char * Parser::read_string(void) {
808 size_t len = read_uint();
809 char * value = new char[len + 1];
811 file->read(value, len);
815 std::cerr << "\tSTRING \"" << value << "\"\n";
821 void Parser::skip_string(void) {
822 size_t len = read_uint();
828 * For the time being, a signed int is encoded as any other value, but we here parse
829 * it without the extra baggage of the Value class.
832 Parser::read_sint(void) {
836 case trace::TYPE_SINT:
837 return -(signed long long)read_uint();
838 case trace::TYPE_UINT:
841 std::cerr << "error: unexpected type " << c << "\n";
849 Parser::skip_sint(void) {
854 unsigned long long Parser::read_uint(void) {
855 unsigned long long value = 0;
863 value |= (unsigned long long)(c & 0x7f) << shift;
867 std::cerr << "\tUINT " << value << "\n";
873 void Parser::skip_uint(void) {
884 inline int Parser::read_byte(void) {
885 int c = file->getc();
888 std::cerr << "\tEOF" << "\n";
890 std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
896 inline void Parser::skip_byte(void) {
901 } /* namespace trace */