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 static uint64_t getRawMagic() {
34 error_code InstrProfReader::create(std::string Path,
35 std::unique_ptr<InstrProfReader> &Result) {
36 std::unique_ptr<MemoryBuffer> Buffer;
37 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
40 // Sanity check the file.
41 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
42 return instrprof_error::too_large;
44 if (Buffer->getBufferSize() < sizeof(uint64_t)) {
45 Result.reset(new TextInstrProfReader(Buffer));
47 return instrprof_error::success;
50 uint64_t Magic = *(uint64_t *)Buffer->getBufferStart();
51 uint64_t SwappedMagic = sys::SwapByteOrder(Magic);
52 if (Magic == getRawMagic() || SwappedMagic == getRawMagic())
53 Result.reset(new RawInstrProfReader(Buffer));
55 Result.reset(new TextInstrProfReader(Buffer));
57 return instrprof_error::success;
60 void InstrProfIterator::Increment() {
61 if (Reader->readNextRecord(Record))
62 *this = InstrProfIterator();
65 error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
67 while (!Line.is_at_end() && Line->empty())
69 // If we hit EOF while looking for a name, we're done.
71 return error(instrprof_error::eof);
73 // Read the function name.
74 Record.Name = *Line++;
76 // Read the function hash.
78 return error(instrprof_error::truncated);
79 if ((Line++)->getAsInteger(10, Record.Hash))
80 return error(instrprof_error::malformed);
82 // Read the number of counters.
85 return error(instrprof_error::truncated);
86 if ((Line++)->getAsInteger(10, NumCounters))
87 return error(instrprof_error::malformed);
89 // Read each counter and fill our internal storage with the values.
91 Counts.reserve(NumCounters);
92 for (uint64_t I = 0; I < NumCounters; ++I) {
94 return error(instrprof_error::truncated);
96 if ((Line++)->getAsInteger(10, Count))
97 return error(instrprof_error::malformed);
98 Counts.push_back(Count);
100 // Give the record a reference to our internal counter storage.
101 Record.Counts = Counts;
106 static uint64_t getRawVersion() {
111 RawInstrProfReader::RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer)
112 : DataBuffer(DataBuffer.release()) { }
114 error_code RawInstrProfReader::readHeader() {
115 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
116 return error(instrprof_error::malformed);
117 const RawHeader *Header = (RawHeader *)DataBuffer->getBufferStart();
118 if (Header->Magic == getRawMagic())
119 ShouldSwapBytes = false;
121 if (sys::SwapByteOrder(Header->Magic) != getRawMagic())
122 return error(instrprof_error::malformed);
124 ShouldSwapBytes = true;
126 return readHeader(*Header);
129 error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
130 if (swap(Header.Version) != getRawVersion())
131 return error(instrprof_error::unsupported_version);
133 CountersDelta = swap(Header.CountersDelta);
134 NamesDelta = swap(Header.NamesDelta);
135 auto DataSize = swap(Header.DataSize);
136 auto CountersSize = swap(Header.CountersSize);
137 auto NamesSize = swap(Header.NamesSize);
139 ptrdiff_t DataOffset = sizeof(RawHeader);
140 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
141 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
142 size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
144 if (FileSize != DataBuffer->getBufferSize())
145 return error(instrprof_error::malformed);
147 Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
148 DataEnd = Data + DataSize;
149 CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
150 NamesStart = DataBuffer->getBufferStart() + NamesOffset;
155 error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
157 return error(instrprof_error::eof);
160 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
161 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
162 swap(Data->NumCounters));
165 if (RawName.data() < NamesStart ||
166 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
167 RawCounts.data() < CountersStart ||
168 RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
169 return error(instrprof_error::malformed);
171 // Store the data in Record, byte-swapping as necessary.
172 Record.Hash = swap(Data->FuncHash);
173 Record.Name = RawName;
174 if (ShouldSwapBytes) {
176 Counts.reserve(RawCounts.size());
177 for (uint64_t Count : RawCounts)
178 Counts.push_back(swap(Count));
179 Record.Counts = Counts;
181 Record.Counts = RawCounts;