1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for reading profiling data for clang's
11 // instrumentation based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/InstrProfReader.h"
16 #include "llvm/ProfileData/InstrProf.h"
22 error_code InstrProfReader::create(std::string Path,
23 std::unique_ptr<InstrProfReader> &Result) {
24 std::unique_ptr<MemoryBuffer> Buffer;
25 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
28 // Sanity check the file.
29 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
30 return instrprof_error::too_large;
33 if (RawInstrProfReader::hasFormat(*Buffer))
34 Result.reset(new RawInstrProfReader(std::move(Buffer)));
36 Result.reset(new TextInstrProfReader(std::move(Buffer)));
38 // Read the header and return the result.
39 return Result->readHeader();
42 void InstrProfIterator::Increment() {
43 if (Reader->readNextRecord(Record))
44 *this = InstrProfIterator();
47 error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
49 while (!Line.is_at_end() && Line->empty())
51 // If we hit EOF while looking for a name, we're done.
53 return error(instrprof_error::eof);
55 // Read the function name.
56 Record.Name = *Line++;
58 // Read the function hash.
60 return error(instrprof_error::truncated);
61 if ((Line++)->getAsInteger(10, Record.Hash))
62 return error(instrprof_error::malformed);
64 // Read the number of counters.
67 return error(instrprof_error::truncated);
68 if ((Line++)->getAsInteger(10, NumCounters))
69 return error(instrprof_error::malformed);
71 // Read each counter and fill our internal storage with the values.
73 Counts.reserve(NumCounters);
74 for (uint64_t I = 0; I < NumCounters; ++I) {
76 return error(instrprof_error::truncated);
78 if ((Line++)->getAsInteger(10, Count))
79 return error(instrprof_error::malformed);
80 Counts.push_back(Count);
82 // Give the record a reference to our internal counter storage.
83 Record.Counts = Counts;
88 static uint64_t getRawMagic() {
100 bool RawInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
101 if (DataBuffer.getBufferSize() < sizeof(getRawMagic()))
103 const RawHeader *Header = (const RawHeader *)DataBuffer.getBufferStart();
104 return getRawMagic() == Header->Magic ||
105 sys::SwapByteOrder(getRawMagic()) == Header->Magic;
108 error_code RawInstrProfReader::readHeader() {
109 if (!hasFormat(*DataBuffer))
110 return error(instrprof_error::bad_magic);
111 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
112 return error(instrprof_error::bad_header);
113 const RawHeader *Header = (const RawHeader *)DataBuffer->getBufferStart();
114 ShouldSwapBytes = Header->Magic != getRawMagic();
115 return readHeader(*Header);
118 static uint64_t getRawVersion() {
122 error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
123 if (swap(Header.Version) != getRawVersion())
124 return error(instrprof_error::unsupported_version);
126 CountersDelta = swap(Header.CountersDelta);
127 NamesDelta = swap(Header.NamesDelta);
128 auto DataSize = swap(Header.DataSize);
129 auto CountersSize = swap(Header.CountersSize);
130 auto NamesSize = swap(Header.NamesSize);
132 ptrdiff_t DataOffset = sizeof(RawHeader);
133 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
134 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
135 size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
137 if (FileSize != DataBuffer->getBufferSize())
138 return error(instrprof_error::bad_header);
140 Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
141 DataEnd = Data + DataSize;
142 CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
143 NamesStart = DataBuffer->getBufferStart() + NamesOffset;
148 error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
150 return error(instrprof_error::eof);
153 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
154 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
155 swap(Data->NumCounters));
158 if (RawName.data() < NamesStart ||
159 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
160 RawCounts.data() < CountersStart ||
161 RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
162 return error(instrprof_error::malformed);
164 // Store the data in Record, byte-swapping as necessary.
165 Record.Hash = swap(Data->FuncHash);
166 Record.Name = RawName;
167 if (ShouldSwapBytes) {
169 Counts.reserve(RawCounts.size());
170 for (uint64_t Count : RawCounts)
171 Counts.push_back(swap(Count));
172 Record.Counts = Counts;
174 Record.Counts = RawCounts;