OSDN Git Service

[llvm-pdbutil] Dump raw bytes of pdb name map.
authorZachary Turner <zturner@google.com>
Fri, 23 Jun 2017 20:18:38 +0000 (20:18 +0000)
committerZachary Turner <zturner@google.com>
Fri, 23 Jun 2017 20:18:38 +0000 (20:18 +0000)
This patch dumps the raw bytes of the pdb name map which contains
the mapping of stream name to stream index for the string table
and other reserved streams.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306148 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/DebugInfo/PDB/Native/InfoStream.h
lib/DebugInfo/PDB/Native/InfoStream.cpp
test/DebugInfo/PDB/pdbdump-raw-bytes.test
tools/llvm-pdbutil/BytesOutputStyle.cpp
tools/llvm-pdbutil/BytesOutputStyle.h
tools/llvm-pdbutil/LinePrinter.cpp
tools/llvm-pdbutil/LinePrinter.h
tools/llvm-pdbutil/llvm-pdbutil.cpp
tools/llvm-pdbutil/llvm-pdbutil.h

index fc91fc7..37bf5f3 100644 (file)
@@ -47,6 +47,8 @@ public:
 
   const NamedStreamMap &getNamedStreams() const;
 
+  BinarySubstreamRef getNamedStreamsBuffer() const;
+
   uint32_t getNamedStreamIndex(llvm::StringRef Name) const;
   iterator_range<StringMapConstIterator<uint32_t>> named_streams() const;
 
@@ -71,6 +73,8 @@ private:
   // universally unique.
   PDB_UniqueId Guid;
 
+  BinarySubstreamRef SubNamedStreams;
+
   std::vector<PdbRaw_FeatureSig> FeatureSignatures;
   PdbRaw_Features Features = PdbFeatureNone;
 
index a3979d4..21b66b3 100644 (file)
@@ -57,6 +57,10 @@ Error InfoStream::reload() {
   uint32_t NewOffset = Reader.getOffset();
   NamedStreamMapByteSize = NewOffset - Offset;
 
+  Reader.setOffset(Offset);
+  if (auto EC = Reader.readSubstream(SubNamedStreams, NamedStreamMapByteSize))
+    return EC;
+
   bool Stop = false;
   while (!Stop && !Reader.empty()) {
     PdbRaw_FeatureSig Sig;
@@ -129,3 +133,7 @@ ArrayRef<PdbRaw_FeatureSig> InfoStream::getFeatureSignatures() const {
 const NamedStreamMap &InfoStream::getNamedStreams() const {
   return NamedStreams;
 }
+
+BinarySubstreamRef InfoStream::getNamedStreamsBuffer() const {
+  return SubNamedStreams;
+}
index 2c5c96c..1087dfb 100644 (file)
@@ -2,6 +2,8 @@
 ; RUN: not llvm-pdbutil bytes -byte-range=100-20 %p/Inputs/empty.pdb 2>&1 | FileCheck --check-prefix=INVALID %s\r
 ; RUN: not llvm-pdbutil bytes -byte-range=100000-200000 %p/Inputs/empty.pdb 2>&1 | FileCheck --check-prefix=INVALID-RANGE %s\r
 \r
+; RUN: llvm-pdbutil bytes -name-map %p/Inputs/empty.pdb | FileCheck --check-prefix=NAME-MAP %s\r
+\r
 \r
 VALID:                               MSF Bytes\r
 VALID-NEXT: ============================================================\r
@@ -13,3 +15,11 @@ VALID-NEXT:  )
 INVALID: llvm-pdbutil: Invalid byte range specified.  Max < Min\r
 \r
 INVALID-RANGE: llvm-pdbutil: Invalid byte range specified.  Requested byte larger than file size\r
+\r
+NAME-MAP:                            Named Stream Map\r
+NAME-MAP-NEXT: ============================================================\r
+NAME-MAP-NEXT:   Named Stream Map (\r
+NAME-MAP-NEXT:     1301C: 22000000 2F4C696E 6B496E66 6F002F6E 616D6573 002F7372 632F6865 61646572  |".../LinkInfo./names./src/header|\r
+NAME-MAP-NEXT:     1303C: 626C6F63 6B000300 00000600 00000100 00001A00 00000000 00001100 00000900  |block...........................|\r
+NAME-MAP-NEXT:     1305C: 00000A00 00000D00 00000000 00000500 0000                                 |..................|\r
+NAME-MAP-NEXT:   )\r
index 9761987..5cf1568 100644 (file)
@@ -13,6 +13,7 @@
 #include "llvm-pdbutil.h"
 
 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
+#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
 #include "llvm/DebugInfo/PDB/Native/RawError.h"
 #include "llvm/Support/BinaryStreamReader.h"
@@ -116,9 +117,25 @@ Error BytesOutputStyle::dump() {
     dumpStreamBytes();
     P.NewLine();
   }
+
+  if (opts::bytes::NameMap) {
+    dumpNameMap();
+    P.NewLine();
+  }
   return Error::success();
 }
 
+void BytesOutputStyle::dumpNameMap() {
+  printHeader(P, "Named Stream Map");
+
+  AutoIndent Indent(P);
+
+  auto &InfoS = Err(File.getPDBInfoStream());
+  BinarySubstreamRef NS = InfoS.getNamedStreamsBuffer();
+  auto Layout = File.getStreamLayout(StreamPDB);
+  P.formatMsfStreamData("Named Stream Map", File, Layout, NS);
+}
+
 void BytesOutputStyle::dumpBlockRanges(uint32_t Min, uint32_t Max) {
   printHeader(P, "MSF Blocks");
 
index a2cefbb..4cf6937 100644 (file)
@@ -28,12 +28,14 @@ public:
   Error dump() override;
 
 private:
+  void dumpNameMap();
   void dumpBlockRanges(uint32_t Min, uint32_t Max);
   void dumpByteRanges(uint32_t Min, uint32_t Max);
   void dumpStreamBytes();
 
   PDBFile &File;
   LinePrinter P;
+  ExitOnError Err;
   SmallVector<std::string, 8> StreamPurposes;
 };
 } // namespace pdb
