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