1 /* 2 * Copyright 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "GcdaParser.h" 18 19 #include <iostream> 20 #include <vector> 21 22 #include "GcdaFile.h" 23 24 using namespace std; 25 26 namespace android { 27 namespace vts { 28 29 bool GcdaRawCoverageParser::ParseMagic() { 30 unsigned magic = gcda_file_->ReadUnsigned(); 31 unsigned version; 32 const char* type = NULL; 33 int endianness = 0; 34 char m[4], v[4]; 35 36 if ((endianness = gcda_file_->Magic(magic, GCOV_DATA_MAGIC))) { 37 type = "data"; 38 } else { 39 cout << __func__ << ": not a GCOV file, " << filename_ << endl; 40 gcda_file_->Close(); 41 return false; 42 } 43 version = gcda_file_->ReadUnsigned(); 44 GCOV_UNSIGNED2STRING(v, version); 45 GCOV_UNSIGNED2STRING(m, magic); 46 if (version != GCOV_VERSION) { 47 char e[4]; 48 GCOV_UNSIGNED2STRING(e, GCOV_VERSION); 49 } 50 return true; 51 } 52 53 void GcdaRawCoverageParser::ParseBody() { 54 unsigned tags[4]; 55 unsigned depth = 0; 56 bool found; 57 unsigned base; 58 unsigned position; 59 unsigned tag; 60 unsigned length; 61 unsigned tag_depth; 62 int error; 63 unsigned mask; 64 65 gcda_file_->ReadUnsigned(); // stamp 66 while (1) { 67 position = gcda_file_->Position(); 68 69 tag = gcda_file_->ReadUnsigned(); 70 if (!tag) break; 71 72 length = gcda_file_->ReadUnsigned(); 73 base = gcda_file_->Position(); 74 mask = GCOV_TAG_MASK(tag) >> 1; 75 for (tag_depth = 4; mask; mask >>= 8) { 76 if ((mask & 0xff) != 0xff) { 77 cerr << __func__ << ": invalid tag, " << tag << endl; 78 break; 79 } 80 tag_depth--; 81 } 82 found = false; 83 84 if (tag) { 85 if (depth && depth < tag_depth && 86 !GCOV_TAG_IS_SUBTAG(tags[depth - 1], tag)) { 87 cerr << __func__ << ": incorrectly nested tag, " << tag << endl; 88 } 89 depth = tag_depth; 90 tags[depth - 1] = tag; 91 } 92 93 switch(tag) { 94 case GCOV_TAG_FUNCTION: 95 TagFunction(tag, length); 96 break; 97 case GCOV_TAG_BLOCKS: 98 TagBlocks(tag, length); 99 break; 100 case GCOV_TAG_ARCS: 101 TagArcs(tag, length); 102 break; 103 case GCOV_TAG_LINES: 104 TagLines(tag, length); 105 break; 106 } 107 gcda_file_->Sync(base, length); 108 109 if ((error = gcda_file_->IsError())) { 110 cerr << __func__ << ": I/O error at " 111 << gcda_file_->Position() << endl; 112 break; 113 } 114 } 115 } 116 117 vector<unsigned> GcdaRawCoverageParser::Parse() { 118 result.clear(); 119 if (!gcda_file_->Open()) { 120 cerr << __func__ << " Cannot open a file, " << filename_ << endl; 121 return result; 122 } 123 124 if (!ParseMagic()) return result; 125 ParseBody(); 126 gcda_file_->Close(); 127 return result; 128 } 129 130 } // namespace vts 131 } // namespace android 132