index a5f48f0..a9761b4 100644 (file)
@@ -203,11 +203,12 @@ void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
              StreamPurpose, Size, S->getLength());
   AutoIndent Indent(*this);
   BinaryStreamRef Slice(*S);
-  Slice = Slice.keep_front(Offset + Size);
-  BinaryStreamReader Reader(Slice);
-  consumeError(Reader.skip(Offset));
+  BinarySubstreamRef Substream;
+  Substream.Offset = Offset;
+  Substream.StreamData = Slice.drop_front(Offset).keep_front(Size);
+
   auto Layout = File.getStreamLayout(StreamIdx);
-  formatMsfStreamData(Label, File, Layout, Reader);
+  formatMsfStreamData(Label, File, Layout, Substream);
 }
 
 void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
@@ -215,13 +216,6 @@ void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
                                       BinarySubstreamRef Substream) {
   BinaryStreamReader Reader(Substream.StreamData);
 
-  consumeError(Reader.skip(Substream.Offset));
-  formatMsfStreamData(Label, File, Stream, Reader);
-}
-
-void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
-                                      const msf::MSFStreamLayout &Stream,
-                                      BinaryStreamReader &Reader) {
   auto Runs = computeBlockRuns(File.getBlockSize(), Stream);
 
   NewLine();
@@ -231,7 +225,7 @@ void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
 
     Run FoundRun;
     uint32_t RunOffset;
-    std::tie(FoundRun, RunOffset) = findRun(Reader.getOffset(), Runs);
+    std::tie(FoundRun, RunOffset) = findRun(Substream.Offset, Runs);
     assert(FoundRun.ByteLen >= RunOffset);
     uint32_t Len = FoundRun.ByteLen - RunOffset;
     Len = std::min(Len, Reader.bytesRemaining());
@@ -245,6 +239,7 @@ void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
       OS << formatv("  {0}",
                     fmt_align("<discontinuity>", AlignStyle::Center, 114, '-'));
     }
+    Substream.Offset += Len;
   }
   NewLine();
   OS << ")";
index 7ce410d..68ce321 100644 (file)
@@ -60,9 +60,6 @@ public:
   void formatMsfStreamData(StringRef Label, PDBFile &File,
                            const msf::MSFStreamLayout &Stream,
                            BinarySubstreamRef Substream);
-  void formatMsfStreamData(StringRef Label, PDBFile &File,
-                           const msf::MSFStreamLayout &Stream,
-                           BinaryStreamReader &Reader);
 
   bool hasColor() const { return UseColor; }
   raw_ostream &getStream() { return OS; }
index bdd8dfa..6204594 100644 (file)
@@ -286,6 +286,9 @@ cl::list<std::string>
                             "is SN[:Start][@Size]"),
                    cl::sub(BytesSubcommand));
 
+cl::opt<bool> NameMap("name-map", cl::desc("Dump bytes of PDB Name Map"),
+                      cl::sub(BytesSubcommand));
+
 cl::list<std::string> InputFilenames(cl::Positional,
                                      cl::desc("<input PDB files>"),
                                      cl::OneOrMore, cl::sub(BytesSubcommand));
index 78cea8f..dc1bbe9 100644 (file)
@@ -101,6 +101,7 @@ struct NumberRange {
 extern llvm::Optional<NumberRange> DumpBlockRange;
 extern llvm::Optional<NumberRange> DumpByteRange;
 extern llvm::cl::list<std::string> DumpStreamData;
+extern llvm::cl::opt<bool> NameMap;
 } // namespace bytes
 
 namespace dump {