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_FILE_H__
18 #define __VTS_SYSFUZZER_LIBMEASUREMENT_GCDA_FILE_H__
19 
20 #include <string>
21 
22 #include "gcov_basic_io.h"
23 
24 using namespace std;
25 
26 namespace android {
27 namespace vts {
28 
29 // Basic I/O methods for a GCOV file.
30 class GcdaFile {
31  public:
GcdaFile(const string & filename)32   explicit GcdaFile(const string& filename) : filename_(filename) {}
~GcdaFile()33   virtual ~GcdaFile() {};
34 
35   // Opens a file.
36   bool Open();
37 
38   // Closes a file and returns any existing error code.
39   int Close();
40 
41   // Synchronizes to the given base and length.
42   void Sync(unsigned base, unsigned length);
43 
44   // Reads a string array where the maximum number of strings is also specified.
45   unsigned ReadStringArray(char** string_array, unsigned num_strings);
46 
47   // Reads a string.
48   const char* ReadString();
49 
50   // Reads an unsigned integer.
51   unsigned ReadUnsigned();
52 
53   // Reads 'words' number of words.
54   const unsigned* ReadWords(unsigned words);
55 
56   // Reads a counter.
57   gcov_type ReadCounter();
58 
59   // Write a block using 'size'.
60   void WriteBlock(unsigned size);
61 
62   // Allocates memory for length.
63   void Allocate(unsigned length);
64 
65   // Processes the magic tag.
66   int Magic(unsigned magic, unsigned expected);
67 
68   // Returns the current position in the file.
Position()69   inline unsigned Position() const {
70     return gcov_var_.start + gcov_var_.offset;
71   }
72 
73   // Returns non-zero error code if there's an error.
IsError()74   inline int IsError() const {
75     return gcov_var_.file ? gcov_var_.error : 1;
76   }
77 
78  protected:
FromFile(unsigned value)79   inline unsigned FromFile(unsigned value) {
80     if (gcov_var_.endian) {
81       value = (value >> 16) | (value << 16);
82       value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
83     }
84     return value;
85   }
86 
87  private:
88   // The GCOV var data structure for an opened file.
89   struct gcov_var_t gcov_var_;
90   const string& filename_;
91 };
92 
93 }  // namespace vts
94 }  // namespace android
95 
96 #endif
97