NativeEnumInjectedSources.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //==- NativeEnumInjectedSources.cpp - Native Injected Source Enumerator --*-==//
  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/PDB/Native/NativeEnumInjectedSources.h"
  9. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  10. #include "llvm/DebugInfo/PDB/Native/HashTable.h"
  11. #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
  12. #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
  13. #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
  14. namespace llvm {
  15. namespace pdb {
  16. namespace {
  17. Expected<std::string> readStreamData(BinaryStream &Stream, uint64_t Limit) {
  18. uint64_t Offset = 0, DataLength = std::min(Limit, Stream.getLength());
  19. std::string Result;
  20. Result.reserve(DataLength);
  21. while (Offset < DataLength) {
  22. ArrayRef<uint8_t> Data;
  23. if (auto E = Stream.readLongestContiguousChunk(Offset, Data))
  24. return std::move(E);
  25. Data = Data.take_front(DataLength - Offset);
  26. Offset += Data.size();
  27. Result += toStringRef(Data);
  28. }
  29. return Result;
  30. }
  31. class NativeInjectedSource final : public IPDBInjectedSource {
  32. const SrcHeaderBlockEntry &Entry;
  33. const PDBStringTable &Strings;
  34. PDBFile &File;
  35. public:
  36. NativeInjectedSource(const SrcHeaderBlockEntry &Entry,
  37. PDBFile &File, const PDBStringTable &Strings)
  38. : Entry(Entry), Strings(Strings), File(File) {}
  39. uint32_t getCrc32() const override { return Entry.CRC; }
  40. uint64_t getCodeByteSize() const override { return Entry.FileSize; }
  41. std::string getFileName() const override {
  42. StringRef Ret = cantFail(Strings.getStringForID(Entry.FileNI),
  43. "InjectedSourceStream should have rejected this");
  44. return std::string(Ret);
  45. }
  46. std::string getObjectFileName() const override {
  47. StringRef Ret = cantFail(Strings.getStringForID(Entry.ObjNI),
  48. "InjectedSourceStream should have rejected this");
  49. return std::string(Ret);
  50. }
  51. std::string getVirtualFileName() const override {
  52. StringRef Ret = cantFail(Strings.getStringForID(Entry.VFileNI),
  53. "InjectedSourceStream should have rejected this");
  54. return std::string(Ret);
  55. }
  56. uint32_t getCompression() const override { return Entry.Compression; }
  57. std::string getCode() const override {
  58. // Get name of stream storing the data.
  59. StringRef VName =
  60. cantFail(Strings.getStringForID(Entry.VFileNI),
  61. "InjectedSourceStream should have rejected this");
  62. std::string StreamName = ("/src/files/" + VName).str();
  63. // Find stream with that name and read its data.
  64. // FIXME: Consider validating (or even loading) all this in
  65. // InjectedSourceStream so that no error can happen here.
  66. auto ExpectedFileStream = File.safelyCreateNamedStream(StreamName);
  67. if (!ExpectedFileStream) {
  68. consumeError(ExpectedFileStream.takeError());
  69. return "(failed to open data stream)";
  70. }
  71. auto Data = readStreamData(**ExpectedFileStream, Entry.FileSize);
  72. if (!Data) {
  73. consumeError(Data.takeError());
  74. return "(failed to read data)";
  75. }
  76. return *Data;
  77. }
  78. };
  79. } // namespace
  80. NativeEnumInjectedSources::NativeEnumInjectedSources(
  81. PDBFile &File, const InjectedSourceStream &IJS,
  82. const PDBStringTable &Strings)
  83. : File(File), Stream(IJS), Strings(Strings), Cur(Stream.begin()) {}
  84. uint32_t NativeEnumInjectedSources::getChildCount() const {
  85. return static_cast<uint32_t>(Stream.size());
  86. }
  87. std::unique_ptr<IPDBInjectedSource>
  88. NativeEnumInjectedSources::getChildAtIndex(uint32_t N) const {
  89. if (N >= getChildCount())
  90. return nullptr;
  91. return std::make_unique<NativeInjectedSource>(std::next(Stream.begin(), N)->second,
  92. File, Strings);
  93. }
  94. std::unique_ptr<IPDBInjectedSource> NativeEnumInjectedSources::getNext() {
  95. if (Cur == Stream.end())
  96. return nullptr;
  97. return std::make_unique<NativeInjectedSource>((Cur++)->second, File, Strings);
  98. }
  99. void NativeEnumInjectedSources::reset() { Cur = Stream.begin(); }
  100. }
  101. }