10b57cec5SDimitry Andric //===- DebugInlineeLinesSubsection.cpp ------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
1381ad6265SDimitry Andric #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
140b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
150b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
160b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
170b57cec5SDimitry Andric #include "llvm/Support/Error.h"
180b57cec5SDimitry Andric #include <cassert>
190b57cec5SDimitry Andric #include <cstdint>
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric using namespace llvm::codeview;
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric Error VarStreamArrayExtractor<InlineeSourceLine>::
operator ()(BinaryStreamRef Stream,uint32_t & Len,InlineeSourceLine & Item)250b57cec5SDimitry Andric operator()(BinaryStreamRef Stream, uint32_t &Len, InlineeSourceLine &Item) {
260b57cec5SDimitry Andric BinaryStreamReader Reader(Stream);
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric if (auto EC = Reader.readObject(Item.Header))
290b57cec5SDimitry Andric return EC;
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric if (HasExtraFiles) {
320b57cec5SDimitry Andric uint32_t ExtraFileCount;
330b57cec5SDimitry Andric if (auto EC = Reader.readInteger(ExtraFileCount))
340b57cec5SDimitry Andric return EC;
350b57cec5SDimitry Andric if (auto EC = Reader.readArray(Item.ExtraFiles, ExtraFileCount))
360b57cec5SDimitry Andric return EC;
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric Len = Reader.getOffset();
400b57cec5SDimitry Andric return Error::success();
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
DebugInlineeLinesSubsectionRef()430b57cec5SDimitry Andric DebugInlineeLinesSubsectionRef::DebugInlineeLinesSubsectionRef()
440b57cec5SDimitry Andric : DebugSubsectionRef(DebugSubsectionKind::InlineeLines) {}
450b57cec5SDimitry Andric
initialize(BinaryStreamReader Reader)460b57cec5SDimitry Andric Error DebugInlineeLinesSubsectionRef::initialize(BinaryStreamReader Reader) {
470b57cec5SDimitry Andric if (auto EC = Reader.readEnum(Signature))
480b57cec5SDimitry Andric return EC;
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric Lines.getExtractor().HasExtraFiles = hasExtraFiles();
510b57cec5SDimitry Andric if (auto EC = Reader.readArray(Lines, Reader.bytesRemaining()))
520b57cec5SDimitry Andric return EC;
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric assert(Reader.bytesRemaining() == 0);
550b57cec5SDimitry Andric return Error::success();
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric
hasExtraFiles() const580b57cec5SDimitry Andric bool DebugInlineeLinesSubsectionRef::hasExtraFiles() const {
590b57cec5SDimitry Andric return Signature == InlineeLinesSignature::ExtraFiles;
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric
DebugInlineeLinesSubsection(DebugChecksumsSubsection & Checksums,bool HasExtraFiles)620b57cec5SDimitry Andric DebugInlineeLinesSubsection::DebugInlineeLinesSubsection(
630b57cec5SDimitry Andric DebugChecksumsSubsection &Checksums, bool HasExtraFiles)
640b57cec5SDimitry Andric : DebugSubsection(DebugSubsectionKind::InlineeLines), Checksums(Checksums),
650b57cec5SDimitry Andric HasExtraFiles(HasExtraFiles) {}
660b57cec5SDimitry Andric
calculateSerializedSize() const670b57cec5SDimitry Andric uint32_t DebugInlineeLinesSubsection::calculateSerializedSize() const {
680b57cec5SDimitry Andric // 4 bytes for the signature
690b57cec5SDimitry Andric uint32_t Size = sizeof(InlineeLinesSignature);
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric // one header for each entry.
720b57cec5SDimitry Andric Size += Entries.size() * sizeof(InlineeSourceLineHeader);
730b57cec5SDimitry Andric if (HasExtraFiles) {
740b57cec5SDimitry Andric // If extra files are enabled, one count for each entry.
750b57cec5SDimitry Andric Size += Entries.size() * sizeof(uint32_t);
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric // And one file id for each file.
780b57cec5SDimitry Andric Size += ExtraFileCount * sizeof(uint32_t);
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric assert(Size % 4 == 0);
810b57cec5SDimitry Andric return Size;
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric
commit(BinaryStreamWriter & Writer) const840b57cec5SDimitry Andric Error DebugInlineeLinesSubsection::commit(BinaryStreamWriter &Writer) const {
850b57cec5SDimitry Andric InlineeLinesSignature Sig = InlineeLinesSignature::Normal;
860b57cec5SDimitry Andric if (HasExtraFiles)
870b57cec5SDimitry Andric Sig = InlineeLinesSignature::ExtraFiles;
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric if (auto EC = Writer.writeEnum(Sig))
900b57cec5SDimitry Andric return EC;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric for (const auto &E : Entries) {
930b57cec5SDimitry Andric if (auto EC = Writer.writeObject(E.Header))
940b57cec5SDimitry Andric return EC;
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric if (!HasExtraFiles)
970b57cec5SDimitry Andric continue;
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric if (auto EC = Writer.writeInteger<uint32_t>(E.ExtraFiles.size()))
1000b57cec5SDimitry Andric return EC;
101*bdd1243dSDimitry Andric if (auto EC = Writer.writeArray(ArrayRef(E.ExtraFiles)))
1020b57cec5SDimitry Andric return EC;
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric return Error::success();
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric
addExtraFile(StringRef FileName)1080b57cec5SDimitry Andric void DebugInlineeLinesSubsection::addExtraFile(StringRef FileName) {
1090b57cec5SDimitry Andric uint32_t Offset = Checksums.mapChecksumOffset(FileName);
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric auto &Entry = Entries.back();
1120b57cec5SDimitry Andric Entry.ExtraFiles.push_back(ulittle32_t(Offset));
1130b57cec5SDimitry Andric ++ExtraFileCount;
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric
addInlineSite(TypeIndex FuncId,StringRef FileName,uint32_t SourceLine)1160b57cec5SDimitry Andric void DebugInlineeLinesSubsection::addInlineSite(TypeIndex FuncId,
1170b57cec5SDimitry Andric StringRef FileName,
1180b57cec5SDimitry Andric uint32_t SourceLine) {
1190b57cec5SDimitry Andric uint32_t Offset = Checksums.mapChecksumOffset(FileName);
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric Entries.emplace_back();
1220b57cec5SDimitry Andric auto &Entry = Entries.back();
1230b57cec5SDimitry Andric Entry.Header.FileID = Offset;
1240b57cec5SDimitry Andric Entry.Header.SourceLineNum = SourceLine;
1250b57cec5SDimitry Andric Entry.Header.Inlinee = FuncId;
1260b57cec5SDimitry Andric }
127