/// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be /// called after readGCNO(). bool GCOVFile::readGCDA(GCOVBuffer &Buffer) { assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()"); if (!Buffer.readGCDAFormat()) return false; GCOV::GCOVVersion GCDAVersion; if (!Buffer.readGCOVVersion(GCDAVersion)) return false; if (Version != GCDAVersion) { errs() << "GCOV versions do not match.\n"; return false; } uint32_t GCDAChecksum; if (!Buffer.readInt(GCDAChecksum)) return false; if (Checksum != GCDAChecksum) { errs() << "File checksums do not match: " << Checksum << " != " << GCDAChecksum << ".\n"; return false; } for (size_t i = 0, e = Functions.size(); i < e; ++i) { if (!Buffer.readFunctionTag()) { errs() << "Unexpected number of functions.\n"; return false; } if (!Functions[i]->readGCDA(Buffer, Version)) return false; } if (Buffer.readObjectTag()) { uint32_t Length; uint32_t Dummy; if (!Buffer.readInt(Length)) return false; if (!Buffer.readInt(Dummy)) return false; // checksum if (!Buffer.readInt(Dummy)) return false; // num if (!Buffer.readInt(RunCount)) return false;; Buffer.advanceCursor(Length-3); } while (Buffer.readProgramTag()) { uint32_t Length; if (!Buffer.readInt(Length)) return false; Buffer.advanceCursor(Length); ++ProgramCount; } return true; }
std::vector<GCOVFunction*> readFunctions(GCOVFile& GF, GCOVBuffer &GCNOBuffer, GCOVBuffer &GCDABuffer) { GCOV::GCOVVersion Version; std::vector<GCOVFunction*> Functions; uint32_t Checksum; GCOVOptions Options(true, true, true, true, true, true, false, false); FileInfo FI(Options); if (!GCNOBuffer.readGCNOFormat() || !GCDABuffer.readGCDAFormat()) return Functions; if (!GCNOBuffer.readGCOVVersion(Version) || !GCDABuffer.readGCOVVersion(Version)) return Functions; if (!GCNOBuffer.readInt(Checksum) || !GCDABuffer.readInt(Checksum)) return Functions; while (true) { if (!GCNOBuffer.readFunctionTag()) break; if (!GCDABuffer.readFunctionTag()) break; auto GFun = new GCOVFunction(GF); if (!GFun->readGCNO(GCNOBuffer, Version) || !GFun->readGCDA(GCDABuffer, Version)) return Functions; GFun->collectLineCounts(FI); Functions.push_back(GFun); } FI.setRunCount(1); FI.setProgramCount(1); return Functions; }
/// readGCNO - Read GCNO buffer. bool GCOVFile::readGCNO(GCOVBuffer &Buffer) { if (!Buffer.readGCNOFormat()) return false; if (!Buffer.readGCOVVersion(Version)) return false; if (!Buffer.readInt(Checksum)) return false; while (true) { if (!Buffer.readFunctionTag()) break; GCOVFunction *GFun = new GCOVFunction(*this); if (!GFun->readGCNO(Buffer, Version)) return false; Functions.push_back(GFun); } GCNOInitialized = true; return true; }
/// readGCNO - Read GCNO buffer. bool GCOVFile::readGCNO(GCOVBuffer &Buffer) { if (!Buffer.readGCNOFormat()) return false; if (!Buffer.readGCOVVersion(Version)) return false; if (!Buffer.readInt(Checksum)) return false; while (true) { if (!Buffer.readFunctionTag()) break; auto GFun = make_unique<GCOVFunction>(*this); if (!GFun->readGCNO(Buffer, Version)) return false; Functions.push_back(std::move(GFun)); } GCNOInitialized = true; return true; }
/// readGCDA - Read a function from the GCDA buffer. Return false if an error /// occurs. bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { uint32_t Dummy; if (!Buff.readInt(Dummy)) return false; // Function header length uint32_t GCDAIdent; if (!Buff.readInt(GCDAIdent)) return false; if (Ident != GCDAIdent) { errs() << "Function identifiers do not match: " << Ident << " != " << GCDAIdent << " (in " << Name << ").\n"; return false; } uint32_t GCDAChecksum; if (!Buff.readInt(GCDAChecksum)) return false; if (Checksum != GCDAChecksum) { errs() << "Function checksums do not match: " << Checksum << " != " << GCDAChecksum << " (in " << Name << ").\n"; return false; } uint32_t CfgChecksum; if (Version != GCOV::V402) { if (!Buff.readInt(CfgChecksum)) return false; if (Parent.getChecksum() != CfgChecksum) { errs() << "File checksums do not match: " << Parent.getChecksum() << " != " << CfgChecksum << " (in " << Name << ").\n"; return false; } } StringRef GCDAName; if (!Buff.readString(GCDAName)) return false; if (Name != GCDAName) { errs() << "Function names do not match: " << Name << " != " << GCDAName << ".\n"; return false; } if (!Buff.readArcTag()) { errs() << "Arc tag not found (in " << Name << ").\n"; return false; } uint32_t Count; if (!Buff.readInt(Count)) return false; Count /= 2; // This for loop adds the counts for each block. A second nested loop is // required to combine the edge counts that are contained in the GCDA file. for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) { // The last block is always reserved for exit block if (BlockNo >= Blocks.size()-1) { errs() << "Unexpected number of edges (in " << Name << ").\n"; return false; } GCOVBlock &Block = *Blocks[BlockNo]; for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End; ++EdgeNo) { if (Count == 0) { errs() << "Unexpected number of edges (in " << Name << ").\n"; return false; } uint64_t ArcCount; if (!Buff.readInt64(ArcCount)) return false; Block.addCount(EdgeNo, ArcCount); --Count; } } return true; }
/// readGCNO - Read a function from the GCNO buffer. Return false if an error /// occurs. bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { uint32_t Dummy; if (!Buff.readInt(Dummy)) return false; // Function header length if (!Buff.readInt(Ident)) return false; if (!Buff.readInt(Checksum)) return false; if (Version != GCOV::V402) { uint32_t CfgChecksum; if (!Buff.readInt(CfgChecksum)) return false; if (Parent.getChecksum() != CfgChecksum) { errs() << "File checksums do not match: " << Parent.getChecksum() << " != " << CfgChecksum << " in (" << Name << ").\n"; return false; } } if (!Buff.readString(Name)) return false; if (!Buff.readString(Filename)) return false; if (!Buff.readInt(LineNumber)) return false; // read blocks. if (!Buff.readBlockTag()) { errs() << "Block tag not found.\n"; return false; } uint32_t BlockCount; if (!Buff.readInt(BlockCount)) return false; for (uint32_t i = 0, e = BlockCount; i != e; ++i) { if (!Buff.readInt(Dummy)) return false; // Block flags; Blocks.push_back(new GCOVBlock(*this, i)); } // read edges. while (Buff.readEdgeTag()) { uint32_t EdgeCount; if (!Buff.readInt(EdgeCount)) return false; EdgeCount = (EdgeCount - 1) / 2; uint32_t BlockNo; if (!Buff.readInt(BlockNo)) return false; if (BlockNo >= BlockCount) { errs() << "Unexpected block number (in " << Name << ").\n"; return false; } for (uint32_t i = 0, e = EdgeCount; i != e; ++i) { uint32_t Dst; if (!Buff.readInt(Dst)) return false; GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]); Edges.push_back(Edge); Blocks[BlockNo]->addDstEdge(Edge); Blocks[Dst]->addSrcEdge(Edge); if (!Buff.readInt(Dummy)) return false; // Edge flag } } // read line table. while (Buff.readLineTag()) { uint32_t LineTableLength; if (!Buff.readInt(LineTableLength)) return false; uint32_t EndPos = Buff.getCursor() + LineTableLength*4; uint32_t BlockNo; if (!Buff.readInt(BlockNo)) return false; if (BlockNo >= BlockCount) { errs() << "Unexpected block number (in " << Name << ").\n"; return false; } GCOVBlock *Block = Blocks[BlockNo]; if (!Buff.readInt(Dummy)) return false; // flag while (Buff.getCursor() != (EndPos - 4)) { StringRef F; if (!Buff.readString(F)) return false; if (F != Filename) { errs() << "Multiple sources for a single basic block (in " << Name << ").\n"; return false; } if (Buff.getCursor() == (EndPos - 4)) break; while (true) { uint32_t Line; if (!Buff.readInt(Line)) return false; if (!Line) break; Block->addLine(Line); } } if (!Buff.readInt(Dummy)) return false; // flag } return true; }
/// read - Read a aunction from the buffer. Return false if buffer cursor /// does not point to a function tag. bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) { if (!Buff.readFunctionTag()) return false; Buff.readInt(); // Function header length Ident = Buff.readInt(); Buff.readInt(); // Checksum #1 if (Format != GCOV::GCNO_402) Buff.readInt(); // Checksum #2 Name = Buff.readString(); if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404) Filename = Buff.readString(); if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) { Buff.readArcTag(); uint32_t Count = Buff.readInt() / 2; for (unsigned i = 0, e = Count; i != e; ++i) { Blocks[i]->addCount(Buff.readInt64()); } return true; } LineNumber = Buff.readInt(); // read blocks. bool BlockTagFound = Buff.readBlockTag(); (void)BlockTagFound; assert(BlockTagFound && "Block Tag not found!"); uint32_t BlockCount = Buff.readInt(); for (int i = 0, e = BlockCount; i != e; ++i) { Buff.readInt(); // Block flags; Blocks.push_back(new GCOVBlock(i)); } // read edges. while (Buff.readEdgeTag()) { uint32_t EdgeCount = (Buff.readInt() - 1) / 2; uint32_t BlockNo = Buff.readInt(); assert(BlockNo < BlockCount && "Unexpected Block number!"); for (int i = 0, e = EdgeCount; i != e; ++i) { Blocks[BlockNo]->addEdge(Buff.readInt()); Buff.readInt(); // Edge flag } } // read line table. while (Buff.readLineTag()) { uint32_t LineTableLength = Buff.readInt(); uint32_t Size = Buff.getCursor() + LineTableLength*4; uint32_t BlockNo = Buff.readInt(); assert(BlockNo < BlockCount && "Unexpected Block number!"); GCOVBlock *Block = Blocks[BlockNo]; Buff.readInt(); // flag while (Buff.getCursor() != (Size - 4)) { StringRef Filename = Buff.readString(); if (Buff.getCursor() == (Size - 4)) break; while (uint32_t L = Buff.readInt()) Block->addLine(Filename, L); } Buff.readInt(); // flag } return true; }
/// readGCNO - Read a function from the GCNO buffer. Return false if an error /// occurs. bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { uint32_t Dummy; if (!Buff.readInt(Dummy)) return false; // Function header length if (!Buff.readInt(Ident)) return false; if (!Buff.readInt(Checksum)) return false; if (Version != GCOV::V402) { uint32_t CfgChecksum; if (!Buff.readInt(CfgChecksum)) return false; if (Parent.getChecksum() != CfgChecksum) { errs() << "File checksums do not match: " << Parent.getChecksum() << " != " << CfgChecksum << " in (" << Name << ").\n"; return false; } } if (!Buff.readString(Name)) return false; if (!Buff.readString(Filename)) return false; if (!Buff.readInt(LineNumber)) return false; // read blocks. if (!Buff.readBlockTag()) { errs() << "Block tag not found.\n"; return false; } uint32_t BlockCount; if (!Buff.readInt(BlockCount)) return false; for (uint32_t i = 0, e = BlockCount; i != e; ++i) { if (!Buff.readInt(Dummy)) return false; // Block flags; Blocks.push_back(make_unique<GCOVBlock>(*this, i)); } // read edges. while (Buff.readEdgeTag()) { uint32_t EdgeCount; if (!Buff.readInt(EdgeCount)) return false; EdgeCount = (EdgeCount - 1) / 2; uint32_t BlockNo; if (!Buff.readInt(BlockNo)) return false; if (BlockNo >= BlockCount) { errs() << "Unexpected block number: " << BlockNo << " (in " << Name << ").\n"; return false; } for (uint32_t i = 0, e = EdgeCount; i != e; ++i) { uint32_t Dst; if (!Buff.readInt(Dst)) return false; Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst])); GCOVEdge *Edge = Edges.back().get(); Blocks[BlockNo]->addDstEdge(Edge); Blocks[Dst]->addSrcEdge(Edge); if (!Buff.readInt(Dummy)) return false; // Edge flag } } // read line table. while (Buff.readLineTag()) { uint32_t LineTableLength; // Read the length of this line table. if (!Buff.readInt(LineTableLength)) return false; uint32_t EndPos = Buff.getCursor() + LineTableLength*4; uint32_t BlockNo; // Read the block number this table is associated with. if (!Buff.readInt(BlockNo)) return false; if (BlockNo >= BlockCount) { errs() << "Unexpected block number: " << BlockNo << " (in " << Name << ").\n"; return false; } GCOVBlock &Block = *Blocks[BlockNo]; // Read the word that pads the beginning of the line table. This may be a // flag of some sort, but seems to always be zero. if (!Buff.readInt(Dummy)) return false; // Line information starts here and continues up until the last word. if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) { StringRef F; // Read the source file name. if (!Buff.readString(F)) return false; if (Filename != F) { errs() << "Multiple sources for a single basic block: " << Filename << " != " << F << " (in " << Name << ").\n"; return false; } // Read lines up to, but not including, the null terminator. while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) { uint32_t Line; if (!Buff.readInt(Line)) return false; // Line 0 means this instruction was injected by the compiler. Skip it. if (!Line) continue; Block.addLine(Line); } // Read the null terminator. if (!Buff.readInt(Dummy)) return false; } // The last word is either a flag or padding, it isn't clear which. Skip // over it. if (!Buff.readInt(Dummy)) return false; } return true; }