Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 1 | //===- LLVMOutputStyle.cpp ------------------------------------ *- C++ --*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "LLVMOutputStyle.h" |
| 11 | |
| 12 | #include "llvm-pdbdump.h" |
| 13 | #include "llvm/DebugInfo/CodeView/EnumTables.h" |
| 14 | #include "llvm/DebugInfo/CodeView/ModuleSubstreamVisitor.h" |
| 15 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" |
| 16 | #include "llvm/DebugInfo/PDB/PDBExtras.h" |
| 17 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
| 18 | #include "llvm/DebugInfo/PDB/Raw/EnumTables.h" |
| 19 | #include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
| 22 | #include "llvm/DebugInfo/PDB/Raw/ModInfo.h" |
| 23 | #include "llvm/DebugInfo/PDB/Raw/ModStream.h" |
| 24 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
| 25 | #include "llvm/DebugInfo/PDB/Raw/PublicsStream.h" |
| 26 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
| 27 | #include "llvm/DebugInfo/PDB/Raw/TpiStream.h" |
| 28 | #include "llvm/Object/COFF.h" |
| 29 | |
| 30 | #include <unordered_map> |
| 31 | |
| 32 | using namespace llvm; |
| 33 | using namespace llvm::codeview; |
| 34 | using namespace llvm::pdb; |
| 35 | |
| 36 | static void printSectionOffset(llvm::raw_ostream &OS, |
| 37 | const SectionOffset &Off) { |
| 38 | OS << Off.Off << ", " << Off.Isect; |
| 39 | } |
| 40 | |
| 41 | LLVMOutputStyle::LLVMOutputStyle(PDBFile &File) |
| 42 | : File(File), P(outs()), TD(&P, false) {} |
| 43 | |
| 44 | Error LLVMOutputStyle::dumpFileHeaders() { |
| 45 | if (!opts::DumpHeaders) |
| 46 | return Error::success(); |
| 47 | |
| 48 | DictScope D(P, "FileHeaders"); |
| 49 | P.printNumber("BlockSize", File.getBlockSize()); |
| 50 | P.printNumber("Unknown0", File.getUnknown0()); |
| 51 | P.printNumber("NumBlocks", File.getBlockCount()); |
| 52 | P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes()); |
| 53 | P.printNumber("Unknown1", File.getUnknown1()); |
| 54 | P.printNumber("BlockMapAddr", File.getBlockMapIndex()); |
| 55 | P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks()); |
| 56 | P.printNumber("BlockMapOffset", File.getBlockMapOffset()); |
| 57 | |
| 58 | // The directory is not contiguous. Instead, the block map contains a |
| 59 | // contiguous list of block numbers whose contents, when concatenated in |
| 60 | // order, make up the directory. |
| 61 | P.printList("DirectoryBlocks", File.getDirectoryBlockArray()); |
| 62 | P.printNumber("NumStreams", File.getNumStreams()); |
| 63 | return Error::success(); |
| 64 | } |
| 65 | |
| 66 | Error LLVMOutputStyle::dumpStreamSummary() { |
| 67 | if (!opts::DumpStreamSummary) |
| 68 | return Error::success(); |
| 69 | |
| 70 | auto DbiS = File.getPDBDbiStream(); |
| 71 | if (auto EC = DbiS.takeError()) |
| 72 | return EC; |
| 73 | auto TpiS = File.getPDBTpiStream(); |
| 74 | if (auto EC = TpiS.takeError()) |
| 75 | return EC; |
| 76 | auto IpiS = File.getPDBIpiStream(); |
| 77 | if (auto EC = IpiS.takeError()) |
| 78 | return EC; |
| 79 | auto InfoS = File.getPDBInfoStream(); |
| 80 | if (auto EC = InfoS.takeError()) |
| 81 | return EC; |
| 82 | DbiStream &DS = DbiS.get(); |
| 83 | TpiStream &TS = TpiS.get(); |
| 84 | TpiStream &TIS = IpiS.get(); |
| 85 | InfoStream &IS = InfoS.get(); |
| 86 | |
| 87 | ListScope L(P, "Streams"); |
| 88 | uint32_t StreamCount = File.getNumStreams(); |
| 89 | std::unordered_map<uint16_t, const ModuleInfoEx *> ModStreams; |
| 90 | std::unordered_map<uint16_t, std::string> NamedStreams; |
| 91 | |
| 92 | for (auto &ModI : DS.modules()) { |
| 93 | uint16_t SN = ModI.Info.getModuleStreamIndex(); |
| 94 | ModStreams[SN] = &ModI; |
| 95 | } |
| 96 | for (auto &NSE : IS.named_streams()) { |
| 97 | NamedStreams[NSE.second] = NSE.first(); |
| 98 | } |
| 99 | |
| 100 | for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
| 101 | std::string Label("Stream "); |
| 102 | Label += to_string(StreamIdx); |
| 103 | std::string Value; |
| 104 | if (StreamIdx == OldMSFDirectory) |
| 105 | Value = "Old MSF Directory"; |
| 106 | else if (StreamIdx == StreamPDB) |
| 107 | Value = "PDB Stream"; |
| 108 | else if (StreamIdx == StreamDBI) |
| 109 | Value = "DBI Stream"; |
| 110 | else if (StreamIdx == StreamTPI) |
| 111 | Value = "TPI Stream"; |
| 112 | else if (StreamIdx == StreamIPI) |
| 113 | Value = "IPI Stream"; |
| 114 | else if (StreamIdx == DS.getGlobalSymbolStreamIndex()) |
| 115 | Value = "Global Symbol Hash"; |
| 116 | else if (StreamIdx == DS.getPublicSymbolStreamIndex()) |
| 117 | Value = "Public Symbol Hash"; |
| 118 | else if (StreamIdx == DS.getSymRecordStreamIndex()) |
| 119 | Value = "Public Symbol Records"; |
| 120 | else if (StreamIdx == TS.getTypeHashStreamIndex()) |
| 121 | Value = "TPI Hash"; |
| 122 | else if (StreamIdx == TS.getTypeHashStreamAuxIndex()) |
| 123 | Value = "TPI Aux Hash"; |
| 124 | else if (StreamIdx == TIS.getTypeHashStreamIndex()) |
| 125 | Value = "IPI Hash"; |
| 126 | else if (StreamIdx == TIS.getTypeHashStreamAuxIndex()) |
| 127 | Value = "IPI Aux Hash"; |
| 128 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::Exception)) |
| 129 | Value = "Exception Data"; |
| 130 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::Fixup)) |
| 131 | Value = "Fixup Data"; |
| 132 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::FPO)) |
| 133 | Value = "FPO Data"; |
| 134 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::NewFPO)) |
| 135 | Value = "New FPO Data"; |
| 136 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::OmapFromSrc)) |
| 137 | Value = "Omap From Source Data"; |
| 138 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::OmapToSrc)) |
| 139 | Value = "Omap To Source Data"; |
| 140 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::Pdata)) |
| 141 | Value = "Pdata"; |
| 142 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::SectionHdr)) |
| 143 | Value = "Section Header Data"; |
| 144 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::SectionHdrOrig)) |
| 145 | Value = "Section Header Original Data"; |
| 146 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::TokenRidMap)) |
| 147 | Value = "Token Rid Data"; |
| 148 | else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::Xdata)) |
| 149 | Value = "Xdata"; |
| 150 | else { |
| 151 | auto ModIter = ModStreams.find(StreamIdx); |
| 152 | auto NSIter = NamedStreams.find(StreamIdx); |
| 153 | if (ModIter != ModStreams.end()) { |
| 154 | Value = "Module \""; |
| 155 | Value += ModIter->second->Info.getModuleName().str(); |
| 156 | Value += "\""; |
| 157 | } else if (NSIter != NamedStreams.end()) { |
| 158 | Value = "Named Stream \""; |
| 159 | Value += NSIter->second; |
| 160 | Value += "\""; |
| 161 | } else { |
| 162 | Value = "???"; |
| 163 | } |
| 164 | } |
| 165 | Value = "[" + Value + "]"; |
| 166 | Value = |
| 167 | Value + " (" + to_string(File.getStreamByteSize(StreamIdx)) + " bytes)"; |
| 168 | |
| 169 | P.printString(Label, Value); |
| 170 | } |
| 171 | P.flush(); |
| 172 | return Error::success(); |
| 173 | } |
| 174 | |
| 175 | Error LLVMOutputStyle::dumpStreamBlocks() { |
| 176 | if (!opts::DumpStreamBlocks) |
| 177 | return Error::success(); |
| 178 | |
| 179 | ListScope L(P, "StreamBlocks"); |
| 180 | uint32_t StreamCount = File.getNumStreams(); |
| 181 | for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
| 182 | std::string Name("Stream "); |
| 183 | Name += to_string(StreamIdx); |
| 184 | auto StreamBlocks = File.getStreamBlockList(StreamIdx); |
| 185 | P.printList(Name, StreamBlocks); |
| 186 | } |
| 187 | return Error::success(); |
| 188 | } |
| 189 | |
| 190 | Error LLVMOutputStyle::dumpStreamData() { |
| 191 | uint32_t StreamCount = File.getNumStreams(); |
| 192 | StringRef DumpStreamStr = opts::DumpStreamDataIdx; |
| 193 | uint32_t DumpStreamNum; |
| 194 | if (DumpStreamStr.getAsInteger(/*Radix=*/0U, DumpStreamNum) || |
| 195 | DumpStreamNum >= StreamCount) |
| 196 | return Error::success(); |
| 197 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 198 | MappedBlockStream S(llvm::make_unique<IndexedStreamData>(DumpStreamNum, File), |
| 199 | File); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 200 | codeview::StreamReader R(S); |
| 201 | while (R.bytesRemaining() > 0) { |
| 202 | ArrayRef<uint8_t> Data; |
| 203 | uint32_t BytesToReadInBlock = std::min( |
| 204 | R.bytesRemaining(), static_cast<uint32_t>(File.getBlockSize())); |
| 205 | if (auto EC = R.readBytes(Data, BytesToReadInBlock)) |
| 206 | return EC; |
| 207 | P.printBinaryBlock( |
| 208 | "Data", |
| 209 | StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size())); |
| 210 | } |
| 211 | return Error::success(); |
| 212 | } |
| 213 | |
| 214 | Error LLVMOutputStyle::dumpInfoStream() { |
| 215 | if (!opts::DumpHeaders) |
| 216 | return Error::success(); |
| 217 | auto InfoS = File.getPDBInfoStream(); |
| 218 | if (auto EC = InfoS.takeError()) |
| 219 | return EC; |
| 220 | |
| 221 | InfoStream &IS = InfoS.get(); |
| 222 | |
| 223 | DictScope D(P, "PDB Stream"); |
| 224 | P.printNumber("Version", IS.getVersion()); |
| 225 | P.printHex("Signature", IS.getSignature()); |
| 226 | P.printNumber("Age", IS.getAge()); |
| 227 | P.printObject("Guid", IS.getGuid()); |
| 228 | return Error::success(); |
| 229 | } |
| 230 | |
| 231 | Error LLVMOutputStyle::dumpNamedStream() { |
| 232 | if (opts::DumpStreamDataName.empty()) |
| 233 | return Error::success(); |
| 234 | |
| 235 | auto InfoS = File.getPDBInfoStream(); |
| 236 | if (auto EC = InfoS.takeError()) |
| 237 | return EC; |
| 238 | InfoStream &IS = InfoS.get(); |
| 239 | |
| 240 | uint32_t NameStreamIndex = IS.getNamedStreamIndex(opts::DumpStreamDataName); |
| 241 | |
| 242 | if (NameStreamIndex != 0) { |
| 243 | std::string Name("Stream '"); |
| 244 | Name += opts::DumpStreamDataName; |
| 245 | Name += "'"; |
| 246 | DictScope D(P, Name); |
| 247 | P.printNumber("Index", NameStreamIndex); |
| 248 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 249 | MappedBlockStream NameStream( |
| 250 | llvm::make_unique<IndexedStreamData>(NameStreamIndex, File), File); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 251 | codeview::StreamReader Reader(NameStream); |
| 252 | |
| 253 | NameHashTable NameTable; |
| 254 | if (auto EC = NameTable.load(Reader)) |
| 255 | return EC; |
| 256 | |
| 257 | P.printHex("Signature", NameTable.getSignature()); |
| 258 | P.printNumber("Version", NameTable.getHashVersion()); |
| 259 | P.printNumber("Name Count", NameTable.getNameCount()); |
| 260 | ListScope L(P, "Names"); |
| 261 | for (uint32_t ID : NameTable.name_ids()) { |
| 262 | StringRef Str = NameTable.getStringForID(ID); |
| 263 | if (!Str.empty()) |
| 264 | P.printString(to_string(ID), Str); |
| 265 | } |
| 266 | } |
| 267 | return Error::success(); |
| 268 | } |
| 269 | |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 270 | static void printTypeIndexOffset(raw_ostream &OS, |
| 271 | const TypeIndexOffset &TIOff) { |
| 272 | OS << "{" << TIOff.Type.getIndex() << ", " << TIOff.Offset << "}"; |
| 273 | } |
| 274 | |
| 275 | static void dumpTpiHash(ScopedPrinter &P, TpiStream &Tpi) { |
| 276 | if (!opts::DumpTpiHash) |
| 277 | return; |
| 278 | DictScope DD(P, "Hash"); |
Rui Ueyama | f14a74c | 2016-06-07 23:53:43 +0000 | [diff] [blame^] | 279 | P.printNumber("Number of Hash Buckets", Tpi.NumHashBuckets()); |
Rui Ueyama | d833917 | 2016-06-07 23:44:27 +0000 | [diff] [blame] | 280 | P.printNumber("Hash Key Size", Tpi.getHashKeySize()); |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 281 | codeview::FixedStreamArray<support::ulittle32_t> S = Tpi.getHashValues(); |
| 282 | P.printList("Values", Tpi.getHashValues()); |
| 283 | P.printList("Type Index Offsets", Tpi.getTypeIndexOffsets(), |
| 284 | printTypeIndexOffset); |
| 285 | P.printList("Hash Adjustments", Tpi.getHashAdjustments(), |
| 286 | printTypeIndexOffset); |
| 287 | } |
| 288 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 289 | Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) { |
| 290 | assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI); |
| 291 | |
| 292 | bool DumpRecordBytes = false; |
| 293 | bool DumpRecords = false; |
| 294 | StringRef Label; |
| 295 | StringRef VerLabel; |
| 296 | if (StreamIdx == StreamTPI) { |
| 297 | DumpRecordBytes = opts::DumpTpiRecordBytes; |
| 298 | DumpRecords = opts::DumpTpiRecords; |
| 299 | Label = "Type Info Stream (TPI)"; |
| 300 | VerLabel = "TPI Version"; |
| 301 | } else if (StreamIdx == StreamIPI) { |
| 302 | DumpRecordBytes = opts::DumpIpiRecordBytes; |
| 303 | DumpRecords = opts::DumpIpiRecords; |
| 304 | Label = "Type Info Stream (IPI)"; |
| 305 | VerLabel = "IPI Version"; |
| 306 | } |
| 307 | if (!DumpRecordBytes && !DumpRecords && !opts::DumpModuleSyms) |
| 308 | return Error::success(); |
| 309 | |
| 310 | auto TpiS = (StreamIdx == StreamTPI) ? File.getPDBTpiStream() |
| 311 | : File.getPDBIpiStream(); |
| 312 | if (auto EC = TpiS.takeError()) |
| 313 | return EC; |
| 314 | TpiStream &Tpi = TpiS.get(); |
| 315 | |
| 316 | if (DumpRecords || DumpRecordBytes) { |
| 317 | DictScope D(P, Label); |
| 318 | |
| 319 | P.printNumber(VerLabel, Tpi.getTpiVersion()); |
| 320 | P.printNumber("Record count", Tpi.NumTypeRecords()); |
| 321 | |
| 322 | ListScope L(P, "Records"); |
| 323 | |
| 324 | bool HadError = false; |
| 325 | for (auto &Type : Tpi.types(&HadError)) { |
| 326 | DictScope DD(P, ""); |
| 327 | |
| 328 | if (DumpRecords) |
| 329 | TD.dump(Type); |
| 330 | |
| 331 | if (DumpRecordBytes) |
| 332 | P.printBinaryBlock("Bytes", Type.Data); |
| 333 | } |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 334 | dumpTpiHash(P, Tpi); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 335 | if (HadError) |
| 336 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 337 | "TPI stream contained corrupt record"); |
| 338 | } else if (opts::DumpModuleSyms) { |
| 339 | // Even if the user doesn't want to dump type records, we still need to |
| 340 | // iterate them in order to build the list of types so that we can print |
| 341 | // them when dumping module symbols. So when they want to dump symbols |
| 342 | // but not types, use a null output stream. |
| 343 | ScopedPrinter *OldP = TD.getPrinter(); |
| 344 | TD.setPrinter(nullptr); |
| 345 | |
| 346 | bool HadError = false; |
| 347 | for (auto &Type : Tpi.types(&HadError)) |
| 348 | TD.dump(Type); |
| 349 | |
| 350 | TD.setPrinter(OldP); |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 351 | dumpTpiHash(P, Tpi); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 352 | if (HadError) |
| 353 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 354 | "TPI stream contained corrupt record"); |
| 355 | } |
| 356 | P.flush(); |
| 357 | return Error::success(); |
| 358 | } |
| 359 | |
| 360 | Error LLVMOutputStyle::dumpDbiStream() { |
| 361 | bool DumpModules = opts::DumpModules || opts::DumpModuleSyms || |
| 362 | opts::DumpModuleFiles || opts::DumpLineInfo; |
| 363 | if (!opts::DumpHeaders && !DumpModules) |
| 364 | return Error::success(); |
| 365 | |
| 366 | auto DbiS = File.getPDBDbiStream(); |
| 367 | if (auto EC = DbiS.takeError()) |
| 368 | return EC; |
| 369 | DbiStream &DS = DbiS.get(); |
| 370 | |
| 371 | DictScope D(P, "DBI Stream"); |
| 372 | P.printNumber("Dbi Version", DS.getDbiVersion()); |
| 373 | P.printNumber("Age", DS.getAge()); |
| 374 | P.printBoolean("Incremental Linking", DS.isIncrementallyLinked()); |
| 375 | P.printBoolean("Has CTypes", DS.hasCTypes()); |
| 376 | P.printBoolean("Is Stripped", DS.isStripped()); |
| 377 | P.printObject("Machine Type", DS.getMachineType()); |
| 378 | P.printNumber("Symbol Record Stream Index", DS.getSymRecordStreamIndex()); |
| 379 | P.printNumber("Public Symbol Stream Index", DS.getPublicSymbolStreamIndex()); |
| 380 | P.printNumber("Global Symbol Stream Index", DS.getGlobalSymbolStreamIndex()); |
| 381 | |
| 382 | uint16_t Major = DS.getBuildMajorVersion(); |
| 383 | uint16_t Minor = DS.getBuildMinorVersion(); |
| 384 | P.printVersion("Toolchain Version", Major, Minor); |
| 385 | |
| 386 | std::string DllName; |
| 387 | raw_string_ostream DllStream(DllName); |
| 388 | DllStream << "mspdb" << Major << Minor << ".dll version"; |
| 389 | DllStream.flush(); |
| 390 | P.printVersion(DllName, Major, Minor, DS.getPdbDllVersion()); |
| 391 | |
| 392 | if (DumpModules) { |
| 393 | ListScope L(P, "Modules"); |
| 394 | for (auto &Modi : DS.modules()) { |
| 395 | DictScope DD(P); |
| 396 | P.printString("Name", Modi.Info.getModuleName().str()); |
| 397 | P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex()); |
| 398 | P.printString("Object File Name", Modi.Info.getObjFileName().str()); |
| 399 | P.printNumber("Num Files", Modi.Info.getNumberOfFiles()); |
| 400 | P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex()); |
| 401 | P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex()); |
| 402 | P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize()); |
| 403 | P.printNumber("C13 Line Info Byte Size", |
| 404 | Modi.Info.getC13LineInfoByteSize()); |
| 405 | P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize()); |
| 406 | P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex()); |
| 407 | P.printBoolean("Has EC Info", Modi.Info.hasECInfo()); |
| 408 | if (opts::DumpModuleFiles) { |
| 409 | std::string FileListName = |
| 410 | to_string(Modi.SourceFiles.size()) + " Contributing Source Files"; |
| 411 | ListScope LL(P, FileListName); |
| 412 | for (auto File : Modi.SourceFiles) |
| 413 | P.printString(File.str()); |
| 414 | } |
| 415 | bool HasModuleDI = |
| 416 | (Modi.Info.getModuleStreamIndex() < File.getNumStreams()); |
| 417 | bool ShouldDumpSymbols = |
| 418 | (opts::DumpModuleSyms || opts::DumpSymRecordBytes); |
| 419 | if (HasModuleDI && (ShouldDumpSymbols || opts::DumpLineInfo)) { |
| 420 | ModStream ModS(File, Modi.Info); |
| 421 | if (auto EC = ModS.reload()) |
| 422 | return EC; |
| 423 | |
| 424 | if (ShouldDumpSymbols) { |
| 425 | ListScope SS(P, "Symbols"); |
| 426 | codeview::CVSymbolDumper SD(P, TD, nullptr, false); |
| 427 | bool HadError = false; |
| 428 | for (const auto &S : ModS.symbols(&HadError)) { |
| 429 | DictScope DD(P, ""); |
| 430 | |
| 431 | if (opts::DumpModuleSyms) |
| 432 | SD.dump(S); |
| 433 | if (opts::DumpSymRecordBytes) |
| 434 | P.printBinaryBlock("Bytes", S.Data); |
| 435 | } |
| 436 | if (HadError) |
| 437 | return make_error<RawError>( |
| 438 | raw_error_code::corrupt_file, |
| 439 | "DBI stream contained corrupt symbol record"); |
| 440 | } |
| 441 | if (opts::DumpLineInfo) { |
| 442 | ListScope SS(P, "LineInfo"); |
| 443 | bool HadError = false; |
| 444 | // Define a locally scoped visitor to print the different |
| 445 | // substream types types. |
| 446 | class RecordVisitor : public codeview::IModuleSubstreamVisitor { |
| 447 | public: |
| 448 | RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {} |
| 449 | Error visitUnknown(ModuleSubstreamKind Kind, |
| 450 | StreamRef Stream) override { |
| 451 | DictScope DD(P, "Unknown"); |
| 452 | ArrayRef<uint8_t> Data; |
| 453 | StreamReader R(Stream); |
| 454 | if (auto EC = R.readBytes(Data, R.bytesRemaining())) { |
| 455 | return make_error<RawError>( |
| 456 | raw_error_code::corrupt_file, |
| 457 | "DBI stream contained corrupt line info record"); |
| 458 | } |
| 459 | P.printBinaryBlock("Data", Data); |
| 460 | return Error::success(); |
| 461 | } |
| 462 | Error |
| 463 | visitFileChecksums(StreamRef Data, |
| 464 | const FileChecksumArray &Checksums) override { |
| 465 | DictScope DD(P, "FileChecksums"); |
| 466 | for (const auto &C : Checksums) { |
| 467 | DictScope DDD(P, "Checksum"); |
| 468 | if (auto Result = getFileNameForOffset(C.FileNameOffset)) |
| 469 | P.printString("FileName", Result.get()); |
| 470 | else |
| 471 | return Result.takeError(); |
| 472 | P.flush(); |
| 473 | P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames()); |
| 474 | P.printBinaryBlock("Checksum", C.Checksum); |
| 475 | } |
| 476 | return Error::success(); |
| 477 | } |
| 478 | |
| 479 | Error visitLines(StreamRef Data, const LineSubstreamHeader *Header, |
| 480 | const LineInfoArray &Lines) override { |
| 481 | DictScope DD(P, "Lines"); |
| 482 | for (const auto &L : Lines) { |
| 483 | if (auto Result = getFileNameForOffset2(L.NameIndex)) |
| 484 | P.printString("FileName", Result.get()); |
| 485 | else |
| 486 | return Result.takeError(); |
| 487 | P.flush(); |
| 488 | for (const auto &N : L.LineNumbers) { |
| 489 | DictScope DDD(P, "Line"); |
| 490 | LineInfo LI(N.Flags); |
| 491 | P.printNumber("Offset", N.Offset); |
| 492 | if (LI.isAlwaysStepInto()) |
| 493 | P.printString("StepInto", StringRef("Always")); |
| 494 | else if (LI.isNeverStepInto()) |
| 495 | P.printString("StepInto", StringRef("Never")); |
| 496 | else |
| 497 | P.printNumber("LineNumberStart", LI.getStartLine()); |
| 498 | P.printNumber("EndDelta", LI.getLineDelta()); |
| 499 | P.printBoolean("IsStatement", LI.isStatement()); |
| 500 | } |
| 501 | for (const auto &C : L.Columns) { |
| 502 | DictScope DDD(P, "Column"); |
| 503 | P.printNumber("Start", C.StartColumn); |
| 504 | P.printNumber("End", C.EndColumn); |
| 505 | } |
| 506 | } |
| 507 | return Error::success(); |
| 508 | } |
| 509 | |
| 510 | private: |
| 511 | Expected<StringRef> getFileNameForOffset(uint32_t Offset) { |
| 512 | auto StringT = F.getStringTable(); |
| 513 | if (auto EC = StringT.takeError()) |
| 514 | return std::move(EC); |
| 515 | NameHashTable &ST = StringT.get(); |
| 516 | return ST.getStringForID(Offset); |
| 517 | } |
| 518 | Expected<StringRef> getFileNameForOffset2(uint32_t Offset) { |
| 519 | auto DbiS = F.getPDBDbiStream(); |
| 520 | if (auto EC = DbiS.takeError()) |
| 521 | return std::move(EC); |
| 522 | auto &DS = DbiS.get(); |
| 523 | return DS.getFileNameForIndex(Offset); |
| 524 | } |
| 525 | ScopedPrinter &P; |
| 526 | PDBFile &F; |
| 527 | }; |
| 528 | |
| 529 | RecordVisitor V(P, File); |
| 530 | for (const auto &L : ModS.lines(&HadError)) { |
| 531 | if (auto EC = codeview::visitModuleSubstream(L, V)) |
| 532 | return EC; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | return Error::success(); |
| 539 | } |
| 540 | |
| 541 | Error LLVMOutputStyle::dumpSectionContribs() { |
| 542 | if (!opts::DumpSectionContribs) |
| 543 | return Error::success(); |
| 544 | |
| 545 | auto DbiS = File.getPDBDbiStream(); |
| 546 | if (auto EC = DbiS.takeError()) |
| 547 | return EC; |
| 548 | DbiStream &DS = DbiS.get(); |
| 549 | ListScope L(P, "Section Contributions"); |
| 550 | class Visitor : public ISectionContribVisitor { |
| 551 | public: |
| 552 | Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {} |
| 553 | void visit(const SectionContrib &SC) override { |
| 554 | DictScope D(P, "Contribution"); |
| 555 | P.printNumber("ISect", SC.ISect); |
| 556 | P.printNumber("Off", SC.Off); |
| 557 | P.printNumber("Size", SC.Size); |
| 558 | P.printFlags("Characteristics", SC.Characteristics, |
| 559 | codeview::getImageSectionCharacteristicNames(), |
| 560 | COFF::SectionCharacteristics(0x00F00000)); |
| 561 | { |
| 562 | DictScope DD(P, "Module"); |
| 563 | P.printNumber("Index", SC.Imod); |
| 564 | auto M = DS.modules(); |
| 565 | if (M.size() > SC.Imod) { |
| 566 | P.printString("Name", M[SC.Imod].Info.getModuleName()); |
| 567 | } |
| 568 | } |
| 569 | P.printNumber("Data CRC", SC.DataCrc); |
| 570 | P.printNumber("Reloc CRC", SC.RelocCrc); |
| 571 | P.flush(); |
| 572 | } |
| 573 | void visit(const SectionContrib2 &SC) override { |
| 574 | visit(SC.Base); |
| 575 | P.printNumber("ISect Coff", SC.ISectCoff); |
| 576 | P.flush(); |
| 577 | } |
| 578 | |
| 579 | private: |
| 580 | ScopedPrinter &P; |
| 581 | DbiStream &DS; |
| 582 | }; |
| 583 | Visitor V(P, DS); |
| 584 | DS.visitSectionContributions(V); |
| 585 | return Error::success(); |
| 586 | } |
| 587 | |
| 588 | Error LLVMOutputStyle::dumpSectionMap() { |
| 589 | if (!opts::DumpSectionMap) |
| 590 | return Error::success(); |
| 591 | |
| 592 | auto DbiS = File.getPDBDbiStream(); |
| 593 | if (auto EC = DbiS.takeError()) |
| 594 | return EC; |
| 595 | DbiStream &DS = DbiS.get(); |
| 596 | ListScope L(P, "Section Map"); |
| 597 | for (auto &M : DS.getSectionMap()) { |
| 598 | DictScope D(P, "Entry"); |
| 599 | P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames()); |
| 600 | P.printNumber("Flags", M.Flags); |
| 601 | P.printNumber("Ovl", M.Ovl); |
| 602 | P.printNumber("Group", M.Group); |
| 603 | P.printNumber("Frame", M.Frame); |
| 604 | P.printNumber("SecName", M.SecName); |
| 605 | P.printNumber("ClassName", M.ClassName); |
| 606 | P.printNumber("Offset", M.Offset); |
| 607 | P.printNumber("SecByteLength", M.SecByteLength); |
| 608 | P.flush(); |
| 609 | } |
| 610 | return Error::success(); |
| 611 | } |
| 612 | |
| 613 | Error LLVMOutputStyle::dumpPublicsStream() { |
| 614 | if (!opts::DumpPublics) |
| 615 | return Error::success(); |
| 616 | |
| 617 | DictScope D(P, "Publics Stream"); |
| 618 | auto PublicsS = File.getPDBPublicsStream(); |
| 619 | if (auto EC = PublicsS.takeError()) |
| 620 | return EC; |
| 621 | PublicsStream &Publics = PublicsS.get(); |
| 622 | P.printNumber("Stream number", Publics.getStreamNum()); |
| 623 | P.printNumber("SymHash", Publics.getSymHash()); |
| 624 | P.printNumber("AddrMap", Publics.getAddrMap()); |
| 625 | P.printNumber("Number of buckets", Publics.getNumBuckets()); |
| 626 | P.printList("Hash Buckets", Publics.getHashBuckets()); |
| 627 | P.printList("Address Map", Publics.getAddressMap()); |
| 628 | P.printList("Thunk Map", Publics.getThunkMap()); |
| 629 | P.printList("Section Offsets", Publics.getSectionOffsets(), |
| 630 | printSectionOffset); |
| 631 | ListScope L(P, "Symbols"); |
| 632 | codeview::CVSymbolDumper SD(P, TD, nullptr, false); |
| 633 | bool HadError = false; |
| 634 | for (auto S : Publics.getSymbols(&HadError)) { |
| 635 | DictScope DD(P, ""); |
| 636 | |
| 637 | SD.dump(S); |
| 638 | if (opts::DumpSymRecordBytes) |
| 639 | P.printBinaryBlock("Bytes", S.Data); |
| 640 | } |
| 641 | if (HadError) |
| 642 | return make_error<RawError>( |
| 643 | raw_error_code::corrupt_file, |
| 644 | "Public symbol stream contained corrupt record"); |
| 645 | |
| 646 | return Error::success(); |
| 647 | } |
| 648 | |
| 649 | Error LLVMOutputStyle::dumpSectionHeaders() { |
| 650 | if (!opts::DumpSectionHeaders) |
| 651 | return Error::success(); |
| 652 | |
| 653 | auto DbiS = File.getPDBDbiStream(); |
| 654 | if (auto EC = DbiS.takeError()) |
| 655 | return EC; |
| 656 | DbiStream &DS = DbiS.get(); |
| 657 | |
| 658 | ListScope D(P, "Section Headers"); |
| 659 | for (const object::coff_section &Section : DS.getSectionHeaders()) { |
| 660 | DictScope DD(P, ""); |
| 661 | |
| 662 | // If a name is 8 characters long, there is no NUL character at end. |
| 663 | StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name))); |
| 664 | P.printString("Name", Name); |
| 665 | P.printNumber("Virtual Size", Section.VirtualSize); |
| 666 | P.printNumber("Virtual Address", Section.VirtualAddress); |
| 667 | P.printNumber("Size of Raw Data", Section.SizeOfRawData); |
| 668 | P.printNumber("File Pointer to Raw Data", Section.PointerToRawData); |
| 669 | P.printNumber("File Pointer to Relocations", Section.PointerToRelocations); |
| 670 | P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers); |
| 671 | P.printNumber("Number of Relocations", Section.NumberOfRelocations); |
| 672 | P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers); |
Rui Ueyama | 2c5384a | 2016-06-06 21:34:55 +0000 | [diff] [blame] | 673 | P.printFlags("Characteristics", Section.Characteristics, |
| 674 | getImageSectionCharacteristicNames()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 675 | } |
| 676 | return Error::success(); |
| 677 | } |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 678 | |
| 679 | Error LLVMOutputStyle::dumpFpoStream() { |
| 680 | if (!opts::DumpFpo) |
| 681 | return Error::success(); |
| 682 | |
| 683 | auto DbiS = File.getPDBDbiStream(); |
| 684 | if (auto EC = DbiS.takeError()) |
| 685 | return EC; |
| 686 | DbiStream &DS = DbiS.get(); |
| 687 | |
| 688 | ListScope D(P, "New FPO"); |
| 689 | for (const object::FpoData &Fpo : DS.getFpoRecords()) { |
| 690 | DictScope DD(P, ""); |
| 691 | P.printNumber("Offset", Fpo.Offset); |
| 692 | P.printNumber("Size", Fpo.Size); |
| 693 | P.printNumber("Number of locals", Fpo.NumLocals); |
| 694 | P.printNumber("Number of params", Fpo.NumParams); |
| 695 | P.printNumber("Size of Prolog", Fpo.getPrologSize()); |
| 696 | P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs()); |
| 697 | P.printBoolean("Has SEH", Fpo.hasSEH()); |
| 698 | P.printBoolean("Use BP", Fpo.useBP()); |
| 699 | P.printNumber("Frame Pointer", Fpo.getFP()); |
| 700 | } |
| 701 | return Error::success(); |
| 702 | } |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 703 | void LLVMOutputStyle::flush() { P.flush(); } |