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
ParseMagic()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
ParseBody()53 void GcdaRawCoverageParser::ParseBody() {
54 unsigned tags[4];
55 unsigned depth = 0;
56 int cnt = 0;
57 bool found;
58 unsigned base;
59 unsigned position;
60 unsigned tag;
61 unsigned length;
62 unsigned tag_depth;
63 int error;
64 unsigned mask;
65
66 gcda_file_->ReadUnsigned(); // stamp
67 while (1) {
68 position = gcda_file_->Position();
69
70 tag = gcda_file_->ReadUnsigned();
71 if (!tag) break;
72
73 length = gcda_file_->ReadUnsigned();
74 base = gcda_file_->Position();
75 mask = GCOV_TAG_MASK(tag) >> 1;
76 for (tag_depth = 4; mask; mask >>= 8) {
77 if ((mask & 0xff) != 0xff) {
78 cerr << __func__ << ": invalid tag, " << tag << endl;
79 break;
80 }
81 tag_depth--;
82 }
83 found = false;
84
85 if (tag) {
86 if (depth && depth < tag_depth &&
87 !GCOV_TAG_IS_SUBTAG(tags[depth - 1], tag)) {
88 cerr << __func__ << ": incorrectly nested tag, " << tag << endl;
89 }
90 depth = tag_depth;
91 tags[depth - 1] = tag;
92 }
93
94 switch(tag) {
95 case GCOV_TAG_FUNCTION:
96 TagFunction(tag, length);
97 break;
98 case GCOV_TAG_BLOCKS:
99 TagBlocks(tag, length);
100 break;
101 case GCOV_TAG_ARCS:
102 TagArcs(tag, length);
103 break;
104 case GCOV_TAG_LINES:
105 TagLines(tag, length);
106 break;
107 }
108 gcda_file_->Sync(base, length);
109
110 if ((error = gcda_file_->IsError())) {
111 cerr << __func__ << ": I/O error at "
112 << gcda_file_->Position() << endl;
113 break;
114 }
115 }
116 }
117
Parse()118 vector<unsigned> GcdaRawCoverageParser::Parse() {
119 result.clear();
120 if (!gcda_file_->Open()) {
121 cerr << __func__ << " Cannot open a file, " << filename_ << endl;
122 return result;
123 }
124
125 if (!ParseMagic()) return result;
126 ParseBody();
127 gcda_file_->Close();
128 return result;
129 }
130
131 } // namespace vts
132 } // namespace android
133