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 #ifndef __VTS_SYSFUZZER_LIBMEASUREMENT_GCDA_PARSER_H__
18 #define __VTS_SYSFUZZER_LIBMEASUREMENT_GCDA_PARSER_H__
19 
20 #include <iostream>
21 #include <string>
22 #include <vector>
23 
24 #include "GcdaFile.h"
25 
26 using namespace std;
27 
28 namespace android {
29 namespace vts {
30 
31 // Parses a GCDA file and extracts raw coverage info.
32 class GcdaRawCoverageParser {
33  public:
GcdaRawCoverageParser(const char * filename)34   explicit GcdaRawCoverageParser(const char* filename)
35       : filename_(filename), gcda_file_(new GcdaFile(filename_)) {}
36 
~GcdaRawCoverageParser()37   virtual ~GcdaRawCoverageParser() {}
38 
39   // Parses a given file and returns a vector which contains IDs of raw
40   // coverage measurement units (e.g., basic blocks).
41   vector<unsigned> Parse();
42 
43  protected:
44   // Parses the GCOV magic number.
45   bool ParseMagic();
46 
47   // Parses the GCOV file body.
48   void ParseBody();
49 
50   // Processes tag for functions.
TagFunction(unsigned,unsigned length)51   void TagFunction(unsigned /*tag*/, unsigned length) {
52     /* unsigned long pos = */ gcda_file_->Position();
53 
54     if (length) {
55       gcda_file_->ReadUnsigned();  // ident %u
56       unsigned lineno_checksum = gcda_file_->ReadUnsigned();
57       result.push_back(lineno_checksum);
58       gcda_file_->ReadUnsigned();  // cfg_checksum 0x%08x
59     }
60   }
61 
62   // Processes tag for blocks.
TagBlocks(unsigned,unsigned length)63   void TagBlocks(unsigned /*tag*/, unsigned length) {
64     unsigned n_blocks = GCOV_TAG_BLOCKS_NUM(length);
65     cout << __func__ << ": " << n_blocks << " blocks" << endl;
66   }
67 
68   // Processes tag for arcs.
TagArcs(unsigned,unsigned length)69   void TagArcs(unsigned /*tag*/, unsigned length) {
70     unsigned n_arcs = GCOV_TAG_ARCS_NUM(length);
71     cout << __func__ << ": " << n_arcs << " arcs" << endl;
72   }
73 
74   // Processes tag for lines.
TagLines(unsigned,unsigned)75   void TagLines(unsigned /*tag*/, unsigned /*length*/) {
76     cout << __func__ << endl;
77   }
78 
79  private:
80   // the name of a target file.
81   const string filename_;
82 
83   // global GcovFile data structure.
84   GcdaFile* gcda_file_;
85 
86   // vector containing the parsed, raw coverage data.
87   vector<unsigned> result;
88 };
89 
90 }  // namespace vts
91 }  // namespace android
92 
93 #endif
94