Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 1 | //===- DebugInlineeLinesSubsection.cpp ------------------------------------===// |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 9 | #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" |
Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/ArrayRef.h" |
| 11 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" |
Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 13 | #include "llvm/Support/BinaryStreamReader.h" |
| 14 | #include "llvm/Support/BinaryStreamWriter.h" |
| 15 | #include "llvm/Support/Endian.h" |
| 16 | #include "llvm/Support/Error.h" |
| 17 | #include <cassert> |
| 18 | #include <cstdint> |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace llvm::codeview; |
| 22 | |
Zachary Turner | 7e62cd1 | 2017-06-09 17:54:36 +0000 | [diff] [blame] | 23 | Error VarStreamArrayExtractor<InlineeSourceLine>:: |
| 24 | operator()(BinaryStreamRef Stream, uint32_t &Len, InlineeSourceLine &Item) { |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 25 | BinaryStreamReader Reader(Stream); |
| 26 | |
| 27 | if (auto EC = Reader.readObject(Item.Header)) |
| 28 | return EC; |
| 29 | |
Zachary Turner | 59e8389 | 2017-05-03 05:34:00 +0000 | [diff] [blame] | 30 | if (HasExtraFiles) { |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 31 | uint32_t ExtraFileCount; |
| 32 | if (auto EC = Reader.readInteger(ExtraFileCount)) |
| 33 | return EC; |
| 34 | if (auto EC = Reader.readArray(Item.ExtraFiles, ExtraFileCount)) |
| 35 | return EC; |
| 36 | } |
| 37 | |
| 38 | Len = Reader.getOffset(); |
| 39 | return Error::success(); |
| 40 | } |
| 41 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 42 | DebugInlineeLinesSubsectionRef::DebugInlineeLinesSubsectionRef() |
| 43 | : DebugSubsectionRef(DebugSubsectionKind::InlineeLines) {} |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 44 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 45 | Error DebugInlineeLinesSubsectionRef::initialize(BinaryStreamReader Reader) { |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 46 | if (auto EC = Reader.readEnum(Signature)) |
| 47 | return EC; |
| 48 | |
Zachary Turner | 7e62cd1 | 2017-06-09 17:54:36 +0000 | [diff] [blame] | 49 | Lines.getExtractor().HasExtraFiles = hasExtraFiles(); |
| 50 | if (auto EC = Reader.readArray(Lines, Reader.bytesRemaining())) |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 51 | return EC; |
| 52 | |
| 53 | assert(Reader.bytesRemaining() == 0); |
| 54 | return Error::success(); |
| 55 | } |
| 56 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 57 | bool DebugInlineeLinesSubsectionRef::hasExtraFiles() const { |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 58 | return Signature == InlineeLinesSignature::ExtraFiles; |
| 59 | } |
| 60 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 61 | DebugInlineeLinesSubsection::DebugInlineeLinesSubsection( |
| 62 | DebugChecksumsSubsection &Checksums, bool HasExtraFiles) |
| 63 | : DebugSubsection(DebugSubsectionKind::InlineeLines), Checksums(Checksums), |
| 64 | HasExtraFiles(HasExtraFiles) {} |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 65 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 66 | uint32_t DebugInlineeLinesSubsection::calculateSerializedSize() const { |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 67 | // 4 bytes for the signature |
| 68 | uint32_t Size = sizeof(InlineeLinesSignature); |
| 69 | |
| 70 | // one header for each entry. |
| 71 | Size += Entries.size() * sizeof(InlineeSourceLineHeader); |
| 72 | if (HasExtraFiles) { |
| 73 | // If extra files are enabled, one count for each entry. |
| 74 | Size += Entries.size() * sizeof(uint32_t); |
| 75 | |
| 76 | // And one file id for each file. |
| 77 | Size += ExtraFileCount * sizeof(uint32_t); |
| 78 | } |
| 79 | assert(Size % 4 == 0); |
| 80 | return Size; |
| 81 | } |
| 82 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 83 | Error DebugInlineeLinesSubsection::commit(BinaryStreamWriter &Writer) const { |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 84 | InlineeLinesSignature Sig = InlineeLinesSignature::Normal; |
| 85 | if (HasExtraFiles) |
| 86 | Sig = InlineeLinesSignature::ExtraFiles; |
| 87 | |
| 88 | if (auto EC = Writer.writeEnum(Sig)) |
| 89 | return EC; |
| 90 | |
| 91 | for (const auto &E : Entries) { |
| 92 | if (auto EC = Writer.writeObject(E.Header)) |
| 93 | return EC; |
| 94 | |
| 95 | if (!HasExtraFiles) |
| 96 | continue; |
| 97 | |
| 98 | if (auto EC = Writer.writeInteger<uint32_t>(E.ExtraFiles.size())) |
| 99 | return EC; |
| 100 | if (auto EC = Writer.writeArray(makeArrayRef(E.ExtraFiles))) |
| 101 | return EC; |
| 102 | } |
| 103 | |
| 104 | return Error::success(); |
| 105 | } |
| 106 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 107 | void DebugInlineeLinesSubsection::addExtraFile(StringRef FileName) { |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 108 | uint32_t Offset = Checksums.mapChecksumOffset(FileName); |
| 109 | |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 110 | auto &Entry = Entries.back(); |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 111 | Entry.ExtraFiles.push_back(ulittle32_t(Offset)); |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 112 | ++ExtraFileCount; |
| 113 | } |
| 114 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 115 | void DebugInlineeLinesSubsection::addInlineSite(TypeIndex FuncId, |
| 116 | StringRef FileName, |
| 117 | uint32_t SourceLine) { |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 118 | uint32_t Offset = Checksums.mapChecksumOffset(FileName); |
| 119 | |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 120 | Entries.emplace_back(); |
| 121 | auto &Entry = Entries.back(); |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 122 | Entry.Header.FileID = Offset; |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 123 | Entry.Header.SourceLineNum = SourceLine; |
| 124 | Entry.Header.Inlinee = FuncId; |
| 125 | } |