Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 1 | //=-- CoverageMappingReader.cpp - Code coverage mapping reader ----*- C++ -*-=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for reading coverage mapping data for |
| 11 | // instrumentation based coverage. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ProfileData/CoverageMappingReader.h" |
| 16 | #include "llvm/ADT/DenseSet.h" |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 17 | #include "llvm/Object/MachOUniversal.h" |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ObjectFile.h" |
Justin Bogner | f584649 | 2014-09-20 15:31:51 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Endian.h" |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 21 | #include "llvm/Support/LEB128.h" |
Justin Bogner | d49d8ee | 2015-06-05 01:23:42 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | using namespace coverage; |
| 27 | using namespace object; |
| 28 | |
Justin Bogner | f584649 | 2014-09-20 15:31:51 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "coverage-mapping" |
| 30 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 31 | void CoverageMappingIterator::increment() { |
| 32 | // Check if all the records were read or if an error occurred while reading |
| 33 | // the next record. |
| 34 | if (Reader->readNextRecord(Record)) |
| 35 | *this = CoverageMappingIterator(); |
| 36 | } |
| 37 | |
| 38 | std::error_code RawCoverageReader::readULEB128(uint64_t &Result) { |
| 39 | if (Data.size() < 1) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 40 | return coveragemap_error::truncated; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 41 | unsigned N = 0; |
| 42 | Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N); |
| 43 | if (N > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 44 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 45 | Data = Data.substr(N); |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 46 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | std::error_code RawCoverageReader::readIntMax(uint64_t &Result, |
| 50 | uint64_t MaxPlus1) { |
| 51 | if (auto Err = readULEB128(Result)) |
| 52 | return Err; |
| 53 | if (Result >= MaxPlus1) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 54 | return coveragemap_error::malformed; |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 55 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | std::error_code RawCoverageReader::readSize(uint64_t &Result) { |
| 59 | if (auto Err = readULEB128(Result)) |
| 60 | return Err; |
| 61 | // Sanity check the number. |
| 62 | if (Result > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 63 | return coveragemap_error::malformed; |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 64 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::error_code RawCoverageReader::readString(StringRef &Result) { |
| 68 | uint64_t Length; |
| 69 | if (auto Err = readSize(Length)) |
| 70 | return Err; |
| 71 | Result = Data.substr(0, Length); |
| 72 | Data = Data.substr(Length); |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 73 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | std::error_code RawCoverageFilenamesReader::read() { |
| 77 | uint64_t NumFilenames; |
| 78 | if (auto Err = readSize(NumFilenames)) |
| 79 | return Err; |
| 80 | for (size_t I = 0; I < NumFilenames; ++I) { |
| 81 | StringRef Filename; |
| 82 | if (auto Err = readString(Filename)) |
| 83 | return Err; |
| 84 | Filenames.push_back(Filename); |
| 85 | } |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 86 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value, |
| 90 | Counter &C) { |
| 91 | auto Tag = Value & Counter::EncodingTagMask; |
| 92 | switch (Tag) { |
| 93 | case Counter::Zero: |
| 94 | C = Counter::getZero(); |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 95 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 96 | case Counter::CounterValueReference: |
| 97 | C = Counter::getCounter(Value >> Counter::EncodingTagBits); |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 98 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 99 | default: |
| 100 | break; |
| 101 | } |
| 102 | Tag -= Counter::Expression; |
| 103 | switch (Tag) { |
| 104 | case CounterExpression::Subtract: |
| 105 | case CounterExpression::Add: { |
| 106 | auto ID = Value >> Counter::EncodingTagBits; |
| 107 | if (ID >= Expressions.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 108 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 109 | Expressions[ID].Kind = CounterExpression::ExprKind(Tag); |
| 110 | C = Counter::getExpression(ID); |
| 111 | break; |
| 112 | } |
| 113 | default: |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 114 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 115 | } |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 116 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | std::error_code RawCoverageMappingReader::readCounter(Counter &C) { |
| 120 | uint64_t EncodedCounter; |
| 121 | if (auto Err = |
| 122 | readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max())) |
| 123 | return Err; |
| 124 | if (auto Err = decodeCounter(EncodedCounter, C)) |
| 125 | return Err; |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 126 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static const unsigned EncodingExpansionRegionBit = 1 |
| 130 | << Counter::EncodingTagBits; |
| 131 | |
| 132 | /// \brief Read the sub-array of regions for the given inferred file id. |
Ehsan Akhgari | 29b61ce | 2014-07-25 02:51:57 +0000 | [diff] [blame] | 133 | /// \param NumFileIDs the number of file ids that are defined for this |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 134 | /// function. |
| 135 | std::error_code RawCoverageMappingReader::readMappingRegionsSubArray( |
| 136 | std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID, |
| 137 | size_t NumFileIDs) { |
| 138 | uint64_t NumRegions; |
| 139 | if (auto Err = readSize(NumRegions)) |
| 140 | return Err; |
| 141 | unsigned LineStart = 0; |
| 142 | for (size_t I = 0; I < NumRegions; ++I) { |
| 143 | Counter C; |
| 144 | CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion; |
| 145 | |
| 146 | // Read the combined counter + region kind. |
| 147 | uint64_t EncodedCounterAndRegion; |
| 148 | if (auto Err = readIntMax(EncodedCounterAndRegion, |
| 149 | std::numeric_limits<unsigned>::max())) |
| 150 | return Err; |
| 151 | unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask; |
| 152 | uint64_t ExpandedFileID = 0; |
| 153 | if (Tag != Counter::Zero) { |
| 154 | if (auto Err = decodeCounter(EncodedCounterAndRegion, C)) |
| 155 | return Err; |
| 156 | } else { |
| 157 | // Is it an expansion region? |
| 158 | if (EncodedCounterAndRegion & EncodingExpansionRegionBit) { |
| 159 | Kind = CounterMappingRegion::ExpansionRegion; |
| 160 | ExpandedFileID = EncodedCounterAndRegion >> |
| 161 | Counter::EncodingCounterTagAndExpansionRegionTagBits; |
| 162 | if (ExpandedFileID >= NumFileIDs) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 163 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 164 | } else { |
| 165 | switch (EncodedCounterAndRegion >> |
| 166 | Counter::EncodingCounterTagAndExpansionRegionTagBits) { |
| 167 | case CounterMappingRegion::CodeRegion: |
| 168 | // Don't do anything when we have a code region with a zero counter. |
| 169 | break; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 170 | case CounterMappingRegion::SkippedRegion: |
| 171 | Kind = CounterMappingRegion::SkippedRegion; |
| 172 | break; |
| 173 | default: |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 174 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Read the source range. |
Justin Bogner | de15817 | 2015-02-03 21:35:36 +0000 | [diff] [blame] | 180 | uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 181 | if (auto Err = |
| 182 | readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max())) |
| 183 | return Err; |
Justin Bogner | de15817 | 2015-02-03 21:35:36 +0000 | [diff] [blame] | 184 | if (auto Err = readULEB128(ColumnStart)) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 185 | return Err; |
Alex Lorenz | 1193b5e | 2014-08-04 18:00:51 +0000 | [diff] [blame] | 186 | if (ColumnStart > std::numeric_limits<unsigned>::max()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 187 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 188 | if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max())) |
| 189 | return Err; |
| 190 | if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max())) |
| 191 | return Err; |
| 192 | LineStart += LineStartDelta; |
| 193 | // Adjust the column locations for the empty regions that are supposed to |
| 194 | // cover whole lines. Those regions should be encoded with the |
| 195 | // column range (1 -> std::numeric_limits<unsigned>::max()), but because |
| 196 | // the encoded std::numeric_limits<unsigned>::max() is several bytes long, |
| 197 | // we set the column range to (0 -> 0) to ensure that the column start and |
| 198 | // column end take up one byte each. |
| 199 | // The std::numeric_limits<unsigned>::max() is used to represent a column |
| 200 | // position at the end of the line without knowing the length of that line. |
| 201 | if (ColumnStart == 0 && ColumnEnd == 0) { |
| 202 | ColumnStart = 1; |
| 203 | ColumnEnd = std::numeric_limits<unsigned>::max(); |
| 204 | } |
Justin Bogner | f584649 | 2014-09-20 15:31:51 +0000 | [diff] [blame] | 205 | |
| 206 | DEBUG({ |
| 207 | dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":" |
| 208 | << ColumnStart << " -> " << (LineStart + NumLines) << ":" |
| 209 | << ColumnEnd << ", "; |
| 210 | if (Kind == CounterMappingRegion::ExpansionRegion) |
| 211 | dbgs() << "Expands to file " << ExpandedFileID; |
| 212 | else |
| 213 | CounterMappingContext(Expressions).dump(C, dbgs()); |
| 214 | dbgs() << "\n"; |
| 215 | }); |
| 216 | |
Justin Bogner | 26b3142 | 2015-02-03 23:59:33 +0000 | [diff] [blame] | 217 | MappingRegions.push_back(CounterMappingRegion( |
| 218 | C, InferredFileID, ExpandedFileID, LineStart, ColumnStart, |
| 219 | LineStart + NumLines, ColumnEnd, Kind)); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 220 | } |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 221 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 224 | std::error_code RawCoverageMappingReader::read() { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 225 | |
| 226 | // Read the virtual file mapping. |
| 227 | llvm::SmallVector<unsigned, 8> VirtualFileMapping; |
| 228 | uint64_t NumFileMappings; |
| 229 | if (auto Err = readSize(NumFileMappings)) |
| 230 | return Err; |
| 231 | for (size_t I = 0; I < NumFileMappings; ++I) { |
| 232 | uint64_t FilenameIndex; |
| 233 | if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size())) |
| 234 | return Err; |
| 235 | VirtualFileMapping.push_back(FilenameIndex); |
| 236 | } |
| 237 | |
| 238 | // Construct the files using unique filenames and virtual file mapping. |
| 239 | for (auto I : VirtualFileMapping) { |
| 240 | Filenames.push_back(TranslationUnitFilenames[I]); |
| 241 | } |
| 242 | |
| 243 | // Read the expressions. |
| 244 | uint64_t NumExpressions; |
| 245 | if (auto Err = readSize(NumExpressions)) |
| 246 | return Err; |
| 247 | // Create an array of dummy expressions that get the proper counters |
| 248 | // when the expressions are read, and the proper kinds when the counters |
| 249 | // are decoded. |
| 250 | Expressions.resize( |
| 251 | NumExpressions, |
| 252 | CounterExpression(CounterExpression::Subtract, Counter(), Counter())); |
| 253 | for (size_t I = 0; I < NumExpressions; ++I) { |
| 254 | if (auto Err = readCounter(Expressions[I].LHS)) |
| 255 | return Err; |
| 256 | if (auto Err = readCounter(Expressions[I].RHS)) |
| 257 | return Err; |
| 258 | } |
| 259 | |
| 260 | // Read the mapping regions sub-arrays. |
| 261 | for (unsigned InferredFileID = 0, S = VirtualFileMapping.size(); |
| 262 | InferredFileID < S; ++InferredFileID) { |
| 263 | if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID, |
| 264 | VirtualFileMapping.size())) |
| 265 | return Err; |
| 266 | } |
| 267 | |
| 268 | // Set the counters for the expansion regions. |
| 269 | // i.e. Counter of expansion region = counter of the first region |
| 270 | // from the expanded file. |
| 271 | // Perform multiple passes to correctly propagate the counters through |
| 272 | // all the nested expansion regions. |
Alex Lorenz | 251b3e3 | 2014-07-29 21:42:24 +0000 | [diff] [blame] | 273 | SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping; |
| 274 | FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 275 | for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) { |
Alex Lorenz | 251b3e3 | 2014-07-29 21:42:24 +0000 | [diff] [blame] | 276 | for (auto &R : MappingRegions) { |
| 277 | if (R.Kind != CounterMappingRegion::ExpansionRegion) |
| 278 | continue; |
| 279 | assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]); |
| 280 | FileIDExpansionRegionMapping[R.ExpandedFileID] = &R; |
| 281 | } |
| 282 | for (auto &R : MappingRegions) { |
| 283 | if (FileIDExpansionRegionMapping[R.FileID]) { |
| 284 | FileIDExpansionRegionMapping[R.FileID]->Count = R.Count; |
| 285 | FileIDExpansionRegionMapping[R.FileID] = nullptr; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
Justin Bogner | 0b13086 | 2015-05-06 23:15:55 +0000 | [diff] [blame] | 290 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 293 | std::error_code InstrProfSymtab::create(SectionRef &Section) { |
| 294 | if (auto Err = Section.getContents(Data)) |
| 295 | return Err; |
| 296 | Address = Section.getAddress(); |
| 297 | return std::error_code(); |
| 298 | } |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 299 | |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 300 | StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) { |
| 301 | if (Pointer < Address) |
| 302 | return StringRef(); |
| 303 | auto Offset = Pointer - Address; |
| 304 | if (Offset + Size > Data.size()) |
| 305 | return StringRef(); |
| 306 | return Data.substr(Pointer - Address, Size); |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 307 | } |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 308 | |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 309 | struct CovMapFuncRecordReader { |
| 310 | // The interface to read coverage mapping function records for |
| 311 | // a module. \p Buf is a reference to the buffer pointer pointing |
| 312 | // to the \c CovHeader of coverage mapping data associated with |
| 313 | // the module. |
| 314 | virtual std::error_code readFunctionRecords(const char *&Buf, |
| 315 | const char *End) = 0; |
| 316 | template <class IntPtrT, support::endianness Endian> |
| 317 | static std::unique_ptr<CovMapFuncRecordReader> |
| 318 | get(coverage::CoverageMappingVersion Version, InstrProfSymtab &P, |
| 319 | std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, |
| 320 | std::vector<StringRef> &F); |
| 321 | }; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 322 | |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 323 | // A class for reading coverage mapping function records for a module. |
| 324 | template <coverage::CoverageMappingVersion CovMapVersion, class IntPtrT, |
| 325 | support::endianness Endian> |
| 326 | class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader { |
| 327 | typedef typename coverage::CovMapTraits< |
| 328 | CovMapVersion, IntPtrT>::CovMapFuncRecordType FuncRecordType; |
| 329 | typedef typename coverage::CovMapTraits<CovMapVersion, IntPtrT>::NameRefType |
| 330 | NameRefType; |
| 331 | |
| 332 | llvm::DenseSet<NameRefType> UniqueFunctionMappingData; |
| 333 | InstrProfSymtab &ProfileNames; |
| 334 | std::vector<StringRef> &Filenames; |
| 335 | std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records; |
| 336 | |
| 337 | public: |
| 338 | VersionedCovMapFuncRecordReader( |
| 339 | InstrProfSymtab &P, |
| 340 | std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, |
| 341 | std::vector<StringRef> &F) |
| 342 | : ProfileNames(P), Filenames(F), Records(R) {} |
| 343 | |
| 344 | std::error_code readFunctionRecords(const char *&Buf, |
| 345 | const char *End) override { |
| 346 | using namespace support; |
Xinliang David Li | 946558d | 2016-01-03 18:57:40 +0000 | [diff] [blame] | 347 | if (Buf + sizeof(CovMapHeader) > End) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 348 | return coveragemap_error::malformed; |
Xinliang David Li | 946558d | 2016-01-03 18:57:40 +0000 | [diff] [blame] | 349 | auto CovHeader = reinterpret_cast<const coverage::CovMapHeader *>(Buf); |
Xinliang David Li | 81f18a5 | 2016-01-13 04:36:15 +0000 | [diff] [blame] | 350 | uint32_t NRecords = CovHeader->getNRecords<Endian>(); |
| 351 | uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>(); |
| 352 | uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>(); |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 353 | assert((CoverageMappingVersion)CovHeader->getVersion<Endian>() == |
| 354 | CovMapVersion); |
| 355 | Buf = reinterpret_cast<const char *>(CovHeader + 1); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 356 | |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 357 | // Skip past the function records, saving the start and end for later. |
| 358 | const char *FunBuf = Buf; |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 359 | Buf += NRecords * sizeof(FuncRecordType); |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 360 | const char *FunEnd = Buf; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 361 | |
| 362 | // Get the filenames. |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 363 | if (Buf + FilenamesSize > End) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 364 | return coveragemap_error::malformed; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 365 | size_t FilenamesBegin = Filenames.size(); |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 366 | RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 367 | if (auto Err = Reader.read()) |
| 368 | return Err; |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 369 | Buf += FilenamesSize; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 370 | |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 371 | // We'll read the coverage mapping records in the loop below. |
| 372 | const char *CovBuf = Buf; |
| 373 | Buf += CoverageSize; |
| 374 | const char *CovEnd = Buf; |
Justin Bogner | d49d8ee | 2015-06-05 01:23:42 +0000 | [diff] [blame] | 375 | |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 376 | if (Buf > End) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 377 | return coveragemap_error::malformed; |
Justin Bogner | d49d8ee | 2015-06-05 01:23:42 +0000 | [diff] [blame] | 378 | // Each coverage map has an alignment of 8, so we need to adjust alignment |
| 379 | // before reading the next map. |
| 380 | Buf += alignmentAdjustment(Buf, 8); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 381 | |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 382 | auto CFR = reinterpret_cast<const FuncRecordType *>(FunBuf); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 383 | while ((const char *)CFR < FunEnd) { |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 384 | // Read the function information |
Xinliang David Li | 81f18a5 | 2016-01-13 04:36:15 +0000 | [diff] [blame] | 385 | uint32_t DataSize = CFR->template getDataSize<Endian>(); |
| 386 | uint64_t FuncHash = CFR->template getFuncHash<Endian>(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 387 | |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 388 | // Now use that to read the coverage data. |
| 389 | if (CovBuf + DataSize > CovEnd) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 390 | return coveragemap_error::malformed; |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 391 | auto Mapping = StringRef(CovBuf, DataSize); |
| 392 | CovBuf += DataSize; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 393 | |
| 394 | // Ignore this record if we already have a record that points to the same |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 395 | // function name. This is useful to ignore the redundant records for the |
| 396 | // functions with ODR linkage. |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 397 | NameRefType NameRef = CFR->template getFuncNameRef<Endian>(); |
Xinliang David Li | 81f18a5 | 2016-01-13 04:36:15 +0000 | [diff] [blame] | 398 | if (!UniqueFunctionMappingData.insert(NameRef).second) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 399 | continue; |
Justin Bogner | 7b33cc9 | 2015-03-16 06:55:45 +0000 | [diff] [blame] | 400 | |
Xinliang David Li | 81f18a5 | 2016-01-13 04:36:15 +0000 | [diff] [blame] | 401 | StringRef FuncName; |
| 402 | if (std::error_code EC = |
| 403 | CFR->template getFuncName<Endian>(ProfileNames, FuncName)) |
| 404 | return EC; |
Justin Bogner | e84891a | 2015-02-26 20:06:24 +0000 | [diff] [blame] | 405 | Records.push_back(BinaryCoverageReader::ProfileMappingRecord( |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 406 | CovMapVersion, FuncName, FuncHash, Mapping, FilenamesBegin, |
| 407 | Filenames.size() - FilenamesBegin)); |
Xinliang David Li | 81f18a5 | 2016-01-13 04:36:15 +0000 | [diff] [blame] | 408 | CFR++; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 409 | } |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 410 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 411 | } |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 412 | }; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 413 | |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 414 | template <class IntPtrT, support::endianness Endian> |
| 415 | std::unique_ptr<CovMapFuncRecordReader> CovMapFuncRecordReader::get( |
| 416 | coverage::CoverageMappingVersion Version, InstrProfSymtab &P, |
| 417 | std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, |
| 418 | std::vector<StringRef> &F) { |
| 419 | using namespace coverage; |
| 420 | switch (Version) { |
| 421 | case CoverageMappingVersion1: |
| 422 | return make_unique<VersionedCovMapFuncRecordReader<CoverageMappingVersion1, |
| 423 | IntPtrT, Endian>>(P, R, |
| 424 | F); |
| 425 | default: |
| 426 | break; |
| 427 | } |
| 428 | llvm_unreachable("Unsupported version"); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Xinliang David Li | aab986f | 2016-01-13 22:58:42 +0000 | [diff] [blame^] | 431 | template <typename T, support::endianness Endian> |
| 432 | static std::error_code readCoverageMappingData( |
| 433 | InstrProfSymtab &ProfileNames, StringRef Data, |
| 434 | std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records, |
| 435 | std::vector<StringRef> &Filenames) { |
| 436 | using namespace coverage; |
| 437 | // Read the records in the coverage data section. |
| 438 | auto CovHeader = |
| 439 | reinterpret_cast<const coverage::CovMapHeader *>(Data.data()); |
| 440 | CoverageMappingVersion Version = |
| 441 | (CoverageMappingVersion)CovHeader->getVersion<Endian>(); |
| 442 | if (Version > coverage::CoverageMappingCurrentVersion) |
| 443 | return coveragemap_error::unsupported_version; |
| 444 | std::unique_ptr<CovMapFuncRecordReader> Reader = |
| 445 | CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records, |
| 446 | Filenames); |
| 447 | for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) { |
| 448 | if (std::error_code EC = Reader->readFunctionRecords(Buf, End)) |
| 449 | return EC; |
| 450 | } |
| 451 | return std::error_code(); |
| 452 | } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 453 | static const char *TestingFormatMagic = "llvmcovmtestdata"; |
| 454 | |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 455 | static std::error_code loadTestingFormat(StringRef Data, |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 456 | InstrProfSymtab &ProfileNames, |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 457 | StringRef &CoverageMapping, |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 458 | uint8_t &BytesInAddress, |
| 459 | support::endianness &Endian) { |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 460 | BytesInAddress = 8; |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 461 | Endian = support::endianness::little; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 462 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 463 | Data = Data.substr(StringRef(TestingFormatMagic).size()); |
| 464 | if (Data.size() < 1) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 465 | return coveragemap_error::truncated; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 466 | unsigned N = 0; |
| 467 | auto ProfileNamesSize = |
| 468 | decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N); |
| 469 | if (N > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 470 | return coveragemap_error::malformed; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 471 | Data = Data.substr(N); |
| 472 | if (Data.size() < 1) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 473 | return coveragemap_error::truncated; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 474 | N = 0; |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 475 | uint64_t Address = |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 476 | decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N); |
| 477 | if (N > Data.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 478 | return coveragemap_error::malformed; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 479 | Data = Data.substr(N); |
| 480 | if (Data.size() < ProfileNamesSize) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 481 | return coveragemap_error::malformed; |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 482 | ProfileNames.create(Data.substr(0, ProfileNamesSize), Address); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 483 | CoverageMapping = Data.substr(ProfileNamesSize); |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 484 | return std::error_code(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Justin Bogner | 5a5c381 | 2015-05-07 00:31:58 +0000 | [diff] [blame] | 487 | static ErrorOr<SectionRef> lookupSection(ObjectFile &OF, StringRef Name) { |
| 488 | StringRef FoundName; |
| 489 | for (const auto &Section : OF.sections()) { |
| 490 | if (auto EC = Section.getName(FoundName)) |
| 491 | return EC; |
| 492 | if (FoundName == Name) |
| 493 | return Section; |
| 494 | } |
| 495 | return coveragemap_error::no_data_found; |
| 496 | } |
| 497 | |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 498 | static std::error_code |
| 499 | loadBinaryFormat(MemoryBufferRef ObjectBuffer, InstrProfSymtab &ProfileNames, |
| 500 | StringRef &CoverageMapping, uint8_t &BytesInAddress, |
| 501 | support::endianness &Endian, StringRef Arch) { |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 502 | auto BinOrErr = object::createBinary(ObjectBuffer); |
| 503 | if (std::error_code EC = BinOrErr.getError()) |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 504 | return EC; |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 505 | auto Bin = std::move(BinOrErr.get()); |
| 506 | std::unique_ptr<ObjectFile> OF; |
| 507 | if (auto *Universal = dyn_cast<object::MachOUniversalBinary>(Bin.get())) { |
| 508 | // If we have a universal binary, try to look up the object for the |
| 509 | // appropriate architecture. |
| 510 | auto ObjectFileOrErr = Universal->getObjectForArch(Arch); |
| 511 | if (std::error_code EC = ObjectFileOrErr.getError()) |
| 512 | return EC; |
| 513 | OF = std::move(ObjectFileOrErr.get()); |
| 514 | } else if (isa<object::ObjectFile>(Bin.get())) { |
| 515 | // For any other object file, upcast and take ownership. |
| 516 | OF.reset(cast<object::ObjectFile>(Bin.release())); |
| 517 | // If we've asked for a particular arch, make sure they match. |
Frederic Riss | ebc162a | 2015-06-22 21:33:24 +0000 | [diff] [blame] | 518 | if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch()) |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 519 | return object_error::arch_not_found; |
| 520 | } else |
| 521 | // We can only handle object files. |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 522 | return coveragemap_error::malformed; |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 523 | |
| 524 | // The coverage uses native pointer sizes for the object it's written in. |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 525 | BytesInAddress = OF->getBytesInAddress(); |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 526 | Endian = OF->isLittleEndian() ? support::endianness::little |
| 527 | : support::endianness::big; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 528 | |
| 529 | // Look for the sections that we are interested in. |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 530 | auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName(false)); |
Justin Bogner | 5a5c381 | 2015-05-07 00:31:58 +0000 | [diff] [blame] | 531 | if (auto EC = NamesSection.getError()) |
| 532 | return EC; |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 533 | auto CoverageSection = |
| 534 | lookupSection(*OF, getInstrProfCoverageSectionName(false)); |
Justin Bogner | 5a5c381 | 2015-05-07 00:31:58 +0000 | [diff] [blame] | 535 | if (auto EC = CoverageSection.getError()) |
| 536 | return EC; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 537 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 538 | // Get the contents of the given sections. |
Justin Bogner | 5a5c381 | 2015-05-07 00:31:58 +0000 | [diff] [blame] | 539 | if (std::error_code EC = CoverageSection->getContents(CoverageMapping)) |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 540 | return EC; |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 541 | if (std::error_code EC = ProfileNames.create(*NamesSection)) |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 542 | return EC; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 543 | |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 544 | return std::error_code(); |
| 545 | } |
| 546 | |
| 547 | ErrorOr<std::unique_ptr<BinaryCoverageReader>> |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 548 | BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer, |
Frederic Riss | ebc162a | 2015-06-22 21:33:24 +0000 | [diff] [blame] | 549 | StringRef Arch) { |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 550 | std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader()); |
| 551 | |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 552 | InstrProfSymtab ProfileNames; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 553 | StringRef Coverage; |
| 554 | uint8_t BytesInAddress; |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 555 | support::endianness Endian; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 556 | std::error_code EC; |
| 557 | if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic)) |
| 558 | // This is a special format used for testing. |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 559 | EC = loadTestingFormat(ObjectBuffer->getBuffer(), ProfileNames, Coverage, |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 560 | BytesInAddress, Endian); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 561 | else |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 562 | EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), ProfileNames, |
| 563 | Coverage, BytesInAddress, Endian, Arch); |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 564 | if (EC) |
| 565 | return EC; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 566 | |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 567 | if (BytesInAddress == 4 && Endian == support::endianness::little) |
| 568 | EC = readCoverageMappingData<uint32_t, support::endianness::little>( |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 569 | ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 570 | else if (BytesInAddress == 4 && Endian == support::endianness::big) |
| 571 | EC = readCoverageMappingData<uint32_t, support::endianness::big>( |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 572 | ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 573 | else if (BytesInAddress == 8 && Endian == support::endianness::little) |
| 574 | EC = readCoverageMappingData<uint64_t, support::endianness::little>( |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 575 | ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); |
Justin Bogner | a438717 | 2015-03-16 21:40:18 +0000 | [diff] [blame] | 576 | else if (BytesInAddress == 8 && Endian == support::endianness::big) |
| 577 | EC = readCoverageMappingData<uint64_t, support::endianness::big>( |
Xinliang David Li | 50de45d | 2015-12-17 00:53:37 +0000 | [diff] [blame] | 578 | ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 579 | else |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 580 | return coveragemap_error::malformed; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 581 | if (EC) |
| 582 | return EC; |
| 583 | return std::move(Reader); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | std::error_code |
Justin Bogner | e84891a | 2015-02-26 20:06:24 +0000 | [diff] [blame] | 587 | BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 588 | if (CurrentRecord >= MappingRecords.size()) |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 589 | return coveragemap_error::eof; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 590 | |
| 591 | FunctionsFilenames.clear(); |
| 592 | Expressions.clear(); |
| 593 | MappingRegions.clear(); |
| 594 | auto &R = MappingRecords[CurrentRecord]; |
| 595 | RawCoverageMappingReader Reader( |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 596 | R.CoverageMapping, |
Justin Bogner | 346359d | 2015-02-03 00:00:00 +0000 | [diff] [blame] | 597 | makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize), |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 598 | FunctionsFilenames, Expressions, MappingRegions); |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 599 | if (auto Err = Reader.read()) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 600 | return Err; |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 601 | |
| 602 | Record.FunctionName = R.FunctionName; |
Alex Lorenz | 936b99c | 2014-08-21 19:23:25 +0000 | [diff] [blame] | 603 | Record.FunctionHash = R.FunctionHash; |
Justin Bogner | 195a4f0 | 2015-02-03 00:20:11 +0000 | [diff] [blame] | 604 | Record.Filenames = FunctionsFilenames; |
| 605 | Record.Expressions = Expressions; |
| 606 | Record.MappingRegions = MappingRegions; |
| 607 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 608 | ++CurrentRecord; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 609 | return std::error_code(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 610 | } |