DebugInlineeLinesSubsection.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===- DebugInlineeLinesSubsection.cpp ------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/DebugInfo/CodeView/CodeView.h"
  11. #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
  12. #include "llvm/Support/BinaryStreamReader.h"
  13. #include "llvm/Support/BinaryStreamWriter.h"
  14. #include "llvm/Support/Endian.h"
  15. #include "llvm/Support/Error.h"
  16. #include <cassert>
  17. #include <cstdint>
  18. using namespace llvm;
  19. using namespace llvm::codeview;
  20. Error VarStreamArrayExtractor<InlineeSourceLine>::
  21. operator()(BinaryStreamRef Stream, uint32_t &Len, InlineeSourceLine &Item) {
  22. BinaryStreamReader Reader(Stream);
  23. if (auto EC = Reader.readObject(Item.Header))
  24. return EC;
  25. if (HasExtraFiles) {
  26. uint32_t ExtraFileCount;
  27. if (auto EC = Reader.readInteger(ExtraFileCount))
  28. return EC;
  29. if (auto EC = Reader.readArray(Item.ExtraFiles, ExtraFileCount))
  30. return EC;
  31. }
  32. Len = Reader.getOffset();
  33. return Error::success();
  34. }
  35. DebugInlineeLinesSubsectionRef::DebugInlineeLinesSubsectionRef()
  36. : DebugSubsectionRef(DebugSubsectionKind::InlineeLines) {}
  37. Error DebugInlineeLinesSubsectionRef::initialize(BinaryStreamReader Reader) {
  38. if (auto EC = Reader.readEnum(Signature))
  39. return EC;
  40. Lines.getExtractor().HasExtraFiles = hasExtraFiles();
  41. if (auto EC = Reader.readArray(Lines, Reader.bytesRemaining()))
  42. return EC;
  43. assert(Reader.bytesRemaining() == 0);
  44. return Error::success();
  45. }
  46. bool DebugInlineeLinesSubsectionRef::hasExtraFiles() const {
  47. return Signature == InlineeLinesSignature::ExtraFiles;
  48. }
  49. DebugInlineeLinesSubsection::DebugInlineeLinesSubsection(
  50. DebugChecksumsSubsection &Checksums, bool HasExtraFiles)
  51. : DebugSubsection(DebugSubsectionKind::InlineeLines), Checksums(Checksums),
  52. HasExtraFiles(HasExtraFiles) {}
  53. uint32_t DebugInlineeLinesSubsection::calculateSerializedSize() const {
  54. // 4 bytes for the signature
  55. uint32_t Size = sizeof(InlineeLinesSignature);
  56. // one header for each entry.
  57. Size += Entries.size() * sizeof(InlineeSourceLineHeader);
  58. if (HasExtraFiles) {
  59. // If extra files are enabled, one count for each entry.
  60. Size += Entries.size() * sizeof(uint32_t);
  61. // And one file id for each file.
  62. Size += ExtraFileCount * sizeof(uint32_t);
  63. }
  64. assert(Size % 4 == 0);
  65. return Size;
  66. }
  67. Error DebugInlineeLinesSubsection::commit(BinaryStreamWriter &Writer) const {
  68. InlineeLinesSignature Sig = InlineeLinesSignature::Normal;
  69. if (HasExtraFiles)
  70. Sig = InlineeLinesSignature::ExtraFiles;
  71. if (auto EC = Writer.writeEnum(Sig))
  72. return EC;
  73. for (const auto &E : Entries) {
  74. if (auto EC = Writer.writeObject(E.Header))
  75. return EC;
  76. if (!HasExtraFiles)
  77. continue;
  78. if (auto EC = Writer.writeInteger<uint32_t>(E.ExtraFiles.size()))
  79. return EC;
  80. if (auto EC = Writer.writeArray(makeArrayRef(E.ExtraFiles)))
  81. return EC;
  82. }
  83. return Error::success();
  84. }
  85. void DebugInlineeLinesSubsection::addExtraFile(StringRef FileName) {
  86. uint32_t Offset = Checksums.mapChecksumOffset(FileName);
  87. auto &Entry = Entries.back();
  88. Entry.ExtraFiles.push_back(ulittle32_t(Offset));
  89. ++ExtraFileCount;
  90. }
  91. void DebugInlineeLinesSubsection::addInlineSite(TypeIndex FuncId,
  92. StringRef FileName,
  93. uint32_t SourceLine) {
  94. uint32_t Offset = Checksums.mapChecksumOffset(FileName);
  95. Entries.emplace_back();
  96. auto &Entry = Entries.back();
  97. Entry.Header.FileID = Offset;
  98. Entry.Header.SourceLineNum = SourceLine;
  99. Entry.Header.Inlinee = FuncId;
  100. }