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 | |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 12 | #include "CompactTypeDumpVisitor.h" |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 13 | #include "StreamUtil.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 14 | #include "llvm-pdbdump.h" |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 15 | |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/CVTypeDumper.h" |
| 17 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/EnumTables.h" |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame^] | 19 | #include "llvm/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/CodeView/ModuleDebugFragmentVisitor.h" |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame^] | 21 | #include "llvm/DebugInfo/CodeView/ModuleDebugLineFragment.h" |
| 22 | #include "llvm/DebugInfo/CodeView/ModuleDebugUnknownFragment.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/CodeView/TypeDatabaseVisitor.h" |
| 25 | #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" |
| 26 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
| 27 | #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h" |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 31 | #include "llvm/DebugInfo/PDB/Native/EnumTables.h" |
| 32 | #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h" |
| 33 | #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h" |
| 34 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 37 | #include "llvm/DebugInfo/PDB/Native/PublicsStream.h" |
| 38 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
| 39 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 40 | #include "llvm/DebugInfo/PDB/PDBExtras.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 41 | #include "llvm/Object/COFF.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 42 | #include "llvm/Support/BinaryStreamReader.h" |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 43 | #include "llvm/Support/FormatVariadic.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 44 | |
| 45 | #include <unordered_map> |
| 46 | |
| 47 | using namespace llvm; |
| 48 | using namespace llvm::codeview; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 49 | using namespace llvm::msf; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 50 | using namespace llvm::pdb; |
| 51 | |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | struct PageStats { |
| 54 | explicit PageStats(const BitVector &FreePages) |
| 55 | : Upm(FreePages), ActualUsedPages(FreePages.size()), |
| 56 | MultiUsePages(FreePages.size()), UseAfterFreePages(FreePages.size()) { |
| 57 | const_cast<BitVector &>(Upm).flip(); |
| 58 | // To calculate orphaned pages, we start with the set of pages that the |
| 59 | // MSF thinks are used. Each time we find one that actually *is* used, |
| 60 | // we unset it. Whichever bits remain set at the end are orphaned. |
| 61 | OrphanedPages = Upm; |
| 62 | } |
| 63 | |
| 64 | // The inverse of the MSF File's copy of the Fpm. The basis for which we |
| 65 | // determine the allocation status of each page. |
| 66 | const BitVector Upm; |
| 67 | |
| 68 | // Pages which are marked as used in the FPM and are used at least once. |
| 69 | BitVector ActualUsedPages; |
| 70 | |
| 71 | // Pages which are marked as used in the FPM but are used more than once. |
| 72 | BitVector MultiUsePages; |
| 73 | |
| 74 | // Pages which are marked as used in the FPM but are not used at all. |
| 75 | BitVector OrphanedPages; |
| 76 | |
| 77 | // Pages which are marked free in the FPM but are used. |
| 78 | BitVector UseAfterFreePages; |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | static void recordKnownUsedPage(PageStats &Stats, uint32_t UsedIndex) { |
| 83 | if (Stats.Upm.test(UsedIndex)) { |
| 84 | if (Stats.ActualUsedPages.test(UsedIndex)) |
| 85 | Stats.MultiUsePages.set(UsedIndex); |
| 86 | Stats.ActualUsedPages.set(UsedIndex); |
| 87 | Stats.OrphanedPages.reset(UsedIndex); |
| 88 | } else { |
| 89 | // The MSF doesn't think this page is used, but it is. |
| 90 | Stats.UseAfterFreePages.set(UsedIndex); |
| 91 | } |
| 92 | } |
| 93 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 94 | static void printSectionOffset(llvm::raw_ostream &OS, |
| 95 | const SectionOffset &Off) { |
| 96 | OS << Off.Off << ", " << Off.Isect; |
| 97 | } |
| 98 | |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 99 | LLVMOutputStyle::LLVMOutputStyle(PDBFile &File) : File(File), P(outs()) {} |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 100 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 101 | Error LLVMOutputStyle::dump() { |
| 102 | if (auto EC = dumpFileHeaders()) |
| 103 | return EC; |
| 104 | |
| 105 | if (auto EC = dumpStreamSummary()) |
| 106 | return EC; |
| 107 | |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 108 | if (auto EC = dumpFreePageMap()) |
| 109 | return EC; |
| 110 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 111 | if (auto EC = dumpStreamBlocks()) |
| 112 | return EC; |
| 113 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 114 | if (auto EC = dumpBlockRanges()) |
| 115 | return EC; |
| 116 | |
| 117 | if (auto EC = dumpStreamBytes()) |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 118 | return EC; |
| 119 | |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 120 | if (auto EC = dumpStringTable()) |
| 121 | return EC; |
| 122 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 123 | if (auto EC = dumpInfoStream()) |
| 124 | return EC; |
| 125 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 126 | if (auto EC = dumpTpiStream(StreamTPI)) |
| 127 | return EC; |
| 128 | |
| 129 | if (auto EC = dumpTpiStream(StreamIPI)) |
| 130 | return EC; |
| 131 | |
| 132 | if (auto EC = dumpDbiStream()) |
| 133 | return EC; |
| 134 | |
| 135 | if (auto EC = dumpSectionContribs()) |
| 136 | return EC; |
| 137 | |
| 138 | if (auto EC = dumpSectionMap()) |
| 139 | return EC; |
| 140 | |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 141 | if (auto EC = dumpGlobalsStream()) |
| 142 | return EC; |
| 143 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 144 | if (auto EC = dumpPublicsStream()) |
| 145 | return EC; |
| 146 | |
| 147 | if (auto EC = dumpSectionHeaders()) |
| 148 | return EC; |
| 149 | |
| 150 | if (auto EC = dumpFpoStream()) |
| 151 | return EC; |
| 152 | |
| 153 | flush(); |
| 154 | |
| 155 | return Error::success(); |
| 156 | } |
| 157 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 158 | Error LLVMOutputStyle::dumpFileHeaders() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 159 | if (!opts::raw::DumpHeaders) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 160 | return Error::success(); |
| 161 | |
| 162 | DictScope D(P, "FileHeaders"); |
| 163 | P.printNumber("BlockSize", File.getBlockSize()); |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 164 | P.printNumber("FreeBlockMap", File.getFreeBlockMapBlock()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 165 | P.printNumber("NumBlocks", File.getBlockCount()); |
| 166 | P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes()); |
| 167 | P.printNumber("Unknown1", File.getUnknown1()); |
| 168 | P.printNumber("BlockMapAddr", File.getBlockMapIndex()); |
| 169 | P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 170 | |
| 171 | // The directory is not contiguous. Instead, the block map contains a |
| 172 | // contiguous list of block numbers whose contents, when concatenated in |
| 173 | // order, make up the directory. |
| 174 | P.printList("DirectoryBlocks", File.getDirectoryBlockArray()); |
| 175 | P.printNumber("NumStreams", File.getNumStreams()); |
| 176 | return Error::success(); |
| 177 | } |
| 178 | |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 179 | Error LLVMOutputStyle::dumpStreamSummary() { |
| 180 | if (!opts::raw::DumpStreamSummary) |
| 181 | return Error::success(); |
| 182 | |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 183 | if (StreamPurposes.empty()) |
| 184 | discoverStreamPurposes(File, StreamPurposes); |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 185 | |
| 186 | uint32_t StreamCount = File.getNumStreams(); |
| 187 | |
| 188 | ListScope L(P, "Streams"); |
| 189 | for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
| 190 | std::string Label("Stream "); |
| 191 | Label += to_string(StreamIdx); |
| 192 | |
| 193 | std::string Value = "[" + StreamPurposes[StreamIdx] + "] ("; |
| 194 | Value += to_string(File.getStreamByteSize(StreamIdx)); |
| 195 | Value += " bytes)"; |
| 196 | |
| 197 | P.printString(Label, Value); |
| 198 | } |
Reid Kleckner | 11582c5 | 2016-06-17 20:38:01 +0000 | [diff] [blame] | 199 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 200 | P.flush(); |
| 201 | return Error::success(); |
| 202 | } |
| 203 | |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 204 | Error LLVMOutputStyle::dumpFreePageMap() { |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 205 | if (!opts::raw::DumpPageStats) |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 206 | return Error::success(); |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 207 | |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 208 | // Start with used pages instead of free pages because |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 209 | // the number of free pages is far larger than used pages. |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 210 | BitVector FPM = File.getMsfLayout().FreePageMap; |
| 211 | |
| 212 | PageStats PS(FPM); |
| 213 | |
| 214 | recordKnownUsedPage(PS, 0); // MSF Super Block |
| 215 | |
Zachary Turner | 8cf51c3 | 2016-08-03 16:53:21 +0000 | [diff] [blame] | 216 | uint32_t BlocksPerSection = msf::getFpmIntervalLength(File.getMsfLayout()); |
| 217 | uint32_t NumSections = msf::getNumFpmIntervals(File.getMsfLayout()); |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 218 | for (uint32_t I = 0; I < NumSections; ++I) { |
| 219 | uint32_t Fpm0 = 1 + BlocksPerSection * I; |
| 220 | // 2 Fpm blocks spaced at `getBlockSize()` block intervals |
| 221 | recordKnownUsedPage(PS, Fpm0); |
| 222 | recordKnownUsedPage(PS, Fpm0 + 1); |
| 223 | } |
| 224 | |
| 225 | recordKnownUsedPage(PS, File.getBlockMapIndex()); // Stream Table |
| 226 | |
Rui Ueyama | 22e6738 | 2016-08-02 23:22:46 +0000 | [diff] [blame] | 227 | for (auto DB : File.getDirectoryBlockArray()) |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 228 | recordKnownUsedPage(PS, DB); |
Rui Ueyama | 22e6738 | 2016-08-02 23:22:46 +0000 | [diff] [blame] | 229 | |
| 230 | // Record pages used by streams. Note that pages for stream 0 |
| 231 | // are considered being unused because that's what MSVC tools do. |
| 232 | // Stream 0 doesn't contain actual data, so it makes some sense, |
| 233 | // though it's a bit confusing to us. |
| 234 | for (auto &SE : File.getStreamMap().drop_front(1)) |
| 235 | for (auto &S : SE) |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 236 | recordKnownUsedPage(PS, S); |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 237 | |
| 238 | dumpBitVector("Msf Free Pages", FPM); |
| 239 | dumpBitVector("Orphaned Pages", PS.OrphanedPages); |
| 240 | dumpBitVector("Multiply Used Pages", PS.MultiUsePages); |
| 241 | dumpBitVector("Use After Free Pages", PS.UseAfterFreePages); |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 242 | return Error::success(); |
| 243 | } |
| 244 | |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 245 | void LLVMOutputStyle::dumpBitVector(StringRef Name, const BitVector &V) { |
| 246 | std::vector<uint32_t> Vec; |
| 247 | for (uint32_t I = 0, E = V.size(); I != E; ++I) |
| 248 | if (V[I]) |
| 249 | Vec.push_back(I); |
| 250 | P.printList(Name, Vec); |
| 251 | } |
| 252 | |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 253 | Error LLVMOutputStyle::dumpGlobalsStream() { |
| 254 | if (!opts::raw::DumpGlobals) |
| 255 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 256 | if (!File.hasPDBGlobalsStream()) { |
| 257 | P.printString("Globals Stream not present"); |
| 258 | return Error::success(); |
| 259 | } |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 260 | |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 261 | auto Globals = File.getPDBGlobalsStream(); |
| 262 | if (!Globals) |
Bob Haarman | 312fd0e | 2016-12-06 00:55:55 +0000 | [diff] [blame] | 263 | return Globals.takeError(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 264 | DictScope D(P, "Globals Stream"); |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 265 | |
| 266 | auto Dbi = File.getPDBDbiStream(); |
| 267 | if (!Dbi) |
| 268 | return Dbi.takeError(); |
| 269 | |
| 270 | P.printNumber("Stream number", Dbi->getGlobalSymbolStreamIndex()); |
| 271 | P.printNumber("Number of buckets", Globals->getNumBuckets()); |
| 272 | P.printList("Hash Buckets", Globals->getHashBuckets()); |
| 273 | |
| 274 | return Error::success(); |
| 275 | } |
| 276 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 277 | Error LLVMOutputStyle::dumpStreamBlocks() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 278 | if (!opts::raw::DumpStreamBlocks) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 279 | return Error::success(); |
| 280 | |
| 281 | ListScope L(P, "StreamBlocks"); |
| 282 | uint32_t StreamCount = File.getNumStreams(); |
| 283 | for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
| 284 | std::string Name("Stream "); |
| 285 | Name += to_string(StreamIdx); |
| 286 | auto StreamBlocks = File.getStreamBlockList(StreamIdx); |
| 287 | P.printList(Name, StreamBlocks); |
| 288 | } |
| 289 | return Error::success(); |
| 290 | } |
| 291 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 292 | Error LLVMOutputStyle::dumpBlockRanges() { |
| 293 | if (!opts::raw::DumpBlockRange.hasValue()) |
| 294 | return Error::success(); |
| 295 | auto &R = *opts::raw::DumpBlockRange; |
| 296 | uint32_t Max = R.Max.getValueOr(R.Min); |
| 297 | |
| 298 | if (Max < R.Min) |
| 299 | return make_error<StringError>( |
| 300 | "Invalid block range specified. Max < Min", |
| 301 | std::make_error_code(std::errc::bad_address)); |
| 302 | if (Max >= File.getBlockCount()) |
| 303 | return make_error<StringError>( |
| 304 | "Invalid block range specified. Requested block out of bounds", |
| 305 | std::make_error_code(std::errc::bad_address)); |
| 306 | |
| 307 | DictScope D(P, "Block Data"); |
| 308 | for (uint32_t I = R.Min; I <= Max; ++I) { |
| 309 | auto ExpectedData = File.getBlockData(I, File.getBlockSize()); |
| 310 | if (!ExpectedData) |
| 311 | return ExpectedData.takeError(); |
| 312 | std::string Label; |
| 313 | llvm::raw_string_ostream S(Label); |
| 314 | S << "Block " << I; |
| 315 | S.flush(); |
| 316 | P.printBinaryBlock(Label, *ExpectedData); |
| 317 | } |
| 318 | |
| 319 | return Error::success(); |
| 320 | } |
| 321 | |
| 322 | Error LLVMOutputStyle::dumpStreamBytes() { |
| 323 | if (opts::raw::DumpStreamData.empty()) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 324 | return Error::success(); |
| 325 | |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 326 | if (StreamPurposes.empty()) |
| 327 | discoverStreamPurposes(File, StreamPurposes); |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 328 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 329 | DictScope D(P, "Stream Data"); |
| 330 | for (uint32_t SI : opts::raw::DumpStreamData) { |
| 331 | if (SI >= File.getNumStreams()) |
| 332 | return make_error<RawError>(raw_error_code::no_stream); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 333 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 334 | auto S = MappedBlockStream::createIndexedStream(File.getMsfLayout(), |
| 335 | File.getMsfBuffer(), SI); |
| 336 | if (!S) |
| 337 | continue; |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 338 | DictScope DD(P, "Stream"); |
| 339 | |
| 340 | P.printNumber("Index", SI); |
| 341 | P.printString("Type", StreamPurposes[SI]); |
| 342 | P.printNumber("Size", S->getLength()); |
| 343 | auto Blocks = File.getMsfLayout().StreamMap[SI]; |
| 344 | P.printList("Blocks", Blocks); |
| 345 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 346 | BinaryStreamReader R(*S); |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 347 | ArrayRef<uint8_t> StreamData; |
| 348 | if (auto EC = R.readBytes(StreamData, S->getLength())) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 349 | return EC; |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 350 | P.printBinaryBlock("Data", StreamData); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 351 | } |
| 352 | return Error::success(); |
| 353 | } |
| 354 | |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 355 | Error LLVMOutputStyle::dumpStringTable() { |
| 356 | if (!opts::raw::DumpStringTable) |
| 357 | return Error::success(); |
| 358 | |
| 359 | auto IS = File.getStringTable(); |
| 360 | if (!IS) |
| 361 | return IS.takeError(); |
| 362 | |
| 363 | DictScope D(P, "String Table"); |
| 364 | for (uint32_t I : IS->name_ids()) { |
| 365 | StringRef S = IS->getStringForID(I); |
| 366 | if (!S.empty()) { |
| 367 | llvm::SmallString<32> Str; |
| 368 | Str.append("'"); |
| 369 | Str.append(S); |
| 370 | Str.append("'"); |
| 371 | P.printString(Str); |
| 372 | } |
| 373 | } |
| 374 | return Error::success(); |
| 375 | } |
| 376 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 377 | Error LLVMOutputStyle::dumpInfoStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 378 | if (!opts::raw::DumpHeaders) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 379 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 380 | if (!File.hasPDBInfoStream()) { |
| 381 | P.printString("PDB Stream not present"); |
| 382 | return Error::success(); |
| 383 | } |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 384 | auto IS = File.getPDBInfoStream(); |
| 385 | if (!IS) |
| 386 | return IS.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 387 | |
| 388 | DictScope D(P, "PDB Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 389 | P.printNumber("Version", IS->getVersion()); |
| 390 | P.printHex("Signature", IS->getSignature()); |
| 391 | P.printNumber("Age", IS->getAge()); |
| 392 | P.printObject("Guid", IS->getGuid()); |
Zachary Turner | 05d5e61 | 2017-03-16 20:19:11 +0000 | [diff] [blame] | 393 | P.printHex("Features", IS->getFeatures()); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 394 | { |
| 395 | DictScope DD(P, "Named Streams"); |
| 396 | for (const auto &S : IS->getNamedStreams().entries()) |
| 397 | P.printObject(S.getKey(), S.getValue()); |
| 398 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 399 | return Error::success(); |
| 400 | } |
| 401 | |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 402 | namespace { |
| 403 | class RecordBytesVisitor : public TypeVisitorCallbacks { |
| 404 | public: |
| 405 | explicit RecordBytesVisitor(ScopedPrinter &P) : P(P) {} |
| 406 | |
| 407 | Error visitTypeEnd(CVType &Record) override { |
| 408 | P.printBinaryBlock("Bytes", Record.content()); |
| 409 | return Error::success(); |
| 410 | } |
| 411 | |
| 412 | private: |
| 413 | ScopedPrinter &P; |
| 414 | }; |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 417 | Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) { |
| 418 | assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI); |
| 419 | |
| 420 | bool DumpRecordBytes = false; |
| 421 | bool DumpRecords = false; |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 422 | bool DumpTpiHash = false; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 423 | StringRef Label; |
| 424 | StringRef VerLabel; |
| 425 | if (StreamIdx == StreamTPI) { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 426 | if (!File.hasPDBTpiStream()) { |
| 427 | P.printString("Type Info Stream (TPI) not present"); |
| 428 | return Error::success(); |
| 429 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 430 | DumpRecordBytes = opts::raw::DumpTpiRecordBytes; |
| 431 | DumpRecords = opts::raw::DumpTpiRecords; |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 432 | DumpTpiHash = opts::raw::DumpTpiHash; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 433 | Label = "Type Info Stream (TPI)"; |
| 434 | VerLabel = "TPI Version"; |
| 435 | } else if (StreamIdx == StreamIPI) { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 436 | if (!File.hasPDBIpiStream()) { |
| 437 | P.printString("Type Info Stream (IPI) not present"); |
| 438 | return Error::success(); |
| 439 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 440 | DumpRecordBytes = opts::raw::DumpIpiRecordBytes; |
| 441 | DumpRecords = opts::raw::DumpIpiRecords; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 442 | Label = "Type Info Stream (IPI)"; |
| 443 | VerLabel = "IPI Version"; |
| 444 | } |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 445 | if (!DumpRecordBytes && !DumpRecords && !DumpTpiHash && |
| 446 | !opts::raw::DumpModuleSyms) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 447 | return Error::success(); |
| 448 | |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 449 | bool IsSilentDatabaseBuild = !DumpRecordBytes && !DumpRecords && !DumpTpiHash; |
| 450 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 451 | auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream() |
| 452 | : File.getPDBIpiStream(); |
| 453 | if (!Tpi) |
| 454 | return Tpi.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 455 | |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 456 | std::unique_ptr<DictScope> StreamScope; |
| 457 | std::unique_ptr<ListScope> RecordScope; |
| 458 | |
| 459 | if (!IsSilentDatabaseBuild) { |
| 460 | StreamScope = llvm::make_unique<DictScope>(P, Label); |
| 461 | P.printNumber(VerLabel, Tpi->getTpiVersion()); |
| 462 | P.printNumber("Record count", Tpi->NumTypeRecords()); |
| 463 | } |
| 464 | |
Reid Kleckner | a5d187b | 2017-03-23 21:36:25 +0000 | [diff] [blame] | 465 | TypeDatabase &StreamDB = (StreamIdx == StreamTPI) ? TypeDB : ItemDB; |
| 466 | |
| 467 | TypeDatabaseVisitor DBV(StreamDB); |
| 468 | CompactTypeDumpVisitor CTDV(StreamDB, &P); |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 469 | TypeDumpVisitor TDV(TypeDB, &P, false); |
Reid Kleckner | a5d187b | 2017-03-23 21:36:25 +0000 | [diff] [blame] | 470 | if (StreamIdx == StreamIPI) |
| 471 | TDV.setItemDB(ItemDB); |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 472 | RecordBytesVisitor RBV(P); |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 473 | TypeDeserializer Deserializer; |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 474 | |
| 475 | // We always need to deserialize and add it to the type database. This is |
| 476 | // true if even if we're not dumping anything, because we could need the |
| 477 | // type database for the purposes of dumping symbols. |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 478 | TypeVisitorCallbackPipeline Pipeline; |
| 479 | Pipeline.addCallbackToPipeline(Deserializer); |
| 480 | Pipeline.addCallbackToPipeline(DBV); |
| 481 | |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 482 | // If we're in dump mode, add a dumper with the appropriate detail level. |
| 483 | if (DumpRecords) { |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 484 | if (opts::raw::CompactRecords) |
| 485 | Pipeline.addCallbackToPipeline(CTDV); |
| 486 | else |
| 487 | Pipeline.addCallbackToPipeline(TDV); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 488 | } |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 489 | if (DumpRecordBytes) |
| 490 | Pipeline.addCallbackToPipeline(RBV); |
| 491 | |
| 492 | CVTypeVisitor Visitor(Pipeline); |
| 493 | |
| 494 | if (DumpRecords || DumpRecordBytes) |
| 495 | RecordScope = llvm::make_unique<ListScope>(P, "Records"); |
| 496 | |
| 497 | bool HadError = false; |
| 498 | |
| 499 | TypeIndex T(TypeIndex::FirstNonSimpleIndex); |
| 500 | for (auto Type : Tpi->types(&HadError)) { |
| 501 | std::unique_ptr<DictScope> OneRecordScope; |
| 502 | |
| 503 | if ((DumpRecords || DumpRecordBytes) && !opts::raw::CompactRecords) |
| 504 | OneRecordScope = llvm::make_unique<DictScope>(P, ""); |
| 505 | |
| 506 | if (auto EC = Visitor.visitTypeRecord(Type)) |
| 507 | return EC; |
| 508 | } |
| 509 | if (HadError) |
| 510 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 511 | "TPI stream contained corrupt record"); |
| 512 | |
| 513 | if (DumpTpiHash) { |
| 514 | DictScope DD(P, "Hash"); |
| 515 | P.printNumber("Number of Hash Buckets", Tpi->NumHashBuckets()); |
| 516 | P.printNumber("Hash Key Size", Tpi->getHashKeySize()); |
| 517 | P.printList("Values", Tpi->getHashValues()); |
| 518 | |
| 519 | ListScope LHA(P, "Adjusters"); |
| 520 | auto ExpectedST = File.getStringTable(); |
| 521 | if (!ExpectedST) |
| 522 | return ExpectedST.takeError(); |
| 523 | const auto &ST = *ExpectedST; |
| 524 | for (const auto &E : Tpi->getHashAdjusters()) { |
| 525 | DictScope DHA(P); |
| 526 | StringRef Name = ST.getStringForID(E.first); |
| 527 | P.printString("Type", Name); |
| 528 | P.printHex("TI", E.second); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (!IsSilentDatabaseBuild) { |
| 533 | ListScope L(P, "TypeIndexOffsets"); |
| 534 | for (const auto &IO : Tpi->getTypeIndexOffsets()) { |
| 535 | P.printString(formatv("Index: {0:x}, Offset: {1:N}", IO.Type.getIndex(), |
| 536 | (uint32_t)IO.Offset) |
| 537 | .str()); |
| 538 | } |
| 539 | } |
| 540 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 541 | P.flush(); |
| 542 | return Error::success(); |
| 543 | } |
| 544 | |
| 545 | Error LLVMOutputStyle::dumpDbiStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 546 | bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms || |
| 547 | opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo; |
| 548 | if (!opts::raw::DumpHeaders && !DumpModules) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 549 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 550 | if (!File.hasPDBDbiStream()) { |
| 551 | P.printString("DBI Stream not present"); |
| 552 | return Error::success(); |
| 553 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 554 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 555 | auto DS = File.getPDBDbiStream(); |
| 556 | if (!DS) |
| 557 | return DS.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 558 | |
| 559 | DictScope D(P, "DBI Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 560 | P.printNumber("Dbi Version", DS->getDbiVersion()); |
| 561 | P.printNumber("Age", DS->getAge()); |
| 562 | P.printBoolean("Incremental Linking", DS->isIncrementallyLinked()); |
| 563 | P.printBoolean("Has CTypes", DS->hasCTypes()); |
| 564 | P.printBoolean("Is Stripped", DS->isStripped()); |
| 565 | P.printObject("Machine Type", DS->getMachineType()); |
| 566 | P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex()); |
| 567 | P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex()); |
| 568 | P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 569 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 570 | uint16_t Major = DS->getBuildMajorVersion(); |
| 571 | uint16_t Minor = DS->getBuildMinorVersion(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 572 | P.printVersion("Toolchain Version", Major, Minor); |
| 573 | |
| 574 | std::string DllName; |
| 575 | raw_string_ostream DllStream(DllName); |
| 576 | DllStream << "mspdb" << Major << Minor << ".dll version"; |
| 577 | DllStream.flush(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 578 | P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 579 | |
| 580 | if (DumpModules) { |
| 581 | ListScope L(P, "Modules"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 582 | for (auto &Modi : DS->modules()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 583 | DictScope DD(P); |
| 584 | P.printString("Name", Modi.Info.getModuleName().str()); |
| 585 | P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex()); |
| 586 | P.printString("Object File Name", Modi.Info.getObjFileName().str()); |
| 587 | P.printNumber("Num Files", Modi.Info.getNumberOfFiles()); |
| 588 | P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex()); |
| 589 | P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex()); |
| 590 | P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize()); |
| 591 | P.printNumber("C13 Line Info Byte Size", |
| 592 | Modi.Info.getC13LineInfoByteSize()); |
| 593 | P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize()); |
| 594 | P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex()); |
| 595 | P.printBoolean("Has EC Info", Modi.Info.hasECInfo()); |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 596 | if (opts::raw::DumpModuleFiles) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 597 | std::string FileListName = |
| 598 | to_string(Modi.SourceFiles.size()) + " Contributing Source Files"; |
| 599 | ListScope LL(P, FileListName); |
| 600 | for (auto File : Modi.SourceFiles) |
| 601 | P.printString(File.str()); |
| 602 | } |
| 603 | bool HasModuleDI = |
| 604 | (Modi.Info.getModuleStreamIndex() < File.getNumStreams()); |
| 605 | bool ShouldDumpSymbols = |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 606 | (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes); |
| 607 | if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 608 | auto ModStreamData = MappedBlockStream::createIndexedStream( |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 609 | File.getMsfLayout(), File.getMsfBuffer(), |
| 610 | Modi.Info.getModuleStreamIndex()); |
| 611 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 612 | ModuleDebugStream ModS(Modi.Info, std::move(ModStreamData)); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 613 | if (auto EC = ModS.reload()) |
| 614 | return EC; |
| 615 | |
| 616 | if (ShouldDumpSymbols) { |
| 617 | ListScope SS(P, "Symbols"); |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 618 | codeview::CVSymbolDumper SD(P, TypeDB, nullptr, false); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 619 | bool HadError = false; |
Zachary Turner | 0d84074 | 2016-10-07 21:34:46 +0000 | [diff] [blame] | 620 | for (auto S : ModS.symbols(&HadError)) { |
| 621 | DictScope LL(P, ""); |
| 622 | if (opts::raw::DumpModuleSyms) { |
| 623 | if (auto EC = SD.dump(S)) { |
| 624 | llvm::consumeError(std::move(EC)); |
| 625 | HadError = true; |
| 626 | break; |
| 627 | } |
| 628 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 629 | if (opts::raw::DumpSymRecordBytes) |
Zachary Turner | c67b00c | 2016-09-14 23:00:16 +0000 | [diff] [blame] | 630 | P.printBinaryBlock("Bytes", S.content()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 631 | } |
| 632 | if (HadError) |
| 633 | return make_error<RawError>( |
| 634 | raw_error_code::corrupt_file, |
| 635 | "DBI stream contained corrupt symbol record"); |
| 636 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 637 | if (opts::raw::DumpLineInfo) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 638 | ListScope SS(P, "LineInfo"); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 639 | // Define a locally scoped visitor to print the different |
| 640 | // substream types types. |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 641 | class RecordVisitor : public codeview::ModuleDebugFragmentVisitor { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 642 | public: |
| 643 | RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {} |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame^] | 644 | Error visitUnknown(ModuleDebugUnknownFragment &Fragment) override { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 645 | DictScope DD(P, "Unknown"); |
| 646 | ArrayRef<uint8_t> Data; |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame^] | 647 | BinaryStreamReader R(Fragment.getData()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 648 | if (auto EC = R.readBytes(Data, R.bytesRemaining())) { |
| 649 | return make_error<RawError>( |
| 650 | raw_error_code::corrupt_file, |
| 651 | "DBI stream contained corrupt line info record"); |
| 652 | } |
| 653 | P.printBinaryBlock("Data", Data); |
| 654 | return Error::success(); |
| 655 | } |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame^] | 656 | Error visitFileChecksums( |
| 657 | ModuleDebugFileChecksumFragment &Checksums) override { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 658 | DictScope DD(P, "FileChecksums"); |
| 659 | for (const auto &C : Checksums) { |
| 660 | DictScope DDD(P, "Checksum"); |
| 661 | if (auto Result = getFileNameForOffset(C.FileNameOffset)) |
| 662 | P.printString("FileName", Result.get()); |
| 663 | else |
| 664 | return Result.takeError(); |
| 665 | P.flush(); |
| 666 | P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames()); |
| 667 | P.printBinaryBlock("Checksum", C.Checksum); |
| 668 | } |
| 669 | return Error::success(); |
| 670 | } |
| 671 | |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame^] | 672 | Error visitLines(ModuleDebugLineFragment &Lines) override { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 673 | DictScope DD(P, "Lines"); |
| 674 | for (const auto &L : Lines) { |
| 675 | if (auto Result = getFileNameForOffset2(L.NameIndex)) |
| 676 | P.printString("FileName", Result.get()); |
| 677 | else |
| 678 | return Result.takeError(); |
| 679 | P.flush(); |
| 680 | for (const auto &N : L.LineNumbers) { |
| 681 | DictScope DDD(P, "Line"); |
| 682 | LineInfo LI(N.Flags); |
| 683 | P.printNumber("Offset", N.Offset); |
| 684 | if (LI.isAlwaysStepInto()) |
| 685 | P.printString("StepInto", StringRef("Always")); |
| 686 | else if (LI.isNeverStepInto()) |
| 687 | P.printString("StepInto", StringRef("Never")); |
| 688 | else |
| 689 | P.printNumber("LineNumberStart", LI.getStartLine()); |
| 690 | P.printNumber("EndDelta", LI.getLineDelta()); |
| 691 | P.printBoolean("IsStatement", LI.isStatement()); |
| 692 | } |
| 693 | for (const auto &C : L.Columns) { |
| 694 | DictScope DDD(P, "Column"); |
| 695 | P.printNumber("Start", C.StartColumn); |
| 696 | P.printNumber("End", C.EndColumn); |
| 697 | } |
| 698 | } |
| 699 | return Error::success(); |
| 700 | } |
| 701 | |
| 702 | private: |
| 703 | Expected<StringRef> getFileNameForOffset(uint32_t Offset) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 704 | auto ST = F.getStringTable(); |
| 705 | if (!ST) |
| 706 | return ST.takeError(); |
| 707 | |
| 708 | return ST->getStringForID(Offset); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 709 | } |
| 710 | Expected<StringRef> getFileNameForOffset2(uint32_t Offset) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 711 | auto DS = F.getPDBDbiStream(); |
| 712 | if (!DS) |
| 713 | return DS.takeError(); |
| 714 | return DS->getFileNameForIndex(Offset); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 715 | } |
| 716 | ScopedPrinter &P; |
| 717 | PDBFile &F; |
| 718 | }; |
| 719 | |
| 720 | RecordVisitor V(P, File); |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame^] | 721 | for (const auto &L : ModS.linesAndChecksums()) { |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 722 | if (auto EC = codeview::visitModuleDebugFragment(L, V)) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 723 | return EC; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | return Error::success(); |
| 730 | } |
| 731 | |
| 732 | Error LLVMOutputStyle::dumpSectionContribs() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 733 | if (!opts::raw::DumpSectionContribs) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 734 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 735 | if (!File.hasPDBDbiStream()) { |
| 736 | P.printString("DBI Stream not present"); |
| 737 | return Error::success(); |
| 738 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 739 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 740 | auto Dbi = File.getPDBDbiStream(); |
| 741 | if (!Dbi) |
| 742 | return Dbi.takeError(); |
| 743 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 744 | ListScope L(P, "Section Contributions"); |
| 745 | class Visitor : public ISectionContribVisitor { |
| 746 | public: |
| 747 | Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {} |
| 748 | void visit(const SectionContrib &SC) override { |
| 749 | DictScope D(P, "Contribution"); |
| 750 | P.printNumber("ISect", SC.ISect); |
| 751 | P.printNumber("Off", SC.Off); |
| 752 | P.printNumber("Size", SC.Size); |
| 753 | P.printFlags("Characteristics", SC.Characteristics, |
| 754 | codeview::getImageSectionCharacteristicNames(), |
| 755 | COFF::SectionCharacteristics(0x00F00000)); |
| 756 | { |
| 757 | DictScope DD(P, "Module"); |
| 758 | P.printNumber("Index", SC.Imod); |
| 759 | auto M = DS.modules(); |
| 760 | if (M.size() > SC.Imod) { |
| 761 | P.printString("Name", M[SC.Imod].Info.getModuleName()); |
| 762 | } |
| 763 | } |
| 764 | P.printNumber("Data CRC", SC.DataCrc); |
| 765 | P.printNumber("Reloc CRC", SC.RelocCrc); |
| 766 | P.flush(); |
| 767 | } |
| 768 | void visit(const SectionContrib2 &SC) override { |
| 769 | visit(SC.Base); |
| 770 | P.printNumber("ISect Coff", SC.ISectCoff); |
| 771 | P.flush(); |
| 772 | } |
| 773 | |
| 774 | private: |
| 775 | ScopedPrinter &P; |
| 776 | DbiStream &DS; |
| 777 | }; |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 778 | Visitor V(P, *Dbi); |
| 779 | Dbi->visitSectionContributions(V); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 780 | return Error::success(); |
| 781 | } |
| 782 | |
| 783 | Error LLVMOutputStyle::dumpSectionMap() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 784 | if (!opts::raw::DumpSectionMap) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 785 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 786 | if (!File.hasPDBDbiStream()) { |
| 787 | P.printString("DBI Stream not present"); |
| 788 | return Error::success(); |
| 789 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 790 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 791 | auto Dbi = File.getPDBDbiStream(); |
| 792 | if (!Dbi) |
| 793 | return Dbi.takeError(); |
| 794 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 795 | ListScope L(P, "Section Map"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 796 | for (auto &M : Dbi->getSectionMap()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 797 | DictScope D(P, "Entry"); |
| 798 | P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 799 | P.printNumber("Ovl", M.Ovl); |
| 800 | P.printNumber("Group", M.Group); |
| 801 | P.printNumber("Frame", M.Frame); |
| 802 | P.printNumber("SecName", M.SecName); |
| 803 | P.printNumber("ClassName", M.ClassName); |
| 804 | P.printNumber("Offset", M.Offset); |
| 805 | P.printNumber("SecByteLength", M.SecByteLength); |
| 806 | P.flush(); |
| 807 | } |
| 808 | return Error::success(); |
| 809 | } |
| 810 | |
| 811 | Error LLVMOutputStyle::dumpPublicsStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 812 | if (!opts::raw::DumpPublics) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 813 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 814 | if (!File.hasPDBPublicsStream()) { |
| 815 | P.printString("Publics Stream not present"); |
| 816 | return Error::success(); |
| 817 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 818 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 819 | auto Publics = File.getPDBPublicsStream(); |
| 820 | if (!Publics) |
| 821 | return Publics.takeError(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 822 | DictScope D(P, "Publics Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 823 | |
| 824 | auto Dbi = File.getPDBDbiStream(); |
| 825 | if (!Dbi) |
| 826 | return Dbi.takeError(); |
| 827 | |
| 828 | P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex()); |
| 829 | P.printNumber("SymHash", Publics->getSymHash()); |
| 830 | P.printNumber("AddrMap", Publics->getAddrMap()); |
| 831 | P.printNumber("Number of buckets", Publics->getNumBuckets()); |
| 832 | P.printList("Hash Buckets", Publics->getHashBuckets()); |
| 833 | P.printList("Address Map", Publics->getAddressMap()); |
| 834 | P.printList("Thunk Map", Publics->getThunkMap()); |
| 835 | P.printList("Section Offsets", Publics->getSectionOffsets(), |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 836 | printSectionOffset); |
| 837 | ListScope L(P, "Symbols"); |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 838 | codeview::CVSymbolDumper SD(P, TypeDB, nullptr, false); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 839 | bool HadError = false; |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 840 | for (auto S : Publics->getSymbols(&HadError)) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 841 | DictScope DD(P, ""); |
| 842 | |
Zachary Turner | 0d84074 | 2016-10-07 21:34:46 +0000 | [diff] [blame] | 843 | if (auto EC = SD.dump(S)) { |
| 844 | HadError = true; |
| 845 | break; |
| 846 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 847 | if (opts::raw::DumpSymRecordBytes) |
Zachary Turner | c67b00c | 2016-09-14 23:00:16 +0000 | [diff] [blame] | 848 | P.printBinaryBlock("Bytes", S.content()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 849 | } |
| 850 | if (HadError) |
| 851 | return make_error<RawError>( |
| 852 | raw_error_code::corrupt_file, |
| 853 | "Public symbol stream contained corrupt record"); |
| 854 | |
| 855 | return Error::success(); |
| 856 | } |
| 857 | |
| 858 | Error LLVMOutputStyle::dumpSectionHeaders() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 859 | if (!opts::raw::DumpSectionHeaders) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 860 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 861 | if (!File.hasPDBDbiStream()) { |
| 862 | P.printString("DBI Stream not present"); |
| 863 | return Error::success(); |
| 864 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 865 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 866 | auto Dbi = File.getPDBDbiStream(); |
| 867 | if (!Dbi) |
| 868 | return Dbi.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 869 | |
| 870 | ListScope D(P, "Section Headers"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 871 | for (const object::coff_section &Section : Dbi->getSectionHeaders()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 872 | DictScope DD(P, ""); |
| 873 | |
| 874 | // If a name is 8 characters long, there is no NUL character at end. |
| 875 | StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name))); |
| 876 | P.printString("Name", Name); |
| 877 | P.printNumber("Virtual Size", Section.VirtualSize); |
| 878 | P.printNumber("Virtual Address", Section.VirtualAddress); |
| 879 | P.printNumber("Size of Raw Data", Section.SizeOfRawData); |
| 880 | P.printNumber("File Pointer to Raw Data", Section.PointerToRawData); |
| 881 | P.printNumber("File Pointer to Relocations", Section.PointerToRelocations); |
| 882 | P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers); |
| 883 | P.printNumber("Number of Relocations", Section.NumberOfRelocations); |
| 884 | P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers); |
Rui Ueyama | 2c5384a | 2016-06-06 21:34:55 +0000 | [diff] [blame] | 885 | P.printFlags("Characteristics", Section.Characteristics, |
| 886 | getImageSectionCharacteristicNames()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 887 | } |
| 888 | return Error::success(); |
| 889 | } |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 890 | |
| 891 | Error LLVMOutputStyle::dumpFpoStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 892 | if (!opts::raw::DumpFpo) |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 893 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 894 | if (!File.hasPDBDbiStream()) { |
| 895 | P.printString("DBI Stream not present"); |
| 896 | return Error::success(); |
| 897 | } |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 898 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 899 | auto Dbi = File.getPDBDbiStream(); |
| 900 | if (!Dbi) |
| 901 | return Dbi.takeError(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 902 | |
| 903 | ListScope D(P, "New FPO"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 904 | for (const object::FpoData &Fpo : Dbi->getFpoRecords()) { |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 905 | DictScope DD(P, ""); |
| 906 | P.printNumber("Offset", Fpo.Offset); |
| 907 | P.printNumber("Size", Fpo.Size); |
| 908 | P.printNumber("Number of locals", Fpo.NumLocals); |
| 909 | P.printNumber("Number of params", Fpo.NumParams); |
| 910 | P.printNumber("Size of Prolog", Fpo.getPrologSize()); |
| 911 | P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs()); |
| 912 | P.printBoolean("Has SEH", Fpo.hasSEH()); |
| 913 | P.printBoolean("Use BP", Fpo.useBP()); |
| 914 | P.printNumber("Frame Pointer", Fpo.getFP()); |
| 915 | } |
| 916 | return Error::success(); |
| 917 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 918 | |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 919 | void LLVMOutputStyle::flush() { P.flush(); } |