Skip to content

Commit

Permalink
[pdb] Write the Named Stream mapping to Yaml and binary.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D28919

llvm-svn: 292665
  • Loading branch information
Zachary Turner committed Jan 20, 2017
1 parent 60667ca commit 760ad4d
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 44 deletions.
1 change: 1 addition & 0 deletions lld/COFF/PDB.cpp
Expand Up @@ -29,6 +29,7 @@
#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
#include "llvm/DebugInfo/PDB/Raw/StringTableBuilder.h"
#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
#include "llvm/Object/COFF.h"
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/DebugInfo/PDB/Raw/InfoStream.h
Expand Up @@ -37,6 +37,8 @@ class InfoStream {
uint32_t getAge() const;
PDB_UniqueId getGuid() const;

const NamedStreamMap &getNamedStreams() const;

uint32_t getNamedStreamIndex(llvm::StringRef Name) const;
iterator_range<StringMapConstIterator<uint32_t>> named_streams() const;

Expand Down
7 changes: 3 additions & 4 deletions llvm/include/llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h
Expand Up @@ -25,10 +25,11 @@ class StreamWriter;
}
namespace pdb {
class PDBFile;
class NamedStreamMap;

class InfoStreamBuilder {
public:
InfoStreamBuilder(msf::MSFBuilder &Msf);
InfoStreamBuilder(msf::MSFBuilder &Msf, NamedStreamMap &NamedStreams);
InfoStreamBuilder(const InfoStreamBuilder &) = delete;
InfoStreamBuilder &operator=(const InfoStreamBuilder &) = delete;

Expand All @@ -37,8 +38,6 @@ class InfoStreamBuilder {
void setAge(uint32_t A);
void setGuid(PDB_UniqueId G);

NamedStreamMap &getNamedStreamsBuilder();

uint32_t finalize();

Error finalizeMsfLayout();
Expand All @@ -54,7 +53,7 @@ class InfoStreamBuilder {
uint32_t Age;
PDB_UniqueId Guid;

NamedStreamMap NamedStreams;
NamedStreamMap &NamedStreams;
};
}
}
Expand Down
9 changes: 8 additions & 1 deletion llvm/include/llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h
Expand Up @@ -13,8 +13,10 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/Optional.h"
#include "llvm/DebugInfo/PDB/Raw/NamedStreamMap.h"
#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
#include "llvm/DebugInfo/PDB/Raw/StringTableBuilder.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
Expand Down Expand Up @@ -44,11 +46,13 @@ class PDBFileBuilder {
DbiStreamBuilder &getDbiBuilder();
TpiStreamBuilder &getTpiBuilder();
TpiStreamBuilder &getIpiBuilder();
StringTableBuilder &getStringTableBuilder();

Error commit(StringRef Filename);

private:
Expected<msf::MSFLayout> finalizeMsfLayout() const;
Error addNamedStream(StringRef Name, uint32_t Size);
Expected<msf::MSFLayout> finalizeMsfLayout();

BumpPtrAllocator &Allocator;

Expand All @@ -57,6 +61,9 @@ class PDBFileBuilder {
std::unique_ptr<DbiStreamBuilder> Dbi;
std::unique_ptr<TpiStreamBuilder> Tpi;
std::unique_ptr<TpiStreamBuilder> Ipi;

StringTableBuilder Strings;
NamedStreamMap NamedStreams;
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/DebugInfo/PDB/Raw/StringTableBuilder.h
Expand Up @@ -31,7 +31,7 @@ class StringTableBuilder {
// Returns the ID for S.
uint32_t insert(StringRef S);

uint32_t calculateSerializedLength() const;
uint32_t finalize();
Error commit(msf::StreamWriter &Writer) const;

private:
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/DebugInfo/PDB/Raw/InfoStream.cpp
Expand Up @@ -75,3 +75,7 @@ uint32_t InfoStream::getSignature() const { return Signature; }
uint32_t InfoStream::getAge() const { return Age; }

PDB_UniqueId InfoStream::getGuid() const { return Guid; }

const NamedStreamMap &InfoStream::getNamedStreams() const {
return NamedStreams;
}
12 changes: 6 additions & 6 deletions llvm/lib/DebugInfo/PDB/Raw/InfoStreamBuilder.cpp
Expand Up @@ -13,6 +13,8 @@
#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
#include "llvm/DebugInfo/MSF/StreamWriter.h"
#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
#include "llvm/DebugInfo/PDB/Raw/NamedStreamMap.h"
#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
#include "llvm/DebugInfo/PDB/Raw/RawError.h"
#include "llvm/DebugInfo/PDB/Raw/RawTypes.h"

Expand All @@ -21,8 +23,10 @@ using namespace llvm::codeview;
using namespace llvm::msf;
using namespace llvm::pdb;

InfoStreamBuilder::InfoStreamBuilder(msf::MSFBuilder &Msf)
: Msf(Msf), Ver(PdbRaw_ImplVer::PdbImplVC70), Sig(-1), Age(0) {}
InfoStreamBuilder::InfoStreamBuilder(msf::MSFBuilder &Msf,
NamedStreamMap &NamedStreams)
: Msf(Msf), Ver(PdbRaw_ImplVer::PdbImplVC70), Sig(-1), Age(0),
NamedStreams(NamedStreams) {}

void InfoStreamBuilder::setVersion(PdbRaw_ImplVer V) { Ver = V; }

Expand All @@ -32,10 +36,6 @@ void InfoStreamBuilder::setAge(uint32_t A) { Age = A; }

void InfoStreamBuilder::setGuid(PDB_UniqueId G) { Guid = G; }

NamedStreamMap &InfoStreamBuilder::getNamedStreamsBuilder() {
return NamedStreams;
}

Error InfoStreamBuilder::finalizeMsfLayout() {
uint32_t Length = sizeof(InfoStreamHeader) + NamedStreams.finalize();
if (auto EC = Msf.setStreamSize(StreamPDB, Length))
Expand Down
34 changes: 32 additions & 2 deletions llvm/lib/DebugInfo/PDB/Raw/PDBFileBuilder.cpp
Expand Up @@ -20,6 +20,7 @@
#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
#include "llvm/DebugInfo/PDB/Raw/RawError.h"
#include "llvm/DebugInfo/PDB/Raw/StringTableBuilder.h"
#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"

Expand All @@ -44,7 +45,7 @@ MSFBuilder &PDBFileBuilder::getMsfBuilder() { return *Msf; }

InfoStreamBuilder &PDBFileBuilder::getInfoBuilder() {
if (!Info)
Info = llvm::make_unique<InfoStreamBuilder>(*Msf);
Info = llvm::make_unique<InfoStreamBuilder>(*Msf, NamedStreams);
return *Info;
}

Expand All @@ -66,7 +67,26 @@ TpiStreamBuilder &PDBFileBuilder::getIpiBuilder() {
return *Ipi;
}

Expected<msf::MSFLayout> PDBFileBuilder::finalizeMsfLayout() const {
StringTableBuilder &PDBFileBuilder::getStringTableBuilder() { return Strings; }

Error PDBFileBuilder::addNamedStream(StringRef Name, uint32_t Size) {
auto ExpectedStream = Msf->addStream(Size);
if (!ExpectedStream)
return ExpectedStream.takeError();
NamedStreams.set(Name, *ExpectedStream);
return Error::success();
}

Expected<msf::MSFLayout> PDBFileBuilder::finalizeMsfLayout() {
uint32_t StringTableSize = Strings.finalize();

if (auto EC = addNamedStream("/names", StringTableSize))
return std::move(EC);
if (auto EC = addNamedStream("/LinkInfo", 0))
return std::move(EC);
if (auto EC = addNamedStream("/src/headerblock", 0))
return std::move(EC);

if (Info) {
if (auto EC = Info->finalizeMsfLayout())
return std::move(EC);
Expand Down Expand Up @@ -124,6 +144,16 @@ Error PDBFileBuilder::commit(StringRef Filename) {
return EC;
}

uint32_t StringTableStreamNo = 0;
if (!NamedStreams.get("/names", StringTableStreamNo))
return llvm::make_error<pdb::RawError>(raw_error_code::no_stream);

auto NS = WritableMappedBlockStream::createIndexedStream(Layout, Buffer,
StringTableStreamNo);
StreamWriter NSWriter(*NS);
if (auto EC = Strings.commit(NSWriter))
return EC;

if (Info) {
if (auto EC = Info->commit(Layout, Buffer))
return EC;
Expand Down
9 changes: 5 additions & 4 deletions llvm/lib/DebugInfo/PDB/Raw/StringTableBuilder.cpp
Expand Up @@ -38,16 +38,17 @@ static uint32_t computeBucketCount(uint32_t NumStrings) {
return (NumStrings + 1) * 1.25;
}

uint32_t StringTableBuilder::calculateSerializedLength() const {
uint32_t StringTableBuilder::finalize() {
uint32_t Size = 0;
Size += sizeof(StringTableHeader);
Size += StringSize;
Size += 4; // Hash table begins with 4-byte size field.
Size += sizeof(uint32_t); // Hash table begins with 4-byte size field.

uint32_t BucketCount = computeBucketCount(Strings.size());
Size += BucketCount * 4;
Size += BucketCount * sizeof(uint32_t);

Size += 4; // The /names stream ends with the number of strings.
Size +=
sizeof(uint32_t); // The /names stream ends with the number of strings.
return Size;
}

Expand Down
17 changes: 16 additions & 1 deletion llvm/test/DebugInfo/PDB/pdbdump-headers.test
@@ -1,4 +1,4 @@
; RUN: llvm-pdbdump raw -headers -tpi-records -tpi-record-bytes -module-syms \
; RUN: llvm-pdbdump raw -headers -string-table -tpi-records -tpi-record-bytes -module-syms \
; RUN: -sym-record-bytes -globals -publics -module-files \
; RUN: -stream-summary -stream-blocks -ipi-records -ipi-record-bytes \
; RUN: -section-contribs -section-map -section-headers -line-info \
Expand Down Expand Up @@ -61,11 +61,21 @@
; EMPTY-NEXT: Stream 15: [21]
; EMPTY-NEXT: Stream 16: [22]
; EMPTY-NEXT: ]
; EMPTY-NEXT: String Table {
; EMPTY-NEXT: 'd:\src\llvm\test\debuginfo\pdb\inputs\predefined c++ attributes (compiler internal)'
; EMPTY-NEXT: 'd:\src\llvm\test\debuginfo\pdb\inputs\empty.cpp'
; EMPTY-NEXT: '$T0 $ebp = $eip $T0 4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = '
; EMPTY-NEXT: }
; EMPTY-NEXT: PDB Stream {
; EMPTY-NEXT: Version: 20000404
; EMPTY-NEXT: Signature: 0x54E507E2
; EMPTY-NEXT: Age: 1
; EMPTY-NEXT: Guid: {0B355641-86A0-A249-896F-9988FAE52FF0}
; EMPTY-NEXT: Named Streams {
; EMPTY-NEXT: /names: 13
; EMPTY-NEXT: /LinkInfo: 5
; EMPTY-NEXT: /src/headerblock: 9
; EMPTY-NEXT: }
; EMPTY-NEXT: }
; EMPTY-NEXT: Type Info Stream (TPI) {
; EMPTY-NEXT: TPI Version: 20040203
Expand Down Expand Up @@ -1692,6 +1702,11 @@
; BIG-NEXT: Signature: 0x571FFE67
; BIG-NEXT: Age: 1
; BIG-NEXT: Guid: {880ECC89-DF81-0B4F-839C-58CBD052E937}
; BIG-NEXT: Named Streams {
; BIG-NEXT: /names: 13
; BIG-NEXT: /LinkInfo: 5
; BIG-NEXT: /src/headerblock: 61
; BIG-NEXT: }
; BIG-NEXT: }
; BIG-NEXT: DBI Stream {
; BIG-NEXT: Dbi Version: 19990903
Expand Down
14 changes: 11 additions & 3 deletions llvm/test/DebugInfo/PDB/pdbdump-readwrite.test
@@ -1,10 +1,10 @@
RUN: llvm-pdbdump pdb2yaml -dbi-module-info -dbi-module-source-info \
RUN: -dbi-stream -pdb-stream -tpi-stream -stream-directory \
RUN: -dbi-stream -pdb-stream -string-table -tpi-stream -stream-directory \
RUN: -stream-metadata %p/Inputs/empty.pdb > %t.1
RUN: llvm-pdbdump yaml2pdb -pdb=%t.2 %t.1

RUN: llvm-pdbdump raw -headers -tpi-records %p/Inputs/empty.pdb | FileCheck %s
RUN: llvm-pdbdump raw -headers -tpi-records %t.2 | FileCheck %s
RUN: llvm-pdbdump raw -headers -string-table -tpi-records %p/Inputs/empty.pdb | FileCheck %s
RUN: llvm-pdbdump raw -headers -string-table -tpi-records %t.2 | FileCheck %s

CHECK: FileHeaders {
CHECK-NEXT: BlockSize: 4096
Expand All @@ -17,11 +17,19 @@ CHECK-NEXT: NumDirectoryBlocks: 1
CHECK-NEXT: DirectoryBlocks:
CHECK-NEXT: NumStreams:
CHECK-NEXT: }
CHECK: String Table {
CHECK-DAG: 'd:\src\llvm\test\debuginfo\pdb\inputs\predefined c++ attributes (compiler internal)'
CHECK-DAG: 'd:\src\llvm\test\debuginfo\pdb\inputs\empty.cpp'
CHECK-DAG: '$T0 $ebp = $eip $T0 4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = '
CHECK-NEXT: }
CHECK: PDB Stream {
CHECK-NEXT: Version: 20000404
CHECK-NEXT: Signature: 0x54E507E2
CHECK-NEXT: Age: 1
CHECK-NEXT: Guid: {0B355641-86A0-A249-896F-9988FAE52FF0}
CHECK-NEXT: Named Streams {
CHECK: /names:
CHECK: }
CHECK-NEXT: }
CHECK: Type Info Stream (TPI) {
CHECK-NEXT: TPI Version: 20040203
Expand Down
9 changes: 6 additions & 3 deletions llvm/test/DebugInfo/PDB/pdbdump-write.test
Expand Up @@ -10,8 +10,11 @@
; stream metadata, since the layout of the MSF file might be different
; (for example if we don't write the entire stream)
;
; RUN: llvm-pdbdump pdb2yaml -stream-metadata -stream-directory -pdb-stream -tpi-stream %p/Inputs/empty.pdb > %t.1
; RUN: llvm-pdbdump pdb2yaml -stream-metadata -stream-directory \
; RUN: -pdb-stream -tpi-stream %p/Inputs/empty.pdb > %t.1
; RUN: llvm-pdbdump yaml2pdb -pdb=%t.2 %t.1
; RUN: llvm-pdbdump pdb2yaml -pdb-stream -tpi-stream -no-file-headers %p/Inputs/empty.pdb > %t.3
; RUN: llvm-pdbdump pdb2yaml -pdb-stream -tpi-stream -no-file-headers %t.2 > %t.4
; RUN: llvm-pdbdump pdb2yaml -pdb-stream -tpi-stream \
; RUN: -no-file-headers %p/Inputs/empty.pdb > %t.3
; RUN: llvm-pdbdump pdb2yaml -pdb-stream -tpi-stream \
; RUN: -no-file-headers %t.2 > %t.4
; RUN: diff %t.3 %t.4
15 changes: 6 additions & 9 deletions llvm/test/DebugInfo/PDB/pdbdump-yaml.test
@@ -1,5 +1,5 @@
; RUN: llvm-pdbdump pdb2yaml -stream-metadata -stream-directory -pdb-stream %p/Inputs/empty.pdb \
; RUN: | FileCheck -check-prefix=YAML %s
; RUN: llvm-pdbdump pdb2yaml -stream-metadata -stream-directory -string-table -pdb-stream \
; RUN: %p/Inputs/empty.pdb | FileCheck -check-prefix=YAML %s
; RUN: llvm-pdbdump pdb2yaml -no-file-headers -stream-metadata -stream-directory -pdb-stream \
; RUN: %p/Inputs/empty.pdb | FileCheck -check-prefix=NO-HEADERS %s

Expand Down Expand Up @@ -36,18 +36,15 @@
; YAML-NEXT: - Stream: [ 7 ]
; YAML-NEXT: - Stream: [ 21 ]
; YAML-NEXT: - Stream: [ 22 ]
; YAML-NEXT: StringTable:
; YAML-NEXT: - 'd:\src\llvm\test\debuginfo\pdb\inputs\predefined c++ attributes (compiler internal)'
; YAML-NEXT: - 'd:\src\llvm\test\debuginfo\pdb\inputs\empty.cpp'
; YAML-NEXT: - '$T0 $ebp = $eip $T0 4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = '
; YAML-NEXT: PdbStream:
; YAML-NEXT: Age: 1
; YAML-NEXT: Guid: '{0B355641-86A0-A249-896F-9988FAE52FF0}'
; YAML-NEXT: Signature: 1424295906
; YAML-NEXT: Version: VC70
; YAML-NEXT: NamedStreams:
; YAML-NEXT: - Name: /names
; YAML-NEXT: StreamNum: 13
; YAML-NEXT: - Name: /LinkInfo
; YAML-NEXT: StreamNum: 5
; YAML-NEXT: - Name: /src/headerblock
; YAML-NEXT: StreamNum: 9
; YAML-NEXT: ...

; NO-HEADERS: ---
Expand Down
30 changes: 30 additions & 0 deletions llvm/tools/llvm-pdbdump/LLVMOutputStyle.cpp
Expand Up @@ -113,6 +113,9 @@ Error LLVMOutputStyle::dump() {
if (auto EC = dumpStreamBytes())
return EC;

if (auto EC = dumpStringTable())
return EC;

if (auto EC = dumpInfoStream())
return EC;

Expand Down Expand Up @@ -456,6 +459,28 @@ Error LLVMOutputStyle::dumpStreamBytes() {
return Error::success();
}

Error LLVMOutputStyle::dumpStringTable() {
if (!opts::raw::DumpStringTable)
return Error::success();

auto IS = File.getStringTable();
if (!IS)
return IS.takeError();

DictScope D(P, "String Table");
for (uint32_t I : IS->name_ids()) {
StringRef S = IS->getStringForID(I);
if (!S.empty()) {
llvm::SmallString<32> Str;
Str.append("'");
Str.append(S);
Str.append("'");
P.printString(Str);
}
}
return Error::success();
}

Error LLVMOutputStyle::dumpInfoStream() {
if (!opts::raw::DumpHeaders)
return Error::success();
Expand All @@ -472,6 +497,11 @@ Error LLVMOutputStyle::dumpInfoStream() {
P.printHex("Signature", IS->getSignature());
P.printNumber("Age", IS->getAge());
P.printObject("Guid", IS->getGuid());
{
DictScope DD(P, "Named Streams");
for (const auto &S : IS->getNamedStreams().entries())
P.printObject(S.getKey(), S.getValue());
}
return Error::success();
}

Expand Down
1 change: 1 addition & 0 deletions llvm/tools/llvm-pdbdump/LLVMOutputStyle.h
Expand Up @@ -34,6 +34,7 @@ class LLVMOutputStyle : public OutputStyle {
Error dumpGlobalsStream();
Error dumpStreamBytes();
Error dumpStreamBlocks();
Error dumpStringTable();
Error dumpInfoStream();
Error dumpTpiStream(uint32_t StreamIdx);
Error dumpDbiStream();
Expand Down

0 comments on commit 760ad4d

Please sign in to comment.